]>
Commit | Line | Data |
---|---|---|
2b144498 | 1 | /* |
7b2d81d4 | 2 | * User-space Probes (UProbes) |
2b144498 SD |
3 | * |
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 | * | |
35aa621b | 18 | * Copyright (C) IBM Corporation, 2008-2012 |
2b144498 SD |
19 | * Authors: |
20 | * Srikar Dronamraju | |
21 | * Jim Keniston | |
35aa621b | 22 | * Copyright (C) 2011-2012 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com> |
2b144498 SD |
23 | */ |
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> | |
e8440c14 | 30 | #include <linux/export.h> |
2b144498 SD |
31 | #include <linux/rmap.h> /* anon_vma_prepare */ |
32 | #include <linux/mmu_notifier.h> /* set_pte_at_notify */ | |
33 | #include <linux/swap.h> /* try_to_free_swap */ | |
0326f5a9 SD |
34 | #include <linux/ptrace.h> /* user_enable_single_step */ |
35 | #include <linux/kdebug.h> /* notifier mechanism */ | |
194f8dcb | 36 | #include "../../mm/internal.h" /* munlock_vma_page */ |
32cdba1e | 37 | #include <linux/percpu-rwsem.h> |
7b2d81d4 | 38 | |
2b144498 SD |
39 | #include <linux/uprobes.h> |
40 | ||
d4b3b638 SD |
41 | #define UINSNS_PER_PAGE (PAGE_SIZE/UPROBE_XOL_SLOT_BYTES) |
42 | #define MAX_UPROBE_XOL_SLOTS UINSNS_PER_PAGE | |
43 | ||
2b144498 | 44 | static struct rb_root uprobes_tree = RB_ROOT; |
441f1eb7 ON |
45 | /* |
46 | * allows us to skip the uprobe_mmap if there are no uprobe events active | |
47 | * at this time. Probably a fine grained per inode count is better? | |
48 | */ | |
49 | #define no_uprobe_events() RB_EMPTY_ROOT(&uprobes_tree) | |
7b2d81d4 | 50 | |
2b144498 SD |
51 | static DEFINE_SPINLOCK(uprobes_treelock); /* serialize rbtree access */ |
52 | ||
53 | #define UPROBES_HASH_SZ 13 | |
2b144498 SD |
54 | /* serialize uprobe->pending_list */ |
55 | static struct mutex uprobes_mmap_mutex[UPROBES_HASH_SZ]; | |
7b2d81d4 | 56 | #define uprobes_mmap_hash(v) (&uprobes_mmap_mutex[((unsigned long)(v)) % UPROBES_HASH_SZ]) |
2b144498 | 57 | |
32cdba1e ON |
58 | static struct percpu_rw_semaphore dup_mmap_sem; |
59 | ||
cb9a19fe | 60 | /* Have a copy of original instruction */ |
71434f2f | 61 | #define UPROBE_COPY_INSN 0 |
cb9a19fe | 62 | /* Can skip singlestep */ |
bb929284 | 63 | #define UPROBE_SKIP_SSTEP 1 |
cb9a19fe | 64 | |
3ff54efd SD |
65 | struct uprobe { |
66 | struct rb_node rb_node; /* node in the rb tree */ | |
67 | atomic_t ref; | |
e591c8d7 | 68 | struct rw_semaphore register_rwsem; |
3ff54efd SD |
69 | struct rw_semaphore consumer_rwsem; |
70 | struct list_head pending_list; | |
71 | struct uprobe_consumer *consumers; | |
72 | struct inode *inode; /* Also hold a ref to inode */ | |
73 | loff_t offset; | |
71434f2f | 74 | unsigned long flags; |
3ff54efd SD |
75 | struct arch_uprobe arch; |
76 | }; | |
77 | ||
2b144498 SD |
78 | /* |
79 | * valid_vma: Verify if the specified vma is an executable vma | |
80 | * Relax restrictions while unregistering: vm_flags might have | |
81 | * changed after breakpoint was inserted. | |
82 | * - is_register: indicates if we are in register context. | |
83 | * - Return 1 if the specified virtual address is in an | |
84 | * executable vma. | |
85 | */ | |
86 | static bool valid_vma(struct vm_area_struct *vma, bool is_register) | |
87 | { | |
e40cfce6 | 88 | vm_flags_t flags = VM_HUGETLB | VM_MAYEXEC | VM_SHARED; |
2b144498 | 89 | |
e40cfce6 ON |
90 | if (is_register) |
91 | flags |= VM_WRITE; | |
2b144498 | 92 | |
e40cfce6 | 93 | return vma->vm_file && (vma->vm_flags & flags) == VM_MAYEXEC; |
2b144498 SD |
94 | } |
95 | ||
57683f72 | 96 | static unsigned long offset_to_vaddr(struct vm_area_struct *vma, loff_t offset) |
2b144498 | 97 | { |
57683f72 | 98 | return vma->vm_start + offset - ((loff_t)vma->vm_pgoff << PAGE_SHIFT); |
2b144498 SD |
99 | } |
100 | ||
cb113b47 ON |
101 | static loff_t vaddr_to_offset(struct vm_area_struct *vma, unsigned long vaddr) |
102 | { | |
103 | return ((loff_t)vma->vm_pgoff << PAGE_SHIFT) + (vaddr - vma->vm_start); | |
104 | } | |
105 | ||
2b144498 SD |
106 | /** |
107 | * __replace_page - replace page in vma by new page. | |
108 | * based on replace_page in mm/ksm.c | |
109 | * | |
110 | * @vma: vma that holds the pte pointing to page | |
c517ee74 | 111 | * @addr: address the old @page is mapped at |
2b144498 SD |
112 | * @page: the cowed page we are replacing by kpage |
113 | * @kpage: the modified page we replace page by | |
114 | * | |
115 | * Returns 0 on success, -EFAULT on failure. | |
116 | */ | |
c517ee74 ON |
117 | static int __replace_page(struct vm_area_struct *vma, unsigned long addr, |
118 | struct page *page, struct page *kpage) | |
2b144498 SD |
119 | { |
120 | struct mm_struct *mm = vma->vm_mm; | |
5323ce71 ON |
121 | spinlock_t *ptl; |
122 | pte_t *ptep; | |
9f92448c | 123 | int err; |
6bdb913f HE |
124 | /* For mmu_notifiers */ |
125 | const unsigned long mmun_start = addr; | |
126 | const unsigned long mmun_end = addr + PAGE_SIZE; | |
2b144498 | 127 | |
194f8dcb | 128 | /* For try_to_free_swap() and munlock_vma_page() below */ |
9f92448c ON |
129 | lock_page(page); |
130 | ||
6bdb913f | 131 | mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); |
9f92448c | 132 | err = -EAGAIN; |
5323ce71 | 133 | ptep = page_check_address(page, mm, addr, &ptl, 0); |
2b144498 | 134 | if (!ptep) |
9f92448c | 135 | goto unlock; |
2b144498 SD |
136 | |
137 | get_page(kpage); | |
138 | page_add_new_anon_rmap(kpage, vma, addr); | |
139 | ||
7396fa81 SD |
140 | if (!PageAnon(page)) { |
141 | dec_mm_counter(mm, MM_FILEPAGES); | |
142 | inc_mm_counter(mm, MM_ANONPAGES); | |
143 | } | |
144 | ||
2b144498 SD |
145 | flush_cache_page(vma, addr, pte_pfn(*ptep)); |
146 | ptep_clear_flush(vma, addr, ptep); | |
147 | set_pte_at_notify(mm, addr, ptep, mk_pte(kpage, vma->vm_page_prot)); | |
148 | ||
149 | page_remove_rmap(page); | |
150 | if (!page_mapped(page)) | |
151 | try_to_free_swap(page); | |
2b144498 | 152 | pte_unmap_unlock(ptep, ptl); |
2b144498 | 153 | |
194f8dcb ON |
154 | if (vma->vm_flags & VM_LOCKED) |
155 | munlock_vma_page(page); | |
156 | put_page(page); | |
157 | ||
9f92448c ON |
158 | err = 0; |
159 | unlock: | |
6bdb913f | 160 | mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); |
9f92448c ON |
161 | unlock_page(page); |
162 | return err; | |
2b144498 SD |
163 | } |
164 | ||
165 | /** | |
5cb4ac3a | 166 | * is_swbp_insn - check if instruction is breakpoint instruction. |
2b144498 | 167 | * @insn: instruction to be checked. |
5cb4ac3a | 168 | * Default implementation of is_swbp_insn |
2b144498 SD |
169 | * Returns true if @insn is a breakpoint instruction. |
170 | */ | |
5cb4ac3a | 171 | bool __weak is_swbp_insn(uprobe_opcode_t *insn) |
2b144498 | 172 | { |
5cb4ac3a | 173 | return *insn == UPROBE_SWBP_INSN; |
2b144498 SD |
174 | } |
175 | ||
0908ad6e AM |
176 | /** |
177 | * is_trap_insn - check if instruction is breakpoint instruction. | |
178 | * @insn: instruction to be checked. | |
179 | * Default implementation of is_trap_insn | |
180 | * Returns true if @insn is a breakpoint instruction. | |
181 | * | |
182 | * This function is needed for the case where an architecture has multiple | |
183 | * trap instructions (like powerpc). | |
184 | */ | |
185 | bool __weak is_trap_insn(uprobe_opcode_t *insn) | |
186 | { | |
187 | return is_swbp_insn(insn); | |
188 | } | |
189 | ||
ab0d805c | 190 | static void copy_from_page(struct page *page, unsigned long vaddr, void *dst, int len) |
cceb55aa ON |
191 | { |
192 | void *kaddr = kmap_atomic(page); | |
ab0d805c | 193 | memcpy(dst, kaddr + (vaddr & ~PAGE_MASK), len); |
cceb55aa ON |
194 | kunmap_atomic(kaddr); |
195 | } | |
196 | ||
ed6f6a50 ON |
197 | static int verify_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t *new_opcode) |
198 | { | |
199 | uprobe_opcode_t old_opcode; | |
200 | bool is_swbp; | |
201 | ||
0908ad6e AM |
202 | /* |
203 | * Note: We only check if the old_opcode is UPROBE_SWBP_INSN here. | |
204 | * We do not check if it is any other 'trap variant' which could | |
205 | * be conditional trap instruction such as the one powerpc supports. | |
206 | * | |
207 | * The logic is that we do not care if the underlying instruction | |
208 | * is a trap variant; uprobes always wins over any other (gdb) | |
209 | * breakpoint. | |
210 | */ | |
ab0d805c | 211 | copy_from_page(page, vaddr, &old_opcode, UPROBE_SWBP_INSN_SIZE); |
ed6f6a50 ON |
212 | is_swbp = is_swbp_insn(&old_opcode); |
213 | ||
214 | if (is_swbp_insn(new_opcode)) { | |
215 | if (is_swbp) /* register: already installed? */ | |
216 | return 0; | |
217 | } else { | |
218 | if (!is_swbp) /* unregister: was it changed by us? */ | |
076a365b | 219 | return 0; |
ed6f6a50 ON |
220 | } |
221 | ||
222 | return 1; | |
223 | } | |
224 | ||
2b144498 SD |
225 | /* |
226 | * NOTE: | |
227 | * Expect the breakpoint instruction to be the smallest size instruction for | |
228 | * the architecture. If an arch has variable length instruction and the | |
229 | * breakpoint instruction is not of the smallest length instruction | |
0908ad6e | 230 | * supported by that architecture then we need to modify is_trap_at_addr and |
2b144498 SD |
231 | * write_opcode accordingly. This would never be a problem for archs that |
232 | * have fixed length instructions. | |
233 | */ | |
234 | ||
235 | /* | |
236 | * write_opcode - write the opcode at a given virtual address. | |
237 | * @mm: the probed process address space. | |
2b144498 SD |
238 | * @vaddr: the virtual address to store the opcode. |
239 | * @opcode: opcode to be written at @vaddr. | |
240 | * | |
241 | * Called with mm->mmap_sem held (for read and with a reference to | |
242 | * mm). | |
243 | * | |
244 | * For mm @mm, write the opcode at @vaddr. | |
245 | * Return 0 (success) or a negative errno. | |
246 | */ | |
cceb55aa ON |
247 | static int write_opcode(struct mm_struct *mm, unsigned long vaddr, |
248 | uprobe_opcode_t opcode) | |
2b144498 SD |
249 | { |
250 | struct page *old_page, *new_page; | |
2b144498 SD |
251 | void *vaddr_old, *vaddr_new; |
252 | struct vm_area_struct *vma; | |
2b144498 | 253 | int ret; |
f403072c | 254 | |
5323ce71 | 255 | retry: |
2b144498 | 256 | /* Read the page with vaddr into memory */ |
75ed82ea | 257 | ret = get_user_pages(NULL, mm, vaddr, 1, 0, 1, &old_page, &vma); |
2b144498 SD |
258 | if (ret <= 0) |
259 | return ret; | |
7b2d81d4 | 260 | |
ed6f6a50 ON |
261 | ret = verify_opcode(old_page, vaddr, &opcode); |
262 | if (ret <= 0) | |
263 | goto put_old; | |
264 | ||
2b144498 SD |
265 | ret = -ENOMEM; |
266 | new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vaddr); | |
267 | if (!new_page) | |
9f92448c | 268 | goto put_old; |
2b144498 SD |
269 | |
270 | __SetPageUptodate(new_page); | |
271 | ||
2b144498 SD |
272 | /* copy the page now that we've got it stable */ |
273 | vaddr_old = kmap_atomic(old_page); | |
274 | vaddr_new = kmap_atomic(new_page); | |
275 | ||
276 | memcpy(vaddr_new, vaddr_old, PAGE_SIZE); | |
d9c4a30e | 277 | memcpy(vaddr_new + (vaddr & ~PAGE_MASK), &opcode, UPROBE_SWBP_INSN_SIZE); |
2b144498 SD |
278 | |
279 | kunmap_atomic(vaddr_new); | |
280 | kunmap_atomic(vaddr_old); | |
281 | ||
282 | ret = anon_vma_prepare(vma); | |
283 | if (ret) | |
9f92448c | 284 | goto put_new; |
2b144498 | 285 | |
c517ee74 | 286 | ret = __replace_page(vma, vaddr, old_page, new_page); |
2b144498 | 287 | |
9f92448c | 288 | put_new: |
2b144498 | 289 | page_cache_release(new_page); |
9f92448c | 290 | put_old: |
7b2d81d4 IM |
291 | put_page(old_page); |
292 | ||
5323ce71 ON |
293 | if (unlikely(ret == -EAGAIN)) |
294 | goto retry; | |
2b144498 SD |
295 | return ret; |
296 | } | |
297 | ||
2b144498 | 298 | /** |
5cb4ac3a | 299 | * set_swbp - store breakpoint at a given address. |
e3343e6a | 300 | * @auprobe: arch specific probepoint information. |
2b144498 | 301 | * @mm: the probed process address space. |
2b144498 SD |
302 | * @vaddr: the virtual address to insert the opcode. |
303 | * | |
304 | * For mm @mm, store the breakpoint instruction at @vaddr. | |
305 | * Return 0 (success) or a negative errno. | |
306 | */ | |
5cb4ac3a | 307 | int __weak set_swbp(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr) |
2b144498 | 308 | { |
cceb55aa | 309 | return write_opcode(mm, vaddr, UPROBE_SWBP_INSN); |
2b144498 SD |
310 | } |
311 | ||
312 | /** | |
313 | * set_orig_insn - Restore the original instruction. | |
314 | * @mm: the probed process address space. | |
e3343e6a | 315 | * @auprobe: arch specific probepoint information. |
2b144498 | 316 | * @vaddr: the virtual address to insert the opcode. |
2b144498 SD |
317 | * |
318 | * For mm @mm, restore the original opcode (opcode) at @vaddr. | |
319 | * Return 0 (success) or a negative errno. | |
320 | */ | |
7b2d81d4 | 321 | int __weak |
ded86e7c | 322 | set_orig_insn(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr) |
2b144498 | 323 | { |
cceb55aa | 324 | return write_opcode(mm, vaddr, *(uprobe_opcode_t *)auprobe->insn); |
2b144498 SD |
325 | } |
326 | ||
327 | static int match_uprobe(struct uprobe *l, struct uprobe *r) | |
328 | { | |
329 | if (l->inode < r->inode) | |
330 | return -1; | |
7b2d81d4 | 331 | |
2b144498 SD |
332 | if (l->inode > r->inode) |
333 | return 1; | |
2b144498 | 334 | |
7b2d81d4 IM |
335 | if (l->offset < r->offset) |
336 | return -1; | |
337 | ||
338 | if (l->offset > r->offset) | |
339 | return 1; | |
2b144498 SD |
340 | |
341 | return 0; | |
342 | } | |
343 | ||
344 | static struct uprobe *__find_uprobe(struct inode *inode, loff_t offset) | |
345 | { | |
346 | struct uprobe u = { .inode = inode, .offset = offset }; | |
347 | struct rb_node *n = uprobes_tree.rb_node; | |
348 | struct uprobe *uprobe; | |
349 | int match; | |
350 | ||
351 | while (n) { | |
352 | uprobe = rb_entry(n, struct uprobe, rb_node); | |
353 | match = match_uprobe(&u, uprobe); | |
354 | if (!match) { | |
355 | atomic_inc(&uprobe->ref); | |
356 | return uprobe; | |
357 | } | |
7b2d81d4 | 358 | |
2b144498 SD |
359 | if (match < 0) |
360 | n = n->rb_left; | |
361 | else | |
362 | n = n->rb_right; | |
363 | } | |
364 | return NULL; | |
365 | } | |
366 | ||
367 | /* | |
368 | * Find a uprobe corresponding to a given inode:offset | |
369 | * Acquires uprobes_treelock | |
370 | */ | |
371 | static struct uprobe *find_uprobe(struct inode *inode, loff_t offset) | |
372 | { | |
373 | struct uprobe *uprobe; | |
2b144498 | 374 | |
6f47caa0 | 375 | spin_lock(&uprobes_treelock); |
2b144498 | 376 | uprobe = __find_uprobe(inode, offset); |
6f47caa0 | 377 | spin_unlock(&uprobes_treelock); |
7b2d81d4 | 378 | |
2b144498 SD |
379 | return uprobe; |
380 | } | |
381 | ||
382 | static struct uprobe *__insert_uprobe(struct uprobe *uprobe) | |
383 | { | |
384 | struct rb_node **p = &uprobes_tree.rb_node; | |
385 | struct rb_node *parent = NULL; | |
386 | struct uprobe *u; | |
387 | int match; | |
388 | ||
389 | while (*p) { | |
390 | parent = *p; | |
391 | u = rb_entry(parent, struct uprobe, rb_node); | |
392 | match = match_uprobe(uprobe, u); | |
393 | if (!match) { | |
394 | atomic_inc(&u->ref); | |
395 | return u; | |
396 | } | |
397 | ||
398 | if (match < 0) | |
399 | p = &parent->rb_left; | |
400 | else | |
401 | p = &parent->rb_right; | |
402 | ||
403 | } | |
7b2d81d4 | 404 | |
2b144498 SD |
405 | u = NULL; |
406 | rb_link_node(&uprobe->rb_node, parent, p); | |
407 | rb_insert_color(&uprobe->rb_node, &uprobes_tree); | |
408 | /* get access + creation ref */ | |
409 | atomic_set(&uprobe->ref, 2); | |
7b2d81d4 | 410 | |
2b144498 SD |
411 | return u; |
412 | } | |
413 | ||
414 | /* | |
7b2d81d4 | 415 | * Acquire uprobes_treelock. |
2b144498 SD |
416 | * Matching uprobe already exists in rbtree; |
417 | * increment (access refcount) and return the matching uprobe. | |
418 | * | |
419 | * No matching uprobe; insert the uprobe in rb_tree; | |
420 | * get a double refcount (access + creation) and return NULL. | |
421 | */ | |
422 | static struct uprobe *insert_uprobe(struct uprobe *uprobe) | |
423 | { | |
2b144498 SD |
424 | struct uprobe *u; |
425 | ||
6f47caa0 | 426 | spin_lock(&uprobes_treelock); |
2b144498 | 427 | u = __insert_uprobe(uprobe); |
6f47caa0 | 428 | spin_unlock(&uprobes_treelock); |
7b2d81d4 | 429 | |
2b144498 SD |
430 | return u; |
431 | } | |
432 | ||
433 | static void put_uprobe(struct uprobe *uprobe) | |
434 | { | |
435 | if (atomic_dec_and_test(&uprobe->ref)) | |
436 | kfree(uprobe); | |
437 | } | |
438 | ||
439 | static struct uprobe *alloc_uprobe(struct inode *inode, loff_t offset) | |
440 | { | |
441 | struct uprobe *uprobe, *cur_uprobe; | |
442 | ||
443 | uprobe = kzalloc(sizeof(struct uprobe), GFP_KERNEL); | |
444 | if (!uprobe) | |
445 | return NULL; | |
446 | ||
447 | uprobe->inode = igrab(inode); | |
448 | uprobe->offset = offset; | |
e591c8d7 | 449 | init_rwsem(&uprobe->register_rwsem); |
2b144498 | 450 | init_rwsem(&uprobe->consumer_rwsem); |
bbc33d05 ON |
451 | /* For now assume that the instruction need not be single-stepped */ |
452 | __set_bit(UPROBE_SKIP_SSTEP, &uprobe->flags); | |
2b144498 SD |
453 | |
454 | /* add to uprobes_tree, sorted on inode:offset */ | |
455 | cur_uprobe = insert_uprobe(uprobe); | |
456 | ||
457 | /* a uprobe exists for this inode:offset combination */ | |
458 | if (cur_uprobe) { | |
459 | kfree(uprobe); | |
460 | uprobe = cur_uprobe; | |
461 | iput(inode); | |
7b2d81d4 IM |
462 | } |
463 | ||
2b144498 SD |
464 | return uprobe; |
465 | } | |
466 | ||
9a98e03c | 467 | static void consumer_add(struct uprobe *uprobe, struct uprobe_consumer *uc) |
2b144498 SD |
468 | { |
469 | down_write(&uprobe->consumer_rwsem); | |
e3343e6a SD |
470 | uc->next = uprobe->consumers; |
471 | uprobe->consumers = uc; | |
2b144498 | 472 | up_write(&uprobe->consumer_rwsem); |
2b144498 SD |
473 | } |
474 | ||
475 | /* | |
e3343e6a SD |
476 | * For uprobe @uprobe, delete the consumer @uc. |
477 | * Return true if the @uc is deleted successfully | |
2b144498 SD |
478 | * or return false. |
479 | */ | |
e3343e6a | 480 | static bool consumer_del(struct uprobe *uprobe, struct uprobe_consumer *uc) |
2b144498 SD |
481 | { |
482 | struct uprobe_consumer **con; | |
483 | bool ret = false; | |
484 | ||
485 | down_write(&uprobe->consumer_rwsem); | |
486 | for (con = &uprobe->consumers; *con; con = &(*con)->next) { | |
e3343e6a SD |
487 | if (*con == uc) { |
488 | *con = uc->next; | |
2b144498 SD |
489 | ret = true; |
490 | break; | |
491 | } | |
492 | } | |
493 | up_write(&uprobe->consumer_rwsem); | |
7b2d81d4 | 494 | |
2b144498 SD |
495 | return ret; |
496 | } | |
497 | ||
e3343e6a | 498 | static int |
d436615e | 499 | __copy_insn(struct address_space *mapping, struct file *filp, char *insn, |
593609a5 | 500 | unsigned long nbytes, loff_t offset) |
2b144498 | 501 | { |
2b144498 | 502 | struct page *page; |
2b144498 | 503 | |
cc359d18 ON |
504 | if (!mapping->a_ops->readpage) |
505 | return -EIO; | |
2b144498 SD |
506 | /* |
507 | * Ensure that the page that has the original instruction is | |
508 | * populated and in page-cache. | |
509 | */ | |
2edb7b55 | 510 | page = read_mapping_page(mapping, offset >> PAGE_CACHE_SHIFT, filp); |
2b144498 SD |
511 | if (IS_ERR(page)) |
512 | return PTR_ERR(page); | |
513 | ||
2edb7b55 | 514 | copy_from_page(page, offset, insn, nbytes); |
2b144498 | 515 | page_cache_release(page); |
7b2d81d4 | 516 | |
2b144498 SD |
517 | return 0; |
518 | } | |
519 | ||
d436615e | 520 | static int copy_insn(struct uprobe *uprobe, struct file *filp) |
2b144498 SD |
521 | { |
522 | struct address_space *mapping; | |
2b144498 | 523 | unsigned long nbytes; |
7b2d81d4 | 524 | int bytes; |
2b144498 | 525 | |
d436615e | 526 | nbytes = PAGE_SIZE - (uprobe->offset & ~PAGE_MASK); |
2b144498 SD |
527 | mapping = uprobe->inode->i_mapping; |
528 | ||
529 | /* Instruction at end of binary; copy only available bytes */ | |
530 | if (uprobe->offset + MAX_UINSN_BYTES > uprobe->inode->i_size) | |
531 | bytes = uprobe->inode->i_size - uprobe->offset; | |
532 | else | |
533 | bytes = MAX_UINSN_BYTES; | |
534 | ||
535 | /* Instruction at the page-boundary; copy bytes in second page */ | |
536 | if (nbytes < bytes) { | |
fc36f595 ON |
537 | int err = __copy_insn(mapping, filp, uprobe->arch.insn + nbytes, |
538 | bytes - nbytes, uprobe->offset + nbytes); | |
539 | if (err) | |
540 | return err; | |
2b144498 SD |
541 | bytes = nbytes; |
542 | } | |
d436615e | 543 | return __copy_insn(mapping, filp, uprobe->arch.insn, bytes, uprobe->offset); |
2b144498 SD |
544 | } |
545 | ||
cb9a19fe ON |
546 | static int prepare_uprobe(struct uprobe *uprobe, struct file *file, |
547 | struct mm_struct *mm, unsigned long vaddr) | |
548 | { | |
549 | int ret = 0; | |
550 | ||
71434f2f | 551 | if (test_bit(UPROBE_COPY_INSN, &uprobe->flags)) |
cb9a19fe ON |
552 | return ret; |
553 | ||
d4d3ccc6 ON |
554 | /* TODO: move this into _register, until then we abuse this sem. */ |
555 | down_write(&uprobe->consumer_rwsem); | |
71434f2f | 556 | if (test_bit(UPROBE_COPY_INSN, &uprobe->flags)) |
4710f05f ON |
557 | goto out; |
558 | ||
cb9a19fe ON |
559 | ret = copy_insn(uprobe, file); |
560 | if (ret) | |
561 | goto out; | |
562 | ||
563 | ret = -ENOTSUPP; | |
0908ad6e | 564 | if (is_trap_insn((uprobe_opcode_t *)uprobe->arch.insn)) |
cb9a19fe ON |
565 | goto out; |
566 | ||
567 | ret = arch_uprobe_analyze_insn(&uprobe->arch, mm, vaddr); | |
568 | if (ret) | |
569 | goto out; | |
570 | ||
571 | /* write_opcode() assumes we don't cross page boundary */ | |
572 | BUG_ON((uprobe->offset & ~PAGE_MASK) + | |
573 | UPROBE_SWBP_INSN_SIZE > PAGE_SIZE); | |
574 | ||
575 | smp_wmb(); /* pairs with rmb() in find_active_uprobe() */ | |
71434f2f | 576 | set_bit(UPROBE_COPY_INSN, &uprobe->flags); |
cb9a19fe ON |
577 | |
578 | out: | |
d4d3ccc6 | 579 | up_write(&uprobe->consumer_rwsem); |
4710f05f | 580 | |
cb9a19fe ON |
581 | return ret; |
582 | } | |
583 | ||
8a7f2fa0 ON |
584 | static inline bool consumer_filter(struct uprobe_consumer *uc, |
585 | enum uprobe_filter_ctx ctx, struct mm_struct *mm) | |
806a98bd | 586 | { |
8a7f2fa0 | 587 | return !uc->filter || uc->filter(uc, ctx, mm); |
806a98bd ON |
588 | } |
589 | ||
8a7f2fa0 ON |
590 | static bool filter_chain(struct uprobe *uprobe, |
591 | enum uprobe_filter_ctx ctx, struct mm_struct *mm) | |
63633cbf | 592 | { |
1ff6fee5 ON |
593 | struct uprobe_consumer *uc; |
594 | bool ret = false; | |
595 | ||
596 | down_read(&uprobe->consumer_rwsem); | |
597 | for (uc = uprobe->consumers; uc; uc = uc->next) { | |
8a7f2fa0 | 598 | ret = consumer_filter(uc, ctx, mm); |
1ff6fee5 ON |
599 | if (ret) |
600 | break; | |
601 | } | |
602 | up_read(&uprobe->consumer_rwsem); | |
603 | ||
604 | return ret; | |
63633cbf ON |
605 | } |
606 | ||
e3343e6a SD |
607 | static int |
608 | install_breakpoint(struct uprobe *uprobe, struct mm_struct *mm, | |
816c03fb | 609 | struct vm_area_struct *vma, unsigned long vaddr) |
2b144498 | 610 | { |
f8ac4ec9 | 611 | bool first_uprobe; |
2b144498 SD |
612 | int ret; |
613 | ||
cb9a19fe ON |
614 | ret = prepare_uprobe(uprobe, vma->vm_file, mm, vaddr); |
615 | if (ret) | |
616 | return ret; | |
682968e0 | 617 | |
f8ac4ec9 ON |
618 | /* |
619 | * set MMF_HAS_UPROBES in advance for uprobe_pre_sstep_notifier(), | |
620 | * the task can hit this breakpoint right after __replace_page(). | |
621 | */ | |
622 | first_uprobe = !test_bit(MMF_HAS_UPROBES, &mm->flags); | |
623 | if (first_uprobe) | |
624 | set_bit(MMF_HAS_UPROBES, &mm->flags); | |
625 | ||
816c03fb | 626 | ret = set_swbp(&uprobe->arch, mm, vaddr); |
9f68f672 ON |
627 | if (!ret) |
628 | clear_bit(MMF_RECALC_UPROBES, &mm->flags); | |
629 | else if (first_uprobe) | |
f8ac4ec9 | 630 | clear_bit(MMF_HAS_UPROBES, &mm->flags); |
2b144498 SD |
631 | |
632 | return ret; | |
633 | } | |
634 | ||
076a365b | 635 | static int |
816c03fb | 636 | remove_breakpoint(struct uprobe *uprobe, struct mm_struct *mm, unsigned long vaddr) |
2b144498 | 637 | { |
9f68f672 | 638 | set_bit(MMF_RECALC_UPROBES, &mm->flags); |
076a365b | 639 | return set_orig_insn(&uprobe->arch, mm, vaddr); |
2b144498 SD |
640 | } |
641 | ||
06b7bcd8 ON |
642 | static inline bool uprobe_is_active(struct uprobe *uprobe) |
643 | { | |
644 | return !RB_EMPTY_NODE(&uprobe->rb_node); | |
645 | } | |
0326f5a9 | 646 | /* |
778b032d ON |
647 | * There could be threads that have already hit the breakpoint. They |
648 | * will recheck the current insn and restart if find_uprobe() fails. | |
649 | * See find_active_uprobe(). | |
0326f5a9 | 650 | */ |
2b144498 SD |
651 | static void delete_uprobe(struct uprobe *uprobe) |
652 | { | |
06b7bcd8 ON |
653 | if (WARN_ON(!uprobe_is_active(uprobe))) |
654 | return; | |
655 | ||
6f47caa0 | 656 | spin_lock(&uprobes_treelock); |
2b144498 | 657 | rb_erase(&uprobe->rb_node, &uprobes_tree); |
6f47caa0 | 658 | spin_unlock(&uprobes_treelock); |
06b7bcd8 | 659 | RB_CLEAR_NODE(&uprobe->rb_node); /* for uprobe_is_active() */ |
2b144498 SD |
660 | iput(uprobe->inode); |
661 | put_uprobe(uprobe); | |
2b144498 SD |
662 | } |
663 | ||
26872090 ON |
664 | struct map_info { |
665 | struct map_info *next; | |
666 | struct mm_struct *mm; | |
816c03fb | 667 | unsigned long vaddr; |
26872090 ON |
668 | }; |
669 | ||
670 | static inline struct map_info *free_map_info(struct map_info *info) | |
2b144498 | 671 | { |
26872090 ON |
672 | struct map_info *next = info->next; |
673 | kfree(info); | |
674 | return next; | |
675 | } | |
676 | ||
677 | static struct map_info * | |
678 | build_map_info(struct address_space *mapping, loff_t offset, bool is_register) | |
679 | { | |
680 | unsigned long pgoff = offset >> PAGE_SHIFT; | |
2b144498 | 681 | struct vm_area_struct *vma; |
26872090 ON |
682 | struct map_info *curr = NULL; |
683 | struct map_info *prev = NULL; | |
684 | struct map_info *info; | |
685 | int more = 0; | |
2b144498 | 686 | |
26872090 ON |
687 | again: |
688 | mutex_lock(&mapping->i_mmap_mutex); | |
6b2dbba8 | 689 | vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) { |
2b144498 SD |
690 | if (!valid_vma(vma, is_register)) |
691 | continue; | |
692 | ||
7a5bfb66 ON |
693 | if (!prev && !more) { |
694 | /* | |
695 | * Needs GFP_NOWAIT to avoid i_mmap_mutex recursion through | |
696 | * reclaim. This is optimistic, no harm done if it fails. | |
697 | */ | |
698 | prev = kmalloc(sizeof(struct map_info), | |
699 | GFP_NOWAIT | __GFP_NOMEMALLOC | __GFP_NOWARN); | |
700 | if (prev) | |
701 | prev->next = NULL; | |
702 | } | |
26872090 ON |
703 | if (!prev) { |
704 | more++; | |
705 | continue; | |
2b144498 | 706 | } |
2b144498 | 707 | |
26872090 ON |
708 | if (!atomic_inc_not_zero(&vma->vm_mm->mm_users)) |
709 | continue; | |
7b2d81d4 | 710 | |
26872090 ON |
711 | info = prev; |
712 | prev = prev->next; | |
713 | info->next = curr; | |
714 | curr = info; | |
2b144498 | 715 | |
26872090 | 716 | info->mm = vma->vm_mm; |
57683f72 | 717 | info->vaddr = offset_to_vaddr(vma, offset); |
26872090 | 718 | } |
2b144498 SD |
719 | mutex_unlock(&mapping->i_mmap_mutex); |
720 | ||
26872090 ON |
721 | if (!more) |
722 | goto out; | |
723 | ||
724 | prev = curr; | |
725 | while (curr) { | |
726 | mmput(curr->mm); | |
727 | curr = curr->next; | |
728 | } | |
7b2d81d4 | 729 | |
26872090 ON |
730 | do { |
731 | info = kmalloc(sizeof(struct map_info), GFP_KERNEL); | |
732 | if (!info) { | |
733 | curr = ERR_PTR(-ENOMEM); | |
734 | goto out; | |
735 | } | |
736 | info->next = prev; | |
737 | prev = info; | |
738 | } while (--more); | |
739 | ||
740 | goto again; | |
741 | out: | |
742 | while (prev) | |
743 | prev = free_map_info(prev); | |
744 | return curr; | |
2b144498 SD |
745 | } |
746 | ||
bdf8647c ON |
747 | static int |
748 | register_for_each_vma(struct uprobe *uprobe, struct uprobe_consumer *new) | |
2b144498 | 749 | { |
bdf8647c | 750 | bool is_register = !!new; |
26872090 ON |
751 | struct map_info *info; |
752 | int err = 0; | |
2b144498 | 753 | |
32cdba1e | 754 | percpu_down_write(&dup_mmap_sem); |
26872090 ON |
755 | info = build_map_info(uprobe->inode->i_mapping, |
756 | uprobe->offset, is_register); | |
32cdba1e ON |
757 | if (IS_ERR(info)) { |
758 | err = PTR_ERR(info); | |
759 | goto out; | |
760 | } | |
7b2d81d4 | 761 | |
26872090 ON |
762 | while (info) { |
763 | struct mm_struct *mm = info->mm; | |
764 | struct vm_area_struct *vma; | |
7b2d81d4 | 765 | |
076a365b | 766 | if (err && is_register) |
26872090 | 767 | goto free; |
7b2d81d4 | 768 | |
77fc4af1 | 769 | down_write(&mm->mmap_sem); |
f4d6dfe5 ON |
770 | vma = find_vma(mm, info->vaddr); |
771 | if (!vma || !valid_vma(vma, is_register) || | |
f281769e | 772 | file_inode(vma->vm_file) != uprobe->inode) |
26872090 ON |
773 | goto unlock; |
774 | ||
f4d6dfe5 ON |
775 | if (vma->vm_start > info->vaddr || |
776 | vaddr_to_offset(vma, info->vaddr) != uprobe->offset) | |
26872090 | 777 | goto unlock; |
2b144498 | 778 | |
806a98bd ON |
779 | if (is_register) { |
780 | /* consult only the "caller", new consumer. */ | |
bdf8647c | 781 | if (consumer_filter(new, |
8a7f2fa0 | 782 | UPROBE_FILTER_REGISTER, mm)) |
806a98bd ON |
783 | err = install_breakpoint(uprobe, mm, vma, info->vaddr); |
784 | } else if (test_bit(MMF_HAS_UPROBES, &mm->flags)) { | |
8a7f2fa0 ON |
785 | if (!filter_chain(uprobe, |
786 | UPROBE_FILTER_UNREGISTER, mm)) | |
806a98bd ON |
787 | err |= remove_breakpoint(uprobe, mm, info->vaddr); |
788 | } | |
78f74116 | 789 | |
26872090 ON |
790 | unlock: |
791 | up_write(&mm->mmap_sem); | |
792 | free: | |
793 | mmput(mm); | |
794 | info = free_map_info(info); | |
2b144498 | 795 | } |
32cdba1e ON |
796 | out: |
797 | percpu_up_write(&dup_mmap_sem); | |
26872090 | 798 | return err; |
2b144498 SD |
799 | } |
800 | ||
9a98e03c | 801 | static int __uprobe_register(struct uprobe *uprobe, struct uprobe_consumer *uc) |
2b144498 | 802 | { |
9a98e03c | 803 | consumer_add(uprobe, uc); |
bdf8647c | 804 | return register_for_each_vma(uprobe, uc); |
2b144498 SD |
805 | } |
806 | ||
04aab9b2 | 807 | static void __uprobe_unregister(struct uprobe *uprobe, struct uprobe_consumer *uc) |
2b144498 | 808 | { |
04aab9b2 ON |
809 | int err; |
810 | ||
811 | if (!consumer_del(uprobe, uc)) /* WARN? */ | |
812 | return; | |
2b144498 | 813 | |
bdf8647c | 814 | err = register_for_each_vma(uprobe, NULL); |
bb929284 ON |
815 | /* TODO : cant unregister? schedule a worker thread */ |
816 | if (!uprobe->consumers && !err) | |
817 | delete_uprobe(uprobe); | |
2b144498 SD |
818 | } |
819 | ||
820 | /* | |
7b2d81d4 | 821 | * uprobe_register - register a probe |
2b144498 SD |
822 | * @inode: the file in which the probe has to be placed. |
823 | * @offset: offset from the start of the file. | |
e3343e6a | 824 | * @uc: information on howto handle the probe.. |
2b144498 | 825 | * |
7b2d81d4 | 826 | * Apart from the access refcount, uprobe_register() takes a creation |
2b144498 SD |
827 | * refcount (thro alloc_uprobe) if and only if this @uprobe is getting |
828 | * inserted into the rbtree (i.e first consumer for a @inode:@offset | |
7b2d81d4 | 829 | * tuple). Creation refcount stops uprobe_unregister from freeing the |
2b144498 | 830 | * @uprobe even before the register operation is complete. Creation |
e3343e6a | 831 | * refcount is released when the last @uc for the @uprobe |
2b144498 SD |
832 | * unregisters. |
833 | * | |
834 | * Return errno if it cannot successully install probes | |
835 | * else return 0 (success) | |
836 | */ | |
e3343e6a | 837 | int uprobe_register(struct inode *inode, loff_t offset, struct uprobe_consumer *uc) |
2b144498 SD |
838 | { |
839 | struct uprobe *uprobe; | |
7b2d81d4 | 840 | int ret; |
2b144498 | 841 | |
f0744af7 | 842 | /* Racy, just to catch the obvious mistakes */ |
2b144498 | 843 | if (offset > i_size_read(inode)) |
7b2d81d4 | 844 | return -EINVAL; |
2b144498 | 845 | |
66d06dff | 846 | retry: |
2b144498 | 847 | uprobe = alloc_uprobe(inode, offset); |
66d06dff ON |
848 | if (!uprobe) |
849 | return -ENOMEM; | |
850 | /* | |
851 | * We can race with uprobe_unregister()->delete_uprobe(). | |
852 | * Check uprobe_is_active() and retry if it is false. | |
853 | */ | |
854 | down_write(&uprobe->register_rwsem); | |
855 | ret = -EAGAIN; | |
856 | if (likely(uprobe_is_active(uprobe))) { | |
9a98e03c ON |
857 | ret = __uprobe_register(uprobe, uc); |
858 | if (ret) | |
04aab9b2 | 859 | __uprobe_unregister(uprobe, uc); |
2b144498 | 860 | } |
66d06dff ON |
861 | up_write(&uprobe->register_rwsem); |
862 | put_uprobe(uprobe); | |
2b144498 | 863 | |
66d06dff ON |
864 | if (unlikely(ret == -EAGAIN)) |
865 | goto retry; | |
2b144498 SD |
866 | return ret; |
867 | } | |
e8440c14 | 868 | EXPORT_SYMBOL_GPL(uprobe_register); |
2b144498 | 869 | |
bdf8647c ON |
870 | /* |
871 | * uprobe_apply - unregister a already registered probe. | |
872 | * @inode: the file in which the probe has to be removed. | |
873 | * @offset: offset from the start of the file. | |
874 | * @uc: consumer which wants to add more or remove some breakpoints | |
875 | * @add: add or remove the breakpoints | |
876 | */ | |
877 | int uprobe_apply(struct inode *inode, loff_t offset, | |
878 | struct uprobe_consumer *uc, bool add) | |
879 | { | |
880 | struct uprobe *uprobe; | |
881 | struct uprobe_consumer *con; | |
882 | int ret = -ENOENT; | |
883 | ||
884 | uprobe = find_uprobe(inode, offset); | |
885 | if (!uprobe) | |
886 | return ret; | |
887 | ||
888 | down_write(&uprobe->register_rwsem); | |
889 | for (con = uprobe->consumers; con && con != uc ; con = con->next) | |
890 | ; | |
891 | if (con) | |
892 | ret = register_for_each_vma(uprobe, add ? uc : NULL); | |
893 | up_write(&uprobe->register_rwsem); | |
894 | put_uprobe(uprobe); | |
895 | ||
896 | return ret; | |
897 | } | |
898 | ||
2b144498 | 899 | /* |
7b2d81d4 | 900 | * uprobe_unregister - unregister a already registered probe. |
2b144498 SD |
901 | * @inode: the file in which the probe has to be removed. |
902 | * @offset: offset from the start of the file. | |
e3343e6a | 903 | * @uc: identify which probe if multiple probes are colocated. |
2b144498 | 904 | */ |
e3343e6a | 905 | void uprobe_unregister(struct inode *inode, loff_t offset, struct uprobe_consumer *uc) |
2b144498 | 906 | { |
7b2d81d4 | 907 | struct uprobe *uprobe; |
2b144498 | 908 | |
2b144498 SD |
909 | uprobe = find_uprobe(inode, offset); |
910 | if (!uprobe) | |
911 | return; | |
912 | ||
e591c8d7 | 913 | down_write(&uprobe->register_rwsem); |
04aab9b2 | 914 | __uprobe_unregister(uprobe, uc); |
e591c8d7 | 915 | up_write(&uprobe->register_rwsem); |
c91368c4 | 916 | put_uprobe(uprobe); |
2b144498 | 917 | } |
e8440c14 | 918 | EXPORT_SYMBOL_GPL(uprobe_unregister); |
2b144498 | 919 | |
da1816b1 ON |
920 | static int unapply_uprobe(struct uprobe *uprobe, struct mm_struct *mm) |
921 | { | |
922 | struct vm_area_struct *vma; | |
923 | int err = 0; | |
924 | ||
925 | down_read(&mm->mmap_sem); | |
926 | for (vma = mm->mmap; vma; vma = vma->vm_next) { | |
927 | unsigned long vaddr; | |
928 | loff_t offset; | |
929 | ||
930 | if (!valid_vma(vma, false) || | |
f281769e | 931 | file_inode(vma->vm_file) != uprobe->inode) |
da1816b1 ON |
932 | continue; |
933 | ||
934 | offset = (loff_t)vma->vm_pgoff << PAGE_SHIFT; | |
935 | if (uprobe->offset < offset || | |
936 | uprobe->offset >= offset + vma->vm_end - vma->vm_start) | |
937 | continue; | |
938 | ||
939 | vaddr = offset_to_vaddr(vma, uprobe->offset); | |
940 | err |= remove_breakpoint(uprobe, mm, vaddr); | |
941 | } | |
942 | up_read(&mm->mmap_sem); | |
943 | ||
944 | return err; | |
945 | } | |
946 | ||
891c3970 ON |
947 | static struct rb_node * |
948 | find_node_in_range(struct inode *inode, loff_t min, loff_t max) | |
2b144498 | 949 | { |
2b144498 | 950 | struct rb_node *n = uprobes_tree.rb_node; |
2b144498 SD |
951 | |
952 | while (n) { | |
891c3970 | 953 | struct uprobe *u = rb_entry(n, struct uprobe, rb_node); |
2b144498 | 954 | |
891c3970 | 955 | if (inode < u->inode) { |
2b144498 | 956 | n = n->rb_left; |
891c3970 | 957 | } else if (inode > u->inode) { |
2b144498 | 958 | n = n->rb_right; |
891c3970 ON |
959 | } else { |
960 | if (max < u->offset) | |
961 | n = n->rb_left; | |
962 | else if (min > u->offset) | |
963 | n = n->rb_right; | |
964 | else | |
965 | break; | |
966 | } | |
2b144498 | 967 | } |
7b2d81d4 | 968 | |
891c3970 | 969 | return n; |
2b144498 SD |
970 | } |
971 | ||
972 | /* | |
891c3970 | 973 | * For a given range in vma, build a list of probes that need to be inserted. |
2b144498 | 974 | */ |
891c3970 ON |
975 | static void build_probe_list(struct inode *inode, |
976 | struct vm_area_struct *vma, | |
977 | unsigned long start, unsigned long end, | |
978 | struct list_head *head) | |
2b144498 | 979 | { |
891c3970 | 980 | loff_t min, max; |
891c3970 ON |
981 | struct rb_node *n, *t; |
982 | struct uprobe *u; | |
7b2d81d4 | 983 | |
891c3970 | 984 | INIT_LIST_HEAD(head); |
cb113b47 | 985 | min = vaddr_to_offset(vma, start); |
891c3970 | 986 | max = min + (end - start) - 1; |
2b144498 | 987 | |
6f47caa0 | 988 | spin_lock(&uprobes_treelock); |
891c3970 ON |
989 | n = find_node_in_range(inode, min, max); |
990 | if (n) { | |
991 | for (t = n; t; t = rb_prev(t)) { | |
992 | u = rb_entry(t, struct uprobe, rb_node); | |
993 | if (u->inode != inode || u->offset < min) | |
994 | break; | |
995 | list_add(&u->pending_list, head); | |
996 | atomic_inc(&u->ref); | |
997 | } | |
998 | for (t = n; (t = rb_next(t)); ) { | |
999 | u = rb_entry(t, struct uprobe, rb_node); | |
1000 | if (u->inode != inode || u->offset > max) | |
1001 | break; | |
1002 | list_add(&u->pending_list, head); | |
1003 | atomic_inc(&u->ref); | |
1004 | } | |
2b144498 | 1005 | } |
6f47caa0 | 1006 | spin_unlock(&uprobes_treelock); |
2b144498 SD |
1007 | } |
1008 | ||
1009 | /* | |
5e5be71a | 1010 | * Called from mmap_region/vma_adjust with mm->mmap_sem acquired. |
2b144498 | 1011 | * |
5e5be71a ON |
1012 | * Currently we ignore all errors and always return 0, the callers |
1013 | * can't handle the failure anyway. | |
2b144498 | 1014 | */ |
7b2d81d4 | 1015 | int uprobe_mmap(struct vm_area_struct *vma) |
2b144498 SD |
1016 | { |
1017 | struct list_head tmp_list; | |
665605a2 | 1018 | struct uprobe *uprobe, *u; |
2b144498 | 1019 | struct inode *inode; |
2b144498 | 1020 | |
441f1eb7 | 1021 | if (no_uprobe_events() || !valid_vma(vma, true)) |
7b2d81d4 | 1022 | return 0; |
2b144498 | 1023 | |
f281769e | 1024 | inode = file_inode(vma->vm_file); |
2b144498 | 1025 | if (!inode) |
7b2d81d4 | 1026 | return 0; |
2b144498 | 1027 | |
2b144498 | 1028 | mutex_lock(uprobes_mmap_hash(inode)); |
891c3970 | 1029 | build_probe_list(inode, vma, vma->vm_start, vma->vm_end, &tmp_list); |
806a98bd ON |
1030 | /* |
1031 | * We can race with uprobe_unregister(), this uprobe can be already | |
1032 | * removed. But in this case filter_chain() must return false, all | |
1033 | * consumers have gone away. | |
1034 | */ | |
665605a2 | 1035 | list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) { |
806a98bd | 1036 | if (!fatal_signal_pending(current) && |
8a7f2fa0 | 1037 | filter_chain(uprobe, UPROBE_FILTER_MMAP, vma->vm_mm)) { |
57683f72 | 1038 | unsigned long vaddr = offset_to_vaddr(vma, uprobe->offset); |
5e5be71a | 1039 | install_breakpoint(uprobe, vma->vm_mm, vma, vaddr); |
2b144498 SD |
1040 | } |
1041 | put_uprobe(uprobe); | |
1042 | } | |
2b144498 SD |
1043 | mutex_unlock(uprobes_mmap_hash(inode)); |
1044 | ||
5e5be71a | 1045 | return 0; |
2b144498 SD |
1046 | } |
1047 | ||
9f68f672 ON |
1048 | static bool |
1049 | vma_has_uprobes(struct vm_area_struct *vma, unsigned long start, unsigned long end) | |
1050 | { | |
1051 | loff_t min, max; | |
1052 | struct inode *inode; | |
1053 | struct rb_node *n; | |
1054 | ||
f281769e | 1055 | inode = file_inode(vma->vm_file); |
9f68f672 ON |
1056 | |
1057 | min = vaddr_to_offset(vma, start); | |
1058 | max = min + (end - start) - 1; | |
1059 | ||
1060 | spin_lock(&uprobes_treelock); | |
1061 | n = find_node_in_range(inode, min, max); | |
1062 | spin_unlock(&uprobes_treelock); | |
1063 | ||
1064 | return !!n; | |
1065 | } | |
1066 | ||
682968e0 SD |
1067 | /* |
1068 | * Called in context of a munmap of a vma. | |
1069 | */ | |
cbc91f71 | 1070 | void uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end) |
682968e0 | 1071 | { |
441f1eb7 | 1072 | if (no_uprobe_events() || !valid_vma(vma, false)) |
682968e0 SD |
1073 | return; |
1074 | ||
2fd611a9 ON |
1075 | if (!atomic_read(&vma->vm_mm->mm_users)) /* called by mmput() ? */ |
1076 | return; | |
1077 | ||
9f68f672 ON |
1078 | if (!test_bit(MMF_HAS_UPROBES, &vma->vm_mm->flags) || |
1079 | test_bit(MMF_RECALC_UPROBES, &vma->vm_mm->flags)) | |
f8ac4ec9 ON |
1080 | return; |
1081 | ||
9f68f672 ON |
1082 | if (vma_has_uprobes(vma, start, end)) |
1083 | set_bit(MMF_RECALC_UPROBES, &vma->vm_mm->flags); | |
682968e0 SD |
1084 | } |
1085 | ||
d4b3b638 SD |
1086 | /* Slot allocation for XOL */ |
1087 | static int xol_add_vma(struct xol_area *area) | |
1088 | { | |
c8a82538 ON |
1089 | struct mm_struct *mm = current->mm; |
1090 | int ret = -EALREADY; | |
d4b3b638 SD |
1091 | |
1092 | down_write(&mm->mmap_sem); | |
1093 | if (mm->uprobes_state.xol_area) | |
1094 | goto fail; | |
1095 | ||
1096 | ret = -ENOMEM; | |
d4b3b638 SD |
1097 | /* Try to map as high as possible, this is only a hint. */ |
1098 | area->vaddr = get_unmapped_area(NULL, TASK_SIZE - PAGE_SIZE, PAGE_SIZE, 0, 0); | |
1099 | if (area->vaddr & ~PAGE_MASK) { | |
1100 | ret = area->vaddr; | |
1101 | goto fail; | |
1102 | } | |
1103 | ||
1104 | ret = install_special_mapping(mm, area->vaddr, PAGE_SIZE, | |
1105 | VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO, &area->page); | |
1106 | if (ret) | |
1107 | goto fail; | |
1108 | ||
1109 | smp_wmb(); /* pairs with get_xol_area() */ | |
1110 | mm->uprobes_state.xol_area = area; | |
1111 | ret = 0; | |
c8a82538 | 1112 | fail: |
d4b3b638 | 1113 | up_write(&mm->mmap_sem); |
d4b3b638 SD |
1114 | |
1115 | return ret; | |
1116 | } | |
1117 | ||
d4b3b638 | 1118 | /* |
9b545df8 ON |
1119 | * get_xol_area - Allocate process's xol_area if necessary. |
1120 | * This area will be used for storing instructions for execution out of line. | |
d4b3b638 SD |
1121 | * |
1122 | * Returns the allocated area or NULL. | |
1123 | */ | |
9b545df8 | 1124 | static struct xol_area *get_xol_area(void) |
d4b3b638 | 1125 | { |
9b545df8 | 1126 | struct mm_struct *mm = current->mm; |
d4b3b638 SD |
1127 | struct xol_area *area; |
1128 | ||
9b545df8 ON |
1129 | area = mm->uprobes_state.xol_area; |
1130 | if (area) | |
1131 | goto ret; | |
1132 | ||
d4b3b638 SD |
1133 | area = kzalloc(sizeof(*area), GFP_KERNEL); |
1134 | if (unlikely(!area)) | |
c8a82538 | 1135 | goto out; |
d4b3b638 SD |
1136 | |
1137 | area->bitmap = kzalloc(BITS_TO_LONGS(UINSNS_PER_PAGE) * sizeof(long), GFP_KERNEL); | |
d4b3b638 | 1138 | if (!area->bitmap) |
c8a82538 ON |
1139 | goto free_area; |
1140 | ||
1141 | area->page = alloc_page(GFP_HIGHUSER); | |
1142 | if (!area->page) | |
1143 | goto free_bitmap; | |
d4b3b638 SD |
1144 | |
1145 | init_waitqueue_head(&area->wq); | |
1146 | if (!xol_add_vma(area)) | |
1147 | return area; | |
1148 | ||
c8a82538 ON |
1149 | __free_page(area->page); |
1150 | free_bitmap: | |
d4b3b638 | 1151 | kfree(area->bitmap); |
c8a82538 | 1152 | free_area: |
d4b3b638 | 1153 | kfree(area); |
c8a82538 | 1154 | out: |
9b545df8 ON |
1155 | area = mm->uprobes_state.xol_area; |
1156 | ret: | |
1157 | smp_read_barrier_depends(); /* pairs with wmb in xol_add_vma() */ | |
1158 | return area; | |
d4b3b638 SD |
1159 | } |
1160 | ||
1161 | /* | |
1162 | * uprobe_clear_state - Free the area allocated for slots. | |
1163 | */ | |
1164 | void uprobe_clear_state(struct mm_struct *mm) | |
1165 | { | |
1166 | struct xol_area *area = mm->uprobes_state.xol_area; | |
1167 | ||
1168 | if (!area) | |
1169 | return; | |
1170 | ||
1171 | put_page(area->page); | |
1172 | kfree(area->bitmap); | |
1173 | kfree(area); | |
1174 | } | |
1175 | ||
32cdba1e ON |
1176 | void uprobe_start_dup_mmap(void) |
1177 | { | |
1178 | percpu_down_read(&dup_mmap_sem); | |
1179 | } | |
1180 | ||
1181 | void uprobe_end_dup_mmap(void) | |
1182 | { | |
1183 | percpu_up_read(&dup_mmap_sem); | |
1184 | } | |
1185 | ||
f8ac4ec9 ON |
1186 | void uprobe_dup_mmap(struct mm_struct *oldmm, struct mm_struct *newmm) |
1187 | { | |
61559a81 ON |
1188 | newmm->uprobes_state.xol_area = NULL; |
1189 | ||
9f68f672 | 1190 | if (test_bit(MMF_HAS_UPROBES, &oldmm->flags)) { |
f8ac4ec9 | 1191 | set_bit(MMF_HAS_UPROBES, &newmm->flags); |
9f68f672 ON |
1192 | /* unconditionally, dup_mmap() skips VM_DONTCOPY vmas */ |
1193 | set_bit(MMF_RECALC_UPROBES, &newmm->flags); | |
1194 | } | |
f8ac4ec9 ON |
1195 | } |
1196 | ||
d4b3b638 SD |
1197 | /* |
1198 | * - search for a free slot. | |
1199 | */ | |
1200 | static unsigned long xol_take_insn_slot(struct xol_area *area) | |
1201 | { | |
1202 | unsigned long slot_addr; | |
1203 | int slot_nr; | |
1204 | ||
1205 | do { | |
1206 | slot_nr = find_first_zero_bit(area->bitmap, UINSNS_PER_PAGE); | |
1207 | if (slot_nr < UINSNS_PER_PAGE) { | |
1208 | if (!test_and_set_bit(slot_nr, area->bitmap)) | |
1209 | break; | |
1210 | ||
1211 | slot_nr = UINSNS_PER_PAGE; | |
1212 | continue; | |
1213 | } | |
1214 | wait_event(area->wq, (atomic_read(&area->slot_count) < UINSNS_PER_PAGE)); | |
1215 | } while (slot_nr >= UINSNS_PER_PAGE); | |
1216 | ||
1217 | slot_addr = area->vaddr + (slot_nr * UPROBE_XOL_SLOT_BYTES); | |
1218 | atomic_inc(&area->slot_count); | |
1219 | ||
1220 | return slot_addr; | |
1221 | } | |
1222 | ||
1223 | /* | |
a6cb3f6d | 1224 | * xol_get_insn_slot - allocate a slot for xol. |
d4b3b638 SD |
1225 | * Returns the allocated slot address or 0. |
1226 | */ | |
a6cb3f6d | 1227 | static unsigned long xol_get_insn_slot(struct uprobe *uprobe) |
d4b3b638 SD |
1228 | { |
1229 | struct xol_area *area; | |
1230 | unsigned long offset; | |
a6cb3f6d | 1231 | unsigned long xol_vaddr; |
d4b3b638 SD |
1232 | void *vaddr; |
1233 | ||
9b545df8 ON |
1234 | area = get_xol_area(); |
1235 | if (!area) | |
1236 | return 0; | |
d4b3b638 | 1237 | |
a6cb3f6d ON |
1238 | xol_vaddr = xol_take_insn_slot(area); |
1239 | if (unlikely(!xol_vaddr)) | |
d4b3b638 SD |
1240 | return 0; |
1241 | ||
a6cb3f6d ON |
1242 | /* Initialize the slot */ |
1243 | offset = xol_vaddr & ~PAGE_MASK; | |
d4b3b638 SD |
1244 | vaddr = kmap_atomic(area->page); |
1245 | memcpy(vaddr + offset, uprobe->arch.insn, MAX_UINSN_BYTES); | |
1246 | kunmap_atomic(vaddr); | |
65b6ecc0 RV |
1247 | /* |
1248 | * We probably need flush_icache_user_range() but it needs vma. | |
1249 | * This should work on supported architectures too. | |
1250 | */ | |
1251 | flush_dcache_page(area->page); | |
d4b3b638 | 1252 | |
a6cb3f6d | 1253 | return xol_vaddr; |
d4b3b638 SD |
1254 | } |
1255 | ||
1256 | /* | |
1257 | * xol_free_insn_slot - If slot was earlier allocated by | |
1258 | * @xol_get_insn_slot(), make the slot available for | |
1259 | * subsequent requests. | |
1260 | */ | |
1261 | static void xol_free_insn_slot(struct task_struct *tsk) | |
1262 | { | |
1263 | struct xol_area *area; | |
1264 | unsigned long vma_end; | |
1265 | unsigned long slot_addr; | |
1266 | ||
1267 | if (!tsk->mm || !tsk->mm->uprobes_state.xol_area || !tsk->utask) | |
1268 | return; | |
1269 | ||
1270 | slot_addr = tsk->utask->xol_vaddr; | |
af4355e9 | 1271 | if (unlikely(!slot_addr)) |
d4b3b638 SD |
1272 | return; |
1273 | ||
1274 | area = tsk->mm->uprobes_state.xol_area; | |
1275 | vma_end = area->vaddr + PAGE_SIZE; | |
1276 | if (area->vaddr <= slot_addr && slot_addr < vma_end) { | |
1277 | unsigned long offset; | |
1278 | int slot_nr; | |
1279 | ||
1280 | offset = slot_addr - area->vaddr; | |
1281 | slot_nr = offset / UPROBE_XOL_SLOT_BYTES; | |
1282 | if (slot_nr >= UINSNS_PER_PAGE) | |
1283 | return; | |
1284 | ||
1285 | clear_bit(slot_nr, area->bitmap); | |
1286 | atomic_dec(&area->slot_count); | |
1287 | if (waitqueue_active(&area->wq)) | |
1288 | wake_up(&area->wq); | |
1289 | ||
1290 | tsk->utask->xol_vaddr = 0; | |
1291 | } | |
1292 | } | |
1293 | ||
0326f5a9 SD |
1294 | /** |
1295 | * uprobe_get_swbp_addr - compute address of swbp given post-swbp regs | |
1296 | * @regs: Reflects the saved state of the task after it has hit a breakpoint | |
1297 | * instruction. | |
1298 | * Return the address of the breakpoint instruction. | |
1299 | */ | |
1300 | unsigned long __weak uprobe_get_swbp_addr(struct pt_regs *regs) | |
1301 | { | |
1302 | return instruction_pointer(regs) - UPROBE_SWBP_INSN_SIZE; | |
1303 | } | |
1304 | ||
1305 | /* | |
1306 | * Called with no locks held. | |
1307 | * Called in context of a exiting or a exec-ing thread. | |
1308 | */ | |
1309 | void uprobe_free_utask(struct task_struct *t) | |
1310 | { | |
1311 | struct uprobe_task *utask = t->utask; | |
1312 | ||
0326f5a9 SD |
1313 | if (!utask) |
1314 | return; | |
1315 | ||
1316 | if (utask->active_uprobe) | |
1317 | put_uprobe(utask->active_uprobe); | |
1318 | ||
d4b3b638 | 1319 | xol_free_insn_slot(t); |
0326f5a9 SD |
1320 | kfree(utask); |
1321 | t->utask = NULL; | |
1322 | } | |
1323 | ||
1324 | /* | |
1325 | * Called in context of a new clone/fork from copy_process. | |
1326 | */ | |
1327 | void uprobe_copy_process(struct task_struct *t) | |
1328 | { | |
1329 | t->utask = NULL; | |
0326f5a9 SD |
1330 | } |
1331 | ||
1332 | /* | |
5a2df662 ON |
1333 | * Allocate a uprobe_task object for the task if if necessary. |
1334 | * Called when the thread hits a breakpoint. | |
0326f5a9 SD |
1335 | * |
1336 | * Returns: | |
1337 | * - pointer to new uprobe_task on success | |
1338 | * - NULL otherwise | |
1339 | */ | |
5a2df662 | 1340 | static struct uprobe_task *get_utask(void) |
0326f5a9 | 1341 | { |
5a2df662 ON |
1342 | if (!current->utask) |
1343 | current->utask = kzalloc(sizeof(struct uprobe_task), GFP_KERNEL); | |
1344 | return current->utask; | |
0326f5a9 SD |
1345 | } |
1346 | ||
1347 | /* Prepare to single-step probed instruction out of line. */ | |
1348 | static int | |
a6cb3f6d | 1349 | pre_ssout(struct uprobe *uprobe, struct pt_regs *regs, unsigned long bp_vaddr) |
0326f5a9 | 1350 | { |
a6cb3f6d ON |
1351 | struct uprobe_task *utask; |
1352 | unsigned long xol_vaddr; | |
aba51024 | 1353 | int err; |
a6cb3f6d | 1354 | |
608e7427 ON |
1355 | utask = get_utask(); |
1356 | if (!utask) | |
1357 | return -ENOMEM; | |
a6cb3f6d ON |
1358 | |
1359 | xol_vaddr = xol_get_insn_slot(uprobe); | |
1360 | if (!xol_vaddr) | |
1361 | return -ENOMEM; | |
1362 | ||
1363 | utask->xol_vaddr = xol_vaddr; | |
1364 | utask->vaddr = bp_vaddr; | |
d4b3b638 | 1365 | |
aba51024 ON |
1366 | err = arch_uprobe_pre_xol(&uprobe->arch, regs); |
1367 | if (unlikely(err)) { | |
1368 | xol_free_insn_slot(current); | |
1369 | return err; | |
1370 | } | |
1371 | ||
608e7427 ON |
1372 | utask->active_uprobe = uprobe; |
1373 | utask->state = UTASK_SSTEP; | |
aba51024 | 1374 | return 0; |
0326f5a9 SD |
1375 | } |
1376 | ||
1377 | /* | |
1378 | * If we are singlestepping, then ensure this thread is not connected to | |
1379 | * non-fatal signals until completion of singlestep. When xol insn itself | |
1380 | * triggers the signal, restart the original insn even if the task is | |
1381 | * already SIGKILL'ed (since coredump should report the correct ip). This | |
1382 | * is even more important if the task has a handler for SIGSEGV/etc, The | |
1383 | * _same_ instruction should be repeated again after return from the signal | |
1384 | * handler, and SSTEP can never finish in this case. | |
1385 | */ | |
1386 | bool uprobe_deny_signal(void) | |
1387 | { | |
1388 | struct task_struct *t = current; | |
1389 | struct uprobe_task *utask = t->utask; | |
1390 | ||
1391 | if (likely(!utask || !utask->active_uprobe)) | |
1392 | return false; | |
1393 | ||
1394 | WARN_ON_ONCE(utask->state != UTASK_SSTEP); | |
1395 | ||
1396 | if (signal_pending(t)) { | |
1397 | spin_lock_irq(&t->sighand->siglock); | |
1398 | clear_tsk_thread_flag(t, TIF_SIGPENDING); | |
1399 | spin_unlock_irq(&t->sighand->siglock); | |
1400 | ||
1401 | if (__fatal_signal_pending(t) || arch_uprobe_xol_was_trapped(t)) { | |
1402 | utask->state = UTASK_SSTEP_TRAPPED; | |
1403 | set_tsk_thread_flag(t, TIF_UPROBE); | |
1404 | set_tsk_thread_flag(t, TIF_NOTIFY_RESUME); | |
1405 | } | |
1406 | } | |
1407 | ||
1408 | return true; | |
1409 | } | |
1410 | ||
1411 | /* | |
1412 | * Avoid singlestepping the original instruction if the original instruction | |
1413 | * is a NOP or can be emulated. | |
1414 | */ | |
1415 | static bool can_skip_sstep(struct uprobe *uprobe, struct pt_regs *regs) | |
1416 | { | |
71434f2f | 1417 | if (test_bit(UPROBE_SKIP_SSTEP, &uprobe->flags)) { |
0578a970 ON |
1418 | if (arch_uprobe_skip_sstep(&uprobe->arch, regs)) |
1419 | return true; | |
71434f2f | 1420 | clear_bit(UPROBE_SKIP_SSTEP, &uprobe->flags); |
0578a970 | 1421 | } |
0326f5a9 SD |
1422 | return false; |
1423 | } | |
1424 | ||
499a4f3e ON |
1425 | static void mmf_recalc_uprobes(struct mm_struct *mm) |
1426 | { | |
1427 | struct vm_area_struct *vma; | |
1428 | ||
1429 | for (vma = mm->mmap; vma; vma = vma->vm_next) { | |
1430 | if (!valid_vma(vma, false)) | |
1431 | continue; | |
1432 | /* | |
1433 | * This is not strictly accurate, we can race with | |
1434 | * uprobe_unregister() and see the already removed | |
1435 | * uprobe if delete_uprobe() was not yet called. | |
63633cbf | 1436 | * Or this uprobe can be filtered out. |
499a4f3e ON |
1437 | */ |
1438 | if (vma_has_uprobes(vma, vma->vm_start, vma->vm_end)) | |
1439 | return; | |
1440 | } | |
1441 | ||
1442 | clear_bit(MMF_HAS_UPROBES, &mm->flags); | |
1443 | } | |
1444 | ||
0908ad6e | 1445 | static int is_trap_at_addr(struct mm_struct *mm, unsigned long vaddr) |
ec75fba9 ON |
1446 | { |
1447 | struct page *page; | |
1448 | uprobe_opcode_t opcode; | |
1449 | int result; | |
1450 | ||
1451 | pagefault_disable(); | |
1452 | result = __copy_from_user_inatomic(&opcode, (void __user*)vaddr, | |
1453 | sizeof(opcode)); | |
1454 | pagefault_enable(); | |
1455 | ||
1456 | if (likely(result == 0)) | |
1457 | goto out; | |
1458 | ||
1459 | result = get_user_pages(NULL, mm, vaddr, 1, 0, 1, &page, NULL); | |
1460 | if (result < 0) | |
1461 | return result; | |
1462 | ||
ab0d805c | 1463 | copy_from_page(page, vaddr, &opcode, UPROBE_SWBP_INSN_SIZE); |
ec75fba9 ON |
1464 | put_page(page); |
1465 | out: | |
0908ad6e AM |
1466 | /* This needs to return true for any variant of the trap insn */ |
1467 | return is_trap_insn(&opcode); | |
ec75fba9 ON |
1468 | } |
1469 | ||
d790d346 | 1470 | static struct uprobe *find_active_uprobe(unsigned long bp_vaddr, int *is_swbp) |
0326f5a9 | 1471 | { |
3a9ea052 ON |
1472 | struct mm_struct *mm = current->mm; |
1473 | struct uprobe *uprobe = NULL; | |
0326f5a9 | 1474 | struct vm_area_struct *vma; |
0326f5a9 | 1475 | |
0326f5a9 SD |
1476 | down_read(&mm->mmap_sem); |
1477 | vma = find_vma(mm, bp_vaddr); | |
3a9ea052 ON |
1478 | if (vma && vma->vm_start <= bp_vaddr) { |
1479 | if (valid_vma(vma, false)) { | |
f281769e | 1480 | struct inode *inode = file_inode(vma->vm_file); |
cb113b47 | 1481 | loff_t offset = vaddr_to_offset(vma, bp_vaddr); |
0326f5a9 | 1482 | |
3a9ea052 ON |
1483 | uprobe = find_uprobe(inode, offset); |
1484 | } | |
d790d346 ON |
1485 | |
1486 | if (!uprobe) | |
0908ad6e | 1487 | *is_swbp = is_trap_at_addr(mm, bp_vaddr); |
d790d346 ON |
1488 | } else { |
1489 | *is_swbp = -EFAULT; | |
0326f5a9 | 1490 | } |
499a4f3e ON |
1491 | |
1492 | if (!uprobe && test_and_clear_bit(MMF_RECALC_UPROBES, &mm->flags)) | |
1493 | mmf_recalc_uprobes(mm); | |
0326f5a9 SD |
1494 | up_read(&mm->mmap_sem); |
1495 | ||
3a9ea052 ON |
1496 | return uprobe; |
1497 | } | |
1498 | ||
da1816b1 ON |
1499 | static void handler_chain(struct uprobe *uprobe, struct pt_regs *regs) |
1500 | { | |
1501 | struct uprobe_consumer *uc; | |
1502 | int remove = UPROBE_HANDLER_REMOVE; | |
1503 | ||
1504 | down_read(&uprobe->register_rwsem); | |
1505 | for (uc = uprobe->consumers; uc; uc = uc->next) { | |
1506 | int rc = uc->handler(uc, regs); | |
1507 | ||
1508 | WARN(rc & ~UPROBE_HANDLER_MASK, | |
1509 | "bad rc=0x%x from %pf()\n", rc, uc->handler); | |
1510 | remove &= rc; | |
1511 | } | |
1512 | ||
1513 | if (remove && uprobe->consumers) { | |
1514 | WARN_ON(!uprobe_is_active(uprobe)); | |
1515 | unapply_uprobe(uprobe, current->mm); | |
1516 | } | |
1517 | up_read(&uprobe->register_rwsem); | |
1518 | } | |
1519 | ||
3a9ea052 ON |
1520 | /* |
1521 | * Run handler and ask thread to singlestep. | |
1522 | * Ensure all non-fatal signals cannot interrupt thread while it singlesteps. | |
1523 | */ | |
1524 | static void handle_swbp(struct pt_regs *regs) | |
1525 | { | |
3a9ea052 ON |
1526 | struct uprobe *uprobe; |
1527 | unsigned long bp_vaddr; | |
56bb4cf6 | 1528 | int uninitialized_var(is_swbp); |
3a9ea052 ON |
1529 | |
1530 | bp_vaddr = uprobe_get_swbp_addr(regs); | |
d790d346 | 1531 | uprobe = find_active_uprobe(bp_vaddr, &is_swbp); |
3a9ea052 | 1532 | |
0326f5a9 | 1533 | if (!uprobe) { |
56bb4cf6 ON |
1534 | if (is_swbp > 0) { |
1535 | /* No matching uprobe; signal SIGTRAP. */ | |
1536 | send_sig(SIGTRAP, current, 0); | |
1537 | } else { | |
1538 | /* | |
1539 | * Either we raced with uprobe_unregister() or we can't | |
1540 | * access this memory. The latter is only possible if | |
1541 | * another thread plays with our ->mm. In both cases | |
1542 | * we can simply restart. If this vma was unmapped we | |
1543 | * can pretend this insn was not executed yet and get | |
1544 | * the (correct) SIGSEGV after restart. | |
1545 | */ | |
1546 | instruction_pointer_set(regs, bp_vaddr); | |
1547 | } | |
0326f5a9 SD |
1548 | return; |
1549 | } | |
74e59dfc ON |
1550 | |
1551 | /* change it in advance for ->handler() and restart */ | |
1552 | instruction_pointer_set(regs, bp_vaddr); | |
1553 | ||
142b18dd ON |
1554 | /* |
1555 | * TODO: move copy_insn/etc into _register and remove this hack. | |
1556 | * After we hit the bp, _unregister + _register can install the | |
1557 | * new and not-yet-analyzed uprobe at the same address, restart. | |
1558 | */ | |
1559 | smp_rmb(); /* pairs with wmb() in install_breakpoint() */ | |
71434f2f | 1560 | if (unlikely(!test_bit(UPROBE_COPY_INSN, &uprobe->flags))) |
74e59dfc | 1561 | goto out; |
0326f5a9 | 1562 | |
0326f5a9 | 1563 | handler_chain(uprobe, regs); |
0578a970 ON |
1564 | if (can_skip_sstep(uprobe, regs)) |
1565 | goto out; | |
0326f5a9 | 1566 | |
608e7427 | 1567 | if (!pre_ssout(uprobe, regs, bp_vaddr)) |
0326f5a9 | 1568 | return; |
0326f5a9 | 1569 | |
74e59dfc | 1570 | /* can_skip_sstep() succeeded, or restart if can't singlestep */ |
0578a970 | 1571 | out: |
8bd87445 | 1572 | put_uprobe(uprobe); |
0326f5a9 SD |
1573 | } |
1574 | ||
1575 | /* | |
1576 | * Perform required fix-ups and disable singlestep. | |
1577 | * Allow pending signals to take effect. | |
1578 | */ | |
1579 | static void handle_singlestep(struct uprobe_task *utask, struct pt_regs *regs) | |
1580 | { | |
1581 | struct uprobe *uprobe; | |
1582 | ||
1583 | uprobe = utask->active_uprobe; | |
1584 | if (utask->state == UTASK_SSTEP_ACK) | |
1585 | arch_uprobe_post_xol(&uprobe->arch, regs); | |
1586 | else if (utask->state == UTASK_SSTEP_TRAPPED) | |
1587 | arch_uprobe_abort_xol(&uprobe->arch, regs); | |
1588 | else | |
1589 | WARN_ON_ONCE(1); | |
1590 | ||
1591 | put_uprobe(uprobe); | |
1592 | utask->active_uprobe = NULL; | |
1593 | utask->state = UTASK_RUNNING; | |
d4b3b638 | 1594 | xol_free_insn_slot(current); |
0326f5a9 SD |
1595 | |
1596 | spin_lock_irq(¤t->sighand->siglock); | |
1597 | recalc_sigpending(); /* see uprobe_deny_signal() */ | |
1598 | spin_unlock_irq(¤t->sighand->siglock); | |
1599 | } | |
1600 | ||
1601 | /* | |
1b08e907 ON |
1602 | * On breakpoint hit, breakpoint notifier sets the TIF_UPROBE flag and |
1603 | * allows the thread to return from interrupt. After that handle_swbp() | |
1604 | * sets utask->active_uprobe. | |
0326f5a9 | 1605 | * |
1b08e907 ON |
1606 | * On singlestep exception, singlestep notifier sets the TIF_UPROBE flag |
1607 | * and allows the thread to return from interrupt. | |
0326f5a9 SD |
1608 | * |
1609 | * While returning to userspace, thread notices the TIF_UPROBE flag and calls | |
1610 | * uprobe_notify_resume(). | |
1611 | */ | |
1612 | void uprobe_notify_resume(struct pt_regs *regs) | |
1613 | { | |
1614 | struct uprobe_task *utask; | |
1615 | ||
db023ea5 ON |
1616 | clear_thread_flag(TIF_UPROBE); |
1617 | ||
0326f5a9 | 1618 | utask = current->utask; |
1b08e907 | 1619 | if (utask && utask->active_uprobe) |
0326f5a9 | 1620 | handle_singlestep(utask, regs); |
1b08e907 ON |
1621 | else |
1622 | handle_swbp(regs); | |
0326f5a9 SD |
1623 | } |
1624 | ||
1625 | /* | |
1626 | * uprobe_pre_sstep_notifier gets called from interrupt context as part of | |
1627 | * notifier mechanism. Set TIF_UPROBE flag and indicate breakpoint hit. | |
1628 | */ | |
1629 | int uprobe_pre_sstep_notifier(struct pt_regs *regs) | |
1630 | { | |
f8ac4ec9 | 1631 | if (!current->mm || !test_bit(MMF_HAS_UPROBES, ¤t->mm->flags)) |
0326f5a9 SD |
1632 | return 0; |
1633 | ||
0326f5a9 | 1634 | set_thread_flag(TIF_UPROBE); |
0326f5a9 SD |
1635 | return 1; |
1636 | } | |
1637 | ||
1638 | /* | |
1639 | * uprobe_post_sstep_notifier gets called in interrupt context as part of notifier | |
1640 | * mechanism. Set TIF_UPROBE flag and indicate completion of singlestep. | |
1641 | */ | |
1642 | int uprobe_post_sstep_notifier(struct pt_regs *regs) | |
1643 | { | |
1644 | struct uprobe_task *utask = current->utask; | |
1645 | ||
1646 | if (!current->mm || !utask || !utask->active_uprobe) | |
1647 | /* task is currently not uprobed */ | |
1648 | return 0; | |
1649 | ||
1650 | utask->state = UTASK_SSTEP_ACK; | |
1651 | set_thread_flag(TIF_UPROBE); | |
1652 | return 1; | |
1653 | } | |
1654 | ||
1655 | static struct notifier_block uprobe_exception_nb = { | |
1656 | .notifier_call = arch_uprobe_exception_notify, | |
1657 | .priority = INT_MAX-1, /* notified after kprobes, kgdb */ | |
1658 | }; | |
1659 | ||
2b144498 SD |
1660 | static int __init init_uprobes(void) |
1661 | { | |
1662 | int i; | |
1663 | ||
66d06dff | 1664 | for (i = 0; i < UPROBES_HASH_SZ; i++) |
2b144498 | 1665 | mutex_init(&uprobes_mmap_mutex[i]); |
0326f5a9 | 1666 | |
32cdba1e ON |
1667 | if (percpu_init_rwsem(&dup_mmap_sem)) |
1668 | return -ENOMEM; | |
1669 | ||
0326f5a9 | 1670 | return register_die_notifier(&uprobe_exception_nb); |
2b144498 | 1671 | } |
0326f5a9 | 1672 | module_init(init_uprobes); |
2b144498 SD |
1673 | |
1674 | static void __exit exit_uprobes(void) | |
1675 | { | |
1676 | } | |
2b144498 | 1677 | module_exit(exit_uprobes); |