blob: 7ec2eb278634cc880148bc1511db16bbda82bca1 [file] [log] [blame]
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +01002 * User-space Probes (UProbes)
Srikar Dronamraju2b144492012-02-09 14:56:42 +05303 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
Ingo Molnar35aa6212012-02-22 11:37:29 +010018 * Copyright (C) IBM Corporation, 2008-2012
Srikar Dronamraju2b144492012-02-09 14:56:42 +053019 * Authors:
20 * Srikar Dronamraju
21 * Jim Keniston
Ingo Molnar35aa6212012-02-22 11:37:29 +010022 * Copyright (C) 2011-2012 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
Srikar Dronamraju2b144492012-02-09 14:56:42 +053023 */
24
25#include <linux/kernel.h>
26#include <linux/highmem.h>
27#include <linux/pagemap.h> /* read_mapping_page */
28#include <linux/slab.h>
29#include <linux/sched.h>
30#include <linux/rmap.h> /* anon_vma_prepare */
31#include <linux/mmu_notifier.h> /* set_pte_at_notify */
32#include <linux/swap.h> /* try_to_free_swap */
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +053033#include <linux/ptrace.h> /* user_enable_single_step */
34#include <linux/kdebug.h> /* notifier mechanism */
Oleg Nesterov194f8dc2012-07-29 20:22:49 +020035#include "../../mm/internal.h" /* munlock_vma_page */
Oleg Nesterov32cdba12012-11-14 19:03:42 +010036#include <linux/percpu-rwsem.h>
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010037
Srikar Dronamraju2b144492012-02-09 14:56:42 +053038#include <linux/uprobes.h>
39
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +053040#define UINSNS_PER_PAGE (PAGE_SIZE/UPROBE_XOL_SLOT_BYTES)
41#define MAX_UPROBE_XOL_SLOTS UINSNS_PER_PAGE
42
Srikar Dronamraju2b144492012-02-09 14:56:42 +053043static struct rb_root uprobes_tree = RB_ROOT;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010044
Srikar Dronamraju2b144492012-02-09 14:56:42 +053045static DEFINE_SPINLOCK(uprobes_treelock); /* serialize rbtree access */
46
47#define UPROBES_HASH_SZ 13
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010048
Peter Zijlstrac5784de2012-06-15 17:43:39 +020049/*
50 * We need separate register/unregister and mmap/munmap lock hashes because
51 * of mmap_sem nesting.
52 *
53 * uprobe_register() needs to install probes on (potentially) all processes
54 * and thus needs to acquire multiple mmap_sems (consequtively, not
55 * concurrently), whereas uprobe_mmap() is called while holding mmap_sem
56 * for the particular process doing the mmap.
57 *
58 * uprobe_register()->register_for_each_vma() needs to drop/acquire mmap_sem
59 * because of lock order against i_mmap_mutex. This means there's a hole in
60 * the register vma iteration where a mmap() can happen.
61 *
62 * Thus uprobe_register() can race with uprobe_mmap() and we can try and
63 * install a probe where one is already installed.
64 */
65
Srikar Dronamraju2b144492012-02-09 14:56:42 +053066/* serialize (un)register */
67static struct mutex uprobes_mutex[UPROBES_HASH_SZ];
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010068
69#define uprobes_hash(v) (&uprobes_mutex[((unsigned long)(v)) % UPROBES_HASH_SZ])
Srikar Dronamraju2b144492012-02-09 14:56:42 +053070
71/* serialize uprobe->pending_list */
72static struct mutex uprobes_mmap_mutex[UPROBES_HASH_SZ];
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010073#define uprobes_mmap_hash(v) (&uprobes_mmap_mutex[((unsigned long)(v)) % UPROBES_HASH_SZ])
Srikar Dronamraju2b144492012-02-09 14:56:42 +053074
Oleg Nesterov32cdba12012-11-14 19:03:42 +010075static struct percpu_rw_semaphore dup_mmap_sem;
76
Srikar Dronamraju2b144492012-02-09 14:56:42 +053077/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010078 * uprobe_events allows us to skip the uprobe_mmap if there are no uprobe
Srikar Dronamraju2b144492012-02-09 14:56:42 +053079 * events active at this time. Probably a fine grained per inode count is
80 * better?
81 */
82static atomic_t uprobe_events = ATOMIC_INIT(0);
83
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +020084/* Have a copy of original instruction */
Oleg Nesterov71434f22012-09-30 21:12:44 +020085#define UPROBE_COPY_INSN 0
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +020086/* Can skip singlestep */
Oleg Nesterovbb929282012-11-24 18:27:08 +010087#define UPROBE_SKIP_SSTEP 1
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +020088
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +053089struct uprobe {
90 struct rb_node rb_node; /* node in the rb tree */
91 atomic_t ref;
Oleg Nesterove591c8d2012-11-24 17:29:40 +010092 struct rw_semaphore register_rwsem;
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +053093 struct rw_semaphore consumer_rwsem;
Oleg Nesterov4710f052012-09-30 20:31:41 +020094 struct mutex copy_mutex; /* TODO: kill me and UPROBE_COPY_INSN */
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +053095 struct list_head pending_list;
96 struct uprobe_consumer *consumers;
97 struct inode *inode; /* Also hold a ref to inode */
98 loff_t offset;
Oleg Nesterov71434f22012-09-30 21:12:44 +020099 unsigned long flags;
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +0530100 struct arch_uprobe arch;
101};
102
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530103/*
104 * valid_vma: Verify if the specified vma is an executable vma
105 * Relax restrictions while unregistering: vm_flags might have
106 * changed after breakpoint was inserted.
107 * - is_register: indicates if we are in register context.
108 * - Return 1 if the specified virtual address is in an
109 * executable vma.
110 */
111static bool valid_vma(struct vm_area_struct *vma, bool is_register)
112{
Oleg Nesterove40cfce2012-09-16 19:31:39 +0200113 vm_flags_t flags = VM_HUGETLB | VM_MAYEXEC | VM_SHARED;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530114
Oleg Nesterove40cfce2012-09-16 19:31:39 +0200115 if (is_register)
116 flags |= VM_WRITE;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530117
Oleg Nesterove40cfce2012-09-16 19:31:39 +0200118 return vma->vm_file && (vma->vm_flags & flags) == VM_MAYEXEC;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530119}
120
Oleg Nesterov57683f72012-07-29 20:22:47 +0200121static unsigned long offset_to_vaddr(struct vm_area_struct *vma, loff_t offset)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530122{
Oleg Nesterov57683f72012-07-29 20:22:47 +0200123 return vma->vm_start + offset - ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530124}
125
Oleg Nesterovcb113b42012-07-29 20:22:42 +0200126static loff_t vaddr_to_offset(struct vm_area_struct *vma, unsigned long vaddr)
127{
128 return ((loff_t)vma->vm_pgoff << PAGE_SHIFT) + (vaddr - vma->vm_start);
129}
130
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530131/**
132 * __replace_page - replace page in vma by new page.
133 * based on replace_page in mm/ksm.c
134 *
135 * @vma: vma that holds the pte pointing to page
Oleg Nesterovc517ee72012-07-29 20:22:16 +0200136 * @addr: address the old @page is mapped at
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530137 * @page: the cowed page we are replacing by kpage
138 * @kpage: the modified page we replace page by
139 *
140 * Returns 0 on success, -EFAULT on failure.
141 */
Oleg Nesterovc517ee72012-07-29 20:22:16 +0200142static int __replace_page(struct vm_area_struct *vma, unsigned long addr,
143 struct page *page, struct page *kpage)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530144{
145 struct mm_struct *mm = vma->vm_mm;
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200146 spinlock_t *ptl;
147 pte_t *ptep;
Oleg Nesterov9f924482012-07-29 20:22:20 +0200148 int err;
Haggai Eran6bdb9132012-10-08 16:33:35 -0700149 /* For mmu_notifiers */
150 const unsigned long mmun_start = addr;
151 const unsigned long mmun_end = addr + PAGE_SIZE;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530152
Oleg Nesterov194f8dc2012-07-29 20:22:49 +0200153 /* For try_to_free_swap() and munlock_vma_page() below */
Oleg Nesterov9f924482012-07-29 20:22:20 +0200154 lock_page(page);
155
Haggai Eran6bdb9132012-10-08 16:33:35 -0700156 mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
Oleg Nesterov9f924482012-07-29 20:22:20 +0200157 err = -EAGAIN;
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200158 ptep = page_check_address(page, mm, addr, &ptl, 0);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530159 if (!ptep)
Oleg Nesterov9f924482012-07-29 20:22:20 +0200160 goto unlock;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530161
162 get_page(kpage);
163 page_add_new_anon_rmap(kpage, vma, addr);
164
Srikar Dronamraju7396fa82012-04-11 16:05:16 +0530165 if (!PageAnon(page)) {
166 dec_mm_counter(mm, MM_FILEPAGES);
167 inc_mm_counter(mm, MM_ANONPAGES);
168 }
169
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530170 flush_cache_page(vma, addr, pte_pfn(*ptep));
171 ptep_clear_flush(vma, addr, ptep);
172 set_pte_at_notify(mm, addr, ptep, mk_pte(kpage, vma->vm_page_prot));
173
174 page_remove_rmap(page);
175 if (!page_mapped(page))
176 try_to_free_swap(page);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530177 pte_unmap_unlock(ptep, ptl);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530178
Oleg Nesterov194f8dc2012-07-29 20:22:49 +0200179 if (vma->vm_flags & VM_LOCKED)
180 munlock_vma_page(page);
181 put_page(page);
182
Oleg Nesterov9f924482012-07-29 20:22:20 +0200183 err = 0;
184 unlock:
Haggai Eran6bdb9132012-10-08 16:33:35 -0700185 mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
Oleg Nesterov9f924482012-07-29 20:22:20 +0200186 unlock_page(page);
187 return err;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530188}
189
190/**
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530191 * is_swbp_insn - check if instruction is breakpoint instruction.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530192 * @insn: instruction to be checked.
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530193 * Default implementation of is_swbp_insn
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530194 * Returns true if @insn is a breakpoint instruction.
195 */
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530196bool __weak is_swbp_insn(uprobe_opcode_t *insn)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530197{
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530198 return *insn == UPROBE_SWBP_INSN;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530199}
200
Oleg Nesterovcceb55a2012-09-23 21:10:18 +0200201static void copy_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t *opcode)
202{
203 void *kaddr = kmap_atomic(page);
204 memcpy(opcode, kaddr + (vaddr & ~PAGE_MASK), UPROBE_SWBP_INSN_SIZE);
205 kunmap_atomic(kaddr);
206}
207
Oleg Nesteroved6f6a52012-09-23 21:30:44 +0200208static int verify_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t *new_opcode)
209{
210 uprobe_opcode_t old_opcode;
211 bool is_swbp;
212
213 copy_opcode(page, vaddr, &old_opcode);
214 is_swbp = is_swbp_insn(&old_opcode);
215
216 if (is_swbp_insn(new_opcode)) {
217 if (is_swbp) /* register: already installed? */
218 return 0;
219 } else {
220 if (!is_swbp) /* unregister: was it changed by us? */
Oleg Nesterov076a3652012-09-30 18:54:53 +0200221 return 0;
Oleg Nesteroved6f6a52012-09-23 21:30:44 +0200222 }
223
224 return 1;
225}
226
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530227/*
228 * NOTE:
229 * Expect the breakpoint instruction to be the smallest size instruction for
230 * the architecture. If an arch has variable length instruction and the
231 * breakpoint instruction is not of the smallest length instruction
Oleg Nesterovcceb55a2012-09-23 21:10:18 +0200232 * supported by that architecture then we need to modify is_swbp_at_addr and
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530233 * write_opcode accordingly. This would never be a problem for archs that
234 * have fixed length instructions.
235 */
236
237/*
238 * write_opcode - write the opcode at a given virtual address.
239 * @mm: the probed process address space.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530240 * @vaddr: the virtual address to store the opcode.
241 * @opcode: opcode to be written at @vaddr.
242 *
243 * Called with mm->mmap_sem held (for read and with a reference to
244 * mm).
245 *
246 * For mm @mm, write the opcode at @vaddr.
247 * Return 0 (success) or a negative errno.
248 */
Oleg Nesterovcceb55a2012-09-23 21:10:18 +0200249static int write_opcode(struct mm_struct *mm, unsigned long vaddr,
250 uprobe_opcode_t opcode)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530251{
252 struct page *old_page, *new_page;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530253 void *vaddr_old, *vaddr_new;
254 struct vm_area_struct *vma;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530255 int ret;
Oleg Nesterovf4030722012-07-29 20:22:12 +0200256
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200257retry:
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530258 /* Read the page with vaddr into memory */
Oleg Nesterov75ed82e2012-09-16 17:20:06 +0200259 ret = get_user_pages(NULL, mm, vaddr, 1, 0, 1, &old_page, &vma);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530260 if (ret <= 0)
261 return ret;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100262
Oleg Nesteroved6f6a52012-09-23 21:30:44 +0200263 ret = verify_opcode(old_page, vaddr, &opcode);
264 if (ret <= 0)
265 goto put_old;
266
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530267 ret = -ENOMEM;
268 new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vaddr);
269 if (!new_page)
Oleg Nesterov9f924482012-07-29 20:22:20 +0200270 goto put_old;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530271
272 __SetPageUptodate(new_page);
273
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530274 /* copy the page now that we've got it stable */
275 vaddr_old = kmap_atomic(old_page);
276 vaddr_new = kmap_atomic(new_page);
277
278 memcpy(vaddr_new, vaddr_old, PAGE_SIZE);
Oleg Nesterovd9c4a302012-06-15 17:43:50 +0200279 memcpy(vaddr_new + (vaddr & ~PAGE_MASK), &opcode, UPROBE_SWBP_INSN_SIZE);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530280
281 kunmap_atomic(vaddr_new);
282 kunmap_atomic(vaddr_old);
283
284 ret = anon_vma_prepare(vma);
285 if (ret)
Oleg Nesterov9f924482012-07-29 20:22:20 +0200286 goto put_new;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530287
Oleg Nesterovc517ee72012-07-29 20:22:16 +0200288 ret = __replace_page(vma, vaddr, old_page, new_page);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530289
Oleg Nesterov9f924482012-07-29 20:22:20 +0200290put_new:
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530291 page_cache_release(new_page);
Oleg Nesterov9f924482012-07-29 20:22:20 +0200292put_old:
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100293 put_page(old_page);
294
Oleg Nesterov5323ce72012-06-15 17:43:28 +0200295 if (unlikely(ret == -EAGAIN))
296 goto retry;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530297 return ret;
298}
299
300/**
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530301 * set_swbp - store breakpoint at a given address.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530302 * @auprobe: arch specific probepoint information.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530303 * @mm: the probed process address space.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530304 * @vaddr: the virtual address to insert the opcode.
305 *
306 * For mm @mm, store the breakpoint instruction at @vaddr.
307 * Return 0 (success) or a negative errno.
308 */
Srikar Dronamraju5cb4ac32012-03-12 14:55:45 +0530309int __weak set_swbp(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530310{
Oleg Nesterovcceb55a2012-09-23 21:10:18 +0200311 return write_opcode(mm, vaddr, UPROBE_SWBP_INSN);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530312}
313
314/**
315 * set_orig_insn - Restore the original instruction.
316 * @mm: the probed process address space.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530317 * @auprobe: arch specific probepoint information.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530318 * @vaddr: the virtual address to insert the opcode.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530319 *
320 * For mm @mm, restore the original opcode (opcode) at @vaddr.
321 * Return 0 (success) or a negative errno.
322 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100323int __weak
Oleg Nesterovded86e72012-08-08 18:07:03 +0200324set_orig_insn(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530325{
Oleg Nesterovcceb55a2012-09-23 21:10:18 +0200326 return write_opcode(mm, vaddr, *(uprobe_opcode_t *)auprobe->insn);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530327}
328
329static int match_uprobe(struct uprobe *l, struct uprobe *r)
330{
331 if (l->inode < r->inode)
332 return -1;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100333
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530334 if (l->inode > r->inode)
335 return 1;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530336
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100337 if (l->offset < r->offset)
338 return -1;
339
340 if (l->offset > r->offset)
341 return 1;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530342
343 return 0;
344}
345
346static struct uprobe *__find_uprobe(struct inode *inode, loff_t offset)
347{
348 struct uprobe u = { .inode = inode, .offset = offset };
349 struct rb_node *n = uprobes_tree.rb_node;
350 struct uprobe *uprobe;
351 int match;
352
353 while (n) {
354 uprobe = rb_entry(n, struct uprobe, rb_node);
355 match = match_uprobe(&u, uprobe);
356 if (!match) {
357 atomic_inc(&uprobe->ref);
358 return uprobe;
359 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100360
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530361 if (match < 0)
362 n = n->rb_left;
363 else
364 n = n->rb_right;
365 }
366 return NULL;
367}
368
369/*
370 * Find a uprobe corresponding to a given inode:offset
371 * Acquires uprobes_treelock
372 */
373static struct uprobe *find_uprobe(struct inode *inode, loff_t offset)
374{
375 struct uprobe *uprobe;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530376
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200377 spin_lock(&uprobes_treelock);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530378 uprobe = __find_uprobe(inode, offset);
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200379 spin_unlock(&uprobes_treelock);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100380
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530381 return uprobe;
382}
383
384static struct uprobe *__insert_uprobe(struct uprobe *uprobe)
385{
386 struct rb_node **p = &uprobes_tree.rb_node;
387 struct rb_node *parent = NULL;
388 struct uprobe *u;
389 int match;
390
391 while (*p) {
392 parent = *p;
393 u = rb_entry(parent, struct uprobe, rb_node);
394 match = match_uprobe(uprobe, u);
395 if (!match) {
396 atomic_inc(&u->ref);
397 return u;
398 }
399
400 if (match < 0)
401 p = &parent->rb_left;
402 else
403 p = &parent->rb_right;
404
405 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100406
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530407 u = NULL;
408 rb_link_node(&uprobe->rb_node, parent, p);
409 rb_insert_color(&uprobe->rb_node, &uprobes_tree);
410 /* get access + creation ref */
411 atomic_set(&uprobe->ref, 2);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100412
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530413 return u;
414}
415
416/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100417 * Acquire uprobes_treelock.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530418 * Matching uprobe already exists in rbtree;
419 * increment (access refcount) and return the matching uprobe.
420 *
421 * No matching uprobe; insert the uprobe in rb_tree;
422 * get a double refcount (access + creation) and return NULL.
423 */
424static struct uprobe *insert_uprobe(struct uprobe *uprobe)
425{
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530426 struct uprobe *u;
427
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200428 spin_lock(&uprobes_treelock);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530429 u = __insert_uprobe(uprobe);
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200430 spin_unlock(&uprobes_treelock);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100431
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530432 return u;
433}
434
435static void put_uprobe(struct uprobe *uprobe)
436{
437 if (atomic_dec_and_test(&uprobe->ref))
438 kfree(uprobe);
439}
440
441static struct uprobe *alloc_uprobe(struct inode *inode, loff_t offset)
442{
443 struct uprobe *uprobe, *cur_uprobe;
444
445 uprobe = kzalloc(sizeof(struct uprobe), GFP_KERNEL);
446 if (!uprobe)
447 return NULL;
448
449 uprobe->inode = igrab(inode);
450 uprobe->offset = offset;
Oleg Nesterove591c8d2012-11-24 17:29:40 +0100451 init_rwsem(&uprobe->register_rwsem);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530452 init_rwsem(&uprobe->consumer_rwsem);
Oleg Nesterov4710f052012-09-30 20:31:41 +0200453 mutex_init(&uprobe->copy_mutex);
Oleg Nesterovbbc33d02012-11-21 16:55:38 +0100454 /* For now assume that the instruction need not be single-stepped */
455 __set_bit(UPROBE_SKIP_SSTEP, &uprobe->flags);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530456
457 /* add to uprobes_tree, sorted on inode:offset */
458 cur_uprobe = insert_uprobe(uprobe);
459
460 /* a uprobe exists for this inode:offset combination */
461 if (cur_uprobe) {
462 kfree(uprobe);
463 uprobe = cur_uprobe;
464 iput(inode);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100465 } else {
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530466 atomic_inc(&uprobe_events);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100467 }
468
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530469 return uprobe;
470}
471
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530472static void handler_chain(struct uprobe *uprobe, struct pt_regs *regs)
473{
474 struct uprobe_consumer *uc;
475
Oleg Nesterove591c8d2012-11-24 17:29:40 +0100476 down_read(&uprobe->register_rwsem);
Oleg Nesterovfe20d712012-11-21 17:32:30 +0100477 for (uc = uprobe->consumers; uc; uc = uc->next)
478 uc->handler(uc, regs);
Oleg Nesterove591c8d2012-11-24 17:29:40 +0100479 up_read(&uprobe->register_rwsem);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530480}
481
Oleg Nesterov9a98e032012-11-23 20:15:17 +0100482static void consumer_add(struct uprobe *uprobe, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530483{
484 down_write(&uprobe->consumer_rwsem);
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530485 uc->next = uprobe->consumers;
486 uprobe->consumers = uc;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530487 up_write(&uprobe->consumer_rwsem);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530488}
489
490/*
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530491 * For uprobe @uprobe, delete the consumer @uc.
492 * Return true if the @uc is deleted successfully
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530493 * or return false.
494 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530495static bool consumer_del(struct uprobe *uprobe, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530496{
497 struct uprobe_consumer **con;
498 bool ret = false;
499
500 down_write(&uprobe->consumer_rwsem);
501 for (con = &uprobe->consumers; *con; con = &(*con)->next) {
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530502 if (*con == uc) {
503 *con = uc->next;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530504 ret = true;
505 break;
506 }
507 }
508 up_write(&uprobe->consumer_rwsem);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100509
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530510 return ret;
511}
512
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530513static int
Oleg Nesterovd4366152012-06-15 17:43:42 +0200514__copy_insn(struct address_space *mapping, struct file *filp, char *insn,
Oleg Nesterov593609a2012-06-15 17:43:59 +0200515 unsigned long nbytes, loff_t offset)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530516{
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530517 struct page *page;
518 void *vaddr;
Oleg Nesterov593609a2012-06-15 17:43:59 +0200519 unsigned long off;
520 pgoff_t idx;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530521
522 if (!filp)
523 return -EINVAL;
524
Oleg Nesterovcc359d12012-06-15 17:43:25 +0200525 if (!mapping->a_ops->readpage)
526 return -EIO;
527
Oleg Nesterov593609a2012-06-15 17:43:59 +0200528 idx = offset >> PAGE_CACHE_SHIFT;
529 off = offset & ~PAGE_MASK;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530530
531 /*
532 * Ensure that the page that has the original instruction is
533 * populated and in page-cache.
534 */
535 page = read_mapping_page(mapping, idx, filp);
536 if (IS_ERR(page))
537 return PTR_ERR(page);
538
539 vaddr = kmap_atomic(page);
Oleg Nesterov593609a2012-06-15 17:43:59 +0200540 memcpy(insn, vaddr + off, nbytes);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530541 kunmap_atomic(vaddr);
542 page_cache_release(page);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100543
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530544 return 0;
545}
546
Oleg Nesterovd4366152012-06-15 17:43:42 +0200547static int copy_insn(struct uprobe *uprobe, struct file *filp)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530548{
549 struct address_space *mapping;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530550 unsigned long nbytes;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100551 int bytes;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530552
Oleg Nesterovd4366152012-06-15 17:43:42 +0200553 nbytes = PAGE_SIZE - (uprobe->offset & ~PAGE_MASK);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530554 mapping = uprobe->inode->i_mapping;
555
556 /* Instruction at end of binary; copy only available bytes */
557 if (uprobe->offset + MAX_UINSN_BYTES > uprobe->inode->i_size)
558 bytes = uprobe->inode->i_size - uprobe->offset;
559 else
560 bytes = MAX_UINSN_BYTES;
561
562 /* Instruction at the page-boundary; copy bytes in second page */
563 if (nbytes < bytes) {
Oleg Nesterovfc36f592012-06-15 17:43:44 +0200564 int err = __copy_insn(mapping, filp, uprobe->arch.insn + nbytes,
565 bytes - nbytes, uprobe->offset + nbytes);
566 if (err)
567 return err;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530568 bytes = nbytes;
569 }
Oleg Nesterovd4366152012-06-15 17:43:42 +0200570 return __copy_insn(mapping, filp, uprobe->arch.insn, bytes, uprobe->offset);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530571}
572
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200573static int prepare_uprobe(struct uprobe *uprobe, struct file *file,
574 struct mm_struct *mm, unsigned long vaddr)
575{
576 int ret = 0;
577
Oleg Nesterov71434f22012-09-30 21:12:44 +0200578 if (test_bit(UPROBE_COPY_INSN, &uprobe->flags))
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200579 return ret;
580
Oleg Nesterov4710f052012-09-30 20:31:41 +0200581 mutex_lock(&uprobe->copy_mutex);
Oleg Nesterov71434f22012-09-30 21:12:44 +0200582 if (test_bit(UPROBE_COPY_INSN, &uprobe->flags))
Oleg Nesterov4710f052012-09-30 20:31:41 +0200583 goto out;
584
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200585 ret = copy_insn(uprobe, file);
586 if (ret)
587 goto out;
588
589 ret = -ENOTSUPP;
590 if (is_swbp_insn((uprobe_opcode_t *)uprobe->arch.insn))
591 goto out;
592
593 ret = arch_uprobe_analyze_insn(&uprobe->arch, mm, vaddr);
594 if (ret)
595 goto out;
596
597 /* write_opcode() assumes we don't cross page boundary */
598 BUG_ON((uprobe->offset & ~PAGE_MASK) +
599 UPROBE_SWBP_INSN_SIZE > PAGE_SIZE);
600
601 smp_wmb(); /* pairs with rmb() in find_active_uprobe() */
Oleg Nesterov71434f22012-09-30 21:12:44 +0200602 set_bit(UPROBE_COPY_INSN, &uprobe->flags);
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200603
604 out:
Oleg Nesterov4710f052012-09-30 20:31:41 +0200605 mutex_unlock(&uprobe->copy_mutex);
606
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200607 return ret;
608}
609
Oleg Nesterov63633cb2012-11-22 18:30:15 +0100610static bool filter_chain(struct uprobe *uprobe)
611{
Oleg Nesterov1ff6fee2012-11-24 18:15:46 +0100612 struct uprobe_consumer *uc;
613 bool ret = false;
614
615 down_read(&uprobe->consumer_rwsem);
616 for (uc = uprobe->consumers; uc; uc = uc->next) {
617 /* TODO: ret = uc->filter(...) */
618 ret = true;
619 if (ret)
620 break;
621 }
622 up_read(&uprobe->consumer_rwsem);
623
624 return ret;
Oleg Nesterov63633cb2012-11-22 18:30:15 +0100625}
626
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530627static int
628install_breakpoint(struct uprobe *uprobe, struct mm_struct *mm,
Oleg Nesterov816c03f2012-06-15 17:43:55 +0200629 struct vm_area_struct *vma, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530630{
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +0200631 bool first_uprobe;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530632 int ret;
633
634 /*
635 * If probe is being deleted, unregister thread could be done with
636 * the vma-rmap-walk through. Adding a probe now can be fatal since
Oleg Nesterov63633cb2012-11-22 18:30:15 +0100637 * nobody will be able to cleanup. But in this case filter_chain()
638 * must return false, all consumers have gone away.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530639 */
Oleg Nesterov63633cb2012-11-22 18:30:15 +0100640 if (!filter_chain(uprobe))
Oleg Nesterov78f74112012-08-08 17:35:08 +0200641 return 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530642
Oleg Nesterovcb9a19f2012-09-30 20:11:45 +0200643 ret = prepare_uprobe(uprobe, vma->vm_file, mm, vaddr);
644 if (ret)
645 return ret;
Srikar Dronamraju682968e2012-03-30 23:56:46 +0530646
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +0200647 /*
648 * set MMF_HAS_UPROBES in advance for uprobe_pre_sstep_notifier(),
649 * the task can hit this breakpoint right after __replace_page().
650 */
651 first_uprobe = !test_bit(MMF_HAS_UPROBES, &mm->flags);
652 if (first_uprobe)
653 set_bit(MMF_HAS_UPROBES, &mm->flags);
654
Oleg Nesterov816c03f2012-06-15 17:43:55 +0200655 ret = set_swbp(&uprobe->arch, mm, vaddr);
Oleg Nesterov9f68f672012-08-19 16:15:09 +0200656 if (!ret)
657 clear_bit(MMF_RECALC_UPROBES, &mm->flags);
658 else if (first_uprobe)
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +0200659 clear_bit(MMF_HAS_UPROBES, &mm->flags);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530660
661 return ret;
662}
663
Oleg Nesterov076a3652012-09-30 18:54:53 +0200664static int
Oleg Nesterov816c03f2012-06-15 17:43:55 +0200665remove_breakpoint(struct uprobe *uprobe, struct mm_struct *mm, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530666{
Oleg Nesterov9f68f672012-08-19 16:15:09 +0200667 if (!test_bit(MMF_HAS_UPROBES, &mm->flags))
Oleg Nesterov076a3652012-09-30 18:54:53 +0200668 return 0;
Oleg Nesterov9f68f672012-08-19 16:15:09 +0200669
Oleg Nesterov63633cb2012-11-22 18:30:15 +0100670 if (filter_chain(uprobe))
671 return 0;
672
Oleg Nesterov9f68f672012-08-19 16:15:09 +0200673 set_bit(MMF_RECALC_UPROBES, &mm->flags);
Oleg Nesterov076a3652012-09-30 18:54:53 +0200674 return set_orig_insn(&uprobe->arch, mm, vaddr);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530675}
676
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530677/*
Oleg Nesterov778b0322012-05-29 21:30:08 +0200678 * There could be threads that have already hit the breakpoint. They
679 * will recheck the current insn and restart if find_uprobe() fails.
680 * See find_active_uprobe().
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +0530681 */
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530682static void delete_uprobe(struct uprobe *uprobe)
683{
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200684 spin_lock(&uprobes_treelock);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530685 rb_erase(&uprobe->rb_node, &uprobes_tree);
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200686 spin_unlock(&uprobes_treelock);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530687 iput(uprobe->inode);
688 put_uprobe(uprobe);
689 atomic_dec(&uprobe_events);
690}
691
Oleg Nesterov26872092012-06-15 17:43:33 +0200692struct map_info {
693 struct map_info *next;
694 struct mm_struct *mm;
Oleg Nesterov816c03f2012-06-15 17:43:55 +0200695 unsigned long vaddr;
Oleg Nesterov26872092012-06-15 17:43:33 +0200696};
697
698static inline struct map_info *free_map_info(struct map_info *info)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530699{
Oleg Nesterov26872092012-06-15 17:43:33 +0200700 struct map_info *next = info->next;
701 kfree(info);
702 return next;
703}
704
705static struct map_info *
706build_map_info(struct address_space *mapping, loff_t offset, bool is_register)
707{
708 unsigned long pgoff = offset >> PAGE_SHIFT;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530709 struct vm_area_struct *vma;
Oleg Nesterov26872092012-06-15 17:43:33 +0200710 struct map_info *curr = NULL;
711 struct map_info *prev = NULL;
712 struct map_info *info;
713 int more = 0;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100714
Oleg Nesterov26872092012-06-15 17:43:33 +0200715 again:
716 mutex_lock(&mapping->i_mmap_mutex);
Michel Lespinasse6b2dbba2012-10-08 16:31:25 -0700717 vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) {
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530718 if (!valid_vma(vma, is_register))
719 continue;
720
Oleg Nesterov7a5bfb62012-06-15 17:43:36 +0200721 if (!prev && !more) {
722 /*
723 * Needs GFP_NOWAIT to avoid i_mmap_mutex recursion through
724 * reclaim. This is optimistic, no harm done if it fails.
725 */
726 prev = kmalloc(sizeof(struct map_info),
727 GFP_NOWAIT | __GFP_NOMEMALLOC | __GFP_NOWARN);
728 if (prev)
729 prev->next = NULL;
730 }
Oleg Nesterov26872092012-06-15 17:43:33 +0200731 if (!prev) {
732 more++;
733 continue;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530734 }
735
Oleg Nesterov26872092012-06-15 17:43:33 +0200736 if (!atomic_inc_not_zero(&vma->vm_mm->mm_users))
737 continue;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100738
Oleg Nesterov26872092012-06-15 17:43:33 +0200739 info = prev;
740 prev = prev->next;
741 info->next = curr;
742 curr = info;
743
744 info->mm = vma->vm_mm;
Oleg Nesterov57683f72012-07-29 20:22:47 +0200745 info->vaddr = offset_to_vaddr(vma, offset);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530746 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530747 mutex_unlock(&mapping->i_mmap_mutex);
748
Oleg Nesterov26872092012-06-15 17:43:33 +0200749 if (!more)
750 goto out;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100751
Oleg Nesterov26872092012-06-15 17:43:33 +0200752 prev = curr;
753 while (curr) {
754 mmput(curr->mm);
755 curr = curr->next;
756 }
757
758 do {
759 info = kmalloc(sizeof(struct map_info), GFP_KERNEL);
760 if (!info) {
761 curr = ERR_PTR(-ENOMEM);
762 goto out;
763 }
764 info->next = prev;
765 prev = info;
766 } while (--more);
767
768 goto again;
769 out:
770 while (prev)
771 prev = free_map_info(prev);
772 return curr;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530773}
774
775static int register_for_each_vma(struct uprobe *uprobe, bool is_register)
776{
Oleg Nesterov26872092012-06-15 17:43:33 +0200777 struct map_info *info;
778 int err = 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530779
Oleg Nesterov32cdba12012-11-14 19:03:42 +0100780 percpu_down_write(&dup_mmap_sem);
Oleg Nesterov26872092012-06-15 17:43:33 +0200781 info = build_map_info(uprobe->inode->i_mapping,
782 uprobe->offset, is_register);
Oleg Nesterov32cdba12012-11-14 19:03:42 +0100783 if (IS_ERR(info)) {
784 err = PTR_ERR(info);
785 goto out;
786 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100787
Oleg Nesterov26872092012-06-15 17:43:33 +0200788 while (info) {
789 struct mm_struct *mm = info->mm;
790 struct vm_area_struct *vma;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100791
Oleg Nesterov076a3652012-09-30 18:54:53 +0200792 if (err && is_register)
Oleg Nesterov26872092012-06-15 17:43:33 +0200793 goto free;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100794
Oleg Nesterov77fc4af2012-05-29 21:29:28 +0200795 down_write(&mm->mmap_sem);
Oleg Nesterovf4d6dfe2012-07-29 20:22:44 +0200796 vma = find_vma(mm, info->vaddr);
797 if (!vma || !valid_vma(vma, is_register) ||
798 vma->vm_file->f_mapping->host != uprobe->inode)
Oleg Nesterov26872092012-06-15 17:43:33 +0200799 goto unlock;
800
Oleg Nesterovf4d6dfe2012-07-29 20:22:44 +0200801 if (vma->vm_start > info->vaddr ||
802 vaddr_to_offset(vma, info->vaddr) != uprobe->offset)
Oleg Nesterov26872092012-06-15 17:43:33 +0200803 goto unlock;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530804
Oleg Nesterov78f74112012-08-08 17:35:08 +0200805 if (is_register)
Oleg Nesterov26872092012-06-15 17:43:33 +0200806 err = install_breakpoint(uprobe, mm, vma, info->vaddr);
Oleg Nesterov78f74112012-08-08 17:35:08 +0200807 else
Oleg Nesterov076a3652012-09-30 18:54:53 +0200808 err |= remove_breakpoint(uprobe, mm, info->vaddr);
Oleg Nesterov78f74112012-08-08 17:35:08 +0200809
Oleg Nesterov26872092012-06-15 17:43:33 +0200810 unlock:
811 up_write(&mm->mmap_sem);
812 free:
813 mmput(mm);
814 info = free_map_info(info);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530815 }
Oleg Nesterov32cdba12012-11-14 19:03:42 +0100816 out:
817 percpu_up_write(&dup_mmap_sem);
Oleg Nesterov26872092012-06-15 17:43:33 +0200818 return err;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530819}
820
Oleg Nesterov9a98e032012-11-23 20:15:17 +0100821static int __uprobe_register(struct uprobe *uprobe, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530822{
Oleg Nesterov9a98e032012-11-23 20:15:17 +0100823 consumer_add(uprobe, uc);
Oleg Nesterovbb929282012-11-24 18:27:08 +0100824 return register_for_each_vma(uprobe, true);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530825}
826
Oleg Nesterov04aab9b2012-11-23 19:43:50 +0100827static void __uprobe_unregister(struct uprobe *uprobe, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530828{
Oleg Nesterov04aab9b2012-11-23 19:43:50 +0100829 int err;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530830
Oleg Nesterov04aab9b2012-11-23 19:43:50 +0100831 if (!consumer_del(uprobe, uc)) /* WARN? */
832 return;
833
834 err = register_for_each_vma(uprobe, false);
Oleg Nesterovbb929282012-11-24 18:27:08 +0100835 /* TODO : cant unregister? schedule a worker thread */
836 if (!uprobe->consumers && !err)
837 delete_uprobe(uprobe);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530838}
839
840/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100841 * uprobe_register - register a probe
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530842 * @inode: the file in which the probe has to be placed.
843 * @offset: offset from the start of the file.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530844 * @uc: information on howto handle the probe..
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530845 *
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100846 * Apart from the access refcount, uprobe_register() takes a creation
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530847 * refcount (thro alloc_uprobe) if and only if this @uprobe is getting
848 * inserted into the rbtree (i.e first consumer for a @inode:@offset
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100849 * tuple). Creation refcount stops uprobe_unregister from freeing the
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530850 * @uprobe even before the register operation is complete. Creation
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530851 * refcount is released when the last @uc for the @uprobe
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530852 * unregisters.
853 *
854 * Return errno if it cannot successully install probes
855 * else return 0 (success)
856 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530857int uprobe_register(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530858{
859 struct uprobe *uprobe;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100860 int ret;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530861
Oleg Nesterovf0744af2012-11-21 18:01:43 +0100862 /* Racy, just to catch the obvious mistakes */
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530863 if (offset > i_size_read(inode))
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100864 return -EINVAL;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530865
Oleg Nesterov9a98e032012-11-23 20:15:17 +0100866 ret = -ENOMEM;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530867 mutex_lock(uprobes_hash(inode));
868 uprobe = alloc_uprobe(inode, offset);
Oleg Nesterov9a98e032012-11-23 20:15:17 +0100869 if (uprobe) {
Oleg Nesterove591c8d2012-11-24 17:29:40 +0100870 down_write(&uprobe->register_rwsem);
Oleg Nesterov9a98e032012-11-23 20:15:17 +0100871 ret = __uprobe_register(uprobe, uc);
872 if (ret)
Oleg Nesterov04aab9b2012-11-23 19:43:50 +0100873 __uprobe_unregister(uprobe, uc);
Oleg Nesterove591c8d2012-11-24 17:29:40 +0100874 up_write(&uprobe->register_rwsem);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530875 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530876 mutex_unlock(uprobes_hash(inode));
Sebastian Andrzej Siewior6d1d8df2012-08-30 19:26:22 +0200877 if (uprobe)
878 put_uprobe(uprobe);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530879
880 return ret;
881}
882
883/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100884 * uprobe_unregister - unregister a already registered probe.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530885 * @inode: the file in which the probe has to be removed.
886 * @offset: offset from the start of the file.
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530887 * @uc: identify which probe if multiple probes are colocated.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530888 */
Srikar Dronamrajue3343e62012-03-12 14:55:30 +0530889void uprobe_unregister(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530890{
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100891 struct uprobe *uprobe;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530892
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530893 uprobe = find_uprobe(inode, offset);
894 if (!uprobe)
895 return;
896
897 mutex_lock(uprobes_hash(inode));
Oleg Nesterove591c8d2012-11-24 17:29:40 +0100898 down_write(&uprobe->register_rwsem);
Oleg Nesterov04aab9b2012-11-23 19:43:50 +0100899 __uprobe_unregister(uprobe, uc);
Oleg Nesterove591c8d2012-11-24 17:29:40 +0100900 up_write(&uprobe->register_rwsem);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530901 mutex_unlock(uprobes_hash(inode));
Sasha Levinc91368c2012-12-20 14:11:30 -0500902 put_uprobe(uprobe);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530903}
904
Oleg Nesterov891c3972012-07-29 20:22:40 +0200905static struct rb_node *
906find_node_in_range(struct inode *inode, loff_t min, loff_t max)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530907{
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530908 struct rb_node *n = uprobes_tree.rb_node;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530909
910 while (n) {
Oleg Nesterov891c3972012-07-29 20:22:40 +0200911 struct uprobe *u = rb_entry(n, struct uprobe, rb_node);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100912
Oleg Nesterov891c3972012-07-29 20:22:40 +0200913 if (inode < u->inode) {
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530914 n = n->rb_left;
Oleg Nesterov891c3972012-07-29 20:22:40 +0200915 } else if (inode > u->inode) {
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530916 n = n->rb_right;
Oleg Nesterov891c3972012-07-29 20:22:40 +0200917 } else {
918 if (max < u->offset)
919 n = n->rb_left;
920 else if (min > u->offset)
921 n = n->rb_right;
922 else
923 break;
924 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530925 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100926
Oleg Nesterov891c3972012-07-29 20:22:40 +0200927 return n;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530928}
929
930/*
Oleg Nesterov891c3972012-07-29 20:22:40 +0200931 * For a given range in vma, build a list of probes that need to be inserted.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530932 */
Oleg Nesterov891c3972012-07-29 20:22:40 +0200933static void build_probe_list(struct inode *inode,
934 struct vm_area_struct *vma,
935 unsigned long start, unsigned long end,
936 struct list_head *head)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530937{
Oleg Nesterov891c3972012-07-29 20:22:40 +0200938 loff_t min, max;
Oleg Nesterov891c3972012-07-29 20:22:40 +0200939 struct rb_node *n, *t;
940 struct uprobe *u;
941
942 INIT_LIST_HEAD(head);
Oleg Nesterovcb113b42012-07-29 20:22:42 +0200943 min = vaddr_to_offset(vma, start);
Oleg Nesterov891c3972012-07-29 20:22:40 +0200944 max = min + (end - start) - 1;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530945
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200946 spin_lock(&uprobes_treelock);
Oleg Nesterov891c3972012-07-29 20:22:40 +0200947 n = find_node_in_range(inode, min, max);
948 if (n) {
949 for (t = n; t; t = rb_prev(t)) {
950 u = rb_entry(t, struct uprobe, rb_node);
951 if (u->inode != inode || u->offset < min)
952 break;
953 list_add(&u->pending_list, head);
954 atomic_inc(&u->ref);
955 }
956 for (t = n; (t = rb_next(t)); ) {
957 u = rb_entry(t, struct uprobe, rb_node);
958 if (u->inode != inode || u->offset > max)
959 break;
960 list_add(&u->pending_list, head);
961 atomic_inc(&u->ref);
962 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530963 }
Oleg Nesterov6f47caa2012-08-18 17:01:57 +0200964 spin_unlock(&uprobes_treelock);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530965}
966
967/*
Oleg Nesterov5e5be712012-08-06 14:49:56 +0200968 * Called from mmap_region/vma_adjust with mm->mmap_sem acquired.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530969 *
Oleg Nesterov5e5be712012-08-06 14:49:56 +0200970 * Currently we ignore all errors and always return 0, the callers
971 * can't handle the failure anyway.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530972 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100973int uprobe_mmap(struct vm_area_struct *vma)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530974{
975 struct list_head tmp_list;
Oleg Nesterov665605a2012-07-29 20:22:29 +0200976 struct uprobe *uprobe, *u;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530977 struct inode *inode;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530978
979 if (!atomic_read(&uprobe_events) || !valid_vma(vma, true))
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100980 return 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530981
982 inode = vma->vm_file->f_mapping->host;
983 if (!inode)
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100984 return 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530985
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530986 mutex_lock(uprobes_mmap_hash(inode));
Oleg Nesterov891c3972012-07-29 20:22:40 +0200987 build_probe_list(inode, vma, vma->vm_start, vma->vm_end, &tmp_list);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100988
Oleg Nesterov665605a2012-07-29 20:22:29 +0200989 list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
Oleg Nesterov5e5be712012-08-06 14:49:56 +0200990 if (!fatal_signal_pending(current)) {
Oleg Nesterov57683f72012-07-29 20:22:47 +0200991 unsigned long vaddr = offset_to_vaddr(vma, uprobe->offset);
Oleg Nesterov5e5be712012-08-06 14:49:56 +0200992 install_breakpoint(uprobe, vma->vm_mm, vma, vaddr);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530993 }
994 put_uprobe(uprobe);
995 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530996 mutex_unlock(uprobes_mmap_hash(inode));
997
Oleg Nesterov5e5be712012-08-06 14:49:56 +0200998 return 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530999}
1000
Oleg Nesterov9f68f672012-08-19 16:15:09 +02001001static bool
1002vma_has_uprobes(struct vm_area_struct *vma, unsigned long start, unsigned long end)
1003{
1004 loff_t min, max;
1005 struct inode *inode;
1006 struct rb_node *n;
1007
1008 inode = vma->vm_file->f_mapping->host;
1009
1010 min = vaddr_to_offset(vma, start);
1011 max = min + (end - start) - 1;
1012
1013 spin_lock(&uprobes_treelock);
1014 n = find_node_in_range(inode, min, max);
1015 spin_unlock(&uprobes_treelock);
1016
1017 return !!n;
1018}
1019
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301020/*
1021 * Called in context of a munmap of a vma.
1022 */
Srikar Dronamrajucbc91f72012-04-11 16:05:27 +05301023void uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end)
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301024{
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301025 if (!atomic_read(&uprobe_events) || !valid_vma(vma, false))
1026 return;
1027
Oleg Nesterov2fd611a2012-07-29 20:22:31 +02001028 if (!atomic_read(&vma->vm_mm->mm_users)) /* called by mmput() ? */
1029 return;
1030
Oleg Nesterov9f68f672012-08-19 16:15:09 +02001031 if (!test_bit(MMF_HAS_UPROBES, &vma->vm_mm->flags) ||
1032 test_bit(MMF_RECALC_UPROBES, &vma->vm_mm->flags))
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +02001033 return;
1034
Oleg Nesterov9f68f672012-08-19 16:15:09 +02001035 if (vma_has_uprobes(vma, start, end))
1036 set_bit(MMF_RECALC_UPROBES, &vma->vm_mm->flags);
Srikar Dronamraju682968e2012-03-30 23:56:46 +05301037}
1038
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301039/* Slot allocation for XOL */
1040static int xol_add_vma(struct xol_area *area)
1041{
1042 struct mm_struct *mm;
1043 int ret;
1044
1045 area->page = alloc_page(GFP_HIGHUSER);
1046 if (!area->page)
1047 return -ENOMEM;
1048
1049 ret = -EALREADY;
1050 mm = current->mm;
1051
1052 down_write(&mm->mmap_sem);
1053 if (mm->uprobes_state.xol_area)
1054 goto fail;
1055
1056 ret = -ENOMEM;
1057
1058 /* Try to map as high as possible, this is only a hint. */
1059 area->vaddr = get_unmapped_area(NULL, TASK_SIZE - PAGE_SIZE, PAGE_SIZE, 0, 0);
1060 if (area->vaddr & ~PAGE_MASK) {
1061 ret = area->vaddr;
1062 goto fail;
1063 }
1064
1065 ret = install_special_mapping(mm, area->vaddr, PAGE_SIZE,
1066 VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO, &area->page);
1067 if (ret)
1068 goto fail;
1069
1070 smp_wmb(); /* pairs with get_xol_area() */
1071 mm->uprobes_state.xol_area = area;
1072 ret = 0;
1073
1074fail:
1075 up_write(&mm->mmap_sem);
1076 if (ret)
1077 __free_page(area->page);
1078
1079 return ret;
1080}
1081
1082static struct xol_area *get_xol_area(struct mm_struct *mm)
1083{
1084 struct xol_area *area;
1085
1086 area = mm->uprobes_state.xol_area;
1087 smp_read_barrier_depends(); /* pairs with wmb in xol_add_vma() */
1088
1089 return area;
1090}
1091
1092/*
1093 * xol_alloc_area - Allocate process's xol_area.
1094 * This area will be used for storing instructions for execution out of
1095 * line.
1096 *
1097 * Returns the allocated area or NULL.
1098 */
1099static struct xol_area *xol_alloc_area(void)
1100{
1101 struct xol_area *area;
1102
1103 area = kzalloc(sizeof(*area), GFP_KERNEL);
1104 if (unlikely(!area))
1105 return NULL;
1106
1107 area->bitmap = kzalloc(BITS_TO_LONGS(UINSNS_PER_PAGE) * sizeof(long), GFP_KERNEL);
1108
1109 if (!area->bitmap)
1110 goto fail;
1111
1112 init_waitqueue_head(&area->wq);
1113 if (!xol_add_vma(area))
1114 return area;
1115
1116fail:
1117 kfree(area->bitmap);
1118 kfree(area);
1119
1120 return get_xol_area(current->mm);
1121}
1122
1123/*
1124 * uprobe_clear_state - Free the area allocated for slots.
1125 */
1126void uprobe_clear_state(struct mm_struct *mm)
1127{
1128 struct xol_area *area = mm->uprobes_state.xol_area;
1129
1130 if (!area)
1131 return;
1132
1133 put_page(area->page);
1134 kfree(area->bitmap);
1135 kfree(area);
1136}
1137
Oleg Nesterov32cdba12012-11-14 19:03:42 +01001138void uprobe_start_dup_mmap(void)
1139{
1140 percpu_down_read(&dup_mmap_sem);
1141}
1142
1143void uprobe_end_dup_mmap(void)
1144{
1145 percpu_up_read(&dup_mmap_sem);
1146}
1147
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +02001148void uprobe_dup_mmap(struct mm_struct *oldmm, struct mm_struct *newmm)
1149{
Oleg Nesterov61559a82012-08-08 17:17:46 +02001150 newmm->uprobes_state.xol_area = NULL;
1151
Oleg Nesterov9f68f672012-08-19 16:15:09 +02001152 if (test_bit(MMF_HAS_UPROBES, &oldmm->flags)) {
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +02001153 set_bit(MMF_HAS_UPROBES, &newmm->flags);
Oleg Nesterov9f68f672012-08-19 16:15:09 +02001154 /* unconditionally, dup_mmap() skips VM_DONTCOPY vmas */
1155 set_bit(MMF_RECALC_UPROBES, &newmm->flags);
1156 }
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +02001157}
1158
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301159/*
1160 * - search for a free slot.
1161 */
1162static unsigned long xol_take_insn_slot(struct xol_area *area)
1163{
1164 unsigned long slot_addr;
1165 int slot_nr;
1166
1167 do {
1168 slot_nr = find_first_zero_bit(area->bitmap, UINSNS_PER_PAGE);
1169 if (slot_nr < UINSNS_PER_PAGE) {
1170 if (!test_and_set_bit(slot_nr, area->bitmap))
1171 break;
1172
1173 slot_nr = UINSNS_PER_PAGE;
1174 continue;
1175 }
1176 wait_event(area->wq, (atomic_read(&area->slot_count) < UINSNS_PER_PAGE));
1177 } while (slot_nr >= UINSNS_PER_PAGE);
1178
1179 slot_addr = area->vaddr + (slot_nr * UPROBE_XOL_SLOT_BYTES);
1180 atomic_inc(&area->slot_count);
1181
1182 return slot_addr;
1183}
1184
1185/*
1186 * xol_get_insn_slot - If was not allocated a slot, then
1187 * allocate a slot.
1188 * Returns the allocated slot address or 0.
1189 */
1190static unsigned long xol_get_insn_slot(struct uprobe *uprobe, unsigned long slot_addr)
1191{
1192 struct xol_area *area;
1193 unsigned long offset;
1194 void *vaddr;
1195
1196 area = get_xol_area(current->mm);
1197 if (!area) {
1198 area = xol_alloc_area();
1199 if (!area)
1200 return 0;
1201 }
1202 current->utask->xol_vaddr = xol_take_insn_slot(area);
1203
1204 /*
1205 * Initialize the slot if xol_vaddr points to valid
1206 * instruction slot.
1207 */
1208 if (unlikely(!current->utask->xol_vaddr))
1209 return 0;
1210
1211 current->utask->vaddr = slot_addr;
1212 offset = current->utask->xol_vaddr & ~PAGE_MASK;
1213 vaddr = kmap_atomic(area->page);
1214 memcpy(vaddr + offset, uprobe->arch.insn, MAX_UINSN_BYTES);
1215 kunmap_atomic(vaddr);
Rabin Vincent65b6ecc2012-11-14 18:27:07 +01001216 /*
1217 * We probably need flush_icache_user_range() but it needs vma.
1218 * This should work on supported architectures too.
1219 */
1220 flush_dcache_page(area->page);
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301221
1222 return current->utask->xol_vaddr;
1223}
1224
1225/*
1226 * xol_free_insn_slot - If slot was earlier allocated by
1227 * @xol_get_insn_slot(), make the slot available for
1228 * subsequent requests.
1229 */
1230static void xol_free_insn_slot(struct task_struct *tsk)
1231{
1232 struct xol_area *area;
1233 unsigned long vma_end;
1234 unsigned long slot_addr;
1235
1236 if (!tsk->mm || !tsk->mm->uprobes_state.xol_area || !tsk->utask)
1237 return;
1238
1239 slot_addr = tsk->utask->xol_vaddr;
1240
1241 if (unlikely(!slot_addr || IS_ERR_VALUE(slot_addr)))
1242 return;
1243
1244 area = tsk->mm->uprobes_state.xol_area;
1245 vma_end = area->vaddr + PAGE_SIZE;
1246 if (area->vaddr <= slot_addr && slot_addr < vma_end) {
1247 unsigned long offset;
1248 int slot_nr;
1249
1250 offset = slot_addr - area->vaddr;
1251 slot_nr = offset / UPROBE_XOL_SLOT_BYTES;
1252 if (slot_nr >= UINSNS_PER_PAGE)
1253 return;
1254
1255 clear_bit(slot_nr, area->bitmap);
1256 atomic_dec(&area->slot_count);
1257 if (waitqueue_active(&area->wq))
1258 wake_up(&area->wq);
1259
1260 tsk->utask->xol_vaddr = 0;
1261 }
1262}
1263
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301264/**
1265 * uprobe_get_swbp_addr - compute address of swbp given post-swbp regs
1266 * @regs: Reflects the saved state of the task after it has hit a breakpoint
1267 * instruction.
1268 * Return the address of the breakpoint instruction.
1269 */
1270unsigned long __weak uprobe_get_swbp_addr(struct pt_regs *regs)
1271{
1272 return instruction_pointer(regs) - UPROBE_SWBP_INSN_SIZE;
1273}
1274
1275/*
1276 * Called with no locks held.
1277 * Called in context of a exiting or a exec-ing thread.
1278 */
1279void uprobe_free_utask(struct task_struct *t)
1280{
1281 struct uprobe_task *utask = t->utask;
1282
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301283 if (!utask)
1284 return;
1285
1286 if (utask->active_uprobe)
1287 put_uprobe(utask->active_uprobe);
1288
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301289 xol_free_insn_slot(t);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301290 kfree(utask);
1291 t->utask = NULL;
1292}
1293
1294/*
1295 * Called in context of a new clone/fork from copy_process.
1296 */
1297void uprobe_copy_process(struct task_struct *t)
1298{
1299 t->utask = NULL;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301300}
1301
1302/*
1303 * Allocate a uprobe_task object for the task.
1304 * Called when the thread hits a breakpoint for the first time.
1305 *
1306 * Returns:
1307 * - pointer to new uprobe_task on success
1308 * - NULL otherwise
1309 */
1310static struct uprobe_task *add_utask(void)
1311{
1312 struct uprobe_task *utask;
1313
1314 utask = kzalloc(sizeof *utask, GFP_KERNEL);
1315 if (unlikely(!utask))
1316 return NULL;
1317
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301318 current->utask = utask;
1319 return utask;
1320}
1321
1322/* Prepare to single-step probed instruction out of line. */
1323static int
1324pre_ssout(struct uprobe *uprobe, struct pt_regs *regs, unsigned long vaddr)
1325{
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301326 if (xol_get_insn_slot(uprobe, vaddr) && !arch_uprobe_pre_xol(&uprobe->arch, regs))
1327 return 0;
1328
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301329 return -EFAULT;
1330}
1331
1332/*
1333 * If we are singlestepping, then ensure this thread is not connected to
1334 * non-fatal signals until completion of singlestep. When xol insn itself
1335 * triggers the signal, restart the original insn even if the task is
1336 * already SIGKILL'ed (since coredump should report the correct ip). This
1337 * is even more important if the task has a handler for SIGSEGV/etc, The
1338 * _same_ instruction should be repeated again after return from the signal
1339 * handler, and SSTEP can never finish in this case.
1340 */
1341bool uprobe_deny_signal(void)
1342{
1343 struct task_struct *t = current;
1344 struct uprobe_task *utask = t->utask;
1345
1346 if (likely(!utask || !utask->active_uprobe))
1347 return false;
1348
1349 WARN_ON_ONCE(utask->state != UTASK_SSTEP);
1350
1351 if (signal_pending(t)) {
1352 spin_lock_irq(&t->sighand->siglock);
1353 clear_tsk_thread_flag(t, TIF_SIGPENDING);
1354 spin_unlock_irq(&t->sighand->siglock);
1355
1356 if (__fatal_signal_pending(t) || arch_uprobe_xol_was_trapped(t)) {
1357 utask->state = UTASK_SSTEP_TRAPPED;
1358 set_tsk_thread_flag(t, TIF_UPROBE);
1359 set_tsk_thread_flag(t, TIF_NOTIFY_RESUME);
1360 }
1361 }
1362
1363 return true;
1364}
1365
1366/*
1367 * Avoid singlestepping the original instruction if the original instruction
1368 * is a NOP or can be emulated.
1369 */
1370static bool can_skip_sstep(struct uprobe *uprobe, struct pt_regs *regs)
1371{
Oleg Nesterov71434f22012-09-30 21:12:44 +02001372 if (test_bit(UPROBE_SKIP_SSTEP, &uprobe->flags)) {
Oleg Nesterov0578a972012-09-14 18:31:23 +02001373 if (arch_uprobe_skip_sstep(&uprobe->arch, regs))
1374 return true;
Oleg Nesterov71434f22012-09-30 21:12:44 +02001375 clear_bit(UPROBE_SKIP_SSTEP, &uprobe->flags);
Oleg Nesterov0578a972012-09-14 18:31:23 +02001376 }
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301377 return false;
1378}
1379
Oleg Nesterov499a4f32012-08-19 17:41:34 +02001380static void mmf_recalc_uprobes(struct mm_struct *mm)
1381{
1382 struct vm_area_struct *vma;
1383
1384 for (vma = mm->mmap; vma; vma = vma->vm_next) {
1385 if (!valid_vma(vma, false))
1386 continue;
1387 /*
1388 * This is not strictly accurate, we can race with
1389 * uprobe_unregister() and see the already removed
1390 * uprobe if delete_uprobe() was not yet called.
Oleg Nesterov63633cb2012-11-22 18:30:15 +01001391 * Or this uprobe can be filtered out.
Oleg Nesterov499a4f32012-08-19 17:41:34 +02001392 */
1393 if (vma_has_uprobes(vma, vma->vm_start, vma->vm_end))
1394 return;
1395 }
1396
1397 clear_bit(MMF_HAS_UPROBES, &mm->flags);
1398}
1399
Oleg Nesterovec75fba2012-09-23 21:55:19 +02001400static int is_swbp_at_addr(struct mm_struct *mm, unsigned long vaddr)
1401{
1402 struct page *page;
1403 uprobe_opcode_t opcode;
1404 int result;
1405
1406 pagefault_disable();
1407 result = __copy_from_user_inatomic(&opcode, (void __user*)vaddr,
1408 sizeof(opcode));
1409 pagefault_enable();
1410
1411 if (likely(result == 0))
1412 goto out;
1413
1414 result = get_user_pages(NULL, mm, vaddr, 1, 0, 1, &page, NULL);
1415 if (result < 0)
1416 return result;
1417
1418 copy_opcode(page, vaddr, &opcode);
1419 put_page(page);
1420 out:
1421 return is_swbp_insn(&opcode);
1422}
1423
Oleg Nesterovd790d342012-05-29 21:29:14 +02001424static struct uprobe *find_active_uprobe(unsigned long bp_vaddr, int *is_swbp)
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001425{
1426 struct mm_struct *mm = current->mm;
1427 struct uprobe *uprobe = NULL;
1428 struct vm_area_struct *vma;
1429
1430 down_read(&mm->mmap_sem);
1431 vma = find_vma(mm, bp_vaddr);
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001432 if (vma && vma->vm_start <= bp_vaddr) {
1433 if (valid_vma(vma, false)) {
Oleg Nesterovcb113b42012-07-29 20:22:42 +02001434 struct inode *inode = vma->vm_file->f_mapping->host;
1435 loff_t offset = vaddr_to_offset(vma, bp_vaddr);
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001436
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001437 uprobe = find_uprobe(inode, offset);
1438 }
Oleg Nesterovd790d342012-05-29 21:29:14 +02001439
1440 if (!uprobe)
1441 *is_swbp = is_swbp_at_addr(mm, bp_vaddr);
1442 } else {
1443 *is_swbp = -EFAULT;
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001444 }
Oleg Nesterov499a4f32012-08-19 17:41:34 +02001445
1446 if (!uprobe && test_and_clear_bit(MMF_RECALC_UPROBES, &mm->flags))
1447 mmf_recalc_uprobes(mm);
Oleg Nesterov3a9ea052012-05-29 21:28:57 +02001448 up_read(&mm->mmap_sem);
1449
1450 return uprobe;
1451}
1452
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301453/*
1454 * Run handler and ask thread to singlestep.
1455 * Ensure all non-fatal signals cannot interrupt thread while it singlesteps.
1456 */
1457static void handle_swbp(struct pt_regs *regs)
1458{
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301459 struct uprobe_task *utask;
1460 struct uprobe *uprobe;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301461 unsigned long bp_vaddr;
Oleg Nesterov56bb4cf2012-05-29 21:29:47 +02001462 int uninitialized_var(is_swbp);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301463
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301464 bp_vaddr = uprobe_get_swbp_addr(regs);
Oleg Nesterovd790d342012-05-29 21:29:14 +02001465 uprobe = find_active_uprobe(bp_vaddr, &is_swbp);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301466
1467 if (!uprobe) {
Oleg Nesterov56bb4cf2012-05-29 21:29:47 +02001468 if (is_swbp > 0) {
1469 /* No matching uprobe; signal SIGTRAP. */
1470 send_sig(SIGTRAP, current, 0);
1471 } else {
1472 /*
1473 * Either we raced with uprobe_unregister() or we can't
1474 * access this memory. The latter is only possible if
1475 * another thread plays with our ->mm. In both cases
1476 * we can simply restart. If this vma was unmapped we
1477 * can pretend this insn was not executed yet and get
1478 * the (correct) SIGSEGV after restart.
1479 */
1480 instruction_pointer_set(regs, bp_vaddr);
1481 }
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301482 return;
1483 }
Oleg Nesterov142b18d2012-09-29 21:56:57 +02001484 /*
1485 * TODO: move copy_insn/etc into _register and remove this hack.
1486 * After we hit the bp, _unregister + _register can install the
1487 * new and not-yet-analyzed uprobe at the same address, restart.
1488 */
1489 smp_rmb(); /* pairs with wmb() in install_breakpoint() */
Oleg Nesterov71434f22012-09-30 21:12:44 +02001490 if (unlikely(!test_bit(UPROBE_COPY_INSN, &uprobe->flags)))
Oleg Nesterov142b18d2012-09-29 21:56:57 +02001491 goto restart;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301492
1493 utask = current->utask;
1494 if (!utask) {
1495 utask = add_utask();
1496 /* Cannot allocate; re-execute the instruction. */
1497 if (!utask)
Oleg Nesterov0578a972012-09-14 18:31:23 +02001498 goto restart;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301499 }
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301500
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301501 handler_chain(uprobe, regs);
Oleg Nesterov0578a972012-09-14 18:31:23 +02001502 if (can_skip_sstep(uprobe, regs))
1503 goto out;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301504
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301505 if (!pre_ssout(uprobe, regs, bp_vaddr)) {
Oleg Nesterov746a9e62012-09-14 18:23:51 +02001506 utask->active_uprobe = uprobe;
1507 utask->state = UTASK_SSTEP;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301508 return;
1509 }
1510
Oleg Nesterov0578a972012-09-14 18:31:23 +02001511restart:
1512 /*
1513 * cannot singlestep; cannot skip instruction;
1514 * re-execute the instruction.
1515 */
1516 instruction_pointer_set(regs, bp_vaddr);
1517out:
Sebastian Andrzej Siewior8bd87442012-08-07 18:12:30 +02001518 put_uprobe(uprobe);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301519}
1520
1521/*
1522 * Perform required fix-ups and disable singlestep.
1523 * Allow pending signals to take effect.
1524 */
1525static void handle_singlestep(struct uprobe_task *utask, struct pt_regs *regs)
1526{
1527 struct uprobe *uprobe;
1528
1529 uprobe = utask->active_uprobe;
1530 if (utask->state == UTASK_SSTEP_ACK)
1531 arch_uprobe_post_xol(&uprobe->arch, regs);
1532 else if (utask->state == UTASK_SSTEP_TRAPPED)
1533 arch_uprobe_abort_xol(&uprobe->arch, regs);
1534 else
1535 WARN_ON_ONCE(1);
1536
1537 put_uprobe(uprobe);
1538 utask->active_uprobe = NULL;
1539 utask->state = UTASK_RUNNING;
Srikar Dronamrajud4b3b632012-03-30 23:56:31 +05301540 xol_free_insn_slot(current);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301541
1542 spin_lock_irq(&current->sighand->siglock);
1543 recalc_sigpending(); /* see uprobe_deny_signal() */
1544 spin_unlock_irq(&current->sighand->siglock);
1545}
1546
1547/*
Oleg Nesterov1b08e9072012-09-14 18:52:10 +02001548 * On breakpoint hit, breakpoint notifier sets the TIF_UPROBE flag and
1549 * allows the thread to return from interrupt. After that handle_swbp()
1550 * sets utask->active_uprobe.
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301551 *
Oleg Nesterov1b08e9072012-09-14 18:52:10 +02001552 * On singlestep exception, singlestep notifier sets the TIF_UPROBE flag
1553 * and allows the thread to return from interrupt.
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301554 *
1555 * While returning to userspace, thread notices the TIF_UPROBE flag and calls
1556 * uprobe_notify_resume().
1557 */
1558void uprobe_notify_resume(struct pt_regs *regs)
1559{
1560 struct uprobe_task *utask;
1561
Oleg Nesterovdb023ea2012-09-14 19:05:46 +02001562 clear_thread_flag(TIF_UPROBE);
1563
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301564 utask = current->utask;
Oleg Nesterov1b08e9072012-09-14 18:52:10 +02001565 if (utask && utask->active_uprobe)
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301566 handle_singlestep(utask, regs);
Oleg Nesterov1b08e9072012-09-14 18:52:10 +02001567 else
1568 handle_swbp(regs);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301569}
1570
1571/*
1572 * uprobe_pre_sstep_notifier gets called from interrupt context as part of
1573 * notifier mechanism. Set TIF_UPROBE flag and indicate breakpoint hit.
1574 */
1575int uprobe_pre_sstep_notifier(struct pt_regs *regs)
1576{
Oleg Nesterovf8ac4ec2012-08-08 17:11:42 +02001577 if (!current->mm || !test_bit(MMF_HAS_UPROBES, &current->mm->flags))
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301578 return 0;
1579
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301580 set_thread_flag(TIF_UPROBE);
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301581 return 1;
1582}
1583
1584/*
1585 * uprobe_post_sstep_notifier gets called in interrupt context as part of notifier
1586 * mechanism. Set TIF_UPROBE flag and indicate completion of singlestep.
1587 */
1588int uprobe_post_sstep_notifier(struct pt_regs *regs)
1589{
1590 struct uprobe_task *utask = current->utask;
1591
1592 if (!current->mm || !utask || !utask->active_uprobe)
1593 /* task is currently not uprobed */
1594 return 0;
1595
1596 utask->state = UTASK_SSTEP_ACK;
1597 set_thread_flag(TIF_UPROBE);
1598 return 1;
1599}
1600
1601static struct notifier_block uprobe_exception_nb = {
1602 .notifier_call = arch_uprobe_exception_notify,
1603 .priority = INT_MAX-1, /* notified after kprobes, kgdb */
1604};
1605
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301606static int __init init_uprobes(void)
1607{
1608 int i;
1609
1610 for (i = 0; i < UPROBES_HASH_SZ; i++) {
1611 mutex_init(&uprobes_mutex[i]);
1612 mutex_init(&uprobes_mmap_mutex[i]);
1613 }
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301614
Oleg Nesterov32cdba12012-11-14 19:03:42 +01001615 if (percpu_init_rwsem(&dup_mmap_sem))
1616 return -ENOMEM;
1617
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301618 return register_die_notifier(&uprobe_exception_nb);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301619}
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05301620module_init(init_uprobes);
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301621
1622static void __exit exit_uprobes(void)
1623{
1624}
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301625module_exit(exit_uprobes);