blob: c92651d619ca9ddacc05334439322a267fdaa365 [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 */
Oleg Nesterov71434f22012-09-30 21:12:44 +020082#define UPROBE_COPY_INSN 0
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +020083/* Dont run handlers when first register/ last unregister in progress*/
Oleg Nesterov71434f22012-09-30 21:12:44 +020084#define UPROBE_RUN_HANDLER 1
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +020085/* Can skip singlestep */
Oleg Nesterov71434f22012-09-30 21:12:44 +020086#define UPROBE_SKIP_SSTEP 2
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +020087
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;
Oleg Nesterov4710f052012-09-30 20:31:41 +020092 struct mutex copy_mutex; /* TODO: kill me and UPROBE_COPY_INSN */
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +053093 struct list_head pending_list;
94 struct uprobe_consumer *consumers;
95 struct inode *inode; /* Also hold a ref to inode */
96 loff_t offset;
Oleg Nesterov71434f22012-09-30 21:12:44 +020097 unsigned long flags;
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +053098 struct arch_uprobe arch;
99};
100
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530101/*
102 * valid_vma: Verify if the specified vma is an executable vma
103 * Relax restrictions while unregistering: vm_flags might have
104 * changed after breakpoint was inserted.
105 * - is_register: indicates if we are in register context.
106 * - Return 1 if the specified virtual address is in an
107 * executable vma.
108 */
109static bool valid_vma(struct vm_area_struct *vma, bool is_register)
110{
Oleg Nesterove40cfce2012-09-16 19:31:39 +0200111 vm_flags_t flags = VM_HUGETLB | VM_MAYEXEC | VM_SHARED;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530112
Oleg Nesterove40cfce2012-09-16 19:31:39 +0200113 if (is_register)
114 flags |= VM_WRITE;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530115
Oleg Nesterove40cfce2012-09-16 19:31:39 +0200116 return vma->vm_file && (vma->vm_flags & flags) == VM_MAYEXEC;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530117}
118
Oleg Nesterov57683f72012-07-29 20:22:47 +0200119static unsigned long offset_to_vaddr(struct vm_area_struct *vma, loff_t offset)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530120{
Oleg Nesterov57683f72012-07-29 20:22:47 +0200121 return vma->vm_start + offset - ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530122}
123
Oleg Nesterovcb113b42012-07-29 20:22:42 +0200124static loff_t vaddr_to_offset(struct vm_area_struct *vma, unsigned long vaddr)
125{
126 return ((loff_t)vma->vm_pgoff << PAGE_SHIFT) + (vaddr - vma->vm_start);
127}
128
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530129/**
130 * __replace_page - replace page in vma by new page.
131 * based on replace_page in mm/ksm.c
132 *
133 * @vma: vma that holds the pte pointing to page
Oleg Nesterovc517ee72012-07-29 20:22:16 +0200134 * @addr: address the old @page is mapped at
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530135 * @page: the cowed page we are replacing by kpage
136 * @kpage: the modified page we replace page by
137 *
138 * Returns 0 on success, -EFAULT on failure.
139 */
Oleg Nesterovc517ee72012-07-29 20:22:16 +0200140static int __replace_page(struct vm_area_struct *vma, unsigned long addr,
141 struct page *page, struct page *kpage)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530142{
143 struct mm_struct *mm = vma->vm_mm;
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200144 spinlock_t *ptl;
145 pte_t *ptep;
Oleg Nesterov9f924482012-07-29 20:22:20 +0200146 int err;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530147
Oleg Nesterov194f8dc2012-07-29 20:22:49 +0200148 /* For try_to_free_swap() and munlock_vma_page() below */
Oleg Nesterov9f924482012-07-29 20:22:20 +0200149 lock_page(page);
150
151 err = -EAGAIN;
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200152 ptep = page_check_address(page, mm, addr, &ptl, 0);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530153 if (!ptep)
Oleg Nesterov9f924482012-07-29 20:22:20 +0200154 goto unlock;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530155
156 get_page(kpage);
157 page_add_new_anon_rmap(kpage, vma, addr);
158
Srikar Dronamraju7396fa82012-04-11 16:05:16 +0530159 if (!PageAnon(page)) {
160 dec_mm_counter(mm, MM_FILEPAGES);
161 inc_mm_counter(mm, MM_ANONPAGES);
162 }
163
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530164 flush_cache_page(vma, addr, pte_pfn(*ptep));
165 ptep_clear_flush(vma, addr, ptep);
166 set_pte_at_notify(mm, addr, ptep, mk_pte(kpage, vma->vm_page_prot));
167
168 page_remove_rmap(page);
169 if (!page_mapped(page))
170 try_to_free_swap(page);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530171 pte_unmap_unlock(ptep, ptl);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530172
Oleg Nesterov194f8dc2012-07-29 20:22:49 +0200173 if (vma->vm_flags & VM_LOCKED)
174 munlock_vma_page(page);
175 put_page(page);
176
Oleg Nesterov9f924482012-07-29 20:22:20 +0200177 err = 0;
178 unlock:
179 unlock_page(page);
180 return err;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530181}
182
183/**
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530184 * is_swbp_insn - check if instruction is breakpoint instruction.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530185 * @insn: instruction to be checked.
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530186 * Default implementation of is_swbp_insn
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530187 * Returns true if @insn is a breakpoint instruction.
188 */
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530189bool __weak is_swbp_insn(uprobe_opcode_t *insn)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530190{
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530191 return *insn == UPROBE_SWBP_INSN;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530192}
193
Oleg Nesterovcceb55a2012-09-23 21:10:18 +0200194static void copy_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t *opcode)
195{
196 void *kaddr = kmap_atomic(page);
197 memcpy(opcode, kaddr + (vaddr & ~PAGE_MASK), UPROBE_SWBP_INSN_SIZE);
198 kunmap_atomic(kaddr);
199}
200
Oleg Nesteroved6f6a52012-09-23 21:30:44 +0200201static int verify_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t *new_opcode)
202{
203 uprobe_opcode_t old_opcode;
204 bool is_swbp;
205
206 copy_opcode(page, vaddr, &old_opcode);
207 is_swbp = is_swbp_insn(&old_opcode);
208
209 if (is_swbp_insn(new_opcode)) {
210 if (is_swbp) /* register: already installed? */
211 return 0;
212 } else {
213 if (!is_swbp) /* unregister: was it changed by us? */
Oleg Nesterov076a3652012-09-30 18:54:53 +0200214 return 0;
Oleg Nesteroved6f6a52012-09-23 21:30:44 +0200215 }
216
217 return 1;
218}
219
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530220/*
221 * NOTE:
222 * Expect the breakpoint instruction to be the smallest size instruction for
223 * the architecture. If an arch has variable length instruction and the
224 * breakpoint instruction is not of the smallest length instruction
Oleg Nesterovcceb55a2012-09-23 21:10:18 +0200225 * supported by that architecture then we need to modify is_swbp_at_addr and
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530226 * write_opcode accordingly. This would never be a problem for archs that
227 * have fixed length instructions.
228 */
229
230/*
231 * write_opcode - write the opcode at a given virtual address.
232 * @mm: the probed process address space.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530233 * @vaddr: the virtual address to store the opcode.
234 * @opcode: opcode to be written at @vaddr.
235 *
236 * Called with mm->mmap_sem held (for read and with a reference to
237 * mm).
238 *
239 * For mm @mm, write the opcode at @vaddr.
240 * Return 0 (success) or a negative errno.
241 */
Oleg Nesterovcceb55a2012-09-23 21:10:18 +0200242static int write_opcode(struct mm_struct *mm, unsigned long vaddr,
243 uprobe_opcode_t opcode)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530244{
245 struct page *old_page, *new_page;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530246 void *vaddr_old, *vaddr_new;
247 struct vm_area_struct *vma;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530248 int ret;
Oleg Nesterovf4030722012-07-29 20:22:12 +0200249
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200250retry:
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530251 /* Read the page with vaddr into memory */
Oleg Nesterov75ed82e2012-09-16 17:20:06 +0200252 ret = get_user_pages(NULL, mm, vaddr, 1, 0, 1, &old_page, &vma);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530253 if (ret <= 0)
254 return ret;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100255
Oleg Nesteroved6f6a52012-09-23 21:30:44 +0200256 ret = verify_opcode(old_page, vaddr, &opcode);
257 if (ret <= 0)
258 goto put_old;
259
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530260 ret = -ENOMEM;
261 new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vaddr);
262 if (!new_page)
Oleg Nesterov9f924482012-07-29 20:22:20 +0200263 goto put_old;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530264
265 __SetPageUptodate(new_page);
266
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530267 /* copy the page now that we've got it stable */
268 vaddr_old = kmap_atomic(old_page);
269 vaddr_new = kmap_atomic(new_page);
270
271 memcpy(vaddr_new, vaddr_old, PAGE_SIZE);
Oleg Nesterovd9c4a302012-06-15 17:43:50 +0200272 memcpy(vaddr_new + (vaddr & ~PAGE_MASK), &opcode, UPROBE_SWBP_INSN_SIZE);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530273
274 kunmap_atomic(vaddr_new);
275 kunmap_atomic(vaddr_old);
276
277 ret = anon_vma_prepare(vma);
278 if (ret)
Oleg Nesterov9f924482012-07-29 20:22:20 +0200279 goto put_new;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530280
Oleg Nesterovc517ee72012-07-29 20:22:16 +0200281 ret = __replace_page(vma, vaddr, old_page, new_page);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530282
Oleg Nesterov9f924482012-07-29 20:22:20 +0200283put_new:
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530284 page_cache_release(new_page);
Oleg Nesterov9f924482012-07-29 20:22:20 +0200285put_old:
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100286 put_page(old_page);
287
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200288 if (unlikely(ret == -EAGAIN))
289 goto retry;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530290 return ret;
291}
292
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530293/**
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530294 * set_swbp - store breakpoint at a given address.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530295 * @auprobe: arch specific probepoint information.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530296 * @mm: the probed process address space.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530297 * @vaddr: the virtual address to insert the opcode.
298 *
299 * For mm @mm, store the breakpoint instruction at @vaddr.
300 * Return 0 (success) or a negative errno.
301 */
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530302int __weak set_swbp(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530303{
Oleg Nesterovcceb55a2012-09-23 21:10:18 +0200304 return write_opcode(mm, vaddr, UPROBE_SWBP_INSN);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530305}
306
307/**
308 * set_orig_insn - Restore the original instruction.
309 * @mm: the probed process address space.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530310 * @auprobe: arch specific probepoint information.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530311 * @vaddr: the virtual address to insert the opcode.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530312 *
313 * For mm @mm, restore the original opcode (opcode) at @vaddr.
314 * Return 0 (success) or a negative errno.
315 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100316int __weak
Oleg Nesterovded86e72012-08-08 18:07:03 +0200317set_orig_insn(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530318{
Oleg Nesterovcceb55a2012-09-23 21:10:18 +0200319 return write_opcode(mm, vaddr, *(uprobe_opcode_t *)auprobe->insn);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530320}
321
322static int match_uprobe(struct uprobe *l, struct uprobe *r)
323{
324 if (l->inode < r->inode)
325 return -1;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100326
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530327 if (l->inode > r->inode)
328 return 1;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530329
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100330 if (l->offset < r->offset)
331 return -1;
332
333 if (l->offset > r->offset)
334 return 1;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530335
336 return 0;
337}
338
339static struct uprobe *__find_uprobe(struct inode *inode, loff_t offset)
340{
341 struct uprobe u = { .inode = inode, .offset = offset };
342 struct rb_node *n = uprobes_tree.rb_node;
343 struct uprobe *uprobe;
344 int match;
345
346 while (n) {
347 uprobe = rb_entry(n, struct uprobe, rb_node);
348 match = match_uprobe(&u, uprobe);
349 if (!match) {
350 atomic_inc(&uprobe->ref);
351 return uprobe;
352 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100353
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530354 if (match < 0)
355 n = n->rb_left;
356 else
357 n = n->rb_right;
358 }
359 return NULL;
360}
361
362/*
363 * Find a uprobe corresponding to a given inode:offset
364 * Acquires uprobes_treelock
365 */
366static struct uprobe *find_uprobe(struct inode *inode, loff_t offset)
367{
368 struct uprobe *uprobe;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530369
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200370 spin_lock(&uprobes_treelock);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530371 uprobe = __find_uprobe(inode, offset);
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200372 spin_unlock(&uprobes_treelock);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100373
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530374 return uprobe;
375}
376
377static struct uprobe *__insert_uprobe(struct uprobe *uprobe)
378{
379 struct rb_node **p = &uprobes_tree.rb_node;
380 struct rb_node *parent = NULL;
381 struct uprobe *u;
382 int match;
383
384 while (*p) {
385 parent = *p;
386 u = rb_entry(parent, struct uprobe, rb_node);
387 match = match_uprobe(uprobe, u);
388 if (!match) {
389 atomic_inc(&u->ref);
390 return u;
391 }
392
393 if (match < 0)
394 p = &parent->rb_left;
395 else
396 p = &parent->rb_right;
397
398 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100399
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530400 u = NULL;
401 rb_link_node(&uprobe->rb_node, parent, p);
402 rb_insert_color(&uprobe->rb_node, &uprobes_tree);
403 /* get access + creation ref */
404 atomic_set(&uprobe->ref, 2);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100405
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530406 return u;
407}
408
409/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100410 * Acquire uprobes_treelock.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530411 * Matching uprobe already exists in rbtree;
412 * increment (access refcount) and return the matching uprobe.
413 *
414 * No matching uprobe; insert the uprobe in rb_tree;
415 * get a double refcount (access + creation) and return NULL.
416 */
417static struct uprobe *insert_uprobe(struct uprobe *uprobe)
418{
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530419 struct uprobe *u;
420
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200421 spin_lock(&uprobes_treelock);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530422 u = __insert_uprobe(uprobe);
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200423 spin_unlock(&uprobes_treelock);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100424
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530425 /* For now assume that the instruction need not be single-stepped */
Oleg Nesterov71434f22012-09-30 21:12:44 +0200426 __set_bit(UPROBE_SKIP_SSTEP, &uprobe->flags);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530427
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530428 return u;
429}
430
431static void put_uprobe(struct uprobe *uprobe)
432{
433 if (atomic_dec_and_test(&uprobe->ref))
434 kfree(uprobe);
435}
436
437static struct uprobe *alloc_uprobe(struct inode *inode, loff_t offset)
438{
439 struct uprobe *uprobe, *cur_uprobe;
440
441 uprobe = kzalloc(sizeof(struct uprobe), GFP_KERNEL);
442 if (!uprobe)
443 return NULL;
444
445 uprobe->inode = igrab(inode);
446 uprobe->offset = offset;
447 init_rwsem(&uprobe->consumer_rwsem);
Oleg Nesterov4710f052012-09-30 20:31:41 +0200448 mutex_init(&uprobe->copy_mutex);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530449
450 /* add to uprobes_tree, sorted on inode:offset */
451 cur_uprobe = insert_uprobe(uprobe);
452
453 /* a uprobe exists for this inode:offset combination */
454 if (cur_uprobe) {
455 kfree(uprobe);
456 uprobe = cur_uprobe;
457 iput(inode);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100458 } else {
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530459 atomic_inc(&uprobe_events);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100460 }
461
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530462 return uprobe;
463}
464
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530465static void handler_chain(struct uprobe *uprobe, struct pt_regs *regs)
466{
467 struct uprobe_consumer *uc;
468
Oleg Nesterov71434f22012-09-30 21:12:44 +0200469 if (!test_bit(UPROBE_RUN_HANDLER, &uprobe->flags))
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530470 return;
471
472 down_read(&uprobe->consumer_rwsem);
473 for (uc = uprobe->consumers; uc; uc = uc->next) {
474 if (!uc->filter || uc->filter(uc, current))
475 uc->handler(uc, regs);
476 }
477 up_read(&uprobe->consumer_rwsem);
478}
479
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530480/* Returns the previous consumer */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100481static struct uprobe_consumer *
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530482consumer_add(struct uprobe *uprobe, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530483{
484 down_write(&uprobe->consumer_rwsem);
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530485 uc->next = uprobe->consumers;
486 uprobe->consumers = uc;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530487 up_write(&uprobe->consumer_rwsem);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100488
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530489 return uc->next;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530490}
491
492/*
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530493 * For uprobe @uprobe, delete the consumer @uc.
494 * Return true if the @uc is deleted successfully
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530495 * or return false.
496 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530497static bool consumer_del(struct uprobe *uprobe, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530498{
499 struct uprobe_consumer **con;
500 bool ret = false;
501
502 down_write(&uprobe->consumer_rwsem);
503 for (con = &uprobe->consumers; *con; con = &(*con)->next) {
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530504 if (*con == uc) {
505 *con = uc->next;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530506 ret = true;
507 break;
508 }
509 }
510 up_write(&uprobe->consumer_rwsem);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100511
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530512 return ret;
513}
514
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530515static int
Oleg Nesterovd4366152012-06-15 17:43:42 +0200516__copy_insn(struct address_space *mapping, struct file *filp, char *insn,
Oleg Nesterov593609a2012-06-15 17:43:59 +0200517 unsigned long nbytes, loff_t offset)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530518{
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530519 struct page *page;
520 void *vaddr;
Oleg Nesterov593609a2012-06-15 17:43:59 +0200521 unsigned long off;
522 pgoff_t idx;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530523
524 if (!filp)
525 return -EINVAL;
526
Oleg Nesterovcc359d12012-06-15 17:43:25 +0200527 if (!mapping->a_ops->readpage)
528 return -EIO;
529
Oleg Nesterov593609a2012-06-15 17:43:59 +0200530 idx = offset >> PAGE_CACHE_SHIFT;
531 off = offset & ~PAGE_MASK;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530532
533 /*
534 * Ensure that the page that has the original instruction is
535 * populated and in page-cache.
536 */
537 page = read_mapping_page(mapping, idx, filp);
538 if (IS_ERR(page))
539 return PTR_ERR(page);
540
541 vaddr = kmap_atomic(page);
Oleg Nesterov593609a2012-06-15 17:43:59 +0200542 memcpy(insn, vaddr + off, nbytes);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530543 kunmap_atomic(vaddr);
544 page_cache_release(page);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100545
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530546 return 0;
547}
548
Oleg Nesterovd4366152012-06-15 17:43:42 +0200549static int copy_insn(struct uprobe *uprobe, struct file *filp)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530550{
551 struct address_space *mapping;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530552 unsigned long nbytes;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100553 int bytes;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530554
Oleg Nesterovd4366152012-06-15 17:43:42 +0200555 nbytes = PAGE_SIZE - (uprobe->offset & ~PAGE_MASK);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530556 mapping = uprobe->inode->i_mapping;
557
558 /* Instruction at end of binary; copy only available bytes */
559 if (uprobe->offset + MAX_UINSN_BYTES > uprobe->inode->i_size)
560 bytes = uprobe->inode->i_size - uprobe->offset;
561 else
562 bytes = MAX_UINSN_BYTES;
563
564 /* Instruction at the page-boundary; copy bytes in second page */
565 if (nbytes < bytes) {
Oleg Nesterovfc36f592012-06-15 17:43:44 +0200566 int err = __copy_insn(mapping, filp, uprobe->arch.insn + nbytes,
567 bytes - nbytes, uprobe->offset + nbytes);
568 if (err)
569 return err;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530570 bytes = nbytes;
571 }
Oleg Nesterovd4366152012-06-15 17:43:42 +0200572 return __copy_insn(mapping, filp, uprobe->arch.insn, bytes, uprobe->offset);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530573}
574
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200575static int prepare_uprobe(struct uprobe *uprobe, struct file *file,
576 struct mm_struct *mm, unsigned long vaddr)
577{
578 int ret = 0;
579
Oleg Nesterov71434f22012-09-30 21:12:44 +0200580 if (test_bit(UPROBE_COPY_INSN, &uprobe->flags))
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200581 return ret;
582
Oleg Nesterov4710f052012-09-30 20:31:41 +0200583 mutex_lock(&uprobe->copy_mutex);
Oleg Nesterov71434f22012-09-30 21:12:44 +0200584 if (test_bit(UPROBE_COPY_INSN, &uprobe->flags))
Oleg Nesterov4710f052012-09-30 20:31:41 +0200585 goto out;
586
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200587 ret = copy_insn(uprobe, file);
588 if (ret)
589 goto out;
590
591 ret = -ENOTSUPP;
592 if (is_swbp_insn((uprobe_opcode_t *)uprobe->arch.insn))
593 goto out;
594
595 ret = arch_uprobe_analyze_insn(&uprobe->arch, mm, vaddr);
596 if (ret)
597 goto out;
598
599 /* write_opcode() assumes we don't cross page boundary */
600 BUG_ON((uprobe->offset & ~PAGE_MASK) +
601 UPROBE_SWBP_INSN_SIZE > PAGE_SIZE);
602
603 smp_wmb(); /* pairs with rmb() in find_active_uprobe() */
Oleg Nesterov71434f22012-09-30 21:12:44 +0200604 set_bit(UPROBE_COPY_INSN, &uprobe->flags);
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200605
606 out:
Oleg Nesterov4710f052012-09-30 20:31:41 +0200607 mutex_unlock(&uprobe->copy_mutex);
608
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200609 return ret;
610}
611
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530612static int
613install_breakpoint(struct uprobe *uprobe, struct mm_struct *mm,
Oleg Nesterov816c03f2012-06-15 17:43:55 +0200614 struct vm_area_struct *vma, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530615{
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +0200616 bool first_uprobe;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530617 int ret;
618
619 /*
620 * If probe is being deleted, unregister thread could be done with
621 * the vma-rmap-walk through. Adding a probe now can be fatal since
622 * nobody will be able to cleanup. Also we could be from fork or
623 * mremap path, where the probe might have already been inserted.
624 * Hence behave as if probe already existed.
625 */
626 if (!uprobe->consumers)
Oleg Nesterov78f74112012-08-08 17:35:08 +0200627 return 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530628
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200629 ret = prepare_uprobe(uprobe, vma->vm_file, mm, vaddr);
630 if (ret)
631 return ret;
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530632
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +0200633 /*
634 * set MMF_HAS_UPROBES in advance for uprobe_pre_sstep_notifier(),
635 * the task can hit this breakpoint right after __replace_page().
636 */
637 first_uprobe = !test_bit(MMF_HAS_UPROBES, &mm->flags);
638 if (first_uprobe)
639 set_bit(MMF_HAS_UPROBES, &mm->flags);
640
Oleg Nesterov816c03f2012-06-15 17:43:55 +0200641 ret = set_swbp(&uprobe->arch, mm, vaddr);
Oleg Nesterov9f68f672012-08-19 16:15:09 +0200642 if (!ret)
643 clear_bit(MMF_RECALC_UPROBES, &mm->flags);
644 else if (first_uprobe)
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +0200645 clear_bit(MMF_HAS_UPROBES, &mm->flags);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530646
647 return ret;
648}
649
Oleg Nesterov076a3652012-09-30 18:54:53 +0200650static int
Oleg Nesterov816c03f2012-06-15 17:43:55 +0200651remove_breakpoint(struct uprobe *uprobe, struct mm_struct *mm, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530652{
Oleg Nesterov9f68f672012-08-19 16:15:09 +0200653 /* can happen if uprobe_register() fails */
654 if (!test_bit(MMF_HAS_UPROBES, &mm->flags))
Oleg Nesterov076a3652012-09-30 18:54:53 +0200655 return 0;
Oleg Nesterov9f68f672012-08-19 16:15:09 +0200656
657 set_bit(MMF_RECALC_UPROBES, &mm->flags);
Oleg Nesterov076a3652012-09-30 18:54:53 +0200658 return set_orig_insn(&uprobe->arch, mm, vaddr);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530659}
660
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530661/*
Oleg Nesterov778b0322012-05-29 21:30:08 +0200662 * There could be threads that have already hit the breakpoint. They
663 * will recheck the current insn and restart if find_uprobe() fails.
664 * See find_active_uprobe().
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530665 */
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530666static void delete_uprobe(struct uprobe *uprobe)
667{
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200668 spin_lock(&uprobes_treelock);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530669 rb_erase(&uprobe->rb_node, &uprobes_tree);
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200670 spin_unlock(&uprobes_treelock);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530671 iput(uprobe->inode);
672 put_uprobe(uprobe);
673 atomic_dec(&uprobe_events);
674}
675
Oleg Nesterov26872092012-06-15 17:43:33 +0200676struct map_info {
677 struct map_info *next;
678 struct mm_struct *mm;
Oleg Nesterov816c03f2012-06-15 17:43:55 +0200679 unsigned long vaddr;
Oleg Nesterov26872092012-06-15 17:43:33 +0200680};
681
682static inline struct map_info *free_map_info(struct map_info *info)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530683{
Oleg Nesterov26872092012-06-15 17:43:33 +0200684 struct map_info *next = info->next;
685 kfree(info);
686 return next;
687}
688
689static struct map_info *
690build_map_info(struct address_space *mapping, loff_t offset, bool is_register)
691{
692 unsigned long pgoff = offset >> PAGE_SHIFT;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530693 struct prio_tree_iter iter;
694 struct vm_area_struct *vma;
Oleg Nesterov26872092012-06-15 17:43:33 +0200695 struct map_info *curr = NULL;
696 struct map_info *prev = NULL;
697 struct map_info *info;
698 int more = 0;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100699
Oleg Nesterov26872092012-06-15 17:43:33 +0200700 again:
701 mutex_lock(&mapping->i_mmap_mutex);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530702 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
703 if (!valid_vma(vma, is_register))
704 continue;
705
Oleg Nesterov7a5bfb62012-06-15 17:43:36 +0200706 if (!prev && !more) {
707 /*
708 * Needs GFP_NOWAIT to avoid i_mmap_mutex recursion through
709 * reclaim. This is optimistic, no harm done if it fails.
710 */
711 prev = kmalloc(sizeof(struct map_info),
712 GFP_NOWAIT | __GFP_NOMEMALLOC | __GFP_NOWARN);
713 if (prev)
714 prev->next = NULL;
715 }
Oleg Nesterov26872092012-06-15 17:43:33 +0200716 if (!prev) {
717 more++;
718 continue;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530719 }
720
Oleg Nesterov26872092012-06-15 17:43:33 +0200721 if (!atomic_inc_not_zero(&vma->vm_mm->mm_users))
722 continue;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100723
Oleg Nesterov26872092012-06-15 17:43:33 +0200724 info = prev;
725 prev = prev->next;
726 info->next = curr;
727 curr = info;
728
729 info->mm = vma->vm_mm;
Oleg Nesterov57683f72012-07-29 20:22:47 +0200730 info->vaddr = offset_to_vaddr(vma, offset);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530731 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530732 mutex_unlock(&mapping->i_mmap_mutex);
733
Oleg Nesterov26872092012-06-15 17:43:33 +0200734 if (!more)
735 goto out;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100736
Oleg Nesterov26872092012-06-15 17:43:33 +0200737 prev = curr;
738 while (curr) {
739 mmput(curr->mm);
740 curr = curr->next;
741 }
742
743 do {
744 info = kmalloc(sizeof(struct map_info), GFP_KERNEL);
745 if (!info) {
746 curr = ERR_PTR(-ENOMEM);
747 goto out;
748 }
749 info->next = prev;
750 prev = info;
751 } while (--more);
752
753 goto again;
754 out:
755 while (prev)
756 prev = free_map_info(prev);
757 return curr;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530758}
759
760static int register_for_each_vma(struct uprobe *uprobe, bool is_register)
761{
Oleg Nesterov26872092012-06-15 17:43:33 +0200762 struct map_info *info;
763 int err = 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530764
Oleg Nesterov26872092012-06-15 17:43:33 +0200765 info = build_map_info(uprobe->inode->i_mapping,
766 uprobe->offset, is_register);
767 if (IS_ERR(info))
768 return PTR_ERR(info);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100769
Oleg Nesterov26872092012-06-15 17:43:33 +0200770 while (info) {
771 struct mm_struct *mm = info->mm;
772 struct vm_area_struct *vma;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100773
Oleg Nesterov076a3652012-09-30 18:54:53 +0200774 if (err && is_register)
Oleg Nesterov26872092012-06-15 17:43:33 +0200775 goto free;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100776
Oleg Nesterov77fc4af2012-05-29 21:29:28 +0200777 down_write(&mm->mmap_sem);
Oleg Nesterovf4d6dfe2012-07-29 20:22:44 +0200778 vma = find_vma(mm, info->vaddr);
779 if (!vma || !valid_vma(vma, is_register) ||
780 vma->vm_file->f_mapping->host != uprobe->inode)
Oleg Nesterov26872092012-06-15 17:43:33 +0200781 goto unlock;
782
Oleg Nesterovf4d6dfe2012-07-29 20:22:44 +0200783 if (vma->vm_start > info->vaddr ||
784 vaddr_to_offset(vma, info->vaddr) != uprobe->offset)
Oleg Nesterov26872092012-06-15 17:43:33 +0200785 goto unlock;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530786
Oleg Nesterov78f74112012-08-08 17:35:08 +0200787 if (is_register)
Oleg Nesterov26872092012-06-15 17:43:33 +0200788 err = install_breakpoint(uprobe, mm, vma, info->vaddr);
Oleg Nesterov78f74112012-08-08 17:35:08 +0200789 else
Oleg Nesterov076a3652012-09-30 18:54:53 +0200790 err |= remove_breakpoint(uprobe, mm, info->vaddr);
Oleg Nesterov78f74112012-08-08 17:35:08 +0200791
Oleg Nesterov26872092012-06-15 17:43:33 +0200792 unlock:
793 up_write(&mm->mmap_sem);
794 free:
795 mmput(mm);
796 info = free_map_info(info);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530797 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100798
Oleg Nesterov26872092012-06-15 17:43:33 +0200799 return err;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530800}
801
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100802static int __uprobe_register(struct uprobe *uprobe)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530803{
804 return register_for_each_vma(uprobe, true);
805}
806
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100807static void __uprobe_unregister(struct uprobe *uprobe)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530808{
809 if (!register_for_each_vma(uprobe, false))
810 delete_uprobe(uprobe);
811
812 /* TODO : cant unregister? schedule a worker thread */
813}
814
815/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100816 * uprobe_register - register a probe
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530817 * @inode: the file in which the probe has to be placed.
818 * @offset: offset from the start of the file.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530819 * @uc: information on howto handle the probe..
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530820 *
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100821 * Apart from the access refcount, uprobe_register() takes a creation
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530822 * refcount (thro alloc_uprobe) if and only if this @uprobe is getting
823 * inserted into the rbtree (i.e first consumer for a @inode:@offset
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100824 * tuple). Creation refcount stops uprobe_unregister from freeing the
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530825 * @uprobe even before the register operation is complete. Creation
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530826 * refcount is released when the last @uc for the @uprobe
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530827 * unregisters.
828 *
829 * Return errno if it cannot successully install probes
830 * else return 0 (success)
831 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530832int uprobe_register(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530833{
834 struct uprobe *uprobe;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100835 int ret;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530836
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530837 if (!inode || !uc || uc->next)
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100838 return -EINVAL;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530839
840 if (offset > i_size_read(inode))
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100841 return -EINVAL;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530842
843 ret = 0;
844 mutex_lock(uprobes_hash(inode));
845 uprobe = alloc_uprobe(inode, offset);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100846
Oleg Nesterova5f658b2012-09-30 18:21:09 +0200847 if (!uprobe) {
848 ret = -ENOMEM;
849 } else if (!consumer_add(uprobe, uc)) {
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100850 ret = __uprobe_register(uprobe);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530851 if (ret) {
852 uprobe->consumers = NULL;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100853 __uprobe_unregister(uprobe);
854 } else {
Oleg Nesterov71434f22012-09-30 21:12:44 +0200855 set_bit(UPROBE_RUN_HANDLER, &uprobe->flags);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100856 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530857 }
858
859 mutex_unlock(uprobes_hash(inode));
Sebastian Andrzej Siewior6d1d8df2012-08-30 19:26:22 +0200860 if (uprobe)
861 put_uprobe(uprobe);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530862
863 return ret;
864}
865
866/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100867 * uprobe_unregister - unregister a already registered probe.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530868 * @inode: the file in which the probe has to be removed.
869 * @offset: offset from the start of the file.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530870 * @uc: identify which probe if multiple probes are colocated.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530871 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530872void uprobe_unregister(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530873{
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100874 struct uprobe *uprobe;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530875
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530876 if (!inode || !uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530877 return;
878
879 uprobe = find_uprobe(inode, offset);
880 if (!uprobe)
881 return;
882
883 mutex_lock(uprobes_hash(inode));
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530884
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530885 if (consumer_del(uprobe, uc)) {
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100886 if (!uprobe->consumers) {
887 __uprobe_unregister(uprobe);
Oleg Nesterov71434f22012-09-30 21:12:44 +0200888 clear_bit(UPROBE_RUN_HANDLER, &uprobe->flags);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100889 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530890 }
891
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530892 mutex_unlock(uprobes_hash(inode));
893 if (uprobe)
894 put_uprobe(uprobe);
895}
896
Oleg Nesterov891c3972012-07-29 20:22:40 +0200897static struct rb_node *
898find_node_in_range(struct inode *inode, loff_t min, loff_t max)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530899{
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530900 struct rb_node *n = uprobes_tree.rb_node;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530901
902 while (n) {
Oleg Nesterov891c3972012-07-29 20:22:40 +0200903 struct uprobe *u = rb_entry(n, struct uprobe, rb_node);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100904
Oleg Nesterov891c3972012-07-29 20:22:40 +0200905 if (inode < u->inode) {
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530906 n = n->rb_left;
Oleg Nesterov891c3972012-07-29 20:22:40 +0200907 } else if (inode > u->inode) {
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530908 n = n->rb_right;
Oleg Nesterov891c3972012-07-29 20:22:40 +0200909 } else {
910 if (max < u->offset)
911 n = n->rb_left;
912 else if (min > u->offset)
913 n = n->rb_right;
914 else
915 break;
916 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530917 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100918
Oleg Nesterov891c3972012-07-29 20:22:40 +0200919 return n;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530920}
921
922/*
Oleg Nesterov891c3972012-07-29 20:22:40 +0200923 * For a given range in vma, build a list of probes that need to be inserted.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530924 */
Oleg Nesterov891c3972012-07-29 20:22:40 +0200925static void build_probe_list(struct inode *inode,
926 struct vm_area_struct *vma,
927 unsigned long start, unsigned long end,
928 struct list_head *head)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530929{
Oleg Nesterov891c3972012-07-29 20:22:40 +0200930 loff_t min, max;
Oleg Nesterov891c3972012-07-29 20:22:40 +0200931 struct rb_node *n, *t;
932 struct uprobe *u;
933
934 INIT_LIST_HEAD(head);
Oleg Nesterovcb113b42012-07-29 20:22:42 +0200935 min = vaddr_to_offset(vma, start);
Oleg Nesterov891c3972012-07-29 20:22:40 +0200936 max = min + (end - start) - 1;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530937
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200938 spin_lock(&uprobes_treelock);
Oleg Nesterov891c3972012-07-29 20:22:40 +0200939 n = find_node_in_range(inode, min, max);
940 if (n) {
941 for (t = n; t; t = rb_prev(t)) {
942 u = rb_entry(t, struct uprobe, rb_node);
943 if (u->inode != inode || u->offset < min)
944 break;
945 list_add(&u->pending_list, head);
946 atomic_inc(&u->ref);
947 }
948 for (t = n; (t = rb_next(t)); ) {
949 u = rb_entry(t, struct uprobe, rb_node);
950 if (u->inode != inode || u->offset > max)
951 break;
952 list_add(&u->pending_list, head);
953 atomic_inc(&u->ref);
954 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530955 }
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200956 spin_unlock(&uprobes_treelock);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530957}
958
959/*
Oleg Nesterov5e5be712012-08-06 14:49:56 +0200960 * Called from mmap_region/vma_adjust with mm->mmap_sem acquired.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530961 *
Oleg Nesterov5e5be712012-08-06 14:49:56 +0200962 * Currently we ignore all errors and always return 0, the callers
963 * can't handle the failure anyway.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530964 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100965int uprobe_mmap(struct vm_area_struct *vma)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530966{
967 struct list_head tmp_list;
Oleg Nesterov665605a2012-07-29 20:22:29 +0200968 struct uprobe *uprobe, *u;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530969 struct inode *inode;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530970
971 if (!atomic_read(&uprobe_events) || !valid_vma(vma, true))
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100972 return 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530973
974 inode = vma->vm_file->f_mapping->host;
975 if (!inode)
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100976 return 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530977
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530978 mutex_lock(uprobes_mmap_hash(inode));
Oleg Nesterov891c3972012-07-29 20:22:40 +0200979 build_probe_list(inode, vma, vma->vm_start, vma->vm_end, &tmp_list);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100980
Oleg Nesterov665605a2012-07-29 20:22:29 +0200981 list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
Oleg Nesterov5e5be712012-08-06 14:49:56 +0200982 if (!fatal_signal_pending(current)) {
Oleg Nesterov57683f72012-07-29 20:22:47 +0200983 unsigned long vaddr = offset_to_vaddr(vma, uprobe->offset);
Oleg Nesterov5e5be712012-08-06 14:49:56 +0200984 install_breakpoint(uprobe, vma->vm_mm, vma, vaddr);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530985 }
986 put_uprobe(uprobe);
987 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530988 mutex_unlock(uprobes_mmap_hash(inode));
989
Oleg Nesterov5e5be712012-08-06 14:49:56 +0200990 return 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530991}
992
Oleg Nesterov9f68f672012-08-19 16:15:09 +0200993static bool
994vma_has_uprobes(struct vm_area_struct *vma, unsigned long start, unsigned long end)
995{
996 loff_t min, max;
997 struct inode *inode;
998 struct rb_node *n;
999
1000 inode = vma->vm_file->f_mapping->host;
1001
1002 min = vaddr_to_offset(vma, start);
1003 max = min + (end - start) - 1;
1004
1005 spin_lock(&uprobes_treelock);
1006 n = find_node_in_range(inode, min, max);
1007 spin_unlock(&uprobes_treelock);
1008
1009 return !!n;
1010}
1011
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301012/*
1013 * Called in context of a munmap of a vma.
1014 */
Srikar Dronamrajucbc91f72012-04-11 16:05:27 +05301015void uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end)
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301016{
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301017 if (!atomic_read(&uprobe_events) || !valid_vma(vma, false))
1018 return;
1019
Oleg Nesterov2fd611a2012-07-29 20:22:31 +02001020 if (!atomic_read(&vma->vm_mm->mm_users)) /* called by mmput() ? */
1021 return;
1022
Oleg Nesterov9f68f672012-08-19 16:15:09 +02001023 if (!test_bit(MMF_HAS_UPROBES, &vma->vm_mm->flags) ||
1024 test_bit(MMF_RECALC_UPROBES, &vma->vm_mm->flags))
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +02001025 return;
1026
Oleg Nesterov9f68f672012-08-19 16:15:09 +02001027 if (vma_has_uprobes(vma, start, end))
1028 set_bit(MMF_RECALC_UPROBES, &vma->vm_mm->flags);
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301029}
1030
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301031/* Slot allocation for XOL */
1032static int xol_add_vma(struct xol_area *area)
1033{
1034 struct mm_struct *mm;
1035 int ret;
1036
1037 area->page = alloc_page(GFP_HIGHUSER);
1038 if (!area->page)
1039 return -ENOMEM;
1040
1041 ret = -EALREADY;
1042 mm = current->mm;
1043
1044 down_write(&mm->mmap_sem);
1045 if (mm->uprobes_state.xol_area)
1046 goto fail;
1047
1048 ret = -ENOMEM;
1049
1050 /* Try to map as high as possible, this is only a hint. */
1051 area->vaddr = get_unmapped_area(NULL, TASK_SIZE - PAGE_SIZE, PAGE_SIZE, 0, 0);
1052 if (area->vaddr & ~PAGE_MASK) {
1053 ret = area->vaddr;
1054 goto fail;
1055 }
1056
1057 ret = install_special_mapping(mm, area->vaddr, PAGE_SIZE,
1058 VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO, &area->page);
1059 if (ret)
1060 goto fail;
1061
1062 smp_wmb(); /* pairs with get_xol_area() */
1063 mm->uprobes_state.xol_area = area;
1064 ret = 0;
1065
1066fail:
1067 up_write(&mm->mmap_sem);
1068 if (ret)
1069 __free_page(area->page);
1070
1071 return ret;
1072}
1073
1074static struct xol_area *get_xol_area(struct mm_struct *mm)
1075{
1076 struct xol_area *area;
1077
1078 area = mm->uprobes_state.xol_area;
1079 smp_read_barrier_depends(); /* pairs with wmb in xol_add_vma() */
1080
1081 return area;
1082}
1083
1084/*
1085 * xol_alloc_area - Allocate process's xol_area.
1086 * This area will be used for storing instructions for execution out of
1087 * line.
1088 *
1089 * Returns the allocated area or NULL.
1090 */
1091static struct xol_area *xol_alloc_area(void)
1092{
1093 struct xol_area *area;
1094
1095 area = kzalloc(sizeof(*area), GFP_KERNEL);
1096 if (unlikely(!area))
1097 return NULL;
1098
1099 area->bitmap = kzalloc(BITS_TO_LONGS(UINSNS_PER_PAGE) * sizeof(long), GFP_KERNEL);
1100
1101 if (!area->bitmap)
1102 goto fail;
1103
1104 init_waitqueue_head(&area->wq);
1105 if (!xol_add_vma(area))
1106 return area;
1107
1108fail:
1109 kfree(area->bitmap);
1110 kfree(area);
1111
1112 return get_xol_area(current->mm);
1113}
1114
1115/*
1116 * uprobe_clear_state - Free the area allocated for slots.
1117 */
1118void uprobe_clear_state(struct mm_struct *mm)
1119{
1120 struct xol_area *area = mm->uprobes_state.xol_area;
1121
1122 if (!area)
1123 return;
1124
1125 put_page(area->page);
1126 kfree(area->bitmap);
1127 kfree(area);
1128}
1129
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +02001130void uprobe_dup_mmap(struct mm_struct *oldmm, struct mm_struct *newmm)
1131{
Oleg Nesterov61559a82012-08-08 17:17:46 +02001132 newmm->uprobes_state.xol_area = NULL;
1133
Oleg Nesterov9f68f672012-08-19 16:15:09 +02001134 if (test_bit(MMF_HAS_UPROBES, &oldmm->flags)) {
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +02001135 set_bit(MMF_HAS_UPROBES, &newmm->flags);
Oleg Nesterov9f68f672012-08-19 16:15:09 +02001136 /* unconditionally, dup_mmap() skips VM_DONTCOPY vmas */
1137 set_bit(MMF_RECALC_UPROBES, &newmm->flags);
1138 }
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +02001139}
1140
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301141/*
1142 * - search for a free slot.
1143 */
1144static unsigned long xol_take_insn_slot(struct xol_area *area)
1145{
1146 unsigned long slot_addr;
1147 int slot_nr;
1148
1149 do {
1150 slot_nr = find_first_zero_bit(area->bitmap, UINSNS_PER_PAGE);
1151 if (slot_nr < UINSNS_PER_PAGE) {
1152 if (!test_and_set_bit(slot_nr, area->bitmap))
1153 break;
1154
1155 slot_nr = UINSNS_PER_PAGE;
1156 continue;
1157 }
1158 wait_event(area->wq, (atomic_read(&area->slot_count) < UINSNS_PER_PAGE));
1159 } while (slot_nr >= UINSNS_PER_PAGE);
1160
1161 slot_addr = area->vaddr + (slot_nr * UPROBE_XOL_SLOT_BYTES);
1162 atomic_inc(&area->slot_count);
1163
1164 return slot_addr;
1165}
1166
1167/*
1168 * xol_get_insn_slot - If was not allocated a slot, then
1169 * allocate a slot.
1170 * Returns the allocated slot address or 0.
1171 */
1172static unsigned long xol_get_insn_slot(struct uprobe *uprobe, unsigned long slot_addr)
1173{
1174 struct xol_area *area;
1175 unsigned long offset;
1176 void *vaddr;
1177
1178 area = get_xol_area(current->mm);
1179 if (!area) {
1180 area = xol_alloc_area();
1181 if (!area)
1182 return 0;
1183 }
1184 current->utask->xol_vaddr = xol_take_insn_slot(area);
1185
1186 /*
1187 * Initialize the slot if xol_vaddr points to valid
1188 * instruction slot.
1189 */
1190 if (unlikely(!current->utask->xol_vaddr))
1191 return 0;
1192
1193 current->utask->vaddr = slot_addr;
1194 offset = current->utask->xol_vaddr & ~PAGE_MASK;
1195 vaddr = kmap_atomic(area->page);
1196 memcpy(vaddr + offset, uprobe->arch.insn, MAX_UINSN_BYTES);
1197 kunmap_atomic(vaddr);
1198
1199 return current->utask->xol_vaddr;
1200}
1201
1202/*
1203 * xol_free_insn_slot - If slot was earlier allocated by
1204 * @xol_get_insn_slot(), make the slot available for
1205 * subsequent requests.
1206 */
1207static void xol_free_insn_slot(struct task_struct *tsk)
1208{
1209 struct xol_area *area;
1210 unsigned long vma_end;
1211 unsigned long slot_addr;
1212
1213 if (!tsk->mm || !tsk->mm->uprobes_state.xol_area || !tsk->utask)
1214 return;
1215
1216 slot_addr = tsk->utask->xol_vaddr;
1217
1218 if (unlikely(!slot_addr || IS_ERR_VALUE(slot_addr)))
1219 return;
1220
1221 area = tsk->mm->uprobes_state.xol_area;
1222 vma_end = area->vaddr + PAGE_SIZE;
1223 if (area->vaddr <= slot_addr && slot_addr < vma_end) {
1224 unsigned long offset;
1225 int slot_nr;
1226
1227 offset = slot_addr - area->vaddr;
1228 slot_nr = offset / UPROBE_XOL_SLOT_BYTES;
1229 if (slot_nr >= UINSNS_PER_PAGE)
1230 return;
1231
1232 clear_bit(slot_nr, area->bitmap);
1233 atomic_dec(&area->slot_count);
1234 if (waitqueue_active(&area->wq))
1235 wake_up(&area->wq);
1236
1237 tsk->utask->xol_vaddr = 0;
1238 }
1239}
1240
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301241/**
1242 * uprobe_get_swbp_addr - compute address of swbp given post-swbp regs
1243 * @regs: Reflects the saved state of the task after it has hit a breakpoint
1244 * instruction.
1245 * Return the address of the breakpoint instruction.
1246 */
1247unsigned long __weak uprobe_get_swbp_addr(struct pt_regs *regs)
1248{
1249 return instruction_pointer(regs) - UPROBE_SWBP_INSN_SIZE;
1250}
1251
1252/*
1253 * Called with no locks held.
1254 * Called in context of a exiting or a exec-ing thread.
1255 */
1256void uprobe_free_utask(struct task_struct *t)
1257{
1258 struct uprobe_task *utask = t->utask;
1259
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301260 if (!utask)
1261 return;
1262
1263 if (utask->active_uprobe)
1264 put_uprobe(utask->active_uprobe);
1265
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301266 xol_free_insn_slot(t);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301267 kfree(utask);
1268 t->utask = NULL;
1269}
1270
1271/*
1272 * Called in context of a new clone/fork from copy_process.
1273 */
1274void uprobe_copy_process(struct task_struct *t)
1275{
1276 t->utask = NULL;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301277}
1278
1279/*
1280 * Allocate a uprobe_task object for the task.
1281 * Called when the thread hits a breakpoint for the first time.
1282 *
1283 * Returns:
1284 * - pointer to new uprobe_task on success
1285 * - NULL otherwise
1286 */
1287static struct uprobe_task *add_utask(void)
1288{
1289 struct uprobe_task *utask;
1290
1291 utask = kzalloc(sizeof *utask, GFP_KERNEL);
1292 if (unlikely(!utask))
1293 return NULL;
1294
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301295 current->utask = utask;
1296 return utask;
1297}
1298
1299/* Prepare to single-step probed instruction out of line. */
1300static int
1301pre_ssout(struct uprobe *uprobe, struct pt_regs *regs, unsigned long vaddr)
1302{
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301303 if (xol_get_insn_slot(uprobe, vaddr) && !arch_uprobe_pre_xol(&uprobe->arch, regs))
1304 return 0;
1305
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301306 return -EFAULT;
1307}
1308
1309/*
1310 * If we are singlestepping, then ensure this thread is not connected to
1311 * non-fatal signals until completion of singlestep. When xol insn itself
1312 * triggers the signal, restart the original insn even if the task is
1313 * already SIGKILL'ed (since coredump should report the correct ip). This
1314 * is even more important if the task has a handler for SIGSEGV/etc, The
1315 * _same_ instruction should be repeated again after return from the signal
1316 * handler, and SSTEP can never finish in this case.
1317 */
1318bool uprobe_deny_signal(void)
1319{
1320 struct task_struct *t = current;
1321 struct uprobe_task *utask = t->utask;
1322
1323 if (likely(!utask || !utask->active_uprobe))
1324 return false;
1325
1326 WARN_ON_ONCE(utask->state != UTASK_SSTEP);
1327
1328 if (signal_pending(t)) {
1329 spin_lock_irq(&t->sighand->siglock);
1330 clear_tsk_thread_flag(t, TIF_SIGPENDING);
1331 spin_unlock_irq(&t->sighand->siglock);
1332
1333 if (__fatal_signal_pending(t) || arch_uprobe_xol_was_trapped(t)) {
1334 utask->state = UTASK_SSTEP_TRAPPED;
1335 set_tsk_thread_flag(t, TIF_UPROBE);
1336 set_tsk_thread_flag(t, TIF_NOTIFY_RESUME);
1337 }
1338 }
1339
1340 return true;
1341}
1342
1343/*
1344 * Avoid singlestepping the original instruction if the original instruction
1345 * is a NOP or can be emulated.
1346 */
1347static bool can_skip_sstep(struct uprobe *uprobe, struct pt_regs *regs)
1348{
Oleg Nesterov71434f22012-09-30 21:12:44 +02001349 if (test_bit(UPROBE_SKIP_SSTEP, &uprobe->flags)) {
Oleg Nesterov0578a972012-09-14 18:31:23 +02001350 if (arch_uprobe_skip_sstep(&uprobe->arch, regs))
1351 return true;
Oleg Nesterov71434f22012-09-30 21:12:44 +02001352 clear_bit(UPROBE_SKIP_SSTEP, &uprobe->flags);
Oleg Nesterov0578a972012-09-14 18:31:23 +02001353 }
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301354 return false;
1355}
1356
Oleg Nesterov499a4f32012-08-19 17:41:34 +02001357static void mmf_recalc_uprobes(struct mm_struct *mm)
1358{
1359 struct vm_area_struct *vma;
1360
1361 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1362 if (!valid_vma(vma, false))
1363 continue;
1364 /*
1365 * This is not strictly accurate, we can race with
1366 * uprobe_unregister() and see the already removed
1367 * uprobe if delete_uprobe() was not yet called.
1368 */
1369 if (vma_has_uprobes(vma, vma->vm_start, vma->vm_end))
1370 return;
1371 }
1372
1373 clear_bit(MMF_HAS_UPROBES, &mm->flags);
1374}
1375
Oleg Nesterovec75fba2012-09-23 21:55:19 +02001376static int is_swbp_at_addr(struct mm_struct *mm, unsigned long vaddr)
1377{
1378 struct page *page;
1379 uprobe_opcode_t opcode;
1380 int result;
1381
1382 pagefault_disable();
1383 result = __copy_from_user_inatomic(&opcode, (void __user*)vaddr,
1384 sizeof(opcode));
1385 pagefault_enable();
1386
1387 if (likely(result == 0))
1388 goto out;
1389
1390 result = get_user_pages(NULL, mm, vaddr, 1, 0, 1, &page, NULL);
1391 if (result < 0)
1392 return result;
1393
1394 copy_opcode(page, vaddr, &opcode);
1395 put_page(page);
1396 out:
1397 return is_swbp_insn(&opcode);
1398}
1399
Oleg Nesterovd790d342012-05-29 21:29:14 +02001400static struct uprobe *find_active_uprobe(unsigned long bp_vaddr, int *is_swbp)
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001401{
1402 struct mm_struct *mm = current->mm;
1403 struct uprobe *uprobe = NULL;
1404 struct vm_area_struct *vma;
1405
1406 down_read(&mm->mmap_sem);
1407 vma = find_vma(mm, bp_vaddr);
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001408 if (vma && vma->vm_start <= bp_vaddr) {
1409 if (valid_vma(vma, false)) {
Oleg Nesterovcb113b42012-07-29 20:22:42 +02001410 struct inode *inode = vma->vm_file->f_mapping->host;
1411 loff_t offset = vaddr_to_offset(vma, bp_vaddr);
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001412
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001413 uprobe = find_uprobe(inode, offset);
1414 }
Oleg Nesterovd790d342012-05-29 21:29:14 +02001415
1416 if (!uprobe)
1417 *is_swbp = is_swbp_at_addr(mm, bp_vaddr);
1418 } else {
1419 *is_swbp = -EFAULT;
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001420 }
Oleg Nesterov499a4f32012-08-19 17:41:34 +02001421
1422 if (!uprobe && test_and_clear_bit(MMF_RECALC_UPROBES, &mm->flags))
1423 mmf_recalc_uprobes(mm);
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001424 up_read(&mm->mmap_sem);
1425
1426 return uprobe;
1427}
1428
Sebastian Andrzej Siewior9d778782012-08-07 18:12:28 +02001429void __weak arch_uprobe_enable_step(struct arch_uprobe *arch)
1430{
1431 user_enable_single_step(current);
1432}
1433
1434void __weak arch_uprobe_disable_step(struct arch_uprobe *arch)
1435{
1436 user_disable_single_step(current);
1437}
1438
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301439/*
1440 * Run handler and ask thread to singlestep.
1441 * Ensure all non-fatal signals cannot interrupt thread while it singlesteps.
1442 */
1443static void handle_swbp(struct pt_regs *regs)
1444{
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301445 struct uprobe_task *utask;
1446 struct uprobe *uprobe;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301447 unsigned long bp_vaddr;
Oleg Nesterov56bb4cf2012-05-29 21:29:47 +02001448 int uninitialized_var(is_swbp);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301449
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301450 bp_vaddr = uprobe_get_swbp_addr(regs);
Oleg Nesterovd790d342012-05-29 21:29:14 +02001451 uprobe = find_active_uprobe(bp_vaddr, &is_swbp);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301452
1453 if (!uprobe) {
Oleg Nesterov56bb4cf2012-05-29 21:29:47 +02001454 if (is_swbp > 0) {
1455 /* No matching uprobe; signal SIGTRAP. */
1456 send_sig(SIGTRAP, current, 0);
1457 } else {
1458 /*
1459 * Either we raced with uprobe_unregister() or we can't
1460 * access this memory. The latter is only possible if
1461 * another thread plays with our ->mm. In both cases
1462 * we can simply restart. If this vma was unmapped we
1463 * can pretend this insn was not executed yet and get
1464 * the (correct) SIGSEGV after restart.
1465 */
1466 instruction_pointer_set(regs, bp_vaddr);
1467 }
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301468 return;
1469 }
Oleg Nesterov142b18d2012-09-29 21:56:57 +02001470 /*
1471 * TODO: move copy_insn/etc into _register and remove this hack.
1472 * After we hit the bp, _unregister + _register can install the
1473 * new and not-yet-analyzed uprobe at the same address, restart.
1474 */
1475 smp_rmb(); /* pairs with wmb() in install_breakpoint() */
Oleg Nesterov71434f22012-09-30 21:12:44 +02001476 if (unlikely(!test_bit(UPROBE_COPY_INSN, &uprobe->flags)))
Oleg Nesterov142b18d2012-09-29 21:56:57 +02001477 goto restart;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301478
Oleg Nesterov1b08e9072012-09-14 18:52:10 +02001479 utask = current->utask;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301480 if (!utask) {
1481 utask = add_utask();
1482 /* Cannot allocate; re-execute the instruction. */
1483 if (!utask)
Oleg Nesterov0578a972012-09-14 18:31:23 +02001484 goto restart;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301485 }
Oleg Nesterov746a9e62012-09-14 18:23:51 +02001486
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301487 handler_chain(uprobe, regs);
Oleg Nesterov0578a972012-09-14 18:31:23 +02001488 if (can_skip_sstep(uprobe, regs))
1489 goto out;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301490
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301491 if (!pre_ssout(uprobe, regs, bp_vaddr)) {
Sebastian Andrzej Siewior9d778782012-08-07 18:12:28 +02001492 arch_uprobe_enable_step(&uprobe->arch);
Oleg Nesterov746a9e62012-09-14 18:23:51 +02001493 utask->active_uprobe = uprobe;
1494 utask->state = UTASK_SSTEP;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301495 return;
1496 }
1497
Oleg Nesterov0578a972012-09-14 18:31:23 +02001498restart:
1499 /*
1500 * cannot singlestep; cannot skip instruction;
1501 * re-execute the instruction.
1502 */
1503 instruction_pointer_set(regs, bp_vaddr);
1504out:
Sebastian Andrzej Siewior8bd87442012-08-07 18:12:30 +02001505 put_uprobe(uprobe);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301506}
1507
1508/*
1509 * Perform required fix-ups and disable singlestep.
1510 * Allow pending signals to take effect.
1511 */
1512static void handle_singlestep(struct uprobe_task *utask, struct pt_regs *regs)
1513{
1514 struct uprobe *uprobe;
1515
1516 uprobe = utask->active_uprobe;
1517 if (utask->state == UTASK_SSTEP_ACK)
1518 arch_uprobe_post_xol(&uprobe->arch, regs);
1519 else if (utask->state == UTASK_SSTEP_TRAPPED)
1520 arch_uprobe_abort_xol(&uprobe->arch, regs);
1521 else
1522 WARN_ON_ONCE(1);
1523
Sebastian Andrzej Siewior9d778782012-08-07 18:12:28 +02001524 arch_uprobe_disable_step(&uprobe->arch);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301525 put_uprobe(uprobe);
1526 utask->active_uprobe = NULL;
1527 utask->state = UTASK_RUNNING;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301528 xol_free_insn_slot(current);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301529
1530 spin_lock_irq(&current->sighand->siglock);
1531 recalc_sigpending(); /* see uprobe_deny_signal() */
1532 spin_unlock_irq(&current->sighand->siglock);
1533}
1534
1535/*
Oleg Nesterov1b08e9072012-09-14 18:52:10 +02001536 * On breakpoint hit, breakpoint notifier sets the TIF_UPROBE flag and
1537 * allows the thread to return from interrupt. After that handle_swbp()
1538 * sets utask->active_uprobe.
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301539 *
Oleg Nesterov1b08e9072012-09-14 18:52:10 +02001540 * On singlestep exception, singlestep notifier sets the TIF_UPROBE flag
1541 * and allows the thread to return from interrupt.
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301542 *
1543 * While returning to userspace, thread notices the TIF_UPROBE flag and calls
1544 * uprobe_notify_resume().
1545 */
1546void uprobe_notify_resume(struct pt_regs *regs)
1547{
1548 struct uprobe_task *utask;
1549
Oleg Nesterovdb023ea2012-09-14 19:05:46 +02001550 clear_thread_flag(TIF_UPROBE);
1551
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301552 utask = current->utask;
Oleg Nesterov1b08e9072012-09-14 18:52:10 +02001553 if (utask && utask->active_uprobe)
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301554 handle_singlestep(utask, regs);
Oleg Nesterov1b08e9072012-09-14 18:52:10 +02001555 else
1556 handle_swbp(regs);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301557}
1558
1559/*
1560 * uprobe_pre_sstep_notifier gets called from interrupt context as part of
1561 * notifier mechanism. Set TIF_UPROBE flag and indicate breakpoint hit.
1562 */
1563int uprobe_pre_sstep_notifier(struct pt_regs *regs)
1564{
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +02001565 if (!current->mm || !test_bit(MMF_HAS_UPROBES, &current->mm->flags))
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301566 return 0;
1567
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301568 set_thread_flag(TIF_UPROBE);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301569 return 1;
1570}
1571
1572/*
1573 * uprobe_post_sstep_notifier gets called in interrupt context as part of notifier
1574 * mechanism. Set TIF_UPROBE flag and indicate completion of singlestep.
1575 */
1576int uprobe_post_sstep_notifier(struct pt_regs *regs)
1577{
1578 struct uprobe_task *utask = current->utask;
1579
1580 if (!current->mm || !utask || !utask->active_uprobe)
1581 /* task is currently not uprobed */
1582 return 0;
1583
1584 utask->state = UTASK_SSTEP_ACK;
1585 set_thread_flag(TIF_UPROBE);
1586 return 1;
1587}
1588
1589static struct notifier_block uprobe_exception_nb = {
1590 .notifier_call = arch_uprobe_exception_notify,
1591 .priority = INT_MAX-1, /* notified after kprobes, kgdb */
1592};
1593
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301594static int __init init_uprobes(void)
1595{
1596 int i;
1597
1598 for (i = 0; i < UPROBES_HASH_SZ; i++) {
1599 mutex_init(&uprobes_mutex[i]);
1600 mutex_init(&uprobes_mmap_mutex[i]);
1601 }
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301602
1603 return register_die_notifier(&uprobe_exception_nb);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301604}
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301605module_init(init_uprobes);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301606
1607static void __exit exit_uprobes(void)
1608{
1609}
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301610module_exit(exit_uprobes);