Skip to contentSkip to conversation

Support MODVERSIONS by disabling Rust modules

PATCH In review Matthew Maurer 4 participants 7 messages Nov 8, 2023 – Nov 9, 2023

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover Support MODVERSIONS by disabling Rust modules

The goal of this patch series is to allow MODVERSIONS to be turned on alongside RUST. Gary Guo previously proposed to adjust the API to support Rust symbols' greater length:
https://lore.kernel.org/lkml/CANiq72n4MbR+AE7eHfVQZOu26FeSttQnEEMT3Jpft+CcGwk9jw@mail.gmail.com/T/

This did not land, so I'm proposing an alternate solution - users who want to use Rust, but who do not need Rust modules, can disable Rust module support in order to retain modversions support. This has the added bonus of removing exported symbols from the kernel surface if we know they won't be used due to lack of Rust module support in the environment.

Matthew Maurer (3):
kconfig: Extend expr_depends_symbol to recurse
kconfig: Add special rust_modules config option
rust: Require RUST_MODULES for module support

 init/Kconfig               | 21 +++++++++++++++++----
 kernel/module/Kconfig      |  1 +
 rust/exports.c             |  4 ++++
 scripts/kconfig/confdata.c |  3 +++
 scripts/kconfig/expr.c     | 22 ++++++++++++++++------
 scripts/kconfig/expr.h     |  3 ++-
 scripts/kconfig/lexer.l    |  1 +
 scripts/kconfig/lkc.h      |  1 +
 scripts/kconfig/menu.c     |  9 ++++++---
 scripts/kconfig/parser.y   | 12 ++++++++++++
 scripts/kconfig/symbol.c   | 31 +++++++++++++++++++++++++++++--
 11 files changed, 92 insertions(+), 16 deletions(-)
Signature · 1 line
2.42.0.869.gea05f2083d-goog
#1 lore raw

PATCH 1/3 kconfig: Extend expr_depends_symbol to recurse

Adds optional recursion support to expr_depends_symbol. If recurse is set to true, it will recurse through other defined symbols' dependencies to determine if the expression hard-depends on the provided argument through an multi-step dependency.

---
 scripts/kconfig/expr.c | 22 ++++++++++++++++------
 scripts/kconfig/expr.h |  2 +-
 scripts/kconfig/menu.c |  2 +-
 3 files changed, 18 insertions(+), 8 deletions(-)
scripts/kconfig/expr.c +16 -6
index 81ebf8108ca7..9d517b897378 100644
@@ -876,25 +876,35 @@ int expr_contains_symbol(struct expr *dep, struct symbol *sym)
 	return 0;
 }
 
