diff mbox series

[v11,10/14] module: kallsyms: Fix suspicious rcu usage

Message ID 20220310102413.3438665-11-atomlin@redhat.com (mailing list archive)
State New, archived
Headers show
Series module: core code clean up | expand

Commit Message

Aaron Tomlin March 10, 2022, 10:24 a.m. UTC
No functional change.

The purpose of this patch is to address the various Sparse warnings
due to the incorrect dereference/or access of an __rcu pointer.

Signed-off-by: Aaron Tomlin <atomlin@redhat.com>
---
 kernel/module/kallsyms.c | 34 ++++++++++++++++++++++------------
 1 file changed, 22 insertions(+), 12 deletions(-)

Comments

Steven Rostedt June 28, 2022, 3:56 a.m. UTC | #1
On Thu, 10 Mar 2022 10:24:09 +0000
Aaron Tomlin <atomlin@redhat.com> wrote:

> No functional change.

There most definitely is a functional change here.

> 
> The purpose of this patch is to address the various Sparse warnings
> due to the incorrect dereference/or access of an __rcu pointer.
> 
> Signed-off-by: Aaron Tomlin <atomlin@redhat.com>
> ---
>  kernel/module/kallsyms.c | 34 ++++++++++++++++++++++------------
>  1 file changed, 22 insertions(+), 12 deletions(-)
> 
> diff --git a/kernel/module/kallsyms.c b/kernel/module/kallsyms.c
> index 1b0780e20aab..a3da0686a2a6 100644
> --- a/kernel/module/kallsyms.c
> +++ b/kernel/module/kallsyms.c
> @@ -171,14 +171,17 @@ void add_kallsyms(struct module *mod, const struct load_info *info)
>  	Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
>  
>  	/* Set up to point into init section. */
> -	mod->kallsyms = mod->init_layout.base + info->mod_kallsyms_init_off;
> +	mod->kallsyms = (void __rcu *)mod->init_layout.base +
> +		info->mod_kallsyms_init_off;
>  
> +	preempt_disable();

        ^^^^^^^^^^^^^^^^^^


>  	/* The following is safe since this pointer cannot change */
> -	mod->kallsyms->symtab = (void *)symsec->sh_addr;
> -	mod->kallsyms->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
> +	rcu_dereference_sched(mod->kallsyms)->symtab = (void *)symsec->sh_addr;
> +	rcu_dereference_sched(mod->kallsyms)->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
>  	/* Make sure we get permanent strtab: don't use info->strtab. */
> -	mod->kallsyms->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
> -	mod->kallsyms->typetab = mod->init_layout.base + info->init_typeoffs;
> +	rcu_dereference_sched(mod->kallsyms)->strtab =
> +		(void *)info->sechdrs[info->index.str].sh_addr;
> +	rcu_dereference_sched(mod->kallsyms)->typetab = mod->init_layout.base + info->init_typeoffs;
>  
>  	/*
>  	 * Now populate the cut down core kallsyms for after init
> @@ -187,20 +190,22 @@ void add_kallsyms(struct module *mod, const struct load_info *info)
>  	mod->core_kallsyms.symtab = dst = mod->core_layout.base + info->symoffs;
>  	mod->core_kallsyms.strtab = s = mod->core_layout.base + info->stroffs;
>  	mod->core_kallsyms.typetab = mod->core_layout.base + info->core_typeoffs;
> -	src = mod->kallsyms->symtab;
> -	for (ndst = i = 0; i < mod->kallsyms->num_symtab; i++) {
> -		mod->kallsyms->typetab[i] = elf_type(src + i, info);
> +	src = rcu_dereference_sched(mod->kallsyms)->symtab;
> +	for (ndst = i = 0; i < rcu_dereference_sched(mod->kallsyms)->num_symtab; i++) {

How big is this loop?

> +		rcu_dereference_sched(mod->kallsyms)->typetab[i] = elf_type(src + i, info);
>  		if (i == 0 || is_livepatch_module(mod) ||
>  		    is_core_symbol(src + i, info->sechdrs, info->hdr->e_shnum,
>  				   info->index.pcpu)) {
>  			mod->core_kallsyms.typetab[ndst] =
> -			    mod->kallsyms->typetab[i];
> +			    rcu_dereference_sched(mod->kallsyms)->typetab[i];
>  			dst[ndst] = src[i];
>  			dst[ndst++].st_name = s - mod->core_kallsyms.strtab;
> -			s += strscpy(s, &mod->kallsyms->strtab[src[i].st_name],
> +			s += strscpy(s,
> +				     &rcu_dereference_sched(mod->kallsyms)->strtab[src[i].st_name],
>  				     KSYM_NAME_LEN) + 1;
>  		}
>  	}
> +	preempt_enable();

        ^^^^^^^^^^^^^^^^^

This just introduced an unbounded latency and will fail PREEMPT_RT.

-- Steve

>  	mod->core_kallsyms.num_symtab = ndst;
>  }
>  
> @@ -478,11 +483,16 @@ int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
>  
>  	mutex_lock(&module_mutex);
>  	list_for_each_entry(mod, &modules, list) {
> -		/* We hold module_mutex: no need for rcu_dereference_sched */
> -		struct mod_kallsyms *kallsyms = mod->kallsyms;
> +		struct mod_kallsyms *kallsyms;
>  
>  		if (mod->state == MODULE_STATE_UNFORMED)
>  			continue;
> +
> +		/* Use rcu_dereference_sched() to remain compliant with the sparse tool */
> +		preempt_disable();
> +		kallsyms = rcu_dereference_sched(mod->kallsyms);
> +		preempt_enable();
> +
>  		for (i = 0; i < kallsyms->num_symtab; i++) {
>  			const Elf_Sym *sym = &kallsyms->symtab[i];
>
Aaron Tomlin June 28, 2022, 4:21 p.m. UTC | #2
On Mon 2022-06-27 23:56 -0400, Steven Rostedt wrote:
> > +	preempt_enable();
> 
>         ^^^^^^^^^^^^^^^^^
> 
> This just introduced an unbounded latency and will fail PREEMPT_RT.
> 

Hi Steve,

Sorry about that!

If I understand correctly, there is absolutely no need to explicitly
disable preemption in this context. I will use: rcu_read_lock() and
rcu_read_unlock(), respectively, which will then be suitable for the
preemptible RCU implementation.


Kind regards,
Steven Rostedt June 28, 2022, 4:24 p.m. UTC | #3
On Tue, 28 Jun 2022 17:21:22 +0100
Aaron Tomlin <atomlin@redhat.com> wrote:

> If I understand correctly, there is absolutely no need to explicitly
> disable preemption in this context. I will use: rcu_read_lock() and
> rcu_read_unlock(), respectively, which will then be suitable for the
> preemptible RCU implementation.

Yes, thank you very much :-)

