diff mbox series

[RFC,3/4] kprobes: Allow kprobes with CONFIG_MODULES=n

Message ID 2af01251ca21d79fa29092505d192f1f1b746cff.1709676663.git.jcalvinowens@gmail.com (mailing list archive)
State RFC
Headers show
Series Make bpf_jit and kprobes work with CONFIG_MODULES=n | expand

Checks

Context Check Description
netdev/tree_selection success Guessing tree name failed - patch did not apply
bpf/vmtest-bpf-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-VM_Test-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-VM_Test-5 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-VM_Test-4 fail Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-VM_Test-6 success Logs for aarch64-gcc / test
bpf/vmtest-bpf-VM_Test-8 fail Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-VM_Test-9 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-VM_Test-7 success Logs for aarch64-gcc / veristat
bpf/vmtest-bpf-VM_Test-24 success Logs for x86_64-llvm-18 / veristat
bpf/vmtest-bpf-VM_Test-10 success Logs for s390x-gcc / test
bpf/vmtest-bpf-VM_Test-11 success Logs for s390x-gcc / veristat
bpf/vmtest-bpf-VM_Test-12 success Logs for set-matrix
bpf/vmtest-bpf-VM_Test-13 fail Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-VM_Test-17 fail Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-VM_Test-19 success Logs for x86_64-llvm-17 / test
bpf/vmtest-bpf-VM_Test-14 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-VM_Test-15 success Logs for x86_64-gcc / test
bpf/vmtest-bpf-VM_Test-23 success Logs for x86_64-llvm-18 / test
bpf/vmtest-bpf-VM_Test-16 success Logs for x86_64-gcc / veristat
bpf/vmtest-bpf-VM_Test-21 fail Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-VM_Test-20 success Logs for x86_64-llvm-17 / veristat
bpf/vmtest-bpf-VM_Test-22 fail Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18 and -O2 optimization
bpf/vmtest-bpf-PR fail PR summary
bpf/vmtest-bpf-VM_Test-18 fail Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17 and -O2 optimization

Commit Message

Calvin Owens March 6, 2024, 8:05 p.m. UTC
If something like this is merged down the road, it can go in at leisure
once the module_alloc change is in: it's a one-way dependency.

Signed-off-by: Calvin Owens <jcalvinowens@gmail.com>
---
 arch/Kconfig                |  2 +-
 kernel/kprobes.c            | 22 ++++++++++++++++++++++
 kernel/trace/trace_kprobe.c | 11 +++++++++++
 3 files changed, 34 insertions(+), 1 deletion(-)

Comments

Mike Rapoport March 7, 2024, 7:22 a.m. UTC | #1
On Wed, Mar 06, 2024 at 12:05:10PM -0800, Calvin Owens wrote:
> If something like this is merged down the road, it can go in at leisure
> once the module_alloc change is in: it's a one-way dependency.
> 
> Signed-off-by: Calvin Owens <jcalvinowens@gmail.com>
> ---
>  arch/Kconfig                |  2 +-
>  kernel/kprobes.c            | 22 ++++++++++++++++++++++
>  kernel/trace/trace_kprobe.c | 11 +++++++++++
>  3 files changed, 34 insertions(+), 1 deletion(-)

When I did this in my last execmem posting, I think I've got slightly less
ugly ifdery, you may want to take a look at that:

https://lore.kernel.org/all/20230918072955.2507221-13-rppt@kernel.org
 
