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