-bool expr_depends_symbol(struct expr *dep, struct symbol *sym)
+static bool sym_depends_symbol(struct symbol *hay, struct symbol *needle, bool recurse) {
+	if (!hay)
+		return false;
+	if (hay == needle)
+		return true;
+	if (recurse)
+		return expr_depends_symbol(hay->dir_dep.expr, needle, recurse);
+	return false;
+}
+
+bool expr_depends_symbol(struct expr *dep, struct symbol *sym, bool recurse)
 {
 	if (!dep)
 		return false;
 
 	switch (dep->type) {
 	case E_AND:
-		return expr_depends_symbol(dep->left.expr, sym) ||
-		       expr_depends_symbol(dep->right.expr, sym);
+		return expr_depends_symbol(dep->left.expr, sym, recurse) ||
+		       expr_depends_symbol(dep->right.expr, sym, recurse);
 	case E_SYMBOL:
-		return dep->left.sym == sym;
+		return sym_depends_symbol(dep->left.sym, sym, recurse);
 	case E_EQUAL:
-		if (dep->left.sym == sym) {
+		if (sym_depends_symbol(dep->left.sym, sym, recurse)) {
 			if (dep->right.sym == &symbol_yes || dep->right.sym == &symbol_mod)
 				return true;
 		}
 		break;
 	case E_UNEQUAL:
-		if (dep->left.sym == sym) {
+		if (sym_depends_symbol(dep->left.sym, sym, recurse)) {
 			if (dep->right.sym == &symbol_no)
 				return true;
 		}
scripts/kconfig/expr.h +1 -1
index 4a9a23b1b7e1..edfe3046d050 100644
@@ -299,7 +299,7 @@ struct expr *expr_trans_bool(struct expr *e);
 struct expr *expr_eliminate_dups(struct expr *e);
 struct expr *expr_transform(struct expr *e);
 int expr_contains_symbol(struct expr *dep, struct symbol *sym);
-bool expr_depends_symbol(struct expr *dep, struct symbol *sym);
+bool expr_depends_symbol(struct expr *dep, struct symbol *sym, bool recurse);
 struct expr *expr_trans_compare(struct expr *e, enum expr_type type, struct symbol *sym);
 
 void expr_fprint(struct expr *e, FILE *out);
scripts/kconfig/menu.c +1 -1
index 61c442d84aef..d5898cd6aeb8 100644
@@ -443,7 +443,7 @@ void menu_finalize(struct menu *parent)
 			if (!expr_contains_symbol(dep, sym))
 				/* No dependency, quit */
 				break;
-			if (expr_depends_symbol(dep, sym))
+			if (expr_depends_symbol(dep, sym, false))
 				/* Absolute dependency, put in submenu */
 				goto next;
 
Signature · 1 line
2.42.0.869.gea05f2083d-goog
#2 lore raw

PATCH 2/3 kconfig: Add special rust_modules config option

In reply to Matthew Maurer

Adds support for the rust_modules kconfig type, which works similarly to modules, but for restricting or allowing the use of modules which directly depend on Rust.

---
 scripts/kconfig/confdata.c |  3 +++
 scripts/kconfig/expr.h     |  1 +
 scripts/kconfig/lexer.l    |  1 +
 scripts/kconfig/lkc.h      |  1 +
 scripts/kconfig/menu.c     |  7 +++++--
 scripts/kconfig/parser.y   | 12 ++++++++++++
 scripts/kconfig/symbol.c   | 31 +++++++++++++++++++++++++++++--
 7 files changed, 52 insertions(+), 4 deletions(-)
scripts/kconfig/confdata.c +3 -0
index 4a6811d77d18..f5e2dcb56e1c 100644
@@ -549,10 +549,12 @@ int conf_read(const char *name)
 
 	if (conf_read_simple(name, S_DEF_USER)) {
 		sym_calc_value(modules_sym);
+		sym_calc_value(modules_rust_sym);
 		return 1;
 	}
 
 	sym_calc_value(modules_sym);
+	sym_calc_value(modules_rust_sym);
 
 	for_all_symbols(i, sym) {
 		sym_calc_value(sym);
@@ -1090,6 +1092,7 @@ static int conf_touch_deps(void)
 
 	conf_read_simple(name, S_DEF_AUTO);
 	sym_calc_value(modules_sym);
+	sym_calc_value(modules_rust_sym);
 
 	for_all_symbols(i, sym) {
 		sym_calc_value(sym);
scripts/kconfig/expr.h +1 -0
index edfe3046d050..6b576fbc330e 100644
@@ -283,6 +283,7 @@ struct file *lookup_file(const char *name);
 
 extern struct symbol symbol_yes, symbol_no, symbol_mod;
 extern struct symbol *modules_sym;
+extern struct symbol *modules_rust_sym;
 extern int cdebug;
 struct expr *expr_alloc_symbol(struct symbol *sym);
 struct expr *expr_alloc_one(enum expr_type type, struct expr *ce);
scripts/kconfig/lexer.l +1 -0
index cc386e443683..d6c7fd11f07e 100644
@@ -114,6 +114,7 @@ n	[A-Za-z0-9_-]
 "optional"		return T_OPTIONAL;
 "prompt"		return T_PROMPT;
 "range"			return T_RANGE;
+"rust_modules"		return T_MODULES_RUST;
 "select"		return T_SELECT;
 "source"		return T_SOURCE;
 "string"		return T_STRING;
scripts/kconfig/lkc.h +1 -0
index 471a59acecec..ea97ff25909e 100644
@@ -108,6 +108,7 @@ void menu_get_ext_help(struct menu *menu, struct gstr *help);
 /* symbol.c */
 void sym_clear_all_valid(void);
 struct symbol *sym_choice_default(struct symbol *sym);
+bool sym_depends_rust(struct symbol *sym);
 struct property *sym_get_range_prop(struct symbol *sym);
 const char *sym_get_string_default(struct symbol *sym);
 struct symbol *sym_check_deps(struct symbol *sym);
scripts/kconfig/menu.c +5 -2
index d5898cd6aeb8..10a115f3a450 100644
@@ -96,8 +96,11 @@ static struct expr *rewrite_m(struct expr *e)
 		break;
 	case E_SYMBOL:
 		/* change 'm' into 'm' && MODULES */
-		if (e->left.sym == &symbol_mod)
-			return expr_alloc_and(e, expr_alloc_symbol(modules_sym));
+		if (e->left.sym == &symbol_mod) {
+			if (sym_depends_rust(e->left.sym))
+				e = expr_alloc_and(e, expr_alloc_symbol(modules_rust_sym));
+			e = expr_alloc_and(e, expr_alloc_symbol(modules_sym));
+		}
 		break;
 	default:
 		break;
scripts/kconfig/parser.y +12 -0
index 2af7ce4e1531..37d9e390f545 100644
@@ -68,6 +68,7 @@ struct menu *current_menu, *current_entry;
 %token T_MENU
 %token T_MENUCONFIG
 %token T_MODULES
+%token T_MODULES_RUST
 %token T_ON
 %token T_OPEN_PAREN
 %token T_OPTIONAL
@@ -224,6 +225,15 @@ config_option: T_MODULES T_EOL
 	modules_sym = current_entry->sym;
 };
 
+config_option: T_MODULES_RUST T_EOL
+{
+	if (modules_rust_sym)
+		zconf_error("symbol '%s' redefines option 'rust_modules' already defined by symbol '%s'",
+			    current_entry->sym->name, modules_rust_sym->name);
+	modules_rust_sym = current_entry->sym;
+};
+
+
 /* choice entry */
 
 choice: T_CHOICE word_opt T_EOL
@@ -495,6 +505,8 @@ void conf_parse(const char *name)
 		exit(1);
 	if (!modules_sym)
 		modules_sym = sym_find( "n" );
+	if (!modules_rust_sym)
+		modules_rust_sym = sym_find( "n" );
 
 	if (!menu_has_prompt(&rootmenu)) {
 		current_entry = &rootmenu;
scripts/kconfig/symbol.c +29 -2
index 0572330bf8a7..d48fbde936d1 100644
@@ -38,6 +38,18 @@ static struct symbol symbol_empty = {
 struct symbol *modules_sym;
 static tristate modules_val;
 
+struct symbol *modules_rust_sym;
+static tristate modules_rust_val;
+
+bool sym_depends_rust(struct symbol *sym)
+{
+	static struct symbol *rust_sym;
+
+	if (!rust_sym)
+		rust_sym = sym_find("RUST");
+	return expr_depends_symbol(sym->dir_dep.expr, rust_sym, true);
+}
+
 enum symbol_type sym_get_type(struct symbol *sym)
 {
 	enum symbol_type type = sym->type;
@@ -47,6 +59,8 @@ enum symbol_type sym_get_type(struct symbol *sym)
 			type = S_BOOLEAN;
 		else if (modules_val == no)
 			type = S_BOOLEAN;
+		else if ((modules_rust_val == no) && sym_depends_rust(sym))
+			type = S_BOOLEAN;
 	}
 	return type;
 }
@@ -443,6 +457,10 @@ void sym_calc_value(struct symbol *sym)
 			sym_set_all_changed();
 			modules_val = modules_sym->curr.tri;
 		}
+		if (modules_rust_sym == sym) {
+			sym_set_all_changed();
+			modules_rust_val = modules_rust_sym->curr.tri;
+		}
 	}
 
 	if (sym_is_choice(sym)) {
@@ -474,6 +492,7 @@ void sym_clear_all_valid(void)
 		sym->flags &= ~SYMBOL_VALID;
 	conf_set_changed(true);
 	sym_calc_value(modules_sym);
+	sym_calc_value(modules_rust_sym);
 }
 
 bool sym_tristate_within_range(struct symbol *sym, tristate val)
@@ -704,6 +723,7 @@ const char *sym_get_string_default(struct symbol *sym)
 
 	sym_calc_visibility(sym);
 	sym_calc_value(modules_sym);
+	sym_calc_value(modules_rust_sym);
 	val = symbol_no.curr.tri;
 	str = symbol_empty.curr.val;
 
@@ -734,9 +754,12 @@ const char *sym_get_string_default(struct symbol *sym)
 	val = EXPR_OR(val, sym->rev_dep.tri);
 
 	/* transpose mod to yes if modules are not enabled */
-	if (val == mod)
-		if (!sym_is_choice_value(sym) && modules_sym->curr.tri == no)
+	if ((val == mod) && !sym_is_choice_value(sym)) {
+		if (modules_sym->curr.tri == no)
+			val = yes;
+		if ((modules_rust_sym->curr.tri == no) && sym_depends_rust(sym))
 			val = yes;
+	}
 
 	/* transpose mod to yes if type is bool */
 	if (sym->type == S_BOOLEAN && val == mod)
@@ -778,7 +801,11 @@ const char *sym_get_string_value(struct symbol *sym)
 			return "n";
 		case mod:
 			sym_calc_value(modules_sym);
+			sym_calc_value(modules_rust_sym);
+			if (sym_depends_rust(sym))
+				return (modules_rust_sym->curr.tri == no) ? "n" : "m";
 			return (modules_sym->curr.tri == no) ? "n" : "m";
+
 		case yes:
 			return "y";
 		}
Signature · 1 line
2.42.0.869.gea05f2083d-goog
#3 lore raw

Re: [PATCH 2/3] kconfig: Add special rust_modules config option

Quoted 5 lines · Matthew Maurer

Adds support for the rust_modules kconfig type, which works similarly to modules, but for restricting or allowing the use of modules which directly depend on Rust.

Quoted 10 lines
---
 scripts/kconfig/confdata.c |  3 +++
 scripts/kconfig/expr.h     |  1 +
 scripts/kconfig/lexer.l    |  1 +
 scripts/kconfig/lkc.h      |  1 +
 scripts/kconfig/menu.c     |  7 +++++--
 scripts/kconfig/parser.y   | 12 ++++++++++++
 scripts/kconfig/symbol.c   | 31 +++++++++++++++++++++++++++++--
 7 files changed, 52 insertions(+), 4 deletions(-)

Hi,

This appears to be a change to the Kconfig language, so please update Documentation/kbuild/kconfig-language.rst with some prose explaining it.

Thanks.

Signature · 1 line
~Randy
#4 lore raw

Re: [PATCH 2/3] kconfig: Add special rust_modules config option

In reply to Matthew Maurer
Quoted 8 lines · Matthew Maurer

Adds support for the rust_modules kconfig type, which works similarly to modules, but for restricting or allowing the use of modules which directly depend on Rust.

[...]

scripts/kconfig/symbol.c +2 -0
+struct symbol *modules_rust_sym;
+static tristate modules_rust_val;

scripts/kconfig/symbol.c static struct symbol symbol_empty = { · line 52

Quoted 10 lines
scripts/kconfig/symbol.c +10 -0
+
+bool sym_depends_rust(struct symbol *sym)
+{
+	static struct symbol *rust_sym;
+
+	if (!rust_sym)
+		rust_sym = sym_find("RUST");
+	return expr_depends_symbol(sym->dir_dep.expr, rust_sym, true);
+}
+

Hm, this feels like a quick and dirty hack to me: will we be hardcoding a symbol for each language?

I know there is little sympathy for other projects that use Kconfig, and whatever Linux needs, goes, but still, this feels like a step too far.

#5 lore raw

PATCH 3/3 rust: Require RUST_MODULES for module support

In reply to Matthew Maurer

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.

---
 init/Kconfig          | 21 +++++++++++++++++----
 kernel/module/Kconfig |  1 +
 rust/exports.c        |  4 ++++
 3 files changed, 22 insertions(+), 4 deletions(-)
init/Kconfig +17 -4
index 9ffb103fc927..6912dbbee3f1 100644
@@ -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
kernel/module/Kconfig +1 -0
index 0ea1b2970a23..a76128887618 100644
@@ -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.
rust/exports.c +4 -0
index 83e2a7070cae..96b706048ed4 100644
@@ -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"
Signature · 1 line
2.42.0.869.gea05f2083d-goog
#6 lore raw
In reply to Matthew Maurer
Quoted 6 lines · Matthew Maurer

The goal of this patch series is to allow MODVERSIONS to be turned on alongside RUST. Gary Guo previously proposed to adjust the API to support Rust symbols' greater length:
https://lore.kernel.org/lkml/CANiq72n4MbR+AE7eHfVQZOu26FeSttQnEEMT3Jpft+CcGwk9jw@mail.gmail.com/T/

Quoted 6 lines

This did not land, so I'm proposing an alternate solution - users who want to use Rust, but who do not need Rust modules, can disable Rust module support in order to retain modversions support. This has the added bonus of removing exported symbols from the kernel surface if we know they won't be used due to lack of Rust module support in the environment.

No. This is not a solution.

It does not change the fact that Rust modules and modversions cannot coexist, and it is even worse than the previous one.

Signature · 2 lines
Best Regards
Masahiro Yamada