From patchwork Fri Nov 8 16:12:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Gomez via B4 Relay X-Patchwork-Id: 13868428 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3F4371BD9E2; Fri, 8 Nov 2024 16:12:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731082348; cv=none; b=ZH+2y5XIMyB8XS2cb7iEiob13yVwGjXWLDmSR+MuR4FK6Qvxb8CAOhhsYSKI4EoiS6PjzFvhJmMW4An20pe5nzueIH1RnB48VA3gupwx7wkgsDl0xmJ/ZVUc4oVI9cpAu7RmAfCsgpWpVzIE4jeP8u4q2C4p/6H2rgCa87MO7s8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731082348; c=relaxed/simple; bh=CQbNWypqAhFi6ysGW+rHEwMrXdX3yqR7EtZZD9S67hI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=WR+px5hOzitJyaK7oAJM8MUKXnOIh+iVSL2pDQywZmuO7A0xgdi2VmEs7T2YsiaBWJ1679/22Uw9w2KJg8JJhTvPiOhntgpA+Hpq5a2bck1iB1uweyjoWIwCAMFM7jEQoGCpAYcfk91WKbBaRwtxHztHfbF6T5y2z43DdsxWezA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=d8kxxNC+; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="d8kxxNC+" Received: by smtp.kernel.org (Postfix) with ESMTPS id D4F62C4CED0; Fri, 8 Nov 2024 16:12:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1731082347; bh=CQbNWypqAhFi6ysGW+rHEwMrXdX3yqR7EtZZD9S67hI=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=d8kxxNC+xuswV9NDqxMbTnpDTO4sWZJRovYv4NkPVCqoV646okpwKcq9A764g1C+/ c48zTV4C3eh1SWjhvjGHkDxmy2Tgow8uZf21gzQWyX+1YDw7Q2FqNtx9rndmpz8zmf t5A9MGQjEvZ+Qt5LgqNT5ctMUEObrof03Qq1NexlcvCVSyAx9h/oXN6UZy747nlO+z f3R9pFqbD4g8ABfbshJPCqk2d4OI6pL5M3hBuN2iIbXHIW0Bj3BfnR1oDDPWy2oGT7 XL3lA9mKBkSBxHyf0FhtrFFzCD/aa1G/B8BwGs0pOUYx3oQ0xe5W8772CkOEyW/8fl 9UJon/Zz/3asA== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id C5690D5C0FB; Fri, 8 Nov 2024 16:12:27 +0000 (UTC) From: Daniel Gomez via B4 Relay Date: Fri, 08 Nov 2024 17:12:13 +0100 Subject: [PATCH RFC v3 1/4] module: remove module_deallocate() unused param Precedence: bulk X-Mailing-List: linux-modules@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241108-modules-ro_after_init-v3-1-6dd041b588a5@samsung.com> References: <20241108-modules-ro_after_init-v3-0-6dd041b588a5@samsung.com> In-Reply-To: <20241108-modules-ro_after_init-v3-0-6dd041b588a5@samsung.com> To: Luis Chamberlain , Petr Pavlu , Sami Tolvanen , Christophe Leroy , Thomas Gleixner , Jinjie Ruan , Jens Axboe Cc: "Daniel Gomez (Samsung)" , linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, Daniel Gomez X-Mailer: b4 0.14.1 X-Developer-Signature: v=1; a=ed25519-sha256; t=1731082346; l=1219; i=da.gomez@samsung.com; s=20240621; h=from:subject:message-id; bh=A6gqpizSENgaBLBO0Z0bdDnIpaPO10y9Bcdf+FyWroA=; b=O7Dbg4205CdfeJlhE+bW3nIQfANZpxsZmBln1qQ0yCbBltIyVE72FQIb3iTKVLmaOI5GiLv7+ LS+p0Qm1bY/BylXOXe0q0fXAS4yRgFxaHr7jyllw5j3BFa0KEBBIMha X-Developer-Key: i=da.gomez@samsung.com; a=ed25519; pk=BqYk31UHkmv0WZShES6pIZcdmPPGay5LbzifAdZ2Ia4= X-Endpoint-Received: by B4 Relay for da.gomez@samsung.com/20240621 with auth_id=175 X-Original-From: Daniel Gomez Reply-To: da.gomez@samsung.com From: Daniel Gomez The module_deallocate() function no longer requires the struct load_info parameter as of commit 48fd11880b5ef04270be8a87d9a9a9ee2fdae338 ("module: Fix performance regression on modules with large symbol tables"). This commit removes the now-unused parameter. No functional changes. Signed-off-by: Daniel Gomez --- kernel/module/main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index 5399c182b3cb..e0b2a8d9e324 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -2767,7 +2767,7 @@ static struct module *layout_and_allocate(struct load_info *info, int flags) } /* mod is no longer valid after this! */ -static void module_deallocate(struct module *mod, struct load_info *info) +static void module_deallocate(struct module *mod) { percpu_modfree(mod); module_arch_freeing_init(mod); @@ -3417,7 +3417,7 @@ static int load_module(struct load_info *info, const char __user *uargs, mod->mem[type].size); } - module_deallocate(mod, info); + module_deallocate(mod); free_copy: /* * The info->len is always set. We distinguish between From patchwork Fri Nov 8 16:12:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Gomez via B4 Relay X-Patchwork-Id: 13868429 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5DA9086250; Fri, 8 Nov 2024 16:12:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731082348; cv=none; b=h0OyNUNT/QUyT4RJQ/rb3xc29WWcq8fOt38fsgWslkfcCYAxFs02vBmLZK10qJoIoPQHqZevRmnpHZTq39Y1LYoF4DMpTVxoZbjdclt1GIJZ9sAuOE8hAN8IbUlSuuXMLoZiBleSPUfHcmXVZRk5sz1FTOF3OsNW8q3zGLW+vEA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731082348; c=relaxed/simple; bh=1d5ON2PggbcA+/anVU20HTNwni2NHoWKO08BLOoL/G8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Mi6W1HzqkiTzQiAQ4tpm6fghjsJBRjX45gz8BoD7GneRfYa8c7SqOBJ4TSQvKbsUPFJbpI6uly94YhDZCEGsUH0W2J5CopP2e1q4cy7o0zQxQJa3DhhJq/sdYD551jfqsjt9u5GS/lpdtQ9PJ0tla4JR8Jw75/zpHLfu6sQkRZw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=MAQQTFdN; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="MAQQTFdN" Received: by smtp.kernel.org (Postfix) with ESMTPS id E756AC4CECF; Fri, 8 Nov 2024 16:12:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1731082348; bh=1d5ON2PggbcA+/anVU20HTNwni2NHoWKO08BLOoL/G8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=MAQQTFdNdtlVaHezPwwSZe+0ExgcyrBWlWmPkSx/oMSJx2tuC2JpulT/fukEY9Wy+ L4E3w8FHjDuVILB4wjH5UpcrIhEAjY477n9MWrCq3/9ctGBFPC0wYzU5znOzPWszXd GcHwHyU/o5P9u97AOn/ID+VJ728KH8Cp5eKGUC+rK+e9mRpvvevaoqkDKaQY9xbblF 0c5YbWfCdSh7wQ+2EyC5yKII4eHN2VKG00s4NghROFscOXCIGEO7wCYUSvu0tyC/cq hPTiio3soePhTexz6OSOj/AmoSh7DQ+TxMY1tXOSy1BNW+IcaeiL6ANbQvTFVAlQNw wt4jCuM1Mp2Dw== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id D5DC5D5C0FF; Fri, 8 Nov 2024 16:12:27 +0000 (UTC) From: Daniel Gomez via B4 Relay Date: Fri, 08 Nov 2024 17:12:14 +0100 Subject: [PATCH RFC v3 2/4] module: avoid double ref drop on ro_after_init failure Precedence: bulk X-Mailing-List: linux-modules@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241108-modules-ro_after_init-v3-2-6dd041b588a5@samsung.com> References: <20241108-modules-ro_after_init-v3-0-6dd041b588a5@samsung.com> In-Reply-To: <20241108-modules-ro_after_init-v3-0-6dd041b588a5@samsung.com> To: Luis Chamberlain , Petr Pavlu , Sami Tolvanen , Christophe Leroy , Thomas Gleixner , Jinjie Ruan , Jens Axboe Cc: "Daniel Gomez (Samsung)" , linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, Daniel Gomez X-Mailer: b4 0.14.1 X-Developer-Signature: v=1; a=ed25519-sha256; t=1731082346; l=1137; i=da.gomez@samsung.com; s=20240621; h=from:subject:message-id; bh=9DltLcORsUZtMTYEZ5ybIyns3a3KM2mPjN1YlLgDFI0=; b=QZs431yGGNbH/0/7tBF4itYMit6iYIAJECiADp0Xq/FSZbSS3fyf+8N8yLOlqCVaakbY8YlG3 nr9/IXGgCWmCuTJXB82rd7r27cxS+jDRSl+P4zu79plf5pt/OAoUKsO X-Developer-Key: i=da.gomez@samsung.com; a=ed25519; pk=BqYk31UHkmv0WZShES6pIZcdmPPGay5LbzifAdZ2Ia4= X-Endpoint-Received: by B4 Relay for da.gomez@samsung.com/20240621 with auth_id=175 X-Original-From: Daniel Gomez Reply-To: da.gomez@samsung.com From: Daniel Gomez Ensure the module reference is only dropped once in case we fail at MOD_RO_AFTER_INIT case. Signed-off-by: Daniel Gomez --- kernel/module/main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index e0b2a8d9e324..35a50ded64a6 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -2941,8 +2941,6 @@ static noinline int do_init_module(struct module *mod) ftrace_free_mem(mod, mod->mem[MOD_INIT_TEXT].base, mod->mem[MOD_INIT_TEXT].base + mod->mem[MOD_INIT_TEXT].size); mutex_lock(&module_mutex); - /* Drop initial reference. */ - module_put(mod); trim_init_extable(mod); #ifdef CONFIG_KALLSYMS /* Switch to core kallsyms now init is done: kallsyms may be walking! */ @@ -2951,6 +2949,8 @@ static noinline int do_init_module(struct module *mod) ret = module_enable_rodata_ro(mod, true); if (ret) goto fail_mutex_unlock; + /* Drop initial reference. */ + module_put(mod); mod_tree_remove_init(mod); module_arch_freeing_init(mod); for_class_mod_mem_type(type, init) { From patchwork Fri Nov 8 16:12:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Gomez via B4 Relay X-Patchwork-Id: 13868430 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 648A112EBE7; Fri, 8 Nov 2024 16:12:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731082348; cv=none; b=Nw76tXZ4mrcEVLcvveHuULmQv5SkjMDnYgOaxwZu2YStsOTTpFgSvzmezIqnqKG/jePj86nN9Kv0dYDCeuh77YxFmNwgaKVnZvp8yWypbzxcCd24afW5KAw7h2U5O8fjJI8b99gJDFMfkUGVpL/Xx7pGuibN7o9hlfTj4m5MOUs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731082348; c=relaxed/simple; bh=y7UEdgJZNHGbfohT5ewpxC1rRkwrbwYbdFePqHEeR/U=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=clbzGr8k0bFgjkLR/1Lu01Shsdn760I/8pDMZOFkJWNPsuKMW2KSeg8BRMvM/vnbgGwslreOpv8KunmreQmPzLk3YmKYIuZDfZLMD4Wng7yjTDD/svWpXTXeX68GmZYmUMg1nuFenofDofSqUCviWnwvt7V5IJfRySEShgBRBcc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=uns91pKf; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="uns91pKf" Received: by smtp.kernel.org (Postfix) with ESMTPS id F07ABC4CED3; Fri, 8 Nov 2024 16:12:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1731082348; bh=y7UEdgJZNHGbfohT5ewpxC1rRkwrbwYbdFePqHEeR/U=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=uns91pKfBAA+OWnZ0+lPlcekTLocGV4CQPqWE040euOvglKYClL/+5azIQ2l6IGnU pFLB9E8wRoD02E5YsC/bPmgk6fAwzMyCvX18vY6tB1KfHp/nFqDRPWM4+RPiaXq4Kp FFtvOAoTHWl6eCcP9rGuMQm7zfZd8JYnnTWEIT4wMxTusZmZ4NCcop6PWrkN79ylxn MsIA+RN2oAU3iznLV8vf0D/AAYt9fRbYePlNqm+/eoIPzLbozqzI+k4TAK/+FCLw1X nDiNFRFTAkaTIEdtyFnHDX/OwH8t8hHrrv/qMTMBsDIrq3M6/Ke+hWOQX3gBnAbPMU RqH0s1HquHECQ== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id E64B6D5D69A; Fri, 8 Nov 2024 16:12:27 +0000 (UTC) From: Daniel Gomez via B4 Relay Date: Fri, 08 Nov 2024 17:12:15 +0100 Subject: [PATCH RFC v3 3/4] module: update state and release reference under lock Precedence: bulk X-Mailing-List: linux-modules@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241108-modules-ro_after_init-v3-3-6dd041b588a5@samsung.com> References: <20241108-modules-ro_after_init-v3-0-6dd041b588a5@samsung.com> In-Reply-To: <20241108-modules-ro_after_init-v3-0-6dd041b588a5@samsung.com> To: Luis Chamberlain , Petr Pavlu , Sami Tolvanen , Christophe Leroy , Thomas Gleixner , Jinjie Ruan , Jens Axboe Cc: "Daniel Gomez (Samsung)" , linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, Daniel Gomez X-Mailer: b4 0.14.1 X-Developer-Signature: v=1; a=ed25519-sha256; t=1731082346; l=927; i=da.gomez@samsung.com; s=20240621; h=from:subject:message-id; bh=oDK90qbYeBOZipkuaq567b8TktP7UW9UN57bt99vHXY=; b=66ib05tLV5YIvQdiwB6EdW2FQlciFDI8VnOCbvAoLDNo9f0q92j48REvKz2O62bMMp4KTZsDa u3HaxoUHdV5A2+WA8zNVf55mbFYUAnUdmd1IWvh5DsVP0LMV8/9bPiK X-Developer-Key: i=da.gomez@samsung.com; a=ed25519; pk=BqYk31UHkmv0WZShES6pIZcdmPPGay5LbzifAdZ2Ia4= X-Endpoint-Received: by B4 Relay for da.gomez@samsung.com/20240621 with auth_id=175 X-Original-From: Daniel Gomez Reply-To: da.gomez@samsung.com From: Daniel Gomez Ensure the module state change and reference drop operations are performed within module_mutex to prevent potential race conditions in the failure path. Signed-off-by: Daniel Gomez --- kernel/module/main.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index 35a50ded64a6..2b45a6efa0a9 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -2994,10 +2994,12 @@ static noinline int do_init_module(struct module *mod) fail_free_freeinit: kfree(freeinit); fail: + mutex_lock(&module_mutex); /* Try to protect us from buggy refcounters. */ mod->state = MODULE_STATE_GOING; - synchronize_rcu(); module_put(mod); + mutex_unlock(&module_mutex); + synchronize_rcu(); blocking_notifier_call_chain(&module_notify_list, MODULE_STATE_GOING, mod); klp_module_going(mod); From patchwork Fri Nov 8 16:12:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Gomez via B4 Relay X-Patchwork-Id: 13868431 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 78B3013B29F; Fri, 8 Nov 2024 16:12:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731082348; cv=none; b=hj6/09DFbbEI2zkHf9q0eg6VMn7YhKpNV8nC/5NURdhY4bp8TEHl/EfAUNKF44jfQo6IblOi0dVDmInrBnSErSenQMNLUNM30PjVV09F90JZTE34FNgs5PeXqZTPbphVifLNrv7lBe3G8SnuS79R6xPCqQYEavCJjDOiKUaY8gw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731082348; c=relaxed/simple; bh=adVffkUOC5N+2b1eGWF++UUJ8/yyvaIQLPhK1rvjyuE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=d5uuJPB3sZCPBLLqmU6g/TzFc9fblD7PIrNOnbH521WrlaI7+acFa4ehbxogMUnu7CIJTfj/WELqIcXCIiDo5mmPO0vmfHmeFcvhCe2N4JPvDG5EgKha73m7KUX+xoB8uC3hB8/Dc5hvZFm0gOwx+i9Afkp7tPpsA0+QDEecnqE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ohv+2qZO; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ohv+2qZO" Received: by smtp.kernel.org (Postfix) with ESMTPS id 0D357C4CED7; Fri, 8 Nov 2024 16:12:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1731082348; bh=adVffkUOC5N+2b1eGWF++UUJ8/yyvaIQLPhK1rvjyuE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=ohv+2qZOwWEexIAz5Hl/RRBav3gEoKhgKtV15OutBKpZ7v2ytF/X6R9hXFX+s8svs dmUOapFC7/My7miMhWdlGN6waNT0u3sLy2R8ZpMgginmSvreW7N50GWPSBYHCfQc5f 0lBqpjn7by7lhI65AnyKjLrq7M7X1pX1RcKgJDBpCCI/lQA9hY/ydgjqfrFiwgO6Bk L+nZbAuDbVInbTsrCAUKm6ULpjCzWDe4j+j661ppKjytkUdKDSJOEqnbG+FT3HfWzF 83A9uFK+X9D8abp8NvFGKORVTcnBXsRbLJ6OwM+d74Uy/QlEImDQf2Bn5xHWlbYgon BjuYDrKLaKRBA== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 05335D5C0FD; Fri, 8 Nov 2024 16:12:28 +0000 (UTC) From: Daniel Gomez via B4 Relay Date: Fri, 08 Nov 2024 17:12:16 +0100 Subject: [PATCH RFC v3 4/4] module: refactor ro_after_init failure path Precedence: bulk X-Mailing-List: linux-modules@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241108-modules-ro_after_init-v3-4-6dd041b588a5@samsung.com> References: <20241108-modules-ro_after_init-v3-0-6dd041b588a5@samsung.com> In-Reply-To: <20241108-modules-ro_after_init-v3-0-6dd041b588a5@samsung.com> To: Luis Chamberlain , Petr Pavlu , Sami Tolvanen , Christophe Leroy , Thomas Gleixner , Jinjie Ruan , Jens Axboe Cc: "Daniel Gomez (Samsung)" , linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, Daniel Gomez X-Mailer: b4 0.14.1 X-Developer-Signature: v=1; a=ed25519-sha256; t=1731082346; l=5513; i=da.gomez@samsung.com; s=20240621; h=from:subject:message-id; bh=Db9jcYz5hKdZ02hfGSOe9kn0EwUQXw4KgNDzf0EpOLc=; b=UsGWnNs2g45kP6nzz3FumHxWrFnuZNaJCJ2vmZtKAwxmeuLHc0aM7x7X7TfCR1iMpEUZwBAYQ g3cmR6/8hEGA50tMDd15IEOfRf4U94fFrNLUlHnALnKAlQCD2gjGq4f X-Developer-Key: i=da.gomez@samsung.com; a=ed25519; pk=BqYk31UHkmv0WZShES6pIZcdmPPGay5LbzifAdZ2Ia4= X-Endpoint-Received: by B4 Relay for da.gomez@samsung.com/20240621 with auth_id=175 X-Original-From: Daniel Gomez Reply-To: da.gomez@samsung.com From: Daniel Gomez When ro_after_init fails, we need to unload the module. Rename the goto tag to fail_ro_after_init to make it more clear and try to check for dependencies, stop the module and call the exit function. This allows to unload the module if ro_after_init fails. This fixes the following error in block loop device driver when forcing ro_after_init error path: Nov 06 11:36:25 debian kernel: loop: module loaded Nov 06 11:36:25 debian kernel: BUG: unable to handle page fault for address: ffffffffa0006320 Nov 06 11:36:25 debian kernel: #PF: supervisor read access in kernel mode Nov 06 11:36:25 debian kernel: #PF: error_code(0x0000) - not-present page Nov 06 11:36:25 debian kernel: PGD 1e3f067 P4D 1e3f067 PUD 1e40063 PMD 10e7d4067 PTE 0 Nov 06 11:36:25 debian kernel: Oops: Oops: 0000 [#1] Nov 06 11:36:25 debian kernel: CPU: 0 UID: 0 PID: 428 Comm: (udev-worker) Not tainted 6.12.0-rc6-g4ade030a2d1b #155 Nov 06 11:36:25 debian kernel: Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.2-debian-1.16.2-1 04/01/2014 Nov 06 11:36:25 debian kernel: RIP: 0010:bdev_open+0x83/0x290 Nov 06 11:36:25 debian kernel: Code: bb 48 01 00 00 48 89 3c 24 e8 79 24 38 00 48 8b 43 40 41 bd fa ff ff ff 48 83 b8 40 03 00 00 00 0f 84 b3 01 00 00 48 8b 43 48 <48> 8b 78 78 e8 d4 c9 c8 ff 84 c0 0f 84 9e 01 00 00 80 3d 45 ad ad Nov 06 11:36:25 debian kernel: RSP: 0018:ffff8881054dbc58 EFLAGS: 00010286 Nov 06 11:36:25 debian kernel: RAX: ffffffffa00062a8 RBX: ffff8881054a6800 RCX: ffff8881075becc0 Nov 06 11:36:25 debian kernel: RDX: 0000000000000000 RSI: 0000000000000009 RDI: ffff8881054a6948 Nov 06 11:36:25 debian kernel: RBP: 0000000000000009 R08: ffff88810e7aa9c0 R09: 0000000000000000 Nov 06 11:36:25 debian kernel: R10: ffff88810e5ab0c0 R11: ffff88810e796190 R12: ffff88810094e980 Nov 06 11:36:25 debian kernel: R13: 00000000fffffffa R14: 0000000000000000 R15: 0000000000000000 Nov 06 11:36:25 debian kernel: FS: 00007fd2ff110900(0000) GS:ffffffff81e47000(0000) knlGS:0000000000000000 Nov 06 11:36:25 debian kernel: CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 Nov 06 11:36:25 debian kernel: CR2: ffffffffa0006320 CR3: 000000010e7ed004 CR4: 00000000003706b0 Nov 06 11:36:25 debian kernel: Call Trace: Nov 06 11:36:25 debian kernel: Nov 06 11:36:25 debian kernel: ? __die_body+0x16/0x60 Nov 06 11:36:25 debian kernel: ? page_fault_oops+0x22a/0x310 Nov 06 11:36:25 debian kernel: ? exc_page_fault+0x99/0xa0 Nov 06 11:36:25 debian kernel: ? asm_exc_page_fault+0x22/0x30 Nov 06 11:36:25 debian kernel: ? bdev_open+0x83/0x290 Nov 06 11:36:25 debian kernel: ? bdev_open+0x67/0x290 Nov 06 11:36:25 debian kernel: ? iput+0x37/0x150 Nov 06 11:36:25 debian kernel: ? blkdev_open+0xab/0xd0 Nov 06 11:36:25 debian kernel: ? blkdev_mmap+0x60/0x60 Nov 06 11:36:25 debian kernel: ? do_dentry_open+0x25d/0x3b0 Nov 06 11:36:25 debian kernel: ? vfs_open+0x1e/0xc0 Nov 06 11:36:25 debian kernel: ? path_openat+0x9cf/0xbb0 Nov 06 11:36:25 debian kernel: ? do_filp_open+0x7f/0xd0 Nov 06 11:36:25 debian kernel: ? do_sys_openat2+0x67/0xb0 Nov 06 11:36:25 debian kernel: ? do_sys_open+0x4b/0x50 Nov 06 11:36:25 debian kernel: ? do_syscall_64+0x3d/0xb0 Nov 06 11:36:25 debian kernel: ? entry_SYSCALL_64_after_hwframe+0x4b/0x53 Nov 06 11:36:25 debian kernel: Nov 06 11:36:25 debian kernel: Modules linked in: Nov 06 11:36:25 debian kernel: CR2: ffffffffa0006320 Nov 06 11:36:25 debian kernel: ---[ end trace 0000000000000000 ]--- ./scripts/faddr2line --list vmlinux bdev_open+0x83/0x290 bdev_open+0x83/0x290: bdev_open at block/bdev.c:908 903 904 mutex_lock(&disk->open_mutex); 905 ret = -ENXIO; 906 if (!disk_live(disk)) 907 goto abort_claiming; >908< if (!try_module_get(disk->fops->owner)) 909 goto abort_claiming; 910 ret = -EBUSY; 911 if (!bdev_may_open(bdev, mode)) 912 goto put_module; 913 if (bdev_is_partition(bdev)) Reported-by: Thomas Gleixner Closes: https://lore.kernel.org/all/20230915082126.4187913-1-ruanjinjie@huawei.com/ Fixes: d1909c022173 ("module: Don't ignore errors from set_memory_XX()"). Signed-off-by: Daniel Gomez --- kernel/module/main.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index 2b45a6efa0a9..c23521ae8bda 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -2880,7 +2880,7 @@ module_param(async_probe, bool, 0644); */ static noinline int do_init_module(struct module *mod) { - int ret = 0; + int ret = 0, forced = 0; struct mod_initfree *freeinit; #if defined(CONFIG_MODULE_STATS) unsigned int text_size = 0, total_size = 0; @@ -2948,7 +2948,7 @@ static noinline int do_init_module(struct module *mod) #endif ret = module_enable_rodata_ro(mod, true); if (ret) - goto fail_mutex_unlock; + goto fail_ro_after_init; /* Drop initial reference. */ module_put(mod); mod_tree_remove_init(mod); @@ -2989,8 +2989,12 @@ static noinline int do_init_module(struct module *mod) return 0; -fail_mutex_unlock: +fail_ro_after_init: + list_empty(&mod->source_list); + try_stop_module(mod, 0, &forced); mutex_unlock(&module_mutex); + if (mod->exit != NULL) + mod->exit(); fail_free_freeinit: kfree(freeinit); fail: