2 * Userspace Probes (UProbes)
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.
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.
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.
18 * Copyright (C) IBM Corporation, 2008-2011
24 #include <linux/kernel.h>
25 #include <linux/highmem.h>
26 #include <linux/pagemap.h> /* read_mapping_page */
27 #include <linux/slab.h>
28 #include <linux/sched.h>
29 #include <linux/rmap.h> /* anon_vma_prepare */
30 #include <linux/mmu_notifier.h> /* set_pte_at_notify */
31 #include <linux/swap.h> /* try_to_free_swap */
32 #include <linux/ptrace.h> /* user_enable_single_step */
33 #include <linux/kdebug.h> /* notifier mechanism */
34 #include <linux/uprobes.h>
36 #define UINSNS_PER_PAGE (PAGE_SIZE/UPROBES_XOL_SLOT_BYTES)
37 #define MAX_UPROBES_XOL_SLOTS UINSNS_PER_PAGE
39 static bulkref_t uprobes_srcu;
40 static struct rb_root uprobes_tree = RB_ROOT;
41 static DEFINE_SPINLOCK(uprobes_treelock); /* serialize rbtree access */
43 #define UPROBES_HASH_SZ 13
44 /* serialize (un)register */
45 static struct mutex uprobes_mutex[UPROBES_HASH_SZ];
46 #define uprobes_hash(v) (&uprobes_mutex[((unsigned long)(v)) %\
49 /* serialize uprobe->pending_list */
50 static struct mutex uprobes_mmap_mutex[UPROBES_HASH_SZ];
51 #define uprobes_mmap_hash(v) (&uprobes_mmap_mutex[((unsigned long)(v)) %\
55 * uprobe_events allows us to skip the mmap_uprobe if there are no uprobe
56 * events active at this time. Probably a fine grained per inode count is
59 static atomic_t uprobe_events = ATOMIC_INIT(0);
62 * Maintain a temporary per vma info that can be used to search if a vma
63 * has already been handled. This structure is introduced since extending
64 * vm_area_struct wasnt recommended.
67 struct list_head probe_list;
73 * valid_vma: Verify if the specified vma is an executable vma
74 * Relax restrictions while unregistering: vm_flags might have
75 * changed after breakpoint was inserted.
76 * - is_reg: indicates if we are in register context.
77 * - Return 1 if the specified virtual address is in an
80 static bool valid_vma(struct vm_area_struct *vma, bool is_reg)
88 if ((vma->vm_flags & (VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)) ==
96 * __replace_page - replace page in vma by new page.
97 * based on replace_page in mm/ksm.c
99 * @vma: vma that holds the pte pointing to page
100 * @page: the cowed page we are replacing by kpage
101 * @kpage: the modified page we replace page by
103 * Returns 0 on success, -EFAULT on failure.
105 static int __replace_page(struct vm_area_struct *vma, struct page *page,
108 struct mm_struct *mm = vma->vm_mm;
117 addr = page_address_in_vma(page, vma);
121 pgd = pgd_offset(mm, addr);
122 if (!pgd_present(*pgd))
125 pud = pud_offset(pgd, addr);
126 if (!pud_present(*pud))
129 pmd = pmd_offset(pud, addr);
130 if (!pmd_present(*pmd))
133 ptep = pte_offset_map_lock(mm, pmd, addr, &ptl);
138 page_add_new_anon_rmap(kpage, vma, addr);
140 flush_cache_page(vma, addr, pte_pfn(*ptep));
141 ptep_clear_flush(vma, addr, ptep);
142 set_pte_at_notify(mm, addr, ptep, mk_pte(kpage, vma->vm_page_prot));
144 page_remove_rmap(page);
145 if (!page_mapped(page))
146 try_to_free_swap(page);
148 pte_unmap_unlock(ptep, ptl);
157 * Expect the breakpoint instruction to be the smallest size instruction for
158 * the architecture. If an arch has variable length instruction and the
159 * breakpoint instruction is not of the smallest length instruction
160 * supported by that architecture then we need to modify read_opcode /
161 * write_opcode accordingly. This would never be a problem for archs that
162 * have fixed length instructions.
166 * write_opcode - write the opcode at a given virtual address.
167 * @mm: the probed process address space.
168 * @uprobe: the breakpointing information.
169 * @vaddr: the virtual address to store the opcode.
170 * @opcode: opcode to be written at @vaddr.
172 * Called with mm->mmap_sem held (for read and with a reference to
175 * For mm @mm, write the opcode at @vaddr.
176 * Return 0 (success) or a negative errno.
178 static int write_opcode(struct mm_struct *mm, struct uprobe *uprobe,
179 unsigned long vaddr, uprobe_opcode_t opcode)
181 struct page *old_page, *new_page;
182 struct address_space *mapping;
183 void *vaddr_old, *vaddr_new;
184 struct vm_area_struct *vma;
188 /* Read the page with vaddr into memory */
189 ret = get_user_pages(NULL, mm, vaddr, 1, 0, 0, &old_page, &vma);
195 * We are interested in text pages only. Our pages of interest
196 * should be mapped for read and execute only. We desist from
197 * adding probes in write mapped pages since the breakpoints
198 * might end up in the file copy.
200 if (!valid_vma(vma, opcode == UPROBES_BKPT_INSN))
203 mapping = uprobe->inode->i_mapping;
204 if (mapping != vma->vm_file->f_mapping)
207 addr = vma->vm_start + uprobe->offset;
208 addr -= vma->vm_pgoff << PAGE_SHIFT;
209 if (vaddr != (unsigned long)addr)
213 new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vaddr);
217 __SetPageUptodate(new_page);
220 * lock page will serialize against do_wp_page()'s
221 * PageAnon() handling
224 /* copy the page now that we've got it stable */
225 vaddr_old = kmap_atomic(old_page);
226 vaddr_new = kmap_atomic(new_page);
228 memcpy(vaddr_new, vaddr_old, PAGE_SIZE);
229 /* poke the new insn in, ASSUMES we don't cross page boundary */
231 memcpy(vaddr_new + vaddr, &opcode, uprobe_opcode_sz);
233 kunmap_atomic(vaddr_new);
234 kunmap_atomic(vaddr_old);
236 ret = anon_vma_prepare(vma);
241 ret = __replace_page(vma, old_page, new_page);
242 unlock_page(new_page);
245 unlock_page(old_page);
246 page_cache_release(new_page);
249 put_page(old_page); /* we did a get_page in the beginning */
254 * read_opcode - read the opcode at a given virtual address.
255 * @mm: the probed process address space.
256 * @vaddr: the virtual address to read the opcode.
257 * @opcode: location to store the read opcode.
259 * Called with mm->mmap_sem held (for read and with a reference to
262 * For mm @mm, read the opcode at @vaddr and store it in @opcode.
263 * Return 0 (success) or a negative errno.
265 static int read_opcode(struct mm_struct *mm, unsigned long vaddr,
266 uprobe_opcode_t *opcode)
272 ret = get_user_pages(NULL, mm, vaddr, 1, 0, 0, &page, NULL);
277 vaddr_new = kmap_atomic(page);
279 memcpy(opcode, vaddr_new + vaddr, uprobe_opcode_sz);
280 kunmap_atomic(vaddr_new);
282 put_page(page); /* we did a get_user_pages in the beginning */
287 * set_bkpt - store breakpoint at a given address.
288 * @mm: the probed process address space.
289 * @uprobe: the probepoint information.
290 * @vaddr: the virtual address to insert the opcode.
292 * For mm @mm, store the breakpoint instruction at @vaddr.
293 * Return 0 (success) or a negative errno.
295 int __weak set_bkpt(struct mm_struct *mm, struct uprobe *uprobe,
298 return write_opcode(mm, uprobe, vaddr, UPROBES_BKPT_INSN);
302 * set_orig_insn - Restore the original instruction.
303 * @mm: the probed process address space.
304 * @uprobe: the probepoint information.
305 * @vaddr: the virtual address to insert the opcode.
306 * @verify: if true, verify existance of breakpoint instruction.
308 * For mm @mm, restore the original opcode (opcode) at @vaddr.
309 * Return 0 (success) or a negative errno.
311 int __weak set_orig_insn(struct mm_struct *mm, struct uprobe *uprobe,
312 unsigned long vaddr, bool verify)
315 uprobe_opcode_t opcode;
316 int result = read_opcode(mm, vaddr, &opcode);
321 if (opcode != UPROBES_BKPT_INSN)
324 return write_opcode(mm, uprobe, vaddr,
325 *(uprobe_opcode_t *)uprobe->insn);
329 * is_bkpt_insn - check if instruction is breakpoint instruction.
330 * @insn: instruction to be checked.
331 * Default implementation of is_bkpt_insn
332 * Returns true if @insn is a breakpoint instruction.
334 bool __weak is_bkpt_insn(u8 *insn)
336 return (insn[0] == UPROBES_BKPT_INSN);
339 static int match_uprobe(struct uprobe *l, struct uprobe *r)
341 if (l->inode < r->inode)
343 if (l->inode > r->inode)
346 if (l->offset < r->offset)
349 if (l->offset > r->offset)
356 static struct uprobe *__find_uprobe(struct inode *inode, loff_t offset)
358 struct uprobe u = { .inode = inode, .offset = offset };
359 struct rb_node *n = uprobes_tree.rb_node;
360 struct uprobe *uprobe;
364 uprobe = rb_entry(n, struct uprobe, rb_node);
365 match = match_uprobe(&u, uprobe);
367 atomic_inc(&uprobe->ref);
379 * Find a uprobe corresponding to a given inode:offset
380 * Acquires uprobes_treelock
382 static struct uprobe *find_uprobe(struct inode *inode, loff_t offset)
384 struct uprobe *uprobe;
387 spin_lock_irqsave(&uprobes_treelock, flags);
388 uprobe = __find_uprobe(inode, offset);
389 spin_unlock_irqrestore(&uprobes_treelock, flags);
393 static struct uprobe *__insert_uprobe(struct uprobe *uprobe)
395 struct rb_node **p = &uprobes_tree.rb_node;
396 struct rb_node *parent = NULL;
402 u = rb_entry(parent, struct uprobe, rb_node);
403 match = match_uprobe(uprobe, u);
410 p = &parent->rb_left;
412 p = &parent->rb_right;
416 rb_link_node(&uprobe->rb_node, parent, p);
417 rb_insert_color(&uprobe->rb_node, &uprobes_tree);
418 /* get access + creation ref */
419 atomic_set(&uprobe->ref, 2);
424 * Acquires uprobes_treelock.
425 * Matching uprobe already exists in rbtree;
426 * increment (access refcount) and return the matching uprobe.
428 * No matching uprobe; insert the uprobe in rb_tree;
429 * get a double refcount (access + creation) and return NULL.
431 static struct uprobe *insert_uprobe(struct uprobe *uprobe)
436 spin_lock_irqsave(&uprobes_treelock, flags);
437 u = __insert_uprobe(uprobe);
438 spin_unlock_irqrestore(&uprobes_treelock, flags);
440 /* For now assume that the instruction need not be single-stepped */
441 uprobe->flags |= UPROBES_SKIP_SSTEP;
445 static void put_uprobe(struct uprobe *uprobe)
447 if (atomic_dec_and_test(&uprobe->ref))
451 static struct uprobe *alloc_uprobe(struct inode *inode, loff_t offset)
453 struct uprobe *uprobe, *cur_uprobe;
455 uprobe = kzalloc(sizeof(struct uprobe), GFP_KERNEL);
459 uprobe->inode = igrab(inode);
460 uprobe->offset = offset;
461 init_rwsem(&uprobe->consumer_rwsem);
462 INIT_LIST_HEAD(&uprobe->pending_list);
464 /* add to uprobes_tree, sorted on inode:offset */
465 cur_uprobe = insert_uprobe(uprobe);
467 /* a uprobe exists for this inode:offset combination */
473 atomic_inc(&uprobe_events);
477 static void handler_chain(struct uprobe *uprobe, struct pt_regs *regs)
479 struct uprobe_consumer *consumer;
481 if (!(uprobe->flags & UPROBES_RUN_HANDLER))
484 down_read(&uprobe->consumer_rwsem);
485 consumer = uprobe->consumers;
486 for (consumer = uprobe->consumers; consumer;
487 consumer = consumer->next) {
488 if (!consumer->filter ||
489 consumer->filter(consumer, current))
490 consumer->handler(consumer, regs);
492 up_read(&uprobe->consumer_rwsem);
495 /* Returns the previous consumer */
496 static struct uprobe_consumer *add_consumer(struct uprobe *uprobe,
497 struct uprobe_consumer *consumer)
499 down_write(&uprobe->consumer_rwsem);
500 consumer->next = uprobe->consumers;
501 uprobe->consumers = consumer;
502 up_write(&uprobe->consumer_rwsem);
503 return consumer->next;
507 * For uprobe @uprobe, delete the consumer @consumer.
508 * Return true if the @consumer is deleted successfully
511 static bool del_consumer(struct uprobe *uprobe,
512 struct uprobe_consumer *consumer)
514 struct uprobe_consumer **con;
517 down_write(&uprobe->consumer_rwsem);
518 for (con = &uprobe->consumers; *con; con = &(*con)->next) {
519 if (*con == consumer) {
520 *con = consumer->next;
525 up_write(&uprobe->consumer_rwsem);
529 static int __copy_insn(struct address_space *mapping,
530 struct vm_area_struct *vma, char *insn,
531 unsigned long nbytes, unsigned long offset)
533 struct file *filp = vma->vm_file;
542 idx = (unsigned long)(offset >> PAGE_CACHE_SHIFT);
543 off1 = offset &= ~PAGE_MASK;
546 * Ensure that the page that has the original instruction is
547 * populated and in page-cache.
549 page = read_mapping_page(mapping, idx, filp);
553 vaddr = kmap_atomic(page);
554 memcpy(insn, vaddr + off1, nbytes);
555 kunmap_atomic(vaddr);
556 page_cache_release(page);
560 static int copy_insn(struct uprobe *uprobe, struct vm_area_struct *vma,
563 struct address_space *mapping;
565 unsigned long nbytes;
568 nbytes = PAGE_SIZE - addr;
569 mapping = uprobe->inode->i_mapping;
571 /* Instruction at end of binary; copy only available bytes */
572 if (uprobe->offset + MAX_UINSN_BYTES > uprobe->inode->i_size)
573 bytes = uprobe->inode->i_size - uprobe->offset;
575 bytes = MAX_UINSN_BYTES;
577 /* Instruction at the page-boundary; copy bytes in second page */
578 if (nbytes < bytes) {
579 if (__copy_insn(mapping, vma, uprobe->insn + nbytes,
580 bytes - nbytes, uprobe->offset + nbytes))
585 return __copy_insn(mapping, vma, uprobe->insn, bytes, uprobe->offset);
588 static int install_breakpoint(struct mm_struct *mm, struct uprobe *uprobe,
589 struct vm_area_struct *vma, loff_t vaddr)
595 * Probe is to be deleted;
596 * Dont know if somebody already inserted the probe;
597 * behave as if probe already exists.
599 if (!uprobe->consumers)
602 addr = (unsigned long)vaddr;
603 if (!(uprobe->flags & UPROBES_COPY_INSN)) {
604 ret = copy_insn(uprobe, vma, addr);
608 if (is_bkpt_insn(uprobe->insn))
611 ret = analyze_insn(mm, uprobe);
615 uprobe->flags |= UPROBES_COPY_INSN;
617 ret = set_bkpt(mm, uprobe, addr);
619 atomic_inc(&mm->mm_uprobes_count);
624 static void remove_breakpoint(struct mm_struct *mm, struct uprobe *uprobe,
627 if (!set_orig_insn(mm, uprobe, (unsigned long)vaddr, true))
628 atomic_dec(&mm->mm_uprobes_count);
632 * There could be threads that have hit the breakpoint and are entering the
633 * notifier code and trying to acquire the uprobes_treelock. The thread
634 * calling delete_uprobe() that is removing the uprobe from the rb_tree can
635 * race with these threads and might acquire the uprobes_treelock compared
636 * to some of the breakpoint hit threads. In such a case, the breakpoint hit
637 * threads will not find the uprobe. Hence wait till the current breakpoint
638 * hit threads acquire the uprobes_treelock before the uprobe is removed
641 static void delete_uprobe(struct uprobe *uprobe)
645 bulkref_wait_old(&uprobes_srcu);
646 spin_lock_irqsave(&uprobes_treelock, flags);
647 rb_erase(&uprobe->rb_node, &uprobes_tree);
648 spin_unlock_irqrestore(&uprobes_treelock, flags);
651 atomic_dec(&uprobe_events);
654 static struct vma_info *__find_next_vma_info(struct list_head *head,
655 loff_t offset, struct address_space *mapping,
656 struct vma_info *vi, bool is_register)
658 struct prio_tree_iter iter;
659 struct vm_area_struct *vma;
660 struct vma_info *tmpvi;
662 unsigned long pgoff = offset >> PAGE_SHIFT;
665 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
666 if (!valid_vma(vma, is_register))
670 vaddr = vma->vm_start + offset;
671 vaddr -= vma->vm_pgoff << PAGE_SHIFT;
672 list_for_each_entry(tmpvi, head, probe_list) {
673 if (tmpvi->mm == vma->vm_mm && tmpvi->vaddr == vaddr) {
680 * Another vma needs a probe to be installed. However skip
681 * installing the probe if the vma is about to be unlinked.
684 atomic_inc_not_zero(&vma->vm_mm->mm_users)) {
687 list_add(&vi->probe_list, head);
695 * Iterate in the rmap prio tree and find a vma where a probe has not
698 static struct vma_info *find_next_vma_info(struct list_head *head,
699 loff_t offset, struct address_space *mapping,
702 struct vma_info *vi, *retvi;
703 vi = kzalloc(sizeof(struct vma_info), GFP_KERNEL);
705 return ERR_PTR(-ENOMEM);
707 mutex_lock(&mapping->i_mmap_mutex);
708 retvi = __find_next_vma_info(head, offset, mapping, vi, is_register);
709 mutex_unlock(&mapping->i_mmap_mutex);
716 static int __register_uprobe(struct inode *inode, loff_t offset,
717 struct uprobe *uprobe)
719 struct list_head try_list;
720 struct vm_area_struct *vma;
721 struct address_space *mapping;
722 struct vma_info *vi, *tmpvi;
723 struct mm_struct *mm;
727 mapping = inode->i_mapping;
728 INIT_LIST_HEAD(&try_list);
729 while ((vi = find_next_vma_info(&try_list, offset,
730 mapping, true)) != NULL) {
736 down_read(&mm->mmap_sem);
737 vma = find_vma(mm, (unsigned long)vi->vaddr);
738 if (!vma || !valid_vma(vma, true)) {
739 list_del(&vi->probe_list);
741 up_read(&mm->mmap_sem);
745 vaddr = vma->vm_start + offset;
746 vaddr -= vma->vm_pgoff << PAGE_SHIFT;
747 if (vma->vm_file->f_mapping->host != inode ||
748 vaddr != vi->vaddr) {
749 list_del(&vi->probe_list);
751 up_read(&mm->mmap_sem);
755 ret = install_breakpoint(mm, uprobe, vma, vi->vaddr);
756 up_read(&mm->mmap_sem);
758 if (ret && ret == -EEXIST)
763 list_for_each_entry_safe(vi, tmpvi, &try_list, probe_list) {
764 list_del(&vi->probe_list);
770 static void __unregister_uprobe(struct inode *inode, loff_t offset,
771 struct uprobe *uprobe)
773 struct list_head try_list;
774 struct address_space *mapping;
775 struct vma_info *vi, *tmpvi;
776 struct vm_area_struct *vma;
777 struct mm_struct *mm;
780 mapping = inode->i_mapping;
781 INIT_LIST_HEAD(&try_list);
782 while ((vi = find_next_vma_info(&try_list, offset,
783 mapping, false)) != NULL) {
787 down_read(&mm->mmap_sem);
788 vma = find_vma(mm, (unsigned long)vi->vaddr);
789 if (!vma || !valid_vma(vma, false)) {
790 list_del(&vi->probe_list);
792 up_read(&mm->mmap_sem);
796 vaddr = vma->vm_start + offset;
797 vaddr -= vma->vm_pgoff << PAGE_SHIFT;
798 if (vma->vm_file->f_mapping->host != inode ||
799 vaddr != vi->vaddr) {
800 list_del(&vi->probe_list);
802 up_read(&mm->mmap_sem);
806 remove_breakpoint(mm, uprobe, vi->vaddr);
807 up_read(&mm->mmap_sem);
811 list_for_each_entry_safe(vi, tmpvi, &try_list, probe_list) {
812 list_del(&vi->probe_list);
815 delete_uprobe(uprobe);
819 * register_uprobe - register a probe
820 * @inode: the file in which the probe has to be placed.
821 * @offset: offset from the start of the file.
822 * @consumer: information on howto handle the probe..
824 * Apart from the access refcount, register_uprobe() takes a creation
825 * refcount (thro alloc_uprobe) if and only if this @uprobe is getting
826 * inserted into the rbtree (i.e first consumer for a @inode:@offset
827 * tuple). Creation refcount stops unregister_uprobe from freeing the
828 * @uprobe even before the register operation is complete. Creation
829 * refcount is released when the last @consumer for the @uprobe
832 * Return errno if it cannot successully install probes
833 * else return 0 (success)
835 int register_uprobe(struct inode *inode, loff_t offset,
836 struct uprobe_consumer *consumer)
838 struct uprobe *uprobe;
841 if (!consumer || consumer->next)
844 inode = igrab(inode);
848 if (offset > i_size_read(inode))
852 mutex_lock(uprobes_hash(inode));
853 uprobe = alloc_uprobe(inode, offset);
854 if (uprobe && !add_consumer(uprobe, consumer)) {
855 ret = __register_uprobe(inode, offset, uprobe);
857 uprobe->consumers = NULL;
858 __unregister_uprobe(inode, offset, uprobe);
860 uprobe->flags |= UPROBES_RUN_HANDLER;
863 mutex_unlock(uprobes_hash(inode));
872 * unregister_uprobe - unregister a already registered probe.
873 * @inode: the file in which the probe has to be removed.
874 * @offset: offset from the start of the file.
875 * @consumer: identify which probe if multiple probes are colocated.
877 void unregister_uprobe(struct inode *inode, loff_t offset,
878 struct uprobe_consumer *consumer)
880 struct uprobe *uprobe = NULL;
882 inode = igrab(inode);
883 if (!inode || !consumer)
886 uprobe = find_uprobe(inode, offset);
890 mutex_lock(uprobes_hash(inode));
891 if (!del_consumer(uprobe, consumer)) {
892 mutex_unlock(uprobes_hash(inode));
896 if (!uprobe->consumers) {
897 __unregister_uprobe(inode, offset, uprobe);
898 uprobe->flags &= ~UPROBES_RUN_HANDLER;
900 mutex_unlock(uprobes_hash(inode));
910 * Of all the nodes that correspond to the given inode, return the node
911 * with the least offset.
913 static struct rb_node *find_least_offset_node(struct inode *inode)
915 struct uprobe u = { .inode = inode, .offset = 0};
916 struct rb_node *n = uprobes_tree.rb_node;
917 struct rb_node *close_node = NULL;
918 struct uprobe *uprobe;
922 uprobe = rb_entry(n, struct uprobe, rb_node);
923 match = match_uprobe(&u, uprobe);
924 if (uprobe->inode == inode)
939 * For a given inode, build a list of probes that need to be inserted.
941 static void build_probe_list(struct inode *inode, struct list_head *head)
943 struct uprobe *uprobe;
947 spin_lock_irqsave(&uprobes_treelock, flags);
948 n = find_least_offset_node(inode);
949 for (; n; n = rb_next(n)) {
950 uprobe = rb_entry(n, struct uprobe, rb_node);
951 if (uprobe->inode != inode)
954 list_add(&uprobe->pending_list, head);
955 atomic_inc(&uprobe->ref);
957 spin_unlock_irqrestore(&uprobes_treelock, flags);
961 * Called from mmap_region.
962 * called with mm->mmap_sem acquired.
964 * Return -ve no if we fail to insert probes and we cannot
966 * Return 0 otherwise. i.e :
967 * - successful insertion of probes
968 * - (or) no possible probes to be inserted.
969 * - (or) insertion of probes failed but we can bail-out.
971 int mmap_uprobe(struct vm_area_struct *vma)
973 struct list_head tmp_list;
974 struct uprobe *uprobe, *u;
976 int ret = 0, count = 0;
978 if (!atomic_read(&uprobe_events) || !valid_vma(vma, true))
979 return ret; /* Bail-out */
981 inode = igrab(vma->vm_file->f_mapping->host);
985 INIT_LIST_HEAD(&tmp_list);
986 mutex_lock(uprobes_mmap_hash(inode));
987 build_probe_list(inode, &tmp_list);
988 list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
991 list_del(&uprobe->pending_list);
993 vaddr = vma->vm_start + uprobe->offset;
994 vaddr -= vma->vm_pgoff << PAGE_SHIFT;
995 if (vaddr < vma->vm_start || vaddr >= vma->vm_end) {
999 ret = install_breakpoint(vma->vm_mm, uprobe, vma,
1001 if (ret == -EEXIST) {
1002 atomic_inc(&vma->vm_mm->mm_uprobes_count);
1011 mutex_unlock(uprobes_mmap_hash(inode));
1014 atomic_sub(count, &vma->vm_mm->mm_uprobes_count);
1020 * Called in context of a munmap of a vma.
1022 void munmap_uprobe(struct vm_area_struct *vma)
1024 struct list_head tmp_list;
1025 struct uprobe *uprobe, *u;
1026 struct inode *inode;
1028 if (!atomic_read(&uprobe_events) || !valid_vma(vma, false))
1029 return; /* Bail-out */
1031 if (!atomic_read(&vma->vm_mm->mm_uprobes_count))
1034 inode = igrab(vma->vm_file->f_mapping->host);
1038 INIT_LIST_HEAD(&tmp_list);
1039 mutex_lock(uprobes_mmap_hash(inode));
1040 build_probe_list(inode, &tmp_list);
1041 list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
1043 uprobe_opcode_t opcode;
1045 list_del(&uprobe->pending_list);
1046 vaddr = vma->vm_start + uprobe->offset;
1047 vaddr -= vma->vm_pgoff << PAGE_SHIFT;
1048 if (vaddr >= vma->vm_start && vaddr < vma->vm_end) {
1051 * An unregister could have removed the probe before
1052 * unmap. So check before we decrement the count.
1054 if (!read_opcode(vma->vm_mm, vaddr, &opcode) &&
1055 (opcode == UPROBES_BKPT_INSN))
1056 atomic_dec(&vma->vm_mm->mm_uprobes_count);
1060 mutex_unlock(uprobes_mmap_hash(inode));
1065 /* Slot allocation for XOL */
1066 static int xol_add_vma(struct uprobes_xol_area *area)
1068 struct mm_struct *mm;
1071 area->page = alloc_page(GFP_HIGHUSER);
1076 down_write(&mm->mmap_sem);
1078 if (mm->uprobes_xol_area)
1083 /* Try to map as high as possible, this is only a hint. */
1084 area->vaddr = get_unmapped_area(NULL, TASK_SIZE - PAGE_SIZE,
1086 if (area->vaddr & ~PAGE_MASK) {
1091 ret = install_special_mapping(mm, area->vaddr, PAGE_SIZE,
1092 VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO,
1097 smp_wmb(); /* pairs with get_uprobes_xol_area() */
1098 mm->uprobes_xol_area = area;
1102 up_write(&mm->mmap_sem);
1104 __free_page(area->page);
1109 static struct uprobes_xol_area *get_uprobes_xol_area(struct mm_struct *mm)
1111 struct uprobes_xol_area *area = mm->uprobes_xol_area;
1112 smp_read_barrier_depends();/* pairs with wmb in xol_add_vma() */
1117 * xol_alloc_area - Allocate process's uprobes_xol_area.
1118 * This area will be used for storing instructions for execution out of
1121 * Returns the allocated area or NULL.
1123 static struct uprobes_xol_area *xol_alloc_area(void)
1125 struct uprobes_xol_area *area;
1127 area = kzalloc(sizeof(*area), GFP_KERNEL);
1128 if (unlikely(!area))
1131 area->bitmap = kzalloc(BITS_TO_LONGS(UINSNS_PER_PAGE) * sizeof(long),
1137 init_waitqueue_head(&area->wq);
1138 if (!xol_add_vma(area))
1142 kfree(area->bitmap);
1144 return get_uprobes_xol_area(current->mm);
1148 * free_uprobes_xol_area - Free the area allocated for slots.
1150 void free_uprobes_xol_area(struct mm_struct *mm)
1152 struct uprobes_xol_area *area = mm->uprobes_xol_area;
1157 put_page(area->page);
1158 kfree(area->bitmap);
1163 * - search for a free slot.
1165 static unsigned long xol_take_insn_slot(struct uprobes_xol_area *area)
1167 unsigned long slot_addr;
1171 slot_nr = find_first_zero_bit(area->bitmap, UINSNS_PER_PAGE);
1172 if (slot_nr < UINSNS_PER_PAGE) {
1173 if (!test_and_set_bit(slot_nr, area->bitmap))
1176 slot_nr = UINSNS_PER_PAGE;
1179 wait_event(area->wq,
1180 (atomic_read(&area->slot_count) < UINSNS_PER_PAGE));
1181 } while (slot_nr >= UINSNS_PER_PAGE);
1183 slot_addr = area->vaddr + (slot_nr * UPROBES_XOL_SLOT_BYTES);
1184 atomic_inc(&area->slot_count);
1189 * xol_get_insn_slot - If was not allocated a slot, then
1191 * Returns the allocated slot address or 0.
1193 static unsigned long xol_get_insn_slot(struct uprobe *uprobe,
1194 unsigned long slot_addr)
1196 struct uprobes_xol_area *area;
1197 unsigned long offset;
1200 area = get_uprobes_xol_area(current->mm);
1202 area = xol_alloc_area();
1206 current->utask->xol_vaddr = xol_take_insn_slot(area);
1209 * Initialize the slot if xol_vaddr points to valid
1212 if (unlikely(!current->utask->xol_vaddr))
1215 current->utask->vaddr = slot_addr;
1216 offset = current->utask->xol_vaddr & ~PAGE_MASK;
1217 vaddr = kmap_atomic(area->page);
1218 memcpy(vaddr + offset, uprobe->insn, MAX_UINSN_BYTES);
1219 kunmap_atomic(vaddr);
1220 return current->utask->xol_vaddr;
1224 * xol_free_insn_slot - If slot was earlier allocated by
1225 * @xol_get_insn_slot(), make the slot available for
1226 * subsequent requests.
1228 static void xol_free_insn_slot(struct task_struct *tsk)
1230 struct uprobes_xol_area *area;
1231 unsigned long vma_end;
1232 unsigned long slot_addr;
1234 if (!tsk->mm || !tsk->mm->uprobes_xol_area || !tsk->utask)
1237 slot_addr = tsk->utask->xol_vaddr;
1239 if (unlikely(!slot_addr || IS_ERR_VALUE(slot_addr)))
1242 area = tsk->mm->uprobes_xol_area;
1243 vma_end = area->vaddr + PAGE_SIZE;
1244 if (area->vaddr <= slot_addr && slot_addr < vma_end) {
1246 unsigned long offset = slot_addr - area->vaddr;
1248 slot_nr = offset / UPROBES_XOL_SLOT_BYTES;
1249 if (slot_nr >= UINSNS_PER_PAGE)
1252 clear_bit(slot_nr, area->bitmap);
1253 atomic_dec(&area->slot_count);
1254 if (waitqueue_active(&area->wq))
1256 tsk->utask->xol_vaddr = 0;
1261 * get_uprobe_bkpt_addr - compute address of bkpt given post-bkpt regs
1262 * @regs: Reflects the saved state of the task after it has hit a breakpoint
1264 * Return the address of the breakpoint instruction.
1266 unsigned long __weak get_uprobe_bkpt_addr(struct pt_regs *regs)
1268 return instruction_pointer(regs) - UPROBES_BKPT_INSN_SIZE;
1272 * Called with no locks held.
1273 * Called in context of a exiting or a exec-ing thread.
1275 void free_uprobe_utask(struct task_struct *tsk)
1277 struct uprobe_task *utask = tsk->utask;
1279 if (tsk->uprobes_bulkref_id != -1)
1280 bulkref_put(&uprobes_srcu, tsk->uprobes_bulkref_id);
1285 if (utask->active_uprobe)
1286 put_uprobe(utask->active_uprobe);
1288 xol_free_insn_slot(tsk);
1294 * Allocate a uprobe_task object for the task.
1295 * Called when the thread hits a breakpoint for the first time.
1298 * - pointer to new uprobe_task on success
1299 * - negative errno otherwise
1301 static struct uprobe_task *add_utask(void)
1303 struct uprobe_task *utask;
1305 utask = kzalloc(sizeof *utask, GFP_KERNEL);
1306 if (unlikely(utask == NULL))
1307 return ERR_PTR(-ENOMEM);
1309 utask->active_uprobe = NULL;
1310 current->utask = utask;
1314 /* Prepare to single-step probed instruction out of line. */
1315 static int pre_ssout(struct uprobe *uprobe, struct pt_regs *regs,
1316 unsigned long vaddr)
1318 if (xol_get_insn_slot(uprobe, vaddr) && !pre_xol(uprobe, regs))
1323 bool uprobe_deny_signal(void)
1325 struct task_struct *tsk = current;
1326 struct uprobe_task *utask = tsk->utask;
1328 if (likely(!utask || !utask->active_uprobe))
1331 WARN_ON_ONCE(utask->state != UTASK_SSTEP);
1333 if (signal_pending(tsk)) {
1334 spin_lock_irq(&tsk->sighand->siglock);
1335 clear_tsk_thread_flag(tsk, TIF_SIGPENDING);
1336 spin_unlock_irq(&tsk->sighand->siglock);
1338 if (__fatal_signal_pending(tsk) || xol_was_trapped(tsk)) {
1339 utask->state = UTASK_SSTEP_TRAPPED;
1340 set_tsk_thread_flag(tsk, TIF_UPROBE);
1341 set_tsk_thread_flag(tsk, TIF_NOTIFY_RESUME);
1348 bool __weak can_skip_xol(struct pt_regs *regs, struct uprobe *u)
1350 u->flags &= ~UPROBES_SKIP_SSTEP;
1355 * uprobe_notify_resume gets called in task context just before returning
1358 * If its the first time the probepoint is hit, slot gets allocated here.
1359 * If its the first time the thread hit a breakpoint, utask gets
1362 void uprobe_notify_resume(struct pt_regs *regs)
1364 struct vm_area_struct *vma;
1365 struct uprobe_task *utask;
1366 struct mm_struct *mm;
1367 struct uprobe *u = NULL;
1368 unsigned long probept;
1370 utask = current->utask;
1372 if (!utask || utask->state == UTASK_BP_HIT) {
1373 probept = get_uprobe_bkpt_addr(regs);
1374 down_read(&mm->mmap_sem);
1375 vma = find_vma(mm, probept);
1376 if (vma && valid_vma(vma, false))
1377 u = find_uprobe(vma->vm_file->f_mapping->host,
1378 probept - vma->vm_start +
1379 (vma->vm_pgoff << PAGE_SHIFT));
1381 bulkref_put(&uprobes_srcu, current->uprobes_bulkref_id);
1382 current->uprobes_bulkref_id = -1;
1383 up_read(&mm->mmap_sem);
1385 /* No matching uprobe; signal SIGTRAP. */
1388 utask = add_utask();
1389 /* Cannot Allocate; re-execute the instruction. */
1393 utask->active_uprobe = u;
1394 handler_chain(u, regs);
1396 if (u->flags & UPROBES_SKIP_SSTEP && can_skip_xol(regs, u))
1399 utask->state = UTASK_SSTEP;
1400 if (!pre_ssout(u, regs, probept))
1401 user_enable_single_step(current);
1403 /* Cannot Singlestep; re-execute the instruction. */
1406 u = utask->active_uprobe;
1407 if (utask->state == UTASK_SSTEP_ACK)
1409 else if (utask->state == UTASK_SSTEP_TRAPPED)
1415 utask->active_uprobe = NULL;
1416 utask->state = UTASK_RUNNING;
1417 user_disable_single_step(current);
1418 xol_free_insn_slot(current);
1420 spin_lock_irq(¤t->sighand->siglock);
1421 recalc_sigpending(); /* see uprobe_deny_signal() */
1422 spin_unlock_irq(¤t->sighand->siglock);
1428 utask->active_uprobe = NULL;
1429 utask->state = UTASK_RUNNING;
1432 if (!(u->flags & UPROBES_SKIP_SSTEP))
1433 set_instruction_pointer(regs, probept);
1437 send_sig(SIGTRAP, current, 0);
1441 * uprobe_bkpt_notifier gets called from interrupt context
1442 * it gets a reference to the ppt and sets TIF_UPROBE flag,
1444 int uprobe_bkpt_notifier(struct pt_regs *regs)
1446 struct uprobe_task *utask;
1448 if (!current->mm || !atomic_read(¤t->mm->mm_uprobes_count))
1449 /* task is currently not uprobed */
1452 utask = current->utask;
1454 utask->state = UTASK_BP_HIT;
1456 set_thread_flag(TIF_UPROBE);
1457 current->uprobes_bulkref_id = bulkref_get(&uprobes_srcu);
1462 * uprobe_post_notifier gets called in interrupt context.
1463 * It completes the single step operation.
1465 int uprobe_post_notifier(struct pt_regs *regs)
1467 struct uprobe_task *utask = current->utask;
1469 if (!current->mm || !utask || !utask->active_uprobe)
1470 /* task is currently not uprobed */
1473 utask->state = UTASK_SSTEP_ACK;
1474 set_thread_flag(TIF_UPROBE);
1478 struct notifier_block uprobe_exception_nb = {
1479 .notifier_call = uprobe_exception_notify,
1480 .priority = INT_MAX - 1, /* notified after kprobes, kgdb */
1483 static int __init init_uprobes(void)
1487 for (i = 0; i < UPROBES_HASH_SZ; i++) {
1488 mutex_init(&uprobes_mutex[i]);
1489 mutex_init(&uprobes_mmap_mutex[i]);
1491 init_bulkref(&uprobes_srcu);
1492 return register_die_notifier(&uprobe_exception_nb);
1495 static void __exit exit_uprobes(void)
1499 module_init(init_uprobes);
1500 module_exit(exit_uprobes);