mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Linus Torvalds <torvalds@linux-foundation.org>
To: Rusty Russell <rusty@rustcorp.com.au>
Cc: Brandon Philips <brandon@ifup.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	"Rafael J. Wysocki" <rjw@sisk.pl>,
	LKML <linux-kernel@vger.kernel.org>,
	Jon Masters <jonathan@jonmasters.org>,
	Tejun Heo <htejun@gmail.com>,
	Masami Hiramatsu <mhiramat@redhat.com>,
	Kay Sievers <kay.sievers@vrfy.org>
Subject: Re: [PATCH 2/2] module: fix bne2 "gave up waiting for init of module libcrc32c"
Date: Wed, 2 Jun 2010 01:12:32 -0700 (PDT)	[thread overview]
Message-ID: <alpine.LFD.2.00.1006020102520.8175@i5.linux-foundation.org> (raw)
In-Reply-To: <alpine.LFD.2.00.1006020022000.8175@i5.linux-foundation.org>



On Wed, 2 Jun 2010, Linus Torvalds wrote:
> 
> IOW, things like this.. Pure code movement to peel off some stuff.

Here's a second one. It's slightly less trivial - since we now have error 
cases - and equally untested so it may well be totally broken. But it also 
cleans up a bit more, and avoids one of the goto targets, because the 
"move_module()" helper now does both allocations or none.

But now I'm bored and tired, so I think this will be all for tonight. The 
load_module function has gone from ~490 lines to ~370 lines, but that's 
still so many that I don't honestly think this makes any readability 
improvement yet.

But add a few more helper functions (say, one for doing relocations, one 
for doing some of the verification, one for doing the license and 
tainting, one to trivially push the icache flush into its own helper etc), 
and it probably will never fit in 50 lines, but maybe it could be 150.

			Linus

---
 kernel/module.c |  119 ++++++++++++++++++++++++++++++-------------------------
 1 files changed, 65 insertions(+), 54 deletions(-)

diff --git a/kernel/module.c b/kernel/module.c
index 165d97e..72567e6 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -2170,6 +2170,67 @@ static void find_module_sections(struct module *mod, Elf_Ehdr *hdr, Elf_Shdr *se
 #endif
 }
 
