kprobes: kretprobes simplifications
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / kernel / kprobes.c
CommitLineData
1da177e4
LT
1/*
2 * Kernel Probes (KProbes)
3 * kernel/kprobes.c
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *
19 * Copyright (C) IBM Corporation, 2002, 2004
20 *
21 * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
22 * Probes initial implementation (includes suggestions from
23 * Rusty Russell).
24 * 2004-Aug Updated by Prasanna S Panchamukhi <prasanna@in.ibm.com> with
25 * hlists and exceptions notifier as suggested by Andi Kleen.
26 * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
27 * interface to access function arguments.
28 * 2004-Sep Prasanna S Panchamukhi <prasanna@in.ibm.com> Changed Kprobes
29 * exceptions notifier to be first on the priority list.
b94cce92
HN
30 * 2005-May Hien Nguyen <hien@us.ibm.com>, Jim Keniston
31 * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
32 * <prasanna@in.ibm.com> added function-return probes.
1da177e4
LT
33 */
34#include <linux/kprobes.h>
1da177e4
LT
35#include <linux/hash.h>
36#include <linux/init.h>
4e57b681 37#include <linux/slab.h>
e3869792 38#include <linux/stddef.h>
1da177e4 39#include <linux/module.h>
9ec4b1f3 40#include <linux/moduleloader.h>
3a872d89 41#include <linux/kallsyms.h>
b4c6c34a 42#include <linux/freezer.h>
346fd59b
SD
43#include <linux/seq_file.h>
44#include <linux/debugfs.h>
1eeb66a1 45#include <linux/kdebug.h>
d0aaff97 46#include <asm-generic/sections.h>
1da177e4
LT
47#include <asm/cacheflush.h>
48#include <asm/errno.h>
1da177e4
LT
49
50#define KPROBE_HASH_BITS 6
51#define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
52
3a872d89
AM
53
54/*
55 * Some oddball architectures like 64bit powerpc have function descriptors
56 * so this must be overridable.
57 */
58#ifndef kprobe_lookup_name
59#define kprobe_lookup_name(name, addr) \
60 addr = ((kprobe_opcode_t *)(kallsyms_lookup_name(name)))
61#endif
62
1da177e4 63static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
b94cce92 64static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
e6f47f97 65static atomic_t kprobe_count;
1da177e4 66
7a7d1cf9 67DEFINE_MUTEX(kprobe_mutex); /* Protects kprobe_table */
3516a460 68DEFINE_SPINLOCK(kretprobe_lock); /* Protects kretprobe_inst_table */
e6584523 69static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL;
1da177e4 70
e6f47f97
AK
71static struct notifier_block kprobe_page_fault_nb = {
72 .notifier_call = kprobe_exceptions_notify,
73 .priority = 0x7fffffff /* we need to notified first */
74};
75
2d14e39d 76#ifdef __ARCH_WANT_KPROBES_INSN_SLOT
9ec4b1f3
AM
77/*
78 * kprobe->ainsn.insn points to the copy of the instruction to be
79 * single-stepped. x86_64, POWER4 and above have no-exec support and
80 * stepping on the instruction on a vmalloced/kmalloced/data page
81 * is a recipe for disaster
82 */
83#define INSNS_PER_PAGE (PAGE_SIZE/(MAX_INSN_SIZE * sizeof(kprobe_opcode_t)))
84
85struct kprobe_insn_page {
86 struct hlist_node hlist;
87 kprobe_opcode_t *insns; /* Page of instruction slots */
88 char slot_used[INSNS_PER_PAGE];
89 int nused;
b4c6c34a 90 int ngarbage;
9ec4b1f3
AM
91};
92
ab40c5c6
MH
93enum kprobe_slot_state {
94 SLOT_CLEAN = 0,
95 SLOT_DIRTY = 1,
96 SLOT_USED = 2,
97};
98
9ec4b1f3 99static struct hlist_head kprobe_insn_pages;
b4c6c34a
MH
100static int kprobe_garbage_slots;
101static int collect_garbage_slots(void);
102
103static int __kprobes check_safety(void)
104{
105 int ret = 0;
106#if defined(CONFIG_PREEMPT) && defined(CONFIG_PM)
107 ret = freeze_processes();
108 if (ret == 0) {
109 struct task_struct *p, *q;
110 do_each_thread(p, q) {
111 if (p != current && p->state == TASK_RUNNING &&
112 p->pid != 0) {
113 printk("Check failed: %s is running\n",p->comm);
114 ret = -1;
115 goto loop_end;
116 }
117 } while_each_thread(p, q);
118 }
119loop_end:
120 thaw_processes();
121#else
122 synchronize_sched();
123#endif
124 return ret;
125}
9ec4b1f3
AM
126
127/**
128 * get_insn_slot() - Find a slot on an executable page for an instruction.
129 * We allocate an executable page if there's no room on existing ones.
130 */
d0aaff97 131kprobe_opcode_t __kprobes *get_insn_slot(void)
9ec4b1f3
AM
132{
133 struct kprobe_insn_page *kip;
134 struct hlist_node *pos;
135
6f716acd 136 retry:
b0bb5016 137 hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) {
9ec4b1f3
AM
138 if (kip->nused < INSNS_PER_PAGE) {
139 int i;
140 for (i = 0; i < INSNS_PER_PAGE; i++) {
ab40c5c6
MH
141 if (kip->slot_used[i] == SLOT_CLEAN) {
142 kip->slot_used[i] = SLOT_USED;
9ec4b1f3
AM
143 kip->nused++;
144 return kip->insns + (i * MAX_INSN_SIZE);
145 }
146 }
147 /* Surprise! No unused slots. Fix kip->nused. */
148 kip->nused = INSNS_PER_PAGE;
149 }
150 }
151
b4c6c34a
MH
152 /* If there are any garbage slots, collect it and try again. */
153 if (kprobe_garbage_slots && collect_garbage_slots() == 0) {
154 goto retry;
155 }
156 /* All out of space. Need to allocate a new page. Use slot 0. */
9ec4b1f3 157 kip = kmalloc(sizeof(struct kprobe_insn_page), GFP_KERNEL);
6f716acd 158 if (!kip)
9ec4b1f3 159 return NULL;
9ec4b1f3
AM
160
161 /*
162 * Use module_alloc so this page is within +/- 2GB of where the
163 * kernel image and loaded module images reside. This is required
164 * so x86_64 can correctly handle the %rip-relative fixups.
165 */
166 kip->insns = module_alloc(PAGE_SIZE);
167 if (!kip->insns) {
168 kfree(kip);
169 return NULL;
170 }
171 INIT_HLIST_NODE(&kip->hlist);
172 hlist_add_head(&kip->hlist, &kprobe_insn_pages);
ab40c5c6
MH
173 memset(kip->slot_used, SLOT_CLEAN, INSNS_PER_PAGE);
174 kip->slot_used[0] = SLOT_USED;
9ec4b1f3 175 kip->nused = 1;
b4c6c34a 176 kip->ngarbage = 0;
9ec4b1f3
AM
177 return kip->insns;
178}
179
b4c6c34a
MH
180/* Return 1 if all garbages are collected, otherwise 0. */
181static int __kprobes collect_one_slot(struct kprobe_insn_page *kip, int idx)
182{
ab40c5c6 183 kip->slot_used[idx] = SLOT_CLEAN;
b4c6c34a
MH
184 kip->nused--;
185 if (kip->nused == 0) {
186 /*
187 * Page is no longer in use. Free it unless
188 * it's the last one. We keep the last one
189 * so as not to have to set it up again the
190 * next time somebody inserts a probe.
191 */
192 hlist_del(&kip->hlist);
193 if (hlist_empty(&kprobe_insn_pages)) {
194 INIT_HLIST_NODE(&kip->hlist);
195 hlist_add_head(&kip->hlist,
196 &kprobe_insn_pages);
197 } else {
198 module_free(NULL, kip->insns);
199 kfree(kip);
200 }
201 return 1;
202 }
203 return 0;
204}
205
206static int __kprobes collect_garbage_slots(void)
207{
208 struct kprobe_insn_page *kip;
209 struct hlist_node *pos, *next;
210
211 /* Ensure no-one is preepmted on the garbages */
212 if (check_safety() != 0)
213 return -EAGAIN;
214
b0bb5016 215 hlist_for_each_entry_safe(kip, pos, next, &kprobe_insn_pages, hlist) {
b4c6c34a 216 int i;
b4c6c34a
MH
217 if (kip->ngarbage == 0)
218 continue;
219 kip->ngarbage = 0; /* we will collect all garbages */
220 for (i = 0; i < INSNS_PER_PAGE; i++) {
ab40c5c6 221 if (kip->slot_used[i] == SLOT_DIRTY &&
b4c6c34a
MH
222 collect_one_slot(kip, i))
223 break;
224 }
225 }
226 kprobe_garbage_slots = 0;
227 return 0;
228}
229
230void __kprobes free_insn_slot(kprobe_opcode_t * slot, int dirty)
9ec4b1f3
AM
231{
232 struct kprobe_insn_page *kip;
233 struct hlist_node *pos;
234
b0bb5016 235 hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) {
9ec4b1f3
AM
236 if (kip->insns <= slot &&
237 slot < kip->insns + (INSNS_PER_PAGE * MAX_INSN_SIZE)) {
238 int i = (slot - kip->insns) / MAX_INSN_SIZE;
b4c6c34a 239 if (dirty) {
ab40c5c6 240 kip->slot_used[i] = SLOT_DIRTY;
b4c6c34a
MH
241 kip->ngarbage++;
242 } else {
243 collect_one_slot(kip, i);
9ec4b1f3 244 }
b4c6c34a 245 break;
9ec4b1f3
AM
246 }
247 }
6f716acd
CH
248
249 if (dirty && ++kprobe_garbage_slots > INSNS_PER_PAGE)
b4c6c34a 250 collect_garbage_slots();
9ec4b1f3 251}
2d14e39d 252#endif
9ec4b1f3 253
e6584523
AM
254/* We have preemption disabled.. so it is safe to use __ versions */
255static inline void set_kprobe_instance(struct kprobe *kp)
256{
257 __get_cpu_var(kprobe_instance) = kp;
258}
259
260static inline void reset_kprobe_instance(void)
261{
262 __get_cpu_var(kprobe_instance) = NULL;
263}
264
3516a460
AM
265/*
266 * This routine is called either:
49a2a1b8 267 * - under the kprobe_mutex - during kprobe_[un]register()
3516a460 268 * OR
d217d545 269 * - with preemption disabled - from arch/xxx/kernel/kprobes.c
3516a460 270 */
d0aaff97 271struct kprobe __kprobes *get_kprobe(void *addr)
1da177e4
LT
272{
273 struct hlist_head *head;
274 struct hlist_node *node;
3516a460 275 struct kprobe *p;
1da177e4
LT
276
277 head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
3516a460 278 hlist_for_each_entry_rcu(p, node, head, hlist) {
1da177e4
LT
279 if (p->addr == addr)
280 return p;
281 }
282 return NULL;
283}
284
64f562c6
AM
285/*
286 * Aggregate handlers for multiple kprobes support - these handlers
287 * take care of invoking the individual kprobe handlers on p->list
288 */
d0aaff97 289static int __kprobes aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
64f562c6
AM
290{
291 struct kprobe *kp;
292
3516a460 293 list_for_each_entry_rcu(kp, &p->list, list) {
64f562c6 294 if (kp->pre_handler) {
e6584523 295 set_kprobe_instance(kp);
8b0914ea
PP
296 if (kp->pre_handler(kp, regs))
297 return 1;
64f562c6 298 }
e6584523 299 reset_kprobe_instance();
64f562c6
AM
300 }
301 return 0;
302}
303
d0aaff97
PP
304static void __kprobes aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
305 unsigned long flags)
64f562c6
AM
306{
307 struct kprobe *kp;
308
3516a460 309 list_for_each_entry_rcu(kp, &p->list, list) {
64f562c6 310 if (kp->post_handler) {
e6584523 311 set_kprobe_instance(kp);
64f562c6 312 kp->post_handler(kp, regs, flags);
e6584523 313 reset_kprobe_instance();
64f562c6
AM
314 }
315 }
64f562c6
AM
316}
317
d0aaff97
PP
318static int __kprobes aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
319 int trapnr)
64f562c6 320{
e6584523
AM
321 struct kprobe *cur = __get_cpu_var(kprobe_instance);
322
64f562c6
AM
323 /*
324 * if we faulted "during" the execution of a user specified
325 * probe handler, invoke just that probe's fault handler
326 */
e6584523
AM
327 if (cur && cur->fault_handler) {
328 if (cur->fault_handler(cur, regs, trapnr))
64f562c6
AM
329 return 1;
330 }
331 return 0;
332}
333
d0aaff97 334static int __kprobes aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
8b0914ea 335{
e6584523
AM
336 struct kprobe *cur = __get_cpu_var(kprobe_instance);
337 int ret = 0;
338
339 if (cur && cur->break_handler) {
340 if (cur->break_handler(cur, regs))
341 ret = 1;
8b0914ea 342 }
e6584523
AM
343 reset_kprobe_instance();
344 return ret;
8b0914ea
PP
345}
346
bf8d5c52
KA
347/* Walks the list and increments nmissed count for multiprobe case */
348void __kprobes kprobes_inc_nmissed_count(struct kprobe *p)
349{
350 struct kprobe *kp;
351 if (p->pre_handler != aggr_pre_handler) {
352 p->nmissed++;
353 } else {
354 list_for_each_entry_rcu(kp, &p->list, list)
355 kp->nmissed++;
356 }
357 return;
358}
359
3516a460 360/* Called with kretprobe_lock held */
99219a3f 361void __kprobes recycle_rp_inst(struct kretprobe_instance *ri,
362 struct hlist_head *head)
b94cce92
HN
363{
364 /* remove rp inst off the rprobe_inst_table */
365 hlist_del(&ri->hlist);
366 if (ri->rp) {
367 /* remove rp inst off the used list */
368 hlist_del(&ri->uflist);
369 /* put rp inst back onto the free list */
370 INIT_HLIST_NODE(&ri->uflist);
371 hlist_add_head(&ri->uflist, &ri->rp->free_instances);
372 } else
373 /* Unregistering */
99219a3f 374 hlist_add_head(&ri->hlist, head);
b94cce92
HN
375}
376
d0aaff97 377struct hlist_head __kprobes *kretprobe_inst_table_head(struct task_struct *tsk)
b94cce92
HN
378{
379 return &kretprobe_inst_table[hash_ptr(tsk, KPROBE_HASH_BITS)];
380}
381
b94cce92 382/*
c6fd91f0 383 * This function is called from finish_task_switch when task tk becomes dead,
384 * so that we can recycle any function-return probe instances associated
385 * with this task. These left over instances represent probed functions
386 * that have been called but will never return.
b94cce92 387 */
d0aaff97 388void __kprobes kprobe_flush_task(struct task_struct *tk)
b94cce92 389{
62c27be0 390 struct kretprobe_instance *ri;
99219a3f 391 struct hlist_head *head, empty_rp;
802eae7c 392 struct hlist_node *node, *tmp;
0aa55e4d 393 unsigned long flags = 0;
802eae7c 394
99219a3f 395 INIT_HLIST_HEAD(&empty_rp);
3516a460 396 spin_lock_irqsave(&kretprobe_lock, flags);
62c27be0 397 head = kretprobe_inst_table_head(tk);
398 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
399 if (ri->task == tk)
99219a3f 400 recycle_rp_inst(ri, &empty_rp);
62c27be0 401 }
3516a460 402 spin_unlock_irqrestore(&kretprobe_lock, flags);
99219a3f 403
404 hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
405 hlist_del(&ri->hlist);
406 kfree(ri);
407 }
b94cce92
HN
408}
409
b94cce92
HN
410static inline void free_rp_inst(struct kretprobe *rp)
411{
412 struct kretprobe_instance *ri;
4c4308cb
CH
413 struct hlist_node *pos, *next;
414
415 hlist_for_each_entry_safe(ri, pos, next, &rp->free_instances, uflist) {
b94cce92
HN
416 hlist_del(&ri->uflist);
417 kfree(ri);
418 }
419}
420
8b0914ea
PP
421/*
422 * Keep all fields in the kprobe consistent
423 */
424static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p)
425{
426 memcpy(&p->opcode, &old_p->opcode, sizeof(kprobe_opcode_t));
427 memcpy(&p->ainsn, &old_p->ainsn, sizeof(struct arch_specific_insn));
428}
429
430/*
431* Add the new probe to old_p->list. Fail if this is the
432* second jprobe at the address - two jprobes can't coexist
433*/
d0aaff97 434static int __kprobes add_new_kprobe(struct kprobe *old_p, struct kprobe *p)
8b0914ea 435{
8b0914ea 436 if (p->break_handler) {
36721656 437 if (old_p->break_handler)
438 return -EEXIST;
3516a460 439 list_add_tail_rcu(&p->list, &old_p->list);
36721656 440 old_p->break_handler = aggr_break_handler;
8b0914ea 441 } else
3516a460 442 list_add_rcu(&p->list, &old_p->list);
36721656 443 if (p->post_handler && !old_p->post_handler)
444 old_p->post_handler = aggr_post_handler;
8b0914ea
PP
445 return 0;
446}
447
64f562c6
AM
448/*
449 * Fill in the required fields of the "manager kprobe". Replace the
450 * earlier kprobe in the hlist with the manager kprobe
451 */
452static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
453{
8b0914ea 454 copy_kprobe(p, ap);
a9ad965e 455 flush_insn_slot(ap);
64f562c6 456 ap->addr = p->addr;
64f562c6 457 ap->pre_handler = aggr_pre_handler;
64f562c6 458 ap->fault_handler = aggr_fault_handler;
36721656 459 if (p->post_handler)
460 ap->post_handler = aggr_post_handler;
461 if (p->break_handler)
462 ap->break_handler = aggr_break_handler;
64f562c6
AM
463
464 INIT_LIST_HEAD(&ap->list);
3516a460 465 list_add_rcu(&p->list, &ap->list);
64f562c6 466
adad0f33 467 hlist_replace_rcu(&p->hlist, &ap->hlist);
64f562c6
AM
468}
469
470/*
471 * This is the second or subsequent kprobe at the address - handle
472 * the intricacies
64f562c6 473 */
d0aaff97
PP
474static int __kprobes register_aggr_kprobe(struct kprobe *old_p,
475 struct kprobe *p)
64f562c6
AM
476{
477 int ret = 0;
478 struct kprobe *ap;
479
8b0914ea
PP
480 if (old_p->pre_handler == aggr_pre_handler) {
481 copy_kprobe(old_p, p);
482 ret = add_new_kprobe(old_p, p);
64f562c6 483 } else {
a0d50069 484 ap = kzalloc(sizeof(struct kprobe), GFP_KERNEL);
64f562c6
AM
485 if (!ap)
486 return -ENOMEM;
487 add_aggr_kprobe(ap, old_p);
8b0914ea
PP
488 copy_kprobe(ap, p);
489 ret = add_new_kprobe(ap, p);
64f562c6
AM
490 }
491 return ret;
492}
493
d0aaff97
PP
494static int __kprobes in_kprobes_functions(unsigned long addr)
495{
6f716acd
CH
496 if (addr >= (unsigned long)__kprobes_text_start &&
497 addr < (unsigned long)__kprobes_text_end)
d0aaff97
PP
498 return -EINVAL;
499 return 0;
500}
501
df019b1d
KA
502static int __kprobes __register_kprobe(struct kprobe *p,
503 unsigned long called_from)
1da177e4
LT
504{
505 int ret = 0;
64f562c6 506 struct kprobe *old_p;
df019b1d 507 struct module *probed_mod;
b3e55c72 508
3a872d89
AM
509 /*
510 * If we have a symbol_name argument look it up,
511 * and add it to the address. That way the addr
512 * field can either be global or relative to a symbol.
513 */
514 if (p->symbol_name) {
515 if (p->addr)
516 return -EINVAL;
517 kprobe_lookup_name(p->symbol_name, p->addr);
518 }
519
520 if (!p->addr)
521 return -EINVAL;
522 p->addr = (kprobe_opcode_t *)(((char *)p->addr)+ p->offset);
523
6f716acd
CH
524 if (!kernel_text_address((unsigned long) p->addr) ||
525 in_kprobes_functions((unsigned long) p->addr))
b3e55c72
MB
526 return -EINVAL;
527
df019b1d 528 p->mod_refcounted = 0;
6f716acd
CH
529
530 /*
531 * Check if are we probing a module.
532 */
533 probed_mod = module_text_address((unsigned long) p->addr);
534 if (probed_mod) {
df019b1d 535 struct module *calling_mod = module_text_address(called_from);
6f716acd
CH
536 /*
537 * We must allow modules to probe themself and in this case
538 * avoid incrementing the module refcount, so as to allow
539 * unloading of self probing modules.
df019b1d 540 */
6f716acd 541 if (calling_mod && calling_mod != probed_mod) {
df019b1d
KA
542 if (unlikely(!try_module_get(probed_mod)))
543 return -EINVAL;
544 p->mod_refcounted = 1;
545 } else
546 probed_mod = NULL;
547 }
1da177e4 548
3516a460 549 p->nmissed = 0;
7a7d1cf9 550 mutex_lock(&kprobe_mutex);
64f562c6
AM
551 old_p = get_kprobe(p->addr);
552 if (old_p) {
553 ret = register_aggr_kprobe(old_p, p);
e6f47f97
AK
554 if (!ret)
555 atomic_inc(&kprobe_count);
1da177e4
LT
556 goto out;
557 }
1da177e4 558
6f716acd
CH
559 ret = arch_prepare_kprobe(p);
560 if (ret)
49a2a1b8
AK
561 goto out;
562
64f562c6 563 INIT_HLIST_NODE(&p->hlist);
3516a460 564 hlist_add_head_rcu(&p->hlist,
1da177e4
LT
565 &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
566
e6f47f97
AK
567 if (atomic_add_return(1, &kprobe_count) == \
568 (ARCH_INACTIVE_KPROBE_COUNT + 1))
569 register_page_fault_notifier(&kprobe_page_fault_nb);
570
62c27be0 571 arch_arm_kprobe(p);
7e1048b1 572
1da177e4 573out:
7a7d1cf9 574 mutex_unlock(&kprobe_mutex);
49a2a1b8 575
df019b1d
KA
576 if (ret && probed_mod)
577 module_put(probed_mod);
1da177e4
LT
578 return ret;
579}
580
df019b1d
KA
581int __kprobes register_kprobe(struct kprobe *p)
582{
6f716acd 583 return __register_kprobe(p, (unsigned long)__builtin_return_address(0));
df019b1d
KA
584}
585
d0aaff97 586void __kprobes unregister_kprobe(struct kprobe *p)
1da177e4 587{
b3e55c72 588 struct module *mod;
f709b122
KA
589 struct kprobe *old_p, *list_p;
590 int cleanup_p;
64f562c6 591
7a7d1cf9 592 mutex_lock(&kprobe_mutex);
64f562c6 593 old_p = get_kprobe(p->addr);
49a2a1b8 594 if (unlikely(!old_p)) {
7a7d1cf9 595 mutex_unlock(&kprobe_mutex);
49a2a1b8
AK
596 return;
597 }
f709b122
KA
598 if (p != old_p) {
599 list_for_each_entry_rcu(list_p, &old_p->list, list)
600 if (list_p == p)
601 /* kprobe p is a valid probe */
602 goto valid_p;
7a7d1cf9 603 mutex_unlock(&kprobe_mutex);
f709b122
KA
604 return;
605 }
606valid_p:
6f716acd
CH
607 if (old_p == p ||
608 (old_p->pre_handler == aggr_pre_handler &&
609 p->list.next == &old_p->list && p->list.prev == &old_p->list)) {
f709b122 610 /* Only probe on the hash list */
49a2a1b8
AK
611 arch_disarm_kprobe(p);
612 hlist_del_rcu(&old_p->hlist);
f709b122 613 cleanup_p = 1;
49a2a1b8
AK
614 } else {
615 list_del_rcu(&p->list);
f709b122 616 cleanup_p = 0;
49a2a1b8 617 }
3516a460 618
7a7d1cf9 619 mutex_unlock(&kprobe_mutex);
b3e55c72 620
49a2a1b8 621 synchronize_sched();
6f716acd
CH
622 if (p->mod_refcounted) {
623 mod = module_text_address((unsigned long)p->addr);
624 if (mod)
625 module_put(mod);
626 }
b3e55c72 627
49a2a1b8 628 if (cleanup_p) {
f709b122 629 if (p != old_p) {
49a2a1b8 630 list_del_rcu(&p->list);
3516a460 631 kfree(old_p);
49a2a1b8 632 }
0498b635 633 arch_remove_kprobe(p);
36721656 634 } else {
635 mutex_lock(&kprobe_mutex);
636 if (p->break_handler)
637 old_p->break_handler = NULL;
638 if (p->post_handler){
639 list_for_each_entry_rcu(list_p, &old_p->list, list){
640 if (list_p->post_handler){
641 cleanup_p = 2;
642 break;
643 }
644 }
645 if (cleanup_p == 0)
646 old_p->post_handler = NULL;
647 }
648 mutex_unlock(&kprobe_mutex);
49a2a1b8 649 }
e6f47f97
AK
650
651 /* Call unregister_page_fault_notifier()
652 * if no probes are active
653 */
654 mutex_lock(&kprobe_mutex);
655 if (atomic_add_return(-1, &kprobe_count) == \
656 ARCH_INACTIVE_KPROBE_COUNT)
657 unregister_page_fault_notifier(&kprobe_page_fault_nb);
658 mutex_unlock(&kprobe_mutex);
659 return;
1da177e4
LT
660}
661
662static struct notifier_block kprobe_exceptions_nb = {
3d5631e0
AK
663 .notifier_call = kprobe_exceptions_notify,
664 .priority = 0x7fffffff /* we need to be notified first */
665};
666
1da177e4 667
d0aaff97 668int __kprobes register_jprobe(struct jprobe *jp)
1da177e4
LT
669{
670 /* Todo: Verify probepoint is a function entry point */
671 jp->kp.pre_handler = setjmp_pre_handler;
672 jp->kp.break_handler = longjmp_break_handler;
673
df019b1d
KA
674 return __register_kprobe(&jp->kp,
675 (unsigned long)__builtin_return_address(0));
1da177e4
LT
676}
677
d0aaff97 678void __kprobes unregister_jprobe(struct jprobe *jp)
1da177e4
LT
679{
680 unregister_kprobe(&jp->kp);
681}
682
b94cce92
HN
683#ifdef ARCH_SUPPORTS_KRETPROBES
684
e65cefe8
AB
685/*
686 * This kprobe pre_handler is registered with every kretprobe. When probe
687 * hits it will set up the return probe.
688 */
689static int __kprobes pre_handler_kretprobe(struct kprobe *p,
690 struct pt_regs *regs)
691{
692 struct kretprobe *rp = container_of(p, struct kretprobe, kp);
693 unsigned long flags = 0;
694
695 /*TODO: consider to only swap the RA after the last pre_handler fired */
696 spin_lock_irqsave(&kretprobe_lock, flags);
4c4308cb
CH
697 if (!hlist_empty(&rp->free_instances)) {
698 struct kretprobe_instance *ri;
699
700 ri = hlist_entry(rp->free_instances.first,
701 struct kretprobe_instance, uflist);
702 ri->rp = rp;
703 ri->task = current;
704 arch_prepare_kretprobe(ri, regs);
705
706 /* XXX(hch): why is there no hlist_move_head? */
707 hlist_del(&ri->uflist);
708 hlist_add_head(&ri->uflist, &ri->rp->used_instances);
709 hlist_add_head(&ri->hlist, kretprobe_inst_table_head(ri->task));
710 } else
711 rp->nmissed++;
e65cefe8
AB
712 spin_unlock_irqrestore(&kretprobe_lock, flags);
713 return 0;
714}
715
d0aaff97 716int __kprobes register_kretprobe(struct kretprobe *rp)
b94cce92
HN
717{
718 int ret = 0;
719 struct kretprobe_instance *inst;
720 int i;
721
722 rp->kp.pre_handler = pre_handler_kretprobe;
7522a842
AM
723 rp->kp.post_handler = NULL;
724 rp->kp.fault_handler = NULL;
725 rp->kp.break_handler = NULL;
b94cce92
HN
726
727 /* Pre-allocate memory for max kretprobe instances */
728 if (rp->maxactive <= 0) {
729#ifdef CONFIG_PREEMPT
730 rp->maxactive = max(10, 2 * NR_CPUS);
731#else
732 rp->maxactive = NR_CPUS;
733#endif
734 }
735 INIT_HLIST_HEAD(&rp->used_instances);
736 INIT_HLIST_HEAD(&rp->free_instances);
737 for (i = 0; i < rp->maxactive; i++) {
738 inst = kmalloc(sizeof(struct kretprobe_instance), GFP_KERNEL);
739 if (inst == NULL) {
740 free_rp_inst(rp);
741 return -ENOMEM;
742 }
743 INIT_HLIST_NODE(&inst->uflist);
744 hlist_add_head(&inst->uflist, &rp->free_instances);
745 }
746
747 rp->nmissed = 0;
748 /* Establish function entry probe point */
df019b1d
KA
749 if ((ret = __register_kprobe(&rp->kp,
750 (unsigned long)__builtin_return_address(0))) != 0)
b94cce92
HN
751 free_rp_inst(rp);
752 return ret;
753}
754
755#else /* ARCH_SUPPORTS_KRETPROBES */
756
d0aaff97 757int __kprobes register_kretprobe(struct kretprobe *rp)
b94cce92
HN
758{
759 return -ENOSYS;
760}
761
346fd59b
SD
762static int __kprobes pre_handler_kretprobe(struct kprobe *p,
763 struct pt_regs *regs)
764{
765 return 0;
766}
767
b94cce92
HN
768#endif /* ARCH_SUPPORTS_KRETPROBES */
769
d0aaff97 770void __kprobes unregister_kretprobe(struct kretprobe *rp)
b94cce92
HN
771{
772 unsigned long flags;
773 struct kretprobe_instance *ri;
4c4308cb 774 struct hlist_node *pos, *next;
b94cce92
HN
775
776 unregister_kprobe(&rp->kp);
4c4308cb 777
b94cce92 778 /* No race here */
3516a460 779 spin_lock_irqsave(&kretprobe_lock, flags);
4c4308cb 780 hlist_for_each_entry_safe(ri, pos, next, &rp->used_instances, uflist) {
b94cce92
HN
781 ri->rp = NULL;
782 hlist_del(&ri->uflist);
783 }
3516a460 784 spin_unlock_irqrestore(&kretprobe_lock, flags);
278ff953 785 free_rp_inst(rp);
b94cce92
HN
786}
787
1da177e4
LT
788static int __init init_kprobes(void)
789{
790 int i, err = 0;
791
792 /* FIXME allocate the probe table, currently defined statically */
793 /* initialize all list heads */
b94cce92 794 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
1da177e4 795 INIT_HLIST_HEAD(&kprobe_table[i]);
b94cce92
HN
796 INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
797 }
e6f47f97 798 atomic_set(&kprobe_count, 0);
1da177e4 799
6772926b 800 err = arch_init_kprobes();
802eae7c
RL
801 if (!err)
802 err = register_die_notifier(&kprobe_exceptions_nb);
803
1da177e4
LT
804 return err;
805}
806
346fd59b
SD
807#ifdef CONFIG_DEBUG_FS
808static void __kprobes report_probe(struct seq_file *pi, struct kprobe *p,
809 const char *sym, int offset,char *modname)
810{
811 char *kprobe_type;
812
813 if (p->pre_handler == pre_handler_kretprobe)
814 kprobe_type = "r";
815 else if (p->pre_handler == setjmp_pre_handler)
816 kprobe_type = "j";
817 else
818 kprobe_type = "k";
819 if (sym)
820 seq_printf(pi, "%p %s %s+0x%x %s\n", p->addr, kprobe_type,
821 sym, offset, (modname ? modname : " "));
822 else
823 seq_printf(pi, "%p %s %p\n", p->addr, kprobe_type, p->addr);
824}
825
826static void __kprobes *kprobe_seq_start(struct seq_file *f, loff_t *pos)
827{
828 return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL;
829}
830
831static void __kprobes *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos)
832{
833 (*pos)++;
834 if (*pos >= KPROBE_TABLE_SIZE)
835 return NULL;
836 return pos;
837}
838
839static void __kprobes kprobe_seq_stop(struct seq_file *f, void *v)
840{
841 /* Nothing to do */
842}
843
844static int __kprobes show_kprobe_addr(struct seq_file *pi, void *v)
845{
846 struct hlist_head *head;
847 struct hlist_node *node;
848 struct kprobe *p, *kp;
849 const char *sym = NULL;
850 unsigned int i = *(loff_t *) v;
ffb45122 851 unsigned long offset = 0;
346fd59b
SD
852 char *modname, namebuf[128];
853
854 head = &kprobe_table[i];
855 preempt_disable();
856 hlist_for_each_entry_rcu(p, node, head, hlist) {
ffb45122 857 sym = kallsyms_lookup((unsigned long)p->addr, NULL,
346fd59b
SD
858 &offset, &modname, namebuf);
859 if (p->pre_handler == aggr_pre_handler) {
860 list_for_each_entry_rcu(kp, &p->list, list)
861 report_probe(pi, kp, sym, offset, modname);
862 } else
863 report_probe(pi, p, sym, offset, modname);
864 }
865 preempt_enable();
866 return 0;
867}
868
869static struct seq_operations kprobes_seq_ops = {
870 .start = kprobe_seq_start,
871 .next = kprobe_seq_next,
872 .stop = kprobe_seq_stop,
873 .show = show_kprobe_addr
874};
875
876static int __kprobes kprobes_open(struct inode *inode, struct file *filp)
877{
878 return seq_open(filp, &kprobes_seq_ops);
879}
880
881static struct file_operations debugfs_kprobes_operations = {
882 .open = kprobes_open,
883 .read = seq_read,
884 .llseek = seq_lseek,
885 .release = seq_release,
886};
887
888static int __kprobes debugfs_kprobe_init(void)
889{
890 struct dentry *dir, *file;
891
892 dir = debugfs_create_dir("kprobes", NULL);
893 if (!dir)
894 return -ENOMEM;
895
e3869792 896 file = debugfs_create_file("list", 0444, dir, NULL,
346fd59b
SD
897 &debugfs_kprobes_operations);
898 if (!file) {
899 debugfs_remove(dir);
900 return -ENOMEM;
901 }
902
903 return 0;
904}
905
906late_initcall(debugfs_kprobe_init);
907#endif /* CONFIG_DEBUG_FS */
908
909module_init(init_kprobes);
1da177e4
LT
910
911EXPORT_SYMBOL_GPL(register_kprobe);
912EXPORT_SYMBOL_GPL(unregister_kprobe);
913EXPORT_SYMBOL_GPL(register_jprobe);
914EXPORT_SYMBOL_GPL(unregister_jprobe);
915EXPORT_SYMBOL_GPL(jprobe_return);
b94cce92
HN
916EXPORT_SYMBOL_GPL(register_kretprobe);
917EXPORT_SYMBOL_GPL(unregister_kretprobe);