From patchwork Sat Mar 11 05:17:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luis Chamberlain X-Patchwork-Id: 13170626 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6C58FC61DA4 for ; Sat, 11 Mar 2023 05:17:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229928AbjCKFRf (ORCPT ); Sat, 11 Mar 2023 00:17:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46736 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229764AbjCKFRc (ORCPT ); Sat, 11 Mar 2023 00:17:32 -0500 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 788F8140520; Fri, 10 Mar 2023 21:17:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=fgKIaEVFe3ZJ+AptWb/si2HEmzuXwtpAjqPKXFiEzog=; b=mc+03uC4v46xoHgDKwIy/jRxpa VXDyh/U/I4ik/2yepzo5eX4By8jC4lxxcVTLAhquivmKGobv0wOcjEMdwLCJM4WEpcWrqeOY7O13j h27DatHPMaYlZfMXrvuJSzcxKYo7uZlT3ax0I9pEJHkPwhWfaqgzKbPmVyaaquBxpVeI0b3iDMC0O 5tGKhUeCbVGJo+jnK8WdTpCECyDy4PgEd5SSLzlDyORHXX9wcO4QJt9APOwDWcOcJojq8AAHB4Hu8 b2he0Fi7jHmXy1uFF6m2sXhSDkGD7oDLrLFewiwKHerM3g7bWGdM1vOx8ZklpVcfnbHt+k+uCnZuj K3/injQA==; Received: from mcgrof by bombadil.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1parbj-00HBMo-1Z; Sat, 11 Mar 2023 05:17:27 +0000 From: Luis Chamberlain To: linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, pmladek@suse.com, david@redhat.com, petr.pavlu@suse.com, prarit@redhat.com Cc: christophe.leroy@csgroup.eu, song@kernel.org, mcgrof@kernel.org, torvalds@linux-foundation.org Subject: [RFC 01/12] module: use goto errors on check_modinfo() and layout_and_allocate() Date: Fri, 10 Mar 2023 21:17:01 -0800 Message-Id: <20230311051712.4095040-2-mcgrof@kernel.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20230311051712.4095040-1-mcgrof@kernel.org> References: <20230311051712.4095040-1-mcgrof@kernel.org> MIME-Version: 1.0 Sender: Luis Chamberlain Precedence: bulk List-ID: Although both routines don't have much complex errors paths we will expand on these routine in the future, setting up error lables now for both will make subsequent changes easier to review. This changes has no functional changes. Signed-off-by: Luis Chamberlain --- kernel/module/main.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index b4759f1695b7..5f1473eb34e0 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1998,7 +1998,7 @@ static int check_modinfo(struct module *mod, struct load_info *info, int flags) err = check_modinfo_livepatch(mod, info); if (err) - return err; + goto err_out; /* Set up license info based on the info section */ set_license(mod, get_modinfo(info, "license")); @@ -2011,6 +2011,8 @@ static int check_modinfo(struct module *mod, struct load_info *info, int flags) } return 0; +err_out: + return err; } static int find_module_sections(struct module *mod, struct load_info *info) @@ -2288,12 +2290,12 @@ static struct module *layout_and_allocate(struct load_info *info, int flags) err = module_frob_arch_sections(info->hdr, info->sechdrs, info->secstrings, info->mod); if (err < 0) - return ERR_PTR(err); + goto err_out; err = module_enforce_rwx_sections(info->hdr, info->sechdrs, info->secstrings, info->mod); if (err < 0) - return ERR_PTR(err); + goto err_out; /* We will do a special allocation for per-cpu sections later. */ info->sechdrs[info->index.pcpu].sh_flags &= ~(unsigned long)SHF_ALLOC; @@ -2327,12 +2329,14 @@ static struct module *layout_and_allocate(struct load_info *info, int flags) /* Allocate and move to the final place */ err = move_module(info->mod, info); if (err) - return ERR_PTR(err); + goto err_out; /* Module has been copied to its final place now: return it. */ mod = (void *)info->sechdrs[info->index.mod].sh_addr; kmemleak_load_module(mod, info); return mod; +err_out: + return ERR_PTR(err); } /* mod is no longer valid after this! */ From patchwork Sat Mar 11 05:17:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luis Chamberlain X-Patchwork-Id: 13170628 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D7B02C6FD1C for ; Sat, 11 Mar 2023 05:17:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229984AbjCKFRi (ORCPT ); Sat, 11 Mar 2023 00:17:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46738 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229817AbjCKFRc (ORCPT ); Sat, 11 Mar 2023 00:17:32 -0500 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 78A2E140526; Fri, 10 Mar 2023 21:17:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=Qx3kvl1TA4Gbwo8RtYmlV05hbgAF2Z4QTvWxKXzU3KI=; b=Lx0vzaE2gzcfyPXU87+uHWfN+Y 90v1y+KP0yZoxipqzRWBcWtgoRuSG83wO1BXpUk1NK/kBtsO0laf1BZabvPqgtVOf3RMAPWe2R+3S ihr/nhf+EDH14m4AnAY8ODpFxgmxMi3hl8dYTSTGXXZphiVH3PrFzA3Dc62GK1WCF/goMx6N8cjOX 9qs2v8ReG3OG7mma51jBbkP99UJ+wGadbYmhhv0vkCmpaQ26vjH4w8/IAUi7eR58yADoCIgCwHfrW +66lnZSBe+u3N7fn5MY1wjGUbsuknXjgRa5fpJ/ytxd3lPEePTHhuHwX7xW9T9SDVxkEVFbyV879u 1EeSfW5g==; Received: from mcgrof by bombadil.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1parbj-00HBMq-37; Sat, 11 Mar 2023 05:17:27 +0000 From: Luis Chamberlain To: linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, pmladek@suse.com, david@redhat.com, petr.pavlu@suse.com, prarit@redhat.com Cc: christophe.leroy@csgroup.eu, song@kernel.org, mcgrof@kernel.org, torvalds@linux-foundation.org Subject: [RFC 02/12] module: move get_modinfo() helpers all above Date: Fri, 10 Mar 2023 21:17:02 -0800 Message-Id: <20230311051712.4095040-3-mcgrof@kernel.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20230311051712.4095040-1-mcgrof@kernel.org> References: <20230311051712.4095040-1-mcgrof@kernel.org> MIME-Version: 1.0 Sender: Luis Chamberlain Precedence: bulk List-ID: Instead of forward declaring routines for get_modinfo() just move everything up. This makes no functional changes. Signed-off-by: Luis Chamberlain --- kernel/module/main.c | 100 +++++++++++++++++++++---------------------- 1 file changed, 48 insertions(+), 52 deletions(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index 5f1473eb34e0..6d6e6a6184b5 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1016,9 +1016,55 @@ int try_to_force_load(struct module *mod, const char *reason) #endif } -static char *get_modinfo(const struct load_info *info, const char *tag); +/* Parse tag=value strings from .modinfo section */ +static char *next_string(char *string, unsigned long *secsize) +{ + /* Skip non-zero chars */ + while (string[0]) { + string++; + if ((*secsize)-- <= 1) + return NULL; + } + + /* Skip any zero padding. */ + while (!string[0]) { + string++; + if ((*secsize)-- <= 1) + return NULL; + } + return string; +} + static char *get_next_modinfo(const struct load_info *info, const char *tag, - char *prev); + char *prev) +{ + char *p; + unsigned int taglen = strlen(tag); + Elf_Shdr *infosec = &info->sechdrs[info->index.info]; + unsigned long size = infosec->sh_size; + + /* + * get_modinfo() calls made before rewrite_section_headers() + * must use sh_offset, as sh_addr isn't set! + */ + char *modinfo = (char *)info->hdr + infosec->sh_offset; + + if (prev) { + size -= prev - modinfo; + modinfo = next_string(prev, &size); + } + + for (p = modinfo; p; p = next_string(p, &size)) { + if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=') + return p + taglen + 1; + } + return NULL; +} + +static char *get_modinfo(const struct load_info *info, const char *tag) +{ + return get_next_modinfo(info, tag, NULL); +} static int verify_namespace_is_imported(const struct load_info *info, const struct kernel_symbol *sym, @@ -1544,56 +1590,6 @@ static void set_license(struct module *mod, const char *license) } } -/* Parse tag=value strings from .modinfo section */ -static char *next_string(char *string, unsigned long *secsize) -{ - /* Skip non-zero chars */ - while (string[0]) { - string++; - if ((*secsize)-- <= 1) - return NULL; - } - - /* Skip any zero padding. */ - while (!string[0]) { - string++; - if ((*secsize)-- <= 1) - return NULL; - } - return string; -} - -static char *get_next_modinfo(const struct load_info *info, const char *tag, - char *prev) -{ - char *p; - unsigned int taglen = strlen(tag); - Elf_Shdr *infosec = &info->sechdrs[info->index.info]; - unsigned long size = infosec->sh_size; - - /* - * get_modinfo() calls made before rewrite_section_headers() - * must use sh_offset, as sh_addr isn't set! - */ - char *modinfo = (char *)info->hdr + infosec->sh_offset; - - if (prev) { - size -= prev - modinfo; - modinfo = next_string(prev, &size); - } - - for (p = modinfo; p; p = next_string(p, &size)) { - if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=') - return p + taglen + 1; - } - return NULL; -} - -static char *get_modinfo(const struct load_info *info, const char *tag) -{ - return get_next_modinfo(info, tag, NULL); -} - static void setup_modinfo(struct module *mod, struct load_info *info) { struct module_attribute *attr; From patchwork Sat Mar 11 05:17:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luis Chamberlain X-Patchwork-Id: 13170623 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E7F2FC678D5 for ; Sat, 11 Mar 2023 05:17:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229940AbjCKFRd (ORCPT ); Sat, 11 Mar 2023 00:17:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46724 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229562AbjCKFRc (ORCPT ); Sat, 11 Mar 2023 00:17:32 -0500 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 791E5140530; Fri, 10 Mar 2023 21:17:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=Ulzd+B0GQOyWFcFb7AVNe7XdcgPMcGdt6XRGjoo6VBo=; b=36lENrK/S8KQeyMPt8RDa26cJW e0ZCzwSKXfwQcJX3aQHLJRvNrlkunyHS+BEIj3jLisOjsWQUxn7/RJPW5TkxFAFV/4fBxu6a1KsO+ fl2LpAmwD+Xo2ql/6C4ZDdXVw5b1G+bJgamCh1ARmD1yuyqe8uO/niy3MZYCDJ3MBt8QI8pm+Nl8C QCmj2+IkOSXLYi0dFZSQPEBTvYUe1pTQfFFjXJWEd8397ntsh9LQZwa8INM85ItwblZRIi3APSu4A rTvnkQx7+/EG19UAuFao7c5MxTbXkVYBAivtXjxSV9mgEpq9Tl3S0DgzbGCT0huWAo3Fj+X0I3CnR hXAcAh+g==; Received: from mcgrof by bombadil.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1parbj-00HBMs-4W; Sat, 11 Mar 2023 05:17:27 +0000 From: Luis Chamberlain To: linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, pmladek@suse.com, david@redhat.com, petr.pavlu@suse.com, prarit@redhat.com Cc: christophe.leroy@csgroup.eu, song@kernel.org, mcgrof@kernel.org, torvalds@linux-foundation.org Subject: [RFC 03/12] module: rename next_string() to module_next_tag_pair() Date: Fri, 10 Mar 2023 21:17:03 -0800 Message-Id: <20230311051712.4095040-4-mcgrof@kernel.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20230311051712.4095040-1-mcgrof@kernel.org> References: <20230311051712.4095040-1-mcgrof@kernel.org> MIME-Version: 1.0 Sender: Luis Chamberlain Precedence: bulk List-ID: This makes it clearer what it is doing. While at it, make it available to other code other than main.c. This will be used in the subsequent patch and make the changes easier to read. Signed-off-by: Luis Chamberlain --- kernel/module/internal.h | 2 ++ kernel/module/main.c | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/kernel/module/internal.h b/kernel/module/internal.h index e3883b7d4840..1fa2328636ec 100644 --- a/kernel/module/internal.h +++ b/kernel/module/internal.h @@ -96,6 +96,8 @@ long module_get_offset_and_type(struct module *mod, enum mod_mem_type type, char *module_flags(struct module *mod, char *buf, bool show_state); size_t module_flags_taint(unsigned long taints, char *buf); +char *module_next_tag_pair(char *string, unsigned long *secsize); + static inline void module_assert_mutex_or_preempt(void) { #ifdef CONFIG_LOCKDEP diff --git a/kernel/module/main.c b/kernel/module/main.c index 6d6e6a6184b5..50364707c0cd 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1017,7 +1017,7 @@ int try_to_force_load(struct module *mod, const char *reason) } /* Parse tag=value strings from .modinfo section */ -static char *next_string(char *string, unsigned long *secsize) +char *module_next_tag_pair(char *string, unsigned long *secsize) { /* Skip non-zero chars */ while (string[0]) { @@ -1051,10 +1051,10 @@ static char *get_next_modinfo(const struct load_info *info, const char *tag, if (prev) { size -= prev - modinfo; - modinfo = next_string(prev, &size); + modinfo = module_next_tag_pair(prev, &size); } - for (p = modinfo; p; p = next_string(p, &size)) { + for (p = modinfo; p; p = module_next_tag_pair(p, &size)) { if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=') return p + taglen + 1; } From patchwork Sat Mar 11 05:17:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luis Chamberlain X-Patchwork-Id: 13170629 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 18D76C74A44 for ; Sat, 11 Mar 2023 05:17:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229999AbjCKFRk (ORCPT ); Sat, 11 Mar 2023 00:17:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46742 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229835AbjCKFRc (ORCPT ); Sat, 11 Mar 2023 00:17:32 -0500 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 790A014052F; Fri, 10 Mar 2023 21:17:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=tYsj98dWQ7w+RJ5JNa62BDiT7Z3rKk0MlMBfrIGdxVY=; b=KjPfOGTgevjwLnMl9fuTdtaJLb ftgYM18+ET3KUP9eosnUMPh+6AtxB8M6zo0mtQYUOT88TQbms+gSscSKreWOtqz2j6++2VRhlDlKM L6dterrWwyq0g4OmKpXS/ERbg2iXBjEBZLDUGyK2BJrFA15ME0/f/WiHaVpT9NxjlPL35lZZUjcOB WfLIA7mrOgtIWsep+L8b7MfiJqIJJU681vZi3vub1yKmvMfkKkMjTpv0+khSTuOGD7gNxiQTD1OCd pY2jxjqUY5rg3EIarrHaWUnvA0USxDTE19qFJeMl9/ksZ23Sa/pgf6H5+HpP1xyMR3biD5rLupr9s c2sKkmHg==; Received: from mcgrof by bombadil.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1parbj-00HBMu-5p; Sat, 11 Mar 2023 05:17:27 +0000 From: Luis Chamberlain To: linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, pmladek@suse.com, david@redhat.com, petr.pavlu@suse.com, prarit@redhat.com Cc: christophe.leroy@csgroup.eu, song@kernel.org, mcgrof@kernel.org, torvalds@linux-foundation.org Subject: [RFC 04/12] module: add a for_each_modinfo_entry() Date: Fri, 10 Mar 2023 21:17:04 -0800 Message-Id: <20230311051712.4095040-5-mcgrof@kernel.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20230311051712.4095040-1-mcgrof@kernel.org> References: <20230311051712.4095040-1-mcgrof@kernel.org> MIME-Version: 1.0 Sender: Luis Chamberlain Precedence: bulk List-ID: Add a for_each_modinfo_entry() to make it easier to read and use. This produces no functional changes but makes this code easiert to read as we are used to with loops in the kernel and trims more lines of code. Signed-off-by: Luis Chamberlain --- kernel/module/internal.h | 3 +++ kernel/module/main.c | 5 +---- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/kernel/module/internal.h b/kernel/module/internal.h index 1fa2328636ec..6ae29bb8836f 100644 --- a/kernel/module/internal.h +++ b/kernel/module/internal.h @@ -98,6 +98,9 @@ size_t module_flags_taint(unsigned long taints, char *buf); char *module_next_tag_pair(char *string, unsigned long *secsize); +#define for_each_modinfo_entry(entry, info, name) \ + for (entry = get_modinfo(info, name); entry; entry = get_next_modinfo(info, name, entry)) + static inline void module_assert_mutex_or_preempt(void) { #ifdef CONFIG_LOCKDEP diff --git a/kernel/module/main.c b/kernel/module/main.c index 50364707c0cd..3f7c8634cf06 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1075,12 +1075,9 @@ static int verify_namespace_is_imported(const struct load_info *info, namespace = kernel_symbol_namespace(sym); if (namespace && namespace[0]) { - imported_namespace = get_modinfo(info, "import_ns"); - while (imported_namespace) { + for_each_modinfo_entry(imported_namespace, info, "import_ns") { if (strcmp(namespace, imported_namespace) == 0) return 0; - imported_namespace = get_next_modinfo( - info, "import_ns", imported_namespace); } #ifdef CONFIG_MODULE_ALLOW_MISSING_NAMESPACE_IMPORTS pr_warn( From patchwork Sat Mar 11 05:17:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luis Chamberlain X-Patchwork-Id: 13170634 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 197E7C74A4B for ; Sat, 11 Mar 2023 05:17:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230022AbjCKFRo (ORCPT ); Sat, 11 Mar 2023 00:17:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46798 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229956AbjCKFRe (ORCPT ); Sat, 11 Mar 2023 00:17:34 -0500 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0CA72140504; Fri, 10 Mar 2023 21:17:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=ZY//fx8IeJdbSxcbOUOAnU2iye6f3fGZL3terh5yTNM=; b=kQ0AiR8Rpv7J2YhZneYg7LT6AC Q6NR6JU6hUmVKCsYc7b9PwJsrGYO9wnUBolBzYLbcVwXqXD/zOK58BudkyytWw1anwsYJTXnXpHRw 36xom8gyxgnuHkZcvYIjT9+CqSVM17zeT/OGov9K+6IIhdCDg6g/nWwoiuC2qzoZ27oBNddkDtgHh Y6gztygIaSBYd+50ligy7bN0eiaj30VPO+7Z7IGRRJA/nFsdZbnJQEwBCs+BgVYR+Ga0CE9qNJzFE mkb8RXBq/I/FpZ0rUW5owMarB7kPG6rmnl2vlV/w5IORVpu4uVLCe98ZeQp6JOgU2wOQU9K03YNqc awL0iiig==; Received: from mcgrof by bombadil.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1parbj-00HBMy-7A; Sat, 11 Mar 2023 05:17:27 +0000 From: Luis Chamberlain To: linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, pmladek@suse.com, david@redhat.com, petr.pavlu@suse.com, prarit@redhat.com Cc: christophe.leroy@csgroup.eu, song@kernel.org, mcgrof@kernel.org, torvalds@linux-foundation.org Subject: [RFC 05/12] module: add debugging alias parsing support Date: Fri, 10 Mar 2023 21:17:05 -0800 Message-Id: <20230311051712.4095040-6-mcgrof@kernel.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20230311051712.4095040-1-mcgrof@kernel.org> References: <20230311051712.4095040-1-mcgrof@kernel.org> MIME-Version: 1.0 Sender: Luis Chamberlain Precedence: bulk List-ID: We don't have in-kernel alias parsing support as aliases are all dealt with in userspace. There has simply been no need to have support for processing them in kernel. We have done this under the assumption that userspace just Does The Right Thing (TM) about aliases and loading modules and that there is no real gain of processing aliases in-kernel. Obviously userspace can be buggy though, and it can lie to us. We currently have no easy way to determine this. Parsing aliases is an example debugging facility we can use to help with these sorts of problems. But there are some possible optimizations that may also be possible and enabling support let's folks experiment with these posibilities. We disable this by default but folks can also enable this to experiment with features which may use aliases in-kernel. Folks should not enable this on production kernels. It'll bloat your loaded kernel modules a tiny bit by the size of the aliases that exist for them once loaded. You can debug aliase by adding to your dynamic debug: GRUB_CMDLINE_LINUX_DEFAULT="dyndbg=\"func module_process_aliases +p;\" " Upon boot for example here a few entries: module ext4 num_aliases: 5 alias[0] = fs-ext4 alias[1] = ext3 alias[2] = fs-ext3 alias[3] = ext2 alias[4] = fs-ext2 module xfs num_aliases: 1 alias[0] = fs-xfs module floppy num_aliases: 3 alias[0] = block-major-2-* alias[1] = acpi*:PNP0700:* alias[2] = pnp:dPNP0700* module ata_piix num_aliases: 89 alias[0] = pci:v00008086d00008C81sv*sd*bc*sc*i* alias[1] = pci:v00008086d00008C80sv*sd*bc*sc*i* alias[2] = pci:v00008086d00008C89sv*sd*bc*sc*i* alias[3] = pci:v00008086d00008C88sv*sd*bc*sc*i* ... etc ... Suggested-by: Linus Torvalds Signed-off-by: Luis Chamberlain --- include/linux/module.h | 4 ++ kernel/module/Kconfig | 19 +++++++++ kernel/module/Makefile | 1 + kernel/module/aliases.c | 92 ++++++++++++++++++++++++++++++++++++++++ kernel/module/internal.h | 15 +++++++ kernel/module/main.c | 17 ++++++-- 6 files changed, 145 insertions(+), 3 deletions(-) create mode 100644 kernel/module/aliases.c diff --git a/include/linux/module.h b/include/linux/module.h index c3b357196470..aed1b43edb55 100644 --- a/include/linux/module.h +++ b/include/linux/module.h @@ -420,6 +420,10 @@ struct module { const char *srcversion; struct kobject *holders_dir; +#ifdef CONFIG_MODULE_KERNEL_ALIAS + unsigned int num_aliases; + const char **aliases; +#endif /* Exported symbols */ const struct kernel_symbol *syms; const s32 *crcs; diff --git a/kernel/module/Kconfig b/kernel/module/Kconfig index 424b3bc58f3f..e4ba335fa279 100644 --- a/kernel/module/Kconfig +++ b/kernel/module/Kconfig @@ -22,6 +22,25 @@ menuconfig MODULES if MODULES +config MODULE_DEBUG + bool "Enable debugging information for modules" + default n + help + Enables debugging of the module infrastructure. Say no unless you + are debugging the module framework. Don't enable this on production. + This is only for experimentation and debugging. + +config MODULE_KERNEL_ALIAS + bool "Enable in-kernel alias processing for modules" + default n + depends on MODULE_DEBUG + help + The kernel has historically not processed aliases in-kernel since + we expect userspace can do all the proper work for us. Enable this + if you want to experiment processing aliases in-kernel. This will + bloat your kernel modules's memory by the number of aliases each + module has once loaded into the kernel. + config MODULE_FORCE_LOAD bool "Forced module loading" default n diff --git a/kernel/module/Makefile b/kernel/module/Makefile index 948efea81e85..34f0db0a016b 100644 --- a/kernel/module/Makefile +++ b/kernel/module/Makefile @@ -19,3 +19,4 @@ obj-$(CONFIG_SYSFS) += sysfs.o obj-$(CONFIG_KGDB_KDB) += kdb.o obj-$(CONFIG_MODVERSIONS) += version.o obj-$(CONFIG_MODULE_UNLOAD_TAINT_TRACKING) += tracking.o +obj-$(CONFIG_MODULE_KERNEL_ALIAS) += aliases.o diff --git a/kernel/module/aliases.c b/kernel/module/aliases.c new file mode 100644 index 000000000000..2f30c9d4c765 --- /dev/null +++ b/kernel/module/aliases.c @@ -0,0 +1,92 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Optional module in-kernel alias processing support. + * + * Copyright (C) 2023 Luis Chamberlain + */ + +#include +#include "internal.h" + +void free_mod_aliases(struct module *mod) +{ + unsigned int i; + + if (!mod->num_aliases) + return; + + for (i=0; i < mod->num_aliases; i++) { + kfree(mod->aliases[i]); + mod->aliases[i] = NULL; + } + + kfree(mod->aliases); + mod->aliases = NULL; +} + +static int get_modinfo_tags(struct load_info *info, + const char *tag, + unsigned int *num_entries) +{ + char *p; + unsigned int taglen = strlen(tag); + Elf_Shdr *infosec = &info->sechdrs[info->index.info]; + unsigned long size = infosec->sh_size; + const char *value; + unsigned int len, tags_size = 0; + + for (p = (char *)infosec->sh_addr; p; p = module_next_tag_pair(p, &size)) { + if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=') { + value = p + taglen + 1; + len = strlen(value); + if (len >=0 && len <= PAGE_SIZE) { + (*num_entries)++; + tags_size+=len; + } + } + } + + return tags_size; +} + +int module_process_aliases(struct module *mod, struct load_info *info) +{ + unsigned int size, i = 0, num_entries = 0; + char *alias; + + size = get_modinfo_tags(info, "alias", &num_entries); + if (WARN_ON(!size)) + return 0; + + mod->aliases = kzalloc(num_entries * sizeof(char *), GFP_KERNEL); + if (!mod->aliases) + return -ENOMEM; + + pr_debug("module %s num_aliases: %u\n", mod->name, num_entries); + + for_each_modinfo_entry(alias, info, "alias") { + pr_debug("alias[%u] = %s\n", i, alias); + mod->aliases[i] = kasprintf(GFP_KERNEL, "%s", alias); + if (!mod->aliases[i]) + goto err_free; + i++; + } + + WARN_ON(i != num_entries); + + mod->num_aliases = num_entries; + + return 0; + +err_free: + while (i!=0) { + i--; + kfree(mod->aliases[i]); + mod->aliases[i] = NULL; + } + + kfree(mod->aliases); + mod->aliases = NULL; + + return -ENOMEM; +} diff --git a/kernel/module/internal.h b/kernel/module/internal.h index 6ae29bb8836f..40bb80ed21e2 100644 --- a/kernel/module/internal.h +++ b/kernel/module/internal.h @@ -96,6 +96,8 @@ long module_get_offset_and_type(struct module *mod, enum mod_mem_type type, char *module_flags(struct module *mod, char *buf, bool show_state); size_t module_flags_taint(unsigned long taints, char *buf); +char *get_modinfo(const struct load_info *info, const char *tag); +char *get_next_modinfo(const struct load_info *info, const char *tag, char *prev); char *module_next_tag_pair(char *string, unsigned long *secsize); #define for_each_modinfo_entry(entry, info, name) \ @@ -300,3 +302,16 @@ static inline int same_magic(const char *amagic, const char *bmagic, bool has_cr return strcmp(amagic, bmagic) == 0; } #endif /* CONFIG_MODVERSIONS */ + +#ifdef CONFIG_MODULE_KERNEL_ALIAS +void free_mod_aliases(struct module *mod); +int module_process_aliases(struct module *mod, struct load_info *info); +#else +static void free_mod_aliases(struct module *mod) +{ +} +static int module_process_aliases(struct module *mod, struct load_info *info) +{ + return 0; +} +#endif /* CONFIG_MODULE_KERNEL_ALIAS */ diff --git a/kernel/module/main.c b/kernel/module/main.c index 3f7c8634cf06..16770942f33a 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -2,6 +2,7 @@ /* * Copyright (C) 2002 Richard Henderson * Copyright (C) 2001 Rusty Russell, 2002, 2010 Rusty Russell IBM. + * Copyright (C) 2023 Luis Chamberlain */ #define INCLUDE_VERMAGIC @@ -1035,8 +1036,7 @@ char *module_next_tag_pair(char *string, unsigned long *secsize) return string; } -static char *get_next_modinfo(const struct load_info *info, const char *tag, - char *prev) +char *get_next_modinfo(const struct load_info *info, const char *tag, char *prev) { char *p; unsigned int taglen = strlen(tag); @@ -1061,7 +1061,7 @@ static char *get_next_modinfo(const struct load_info *info, const char *tag, return NULL; } -static char *get_modinfo(const struct load_info *info, const char *tag) +char *get_modinfo(const struct load_info *info, const char *tag) { return get_next_modinfo(info, tag, NULL); } @@ -1289,6 +1289,7 @@ static void free_module(struct module *mod) module_arch_freeing_init(mod); kfree(mod->args); percpu_modfree(mod); + free_mod_aliases(mod); free_mod_mem(mod); } @@ -1989,6 +1990,12 @@ static int check_modinfo(struct module *mod, struct load_info *info, int flags) "is unknown, you have been warned.\n", mod->name); } + if (get_modinfo(info, "alias")) { + err = module_process_aliases(mod, info); + if (err) + goto err_out_skip_alloc; + } + err = check_modinfo_livepatch(mod, info); if (err) goto err_out; @@ -2005,6 +2012,8 @@ static int check_modinfo(struct module *mod, struct load_info *info, int flags) return 0; err_out: + free_mod_aliases(mod); +err_out_skip_alloc: return err; } @@ -2329,6 +2338,7 @@ static struct module *layout_and_allocate(struct load_info *info, int flags) kmemleak_load_module(mod, info); return mod; err_out: + free_mod_aliases(mod); return ERR_PTR(err); } @@ -2890,6 +2900,7 @@ static int load_module(struct load_info *info, const char __user *uargs, synchronize_rcu(); mutex_unlock(&module_mutex); free_module: + free_mod_aliases(mod); /* Free lock-classes; relies on the preceding sync_rcu() */ for_class_mod_mem_type(type, core_data) { lockdep_free_key_range(mod->mem[type].base, From patchwork Sat Mar 11 05:17:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luis Chamberlain X-Patchwork-Id: 13170631 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 947BFC61DA4 for ; Sat, 11 Mar 2023 05:17:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230016AbjCKFRl (ORCPT ); Sat, 11 Mar 2023 00:17:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46750 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229924AbjCKFRd (ORCPT ); Sat, 11 Mar 2023 00:17:33 -0500 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 78B9914052B; Fri, 10 Mar 2023 21:17:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=8q3uBRgYTBxQf0UmjMKtoxjIBw/L6feX+juCAJ35hgI=; b=Oyl71dYJ9qFUi+q5p2VcJul0tM ZBRmlGLKJ9mymmLjGW0bRvUMzDGuBPGDHYlWhQ4hltD+UY91QtkhPBSb27F3nWCgDHgir99wU6EVa omTLv4RMHJT64CTjVKUVPwcXuxi/dLXuBK48JXPFoWtVbA93wXRcwc0XEDQg5mD0fK/ZkVfzgdxK3 Ay1098NxnT9gK5Ts644ycAPgJosGfyWxUe9Ujcg9cDKWvZ/kx3AfyUsXjdYbmJ6wz9bms39qWravQ We7GU1A12qC/VgpXNhXViIkK6PIikLijeOmWN4w9/1eLThfvnNBiT7NCI7Nxz8y7SYY4nzjQGJcNr P1B/4h5g==; Received: from mcgrof by bombadil.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1parbj-00HBN0-93; Sat, 11 Mar 2023 05:17:27 +0000 From: Luis Chamberlain To: linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, pmladek@suse.com, david@redhat.com, petr.pavlu@suse.com, prarit@redhat.com Cc: christophe.leroy@csgroup.eu, song@kernel.org, mcgrof@kernel.org, torvalds@linux-foundation.org Subject: [RFC 06/12] module: move early sanity checks into a helper Date: Fri, 10 Mar 2023 21:17:06 -0800 Message-Id: <20230311051712.4095040-7-mcgrof@kernel.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20230311051712.4095040-1-mcgrof@kernel.org> References: <20230311051712.4095040-1-mcgrof@kernel.org> MIME-Version: 1.0 Sender: Luis Chamberlain Precedence: bulk List-ID: Move early sanity checkers for the module into a helper. This let's us make it clear when we are working with the local copy of the module prior to allocation. This produces no functional changes, it just makes subsequent changes easier to read. Signed-off-by: Luis Chamberlain --- kernel/module/main.c | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index 16770942f33a..32c92fb69c05 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -2682,6 +2682,31 @@ static int unknown_module_param_cb(char *param, char *val, const char *modname, return 0; } +/* Module within temporary copy, this doesn't do any allocation */ +static int early_mod_check(struct load_info *info, int flags) +{ + int err; + + /* + * Now that we know we have the correct module name, check + * if it's blacklisted. + */ + if (blacklisted(info->name)) { + pr_err("Module %s is blacklisted\n", info->name); + return -EPERM; + } + + err = rewrite_section_headers(info, flags); + if (err) + return err; + + /* Check module struct version now, before we try to use module. */ + if (!check_modstruct_version(info, info->mod)) + return ENOEXEC; + + return 0; +} + /* * Allocate and load the module: note that size of section 0 is always * zero, and we rely on this for optional sections. @@ -2725,26 +2750,10 @@ static int load_module(struct load_info *info, const char __user *uargs, if (err) goto free_copy; - /* - * Now that we know we have the correct module name, check - * if it's blacklisted. - */ - if (blacklisted(info->name)) { - err = -EPERM; - pr_err("Module %s is blacklisted\n", info->name); - goto free_copy; - } - - err = rewrite_section_headers(info, flags); + err = early_mod_check(info, flags); if (err) goto free_copy; - /* Check module struct version now, before we try to use module. */ - if (!check_modstruct_version(info, info->mod)) { - err = -ENOEXEC; - goto free_copy; - } - /* Figure out module layout, and allocate all the memory. */ mod = layout_and_allocate(info, flags); if (IS_ERR(mod)) { From patchwork Sat Mar 11 05:17:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luis Chamberlain X-Patchwork-Id: 13170624 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 33C5EC6FD1C for ; Sat, 11 Mar 2023 05:17:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229922AbjCKFRc (ORCPT ); Sat, 11 Mar 2023 00:17:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46712 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229685AbjCKFRb (ORCPT ); Sat, 11 Mar 2023 00:17:31 -0500 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7776A14050A; Fri, 10 Mar 2023 21:17:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=qzqBEKCC2vC00pN2R7hc5pPU3nToqryyRx5JQLW/z9M=; b=TEsvSmfSDwpaf+lGKHbExgRkTY RaHoiv1xaoskdj4IYejWfTgE11m/GKkwy0mCL3C/iim1GxE7OHZfJbdGACeRKMTkBEF1ZQjf4ghVI BXdJ+uQp3ez6TA8qFVJf6LAts/cEkpfQK4LSLYn0Q+fqX0udUrWRZolJ2wtqDf/YPUVFAINZcjPMa lSagEX7ap2lUAKGMLSq9bMBtYjo3ecSjlRcnwqLZDxWFq8HgpjcIP2SIBsC4m5lNVqpOdB2a8xyad mGzUpQLZ/awK8V3YUlAH4MrmXxxqVpl+EHQher4RL+RFEFuQ4sLN68G+qdPlKYzu6AK+az2Mq8icF NQl4r/Sw==; Received: from mcgrof by bombadil.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1parbj-00HBN2-AK; Sat, 11 Mar 2023 05:17:27 +0000 From: Luis Chamberlain To: linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, pmladek@suse.com, david@redhat.com, petr.pavlu@suse.com, prarit@redhat.com Cc: christophe.leroy@csgroup.eu, song@kernel.org, mcgrof@kernel.org, torvalds@linux-foundation.org Subject: [RFC 07/12] module: move check_modinfo() early to early_mod_check() Date: Fri, 10 Mar 2023 21:17:07 -0800 Message-Id: <20230311051712.4095040-8-mcgrof@kernel.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20230311051712.4095040-1-mcgrof@kernel.org> References: <20230311051712.4095040-1-mcgrof@kernel.org> MIME-Version: 1.0 Sender: Luis Chamberlain Precedence: bulk List-ID: This moves check_modinfo() to early_mod_check(). This doesn't make any functional changes either, as check_modinfo() was the first call on layout_and_allocate(), so we're just moving it back one routine and at the end. This let's us keep separate the checkers from the allocater. Signed-off-by: Luis Chamberlain --- kernel/module/main.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index 32c92fb69c05..e9c7eb827f0d 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -2284,10 +2284,6 @@ static struct module *layout_and_allocate(struct load_info *info, int flags) unsigned int ndx; int err; - err = check_modinfo(info->mod, info, flags); - if (err) - return ERR_PTR(err); - /* Allow arches to frob section contents and sizes. */ err = module_frob_arch_sections(info->hdr, info->sechdrs, info->secstrings, info->mod); @@ -2702,7 +2698,11 @@ static int early_mod_check(struct load_info *info, int flags) /* Check module struct version now, before we try to use module. */ if (!check_modstruct_version(info, info->mod)) - return ENOEXEC; + return -ENOEXEC; + + err = check_modinfo(info->mod, info, flags); + if (err) + return err; return 0; } From patchwork Sat Mar 11 05:17:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luis Chamberlain X-Patchwork-Id: 13170625 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7EBC7C678D5 for ; Sat, 11 Mar 2023 05:17:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229960AbjCKFRg (ORCPT ); Sat, 11 Mar 2023 00:17:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46734 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229754AbjCKFRc (ORCPT ); Sat, 11 Mar 2023 00:17:32 -0500 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 78806140518; Fri, 10 Mar 2023 21:17:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=0Kfz9ManWhljt3gWTtQeON0UUzOKjwUIYWv5h32VpPw=; b=MnN1/ZLHgS6/DusPEn8slWDfHS 2uYR5J1cw9XFTdAxvBmp0T5H3IjRvJslb+CP2mKFbo4Jx6ze3QIy9GK6ejC+7gm+vr6Rwl4BA/IF/ CTjsgTvrAnLnbGCRzKyc55zFZBahioLHJTb/YTef1kDF/JVtOpMFjx4uROYEG8atCZn6TnzhFzWjD G1tzhbd1LDFijal17jC4m7Sg67t48rBywMqQ1jREijt4+mGhqmXEvbw7gIYFxzDYKk2gxMspbqVyV GNN8wTC/YOp+zEc5RWU2NOAfs2TdO3hT7vBt0jWaBKenj7UII01CdlQ+2ZNZowVyyiEEVP5WuXKbs yAAe/NIw==; Received: from mcgrof by bombadil.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1parbj-00HBN4-Bs; Sat, 11 Mar 2023 05:17:27 +0000 From: Luis Chamberlain To: linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, pmladek@suse.com, david@redhat.com, petr.pavlu@suse.com, prarit@redhat.com Cc: christophe.leroy@csgroup.eu, song@kernel.org, mcgrof@kernel.org, torvalds@linux-foundation.org Subject: [RFC 08/12] module: move finished_loading() Date: Fri, 10 Mar 2023 21:17:08 -0800 Message-Id: <20230311051712.4095040-9-mcgrof@kernel.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20230311051712.4095040-1-mcgrof@kernel.org> References: <20230311051712.4095040-1-mcgrof@kernel.org> MIME-Version: 1.0 Sender: Luis Chamberlain Precedence: bulk List-ID: This has no functional change, just moves a routine earlier as we'll make use of it next. Signed-off-by: Luis Chamberlain --- kernel/module/main.c | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index e9c7eb827f0d..c3e5076c0436 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -2370,27 +2370,6 @@ static int post_relocation(struct module *mod, const struct load_info *info) return module_finalize(info->hdr, info->sechdrs, mod); } -/* Is this module of this name done loading? No locks held. */ -static bool finished_loading(const char *name) -{ - struct module *mod; - bool ret; - - /* - * The module_mutex should not be a heavily contended lock; - * if we get the occasional sleep here, we'll go an extra iteration - * in the wait_event_interruptible(), which is harmless. - */ - sched_annotate_sleep(); - mutex_lock(&module_mutex); - mod = find_module_all(name, strlen(name), true); - ret = !mod || mod->state == MODULE_STATE_LIVE - || mod->state == MODULE_STATE_GOING; - mutex_unlock(&module_mutex); - - return ret; -} - /* Call module constructors. */ static void do_mod_ctors(struct module *mod) { @@ -2554,6 +2533,27 @@ static int may_init_module(void) return 0; } +/* Is this module of this name done loading? No locks held. */ +static bool finished_loading(const char *name) +{ + struct module *mod; + bool ret; + + /* + * The module_mutex should not be a heavily contended lock; + * if we get the occasional sleep here, we'll go an extra iteration + * in the wait_event_interruptible(), which is harmless. + */ + sched_annotate_sleep(); + mutex_lock(&module_mutex); + mod = find_module_all(name, strlen(name), true); + ret = !mod || mod->state == MODULE_STATE_LIVE + || mod->state == MODULE_STATE_GOING; + mutex_unlock(&module_mutex); + + return ret; +} + /* * We try to place it in the list now to make sure it's unique before * we dedicate too many resources. In particular, temporary percpu From patchwork Sat Mar 11 05:17:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luis Chamberlain X-Patchwork-Id: 13170627 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8E02FC74A4B for ; Sat, 11 Mar 2023 05:17:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229997AbjCKFRj (ORCPT ); Sat, 11 Mar 2023 00:17:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46740 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229768AbjCKFRc (ORCPT ); Sat, 11 Mar 2023 00:17:32 -0500 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7841714050D; Fri, 10 Mar 2023 21:17:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=WsfRRtm6UalI9L41GZ3NbpGQ5o56fXzEWOq/MZtycqM=; b=TYZmpPogBKWjK0SvC5ljLiPSLg avytpGS9084TZtpFLSKLx6XDeAES3WmNrEQY+rsGV06rM43+H1p5XC/W+K+nRI9lbPWL00Qx3G8Pe oDDjmDFBO8KWYJGQJf976ZCM4Cskip1Q6xIEO7mY1wKndTC1c27BQMBHXewTUJYnhHWISm1HOcrM7 c/ULk9O6absN+9pj6GHXjw6bGZSYna0sqnj2Hl+XQHYy3ydHZPSchsgJ+GfewDpdsHHIvg84hcHhX MR5SxEegoEK/qnkVUJlvy1HigaYMynjguVQWf28GX7JIjz6ObqcF+oj4JL7c+/xv6gszjcuOHpLVY dcardj8A==; Received: from mcgrof by bombadil.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1parbj-00HBN9-Dj; Sat, 11 Mar 2023 05:17:27 +0000 From: Luis Chamberlain To: linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, pmladek@suse.com, david@redhat.com, petr.pavlu@suse.com, prarit@redhat.com Cc: christophe.leroy@csgroup.eu, song@kernel.org, mcgrof@kernel.org, torvalds@linux-foundation.org Subject: [RFC 09/12] module: extract patient module check into helper Date: Fri, 10 Mar 2023 21:17:09 -0800 Message-Id: <20230311051712.4095040-10-mcgrof@kernel.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20230311051712.4095040-1-mcgrof@kernel.org> References: <20230311051712.4095040-1-mcgrof@kernel.org> MIME-Version: 1.0 Sender: Luis Chamberlain Precedence: bulk List-ID: The patient module check inside add_unformed_module() is large enough as we need it. It is a bit hard to read too, so just move it to a helper and do the inverse checks first to help shift the code and make it easier to read. The new helper then is module_patient_check_exists(). Signed-off-by: Luis Chamberlain --- kernel/module/main.c | 71 +++++++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 31 deletions(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index c3e5076c0436..e24323e2c499 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -2554,6 +2554,43 @@ static bool finished_loading(const char *name) return ret; } +/* Must be called with module_mutex held */ +static int module_patient_check_exists(const char *name) +{ + struct module *old; + int err = 0; + + old = find_module_all(name, strlen(name), true); + if (old == NULL) + return 0; + + if (old->state == MODULE_STATE_COMING + || old->state == MODULE_STATE_UNFORMED) { + /* Wait in case it fails to load. */ + mutex_unlock(&module_mutex); + err = wait_event_interruptible(module_wq, + finished_loading(name)); + if (err) + return err; + + /* The module might have gone in the meantime. */ + mutex_lock(&module_mutex); + old = find_module_all(name, strlen(name), true); + } + + /* + * We are here only when the same module was being loaded. Do + * not try to load it again right now. It prevents long delays + * caused by serialized module load failures. It might happen + * when more devices of the same type trigger load of + * a particular module. + */ + if (old && old->state == MODULE_STATE_LIVE) + return -EEXIST; + else + return -EBUSY; +} + /* * We try to place it in the list now to make sure it's unique before * we dedicate too many resources. In particular, temporary percpu @@ -2562,41 +2599,14 @@ static bool finished_loading(const char *name) static int add_unformed_module(struct module *mod) { int err; - struct module *old; mod->state = MODULE_STATE_UNFORMED; mutex_lock(&module_mutex); - old = find_module_all(mod->name, strlen(mod->name), true); - if (old != NULL) { - if (old->state == MODULE_STATE_COMING - || old->state == MODULE_STATE_UNFORMED) { - /* Wait in case it fails to load. */ - mutex_unlock(&module_mutex); - err = wait_event_interruptible(module_wq, - finished_loading(mod->name)); - if (err) - goto out_unlocked; - - /* The module might have gone in the meantime. */ - mutex_lock(&module_mutex); - old = find_module_all(mod->name, strlen(mod->name), - true); - } - - /* - * We are here only when the same module was being loaded. Do - * not try to load it again right now. It prevents long delays - * caused by serialized module load failures. It might happen - * when more devices of the same type trigger load of - * a particular module. - */ - if (old && old->state == MODULE_STATE_LIVE) - err = -EEXIST; - else - err = -EBUSY; + err = module_patient_check_exists(mod->name); + if (err) goto out; - } + mod_update_bounds(mod); list_add_rcu(&mod->list, &modules); mod_tree_insert(mod); @@ -2604,7 +2614,6 @@ static int add_unformed_module(struct module *mod) out: mutex_unlock(&module_mutex); -out_unlocked: return err; } From patchwork Sat Mar 11 05:17:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luis Chamberlain X-Patchwork-Id: 13170630 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AAB26C678D5 for ; Sat, 11 Mar 2023 05:17:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230000AbjCKFRk (ORCPT ); Sat, 11 Mar 2023 00:17:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46768 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229945AbjCKFRe (ORCPT ); Sat, 11 Mar 2023 00:17:34 -0500 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0CB0F140507; Fri, 10 Mar 2023 21:17:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=P2MoYZYdnAb3Oq0MP2kQqdmToxGWZIkjpMBdgpAlhMs=; b=SuFhUVy5Zd7B7mtIDnpPByC4Ao 10YL+FTyhpuXcsai5/ra07AtTaL5mvac+FSPlnnWqTEKl+hS31lsFUdNEawCii+7q0glpKsoCEWpZ HcsVn3gxQ7LZm2Nf1pUIAmQ/B4IW0uQW06uwV62r6p7PoZk45GSTxT+rmIlduySRK0EbiYsjvYSBk qYbYPz9wY0Du2LmXb20Xw88j30mQqJmGCRzcy7WzsiZ4/yUXWrD9w8DQHNImzr34AIc26mHOVpvqD gRqKNWz7jxO2yBDwYX5Tg3I1gxxwwuG2ANUNOjq/se13XzV1zupepqdj2WnJedLFKD3759M+PlDkn fhPpKksg==; Received: from mcgrof by bombadil.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1parbj-00HBNC-Fl; Sat, 11 Mar 2023 05:17:27 +0000 From: Luis Chamberlain To: linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, pmladek@suse.com, david@redhat.com, petr.pavlu@suse.com, prarit@redhat.com Cc: christophe.leroy@csgroup.eu, song@kernel.org, mcgrof@kernel.org, torvalds@linux-foundation.org Subject: [RFC 10/12] module: avoid allocation if module is already present and ready Date: Fri, 10 Mar 2023 21:17:10 -0800 Message-Id: <20230311051712.4095040-11-mcgrof@kernel.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20230311051712.4095040-1-mcgrof@kernel.org> References: <20230311051712.4095040-1-mcgrof@kernel.org> MIME-Version: 1.0 Sender: Luis Chamberlain Precedence: bulk List-ID: load_module() will allocate a struct module before even checking if the module is already loaded. This can create unecessary memory pressure since we can easily just check if the module is already present early with the copy of the module information from userspace after we've validated it a bit. This can only be an issue if a system is getting hammered with userspace loading modules. Note that there are two ways to load modules, one is auto-loading in-kernel and that pings back to userspace to just call modprobe. Then userspace itself *is* supposed to check if a module is present before loading it. But we're observing situations where tons of the same module are in effect being loaded. In that situation we can end up allocating memory for module requests which after allocating memory will fail to load. To avoid memory pressure for such stupid cases put a stop gap for them. Signed-off-by: Luis Chamberlain --- kernel/module/main.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/kernel/module/main.c b/kernel/module/main.c index e24323e2c499..909454f9616e 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -2713,6 +2713,10 @@ static int early_mod_check(struct load_info *info, int flags) if (err) return err; + err = module_patient_check_exists(info->mod->name); + if (err) + return err; + return 0; } From patchwork Sat Mar 11 05:17:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luis Chamberlain X-Patchwork-Id: 13170633 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7B9BDC61DA4 for ; Sat, 11 Mar 2023 05:17:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230032AbjCKFRp (ORCPT ); Sat, 11 Mar 2023 00:17:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46746 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229910AbjCKFRc (ORCPT ); Sat, 11 Mar 2023 00:17:32 -0500 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 78563140515; Fri, 10 Mar 2023 21:17:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=O4b3Qxh/i39uh+zwcsp8AsaOxA5M41InYNm+nORqDPs=; b=3Ow6eDvBTwIXsDf145Zz2dBLVB yuBPoQbmbDY0TruIbFFrwghKp2pc0QFVg5Lm3yW9FgiDn5BzW4MiwSmotkIXMBPGPXebmLnuLQfxE 62RMq0qzsEX+dOSlhoOuwYd8Bbwmq6vO7DuS1Dv9RM4oKsz03tnLCwCxBU/hwSf0YhPHS5TQyNae0 +KqHD4ebj04pdEw4z40rApf4pnL5D2rC+G/Yb4ddAN0AACFeRBYoHeXVYYKvNdbc+paOa5z5Ymlw7 Hv871ZP8Gojpg8tRM5KsGW8Zcv7Z7JHAHpEWEIOX8mGKb8hXlKlk8AeDIJjk0ql6FnsB6BixcMqfc /DUlEzCQ==; Received: from mcgrof by bombadil.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1parbj-00HBNE-HP; Sat, 11 Mar 2023 05:17:27 +0000 From: Luis Chamberlain To: linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, pmladek@suse.com, david@redhat.com, petr.pavlu@suse.com, prarit@redhat.com Cc: christophe.leroy@csgroup.eu, song@kernel.org, mcgrof@kernel.org, torvalds@linux-foundation.org Subject: [RFC 11/12] module: use list_add_tail_rcu() when adding module Date: Fri, 10 Mar 2023 21:17:11 -0800 Message-Id: <20230311051712.4095040-12-mcgrof@kernel.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20230311051712.4095040-1-mcgrof@kernel.org> References: <20230311051712.4095040-1-mcgrof@kernel.org> MIME-Version: 1.0 Sender: Luis Chamberlain Precedence: bulk List-ID: Put a new module at the end of the list intead of making new modules at the top of the list. find_module_all() start the hunt using the first entry on the list, if we assume that the modules which are first loaded are the most frequently looked for modules this should provide a tiny optimization. This is theoretical, and could use more actual data by analzying the impact of this patch on boot time a slew of systems using systemd-analyze. Signed-off-by: Luis R. Rodriguez --- kernel/module/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index 909454f9616e..bc9202b60d55 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -2608,7 +2608,7 @@ static int add_unformed_module(struct module *mod) goto out; mod_update_bounds(mod); - list_add_rcu(&mod->list, &modules); + list_add_tail_rcu(&mod->list, &modules); mod_tree_insert(mod); err = 0; From patchwork Sat Mar 11 05:17:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luis Chamberlain X-Patchwork-Id: 13170632 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0F6A9C6FD1C for ; Sat, 11 Mar 2023 05:17:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229514AbjCKFRm (ORCPT ); Sat, 11 Mar 2023 00:17:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46760 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229933AbjCKFRd (ORCPT ); Sat, 11 Mar 2023 00:17:33 -0500 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7836114050B; Fri, 10 Mar 2023 21:17:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=pxTUOVx9i1JFOyCPATR0il1cHRcDZ89gN6jPm1YO3X8=; b=1KBw30U8YggjwHFTZnlXveoHDd GHJ64T+dUK7GtsvB5Dhia8zLF/jfDFwlRiLp+6PdSDaNqL3UFUTQR7u6gcYa4F+8LfXZjmD7Ulzr8 g6+S+A0gCudBGun8jhkiKPTyR5Tk4dr3G0WXRF7ecWP459aKYrZt48opPxV52Y5p7744catjlqhom d8O4k7pfZau8Q2dolG+oPgU38RF+RfkFOcedICrOivuPfNbAQ8LQ/nZeELimz6nxUbQMHgobvTO/V tPjs9Eum+aCYa+t7ti/qhZYwJaFuxOaB+k4rOkRXNDz1dE0MP0n2ijgLpk/L3iisEIl1miJmvHIl0 AXbqfwAQ==; Received: from mcgrof by bombadil.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1parbj-00HBNG-JB; Sat, 11 Mar 2023 05:17:27 +0000 From: Luis Chamberlain To: linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, pmladek@suse.com, david@redhat.com, petr.pavlu@suse.com, prarit@redhat.com Cc: christophe.leroy@csgroup.eu, song@kernel.org, mcgrof@kernel.org, torvalds@linux-foundation.org Subject: [RFC 12/12] module: use aliases to find module on find_module_all() Date: Fri, 10 Mar 2023 21:17:12 -0800 Message-Id: <20230311051712.4095040-13-mcgrof@kernel.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20230311051712.4095040-1-mcgrof@kernel.org> References: <20230311051712.4095040-1-mcgrof@kernel.org> MIME-Version: 1.0 Sender: Luis Chamberlain Precedence: bulk List-ID: Modules can have a series of aliases, but we don't currently use them to check if a module is already loaded. Part of this is because load_module() will stick to checking for already loaded modules using the actual module name, not an alias. Its however desriable to also check for aliases on find_module_all() for existing callers and future callers. The curent gain to using aliases on find_module_all() will simply be to be able to support unloading modules using the alias using the delete_module() syscall. You can debug this with dynamic debug: GRUB_CMDLINE_LINUX_DEFAULT="dyndbg=\"func module_process_aliases +p; func module_name_match +p; \" " Suggested-by: Linus Torvalds Signed-off-by: Luis Chamberlain --- kernel/module/aliases.c | 17 +++++++++++++++++ kernel/module/internal.h | 5 +++++ kernel/module/main.c | 24 +++++++++++++++++++++++- 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/kernel/module/aliases.c b/kernel/module/aliases.c index 2f30c9d4c765..69518bc5169a 100644 --- a/kernel/module/aliases.c +++ b/kernel/module/aliases.c @@ -90,3 +90,20 @@ int module_process_aliases(struct module *mod, struct load_info *info) return -ENOMEM; } + +bool module_name_match_aliases(struct module *mod, const char *name, size_t len) +{ + unsigned int i; + const char *alias; + + for (i=0; i < mod->num_aliases; i++) { + alias = mod->aliases[i]; + if (strlen(alias) == len && !memcmp(alias, name, len)) { + pr_debug("module %s alias matched: alias[%u] = %s\n", + mod->name, i, alias); + return true; + } + } + + return false; +} diff --git a/kernel/module/internal.h b/kernel/module/internal.h index 40bb80ed21e2..78aaad74f4ca 100644 --- a/kernel/module/internal.h +++ b/kernel/module/internal.h @@ -306,6 +306,7 @@ static inline int same_magic(const char *amagic, const char *bmagic, bool has_cr #ifdef CONFIG_MODULE_KERNEL_ALIAS void free_mod_aliases(struct module *mod); int module_process_aliases(struct module *mod, struct load_info *info); +bool module_name_match_aliases(struct module *mod, const char *name, size_t len); #else static void free_mod_aliases(struct module *mod) { @@ -314,4 +315,8 @@ static int module_process_aliases(struct module *mod, struct load_info *info) { return 0; } +static bool module_name_match_aliases(struct module *mod, const char *name, size_t len) +{ + return false; +} #endif /* CONFIG_MODULE_KERNEL_ALIAS */ diff --git a/kernel/module/main.c b/kernel/module/main.c index bc9202b60d55..cf044329da3c 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -338,6 +338,28 @@ bool find_symbol(struct find_symbol_arg *fsa) return false; } +static bool module_name_match(struct module *mod, const char *name, size_t len) +{ + unsigned int i; + const char *alias; + + if (strlen(mod->name) == len && !memcmp(mod->name, name, len)) + return true; + + return module_name_match_aliases(mod, name, len); + + for (i=0; i < mod->num_aliases; i++) { + alias = mod->aliases[i]; + if (strlen(alias) == len && !memcmp(alias, name, len)) { + pr_debug("module %s alias matched: alias[%u] = %s\n", + mod->name, i, alias); + return true; + } + } + + return false; +} + /* * Search for module by name: must hold module_mutex (or preempt disabled * for read-only access). @@ -353,7 +375,7 @@ struct module *find_module_all(const char *name, size_t len, lockdep_is_held(&module_mutex)) { if (!even_unformed && mod->state == MODULE_STATE_UNFORMED) continue; - if (strlen(mod->name) == len && !memcmp(mod->name, name, len)) + if (module_name_match(mod, name, len)) return mod; } return NULL;