1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Kernel Probes (KProbes)
6 * Copyright (C) IBM Corporation, 2002, 2004
8 * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
9 * Probes initial implementation (includes suggestions from
11 * 2004-Aug Updated by Prasanna S Panchamukhi <prasanna@in.ibm.com> with
12 * hlists and exceptions notifier as suggested by Andi Kleen.
13 * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
14 * interface to access function arguments.
15 * 2004-Sep Prasanna S Panchamukhi <prasanna@in.ibm.com> Changed Kprobes
16 * exceptions notifier to be first on the priority list.
17 * 2005-May Hien Nguyen <hien@us.ibm.com>, Jim Keniston
18 * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
19 * <prasanna@in.ibm.com> added function-return probes.
21 #include <linux/kprobes.h>
22 #include <linux/hash.h>
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/stddef.h>
26 #include <linux/export.h>
27 #include <linux/moduleloader.h>
28 #include <linux/kallsyms.h>
29 #include <linux/freezer.h>
30 #include <linux/seq_file.h>
31 #include <linux/debugfs.h>
32 #include <linux/sysctl.h>
33 #include <linux/kdebug.h>
34 #include <linux/memory.h>
35 #include <linux/ftrace.h>
36 #include <linux/cpu.h>
37 #include <linux/jump_label.h>
38 #include <linux/perf_event.h>
40 #include <asm/sections.h>
41 #include <asm/cacheflush.h>
42 #include <asm/errno.h>
43 #include <linux/uaccess.h>
45 #define KPROBE_HASH_BITS 6
46 #define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
49 static int kprobes_initialized;
50 /* kprobe_table can be accessed by
51 * - Normal hlist traversal and RCU add/del under kprobe_mutex is held.
53 * - RCU hlist traversal under disabling preempt (breakpoint handlers)
55 static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
57 /* NOTE: change this value only with kprobe_mutex held */
58 static bool kprobes_all_disarmed;
60 /* This protects kprobe_table and optimizing_list */
61 static DEFINE_MUTEX(kprobe_mutex);
62 static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL;
64 kprobe_opcode_t * __weak kprobe_lookup_name(const char *name,
65 unsigned int __unused)
67 return ((kprobe_opcode_t *)(kallsyms_lookup_name(name)));
70 /* Blacklist -- list of struct kprobe_blacklist_entry */
71 static LIST_HEAD(kprobe_blacklist);
73 #ifdef __ARCH_WANT_KPROBES_INSN_SLOT
75 * kprobe->ainsn.insn points to the copy of the instruction to be
76 * single-stepped. x86_64, POWER4 and above have no-exec support and
77 * stepping on the instruction on a vmalloced/kmalloced/data page
78 * is a recipe for disaster
80 struct kprobe_insn_page {
81 struct list_head list;
82 kprobe_opcode_t *insns; /* Page of instruction slots */
83 struct kprobe_insn_cache *cache;
89 #define KPROBE_INSN_PAGE_SIZE(slots) \
90 (offsetof(struct kprobe_insn_page, slot_used) + \
91 (sizeof(char) * (slots)))
93 static int slots_per_page(struct kprobe_insn_cache *c)
95 return PAGE_SIZE/(c->insn_size * sizeof(kprobe_opcode_t));
98 enum kprobe_slot_state {
104 void __weak *alloc_insn_page(void)
106 return module_alloc(PAGE_SIZE);
109 void __weak free_insn_page(void *page)
111 module_memfree(page);
114 struct kprobe_insn_cache kprobe_insn_slots = {
115 .mutex = __MUTEX_INITIALIZER(kprobe_insn_slots.mutex),
116 .alloc = alloc_insn_page,
117 .free = free_insn_page,
118 .sym = KPROBE_INSN_PAGE_SYM,
119 .pages = LIST_HEAD_INIT(kprobe_insn_slots.pages),
120 .insn_size = MAX_INSN_SIZE,
123 static int collect_garbage_slots(struct kprobe_insn_cache *c);
126 * __get_insn_slot() - Find a slot on an executable page for an instruction.
127 * We allocate an executable page if there's no room on existing ones.
129 kprobe_opcode_t *__get_insn_slot(struct kprobe_insn_cache *c)
131 struct kprobe_insn_page *kip;
132 kprobe_opcode_t *slot = NULL;
134 /* Since the slot array is not protected by rcu, we need a mutex */
135 mutex_lock(&c->mutex);
138 list_for_each_entry_rcu(kip, &c->pages, list) {
139 if (kip->nused < slots_per_page(c)) {
141 for (i = 0; i < slots_per_page(c); i++) {
142 if (kip->slot_used[i] == SLOT_CLEAN) {
143 kip->slot_used[i] = SLOT_USED;
145 slot = kip->insns + (i * c->insn_size);
150 /* kip->nused is broken. Fix it. */
151 kip->nused = slots_per_page(c);
157 /* If there are any garbage slots, collect it and try again. */
158 if (c->nr_garbage && collect_garbage_slots(c) == 0)
161 /* All out of space. Need to allocate a new page. */
162 kip = kmalloc(KPROBE_INSN_PAGE_SIZE(slots_per_page(c)), GFP_KERNEL);
167 * Use module_alloc so this page is within +/- 2GB of where the
168 * kernel image and loaded module images reside. This is required
169 * so x86_64 can correctly handle the %rip-relative fixups.
171 kip->insns = c->alloc();
176 INIT_LIST_HEAD(&kip->list);
177 memset(kip->slot_used, SLOT_CLEAN, slots_per_page(c));
178 kip->slot_used[0] = SLOT_USED;
182 list_add_rcu(&kip->list, &c->pages);
185 /* Record the perf ksymbol register event after adding the page */
186 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL, (unsigned long)kip->insns,
187 PAGE_SIZE, false, c->sym);
189 mutex_unlock(&c->mutex);
193 /* Return 1 if all garbages are collected, otherwise 0. */
194 static int collect_one_slot(struct kprobe_insn_page *kip, int idx)
196 kip->slot_used[idx] = SLOT_CLEAN;
198 if (kip->nused == 0) {
200 * Page is no longer in use. Free it unless
201 * it's the last one. We keep the last one
202 * so as not to have to set it up again the
203 * next time somebody inserts a probe.
205 if (!list_is_singular(&kip->list)) {
207 * Record perf ksymbol unregister event before removing
210 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL,
211 (unsigned long)kip->insns, PAGE_SIZE, true,
213 list_del_rcu(&kip->list);
215 kip->cache->free(kip->insns);
223 static int collect_garbage_slots(struct kprobe_insn_cache *c)
225 struct kprobe_insn_page *kip, *next;
227 /* Ensure no-one is interrupted on the garbages */
230 list_for_each_entry_safe(kip, next, &c->pages, list) {
232 if (kip->ngarbage == 0)
234 kip->ngarbage = 0; /* we will collect all garbages */
235 for (i = 0; i < slots_per_page(c); i++) {
236 if (kip->slot_used[i] == SLOT_DIRTY && collect_one_slot(kip, i))
244 void __free_insn_slot(struct kprobe_insn_cache *c,
245 kprobe_opcode_t *slot, int dirty)
247 struct kprobe_insn_page *kip;
250 mutex_lock(&c->mutex);
252 list_for_each_entry_rcu(kip, &c->pages, list) {
253 idx = ((long)slot - (long)kip->insns) /
254 (c->insn_size * sizeof(kprobe_opcode_t));
255 if (idx >= 0 && idx < slots_per_page(c))
258 /* Could not find this slot. */
263 /* Mark and sweep: this may sleep */
265 /* Check double free */
266 WARN_ON(kip->slot_used[idx] != SLOT_USED);
268 kip->slot_used[idx] = SLOT_DIRTY;
270 if (++c->nr_garbage > slots_per_page(c))
271 collect_garbage_slots(c);
273 collect_one_slot(kip, idx);
276 mutex_unlock(&c->mutex);
280 * Check given address is on the page of kprobe instruction slots.
281 * This will be used for checking whether the address on a stack
282 * is on a text area or not.
284 bool __is_insn_slot_addr(struct kprobe_insn_cache *c, unsigned long addr)
286 struct kprobe_insn_page *kip;
290 list_for_each_entry_rcu(kip, &c->pages, list) {
291 if (addr >= (unsigned long)kip->insns &&
292 addr < (unsigned long)kip->insns + PAGE_SIZE) {
302 int kprobe_cache_get_kallsym(struct kprobe_insn_cache *c, unsigned int *symnum,
303 unsigned long *value, char *type, char *sym)
305 struct kprobe_insn_page *kip;
309 list_for_each_entry_rcu(kip, &c->pages, list) {
312 strlcpy(sym, c->sym, KSYM_NAME_LEN);
314 *value = (unsigned long)kip->insns;
323 #ifdef CONFIG_OPTPROBES
324 /* For optimized_kprobe buffer */
325 struct kprobe_insn_cache kprobe_optinsn_slots = {
326 .mutex = __MUTEX_INITIALIZER(kprobe_optinsn_slots.mutex),
327 .alloc = alloc_insn_page,
328 .free = free_insn_page,
329 .sym = KPROBE_OPTINSN_PAGE_SYM,
330 .pages = LIST_HEAD_INIT(kprobe_optinsn_slots.pages),
331 /* .insn_size is initialized later */
337 /* We have preemption disabled.. so it is safe to use __ versions */
338 static inline void set_kprobe_instance(struct kprobe *kp)
340 __this_cpu_write(kprobe_instance, kp);
343 static inline void reset_kprobe_instance(void)
345 __this_cpu_write(kprobe_instance, NULL);
349 * This routine is called either:
350 * - under the kprobe_mutex - during kprobe_[un]register()
352 * - with preemption disabled - from arch/xxx/kernel/kprobes.c
354 struct kprobe *get_kprobe(void *addr)
356 struct hlist_head *head;
359 head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
360 hlist_for_each_entry_rcu(p, head, hlist,
361 lockdep_is_held(&kprobe_mutex)) {
368 NOKPROBE_SYMBOL(get_kprobe);
370 static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs);
372 /* Return true if the kprobe is an aggregator */
373 static inline int kprobe_aggrprobe(struct kprobe *p)
375 return p->pre_handler == aggr_pre_handler;
378 /* Return true(!0) if the kprobe is unused */
379 static inline int kprobe_unused(struct kprobe *p)
381 return kprobe_aggrprobe(p) && kprobe_disabled(p) &&
382 list_empty(&p->list);
386 * Keep all fields in the kprobe consistent
388 static inline void copy_kprobe(struct kprobe *ap, struct kprobe *p)
390 memcpy(&p->opcode, &ap->opcode, sizeof(kprobe_opcode_t));
391 memcpy(&p->ainsn, &ap->ainsn, sizeof(struct arch_specific_insn));
394 #ifdef CONFIG_OPTPROBES
395 /* NOTE: change this value only with kprobe_mutex held */
396 static bool kprobes_allow_optimization;
399 * Call all pre_handler on the list, but ignores its return value.
400 * This must be called from arch-dep optimized caller.
402 void opt_pre_handler(struct kprobe *p, struct pt_regs *regs)
406 list_for_each_entry_rcu(kp, &p->list, list) {
407 if (kp->pre_handler && likely(!kprobe_disabled(kp))) {
408 set_kprobe_instance(kp);
409 kp->pre_handler(kp, regs);
411 reset_kprobe_instance();
414 NOKPROBE_SYMBOL(opt_pre_handler);
416 /* Free optimized instructions and optimized_kprobe */
417 static void free_aggr_kprobe(struct kprobe *p)
419 struct optimized_kprobe *op;
421 op = container_of(p, struct optimized_kprobe, kp);
422 arch_remove_optimized_kprobe(op);
423 arch_remove_kprobe(p);
427 /* Return true(!0) if the kprobe is ready for optimization. */
428 static inline int kprobe_optready(struct kprobe *p)
430 struct optimized_kprobe *op;
432 if (kprobe_aggrprobe(p)) {
433 op = container_of(p, struct optimized_kprobe, kp);
434 return arch_prepared_optinsn(&op->optinsn);
440 /* Return true(!0) if the kprobe is disarmed. Note: p must be on hash list */
441 static inline int kprobe_disarmed(struct kprobe *p)
443 struct optimized_kprobe *op;
445 /* If kprobe is not aggr/opt probe, just return kprobe is disabled */
446 if (!kprobe_aggrprobe(p))
447 return kprobe_disabled(p);
449 op = container_of(p, struct optimized_kprobe, kp);
451 return kprobe_disabled(p) && list_empty(&op->list);
454 /* Return true(!0) if the probe is queued on (un)optimizing lists */
455 static int kprobe_queued(struct kprobe *p)
457 struct optimized_kprobe *op;
459 if (kprobe_aggrprobe(p)) {
460 op = container_of(p, struct optimized_kprobe, kp);
461 if (!list_empty(&op->list))
468 * Return an optimized kprobe whose optimizing code replaces
469 * instructions including addr (exclude breakpoint).
471 static struct kprobe *get_optimized_kprobe(unsigned long addr)
474 struct kprobe *p = NULL;
475 struct optimized_kprobe *op;
477 /* Don't check i == 0, since that is a breakpoint case. */
478 for (i = 1; !p && i < MAX_OPTIMIZED_LENGTH; i++)
479 p = get_kprobe((void *)(addr - i));
481 if (p && kprobe_optready(p)) {
482 op = container_of(p, struct optimized_kprobe, kp);
483 if (arch_within_optimized_kprobe(op, addr))
490 /* Optimization staging list, protected by kprobe_mutex */
491 static LIST_HEAD(optimizing_list);
492 static LIST_HEAD(unoptimizing_list);
493 static LIST_HEAD(freeing_list);
495 static void kprobe_optimizer(struct work_struct *work);
496 static DECLARE_DELAYED_WORK(optimizing_work, kprobe_optimizer);
497 #define OPTIMIZE_DELAY 5
500 * Optimize (replace a breakpoint with a jump) kprobes listed on
503 static void do_optimize_kprobes(void)
505 lockdep_assert_held(&text_mutex);
507 * The optimization/unoptimization refers online_cpus via
508 * stop_machine() and cpu-hotplug modifies online_cpus.
509 * And same time, text_mutex will be held in cpu-hotplug and here.
510 * This combination can cause a deadlock (cpu-hotplug try to lock
511 * text_mutex but stop_machine can not be done because online_cpus
513 * To avoid this deadlock, caller must have locked cpu hotplug
514 * for preventing cpu-hotplug outside of text_mutex locking.
516 lockdep_assert_cpus_held();
518 /* Optimization never be done when disarmed */
519 if (kprobes_all_disarmed || !kprobes_allow_optimization ||
520 list_empty(&optimizing_list))
523 arch_optimize_kprobes(&optimizing_list);
527 * Unoptimize (replace a jump with a breakpoint and remove the breakpoint
528 * if need) kprobes listed on unoptimizing_list.
530 static void do_unoptimize_kprobes(void)
532 struct optimized_kprobe *op, *tmp;
534 lockdep_assert_held(&text_mutex);
535 /* See comment in do_optimize_kprobes() */
536 lockdep_assert_cpus_held();
538 /* Unoptimization must be done anytime */
539 if (list_empty(&unoptimizing_list))
542 arch_unoptimize_kprobes(&unoptimizing_list, &freeing_list);
543 /* Loop free_list for disarming */
544 list_for_each_entry_safe(op, tmp, &freeing_list, list) {
545 /* Switching from detour code to origin */
546 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
547 /* Disarm probes if marked disabled */
548 if (kprobe_disabled(&op->kp))
549 arch_disarm_kprobe(&op->kp);
550 if (kprobe_unused(&op->kp)) {
552 * Remove unused probes from hash list. After waiting
553 * for synchronization, these probes are reclaimed.
554 * (reclaiming is done by do_free_cleaned_kprobes.)
556 hlist_del_rcu(&op->kp.hlist);
558 list_del_init(&op->list);
562 /* Reclaim all kprobes on the free_list */
563 static void do_free_cleaned_kprobes(void)
565 struct optimized_kprobe *op, *tmp;
567 list_for_each_entry_safe(op, tmp, &freeing_list, list) {
568 list_del_init(&op->list);
569 if (WARN_ON_ONCE(!kprobe_unused(&op->kp))) {
571 * This must not happen, but if there is a kprobe
572 * still in use, keep it on kprobes hash list.
576 free_aggr_kprobe(&op->kp);
580 /* Start optimizer after OPTIMIZE_DELAY passed */
581 static void kick_kprobe_optimizer(void)
583 schedule_delayed_work(&optimizing_work, OPTIMIZE_DELAY);
586 /* Kprobe jump optimizer */
587 static void kprobe_optimizer(struct work_struct *work)
589 mutex_lock(&kprobe_mutex);
591 mutex_lock(&text_mutex);
594 * Step 1: Unoptimize kprobes and collect cleaned (unused and disarmed)
595 * kprobes before waiting for quiesence period.
597 do_unoptimize_kprobes();
600 * Step 2: Wait for quiesence period to ensure all potentially
601 * preempted tasks to have normally scheduled. Because optprobe
602 * may modify multiple instructions, there is a chance that Nth
603 * instruction is preempted. In that case, such tasks can return
604 * to 2nd-Nth byte of jump instruction. This wait is for avoiding it.
605 * Note that on non-preemptive kernel, this is transparently converted
606 * to synchronoze_sched() to wait for all interrupts to have completed.
608 synchronize_rcu_tasks();
610 /* Step 3: Optimize kprobes after quiesence period */
611 do_optimize_kprobes();
613 /* Step 4: Free cleaned kprobes after quiesence period */
614 do_free_cleaned_kprobes();
616 mutex_unlock(&text_mutex);
619 /* Step 5: Kick optimizer again if needed */
620 if (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list))
621 kick_kprobe_optimizer();
623 mutex_unlock(&kprobe_mutex);
626 /* Wait for completing optimization and unoptimization */
627 void wait_for_kprobe_optimizer(void)
629 mutex_lock(&kprobe_mutex);
631 while (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list)) {
632 mutex_unlock(&kprobe_mutex);
634 /* this will also make optimizing_work execute immmediately */
635 flush_delayed_work(&optimizing_work);
636 /* @optimizing_work might not have been queued yet, relax */
639 mutex_lock(&kprobe_mutex);
642 mutex_unlock(&kprobe_mutex);
645 static bool optprobe_queued_unopt(struct optimized_kprobe *op)
647 struct optimized_kprobe *_op;
649 list_for_each_entry(_op, &unoptimizing_list, list) {
657 /* Optimize kprobe if p is ready to be optimized */
658 static void optimize_kprobe(struct kprobe *p)
660 struct optimized_kprobe *op;
662 /* Check if the kprobe is disabled or not ready for optimization. */
663 if (!kprobe_optready(p) || !kprobes_allow_optimization ||
664 (kprobe_disabled(p) || kprobes_all_disarmed))
667 /* kprobes with post_handler can not be optimized */
671 op = container_of(p, struct optimized_kprobe, kp);
673 /* Check there is no other kprobes at the optimized instructions */
674 if (arch_check_optimized_kprobe(op) < 0)
677 /* Check if it is already optimized. */
678 if (op->kp.flags & KPROBE_FLAG_OPTIMIZED) {
679 if (optprobe_queued_unopt(op)) {
680 /* This is under unoptimizing. Just dequeue the probe */
681 list_del_init(&op->list);
685 op->kp.flags |= KPROBE_FLAG_OPTIMIZED;
687 /* On unoptimizing/optimizing_list, op must have OPTIMIZED flag */
688 if (WARN_ON_ONCE(!list_empty(&op->list)))
691 list_add(&op->list, &optimizing_list);
692 kick_kprobe_optimizer();
695 /* Short cut to direct unoptimizing */
696 static void force_unoptimize_kprobe(struct optimized_kprobe *op)
698 lockdep_assert_cpus_held();
699 arch_unoptimize_kprobe(op);
700 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
703 /* Unoptimize a kprobe if p is optimized */
704 static void unoptimize_kprobe(struct kprobe *p, bool force)
706 struct optimized_kprobe *op;
708 if (!kprobe_aggrprobe(p) || kprobe_disarmed(p))
709 return; /* This is not an optprobe nor optimized */
711 op = container_of(p, struct optimized_kprobe, kp);
712 if (!kprobe_optimized(p))
715 if (!list_empty(&op->list)) {
716 if (optprobe_queued_unopt(op)) {
717 /* Queued in unoptimizing queue */
720 * Forcibly unoptimize the kprobe here, and queue it
721 * in the freeing list for release afterwards.
723 force_unoptimize_kprobe(op);
724 list_move(&op->list, &freeing_list);
727 /* Dequeue from the optimizing queue */
728 list_del_init(&op->list);
729 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
734 /* Optimized kprobe case */
736 /* Forcibly update the code: this is a special case */
737 force_unoptimize_kprobe(op);
739 list_add(&op->list, &unoptimizing_list);
740 kick_kprobe_optimizer();
744 /* Cancel unoptimizing for reusing */
745 static int reuse_unused_kprobe(struct kprobe *ap)
747 struct optimized_kprobe *op;
750 * Unused kprobe MUST be on the way of delayed unoptimizing (means
751 * there is still a relative jump) and disabled.
753 op = container_of(ap, struct optimized_kprobe, kp);
754 WARN_ON_ONCE(list_empty(&op->list));
755 /* Enable the probe again */
756 ap->flags &= ~KPROBE_FLAG_DISABLED;
757 /* Optimize it again (remove from op->list) */
758 if (!kprobe_optready(ap))
765 /* Remove optimized instructions */
766 static void kill_optimized_kprobe(struct kprobe *p)
768 struct optimized_kprobe *op;
770 op = container_of(p, struct optimized_kprobe, kp);
771 if (!list_empty(&op->list))
772 /* Dequeue from the (un)optimization queue */
773 list_del_init(&op->list);
774 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
776 if (kprobe_unused(p)) {
777 /* Enqueue if it is unused */
778 list_add(&op->list, &freeing_list);
780 * Remove unused probes from the hash list. After waiting
781 * for synchronization, this probe is reclaimed.
782 * (reclaiming is done by do_free_cleaned_kprobes().)
784 hlist_del_rcu(&op->kp.hlist);
787 /* Don't touch the code, because it is already freed. */
788 arch_remove_optimized_kprobe(op);
792 void __prepare_optimized_kprobe(struct optimized_kprobe *op, struct kprobe *p)
794 if (!kprobe_ftrace(p))
795 arch_prepare_optimized_kprobe(op, p);
798 /* Try to prepare optimized instructions */
799 static void prepare_optimized_kprobe(struct kprobe *p)
801 struct optimized_kprobe *op;
803 op = container_of(p, struct optimized_kprobe, kp);
804 __prepare_optimized_kprobe(op, p);
807 /* Allocate new optimized_kprobe and try to prepare optimized instructions */
808 static struct kprobe *alloc_aggr_kprobe(struct kprobe *p)
810 struct optimized_kprobe *op;
812 op = kzalloc(sizeof(struct optimized_kprobe), GFP_KERNEL);
816 INIT_LIST_HEAD(&op->list);
817 op->kp.addr = p->addr;
818 __prepare_optimized_kprobe(op, p);
823 static void init_aggr_kprobe(struct kprobe *ap, struct kprobe *p);
826 * Prepare an optimized_kprobe and optimize it
827 * NOTE: p must be a normal registered kprobe
829 static void try_to_optimize_kprobe(struct kprobe *p)
832 struct optimized_kprobe *op;
834 /* Impossible to optimize ftrace-based kprobe */
835 if (kprobe_ftrace(p))
838 /* For preparing optimization, jump_label_text_reserved() is called */
841 mutex_lock(&text_mutex);
843 ap = alloc_aggr_kprobe(p);
847 op = container_of(ap, struct optimized_kprobe, kp);
848 if (!arch_prepared_optinsn(&op->optinsn)) {
849 /* If failed to setup optimizing, fallback to kprobe */
850 arch_remove_optimized_kprobe(op);
855 init_aggr_kprobe(ap, p);
856 optimize_kprobe(ap); /* This just kicks optimizer thread */
859 mutex_unlock(&text_mutex);
864 static void optimize_all_kprobes(void)
866 struct hlist_head *head;
870 mutex_lock(&kprobe_mutex);
871 /* If optimization is already allowed, just return */
872 if (kprobes_allow_optimization)
876 kprobes_allow_optimization = true;
877 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
878 head = &kprobe_table[i];
879 hlist_for_each_entry(p, head, hlist)
880 if (!kprobe_disabled(p))
884 printk(KERN_INFO "Kprobes globally optimized\n");
886 mutex_unlock(&kprobe_mutex);
890 static void unoptimize_all_kprobes(void)
892 struct hlist_head *head;
896 mutex_lock(&kprobe_mutex);
897 /* If optimization is already prohibited, just return */
898 if (!kprobes_allow_optimization) {
899 mutex_unlock(&kprobe_mutex);
904 kprobes_allow_optimization = false;
905 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
906 head = &kprobe_table[i];
907 hlist_for_each_entry(p, head, hlist) {
908 if (!kprobe_disabled(p))
909 unoptimize_kprobe(p, false);
913 mutex_unlock(&kprobe_mutex);
915 /* Wait for unoptimizing completion */
916 wait_for_kprobe_optimizer();
917 printk(KERN_INFO "Kprobes globally unoptimized\n");
920 static DEFINE_MUTEX(kprobe_sysctl_mutex);
921 int sysctl_kprobes_optimization;
922 int proc_kprobes_optimization_handler(struct ctl_table *table, int write,
923 void *buffer, size_t *length,
928 mutex_lock(&kprobe_sysctl_mutex);
929 sysctl_kprobes_optimization = kprobes_allow_optimization ? 1 : 0;
930 ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
932 if (sysctl_kprobes_optimization)
933 optimize_all_kprobes();
935 unoptimize_all_kprobes();
936 mutex_unlock(&kprobe_sysctl_mutex);
940 #endif /* CONFIG_SYSCTL */
942 /* Put a breakpoint for a probe. Must be called with text_mutex locked */
943 static void __arm_kprobe(struct kprobe *p)
947 /* Check collision with other optimized kprobes */
948 _p = get_optimized_kprobe((unsigned long)p->addr);
950 /* Fallback to unoptimized kprobe */
951 unoptimize_kprobe(_p, true);
954 optimize_kprobe(p); /* Try to optimize (add kprobe to a list) */
957 /* Remove the breakpoint of a probe. Must be called with text_mutex locked */
958 static void __disarm_kprobe(struct kprobe *p, bool reopt)
962 /* Try to unoptimize */
963 unoptimize_kprobe(p, kprobes_all_disarmed);
965 if (!kprobe_queued(p)) {
966 arch_disarm_kprobe(p);
967 /* If another kprobe was blocked, optimize it. */
968 _p = get_optimized_kprobe((unsigned long)p->addr);
969 if (unlikely(_p) && reopt)
972 /* TODO: reoptimize others after unoptimized this probe */
975 #else /* !CONFIG_OPTPROBES */
977 #define optimize_kprobe(p) do {} while (0)
978 #define unoptimize_kprobe(p, f) do {} while (0)
979 #define kill_optimized_kprobe(p) do {} while (0)
980 #define prepare_optimized_kprobe(p) do {} while (0)
981 #define try_to_optimize_kprobe(p) do {} while (0)
982 #define __arm_kprobe(p) arch_arm_kprobe(p)
983 #define __disarm_kprobe(p, o) arch_disarm_kprobe(p)
984 #define kprobe_disarmed(p) kprobe_disabled(p)
985 #define wait_for_kprobe_optimizer() do {} while (0)
987 static int reuse_unused_kprobe(struct kprobe *ap)
990 * If the optimized kprobe is NOT supported, the aggr kprobe is
991 * released at the same time that the last aggregated kprobe is
993 * Thus there should be no chance to reuse unused kprobe.
995 printk(KERN_ERR "Error: There should be no unused kprobe here.\n");
999 static void free_aggr_kprobe(struct kprobe *p)
1001 arch_remove_kprobe(p);
1005 static struct kprobe *alloc_aggr_kprobe(struct kprobe *p)
1007 return kzalloc(sizeof(struct kprobe), GFP_KERNEL);
1009 #endif /* CONFIG_OPTPROBES */
1011 #ifdef CONFIG_KPROBES_ON_FTRACE
1012 static struct ftrace_ops kprobe_ftrace_ops __read_mostly = {
1013 .func = kprobe_ftrace_handler,
1014 .flags = FTRACE_OPS_FL_SAVE_REGS,
1017 static struct ftrace_ops kprobe_ipmodify_ops __read_mostly = {
1018 .func = kprobe_ftrace_handler,
1019 .flags = FTRACE_OPS_FL_SAVE_REGS | FTRACE_OPS_FL_IPMODIFY,
1022 static int kprobe_ipmodify_enabled;
1023 static int kprobe_ftrace_enabled;
1025 /* Must ensure p->addr is really on ftrace */
1026 static int prepare_kprobe(struct kprobe *p)
1028 if (!kprobe_ftrace(p))
1029 return arch_prepare_kprobe(p);
1031 return arch_prepare_kprobe_ftrace(p);
1034 /* Caller must lock kprobe_mutex */
1035 static int __arm_kprobe_ftrace(struct kprobe *p, struct ftrace_ops *ops,
1040 ret = ftrace_set_filter_ip(ops, (unsigned long)p->addr, 0, 0);
1042 pr_debug("Failed to arm kprobe-ftrace at %pS (%d)\n",
1048 ret = register_ftrace_function(ops);
1050 pr_debug("Failed to init kprobe-ftrace (%d)\n", ret);
1060 * At this point, sinec ops is not registered, we should be sefe from
1061 * registering empty filter.
1063 ftrace_set_filter_ip(ops, (unsigned long)p->addr, 1, 0);
1067 static int arm_kprobe_ftrace(struct kprobe *p)
1069 bool ipmodify = (p->post_handler != NULL);
1071 return __arm_kprobe_ftrace(p,
1072 ipmodify ? &kprobe_ipmodify_ops : &kprobe_ftrace_ops,
1073 ipmodify ? &kprobe_ipmodify_enabled : &kprobe_ftrace_enabled);
1076 /* Caller must lock kprobe_mutex */
1077 static int __disarm_kprobe_ftrace(struct kprobe *p, struct ftrace_ops *ops,
1083 ret = unregister_ftrace_function(ops);
1084 if (WARN(ret < 0, "Failed to unregister kprobe-ftrace (%d)\n", ret))
1090 ret = ftrace_set_filter_ip(ops, (unsigned long)p->addr, 1, 0);
1091 WARN_ONCE(ret < 0, "Failed to disarm kprobe-ftrace at %pS (%d)\n",
1096 static int disarm_kprobe_ftrace(struct kprobe *p)
1098 bool ipmodify = (p->post_handler != NULL);
1100 return __disarm_kprobe_ftrace(p,
1101 ipmodify ? &kprobe_ipmodify_ops : &kprobe_ftrace_ops,
1102 ipmodify ? &kprobe_ipmodify_enabled : &kprobe_ftrace_enabled);
1104 #else /* !CONFIG_KPROBES_ON_FTRACE */
1105 static inline int prepare_kprobe(struct kprobe *p)
1107 return arch_prepare_kprobe(p);
1110 static inline int arm_kprobe_ftrace(struct kprobe *p)
1115 static inline int disarm_kprobe_ftrace(struct kprobe *p)
1121 /* Arm a kprobe with text_mutex */
1122 static int arm_kprobe(struct kprobe *kp)
1124 if (unlikely(kprobe_ftrace(kp)))
1125 return arm_kprobe_ftrace(kp);
1128 mutex_lock(&text_mutex);
1130 mutex_unlock(&text_mutex);
1136 /* Disarm a kprobe with text_mutex */
1137 static int disarm_kprobe(struct kprobe *kp, bool reopt)
1139 if (unlikely(kprobe_ftrace(kp)))
1140 return disarm_kprobe_ftrace(kp);
1143 mutex_lock(&text_mutex);
1144 __disarm_kprobe(kp, reopt);
1145 mutex_unlock(&text_mutex);
1152 * Aggregate handlers for multiple kprobes support - these handlers
1153 * take care of invoking the individual kprobe handlers on p->list
1155 static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
1159 list_for_each_entry_rcu(kp, &p->list, list) {
1160 if (kp->pre_handler && likely(!kprobe_disabled(kp))) {
1161 set_kprobe_instance(kp);
1162 if (kp->pre_handler(kp, regs))
1165 reset_kprobe_instance();
1169 NOKPROBE_SYMBOL(aggr_pre_handler);
1171 static void aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
1172 unsigned long flags)
1176 list_for_each_entry_rcu(kp, &p->list, list) {
1177 if (kp->post_handler && likely(!kprobe_disabled(kp))) {
1178 set_kprobe_instance(kp);
1179 kp->post_handler(kp, regs, flags);
1180 reset_kprobe_instance();
1184 NOKPROBE_SYMBOL(aggr_post_handler);
1186 /* Walks the list and increments nmissed count for multiprobe case */
1187 void kprobes_inc_nmissed_count(struct kprobe *p)
1190 if (!kprobe_aggrprobe(p)) {
1193 list_for_each_entry_rcu(kp, &p->list, list)
1198 NOKPROBE_SYMBOL(kprobes_inc_nmissed_count);
1200 static void free_rp_inst_rcu(struct rcu_head *head)
1202 struct kretprobe_instance *ri = container_of(head, struct kretprobe_instance, rcu);
1204 if (refcount_dec_and_test(&ri->rph->ref))
1208 NOKPROBE_SYMBOL(free_rp_inst_rcu);
1210 static void recycle_rp_inst(struct kretprobe_instance *ri)
1212 struct kretprobe *rp = get_kretprobe(ri);
1215 freelist_add(&ri->freelist, &rp->freelist);
1217 call_rcu(&ri->rcu, free_rp_inst_rcu);
1219 NOKPROBE_SYMBOL(recycle_rp_inst);
1221 static struct kprobe kprobe_busy = {
1222 .addr = (void *) get_kprobe,
1225 void kprobe_busy_begin(void)
1227 struct kprobe_ctlblk *kcb;
1230 __this_cpu_write(current_kprobe, &kprobe_busy);
1231 kcb = get_kprobe_ctlblk();
1232 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
1235 void kprobe_busy_end(void)
1237 __this_cpu_write(current_kprobe, NULL);
1242 * This function is called from finish_task_switch when task tk becomes dead,
1243 * so that we can recycle any function-return probe instances associated
1244 * with this task. These left over instances represent probed functions
1245 * that have been called but will never return.
1247 void kprobe_flush_task(struct task_struct *tk)
1249 struct kretprobe_instance *ri;
1250 struct llist_node *node;
1252 /* Early boot, not yet initialized. */
1253 if (unlikely(!kprobes_initialized))
1256 kprobe_busy_begin();
1258 node = __llist_del_all(&tk->kretprobe_instances);
1260 ri = container_of(node, struct kretprobe_instance, llist);
1263 recycle_rp_inst(ri);
1268 NOKPROBE_SYMBOL(kprobe_flush_task);
1270 static inline void free_rp_inst(struct kretprobe *rp)
1272 struct kretprobe_instance *ri;
1273 struct freelist_node *node;
1276 node = rp->freelist.head;
1278 ri = container_of(node, struct kretprobe_instance, freelist);
1285 if (refcount_sub_and_test(count, &rp->rph->ref)) {
1291 /* Add the new probe to ap->list */
1292 static int add_new_kprobe(struct kprobe *ap, struct kprobe *p)
1294 if (p->post_handler)
1295 unoptimize_kprobe(ap, true); /* Fall back to normal kprobe */
1297 list_add_rcu(&p->list, &ap->list);
1298 if (p->post_handler && !ap->post_handler)
1299 ap->post_handler = aggr_post_handler;
1305 * Fill in the required fields of the "manager kprobe". Replace the
1306 * earlier kprobe in the hlist with the manager kprobe
1308 static void init_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
1310 /* Copy p's insn slot to ap */
1312 flush_insn_slot(ap);
1314 ap->flags = p->flags & ~KPROBE_FLAG_OPTIMIZED;
1315 ap->pre_handler = aggr_pre_handler;
1316 /* We don't care the kprobe which has gone. */
1317 if (p->post_handler && !kprobe_gone(p))
1318 ap->post_handler = aggr_post_handler;
1320 INIT_LIST_HEAD(&ap->list);
1321 INIT_HLIST_NODE(&ap->hlist);
1323 list_add_rcu(&p->list, &ap->list);
1324 hlist_replace_rcu(&p->hlist, &ap->hlist);
1328 * This is the second or subsequent kprobe at the address - handle
1331 static int register_aggr_kprobe(struct kprobe *orig_p, struct kprobe *p)
1334 struct kprobe *ap = orig_p;
1338 /* For preparing optimization, jump_label_text_reserved() is called */
1340 mutex_lock(&text_mutex);
1342 if (!kprobe_aggrprobe(orig_p)) {
1343 /* If orig_p is not an aggr_kprobe, create new aggr_kprobe. */
1344 ap = alloc_aggr_kprobe(orig_p);
1349 init_aggr_kprobe(ap, orig_p);
1350 } else if (kprobe_unused(ap)) {
1351 /* This probe is going to die. Rescue it */
1352 ret = reuse_unused_kprobe(ap);
1357 if (kprobe_gone(ap)) {
1359 * Attempting to insert new probe at the same location that
1360 * had a probe in the module vaddr area which already
1361 * freed. So, the instruction slot has already been
1362 * released. We need a new slot for the new probe.
1364 ret = arch_prepare_kprobe(ap);
1367 * Even if fail to allocate new slot, don't need to
1368 * free aggr_probe. It will be used next time, or
1369 * freed by unregister_kprobe.
1373 /* Prepare optimized instructions if possible. */
1374 prepare_optimized_kprobe(ap);
1377 * Clear gone flag to prevent allocating new slot again, and
1378 * set disabled flag because it is not armed yet.
1380 ap->flags = (ap->flags & ~KPROBE_FLAG_GONE)
1381 | KPROBE_FLAG_DISABLED;
1384 /* Copy ap's insn slot to p */
1386 ret = add_new_kprobe(ap, p);
1389 mutex_unlock(&text_mutex);
1390 jump_label_unlock();
1393 if (ret == 0 && kprobe_disabled(ap) && !kprobe_disabled(p)) {
1394 ap->flags &= ~KPROBE_FLAG_DISABLED;
1395 if (!kprobes_all_disarmed) {
1396 /* Arm the breakpoint again. */
1397 ret = arm_kprobe(ap);
1399 ap->flags |= KPROBE_FLAG_DISABLED;
1400 list_del_rcu(&p->list);
1408 bool __weak arch_within_kprobe_blacklist(unsigned long addr)
1410 /* The __kprobes marked functions and entry code must not be probed */
1411 return addr >= (unsigned long)__kprobes_text_start &&
1412 addr < (unsigned long)__kprobes_text_end;
1415 static bool __within_kprobe_blacklist(unsigned long addr)
1417 struct kprobe_blacklist_entry *ent;
1419 if (arch_within_kprobe_blacklist(addr))
1422 * If there exists a kprobe_blacklist, verify and
1423 * fail any probe registration in the prohibited area
1425 list_for_each_entry(ent, &kprobe_blacklist, list) {
1426 if (addr >= ent->start_addr && addr < ent->end_addr)
1432 bool within_kprobe_blacklist(unsigned long addr)
1434 char symname[KSYM_NAME_LEN], *p;
1436 if (__within_kprobe_blacklist(addr))
1439 /* Check if the address is on a suffixed-symbol */
1440 if (!lookup_symbol_name(addr, symname)) {
1441 p = strchr(symname, '.');
1445 addr = (unsigned long)kprobe_lookup_name(symname, 0);
1447 return __within_kprobe_blacklist(addr);
1453 * If we have a symbol_name argument, look it up and add the offset field
1454 * to it. This way, we can specify a relative address to a symbol.
1455 * This returns encoded errors if it fails to look up symbol or invalid
1456 * combination of parameters.
1458 static kprobe_opcode_t *_kprobe_addr(kprobe_opcode_t *addr,
1459 const char *symbol_name, unsigned int offset)
1461 if ((symbol_name && addr) || (!symbol_name && !addr))
1465 addr = kprobe_lookup_name(symbol_name, offset);
1467 return ERR_PTR(-ENOENT);
1470 addr = (kprobe_opcode_t *)(((char *)addr) + offset);
1475 return ERR_PTR(-EINVAL);
1478 static kprobe_opcode_t *kprobe_addr(struct kprobe *p)
1480 return _kprobe_addr(p->addr, p->symbol_name, p->offset);
1483 /* Check passed kprobe is valid and return kprobe in kprobe_table. */
1484 static struct kprobe *__get_valid_kprobe(struct kprobe *p)
1486 struct kprobe *ap, *list_p;
1488 lockdep_assert_held(&kprobe_mutex);
1490 ap = get_kprobe(p->addr);
1495 list_for_each_entry(list_p, &ap->list, list)
1497 /* kprobe p is a valid probe */
1506 * Warn and return error if the kprobe is being re-registered since
1507 * there must be a software bug.
1509 static inline int warn_kprobe_rereg(struct kprobe *p)
1513 mutex_lock(&kprobe_mutex);
1514 if (WARN_ON_ONCE(__get_valid_kprobe(p)))
1516 mutex_unlock(&kprobe_mutex);
1521 int __weak arch_check_ftrace_location(struct kprobe *p)
1523 unsigned long ftrace_addr;
1525 ftrace_addr = ftrace_location((unsigned long)p->addr);
1527 #ifdef CONFIG_KPROBES_ON_FTRACE
1528 /* Given address is not on the instruction boundary */
1529 if ((unsigned long)p->addr != ftrace_addr)
1531 p->flags |= KPROBE_FLAG_FTRACE;
1532 #else /* !CONFIG_KPROBES_ON_FTRACE */
1539 static int check_kprobe_address_safe(struct kprobe *p,
1540 struct module **probed_mod)
1544 ret = arch_check_ftrace_location(p);
1550 /* Ensure it is not in reserved area nor out of text */
1551 if (!kernel_text_address((unsigned long) p->addr) ||
1552 within_kprobe_blacklist((unsigned long) p->addr) ||
1553 jump_label_text_reserved(p->addr, p->addr) ||
1554 find_bug((unsigned long)p->addr)) {
1559 /* Check if are we probing a module */
1560 *probed_mod = __module_text_address((unsigned long) p->addr);
1563 * We must hold a refcount of the probed module while updating
1564 * its code to prohibit unexpected unloading.
1566 if (unlikely(!try_module_get(*probed_mod))) {
1572 * If the module freed .init.text, we couldn't insert
1575 if (within_module_init((unsigned long)p->addr, *probed_mod) &&
1576 (*probed_mod)->state != MODULE_STATE_COMING) {
1577 module_put(*probed_mod);
1584 jump_label_unlock();
1589 int register_kprobe(struct kprobe *p)
1592 struct kprobe *old_p;
1593 struct module *probed_mod;
1594 kprobe_opcode_t *addr;
1596 /* Adjust probe address from symbol */
1597 addr = kprobe_addr(p);
1599 return PTR_ERR(addr);
1602 ret = warn_kprobe_rereg(p);
1606 /* User can pass only KPROBE_FLAG_DISABLED to register_kprobe */
1607 p->flags &= KPROBE_FLAG_DISABLED;
1609 INIT_LIST_HEAD(&p->list);
1611 ret = check_kprobe_address_safe(p, &probed_mod);
1615 mutex_lock(&kprobe_mutex);
1617 old_p = get_kprobe(p->addr);
1619 /* Since this may unoptimize old_p, locking text_mutex. */
1620 ret = register_aggr_kprobe(old_p, p);
1625 /* Prevent text modification */
1626 mutex_lock(&text_mutex);
1627 ret = prepare_kprobe(p);
1628 mutex_unlock(&text_mutex);
1633 INIT_HLIST_NODE(&p->hlist);
1634 hlist_add_head_rcu(&p->hlist,
1635 &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
1637 if (!kprobes_all_disarmed && !kprobe_disabled(p)) {
1638 ret = arm_kprobe(p);
1640 hlist_del_rcu(&p->hlist);
1646 /* Try to optimize kprobe */
1647 try_to_optimize_kprobe(p);
1649 mutex_unlock(&kprobe_mutex);
1652 module_put(probed_mod);
1656 EXPORT_SYMBOL_GPL(register_kprobe);
1658 /* Check if all probes on the aggrprobe are disabled */
1659 static int aggr_kprobe_disabled(struct kprobe *ap)
1663 lockdep_assert_held(&kprobe_mutex);
1665 list_for_each_entry(kp, &ap->list, list)
1666 if (!kprobe_disabled(kp))
1668 * There is an active probe on the list.
1669 * We can't disable this ap.
1676 /* Disable one kprobe: Make sure called under kprobe_mutex is locked */
1677 static struct kprobe *__disable_kprobe(struct kprobe *p)
1679 struct kprobe *orig_p;
1682 /* Get an original kprobe for return */
1683 orig_p = __get_valid_kprobe(p);
1684 if (unlikely(orig_p == NULL))
1685 return ERR_PTR(-EINVAL);
1687 if (!kprobe_disabled(p)) {
1688 /* Disable probe if it is a child probe */
1690 p->flags |= KPROBE_FLAG_DISABLED;
1692 /* Try to disarm and disable this/parent probe */
1693 if (p == orig_p || aggr_kprobe_disabled(orig_p)) {
1695 * If kprobes_all_disarmed is set, orig_p
1696 * should have already been disarmed, so
1697 * skip unneed disarming process.
1699 if (!kprobes_all_disarmed) {
1700 ret = disarm_kprobe(orig_p, true);
1702 p->flags &= ~KPROBE_FLAG_DISABLED;
1703 return ERR_PTR(ret);
1706 orig_p->flags |= KPROBE_FLAG_DISABLED;
1714 * Unregister a kprobe without a scheduler synchronization.
1716 static int __unregister_kprobe_top(struct kprobe *p)
1718 struct kprobe *ap, *list_p;
1720 /* Disable kprobe. This will disarm it if needed. */
1721 ap = __disable_kprobe(p);
1727 * This probe is an independent(and non-optimized) kprobe
1728 * (not an aggrprobe). Remove from the hash list.
1732 /* Following process expects this probe is an aggrprobe */
1733 WARN_ON(!kprobe_aggrprobe(ap));
1735 if (list_is_singular(&ap->list) && kprobe_disarmed(ap))
1737 * !disarmed could be happen if the probe is under delayed
1742 /* If disabling probe has special handlers, update aggrprobe */
1743 if (p->post_handler && !kprobe_gone(p)) {
1744 list_for_each_entry(list_p, &ap->list, list) {
1745 if ((list_p != p) && (list_p->post_handler))
1748 ap->post_handler = NULL;
1752 * Remove from the aggrprobe: this path will do nothing in
1753 * __unregister_kprobe_bottom().
1755 list_del_rcu(&p->list);
1756 if (!kprobe_disabled(ap) && !kprobes_all_disarmed)
1758 * Try to optimize this probe again, because post
1759 * handler may have been changed.
1761 optimize_kprobe(ap);
1766 hlist_del_rcu(&ap->hlist);
1770 static void __unregister_kprobe_bottom(struct kprobe *p)
1774 if (list_empty(&p->list))
1775 /* This is an independent kprobe */
1776 arch_remove_kprobe(p);
1777 else if (list_is_singular(&p->list)) {
1778 /* This is the last child of an aggrprobe */
1779 ap = list_entry(p->list.next, struct kprobe, list);
1781 free_aggr_kprobe(ap);
1783 /* Otherwise, do nothing. */
1786 int register_kprobes(struct kprobe **kps, int num)
1792 for (i = 0; i < num; i++) {
1793 ret = register_kprobe(kps[i]);
1796 unregister_kprobes(kps, i);
1802 EXPORT_SYMBOL_GPL(register_kprobes);
1804 void unregister_kprobe(struct kprobe *p)
1806 unregister_kprobes(&p, 1);
1808 EXPORT_SYMBOL_GPL(unregister_kprobe);
1810 void unregister_kprobes(struct kprobe **kps, int num)
1816 mutex_lock(&kprobe_mutex);
1817 for (i = 0; i < num; i++)
1818 if (__unregister_kprobe_top(kps[i]) < 0)
1819 kps[i]->addr = NULL;
1820 mutex_unlock(&kprobe_mutex);
1823 for (i = 0; i < num; i++)
1825 __unregister_kprobe_bottom(kps[i]);
1827 EXPORT_SYMBOL_GPL(unregister_kprobes);
1829 int __weak kprobe_exceptions_notify(struct notifier_block *self,
1830 unsigned long val, void *data)
1834 NOKPROBE_SYMBOL(kprobe_exceptions_notify);
1836 static struct notifier_block kprobe_exceptions_nb = {
1837 .notifier_call = kprobe_exceptions_notify,
1838 .priority = 0x7fffffff /* we need to be notified first */
1841 unsigned long __weak arch_deref_entry_point(void *entry)
1843 return (unsigned long)entry;
1846 #ifdef CONFIG_KRETPROBES
1848 unsigned long __kretprobe_trampoline_handler(struct pt_regs *regs,
1849 void *trampoline_address,
1850 void *frame_pointer)
1852 kprobe_opcode_t *correct_ret_addr = NULL;
1853 struct kretprobe_instance *ri = NULL;
1854 struct llist_node *first, *node;
1855 struct kretprobe *rp;
1857 /* Find all nodes for this frame. */
1858 first = node = current->kretprobe_instances.first;
1860 ri = container_of(node, struct kretprobe_instance, llist);
1862 BUG_ON(ri->fp != frame_pointer);
1864 if (ri->ret_addr != trampoline_address) {
1865 correct_ret_addr = ri->ret_addr;
1867 * This is the real return address. Any other
1868 * instances associated with this task are for
1869 * other calls deeper on the call stack
1876 pr_err("Oops! Kretprobe fails to find correct return address.\n");
1880 /* Unlink all nodes for this frame. */
1881 current->kretprobe_instances.first = node->next;
1886 ri = container_of(first, struct kretprobe_instance, llist);
1887 first = first->next;
1889 rp = get_kretprobe(ri);
1890 if (rp && rp->handler) {
1891 struct kprobe *prev = kprobe_running();
1893 __this_cpu_write(current_kprobe, &rp->kp);
1894 ri->ret_addr = correct_ret_addr;
1895 rp->handler(ri, regs);
1896 __this_cpu_write(current_kprobe, prev);
1899 recycle_rp_inst(ri);
1902 return (unsigned long)correct_ret_addr;
1904 NOKPROBE_SYMBOL(__kretprobe_trampoline_handler)
1907 * This kprobe pre_handler is registered with every kretprobe. When probe
1908 * hits it will set up the return probe.
1910 static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
1912 struct kretprobe *rp = container_of(p, struct kretprobe, kp);
1913 struct kretprobe_instance *ri;
1914 struct freelist_node *fn;
1916 fn = freelist_try_get(&rp->freelist);
1922 ri = container_of(fn, struct kretprobe_instance, freelist);
1924 if (rp->entry_handler && rp->entry_handler(ri, regs)) {
1925 freelist_add(&ri->freelist, &rp->freelist);
1929 arch_prepare_kretprobe(ri, regs);
1931 __llist_add(&ri->llist, ¤t->kretprobe_instances);
1935 NOKPROBE_SYMBOL(pre_handler_kretprobe);
1937 bool __weak arch_kprobe_on_func_entry(unsigned long offset)
1943 * kprobe_on_func_entry() -- check whether given address is function entry
1944 * @addr: Target address
1945 * @sym: Target symbol name
1946 * @offset: The offset from the symbol or the address
1948 * This checks whether the given @addr+@offset or @sym+@offset is on the
1949 * function entry address or not.
1950 * This returns 0 if it is the function entry, or -EINVAL if it is not.
1951 * And also it returns -ENOENT if it fails the symbol or address lookup.
1952 * Caller must pass @addr or @sym (either one must be NULL), or this
1955 int kprobe_on_func_entry(kprobe_opcode_t *addr, const char *sym, unsigned long offset)
1957 kprobe_opcode_t *kp_addr = _kprobe_addr(addr, sym, offset);
1959 if (IS_ERR(kp_addr))
1960 return PTR_ERR(kp_addr);
1962 if (!kallsyms_lookup_size_offset((unsigned long)kp_addr, NULL, &offset))
1965 if (!arch_kprobe_on_func_entry(offset))
1971 int register_kretprobe(struct kretprobe *rp)
1974 struct kretprobe_instance *inst;
1978 ret = kprobe_on_func_entry(rp->kp.addr, rp->kp.symbol_name, rp->kp.offset);
1982 /* If only rp->kp.addr is specified, check reregistering kprobes */
1983 if (rp->kp.addr && warn_kprobe_rereg(&rp->kp))
1986 if (kretprobe_blacklist_size) {
1987 addr = kprobe_addr(&rp->kp);
1989 return PTR_ERR(addr);
1991 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
1992 if (kretprobe_blacklist[i].addr == addr)
1997 rp->kp.pre_handler = pre_handler_kretprobe;
1998 rp->kp.post_handler = NULL;
2000 /* Pre-allocate memory for max kretprobe instances */
2001 if (rp->maxactive <= 0) {
2002 #ifdef CONFIG_PREEMPTION
2003 rp->maxactive = max_t(unsigned int, 10, 2*num_possible_cpus());
2005 rp->maxactive = num_possible_cpus();
2008 rp->freelist.head = NULL;
2009 rp->rph = kzalloc(sizeof(struct kretprobe_holder), GFP_KERNEL);
2014 for (i = 0; i < rp->maxactive; i++) {
2015 inst = kzalloc(sizeof(struct kretprobe_instance) +
2016 rp->data_size, GFP_KERNEL);
2018 refcount_set(&rp->rph->ref, i);
2022 inst->rph = rp->rph;
2023 freelist_add(&inst->freelist, &rp->freelist);
2025 refcount_set(&rp->rph->ref, i);
2028 /* Establish function entry probe point */
2029 ret = register_kprobe(&rp->kp);
2034 EXPORT_SYMBOL_GPL(register_kretprobe);
2036 int register_kretprobes(struct kretprobe **rps, int num)
2042 for (i = 0; i < num; i++) {
2043 ret = register_kretprobe(rps[i]);
2046 unregister_kretprobes(rps, i);
2052 EXPORT_SYMBOL_GPL(register_kretprobes);
2054 void unregister_kretprobe(struct kretprobe *rp)
2056 unregister_kretprobes(&rp, 1);
2058 EXPORT_SYMBOL_GPL(unregister_kretprobe);
2060 void unregister_kretprobes(struct kretprobe **rps, int num)
2066 mutex_lock(&kprobe_mutex);
2067 for (i = 0; i < num; i++) {
2068 if (__unregister_kprobe_top(&rps[i]->kp) < 0)
2069 rps[i]->kp.addr = NULL;
2070 rps[i]->rph->rp = NULL;
2072 mutex_unlock(&kprobe_mutex);
2075 for (i = 0; i < num; i++) {
2076 if (rps[i]->kp.addr) {
2077 __unregister_kprobe_bottom(&rps[i]->kp);
2078 free_rp_inst(rps[i]);
2082 EXPORT_SYMBOL_GPL(unregister_kretprobes);
2084 #else /* CONFIG_KRETPROBES */
2085 int register_kretprobe(struct kretprobe *rp)
2089 EXPORT_SYMBOL_GPL(register_kretprobe);
2091 int register_kretprobes(struct kretprobe **rps, int num)
2095 EXPORT_SYMBOL_GPL(register_kretprobes);
2097 void unregister_kretprobe(struct kretprobe *rp)
2100 EXPORT_SYMBOL_GPL(unregister_kretprobe);
2102 void unregister_kretprobes(struct kretprobe **rps, int num)
2105 EXPORT_SYMBOL_GPL(unregister_kretprobes);
2107 static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
2111 NOKPROBE_SYMBOL(pre_handler_kretprobe);
2113 #endif /* CONFIG_KRETPROBES */
2115 /* Set the kprobe gone and remove its instruction buffer. */
2116 static void kill_kprobe(struct kprobe *p)
2120 lockdep_assert_held(&kprobe_mutex);
2122 p->flags |= KPROBE_FLAG_GONE;
2123 if (kprobe_aggrprobe(p)) {
2125 * If this is an aggr_kprobe, we have to list all the
2126 * chained probes and mark them GONE.
2128 list_for_each_entry(kp, &p->list, list)
2129 kp->flags |= KPROBE_FLAG_GONE;
2130 p->post_handler = NULL;
2131 kill_optimized_kprobe(p);
2134 * Here, we can remove insn_slot safely, because no thread calls
2135 * the original probed function (which will be freed soon) any more.
2137 arch_remove_kprobe(p);
2140 * The module is going away. We should disarm the kprobe which
2141 * is using ftrace, because ftrace framework is still available at
2142 * MODULE_STATE_GOING notification.
2144 if (kprobe_ftrace(p) && !kprobe_disabled(p) && !kprobes_all_disarmed)
2145 disarm_kprobe_ftrace(p);
2148 /* Disable one kprobe */
2149 int disable_kprobe(struct kprobe *kp)
2154 mutex_lock(&kprobe_mutex);
2156 /* Disable this kprobe */
2157 p = __disable_kprobe(kp);
2161 mutex_unlock(&kprobe_mutex);
2164 EXPORT_SYMBOL_GPL(disable_kprobe);
2166 /* Enable one kprobe */
2167 int enable_kprobe(struct kprobe *kp)
2172 mutex_lock(&kprobe_mutex);
2174 /* Check whether specified probe is valid. */
2175 p = __get_valid_kprobe(kp);
2176 if (unlikely(p == NULL)) {
2181 if (kprobe_gone(kp)) {
2182 /* This kprobe has gone, we couldn't enable it. */
2188 kp->flags &= ~KPROBE_FLAG_DISABLED;
2190 if (!kprobes_all_disarmed && kprobe_disabled(p)) {
2191 p->flags &= ~KPROBE_FLAG_DISABLED;
2192 ret = arm_kprobe(p);
2194 p->flags |= KPROBE_FLAG_DISABLED;
2197 mutex_unlock(&kprobe_mutex);
2200 EXPORT_SYMBOL_GPL(enable_kprobe);
2202 /* Caller must NOT call this in usual path. This is only for critical case */
2203 void dump_kprobe(struct kprobe *kp)
2205 pr_err("Dumping kprobe:\n");
2206 pr_err("Name: %s\nOffset: %x\nAddress: %pS\n",
2207 kp->symbol_name, kp->offset, kp->addr);
2209 NOKPROBE_SYMBOL(dump_kprobe);
2211 int kprobe_add_ksym_blacklist(unsigned long entry)
2213 struct kprobe_blacklist_entry *ent;
2214 unsigned long offset = 0, size = 0;
2216 if (!kernel_text_address(entry) ||
2217 !kallsyms_lookup_size_offset(entry, &size, &offset))
2220 ent = kmalloc(sizeof(*ent), GFP_KERNEL);
2223 ent->start_addr = entry;
2224 ent->end_addr = entry + size;
2225 INIT_LIST_HEAD(&ent->list);
2226 list_add_tail(&ent->list, &kprobe_blacklist);
2231 /* Add all symbols in given area into kprobe blacklist */
2232 int kprobe_add_area_blacklist(unsigned long start, unsigned long end)
2234 unsigned long entry;
2237 for (entry = start; entry < end; entry += ret) {
2238 ret = kprobe_add_ksym_blacklist(entry);
2241 if (ret == 0) /* In case of alias symbol */
2247 /* Remove all symbols in given area from kprobe blacklist */
2248 static void kprobe_remove_area_blacklist(unsigned long start, unsigned long end)
2250 struct kprobe_blacklist_entry *ent, *n;
2252 list_for_each_entry_safe(ent, n, &kprobe_blacklist, list) {
2253 if (ent->start_addr < start || ent->start_addr >= end)
2255 list_del(&ent->list);
2260 static void kprobe_remove_ksym_blacklist(unsigned long entry)
2262 kprobe_remove_area_blacklist(entry, entry + 1);
2265 int __weak arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *value,
2266 char *type, char *sym)
2271 int kprobe_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2274 #ifdef __ARCH_WANT_KPROBES_INSN_SLOT
2275 if (!kprobe_cache_get_kallsym(&kprobe_insn_slots, &symnum, value, type, sym))
2277 #ifdef CONFIG_OPTPROBES
2278 if (!kprobe_cache_get_kallsym(&kprobe_optinsn_slots, &symnum, value, type, sym))
2282 if (!arch_kprobe_get_kallsym(&symnum, value, type, sym))
2287 int __init __weak arch_populate_kprobe_blacklist(void)
2293 * Lookup and populate the kprobe_blacklist.
2295 * Unlike the kretprobe blacklist, we'll need to determine
2296 * the range of addresses that belong to the said functions,
2297 * since a kprobe need not necessarily be at the beginning
2300 static int __init populate_kprobe_blacklist(unsigned long *start,
2303 unsigned long entry;
2304 unsigned long *iter;
2307 for (iter = start; iter < end; iter++) {
2308 entry = arch_deref_entry_point((void *)*iter);
2309 ret = kprobe_add_ksym_blacklist(entry);
2316 /* Symbols in __kprobes_text are blacklisted */
2317 ret = kprobe_add_area_blacklist((unsigned long)__kprobes_text_start,
2318 (unsigned long)__kprobes_text_end);
2322 /* Symbols in noinstr section are blacklisted */
2323 ret = kprobe_add_area_blacklist((unsigned long)__noinstr_text_start,
2324 (unsigned long)__noinstr_text_end);
2326 return ret ? : arch_populate_kprobe_blacklist();
2329 static void add_module_kprobe_blacklist(struct module *mod)
2331 unsigned long start, end;
2334 if (mod->kprobe_blacklist) {
2335 for (i = 0; i < mod->num_kprobe_blacklist; i++)
2336 kprobe_add_ksym_blacklist(mod->kprobe_blacklist[i]);
2339 start = (unsigned long)mod->kprobes_text_start;
2341 end = start + mod->kprobes_text_size;
2342 kprobe_add_area_blacklist(start, end);
2345 start = (unsigned long)mod->noinstr_text_start;
2347 end = start + mod->noinstr_text_size;
2348 kprobe_add_area_blacklist(start, end);
2352 static void remove_module_kprobe_blacklist(struct module *mod)
2354 unsigned long start, end;
2357 if (mod->kprobe_blacklist) {
2358 for (i = 0; i < mod->num_kprobe_blacklist; i++)
2359 kprobe_remove_ksym_blacklist(mod->kprobe_blacklist[i]);
2362 start = (unsigned long)mod->kprobes_text_start;
2364 end = start + mod->kprobes_text_size;
2365 kprobe_remove_area_blacklist(start, end);
2368 start = (unsigned long)mod->noinstr_text_start;
2370 end = start + mod->noinstr_text_size;
2371 kprobe_remove_area_blacklist(start, end);
2375 /* Module notifier call back, checking kprobes on the module */
2376 static int kprobes_module_callback(struct notifier_block *nb,
2377 unsigned long val, void *data)
2379 struct module *mod = data;
2380 struct hlist_head *head;
2383 int checkcore = (val == MODULE_STATE_GOING);
2385 if (val == MODULE_STATE_COMING) {
2386 mutex_lock(&kprobe_mutex);
2387 add_module_kprobe_blacklist(mod);
2388 mutex_unlock(&kprobe_mutex);
2390 if (val != MODULE_STATE_GOING && val != MODULE_STATE_LIVE)
2394 * When MODULE_STATE_GOING was notified, both of module .text and
2395 * .init.text sections would be freed. When MODULE_STATE_LIVE was
2396 * notified, only .init.text section would be freed. We need to
2397 * disable kprobes which have been inserted in the sections.
2399 mutex_lock(&kprobe_mutex);
2400 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2401 head = &kprobe_table[i];
2402 hlist_for_each_entry(p, head, hlist)
2403 if (within_module_init((unsigned long)p->addr, mod) ||
2405 within_module_core((unsigned long)p->addr, mod))) {
2407 * The vaddr this probe is installed will soon
2408 * be vfreed buy not synced to disk. Hence,
2409 * disarming the breakpoint isn't needed.
2411 * Note, this will also move any optimized probes
2412 * that are pending to be removed from their
2413 * corresponding lists to the freeing_list and
2414 * will not be touched by the delayed
2415 * kprobe_optimizer work handler.
2420 if (val == MODULE_STATE_GOING)
2421 remove_module_kprobe_blacklist(mod);
2422 mutex_unlock(&kprobe_mutex);
2426 static struct notifier_block kprobe_module_nb = {
2427 .notifier_call = kprobes_module_callback,
2431 /* Markers of _kprobe_blacklist section */
2432 extern unsigned long __start_kprobe_blacklist[];
2433 extern unsigned long __stop_kprobe_blacklist[];
2435 void kprobe_free_init_mem(void)
2437 void *start = (void *)(&__init_begin);
2438 void *end = (void *)(&__init_end);
2439 struct hlist_head *head;
2443 mutex_lock(&kprobe_mutex);
2445 /* Kill all kprobes on initmem */
2446 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2447 head = &kprobe_table[i];
2448 hlist_for_each_entry(p, head, hlist) {
2449 if (start <= (void *)p->addr && (void *)p->addr < end)
2454 mutex_unlock(&kprobe_mutex);
2457 static int __init init_kprobes(void)
2461 /* FIXME allocate the probe table, currently defined statically */
2462 /* initialize all list heads */
2463 for (i = 0; i < KPROBE_TABLE_SIZE; i++)
2464 INIT_HLIST_HEAD(&kprobe_table[i]);
2466 err = populate_kprobe_blacklist(__start_kprobe_blacklist,
2467 __stop_kprobe_blacklist);
2469 pr_err("kprobes: failed to populate blacklist: %d\n", err);
2470 pr_err("Please take care of using kprobes.\n");
2473 if (kretprobe_blacklist_size) {
2474 /* lookup the function address from its name */
2475 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
2476 kretprobe_blacklist[i].addr =
2477 kprobe_lookup_name(kretprobe_blacklist[i].name, 0);
2478 if (!kretprobe_blacklist[i].addr)
2479 printk("kretprobe: lookup failed: %s\n",
2480 kretprobe_blacklist[i].name);
2484 /* By default, kprobes are armed */
2485 kprobes_all_disarmed = false;
2487 #if defined(CONFIG_OPTPROBES) && defined(__ARCH_WANT_KPROBES_INSN_SLOT)
2488 /* Init kprobe_optinsn_slots for allocation */
2489 kprobe_optinsn_slots.insn_size = MAX_OPTINSN_SIZE;
2492 err = arch_init_kprobes();
2494 err = register_die_notifier(&kprobe_exceptions_nb);
2496 err = register_module_notifier(&kprobe_module_nb);
2498 kprobes_initialized = (err == 0);
2504 early_initcall(init_kprobes);
2506 #if defined(CONFIG_OPTPROBES)
2507 static int __init init_optprobes(void)
2510 * Enable kprobe optimization - this kicks the optimizer which
2511 * depends on synchronize_rcu_tasks() and ksoftirqd, that is
2512 * not spawned in early initcall. So delay the optimization.
2514 optimize_all_kprobes();
2518 subsys_initcall(init_optprobes);
2521 #ifdef CONFIG_DEBUG_FS
2522 static void report_probe(struct seq_file *pi, struct kprobe *p,
2523 const char *sym, int offset, char *modname, struct kprobe *pp)
2526 void *addr = p->addr;
2528 if (p->pre_handler == pre_handler_kretprobe)
2533 if (!kallsyms_show_value(pi->file->f_cred))
2537 seq_printf(pi, "%px %s %s+0x%x %s ",
2538 addr, kprobe_type, sym, offset,
2539 (modname ? modname : " "));
2540 else /* try to use %pS */
2541 seq_printf(pi, "%px %s %pS ",
2542 addr, kprobe_type, p->addr);
2546 seq_printf(pi, "%s%s%s%s\n",
2547 (kprobe_gone(p) ? "[GONE]" : ""),
2548 ((kprobe_disabled(p) && !kprobe_gone(p)) ? "[DISABLED]" : ""),
2549 (kprobe_optimized(pp) ? "[OPTIMIZED]" : ""),
2550 (kprobe_ftrace(pp) ? "[FTRACE]" : ""));
2553 static void *kprobe_seq_start(struct seq_file *f, loff_t *pos)
2555 return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL;
2558 static void *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos)
2561 if (*pos >= KPROBE_TABLE_SIZE)
2566 static void kprobe_seq_stop(struct seq_file *f, void *v)
2571 static int show_kprobe_addr(struct seq_file *pi, void *v)
2573 struct hlist_head *head;
2574 struct kprobe *p, *kp;
2575 const char *sym = NULL;
2576 unsigned int i = *(loff_t *) v;
2577 unsigned long offset = 0;
2578 char *modname, namebuf[KSYM_NAME_LEN];
2580 head = &kprobe_table[i];
2582 hlist_for_each_entry_rcu(p, head, hlist) {
2583 sym = kallsyms_lookup((unsigned long)p->addr, NULL,
2584 &offset, &modname, namebuf);
2585 if (kprobe_aggrprobe(p)) {
2586 list_for_each_entry_rcu(kp, &p->list, list)
2587 report_probe(pi, kp, sym, offset, modname, p);
2589 report_probe(pi, p, sym, offset, modname, NULL);
2595 static const struct seq_operations kprobes_sops = {
2596 .start = kprobe_seq_start,
2597 .next = kprobe_seq_next,
2598 .stop = kprobe_seq_stop,
2599 .show = show_kprobe_addr
2602 DEFINE_SEQ_ATTRIBUTE(kprobes);
2604 /* kprobes/blacklist -- shows which functions can not be probed */
2605 static void *kprobe_blacklist_seq_start(struct seq_file *m, loff_t *pos)
2607 mutex_lock(&kprobe_mutex);
2608 return seq_list_start(&kprobe_blacklist, *pos);
2611 static void *kprobe_blacklist_seq_next(struct seq_file *m, void *v, loff_t *pos)
2613 return seq_list_next(v, &kprobe_blacklist, pos);
2616 static int kprobe_blacklist_seq_show(struct seq_file *m, void *v)
2618 struct kprobe_blacklist_entry *ent =
2619 list_entry(v, struct kprobe_blacklist_entry, list);
2622 * If /proc/kallsyms is not showing kernel address, we won't
2623 * show them here either.
2625 if (!kallsyms_show_value(m->file->f_cred))
2626 seq_printf(m, "0x%px-0x%px\t%ps\n", NULL, NULL,
2627 (void *)ent->start_addr);
2629 seq_printf(m, "0x%px-0x%px\t%ps\n", (void *)ent->start_addr,
2630 (void *)ent->end_addr, (void *)ent->start_addr);
2634 static void kprobe_blacklist_seq_stop(struct seq_file *f, void *v)
2636 mutex_unlock(&kprobe_mutex);
2639 static const struct seq_operations kprobe_blacklist_sops = {
2640 .start = kprobe_blacklist_seq_start,
2641 .next = kprobe_blacklist_seq_next,
2642 .stop = kprobe_blacklist_seq_stop,
2643 .show = kprobe_blacklist_seq_show,
2645 DEFINE_SEQ_ATTRIBUTE(kprobe_blacklist);
2647 static int arm_all_kprobes(void)
2649 struct hlist_head *head;
2651 unsigned int i, total = 0, errors = 0;
2654 mutex_lock(&kprobe_mutex);
2656 /* If kprobes are armed, just return */
2657 if (!kprobes_all_disarmed)
2658 goto already_enabled;
2661 * optimize_kprobe() called by arm_kprobe() checks
2662 * kprobes_all_disarmed, so set kprobes_all_disarmed before
2665 kprobes_all_disarmed = false;
2666 /* Arming kprobes doesn't optimize kprobe itself */
2667 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2668 head = &kprobe_table[i];
2669 /* Arm all kprobes on a best-effort basis */
2670 hlist_for_each_entry(p, head, hlist) {
2671 if (!kprobe_disabled(p)) {
2672 err = arm_kprobe(p);
2683 pr_warn("Kprobes globally enabled, but failed to arm %d out of %d probes\n",
2686 pr_info("Kprobes globally enabled\n");
2689 mutex_unlock(&kprobe_mutex);
2693 static int disarm_all_kprobes(void)
2695 struct hlist_head *head;
2697 unsigned int i, total = 0, errors = 0;
2700 mutex_lock(&kprobe_mutex);
2702 /* If kprobes are already disarmed, just return */
2703 if (kprobes_all_disarmed) {
2704 mutex_unlock(&kprobe_mutex);
2708 kprobes_all_disarmed = true;
2710 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2711 head = &kprobe_table[i];
2712 /* Disarm all kprobes on a best-effort basis */
2713 hlist_for_each_entry(p, head, hlist) {
2714 if (!arch_trampoline_kprobe(p) && !kprobe_disabled(p)) {
2715 err = disarm_kprobe(p, false);
2726 pr_warn("Kprobes globally disabled, but failed to disarm %d out of %d probes\n",
2729 pr_info("Kprobes globally disabled\n");
2731 mutex_unlock(&kprobe_mutex);
2733 /* Wait for disarming all kprobes by optimizer */
2734 wait_for_kprobe_optimizer();
2740 * XXX: The debugfs bool file interface doesn't allow for callbacks
2741 * when the bool state is switched. We can reuse that facility when
2744 static ssize_t read_enabled_file_bool(struct file *file,
2745 char __user *user_buf, size_t count, loff_t *ppos)
2749 if (!kprobes_all_disarmed)
2755 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
2758 static ssize_t write_enabled_file_bool(struct file *file,
2759 const char __user *user_buf, size_t count, loff_t *ppos)
2765 buf_size = min(count, (sizeof(buf)-1));
2766 if (copy_from_user(buf, user_buf, buf_size))
2769 buf[buf_size] = '\0';
2774 ret = arm_all_kprobes();
2779 ret = disarm_all_kprobes();
2791 static const struct file_operations fops_kp = {
2792 .read = read_enabled_file_bool,
2793 .write = write_enabled_file_bool,
2794 .llseek = default_llseek,
2797 static int __init debugfs_kprobe_init(void)
2800 unsigned int value = 1;
2802 dir = debugfs_create_dir("kprobes", NULL);
2804 debugfs_create_file("list", 0400, dir, NULL, &kprobes_fops);
2806 debugfs_create_file("enabled", 0600, dir, &value, &fops_kp);
2808 debugfs_create_file("blacklist", 0400, dir, NULL,
2809 &kprobe_blacklist_fops);
2814 late_initcall(debugfs_kprobe_init);
2815 #endif /* CONFIG_DEBUG_FS */