blob: 2671d9ad49beaa11943be1eb0227239b106719df [file] [log] [blame]
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01002 * User-space Probes (UProbes)
Srikar Dronamraju2b144492012-02-09 14:56:42 +05303 *
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 Molnar35aa6212012-02-22 11:37:29 +010018 * Copyright (C) IBM Corporation, 2008-2012
Srikar Dronamraju2b144492012-02-09 14:56:42 +053019 * Authors:
20 * Srikar Dronamraju
21 * Jim Keniston
Ingo Molnar35aa6212012-02-22 11:37:29 +010022 * Copyright (C) 2011-2012 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
Srikar Dronamraju2b144492012-02-09 14:56:42 +053023 */
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 Dronamraju0326f5a2012-03-13 23:30:11 +053033#include <linux/ptrace.h> /* user_enable_single_step */
34#include <linux/kdebug.h> /* notifier mechanism */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010035
Srikar Dronamraju2b144492012-02-09 14:56:42 +053036#include <linux/uprobes.h>
37
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +053038#define UINSNS_PER_PAGE (PAGE_SIZE/UPROBE_XOL_SLOT_BYTES)
39#define MAX_UPROBE_XOL_SLOTS UINSNS_PER_PAGE
40
Srikar Dronamraju2b144492012-02-09 14:56:42 +053041static struct rb_root uprobes_tree = RB_ROOT;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010042
Srikar Dronamraju2b144492012-02-09 14:56:42 +053043static DEFINE_SPINLOCK(uprobes_treelock); /* serialize rbtree access */
44
45#define UPROBES_HASH_SZ 13
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010046
Peter Zijlstrac5784de2012-06-15 17:43:39 +020047/*
48 * We need separate register/unregister and mmap/munmap lock hashes because
49 * of mmap_sem nesting.
50 *
51 * uprobe_register() needs to install probes on (potentially) all processes
52 * and thus needs to acquire multiple mmap_sems (consequtively, not
53 * concurrently), whereas uprobe_mmap() is called while holding mmap_sem
54 * for the particular process doing the mmap.
55 *
56 * uprobe_register()->register_for_each_vma() needs to drop/acquire mmap_sem
57 * because of lock order against i_mmap_mutex. This means there's a hole in
58 * the register vma iteration where a mmap() can happen.
59 *
60 * Thus uprobe_register() can race with uprobe_mmap() and we can try and
61 * install a probe where one is already installed.
62 */
63
Srikar Dronamraju2b144492012-02-09 14:56:42 +053064/* serialize (un)register */
65static struct mutex uprobes_mutex[UPROBES_HASH_SZ];
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010066
67#define uprobes_hash(v) (&uprobes_mutex[((unsigned long)(v)) % UPROBES_HASH_SZ])
Srikar Dronamraju2b144492012-02-09 14:56:42 +053068
69/* serialize uprobe->pending_list */
70static struct mutex uprobes_mmap_mutex[UPROBES_HASH_SZ];
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010071#define uprobes_mmap_hash(v) (&uprobes_mmap_mutex[((unsigned long)(v)) % UPROBES_HASH_SZ])
Srikar Dronamraju2b144492012-02-09 14:56:42 +053072
73/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010074 * uprobe_events allows us to skip the uprobe_mmap if there are no uprobe
Srikar Dronamraju2b144492012-02-09 14:56:42 +053075 * events active at this time. Probably a fine grained per inode count is
76 * better?
77 */
78static atomic_t uprobe_events = ATOMIC_INIT(0);
79
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +053080struct uprobe {
81 struct rb_node rb_node; /* node in the rb tree */
82 atomic_t ref;
83 struct rw_semaphore consumer_rwsem;
84 struct list_head pending_list;
85 struct uprobe_consumer *consumers;
86 struct inode *inode; /* Also hold a ref to inode */
87 loff_t offset;
88 int flags;
89 struct arch_uprobe arch;
90};
91
Srikar Dronamraju2b144492012-02-09 14:56:42 +053092/*
93 * valid_vma: Verify if the specified vma is an executable vma
94 * Relax restrictions while unregistering: vm_flags might have
95 * changed after breakpoint was inserted.
96 * - is_register: indicates if we are in register context.
97 * - Return 1 if the specified virtual address is in an
98 * executable vma.
99 */
100static bool valid_vma(struct vm_area_struct *vma, bool is_register)
101{
102 if (!vma->vm_file)
103 return false;
104
105 if (!is_register)
106 return true;
107
Oleg Nesterovea131372012-06-15 17:43:22 +0200108 if ((vma->vm_flags & (VM_HUGETLB|VM_READ|VM_WRITE|VM_EXEC|VM_SHARED))
109 == (VM_READ|VM_EXEC))
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530110 return true;
111
112 return false;
113}
114
115static loff_t vma_address(struct vm_area_struct *vma, loff_t offset)
116{
117 loff_t vaddr;
118
119 vaddr = vma->vm_start + offset;
120 vaddr -= vma->vm_pgoff << PAGE_SHIFT;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100121
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530122 return vaddr;
123}
124
125/**
126 * __replace_page - replace page in vma by new page.
127 * based on replace_page in mm/ksm.c
128 *
129 * @vma: vma that holds the pte pointing to page
130 * @page: the cowed page we are replacing by kpage
131 * @kpage: the modified page we replace page by
132 *
133 * Returns 0 on success, -EFAULT on failure.
134 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100135static int __replace_page(struct vm_area_struct *vma, struct page *page, struct page *kpage)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530136{
137 struct mm_struct *mm = vma->vm_mm;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530138 unsigned long addr;
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200139 spinlock_t *ptl;
140 pte_t *ptep;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530141
142 addr = page_address_in_vma(page, vma);
143 if (addr == -EFAULT)
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200144 return -EFAULT;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530145
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200146 ptep = page_check_address(page, mm, addr, &ptl, 0);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530147 if (!ptep)
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200148 return -EAGAIN;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530149
150 get_page(kpage);
151 page_add_new_anon_rmap(kpage, vma, addr);
152
Srikar Dronamraju7396fa82012-04-11 16:05:16 +0530153 if (!PageAnon(page)) {
154 dec_mm_counter(mm, MM_FILEPAGES);
155 inc_mm_counter(mm, MM_ANONPAGES);
156 }
157
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530158 flush_cache_page(vma, addr, pte_pfn(*ptep));
159 ptep_clear_flush(vma, addr, ptep);
160 set_pte_at_notify(mm, addr, ptep, mk_pte(kpage, vma->vm_page_prot));
161
162 page_remove_rmap(page);
163 if (!page_mapped(page))
164 try_to_free_swap(page);
165 put_page(page);
166 pte_unmap_unlock(ptep, ptl);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530167
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200168 return 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530169}
170
171/**
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530172 * is_swbp_insn - check if instruction is breakpoint instruction.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530173 * @insn: instruction to be checked.
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530174 * Default implementation of is_swbp_insn
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530175 * Returns true if @insn is a breakpoint instruction.
176 */
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530177bool __weak is_swbp_insn(uprobe_opcode_t *insn)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530178{
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530179 return *insn == UPROBE_SWBP_INSN;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530180}
181
182/*
183 * NOTE:
184 * Expect the breakpoint instruction to be the smallest size instruction for
185 * the architecture. If an arch has variable length instruction and the
186 * breakpoint instruction is not of the smallest length instruction
187 * supported by that architecture then we need to modify read_opcode /
188 * write_opcode accordingly. This would never be a problem for archs that
189 * have fixed length instructions.
190 */
191
192/*
193 * write_opcode - write the opcode at a given virtual address.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530194 * @auprobe: arch breakpointing information.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530195 * @mm: the probed process address space.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530196 * @vaddr: the virtual address to store the opcode.
197 * @opcode: opcode to be written at @vaddr.
198 *
199 * Called with mm->mmap_sem held (for read and with a reference to
200 * mm).
201 *
202 * For mm @mm, write the opcode at @vaddr.
203 * Return 0 (success) or a negative errno.
204 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530205static int write_opcode(struct arch_uprobe *auprobe, struct mm_struct *mm,
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530206 unsigned long vaddr, uprobe_opcode_t opcode)
207{
208 struct page *old_page, *new_page;
209 struct address_space *mapping;
210 void *vaddr_old, *vaddr_new;
211 struct vm_area_struct *vma;
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +0530212 struct uprobe *uprobe;
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200213 unsigned long pgoff;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530214 loff_t addr;
215 int ret;
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200216retry:
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530217 /* Read the page with vaddr into memory */
218 ret = get_user_pages(NULL, mm, vaddr, 1, 0, 0, &old_page, &vma);
219 if (ret <= 0)
220 return ret;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100221
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530222 ret = -EINVAL;
223
224 /*
225 * We are interested in text pages only. Our pages of interest
226 * should be mapped for read and execute only. We desist from
227 * adding probes in write mapped pages since the breakpoints
228 * might end up in the file copy.
229 */
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530230 if (!valid_vma(vma, is_swbp_insn(&opcode)))
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530231 goto put_out;
232
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +0530233 uprobe = container_of(auprobe, struct uprobe, arch);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530234 mapping = uprobe->inode->i_mapping;
235 if (mapping != vma->vm_file->f_mapping)
236 goto put_out;
237
238 addr = vma_address(vma, uprobe->offset);
239 if (vaddr != (unsigned long)addr)
240 goto put_out;
241
242 ret = -ENOMEM;
243 new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vaddr);
244 if (!new_page)
245 goto put_out;
246
247 __SetPageUptodate(new_page);
248
249 /*
250 * lock page will serialize against do_wp_page()'s
251 * PageAnon() handling
252 */
253 lock_page(old_page);
254 /* copy the page now that we've got it stable */
255 vaddr_old = kmap_atomic(old_page);
256 vaddr_new = kmap_atomic(new_page);
257
258 memcpy(vaddr_new, vaddr_old, PAGE_SIZE);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100259
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530260 /* poke the new insn in, ASSUMES we don't cross page boundary */
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200261 pgoff = (vaddr & ~PAGE_MASK);
262 BUG_ON(pgoff + UPROBE_SWBP_INSN_SIZE > PAGE_SIZE);
263 memcpy(vaddr_new + pgoff, &opcode, UPROBE_SWBP_INSN_SIZE);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530264
265 kunmap_atomic(vaddr_new);
266 kunmap_atomic(vaddr_old);
267
268 ret = anon_vma_prepare(vma);
269 if (ret)
270 goto unlock_out;
271
272 lock_page(new_page);
273 ret = __replace_page(vma, old_page, new_page);
274 unlock_page(new_page);
275
276unlock_out:
277 unlock_page(old_page);
278 page_cache_release(new_page);
279
280put_out:
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100281 put_page(old_page);
282
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200283 if (unlikely(ret == -EAGAIN))
284 goto retry;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530285 return ret;
286}
287
288/**
289 * read_opcode - read the opcode at a given virtual address.
290 * @mm: the probed process address space.
291 * @vaddr: the virtual address to read the opcode.
292 * @opcode: location to store the read opcode.
293 *
294 * Called with mm->mmap_sem held (for read and with a reference to
295 * mm.
296 *
297 * For mm @mm, read the opcode at @vaddr and store it in @opcode.
298 * Return 0 (success) or a negative errno.
299 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100300static int read_opcode(struct mm_struct *mm, unsigned long vaddr, uprobe_opcode_t *opcode)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530301{
302 struct page *page;
303 void *vaddr_new;
304 int ret;
305
Oleg Nesterova3d7bb42012-05-29 21:27:59 +0200306 ret = get_user_pages(NULL, mm, vaddr, 1, 0, 1, &page, NULL);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530307 if (ret <= 0)
308 return ret;
309
310 lock_page(page);
311 vaddr_new = kmap_atomic(page);
312 vaddr &= ~PAGE_MASK;
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530313 memcpy(opcode, vaddr_new + vaddr, UPROBE_SWBP_INSN_SIZE);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530314 kunmap_atomic(vaddr_new);
315 unlock_page(page);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100316
317 put_page(page);
318
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530319 return 0;
320}
321
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530322static int is_swbp_at_addr(struct mm_struct *mm, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530323{
324 uprobe_opcode_t opcode;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100325 int result;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530326
Oleg Nesterovc00b2752012-05-29 21:27:44 +0200327 if (current->mm == mm) {
328 pagefault_disable();
329 result = __copy_from_user_inatomic(&opcode, (void __user*)vaddr,
330 sizeof(opcode));
331 pagefault_enable();
332
333 if (likely(result == 0))
334 goto out;
335 }
336
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100337 result = read_opcode(mm, vaddr, &opcode);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530338 if (result)
339 return result;
Oleg Nesterovc00b2752012-05-29 21:27:44 +0200340out:
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530341 if (is_swbp_insn(&opcode))
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530342 return 1;
343
344 return 0;
345}
346
347/**
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530348 * set_swbp - store breakpoint at a given address.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530349 * @auprobe: arch specific probepoint information.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530350 * @mm: the probed process address space.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530351 * @vaddr: the virtual address to insert the opcode.
352 *
353 * For mm @mm, store the breakpoint instruction at @vaddr.
354 * Return 0 (success) or a negative errno.
355 */
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530356int __weak set_swbp(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530357{
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100358 int result;
Peter Zijlstrac5784de2012-06-15 17:43:39 +0200359 /*
360 * See the comment near uprobes_hash().
361 */
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530362 result = is_swbp_at_addr(mm, vaddr);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530363 if (result == 1)
364 return -EEXIST;
365
366 if (result)
367 return result;
368
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530369 return write_opcode(auprobe, mm, vaddr, UPROBE_SWBP_INSN);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530370}
371
372/**
373 * set_orig_insn - Restore the original instruction.
374 * @mm: the probed process address space.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530375 * @auprobe: arch specific probepoint information.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530376 * @vaddr: the virtual address to insert the opcode.
377 * @verify: if true, verify existance of breakpoint instruction.
378 *
379 * For mm @mm, restore the original opcode (opcode) at @vaddr.
380 * Return 0 (success) or a negative errno.
381 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100382int __weak
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530383set_orig_insn(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr, bool verify)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530384{
385 if (verify) {
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100386 int result;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530387
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530388 result = is_swbp_at_addr(mm, vaddr);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530389 if (!result)
390 return -EINVAL;
391
392 if (result != 1)
393 return result;
394 }
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530395 return write_opcode(auprobe, mm, vaddr, *(uprobe_opcode_t *)auprobe->insn);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530396}
397
398static int match_uprobe(struct uprobe *l, struct uprobe *r)
399{
400 if (l->inode < r->inode)
401 return -1;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100402
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530403 if (l->inode > r->inode)
404 return 1;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530405
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100406 if (l->offset < r->offset)
407 return -1;
408
409 if (l->offset > r->offset)
410 return 1;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530411
412 return 0;
413}
414
415static struct uprobe *__find_uprobe(struct inode *inode, loff_t offset)
416{
417 struct uprobe u = { .inode = inode, .offset = offset };
418 struct rb_node *n = uprobes_tree.rb_node;
419 struct uprobe *uprobe;
420 int match;
421
422 while (n) {
423 uprobe = rb_entry(n, struct uprobe, rb_node);
424 match = match_uprobe(&u, uprobe);
425 if (!match) {
426 atomic_inc(&uprobe->ref);
427 return uprobe;
428 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100429
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530430 if (match < 0)
431 n = n->rb_left;
432 else
433 n = n->rb_right;
434 }
435 return NULL;
436}
437
438/*
439 * Find a uprobe corresponding to a given inode:offset
440 * Acquires uprobes_treelock
441 */
442static struct uprobe *find_uprobe(struct inode *inode, loff_t offset)
443{
444 struct uprobe *uprobe;
445 unsigned long flags;
446
447 spin_lock_irqsave(&uprobes_treelock, flags);
448 uprobe = __find_uprobe(inode, offset);
449 spin_unlock_irqrestore(&uprobes_treelock, flags);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100450
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530451 return uprobe;
452}
453
454static struct uprobe *__insert_uprobe(struct uprobe *uprobe)
455{
456 struct rb_node **p = &uprobes_tree.rb_node;
457 struct rb_node *parent = NULL;
458 struct uprobe *u;
459 int match;
460
461 while (*p) {
462 parent = *p;
463 u = rb_entry(parent, struct uprobe, rb_node);
464 match = match_uprobe(uprobe, u);
465 if (!match) {
466 atomic_inc(&u->ref);
467 return u;
468 }
469
470 if (match < 0)
471 p = &parent->rb_left;
472 else
473 p = &parent->rb_right;
474
475 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100476
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530477 u = NULL;
478 rb_link_node(&uprobe->rb_node, parent, p);
479 rb_insert_color(&uprobe->rb_node, &uprobes_tree);
480 /* get access + creation ref */
481 atomic_set(&uprobe->ref, 2);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100482
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530483 return u;
484}
485
486/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100487 * Acquire uprobes_treelock.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530488 * Matching uprobe already exists in rbtree;
489 * increment (access refcount) and return the matching uprobe.
490 *
491 * No matching uprobe; insert the uprobe in rb_tree;
492 * get a double refcount (access + creation) and return NULL.
493 */
494static struct uprobe *insert_uprobe(struct uprobe *uprobe)
495{
496 unsigned long flags;
497 struct uprobe *u;
498
499 spin_lock_irqsave(&uprobes_treelock, flags);
500 u = __insert_uprobe(uprobe);
501 spin_unlock_irqrestore(&uprobes_treelock, flags);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100502
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530503 /* For now assume that the instruction need not be single-stepped */
504 uprobe->flags |= UPROBE_SKIP_SSTEP;
505
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530506 return u;
507}
508
509static void put_uprobe(struct uprobe *uprobe)
510{
511 if (atomic_dec_and_test(&uprobe->ref))
512 kfree(uprobe);
513}
514
515static struct uprobe *alloc_uprobe(struct inode *inode, loff_t offset)
516{
517 struct uprobe *uprobe, *cur_uprobe;
518
519 uprobe = kzalloc(sizeof(struct uprobe), GFP_KERNEL);
520 if (!uprobe)
521 return NULL;
522
523 uprobe->inode = igrab(inode);
524 uprobe->offset = offset;
525 init_rwsem(&uprobe->consumer_rwsem);
526 INIT_LIST_HEAD(&uprobe->pending_list);
527
528 /* add to uprobes_tree, sorted on inode:offset */
529 cur_uprobe = insert_uprobe(uprobe);
530
531 /* a uprobe exists for this inode:offset combination */
532 if (cur_uprobe) {
533 kfree(uprobe);
534 uprobe = cur_uprobe;
535 iput(inode);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100536 } else {
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530537 atomic_inc(&uprobe_events);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100538 }
539
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530540 return uprobe;
541}
542
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530543static void handler_chain(struct uprobe *uprobe, struct pt_regs *regs)
544{
545 struct uprobe_consumer *uc;
546
547 if (!(uprobe->flags & UPROBE_RUN_HANDLER))
548 return;
549
550 down_read(&uprobe->consumer_rwsem);
551 for (uc = uprobe->consumers; uc; uc = uc->next) {
552 if (!uc->filter || uc->filter(uc, current))
553 uc->handler(uc, regs);
554 }
555 up_read(&uprobe->consumer_rwsem);
556}
557
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530558/* Returns the previous consumer */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100559static struct uprobe_consumer *
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530560consumer_add(struct uprobe *uprobe, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530561{
562 down_write(&uprobe->consumer_rwsem);
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530563 uc->next = uprobe->consumers;
564 uprobe->consumers = uc;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530565 up_write(&uprobe->consumer_rwsem);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100566
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530567 return uc->next;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530568}
569
570/*
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530571 * For uprobe @uprobe, delete the consumer @uc.
572 * Return true if the @uc is deleted successfully
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530573 * or return false.
574 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530575static bool consumer_del(struct uprobe *uprobe, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530576{
577 struct uprobe_consumer **con;
578 bool ret = false;
579
580 down_write(&uprobe->consumer_rwsem);
581 for (con = &uprobe->consumers; *con; con = &(*con)->next) {
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530582 if (*con == uc) {
583 *con = uc->next;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530584 ret = true;
585 break;
586 }
587 }
588 up_write(&uprobe->consumer_rwsem);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100589
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530590 return ret;
591}
592
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530593static int
594__copy_insn(struct address_space *mapping, struct vm_area_struct *vma, char *insn,
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530595 unsigned long nbytes, unsigned long offset)
596{
597 struct file *filp = vma->vm_file;
598 struct page *page;
599 void *vaddr;
600 unsigned long off1;
601 unsigned long idx;
602
603 if (!filp)
604 return -EINVAL;
605
Oleg Nesterovcc359d12012-06-15 17:43:25 +0200606 if (!mapping->a_ops->readpage)
607 return -EIO;
608
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530609 idx = (unsigned long)(offset >> PAGE_CACHE_SHIFT);
610 off1 = offset &= ~PAGE_MASK;
611
612 /*
613 * Ensure that the page that has the original instruction is
614 * populated and in page-cache.
615 */
616 page = read_mapping_page(mapping, idx, filp);
617 if (IS_ERR(page))
618 return PTR_ERR(page);
619
620 vaddr = kmap_atomic(page);
621 memcpy(insn, vaddr + off1, nbytes);
622 kunmap_atomic(vaddr);
623 page_cache_release(page);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100624
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530625 return 0;
626}
627
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530628static int
629copy_insn(struct uprobe *uprobe, struct vm_area_struct *vma, unsigned long addr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530630{
631 struct address_space *mapping;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530632 unsigned long nbytes;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100633 int bytes;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530634
635 addr &= ~PAGE_MASK;
636 nbytes = PAGE_SIZE - addr;
637 mapping = uprobe->inode->i_mapping;
638
639 /* Instruction at end of binary; copy only available bytes */
640 if (uprobe->offset + MAX_UINSN_BYTES > uprobe->inode->i_size)
641 bytes = uprobe->inode->i_size - uprobe->offset;
642 else
643 bytes = MAX_UINSN_BYTES;
644
645 /* Instruction at the page-boundary; copy bytes in second page */
646 if (nbytes < bytes) {
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +0530647 if (__copy_insn(mapping, vma, uprobe->arch.insn + nbytes,
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530648 bytes - nbytes, uprobe->offset + nbytes))
649 return -ENOMEM;
650
651 bytes = nbytes;
652 }
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +0530653 return __copy_insn(mapping, vma, uprobe->arch.insn, bytes, uprobe->offset);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530654}
655
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530656/*
657 * How mm->uprobes_state.count gets updated
658 * uprobe_mmap() increments the count if
659 * - it successfully adds a breakpoint.
660 * - it cannot add a breakpoint, but sees that there is a underlying
661 * breakpoint (via a is_swbp_at_addr()).
662 *
663 * uprobe_munmap() decrements the count if
664 * - it sees a underlying breakpoint, (via is_swbp_at_addr)
665 * (Subsequent uprobe_unregister wouldnt find the breakpoint
666 * unless a uprobe_mmap kicks in, since the old vma would be
667 * dropped just after uprobe_munmap.)
668 *
669 * uprobe_register increments the count if:
670 * - it successfully adds a breakpoint.
671 *
672 * uprobe_unregister decrements the count if:
673 * - it sees a underlying breakpoint and removes successfully.
674 * (via is_swbp_at_addr)
675 * (Subsequent uprobe_munmap wouldnt find the breakpoint
676 * since there is no underlying breakpoint after the
677 * breakpoint removal.)
678 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530679static int
680install_breakpoint(struct uprobe *uprobe, struct mm_struct *mm,
681 struct vm_area_struct *vma, loff_t vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530682{
683 unsigned long addr;
684 int ret;
685
686 /*
687 * If probe is being deleted, unregister thread could be done with
688 * the vma-rmap-walk through. Adding a probe now can be fatal since
689 * nobody will be able to cleanup. Also we could be from fork or
690 * mremap path, where the probe might have already been inserted.
691 * Hence behave as if probe already existed.
692 */
693 if (!uprobe->consumers)
694 return -EEXIST;
695
696 addr = (unsigned long)vaddr;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100697
Srikar Dronamraju900771a2012-03-12 14:55:14 +0530698 if (!(uprobe->flags & UPROBE_COPY_INSN)) {
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530699 ret = copy_insn(uprobe, vma, addr);
700 if (ret)
701 return ret;
702
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530703 if (is_swbp_insn((uprobe_opcode_t *)uprobe->arch.insn))
Oleg Nesterovc1914a02012-06-15 17:43:31 +0200704 return -ENOTSUPP;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530705
Ananth N Mavinakayanahalli7eb9ba52012-06-08 15:02:57 +0530706 ret = arch_uprobe_analyze_insn(&uprobe->arch, mm, addr);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530707 if (ret)
708 return ret;
709
Srikar Dronamraju900771a2012-03-12 14:55:14 +0530710 uprobe->flags |= UPROBE_COPY_INSN;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530711 }
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530712
713 /*
714 * Ideally, should be updating the probe count after the breakpoint
715 * has been successfully inserted. However a thread could hit the
716 * breakpoint we just inserted even before the probe count is
717 * incremented. If this is the first breakpoint placed, breakpoint
718 * notifier might ignore uprobes and pass the trap to the thread.
719 * Hence increment before and decrement on failure.
720 */
721 atomic_inc(&mm->uprobes_state.count);
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530722 ret = set_swbp(&uprobe->arch, mm, addr);
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530723 if (ret)
724 atomic_dec(&mm->uprobes_state.count);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530725
726 return ret;
727}
728
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530729static void
730remove_breakpoint(struct uprobe *uprobe, struct mm_struct *mm, loff_t vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530731{
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530732 if (!set_orig_insn(&uprobe->arch, mm, (unsigned long)vaddr, true))
733 atomic_dec(&mm->uprobes_state.count);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530734}
735
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530736/*
Oleg Nesterov778b0322012-05-29 21:30:08 +0200737 * There could be threads that have already hit the breakpoint. They
738 * will recheck the current insn and restart if find_uprobe() fails.
739 * See find_active_uprobe().
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530740 */
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530741static void delete_uprobe(struct uprobe *uprobe)
742{
743 unsigned long flags;
744
745 spin_lock_irqsave(&uprobes_treelock, flags);
746 rb_erase(&uprobe->rb_node, &uprobes_tree);
747 spin_unlock_irqrestore(&uprobes_treelock, flags);
748 iput(uprobe->inode);
749 put_uprobe(uprobe);
750 atomic_dec(&uprobe_events);
751}
752
Oleg Nesterov26872092012-06-15 17:43:33 +0200753struct map_info {
754 struct map_info *next;
755 struct mm_struct *mm;
756 loff_t vaddr;
757};
758
759static inline struct map_info *free_map_info(struct map_info *info)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530760{
Oleg Nesterov26872092012-06-15 17:43:33 +0200761 struct map_info *next = info->next;
762 kfree(info);
763 return next;
764}
765
766static struct map_info *
767build_map_info(struct address_space *mapping, loff_t offset, bool is_register)
768{
769 unsigned long pgoff = offset >> PAGE_SHIFT;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530770 struct prio_tree_iter iter;
771 struct vm_area_struct *vma;
Oleg Nesterov26872092012-06-15 17:43:33 +0200772 struct map_info *curr = NULL;
773 struct map_info *prev = NULL;
774 struct map_info *info;
775 int more = 0;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100776
Oleg Nesterov26872092012-06-15 17:43:33 +0200777 again:
778 mutex_lock(&mapping->i_mmap_mutex);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530779 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
780 if (!valid_vma(vma, is_register))
781 continue;
782
Oleg Nesterov7a5bfb62012-06-15 17:43:36 +0200783 if (!prev && !more) {
784 /*
785 * Needs GFP_NOWAIT to avoid i_mmap_mutex recursion through
786 * reclaim. This is optimistic, no harm done if it fails.
787 */
788 prev = kmalloc(sizeof(struct map_info),
789 GFP_NOWAIT | __GFP_NOMEMALLOC | __GFP_NOWARN);
790 if (prev)
791 prev->next = NULL;
792 }
Oleg Nesterov26872092012-06-15 17:43:33 +0200793 if (!prev) {
794 more++;
795 continue;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530796 }
797
Oleg Nesterov26872092012-06-15 17:43:33 +0200798 if (!atomic_inc_not_zero(&vma->vm_mm->mm_users))
799 continue;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100800
Oleg Nesterov26872092012-06-15 17:43:33 +0200801 info = prev;
802 prev = prev->next;
803 info->next = curr;
804 curr = info;
805
806 info->mm = vma->vm_mm;
807 info->vaddr = vma_address(vma, offset);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530808 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530809 mutex_unlock(&mapping->i_mmap_mutex);
810
Oleg Nesterov26872092012-06-15 17:43:33 +0200811 if (!more)
812 goto out;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100813
Oleg Nesterov26872092012-06-15 17:43:33 +0200814 prev = curr;
815 while (curr) {
816 mmput(curr->mm);
817 curr = curr->next;
818 }
819
820 do {
821 info = kmalloc(sizeof(struct map_info), GFP_KERNEL);
822 if (!info) {
823 curr = ERR_PTR(-ENOMEM);
824 goto out;
825 }
826 info->next = prev;
827 prev = info;
828 } while (--more);
829
830 goto again;
831 out:
832 while (prev)
833 prev = free_map_info(prev);
834 return curr;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530835}
836
837static int register_for_each_vma(struct uprobe *uprobe, bool is_register)
838{
Oleg Nesterov26872092012-06-15 17:43:33 +0200839 struct map_info *info;
840 int err = 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530841
Oleg Nesterov26872092012-06-15 17:43:33 +0200842 info = build_map_info(uprobe->inode->i_mapping,
843 uprobe->offset, is_register);
844 if (IS_ERR(info))
845 return PTR_ERR(info);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100846
Oleg Nesterov26872092012-06-15 17:43:33 +0200847 while (info) {
848 struct mm_struct *mm = info->mm;
849 struct vm_area_struct *vma;
850 loff_t vaddr;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100851
Oleg Nesterov26872092012-06-15 17:43:33 +0200852 if (err)
853 goto free;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100854
Oleg Nesterov77fc4af2012-05-29 21:29:28 +0200855 down_write(&mm->mmap_sem);
Oleg Nesterov26872092012-06-15 17:43:33 +0200856 vma = find_vma(mm, (unsigned long)info->vaddr);
857 if (!vma || !valid_vma(vma, is_register))
858 goto unlock;
859
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530860 vaddr = vma_address(vma, uprobe->offset);
861 if (vma->vm_file->f_mapping->host != uprobe->inode ||
Oleg Nesterov26872092012-06-15 17:43:33 +0200862 vaddr != info->vaddr)
863 goto unlock;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530864
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530865 if (is_register) {
Oleg Nesterov26872092012-06-15 17:43:33 +0200866 err = install_breakpoint(uprobe, mm, vma, info->vaddr);
Peter Zijlstrac5784de2012-06-15 17:43:39 +0200867 /*
868 * We can race against uprobe_mmap(), see the
869 * comment near uprobe_hash().
870 */
Oleg Nesterov26872092012-06-15 17:43:33 +0200871 if (err == -EEXIST)
872 err = 0;
873 } else {
874 remove_breakpoint(uprobe, mm, info->vaddr);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530875 }
Oleg Nesterov26872092012-06-15 17:43:33 +0200876 unlock:
877 up_write(&mm->mmap_sem);
878 free:
879 mmput(mm);
880 info = free_map_info(info);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530881 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100882
Oleg Nesterov26872092012-06-15 17:43:33 +0200883 return err;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530884}
885
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100886static int __uprobe_register(struct uprobe *uprobe)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530887{
888 return register_for_each_vma(uprobe, true);
889}
890
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100891static void __uprobe_unregister(struct uprobe *uprobe)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530892{
893 if (!register_for_each_vma(uprobe, false))
894 delete_uprobe(uprobe);
895
896 /* TODO : cant unregister? schedule a worker thread */
897}
898
899/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100900 * uprobe_register - register a probe
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530901 * @inode: the file in which the probe has to be placed.
902 * @offset: offset from the start of the file.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530903 * @uc: information on howto handle the probe..
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530904 *
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100905 * Apart from the access refcount, uprobe_register() takes a creation
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530906 * refcount (thro alloc_uprobe) if and only if this @uprobe is getting
907 * inserted into the rbtree (i.e first consumer for a @inode:@offset
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100908 * tuple). Creation refcount stops uprobe_unregister from freeing the
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530909 * @uprobe even before the register operation is complete. Creation
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530910 * refcount is released when the last @uc for the @uprobe
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530911 * unregisters.
912 *
913 * Return errno if it cannot successully install probes
914 * else return 0 (success)
915 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530916int uprobe_register(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530917{
918 struct uprobe *uprobe;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100919 int ret;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530920
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530921 if (!inode || !uc || uc->next)
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100922 return -EINVAL;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530923
924 if (offset > i_size_read(inode))
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100925 return -EINVAL;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530926
927 ret = 0;
928 mutex_lock(uprobes_hash(inode));
929 uprobe = alloc_uprobe(inode, offset);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100930
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530931 if (uprobe && !consumer_add(uprobe, uc)) {
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100932 ret = __uprobe_register(uprobe);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530933 if (ret) {
934 uprobe->consumers = NULL;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100935 __uprobe_unregister(uprobe);
936 } else {
Srikar Dronamraju900771a2012-03-12 14:55:14 +0530937 uprobe->flags |= UPROBE_RUN_HANDLER;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100938 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530939 }
940
941 mutex_unlock(uprobes_hash(inode));
942 put_uprobe(uprobe);
943
944 return ret;
945}
946
947/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100948 * uprobe_unregister - unregister a already registered probe.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530949 * @inode: the file in which the probe has to be removed.
950 * @offset: offset from the start of the file.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530951 * @uc: identify which probe if multiple probes are colocated.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530952 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530953void uprobe_unregister(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530954{
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100955 struct uprobe *uprobe;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530956
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530957 if (!inode || !uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530958 return;
959
960 uprobe = find_uprobe(inode, offset);
961 if (!uprobe)
962 return;
963
964 mutex_lock(uprobes_hash(inode));
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530965
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530966 if (consumer_del(uprobe, uc)) {
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100967 if (!uprobe->consumers) {
968 __uprobe_unregister(uprobe);
Srikar Dronamraju900771a2012-03-12 14:55:14 +0530969 uprobe->flags &= ~UPROBE_RUN_HANDLER;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100970 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530971 }
972
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530973 mutex_unlock(uprobes_hash(inode));
974 if (uprobe)
975 put_uprobe(uprobe);
976}
977
978/*
979 * Of all the nodes that correspond to the given inode, return the node
980 * with the least offset.
981 */
982static struct rb_node *find_least_offset_node(struct inode *inode)
983{
984 struct uprobe u = { .inode = inode, .offset = 0};
985 struct rb_node *n = uprobes_tree.rb_node;
986 struct rb_node *close_node = NULL;
987 struct uprobe *uprobe;
988 int match;
989
990 while (n) {
991 uprobe = rb_entry(n, struct uprobe, rb_node);
992 match = match_uprobe(&u, uprobe);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100993
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530994 if (uprobe->inode == inode)
995 close_node = n;
996
997 if (!match)
998 return close_node;
999
1000 if (match < 0)
1001 n = n->rb_left;
1002 else
1003 n = n->rb_right;
1004 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001005
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301006 return close_node;
1007}
1008
1009/*
1010 * For a given inode, build a list of probes that need to be inserted.
1011 */
1012static void build_probe_list(struct inode *inode, struct list_head *head)
1013{
1014 struct uprobe *uprobe;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301015 unsigned long flags;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001016 struct rb_node *n;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301017
1018 spin_lock_irqsave(&uprobes_treelock, flags);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001019
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301020 n = find_least_offset_node(inode);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001021
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301022 for (; n; n = rb_next(n)) {
1023 uprobe = rb_entry(n, struct uprobe, rb_node);
1024 if (uprobe->inode != inode)
1025 break;
1026
1027 list_add(&uprobe->pending_list, head);
1028 atomic_inc(&uprobe->ref);
1029 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001030
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301031 spin_unlock_irqrestore(&uprobes_treelock, flags);
1032}
1033
1034/*
1035 * Called from mmap_region.
1036 * called with mm->mmap_sem acquired.
1037 *
1038 * Return -ve no if we fail to insert probes and we cannot
1039 * bail-out.
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001040 * Return 0 otherwise. i.e:
1041 *
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301042 * - successful insertion of probes
1043 * - (or) no possible probes to be inserted.
1044 * - (or) insertion of probes failed but we can bail-out.
1045 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001046int uprobe_mmap(struct vm_area_struct *vma)
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301047{
1048 struct list_head tmp_list;
1049 struct uprobe *uprobe, *u;
1050 struct inode *inode;
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301051 int ret, count;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301052
1053 if (!atomic_read(&uprobe_events) || !valid_vma(vma, true))
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001054 return 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301055
1056 inode = vma->vm_file->f_mapping->host;
1057 if (!inode)
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001058 return 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301059
1060 INIT_LIST_HEAD(&tmp_list);
1061 mutex_lock(uprobes_mmap_hash(inode));
1062 build_probe_list(inode, &tmp_list);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001063
1064 ret = 0;
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301065 count = 0;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001066
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301067 list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
1068 loff_t vaddr;
1069
1070 list_del(&uprobe->pending_list);
1071 if (!ret) {
1072 vaddr = vma_address(vma, uprobe->offset);
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301073
1074 if (vaddr < vma->vm_start || vaddr >= vma->vm_end) {
1075 put_uprobe(uprobe);
1076 continue;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301077 }
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301078
1079 ret = install_breakpoint(uprobe, vma->vm_mm, vma, vaddr);
Peter Zijlstrac5784de2012-06-15 17:43:39 +02001080 /*
1081 * We can race against uprobe_register(), see the
1082 * comment near uprobe_hash().
1083 */
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301084 if (ret == -EEXIST) {
1085 ret = 0;
1086
1087 if (!is_swbp_at_addr(vma->vm_mm, vaddr))
1088 continue;
1089
1090 /*
1091 * Unable to insert a breakpoint, but
1092 * breakpoint lies underneath. Increment the
1093 * probe count.
1094 */
1095 atomic_inc(&vma->vm_mm->uprobes_state.count);
1096 }
1097
1098 if (!ret)
1099 count++;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301100 }
1101 put_uprobe(uprobe);
1102 }
1103
1104 mutex_unlock(uprobes_mmap_hash(inode));
1105
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301106 if (ret)
1107 atomic_sub(count, &vma->vm_mm->uprobes_state.count);
1108
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301109 return ret;
1110}
1111
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301112/*
1113 * Called in context of a munmap of a vma.
1114 */
Srikar Dronamrajucbc91f72012-04-11 16:05:27 +05301115void uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end)
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301116{
1117 struct list_head tmp_list;
1118 struct uprobe *uprobe, *u;
1119 struct inode *inode;
1120
1121 if (!atomic_read(&uprobe_events) || !valid_vma(vma, false))
1122 return;
1123
1124 if (!atomic_read(&vma->vm_mm->uprobes_state.count))
1125 return;
1126
1127 inode = vma->vm_file->f_mapping->host;
1128 if (!inode)
1129 return;
1130
1131 INIT_LIST_HEAD(&tmp_list);
1132 mutex_lock(uprobes_mmap_hash(inode));
1133 build_probe_list(inode, &tmp_list);
1134
1135 list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
1136 loff_t vaddr;
1137
1138 list_del(&uprobe->pending_list);
1139 vaddr = vma_address(vma, uprobe->offset);
1140
Srikar Dronamrajucbc91f72012-04-11 16:05:27 +05301141 if (vaddr >= start && vaddr < end) {
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301142 /*
1143 * An unregister could have removed the probe before
1144 * unmap. So check before we decrement the count.
1145 */
1146 if (is_swbp_at_addr(vma->vm_mm, vaddr) == 1)
1147 atomic_dec(&vma->vm_mm->uprobes_state.count);
1148 }
1149 put_uprobe(uprobe);
1150 }
1151 mutex_unlock(uprobes_mmap_hash(inode));
1152}
1153
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301154/* Slot allocation for XOL */
1155static int xol_add_vma(struct xol_area *area)
1156{
1157 struct mm_struct *mm;
1158 int ret;
1159
1160 area->page = alloc_page(GFP_HIGHUSER);
1161 if (!area->page)
1162 return -ENOMEM;
1163
1164 ret = -EALREADY;
1165 mm = current->mm;
1166
1167 down_write(&mm->mmap_sem);
1168 if (mm->uprobes_state.xol_area)
1169 goto fail;
1170
1171 ret = -ENOMEM;
1172
1173 /* Try to map as high as possible, this is only a hint. */
1174 area->vaddr = get_unmapped_area(NULL, TASK_SIZE - PAGE_SIZE, PAGE_SIZE, 0, 0);
1175 if (area->vaddr & ~PAGE_MASK) {
1176 ret = area->vaddr;
1177 goto fail;
1178 }
1179
1180 ret = install_special_mapping(mm, area->vaddr, PAGE_SIZE,
1181 VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO, &area->page);
1182 if (ret)
1183 goto fail;
1184
1185 smp_wmb(); /* pairs with get_xol_area() */
1186 mm->uprobes_state.xol_area = area;
1187 ret = 0;
1188
1189fail:
1190 up_write(&mm->mmap_sem);
1191 if (ret)
1192 __free_page(area->page);
1193
1194 return ret;
1195}
1196
1197static struct xol_area *get_xol_area(struct mm_struct *mm)
1198{
1199 struct xol_area *area;
1200
1201 area = mm->uprobes_state.xol_area;
1202 smp_read_barrier_depends(); /* pairs with wmb in xol_add_vma() */
1203
1204 return area;
1205}
1206
1207/*
1208 * xol_alloc_area - Allocate process's xol_area.
1209 * This area will be used for storing instructions for execution out of
1210 * line.
1211 *
1212 * Returns the allocated area or NULL.
1213 */
1214static struct xol_area *xol_alloc_area(void)
1215{
1216 struct xol_area *area;
1217
1218 area = kzalloc(sizeof(*area), GFP_KERNEL);
1219 if (unlikely(!area))
1220 return NULL;
1221
1222 area->bitmap = kzalloc(BITS_TO_LONGS(UINSNS_PER_PAGE) * sizeof(long), GFP_KERNEL);
1223
1224 if (!area->bitmap)
1225 goto fail;
1226
1227 init_waitqueue_head(&area->wq);
1228 if (!xol_add_vma(area))
1229 return area;
1230
1231fail:
1232 kfree(area->bitmap);
1233 kfree(area);
1234
1235 return get_xol_area(current->mm);
1236}
1237
1238/*
1239 * uprobe_clear_state - Free the area allocated for slots.
1240 */
1241void uprobe_clear_state(struct mm_struct *mm)
1242{
1243 struct xol_area *area = mm->uprobes_state.xol_area;
1244
1245 if (!area)
1246 return;
1247
1248 put_page(area->page);
1249 kfree(area->bitmap);
1250 kfree(area);
1251}
1252
1253/*
1254 * uprobe_reset_state - Free the area allocated for slots.
1255 */
1256void uprobe_reset_state(struct mm_struct *mm)
1257{
1258 mm->uprobes_state.xol_area = NULL;
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301259 atomic_set(&mm->uprobes_state.count, 0);
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301260}
1261
1262/*
1263 * - search for a free slot.
1264 */
1265static unsigned long xol_take_insn_slot(struct xol_area *area)
1266{
1267 unsigned long slot_addr;
1268 int slot_nr;
1269
1270 do {
1271 slot_nr = find_first_zero_bit(area->bitmap, UINSNS_PER_PAGE);
1272 if (slot_nr < UINSNS_PER_PAGE) {
1273 if (!test_and_set_bit(slot_nr, area->bitmap))
1274 break;
1275
1276 slot_nr = UINSNS_PER_PAGE;
1277 continue;
1278 }
1279 wait_event(area->wq, (atomic_read(&area->slot_count) < UINSNS_PER_PAGE));
1280 } while (slot_nr >= UINSNS_PER_PAGE);
1281
1282 slot_addr = area->vaddr + (slot_nr * UPROBE_XOL_SLOT_BYTES);
1283 atomic_inc(&area->slot_count);
1284
1285 return slot_addr;
1286}
1287
1288/*
1289 * xol_get_insn_slot - If was not allocated a slot, then
1290 * allocate a slot.
1291 * Returns the allocated slot address or 0.
1292 */
1293static unsigned long xol_get_insn_slot(struct uprobe *uprobe, unsigned long slot_addr)
1294{
1295 struct xol_area *area;
1296 unsigned long offset;
1297 void *vaddr;
1298
1299 area = get_xol_area(current->mm);
1300 if (!area) {
1301 area = xol_alloc_area();
1302 if (!area)
1303 return 0;
1304 }
1305 current->utask->xol_vaddr = xol_take_insn_slot(area);
1306
1307 /*
1308 * Initialize the slot if xol_vaddr points to valid
1309 * instruction slot.
1310 */
1311 if (unlikely(!current->utask->xol_vaddr))
1312 return 0;
1313
1314 current->utask->vaddr = slot_addr;
1315 offset = current->utask->xol_vaddr & ~PAGE_MASK;
1316 vaddr = kmap_atomic(area->page);
1317 memcpy(vaddr + offset, uprobe->arch.insn, MAX_UINSN_BYTES);
1318 kunmap_atomic(vaddr);
1319
1320 return current->utask->xol_vaddr;
1321}
1322
1323/*
1324 * xol_free_insn_slot - If slot was earlier allocated by
1325 * @xol_get_insn_slot(), make the slot available for
1326 * subsequent requests.
1327 */
1328static void xol_free_insn_slot(struct task_struct *tsk)
1329{
1330 struct xol_area *area;
1331 unsigned long vma_end;
1332 unsigned long slot_addr;
1333
1334 if (!tsk->mm || !tsk->mm->uprobes_state.xol_area || !tsk->utask)
1335 return;
1336
1337 slot_addr = tsk->utask->xol_vaddr;
1338
1339 if (unlikely(!slot_addr || IS_ERR_VALUE(slot_addr)))
1340 return;
1341
1342 area = tsk->mm->uprobes_state.xol_area;
1343 vma_end = area->vaddr + PAGE_SIZE;
1344 if (area->vaddr <= slot_addr && slot_addr < vma_end) {
1345 unsigned long offset;
1346 int slot_nr;
1347
1348 offset = slot_addr - area->vaddr;
1349 slot_nr = offset / UPROBE_XOL_SLOT_BYTES;
1350 if (slot_nr >= UINSNS_PER_PAGE)
1351 return;
1352
1353 clear_bit(slot_nr, area->bitmap);
1354 atomic_dec(&area->slot_count);
1355 if (waitqueue_active(&area->wq))
1356 wake_up(&area->wq);
1357
1358 tsk->utask->xol_vaddr = 0;
1359 }
1360}
1361
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301362/**
1363 * uprobe_get_swbp_addr - compute address of swbp given post-swbp regs
1364 * @regs: Reflects the saved state of the task after it has hit a breakpoint
1365 * instruction.
1366 * Return the address of the breakpoint instruction.
1367 */
1368unsigned long __weak uprobe_get_swbp_addr(struct pt_regs *regs)
1369{
1370 return instruction_pointer(regs) - UPROBE_SWBP_INSN_SIZE;
1371}
1372
1373/*
1374 * Called with no locks held.
1375 * Called in context of a exiting or a exec-ing thread.
1376 */
1377void uprobe_free_utask(struct task_struct *t)
1378{
1379 struct uprobe_task *utask = t->utask;
1380
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301381 if (!utask)
1382 return;
1383
1384 if (utask->active_uprobe)
1385 put_uprobe(utask->active_uprobe);
1386
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301387 xol_free_insn_slot(t);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301388 kfree(utask);
1389 t->utask = NULL;
1390}
1391
1392/*
1393 * Called in context of a new clone/fork from copy_process.
1394 */
1395void uprobe_copy_process(struct task_struct *t)
1396{
1397 t->utask = NULL;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301398}
1399
1400/*
1401 * Allocate a uprobe_task object for the task.
1402 * Called when the thread hits a breakpoint for the first time.
1403 *
1404 * Returns:
1405 * - pointer to new uprobe_task on success
1406 * - NULL otherwise
1407 */
1408static struct uprobe_task *add_utask(void)
1409{
1410 struct uprobe_task *utask;
1411
1412 utask = kzalloc(sizeof *utask, GFP_KERNEL);
1413 if (unlikely(!utask))
1414 return NULL;
1415
1416 utask->active_uprobe = NULL;
1417 current->utask = utask;
1418 return utask;
1419}
1420
1421/* Prepare to single-step probed instruction out of line. */
1422static int
1423pre_ssout(struct uprobe *uprobe, struct pt_regs *regs, unsigned long vaddr)
1424{
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301425 if (xol_get_insn_slot(uprobe, vaddr) && !arch_uprobe_pre_xol(&uprobe->arch, regs))
1426 return 0;
1427
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301428 return -EFAULT;
1429}
1430
1431/*
1432 * If we are singlestepping, then ensure this thread is not connected to
1433 * non-fatal signals until completion of singlestep. When xol insn itself
1434 * triggers the signal, restart the original insn even if the task is
1435 * already SIGKILL'ed (since coredump should report the correct ip). This
1436 * is even more important if the task has a handler for SIGSEGV/etc, The
1437 * _same_ instruction should be repeated again after return from the signal
1438 * handler, and SSTEP can never finish in this case.
1439 */
1440bool uprobe_deny_signal(void)
1441{
1442 struct task_struct *t = current;
1443 struct uprobe_task *utask = t->utask;
1444
1445 if (likely(!utask || !utask->active_uprobe))
1446 return false;
1447
1448 WARN_ON_ONCE(utask->state != UTASK_SSTEP);
1449
1450 if (signal_pending(t)) {
1451 spin_lock_irq(&t->sighand->siglock);
1452 clear_tsk_thread_flag(t, TIF_SIGPENDING);
1453 spin_unlock_irq(&t->sighand->siglock);
1454
1455 if (__fatal_signal_pending(t) || arch_uprobe_xol_was_trapped(t)) {
1456 utask->state = UTASK_SSTEP_TRAPPED;
1457 set_tsk_thread_flag(t, TIF_UPROBE);
1458 set_tsk_thread_flag(t, TIF_NOTIFY_RESUME);
1459 }
1460 }
1461
1462 return true;
1463}
1464
1465/*
1466 * Avoid singlestepping the original instruction if the original instruction
1467 * is a NOP or can be emulated.
1468 */
1469static bool can_skip_sstep(struct uprobe *uprobe, struct pt_regs *regs)
1470{
1471 if (arch_uprobe_skip_sstep(&uprobe->arch, regs))
1472 return true;
1473
1474 uprobe->flags &= ~UPROBE_SKIP_SSTEP;
1475 return false;
1476}
1477
Oleg Nesterovd790d342012-05-29 21:29:14 +02001478static struct uprobe *find_active_uprobe(unsigned long bp_vaddr, int *is_swbp)
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001479{
1480 struct mm_struct *mm = current->mm;
1481 struct uprobe *uprobe = NULL;
1482 struct vm_area_struct *vma;
1483
1484 down_read(&mm->mmap_sem);
1485 vma = find_vma(mm, bp_vaddr);
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001486 if (vma && vma->vm_start <= bp_vaddr) {
1487 if (valid_vma(vma, false)) {
1488 struct inode *inode;
1489 loff_t offset;
1490
1491 inode = vma->vm_file->f_mapping->host;
1492 offset = bp_vaddr - vma->vm_start;
1493 offset += (vma->vm_pgoff << PAGE_SHIFT);
1494 uprobe = find_uprobe(inode, offset);
1495 }
Oleg Nesterovd790d342012-05-29 21:29:14 +02001496
1497 if (!uprobe)
1498 *is_swbp = is_swbp_at_addr(mm, bp_vaddr);
1499 } else {
1500 *is_swbp = -EFAULT;
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001501 }
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001502 up_read(&mm->mmap_sem);
1503
1504 return uprobe;
1505}
1506
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301507/*
1508 * Run handler and ask thread to singlestep.
1509 * Ensure all non-fatal signals cannot interrupt thread while it singlesteps.
1510 */
1511static void handle_swbp(struct pt_regs *regs)
1512{
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301513 struct uprobe_task *utask;
1514 struct uprobe *uprobe;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301515 unsigned long bp_vaddr;
Oleg Nesterov56bb4cf2012-05-29 21:29:47 +02001516 int uninitialized_var(is_swbp);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301517
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301518 bp_vaddr = uprobe_get_swbp_addr(regs);
Oleg Nesterovd790d342012-05-29 21:29:14 +02001519 uprobe = find_active_uprobe(bp_vaddr, &is_swbp);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301520
1521 if (!uprobe) {
Oleg Nesterov56bb4cf2012-05-29 21:29:47 +02001522 if (is_swbp > 0) {
1523 /* No matching uprobe; signal SIGTRAP. */
1524 send_sig(SIGTRAP, current, 0);
1525 } else {
1526 /*
1527 * Either we raced with uprobe_unregister() or we can't
1528 * access this memory. The latter is only possible if
1529 * another thread plays with our ->mm. In both cases
1530 * we can simply restart. If this vma was unmapped we
1531 * can pretend this insn was not executed yet and get
1532 * the (correct) SIGSEGV after restart.
1533 */
1534 instruction_pointer_set(regs, bp_vaddr);
1535 }
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301536 return;
1537 }
1538
1539 utask = current->utask;
1540 if (!utask) {
1541 utask = add_utask();
1542 /* Cannot allocate; re-execute the instruction. */
1543 if (!utask)
1544 goto cleanup_ret;
1545 }
1546 utask->active_uprobe = uprobe;
1547 handler_chain(uprobe, regs);
1548 if (uprobe->flags & UPROBE_SKIP_SSTEP && can_skip_sstep(uprobe, regs))
1549 goto cleanup_ret;
1550
1551 utask->state = UTASK_SSTEP;
1552 if (!pre_ssout(uprobe, regs, bp_vaddr)) {
1553 user_enable_single_step(current);
1554 return;
1555 }
1556
1557cleanup_ret:
1558 if (utask) {
1559 utask->active_uprobe = NULL;
1560 utask->state = UTASK_RUNNING;
1561 }
1562 if (uprobe) {
1563 if (!(uprobe->flags & UPROBE_SKIP_SSTEP))
1564
1565 /*
1566 * cannot singlestep; cannot skip instruction;
1567 * re-execute the instruction.
1568 */
1569 instruction_pointer_set(regs, bp_vaddr);
1570
1571 put_uprobe(uprobe);
1572 }
1573}
1574
1575/*
1576 * Perform required fix-ups and disable singlestep.
1577 * Allow pending signals to take effect.
1578 */
1579static void handle_singlestep(struct uprobe_task *utask, struct pt_regs *regs)
1580{
1581 struct uprobe *uprobe;
1582
1583 uprobe = utask->active_uprobe;
1584 if (utask->state == UTASK_SSTEP_ACK)
1585 arch_uprobe_post_xol(&uprobe->arch, regs);
1586 else if (utask->state == UTASK_SSTEP_TRAPPED)
1587 arch_uprobe_abort_xol(&uprobe->arch, regs);
1588 else
1589 WARN_ON_ONCE(1);
1590
1591 put_uprobe(uprobe);
1592 utask->active_uprobe = NULL;
1593 utask->state = UTASK_RUNNING;
1594 user_disable_single_step(current);
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301595 xol_free_insn_slot(current);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301596
1597 spin_lock_irq(&current->sighand->siglock);
1598 recalc_sigpending(); /* see uprobe_deny_signal() */
1599 spin_unlock_irq(&current->sighand->siglock);
1600}
1601
1602/*
1603 * On breakpoint hit, breakpoint notifier sets the TIF_UPROBE flag. (and on
1604 * subsequent probe hits on the thread sets the state to UTASK_BP_HIT) and
1605 * allows the thread to return from interrupt.
1606 *
1607 * On singlestep exception, singlestep notifier sets the TIF_UPROBE flag and
1608 * also sets the state to UTASK_SSTEP_ACK and allows the thread to return from
1609 * interrupt.
1610 *
1611 * While returning to userspace, thread notices the TIF_UPROBE flag and calls
1612 * uprobe_notify_resume().
1613 */
1614void uprobe_notify_resume(struct pt_regs *regs)
1615{
1616 struct uprobe_task *utask;
1617
1618 utask = current->utask;
1619 if (!utask || utask->state == UTASK_BP_HIT)
1620 handle_swbp(regs);
1621 else
1622 handle_singlestep(utask, regs);
1623}
1624
1625/*
1626 * uprobe_pre_sstep_notifier gets called from interrupt context as part of
1627 * notifier mechanism. Set TIF_UPROBE flag and indicate breakpoint hit.
1628 */
1629int uprobe_pre_sstep_notifier(struct pt_regs *regs)
1630{
1631 struct uprobe_task *utask;
1632
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301633 if (!current->mm || !atomic_read(&current->mm->uprobes_state.count))
1634 /* task is currently not uprobed */
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301635 return 0;
1636
1637 utask = current->utask;
1638 if (utask)
1639 utask->state = UTASK_BP_HIT;
1640
1641 set_thread_flag(TIF_UPROBE);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301642
1643 return 1;
1644}
1645
1646/*
1647 * uprobe_post_sstep_notifier gets called in interrupt context as part of notifier
1648 * mechanism. Set TIF_UPROBE flag and indicate completion of singlestep.
1649 */
1650int uprobe_post_sstep_notifier(struct pt_regs *regs)
1651{
1652 struct uprobe_task *utask = current->utask;
1653
1654 if (!current->mm || !utask || !utask->active_uprobe)
1655 /* task is currently not uprobed */
1656 return 0;
1657
1658 utask->state = UTASK_SSTEP_ACK;
1659 set_thread_flag(TIF_UPROBE);
1660 return 1;
1661}
1662
1663static struct notifier_block uprobe_exception_nb = {
1664 .notifier_call = arch_uprobe_exception_notify,
1665 .priority = INT_MAX-1, /* notified after kprobes, kgdb */
1666};
1667
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301668static int __init init_uprobes(void)
1669{
1670 int i;
1671
1672 for (i = 0; i < UPROBES_HASH_SZ; i++) {
1673 mutex_init(&uprobes_mutex[i]);
1674 mutex_init(&uprobes_mmap_mutex[i]);
1675 }
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301676
1677 return register_die_notifier(&uprobe_exception_nb);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301678}
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301679module_init(init_uprobes);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301680
1681static void __exit exit_uprobes(void)
1682{
1683}
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301684module_exit(exit_uprobes);