-- Steve
diff mbox series

Patch

diff --git a/kernel/module/kallsyms.c b/kernel/module/kallsyms.c
index 1b0780e20aab..a3da0686a2a6 100644
--- a/kernel/module/kallsyms.c
+++ b/kernel/module/kallsyms.c
@@ -171,14 +171,17 @@  void add_kallsyms(struct module *mod, const struct load_info *info)
 	Elf_Shdr *symsec = &info->sechdrs[info->index.sym];
 
 	/* Set up to point into init section. */
-	mod->kallsyms = mod->init_layout.base + info->mod_kallsyms_init_off;
+	mod->kallsyms = (void __rcu *)mod->init_layout.base +
+		info->mod_kallsyms_init_off;
 
+	preempt_disable();
 	/* The following is safe since this pointer cannot change */
-	mod->kallsyms->symtab = (void *)symsec->sh_addr;
-	mod->kallsyms->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
+	rcu_dereference_sched(mod->kallsyms)->symtab = (void *)symsec->sh_addr;
+	rcu_dereference_sched(mod->kallsyms)->num_symtab = symsec->sh_size / sizeof(Elf_Sym);
 	/* Make sure we get permanent strtab: don't use info->strtab. */
-	mod->kallsyms->strtab = (void *)info->sechdrs[info->index.str].sh_addr;
-	mod->kallsyms->typetab = mod->init_layout.base + info->init_typeoffs;
+	rcu_dereference_sched(mod->kallsyms)->strtab =
+		(void *)info->sechdrs[info->index.str].sh_addr;
+	rcu_dereference_sched(mod->kallsyms)->typetab = mod->init_layout.base + info->init_typeoffs;
 
 	/*
 	 * Now populate the cut down core kallsyms for after init
@@ -187,20 +190,22 @@  void add_kallsyms(struct module *mod, const struct load_info *info)
 	mod->core_kallsyms.symtab = dst = mod->core_layout.base + info->symoffs;
 	mod->core_kallsyms.strtab = s = mod->core_layout.base + info->stroffs;
 	mod->core_kallsyms.typetab = mod->core_layout.base + info->core_typeoffs;
-	src = mod->kallsyms->symtab;
-	for (ndst = i = 0; i < mod->kallsyms->num_symtab; i++) {
-		mod->kallsyms->typetab[i] = elf_type(src + i, info);
+	src = rcu_dereference_sched(mod->kallsyms)->symtab;
+	for (ndst = i = 0; i < rcu_dereference_sched(mod->kallsyms)->num_symtab; i++) {
+		rcu_dereference_sched(mod->kallsyms)->typetab[i] = elf_type(src + i, info);
 		if (i == 0 || is_livepatch_module(mod) ||
 		    is_core_symbol(src + i, info->sechdrs, info->hdr->e_shnum,
 				   info->index.pcpu)) {
 			mod->core_kallsyms.typetab[ndst] =
-			    mod->kallsyms->typetab[i];
+			    rcu_dereference_sched(mod->kallsyms)->typetab[i];
 			dst[ndst] = src[i];
 			dst[ndst++].st_name = s - mod->core_kallsyms.strtab;
-			s += strscpy(s, &mod->kallsyms->strtab[src[i].st_name],
+			s += strscpy(s,
+				     &rcu_dereference_sched(mod->kallsyms)->strtab[src[i].st_name],
 				     KSYM_NAME_LEN) + 1;
 		}
 	}
+	preempt_enable();
 	mod->core_kallsyms.num_symtab = ndst;
 }
 
@@ -478,11 +483,16 @@  int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *,
 
 	mutex_lock(&module_mutex);
 	list_for_each_entry(mod, &modules, list) {
-		/* We hold module_mutex: no need for rcu_dereference_sched */
-		struct mod_kallsyms *kallsyms = mod->kallsyms;
+		struct mod_kallsyms *kallsyms;
 
 		if (mod->state == MODULE_STATE_UNFORMED)
 			continue;
+
+		/* Use rcu_dereference_sched() to remain compliant with the sparse tool */
+		preempt_disable();
+		kallsyms = rcu_dereference_sched(mod->kallsyms);
+		preempt_enable();
+
 		for (i = 0; i < kallsyms->num_symtab; i++) {
 			const Elf_Sym *sym = &kallsyms->symtab[i];