blob: ca9012930ce79b7dd1eb979631d383d3e7c6e507 [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>
Josh Stonee8440c12013-01-13 19:03:34 +010030#include <linux/export.h>
Srikar Dronamraju2b144492012-02-09 14:56:42 +053031#include <linux/rmap.h> /* anon_vma_prepare */
32#include <linux/mmu_notifier.h> /* set_pte_at_notify */
33#include <linux/swap.h> /* try_to_free_swap */
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +053034#include <linux/ptrace.h> /* user_enable_single_step */
35#include <linux/kdebug.h> /* notifier mechanism */
Oleg Nesterov194f8dc2012-07-29 20:22:49 +020036#include "../../mm/internal.h" /* munlock_vma_page */
Oleg Nesterov32cdba12012-11-14 19:03:42 +010037#include <linux/percpu-rwsem.h>
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010038
Srikar Dronamraju2b144492012-02-09 14:56:42 +053039#include <linux/uprobes.h>
40
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +053041#define UINSNS_PER_PAGE (PAGE_SIZE/UPROBE_XOL_SLOT_BYTES)
42#define MAX_UPROBE_XOL_SLOTS UINSNS_PER_PAGE
43
Srikar Dronamraju2b144492012-02-09 14:56:42 +053044static struct rb_root uprobes_tree = RB_ROOT;
Oleg Nesterov441f1eb2012-11-25 19:54:29 +010045/*
46 * allows us to skip the uprobe_mmap if there are no uprobe events active
47 * at this time. Probably a fine grained per inode count is better?
48 */
49#define no_uprobe_events() RB_EMPTY_ROOT(&uprobes_tree)
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010050
Srikar Dronamraju2b144492012-02-09 14:56:42 +053051static DEFINE_SPINLOCK(uprobes_treelock); /* serialize rbtree access */
52
53#define UPROBES_HASH_SZ 13
Srikar Dronamraju2b144492012-02-09 14:56:42 +053054/* serialize uprobe->pending_list */
55static struct mutex uprobes_mmap_mutex[UPROBES_HASH_SZ];
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010056#define uprobes_mmap_hash(v) (&uprobes_mmap_mutex[((unsigned long)(v)) % UPROBES_HASH_SZ])
Srikar Dronamraju2b144492012-02-09 14:56:42 +053057
Oleg Nesterov32cdba12012-11-14 19:03:42 +010058static struct percpu_rw_semaphore dup_mmap_sem;
59
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +020060/* Have a copy of original instruction */
Oleg Nesterov71434f22012-09-30 21:12:44 +020061#define UPROBE_COPY_INSN 0
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +020062/* Can skip singlestep */
Oleg Nesterovbb929282012-11-24 18:27:08 +010063#define UPROBE_SKIP_SSTEP 1
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +020064
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +053065struct uprobe {
66 struct rb_node rb_node; /* node in the rb tree */
67 atomic_t ref;
Oleg Nesterove591c8d2012-11-24 17:29:40 +010068 struct rw_semaphore register_rwsem;
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +053069 struct rw_semaphore consumer_rwsem;
70 struct list_head pending_list;
71 struct uprobe_consumer *consumers;
72 struct inode *inode; /* Also hold a ref to inode */
73 loff_t offset;
Oleg Nesterov71434f22012-09-30 21:12:44 +020074 unsigned long flags;
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +053075 struct arch_uprobe arch;
76};
77
Srikar Dronamraju2b144492012-02-09 14:56:42 +053078/*
79 * valid_vma: Verify if the specified vma is an executable vma
80 * Relax restrictions while unregistering: vm_flags might have
81 * changed after breakpoint was inserted.
82 * - is_register: indicates if we are in register context.
83 * - Return 1 if the specified virtual address is in an
84 * executable vma.
85 */
86static bool valid_vma(struct vm_area_struct *vma, bool is_register)
87{
Oleg Nesterove40cfce2012-09-16 19:31:39 +020088 vm_flags_t flags = VM_HUGETLB | VM_MAYEXEC | VM_SHARED;
Srikar Dronamraju2b144492012-02-09 14:56:42 +053089
Oleg Nesterove40cfce2012-09-16 19:31:39 +020090 if (is_register)
91 flags |= VM_WRITE;
Srikar Dronamraju2b144492012-02-09 14:56:42 +053092
Oleg Nesterove40cfce2012-09-16 19:31:39 +020093 return vma->vm_file && (vma->vm_flags & flags) == VM_MAYEXEC;
Srikar Dronamraju2b144492012-02-09 14:56:42 +053094}
95
Oleg Nesterov57683f72012-07-29 20:22:47 +020096static unsigned long offset_to_vaddr(struct vm_area_struct *vma, loff_t offset)
Srikar Dronamraju2b144492012-02-09 14:56:42 +053097{
Oleg Nesterov57683f72012-07-29 20:22:47 +020098 return vma->vm_start + offset - ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
Srikar Dronamraju2b144492012-02-09 14:56:42 +053099}
100
Oleg Nesterovcb113b42012-07-29 20:22:42 +0200101static loff_t vaddr_to_offset(struct vm_area_struct *vma, unsigned long vaddr)
102{
103 return ((loff_t)vma->vm_pgoff << PAGE_SHIFT) + (vaddr - vma->vm_start);
104}
105
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530106/**
107 * __replace_page - replace page in vma by new page.
108 * based on replace_page in mm/ksm.c
109 *
110 * @vma: vma that holds the pte pointing to page
Oleg Nesterovc517ee72012-07-29 20:22:16 +0200111 * @addr: address the old @page is mapped at
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530112 * @page: the cowed page we are replacing by kpage
113 * @kpage: the modified page we replace page by
114 *
115 * Returns 0 on success, -EFAULT on failure.
116 */
Oleg Nesterovc517ee72012-07-29 20:22:16 +0200117static int __replace_page(struct vm_area_struct *vma, unsigned long addr,
118 struct page *page, struct page *kpage)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530119{
120 struct mm_struct *mm = vma->vm_mm;
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200121 spinlock_t *ptl;
122 pte_t *ptep;
Oleg Nesterov9f924482012-07-29 20:22:20 +0200123 int err;
Haggai Eran6bdb9132012-10-08 16:33:35 -0700124 /* For mmu_notifiers */
125 const unsigned long mmun_start = addr;
126 const unsigned long mmun_end = addr + PAGE_SIZE;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530127
Oleg Nesterov194f8dc2012-07-29 20:22:49 +0200128 /* For try_to_free_swap() and munlock_vma_page() below */
Oleg Nesterov9f924482012-07-29 20:22:20 +0200129 lock_page(page);
130
Haggai Eran6bdb9132012-10-08 16:33:35 -0700131 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Oleg Nesterov9f924482012-07-29 20:22:20 +0200132 err = -EAGAIN;
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200133 ptep = page_check_address(page, mm, addr, &ptl, 0);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530134 if (!ptep)
Oleg Nesterov9f924482012-07-29 20:22:20 +0200135 goto unlock;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530136
137 get_page(kpage);
138 page_add_new_anon_rmap(kpage, vma, addr);
139
Srikar Dronamraju7396fa82012-04-11 16:05:16 +0530140 if (!PageAnon(page)) {
141 dec_mm_counter(mm, MM_FILEPAGES);
142 inc_mm_counter(mm, MM_ANONPAGES);
143 }
144
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530145 flush_cache_page(vma, addr, pte_pfn(*ptep));
146 ptep_clear_flush(vma, addr, ptep);
147 set_pte_at_notify(mm, addr, ptep, mk_pte(kpage, vma->vm_page_prot));
148
149 page_remove_rmap(page);
150 if (!page_mapped(page))
151 try_to_free_swap(page);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530152 pte_unmap_unlock(ptep, ptl);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530153
Oleg Nesterov194f8dc2012-07-29 20:22:49 +0200154 if (vma->vm_flags & VM_LOCKED)
155 munlock_vma_page(page);
156 put_page(page);
157
Oleg Nesterov9f924482012-07-29 20:22:20 +0200158 err = 0;
159 unlock:
Haggai Eran6bdb9132012-10-08 16:33:35 -0700160 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Oleg Nesterov9f924482012-07-29 20:22:20 +0200161 unlock_page(page);
162 return err;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530163}
164
165/**
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530166 * is_swbp_insn - check if instruction is breakpoint instruction.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530167 * @insn: instruction to be checked.
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530168 * Default implementation of is_swbp_insn
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530169 * Returns true if @insn is a breakpoint instruction.
170 */
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530171bool __weak is_swbp_insn(uprobe_opcode_t *insn)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530172{
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530173 return *insn == UPROBE_SWBP_INSN;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530174}
175
Ananth N Mavinakayanahalli0908ad62013-03-22 20:46:27 +0530176/**
177 * is_trap_insn - check if instruction is breakpoint instruction.
178 * @insn: instruction to be checked.
179 * Default implementation of is_trap_insn
180 * Returns true if @insn is a breakpoint instruction.
181 *
182 * This function is needed for the case where an architecture has multiple
183 * trap instructions (like powerpc).
184 */
185bool __weak is_trap_insn(uprobe_opcode_t *insn)
186{
187 return is_swbp_insn(insn);
188}
189
Oleg Nesterovcceb55a2012-09-23 21:10:18 +0200190static void copy_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t *opcode)
191{
192 void *kaddr = kmap_atomic(page);
193 memcpy(opcode, kaddr + (vaddr & ~PAGE_MASK), UPROBE_SWBP_INSN_SIZE);
194 kunmap_atomic(kaddr);
195}
196
Oleg Nesteroved6f6a52012-09-23 21:30:44 +0200197static int verify_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t *new_opcode)
198{
199 uprobe_opcode_t old_opcode;
200 bool is_swbp;
201
Ananth N Mavinakayanahalli0908ad62013-03-22 20:46:27 +0530202 /*
203 * Note: We only check if the old_opcode is UPROBE_SWBP_INSN here.
204 * We do not check if it is any other 'trap variant' which could
205 * be conditional trap instruction such as the one powerpc supports.
206 *
207 * The logic is that we do not care if the underlying instruction
208 * is a trap variant; uprobes always wins over any other (gdb)
209 * breakpoint.
210 */
Oleg Nesteroved6f6a52012-09-23 21:30:44 +0200211 copy_opcode(page, vaddr, &old_opcode);
212 is_swbp = is_swbp_insn(&old_opcode);
213
214 if (is_swbp_insn(new_opcode)) {
215 if (is_swbp) /* register: already installed? */
216 return 0;
217 } else {
218 if (!is_swbp) /* unregister: was it changed by us? */
Oleg Nesterov076a3652012-09-30 18:54:53 +0200219 return 0;
Oleg Nesteroved6f6a52012-09-23 21:30:44 +0200220 }
221
222 return 1;
223}
224
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530225/*
226 * NOTE:
227 * Expect the breakpoint instruction to be the smallest size instruction for
228 * the architecture. If an arch has variable length instruction and the
229 * breakpoint instruction is not of the smallest length instruction
Ananth N Mavinakayanahalli0908ad62013-03-22 20:46:27 +0530230 * supported by that architecture then we need to modify is_trap_at_addr and
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530231 * write_opcode accordingly. This would never be a problem for archs that
232 * have fixed length instructions.
233 */
234
235/*
236 * write_opcode - write the opcode at a given virtual address.
237 * @mm: the probed process address space.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530238 * @vaddr: the virtual address to store the opcode.
239 * @opcode: opcode to be written at @vaddr.
240 *
241 * Called with mm->mmap_sem held (for read and with a reference to
242 * mm).
243 *
244 * For mm @mm, write the opcode at @vaddr.
245 * Return 0 (success) or a negative errno.
246 */
Oleg Nesterovcceb55a2012-09-23 21:10:18 +0200247static int write_opcode(struct mm_struct *mm, unsigned long vaddr,
248 uprobe_opcode_t opcode)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530249{
250 struct page *old_page, *new_page;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530251 void *vaddr_old, *vaddr_new;
252 struct vm_area_struct *vma;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530253 int ret;
Oleg Nesterovf4030722012-07-29 20:22:12 +0200254
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200255retry:
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530256 /* Read the page with vaddr into memory */
Oleg Nesterov75ed82e2012-09-16 17:20:06 +0200257 ret = get_user_pages(NULL, mm, vaddr, 1, 0, 1, &old_page, &vma);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530258 if (ret <= 0)
259 return ret;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100260
Oleg Nesteroved6f6a52012-09-23 21:30:44 +0200261 ret = verify_opcode(old_page, vaddr, &opcode);
262 if (ret <= 0)
263 goto put_old;
264
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530265 ret = -ENOMEM;
266 new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vaddr);
267 if (!new_page)
Oleg Nesterov9f924482012-07-29 20:22:20 +0200268 goto put_old;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530269
270 __SetPageUptodate(new_page);
271
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530272 /* copy the page now that we've got it stable */
273 vaddr_old = kmap_atomic(old_page);
274 vaddr_new = kmap_atomic(new_page);
275
276 memcpy(vaddr_new, vaddr_old, PAGE_SIZE);
Oleg Nesterovd9c4a302012-06-15 17:43:50 +0200277 memcpy(vaddr_new + (vaddr & ~PAGE_MASK), &opcode, UPROBE_SWBP_INSN_SIZE);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530278
279 kunmap_atomic(vaddr_new);
280 kunmap_atomic(vaddr_old);
281
282 ret = anon_vma_prepare(vma);
283 if (ret)
Oleg Nesterov9f924482012-07-29 20:22:20 +0200284 goto put_new;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530285
Oleg Nesterovc517ee72012-07-29 20:22:16 +0200286 ret = __replace_page(vma, vaddr, old_page, new_page);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530287
Oleg Nesterov9f924482012-07-29 20:22:20 +0200288put_new:
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530289 page_cache_release(new_page);
Oleg Nesterov9f924482012-07-29 20:22:20 +0200290put_old:
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100291 put_page(old_page);
292
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200293 if (unlikely(ret == -EAGAIN))
294 goto retry;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530295 return ret;
296}
297
298/**
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530299 * set_swbp - store breakpoint at a given address.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530300 * @auprobe: arch specific probepoint information.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530301 * @mm: the probed process address space.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530302 * @vaddr: the virtual address to insert the opcode.
303 *
304 * For mm @mm, store the breakpoint instruction at @vaddr.
305 * Return 0 (success) or a negative errno.
306 */
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530307int __weak set_swbp(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530308{
Oleg Nesterovcceb55a2012-09-23 21:10:18 +0200309 return write_opcode(mm, vaddr, UPROBE_SWBP_INSN);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530310}
311
312/**
313 * set_orig_insn - Restore the original instruction.
314 * @mm: the probed process address space.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530315 * @auprobe: arch specific probepoint information.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530316 * @vaddr: the virtual address to insert the opcode.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530317 *
318 * For mm @mm, restore the original opcode (opcode) at @vaddr.
319 * Return 0 (success) or a negative errno.
320 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100321int __weak
Oleg Nesterovded86e72012-08-08 18:07:03 +0200322set_orig_insn(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530323{
Oleg Nesterovcceb55a2012-09-23 21:10:18 +0200324 return write_opcode(mm, vaddr, *(uprobe_opcode_t *)auprobe->insn);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530325}
326
327static int match_uprobe(struct uprobe *l, struct uprobe *r)
328{
329 if (l->inode < r->inode)
330 return -1;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100331
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530332 if (l->inode > r->inode)
333 return 1;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530334
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100335 if (l->offset < r->offset)
336 return -1;
337
338 if (l->offset > r->offset)
339 return 1;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530340
341 return 0;
342}
343
344static struct uprobe *__find_uprobe(struct inode *inode, loff_t offset)
345{
346 struct uprobe u = { .inode = inode, .offset = offset };
347 struct rb_node *n = uprobes_tree.rb_node;
348 struct uprobe *uprobe;
349 int match;
350
351 while (n) {
352 uprobe = rb_entry(n, struct uprobe, rb_node);
353 match = match_uprobe(&u, uprobe);
354 if (!match) {
355 atomic_inc(&uprobe->ref);
356 return uprobe;
357 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100358
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530359 if (match < 0)
360 n = n->rb_left;
361 else
362 n = n->rb_right;
363 }
364 return NULL;
365}
366
367/*
368 * Find a uprobe corresponding to a given inode:offset
369 * Acquires uprobes_treelock
370 */
371static struct uprobe *find_uprobe(struct inode *inode, loff_t offset)
372{
373 struct uprobe *uprobe;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530374
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200375 spin_lock(&uprobes_treelock);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530376 uprobe = __find_uprobe(inode, offset);
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200377 spin_unlock(&uprobes_treelock);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100378
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530379 return uprobe;
380}
381
382static struct uprobe *__insert_uprobe(struct uprobe *uprobe)
383{
384 struct rb_node **p = &uprobes_tree.rb_node;
385 struct rb_node *parent = NULL;
386 struct uprobe *u;
387 int match;
388
389 while (*p) {
390 parent = *p;
391 u = rb_entry(parent, struct uprobe, rb_node);
392 match = match_uprobe(uprobe, u);
393 if (!match) {
394 atomic_inc(&u->ref);
395 return u;
396 }
397
398 if (match < 0)
399 p = &parent->rb_left;
400 else
401 p = &parent->rb_right;
402
403 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100404
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530405 u = NULL;
406 rb_link_node(&uprobe->rb_node, parent, p);
407 rb_insert_color(&uprobe->rb_node, &uprobes_tree);
408 /* get access + creation ref */
409 atomic_set(&uprobe->ref, 2);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100410
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530411 return u;
412}
413
414/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100415 * Acquire uprobes_treelock.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530416 * Matching uprobe already exists in rbtree;
417 * increment (access refcount) and return the matching uprobe.
418 *
419 * No matching uprobe; insert the uprobe in rb_tree;
420 * get a double refcount (access + creation) and return NULL.
421 */
422static struct uprobe *insert_uprobe(struct uprobe *uprobe)
423{
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530424 struct uprobe *u;
425
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200426 spin_lock(&uprobes_treelock);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530427 u = __insert_uprobe(uprobe);
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200428 spin_unlock(&uprobes_treelock);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100429
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530430 return u;
431}
432
433static void put_uprobe(struct uprobe *uprobe)
434{
435 if (atomic_dec_and_test(&uprobe->ref))
436 kfree(uprobe);
437}
438
439static struct uprobe *alloc_uprobe(struct inode *inode, loff_t offset)
440{
441 struct uprobe *uprobe, *cur_uprobe;
442
443 uprobe = kzalloc(sizeof(struct uprobe), GFP_KERNEL);
444 if (!uprobe)
445 return NULL;
446
447 uprobe->inode = igrab(inode);
448 uprobe->offset = offset;
Oleg Nesterove591c8d2012-11-24 17:29:40 +0100449 init_rwsem(&uprobe->register_rwsem);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530450 init_rwsem(&uprobe->consumer_rwsem);
Oleg Nesterovbbc33d02012-11-21 16:55:38 +0100451 /* For now assume that the instruction need not be single-stepped */
452 __set_bit(UPROBE_SKIP_SSTEP, &uprobe->flags);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530453
454 /* add to uprobes_tree, sorted on inode:offset */
455 cur_uprobe = insert_uprobe(uprobe);
456
457 /* a uprobe exists for this inode:offset combination */
458 if (cur_uprobe) {
459 kfree(uprobe);
460 uprobe = cur_uprobe;
461 iput(inode);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100462 }
463
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530464 return uprobe;
465}
466
Oleg Nesterov9a98e032012-11-23 20:15:17 +0100467static void consumer_add(struct uprobe *uprobe, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530468{
469 down_write(&uprobe->consumer_rwsem);
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530470 uc->next = uprobe->consumers;
471 uprobe->consumers = uc;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530472 up_write(&uprobe->consumer_rwsem);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530473}
474
475/*
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530476 * For uprobe @uprobe, delete the consumer @uc.
477 * Return true if the @uc is deleted successfully
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530478 * or return false.
479 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530480static bool consumer_del(struct uprobe *uprobe, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530481{
482 struct uprobe_consumer **con;
483 bool ret = false;
484
485 down_write(&uprobe->consumer_rwsem);
486 for (con = &uprobe->consumers; *con; con = &(*con)->next) {
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530487 if (*con == uc) {
488 *con = uc->next;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530489 ret = true;
490 break;
491 }
492 }
493 up_write(&uprobe->consumer_rwsem);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100494
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530495 return ret;
496}
497
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530498static int
Oleg Nesterovd4366152012-06-15 17:43:42 +0200499__copy_insn(struct address_space *mapping, struct file *filp, char *insn,
Oleg Nesterov593609a2012-06-15 17:43:59 +0200500 unsigned long nbytes, loff_t offset)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530501{
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530502 struct page *page;
503 void *vaddr;
Oleg Nesterov593609a2012-06-15 17:43:59 +0200504 unsigned long off;
505 pgoff_t idx;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530506
507 if (!filp)
508 return -EINVAL;
509
Oleg Nesterovcc359d12012-06-15 17:43:25 +0200510 if (!mapping->a_ops->readpage)
511 return -EIO;
512
Oleg Nesterov593609a2012-06-15 17:43:59 +0200513 idx = offset >> PAGE_CACHE_SHIFT;
514 off = offset & ~PAGE_MASK;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530515
516 /*
517 * Ensure that the page that has the original instruction is
518 * populated and in page-cache.
519 */
520 page = read_mapping_page(mapping, idx, filp);
521 if (IS_ERR(page))
522 return PTR_ERR(page);
523
524 vaddr = kmap_atomic(page);
Oleg Nesterov593609a2012-06-15 17:43:59 +0200525 memcpy(insn, vaddr + off, nbytes);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530526 kunmap_atomic(vaddr);
527 page_cache_release(page);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100528
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530529 return 0;
530}
531
Oleg Nesterovd4366152012-06-15 17:43:42 +0200532static int copy_insn(struct uprobe *uprobe, struct file *filp)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530533{
534 struct address_space *mapping;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530535 unsigned long nbytes;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100536 int bytes;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530537
Oleg Nesterovd4366152012-06-15 17:43:42 +0200538 nbytes = PAGE_SIZE - (uprobe->offset & ~PAGE_MASK);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530539 mapping = uprobe->inode->i_mapping;
540
541 /* Instruction at end of binary; copy only available bytes */
542 if (uprobe->offset + MAX_UINSN_BYTES > uprobe->inode->i_size)
543 bytes = uprobe->inode->i_size - uprobe->offset;
544 else
545 bytes = MAX_UINSN_BYTES;
546
547 /* Instruction at the page-boundary; copy bytes in second page */
548 if (nbytes < bytes) {
Oleg Nesterovfc36f592012-06-15 17:43:44 +0200549 int err = __copy_insn(mapping, filp, uprobe->arch.insn + nbytes,
550 bytes - nbytes, uprobe->offset + nbytes);
551 if (err)
552 return err;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530553 bytes = nbytes;
554 }
Oleg Nesterovd4366152012-06-15 17:43:42 +0200555 return __copy_insn(mapping, filp, uprobe->arch.insn, bytes, uprobe->offset);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530556}
557
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200558static int prepare_uprobe(struct uprobe *uprobe, struct file *file,
559 struct mm_struct *mm, unsigned long vaddr)
560{
561 int ret = 0;
562
Oleg Nesterov71434f22012-09-30 21:12:44 +0200563 if (test_bit(UPROBE_COPY_INSN, &uprobe->flags))
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200564 return ret;
565
Oleg Nesterovd4d3ccc2012-11-24 18:51:34 +0100566 /* TODO: move this into _register, until then we abuse this sem. */
567 down_write(&uprobe->consumer_rwsem);
Oleg Nesterov71434f22012-09-30 21:12:44 +0200568 if (test_bit(UPROBE_COPY_INSN, &uprobe->flags))
Oleg Nesterov4710f052012-09-30 20:31:41 +0200569 goto out;
570
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200571 ret = copy_insn(uprobe, file);
572 if (ret)
573 goto out;
574
575 ret = -ENOTSUPP;
Ananth N Mavinakayanahalli0908ad62013-03-22 20:46:27 +0530576 if (is_trap_insn((uprobe_opcode_t *)uprobe->arch.insn))
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200577 goto out;
578
579 ret = arch_uprobe_analyze_insn(&uprobe->arch, mm, vaddr);
580 if (ret)
581 goto out;
582
583 /* write_opcode() assumes we don't cross page boundary */
584 BUG_ON((uprobe->offset & ~PAGE_MASK) +
585 UPROBE_SWBP_INSN_SIZE > PAGE_SIZE);
586
587 smp_wmb(); /* pairs with rmb() in find_active_uprobe() */
Oleg Nesterov71434f22012-09-30 21:12:44 +0200588 set_bit(UPROBE_COPY_INSN, &uprobe->flags);
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200589
590 out:
Oleg Nesterovd4d3ccc2012-11-24 18:51:34 +0100591 up_write(&uprobe->consumer_rwsem);
Oleg Nesterov4710f052012-09-30 20:31:41 +0200592
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200593 return ret;
594}
595
Oleg Nesterov8a7f2fa2012-12-28 17:58:38 +0100596static inline bool consumer_filter(struct uprobe_consumer *uc,
597 enum uprobe_filter_ctx ctx, struct mm_struct *mm)
Oleg Nesterov806a98b2012-12-27 18:21:11 +0100598{
Oleg Nesterov8a7f2fa2012-12-28 17:58:38 +0100599 return !uc->filter || uc->filter(uc, ctx, mm);
Oleg Nesterov806a98b2012-12-27 18:21:11 +0100600}
601
Oleg Nesterov8a7f2fa2012-12-28 17:58:38 +0100602static bool filter_chain(struct uprobe *uprobe,
603 enum uprobe_filter_ctx ctx, struct mm_struct *mm)
Oleg Nesterov63633cb2012-11-22 18:30:15 +0100604{
Oleg Nesterov1ff6fee2012-11-24 18:15:46 +0100605 struct uprobe_consumer *uc;
606 bool ret = false;
607
608 down_read(&uprobe->consumer_rwsem);
609 for (uc = uprobe->consumers; uc; uc = uc->next) {
Oleg Nesterov8a7f2fa2012-12-28 17:58:38 +0100610 ret = consumer_filter(uc, ctx, mm);
Oleg Nesterov1ff6fee2012-11-24 18:15:46 +0100611 if (ret)
612 break;
613 }
614 up_read(&uprobe->consumer_rwsem);
615
616 return ret;
Oleg Nesterov63633cb2012-11-22 18:30:15 +0100617}
618
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530619static int
620install_breakpoint(struct uprobe *uprobe, struct mm_struct *mm,
Oleg Nesterov816c03f2012-06-15 17:43:55 +0200621 struct vm_area_struct *vma, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530622{
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +0200623 bool first_uprobe;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530624 int ret;
625
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200626 ret = prepare_uprobe(uprobe, vma->vm_file, mm, vaddr);
627 if (ret)
628 return ret;
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530629
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +0200630 /*
631 * set MMF_HAS_UPROBES in advance for uprobe_pre_sstep_notifier(),
632 * the task can hit this breakpoint right after __replace_page().
633 */
634 first_uprobe = !test_bit(MMF_HAS_UPROBES, &mm->flags);
635 if (first_uprobe)
636 set_bit(MMF_HAS_UPROBES, &mm->flags);
637
Oleg Nesterov816c03f2012-06-15 17:43:55 +0200638 ret = set_swbp(&uprobe->arch, mm, vaddr);
Oleg Nesterov9f68f672012-08-19 16:15:09 +0200639 if (!ret)
640 clear_bit(MMF_RECALC_UPROBES, &mm->flags);
641 else if (first_uprobe)
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +0200642 clear_bit(MMF_HAS_UPROBES, &mm->flags);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530643
644 return ret;
645}
646
Oleg Nesterov076a3652012-09-30 18:54:53 +0200647static int
Oleg Nesterov816c03f2012-06-15 17:43:55 +0200648remove_breakpoint(struct uprobe *uprobe, struct mm_struct *mm, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530649{
Oleg Nesterov9f68f672012-08-19 16:15:09 +0200650 set_bit(MMF_RECALC_UPROBES, &mm->flags);
Oleg Nesterov076a3652012-09-30 18:54:53 +0200651 return set_orig_insn(&uprobe->arch, mm, vaddr);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530652}
653
Oleg Nesterov06b7bcd2012-11-25 22:01:42 +0100654static inline bool uprobe_is_active(struct uprobe *uprobe)
655{
656 return !RB_EMPTY_NODE(&uprobe->rb_node);
657}
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530658/*
Oleg Nesterov778b0322012-05-29 21:30:08 +0200659 * There could be threads that have already hit the breakpoint. They
660 * will recheck the current insn and restart if find_uprobe() fails.
661 * See find_active_uprobe().
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530662 */
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530663static void delete_uprobe(struct uprobe *uprobe)
664{
Oleg Nesterov06b7bcd2012-11-25 22:01:42 +0100665 if (WARN_ON(!uprobe_is_active(uprobe)))
666 return;
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);
Oleg Nesterov06b7bcd2012-11-25 22:01:42 +0100671 RB_CLEAR_NODE(&uprobe->rb_node); /* for uprobe_is_active() */
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530672 iput(uprobe->inode);
673 put_uprobe(uprobe);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530674}
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 vm_area_struct *vma;
Oleg Nesterov26872092012-06-15 17:43:33 +0200694 struct map_info *curr = NULL;
695 struct map_info *prev = NULL;
696 struct map_info *info;
697 int more = 0;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100698
Oleg Nesterov26872092012-06-15 17:43:33 +0200699 again:
700 mutex_lock(&mapping->i_mmap_mutex);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700701 vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) {
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530702 if (!valid_vma(vma, is_register))
703 continue;
704
Oleg Nesterov7a5bfb62012-06-15 17:43:36 +0200705 if (!prev && !more) {
706 /*
707 * Needs GFP_NOWAIT to avoid i_mmap_mutex recursion through
708 * reclaim. This is optimistic, no harm done if it fails.
709 */
710 prev = kmalloc(sizeof(struct map_info),
711 GFP_NOWAIT | __GFP_NOMEMALLOC | __GFP_NOWARN);
712 if (prev)
713 prev->next = NULL;
714 }
Oleg Nesterov26872092012-06-15 17:43:33 +0200715 if (!prev) {
716 more++;
717 continue;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530718 }
719
Oleg Nesterov26872092012-06-15 17:43:33 +0200720 if (!atomic_inc_not_zero(&vma->vm_mm->mm_users))
721 continue;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100722
Oleg Nesterov26872092012-06-15 17:43:33 +0200723 info = prev;
724 prev = prev->next;
725 info->next = curr;
726 curr = info;
727
728 info->mm = vma->vm_mm;
Oleg Nesterov57683f72012-07-29 20:22:47 +0200729 info->vaddr = offset_to_vaddr(vma, offset);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530730 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530731 mutex_unlock(&mapping->i_mmap_mutex);
732
Oleg Nesterov26872092012-06-15 17:43:33 +0200733 if (!more)
734 goto out;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100735
Oleg Nesterov26872092012-06-15 17:43:33 +0200736 prev = curr;
737 while (curr) {
738 mmput(curr->mm);
739 curr = curr->next;
740 }
741
742 do {
743 info = kmalloc(sizeof(struct map_info), GFP_KERNEL);
744 if (!info) {
745 curr = ERR_PTR(-ENOMEM);
746 goto out;
747 }
748 info->next = prev;
749 prev = info;
750 } while (--more);
751
752 goto again;
753 out:
754 while (prev)
755 prev = free_map_info(prev);
756 return curr;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530757}
758
Oleg Nesterovbdf86472013-02-03 19:21:12 +0100759static int
760register_for_each_vma(struct uprobe *uprobe, struct uprobe_consumer *new)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530761{
Oleg Nesterovbdf86472013-02-03 19:21:12 +0100762 bool is_register = !!new;
Oleg Nesterov26872092012-06-15 17:43:33 +0200763 struct map_info *info;
764 int err = 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530765
Oleg Nesterov32cdba12012-11-14 19:03:42 +0100766 percpu_down_write(&dup_mmap_sem);
Oleg Nesterov26872092012-06-15 17:43:33 +0200767 info = build_map_info(uprobe->inode->i_mapping,
768 uprobe->offset, is_register);
Oleg Nesterov32cdba12012-11-14 19:03:42 +0100769 if (IS_ERR(info)) {
770 err = PTR_ERR(info);
771 goto out;
772 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100773
Oleg Nesterov26872092012-06-15 17:43:33 +0200774 while (info) {
775 struct mm_struct *mm = info->mm;
776 struct vm_area_struct *vma;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100777
Oleg Nesterov076a3652012-09-30 18:54:53 +0200778 if (err && is_register)
Oleg Nesterov26872092012-06-15 17:43:33 +0200779 goto free;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100780
Oleg Nesterov77fc4af2012-05-29 21:29:28 +0200781 down_write(&mm->mmap_sem);
Oleg Nesterovf4d6dfe2012-07-29 20:22:44 +0200782 vma = find_vma(mm, info->vaddr);
783 if (!vma || !valid_vma(vma, is_register) ||
Oleg Nesterovf2817692013-03-17 18:54:44 +0100784 file_inode(vma->vm_file) != uprobe->inode)
Oleg Nesterov26872092012-06-15 17:43:33 +0200785 goto unlock;
786
Oleg Nesterovf4d6dfe2012-07-29 20:22:44 +0200787 if (vma->vm_start > info->vaddr ||
788 vaddr_to_offset(vma, info->vaddr) != uprobe->offset)
Oleg Nesterov26872092012-06-15 17:43:33 +0200789 goto unlock;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530790
Oleg Nesterov806a98b2012-12-27 18:21:11 +0100791 if (is_register) {
792 /* consult only the "caller", new consumer. */
Oleg Nesterovbdf86472013-02-03 19:21:12 +0100793 if (consumer_filter(new,
Oleg Nesterov8a7f2fa2012-12-28 17:58:38 +0100794 UPROBE_FILTER_REGISTER, mm))
Oleg Nesterov806a98b2012-12-27 18:21:11 +0100795 err = install_breakpoint(uprobe, mm, vma, info->vaddr);
796 } else if (test_bit(MMF_HAS_UPROBES, &mm->flags)) {
Oleg Nesterov8a7f2fa2012-12-28 17:58:38 +0100797 if (!filter_chain(uprobe,
798 UPROBE_FILTER_UNREGISTER, mm))
Oleg Nesterov806a98b2012-12-27 18:21:11 +0100799 err |= remove_breakpoint(uprobe, mm, info->vaddr);
800 }
Oleg Nesterov78f74112012-08-08 17:35:08 +0200801
Oleg Nesterov26872092012-06-15 17:43:33 +0200802 unlock:
803 up_write(&mm->mmap_sem);
804 free:
805 mmput(mm);
806 info = free_map_info(info);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530807 }
Oleg Nesterov32cdba12012-11-14 19:03:42 +0100808 out:
809 percpu_up_write(&dup_mmap_sem);
Oleg Nesterov26872092012-06-15 17:43:33 +0200810 return err;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530811}
812
Oleg Nesterov9a98e032012-11-23 20:15:17 +0100813static int __uprobe_register(struct uprobe *uprobe, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530814{
Oleg Nesterov9a98e032012-11-23 20:15:17 +0100815 consumer_add(uprobe, uc);
Oleg Nesterovbdf86472013-02-03 19:21:12 +0100816 return register_for_each_vma(uprobe, uc);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530817}
818
Oleg Nesterov04aab9b2012-11-23 19:43:50 +0100819static void __uprobe_unregister(struct uprobe *uprobe, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530820{
Oleg Nesterov04aab9b2012-11-23 19:43:50 +0100821 int err;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530822
Oleg Nesterov04aab9b2012-11-23 19:43:50 +0100823 if (!consumer_del(uprobe, uc)) /* WARN? */
824 return;
825
Oleg Nesterovbdf86472013-02-03 19:21:12 +0100826 err = register_for_each_vma(uprobe, NULL);
Oleg Nesterovbb929282012-11-24 18:27:08 +0100827 /* TODO : cant unregister? schedule a worker thread */
828 if (!uprobe->consumers && !err)
829 delete_uprobe(uprobe);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530830}
831
832/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100833 * uprobe_register - register a probe
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530834 * @inode: the file in which the probe has to be placed.
835 * @offset: offset from the start of the file.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530836 * @uc: information on howto handle the probe..
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530837 *
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100838 * Apart from the access refcount, uprobe_register() takes a creation
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530839 * refcount (thro alloc_uprobe) if and only if this @uprobe is getting
840 * inserted into the rbtree (i.e first consumer for a @inode:@offset
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100841 * tuple). Creation refcount stops uprobe_unregister from freeing the
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530842 * @uprobe even before the register operation is complete. Creation
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530843 * refcount is released when the last @uc for the @uprobe
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530844 * unregisters.
845 *
846 * Return errno if it cannot successully install probes
847 * else return 0 (success)
848 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530849int uprobe_register(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530850{
851 struct uprobe *uprobe;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100852 int ret;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530853
Oleg Nesterovf0744af2012-11-21 18:01:43 +0100854 /* Racy, just to catch the obvious mistakes */
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530855 if (offset > i_size_read(inode))
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100856 return -EINVAL;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530857
Oleg Nesterov66d06df2012-11-25 22:48:37 +0100858 retry:
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530859 uprobe = alloc_uprobe(inode, offset);
Oleg Nesterov66d06df2012-11-25 22:48:37 +0100860 if (!uprobe)
861 return -ENOMEM;
862 /*
863 * We can race with uprobe_unregister()->delete_uprobe().
864 * Check uprobe_is_active() and retry if it is false.
865 */
866 down_write(&uprobe->register_rwsem);
867 ret = -EAGAIN;
868 if (likely(uprobe_is_active(uprobe))) {
Oleg Nesterov9a98e032012-11-23 20:15:17 +0100869 ret = __uprobe_register(uprobe, uc);
870 if (ret)
Oleg Nesterov04aab9b2012-11-23 19:43:50 +0100871 __uprobe_unregister(uprobe, uc);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530872 }
Oleg Nesterov66d06df2012-11-25 22:48:37 +0100873 up_write(&uprobe->register_rwsem);
874 put_uprobe(uprobe);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530875
Oleg Nesterov66d06df2012-11-25 22:48:37 +0100876 if (unlikely(ret == -EAGAIN))
877 goto retry;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530878 return ret;
879}
Josh Stonee8440c12013-01-13 19:03:34 +0100880EXPORT_SYMBOL_GPL(uprobe_register);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530881
882/*
Oleg Nesterovbdf86472013-02-03 19:21:12 +0100883 * uprobe_apply - unregister a already registered probe.
884 * @inode: the file in which the probe has to be removed.
885 * @offset: offset from the start of the file.
886 * @uc: consumer which wants to add more or remove some breakpoints
887 * @add: add or remove the breakpoints
888 */
889int uprobe_apply(struct inode *inode, loff_t offset,
890 struct uprobe_consumer *uc, bool add)
891{
892 struct uprobe *uprobe;
893 struct uprobe_consumer *con;
894 int ret = -ENOENT;
895
896 uprobe = find_uprobe(inode, offset);
897 if (!uprobe)
898 return ret;
899
900 down_write(&uprobe->register_rwsem);
901 for (con = uprobe->consumers; con && con != uc ; con = con->next)
902 ;
903 if (con)
904 ret = register_for_each_vma(uprobe, add ? uc : NULL);
905 up_write(&uprobe->register_rwsem);
906 put_uprobe(uprobe);
907
908 return ret;
909}
910
911/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100912 * uprobe_unregister - unregister a already registered probe.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530913 * @inode: the file in which the probe has to be removed.
914 * @offset: offset from the start of the file.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530915 * @uc: identify which probe if multiple probes are colocated.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530916 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530917void uprobe_unregister(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530918{
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100919 struct uprobe *uprobe;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530920
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530921 uprobe = find_uprobe(inode, offset);
922 if (!uprobe)
923 return;
924
Oleg Nesterove591c8d2012-11-24 17:29:40 +0100925 down_write(&uprobe->register_rwsem);
Oleg Nesterov04aab9b2012-11-23 19:43:50 +0100926 __uprobe_unregister(uprobe, uc);
Oleg Nesterove591c8d2012-11-24 17:29:40 +0100927 up_write(&uprobe->register_rwsem);
Sasha Levinc91368c2012-12-20 14:11:30 -0500928 put_uprobe(uprobe);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530929}
Josh Stonee8440c12013-01-13 19:03:34 +0100930EXPORT_SYMBOL_GPL(uprobe_unregister);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530931
Oleg Nesterovda1816b2012-12-29 17:49:11 +0100932static int unapply_uprobe(struct uprobe *uprobe, struct mm_struct *mm)
933{
934 struct vm_area_struct *vma;
935 int err = 0;
936
937 down_read(&mm->mmap_sem);
938 for (vma = mm->mmap; vma; vma = vma->vm_next) {
939 unsigned long vaddr;
940 loff_t offset;
941
942 if (!valid_vma(vma, false) ||
Oleg Nesterovf2817692013-03-17 18:54:44 +0100943 file_inode(vma->vm_file) != uprobe->inode)
Oleg Nesterovda1816b2012-12-29 17:49:11 +0100944 continue;
945
946 offset = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
947 if (uprobe->offset < offset ||
948 uprobe->offset >= offset + vma->vm_end - vma->vm_start)
949 continue;
950
951 vaddr = offset_to_vaddr(vma, uprobe->offset);
952 err |= remove_breakpoint(uprobe, mm, vaddr);
953 }
954 up_read(&mm->mmap_sem);
955
956 return err;
957}
958
Oleg Nesterov891c3972012-07-29 20:22:40 +0200959static struct rb_node *
960find_node_in_range(struct inode *inode, loff_t min, loff_t max)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530961{
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530962 struct rb_node *n = uprobes_tree.rb_node;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530963
964 while (n) {
Oleg Nesterov891c3972012-07-29 20:22:40 +0200965 struct uprobe *u = rb_entry(n, struct uprobe, rb_node);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100966
Oleg Nesterov891c3972012-07-29 20:22:40 +0200967 if (inode < u->inode) {
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530968 n = n->rb_left;
Oleg Nesterov891c3972012-07-29 20:22:40 +0200969 } else if (inode > u->inode) {
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530970 n = n->rb_right;
Oleg Nesterov891c3972012-07-29 20:22:40 +0200971 } else {
972 if (max < u->offset)
973 n = n->rb_left;
974 else if (min > u->offset)
975 n = n->rb_right;
976 else
977 break;
978 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530979 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100980
Oleg Nesterov891c3972012-07-29 20:22:40 +0200981 return n;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530982}
983
984/*
Oleg Nesterov891c3972012-07-29 20:22:40 +0200985 * For a given range in vma, build a list of probes that need to be inserted.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530986 */
Oleg Nesterov891c3972012-07-29 20:22:40 +0200987static void build_probe_list(struct inode *inode,
988 struct vm_area_struct *vma,
989 unsigned long start, unsigned long end,
990 struct list_head *head)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530991{
Oleg Nesterov891c3972012-07-29 20:22:40 +0200992 loff_t min, max;
Oleg Nesterov891c3972012-07-29 20:22:40 +0200993 struct rb_node *n, *t;
994 struct uprobe *u;
995
996 INIT_LIST_HEAD(head);
Oleg Nesterovcb113b42012-07-29 20:22:42 +0200997 min = vaddr_to_offset(vma, start);
Oleg Nesterov891c3972012-07-29 20:22:40 +0200998 max = min + (end - start) - 1;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530999
Oleg Nesterov6f47caa2012-08-18 17:01:57 +02001000 spin_lock(&uprobes_treelock);
Oleg Nesterov891c3972012-07-29 20:22:40 +02001001 n = find_node_in_range(inode, min, max);
1002 if (n) {
1003 for (t = n; t; t = rb_prev(t)) {
1004 u = rb_entry(t, struct uprobe, rb_node);
1005 if (u->inode != inode || u->offset < min)
1006 break;
1007 list_add(&u->pending_list, head);
1008 atomic_inc(&u->ref);
1009 }
1010 for (t = n; (t = rb_next(t)); ) {
1011 u = rb_entry(t, struct uprobe, rb_node);
1012 if (u->inode != inode || u->offset > max)
1013 break;
1014 list_add(&u->pending_list, head);
1015 atomic_inc(&u->ref);
1016 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301017 }
Oleg Nesterov6f47caa2012-08-18 17:01:57 +02001018 spin_unlock(&uprobes_treelock);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301019}
1020
1021/*
Oleg Nesterov5e5be712012-08-06 14:49:56 +02001022 * Called from mmap_region/vma_adjust with mm->mmap_sem acquired.
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301023 *
Oleg Nesterov5e5be712012-08-06 14:49:56 +02001024 * Currently we ignore all errors and always return 0, the callers
1025 * can't handle the failure anyway.
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301026 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001027int uprobe_mmap(struct vm_area_struct *vma)
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301028{
1029 struct list_head tmp_list;
Oleg Nesterov665605a2012-07-29 20:22:29 +02001030 struct uprobe *uprobe, *u;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301031 struct inode *inode;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301032
Oleg Nesterov441f1eb2012-11-25 19:54:29 +01001033 if (no_uprobe_events() || !valid_vma(vma, true))
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001034 return 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301035
Oleg Nesterovf2817692013-03-17 18:54:44 +01001036 inode = file_inode(vma->vm_file);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301037 if (!inode)
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001038 return 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301039
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301040 mutex_lock(uprobes_mmap_hash(inode));
Oleg Nesterov891c3972012-07-29 20:22:40 +02001041 build_probe_list(inode, vma, vma->vm_start, vma->vm_end, &tmp_list);
Oleg Nesterov806a98b2012-12-27 18:21:11 +01001042 /*
1043 * We can race with uprobe_unregister(), this uprobe can be already
1044 * removed. But in this case filter_chain() must return false, all
1045 * consumers have gone away.
1046 */
Oleg Nesterov665605a2012-07-29 20:22:29 +02001047 list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
Oleg Nesterov806a98b2012-12-27 18:21:11 +01001048 if (!fatal_signal_pending(current) &&
Oleg Nesterov8a7f2fa2012-12-28 17:58:38 +01001049 filter_chain(uprobe, UPROBE_FILTER_MMAP, vma->vm_mm)) {
Oleg Nesterov57683f72012-07-29 20:22:47 +02001050 unsigned long vaddr = offset_to_vaddr(vma, uprobe->offset);
Oleg Nesterov5e5be712012-08-06 14:49:56 +02001051 install_breakpoint(uprobe, vma->vm_mm, vma, vaddr);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301052 }
1053 put_uprobe(uprobe);
1054 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301055 mutex_unlock(uprobes_mmap_hash(inode));
1056
Oleg Nesterov5e5be712012-08-06 14:49:56 +02001057 return 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301058}
1059
Oleg Nesterov9f68f672012-08-19 16:15:09 +02001060static bool
1061vma_has_uprobes(struct vm_area_struct *vma, unsigned long start, unsigned long end)
1062{
1063 loff_t min, max;
1064 struct inode *inode;
1065 struct rb_node *n;
1066
Oleg Nesterovf2817692013-03-17 18:54:44 +01001067 inode = file_inode(vma->vm_file);
Oleg Nesterov9f68f672012-08-19 16:15:09 +02001068
1069 min = vaddr_to_offset(vma, start);
1070 max = min + (end - start) - 1;
1071
1072 spin_lock(&uprobes_treelock);
1073 n = find_node_in_range(inode, min, max);
1074 spin_unlock(&uprobes_treelock);
1075
1076 return !!n;
1077}
1078
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301079/*
1080 * Called in context of a munmap of a vma.
1081 */
Srikar Dronamrajucbc91f72012-04-11 16:05:27 +05301082void uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end)
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301083{
Oleg Nesterov441f1eb2012-11-25 19:54:29 +01001084 if (no_uprobe_events() || !valid_vma(vma, false))
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301085 return;
1086
Oleg Nesterov2fd611a2012-07-29 20:22:31 +02001087 if (!atomic_read(&vma->vm_mm->mm_users)) /* called by mmput() ? */
1088 return;
1089
Oleg Nesterov9f68f672012-08-19 16:15:09 +02001090 if (!test_bit(MMF_HAS_UPROBES, &vma->vm_mm->flags) ||
1091 test_bit(MMF_RECALC_UPROBES, &vma->vm_mm->flags))
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +02001092 return;
1093
Oleg Nesterov9f68f672012-08-19 16:15:09 +02001094 if (vma_has_uprobes(vma, start, end))
1095 set_bit(MMF_RECALC_UPROBES, &vma->vm_mm->flags);
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301096}
1097
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301098/* Slot allocation for XOL */
1099static int xol_add_vma(struct xol_area *area)
1100{
Oleg Nesterovc8a82532012-12-30 17:40:39 +01001101 struct mm_struct *mm = current->mm;
1102 int ret = -EALREADY;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301103
1104 down_write(&mm->mmap_sem);
1105 if (mm->uprobes_state.xol_area)
1106 goto fail;
1107
1108 ret = -ENOMEM;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301109 /* Try to map as high as possible, this is only a hint. */
1110 area->vaddr = get_unmapped_area(NULL, TASK_SIZE - PAGE_SIZE, PAGE_SIZE, 0, 0);
1111 if (area->vaddr & ~PAGE_MASK) {
1112 ret = area->vaddr;
1113 goto fail;
1114 }
1115
1116 ret = install_special_mapping(mm, area->vaddr, PAGE_SIZE,
1117 VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO, &area->page);
1118 if (ret)
1119 goto fail;
1120
1121 smp_wmb(); /* pairs with get_xol_area() */
1122 mm->uprobes_state.xol_area = area;
1123 ret = 0;
Oleg Nesterovc8a82532012-12-30 17:40:39 +01001124 fail:
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301125 up_write(&mm->mmap_sem);
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301126
1127 return ret;
1128}
1129
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301130/*
Oleg Nesterov9b545df2012-12-31 16:39:49 +01001131 * get_xol_area - Allocate process's xol_area if necessary.
1132 * This area will be used for storing instructions for execution out of line.
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301133 *
1134 * Returns the allocated area or NULL.
1135 */
Oleg Nesterov9b545df2012-12-31 16:39:49 +01001136static struct xol_area *get_xol_area(void)
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301137{
Oleg Nesterov9b545df2012-12-31 16:39:49 +01001138 struct mm_struct *mm = current->mm;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301139 struct xol_area *area;
1140
Oleg Nesterov9b545df2012-12-31 16:39:49 +01001141 area = mm->uprobes_state.xol_area;
1142 if (area)
1143 goto ret;
1144
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301145 area = kzalloc(sizeof(*area), GFP_KERNEL);
1146 if (unlikely(!area))
Oleg Nesterovc8a82532012-12-30 17:40:39 +01001147 goto out;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301148
1149 area->bitmap = kzalloc(BITS_TO_LONGS(UINSNS_PER_PAGE) * sizeof(long), GFP_KERNEL);
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301150 if (!area->bitmap)
Oleg Nesterovc8a82532012-12-30 17:40:39 +01001151 goto free_area;
1152
1153 area->page = alloc_page(GFP_HIGHUSER);
1154 if (!area->page)
1155 goto free_bitmap;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301156
1157 init_waitqueue_head(&area->wq);
1158 if (!xol_add_vma(area))
1159 return area;
1160
Oleg Nesterovc8a82532012-12-30 17:40:39 +01001161 __free_page(area->page);
1162 free_bitmap:
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301163 kfree(area->bitmap);
Oleg Nesterovc8a82532012-12-30 17:40:39 +01001164 free_area:
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301165 kfree(area);
Oleg Nesterovc8a82532012-12-30 17:40:39 +01001166 out:
Oleg Nesterov9b545df2012-12-31 16:39:49 +01001167 area = mm->uprobes_state.xol_area;
1168 ret:
1169 smp_read_barrier_depends(); /* pairs with wmb in xol_add_vma() */
1170 return area;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301171}
1172
1173/*
1174 * uprobe_clear_state - Free the area allocated for slots.
1175 */
1176void uprobe_clear_state(struct mm_struct *mm)
1177{
1178 struct xol_area *area = mm->uprobes_state.xol_area;
1179
1180 if (!area)
1181 return;
1182
1183 put_page(area->page);
1184 kfree(area->bitmap);
1185 kfree(area);
1186}
1187
Oleg Nesterov32cdba12012-11-14 19:03:42 +01001188void uprobe_start_dup_mmap(void)
1189{
1190 percpu_down_read(&dup_mmap_sem);
1191}
1192
1193void uprobe_end_dup_mmap(void)
1194{
1195 percpu_up_read(&dup_mmap_sem);
1196}
1197
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +02001198void uprobe_dup_mmap(struct mm_struct *oldmm, struct mm_struct *newmm)
1199{
Oleg Nesterov61559a82012-08-08 17:17:46 +02001200 newmm->uprobes_state.xol_area = NULL;
1201
Oleg Nesterov9f68f672012-08-19 16:15:09 +02001202 if (test_bit(MMF_HAS_UPROBES, &oldmm->flags)) {
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +02001203 set_bit(MMF_HAS_UPROBES, &newmm->flags);
Oleg Nesterov9f68f672012-08-19 16:15:09 +02001204 /* unconditionally, dup_mmap() skips VM_DONTCOPY vmas */
1205 set_bit(MMF_RECALC_UPROBES, &newmm->flags);
1206 }
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +02001207}
1208
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301209/*
1210 * - search for a free slot.
1211 */
1212static unsigned long xol_take_insn_slot(struct xol_area *area)
1213{
1214 unsigned long slot_addr;
1215 int slot_nr;
1216
1217 do {
1218 slot_nr = find_first_zero_bit(area->bitmap, UINSNS_PER_PAGE);
1219 if (slot_nr < UINSNS_PER_PAGE) {
1220 if (!test_and_set_bit(slot_nr, area->bitmap))
1221 break;
1222
1223 slot_nr = UINSNS_PER_PAGE;
1224 continue;
1225 }
1226 wait_event(area->wq, (atomic_read(&area->slot_count) < UINSNS_PER_PAGE));
1227 } while (slot_nr >= UINSNS_PER_PAGE);
1228
1229 slot_addr = area->vaddr + (slot_nr * UPROBE_XOL_SLOT_BYTES);
1230 atomic_inc(&area->slot_count);
1231
1232 return slot_addr;
1233}
1234
1235/*
Oleg Nesterova6cb3f62012-12-31 18:00:06 +01001236 * xol_get_insn_slot - allocate a slot for xol.
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301237 * Returns the allocated slot address or 0.
1238 */
Oleg Nesterova6cb3f62012-12-31 18:00:06 +01001239static unsigned long xol_get_insn_slot(struct uprobe *uprobe)
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301240{
1241 struct xol_area *area;
1242 unsigned long offset;
Oleg Nesterova6cb3f62012-12-31 18:00:06 +01001243 unsigned long xol_vaddr;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301244 void *vaddr;
1245
Oleg Nesterov9b545df2012-12-31 16:39:49 +01001246 area = get_xol_area();
1247 if (!area)
1248 return 0;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301249
Oleg Nesterova6cb3f62012-12-31 18:00:06 +01001250 xol_vaddr = xol_take_insn_slot(area);
1251 if (unlikely(!xol_vaddr))
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301252 return 0;
1253
Oleg Nesterova6cb3f62012-12-31 18:00:06 +01001254 /* Initialize the slot */
1255 offset = xol_vaddr & ~PAGE_MASK;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301256 vaddr = kmap_atomic(area->page);
1257 memcpy(vaddr + offset, uprobe->arch.insn, MAX_UINSN_BYTES);
1258 kunmap_atomic(vaddr);
Rabin Vincent65b6ecc2012-11-14 18:27:07 +01001259 /*
1260 * We probably need flush_icache_user_range() but it needs vma.
1261 * This should work on supported architectures too.
1262 */
1263 flush_dcache_page(area->page);
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301264
Oleg Nesterova6cb3f62012-12-31 18:00:06 +01001265 return xol_vaddr;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301266}
1267
1268/*
1269 * xol_free_insn_slot - If slot was earlier allocated by
1270 * @xol_get_insn_slot(), make the slot available for
1271 * subsequent requests.
1272 */
1273static void xol_free_insn_slot(struct task_struct *tsk)
1274{
1275 struct xol_area *area;
1276 unsigned long vma_end;
1277 unsigned long slot_addr;
1278
1279 if (!tsk->mm || !tsk->mm->uprobes_state.xol_area || !tsk->utask)
1280 return;
1281
1282 slot_addr = tsk->utask->xol_vaddr;
Oleg Nesterovaf4355e2012-12-31 18:37:11 +01001283 if (unlikely(!slot_addr))
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301284 return;
1285
1286 area = tsk->mm->uprobes_state.xol_area;
1287 vma_end = area->vaddr + PAGE_SIZE;
1288 if (area->vaddr <= slot_addr && slot_addr < vma_end) {
1289 unsigned long offset;
1290 int slot_nr;
1291
1292 offset = slot_addr - area->vaddr;
1293 slot_nr = offset / UPROBE_XOL_SLOT_BYTES;
1294 if (slot_nr >= UINSNS_PER_PAGE)
1295 return;
1296
1297 clear_bit(slot_nr, area->bitmap);
1298 atomic_dec(&area->slot_count);
1299 if (waitqueue_active(&area->wq))
1300 wake_up(&area->wq);
1301
1302 tsk->utask->xol_vaddr = 0;
1303 }
1304}
1305
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301306/**
1307 * uprobe_get_swbp_addr - compute address of swbp given post-swbp regs
1308 * @regs: Reflects the saved state of the task after it has hit a breakpoint
1309 * instruction.
1310 * Return the address of the breakpoint instruction.
1311 */
1312unsigned long __weak uprobe_get_swbp_addr(struct pt_regs *regs)
1313{
1314 return instruction_pointer(regs) - UPROBE_SWBP_INSN_SIZE;
1315}
1316
1317/*
1318 * Called with no locks held.
1319 * Called in context of a exiting or a exec-ing thread.
1320 */
1321void uprobe_free_utask(struct task_struct *t)
1322{
1323 struct uprobe_task *utask = t->utask;
1324
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301325 if (!utask)
1326 return;
1327
1328 if (utask->active_uprobe)
1329 put_uprobe(utask->active_uprobe);
1330
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301331 xol_free_insn_slot(t);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301332 kfree(utask);
1333 t->utask = NULL;
1334}
1335
1336/*
1337 * Called in context of a new clone/fork from copy_process.
1338 */
1339void uprobe_copy_process(struct task_struct *t)
1340{
1341 t->utask = NULL;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301342}
1343
1344/*
Oleg Nesterov5a2df662012-12-31 17:03:32 +01001345 * Allocate a uprobe_task object for the task if if necessary.
1346 * Called when the thread hits a breakpoint.
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301347 *
1348 * Returns:
1349 * - pointer to new uprobe_task on success
1350 * - NULL otherwise
1351 */
Oleg Nesterov5a2df662012-12-31 17:03:32 +01001352static struct uprobe_task *get_utask(void)
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301353{
Oleg Nesterov5a2df662012-12-31 17:03:32 +01001354 if (!current->utask)
1355 current->utask = kzalloc(sizeof(struct uprobe_task), GFP_KERNEL);
1356 return current->utask;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301357}
1358
1359/* Prepare to single-step probed instruction out of line. */
1360static int
Oleg Nesterova6cb3f62012-12-31 18:00:06 +01001361pre_ssout(struct uprobe *uprobe, struct pt_regs *regs, unsigned long bp_vaddr)
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301362{
Oleg Nesterova6cb3f62012-12-31 18:00:06 +01001363 struct uprobe_task *utask;
1364 unsigned long xol_vaddr;
Oleg Nesterovaba51022012-12-31 18:12:48 +01001365 int err;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301366
Oleg Nesterov608e7422012-12-31 18:20:42 +01001367 utask = get_utask();
1368 if (!utask)
1369 return -ENOMEM;
Oleg Nesterova6cb3f62012-12-31 18:00:06 +01001370
1371 xol_vaddr = xol_get_insn_slot(uprobe);
1372 if (!xol_vaddr)
1373 return -ENOMEM;
1374
1375 utask->xol_vaddr = xol_vaddr;
1376 utask->vaddr = bp_vaddr;
1377
Oleg Nesterovaba51022012-12-31 18:12:48 +01001378 err = arch_uprobe_pre_xol(&uprobe->arch, regs);
1379 if (unlikely(err)) {
1380 xol_free_insn_slot(current);
1381 return err;
1382 }
1383
Oleg Nesterov608e7422012-12-31 18:20:42 +01001384 utask->active_uprobe = uprobe;
1385 utask->state = UTASK_SSTEP;
Oleg Nesterovaba51022012-12-31 18:12:48 +01001386 return 0;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301387}
1388
1389/*
1390 * If we are singlestepping, then ensure this thread is not connected to
1391 * non-fatal signals until completion of singlestep. When xol insn itself
1392 * triggers the signal, restart the original insn even if the task is
1393 * already SIGKILL'ed (since coredump should report the correct ip). This
1394 * is even more important if the task has a handler for SIGSEGV/etc, The
1395 * _same_ instruction should be repeated again after return from the signal
1396 * handler, and SSTEP can never finish in this case.
1397 */
1398bool uprobe_deny_signal(void)
1399{
1400 struct task_struct *t = current;
1401 struct uprobe_task *utask = t->utask;
1402
1403 if (likely(!utask || !utask->active_uprobe))
1404 return false;
1405
1406 WARN_ON_ONCE(utask->state != UTASK_SSTEP);
1407
1408 if (signal_pending(t)) {
1409 spin_lock_irq(&t->sighand->siglock);
1410 clear_tsk_thread_flag(t, TIF_SIGPENDING);
1411 spin_unlock_irq(&t->sighand->siglock);
1412
1413 if (__fatal_signal_pending(t) || arch_uprobe_xol_was_trapped(t)) {
1414 utask->state = UTASK_SSTEP_TRAPPED;
1415 set_tsk_thread_flag(t, TIF_UPROBE);
1416 set_tsk_thread_flag(t, TIF_NOTIFY_RESUME);
1417 }
1418 }
1419
1420 return true;
1421}
1422
1423/*
1424 * Avoid singlestepping the original instruction if the original instruction
1425 * is a NOP or can be emulated.
1426 */
1427static bool can_skip_sstep(struct uprobe *uprobe, struct pt_regs *regs)
1428{
Oleg Nesterov71434f22012-09-30 21:12:44 +02001429 if (test_bit(UPROBE_SKIP_SSTEP, &uprobe->flags)) {
Oleg Nesterov0578a972012-09-14 18:31:23 +02001430 if (arch_uprobe_skip_sstep(&uprobe->arch, regs))
1431 return true;
Oleg Nesterov71434f22012-09-30 21:12:44 +02001432 clear_bit(UPROBE_SKIP_SSTEP, &uprobe->flags);
Oleg Nesterov0578a972012-09-14 18:31:23 +02001433 }
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301434 return false;
1435}
1436
Oleg Nesterov499a4f32012-08-19 17:41:34 +02001437static void mmf_recalc_uprobes(struct mm_struct *mm)
1438{
1439 struct vm_area_struct *vma;
1440
1441 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1442 if (!valid_vma(vma, false))
1443 continue;
1444 /*
1445 * This is not strictly accurate, we can race with
1446 * uprobe_unregister() and see the already removed
1447 * uprobe if delete_uprobe() was not yet called.
Oleg Nesterov63633cb2012-11-22 18:30:15 +01001448 * Or this uprobe can be filtered out.
Oleg Nesterov499a4f32012-08-19 17:41:34 +02001449 */
1450 if (vma_has_uprobes(vma, vma->vm_start, vma->vm_end))
1451 return;
1452 }
1453
1454 clear_bit(MMF_HAS_UPROBES, &mm->flags);
1455}
1456
Ananth N Mavinakayanahalli0908ad62013-03-22 20:46:27 +05301457static int is_trap_at_addr(struct mm_struct *mm, unsigned long vaddr)
Oleg Nesterovec75fba2012-09-23 21:55:19 +02001458{
1459 struct page *page;
1460 uprobe_opcode_t opcode;
1461 int result;
1462
1463 pagefault_disable();
1464 result = __copy_from_user_inatomic(&opcode, (void __user*)vaddr,
1465 sizeof(opcode));
1466 pagefault_enable();
1467
1468 if (likely(result == 0))
1469 goto out;
1470
1471 result = get_user_pages(NULL, mm, vaddr, 1, 0, 1, &page, NULL);
1472 if (result < 0)
1473 return result;
1474
1475 copy_opcode(page, vaddr, &opcode);
1476 put_page(page);
1477 out:
Ananth N Mavinakayanahalli0908ad62013-03-22 20:46:27 +05301478 /* This needs to return true for any variant of the trap insn */
1479 return is_trap_insn(&opcode);
Oleg Nesterovec75fba2012-09-23 21:55:19 +02001480}
1481
Oleg Nesterovd790d342012-05-29 21:29:14 +02001482static struct uprobe *find_active_uprobe(unsigned long bp_vaddr, int *is_swbp)
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001483{
1484 struct mm_struct *mm = current->mm;
1485 struct uprobe *uprobe = NULL;
1486 struct vm_area_struct *vma;
1487
1488 down_read(&mm->mmap_sem);
1489 vma = find_vma(mm, bp_vaddr);
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001490 if (vma && vma->vm_start <= bp_vaddr) {
1491 if (valid_vma(vma, false)) {
Oleg Nesterovf2817692013-03-17 18:54:44 +01001492 struct inode *inode = file_inode(vma->vm_file);
Oleg Nesterovcb113b42012-07-29 20:22:42 +02001493 loff_t offset = vaddr_to_offset(vma, bp_vaddr);
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001494
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001495 uprobe = find_uprobe(inode, offset);
1496 }
Oleg Nesterovd790d342012-05-29 21:29:14 +02001497
1498 if (!uprobe)
Ananth N Mavinakayanahalli0908ad62013-03-22 20:46:27 +05301499 *is_swbp = is_trap_at_addr(mm, bp_vaddr);
Oleg Nesterovd790d342012-05-29 21:29:14 +02001500 } else {
1501 *is_swbp = -EFAULT;
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001502 }
Oleg Nesterov499a4f32012-08-19 17:41:34 +02001503
1504 if (!uprobe && test_and_clear_bit(MMF_RECALC_UPROBES, &mm->flags))
1505 mmf_recalc_uprobes(mm);
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001506 up_read(&mm->mmap_sem);
1507
1508 return uprobe;
1509}
1510
Oleg Nesterovda1816b2012-12-29 17:49:11 +01001511static void handler_chain(struct uprobe *uprobe, struct pt_regs *regs)
1512{
1513 struct uprobe_consumer *uc;
1514 int remove = UPROBE_HANDLER_REMOVE;
1515
1516 down_read(&uprobe->register_rwsem);
1517 for (uc = uprobe->consumers; uc; uc = uc->next) {
1518 int rc = uc->handler(uc, regs);
1519
1520 WARN(rc & ~UPROBE_HANDLER_MASK,
1521 "bad rc=0x%x from %pf()\n", rc, uc->handler);
1522 remove &= rc;
1523 }
1524
1525 if (remove && uprobe->consumers) {
1526 WARN_ON(!uprobe_is_active(uprobe));
1527 unapply_uprobe(uprobe, current->mm);
1528 }
1529 up_read(&uprobe->register_rwsem);
1530}
1531
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301532/*
1533 * Run handler and ask thread to singlestep.
1534 * Ensure all non-fatal signals cannot interrupt thread while it singlesteps.
1535 */
1536static void handle_swbp(struct pt_regs *regs)
1537{
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301538 struct uprobe *uprobe;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301539 unsigned long bp_vaddr;
Oleg Nesterov56bb4cf2012-05-29 21:29:47 +02001540 int uninitialized_var(is_swbp);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301541
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301542 bp_vaddr = uprobe_get_swbp_addr(regs);
Oleg Nesterovd790d342012-05-29 21:29:14 +02001543 uprobe = find_active_uprobe(bp_vaddr, &is_swbp);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301544
1545 if (!uprobe) {
Oleg Nesterov56bb4cf2012-05-29 21:29:47 +02001546 if (is_swbp > 0) {
1547 /* No matching uprobe; signal SIGTRAP. */
1548 send_sig(SIGTRAP, current, 0);
1549 } else {
1550 /*
1551 * Either we raced with uprobe_unregister() or we can't
1552 * access this memory. The latter is only possible if
1553 * another thread plays with our ->mm. In both cases
1554 * we can simply restart. If this vma was unmapped we
1555 * can pretend this insn was not executed yet and get
1556 * the (correct) SIGSEGV after restart.
1557 */
1558 instruction_pointer_set(regs, bp_vaddr);
1559 }
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301560 return;
1561 }
Oleg Nesterov74e59df2012-12-30 15:54:08 +01001562
1563 /* change it in advance for ->handler() and restart */
1564 instruction_pointer_set(regs, bp_vaddr);
1565
Oleg Nesterov142b18d2012-09-29 21:56:57 +02001566 /*
1567 * TODO: move copy_insn/etc into _register and remove this hack.
1568 * After we hit the bp, _unregister + _register can install the
1569 * new and not-yet-analyzed uprobe at the same address, restart.
1570 */
1571 smp_rmb(); /* pairs with wmb() in install_breakpoint() */
Oleg Nesterov71434f22012-09-30 21:12:44 +02001572 if (unlikely(!test_bit(UPROBE_COPY_INSN, &uprobe->flags)))
Oleg Nesterov74e59df2012-12-30 15:54:08 +01001573 goto out;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301574
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301575 handler_chain(uprobe, regs);
Oleg Nesterov0578a972012-09-14 18:31:23 +02001576 if (can_skip_sstep(uprobe, regs))
1577 goto out;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301578
Oleg Nesterov608e7422012-12-31 18:20:42 +01001579 if (!pre_ssout(uprobe, regs, bp_vaddr))
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301580 return;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301581
Oleg Nesterov74e59df2012-12-30 15:54:08 +01001582 /* can_skip_sstep() succeeded, or restart if can't singlestep */
Oleg Nesterov0578a972012-09-14 18:31:23 +02001583out:
Sebastian Andrzej Siewior8bd87442012-08-07 18:12:30 +02001584 put_uprobe(uprobe);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301585}
1586
1587/*
1588 * Perform required fix-ups and disable singlestep.
1589 * Allow pending signals to take effect.
1590 */
1591static void handle_singlestep(struct uprobe_task *utask, struct pt_regs *regs)
1592{
1593 struct uprobe *uprobe;
1594
1595 uprobe = utask->active_uprobe;
1596 if (utask->state == UTASK_SSTEP_ACK)
1597 arch_uprobe_post_xol(&uprobe->arch, regs);
1598 else if (utask->state == UTASK_SSTEP_TRAPPED)
1599 arch_uprobe_abort_xol(&uprobe->arch, regs);
1600 else
1601 WARN_ON_ONCE(1);
1602
1603 put_uprobe(uprobe);
1604 utask->active_uprobe = NULL;
1605 utask->state = UTASK_RUNNING;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301606 xol_free_insn_slot(current);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301607
1608 spin_lock_irq(&current->sighand->siglock);
1609 recalc_sigpending(); /* see uprobe_deny_signal() */
1610 spin_unlock_irq(&current->sighand->siglock);
1611}
1612
1613/*
Oleg Nesterov1b08e9072012-09-14 18:52:10 +02001614 * On breakpoint hit, breakpoint notifier sets the TIF_UPROBE flag and
1615 * allows the thread to return from interrupt. After that handle_swbp()
1616 * sets utask->active_uprobe.
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301617 *
Oleg Nesterov1b08e9072012-09-14 18:52:10 +02001618 * On singlestep exception, singlestep notifier sets the TIF_UPROBE flag
1619 * and allows the thread to return from interrupt.
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301620 *
1621 * While returning to userspace, thread notices the TIF_UPROBE flag and calls
1622 * uprobe_notify_resume().
1623 */
1624void uprobe_notify_resume(struct pt_regs *regs)
1625{
1626 struct uprobe_task *utask;
1627
Oleg Nesterovdb023ea2012-09-14 19:05:46 +02001628 clear_thread_flag(TIF_UPROBE);
1629
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301630 utask = current->utask;
Oleg Nesterov1b08e9072012-09-14 18:52:10 +02001631 if (utask && utask->active_uprobe)
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301632 handle_singlestep(utask, regs);
Oleg Nesterov1b08e9072012-09-14 18:52:10 +02001633 else
1634 handle_swbp(regs);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301635}
1636
1637/*
1638 * uprobe_pre_sstep_notifier gets called from interrupt context as part of
1639 * notifier mechanism. Set TIF_UPROBE flag and indicate breakpoint hit.
1640 */
1641int uprobe_pre_sstep_notifier(struct pt_regs *regs)
1642{
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +02001643 if (!current->mm || !test_bit(MMF_HAS_UPROBES, &current->mm->flags))
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301644 return 0;
1645
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301646 set_thread_flag(TIF_UPROBE);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301647 return 1;
1648}
1649
1650/*
1651 * uprobe_post_sstep_notifier gets called in interrupt context as part of notifier
1652 * mechanism. Set TIF_UPROBE flag and indicate completion of singlestep.
1653 */
1654int uprobe_post_sstep_notifier(struct pt_regs *regs)
1655{
1656 struct uprobe_task *utask = current->utask;
1657
1658 if (!current->mm || !utask || !utask->active_uprobe)
1659 /* task is currently not uprobed */
1660 return 0;
1661
1662 utask->state = UTASK_SSTEP_ACK;
1663 set_thread_flag(TIF_UPROBE);
1664 return 1;
1665}
1666
1667static struct notifier_block uprobe_exception_nb = {
1668 .notifier_call = arch_uprobe_exception_notify,
1669 .priority = INT_MAX-1, /* notified after kprobes, kgdb */
1670};
1671
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301672static int __init init_uprobes(void)
1673{
1674 int i;
1675
Oleg Nesterov66d06df2012-11-25 22:48:37 +01001676 for (i = 0; i < UPROBES_HASH_SZ; i++)
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301677 mutex_init(&uprobes_mmap_mutex[i]);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301678
Oleg Nesterov32cdba12012-11-14 19:03:42 +01001679 if (percpu_init_rwsem(&dup_mmap_sem))
1680 return -ENOMEM;
1681
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301682 return register_die_notifier(&uprobe_exception_nb);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301683}
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301684module_init(init_uprobes);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301685
1686static void __exit exit_uprobes(void)
1687{
1688}
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301689module_exit(exit_uprobes);