From patchwork Wed Mar 29 05:31:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luis Chamberlain X-Patchwork-Id: 13191894 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id ACCC3C6FD18 for ; Wed, 29 Mar 2023 05:31:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 258AD6B0075; Wed, 29 Mar 2023 01:31:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 208036B0078; Wed, 29 Mar 2023 01:31:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0D34D6B007B; Wed, 29 Mar 2023 01:31:57 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id EE86F6B0075 for ; Wed, 29 Mar 2023 01:31:56 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id BE2AB1C5DB2 for ; Wed, 29 Mar 2023 05:31:56 +0000 (UTC) X-FDA: 80620814232.23.BFEC09A Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) by imf05.hostedemail.com (Postfix) with ESMTP id 2198E100004 for ; Wed, 29 Mar 2023 05:31:54 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=Hq7MpAhb; dmarc=fail reason="No valid SPF, DKIM not aligned (relaxed)" header.from=kernel.org (policy=none); spf=none (imf05.hostedemail.com: domain of mcgrof@infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=mcgrof@infradead.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1680067915; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=C7tEOVKbmd31kJKkSCr8L6PeUYpxvjj7kEhb9lxj2lU=; b=YtIbLcDFPSgmWu4xe7se5sKLMaFGDOIiOtMNWfHOMTndT0t2Gn8B5S1lJX8XJf7LYFHCSP PH0pyLNBCcCuLU40ph6YCsCOHJT6wrLPZzyIAuPj/1RqRH6uJFmnwJF7feYVcH8iepkq29 l4Rq0GNYa5fapNJp3zNX7HW+asF2q9o= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=Hq7MpAhb; dmarc=fail reason="No valid SPF, DKIM not aligned (relaxed)" header.from=kernel.org (policy=none); spf=none (imf05.hostedemail.com: domain of mcgrof@infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=mcgrof@infradead.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1680067915; a=rsa-sha256; cv=none; b=8b3ih0GH08dGyGveSqyNioq75p7mMTIFlklIe11874myizXvf3ic/NlI3uwyNGJy/4ULwo XPKiCE2Ki/vwXI+X5iSO03t+wsK2iMf1l6O/e++QencEYs29GDadFDufH2jWj3SBoa1SIf 1rkc+vkSV0YGJKbfGuszvb1wyb0vNxk= 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=C7tEOVKbmd31kJKkSCr8L6PeUYpxvjj7kEhb9lxj2lU=; b=Hq7MpAhblc40y8k1Y4Pqo/enTW SslWm7fO+exvvf3HZRxh+bgxhZOS3P1IYnAydKJ5ZTarr8GhRDhxNEnxrSNR8qTIPYRQkgU3YNnx+ +hKEQpyCrK+p5tnt4YfYVc53F88QARD2q7d7/0iy49zIQ53fg9qMpgODyjglcWGSsnuydP/xVL3Pf FSVm+HQx1wkfASdfb4mN3vTxsgy2DNRu5unP+d8hu1QfAr3UlqH/JXn0cVkRXiLoYO4YS6swQky9l OqFeYbHv1S5N2op7xH0OmscjuK1xIZLjsk8AFnOHnf/yFLpseFdNdJMXGHqpmE5FhbNXRa3cLgGxL JBMH5C3A==; Received: from mcgrof by bombadil.infradead.org with local (Exim 4.96 #2 (Red Hat Linux)) id 1phOPW-00GgRW-23; Wed, 29 Mar 2023 05:31:50 +0000 From: Luis Chamberlain To: david@redhat.com, patches@lists.linux.dev, linux-modules@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, pmladek@suse.com, petr.pavlu@suse.com, prarit@redhat.com, torvalds@linux-foundation.org, gregkh@linuxfoundation.org, rafael@kernel.org Cc: christophe.leroy@csgroup.eu, tglx@linutronix.de, peterz@infradead.org, song@kernel.org, rppt@kernel.org, willy@infradead.org, vbabka@suse.cz, mhocko@suse.com, dave.hansen@linux.intel.com, mcgrof@kernel.org Subject: [PATCH 2/7] module: extract patient module check into helper Date: Tue, 28 Mar 2023 22:31:44 -0700 Message-Id: <20230329053149.3976378-3-mcgrof@kernel.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20230329053149.3976378-1-mcgrof@kernel.org> References: <20230329053149.3976378-1-mcgrof@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 2198E100004 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: n5bsmr8sxfbghtof6g61ao51twcot86h X-HE-Tag: 1680067914-95069 X-HE-Meta: U2FsdGVkX18M1AOO509Q6EbxKA/dhKkKhD42UNf0SrMkowI9fZdxI4eGF5MYJFCjwfZRWpZuN9egSgj4sFP6yZbuJAM1wboEaQqwaAZoJkOX4161wljyOtKJw0KbUB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5iesdLffbscpjQF94b9f9Tn/9xyFWS5wONTz006RW4xFoUnkysf4bsY9/O5aCTlGx7LuGprBi4hF5My3UO6BKXcbm1LnBZZBTaWsRkkufjzKAZsh5fOwOOzuXNlzo6o3rYtrCwPhwFH0gbCDHj+dKeUB6rqrH6hgqSG3XdXUlBf59ZU3ngfKzeD5I5B1h6PUMxzeJkqN79VWDbpuS/G4PgbS X-Bogosity: Ham, tests=bogofilter, spamicity=0.000083, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org 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 af58e63e5daf..77c2e7a60f2e 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -2626,6 +2626,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 @@ -2634,41 +2671,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); @@ -2676,7 +2686,6 @@ static int add_unformed_module(struct module *mod) out: mutex_unlock(&module_mutex); -out_unlocked: return err; }