Skip to content

Commit 7582b7b

Browse files
rpptmcgrof
authored andcommitted
kprobes: remove dependency on CONFIG_MODULES
kprobes depended on CONFIG_MODULES because it has to allocate memory for code. Since code allocations are now implemented with execmem, kprobes can be enabled in non-modular kernels. Add #ifdef CONFIG_MODULE guards for the code dealing with kprobes inside modules, make CONFIG_KPROBES select CONFIG_EXECMEM and drop the dependency of CONFIG_KPROBES on CONFIG_MODULES. Signed-off-by: Mike Rapoport (IBM) <[email protected]> Acked-by: Masami Hiramatsu (Google) <[email protected]> [mcgrof: rebase in light of NEED_TASKS_RCU ] Signed-off-by: Luis Chamberlain <[email protected]>
1 parent 0a956d5 commit 7582b7b

File tree

4 files changed

+63
-23
lines changed

4 files changed

+63
-23
lines changed

arch/Kconfig

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ config GENERIC_ENTRY
6060

6161
config KPROBES
6262
bool "Kprobes"
63-
depends on MODULES
6463
depends on HAVE_KPROBES
6564
select KALLSYMS
65+
select EXECMEM
6666
select NEED_TASKS_RCU
6767
help
6868
Kprobes allows you to trap at almost any kernel address and

include/linux/module.h

+9
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,11 @@ static inline bool module_is_live(struct module *mod)
605605
return mod->state != MODULE_STATE_GOING;
606606
}
607607

608+
static inline bool module_is_coming(struct module *mod)
609+
{
610+
return mod->state == MODULE_STATE_COMING;
611+
}
612+
608613
struct module *__module_text_address(unsigned long addr);
609614
struct module *__module_address(unsigned long addr);
610615
bool is_module_address(unsigned long addr);
@@ -857,6 +862,10 @@ void *dereference_module_function_descriptor(struct module *mod, void *ptr)
857862
return ptr;
858863
}
859864

865+
static inline bool module_is_coming(struct module *mod)
866+
{
867+
return false;
868+
}
860869
#endif /* CONFIG_MODULES */
861870

862871
#ifdef CONFIG_SYSFS

kernel/kprobes.c

+34-21
Original file line numberDiff line numberDiff line change
@@ -1588,7 +1588,7 @@ static int check_kprobe_address_safe(struct kprobe *p,
15881588
}
15891589

15901590
/* Get module refcount and reject __init functions for loaded modules. */
1591-
if (*probed_mod) {
1591+
if (IS_ENABLED(CONFIG_MODULES) && *probed_mod) {
15921592
/*
15931593
* We must hold a refcount of the probed module while updating
15941594
* its code to prohibit unexpected unloading.
@@ -1603,12 +1603,13 @@ static int check_kprobe_address_safe(struct kprobe *p,
16031603
* kprobes in there.
16041604
*/
16051605
if (within_module_init((unsigned long)p->addr, *probed_mod) &&
1606-
(*probed_mod)->state != MODULE_STATE_COMING) {
1606+
!module_is_coming(*probed_mod)) {
16071607
module_put(*probed_mod);
16081608
*probed_mod = NULL;
16091609
ret = -ENOENT;
16101610
}
16111611
}
1612+
16121613
out:
16131614
preempt_enable();
16141615
jump_label_unlock();
@@ -2488,24 +2489,6 @@ int kprobe_add_area_blacklist(unsigned long start, unsigned long end)
24882489
return 0;
24892490
}
24902491

