blob: 4f315fa94c520187bd4649c84bae5d13b8967dc9 [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 */
Oleg Nesterov194f8dc2012-07-29 20:22:49 +020035#include "../../mm/internal.h" /* munlock_vma_page */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010036
Srikar Dronamraju2b144492012-02-09 14:56:42 +053037#include <linux/uprobes.h>
38
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +053039#define UINSNS_PER_PAGE (PAGE_SIZE/UPROBE_XOL_SLOT_BYTES)
40#define MAX_UPROBE_XOL_SLOTS UINSNS_PER_PAGE
41
Srikar Dronamraju2b144492012-02-09 14:56:42 +053042static struct rb_root uprobes_tree = RB_ROOT;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010043
Srikar Dronamraju2b144492012-02-09 14:56:42 +053044static DEFINE_SPINLOCK(uprobes_treelock); /* serialize rbtree access */
45
46#define UPROBES_HASH_SZ 13
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010047
Peter Zijlstrac5784de2012-06-15 17:43:39 +020048/*
49 * We need separate register/unregister and mmap/munmap lock hashes because
50 * of mmap_sem nesting.
51 *
52 * uprobe_register() needs to install probes on (potentially) all processes
53 * and thus needs to acquire multiple mmap_sems (consequtively, not
54 * concurrently), whereas uprobe_mmap() is called while holding mmap_sem
55 * for the particular process doing the mmap.
56 *
57 * uprobe_register()->register_for_each_vma() needs to drop/acquire mmap_sem
58 * because of lock order against i_mmap_mutex. This means there's a hole in
59 * the register vma iteration where a mmap() can happen.
60 *
61 * Thus uprobe_register() can race with uprobe_mmap() and we can try and
62 * install a probe where one is already installed.
63 */
64
Srikar Dronamraju2b144492012-02-09 14:56:42 +053065/* serialize (un)register */
66static struct mutex uprobes_mutex[UPROBES_HASH_SZ];
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010067
68#define uprobes_hash(v) (&uprobes_mutex[((unsigned long)(v)) % UPROBES_HASH_SZ])
Srikar Dronamraju2b144492012-02-09 14:56:42 +053069
70/* serialize uprobe->pending_list */
71static struct mutex uprobes_mmap_mutex[UPROBES_HASH_SZ];
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010072#define uprobes_mmap_hash(v) (&uprobes_mmap_mutex[((unsigned long)(v)) % UPROBES_HASH_SZ])
Srikar Dronamraju2b144492012-02-09 14:56:42 +053073
74/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010075 * uprobe_events allows us to skip the uprobe_mmap if there are no uprobe
Srikar Dronamraju2b144492012-02-09 14:56:42 +053076 * events active at this time. Probably a fine grained per inode count is
77 * better?
78 */
79static atomic_t uprobe_events = ATOMIC_INIT(0);
80
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +020081/* Have a copy of original instruction */
82#define UPROBE_COPY_INSN 0x1
83/* Dont run handlers when first register/ last unregister in progress*/
84#define UPROBE_RUN_HANDLER 0x2
85/* Can skip singlestep */
86#define UPROBE_SKIP_SSTEP 0x4
87
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +053088struct uprobe {
89 struct rb_node rb_node; /* node in the rb tree */
90 atomic_t ref;
91 struct rw_semaphore consumer_rwsem;
92 struct list_head pending_list;
93 struct uprobe_consumer *consumers;
94 struct inode *inode; /* Also hold a ref to inode */
95 loff_t offset;
96 int flags;
97 struct arch_uprobe arch;
98};
99
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530100/*
101 * valid_vma: Verify if the specified vma is an executable vma
102 * Relax restrictions while unregistering: vm_flags might have
103 * changed after breakpoint was inserted.
104 * - is_register: indicates if we are in register context.
105 * - Return 1 if the specified virtual address is in an
106 * executable vma.
107 */
108static bool valid_vma(struct vm_area_struct *vma, bool is_register)
109{
Oleg Nesterove40cfce2012-09-16 19:31:39 +0200110 vm_flags_t flags = VM_HUGETLB | VM_MAYEXEC | VM_SHARED;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530111
Oleg Nesterove40cfce2012-09-16 19:31:39 +0200112 if (is_register)
113 flags |= VM_WRITE;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530114
Oleg Nesterove40cfce2012-09-16 19:31:39 +0200115 return vma->vm_file && (vma->vm_flags & flags) == VM_MAYEXEC;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530116}
117
Oleg Nesterov57683f72012-07-29 20:22:47 +0200118static unsigned long offset_to_vaddr(struct vm_area_struct *vma, loff_t offset)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530119{
Oleg Nesterov57683f72012-07-29 20:22:47 +0200120 return vma->vm_start + offset - ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530121}
122
Oleg Nesterovcb113b42012-07-29 20:22:42 +0200123static loff_t vaddr_to_offset(struct vm_area_struct *vma, unsigned long vaddr)
124{
125 return ((loff_t)vma->vm_pgoff << PAGE_SHIFT) + (vaddr - vma->vm_start);
126}
127
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530128/**
129 * __replace_page - replace page in vma by new page.
130 * based on replace_page in mm/ksm.c
131 *
132 * @vma: vma that holds the pte pointing to page
Oleg Nesterovc517ee72012-07-29 20:22:16 +0200133 * @addr: address the old @page is mapped at
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530134 * @page: the cowed page we are replacing by kpage
135 * @kpage: the modified page we replace page by
136 *
137 * Returns 0 on success, -EFAULT on failure.
138 */
Oleg Nesterovc517ee72012-07-29 20:22:16 +0200139static int __replace_page(struct vm_area_struct *vma, unsigned long addr,
140 struct page *page, struct page *kpage)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530141{
142 struct mm_struct *mm = vma->vm_mm;
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200143 spinlock_t *ptl;
144 pte_t *ptep;
Oleg Nesterov9f924482012-07-29 20:22:20 +0200145 int err;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530146
Oleg Nesterov194f8dc2012-07-29 20:22:49 +0200147 /* For try_to_free_swap() and munlock_vma_page() below */
Oleg Nesterov9f924482012-07-29 20:22:20 +0200148 lock_page(page);
149
150 err = -EAGAIN;
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200151 ptep = page_check_address(page, mm, addr, &ptl, 0);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530152 if (!ptep)
Oleg Nesterov9f924482012-07-29 20:22:20 +0200153 goto unlock;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530154
155 get_page(kpage);
156 page_add_new_anon_rmap(kpage, vma, addr);
157
Srikar Dronamraju7396fa82012-04-11 16:05:16 +0530158 if (!PageAnon(page)) {
159 dec_mm_counter(mm, MM_FILEPAGES);
160 inc_mm_counter(mm, MM_ANONPAGES);
161 }
162
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530163 flush_cache_page(vma, addr, pte_pfn(*ptep));
164 ptep_clear_flush(vma, addr, ptep);
165 set_pte_at_notify(mm, addr, ptep, mk_pte(kpage, vma->vm_page_prot));
166
167 page_remove_rmap(page);
168 if (!page_mapped(page))
169 try_to_free_swap(page);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530170 pte_unmap_unlock(ptep, ptl);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530171
Oleg Nesterov194f8dc2012-07-29 20:22:49 +0200172 if (vma->vm_flags & VM_LOCKED)
173 munlock_vma_page(page);
174 put_page(page);
175
Oleg Nesterov9f924482012-07-29 20:22:20 +0200176 err = 0;
177 unlock:
178 unlock_page(page);
179 return err;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530180}
181
182/**
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530183 * is_swbp_insn - check if instruction is breakpoint instruction.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530184 * @insn: instruction to be checked.
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530185 * Default implementation of is_swbp_insn
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530186 * Returns true if @insn is a breakpoint instruction.
187 */
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530188bool __weak is_swbp_insn(uprobe_opcode_t *insn)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530189{
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530190 return *insn == UPROBE_SWBP_INSN;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530191}
192
Oleg Nesterovcceb55a2012-09-23 21:10:18 +0200193static void copy_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t *opcode)
194{
195 void *kaddr = kmap_atomic(page);
196 memcpy(opcode, kaddr + (vaddr & ~PAGE_MASK), UPROBE_SWBP_INSN_SIZE);
197 kunmap_atomic(kaddr);
198}
199
Oleg Nesteroved6f6a52012-09-23 21:30:44 +0200200static int verify_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t *new_opcode)
201{
202 uprobe_opcode_t old_opcode;
203 bool is_swbp;
204
205 copy_opcode(page, vaddr, &old_opcode);
206 is_swbp = is_swbp_insn(&old_opcode);
207
208 if (is_swbp_insn(new_opcode)) {
209 if (is_swbp) /* register: already installed? */
210 return 0;
211 } else {
212 if (!is_swbp) /* unregister: was it changed by us? */
Oleg Nesterov076a3652012-09-30 18:54:53 +0200213 return 0;
Oleg Nesteroved6f6a52012-09-23 21:30:44 +0200214 }
215
216 return 1;
217}
218
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530219/*
220 * NOTE:
221 * Expect the breakpoint instruction to be the smallest size instruction for
222 * the architecture. If an arch has variable length instruction and the
223 * breakpoint instruction is not of the smallest length instruction
Oleg Nesterovcceb55a2012-09-23 21:10:18 +0200224 * supported by that architecture then we need to modify is_swbp_at_addr and
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530225 * write_opcode accordingly. This would never be a problem for archs that
226 * have fixed length instructions.
227 */
228
229/*
230 * write_opcode - write the opcode at a given virtual address.
231 * @mm: the probed process address space.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530232 * @vaddr: the virtual address to store the opcode.
233 * @opcode: opcode to be written at @vaddr.
234 *
235 * Called with mm->mmap_sem held (for read and with a reference to
236 * mm).
237 *
238 * For mm @mm, write the opcode at @vaddr.
239 * Return 0 (success) or a negative errno.
240 */
Oleg Nesterovcceb55a2012-09-23 21:10:18 +0200241static int write_opcode(struct mm_struct *mm, unsigned long vaddr,
242 uprobe_opcode_t opcode)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530243{
244 struct page *old_page, *new_page;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530245 void *vaddr_old, *vaddr_new;
246 struct vm_area_struct *vma;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530247 int ret;
Oleg Nesterovf4030722012-07-29 20:22:12 +0200248
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200249retry:
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530250 /* Read the page with vaddr into memory */
Oleg Nesterov75ed82e2012-09-16 17:20:06 +0200251 ret = get_user_pages(NULL, mm, vaddr, 1, 0, 1, &old_page, &vma);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530252 if (ret <= 0)
253 return ret;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100254
Oleg Nesteroved6f6a52012-09-23 21:30:44 +0200255 ret = verify_opcode(old_page, vaddr, &opcode);
256 if (ret <= 0)
257 goto put_old;
258
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530259 ret = -ENOMEM;
260 new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vaddr);
261 if (!new_page)
Oleg Nesterov9f924482012-07-29 20:22:20 +0200262 goto put_old;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530263
264 __SetPageUptodate(new_page);
265
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530266 /* copy the page now that we've got it stable */
267 vaddr_old = kmap_atomic(old_page);
268 vaddr_new = kmap_atomic(new_page);
269
270 memcpy(vaddr_new, vaddr_old, PAGE_SIZE);
Oleg Nesterovd9c4a302012-06-15 17:43:50 +0200271 memcpy(vaddr_new + (vaddr & ~PAGE_MASK), &opcode, UPROBE_SWBP_INSN_SIZE);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530272
273 kunmap_atomic(vaddr_new);
274 kunmap_atomic(vaddr_old);
275
276 ret = anon_vma_prepare(vma);
277 if (ret)
Oleg Nesterov9f924482012-07-29 20:22:20 +0200278 goto put_new;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530279
Oleg Nesterovc517ee72012-07-29 20:22:16 +0200280 ret = __replace_page(vma, vaddr, old_page, new_page);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530281
Oleg Nesterov9f924482012-07-29 20:22:20 +0200282put_new:
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530283 page_cache_release(new_page);
Oleg Nesterov9f924482012-07-29 20:22:20 +0200284put_old:
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100285 put_page(old_page);
286
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200287 if (unlikely(ret == -EAGAIN))
288 goto retry;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530289 return ret;
290}
291
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530292/**
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530293 * set_swbp - store breakpoint at a given address.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530294 * @auprobe: arch specific probepoint information.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530295 * @mm: the probed process address space.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530296 * @vaddr: the virtual address to insert the opcode.
297 *
298 * For mm @mm, store the breakpoint instruction at @vaddr.
299 * Return 0 (success) or a negative errno.
300 */
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530301int __weak set_swbp(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530302{
Oleg Nesterovcceb55a2012-09-23 21:10:18 +0200303 return write_opcode(mm, vaddr, UPROBE_SWBP_INSN);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530304}
305
306/**
307 * set_orig_insn - Restore the original instruction.
308 * @mm: the probed process address space.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530309 * @auprobe: arch specific probepoint information.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530310 * @vaddr: the virtual address to insert the opcode.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530311 *
312 * For mm @mm, restore the original opcode (opcode) at @vaddr.
313 * Return 0 (success) or a negative errno.
314 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100315int __weak
Oleg Nesterovded86e72012-08-08 18:07:03 +0200316set_orig_insn(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530317{
Oleg Nesterovcceb55a2012-09-23 21:10:18 +0200318 return write_opcode(mm, vaddr, *(uprobe_opcode_t *)auprobe->insn);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530319}
320
321static int match_uprobe(struct uprobe *l, struct uprobe *r)
322{
323 if (l->inode < r->inode)
324 return -1;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100325
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530326 if (l->inode > r->inode)
327 return 1;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530328
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100329 if (l->offset < r->offset)
330 return -1;
331
332 if (l->offset > r->offset)
333 return 1;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530334
335 return 0;
336}
337
338static struct uprobe *__find_uprobe(struct inode *inode, loff_t offset)
339{
340 struct uprobe u = { .inode = inode, .offset = offset };
341 struct rb_node *n = uprobes_tree.rb_node;
342 struct uprobe *uprobe;
343 int match;
344
345 while (n) {
346 uprobe = rb_entry(n, struct uprobe, rb_node);
347 match = match_uprobe(&u, uprobe);
348 if (!match) {
349 atomic_inc(&uprobe->ref);
350 return uprobe;
351 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100352
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530353 if (match < 0)
354 n = n->rb_left;
355 else
356 n = n->rb_right;
357 }
358 return NULL;
359}
360
361/*
362 * Find a uprobe corresponding to a given inode:offset
363 * Acquires uprobes_treelock
364 */
365static struct uprobe *find_uprobe(struct inode *inode, loff_t offset)
366{
367 struct uprobe *uprobe;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530368
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200369 spin_lock(&uprobes_treelock);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530370 uprobe = __find_uprobe(inode, offset);
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200371 spin_unlock(&uprobes_treelock);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100372
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530373 return uprobe;
374}
375
376static struct uprobe *__insert_uprobe(struct uprobe *uprobe)
377{
378 struct rb_node **p = &uprobes_tree.rb_node;
379 struct rb_node *parent = NULL;
380 struct uprobe *u;
381 int match;
382
383 while (*p) {
384 parent = *p;
385 u = rb_entry(parent, struct uprobe, rb_node);
386 match = match_uprobe(uprobe, u);
387 if (!match) {
388 atomic_inc(&u->ref);
389 return u;
390 }
391
392 if (match < 0)
393 p = &parent->rb_left;
394 else
395 p = &parent->rb_right;
396
397 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100398
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530399 u = NULL;
400 rb_link_node(&uprobe->rb_node, parent, p);
401 rb_insert_color(&uprobe->rb_node, &uprobes_tree);
402 /* get access + creation ref */
403 atomic_set(&uprobe->ref, 2);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100404
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530405 return u;
406}
407
408/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100409 * Acquire uprobes_treelock.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530410 * Matching uprobe already exists in rbtree;
411 * increment (access refcount) and return the matching uprobe.
412 *
413 * No matching uprobe; insert the uprobe in rb_tree;
414 * get a double refcount (access + creation) and return NULL.
415 */
416static struct uprobe *insert_uprobe(struct uprobe *uprobe)
417{
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530418 struct uprobe *u;
419
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200420 spin_lock(&uprobes_treelock);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530421 u = __insert_uprobe(uprobe);
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200422 spin_unlock(&uprobes_treelock);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100423
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530424 /* For now assume that the instruction need not be single-stepped */
425 uprobe->flags |= UPROBE_SKIP_SSTEP;
426
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530427 return u;
428}
429
430static void put_uprobe(struct uprobe *uprobe)
431{
432 if (atomic_dec_and_test(&uprobe->ref))
433 kfree(uprobe);
434}
435
436static struct uprobe *alloc_uprobe(struct inode *inode, loff_t offset)
437{
438 struct uprobe *uprobe, *cur_uprobe;
439
440 uprobe = kzalloc(sizeof(struct uprobe), GFP_KERNEL);
441 if (!uprobe)
442 return NULL;
443
444 uprobe->inode = igrab(inode);
445 uprobe->offset = offset;
446 init_rwsem(&uprobe->consumer_rwsem);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530447
448 /* add to uprobes_tree, sorted on inode:offset */
449 cur_uprobe = insert_uprobe(uprobe);
450
451 /* a uprobe exists for this inode:offset combination */
452 if (cur_uprobe) {
453 kfree(uprobe);
454 uprobe = cur_uprobe;
455 iput(inode);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100456 } else {
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530457 atomic_inc(&uprobe_events);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100458 }
459
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530460 return uprobe;
461}
462
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530463static void handler_chain(struct uprobe *uprobe, struct pt_regs *regs)
464{
465 struct uprobe_consumer *uc;
466
467 if (!(uprobe->flags & UPROBE_RUN_HANDLER))
468 return;
469
470 down_read(&uprobe->consumer_rwsem);
471 for (uc = uprobe->consumers; uc; uc = uc->next) {
472 if (!uc->filter || uc->filter(uc, current))
473 uc->handler(uc, regs);
474 }
475 up_read(&uprobe->consumer_rwsem);
476}
477
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530478/* Returns the previous consumer */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100479static struct uprobe_consumer *
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530480consumer_add(struct uprobe *uprobe, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530481{
482 down_write(&uprobe->consumer_rwsem);
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530483 uc->next = uprobe->consumers;
484 uprobe->consumers = uc;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530485 up_write(&uprobe->consumer_rwsem);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100486
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530487 return uc->next;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530488}
489
490/*
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530491 * For uprobe @uprobe, delete the consumer @uc.
492 * Return true if the @uc is deleted successfully
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530493 * or return false.
494 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530495static bool consumer_del(struct uprobe *uprobe, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530496{
497 struct uprobe_consumer **con;
498 bool ret = false;
499
500 down_write(&uprobe->consumer_rwsem);
501 for (con = &uprobe->consumers; *con; con = &(*con)->next) {
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530502 if (*con == uc) {
503 *con = uc->next;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530504 ret = true;
505 break;
506 }
507 }
508 up_write(&uprobe->consumer_rwsem);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100509
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530510 return ret;
511}
512
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530513static int
Oleg Nesterovd4366152012-06-15 17:43:42 +0200514__copy_insn(struct address_space *mapping, struct file *filp, char *insn,
Oleg Nesterov593609a2012-06-15 17:43:59 +0200515 unsigned long nbytes, loff_t offset)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530516{
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530517 struct page *page;
518 void *vaddr;
Oleg Nesterov593609a2012-06-15 17:43:59 +0200519 unsigned long off;
520 pgoff_t idx;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530521
522 if (!filp)
523 return -EINVAL;
524
Oleg Nesterovcc359d12012-06-15 17:43:25 +0200525 if (!mapping->a_ops->readpage)
526 return -EIO;
527
Oleg Nesterov593609a2012-06-15 17:43:59 +0200528 idx = offset >> PAGE_CACHE_SHIFT;
529 off = offset & ~PAGE_MASK;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530530
531 /*
532 * Ensure that the page that has the original instruction is
533 * populated and in page-cache.
534 */
535 page = read_mapping_page(mapping, idx, filp);
536 if (IS_ERR(page))
537 return PTR_ERR(page);
538
539 vaddr = kmap_atomic(page);
Oleg Nesterov593609a2012-06-15 17:43:59 +0200540 memcpy(insn, vaddr + off, nbytes);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530541 kunmap_atomic(vaddr);
542 page_cache_release(page);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100543
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530544 return 0;
545}
546
Oleg Nesterovd4366152012-06-15 17:43:42 +0200547static int copy_insn(struct uprobe *uprobe, struct file *filp)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530548{
549 struct address_space *mapping;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530550 unsigned long nbytes;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100551 int bytes;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530552
Oleg Nesterovd4366152012-06-15 17:43:42 +0200553 nbytes = PAGE_SIZE - (uprobe->offset & ~PAGE_MASK);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530554 mapping = uprobe->inode->i_mapping;
555
556 /* Instruction at end of binary; copy only available bytes */
557 if (uprobe->offset + MAX_UINSN_BYTES > uprobe->inode->i_size)
558 bytes = uprobe->inode->i_size - uprobe->offset;
559 else
560 bytes = MAX_UINSN_BYTES;
561
562 /* Instruction at the page-boundary; copy bytes in second page */
563 if (nbytes < bytes) {
Oleg Nesterovfc36f592012-06-15 17:43:44 +0200564 int err = __copy_insn(mapping, filp, uprobe->arch.insn + nbytes,
565 bytes - nbytes, uprobe->offset + nbytes);
566 if (err)
567 return err;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530568 bytes = nbytes;
569 }
Oleg Nesterovd4366152012-06-15 17:43:42 +0200570 return __copy_insn(mapping, filp, uprobe->arch.insn, bytes, uprobe->offset);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530571}
572
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200573static int prepare_uprobe(struct uprobe *uprobe, struct file *file,
574 struct mm_struct *mm, unsigned long vaddr)
575{
576 int ret = 0;
577
578 if (uprobe->flags & UPROBE_COPY_INSN)
579 return ret;
580
581 ret = copy_insn(uprobe, file);
582 if (ret)
583 goto out;
584
585 ret = -ENOTSUPP;
586 if (is_swbp_insn((uprobe_opcode_t *)uprobe->arch.insn))
587 goto out;
588
589 ret = arch_uprobe_analyze_insn(&uprobe->arch, mm, vaddr);
590 if (ret)
591 goto out;
592
593 /* write_opcode() assumes we don't cross page boundary */
594 BUG_ON((uprobe->offset & ~PAGE_MASK) +
595 UPROBE_SWBP_INSN_SIZE > PAGE_SIZE);
596
597 smp_wmb(); /* pairs with rmb() in find_active_uprobe() */
598 uprobe->flags |= UPROBE_COPY_INSN;
599
600 out:
601 return ret;
602}
603
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530604static int
605install_breakpoint(struct uprobe *uprobe, struct mm_struct *mm,
Oleg Nesterov816c03f2012-06-15 17:43:55 +0200606 struct vm_area_struct *vma, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530607{
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +0200608 bool first_uprobe;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530609 int ret;
610
611 /*
612 * If probe is being deleted, unregister thread could be done with
613 * the vma-rmap-walk through. Adding a probe now can be fatal since
614 * nobody will be able to cleanup. Also we could be from fork or
615 * mremap path, where the probe might have already been inserted.
616 * Hence behave as if probe already existed.
617 */
618 if (!uprobe->consumers)
Oleg Nesterov78f74112012-08-08 17:35:08 +0200619 return 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530620
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200621 ret = prepare_uprobe(uprobe, vma->vm_file, mm, vaddr);
622 if (ret)
623 return ret;
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530624
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +0200625 /*
626 * set MMF_HAS_UPROBES in advance for uprobe_pre_sstep_notifier(),
627 * the task can hit this breakpoint right after __replace_page().
628 */
629 first_uprobe = !test_bit(MMF_HAS_UPROBES, &mm->flags);
630 if (first_uprobe)
631 set_bit(MMF_HAS_UPROBES, &mm->flags);
632
Oleg Nesterov816c03f2012-06-15 17:43:55 +0200633 ret = set_swbp(&uprobe->arch, mm, vaddr);
Oleg Nesterov9f68f672012-08-19 16:15:09 +0200634 if (!ret)
635 clear_bit(MMF_RECALC_UPROBES, &mm->flags);
636 else if (first_uprobe)
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +0200637 clear_bit(MMF_HAS_UPROBES, &mm->flags);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530638
639 return ret;
640}
641
Oleg Nesterov076a3652012-09-30 18:54:53 +0200642static int
Oleg Nesterov816c03f2012-06-15 17:43:55 +0200643remove_breakpoint(struct uprobe *uprobe, struct mm_struct *mm, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530644{
Oleg Nesterov9f68f672012-08-19 16:15:09 +0200645 /* can happen if uprobe_register() fails */
646 if (!test_bit(MMF_HAS_UPROBES, &mm->flags))
Oleg Nesterov076a3652012-09-30 18:54:53 +0200647 return 0;
Oleg Nesterov9f68f672012-08-19 16:15:09 +0200648
649 set_bit(MMF_RECALC_UPROBES, &mm->flags);
Oleg Nesterov076a3652012-09-30 18:54:53 +0200650 return set_orig_insn(&uprobe->arch, mm, vaddr);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530651}
652
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530653/*
Oleg Nesterov778b0322012-05-29 21:30:08 +0200654 * There could be threads that have already hit the breakpoint. They
655 * will recheck the current insn and restart if find_uprobe() fails.
656 * See find_active_uprobe().
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530657 */
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530658static void delete_uprobe(struct uprobe *uprobe)
659{
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200660 spin_lock(&uprobes_treelock);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530661 rb_erase(&uprobe->rb_node, &uprobes_tree);
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200662 spin_unlock(&uprobes_treelock);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530663 iput(uprobe->inode);
664 put_uprobe(uprobe);
665 atomic_dec(&uprobe_events);
666}
667
Oleg Nesterov26872092012-06-15 17:43:33 +0200668struct map_info {
669 struct map_info *next;
670 struct mm_struct *mm;
Oleg Nesterov816c03f2012-06-15 17:43:55 +0200671 unsigned long vaddr;
Oleg Nesterov26872092012-06-15 17:43:33 +0200672};
673
674static inline struct map_info *free_map_info(struct map_info *info)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530675{
Oleg Nesterov26872092012-06-15 17:43:33 +0200676 struct map_info *next = info->next;
677 kfree(info);
678 return next;
679}
680
681static struct map_info *
682build_map_info(struct address_space *mapping, loff_t offset, bool is_register)
683{
684 unsigned long pgoff = offset >> PAGE_SHIFT;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530685 struct prio_tree_iter iter;
686 struct vm_area_struct *vma;
Oleg Nesterov26872092012-06-15 17:43:33 +0200687 struct map_info *curr = NULL;
688 struct map_info *prev = NULL;
689 struct map_info *info;
690 int more = 0;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100691
Oleg Nesterov26872092012-06-15 17:43:33 +0200692 again:
693 mutex_lock(&mapping->i_mmap_mutex);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530694 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
695 if (!valid_vma(vma, is_register))
696 continue;
697
Oleg Nesterov7a5bfb62012-06-15 17:43:36 +0200698 if (!prev && !more) {
699 /*
700 * Needs GFP_NOWAIT to avoid i_mmap_mutex recursion through
701 * reclaim. This is optimistic, no harm done if it fails.
702 */
703 prev = kmalloc(sizeof(struct map_info),
704 GFP_NOWAIT | __GFP_NOMEMALLOC | __GFP_NOWARN);
705 if (prev)
706 prev->next = NULL;
707 }
Oleg Nesterov26872092012-06-15 17:43:33 +0200708 if (!prev) {
709 more++;
710 continue;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530711 }
712
Oleg Nesterov26872092012-06-15 17:43:33 +0200713 if (!atomic_inc_not_zero(&vma->vm_mm->mm_users))
714 continue;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100715
Oleg Nesterov26872092012-06-15 17:43:33 +0200716 info = prev;
717 prev = prev->next;
718 info->next = curr;
719 curr = info;
720
721 info->mm = vma->vm_mm;
Oleg Nesterov57683f72012-07-29 20:22:47 +0200722 info->vaddr = offset_to_vaddr(vma, offset);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530723 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530724 mutex_unlock(&mapping->i_mmap_mutex);
725
Oleg Nesterov26872092012-06-15 17:43:33 +0200726 if (!more)
727 goto out;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100728
Oleg Nesterov26872092012-06-15 17:43:33 +0200729 prev = curr;
730 while (curr) {
731 mmput(curr->mm);
732 curr = curr->next;
733 }
734
735 do {
736 info = kmalloc(sizeof(struct map_info), GFP_KERNEL);
737 if (!info) {
738 curr = ERR_PTR(-ENOMEM);
739 goto out;
740 }
741 info->next = prev;
742 prev = info;
743 } while (--more);
744
745 goto again;
746 out:
747 while (prev)
748 prev = free_map_info(prev);
749 return curr;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530750}
751
752static int register_for_each_vma(struct uprobe *uprobe, bool is_register)
753{
Oleg Nesterov26872092012-06-15 17:43:33 +0200754 struct map_info *info;
755 int err = 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530756
Oleg Nesterov26872092012-06-15 17:43:33 +0200757 info = build_map_info(uprobe->inode->i_mapping,
758 uprobe->offset, is_register);
759 if (IS_ERR(info))
760 return PTR_ERR(info);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100761
Oleg Nesterov26872092012-06-15 17:43:33 +0200762 while (info) {
763 struct mm_struct *mm = info->mm;
764 struct vm_area_struct *vma;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100765
Oleg Nesterov076a3652012-09-30 18:54:53 +0200766 if (err && is_register)
Oleg Nesterov26872092012-06-15 17:43:33 +0200767 goto free;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100768
Oleg Nesterov77fc4af2012-05-29 21:29:28 +0200769 down_write(&mm->mmap_sem);
Oleg Nesterovf4d6dfe2012-07-29 20:22:44 +0200770 vma = find_vma(mm, info->vaddr);
771 if (!vma || !valid_vma(vma, is_register) ||
772 vma->vm_file->f_mapping->host != uprobe->inode)
Oleg Nesterov26872092012-06-15 17:43:33 +0200773 goto unlock;
774
Oleg Nesterovf4d6dfe2012-07-29 20:22:44 +0200775 if (vma->vm_start > info->vaddr ||
776 vaddr_to_offset(vma, info->vaddr) != uprobe->offset)
Oleg Nesterov26872092012-06-15 17:43:33 +0200777 goto unlock;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530778
Oleg Nesterov78f74112012-08-08 17:35:08 +0200779 if (is_register)
Oleg Nesterov26872092012-06-15 17:43:33 +0200780 err = install_breakpoint(uprobe, mm, vma, info->vaddr);
Oleg Nesterov78f74112012-08-08 17:35:08 +0200781 else
Oleg Nesterov076a3652012-09-30 18:54:53 +0200782 err |= remove_breakpoint(uprobe, mm, info->vaddr);
Oleg Nesterov78f74112012-08-08 17:35:08 +0200783
Oleg Nesterov26872092012-06-15 17:43:33 +0200784 unlock:
785 up_write(&mm->mmap_sem);
786 free:
787 mmput(mm);
788 info = free_map_info(info);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530789 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100790
Oleg Nesterov26872092012-06-15 17:43:33 +0200791 return err;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530792}
793
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100794static int __uprobe_register(struct uprobe *uprobe)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530795{
796 return register_for_each_vma(uprobe, true);
797}
798
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100799static void __uprobe_unregister(struct uprobe *uprobe)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530800{
801 if (!register_for_each_vma(uprobe, false))
802 delete_uprobe(uprobe);
803
804 /* TODO : cant unregister? schedule a worker thread */
805}
806
807/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100808 * uprobe_register - register a probe
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530809 * @inode: the file in which the probe has to be placed.
810 * @offset: offset from the start of the file.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530811 * @uc: information on howto handle the probe..
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530812 *
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100813 * Apart from the access refcount, uprobe_register() takes a creation
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530814 * refcount (thro alloc_uprobe) if and only if this @uprobe is getting
815 * inserted into the rbtree (i.e first consumer for a @inode:@offset
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100816 * tuple). Creation refcount stops uprobe_unregister from freeing the
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530817 * @uprobe even before the register operation is complete. Creation
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530818 * refcount is released when the last @uc for the @uprobe
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530819 * unregisters.
820 *
821 * Return errno if it cannot successully install probes
822 * else return 0 (success)
823 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530824int uprobe_register(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530825{
826 struct uprobe *uprobe;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100827 int ret;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530828
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530829 if (!inode || !uc || uc->next)
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100830 return -EINVAL;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530831
832 if (offset > i_size_read(inode))
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100833 return -EINVAL;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530834
835 ret = 0;
836 mutex_lock(uprobes_hash(inode));
837 uprobe = alloc_uprobe(inode, offset);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100838
Oleg Nesterova5f658b2012-09-30 18:21:09 +0200839 if (!uprobe) {
840 ret = -ENOMEM;
841 } else if (!consumer_add(uprobe, uc)) {
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100842 ret = __uprobe_register(uprobe);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530843 if (ret) {
844 uprobe->consumers = NULL;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100845 __uprobe_unregister(uprobe);
846 } else {
Srikar Dronamraju900771a2012-03-12 14:55:14 +0530847 uprobe->flags |= UPROBE_RUN_HANDLER;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100848 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530849 }
850
851 mutex_unlock(uprobes_hash(inode));
Sebastian Andrzej Siewior6d1d8df2012-08-30 19:26:22 +0200852 if (uprobe)
853 put_uprobe(uprobe);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530854
855 return ret;
856}
857
858/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100859 * uprobe_unregister - unregister a already registered probe.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530860 * @inode: the file in which the probe has to be removed.
861 * @offset: offset from the start of the file.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530862 * @uc: identify which probe if multiple probes are colocated.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530863 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530864void uprobe_unregister(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530865{
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100866 struct uprobe *uprobe;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530867
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530868 if (!inode || !uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530869 return;
870
871 uprobe = find_uprobe(inode, offset);
872 if (!uprobe)
873 return;
874
875 mutex_lock(uprobes_hash(inode));
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530876
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530877 if (consumer_del(uprobe, uc)) {
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100878 if (!uprobe->consumers) {
879 __uprobe_unregister(uprobe);
Srikar Dronamraju900771a2012-03-12 14:55:14 +0530880 uprobe->flags &= ~UPROBE_RUN_HANDLER;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100881 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530882 }
883
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530884 mutex_unlock(uprobes_hash(inode));
885 if (uprobe)
886 put_uprobe(uprobe);
887}
888
Oleg Nesterov891c3972012-07-29 20:22:40 +0200889static struct rb_node *
890find_node_in_range(struct inode *inode, loff_t min, loff_t max)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530891{
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530892 struct rb_node *n = uprobes_tree.rb_node;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530893
894 while (n) {
Oleg Nesterov891c3972012-07-29 20:22:40 +0200895 struct uprobe *u = rb_entry(n, struct uprobe, rb_node);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100896
Oleg Nesterov891c3972012-07-29 20:22:40 +0200897 if (inode < u->inode) {
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530898 n = n->rb_left;
Oleg Nesterov891c3972012-07-29 20:22:40 +0200899 } else if (inode > u->inode) {
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530900 n = n->rb_right;
Oleg Nesterov891c3972012-07-29 20:22:40 +0200901 } else {
902 if (max < u->offset)
903 n = n->rb_left;
904 else if (min > u->offset)
905 n = n->rb_right;
906 else
907 break;
908 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530909 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100910
Oleg Nesterov891c3972012-07-29 20:22:40 +0200911 return n;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530912}
913
914/*
Oleg Nesterov891c3972012-07-29 20:22:40 +0200915 * For a given range in vma, build a list of probes that need to be inserted.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530916 */
Oleg Nesterov891c3972012-07-29 20:22:40 +0200917static void build_probe_list(struct inode *inode,
918 struct vm_area_struct *vma,
919 unsigned long start, unsigned long end,
920 struct list_head *head)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530921{
Oleg Nesterov891c3972012-07-29 20:22:40 +0200922 loff_t min, max;
Oleg Nesterov891c3972012-07-29 20:22:40 +0200923 struct rb_node *n, *t;
924 struct uprobe *u;
925
926 INIT_LIST_HEAD(head);
Oleg Nesterovcb113b42012-07-29 20:22:42 +0200927 min = vaddr_to_offset(vma, start);
Oleg Nesterov891c3972012-07-29 20:22:40 +0200928 max = min + (end - start) - 1;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530929
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200930 spin_lock(&uprobes_treelock);
Oleg Nesterov891c3972012-07-29 20:22:40 +0200931 n = find_node_in_range(inode, min, max);
932 if (n) {
933 for (t = n; t; t = rb_prev(t)) {
934 u = rb_entry(t, struct uprobe, rb_node);
935 if (u->inode != inode || u->offset < min)
936 break;
937 list_add(&u->pending_list, head);
938 atomic_inc(&u->ref);
939 }
940 for (t = n; (t = rb_next(t)); ) {
941 u = rb_entry(t, struct uprobe, rb_node);
942 if (u->inode != inode || u->offset > max)
943 break;
944 list_add(&u->pending_list, head);
945 atomic_inc(&u->ref);
946 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530947 }
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200948 spin_unlock(&uprobes_treelock);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530949}
950
951/*
Oleg Nesterov5e5be712012-08-06 14:49:56 +0200952 * Called from mmap_region/vma_adjust with mm->mmap_sem acquired.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530953 *
Oleg Nesterov5e5be712012-08-06 14:49:56 +0200954 * Currently we ignore all errors and always return 0, the callers
955 * can't handle the failure anyway.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530956 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100957int uprobe_mmap(struct vm_area_struct *vma)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530958{
959 struct list_head tmp_list;
Oleg Nesterov665605a2012-07-29 20:22:29 +0200960 struct uprobe *uprobe, *u;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530961 struct inode *inode;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530962
963 if (!atomic_read(&uprobe_events) || !valid_vma(vma, true))
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100964 return 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530965
966 inode = vma->vm_file->f_mapping->host;
967 if (!inode)
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100968 return 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530969
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530970 mutex_lock(uprobes_mmap_hash(inode));
Oleg Nesterov891c3972012-07-29 20:22:40 +0200971 build_probe_list(inode, vma, vma->vm_start, vma->vm_end, &tmp_list);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100972
Oleg Nesterov665605a2012-07-29 20:22:29 +0200973 list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
Oleg Nesterov5e5be712012-08-06 14:49:56 +0200974 if (!fatal_signal_pending(current)) {
Oleg Nesterov57683f72012-07-29 20:22:47 +0200975 unsigned long vaddr = offset_to_vaddr(vma, uprobe->offset);
Oleg Nesterov5e5be712012-08-06 14:49:56 +0200976 install_breakpoint(uprobe, vma->vm_mm, vma, vaddr);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530977 }
978 put_uprobe(uprobe);
979 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530980 mutex_unlock(uprobes_mmap_hash(inode));
981
Oleg Nesterov5e5be712012-08-06 14:49:56 +0200982 return 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530983}
984
Oleg Nesterov9f68f672012-08-19 16:15:09 +0200985static bool
986vma_has_uprobes(struct vm_area_struct *vma, unsigned long start, unsigned long end)
987{
988 loff_t min, max;
989 struct inode *inode;
990 struct rb_node *n;
991
992 inode = vma->vm_file->f_mapping->host;
993
994 min = vaddr_to_offset(vma, start);
995 max = min + (end - start) - 1;
996
997 spin_lock(&uprobes_treelock);
998 n = find_node_in_range(inode, min, max);
999 spin_unlock(&uprobes_treelock);
1000
1001 return !!n;
1002}
1003
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301004/*
1005 * Called in context of a munmap of a vma.
1006 */
Srikar Dronamrajucbc91f72012-04-11 16:05:27 +05301007void uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end)
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301008{
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301009 if (!atomic_read(&uprobe_events) || !valid_vma(vma, false))
1010 return;
1011
Oleg Nesterov2fd611a2012-07-29 20:22:31 +02001012 if (!atomic_read(&vma->vm_mm->mm_users)) /* called by mmput() ? */
1013 return;
1014
Oleg Nesterov9f68f672012-08-19 16:15:09 +02001015 if (!test_bit(MMF_HAS_UPROBES, &vma->vm_mm->flags) ||
1016 test_bit(MMF_RECALC_UPROBES, &vma->vm_mm->flags))
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +02001017 return;
1018
Oleg Nesterov9f68f672012-08-19 16:15:09 +02001019 if (vma_has_uprobes(vma, start, end))
1020 set_bit(MMF_RECALC_UPROBES, &vma->vm_mm->flags);
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301021}
1022
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301023/* Slot allocation for XOL */
1024static int xol_add_vma(struct xol_area *area)
1025{
1026 struct mm_struct *mm;
1027 int ret;
1028
1029 area->page = alloc_page(GFP_HIGHUSER);
1030 if (!area->page)
1031 return -ENOMEM;
1032
1033 ret = -EALREADY;
1034 mm = current->mm;
1035
1036 down_write(&mm->mmap_sem);
1037 if (mm->uprobes_state.xol_area)
1038 goto fail;
1039
1040 ret = -ENOMEM;
1041
1042 /* Try to map as high as possible, this is only a hint. */
1043 area->vaddr = get_unmapped_area(NULL, TASK_SIZE - PAGE_SIZE, PAGE_SIZE, 0, 0);
1044 if (area->vaddr & ~PAGE_MASK) {
1045 ret = area->vaddr;
1046 goto fail;
1047 }
1048
1049 ret = install_special_mapping(mm, area->vaddr, PAGE_SIZE,
1050 VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO, &area->page);
1051 if (ret)
1052 goto fail;
1053
1054 smp_wmb(); /* pairs with get_xol_area() */
1055 mm->uprobes_state.xol_area = area;
1056 ret = 0;
1057
1058fail:
1059 up_write(&mm->mmap_sem);
1060 if (ret)
1061 __free_page(area->page);
1062
1063 return ret;
1064}
1065
1066static struct xol_area *get_xol_area(struct mm_struct *mm)
1067{
1068 struct xol_area *area;
1069
1070 area = mm->uprobes_state.xol_area;
1071 smp_read_barrier_depends(); /* pairs with wmb in xol_add_vma() */
1072
1073 return area;
1074}
1075
1076/*
1077 * xol_alloc_area - Allocate process's xol_area.
1078 * This area will be used for storing instructions for execution out of
1079 * line.
1080 *
1081 * Returns the allocated area or NULL.
1082 */
1083static struct xol_area *xol_alloc_area(void)
1084{
1085 struct xol_area *area;
1086
1087 area = kzalloc(sizeof(*area), GFP_KERNEL);
1088 if (unlikely(!area))
1089 return NULL;
1090
1091 area->bitmap = kzalloc(BITS_TO_LONGS(UINSNS_PER_PAGE) * sizeof(long), GFP_KERNEL);
1092
1093 if (!area->bitmap)
1094 goto fail;
1095
1096 init_waitqueue_head(&area->wq);
1097 if (!xol_add_vma(area))
1098 return area;
1099
1100fail:
1101 kfree(area->bitmap);
1102 kfree(area);
1103
1104 return get_xol_area(current->mm);
1105}
1106
1107/*
1108 * uprobe_clear_state - Free the area allocated for slots.
1109 */
1110void uprobe_clear_state(struct mm_struct *mm)
1111{
1112 struct xol_area *area = mm->uprobes_state.xol_area;
1113
1114 if (!area)
1115 return;
1116
1117 put_page(area->page);
1118 kfree(area->bitmap);
1119 kfree(area);
1120}
1121
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +02001122void uprobe_dup_mmap(struct mm_struct *oldmm, struct mm_struct *newmm)
1123{
Oleg Nesterov61559a82012-08-08 17:17:46 +02001124 newmm->uprobes_state.xol_area = NULL;
1125
Oleg Nesterov9f68f672012-08-19 16:15:09 +02001126 if (test_bit(MMF_HAS_UPROBES, &oldmm->flags)) {
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +02001127 set_bit(MMF_HAS_UPROBES, &newmm->flags);
Oleg Nesterov9f68f672012-08-19 16:15:09 +02001128 /* unconditionally, dup_mmap() skips VM_DONTCOPY vmas */
1129 set_bit(MMF_RECALC_UPROBES, &newmm->flags);
1130 }
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +02001131}
1132
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301133/*
1134 * - search for a free slot.
1135 */
1136static unsigned long xol_take_insn_slot(struct xol_area *area)
1137{
1138 unsigned long slot_addr;
1139 int slot_nr;
1140
1141 do {
1142 slot_nr = find_first_zero_bit(area->bitmap, UINSNS_PER_PAGE);
1143 if (slot_nr < UINSNS_PER_PAGE) {
1144 if (!test_and_set_bit(slot_nr, area->bitmap))
1145 break;
1146
1147 slot_nr = UINSNS_PER_PAGE;
1148 continue;
1149 }
1150 wait_event(area->wq, (atomic_read(&area->slot_count) < UINSNS_PER_PAGE));
1151 } while (slot_nr >= UINSNS_PER_PAGE);
1152
1153 slot_addr = area->vaddr + (slot_nr * UPROBE_XOL_SLOT_BYTES);
1154 atomic_inc(&area->slot_count);
1155
1156 return slot_addr;
1157}
1158
1159/*
1160 * xol_get_insn_slot - If was not allocated a slot, then
1161 * allocate a slot.
1162 * Returns the allocated slot address or 0.
1163 */
1164static unsigned long xol_get_insn_slot(struct uprobe *uprobe, unsigned long slot_addr)
1165{
1166 struct xol_area *area;
1167 unsigned long offset;
1168 void *vaddr;
1169
1170 area = get_xol_area(current->mm);
1171 if (!area) {
1172 area = xol_alloc_area();
1173 if (!area)
1174 return 0;
1175 }
1176 current->utask->xol_vaddr = xol_take_insn_slot(area);
1177
1178 /*
1179 * Initialize the slot if xol_vaddr points to valid
1180 * instruction slot.
1181 */
1182 if (unlikely(!current->utask->xol_vaddr))
1183 return 0;
1184
1185 current->utask->vaddr = slot_addr;
1186 offset = current->utask->xol_vaddr & ~PAGE_MASK;
1187 vaddr = kmap_atomic(area->page);
1188 memcpy(vaddr + offset, uprobe->arch.insn, MAX_UINSN_BYTES);
1189 kunmap_atomic(vaddr);
1190
1191 return current->utask->xol_vaddr;
1192}
1193
1194/*
1195 * xol_free_insn_slot - If slot was earlier allocated by
1196 * @xol_get_insn_slot(), make the slot available for
1197 * subsequent requests.
1198 */
1199static void xol_free_insn_slot(struct task_struct *tsk)
1200{
1201 struct xol_area *area;
1202 unsigned long vma_end;
1203 unsigned long slot_addr;
1204
1205 if (!tsk->mm || !tsk->mm->uprobes_state.xol_area || !tsk->utask)
1206 return;
1207
1208 slot_addr = tsk->utask->xol_vaddr;
1209
1210 if (unlikely(!slot_addr || IS_ERR_VALUE(slot_addr)))
1211 return;
1212
1213 area = tsk->mm->uprobes_state.xol_area;
1214 vma_end = area->vaddr + PAGE_SIZE;
1215 if (area->vaddr <= slot_addr && slot_addr < vma_end) {
1216 unsigned long offset;
1217 int slot_nr;
1218
1219 offset = slot_addr - area->vaddr;
1220 slot_nr = offset / UPROBE_XOL_SLOT_BYTES;
1221 if (slot_nr >= UINSNS_PER_PAGE)
1222 return;
1223
1224 clear_bit(slot_nr, area->bitmap);
1225 atomic_dec(&area->slot_count);
1226 if (waitqueue_active(&area->wq))
1227 wake_up(&area->wq);
1228
1229 tsk->utask->xol_vaddr = 0;
1230 }
1231}
1232
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301233/**
1234 * uprobe_get_swbp_addr - compute address of swbp given post-swbp regs
1235 * @regs: Reflects the saved state of the task after it has hit a breakpoint
1236 * instruction.
1237 * Return the address of the breakpoint instruction.
1238 */
1239unsigned long __weak uprobe_get_swbp_addr(struct pt_regs *regs)
1240{
1241 return instruction_pointer(regs) - UPROBE_SWBP_INSN_SIZE;
1242}
1243
1244/*
1245 * Called with no locks held.
1246 * Called in context of a exiting or a exec-ing thread.
1247 */
1248void uprobe_free_utask(struct task_struct *t)
1249{
1250 struct uprobe_task *utask = t->utask;
1251
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301252 if (!utask)
1253 return;
1254
1255 if (utask->active_uprobe)
1256 put_uprobe(utask->active_uprobe);
1257
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301258 xol_free_insn_slot(t);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301259 kfree(utask);
1260 t->utask = NULL;
1261}
1262
1263/*
1264 * Called in context of a new clone/fork from copy_process.
1265 */
1266void uprobe_copy_process(struct task_struct *t)
1267{
1268 t->utask = NULL;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301269}
1270
1271/*
1272 * Allocate a uprobe_task object for the task.
1273 * Called when the thread hits a breakpoint for the first time.
1274 *
1275 * Returns:
1276 * - pointer to new uprobe_task on success
1277 * - NULL otherwise
1278 */
1279static struct uprobe_task *add_utask(void)
1280{
1281 struct uprobe_task *utask;
1282
1283 utask = kzalloc(sizeof *utask, GFP_KERNEL);
1284 if (unlikely(!utask))
1285 return NULL;
1286
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301287 current->utask = utask;
1288 return utask;
1289}
1290
1291/* Prepare to single-step probed instruction out of line. */
1292static int
1293pre_ssout(struct uprobe *uprobe, struct pt_regs *regs, unsigned long vaddr)
1294{
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301295 if (xol_get_insn_slot(uprobe, vaddr) && !arch_uprobe_pre_xol(&uprobe->arch, regs))
1296 return 0;
1297
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301298 return -EFAULT;
1299}
1300
1301/*
1302 * If we are singlestepping, then ensure this thread is not connected to
1303 * non-fatal signals until completion of singlestep. When xol insn itself
1304 * triggers the signal, restart the original insn even if the task is
1305 * already SIGKILL'ed (since coredump should report the correct ip). This
1306 * is even more important if the task has a handler for SIGSEGV/etc, The
1307 * _same_ instruction should be repeated again after return from the signal
1308 * handler, and SSTEP can never finish in this case.
1309 */
1310bool uprobe_deny_signal(void)
1311{
1312 struct task_struct *t = current;
1313 struct uprobe_task *utask = t->utask;
1314
1315 if (likely(!utask || !utask->active_uprobe))
1316 return false;
1317
1318 WARN_ON_ONCE(utask->state != UTASK_SSTEP);
1319
1320 if (signal_pending(t)) {
1321 spin_lock_irq(&t->sighand->siglock);
1322 clear_tsk_thread_flag(t, TIF_SIGPENDING);
1323 spin_unlock_irq(&t->sighand->siglock);
1324
1325 if (__fatal_signal_pending(t) || arch_uprobe_xol_was_trapped(t)) {
1326 utask->state = UTASK_SSTEP_TRAPPED;
1327 set_tsk_thread_flag(t, TIF_UPROBE);
1328 set_tsk_thread_flag(t, TIF_NOTIFY_RESUME);
1329 }
1330 }
1331
1332 return true;
1333}
1334
1335/*
1336 * Avoid singlestepping the original instruction if the original instruction
1337 * is a NOP or can be emulated.
1338 */
1339static bool can_skip_sstep(struct uprobe *uprobe, struct pt_regs *regs)
1340{
Oleg Nesterov0578a972012-09-14 18:31:23 +02001341 if (uprobe->flags & UPROBE_SKIP_SSTEP) {
1342 if (arch_uprobe_skip_sstep(&uprobe->arch, regs))
1343 return true;
1344 uprobe->flags &= ~UPROBE_SKIP_SSTEP;
1345 }
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301346 return false;
1347}
1348
Oleg Nesterov499a4f32012-08-19 17:41:34 +02001349static void mmf_recalc_uprobes(struct mm_struct *mm)
1350{
1351 struct vm_area_struct *vma;
1352
1353 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1354 if (!valid_vma(vma, false))
1355 continue;
1356 /*
1357 * This is not strictly accurate, we can race with
1358 * uprobe_unregister() and see the already removed
1359 * uprobe if delete_uprobe() was not yet called.
1360 */
1361 if (vma_has_uprobes(vma, vma->vm_start, vma->vm_end))
1362 return;
1363 }
1364
1365 clear_bit(MMF_HAS_UPROBES, &mm->flags);
1366}
1367
Oleg Nesterovec75fba2012-09-23 21:55:19 +02001368static int is_swbp_at_addr(struct mm_struct *mm, unsigned long vaddr)
1369{
1370 struct page *page;
1371 uprobe_opcode_t opcode;
1372 int result;
1373
1374 pagefault_disable();
1375 result = __copy_from_user_inatomic(&opcode, (void __user*)vaddr,
1376 sizeof(opcode));
1377 pagefault_enable();
1378
1379 if (likely(result == 0))
1380 goto out;
1381
1382 result = get_user_pages(NULL, mm, vaddr, 1, 0, 1, &page, NULL);
1383 if (result < 0)
1384 return result;
1385
1386 copy_opcode(page, vaddr, &opcode);
1387 put_page(page);
1388 out:
1389 return is_swbp_insn(&opcode);
1390}
1391
Oleg Nesterovd790d342012-05-29 21:29:14 +02001392static struct uprobe *find_active_uprobe(unsigned long bp_vaddr, int *is_swbp)
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001393{
1394 struct mm_struct *mm = current->mm;
1395 struct uprobe *uprobe = NULL;
1396 struct vm_area_struct *vma;
1397
1398 down_read(&mm->mmap_sem);
1399 vma = find_vma(mm, bp_vaddr);
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001400 if (vma && vma->vm_start <= bp_vaddr) {
1401 if (valid_vma(vma, false)) {
Oleg Nesterovcb113b42012-07-29 20:22:42 +02001402 struct inode *inode = vma->vm_file->f_mapping->host;
1403 loff_t offset = vaddr_to_offset(vma, bp_vaddr);
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001404
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001405 uprobe = find_uprobe(inode, offset);
1406 }
Oleg Nesterovd790d342012-05-29 21:29:14 +02001407
1408 if (!uprobe)
1409 *is_swbp = is_swbp_at_addr(mm, bp_vaddr);
1410 } else {
1411 *is_swbp = -EFAULT;
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001412 }
Oleg Nesterov499a4f32012-08-19 17:41:34 +02001413
1414 if (!uprobe && test_and_clear_bit(MMF_RECALC_UPROBES, &mm->flags))
1415 mmf_recalc_uprobes(mm);
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001416 up_read(&mm->mmap_sem);
1417
1418 return uprobe;
1419}
1420
Sebastian Andrzej Siewior9d778782012-08-07 18:12:28 +02001421void __weak arch_uprobe_enable_step(struct arch_uprobe *arch)
1422{
1423 user_enable_single_step(current);
1424}
1425
1426void __weak arch_uprobe_disable_step(struct arch_uprobe *arch)
1427{
1428 user_disable_single_step(current);
1429}
1430
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301431/*
1432 * Run handler and ask thread to singlestep.
1433 * Ensure all non-fatal signals cannot interrupt thread while it singlesteps.
1434 */
1435static void handle_swbp(struct pt_regs *regs)
1436{
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301437 struct uprobe_task *utask;
1438 struct uprobe *uprobe;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301439 unsigned long bp_vaddr;
Oleg Nesterov56bb4cf2012-05-29 21:29:47 +02001440 int uninitialized_var(is_swbp);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301441
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301442 bp_vaddr = uprobe_get_swbp_addr(regs);
Oleg Nesterovd790d342012-05-29 21:29:14 +02001443 uprobe = find_active_uprobe(bp_vaddr, &is_swbp);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301444
1445 if (!uprobe) {
Oleg Nesterov56bb4cf2012-05-29 21:29:47 +02001446 if (is_swbp > 0) {
1447 /* No matching uprobe; signal SIGTRAP. */
1448 send_sig(SIGTRAP, current, 0);
1449 } else {
1450 /*
1451 * Either we raced with uprobe_unregister() or we can't
1452 * access this memory. The latter is only possible if
1453 * another thread plays with our ->mm. In both cases
1454 * we can simply restart. If this vma was unmapped we
1455 * can pretend this insn was not executed yet and get
1456 * the (correct) SIGSEGV after restart.
1457 */
1458 instruction_pointer_set(regs, bp_vaddr);
1459 }
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301460 return;
1461 }
Oleg Nesterov142b18d2012-09-29 21:56:57 +02001462 /*
1463 * TODO: move copy_insn/etc into _register and remove this hack.
1464 * After we hit the bp, _unregister + _register can install the
1465 * new and not-yet-analyzed uprobe at the same address, restart.
1466 */
1467 smp_rmb(); /* pairs with wmb() in install_breakpoint() */
1468 if (unlikely(!(uprobe->flags & UPROBE_COPY_INSN)))
1469 goto restart;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301470
Oleg Nesterov1b08e9072012-09-14 18:52:10 +02001471 utask = current->utask;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301472 if (!utask) {
1473 utask = add_utask();
1474 /* Cannot allocate; re-execute the instruction. */
1475 if (!utask)
Oleg Nesterov0578a972012-09-14 18:31:23 +02001476 goto restart;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301477 }
Oleg Nesterov746a9e62012-09-14 18:23:51 +02001478
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301479 handler_chain(uprobe, regs);
Oleg Nesterov0578a972012-09-14 18:31:23 +02001480 if (can_skip_sstep(uprobe, regs))
1481 goto out;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301482
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301483 if (!pre_ssout(uprobe, regs, bp_vaddr)) {
Sebastian Andrzej Siewior9d778782012-08-07 18:12:28 +02001484 arch_uprobe_enable_step(&uprobe->arch);
Oleg Nesterov746a9e62012-09-14 18:23:51 +02001485 utask->active_uprobe = uprobe;
1486 utask->state = UTASK_SSTEP;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301487 return;
1488 }
1489
Oleg Nesterov0578a972012-09-14 18:31:23 +02001490restart:
1491 /*
1492 * cannot singlestep; cannot skip instruction;
1493 * re-execute the instruction.
1494 */
1495 instruction_pointer_set(regs, bp_vaddr);
1496out:
Sebastian Andrzej Siewior8bd87442012-08-07 18:12:30 +02001497 put_uprobe(uprobe);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301498}
1499
1500/*
1501 * Perform required fix-ups and disable singlestep.
1502 * Allow pending signals to take effect.
1503 */
1504static void handle_singlestep(struct uprobe_task *utask, struct pt_regs *regs)
1505{
1506 struct uprobe *uprobe;
1507
1508 uprobe = utask->active_uprobe;
1509 if (utask->state == UTASK_SSTEP_ACK)
1510 arch_uprobe_post_xol(&uprobe->arch, regs);
1511 else if (utask->state == UTASK_SSTEP_TRAPPED)
1512 arch_uprobe_abort_xol(&uprobe->arch, regs);
1513 else
1514 WARN_ON_ONCE(1);
1515
Sebastian Andrzej Siewior9d778782012-08-07 18:12:28 +02001516 arch_uprobe_disable_step(&uprobe->arch);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301517 put_uprobe(uprobe);
1518 utask->active_uprobe = NULL;
1519 utask->state = UTASK_RUNNING;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301520 xol_free_insn_slot(current);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301521
1522 spin_lock_irq(&current->sighand->siglock);
1523 recalc_sigpending(); /* see uprobe_deny_signal() */
1524 spin_unlock_irq(&current->sighand->siglock);
1525}
1526
1527/*
Oleg Nesterov1b08e9072012-09-14 18:52:10 +02001528 * On breakpoint hit, breakpoint notifier sets the TIF_UPROBE flag and
1529 * allows the thread to return from interrupt. After that handle_swbp()
1530 * sets utask->active_uprobe.
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301531 *
Oleg Nesterov1b08e9072012-09-14 18:52:10 +02001532 * On singlestep exception, singlestep notifier sets the TIF_UPROBE flag
1533 * and allows the thread to return from interrupt.
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301534 *
1535 * While returning to userspace, thread notices the TIF_UPROBE flag and calls
1536 * uprobe_notify_resume().
1537 */
1538void uprobe_notify_resume(struct pt_regs *regs)
1539{
1540 struct uprobe_task *utask;
1541
Oleg Nesterovdb023ea2012-09-14 19:05:46 +02001542 clear_thread_flag(TIF_UPROBE);
1543
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301544 utask = current->utask;
Oleg Nesterov1b08e9072012-09-14 18:52:10 +02001545 if (utask && utask->active_uprobe)
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301546 handle_singlestep(utask, regs);
Oleg Nesterov1b08e9072012-09-14 18:52:10 +02001547 else
1548 handle_swbp(regs);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301549}
1550
1551/*
1552 * uprobe_pre_sstep_notifier gets called from interrupt context as part of
1553 * notifier mechanism. Set TIF_UPROBE flag and indicate breakpoint hit.
1554 */
1555int uprobe_pre_sstep_notifier(struct pt_regs *regs)
1556{
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +02001557 if (!current->mm || !test_bit(MMF_HAS_UPROBES, &current->mm->flags))
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301558 return 0;
1559
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301560 set_thread_flag(TIF_UPROBE);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301561 return 1;
1562}
1563
1564/*
1565 * uprobe_post_sstep_notifier gets called in interrupt context as part of notifier
1566 * mechanism. Set TIF_UPROBE flag and indicate completion of singlestep.
1567 */
1568int uprobe_post_sstep_notifier(struct pt_regs *regs)
1569{
1570 struct uprobe_task *utask = current->utask;
1571
1572 if (!current->mm || !utask || !utask->active_uprobe)
1573 /* task is currently not uprobed */
1574 return 0;
1575
1576 utask->state = UTASK_SSTEP_ACK;
1577 set_thread_flag(TIF_UPROBE);
1578 return 1;
1579}
1580
1581static struct notifier_block uprobe_exception_nb = {
1582 .notifier_call = arch_uprobe_exception_notify,
1583 .priority = INT_MAX-1, /* notified after kprobes, kgdb */
1584};
1585
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301586static int __init init_uprobes(void)
1587{
1588 int i;
1589
1590 for (i = 0; i < UPROBES_HASH_SZ; i++) {
1591 mutex_init(&uprobes_mutex[i]);
1592 mutex_init(&uprobes_mmap_mutex[i]);
1593 }
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301594
1595 return register_die_notifier(&uprobe_exception_nb);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301596}
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301597module_init(init_uprobes);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301598
1599static void __exit exit_uprobes(void)
1600{
1601}
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301602module_exit(exit_uprobes);