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