]> bbs.cooldavid.org Git - net-next-2.6.git/blame - kernel/kprobes.c
kprobes: add (un)register_kprobes for batch registration
[net-next-2.6.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>
bf8f6e5b 46
d0aaff97 47#include <asm-generic/sections.h>
1da177e4
LT
48#include <asm/cacheflush.h>
49#include <asm/errno.h>
bf8f6e5b 50#include <asm/uaccess.h>
1da177e4
LT
51
52#define KPROBE_HASH_BITS 6
53#define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
54
3a872d89
AM
55
56/*
57 * Some oddball architectures like 64bit powerpc have function descriptors
58 * so this must be overridable.
59 */
60#ifndef kprobe_lookup_name
61#define kprobe_lookup_name(name, addr) \
62 addr = ((kprobe_opcode_t *)(kallsyms_lookup_name(name)))
63#endif
64
1da177e4 65static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
b94cce92 66static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
1da177e4 67
bf8f6e5b
AM
68/* NOTE: change this value only with kprobe_mutex held */
69static bool kprobe_enabled;
70
7a7d1cf9 71DEFINE_MUTEX(kprobe_mutex); /* Protects kprobe_table */
3516a460 72DEFINE_SPINLOCK(kretprobe_lock); /* Protects kretprobe_inst_table */
e6584523 73static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL;
1da177e4 74
3d8d996e
SD
75/*
76 * Normally, functions that we'd want to prohibit kprobes in, are marked
77 * __kprobes. But, there are cases where such functions already belong to
78 * a different section (__sched for preempt_schedule)
79 *
80 * For such cases, we now have a blacklist
81 */
82struct kprobe_blackpoint kprobe_blacklist[] = {
83 {"preempt_schedule",},
84 {NULL} /* Terminator */
85};
86
2d14e39d 87#ifdef __ARCH_WANT_KPROBES_INSN_SLOT
9ec4b1f3
AM
88/*
89 * kprobe->ainsn.insn points to the copy of the instruction to be
90 * single-stepped. x86_64, POWER4 and above have no-exec support and
91 * stepping on the instruction on a vmalloced/kmalloced/data page
92 * is a recipe for disaster
93 */
94#define INSNS_PER_PAGE (PAGE_SIZE/(MAX_INSN_SIZE * sizeof(kprobe_opcode_t)))
95
96struct kprobe_insn_page {
97 struct hlist_node hlist;
98 kprobe_opcode_t *insns; /* Page of instruction slots */
99 char slot_used[INSNS_PER_PAGE];
100 int nused;
b4c6c34a 101 int ngarbage;
9ec4b1f3
AM
102};
103
ab40c5c6
MH
104enum kprobe_slot_state {
105 SLOT_CLEAN = 0,
106 SLOT_DIRTY = 1,
107 SLOT_USED = 2,
108};
109
9ec4b1f3 110static struct hlist_head kprobe_insn_pages;
b4c6c34a
MH
111static int kprobe_garbage_slots;
112static int collect_garbage_slots(void);
113
114static int __kprobes check_safety(void)
115{
116 int ret = 0;
117#if defined(CONFIG_PREEMPT) && defined(CONFIG_PM)
118 ret = freeze_processes();
119 if (ret == 0) {
120 struct task_struct *p, *q;
121 do_each_thread(p, q) {
122 if (p != current && p->state == TASK_RUNNING &&
123 p->pid != 0) {
124 printk("Check failed: %s is running\n",p->comm);
125 ret = -1;
126 goto loop_end;
127 }
128 } while_each_thread(p, q);
129 }
130loop_end:
131 thaw_processes();
132#else
133 synchronize_sched();
134#endif
135 return ret;
136}
9ec4b1f3
AM
137
138/**
139 * get_insn_slot() - Find a slot on an executable page for an instruction.
140 * We allocate an executable page if there's no room on existing ones.
141 */
d0aaff97 142kprobe_opcode_t __kprobes *get_insn_slot(void)
9ec4b1f3
AM
143{
144 struct kprobe_insn_page *kip;
145 struct hlist_node *pos;
146
6f716acd 147 retry:
b0bb5016 148 hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) {
9ec4b1f3
AM
149 if (kip->nused < INSNS_PER_PAGE) {
150 int i;
151 for (i = 0; i < INSNS_PER_PAGE; i++) {
ab40c5c6
MH
152 if (kip->slot_used[i] == SLOT_CLEAN) {
153 kip->slot_used[i] = SLOT_USED;
9ec4b1f3
AM
154 kip->nused++;
155 return kip->insns + (i * MAX_INSN_SIZE);
156 }
157 }
158 /* Surprise! No unused slots. Fix kip->nused. */
159 kip->nused = INSNS_PER_PAGE;
160 }
161 }
162
b4c6c34a
MH
163 /* If there are any garbage slots, collect it and try again. */
164 if (kprobe_garbage_slots && collect_garbage_slots() == 0) {
165 goto retry;
166 }
167 /* All out of space. Need to allocate a new page. Use slot 0. */
9ec4b1f3 168 kip = kmalloc(sizeof(struct kprobe_insn_page), GFP_KERNEL);
6f716acd 169 if (!kip)
9ec4b1f3 170 return NULL;
9ec4b1f3
AM
171
172 /*
173 * Use module_alloc so this page is within +/- 2GB of where the
174 * kernel image and loaded module images reside. This is required
175 * so x86_64 can correctly handle the %rip-relative fixups.
176 */
177 kip->insns = module_alloc(PAGE_SIZE);
178 if (!kip->insns) {
179 kfree(kip);
180 return NULL;
181 }
182 INIT_HLIST_NODE(&kip->hlist);
183 hlist_add_head(&kip->hlist, &kprobe_insn_pages);
ab40c5c6
MH
184 memset(kip->slot_used, SLOT_CLEAN, INSNS_PER_PAGE);
185 kip->slot_used[0] = SLOT_USED;
9ec4b1f3 186 kip->nused = 1;
b4c6c34a 187 kip->ngarbage = 0;
9ec4b1f3
AM
188 return kip->insns;
189}
190
b4c6c34a
MH
191/* Return 1 if all garbages are collected, otherwise 0. */
192static int __kprobes collect_one_slot(struct kprobe_insn_page *kip, int idx)
193{
ab40c5c6 194 kip->slot_used[idx] = SLOT_CLEAN;
b4c6c34a
MH
195 kip->nused--;
196 if (kip->nused == 0) {
197 /*
198 * Page is no longer in use. Free it unless
199 * it's the last one. We keep the last one
200 * so as not to have to set it up again the
201 * next time somebody inserts a probe.
202 */
203 hlist_del(&kip->hlist);
204 if (hlist_empty(&kprobe_insn_pages)) {
205 INIT_HLIST_NODE(&kip->hlist);
206 hlist_add_head(&kip->hlist,
207 &kprobe_insn_pages);
208 } else {
209 module_free(NULL, kip->insns);
210 kfree(kip);
211 }
212 return 1;
213 }
214 return 0;
215}
216
217static int __kprobes collect_garbage_slots(void)
218{
219 struct kprobe_insn_page *kip;
220 struct hlist_node *pos, *next;
221
222 /* Ensure no-one is preepmted on the garbages */
223 if (check_safety() != 0)
224 return -EAGAIN;
225
b0bb5016 226 hlist_for_each_entry_safe(kip, pos, next, &kprobe_insn_pages, hlist) {
b4c6c34a 227 int i;
b4c6c34a
MH
228 if (kip->ngarbage == 0)
229 continue;
230 kip->ngarbage = 0; /* we will collect all garbages */
231 for (i = 0; i < INSNS_PER_PAGE; i++) {
ab40c5c6 232 if (kip->slot_used[i] == SLOT_DIRTY &&
b4c6c34a
MH
233 collect_one_slot(kip, i))
234 break;
235 }
236 }
237 kprobe_garbage_slots = 0;
238 return 0;
239}
240
241void __kprobes free_insn_slot(kprobe_opcode_t * slot, int dirty)
9ec4b1f3
AM
242{
243 struct kprobe_insn_page *kip;
244 struct hlist_node *pos;
245
b0bb5016 246 hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) {
9ec4b1f3
AM
247 if (kip->insns <= slot &&
248 slot < kip->insns + (INSNS_PER_PAGE * MAX_INSN_SIZE)) {
249 int i = (slot - kip->insns) / MAX_INSN_SIZE;
b4c6c34a 250 if (dirty) {
ab40c5c6 251 kip->slot_used[i] = SLOT_DIRTY;
b4c6c34a
MH
252 kip->ngarbage++;
253 } else {
254 collect_one_slot(kip, i);
9ec4b1f3 255 }
b4c6c34a 256 break;
9ec4b1f3
AM
257 }
258 }
6f716acd
CH
259
260 if (dirty && ++kprobe_garbage_slots > INSNS_PER_PAGE)
b4c6c34a 261 collect_garbage_slots();
9ec4b1f3 262}
2d14e39d 263#endif
9ec4b1f3 264
e6584523
AM
265/* We have preemption disabled.. so it is safe to use __ versions */
266static inline void set_kprobe_instance(struct kprobe *kp)
267{
268 __get_cpu_var(kprobe_instance) = kp;
269}
270
271static inline void reset_kprobe_instance(void)
272{
273 __get_cpu_var(kprobe_instance) = NULL;
274}
275
3516a460
AM
276/*
277 * This routine is called either:
49a2a1b8 278 * - under the kprobe_mutex - during kprobe_[un]register()
3516a460 279 * OR
d217d545 280 * - with preemption disabled - from arch/xxx/kernel/kprobes.c
3516a460 281 */
d0aaff97 282struct kprobe __kprobes *get_kprobe(void *addr)
1da177e4
LT
283{
284 struct hlist_head *head;
285 struct hlist_node *node;
3516a460 286 struct kprobe *p;
1da177e4
LT
287
288 head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
3516a460 289 hlist_for_each_entry_rcu(p, node, head, hlist) {
1da177e4
LT
290 if (p->addr == addr)
291 return p;
292 }
293 return NULL;
294}
295
64f562c6
AM
296/*
297 * Aggregate handlers for multiple kprobes support - these handlers
298 * take care of invoking the individual kprobe handlers on p->list
299 */
d0aaff97 300static int __kprobes aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
64f562c6
AM
301{
302 struct kprobe *kp;
303
3516a460 304 list_for_each_entry_rcu(kp, &p->list, list) {
64f562c6 305 if (kp->pre_handler) {
e6584523 306 set_kprobe_instance(kp);
8b0914ea
PP
307 if (kp->pre_handler(kp, regs))
308 return 1;
64f562c6 309 }
e6584523 310 reset_kprobe_instance();
64f562c6
AM
311 }
312 return 0;
313}
314
d0aaff97
PP
315static void __kprobes aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
316 unsigned long flags)
64f562c6
AM
317{
318 struct kprobe *kp;
319
3516a460 320 list_for_each_entry_rcu(kp, &p->list, list) {
64f562c6 321 if (kp->post_handler) {
e6584523 322 set_kprobe_instance(kp);
64f562c6 323 kp->post_handler(kp, regs, flags);
e6584523 324 reset_kprobe_instance();
64f562c6
AM
325 }
326 }
64f562c6
AM
327}
328
d0aaff97
PP
329static int __kprobes aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
330 int trapnr)
64f562c6 331{
e6584523
AM
332 struct kprobe *cur = __get_cpu_var(kprobe_instance);
333
64f562c6
AM
334 /*
335 * if we faulted "during" the execution of a user specified
336 * probe handler, invoke just that probe's fault handler
337 */
e6584523
AM
338 if (cur && cur->fault_handler) {
339 if (cur->fault_handler(cur, regs, trapnr))
64f562c6
AM
340 return 1;
341 }
342 return 0;
343}
344
d0aaff97 345static int __kprobes aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
8b0914ea 346{
e6584523
AM
347 struct kprobe *cur = __get_cpu_var(kprobe_instance);
348 int ret = 0;
349
350 if (cur && cur->break_handler) {
351 if (cur->break_handler(cur, regs))
352 ret = 1;
8b0914ea 353 }
e6584523
AM
354 reset_kprobe_instance();
355 return ret;
8b0914ea
PP
356}
357
bf8d5c52
KA
358/* Walks the list and increments nmissed count for multiprobe case */
359void __kprobes kprobes_inc_nmissed_count(struct kprobe *p)
360{
361 struct kprobe *kp;
362 if (p->pre_handler != aggr_pre_handler) {
363 p->nmissed++;
364 } else {
365 list_for_each_entry_rcu(kp, &p->list, list)
366 kp->nmissed++;
367 }
368 return;
369}
370
3516a460 371/* Called with kretprobe_lock held */
99219a3f 372void __kprobes recycle_rp_inst(struct kretprobe_instance *ri,
373 struct hlist_head *head)
b94cce92
HN
374{
375 /* remove rp inst off the rprobe_inst_table */
376 hlist_del(&ri->hlist);
377 if (ri->rp) {
378 /* remove rp inst off the used list */
379 hlist_del(&ri->uflist);
380 /* put rp inst back onto the free list */
381 INIT_HLIST_NODE(&ri->uflist);
382 hlist_add_head(&ri->uflist, &ri->rp->free_instances);
383 } else
384 /* Unregistering */
99219a3f 385 hlist_add_head(&ri->hlist, head);
b94cce92
HN
386}
387
d0aaff97 388struct hlist_head __kprobes *kretprobe_inst_table_head(struct task_struct *tsk)
b94cce92
HN
389{
390 return &kretprobe_inst_table[hash_ptr(tsk, KPROBE_HASH_BITS)];
391}
392
b94cce92 393/*
c6fd91f0 394 * This function is called from finish_task_switch when task tk becomes dead,
395 * so that we can recycle any function-return probe instances associated
396 * with this task. These left over instances represent probed functions
397 * that have been called but will never return.
b94cce92 398 */
d0aaff97 399void __kprobes kprobe_flush_task(struct task_struct *tk)
b94cce92 400{
62c27be0 401 struct kretprobe_instance *ri;
99219a3f 402 struct hlist_head *head, empty_rp;
802eae7c 403 struct hlist_node *node, *tmp;
0aa55e4d 404 unsigned long flags = 0;
802eae7c 405
99219a3f 406 INIT_HLIST_HEAD(&empty_rp);
3516a460 407 spin_lock_irqsave(&kretprobe_lock, flags);
62c27be0 408 head = kretprobe_inst_table_head(tk);
409 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
410 if (ri->task == tk)
99219a3f 411 recycle_rp_inst(ri, &empty_rp);
62c27be0 412 }
3516a460 413 spin_unlock_irqrestore(&kretprobe_lock, flags);
99219a3f 414
415 hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
416 hlist_del(&ri->hlist);
417 kfree(ri);
418 }
b94cce92
HN
419}
420
b94cce92
HN
421static inline void free_rp_inst(struct kretprobe *rp)
422{
423 struct kretprobe_instance *ri;
4c4308cb
CH
424 struct hlist_node *pos, *next;
425
426 hlist_for_each_entry_safe(ri, pos, next, &rp->free_instances, uflist) {
b94cce92
HN
427 hlist_del(&ri->uflist);
428 kfree(ri);
429 }
430}
431
8b0914ea
PP
432/*
433 * Keep all fields in the kprobe consistent
434 */
435static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p)
436{
437 memcpy(&p->opcode, &old_p->opcode, sizeof(kprobe_opcode_t));
438 memcpy(&p->ainsn, &old_p->ainsn, sizeof(struct arch_specific_insn));
439}
440
441/*
442* Add the new probe to old_p->list. Fail if this is the
443* second jprobe at the address - two jprobes can't coexist
444*/
d0aaff97 445static int __kprobes add_new_kprobe(struct kprobe *old_p, struct kprobe *p)
8b0914ea 446{
8b0914ea 447 if (p->break_handler) {
36721656 448 if (old_p->break_handler)
449 return -EEXIST;
3516a460 450 list_add_tail_rcu(&p->list, &old_p->list);
36721656 451 old_p->break_handler = aggr_break_handler;
8b0914ea 452 } else
3516a460 453 list_add_rcu(&p->list, &old_p->list);
36721656 454 if (p->post_handler && !old_p->post_handler)
455 old_p->post_handler = aggr_post_handler;
8b0914ea
PP
456 return 0;
457}
458
64f562c6
AM
459/*
460 * Fill in the required fields of the "manager kprobe". Replace the
461 * earlier kprobe in the hlist with the manager kprobe
462 */
463static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
464{
8b0914ea 465 copy_kprobe(p, ap);
a9ad965e 466 flush_insn_slot(ap);
64f562c6 467 ap->addr = p->addr;
64f562c6 468 ap->pre_handler = aggr_pre_handler;
64f562c6 469 ap->fault_handler = aggr_fault_handler;
36721656 470 if (p->post_handler)
471 ap->post_handler = aggr_post_handler;
472 if (p->break_handler)
473 ap->break_handler = aggr_break_handler;
64f562c6
AM
474
475 INIT_LIST_HEAD(&ap->list);
3516a460 476 list_add_rcu(&p->list, &ap->list);
64f562c6 477
adad0f33 478 hlist_replace_rcu(&p->hlist, &ap->hlist);
64f562c6
AM
479}
480
481/*
482 * This is the second or subsequent kprobe at the address - handle
483 * the intricacies
64f562c6 484 */
d0aaff97
PP
485static int __kprobes register_aggr_kprobe(struct kprobe *old_p,
486 struct kprobe *p)
64f562c6
AM
487{
488 int ret = 0;
489 struct kprobe *ap;
490
8b0914ea
PP
491 if (old_p->pre_handler == aggr_pre_handler) {
492 copy_kprobe(old_p, p);
493 ret = add_new_kprobe(old_p, p);
64f562c6 494 } else {
a0d50069 495 ap = kzalloc(sizeof(struct kprobe), GFP_KERNEL);
64f562c6
AM
496 if (!ap)
497 return -ENOMEM;
498 add_aggr_kprobe(ap, old_p);
8b0914ea
PP
499 copy_kprobe(ap, p);
500 ret = add_new_kprobe(ap, p);
64f562c6
AM
501 }
502 return ret;
503}
504
d0aaff97
PP
505static int __kprobes in_kprobes_functions(unsigned long addr)
506{
3d8d996e
SD
507 struct kprobe_blackpoint *kb;
508
6f716acd
CH
509 if (addr >= (unsigned long)__kprobes_text_start &&
510 addr < (unsigned long)__kprobes_text_end)
d0aaff97 511 return -EINVAL;
3d8d996e
SD
512 /*
513 * If there exists a kprobe_blacklist, verify and
514 * fail any probe registration in the prohibited area
515 */
516 for (kb = kprobe_blacklist; kb->name != NULL; kb++) {
517 if (kb->start_addr) {
518 if (addr >= kb->start_addr &&
519 addr < (kb->start_addr + kb->range))
520 return -EINVAL;
521 }
522 }
d0aaff97
PP
523 return 0;
524}
525
b2a5cd69
MH
526/*
527 * If we have a symbol_name argument, look it up and add the offset field
528 * to it. This way, we can specify a relative address to a symbol.
529 */
530static kprobe_opcode_t __kprobes *kprobe_addr(struct kprobe *p)
531{
532 kprobe_opcode_t *addr = p->addr;
533 if (p->symbol_name) {
534 if (addr)
535 return NULL;
536 kprobe_lookup_name(p->symbol_name, addr);
537 }
538
539 if (!addr)
540 return NULL;
541 return (kprobe_opcode_t *)(((char *)addr) + p->offset);
542}
543
df019b1d
KA
544static int __kprobes __register_kprobe(struct kprobe *p,
545 unsigned long called_from)
1da177e4
LT
546{
547 int ret = 0;
64f562c6 548 struct kprobe *old_p;
df019b1d 549 struct module *probed_mod;
b2a5cd69 550 kprobe_opcode_t *addr;
b3e55c72 551
b2a5cd69
MH
552 addr = kprobe_addr(p);
553 if (!addr)
3a872d89 554 return -EINVAL;
b2a5cd69 555 p->addr = addr;
3a872d89 556
6f716acd
CH
557 if (!kernel_text_address((unsigned long) p->addr) ||
558 in_kprobes_functions((unsigned long) p->addr))
b3e55c72
MB
559 return -EINVAL;
560
df019b1d 561 p->mod_refcounted = 0;
6f716acd
CH
562
563 /*
564 * Check if are we probing a module.
565 */
566 probed_mod = module_text_address((unsigned long) p->addr);
567 if (probed_mod) {
df019b1d 568 struct module *calling_mod = module_text_address(called_from);
6f716acd
CH
569 /*
570 * We must allow modules to probe themself and in this case
571 * avoid incrementing the module refcount, so as to allow
572 * unloading of self probing modules.
df019b1d 573 */
6f716acd 574 if (calling_mod && calling_mod != probed_mod) {
df019b1d
KA
575 if (unlikely(!try_module_get(probed_mod)))
576 return -EINVAL;
577 p->mod_refcounted = 1;
578 } else
579 probed_mod = NULL;
580 }
1da177e4 581
3516a460 582 p->nmissed = 0;
9861668f 583 INIT_LIST_HEAD(&p->list);
7a7d1cf9 584 mutex_lock(&kprobe_mutex);
64f562c6
AM
585 old_p = get_kprobe(p->addr);
586 if (old_p) {
587 ret = register_aggr_kprobe(old_p, p);
1da177e4
LT
588 goto out;
589 }
1da177e4 590
6f716acd
CH
591 ret = arch_prepare_kprobe(p);
592 if (ret)
49a2a1b8
AK
593 goto out;
594
64f562c6 595 INIT_HLIST_NODE(&p->hlist);
3516a460 596 hlist_add_head_rcu(&p->hlist,
1da177e4
LT
597 &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
598
74a0b576 599 if (kprobe_enabled)
bf8f6e5b 600 arch_arm_kprobe(p);
74a0b576 601
1da177e4 602out:
7a7d1cf9 603 mutex_unlock(&kprobe_mutex);
49a2a1b8 604
df019b1d
KA
605 if (ret && probed_mod)
606 module_put(probed_mod);
1da177e4
LT
607 return ret;
608}
609
9861668f
MH
610/*
611 * Unregister a kprobe without a scheduler synchronization.
612 */
613static int __kprobes __unregister_kprobe_top(struct kprobe *p)
1da177e4 614{
f709b122 615 struct kprobe *old_p, *list_p;
64f562c6 616
64f562c6 617 old_p = get_kprobe(p->addr);
9861668f
MH
618 if (unlikely(!old_p))
619 return -EINVAL;
620
f709b122
KA
621 if (p != old_p) {
622 list_for_each_entry_rcu(list_p, &old_p->list, list)
623 if (list_p == p)
624 /* kprobe p is a valid probe */
625 goto valid_p;
9861668f 626 return -EINVAL;
f709b122
KA
627 }
628valid_p:
6f716acd
CH
629 if (old_p == p ||
630 (old_p->pre_handler == aggr_pre_handler &&
9861668f 631 list_is_singular(&old_p->list))) {
bf8f6e5b
AM
632 /*
633 * Only probe on the hash list. Disarm only if kprobes are
634 * enabled - otherwise, the breakpoint would already have
635 * been removed. We save on flushing icache.
636 */
637 if (kprobe_enabled)
638 arch_disarm_kprobe(p);
49a2a1b8 639 hlist_del_rcu(&old_p->hlist);
49a2a1b8 640 } else {
9861668f
MH
641 if (p->break_handler)
642 old_p->break_handler = NULL;
643 if (p->post_handler) {
644 list_for_each_entry_rcu(list_p, &old_p->list, list) {
645 if ((list_p != p) && (list_p->post_handler))
646 goto noclean;
647 }
648 old_p->post_handler = NULL;
649 }
650noclean:
49a2a1b8 651 list_del_rcu(&p->list);
49a2a1b8 652 }
9861668f
MH
653 return 0;
654}
3516a460 655
9861668f
MH
656static void __kprobes __unregister_kprobe_bottom(struct kprobe *p)
657{
658 struct module *mod;
659 struct kprobe *old_p;
b3e55c72 660
6f716acd
CH
661 if (p->mod_refcounted) {
662 mod = module_text_address((unsigned long)p->addr);
663 if (mod)
664 module_put(mod);
665 }
b3e55c72 666
9861668f
MH
667 if (list_empty(&p->list) || list_is_singular(&p->list)) {
668 if (!list_empty(&p->list)) {
669 /* "p" is the last child of an aggr_kprobe */
670 old_p = list_entry(p->list.next, struct kprobe, list);
671 list_del(&p->list);
3516a460 672 kfree(old_p);
49a2a1b8 673 }
0498b635 674 arch_remove_kprobe(p);
9861668f
MH
675 }
676}
677
678static int __register_kprobes(struct kprobe **kps, int num,
679 unsigned long called_from)
680{
681 int i, ret = 0;
682
683 if (num <= 0)
684 return -EINVAL;
685 for (i = 0; i < num; i++) {
686 ret = __register_kprobe(kps[i], called_from);
687 if (ret < 0 && i > 0) {
688 unregister_kprobes(kps, i);
689 break;
36721656 690 }
49a2a1b8 691 }
9861668f
MH
692 return ret;
693}
694
695/*
696 * Registration and unregistration functions for kprobe.
697 */
698int __kprobes register_kprobe(struct kprobe *p)
699{
700 return __register_kprobes(&p, 1,
701 (unsigned long)__builtin_return_address(0));
702}
703
704void __kprobes unregister_kprobe(struct kprobe *p)
705{
706 unregister_kprobes(&p, 1);
707}
708
709int __kprobes register_kprobes(struct kprobe **kps, int num)
710{
711 return __register_kprobes(kps, num,
712 (unsigned long)__builtin_return_address(0));
713}
714
715void __kprobes unregister_kprobes(struct kprobe **kps, int num)
716{
717 int i;
718
719 if (num <= 0)
720 return;
721 mutex_lock(&kprobe_mutex);
722 for (i = 0; i < num; i++)
723 if (__unregister_kprobe_top(kps[i]) < 0)
724 kps[i]->addr = NULL;
725 mutex_unlock(&kprobe_mutex);
726
727 synchronize_sched();
728 for (i = 0; i < num; i++)
729 if (kps[i]->addr)
730 __unregister_kprobe_bottom(kps[i]);
1da177e4
LT
731}
732
733static struct notifier_block kprobe_exceptions_nb = {
3d5631e0
AK
734 .notifier_call = kprobe_exceptions_notify,
735 .priority = 0x7fffffff /* we need to be notified first */
736};
737
3d7e3382
ME
738unsigned long __weak arch_deref_entry_point(void *entry)
739{
740 return (unsigned long)entry;
741}
1da177e4 742
d0aaff97 743int __kprobes register_jprobe(struct jprobe *jp)
1da177e4 744{
3d7e3382
ME
745 unsigned long addr = arch_deref_entry_point(jp->entry);
746
747 if (!kernel_text_address(addr))
748 return -EINVAL;
749
1da177e4
LT
750 /* Todo: Verify probepoint is a function entry point */
751 jp->kp.pre_handler = setjmp_pre_handler;
752 jp->kp.break_handler = longjmp_break_handler;
753
df019b1d
KA
754 return __register_kprobe(&jp->kp,
755 (unsigned long)__builtin_return_address(0));
1da177e4
LT
756}
757
d0aaff97 758void __kprobes unregister_jprobe(struct jprobe *jp)
1da177e4
LT
759{
760 unregister_kprobe(&jp->kp);
761}
762
9edddaa2 763#ifdef CONFIG_KRETPROBES
e65cefe8
AB
764/*
765 * This kprobe pre_handler is registered with every kretprobe. When probe
766 * hits it will set up the return probe.
767 */
768static int __kprobes pre_handler_kretprobe(struct kprobe *p,
769 struct pt_regs *regs)
770{
771 struct kretprobe *rp = container_of(p, struct kretprobe, kp);
772 unsigned long flags = 0;
773
774 /*TODO: consider to only swap the RA after the last pre_handler fired */
775 spin_lock_irqsave(&kretprobe_lock, flags);
4c4308cb
CH
776 if (!hlist_empty(&rp->free_instances)) {
777 struct kretprobe_instance *ri;
778
779 ri = hlist_entry(rp->free_instances.first,
780 struct kretprobe_instance, uflist);
781 ri->rp = rp;
782 ri->task = current;
f47cd9b5
AS
783
784 if (rp->entry_handler && rp->entry_handler(ri, regs)) {
785 spin_unlock_irqrestore(&kretprobe_lock, flags);
786 return 0;
787 }
788
4c4308cb
CH
789 arch_prepare_kretprobe(ri, regs);
790
791 /* XXX(hch): why is there no hlist_move_head? */
792 hlist_del(&ri->uflist);
793 hlist_add_head(&ri->uflist, &ri->rp->used_instances);
794 hlist_add_head(&ri->hlist, kretprobe_inst_table_head(ri->task));
795 } else
796 rp->nmissed++;
e65cefe8
AB
797 spin_unlock_irqrestore(&kretprobe_lock, flags);
798 return 0;
799}
800
d0aaff97 801int __kprobes register_kretprobe(struct kretprobe *rp)
b94cce92
HN
802{
803 int ret = 0;
804 struct kretprobe_instance *inst;
805 int i;
b2a5cd69 806 void *addr;
f438d914
MH
807
808 if (kretprobe_blacklist_size) {
b2a5cd69
MH
809 addr = kprobe_addr(&rp->kp);
810 if (!addr)
811 return -EINVAL;
f438d914
MH
812
813 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
814 if (kretprobe_blacklist[i].addr == addr)
815 return -EINVAL;
816 }
817 }
b94cce92
HN
818
819 rp->kp.pre_handler = pre_handler_kretprobe;
7522a842
AM
820 rp->kp.post_handler = NULL;
821 rp->kp.fault_handler = NULL;
822 rp->kp.break_handler = NULL;
b94cce92
HN
823
824 /* Pre-allocate memory for max kretprobe instances */
825 if (rp->maxactive <= 0) {
826#ifdef CONFIG_PREEMPT
827 rp->maxactive = max(10, 2 * NR_CPUS);
828#else
829 rp->maxactive = NR_CPUS;
830#endif
831 }
832 INIT_HLIST_HEAD(&rp->used_instances);
833 INIT_HLIST_HEAD(&rp->free_instances);
834 for (i = 0; i < rp->maxactive; i++) {
f47cd9b5
AS
835 inst = kmalloc(sizeof(struct kretprobe_instance) +
836 rp->data_size, GFP_KERNEL);
b94cce92
HN
837 if (inst == NULL) {
838 free_rp_inst(rp);
839 return -ENOMEM;
840 }
841 INIT_HLIST_NODE(&inst->uflist);
842 hlist_add_head(&inst->uflist, &rp->free_instances);
843 }
844
845 rp->nmissed = 0;
846 /* Establish function entry probe point */
df019b1d
KA
847 if ((ret = __register_kprobe(&rp->kp,
848 (unsigned long)__builtin_return_address(0))) != 0)
b94cce92
HN
849 free_rp_inst(rp);
850 return ret;
851}
852
9edddaa2 853#else /* CONFIG_KRETPROBES */
d0aaff97 854int __kprobes register_kretprobe(struct kretprobe *rp)
b94cce92
HN
855{
856 return -ENOSYS;
857}
858
346fd59b
SD
859static int __kprobes pre_handler_kretprobe(struct kprobe *p,
860 struct pt_regs *regs)
861{
862 return 0;
863}
9edddaa2 864#endif /* CONFIG_KRETPROBES */
b94cce92 865
d0aaff97 866void __kprobes unregister_kretprobe(struct kretprobe *rp)
b94cce92
HN
867{
868 unsigned long flags;
869 struct kretprobe_instance *ri;
4c4308cb 870 struct hlist_node *pos, *next;
b94cce92
HN
871
872 unregister_kprobe(&rp->kp);
4c4308cb 873
b94cce92 874 /* No race here */
3516a460 875 spin_lock_irqsave(&kretprobe_lock, flags);
4c4308cb 876 hlist_for_each_entry_safe(ri, pos, next, &rp->used_instances, uflist) {
b94cce92
HN
877 ri->rp = NULL;
878 hlist_del(&ri->uflist);
879 }
3516a460 880 spin_unlock_irqrestore(&kretprobe_lock, flags);
278ff953 881 free_rp_inst(rp);
b94cce92
HN
882}
883
1da177e4
LT
884static int __init init_kprobes(void)
885{
886 int i, err = 0;
3d8d996e
SD
887 unsigned long offset = 0, size = 0;
888 char *modname, namebuf[128];
889 const char *symbol_name;
890 void *addr;
891 struct kprobe_blackpoint *kb;
1da177e4
LT
892
893 /* FIXME allocate the probe table, currently defined statically */
894 /* initialize all list heads */
b94cce92 895 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
1da177e4 896 INIT_HLIST_HEAD(&kprobe_table[i]);
b94cce92
HN
897 INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
898 }
1da177e4 899
3d8d996e
SD
900 /*
901 * Lookup and populate the kprobe_blacklist.
902 *
903 * Unlike the kretprobe blacklist, we'll need to determine
904 * the range of addresses that belong to the said functions,
905 * since a kprobe need not necessarily be at the beginning
906 * of a function.
907 */
908 for (kb = kprobe_blacklist; kb->name != NULL; kb++) {
909 kprobe_lookup_name(kb->name, addr);
910 if (!addr)
911 continue;
912
913 kb->start_addr = (unsigned long)addr;
914 symbol_name = kallsyms_lookup(kb->start_addr,
915 &size, &offset, &modname, namebuf);
916 if (!symbol_name)
917 kb->range = 0;
918 else
919 kb->range = size;
920 }
921
f438d914
MH
922 if (kretprobe_blacklist_size) {
923 /* lookup the function address from its name */
924 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
925 kprobe_lookup_name(kretprobe_blacklist[i].name,
926 kretprobe_blacklist[i].addr);
927 if (!kretprobe_blacklist[i].addr)
928 printk("kretprobe: lookup failed: %s\n",
929 kretprobe_blacklist[i].name);
930 }
931 }
932
bf8f6e5b
AM
933 /* By default, kprobes are enabled */
934 kprobe_enabled = true;
935
6772926b 936 err = arch_init_kprobes();
802eae7c
RL
937 if (!err)
938 err = register_die_notifier(&kprobe_exceptions_nb);
939
8c1c9356
AM
940 if (!err)
941 init_test_probes();
1da177e4
LT
942 return err;
943}
944
346fd59b
SD
945#ifdef CONFIG_DEBUG_FS
946static void __kprobes report_probe(struct seq_file *pi, struct kprobe *p,
bf8f6e5b 947 const char *sym, int offset,char *modname)
346fd59b
SD
948{
949 char *kprobe_type;
950
951 if (p->pre_handler == pre_handler_kretprobe)
952 kprobe_type = "r";
953 else if (p->pre_handler == setjmp_pre_handler)
954 kprobe_type = "j";
955 else
956 kprobe_type = "k";
957 if (sym)
958 seq_printf(pi, "%p %s %s+0x%x %s\n", p->addr, kprobe_type,
959 sym, offset, (modname ? modname : " "));
960 else
961 seq_printf(pi, "%p %s %p\n", p->addr, kprobe_type, p->addr);
962}
963
964static void __kprobes *kprobe_seq_start(struct seq_file *f, loff_t *pos)
965{
966 return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL;
967}
968
969static void __kprobes *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos)
970{
971 (*pos)++;
972 if (*pos >= KPROBE_TABLE_SIZE)
973 return NULL;
974 return pos;
975}
976
977static void __kprobes kprobe_seq_stop(struct seq_file *f, void *v)
978{
979 /* Nothing to do */
980}
981
982static int __kprobes show_kprobe_addr(struct seq_file *pi, void *v)
983{
984 struct hlist_head *head;
985 struct hlist_node *node;
986 struct kprobe *p, *kp;
987 const char *sym = NULL;
988 unsigned int i = *(loff_t *) v;
ffb45122 989 unsigned long offset = 0;
346fd59b
SD
990 char *modname, namebuf[128];
991
992 head = &kprobe_table[i];
993 preempt_disable();
994 hlist_for_each_entry_rcu(p, node, head, hlist) {
ffb45122 995 sym = kallsyms_lookup((unsigned long)p->addr, NULL,
346fd59b
SD
996 &offset, &modname, namebuf);
997 if (p->pre_handler == aggr_pre_handler) {
998 list_for_each_entry_rcu(kp, &p->list, list)
999 report_probe(pi, kp, sym, offset, modname);
1000 } else
1001 report_probe(pi, p, sym, offset, modname);
1002 }
1003 preempt_enable();
1004 return 0;
1005}
1006
1007static struct seq_operations kprobes_seq_ops = {
1008 .start = kprobe_seq_start,
1009 .next = kprobe_seq_next,
1010 .stop = kprobe_seq_stop,
1011 .show = show_kprobe_addr
1012};
1013
1014static int __kprobes kprobes_open(struct inode *inode, struct file *filp)
1015{
1016 return seq_open(filp, &kprobes_seq_ops);
1017}
1018
1019static struct file_operations debugfs_kprobes_operations = {
1020 .open = kprobes_open,
1021 .read = seq_read,
1022 .llseek = seq_lseek,
1023 .release = seq_release,
1024};
1025
bf8f6e5b
AM
1026static void __kprobes enable_all_kprobes(void)
1027{
1028 struct hlist_head *head;
1029 struct hlist_node *node;
1030 struct kprobe *p;
1031 unsigned int i;
1032
1033 mutex_lock(&kprobe_mutex);
1034
1035 /* If kprobes are already enabled, just return */
1036 if (kprobe_enabled)
1037 goto already_enabled;
1038
bf8f6e5b
AM
1039 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
1040 head = &kprobe_table[i];
1041 hlist_for_each_entry_rcu(p, node, head, hlist)
1042 arch_arm_kprobe(p);
1043 }
1044
1045 kprobe_enabled = true;
1046 printk(KERN_INFO "Kprobes globally enabled\n");
1047
1048already_enabled:
1049 mutex_unlock(&kprobe_mutex);
1050 return;
1051}
1052
1053static void __kprobes disable_all_kprobes(void)
1054{
1055 struct hlist_head *head;
1056 struct hlist_node *node;
1057 struct kprobe *p;
1058 unsigned int i;
1059
1060 mutex_lock(&kprobe_mutex);
1061
1062 /* If kprobes are already disabled, just return */
1063 if (!kprobe_enabled)
1064 goto already_disabled;
1065
1066 kprobe_enabled = false;
1067 printk(KERN_INFO "Kprobes globally disabled\n");
1068 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
1069 head = &kprobe_table[i];
1070 hlist_for_each_entry_rcu(p, node, head, hlist) {
1071 if (!arch_trampoline_kprobe(p))
1072 arch_disarm_kprobe(p);
1073 }
1074 }
1075
1076 mutex_unlock(&kprobe_mutex);
1077 /* Allow all currently running kprobes to complete */
1078 synchronize_sched();
74a0b576 1079 return;
bf8f6e5b
AM
1080
1081already_disabled:
1082 mutex_unlock(&kprobe_mutex);
1083 return;
1084}
1085
1086/*
1087 * XXX: The debugfs bool file interface doesn't allow for callbacks
1088 * when the bool state is switched. We can reuse that facility when
1089 * available
1090 */
1091static ssize_t read_enabled_file_bool(struct file *file,
1092 char __user *user_buf, size_t count, loff_t *ppos)
1093{
1094 char buf[3];
1095
1096 if (kprobe_enabled)
1097 buf[0] = '1';
1098 else
1099 buf[0] = '0';
1100 buf[1] = '\n';
1101 buf[2] = 0x00;
1102 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
1103}
1104
1105static ssize_t write_enabled_file_bool(struct file *file,
1106 const char __user *user_buf, size_t count, loff_t *ppos)
1107{
1108 char buf[32];
1109 int buf_size;
1110
1111 buf_size = min(count, (sizeof(buf)-1));
1112 if (copy_from_user(buf, user_buf, buf_size))
1113 return -EFAULT;
1114
1115 switch (buf[0]) {
1116 case 'y':
1117 case 'Y':
1118 case '1':
1119 enable_all_kprobes();
1120 break;
1121 case 'n':
1122 case 'N':
1123 case '0':
1124 disable_all_kprobes();
1125 break;
1126 }
1127
1128 return count;
1129}
1130
1131static struct file_operations fops_kp = {
1132 .read = read_enabled_file_bool,
1133 .write = write_enabled_file_bool,
1134};
1135
346fd59b
SD
1136static int __kprobes debugfs_kprobe_init(void)
1137{
1138 struct dentry *dir, *file;
bf8f6e5b 1139 unsigned int value = 1;
346fd59b
SD
1140
1141 dir = debugfs_create_dir("kprobes", NULL);
1142 if (!dir)
1143 return -ENOMEM;
1144
e3869792 1145 file = debugfs_create_file("list", 0444, dir, NULL,
346fd59b
SD
1146 &debugfs_kprobes_operations);
1147 if (!file) {
1148 debugfs_remove(dir);
1149 return -ENOMEM;
1150 }
1151
bf8f6e5b
AM
1152 file = debugfs_create_file("enabled", 0600, dir,
1153 &value, &fops_kp);
1154 if (!file) {
1155 debugfs_remove(dir);
1156 return -ENOMEM;
1157 }
1158
346fd59b
SD
1159 return 0;
1160}
1161
1162late_initcall(debugfs_kprobe_init);
1163#endif /* CONFIG_DEBUG_FS */
1164
1165module_init(init_kprobes);
1da177e4
LT
1166
1167EXPORT_SYMBOL_GPL(register_kprobe);
1168EXPORT_SYMBOL_GPL(unregister_kprobe);
9861668f
MH
1169EXPORT_SYMBOL_GPL(register_kprobes);
1170EXPORT_SYMBOL_GPL(unregister_kprobes);
1da177e4
LT
1171EXPORT_SYMBOL_GPL(register_jprobe);
1172EXPORT_SYMBOL_GPL(unregister_jprobe);
cd5bfea2 1173#ifdef CONFIG_KPROBES
1da177e4 1174EXPORT_SYMBOL_GPL(jprobe_return);
cd5bfea2
PC
1175#endif
1176
1177#ifdef CONFIG_KPROBES
b94cce92
HN
1178EXPORT_SYMBOL_GPL(register_kretprobe);
1179EXPORT_SYMBOL_GPL(unregister_kretprobe);
cd5bfea2 1180#endif