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