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