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
) {
336 NOKPROBE_SYMBOL(get_kprobe
);
338 static int aggr_pre_handler(struct kprobe
*p
, struct pt_regs
*regs
);
340 /* Return true if the kprobe is an aggregator */
341 static inline int kprobe_aggrprobe(struct kprobe
*p
)
343 return p
->pre_handler
== aggr_pre_handler
;
346 /* Return true(!0) if the kprobe is unused */
347 static inline int kprobe_unused(struct kprobe
*p
)
349 return kprobe_aggrprobe(p
) && kprobe_disabled(p
) &&
350 list_empty(&p
->list
);
354 * Keep all fields in the kprobe consistent
356 static inline void copy_kprobe(struct kprobe
*ap
, struct kprobe
*p
)
358 memcpy(&p
->opcode
, &ap
->opcode
, sizeof(kprobe_opcode_t
));
359 memcpy(&p
->ainsn
, &ap
->ainsn
, sizeof(struct arch_specific_insn
));
362 #ifdef CONFIG_OPTPROBES
363 /* NOTE: change this value only with kprobe_mutex held */
364 static bool kprobes_allow_optimization
;
367 * Call all pre_handler on the list, but ignores its return value.
368 * This must be called from arch-dep optimized caller.
370 void opt_pre_handler(struct kprobe
*p
, struct pt_regs
*regs
)
374 list_for_each_entry_rcu(kp
, &p
->list
, list
) {
375 if (kp
->pre_handler
&& likely(!kprobe_disabled(kp
))) {
376 set_kprobe_instance(kp
);
377 kp
->pre_handler(kp
, regs
);
379 reset_kprobe_instance();
382 NOKPROBE_SYMBOL(opt_pre_handler
);
384 /* Free optimized instructions and optimized_kprobe */
385 static void free_aggr_kprobe(struct kprobe
*p
)
387 struct optimized_kprobe
*op
;
389 op
= container_of(p
, struct optimized_kprobe
, kp
);
390 arch_remove_optimized_kprobe(op
);
391 arch_remove_kprobe(p
);
395 /* Return true(!0) if the kprobe is ready for optimization. */
396 static inline int kprobe_optready(struct kprobe
*p
)
398 struct optimized_kprobe
*op
;
400 if (kprobe_aggrprobe(p
)) {
401 op
= container_of(p
, struct optimized_kprobe
, kp
);
402 return arch_prepared_optinsn(&op
->optinsn
);
408 /* Return true(!0) if the kprobe is disarmed. Note: p must be on hash list */
409 static inline int kprobe_disarmed(struct kprobe
*p
)
411 struct optimized_kprobe
*op
;
413 /* If kprobe is not aggr/opt probe, just return kprobe is disabled */
414 if (!kprobe_aggrprobe(p
))
415 return kprobe_disabled(p
);
417 op
= container_of(p
, struct optimized_kprobe
, kp
);
419 return kprobe_disabled(p
) && list_empty(&op
->list
);
422 /* Return true(!0) if the probe is queued on (un)optimizing lists */
423 static int kprobe_queued(struct kprobe
*p
)
425 struct optimized_kprobe
*op
;
427 if (kprobe_aggrprobe(p
)) {
428 op
= container_of(p
, struct optimized_kprobe
, kp
);
429 if (!list_empty(&op
->list
))
436 * Return an optimized kprobe whose optimizing code replaces
437 * instructions including addr (exclude breakpoint).
439 static struct kprobe
*get_optimized_kprobe(unsigned long addr
)
442 struct kprobe
*p
= NULL
;
443 struct optimized_kprobe
*op
;
445 /* Don't check i == 0, since that is a breakpoint case. */
446 for (i
= 1; !p
&& i
< MAX_OPTIMIZED_LENGTH
; i
++)
447 p
= get_kprobe((void *)(addr
- i
));
449 if (p
&& kprobe_optready(p
)) {
450 op
= container_of(p
, struct optimized_kprobe
, kp
);
451 if (arch_within_optimized_kprobe(op
, addr
))
458 /* Optimization staging list, protected by kprobe_mutex */
459 static LIST_HEAD(optimizing_list
);
460 static LIST_HEAD(unoptimizing_list
);
461 static LIST_HEAD(freeing_list
);
463 static void kprobe_optimizer(struct work_struct
*work
);
464 static DECLARE_DELAYED_WORK(optimizing_work
, kprobe_optimizer
);
465 #define OPTIMIZE_DELAY 5
468 * Optimize (replace a breakpoint with a jump) kprobes listed on
471 static void do_optimize_kprobes(void)
473 lockdep_assert_held(&text_mutex
);
475 * The optimization/unoptimization refers online_cpus via
476 * stop_machine() and cpu-hotplug modifies online_cpus.
477 * And same time, text_mutex will be held in cpu-hotplug and here.
478 * This combination can cause a deadlock (cpu-hotplug try to lock
479 * text_mutex but stop_machine can not be done because online_cpus
481 * To avoid this deadlock, caller must have locked cpu hotplug
482 * for preventing cpu-hotplug outside of text_mutex locking.
484 lockdep_assert_cpus_held();
486 /* Optimization never be done when disarmed */
487 if (kprobes_all_disarmed
|| !kprobes_allow_optimization
||
488 list_empty(&optimizing_list
))
491 arch_optimize_kprobes(&optimizing_list
);
495 * Unoptimize (replace a jump with a breakpoint and remove the breakpoint
496 * if need) kprobes listed on unoptimizing_list.
498 static void do_unoptimize_kprobes(void)
500 struct optimized_kprobe
*op
, *tmp
;
502 lockdep_assert_held(&text_mutex
);
503 /* See comment in do_optimize_kprobes() */
504 lockdep_assert_cpus_held();
506 /* Unoptimization must be done anytime */
507 if (list_empty(&unoptimizing_list
))
510 arch_unoptimize_kprobes(&unoptimizing_list
, &freeing_list
);
511 /* Loop free_list for disarming */
512 list_for_each_entry_safe(op
, tmp
, &freeing_list
, list
) {
513 /* Switching from detour code to origin */
514 op
->kp
.flags
&= ~KPROBE_FLAG_OPTIMIZED
;
515 /* Disarm probes if marked disabled */
516 if (kprobe_disabled(&op
->kp
))
517 arch_disarm_kprobe(&op
->kp
);
518 if (kprobe_unused(&op
->kp
)) {
520 * Remove unused probes from hash list. After waiting
521 * for synchronization, these probes are reclaimed.
522 * (reclaiming is done by do_free_cleaned_kprobes.)
524 hlist_del_rcu(&op
->kp
.hlist
);
526 list_del_init(&op
->list
);
530 /* Reclaim all kprobes on the free_list */
531 static void do_free_cleaned_kprobes(void)
533 struct optimized_kprobe
*op
, *tmp
;
535 list_for_each_entry_safe(op
, tmp
, &freeing_list
, list
) {
536 list_del_init(&op
->list
);
537 if (WARN_ON_ONCE(!kprobe_unused(&op
->kp
))) {
539 * This must not happen, but if there is a kprobe
540 * still in use, keep it on kprobes hash list.
544 free_aggr_kprobe(&op
->kp
);
548 /* Start optimizer after OPTIMIZE_DELAY passed */
549 static void kick_kprobe_optimizer(void)
551 schedule_delayed_work(&optimizing_work
, OPTIMIZE_DELAY
);
554 /* Kprobe jump optimizer */
555 static void kprobe_optimizer(struct work_struct
*work
)
557 mutex_lock(&kprobe_mutex
);
559 mutex_lock(&text_mutex
);
560 /* Lock modules while optimizing kprobes */
561 mutex_lock(&module_mutex
);
564 * Step 1: Unoptimize kprobes and collect cleaned (unused and disarmed)
565 * kprobes before waiting for quiesence period.
567 do_unoptimize_kprobes();
570 * Step 2: Wait for quiesence period to ensure all potentially
571 * preempted tasks to have normally scheduled. Because optprobe
572 * may modify multiple instructions, there is a chance that Nth
573 * instruction is preempted. In that case, such tasks can return
574 * to 2nd-Nth byte of jump instruction. This wait is for avoiding it.
575 * Note that on non-preemptive kernel, this is transparently converted
576 * to synchronoze_sched() to wait for all interrupts to have completed.
578 synchronize_rcu_tasks();
580 /* Step 3: Optimize kprobes after quiesence period */
581 do_optimize_kprobes();
583 /* Step 4: Free cleaned kprobes after quiesence period */
584 do_free_cleaned_kprobes();
586 mutex_unlock(&module_mutex
);
587 mutex_unlock(&text_mutex
);
590 /* Step 5: Kick optimizer again if needed */
591 if (!list_empty(&optimizing_list
) || !list_empty(&unoptimizing_list
))
592 kick_kprobe_optimizer();
594 mutex_unlock(&kprobe_mutex
);
597 /* Wait for completing optimization and unoptimization */
598 void wait_for_kprobe_optimizer(void)
600 mutex_lock(&kprobe_mutex
);
602 while (!list_empty(&optimizing_list
) || !list_empty(&unoptimizing_list
)) {
603 mutex_unlock(&kprobe_mutex
);
605 /* this will also make optimizing_work execute immmediately */
606 flush_delayed_work(&optimizing_work
);
607 /* @optimizing_work might not have been queued yet, relax */
610 mutex_lock(&kprobe_mutex
);
613 mutex_unlock(&kprobe_mutex
);
616 static bool optprobe_queued_unopt(struct optimized_kprobe
*op
)
618 struct optimized_kprobe
*_op
;
620 list_for_each_entry(_op
, &unoptimizing_list
, list
) {
628 /* Optimize kprobe if p is ready to be optimized */
629 static void optimize_kprobe(struct kprobe
*p
)
631 struct optimized_kprobe
*op
;
633 /* Check if the kprobe is disabled or not ready for optimization. */
634 if (!kprobe_optready(p
) || !kprobes_allow_optimization
||
635 (kprobe_disabled(p
) || kprobes_all_disarmed
))
638 /* kprobes with post_handler can not be optimized */
642 op
= container_of(p
, struct optimized_kprobe
, kp
);
644 /* Check there is no other kprobes at the optimized instructions */
645 if (arch_check_optimized_kprobe(op
) < 0)
648 /* Check if it is already optimized. */
649 if (op
->kp
.flags
& KPROBE_FLAG_OPTIMIZED
) {
650 if (optprobe_queued_unopt(op
)) {
651 /* This is under unoptimizing. Just dequeue the probe */
652 list_del_init(&op
->list
);
656 op
->kp
.flags
|= KPROBE_FLAG_OPTIMIZED
;
658 /* On unoptimizing/optimizing_list, op must have OPTIMIZED flag */
659 if (WARN_ON_ONCE(!list_empty(&op
->list
)))
662 list_add(&op
->list
, &optimizing_list
);
663 kick_kprobe_optimizer();
666 /* Short cut to direct unoptimizing */
667 static void force_unoptimize_kprobe(struct optimized_kprobe
*op
)
669 lockdep_assert_cpus_held();
670 arch_unoptimize_kprobe(op
);
671 op
->kp
.flags
&= ~KPROBE_FLAG_OPTIMIZED
;
672 if (kprobe_disabled(&op
->kp
))
673 arch_disarm_kprobe(&op
->kp
);
676 /* Unoptimize a kprobe if p is optimized */
677 static void unoptimize_kprobe(struct kprobe
*p
, bool force
)
679 struct optimized_kprobe
*op
;
681 if (!kprobe_aggrprobe(p
) || kprobe_disarmed(p
))
682 return; /* This is not an optprobe nor optimized */
684 op
= container_of(p
, struct optimized_kprobe
, kp
);
685 if (!kprobe_optimized(p
))
688 if (!list_empty(&op
->list
)) {
689 if (optprobe_queued_unopt(op
)) {
690 /* Queued in unoptimizing queue */
693 * Forcibly unoptimize the kprobe here, and queue it
694 * in the freeing list for release afterwards.
696 force_unoptimize_kprobe(op
);
697 list_move(&op
->list
, &freeing_list
);
700 /* Dequeue from the optimizing queue */
701 list_del_init(&op
->list
);
702 op
->kp
.flags
&= ~KPROBE_FLAG_OPTIMIZED
;
707 /* Optimized kprobe case */
709 /* Forcibly update the code: this is a special case */
710 force_unoptimize_kprobe(op
);
712 list_add(&op
->list
, &unoptimizing_list
);
713 kick_kprobe_optimizer();
717 /* Cancel unoptimizing for reusing */
718 static int reuse_unused_kprobe(struct kprobe
*ap
)
720 struct optimized_kprobe
*op
;
723 * Unused kprobe MUST be on the way of delayed unoptimizing (means
724 * there is still a relative jump) and disabled.
726 op
= container_of(ap
, struct optimized_kprobe
, kp
);
727 WARN_ON_ONCE(list_empty(&op
->list
));
728 /* Enable the probe again */
729 ap
->flags
&= ~KPROBE_FLAG_DISABLED
;
730 /* Optimize it again (remove from op->list) */
731 if (!kprobe_optready(ap
))
738 /* Remove optimized instructions */
739 static void kill_optimized_kprobe(struct kprobe
*p
)
741 struct optimized_kprobe
*op
;
743 op
= container_of(p
, struct optimized_kprobe
, kp
);
744 if (!list_empty(&op
->list
))
745 /* Dequeue from the (un)optimization queue */
746 list_del_init(&op
->list
);
747 op
->kp
.flags
&= ~KPROBE_FLAG_OPTIMIZED
;
749 if (kprobe_unused(p
)) {
750 /* Enqueue if it is unused */
751 list_add(&op
->list
, &freeing_list
);
753 * Remove unused probes from the hash list. After waiting
754 * for synchronization, this probe is reclaimed.
755 * (reclaiming is done by do_free_cleaned_kprobes().)
757 hlist_del_rcu(&op
->kp
.hlist
);
760 /* Don't touch the code, because it is already freed. */
761 arch_remove_optimized_kprobe(op
);
765 void __prepare_optimized_kprobe(struct optimized_kprobe
*op
, struct kprobe
*p
)
767 if (!kprobe_ftrace(p
))
768 arch_prepare_optimized_kprobe(op
, p
);
771 /* Try to prepare optimized instructions */
772 static void prepare_optimized_kprobe(struct kprobe
*p
)
774 struct optimized_kprobe
*op
;
776 op
= container_of(p
, struct optimized_kprobe
, kp
);
777 __prepare_optimized_kprobe(op
, p
);
780 /* Allocate new optimized_kprobe and try to prepare optimized instructions */
781 static struct kprobe
*alloc_aggr_kprobe(struct kprobe
*p
)
783 struct optimized_kprobe
*op
;
785 op
= kzalloc(sizeof(struct optimized_kprobe
), GFP_KERNEL
);
789 INIT_LIST_HEAD(&op
->list
);
790 op
->kp
.addr
= p
->addr
;
791 __prepare_optimized_kprobe(op
, p
);
796 static void init_aggr_kprobe(struct kprobe
*ap
, struct kprobe
*p
);
799 * Prepare an optimized_kprobe and optimize it
800 * NOTE: p must be a normal registered kprobe
802 static void try_to_optimize_kprobe(struct kprobe
*p
)
805 struct optimized_kprobe
*op
;
807 /* Impossible to optimize ftrace-based kprobe */
808 if (kprobe_ftrace(p
))
811 /* For preparing optimization, jump_label_text_reserved() is called */
814 mutex_lock(&text_mutex
);
816 ap
= alloc_aggr_kprobe(p
);
820 op
= container_of(ap
, struct optimized_kprobe
, kp
);
821 if (!arch_prepared_optinsn(&op
->optinsn
)) {
822 /* If failed to setup optimizing, fallback to kprobe */
823 arch_remove_optimized_kprobe(op
);
828 init_aggr_kprobe(ap
, p
);
829 optimize_kprobe(ap
); /* This just kicks optimizer thread */
832 mutex_unlock(&text_mutex
);
838 static void optimize_all_kprobes(void)
840 struct hlist_head
*head
;
844 mutex_lock(&kprobe_mutex
);
845 /* If optimization is already allowed, just return */
846 if (kprobes_allow_optimization
)
850 kprobes_allow_optimization
= true;
851 for (i
= 0; i
< KPROBE_TABLE_SIZE
; i
++) {
852 head
= &kprobe_table
[i
];
853 hlist_for_each_entry_rcu(p
, head
, hlist
)
854 if (!kprobe_disabled(p
))
858 printk(KERN_INFO
"Kprobes globally optimized\n");
860 mutex_unlock(&kprobe_mutex
);
863 static void unoptimize_all_kprobes(void)
865 struct hlist_head
*head
;
869 mutex_lock(&kprobe_mutex
);
870 /* If optimization is already prohibited, just return */
871 if (!kprobes_allow_optimization
) {
872 mutex_unlock(&kprobe_mutex
);
877 kprobes_allow_optimization
= false;
878 for (i
= 0; i
< KPROBE_TABLE_SIZE
; i
++) {
879 head
= &kprobe_table
[i
];
880 hlist_for_each_entry_rcu(p
, head
, hlist
) {
881 if (!kprobe_disabled(p
))
882 unoptimize_kprobe(p
, false);
886 mutex_unlock(&kprobe_mutex
);
888 /* Wait for unoptimizing completion */
889 wait_for_kprobe_optimizer();
890 printk(KERN_INFO
"Kprobes globally unoptimized\n");
893 static DEFINE_MUTEX(kprobe_sysctl_mutex
);
894 int sysctl_kprobes_optimization
;
895 int proc_kprobes_optimization_handler(struct ctl_table
*table
, int write
,
896 void __user
*buffer
, size_t *length
,
901 mutex_lock(&kprobe_sysctl_mutex
);
902 sysctl_kprobes_optimization
= kprobes_allow_optimization
? 1 : 0;
903 ret
= proc_dointvec_minmax(table
, write
, buffer
, length
, ppos
);
905 if (sysctl_kprobes_optimization
)
906 optimize_all_kprobes();
908 unoptimize_all_kprobes();
909 mutex_unlock(&kprobe_sysctl_mutex
);
913 #endif /* CONFIG_SYSCTL */
915 /* Put a breakpoint for a probe. Must be called with text_mutex locked */
916 static void __arm_kprobe(struct kprobe
*p
)
920 /* Check collision with other optimized kprobes */
921 _p
= get_optimized_kprobe((unsigned long)p
->addr
);
923 /* Fallback to unoptimized kprobe */
924 unoptimize_kprobe(_p
, true);
927 optimize_kprobe(p
); /* Try to optimize (add kprobe to a list) */
930 /* Remove the breakpoint of a probe. Must be called with text_mutex locked */
931 static void __disarm_kprobe(struct kprobe
*p
, bool reopt
)
935 /* Try to unoptimize */
936 unoptimize_kprobe(p
, kprobes_all_disarmed
);
938 if (!kprobe_queued(p
)) {
939 arch_disarm_kprobe(p
);
940 /* If another kprobe was blocked, optimize it. */
941 _p
= get_optimized_kprobe((unsigned long)p
->addr
);
942 if (unlikely(_p
) && reopt
)
945 /* TODO: reoptimize others after unoptimized this probe */
948 #else /* !CONFIG_OPTPROBES */
950 #define optimize_kprobe(p) do {} while (0)
951 #define unoptimize_kprobe(p, f) do {} while (0)
952 #define kill_optimized_kprobe(p) do {} while (0)
953 #define prepare_optimized_kprobe(p) do {} while (0)
954 #define try_to_optimize_kprobe(p) do {} while (0)
955 #define __arm_kprobe(p) arch_arm_kprobe(p)
956 #define __disarm_kprobe(p, o) arch_disarm_kprobe(p)
957 #define kprobe_disarmed(p) kprobe_disabled(p)
958 #define wait_for_kprobe_optimizer() do {} while (0)
960 static int reuse_unused_kprobe(struct kprobe
*ap
)
963 * If the optimized kprobe is NOT supported, the aggr kprobe is
964 * released at the same time that the last aggregated kprobe is
966 * Thus there should be no chance to reuse unused kprobe.
968 printk(KERN_ERR
"Error: There should be no unused kprobe here.\n");
972 static void free_aggr_kprobe(struct kprobe
*p
)
974 arch_remove_kprobe(p
);
978 static struct kprobe
*alloc_aggr_kprobe(struct kprobe
*p
)
980 return kzalloc(sizeof(struct kprobe
), GFP_KERNEL
);
982 #endif /* CONFIG_OPTPROBES */
984 #ifdef CONFIG_KPROBES_ON_FTRACE
985 static struct ftrace_ops kprobe_ftrace_ops __read_mostly
= {
986 .func
= kprobe_ftrace_handler
,
987 .flags
= FTRACE_OPS_FL_SAVE_REGS
,
990 static struct ftrace_ops kprobe_ipmodify_ops __read_mostly
= {
991 .func
= kprobe_ftrace_handler
,
992 .flags
= FTRACE_OPS_FL_SAVE_REGS
| FTRACE_OPS_FL_IPMODIFY
,
995 static int kprobe_ipmodify_enabled
;
996 static int kprobe_ftrace_enabled
;
998 /* Must ensure p->addr is really on ftrace */
999 static int prepare_kprobe(struct kprobe
*p
)
1001 if (!kprobe_ftrace(p
))
1002 return arch_prepare_kprobe(p
);
1004 return arch_prepare_kprobe_ftrace(p
);
1007 /* Caller must lock kprobe_mutex */
1008 static int __arm_kprobe_ftrace(struct kprobe
*p
, struct ftrace_ops
*ops
,
1013 ret
= ftrace_set_filter_ip(ops
, (unsigned long)p
->addr
, 0, 0);
1015 pr_debug("Failed to arm kprobe-ftrace at %pS (%d)\n",
1021 ret
= register_ftrace_function(ops
);
1023 pr_debug("Failed to init kprobe-ftrace (%d)\n", ret
);
1033 * At this point, sinec ops is not registered, we should be sefe from
1034 * registering empty filter.
1036 ftrace_set_filter_ip(ops
, (unsigned long)p
->addr
, 1, 0);
1040 static int arm_kprobe_ftrace(struct kprobe
*p
)
1042 bool ipmodify
= (p
->post_handler
!= NULL
);
1044 return __arm_kprobe_ftrace(p
,
1045 ipmodify
? &kprobe_ipmodify_ops
: &kprobe_ftrace_ops
,
1046 ipmodify
? &kprobe_ipmodify_enabled
: &kprobe_ftrace_enabled
);
1049 /* Caller must lock kprobe_mutex */
1050 static int __disarm_kprobe_ftrace(struct kprobe
*p
, struct ftrace_ops
*ops
,
1056 ret
= unregister_ftrace_function(ops
);
1057 if (WARN(ret
< 0, "Failed to unregister kprobe-ftrace (%d)\n", ret
))
1063 ret
= ftrace_set_filter_ip(ops
, (unsigned long)p
->addr
, 1, 0);
1064 WARN_ONCE(ret
< 0, "Failed to disarm kprobe-ftrace at %pS (%d)\n",
1069 static int disarm_kprobe_ftrace(struct kprobe
*p
)
1071 bool ipmodify
= (p
->post_handler
!= NULL
);
1073 return __disarm_kprobe_ftrace(p
,
1074 ipmodify
? &kprobe_ipmodify_ops
: &kprobe_ftrace_ops
,
1075 ipmodify
? &kprobe_ipmodify_enabled
: &kprobe_ftrace_enabled
);
1077 #else /* !CONFIG_KPROBES_ON_FTRACE */
1078 #define prepare_kprobe(p) arch_prepare_kprobe(p)
1079 #define arm_kprobe_ftrace(p) (-ENODEV)
1080 #define disarm_kprobe_ftrace(p) (-ENODEV)
1083 /* Arm a kprobe with text_mutex */
1084 static int arm_kprobe(struct kprobe
*kp
)
1086 if (unlikely(kprobe_ftrace(kp
)))
1087 return arm_kprobe_ftrace(kp
);
1090 mutex_lock(&text_mutex
);
1092 mutex_unlock(&text_mutex
);
1098 /* Disarm a kprobe with text_mutex */
1099 static int disarm_kprobe(struct kprobe
*kp
, bool reopt
)
1101 if (unlikely(kprobe_ftrace(kp
)))
1102 return disarm_kprobe_ftrace(kp
);
1105 mutex_lock(&text_mutex
);
1106 __disarm_kprobe(kp
, reopt
);
1107 mutex_unlock(&text_mutex
);
1114 * Aggregate handlers for multiple kprobes support - these handlers
1115 * take care of invoking the individual kprobe handlers on p->list
1117 static int aggr_pre_handler(struct kprobe
*p
, struct pt_regs
*regs
)
1121 list_for_each_entry_rcu(kp
, &p
->list
, list
) {
1122 if (kp
->pre_handler
&& likely(!kprobe_disabled(kp
))) {
1123 set_kprobe_instance(kp
);
1124 if (kp
->pre_handler(kp
, regs
))
1127 reset_kprobe_instance();
1131 NOKPROBE_SYMBOL(aggr_pre_handler
);
1133 static void aggr_post_handler(struct kprobe
*p
, struct pt_regs
*regs
,
1134 unsigned long flags
)
1138 list_for_each_entry_rcu(kp
, &p
->list
, list
) {
1139 if (kp
->post_handler
&& likely(!kprobe_disabled(kp
))) {
1140 set_kprobe_instance(kp
);
1141 kp
->post_handler(kp
, regs
, flags
);
1142 reset_kprobe_instance();
1146 NOKPROBE_SYMBOL(aggr_post_handler
);
1148 static int aggr_fault_handler(struct kprobe
*p
, struct pt_regs
*regs
,
1151 struct kprobe
*cur
= __this_cpu_read(kprobe_instance
);
1154 * if we faulted "during" the execution of a user specified
1155 * probe handler, invoke just that probe's fault handler
1157 if (cur
&& cur
->fault_handler
) {
1158 if (cur
->fault_handler(cur
, regs
, trapnr
))
1163 NOKPROBE_SYMBOL(aggr_fault_handler
);
1165 /* Walks the list and increments nmissed count for multiprobe case */
1166 void kprobes_inc_nmissed_count(struct kprobe
*p
)
1169 if (!kprobe_aggrprobe(p
)) {
1172 list_for_each_entry_rcu(kp
, &p
->list
, list
)
1177 NOKPROBE_SYMBOL(kprobes_inc_nmissed_count
);
1179 void recycle_rp_inst(struct kretprobe_instance
*ri
,
1180 struct hlist_head
*head
)
1182 struct kretprobe
*rp
= ri
->rp
;
1184 /* remove rp inst off the rprobe_inst_table */
1185 hlist_del(&ri
->hlist
);
1186 INIT_HLIST_NODE(&ri
->hlist
);
1188 raw_spin_lock(&rp
->lock
);
1189 hlist_add_head(&ri
->hlist
, &rp
->free_instances
);
1190 raw_spin_unlock(&rp
->lock
);
1193 hlist_add_head(&ri
->hlist
, head
);
1195 NOKPROBE_SYMBOL(recycle_rp_inst
);
1197 void kretprobe_hash_lock(struct task_struct
*tsk
,
1198 struct hlist_head
**head
, unsigned long *flags
)
1199 __acquires(hlist_lock
)
1201 unsigned long hash
= hash_ptr(tsk
, KPROBE_HASH_BITS
);
1202 raw_spinlock_t
*hlist_lock
;
1204 *head
= &kretprobe_inst_table
[hash
];
1205 hlist_lock
= kretprobe_table_lock_ptr(hash
);
1206 raw_spin_lock_irqsave(hlist_lock
, *flags
);
1208 NOKPROBE_SYMBOL(kretprobe_hash_lock
);
1210 static void kretprobe_table_lock(unsigned long hash
,
1211 unsigned long *flags
)
1212 __acquires(hlist_lock
)
1214 raw_spinlock_t
*hlist_lock
= kretprobe_table_lock_ptr(hash
);
1215 raw_spin_lock_irqsave(hlist_lock
, *flags
);
1217 NOKPROBE_SYMBOL(kretprobe_table_lock
);
1219 void kretprobe_hash_unlock(struct task_struct
*tsk
,
1220 unsigned long *flags
)
1221 __releases(hlist_lock
)
1223 unsigned long hash
= hash_ptr(tsk
, KPROBE_HASH_BITS
);
1224 raw_spinlock_t
*hlist_lock
;
1226 hlist_lock
= kretprobe_table_lock_ptr(hash
);
1227 raw_spin_unlock_irqrestore(hlist_lock
, *flags
);
1229 NOKPROBE_SYMBOL(kretprobe_hash_unlock
);
1231 static void kretprobe_table_unlock(unsigned long hash
,
1232 unsigned long *flags
)
1233 __releases(hlist_lock
)
1235 raw_spinlock_t
*hlist_lock
= kretprobe_table_lock_ptr(hash
);
1236 raw_spin_unlock_irqrestore(hlist_lock
, *flags
);
1238 NOKPROBE_SYMBOL(kretprobe_table_unlock
);
1240 struct kprobe kprobe_busy
= {
1241 .addr
= (void *) get_kprobe
,
1244 void kprobe_busy_begin(void)
1246 struct kprobe_ctlblk
*kcb
;
1249 __this_cpu_write(current_kprobe
, &kprobe_busy
);
1250 kcb
= get_kprobe_ctlblk();
1251 kcb
->kprobe_status
= KPROBE_HIT_ACTIVE
;
1254 void kprobe_busy_end(void)
1256 __this_cpu_write(current_kprobe
, NULL
);
1261 * This function is called from finish_task_switch when task tk becomes dead,
1262 * so that we can recycle any function-return probe instances associated
1263 * with this task. These left over instances represent probed functions
1264 * that have been called but will never return.
1266 void kprobe_flush_task(struct task_struct
*tk
)
1268 struct kretprobe_instance
*ri
;
1269 struct hlist_head
*head
, empty_rp
;
1270 struct hlist_node
*tmp
;
1271 unsigned long hash
, flags
= 0;
1273 if (unlikely(!kprobes_initialized
))
1274 /* Early boot. kretprobe_table_locks not yet initialized. */
1277 kprobe_busy_begin();
1279 INIT_HLIST_HEAD(&empty_rp
);
1280 hash
= hash_ptr(tk
, KPROBE_HASH_BITS
);
1281 head
= &kretprobe_inst_table
[hash
];
1282 kretprobe_table_lock(hash
, &flags
);
1283 hlist_for_each_entry_safe(ri
, tmp
, head
, hlist
) {
1285 recycle_rp_inst(ri
, &empty_rp
);
1287 kretprobe_table_unlock(hash
, &flags
);
1288 hlist_for_each_entry_safe(ri
, tmp
, &empty_rp
, hlist
) {
1289 hlist_del(&ri
->hlist
);
1295 NOKPROBE_SYMBOL(kprobe_flush_task
);
1297 static inline void free_rp_inst(struct kretprobe
*rp
)
1299 struct kretprobe_instance
*ri
;
1300 struct hlist_node
*next
;
1302 hlist_for_each_entry_safe(ri
, next
, &rp
->free_instances
, hlist
) {
1303 hlist_del(&ri
->hlist
);
1308 static void cleanup_rp_inst(struct kretprobe
*rp
)
1310 unsigned long flags
, hash
;
1311 struct kretprobe_instance
*ri
;
1312 struct hlist_node
*next
;
1313 struct hlist_head
*head
;
1316 for (hash
= 0; hash
< KPROBE_TABLE_SIZE
; hash
++) {
1317 kretprobe_table_lock(hash
, &flags
);
1318 head
= &kretprobe_inst_table
[hash
];
1319 hlist_for_each_entry_safe(ri
, next
, head
, hlist
) {
1323 kretprobe_table_unlock(hash
, &flags
);
1327 NOKPROBE_SYMBOL(cleanup_rp_inst
);
1329 /* Add the new probe to ap->list */
1330 static int add_new_kprobe(struct kprobe
*ap
, struct kprobe
*p
)
1332 if (p
->post_handler
)
1333 unoptimize_kprobe(ap
, true); /* Fall back to normal kprobe */
1335 list_add_rcu(&p
->list
, &ap
->list
);
1336 if (p
->post_handler
&& !ap
->post_handler
)
1337 ap
->post_handler
= aggr_post_handler
;
1343 * Fill in the required fields of the "manager kprobe". Replace the
1344 * earlier kprobe in the hlist with the manager kprobe
1346 static void init_aggr_kprobe(struct kprobe
*ap
, struct kprobe
*p
)
1348 /* Copy p's insn slot to ap */
1350 flush_insn_slot(ap
);
1352 ap
->flags
= p
->flags
& ~KPROBE_FLAG_OPTIMIZED
;
1353 ap
->pre_handler
= aggr_pre_handler
;
1354 ap
->fault_handler
= aggr_fault_handler
;
1355 /* We don't care the kprobe which has gone. */
1356 if (p
->post_handler
&& !kprobe_gone(p
))
1357 ap
->post_handler
= aggr_post_handler
;
1359 INIT_LIST_HEAD(&ap
->list
);
1360 INIT_HLIST_NODE(&ap
->hlist
);
1362 list_add_rcu(&p
->list
, &ap
->list
);
1363 hlist_replace_rcu(&p
->hlist
, &ap
->hlist
);
1367 * This is the second or subsequent kprobe at the address - handle
1370 static int register_aggr_kprobe(struct kprobe
*orig_p
, struct kprobe
*p
)
1373 struct kprobe
*ap
= orig_p
;
1377 /* For preparing optimization, jump_label_text_reserved() is called */
1379 mutex_lock(&text_mutex
);
1381 if (!kprobe_aggrprobe(orig_p
)) {
1382 /* If orig_p is not an aggr_kprobe, create new aggr_kprobe. */
1383 ap
= alloc_aggr_kprobe(orig_p
);
1388 init_aggr_kprobe(ap
, orig_p
);
1389 } else if (kprobe_unused(ap
)) {
1390 /* This probe is going to die. Rescue it */
1391 ret
= reuse_unused_kprobe(ap
);
1396 if (kprobe_gone(ap
)) {
1398 * Attempting to insert new probe at the same location that
1399 * had a probe in the module vaddr area which already
1400 * freed. So, the instruction slot has already been
1401 * released. We need a new slot for the new probe.
1403 ret
= arch_prepare_kprobe(ap
);
1406 * Even if fail to allocate new slot, don't need to
1407 * free aggr_probe. It will be used next time, or
1408 * freed by unregister_kprobe.
1412 /* Prepare optimized instructions if possible. */
1413 prepare_optimized_kprobe(ap
);
1416 * Clear gone flag to prevent allocating new slot again, and
1417 * set disabled flag because it is not armed yet.
1419 ap
->flags
= (ap
->flags
& ~KPROBE_FLAG_GONE
)
1420 | KPROBE_FLAG_DISABLED
;
1423 /* Copy ap's insn slot to p */
1425 ret
= add_new_kprobe(ap
, p
);
1428 mutex_unlock(&text_mutex
);
1429 jump_label_unlock();
1432 if (ret
== 0 && kprobe_disabled(ap
) && !kprobe_disabled(p
)) {
1433 ap
->flags
&= ~KPROBE_FLAG_DISABLED
;
1434 if (!kprobes_all_disarmed
) {
1435 /* Arm the breakpoint again. */
1436 ret
= arm_kprobe(ap
);
1438 ap
->flags
|= KPROBE_FLAG_DISABLED
;
1439 list_del_rcu(&p
->list
);
1447 bool __weak
arch_within_kprobe_blacklist(unsigned long addr
)
1449 /* The __kprobes marked functions and entry code must not be probed */
1450 return addr
>= (unsigned long)__kprobes_text_start
&&
1451 addr
< (unsigned long)__kprobes_text_end
;
1454 static bool __within_kprobe_blacklist(unsigned long addr
)
1456 struct kprobe_blacklist_entry
*ent
;
1458 if (arch_within_kprobe_blacklist(addr
))
1461 * If there exists a kprobe_blacklist, verify and
1462 * fail any probe registration in the prohibited area
1464 list_for_each_entry(ent
, &kprobe_blacklist
, list
) {
1465 if (addr
>= ent
->start_addr
&& addr
< ent
->end_addr
)
1471 bool within_kprobe_blacklist(unsigned long addr
)
1473 char symname
[KSYM_NAME_LEN
], *p
;
1475 if (__within_kprobe_blacklist(addr
))
1478 /* Check if the address is on a suffixed-symbol */
1479 if (!lookup_symbol_name(addr
, symname
)) {
1480 p
= strchr(symname
, '.');
1484 addr
= (unsigned long)kprobe_lookup_name(symname
, 0);
1486 return __within_kprobe_blacklist(addr
);
1492 * If we have a symbol_name argument, look it up and add the offset field
1493 * to it. This way, we can specify a relative address to a symbol.
1494 * This returns encoded errors if it fails to look up symbol or invalid
1495 * combination of parameters.
1497 static kprobe_opcode_t
*_kprobe_addr(kprobe_opcode_t
*addr
,
1498 const char *symbol_name
, unsigned int offset
)
1500 if ((symbol_name
&& addr
) || (!symbol_name
&& !addr
))
1504 addr
= kprobe_lookup_name(symbol_name
, offset
);
1506 return ERR_PTR(-ENOENT
);
1509 addr
= (kprobe_opcode_t
*)(((char *)addr
) + offset
);
1514 return ERR_PTR(-EINVAL
);
1517 static kprobe_opcode_t
*kprobe_addr(struct kprobe
*p
)
1519 return _kprobe_addr(p
->addr
, p
->symbol_name
, p
->offset
);
1522 /* Check passed kprobe is valid and return kprobe in kprobe_table. */
1523 static struct kprobe
*__get_valid_kprobe(struct kprobe
*p
)
1525 struct kprobe
*ap
, *list_p
;
1527 ap
= get_kprobe(p
->addr
);
1532 list_for_each_entry_rcu(list_p
, &ap
->list
, list
)
1534 /* kprobe p is a valid probe */
1542 /* Return error if the kprobe is being re-registered */
1543 static inline int check_kprobe_rereg(struct kprobe
*p
)
1547 mutex_lock(&kprobe_mutex
);
1548 if (__get_valid_kprobe(p
))
1550 mutex_unlock(&kprobe_mutex
);
1555 int __weak
arch_check_ftrace_location(struct kprobe
*p
)
1557 unsigned long ftrace_addr
;
1559 ftrace_addr
= ftrace_location((unsigned long)p
->addr
);
1561 #ifdef CONFIG_KPROBES_ON_FTRACE
1562 /* Given address is not on the instruction boundary */
1563 if ((unsigned long)p
->addr
!= ftrace_addr
)
1565 p
->flags
|= KPROBE_FLAG_FTRACE
;
1566 #else /* !CONFIG_KPROBES_ON_FTRACE */
1573 static int check_kprobe_address_safe(struct kprobe
*p
,
1574 struct module
**probed_mod
)
1578 ret
= arch_check_ftrace_location(p
);
1584 /* Ensure it is not in reserved area nor out of text */
1585 if (!kernel_text_address((unsigned long) p
->addr
) ||
1586 within_kprobe_blacklist((unsigned long) p
->addr
) ||
1587 jump_label_text_reserved(p
->addr
, p
->addr
) ||
1588 find_bug((unsigned long)p
->addr
)) {
1593 /* Check if are we probing a module */
1594 *probed_mod
= __module_text_address((unsigned long) p
->addr
);
1597 * We must hold a refcount of the probed module while updating
1598 * its code to prohibit unexpected unloading.
1600 if (unlikely(!try_module_get(*probed_mod
))) {
1606 * If the module freed .init.text, we couldn't insert
1609 if (within_module_init((unsigned long)p
->addr
, *probed_mod
) &&
1610 (*probed_mod
)->state
!= MODULE_STATE_COMING
) {
1611 module_put(*probed_mod
);
1618 jump_label_unlock();
1623 int register_kprobe(struct kprobe
*p
)
1626 struct kprobe
*old_p
;
1627 struct module
*probed_mod
;
1628 kprobe_opcode_t
*addr
;
1630 /* Adjust probe address from symbol */
1631 addr
= kprobe_addr(p
);
1633 return PTR_ERR(addr
);
1636 ret
= check_kprobe_rereg(p
);
1640 /* User can pass only KPROBE_FLAG_DISABLED to register_kprobe */
1641 p
->flags
&= KPROBE_FLAG_DISABLED
;
1643 INIT_LIST_HEAD(&p
->list
);
1645 ret
= check_kprobe_address_safe(p
, &probed_mod
);
1649 mutex_lock(&kprobe_mutex
);
1651 old_p
= get_kprobe(p
->addr
);
1653 /* Since this may unoptimize old_p, locking text_mutex. */
1654 ret
= register_aggr_kprobe(old_p
, p
);
1659 /* Prevent text modification */
1660 mutex_lock(&text_mutex
);
1661 ret
= prepare_kprobe(p
);
1662 mutex_unlock(&text_mutex
);
1667 INIT_HLIST_NODE(&p
->hlist
);
1668 hlist_add_head_rcu(&p
->hlist
,
1669 &kprobe_table
[hash_ptr(p
->addr
, KPROBE_HASH_BITS
)]);
1671 if (!kprobes_all_disarmed
&& !kprobe_disabled(p
)) {
1672 ret
= arm_kprobe(p
);
1674 hlist_del_rcu(&p
->hlist
);
1680 /* Try to optimize kprobe */
1681 try_to_optimize_kprobe(p
);
1683 mutex_unlock(&kprobe_mutex
);
1686 module_put(probed_mod
);
1690 EXPORT_SYMBOL_GPL(register_kprobe
);
1692 /* Check if all probes on the aggrprobe are disabled */
1693 static int aggr_kprobe_disabled(struct kprobe
*ap
)
1697 list_for_each_entry_rcu(kp
, &ap
->list
, list
)
1698 if (!kprobe_disabled(kp
))
1700 * There is an active probe on the list.
1701 * We can't disable this ap.
1708 /* Disable one kprobe: Make sure called under kprobe_mutex is locked */
1709 static struct kprobe
*__disable_kprobe(struct kprobe
*p
)
1711 struct kprobe
*orig_p
;
1714 /* Get an original kprobe for return */
1715 orig_p
= __get_valid_kprobe(p
);
1716 if (unlikely(orig_p
== NULL
))
1717 return ERR_PTR(-EINVAL
);
1719 if (!kprobe_disabled(p
)) {
1720 /* Disable probe if it is a child probe */
1722 p
->flags
|= KPROBE_FLAG_DISABLED
;
1724 /* Try to disarm and disable this/parent probe */
1725 if (p
== orig_p
|| aggr_kprobe_disabled(orig_p
)) {
1727 * If kprobes_all_disarmed is set, orig_p
1728 * should have already been disarmed, so
1729 * skip unneed disarming process.
1731 if (!kprobes_all_disarmed
) {
1732 ret
= disarm_kprobe(orig_p
, true);
1734 p
->flags
&= ~KPROBE_FLAG_DISABLED
;
1735 return ERR_PTR(ret
);
1738 orig_p
->flags
|= KPROBE_FLAG_DISABLED
;
1746 * Unregister a kprobe without a scheduler synchronization.
1748 static int __unregister_kprobe_top(struct kprobe
*p
)
1750 struct kprobe
*ap
, *list_p
;
1752 /* Disable kprobe. This will disarm it if needed. */
1753 ap
= __disable_kprobe(p
);
1759 * This probe is an independent(and non-optimized) kprobe
1760 * (not an aggrprobe). Remove from the hash list.
1764 /* Following process expects this probe is an aggrprobe */
1765 WARN_ON(!kprobe_aggrprobe(ap
));
1767 if (list_is_singular(&ap
->list
) && kprobe_disarmed(ap
))
1769 * !disarmed could be happen if the probe is under delayed
1774 /* If disabling probe has special handlers, update aggrprobe */
1775 if (p
->post_handler
&& !kprobe_gone(p
)) {
1776 list_for_each_entry_rcu(list_p
, &ap
->list
, list
) {
1777 if ((list_p
!= p
) && (list_p
->post_handler
))
1780 ap
->post_handler
= NULL
;
1784 * Remove from the aggrprobe: this path will do nothing in
1785 * __unregister_kprobe_bottom().
1787 list_del_rcu(&p
->list
);
1788 if (!kprobe_disabled(ap
) && !kprobes_all_disarmed
)
1790 * Try to optimize this probe again, because post
1791 * handler may have been changed.
1793 optimize_kprobe(ap
);
1798 hlist_del_rcu(&ap
->hlist
);
1802 static void __unregister_kprobe_bottom(struct kprobe
*p
)
1806 if (list_empty(&p
->list
))
1807 /* This is an independent kprobe */
1808 arch_remove_kprobe(p
);
1809 else if (list_is_singular(&p
->list
)) {
1810 /* This is the last child of an aggrprobe */
1811 ap
= list_entry(p
->list
.next
, struct kprobe
, list
);
1813 free_aggr_kprobe(ap
);
1815 /* Otherwise, do nothing. */
1818 int register_kprobes(struct kprobe
**kps
, int num
)
1824 for (i
= 0; i
< num
; i
++) {
1825 ret
= register_kprobe(kps
[i
]);
1828 unregister_kprobes(kps
, i
);
1834 EXPORT_SYMBOL_GPL(register_kprobes
);
1836 void unregister_kprobe(struct kprobe
*p
)
1838 unregister_kprobes(&p
, 1);
1840 EXPORT_SYMBOL_GPL(unregister_kprobe
);
1842 void unregister_kprobes(struct kprobe
**kps
, int num
)
1848 mutex_lock(&kprobe_mutex
);
1849 for (i
= 0; i
< num
; i
++)
1850 if (__unregister_kprobe_top(kps
[i
]) < 0)
1851 kps
[i
]->addr
= NULL
;
1852 mutex_unlock(&kprobe_mutex
);
1855 for (i
= 0; i
< num
; i
++)
1857 __unregister_kprobe_bottom(kps
[i
]);
1859 EXPORT_SYMBOL_GPL(unregister_kprobes
);
1861 int __weak
kprobe_exceptions_notify(struct notifier_block
*self
,
1862 unsigned long val
, void *data
)
1866 NOKPROBE_SYMBOL(kprobe_exceptions_notify
);
1868 static struct notifier_block kprobe_exceptions_nb
= {
1869 .notifier_call
= kprobe_exceptions_notify
,
1870 .priority
= 0x7fffffff /* we need to be notified first */
1873 unsigned long __weak
arch_deref_entry_point(void *entry
)
1875 return (unsigned long)entry
;
1878 #ifdef CONFIG_KRETPROBES
1880 * This kprobe pre_handler is registered with every kretprobe. When probe
1881 * hits it will set up the return probe.
1883 static int pre_handler_kretprobe(struct kprobe
*p
, struct pt_regs
*regs
)
1885 struct kretprobe
*rp
= container_of(p
, struct kretprobe
, kp
);
1886 unsigned long hash
, flags
= 0;
1887 struct kretprobe_instance
*ri
;
1890 * To avoid deadlocks, prohibit return probing in NMI contexts,
1891 * just skip the probe and increase the (inexact) 'nmissed'
1892 * statistical counter, so that the user is informed that
1893 * something happened:
1895 if (unlikely(in_nmi())) {
1900 /* TODO: consider to only swap the RA after the last pre_handler fired */
1901 hash
= hash_ptr(current
, KPROBE_HASH_BITS
);
1902 raw_spin_lock_irqsave(&rp
->lock
, flags
);
1903 if (!hlist_empty(&rp
->free_instances
)) {
1904 ri
= hlist_entry(rp
->free_instances
.first
,
1905 struct kretprobe_instance
, hlist
);
1906 hlist_del(&ri
->hlist
);
1907 raw_spin_unlock_irqrestore(&rp
->lock
, flags
);
1912 if (rp
->entry_handler
&& rp
->entry_handler(ri
, regs
)) {
1913 raw_spin_lock_irqsave(&rp
->lock
, flags
);
1914 hlist_add_head(&ri
->hlist
, &rp
->free_instances
);
1915 raw_spin_unlock_irqrestore(&rp
->lock
, flags
);
1919 arch_prepare_kretprobe(ri
, regs
);
1921 /* XXX(hch): why is there no hlist_move_head? */
1922 INIT_HLIST_NODE(&ri
->hlist
);
1923 kretprobe_table_lock(hash
, &flags
);
1924 hlist_add_head(&ri
->hlist
, &kretprobe_inst_table
[hash
]);
1925 kretprobe_table_unlock(hash
, &flags
);
1928 raw_spin_unlock_irqrestore(&rp
->lock
, flags
);
1932 NOKPROBE_SYMBOL(pre_handler_kretprobe
);
1934 bool __weak
arch_kprobe_on_func_entry(unsigned long offset
)
1939 bool kprobe_on_func_entry(kprobe_opcode_t
*addr
, const char *sym
, unsigned long offset
)
1941 kprobe_opcode_t
*kp_addr
= _kprobe_addr(addr
, sym
, offset
);
1943 if (IS_ERR(kp_addr
))
1946 if (!kallsyms_lookup_size_offset((unsigned long)kp_addr
, NULL
, &offset
) ||
1947 !arch_kprobe_on_func_entry(offset
))
1953 int register_kretprobe(struct kretprobe
*rp
)
1956 struct kretprobe_instance
*inst
;
1960 if (!kprobe_on_func_entry(rp
->kp
.addr
, rp
->kp
.symbol_name
, rp
->kp
.offset
))
1963 if (kretprobe_blacklist_size
) {
1964 addr
= kprobe_addr(&rp
->kp
);
1966 return PTR_ERR(addr
);
1968 for (i
= 0; kretprobe_blacklist
[i
].name
!= NULL
; i
++) {
1969 if (kretprobe_blacklist
[i
].addr
== addr
)
1974 rp
->kp
.pre_handler
= pre_handler_kretprobe
;
1975 rp
->kp
.post_handler
= NULL
;
1976 rp
->kp
.fault_handler
= NULL
;
1978 /* Pre-allocate memory for max kretprobe instances */
1979 if (rp
->maxactive
<= 0) {
1980 #ifdef CONFIG_PREEMPTION
1981 rp
->maxactive
= max_t(unsigned int, 10, 2*num_possible_cpus());
1983 rp
->maxactive
= num_possible_cpus();
1986 raw_spin_lock_init(&rp
->lock
);
1987 INIT_HLIST_HEAD(&rp
->free_instances
);
1988 for (i
= 0; i
< rp
->maxactive
; i
++) {
1989 inst
= kmalloc(sizeof(struct kretprobe_instance
) +
1990 rp
->data_size
, GFP_KERNEL
);
1995 INIT_HLIST_NODE(&inst
->hlist
);
1996 hlist_add_head(&inst
->hlist
, &rp
->free_instances
);
2000 /* Establish function entry probe point */
2001 ret
= register_kprobe(&rp
->kp
);
2006 EXPORT_SYMBOL_GPL(register_kretprobe
);
2008 int register_kretprobes(struct kretprobe
**rps
, int num
)
2014 for (i
= 0; i
< num
; i
++) {
2015 ret
= register_kretprobe(rps
[i
]);
2018 unregister_kretprobes(rps
, i
);
2024 EXPORT_SYMBOL_GPL(register_kretprobes
);
2026 void unregister_kretprobe(struct kretprobe
*rp
)
2028 unregister_kretprobes(&rp
, 1);
2030 EXPORT_SYMBOL_GPL(unregister_kretprobe
);
2032 void unregister_kretprobes(struct kretprobe
**rps
, int num
)
2038 mutex_lock(&kprobe_mutex
);
2039 for (i
= 0; i
< num
; i
++)
2040 if (__unregister_kprobe_top(&rps
[i
]->kp
) < 0)
2041 rps
[i
]->kp
.addr
= NULL
;
2042 mutex_unlock(&kprobe_mutex
);
2045 for (i
= 0; i
< num
; i
++) {
2046 if (rps
[i
]->kp
.addr
) {
2047 __unregister_kprobe_bottom(&rps
[i
]->kp
);
2048 cleanup_rp_inst(rps
[i
]);
2052 EXPORT_SYMBOL_GPL(unregister_kretprobes
);
2054 #else /* CONFIG_KRETPROBES */
2055 int register_kretprobe(struct kretprobe
*rp
)
2059 EXPORT_SYMBOL_GPL(register_kretprobe
);
2061 int register_kretprobes(struct kretprobe
**rps
, int num
)
2065 EXPORT_SYMBOL_GPL(register_kretprobes
);
2067 void unregister_kretprobe(struct kretprobe
*rp
)
2070 EXPORT_SYMBOL_GPL(unregister_kretprobe
);
2072 void unregister_kretprobes(struct kretprobe
**rps
, int num
)
2075 EXPORT_SYMBOL_GPL(unregister_kretprobes
);
2077 static int pre_handler_kretprobe(struct kprobe
*p
, struct pt_regs
*regs
)
2081 NOKPROBE_SYMBOL(pre_handler_kretprobe
);
2083 #endif /* CONFIG_KRETPROBES */
2085 /* Set the kprobe gone and remove its instruction buffer. */
2086 static void kill_kprobe(struct kprobe
*p
)
2090 p
->flags
|= KPROBE_FLAG_GONE
;
2091 if (kprobe_aggrprobe(p
)) {
2093 * If this is an aggr_kprobe, we have to list all the
2094 * chained probes and mark them GONE.
2096 list_for_each_entry_rcu(kp
, &p
->list
, list
)
2097 kp
->flags
|= KPROBE_FLAG_GONE
;
2098 p
->post_handler
= NULL
;
2099 kill_optimized_kprobe(p
);
2102 * Here, we can remove insn_slot safely, because no thread calls
2103 * the original probed function (which will be freed soon) any more.
2105 arch_remove_kprobe(p
);
2108 /* Disable one kprobe */
2109 int disable_kprobe(struct kprobe
*kp
)
2114 mutex_lock(&kprobe_mutex
);
2116 /* Disable this kprobe */
2117 p
= __disable_kprobe(kp
);
2121 mutex_unlock(&kprobe_mutex
);
2124 EXPORT_SYMBOL_GPL(disable_kprobe
);
2126 /* Enable one kprobe */
2127 int enable_kprobe(struct kprobe
*kp
)
2132 mutex_lock(&kprobe_mutex
);
2134 /* Check whether specified probe is valid. */
2135 p
= __get_valid_kprobe(kp
);
2136 if (unlikely(p
== NULL
)) {
2141 if (kprobe_gone(kp
)) {
2142 /* This kprobe has gone, we couldn't enable it. */
2148 kp
->flags
&= ~KPROBE_FLAG_DISABLED
;
2150 if (!kprobes_all_disarmed
&& kprobe_disabled(p
)) {
2151 p
->flags
&= ~KPROBE_FLAG_DISABLED
;
2152 ret
= arm_kprobe(p
);
2154 p
->flags
|= KPROBE_FLAG_DISABLED
;
2157 mutex_unlock(&kprobe_mutex
);
2160 EXPORT_SYMBOL_GPL(enable_kprobe
);
2162 /* Caller must NOT call this in usual path. This is only for critical case */
2163 void dump_kprobe(struct kprobe
*kp
)
2165 pr_err("Dumping kprobe:\n");
2166 pr_err("Name: %s\nOffset: %x\nAddress: %pS\n",
2167 kp
->symbol_name
, kp
->offset
, kp
->addr
);
2169 NOKPROBE_SYMBOL(dump_kprobe
);
2171 int kprobe_add_ksym_blacklist(unsigned long entry
)
2173 struct kprobe_blacklist_entry
*ent
;
2174 unsigned long offset
= 0, size
= 0;
2176 if (!kernel_text_address(entry
) ||
2177 !kallsyms_lookup_size_offset(entry
, &size
, &offset
))
2180 ent
= kmalloc(sizeof(*ent
), GFP_KERNEL
);
2183 ent
->start_addr
= entry
;
2184 ent
->end_addr
= entry
+ size
;
2185 INIT_LIST_HEAD(&ent
->list
);
2186 list_add_tail(&ent
->list
, &kprobe_blacklist
);
2191 /* Add all symbols in given area into kprobe blacklist */
2192 int kprobe_add_area_blacklist(unsigned long start
, unsigned long end
)
2194 unsigned long entry
;
2197 for (entry
= start
; entry
< end
; entry
+= ret
) {
2198 ret
= kprobe_add_ksym_blacklist(entry
);
2201 if (ret
== 0) /* In case of alias symbol */
2207 int __init __weak
arch_populate_kprobe_blacklist(void)
2213 * Lookup and populate the kprobe_blacklist.
2215 * Unlike the kretprobe blacklist, we'll need to determine
2216 * the range of addresses that belong to the said functions,
2217 * since a kprobe need not necessarily be at the beginning
2220 static int __init
populate_kprobe_blacklist(unsigned long *start
,
2223 unsigned long entry
;
2224 unsigned long *iter
;
2227 for (iter
= start
; iter
< end
; iter
++) {
2228 entry
= arch_deref_entry_point((void *)*iter
);
2229 ret
= kprobe_add_ksym_blacklist(entry
);
2236 /* Symbols in __kprobes_text are blacklisted */
2237 ret
= kprobe_add_area_blacklist((unsigned long)__kprobes_text_start
,
2238 (unsigned long)__kprobes_text_end
);
2240 return ret
? : arch_populate_kprobe_blacklist();
2243 /* Module notifier call back, checking kprobes on the module */
2244 static int kprobes_module_callback(struct notifier_block
*nb
,
2245 unsigned long val
, void *data
)
2247 struct module
*mod
= data
;
2248 struct hlist_head
*head
;
2251 int checkcore
= (val
== MODULE_STATE_GOING
);
2253 if (val
!= MODULE_STATE_GOING
&& val
!= MODULE_STATE_LIVE
)
2257 * When MODULE_STATE_GOING was notified, both of module .text and
2258 * .init.text sections would be freed. When MODULE_STATE_LIVE was
2259 * notified, only .init.text section would be freed. We need to
2260 * disable kprobes which have been inserted in the sections.
2262 mutex_lock(&kprobe_mutex
);
2263 for (i
= 0; i
< KPROBE_TABLE_SIZE
; i
++) {
2264 head
= &kprobe_table
[i
];
2265 hlist_for_each_entry_rcu(p
, head
, hlist
)
2266 if (within_module_init((unsigned long)p
->addr
, mod
) ||
2268 within_module_core((unsigned long)p
->addr
, mod
))) {
2270 * The vaddr this probe is installed will soon
2271 * be vfreed buy not synced to disk. Hence,
2272 * disarming the breakpoint isn't needed.
2274 * Note, this will also move any optimized probes
2275 * that are pending to be removed from their
2276 * corresponding lists to the freeing_list and
2277 * will not be touched by the delayed
2278 * kprobe_optimizer work handler.
2283 mutex_unlock(&kprobe_mutex
);
2287 static struct notifier_block kprobe_module_nb
= {
2288 .notifier_call
= kprobes_module_callback
,
2292 /* Markers of _kprobe_blacklist section */
2293 extern unsigned long __start_kprobe_blacklist
[];
2294 extern unsigned long __stop_kprobe_blacklist
[];
2296 static int __init
init_kprobes(void)
2300 /* FIXME allocate the probe table, currently defined statically */
2301 /* initialize all list heads */
2302 for (i
= 0; i
< KPROBE_TABLE_SIZE
; i
++) {
2303 INIT_HLIST_HEAD(&kprobe_table
[i
]);
2304 INIT_HLIST_HEAD(&kretprobe_inst_table
[i
]);
2305 raw_spin_lock_init(&(kretprobe_table_locks
[i
].lock
));
2308 err
= populate_kprobe_blacklist(__start_kprobe_blacklist
,
2309 __stop_kprobe_blacklist
);
2311 pr_err("kprobes: failed to populate blacklist: %d\n", err
);
2312 pr_err("Please take care of using kprobes.\n");
2315 if (kretprobe_blacklist_size
) {
2316 /* lookup the function address from its name */
2317 for (i
= 0; kretprobe_blacklist
[i
].name
!= NULL
; i
++) {
2318 kretprobe_blacklist
[i
].addr
=
2319 kprobe_lookup_name(kretprobe_blacklist
[i
].name
, 0);
2320 if (!kretprobe_blacklist
[i
].addr
)
2321 printk("kretprobe: lookup failed: %s\n",
2322 kretprobe_blacklist
[i
].name
);
2326 #if defined(CONFIG_OPTPROBES)
2327 #if defined(__ARCH_WANT_KPROBES_INSN_SLOT)
2328 /* Init kprobe_optinsn_slots */
2329 kprobe_optinsn_slots
.insn_size
= MAX_OPTINSN_SIZE
;
2331 /* By default, kprobes can be optimized */
2332 kprobes_allow_optimization
= true;
2335 /* By default, kprobes are armed */
2336 kprobes_all_disarmed
= false;
2338 err
= arch_init_kprobes();
2340 err
= register_die_notifier(&kprobe_exceptions_nb
);
2342 err
= register_module_notifier(&kprobe_module_nb
);
2344 kprobes_initialized
= (err
== 0);
2350 subsys_initcall(init_kprobes
);
2352 #ifdef CONFIG_DEBUG_FS
2353 static void report_probe(struct seq_file
*pi
, struct kprobe
*p
,
2354 const char *sym
, int offset
, char *modname
, struct kprobe
*pp
)
2357 void *addr
= p
->addr
;
2359 if (p
->pre_handler
== pre_handler_kretprobe
)
2364 if (!kallsyms_show_value())
2368 seq_printf(pi
, "%px %s %s+0x%x %s ",
2369 addr
, kprobe_type
, sym
, offset
,
2370 (modname
? modname
: " "));
2371 else /* try to use %pS */
2372 seq_printf(pi
, "%px %s %pS ",
2373 addr
, kprobe_type
, p
->addr
);
2377 seq_printf(pi
, "%s%s%s%s\n",
2378 (kprobe_gone(p
) ? "[GONE]" : ""),
2379 ((kprobe_disabled(p
) && !kprobe_gone(p
)) ? "[DISABLED]" : ""),
2380 (kprobe_optimized(pp
) ? "[OPTIMIZED]" : ""),
2381 (kprobe_ftrace(pp
) ? "[FTRACE]" : ""));
2384 static void *kprobe_seq_start(struct seq_file
*f
, loff_t
*pos
)
2386 return (*pos
< KPROBE_TABLE_SIZE
) ? pos
: NULL
;
2389 static void *kprobe_seq_next(struct seq_file
*f
, void *v
, loff_t
*pos
)
2392 if (*pos
>= KPROBE_TABLE_SIZE
)
2397 static void kprobe_seq_stop(struct seq_file
*f
, void *v
)
2402 static int show_kprobe_addr(struct seq_file
*pi
, void *v
)
2404 struct hlist_head
*head
;
2405 struct kprobe
*p
, *kp
;
2406 const char *sym
= NULL
;
2407 unsigned int i
= *(loff_t
*) v
;
2408 unsigned long offset
= 0;
2409 char *modname
, namebuf
[KSYM_NAME_LEN
];
2411 head
= &kprobe_table
[i
];
2413 hlist_for_each_entry_rcu(p
, head
, hlist
) {
2414 sym
= kallsyms_lookup((unsigned long)p
->addr
, NULL
,
2415 &offset
, &modname
, namebuf
);
2416 if (kprobe_aggrprobe(p
)) {
2417 list_for_each_entry_rcu(kp
, &p
->list
, list
)
2418 report_probe(pi
, kp
, sym
, offset
, modname
, p
);
2420 report_probe(pi
, p
, sym
, offset
, modname
, NULL
);
2426 static const struct seq_operations kprobes_seq_ops
= {
2427 .start
= kprobe_seq_start
,
2428 .next
= kprobe_seq_next
,
2429 .stop
= kprobe_seq_stop
,
2430 .show
= show_kprobe_addr
2433 static int kprobes_open(struct inode
*inode
, struct file
*filp
)
2435 return seq_open(filp
, &kprobes_seq_ops
);
2438 static const struct file_operations debugfs_kprobes_operations
= {
2439 .open
= kprobes_open
,
2441 .llseek
= seq_lseek
,
2442 .release
= seq_release
,
2445 /* kprobes/blacklist -- shows which functions can not be probed */
2446 static void *kprobe_blacklist_seq_start(struct seq_file
*m
, loff_t
*pos
)
2448 return seq_list_start(&kprobe_blacklist
, *pos
);
2451 static void *kprobe_blacklist_seq_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2453 return seq_list_next(v
, &kprobe_blacklist
, pos
);
2456 static int kprobe_blacklist_seq_show(struct seq_file
*m
, void *v
)
2458 struct kprobe_blacklist_entry
*ent
=
2459 list_entry(v
, struct kprobe_blacklist_entry
, list
);
2462 * If /proc/kallsyms is not showing kernel address, we won't
2463 * show them here either.
2465 if (!kallsyms_show_value())
2466 seq_printf(m
, "0x%px-0x%px\t%ps\n", NULL
, NULL
,
2467 (void *)ent
->start_addr
);
2469 seq_printf(m
, "0x%px-0x%px\t%ps\n", (void *)ent
->start_addr
,
2470 (void *)ent
->end_addr
, (void *)ent
->start_addr
);
2474 static const struct seq_operations kprobe_blacklist_seq_ops
= {
2475 .start
= kprobe_blacklist_seq_start
,
2476 .next
= kprobe_blacklist_seq_next
,
2477 .stop
= kprobe_seq_stop
, /* Reuse void function */
2478 .show
= kprobe_blacklist_seq_show
,
2481 static int kprobe_blacklist_open(struct inode
*inode
, struct file
*filp
)
2483 return seq_open(filp
, &kprobe_blacklist_seq_ops
);
2486 static const struct file_operations debugfs_kprobe_blacklist_ops
= {
2487 .open
= kprobe_blacklist_open
,
2489 .llseek
= seq_lseek
,
2490 .release
= seq_release
,
2493 static int arm_all_kprobes(void)
2495 struct hlist_head
*head
;
2497 unsigned int i
, total
= 0, errors
= 0;
2500 mutex_lock(&kprobe_mutex
);
2502 /* If kprobes are armed, just return */
2503 if (!kprobes_all_disarmed
)
2504 goto already_enabled
;
2507 * optimize_kprobe() called by arm_kprobe() checks
2508 * kprobes_all_disarmed, so set kprobes_all_disarmed before
2511 kprobes_all_disarmed
= false;
2512 /* Arming kprobes doesn't optimize kprobe itself */
2513 for (i
= 0; i
< KPROBE_TABLE_SIZE
; i
++) {
2514 head
= &kprobe_table
[i
];
2515 /* Arm all kprobes on a best-effort basis */
2516 hlist_for_each_entry_rcu(p
, head
, hlist
) {
2517 if (!kprobe_disabled(p
)) {
2518 err
= arm_kprobe(p
);
2529 pr_warn("Kprobes globally enabled, but failed to arm %d out of %d probes\n",
2532 pr_info("Kprobes globally enabled\n");
2535 mutex_unlock(&kprobe_mutex
);
2539 static int disarm_all_kprobes(void)
2541 struct hlist_head
*head
;
2543 unsigned int i
, total
= 0, errors
= 0;
2546 mutex_lock(&kprobe_mutex
);
2548 /* If kprobes are already disarmed, just return */
2549 if (kprobes_all_disarmed
) {
2550 mutex_unlock(&kprobe_mutex
);
2554 kprobes_all_disarmed
= true;
2556 for (i
= 0; i
< KPROBE_TABLE_SIZE
; i
++) {
2557 head
= &kprobe_table
[i
];
2558 /* Disarm all kprobes on a best-effort basis */
2559 hlist_for_each_entry_rcu(p
, head
, hlist
) {
2560 if (!arch_trampoline_kprobe(p
) && !kprobe_disabled(p
)) {
2561 err
= disarm_kprobe(p
, false);
2572 pr_warn("Kprobes globally disabled, but failed to disarm %d out of %d probes\n",
2575 pr_info("Kprobes globally disabled\n");
2577 mutex_unlock(&kprobe_mutex
);
2579 /* Wait for disarming all kprobes by optimizer */
2580 wait_for_kprobe_optimizer();
2586 * XXX: The debugfs bool file interface doesn't allow for callbacks
2587 * when the bool state is switched. We can reuse that facility when
2590 static ssize_t
read_enabled_file_bool(struct file
*file
,
2591 char __user
*user_buf
, size_t count
, loff_t
*ppos
)
2595 if (!kprobes_all_disarmed
)
2601 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, 2);
2604 static ssize_t
write_enabled_file_bool(struct file
*file
,
2605 const char __user
*user_buf
, size_t count
, loff_t
*ppos
)
2611 buf_size
= min(count
, (sizeof(buf
)-1));
2612 if (copy_from_user(buf
, user_buf
, buf_size
))
2615 buf
[buf_size
] = '\0';
2620 ret
= arm_all_kprobes();
2625 ret
= disarm_all_kprobes();
2637 static const struct file_operations fops_kp
= {
2638 .read
= read_enabled_file_bool
,
2639 .write
= write_enabled_file_bool
,
2640 .llseek
= default_llseek
,
2643 static int __init
debugfs_kprobe_init(void)
2646 unsigned int value
= 1;
2648 dir
= debugfs_create_dir("kprobes", NULL
);
2650 debugfs_create_file("list", 0400, dir
, NULL
,
2651 &debugfs_kprobes_operations
);
2653 debugfs_create_file("enabled", 0600, dir
, &value
, &fops_kp
);
2655 debugfs_create_file("blacklist", 0400, dir
, NULL
,
2656 &debugfs_kprobe_blacklist_ops
);
2661 late_initcall(debugfs_kprobe_init
);
2662 #endif /* CONFIG_DEBUG_FS */