+static struct module *move_module(struct module *mod, Elf_Ehdr *hdr, Elf_Shdr *sechdrs, char *secstrings, unsigned modindex)
+{
+	int i;
+	void *ptr;
+
+	/* Do the allocs. */
+	ptr = module_alloc_update_bounds(mod->core_size);
+	/*
+	 * The pointer to this block is stored in the module structure
+	 * which is inside the block. Just mark it as not being a
+	 * leak.
+	 */
+	kmemleak_not_leak(ptr);
+	if (!ptr)
+		return ERR_PTR(-ENOMEM);
+
+	memset(ptr, 0, mod->core_size);
+	mod->module_core = ptr;
+
+	ptr = module_alloc_update_bounds(mod->init_size);
+	/*
+	 * The pointer to this block is stored in the module structure
+	 * which is inside the block. This block doesn't need to be
+	 * scanned as it contains data and code that will be freed
+	 * after the module is initialized.
+	 */
+	kmemleak_ignore(ptr);
+	if (!ptr && mod->init_size) {
+		module_free(mod, mod->module_core);
+		return ERR_PTR(-ENOMEM);
+	}
+	memset(ptr, 0, mod->init_size);
+	mod->module_init = ptr;
+
+	/* Transfer each section which specifies SHF_ALLOC */
+	DEBUGP("final section addresses:\n");
+	for (i = 0; i < hdr->e_shnum; i++) {
+		void *dest;
+
+		if (!(sechdrs[i].sh_flags & SHF_ALLOC))
+			continue;
+
+		if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
+			dest = mod->module_init
+				+ (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
+		else
+			dest = mod->module_core + sechdrs[i].sh_entsize;
+
+		if (sechdrs[i].sh_type != SHT_NOBITS)
+			memcpy(dest, (void *)sechdrs[i].sh_addr,
+			       sechdrs[i].sh_size);
+		/* Update sh_addr to point to copy in image. */
+		sechdrs[i].sh_addr = (unsigned long)dest;
+		DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
+	}
+	/* Module has been moved. */
+	mod = (void *)sechdrs[modindex].sh_addr;
+	kmemleak_load_module(mod, hdr, sechdrs, secstrings);
+	return mod;
+}
+
 /* Allocate and load the module: note that size of section 0 is always
    zero, and we rely on this for optional sections. */
 static noinline struct module *load_module(void __user *umod,
@@ -2186,7 +2247,6 @@ static noinline struct module *load_module(void __user *umod,
 	unsigned int modindex, versindex, infoindex, pcpuindex;
 	struct module *mod;
 	long err = 0;
-	void *ptr = NULL; /* Stops spurious gcc warning */
 	unsigned long symoffs, stroffs, *strmap;
 	void __percpu *percpu;
 
@@ -2338,60 +2398,12 @@ static noinline struct module *load_module(void __user *umod,
 	symoffs = layout_symtab(mod, sechdrs, symindex, strindex, hdr,
 				secstrings, &stroffs, strmap);
 
-	/* Do the allocs. */
-	ptr = module_alloc_update_bounds(mod->core_size);
-	/*
-	 * The pointer to this block is stored in the module structure
-	 * which is inside the block. Just mark it as not being a
-	 * leak.
-	 */
-	kmemleak_not_leak(ptr);
-	if (!ptr) {
-		err = -ENOMEM;
+	/* Allocate and move to the final place */
+	mod = move_module(mod, hdr, sechdrs, secstrings, modindex);
+	if (IS_ERR(mod)) {
+		err = PTR_ERR(mod);
 		goto free_percpu;
 	}
-	memset(ptr, 0, mod->core_size);
-	mod->module_core = ptr;
-
-	ptr = module_alloc_update_bounds(mod->init_size);
-	/*
-	 * The pointer to this block is stored in the module structure
-	 * which is inside the block. This block doesn't need to be
-	 * scanned as it contains data and code that will be freed
-	 * after the module is initialized.
-	 */
-	kmemleak_ignore(ptr);
-	if (!ptr && mod->init_size) {
-		err = -ENOMEM;
-		goto free_core;
-	}
-	memset(ptr, 0, mod->init_size);
-	mod->module_init = ptr;
-
-	/* Transfer each section which specifies SHF_ALLOC */
-	DEBUGP("final section addresses:\n");
-	for (i = 0; i < hdr->e_shnum; i++) {
-		void *dest;
-
-		if (!(sechdrs[i].sh_flags & SHF_ALLOC))
-			continue;
-
-		if (sechdrs[i].sh_entsize & INIT_OFFSET_MASK)
-			dest = mod->module_init
-				+ (sechdrs[i].sh_entsize & ~INIT_OFFSET_MASK);
-		else
-			dest = mod->module_core + sechdrs[i].sh_entsize;
-
-		if (sechdrs[i].sh_type != SHT_NOBITS)
-			memcpy(dest, (void *)sechdrs[i].sh_addr,
-			       sechdrs[i].sh_size);
-		/* Update sh_addr to point to copy in image. */
-		sechdrs[i].sh_addr = (unsigned long)dest;
-		DEBUGP("\t0x%lx %s\n", sechdrs[i].sh_addr, secstrings + sechdrs[i].sh_name);
-	}
-	/* Module has been moved. */
-	mod = (void *)sechdrs[modindex].sh_addr;
-	kmemleak_load_module(mod, hdr, sechdrs, secstrings);
 
 #if defined(CONFIG_MODULE_UNLOAD)
 	mod->refptr = alloc_percpu(struct module_ref);
@@ -2577,7 +2589,6 @@ static noinline struct module *load_module(void __user *umod,
  free_init:
 #endif
 	module_free(mod, mod->module_init);
- free_core:
 	module_free(mod, mod->module_core);
 	/* mod will be freed with core. Don't access it beyond this line! */
  free_percpu:

  reply	other threads:[~2010-06-02  8:17 UTC|newest]

Thread overview: 71+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-05-25 21:00 [Regression] Crash in load_module() while freeing args Rafael J. Wysocki
2010-05-25 22:54 ` Rafael J. Wysocki
2010-05-25 23:47   ` Linus Torvalds
2010-05-26  8:00     ` Rusty Russell
2010-05-26 11:57       ` Rusty Russell
2010-05-26 22:56         ` Rafael J. Wysocki
2010-05-26 23:07           ` Linus Torvalds
2010-05-27  5:26           ` Rusty Russell
2010-05-27 18:46             ` Brandon Philips
2010-05-31  9:40               ` Rusty Russell
2010-05-31 12:00                 ` [PATCH 0/2] kernel/module.c locking changes Rusty Russell
2010-05-31 12:01                   ` [PATCH 1/2] module: make locking more fine-grained Rusty Russell
2010-05-31 12:02                     ` [PATCH 2/2] module: fix bne2 "gave up waiting for init of module libcrc32c" Rusty Russell
2010-05-31 16:48                       ` Andrew Morton
2010-05-31 18:19                         ` Linus Torvalds
2010-05-31 20:15                           ` Linus Torvalds
2010-05-31 20:16                             ` [PATCH 1/2] Make the module 'usage' lists be two-way Linus Torvalds
2010-05-31 20:17                               ` [PATCH 2/2] module: wait for other modules after dropping the module_mutex Linus Torvalds
2010-06-01  1:37                               ` [PATCH 1/2] Make the module 'usage' lists be two-way Rusty Russell
2010-06-01  3:42                                 ` Rusty Russell
2010-06-01  4:00                                   ` Linus Torvalds
2010-06-01  4:05                                     ` Linus Torvalds
2010-06-01  2:44                               ` Américo Wang
2010-06-01  3:51                                 ` Linus Torvalds
2010-06-01  1:57                             ` [PATCH 2/2] module: fix bne2 "gave up waiting for init of module libcrc32c" Rusty Russell
2010-06-01  3:40                               ` Linus Torvalds
2010-06-01  4:27                                 ` Linus Torvalds
2010-06-01  5:19                                 ` Rusty Russell
2010-06-02  3:15                                   ` Rusty Russell
2010-06-01  1:21                           ` Rusty Russell
2010-06-01  3:24                             ` Linus Torvalds
2010-06-01  5:22                               ` Rusty Russell
2010-06-01 14:58                                 ` Linus Torvalds
2010-06-01 17:53                                   ` Linus Torvalds
2010-06-01 23:24                                     ` Brandon Philips
2010-06-01 23:51                                       ` Linus Torvalds
2010-06-02  2:10                                         ` Brandon Philips
2010-06-02  3:03                                           ` Rusty Russell
2010-06-02  4:35                                           ` Linus Torvalds
2010-06-02  4:44                                             ` Linus Torvalds
2010-06-02  6:35                                               ` Rusty Russell
2010-06-02  7:45                                                 ` Linus Torvalds
2010-06-02  8:12                                                   ` Linus Torvalds [this message]
2010-06-02  9:07                                                     ` Rusty Russell
2010-06-02  5:52                                             ` Rusty Russell
2010-06-02  7:21                                               ` Linus Torvalds
2010-06-02 14:06                                                 ` Rusty Russell
2010-06-02 14:50                                                   ` Linus Torvalds
2010-06-03 13:06                                                     ` Rusty Russell
2010-06-02 16:53                                                   ` Brandon Philips
2010-06-02 18:01                                                   ` Linus Torvalds
2010-06-03  5:20                                                     ` Rusty Russell
2010-06-03 16:24                                                       ` Linus Torvalds
2010-06-04  1:02                                                         ` Rusty Russell
2010-06-04  1:55                                                           ` Linus Torvalds
2010-06-04  5:20                                                             ` Rusty Russell
2010-06-04 22:48                                                               ` Linus Torvalds
2010-06-05  1:49                                                                 ` Rusty Russell
2010-06-02  3:09                                   ` Rusty Russell
2010-06-02  4:32                                     ` Linus Torvalds
2010-06-02  4:56                                     ` Linus Torvalds
2010-06-02  5:52                                       ` Rusty Russell
2010-06-02  6:59                                         ` Linus Torvalds
2010-06-01  1:04                         ` Rusty Russell
2010-06-01  5:38                     ` [PATCH 1/2] module: make locking more fine-grained Américo Wang
2010-06-01  5:55                       ` Rusty Russell
2010-05-27 21:57             ` [Regression] Crash in load_module() while freeing args Rafael J. Wysocki
2010-05-31  7:54               ` Rusty Russell
2010-05-31 10:23               ` [PATCH] module: fix reference to mod->percpu after freeing module Rusty Russell
2010-05-31 10:25                 ` Tejun Heo
2010-05-26 15:41       ` [Regression] Crash in load_module() while freeing args Linus Torvalds

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=alpine.LFD.2.00.1006020102520.8175@i5.linux-foundation.org \
    --to=torvalds@linux-foundation.org \
    --cc=akpm@linux-foundation.org \
    --cc=brandon@ifup.org \
    --cc=htejun@gmail.com \
    --cc=jonathan@jonmasters.org \
    --cc=kay.sievers@vrfy.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mhiramat@redhat.com \
    --cc=rjw@sisk.pl \
    --cc=rusty@rustcorp.com.au \
    /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

all inboxes | Powered by JetHome®