> diff --git a/arch/Kconfig b/arch/Kconfig
> index cfc24ced16dd..e60ce984d095 100644
> --- a/arch/Kconfig
> +++ b/arch/Kconfig
> @@ -52,8 +52,8 @@ config GENERIC_ENTRY
>  
>  config KPROBES
>  	bool "Kprobes"
> -	depends on MODULES
>  	depends on HAVE_KPROBES
> +	select MODULE_ALLOC
>  	select KALLSYMS
>  	select TASKS_RCU if PREEMPTION
>  	help
> diff --git a/kernel/kprobes.c b/kernel/kprobes.c
> index 9d9095e81792..194270e17d57 100644
> --- a/kernel/kprobes.c
> +++ b/kernel/kprobes.c
> @@ -1556,8 +1556,12 @@ static bool is_cfi_preamble_symbol(unsigned long addr)
>  		str_has_prefix("__pfx_", symbuf);
>  }
>  
> +#if IS_ENABLED(CONFIG_MODULES)
>  static int check_kprobe_address_safe(struct kprobe *p,
>  				     struct module **probed_mod)
> +#else
> +static int check_kprobe_address_safe(struct kprobe *p)
> +#endif
>  {
>  	int ret;
>  
> @@ -1580,6 +1584,7 @@ static int check_kprobe_address_safe(struct kprobe *p,
>  		goto out;
>  	}
>  
> +#if IS_ENABLED(CONFIG_MODULES)

Plain #ifdef will do here and below. IS_ENABLED is for usage withing the
code, like

	if (IS_ENABLED(CONFIG_MODULES))
		;

>  	/* Check if 'p' is probing a module. */
>  	*probed_mod = __module_text_address((unsigned long) p->addr);
>  	if (*probed_mod) {
Christophe Leroy March 7, 2024, 10:16 p.m. UTC | #2
Le 06/03/2024 à 21:05, Calvin Owens a écrit :
> [Vous ne recevez pas souvent de courriers de jcalvinowens@gmail.com. Découvrez pourquoi ceci est important à https://aka.ms/LearnAboutSenderIdentification ]
> 
> If something like this is merged down the road, it can go in at leisure
> once the module_alloc change is in: it's a one-way dependency.

Too many #ifdef, please reorganise stuff to avoid that and avoid 
changing prototypes based of CONFIG_MODULES.

Other few comments below.

> 
> Signed-off-by: Calvin Owens <jcalvinowens@gmail.com>
> ---
>   arch/Kconfig                |  2 +-
>   kernel/kprobes.c            | 22 ++++++++++++++++++++++
>   kernel/trace/trace_kprobe.c | 11 +++++++++++
>   3 files changed, 34 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/Kconfig b/arch/Kconfig
> index cfc24ced16dd..e60ce984d095 100644
> --- a/arch/Kconfig
> +++ b/arch/Kconfig
> @@ -52,8 +52,8 @@ config GENERIC_ENTRY
> 
>   config KPROBES
>          bool "Kprobes"
> -       depends on MODULES
>          depends on HAVE_KPROBES
> +       select MODULE_ALLOC
>          select KALLSYMS
>          select TASKS_RCU if PREEMPTION
>          help
> diff --git a/kernel/kprobes.c b/kernel/kprobes.c
> index 9d9095e81792..194270e17d57 100644
> --- a/kernel/kprobes.c
> +++ b/kernel/kprobes.c
> @@ -1556,8 +1556,12 @@ static bool is_cfi_preamble_symbol(unsigned long addr)
>                  str_has_prefix("__pfx_", symbuf);
>   }
> 
> +#if IS_ENABLED(CONFIG_MODULES)
>   static int check_kprobe_address_safe(struct kprobe *p,
>                                       struct module **probed_mod)
> +#else
> +static int check_kprobe_address_safe(struct kprobe *p)
> +#endif

A bit ugly to have to change the prototype, why not just keep probed_mod 
at all time ?

When CONFIG_MODULES is not selected, __module_text_address() returns 
NULL so it should work without that many #ifdefs.

>   {
>          int ret;
> 
> @@ -1580,6 +1584,7 @@ static int check_kprobe_address_safe(struct kprobe *p,
>                  goto out;
>          }
> 
> +#if IS_ENABLED(CONFIG_MODULES)
>          /* Check if 'p' is probing a module. */
>          *probed_mod = __module_text_address((unsigned long) p->addr);
>          if (*probed_mod) {
> @@ -1603,6 +1608,8 @@ static int check_kprobe_address_safe(struct kprobe *p,
>                          ret = -ENOENT;
>                  }
>          }
> +#endif
> +
>   out:
>          preempt_enable();
>          jump_label_unlock();
> @@ -1614,7 +1621,9 @@ int register_kprobe(struct kprobe *p)
>   {
>          int ret;
>          struct kprobe *old_p;
> +#if IS_ENABLED(CONFIG_MODULES)
>          struct module *probed_mod;
> +#endif
>          kprobe_opcode_t *addr;
>          bool on_func_entry;
> 
> @@ -1633,7 +1642,11 @@ int register_kprobe(struct kprobe *p)
>          p->nmissed = 0;
>          INIT_LIST_HEAD(&p->list);
> 
> +#if IS_ENABLED(CONFIG_MODULES)
>          ret = check_kprobe_address_safe(p, &probed_mod);
> +#else
> +       ret = check_kprobe_address_safe(p);
> +#endif
>          if (ret)
>                  return ret;
> 
> @@ -1676,8 +1689,10 @@ int register_kprobe(struct kprobe *p)
>   out:
>          mutex_unlock(&kprobe_mutex);
> 
> +#if IS_ENABLED(CONFIG_MODULES)
>          if (probed_mod)
>                  module_put(probed_mod);
> +#endif
> 
>          return ret;
>   }
> @@ -2482,6 +2497,7 @@ int kprobe_add_area_blacklist(unsigned long start, unsigned long end)
>          return 0;
>   }
> 
> +#if IS_ENABLED(CONFIG_MODULES)
>   /* Remove all symbols in given area from kprobe blacklist */
>   static void kprobe_remove_area_blacklist(unsigned long start, unsigned long end)
>   {
> @@ -2499,6 +2515,7 @@ static void kprobe_remove_ksym_blacklist(unsigned long entry)
>   {
>          kprobe_remove_area_blacklist(entry, entry + 1);
>   }
> +#endif
> 
>   int __weak arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *value,
>                                     char *type, char *sym)
> @@ -2564,6 +2581,7 @@ static int __init populate_kprobe_blacklist(unsigned long *start,
>          return ret ? : arch_populate_kprobe_blacklist();
>   }
> 
> +#if IS_ENABLED(CONFIG_MODULES)
>   static void add_module_kprobe_blacklist(struct module *mod)
>   {
>          unsigned long start, end;
> @@ -2665,6 +2683,7 @@ static struct notifier_block kprobe_module_nb = {
>          .notifier_call = kprobes_module_callback,
>          .priority = 0
>   };
> +#endif /* IS_ENABLED(CONFIG_MODULES) */
> 
>   void kprobe_free_init_mem(void)
>   {
> @@ -2724,8 +2743,11 @@ static int __init init_kprobes(void)
>          err = arch_init_kprobes();
>          if (!err)
>                  err = register_die_notifier(&kprobe_exceptions_nb);
> +
> +#if IS_ENABLED(CONFIG_MODULES)
>          if (!err)
>                  err = register_module_notifier(&kprobe_module_nb);
> +#endif
> 
>          kprobes_initialized = (err == 0);
>          kprobe_sysctls_init();
> diff --git a/kernel/trace/trace_kprobe.c b/kernel/trace/trace_kprobe.c
> index c4c6e0e0068b..dd4598f775b9 100644
> --- a/kernel/trace/trace_kprobe.c
> +++ b/kernel/trace/trace_kprobe.c
> @@ -102,6 +102,7 @@ static nokprobe_inline bool trace_kprobe_has_gone(struct trace_kprobe *tk)
>          return kprobe_gone(&tk->rp.kp);
>   }
> 
> +#if IS_ENABLED(CONFIG_MODULES)
>   static nokprobe_inline bool trace_kprobe_within_module(struct trace_kprobe *tk,
>                                                   struct module *mod)
>   {
> @@ -129,6 +130,12 @@ static nokprobe_inline bool trace_kprobe_module_exist(struct trace_kprobe *tk)
> 
>          return ret;
>   }
> +#else
> +static nokprobe_inline bool trace_kprobe_module_exist(struct trace_kprobe *tk)
> +{
> +       return true;
> +}
> +#endif
> 
>   static bool trace_kprobe_is_busy(struct dyn_event *ev)
>   {
> @@ -670,6 +677,7 @@ static int register_trace_kprobe(struct trace_kprobe *tk)
>          return ret;
>   }
> 
> +#if IS_ENABLED(CONFIG_MODULES)
>   /* Module notifier call back, checking event on the module */
>   static int trace_kprobe_module_callback(struct notifier_block *nb,
>                                         unsigned long val, void *data)
> @@ -704,6 +712,7 @@ static struct notifier_block trace_kprobe_module_nb = {
>          .notifier_call = trace_kprobe_module_callback,
>          .priority = 1   /* Invoked after kprobe module callback */
>   };
> +#endif /* IS_ENABLED(CONFIG_MODULES) */
> 
>   static int count_symbols(void *data, unsigned long unused)
>   {
> @@ -1897,8 +1906,10 @@ static __init int init_kprobe_trace_early(void)
>          if (ret)
>                  return ret;
> 
> +#if IS_ENABLED(CONFIG_MODULES)
>          if (register_module_notifier(&trace_kprobe_module_nb))
>                  return -EINVAL;
Why a #if here ?

If CONFIG_MODULES is not selected, register_module_notifier() return 
always 0.

> +#endif
> 
>          return 0;
>   }
> --
> 2.43.0
> 
>
Masami Hiramatsu (Google) March 8, 2024, 2:46 a.m. UTC | #3
On Wed,  6 Mar 2024 12:05:10 -0800
Calvin Owens <jcalvinowens@gmail.com> wrote:

> If something like this is merged down the road, it can go in at leisure
> once the module_alloc change is in: it's a one-way dependency.
> 
> Signed-off-by: Calvin Owens <jcalvinowens@gmail.com>
> ---
>  arch/Kconfig                |  2 +-
>  kernel/kprobes.c            | 22 ++++++++++++++++++++++
>  kernel/trace/trace_kprobe.c | 11 +++++++++++
>  3 files changed, 34 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/Kconfig b/arch/Kconfig
> index cfc24ced16dd..e60ce984d095 100644
> --- a/arch/Kconfig
> +++ b/arch/Kconfig
> @@ -52,8 +52,8 @@ config GENERIC_ENTRY
>  
>  config KPROBES
>  	bool "Kprobes"
> -	depends on MODULES
>  	depends on HAVE_KPROBES
> +	select MODULE_ALLOC

OK, if we use EXEC_ALLOC,

config EXEC_ALLOC
	depends on HAVE_EXEC_ALLOC

And 

  config KPROBES
  	bool "Kprobes"
	depends on MODULES || EXEC_ALLOC
	select EXEC_ALLOC if HAVE_EXEC_ALLOC

then kprobes can be enabled either modules supported or exec_alloc is supported.
(new arch does not need to implement exec_alloc)

Maybe we also need something like

#ifdef CONFIG_EXEC_ALLOC
#define module_alloc(size) exec_alloc(size)
#endif

in kprobes.h, or just add `replacing module_alloc with exec_alloc` patch.

Thank you,

>  	select KALLSYMS
>  	select TASKS_RCU if PREEMPTION
>  	help
> diff --git a/kernel/kprobes.c b/kernel/kprobes.c
> index 9d9095e81792..194270e17d57 100644
> --- a/kernel/kprobes.c
> +++ b/kernel/kprobes.c
> @@ -1556,8 +1556,12 @@ static bool is_cfi_preamble_symbol(unsigned long addr)
>  		str_has_prefix("__pfx_", symbuf);
>  }
>  
> +#if IS_ENABLED(CONFIG_MODULES)
>  static int check_kprobe_address_safe(struct kprobe *p,
>  				     struct module **probed_mod)
> +#else
> +static int check_kprobe_address_safe(struct kprobe *p)
> +#endif
>  {
>  	int ret;
>  
> @@ -1580,6 +1584,7 @@ static int check_kprobe_address_safe(struct kprobe *p,
>  		goto out;
>  	}
>  
> +#if IS_ENABLED(CONFIG_MODULES)
>  	/* Check if 'p' is probing a module. */
>  	*probed_mod = __module_text_address((unsigned long) p->addr);
>  	if (*probed_mod) {
> @@ -1603,6 +1608,8 @@ static int check_kprobe_address_safe(struct kprobe *p,
>  			ret = -ENOENT;
>  		}
>  	}
> +#endif
> +
>  out:
>  	preempt_enable();
>  	jump_label_unlock();
> @@ -1614,7 +1621,9 @@ int register_kprobe(struct kprobe *p)
>  {
>  	int ret;
>  	struct kprobe *old_p;
> +#if IS_ENABLED(CONFIG_MODULES)
>  	struct module *probed_mod;
> +#endif
>  	kprobe_opcode_t *addr;
>  	bool on_func_entry;
>  
> @@ -1633,7 +1642,11 @@ int register_kprobe(struct kprobe *p)
>  	p->nmissed = 0;
>  	INIT_LIST_HEAD(&p->list);
>  
> +#if IS_ENABLED(CONFIG_MODULES)
>  	ret = check_kprobe_address_safe(p, &probed_mod);
> +#else
> +	ret = check_kprobe_address_safe(p);
> +#endif
>  	if (ret)
>  		return ret;
>  
> @@ -1676,8 +1689,10 @@ int register_kprobe(struct kprobe *p)
>  out:
>  	mutex_unlock(&kprobe_mutex);
>  
> +#if IS_ENABLED(CONFIG_MODULES)
>  	if (probed_mod)
>  		module_put(probed_mod);
> +#endif
>  
>  	return ret;
>  }
> @@ -2482,6 +2497,7 @@ int kprobe_add_area_blacklist(unsigned long start, unsigned long end)
>  	return 0;
>  }
>  
> +#if IS_ENABLED(CONFIG_MODULES)
>  /* Remove all symbols in given area from kprobe blacklist */
>  static void kprobe_remove_area_blacklist(unsigned long start, unsigned long end)
>  {
> @@ -2499,6 +2515,7 @@ static void kprobe_remove_ksym_blacklist(unsigned long entry)
>  {
>  	kprobe_remove_area_blacklist(entry, entry + 1);
>  }
> +#endif
>  
>  int __weak arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *value,
>  				   char *type, char *sym)
> @@ -2564,6 +2581,7 @@ static int __init populate_kprobe_blacklist(unsigned long *start,
>  	return ret ? : arch_populate_kprobe_blacklist();
>  }
>  
> +#if IS_ENABLED(CONFIG_MODULES)
>  static void add_module_kprobe_blacklist(struct module *mod)
>  {
>  	unsigned long start, end;
> @@ -2665,6 +2683,7 @@ static struct notifier_block kprobe_module_nb = {
>  	.notifier_call = kprobes_module_callback,
>  	.priority = 0
>  };
> +#endif /* IS_ENABLED(CONFIG_MODULES) */
>  
>  void kprobe_free_init_mem(void)
>  {
> @@ -2724,8 +2743,11 @@ static int __init init_kprobes(void)
>  	err = arch_init_kprobes();
>  	if (!err)
>  		err = register_die_notifier(&kprobe_exceptions_nb);
> +
> +#if IS_ENABLED(CONFIG_MODULES)
>  	if (!err)
>  		err = register_module_notifier(&kprobe_module_nb);
> +#endif
>  
>  	kprobes_initialized = (err == 0);
>  	kprobe_sysctls_init();
> diff --git a/kernel/trace/trace_kprobe.c b/kernel/trace/trace_kprobe.c
> index c4c6e0e0068b..dd4598f775b9 100644
> --- a/kernel/trace/trace_kprobe.c
> +++ b/kernel/trace/trace_kprobe.c
> @@ -102,6 +102,7 @@ static nokprobe_inline bool trace_kprobe_has_gone(struct trace_kprobe *tk)
>  	return kprobe_gone(&tk->rp.kp);
>  }
>  
> +#if IS_ENABLED(CONFIG_MODULES)
>  static nokprobe_inline bool trace_kprobe_within_module(struct trace_kprobe *tk,
>  						 struct module *mod)
>  {
> @@ -129,6 +130,12 @@ static nokprobe_inline bool trace_kprobe_module_exist(struct trace_kprobe *tk)
>  
>  	return ret;
>  }
> +#else
> +static nokprobe_inline bool trace_kprobe_module_exist(struct trace_kprobe *tk)
> +{
> +	return true;
> +}
> +#endif
>  
>  static bool trace_kprobe_is_busy(struct dyn_event *ev)
>  {
> @@ -670,6 +677,7 @@ static int register_trace_kprobe(struct trace_kprobe *tk)
>  	return ret;
>  }
>  
> +#if IS_ENABLED(CONFIG_MODULES)
>  /* Module notifier call back, checking event on the module */
>  static int trace_kprobe_module_callback(struct notifier_block *nb,
>  				       unsigned long val, void *data)
> @@ -704,6 +712,7 @@ static struct notifier_block trace_kprobe_module_nb = {
>  	.notifier_call = trace_kprobe_module_callback,
>  	.priority = 1	/* Invoked after kprobe module callback */
>  };
> +#endif /* IS_ENABLED(CONFIG_MODULES) */
>  
>  static int count_symbols(void *data, unsigned long unused)
>  {
> @@ -1897,8 +1906,10 @@ static __init int init_kprobe_trace_early(void)
>  	if (ret)
>  		return ret;
>  
> +#if IS_ENABLED(CONFIG_MODULES)
>  	if (register_module_notifier(&trace_kprobe_module_nb))
>  		return -EINVAL;
> +#endif
>  
>  	return 0;
>  }
> -- 
> 2.43.0
>
Masami Hiramatsu (Google) March 8, 2024, 2:46 a.m. UTC | #4
On Thu, 7 Mar 2024 09:22:07 +0200
Mike Rapoport <rppt@kernel.org> wrote:

> On Wed, Mar 06, 2024 at 12:05:10PM -0800, Calvin Owens wrote:
> > If something like this is merged down the road, it can go in at leisure
> > once the module_alloc change is in: it's a one-way dependency.
> > 
> > Signed-off-by: Calvin Owens <jcalvinowens@gmail.com>
> > ---
> >  arch/Kconfig                |  2 +-
> >  kernel/kprobes.c            | 22 ++++++++++++++++++++++
> >  kernel/trace/trace_kprobe.c | 11 +++++++++++
> >  3 files changed, 34 insertions(+), 1 deletion(-)
> 
> When I did this in my last execmem posting, I think I've got slightly less
> ugly ifdery, you may want to take a look at that:
> 
> https://lore.kernel.org/all/20230918072955.2507221-13-rppt@kernel.org

Good catch, and sorry I missed that series last year.
But it seems your patch seems less ugly.

Calvin, can you follow his patch?

Thank you,

>  
> > diff --git a/arch/Kconfig b/arch/Kconfig
> > index cfc24ced16dd..e60ce984d095 100644
> > --- a/arch/Kconfig
> > +++ b/arch/Kconfig
> > @@ -52,8 +52,8 @@ config GENERIC_ENTRY
> >  
> >  config KPROBES
> >  	bool "Kprobes"
> > -	depends on MODULES
> >  	depends on HAVE_KPROBES
> > +	select MODULE_ALLOC
> >  	select KALLSYMS
> >  	select TASKS_RCU if PREEMPTION
> >  	help
> > diff --git a/kernel/kprobes.c b/kernel/kprobes.c
> > index 9d9095e81792..194270e17d57 100644
> > --- a/kernel/kprobes.c
> > +++ b/kernel/kprobes.c
> > @@ -1556,8 +1556,12 @@ static bool is_cfi_preamble_symbol(unsigned long addr)
> >  		str_has_prefix("__pfx_", symbuf);
> >  }
> >  
> > +#if IS_ENABLED(CONFIG_MODULES)
> >  static int check_kprobe_address_safe(struct kprobe *p,
> >  				     struct module **probed_mod)
> > +#else
> > +static int check_kprobe_address_safe(struct kprobe *p)
> > +#endif
> >  {
> >  	int ret;
> >  
> > @@ -1580,6 +1584,7 @@ static int check_kprobe_address_safe(struct kprobe *p,
> >  		goto out;
> >  	}
> >  
> > +#if IS_ENABLED(CONFIG_MODULES)
> 
> Plain #ifdef will do here and below. IS_ENABLED is for usage withing the
> code, like
> 
> 	if (IS_ENABLED(CONFIG_MODULES))
> 		;
> 
> >  	/* Check if 'p' is probing a module. */
> >  	*probed_mod = __module_text_address((unsigned long) p->addr);
> >  	if (*probed_mod) {
> 
> -- 
> Sincerely yours,
> Mike.
Calvin Owens March 8, 2024, 8:36 p.m. UTC | #5
On Thursday 03/07 at 09:22 +0200, Mike Rapoport wrote:
> On Wed, Mar 06, 2024 at 12:05:10PM -0800, Calvin Owens wrote:
> > If something like this is merged down the road, it can go in at leisure
> > once the module_alloc change is in: it's a one-way dependency.
> > 
> > Signed-off-by: Calvin Owens <jcalvinowens@gmail.com>
> > ---
> >  arch/Kconfig                |  2 +-
> >  kernel/kprobes.c            | 22 ++++++++++++++++++++++
> >  kernel/trace/trace_kprobe.c | 11 +++++++++++
> >  3 files changed, 34 insertions(+), 1 deletion(-)
> 
> When I did this in my last execmem posting, I think I've got slightly less
> ugly ifdery, you may want to take a look at that:
> 
> https://lore.kernel.org/all/20230918072955.2507221-13-rppt@kernel.org

Thanks Mike, I definitely agree. I'm annoyed at myself for not finding
your patches, I spent some time looking for prior work and I really
don't know how I missed it...

> > diff --git a/arch/Kconfig b/arch/Kconfig
> > index cfc24ced16dd..e60ce984d095 100644
> > --- a/arch/Kconfig
> > +++ b/arch/Kconfig
> > @@ -52,8 +52,8 @@ config GENERIC_ENTRY
> >  
> >  config KPROBES
> >  	bool "Kprobes"
> > -	depends on MODULES
> >  	depends on HAVE_KPROBES
> > +	select MODULE_ALLOC
> >  	select KALLSYMS
> >  	select TASKS_RCU if PREEMPTION
> >  	help
> > diff --git a/kernel/kprobes.c b/kernel/kprobes.c
> > index 9d9095e81792..194270e17d57 100644
> > --- a/kernel/kprobes.c
> > +++ b/kernel/kprobes.c
> > @@ -1556,8 +1556,12 @@ static bool is_cfi_preamble_symbol(unsigned long addr)
> >  		str_has_prefix("__pfx_", symbuf);
> >  }
> >  
> > +#if IS_ENABLED(CONFIG_MODULES)
> >  static int check_kprobe_address_safe(struct kprobe *p,
> >  				     struct module **probed_mod)
> > +#else
> > +static int check_kprobe_address_safe(struct kprobe *p)
> > +#endif
> >  {
> >  	int ret;
> >  
> > @@ -1580,6 +1584,7 @@ static int check_kprobe_address_safe(struct kprobe *p,
> >  		goto out;
> >  	}
> >  
> > +#if IS_ENABLED(CONFIG_MODULES)
> 
> Plain #ifdef will do here and below. IS_ENABLED is for usage withing the
> code, like
> 
> 	if (IS_ENABLED(CONFIG_MODULES))
> 		;
> 
> >  	/* Check if 'p' is probing a module. */
> >  	*probed_mod = __module_text_address((unsigned long) p->addr);
> >  	if (*probed_mod) {
> 
> -- 
> Sincerely yours,
> Mike.
Calvin Owens March 8, 2024, 8:57 p.m. UTC | #6
On Friday 03/08 at 11:46 +0900, Masami Hiramatsu wrote:
> On Wed,  6 Mar 2024 12:05:10 -0800
> Calvin Owens <jcalvinowens@gmail.com> wrote:
> 
> > If something like this is merged down the road, it can go in at leisure
> > once the module_alloc change is in: it's a one-way dependency.
> > 
> > Signed-off-by: Calvin Owens <jcalvinowens@gmail.com>
> > ---
> >  arch/Kconfig                |  2 +-
> >  kernel/kprobes.c            | 22 ++++++++++++++++++++++
> >  kernel/trace/trace_kprobe.c | 11 +++++++++++
> >  3 files changed, 34 insertions(+), 1 deletion(-)
> > 
> > diff --git a/arch/Kconfig b/arch/Kconfig
> > index cfc24ced16dd..e60ce984d095 100644
> > --- a/arch/Kconfig
> > +++ b/arch/Kconfig
> > @@ -52,8 +52,8 @@ config GENERIC_ENTRY
> >  
> >  config KPROBES
> >  	bool "Kprobes"
> > -	depends on MODULES
> >  	depends on HAVE_KPROBES
> > +	select MODULE_ALLOC
> 
> OK, if we use EXEC_ALLOC,
> 
> config EXEC_ALLOC
> 	depends on HAVE_EXEC_ALLOC
> 
> And 
> 
>   config KPROBES
>   	bool "Kprobes"
> 	depends on MODULES || EXEC_ALLOC
> 	select EXEC_ALLOC if HAVE_EXEC_ALLOC
> 
> then kprobes can be enabled either modules supported or exec_alloc is supported.
> (new arch does not need to implement exec_alloc)
> 
> Maybe we also need something like
> 
> #ifdef CONFIG_EXEC_ALLOC
> #define module_alloc(size) exec_alloc(size)
> #endif
> 
> in kprobes.h, or just add `replacing module_alloc with exec_alloc` patch.
> 
> Thank you,

The example was helpful, thanks. I see what you mean with
HAVE_EXEC_ALLOC, I'll implement it like that in the next verison.

> >  	select KALLSYMS
> >  	select TASKS_RCU if PREEMPTION
> >  	help
> > diff --git a/kernel/kprobes.c b/kernel/kprobes.c
> > index 9d9095e81792..194270e17d57 100644
> > --- a/kernel/kprobes.c
> > +++ b/kernel/kprobes.c
> > @@ -1556,8 +1556,12 @@ static bool is_cfi_preamble_symbol(unsigned long addr)
> >  		str_has_prefix("__pfx_", symbuf);
> >  }
> >  
> > +#if IS_ENABLED(CONFIG_MODULES)
> >  static int check_kprobe_address_safe(struct kprobe *p,
> >  				     struct module **probed_mod)
> > +#else
> > +static int check_kprobe_address_safe(struct kprobe *p)
> > +#endif
> >  {
> >  	int ret;
> >  
> > @@ -1580,6 +1584,7 @@ static int check_kprobe_address_safe(struct kprobe *p,
> >  		goto out;
> >  	}
> >  
> > +#if IS_ENABLED(CONFIG_MODULES)
> >  	/* Check if 'p' is probing a module. */
> >  	*probed_mod = __module_text_address((unsigned long) p->addr);
> >  	if (*probed_mod) {
> > @@ -1603,6 +1608,8 @@ static int check_kprobe_address_safe(struct kprobe *p,
> >  			ret = -ENOENT;
> >  		}
> >  	}
> > +#endif
> > +
> >  out:
> >  	preempt_enable();
> >  	jump_label_unlock();
> > @@ -1614,7 +1621,9 @@ int register_kprobe(struct kprobe *p)
> >  {
> >  	int ret;
> >  	struct kprobe *old_p;
> > +#if IS_ENABLED(CONFIG_MODULES)
> >  	struct module *probed_mod;
> > +#endif
> >  	kprobe_opcode_t *addr;
> >  	bool on_func_entry;
> >  
> > @@ -1633,7 +1642,11 @@ int register_kprobe(struct kprobe *p)
> >  	p->nmissed = 0;
> >  	INIT_LIST_HEAD(&p->list);
> >  
> > +#if IS_ENABLED(CONFIG_MODULES)
> >  	ret = check_kprobe_address_safe(p, &probed_mod);
> > +#else
> > +	ret = check_kprobe_address_safe(p);
> > +#endif
> >  	if (ret)
> >  		return ret;
> >  
> > @@ -1676,8 +1689,10 @@ int register_kprobe(struct kprobe *p)
> >  out:
> >  	mutex_unlock(&kprobe_mutex);
> >  
> > +#if IS_ENABLED(CONFIG_MODULES)
> >  	if (probed_mod)
> >  		module_put(probed_mod);
> > +#endif
> >  
> >  	return ret;
> >  }
> > @@ -2482,6 +2497,7 @@ int kprobe_add_area_blacklist(unsigned long start, unsigned long end)
> >  	return 0;
> >  }
> >  
> > +#if IS_ENABLED(CONFIG_MODULES)
> >  /* Remove all symbols in given area from kprobe blacklist */
> >  static void kprobe_remove_area_blacklist(unsigned long start, unsigned long end)
> >  {
> > @@ -2499,6 +2515,7 @@ static void kprobe_remove_ksym_blacklist(unsigned long entry)
> >  {
> >  	kprobe_remove_area_blacklist(entry, entry + 1);
> >  }
> > +#endif
> >  
> >  int __weak arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *value,
> >  				   char *type, char *sym)
> > @@ -2564,6 +2581,7 @@ static int __init populate_kprobe_blacklist(unsigned long *start,
> >  	return ret ? : arch_populate_kprobe_blacklist();
> >  }
> >  
> > +#if IS_ENABLED(CONFIG_MODULES)
> >  static void add_module_kprobe_blacklist(struct module *mod)
> >  {
> >  	unsigned long start, end;
> > @@ -2665,6 +2683,7 @@ static struct notifier_block kprobe_module_nb = {
> >  	.notifier_call = kprobes_module_callback,
> >  	.priority = 0
> >  };
> > +#endif /* IS_ENABLED(CONFIG_MODULES) */
> >  
> >  void kprobe_free_init_mem(void)
> >  {
> > @@ -2724,8 +2743,11 @@ static int __init init_kprobes(void)
> >  	err = arch_init_kprobes();
> >  	if (!err)
> >  		err = register_die_notifier(&kprobe_exceptions_nb);
> > +
> > +#if IS_ENABLED(CONFIG_MODULES)
> >  	if (!err)
> >  		err = register_module_notifier(&kprobe_module_nb);
> > +#endif
> >  
> >  	kprobes_initialized = (err == 0);
> >  	kprobe_sysctls_init();
> > diff --git a/kernel/trace/trace_kprobe.c b/kernel/trace/trace_kprobe.c
> > index c4c6e0e0068b..dd4598f775b9 100644
> > --- a/kernel/trace/trace_kprobe.c
> > +++ b/kernel/trace/trace_kprobe.c
> > @@ -102,6 +102,7 @@ static nokprobe_inline bool trace_kprobe_has_gone(struct trace_kprobe *tk)
> >  	return kprobe_gone(&tk->rp.kp);
> >  }
> >  
> > +#if IS_ENABLED(CONFIG_MODULES)
> >  static nokprobe_inline bool trace_kprobe_within_module(struct trace_kprobe *tk,
> >  						 struct module *mod)
> >  {
> > @@ -129,6 +130,12 @@ static nokprobe_inline bool trace_kprobe_module_exist(struct trace_kprobe *tk)
> >  
> >  	return ret;
> >  }
> > +#else
> > +static nokprobe_inline bool trace_kprobe_module_exist(struct trace_kprobe *tk)
> > +{
> > +	return true;
> > +}
> > +#endif
> >  
> >  static bool trace_kprobe_is_busy(struct dyn_event *ev)
> >  {
> > @@ -670,6 +677,7 @@ static int register_trace_kprobe(struct trace_kprobe *tk)
> >  	return ret;
> >  }
> >  
> > +#if IS_ENABLED(CONFIG_MODULES)
> >  /* Module notifier call back, checking event on the module */
> >  static int trace_kprobe_module_callback(struct notifier_block *nb,
> >  				       unsigned long val, void *data)
> > @@ -704,6 +712,7 @@ static struct notifier_block trace_kprobe_module_nb = {
> >  	.notifier_call = trace_kprobe_module_callback,
> >  	.priority = 1	/* Invoked after kprobe module callback */
> >  };
> > +#endif /* IS_ENABLED(CONFIG_MODULES) */
> >  
> >  static int count_symbols(void *data, unsigned long unused)
> >  {
> > @@ -1897,8 +1906,10 @@ static __init int init_kprobe_trace_early(void)
> >  	if (ret)
> >  		return ret;
> >  
> > +#if IS_ENABLED(CONFIG_MODULES)
> >  	if (register_module_notifier(&trace_kprobe_module_nb))
> >  		return -EINVAL;
> > +#endif
> >  
> >  	return 0;
> >  }
> > -- 
> > 2.43.0
> > 
> 
> 
> -- 
> Masami Hiramatsu (Google) <mhiramat@kernel.org>
Calvin Owens March 8, 2024, 9:02 p.m. UTC | #7
On Thursday 03/07 at 22:16 +0000, Christophe Leroy wrote:
> 
> 
> Le 06/03/2024 à 21:05, Calvin Owens a écrit :
> > [Vous ne recevez pas souvent de courriers de jcalvinowens@gmail.com. Découvrez pourquoi ceci est important à https://aka.ms/LearnAboutSenderIdentification ]
> > 
> > If something like this is merged down the road, it can go in at leisure
> > once the module_alloc change is in: it's a one-way dependency.
> 
> Too many #ifdef, please reorganise stuff to avoid that and avoid 
> changing prototypes based of CONFIG_MODULES.
> 
> Other few comments below.

TBH the ugliness here was just me trying not to trigger -Wunused, but
that was silly: as you point out below, it's unncessary. I'll clean it
up.

> > 
> > Signed-off-by: Calvin Owens <jcalvinowens@gmail.com>
> > ---
> >   arch/Kconfig                |  2 +-
> >   kernel/kprobes.c            | 22 ++++++++++++++++++++++
> >   kernel/trace/trace_kprobe.c | 11 +++++++++++
> >   3 files changed, 34 insertions(+), 1 deletion(-)
> > 
> > diff --git a/arch/Kconfig b/arch/Kconfig
> > index cfc24ced16dd..e60ce984d095 100644
> > --- a/arch/Kconfig
> > +++ b/arch/Kconfig
> > @@ -52,8 +52,8 @@ config GENERIC_ENTRY
> > 
> >   config KPROBES
> >          bool "Kprobes"
> > -       depends on MODULES
> >          depends on HAVE_KPROBES
> > +       select MODULE_ALLOC
> >          select KALLSYMS
> >          select TASKS_RCU if PREEMPTION
> >          help
> > diff --git a/kernel/kprobes.c b/kernel/kprobes.c
> > index 9d9095e81792..194270e17d57 100644
> > --- a/kernel/kprobes.c
> > +++ b/kernel/kprobes.c
> > @@ -1556,8 +1556,12 @@ static bool is_cfi_preamble_symbol(unsigned long addr)
> >                  str_has_prefix("__pfx_", symbuf);
> >   }
> > 
> > +#if IS_ENABLED(CONFIG_MODULES)
> >   static int check_kprobe_address_safe(struct kprobe *p,
> >                                       struct module **probed_mod)
> > +#else
> > +static int check_kprobe_address_safe(struct kprobe *p)
> > +#endif
> 
> A bit ugly to have to change the prototype, why not just keep probed_mod 
> at all time ?
> 
> When CONFIG_MODULES is not selected, __module_text_address() returns 
> NULL so it should work without that many #ifdefs.
> 
> >   {
> >          int ret;
> > 
> > @@ -1580,6 +1584,7 @@ static int check_kprobe_address_safe(struct kprobe *p,
> >                  goto out;
> >          }
> > 
> > +#if IS_ENABLED(CONFIG_MODULES)
> >          /* Check if 'p' is probing a module. */
> >          *probed_mod = __module_text_address((unsigned long) p->addr);
> >          if (*probed_mod) {
> > @@ -1603,6 +1608,8 @@ static int check_kprobe_address_safe(struct kprobe *p,
> >                          ret = -ENOENT;
> >                  }
> >          }
> > +#endif
> > +
> >   out:
> >          preempt_enable();
> >          jump_label_unlock();
> > @@ -1614,7 +1621,9 @@ int register_kprobe(struct kprobe *p)
> >   {
> >          int ret;
> >          struct kprobe *old_p;
> > +#if IS_ENABLED(CONFIG_MODULES)
> >          struct module *probed_mod;
> > +#endif
> >          kprobe_opcode_t *addr;
> >          bool on_func_entry;
> > 
> > @@ -1633,7 +1642,11 @@ int register_kprobe(struct kprobe *p)
> >          p->nmissed = 0;
> >          INIT_LIST_HEAD(&p->list);
> > 
> > +#if IS_ENABLED(CONFIG_MODULES)
> >          ret = check_kprobe_address_safe(p, &probed_mod);
> > +#else
> > +       ret = check_kprobe_address_safe(p);
> > +#endif
> >          if (ret)
> >                  return ret;
> > 
> > @@ -1676,8 +1689,10 @@ int register_kprobe(struct kprobe *p)
> >   out:
> >          mutex_unlock(&kprobe_mutex);
> > 
> > +#if IS_ENABLED(CONFIG_MODULES)
> >          if (probed_mod)
> >                  module_put(probed_mod);
> > +#endif
> > 
> >          return ret;
> >   }
> > @@ -2482,6 +2497,7 @@ int kprobe_add_area_blacklist(unsigned long start, unsigned long end)
> >          return 0;
> >   }
> > 
> > +#if IS_ENABLED(CONFIG_MODULES)
> >   /* Remove all symbols in given area from kprobe blacklist */
> >   static void kprobe_remove_area_blacklist(unsigned long start, unsigned long end)
> >   {
> > @@ -2499,6 +2515,7 @@ static void kprobe_remove_ksym_blacklist(unsigned long entry)
> >   {
> >          kprobe_remove_area_blacklist(entry, entry + 1);
> >   }
> > +#endif
> > 
> >   int __weak arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *value,
> >                                     char *type, char *sym)
> > @@ -2564,6 +2581,7 @@ static int __init populate_kprobe_blacklist(unsigned long *start,
> >          return ret ? : arch_populate_kprobe_blacklist();
> >   }
> > 
> > +#if IS_ENABLED(CONFIG_MODULES)
> >   static void add_module_kprobe_blacklist(struct module *mod)
> >   {
> >          unsigned long start, end;
> > @@ -2665,6 +2683,7 @@ static struct notifier_block kprobe_module_nb = {
> >          .notifier_call = kprobes_module_callback,
> >          .priority = 0
> >   };
> > +#endif /* IS_ENABLED(CONFIG_MODULES) */
> > 
> >   void kprobe_free_init_mem(void)
> >   {
> > @@ -2724,8 +2743,11 @@ static int __init init_kprobes(void)
> >          err = arch_init_kprobes();
> >          if (!err)
> >                  err = register_die_notifier(&kprobe_exceptions_nb);
> > +
> > +#if IS_ENABLED(CONFIG_MODULES)
> >          if (!err)
> >                  err = register_module_notifier(&kprobe_module_nb);
> > +#endif
> > 
> >          kprobes_initialized = (err == 0);
> >          kprobe_sysctls_init();
> > diff --git a/kernel/trace/trace_kprobe.c b/kernel/trace/trace_kprobe.c
> > index c4c6e0e0068b..dd4598f775b9 100644
> > --- a/kernel/trace/trace_kprobe.c
> > +++ b/kernel/trace/trace_kprobe.c
> > @@ -102,6 +102,7 @@ static nokprobe_inline bool trace_kprobe_has_gone(struct trace_kprobe *tk)
> >          return kprobe_gone(&tk->rp.kp);
> >   }
> > 
> > +#if IS_ENABLED(CONFIG_MODULES)
> >   static nokprobe_inline bool trace_kprobe_within_module(struct trace_kprobe *tk,
> >                                                   struct module *mod)
> >   {
> > @@ -129,6 +130,12 @@ static nokprobe_inline bool trace_kprobe_module_exist(struct trace_kprobe *tk)
> > 
> >          return ret;
> >   }
> > +#else
> > +static nokprobe_inline bool trace_kprobe_module_exist(struct trace_kprobe *tk)
> > +{
> > +       return true;
> > +}
> > +#endif
> > 
> >   static bool trace_kprobe_is_busy(struct dyn_event *ev)
> >   {
> > @@ -670,6 +677,7 @@ static int register_trace_kprobe(struct trace_kprobe *tk)
> >          return ret;
> >   }
> > 
> > +#if IS_ENABLED(CONFIG_MODULES)
> >   /* Module notifier call back, checking event on the module */
> >   static int trace_kprobe_module_callback(struct notifier_block *nb,
> >                                         unsigned long val, void *data)
> > @@ -704,6 +712,7 @@ static struct notifier_block trace_kprobe_module_nb = {
> >          .notifier_call = trace_kprobe_module_callback,
> >          .priority = 1   /* Invoked after kprobe module callback */
> >   };
> > +#endif /* IS_ENABLED(CONFIG_MODULES) */
> > 
> >   static int count_symbols(void *data, unsigned long unused)
> >   {
> > @@ -1897,8 +1906,10 @@ static __init int init_kprobe_trace_early(void)
> >          if (ret)
> >                  return ret;
> > 
> > +#if IS_ENABLED(CONFIG_MODULES)
> >          if (register_module_notifier(&trace_kprobe_module_nb))
> >                  return -EINVAL;
> Why a #if here ?
> 
> If CONFIG_MODULES is not selected, register_module_notifier() return 
> always 0.
> 
> > +#endif
> > 
> >          return 0;
> >   }
> > --
> > 2.43.0
> > 
> >
diff mbox series

Patch

diff --git a/arch/Kconfig b/arch/Kconfig
index cfc24ced16dd..e60ce984d095 100644
--- a/arch/Kconfig
+++ b/arch/Kconfig
@@ -52,8 +52,8 @@  config GENERIC_ENTRY
 
 config KPROBES
 	bool "Kprobes"
-	depends on MODULES
 	depends on HAVE_KPROBES
+	select MODULE_ALLOC
 	select KALLSYMS
 	select TASKS_RCU if PREEMPTION
 	help
diff --git a/kernel/kprobes.c b/kernel/kprobes.c
index 9d9095e81792..194270e17d57 100644
--- a/kernel/kprobes.c
+++ b/kernel/kprobes.c
@@ -1556,8 +1556,12 @@  static bool is_cfi_preamble_symbol(unsigned long addr)
 		str_has_prefix("__pfx_", symbuf);
 }
 
+#if IS_ENABLED(CONFIG_MODULES)
 static int check_kprobe_address_safe(struct kprobe *p,
 				     struct module **probed_mod)
+#else
+static int check_kprobe_address_safe(struct kprobe *p)
+#endif
 {
 	int ret;
 
@@ -1580,6 +1584,7 @@  static int check_kprobe_address_safe(struct kprobe *p,
 		goto out;
 	}
 
+#if IS_ENABLED(CONFIG_MODULES)
 	/* Check if 'p' is probing a module. */
 	*probed_mod = __module_text_address((unsigned long) p->addr);
 	if (*probed_mod) {
@@ -1603,6 +1608,8 @@  static int check_kprobe_address_safe(struct kprobe *p,
 			ret = -ENOENT;
 		}
 	}
+#endif
+
 out:
 	preempt_enable();
 	jump_label_unlock();
@@ -1614,7 +1621,9 @@  int register_kprobe(struct kprobe *p)
 {
 	int ret;
 	struct kprobe *old_p;
+#if IS_ENABLED(CONFIG_MODULES)
 	struct module *probed_mod;
+#endif
 	kprobe_opcode_t *addr;
 	bool on_func_entry;
 
@@ -1633,7 +1642,11 @@  int register_kprobe(struct kprobe *p)
 	p->nmissed = 0;
 	INIT_LIST_HEAD(&p->list);
 
+#if IS_ENABLED(CONFIG_MODULES)
 	ret = check_kprobe_address_safe(p, &probed_mod);
+#else
+	ret = check_kprobe_address_safe(p);
+#endif
 	if (ret)
 		return ret;
 
@@ -1676,8 +1689,10 @@  int register_kprobe(struct kprobe *p)
 out:
 	mutex_unlock(&kprobe_mutex);
 
+#if IS_ENABLED(CONFIG_MODULES)
 	if (probed_mod)
 		module_put(probed_mod);
+#endif
 
 	return ret;
 }
@@ -2482,6 +2497,7 @@  int kprobe_add_area_blacklist(unsigned long start, unsigned long end)
 	return 0;
 }
 
+#if IS_ENABLED(CONFIG_MODULES)
 /* Remove all symbols in given area from kprobe blacklist */
 static void kprobe_remove_area_blacklist(unsigned long start, unsigned long end)
 {
@@ -2499,6 +2515,7 @@  static void kprobe_remove_ksym_blacklist(unsigned long entry)
 {
 	kprobe_remove_area_blacklist(entry, entry + 1);
 }
+#endif
 
 int __weak arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *value,
 				   char *type, char *sym)
@@ -2564,6 +2581,7 @@  static int __init populate_kprobe_blacklist(unsigned long *start,
 	return ret ? : arch_populate_kprobe_blacklist();
 }
 
+#if IS_ENABLED(CONFIG_MODULES)
 static void add_module_kprobe_blacklist(struct module *mod)
 {
 	unsigned long start, end;
@@ -2665,6 +2683,7 @@  static struct notifier_block kprobe_module_nb = {
 	.notifier_call = kprobes_module_callback,
 	.priority = 0
 };
+#endif /* IS_ENABLED(CONFIG_MODULES) */
 
 void kprobe_free_init_mem(void)
 {
@@ -2724,8 +2743,11 @@  static int __init init_kprobes(void)
 	err = arch_init_kprobes();
 	if (!err)
 		err = register_die_notifier(&kprobe_exceptions_nb);
+
+#if IS_ENABLED(CONFIG_MODULES)
 	if (!err)
 		err = register_module_notifier(&kprobe_module_nb);
+#endif
 
 	kprobes_initialized = (err == 0);
 	kprobe_sysctls_init();
diff --git a/kernel/trace/trace_kprobe.c b/kernel/trace/trace_kprobe.c
index c4c6e0e0068b..dd4598f775b9 100644
--- a/kernel/trace/trace_kprobe.c
+++ b/kernel/trace/trace_kprobe.c
@@ -102,6 +102,7 @@  static nokprobe_inline bool trace_kprobe_has_gone(struct trace_kprobe *tk)
 	return kprobe_gone(&tk->rp.kp);
 }
 
+#if IS_ENABLED(CONFIG_MODULES)
 static nokprobe_inline bool trace_kprobe_within_module(struct trace_kprobe *tk,
 						 struct module *mod)
 {
@@ -129,6 +130,12 @@  static nokprobe_inline bool trace_kprobe_module_exist(struct trace_kprobe *tk)
 
 	return ret;
 }
+#else
+static nokprobe_inline bool trace_kprobe_module_exist(struct trace_kprobe *tk)
+{
+	return true;
+}
+#endif
 
 static bool trace_kprobe_is_busy(struct dyn_event *ev)
 {
@@ -670,6 +677,7 @@  static int register_trace_kprobe(struct trace_kprobe *tk)
 	return ret;
 }
 
+#if IS_ENABLED(CONFIG_MODULES)
 /* Module notifier call back, checking event on the module */
 static int trace_kprobe_module_callback(struct notifier_block *nb,
 				       unsigned long val, void *data)
@@ -704,6 +712,7 @@  static struct notifier_block trace_kprobe_module_nb = {
 	.notifier_call = trace_kprobe_module_callback,
 	.priority = 1	/* Invoked after kprobe module callback */
 };
+#endif /* IS_ENABLED(CONFIG_MODULES) */
 
 static int count_symbols(void *data, unsigned long unused)
 {
@@ -1897,8 +1906,10 @@  static __init int init_kprobe_trace_early(void)
 	if (ret)
 		return ret;
 
+#if IS_ENABLED(CONFIG_MODULES)
 	if (register_module_notifier(&trace_kprobe_module_nb))
 		return -EINVAL;
+#endif
 
 	return 0;
 }