]>
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 | |
90eec103 | 22 | * Copyright (C) 2011-2012 Red Hat, Inc., Peter Zijlstra |
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> | |
6e84f315 | 30 | #include <linux/sched/mm.h> |
f7ccbae4 | 31 | #include <linux/sched/coredump.h> |
e8440c14 | 32 | #include <linux/export.h> |
2b144498 SD |
33 | #include <linux/rmap.h> /* anon_vma_prepare */ |
34 | #include <linux/mmu_notifier.h> /* set_pte_at_notify */ | |
35 | #include <linux/swap.h> /* try_to_free_swap */ | |
0326f5a9 SD |
36 | #include <linux/ptrace.h> /* user_enable_single_step */ |
37 | #include <linux/kdebug.h> /* notifier mechanism */ | |
194f8dcb | 38 | #include "../../mm/internal.h" /* munlock_vma_page */ |
32cdba1e | 39 | #include <linux/percpu-rwsem.h> |
aa59c53f | 40 | #include <linux/task_work.h> |
40814f68 | 41 | #include <linux/shmem_fs.h> |
7b2d81d4 | 42 | |
2b144498 SD |
43 | #include <linux/uprobes.h> |
44 | ||
d4b3b638 SD |
45 | #define UINSNS_PER_PAGE (PAGE_SIZE/UPROBE_XOL_SLOT_BYTES) |
46 | #define MAX_UPROBE_XOL_SLOTS UINSNS_PER_PAGE | |
47 | ||
2b144498 | 48 | static struct rb_root uprobes_tree = RB_ROOT; |
441f1eb7 ON |
49 | /* |
50 | * allows us to skip the uprobe_mmap if there are no uprobe events active | |
51 | * at this time. Probably a fine grained per inode count is better? | |
52 | */ | |
53 | #define no_uprobe_events() RB_EMPTY_ROOT(&uprobes_tree) | |
7b2d81d4 | 54 | |
2b144498 SD |
55 | static DEFINE_SPINLOCK(uprobes_treelock); /* serialize rbtree access */ |
56 | ||
57 | #define UPROBES_HASH_SZ 13 | |
2b144498 SD |
58 | /* serialize uprobe->pending_list */ |
59 | static struct mutex uprobes_mmap_mutex[UPROBES_HASH_SZ]; | |
7b2d81d4 | 60 | #define uprobes_mmap_hash(v) (&uprobes_mmap_mutex[((unsigned long)(v)) % UPROBES_HASH_SZ]) |
2b144498 | 61 | |
32cdba1e ON |
62 | static struct percpu_rw_semaphore dup_mmap_sem; |
63 | ||
cb9a19fe | 64 | /* Have a copy of original instruction */ |
71434f2f | 65 | #define UPROBE_COPY_INSN 0 |
cb9a19fe | 66 | |
3ff54efd SD |
67 | struct uprobe { |
68 | struct rb_node rb_node; /* node in the rb tree */ | |
69 | atomic_t ref; | |
e591c8d7 | 70 | struct rw_semaphore register_rwsem; |
3ff54efd SD |
71 | struct rw_semaphore consumer_rwsem; |
72 | struct list_head pending_list; | |
73 | struct uprobe_consumer *consumers; | |
74 | struct inode *inode; /* Also hold a ref to inode */ | |
75 | loff_t offset; | |
1cc33161 | 76 | loff_t ref_ctr_offset; |
71434f2f | 77 | unsigned long flags; |
ad439356 ON |
78 | |
79 | /* | |
80 | * The generic code assumes that it has two members of unknown type | |
81 | * owned by the arch-specific code: | |
82 | * | |
83 | * insn - copy_insn() saves the original instruction here for | |
84 | * arch_uprobe_analyze_insn(). | |
85 | * | |
86 | * ixol - potentially modified instruction to execute out of | |
87 | * line, copied to xol_area by xol_get_insn_slot(). | |
88 | */ | |
3ff54efd SD |
89 | struct arch_uprobe arch; |
90 | }; | |
91 | ||
1cc33161 RB |
92 | struct delayed_uprobe { |
93 | struct list_head list; | |
94 | struct uprobe *uprobe; | |
95 | struct mm_struct *mm; | |
96 | }; | |
97 | ||
98 | static DEFINE_MUTEX(delayed_uprobe_lock); | |
99 | static LIST_HEAD(delayed_uprobe_list); | |
100 | ||
c912dae6 | 101 | /* |
ad439356 ON |
102 | * Execute out of line area: anonymous executable mapping installed |
103 | * by the probed task to execute the copy of the original instruction | |
104 | * mangled by set_swbp(). | |
105 | * | |
c912dae6 ON |
106 | * On a breakpoint hit, thread contests for a slot. It frees the |
107 | * slot after singlestep. Currently a fixed number of slots are | |
108 | * allocated. | |
109 | */ | |
110 | struct xol_area { | |
704bde3c ON |
111 | wait_queue_head_t wq; /* if all slots are busy */ |
112 | atomic_t slot_count; /* number of in-use slots */ | |
113 | unsigned long *bitmap; /* 0 = free slot */ | |
c912dae6 | 114 | |
704bde3c ON |
115 | struct vm_special_mapping xol_mapping; |
116 | struct page *pages[2]; | |
c912dae6 ON |
117 | /* |
118 | * We keep the vma's vm_start rather than a pointer to the vma | |
119 | * itself. The probed process or a naughty kernel module could make | |
120 | * the vma go away, and we must handle that reasonably gracefully. | |
121 | */ | |
704bde3c | 122 | unsigned long vaddr; /* Page(s) of instruction slots */ |
c912dae6 ON |
123 | }; |
124 | ||
2b144498 SD |
125 | /* |
126 | * valid_vma: Verify if the specified vma is an executable vma | |
127 | * Relax restrictions while unregistering: vm_flags might have | |
128 | * changed after breakpoint was inserted. | |
129 | * - is_register: indicates if we are in register context. | |
130 | * - Return 1 if the specified virtual address is in an | |
131 | * executable vma. | |
132 | */ | |
133 | static bool valid_vma(struct vm_area_struct *vma, bool is_register) | |
134 | { | |
13f59c5e | 135 | vm_flags_t flags = VM_HUGETLB | VM_MAYEXEC | VM_MAYSHARE; |
2b144498 | 136 | |
e40cfce6 ON |
137 | if (is_register) |
138 | flags |= VM_WRITE; | |
2b144498 | 139 | |
e40cfce6 | 140 | return vma->vm_file && (vma->vm_flags & flags) == VM_MAYEXEC; |
2b144498 SD |
141 | } |
142 | ||
57683f72 | 143 | static unsigned long offset_to_vaddr(struct vm_area_struct *vma, loff_t offset) |
2b144498 | 144 | { |
57683f72 | 145 | return vma->vm_start + offset - ((loff_t)vma->vm_pgoff << PAGE_SHIFT); |
2b144498 SD |
146 | } |
147 | ||
cb113b47 ON |
148 | static loff_t vaddr_to_offset(struct vm_area_struct *vma, unsigned long vaddr) |
149 | { | |
150 | return ((loff_t)vma->vm_pgoff << PAGE_SHIFT) + (vaddr - vma->vm_start); | |
151 | } | |
152 | ||
2b144498 SD |
153 | /** |
154 | * __replace_page - replace page in vma by new page. | |
155 | * based on replace_page in mm/ksm.c | |
156 | * | |
157 | * @vma: vma that holds the pte pointing to page | |
c517ee74 | 158 | * @addr: address the old @page is mapped at |
2b144498 SD |
159 | * @page: the cowed page we are replacing by kpage |
160 | * @kpage: the modified page we replace page by | |
161 | * | |
162 | * Returns 0 on success, -EFAULT on failure. | |
163 | */ | |
c517ee74 | 164 | static int __replace_page(struct vm_area_struct *vma, unsigned long addr, |
bdfaa2ee | 165 | struct page *old_page, struct page *new_page) |
2b144498 SD |
166 | { |
167 | struct mm_struct *mm = vma->vm_mm; | |
14fa2daa KS |
168 | struct page_vma_mapped_walk pvmw = { |
169 | .page = old_page, | |
170 | .vma = vma, | |
171 | .address = addr, | |
172 | }; | |
9f92448c | 173 | int err; |
6bdb913f HE |
174 | /* For mmu_notifiers */ |
175 | const unsigned long mmun_start = addr; | |
176 | const unsigned long mmun_end = addr + PAGE_SIZE; | |
00501b53 JW |
177 | struct mem_cgroup *memcg; |
178 | ||
14fa2daa KS |
179 | VM_BUG_ON_PAGE(PageTransHuge(old_page), old_page); |
180 | ||
bdfaa2ee | 181 | err = mem_cgroup_try_charge(new_page, vma->vm_mm, GFP_KERNEL, &memcg, |
f627c2f5 | 182 | false); |
00501b53 JW |
183 | if (err) |
184 | return err; | |
2b144498 | 185 | |
194f8dcb | 186 | /* For try_to_free_swap() and munlock_vma_page() below */ |
bdfaa2ee | 187 | lock_page(old_page); |
9f92448c | 188 | |
6bdb913f | 189 | mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end); |
9f92448c | 190 | err = -EAGAIN; |
14fa2daa | 191 | if (!page_vma_mapped_walk(&pvmw)) { |
bdfaa2ee | 192 | mem_cgroup_cancel_charge(new_page, memcg, false); |
9f92448c | 193 | goto unlock; |
6c4687cc | 194 | } |
14fa2daa | 195 | VM_BUG_ON_PAGE(addr != pvmw.address, old_page); |
2b144498 | 196 | |
bdfaa2ee ON |
197 | get_page(new_page); |
198 | page_add_new_anon_rmap(new_page, vma, addr, false); | |
199 | mem_cgroup_commit_charge(new_page, memcg, false, false); | |
200 | lru_cache_add_active_or_unevictable(new_page, vma); | |
2b144498 | 201 | |
bdfaa2ee ON |
202 | if (!PageAnon(old_page)) { |
203 | dec_mm_counter(mm, mm_counter_file(old_page)); | |
7396fa81 SD |
204 | inc_mm_counter(mm, MM_ANONPAGES); |
205 | } | |
206 | ||
14fa2daa KS |
207 | flush_cache_page(vma, addr, pte_pfn(*pvmw.pte)); |
208 | ptep_clear_flush_notify(vma, addr, pvmw.pte); | |
209 | set_pte_at_notify(mm, addr, pvmw.pte, | |
210 | mk_pte(new_page, vma->vm_page_prot)); | |
2b144498 | 211 | |
bdfaa2ee ON |
212 | page_remove_rmap(old_page, false); |
213 | if (!page_mapped(old_page)) | |
214 | try_to_free_swap(old_page); | |
14fa2daa | 215 | page_vma_mapped_walk_done(&pvmw); |
2b144498 | 216 | |
194f8dcb | 217 | if (vma->vm_flags & VM_LOCKED) |
bdfaa2ee ON |
218 | munlock_vma_page(old_page); |
219 | put_page(old_page); | |
194f8dcb | 220 | |
9f92448c ON |
221 | err = 0; |
222 | unlock: | |
6bdb913f | 223 | mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end); |
bdfaa2ee | 224 | unlock_page(old_page); |
9f92448c | 225 | return err; |
2b144498 SD |
226 | } |
227 | ||
228 | /** | |
5cb4ac3a | 229 | * is_swbp_insn - check if instruction is breakpoint instruction. |
2b144498 | 230 | * @insn: instruction to be checked. |
5cb4ac3a | 231 | * Default implementation of is_swbp_insn |
2b144498 SD |
232 | * Returns true if @insn is a breakpoint instruction. |
233 | */ | |
5cb4ac3a | 234 | bool __weak is_swbp_insn(uprobe_opcode_t *insn) |
2b144498 | 235 | { |
5cb4ac3a | 236 | return *insn == UPROBE_SWBP_INSN; |
2b144498 SD |
237 | } |
238 | ||
0908ad6e AM |
239 | /** |
240 | * is_trap_insn - check if instruction is breakpoint instruction. | |
241 | * @insn: instruction to be checked. | |
242 | * Default implementation of is_trap_insn | |
243 | * Returns true if @insn is a breakpoint instruction. | |
244 | * | |
245 | * This function is needed for the case where an architecture has multiple | |
246 | * trap instructions (like powerpc). | |
247 | */ | |
248 | bool __weak is_trap_insn(uprobe_opcode_t *insn) | |
249 | { | |
250 | return is_swbp_insn(insn); | |
251 | } | |
252 | ||
ab0d805c | 253 | static void copy_from_page(struct page *page, unsigned long vaddr, void *dst, int len) |
cceb55aa ON |
254 | { |
255 | void *kaddr = kmap_atomic(page); | |
ab0d805c | 256 | memcpy(dst, kaddr + (vaddr & ~PAGE_MASK), len); |
cceb55aa ON |
257 | kunmap_atomic(kaddr); |
258 | } | |
259 | ||
5669ccee ON |
260 | static void copy_to_page(struct page *page, unsigned long vaddr, const void *src, int len) |
261 | { | |
262 | void *kaddr = kmap_atomic(page); | |
263 | memcpy(kaddr + (vaddr & ~PAGE_MASK), src, len); | |
264 | kunmap_atomic(kaddr); | |
265 | } | |
266 | ||
ed6f6a50 ON |
267 | static int verify_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t *new_opcode) |
268 | { | |
269 | uprobe_opcode_t old_opcode; | |
270 | bool is_swbp; | |
271 | ||
0908ad6e AM |
272 | /* |
273 | * Note: We only check if the old_opcode is UPROBE_SWBP_INSN here. | |
274 | * We do not check if it is any other 'trap variant' which could | |
275 | * be conditional trap instruction such as the one powerpc supports. | |
276 | * | |
277 | * The logic is that we do not care if the underlying instruction | |
278 | * is a trap variant; uprobes always wins over any other (gdb) | |
279 | * breakpoint. | |
280 | */ | |
ab0d805c | 281 | copy_from_page(page, vaddr, &old_opcode, UPROBE_SWBP_INSN_SIZE); |
ed6f6a50 ON |
282 | is_swbp = is_swbp_insn(&old_opcode); |
283 | ||
284 | if (is_swbp_insn(new_opcode)) { | |
285 | if (is_swbp) /* register: already installed? */ | |
286 | return 0; | |
287 | } else { | |
288 | if (!is_swbp) /* unregister: was it changed by us? */ | |
076a365b | 289 | return 0; |
ed6f6a50 ON |
290 | } |
291 | ||
292 | return 1; | |
293 | } | |
294 | ||
1cc33161 RB |
295 | static struct delayed_uprobe * |
296 | delayed_uprobe_check(struct uprobe *uprobe, struct mm_struct *mm) | |
297 | { | |
298 | struct delayed_uprobe *du; | |
299 | ||
300 | list_for_each_entry(du, &delayed_uprobe_list, list) | |
301 | if (du->uprobe == uprobe && du->mm == mm) | |
302 | return du; | |
303 | return NULL; | |
304 | } | |
305 | ||
306 | static int delayed_uprobe_add(struct uprobe *uprobe, struct mm_struct *mm) | |
307 | { | |
308 | struct delayed_uprobe *du; | |
309 | ||
310 | if (delayed_uprobe_check(uprobe, mm)) | |
311 | return 0; | |
312 | ||
313 | du = kzalloc(sizeof(*du), GFP_KERNEL); | |
314 | if (!du) | |
315 | return -ENOMEM; | |
316 | ||
317 | du->uprobe = uprobe; | |
318 | du->mm = mm; | |
319 | list_add(&du->list, &delayed_uprobe_list); | |
320 | return 0; | |
321 | } | |
322 | ||
323 | static void delayed_uprobe_delete(struct delayed_uprobe *du) | |
324 | { | |
325 | if (WARN_ON(!du)) | |
326 | return; | |
327 | list_del(&du->list); | |
328 | kfree(du); | |
329 | } | |
330 | ||
331 | static void delayed_uprobe_remove(struct uprobe *uprobe, struct mm_struct *mm) | |
332 | { | |
333 | struct list_head *pos, *q; | |
334 | struct delayed_uprobe *du; | |
335 | ||
336 | if (!uprobe && !mm) | |
337 | return; | |
338 | ||
339 | list_for_each_safe(pos, q, &delayed_uprobe_list) { | |
340 | du = list_entry(pos, struct delayed_uprobe, list); | |
341 | ||
342 | if (uprobe && du->uprobe != uprobe) | |
343 | continue; | |
344 | if (mm && du->mm != mm) | |
345 | continue; | |
346 | ||
347 | delayed_uprobe_delete(du); | |
348 | } | |
349 | } | |
350 | ||
351 | static bool valid_ref_ctr_vma(struct uprobe *uprobe, | |
352 | struct vm_area_struct *vma) | |
353 | { | |
354 | unsigned long vaddr = offset_to_vaddr(vma, uprobe->ref_ctr_offset); | |
355 | ||
356 | return uprobe->ref_ctr_offset && | |
357 | vma->vm_file && | |
358 | file_inode(vma->vm_file) == uprobe->inode && | |
359 | (vma->vm_flags & (VM_WRITE|VM_SHARED)) == VM_WRITE && | |
360 | vma->vm_start <= vaddr && | |
361 | vma->vm_end > vaddr; | |
362 | } | |
363 | ||
364 | static struct vm_area_struct * | |
365 | find_ref_ctr_vma(struct uprobe *uprobe, struct mm_struct *mm) | |
366 | { | |
367 | struct vm_area_struct *tmp; | |
368 | ||
369 | for (tmp = mm->mmap; tmp; tmp = tmp->vm_next) | |
370 | if (valid_ref_ctr_vma(uprobe, tmp)) | |
371 | return tmp; | |
372 | ||
373 | return NULL; | |
374 | } | |
375 | ||
376 | static int | |
377 | __update_ref_ctr(struct mm_struct *mm, unsigned long vaddr, short d) | |
378 | { | |
379 | void *kaddr; | |
380 | struct page *page; | |
381 | struct vm_area_struct *vma; | |
382 | int ret; | |
383 | short *ptr; | |
384 | ||
385 | if (!vaddr || !d) | |
386 | return -EINVAL; | |
387 | ||
388 | ret = get_user_pages_remote(NULL, mm, vaddr, 1, | |
389 | FOLL_WRITE, &page, &vma, NULL); | |
390 | if (unlikely(ret <= 0)) { | |
391 | /* | |
392 | * We are asking for 1 page. If get_user_pages_remote() fails, | |
393 | * it may return 0, in that case we have to return error. | |
394 | */ | |
395 | return ret == 0 ? -EBUSY : ret; | |
396 | } | |
397 | ||
398 | kaddr = kmap_atomic(page); | |
399 | ptr = kaddr + (vaddr & ~PAGE_MASK); | |
400 | ||
401 | if (unlikely(*ptr + d < 0)) { | |
402 | pr_warn("ref_ctr going negative. vaddr: 0x%lx, " | |
403 | "curr val: %d, delta: %d\n", vaddr, *ptr, d); | |
404 | ret = -EINVAL; | |
405 | goto out; | |
406 | } | |
407 | ||
408 | *ptr += d; | |
409 | ret = 0; | |
410 | out: | |
411 | kunmap_atomic(kaddr); | |
412 | put_page(page); | |
413 | return ret; | |
414 | } | |
415 | ||
416 | static void update_ref_ctr_warn(struct uprobe *uprobe, | |
417 | struct mm_struct *mm, short d) | |
418 | { | |
419 | pr_warn("ref_ctr %s failed for inode: 0x%lx offset: " | |
420 | "0x%llx ref_ctr_offset: 0x%llx of mm: 0x%pK\n", | |
421 | d > 0 ? "increment" : "decrement", uprobe->inode->i_ino, | |
422 | (unsigned long long) uprobe->offset, | |
423 | (unsigned long long) uprobe->ref_ctr_offset, mm); | |
424 | } | |
425 | ||
426 | static int update_ref_ctr(struct uprobe *uprobe, struct mm_struct *mm, | |
427 | short d) | |
428 | { | |
429 | struct vm_area_struct *rc_vma; | |
430 | unsigned long rc_vaddr; | |
431 | int ret = 0; | |
432 | ||
433 | rc_vma = find_ref_ctr_vma(uprobe, mm); | |
434 | ||
435 | if (rc_vma) { | |
436 | rc_vaddr = offset_to_vaddr(rc_vma, uprobe->ref_ctr_offset); | |
437 | ret = __update_ref_ctr(mm, rc_vaddr, d); | |
438 | if (ret) | |
439 | update_ref_ctr_warn(uprobe, mm, d); | |
440 | ||
441 | if (d > 0) | |
442 | return ret; | |
443 | } | |
444 | ||
445 | mutex_lock(&delayed_uprobe_lock); | |
446 | if (d > 0) | |
447 | ret = delayed_uprobe_add(uprobe, mm); | |
448 | else | |
449 | delayed_uprobe_remove(uprobe, mm); | |
450 | mutex_unlock(&delayed_uprobe_lock); | |
451 | ||
452 | return ret; | |
453 | } | |
454 | ||
2b144498 SD |
455 | /* |
456 | * NOTE: | |
457 | * Expect the breakpoint instruction to be the smallest size instruction for | |
458 | * the architecture. If an arch has variable length instruction and the | |
459 | * breakpoint instruction is not of the smallest length instruction | |
0908ad6e | 460 | * supported by that architecture then we need to modify is_trap_at_addr and |
f72d41fa ON |
461 | * uprobe_write_opcode accordingly. This would never be a problem for archs |
462 | * that have fixed length instructions. | |
29dedee0 | 463 | * |
f72d41fa | 464 | * uprobe_write_opcode - write the opcode at a given virtual address. |
2b144498 | 465 | * @mm: the probed process address space. |
2b144498 SD |
466 | * @vaddr: the virtual address to store the opcode. |
467 | * @opcode: opcode to be written at @vaddr. | |
468 | * | |
29dedee0 | 469 | * Called with mm->mmap_sem held for write. |
2b144498 SD |
470 | * Return 0 (success) or a negative errno. |
471 | */ | |
6d43743e RB |
472 | int uprobe_write_opcode(struct arch_uprobe *auprobe, struct mm_struct *mm, |
473 | unsigned long vaddr, uprobe_opcode_t opcode) | |
2b144498 | 474 | { |
1cc33161 | 475 | struct uprobe *uprobe; |
2b144498 | 476 | struct page *old_page, *new_page; |
2b144498 | 477 | struct vm_area_struct *vma; |
1cc33161 RB |
478 | int ret, is_register, ref_ctr_updated = 0; |
479 | ||
480 | is_register = is_swbp_insn(&opcode); | |
481 | uprobe = container_of(auprobe, struct uprobe, arch); | |
f403072c | 482 | |
5323ce71 | 483 | retry: |
2b144498 | 484 | /* Read the page with vaddr into memory */ |
c8394812 KS |
485 | ret = get_user_pages_remote(NULL, mm, vaddr, 1, |
486 | FOLL_FORCE | FOLL_SPLIT, &old_page, &vma, NULL); | |
2b144498 SD |
487 | if (ret <= 0) |
488 | return ret; | |
7b2d81d4 | 489 | |
ed6f6a50 ON |
490 | ret = verify_opcode(old_page, vaddr, &opcode); |
491 | if (ret <= 0) | |
492 | goto put_old; | |
493 | ||
1cc33161 RB |
494 | /* We are going to replace instruction, update ref_ctr. */ |
495 | if (!ref_ctr_updated && uprobe->ref_ctr_offset) { | |
496 | ret = update_ref_ctr(uprobe, mm, is_register ? 1 : -1); | |
497 | if (ret) | |
498 | goto put_old; | |
499 | ||
500 | ref_ctr_updated = 1; | |
501 | } | |
502 | ||
29dedee0 ON |
503 | ret = anon_vma_prepare(vma); |
504 | if (ret) | |
505 | goto put_old; | |
506 | ||
2b144498 SD |
507 | ret = -ENOMEM; |
508 | new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vaddr); | |
509 | if (!new_page) | |
9f92448c | 510 | goto put_old; |
2b144498 | 511 | |
29dedee0 | 512 | __SetPageUptodate(new_page); |
3f47107c ON |
513 | copy_highpage(new_page, old_page); |
514 | copy_to_page(new_page, vaddr, &opcode, UPROBE_SWBP_INSN_SIZE); | |
2b144498 | 515 | |
c517ee74 | 516 | ret = __replace_page(vma, vaddr, old_page, new_page); |
09cbfeaf | 517 | put_page(new_page); |
9f92448c | 518 | put_old: |
7b2d81d4 IM |
519 | put_page(old_page); |
520 | ||
5323ce71 ON |
521 | if (unlikely(ret == -EAGAIN)) |
522 | goto retry; | |
1cc33161 RB |
523 | |
524 | /* Revert back reference counter if instruction update failed. */ | |
525 | if (ret && is_register && ref_ctr_updated) | |
526 | update_ref_ctr(uprobe, mm, -1); | |
527 | ||
2b144498 SD |
528 | return ret; |
529 | } | |
530 | ||
2b144498 | 531 | /** |
5cb4ac3a | 532 | * set_swbp - store breakpoint at a given address. |
e3343e6a | 533 | * @auprobe: arch specific probepoint information. |
2b144498 | 534 | * @mm: the probed process address space. |
2b144498 SD |
535 | * @vaddr: the virtual address to insert the opcode. |
536 | * | |
537 | * For mm @mm, store the breakpoint instruction at @vaddr. | |
538 | * Return 0 (success) or a negative errno. | |
539 | */ | |
5cb4ac3a | 540 | int __weak set_swbp(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr) |
2b144498 | 541 | { |
6d43743e | 542 | return uprobe_write_opcode(auprobe, mm, vaddr, UPROBE_SWBP_INSN); |
2b144498 SD |
543 | } |
544 | ||
545 | /** | |
546 | * set_orig_insn - Restore the original instruction. | |
547 | * @mm: the probed process address space. | |
e3343e6a | 548 | * @auprobe: arch specific probepoint information. |
2b144498 | 549 | * @vaddr: the virtual address to insert the opcode. |
2b144498 SD |
550 | * |
551 | * For mm @mm, restore the original opcode (opcode) at @vaddr. | |
552 | * Return 0 (success) or a negative errno. | |
553 | */ | |
7b2d81d4 | 554 | int __weak |
ded86e7c | 555 | set_orig_insn(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr) |
2b144498 | 556 | { |
6d43743e RB |
557 | return uprobe_write_opcode(auprobe, mm, vaddr, |
558 | *(uprobe_opcode_t *)&auprobe->insn); | |
2b144498 SD |
559 | } |
560 | ||
f231722a ON |
561 | static struct uprobe *get_uprobe(struct uprobe *uprobe) |
562 | { | |
563 | atomic_inc(&uprobe->ref); | |
564 | return uprobe; | |
565 | } | |
566 | ||
567 | static void put_uprobe(struct uprobe *uprobe) | |
568 | { | |
1cc33161 RB |
569 | if (atomic_dec_and_test(&uprobe->ref)) { |
570 | /* | |
571 | * If application munmap(exec_vma) before uprobe_unregister() | |
572 | * gets called, we don't get a chance to remove uprobe from | |
573 | * delayed_uprobe_list from remove_breakpoint(). Do it here. | |
574 | */ | |
575 | delayed_uprobe_remove(uprobe, NULL); | |
f231722a | 576 | kfree(uprobe); |
1cc33161 | 577 | } |
f231722a ON |
578 | } |
579 | ||
2b144498 SD |
580 | static int match_uprobe(struct uprobe *l, struct uprobe *r) |
581 | { | |
582 | if (l->inode < r->inode) | |
583 | return -1; | |
7b2d81d4 | 584 | |
2b144498 SD |
585 | if (l->inode > r->inode) |
586 | return 1; | |
2b144498 | 587 | |
7b2d81d4 IM |
588 | if (l->offset < r->offset) |
589 | return -1; | |
590 | ||
591 | if (l->offset > r->offset) | |
592 | return 1; | |
2b144498 SD |
593 | |
594 | return 0; | |
595 | } | |
596 | ||
597 | static struct uprobe *__find_uprobe(struct inode *inode, loff_t offset) | |
598 | { | |
599 | struct uprobe u = { .inode = inode, .offset = offset }; | |
600 | struct rb_node *n = uprobes_tree.rb_node; | |
601 | struct uprobe *uprobe; | |
602 | int match; | |
603 | ||
604 | while (n) { | |
605 | uprobe = rb_entry(n, struct uprobe, rb_node); | |
606 | match = match_uprobe(&u, uprobe); | |
f231722a ON |
607 | if (!match) |
608 | return get_uprobe(uprobe); | |
7b2d81d4 | 609 | |
2b144498 SD |
610 | if (match < 0) |
611 | n = n->rb_left; | |
612 | else | |
613 | n = n->rb_right; | |
614 | } | |
615 | return NULL; | |
616 | } | |
617 | ||
618 | /* | |
619 | * Find a uprobe corresponding to a given inode:offset | |
620 | * Acquires uprobes_treelock | |
621 | */ | |
622 | static struct uprobe *find_uprobe(struct inode *inode, loff_t offset) | |
623 | { | |
624 | struct uprobe *uprobe; | |
2b144498 | 625 | |
6f47caa0 | 626 | spin_lock(&uprobes_treelock); |
2b144498 | 627 | uprobe = __find_uprobe(inode, offset); |
6f47caa0 | 628 | spin_unlock(&uprobes_treelock); |
7b2d81d4 | 629 | |
2b144498 SD |
630 | return uprobe; |
631 | } | |
632 | ||
633 | static struct uprobe *__insert_uprobe(struct uprobe *uprobe) | |
634 | { | |
635 | struct rb_node **p = &uprobes_tree.rb_node; | |
636 | struct rb_node *parent = NULL; | |
637 | struct uprobe *u; | |
638 | int match; | |
639 | ||
640 | while (*p) { | |
641 | parent = *p; | |
642 | u = rb_entry(parent, struct uprobe, rb_node); | |
643 | match = match_uprobe(uprobe, u); | |
f231722a ON |
644 | if (!match) |
645 | return get_uprobe(u); | |
2b144498 SD |
646 | |
647 | if (match < 0) | |
648 | p = &parent->rb_left; | |
649 | else | |
650 | p = &parent->rb_right; | |
651 | ||
652 | } | |
7b2d81d4 | 653 | |
2b144498 SD |
654 | u = NULL; |
655 | rb_link_node(&uprobe->rb_node, parent, p); | |
656 | rb_insert_color(&uprobe->rb_node, &uprobes_tree); | |
657 | /* get access + creation ref */ | |
658 | atomic_set(&uprobe->ref, 2); | |
7b2d81d4 | 659 | |
2b144498 SD |
660 | return u; |
661 | } | |
662 | ||
663 | /* | |
7b2d81d4 | 664 | * Acquire uprobes_treelock. |
2b144498 SD |
665 | * Matching uprobe already exists in rbtree; |
666 | * increment (access refcount) and return the matching uprobe. | |
667 | * | |
668 | * No matching uprobe; insert the uprobe in rb_tree; | |
669 | * get a double refcount (access + creation) and return NULL. | |
670 | */ | |
671 | static struct uprobe *insert_uprobe(struct uprobe *uprobe) | |
672 | { | |
2b144498 SD |
673 | struct uprobe *u; |
674 | ||
6f47caa0 | 675 | spin_lock(&uprobes_treelock); |
2b144498 | 676 | u = __insert_uprobe(uprobe); |
6f47caa0 | 677 | spin_unlock(&uprobes_treelock); |
7b2d81d4 | 678 | |
2b144498 SD |
679 | return u; |
680 | } | |
681 | ||
1cc33161 RB |
682 | static struct uprobe *alloc_uprobe(struct inode *inode, loff_t offset, |
683 | loff_t ref_ctr_offset) | |
2b144498 SD |
684 | { |
685 | struct uprobe *uprobe, *cur_uprobe; | |
686 | ||
687 | uprobe = kzalloc(sizeof(struct uprobe), GFP_KERNEL); | |
688 | if (!uprobe) | |
689 | return NULL; | |
690 | ||
61f94203 | 691 | uprobe->inode = inode; |
2b144498 | 692 | uprobe->offset = offset; |
1cc33161 | 693 | uprobe->ref_ctr_offset = ref_ctr_offset; |
e591c8d7 | 694 | init_rwsem(&uprobe->register_rwsem); |
2b144498 | 695 | init_rwsem(&uprobe->consumer_rwsem); |
2b144498 SD |
696 | |
697 | /* add to uprobes_tree, sorted on inode:offset */ | |
698 | cur_uprobe = insert_uprobe(uprobe); | |
2b144498 SD |
699 | /* a uprobe exists for this inode:offset combination */ |
700 | if (cur_uprobe) { | |
701 | kfree(uprobe); | |
702 | uprobe = cur_uprobe; | |
7b2d81d4 IM |
703 | } |
704 | ||
2b144498 SD |
705 | return uprobe; |
706 | } | |
707 | ||
9a98e03c | 708 | static void consumer_add(struct uprobe *uprobe, struct uprobe_consumer *uc) |
2b144498 SD |
709 | { |
710 | down_write(&uprobe->consumer_rwsem); | |
e3343e6a SD |
711 | uc->next = uprobe->consumers; |
712 | uprobe->consumers = uc; | |
2b144498 | 713 | up_write(&uprobe->consumer_rwsem); |
2b144498 SD |
714 | } |
715 | ||
716 | /* | |
e3343e6a SD |
717 | * For uprobe @uprobe, delete the consumer @uc. |
718 | * Return true if the @uc is deleted successfully | |
2b144498 SD |
719 | * or return false. |
720 | */ | |
e3343e6a | 721 | static bool consumer_del(struct uprobe *uprobe, struct uprobe_consumer *uc) |
2b144498 SD |
722 | { |
723 | struct uprobe_consumer **con; | |
724 | bool ret = false; | |
725 | ||
726 | down_write(&uprobe->consumer_rwsem); | |
727 | for (con = &uprobe->consumers; *con; con = &(*con)->next) { | |
e3343e6a SD |
728 | if (*con == uc) { |
729 | *con = uc->next; | |
2b144498 SD |
730 | ret = true; |
731 | break; | |
732 | } | |
733 | } | |
734 | up_write(&uprobe->consumer_rwsem); | |
7b2d81d4 | 735 | |
2b144498 SD |
736 | return ret; |
737 | } | |
738 | ||
2ded0980 ON |
739 | static int __copy_insn(struct address_space *mapping, struct file *filp, |
740 | void *insn, int nbytes, loff_t offset) | |
2b144498 | 741 | { |
2b144498 | 742 | struct page *page; |
2b144498 | 743 | /* |
40814f68 ON |
744 | * Ensure that the page that has the original instruction is populated |
745 | * and in page-cache. If ->readpage == NULL it must be shmem_mapping(), | |
746 | * see uprobe_register(). | |
2b144498 | 747 | */ |
40814f68 | 748 | if (mapping->a_ops->readpage) |
09cbfeaf | 749 | page = read_mapping_page(mapping, offset >> PAGE_SHIFT, filp); |
40814f68 | 750 | else |
09cbfeaf | 751 | page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT); |
2b144498 SD |
752 | if (IS_ERR(page)) |
753 | return PTR_ERR(page); | |
754 | ||
2edb7b55 | 755 | copy_from_page(page, offset, insn, nbytes); |
09cbfeaf | 756 | put_page(page); |
7b2d81d4 | 757 | |
2b144498 SD |
758 | return 0; |
759 | } | |
760 | ||
d436615e | 761 | static int copy_insn(struct uprobe *uprobe, struct file *filp) |
2b144498 | 762 | { |
2ded0980 ON |
763 | struct address_space *mapping = uprobe->inode->i_mapping; |
764 | loff_t offs = uprobe->offset; | |
803200e2 ON |
765 | void *insn = &uprobe->arch.insn; |
766 | int size = sizeof(uprobe->arch.insn); | |
2ded0980 ON |
767 | int len, err = -EIO; |
768 | ||
769 | /* Copy only available bytes, -EIO if nothing was read */ | |
770 | do { | |
771 | if (offs >= i_size_read(uprobe->inode)) | |
772 | break; | |
773 | ||
774 | len = min_t(int, size, PAGE_SIZE - (offs & ~PAGE_MASK)); | |
775 | err = __copy_insn(mapping, filp, insn, len, offs); | |
fc36f595 | 776 | if (err) |
2ded0980 ON |
777 | break; |
778 | ||
779 | insn += len; | |
780 | offs += len; | |
781 | size -= len; | |
782 | } while (size); | |
783 | ||
784 | return err; | |
2b144498 SD |
785 | } |
786 | ||
cb9a19fe ON |
787 | static int prepare_uprobe(struct uprobe *uprobe, struct file *file, |
788 | struct mm_struct *mm, unsigned long vaddr) | |
789 | { | |
790 | int ret = 0; | |
791 | ||
71434f2f | 792 | if (test_bit(UPROBE_COPY_INSN, &uprobe->flags)) |
cb9a19fe ON |
793 | return ret; |
794 | ||
d4d3ccc6 ON |
795 | /* TODO: move this into _register, until then we abuse this sem. */ |
796 | down_write(&uprobe->consumer_rwsem); | |
71434f2f | 797 | if (test_bit(UPROBE_COPY_INSN, &uprobe->flags)) |
4710f05f ON |
798 | goto out; |
799 | ||
cb9a19fe ON |
800 | ret = copy_insn(uprobe, file); |
801 | if (ret) | |
802 | goto out; | |
803 | ||
804 | ret = -ENOTSUPP; | |
803200e2 | 805 | if (is_trap_insn((uprobe_opcode_t *)&uprobe->arch.insn)) |
cb9a19fe ON |
806 | goto out; |
807 | ||
808 | ret = arch_uprobe_analyze_insn(&uprobe->arch, mm, vaddr); | |
809 | if (ret) | |
810 | goto out; | |
811 | ||
f72d41fa | 812 | /* uprobe_write_opcode() assumes we don't cross page boundary */ |
cb9a19fe ON |
813 | BUG_ON((uprobe->offset & ~PAGE_MASK) + |
814 | UPROBE_SWBP_INSN_SIZE > PAGE_SIZE); | |
815 | ||
816 | smp_wmb(); /* pairs with rmb() in find_active_uprobe() */ | |
71434f2f | 817 | set_bit(UPROBE_COPY_INSN, &uprobe->flags); |
cb9a19fe ON |
818 | |
819 | out: | |
d4d3ccc6 | 820 | up_write(&uprobe->consumer_rwsem); |
4710f05f | 821 | |
cb9a19fe ON |
822 | return ret; |
823 | } | |
824 | ||
8a7f2fa0 ON |
825 | static inline bool consumer_filter(struct uprobe_consumer *uc, |
826 | enum uprobe_filter_ctx ctx, struct mm_struct *mm) | |
806a98bd | 827 | { |
8a7f2fa0 | 828 | return !uc->filter || uc->filter(uc, ctx, mm); |
806a98bd ON |
829 | } |
830 | ||
8a7f2fa0 ON |
831 | static bool filter_chain(struct uprobe *uprobe, |
832 | enum uprobe_filter_ctx ctx, struct mm_struct *mm) | |
63633cbf | 833 | { |
1ff6fee5 ON |
834 | struct uprobe_consumer *uc; |
835 | bool ret = false; | |
836 | ||
837 | down_read(&uprobe->consumer_rwsem); | |
838 | for (uc = uprobe->consumers; uc; uc = uc->next) { | |
8a7f2fa0 | 839 | ret = consumer_filter(uc, ctx, mm); |
1ff6fee5 ON |
840 | if (ret) |
841 | break; | |
842 | } | |
843 | up_read(&uprobe->consumer_rwsem); | |
844 | ||
845 | return ret; | |
63633cbf ON |
846 | } |
847 | ||
e3343e6a SD |
848 | static int |
849 | install_breakpoint(struct uprobe *uprobe, struct mm_struct *mm, | |
816c03fb | 850 | struct vm_area_struct *vma, unsigned long vaddr) |
2b144498 | 851 | { |
f8ac4ec9 | 852 | bool first_uprobe; |
2b144498 SD |
853 | int ret; |
854 | ||
cb9a19fe ON |
855 | ret = prepare_uprobe(uprobe, vma->vm_file, mm, vaddr); |
856 | if (ret) | |
857 | return ret; | |
682968e0 | 858 | |
f8ac4ec9 ON |
859 | /* |
860 | * set MMF_HAS_UPROBES in advance for uprobe_pre_sstep_notifier(), | |
861 | * the task can hit this breakpoint right after __replace_page(). | |
862 | */ | |
863 | first_uprobe = !test_bit(MMF_HAS_UPROBES, &mm->flags); | |
864 | if (first_uprobe) | |
865 | set_bit(MMF_HAS_UPROBES, &mm->flags); | |
866 | ||
816c03fb | 867 | ret = set_swbp(&uprobe->arch, mm, vaddr); |
9f68f672 ON |
868 | if (!ret) |
869 | clear_bit(MMF_RECALC_UPROBES, &mm->flags); | |
870 | else if (first_uprobe) | |
f8ac4ec9 | 871 | clear_bit(MMF_HAS_UPROBES, &mm->flags); |
2b144498 SD |
872 | |
873 | return ret; | |
874 | } | |
875 | ||
076a365b | 876 | static int |
816c03fb | 877 | remove_breakpoint(struct uprobe *uprobe, struct mm_struct *mm, unsigned long vaddr) |
2b144498 | 878 | { |
9f68f672 | 879 | set_bit(MMF_RECALC_UPROBES, &mm->flags); |
076a365b | 880 | return set_orig_insn(&uprobe->arch, mm, vaddr); |
2b144498 SD |
881 | } |
882 | ||
06b7bcd8 ON |
883 | static inline bool uprobe_is_active(struct uprobe *uprobe) |
884 | { | |
885 | return !RB_EMPTY_NODE(&uprobe->rb_node); | |
886 | } | |
0326f5a9 | 887 | /* |
778b032d ON |
888 | * There could be threads that have already hit the breakpoint. They |
889 | * will recheck the current insn and restart if find_uprobe() fails. | |
890 | * See find_active_uprobe(). | |
0326f5a9 | 891 | */ |
2b144498 SD |
892 | static void delete_uprobe(struct uprobe *uprobe) |
893 | { | |
06b7bcd8 ON |
894 | if (WARN_ON(!uprobe_is_active(uprobe))) |
895 | return; | |
896 | ||
6f47caa0 | 897 | spin_lock(&uprobes_treelock); |
2b144498 | 898 | rb_erase(&uprobe->rb_node, &uprobes_tree); |
6f47caa0 | 899 | spin_unlock(&uprobes_treelock); |
06b7bcd8 | 900 | RB_CLEAR_NODE(&uprobe->rb_node); /* for uprobe_is_active() */ |
2b144498 | 901 | put_uprobe(uprobe); |
2b144498 SD |
902 | } |
903 | ||
26872090 ON |
904 | struct map_info { |
905 | struct map_info *next; | |
906 | struct mm_struct *mm; | |
816c03fb | 907 | unsigned long vaddr; |
26872090 ON |
908 | }; |
909 | ||
910 | static inline struct map_info *free_map_info(struct map_info *info) | |
2b144498 | 911 | { |
26872090 ON |
912 | struct map_info *next = info->next; |
913 | kfree(info); | |
914 | return next; | |
915 | } | |
916 | ||
917 | static struct map_info * | |
918 | build_map_info(struct address_space *mapping, loff_t offset, bool is_register) | |
919 | { | |
920 | unsigned long pgoff = offset >> PAGE_SHIFT; | |
2b144498 | 921 | struct vm_area_struct *vma; |
26872090 ON |
922 | struct map_info *curr = NULL; |
923 | struct map_info *prev = NULL; | |
924 | struct map_info *info; | |
925 | int more = 0; | |
2b144498 | 926 | |
26872090 | 927 | again: |
4a23717a | 928 | i_mmap_lock_read(mapping); |
6b2dbba8 | 929 | vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) { |
2b144498 SD |
930 | if (!valid_vma(vma, is_register)) |
931 | continue; | |
932 | ||
7a5bfb66 ON |
933 | if (!prev && !more) { |
934 | /* | |
c8c06efa | 935 | * Needs GFP_NOWAIT to avoid i_mmap_rwsem recursion through |
7a5bfb66 ON |
936 | * reclaim. This is optimistic, no harm done if it fails. |
937 | */ | |
938 | prev = kmalloc(sizeof(struct map_info), | |
939 | GFP_NOWAIT | __GFP_NOMEMALLOC | __GFP_NOWARN); | |
940 | if (prev) | |
941 | prev->next = NULL; | |
942 | } | |
26872090 ON |
943 | if (!prev) { |
944 | more++; | |
945 | continue; | |
2b144498 | 946 | } |
2b144498 | 947 | |
388f7934 | 948 | if (!mmget_not_zero(vma->vm_mm)) |
26872090 | 949 | continue; |
7b2d81d4 | 950 | |
26872090 ON |
951 | info = prev; |
952 | prev = prev->next; | |
953 | info->next = curr; | |
954 | curr = info; | |
2b144498 | 955 | |
26872090 | 956 | info->mm = vma->vm_mm; |
57683f72 | 957 | info->vaddr = offset_to_vaddr(vma, offset); |
26872090 | 958 | } |
4a23717a | 959 | i_mmap_unlock_read(mapping); |
2b144498 | 960 | |
26872090 ON |
961 | if (!more) |
962 | goto out; | |
963 | ||
964 | prev = curr; | |
965 | while (curr) { | |
966 | mmput(curr->mm); | |
967 | curr = curr->next; | |
968 | } | |
7b2d81d4 | 969 | |
26872090 ON |
970 | do { |
971 | info = kmalloc(sizeof(struct map_info), GFP_KERNEL); | |
972 | if (!info) { | |
973 | curr = ERR_PTR(-ENOMEM); | |
974 | goto out; | |
975 | } | |
976 | info->next = prev; | |
977 | prev = info; | |
978 | } while (--more); | |
979 | ||
980 | goto again; | |
981 | out: | |
982 | while (prev) | |
983 | prev = free_map_info(prev); | |
984 | return curr; | |
2b144498 SD |
985 | } |
986 | ||
bdf8647c ON |
987 | static int |
988 | register_for_each_vma(struct uprobe *uprobe, struct uprobe_consumer *new) | |
2b144498 | 989 | { |
bdf8647c | 990 | bool is_register = !!new; |
26872090 ON |
991 | struct map_info *info; |
992 | int err = 0; | |
2b144498 | 993 | |
32cdba1e | 994 | percpu_down_write(&dup_mmap_sem); |
26872090 ON |
995 | info = build_map_info(uprobe->inode->i_mapping, |
996 | uprobe->offset, is_register); | |
32cdba1e ON |
997 | if (IS_ERR(info)) { |
998 | err = PTR_ERR(info); | |
999 | goto out; | |
1000 | } | |
7b2d81d4 | 1001 | |
26872090 ON |
1002 | while (info) { |
1003 | struct mm_struct *mm = info->mm; | |
1004 | struct vm_area_struct *vma; | |
7b2d81d4 | 1005 | |
076a365b | 1006 | if (err && is_register) |
26872090 | 1007 | goto free; |
7b2d81d4 | 1008 | |
77fc4af1 | 1009 | down_write(&mm->mmap_sem); |
f4d6dfe5 ON |
1010 | vma = find_vma(mm, info->vaddr); |
1011 | if (!vma || !valid_vma(vma, is_register) || | |
f281769e | 1012 | file_inode(vma->vm_file) != uprobe->inode) |
26872090 ON |
1013 | goto unlock; |
1014 | ||
f4d6dfe5 ON |
1015 | if (vma->vm_start > info->vaddr || |
1016 | vaddr_to_offset(vma, info->vaddr) != uprobe->offset) | |
26872090 | 1017 | goto unlock; |
2b144498 | 1018 | |
806a98bd ON |
1019 | if (is_register) { |
1020 | /* consult only the "caller", new consumer. */ | |
bdf8647c | 1021 | if (consumer_filter(new, |
8a7f2fa0 | 1022 | UPROBE_FILTER_REGISTER, mm)) |
806a98bd ON |
1023 | err = install_breakpoint(uprobe, mm, vma, info->vaddr); |
1024 | } else if (test_bit(MMF_HAS_UPROBES, &mm->flags)) { | |
8a7f2fa0 ON |
1025 | if (!filter_chain(uprobe, |
1026 | UPROBE_FILTER_UNREGISTER, mm)) | |
806a98bd ON |
1027 | err |= remove_breakpoint(uprobe, mm, info->vaddr); |
1028 | } | |
78f74116 | 1029 | |
26872090 ON |
1030 | unlock: |
1031 | up_write(&mm->mmap_sem); | |
1032 | free: | |
1033 | mmput(mm); | |
1034 | info = free_map_info(info); | |
2b144498 | 1035 | } |
32cdba1e ON |
1036 | out: |
1037 | percpu_up_write(&dup_mmap_sem); | |
26872090 | 1038 | return err; |
2b144498 SD |
1039 | } |
1040 | ||
38e967ae RB |
1041 | static void |
1042 | __uprobe_unregister(struct uprobe *uprobe, struct uprobe_consumer *uc) | |
2b144498 | 1043 | { |
04aab9b2 ON |
1044 | int err; |
1045 | ||
06d07139 | 1046 | if (WARN_ON(!consumer_del(uprobe, uc))) |
04aab9b2 | 1047 | return; |
2b144498 | 1048 | |
bdf8647c | 1049 | err = register_for_each_vma(uprobe, NULL); |
bb929284 ON |
1050 | /* TODO : cant unregister? schedule a worker thread */ |
1051 | if (!uprobe->consumers && !err) | |
1052 | delete_uprobe(uprobe); | |
2b144498 SD |
1053 | } |
1054 | ||
1055 | /* | |
7140ad38 | 1056 | * uprobe_unregister - unregister an already registered probe. |
38e967ae RB |
1057 | * @inode: the file in which the probe has to be removed. |
1058 | * @offset: offset from the start of the file. | |
1059 | * @uc: identify which probe if multiple probes are colocated. | |
1060 | */ | |
1061 | void uprobe_unregister(struct inode *inode, loff_t offset, struct uprobe_consumer *uc) | |
1062 | { | |
1063 | struct uprobe *uprobe; | |
1064 | ||
1065 | uprobe = find_uprobe(inode, offset); | |
1066 | if (WARN_ON(!uprobe)) | |
1067 | return; | |
1068 | ||
1069 | down_write(&uprobe->register_rwsem); | |
1070 | __uprobe_unregister(uprobe, uc); | |
1071 | up_write(&uprobe->register_rwsem); | |
1072 | put_uprobe(uprobe); | |
1073 | } | |
1074 | EXPORT_SYMBOL_GPL(uprobe_unregister); | |
1075 | ||
1076 | /* | |
1077 | * __uprobe_register - register a probe | |
2b144498 SD |
1078 | * @inode: the file in which the probe has to be placed. |
1079 | * @offset: offset from the start of the file. | |
e3343e6a | 1080 | * @uc: information on howto handle the probe.. |
2b144498 | 1081 | * |
38e967ae | 1082 | * Apart from the access refcount, __uprobe_register() takes a creation |
2b144498 SD |
1083 | * refcount (thro alloc_uprobe) if and only if this @uprobe is getting |
1084 | * inserted into the rbtree (i.e first consumer for a @inode:@offset | |
7b2d81d4 | 1085 | * tuple). Creation refcount stops uprobe_unregister from freeing the |
2b144498 | 1086 | * @uprobe even before the register operation is complete. Creation |
e3343e6a | 1087 | * refcount is released when the last @uc for the @uprobe |
38e967ae | 1088 | * unregisters. Caller of __uprobe_register() is required to keep @inode |
61f94203 | 1089 | * (and the containing mount) referenced. |
2b144498 SD |
1090 | * |
1091 | * Return errno if it cannot successully install probes | |
1092 | * else return 0 (success) | |
1093 | */ | |
38e967ae | 1094 | static int __uprobe_register(struct inode *inode, loff_t offset, |
1cc33161 | 1095 | loff_t ref_ctr_offset, struct uprobe_consumer *uc) |
2b144498 SD |
1096 | { |
1097 | struct uprobe *uprobe; | |
7b2d81d4 | 1098 | int ret; |
2b144498 | 1099 | |
ea024870 AA |
1100 | /* Uprobe must have at least one set consumer */ |
1101 | if (!uc->handler && !uc->ret_handler) | |
1102 | return -EINVAL; | |
1103 | ||
40814f68 ON |
1104 | /* copy_insn() uses read_mapping_page() or shmem_read_mapping_page() */ |
1105 | if (!inode->i_mapping->a_ops->readpage && !shmem_mapping(inode->i_mapping)) | |
41ccba02 | 1106 | return -EIO; |
f0744af7 | 1107 | /* Racy, just to catch the obvious mistakes */ |
2b144498 | 1108 | if (offset > i_size_read(inode)) |
7b2d81d4 | 1109 | return -EINVAL; |
2b144498 | 1110 | |
66d06dff | 1111 | retry: |
1cc33161 | 1112 | uprobe = alloc_uprobe(inode, offset, ref_ctr_offset); |
66d06dff ON |
1113 | if (!uprobe) |
1114 | return -ENOMEM; | |
1115 | /* | |
1116 | * We can race with uprobe_unregister()->delete_uprobe(). | |
1117 | * Check uprobe_is_active() and retry if it is false. | |
1118 | */ | |
1119 | down_write(&uprobe->register_rwsem); | |
1120 | ret = -EAGAIN; | |
1121 | if (likely(uprobe_is_active(uprobe))) { | |
38e967ae RB |
1122 | consumer_add(uprobe, uc); |
1123 | ret = register_for_each_vma(uprobe, uc); | |
9a98e03c | 1124 | if (ret) |
04aab9b2 | 1125 | __uprobe_unregister(uprobe, uc); |
2b144498 | 1126 | } |
66d06dff ON |
1127 | up_write(&uprobe->register_rwsem); |
1128 | put_uprobe(uprobe); | |
2b144498 | 1129 | |
66d06dff ON |
1130 | if (unlikely(ret == -EAGAIN)) |
1131 | goto retry; | |
2b144498 SD |
1132 | return ret; |
1133 | } | |
38e967ae RB |
1134 | |
1135 | int uprobe_register(struct inode *inode, loff_t offset, | |
1136 | struct uprobe_consumer *uc) | |
1137 | { | |
1cc33161 | 1138 | return __uprobe_register(inode, offset, 0, uc); |
38e967ae | 1139 | } |
e8440c14 | 1140 | EXPORT_SYMBOL_GPL(uprobe_register); |
2b144498 | 1141 | |
1cc33161 RB |
1142 | int uprobe_register_refctr(struct inode *inode, loff_t offset, |
1143 | loff_t ref_ctr_offset, struct uprobe_consumer *uc) | |
1144 | { | |
1145 | return __uprobe_register(inode, offset, ref_ctr_offset, uc); | |
1146 | } | |
1147 | EXPORT_SYMBOL_GPL(uprobe_register_refctr); | |
1148 | ||
bdf8647c | 1149 | /* |
788faab7 | 1150 | * uprobe_apply - unregister an already registered probe. |
bdf8647c ON |
1151 | * @inode: the file in which the probe has to be removed. |
1152 | * @offset: offset from the start of the file. | |
1153 | * @uc: consumer which wants to add more or remove some breakpoints | |
1154 | * @add: add or remove the breakpoints | |
1155 | */ | |
1156 | int uprobe_apply(struct inode *inode, loff_t offset, | |
1157 | struct uprobe_consumer *uc, bool add) | |
1158 | { | |
1159 | struct uprobe *uprobe; | |
1160 | struct uprobe_consumer *con; | |
1161 | int ret = -ENOENT; | |
1162 | ||
1163 | uprobe = find_uprobe(inode, offset); | |
06d07139 | 1164 | if (WARN_ON(!uprobe)) |
bdf8647c ON |
1165 | return ret; |
1166 | ||
1167 | down_write(&uprobe->register_rwsem); | |
1168 | for (con = uprobe->consumers; con && con != uc ; con = con->next) | |
1169 | ; | |
1170 | if (con) | |
1171 | ret = register_for_each_vma(uprobe, add ? uc : NULL); | |
1172 | up_write(&uprobe->register_rwsem); | |
1173 | put_uprobe(uprobe); | |
1174 | ||
1175 | return ret; | |
1176 | } | |
1177 | ||
da1816b1 ON |
1178 | static int unapply_uprobe(struct uprobe *uprobe, struct mm_struct *mm) |
1179 | { | |
1180 | struct vm_area_struct *vma; | |
1181 | int err = 0; | |
1182 | ||
1183 | down_read(&mm->mmap_sem); | |
1184 | for (vma = mm->mmap; vma; vma = vma->vm_next) { | |
1185 | unsigned long vaddr; | |
1186 | loff_t offset; | |
1187 | ||
1188 | if (!valid_vma(vma, false) || | |
f281769e | 1189 | file_inode(vma->vm_file) != uprobe->inode) |
da1816b1 ON |
1190 | continue; |
1191 | ||
1192 | offset = (loff_t)vma->vm_pgoff << PAGE_SHIFT; | |
1193 | if (uprobe->offset < offset || | |
1194 | uprobe->offset >= offset + vma->vm_end - vma->vm_start) | |
1195 | continue; | |
1196 | ||
1197 | vaddr = offset_to_vaddr(vma, uprobe->offset); | |
1198 | err |= remove_breakpoint(uprobe, mm, vaddr); | |
1199 | } | |
1200 | up_read(&mm->mmap_sem); | |
1201 | ||
1202 | return err; | |
1203 | } | |
1204 | ||
891c3970 ON |
1205 | static struct rb_node * |
1206 | find_node_in_range(struct inode *inode, loff_t min, loff_t max) | |
2b144498 | 1207 | { |
2b144498 | 1208 | struct rb_node *n = uprobes_tree.rb_node; |
2b144498 SD |
1209 | |
1210 | while (n) { | |
891c3970 | 1211 | struct uprobe *u = rb_entry(n, struct uprobe, rb_node); |
2b144498 | 1212 | |
891c3970 | 1213 | if (inode < u->inode) { |
2b144498 | 1214 | n = n->rb_left; |
891c3970 | 1215 | } else if (inode > u->inode) { |
2b144498 | 1216 | n = n->rb_right; |
891c3970 ON |
1217 | } else { |
1218 | if (max < u->offset) | |
1219 | n = n->rb_left; | |
1220 | else if (min > u->offset) | |
1221 | n = n->rb_right; | |
1222 | else | |
1223 | break; | |
1224 | } | |
2b144498 | 1225 | } |
7b2d81d4 | 1226 | |
891c3970 | 1227 | return n; |
2b144498 SD |
1228 | } |
1229 | ||
1230 | /* | |
891c3970 | 1231 | * For a given range in vma, build a list of probes that need to be inserted. |
2b144498 | 1232 | */ |
891c3970 ON |
1233 | static void build_probe_list(struct inode *inode, |
1234 | struct vm_area_struct *vma, | |
1235 | unsigned long start, unsigned long end, | |
1236 | struct list_head *head) | |
2b144498 | 1237 | { |
891c3970 | 1238 | loff_t min, max; |
891c3970 ON |
1239 | struct rb_node *n, *t; |
1240 | struct uprobe *u; | |
7b2d81d4 | 1241 | |
891c3970 | 1242 | INIT_LIST_HEAD(head); |
cb113b47 | 1243 | min = vaddr_to_offset(vma, start); |
891c3970 | 1244 | max = min + (end - start) - 1; |
2b144498 | 1245 | |
6f47caa0 | 1246 | spin_lock(&uprobes_treelock); |
891c3970 ON |
1247 | n = find_node_in_range(inode, min, max); |
1248 | if (n) { | |
1249 | for (t = n; t; t = rb_prev(t)) { | |
1250 | u = rb_entry(t, struct uprobe, rb_node); | |
1251 | if (u->inode != inode || u->offset < min) | |
1252 | break; | |
1253 | list_add(&u->pending_list, head); | |
f231722a | 1254 | get_uprobe(u); |
891c3970 ON |
1255 | } |
1256 | for (t = n; (t = rb_next(t)); ) { | |
1257 | u = rb_entry(t, struct uprobe, rb_node); | |
1258 | if (u->inode != inode || u->offset > max) | |
1259 | break; | |
1260 | list_add(&u->pending_list, head); | |
f231722a | 1261 | get_uprobe(u); |
891c3970 | 1262 | } |
2b144498 | 1263 | } |
6f47caa0 | 1264 | spin_unlock(&uprobes_treelock); |
2b144498 SD |
1265 | } |
1266 | ||
1cc33161 RB |
1267 | /* @vma contains reference counter, not the probed instruction. */ |
1268 | static int delayed_ref_ctr_inc(struct vm_area_struct *vma) | |
1269 | { | |
1270 | struct list_head *pos, *q; | |
1271 | struct delayed_uprobe *du; | |
1272 | unsigned long vaddr; | |
1273 | int ret = 0, err = 0; | |
1274 | ||
1275 | mutex_lock(&delayed_uprobe_lock); | |
1276 | list_for_each_safe(pos, q, &delayed_uprobe_list) { | |
1277 | du = list_entry(pos, struct delayed_uprobe, list); | |
1278 | ||
1279 | if (du->mm != vma->vm_mm || | |
1280 | !valid_ref_ctr_vma(du->uprobe, vma)) | |
1281 | continue; | |
1282 | ||
1283 | vaddr = offset_to_vaddr(vma, du->uprobe->ref_ctr_offset); | |
1284 | ret = __update_ref_ctr(vma->vm_mm, vaddr, 1); | |
1285 | if (ret) { | |
1286 | update_ref_ctr_warn(du->uprobe, vma->vm_mm, 1); | |
1287 | if (!err) | |
1288 | err = ret; | |
1289 | } | |
1290 | delayed_uprobe_delete(du); | |
1291 | } | |
1292 | mutex_unlock(&delayed_uprobe_lock); | |
1293 | return err; | |
1294 | } | |
1295 | ||
2b144498 | 1296 | /* |
5e5be71a | 1297 | * Called from mmap_region/vma_adjust with mm->mmap_sem acquired. |
2b144498 | 1298 | * |
5e5be71a ON |
1299 | * Currently we ignore all errors and always return 0, the callers |
1300 | * can't handle the failure anyway. | |
2b144498 | 1301 | */ |
7b2d81d4 | 1302 | int uprobe_mmap(struct vm_area_struct *vma) |
2b144498 SD |
1303 | { |
1304 | struct list_head tmp_list; | |
665605a2 | 1305 | struct uprobe *uprobe, *u; |
2b144498 | 1306 | struct inode *inode; |
2b144498 | 1307 | |
1cc33161 RB |
1308 | if (no_uprobe_events()) |
1309 | return 0; | |
1310 | ||
1311 | if (vma->vm_file && | |
1312 | (vma->vm_flags & (VM_WRITE|VM_SHARED)) == VM_WRITE && | |
1313 | test_bit(MMF_HAS_UPROBES, &vma->vm_mm->flags)) | |
1314 | delayed_ref_ctr_inc(vma); | |
1315 | ||
1316 | if (!valid_vma(vma, true)) | |
7b2d81d4 | 1317 | return 0; |
2b144498 | 1318 | |
f281769e | 1319 | inode = file_inode(vma->vm_file); |
2b144498 | 1320 | if (!inode) |
7b2d81d4 | 1321 | return 0; |
2b144498 | 1322 | |
2b144498 | 1323 | mutex_lock(uprobes_mmap_hash(inode)); |
891c3970 | 1324 | build_probe_list(inode, vma, vma->vm_start, vma->vm_end, &tmp_list); |
806a98bd ON |
1325 | /* |
1326 | * We can race with uprobe_unregister(), this uprobe can be already | |
1327 | * removed. But in this case filter_chain() must return false, all | |
1328 | * consumers have gone away. | |
1329 | */ | |
665605a2 | 1330 | list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) { |
806a98bd | 1331 | if (!fatal_signal_pending(current) && |
8a7f2fa0 | 1332 | filter_chain(uprobe, UPROBE_FILTER_MMAP, vma->vm_mm)) { |
57683f72 | 1333 | unsigned long vaddr = offset_to_vaddr(vma, uprobe->offset); |
5e5be71a | 1334 | install_breakpoint(uprobe, vma->vm_mm, vma, vaddr); |
2b144498 SD |
1335 | } |
1336 | put_uprobe(uprobe); | |
1337 | } | |
2b144498 SD |
1338 | mutex_unlock(uprobes_mmap_hash(inode)); |
1339 | ||
5e5be71a | 1340 | return 0; |
2b144498 SD |
1341 | } |
1342 | ||
9f68f672 ON |
1343 | static bool |
1344 | vma_has_uprobes(struct vm_area_struct *vma, unsigned long start, unsigned long end) | |
1345 | { | |
1346 | loff_t min, max; | |
1347 | struct inode *inode; | |
1348 | struct rb_node *n; | |
1349 | ||
f281769e | 1350 | inode = file_inode(vma->vm_file); |
9f68f672 ON |
1351 | |
1352 | min = vaddr_to_offset(vma, start); | |
1353 | max = min + (end - start) - 1; | |
1354 | ||
1355 | spin_lock(&uprobes_treelock); | |
1356 | n = find_node_in_range(inode, min, max); | |
1357 | spin_unlock(&uprobes_treelock); | |
1358 | ||
1359 | return !!n; | |
1360 | } | |
1361 | ||
682968e0 SD |
1362 | /* |
1363 | * Called in context of a munmap of a vma. | |
1364 | */ | |
cbc91f71 | 1365 | void uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end) |
682968e0 | 1366 | { |
441f1eb7 | 1367 | if (no_uprobe_events() || !valid_vma(vma, false)) |
682968e0 SD |
1368 | return; |
1369 | ||
2fd611a9 ON |
1370 | if (!atomic_read(&vma->vm_mm->mm_users)) /* called by mmput() ? */ |
1371 | return; | |
1372 | ||
9f68f672 ON |
1373 | if (!test_bit(MMF_HAS_UPROBES, &vma->vm_mm->flags) || |
1374 | test_bit(MMF_RECALC_UPROBES, &vma->vm_mm->flags)) | |
f8ac4ec9 ON |
1375 | return; |
1376 | ||
9f68f672 ON |
1377 | if (vma_has_uprobes(vma, start, end)) |
1378 | set_bit(MMF_RECALC_UPROBES, &vma->vm_mm->flags); | |
682968e0 SD |
1379 | } |
1380 | ||
d4b3b638 | 1381 | /* Slot allocation for XOL */ |
6441ec8b | 1382 | static int xol_add_vma(struct mm_struct *mm, struct xol_area *area) |
d4b3b638 | 1383 | { |
704bde3c ON |
1384 | struct vm_area_struct *vma; |
1385 | int ret; | |
d4b3b638 | 1386 | |
598fdc1d MH |
1387 | if (down_write_killable(&mm->mmap_sem)) |
1388 | return -EINTR; | |
1389 | ||
704bde3c ON |
1390 | if (mm->uprobes_state.xol_area) { |
1391 | ret = -EALREADY; | |
d4b3b638 | 1392 | goto fail; |
704bde3c | 1393 | } |
d4b3b638 | 1394 | |
af0d95af ON |
1395 | if (!area->vaddr) { |
1396 | /* Try to map as high as possible, this is only a hint. */ | |
1397 | area->vaddr = get_unmapped_area(NULL, TASK_SIZE - PAGE_SIZE, | |
1398 | PAGE_SIZE, 0, 0); | |
1399 | if (area->vaddr & ~PAGE_MASK) { | |
1400 | ret = area->vaddr; | |
1401 | goto fail; | |
1402 | } | |
d4b3b638 SD |
1403 | } |
1404 | ||
704bde3c ON |
1405 | vma = _install_special_mapping(mm, area->vaddr, PAGE_SIZE, |
1406 | VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO, | |
1407 | &area->xol_mapping); | |
1408 | if (IS_ERR(vma)) { | |
1409 | ret = PTR_ERR(vma); | |
d4b3b638 | 1410 | goto fail; |
704bde3c | 1411 | } |
d4b3b638 | 1412 | |
704bde3c | 1413 | ret = 0; |
5c6338b4 PM |
1414 | /* pairs with get_xol_area() */ |
1415 | smp_store_release(&mm->uprobes_state.xol_area, area); /* ^^^ */ | |
c8a82538 | 1416 | fail: |
d4b3b638 | 1417 | up_write(&mm->mmap_sem); |
d4b3b638 SD |
1418 | |
1419 | return ret; | |
1420 | } | |
1421 | ||
af0d95af | 1422 | static struct xol_area *__create_xol_area(unsigned long vaddr) |
d4b3b638 | 1423 | { |
9b545df8 | 1424 | struct mm_struct *mm = current->mm; |
e78aebfd | 1425 | uprobe_opcode_t insn = UPROBE_SWBP_INSN; |
6441ec8b | 1426 | struct xol_area *area; |
9b545df8 | 1427 | |
af0d95af | 1428 | area = kmalloc(sizeof(*area), GFP_KERNEL); |
d4b3b638 | 1429 | if (unlikely(!area)) |
c8a82538 | 1430 | goto out; |
d4b3b638 | 1431 | |
6396bb22 KC |
1432 | area->bitmap = kcalloc(BITS_TO_LONGS(UINSNS_PER_PAGE), sizeof(long), |
1433 | GFP_KERNEL); | |
d4b3b638 | 1434 | if (!area->bitmap) |
c8a82538 ON |
1435 | goto free_area; |
1436 | ||
704bde3c | 1437 | area->xol_mapping.name = "[uprobes]"; |
869ae761 | 1438 | area->xol_mapping.fault = NULL; |
704bde3c | 1439 | area->xol_mapping.pages = area->pages; |
f58bea2f ON |
1440 | area->pages[0] = alloc_page(GFP_HIGHUSER); |
1441 | if (!area->pages[0]) | |
c8a82538 | 1442 | goto free_bitmap; |
f58bea2f | 1443 | area->pages[1] = NULL; |
d4b3b638 | 1444 | |
af0d95af | 1445 | area->vaddr = vaddr; |
6441ec8b ON |
1446 | init_waitqueue_head(&area->wq); |
1447 | /* Reserve the 1st slot for get_trampoline_vaddr() */ | |
e78aebfd | 1448 | set_bit(0, area->bitmap); |
e78aebfd | 1449 | atomic_set(&area->slot_count, 1); |
297e765e | 1450 | arch_uprobe_copy_ixol(area->pages[0], 0, &insn, UPROBE_SWBP_INSN_SIZE); |
e78aebfd | 1451 | |
6441ec8b | 1452 | if (!xol_add_vma(mm, area)) |
d4b3b638 SD |
1453 | return area; |
1454 | ||
f58bea2f | 1455 | __free_page(area->pages[0]); |
c8a82538 | 1456 | free_bitmap: |
d4b3b638 | 1457 | kfree(area->bitmap); |
c8a82538 | 1458 | free_area: |
d4b3b638 | 1459 | kfree(area); |
c8a82538 | 1460 | out: |
6441ec8b ON |
1461 | return NULL; |
1462 | } | |
1463 | ||
1464 | /* | |
1465 | * get_xol_area - Allocate process's xol_area if necessary. | |
1466 | * This area will be used for storing instructions for execution out of line. | |
1467 | * | |
1468 | * Returns the allocated area or NULL. | |
1469 | */ | |
1470 | static struct xol_area *get_xol_area(void) | |
1471 | { | |
1472 | struct mm_struct *mm = current->mm; | |
1473 | struct xol_area *area; | |
1474 | ||
1475 | if (!mm->uprobes_state.xol_area) | |
af0d95af | 1476 | __create_xol_area(0); |
6441ec8b | 1477 | |
5c6338b4 PM |
1478 | /* Pairs with xol_add_vma() smp_store_release() */ |
1479 | area = READ_ONCE(mm->uprobes_state.xol_area); /* ^^^ */ | |
9b545df8 | 1480 | return area; |
d4b3b638 SD |
1481 | } |
1482 | ||
1483 | /* | |
1484 | * uprobe_clear_state - Free the area allocated for slots. | |
1485 | */ | |
1486 | void uprobe_clear_state(struct mm_struct *mm) | |
1487 | { | |
1488 | struct xol_area *area = mm->uprobes_state.xol_area; | |
1489 | ||
1cc33161 RB |
1490 | mutex_lock(&delayed_uprobe_lock); |
1491 | delayed_uprobe_remove(NULL, mm); | |
1492 | mutex_unlock(&delayed_uprobe_lock); | |
1493 | ||
d4b3b638 SD |
1494 | if (!area) |
1495 | return; | |
1496 | ||
f58bea2f | 1497 | put_page(area->pages[0]); |
d4b3b638 SD |
1498 | kfree(area->bitmap); |
1499 | kfree(area); | |
1500 | } | |
1501 | ||
32cdba1e ON |
1502 | void uprobe_start_dup_mmap(void) |
1503 | { | |
1504 | percpu_down_read(&dup_mmap_sem); | |
1505 | } | |
1506 | ||
1507 | void uprobe_end_dup_mmap(void) | |
1508 | { | |
1509 | percpu_up_read(&dup_mmap_sem); | |
1510 | } | |
1511 | ||
f8ac4ec9 ON |
1512 | void uprobe_dup_mmap(struct mm_struct *oldmm, struct mm_struct *newmm) |
1513 | { | |
9f68f672 | 1514 | if (test_bit(MMF_HAS_UPROBES, &oldmm->flags)) { |
f8ac4ec9 | 1515 | set_bit(MMF_HAS_UPROBES, &newmm->flags); |
9f68f672 ON |
1516 | /* unconditionally, dup_mmap() skips VM_DONTCOPY vmas */ |
1517 | set_bit(MMF_RECALC_UPROBES, &newmm->flags); | |
1518 | } | |
f8ac4ec9 ON |
1519 | } |
1520 | ||
d4b3b638 SD |
1521 | /* |
1522 | * - search for a free slot. | |
1523 | */ | |
1524 | static unsigned long xol_take_insn_slot(struct xol_area *area) | |
1525 | { | |
1526 | unsigned long slot_addr; | |
1527 | int slot_nr; | |
1528 | ||
1529 | do { | |
1530 | slot_nr = find_first_zero_bit(area->bitmap, UINSNS_PER_PAGE); | |
1531 | if (slot_nr < UINSNS_PER_PAGE) { | |
1532 | if (!test_and_set_bit(slot_nr, area->bitmap)) | |
1533 | break; | |
1534 | ||
1535 | slot_nr = UINSNS_PER_PAGE; | |
1536 | continue; | |
1537 | } | |
1538 | wait_event(area->wq, (atomic_read(&area->slot_count) < UINSNS_PER_PAGE)); | |
1539 | } while (slot_nr >= UINSNS_PER_PAGE); | |
1540 | ||
1541 | slot_addr = area->vaddr + (slot_nr * UPROBE_XOL_SLOT_BYTES); | |
1542 | atomic_inc(&area->slot_count); | |
1543 | ||
1544 | return slot_addr; | |
1545 | } | |
1546 | ||
1547 | /* | |
a6cb3f6d | 1548 | * xol_get_insn_slot - allocate a slot for xol. |
d4b3b638 SD |
1549 | * Returns the allocated slot address or 0. |
1550 | */ | |
a6cb3f6d | 1551 | static unsigned long xol_get_insn_slot(struct uprobe *uprobe) |
d4b3b638 SD |
1552 | { |
1553 | struct xol_area *area; | |
a6cb3f6d | 1554 | unsigned long xol_vaddr; |
d4b3b638 | 1555 | |
9b545df8 ON |
1556 | area = get_xol_area(); |
1557 | if (!area) | |
1558 | return 0; | |
d4b3b638 | 1559 | |
a6cb3f6d ON |
1560 | xol_vaddr = xol_take_insn_slot(area); |
1561 | if (unlikely(!xol_vaddr)) | |
d4b3b638 SD |
1562 | return 0; |
1563 | ||
f58bea2f | 1564 | arch_uprobe_copy_ixol(area->pages[0], xol_vaddr, |
72e6ae28 | 1565 | &uprobe->arch.ixol, sizeof(uprobe->arch.ixol)); |
d4b3b638 | 1566 | |
a6cb3f6d | 1567 | return xol_vaddr; |
d4b3b638 SD |
1568 | } |
1569 | ||
1570 | /* | |
1571 | * xol_free_insn_slot - If slot was earlier allocated by | |
1572 | * @xol_get_insn_slot(), make the slot available for | |
1573 | * subsequent requests. | |
1574 | */ | |
1575 | static void xol_free_insn_slot(struct task_struct *tsk) | |
1576 | { | |
1577 | struct xol_area *area; | |
1578 | unsigned long vma_end; | |
1579 | unsigned long slot_addr; | |
1580 | ||
1581 | if (!tsk->mm || !tsk->mm->uprobes_state.xol_area || !tsk->utask) | |
1582 | return; | |
1583 | ||
1584 | slot_addr = tsk->utask->xol_vaddr; | |
af4355e9 | 1585 | if (unlikely(!slot_addr)) |
d4b3b638 SD |
1586 | return; |
1587 | ||
1588 | area = tsk->mm->uprobes_state.xol_area; | |
1589 | vma_end = area->vaddr + PAGE_SIZE; | |
1590 | if (area->vaddr <= slot_addr && slot_addr < vma_end) { | |
1591 | unsigned long offset; | |
1592 | int slot_nr; | |
1593 | ||
1594 | offset = slot_addr - area->vaddr; | |
1595 | slot_nr = offset / UPROBE_XOL_SLOT_BYTES; | |
1596 | if (slot_nr >= UINSNS_PER_PAGE) | |
1597 | return; | |
1598 | ||
1599 | clear_bit(slot_nr, area->bitmap); | |
1600 | atomic_dec(&area->slot_count); | |
2a742ced | 1601 | smp_mb__after_atomic(); /* pairs with prepare_to_wait() */ |
d4b3b638 SD |
1602 | if (waitqueue_active(&area->wq)) |
1603 | wake_up(&area->wq); | |
1604 | ||
1605 | tsk->utask->xol_vaddr = 0; | |
1606 | } | |
1607 | } | |
1608 | ||
72e6ae28 VK |
1609 | void __weak arch_uprobe_copy_ixol(struct page *page, unsigned long vaddr, |
1610 | void *src, unsigned long len) | |
1611 | { | |
1612 | /* Initialize the slot */ | |
1613 | copy_to_page(page, vaddr, src, len); | |
1614 | ||
1615 | /* | |
1616 | * We probably need flush_icache_user_range() but it needs vma. | |
1617 | * This should work on most of architectures by default. If | |
1618 | * architecture needs to do something different it can define | |
1619 | * its own version of the function. | |
1620 | */ | |
1621 | flush_dcache_page(page); | |
1622 | } | |
1623 | ||
0326f5a9 SD |
1624 | /** |
1625 | * uprobe_get_swbp_addr - compute address of swbp given post-swbp regs | |
1626 | * @regs: Reflects the saved state of the task after it has hit a breakpoint | |
1627 | * instruction. | |
1628 | * Return the address of the breakpoint instruction. | |
1629 | */ | |
1630 | unsigned long __weak uprobe_get_swbp_addr(struct pt_regs *regs) | |
1631 | { | |
1632 | return instruction_pointer(regs) - UPROBE_SWBP_INSN_SIZE; | |
1633 | } | |
1634 | ||
b02ef20a ON |
1635 | unsigned long uprobe_get_trap_addr(struct pt_regs *regs) |
1636 | { | |
1637 | struct uprobe_task *utask = current->utask; | |
1638 | ||
1639 | if (unlikely(utask && utask->active_uprobe)) | |
1640 | return utask->vaddr; | |
1641 | ||
1642 | return instruction_pointer(regs); | |
1643 | } | |
1644 | ||
2bb5e840 ON |
1645 | static struct return_instance *free_ret_instance(struct return_instance *ri) |
1646 | { | |
1647 | struct return_instance *next = ri->next; | |
1648 | put_uprobe(ri->uprobe); | |
1649 | kfree(ri); | |
1650 | return next; | |
1651 | } | |
1652 | ||
0326f5a9 SD |
1653 | /* |
1654 | * Called with no locks held. | |
788faab7 | 1655 | * Called in context of an exiting or an exec-ing thread. |
0326f5a9 SD |
1656 | */ |
1657 | void uprobe_free_utask(struct task_struct *t) | |
1658 | { | |
1659 | struct uprobe_task *utask = t->utask; | |
2bb5e840 | 1660 | struct return_instance *ri; |
0326f5a9 | 1661 | |
0326f5a9 SD |
1662 | if (!utask) |
1663 | return; | |
1664 | ||
1665 | if (utask->active_uprobe) | |
1666 | put_uprobe(utask->active_uprobe); | |
1667 | ||
0dfd0eb8 | 1668 | ri = utask->return_instances; |
2bb5e840 ON |
1669 | while (ri) |
1670 | ri = free_ret_instance(ri); | |
0dfd0eb8 | 1671 | |
d4b3b638 | 1672 | xol_free_insn_slot(t); |
0326f5a9 SD |
1673 | kfree(utask); |
1674 | t->utask = NULL; | |
1675 | } | |
1676 | ||
0326f5a9 | 1677 | /* |
5a2df662 ON |
1678 | * Allocate a uprobe_task object for the task if if necessary. |
1679 | * Called when the thread hits a breakpoint. | |
0326f5a9 SD |
1680 | * |
1681 | * Returns: | |
1682 | * - pointer to new uprobe_task on success | |
1683 | * - NULL otherwise | |
1684 | */ | |
5a2df662 | 1685 | static struct uprobe_task *get_utask(void) |
0326f5a9 | 1686 | { |
5a2df662 ON |
1687 | if (!current->utask) |
1688 | current->utask = kzalloc(sizeof(struct uprobe_task), GFP_KERNEL); | |
1689 | return current->utask; | |
0326f5a9 SD |
1690 | } |
1691 | ||
248d3a7b ON |
1692 | static int dup_utask(struct task_struct *t, struct uprobe_task *o_utask) |
1693 | { | |
1694 | struct uprobe_task *n_utask; | |
1695 | struct return_instance **p, *o, *n; | |
1696 | ||
1697 | n_utask = kzalloc(sizeof(struct uprobe_task), GFP_KERNEL); | |
1698 | if (!n_utask) | |
1699 | return -ENOMEM; | |
1700 | t->utask = n_utask; | |
1701 | ||
1702 | p = &n_utask->return_instances; | |
1703 | for (o = o_utask->return_instances; o; o = o->next) { | |
1704 | n = kmalloc(sizeof(struct return_instance), GFP_KERNEL); | |
1705 | if (!n) | |
1706 | return -ENOMEM; | |
1707 | ||
1708 | *n = *o; | |
f231722a | 1709 | get_uprobe(n->uprobe); |
248d3a7b ON |
1710 | n->next = NULL; |
1711 | ||
1712 | *p = n; | |
1713 | p = &n->next; | |
1714 | n_utask->depth++; | |
1715 | } | |
1716 | ||
1717 | return 0; | |
1718 | } | |
1719 | ||
1720 | static void uprobe_warn(struct task_struct *t, const char *msg) | |
1721 | { | |
1722 | pr_warn("uprobe: %s:%d failed to %s\n", | |
1723 | current->comm, current->pid, msg); | |
1724 | } | |
1725 | ||
aa59c53f ON |
1726 | static void dup_xol_work(struct callback_head *work) |
1727 | { | |
aa59c53f ON |
1728 | if (current->flags & PF_EXITING) |
1729 | return; | |
1730 | ||
598fdc1d MH |
1731 | if (!__create_xol_area(current->utask->dup_xol_addr) && |
1732 | !fatal_signal_pending(current)) | |
aa59c53f ON |
1733 | uprobe_warn(current, "dup xol area"); |
1734 | } | |
1735 | ||
b68e0749 ON |
1736 | /* |
1737 | * Called in context of a new clone/fork from copy_process. | |
1738 | */ | |
3ab67966 | 1739 | void uprobe_copy_process(struct task_struct *t, unsigned long flags) |
b68e0749 | 1740 | { |
248d3a7b ON |
1741 | struct uprobe_task *utask = current->utask; |
1742 | struct mm_struct *mm = current->mm; | |
aa59c53f | 1743 | struct xol_area *area; |
248d3a7b | 1744 | |
b68e0749 | 1745 | t->utask = NULL; |
248d3a7b | 1746 | |
3ab67966 ON |
1747 | if (!utask || !utask->return_instances) |
1748 | return; | |
1749 | ||
1750 | if (mm == t->mm && !(flags & CLONE_VFORK)) | |
248d3a7b ON |
1751 | return; |
1752 | ||
1753 | if (dup_utask(t, utask)) | |
1754 | return uprobe_warn(t, "dup ret instances"); | |
aa59c53f ON |
1755 | |
1756 | /* The task can fork() after dup_xol_work() fails */ | |
1757 | area = mm->uprobes_state.xol_area; | |
1758 | if (!area) | |
1759 | return uprobe_warn(t, "dup xol area"); | |
1760 | ||
3ab67966 ON |
1761 | if (mm == t->mm) |
1762 | return; | |
1763 | ||
32473431 ON |
1764 | t->utask->dup_xol_addr = area->vaddr; |
1765 | init_task_work(&t->utask->dup_xol_work, dup_xol_work); | |
1766 | task_work_add(t, &t->utask->dup_xol_work, true); | |
b68e0749 ON |
1767 | } |
1768 | ||
e78aebfd AA |
1769 | /* |
1770 | * Current area->vaddr notion assume the trampoline address is always | |
1771 | * equal area->vaddr. | |
1772 | * | |
1773 | * Returns -1 in case the xol_area is not allocated. | |
1774 | */ | |
1775 | static unsigned long get_trampoline_vaddr(void) | |
1776 | { | |
1777 | struct xol_area *area; | |
1778 | unsigned long trampoline_vaddr = -1; | |
1779 | ||
5c6338b4 PM |
1780 | /* Pairs with xol_add_vma() smp_store_release() */ |
1781 | area = READ_ONCE(current->mm->uprobes_state.xol_area); /* ^^^ */ | |
e78aebfd AA |
1782 | if (area) |
1783 | trampoline_vaddr = area->vaddr; | |
1784 | ||
1785 | return trampoline_vaddr; | |
1786 | } | |
1787 | ||
db087ef6 ON |
1788 | static void cleanup_return_instances(struct uprobe_task *utask, bool chained, |
1789 | struct pt_regs *regs) | |
a5b7e1a8 ON |
1790 | { |
1791 | struct return_instance *ri = utask->return_instances; | |
db087ef6 | 1792 | enum rp_check ctx = chained ? RP_CHECK_CHAIN_CALL : RP_CHECK_CALL; |
86dcb702 ON |
1793 | |
1794 | while (ri && !arch_uretprobe_is_alive(ri, ctx, regs)) { | |
a5b7e1a8 ON |
1795 | ri = free_ret_instance(ri); |
1796 | utask->depth--; | |
1797 | } | |
1798 | utask->return_instances = ri; | |
1799 | } | |
1800 | ||
0dfd0eb8 AA |
1801 | static void prepare_uretprobe(struct uprobe *uprobe, struct pt_regs *regs) |
1802 | { | |
1803 | struct return_instance *ri; | |
1804 | struct uprobe_task *utask; | |
1805 | unsigned long orig_ret_vaddr, trampoline_vaddr; | |
db087ef6 | 1806 | bool chained; |
0dfd0eb8 AA |
1807 | |
1808 | if (!get_xol_area()) | |
1809 | return; | |
1810 | ||
1811 | utask = get_utask(); | |
1812 | if (!utask) | |
1813 | return; | |
1814 | ||
ded49c55 AA |
1815 | if (utask->depth >= MAX_URETPROBE_DEPTH) { |
1816 | printk_ratelimited(KERN_INFO "uprobe: omit uretprobe due to" | |
1817 | " nestedness limit pid/tgid=%d/%d\n", | |
1818 | current->pid, current->tgid); | |
1819 | return; | |
1820 | } | |
1821 | ||
6c58d0e4 | 1822 | ri = kmalloc(sizeof(struct return_instance), GFP_KERNEL); |
0dfd0eb8 | 1823 | if (!ri) |
6c58d0e4 | 1824 | return; |
0dfd0eb8 AA |
1825 | |
1826 | trampoline_vaddr = get_trampoline_vaddr(); | |
1827 | orig_ret_vaddr = arch_uretprobe_hijack_return_addr(trampoline_vaddr, regs); | |
1828 | if (orig_ret_vaddr == -1) | |
1829 | goto fail; | |
1830 | ||
a5b7e1a8 | 1831 | /* drop the entries invalidated by longjmp() */ |
db087ef6 ON |
1832 | chained = (orig_ret_vaddr == trampoline_vaddr); |
1833 | cleanup_return_instances(utask, chained, regs); | |
a5b7e1a8 | 1834 | |
0dfd0eb8 AA |
1835 | /* |
1836 | * We don't want to keep trampoline address in stack, rather keep the | |
1837 | * original return address of first caller thru all the consequent | |
1838 | * instances. This also makes breakpoint unwrapping easier. | |
1839 | */ | |
db087ef6 | 1840 | if (chained) { |
0dfd0eb8 AA |
1841 | if (!utask->return_instances) { |
1842 | /* | |
1843 | * This situation is not possible. Likely we have an | |
1844 | * attack from user-space. | |
1845 | */ | |
6c58d0e4 | 1846 | uprobe_warn(current, "handle tail call"); |
0dfd0eb8 AA |
1847 | goto fail; |
1848 | } | |
0dfd0eb8 AA |
1849 | orig_ret_vaddr = utask->return_instances->orig_ret_vaddr; |
1850 | } | |
1851 | ||
f231722a | 1852 | ri->uprobe = get_uprobe(uprobe); |
0dfd0eb8 | 1853 | ri->func = instruction_pointer(regs); |
7b868e48 | 1854 | ri->stack = user_stack_pointer(regs); |
0dfd0eb8 AA |
1855 | ri->orig_ret_vaddr = orig_ret_vaddr; |
1856 | ri->chained = chained; | |
1857 | ||
ded49c55 | 1858 | utask->depth++; |
0dfd0eb8 AA |
1859 | ri->next = utask->return_instances; |
1860 | utask->return_instances = ri; | |
1861 | ||
1862 | return; | |
0dfd0eb8 AA |
1863 | fail: |
1864 | kfree(ri); | |
1865 | } | |
1866 | ||
0326f5a9 SD |
1867 | /* Prepare to single-step probed instruction out of line. */ |
1868 | static int | |
a6cb3f6d | 1869 | pre_ssout(struct uprobe *uprobe, struct pt_regs *regs, unsigned long bp_vaddr) |
0326f5a9 | 1870 | { |
a6cb3f6d ON |
1871 | struct uprobe_task *utask; |
1872 | unsigned long xol_vaddr; | |
aba51024 | 1873 | int err; |
a6cb3f6d | 1874 | |
608e7427 ON |
1875 | utask = get_utask(); |
1876 | if (!utask) | |
1877 | return -ENOMEM; | |
a6cb3f6d ON |
1878 | |
1879 | xol_vaddr = xol_get_insn_slot(uprobe); | |
1880 | if (!xol_vaddr) | |
1881 | return -ENOMEM; | |
1882 | ||
1883 | utask->xol_vaddr = xol_vaddr; | |
1884 | utask->vaddr = bp_vaddr; | |
d4b3b638 | 1885 | |
aba51024 ON |
1886 | err = arch_uprobe_pre_xol(&uprobe->arch, regs); |
1887 | if (unlikely(err)) { | |
1888 | xol_free_insn_slot(current); | |
1889 | return err; | |
1890 | } | |
1891 | ||
608e7427 ON |
1892 | utask->active_uprobe = uprobe; |
1893 | utask->state = UTASK_SSTEP; | |
aba51024 | 1894 | return 0; |
0326f5a9 SD |
1895 | } |
1896 | ||
1897 | /* | |
1898 | * If we are singlestepping, then ensure this thread is not connected to | |
1899 | * non-fatal signals until completion of singlestep. When xol insn itself | |
1900 | * triggers the signal, restart the original insn even if the task is | |
1901 | * already SIGKILL'ed (since coredump should report the correct ip). This | |
1902 | * is even more important if the task has a handler for SIGSEGV/etc, The | |
1903 | * _same_ instruction should be repeated again after return from the signal | |
1904 | * handler, and SSTEP can never finish in this case. | |
1905 | */ | |
1906 | bool uprobe_deny_signal(void) | |
1907 | { | |
1908 | struct task_struct *t = current; | |
1909 | struct uprobe_task *utask = t->utask; | |
1910 | ||
1911 | if (likely(!utask || !utask->active_uprobe)) | |
1912 | return false; | |
1913 | ||
1914 | WARN_ON_ONCE(utask->state != UTASK_SSTEP); | |
1915 | ||
1916 | if (signal_pending(t)) { | |
1917 | spin_lock_irq(&t->sighand->siglock); | |
1918 | clear_tsk_thread_flag(t, TIF_SIGPENDING); | |
1919 | spin_unlock_irq(&t->sighand->siglock); | |
1920 | ||
1921 | if (__fatal_signal_pending(t) || arch_uprobe_xol_was_trapped(t)) { | |
1922 | utask->state = UTASK_SSTEP_TRAPPED; | |
1923 | set_tsk_thread_flag(t, TIF_UPROBE); | |
0326f5a9 SD |
1924 | } |
1925 | } | |
1926 | ||
1927 | return true; | |
1928 | } | |
1929 | ||
499a4f3e ON |
1930 | static void mmf_recalc_uprobes(struct mm_struct *mm) |
1931 | { | |
1932 | struct vm_area_struct *vma; | |
1933 | ||
1934 | for (vma = mm->mmap; vma; vma = vma->vm_next) { | |
1935 | if (!valid_vma(vma, false)) | |
1936 | continue; | |
1937 | /* | |
1938 | * This is not strictly accurate, we can race with | |
1939 | * uprobe_unregister() and see the already removed | |
1940 | * uprobe if delete_uprobe() was not yet called. | |
63633cbf | 1941 | * Or this uprobe can be filtered out. |
499a4f3e ON |
1942 | */ |
1943 | if (vma_has_uprobes(vma, vma->vm_start, vma->vm_end)) | |
1944 | return; | |
1945 | } | |
1946 | ||
1947 | clear_bit(MMF_HAS_UPROBES, &mm->flags); | |
1948 | } | |
1949 | ||
0908ad6e | 1950 | static int is_trap_at_addr(struct mm_struct *mm, unsigned long vaddr) |
ec75fba9 ON |
1951 | { |
1952 | struct page *page; | |
1953 | uprobe_opcode_t opcode; | |
1954 | int result; | |
1955 | ||
1956 | pagefault_disable(); | |
bd28b145 | 1957 | result = __get_user(opcode, (uprobe_opcode_t __user *)vaddr); |
ec75fba9 ON |
1958 | pagefault_enable(); |
1959 | ||
1960 | if (likely(result == 0)) | |
1961 | goto out; | |
1962 | ||
1e987790 DH |
1963 | /* |
1964 | * The NULL 'tsk' here ensures that any faults that occur here | |
1965 | * will not be accounted to the task. 'mm' *is* current->mm, | |
1966 | * but we treat this as a 'remote' access since it is | |
1967 | * essentially a kernel access to the memory. | |
1968 | */ | |
9beae1ea | 1969 | result = get_user_pages_remote(NULL, mm, vaddr, 1, FOLL_FORCE, &page, |
5b56d49f | 1970 | NULL, NULL); |
ec75fba9 ON |
1971 | if (result < 0) |
1972 | return result; | |
1973 | ||
ab0d805c | 1974 | copy_from_page(page, vaddr, &opcode, UPROBE_SWBP_INSN_SIZE); |
ec75fba9 ON |
1975 | put_page(page); |
1976 | out: | |
0908ad6e AM |
1977 | /* This needs to return true for any variant of the trap insn */ |
1978 | return is_trap_insn(&opcode); | |
ec75fba9 ON |
1979 | } |
1980 | ||
d790d346 | 1981 | static struct uprobe *find_active_uprobe(unsigned long bp_vaddr, int *is_swbp) |
0326f5a9 | 1982 | { |
3a9ea052 ON |
1983 | struct mm_struct *mm = current->mm; |
1984 | struct uprobe *uprobe = NULL; | |
0326f5a9 | 1985 | struct vm_area_struct *vma; |
0326f5a9 | 1986 | |
0326f5a9 SD |
1987 | down_read(&mm->mmap_sem); |
1988 | vma = find_vma(mm, bp_vaddr); | |
3a9ea052 ON |
1989 | if (vma && vma->vm_start <= bp_vaddr) { |
1990 | if (valid_vma(vma, false)) { | |
f281769e | 1991 | struct inode *inode = file_inode(vma->vm_file); |
cb113b47 | 1992 | loff_t offset = vaddr_to_offset(vma, bp_vaddr); |
0326f5a9 | 1993 | |
3a9ea052 ON |
1994 | uprobe = find_uprobe(inode, offset); |
1995 | } | |
d790d346 ON |
1996 | |
1997 | if (!uprobe) | |
0908ad6e | 1998 | *is_swbp = is_trap_at_addr(mm, bp_vaddr); |
d790d346 ON |
1999 | } else { |
2000 | *is_swbp = -EFAULT; | |
0326f5a9 | 2001 | } |
499a4f3e ON |
2002 | |
2003 | if (!uprobe && test_and_clear_bit(MMF_RECALC_UPROBES, &mm->flags)) | |
2004 | mmf_recalc_uprobes(mm); | |
0326f5a9 SD |
2005 | up_read(&mm->mmap_sem); |
2006 | ||
3a9ea052 ON |
2007 | return uprobe; |
2008 | } | |
2009 | ||
da1816b1 ON |
2010 | static void handler_chain(struct uprobe *uprobe, struct pt_regs *regs) |
2011 | { | |
2012 | struct uprobe_consumer *uc; | |
2013 | int remove = UPROBE_HANDLER_REMOVE; | |
0dfd0eb8 | 2014 | bool need_prep = false; /* prepare return uprobe, when needed */ |
da1816b1 ON |
2015 | |
2016 | down_read(&uprobe->register_rwsem); | |
2017 | for (uc = uprobe->consumers; uc; uc = uc->next) { | |
ea024870 | 2018 | int rc = 0; |
da1816b1 | 2019 | |
ea024870 AA |
2020 | if (uc->handler) { |
2021 | rc = uc->handler(uc, regs); | |
2022 | WARN(rc & ~UPROBE_HANDLER_MASK, | |
2023 | "bad rc=0x%x from %pf()\n", rc, uc->handler); | |
2024 | } | |
0dfd0eb8 AA |
2025 | |
2026 | if (uc->ret_handler) | |
2027 | need_prep = true; | |
2028 | ||
da1816b1 ON |
2029 | remove &= rc; |
2030 | } | |
2031 | ||
0dfd0eb8 AA |
2032 | if (need_prep && !remove) |
2033 | prepare_uretprobe(uprobe, regs); /* put bp at return */ | |
2034 | ||
da1816b1 ON |
2035 | if (remove && uprobe->consumers) { |
2036 | WARN_ON(!uprobe_is_active(uprobe)); | |
2037 | unapply_uprobe(uprobe, current->mm); | |
2038 | } | |
2039 | up_read(&uprobe->register_rwsem); | |
2040 | } | |
2041 | ||
fec8898d AA |
2042 | static void |
2043 | handle_uretprobe_chain(struct return_instance *ri, struct pt_regs *regs) | |
2044 | { | |
2045 | struct uprobe *uprobe = ri->uprobe; | |
2046 | struct uprobe_consumer *uc; | |
2047 | ||
2048 | down_read(&uprobe->register_rwsem); | |
2049 | for (uc = uprobe->consumers; uc; uc = uc->next) { | |
2050 | if (uc->ret_handler) | |
2051 | uc->ret_handler(uc, ri->func, regs); | |
2052 | } | |
2053 | up_read(&uprobe->register_rwsem); | |
2054 | } | |
2055 | ||
a83cfeb9 ON |
2056 | static struct return_instance *find_next_ret_chain(struct return_instance *ri) |
2057 | { | |
2058 | bool chained; | |
2059 | ||
2060 | do { | |
2061 | chained = ri->chained; | |
2062 | ri = ri->next; /* can't be NULL if chained */ | |
2063 | } while (chained); | |
2064 | ||
2065 | return ri; | |
2066 | } | |
2067 | ||
0b5256c7 | 2068 | static void handle_trampoline(struct pt_regs *regs) |
fec8898d AA |
2069 | { |
2070 | struct uprobe_task *utask; | |
a83cfeb9 | 2071 | struct return_instance *ri, *next; |
5eeb50de | 2072 | bool valid; |
fec8898d AA |
2073 | |
2074 | utask = current->utask; | |
2075 | if (!utask) | |
0b5256c7 | 2076 | goto sigill; |
fec8898d AA |
2077 | |
2078 | ri = utask->return_instances; | |
2079 | if (!ri) | |
0b5256c7 | 2080 | goto sigill; |
fec8898d | 2081 | |
a83cfeb9 | 2082 | do { |
5eeb50de ON |
2083 | /* |
2084 | * We should throw out the frames invalidated by longjmp(). | |
2085 | * If this chain is valid, then the next one should be alive | |
2086 | * or NULL; the latter case means that nobody but ri->func | |
2087 | * could hit this trampoline on return. TODO: sigaltstack(). | |
2088 | */ | |
2089 | next = find_next_ret_chain(ri); | |
86dcb702 | 2090 | valid = !next || arch_uretprobe_is_alive(next, RP_CHECK_RET, regs); |
5eeb50de ON |
2091 | |
2092 | instruction_pointer_set(regs, ri->orig_ret_vaddr); | |
2093 | do { | |
2094 | if (valid) | |
2095 | handle_uretprobe_chain(ri, regs); | |
2096 | ri = free_ret_instance(ri); | |
2097 | utask->depth--; | |
2098 | } while (ri != next); | |
2099 | } while (!valid); | |
fec8898d AA |
2100 | |
2101 | utask->return_instances = ri; | |
0b5256c7 ON |
2102 | return; |
2103 | ||
2104 | sigill: | |
2105 | uprobe_warn(current, "handle uretprobe, sending SIGILL."); | |
2106 | force_sig_info(SIGILL, SEND_SIG_FORCED, current); | |
fec8898d | 2107 | |
fec8898d AA |
2108 | } |
2109 | ||
6fe50a28 DL |
2110 | bool __weak arch_uprobe_ignore(struct arch_uprobe *aup, struct pt_regs *regs) |
2111 | { | |
2112 | return false; | |
2113 | } | |
2114 | ||
86dcb702 ON |
2115 | bool __weak arch_uretprobe_is_alive(struct return_instance *ret, enum rp_check ctx, |
2116 | struct pt_regs *regs) | |
97da8976 ON |
2117 | { |
2118 | return true; | |
2119 | } | |
2120 | ||
3a9ea052 ON |
2121 | /* |
2122 | * Run handler and ask thread to singlestep. | |
2123 | * Ensure all non-fatal signals cannot interrupt thread while it singlesteps. | |
2124 | */ | |
2125 | static void handle_swbp(struct pt_regs *regs) | |
2126 | { | |
3a9ea052 ON |
2127 | struct uprobe *uprobe; |
2128 | unsigned long bp_vaddr; | |
56bb4cf6 | 2129 | int uninitialized_var(is_swbp); |
3a9ea052 ON |
2130 | |
2131 | bp_vaddr = uprobe_get_swbp_addr(regs); | |
0b5256c7 ON |
2132 | if (bp_vaddr == get_trampoline_vaddr()) |
2133 | return handle_trampoline(regs); | |
fec8898d AA |
2134 | |
2135 | uprobe = find_active_uprobe(bp_vaddr, &is_swbp); | |
0326f5a9 | 2136 | if (!uprobe) { |
56bb4cf6 ON |
2137 | if (is_swbp > 0) { |
2138 | /* No matching uprobe; signal SIGTRAP. */ | |
2139 | send_sig(SIGTRAP, current, 0); | |
2140 | } else { | |
2141 | /* | |
2142 | * Either we raced with uprobe_unregister() or we can't | |
2143 | * access this memory. The latter is only possible if | |
2144 | * another thread plays with our ->mm. In both cases | |
2145 | * we can simply restart. If this vma was unmapped we | |
2146 | * can pretend this insn was not executed yet and get | |
2147 | * the (correct) SIGSEGV after restart. | |
2148 | */ | |
2149 | instruction_pointer_set(regs, bp_vaddr); | |
2150 | } | |
0326f5a9 SD |
2151 | return; |
2152 | } | |
74e59dfc ON |
2153 | |
2154 | /* change it in advance for ->handler() and restart */ | |
2155 | instruction_pointer_set(regs, bp_vaddr); | |
2156 | ||
142b18dd ON |
2157 | /* |
2158 | * TODO: move copy_insn/etc into _register and remove this hack. | |
2159 | * After we hit the bp, _unregister + _register can install the | |
2160 | * new and not-yet-analyzed uprobe at the same address, restart. | |
2161 | */ | |
2162 | smp_rmb(); /* pairs with wmb() in install_breakpoint() */ | |
71434f2f | 2163 | if (unlikely(!test_bit(UPROBE_COPY_INSN, &uprobe->flags))) |
74e59dfc | 2164 | goto out; |
0326f5a9 | 2165 | |
72fd293a ON |
2166 | /* Tracing handlers use ->utask to communicate with fetch methods */ |
2167 | if (!get_utask()) | |
2168 | goto out; | |
2169 | ||
6fe50a28 DL |
2170 | if (arch_uprobe_ignore(&uprobe->arch, regs)) |
2171 | goto out; | |
2172 | ||
0326f5a9 | 2173 | handler_chain(uprobe, regs); |
6fe50a28 | 2174 | |
8a6b1732 | 2175 | if (arch_uprobe_skip_sstep(&uprobe->arch, regs)) |
0578a970 | 2176 | goto out; |
0326f5a9 | 2177 | |
608e7427 | 2178 | if (!pre_ssout(uprobe, regs, bp_vaddr)) |
0326f5a9 | 2179 | return; |
0326f5a9 | 2180 | |
8a6b1732 | 2181 | /* arch_uprobe_skip_sstep() succeeded, or restart if can't singlestep */ |
0578a970 | 2182 | out: |
8bd87445 | 2183 | put_uprobe(uprobe); |
0326f5a9 SD |
2184 | } |
2185 | ||
2186 | /* | |
2187 | * Perform required fix-ups and disable singlestep. | |
2188 | * Allow pending signals to take effect. | |
2189 | */ | |
2190 | static void handle_singlestep(struct uprobe_task *utask, struct pt_regs *regs) | |
2191 | { | |
2192 | struct uprobe *uprobe; | |
014940ba | 2193 | int err = 0; |
0326f5a9 SD |
2194 | |
2195 | uprobe = utask->active_uprobe; | |
2196 | if (utask->state == UTASK_SSTEP_ACK) | |
014940ba | 2197 | err = arch_uprobe_post_xol(&uprobe->arch, regs); |
0326f5a9 SD |
2198 | else if (utask->state == UTASK_SSTEP_TRAPPED) |
2199 | arch_uprobe_abort_xol(&uprobe->arch, regs); | |
2200 | else | |
2201 | WARN_ON_ONCE(1); | |
2202 | ||
2203 | put_uprobe(uprobe); | |
2204 | utask->active_uprobe = NULL; | |
2205 | utask->state = UTASK_RUNNING; | |
d4b3b638 | 2206 | xol_free_insn_slot(current); |
0326f5a9 SD |
2207 | |
2208 | spin_lock_irq(¤t->sighand->siglock); | |
2209 | recalc_sigpending(); /* see uprobe_deny_signal() */ | |
2210 | spin_unlock_irq(¤t->sighand->siglock); | |
014940ba ON |
2211 | |
2212 | if (unlikely(err)) { | |
2213 | uprobe_warn(current, "execute the probed insn, sending SIGILL."); | |
2214 | force_sig_info(SIGILL, SEND_SIG_FORCED, current); | |
2215 | } | |
0326f5a9 SD |
2216 | } |
2217 | ||
2218 | /* | |
1b08e907 ON |
2219 | * On breakpoint hit, breakpoint notifier sets the TIF_UPROBE flag and |
2220 | * allows the thread to return from interrupt. After that handle_swbp() | |
2221 | * sets utask->active_uprobe. | |
0326f5a9 | 2222 | * |
1b08e907 ON |
2223 | * On singlestep exception, singlestep notifier sets the TIF_UPROBE flag |
2224 | * and allows the thread to return from interrupt. | |
0326f5a9 SD |
2225 | * |
2226 | * While returning to userspace, thread notices the TIF_UPROBE flag and calls | |
2227 | * uprobe_notify_resume(). | |
2228 | */ | |
2229 | void uprobe_notify_resume(struct pt_regs *regs) | |
2230 | { | |
2231 | struct uprobe_task *utask; | |
2232 | ||
db023ea5 ON |
2233 | clear_thread_flag(TIF_UPROBE); |
2234 | ||
0326f5a9 | 2235 | utask = current->utask; |
1b08e907 | 2236 | if (utask && utask->active_uprobe) |
0326f5a9 | 2237 | handle_singlestep(utask, regs); |
1b08e907 ON |
2238 | else |
2239 | handle_swbp(regs); | |
0326f5a9 SD |
2240 | } |
2241 | ||
2242 | /* | |
2243 | * uprobe_pre_sstep_notifier gets called from interrupt context as part of | |
2244 | * notifier mechanism. Set TIF_UPROBE flag and indicate breakpoint hit. | |
2245 | */ | |
2246 | int uprobe_pre_sstep_notifier(struct pt_regs *regs) | |
2247 | { | |
0dfd0eb8 AA |
2248 | if (!current->mm) |
2249 | return 0; | |
2250 | ||
2251 | if (!test_bit(MMF_HAS_UPROBES, ¤t->mm->flags) && | |
2252 | (!current->utask || !current->utask->return_instances)) | |
0326f5a9 SD |
2253 | return 0; |
2254 | ||
0326f5a9 | 2255 | set_thread_flag(TIF_UPROBE); |
0326f5a9 SD |
2256 | return 1; |
2257 | } | |
2258 | ||
2259 | /* | |
2260 | * uprobe_post_sstep_notifier gets called in interrupt context as part of notifier | |
2261 | * mechanism. Set TIF_UPROBE flag and indicate completion of singlestep. | |
2262 | */ | |
2263 | int uprobe_post_sstep_notifier(struct pt_regs *regs) | |
2264 | { | |
2265 | struct uprobe_task *utask = current->utask; | |
2266 | ||
2267 | if (!current->mm || !utask || !utask->active_uprobe) | |
2268 | /* task is currently not uprobed */ | |
2269 | return 0; | |
2270 | ||
2271 | utask->state = UTASK_SSTEP_ACK; | |
2272 | set_thread_flag(TIF_UPROBE); | |
2273 | return 1; | |
2274 | } | |
2275 | ||
2276 | static struct notifier_block uprobe_exception_nb = { | |
2277 | .notifier_call = arch_uprobe_exception_notify, | |
2278 | .priority = INT_MAX-1, /* notified after kprobes, kgdb */ | |
2279 | }; | |
2280 | ||
2b144498 SD |
2281 | static int __init init_uprobes(void) |
2282 | { | |
2283 | int i; | |
2284 | ||
66d06dff | 2285 | for (i = 0; i < UPROBES_HASH_SZ; i++) |
2b144498 | 2286 | mutex_init(&uprobes_mmap_mutex[i]); |
0326f5a9 | 2287 | |
32cdba1e ON |
2288 | if (percpu_init_rwsem(&dup_mmap_sem)) |
2289 | return -ENOMEM; | |
2290 | ||
0326f5a9 | 2291 | return register_die_notifier(&uprobe_exception_nb); |
2b144498 | 2292 | } |
736e89d9 | 2293 | __initcall(init_uprobes); |