c3340e836c37bbb8c59abe4c09e856ae05383669
[linux-2.6.git] / kernel / kprobes.c
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.
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.
33  */
34 #include <linux/kprobes.h>
35 #include <linux/hash.h>
36 #include <linux/init.h>
37 #include <linux/slab.h>
38 #include <linux/stddef.h>
39 #include <linux/module.h>
40 #include <linux/moduleloader.h>
41 #include <linux/kallsyms.h>
42 #include <linux/freezer.h>
43 #include <linux/seq_file.h>
44 #include <linux/debugfs.h>
45 #include <linux/kdebug.h>
46 #include <linux/memory.h>
47 #include <linux/ftrace.h>
48
49 #include <asm-generic/sections.h>
50 #include <asm/cacheflush.h>
51 #include <asm/errno.h>
52 #include <asm/uaccess.h>
53
54 #define KPROBE_HASH_BITS 6
55 #define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
56
57
58 /*
59  * Some oddball architectures like 64bit powerpc have function descriptors
60  * so this must be overridable.
61  */
62 #ifndef kprobe_lookup_name
63 #define kprobe_lookup_name(name, addr) \
64         addr = ((kprobe_opcode_t *)(kallsyms_lookup_name(name)))
65 #endif
66
67 static int kprobes_initialized;
68 static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
69 static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
70
71 /* NOTE: change this value only with kprobe_mutex held */
72 static bool kprobes_all_disarmed;
73
74 static DEFINE_MUTEX(kprobe_mutex);      /* Protects kprobe_table */
75 static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL;
76 static struct {
77         spinlock_t lock ____cacheline_aligned_in_smp;
78 } kretprobe_table_locks[KPROBE_TABLE_SIZE];
79
80 static spinlock_t *kretprobe_table_lock_ptr(unsigned long hash)
81 {
82         return &(kretprobe_table_locks[hash].lock);
83 }
84
85 /*
86  * Normally, functions that we'd want to prohibit kprobes in, are marked
87  * __kprobes. But, there are cases where such functions already belong to
88  * a different section (__sched for preempt_schedule)
89  *
90  * For such cases, we now have a blacklist
91  */
92 static struct kprobe_blackpoint kprobe_blacklist[] = {
93         {"preempt_schedule",},
94         {"native_get_debugreg",},
95         {"irq_entries_start",},
96         {"common_interrupt",},
97         {NULL}    /* Terminator */
98 };
99
100 #ifdef __ARCH_WANT_KPROBES_INSN_SLOT
101 /*
102  * kprobe->ainsn.insn points to the copy of the instruction to be
103  * single-stepped. x86_64, POWER4 and above have no-exec support and
104  * stepping on the instruction on a vmalloced/kmalloced/data page
105  * is a recipe for disaster
106  */
107 #define INSNS_PER_PAGE  (PAGE_SIZE/(MAX_INSN_SIZE * sizeof(kprobe_opcode_t)))
108
109 struct kprobe_insn_page {
110         struct list_head list;
111         kprobe_opcode_t *insns;         /* Page of instruction slots */
112         char slot_used[INSNS_PER_PAGE];
113         int nused;
114         int ngarbage;
115 };
116
117 enum kprobe_slot_state {
118         SLOT_CLEAN = 0,
119         SLOT_DIRTY = 1,
120         SLOT_USED = 2,
121 };
122
123 static DEFINE_MUTEX(kprobe_insn_mutex); /* Protects kprobe_insn_pages */
124 static LIST_HEAD(kprobe_insn_pages);
125 static int kprobe_garbage_slots;
126 static int collect_garbage_slots(void);
127
128 /**
129  * __get_insn_slot() - Find a slot on an executable page for an instruction.
130  * We allocate an executable page if there's no room on existing ones.
131  */
132 static kprobe_opcode_t __kprobes *__get_insn_slot(void)
133 {
134         struct kprobe_insn_page *kip;
135
136  retry:
137         list_for_each_entry(kip, &kprobe_insn_pages, list) {
138                 if (kip->nused < INSNS_PER_PAGE) {
139                         int i;
140                         for (i = 0; i < INSNS_PER_PAGE; i++) {
141                                 if (kip->slot_used[i] == SLOT_CLEAN) {
142                                         kip->slot_used[i] = SLOT_USED;
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
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. */
157         kip = kmalloc(sizeof(struct kprobe_insn_page), GFP_KERNEL);
158         if (!kip)
159                 return NULL;
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_LIST_HEAD(&kip->list);
172         list_add(&kip->list, &kprobe_insn_pages);
173         memset(kip->slot_used, SLOT_CLEAN, INSNS_PER_PAGE);
174         kip->slot_used[0] = SLOT_USED;
175         kip->nused = 1;
176         kip->ngarbage = 0;
177         return kip->insns;
178 }
179
180 kprobe_opcode_t __kprobes *get_insn_slot(void)
181 {
182         kprobe_opcode_t *ret;
183         mutex_lock(&kprobe_insn_mutex);
184         ret = __get_insn_slot();
185         mutex_unlock(&kprobe_insn_mutex);
186         return ret;
187 }
188
189 /* Return 1 if all garbages are collected, otherwise 0. */
190 static int __kprobes collect_one_slot(struct kprobe_insn_page *kip, int idx)
191 {
192         kip->slot_used[idx] = SLOT_CLEAN;
193         kip->nused--;
194         if (kip->nused == 0) {
195                 /*
196                  * Page is no longer in use.  Free it unless
197                  * it's the last one.  We keep the last one
198                  * so as not to have to set it up again the
199                  * next time somebody inserts a probe.
200                  */
201                 if (!list_is_singular(&kprobe_insn_pages)) {
202                         list_del(&kip->list);
203                         module_free(NULL, kip->insns);
204                         kfree(kip);
205                 }
206                 return 1;
207         }
208         return 0;
209 }
210
211 static int __kprobes collect_garbage_slots(void)
212 {
213         struct kprobe_insn_page *kip, *next;
214
215         /* Ensure no-one is interrupted on the garbages */
216         synchronize_sched();
217
218         list_for_each_entry_safe(kip, next, &kprobe_insn_pages, list) {
219                 int i;
220                 if (kip->ngarbage == 0)
221                         continue;
222                 kip->ngarbage = 0;      /* we will collect all garbages */
223                 for (i = 0; i < INSNS_PER_PAGE; i++) {
224                         if (kip->slot_used[i] == SLOT_DIRTY &&
225                             collect_one_slot(kip, i))
226                                 break;
227                 }
228         }
229         kprobe_garbage_slots = 0;
230         return 0;
231 }
232
233 void __kprobes free_insn_slot(kprobe_opcode_t * slot, int dirty)
234 {
235         struct kprobe_insn_page *kip;
236
237         mutex_lock(&kprobe_insn_mutex);
238         list_for_each_entry(kip, &kprobe_insn_pages, list) {
239                 if (kip->insns <= slot &&
240                     slot < kip->insns + (INSNS_PER_PAGE * MAX_INSN_SIZE)) {
241                         int i = (slot - kip->insns) / MAX_INSN_SIZE;
242                         if (dirty) {
243                                 kip->slot_used[i] = SLOT_DIRTY;
244                                 kip->ngarbage++;
245                         } else
246                                 collect_one_slot(kip, i);
247                         break;
248                 }
249         }
250
251         if (dirty && ++kprobe_garbage_slots > INSNS_PER_PAGE)
252                 collect_garbage_slots();
253
254         mutex_unlock(&kprobe_insn_mutex);
255 }
256 #endif
257
258 /* We have preemption disabled.. so it is safe to use __ versions */
259 static inline void set_kprobe_instance(struct kprobe *kp)
260 {
261         __get_cpu_var(kprobe_instance) = kp;
262 }
263
264 static inline void reset_kprobe_instance(void)
265 {
266         __get_cpu_var(kprobe_instance) = NULL;
267 }
268
269 /*
270  * This routine is called either:
271  *      - under the kprobe_mutex - during kprobe_[un]register()
272  *                              OR
273  *      - with preemption disabled - from arch/xxx/kernel/kprobes.c
274  */
275 struct kprobe __kprobes *get_kprobe(void *addr)
276 {
277         struct hlist_head *head;
278         struct hlist_node *node;
279         struct kprobe *p;
280
281         head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
282         hlist_for_each_entry_rcu(p, node, head, hlist) {
283                 if (p->addr == addr)
284                         return p;
285         }
286         return NULL;
287 }
288
289 /* Arm a kprobe with text_mutex */
290 static void __kprobes arm_kprobe(struct kprobe *kp)
291 {
292         mutex_lock(&text_mutex);
293         arch_arm_kprobe(kp);
294         mutex_unlock(&text_mutex);
295 }
296
297 /* Disarm a kprobe with text_mutex */
298 static void __kprobes disarm_kprobe(struct kprobe *kp)
299 {
300         mutex_lock(&text_mutex);
301         arch_disarm_kprobe(kp);
302         mutex_unlock(&text_mutex);
303 }
304
305 /*
306  * Aggregate handlers for multiple kprobes support - these handlers
307  * take care of invoking the individual kprobe handlers on p->list
308  */
309 static int __kprobes aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
310 {
311         struct kprobe *kp;
312
313         list_for_each_entry_rcu(kp, &p->list, list) {
314                 if (kp->pre_handler && likely(!kprobe_disabled(kp))) {
315                         set_kprobe_instance(kp);
316                         if (kp->pre_handler(kp, regs))
317                                 return 1;
318                 }
319                 reset_kprobe_instance();
320         }
321         return 0;
322 }
323
324 static void __kprobes aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
325                                         unsigned long flags)
326 {
327         struct kprobe *kp;
328
329         list_for_each_entry_rcu(kp, &p->list, list) {
330                 if (kp->post_handler && likely(!kprobe_disabled(kp))) {
331                         set_kprobe_instance(kp);
332                         kp->post_handler(kp, regs, flags);
333                         reset_kprobe_instance();
334                 }
335         }
336 }
337
338 static int __kprobes aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
339                                         int trapnr)
340 {
341         struct kprobe *cur = __get_cpu_var(kprobe_instance);
342
343         /*
344          * if we faulted "during" the execution of a user specified
345          * probe handler, invoke just that probe's fault handler
346          */
347         if (cur && cur->fault_handler) {
348                 if (cur->fault_handler(cur, regs, trapnr))
349                         return 1;
350         }
351         return 0;
352 }
353
354 static int __kprobes aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
355 {
356         struct kprobe *cur = __get_cpu_var(kprobe_instance);
357         int ret = 0;
358
359         if (cur && cur->break_handler) {
360                 if (cur->break_handler(cur, regs))
361                         ret = 1;
362         }
363         reset_kprobe_instance();
364         return ret;
365 }
366
367 /* Walks the list and increments nmissed count for multiprobe case */
368 void __kprobes kprobes_inc_nmissed_count(struct kprobe *p)
369 {
370         struct kprobe *kp;
371         if (p->pre_handler != aggr_pre_handler) {
372                 p->nmissed++;
373         } else {
374                 list_for_each_entry_rcu(kp, &p->list, list)
375                         kp->nmissed++;
376         }
377         return;
378 }
379
380 void __kprobes recycle_rp_inst(struct kretprobe_instance *ri,
381                                 struct hlist_head *head)
382 {
383         struct kretprobe *rp = ri->rp;
384
385         /* remove rp inst off the rprobe_inst_table */
386         hlist_del(&ri->hlist);
387         INIT_HLIST_NODE(&ri->hlist);
388         if (likely(rp)) {
389                 spin_lock(&rp->lock);
390                 hlist_add_head(&ri->hlist, &rp->free_instances);
391                 spin_unlock(&rp->lock);
392         } else
393                 /* Unregistering */
394                 hlist_add_head(&ri->hlist, head);
395 }
396
397 void __kprobes kretprobe_hash_lock(struct task_struct *tsk,
398                          struct hlist_head **head, unsigned long *flags)
399 {
400         unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS);
401         spinlock_t *hlist_lock;
402
403         *head = &kretprobe_inst_table[hash];
404         hlist_lock = kretprobe_table_lock_ptr(hash);
405         spin_lock_irqsave(hlist_lock, *flags);
406 }
407
408 static void __kprobes kretprobe_table_lock(unsigned long hash,
409         unsigned long *flags)
410 {
411         spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash);
412         spin_lock_irqsave(hlist_lock, *flags);
413 }
414
415 void __kprobes kretprobe_hash_unlock(struct task_struct *tsk,
416         unsigned long *flags)
417 {
418         unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS);
419         spinlock_t *hlist_lock;
420
421         hlist_lock = kretprobe_table_lock_ptr(hash);
422         spin_unlock_irqrestore(hlist_lock, *flags);
423 }
424
425 void __kprobes kretprobe_table_unlock(unsigned long hash, unsigned long *flags)
426 {
427         spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash);
428         spin_unlock_irqrestore(hlist_lock, *flags);
429 }
430
431 /*
432  * This function is called from finish_task_switch when task tk becomes dead,
433  * so that we can recycle any function-return probe instances associated
434  * with this task. These left over instances represent probed functions
435  * that have been called but will never return.
436  */
437 void __kprobes kprobe_flush_task(struct task_struct *tk)
438 {
439         struct kretprobe_instance *ri;
440         struct hlist_head *head, empty_rp;
441         struct hlist_node *node, *tmp;
442         unsigned long hash, flags = 0;
443
444         if (unlikely(!kprobes_initialized))
445                 /* Early boot.  kretprobe_table_locks not yet initialized. */
446                 return;
447
448         hash = hash_ptr(tk, KPROBE_HASH_BITS);
449         head = &kretprobe_inst_table[hash];
450         kretprobe_table_lock(hash, &flags);
451         hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
452                 if (ri->task == tk)
453                         recycle_rp_inst(ri, &empty_rp);
454         }
455         kretprobe_table_unlock(hash, &flags);
456         INIT_HLIST_HEAD(&empty_rp);
457         hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
458                 hlist_del(&ri->hlist);
459                 kfree(ri);
460         }
461 }
462
463 static inline void free_rp_inst(struct kretprobe *rp)
464 {
465         struct kretprobe_instance *ri;
466         struct hlist_node *pos, *next;
467
468         hlist_for_each_entry_safe(ri, pos, next, &rp->free_instances, hlist) {
469                 hlist_del(&ri->hlist);
470                 kfree(ri);
471         }
472 }
473
474 static void __kprobes cleanup_rp_inst(struct kretprobe *rp)
475 {
476         unsigned long flags, hash;
477         struct kretprobe_instance *ri;
478         struct hlist_node *pos, *next;
479         struct hlist_head *head;
480
481         /* No race here */
482         for (hash = 0; hash < KPROBE_TABLE_SIZE; hash++) {
483                 kretprobe_table_lock(hash, &flags);
484                 head = &kretprobe_inst_table[hash];
485                 hlist_for_each_entry_safe(ri, pos, next, head, hlist) {
486                         if (ri->rp == rp)
487                                 ri->rp = NULL;
488                 }
489                 kretprobe_table_unlock(hash, &flags);
490         }
491         free_rp_inst(rp);
492 }
493
494 /*
495  * Keep all fields in the kprobe consistent
496  */
497 static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p)
498 {
499         memcpy(&p->opcode, &old_p->opcode, sizeof(kprobe_opcode_t));
500         memcpy(&p->ainsn, &old_p->ainsn, sizeof(struct arch_specific_insn));
501 }
502
503 /*
504 * Add the new probe to ap->list. Fail if this is the
505 * second jprobe at the address - two jprobes can't coexist
506 */
507 static int __kprobes add_new_kprobe(struct kprobe *ap, struct kprobe *p)
508 {
509         BUG_ON(kprobe_gone(ap) || kprobe_gone(p));
510         if (p->break_handler) {
511                 if (ap->break_handler)
512                         return -EEXIST;
513                 list_add_tail_rcu(&p->list, &ap->list);
514                 ap->break_handler = aggr_break_handler;
515         } else
516                 list_add_rcu(&p->list, &ap->list);
517         if (p->post_handler && !ap->post_handler)
518                 ap->post_handler = aggr_post_handler;
519
520         if (kprobe_disabled(ap) && !kprobe_disabled(p)) {
521                 ap->flags &= ~KPROBE_FLAG_DISABLED;
522                 if (!kprobes_all_disarmed)
523                         /* Arm the breakpoint again. */
524                         arm_kprobe(ap);
525         }
526         return 0;
527 }
528
529 /*
530  * Fill in the required fields of the "manager kprobe". Replace the
531  * earlier kprobe in the hlist with the manager kprobe
532  */
533 static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
534 {
535         copy_kprobe(p, ap);
536         flush_insn_slot(ap);
537         ap->addr = p->addr;
538         ap->flags = p->flags;
539         ap->pre_handler = aggr_pre_handler;
540         ap->fault_handler = aggr_fault_handler;
541         /* We don't care the kprobe which has gone. */
542         if (p->post_handler && !kprobe_gone(p))
543                 ap->post_handler = aggr_post_handler;
544         if (p->break_handler && !kprobe_gone(p))
545                 ap->break_handler = aggr_break_handler;
546
547         INIT_LIST_HEAD(&ap->list);
548         list_add_rcu(&p->list, &ap->list);
549
550         hlist_replace_rcu(&p->hlist, &ap->hlist);
551 }
552
553 /*
554  * This is the second or subsequent kprobe at the address - handle
555  * the intricacies
556  */
557 static int __kprobes register_aggr_kprobe(struct kprobe *old_p,
558                                           struct kprobe *p)
559 {
560         int ret = 0;
561         struct kprobe *ap = old_p;
562
563         if (old_p->pre_handler != aggr_pre_handler) {
564                 /* If old_p is not an aggr_probe, create new aggr_kprobe. */
565                 ap = kzalloc(sizeof(struct kprobe), GFP_KERNEL);
566                 if (!ap)
567                         return -ENOMEM;
568                 add_aggr_kprobe(ap, old_p);
569         }
570
571         if (kprobe_gone(ap)) {
572                 /*
573                  * Attempting to insert new probe at the same location that
574                  * had a probe in the module vaddr area which already
575                  * freed. So, the instruction slot has already been
576                  * released. We need a new slot for the new probe.
577                  */
578                 ret = arch_prepare_kprobe(ap);
579                 if (ret)
580                         /*
581                          * Even if fail to allocate new slot, don't need to
582                          * free aggr_probe. It will be used next time, or
583                          * freed by unregister_kprobe.
584                          */
585                         return ret;
586
587                 /*
588                  * Clear gone flag to prevent allocating new slot again, and
589                  * set disabled flag because it is not armed yet.
590                  */
591                 ap->flags = (ap->flags & ~KPROBE_FLAG_GONE)
592                             | KPROBE_FLAG_DISABLED;
593         }
594
595         copy_kprobe(ap, p);
596         return add_new_kprobe(ap, p);
597 }
598
599 /* Try to disable aggr_kprobe, and return 1 if succeeded.*/
600 static int __kprobes try_to_disable_aggr_kprobe(struct kprobe *p)
601 {
602         struct kprobe *kp;
603
604         list_for_each_entry_rcu(kp, &p->list, list) {
605                 if (!kprobe_disabled(kp))
606                         /*
607                          * There is an active probe on the list.
608                          * We can't disable aggr_kprobe.
609                          */
610                         return 0;
611         }
612         p->flags |= KPROBE_FLAG_DISABLED;
613         return 1;
614 }
615
616 static int __kprobes in_kprobes_functions(unsigned long addr)
617 {
618         struct kprobe_blackpoint *kb;
619
620         if (addr >= (unsigned long)__kprobes_text_start &&
621             addr < (unsigned long)__kprobes_text_end)
622                 return -EINVAL;
623         /*
624          * If there exists a kprobe_blacklist, verify and
625          * fail any probe registration in the prohibited area
626          */
627         for (kb = kprobe_blacklist; kb->name != NULL; kb++) {
628                 if (kb->start_addr) {
629                         if (addr >= kb->start_addr &&
630                             addr < (kb->start_addr + kb->range))
631                                 return -EINVAL;
632                 }
633         }
634         return 0;
635 }
636
637 /*
638  * If we have a symbol_name argument, look it up and add the offset field
639  * to it. This way, we can specify a relative address to a symbol.
640  */
641 static kprobe_opcode_t __kprobes *kprobe_addr(struct kprobe *p)
642 {
643         kprobe_opcode_t *addr = p->addr;
644         if (p->symbol_name) {
645                 if (addr)
646                         return NULL;
647                 kprobe_lookup_name(p->symbol_name, addr);
648         }
649
650         if (!addr)
651                 return NULL;
652         return (kprobe_opcode_t *)(((char *)addr) + p->offset);
653 }
654
655 /* Check passed kprobe is valid and return kprobe in kprobe_table. */
656 static struct kprobe * __kprobes __get_valid_kprobe(struct kprobe *p)
657 {
658         struct kprobe *old_p, *list_p;
659
660         old_p = get_kprobe(p->addr);
661         if (unlikely(!old_p))
662                 return NULL;
663
664         if (p != old_p) {
665                 list_for_each_entry_rcu(list_p, &old_p->list, list)
666                         if (list_p == p)
667                         /* kprobe p is a valid probe */
668                                 goto valid;
669                 return NULL;
670         }
671 valid:
672         return old_p;
673 }
674
675 /* Return error if the kprobe is being re-registered */
676 static inline int check_kprobe_rereg(struct kprobe *p)
677 {
678         int ret = 0;
679         struct kprobe *old_p;
680
681         mutex_lock(&kprobe_mutex);
682         old_p = __get_valid_kprobe(p);
683         if (old_p)
684                 ret = -EINVAL;
685         mutex_unlock(&kprobe_mutex);
686         return ret;
687 }
688
689 int __kprobes register_kprobe(struct kprobe *p)
690 {
691         int ret = 0;
692         struct kprobe *old_p;
693         struct module *probed_mod;
694         kprobe_opcode_t *addr;
695
696         addr = kprobe_addr(p);
697         if (!addr)
698                 return -EINVAL;
699         p->addr = addr;
700
701         ret = check_kprobe_rereg(p);
702         if (ret)
703                 return ret;
704
705         preempt_disable();
706         if (!kernel_text_address((unsigned long) p->addr) ||
707             in_kprobes_functions((unsigned long) p->addr) ||
708             ftrace_text_reserved(p->addr, p->addr)) {
709                 preempt_enable();
710                 return -EINVAL;
711         }
712
713         /* User can pass only KPROBE_FLAG_DISABLED to register_kprobe */
714         p->flags &= KPROBE_FLAG_DISABLED;
715
716         /*
717          * Check if are we probing a module.
718          */
719         probed_mod = __module_text_address((unsigned long) p->addr);
720         if (probed_mod) {
721                 /*
722                  * We must hold a refcount of the probed module while updating
723                  * its code to prohibit unexpected unloading.
724                  */
725                 if (unlikely(!try_module_get(probed_mod))) {
726                         preempt_enable();
727                         return -EINVAL;
728                 }
729                 /*
730                  * If the module freed .init.text, we couldn't insert
731                  * kprobes in there.
732                  */
733                 if (within_module_init((unsigned long)p->addr, probed_mod) &&
734                     probed_mod->state != MODULE_STATE_COMING) {
735                         module_put(probed_mod);
736                         preempt_enable();
737                         return -EINVAL;
738                 }
739         }
740         preempt_enable();
741
742         p->nmissed = 0;
743         INIT_LIST_HEAD(&p->list);
744         mutex_lock(&kprobe_mutex);
745         old_p = get_kprobe(p->addr);
746         if (old_p) {
747                 ret = register_aggr_kprobe(old_p, p);
748                 goto out;
749         }
750
751         mutex_lock(&text_mutex);
752         ret = arch_prepare_kprobe(p);
753         if (ret)
754                 goto out_unlock_text;
755
756         INIT_HLIST_NODE(&p->hlist);
757         hlist_add_head_rcu(&p->hlist,
758                        &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
759
760         if (!kprobes_all_disarmed && !kprobe_disabled(p))
761                 arch_arm_kprobe(p);
762
763 out_unlock_text:
764         mutex_unlock(&text_mutex);
765 out:
766         mutex_unlock(&kprobe_mutex);
767
768         if (probed_mod)
769                 module_put(probed_mod);
770
771         return ret;
772 }
773 EXPORT_SYMBOL_GPL(register_kprobe);
774
775 /*
776  * Unregister a kprobe without a scheduler synchronization.
777  */
778 static int __kprobes __unregister_kprobe_top(struct kprobe *p)
779 {
780         struct kprobe *old_p, *list_p;
781
782         old_p = __get_valid_kprobe(p);
783         if (old_p == NULL)
784                 return -EINVAL;
785
786         if (old_p == p ||
787             (old_p->pre_handler == aggr_pre_handler &&
788              list_is_singular(&old_p->list))) {
789                 /*
790                  * Only probe on the hash list. Disarm only if kprobes are
791                  * enabled and not gone - otherwise, the breakpoint would
792                  * already have been removed. We save on flushing icache.
793                  */
794                 if (!kprobes_all_disarmed && !kprobe_disabled(old_p))
795                         disarm_kprobe(p);
796                 hlist_del_rcu(&old_p->hlist);
797         } else {
798                 if (p->break_handler && !kprobe_gone(p))
799                         old_p->break_handler = NULL;
800                 if (p->post_handler && !kprobe_gone(p)) {
801                         list_for_each_entry_rcu(list_p, &old_p->list, list) {
802                                 if ((list_p != p) && (list_p->post_handler))
803                                         goto noclean;
804                         }
805                         old_p->post_handler = NULL;
806                 }
807 noclean:
808                 list_del_rcu(&p->list);
809                 if (!kprobe_disabled(old_p)) {
810                         try_to_disable_aggr_kprobe(old_p);
811                         if (!kprobes_all_disarmed && kprobe_disabled(old_p))
812                                 disarm_kprobe(old_p);
813                 }
814         }
815         return 0;
816 }
817
818 static void __kprobes __unregister_kprobe_bottom(struct kprobe *p)
819 {
820         struct kprobe *old_p;
821
822         if (list_empty(&p->list))
823                 arch_remove_kprobe(p);
824         else if (list_is_singular(&p->list)) {
825                 /* "p" is the last child of an aggr_kprobe */
826                 old_p = list_entry(p->list.next, struct kprobe, list);
827                 list_del(&p->list);
828                 arch_remove_kprobe(old_p);
829                 kfree(old_p);
830         }
831 }
832
833 int __kprobes register_kprobes(struct kprobe **kps, int num)
834 {
835         int i, ret = 0;
836
837         if (num <= 0)
838                 return -EINVAL;
839         for (i = 0; i < num; i++) {
840                 ret = register_kprobe(kps[i]);
841                 if (ret < 0) {
842                         if (i > 0)
843                                 unregister_kprobes(kps, i);
844                         break;
845                 }
846         }
847         return ret;
848 }
849 EXPORT_SYMBOL_GPL(register_kprobes);
850
851 void __kprobes unregister_kprobe(struct kprobe *p)
852 {
853         unregister_kprobes(&p, 1);
854 }
855 EXPORT_SYMBOL_GPL(unregister_kprobe);
856
857 void __kprobes unregister_kprobes(struct kprobe **kps, int num)
858 {
859         int i;
860
861         if (num <= 0)
862                 return;
863         mutex_lock(&kprobe_mutex);
864         for (i = 0; i < num; i++)
865                 if (__unregister_kprobe_top(kps[i]) < 0)
866                         kps[i]->addr = NULL;
867         mutex_unlock(&kprobe_mutex);
868
869         synchronize_sched();
870         for (i = 0; i < num; i++)
871                 if (kps[i]->addr)
872                         __unregister_kprobe_bottom(kps[i]);
873 }
874 EXPORT_SYMBOL_GPL(unregister_kprobes);
875
876 static struct notifier_block kprobe_exceptions_nb = {
877         .notifier_call = kprobe_exceptions_notify,
878         .priority = 0x7fffffff /* we need to be notified first */
879 };
880
881 unsigned long __weak arch_deref_entry_point(void *entry)
882 {
883         return (unsigned long)entry;
884 }
885
886 int __kprobes register_jprobes(struct jprobe **jps, int num)
887 {
888         struct jprobe *jp;
889         int ret = 0, i;
890
891         if (num <= 0)
892                 return -EINVAL;
893         for (i = 0; i < num; i++) {
894                 unsigned long addr;
895                 jp = jps[i];
896                 addr = arch_deref_entry_point(jp->entry);
897
898                 if (!kernel_text_address(addr))
899                         ret = -EINVAL;
900                 else {
901                         /* Todo: Verify probepoint is a function entry point */
902                         jp->kp.pre_handler = setjmp_pre_handler;
903                         jp->kp.break_handler = longjmp_break_handler;
904                         ret = register_kprobe(&jp->kp);
905                 }
906                 if (ret < 0) {
907                         if (i > 0)
908                                 unregister_jprobes(jps, i);
909                         break;
910                 }
911         }
912         return ret;
913 }
914 EXPORT_SYMBOL_GPL(register_jprobes);
915
916 int __kprobes register_jprobe(struct jprobe *jp)
917 {
918         return register_jprobes(&jp, 1);
919 }
920 EXPORT_SYMBOL_GPL(register_jprobe);
921
922 void __kprobes unregister_jprobe(struct jprobe *jp)
923 {
924         unregister_jprobes(&jp, 1);
925 }
926 EXPORT_SYMBOL_GPL(unregister_jprobe);
927
928 void __kprobes unregister_jprobes(struct jprobe **jps, int num)
929 {
930         int i;
931
932         if (num <= 0)
933                 return;
934         mutex_lock(&kprobe_mutex);
935         for (i = 0; i < num; i++)
936                 if (__unregister_kprobe_top(&jps[i]->kp) < 0)
937                         jps[i]->kp.addr = NULL;
938         mutex_unlock(&kprobe_mutex);
939
940         synchronize_sched();
941         for (i = 0; i < num; i++) {
942                 if (jps[i]->kp.addr)
943                         __unregister_kprobe_bottom(&jps[i]->kp);
944         }
945 }
946 EXPORT_SYMBOL_GPL(unregister_jprobes);
947
948 #ifdef CONFIG_KRETPROBES
949 /*
950  * This kprobe pre_handler is registered with every kretprobe. When probe
951  * hits it will set up the return probe.
952  */
953 static int __kprobes pre_handler_kretprobe(struct kprobe *p,
954                                            struct pt_regs *regs)
955 {
956         struct kretprobe *rp = container_of(p, struct kretprobe, kp);
957         unsigned long hash, flags = 0;
958         struct kretprobe_instance *ri;
959
960         /*TODO: consider to only swap the RA after the last pre_handler fired */
961         hash = hash_ptr(current, KPROBE_HASH_BITS);
962         spin_lock_irqsave(&rp->lock, flags);
963         if (!hlist_empty(&rp->free_instances)) {
964                 ri = hlist_entry(rp->free_instances.first,
965                                 struct kretprobe_instance, hlist);
966                 hlist_del(&ri->hlist);
967                 spin_unlock_irqrestore(&rp->lock, flags);
968
969                 ri->rp = rp;
970                 ri->task = current;
971
972                 if (rp->entry_handler && rp->entry_handler(ri, regs))
973                         return 0;
974
975                 arch_prepare_kretprobe(ri, regs);
976
977                 /* XXX(hch): why is there no hlist_move_head? */
978                 INIT_HLIST_NODE(&ri->hlist);
979                 kretprobe_table_lock(hash, &flags);
980                 hlist_add_head(&ri->hlist, &kretprobe_inst_table[hash]);
981                 kretprobe_table_unlock(hash, &flags);
982         } else {
983                 rp->nmissed++;
984                 spin_unlock_irqrestore(&rp->lock, flags);
985         }
986         return 0;
987 }
988
989 int __kprobes register_kretprobe(struct kretprobe *rp)
990 {
991         int ret = 0;
992         struct kretprobe_instance *inst;
993         int i;
994         void *addr;
995
996         if (kretprobe_blacklist_size) {
997                 addr = kprobe_addr(&rp->kp);
998                 if (!addr)
999                         return -EINVAL;
1000
1001                 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
1002                         if (kretprobe_blacklist[i].addr == addr)
1003                                 return -EINVAL;
1004                 }
1005         }
1006
1007         rp->kp.pre_handler = pre_handler_kretprobe;
1008         rp->kp.post_handler = NULL;
1009         rp->kp.fault_handler = NULL;
1010         rp->kp.break_handler = NULL;
1011
1012         /* Pre-allocate memory for max kretprobe instances */
1013         if (rp->maxactive <= 0) {
1014 #ifdef CONFIG_PREEMPT
1015                 rp->maxactive = max_t(unsigned int, 10, 2*num_possible_cpus());
1016 #else
1017                 rp->maxactive = num_possible_cpus();
1018 #endif
1019         }
1020         spin_lock_init(&rp->lock);
1021         INIT_HLIST_HEAD(&rp->free_instances);
1022         for (i = 0; i < rp->maxactive; i++) {
1023                 inst = kmalloc(sizeof(struct kretprobe_instance) +
1024                                rp->data_size, GFP_KERNEL);
1025                 if (inst == NULL) {
1026                         free_rp_inst(rp);
1027                         return -ENOMEM;
1028                 }
1029                 INIT_HLIST_NODE(&inst->hlist);
1030                 hlist_add_head(&inst->hlist, &rp->free_instances);
1031         }
1032
1033         rp->nmissed = 0;
1034         /* Establish function entry probe point */
1035         ret = register_kprobe(&rp->kp);
1036         if (ret != 0)
1037                 free_rp_inst(rp);
1038         return ret;
1039 }
1040 EXPORT_SYMBOL_GPL(register_kretprobe);
1041
1042 int __kprobes register_kretprobes(struct kretprobe **rps, int num)
1043 {
1044         int ret = 0, i;
1045
1046         if (num <= 0)
1047                 return -EINVAL;
1048         for (i = 0; i < num; i++) {
1049                 ret = register_kretprobe(rps[i]);
1050                 if (ret < 0) {
1051                         if (i > 0)
1052                                 unregister_kretprobes(rps, i);
1053                         break;
1054                 }
1055         }
1056         return ret;
1057 }
1058 EXPORT_SYMBOL_GPL(register_kretprobes);
1059
1060 void __kprobes unregister_kretprobe(struct kretprobe *rp)
1061 {
1062         unregister_kretprobes(&rp, 1);
1063 }
1064 EXPORT_SYMBOL_GPL(unregister_kretprobe);
1065
1066 void __kprobes unregister_kretprobes(struct kretprobe **rps, int num)
1067 {
1068         int i;
1069
1070         if (num <= 0)
1071                 return;
1072         mutex_lock(&kprobe_mutex);
1073         for (i = 0; i < num; i++)
1074                 if (__unregister_kprobe_top(&rps[i]->kp) < 0)
1075                         rps[i]->kp.addr = NULL;
1076         mutex_unlock(&kprobe_mutex);
1077
1078         synchronize_sched();
1079         for (i = 0; i < num; i++) {
1080                 if (rps[i]->kp.addr) {
1081                         __unregister_kprobe_bottom(&rps[i]->kp);
1082                         cleanup_rp_inst(rps[i]);
1083                 }
1084         }
1085 }
1086 EXPORT_SYMBOL_GPL(unregister_kretprobes);
1087
1088 #else /* CONFIG_KRETPROBES */
1089 int __kprobes register_kretprobe(struct kretprobe *rp)
1090 {
1091         return -ENOSYS;
1092 }
1093 EXPORT_SYMBOL_GPL(register_kretprobe);
1094
1095 int __kprobes register_kretprobes(struct kretprobe **rps, int num)
1096 {
1097         return -ENOSYS;
1098 }
1099 EXPORT_SYMBOL_GPL(register_kretprobes);
1100
1101 void __kprobes unregister_kretprobe(struct kretprobe *rp)
1102 {
1103 }
1104 EXPORT_SYMBOL_GPL(unregister_kretprobe);
1105
1106 void __kprobes unregister_kretprobes(struct kretprobe **rps, int num)
1107 {
1108 }
1109 EXPORT_SYMBOL_GPL(unregister_kretprobes);
1110
1111 static int __kprobes pre_handler_kretprobe(struct kprobe *p,
1112                                            struct pt_regs *regs)
1113 {
1114         return 0;
1115 }
1116
1117 #endif /* CONFIG_KRETPROBES */
1118
1119 /* Set the kprobe gone and remove its instruction buffer. */
1120 static void __kprobes kill_kprobe(struct kprobe *p)
1121 {
1122         struct kprobe *kp;
1123
1124         p->flags |= KPROBE_FLAG_GONE;
1125         if (p->pre_handler == aggr_pre_handler) {
1126                 /*
1127                  * If this is an aggr_kprobe, we have to list all the
1128                  * chained probes and mark them GONE.
1129                  */
1130                 list_for_each_entry_rcu(kp, &p->list, list)
1131                         kp->flags |= KPROBE_FLAG_GONE;
1132                 p->post_handler = NULL;
1133                 p->break_handler = NULL;
1134         }
1135         /*
1136          * Here, we can remove insn_slot safely, because no thread calls
1137          * the original probed function (which will be freed soon) any more.
1138          */
1139         arch_remove_kprobe(p);
1140 }
1141
1142 void __kprobes dump_kprobe(struct kprobe *kp)
1143 {
1144         printk(KERN_WARNING "Dumping kprobe:\n");
1145         printk(KERN_WARNING "Name: %s\nAddress: %p\nOffset: %x\n",
1146                kp->symbol_name, kp->addr, kp->offset);
1147 }
1148
1149 /* Module notifier call back, checking kprobes on the module */
1150 static int __kprobes kprobes_module_callback(struct notifier_block *nb,
1151                                              unsigned long val, void *data)
1152 {
1153         struct module *mod = data;
1154         struct hlist_head *head;
1155         struct hlist_node *node;
1156         struct kprobe *p;
1157         unsigned int i;
1158         int checkcore = (val == MODULE_STATE_GOING);
1159
1160         if (val != MODULE_STATE_GOING && val != MODULE_STATE_LIVE)
1161                 return NOTIFY_DONE;
1162
1163         /*
1164          * When MODULE_STATE_GOING was notified, both of module .text and
1165          * .init.text sections would be freed. When MODULE_STATE_LIVE was
1166          * notified, only .init.text section would be freed. We need to
1167          * disable kprobes which have been inserted in the sections.
1168          */
1169         mutex_lock(&kprobe_mutex);
1170         for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
1171                 head = &kprobe_table[i];
1172                 hlist_for_each_entry_rcu(p, node, head, hlist)
1173                         if (within_module_init((unsigned long)p->addr, mod) ||
1174                             (checkcore &&
1175                              within_module_core((unsigned long)p->addr, mod))) {
1176                                 /*
1177                                  * The vaddr this probe is installed will soon
1178                                  * be vfreed buy not synced to disk. Hence,
1179                                  * disarming the breakpoint isn't needed.
1180                                  */
1181                                 kill_kprobe(p);
1182                         }
1183         }
1184         mutex_unlock(&kprobe_mutex);
1185         return NOTIFY_DONE;
1186 }
1187
1188 static struct notifier_block kprobe_module_nb = {
1189         .notifier_call = kprobes_module_callback,
1190         .priority = 0
1191 };
1192
1193 static int __init init_kprobes(void)
1194 {
1195         int i, err = 0;
1196         unsigned long offset = 0, size = 0;
1197         char *modname, namebuf[128];
1198         const char *symbol_name;
1199         void *addr;
1200         struct kprobe_blackpoint *kb;
1201
1202         /* FIXME allocate the probe table, currently defined statically */
1203         /* initialize all list heads */
1204         for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
1205                 INIT_HLIST_HEAD(&kprobe_table[i]);
1206                 INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
1207                 spin_lock_init(&(kretprobe_table_locks[i].lock));
1208         }
1209
1210         /*
1211          * Lookup and populate the kprobe_blacklist.
1212          *
1213          * Unlike the kretprobe blacklist, we'll need to determine
1214          * the range of addresses that belong to the said functions,
1215          * since a kprobe need not necessarily be at the beginning
1216          * of a function.
1217          */
1218         for (kb = kprobe_blacklist; kb->name != NULL; kb++) {
1219                 kprobe_lookup_name(kb->name, addr);
1220                 if (!addr)
1221                         continue;
1222
1223                 kb->start_addr = (unsigned long)addr;
1224                 symbol_name = kallsyms_lookup(kb->start_addr,
1225                                 &size, &offset, &modname, namebuf);
1226                 if (!symbol_name)
1227                         kb->range = 0;
1228                 else
1229                         kb->range = size;
1230         }
1231
1232         if (kretprobe_blacklist_size) {
1233                 /* lookup the function address from its name */
1234                 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
1235                         kprobe_lookup_name(kretprobe_blacklist[i].name,
1236                                            kretprobe_blacklist[i].addr);
1237                         if (!kretprobe_blacklist[i].addr)
1238                                 printk("kretprobe: lookup failed: %s\n",
1239                                        kretprobe_blacklist[i].name);
1240                 }
1241         }
1242
1243         /* By default, kprobes are armed */
1244         kprobes_all_disarmed = false;
1245
1246         err = arch_init_kprobes();
1247         if (!err)
1248                 err = register_die_notifier(&kprobe_exceptions_nb);
1249         if (!err)
1250                 err = register_module_notifier(&kprobe_module_nb);
1251
1252         kprobes_initialized = (err == 0);
1253
1254         if (!err)
1255                 init_test_probes();
1256         return err;
1257 }
1258
1259 #ifdef CONFIG_DEBUG_FS
1260 static void __kprobes report_probe(struct seq_file *pi, struct kprobe *p,
1261                 const char *sym, int offset,char *modname)
1262 {
1263         char *kprobe_type;
1264
1265         if (p->pre_handler == pre_handler_kretprobe)
1266                 kprobe_type = "r";
1267         else if (p->pre_handler == setjmp_pre_handler)
1268                 kprobe_type = "j";
1269         else
1270                 kprobe_type = "k";
1271         if (sym)
1272                 seq_printf(pi, "%p  %s  %s+0x%x  %s %s%s\n",
1273                         p->addr, kprobe_type, sym, offset,
1274                         (modname ? modname : " "),
1275                         (kprobe_gone(p) ? "[GONE]" : ""),
1276                         ((kprobe_disabled(p) && !kprobe_gone(p)) ?
1277                          "[DISABLED]" : ""));
1278         else
1279                 seq_printf(pi, "%p  %s  %p %s%s\n",
1280                         p->addr, kprobe_type, p->addr,
1281                         (kprobe_gone(p) ? "[GONE]" : ""),
1282                         ((kprobe_disabled(p) && !kprobe_gone(p)) ?
1283                          "[DISABLED]" : ""));
1284 }
1285
1286 static void __kprobes *kprobe_seq_start(struct seq_file *f, loff_t *pos)
1287 {
1288         return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL;
1289 }
1290
1291 static void __kprobes *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos)
1292 {
1293         (*pos)++;
1294         if (*pos >= KPROBE_TABLE_SIZE)
1295                 return NULL;
1296         return pos;
1297 }
1298
1299 static void __kprobes kprobe_seq_stop(struct seq_file *f, void *v)
1300 {
1301         /* Nothing to do */
1302 }
1303
1304 static int __kprobes show_kprobe_addr(struct seq_file *pi, void *v)
1305 {
1306         struct hlist_head *head;
1307         struct hlist_node *node;
1308         struct kprobe *p, *kp;
1309         const char *sym = NULL;
1310         unsigned int i = *(loff_t *) v;
1311         unsigned long offset = 0;
1312         char *modname, namebuf[128];
1313
1314         head = &kprobe_table[i];
1315         preempt_disable();
1316         hlist_for_each_entry_rcu(p, node, head, hlist) {
1317                 sym = kallsyms_lookup((unsigned long)p->addr, NULL,
1318                                         &offset, &modname, namebuf);
1319                 if (p->pre_handler == aggr_pre_handler) {
1320                         list_for_each_entry_rcu(kp, &p->list, list)
1321                                 report_probe(pi, kp, sym, offset, modname);
1322                 } else
1323                         report_probe(pi, p, sym, offset, modname);
1324         }
1325         preempt_enable();
1326         return 0;
1327 }
1328
1329 static const struct seq_operations kprobes_seq_ops = {
1330         .start = kprobe_seq_start,
1331         .next  = kprobe_seq_next,
1332         .stop  = kprobe_seq_stop,
1333         .show  = show_kprobe_addr
1334 };
1335
1336 static int __kprobes kprobes_open(struct inode *inode, struct file *filp)
1337 {
1338         return seq_open(filp, &kprobes_seq_ops);
1339 }
1340
1341 static const struct file_operations debugfs_kprobes_operations = {
1342         .open           = kprobes_open,
1343         .read           = seq_read,
1344         .llseek         = seq_lseek,
1345         .release        = seq_release,
1346 };
1347
1348 /* Disable one kprobe */
1349 int __kprobes disable_kprobe(struct kprobe *kp)
1350 {
1351         int ret = 0;
1352         struct kprobe *p;
1353
1354         mutex_lock(&kprobe_mutex);
1355
1356         /* Check whether specified probe is valid. */
1357         p = __get_valid_kprobe(kp);
1358         if (unlikely(p == NULL)) {
1359                 ret = -EINVAL;
1360                 goto out;
1361         }
1362
1363         /* If the probe is already disabled (or gone), just return */
1364         if (kprobe_disabled(kp))
1365                 goto out;
1366
1367         kp->flags |= KPROBE_FLAG_DISABLED;
1368         if (p != kp)
1369                 /* When kp != p, p is always enabled. */
1370                 try_to_disable_aggr_kprobe(p);
1371
1372         if (!kprobes_all_disarmed && kprobe_disabled(p))
1373                 disarm_kprobe(p);
1374 out:
1375         mutex_unlock(&kprobe_mutex);
1376         return ret;
1377 }
1378 EXPORT_SYMBOL_GPL(disable_kprobe);
1379
1380 /* Enable one kprobe */
1381 int __kprobes enable_kprobe(struct kprobe *kp)
1382 {
1383         int ret = 0;
1384         struct kprobe *p;
1385
1386         mutex_lock(&kprobe_mutex);
1387
1388         /* Check whether specified probe is valid. */
1389         p = __get_valid_kprobe(kp);
1390         if (unlikely(p == NULL)) {
1391                 ret = -EINVAL;
1392                 goto out;
1393         }
1394
1395         if (kprobe_gone(kp)) {
1396                 /* This kprobe has gone, we couldn't enable it. */
1397                 ret = -EINVAL;
1398                 goto out;
1399         }
1400
1401         if (!kprobes_all_disarmed && kprobe_disabled(p))
1402                 arm_kprobe(p);
1403
1404         p->flags &= ~KPROBE_FLAG_DISABLED;
1405         if (p != kp)
1406                 kp->flags &= ~KPROBE_FLAG_DISABLED;
1407 out:
1408         mutex_unlock(&kprobe_mutex);
1409         return ret;
1410 }
1411 EXPORT_SYMBOL_GPL(enable_kprobe);
1412
1413 static void __kprobes arm_all_kprobes(void)
1414 {
1415         struct hlist_head *head;
1416         struct hlist_node *node;
1417         struct kprobe *p;
1418         unsigned int i;
1419
1420         mutex_lock(&kprobe_mutex);
1421
1422         /* If kprobes are armed, just return */
1423         if (!kprobes_all_disarmed)
1424                 goto already_enabled;
1425
1426         mutex_lock(&text_mutex);
1427         for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
1428                 head = &kprobe_table[i];
1429                 hlist_for_each_entry_rcu(p, node, head, hlist)
1430                         if (!kprobe_disabled(p))
1431                                 arch_arm_kprobe(p);
1432         }
1433         mutex_unlock(&text_mutex);
1434
1435         kprobes_all_disarmed = false;
1436         printk(KERN_INFO "Kprobes globally enabled\n");
1437
1438 already_enabled:
1439         mutex_unlock(&kprobe_mutex);
1440         return;
1441 }
1442
1443 static void __kprobes disarm_all_kprobes(void)
1444 {
1445         struct hlist_head *head;
1446         struct hlist_node *node;
1447         struct kprobe *p;
1448         unsigned int i;
1449
1450         mutex_lock(&kprobe_mutex);
1451
1452         /* If kprobes are already disarmed, just return */
1453         if (kprobes_all_disarmed)
1454                 goto already_disabled;
1455
1456         kprobes_all_disarmed = true;
1457         printk(KERN_INFO "Kprobes globally disabled\n");
1458         mutex_lock(&text_mutex);
1459         for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
1460                 head = &kprobe_table[i];
1461                 hlist_for_each_entry_rcu(p, node, head, hlist) {
1462                         if (!arch_trampoline_kprobe(p) && !kprobe_disabled(p))
1463                                 arch_disarm_kprobe(p);
1464                 }
1465         }
1466
1467         mutex_unlock(&text_mutex);
1468         mutex_unlock(&kprobe_mutex);
1469         /* Allow all currently running kprobes to complete */
1470         synchronize_sched();
1471         return;
1472
1473 already_disabled:
1474         mutex_unlock(&kprobe_mutex);
1475         return;
1476 }
1477
1478 /*
1479  * XXX: The debugfs bool file interface doesn't allow for callbacks
1480  * when the bool state is switched. We can reuse that facility when
1481  * available
1482  */
1483 static ssize_t read_enabled_file_bool(struct file *file,
1484                char __user *user_buf, size_t count, loff_t *ppos)
1485 {
1486         char buf[3];
1487
1488         if (!kprobes_all_disarmed)
1489                 buf[0] = '1';
1490         else
1491                 buf[0] = '0';
1492         buf[1] = '\n';
1493         buf[2] = 0x00;
1494         return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
1495 }
1496
1497 static ssize_t write_enabled_file_bool(struct file *file,
1498                const char __user *user_buf, size_t count, loff_t *ppos)
1499 {
1500         char buf[32];
1501         int buf_size;
1502
1503         buf_size = min(count, (sizeof(buf)-1));
1504         if (copy_from_user(buf, user_buf, buf_size))
1505                 return -EFAULT;
1506
1507         switch (buf[0]) {
1508         case 'y':
1509         case 'Y':
1510         case '1':
1511                 arm_all_kprobes();
1512                 break;
1513         case 'n':
1514         case 'N':
1515         case '0':
1516                 disarm_all_kprobes();
1517                 break;
1518         }
1519
1520         return count;
1521 }
1522
1523 static const struct file_operations fops_kp = {
1524         .read =         read_enabled_file_bool,
1525         .write =        write_enabled_file_bool,
1526 };
1527
1528 static int __kprobes debugfs_kprobe_init(void)
1529 {
1530         struct dentry *dir, *file;
1531         unsigned int value = 1;
1532
1533         dir = debugfs_create_dir("kprobes", NULL);
1534         if (!dir)
1535                 return -ENOMEM;
1536
1537         file = debugfs_create_file("list", 0444, dir, NULL,
1538                                 &debugfs_kprobes_operations);
1539         if (!file) {
1540                 debugfs_remove(dir);
1541                 return -ENOMEM;
1542         }
1543
1544         file = debugfs_create_file("enabled", 0600, dir,
1545                                         &value, &fops_kp);
1546         if (!file) {
1547                 debugfs_remove(dir);
1548                 return -ENOMEM;
1549         }
1550
1551         return 0;
1552 }
1553
1554 late_initcall(debugfs_kprobe_init);
1555 #endif /* CONFIG_DEBUG_FS */
1556
1557 module_init(init_kprobes);
1558
1559 /* defined in arch/.../kernel/kprobes.c */
1560 EXPORT_SYMBOL_GPL(jprobe_return);