1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Kernel Probes (KProbes)
6 * Copyright (C) IBM Corporation, 2002, 2004
9 * Probes initial implementation (includes suggestions from
12 * hlists and exceptions notifier as suggested by Andi Kleen.
14 * interface to access function arguments.
16 * exceptions notifier to be first on the priority list.
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>
39 #include <linux/static_call.h>
41 #include <asm/sections.h>
42 #include <asm/cacheflush.h>
43 #include <asm/errno.h>
44 #include <linux/uaccess.h>
46 #define KPROBE_HASH_BITS 6
47 #define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
50 static int kprobes_initialized;
51 /* kprobe_table can be accessed by
52 * - Normal hlist traversal and RCU add/del under kprobe_mutex is held.
54 * - RCU hlist traversal under disabling preempt (breakpoint handlers)
56 static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
57 static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
59 /* NOTE: change this value only with kprobe_mutex held */
60 static bool kprobes_all_disarmed;
62 /* This protects kprobe_table and optimizing_list */
63 static DEFINE_MUTEX(kprobe_mutex);
64 static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL;
66 raw_spinlock_t lock ____cacheline_aligned_in_smp;
67 } kretprobe_table_locks[KPROBE_TABLE_SIZE];
69 kprobe_opcode_t * __weak kprobe_lookup_name(const char *name,
70 unsigned int __unused)
72 return ((kprobe_opcode_t *)(kallsyms_lookup_name(name)));
75 static raw_spinlock_t *kretprobe_table_lock_ptr(unsigned long hash)
77 return &(kretprobe_table_locks[hash].lock);
80 /* Blacklist -- list of struct kprobe_blacklist_entry */
81 static LIST_HEAD(kprobe_blacklist);
83 #ifdef __ARCH_WANT_KPROBES_INSN_SLOT
85 * kprobe->ainsn.insn points to the copy of the instruction to be
86 * single-stepped. x86_64, POWER4 and above have no-exec support and
87 * stepping on the instruction on a vmalloced/kmalloced/data page
88 * is a recipe for disaster
90 struct kprobe_insn_page {
91 struct list_head list;
92 kprobe_opcode_t *insns; /* Page of instruction slots */
93 struct kprobe_insn_cache *cache;
99 #define KPROBE_INSN_PAGE_SIZE(slots) \
100 (offsetof(struct kprobe_insn_page, slot_used) + \
101 (sizeof(char) * (slots)))
103 static int slots_per_page(struct kprobe_insn_cache *c)
105 return PAGE_SIZE/(c->insn_size * sizeof(kprobe_opcode_t));
108 enum kprobe_slot_state {
114 void __weak *alloc_insn_page(void)
116 return module_alloc(PAGE_SIZE);
119 void __weak free_insn_page(void *page)
121 module_memfree(page);
124 struct kprobe_insn_cache kprobe_insn_slots = {
125 .mutex = __MUTEX_INITIALIZER(kprobe_insn_slots.mutex),
126 .alloc = alloc_insn_page,
127 .free = free_insn_page,
128 .sym = KPROBE_INSN_PAGE_SYM,
129 .pages = LIST_HEAD_INIT(kprobe_insn_slots.pages),
130 .insn_size = MAX_INSN_SIZE,
133 static int collect_garbage_slots(struct kprobe_insn_cache *c);
136 * __get_insn_slot() - Find a slot on an executable page for an instruction.
137 * We allocate an executable page if there's no room on existing ones.
139 kprobe_opcode_t *__get_insn_slot(struct kprobe_insn_cache *c)
141 struct kprobe_insn_page *kip;
142 kprobe_opcode_t *slot = NULL;
144 /* Since the slot array is not protected by rcu, we need a mutex */
145 mutex_lock(&c->mutex);
148 list_for_each_entry_rcu(kip, &c->pages, list) {
149 if (kip->nused < slots_per_page(c)) {
151 for (i = 0; i < slots_per_page(c); i++) {
152 if (kip->slot_used[i] == SLOT_CLEAN) {
153 kip->slot_used[i] = SLOT_USED;
155 slot = kip->insns + (i * c->insn_size);
160 /* kip->nused is broken. Fix it. */
161 kip->nused = slots_per_page(c);
167 /* If there are any garbage slots, collect it and try again. */
168 if (c->nr_garbage && collect_garbage_slots(c) == 0)
171 /* All out of space. Need to allocate a new page. */
172 kip = kmalloc(KPROBE_INSN_PAGE_SIZE(slots_per_page(c)), GFP_KERNEL);
177 * Use module_alloc so this page is within +/- 2GB of where the
178 * kernel image and loaded module images reside. This is required
179 * so x86_64 can correctly handle the %rip-relative fixups.
181 kip->insns = c->alloc();
186 INIT_LIST_HEAD(&kip->list);
187 memset(kip->slot_used, SLOT_CLEAN, slots_per_page(c));
188 kip->slot_used[0] = SLOT_USED;
192 list_add_rcu(&kip->list, &c->pages);
195 /* Record the perf ksymbol register event after adding the page */
196 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL, (unsigned long)kip->insns,
197 PAGE_SIZE, false, c->sym);
199 mutex_unlock(&c->mutex);
203 /* Return 1 if all garbages are collected, otherwise 0. */
204 static int collect_one_slot(struct kprobe_insn_page *kip, int idx)
206 kip->slot_used[idx] = SLOT_CLEAN;
208 if (kip->nused == 0) {
210 * Page is no longer in use. Free it unless
211 * it's the last one. We keep the last one
212 * so as not to have to set it up again the
213 * next time somebody inserts a probe.
215 if (!list_is_singular(&kip->list)) {
217 * Record perf ksymbol unregister event before removing
220 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_OOL,
221 (unsigned long)kip->insns, PAGE_SIZE, true,
223 list_del_rcu(&kip->list);
225 kip->cache->free(kip->insns);
233 static int collect_garbage_slots(struct kprobe_insn_cache *c)
235 struct kprobe_insn_page *kip, *next;
237 /* Ensure no-one is interrupted on the garbages */
240 list_for_each_entry_safe(kip, next, &c->pages, list) {
242 if (kip->ngarbage == 0)
244 kip->ngarbage = 0; /* we will collect all garbages */
245 for (i = 0; i < slots_per_page(c); i++) {
246 if (kip->slot_used[i] == SLOT_DIRTY && collect_one_slot(kip, i))
254 void __free_insn_slot(struct kprobe_insn_cache *c,
255 kprobe_opcode_t *slot, int dirty)
257 struct kprobe_insn_page *kip;
260 mutex_lock(&c->mutex);
262 list_for_each_entry_rcu(kip, &c->pages, list) {
263 idx = ((long)slot - (long)kip->insns) /
264 (c->insn_size * sizeof(kprobe_opcode_t));
265 if (idx >= 0 && idx < slots_per_page(c))
268 /* Could not find this slot. */
273 /* Mark and sweep: this may sleep */
275 /* Check double free */
276 WARN_ON(kip->slot_used[idx] != SLOT_USED);
278 kip->slot_used[idx] = SLOT_DIRTY;
280 if (++c->nr_garbage > slots_per_page(c))
281 collect_garbage_slots(c);
283 collect_one_slot(kip, idx);
286 mutex_unlock(&c->mutex);
290 * Check given address is on the page of kprobe instruction slots.
291 * This will be used for checking whether the address on a stack
292 * is on a text area or not.
294 bool __is_insn_slot_addr(struct kprobe_insn_cache *c, unsigned long addr)
296 struct kprobe_insn_page *kip;
300 list_for_each_entry_rcu(kip, &c->pages, list) {
301 if (addr >= (unsigned long)kip->insns &&
302 addr < (unsigned long)kip->insns + PAGE_SIZE) {
312 int kprobe_cache_get_kallsym(struct kprobe_insn_cache *c, unsigned int *symnum,
313 unsigned long *value, char *type, char *sym)
315 struct kprobe_insn_page *kip;
319 list_for_each_entry_rcu(kip, &c->pages, list) {
322 strlcpy(sym, c->sym, KSYM_NAME_LEN);
324 *value = (unsigned long)kip->insns;
333 #ifdef CONFIG_OPTPROBES
334 /* For optimized_kprobe buffer */
335 struct kprobe_insn_cache kprobe_optinsn_slots = {
336 .mutex = __MUTEX_INITIALIZER(kprobe_optinsn_slots.mutex),
337 .alloc = alloc_insn_page,
338 .free = free_insn_page,
339 .sym = KPROBE_OPTINSN_PAGE_SYM,
340 .pages = LIST_HEAD_INIT(kprobe_optinsn_slots.pages),
341 /* .insn_size is initialized later */
347 /* We have preemption disabled.. so it is safe to use __ versions */
348 static inline void set_kprobe_instance(struct kprobe *kp)
350 __this_cpu_write(kprobe_instance, kp);
353 static inline void reset_kprobe_instance(void)
355 __this_cpu_write(kprobe_instance, NULL);
359 * This routine is called either:
360 * - under the kprobe_mutex - during kprobe_[un]register()
362 * - with preemption disabled - from arch/xxx/kernel/kprobes.c
364 struct kprobe *get_kprobe(void *addr)
366 struct hlist_head *head;
369 head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
370 hlist_for_each_entry_rcu(p, head, hlist,
371 lockdep_is_held(&kprobe_mutex)) {
378 NOKPROBE_SYMBOL(get_kprobe);
380 static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs);
382 /* Return true if the kprobe is an aggregator */
383 static inline int kprobe_aggrprobe(struct kprobe *p)
385 return p->pre_handler == aggr_pre_handler;
388 /* Return true(!0) if the kprobe is unused */
389 static inline int kprobe_unused(struct kprobe *p)
391 return kprobe_aggrprobe(p) && kprobe_disabled(p) &&
392 list_empty(&p->list);
396 * Keep all fields in the kprobe consistent
398 static inline void copy_kprobe(struct kprobe *ap, struct kprobe *p)
400 memcpy(&p->opcode, &ap->opcode, sizeof(kprobe_opcode_t));
401 memcpy(&p->ainsn, &ap->ainsn, sizeof(struct arch_specific_insn));
404 #ifdef CONFIG_OPTPROBES
405 /* NOTE: change this value only with kprobe_mutex held */
406 static bool kprobes_allow_optimization;
409 * Call all pre_handler on the list, but ignores its return value.
410 * This must be called from arch-dep optimized caller.
412 void opt_pre_handler(struct kprobe *p, struct pt_regs *regs)
416 list_for_each_entry_rcu(kp, &p->list, list) {
417 if (kp->pre_handler && likely(!kprobe_disabled(kp))) {
418 set_kprobe_instance(kp);
419 kp->pre_handler(kp, regs);
421 reset_kprobe_instance();
424 NOKPROBE_SYMBOL(opt_pre_handler);
426 /* Free optimized instructions and optimized_kprobe */
427 static void free_aggr_kprobe(struct kprobe *p)
429 struct optimized_kprobe *op;
431 op = container_of(p, struct optimized_kprobe, kp);
432 arch_remove_optimized_kprobe(op);
433 arch_remove_kprobe(p);
437 /* Return true(!0) if the kprobe is ready for optimization. */
438 static inline int kprobe_optready(struct kprobe *p)
440 struct optimized_kprobe *op;
442 if (kprobe_aggrprobe(p)) {
443 op = container_of(p, struct optimized_kprobe, kp);
444 return arch_prepared_optinsn(&op->optinsn);
450 /* Return true(!0) if the kprobe is disarmed. Note: p must be on hash list */
451 static inline int kprobe_disarmed(struct kprobe *p)
453 struct optimized_kprobe *op;
455 /* If kprobe is not aggr/opt probe, just return kprobe is disabled */
456 if (!kprobe_aggrprobe(p))
457 return kprobe_disabled(p);
459 op = container_of(p, struct optimized_kprobe, kp);
461 return kprobe_disabled(p) && list_empty(&op->list);
464 /* Return true(!0) if the probe is queued on (un)optimizing lists */
465 static int kprobe_queued(struct kprobe *p)
467 struct optimized_kprobe *op;
469 if (kprobe_aggrprobe(p)) {
470 op = container_of(p, struct optimized_kprobe, kp);
471 if (!list_empty(&op->list))
478 * Return an optimized kprobe whose optimizing code replaces
479 * instructions including addr (exclude breakpoint).
481 static struct kprobe *get_optimized_kprobe(unsigned long addr)
484 struct kprobe *p = NULL;
485 struct optimized_kprobe *op;
487 /* Don't check i == 0, since that is a breakpoint case. */
488 for (i = 1; !p && i < MAX_OPTIMIZED_LENGTH; i++)
489 p = get_kprobe((void *)(addr - i));
491 if (p && kprobe_optready(p)) {
492 op = container_of(p, struct optimized_kprobe, kp);
493 if (arch_within_optimized_kprobe(op, addr))
500 /* Optimization staging list, protected by kprobe_mutex */
501 static LIST_HEAD(optimizing_list);
502 static LIST_HEAD(unoptimizing_list);
503 static LIST_HEAD(freeing_list);
505 static void kprobe_optimizer(struct work_struct *work);
506 static DECLARE_DELAYED_WORK(optimizing_work, kprobe_optimizer);
507 #define OPTIMIZE_DELAY 5
510 * Optimize (replace a breakpoint with a jump) kprobes listed on
513 static void do_optimize_kprobes(void)
515 lockdep_assert_held(&text_mutex);
517 * The optimization/unoptimization refers online_cpus via
518 * stop_machine() and cpu-hotplug modifies online_cpus.
519 * And same time, text_mutex will be held in cpu-hotplug and here.
520 * This combination can cause a deadlock (cpu-hotplug try to lock
521 * text_mutex but stop_machine can not be done because online_cpus
523 * To avoid this deadlock, caller must have locked cpu hotplug
524 * for preventing cpu-hotplug outside of text_mutex locking.
526 lockdep_assert_cpus_held();
528 /* Optimization never be done when disarmed */
529 if (kprobes_all_disarmed || !kprobes_allow_optimization ||
530 list_empty(&optimizing_list))
533 arch_optimize_kprobes(&optimizing_list);
537 * Unoptimize (replace a jump with a breakpoint and remove the breakpoint
538 * if need) kprobes listed on unoptimizing_list.
540 static void do_unoptimize_kprobes(void)
542 struct optimized_kprobe *op, *tmp;
544 lockdep_assert_held(&text_mutex);
545 /* See comment in do_optimize_kprobes() */
546 lockdep_assert_cpus_held();
548 /* Unoptimization must be done anytime */
549 if (list_empty(&unoptimizing_list))
552 arch_unoptimize_kprobes(&unoptimizing_list, &freeing_list);
553 /* Loop free_list for disarming */
554 list_for_each_entry_safe(op, tmp, &freeing_list, list) {
555 /* Switching from detour code to origin */
556 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
557 /* Disarm probes if marked disabled */
558 if (kprobe_disabled(&op->kp))
559 arch_disarm_kprobe(&op->kp);
560 if (kprobe_unused(&op->kp)) {
562 * Remove unused probes from hash list. After waiting
563 * for synchronization, these probes are reclaimed.
564 * (reclaiming is done by do_free_cleaned_kprobes.)
566 hlist_del_rcu(&op->kp.hlist);
568 list_del_init(&op->list);
572 /* Reclaim all kprobes on the free_list */
573 static void do_free_cleaned_kprobes(void)
575 struct optimized_kprobe *op, *tmp;
577 list_for_each_entry_safe(op, tmp, &freeing_list, list) {
578 list_del_init(&op->list);
579 if (WARN_ON_ONCE(!kprobe_unused(&op->kp))) {
581 * This must not happen, but if there is a kprobe
582 * still in use, keep it on kprobes hash list.
586 free_aggr_kprobe(&op->kp);
590 /* Start optimizer after OPTIMIZE_DELAY passed */
591 static void kick_kprobe_optimizer(void)
593 schedule_delayed_work(&optimizing_work, OPTIMIZE_DELAY);
596 /* Kprobe jump optimizer */
597 static void kprobe_optimizer(struct work_struct *work)
599 mutex_lock(&kprobe_mutex);
601 mutex_lock(&text_mutex);
604 * Step 1: Unoptimize kprobes and collect cleaned (unused and disarmed)
605 * kprobes before waiting for quiesence period.
607 do_unoptimize_kprobes();
610 * Step 2: Wait for quiesence period to ensure all potentially
611 * preempted tasks to have normally scheduled. Because optprobe
612 * may modify multiple instructions, there is a chance that Nth
613 * instruction is preempted. In that case, such tasks can return
614 * to 2nd-Nth byte of jump instruction. This wait is for avoiding it.
615 * Note that on non-preemptive kernel, this is transparently converted
616 * to synchronoze_sched() to wait for all interrupts to have completed.
618 synchronize_rcu_tasks();
620 /* Step 3: Optimize kprobes after quiesence period */
621 do_optimize_kprobes();
623 /* Step 4: Free cleaned kprobes after quiesence period */
624 do_free_cleaned_kprobes();
626 mutex_unlock(&text_mutex);
629 /* Step 5: Kick optimizer again if needed */
630 if (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list))
631 kick_kprobe_optimizer();
633 mutex_unlock(&kprobe_mutex);
636 /* Wait for completing optimization and unoptimization */
637 void wait_for_kprobe_optimizer(void)
639 mutex_lock(&kprobe_mutex);
641 while (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list)) {
642 mutex_unlock(&kprobe_mutex);
644 /* this will also make optimizing_work execute immmediately */
645 flush_delayed_work(&optimizing_work);
646 /* @optimizing_work might not have been queued yet, relax */
649 mutex_lock(&kprobe_mutex);
652 mutex_unlock(&kprobe_mutex);
655 static bool optprobe_queued_unopt(struct optimized_kprobe *op)
657 struct optimized_kprobe *_op;
659 list_for_each_entry(_op, &unoptimizing_list, list) {
667 /* Optimize kprobe if p is ready to be optimized */
668 static void optimize_kprobe(struct kprobe *p)
670 struct optimized_kprobe *op;
672 /* Check if the kprobe is disabled or not ready for optimization. */
673 if (!kprobe_optready(p) || !kprobes_allow_optimization ||
674 (kprobe_disabled(p) || kprobes_all_disarmed))
677 /* kprobes with post_handler can not be optimized */
681 op = container_of(p, struct optimized_kprobe, kp);
683 /* Check there is no other kprobes at the optimized instructions */
684 if (arch_check_optimized_kprobe(op) < 0)
687 /* Check if it is already optimized. */
688 if (op->kp.flags & KPROBE_FLAG_OPTIMIZED) {
689 if (optprobe_queued_unopt(op)) {
690 /* This is under unoptimizing. Just dequeue the probe */
691 list_del_init(&op->list);
695 op->kp.flags |= KPROBE_FLAG_OPTIMIZED;
697 /* On unoptimizing/optimizing_list, op must have OPTIMIZED flag */
698 if (WARN_ON_ONCE(!list_empty(&op->list)))
701 list_add(&op->list, &optimizing_list);
702 kick_kprobe_optimizer();
705 /* Short cut to direct unoptimizing */
706 static void force_unoptimize_kprobe(struct optimized_kprobe *op)
708 lockdep_assert_cpus_held();
709 arch_unoptimize_kprobe(op);
710 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
713 /* Unoptimize a kprobe if p is optimized */
714 static void unoptimize_kprobe(struct kprobe *p, bool force)
716 struct optimized_kprobe *op;
718 if (!kprobe_aggrprobe(p) || kprobe_disarmed(p))
719 return; /* This is not an optprobe nor optimized */
721 op = container_of(p, struct optimized_kprobe, kp);
722 if (!kprobe_optimized(p))
725 if (!list_empty(&op->list)) {
726 if (optprobe_queued_unopt(op)) {
727 /* Queued in unoptimizing queue */
730 * Forcibly unoptimize the kprobe here, and queue it
731 * in the freeing list for release afterwards.
733 force_unoptimize_kprobe(op);
734 list_move(&op->list, &freeing_list);
737 /* Dequeue from the optimizing queue */
738 list_del_init(&op->list);
739 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
744 /* Optimized kprobe case */
746 /* Forcibly update the code: this is a special case */
747 force_unoptimize_kprobe(op);
749 list_add(&op->list, &unoptimizing_list);
750 kick_kprobe_optimizer();
754 /* Cancel unoptimizing for reusing */
755 static int reuse_unused_kprobe(struct kprobe *ap)
757 struct optimized_kprobe *op;
760 * Unused kprobe MUST be on the way of delayed unoptimizing (means
761 * there is still a relative jump) and disabled.
763 op = container_of(ap, struct optimized_kprobe, kp);
764 WARN_ON_ONCE(list_empty(&op->list));
765 /* Enable the probe again */
766 ap->flags &= ~KPROBE_FLAG_DISABLED;
767 /* Optimize it again (remove from op->list) */
768 if (!kprobe_optready(ap))
775 /* Remove optimized instructions */
776 static void kill_optimized_kprobe(struct kprobe *p)
778 struct optimized_kprobe *op;
780 op = container_of(p, struct optimized_kprobe, kp);
781 if (!list_empty(&op->list))
782 /* Dequeue from the (un)optimization queue */
783 list_del_init(&op->list);
784 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
786 if (kprobe_unused(p)) {
787 /* Enqueue if it is unused */
788 list_add(&op->list, &freeing_list);
790 * Remove unused probes from the hash list. After waiting
791 * for synchronization, this probe is reclaimed.
792 * (reclaiming is done by do_free_cleaned_kprobes().)
794 hlist_del_rcu(&op->kp.hlist);
797 /* Don't touch the code, because it is already freed. */
798 arch_remove_optimized_kprobe(op);
802 void __prepare_optimized_kprobe(struct optimized_kprobe *op, struct kprobe *p)
804 if (!kprobe_ftrace(p))
805 arch_prepare_optimized_kprobe(op, p);
808 /* Try to prepare optimized instructions */
809 static void prepare_optimized_kprobe(struct kprobe *p)
811 struct optimized_kprobe *op;
813 op = container_of(p, struct optimized_kprobe, kp);
814 __prepare_optimized_kprobe(op, p);
817 /* Allocate new optimized_kprobe and try to prepare optimized instructions */
818 static struct kprobe *alloc_aggr_kprobe(struct kprobe *p)
820 struct optimized_kprobe *op;
822 op = kzalloc(sizeof(struct optimized_kprobe), GFP_KERNEL);
826 INIT_LIST_HEAD(&op->list);
827 op->kp.addr = p->addr;
828 __prepare_optimized_kprobe(op, p);
833 static void init_aggr_kprobe(struct kprobe *ap, struct kprobe *p);
836 * Prepare an optimized_kprobe and optimize it
837 * NOTE: p must be a normal registered kprobe
839 static void try_to_optimize_kprobe(struct kprobe *p)
842 struct optimized_kprobe *op;
844 /* Impossible to optimize ftrace-based kprobe */
845 if (kprobe_ftrace(p))
848 /* For preparing optimization, jump_label_text_reserved() is called */
851 mutex_lock(&text_mutex);
853 ap = alloc_aggr_kprobe(p);
857 op = container_of(ap, struct optimized_kprobe, kp);
858 if (!arch_prepared_optinsn(&op->optinsn)) {
859 /* If failed to setup optimizing, fallback to kprobe */
860 arch_remove_optimized_kprobe(op);
865 init_aggr_kprobe(ap, p);
866 optimize_kprobe(ap); /* This just kicks optimizer thread */
869 mutex_unlock(&text_mutex);
875 static void optimize_all_kprobes(void)
877 struct hlist_head *head;
881 mutex_lock(&kprobe_mutex);
882 /* If optimization is already allowed, just return */
883 if (kprobes_allow_optimization)
887 kprobes_allow_optimization = true;
888 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
889 head = &kprobe_table[i];
890 hlist_for_each_entry(p, head, hlist)
891 if (!kprobe_disabled(p))
895 printk(KERN_INFO "Kprobes globally optimized\n");
897 mutex_unlock(&kprobe_mutex);
900 static void unoptimize_all_kprobes(void)
902 struct hlist_head *head;
906 mutex_lock(&kprobe_mutex);
907 /* If optimization is already prohibited, just return */
908 if (!kprobes_allow_optimization) {
909 mutex_unlock(&kprobe_mutex);
914 kprobes_allow_optimization = false;
915 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
916 head = &kprobe_table[i];
917 hlist_for_each_entry(p, head, hlist) {
918 if (!kprobe_disabled(p))
919 unoptimize_kprobe(p, false);
923 mutex_unlock(&kprobe_mutex);
925 /* Wait for unoptimizing completion */
926 wait_for_kprobe_optimizer();
927 printk(KERN_INFO "Kprobes globally unoptimized\n");
930 static DEFINE_MUTEX(kprobe_sysctl_mutex);
931 int sysctl_kprobes_optimization;
932 int proc_kprobes_optimization_handler(struct ctl_table *table, int write,
933 void *buffer, size_t *length,
938 mutex_lock(&kprobe_sysctl_mutex);
939 sysctl_kprobes_optimization = kprobes_allow_optimization ? 1 : 0;
940 ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
942 if (sysctl_kprobes_optimization)
943 optimize_all_kprobes();
945 unoptimize_all_kprobes();
946 mutex_unlock(&kprobe_sysctl_mutex);
950 #endif /* CONFIG_SYSCTL */
952 /* Put a breakpoint for a probe. Must be called with text_mutex locked */
953 static void __arm_kprobe(struct kprobe *p)
957 /* Check collision with other optimized kprobes */
958 _p = get_optimized_kprobe((unsigned long)p->addr);
960 /* Fallback to unoptimized kprobe */
961 unoptimize_kprobe(_p, true);
964 optimize_kprobe(p); /* Try to optimize (add kprobe to a list) */
967 /* Remove the breakpoint of a probe. Must be called with text_mutex locked */
968 static void __disarm_kprobe(struct kprobe *p, bool reopt)
972 /* Try to unoptimize */
973 unoptimize_kprobe(p, kprobes_all_disarmed);
975 if (!kprobe_queued(p)) {
976 arch_disarm_kprobe(p);
977 /* If another kprobe was blocked, optimize it. */
978 _p = get_optimized_kprobe((unsigned long)p->addr);
979 if (unlikely(_p) && reopt)
982 /* TODO: reoptimize others after unoptimized this probe */
985 #else /* !CONFIG_OPTPROBES */
987 #define optimize_kprobe(p) do {} while (0)
988 #define unoptimize_kprobe(p, f) do {} while (0)
989 #define kill_optimized_kprobe(p) do {} while (0)
990 #define prepare_optimized_kprobe(p) do {} while (0)
991 #define try_to_optimize_kprobe(p) do {} while (0)
992 #define __arm_kprobe(p) arch_arm_kprobe(p)
993 #define __disarm_kprobe(p, o) arch_disarm_kprobe(p)
994 #define kprobe_disarmed(p) kprobe_disabled(p)
995 #define wait_for_kprobe_optimizer() do {} while (0)
997 static int reuse_unused_kprobe(struct kprobe *ap)
1000 * If the optimized kprobe is NOT supported, the aggr kprobe is
1001 * released at the same time that the last aggregated kprobe is
1003 * Thus there should be no chance to reuse unused kprobe.
1005 printk(KERN_ERR "Error: There should be no unused kprobe here.\n");
1009 static void free_aggr_kprobe(struct kprobe *p)
1011 arch_remove_kprobe(p);
1015 static struct kprobe *alloc_aggr_kprobe(struct kprobe *p)
1017 return kzalloc(sizeof(struct kprobe), GFP_KERNEL);
1019 #endif /* CONFIG_OPTPROBES */
1021 #ifdef CONFIG_KPROBES_ON_FTRACE
1022 static struct ftrace_ops kprobe_ftrace_ops __read_mostly = {
1023 .func = kprobe_ftrace_handler,
1024 .flags = FTRACE_OPS_FL_SAVE_REGS,
1027 static struct ftrace_ops kprobe_ipmodify_ops __read_mostly = {
1028 .func = kprobe_ftrace_handler,
1029 .flags = FTRACE_OPS_FL_SAVE_REGS | FTRACE_OPS_FL_IPMODIFY,
1032 static int kprobe_ipmodify_enabled;
1033 static int kprobe_ftrace_enabled;
1035 /* Must ensure p->addr is really on ftrace */
1036 static int prepare_kprobe(struct kprobe *p)
1038 if (!kprobe_ftrace(p))
1039 return arch_prepare_kprobe(p);
1041 return arch_prepare_kprobe_ftrace(p);
1044 /* Caller must lock kprobe_mutex */
1045 static int __arm_kprobe_ftrace(struct kprobe *p, struct ftrace_ops *ops,
1050 ret = ftrace_set_filter_ip(ops, (unsigned long)p->addr, 0, 0);
1052 pr_debug("Failed to arm kprobe-ftrace at %pS (%d)\n",
1058 ret = register_ftrace_function(ops);
1060 pr_debug("Failed to init kprobe-ftrace (%d)\n", ret);
1070 * At this point, sinec ops is not registered, we should be sefe from
1071 * registering empty filter.
1073 ftrace_set_filter_ip(ops, (unsigned long)p->addr, 1, 0);
1077 static int arm_kprobe_ftrace(struct kprobe *p)
1079 bool ipmodify = (p->post_handler != NULL);
1081 return __arm_kprobe_ftrace(p,
1082 ipmodify ? &kprobe_ipmodify_ops : &kprobe_ftrace_ops,
1083 ipmodify ? &kprobe_ipmodify_enabled : &kprobe_ftrace_enabled);
1086 /* Caller must lock kprobe_mutex */
1087 static int __disarm_kprobe_ftrace(struct kprobe *p, struct ftrace_ops *ops,
1093 ret = unregister_ftrace_function(ops);
1094 if (WARN(ret < 0, "Failed to unregister kprobe-ftrace (%d)\n", ret))
1100 ret = ftrace_set_filter_ip(ops, (unsigned long)p->addr, 1, 0);
1101 WARN_ONCE(ret < 0, "Failed to disarm kprobe-ftrace at %pS (%d)\n",
1106 static int disarm_kprobe_ftrace(struct kprobe *p)
1108 bool ipmodify = (p->post_handler != NULL);
1110 return __disarm_kprobe_ftrace(p,
1111 ipmodify ? &kprobe_ipmodify_ops : &kprobe_ftrace_ops,
1112 ipmodify ? &kprobe_ipmodify_enabled : &kprobe_ftrace_enabled);
1114 #else /* !CONFIG_KPROBES_ON_FTRACE */
1115 static inline int prepare_kprobe(struct kprobe *p)
1117 return arch_prepare_kprobe(p);
1120 static inline int arm_kprobe_ftrace(struct kprobe *p)
1125 static inline int disarm_kprobe_ftrace(struct kprobe *p)
1131 /* Arm a kprobe with text_mutex */
1132 static int arm_kprobe(struct kprobe *kp)
1134 if (unlikely(kprobe_ftrace(kp)))
1135 return arm_kprobe_ftrace(kp);
1138 mutex_lock(&text_mutex);
1140 mutex_unlock(&text_mutex);
1146 /* Disarm a kprobe with text_mutex */
1147 static int disarm_kprobe(struct kprobe *kp, bool reopt)
1149 if (unlikely(kprobe_ftrace(kp)))
1150 return disarm_kprobe_ftrace(kp);
1153 mutex_lock(&text_mutex);
1154 __disarm_kprobe(kp, reopt);
1155 mutex_unlock(&text_mutex);
1162 * Aggregate handlers for multiple kprobes support - these handlers
1163 * take care of invoking the individual kprobe handlers on p->list
1165 static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
1169 list_for_each_entry_rcu(kp, &p->list, list) {
1170 if (kp->pre_handler && likely(!kprobe_disabled(kp))) {
1171 set_kprobe_instance(kp);
1172 if (kp->pre_handler(kp, regs))
1175 reset_kprobe_instance();
1179 NOKPROBE_SYMBOL(aggr_pre_handler);
1181 static void aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
1182 unsigned long flags)
1186 list_for_each_entry_rcu(kp, &p->list, list) {
1187 if (kp->post_handler && likely(!kprobe_disabled(kp))) {
1188 set_kprobe_instance(kp);
1189 kp->post_handler(kp, regs, flags);
1190 reset_kprobe_instance();
1194 NOKPROBE_SYMBOL(aggr_post_handler);
1196 static int aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
1199 struct kprobe *cur = __this_cpu_read(kprobe_instance);
1202 * if we faulted "during" the execution of a user specified
1203 * probe handler, invoke just that probe's fault handler
1205 if (cur && cur->fault_handler) {
1206 if (cur->fault_handler(cur, regs, trapnr))
1211 NOKPROBE_SYMBOL(aggr_fault_handler);
1213 /* Walks the list and increments nmissed count for multiprobe case */
1214 void kprobes_inc_nmissed_count(struct kprobe *p)
1217 if (!kprobe_aggrprobe(p)) {
1220 list_for_each_entry_rcu(kp, &p->list, list)
1225 NOKPROBE_SYMBOL(kprobes_inc_nmissed_count);
1227 static void recycle_rp_inst(struct kretprobe_instance *ri)
1229 struct kretprobe *rp = ri->rp;
1231 /* remove rp inst off the rprobe_inst_table */
1232 hlist_del(&ri->hlist);
1233 INIT_HLIST_NODE(&ri->hlist);
1235 raw_spin_lock(&rp->lock);
1236 hlist_add_head(&ri->hlist, &rp->free_instances);
1237 raw_spin_unlock(&rp->lock);
1241 NOKPROBE_SYMBOL(recycle_rp_inst);
1243 static void kretprobe_hash_lock(struct task_struct *tsk,
1244 struct hlist_head **head, unsigned long *flags)
1245 __acquires(hlist_lock)
1247 unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS);
1248 raw_spinlock_t *hlist_lock;
1250 *head = &kretprobe_inst_table[hash];
1251 hlist_lock = kretprobe_table_lock_ptr(hash);
1252 raw_spin_lock_irqsave(hlist_lock, *flags);
1254 NOKPROBE_SYMBOL(kretprobe_hash_lock);
1256 static void kretprobe_table_lock(unsigned long hash,
1257 unsigned long *flags)
1258 __acquires(hlist_lock)
1260 raw_spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash);
1261 raw_spin_lock_irqsave(hlist_lock, *flags);
1263 NOKPROBE_SYMBOL(kretprobe_table_lock);
1265 static void kretprobe_hash_unlock(struct task_struct *tsk,
1266 unsigned long *flags)
1267 __releases(hlist_lock)
1269 unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS);
1270 raw_spinlock_t *hlist_lock;
1272 hlist_lock = kretprobe_table_lock_ptr(hash);
1273 raw_spin_unlock_irqrestore(hlist_lock, *flags);
1275 NOKPROBE_SYMBOL(kretprobe_hash_unlock);
1277 static void kretprobe_table_unlock(unsigned long hash,
1278 unsigned long *flags)
1279 __releases(hlist_lock)
1281 raw_spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash);
1282 raw_spin_unlock_irqrestore(hlist_lock, *flags);
1284 NOKPROBE_SYMBOL(kretprobe_table_unlock);
1286 static struct kprobe kprobe_busy = {
1287 .addr = (void *) get_kprobe,
1290 void kprobe_busy_begin(void)
1292 struct kprobe_ctlblk *kcb;
1295 __this_cpu_write(current_kprobe, &kprobe_busy);
1296 kcb = get_kprobe_ctlblk();
1297 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
1300 void kprobe_busy_end(void)
1302 __this_cpu_write(current_kprobe, NULL);
1307 * This function is called from finish_task_switch when task tk becomes dead,
1308 * so that we can recycle any function-return probe instances associated
1309 * with this task. These left over instances represent probed functions
1310 * that have been called but will never return.
1312 void kprobe_flush_task(struct task_struct *tk)
1314 struct kretprobe_instance *ri;
1315 struct hlist_head *head;
1316 struct hlist_node *tmp;
1317 unsigned long hash, flags = 0;
1319 if (unlikely(!kprobes_initialized))
1320 /* Early boot. kretprobe_table_locks not yet initialized. */
1323 kprobe_busy_begin();
1325 hash = hash_ptr(tk, KPROBE_HASH_BITS);
1326 head = &kretprobe_inst_table[hash];
1327 kretprobe_table_lock(hash, &flags);
1328 hlist_for_each_entry_safe(ri, tmp, head, hlist) {
1330 recycle_rp_inst(ri);
1332 kretprobe_table_unlock(hash, &flags);
1336 NOKPROBE_SYMBOL(kprobe_flush_task);
1338 static inline void free_rp_inst(struct kretprobe *rp)
1340 struct kretprobe_instance *ri;
1341 struct hlist_node *next;
1343 hlist_for_each_entry_safe(ri, next, &rp->free_instances, hlist) {
1344 hlist_del(&ri->hlist);
1349 static void cleanup_rp_inst(struct kretprobe *rp)
1351 unsigned long flags, hash;
1352 struct kretprobe_instance *ri;
1353 struct hlist_node *next;
1354 struct hlist_head *head;
1356 /* To avoid recursive kretprobe by NMI, set kprobe busy here */
1357 kprobe_busy_begin();
1358 for (hash = 0; hash < KPROBE_TABLE_SIZE; hash++) {
1359 kretprobe_table_lock(hash, &flags);
1360 head = &kretprobe_inst_table[hash];
1361 hlist_for_each_entry_safe(ri, next, head, hlist) {
1365 kretprobe_table_unlock(hash, &flags);
1371 NOKPROBE_SYMBOL(cleanup_rp_inst);
1373 /* Add the new probe to ap->list */
1374 static int add_new_kprobe(struct kprobe *ap, struct kprobe *p)
1376 if (p->post_handler)
1377 unoptimize_kprobe(ap, true); /* Fall back to normal kprobe */
1379 list_add_rcu(&p->list, &ap->list);
1380 if (p->post_handler && !ap->post_handler)
1381 ap->post_handler = aggr_post_handler;
1387 * Fill in the required fields of the "manager kprobe". Replace the
1388 * earlier kprobe in the hlist with the manager kprobe
1390 static void init_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
1392 /* Copy p's insn slot to ap */
1394 flush_insn_slot(ap);
1396 ap->flags = p->flags & ~KPROBE_FLAG_OPTIMIZED;
1397 ap->pre_handler = aggr_pre_handler;
1398 ap->fault_handler = aggr_fault_handler;
1399 /* We don't care the kprobe which has gone. */
1400 if (p->post_handler && !kprobe_gone(p))
1401 ap->post_handler = aggr_post_handler;
1403 INIT_LIST_HEAD(&ap->list);
1404 INIT_HLIST_NODE(&ap->hlist);
1406 list_add_rcu(&p->list, &ap->list);
1407 hlist_replace_rcu(&p->hlist, &ap->hlist);
1411 * This is the second or subsequent kprobe at the address - handle
1414 static int register_aggr_kprobe(struct kprobe *orig_p, struct kprobe *p)
1417 struct kprobe *ap = orig_p;
1421 /* For preparing optimization, jump_label_text_reserved() is called */
1423 mutex_lock(&text_mutex);
1425 if (!kprobe_aggrprobe(orig_p)) {
1426 /* If orig_p is not an aggr_kprobe, create new aggr_kprobe. */
1427 ap = alloc_aggr_kprobe(orig_p);
1432 init_aggr_kprobe(ap, orig_p);
1433 } else if (kprobe_unused(ap)) {
1434 /* This probe is going to die. Rescue it */
1435 ret = reuse_unused_kprobe(ap);
1440 if (kprobe_gone(ap)) {
1442 * Attempting to insert new probe at the same location that
1443 * had a probe in the module vaddr area which already
1444 * freed. So, the instruction slot has already been
1445 * released. We need a new slot for the new probe.
1447 ret = arch_prepare_kprobe(ap);
1450 * Even if fail to allocate new slot, don't need to
1451 * free aggr_probe. It will be used next time, or
1452 * freed by unregister_kprobe.
1456 /* Prepare optimized instructions if possible. */
1457 prepare_optimized_kprobe(ap);
1460 * Clear gone flag to prevent allocating new slot again, and
1461 * set disabled flag because it is not armed yet.
1463 ap->flags = (ap->flags & ~KPROBE_FLAG_GONE)
1464 | KPROBE_FLAG_DISABLED;
1467 /* Copy ap's insn slot to p */
1469 ret = add_new_kprobe(ap, p);
1472 mutex_unlock(&text_mutex);
1473 jump_label_unlock();
1476 if (ret == 0 && kprobe_disabled(ap) && !kprobe_disabled(p)) {
1477 ap->flags &= ~KPROBE_FLAG_DISABLED;
1478 if (!kprobes_all_disarmed) {
1479 /* Arm the breakpoint again. */
1480 ret = arm_kprobe(ap);
1482 ap->flags |= KPROBE_FLAG_DISABLED;
1483 list_del_rcu(&p->list);
1491 bool __weak arch_within_kprobe_blacklist(unsigned long addr)
1493 /* The __kprobes marked functions and entry code must not be probed */
1494 return addr >= (unsigned long)__kprobes_text_start &&
1495 addr < (unsigned long)__kprobes_text_end;
1498 static bool __within_kprobe_blacklist(unsigned long addr)
1500 struct kprobe_blacklist_entry *ent;
1502 if (arch_within_kprobe_blacklist(addr))
1505 * If there exists a kprobe_blacklist, verify and
1506 * fail any probe registration in the prohibited area
1508 list_for_each_entry(ent, &kprobe_blacklist, list) {
1509 if (addr >= ent->start_addr && addr < ent->end_addr)
1515 bool within_kprobe_blacklist(unsigned long addr)
1517 char symname[KSYM_NAME_LEN], *p;
1519 if (__within_kprobe_blacklist(addr))
1522 /* Check if the address is on a suffixed-symbol */
1523 if (!lookup_symbol_name(addr, symname)) {
1524 p = strchr(symname, '.');
1528 addr = (unsigned long)kprobe_lookup_name(symname, 0);
1530 return __within_kprobe_blacklist(addr);
1536 * If we have a symbol_name argument, look it up and add the offset field
1537 * to it. This way, we can specify a relative address to a symbol.
1538 * This returns encoded errors if it fails to look up symbol or invalid
1539 * combination of parameters.
1541 static kprobe_opcode_t *_kprobe_addr(kprobe_opcode_t *addr,
1542 const char *symbol_name, unsigned int offset)
1544 if ((symbol_name && addr) || (!symbol_name && !addr))
1548 addr = kprobe_lookup_name(symbol_name, offset);
1550 return ERR_PTR(-ENOENT);
1553 addr = (kprobe_opcode_t *)(((char *)addr) + offset);
1558 return ERR_PTR(-EINVAL);
1561 static kprobe_opcode_t *kprobe_addr(struct kprobe *p)
1563 return _kprobe_addr(p->addr, p->symbol_name, p->offset);
1566 /* Check passed kprobe is valid and return kprobe in kprobe_table. */
1567 static struct kprobe *__get_valid_kprobe(struct kprobe *p)
1569 struct kprobe *ap, *list_p;
1571 lockdep_assert_held(&kprobe_mutex);
1573 ap = get_kprobe(p->addr);
1578 list_for_each_entry(list_p, &ap->list, list)
1580 /* kprobe p is a valid probe */
1588 /* Return error if the kprobe is being re-registered */
1589 static inline int check_kprobe_rereg(struct kprobe *p)
1593 mutex_lock(&kprobe_mutex);
1594 if (__get_valid_kprobe(p))
1596 mutex_unlock(&kprobe_mutex);
1601 int __weak arch_check_ftrace_location(struct kprobe *p)
1603 unsigned long ftrace_addr;
1605 ftrace_addr = ftrace_location((unsigned long)p->addr);
1607 #ifdef CONFIG_KPROBES_ON_FTRACE
1608 /* Given address is not on the instruction boundary */
1609 if ((unsigned long)p->addr != ftrace_addr)
1611 p->flags |= KPROBE_FLAG_FTRACE;
1612 #else /* !CONFIG_KPROBES_ON_FTRACE */
1619 static int check_kprobe_address_safe(struct kprobe *p,
1620 struct module **probed_mod)
1624 ret = arch_check_ftrace_location(p);
1630 /* Ensure it is not in reserved area nor out of text */
1631 if (!kernel_text_address((unsigned long) p->addr) ||
1632 within_kprobe_blacklist((unsigned long) p->addr) ||
1633 jump_label_text_reserved(p->addr, p->addr) ||
1634 static_call_text_reserved(p->addr, p->addr) ||
1635 find_bug((unsigned long)p->addr)) {
1640 /* Check if are we probing a module */
1641 *probed_mod = __module_text_address((unsigned long) p->addr);
1644 * We must hold a refcount of the probed module while updating
1645 * its code to prohibit unexpected unloading.
1647 if (unlikely(!try_module_get(*probed_mod))) {
1653 * If the module freed .init.text, we couldn't insert
1656 if (within_module_init((unsigned long)p->addr, *probed_mod) &&
1657 (*probed_mod)->state != MODULE_STATE_COMING) {
1658 module_put(*probed_mod);
1665 jump_label_unlock();
1670 int register_kprobe(struct kprobe *p)
1673 struct kprobe *old_p;
1674 struct module *probed_mod;
1675 kprobe_opcode_t *addr;
1677 /* Adjust probe address from symbol */
1678 addr = kprobe_addr(p);
1680 return PTR_ERR(addr);
1683 ret = check_kprobe_rereg(p);
1687 /* User can pass only KPROBE_FLAG_DISABLED to register_kprobe */
1688 p->flags &= KPROBE_FLAG_DISABLED;
1690 INIT_LIST_HEAD(&p->list);
1692 ret = check_kprobe_address_safe(p, &probed_mod);
1696 mutex_lock(&kprobe_mutex);
1698 old_p = get_kprobe(p->addr);
1700 /* Since this may unoptimize old_p, locking text_mutex. */
1701 ret = register_aggr_kprobe(old_p, p);
1706 /* Prevent text modification */
1707 mutex_lock(&text_mutex);
1708 ret = prepare_kprobe(p);
1709 mutex_unlock(&text_mutex);
1714 INIT_HLIST_NODE(&p->hlist);
1715 hlist_add_head_rcu(&p->hlist,
1716 &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
1718 if (!kprobes_all_disarmed && !kprobe_disabled(p)) {
1719 ret = arm_kprobe(p);
1721 hlist_del_rcu(&p->hlist);
1727 /* Try to optimize kprobe */
1728 try_to_optimize_kprobe(p);
1730 mutex_unlock(&kprobe_mutex);
1733 module_put(probed_mod);
1737 EXPORT_SYMBOL_GPL(register_kprobe);
1739 /* Check if all probes on the aggrprobe are disabled */
1740 static int aggr_kprobe_disabled(struct kprobe *ap)
1744 lockdep_assert_held(&kprobe_mutex);
1746 list_for_each_entry(kp, &ap->list, list)
1747 if (!kprobe_disabled(kp))
1749 * There is an active probe on the list.
1750 * We can't disable this ap.
1757 /* Disable one kprobe: Make sure called under kprobe_mutex is locked */
1758 static struct kprobe *__disable_kprobe(struct kprobe *p)
1760 struct kprobe *orig_p;
1763 /* Get an original kprobe for return */
1764 orig_p = __get_valid_kprobe(p);
1765 if (unlikely(orig_p == NULL))
1766 return ERR_PTR(-EINVAL);
1768 if (!kprobe_disabled(p)) {
1769 /* Disable probe if it is a child probe */
1771 p->flags |= KPROBE_FLAG_DISABLED;
1773 /* Try to disarm and disable this/parent probe */
1774 if (p == orig_p || aggr_kprobe_disabled(orig_p)) {
1776 * If kprobes_all_disarmed is set, orig_p
1777 * should have already been disarmed, so
1778 * skip unneed disarming process.
1780 if (!kprobes_all_disarmed) {
1781 ret = disarm_kprobe(orig_p, true);
1783 p->flags &= ~KPROBE_FLAG_DISABLED;
1784 return ERR_PTR(ret);
1787 orig_p->flags |= KPROBE_FLAG_DISABLED;
1795 * Unregister a kprobe without a scheduler synchronization.
1797 static int __unregister_kprobe_top(struct kprobe *p)
1799 struct kprobe *ap, *list_p;
1801 /* Disable kprobe. This will disarm it if needed. */
1802 ap = __disable_kprobe(p);
1808 * This probe is an independent(and non-optimized) kprobe
1809 * (not an aggrprobe). Remove from the hash list.
1813 /* Following process expects this probe is an aggrprobe */
1814 WARN_ON(!kprobe_aggrprobe(ap));
1816 if (list_is_singular(&ap->list) && kprobe_disarmed(ap))
1818 * !disarmed could be happen if the probe is under delayed
1823 /* If disabling probe has special handlers, update aggrprobe */
1824 if (p->post_handler && !kprobe_gone(p)) {
1825 list_for_each_entry(list_p, &ap->list, list) {
1826 if ((list_p != p) && (list_p->post_handler))
1829 ap->post_handler = NULL;
1833 * Remove from the aggrprobe: this path will do nothing in
1834 * __unregister_kprobe_bottom().
1836 list_del_rcu(&p->list);
1837 if (!kprobe_disabled(ap) && !kprobes_all_disarmed)
1839 * Try to optimize this probe again, because post
1840 * handler may have been changed.
1842 optimize_kprobe(ap);
1847 hlist_del_rcu(&ap->hlist);
1851 static void __unregister_kprobe_bottom(struct kprobe *p)
1855 if (list_empty(&p->list))
1856 /* This is an independent kprobe */
1857 arch_remove_kprobe(p);
1858 else if (list_is_singular(&p->list)) {
1859 /* This is the last child of an aggrprobe */
1860 ap = list_entry(p->list.next, struct kprobe, list);
1862 free_aggr_kprobe(ap);
1864 /* Otherwise, do nothing. */
1867 int register_kprobes(struct kprobe **kps, int num)
1873 for (i = 0; i < num; i++) {
1874 ret = register_kprobe(kps[i]);
1877 unregister_kprobes(kps, i);
1883 EXPORT_SYMBOL_GPL(register_kprobes);
1885 void unregister_kprobe(struct kprobe *p)
1887 unregister_kprobes(&p, 1);
1889 EXPORT_SYMBOL_GPL(unregister_kprobe);
1891 void unregister_kprobes(struct kprobe **kps, int num)
1897 mutex_lock(&kprobe_mutex);
1898 for (i = 0; i < num; i++)
1899 if (__unregister_kprobe_top(kps[i]) < 0)
1900 kps[i]->addr = NULL;
1901 mutex_unlock(&kprobe_mutex);
1904 for (i = 0; i < num; i++)
1906 __unregister_kprobe_bottom(kps[i]);
1908 EXPORT_SYMBOL_GPL(unregister_kprobes);
1910 int __weak kprobe_exceptions_notify(struct notifier_block *self,
1911 unsigned long val, void *data)
1915 NOKPROBE_SYMBOL(kprobe_exceptions_notify);
1917 static struct notifier_block kprobe_exceptions_nb = {
1918 .notifier_call = kprobe_exceptions_notify,
1919 .priority = 0x7fffffff /* we need to be notified first */
1922 unsigned long __weak arch_deref_entry_point(void *entry)
1924 return (unsigned long)entry;
1927 #ifdef CONFIG_KRETPROBES
1929 unsigned long __kretprobe_trampoline_handler(struct pt_regs *regs,
1930 void *trampoline_address,
1931 void *frame_pointer)
1933 struct kretprobe_instance *ri = NULL, *last = NULL;
1934 struct hlist_head *head;
1935 struct hlist_node *tmp;
1936 unsigned long flags;
1937 kprobe_opcode_t *correct_ret_addr = NULL;
1938 bool skipped = false;
1940 kretprobe_hash_lock(current, &head, &flags);
1943 * It is possible to have multiple instances associated with a given
1944 * task either because multiple functions in the call path have
1945 * return probes installed on them, and/or more than one
1946 * return probe was registered for a target function.
1948 * We can handle this because:
1949 * - instances are always pushed into the head of the list
1950 * - when multiple return probes are registered for the same
1951 * function, the (chronologically) first instance's ret_addr
1952 * will be the real return address, and all the rest will
1953 * point to kretprobe_trampoline.
1955 hlist_for_each_entry(ri, head, hlist) {
1956 if (ri->task != current)
1957 /* another task is sharing our hash bucket */
1960 * Return probes must be pushed on this hash list correct
1961 * order (same as return order) so that it can be popped
1962 * correctly. However, if we find it is pushed it incorrect
1963 * order, this means we find a function which should not be
1964 * probed, because the wrong order entry is pushed on the
1965 * path of processing other kretprobe itself.
1967 if (ri->fp != frame_pointer) {
1969 pr_warn("kretprobe is stacked incorrectly. Trying to fixup.\n");
1974 correct_ret_addr = ri->ret_addr;
1976 pr_warn("%ps must be blacklisted because of incorrect kretprobe order\n",
1979 if (correct_ret_addr != trampoline_address)
1981 * This is the real return address. Any other
1982 * instances associated with this task are for
1983 * other calls deeper on the call stack
1988 BUG_ON(!correct_ret_addr || (correct_ret_addr == trampoline_address));
1991 hlist_for_each_entry_safe(ri, tmp, head, hlist) {
1992 if (ri->task != current)
1993 /* another task is sharing our hash bucket */
1995 if (ri->fp != frame_pointer)
1998 if (ri->rp && ri->rp->handler) {
1999 struct kprobe *prev = kprobe_running();
2001 __this_cpu_write(current_kprobe, &ri->rp->kp);
2002 ri->ret_addr = correct_ret_addr;
2003 ri->rp->handler(ri, regs);
2004 __this_cpu_write(current_kprobe, prev);
2007 recycle_rp_inst(ri);
2013 kretprobe_hash_unlock(current, &flags);
2015 return (unsigned long)correct_ret_addr;
2017 NOKPROBE_SYMBOL(__kretprobe_trampoline_handler)
2020 * This kprobe pre_handler is registered with every kretprobe. When probe
2021 * hits it will set up the return probe.
2023 static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
2025 struct kretprobe *rp = container_of(p, struct kretprobe, kp);
2026 unsigned long hash, flags = 0;
2027 struct kretprobe_instance *ri;
2029 /* TODO: consider to only swap the RA after the last pre_handler fired */
2030 hash = hash_ptr(current, KPROBE_HASH_BITS);
2031 raw_spin_lock_irqsave(&rp->lock, flags);
2032 if (!hlist_empty(&rp->free_instances)) {
2033 ri = hlist_entry(rp->free_instances.first,
2034 struct kretprobe_instance, hlist);
2035 hlist_del(&ri->hlist);
2036 raw_spin_unlock_irqrestore(&rp->lock, flags);
2041 if (rp->entry_handler && rp->entry_handler(ri, regs)) {
2042 raw_spin_lock_irqsave(&rp->lock, flags);
2043 hlist_add_head(&ri->hlist, &rp->free_instances);
2044 raw_spin_unlock_irqrestore(&rp->lock, flags);
2048 arch_prepare_kretprobe(ri, regs);
2050 /* XXX(hch): why is there no hlist_move_head? */
2051 INIT_HLIST_NODE(&ri->hlist);
2052 kretprobe_table_lock(hash, &flags);
2053 hlist_add_head(&ri->hlist, &kretprobe_inst_table[hash]);
2054 kretprobe_table_unlock(hash, &flags);
2057 raw_spin_unlock_irqrestore(&rp->lock, flags);
2061 NOKPROBE_SYMBOL(pre_handler_kretprobe);
2063 bool __weak arch_kprobe_on_func_entry(unsigned long offset)
2068 bool kprobe_on_func_entry(kprobe_opcode_t *addr, const char *sym, unsigned long offset)
2070 kprobe_opcode_t *kp_addr = _kprobe_addr(addr, sym, offset);
2072 if (IS_ERR(kp_addr))
2075 if (!kallsyms_lookup_size_offset((unsigned long)kp_addr, NULL, &offset) ||
2076 !arch_kprobe_on_func_entry(offset))
2082 int register_kretprobe(struct kretprobe *rp)
2085 struct kretprobe_instance *inst;
2089 if (!kprobe_on_func_entry(rp->kp.addr, rp->kp.symbol_name, rp->kp.offset))
2092 if (kretprobe_blacklist_size) {
2093 addr = kprobe_addr(&rp->kp);
2095 return PTR_ERR(addr);
2097 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
2098 if (kretprobe_blacklist[i].addr == addr)
2103 rp->kp.pre_handler = pre_handler_kretprobe;
2104 rp->kp.post_handler = NULL;
2105 rp->kp.fault_handler = NULL;
2107 /* Pre-allocate memory for max kretprobe instances */
2108 if (rp->maxactive <= 0) {
2109 #ifdef CONFIG_PREEMPTION
2110 rp->maxactive = max_t(unsigned int, 10, 2*num_possible_cpus());
2112 rp->maxactive = num_possible_cpus();
2115 raw_spin_lock_init(&rp->lock);
2116 INIT_HLIST_HEAD(&rp->free_instances);
2117 for (i = 0; i < rp->maxactive; i++) {
2118 inst = kmalloc(sizeof(struct kretprobe_instance) +
2119 rp->data_size, GFP_KERNEL);
2124 INIT_HLIST_NODE(&inst->hlist);
2125 hlist_add_head(&inst->hlist, &rp->free_instances);
2129 /* Establish function entry probe point */
2130 ret = register_kprobe(&rp->kp);
2135 EXPORT_SYMBOL_GPL(register_kretprobe);
2137 int register_kretprobes(struct kretprobe **rps, int num)
2143 for (i = 0; i < num; i++) {
2144 ret = register_kretprobe(rps[i]);
2147 unregister_kretprobes(rps, i);
2153 EXPORT_SYMBOL_GPL(register_kretprobes);
2155 void unregister_kretprobe(struct kretprobe *rp)
2157 unregister_kretprobes(&rp, 1);
2159 EXPORT_SYMBOL_GPL(unregister_kretprobe);
2161 void unregister_kretprobes(struct kretprobe **rps, int num)
2167 mutex_lock(&kprobe_mutex);
2168 for (i = 0; i < num; i++)
2169 if (__unregister_kprobe_top(&rps[i]->kp) < 0)
2170 rps[i]->kp.addr = NULL;
2171 mutex_unlock(&kprobe_mutex);
2174 for (i = 0; i < num; i++) {
2175 if (rps[i]->kp.addr) {
2176 __unregister_kprobe_bottom(&rps[i]->kp);
2177 cleanup_rp_inst(rps[i]);
2181 EXPORT_SYMBOL_GPL(unregister_kretprobes);
2183 #else /* CONFIG_KRETPROBES */
2184 int register_kretprobe(struct kretprobe *rp)
2188 EXPORT_SYMBOL_GPL(register_kretprobe);
2190 int register_kretprobes(struct kretprobe **rps, int num)
2194 EXPORT_SYMBOL_GPL(register_kretprobes);
2196 void unregister_kretprobe(struct kretprobe *rp)
2199 EXPORT_SYMBOL_GPL(unregister_kretprobe);
2201 void unregister_kretprobes(struct kretprobe **rps, int num)
2204 EXPORT_SYMBOL_GPL(unregister_kretprobes);
2206 static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
2210 NOKPROBE_SYMBOL(pre_handler_kretprobe);
2212 #endif /* CONFIG_KRETPROBES */
2214 /* Set the kprobe gone and remove its instruction buffer. */
2215 static void kill_kprobe(struct kprobe *p)
2219 lockdep_assert_held(&kprobe_mutex);
2221 if (WARN_ON_ONCE(kprobe_gone(p)))
2224 p->flags |= KPROBE_FLAG_GONE;
2225 if (kprobe_aggrprobe(p)) {
2227 * If this is an aggr_kprobe, we have to list all the
2228 * chained probes and mark them GONE.
2230 list_for_each_entry(kp, &p->list, list)
2231 kp->flags |= KPROBE_FLAG_GONE;
2232 p->post_handler = NULL;
2233 kill_optimized_kprobe(p);
2236 * Here, we can remove insn_slot safely, because no thread calls
2237 * the original probed function (which will be freed soon) any more.
2239 arch_remove_kprobe(p);
2242 * The module is going away. We should disarm the kprobe which
2243 * is using ftrace, because ftrace framework is still available at
2244 * MODULE_STATE_GOING notification.
2246 if (kprobe_ftrace(p) && !kprobe_disabled(p) && !kprobes_all_disarmed)
2247 disarm_kprobe_ftrace(p);
2250 /* Disable one kprobe */
2251 int disable_kprobe(struct kprobe *kp)
2256 mutex_lock(&kprobe_mutex);
2258 /* Disable this kprobe */
2259 p = __disable_kprobe(kp);
2263 mutex_unlock(&kprobe_mutex);
2266 EXPORT_SYMBOL_GPL(disable_kprobe);
2268 /* Enable one kprobe */
2269 int enable_kprobe(struct kprobe *kp)
2274 mutex_lock(&kprobe_mutex);
2276 /* Check whether specified probe is valid. */
2277 p = __get_valid_kprobe(kp);
2278 if (unlikely(p == NULL)) {
2283 if (kprobe_gone(kp)) {
2284 /* This kprobe has gone, we couldn't enable it. */
2290 kp->flags &= ~KPROBE_FLAG_DISABLED;
2292 if (!kprobes_all_disarmed && kprobe_disabled(p)) {
2293 p->flags &= ~KPROBE_FLAG_DISABLED;
2294 ret = arm_kprobe(p);
2296 p->flags |= KPROBE_FLAG_DISABLED;
2299 mutex_unlock(&kprobe_mutex);
2302 EXPORT_SYMBOL_GPL(enable_kprobe);
2304 /* Caller must NOT call this in usual path. This is only for critical case */
2305 void dump_kprobe(struct kprobe *kp)
2307 pr_err("Dumping kprobe:\n");
2308 pr_err("Name: %s\nOffset: %x\nAddress: %pS\n",
2309 kp->symbol_name, kp->offset, kp->addr);
2311 NOKPROBE_SYMBOL(dump_kprobe);
2313 int kprobe_add_ksym_blacklist(unsigned long entry)
2315 struct kprobe_blacklist_entry *ent;
2316 unsigned long offset = 0, size = 0;
2318 if (!kernel_text_address(entry) ||
2319 !kallsyms_lookup_size_offset(entry, &size, &offset))
2322 ent = kmalloc(sizeof(*ent), GFP_KERNEL);
2325 ent->start_addr = entry;
2326 ent->end_addr = entry + size;
2327 INIT_LIST_HEAD(&ent->list);
2328 list_add_tail(&ent->list, &kprobe_blacklist);
2333 /* Add all symbols in given area into kprobe blacklist */
2334 int kprobe_add_area_blacklist(unsigned long start, unsigned long end)
2336 unsigned long entry;
2339 for (entry = start; entry < end; entry += ret) {
2340 ret = kprobe_add_ksym_blacklist(entry);
2343 if (ret == 0) /* In case of alias symbol */
2349 /* Remove all symbols in given area from kprobe blacklist */
2350 static void kprobe_remove_area_blacklist(unsigned long start, unsigned long end)
2352 struct kprobe_blacklist_entry *ent, *n;
2354 list_for_each_entry_safe(ent, n, &kprobe_blacklist, list) {
2355 if (ent->start_addr < start || ent->start_addr >= end)
2357 list_del(&ent->list);
2362 static void kprobe_remove_ksym_blacklist(unsigned long entry)
2364 kprobe_remove_area_blacklist(entry, entry + 1);
2367 int __weak arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *value,
2368 char *type, char *sym)
2373 int kprobe_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2376 #ifdef __ARCH_WANT_KPROBES_INSN_SLOT
2377 if (!kprobe_cache_get_kallsym(&kprobe_insn_slots, &symnum, value, type, sym))
2379 #ifdef CONFIG_OPTPROBES
2380 if (!kprobe_cache_get_kallsym(&kprobe_optinsn_slots, &symnum, value, type, sym))
2384 if (!arch_kprobe_get_kallsym(&symnum, value, type, sym))
2389 int __init __weak arch_populate_kprobe_blacklist(void)
2395 * Lookup and populate the kprobe_blacklist.
2397 * Unlike the kretprobe blacklist, we'll need to determine
2398 * the range of addresses that belong to the said functions,
2399 * since a kprobe need not necessarily be at the beginning
2402 static int __init populate_kprobe_blacklist(unsigned long *start,
2405 unsigned long entry;
2406 unsigned long *iter;
2409 for (iter = start; iter < end; iter++) {
2410 entry = arch_deref_entry_point((void *)*iter);
2411 ret = kprobe_add_ksym_blacklist(entry);
2418 /* Symbols in __kprobes_text are blacklisted */
2419 ret = kprobe_add_area_blacklist((unsigned long)__kprobes_text_start,
2420 (unsigned long)__kprobes_text_end);
2424 /* Symbols in noinstr section are blacklisted */
2425 ret = kprobe_add_area_blacklist((unsigned long)__noinstr_text_start,
2426 (unsigned long)__noinstr_text_end);
2428 return ret ? : arch_populate_kprobe_blacklist();
2431 static void add_module_kprobe_blacklist(struct module *mod)
2433 unsigned long start, end;
2436 if (mod->kprobe_blacklist) {
2437 for (i = 0; i < mod->num_kprobe_blacklist; i++)
2438 kprobe_add_ksym_blacklist(mod->kprobe_blacklist[i]);
2441 start = (unsigned long)mod->kprobes_text_start;
2443 end = start + mod->kprobes_text_size;
2444 kprobe_add_area_blacklist(start, end);
2447 start = (unsigned long)mod->noinstr_text_start;
2449 end = start + mod->noinstr_text_size;
2450 kprobe_add_area_blacklist(start, end);
2454 static void remove_module_kprobe_blacklist(struct module *mod)
2456 unsigned long start, end;
2459 if (mod->kprobe_blacklist) {
2460 for (i = 0; i < mod->num_kprobe_blacklist; i++)
2461 kprobe_remove_ksym_blacklist(mod->kprobe_blacklist[i]);
2464 start = (unsigned long)mod->kprobes_text_start;
2466 end = start + mod->kprobes_text_size;
2467 kprobe_remove_area_blacklist(start, end);
2470 start = (unsigned long)mod->noinstr_text_start;
2472 end = start + mod->noinstr_text_size;
2473 kprobe_remove_area_blacklist(start, end);
2477 /* Module notifier call back, checking kprobes on the module */
2478 static int kprobes_module_callback(struct notifier_block *nb,
2479 unsigned long val, void *data)
2481 struct module *mod = data;
2482 struct hlist_head *head;
2485 int checkcore = (val == MODULE_STATE_GOING);
2487 if (val == MODULE_STATE_COMING) {
2488 mutex_lock(&kprobe_mutex);
2489 add_module_kprobe_blacklist(mod);
2490 mutex_unlock(&kprobe_mutex);
2492 if (val != MODULE_STATE_GOING && val != MODULE_STATE_LIVE)
2496 * When MODULE_STATE_GOING was notified, both of module .text and
2497 * .init.text sections would be freed. When MODULE_STATE_LIVE was
2498 * notified, only .init.text section would be freed. We need to
2499 * disable kprobes which have been inserted in the sections.
2501 mutex_lock(&kprobe_mutex);
2502 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2503 head = &kprobe_table[i];
2504 hlist_for_each_entry(p, head, hlist) {
2508 if (within_module_init((unsigned long)p->addr, mod) ||
2510 within_module_core((unsigned long)p->addr, mod))) {
2512 * The vaddr this probe is installed will soon
2513 * be vfreed buy not synced to disk. Hence,
2514 * disarming the breakpoint isn't needed.
2516 * Note, this will also move any optimized probes
2517 * that are pending to be removed from their
2518 * corresponding lists to the freeing_list and
2519 * will not be touched by the delayed
2520 * kprobe_optimizer work handler.
2526 if (val == MODULE_STATE_GOING)
2527 remove_module_kprobe_blacklist(mod);
2528 mutex_unlock(&kprobe_mutex);
2532 static struct notifier_block kprobe_module_nb = {
2533 .notifier_call = kprobes_module_callback,
2537 /* Markers of _kprobe_blacklist section */
2538 extern unsigned long __start_kprobe_blacklist[];
2539 extern unsigned long __stop_kprobe_blacklist[];
2541 void kprobe_free_init_mem(void)
2543 void *start = (void *)(&__init_begin);
2544 void *end = (void *)(&__init_end);
2545 struct hlist_head *head;
2549 mutex_lock(&kprobe_mutex);
2551 /* Kill all kprobes on initmem */
2552 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2553 head = &kprobe_table[i];
2554 hlist_for_each_entry(p, head, hlist) {
2555 if (start <= (void *)p->addr && (void *)p->addr < end)
2560 mutex_unlock(&kprobe_mutex);
2563 static int __init init_kprobes(void)
2567 /* FIXME allocate the probe table, currently defined statically */
2568 /* initialize all list heads */
2569 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2570 INIT_HLIST_HEAD(&kprobe_table[i]);
2571 INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
2572 raw_spin_lock_init(&(kretprobe_table_locks[i].lock));
2575 err = populate_kprobe_blacklist(__start_kprobe_blacklist,
2576 __stop_kprobe_blacklist);
2578 pr_err("kprobes: failed to populate blacklist: %d\n", err);
2579 pr_err("Please take care of using kprobes.\n");
2582 if (kretprobe_blacklist_size) {
2583 /* lookup the function address from its name */
2584 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
2585 kretprobe_blacklist[i].addr =
2586 kprobe_lookup_name(kretprobe_blacklist[i].name, 0);
2587 if (!kretprobe_blacklist[i].addr)
2588 printk("kretprobe: lookup failed: %s\n",
2589 kretprobe_blacklist[i].name);
2593 #if defined(CONFIG_OPTPROBES)
2594 #if defined(__ARCH_WANT_KPROBES_INSN_SLOT)
2595 /* Init kprobe_optinsn_slots */
2596 kprobe_optinsn_slots.insn_size = MAX_OPTINSN_SIZE;
2598 /* By default, kprobes can be optimized */
2599 kprobes_allow_optimization = true;
2602 /* By default, kprobes are armed */
2603 kprobes_all_disarmed = false;
2605 err = arch_init_kprobes();
2607 err = register_die_notifier(&kprobe_exceptions_nb);
2609 err = register_module_notifier(&kprobe_module_nb);
2611 kprobes_initialized = (err == 0);
2617 early_initcall(init_kprobes);
2619 #ifdef CONFIG_DEBUG_FS
2620 static void report_probe(struct seq_file *pi, struct kprobe *p,
2621 const char *sym, int offset, char *modname, struct kprobe *pp)
2624 void *addr = p->addr;
2626 if (p->pre_handler == pre_handler_kretprobe)
2631 if (!kallsyms_show_value(pi->file->f_cred))
2635 seq_printf(pi, "%px %s %s+0x%x %s ",
2636 addr, kprobe_type, sym, offset,
2637 (modname ? modname : " "));
2638 else /* try to use %pS */
2639 seq_printf(pi, "%px %s %pS ",
2640 addr, kprobe_type, p->addr);
2644 seq_printf(pi, "%s%s%s%s\n",
2645 (kprobe_gone(p) ? "[GONE]" : ""),
2646 ((kprobe_disabled(p) && !kprobe_gone(p)) ? "[DISABLED]" : ""),
2647 (kprobe_optimized(pp) ? "[OPTIMIZED]" : ""),
2648 (kprobe_ftrace(pp) ? "[FTRACE]" : ""));
2651 static void *kprobe_seq_start(struct seq_file *f, loff_t *pos)
2653 return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL;
2656 static void *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos)
2659 if (*pos >= KPROBE_TABLE_SIZE)
2664 static void kprobe_seq_stop(struct seq_file *f, void *v)
2669 static int show_kprobe_addr(struct seq_file *pi, void *v)
2671 struct hlist_head *head;
2672 struct kprobe *p, *kp;
2673 const char *sym = NULL;
2674 unsigned int i = *(loff_t *) v;
2675 unsigned long offset = 0;
2676 char *modname, namebuf[KSYM_NAME_LEN];
2678 head = &kprobe_table[i];
2680 hlist_for_each_entry_rcu(p, head, hlist) {
2681 sym = kallsyms_lookup((unsigned long)p->addr, NULL,
2682 &offset, &modname, namebuf);
2683 if (kprobe_aggrprobe(p)) {
2684 list_for_each_entry_rcu(kp, &p->list, list)
2685 report_probe(pi, kp, sym, offset, modname, p);
2687 report_probe(pi, p, sym, offset, modname, NULL);
2693 static const struct seq_operations kprobes_sops = {
2694 .start = kprobe_seq_start,
2695 .next = kprobe_seq_next,
2696 .stop = kprobe_seq_stop,
2697 .show = show_kprobe_addr
2700 DEFINE_SEQ_ATTRIBUTE(kprobes);
2702 /* kprobes/blacklist -- shows which functions can not be probed */
2703 static void *kprobe_blacklist_seq_start(struct seq_file *m, loff_t *pos)
2705 mutex_lock(&kprobe_mutex);
2706 return seq_list_start(&kprobe_blacklist, *pos);
2709 static void *kprobe_blacklist_seq_next(struct seq_file *m, void *v, loff_t *pos)
2711 return seq_list_next(v, &kprobe_blacklist, pos);
2714 static int kprobe_blacklist_seq_show(struct seq_file *m, void *v)
2716 struct kprobe_blacklist_entry *ent =
2717 list_entry(v, struct kprobe_blacklist_entry, list);
2720 * If /proc/kallsyms is not showing kernel address, we won't
2721 * show them here either.
2723 if (!kallsyms_show_value(m->file->f_cred))
2724 seq_printf(m, "0x%px-0x%px\t%ps\n", NULL, NULL,
2725 (void *)ent->start_addr);
2727 seq_printf(m, "0x%px-0x%px\t%ps\n", (void *)ent->start_addr,
2728 (void *)ent->end_addr, (void *)ent->start_addr);
2732 static void kprobe_blacklist_seq_stop(struct seq_file *f, void *v)
2734 mutex_unlock(&kprobe_mutex);
2737 static const struct seq_operations kprobe_blacklist_sops = {
2738 .start = kprobe_blacklist_seq_start,
2739 .next = kprobe_blacklist_seq_next,
2740 .stop = kprobe_blacklist_seq_stop,
2741 .show = kprobe_blacklist_seq_show,
2743 DEFINE_SEQ_ATTRIBUTE(kprobe_blacklist);
2745 static int arm_all_kprobes(void)
2747 struct hlist_head *head;
2749 unsigned int i, total = 0, errors = 0;
2752 mutex_lock(&kprobe_mutex);
2754 /* If kprobes are armed, just return */
2755 if (!kprobes_all_disarmed)
2756 goto already_enabled;
2759 * optimize_kprobe() called by arm_kprobe() checks
2760 * kprobes_all_disarmed, so set kprobes_all_disarmed before
2763 kprobes_all_disarmed = false;
2764 /* Arming kprobes doesn't optimize kprobe itself */
2765 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2766 head = &kprobe_table[i];
2767 /* Arm all kprobes on a best-effort basis */
2768 hlist_for_each_entry(p, head, hlist) {
2769 if (!kprobe_disabled(p)) {
2770 err = arm_kprobe(p);
2781 pr_warn("Kprobes globally enabled, but failed to arm %d out of %d probes\n",
2784 pr_info("Kprobes globally enabled\n");
2787 mutex_unlock(&kprobe_mutex);
2791 static int disarm_all_kprobes(void)
2793 struct hlist_head *head;
2795 unsigned int i, total = 0, errors = 0;
2798 mutex_lock(&kprobe_mutex);
2800 /* If kprobes are already disarmed, just return */
2801 if (kprobes_all_disarmed) {
2802 mutex_unlock(&kprobe_mutex);
2806 kprobes_all_disarmed = true;
2808 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2809 head = &kprobe_table[i];
2810 /* Disarm all kprobes on a best-effort basis */
2811 hlist_for_each_entry(p, head, hlist) {
2812 if (!arch_trampoline_kprobe(p) && !kprobe_disabled(p)) {
2813 err = disarm_kprobe(p, false);
2824 pr_warn("Kprobes globally disabled, but failed to disarm %d out of %d probes\n",
2827 pr_info("Kprobes globally disabled\n");
2829 mutex_unlock(&kprobe_mutex);
2831 /* Wait for disarming all kprobes by optimizer */
2832 wait_for_kprobe_optimizer();
2838 * XXX: The debugfs bool file interface doesn't allow for callbacks
2839 * when the bool state is switched. We can reuse that facility when
2842 static ssize_t read_enabled_file_bool(struct file *file,
2843 char __user *user_buf, size_t count, loff_t *ppos)
2847 if (!kprobes_all_disarmed)
2853 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
2856 static ssize_t write_enabled_file_bool(struct file *file,
2857 const char __user *user_buf, size_t count, loff_t *ppos)
2863 buf_size = min(count, (sizeof(buf)-1));
2864 if (copy_from_user(buf, user_buf, buf_size))
2867 buf[buf_size] = '\0';
2872 ret = arm_all_kprobes();
2877 ret = disarm_all_kprobes();
2889 static const struct file_operations fops_kp = {
2890 .read = read_enabled_file_bool,
2891 .write = write_enabled_file_bool,
2892 .llseek = default_llseek,
2895 static int __init debugfs_kprobe_init(void)
2898 unsigned int value = 1;
2900 dir = debugfs_create_dir("kprobes", NULL);
2902 debugfs_create_file("list", 0400, dir, NULL, &kprobes_fops);
2904 debugfs_create_file("enabled", 0600, dir, &value, &fops_kp);
2906 debugfs_create_file("blacklist", 0400, dir, NULL,
2907 &kprobe_blacklist_fops);
2912 late_initcall(debugfs_kprobe_init);
2913 #endif /* CONFIG_DEBUG_FS */