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>
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 if the kprobe is disarmed. Note: p must be on hash list */
451 bool 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 if (!list_empty(&unoptimizing_list))
549 arch_unoptimize_kprobes(&unoptimizing_list, &freeing_list);
551 /* Loop on 'freeing_list' for disarming and removing from kprobe hash list */
552 list_for_each_entry_safe(op, tmp, &freeing_list, list) {
553 /* Switching from detour code to origin */
554 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
555 /* Disarm probes if marked disabled and not gone */
556 if (kprobe_disabled(&op->kp) && !kprobe_gone(&op->kp))
557 arch_disarm_kprobe(&op->kp);
558 if (kprobe_unused(&op->kp)) {
560 * Remove unused probes from hash list. After waiting
561 * for synchronization, these probes are reclaimed.
562 * (reclaiming is done by do_free_cleaned_kprobes.)
564 hlist_del_rcu(&op->kp.hlist);
566 list_del_init(&op->list);
570 /* Reclaim all kprobes on the free_list */
571 static void do_free_cleaned_kprobes(void)
573 struct optimized_kprobe *op, *tmp;
575 list_for_each_entry_safe(op, tmp, &freeing_list, list) {
576 list_del_init(&op->list);
577 if (WARN_ON_ONCE(!kprobe_unused(&op->kp))) {
579 * This must not happen, but if there is a kprobe
580 * still in use, keep it on kprobes hash list.
584 free_aggr_kprobe(&op->kp);
588 /* Start optimizer after OPTIMIZE_DELAY passed */
589 static void kick_kprobe_optimizer(void)
591 schedule_delayed_work(&optimizing_work, OPTIMIZE_DELAY);
594 /* Kprobe jump optimizer */
595 static void kprobe_optimizer(struct work_struct *work)
597 mutex_lock(&kprobe_mutex);
599 mutex_lock(&text_mutex);
602 * Step 1: Unoptimize kprobes and collect cleaned (unused and disarmed)
603 * kprobes before waiting for quiesence period.
605 do_unoptimize_kprobes();
608 * Step 2: Wait for quiesence period to ensure all potentially
609 * preempted tasks to have normally scheduled. Because optprobe
610 * may modify multiple instructions, there is a chance that Nth
611 * instruction is preempted. In that case, such tasks can return
612 * to 2nd-Nth byte of jump instruction. This wait is for avoiding it.
613 * Note that on non-preemptive kernel, this is transparently converted
614 * to synchronoze_sched() to wait for all interrupts to have completed.
616 synchronize_rcu_tasks();
618 /* Step 3: Optimize kprobes after quiesence period */
619 do_optimize_kprobes();
621 /* Step 4: Free cleaned kprobes after quiesence period */
622 do_free_cleaned_kprobes();
624 mutex_unlock(&text_mutex);
627 /* Step 5: Kick optimizer again if needed */
628 if (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list))
629 kick_kprobe_optimizer();
631 mutex_unlock(&kprobe_mutex);
634 /* Wait for completing optimization and unoptimization */
635 void wait_for_kprobe_optimizer(void)
637 mutex_lock(&kprobe_mutex);
639 while (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list)) {
640 mutex_unlock(&kprobe_mutex);
642 /* this will also make optimizing_work execute immmediately */
643 flush_delayed_work(&optimizing_work);
644 /* @optimizing_work might not have been queued yet, relax */
647 mutex_lock(&kprobe_mutex);
650 mutex_unlock(&kprobe_mutex);
653 bool optprobe_queued_unopt(struct optimized_kprobe *op)
655 struct optimized_kprobe *_op;
657 list_for_each_entry(_op, &unoptimizing_list, list) {
665 /* Optimize kprobe if p is ready to be optimized */
666 static void optimize_kprobe(struct kprobe *p)
668 struct optimized_kprobe *op;
670 /* Check if the kprobe is disabled or not ready for optimization. */
671 if (!kprobe_optready(p) || !kprobes_allow_optimization ||
672 (kprobe_disabled(p) || kprobes_all_disarmed))
675 /* kprobes with post_handler can not be optimized */
679 op = container_of(p, struct optimized_kprobe, kp);
681 /* Check there is no other kprobes at the optimized instructions */
682 if (arch_check_optimized_kprobe(op) < 0)
685 /* Check if it is already optimized. */
686 if (op->kp.flags & KPROBE_FLAG_OPTIMIZED) {
687 if (optprobe_queued_unopt(op)) {
688 /* This is under unoptimizing. Just dequeue the probe */
689 list_del_init(&op->list);
693 op->kp.flags |= KPROBE_FLAG_OPTIMIZED;
695 /* On unoptimizing/optimizing_list, op must have OPTIMIZED flag */
696 if (WARN_ON_ONCE(!list_empty(&op->list)))
699 list_add(&op->list, &optimizing_list);
700 kick_kprobe_optimizer();
703 /* Short cut to direct unoptimizing */
704 static void force_unoptimize_kprobe(struct optimized_kprobe *op)
706 lockdep_assert_cpus_held();
707 arch_unoptimize_kprobe(op);
708 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
711 /* Unoptimize a kprobe if p is optimized */
712 static void unoptimize_kprobe(struct kprobe *p, bool force)
714 struct optimized_kprobe *op;
716 if (!kprobe_aggrprobe(p) || kprobe_disarmed(p))
717 return; /* This is not an optprobe nor optimized */
719 op = container_of(p, struct optimized_kprobe, kp);
720 if (!kprobe_optimized(p))
723 if (!list_empty(&op->list)) {
724 if (optprobe_queued_unopt(op)) {
725 /* Queued in unoptimizing queue */
728 * Forcibly unoptimize the kprobe here, and queue it
729 * in the freeing list for release afterwards.
731 force_unoptimize_kprobe(op);
732 list_move(&op->list, &freeing_list);
735 /* Dequeue from the optimizing queue */
736 list_del_init(&op->list);
737 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
742 /* Optimized kprobe case */
744 /* Forcibly update the code: this is a special case */
745 force_unoptimize_kprobe(op);
747 list_add(&op->list, &unoptimizing_list);
748 kick_kprobe_optimizer();
752 /* Cancel unoptimizing for reusing */
753 static int reuse_unused_kprobe(struct kprobe *ap)
755 struct optimized_kprobe *op;
758 * Unused kprobe MUST be on the way of delayed unoptimizing (means
759 * there is still a relative jump) and disabled.
761 op = container_of(ap, struct optimized_kprobe, kp);
762 WARN_ON_ONCE(list_empty(&op->list));
763 /* Enable the probe again */
764 ap->flags &= ~KPROBE_FLAG_DISABLED;
765 /* Optimize it again (remove from op->list) */
766 if (!kprobe_optready(ap))
773 /* Remove optimized instructions */
774 static void kill_optimized_kprobe(struct kprobe *p)
776 struct optimized_kprobe *op;
778 op = container_of(p, struct optimized_kprobe, kp);
779 if (!list_empty(&op->list))
780 /* Dequeue from the (un)optimization queue */
781 list_del_init(&op->list);
782 op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
784 if (kprobe_unused(p)) {
786 * Unused kprobe is on unoptimizing or freeing list. We move it
787 * to freeing_list and let the kprobe_optimizer() remove it from
788 * the kprobe hash list and free it.
790 if (optprobe_queued_unopt(op))
791 list_move(&op->list, &freeing_list);
794 /* Don't touch the code, because it is already freed. */
795 arch_remove_optimized_kprobe(op);
799 void __prepare_optimized_kprobe(struct optimized_kprobe *op, struct kprobe *p)
801 if (!kprobe_ftrace(p))
802 arch_prepare_optimized_kprobe(op, p);
805 /* Try to prepare optimized instructions */
806 static void prepare_optimized_kprobe(struct kprobe *p)
808 struct optimized_kprobe *op;
810 op = container_of(p, struct optimized_kprobe, kp);
811 __prepare_optimized_kprobe(op, p);
814 /* Allocate new optimized_kprobe and try to prepare optimized instructions */
815 static struct kprobe *alloc_aggr_kprobe(struct kprobe *p)
817 struct optimized_kprobe *op;
819 op = kzalloc(sizeof(struct optimized_kprobe), GFP_KERNEL);
823 INIT_LIST_HEAD(&op->list);
824 op->kp.addr = p->addr;
825 __prepare_optimized_kprobe(op, p);
830 static void init_aggr_kprobe(struct kprobe *ap, struct kprobe *p);
833 * Prepare an optimized_kprobe and optimize it
834 * NOTE: p must be a normal registered kprobe
836 static void try_to_optimize_kprobe(struct kprobe *p)
839 struct optimized_kprobe *op;
841 /* Impossible to optimize ftrace-based kprobe */
842 if (kprobe_ftrace(p))
845 /* For preparing optimization, jump_label_text_reserved() is called */
848 mutex_lock(&text_mutex);
850 ap = alloc_aggr_kprobe(p);
854 op = container_of(ap, struct optimized_kprobe, kp);
855 if (!arch_prepared_optinsn(&op->optinsn)) {
856 /* If failed to setup optimizing, fallback to kprobe */
857 arch_remove_optimized_kprobe(op);
862 init_aggr_kprobe(ap, p);
863 optimize_kprobe(ap); /* This just kicks optimizer thread */
866 mutex_unlock(&text_mutex);
871 static void optimize_all_kprobes(void)
873 struct hlist_head *head;
877 mutex_lock(&kprobe_mutex);
878 /* If optimization is already allowed, just return */
879 if (kprobes_allow_optimization)
883 kprobes_allow_optimization = true;
884 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
885 head = &kprobe_table[i];
886 hlist_for_each_entry(p, head, hlist)
887 if (!kprobe_disabled(p))
891 printk(KERN_INFO "Kprobes globally optimized\n");
893 mutex_unlock(&kprobe_mutex);
897 static void unoptimize_all_kprobes(void)
899 struct hlist_head *head;
903 mutex_lock(&kprobe_mutex);
904 /* If optimization is already prohibited, just return */
905 if (!kprobes_allow_optimization) {
906 mutex_unlock(&kprobe_mutex);
911 kprobes_allow_optimization = false;
912 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
913 head = &kprobe_table[i];
914 hlist_for_each_entry(p, head, hlist) {
915 if (!kprobe_disabled(p))
916 unoptimize_kprobe(p, false);
920 mutex_unlock(&kprobe_mutex);
922 /* Wait for unoptimizing completion */
923 wait_for_kprobe_optimizer();
924 printk(KERN_INFO "Kprobes globally unoptimized\n");
927 static DEFINE_MUTEX(kprobe_sysctl_mutex);
928 int sysctl_kprobes_optimization;
929 int proc_kprobes_optimization_handler(struct ctl_table *table, int write,
930 void *buffer, size_t *length,
935 mutex_lock(&kprobe_sysctl_mutex);
936 sysctl_kprobes_optimization = kprobes_allow_optimization ? 1 : 0;
937 ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
939 if (sysctl_kprobes_optimization)
940 optimize_all_kprobes();
942 unoptimize_all_kprobes();
943 mutex_unlock(&kprobe_sysctl_mutex);
947 #endif /* CONFIG_SYSCTL */
949 /* Put a breakpoint for a probe. Must be called with text_mutex locked */
950 static void __arm_kprobe(struct kprobe *p)
954 /* Check collision with other optimized kprobes */
955 _p = get_optimized_kprobe((unsigned long)p->addr);
957 /* Fallback to unoptimized kprobe */
958 unoptimize_kprobe(_p, true);
961 optimize_kprobe(p); /* Try to optimize (add kprobe to a list) */
964 /* Remove the breakpoint of a probe. Must be called with text_mutex locked */
965 static void __disarm_kprobe(struct kprobe *p, bool reopt)
969 /* Try to unoptimize */
970 unoptimize_kprobe(p, kprobes_all_disarmed);
972 if (!kprobe_queued(p)) {
973 arch_disarm_kprobe(p);
974 /* If another kprobe was blocked, optimize it. */
975 _p = get_optimized_kprobe((unsigned long)p->addr);
976 if (unlikely(_p) && reopt)
979 /* TODO: reoptimize others after unoptimized this probe */
982 #else /* !CONFIG_OPTPROBES */
984 #define optimize_kprobe(p) do {} while (0)
985 #define unoptimize_kprobe(p, f) do {} while (0)
986 #define kill_optimized_kprobe(p) do {} while (0)
987 #define prepare_optimized_kprobe(p) do {} while (0)
988 #define try_to_optimize_kprobe(p) do {} while (0)
989 #define __arm_kprobe(p) arch_arm_kprobe(p)
990 #define __disarm_kprobe(p, o) arch_disarm_kprobe(p)
991 #define kprobe_disarmed(p) kprobe_disabled(p)
992 #define wait_for_kprobe_optimizer() do {} while (0)
994 static int reuse_unused_kprobe(struct kprobe *ap)
997 * If the optimized kprobe is NOT supported, the aggr kprobe is
998 * released at the same time that the last aggregated kprobe is
1000 * Thus there should be no chance to reuse unused kprobe.
1002 printk(KERN_ERR "Error: There should be no unused kprobe here.\n");
1006 static void free_aggr_kprobe(struct kprobe *p)
1008 arch_remove_kprobe(p);
1012 static struct kprobe *alloc_aggr_kprobe(struct kprobe *p)
1014 return kzalloc(sizeof(struct kprobe), GFP_KERNEL);
1016 #endif /* CONFIG_OPTPROBES */
1018 #ifdef CONFIG_KPROBES_ON_FTRACE
1019 static struct ftrace_ops kprobe_ftrace_ops __read_mostly = {
1020 .func = kprobe_ftrace_handler,
1021 .flags = FTRACE_OPS_FL_SAVE_REGS,
1024 static struct ftrace_ops kprobe_ipmodify_ops __read_mostly = {
1025 .func = kprobe_ftrace_handler,
1026 .flags = FTRACE_OPS_FL_SAVE_REGS | FTRACE_OPS_FL_IPMODIFY,
1029 static int kprobe_ipmodify_enabled;
1030 static int kprobe_ftrace_enabled;
1032 /* Must ensure p->addr is really on ftrace */
1033 static int prepare_kprobe(struct kprobe *p)
1035 if (!kprobe_ftrace(p))
1036 return arch_prepare_kprobe(p);
1038 return arch_prepare_kprobe_ftrace(p);
1041 /* Caller must lock kprobe_mutex */
1042 static int __arm_kprobe_ftrace(struct kprobe *p, struct ftrace_ops *ops,
1047 ret = ftrace_set_filter_ip(ops, (unsigned long)p->addr, 0, 0);
1049 pr_debug("Failed to arm kprobe-ftrace at %pS (%d)\n",
1055 ret = register_ftrace_function(ops);
1057 pr_debug("Failed to init kprobe-ftrace (%d)\n", ret);
1067 * At this point, sinec ops is not registered, we should be sefe from
1068 * registering empty filter.
1070 ftrace_set_filter_ip(ops, (unsigned long)p->addr, 1, 0);
1074 static int arm_kprobe_ftrace(struct kprobe *p)
1076 bool ipmodify = (p->post_handler != NULL);
1078 return __arm_kprobe_ftrace(p,
1079 ipmodify ? &kprobe_ipmodify_ops : &kprobe_ftrace_ops,
1080 ipmodify ? &kprobe_ipmodify_enabled : &kprobe_ftrace_enabled);
1083 /* Caller must lock kprobe_mutex */
1084 static int __disarm_kprobe_ftrace(struct kprobe *p, struct ftrace_ops *ops,
1090 ret = unregister_ftrace_function(ops);
1091 if (WARN(ret < 0, "Failed to unregister kprobe-ftrace (%d)\n", ret))
1097 ret = ftrace_set_filter_ip(ops, (unsigned long)p->addr, 1, 0);
1098 WARN_ONCE(ret < 0, "Failed to disarm kprobe-ftrace at %pS (%d)\n",
1103 static int disarm_kprobe_ftrace(struct kprobe *p)
1105 bool ipmodify = (p->post_handler != NULL);
1107 return __disarm_kprobe_ftrace(p,
1108 ipmodify ? &kprobe_ipmodify_ops : &kprobe_ftrace_ops,
1109 ipmodify ? &kprobe_ipmodify_enabled : &kprobe_ftrace_enabled);
1111 #else /* !CONFIG_KPROBES_ON_FTRACE */
1112 static inline int prepare_kprobe(struct kprobe *p)
1114 return arch_prepare_kprobe(p);
1117 static inline int arm_kprobe_ftrace(struct kprobe *p)
1122 static inline int disarm_kprobe_ftrace(struct kprobe *p)
1128 /* Arm a kprobe with text_mutex */
1129 static int arm_kprobe(struct kprobe *kp)
1131 if (unlikely(kprobe_ftrace(kp)))
1132 return arm_kprobe_ftrace(kp);
1135 mutex_lock(&text_mutex);
1137 mutex_unlock(&text_mutex);
1143 /* Disarm a kprobe with text_mutex */
1144 static int disarm_kprobe(struct kprobe *kp, bool reopt)
1146 if (unlikely(kprobe_ftrace(kp)))
1147 return disarm_kprobe_ftrace(kp);
1150 mutex_lock(&text_mutex);
1151 __disarm_kprobe(kp, reopt);
1152 mutex_unlock(&text_mutex);
1159 * Aggregate handlers for multiple kprobes support - these handlers
1160 * take care of invoking the individual kprobe handlers on p->list
1162 static int aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
1166 list_for_each_entry_rcu(kp, &p->list, list) {
1167 if (kp->pre_handler && likely(!kprobe_disabled(kp))) {
1168 set_kprobe_instance(kp);
1169 if (kp->pre_handler(kp, regs))
1172 reset_kprobe_instance();
1176 NOKPROBE_SYMBOL(aggr_pre_handler);
1178 static void aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
1179 unsigned long flags)
1183 list_for_each_entry_rcu(kp, &p->list, list) {
1184 if (kp->post_handler && likely(!kprobe_disabled(kp))) {
1185 set_kprobe_instance(kp);
1186 kp->post_handler(kp, regs, flags);
1187 reset_kprobe_instance();
1191 NOKPROBE_SYMBOL(aggr_post_handler);
1193 static int aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
1196 struct kprobe *cur = __this_cpu_read(kprobe_instance);
1199 * if we faulted "during" the execution of a user specified
1200 * probe handler, invoke just that probe's fault handler
1202 if (cur && cur->fault_handler) {
1203 if (cur->fault_handler(cur, regs, trapnr))
1208 NOKPROBE_SYMBOL(aggr_fault_handler);
1210 /* Walks the list and increments nmissed count for multiprobe case */
1211 void kprobes_inc_nmissed_count(struct kprobe *p)
1214 if (!kprobe_aggrprobe(p)) {
1217 list_for_each_entry_rcu(kp, &p->list, list)
1222 NOKPROBE_SYMBOL(kprobes_inc_nmissed_count);
1224 static void recycle_rp_inst(struct kretprobe_instance *ri)
1226 struct kretprobe *rp = ri->rp;
1228 /* remove rp inst off the rprobe_inst_table */
1229 hlist_del(&ri->hlist);
1230 INIT_HLIST_NODE(&ri->hlist);
1232 raw_spin_lock(&rp->lock);
1233 hlist_add_head(&ri->hlist, &rp->free_instances);
1234 raw_spin_unlock(&rp->lock);
1238 NOKPROBE_SYMBOL(recycle_rp_inst);
1240 static void kretprobe_hash_lock(struct task_struct *tsk,
1241 struct hlist_head **head, unsigned long *flags)
1242 __acquires(hlist_lock)
1244 unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS);
1245 raw_spinlock_t *hlist_lock;
1247 *head = &kretprobe_inst_table[hash];
1248 hlist_lock = kretprobe_table_lock_ptr(hash);
1250 * Nested is a workaround that will soon not be needed.
1251 * There's other protections that make sure the same lock
1252 * is not taken on the same CPU that lockdep is unaware of.
1253 * Differentiate when it is taken in NMI context.
1255 raw_spin_lock_irqsave_nested(hlist_lock, *flags, !!in_nmi());
1257 NOKPROBE_SYMBOL(kretprobe_hash_lock);
1259 static void kretprobe_table_lock(unsigned long hash,
1260 unsigned long *flags)
1261 __acquires(hlist_lock)
1263 raw_spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash);
1265 * Nested is a workaround that will soon not be needed.
1266 * There's other protections that make sure the same lock
1267 * is not taken on the same CPU that lockdep is unaware of.
1268 * Differentiate when it is taken in NMI context.
1270 raw_spin_lock_irqsave_nested(hlist_lock, *flags, !!in_nmi());
1272 NOKPROBE_SYMBOL(kretprobe_table_lock);
1274 static void kretprobe_hash_unlock(struct task_struct *tsk,
1275 unsigned long *flags)
1276 __releases(hlist_lock)
1278 unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS);
1279 raw_spinlock_t *hlist_lock;
1281 hlist_lock = kretprobe_table_lock_ptr(hash);
1282 raw_spin_unlock_irqrestore(hlist_lock, *flags);
1284 NOKPROBE_SYMBOL(kretprobe_hash_unlock);
1286 static void kretprobe_table_unlock(unsigned long hash,
1287 unsigned long *flags)
1288 __releases(hlist_lock)
1290 raw_spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash);
1291 raw_spin_unlock_irqrestore(hlist_lock, *flags);
1293 NOKPROBE_SYMBOL(kretprobe_table_unlock);
1295 static struct kprobe kprobe_busy = {
1296 .addr = (void *) get_kprobe,
1299 void kprobe_busy_begin(void)
1301 struct kprobe_ctlblk *kcb;
1304 __this_cpu_write(current_kprobe, &kprobe_busy);
1305 kcb = get_kprobe_ctlblk();
1306 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
1309 void kprobe_busy_end(void)
1311 __this_cpu_write(current_kprobe, NULL);
1316 * This function is called from finish_task_switch when task tk becomes dead,
1317 * so that we can recycle any function-return probe instances associated
1318 * with this task. These left over instances represent probed functions
1319 * that have been called but will never return.
1321 void kprobe_flush_task(struct task_struct *tk)
1323 struct kretprobe_instance *ri;
1324 struct hlist_head *head;
1325 struct hlist_node *tmp;
1326 unsigned long hash, flags = 0;
1328 if (unlikely(!kprobes_initialized))
1329 /* Early boot. kretprobe_table_locks not yet initialized. */
1332 kprobe_busy_begin();
1334 hash = hash_ptr(tk, KPROBE_HASH_BITS);
1335 head = &kretprobe_inst_table[hash];
1336 kretprobe_table_lock(hash, &flags);
1337 hlist_for_each_entry_safe(ri, tmp, head, hlist) {
1339 recycle_rp_inst(ri);
1341 kretprobe_table_unlock(hash, &flags);
1345 NOKPROBE_SYMBOL(kprobe_flush_task);
1347 static inline void free_rp_inst(struct kretprobe *rp)
1349 struct kretprobe_instance *ri;
1350 struct hlist_node *next;
1352 hlist_for_each_entry_safe(ri, next, &rp->free_instances, hlist) {
1353 hlist_del(&ri->hlist);
1358 static void cleanup_rp_inst(struct kretprobe *rp)
1360 unsigned long flags, hash;
1361 struct kretprobe_instance *ri;
1362 struct hlist_node *next;
1363 struct hlist_head *head;
1365 /* To avoid recursive kretprobe by NMI, set kprobe busy here */
1366 kprobe_busy_begin();
1367 for (hash = 0; hash < KPROBE_TABLE_SIZE; hash++) {
1368 kretprobe_table_lock(hash, &flags);
1369 head = &kretprobe_inst_table[hash];
1370 hlist_for_each_entry_safe(ri, next, head, hlist) {
1374 kretprobe_table_unlock(hash, &flags);
1380 NOKPROBE_SYMBOL(cleanup_rp_inst);
1382 /* Add the new probe to ap->list */
1383 static int add_new_kprobe(struct kprobe *ap, struct kprobe *p)
1385 if (p->post_handler)
1386 unoptimize_kprobe(ap, true); /* Fall back to normal kprobe */
1388 list_add_rcu(&p->list, &ap->list);
1389 if (p->post_handler && !ap->post_handler)
1390 ap->post_handler = aggr_post_handler;
1396 * Fill in the required fields of the "manager kprobe". Replace the
1397 * earlier kprobe in the hlist with the manager kprobe
1399 static void init_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
1401 /* Copy p's insn slot to ap */
1403 flush_insn_slot(ap);
1405 ap->flags = p->flags & ~KPROBE_FLAG_OPTIMIZED;
1406 ap->pre_handler = aggr_pre_handler;
1407 ap->fault_handler = aggr_fault_handler;
1408 /* We don't care the kprobe which has gone. */
1409 if (p->post_handler && !kprobe_gone(p))
1410 ap->post_handler = aggr_post_handler;
1412 INIT_LIST_HEAD(&ap->list);
1413 INIT_HLIST_NODE(&ap->hlist);
1415 list_add_rcu(&p->list, &ap->list);
1416 hlist_replace_rcu(&p->hlist, &ap->hlist);
1420 * This is the second or subsequent kprobe at the address - handle
1423 static int register_aggr_kprobe(struct kprobe *orig_p, struct kprobe *p)
1426 struct kprobe *ap = orig_p;
1430 /* For preparing optimization, jump_label_text_reserved() is called */
1432 mutex_lock(&text_mutex);
1434 if (!kprobe_aggrprobe(orig_p)) {
1435 /* If orig_p is not an aggr_kprobe, create new aggr_kprobe. */
1436 ap = alloc_aggr_kprobe(orig_p);
1441 init_aggr_kprobe(ap, orig_p);
1442 } else if (kprobe_unused(ap)) {
1443 /* This probe is going to die. Rescue it */
1444 ret = reuse_unused_kprobe(ap);
1449 if (kprobe_gone(ap)) {
1451 * Attempting to insert new probe at the same location that
1452 * had a probe in the module vaddr area which already
1453 * freed. So, the instruction slot has already been
1454 * released. We need a new slot for the new probe.
1456 ret = arch_prepare_kprobe(ap);
1459 * Even if fail to allocate new slot, don't need to
1460 * free aggr_probe. It will be used next time, or
1461 * freed by unregister_kprobe.
1465 /* Prepare optimized instructions if possible. */
1466 prepare_optimized_kprobe(ap);
1469 * Clear gone flag to prevent allocating new slot again, and
1470 * set disabled flag because it is not armed yet.
1472 ap->flags = (ap->flags & ~KPROBE_FLAG_GONE)
1473 | KPROBE_FLAG_DISABLED;
1476 /* Copy ap's insn slot to p */
1478 ret = add_new_kprobe(ap, p);
1481 mutex_unlock(&text_mutex);
1482 jump_label_unlock();
1485 if (ret == 0 && kprobe_disabled(ap) && !kprobe_disabled(p)) {
1486 ap->flags &= ~KPROBE_FLAG_DISABLED;
1487 if (!kprobes_all_disarmed) {
1488 /* Arm the breakpoint again. */
1489 ret = arm_kprobe(ap);
1491 ap->flags |= KPROBE_FLAG_DISABLED;
1492 list_del_rcu(&p->list);
1500 bool __weak arch_within_kprobe_blacklist(unsigned long addr)
1502 /* The __kprobes marked functions and entry code must not be probed */
1503 return addr >= (unsigned long)__kprobes_text_start &&
1504 addr < (unsigned long)__kprobes_text_end;
1507 static bool __within_kprobe_blacklist(unsigned long addr)
1509 struct kprobe_blacklist_entry *ent;
1511 if (arch_within_kprobe_blacklist(addr))
1514 * If there exists a kprobe_blacklist, verify and
1515 * fail any probe registration in the prohibited area
1517 list_for_each_entry(ent, &kprobe_blacklist, list) {
1518 if (addr >= ent->start_addr && addr < ent->end_addr)
1524 bool within_kprobe_blacklist(unsigned long addr)
1526 char symname[KSYM_NAME_LEN], *p;
1528 if (__within_kprobe_blacklist(addr))
1531 /* Check if the address is on a suffixed-symbol */
1532 if (!lookup_symbol_name(addr, symname)) {
1533 p = strchr(symname, '.');
1537 addr = (unsigned long)kprobe_lookup_name(symname, 0);
1539 return __within_kprobe_blacklist(addr);
1545 * If we have a symbol_name argument, look it up and add the offset field
1546 * to it. This way, we can specify a relative address to a symbol.
1547 * This returns encoded errors if it fails to look up symbol or invalid
1548 * combination of parameters.
1550 static kprobe_opcode_t *_kprobe_addr(kprobe_opcode_t *addr,
1551 const char *symbol_name, unsigned int offset)
1553 if ((symbol_name && addr) || (!symbol_name && !addr))
1557 addr = kprobe_lookup_name(symbol_name, offset);
1559 return ERR_PTR(-ENOENT);
1562 addr = (kprobe_opcode_t *)(((char *)addr) + offset);
1567 return ERR_PTR(-EINVAL);
1570 static kprobe_opcode_t *kprobe_addr(struct kprobe *p)
1572 return _kprobe_addr(p->addr, p->symbol_name, p->offset);
1575 /* Check passed kprobe is valid and return kprobe in kprobe_table. */
1576 static struct kprobe *__get_valid_kprobe(struct kprobe *p)
1578 struct kprobe *ap, *list_p;
1580 lockdep_assert_held(&kprobe_mutex);
1582 ap = get_kprobe(p->addr);
1587 list_for_each_entry(list_p, &ap->list, list)
1589 /* kprobe p is a valid probe */
1597 /* Return error if the kprobe is being re-registered */
1598 static inline int check_kprobe_rereg(struct kprobe *p)
1602 mutex_lock(&kprobe_mutex);
1603 if (__get_valid_kprobe(p))
1605 mutex_unlock(&kprobe_mutex);
1610 int __weak arch_check_ftrace_location(struct kprobe *p)
1612 unsigned long ftrace_addr;
1614 ftrace_addr = ftrace_location((unsigned long)p->addr);
1616 #ifdef CONFIG_KPROBES_ON_FTRACE
1617 /* Given address is not on the instruction boundary */
1618 if ((unsigned long)p->addr != ftrace_addr)
1620 p->flags |= KPROBE_FLAG_FTRACE;
1621 #else /* !CONFIG_KPROBES_ON_FTRACE */
1628 static bool is_cfi_preamble_symbol(unsigned long addr)
1630 char symbuf[KSYM_NAME_LEN];
1632 if (lookup_symbol_name(addr, symbuf))
1635 return str_has_prefix("__cfi_", symbuf) ||
1636 str_has_prefix("__pfx_", symbuf);
1639 static int check_kprobe_address_safe(struct kprobe *p,
1640 struct module **probed_mod)
1644 ret = arch_check_ftrace_location(p);
1650 /* Ensure it is not in reserved area nor out of text */
1651 if (!(core_kernel_text((unsigned long) p->addr) ||
1652 is_module_text_address((unsigned long) p->addr)) ||
1653 in_gate_area_no_mm((unsigned long) p->addr) ||
1654 within_kprobe_blacklist((unsigned long) p->addr) ||
1655 jump_label_text_reserved(p->addr, p->addr) ||
1656 static_call_text_reserved(p->addr, p->addr) ||
1657 find_bug((unsigned long)p->addr) ||
1658 is_cfi_preamble_symbol((unsigned long)p->addr)) {
1663 /* Check if are we probing a module */
1664 *probed_mod = __module_text_address((unsigned long) p->addr);
1667 * We must hold a refcount of the probed module while updating
1668 * its code to prohibit unexpected unloading.
1670 if (unlikely(!try_module_get(*probed_mod))) {
1676 * If the module freed .init.text, we couldn't insert
1679 if (within_module_init((unsigned long)p->addr, *probed_mod) &&
1680 (*probed_mod)->state != MODULE_STATE_COMING) {
1681 module_put(*probed_mod);
1688 jump_label_unlock();
1693 int register_kprobe(struct kprobe *p)
1696 struct kprobe *old_p;
1697 struct module *probed_mod;
1698 kprobe_opcode_t *addr;
1700 /* Adjust probe address from symbol */
1701 addr = kprobe_addr(p);
1703 return PTR_ERR(addr);
1706 ret = check_kprobe_rereg(p);
1710 /* User can pass only KPROBE_FLAG_DISABLED to register_kprobe */
1711 p->flags &= KPROBE_FLAG_DISABLED;
1713 INIT_LIST_HEAD(&p->list);
1715 ret = check_kprobe_address_safe(p, &probed_mod);
1719 mutex_lock(&kprobe_mutex);
1721 old_p = get_kprobe(p->addr);
1723 /* Since this may unoptimize old_p, locking text_mutex. */
1724 ret = register_aggr_kprobe(old_p, p);
1729 /* Prevent text modification */
1730 mutex_lock(&text_mutex);
1731 ret = prepare_kprobe(p);
1732 mutex_unlock(&text_mutex);
1737 INIT_HLIST_NODE(&p->hlist);
1738 hlist_add_head_rcu(&p->hlist,
1739 &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
1741 if (!kprobes_all_disarmed && !kprobe_disabled(p)) {
1742 ret = arm_kprobe(p);
1744 hlist_del_rcu(&p->hlist);
1750 /* Try to optimize kprobe */
1751 try_to_optimize_kprobe(p);
1753 mutex_unlock(&kprobe_mutex);
1756 module_put(probed_mod);
1760 EXPORT_SYMBOL_GPL(register_kprobe);
1762 /* Check if all probes on the aggrprobe are disabled */
1763 static int aggr_kprobe_disabled(struct kprobe *ap)
1767 lockdep_assert_held(&kprobe_mutex);
1769 list_for_each_entry(kp, &ap->list, list)
1770 if (!kprobe_disabled(kp))
1772 * There is an active probe on the list.
1773 * We can't disable this ap.
1780 /* Disable one kprobe: Make sure called under kprobe_mutex is locked */
1781 static struct kprobe *__disable_kprobe(struct kprobe *p)
1783 struct kprobe *orig_p;
1786 /* Get an original kprobe for return */
1787 orig_p = __get_valid_kprobe(p);
1788 if (unlikely(orig_p == NULL))
1789 return ERR_PTR(-EINVAL);
1791 if (!kprobe_disabled(p)) {
1792 /* Disable probe if it is a child probe */
1794 p->flags |= KPROBE_FLAG_DISABLED;
1796 /* Try to disarm and disable this/parent probe */
1797 if (p == orig_p || aggr_kprobe_disabled(orig_p)) {
1799 * Don't be lazy here. Even if 'kprobes_all_disarmed'
1800 * is false, 'orig_p' might not have been armed yet.
1801 * Note arm_all_kprobes() __tries__ to arm all kprobes
1802 * on the best effort basis.
1804 if (!kprobes_all_disarmed && !kprobe_disabled(orig_p)) {
1805 ret = disarm_kprobe(orig_p, true);
1807 p->flags &= ~KPROBE_FLAG_DISABLED;
1808 return ERR_PTR(ret);
1811 orig_p->flags |= KPROBE_FLAG_DISABLED;
1819 * Unregister a kprobe without a scheduler synchronization.
1821 static int __unregister_kprobe_top(struct kprobe *p)
1823 struct kprobe *ap, *list_p;
1825 /* Disable kprobe. This will disarm it if needed. */
1826 ap = __disable_kprobe(p);
1832 * This probe is an independent(and non-optimized) kprobe
1833 * (not an aggrprobe). Remove from the hash list.
1837 /* Following process expects this probe is an aggrprobe */
1838 WARN_ON(!kprobe_aggrprobe(ap));
1840 if (list_is_singular(&ap->list) && kprobe_disarmed(ap))
1842 * !disarmed could be happen if the probe is under delayed
1847 /* If disabling probe has special handlers, update aggrprobe */
1848 if (p->post_handler && !kprobe_gone(p)) {
1849 list_for_each_entry(list_p, &ap->list, list) {
1850 if ((list_p != p) && (list_p->post_handler))
1854 * For the kprobe-on-ftrace case, we keep the
1855 * post_handler setting to identify this aggrprobe
1856 * armed with kprobe_ipmodify_ops.
1858 if (!kprobe_ftrace(ap))
1859 ap->post_handler = NULL;
1863 * Remove from the aggrprobe: this path will do nothing in
1864 * __unregister_kprobe_bottom().
1866 list_del_rcu(&p->list);
1867 if (!kprobe_disabled(ap) && !kprobes_all_disarmed)
1869 * Try to optimize this probe again, because post
1870 * handler may have been changed.
1872 optimize_kprobe(ap);
1877 hlist_del_rcu(&ap->hlist);
1881 static void __unregister_kprobe_bottom(struct kprobe *p)
1885 if (list_empty(&p->list))
1886 /* This is an independent kprobe */
1887 arch_remove_kprobe(p);
1888 else if (list_is_singular(&p->list)) {
1889 /* This is the last child of an aggrprobe */
1890 ap = list_entry(p->list.next, struct kprobe, list);
1892 free_aggr_kprobe(ap);
1894 /* Otherwise, do nothing. */
1897 int register_kprobes(struct kprobe **kps, int num)
1903 for (i = 0; i < num; i++) {
1904 ret = register_kprobe(kps[i]);
1907 unregister_kprobes(kps, i);
1913 EXPORT_SYMBOL_GPL(register_kprobes);
1915 void unregister_kprobe(struct kprobe *p)
1917 unregister_kprobes(&p, 1);
1919 EXPORT_SYMBOL_GPL(unregister_kprobe);
1921 void unregister_kprobes(struct kprobe **kps, int num)
1927 mutex_lock(&kprobe_mutex);
1928 for (i = 0; i < num; i++)
1929 if (__unregister_kprobe_top(kps[i]) < 0)
1930 kps[i]->addr = NULL;
1931 mutex_unlock(&kprobe_mutex);
1934 for (i = 0; i < num; i++)
1936 __unregister_kprobe_bottom(kps[i]);
1938 EXPORT_SYMBOL_GPL(unregister_kprobes);
1940 int __weak kprobe_exceptions_notify(struct notifier_block *self,
1941 unsigned long val, void *data)
1945 NOKPROBE_SYMBOL(kprobe_exceptions_notify);
1947 static struct notifier_block kprobe_exceptions_nb = {
1948 .notifier_call = kprobe_exceptions_notify,
1949 .priority = 0x7fffffff /* we need to be notified first */
1952 unsigned long __weak arch_deref_entry_point(void *entry)
1954 return (unsigned long)entry;
1957 #ifdef CONFIG_KRETPROBES
1959 unsigned long __kretprobe_trampoline_handler(struct pt_regs *regs,
1960 void *trampoline_address,
1961 void *frame_pointer)
1963 struct kretprobe_instance *ri = NULL, *last = NULL;
1964 struct hlist_head *head;
1965 struct hlist_node *tmp;
1966 unsigned long flags;
1967 kprobe_opcode_t *correct_ret_addr = NULL;
1968 bool skipped = false;
1970 kretprobe_hash_lock(current, &head, &flags);
1973 * It is possible to have multiple instances associated with a given
1974 * task either because multiple functions in the call path have
1975 * return probes installed on them, and/or more than one
1976 * return probe was registered for a target function.
1978 * We can handle this because:
1979 * - instances are always pushed into the head of the list
1980 * - when multiple return probes are registered for the same
1981 * function, the (chronologically) first instance's ret_addr
1982 * will be the real return address, and all the rest will
1983 * point to kretprobe_trampoline.
1985 hlist_for_each_entry(ri, head, hlist) {
1986 if (ri->task != current)
1987 /* another task is sharing our hash bucket */
1990 * Return probes must be pushed on this hash list correct
1991 * order (same as return order) so that it can be popped
1992 * correctly. However, if we find it is pushed it incorrect
1993 * order, this means we find a function which should not be
1994 * probed, because the wrong order entry is pushed on the
1995 * path of processing other kretprobe itself.
1997 if (ri->fp != frame_pointer) {
1999 pr_warn("kretprobe is stacked incorrectly. Trying to fixup.\n");
2004 correct_ret_addr = ri->ret_addr;
2006 pr_warn("%ps must be blacklisted because of incorrect kretprobe order\n",
2009 if (correct_ret_addr != trampoline_address)
2011 * This is the real return address. Any other
2012 * instances associated with this task are for
2013 * other calls deeper on the call stack
2018 BUG_ON(!correct_ret_addr || (correct_ret_addr == trampoline_address));
2021 hlist_for_each_entry_safe(ri, tmp, head, hlist) {
2022 if (ri->task != current)
2023 /* another task is sharing our hash bucket */
2025 if (ri->fp != frame_pointer)
2028 if (ri->rp && ri->rp->handler) {
2029 struct kprobe *prev = kprobe_running();
2031 __this_cpu_write(current_kprobe, &ri->rp->kp);
2032 ri->ret_addr = correct_ret_addr;
2033 ri->rp->handler(ri, regs);
2034 __this_cpu_write(current_kprobe, prev);
2037 recycle_rp_inst(ri);
2043 kretprobe_hash_unlock(current, &flags);
2045 return (unsigned long)correct_ret_addr;
2047 NOKPROBE_SYMBOL(__kretprobe_trampoline_handler)
2050 * This kprobe pre_handler is registered with every kretprobe. When probe
2051 * hits it will set up the return probe.
2053 static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
2055 struct kretprobe *rp = container_of(p, struct kretprobe, kp);
2056 unsigned long hash, flags = 0;
2057 struct kretprobe_instance *ri;
2059 /* TODO: consider to only swap the RA after the last pre_handler fired */
2060 hash = hash_ptr(current, KPROBE_HASH_BITS);
2062 * Nested is a workaround that will soon not be needed.
2063 * There's other protections that make sure the same lock
2064 * is not taken on the same CPU that lockdep is unaware of.
2066 raw_spin_lock_irqsave_nested(&rp->lock, flags, 1);
2067 if (!hlist_empty(&rp->free_instances)) {
2068 ri = hlist_entry(rp->free_instances.first,
2069 struct kretprobe_instance, hlist);
2070 hlist_del(&ri->hlist);
2071 raw_spin_unlock_irqrestore(&rp->lock, flags);
2076 if (rp->entry_handler && rp->entry_handler(ri, regs)) {
2077 raw_spin_lock_irqsave_nested(&rp->lock, flags, 1);
2078 hlist_add_head(&ri->hlist, &rp->free_instances);
2079 raw_spin_unlock_irqrestore(&rp->lock, flags);
2083 arch_prepare_kretprobe(ri, regs);
2085 /* XXX(hch): why is there no hlist_move_head? */
2086 INIT_HLIST_NODE(&ri->hlist);
2087 kretprobe_table_lock(hash, &flags);
2088 hlist_add_head(&ri->hlist, &kretprobe_inst_table[hash]);
2089 kretprobe_table_unlock(hash, &flags);
2092 raw_spin_unlock_irqrestore(&rp->lock, flags);
2096 NOKPROBE_SYMBOL(pre_handler_kretprobe);
2098 bool __weak arch_kprobe_on_func_entry(unsigned long offset)
2104 * kprobe_on_func_entry() -- check whether given address is function entry
2105 * @addr: Target address
2106 * @sym: Target symbol name
2107 * @offset: The offset from the symbol or the address
2109 * This checks whether the given @addr+@offset or @sym+@offset is on the
2110 * function entry address or not.
2111 * This returns 0 if it is the function entry, or -EINVAL if it is not.
2112 * And also it returns -ENOENT if it fails the symbol or address lookup.
2113 * Caller must pass @addr or @sym (either one must be NULL), or this
2116 int kprobe_on_func_entry(kprobe_opcode_t *addr, const char *sym, unsigned long offset)
2118 kprobe_opcode_t *kp_addr = _kprobe_addr(addr, sym, offset);
2120 if (IS_ERR(kp_addr))
2121 return PTR_ERR(kp_addr);
2123 if (!kallsyms_lookup_size_offset((unsigned long)kp_addr, NULL, &offset))
2126 if (!arch_kprobe_on_func_entry(offset))
2132 int register_kretprobe(struct kretprobe *rp)
2135 struct kretprobe_instance *inst;
2139 ret = kprobe_on_func_entry(rp->kp.addr, rp->kp.symbol_name, rp->kp.offset);
2143 /* If only rp->kp.addr is specified, check reregistering kprobes */
2144 if (rp->kp.addr && check_kprobe_rereg(&rp->kp))
2147 if (kretprobe_blacklist_size) {
2148 addr = kprobe_addr(&rp->kp);
2150 return PTR_ERR(addr);
2152 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
2153 if (kretprobe_blacklist[i].addr == addr)
2158 if (rp->data_size > KRETPROBE_MAX_DATA_SIZE)
2161 rp->kp.pre_handler = pre_handler_kretprobe;
2162 rp->kp.post_handler = NULL;
2163 rp->kp.fault_handler = NULL;
2165 /* Pre-allocate memory for max kretprobe instances */
2166 if (rp->maxactive <= 0) {
2167 #ifdef CONFIG_PREEMPTION
2168 rp->maxactive = max_t(unsigned int, 10, 2*num_possible_cpus());
2170 rp->maxactive = num_possible_cpus();
2173 raw_spin_lock_init(&rp->lock);
2174 INIT_HLIST_HEAD(&rp->free_instances);
2175 for (i = 0; i < rp->maxactive; i++) {
2176 inst = kmalloc(sizeof(struct kretprobe_instance) +
2177 rp->data_size, GFP_KERNEL);
2182 INIT_HLIST_NODE(&inst->hlist);
2183 hlist_add_head(&inst->hlist, &rp->free_instances);
2187 /* Establish function entry probe point */
2188 ret = register_kprobe(&rp->kp);
2193 EXPORT_SYMBOL_GPL(register_kretprobe);
2195 int register_kretprobes(struct kretprobe **rps, int num)
2201 for (i = 0; i < num; i++) {
2202 ret = register_kretprobe(rps[i]);
2205 unregister_kretprobes(rps, i);
2211 EXPORT_SYMBOL_GPL(register_kretprobes);
2213 void unregister_kretprobe(struct kretprobe *rp)
2215 unregister_kretprobes(&rp, 1);
2217 EXPORT_SYMBOL_GPL(unregister_kretprobe);
2219 void unregister_kretprobes(struct kretprobe **rps, int num)
2225 mutex_lock(&kprobe_mutex);
2226 for (i = 0; i < num; i++)
2227 if (__unregister_kprobe_top(&rps[i]->kp) < 0)
2228 rps[i]->kp.addr = NULL;
2229 mutex_unlock(&kprobe_mutex);
2232 for (i = 0; i < num; i++) {
2233 if (rps[i]->kp.addr) {
2234 __unregister_kprobe_bottom(&rps[i]->kp);
2235 cleanup_rp_inst(rps[i]);
2239 EXPORT_SYMBOL_GPL(unregister_kretprobes);
2241 #else /* CONFIG_KRETPROBES */
2242 int register_kretprobe(struct kretprobe *rp)
2246 EXPORT_SYMBOL_GPL(register_kretprobe);
2248 int register_kretprobes(struct kretprobe **rps, int num)
2252 EXPORT_SYMBOL_GPL(register_kretprobes);
2254 void unregister_kretprobe(struct kretprobe *rp)
2257 EXPORT_SYMBOL_GPL(unregister_kretprobe);
2259 void unregister_kretprobes(struct kretprobe **rps, int num)
2262 EXPORT_SYMBOL_GPL(unregister_kretprobes);
2264 static int pre_handler_kretprobe(struct kprobe *p, struct pt_regs *regs)
2268 NOKPROBE_SYMBOL(pre_handler_kretprobe);
2270 #endif /* CONFIG_KRETPROBES */
2272 /* Set the kprobe gone and remove its instruction buffer. */
2273 static void kill_kprobe(struct kprobe *p)
2277 lockdep_assert_held(&kprobe_mutex);
2279 if (WARN_ON_ONCE(kprobe_gone(p)))
2282 p->flags |= KPROBE_FLAG_GONE;
2283 if (kprobe_aggrprobe(p)) {
2285 * If this is an aggr_kprobe, we have to list all the
2286 * chained probes and mark them GONE.
2288 list_for_each_entry(kp, &p->list, list)
2289 kp->flags |= KPROBE_FLAG_GONE;
2290 p->post_handler = NULL;
2291 kill_optimized_kprobe(p);
2294 * Here, we can remove insn_slot safely, because no thread calls
2295 * the original probed function (which will be freed soon) any more.
2297 arch_remove_kprobe(p);
2300 * The module is going away. We should disarm the kprobe which
2301 * is using ftrace, because ftrace framework is still available at
2302 * MODULE_STATE_GOING notification.
2304 if (kprobe_ftrace(p) && !kprobe_disabled(p) && !kprobes_all_disarmed)
2305 disarm_kprobe_ftrace(p);
2308 /* Disable one kprobe */
2309 int disable_kprobe(struct kprobe *kp)
2314 mutex_lock(&kprobe_mutex);
2316 /* Disable this kprobe */
2317 p = __disable_kprobe(kp);
2321 mutex_unlock(&kprobe_mutex);
2324 EXPORT_SYMBOL_GPL(disable_kprobe);
2326 /* Enable one kprobe */
2327 int enable_kprobe(struct kprobe *kp)
2332 mutex_lock(&kprobe_mutex);
2334 /* Check whether specified probe is valid. */
2335 p = __get_valid_kprobe(kp);
2336 if (unlikely(p == NULL)) {
2341 if (kprobe_gone(kp)) {
2342 /* This kprobe has gone, we couldn't enable it. */
2348 kp->flags &= ~KPROBE_FLAG_DISABLED;
2350 if (!kprobes_all_disarmed && kprobe_disabled(p)) {
2351 p->flags &= ~KPROBE_FLAG_DISABLED;
2352 ret = arm_kprobe(p);
2354 p->flags |= KPROBE_FLAG_DISABLED;
2356 kp->flags |= KPROBE_FLAG_DISABLED;
2360 mutex_unlock(&kprobe_mutex);
2363 EXPORT_SYMBOL_GPL(enable_kprobe);
2365 /* Caller must NOT call this in usual path. This is only for critical case */
2366 void dump_kprobe(struct kprobe *kp)
2368 pr_err("Dumping kprobe:\n");
2369 pr_err("Name: %s\nOffset: %x\nAddress: %pS\n",
2370 kp->symbol_name, kp->offset, kp->addr);
2372 NOKPROBE_SYMBOL(dump_kprobe);
2374 int kprobe_add_ksym_blacklist(unsigned long entry)
2376 struct kprobe_blacklist_entry *ent;
2377 unsigned long offset = 0, size = 0;
2379 if (!kernel_text_address(entry) ||
2380 !kallsyms_lookup_size_offset(entry, &size, &offset))
2383 ent = kmalloc(sizeof(*ent), GFP_KERNEL);
2386 ent->start_addr = entry;
2387 ent->end_addr = entry + size;
2388 INIT_LIST_HEAD(&ent->list);
2389 list_add_tail(&ent->list, &kprobe_blacklist);
2394 /* Add all symbols in given area into kprobe blacklist */
2395 int kprobe_add_area_blacklist(unsigned long start, unsigned long end)
2397 unsigned long entry;
2400 for (entry = start; entry < end; entry += ret) {
2401 ret = kprobe_add_ksym_blacklist(entry);
2404 if (ret == 0) /* In case of alias symbol */
2410 /* Remove all symbols in given area from kprobe blacklist */
2411 static void kprobe_remove_area_blacklist(unsigned long start, unsigned long end)
2413 struct kprobe_blacklist_entry *ent, *n;
2415 list_for_each_entry_safe(ent, n, &kprobe_blacklist, list) {
2416 if (ent->start_addr < start || ent->start_addr >= end)
2418 list_del(&ent->list);
2423 static void kprobe_remove_ksym_blacklist(unsigned long entry)
2425 kprobe_remove_area_blacklist(entry, entry + 1);
2428 int __weak arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *value,
2429 char *type, char *sym)
2434 int kprobe_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2437 #ifdef __ARCH_WANT_KPROBES_INSN_SLOT
2438 if (!kprobe_cache_get_kallsym(&kprobe_insn_slots, &symnum, value, type, sym))
2440 #ifdef CONFIG_OPTPROBES
2441 if (!kprobe_cache_get_kallsym(&kprobe_optinsn_slots, &symnum, value, type, sym))
2445 if (!arch_kprobe_get_kallsym(&symnum, value, type, sym))
2450 int __init __weak arch_populate_kprobe_blacklist(void)
2456 * Lookup and populate the kprobe_blacklist.
2458 * Unlike the kretprobe blacklist, we'll need to determine
2459 * the range of addresses that belong to the said functions,
2460 * since a kprobe need not necessarily be at the beginning
2463 static int __init populate_kprobe_blacklist(unsigned long *start,
2466 unsigned long entry;
2467 unsigned long *iter;
2470 for (iter = start; iter < end; iter++) {
2471 entry = arch_deref_entry_point((void *)*iter);
2472 ret = kprobe_add_ksym_blacklist(entry);
2479 /* Symbols in __kprobes_text are blacklisted */
2480 ret = kprobe_add_area_blacklist((unsigned long)__kprobes_text_start,
2481 (unsigned long)__kprobes_text_end);
2485 /* Symbols in noinstr section are blacklisted */
2486 ret = kprobe_add_area_blacklist((unsigned long)__noinstr_text_start,
2487 (unsigned long)__noinstr_text_end);
2489 return ret ? : arch_populate_kprobe_blacklist();
2492 static void add_module_kprobe_blacklist(struct module *mod)
2494 unsigned long start, end;
2497 if (mod->kprobe_blacklist) {
2498 for (i = 0; i < mod->num_kprobe_blacklist; i++)
2499 kprobe_add_ksym_blacklist(mod->kprobe_blacklist[i]);
2502 start = (unsigned long)mod->kprobes_text_start;
2504 end = start + mod->kprobes_text_size;
2505 kprobe_add_area_blacklist(start, end);
2508 start = (unsigned long)mod->noinstr_text_start;
2510 end = start + mod->noinstr_text_size;
2511 kprobe_add_area_blacklist(start, end);
2515 static void remove_module_kprobe_blacklist(struct module *mod)
2517 unsigned long start, end;
2520 if (mod->kprobe_blacklist) {
2521 for (i = 0; i < mod->num_kprobe_blacklist; i++)
2522 kprobe_remove_ksym_blacklist(mod->kprobe_blacklist[i]);
2525 start = (unsigned long)mod->kprobes_text_start;
2527 end = start + mod->kprobes_text_size;
2528 kprobe_remove_area_blacklist(start, end);
2531 start = (unsigned long)mod->noinstr_text_start;
2533 end = start + mod->noinstr_text_size;
2534 kprobe_remove_area_blacklist(start, end);
2538 /* Module notifier call back, checking kprobes on the module */
2539 static int kprobes_module_callback(struct notifier_block *nb,
2540 unsigned long val, void *data)
2542 struct module *mod = data;
2543 struct hlist_head *head;
2546 int checkcore = (val == MODULE_STATE_GOING);
2548 if (val == MODULE_STATE_COMING) {
2549 mutex_lock(&kprobe_mutex);
2550 add_module_kprobe_blacklist(mod);
2551 mutex_unlock(&kprobe_mutex);
2553 if (val != MODULE_STATE_GOING && val != MODULE_STATE_LIVE)
2557 * When MODULE_STATE_GOING was notified, both of module .text and
2558 * .init.text sections would be freed. When MODULE_STATE_LIVE was
2559 * notified, only .init.text section would be freed. We need to
2560 * disable kprobes which have been inserted in the sections.
2562 mutex_lock(&kprobe_mutex);
2563 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2564 head = &kprobe_table[i];
2565 hlist_for_each_entry(p, head, hlist) {
2569 if (within_module_init((unsigned long)p->addr, mod) ||
2571 within_module_core((unsigned long)p->addr, mod))) {
2573 * The vaddr this probe is installed will soon
2574 * be vfreed buy not synced to disk. Hence,
2575 * disarming the breakpoint isn't needed.
2577 * Note, this will also move any optimized probes
2578 * that are pending to be removed from their
2579 * corresponding lists to the freeing_list and
2580 * will not be touched by the delayed
2581 * kprobe_optimizer work handler.
2587 if (val == MODULE_STATE_GOING)
2588 remove_module_kprobe_blacklist(mod);
2589 mutex_unlock(&kprobe_mutex);
2593 static struct notifier_block kprobe_module_nb = {
2594 .notifier_call = kprobes_module_callback,
2598 /* Markers of _kprobe_blacklist section */
2599 extern unsigned long __start_kprobe_blacklist[];
2600 extern unsigned long __stop_kprobe_blacklist[];
2602 void kprobe_free_init_mem(void)
2604 void *start = (void *)(&__init_begin);
2605 void *end = (void *)(&__init_end);
2606 struct hlist_head *head;
2610 mutex_lock(&kprobe_mutex);
2612 /* Kill all kprobes on initmem */
2613 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2614 head = &kprobe_table[i];
2615 hlist_for_each_entry(p, head, hlist) {
2616 if (start <= (void *)p->addr && (void *)p->addr < end)
2621 mutex_unlock(&kprobe_mutex);
2624 static int __init init_kprobes(void)
2628 /* FIXME allocate the probe table, currently defined statically */
2629 /* initialize all list heads */
2630 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2631 INIT_HLIST_HEAD(&kprobe_table[i]);
2632 INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
2633 raw_spin_lock_init(&(kretprobe_table_locks[i].lock));
2636 err = populate_kprobe_blacklist(__start_kprobe_blacklist,
2637 __stop_kprobe_blacklist);
2639 pr_err("kprobes: failed to populate blacklist: %d\n", err);
2640 pr_err("Please take care of using kprobes.\n");
2643 if (kretprobe_blacklist_size) {
2644 /* lookup the function address from its name */
2645 for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
2646 kretprobe_blacklist[i].addr =
2647 kprobe_lookup_name(kretprobe_blacklist[i].name, 0);
2648 if (!kretprobe_blacklist[i].addr)
2649 printk("kretprobe: lookup failed: %s\n",
2650 kretprobe_blacklist[i].name);
2654 /* By default, kprobes are armed */
2655 kprobes_all_disarmed = false;
2657 #if defined(CONFIG_OPTPROBES) && defined(__ARCH_WANT_KPROBES_INSN_SLOT)
2658 /* Init kprobe_optinsn_slots for allocation */
2659 kprobe_optinsn_slots.insn_size = MAX_OPTINSN_SIZE;
2662 err = arch_init_kprobes();
2664 err = register_die_notifier(&kprobe_exceptions_nb);
2666 err = register_module_notifier(&kprobe_module_nb);
2668 kprobes_initialized = (err == 0);
2674 early_initcall(init_kprobes);
2676 #if defined(CONFIG_OPTPROBES)
2677 static int __init init_optprobes(void)
2680 * Enable kprobe optimization - this kicks the optimizer which
2681 * depends on synchronize_rcu_tasks() and ksoftirqd, that is
2682 * not spawned in early initcall. So delay the optimization.
2684 optimize_all_kprobes();
2688 subsys_initcall(init_optprobes);
2691 #ifdef CONFIG_DEBUG_FS
2692 static void report_probe(struct seq_file *pi, struct kprobe *p,
2693 const char *sym, int offset, char *modname, struct kprobe *pp)
2696 void *addr = p->addr;
2698 if (p->pre_handler == pre_handler_kretprobe)
2703 if (!kallsyms_show_value(pi->file->f_cred))
2707 seq_printf(pi, "%px %s %s+0x%x %s ",
2708 addr, kprobe_type, sym, offset,
2709 (modname ? modname : " "));
2710 else /* try to use %pS */
2711 seq_printf(pi, "%px %s %pS ",
2712 addr, kprobe_type, p->addr);
2716 seq_printf(pi, "%s%s%s%s\n",
2717 (kprobe_gone(p) ? "[GONE]" : ""),
2718 ((kprobe_disabled(p) && !kprobe_gone(p)) ? "[DISABLED]" : ""),
2719 (kprobe_optimized(pp) ? "[OPTIMIZED]" : ""),
2720 (kprobe_ftrace(pp) ? "[FTRACE]" : ""));
2723 static void *kprobe_seq_start(struct seq_file *f, loff_t *pos)
2725 return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL;
2728 static void *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos)
2731 if (*pos >= KPROBE_TABLE_SIZE)
2736 static void kprobe_seq_stop(struct seq_file *f, void *v)
2741 static int show_kprobe_addr(struct seq_file *pi, void *v)
2743 struct hlist_head *head;
2744 struct kprobe *p, *kp;
2745 const char *sym = NULL;
2746 unsigned int i = *(loff_t *) v;
2747 unsigned long offset = 0;
2748 char *modname, namebuf[KSYM_NAME_LEN];
2750 head = &kprobe_table[i];
2752 hlist_for_each_entry_rcu(p, head, hlist) {
2753 sym = kallsyms_lookup((unsigned long)p->addr, NULL,
2754 &offset, &modname, namebuf);
2755 if (kprobe_aggrprobe(p)) {
2756 list_for_each_entry_rcu(kp, &p->list, list)
2757 report_probe(pi, kp, sym, offset, modname, p);
2759 report_probe(pi, p, sym, offset, modname, NULL);
2765 static const struct seq_operations kprobes_sops = {
2766 .start = kprobe_seq_start,
2767 .next = kprobe_seq_next,
2768 .stop = kprobe_seq_stop,
2769 .show = show_kprobe_addr
2772 DEFINE_SEQ_ATTRIBUTE(kprobes);
2774 /* kprobes/blacklist -- shows which functions can not be probed */
2775 static void *kprobe_blacklist_seq_start(struct seq_file *m, loff_t *pos)
2777 mutex_lock(&kprobe_mutex);
2778 return seq_list_start(&kprobe_blacklist, *pos);
2781 static void *kprobe_blacklist_seq_next(struct seq_file *m, void *v, loff_t *pos)
2783 return seq_list_next(v, &kprobe_blacklist, pos);
2786 static int kprobe_blacklist_seq_show(struct seq_file *m, void *v)
2788 struct kprobe_blacklist_entry *ent =
2789 list_entry(v, struct kprobe_blacklist_entry, list);
2792 * If /proc/kallsyms is not showing kernel address, we won't
2793 * show them here either.
2795 if (!kallsyms_show_value(m->file->f_cred))
2796 seq_printf(m, "0x%px-0x%px\t%ps\n", NULL, NULL,
2797 (void *)ent->start_addr);
2799 seq_printf(m, "0x%px-0x%px\t%ps\n", (void *)ent->start_addr,
2800 (void *)ent->end_addr, (void *)ent->start_addr);
2804 static void kprobe_blacklist_seq_stop(struct seq_file *f, void *v)
2806 mutex_unlock(&kprobe_mutex);
2809 static const struct seq_operations kprobe_blacklist_sops = {
2810 .start = kprobe_blacklist_seq_start,
2811 .next = kprobe_blacklist_seq_next,
2812 .stop = kprobe_blacklist_seq_stop,
2813 .show = kprobe_blacklist_seq_show,
2815 DEFINE_SEQ_ATTRIBUTE(kprobe_blacklist);
2817 static int arm_all_kprobes(void)
2819 struct hlist_head *head;
2821 unsigned int i, total = 0, errors = 0;
2824 mutex_lock(&kprobe_mutex);
2826 /* If kprobes are armed, just return */
2827 if (!kprobes_all_disarmed)
2828 goto already_enabled;
2831 * optimize_kprobe() called by arm_kprobe() checks
2832 * kprobes_all_disarmed, so set kprobes_all_disarmed before
2835 kprobes_all_disarmed = false;
2836 /* Arming kprobes doesn't optimize kprobe itself */
2837 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2838 head = &kprobe_table[i];
2839 /* Arm all kprobes on a best-effort basis */
2840 hlist_for_each_entry(p, head, hlist) {
2841 if (!kprobe_disabled(p)) {
2842 err = arm_kprobe(p);
2853 pr_warn("Kprobes globally enabled, but failed to arm %d out of %d probes\n",
2856 pr_info("Kprobes globally enabled\n");
2859 mutex_unlock(&kprobe_mutex);
2863 static int disarm_all_kprobes(void)
2865 struct hlist_head *head;
2867 unsigned int i, total = 0, errors = 0;
2870 mutex_lock(&kprobe_mutex);
2872 /* If kprobes are already disarmed, just return */
2873 if (kprobes_all_disarmed) {
2874 mutex_unlock(&kprobe_mutex);
2878 kprobes_all_disarmed = true;
2880 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
2881 head = &kprobe_table[i];
2882 /* Disarm all kprobes on a best-effort basis */
2883 hlist_for_each_entry(p, head, hlist) {
2884 if (!arch_trampoline_kprobe(p) && !kprobe_disabled(p)) {
2885 err = disarm_kprobe(p, false);
2896 pr_warn("Kprobes globally disabled, but failed to disarm %d out of %d probes\n",
2899 pr_info("Kprobes globally disabled\n");
2901 mutex_unlock(&kprobe_mutex);
2903 /* Wait for disarming all kprobes by optimizer */
2904 wait_for_kprobe_optimizer();
2910 * XXX: The debugfs bool file interface doesn't allow for callbacks
2911 * when the bool state is switched. We can reuse that facility when
2914 static ssize_t read_enabled_file_bool(struct file *file,
2915 char __user *user_buf, size_t count, loff_t *ppos)
2919 if (!kprobes_all_disarmed)
2925 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
2928 static ssize_t write_enabled_file_bool(struct file *file,
2929 const char __user *user_buf, size_t count, loff_t *ppos)
2935 buf_size = min(count, (sizeof(buf)-1));
2936 if (copy_from_user(buf, user_buf, buf_size))
2939 buf[buf_size] = '\0';
2944 ret = arm_all_kprobes();
2949 ret = disarm_all_kprobes();
2961 static const struct file_operations fops_kp = {
2962 .read = read_enabled_file_bool,
2963 .write = write_enabled_file_bool,
2964 .llseek = default_llseek,
2967 static int __init debugfs_kprobe_init(void)
2971 dir = debugfs_create_dir("kprobes", NULL);
2973 debugfs_create_file("list", 0400, dir, NULL, &kprobes_fops);
2975 debugfs_create_file("enabled", 0600, dir, NULL, &fops_kp);
2977 debugfs_create_file("blacklist", 0400, dir, NULL,
2978 &kprobe_blacklist_fops);
2983 late_initcall(debugfs_kprobe_init);
2984 #endif /* CONFIG_DEBUG_FS */