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