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>
39 #include <asm/sections.h>
40 #include <asm/cacheflush.h>
41 #include <asm/errno.h>
42 #include <linux/uaccess.h>
44 #define KPROBE_HASH_BITS 6
45 #define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
48 static int kprobes_initialized
;
49 static struct hlist_head kprobe_table
[KPROBE_TABLE_SIZE
];
50 static struct hlist_head kretprobe_inst_table
[KPROBE_TABLE_SIZE
];
52 /* NOTE: change this value only with kprobe_mutex held */
53 static bool kprobes_all_disarmed
;
55 /* This protects kprobe_table and optimizing_list */
56 static DEFINE_MUTEX(kprobe_mutex
);
57 static DEFINE_PER_CPU(struct kprobe
*, kprobe_instance
) = NULL
;
59 raw_spinlock_t lock ____cacheline_aligned_in_smp
;
60 } kretprobe_table_locks
[KPROBE_TABLE_SIZE
];
62 kprobe_opcode_t
* __weak
kprobe_lookup_name(const char *name
,
63 unsigned int __unused
)
65 return ((kprobe_opcode_t
*)(kallsyms_lookup_name(name
)));
68 static raw_spinlock_t
*kretprobe_table_lock_ptr(unsigned long hash
)
70 return &(kretprobe_table_locks
[hash
].lock
);
73 /* Blacklist -- list of struct kprobe_blacklist_entry */
74 static LIST_HEAD(kprobe_blacklist
);
76 #ifdef __ARCH_WANT_KPROBES_INSN_SLOT
78 * kprobe->ainsn.insn points to the copy of the instruction to be
79 * single-stepped. x86_64, POWER4 and above have no-exec support and
80 * stepping on the instruction on a vmalloced/kmalloced/data page
81 * is a recipe for disaster
83 struct kprobe_insn_page
{
84 struct list_head list
;
85 kprobe_opcode_t
*insns
; /* Page of instruction slots */
86 struct kprobe_insn_cache
*cache
;
92 #define KPROBE_INSN_PAGE_SIZE(slots) \
93 (offsetof(struct kprobe_insn_page, slot_used) + \
94 (sizeof(char) * (slots)))
96 static int slots_per_page(struct kprobe_insn_cache
*c
)
98 return PAGE_SIZE
/(c
->insn_size
* sizeof(kprobe_opcode_t
));
101 enum kprobe_slot_state
{
107 void __weak
*alloc_insn_page(void)
109 return module_alloc(PAGE_SIZE
);
112 void __weak
free_insn_page(void *page
)
114 module_memfree(page
);
117 struct kprobe_insn_cache kprobe_insn_slots
= {
118 .mutex
= __MUTEX_INITIALIZER(kprobe_insn_slots
.mutex
),
119 .alloc
= alloc_insn_page
,
120 .free
= free_insn_page
,
121 .pages
= LIST_HEAD_INIT(kprobe_insn_slots
.pages
),
122 .insn_size
= MAX_INSN_SIZE
,
125 static int collect_garbage_slots(struct kprobe_insn_cache
*c
);
128 * __get_insn_slot() - Find a slot on an executable page for an instruction.
129 * We allocate an executable page if there's no room on existing ones.
131 kprobe_opcode_t
*__get_insn_slot(struct kprobe_insn_cache
*c
)
133 struct kprobe_insn_page
*kip
;
134 kprobe_opcode_t
*slot
= NULL
;
136 /* Since the slot array is not protected by rcu, we need a mutex */
137 mutex_lock(&c
->mutex
);
140 list_for_each_entry_rcu(kip
, &c
->pages
, list
) {
141 if (kip
->nused
< slots_per_page(c
)) {
143 for (i
= 0; i
< slots_per_page(c
); i
++) {
144 if (kip
->slot_used
[i
] == SLOT_CLEAN
) {
145 kip
->slot_used
[i
] = SLOT_USED
;
147 slot
= kip
->insns
+ (i
* c
->insn_size
);
152 /* kip->nused is broken. Fix it. */
153 kip
->nused
= slots_per_page(c
);
159 /* If there are any garbage slots, collect it and try again. */
160 if (c
->nr_garbage
&& collect_garbage_slots(c
) == 0)
163 /* All out of space. Need to allocate a new page. */
164 kip
= kmalloc(KPROBE_INSN_PAGE_SIZE(slots_per_page(c
)), GFP_KERNEL
);
169 * Use module_alloc so this page is within +/- 2GB of where the
170 * kernel image and loaded module images reside. This is required
171 * so x86_64 can correctly handle the %rip-relative fixups.
173 kip
->insns
= c
->alloc();
178 INIT_LIST_HEAD(&kip
->list
);
179 memset(kip
->slot_used
, SLOT_CLEAN
, slots_per_page(c
));
180 kip
->slot_used
[0] = SLOT_USED
;
184 list_add_rcu(&kip
->list
, &c
->pages
);
187 mutex_unlock(&c
->mutex
);
191 /* Return 1 if all garbages are collected, otherwise 0. */
192 static int collect_one_slot(struct kprobe_insn_page
*kip
, int idx
)
194 kip
->slot_used
[idx
] = SLOT_CLEAN
;
196 if (kip
->nused
== 0) {
198 * Page is no longer in use. Free it unless
199 * it's the last one. We keep the last one
200 * so as not to have to set it up again the
201 * next time somebody inserts a probe.
203 if (!list_is_singular(&kip
->list
)) {
204 list_del_rcu(&kip
->list
);
206 kip
->cache
->free(kip
->insns
);
214 static int collect_garbage_slots(struct kprobe_insn_cache
*c
)
216 struct kprobe_insn_page
*kip
, *next
;
218 /* Ensure no-one is interrupted on the garbages */
221 list_for_each_entry_safe(kip
, next
, &c
->pages
, list
) {
223 if (kip
->ngarbage
== 0)
225 kip
->ngarbage
= 0; /* we will collect all garbages */
226 for (i
= 0; i
< slots_per_page(c
); i
++) {
227 if (kip
->slot_used
[i
] == SLOT_DIRTY
&& collect_one_slot(kip
, i
))
235 void __free_insn_slot(struct kprobe_insn_cache
*c
,
236 kprobe_opcode_t
*slot
, int dirty
)
238 struct kprobe_insn_page
*kip
;
241 mutex_lock(&c
->mutex
);
243 list_for_each_entry_rcu(kip
, &c
->pages
, list
) {
244 idx
= ((long)slot
- (long)kip
->insns
) /
245 (c
->insn_size
* sizeof(kprobe_opcode_t
));
246 if (idx
>= 0 && idx
< slots_per_page(c
))
249 /* Could not find this slot. */
254 /* Mark and sweep: this may sleep */
256 /* Check double free */
257 WARN_ON(kip
->slot_used
[idx
] != SLOT_USED
);
259 kip
->slot_used
[idx
] = SLOT_DIRTY
;
261 if (++c
->nr_garbage
> slots_per_page(c
))
262 collect_garbage_slots(c
);
264 collect_one_slot(kip
, idx
);
267 mutex_unlock(&c
->mutex
);
271 * Check given address is on the page of kprobe instruction slots.
272 * This will be used for checking whether the address on a stack
273 * is on a text area or not.
275 bool __is_insn_slot_addr(struct kprobe_insn_cache
*c
, unsigned long addr
)
277 struct kprobe_insn_page
*kip
;
281 list_for_each_entry_rcu(kip
, &c
->pages
, list
) {
282 if (addr
>= (unsigned long)kip
->insns
&&
283 addr
< (unsigned long)kip
->insns
+ PAGE_SIZE
) {
293 #ifdef CONFIG_OPTPROBES
294 /* For optimized_kprobe buffer */
295 struct kprobe_insn_cache kprobe_optinsn_slots
= {
296 .mutex
= __MUTEX_INITIALIZER(kprobe_optinsn_slots
.mutex
),
297 .alloc
= alloc_insn_page
,
298 .free
= free_insn_page
,
299 .pages
= LIST_HEAD_INIT(kprobe_optinsn_slots
.pages
),
300 /* .insn_size is initialized later */
306 /* We have preemption disabled.. so it is safe to use __ versions */
307 static inline void set_kprobe_instance(struct kprobe
*kp
)
309 __this_cpu_write(kprobe_instance
, kp
);
312 static inline void reset_kprobe_instance(void)
314 __this_cpu_write(kprobe_instance
, NULL
);
318 * This routine is called either:
319 * - under the kprobe_mutex - during kprobe_[un]register()
321 * - with preemption disabled - from arch/xxx/kernel/kprobes.c
323 struct kprobe
*get_kprobe(void *addr
)
325 struct hlist_head
*head
;
328 head
= &kprobe_table
[hash_ptr(addr
, KPROBE_HASH_BITS
)];
329 hlist_for_each_entry_rcu(p
, head
, hlist
,
330 lockdep_is_held(&kprobe_mutex
)) {
337 NOKPROBE_SYMBOL(get_kprobe
);
339 static int aggr_pre_handler(struct kprobe
*p
, struct pt_regs
*regs
);
341 /* Return true if the kprobe is an aggregator */
342 static inline int kprobe_aggrprobe(struct kprobe
*p
)
344 return p
->pre_handler
== aggr_pre_handler
;
347 /* Return true(!0) if the kprobe is unused */
348 static inline int kprobe_unused(struct kprobe
*p
)
350 return kprobe_aggrprobe(p
) && kprobe_disabled(p
) &&
351 list_empty(&p
->list
);
355 * Keep all fields in the kprobe consistent
357 static inline void copy_kprobe(struct kprobe
*ap
, struct kprobe
*p
)
359 memcpy(&p
->opcode
, &ap
->opcode
, sizeof(kprobe_opcode_t
));
360 memcpy(&p
->ainsn
, &ap
->ainsn
, sizeof(struct arch_specific_insn
));
363 #ifdef CONFIG_OPTPROBES
364 /* NOTE: change this value only with kprobe_mutex held */
365 static bool kprobes_allow_optimization
;
368 * Call all pre_handler on the list, but ignores its return value.
369 * This must be called from arch-dep optimized caller.
371 void opt_pre_handler(struct kprobe
*p
, struct pt_regs
*regs
)
375 list_for_each_entry_rcu(kp
, &p
->list
, list
) {
376 if (kp
->pre_handler
&& likely(!kprobe_disabled(kp
))) {
377 set_kprobe_instance(kp
);
378 kp
->pre_handler(kp
, regs
);
380 reset_kprobe_instance();
383 NOKPROBE_SYMBOL(opt_pre_handler
);
385 /* Free optimized instructions and optimized_kprobe */
386 static void free_aggr_kprobe(struct kprobe
*p
)
388 struct optimized_kprobe
*op
;
390 op
= container_of(p
, struct optimized_kprobe
, kp
);
391 arch_remove_optimized_kprobe(op
);
392 arch_remove_kprobe(p
);
396 /* Return true(!0) if the kprobe is ready for optimization. */
397 static inline int kprobe_optready(struct kprobe
*p
)
399 struct optimized_kprobe
*op
;
401 if (kprobe_aggrprobe(p
)) {
402 op
= container_of(p
, struct optimized_kprobe
, kp
);
403 return arch_prepared_optinsn(&op
->optinsn
);
409 /* Return true(!0) if the kprobe is disarmed. Note: p must be on hash list */
410 static inline int kprobe_disarmed(struct kprobe
*p
)
412 struct optimized_kprobe
*op
;
414 /* If kprobe is not aggr/opt probe, just return kprobe is disabled */
415 if (!kprobe_aggrprobe(p
))
416 return kprobe_disabled(p
);
418 op
= container_of(p
, struct optimized_kprobe
, kp
);
420 return kprobe_disabled(p
) && list_empty(&op
->list
);
423 /* Return true(!0) if the probe is queued on (un)optimizing lists */
424 static int kprobe_queued(struct kprobe
*p
)
426 struct optimized_kprobe
*op
;
428 if (kprobe_aggrprobe(p
)) {
429 op
= container_of(p
, struct optimized_kprobe
, kp
);
430 if (!list_empty(&op
->list
))
437 * Return an optimized kprobe whose optimizing code replaces
438 * instructions including addr (exclude breakpoint).
440 static struct kprobe
*get_optimized_kprobe(unsigned long addr
)
443 struct kprobe
*p
= NULL
;
444 struct optimized_kprobe
*op
;
446 /* Don't check i == 0, since that is a breakpoint case. */
447 for (i
= 1; !p
&& i
< MAX_OPTIMIZED_LENGTH
; i
++)
448 p
= get_kprobe((void *)(addr
- i
));
450 if (p
&& kprobe_optready(p
)) {
451 op
= container_of(p
, struct optimized_kprobe
, kp
);
452 if (arch_within_optimized_kprobe(op
, addr
))
459 /* Optimization staging list, protected by kprobe_mutex */
460 static LIST_HEAD(optimizing_list
);
461 static LIST_HEAD(unoptimizing_list
);
462 static LIST_HEAD(freeing_list
);
464 static void kprobe_optimizer(struct work_struct
*work
);
465 static DECLARE_DELAYED_WORK(optimizing_work
, kprobe_optimizer
);
466 #define OPTIMIZE_DELAY 5
469 * Optimize (replace a breakpoint with a jump) kprobes listed on
472 static void do_optimize_kprobes(void)
474 lockdep_assert_held(&text_mutex
);
476 * The optimization/unoptimization refers online_cpus via
477 * stop_machine() and cpu-hotplug modifies online_cpus.
478 * And same time, text_mutex will be held in cpu-hotplug and here.
479 * This combination can cause a deadlock (cpu-hotplug try to lock
480 * text_mutex but stop_machine can not be done because online_cpus
482 * To avoid this deadlock, caller must have locked cpu hotplug
483 * for preventing cpu-hotplug outside of text_mutex locking.
485 lockdep_assert_cpus_held();
487 /* Optimization never be done when disarmed */
488 if (kprobes_all_disarmed
|| !kprobes_allow_optimization
||
489 list_empty(&optimizing_list
))
492 arch_optimize_kprobes(&optimizing_list
);
496 * Unoptimize (replace a jump with a breakpoint and remove the breakpoint
497 * if need) kprobes listed on unoptimizing_list.
499 static void do_unoptimize_kprobes(void)
501 struct optimized_kprobe
*op
, *tmp
;
503 lockdep_assert_held(&text_mutex
);
504 /* See comment in do_optimize_kprobes() */
505 lockdep_assert_cpus_held();
507 /* Unoptimization must be done anytime */
508 if (list_empty(&unoptimizing_list
))
511 arch_unoptimize_kprobes(&unoptimizing_list
, &freeing_list
);
512 /* Loop free_list for disarming */
513 list_for_each_entry_safe(op
, tmp
, &freeing_list
, list
) {
514 /* Switching from detour code to origin */
515 op
->kp
.flags
&= ~KPROBE_FLAG_OPTIMIZED
;
516 /* Disarm probes if marked disabled */
517 if (kprobe_disabled(&op
->kp
))
518 arch_disarm_kprobe(&op
->kp
);
519 if (kprobe_unused(&op
->kp
)) {
521 * Remove unused probes from hash list. After waiting
522 * for synchronization, these probes are reclaimed.
523 * (reclaiming is done by do_free_cleaned_kprobes.)
525 hlist_del_rcu(&op
->kp
.hlist
);
527 list_del_init(&op
->list
);
531 /* Reclaim all kprobes on the free_list */
532 static void do_free_cleaned_kprobes(void)
534 struct optimized_kprobe
*op
, *tmp
;
536 list_for_each_entry_safe(op
, tmp
, &freeing_list
, list
) {
537 list_del_init(&op
->list
);
538 if (WARN_ON_ONCE(!kprobe_unused(&op
->kp
))) {
540 * This must not happen, but if there is a kprobe
541 * still in use, keep it on kprobes hash list.
545 free_aggr_kprobe(&op
->kp
);
549 /* Start optimizer after OPTIMIZE_DELAY passed */
550 static void kick_kprobe_optimizer(void)
552 schedule_delayed_work(&optimizing_work
, OPTIMIZE_DELAY
);
555 /* Kprobe jump optimizer */
556 static void kprobe_optimizer(struct work_struct
*work
)
558 mutex_lock(&kprobe_mutex
);
560 mutex_lock(&text_mutex
);
561 /* Lock modules while optimizing kprobes */
562 mutex_lock(&module_mutex
);
565 * Step 1: Unoptimize kprobes and collect cleaned (unused and disarmed)
566 * kprobes before waiting for quiesence period.
568 do_unoptimize_kprobes();
571 * Step 2: Wait for quiesence period to ensure all potentially
572 * preempted tasks to have normally scheduled. Because optprobe
573 * may modify multiple instructions, there is a chance that Nth
574 * instruction is preempted. In that case, such tasks can return
575 * to 2nd-Nth byte of jump instruction. This wait is for avoiding it.
576 * Note that on non-preemptive kernel, this is transparently converted
577 * to synchronoze_sched() to wait for all interrupts to have completed.
579 synchronize_rcu_tasks();
581 /* Step 3: Optimize kprobes after quiesence period */
582 do_optimize_kprobes();
584 /* Step 4: Free cleaned kprobes after quiesence period */
585 do_free_cleaned_kprobes();
587 mutex_unlock(&module_mutex
);
588 mutex_unlock(&text_mutex
);
591 /* Step 5: Kick optimizer again if needed */
592 if (!list_empty(&optimizing_list
) || !list_empty(&unoptimizing_list
))
593 kick_kprobe_optimizer();
595 mutex_unlock(&kprobe_mutex
);
598 /* Wait for completing optimization and unoptimization */
599 void wait_for_kprobe_optimizer(void)
601 mutex_lock(&kprobe_mutex
);
603 while (!list_empty(&optimizing_list
) || !list_empty(&unoptimizing_list
)) {
604 mutex_unlock(&kprobe_mutex
);
606 /* this will also make optimizing_work execute immmediately */
607 flush_delayed_work(&optimizing_work
);
608 /* @optimizing_work might not have been queued yet, relax */
611 mutex_lock(&kprobe_mutex
);
614 mutex_unlock(&kprobe_mutex
);
617 static bool optprobe_queued_unopt(struct optimized_kprobe
*op
)
619 struct optimized_kprobe
*_op
;
621 list_for_each_entry(_op
, &unoptimizing_list
, list
) {
629 /* Optimize kprobe if p is ready to be optimized */
630 static void optimize_kprobe(struct kprobe
*p
)
632 struct optimized_kprobe
*op
;
634 /* Check if the kprobe is disabled or not ready for optimization. */
635 if (!kprobe_optready(p
) || !kprobes_allow_optimization
||
636 (kprobe_disabled(p
) || kprobes_all_disarmed
))
639 /* kprobes with post_handler can not be optimized */
643 op
= container_of(p
, struct optimized_kprobe
, kp
);
645 /* Check there is no other kprobes at the optimized instructions */
646 if (arch_check_optimized_kprobe(op
) < 0)
649 /* Check if it is already optimized. */
650 if (op
->kp
.flags
& KPROBE_FLAG_OPTIMIZED
) {
651 if (optprobe_queued_unopt(op
)) {
652 /* This is under unoptimizing. Just dequeue the probe */
653 list_del_init(&op
->list
);
657 op
->kp
.flags
|= KPROBE_FLAG_OPTIMIZED
;
659 /* On unoptimizing/optimizing_list, op must have OPTIMIZED flag */
660 if (WARN_ON_ONCE(!list_empty(&op
->list
)))
663 list_add(&op
->list
, &optimizing_list
);
664 kick_kprobe_optimizer();
667 /* Short cut to direct unoptimizing */
668 static void force_unoptimize_kprobe(struct optimized_kprobe
*op
)
670 lockdep_assert_cpus_held();
671 arch_unoptimize_kprobe(op
);
672 op
->kp
.flags
&= ~KPROBE_FLAG_OPTIMIZED
;
673 if (kprobe_disabled(&op
->kp
))
674 arch_disarm_kprobe(&op
->kp
);
677 /* Unoptimize a kprobe if p is optimized */
678 static void unoptimize_kprobe(struct kprobe
*p
, bool force
)
680 struct optimized_kprobe
*op
;
682 if (!kprobe_aggrprobe(p
) || kprobe_disarmed(p
))
683 return; /* This is not an optprobe nor optimized */
685 op
= container_of(p
, struct optimized_kprobe
, kp
);
686 if (!kprobe_optimized(p
))
689 if (!list_empty(&op
->list
)) {
690 if (optprobe_queued_unopt(op
)) {
691 /* Queued in unoptimizing queue */
694 * Forcibly unoptimize the kprobe here, and queue it
695 * in the freeing list for release afterwards.
697 force_unoptimize_kprobe(op
);
698 list_move(&op
->list
, &freeing_list
);
701 /* Dequeue from the optimizing queue */
702 list_del_init(&op
->list
);
703 op
->kp
.flags
&= ~KPROBE_FLAG_OPTIMIZED
;
708 /* Optimized kprobe case */
710 /* Forcibly update the code: this is a special case */
711 force_unoptimize_kprobe(op
);
713 list_add(&op
->list
, &unoptimizing_list
);
714 kick_kprobe_optimizer();
718 /* Cancel unoptimizing for reusing */
719 static int reuse_unused_kprobe(struct kprobe
*ap
)
721 struct optimized_kprobe
*op
;
724 * Unused kprobe MUST be on the way of delayed unoptimizing (means
725 * there is still a relative jump) and disabled.
727 op
= container_of(ap
, struct optimized_kprobe
, kp
);
728 WARN_ON_ONCE(list_empty(&op
->list
));
729 /* Enable the probe again */
730 ap
->flags
&= ~KPROBE_FLAG_DISABLED
;
731 /* Optimize it again (remove from op->list) */
732 if (!kprobe_optready(ap
))
739 /* Remove optimized instructions */
740 static void kill_optimized_kprobe(struct kprobe
*p
)
742 struct optimized_kprobe
*op
;
744 op
= container_of(p
, struct optimized_kprobe
, kp
);
745 if (!list_empty(&op
->list
))
746 /* Dequeue from the (un)optimization queue */
747 list_del_init(&op
->list
);
748 op
->kp
.flags
&= ~KPROBE_FLAG_OPTIMIZED
;
750 if (kprobe_unused(p
)) {
751 /* Enqueue if it is unused */
752 list_add(&op
->list
, &freeing_list
);
754 * Remove unused probes from the hash list. After waiting
755 * for synchronization, this probe is reclaimed.
756 * (reclaiming is done by do_free_cleaned_kprobes().)
758 hlist_del_rcu(&op
->kp
.hlist
);
761 /* Don't touch the code, because it is already freed. */
762 arch_remove_optimized_kprobe(op
);
766 void __prepare_optimized_kprobe(struct optimized_kprobe
*op
, struct kprobe
*p
)
768 if (!kprobe_ftrace(p
))
769 arch_prepare_optimized_kprobe(op
, p
);
772 /* Try to prepare optimized instructions */
773 static void prepare_optimized_kprobe(struct kprobe
*p
)
775 struct optimized_kprobe
*op
;
777 op
= container_of(p
, struct optimized_kprobe
, kp
);
778 __prepare_optimized_kprobe(op
, p
);
781 /* Allocate new optimized_kprobe and try to prepare optimized instructions */
782 static struct kprobe
*alloc_aggr_kprobe(struct kprobe
*p
)
784 struct optimized_kprobe
*op
;
786 op
= kzalloc(sizeof(struct optimized_kprobe
), GFP_KERNEL
);
790 INIT_LIST_HEAD(&op
->list
);
791 op
->kp
.addr
= p
->addr
;
792 __prepare_optimized_kprobe(op
, p
);
797 static void init_aggr_kprobe(struct kprobe
*ap
, struct kprobe
*p
);
800 * Prepare an optimized_kprobe and optimize it
801 * NOTE: p must be a normal registered kprobe
803 static void try_to_optimize_kprobe(struct kprobe
*p
)
806 struct optimized_kprobe
*op
;
808 /* Impossible to optimize ftrace-based kprobe */
809 if (kprobe_ftrace(p
))
812 /* For preparing optimization, jump_label_text_reserved() is called */
815 mutex_lock(&text_mutex
);
817 ap
= alloc_aggr_kprobe(p
);
821 op
= container_of(ap
, struct optimized_kprobe
, kp
);
822 if (!arch_prepared_optinsn(&op
->optinsn
)) {
823 /* If failed to setup optimizing, fallback to kprobe */
824 arch_remove_optimized_kprobe(op
);
829 init_aggr_kprobe(ap
, p
);
830 optimize_kprobe(ap
); /* This just kicks optimizer thread */
833 mutex_unlock(&text_mutex
);
839 static void optimize_all_kprobes(void)
841 struct hlist_head
*head
;
845 mutex_lock(&kprobe_mutex
);
846 /* If optimization is already allowed, just return */
847 if (kprobes_allow_optimization
)
851 kprobes_allow_optimization
= true;
852 for (i
= 0; i
< KPROBE_TABLE_SIZE
; i
++) {
853 head
= &kprobe_table
[i
];
854 hlist_for_each_entry_rcu(p
, head
, hlist
)
855 if (!kprobe_disabled(p
))
859 printk(KERN_INFO
"Kprobes globally optimized\n");
861 mutex_unlock(&kprobe_mutex
);
864 static void unoptimize_all_kprobes(void)
866 struct hlist_head
*head
;
870 mutex_lock(&kprobe_mutex
);
871 /* If optimization is already prohibited, just return */
872 if (!kprobes_allow_optimization
) {
873 mutex_unlock(&kprobe_mutex
);
878 kprobes_allow_optimization
= false;
879 for (i
= 0; i
< KPROBE_TABLE_SIZE
; i
++) {
880 head
= &kprobe_table
[i
];
881 hlist_for_each_entry_rcu(p
, head
, hlist
) {
882 if (!kprobe_disabled(p
))
883 unoptimize_kprobe(p
, false);
887 mutex_unlock(&kprobe_mutex
);
889 /* Wait for unoptimizing completion */
890 wait_for_kprobe_optimizer();
891 printk(KERN_INFO
"Kprobes globally unoptimized\n");
894 static DEFINE_MUTEX(kprobe_sysctl_mutex
);
895 int sysctl_kprobes_optimization
;
896 int proc_kprobes_optimization_handler(struct ctl_table
*table
, int write
,
897 void __user
*buffer
, size_t *length
,
902 mutex_lock(&kprobe_sysctl_mutex
);
903 sysctl_kprobes_optimization
= kprobes_allow_optimization
? 1 : 0;
904 ret
= proc_dointvec_minmax(table
, write
, buffer
, length
, ppos
);
906 if (sysctl_kprobes_optimization
)
907 optimize_all_kprobes();
909 unoptimize_all_kprobes();
910 mutex_unlock(&kprobe_sysctl_mutex
);
914 #endif /* CONFIG_SYSCTL */
916 /* Put a breakpoint for a probe. Must be called with text_mutex locked */
917 static void __arm_kprobe(struct kprobe
*p
)
921 /* Check collision with other optimized kprobes */
922 _p
= get_optimized_kprobe((unsigned long)p
->addr
);
924 /* Fallback to unoptimized kprobe */
925 unoptimize_kprobe(_p
, true);
928 optimize_kprobe(p
); /* Try to optimize (add kprobe to a list) */
931 /* Remove the breakpoint of a probe. Must be called with text_mutex locked */
932 static void __disarm_kprobe(struct kprobe
*p
, bool reopt
)
936 /* Try to unoptimize */
937 unoptimize_kprobe(p
, kprobes_all_disarmed
);
939 if (!kprobe_queued(p
)) {
940 arch_disarm_kprobe(p
);
941 /* If another kprobe was blocked, optimize it. */
942 _p
= get_optimized_kprobe((unsigned long)p
->addr
);
943 if (unlikely(_p
) && reopt
)
946 /* TODO: reoptimize others after unoptimized this probe */
949 #else /* !CONFIG_OPTPROBES */
951 #define optimize_kprobe(p) do {} while (0)
952 #define unoptimize_kprobe(p, f) do {} while (0)
953 #define kill_optimized_kprobe(p) do {} while (0)
954 #define prepare_optimized_kprobe(p) do {} while (0)
955 #define try_to_optimize_kprobe(p) do {} while (0)
956 #define __arm_kprobe(p) arch_arm_kprobe(p)
957 #define __disarm_kprobe(p, o) arch_disarm_kprobe(p)
958 #define kprobe_disarmed(p) kprobe_disabled(p)
959 #define wait_for_kprobe_optimizer() do {} while (0)
961 static int reuse_unused_kprobe(struct kprobe
*ap
)
964 * If the optimized kprobe is NOT supported, the aggr kprobe is
965 * released at the same time that the last aggregated kprobe is
967 * Thus there should be no chance to reuse unused kprobe.
969 printk(KERN_ERR
"Error: There should be no unused kprobe here.\n");
973 static void free_aggr_kprobe(struct kprobe
*p
)
975 arch_remove_kprobe(p
);
979 static struct kprobe
*alloc_aggr_kprobe(struct kprobe
*p
)
981 return kzalloc(sizeof(struct kprobe
), GFP_KERNEL
);
983 #endif /* CONFIG_OPTPROBES */
985 #ifdef CONFIG_KPROBES_ON_FTRACE
986 static struct ftrace_ops kprobe_ftrace_ops __read_mostly
= {
987 .func
= kprobe_ftrace_handler
,
988 .flags
= FTRACE_OPS_FL_SAVE_REGS
,
991 static struct ftrace_ops kprobe_ipmodify_ops __read_mostly
= {
992 .func
= kprobe_ftrace_handler
,
993 .flags
= FTRACE_OPS_FL_SAVE_REGS
| FTRACE_OPS_FL_IPMODIFY
,
996 static int kprobe_ipmodify_enabled
;
997 static int kprobe_ftrace_enabled
;
999 /* Must ensure p->addr is really on ftrace */
1000 static int prepare_kprobe(struct kprobe
*p
)
1002 if (!kprobe_ftrace(p
))
1003 return arch_prepare_kprobe(p
);
1005 return arch_prepare_kprobe_ftrace(p
);
1008 /* Caller must lock kprobe_mutex */
1009 static int __arm_kprobe_ftrace(struct kprobe
*p
, struct ftrace_ops
*ops
,
1014 ret
= ftrace_set_filter_ip(ops
, (unsigned long)p
->addr
, 0, 0);
1016 pr_debug("Failed to arm kprobe-ftrace at %pS (%d)\n",
1022 ret
= register_ftrace_function(ops
);
1024 pr_debug("Failed to init kprobe-ftrace (%d)\n", ret
);
1034 * At this point, sinec ops is not registered, we should be sefe from
1035 * registering empty filter.
1037 ftrace_set_filter_ip(ops
, (unsigned long)p
->addr
, 1, 0);
1041 static int arm_kprobe_ftrace(struct kprobe
*p
)
1043 bool ipmodify
= (p
->post_handler
!= NULL
);
1045 return __arm_kprobe_ftrace(p
,
1046 ipmodify
? &kprobe_ipmodify_ops
: &kprobe_ftrace_ops
,
1047 ipmodify
? &kprobe_ipmodify_enabled
: &kprobe_ftrace_enabled
);
1050 /* Caller must lock kprobe_mutex */
1051 static int __disarm_kprobe_ftrace(struct kprobe
*p
, struct ftrace_ops
*ops
,
1057 ret
= unregister_ftrace_function(ops
);
1058 if (WARN(ret
< 0, "Failed to unregister kprobe-ftrace (%d)\n", ret
))
1064 ret
= ftrace_set_filter_ip(ops
, (unsigned long)p
->addr
, 1, 0);
1065 WARN_ONCE(ret
< 0, "Failed to disarm kprobe-ftrace at %pS (%d)\n",
1070 static int disarm_kprobe_ftrace(struct kprobe
*p
)
1072 bool ipmodify
= (p
->post_handler
!= NULL
);
1074 return __disarm_kprobe_ftrace(p
,
1075 ipmodify
? &kprobe_ipmodify_ops
: &kprobe_ftrace_ops
,
1076 ipmodify
? &kprobe_ipmodify_enabled
: &kprobe_ftrace_enabled
);
1078 #else /* !CONFIG_KPROBES_ON_FTRACE */
1079 #define prepare_kprobe(p) arch_prepare_kprobe(p)
1080 #define arm_kprobe_ftrace(p) (-ENODEV)
1081 #define disarm_kprobe_ftrace(p) (-ENODEV)
1084 /* Arm a kprobe with text_mutex */
1085 static int arm_kprobe(struct kprobe
*kp
)
1087 if (unlikely(kprobe_ftrace(kp
)))
1088 return arm_kprobe_ftrace(kp
);
1091 mutex_lock(&text_mutex
);
1093 mutex_unlock(&text_mutex
);
1099 /* Disarm a kprobe with text_mutex */
1100 static int disarm_kprobe(struct kprobe
*kp
, bool reopt
)
1102 if (unlikely(kprobe_ftrace(kp
)))
1103 return disarm_kprobe_ftrace(kp
);
1106 mutex_lock(&text_mutex
);
1107 __disarm_kprobe(kp
, reopt
);
1108 mutex_unlock(&text_mutex
);
1115 * Aggregate handlers for multiple kprobes support - these handlers
1116 * take care of invoking the individual kprobe handlers on p->list
1118 static int aggr_pre_handler(struct kprobe
*p
, struct pt_regs
*regs
)
1122 list_for_each_entry_rcu(kp
, &p
->list
, list
) {
1123 if (kp
->pre_handler
&& likely(!kprobe_disabled(kp
))) {
1124 set_kprobe_instance(kp
);
1125 if (kp
->pre_handler(kp
, regs
))
1128 reset_kprobe_instance();
1132 NOKPROBE_SYMBOL(aggr_pre_handler
);
1134 static void aggr_post_handler(struct kprobe
*p
, struct pt_regs
*regs
,
1135 unsigned long flags
)
1139 list_for_each_entry_rcu(kp
, &p
->list
, list
) {
1140 if (kp
->post_handler
&& likely(!kprobe_disabled(kp
))) {
1141 set_kprobe_instance(kp
);
1142 kp
->post_handler(kp
, regs
, flags
);
1143 reset_kprobe_instance();
1147 NOKPROBE_SYMBOL(aggr_post_handler
);
1149 static int aggr_fault_handler(struct kprobe
*p
, struct pt_regs
*regs
,
1152 struct kprobe
*cur
= __this_cpu_read(kprobe_instance
);
1155 * if we faulted "during" the execution of a user specified
1156 * probe handler, invoke just that probe's fault handler
1158 if (cur
&& cur
->fault_handler
) {
1159 if (cur
->fault_handler(cur
, regs
, trapnr
))
1164 NOKPROBE_SYMBOL(aggr_fault_handler
);
1166 /* Walks the list and increments nmissed count for multiprobe case */
1167 void kprobes_inc_nmissed_count(struct kprobe
*p
)
1170 if (!kprobe_aggrprobe(p
)) {
1173 list_for_each_entry_rcu(kp
, &p
->list
, list
)
1178 NOKPROBE_SYMBOL(kprobes_inc_nmissed_count
);
1180 void recycle_rp_inst(struct kretprobe_instance
*ri
,
1181 struct hlist_head
*head
)
1183 struct kretprobe
*rp
= ri
->rp
;
1185 /* remove rp inst off the rprobe_inst_table */
1186 hlist_del(&ri
->hlist
);
1187 INIT_HLIST_NODE(&ri
->hlist
);
1189 raw_spin_lock(&rp
->lock
);
1190 hlist_add_head(&ri
->hlist
, &rp
->free_instances
);
1191 raw_spin_unlock(&rp
->lock
);
1194 hlist_add_head(&ri
->hlist
, head
);
1196 NOKPROBE_SYMBOL(recycle_rp_inst
);
1198 void kretprobe_hash_lock(struct task_struct
*tsk
,
1199 struct hlist_head
**head
, unsigned long *flags
)
1200 __acquires(hlist_lock
)
1202 unsigned long hash
= hash_ptr(tsk
, KPROBE_HASH_BITS
);
1203 raw_spinlock_t
*hlist_lock
;
1205 *head
= &kretprobe_inst_table
[hash
];
1206 hlist_lock
= kretprobe_table_lock_ptr(hash
);
1207 raw_spin_lock_irqsave(hlist_lock
, *flags
);
1209 NOKPROBE_SYMBOL(kretprobe_hash_lock
);
1211 static void kretprobe_table_lock(unsigned long hash
,
1212 unsigned long *flags
)
1213 __acquires(hlist_lock
)
1215 raw_spinlock_t
*hlist_lock
= kretprobe_table_lock_ptr(hash
);
1216 raw_spin_lock_irqsave(hlist_lock
, *flags
);
1218 NOKPROBE_SYMBOL(kretprobe_table_lock
);
1220 void kretprobe_hash_unlock(struct task_struct
*tsk
,
1221 unsigned long *flags
)
1222 __releases(hlist_lock
)
1224 unsigned long hash
= hash_ptr(tsk
, KPROBE_HASH_BITS
);
1225 raw_spinlock_t
*hlist_lock
;
1227 hlist_lock
= kretprobe_table_lock_ptr(hash
);
1228 raw_spin_unlock_irqrestore(hlist_lock
, *flags
);
1230 NOKPROBE_SYMBOL(kretprobe_hash_unlock
);
1232 static void kretprobe_table_unlock(unsigned long hash
,
1233 unsigned long *flags
)
1234 __releases(hlist_lock
)
1236 raw_spinlock_t
*hlist_lock
= kretprobe_table_lock_ptr(hash
);
1237 raw_spin_unlock_irqrestore(hlist_lock
, *flags
);
1239 NOKPROBE_SYMBOL(kretprobe_table_unlock
);
1241 struct kprobe kprobe_busy
= {
1242 .addr
= (void *) get_kprobe
,
1245 void kprobe_busy_begin(void)
1247 struct kprobe_ctlblk
*kcb
;
1250 __this_cpu_write(current_kprobe
, &kprobe_busy
);
1251 kcb
= get_kprobe_ctlblk();
1252 kcb
->kprobe_status
= KPROBE_HIT_ACTIVE
;
1255 void kprobe_busy_end(void)
1257 __this_cpu_write(current_kprobe
, NULL
);
1262 * This function is called from finish_task_switch when task tk becomes dead,
1263 * so that we can recycle any function-return probe instances associated
1264 * with this task. These left over instances represent probed functions
1265 * that have been called but will never return.
1267 void kprobe_flush_task(struct task_struct
*tk
)
1269 struct kretprobe_instance
*ri
;
1270 struct hlist_head
*head
, empty_rp
;
1271 struct hlist_node
*tmp
;
1272 unsigned long hash
, flags
= 0;
1274 if (unlikely(!kprobes_initialized
))
1275 /* Early boot. kretprobe_table_locks not yet initialized. */
1278 kprobe_busy_begin();
1280 INIT_HLIST_HEAD(&empty_rp
);
1281 hash
= hash_ptr(tk
, KPROBE_HASH_BITS
);
1282 head
= &kretprobe_inst_table
[hash
];
1283 kretprobe_table_lock(hash
, &flags
);
1284 hlist_for_each_entry_safe(ri
, tmp
, head
, hlist
) {
1286 recycle_rp_inst(ri
, &empty_rp
);
1288 kretprobe_table_unlock(hash
, &flags
);
1289 hlist_for_each_entry_safe(ri
, tmp
, &empty_rp
, hlist
) {
1290 hlist_del(&ri
->hlist
);
1296 NOKPROBE_SYMBOL(kprobe_flush_task
);
1298 static inline void free_rp_inst(struct kretprobe
*rp
)
1300 struct kretprobe_instance
*ri
;
1301 struct hlist_node
*next
;
1303 hlist_for_each_entry_safe(ri
, next
, &rp
->free_instances
, hlist
) {
1304 hlist_del(&ri
->hlist
);
1309 static void cleanup_rp_inst(struct kretprobe
*rp
)
1311 unsigned long flags
, hash
;
1312 struct kretprobe_instance
*ri
;
1313 struct hlist_node
*next
;
1314 struct hlist_head
*head
;
1317 for (hash
= 0; hash
< KPROBE_TABLE_SIZE
; hash
++) {
1318 kretprobe_table_lock(hash
, &flags
);
1319 head
= &kretprobe_inst_table
[hash
];
1320 hlist_for_each_entry_safe(ri
, next
, head
, hlist
) {
1324 kretprobe_table_unlock(hash
, &flags
);
1328 NOKPROBE_SYMBOL(cleanup_rp_inst
);
1330 /* Add the new probe to ap->list */
1331 static int add_new_kprobe(struct kprobe
*ap
, struct kprobe
*p
)
1333 if (p
->post_handler
)
1334 unoptimize_kprobe(ap
, true); /* Fall back to normal kprobe */
1336 list_add_rcu(&p
->list
, &ap
->list
);
1337 if (p
->post_handler
&& !ap
->post_handler
)
1338 ap
->post_handler
= aggr_post_handler
;
1344 * Fill in the required fields of the "manager kprobe". Replace the
1345 * earlier kprobe in the hlist with the manager kprobe
1347 static void init_aggr_kprobe(struct kprobe
*ap
, struct kprobe
*p
)
1349 /* Copy p's insn slot to ap */
1351 flush_insn_slot(ap
);
1353 ap
->flags
= p
->flags
& ~KPROBE_FLAG_OPTIMIZED
;
1354 ap
->pre_handler
= aggr_pre_handler
;
1355 ap
->fault_handler
= aggr_fault_handler
;
1356 /* We don't care the kprobe which has gone. */
1357 if (p
->post_handler
&& !kprobe_gone(p
))
1358 ap
->post_handler
= aggr_post_handler
;
1360 INIT_LIST_HEAD(&ap
->list
);
1361 INIT_HLIST_NODE(&ap
->hlist
);
1363 list_add_rcu(&p
->list
, &ap
->list
);
1364 hlist_replace_rcu(&p
->hlist
, &ap
->hlist
);
1368 * This is the second or subsequent kprobe at the address - handle
1371 static int register_aggr_kprobe(struct kprobe
*orig_p
, struct kprobe
*p
)
1374 struct kprobe
*ap
= orig_p
;
1378 /* For preparing optimization, jump_label_text_reserved() is called */
1380 mutex_lock(&text_mutex
);
1382 if (!kprobe_aggrprobe(orig_p
)) {
1383 /* If orig_p is not an aggr_kprobe, create new aggr_kprobe. */
1384 ap
= alloc_aggr_kprobe(orig_p
);
1389 init_aggr_kprobe(ap
, orig_p
);
1390 } else if (kprobe_unused(ap
)) {
1391 /* This probe is going to die. Rescue it */
1392 ret
= reuse_unused_kprobe(ap
);
1397 if (kprobe_gone(ap
)) {
1399 * Attempting to insert new probe at the same location that
1400 * had a probe in the module vaddr area which already
1401 * freed. So, the instruction slot has already been
1402 * released. We need a new slot for the new probe.
1404 ret
= arch_prepare_kprobe(ap
);
1407 * Even if fail to allocate new slot, don't need to
1408 * free aggr_probe. It will be used next time, or
1409 * freed by unregister_kprobe.
1413 /* Prepare optimized instructions if possible. */
1414 prepare_optimized_kprobe(ap
);
1417 * Clear gone flag to prevent allocating new slot again, and
1418 * set disabled flag because it is not armed yet.
1420 ap
->flags
= (ap
->flags
& ~KPROBE_FLAG_GONE
)
1421 | KPROBE_FLAG_DISABLED
;
1424 /* Copy ap's insn slot to p */
1426 ret
= add_new_kprobe(ap
, p
);
1429 mutex_unlock(&text_mutex
);
1430 jump_label_unlock();
1433 if (ret
== 0 && kprobe_disabled(ap
) && !kprobe_disabled(p
)) {
1434 ap
->flags
&= ~KPROBE_FLAG_DISABLED
;
1435 if (!kprobes_all_disarmed
) {
1436 /* Arm the breakpoint again. */
1437 ret
= arm_kprobe(ap
);
1439 ap
->flags
|= KPROBE_FLAG_DISABLED
;
1440 list_del_rcu(&p
->list
);
1448 bool __weak
arch_within_kprobe_blacklist(unsigned long addr
)
1450 /* The __kprobes marked functions and entry code must not be probed */
1451 return addr
>= (unsigned long)__kprobes_text_start
&&
1452 addr
< (unsigned long)__kprobes_text_end
;
1455 static bool __within_kprobe_blacklist(unsigned long addr
)
1457 struct kprobe_blacklist_entry
*ent
;
1459 if (arch_within_kprobe_blacklist(addr
))
1462 * If there exists a kprobe_blacklist, verify and
1463 * fail any probe registration in the prohibited area
1465 list_for_each_entry(ent
, &kprobe_blacklist
, list
) {
1466 if (addr
>= ent
->start_addr
&& addr
< ent
->end_addr
)
1472 bool within_kprobe_blacklist(unsigned long addr
)
1474 char symname
[KSYM_NAME_LEN
], *p
;
1476 if (__within_kprobe_blacklist(addr
))
1479 /* Check if the address is on a suffixed-symbol */
1480 if (!lookup_symbol_name(addr
, symname
)) {
1481 p
= strchr(symname
, '.');
1485 addr
= (unsigned long)kprobe_lookup_name(symname
, 0);
1487 return __within_kprobe_blacklist(addr
);
1493 * If we have a symbol_name argument, look it up and add the offset field
1494 * to it. This way, we can specify a relative address to a symbol.
1495 * This returns encoded errors if it fails to look up symbol or invalid
1496 * combination of parameters.
1498 static kprobe_opcode_t
*_kprobe_addr(kprobe_opcode_t
*addr
,
1499 const char *symbol_name
, unsigned int offset
)
1501 if ((symbol_name
&& addr
) || (!symbol_name
&& !addr
))
1505 addr
= kprobe_lookup_name(symbol_name
, offset
);
1507 return ERR_PTR(-ENOENT
);
1510 addr
= (kprobe_opcode_t
*)(((char *)addr
) + offset
);
1515 return ERR_PTR(-EINVAL
);
1518 static kprobe_opcode_t
*kprobe_addr(struct kprobe
*p
)
1520 return _kprobe_addr(p
->addr
, p
->symbol_name
, p
->offset
);
1523 /* Check passed kprobe is valid and return kprobe in kprobe_table. */
1524 static struct kprobe
*__get_valid_kprobe(struct kprobe
*p
)
1526 struct kprobe
*ap
, *list_p
;
1528 ap
= get_kprobe(p
->addr
);
1533 list_for_each_entry_rcu(list_p
, &ap
->list
, list
)
1535 /* kprobe p is a valid probe */
1543 /* Return error if the kprobe is being re-registered */
1544 static inline int check_kprobe_rereg(struct kprobe
*p
)
1548 mutex_lock(&kprobe_mutex
);
1549 if (__get_valid_kprobe(p
))
1551 mutex_unlock(&kprobe_mutex
);
1556 int __weak
arch_check_ftrace_location(struct kprobe
*p
)
1558 unsigned long ftrace_addr
;
1560 ftrace_addr
= ftrace_location((unsigned long)p
->addr
);
1562 #ifdef CONFIG_KPROBES_ON_FTRACE
1563 /* Given address is not on the instruction boundary */
1564 if ((unsigned long)p
->addr
!= ftrace_addr
)
1566 p
->flags
|= KPROBE_FLAG_FTRACE
;
1567 #else /* !CONFIG_KPROBES_ON_FTRACE */
1574 static int check_kprobe_address_safe(struct kprobe
*p
,
1575 struct module
**probed_mod
)
1579 ret
= arch_check_ftrace_location(p
);
1585 /* Ensure it is not in reserved area nor out of text */
1586 if (!kernel_text_address((unsigned long) p
->addr
) ||
1587 within_kprobe_blacklist((unsigned long) p
->addr
) ||
1588 jump_label_text_reserved(p
->addr
, p
->addr
) ||
1589 find_bug((unsigned long)p
->addr
)) {
1594 /* Check if are we probing a module */
1595 *probed_mod
= __module_text_address((unsigned long) p
->addr
);
1598 * We must hold a refcount of the probed module while updating
1599 * its code to prohibit unexpected unloading.
1601 if (unlikely(!try_module_get(*probed_mod
))) {
1607 * If the module freed .init.text, we couldn't insert
1610 if (within_module_init((unsigned long)p
->addr
, *probed_mod
) &&
1611 (*probed_mod
)->state
!= MODULE_STATE_COMING
) {
1612 module_put(*probed_mod
);
1619 jump_label_unlock();
1624 int register_kprobe(struct kprobe
*p
)
1627 struct kprobe
*old_p
;
1628 struct module
*probed_mod
;
1629 kprobe_opcode_t
*addr
;
1631 /* Adjust probe address from symbol */
1632 addr
= kprobe_addr(p
);
1634 return PTR_ERR(addr
);
1637 ret
= check_kprobe_rereg(p
);
1641 /* User can pass only KPROBE_FLAG_DISABLED to register_kprobe */
1642 p
->flags
&= KPROBE_FLAG_DISABLED
;
1644 INIT_LIST_HEAD(&p
->list
);
1646 ret
= check_kprobe_address_safe(p
, &probed_mod
);
1650 mutex_lock(&kprobe_mutex
);
1652 old_p
= get_kprobe(p
->addr
);
1654 /* Since this may unoptimize old_p, locking text_mutex. */
1655 ret
= register_aggr_kprobe(old_p
, p
);
1660 /* Prevent text modification */
1661 mutex_lock(&text_mutex
);
1662 ret
= prepare_kprobe(p
);
1663 mutex_unlock(&text_mutex
);
1668 INIT_HLIST_NODE(&p
->hlist
);
1669 hlist_add_head_rcu(&p
->hlist
,
1670 &kprobe_table
[hash_ptr(p
->addr
, KPROBE_HASH_BITS
)]);
1672 if (!kprobes_all_disarmed
&& !kprobe_disabled(p
)) {
1673 ret
= arm_kprobe(p
);
1675 hlist_del_rcu(&p
->hlist
);
1681 /* Try to optimize kprobe */
1682 try_to_optimize_kprobe(p
);
1684 mutex_unlock(&kprobe_mutex
);
1687 module_put(probed_mod
);
1691 EXPORT_SYMBOL_GPL(register_kprobe
);
1693 /* Check if all probes on the aggrprobe are disabled */
1694 static int aggr_kprobe_disabled(struct kprobe
*ap
)
1698 list_for_each_entry_rcu(kp
, &ap
->list
, list
)
1699 if (!kprobe_disabled(kp
))
1701 * There is an active probe on the list.
1702 * We can't disable this ap.
1709 /* Disable one kprobe: Make sure called under kprobe_mutex is locked */
1710 static struct kprobe
*__disable_kprobe(struct kprobe
*p
)
1712 struct kprobe
*orig_p
;
1715 /* Get an original kprobe for return */
1716 orig_p
= __get_valid_kprobe(p
);
1717 if (unlikely(orig_p
== NULL
))
1718 return ERR_PTR(-EINVAL
);
1720 if (!kprobe_disabled(p
)) {
1721 /* Disable probe if it is a child probe */
1723 p
->flags
|= KPROBE_FLAG_DISABLED
;
1725 /* Try to disarm and disable this/parent probe */
1726 if (p
== orig_p
|| aggr_kprobe_disabled(orig_p
)) {
1728 * If kprobes_all_disarmed is set, orig_p
1729 * should have already been disarmed, so
1730 * skip unneed disarming process.
1732 if (!kprobes_all_disarmed
) {
1733 ret
= disarm_kprobe(orig_p
, true);
1735 p
->flags
&= ~KPROBE_FLAG_DISABLED
;
1736 return ERR_PTR(ret
);
1739 orig_p
->flags
|= KPROBE_FLAG_DISABLED
;
1747 * Unregister a kprobe without a scheduler synchronization.
1749 static int __unregister_kprobe_top(struct kprobe
*p
)
1751 struct kprobe
*ap
, *list_p
;
1753 /* Disable kprobe. This will disarm it if needed. */
1754 ap
= __disable_kprobe(p
);
1760 * This probe is an independent(and non-optimized) kprobe
1761 * (not an aggrprobe). Remove from the hash list.
1765 /* Following process expects this probe is an aggrprobe */
1766 WARN_ON(!kprobe_aggrprobe(ap
));
1768 if (list_is_singular(&ap
->list
) && kprobe_disarmed(ap
))
1770 * !disarmed could be happen if the probe is under delayed
1775 /* If disabling probe has special handlers, update aggrprobe */
1776 if (p
->post_handler
&& !kprobe_gone(p
)) {
1777 list_for_each_entry_rcu(list_p
, &ap
->list
, list
) {
1778 if ((list_p
!= p
) && (list_p
->post_handler
))
1781 ap
->post_handler
= NULL
;
1785 * Remove from the aggrprobe: this path will do nothing in
1786 * __unregister_kprobe_bottom().
1788 list_del_rcu(&p
->list
);
1789 if (!kprobe_disabled(ap
) && !kprobes_all_disarmed
)
1791 * Try to optimize this probe again, because post
1792 * handler may have been changed.
1794 optimize_kprobe(ap
);
1799 hlist_del_rcu(&ap
->hlist
);
1803 static void __unregister_kprobe_bottom(struct kprobe
*p
)
1807 if (list_empty(&p
->list
))
1808 /* This is an independent kprobe */
1809 arch_remove_kprobe(p
);
1810 else if (list_is_singular(&p
->list
)) {
1811 /* This is the last child of an aggrprobe */
1812 ap
= list_entry(p
->list
.next
, struct kprobe
, list
);
1814 free_aggr_kprobe(ap
);
1816 /* Otherwise, do nothing. */
1819 int register_kprobes(struct kprobe
**kps
, int num
)
1825 for (i
= 0; i
< num
; i
++) {
1826 ret
= register_kprobe(kps
[i
]);
1829 unregister_kprobes(kps
, i
);
1835 EXPORT_SYMBOL_GPL(register_kprobes
);
1837 void unregister_kprobe(struct kprobe
*p
)
1839 unregister_kprobes(&p
, 1);
1841 EXPORT_SYMBOL_GPL(unregister_kprobe
);
1843 void unregister_kprobes(struct kprobe
**kps
, int num
)
1849 mutex_lock(&kprobe_mutex
);
1850 for (i
= 0; i
< num
; i
++)
1851 if (__unregister_kprobe_top(kps
[i
]) < 0)
1852 kps
[i
]->addr
= NULL
;
1853 mutex_unlock(&kprobe_mutex
);
1856 for (i
= 0; i
< num
; i
++)
1858 __unregister_kprobe_bottom(kps
[i
]);
1860 EXPORT_SYMBOL_GPL(unregister_kprobes
);
1862 int __weak
kprobe_exceptions_notify(struct notifier_block
*self
,
1863 unsigned long val
, void *data
)
1867 NOKPROBE_SYMBOL(kprobe_exceptions_notify
);
1869 static struct notifier_block kprobe_exceptions_nb
= {
1870 .notifier_call
= kprobe_exceptions_notify
,
1871 .priority
= 0x7fffffff /* we need to be notified first */
1874 unsigned long __weak
arch_deref_entry_point(void *entry
)
1876 return (unsigned long)entry
;
1879 #ifdef CONFIG_KRETPROBES
1881 * This kprobe pre_handler is registered with every kretprobe. When probe
1882 * hits it will set up the return probe.
1884 static int pre_handler_kretprobe(struct kprobe
*p
, struct pt_regs
*regs
)
1886 struct kretprobe
*rp
= container_of(p
, struct kretprobe
, kp
);
1887 unsigned long hash
, flags
= 0;
1888 struct kretprobe_instance
*ri
;
1891 * To avoid deadlocks, prohibit return probing in NMI contexts,
1892 * just skip the probe and increase the (inexact) 'nmissed'
1893 * statistical counter, so that the user is informed that
1894 * something happened:
1896 if (unlikely(in_nmi())) {
1901 /* TODO: consider to only swap the RA after the last pre_handler fired */
1902 hash
= hash_ptr(current
, KPROBE_HASH_BITS
);
1903 raw_spin_lock_irqsave(&rp
->lock
, flags
);
1904 if (!hlist_empty(&rp
->free_instances
)) {
1905 ri
= hlist_entry(rp
->free_instances
.first
,
1906 struct kretprobe_instance
, hlist
);
1907 hlist_del(&ri
->hlist
);
1908 raw_spin_unlock_irqrestore(&rp
->lock
, flags
);
1913 if (rp
->entry_handler
&& rp
->entry_handler(ri
, regs
)) {
1914 raw_spin_lock_irqsave(&rp
->lock
, flags
);
1915 hlist_add_head(&ri
->hlist
, &rp
->free_instances
);
1916 raw_spin_unlock_irqrestore(&rp
->lock
, flags
);
1920 arch_prepare_kretprobe(ri
, regs
);
1922 /* XXX(hch): why is there no hlist_move_head? */
1923 INIT_HLIST_NODE(&ri
->hlist
);
1924 kretprobe_table_lock(hash
, &flags
);
1925 hlist_add_head(&ri
->hlist
, &kretprobe_inst_table
[hash
]);
1926 kretprobe_table_unlock(hash
, &flags
);
1929 raw_spin_unlock_irqrestore(&rp
->lock
, flags
);
1933 NOKPROBE_SYMBOL(pre_handler_kretprobe
);
1935 bool __weak
arch_kprobe_on_func_entry(unsigned long offset
)
1940 bool kprobe_on_func_entry(kprobe_opcode_t
*addr
, const char *sym
, unsigned long offset
)
1942 kprobe_opcode_t
*kp_addr
= _kprobe_addr(addr
, sym
, offset
);
1944 if (IS_ERR(kp_addr
))
1947 if (!kallsyms_lookup_size_offset((unsigned long)kp_addr
, NULL
, &offset
) ||
1948 !arch_kprobe_on_func_entry(offset
))
1954 int register_kretprobe(struct kretprobe
*rp
)
1957 struct kretprobe_instance
*inst
;
1961 if (!kprobe_on_func_entry(rp
->kp
.addr
, rp
->kp
.symbol_name
, rp
->kp
.offset
))
1964 if (kretprobe_blacklist_size
) {
1965 addr
= kprobe_addr(&rp
->kp
);
1967 return PTR_ERR(addr
);
1969 for (i
= 0; kretprobe_blacklist
[i
].name
!= NULL
; i
++) {
1970 if (kretprobe_blacklist
[i
].addr
== addr
)
1975 rp
->kp
.pre_handler
= pre_handler_kretprobe
;
1976 rp
->kp
.post_handler
= NULL
;
1977 rp
->kp
.fault_handler
= NULL
;
1979 /* Pre-allocate memory for max kretprobe instances */
1980 if (rp
->maxactive
<= 0) {
1981 #ifdef CONFIG_PREEMPTION
1982 rp
->maxactive
= max_t(unsigned int, 10, 2*num_possible_cpus());
1984 rp
->maxactive
= num_possible_cpus();
1987 raw_spin_lock_init(&rp
->lock
);
1988 INIT_HLIST_HEAD(&rp
->free_instances
);
1989 for (i
= 0; i
< rp
->maxactive
; i
++) {
1990 inst
= kmalloc(sizeof(struct kretprobe_instance
) +
1991 rp
->data_size
, GFP_KERNEL
);
1996 INIT_HLIST_NODE(&inst
->hlist
);
1997 hlist_add_head(&inst
->hlist
, &rp
->free_instances
);
2001 /* Establish function entry probe point */
2002 ret
= register_kprobe(&rp
->kp
);
2007 EXPORT_SYMBOL_GPL(register_kretprobe
);
2009 int register_kretprobes(struct kretprobe
**rps
, int num
)
2015 for (i
= 0; i
< num
; i
++) {
2016 ret
= register_kretprobe(rps
[i
]);
2019 unregister_kretprobes(rps
, i
);
2025 EXPORT_SYMBOL_GPL(register_kretprobes
);
2027 void unregister_kretprobe(struct kretprobe
*rp
)
2029 unregister_kretprobes(&rp
, 1);
2031 EXPORT_SYMBOL_GPL(unregister_kretprobe
);
2033 void unregister_kretprobes(struct kretprobe
**rps
, int num
)
2039 mutex_lock(&kprobe_mutex
);
2040 for (i
= 0; i
< num
; i
++)
2041 if (__unregister_kprobe_top(&rps
[i
]->kp
) < 0)
2042 rps
[i
]->kp
.addr
= NULL
;
2043 mutex_unlock(&kprobe_mutex
);
2046 for (i
= 0; i
< num
; i
++) {
2047 if (rps
[i
]->kp
.addr
) {
2048 __unregister_kprobe_bottom(&rps
[i
]->kp
);
2049 cleanup_rp_inst(rps
[i
]);
2053 EXPORT_SYMBOL_GPL(unregister_kretprobes
);
2055 #else /* CONFIG_KRETPROBES */
2056 int register_kretprobe(struct kretprobe
*rp
)
2060 EXPORT_SYMBOL_GPL(register_kretprobe
);
2062 int register_kretprobes(struct kretprobe
**rps
, int num
)
2066 EXPORT_SYMBOL_GPL(register_kretprobes
);
2068 void unregister_kretprobe(struct kretprobe
*rp
)
2071 EXPORT_SYMBOL_GPL(unregister_kretprobe
);
2073 void unregister_kretprobes(struct kretprobe
**rps
, int num
)
2076 EXPORT_SYMBOL_GPL(unregister_kretprobes
);
2078 static int pre_handler_kretprobe(struct kprobe
*p
, struct pt_regs
*regs
)
2082 NOKPROBE_SYMBOL(pre_handler_kretprobe
);
2084 #endif /* CONFIG_KRETPROBES */
2086 /* Set the kprobe gone and remove its instruction buffer. */
2087 static void kill_kprobe(struct kprobe
*p
)
2091 p
->flags
|= KPROBE_FLAG_GONE
;
2092 if (kprobe_aggrprobe(p
)) {
2094 * If this is an aggr_kprobe, we have to list all the
2095 * chained probes and mark them GONE.
2097 list_for_each_entry_rcu(kp
, &p
->list
, list
)
2098 kp
->flags
|= KPROBE_FLAG_GONE
;
2099 p
->post_handler
= NULL
;
2100 kill_optimized_kprobe(p
);
2103 * Here, we can remove insn_slot safely, because no thread calls
2104 * the original probed function (which will be freed soon) any more.
2106 arch_remove_kprobe(p
);
2109 /* Disable one kprobe */
2110 int disable_kprobe(struct kprobe
*kp
)
2115 mutex_lock(&kprobe_mutex
);
2117 /* Disable this kprobe */
2118 p
= __disable_kprobe(kp
);
2122 mutex_unlock(&kprobe_mutex
);
2125 EXPORT_SYMBOL_GPL(disable_kprobe
);
2127 /* Enable one kprobe */
2128 int enable_kprobe(struct kprobe
*kp
)
2133 mutex_lock(&kprobe_mutex
);
2135 /* Check whether specified probe is valid. */
2136 p
= __get_valid_kprobe(kp
);
2137 if (unlikely(p
== NULL
)) {
2142 if (kprobe_gone(kp
)) {
2143 /* This kprobe has gone, we couldn't enable it. */
2149 kp
->flags
&= ~KPROBE_FLAG_DISABLED
;
2151 if (!kprobes_all_disarmed
&& kprobe_disabled(p
)) {
2152 p
->flags
&= ~KPROBE_FLAG_DISABLED
;
2153 ret
= arm_kprobe(p
);
2155 p
->flags
|= KPROBE_FLAG_DISABLED
;
2158 mutex_unlock(&kprobe_mutex
);
2161 EXPORT_SYMBOL_GPL(enable_kprobe
);
2163 /* Caller must NOT call this in usual path. This is only for critical case */
2164 void dump_kprobe(struct kprobe
*kp
)
2166 pr_err("Dumping kprobe:\n");
2167 pr_err("Name: %s\nOffset: %x\nAddress: %pS\n",
2168 kp
->symbol_name
, kp
->offset
, kp
->addr
);
2170 NOKPROBE_SYMBOL(dump_kprobe
);
2172 int kprobe_add_ksym_blacklist(unsigned long entry
)
2174 struct kprobe_blacklist_entry
*ent
;
2175 unsigned long offset
= 0, size
= 0;
2177 if (!kernel_text_address(entry
) ||
2178 !kallsyms_lookup_size_offset(entry
, &size
, &offset
))
2181 ent
= kmalloc(sizeof(*ent
), GFP_KERNEL
);
2184 ent
->start_addr
= entry
;
2185 ent
->end_addr
= entry
+ size
;
2186 INIT_LIST_HEAD(&ent
->list
);
2187 list_add_tail(&ent
->list
, &kprobe_blacklist
);
2192 /* Add all symbols in given area into kprobe blacklist */
2193 int kprobe_add_area_blacklist(unsigned long start
, unsigned long end
)
2195 unsigned long entry
;
2198 for (entry
= start
; entry
< end
; entry
+= ret
) {
2199 ret
= kprobe_add_ksym_blacklist(entry
);
2202 if (ret
== 0) /* In case of alias symbol */
2208 int __init __weak
arch_populate_kprobe_blacklist(void)
2214 * Lookup and populate the kprobe_blacklist.
2216 * Unlike the kretprobe blacklist, we'll need to determine
2217 * the range of addresses that belong to the said functions,
2218 * since a kprobe need not necessarily be at the beginning
2221 static int __init
populate_kprobe_blacklist(unsigned long *start
,
2224 unsigned long entry
;
2225 unsigned long *iter
;
2228 for (iter
= start
; iter
< end
; iter
++) {
2229 entry
= arch_deref_entry_point((void *)*iter
);
2230 ret
= kprobe_add_ksym_blacklist(entry
);
2237 /* Symbols in __kprobes_text are blacklisted */
2238 ret
= kprobe_add_area_blacklist((unsigned long)__kprobes_text_start
,
2239 (unsigned long)__kprobes_text_end
);
2241 return ret
? : arch_populate_kprobe_blacklist();
2244 /* Module notifier call back, checking kprobes on the module */
2245 static int kprobes_module_callback(struct notifier_block
*nb
,
2246 unsigned long val
, void *data
)
2248 struct module
*mod
= data
;
2249 struct hlist_head
*head
;
2252 int checkcore
= (val
== MODULE_STATE_GOING
);
2254 if (val
!= MODULE_STATE_GOING
&& val
!= MODULE_STATE_LIVE
)
2258 * When MODULE_STATE_GOING was notified, both of module .text and
2259 * .init.text sections would be freed. When MODULE_STATE_LIVE was
2260 * notified, only .init.text section would be freed. We need to
2261 * disable kprobes which have been inserted in the sections.
2263 mutex_lock(&kprobe_mutex
);
2264 for (i
= 0; i
< KPROBE_TABLE_SIZE
; i
++) {
2265 head
= &kprobe_table
[i
];
2266 hlist_for_each_entry_rcu(p
, head
, hlist
)
2267 if (within_module_init((unsigned long)p
->addr
, mod
) ||
2269 within_module_core((unsigned long)p
->addr
, mod
))) {
2271 * The vaddr this probe is installed will soon
2272 * be vfreed buy not synced to disk. Hence,
2273 * disarming the breakpoint isn't needed.
2275 * Note, this will also move any optimized probes
2276 * that are pending to be removed from their
2277 * corresponding lists to the freeing_list and
2278 * will not be touched by the delayed
2279 * kprobe_optimizer work handler.
2284 mutex_unlock(&kprobe_mutex
);
2288 static struct notifier_block kprobe_module_nb
= {
2289 .notifier_call
= kprobes_module_callback
,
2293 /* Markers of _kprobe_blacklist section */
2294 extern unsigned long __start_kprobe_blacklist
[];
2295 extern unsigned long __stop_kprobe_blacklist
[];
2297 static int __init
init_kprobes(void)
2301 /* FIXME allocate the probe table, currently defined statically */
2302 /* initialize all list heads */
2303 for (i
= 0; i
< KPROBE_TABLE_SIZE
; i
++) {
2304 INIT_HLIST_HEAD(&kprobe_table
[i
]);
2305 INIT_HLIST_HEAD(&kretprobe_inst_table
[i
]);
2306 raw_spin_lock_init(&(kretprobe_table_locks
[i
].lock
));
2309 err
= populate_kprobe_blacklist(__start_kprobe_blacklist
,
2310 __stop_kprobe_blacklist
);
2312 pr_err("kprobes: failed to populate blacklist: %d\n", err
);
2313 pr_err("Please take care of using kprobes.\n");
2316 if (kretprobe_blacklist_size
) {
2317 /* lookup the function address from its name */
2318 for (i
= 0; kretprobe_blacklist
[i
].name
!= NULL
; i
++) {
2319 kretprobe_blacklist
[i
].addr
=
2320 kprobe_lookup_name(kretprobe_blacklist
[i
].name
, 0);
2321 if (!kretprobe_blacklist
[i
].addr
)
2322 printk("kretprobe: lookup failed: %s\n",
2323 kretprobe_blacklist
[i
].name
);
2327 #if defined(CONFIG_OPTPROBES)
2328 #if defined(__ARCH_WANT_KPROBES_INSN_SLOT)
2329 /* Init kprobe_optinsn_slots */
2330 kprobe_optinsn_slots
.insn_size
= MAX_OPTINSN_SIZE
;
2332 /* By default, kprobes can be optimized */
2333 kprobes_allow_optimization
= true;
2336 /* By default, kprobes are armed */
2337 kprobes_all_disarmed
= false;
2339 err
= arch_init_kprobes();
2341 err
= register_die_notifier(&kprobe_exceptions_nb
);
2343 err
= register_module_notifier(&kprobe_module_nb
);
2345 kprobes_initialized
= (err
== 0);
2351 subsys_initcall(init_kprobes
);
2353 #ifdef CONFIG_DEBUG_FS
2354 static void report_probe(struct seq_file
*pi
, struct kprobe
*p
,
2355 const char *sym
, int offset
, char *modname
, struct kprobe
*pp
)
2358 void *addr
= p
->addr
;
2360 if (p
->pre_handler
== pre_handler_kretprobe
)
2365 if (!kallsyms_show_value(pi
->file
->f_cred
))
2369 seq_printf(pi
, "%px %s %s+0x%x %s ",
2370 addr
, kprobe_type
, sym
, offset
,
2371 (modname
? modname
: " "));
2372 else /* try to use %pS */
2373 seq_printf(pi
, "%px %s %pS ",
2374 addr
, kprobe_type
, p
->addr
);
2378 seq_printf(pi
, "%s%s%s%s\n",
2379 (kprobe_gone(p
) ? "[GONE]" : ""),
2380 ((kprobe_disabled(p
) && !kprobe_gone(p
)) ? "[DISABLED]" : ""),
2381 (kprobe_optimized(pp
) ? "[OPTIMIZED]" : ""),
2382 (kprobe_ftrace(pp
) ? "[FTRACE]" : ""));
2385 static void *kprobe_seq_start(struct seq_file
*f
, loff_t
*pos
)
2387 return (*pos
< KPROBE_TABLE_SIZE
) ? pos
: NULL
;
2390 static void *kprobe_seq_next(struct seq_file
*f
, void *v
, loff_t
*pos
)
2393 if (*pos
>= KPROBE_TABLE_SIZE
)
2398 static void kprobe_seq_stop(struct seq_file
*f
, void *v
)
2403 static int show_kprobe_addr(struct seq_file
*pi
, void *v
)
2405 struct hlist_head
*head
;
2406 struct kprobe
*p
, *kp
;
2407 const char *sym
= NULL
;
2408 unsigned int i
= *(loff_t
*) v
;
2409 unsigned long offset
= 0;
2410 char *modname
, namebuf
[KSYM_NAME_LEN
];
2412 head
= &kprobe_table
[i
];
2414 hlist_for_each_entry_rcu(p
, head
, hlist
) {
2415 sym
= kallsyms_lookup((unsigned long)p
->addr
, NULL
,
2416 &offset
, &modname
, namebuf
);
2417 if (kprobe_aggrprobe(p
)) {
2418 list_for_each_entry_rcu(kp
, &p
->list
, list
)
2419 report_probe(pi
, kp
, sym
, offset
, modname
, p
);
2421 report_probe(pi
, p
, sym
, offset
, modname
, NULL
);
2427 static const struct seq_operations kprobes_seq_ops
= {
2428 .start
= kprobe_seq_start
,
2429 .next
= kprobe_seq_next
,
2430 .stop
= kprobe_seq_stop
,
2431 .show
= show_kprobe_addr
2434 static int kprobes_open(struct inode
*inode
, struct file
*filp
)
2436 return seq_open(filp
, &kprobes_seq_ops
);
2439 static const struct file_operations debugfs_kprobes_operations
= {
2440 .open
= kprobes_open
,
2442 .llseek
= seq_lseek
,
2443 .release
= seq_release
,
2446 /* kprobes/blacklist -- shows which functions can not be probed */
2447 static void *kprobe_blacklist_seq_start(struct seq_file
*m
, loff_t
*pos
)
2449 return seq_list_start(&kprobe_blacklist
, *pos
);
2452 static void *kprobe_blacklist_seq_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2454 return seq_list_next(v
, &kprobe_blacklist
, pos
);
2457 static int kprobe_blacklist_seq_show(struct seq_file
*m
, void *v
)
2459 struct kprobe_blacklist_entry
*ent
=
2460 list_entry(v
, struct kprobe_blacklist_entry
, list
);
2463 * If /proc/kallsyms is not showing kernel address, we won't
2464 * show them here either.
2466 if (!kallsyms_show_value(m
->file
->f_cred
))
2467 seq_printf(m
, "0x%px-0x%px\t%ps\n", NULL
, NULL
,
2468 (void *)ent
->start_addr
);
2470 seq_printf(m
, "0x%px-0x%px\t%ps\n", (void *)ent
->start_addr
,
2471 (void *)ent
->end_addr
, (void *)ent
->start_addr
);
2475 static const struct seq_operations kprobe_blacklist_seq_ops
= {
2476 .start
= kprobe_blacklist_seq_start
,
2477 .next
= kprobe_blacklist_seq_next
,
2478 .stop
= kprobe_seq_stop
, /* Reuse void function */
2479 .show
= kprobe_blacklist_seq_show
,
2482 static int kprobe_blacklist_open(struct inode
*inode
, struct file
*filp
)
2484 return seq_open(filp
, &kprobe_blacklist_seq_ops
);
2487 static const struct file_operations debugfs_kprobe_blacklist_ops
= {
2488 .open
= kprobe_blacklist_open
,
2490 .llseek
= seq_lseek
,
2491 .release
= seq_release
,
2494 static int arm_all_kprobes(void)
2496 struct hlist_head
*head
;
2498 unsigned int i
, total
= 0, errors
= 0;
2501 mutex_lock(&kprobe_mutex
);
2503 /* If kprobes are armed, just return */
2504 if (!kprobes_all_disarmed
)
2505 goto already_enabled
;
2508 * optimize_kprobe() called by arm_kprobe() checks
2509 * kprobes_all_disarmed, so set kprobes_all_disarmed before
2512 kprobes_all_disarmed
= false;
2513 /* Arming kprobes doesn't optimize kprobe itself */
2514 for (i
= 0; i
< KPROBE_TABLE_SIZE
; i
++) {
2515 head
= &kprobe_table
[i
];
2516 /* Arm all kprobes on a best-effort basis */
2517 hlist_for_each_entry_rcu(p
, head
, hlist
) {
2518 if (!kprobe_disabled(p
)) {
2519 err
= arm_kprobe(p
);
2530 pr_warn("Kprobes globally enabled, but failed to arm %d out of %d probes\n",
2533 pr_info("Kprobes globally enabled\n");
2536 mutex_unlock(&kprobe_mutex
);
2540 static int disarm_all_kprobes(void)
2542 struct hlist_head
*head
;
2544 unsigned int i
, total
= 0, errors
= 0;
2547 mutex_lock(&kprobe_mutex
);
2549 /* If kprobes are already disarmed, just return */
2550 if (kprobes_all_disarmed
) {
2551 mutex_unlock(&kprobe_mutex
);
2555 kprobes_all_disarmed
= true;
2557 for (i
= 0; i
< KPROBE_TABLE_SIZE
; i
++) {
2558 head
= &kprobe_table
[i
];
2559 /* Disarm all kprobes on a best-effort basis */
2560 hlist_for_each_entry_rcu(p
, head
, hlist
) {
2561 if (!arch_trampoline_kprobe(p
) && !kprobe_disabled(p
)) {
2562 err
= disarm_kprobe(p
, false);
2573 pr_warn("Kprobes globally disabled, but failed to disarm %d out of %d probes\n",
2576 pr_info("Kprobes globally disabled\n");
2578 mutex_unlock(&kprobe_mutex
);
2580 /* Wait for disarming all kprobes by optimizer */
2581 wait_for_kprobe_optimizer();
2587 * XXX: The debugfs bool file interface doesn't allow for callbacks
2588 * when the bool state is switched. We can reuse that facility when
2591 static ssize_t
read_enabled_file_bool(struct file
*file
,
2592 char __user
*user_buf
, size_t count
, loff_t
*ppos
)
2596 if (!kprobes_all_disarmed
)
2602 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, 2);
2605 static ssize_t
write_enabled_file_bool(struct file
*file
,
2606 const char __user
*user_buf
, size_t count
, loff_t
*ppos
)
2612 buf_size
= min(count
, (sizeof(buf
)-1));
2613 if (copy_from_user(buf
, user_buf
, buf_size
))
2616 buf
[buf_size
] = '\0';
2621 ret
= arm_all_kprobes();
2626 ret
= disarm_all_kprobes();
2638 static const struct file_operations fops_kp
= {
2639 .read
= read_enabled_file_bool
,
2640 .write
= write_enabled_file_bool
,
2641 .llseek
= default_llseek
,
2644 static int __init
debugfs_kprobe_init(void)
2647 unsigned int value
= 1;
2649 dir
= debugfs_create_dir("kprobes", NULL
);
2651 debugfs_create_file("list", 0400, dir
, NULL
,
2652 &debugfs_kprobes_operations
);
2654 debugfs_create_file("enabled", 0600, dir
, &value
, &fops_kp
);
2656 debugfs_create_file("blacklist", 0400, dir
, NULL
,
2657 &debugfs_kprobe_blacklist_ops
);
2662 late_initcall(debugfs_kprobe_init
);
2663 #endif /* CONFIG_DEBUG_FS */