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