From mboxrd@z Thu Jan 1 00:00:00 1970 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 A1FC747A6C; Tue, 14 May 2024 11:36: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=1715686587; cv=none; b=qKh4ndMmnyzdK26jkHahV7tie0MpXhhItH0XUps9O0tsJF0AeHvhMolEEV1rbwCDweAcJbYexBreduDDfbO+2i/kQRPDI35cEx5aRLWtc7ao2aBhDB16E99lQvmDCD4m93fH3GetrCm2taFuA1gReWGhW5odVpW0lXhpgyYuiWw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715686587; c=relaxed/simple; bh=D77+0cb0fBWXzIDX/4qH5+CWo89SVPKd+B8i1Ap9U8c=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iADAYd6WDrbIF1n0E0d7xhsgQ9be+bZXeBJ5s4jlw5byJzmSSOpc9XAramFoxai3q0GTvoXlHddI+HWoT9Yw/I6E1iLfirdeb0Ix2Mme51PyM4uV39tr9j0kGvmwNhXsp2igZMbH6N5HNVQcbOl2IeDnHTRQ51iFBXh9c7r6Xhc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=P/wJp4+3; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="P/wJp4+3" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 20102C2BD10; Tue, 14 May 2024 11:36:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1715686587; bh=D77+0cb0fBWXzIDX/4qH5+CWo89SVPKd+B8i1Ap9U8c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=P/wJp4+3Wgj7rqpl3VTfXeyeOlrev1471mTS5LSNqjstAU2RhH2ZML20vyvwxP2su o0YuF0fHyDwIkzEdVZ8mo2s2ZIv6iQ4tndvNe1xyir0+vDf4ulQI2A6md4XVN4/xDG DIR0S+YezF15fRqc5iyGtp+iaTg2FjMD8kmibmC8= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Thomas Bertschinger , Martin Rodriguez Reboredo , Alice Ryhl , Miguel Ojeda Subject: [PATCH 6.1 190/236] rust: module: place generated init_module() function in .init.text Date: Tue, 14 May 2024 12:19:12 +0200 Message-ID: <20240514101027.575206337@linuxfoundation.org> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240514101020.320785513@linuxfoundation.org> References: <20240514101020.320785513@linuxfoundation.org> User-Agent: quilt/0.67 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.1-stable review patch. If anyone has any objections, please let me know. ------------------ From: Thomas Bertschinger commit 1b6170ff7a203a5e8354f19b7839fe8b897a9c0d upstream. Currently Rust kernel modules have their init code placed in the `.text` section of the .ko file. I don't think this causes any real problems for Rust modules as long as all code called during initialization lives in `.text`. However, if a Rust `init_module()` function (that lives in `.text`) calls a function marked with `__init` (in C) or `#[link_section = ".init.text"]` (in Rust), then a warning is generated by modpost because that function lives in `.init.text`. For example: WARNING: modpost: fs/bcachefs/bcachefs: section mismatch in reference: init_module+0x6 (section: .text) -> _RNvXCsj7d3tFpT5JS_15bcachefs_moduleNtB2_8BcachefsNtCsjDtqRIL3JAG_6kernel6Module4init (section: .init.text) I ran into this while experimenting with converting the bcachefs kernel module from C to Rust. The module's `init()`, written in Rust, calls C functions like `bch2_vfs_init()` which are placed in `.init.text`. This patch places the macro-generated `init_module()` Rust function in the `.init.text` section. It also marks `init_module()` as unsafe--now it may not be called after module initialization completes because it may be freed already. Note that this is not enough on its own to actually get all the module initialization code in that section. The module author must still add the `#[link_section = ".init.text"]` attribute to the Rust `init()` in the `impl kernel::Module` block in order to then call `__init` functions. However, this patch enables module authors do so, when previously it would not be possible (without warnings). Signed-off-by: Thomas Bertschinger Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Alice Ryhl Link: https://lore.kernel.org/r/20240206153806.567055-1-tahbertschinger@gmail.com [ Reworded title to add prefix. ] Signed-off-by: Miguel Ojeda Signed-off-by: Greg Kroah-Hartman --- rust/macros/module.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) --- a/rust/macros/module.rs +++ b/rust/macros/module.rs @@ -202,10 +202,15 @@ pub(crate) fn module(ts: TokenStream) -> }}; // Loadable modules need to export the `{{init,cleanup}}_module` identifiers. + /// # Safety + /// + /// This function must not be called after module initialization, because it may be + /// freed after that completes. #[cfg(MODULE)] #[doc(hidden)] #[no_mangle] - pub extern \"C\" fn init_module() -> core::ffi::c_int {{ + #[link_section = \".init.text\"] + pub unsafe extern \"C\" fn init_module() -> core::ffi::c_int {{ __init() }}