2491-
/* Remove all symbols in given area from kprobe blacklist */
2492-
static void kprobe_remove_area_blacklist(unsigned long start, unsigned long end)
2493-
{
2494-
struct kprobe_blacklist_entry *ent, *n;
2495-
2496-
list_for_each_entry_safe(ent, n, &kprobe_blacklist, list) {
2497-
if (ent->start_addr < start || ent->start_addr >= end)
2498-
continue;
2499-
list_del(&ent->list);
2500-
kfree(ent);
2501-
}
2502-
}
2503-
2504-
static void kprobe_remove_ksym_blacklist(unsigned long entry)
2505-
{
2506-
kprobe_remove_area_blacklist(entry, entry + 1);
2507-
}
2508-
25092492
int __weak arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *value,
25102493
char *type, char *sym)
25112494
{
@@ -2570,6 +2553,25 @@ static int __init populate_kprobe_blacklist(unsigned long *start,
25702553
return ret ? : arch_populate_kprobe_blacklist();
25712554
}
25722555

2556+
#ifdef CONFIG_MODULES
2557+
/* Remove all symbols in given area from kprobe blacklist */
2558+
static void kprobe_remove_area_blacklist(unsigned long start, unsigned long end)
2559+
{
2560+
struct kprobe_blacklist_entry *ent, *n;
2561+
2562+
list_for_each_entry_safe(ent, n, &kprobe_blacklist, list) {
2563+
if (ent->start_addr < start || ent->start_addr >= end)
2564+
continue;
2565+
list_del(&ent->list);
2566+
kfree(ent);
2567+
}
2568+
}
2569+
2570+
static void kprobe_remove_ksym_blacklist(unsigned long entry)
2571+
{
2572+
kprobe_remove_area_blacklist(entry, entry + 1);
2573+
}
2574+
25732575
static void add_module_kprobe_blacklist(struct module *mod)
25742576
{
25752577
unsigned long start, end;
@@ -2672,6 +2674,17 @@ static struct notifier_block kprobe_module_nb = {
26722674
.priority = 0
26732675
};
26742676

2677+
static int kprobe_register_module_notifier(void)
2678+
{
2679+
return register_module_notifier(&kprobe_module_nb);
2680+
}
2681+
#else
2682+
static int kprobe_register_module_notifier(void)
2683+
{
2684+
return 0;
2685+
}
2686+
#endif /* CONFIG_MODULES */
2687+
26752688
void kprobe_free_init_mem(void)
26762689
{
26772690
void *start = (void *)(&__init_begin);
@@ -2731,7 +2744,7 @@ static int __init init_kprobes(void)
27312744
if (!err)
27322745
err = register_die_notifier(&kprobe_exceptions_nb);
27332746
if (!err)
2734-
err = register_module_notifier(&kprobe_module_nb);
2747+
err = kprobe_register_module_notifier();
27352748

27362749
kprobes_initialized = (err == 0);
27372750
kprobe_sysctls_init();

kernel/trace/trace_kprobe.c

+19-1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ static nokprobe_inline bool trace_kprobe_within_module(struct trace_kprobe *tk,
111111
return strncmp(module_name(mod), name, len) == 0 && name[len] == ':';
112112
}
113113

114+
#ifdef CONFIG_MODULES
114115
static nokprobe_inline bool trace_kprobe_module_exist(struct trace_kprobe *tk)
115116
{
116117
char *p;
@@ -129,6 +130,12 @@ static nokprobe_inline bool trace_kprobe_module_exist(struct trace_kprobe *tk)
129130

130131
return ret;
131132
}
133+
#else
134+
static inline bool trace_kprobe_module_exist(struct trace_kprobe *tk)
135+
{
136+
return false;
137+
}
138+
#endif
132139

133140
static bool trace_kprobe_is_busy(struct dyn_event *ev)
134141
{
@@ -670,6 +677,7 @@ static int register_trace_kprobe(struct trace_kprobe *tk)
670677
return ret;
671678
}
672679

680+
#ifdef CONFIG_MODULES
673681
/* Module notifier call back, checking event on the module */
674682
static int trace_kprobe_module_callback(struct notifier_block *nb,
675683
unsigned long val, void *data)
@@ -704,6 +712,16 @@ static struct notifier_block trace_kprobe_module_nb = {
704712
.notifier_call = trace_kprobe_module_callback,
705713
.priority = 1 /* Invoked after kprobe module callback */
706714
};
715+
static int trace_kprobe_register_module_notifier(void)
716+
{
717+
return register_module_notifier(&trace_kprobe_module_nb);
718+
}
719+
#else
720+
static int trace_kprobe_register_module_notifier(void)
721+
{
722+
return 0;
723+
}
724+
#endif /* CONFIG_MODULES */
707725

708726
static int count_symbols(void *data, unsigned long unused)
709727
{
@@ -1933,7 +1951,7 @@ static __init int init_kprobe_trace_early(void)
19331951
if (ret)
19341952
return ret;
19351953

1936-
if (register_module_notifier(&trace_kprobe_module_nb))
1954+
if (trace_kprobe_register_module_notifier())
19371955
return -EINVAL;
19381956

19391957
return 0;

0 commit comments

Comments
 (0)