blob: b395edb97f53bb07f2dab4ea78edad7ee9344719 [file] [log] [blame]
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01002 * User-space Probes (UProbes)
Srikar Dronamraju2b144492012-02-09 14:56:42 +05303 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
Ingo Molnar35aa6212012-02-22 11:37:29 +010018 * Copyright (C) IBM Corporation, 2008-2012
Srikar Dronamraju2b144492012-02-09 14:56:42 +053019 * Authors:
20 * Srikar Dronamraju
21 * Jim Keniston
Ingo Molnar35aa6212012-02-22 11:37:29 +010022 * Copyright (C) 2011-2012 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
Srikar Dronamraju2b144492012-02-09 14:56:42 +053023 */
24
25#include <linux/kernel.h>
26#include <linux/highmem.h>
27#include <linux/pagemap.h> /* read_mapping_page */
28#include <linux/slab.h>
29#include <linux/sched.h>
30#include <linux/rmap.h> /* anon_vma_prepare */
31#include <linux/mmu_notifier.h> /* set_pte_at_notify */
32#include <linux/swap.h> /* try_to_free_swap */
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +053033#include <linux/ptrace.h> /* user_enable_single_step */
34#include <linux/kdebug.h> /* notifier mechanism */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010035
Srikar Dronamraju2b144492012-02-09 14:56:42 +053036#include <linux/uprobes.h>
37
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +053038#define UINSNS_PER_PAGE (PAGE_SIZE/UPROBE_XOL_SLOT_BYTES)
39#define MAX_UPROBE_XOL_SLOTS UINSNS_PER_PAGE
40
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +053041static struct srcu_struct uprobes_srcu;
Srikar Dronamraju2b144492012-02-09 14:56:42 +053042static struct rb_root uprobes_tree = RB_ROOT;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010043
Srikar Dronamraju2b144492012-02-09 14:56:42 +053044static DEFINE_SPINLOCK(uprobes_treelock); /* serialize rbtree access */
45
46#define UPROBES_HASH_SZ 13
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010047
Srikar Dronamraju2b144492012-02-09 14:56:42 +053048/* serialize (un)register */
49static struct mutex uprobes_mutex[UPROBES_HASH_SZ];
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010050
51#define uprobes_hash(v) (&uprobes_mutex[((unsigned long)(v)) % UPROBES_HASH_SZ])
Srikar Dronamraju2b144492012-02-09 14:56:42 +053052
53/* serialize uprobe->pending_list */
54static struct mutex uprobes_mmap_mutex[UPROBES_HASH_SZ];
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010055#define uprobes_mmap_hash(v) (&uprobes_mmap_mutex[((unsigned long)(v)) % UPROBES_HASH_SZ])
Srikar Dronamraju2b144492012-02-09 14:56:42 +053056
57/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010058 * uprobe_events allows us to skip the uprobe_mmap if there are no uprobe
Srikar Dronamraju2b144492012-02-09 14:56:42 +053059 * events active at this time. Probably a fine grained per inode count is
60 * better?
61 */
62static atomic_t uprobe_events = ATOMIC_INIT(0);
63
64/*
65 * Maintain a temporary per vma info that can be used to search if a vma
66 * has already been handled. This structure is introduced since extending
67 * vm_area_struct wasnt recommended.
68 */
69struct vma_info {
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010070 struct list_head probe_list;
71 struct mm_struct *mm;
72 loff_t vaddr;
Srikar Dronamraju2b144492012-02-09 14:56:42 +053073};
74
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +053075struct uprobe {
76 struct rb_node rb_node; /* node in the rb tree */
77 atomic_t ref;
78 struct rw_semaphore consumer_rwsem;
79 struct list_head pending_list;
80 struct uprobe_consumer *consumers;
81 struct inode *inode; /* Also hold a ref to inode */
82 loff_t offset;
83 int flags;
84 struct arch_uprobe arch;
85};
86
Srikar Dronamraju2b144492012-02-09 14:56:42 +053087/*
88 * valid_vma: Verify if the specified vma is an executable vma
89 * Relax restrictions while unregistering: vm_flags might have
90 * changed after breakpoint was inserted.
91 * - is_register: indicates if we are in register context.
92 * - Return 1 if the specified virtual address is in an
93 * executable vma.
94 */
95static bool valid_vma(struct vm_area_struct *vma, bool is_register)
96{
97 if (!vma->vm_file)
98 return false;
99
100 if (!is_register)
101 return true;
102
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100103 if ((vma->vm_flags & (VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)) == (VM_READ|VM_EXEC))
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530104 return true;
105
106 return false;
107}
108
109static loff_t vma_address(struct vm_area_struct *vma, loff_t offset)
110{
111 loff_t vaddr;
112
113 vaddr = vma->vm_start + offset;
114 vaddr -= vma->vm_pgoff << PAGE_SHIFT;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100115
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530116 return vaddr;
117}
118
119/**
120 * __replace_page - replace page in vma by new page.
121 * based on replace_page in mm/ksm.c
122 *
123 * @vma: vma that holds the pte pointing to page
124 * @page: the cowed page we are replacing by kpage
125 * @kpage: the modified page we replace page by
126 *
127 * Returns 0 on success, -EFAULT on failure.
128 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100129static int __replace_page(struct vm_area_struct *vma, struct page *page, struct page *kpage)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530130{
131 struct mm_struct *mm = vma->vm_mm;
132 pgd_t *pgd;
133 pud_t *pud;
134 pmd_t *pmd;
135 pte_t *ptep;
136 spinlock_t *ptl;
137 unsigned long addr;
138 int err = -EFAULT;
139
140 addr = page_address_in_vma(page, vma);
141 if (addr == -EFAULT)
142 goto out;
143
144 pgd = pgd_offset(mm, addr);
145 if (!pgd_present(*pgd))
146 goto out;
147
148 pud = pud_offset(pgd, addr);
149 if (!pud_present(*pud))
150 goto out;
151
152 pmd = pmd_offset(pud, addr);
153 if (!pmd_present(*pmd))
154 goto out;
155
156 ptep = pte_offset_map_lock(mm, pmd, addr, &ptl);
157 if (!ptep)
158 goto out;
159
160 get_page(kpage);
161 page_add_new_anon_rmap(kpage, vma, addr);
162
163 flush_cache_page(vma, addr, pte_pfn(*ptep));
164 ptep_clear_flush(vma, addr, ptep);
165 set_pte_at_notify(mm, addr, ptep, mk_pte(kpage, vma->vm_page_prot));
166
167 page_remove_rmap(page);
168 if (!page_mapped(page))
169 try_to_free_swap(page);
170 put_page(page);
171 pte_unmap_unlock(ptep, ptl);
172 err = 0;
173
174out:
175 return err;
176}
177
178/**
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530179 * is_swbp_insn - check if instruction is breakpoint instruction.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530180 * @insn: instruction to be checked.
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530181 * Default implementation of is_swbp_insn
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530182 * Returns true if @insn is a breakpoint instruction.
183 */
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530184bool __weak is_swbp_insn(uprobe_opcode_t *insn)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530185{
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530186 return *insn == UPROBE_SWBP_INSN;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530187}
188
189/*
190 * NOTE:
191 * Expect the breakpoint instruction to be the smallest size instruction for
192 * the architecture. If an arch has variable length instruction and the
193 * breakpoint instruction is not of the smallest length instruction
194 * supported by that architecture then we need to modify read_opcode /
195 * write_opcode accordingly. This would never be a problem for archs that
196 * have fixed length instructions.
197 */
198
199/*
200 * write_opcode - write the opcode at a given virtual address.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530201 * @auprobe: arch breakpointing information.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530202 * @mm: the probed process address space.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530203 * @vaddr: the virtual address to store the opcode.
204 * @opcode: opcode to be written at @vaddr.
205 *
206 * Called with mm->mmap_sem held (for read and with a reference to
207 * mm).
208 *
209 * For mm @mm, write the opcode at @vaddr.
210 * Return 0 (success) or a negative errno.
211 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530212static int write_opcode(struct arch_uprobe *auprobe, struct mm_struct *mm,
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530213 unsigned long vaddr, uprobe_opcode_t opcode)
214{
215 struct page *old_page, *new_page;
216 struct address_space *mapping;
217 void *vaddr_old, *vaddr_new;
218 struct vm_area_struct *vma;
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +0530219 struct uprobe *uprobe;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530220 loff_t addr;
221 int ret;
222
223 /* Read the page with vaddr into memory */
224 ret = get_user_pages(NULL, mm, vaddr, 1, 0, 0, &old_page, &vma);
225 if (ret <= 0)
226 return ret;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100227
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530228 ret = -EINVAL;
229
230 /*
231 * We are interested in text pages only. Our pages of interest
232 * should be mapped for read and execute only. We desist from
233 * adding probes in write mapped pages since the breakpoints
234 * might end up in the file copy.
235 */
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530236 if (!valid_vma(vma, is_swbp_insn(&opcode)))
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530237 goto put_out;
238
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +0530239 uprobe = container_of(auprobe, struct uprobe, arch);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530240 mapping = uprobe->inode->i_mapping;
241 if (mapping != vma->vm_file->f_mapping)
242 goto put_out;
243
244 addr = vma_address(vma, uprobe->offset);
245 if (vaddr != (unsigned long)addr)
246 goto put_out;
247
248 ret = -ENOMEM;
249 new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vaddr);
250 if (!new_page)
251 goto put_out;
252
253 __SetPageUptodate(new_page);
254
255 /*
256 * lock page will serialize against do_wp_page()'s
257 * PageAnon() handling
258 */
259 lock_page(old_page);
260 /* copy the page now that we've got it stable */
261 vaddr_old = kmap_atomic(old_page);
262 vaddr_new = kmap_atomic(new_page);
263
264 memcpy(vaddr_new, vaddr_old, PAGE_SIZE);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100265
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530266 /* poke the new insn in, ASSUMES we don't cross page boundary */
267 vaddr &= ~PAGE_MASK;
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530268 BUG_ON(vaddr + UPROBE_SWBP_INSN_SIZE > PAGE_SIZE);
269 memcpy(vaddr_new + vaddr, &opcode, UPROBE_SWBP_INSN_SIZE);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530270
271 kunmap_atomic(vaddr_new);
272 kunmap_atomic(vaddr_old);
273
274 ret = anon_vma_prepare(vma);
275 if (ret)
276 goto unlock_out;
277
278 lock_page(new_page);
279 ret = __replace_page(vma, old_page, new_page);
280 unlock_page(new_page);
281
282unlock_out:
283 unlock_page(old_page);
284 page_cache_release(new_page);
285
286put_out:
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100287 put_page(old_page);
288
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530289 return ret;
290}
291
292/**
293 * read_opcode - read the opcode at a given virtual address.
294 * @mm: the probed process address space.
295 * @vaddr: the virtual address to read the opcode.
296 * @opcode: location to store the read opcode.
297 *
298 * Called with mm->mmap_sem held (for read and with a reference to
299 * mm.
300 *
301 * For mm @mm, read the opcode at @vaddr and store it in @opcode.
302 * Return 0 (success) or a negative errno.
303 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100304static int read_opcode(struct mm_struct *mm, unsigned long vaddr, uprobe_opcode_t *opcode)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530305{
306 struct page *page;
307 void *vaddr_new;
308 int ret;
309
310 ret = get_user_pages(NULL, mm, vaddr, 1, 0, 0, &page, NULL);
311 if (ret <= 0)
312 return ret;
313
314 lock_page(page);
315 vaddr_new = kmap_atomic(page);
316 vaddr &= ~PAGE_MASK;
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530317 memcpy(opcode, vaddr_new + vaddr, UPROBE_SWBP_INSN_SIZE);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530318 kunmap_atomic(vaddr_new);
319 unlock_page(page);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100320
321 put_page(page);
322
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530323 return 0;
324}
325
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530326static int is_swbp_at_addr(struct mm_struct *mm, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530327{
328 uprobe_opcode_t opcode;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100329 int result;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530330
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100331 result = read_opcode(mm, vaddr, &opcode);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530332 if (result)
333 return result;
334
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530335 if (is_swbp_insn(&opcode))
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530336 return 1;
337
338 return 0;
339}
340
341/**
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530342 * set_swbp - store breakpoint at a given address.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530343 * @auprobe: arch specific probepoint information.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530344 * @mm: the probed process address space.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530345 * @vaddr: the virtual address to insert the opcode.
346 *
347 * For mm @mm, store the breakpoint instruction at @vaddr.
348 * Return 0 (success) or a negative errno.
349 */
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530350int __weak set_swbp(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530351{
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100352 int result;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530353
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530354 result = is_swbp_at_addr(mm, vaddr);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530355 if (result == 1)
356 return -EEXIST;
357
358 if (result)
359 return result;
360
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530361 return write_opcode(auprobe, mm, vaddr, UPROBE_SWBP_INSN);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530362}
363
364/**
365 * set_orig_insn - Restore the original instruction.
366 * @mm: the probed process address space.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530367 * @auprobe: arch specific probepoint information.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530368 * @vaddr: the virtual address to insert the opcode.
369 * @verify: if true, verify existance of breakpoint instruction.
370 *
371 * For mm @mm, restore the original opcode (opcode) at @vaddr.
372 * Return 0 (success) or a negative errno.
373 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100374int __weak
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530375set_orig_insn(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr, bool verify)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530376{
377 if (verify) {
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100378 int result;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530379
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530380 result = is_swbp_at_addr(mm, vaddr);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530381 if (!result)
382 return -EINVAL;
383
384 if (result != 1)
385 return result;
386 }
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530387 return write_opcode(auprobe, mm, vaddr, *(uprobe_opcode_t *)auprobe->insn);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530388}
389
390static int match_uprobe(struct uprobe *l, struct uprobe *r)
391{
392 if (l->inode < r->inode)
393 return -1;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100394
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530395 if (l->inode > r->inode)
396 return 1;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530397
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100398 if (l->offset < r->offset)
399 return -1;
400
401 if (l->offset > r->offset)
402 return 1;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530403
404 return 0;
405}
406
407static struct uprobe *__find_uprobe(struct inode *inode, loff_t offset)
408{
409 struct uprobe u = { .inode = inode, .offset = offset };
410 struct rb_node *n = uprobes_tree.rb_node;
411 struct uprobe *uprobe;
412 int match;
413
414 while (n) {
415 uprobe = rb_entry(n, struct uprobe, rb_node);
416 match = match_uprobe(&u, uprobe);
417 if (!match) {
418 atomic_inc(&uprobe->ref);
419 return uprobe;
420 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100421
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530422 if (match < 0)
423 n = n->rb_left;
424 else
425 n = n->rb_right;
426 }
427 return NULL;
428}
429
430/*
431 * Find a uprobe corresponding to a given inode:offset
432 * Acquires uprobes_treelock
433 */
434static struct uprobe *find_uprobe(struct inode *inode, loff_t offset)
435{
436 struct uprobe *uprobe;
437 unsigned long flags;
438
439 spin_lock_irqsave(&uprobes_treelock, flags);
440 uprobe = __find_uprobe(inode, offset);
441 spin_unlock_irqrestore(&uprobes_treelock, flags);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100442
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530443 return uprobe;
444}
445
446static struct uprobe *__insert_uprobe(struct uprobe *uprobe)
447{
448 struct rb_node **p = &uprobes_tree.rb_node;
449 struct rb_node *parent = NULL;
450 struct uprobe *u;
451 int match;
452
453 while (*p) {
454 parent = *p;
455 u = rb_entry(parent, struct uprobe, rb_node);
456 match = match_uprobe(uprobe, u);
457 if (!match) {
458 atomic_inc(&u->ref);
459 return u;
460 }
461
462 if (match < 0)
463 p = &parent->rb_left;
464 else
465 p = &parent->rb_right;
466
467 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100468
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530469 u = NULL;
470 rb_link_node(&uprobe->rb_node, parent, p);
471 rb_insert_color(&uprobe->rb_node, &uprobes_tree);
472 /* get access + creation ref */
473 atomic_set(&uprobe->ref, 2);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100474
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530475 return u;
476}
477
478/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100479 * Acquire uprobes_treelock.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530480 * Matching uprobe already exists in rbtree;
481 * increment (access refcount) and return the matching uprobe.
482 *
483 * No matching uprobe; insert the uprobe in rb_tree;
484 * get a double refcount (access + creation) and return NULL.
485 */
486static struct uprobe *insert_uprobe(struct uprobe *uprobe)
487{
488 unsigned long flags;
489 struct uprobe *u;
490
491 spin_lock_irqsave(&uprobes_treelock, flags);
492 u = __insert_uprobe(uprobe);
493 spin_unlock_irqrestore(&uprobes_treelock, flags);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100494
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530495 /* For now assume that the instruction need not be single-stepped */
496 uprobe->flags |= UPROBE_SKIP_SSTEP;
497
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530498 return u;
499}
500
501static void put_uprobe(struct uprobe *uprobe)
502{
503 if (atomic_dec_and_test(&uprobe->ref))
504 kfree(uprobe);
505}
506
507static struct uprobe *alloc_uprobe(struct inode *inode, loff_t offset)
508{
509 struct uprobe *uprobe, *cur_uprobe;
510
511 uprobe = kzalloc(sizeof(struct uprobe), GFP_KERNEL);
512 if (!uprobe)
513 return NULL;
514
515 uprobe->inode = igrab(inode);
516 uprobe->offset = offset;
517 init_rwsem(&uprobe->consumer_rwsem);
518 INIT_LIST_HEAD(&uprobe->pending_list);
519
520 /* add to uprobes_tree, sorted on inode:offset */
521 cur_uprobe = insert_uprobe(uprobe);
522
523 /* a uprobe exists for this inode:offset combination */
524 if (cur_uprobe) {
525 kfree(uprobe);
526 uprobe = cur_uprobe;
527 iput(inode);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100528 } else {
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530529 atomic_inc(&uprobe_events);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100530 }
531
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530532 return uprobe;
533}
534
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530535static void handler_chain(struct uprobe *uprobe, struct pt_regs *regs)
536{
537 struct uprobe_consumer *uc;
538
539 if (!(uprobe->flags & UPROBE_RUN_HANDLER))
540 return;
541
542 down_read(&uprobe->consumer_rwsem);
543 for (uc = uprobe->consumers; uc; uc = uc->next) {
544 if (!uc->filter || uc->filter(uc, current))
545 uc->handler(uc, regs);
546 }
547 up_read(&uprobe->consumer_rwsem);
548}
549
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530550/* Returns the previous consumer */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100551static struct uprobe_consumer *
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530552consumer_add(struct uprobe *uprobe, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530553{
554 down_write(&uprobe->consumer_rwsem);
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530555 uc->next = uprobe->consumers;
556 uprobe->consumers = uc;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530557 up_write(&uprobe->consumer_rwsem);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100558
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530559 return uc->next;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530560}
561
562/*
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530563 * For uprobe @uprobe, delete the consumer @uc.
564 * Return true if the @uc is deleted successfully
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530565 * or return false.
566 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530567static bool consumer_del(struct uprobe *uprobe, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530568{
569 struct uprobe_consumer **con;
570 bool ret = false;
571
572 down_write(&uprobe->consumer_rwsem);
573 for (con = &uprobe->consumers; *con; con = &(*con)->next) {
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530574 if (*con == uc) {
575 *con = uc->next;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530576 ret = true;
577 break;
578 }
579 }
580 up_write(&uprobe->consumer_rwsem);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100581
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530582 return ret;
583}
584
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530585static int
586__copy_insn(struct address_space *mapping, struct vm_area_struct *vma, char *insn,
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530587 unsigned long nbytes, unsigned long offset)
588{
589 struct file *filp = vma->vm_file;
590 struct page *page;
591 void *vaddr;
592 unsigned long off1;
593 unsigned long idx;
594
595 if (!filp)
596 return -EINVAL;
597
598 idx = (unsigned long)(offset >> PAGE_CACHE_SHIFT);
599 off1 = offset &= ~PAGE_MASK;
600
601 /*
602 * Ensure that the page that has the original instruction is
603 * populated and in page-cache.
604 */
605 page = read_mapping_page(mapping, idx, filp);
606 if (IS_ERR(page))
607 return PTR_ERR(page);
608
609 vaddr = kmap_atomic(page);
610 memcpy(insn, vaddr + off1, nbytes);
611 kunmap_atomic(vaddr);
612 page_cache_release(page);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100613
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530614 return 0;
615}
616
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530617static int
618copy_insn(struct uprobe *uprobe, struct vm_area_struct *vma, unsigned long addr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530619{
620 struct address_space *mapping;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530621 unsigned long nbytes;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100622 int bytes;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530623
624 addr &= ~PAGE_MASK;
625 nbytes = PAGE_SIZE - addr;
626 mapping = uprobe->inode->i_mapping;
627
628 /* Instruction at end of binary; copy only available bytes */
629 if (uprobe->offset + MAX_UINSN_BYTES > uprobe->inode->i_size)
630 bytes = uprobe->inode->i_size - uprobe->offset;
631 else
632 bytes = MAX_UINSN_BYTES;
633
634 /* Instruction at the page-boundary; copy bytes in second page */
635 if (nbytes < bytes) {
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +0530636 if (__copy_insn(mapping, vma, uprobe->arch.insn + nbytes,
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530637 bytes - nbytes, uprobe->offset + nbytes))
638 return -ENOMEM;
639
640 bytes = nbytes;
641 }
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +0530642 return __copy_insn(mapping, vma, uprobe->arch.insn, bytes, uprobe->offset);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530643}
644
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530645static int
646install_breakpoint(struct uprobe *uprobe, struct mm_struct *mm,
647 struct vm_area_struct *vma, loff_t vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530648{
649 unsigned long addr;
650 int ret;
651
652 /*
653 * If probe is being deleted, unregister thread could be done with
654 * the vma-rmap-walk through. Adding a probe now can be fatal since
655 * nobody will be able to cleanup. Also we could be from fork or
656 * mremap path, where the probe might have already been inserted.
657 * Hence behave as if probe already existed.
658 */
659 if (!uprobe->consumers)
660 return -EEXIST;
661
662 addr = (unsigned long)vaddr;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100663
Srikar Dronamraju900771a2012-03-12 14:55:14 +0530664 if (!(uprobe->flags & UPROBE_COPY_INSN)) {
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530665 ret = copy_insn(uprobe, vma, addr);
666 if (ret)
667 return ret;
668
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530669 if (is_swbp_insn((uprobe_opcode_t *)uprobe->arch.insn))
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530670 return -EEXIST;
671
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530672 ret = arch_uprobe_analyze_insn(&uprobe->arch, mm);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530673 if (ret)
674 return ret;
675
Srikar Dronamraju900771a2012-03-12 14:55:14 +0530676 uprobe->flags |= UPROBE_COPY_INSN;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530677 }
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530678 ret = set_swbp(&uprobe->arch, mm, addr);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530679
680 return ret;
681}
682
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530683static void
684remove_breakpoint(struct uprobe *uprobe, struct mm_struct *mm, loff_t vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530685{
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530686 set_orig_insn(&uprobe->arch, mm, (unsigned long)vaddr, true);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530687}
688
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530689/*
690 * There could be threads that have hit the breakpoint and are entering the
691 * notifier code and trying to acquire the uprobes_treelock. The thread
692 * calling delete_uprobe() that is removing the uprobe from the rb_tree can
693 * race with these threads and might acquire the uprobes_treelock compared
694 * to some of the breakpoint hit threads. In such a case, the breakpoint
695 * hit threads will not find the uprobe. The current unregistering thread
696 * waits till all other threads have hit a breakpoint, to acquire the
697 * uprobes_treelock before the uprobe is removed from the rbtree.
698 */
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530699static void delete_uprobe(struct uprobe *uprobe)
700{
701 unsigned long flags;
702
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530703 synchronize_srcu(&uprobes_srcu);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530704 spin_lock_irqsave(&uprobes_treelock, flags);
705 rb_erase(&uprobe->rb_node, &uprobes_tree);
706 spin_unlock_irqrestore(&uprobes_treelock, flags);
707 iput(uprobe->inode);
708 put_uprobe(uprobe);
709 atomic_dec(&uprobe_events);
710}
711
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530712static struct vma_info *
713__find_next_vma_info(struct address_space *mapping, struct list_head *head,
714 struct vma_info *vi, loff_t offset, bool is_register)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530715{
716 struct prio_tree_iter iter;
717 struct vm_area_struct *vma;
718 struct vma_info *tmpvi;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100719 unsigned long pgoff;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530720 int existing_vma;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100721 loff_t vaddr;
722
723 pgoff = offset >> PAGE_SHIFT;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530724
725 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
726 if (!valid_vma(vma, is_register))
727 continue;
728
729 existing_vma = 0;
730 vaddr = vma_address(vma, offset);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100731
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530732 list_for_each_entry(tmpvi, head, probe_list) {
733 if (tmpvi->mm == vma->vm_mm && tmpvi->vaddr == vaddr) {
734 existing_vma = 1;
735 break;
736 }
737 }
738
739 /*
740 * Another vma needs a probe to be installed. However skip
741 * installing the probe if the vma is about to be unlinked.
742 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100743 if (!existing_vma && atomic_inc_not_zero(&vma->vm_mm->mm_users)) {
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530744 vi->mm = vma->vm_mm;
745 vi->vaddr = vaddr;
746 list_add(&vi->probe_list, head);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100747
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530748 return vi;
749 }
750 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100751
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530752 return NULL;
753}
754
755/*
756 * Iterate in the rmap prio tree and find a vma where a probe has not
757 * yet been inserted.
758 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100759static struct vma_info *
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530760find_next_vma_info(struct address_space *mapping, struct list_head *head,
761 loff_t offset, bool is_register)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530762{
763 struct vma_info *vi, *retvi;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100764
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530765 vi = kzalloc(sizeof(struct vma_info), GFP_KERNEL);
766 if (!vi)
767 return ERR_PTR(-ENOMEM);
768
769 mutex_lock(&mapping->i_mmap_mutex);
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530770 retvi = __find_next_vma_info(mapping, head, vi, offset, is_register);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530771 mutex_unlock(&mapping->i_mmap_mutex);
772
773 if (!retvi)
774 kfree(vi);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100775
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530776 return retvi;
777}
778
779static int register_for_each_vma(struct uprobe *uprobe, bool is_register)
780{
781 struct list_head try_list;
782 struct vm_area_struct *vma;
783 struct address_space *mapping;
784 struct vma_info *vi, *tmpvi;
785 struct mm_struct *mm;
786 loff_t vaddr;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100787 int ret;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530788
789 mapping = uprobe->inode->i_mapping;
790 INIT_LIST_HEAD(&try_list);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100791
792 ret = 0;
793
794 for (;;) {
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530795 vi = find_next_vma_info(mapping, &try_list, uprobe->offset, is_register);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100796 if (!vi)
797 break;
798
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530799 if (IS_ERR(vi)) {
800 ret = PTR_ERR(vi);
801 break;
802 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100803
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530804 mm = vi->mm;
805 down_read(&mm->mmap_sem);
806 vma = find_vma(mm, (unsigned long)vi->vaddr);
807 if (!vma || !valid_vma(vma, is_register)) {
808 list_del(&vi->probe_list);
809 kfree(vi);
810 up_read(&mm->mmap_sem);
811 mmput(mm);
812 continue;
813 }
814 vaddr = vma_address(vma, uprobe->offset);
815 if (vma->vm_file->f_mapping->host != uprobe->inode ||
816 vaddr != vi->vaddr) {
817 list_del(&vi->probe_list);
818 kfree(vi);
819 up_read(&mm->mmap_sem);
820 mmput(mm);
821 continue;
822 }
823
824 if (is_register)
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530825 ret = install_breakpoint(uprobe, mm, vma, vi->vaddr);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530826 else
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530827 remove_breakpoint(uprobe, mm, vi->vaddr);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530828
829 up_read(&mm->mmap_sem);
830 mmput(mm);
831 if (is_register) {
832 if (ret && ret == -EEXIST)
833 ret = 0;
834 if (ret)
835 break;
836 }
837 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100838
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530839 list_for_each_entry_safe(vi, tmpvi, &try_list, probe_list) {
840 list_del(&vi->probe_list);
841 kfree(vi);
842 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100843
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530844 return ret;
845}
846
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100847static int __uprobe_register(struct uprobe *uprobe)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530848{
849 return register_for_each_vma(uprobe, true);
850}
851
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100852static void __uprobe_unregister(struct uprobe *uprobe)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530853{
854 if (!register_for_each_vma(uprobe, false))
855 delete_uprobe(uprobe);
856
857 /* TODO : cant unregister? schedule a worker thread */
858}
859
860/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100861 * uprobe_register - register a probe
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530862 * @inode: the file in which the probe has to be placed.
863 * @offset: offset from the start of the file.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530864 * @uc: information on howto handle the probe..
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530865 *
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100866 * Apart from the access refcount, uprobe_register() takes a creation
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530867 * refcount (thro alloc_uprobe) if and only if this @uprobe is getting
868 * inserted into the rbtree (i.e first consumer for a @inode:@offset
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100869 * tuple). Creation refcount stops uprobe_unregister from freeing the
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530870 * @uprobe even before the register operation is complete. Creation
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530871 * refcount is released when the last @uc for the @uprobe
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530872 * unregisters.
873 *
874 * Return errno if it cannot successully install probes
875 * else return 0 (success)
876 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530877int uprobe_register(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530878{
879 struct uprobe *uprobe;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100880 int ret;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530881
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530882 if (!inode || !uc || uc->next)
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100883 return -EINVAL;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530884
885 if (offset > i_size_read(inode))
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100886 return -EINVAL;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530887
888 ret = 0;
889 mutex_lock(uprobes_hash(inode));
890 uprobe = alloc_uprobe(inode, offset);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100891
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530892 if (uprobe && !consumer_add(uprobe, uc)) {
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100893 ret = __uprobe_register(uprobe);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530894 if (ret) {
895 uprobe->consumers = NULL;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100896 __uprobe_unregister(uprobe);
897 } else {
Srikar Dronamraju900771a2012-03-12 14:55:14 +0530898 uprobe->flags |= UPROBE_RUN_HANDLER;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100899 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530900 }
901
902 mutex_unlock(uprobes_hash(inode));
903 put_uprobe(uprobe);
904
905 return ret;
906}
907
908/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100909 * uprobe_unregister - unregister a already registered probe.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530910 * @inode: the file in which the probe has to be removed.
911 * @offset: offset from the start of the file.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530912 * @uc: identify which probe if multiple probes are colocated.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530913 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530914void uprobe_unregister(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530915{
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100916 struct uprobe *uprobe;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530917
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530918 if (!inode || !uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530919 return;
920
921 uprobe = find_uprobe(inode, offset);
922 if (!uprobe)
923 return;
924
925 mutex_lock(uprobes_hash(inode));
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530926
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530927 if (consumer_del(uprobe, uc)) {
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100928 if (!uprobe->consumers) {
929 __uprobe_unregister(uprobe);
Srikar Dronamraju900771a2012-03-12 14:55:14 +0530930 uprobe->flags &= ~UPROBE_RUN_HANDLER;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100931 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530932 }
933
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530934 mutex_unlock(uprobes_hash(inode));
935 if (uprobe)
936 put_uprobe(uprobe);
937}
938
939/*
940 * Of all the nodes that correspond to the given inode, return the node
941 * with the least offset.
942 */
943static struct rb_node *find_least_offset_node(struct inode *inode)
944{
945 struct uprobe u = { .inode = inode, .offset = 0};
946 struct rb_node *n = uprobes_tree.rb_node;
947 struct rb_node *close_node = NULL;
948 struct uprobe *uprobe;
949 int match;
950
951 while (n) {
952 uprobe = rb_entry(n, struct uprobe, rb_node);
953 match = match_uprobe(&u, uprobe);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100954
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530955 if (uprobe->inode == inode)
956 close_node = n;
957
958 if (!match)
959 return close_node;
960
961 if (match < 0)
962 n = n->rb_left;
963 else
964 n = n->rb_right;
965 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100966
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530967 return close_node;
968}
969
970/*
971 * For a given inode, build a list of probes that need to be inserted.
972 */
973static void build_probe_list(struct inode *inode, struct list_head *head)
974{
975 struct uprobe *uprobe;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530976 unsigned long flags;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100977 struct rb_node *n;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530978
979 spin_lock_irqsave(&uprobes_treelock, flags);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100980
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530981 n = find_least_offset_node(inode);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100982
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530983 for (; n; n = rb_next(n)) {
984 uprobe = rb_entry(n, struct uprobe, rb_node);
985 if (uprobe->inode != inode)
986 break;
987
988 list_add(&uprobe->pending_list, head);
989 atomic_inc(&uprobe->ref);
990 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100991
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530992 spin_unlock_irqrestore(&uprobes_treelock, flags);
993}
994
995/*
996 * Called from mmap_region.
997 * called with mm->mmap_sem acquired.
998 *
999 * Return -ve no if we fail to insert probes and we cannot
1000 * bail-out.
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001001 * Return 0 otherwise. i.e:
1002 *
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301003 * - successful insertion of probes
1004 * - (or) no possible probes to be inserted.
1005 * - (or) insertion of probes failed but we can bail-out.
1006 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001007int uprobe_mmap(struct vm_area_struct *vma)
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301008{
1009 struct list_head tmp_list;
1010 struct uprobe *uprobe, *u;
1011 struct inode *inode;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001012 int ret;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301013
1014 if (!atomic_read(&uprobe_events) || !valid_vma(vma, true))
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001015 return 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301016
1017 inode = vma->vm_file->f_mapping->host;
1018 if (!inode)
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001019 return 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301020
1021 INIT_LIST_HEAD(&tmp_list);
1022 mutex_lock(uprobes_mmap_hash(inode));
1023 build_probe_list(inode, &tmp_list);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001024
1025 ret = 0;
1026
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301027 list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
1028 loff_t vaddr;
1029
1030 list_del(&uprobe->pending_list);
1031 if (!ret) {
1032 vaddr = vma_address(vma, uprobe->offset);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001033 if (vaddr >= vma->vm_start && vaddr < vma->vm_end) {
Srikar Dronamrajue3343e62012-03-12 14:55:30 +05301034 ret = install_breakpoint(uprobe, vma->vm_mm, vma, vaddr);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001035 /* Ignore double add: */
1036 if (ret == -EEXIST)
1037 ret = 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301038 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301039 }
1040 put_uprobe(uprobe);
1041 }
1042
1043 mutex_unlock(uprobes_mmap_hash(inode));
1044
1045 return ret;
1046}
1047
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301048/* Slot allocation for XOL */
1049static int xol_add_vma(struct xol_area *area)
1050{
1051 struct mm_struct *mm;
1052 int ret;
1053
1054 area->page = alloc_page(GFP_HIGHUSER);
1055 if (!area->page)
1056 return -ENOMEM;
1057
1058 ret = -EALREADY;
1059 mm = current->mm;
1060
1061 down_write(&mm->mmap_sem);
1062 if (mm->uprobes_state.xol_area)
1063 goto fail;
1064
1065 ret = -ENOMEM;
1066
1067 /* Try to map as high as possible, this is only a hint. */
1068 area->vaddr = get_unmapped_area(NULL, TASK_SIZE - PAGE_SIZE, PAGE_SIZE, 0, 0);
1069 if (area->vaddr & ~PAGE_MASK) {
1070 ret = area->vaddr;
1071 goto fail;
1072 }
1073
1074 ret = install_special_mapping(mm, area->vaddr, PAGE_SIZE,
1075 VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO, &area->page);
1076 if (ret)
1077 goto fail;
1078
1079 smp_wmb(); /* pairs with get_xol_area() */
1080 mm->uprobes_state.xol_area = area;
1081 ret = 0;
1082
1083fail:
1084 up_write(&mm->mmap_sem);
1085 if (ret)
1086 __free_page(area->page);
1087
1088 return ret;
1089}
1090
1091static struct xol_area *get_xol_area(struct mm_struct *mm)
1092{
1093 struct xol_area *area;
1094
1095 area = mm->uprobes_state.xol_area;
1096 smp_read_barrier_depends(); /* pairs with wmb in xol_add_vma() */
1097
1098 return area;
1099}
1100
1101/*
1102 * xol_alloc_area - Allocate process's xol_area.
1103 * This area will be used for storing instructions for execution out of
1104 * line.
1105 *
1106 * Returns the allocated area or NULL.
1107 */
1108static struct xol_area *xol_alloc_area(void)
1109{
1110 struct xol_area *area;
1111
1112 area = kzalloc(sizeof(*area), GFP_KERNEL);
1113 if (unlikely(!area))
1114 return NULL;
1115
1116 area->bitmap = kzalloc(BITS_TO_LONGS(UINSNS_PER_PAGE) * sizeof(long), GFP_KERNEL);
1117
1118 if (!area->bitmap)
1119 goto fail;
1120
1121 init_waitqueue_head(&area->wq);
1122 if (!xol_add_vma(area))
1123 return area;
1124
1125fail:
1126 kfree(area->bitmap);
1127 kfree(area);
1128
1129 return get_xol_area(current->mm);
1130}
1131
1132/*
1133 * uprobe_clear_state - Free the area allocated for slots.
1134 */
1135void uprobe_clear_state(struct mm_struct *mm)
1136{
1137 struct xol_area *area = mm->uprobes_state.xol_area;
1138
1139 if (!area)
1140 return;
1141
1142 put_page(area->page);
1143 kfree(area->bitmap);
1144 kfree(area);
1145}
1146
1147/*
1148 * uprobe_reset_state - Free the area allocated for slots.
1149 */
1150void uprobe_reset_state(struct mm_struct *mm)
1151{
1152 mm->uprobes_state.xol_area = NULL;
1153}
1154
1155/*
1156 * - search for a free slot.
1157 */
1158static unsigned long xol_take_insn_slot(struct xol_area *area)
1159{
1160 unsigned long slot_addr;
1161 int slot_nr;
1162
1163 do {
1164 slot_nr = find_first_zero_bit(area->bitmap, UINSNS_PER_PAGE);
1165 if (slot_nr < UINSNS_PER_PAGE) {
1166 if (!test_and_set_bit(slot_nr, area->bitmap))
1167 break;
1168
1169 slot_nr = UINSNS_PER_PAGE;
1170 continue;
1171 }
1172 wait_event(area->wq, (atomic_read(&area->slot_count) < UINSNS_PER_PAGE));
1173 } while (slot_nr >= UINSNS_PER_PAGE);
1174
1175 slot_addr = area->vaddr + (slot_nr * UPROBE_XOL_SLOT_BYTES);
1176 atomic_inc(&area->slot_count);
1177
1178 return slot_addr;
1179}
1180
1181/*
1182 * xol_get_insn_slot - If was not allocated a slot, then
1183 * allocate a slot.
1184 * Returns the allocated slot address or 0.
1185 */
1186static unsigned long xol_get_insn_slot(struct uprobe *uprobe, unsigned long slot_addr)
1187{
1188 struct xol_area *area;
1189 unsigned long offset;
1190 void *vaddr;
1191
1192 area = get_xol_area(current->mm);
1193 if (!area) {
1194 area = xol_alloc_area();
1195 if (!area)
1196 return 0;
1197 }
1198 current->utask->xol_vaddr = xol_take_insn_slot(area);
1199
1200 /*
1201 * Initialize the slot if xol_vaddr points to valid
1202 * instruction slot.
1203 */
1204 if (unlikely(!current->utask->xol_vaddr))
1205 return 0;
1206
1207 current->utask->vaddr = slot_addr;
1208 offset = current->utask->xol_vaddr & ~PAGE_MASK;
1209 vaddr = kmap_atomic(area->page);
1210 memcpy(vaddr + offset, uprobe->arch.insn, MAX_UINSN_BYTES);
1211 kunmap_atomic(vaddr);
1212
1213 return current->utask->xol_vaddr;
1214}
1215
1216/*
1217 * xol_free_insn_slot - If slot was earlier allocated by
1218 * @xol_get_insn_slot(), make the slot available for
1219 * subsequent requests.
1220 */
1221static void xol_free_insn_slot(struct task_struct *tsk)
1222{
1223 struct xol_area *area;
1224 unsigned long vma_end;
1225 unsigned long slot_addr;
1226
1227 if (!tsk->mm || !tsk->mm->uprobes_state.xol_area || !tsk->utask)
1228 return;
1229
1230 slot_addr = tsk->utask->xol_vaddr;
1231
1232 if (unlikely(!slot_addr || IS_ERR_VALUE(slot_addr)))
1233 return;
1234
1235 area = tsk->mm->uprobes_state.xol_area;
1236 vma_end = area->vaddr + PAGE_SIZE;
1237 if (area->vaddr <= slot_addr && slot_addr < vma_end) {
1238 unsigned long offset;
1239 int slot_nr;
1240
1241 offset = slot_addr - area->vaddr;
1242 slot_nr = offset / UPROBE_XOL_SLOT_BYTES;
1243 if (slot_nr >= UINSNS_PER_PAGE)
1244 return;
1245
1246 clear_bit(slot_nr, area->bitmap);
1247 atomic_dec(&area->slot_count);
1248 if (waitqueue_active(&area->wq))
1249 wake_up(&area->wq);
1250
1251 tsk->utask->xol_vaddr = 0;
1252 }
1253}
1254
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301255/**
1256 * uprobe_get_swbp_addr - compute address of swbp given post-swbp regs
1257 * @regs: Reflects the saved state of the task after it has hit a breakpoint
1258 * instruction.
1259 * Return the address of the breakpoint instruction.
1260 */
1261unsigned long __weak uprobe_get_swbp_addr(struct pt_regs *regs)
1262{
1263 return instruction_pointer(regs) - UPROBE_SWBP_INSN_SIZE;
1264}
1265
1266/*
1267 * Called with no locks held.
1268 * Called in context of a exiting or a exec-ing thread.
1269 */
1270void uprobe_free_utask(struct task_struct *t)
1271{
1272 struct uprobe_task *utask = t->utask;
1273
1274 if (t->uprobe_srcu_id != -1)
1275 srcu_read_unlock_raw(&uprobes_srcu, t->uprobe_srcu_id);
1276
1277 if (!utask)
1278 return;
1279
1280 if (utask->active_uprobe)
1281 put_uprobe(utask->active_uprobe);
1282
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301283 xol_free_insn_slot(t);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301284 kfree(utask);
1285 t->utask = NULL;
1286}
1287
1288/*
1289 * Called in context of a new clone/fork from copy_process.
1290 */
1291void uprobe_copy_process(struct task_struct *t)
1292{
1293 t->utask = NULL;
1294 t->uprobe_srcu_id = -1;
1295}
1296
1297/*
1298 * Allocate a uprobe_task object for the task.
1299 * Called when the thread hits a breakpoint for the first time.
1300 *
1301 * Returns:
1302 * - pointer to new uprobe_task on success
1303 * - NULL otherwise
1304 */
1305static struct uprobe_task *add_utask(void)
1306{
1307 struct uprobe_task *utask;
1308
1309 utask = kzalloc(sizeof *utask, GFP_KERNEL);
1310 if (unlikely(!utask))
1311 return NULL;
1312
1313 utask->active_uprobe = NULL;
1314 current->utask = utask;
1315 return utask;
1316}
1317
1318/* Prepare to single-step probed instruction out of line. */
1319static int
1320pre_ssout(struct uprobe *uprobe, struct pt_regs *regs, unsigned long vaddr)
1321{
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301322 if (xol_get_insn_slot(uprobe, vaddr) && !arch_uprobe_pre_xol(&uprobe->arch, regs))
1323 return 0;
1324
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301325 return -EFAULT;
1326}
1327
1328/*
1329 * If we are singlestepping, then ensure this thread is not connected to
1330 * non-fatal signals until completion of singlestep. When xol insn itself
1331 * triggers the signal, restart the original insn even if the task is
1332 * already SIGKILL'ed (since coredump should report the correct ip). This
1333 * is even more important if the task has a handler for SIGSEGV/etc, The
1334 * _same_ instruction should be repeated again after return from the signal
1335 * handler, and SSTEP can never finish in this case.
1336 */
1337bool uprobe_deny_signal(void)
1338{
1339 struct task_struct *t = current;
1340 struct uprobe_task *utask = t->utask;
1341
1342 if (likely(!utask || !utask->active_uprobe))
1343 return false;
1344
1345 WARN_ON_ONCE(utask->state != UTASK_SSTEP);
1346
1347 if (signal_pending(t)) {
1348 spin_lock_irq(&t->sighand->siglock);
1349 clear_tsk_thread_flag(t, TIF_SIGPENDING);
1350 spin_unlock_irq(&t->sighand->siglock);
1351
1352 if (__fatal_signal_pending(t) || arch_uprobe_xol_was_trapped(t)) {
1353 utask->state = UTASK_SSTEP_TRAPPED;
1354 set_tsk_thread_flag(t, TIF_UPROBE);
1355 set_tsk_thread_flag(t, TIF_NOTIFY_RESUME);
1356 }
1357 }
1358
1359 return true;
1360}
1361
1362/*
1363 * Avoid singlestepping the original instruction if the original instruction
1364 * is a NOP or can be emulated.
1365 */
1366static bool can_skip_sstep(struct uprobe *uprobe, struct pt_regs *regs)
1367{
1368 if (arch_uprobe_skip_sstep(&uprobe->arch, regs))
1369 return true;
1370
1371 uprobe->flags &= ~UPROBE_SKIP_SSTEP;
1372 return false;
1373}
1374
1375/*
1376 * Run handler and ask thread to singlestep.
1377 * Ensure all non-fatal signals cannot interrupt thread while it singlesteps.
1378 */
1379static void handle_swbp(struct pt_regs *regs)
1380{
1381 struct vm_area_struct *vma;
1382 struct uprobe_task *utask;
1383 struct uprobe *uprobe;
1384 struct mm_struct *mm;
1385 unsigned long bp_vaddr;
1386
1387 uprobe = NULL;
1388 bp_vaddr = uprobe_get_swbp_addr(regs);
1389 mm = current->mm;
1390 down_read(&mm->mmap_sem);
1391 vma = find_vma(mm, bp_vaddr);
1392
1393 if (vma && vma->vm_start <= bp_vaddr && valid_vma(vma, false)) {
1394 struct inode *inode;
1395 loff_t offset;
1396
1397 inode = vma->vm_file->f_mapping->host;
1398 offset = bp_vaddr - vma->vm_start;
1399 offset += (vma->vm_pgoff << PAGE_SHIFT);
1400 uprobe = find_uprobe(inode, offset);
1401 }
1402
1403 srcu_read_unlock_raw(&uprobes_srcu, current->uprobe_srcu_id);
1404 current->uprobe_srcu_id = -1;
1405 up_read(&mm->mmap_sem);
1406
1407 if (!uprobe) {
1408 /* No matching uprobe; signal SIGTRAP. */
1409 send_sig(SIGTRAP, current, 0);
1410 return;
1411 }
1412
1413 utask = current->utask;
1414 if (!utask) {
1415 utask = add_utask();
1416 /* Cannot allocate; re-execute the instruction. */
1417 if (!utask)
1418 goto cleanup_ret;
1419 }
1420 utask->active_uprobe = uprobe;
1421 handler_chain(uprobe, regs);
1422 if (uprobe->flags & UPROBE_SKIP_SSTEP && can_skip_sstep(uprobe, regs))
1423 goto cleanup_ret;
1424
1425 utask->state = UTASK_SSTEP;
1426 if (!pre_ssout(uprobe, regs, bp_vaddr)) {
1427 user_enable_single_step(current);
1428 return;
1429 }
1430
1431cleanup_ret:
1432 if (utask) {
1433 utask->active_uprobe = NULL;
1434 utask->state = UTASK_RUNNING;
1435 }
1436 if (uprobe) {
1437 if (!(uprobe->flags & UPROBE_SKIP_SSTEP))
1438
1439 /*
1440 * cannot singlestep; cannot skip instruction;
1441 * re-execute the instruction.
1442 */
1443 instruction_pointer_set(regs, bp_vaddr);
1444
1445 put_uprobe(uprobe);
1446 }
1447}
1448
1449/*
1450 * Perform required fix-ups and disable singlestep.
1451 * Allow pending signals to take effect.
1452 */
1453static void handle_singlestep(struct uprobe_task *utask, struct pt_regs *regs)
1454{
1455 struct uprobe *uprobe;
1456
1457 uprobe = utask->active_uprobe;
1458 if (utask->state == UTASK_SSTEP_ACK)
1459 arch_uprobe_post_xol(&uprobe->arch, regs);
1460 else if (utask->state == UTASK_SSTEP_TRAPPED)
1461 arch_uprobe_abort_xol(&uprobe->arch, regs);
1462 else
1463 WARN_ON_ONCE(1);
1464
1465 put_uprobe(uprobe);
1466 utask->active_uprobe = NULL;
1467 utask->state = UTASK_RUNNING;
1468 user_disable_single_step(current);
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301469 xol_free_insn_slot(current);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301470
1471 spin_lock_irq(&current->sighand->siglock);
1472 recalc_sigpending(); /* see uprobe_deny_signal() */
1473 spin_unlock_irq(&current->sighand->siglock);
1474}
1475
1476/*
1477 * On breakpoint hit, breakpoint notifier sets the TIF_UPROBE flag. (and on
1478 * subsequent probe hits on the thread sets the state to UTASK_BP_HIT) and
1479 * allows the thread to return from interrupt.
1480 *
1481 * On singlestep exception, singlestep notifier sets the TIF_UPROBE flag and
1482 * also sets the state to UTASK_SSTEP_ACK and allows the thread to return from
1483 * interrupt.
1484 *
1485 * While returning to userspace, thread notices the TIF_UPROBE flag and calls
1486 * uprobe_notify_resume().
1487 */
1488void uprobe_notify_resume(struct pt_regs *regs)
1489{
1490 struct uprobe_task *utask;
1491
1492 utask = current->utask;
1493 if (!utask || utask->state == UTASK_BP_HIT)
1494 handle_swbp(regs);
1495 else
1496 handle_singlestep(utask, regs);
1497}
1498
1499/*
1500 * uprobe_pre_sstep_notifier gets called from interrupt context as part of
1501 * notifier mechanism. Set TIF_UPROBE flag and indicate breakpoint hit.
1502 */
1503int uprobe_pre_sstep_notifier(struct pt_regs *regs)
1504{
1505 struct uprobe_task *utask;
1506
1507 if (!current->mm)
1508 return 0;
1509
1510 utask = current->utask;
1511 if (utask)
1512 utask->state = UTASK_BP_HIT;
1513
1514 set_thread_flag(TIF_UPROBE);
1515 current->uprobe_srcu_id = srcu_read_lock_raw(&uprobes_srcu);
1516
1517 return 1;
1518}
1519
1520/*
1521 * uprobe_post_sstep_notifier gets called in interrupt context as part of notifier
1522 * mechanism. Set TIF_UPROBE flag and indicate completion of singlestep.
1523 */
1524int uprobe_post_sstep_notifier(struct pt_regs *regs)
1525{
1526 struct uprobe_task *utask = current->utask;
1527
1528 if (!current->mm || !utask || !utask->active_uprobe)
1529 /* task is currently not uprobed */
1530 return 0;
1531
1532 utask->state = UTASK_SSTEP_ACK;
1533 set_thread_flag(TIF_UPROBE);
1534 return 1;
1535}
1536
1537static struct notifier_block uprobe_exception_nb = {
1538 .notifier_call = arch_uprobe_exception_notify,
1539 .priority = INT_MAX-1, /* notified after kprobes, kgdb */
1540};
1541
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301542static int __init init_uprobes(void)
1543{
1544 int i;
1545
1546 for (i = 0; i < UPROBES_HASH_SZ; i++) {
1547 mutex_init(&uprobes_mutex[i]);
1548 mutex_init(&uprobes_mmap_mutex[i]);
1549 }
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301550 init_srcu_struct(&uprobes_srcu);
1551
1552 return register_die_notifier(&uprobe_exception_nb);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301553}
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301554module_init(init_uprobes);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301555
1556static void __exit exit_uprobes(void)
1557{
1558}
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301559module_exit(exit_uprobes);