blob: ba6acfe7c0de557d2fb8c29e4815afc483cf3691 [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 Nesterovab0d8052013-03-24 18:24:37 +0100190static void copy_from_page(struct page *page, unsigned long vaddr, void *dst, int len)
Oleg Nesterovcceb55a2012-09-23 21:10:18 +0200191{
192 void *kaddr = kmap_atomic(page);
Oleg Nesterovab0d8052013-03-24 18:24:37 +0100193 memcpy(dst, kaddr + (vaddr & ~PAGE_MASK), len);
Oleg Nesterovcceb55a2012-09-23 21:10:18 +0200194 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 Nesterovab0d8052013-03-24 18:24:37 +0100211 copy_from_page(page, vaddr, &old_opcode, UPROBE_SWBP_INSN_SIZE);
Oleg Nesteroved6f6a52012-09-23 21:30:44 +0200212 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;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530503
504 if (!filp)
505 return -EINVAL;
506
Oleg Nesterovcc359d12012-06-15 17:43:25 +0200507 if (!mapping->a_ops->readpage)
508 return -EIO;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530509 /*
510 * Ensure that the page that has the original instruction is
511 * populated and in page-cache.
512 */
Oleg Nesterov2edb7b52013-03-24 18:37:48 +0100513 page = read_mapping_page(mapping, offset >> PAGE_CACHE_SHIFT, filp);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530514 if (IS_ERR(page))
515 return PTR_ERR(page);
516
Oleg Nesterov2edb7b52013-03-24 18:37:48 +0100517 copy_from_page(page, offset, insn, nbytes);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530518 page_cache_release(page);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100519
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530520 return 0;
521}
522
Oleg Nesterovd4366152012-06-15 17:43:42 +0200523static int copy_insn(struct uprobe *uprobe, struct file *filp)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530524{
525 struct address_space *mapping;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530526 unsigned long nbytes;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100527 int bytes;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530528
Oleg Nesterovd4366152012-06-15 17:43:42 +0200529 nbytes = PAGE_SIZE - (uprobe->offset & ~PAGE_MASK);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530530 mapping = uprobe->inode->i_mapping;
531
532 /* Instruction at end of binary; copy only available bytes */
533 if (uprobe->offset + MAX_UINSN_BYTES > uprobe->inode->i_size)
534 bytes = uprobe->inode->i_size - uprobe->offset;
535 else
536 bytes = MAX_UINSN_BYTES;
537
538 /* Instruction at the page-boundary; copy bytes in second page */
539 if (nbytes < bytes) {
Oleg Nesterovfc36f592012-06-15 17:43:44 +0200540 int err = __copy_insn(mapping, filp, uprobe->arch.insn + nbytes,
541 bytes - nbytes, uprobe->offset + nbytes);
542 if (err)
543 return err;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530544 bytes = nbytes;
545 }
Oleg Nesterovd4366152012-06-15 17:43:42 +0200546 return __copy_insn(mapping, filp, uprobe->arch.insn, bytes, uprobe->offset);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530547}
548
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200549static int prepare_uprobe(struct uprobe *uprobe, struct file *file,
550 struct mm_struct *mm, unsigned long vaddr)
551{
552 int ret = 0;
553
Oleg Nesterov71434f22012-09-30 21:12:44 +0200554 if (test_bit(UPROBE_COPY_INSN, &uprobe->flags))
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200555 return ret;
556
Oleg Nesterovd4d3ccc2012-11-24 18:51:34 +0100557 /* TODO: move this into _register, until then we abuse this sem. */
558 down_write(&uprobe->consumer_rwsem);
Oleg Nesterov71434f22012-09-30 21:12:44 +0200559 if (test_bit(UPROBE_COPY_INSN, &uprobe->flags))
Oleg Nesterov4710f052012-09-30 20:31:41 +0200560 goto out;
561
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200562 ret = copy_insn(uprobe, file);
563 if (ret)
564 goto out;
565
566 ret = -ENOTSUPP;
Ananth N Mavinakayanahalli0908ad62013-03-22 20:46:27 +0530567 if (is_trap_insn((uprobe_opcode_t *)uprobe->arch.insn))
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200568 goto out;
569
570 ret = arch_uprobe_analyze_insn(&uprobe->arch, mm, vaddr);
571 if (ret)
572 goto out;
573
574 /* write_opcode() assumes we don't cross page boundary */
575 BUG_ON((uprobe->offset & ~PAGE_MASK) +
576 UPROBE_SWBP_INSN_SIZE > PAGE_SIZE);
577
578 smp_wmb(); /* pairs with rmb() in find_active_uprobe() */
Oleg Nesterov71434f22012-09-30 21:12:44 +0200579 set_bit(UPROBE_COPY_INSN, &uprobe->flags);
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200580
581 out:
Oleg Nesterovd4d3ccc2012-11-24 18:51:34 +0100582 up_write(&uprobe->consumer_rwsem);
Oleg Nesterov4710f052012-09-30 20:31:41 +0200583
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200584 return ret;
585}
586
Oleg Nesterov8a7f2fa2012-12-28 17:58:38 +0100587static inline bool consumer_filter(struct uprobe_consumer *uc,
588 enum uprobe_filter_ctx ctx, struct mm_struct *mm)
Oleg Nesterov806a98b2012-12-27 18:21:11 +0100589{
Oleg Nesterov8a7f2fa2012-12-28 17:58:38 +0100590 return !uc->filter || uc->filter(uc, ctx, mm);
Oleg Nesterov806a98b2012-12-27 18:21:11 +0100591}
592
Oleg Nesterov8a7f2fa2012-12-28 17:58:38 +0100593static bool filter_chain(struct uprobe *uprobe,
594 enum uprobe_filter_ctx ctx, struct mm_struct *mm)
Oleg Nesterov63633cb2012-11-22 18:30:15 +0100595{
Oleg Nesterov1ff6fee2012-11-24 18:15:46 +0100596 struct uprobe_consumer *uc;
597 bool ret = false;
598
599 down_read(&uprobe->consumer_rwsem);
600 for (uc = uprobe->consumers; uc; uc = uc->next) {
Oleg Nesterov8a7f2fa2012-12-28 17:58:38 +0100601 ret = consumer_filter(uc, ctx, mm);
Oleg Nesterov1ff6fee2012-11-24 18:15:46 +0100602 if (ret)
603 break;
604 }
605 up_read(&uprobe->consumer_rwsem);
606
607 return ret;
Oleg Nesterov63633cb2012-11-22 18:30:15 +0100608}
609
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530610static int
611install_breakpoint(struct uprobe *uprobe, struct mm_struct *mm,
Oleg Nesterov816c03f2012-06-15 17:43:55 +0200612 struct vm_area_struct *vma, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530613{
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +0200614 bool first_uprobe;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530615 int ret;
616
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200617 ret = prepare_uprobe(uprobe, vma->vm_file, mm, vaddr);
618 if (ret)
619 return ret;
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530620
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +0200621 /*
622 * set MMF_HAS_UPROBES in advance for uprobe_pre_sstep_notifier(),
623 * the task can hit this breakpoint right after __replace_page().
624 */
625 first_uprobe = !test_bit(MMF_HAS_UPROBES, &mm->flags);
626 if (first_uprobe)
627 set_bit(MMF_HAS_UPROBES, &mm->flags);
628
Oleg Nesterov816c03f2012-06-15 17:43:55 +0200629 ret = set_swbp(&uprobe->arch, mm, vaddr);
Oleg Nesterov9f68f672012-08-19 16:15:09 +0200630 if (!ret)
631 clear_bit(MMF_RECALC_UPROBES, &mm->flags);
632 else if (first_uprobe)
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +0200633 clear_bit(MMF_HAS_UPROBES, &mm->flags);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530634
635 return ret;
636}
637
Oleg Nesterov076a3652012-09-30 18:54:53 +0200638static int
Oleg Nesterov816c03f2012-06-15 17:43:55 +0200639remove_breakpoint(struct uprobe *uprobe, struct mm_struct *mm, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530640{
Oleg Nesterov9f68f672012-08-19 16:15:09 +0200641 set_bit(MMF_RECALC_UPROBES, &mm->flags);
Oleg Nesterov076a3652012-09-30 18:54:53 +0200642 return set_orig_insn(&uprobe->arch, mm, vaddr);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530643}
644
Oleg Nesterov06b7bcd2012-11-25 22:01:42 +0100645static inline bool uprobe_is_active(struct uprobe *uprobe)
646{
647 return !RB_EMPTY_NODE(&uprobe->rb_node);
648}
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530649/*
Oleg Nesterov778b0322012-05-29 21:30:08 +0200650 * There could be threads that have already hit the breakpoint. They
651 * will recheck the current insn and restart if find_uprobe() fails.
652 * See find_active_uprobe().
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530653 */
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530654static void delete_uprobe(struct uprobe *uprobe)
655{
Oleg Nesterov06b7bcd2012-11-25 22:01:42 +0100656 if (WARN_ON(!uprobe_is_active(uprobe)))
657 return;
658
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200659 spin_lock(&uprobes_treelock);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530660 rb_erase(&uprobe->rb_node, &uprobes_tree);
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200661 spin_unlock(&uprobes_treelock);
Oleg Nesterov06b7bcd2012-11-25 22:01:42 +0100662 RB_CLEAR_NODE(&uprobe->rb_node); /* for uprobe_is_active() */
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530663 iput(uprobe->inode);
664 put_uprobe(uprobe);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530665}
666
Oleg Nesterov26872092012-06-15 17:43:33 +0200667struct map_info {
668 struct map_info *next;
669 struct mm_struct *mm;
Oleg Nesterov816c03f2012-06-15 17:43:55 +0200670 unsigned long vaddr;
Oleg Nesterov26872092012-06-15 17:43:33 +0200671};
672
673static inline struct map_info *free_map_info(struct map_info *info)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530674{
Oleg Nesterov26872092012-06-15 17:43:33 +0200675 struct map_info *next = info->next;
676 kfree(info);
677 return next;
678}
679
680static struct map_info *
681build_map_info(struct address_space *mapping, loff_t offset, bool is_register)
682{
683 unsigned long pgoff = offset >> PAGE_SHIFT;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530684 struct vm_area_struct *vma;
Oleg Nesterov26872092012-06-15 17:43:33 +0200685 struct map_info *curr = NULL;
686 struct map_info *prev = NULL;
687 struct map_info *info;
688 int more = 0;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100689
Oleg Nesterov26872092012-06-15 17:43:33 +0200690 again:
691 mutex_lock(&mapping->i_mmap_mutex);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700692 vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) {
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530693 if (!valid_vma(vma, is_register))
694 continue;
695
Oleg Nesterov7a5bfb62012-06-15 17:43:36 +0200696 if (!prev && !more) {
697 /*
698 * Needs GFP_NOWAIT to avoid i_mmap_mutex recursion through
699 * reclaim. This is optimistic, no harm done if it fails.
700 */
701 prev = kmalloc(sizeof(struct map_info),
702 GFP_NOWAIT | __GFP_NOMEMALLOC | __GFP_NOWARN);
703 if (prev)
704 prev->next = NULL;
705 }
Oleg Nesterov26872092012-06-15 17:43:33 +0200706 if (!prev) {
707 more++;
708 continue;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530709 }
710
Oleg Nesterov26872092012-06-15 17:43:33 +0200711 if (!atomic_inc_not_zero(&vma->vm_mm->mm_users))
712 continue;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100713
Oleg Nesterov26872092012-06-15 17:43:33 +0200714 info = prev;
715 prev = prev->next;
716 info->next = curr;
717 curr = info;
718
719 info->mm = vma->vm_mm;
Oleg Nesterov57683f72012-07-29 20:22:47 +0200720 info->vaddr = offset_to_vaddr(vma, offset);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530721 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530722 mutex_unlock(&mapping->i_mmap_mutex);
723
Oleg Nesterov26872092012-06-15 17:43:33 +0200724 if (!more)
725 goto out;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100726
Oleg Nesterov26872092012-06-15 17:43:33 +0200727 prev = curr;
728 while (curr) {
729 mmput(curr->mm);
730 curr = curr->next;
731 }
732
733 do {
734 info = kmalloc(sizeof(struct map_info), GFP_KERNEL);
735 if (!info) {
736 curr = ERR_PTR(-ENOMEM);
737 goto out;
738 }
739 info->next = prev;
740 prev = info;
741 } while (--more);
742
743 goto again;
744 out:
745 while (prev)
746 prev = free_map_info(prev);
747 return curr;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530748}
749
Oleg Nesterovbdf86472013-02-03 19:21:12 +0100750static int
751register_for_each_vma(struct uprobe *uprobe, struct uprobe_consumer *new)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530752{
Oleg Nesterovbdf86472013-02-03 19:21:12 +0100753 bool is_register = !!new;
Oleg Nesterov26872092012-06-15 17:43:33 +0200754 struct map_info *info;
755 int err = 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530756
Oleg Nesterov32cdba12012-11-14 19:03:42 +0100757 percpu_down_write(&dup_mmap_sem);
Oleg Nesterov26872092012-06-15 17:43:33 +0200758 info = build_map_info(uprobe->inode->i_mapping,
759 uprobe->offset, is_register);
Oleg Nesterov32cdba12012-11-14 19:03:42 +0100760 if (IS_ERR(info)) {
761 err = PTR_ERR(info);
762 goto out;
763 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100764
Oleg Nesterov26872092012-06-15 17:43:33 +0200765 while (info) {
766 struct mm_struct *mm = info->mm;
767 struct vm_area_struct *vma;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100768
Oleg Nesterov076a3652012-09-30 18:54:53 +0200769 if (err && is_register)
Oleg Nesterov26872092012-06-15 17:43:33 +0200770 goto free;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100771
Oleg Nesterov77fc4af2012-05-29 21:29:28 +0200772 down_write(&mm->mmap_sem);
Oleg Nesterovf4d6dfe2012-07-29 20:22:44 +0200773 vma = find_vma(mm, info->vaddr);
774 if (!vma || !valid_vma(vma, is_register) ||
Oleg Nesterovf2817692013-03-17 18:54:44 +0100775 file_inode(vma->vm_file) != uprobe->inode)
Oleg Nesterov26872092012-06-15 17:43:33 +0200776 goto unlock;
777
Oleg Nesterovf4d6dfe2012-07-29 20:22:44 +0200778 if (vma->vm_start > info->vaddr ||
779 vaddr_to_offset(vma, info->vaddr) != uprobe->offset)
Oleg Nesterov26872092012-06-15 17:43:33 +0200780 goto unlock;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530781
Oleg Nesterov806a98b2012-12-27 18:21:11 +0100782 if (is_register) {
783 /* consult only the "caller", new consumer. */
Oleg Nesterovbdf86472013-02-03 19:21:12 +0100784 if (consumer_filter(new,
Oleg Nesterov8a7f2fa2012-12-28 17:58:38 +0100785 UPROBE_FILTER_REGISTER, mm))
Oleg Nesterov806a98b2012-12-27 18:21:11 +0100786 err = install_breakpoint(uprobe, mm, vma, info->vaddr);
787 } else if (test_bit(MMF_HAS_UPROBES, &mm->flags)) {
Oleg Nesterov8a7f2fa2012-12-28 17:58:38 +0100788 if (!filter_chain(uprobe,
789 UPROBE_FILTER_UNREGISTER, mm))
Oleg Nesterov806a98b2012-12-27 18:21:11 +0100790 err |= remove_breakpoint(uprobe, mm, info->vaddr);
791 }
Oleg Nesterov78f74112012-08-08 17:35:08 +0200792
Oleg Nesterov26872092012-06-15 17:43:33 +0200793 unlock:
794 up_write(&mm->mmap_sem);
795 free:
796 mmput(mm);
797 info = free_map_info(info);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530798 }
Oleg Nesterov32cdba12012-11-14 19:03:42 +0100799 out:
800 percpu_up_write(&dup_mmap_sem);
Oleg Nesterov26872092012-06-15 17:43:33 +0200801 return err;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530802}
803
Oleg Nesterov9a98e032012-11-23 20:15:17 +0100804static int __uprobe_register(struct uprobe *uprobe, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530805{
Oleg Nesterov9a98e032012-11-23 20:15:17 +0100806 consumer_add(uprobe, uc);
Oleg Nesterovbdf86472013-02-03 19:21:12 +0100807 return register_for_each_vma(uprobe, uc);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530808}
809
Oleg Nesterov04aab9b2012-11-23 19:43:50 +0100810static void __uprobe_unregister(struct uprobe *uprobe, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530811{
Oleg Nesterov04aab9b2012-11-23 19:43:50 +0100812 int err;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530813
Oleg Nesterov04aab9b2012-11-23 19:43:50 +0100814 if (!consumer_del(uprobe, uc)) /* WARN? */
815 return;
816
Oleg Nesterovbdf86472013-02-03 19:21:12 +0100817 err = register_for_each_vma(uprobe, NULL);
Oleg Nesterovbb929282012-11-24 18:27:08 +0100818 /* TODO : cant unregister? schedule a worker thread */
819 if (!uprobe->consumers && !err)
820 delete_uprobe(uprobe);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530821}
822
823/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100824 * uprobe_register - register a probe
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530825 * @inode: the file in which the probe has to be placed.
826 * @offset: offset from the start of the file.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530827 * @uc: information on howto handle the probe..
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530828 *
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100829 * Apart from the access refcount, uprobe_register() takes a creation
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530830 * refcount (thro alloc_uprobe) if and only if this @uprobe is getting
831 * inserted into the rbtree (i.e first consumer for a @inode:@offset
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100832 * tuple). Creation refcount stops uprobe_unregister from freeing the
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530833 * @uprobe even before the register operation is complete. Creation
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530834 * refcount is released when the last @uc for the @uprobe
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530835 * unregisters.
836 *
837 * Return errno if it cannot successully install probes
838 * else return 0 (success)
839 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530840int uprobe_register(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530841{
842 struct uprobe *uprobe;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100843 int ret;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530844
Oleg Nesterovf0744af2012-11-21 18:01:43 +0100845 /* Racy, just to catch the obvious mistakes */
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530846 if (offset > i_size_read(inode))
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100847 return -EINVAL;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530848
Oleg Nesterov66d06df2012-11-25 22:48:37 +0100849 retry:
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530850 uprobe = alloc_uprobe(inode, offset);
Oleg Nesterov66d06df2012-11-25 22:48:37 +0100851 if (!uprobe)
852 return -ENOMEM;
853 /*
854 * We can race with uprobe_unregister()->delete_uprobe().
855 * Check uprobe_is_active() and retry if it is false.
856 */
857 down_write(&uprobe->register_rwsem);
858 ret = -EAGAIN;
859 if (likely(uprobe_is_active(uprobe))) {
Oleg Nesterov9a98e032012-11-23 20:15:17 +0100860 ret = __uprobe_register(uprobe, uc);
861 if (ret)
Oleg Nesterov04aab9b2012-11-23 19:43:50 +0100862 __uprobe_unregister(uprobe, uc);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530863 }
Oleg Nesterov66d06df2012-11-25 22:48:37 +0100864 up_write(&uprobe->register_rwsem);
865 put_uprobe(uprobe);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530866
Oleg Nesterov66d06df2012-11-25 22:48:37 +0100867 if (unlikely(ret == -EAGAIN))
868 goto retry;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530869 return ret;
870}
Josh Stonee8440c12013-01-13 19:03:34 +0100871EXPORT_SYMBOL_GPL(uprobe_register);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530872
873/*
Oleg Nesterovbdf86472013-02-03 19:21:12 +0100874 * uprobe_apply - unregister a already registered probe.
875 * @inode: the file in which the probe has to be removed.
876 * @offset: offset from the start of the file.
877 * @uc: consumer which wants to add more or remove some breakpoints
878 * @add: add or remove the breakpoints
879 */
880int uprobe_apply(struct inode *inode, loff_t offset,
881 struct uprobe_consumer *uc, bool add)
882{
883 struct uprobe *uprobe;
884 struct uprobe_consumer *con;
885 int ret = -ENOENT;
886
887 uprobe = find_uprobe(inode, offset);
888 if (!uprobe)
889 return ret;
890
891 down_write(&uprobe->register_rwsem);
892 for (con = uprobe->consumers; con && con != uc ; con = con->next)
893 ;
894 if (con)
895 ret = register_for_each_vma(uprobe, add ? uc : NULL);
896 up_write(&uprobe->register_rwsem);
897 put_uprobe(uprobe);
898
899 return ret;
900}
901
902/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100903 * uprobe_unregister - unregister a already registered probe.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530904 * @inode: the file in which the probe has to be removed.
905 * @offset: offset from the start of the file.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530906 * @uc: identify which probe if multiple probes are colocated.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530907 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530908void uprobe_unregister(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530909{
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100910 struct uprobe *uprobe;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530911
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530912 uprobe = find_uprobe(inode, offset);
913 if (!uprobe)
914 return;
915
Oleg Nesterove591c8d2012-11-24 17:29:40 +0100916 down_write(&uprobe->register_rwsem);
Oleg Nesterov04aab9b2012-11-23 19:43:50 +0100917 __uprobe_unregister(uprobe, uc);
Oleg Nesterove591c8d2012-11-24 17:29:40 +0100918 up_write(&uprobe->register_rwsem);
Sasha Levinc91368c2012-12-20 14:11:30 -0500919 put_uprobe(uprobe);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530920}
Josh Stonee8440c12013-01-13 19:03:34 +0100921EXPORT_SYMBOL_GPL(uprobe_unregister);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530922
Oleg Nesterovda1816b2012-12-29 17:49:11 +0100923static int unapply_uprobe(struct uprobe *uprobe, struct mm_struct *mm)
924{
925 struct vm_area_struct *vma;
926 int err = 0;
927
928 down_read(&mm->mmap_sem);
929 for (vma = mm->mmap; vma; vma = vma->vm_next) {
930 unsigned long vaddr;
931 loff_t offset;
932
933 if (!valid_vma(vma, false) ||
Oleg Nesterovf2817692013-03-17 18:54:44 +0100934 file_inode(vma->vm_file) != uprobe->inode)
Oleg Nesterovda1816b2012-12-29 17:49:11 +0100935 continue;
936
937 offset = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
938 if (uprobe->offset < offset ||
939 uprobe->offset >= offset + vma->vm_end - vma->vm_start)
940 continue;
941
942 vaddr = offset_to_vaddr(vma, uprobe->offset);
943 err |= remove_breakpoint(uprobe, mm, vaddr);
944 }
945 up_read(&mm->mmap_sem);
946
947 return err;
948}
949
Oleg Nesterov891c3972012-07-29 20:22:40 +0200950static struct rb_node *
951find_node_in_range(struct inode *inode, loff_t min, loff_t max)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530952{
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530953 struct rb_node *n = uprobes_tree.rb_node;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530954
955 while (n) {
Oleg Nesterov891c3972012-07-29 20:22:40 +0200956 struct uprobe *u = rb_entry(n, struct uprobe, rb_node);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100957
Oleg Nesterov891c3972012-07-29 20:22:40 +0200958 if (inode < u->inode) {
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530959 n = n->rb_left;
Oleg Nesterov891c3972012-07-29 20:22:40 +0200960 } else if (inode > u->inode) {
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530961 n = n->rb_right;
Oleg Nesterov891c3972012-07-29 20:22:40 +0200962 } else {
963 if (max < u->offset)
964 n = n->rb_left;
965 else if (min > u->offset)
966 n = n->rb_right;
967 else
968 break;
969 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530970 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100971
Oleg Nesterov891c3972012-07-29 20:22:40 +0200972 return n;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530973}
974
975/*
Oleg Nesterov891c3972012-07-29 20:22:40 +0200976 * For a given range in vma, build a list of probes that need to be inserted.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530977 */
Oleg Nesterov891c3972012-07-29 20:22:40 +0200978static void build_probe_list(struct inode *inode,
979 struct vm_area_struct *vma,
980 unsigned long start, unsigned long end,
981 struct list_head *head)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530982{
Oleg Nesterov891c3972012-07-29 20:22:40 +0200983 loff_t min, max;
Oleg Nesterov891c3972012-07-29 20:22:40 +0200984 struct rb_node *n, *t;
985 struct uprobe *u;
986
987 INIT_LIST_HEAD(head);
Oleg Nesterovcb113b42012-07-29 20:22:42 +0200988 min = vaddr_to_offset(vma, start);
Oleg Nesterov891c3972012-07-29 20:22:40 +0200989 max = min + (end - start) - 1;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530990
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200991 spin_lock(&uprobes_treelock);
Oleg Nesterov891c3972012-07-29 20:22:40 +0200992 n = find_node_in_range(inode, min, max);
993 if (n) {
994 for (t = n; t; t = rb_prev(t)) {
995 u = rb_entry(t, struct uprobe, rb_node);
996 if (u->inode != inode || u->offset < min)
997 break;
998 list_add(&u->pending_list, head);
999 atomic_inc(&u->ref);
1000 }
1001 for (t = n; (t = rb_next(t)); ) {
1002 u = rb_entry(t, struct uprobe, rb_node);
1003 if (u->inode != inode || u->offset > max)
1004 break;
1005 list_add(&u->pending_list, head);
1006 atomic_inc(&u->ref);
1007 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301008 }
Oleg Nesterov6f47caa2012-08-18 17:01:57 +02001009 spin_unlock(&uprobes_treelock);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301010}
1011
1012/*
Oleg Nesterov5e5be712012-08-06 14:49:56 +02001013 * Called from mmap_region/vma_adjust with mm->mmap_sem acquired.
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301014 *
Oleg Nesterov5e5be712012-08-06 14:49:56 +02001015 * Currently we ignore all errors and always return 0, the callers
1016 * can't handle the failure anyway.
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301017 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001018int uprobe_mmap(struct vm_area_struct *vma)
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301019{
1020 struct list_head tmp_list;
Oleg Nesterov665605a2012-07-29 20:22:29 +02001021 struct uprobe *uprobe, *u;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301022 struct inode *inode;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301023
Oleg Nesterov441f1eb2012-11-25 19:54:29 +01001024 if (no_uprobe_events() || !valid_vma(vma, true))
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001025 return 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301026
Oleg Nesterovf2817692013-03-17 18:54:44 +01001027 inode = file_inode(vma->vm_file);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301028 if (!inode)
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001029 return 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301030
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301031 mutex_lock(uprobes_mmap_hash(inode));
Oleg Nesterov891c3972012-07-29 20:22:40 +02001032 build_probe_list(inode, vma, vma->vm_start, vma->vm_end, &tmp_list);
Oleg Nesterov806a98b2012-12-27 18:21:11 +01001033 /*
1034 * We can race with uprobe_unregister(), this uprobe can be already
1035 * removed. But in this case filter_chain() must return false, all
1036 * consumers have gone away.
1037 */
Oleg Nesterov665605a2012-07-29 20:22:29 +02001038 list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
Oleg Nesterov806a98b2012-12-27 18:21:11 +01001039 if (!fatal_signal_pending(current) &&
Oleg Nesterov8a7f2fa2012-12-28 17:58:38 +01001040 filter_chain(uprobe, UPROBE_FILTER_MMAP, vma->vm_mm)) {
Oleg Nesterov57683f72012-07-29 20:22:47 +02001041 unsigned long vaddr = offset_to_vaddr(vma, uprobe->offset);
Oleg Nesterov5e5be712012-08-06 14:49:56 +02001042 install_breakpoint(uprobe, vma->vm_mm, vma, vaddr);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301043 }
1044 put_uprobe(uprobe);
1045 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301046 mutex_unlock(uprobes_mmap_hash(inode));
1047
Oleg Nesterov5e5be712012-08-06 14:49:56 +02001048 return 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301049}
1050
Oleg Nesterov9f68f672012-08-19 16:15:09 +02001051static bool
1052vma_has_uprobes(struct vm_area_struct *vma, unsigned long start, unsigned long end)
1053{
1054 loff_t min, max;
1055 struct inode *inode;
1056 struct rb_node *n;
1057
Oleg Nesterovf2817692013-03-17 18:54:44 +01001058 inode = file_inode(vma->vm_file);
Oleg Nesterov9f68f672012-08-19 16:15:09 +02001059
1060 min = vaddr_to_offset(vma, start);
1061 max = min + (end - start) - 1;
1062
1063 spin_lock(&uprobes_treelock);
1064 n = find_node_in_range(inode, min, max);
1065 spin_unlock(&uprobes_treelock);
1066
1067 return !!n;
1068}
1069
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301070/*
1071 * Called in context of a munmap of a vma.
1072 */
Srikar Dronamrajucbc91f72012-04-11 16:05:27 +05301073void uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end)
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301074{
Oleg Nesterov441f1eb2012-11-25 19:54:29 +01001075 if (no_uprobe_events() || !valid_vma(vma, false))
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301076 return;
1077
Oleg Nesterov2fd611a2012-07-29 20:22:31 +02001078 if (!atomic_read(&vma->vm_mm->mm_users)) /* called by mmput() ? */
1079 return;
1080
Oleg Nesterov9f68f672012-08-19 16:15:09 +02001081 if (!test_bit(MMF_HAS_UPROBES, &vma->vm_mm->flags) ||
1082 test_bit(MMF_RECALC_UPROBES, &vma->vm_mm->flags))
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +02001083 return;
1084
Oleg Nesterov9f68f672012-08-19 16:15:09 +02001085 if (vma_has_uprobes(vma, start, end))
1086 set_bit(MMF_RECALC_UPROBES, &vma->vm_mm->flags);
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301087}
1088
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301089/* Slot allocation for XOL */
1090static int xol_add_vma(struct xol_area *area)
1091{
Oleg Nesterovc8a82532012-12-30 17:40:39 +01001092 struct mm_struct *mm = current->mm;
1093 int ret = -EALREADY;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301094
1095 down_write(&mm->mmap_sem);
1096 if (mm->uprobes_state.xol_area)
1097 goto fail;
1098
1099 ret = -ENOMEM;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301100 /* Try to map as high as possible, this is only a hint. */
1101 area->vaddr = get_unmapped_area(NULL, TASK_SIZE - PAGE_SIZE, PAGE_SIZE, 0, 0);
1102 if (area->vaddr & ~PAGE_MASK) {
1103 ret = area->vaddr;
1104 goto fail;
1105 }
1106
1107 ret = install_special_mapping(mm, area->vaddr, PAGE_SIZE,
1108 VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO, &area->page);
1109 if (ret)
1110 goto fail;
1111
1112 smp_wmb(); /* pairs with get_xol_area() */
1113 mm->uprobes_state.xol_area = area;
1114 ret = 0;
Oleg Nesterovc8a82532012-12-30 17:40:39 +01001115 fail:
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301116 up_write(&mm->mmap_sem);
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301117
1118 return ret;
1119}
1120
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301121/*
Oleg Nesterov9b545df2012-12-31 16:39:49 +01001122 * get_xol_area - Allocate process's xol_area if necessary.
1123 * This area will be used for storing instructions for execution out of line.
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301124 *
1125 * Returns the allocated area or NULL.
1126 */
Oleg Nesterov9b545df2012-12-31 16:39:49 +01001127static struct xol_area *get_xol_area(void)
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301128{
Oleg Nesterov9b545df2012-12-31 16:39:49 +01001129 struct mm_struct *mm = current->mm;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301130 struct xol_area *area;
1131
Oleg Nesterov9b545df2012-12-31 16:39:49 +01001132 area = mm->uprobes_state.xol_area;
1133 if (area)
1134 goto ret;
1135
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301136 area = kzalloc(sizeof(*area), GFP_KERNEL);
1137 if (unlikely(!area))
Oleg Nesterovc8a82532012-12-30 17:40:39 +01001138 goto out;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301139
1140 area->bitmap = kzalloc(BITS_TO_LONGS(UINSNS_PER_PAGE) * sizeof(long), GFP_KERNEL);
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301141 if (!area->bitmap)
Oleg Nesterovc8a82532012-12-30 17:40:39 +01001142 goto free_area;
1143
1144 area->page = alloc_page(GFP_HIGHUSER);
1145 if (!area->page)
1146 goto free_bitmap;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301147
1148 init_waitqueue_head(&area->wq);
1149 if (!xol_add_vma(area))
1150 return area;
1151
Oleg Nesterovc8a82532012-12-30 17:40:39 +01001152 __free_page(area->page);
1153 free_bitmap:
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301154 kfree(area->bitmap);
Oleg Nesterovc8a82532012-12-30 17:40:39 +01001155 free_area:
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301156 kfree(area);
Oleg Nesterovc8a82532012-12-30 17:40:39 +01001157 out:
Oleg Nesterov9b545df2012-12-31 16:39:49 +01001158 area = mm->uprobes_state.xol_area;
1159 ret:
1160 smp_read_barrier_depends(); /* pairs with wmb in xol_add_vma() */
1161 return area;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301162}
1163
1164/*
1165 * uprobe_clear_state - Free the area allocated for slots.
1166 */
1167void uprobe_clear_state(struct mm_struct *mm)
1168{
1169 struct xol_area *area = mm->uprobes_state.xol_area;
1170
1171 if (!area)
1172 return;
1173
1174 put_page(area->page);
1175 kfree(area->bitmap);
1176 kfree(area);
1177}
1178
Oleg Nesterov32cdba12012-11-14 19:03:42 +01001179void uprobe_start_dup_mmap(void)
1180{
1181 percpu_down_read(&dup_mmap_sem);
1182}
1183
1184void uprobe_end_dup_mmap(void)
1185{
1186 percpu_up_read(&dup_mmap_sem);
1187}
1188
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +02001189void uprobe_dup_mmap(struct mm_struct *oldmm, struct mm_struct *newmm)
1190{
Oleg Nesterov61559a82012-08-08 17:17:46 +02001191 newmm->uprobes_state.xol_area = NULL;
1192
Oleg Nesterov9f68f672012-08-19 16:15:09 +02001193 if (test_bit(MMF_HAS_UPROBES, &oldmm->flags)) {
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +02001194 set_bit(MMF_HAS_UPROBES, &newmm->flags);
Oleg Nesterov9f68f672012-08-19 16:15:09 +02001195 /* unconditionally, dup_mmap() skips VM_DONTCOPY vmas */
1196 set_bit(MMF_RECALC_UPROBES, &newmm->flags);
1197 }
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +02001198}
1199
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301200/*
1201 * - search for a free slot.
1202 */
1203static unsigned long xol_take_insn_slot(struct xol_area *area)
1204{
1205 unsigned long slot_addr;
1206 int slot_nr;
1207
1208 do {
1209 slot_nr = find_first_zero_bit(area->bitmap, UINSNS_PER_PAGE);
1210 if (slot_nr < UINSNS_PER_PAGE) {
1211 if (!test_and_set_bit(slot_nr, area->bitmap))
1212 break;
1213
1214 slot_nr = UINSNS_PER_PAGE;
1215 continue;
1216 }
1217 wait_event(area->wq, (atomic_read(&area->slot_count) < UINSNS_PER_PAGE));
1218 } while (slot_nr >= UINSNS_PER_PAGE);
1219
1220 slot_addr = area->vaddr + (slot_nr * UPROBE_XOL_SLOT_BYTES);
1221 atomic_inc(&area->slot_count);
1222
1223 return slot_addr;
1224}
1225
1226/*
Oleg Nesterova6cb3f62012-12-31 18:00:06 +01001227 * xol_get_insn_slot - allocate a slot for xol.
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301228 * Returns the allocated slot address or 0.
1229 */
Oleg Nesterova6cb3f62012-12-31 18:00:06 +01001230static unsigned long xol_get_insn_slot(struct uprobe *uprobe)
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301231{
1232 struct xol_area *area;
1233 unsigned long offset;
Oleg Nesterova6cb3f62012-12-31 18:00:06 +01001234 unsigned long xol_vaddr;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301235 void *vaddr;
1236
Oleg Nesterov9b545df2012-12-31 16:39:49 +01001237 area = get_xol_area();
1238 if (!area)
1239 return 0;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301240
Oleg Nesterova6cb3f62012-12-31 18:00:06 +01001241 xol_vaddr = xol_take_insn_slot(area);
1242 if (unlikely(!xol_vaddr))
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301243 return 0;
1244
Oleg Nesterova6cb3f62012-12-31 18:00:06 +01001245 /* Initialize the slot */
1246 offset = xol_vaddr & ~PAGE_MASK;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301247 vaddr = kmap_atomic(area->page);
1248 memcpy(vaddr + offset, uprobe->arch.insn, MAX_UINSN_BYTES);
1249 kunmap_atomic(vaddr);
Rabin Vincent65b6ecc2012-11-14 18:27:07 +01001250 /*
1251 * We probably need flush_icache_user_range() but it needs vma.
1252 * This should work on supported architectures too.
1253 */
1254 flush_dcache_page(area->page);
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301255
Oleg Nesterova6cb3f62012-12-31 18:00:06 +01001256 return xol_vaddr;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301257}
1258
1259/*
1260 * xol_free_insn_slot - If slot was earlier allocated by
1261 * @xol_get_insn_slot(), make the slot available for
1262 * subsequent requests.
1263 */
1264static void xol_free_insn_slot(struct task_struct *tsk)
1265{
1266 struct xol_area *area;
1267 unsigned long vma_end;
1268 unsigned long slot_addr;
1269
1270 if (!tsk->mm || !tsk->mm->uprobes_state.xol_area || !tsk->utask)
1271 return;
1272
1273 slot_addr = tsk->utask->xol_vaddr;
Oleg Nesterovaf4355e2012-12-31 18:37:11 +01001274 if (unlikely(!slot_addr))
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301275 return;
1276
1277 area = tsk->mm->uprobes_state.xol_area;
1278 vma_end = area->vaddr + PAGE_SIZE;
1279 if (area->vaddr <= slot_addr && slot_addr < vma_end) {
1280 unsigned long offset;
1281 int slot_nr;
1282
1283 offset = slot_addr - area->vaddr;
1284 slot_nr = offset / UPROBE_XOL_SLOT_BYTES;
1285 if (slot_nr >= UINSNS_PER_PAGE)
1286 return;
1287
1288 clear_bit(slot_nr, area->bitmap);
1289 atomic_dec(&area->slot_count);
1290 if (waitqueue_active(&area->wq))
1291 wake_up(&area->wq);
1292
1293 tsk->utask->xol_vaddr = 0;
1294 }
1295}
1296
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301297/**
1298 * uprobe_get_swbp_addr - compute address of swbp given post-swbp regs
1299 * @regs: Reflects the saved state of the task after it has hit a breakpoint
1300 * instruction.
1301 * Return the address of the breakpoint instruction.
1302 */
1303unsigned long __weak uprobe_get_swbp_addr(struct pt_regs *regs)
1304{
1305 return instruction_pointer(regs) - UPROBE_SWBP_INSN_SIZE;
1306}
1307
1308/*
1309 * Called with no locks held.
1310 * Called in context of a exiting or a exec-ing thread.
1311 */
1312void uprobe_free_utask(struct task_struct *t)
1313{
1314 struct uprobe_task *utask = t->utask;
1315
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301316 if (!utask)
1317 return;
1318
1319 if (utask->active_uprobe)
1320 put_uprobe(utask->active_uprobe);
1321
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301322 xol_free_insn_slot(t);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301323 kfree(utask);
1324 t->utask = NULL;
1325}
1326
1327/*
1328 * Called in context of a new clone/fork from copy_process.
1329 */
1330void uprobe_copy_process(struct task_struct *t)
1331{
1332 t->utask = NULL;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301333}
1334
1335/*
Oleg Nesterov5a2df662012-12-31 17:03:32 +01001336 * Allocate a uprobe_task object for the task if if necessary.
1337 * Called when the thread hits a breakpoint.
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301338 *
1339 * Returns:
1340 * - pointer to new uprobe_task on success
1341 * - NULL otherwise
1342 */
Oleg Nesterov5a2df662012-12-31 17:03:32 +01001343static struct uprobe_task *get_utask(void)
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301344{
Oleg Nesterov5a2df662012-12-31 17:03:32 +01001345 if (!current->utask)
1346 current->utask = kzalloc(sizeof(struct uprobe_task), GFP_KERNEL);
1347 return current->utask;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301348}
1349
1350/* Prepare to single-step probed instruction out of line. */
1351static int
Oleg Nesterova6cb3f62012-12-31 18:00:06 +01001352pre_ssout(struct uprobe *uprobe, struct pt_regs *regs, unsigned long bp_vaddr)
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301353{
Oleg Nesterova6cb3f62012-12-31 18:00:06 +01001354 struct uprobe_task *utask;
1355 unsigned long xol_vaddr;
Oleg Nesterovaba51022012-12-31 18:12:48 +01001356 int err;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301357
Oleg Nesterov608e7422012-12-31 18:20:42 +01001358 utask = get_utask();
1359 if (!utask)
1360 return -ENOMEM;
Oleg Nesterova6cb3f62012-12-31 18:00:06 +01001361
1362 xol_vaddr = xol_get_insn_slot(uprobe);
1363 if (!xol_vaddr)
1364 return -ENOMEM;
1365
1366 utask->xol_vaddr = xol_vaddr;
1367 utask->vaddr = bp_vaddr;
1368
Oleg Nesterovaba51022012-12-31 18:12:48 +01001369 err = arch_uprobe_pre_xol(&uprobe->arch, regs);
1370 if (unlikely(err)) {
1371 xol_free_insn_slot(current);
1372 return err;
1373 }
1374
Oleg Nesterov608e7422012-12-31 18:20:42 +01001375 utask->active_uprobe = uprobe;
1376 utask->state = UTASK_SSTEP;
Oleg Nesterovaba51022012-12-31 18:12:48 +01001377 return 0;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301378}
1379
1380/*
1381 * If we are singlestepping, then ensure this thread is not connected to
1382 * non-fatal signals until completion of singlestep. When xol insn itself
1383 * triggers the signal, restart the original insn even if the task is
1384 * already SIGKILL'ed (since coredump should report the correct ip). This
1385 * is even more important if the task has a handler for SIGSEGV/etc, The
1386 * _same_ instruction should be repeated again after return from the signal
1387 * handler, and SSTEP can never finish in this case.
1388 */
1389bool uprobe_deny_signal(void)
1390{
1391 struct task_struct *t = current;
1392 struct uprobe_task *utask = t->utask;
1393
1394 if (likely(!utask || !utask->active_uprobe))
1395 return false;
1396
1397 WARN_ON_ONCE(utask->state != UTASK_SSTEP);
1398
1399 if (signal_pending(t)) {
1400 spin_lock_irq(&t->sighand->siglock);
1401 clear_tsk_thread_flag(t, TIF_SIGPENDING);
1402 spin_unlock_irq(&t->sighand->siglock);
1403
1404 if (__fatal_signal_pending(t) || arch_uprobe_xol_was_trapped(t)) {
1405 utask->state = UTASK_SSTEP_TRAPPED;
1406 set_tsk_thread_flag(t, TIF_UPROBE);
1407 set_tsk_thread_flag(t, TIF_NOTIFY_RESUME);
1408 }
1409 }
1410
1411 return true;
1412}
1413
1414/*
1415 * Avoid singlestepping the original instruction if the original instruction
1416 * is a NOP or can be emulated.
1417 */
1418static bool can_skip_sstep(struct uprobe *uprobe, struct pt_regs *regs)
1419{
Oleg Nesterov71434f22012-09-30 21:12:44 +02001420 if (test_bit(UPROBE_SKIP_SSTEP, &uprobe->flags)) {
Oleg Nesterov0578a972012-09-14 18:31:23 +02001421 if (arch_uprobe_skip_sstep(&uprobe->arch, regs))
1422 return true;
Oleg Nesterov71434f22012-09-30 21:12:44 +02001423 clear_bit(UPROBE_SKIP_SSTEP, &uprobe->flags);
Oleg Nesterov0578a972012-09-14 18:31:23 +02001424 }
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301425 return false;
1426}
1427
Oleg Nesterov499a4f32012-08-19 17:41:34 +02001428static void mmf_recalc_uprobes(struct mm_struct *mm)
1429{
1430 struct vm_area_struct *vma;
1431
1432 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1433 if (!valid_vma(vma, false))
1434 continue;
1435 /*
1436 * This is not strictly accurate, we can race with
1437 * uprobe_unregister() and see the already removed
1438 * uprobe if delete_uprobe() was not yet called.
Oleg Nesterov63633cb2012-11-22 18:30:15 +01001439 * Or this uprobe can be filtered out.
Oleg Nesterov499a4f32012-08-19 17:41:34 +02001440 */
1441 if (vma_has_uprobes(vma, vma->vm_start, vma->vm_end))
1442 return;
1443 }
1444
1445 clear_bit(MMF_HAS_UPROBES, &mm->flags);
1446}
1447
Ananth N Mavinakayanahalli0908ad62013-03-22 20:46:27 +05301448static int is_trap_at_addr(struct mm_struct *mm, unsigned long vaddr)
Oleg Nesterovec75fba2012-09-23 21:55:19 +02001449{
1450 struct page *page;
1451 uprobe_opcode_t opcode;
1452 int result;
1453
1454 pagefault_disable();
1455 result = __copy_from_user_inatomic(&opcode, (void __user*)vaddr,
1456 sizeof(opcode));
1457 pagefault_enable();
1458
1459 if (likely(result == 0))
1460 goto out;
1461
1462 result = get_user_pages(NULL, mm, vaddr, 1, 0, 1, &page, NULL);
1463 if (result < 0)
1464 return result;
1465
Oleg Nesterovab0d8052013-03-24 18:24:37 +01001466 copy_from_page(page, vaddr, &opcode, UPROBE_SWBP_INSN_SIZE);
Oleg Nesterovec75fba2012-09-23 21:55:19 +02001467 put_page(page);
1468 out:
Ananth N Mavinakayanahalli0908ad62013-03-22 20:46:27 +05301469 /* This needs to return true for any variant of the trap insn */
1470 return is_trap_insn(&opcode);
Oleg Nesterovec75fba2012-09-23 21:55:19 +02001471}
1472
Oleg Nesterovd790d342012-05-29 21:29:14 +02001473static struct uprobe *find_active_uprobe(unsigned long bp_vaddr, int *is_swbp)
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001474{
1475 struct mm_struct *mm = current->mm;
1476 struct uprobe *uprobe = NULL;
1477 struct vm_area_struct *vma;
1478
1479 down_read(&mm->mmap_sem);
1480 vma = find_vma(mm, bp_vaddr);
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001481 if (vma && vma->vm_start <= bp_vaddr) {
1482 if (valid_vma(vma, false)) {
Oleg Nesterovf2817692013-03-17 18:54:44 +01001483 struct inode *inode = file_inode(vma->vm_file);
Oleg Nesterovcb113b42012-07-29 20:22:42 +02001484 loff_t offset = vaddr_to_offset(vma, bp_vaddr);
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001485
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001486 uprobe = find_uprobe(inode, offset);
1487 }
Oleg Nesterovd790d342012-05-29 21:29:14 +02001488
1489 if (!uprobe)
Ananth N Mavinakayanahalli0908ad62013-03-22 20:46:27 +05301490 *is_swbp = is_trap_at_addr(mm, bp_vaddr);
Oleg Nesterovd790d342012-05-29 21:29:14 +02001491 } else {
1492 *is_swbp = -EFAULT;
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001493 }
Oleg Nesterov499a4f32012-08-19 17:41:34 +02001494
1495 if (!uprobe && test_and_clear_bit(MMF_RECALC_UPROBES, &mm->flags))
1496 mmf_recalc_uprobes(mm);
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001497 up_read(&mm->mmap_sem);
1498
1499 return uprobe;
1500}
1501
Oleg Nesterovda1816b2012-12-29 17:49:11 +01001502static void handler_chain(struct uprobe *uprobe, struct pt_regs *regs)
1503{
1504 struct uprobe_consumer *uc;
1505 int remove = UPROBE_HANDLER_REMOVE;
1506
1507 down_read(&uprobe->register_rwsem);
1508 for (uc = uprobe->consumers; uc; uc = uc->next) {
1509 int rc = uc->handler(uc, regs);
1510
1511 WARN(rc & ~UPROBE_HANDLER_MASK,
1512 "bad rc=0x%x from %pf()\n", rc, uc->handler);
1513 remove &= rc;
1514 }
1515
1516 if (remove && uprobe->consumers) {
1517 WARN_ON(!uprobe_is_active(uprobe));
1518 unapply_uprobe(uprobe, current->mm);
1519 }
1520 up_read(&uprobe->register_rwsem);
1521}
1522
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301523/*
1524 * Run handler and ask thread to singlestep.
1525 * Ensure all non-fatal signals cannot interrupt thread while it singlesteps.
1526 */
1527static void handle_swbp(struct pt_regs *regs)
1528{
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301529 struct uprobe *uprobe;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301530 unsigned long bp_vaddr;
Oleg Nesterov56bb4cf2012-05-29 21:29:47 +02001531 int uninitialized_var(is_swbp);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301532
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301533 bp_vaddr = uprobe_get_swbp_addr(regs);
Oleg Nesterovd790d342012-05-29 21:29:14 +02001534 uprobe = find_active_uprobe(bp_vaddr, &is_swbp);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301535
1536 if (!uprobe) {
Oleg Nesterov56bb4cf2012-05-29 21:29:47 +02001537 if (is_swbp > 0) {
1538 /* No matching uprobe; signal SIGTRAP. */
1539 send_sig(SIGTRAP, current, 0);
1540 } else {
1541 /*
1542 * Either we raced with uprobe_unregister() or we can't
1543 * access this memory. The latter is only possible if
1544 * another thread plays with our ->mm. In both cases
1545 * we can simply restart. If this vma was unmapped we
1546 * can pretend this insn was not executed yet and get
1547 * the (correct) SIGSEGV after restart.
1548 */
1549 instruction_pointer_set(regs, bp_vaddr);
1550 }
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301551 return;
1552 }
Oleg Nesterov74e59df2012-12-30 15:54:08 +01001553
1554 /* change it in advance for ->handler() and restart */
1555 instruction_pointer_set(regs, bp_vaddr);
1556
Oleg Nesterov142b18d2012-09-29 21:56:57 +02001557 /*
1558 * TODO: move copy_insn/etc into _register and remove this hack.
1559 * After we hit the bp, _unregister + _register can install the
1560 * new and not-yet-analyzed uprobe at the same address, restart.
1561 */
1562 smp_rmb(); /* pairs with wmb() in install_breakpoint() */
Oleg Nesterov71434f22012-09-30 21:12:44 +02001563 if (unlikely(!test_bit(UPROBE_COPY_INSN, &uprobe->flags)))
Oleg Nesterov74e59df2012-12-30 15:54:08 +01001564 goto out;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301565
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301566 handler_chain(uprobe, regs);
Oleg Nesterov0578a972012-09-14 18:31:23 +02001567 if (can_skip_sstep(uprobe, regs))
1568 goto out;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301569
Oleg Nesterov608e7422012-12-31 18:20:42 +01001570 if (!pre_ssout(uprobe, regs, bp_vaddr))
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301571 return;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301572
Oleg Nesterov74e59df2012-12-30 15:54:08 +01001573 /* can_skip_sstep() succeeded, or restart if can't singlestep */
Oleg Nesterov0578a972012-09-14 18:31:23 +02001574out:
Sebastian Andrzej Siewior8bd87442012-08-07 18:12:30 +02001575 put_uprobe(uprobe);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301576}
1577
1578/*
1579 * Perform required fix-ups and disable singlestep.
1580 * Allow pending signals to take effect.
1581 */
1582static void handle_singlestep(struct uprobe_task *utask, struct pt_regs *regs)
1583{
1584 struct uprobe *uprobe;
1585
1586 uprobe = utask->active_uprobe;
1587 if (utask->state == UTASK_SSTEP_ACK)
1588 arch_uprobe_post_xol(&uprobe->arch, regs);
1589 else if (utask->state == UTASK_SSTEP_TRAPPED)
1590 arch_uprobe_abort_xol(&uprobe->arch, regs);
1591 else
1592 WARN_ON_ONCE(1);
1593
1594 put_uprobe(uprobe);
1595 utask->active_uprobe = NULL;
1596 utask->state = UTASK_RUNNING;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301597 xol_free_insn_slot(current);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301598
1599 spin_lock_irq(&current->sighand->siglock);
1600 recalc_sigpending(); /* see uprobe_deny_signal() */
1601 spin_unlock_irq(&current->sighand->siglock);
1602}
1603
1604/*
Oleg Nesterov1b08e9072012-09-14 18:52:10 +02001605 * On breakpoint hit, breakpoint notifier sets the TIF_UPROBE flag and
1606 * allows the thread to return from interrupt. After that handle_swbp()
1607 * sets utask->active_uprobe.
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301608 *
Oleg Nesterov1b08e9072012-09-14 18:52:10 +02001609 * On singlestep exception, singlestep notifier sets the TIF_UPROBE flag
1610 * and allows the thread to return from interrupt.
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301611 *
1612 * While returning to userspace, thread notices the TIF_UPROBE flag and calls
1613 * uprobe_notify_resume().
1614 */
1615void uprobe_notify_resume(struct pt_regs *regs)
1616{
1617 struct uprobe_task *utask;
1618
Oleg Nesterovdb023ea2012-09-14 19:05:46 +02001619 clear_thread_flag(TIF_UPROBE);
1620
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301621 utask = current->utask;
Oleg Nesterov1b08e9072012-09-14 18:52:10 +02001622 if (utask && utask->active_uprobe)
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301623 handle_singlestep(utask, regs);
Oleg Nesterov1b08e9072012-09-14 18:52:10 +02001624 else
1625 handle_swbp(regs);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301626}
1627
1628/*
1629 * uprobe_pre_sstep_notifier gets called from interrupt context as part of
1630 * notifier mechanism. Set TIF_UPROBE flag and indicate breakpoint hit.
1631 */
1632int uprobe_pre_sstep_notifier(struct pt_regs *regs)
1633{
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +02001634 if (!current->mm || !test_bit(MMF_HAS_UPROBES, &current->mm->flags))
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301635 return 0;
1636
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301637 set_thread_flag(TIF_UPROBE);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301638 return 1;
1639}
1640
1641/*
1642 * uprobe_post_sstep_notifier gets called in interrupt context as part of notifier
1643 * mechanism. Set TIF_UPROBE flag and indicate completion of singlestep.
1644 */
1645int uprobe_post_sstep_notifier(struct pt_regs *regs)
1646{
1647 struct uprobe_task *utask = current->utask;
1648
1649 if (!current->mm || !utask || !utask->active_uprobe)
1650 /* task is currently not uprobed */
1651 return 0;
1652
1653 utask->state = UTASK_SSTEP_ACK;
1654 set_thread_flag(TIF_UPROBE);
1655 return 1;
1656}
1657
1658static struct notifier_block uprobe_exception_nb = {
1659 .notifier_call = arch_uprobe_exception_notify,
1660 .priority = INT_MAX-1, /* notified after kprobes, kgdb */
1661};
1662
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301663static int __init init_uprobes(void)
1664{
1665 int i;
1666
Oleg Nesterov66d06df2012-11-25 22:48:37 +01001667 for (i = 0; i < UPROBES_HASH_SZ; i++)
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301668 mutex_init(&uprobes_mmap_mutex[i]);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301669
Oleg Nesterov32cdba12012-11-14 19:03:42 +01001670 if (percpu_init_rwsem(&dup_mmap_sem))
1671 return -ENOMEM;
1672
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301673 return register_die_notifier(&uprobe_exception_nb);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301674}
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301675module_init(init_uprobes);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301676
1677static void __exit exit_uprobes(void)
1678{
1679}
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301680module_exit(exit_uprobes);