blob: 5ce32e3ae9e9ae49866621581afc7c22319a4906 [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 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010033
Srikar Dronamraju2b144492012-02-09 14:56:42 +053034#include <linux/uprobes.h>
35
36static struct rb_root uprobes_tree = RB_ROOT;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010037
Srikar Dronamraju2b144492012-02-09 14:56:42 +053038static DEFINE_SPINLOCK(uprobes_treelock); /* serialize rbtree access */
39
40#define UPROBES_HASH_SZ 13
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010041
Srikar Dronamraju2b144492012-02-09 14:56:42 +053042/* serialize (un)register */
43static struct mutex uprobes_mutex[UPROBES_HASH_SZ];
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010044
45#define uprobes_hash(v) (&uprobes_mutex[((unsigned long)(v)) % UPROBES_HASH_SZ])
Srikar Dronamraju2b144492012-02-09 14:56:42 +053046
47/* serialize uprobe->pending_list */
48static struct mutex uprobes_mmap_mutex[UPROBES_HASH_SZ];
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010049#define uprobes_mmap_hash(v) (&uprobes_mmap_mutex[((unsigned long)(v)) % UPROBES_HASH_SZ])
Srikar Dronamraju2b144492012-02-09 14:56:42 +053050
51/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010052 * uprobe_events allows us to skip the uprobe_mmap if there are no uprobe
Srikar Dronamraju2b144492012-02-09 14:56:42 +053053 * events active at this time. Probably a fine grained per inode count is
54 * better?
55 */
56static atomic_t uprobe_events = ATOMIC_INIT(0);
57
58/*
59 * Maintain a temporary per vma info that can be used to search if a vma
60 * has already been handled. This structure is introduced since extending
61 * vm_area_struct wasnt recommended.
62 */
63struct vma_info {
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010064 struct list_head probe_list;
65 struct mm_struct *mm;
66 loff_t vaddr;
Srikar Dronamraju2b144492012-02-09 14:56:42 +053067};
68
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +053069struct uprobe {
70 struct rb_node rb_node; /* node in the rb tree */
71 atomic_t ref;
72 struct rw_semaphore consumer_rwsem;
73 struct list_head pending_list;
74 struct uprobe_consumer *consumers;
75 struct inode *inode; /* Also hold a ref to inode */
76 loff_t offset;
77 int flags;
78 struct arch_uprobe arch;
79};
80
Srikar Dronamraju2b144492012-02-09 14:56:42 +053081/*
82 * valid_vma: Verify if the specified vma is an executable vma
83 * Relax restrictions while unregistering: vm_flags might have
84 * changed after breakpoint was inserted.
85 * - is_register: indicates if we are in register context.
86 * - Return 1 if the specified virtual address is in an
87 * executable vma.
88 */
89static bool valid_vma(struct vm_area_struct *vma, bool is_register)
90{
91 if (!vma->vm_file)
92 return false;
93
94 if (!is_register)
95 return true;
96
Ingo Molnar7b2d81d2012-02-17 09:27:41 +010097 if ((vma->vm_flags & (VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)) == (VM_READ|VM_EXEC))
Srikar Dronamraju2b144492012-02-09 14:56:42 +053098 return true;
99
100 return false;
101}
102
103static loff_t vma_address(struct vm_area_struct *vma, loff_t offset)
104{
105 loff_t vaddr;
106
107 vaddr = vma->vm_start + offset;
108 vaddr -= vma->vm_pgoff << PAGE_SHIFT;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100109
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530110 return vaddr;
111}
112
113/**
114 * __replace_page - replace page in vma by new page.
115 * based on replace_page in mm/ksm.c
116 *
117 * @vma: vma that holds the pte pointing to page
118 * @page: the cowed page we are replacing by kpage
119 * @kpage: the modified page we replace page by
120 *
121 * Returns 0 on success, -EFAULT on failure.
122 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100123static int __replace_page(struct vm_area_struct *vma, struct page *page, struct page *kpage)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530124{
125 struct mm_struct *mm = vma->vm_mm;
126 pgd_t *pgd;
127 pud_t *pud;
128 pmd_t *pmd;
129 pte_t *ptep;
130 spinlock_t *ptl;
131 unsigned long addr;
132 int err = -EFAULT;
133
134 addr = page_address_in_vma(page, vma);
135 if (addr == -EFAULT)
136 goto out;
137
138 pgd = pgd_offset(mm, addr);
139 if (!pgd_present(*pgd))
140 goto out;
141
142 pud = pud_offset(pgd, addr);
143 if (!pud_present(*pud))
144 goto out;
145
146 pmd = pmd_offset(pud, addr);
147 if (!pmd_present(*pmd))
148 goto out;
149
150 ptep = pte_offset_map_lock(mm, pmd, addr, &ptl);
151 if (!ptep)
152 goto out;
153
154 get_page(kpage);
155 page_add_new_anon_rmap(kpage, vma, addr);
156
157 flush_cache_page(vma, addr, pte_pfn(*ptep));
158 ptep_clear_flush(vma, addr, ptep);
159 set_pte_at_notify(mm, addr, ptep, mk_pte(kpage, vma->vm_page_prot));
160
161 page_remove_rmap(page);
162 if (!page_mapped(page))
163 try_to_free_swap(page);
164 put_page(page);
165 pte_unmap_unlock(ptep, ptl);
166 err = 0;
167
168out:
169 return err;
170}
171
172/**
173 * is_bkpt_insn - check if instruction is breakpoint instruction.
174 * @insn: instruction to be checked.
175 * Default implementation of is_bkpt_insn
176 * Returns true if @insn is a breakpoint instruction.
177 */
178bool __weak is_bkpt_insn(uprobe_opcode_t *insn)
179{
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100180 return *insn == UPROBES_BKPT_INSN;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530181}
182
183/*
184 * NOTE:
185 * Expect the breakpoint instruction to be the smallest size instruction for
186 * the architecture. If an arch has variable length instruction and the
187 * breakpoint instruction is not of the smallest length instruction
188 * supported by that architecture then we need to modify read_opcode /
189 * write_opcode accordingly. This would never be a problem for archs that
190 * have fixed length instructions.
191 */
192
193/*
194 * write_opcode - write the opcode at a given virtual address.
195 * @mm: the probed process address space.
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +0530196 * @arch_uprobe: the breakpointing information.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530197 * @vaddr: the virtual address to store the opcode.
198 * @opcode: opcode to be written at @vaddr.
199 *
200 * Called with mm->mmap_sem held (for read and with a reference to
201 * mm).
202 *
203 * For mm @mm, write the opcode at @vaddr.
204 * Return 0 (success) or a negative errno.
205 */
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +0530206static int write_opcode(struct mm_struct *mm, struct arch_uprobe *auprobe,
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530207 unsigned long vaddr, uprobe_opcode_t opcode)
208{
209 struct page *old_page, *new_page;
210 struct address_space *mapping;
211 void *vaddr_old, *vaddr_new;
212 struct vm_area_struct *vma;
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +0530213 struct uprobe *uprobe;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530214 loff_t addr;
215 int ret;
216
217 /* Read the page with vaddr into memory */
218 ret = get_user_pages(NULL, mm, vaddr, 1, 0, 0, &old_page, &vma);
219 if (ret <= 0)
220 return ret;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100221
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530222 ret = -EINVAL;
223
224 /*
225 * We are interested in text pages only. Our pages of interest
226 * should be mapped for read and execute only. We desist from
227 * adding probes in write mapped pages since the breakpoints
228 * might end up in the file copy.
229 */
230 if (!valid_vma(vma, is_bkpt_insn(&opcode)))
231 goto put_out;
232
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +0530233 uprobe = container_of(auprobe, struct uprobe, arch);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530234 mapping = uprobe->inode->i_mapping;
235 if (mapping != vma->vm_file->f_mapping)
236 goto put_out;
237
238 addr = vma_address(vma, uprobe->offset);
239 if (vaddr != (unsigned long)addr)
240 goto put_out;
241
242 ret = -ENOMEM;
243 new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vaddr);
244 if (!new_page)
245 goto put_out;
246
247 __SetPageUptodate(new_page);
248
249 /*
250 * lock page will serialize against do_wp_page()'s
251 * PageAnon() handling
252 */
253 lock_page(old_page);
254 /* copy the page now that we've got it stable */
255 vaddr_old = kmap_atomic(old_page);
256 vaddr_new = kmap_atomic(new_page);
257
258 memcpy(vaddr_new, vaddr_old, PAGE_SIZE);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100259
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530260 /* poke the new insn in, ASSUMES we don't cross page boundary */
261 vaddr &= ~PAGE_MASK;
Srikar Dronamraju96379f62012-02-22 14:45:49 +0530262 BUG_ON(vaddr + UPROBES_BKPT_INSN_SIZE > PAGE_SIZE);
263 memcpy(vaddr_new + vaddr, &opcode, UPROBES_BKPT_INSN_SIZE);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530264
265 kunmap_atomic(vaddr_new);
266 kunmap_atomic(vaddr_old);
267
268 ret = anon_vma_prepare(vma);
269 if (ret)
270 goto unlock_out;
271
272 lock_page(new_page);
273 ret = __replace_page(vma, old_page, new_page);
274 unlock_page(new_page);
275
276unlock_out:
277 unlock_page(old_page);
278 page_cache_release(new_page);
279
280put_out:
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100281 put_page(old_page);
282
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530283 return ret;
284}
285
286/**
287 * read_opcode - read the opcode at a given virtual address.
288 * @mm: the probed process address space.
289 * @vaddr: the virtual address to read the opcode.
290 * @opcode: location to store the read opcode.
291 *
292 * Called with mm->mmap_sem held (for read and with a reference to
293 * mm.
294 *
295 * For mm @mm, read the opcode at @vaddr and store it in @opcode.
296 * Return 0 (success) or a negative errno.
297 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100298static int read_opcode(struct mm_struct *mm, unsigned long vaddr, uprobe_opcode_t *opcode)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530299{
300 struct page *page;
301 void *vaddr_new;
302 int ret;
303
304 ret = get_user_pages(NULL, mm, vaddr, 1, 0, 0, &page, NULL);
305 if (ret <= 0)
306 return ret;
307
308 lock_page(page);
309 vaddr_new = kmap_atomic(page);
310 vaddr &= ~PAGE_MASK;
Srikar Dronamraju96379f62012-02-22 14:45:49 +0530311 memcpy(opcode, vaddr_new + vaddr, UPROBES_BKPT_INSN_SIZE);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530312 kunmap_atomic(vaddr_new);
313 unlock_page(page);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100314
315 put_page(page);
316
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530317 return 0;
318}
319
320static int is_bkpt_at_addr(struct mm_struct *mm, unsigned long vaddr)
321{
322 uprobe_opcode_t opcode;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100323 int result;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530324
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100325 result = read_opcode(mm, vaddr, &opcode);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530326 if (result)
327 return result;
328
329 if (is_bkpt_insn(&opcode))
330 return 1;
331
332 return 0;
333}
334
335/**
336 * set_bkpt - store breakpoint at a given address.
337 * @mm: the probed process address space.
338 * @uprobe: the probepoint information.
339 * @vaddr: the virtual address to insert the opcode.
340 *
341 * For mm @mm, store the breakpoint instruction at @vaddr.
342 * Return 0 (success) or a negative errno.
343 */
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +0530344int __weak set_bkpt(struct mm_struct *mm, struct arch_uprobe *auprobe, unsigned long vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530345{
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100346 int result;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530347
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100348 result = is_bkpt_at_addr(mm, vaddr);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530349 if (result == 1)
350 return -EEXIST;
351
352 if (result)
353 return result;
354
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +0530355 return write_opcode(mm, auprobe, vaddr, UPROBES_BKPT_INSN);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530356}
357
358/**
359 * set_orig_insn - Restore the original instruction.
360 * @mm: the probed process address space.
361 * @uprobe: the probepoint information.
362 * @vaddr: the virtual address to insert the opcode.
363 * @verify: if true, verify existance of breakpoint instruction.
364 *
365 * For mm @mm, restore the original opcode (opcode) at @vaddr.
366 * Return 0 (success) or a negative errno.
367 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100368int __weak
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +0530369set_orig_insn(struct mm_struct *mm, struct arch_uprobe *auprobe, unsigned long vaddr, bool verify)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530370{
371 if (verify) {
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100372 int result;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530373
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100374 result = is_bkpt_at_addr(mm, vaddr);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530375 if (!result)
376 return -EINVAL;
377
378 if (result != 1)
379 return result;
380 }
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +0530381 return write_opcode(mm, auprobe, vaddr, *(uprobe_opcode_t *)auprobe->insn);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530382}
383
384static int match_uprobe(struct uprobe *l, struct uprobe *r)
385{
386 if (l->inode < r->inode)
387 return -1;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100388
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530389 if (l->inode > r->inode)
390 return 1;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530391
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100392 if (l->offset < r->offset)
393 return -1;
394
395 if (l->offset > r->offset)
396 return 1;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530397
398 return 0;
399}
400
401static struct uprobe *__find_uprobe(struct inode *inode, loff_t offset)
402{
403 struct uprobe u = { .inode = inode, .offset = offset };
404 struct rb_node *n = uprobes_tree.rb_node;
405 struct uprobe *uprobe;
406 int match;
407
408 while (n) {
409 uprobe = rb_entry(n, struct uprobe, rb_node);
410 match = match_uprobe(&u, uprobe);
411 if (!match) {
412 atomic_inc(&uprobe->ref);
413 return uprobe;
414 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100415
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530416 if (match < 0)
417 n = n->rb_left;
418 else
419 n = n->rb_right;
420 }
421 return NULL;
422}
423
424/*
425 * Find a uprobe corresponding to a given inode:offset
426 * Acquires uprobes_treelock
427 */
428static struct uprobe *find_uprobe(struct inode *inode, loff_t offset)
429{
430 struct uprobe *uprobe;
431 unsigned long flags;
432
433 spin_lock_irqsave(&uprobes_treelock, flags);
434 uprobe = __find_uprobe(inode, offset);
435 spin_unlock_irqrestore(&uprobes_treelock, flags);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100436
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530437 return uprobe;
438}
439
440static struct uprobe *__insert_uprobe(struct uprobe *uprobe)
441{
442 struct rb_node **p = &uprobes_tree.rb_node;
443 struct rb_node *parent = NULL;
444 struct uprobe *u;
445 int match;
446
447 while (*p) {
448 parent = *p;
449 u = rb_entry(parent, struct uprobe, rb_node);
450 match = match_uprobe(uprobe, u);
451 if (!match) {
452 atomic_inc(&u->ref);
453 return u;
454 }
455
456 if (match < 0)
457 p = &parent->rb_left;
458 else
459 p = &parent->rb_right;
460
461 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100462
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530463 u = NULL;
464 rb_link_node(&uprobe->rb_node, parent, p);
465 rb_insert_color(&uprobe->rb_node, &uprobes_tree);
466 /* get access + creation ref */
467 atomic_set(&uprobe->ref, 2);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100468
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530469 return u;
470}
471
472/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100473 * Acquire uprobes_treelock.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530474 * Matching uprobe already exists in rbtree;
475 * increment (access refcount) and return the matching uprobe.
476 *
477 * No matching uprobe; insert the uprobe in rb_tree;
478 * get a double refcount (access + creation) and return NULL.
479 */
480static struct uprobe *insert_uprobe(struct uprobe *uprobe)
481{
482 unsigned long flags;
483 struct uprobe *u;
484
485 spin_lock_irqsave(&uprobes_treelock, flags);
486 u = __insert_uprobe(uprobe);
487 spin_unlock_irqrestore(&uprobes_treelock, flags);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100488
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530489 return u;
490}
491
492static void put_uprobe(struct uprobe *uprobe)
493{
494 if (atomic_dec_and_test(&uprobe->ref))
495 kfree(uprobe);
496}
497
498static struct uprobe *alloc_uprobe(struct inode *inode, loff_t offset)
499{
500 struct uprobe *uprobe, *cur_uprobe;
501
502 uprobe = kzalloc(sizeof(struct uprobe), GFP_KERNEL);
503 if (!uprobe)
504 return NULL;
505
506 uprobe->inode = igrab(inode);
507 uprobe->offset = offset;
508 init_rwsem(&uprobe->consumer_rwsem);
509 INIT_LIST_HEAD(&uprobe->pending_list);
510
511 /* add to uprobes_tree, sorted on inode:offset */
512 cur_uprobe = insert_uprobe(uprobe);
513
514 /* a uprobe exists for this inode:offset combination */
515 if (cur_uprobe) {
516 kfree(uprobe);
517 uprobe = cur_uprobe;
518 iput(inode);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100519 } else {
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530520 atomic_inc(&uprobe_events);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100521 }
522
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530523 return uprobe;
524}
525
526/* Returns the previous consumer */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100527static struct uprobe_consumer *
528consumer_add(struct uprobe *uprobe, struct uprobe_consumer *consumer)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530529{
530 down_write(&uprobe->consumer_rwsem);
531 consumer->next = uprobe->consumers;
532 uprobe->consumers = consumer;
533 up_write(&uprobe->consumer_rwsem);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100534
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530535 return consumer->next;
536}
537
538/*
539 * For uprobe @uprobe, delete the consumer @consumer.
540 * Return true if the @consumer is deleted successfully
541 * or return false.
542 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100543static bool consumer_del(struct uprobe *uprobe, struct uprobe_consumer *consumer)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530544{
545 struct uprobe_consumer **con;
546 bool ret = false;
547
548 down_write(&uprobe->consumer_rwsem);
549 for (con = &uprobe->consumers; *con; con = &(*con)->next) {
550 if (*con == consumer) {
551 *con = consumer->next;
552 ret = true;
553 break;
554 }
555 }
556 up_write(&uprobe->consumer_rwsem);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100557
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530558 return ret;
559}
560
561static int __copy_insn(struct address_space *mapping,
562 struct vm_area_struct *vma, char *insn,
563 unsigned long nbytes, unsigned long offset)
564{
565 struct file *filp = vma->vm_file;
566 struct page *page;
567 void *vaddr;
568 unsigned long off1;
569 unsigned long idx;
570
571 if (!filp)
572 return -EINVAL;
573
574 idx = (unsigned long)(offset >> PAGE_CACHE_SHIFT);
575 off1 = offset &= ~PAGE_MASK;
576
577 /*
578 * Ensure that the page that has the original instruction is
579 * populated and in page-cache.
580 */
581 page = read_mapping_page(mapping, idx, filp);
582 if (IS_ERR(page))
583 return PTR_ERR(page);
584
585 vaddr = kmap_atomic(page);
586 memcpy(insn, vaddr + off1, nbytes);
587 kunmap_atomic(vaddr);
588 page_cache_release(page);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100589
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530590 return 0;
591}
592
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100593static int copy_insn(struct uprobe *uprobe, struct vm_area_struct *vma, unsigned long addr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530594{
595 struct address_space *mapping;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530596 unsigned long nbytes;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100597 int bytes;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530598
599 addr &= ~PAGE_MASK;
600 nbytes = PAGE_SIZE - addr;
601 mapping = uprobe->inode->i_mapping;
602
603 /* Instruction at end of binary; copy only available bytes */
604 if (uprobe->offset + MAX_UINSN_BYTES > uprobe->inode->i_size)
605 bytes = uprobe->inode->i_size - uprobe->offset;
606 else
607 bytes = MAX_UINSN_BYTES;
608
609 /* Instruction at the page-boundary; copy bytes in second page */
610 if (nbytes < bytes) {
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +0530611 if (__copy_insn(mapping, vma, uprobe->arch.insn + nbytes,
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530612 bytes - nbytes, uprobe->offset + nbytes))
613 return -ENOMEM;
614
615 bytes = nbytes;
616 }
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +0530617 return __copy_insn(mapping, vma, uprobe->arch.insn, bytes, uprobe->offset);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530618}
619
620static int install_breakpoint(struct mm_struct *mm, struct uprobe *uprobe,
621 struct vm_area_struct *vma, loff_t vaddr)
622{
623 unsigned long addr;
624 int ret;
625
626 /*
627 * If probe is being deleted, unregister thread could be done with
628 * the vma-rmap-walk through. Adding a probe now can be fatal since
629 * nobody will be able to cleanup. Also we could be from fork or
630 * mremap path, where the probe might have already been inserted.
631 * Hence behave as if probe already existed.
632 */
633 if (!uprobe->consumers)
634 return -EEXIST;
635
636 addr = (unsigned long)vaddr;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100637
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530638 if (!(uprobe->flags & UPROBES_COPY_INSN)) {
639 ret = copy_insn(uprobe, vma, addr);
640 if (ret)
641 return ret;
642
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +0530643 if (is_bkpt_insn((uprobe_opcode_t *)uprobe->arch.insn))
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530644 return -EEXIST;
645
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +0530646 ret = arch_uprobes_analyze_insn(mm, &uprobe->arch);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530647 if (ret)
648 return ret;
649
650 uprobe->flags |= UPROBES_COPY_INSN;
651 }
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +0530652 ret = set_bkpt(mm, &uprobe->arch, addr);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530653
654 return ret;
655}
656
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100657static void remove_breakpoint(struct mm_struct *mm, struct uprobe *uprobe, loff_t vaddr)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530658{
Srikar Dronamraju3ff54ef2012-02-22 14:46:02 +0530659 set_orig_insn(mm, &uprobe->arch, (unsigned long)vaddr, true);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530660}
661
662static void delete_uprobe(struct uprobe *uprobe)
663{
664 unsigned long flags;
665
666 spin_lock_irqsave(&uprobes_treelock, flags);
667 rb_erase(&uprobe->rb_node, &uprobes_tree);
668 spin_unlock_irqrestore(&uprobes_treelock, flags);
669 iput(uprobe->inode);
670 put_uprobe(uprobe);
671 atomic_dec(&uprobe_events);
672}
673
674static struct vma_info *__find_next_vma_info(struct list_head *head,
675 loff_t offset, struct address_space *mapping,
676 struct vma_info *vi, bool is_register)
677{
678 struct prio_tree_iter iter;
679 struct vm_area_struct *vma;
680 struct vma_info *tmpvi;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100681 unsigned long pgoff;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530682 int existing_vma;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100683 loff_t vaddr;
684
685 pgoff = offset >> PAGE_SHIFT;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530686
687 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
688 if (!valid_vma(vma, is_register))
689 continue;
690
691 existing_vma = 0;
692 vaddr = vma_address(vma, offset);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100693
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530694 list_for_each_entry(tmpvi, head, probe_list) {
695 if (tmpvi->mm == vma->vm_mm && tmpvi->vaddr == vaddr) {
696 existing_vma = 1;
697 break;
698 }
699 }
700
701 /*
702 * Another vma needs a probe to be installed. However skip
703 * installing the probe if the vma is about to be unlinked.
704 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100705 if (!existing_vma && atomic_inc_not_zero(&vma->vm_mm->mm_users)) {
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530706 vi->mm = vma->vm_mm;
707 vi->vaddr = vaddr;
708 list_add(&vi->probe_list, head);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100709
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530710 return vi;
711 }
712 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100713
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530714 return NULL;
715}
716
717/*
718 * Iterate in the rmap prio tree and find a vma where a probe has not
719 * yet been inserted.
720 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100721static struct vma_info *
722find_next_vma_info(struct list_head *head, loff_t offset, struct address_space *mapping,
723 bool is_register)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530724{
725 struct vma_info *vi, *retvi;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100726
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530727 vi = kzalloc(sizeof(struct vma_info), GFP_KERNEL);
728 if (!vi)
729 return ERR_PTR(-ENOMEM);
730
731 mutex_lock(&mapping->i_mmap_mutex);
732 retvi = __find_next_vma_info(head, offset, mapping, vi, is_register);
733 mutex_unlock(&mapping->i_mmap_mutex);
734
735 if (!retvi)
736 kfree(vi);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100737
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530738 return retvi;
739}
740
741static int register_for_each_vma(struct uprobe *uprobe, bool is_register)
742{
743 struct list_head try_list;
744 struct vm_area_struct *vma;
745 struct address_space *mapping;
746 struct vma_info *vi, *tmpvi;
747 struct mm_struct *mm;
748 loff_t vaddr;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100749 int ret;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530750
751 mapping = uprobe->inode->i_mapping;
752 INIT_LIST_HEAD(&try_list);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100753
754 ret = 0;
755
756 for (;;) {
757 vi = find_next_vma_info(&try_list, uprobe->offset, mapping, is_register);
758 if (!vi)
759 break;
760
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530761 if (IS_ERR(vi)) {
762 ret = PTR_ERR(vi);
763 break;
764 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100765
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530766 mm = vi->mm;
767 down_read(&mm->mmap_sem);
768 vma = find_vma(mm, (unsigned long)vi->vaddr);
769 if (!vma || !valid_vma(vma, is_register)) {
770 list_del(&vi->probe_list);
771 kfree(vi);
772 up_read(&mm->mmap_sem);
773 mmput(mm);
774 continue;
775 }
776 vaddr = vma_address(vma, uprobe->offset);
777 if (vma->vm_file->f_mapping->host != uprobe->inode ||
778 vaddr != vi->vaddr) {
779 list_del(&vi->probe_list);
780 kfree(vi);
781 up_read(&mm->mmap_sem);
782 mmput(mm);
783 continue;
784 }
785
786 if (is_register)
787 ret = install_breakpoint(mm, uprobe, vma, vi->vaddr);
788 else
789 remove_breakpoint(mm, uprobe, vi->vaddr);
790
791 up_read(&mm->mmap_sem);
792 mmput(mm);
793 if (is_register) {
794 if (ret && ret == -EEXIST)
795 ret = 0;
796 if (ret)
797 break;
798 }
799 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100800
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530801 list_for_each_entry_safe(vi, tmpvi, &try_list, probe_list) {
802 list_del(&vi->probe_list);
803 kfree(vi);
804 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100805
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530806 return ret;
807}
808
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100809static int __uprobe_register(struct uprobe *uprobe)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530810{
811 return register_for_each_vma(uprobe, true);
812}
813
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100814static void __uprobe_unregister(struct uprobe *uprobe)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530815{
816 if (!register_for_each_vma(uprobe, false))
817 delete_uprobe(uprobe);
818
819 /* TODO : cant unregister? schedule a worker thread */
820}
821
822/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100823 * uprobe_register - register a probe
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530824 * @inode: the file in which the probe has to be placed.
825 * @offset: offset from the start of the file.
826 * @consumer: information on howto handle the probe..
827 *
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100828 * Apart from the access refcount, uprobe_register() takes a creation
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530829 * refcount (thro alloc_uprobe) if and only if this @uprobe is getting
830 * inserted into the rbtree (i.e first consumer for a @inode:@offset
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100831 * tuple). Creation refcount stops uprobe_unregister from freeing the
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530832 * @uprobe even before the register operation is complete. Creation
833 * refcount is released when the last @consumer for the @uprobe
834 * unregisters.
835 *
836 * Return errno if it cannot successully install probes
837 * else return 0 (success)
838 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100839int uprobe_register(struct inode *inode, loff_t offset, struct uprobe_consumer *consumer)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530840{
841 struct uprobe *uprobe;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100842 int ret;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530843
844 if (!inode || !consumer || consumer->next)
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100845 return -EINVAL;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530846
847 if (offset > i_size_read(inode))
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100848 return -EINVAL;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530849
850 ret = 0;
851 mutex_lock(uprobes_hash(inode));
852 uprobe = alloc_uprobe(inode, offset);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100853
854 if (uprobe && !consumer_add(uprobe, consumer)) {
855 ret = __uprobe_register(uprobe);
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530856 if (ret) {
857 uprobe->consumers = NULL;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100858 __uprobe_unregister(uprobe);
859 } else {
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530860 uprobe->flags |= UPROBES_RUN_HANDLER;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100861 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530862 }
863
864 mutex_unlock(uprobes_hash(inode));
865 put_uprobe(uprobe);
866
867 return ret;
868}
869
870/*
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100871 * uprobe_unregister - unregister a already registered probe.
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530872 * @inode: the file in which the probe has to be removed.
873 * @offset: offset from the start of the file.
874 * @consumer: identify which probe if multiple probes are colocated.
875 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100876void uprobe_unregister(struct inode *inode, loff_t offset, struct uprobe_consumer *consumer)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530877{
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100878 struct uprobe *uprobe;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530879
880 if (!inode || !consumer)
881 return;
882
883 uprobe = find_uprobe(inode, offset);
884 if (!uprobe)
885 return;
886
887 mutex_lock(uprobes_hash(inode));
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530888
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100889 if (consumer_del(uprobe, consumer)) {
890 if (!uprobe->consumers) {
891 __uprobe_unregister(uprobe);
892 uprobe->flags &= ~UPROBES_RUN_HANDLER;
893 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530894 }
895
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530896 mutex_unlock(uprobes_hash(inode));
897 if (uprobe)
898 put_uprobe(uprobe);
899}
900
901/*
902 * Of all the nodes that correspond to the given inode, return the node
903 * with the least offset.
904 */
905static struct rb_node *find_least_offset_node(struct inode *inode)
906{
907 struct uprobe u = { .inode = inode, .offset = 0};
908 struct rb_node *n = uprobes_tree.rb_node;
909 struct rb_node *close_node = NULL;
910 struct uprobe *uprobe;
911 int match;
912
913 while (n) {
914 uprobe = rb_entry(n, struct uprobe, rb_node);
915 match = match_uprobe(&u, uprobe);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100916
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530917 if (uprobe->inode == inode)
918 close_node = n;
919
920 if (!match)
921 return close_node;
922
923 if (match < 0)
924 n = n->rb_left;
925 else
926 n = n->rb_right;
927 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100928
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530929 return close_node;
930}
931
932/*
933 * For a given inode, build a list of probes that need to be inserted.
934 */
935static void build_probe_list(struct inode *inode, struct list_head *head)
936{
937 struct uprobe *uprobe;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530938 unsigned long flags;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100939 struct rb_node *n;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530940
941 spin_lock_irqsave(&uprobes_treelock, flags);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100942
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530943 n = find_least_offset_node(inode);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100944
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530945 for (; n; n = rb_next(n)) {
946 uprobe = rb_entry(n, struct uprobe, rb_node);
947 if (uprobe->inode != inode)
948 break;
949
950 list_add(&uprobe->pending_list, head);
951 atomic_inc(&uprobe->ref);
952 }
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100953
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530954 spin_unlock_irqrestore(&uprobes_treelock, flags);
955}
956
957/*
958 * Called from mmap_region.
959 * called with mm->mmap_sem acquired.
960 *
961 * Return -ve no if we fail to insert probes and we cannot
962 * bail-out.
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100963 * Return 0 otherwise. i.e:
964 *
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530965 * - successful insertion of probes
966 * - (or) no possible probes to be inserted.
967 * - (or) insertion of probes failed but we can bail-out.
968 */
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100969int uprobe_mmap(struct vm_area_struct *vma)
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530970{
971 struct list_head tmp_list;
972 struct uprobe *uprobe, *u;
973 struct inode *inode;
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100974 int ret;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530975
976 if (!atomic_read(&uprobe_events) || !valid_vma(vma, true))
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100977 return 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530978
979 inode = vma->vm_file->f_mapping->host;
980 if (!inode)
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100981 return 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530982
983 INIT_LIST_HEAD(&tmp_list);
984 mutex_lock(uprobes_mmap_hash(inode));
985 build_probe_list(inode, &tmp_list);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100986
987 ret = 0;
988
Srikar Dronamraju2b144492012-02-09 14:56:42 +0530989 list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
990 loff_t vaddr;
991
992 list_del(&uprobe->pending_list);
993 if (!ret) {
994 vaddr = vma_address(vma, uprobe->offset);
Ingo Molnar7b2d81d2012-02-17 09:27:41 +0100995 if (vaddr >= vma->vm_start && vaddr < vma->vm_end) {
996 ret = install_breakpoint(vma->vm_mm, uprobe, vma, vaddr);
997 /* Ignore double add: */
998 if (ret == -EEXIST)
999 ret = 0;
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301000 }
Srikar Dronamraju2b144492012-02-09 14:56:42 +05301001 }
1002 put_uprobe(uprobe);
1003 }
1004
1005 mutex_unlock(uprobes_mmap_hash(inode));
1006
1007 return ret;
1008}
1009
1010static int __init init_uprobes(void)
1011{
1012 int i;
1013
1014 for (i = 0; i < UPROBES_HASH_SZ; i++) {
1015 mutex_init(&uprobes_mutex[i]);
1016 mutex_init(&uprobes_mmap_mutex[i]);
1017 }
1018 return 0;
1019}
1020
1021static void __exit exit_uprobes(void)
1022{
1023}
1024
1025module_init(init_uprobes);
1026module_exit(exit_uprobes);