blob: cdc3c951251c912e5e098dbb859fba5ac41a9433 [file] [log] [blame]
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01002 * User-space Probes (UProbes)
Srikar Dronamraju2b144492012-02-09 14:56:42 +05303 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
Ingo Molnar35aa6212012-02-22 11:37:29 +010018 * Copyright (C) IBM Corporation, 2008-2012
Srikar Dronamraju2b144492012-02-09 14:56:42 +053019 * Authors:
20 * Srikar Dronamraju
21 * Jim Keniston
Ingo Molnar35aa6212012-02-22 11:37:29 +010022 * Copyright (C) 2011-2012 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
Srikar Dronamraju2b144492012-02-09 14:56:42 +053023 */
24
25#include <linux/kernel.h>
26#include <linux/highmem.h>
27#include <linux/pagemap.h> /* read_mapping_page */
28#include <linux/slab.h>
29#include <linux/sched.h>
30#include <linux/rmap.h> /* anon_vma_prepare */
31#include <linux/mmu_notifier.h> /* set_pte_at_notify */
32#include <linux/swap.h> /* try_to_free_swap */
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +053033#include <linux/ptrace.h> /* user_enable_single_step */
34#include <linux/kdebug.h> /* notifier mechanism */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010035
Srikar Dronamraju2b144492012-02-09 14:56:42 +053036#include <linux/uprobes.h>
37
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +053038#define UINSNS_PER_PAGE (PAGE_SIZE/UPROBE_XOL_SLOT_BYTES)
39#define MAX_UPROBE_XOL_SLOTS UINSNS_PER_PAGE
40
Srikar Dronamraju2b144492012-02-09 14:56:42 +053041static struct rb_root uprobes_tree = RB_ROOT;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010042
Srikar Dronamraju2b144492012-02-09 14:56:42 +053043static DEFINE_SPINLOCK(uprobes_treelock); /* serialize rbtree access */
44
45#define UPROBES_HASH_SZ 13
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010046
Peter Zijlstrac5784de2012-06-15 17:43:39 +020047/*
48 * We need separate register/unregister and mmap/munmap lock hashes because
49 * of mmap_sem nesting.
50 *
51 * uprobe_register() needs to install probes on (potentially) all processes
52 * and thus needs to acquire multiple mmap_sems (consequtively, not
53 * concurrently), whereas uprobe_mmap() is called while holding mmap_sem
54 * for the particular process doing the mmap.
55 *
56 * uprobe_register()->register_for_each_vma() needs to drop/acquire mmap_sem
57 * because of lock order against i_mmap_mutex. This means there's a hole in
58 * the register vma iteration where a mmap() can happen.
59 *
60 * Thus uprobe_register() can race with uprobe_mmap() and we can try and
61 * install a probe where one is already installed.
62 */
63
Srikar Dronamraju2b144492012-02-09 14:56:42 +053064/* serialize (un)register */
65static struct mutex uprobes_mutex[UPROBES_HASH_SZ];
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010066
67#define uprobes_hash(v) (&uprobes_mutex[((unsigned long)(v)) % UPROBES_HASH_SZ])
Srikar Dronamraju2b144492012-02-09 14:56:42 +053068
69/* serialize uprobe->pending_list */
70static struct mutex uprobes_mmap_mutex[UPROBES_HASH_SZ];
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010071#define uprobes_mmap_hash(v) (&uprobes_mmap_mutex[((unsigned long)(v)) % UPROBES_HASH_SZ])
Srikar Dronamraju2b144492012-02-09 14:56:42 +053072
73/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010074 * uprobe_events allows us to skip the uprobe_mmap if there are no uprobe
Srikar Dronamraju2b144492012-02-09 14:56:42 +053075 * events active at this time. Probably a fine grained per inode count is
76 * better?
77 */
78static atomic_t uprobe_events = ATOMIC_INIT(0);
79
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +053080struct uprobe {
81 struct rb_node rb_node; /* node in the rb tree */
82 atomic_t ref;
83 struct rw_semaphore consumer_rwsem;
84 struct list_head pending_list;
85 struct uprobe_consumer *consumers;
86 struct inode *inode; /* Also hold a ref to inode */
87 loff_t offset;
88 int flags;
89 struct arch_uprobe arch;
90};
91
Srikar Dronamraju2b144492012-02-09 14:56:42 +053092/*
93 * valid_vma: Verify if the specified vma is an executable vma
94 * Relax restrictions while unregistering: vm_flags might have
95 * changed after breakpoint was inserted.
96 * - is_register: indicates if we are in register context.
97 * - Return 1 if the specified virtual address is in an
98 * executable vma.
99 */
100static bool valid_vma(struct vm_area_struct *vma, bool is_register)
101{
102 if (!vma->vm_file)
103 return false;
104
105 if (!is_register)
106 return true;
107
Oleg Nesterovea131372012-06-15 17:43:22 +0200108 if ((vma->vm_flags & (VM_HUGETLB|VM_READ|VM_WRITE|VM_EXEC|VM_SHARED))
109 == (VM_READ|VM_EXEC))
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530110 return true;
111
112 return false;
113}
114
115static loff_t vma_address(struct vm_area_struct *vma, loff_t offset)
116{
117 loff_t vaddr;
118
119 vaddr = vma->vm_start + offset;
Oleg Nesterovaefd8932012-07-29 20:22:33 +0200120 vaddr -= (loff_t)vma->vm_pgoff << PAGE_SHIFT;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100121
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530122 return vaddr;
123}
124
Oleg Nesterovcb113b42012-07-29 20:22:42 +0200125static loff_t vaddr_to_offset(struct vm_area_struct *vma, unsigned long vaddr)
126{
127 return ((loff_t)vma->vm_pgoff << PAGE_SHIFT) + (vaddr - vma->vm_start);
128}
129
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530130/**
131 * __replace_page - replace page in vma by new page.
132 * based on replace_page in mm/ksm.c
133 *
134 * @vma: vma that holds the pte pointing to page
Oleg Nesterovc517ee72012-07-29 20:22:16 +0200135 * @addr: address the old @page is mapped at
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530136 * @page: the cowed page we are replacing by kpage
137 * @kpage: the modified page we replace page by
138 *
139 * Returns 0 on success, -EFAULT on failure.
140 */
Oleg Nesterovc517ee72012-07-29 20:22:16 +0200141static int __replace_page(struct vm_area_struct *vma, unsigned long addr,
142 struct page *page, struct page *kpage)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530143{
144 struct mm_struct *mm = vma->vm_mm;
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200145 spinlock_t *ptl;
146 pte_t *ptep;
Oleg Nesterov9f924482012-07-29 20:22:20 +0200147 int err;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530148
Oleg Nesterov9f924482012-07-29 20:22:20 +0200149 /* freeze PageSwapCache() for try_to_free_swap() below */
150 lock_page(page);
151
152 err = -EAGAIN;
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200153 ptep = page_check_address(page, mm, addr, &ptl, 0);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530154 if (!ptep)
Oleg Nesterov9f924482012-07-29 20:22:20 +0200155 goto unlock;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530156
157 get_page(kpage);
158 page_add_new_anon_rmap(kpage, vma, addr);
159
Srikar Dronamraju7396fa82012-04-11 16:05:16 +0530160 if (!PageAnon(page)) {
161 dec_mm_counter(mm, MM_FILEPAGES);
162 inc_mm_counter(mm, MM_ANONPAGES);
163 }
164
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530165 flush_cache_page(vma, addr, pte_pfn(*ptep));
166 ptep_clear_flush(vma, addr, ptep);
167 set_pte_at_notify(mm, addr, ptep, mk_pte(kpage, vma->vm_page_prot));
168
169 page_remove_rmap(page);
170 if (!page_mapped(page))
171 try_to_free_swap(page);
172 put_page(page);
173 pte_unmap_unlock(ptep, ptl);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530174
Oleg Nesterov9f924482012-07-29 20:22:20 +0200175 err = 0;
176 unlock:
177 unlock_page(page);
178 return err;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530179}
180
181/**
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530182 * is_swbp_insn - check if instruction is breakpoint instruction.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530183 * @insn: instruction to be checked.
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530184 * Default implementation of is_swbp_insn
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530185 * Returns true if @insn is a breakpoint instruction.
186 */
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530187bool __weak is_swbp_insn(uprobe_opcode_t *insn)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530188{
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530189 return *insn == UPROBE_SWBP_INSN;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530190}
191
192/*
193 * NOTE:
194 * Expect the breakpoint instruction to be the smallest size instruction for
195 * the architecture. If an arch has variable length instruction and the
196 * breakpoint instruction is not of the smallest length instruction
197 * supported by that architecture then we need to modify read_opcode /
198 * write_opcode accordingly. This would never be a problem for archs that
199 * have fixed length instructions.
200 */
201
202/*
203 * write_opcode - write the opcode at a given virtual address.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530204 * @auprobe: arch breakpointing information.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530205 * @mm: the probed process address space.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530206 * @vaddr: the virtual address to store the opcode.
207 * @opcode: opcode to be written at @vaddr.
208 *
209 * Called with mm->mmap_sem held (for read and with a reference to
210 * mm).
211 *
212 * For mm @mm, write the opcode at @vaddr.
213 * Return 0 (success) or a negative errno.
214 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530215static int write_opcode(struct arch_uprobe *auprobe, struct mm_struct *mm,
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530216 unsigned long vaddr, uprobe_opcode_t opcode)
217{
218 struct page *old_page, *new_page;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530219 void *vaddr_old, *vaddr_new;
220 struct vm_area_struct *vma;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530221 int ret;
Oleg Nesterovf4030722012-07-29 20:22:12 +0200222
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200223retry:
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530224 /* Read the page with vaddr into memory */
225 ret = get_user_pages(NULL, mm, vaddr, 1, 0, 0, &old_page, &vma);
226 if (ret <= 0)
227 return ret;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100228
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530229 ret = -ENOMEM;
230 new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vaddr);
231 if (!new_page)
Oleg Nesterov9f924482012-07-29 20:22:20 +0200232 goto put_old;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530233
234 __SetPageUptodate(new_page);
235
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530236 /* copy the page now that we've got it stable */
237 vaddr_old = kmap_atomic(old_page);
238 vaddr_new = kmap_atomic(new_page);
239
240 memcpy(vaddr_new, vaddr_old, PAGE_SIZE);
Oleg Nesterovd9c4a302012-06-15 17:43:50 +0200241 memcpy(vaddr_new + (vaddr & ~PAGE_MASK), &opcode, UPROBE_SWBP_INSN_SIZE);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530242
243 kunmap_atomic(vaddr_new);
244 kunmap_atomic(vaddr_old);
245
246 ret = anon_vma_prepare(vma);
247 if (ret)
Oleg Nesterov9f924482012-07-29 20:22:20 +0200248 goto put_new;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530249
Oleg Nesterovc517ee72012-07-29 20:22:16 +0200250 ret = __replace_page(vma, vaddr, old_page, new_page);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530251
Oleg Nesterov9f924482012-07-29 20:22:20 +0200252put_new:
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530253 page_cache_release(new_page);
Oleg Nesterov9f924482012-07-29 20:22:20 +0200254put_old:
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100255 put_page(old_page);
256
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200257 if (unlikely(ret == -EAGAIN))
258 goto retry;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530259 return ret;
260}
261
262/**
263 * read_opcode - read the opcode at a given virtual address.
264 * @mm: the probed process address space.
265 * @vaddr: the virtual address to read the opcode.
266 * @opcode: location to store the read opcode.
267 *
268 * Called with mm->mmap_sem held (for read and with a reference to
269 * mm.
270 *
271 * For mm @mm, read the opcode at @vaddr and store it in @opcode.
272 * Return 0 (success) or a negative errno.
273 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100274static int read_opcode(struct mm_struct *mm, unsigned long vaddr, uprobe_opcode_t *opcode)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530275{
276 struct page *page;
277 void *vaddr_new;
278 int ret;
279
Oleg Nesterova3d7bb42012-05-29 21:27:59 +0200280 ret = get_user_pages(NULL, mm, vaddr, 1, 0, 1, &page, NULL);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530281 if (ret <= 0)
282 return ret;
283
284 lock_page(page);
285 vaddr_new = kmap_atomic(page);
286 vaddr &= ~PAGE_MASK;
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530287 memcpy(opcode, vaddr_new + vaddr, UPROBE_SWBP_INSN_SIZE);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530288 kunmap_atomic(vaddr_new);
289 unlock_page(page);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100290
291 put_page(page);
292
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530293 return 0;
294}
295
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530296static int is_swbp_at_addr(struct mm_struct *mm, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530297{
298 uprobe_opcode_t opcode;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100299 int result;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530300
Oleg Nesterovc00b2752012-05-29 21:27:44 +0200301 if (current->mm == mm) {
302 pagefault_disable();
303 result = __copy_from_user_inatomic(&opcode, (void __user*)vaddr,
304 sizeof(opcode));
305 pagefault_enable();
306
307 if (likely(result == 0))
308 goto out;
309 }
310
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100311 result = read_opcode(mm, vaddr, &opcode);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530312 if (result)
313 return result;
Oleg Nesterovc00b2752012-05-29 21:27:44 +0200314out:
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530315 if (is_swbp_insn(&opcode))
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530316 return 1;
317
318 return 0;
319}
320
321/**
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530322 * set_swbp - store breakpoint at a given address.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530323 * @auprobe: arch specific probepoint information.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530324 * @mm: the probed process address space.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530325 * @vaddr: the virtual address to insert the opcode.
326 *
327 * For mm @mm, store the breakpoint instruction at @vaddr.
328 * Return 0 (success) or a negative errno.
329 */
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530330int __weak set_swbp(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530331{
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100332 int result;
Peter Zijlstrac5784de2012-06-15 17:43:39 +0200333 /*
334 * See the comment near uprobes_hash().
335 */
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530336 result = is_swbp_at_addr(mm, vaddr);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530337 if (result == 1)
338 return -EEXIST;
339
340 if (result)
341 return result;
342
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530343 return write_opcode(auprobe, mm, vaddr, UPROBE_SWBP_INSN);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530344}
345
346/**
347 * set_orig_insn - Restore the original instruction.
348 * @mm: the probed process address space.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530349 * @auprobe: arch specific probepoint information.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530350 * @vaddr: the virtual address to insert the opcode.
351 * @verify: if true, verify existance of breakpoint instruction.
352 *
353 * For mm @mm, restore the original opcode (opcode) at @vaddr.
354 * Return 0 (success) or a negative errno.
355 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100356int __weak
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530357set_orig_insn(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr, bool verify)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530358{
359 if (verify) {
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100360 int result;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530361
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530362 result = is_swbp_at_addr(mm, vaddr);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530363 if (!result)
364 return -EINVAL;
365
366 if (result != 1)
367 return result;
368 }
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530369 return write_opcode(auprobe, mm, vaddr, *(uprobe_opcode_t *)auprobe->insn);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530370}
371
372static int match_uprobe(struct uprobe *l, struct uprobe *r)
373{
374 if (l->inode < r->inode)
375 return -1;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100376
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530377 if (l->inode > r->inode)
378 return 1;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530379
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100380 if (l->offset < r->offset)
381 return -1;
382
383 if (l->offset > r->offset)
384 return 1;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530385
386 return 0;
387}
388
389static struct uprobe *__find_uprobe(struct inode *inode, loff_t offset)
390{
391 struct uprobe u = { .inode = inode, .offset = offset };
392 struct rb_node *n = uprobes_tree.rb_node;
393 struct uprobe *uprobe;
394 int match;
395
396 while (n) {
397 uprobe = rb_entry(n, struct uprobe, rb_node);
398 match = match_uprobe(&u, uprobe);
399 if (!match) {
400 atomic_inc(&uprobe->ref);
401 return uprobe;
402 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100403
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530404 if (match < 0)
405 n = n->rb_left;
406 else
407 n = n->rb_right;
408 }
409 return NULL;
410}
411
412/*
413 * Find a uprobe corresponding to a given inode:offset
414 * Acquires uprobes_treelock
415 */
416static struct uprobe *find_uprobe(struct inode *inode, loff_t offset)
417{
418 struct uprobe *uprobe;
419 unsigned long flags;
420
421 spin_lock_irqsave(&uprobes_treelock, flags);
422 uprobe = __find_uprobe(inode, offset);
423 spin_unlock_irqrestore(&uprobes_treelock, flags);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100424
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530425 return uprobe;
426}
427
428static struct uprobe *__insert_uprobe(struct uprobe *uprobe)
429{
430 struct rb_node **p = &uprobes_tree.rb_node;
431 struct rb_node *parent = NULL;
432 struct uprobe *u;
433 int match;
434
435 while (*p) {
436 parent = *p;
437 u = rb_entry(parent, struct uprobe, rb_node);
438 match = match_uprobe(uprobe, u);
439 if (!match) {
440 atomic_inc(&u->ref);
441 return u;
442 }
443
444 if (match < 0)
445 p = &parent->rb_left;
446 else
447 p = &parent->rb_right;
448
449 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100450
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530451 u = NULL;
452 rb_link_node(&uprobe->rb_node, parent, p);
453 rb_insert_color(&uprobe->rb_node, &uprobes_tree);
454 /* get access + creation ref */
455 atomic_set(&uprobe->ref, 2);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100456
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530457 return u;
458}
459
460/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100461 * Acquire uprobes_treelock.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530462 * Matching uprobe already exists in rbtree;
463 * increment (access refcount) and return the matching uprobe.
464 *
465 * No matching uprobe; insert the uprobe in rb_tree;
466 * get a double refcount (access + creation) and return NULL.
467 */
468static struct uprobe *insert_uprobe(struct uprobe *uprobe)
469{
470 unsigned long flags;
471 struct uprobe *u;
472
473 spin_lock_irqsave(&uprobes_treelock, flags);
474 u = __insert_uprobe(uprobe);
475 spin_unlock_irqrestore(&uprobes_treelock, flags);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100476
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530477 /* For now assume that the instruction need not be single-stepped */
478 uprobe->flags |= UPROBE_SKIP_SSTEP;
479
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530480 return u;
481}
482
483static void put_uprobe(struct uprobe *uprobe)
484{
485 if (atomic_dec_and_test(&uprobe->ref))
486 kfree(uprobe);
487}
488
489static struct uprobe *alloc_uprobe(struct inode *inode, loff_t offset)
490{
491 struct uprobe *uprobe, *cur_uprobe;
492
493 uprobe = kzalloc(sizeof(struct uprobe), GFP_KERNEL);
494 if (!uprobe)
495 return NULL;
496
497 uprobe->inode = igrab(inode);
498 uprobe->offset = offset;
499 init_rwsem(&uprobe->consumer_rwsem);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530500
501 /* add to uprobes_tree, sorted on inode:offset */
502 cur_uprobe = insert_uprobe(uprobe);
503
504 /* a uprobe exists for this inode:offset combination */
505 if (cur_uprobe) {
506 kfree(uprobe);
507 uprobe = cur_uprobe;
508 iput(inode);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100509 } else {
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530510 atomic_inc(&uprobe_events);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100511 }
512
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530513 return uprobe;
514}
515
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530516static void handler_chain(struct uprobe *uprobe, struct pt_regs *regs)
517{
518 struct uprobe_consumer *uc;
519
520 if (!(uprobe->flags & UPROBE_RUN_HANDLER))
521 return;
522
523 down_read(&uprobe->consumer_rwsem);
524 for (uc = uprobe->consumers; uc; uc = uc->next) {
525 if (!uc->filter || uc->filter(uc, current))
526 uc->handler(uc, regs);
527 }
528 up_read(&uprobe->consumer_rwsem);
529}
530
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530531/* Returns the previous consumer */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100532static struct uprobe_consumer *
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530533consumer_add(struct uprobe *uprobe, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530534{
535 down_write(&uprobe->consumer_rwsem);
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530536 uc->next = uprobe->consumers;
537 uprobe->consumers = uc;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530538 up_write(&uprobe->consumer_rwsem);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100539
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530540 return uc->next;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530541}
542
543/*
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530544 * For uprobe @uprobe, delete the consumer @uc.
545 * Return true if the @uc is deleted successfully
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530546 * or return false.
547 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530548static bool consumer_del(struct uprobe *uprobe, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530549{
550 struct uprobe_consumer **con;
551 bool ret = false;
552
553 down_write(&uprobe->consumer_rwsem);
554 for (con = &uprobe->consumers; *con; con = &(*con)->next) {
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530555 if (*con == uc) {
556 *con = uc->next;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530557 ret = true;
558 break;
559 }
560 }
561 up_write(&uprobe->consumer_rwsem);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100562
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530563 return ret;
564}
565
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530566static int
Oleg Nesterovd4366152012-06-15 17:43:42 +0200567__copy_insn(struct address_space *mapping, struct file *filp, char *insn,
Oleg Nesterov593609a2012-06-15 17:43:59 +0200568 unsigned long nbytes, loff_t offset)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530569{
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530570 struct page *page;
571 void *vaddr;
Oleg Nesterov593609a2012-06-15 17:43:59 +0200572 unsigned long off;
573 pgoff_t idx;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530574
575 if (!filp)
576 return -EINVAL;
577
Oleg Nesterovcc359d12012-06-15 17:43:25 +0200578 if (!mapping->a_ops->readpage)
579 return -EIO;
580
Oleg Nesterov593609a2012-06-15 17:43:59 +0200581 idx = offset >> PAGE_CACHE_SHIFT;
582 off = offset & ~PAGE_MASK;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530583
584 /*
585 * Ensure that the page that has the original instruction is
586 * populated and in page-cache.
587 */
588 page = read_mapping_page(mapping, idx, filp);
589 if (IS_ERR(page))
590 return PTR_ERR(page);
591
592 vaddr = kmap_atomic(page);
Oleg Nesterov593609a2012-06-15 17:43:59 +0200593 memcpy(insn, vaddr + off, nbytes);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530594 kunmap_atomic(vaddr);
595 page_cache_release(page);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100596
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530597 return 0;
598}
599
Oleg Nesterovd4366152012-06-15 17:43:42 +0200600static int copy_insn(struct uprobe *uprobe, struct file *filp)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530601{
602 struct address_space *mapping;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530603 unsigned long nbytes;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100604 int bytes;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530605
Oleg Nesterovd4366152012-06-15 17:43:42 +0200606 nbytes = PAGE_SIZE - (uprobe->offset & ~PAGE_MASK);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530607 mapping = uprobe->inode->i_mapping;
608
609 /* Instruction at end of binary; copy only available bytes */
610 if (uprobe->offset + MAX_UINSN_BYTES > uprobe->inode->i_size)
611 bytes = uprobe->inode->i_size - uprobe->offset;
612 else
613 bytes = MAX_UINSN_BYTES;
614
615 /* Instruction at the page-boundary; copy bytes in second page */
616 if (nbytes < bytes) {
Oleg Nesterovfc36f592012-06-15 17:43:44 +0200617 int err = __copy_insn(mapping, filp, uprobe->arch.insn + nbytes,
618 bytes - nbytes, uprobe->offset + nbytes);
619 if (err)
620 return err;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530621 bytes = nbytes;
622 }
Oleg Nesterovd4366152012-06-15 17:43:42 +0200623 return __copy_insn(mapping, filp, uprobe->arch.insn, bytes, uprobe->offset);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530624}
625
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530626/*
627 * How mm->uprobes_state.count gets updated
628 * uprobe_mmap() increments the count if
629 * - it successfully adds a breakpoint.
630 * - it cannot add a breakpoint, but sees that there is a underlying
631 * breakpoint (via a is_swbp_at_addr()).
632 *
633 * uprobe_munmap() decrements the count if
634 * - it sees a underlying breakpoint, (via is_swbp_at_addr)
635 * (Subsequent uprobe_unregister wouldnt find the breakpoint
636 * unless a uprobe_mmap kicks in, since the old vma would be
637 * dropped just after uprobe_munmap.)
638 *
639 * uprobe_register increments the count if:
640 * - it successfully adds a breakpoint.
641 *
642 * uprobe_unregister decrements the count if:
643 * - it sees a underlying breakpoint and removes successfully.
644 * (via is_swbp_at_addr)
645 * (Subsequent uprobe_munmap wouldnt find the breakpoint
646 * since there is no underlying breakpoint after the
647 * breakpoint removal.)
648 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530649static int
650install_breakpoint(struct uprobe *uprobe, struct mm_struct *mm,
Oleg Nesterov816c03f2012-06-15 17:43:55 +0200651 struct vm_area_struct *vma, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530652{
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530653 int ret;
654
655 /*
656 * If probe is being deleted, unregister thread could be done with
657 * the vma-rmap-walk through. Adding a probe now can be fatal since
658 * nobody will be able to cleanup. Also we could be from fork or
659 * mremap path, where the probe might have already been inserted.
660 * Hence behave as if probe already existed.
661 */
662 if (!uprobe->consumers)
663 return -EEXIST;
664
Srikar Dronamraju900771a2012-03-12 14:55:14 +0530665 if (!(uprobe->flags & UPROBE_COPY_INSN)) {
Oleg Nesterovd4366152012-06-15 17:43:42 +0200666 ret = copy_insn(uprobe, vma->vm_file);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530667 if (ret)
668 return ret;
669
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530670 if (is_swbp_insn((uprobe_opcode_t *)uprobe->arch.insn))
Oleg Nesterovc1914a02012-06-15 17:43:31 +0200671 return -ENOTSUPP;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530672
Oleg Nesterov816c03f2012-06-15 17:43:55 +0200673 ret = arch_uprobe_analyze_insn(&uprobe->arch, mm, vaddr);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530674 if (ret)
675 return ret;
676
Oleg Nesterovd9c4a302012-06-15 17:43:50 +0200677 /* write_opcode() assumes we don't cross page boundary */
678 BUG_ON((uprobe->offset & ~PAGE_MASK) +
679 UPROBE_SWBP_INSN_SIZE > PAGE_SIZE);
680
Srikar Dronamraju900771a2012-03-12 14:55:14 +0530681 uprobe->flags |= UPROBE_COPY_INSN;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530682 }
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530683
684 /*
685 * Ideally, should be updating the probe count after the breakpoint
686 * has been successfully inserted. However a thread could hit the
687 * breakpoint we just inserted even before the probe count is
688 * incremented. If this is the first breakpoint placed, breakpoint
689 * notifier might ignore uprobes and pass the trap to the thread.
690 * Hence increment before and decrement on failure.
691 */
692 atomic_inc(&mm->uprobes_state.count);
Oleg Nesterov816c03f2012-06-15 17:43:55 +0200693 ret = set_swbp(&uprobe->arch, mm, vaddr);
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530694 if (ret)
695 atomic_dec(&mm->uprobes_state.count);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530696
697 return ret;
698}
699
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530700static void
Oleg Nesterov816c03f2012-06-15 17:43:55 +0200701remove_breakpoint(struct uprobe *uprobe, struct mm_struct *mm, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530702{
Oleg Nesterov816c03f2012-06-15 17:43:55 +0200703 if (!set_orig_insn(&uprobe->arch, mm, vaddr, true))
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530704 atomic_dec(&mm->uprobes_state.count);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530705}
706
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530707/*
Oleg Nesterov778b0322012-05-29 21:30:08 +0200708 * There could be threads that have already hit the breakpoint. They
709 * will recheck the current insn and restart if find_uprobe() fails.
710 * See find_active_uprobe().
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530711 */
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530712static void delete_uprobe(struct uprobe *uprobe)
713{
714 unsigned long flags;
715
716 spin_lock_irqsave(&uprobes_treelock, flags);
717 rb_erase(&uprobe->rb_node, &uprobes_tree);
718 spin_unlock_irqrestore(&uprobes_treelock, flags);
719 iput(uprobe->inode);
720 put_uprobe(uprobe);
721 atomic_dec(&uprobe_events);
722}
723
Oleg Nesterov26872092012-06-15 17:43:33 +0200724struct map_info {
725 struct map_info *next;
726 struct mm_struct *mm;
Oleg Nesterov816c03f2012-06-15 17:43:55 +0200727 unsigned long vaddr;
Oleg Nesterov26872092012-06-15 17:43:33 +0200728};
729
730static inline struct map_info *free_map_info(struct map_info *info)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530731{
Oleg Nesterov26872092012-06-15 17:43:33 +0200732 struct map_info *next = info->next;
733 kfree(info);
734 return next;
735}
736
737static struct map_info *
738build_map_info(struct address_space *mapping, loff_t offset, bool is_register)
739{
740 unsigned long pgoff = offset >> PAGE_SHIFT;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530741 struct prio_tree_iter iter;
742 struct vm_area_struct *vma;
Oleg Nesterov26872092012-06-15 17:43:33 +0200743 struct map_info *curr = NULL;
744 struct map_info *prev = NULL;
745 struct map_info *info;
746 int more = 0;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100747
Oleg Nesterov26872092012-06-15 17:43:33 +0200748 again:
749 mutex_lock(&mapping->i_mmap_mutex);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530750 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
751 if (!valid_vma(vma, is_register))
752 continue;
753
Oleg Nesterov7a5bfb62012-06-15 17:43:36 +0200754 if (!prev && !more) {
755 /*
756 * Needs GFP_NOWAIT to avoid i_mmap_mutex recursion through
757 * reclaim. This is optimistic, no harm done if it fails.
758 */
759 prev = kmalloc(sizeof(struct map_info),
760 GFP_NOWAIT | __GFP_NOMEMALLOC | __GFP_NOWARN);
761 if (prev)
762 prev->next = NULL;
763 }
Oleg Nesterov26872092012-06-15 17:43:33 +0200764 if (!prev) {
765 more++;
766 continue;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530767 }
768
Oleg Nesterov26872092012-06-15 17:43:33 +0200769 if (!atomic_inc_not_zero(&vma->vm_mm->mm_users))
770 continue;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100771
Oleg Nesterov26872092012-06-15 17:43:33 +0200772 info = prev;
773 prev = prev->next;
774 info->next = curr;
775 curr = info;
776
777 info->mm = vma->vm_mm;
778 info->vaddr = vma_address(vma, offset);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530779 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530780 mutex_unlock(&mapping->i_mmap_mutex);
781
Oleg Nesterov26872092012-06-15 17:43:33 +0200782 if (!more)
783 goto out;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100784
Oleg Nesterov26872092012-06-15 17:43:33 +0200785 prev = curr;
786 while (curr) {
787 mmput(curr->mm);
788 curr = curr->next;
789 }
790
791 do {
792 info = kmalloc(sizeof(struct map_info), GFP_KERNEL);
793 if (!info) {
794 curr = ERR_PTR(-ENOMEM);
795 goto out;
796 }
797 info->next = prev;
798 prev = info;
799 } while (--more);
800
801 goto again;
802 out:
803 while (prev)
804 prev = free_map_info(prev);
805 return curr;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530806}
807
808static int register_for_each_vma(struct uprobe *uprobe, bool is_register)
809{
Oleg Nesterov26872092012-06-15 17:43:33 +0200810 struct map_info *info;
811 int err = 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530812
Oleg Nesterov26872092012-06-15 17:43:33 +0200813 info = build_map_info(uprobe->inode->i_mapping,
814 uprobe->offset, is_register);
815 if (IS_ERR(info))
816 return PTR_ERR(info);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100817
Oleg Nesterov26872092012-06-15 17:43:33 +0200818 while (info) {
819 struct mm_struct *mm = info->mm;
820 struct vm_area_struct *vma;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100821
Oleg Nesterov26872092012-06-15 17:43:33 +0200822 if (err)
823 goto free;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100824
Oleg Nesterov77fc4af2012-05-29 21:29:28 +0200825 down_write(&mm->mmap_sem);
Oleg Nesterovf4d6dfe2012-07-29 20:22:44 +0200826 vma = find_vma(mm, info->vaddr);
827 if (!vma || !valid_vma(vma, is_register) ||
828 vma->vm_file->f_mapping->host != uprobe->inode)
Oleg Nesterov26872092012-06-15 17:43:33 +0200829 goto unlock;
830
Oleg Nesterovf4d6dfe2012-07-29 20:22:44 +0200831 if (vma->vm_start > info->vaddr ||
832 vaddr_to_offset(vma, info->vaddr) != uprobe->offset)
Oleg Nesterov26872092012-06-15 17:43:33 +0200833 goto unlock;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530834
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530835 if (is_register) {
Oleg Nesterov26872092012-06-15 17:43:33 +0200836 err = install_breakpoint(uprobe, mm, vma, info->vaddr);
Peter Zijlstrac5784de2012-06-15 17:43:39 +0200837 /*
838 * We can race against uprobe_mmap(), see the
839 * comment near uprobe_hash().
840 */
Oleg Nesterov26872092012-06-15 17:43:33 +0200841 if (err == -EEXIST)
842 err = 0;
843 } else {
844 remove_breakpoint(uprobe, mm, info->vaddr);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530845 }
Oleg Nesterov26872092012-06-15 17:43:33 +0200846 unlock:
847 up_write(&mm->mmap_sem);
848 free:
849 mmput(mm);
850 info = free_map_info(info);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530851 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100852
Oleg Nesterov26872092012-06-15 17:43:33 +0200853 return err;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530854}
855
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100856static int __uprobe_register(struct uprobe *uprobe)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530857{
858 return register_for_each_vma(uprobe, true);
859}
860
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100861static void __uprobe_unregister(struct uprobe *uprobe)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530862{
863 if (!register_for_each_vma(uprobe, false))
864 delete_uprobe(uprobe);
865
866 /* TODO : cant unregister? schedule a worker thread */
867}
868
869/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100870 * uprobe_register - register a probe
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530871 * @inode: the file in which the probe has to be placed.
872 * @offset: offset from the start of the file.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530873 * @uc: information on howto handle the probe..
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530874 *
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100875 * Apart from the access refcount, uprobe_register() takes a creation
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530876 * refcount (thro alloc_uprobe) if and only if this @uprobe is getting
877 * inserted into the rbtree (i.e first consumer for a @inode:@offset
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100878 * tuple). Creation refcount stops uprobe_unregister from freeing the
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530879 * @uprobe even before the register operation is complete. Creation
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530880 * refcount is released when the last @uc for the @uprobe
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530881 * unregisters.
882 *
883 * Return errno if it cannot successully install probes
884 * else return 0 (success)
885 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530886int uprobe_register(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530887{
888 struct uprobe *uprobe;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100889 int ret;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530890
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530891 if (!inode || !uc || uc->next)
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100892 return -EINVAL;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530893
894 if (offset > i_size_read(inode))
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100895 return -EINVAL;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530896
897 ret = 0;
898 mutex_lock(uprobes_hash(inode));
899 uprobe = alloc_uprobe(inode, offset);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100900
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530901 if (uprobe && !consumer_add(uprobe, uc)) {
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100902 ret = __uprobe_register(uprobe);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530903 if (ret) {
904 uprobe->consumers = NULL;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100905 __uprobe_unregister(uprobe);
906 } else {
Srikar Dronamraju900771a2012-03-12 14:55:14 +0530907 uprobe->flags |= UPROBE_RUN_HANDLER;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100908 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530909 }
910
911 mutex_unlock(uprobes_hash(inode));
912 put_uprobe(uprobe);
913
914 return ret;
915}
916
917/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100918 * uprobe_unregister - unregister a already registered probe.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530919 * @inode: the file in which the probe has to be removed.
920 * @offset: offset from the start of the file.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530921 * @uc: identify which probe if multiple probes are colocated.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530922 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530923void uprobe_unregister(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530924{
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100925 struct uprobe *uprobe;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530926
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530927 if (!inode || !uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530928 return;
929
930 uprobe = find_uprobe(inode, offset);
931 if (!uprobe)
932 return;
933
934 mutex_lock(uprobes_hash(inode));
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530935
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530936 if (consumer_del(uprobe, uc)) {
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100937 if (!uprobe->consumers) {
938 __uprobe_unregister(uprobe);
Srikar Dronamraju900771a2012-03-12 14:55:14 +0530939 uprobe->flags &= ~UPROBE_RUN_HANDLER;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100940 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530941 }
942
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530943 mutex_unlock(uprobes_hash(inode));
944 if (uprobe)
945 put_uprobe(uprobe);
946}
947
Oleg Nesterov891c3972012-07-29 20:22:40 +0200948static struct rb_node *
949find_node_in_range(struct inode *inode, loff_t min, loff_t max)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530950{
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530951 struct rb_node *n = uprobes_tree.rb_node;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530952
953 while (n) {
Oleg Nesterov891c3972012-07-29 20:22:40 +0200954 struct uprobe *u = rb_entry(n, struct uprobe, rb_node);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100955
Oleg Nesterov891c3972012-07-29 20:22:40 +0200956 if (inode < u->inode) {
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530957 n = n->rb_left;
Oleg Nesterov891c3972012-07-29 20:22:40 +0200958 } else if (inode > u->inode) {
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530959 n = n->rb_right;
Oleg Nesterov891c3972012-07-29 20:22:40 +0200960 } else {
961 if (max < u->offset)
962 n = n->rb_left;
963 else if (min > u->offset)
964 n = n->rb_right;
965 else
966 break;
967 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530968 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100969
Oleg Nesterov891c3972012-07-29 20:22:40 +0200970 return n;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530971}
972
973/*
Oleg Nesterov891c3972012-07-29 20:22:40 +0200974 * For a given range in vma, build a list of probes that need to be inserted.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530975 */
Oleg Nesterov891c3972012-07-29 20:22:40 +0200976static void build_probe_list(struct inode *inode,
977 struct vm_area_struct *vma,
978 unsigned long start, unsigned long end,
979 struct list_head *head)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530980{
Oleg Nesterov891c3972012-07-29 20:22:40 +0200981 loff_t min, max;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530982 unsigned long flags;
Oleg Nesterov891c3972012-07-29 20:22:40 +0200983 struct rb_node *n, *t;
984 struct uprobe *u;
985
986 INIT_LIST_HEAD(head);
Oleg Nesterovcb113b42012-07-29 20:22:42 +0200987 min = vaddr_to_offset(vma, start);
Oleg Nesterov891c3972012-07-29 20:22:40 +0200988 max = min + (end - start) - 1;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530989
990 spin_lock_irqsave(&uprobes_treelock, flags);
Oleg Nesterov891c3972012-07-29 20:22:40 +0200991 n = find_node_in_range(inode, min, max);
992 if (n) {
993 for (t = n; t; t = rb_prev(t)) {
994 u = rb_entry(t, struct uprobe, rb_node);
995 if (u->inode != inode || u->offset < min)
996 break;
997 list_add(&u->pending_list, head);
998 atomic_inc(&u->ref);
999 }
1000 for (t = n; (t = rb_next(t)); ) {
1001 u = rb_entry(t, struct uprobe, rb_node);
1002 if (u->inode != inode || u->offset > max)
1003 break;
1004 list_add(&u->pending_list, head);
1005 atomic_inc(&u->ref);
1006 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301007 }
1008 spin_unlock_irqrestore(&uprobes_treelock, flags);
1009}
1010
1011/*
1012 * Called from mmap_region.
1013 * called with mm->mmap_sem acquired.
1014 *
1015 * Return -ve no if we fail to insert probes and we cannot
1016 * bail-out.
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001017 * Return 0 otherwise. i.e:
1018 *
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301019 * - successful insertion of probes
1020 * - (or) no possible probes to be inserted.
1021 * - (or) insertion of probes failed but we can bail-out.
1022 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001023int uprobe_mmap(struct vm_area_struct *vma)
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301024{
1025 struct list_head tmp_list;
Oleg Nesterov665605a2012-07-29 20:22:29 +02001026 struct uprobe *uprobe, *u;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301027 struct inode *inode;
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301028 int ret, count;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301029
1030 if (!atomic_read(&uprobe_events) || !valid_vma(vma, true))
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001031 return 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301032
1033 inode = vma->vm_file->f_mapping->host;
1034 if (!inode)
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001035 return 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301036
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301037 mutex_lock(uprobes_mmap_hash(inode));
Oleg Nesterov891c3972012-07-29 20:22:40 +02001038 build_probe_list(inode, vma, vma->vm_start, vma->vm_end, &tmp_list);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001039
1040 ret = 0;
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301041 count = 0;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01001042
Oleg Nesterov665605a2012-07-29 20:22:29 +02001043 list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301044 if (!ret) {
Oleg Nesterov816c03f2012-06-15 17:43:55 +02001045 loff_t vaddr = vma_address(vma, uprobe->offset);
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301046
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301047 ret = install_breakpoint(uprobe, vma->vm_mm, vma, vaddr);
Peter Zijlstrac5784de2012-06-15 17:43:39 +02001048 /*
1049 * We can race against uprobe_register(), see the
1050 * comment near uprobe_hash().
1051 */
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301052 if (ret == -EEXIST) {
1053 ret = 0;
1054
1055 if (!is_swbp_at_addr(vma->vm_mm, vaddr))
1056 continue;
1057
1058 /*
1059 * Unable to insert a breakpoint, but
1060 * breakpoint lies underneath. Increment the
1061 * probe count.
1062 */
1063 atomic_inc(&vma->vm_mm->uprobes_state.count);
1064 }
1065
1066 if (!ret)
1067 count++;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301068 }
1069 put_uprobe(uprobe);
1070 }
1071
1072 mutex_unlock(uprobes_mmap_hash(inode));
1073
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301074 if (ret)
1075 atomic_sub(count, &vma->vm_mm->uprobes_state.count);
1076
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301077 return ret;
1078}
1079
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301080/*
1081 * Called in context of a munmap of a vma.
1082 */
Srikar Dronamrajucbc91f72012-04-11 16:05:27 +05301083void uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end)
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301084{
1085 struct list_head tmp_list;
Oleg Nesterov665605a2012-07-29 20:22:29 +02001086 struct uprobe *uprobe, *u;
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301087 struct inode *inode;
1088
1089 if (!atomic_read(&uprobe_events) || !valid_vma(vma, false))
1090 return;
1091
Oleg Nesterov2fd611a2012-07-29 20:22:31 +02001092 if (!atomic_read(&vma->vm_mm->mm_users)) /* called by mmput() ? */
1093 return;
1094
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301095 if (!atomic_read(&vma->vm_mm->uprobes_state.count))
1096 return;
1097
1098 inode = vma->vm_file->f_mapping->host;
1099 if (!inode)
1100 return;
1101
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301102 mutex_lock(uprobes_mmap_hash(inode));
Oleg Nesterov891c3972012-07-29 20:22:40 +02001103 build_probe_list(inode, vma, start, end, &tmp_list);
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301104
Oleg Nesterov665605a2012-07-29 20:22:29 +02001105 list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
Oleg Nesterov816c03f2012-06-15 17:43:55 +02001106 loff_t vaddr = vma_address(vma, uprobe->offset);
Oleg Nesterov891c3972012-07-29 20:22:40 +02001107 /*
1108 * An unregister could have removed the probe before
1109 * unmap. So check before we decrement the count.
1110 */
1111 if (is_swbp_at_addr(vma->vm_mm, vaddr) == 1)
1112 atomic_dec(&vma->vm_mm->uprobes_state.count);
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301113 put_uprobe(uprobe);
1114 }
1115 mutex_unlock(uprobes_mmap_hash(inode));
1116}
1117
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301118/* Slot allocation for XOL */
1119static int xol_add_vma(struct xol_area *area)
1120{
1121 struct mm_struct *mm;
1122 int ret;
1123
1124 area->page = alloc_page(GFP_HIGHUSER);
1125 if (!area->page)
1126 return -ENOMEM;
1127
1128 ret = -EALREADY;
1129 mm = current->mm;
1130
1131 down_write(&mm->mmap_sem);
1132 if (mm->uprobes_state.xol_area)
1133 goto fail;
1134
1135 ret = -ENOMEM;
1136
1137 /* Try to map as high as possible, this is only a hint. */
1138 area->vaddr = get_unmapped_area(NULL, TASK_SIZE - PAGE_SIZE, PAGE_SIZE, 0, 0);
1139 if (area->vaddr & ~PAGE_MASK) {
1140 ret = area->vaddr;
1141 goto fail;
1142 }
1143
1144 ret = install_special_mapping(mm, area->vaddr, PAGE_SIZE,
1145 VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO, &area->page);
1146 if (ret)
1147 goto fail;
1148
1149 smp_wmb(); /* pairs with get_xol_area() */
1150 mm->uprobes_state.xol_area = area;
1151 ret = 0;
1152
1153fail:
1154 up_write(&mm->mmap_sem);
1155 if (ret)
1156 __free_page(area->page);
1157
1158 return ret;
1159}
1160
1161static struct xol_area *get_xol_area(struct mm_struct *mm)
1162{
1163 struct xol_area *area;
1164
1165 area = mm->uprobes_state.xol_area;
1166 smp_read_barrier_depends(); /* pairs with wmb in xol_add_vma() */
1167
1168 return area;
1169}
1170
1171/*
1172 * xol_alloc_area - Allocate process's xol_area.
1173 * This area will be used for storing instructions for execution out of
1174 * line.
1175 *
1176 * Returns the allocated area or NULL.
1177 */
1178static struct xol_area *xol_alloc_area(void)
1179{
1180 struct xol_area *area;
1181
1182 area = kzalloc(sizeof(*area), GFP_KERNEL);
1183 if (unlikely(!area))
1184 return NULL;
1185
1186 area->bitmap = kzalloc(BITS_TO_LONGS(UINSNS_PER_PAGE) * sizeof(long), GFP_KERNEL);
1187
1188 if (!area->bitmap)
1189 goto fail;
1190
1191 init_waitqueue_head(&area->wq);
1192 if (!xol_add_vma(area))
1193 return area;
1194
1195fail:
1196 kfree(area->bitmap);
1197 kfree(area);
1198
1199 return get_xol_area(current->mm);
1200}
1201
1202/*
1203 * uprobe_clear_state - Free the area allocated for slots.
1204 */
1205void uprobe_clear_state(struct mm_struct *mm)
1206{
1207 struct xol_area *area = mm->uprobes_state.xol_area;
1208
1209 if (!area)
1210 return;
1211
1212 put_page(area->page);
1213 kfree(area->bitmap);
1214 kfree(area);
1215}
1216
1217/*
1218 * uprobe_reset_state - Free the area allocated for slots.
1219 */
1220void uprobe_reset_state(struct mm_struct *mm)
1221{
1222 mm->uprobes_state.xol_area = NULL;
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301223 atomic_set(&mm->uprobes_state.count, 0);
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301224}
1225
1226/*
1227 * - search for a free slot.
1228 */
1229static unsigned long xol_take_insn_slot(struct xol_area *area)
1230{
1231 unsigned long slot_addr;
1232 int slot_nr;
1233
1234 do {
1235 slot_nr = find_first_zero_bit(area->bitmap, UINSNS_PER_PAGE);
1236 if (slot_nr < UINSNS_PER_PAGE) {
1237 if (!test_and_set_bit(slot_nr, area->bitmap))
1238 break;
1239
1240 slot_nr = UINSNS_PER_PAGE;
1241 continue;
1242 }
1243 wait_event(area->wq, (atomic_read(&area->slot_count) < UINSNS_PER_PAGE));
1244 } while (slot_nr >= UINSNS_PER_PAGE);
1245
1246 slot_addr = area->vaddr + (slot_nr * UPROBE_XOL_SLOT_BYTES);
1247 atomic_inc(&area->slot_count);
1248
1249 return slot_addr;
1250}
1251
1252/*
1253 * xol_get_insn_slot - If was not allocated a slot, then
1254 * allocate a slot.
1255 * Returns the allocated slot address or 0.
1256 */
1257static unsigned long xol_get_insn_slot(struct uprobe *uprobe, unsigned long slot_addr)
1258{
1259 struct xol_area *area;
1260 unsigned long offset;
1261 void *vaddr;
1262
1263 area = get_xol_area(current->mm);
1264 if (!area) {
1265 area = xol_alloc_area();
1266 if (!area)
1267 return 0;
1268 }
1269 current->utask->xol_vaddr = xol_take_insn_slot(area);
1270
1271 /*
1272 * Initialize the slot if xol_vaddr points to valid
1273 * instruction slot.
1274 */
1275 if (unlikely(!current->utask->xol_vaddr))
1276 return 0;
1277
1278 current->utask->vaddr = slot_addr;
1279 offset = current->utask->xol_vaddr & ~PAGE_MASK;
1280 vaddr = kmap_atomic(area->page);
1281 memcpy(vaddr + offset, uprobe->arch.insn, MAX_UINSN_BYTES);
1282 kunmap_atomic(vaddr);
1283
1284 return current->utask->xol_vaddr;
1285}
1286
1287/*
1288 * xol_free_insn_slot - If slot was earlier allocated by
1289 * @xol_get_insn_slot(), make the slot available for
1290 * subsequent requests.
1291 */
1292static void xol_free_insn_slot(struct task_struct *tsk)
1293{
1294 struct xol_area *area;
1295 unsigned long vma_end;
1296 unsigned long slot_addr;
1297
1298 if (!tsk->mm || !tsk->mm->uprobes_state.xol_area || !tsk->utask)
1299 return;
1300
1301 slot_addr = tsk->utask->xol_vaddr;
1302
1303 if (unlikely(!slot_addr || IS_ERR_VALUE(slot_addr)))
1304 return;
1305
1306 area = tsk->mm->uprobes_state.xol_area;
1307 vma_end = area->vaddr + PAGE_SIZE;
1308 if (area->vaddr <= slot_addr && slot_addr < vma_end) {
1309 unsigned long offset;
1310 int slot_nr;
1311
1312 offset = slot_addr - area->vaddr;
1313 slot_nr = offset / UPROBE_XOL_SLOT_BYTES;
1314 if (slot_nr >= UINSNS_PER_PAGE)
1315 return;
1316
1317 clear_bit(slot_nr, area->bitmap);
1318 atomic_dec(&area->slot_count);
1319 if (waitqueue_active(&area->wq))
1320 wake_up(&area->wq);
1321
1322 tsk->utask->xol_vaddr = 0;
1323 }
1324}
1325
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301326/**
1327 * uprobe_get_swbp_addr - compute address of swbp given post-swbp regs
1328 * @regs: Reflects the saved state of the task after it has hit a breakpoint
1329 * instruction.
1330 * Return the address of the breakpoint instruction.
1331 */
1332unsigned long __weak uprobe_get_swbp_addr(struct pt_regs *regs)
1333{
1334 return instruction_pointer(regs) - UPROBE_SWBP_INSN_SIZE;
1335}
1336
1337/*
1338 * Called with no locks held.
1339 * Called in context of a exiting or a exec-ing thread.
1340 */
1341void uprobe_free_utask(struct task_struct *t)
1342{
1343 struct uprobe_task *utask = t->utask;
1344
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301345 if (!utask)
1346 return;
1347
1348 if (utask->active_uprobe)
1349 put_uprobe(utask->active_uprobe);
1350
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301351 xol_free_insn_slot(t);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301352 kfree(utask);
1353 t->utask = NULL;
1354}
1355
1356/*
1357 * Called in context of a new clone/fork from copy_process.
1358 */
1359void uprobe_copy_process(struct task_struct *t)
1360{
1361 t->utask = NULL;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301362}
1363
1364/*
1365 * Allocate a uprobe_task object for the task.
1366 * Called when the thread hits a breakpoint for the first time.
1367 *
1368 * Returns:
1369 * - pointer to new uprobe_task on success
1370 * - NULL otherwise
1371 */
1372static struct uprobe_task *add_utask(void)
1373{
1374 struct uprobe_task *utask;
1375
1376 utask = kzalloc(sizeof *utask, GFP_KERNEL);
1377 if (unlikely(!utask))
1378 return NULL;
1379
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301380 current->utask = utask;
1381 return utask;
1382}
1383
1384/* Prepare to single-step probed instruction out of line. */
1385static int
1386pre_ssout(struct uprobe *uprobe, struct pt_regs *regs, unsigned long vaddr)
1387{
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301388 if (xol_get_insn_slot(uprobe, vaddr) && !arch_uprobe_pre_xol(&uprobe->arch, regs))
1389 return 0;
1390
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301391 return -EFAULT;
1392}
1393
1394/*
1395 * If we are singlestepping, then ensure this thread is not connected to
1396 * non-fatal signals until completion of singlestep. When xol insn itself
1397 * triggers the signal, restart the original insn even if the task is
1398 * already SIGKILL'ed (since coredump should report the correct ip). This
1399 * is even more important if the task has a handler for SIGSEGV/etc, The
1400 * _same_ instruction should be repeated again after return from the signal
1401 * handler, and SSTEP can never finish in this case.
1402 */
1403bool uprobe_deny_signal(void)
1404{
1405 struct task_struct *t = current;
1406 struct uprobe_task *utask = t->utask;
1407
1408 if (likely(!utask || !utask->active_uprobe))
1409 return false;
1410
1411 WARN_ON_ONCE(utask->state != UTASK_SSTEP);
1412
1413 if (signal_pending(t)) {
1414 spin_lock_irq(&t->sighand->siglock);
1415 clear_tsk_thread_flag(t, TIF_SIGPENDING);
1416 spin_unlock_irq(&t->sighand->siglock);
1417
1418 if (__fatal_signal_pending(t) || arch_uprobe_xol_was_trapped(t)) {
1419 utask->state = UTASK_SSTEP_TRAPPED;
1420 set_tsk_thread_flag(t, TIF_UPROBE);
1421 set_tsk_thread_flag(t, TIF_NOTIFY_RESUME);
1422 }
1423 }
1424
1425 return true;
1426}
1427
1428/*
1429 * Avoid singlestepping the original instruction if the original instruction
1430 * is a NOP or can be emulated.
1431 */
1432static bool can_skip_sstep(struct uprobe *uprobe, struct pt_regs *regs)
1433{
1434 if (arch_uprobe_skip_sstep(&uprobe->arch, regs))
1435 return true;
1436
1437 uprobe->flags &= ~UPROBE_SKIP_SSTEP;
1438 return false;
1439}
1440
Oleg Nesterovd790d342012-05-29 21:29:14 +02001441static struct uprobe *find_active_uprobe(unsigned long bp_vaddr, int *is_swbp)
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001442{
1443 struct mm_struct *mm = current->mm;
1444 struct uprobe *uprobe = NULL;
1445 struct vm_area_struct *vma;
1446
1447 down_read(&mm->mmap_sem);
1448 vma = find_vma(mm, bp_vaddr);
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001449 if (vma && vma->vm_start <= bp_vaddr) {
1450 if (valid_vma(vma, false)) {
Oleg Nesterovcb113b42012-07-29 20:22:42 +02001451 struct inode *inode = vma->vm_file->f_mapping->host;
1452 loff_t offset = vaddr_to_offset(vma, bp_vaddr);
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001453
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001454 uprobe = find_uprobe(inode, offset);
1455 }
Oleg Nesterovd790d342012-05-29 21:29:14 +02001456
1457 if (!uprobe)
1458 *is_swbp = is_swbp_at_addr(mm, bp_vaddr);
1459 } else {
1460 *is_swbp = -EFAULT;
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001461 }
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001462 up_read(&mm->mmap_sem);
1463
1464 return uprobe;
1465}
1466
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301467/*
1468 * Run handler and ask thread to singlestep.
1469 * Ensure all non-fatal signals cannot interrupt thread while it singlesteps.
1470 */
1471static void handle_swbp(struct pt_regs *regs)
1472{
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301473 struct uprobe_task *utask;
1474 struct uprobe *uprobe;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301475 unsigned long bp_vaddr;
Oleg Nesterov56bb4cf2012-05-29 21:29:47 +02001476 int uninitialized_var(is_swbp);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301477
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301478 bp_vaddr = uprobe_get_swbp_addr(regs);
Oleg Nesterovd790d342012-05-29 21:29:14 +02001479 uprobe = find_active_uprobe(bp_vaddr, &is_swbp);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301480
1481 if (!uprobe) {
Oleg Nesterov56bb4cf2012-05-29 21:29:47 +02001482 if (is_swbp > 0) {
1483 /* No matching uprobe; signal SIGTRAP. */
1484 send_sig(SIGTRAP, current, 0);
1485 } else {
1486 /*
1487 * Either we raced with uprobe_unregister() or we can't
1488 * access this memory. The latter is only possible if
1489 * another thread plays with our ->mm. In both cases
1490 * we can simply restart. If this vma was unmapped we
1491 * can pretend this insn was not executed yet and get
1492 * the (correct) SIGSEGV after restart.
1493 */
1494 instruction_pointer_set(regs, bp_vaddr);
1495 }
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301496 return;
1497 }
1498
1499 utask = current->utask;
1500 if (!utask) {
1501 utask = add_utask();
1502 /* Cannot allocate; re-execute the instruction. */
1503 if (!utask)
1504 goto cleanup_ret;
1505 }
1506 utask->active_uprobe = uprobe;
1507 handler_chain(uprobe, regs);
1508 if (uprobe->flags & UPROBE_SKIP_SSTEP && can_skip_sstep(uprobe, regs))
1509 goto cleanup_ret;
1510
1511 utask->state = UTASK_SSTEP;
1512 if (!pre_ssout(uprobe, regs, bp_vaddr)) {
1513 user_enable_single_step(current);
1514 return;
1515 }
1516
1517cleanup_ret:
1518 if (utask) {
1519 utask->active_uprobe = NULL;
1520 utask->state = UTASK_RUNNING;
1521 }
1522 if (uprobe) {
1523 if (!(uprobe->flags & UPROBE_SKIP_SSTEP))
1524
1525 /*
1526 * cannot singlestep; cannot skip instruction;
1527 * re-execute the instruction.
1528 */
1529 instruction_pointer_set(regs, bp_vaddr);
1530
1531 put_uprobe(uprobe);
1532 }
1533}
1534
1535/*
1536 * Perform required fix-ups and disable singlestep.
1537 * Allow pending signals to take effect.
1538 */
1539static void handle_singlestep(struct uprobe_task *utask, struct pt_regs *regs)
1540{
1541 struct uprobe *uprobe;
1542
1543 uprobe = utask->active_uprobe;
1544 if (utask->state == UTASK_SSTEP_ACK)
1545 arch_uprobe_post_xol(&uprobe->arch, regs);
1546 else if (utask->state == UTASK_SSTEP_TRAPPED)
1547 arch_uprobe_abort_xol(&uprobe->arch, regs);
1548 else
1549 WARN_ON_ONCE(1);
1550
1551 put_uprobe(uprobe);
1552 utask->active_uprobe = NULL;
1553 utask->state = UTASK_RUNNING;
1554 user_disable_single_step(current);
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301555 xol_free_insn_slot(current);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301556
1557 spin_lock_irq(&current->sighand->siglock);
1558 recalc_sigpending(); /* see uprobe_deny_signal() */
1559 spin_unlock_irq(&current->sighand->siglock);
1560}
1561
1562/*
1563 * On breakpoint hit, breakpoint notifier sets the TIF_UPROBE flag. (and on
1564 * subsequent probe hits on the thread sets the state to UTASK_BP_HIT) and
1565 * allows the thread to return from interrupt.
1566 *
1567 * On singlestep exception, singlestep notifier sets the TIF_UPROBE flag and
1568 * also sets the state to UTASK_SSTEP_ACK and allows the thread to return from
1569 * interrupt.
1570 *
1571 * While returning to userspace, thread notices the TIF_UPROBE flag and calls
1572 * uprobe_notify_resume().
1573 */
1574void uprobe_notify_resume(struct pt_regs *regs)
1575{
1576 struct uprobe_task *utask;
1577
1578 utask = current->utask;
1579 if (!utask || utask->state == UTASK_BP_HIT)
1580 handle_swbp(regs);
1581 else
1582 handle_singlestep(utask, regs);
1583}
1584
1585/*
1586 * uprobe_pre_sstep_notifier gets called from interrupt context as part of
1587 * notifier mechanism. Set TIF_UPROBE flag and indicate breakpoint hit.
1588 */
1589int uprobe_pre_sstep_notifier(struct pt_regs *regs)
1590{
1591 struct uprobe_task *utask;
1592
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301593 if (!current->mm || !atomic_read(&current->mm->uprobes_state.count))
1594 /* task is currently not uprobed */
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301595 return 0;
1596
1597 utask = current->utask;
1598 if (utask)
1599 utask->state = UTASK_BP_HIT;
1600
1601 set_thread_flag(TIF_UPROBE);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301602
1603 return 1;
1604}
1605
1606/*
1607 * uprobe_post_sstep_notifier gets called in interrupt context as part of notifier
1608 * mechanism. Set TIF_UPROBE flag and indicate completion of singlestep.
1609 */
1610int uprobe_post_sstep_notifier(struct pt_regs *regs)
1611{
1612 struct uprobe_task *utask = current->utask;
1613
1614 if (!current->mm || !utask || !utask->active_uprobe)
1615 /* task is currently not uprobed */
1616 return 0;
1617
1618 utask->state = UTASK_SSTEP_ACK;
1619 set_thread_flag(TIF_UPROBE);
1620 return 1;
1621}
1622
1623static struct notifier_block uprobe_exception_nb = {
1624 .notifier_call = arch_uprobe_exception_notify,
1625 .priority = INT_MAX-1, /* notified after kprobes, kgdb */
1626};
1627
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301628static int __init init_uprobes(void)
1629{
1630 int i;
1631
1632 for (i = 0; i < UPROBES_HASH_SZ; i++) {
1633 mutex_init(&uprobes_mutex[i]);
1634 mutex_init(&uprobes_mmap_mutex[i]);
1635 }
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301636
1637 return register_die_notifier(&uprobe_exception_nb);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301638}
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301639module_init(init_uprobes);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301640
1641static void __exit exit_uprobes(void)
1642{
1643}
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301644module_exit(exit_uprobes);