linux-modules.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Aaron Tomlin <atomlin@redhat.com>
To: mcgrof@kernel.org, christophe.leroy@csgroup.eu
Cc: cl@linux.com, mbenes@suse.cz, akpm@linux-foundation.org,
	jeyu@kernel.org, linux-kernel@vger.kernel.org,
	linux-modules@vger.kernel.org, void@manifault.com,
	atomlin@atomlin.com, allen.lkml@gmail.com, joe@perches.com,
	msuchanek@suse.de, oleksandr@natalenko.name,
	jason.wessel@windriver.com, pmladek@suse.com,
	daniel.thompson@linaro.org, hch@infradead.org
Subject: [PATCH v11 10/14] module: kallsyms: Fix suspicious rcu usage
Date: Thu, 10 Mar 2022 10:24:09 +0000	[thread overview]
Message-ID: <20220310102413.3438665-11-atomlin@redhat.com> (raw)
In-Reply-To: <20220310102413.3438665-1-atomlin@redhat.com>

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(-)

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];
 
-- 
2.34.1


  parent reply	other threads:[~2022-03-10 10:25 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-10 10:23 [PATCH v11 00/14] module: core code clean up Aaron Tomlin
2022-03-10 10:24 ` [PATCH v11 01/14] module: Move all into module/ Aaron Tomlin
2022-03-10 10:24 ` [PATCH v11 02/14] module: Simple refactor in preparation for split Aaron Tomlin
2022-03-10 10:24 ` [PATCH v11 03/14] module: Make internal.h and decompress.c more compliant Aaron Tomlin
2022-03-10 10:24 ` [PATCH v11 04/14] module: Move livepatch support to a separate file Aaron Tomlin
2022-03-10 10:24 ` [PATCH v11 05/14] module: Move latched RB-tree " Aaron Tomlin
2022-03-10 10:24 ` [PATCH v11 06/14] module: Move strict rwx " Aaron Tomlin
2022-03-10 10:24 ` [PATCH v11 07/14] module: Move extra signature support out of core code Aaron Tomlin
2022-03-10 10:24 ` [PATCH v11 08/14] module: Move kmemleak support to a separate file Aaron Tomlin
2022-03-10 10:24 ` [PATCH v11 09/14] module: Move kallsyms support into " Aaron Tomlin
2022-06-28  4:05   ` Steven Rostedt
2022-06-28  8:19     ` Aaron Tomlin
2022-07-01 22:34       ` Luis Chamberlain
2022-07-03  8:33         ` Aaron Tomlin
2022-07-03 13:23           ` Steven Rostedt
2022-07-03 13:57             ` Aaron Tomlin
2022-07-03 14:13               ` Steven Rostedt
2022-07-03 14:24                 ` Aaron Tomlin
2022-07-03 17:58                 ` Christophe Leroy
2022-07-03 23:18                   ` Steven Rostedt
2022-03-10 10:24 ` Aaron Tomlin [this message]
2022-06-28  3:56   ` [PATCH v11 10/14] module: kallsyms: Fix suspicious rcu usage Steven Rostedt
2022-06-28 16:21     ` Aaron Tomlin
2022-06-28 16:24       ` Steven Rostedt
2022-03-10 10:24 ` [PATCH v11 11/14] module: Move procfs support into a separate file Aaron Tomlin
2022-03-10 10:26 ` [PATCH v11 12/14] module: Move sysfs " Aaron Tomlin
2022-03-10 10:26   ` [PATCH v11 13/14] module: Move kdb module related code out of main kdb code Aaron Tomlin
2022-03-10 10:30     ` Aaron Tomlin
2022-03-11 16:17     ` Daniel Thompson
2022-03-11 18:00       ` Luis Chamberlain
2022-03-10 10:26   ` [PATCH v11 14/14] module: Move version support into a separate file Aaron Tomlin
2022-03-10 10:32     ` Aaron Tomlin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220310102413.3438665-11-atomlin@redhat.com \
    --to=atomlin@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=allen.lkml@gmail.com \
    --cc=atomlin@atomlin.com \
    --cc=christophe.leroy@csgroup.eu \
    --cc=cl@linux.com \
    --cc=daniel.thompson@linaro.org \
    --cc=hch@infradead.org \
    --cc=jason.wessel@windriver.com \
    --cc=jeyu@kernel.org \
    --cc=joe@perches.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-modules@vger.kernel.org \
    --cc=mbenes@suse.cz \
    --cc=mcgrof@kernel.org \
    --cc=msuchanek@suse.de \
    --cc=oleksandr@natalenko.name \
    --cc=pmladek@suse.com \
    --cc=void@manifault.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).