blob: 3798947b3b58cdc4d97ac4398f71d2175865d284 [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
Anton Arapov0dfd0eb2013-04-03 18:00:35 +020078struct return_instance {
79 struct uprobe *uprobe;
80 unsigned long func;
81 unsigned long orig_ret_vaddr; /* original return address */
82 bool chained; /* true, if instance is nested */
83
84 struct return_instance *next; /* keep as stack */
85};
86
Srikar Dronamraju2b144492012-02-09 14:56:42 +053087/*
88 * valid_vma: Verify if the specified vma is an executable vma
89 * Relax restrictions while unregistering: vm_flags might have
90 * changed after breakpoint was inserted.
91 * - is_register: indicates if we are in register context.
92 * - Return 1 if the specified virtual address is in an
93 * executable vma.
94 */
95static bool valid_vma(struct vm_area_struct *vma, bool is_register)
96{
Oleg Nesterove40cfce2012-09-16 19:31:39 +020097 vm_flags_t flags = VM_HUGETLB | VM_MAYEXEC | VM_SHARED;
Srikar Dronamraju2b144492012-02-09 14:56:42 +053098
Oleg Nesterove40cfce2012-09-16 19:31:39 +020099 if (is_register)
100 flags |= VM_WRITE;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530101
Oleg Nesterove40cfce2012-09-16 19:31:39 +0200102 return vma->vm_file && (vma->vm_flags & flags) == VM_MAYEXEC;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530103}
104
Oleg Nesterov57683f72012-07-29 20:22:47 +0200105static unsigned long offset_to_vaddr(struct vm_area_struct *vma, loff_t offset)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530106{
Oleg Nesterov57683f72012-07-29 20:22:47 +0200107 return vma->vm_start + offset - ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530108}
109
Oleg Nesterovcb113b42012-07-29 20:22:42 +0200110static loff_t vaddr_to_offset(struct vm_area_struct *vma, unsigned long vaddr)
111{
112 return ((loff_t)vma->vm_pgoff << PAGE_SHIFT) + (vaddr - vma->vm_start);
113}
114
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530115/**
116 * __replace_page - replace page in vma by new page.
117 * based on replace_page in mm/ksm.c
118 *
119 * @vma: vma that holds the pte pointing to page
Oleg Nesterovc517ee72012-07-29 20:22:16 +0200120 * @addr: address the old @page is mapped at
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530121 * @page: the cowed page we are replacing by kpage
122 * @kpage: the modified page we replace page by
123 *
124 * Returns 0 on success, -EFAULT on failure.
125 */
Oleg Nesterovc517ee72012-07-29 20:22:16 +0200126static int __replace_page(struct vm_area_struct *vma, unsigned long addr,
127 struct page *page, struct page *kpage)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530128{
129 struct mm_struct *mm = vma->vm_mm;
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200130 spinlock_t *ptl;
131 pte_t *ptep;
Oleg Nesterov9f924482012-07-29 20:22:20 +0200132 int err;
Haggai Eran6bdb9132012-10-08 16:33:35 -0700133 /* For mmu_notifiers */
134 const unsigned long mmun_start = addr;
135 const unsigned long mmun_end = addr + PAGE_SIZE;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530136
Oleg Nesterov194f8dc2012-07-29 20:22:49 +0200137 /* For try_to_free_swap() and munlock_vma_page() below */
Oleg Nesterov9f924482012-07-29 20:22:20 +0200138 lock_page(page);
139
Haggai Eran6bdb9132012-10-08 16:33:35 -0700140 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Oleg Nesterov9f924482012-07-29 20:22:20 +0200141 err = -EAGAIN;
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200142 ptep = page_check_address(page, mm, addr, &ptl, 0);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530143 if (!ptep)
Oleg Nesterov9f924482012-07-29 20:22:20 +0200144 goto unlock;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530145
146 get_page(kpage);
147 page_add_new_anon_rmap(kpage, vma, addr);
148
Srikar Dronamraju7396fa82012-04-11 16:05:16 +0530149 if (!PageAnon(page)) {
150 dec_mm_counter(mm, MM_FILEPAGES);
151 inc_mm_counter(mm, MM_ANONPAGES);
152 }
153
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530154 flush_cache_page(vma, addr, pte_pfn(*ptep));
155 ptep_clear_flush(vma, addr, ptep);
156 set_pte_at_notify(mm, addr, ptep, mk_pte(kpage, vma->vm_page_prot));
157
158 page_remove_rmap(page);
159 if (!page_mapped(page))
160 try_to_free_swap(page);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530161 pte_unmap_unlock(ptep, ptl);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530162
Oleg Nesterov194f8dc2012-07-29 20:22:49 +0200163 if (vma->vm_flags & VM_LOCKED)
164 munlock_vma_page(page);
165 put_page(page);
166
Oleg Nesterov9f924482012-07-29 20:22:20 +0200167 err = 0;
168 unlock:
Haggai Eran6bdb9132012-10-08 16:33:35 -0700169 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Oleg Nesterov9f924482012-07-29 20:22:20 +0200170 unlock_page(page);
171 return err;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530172}
173
174/**
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530175 * is_swbp_insn - check if instruction is breakpoint instruction.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530176 * @insn: instruction to be checked.
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530177 * Default implementation of is_swbp_insn
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530178 * Returns true if @insn is a breakpoint instruction.
179 */
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530180bool __weak is_swbp_insn(uprobe_opcode_t *insn)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530181{
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530182 return *insn == UPROBE_SWBP_INSN;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530183}
184
Ananth N Mavinakayanahalli0908ad62013-03-22 20:46:27 +0530185/**
186 * is_trap_insn - check if instruction is breakpoint instruction.
187 * @insn: instruction to be checked.
188 * Default implementation of is_trap_insn
189 * Returns true if @insn is a breakpoint instruction.
190 *
191 * This function is needed for the case where an architecture has multiple
192 * trap instructions (like powerpc).
193 */
194bool __weak is_trap_insn(uprobe_opcode_t *insn)
195{
196 return is_swbp_insn(insn);
197}
198
Oleg Nesterovab0d8052013-03-24 18:24:37 +0100199static void copy_from_page(struct page *page, unsigned long vaddr, void *dst, int len)
Oleg Nesterovcceb55a2012-09-23 21:10:18 +0200200{
201 void *kaddr = kmap_atomic(page);
Oleg Nesterovab0d8052013-03-24 18:24:37 +0100202 memcpy(dst, kaddr + (vaddr & ~PAGE_MASK), len);
Oleg Nesterovcceb55a2012-09-23 21:10:18 +0200203 kunmap_atomic(kaddr);
204}
205
Oleg Nesterov5669cce2013-03-24 18:58:04 +0100206static void copy_to_page(struct page *page, unsigned long vaddr, const void *src, int len)
207{
208 void *kaddr = kmap_atomic(page);
209 memcpy(kaddr + (vaddr & ~PAGE_MASK), src, len);
210 kunmap_atomic(kaddr);
211}
212
Oleg Nesteroved6f6a52012-09-23 21:30:44 +0200213static int verify_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t *new_opcode)
214{
215 uprobe_opcode_t old_opcode;
216 bool is_swbp;
217
Ananth N Mavinakayanahalli0908ad62013-03-22 20:46:27 +0530218 /*
219 * Note: We only check if the old_opcode is UPROBE_SWBP_INSN here.
220 * We do not check if it is any other 'trap variant' which could
221 * be conditional trap instruction such as the one powerpc supports.
222 *
223 * The logic is that we do not care if the underlying instruction
224 * is a trap variant; uprobes always wins over any other (gdb)
225 * breakpoint.
226 */
Oleg Nesterovab0d8052013-03-24 18:24:37 +0100227 copy_from_page(page, vaddr, &old_opcode, UPROBE_SWBP_INSN_SIZE);
Oleg Nesteroved6f6a52012-09-23 21:30:44 +0200228 is_swbp = is_swbp_insn(&old_opcode);
229
230 if (is_swbp_insn(new_opcode)) {
231 if (is_swbp) /* register: already installed? */
232 return 0;
233 } else {
234 if (!is_swbp) /* unregister: was it changed by us? */
Oleg Nesterov076a3652012-09-30 18:54:53 +0200235 return 0;
Oleg Nesteroved6f6a52012-09-23 21:30:44 +0200236 }
237
238 return 1;
239}
240
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530241/*
242 * NOTE:
243 * Expect the breakpoint instruction to be the smallest size instruction for
244 * the architecture. If an arch has variable length instruction and the
245 * breakpoint instruction is not of the smallest length instruction
Ananth N Mavinakayanahalli0908ad62013-03-22 20:46:27 +0530246 * supported by that architecture then we need to modify is_trap_at_addr and
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530247 * write_opcode accordingly. This would never be a problem for archs that
248 * have fixed length instructions.
249 */
250
251/*
252 * write_opcode - write the opcode at a given virtual address.
253 * @mm: the probed process address space.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530254 * @vaddr: the virtual address to store the opcode.
255 * @opcode: opcode to be written at @vaddr.
256 *
257 * Called with mm->mmap_sem held (for read and with a reference to
258 * mm).
259 *
260 * For mm @mm, write the opcode at @vaddr.
261 * Return 0 (success) or a negative errno.
262 */
Oleg Nesterovcceb55a2012-09-23 21:10:18 +0200263static int write_opcode(struct mm_struct *mm, unsigned long vaddr,
264 uprobe_opcode_t opcode)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530265{
266 struct page *old_page, *new_page;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530267 struct vm_area_struct *vma;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530268 int ret;
Oleg Nesterovf4030722012-07-29 20:22:12 +0200269
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200270retry:
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530271 /* Read the page with vaddr into memory */
Oleg Nesterov75ed82e2012-09-16 17:20:06 +0200272 ret = get_user_pages(NULL, mm, vaddr, 1, 0, 1, &old_page, &vma);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530273 if (ret <= 0)
274 return ret;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100275
Oleg Nesteroved6f6a52012-09-23 21:30:44 +0200276 ret = verify_opcode(old_page, vaddr, &opcode);
277 if (ret <= 0)
278 goto put_old;
279
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530280 ret = -ENOMEM;
281 new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vaddr);
282 if (!new_page)
Oleg Nesterov9f924482012-07-29 20:22:20 +0200283 goto put_old;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530284
285 __SetPageUptodate(new_page);
286
Oleg Nesterov3f471072013-03-24 19:04:36 +0100287 copy_highpage(new_page, old_page);
288 copy_to_page(new_page, vaddr, &opcode, UPROBE_SWBP_INSN_SIZE);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530289
290 ret = anon_vma_prepare(vma);
291 if (ret)
Oleg Nesterov9f924482012-07-29 20:22:20 +0200292 goto put_new;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530293
Oleg Nesterovc517ee72012-07-29 20:22:16 +0200294 ret = __replace_page(vma, vaddr, old_page, new_page);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530295
Oleg Nesterov9f924482012-07-29 20:22:20 +0200296put_new:
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530297 page_cache_release(new_page);
Oleg Nesterov9f924482012-07-29 20:22:20 +0200298put_old:
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100299 put_page(old_page);
300
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200301 if (unlikely(ret == -EAGAIN))
302 goto retry;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530303 return ret;
304}
305
306/**
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530307 * set_swbp - store breakpoint at a given address.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530308 * @auprobe: arch specific probepoint information.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530309 * @mm: the probed process address space.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530310 * @vaddr: the virtual address to insert the opcode.
311 *
312 * For mm @mm, store the breakpoint instruction at @vaddr.
313 * Return 0 (success) or a negative errno.
314 */
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530315int __weak set_swbp(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530316{
Oleg Nesterovcceb55a2012-09-23 21:10:18 +0200317 return write_opcode(mm, vaddr, UPROBE_SWBP_INSN);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530318}
319
320/**
321 * set_orig_insn - Restore the original instruction.
322 * @mm: the probed process address space.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530323 * @auprobe: arch specific probepoint information.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530324 * @vaddr: the virtual address to insert the opcode.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530325 *
326 * For mm @mm, restore the original opcode (opcode) at @vaddr.
327 * Return 0 (success) or a negative errno.
328 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100329int __weak
Oleg Nesterovded86e72012-08-08 18:07:03 +0200330set_orig_insn(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530331{
Oleg Nesterovcceb55a2012-09-23 21:10:18 +0200332 return write_opcode(mm, vaddr, *(uprobe_opcode_t *)auprobe->insn);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530333}
334
335static int match_uprobe(struct uprobe *l, struct uprobe *r)
336{
337 if (l->inode < r->inode)
338 return -1;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100339
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530340 if (l->inode > r->inode)
341 return 1;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530342
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100343 if (l->offset < r->offset)
344 return -1;
345
346 if (l->offset > r->offset)
347 return 1;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530348
349 return 0;
350}
351
352static struct uprobe *__find_uprobe(struct inode *inode, loff_t offset)
353{
354 struct uprobe u = { .inode = inode, .offset = offset };
355 struct rb_node *n = uprobes_tree.rb_node;
356 struct uprobe *uprobe;
357 int match;
358
359 while (n) {
360 uprobe = rb_entry(n, struct uprobe, rb_node);
361 match = match_uprobe(&u, uprobe);
362 if (!match) {
363 atomic_inc(&uprobe->ref);
364 return uprobe;
365 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100366
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530367 if (match < 0)
368 n = n->rb_left;
369 else
370 n = n->rb_right;
371 }
372 return NULL;
373}
374
375/*
376 * Find a uprobe corresponding to a given inode:offset
377 * Acquires uprobes_treelock
378 */
379static struct uprobe *find_uprobe(struct inode *inode, loff_t offset)
380{
381 struct uprobe *uprobe;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530382
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200383 spin_lock(&uprobes_treelock);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530384 uprobe = __find_uprobe(inode, offset);
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200385 spin_unlock(&uprobes_treelock);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100386
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530387 return uprobe;
388}
389
390static struct uprobe *__insert_uprobe(struct uprobe *uprobe)
391{
392 struct rb_node **p = &uprobes_tree.rb_node;
393 struct rb_node *parent = NULL;
394 struct uprobe *u;
395 int match;
396
397 while (*p) {
398 parent = *p;
399 u = rb_entry(parent, struct uprobe, rb_node);
400 match = match_uprobe(uprobe, u);
401 if (!match) {
402 atomic_inc(&u->ref);
403 return u;
404 }
405
406 if (match < 0)
407 p = &parent->rb_left;
408 else
409 p = &parent->rb_right;
410
411 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100412
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530413 u = NULL;
414 rb_link_node(&uprobe->rb_node, parent, p);
415 rb_insert_color(&uprobe->rb_node, &uprobes_tree);
416 /* get access + creation ref */
417 atomic_set(&uprobe->ref, 2);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100418
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530419 return u;
420}
421
422/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100423 * Acquire uprobes_treelock.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530424 * Matching uprobe already exists in rbtree;
425 * increment (access refcount) and return the matching uprobe.
426 *
427 * No matching uprobe; insert the uprobe in rb_tree;
428 * get a double refcount (access + creation) and return NULL.
429 */
430static struct uprobe *insert_uprobe(struct uprobe *uprobe)
431{
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530432 struct uprobe *u;
433
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200434 spin_lock(&uprobes_treelock);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530435 u = __insert_uprobe(uprobe);
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200436 spin_unlock(&uprobes_treelock);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100437
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530438 return u;
439}
440
441static void put_uprobe(struct uprobe *uprobe)
442{
443 if (atomic_dec_and_test(&uprobe->ref))
444 kfree(uprobe);
445}
446
447static struct uprobe *alloc_uprobe(struct inode *inode, loff_t offset)
448{
449 struct uprobe *uprobe, *cur_uprobe;
450
451 uprobe = kzalloc(sizeof(struct uprobe), GFP_KERNEL);
452 if (!uprobe)
453 return NULL;
454
455 uprobe->inode = igrab(inode);
456 uprobe->offset = offset;
Oleg Nesterove591c8d2012-11-24 17:29:40 +0100457 init_rwsem(&uprobe->register_rwsem);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530458 init_rwsem(&uprobe->consumer_rwsem);
Oleg Nesterovbbc33d02012-11-21 16:55:38 +0100459 /* For now assume that the instruction need not be single-stepped */
460 __set_bit(UPROBE_SKIP_SSTEP, &uprobe->flags);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530461
462 /* add to uprobes_tree, sorted on inode:offset */
463 cur_uprobe = insert_uprobe(uprobe);
464
465 /* a uprobe exists for this inode:offset combination */
466 if (cur_uprobe) {
467 kfree(uprobe);
468 uprobe = cur_uprobe;
469 iput(inode);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100470 }
471
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530472 return uprobe;
473}
474
Oleg Nesterov9a98e032012-11-23 20:15:17 +0100475static void consumer_add(struct uprobe *uprobe, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530476{
477 down_write(&uprobe->consumer_rwsem);
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530478 uc->next = uprobe->consumers;
479 uprobe->consumers = uc;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530480 up_write(&uprobe->consumer_rwsem);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530481}
482
483/*
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530484 * For uprobe @uprobe, delete the consumer @uc.
485 * Return true if the @uc is deleted successfully
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530486 * or return false.
487 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530488static bool consumer_del(struct uprobe *uprobe, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530489{
490 struct uprobe_consumer **con;
491 bool ret = false;
492
493 down_write(&uprobe->consumer_rwsem);
494 for (con = &uprobe->consumers; *con; con = &(*con)->next) {
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530495 if (*con == uc) {
496 *con = uc->next;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530497 ret = true;
498 break;
499 }
500 }
501 up_write(&uprobe->consumer_rwsem);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100502
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530503 return ret;
504}
505
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530506static int
Oleg Nesterovd4366152012-06-15 17:43:42 +0200507__copy_insn(struct address_space *mapping, struct file *filp, char *insn,
Oleg Nesterov593609a2012-06-15 17:43:59 +0200508 unsigned long nbytes, loff_t offset)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530509{
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530510 struct page *page;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530511
Oleg Nesterovcc359d12012-06-15 17:43:25 +0200512 if (!mapping->a_ops->readpage)
513 return -EIO;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530514 /*
515 * Ensure that the page that has the original instruction is
516 * populated and in page-cache.
517 */
Oleg Nesterov2edb7b52013-03-24 18:37:48 +0100518 page = read_mapping_page(mapping, offset >> PAGE_CACHE_SHIFT, filp);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530519 if (IS_ERR(page))
520 return PTR_ERR(page);
521
Oleg Nesterov2edb7b52013-03-24 18:37:48 +0100522 copy_from_page(page, offset, insn, nbytes);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530523 page_cache_release(page);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100524
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530525 return 0;
526}
527
Oleg Nesterovd4366152012-06-15 17:43:42 +0200528static int copy_insn(struct uprobe *uprobe, struct file *filp)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530529{
530 struct address_space *mapping;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530531 unsigned long nbytes;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100532 int bytes;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530533
Oleg Nesterovd4366152012-06-15 17:43:42 +0200534 nbytes = PAGE_SIZE - (uprobe->offset & ~PAGE_MASK);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530535 mapping = uprobe->inode->i_mapping;
536
537 /* Instruction at end of binary; copy only available bytes */
538 if (uprobe->offset + MAX_UINSN_BYTES > uprobe->inode->i_size)
539 bytes = uprobe->inode->i_size - uprobe->offset;
540 else
541 bytes = MAX_UINSN_BYTES;
542
543 /* Instruction at the page-boundary; copy bytes in second page */
544 if (nbytes < bytes) {
Oleg Nesterovfc36f592012-06-15 17:43:44 +0200545 int err = __copy_insn(mapping, filp, uprobe->arch.insn + nbytes,
546 bytes - nbytes, uprobe->offset + nbytes);
547 if (err)
548 return err;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530549 bytes = nbytes;
550 }
Oleg Nesterovd4366152012-06-15 17:43:42 +0200551 return __copy_insn(mapping, filp, uprobe->arch.insn, bytes, uprobe->offset);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530552}
553
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200554static int prepare_uprobe(struct uprobe *uprobe, struct file *file,
555 struct mm_struct *mm, unsigned long vaddr)
556{
557 int ret = 0;
558
Oleg Nesterov71434f22012-09-30 21:12:44 +0200559 if (test_bit(UPROBE_COPY_INSN, &uprobe->flags))
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200560 return ret;
561
Oleg Nesterovd4d3ccc2012-11-24 18:51:34 +0100562 /* TODO: move this into _register, until then we abuse this sem. */
563 down_write(&uprobe->consumer_rwsem);
Oleg Nesterov71434f22012-09-30 21:12:44 +0200564 if (test_bit(UPROBE_COPY_INSN, &uprobe->flags))
Oleg Nesterov4710f052012-09-30 20:31:41 +0200565 goto out;
566
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200567 ret = copy_insn(uprobe, file);
568 if (ret)
569 goto out;
570
571 ret = -ENOTSUPP;
Ananth N Mavinakayanahalli0908ad62013-03-22 20:46:27 +0530572 if (is_trap_insn((uprobe_opcode_t *)uprobe->arch.insn))
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200573 goto out;
574
575 ret = arch_uprobe_analyze_insn(&uprobe->arch, mm, vaddr);
576 if (ret)
577 goto out;
578
579 /* write_opcode() assumes we don't cross page boundary */
580 BUG_ON((uprobe->offset & ~PAGE_MASK) +
581 UPROBE_SWBP_INSN_SIZE > PAGE_SIZE);
582
583 smp_wmb(); /* pairs with rmb() in find_active_uprobe() */
Oleg Nesterov71434f22012-09-30 21:12:44 +0200584 set_bit(UPROBE_COPY_INSN, &uprobe->flags);
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200585
586 out:
Oleg Nesterovd4d3ccc2012-11-24 18:51:34 +0100587 up_write(&uprobe->consumer_rwsem);
Oleg Nesterov4710f052012-09-30 20:31:41 +0200588
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200589 return ret;
590}
591
Oleg Nesterov8a7f2fa2012-12-28 17:58:38 +0100592static inline bool consumer_filter(struct uprobe_consumer *uc,
593 enum uprobe_filter_ctx ctx, struct mm_struct *mm)
Oleg Nesterov806a98b2012-12-27 18:21:11 +0100594{
Oleg Nesterov8a7f2fa2012-12-28 17:58:38 +0100595 return !uc->filter || uc->filter(uc, ctx, mm);
Oleg Nesterov806a98b2012-12-27 18:21:11 +0100596}
597
Oleg Nesterov8a7f2fa2012-12-28 17:58:38 +0100598static bool filter_chain(struct uprobe *uprobe,
599 enum uprobe_filter_ctx ctx, struct mm_struct *mm)
Oleg Nesterov63633cb2012-11-22 18:30:15 +0100600{
Oleg Nesterov1ff6fee2012-11-24 18:15:46 +0100601 struct uprobe_consumer *uc;
602 bool ret = false;
603
604 down_read(&uprobe->consumer_rwsem);
605 for (uc = uprobe->consumers; uc; uc = uc->next) {
Oleg Nesterov8a7f2fa2012-12-28 17:58:38 +0100606 ret = consumer_filter(uc, ctx, mm);
Oleg Nesterov1ff6fee2012-11-24 18:15:46 +0100607 if (ret)
608 break;
609 }
610 up_read(&uprobe->consumer_rwsem);
611
612 return ret;
Oleg Nesterov63633cb2012-11-22 18:30:15 +0100613}
614
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530615static int
616install_breakpoint(struct uprobe *uprobe, struct mm_struct *mm,
Oleg Nesterov816c03f2012-06-15 17:43:55 +0200617 struct vm_area_struct *vma, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530618{
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +0200619 bool first_uprobe;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530620 int ret;
621
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200622 ret = prepare_uprobe(uprobe, vma->vm_file, mm, vaddr);
623 if (ret)
624 return ret;
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530625
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +0200626 /*
627 * set MMF_HAS_UPROBES in advance for uprobe_pre_sstep_notifier(),
628 * the task can hit this breakpoint right after __replace_page().
629 */
630 first_uprobe = !test_bit(MMF_HAS_UPROBES, &mm->flags);
631 if (first_uprobe)
632 set_bit(MMF_HAS_UPROBES, &mm->flags);
633
Oleg Nesterov816c03f2012-06-15 17:43:55 +0200634 ret = set_swbp(&uprobe->arch, mm, vaddr);
Oleg Nesterov9f68f672012-08-19 16:15:09 +0200635 if (!ret)
636 clear_bit(MMF_RECALC_UPROBES, &mm->flags);
637 else if (first_uprobe)
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +0200638 clear_bit(MMF_HAS_UPROBES, &mm->flags);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530639
640 return ret;
641}
642
Oleg Nesterov076a3652012-09-30 18:54:53 +0200643static int
Oleg Nesterov816c03f2012-06-15 17:43:55 +0200644remove_breakpoint(struct uprobe *uprobe, struct mm_struct *mm, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530645{
Oleg Nesterov9f68f672012-08-19 16:15:09 +0200646 set_bit(MMF_RECALC_UPROBES, &mm->flags);
Oleg Nesterov076a3652012-09-30 18:54:53 +0200647 return set_orig_insn(&uprobe->arch, mm, vaddr);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530648}
649
Oleg Nesterov06b7bcd2012-11-25 22:01:42 +0100650static inline bool uprobe_is_active(struct uprobe *uprobe)
651{
652 return !RB_EMPTY_NODE(&uprobe->rb_node);
653}
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530654/*
Oleg Nesterov778b0322012-05-29 21:30:08 +0200655 * There could be threads that have already hit the breakpoint. They
656 * will recheck the current insn and restart if find_uprobe() fails.
657 * See find_active_uprobe().
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530658 */
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530659static void delete_uprobe(struct uprobe *uprobe)
660{
Oleg Nesterov06b7bcd2012-11-25 22:01:42 +0100661 if (WARN_ON(!uprobe_is_active(uprobe)))
662 return;
663
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200664 spin_lock(&uprobes_treelock);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530665 rb_erase(&uprobe->rb_node, &uprobes_tree);
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200666 spin_unlock(&uprobes_treelock);
Oleg Nesterov06b7bcd2012-11-25 22:01:42 +0100667 RB_CLEAR_NODE(&uprobe->rb_node); /* for uprobe_is_active() */
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530668 iput(uprobe->inode);
669 put_uprobe(uprobe);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530670}
671
Oleg Nesterov26872092012-06-15 17:43:33 +0200672struct map_info {
673 struct map_info *next;
674 struct mm_struct *mm;
Oleg Nesterov816c03f2012-06-15 17:43:55 +0200675 unsigned long vaddr;
Oleg Nesterov26872092012-06-15 17:43:33 +0200676};
677
678static inline struct map_info *free_map_info(struct map_info *info)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530679{
Oleg Nesterov26872092012-06-15 17:43:33 +0200680 struct map_info *next = info->next;
681 kfree(info);
682 return next;
683}
684
685static struct map_info *
686build_map_info(struct address_space *mapping, loff_t offset, bool is_register)
687{
688 unsigned long pgoff = offset >> PAGE_SHIFT;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530689 struct vm_area_struct *vma;
Oleg Nesterov26872092012-06-15 17:43:33 +0200690 struct map_info *curr = NULL;
691 struct map_info *prev = NULL;
692 struct map_info *info;
693 int more = 0;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100694
Oleg Nesterov26872092012-06-15 17:43:33 +0200695 again:
696 mutex_lock(&mapping->i_mmap_mutex);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700697 vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) {
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530698 if (!valid_vma(vma, is_register))
699 continue;
700
Oleg Nesterov7a5bfb62012-06-15 17:43:36 +0200701 if (!prev && !more) {
702 /*
703 * Needs GFP_NOWAIT to avoid i_mmap_mutex recursion through
704 * reclaim. This is optimistic, no harm done if it fails.
705 */
706 prev = kmalloc(sizeof(struct map_info),
707 GFP_NOWAIT | __GFP_NOMEMALLOC | __GFP_NOWARN);
708 if (prev)
709 prev->next = NULL;
710 }
Oleg Nesterov26872092012-06-15 17:43:33 +0200711 if (!prev) {
712 more++;
713 continue;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530714 }
715
Oleg Nesterov26872092012-06-15 17:43:33 +0200716 if (!atomic_inc_not_zero(&vma->vm_mm->mm_users))
717 continue;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100718
Oleg Nesterov26872092012-06-15 17:43:33 +0200719 info = prev;
720 prev = prev->next;
721 info->next = curr;
722 curr = info;
723
724 info->mm = vma->vm_mm;
Oleg Nesterov57683f72012-07-29 20:22:47 +0200725 info->vaddr = offset_to_vaddr(vma, offset);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530726 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530727 mutex_unlock(&mapping->i_mmap_mutex);
728
Oleg Nesterov26872092012-06-15 17:43:33 +0200729 if (!more)
730 goto out;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100731
Oleg Nesterov26872092012-06-15 17:43:33 +0200732 prev = curr;
733 while (curr) {
734 mmput(curr->mm);
735 curr = curr->next;
736 }
737
738 do {
739 info = kmalloc(sizeof(struct map_info), GFP_KERNEL);
740 if (!info) {
741 curr = ERR_PTR(-ENOMEM);
742 goto out;
743 }
744 info->next = prev;
745 prev = info;
746 } while (--more);
747
748 goto again;
749 out:
750 while (prev)
751 prev = free_map_info(prev);
752 return curr;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530753}
754
Oleg Nesterovbdf86472013-02-03 19:21:12 +0100755static int
756register_for_each_vma(struct uprobe *uprobe, struct uprobe_consumer *new)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530757{
Oleg Nesterovbdf86472013-02-03 19:21:12 +0100758 bool is_register = !!new;
Oleg Nesterov26872092012-06-15 17:43:33 +0200759 struct map_info *info;
760 int err = 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530761
Oleg Nesterov32cdba12012-11-14 19:03:42 +0100762 percpu_down_write(&dup_mmap_sem);
Oleg Nesterov26872092012-06-15 17:43:33 +0200763 info = build_map_info(uprobe->inode->i_mapping,
764 uprobe->offset, is_register);
Oleg Nesterov32cdba12012-11-14 19:03:42 +0100765 if (IS_ERR(info)) {
766 err = PTR_ERR(info);
767 goto out;
768 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100769
Oleg Nesterov26872092012-06-15 17:43:33 +0200770 while (info) {
771 struct mm_struct *mm = info->mm;
772 struct vm_area_struct *vma;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100773
Oleg Nesterov076a3652012-09-30 18:54:53 +0200774 if (err && is_register)
Oleg Nesterov26872092012-06-15 17:43:33 +0200775 goto free;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100776
Oleg Nesterov77fc4af2012-05-29 21:29:28 +0200777 down_write(&mm->mmap_sem);
Oleg Nesterovf4d6dfe2012-07-29 20:22:44 +0200778 vma = find_vma(mm, info->vaddr);
779 if (!vma || !valid_vma(vma, is_register) ||
Oleg Nesterovf2817692013-03-17 18:54:44 +0100780 file_inode(vma->vm_file) != uprobe->inode)
Oleg Nesterov26872092012-06-15 17:43:33 +0200781 goto unlock;
782
Oleg Nesterovf4d6dfe2012-07-29 20:22:44 +0200783 if (vma->vm_start > info->vaddr ||
784 vaddr_to_offset(vma, info->vaddr) != uprobe->offset)
Oleg Nesterov26872092012-06-15 17:43:33 +0200785 goto unlock;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530786
Oleg Nesterov806a98b2012-12-27 18:21:11 +0100787 if (is_register) {
788 /* consult only the "caller", new consumer. */
Oleg Nesterovbdf86472013-02-03 19:21:12 +0100789 if (consumer_filter(new,
Oleg Nesterov8a7f2fa2012-12-28 17:58:38 +0100790 UPROBE_FILTER_REGISTER, mm))
Oleg Nesterov806a98b2012-12-27 18:21:11 +0100791 err = install_breakpoint(uprobe, mm, vma, info->vaddr);
792 } else if (test_bit(MMF_HAS_UPROBES, &mm->flags)) {
Oleg Nesterov8a7f2fa2012-12-28 17:58:38 +0100793 if (!filter_chain(uprobe,
794 UPROBE_FILTER_UNREGISTER, mm))
Oleg Nesterov806a98b2012-12-27 18:21:11 +0100795 err |= remove_breakpoint(uprobe, mm, info->vaddr);
796 }
Oleg Nesterov78f74112012-08-08 17:35:08 +0200797
Oleg Nesterov26872092012-06-15 17:43:33 +0200798 unlock:
799 up_write(&mm->mmap_sem);
800 free:
801 mmput(mm);
802 info = free_map_info(info);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530803 }
Oleg Nesterov32cdba12012-11-14 19:03:42 +0100804 out:
805 percpu_up_write(&dup_mmap_sem);
Oleg Nesterov26872092012-06-15 17:43:33 +0200806 return err;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530807}
808
Oleg Nesterov9a98e032012-11-23 20:15:17 +0100809static int __uprobe_register(struct uprobe *uprobe, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530810{
Oleg Nesterov9a98e032012-11-23 20:15:17 +0100811 consumer_add(uprobe, uc);
Oleg Nesterovbdf86472013-02-03 19:21:12 +0100812 return register_for_each_vma(uprobe, uc);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530813}
814
Oleg Nesterov04aab9b2012-11-23 19:43:50 +0100815static void __uprobe_unregister(struct uprobe *uprobe, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530816{
Oleg Nesterov04aab9b2012-11-23 19:43:50 +0100817 int err;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530818
Oleg Nesterov04aab9b2012-11-23 19:43:50 +0100819 if (!consumer_del(uprobe, uc)) /* WARN? */
820 return;
821
Oleg Nesterovbdf86472013-02-03 19:21:12 +0100822 err = register_for_each_vma(uprobe, NULL);
Oleg Nesterovbb929282012-11-24 18:27:08 +0100823 /* TODO : cant unregister? schedule a worker thread */
824 if (!uprobe->consumers && !err)
825 delete_uprobe(uprobe);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530826}
827
828/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100829 * uprobe_register - register a probe
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530830 * @inode: the file in which the probe has to be placed.
831 * @offset: offset from the start of the file.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530832 * @uc: information on howto handle the probe..
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530833 *
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100834 * Apart from the access refcount, uprobe_register() takes a creation
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530835 * refcount (thro alloc_uprobe) if and only if this @uprobe is getting
836 * inserted into the rbtree (i.e first consumer for a @inode:@offset
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100837 * tuple). Creation refcount stops uprobe_unregister from freeing the
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530838 * @uprobe even before the register operation is complete. Creation
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530839 * refcount is released when the last @uc for the @uprobe
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530840 * unregisters.
841 *
842 * Return errno if it cannot successully install probes
843 * else return 0 (success)
844 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530845int uprobe_register(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530846{
847 struct uprobe *uprobe;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100848 int ret;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530849
Anton Arapovea024872013-04-03 18:00:31 +0200850 /* Uprobe must have at least one set consumer */
851 if (!uc->handler && !uc->ret_handler)
852 return -EINVAL;
853
854 /* TODO: Implement return probes */
855 if (uc->ret_handler)
856 return -ENOSYS;
857
Oleg Nesterovf0744af2012-11-21 18:01:43 +0100858 /* Racy, just to catch the obvious mistakes */
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530859 if (offset > i_size_read(inode))
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100860 return -EINVAL;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530861
Oleg Nesterov66d06df2012-11-25 22:48:37 +0100862 retry:
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530863 uprobe = alloc_uprobe(inode, offset);
Oleg Nesterov66d06df2012-11-25 22:48:37 +0100864 if (!uprobe)
865 return -ENOMEM;
866 /*
867 * We can race with uprobe_unregister()->delete_uprobe().
868 * Check uprobe_is_active() and retry if it is false.
869 */
870 down_write(&uprobe->register_rwsem);
871 ret = -EAGAIN;
872 if (likely(uprobe_is_active(uprobe))) {
Oleg Nesterov9a98e032012-11-23 20:15:17 +0100873 ret = __uprobe_register(uprobe, uc);
874 if (ret)
Oleg Nesterov04aab9b2012-11-23 19:43:50 +0100875 __uprobe_unregister(uprobe, uc);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530876 }
Oleg Nesterov66d06df2012-11-25 22:48:37 +0100877 up_write(&uprobe->register_rwsem);
878 put_uprobe(uprobe);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530879
Oleg Nesterov66d06df2012-11-25 22:48:37 +0100880 if (unlikely(ret == -EAGAIN))
881 goto retry;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530882 return ret;
883}
Josh Stonee8440c12013-01-13 19:03:34 +0100884EXPORT_SYMBOL_GPL(uprobe_register);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530885
886/*
Oleg Nesterovbdf86472013-02-03 19:21:12 +0100887 * uprobe_apply - unregister a already registered probe.
888 * @inode: the file in which the probe has to be removed.
889 * @offset: offset from the start of the file.
890 * @uc: consumer which wants to add more or remove some breakpoints
891 * @add: add or remove the breakpoints
892 */
893int uprobe_apply(struct inode *inode, loff_t offset,
894 struct uprobe_consumer *uc, bool add)
895{
896 struct uprobe *uprobe;
897 struct uprobe_consumer *con;
898 int ret = -ENOENT;
899
900 uprobe = find_uprobe(inode, offset);
901 if (!uprobe)
902 return ret;
903
904 down_write(&uprobe->register_rwsem);
905 for (con = uprobe->consumers; con && con != uc ; con = con->next)
906 ;
907 if (con)
908 ret = register_for_each_vma(uprobe, add ? uc : NULL);
909 up_write(&uprobe->register_rwsem);
910 put_uprobe(uprobe);
911
912 return ret;
913}
914
915/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100916 * uprobe_unregister - unregister a already registered probe.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530917 * @inode: the file in which the probe has to be removed.
918 * @offset: offset from the start of the file.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530919 * @uc: identify which probe if multiple probes are colocated.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530920 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530921void uprobe_unregister(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530922{
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100923 struct uprobe *uprobe;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530924
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530925 uprobe = find_uprobe(inode, offset);
926 if (!uprobe)
927 return;
928
Oleg Nesterove591c8d2012-11-24 17:29:40 +0100929 down_write(&uprobe->register_rwsem);
Oleg Nesterov04aab9b2012-11-23 19:43:50 +0100930 __uprobe_unregister(uprobe, uc);
Oleg Nesterove591c8d2012-11-24 17:29:40 +0100931 up_write(&uprobe->register_rwsem);
Sasha Levinc91368c2012-12-20 14:11:30 -0500932 put_uprobe(uprobe);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530933}
Josh Stonee8440c12013-01-13 19:03:34 +0100934EXPORT_SYMBOL_GPL(uprobe_unregister);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530935
Oleg Nesterovda1816b2012-12-29 17:49:11 +0100936static int unapply_uprobe(struct uprobe *uprobe, struct mm_struct *mm)
937{
938 struct vm_area_struct *vma;
939 int err = 0;
940
941 down_read(&mm->mmap_sem);
942 for (vma = mm->mmap; vma; vma = vma->vm_next) {
943 unsigned long vaddr;
944 loff_t offset;
945
946 if (!valid_vma(vma, false) ||
Oleg Nesterovf2817692013-03-17 18:54:44 +0100947 file_inode(vma->vm_file) != uprobe->inode)
Oleg Nesterovda1816b2012-12-29 17:49:11 +0100948 continue;
949
950 offset = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
951 if (uprobe->offset < offset ||
952 uprobe->offset >= offset + vma->vm_end - vma->vm_start)
953 continue;
954
955 vaddr = offset_to_vaddr(vma, uprobe->offset);
956 err |= remove_breakpoint(uprobe, mm, vaddr);
957 }
958 up_read(&mm->mmap_sem);
959
960 return err;
961}
962
Oleg Nesterov891c3972012-07-29 20:22:40 +0200963static struct rb_node *
964find_node_in_range(struct inode *inode, loff_t min, loff_t max)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530965{
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530966 struct rb_node *n = uprobes_tree.rb_node;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530967
968 while (n) {
Oleg Nesterov891c3972012-07-29 20:22:40 +0200969 struct uprobe *u = rb_entry(n, struct uprobe, rb_node);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100970
Oleg Nesterov891c3972012-07-29 20:22:40 +0200971 if (inode < u->inode) {
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530972 n = n->rb_left;
Oleg Nesterov891c3972012-07-29 20:22:40 +0200973 } else if (inode > u->inode) {
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530974 n = n->rb_right;
Oleg Nesterov891c3972012-07-29 20:22:40 +0200975 } else {
976 if (max < u->offset)
977 n = n->rb_left;
978 else if (min > u->offset)
979 n = n->rb_right;
980 else
981 break;
982 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530983 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100984
Oleg Nesterov891c3972012-07-29 20:22:40 +0200985 return n;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530986}
987
988/*
Oleg Nesterov891c3972012-07-29 20:22:40 +0200989 * For a given range in vma, build a list of probes that need to be inserted.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530990 */
Oleg Nesterov891c3972012-07-29 20:22:40 +0200991static void build_probe_list(struct inode *inode,
992 struct vm_area_struct *vma,
993 unsigned long start, unsigned long end,
994 struct list_head *head)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530995{
Oleg Nesterov891c3972012-07-29 20:22:40 +0200996 loff_t min, max;
Oleg Nesterov891c3972012-07-29 20:22:40 +0200997 struct rb_node *n, *t;
998 struct uprobe *u;
999
1000 INIT_LIST_HEAD(head);
Oleg Nesterovcb113b42012-07-29 20:22:42 +02001001 min = vaddr_to_offset(vma, start);
Oleg Nesterov891c3972012-07-29 20:22:40 +02001002 max = min + (end - start) - 1;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301003
Oleg Nesterov6f47caa2012-08-18 17:01:57 +02001004 spin_lock(&uprobes_treelock);
Oleg Nesterov891c3972012-07-29 20:22:40 +02001005 n = find_node_in_range(inode, min, max);
1006 if (n) {
1007 for (t = n; t; t = rb_prev(t)) {
1008 u = rb_entry(t, struct uprobe, rb_node);
1009 if (u->inode != inode || u->offset < min)
1010 break;
1011 list_add(&u->pending_list, head);
1012 atomic_inc(&u->ref);
1013 }
1014 for (t = n; (t = rb_next(t)); ) {
1015 u = rb_entry(t, struct uprobe, rb_node);
1016 if (u->inode != inode || u->offset > max)
1017 break;
1018 list_add(&u->pending_list, head);
1019 atomic_inc(&u->ref);
1020 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301021 }
Oleg Nesterov6f47caa2012-08-18 17:01:57 +02001022 spin_unlock(&uprobes_treelock);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301023}
1024
1025/*
Oleg Nesterov5e5be712012-08-06 14:49:56 +02001026 * Called from mmap_region/vma_adjust with mm->mmap_sem acquired.
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301027 *
Oleg Nesterov5e5be712012-08-06 14:49:56 +02001028 * Currently we ignore all errors and always return 0, the callers
1029 * can't handle the failure anyway.
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301030 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001031int uprobe_mmap(struct vm_area_struct *vma)
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301032{
1033 struct list_head tmp_list;
Oleg Nesterov665605a2012-07-29 20:22:29 +02001034 struct uprobe *uprobe, *u;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301035 struct inode *inode;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301036
Oleg Nesterov441f1eb2012-11-25 19:54:29 +01001037 if (no_uprobe_events() || !valid_vma(vma, true))
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001038 return 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301039
Oleg Nesterovf2817692013-03-17 18:54:44 +01001040 inode = file_inode(vma->vm_file);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301041 if (!inode)
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001042 return 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301043
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301044 mutex_lock(uprobes_mmap_hash(inode));
Oleg Nesterov891c3972012-07-29 20:22:40 +02001045 build_probe_list(inode, vma, vma->vm_start, vma->vm_end, &tmp_list);
Oleg Nesterov806a98b2012-12-27 18:21:11 +01001046 /*
1047 * We can race with uprobe_unregister(), this uprobe can be already
1048 * removed. But in this case filter_chain() must return false, all
1049 * consumers have gone away.
1050 */
Oleg Nesterov665605a2012-07-29 20:22:29 +02001051 list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
Oleg Nesterov806a98b2012-12-27 18:21:11 +01001052 if (!fatal_signal_pending(current) &&
Oleg Nesterov8a7f2fa2012-12-28 17:58:38 +01001053 filter_chain(uprobe, UPROBE_FILTER_MMAP, vma->vm_mm)) {
Oleg Nesterov57683f72012-07-29 20:22:47 +02001054 unsigned long vaddr = offset_to_vaddr(vma, uprobe->offset);
Oleg Nesterov5e5be712012-08-06 14:49:56 +02001055 install_breakpoint(uprobe, vma->vm_mm, vma, vaddr);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301056 }
1057 put_uprobe(uprobe);
1058 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301059 mutex_unlock(uprobes_mmap_hash(inode));
1060
Oleg Nesterov5e5be712012-08-06 14:49:56 +02001061 return 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301062}
1063
Oleg Nesterov9f68f672012-08-19 16:15:09 +02001064static bool
1065vma_has_uprobes(struct vm_area_struct *vma, unsigned long start, unsigned long end)
1066{
1067 loff_t min, max;
1068 struct inode *inode;
1069 struct rb_node *n;
1070
Oleg Nesterovf2817692013-03-17 18:54:44 +01001071 inode = file_inode(vma->vm_file);
Oleg Nesterov9f68f672012-08-19 16:15:09 +02001072
1073 min = vaddr_to_offset(vma, start);
1074 max = min + (end - start) - 1;
1075
1076 spin_lock(&uprobes_treelock);
1077 n = find_node_in_range(inode, min, max);
1078 spin_unlock(&uprobes_treelock);
1079
1080 return !!n;
1081}
1082
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301083/*
1084 * Called in context of a munmap of a vma.
1085 */
Srikar Dronamrajucbc91f72012-04-11 16:05:27 +05301086void uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end)
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301087{
Oleg Nesterov441f1eb2012-11-25 19:54:29 +01001088 if (no_uprobe_events() || !valid_vma(vma, false))
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301089 return;
1090
Oleg Nesterov2fd611a2012-07-29 20:22:31 +02001091 if (!atomic_read(&vma->vm_mm->mm_users)) /* called by mmput() ? */
1092 return;
1093
Oleg Nesterov9f68f672012-08-19 16:15:09 +02001094 if (!test_bit(MMF_HAS_UPROBES, &vma->vm_mm->flags) ||
1095 test_bit(MMF_RECALC_UPROBES, &vma->vm_mm->flags))
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +02001096 return;
1097
Oleg Nesterov9f68f672012-08-19 16:15:09 +02001098 if (vma_has_uprobes(vma, start, end))
1099 set_bit(MMF_RECALC_UPROBES, &vma->vm_mm->flags);
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301100}
1101
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301102/* Slot allocation for XOL */
1103static int xol_add_vma(struct xol_area *area)
1104{
Oleg Nesterovc8a82532012-12-30 17:40:39 +01001105 struct mm_struct *mm = current->mm;
1106 int ret = -EALREADY;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301107
1108 down_write(&mm->mmap_sem);
1109 if (mm->uprobes_state.xol_area)
1110 goto fail;
1111
1112 ret = -ENOMEM;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301113 /* Try to map as high as possible, this is only a hint. */
1114 area->vaddr = get_unmapped_area(NULL, TASK_SIZE - PAGE_SIZE, PAGE_SIZE, 0, 0);
1115 if (area->vaddr & ~PAGE_MASK) {
1116 ret = area->vaddr;
1117 goto fail;
1118 }
1119
1120 ret = install_special_mapping(mm, area->vaddr, PAGE_SIZE,
1121 VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO, &area->page);
1122 if (ret)
1123 goto fail;
1124
1125 smp_wmb(); /* pairs with get_xol_area() */
1126 mm->uprobes_state.xol_area = area;
1127 ret = 0;
Oleg Nesterovc8a82532012-12-30 17:40:39 +01001128 fail:
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301129 up_write(&mm->mmap_sem);
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301130
1131 return ret;
1132}
1133
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301134/*
Oleg Nesterov9b545df2012-12-31 16:39:49 +01001135 * get_xol_area - Allocate process's xol_area if necessary.
1136 * This area will be used for storing instructions for execution out of line.
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301137 *
1138 * Returns the allocated area or NULL.
1139 */
Oleg Nesterov9b545df2012-12-31 16:39:49 +01001140static struct xol_area *get_xol_area(void)
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301141{
Oleg Nesterov9b545df2012-12-31 16:39:49 +01001142 struct mm_struct *mm = current->mm;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301143 struct xol_area *area;
Anton Arapove78aebf2013-04-03 18:00:32 +02001144 uprobe_opcode_t insn = UPROBE_SWBP_INSN;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301145
Oleg Nesterov9b545df2012-12-31 16:39:49 +01001146 area = mm->uprobes_state.xol_area;
1147 if (area)
1148 goto ret;
1149
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301150 area = kzalloc(sizeof(*area), GFP_KERNEL);
1151 if (unlikely(!area))
Oleg Nesterovc8a82532012-12-30 17:40:39 +01001152 goto out;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301153
1154 area->bitmap = kzalloc(BITS_TO_LONGS(UINSNS_PER_PAGE) * sizeof(long), GFP_KERNEL);
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301155 if (!area->bitmap)
Oleg Nesterovc8a82532012-12-30 17:40:39 +01001156 goto free_area;
1157
1158 area->page = alloc_page(GFP_HIGHUSER);
1159 if (!area->page)
1160 goto free_bitmap;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301161
Anton Arapove78aebf2013-04-03 18:00:32 +02001162 /* allocate first slot of task's xol_area for the return probes */
1163 set_bit(0, area->bitmap);
1164 copy_to_page(area->page, 0, &insn, UPROBE_SWBP_INSN_SIZE);
1165 atomic_set(&area->slot_count, 1);
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301166 init_waitqueue_head(&area->wq);
Anton Arapove78aebf2013-04-03 18:00:32 +02001167
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301168 if (!xol_add_vma(area))
1169 return area;
1170
Oleg Nesterovc8a82532012-12-30 17:40:39 +01001171 __free_page(area->page);
1172 free_bitmap:
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301173 kfree(area->bitmap);
Oleg Nesterovc8a82532012-12-30 17:40:39 +01001174 free_area:
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301175 kfree(area);
Oleg Nesterovc8a82532012-12-30 17:40:39 +01001176 out:
Oleg Nesterov9b545df2012-12-31 16:39:49 +01001177 area = mm->uprobes_state.xol_area;
1178 ret:
1179 smp_read_barrier_depends(); /* pairs with wmb in xol_add_vma() */
1180 return area;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301181}
1182
1183/*
1184 * uprobe_clear_state - Free the area allocated for slots.
1185 */
1186void uprobe_clear_state(struct mm_struct *mm)
1187{
1188 struct xol_area *area = mm->uprobes_state.xol_area;
1189
1190 if (!area)
1191 return;
1192
1193 put_page(area->page);
1194 kfree(area->bitmap);
1195 kfree(area);
1196}
1197
Oleg Nesterov32cdba12012-11-14 19:03:42 +01001198void uprobe_start_dup_mmap(void)
1199{
1200 percpu_down_read(&dup_mmap_sem);
1201}
1202
1203void uprobe_end_dup_mmap(void)
1204{
1205 percpu_up_read(&dup_mmap_sem);
1206}
1207
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +02001208void uprobe_dup_mmap(struct mm_struct *oldmm, struct mm_struct *newmm)
1209{
Oleg Nesterov61559a82012-08-08 17:17:46 +02001210 newmm->uprobes_state.xol_area = NULL;
1211
Oleg Nesterov9f68f672012-08-19 16:15:09 +02001212 if (test_bit(MMF_HAS_UPROBES, &oldmm->flags)) {
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +02001213 set_bit(MMF_HAS_UPROBES, &newmm->flags);
Oleg Nesterov9f68f672012-08-19 16:15:09 +02001214 /* unconditionally, dup_mmap() skips VM_DONTCOPY vmas */
1215 set_bit(MMF_RECALC_UPROBES, &newmm->flags);
1216 }
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +02001217}
1218
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301219/*
1220 * - search for a free slot.
1221 */
1222static unsigned long xol_take_insn_slot(struct xol_area *area)
1223{
1224 unsigned long slot_addr;
1225 int slot_nr;
1226
1227 do {
1228 slot_nr = find_first_zero_bit(area->bitmap, UINSNS_PER_PAGE);
1229 if (slot_nr < UINSNS_PER_PAGE) {
1230 if (!test_and_set_bit(slot_nr, area->bitmap))
1231 break;
1232
1233 slot_nr = UINSNS_PER_PAGE;
1234 continue;
1235 }
1236 wait_event(area->wq, (atomic_read(&area->slot_count) < UINSNS_PER_PAGE));
1237 } while (slot_nr >= UINSNS_PER_PAGE);
1238
1239 slot_addr = area->vaddr + (slot_nr * UPROBE_XOL_SLOT_BYTES);
1240 atomic_inc(&area->slot_count);
1241
1242 return slot_addr;
1243}
1244
1245/*
Oleg Nesterova6cb3f62012-12-31 18:00:06 +01001246 * xol_get_insn_slot - allocate a slot for xol.
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301247 * Returns the allocated slot address or 0.
1248 */
Oleg Nesterova6cb3f62012-12-31 18:00:06 +01001249static unsigned long xol_get_insn_slot(struct uprobe *uprobe)
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301250{
1251 struct xol_area *area;
Oleg Nesterova6cb3f62012-12-31 18:00:06 +01001252 unsigned long xol_vaddr;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301253
Oleg Nesterov9b545df2012-12-31 16:39:49 +01001254 area = get_xol_area();
1255 if (!area)
1256 return 0;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301257
Oleg Nesterova6cb3f62012-12-31 18:00:06 +01001258 xol_vaddr = xol_take_insn_slot(area);
1259 if (unlikely(!xol_vaddr))
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301260 return 0;
1261
Oleg Nesterova6cb3f62012-12-31 18:00:06 +01001262 /* Initialize the slot */
Oleg Nesterov5669cce2013-03-24 18:58:04 +01001263 copy_to_page(area->page, xol_vaddr, uprobe->arch.insn, MAX_UINSN_BYTES);
Rabin Vincent65b6ecc2012-11-14 18:27:07 +01001264 /*
1265 * We probably need flush_icache_user_range() but it needs vma.
1266 * This should work on supported architectures too.
1267 */
1268 flush_dcache_page(area->page);
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301269
Oleg Nesterova6cb3f62012-12-31 18:00:06 +01001270 return xol_vaddr;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301271}
1272
1273/*
1274 * xol_free_insn_slot - If slot was earlier allocated by
1275 * @xol_get_insn_slot(), make the slot available for
1276 * subsequent requests.
1277 */
1278static void xol_free_insn_slot(struct task_struct *tsk)
1279{
1280 struct xol_area *area;
1281 unsigned long vma_end;
1282 unsigned long slot_addr;
1283
1284 if (!tsk->mm || !tsk->mm->uprobes_state.xol_area || !tsk->utask)
1285 return;
1286
1287 slot_addr = tsk->utask->xol_vaddr;
Oleg Nesterovaf4355e2012-12-31 18:37:11 +01001288 if (unlikely(!slot_addr))
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301289 return;
1290
1291 area = tsk->mm->uprobes_state.xol_area;
1292 vma_end = area->vaddr + PAGE_SIZE;
1293 if (area->vaddr <= slot_addr && slot_addr < vma_end) {
1294 unsigned long offset;
1295 int slot_nr;
1296
1297 offset = slot_addr - area->vaddr;
1298 slot_nr = offset / UPROBE_XOL_SLOT_BYTES;
1299 if (slot_nr >= UINSNS_PER_PAGE)
1300 return;
1301
1302 clear_bit(slot_nr, area->bitmap);
1303 atomic_dec(&area->slot_count);
1304 if (waitqueue_active(&area->wq))
1305 wake_up(&area->wq);
1306
1307 tsk->utask->xol_vaddr = 0;
1308 }
1309}
1310
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301311/**
1312 * uprobe_get_swbp_addr - compute address of swbp given post-swbp regs
1313 * @regs: Reflects the saved state of the task after it has hit a breakpoint
1314 * instruction.
1315 * Return the address of the breakpoint instruction.
1316 */
1317unsigned long __weak uprobe_get_swbp_addr(struct pt_regs *regs)
1318{
1319 return instruction_pointer(regs) - UPROBE_SWBP_INSN_SIZE;
1320}
1321
1322/*
1323 * Called with no locks held.
1324 * Called in context of a exiting or a exec-ing thread.
1325 */
1326void uprobe_free_utask(struct task_struct *t)
1327{
1328 struct uprobe_task *utask = t->utask;
Anton Arapov0dfd0eb2013-04-03 18:00:35 +02001329 struct return_instance *ri, *tmp;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301330
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301331 if (!utask)
1332 return;
1333
1334 if (utask->active_uprobe)
1335 put_uprobe(utask->active_uprobe);
1336
Anton Arapov0dfd0eb2013-04-03 18:00:35 +02001337 ri = utask->return_instances;
1338 while (ri) {
1339 tmp = ri;
1340 ri = ri->next;
1341
1342 put_uprobe(tmp->uprobe);
1343 kfree(tmp);
1344 }
1345
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301346 xol_free_insn_slot(t);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301347 kfree(utask);
1348 t->utask = NULL;
1349}
1350
1351/*
1352 * Called in context of a new clone/fork from copy_process.
1353 */
1354void uprobe_copy_process(struct task_struct *t)
1355{
1356 t->utask = NULL;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301357}
1358
1359/*
Oleg Nesterov5a2df662012-12-31 17:03:32 +01001360 * Allocate a uprobe_task object for the task if if necessary.
1361 * Called when the thread hits a breakpoint.
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301362 *
1363 * Returns:
1364 * - pointer to new uprobe_task on success
1365 * - NULL otherwise
1366 */
Oleg Nesterov5a2df662012-12-31 17:03:32 +01001367static struct uprobe_task *get_utask(void)
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301368{
Oleg Nesterov5a2df662012-12-31 17:03:32 +01001369 if (!current->utask)
1370 current->utask = kzalloc(sizeof(struct uprobe_task), GFP_KERNEL);
1371 return current->utask;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301372}
1373
Anton Arapove78aebf2013-04-03 18:00:32 +02001374/*
1375 * Current area->vaddr notion assume the trampoline address is always
1376 * equal area->vaddr.
1377 *
1378 * Returns -1 in case the xol_area is not allocated.
1379 */
1380static unsigned long get_trampoline_vaddr(void)
1381{
1382 struct xol_area *area;
1383 unsigned long trampoline_vaddr = -1;
1384
1385 area = current->mm->uprobes_state.xol_area;
1386 smp_read_barrier_depends();
1387 if (area)
1388 trampoline_vaddr = area->vaddr;
1389
1390 return trampoline_vaddr;
1391}
1392
Anton Arapov0dfd0eb2013-04-03 18:00:35 +02001393static void prepare_uretprobe(struct uprobe *uprobe, struct pt_regs *regs)
1394{
1395 struct return_instance *ri;
1396 struct uprobe_task *utask;
1397 unsigned long orig_ret_vaddr, trampoline_vaddr;
1398 bool chained = false;
1399
1400 if (!get_xol_area())
1401 return;
1402
1403 utask = get_utask();
1404 if (!utask)
1405 return;
1406
1407 ri = kzalloc(sizeof(struct return_instance), GFP_KERNEL);
1408 if (!ri)
1409 goto fail;
1410
1411 trampoline_vaddr = get_trampoline_vaddr();
1412 orig_ret_vaddr = arch_uretprobe_hijack_return_addr(trampoline_vaddr, regs);
1413 if (orig_ret_vaddr == -1)
1414 goto fail;
1415
1416 /*
1417 * We don't want to keep trampoline address in stack, rather keep the
1418 * original return address of first caller thru all the consequent
1419 * instances. This also makes breakpoint unwrapping easier.
1420 */
1421 if (orig_ret_vaddr == trampoline_vaddr) {
1422 if (!utask->return_instances) {
1423 /*
1424 * This situation is not possible. Likely we have an
1425 * attack from user-space.
1426 */
1427 pr_warn("uprobe: unable to set uretprobe pid/tgid=%d/%d\n",
1428 current->pid, current->tgid);
1429 goto fail;
1430 }
1431
1432 chained = true;
1433 orig_ret_vaddr = utask->return_instances->orig_ret_vaddr;
1434 }
1435
1436 atomic_inc(&uprobe->ref);
1437 ri->uprobe = uprobe;
1438 ri->func = instruction_pointer(regs);
1439 ri->orig_ret_vaddr = orig_ret_vaddr;
1440 ri->chained = chained;
1441
1442 /* add instance to the stack */
1443 ri->next = utask->return_instances;
1444 utask->return_instances = ri;
1445
1446 return;
1447
1448 fail:
1449 kfree(ri);
1450}
1451
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301452/* Prepare to single-step probed instruction out of line. */
1453static int
Oleg Nesterova6cb3f62012-12-31 18:00:06 +01001454pre_ssout(struct uprobe *uprobe, struct pt_regs *regs, unsigned long bp_vaddr)
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301455{
Oleg Nesterova6cb3f62012-12-31 18:00:06 +01001456 struct uprobe_task *utask;
1457 unsigned long xol_vaddr;
Oleg Nesterovaba51022012-12-31 18:12:48 +01001458 int err;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301459
Oleg Nesterov608e7422012-12-31 18:20:42 +01001460 utask = get_utask();
1461 if (!utask)
1462 return -ENOMEM;
Oleg Nesterova6cb3f62012-12-31 18:00:06 +01001463
1464 xol_vaddr = xol_get_insn_slot(uprobe);
1465 if (!xol_vaddr)
1466 return -ENOMEM;
1467
1468 utask->xol_vaddr = xol_vaddr;
1469 utask->vaddr = bp_vaddr;
1470
Oleg Nesterovaba51022012-12-31 18:12:48 +01001471 err = arch_uprobe_pre_xol(&uprobe->arch, regs);
1472 if (unlikely(err)) {
1473 xol_free_insn_slot(current);
1474 return err;
1475 }
1476
Oleg Nesterov608e7422012-12-31 18:20:42 +01001477 utask->active_uprobe = uprobe;
1478 utask->state = UTASK_SSTEP;
Oleg Nesterovaba51022012-12-31 18:12:48 +01001479 return 0;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301480}
1481
1482/*
1483 * If we are singlestepping, then ensure this thread is not connected to
1484 * non-fatal signals until completion of singlestep. When xol insn itself
1485 * triggers the signal, restart the original insn even if the task is
1486 * already SIGKILL'ed (since coredump should report the correct ip). This
1487 * is even more important if the task has a handler for SIGSEGV/etc, The
1488 * _same_ instruction should be repeated again after return from the signal
1489 * handler, and SSTEP can never finish in this case.
1490 */
1491bool uprobe_deny_signal(void)
1492{
1493 struct task_struct *t = current;
1494 struct uprobe_task *utask = t->utask;
1495
1496 if (likely(!utask || !utask->active_uprobe))
1497 return false;
1498
1499 WARN_ON_ONCE(utask->state != UTASK_SSTEP);
1500
1501 if (signal_pending(t)) {
1502 spin_lock_irq(&t->sighand->siglock);
1503 clear_tsk_thread_flag(t, TIF_SIGPENDING);
1504 spin_unlock_irq(&t->sighand->siglock);
1505
1506 if (__fatal_signal_pending(t) || arch_uprobe_xol_was_trapped(t)) {
1507 utask->state = UTASK_SSTEP_TRAPPED;
1508 set_tsk_thread_flag(t, TIF_UPROBE);
1509 set_tsk_thread_flag(t, TIF_NOTIFY_RESUME);
1510 }
1511 }
1512
1513 return true;
1514}
1515
1516/*
1517 * Avoid singlestepping the original instruction if the original instruction
1518 * is a NOP or can be emulated.
1519 */
1520static bool can_skip_sstep(struct uprobe *uprobe, struct pt_regs *regs)
1521{
Oleg Nesterov71434f22012-09-30 21:12:44 +02001522 if (test_bit(UPROBE_SKIP_SSTEP, &uprobe->flags)) {
Oleg Nesterov0578a972012-09-14 18:31:23 +02001523 if (arch_uprobe_skip_sstep(&uprobe->arch, regs))
1524 return true;
Oleg Nesterov71434f22012-09-30 21:12:44 +02001525 clear_bit(UPROBE_SKIP_SSTEP, &uprobe->flags);
Oleg Nesterov0578a972012-09-14 18:31:23 +02001526 }
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301527 return false;
1528}
1529
Oleg Nesterov499a4f32012-08-19 17:41:34 +02001530static void mmf_recalc_uprobes(struct mm_struct *mm)
1531{
1532 struct vm_area_struct *vma;
1533
1534 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1535 if (!valid_vma(vma, false))
1536 continue;
1537 /*
1538 * This is not strictly accurate, we can race with
1539 * uprobe_unregister() and see the already removed
1540 * uprobe if delete_uprobe() was not yet called.
Oleg Nesterov63633cb2012-11-22 18:30:15 +01001541 * Or this uprobe can be filtered out.
Oleg Nesterov499a4f32012-08-19 17:41:34 +02001542 */
1543 if (vma_has_uprobes(vma, vma->vm_start, vma->vm_end))
1544 return;
1545 }
1546
1547 clear_bit(MMF_HAS_UPROBES, &mm->flags);
1548}
1549
Ananth N Mavinakayanahalli0908ad62013-03-22 20:46:27 +05301550static int is_trap_at_addr(struct mm_struct *mm, unsigned long vaddr)
Oleg Nesterovec75fba2012-09-23 21:55:19 +02001551{
1552 struct page *page;
1553 uprobe_opcode_t opcode;
1554 int result;
1555
1556 pagefault_disable();
1557 result = __copy_from_user_inatomic(&opcode, (void __user*)vaddr,
1558 sizeof(opcode));
1559 pagefault_enable();
1560
1561 if (likely(result == 0))
1562 goto out;
1563
1564 result = get_user_pages(NULL, mm, vaddr, 1, 0, 1, &page, NULL);
1565 if (result < 0)
1566 return result;
1567
Oleg Nesterovab0d8052013-03-24 18:24:37 +01001568 copy_from_page(page, vaddr, &opcode, UPROBE_SWBP_INSN_SIZE);
Oleg Nesterovec75fba2012-09-23 21:55:19 +02001569 put_page(page);
1570 out:
Ananth N Mavinakayanahalli0908ad62013-03-22 20:46:27 +05301571 /* This needs to return true for any variant of the trap insn */
1572 return is_trap_insn(&opcode);
Oleg Nesterovec75fba2012-09-23 21:55:19 +02001573}
1574
Oleg Nesterovd790d342012-05-29 21:29:14 +02001575static struct uprobe *find_active_uprobe(unsigned long bp_vaddr, int *is_swbp)
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001576{
1577 struct mm_struct *mm = current->mm;
1578 struct uprobe *uprobe = NULL;
1579 struct vm_area_struct *vma;
1580
1581 down_read(&mm->mmap_sem);
1582 vma = find_vma(mm, bp_vaddr);
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001583 if (vma && vma->vm_start <= bp_vaddr) {
1584 if (valid_vma(vma, false)) {
Oleg Nesterovf2817692013-03-17 18:54:44 +01001585 struct inode *inode = file_inode(vma->vm_file);
Oleg Nesterovcb113b42012-07-29 20:22:42 +02001586 loff_t offset = vaddr_to_offset(vma, bp_vaddr);
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001587
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001588 uprobe = find_uprobe(inode, offset);
1589 }
Oleg Nesterovd790d342012-05-29 21:29:14 +02001590
1591 if (!uprobe)
Ananth N Mavinakayanahalli0908ad62013-03-22 20:46:27 +05301592 *is_swbp = is_trap_at_addr(mm, bp_vaddr);
Oleg Nesterovd790d342012-05-29 21:29:14 +02001593 } else {
1594 *is_swbp = -EFAULT;
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001595 }
Oleg Nesterov499a4f32012-08-19 17:41:34 +02001596
1597 if (!uprobe && test_and_clear_bit(MMF_RECALC_UPROBES, &mm->flags))
1598 mmf_recalc_uprobes(mm);
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001599 up_read(&mm->mmap_sem);
1600
1601 return uprobe;
1602}
1603
Oleg Nesterovda1816b2012-12-29 17:49:11 +01001604static void handler_chain(struct uprobe *uprobe, struct pt_regs *regs)
1605{
1606 struct uprobe_consumer *uc;
1607 int remove = UPROBE_HANDLER_REMOVE;
Anton Arapov0dfd0eb2013-04-03 18:00:35 +02001608 bool need_prep = false; /* prepare return uprobe, when needed */
Oleg Nesterovda1816b2012-12-29 17:49:11 +01001609
1610 down_read(&uprobe->register_rwsem);
1611 for (uc = uprobe->consumers; uc; uc = uc->next) {
Anton Arapovea024872013-04-03 18:00:31 +02001612 int rc = 0;
Oleg Nesterovda1816b2012-12-29 17:49:11 +01001613
Anton Arapovea024872013-04-03 18:00:31 +02001614 if (uc->handler) {
1615 rc = uc->handler(uc, regs);
1616 WARN(rc & ~UPROBE_HANDLER_MASK,
1617 "bad rc=0x%x from %pf()\n", rc, uc->handler);
1618 }
Anton Arapov0dfd0eb2013-04-03 18:00:35 +02001619
1620 if (uc->ret_handler)
1621 need_prep = true;
1622
Oleg Nesterovda1816b2012-12-29 17:49:11 +01001623 remove &= rc;
1624 }
1625
Anton Arapov0dfd0eb2013-04-03 18:00:35 +02001626 if (need_prep && !remove)
1627 prepare_uretprobe(uprobe, regs); /* put bp at return */
1628
Oleg Nesterovda1816b2012-12-29 17:49:11 +01001629 if (remove && uprobe->consumers) {
1630 WARN_ON(!uprobe_is_active(uprobe));
1631 unapply_uprobe(uprobe, current->mm);
1632 }
1633 up_read(&uprobe->register_rwsem);
1634}
1635
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301636/*
1637 * Run handler and ask thread to singlestep.
1638 * Ensure all non-fatal signals cannot interrupt thread while it singlesteps.
1639 */
1640static void handle_swbp(struct pt_regs *regs)
1641{
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301642 struct uprobe *uprobe;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301643 unsigned long bp_vaddr;
Oleg Nesterov56bb4cf2012-05-29 21:29:47 +02001644 int uninitialized_var(is_swbp);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301645
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301646 bp_vaddr = uprobe_get_swbp_addr(regs);
Oleg Nesterovd790d342012-05-29 21:29:14 +02001647 uprobe = find_active_uprobe(bp_vaddr, &is_swbp);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301648
1649 if (!uprobe) {
Oleg Nesterov56bb4cf2012-05-29 21:29:47 +02001650 if (is_swbp > 0) {
1651 /* No matching uprobe; signal SIGTRAP. */
1652 send_sig(SIGTRAP, current, 0);
1653 } else {
1654 /*
1655 * Either we raced with uprobe_unregister() or we can't
1656 * access this memory. The latter is only possible if
1657 * another thread plays with our ->mm. In both cases
1658 * we can simply restart. If this vma was unmapped we
1659 * can pretend this insn was not executed yet and get
1660 * the (correct) SIGSEGV after restart.
1661 */
1662 instruction_pointer_set(regs, bp_vaddr);
1663 }
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301664 return;
1665 }
Oleg Nesterov74e59df2012-12-30 15:54:08 +01001666
1667 /* change it in advance for ->handler() and restart */
1668 instruction_pointer_set(regs, bp_vaddr);
1669
Oleg Nesterov142b18d2012-09-29 21:56:57 +02001670 /*
1671 * TODO: move copy_insn/etc into _register and remove this hack.
1672 * After we hit the bp, _unregister + _register can install the
1673 * new and not-yet-analyzed uprobe at the same address, restart.
1674 */
1675 smp_rmb(); /* pairs with wmb() in install_breakpoint() */
Oleg Nesterov71434f22012-09-30 21:12:44 +02001676 if (unlikely(!test_bit(UPROBE_COPY_INSN, &uprobe->flags)))
Oleg Nesterov74e59df2012-12-30 15:54:08 +01001677 goto out;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301678
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301679 handler_chain(uprobe, regs);
Oleg Nesterov0578a972012-09-14 18:31:23 +02001680 if (can_skip_sstep(uprobe, regs))
1681 goto out;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301682
Oleg Nesterov608e7422012-12-31 18:20:42 +01001683 if (!pre_ssout(uprobe, regs, bp_vaddr))
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301684 return;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301685
Oleg Nesterov74e59df2012-12-30 15:54:08 +01001686 /* can_skip_sstep() succeeded, or restart if can't singlestep */
Oleg Nesterov0578a972012-09-14 18:31:23 +02001687out:
Sebastian Andrzej Siewior8bd87442012-08-07 18:12:30 +02001688 put_uprobe(uprobe);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301689}
1690
1691/*
1692 * Perform required fix-ups and disable singlestep.
1693 * Allow pending signals to take effect.
1694 */
1695static void handle_singlestep(struct uprobe_task *utask, struct pt_regs *regs)
1696{
1697 struct uprobe *uprobe;
1698
1699 uprobe = utask->active_uprobe;
1700 if (utask->state == UTASK_SSTEP_ACK)
1701 arch_uprobe_post_xol(&uprobe->arch, regs);
1702 else if (utask->state == UTASK_SSTEP_TRAPPED)
1703 arch_uprobe_abort_xol(&uprobe->arch, regs);
1704 else
1705 WARN_ON_ONCE(1);
1706
1707 put_uprobe(uprobe);
1708 utask->active_uprobe = NULL;
1709 utask->state = UTASK_RUNNING;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301710 xol_free_insn_slot(current);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301711
1712 spin_lock_irq(&current->sighand->siglock);
1713 recalc_sigpending(); /* see uprobe_deny_signal() */
1714 spin_unlock_irq(&current->sighand->siglock);
1715}
1716
1717/*
Oleg Nesterov1b08e9072012-09-14 18:52:10 +02001718 * On breakpoint hit, breakpoint notifier sets the TIF_UPROBE flag and
1719 * allows the thread to return from interrupt. After that handle_swbp()
1720 * sets utask->active_uprobe.
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301721 *
Oleg Nesterov1b08e9072012-09-14 18:52:10 +02001722 * On singlestep exception, singlestep notifier sets the TIF_UPROBE flag
1723 * and allows the thread to return from interrupt.
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301724 *
1725 * While returning to userspace, thread notices the TIF_UPROBE flag and calls
1726 * uprobe_notify_resume().
1727 */
1728void uprobe_notify_resume(struct pt_regs *regs)
1729{
1730 struct uprobe_task *utask;
1731
Oleg Nesterovdb023ea2012-09-14 19:05:46 +02001732 clear_thread_flag(TIF_UPROBE);
1733
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301734 utask = current->utask;
Oleg Nesterov1b08e9072012-09-14 18:52:10 +02001735 if (utask && utask->active_uprobe)
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301736 handle_singlestep(utask, regs);
Oleg Nesterov1b08e9072012-09-14 18:52:10 +02001737 else
1738 handle_swbp(regs);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301739}
1740
1741/*
1742 * uprobe_pre_sstep_notifier gets called from interrupt context as part of
1743 * notifier mechanism. Set TIF_UPROBE flag and indicate breakpoint hit.
1744 */
1745int uprobe_pre_sstep_notifier(struct pt_regs *regs)
1746{
Anton Arapov0dfd0eb2013-04-03 18:00:35 +02001747 if (!current->mm)
1748 return 0;
1749
1750 if (!test_bit(MMF_HAS_UPROBES, &current->mm->flags) &&
1751 (!current->utask || !current->utask->return_instances))
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301752 return 0;
1753
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301754 set_thread_flag(TIF_UPROBE);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301755 return 1;
1756}
1757
1758/*
1759 * uprobe_post_sstep_notifier gets called in interrupt context as part of notifier
1760 * mechanism. Set TIF_UPROBE flag and indicate completion of singlestep.
1761 */
1762int uprobe_post_sstep_notifier(struct pt_regs *regs)
1763{
1764 struct uprobe_task *utask = current->utask;
1765
1766 if (!current->mm || !utask || !utask->active_uprobe)
1767 /* task is currently not uprobed */
1768 return 0;
1769
1770 utask->state = UTASK_SSTEP_ACK;
1771 set_thread_flag(TIF_UPROBE);
1772 return 1;
1773}
1774
1775static struct notifier_block uprobe_exception_nb = {
1776 .notifier_call = arch_uprobe_exception_notify,
1777 .priority = INT_MAX-1, /* notified after kprobes, kgdb */
1778};
1779
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301780static int __init init_uprobes(void)
1781{
1782 int i;
1783
Oleg Nesterov66d06df2012-11-25 22:48:37 +01001784 for (i = 0; i < UPROBES_HASH_SZ; i++)
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301785 mutex_init(&uprobes_mmap_mutex[i]);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301786
Oleg Nesterov32cdba12012-11-14 19:03:42 +01001787 if (percpu_init_rwsem(&dup_mmap_sem))
1788 return -ENOMEM;
1789
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301790 return register_die_notifier(&uprobe_exception_nb);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301791}
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301792module_init(init_uprobes);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301793
1794static void __exit exit_uprobes(void)
1795{
1796}
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301797module_exit(exit_uprobes);