[PATCH 3/3] rust: Require RUST_MODULES for module support

Matthew Maurer
2023-11-08T02:27:57Z

Currently, we don't support MODVERSIONS for Rust symbols. For users that
want to use Rust in the kernel but for whom MODVERSIONS is required,
this allows the use of built-in Rust code even with MODVERSIONS enabled.
It may additionally allow code-size reduction by avoiding exporting
symbols that won't be used without Rust modules.

Signed-off-by: Matthew Maurer <mmaurer@google.com>
---
init/Kconfig | 21 +++++++++++++++++----
kernel/module/Kconfig | 1 +
rust/exports.c | 4 ++++
3 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/init/Kconfig b/init/Kconfig
index 9ffb103fc927..6912dbbee3f1 100644
--- a/init/Kconfig
+++ b/init/Kconfig
@@ -1885,7 +1885,6 @@ config RUST
bool "Rust support"
depends on HAVE_RUST
depends on RUST_IS_AVAILABLE
- depends on !MODVERSIONS
depends on !GCC_PLUGINS
depends on !RANDSTRUCT
depends on !DEBUG_INFO_BTF || PAHOLE_HAS_LANG_EXCLUDE
@@ -1896,13 +1895,27 @@ config RUST
This allows other Rust-related options, like drivers written in Rust,
to be selected.

- It is also required to be able to load external kernel modules
- written in Rust.
-
See Documentation/rust/ for more information.

If unsure, say N.

+config RUST_MODULES
+ bool "Rust Module Support"
+ depends on RUST
+ depends on MODULES
+ rust_modules
+ help
+ Enables support for Rust Modules in the kernel.
+
+ This is required to load external kernel modules written in Rust.
+
+ The two primary reasons to consider disabling this are:
+ * Allow MODVERSIONS support
+ * Allow additional code to be optimized out by the compiler if you
+ know that you'll only be using built-in Rust code.
+
+ If unsure, say Y.
+
config RUSTC_VERSION_TEXT
string
depends on RUST
diff --git a/kernel/module/Kconfig b/kernel/module/Kconfig
index 0ea1b2970a23..a76128887618 100644
--- a/kernel/module/Kconfig
+++ b/kernel/module/Kconfig
@@ -158,6 +158,7 @@ config MODULE_UNLOAD_TAINT_TRACKING
shown. If unsure, say N.

config MODVERSIONS
+ depends on !RUST_MODULES
bool "Module versioning support"
help
Usually, you have to use modules compiled with your kernel.
diff --git a/rust/exports.c b/rust/exports.c
index 83e2a7070cae..96b706048ed4 100644
--- a/rust/exports.c
+++ b/rust/exports.c
@@ -13,7 +13,11 @@

#include <linux/module.h>

+#ifdef CONFIG_RUST_MODULES
#define EXPORT_SYMBOL_RUST_GPL(sym) extern int sym; EXPORT_SYMBOL_GPL(sym)
+#else
+#define EXPORT_SYMBOL_RUST_GPL(sym)
+#endif

#include "exports_core_generated.h"
#include "exports_alloc_generated.h"
--
2.42.0.869.gea05f2083d-goog