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