Skip to contentSkip to conversation

Extended MODVERSIONS Support

PATCH Superseded Matthew Maurer 4 participants 13 messages Oct 15, 2024 – Oct 28, 2024

Subsystems (4)

v6 of 13 Superseded Latest is v13 →

lore
Share & tools
lore
#0 lore raw

v6 cover Extended MODVERSIONS Support

This patch series is intended for use alongside the Implement MODVERSIONS for RUST [1] series as a replacement for the symbol name hashing approach used there to enable RUST and MODVERSIONS at the same time.

Elsewhere, we've seen a desire for long symbol name support for LTO symbol names [2], and the previous series came up [3] as a possible solution rather than hashing, which some have objected [4] to.

This series adds a MODVERSIONS format which uses a section per column. This avoids userspace tools breaking if we need to make a similar change to the format in the future - we would do so by adding a new section, rather than editing the struct definition. In the new format, the name section is formatted as a concatenated sequence of NUL-terminated strings, which allows for arbitrary length names.

Emitting the extended format is guarded by CONFIG_EXTENDED_MODVERSIONS, but the kernel always knows how to validate both the original and extended formats.

If you are testing this patch alongside RUST by manually removing the !MODVERSIONS restriction (this series doesn't remove it, because the CRCs don't mean what we'd want them to yet, we need the DWARF patch for that) and have kernel hardening enabled, you may need the CPU Mitigations [5] series. Without it, the foo.mod.o file produced by the C compiler will reference __x86_return_thunk, but foo.o will not. This means that the version table will not contain a version for __x86_return_thunk, but foo.ko will reference it, which will result in a version check failure.

This series depends upon the module verification refactor patches [6] that were split off of v5.

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/
[3] https://lore.kernel.org/lkml/[email protected]/
[4] https://lore.kernel.org/lkml/[email protected]/
[5] https://lore.kernel.org/all/[email protected]/
[6] https://lore.kernel.org/linux-modules/[email protected]/T/#t

Changes in v6:
- Splits verification refactor Luis requested out to a separate change
- Clarifies commits around export_report.pl repairs
- Add CONFIG_EXTENDED_MODVERSIONS to control whether extended information is included in the module, per Luis's request.

v5: https://lore.kernel.org/all/[email protected]/
- Addresses Sami's comments from v3 that I missed in v4 (missing early return, extra parens)

v4: https://lore.kernel.org/asahi/[email protected]/
- Fix incorrect dot munging in PPC

v3: https://lore.kernel.org/lkml/[email protected]/T/
- Split up the module verification refactor into smaller patches, per Greg K-H's suggestion.

v2: https://lore.kernel.org/all/[email protected]/
- Add loading/verification refactor before modifying, per Luis's request

v1: https://lore.kernel.org/rust-for-linux/[email protected]/

Matthew Maurer (5):
export_report: Rehabilitate script
modules: Support extended MODVERSIONS info
export_report: Tolerate additional .mod.c content
modpost: Produce extended MODVERSIONS information
export_report: Use new version info format

 arch/powerpc/kernel/module_64.c | 23 ++++++++-
 kernel/module/Kconfig           |  8 +++
 kernel/module/internal.h        | 11 ++++
 kernel/module/main.c            | 92 ++++++++++++++++++++++++++++++---
 kernel/module/version.c         | 45 ++++++++++++++++
 scripts/export_report.pl        | 17 +++---
 scripts/mod/modpost.c           | 41 +++++++++++++++
 7 files changed, 220 insertions(+), 17 deletions(-)
Signature · 1 line
2.47.0.rc1.288.g06298d1525-goog
#1 lore raw

PATCH v6 1/5 export_report: Rehabilitate script

The export_report.pl script was broken [1] a while back due to a code cleanup causing the regex to no longer match. Additionally, it assumes a modules.order file containing .ko in a build directory with .mod.c files. I cannot find when this would have been the case in the history, as normally .ko files only appear in modules.order in installed modules directories, and those do not contain .mod.c files. This patch makes it able to report symbol usage counts for a build tree with modules and MODVERSIONS.

Since the rest of this series will change the format of .mod.c, this change fixes the script to work correctly against a current build tree. Given that the regex no longer matches the format used in .mod.c, it cannot have worked since 2019, so updating this script is purely out of an abundance of caution. I am unsure who uses this script or for what purpose.

* modules.order in a build directory uses .o, not .ko files. Allow .o files when parsing modules.order.
* The .mod.c format changed [1] how it expressed the section attribute, leading to a regex mismatch. Update it to match modpost.c

[1]: https://lore.kernel.org/all/[email protected]/

---
 scripts/export_report.pl | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
scripts/export_report.pl +2 -1
index feb3d5542a62..30b5f7819086 100755
@@ -55,6 +55,7 @@ sub collectcfiles {
     open my $fh, '< modules.order' or die "cannot open modules.order: $!\n";
     while (<$fh>) {
 	s/\.ko$/.mod.c/;
+	s/\.o$/.mod.c/;
 	push (@file, $_)
     }
     close($fh);
@@ -120,7 +121,7 @@ foreach my $thismod (@allcfiles) {
 			next;
 		}
 		if ($state == 1) {
-			$state = 2 if ($_ =~ /__attribute__\(\(section\("__versions"\)\)\)/);
+			$state = 2 if ($_ =~ /__used __section\("__versions"\)/);
 			next;
 		}
 		if ($state == 2) {
Signature · 1 line
2.47.0.rc1.288.g06298d1525-goog
maintainer
#2 lore raw

Re: [PATCH v6 1/5] export_report: Rehabilitate script

Hi,

Quoted 17 lines · Matthew Maurer

The export_report.pl script was broken [1] a while back due to a code cleanup causing the regex to no longer match. Additionally, it assumes a modules.order file containing .ko in a build directory with .mod.c files. I cannot find when this would have been the case in the history, as normally .ko files only appear in modules.order in installed modules directories, and those do not contain .mod.c files. This patch makes it able to report symbol usage counts for a build tree with modules and MODVERSIONS.

Since the rest of this series will change the format of .mod.c, this change fixes the script to work correctly against a current build tree. Given that the regex no longer matches the format used in .mod.c, it cannot have worked since 2019, so updating this script is purely out of an abundance of caution. I am unsure who uses this script or for what purpose.

Quoted 6 lines

* modules.order in a build directory uses .o, not .ko files. Allow .o files when parsing modules.order.
* The .mod.c format changed [1] how it expressed the section attribute, leading to a regex mismatch. Update it to match modpost.c

[1]: https://lore.kernel.org/all/[email protected]/

If this script has been broken for half a decade and nobody noticed, does anyone actually use it? If this is dead code, I would prefer to just delete it.

Sami

maintainer
#3 lore raw

Re: [PATCH v6 1/5] export_report: Rehabilitate script

Quoted 25 lines · Sami Tolvanen

Hi,

Quoted 22 lines · Matthew Maurer

The export_report.pl script was broken [1] a while back due to a code cleanup causing the regex to no longer match. Additionally, it assumes a modules.order file containing .ko in a build directory with .mod.c files. I cannot find when this would have been the case in the history, as normally .ko files only appear in modules.order in installed modules directories, and those do not contain .mod.c files. This patch makes it able to report symbol usage counts for a build tree with modules and MODVERSIONS.

Since the rest of this series will change the format of .mod.c, this change fixes the script to work correctly against a current build tree. Given that the regex no longer matches the format used in .mod.c, it cannot have worked since 2019, so updating this script is purely out of an abundance of caution. I am unsure who uses this script or for what purpose.

* modules.order in a build directory uses .o, not .ko files. Allow .o files when parsing modules.order.
* The .mod.c format changed [1] how it expressed the section attribute, leading to a regex mismatch. Update it to match modpost.c

Quoted 5 lines
Quoted 1 line

[1]: https://lore.kernel.org/all/[email protected]/

If this script has been broken for half a decade and nobody noticed, does anyone actually use it? If this is dead code, I would prefer to just delete it.

I'm in full agreement, please trace back the history down to why the heck we have this, otherwise chuck it.

Luis

#4 lore raw

Re: [PATCH v6 1/5] export_report: Rehabilitate script

In reply to Matthew Maurer
Quoted 3 lines · Matthew Maurer

The export_report.pl script was broken [1] a while back due to a code cleanup causing the regex to no longer match.

Instead of the link to lore, you can refer to
commit a3d0cb04f7df ("modpost: use __section in the output to *.mod.c")

Quoted 5 lines

Additionally, it assumes a
modules.order file containing .ko in a build directory with .mod.c files. I cannot find when this would have been the case in the history, as normally .ko files only appear in modules.order in installed modules directories, and those do not contain .mod.c files.

If necessary, you can refer to
commit f65a486821cf ("kbuild: change module.order to list *.o instead of *.ko")

As suggested, I vote for the removal since it has been broken for 5 years since a3d0cb04f7df.

Signature · 2 lines
Best Regards
Masahiro Yamada
#5 lore raw

PATCH v6 2/5 modules: Support extended MODVERSIONS info

In reply to Matthew Maurer

Adds a new format for MODVERSIONS which stores each field in a separate ELF section. This initially adds support for variable length names, but could later be used to add additional fields to MODVERSIONS in a backwards compatible way if needed. Any new fields will be ignored by old user tooling, unlike the current format where user tooling cannot tolerate adjustments to the format (for example making the name field longer).

Since PPC munges its version records to strip leading dots, we reproduce the munging for the new format. Other architectures do not appear to have architecture-specific usage of this information.

---
 arch/powerpc/kernel/module_64.c | 23 ++++++++-
 kernel/module/internal.h        | 11 ++++
 kernel/module/main.c            | 92 ++++++++++++++++++++++++++++++---
 kernel/module/version.c         | 45 ++++++++++++++++
 4 files changed, 161 insertions(+), 10 deletions(-)
arch/powerpc/kernel/module_64.c +21 -2
index 135960918d14..0f7ef7e7e9d6 100644
@@ -355,6 +355,23 @@ static void dedotify_versions(struct modversion_info *vers,
 		}
 }
 
+static void dedotify_ext_version_names(char *str_seq, unsigned long size)
+{
+	unsigned long out = 0;
+	unsigned long in;
+	char last = '\0';
+
+	for (in = 0; in < size; in++) {
+		/* Skip one leading dot */
+		if (last == '\0' && str_seq[in] == '.')
+			in++;
+		last = str_seq[in];
+		str_seq[out++] = last;
+	}
+	/* Zero the trailing portion of the names table for robustness */
+	memset(&str_seq[out], 0, size - out);
+}
+
 /*
  * Undefined symbols which refer to .funcname, hack to funcname. Make .TOC.
  * seem to be defined (value set later).
@@ -424,10 +441,12 @@ int module_frob_arch_sections(Elf64_Ehdr *hdr,
 			me->arch.toc_section = i;
 			if (sechdrs[i].sh_addralign < 8)
 				sechdrs[i].sh_addralign = 8;
-		}
-		else if (strcmp(secstrings+sechdrs[i].sh_name,"__versions")==0)
+		} else if (strcmp(secstrings + sechdrs[i].sh_name, "__versions") == 0)
 			dedotify_versions((void *)hdr + sechdrs[i].sh_offset,
 					  sechdrs[i].sh_size);
+		else if (strcmp(secstrings + sechdrs[i].sh_name, "__version_ext_names") == 0)
+			dedotify_ext_version_names((void *)hdr + sechdrs[i].sh_offset,
+						   sechdrs[i].sh_size);
 
 		if (sechdrs[i].sh_type == SHT_SYMTAB)
 			dedotify((void *)hdr + sechdrs[i].sh_offset,
kernel/module/internal.h +11 -0
index daef2be83902..59959c21b205 100644
@@ -86,6 +86,8 @@ struct load_info {
 		unsigned int vers;
 		unsigned int info;
 		unsigned int pcpu;
+		unsigned int vers_ext_crc;
+		unsigned int vers_ext_name;
 	} index;
 };
 
@@ -389,6 +391,15 @@ void module_layout(struct module *mod, struct modversion_info *ver, struct kerne
 		   struct kernel_symbol *ks, struct tracepoint * const *tp);
 int check_modstruct_version(const struct load_info *info, struct module *mod);
 int same_magic(const char *amagic, const char *bmagic, bool has_crcs);
+struct modversion_info_ext {
+	size_t remaining;
+	const s32 *crc;
+	const char *name;
+};
+void modversion_ext_start(const struct load_info *info, struct modversion_info_ext *ver);
+void modversion_ext_advance(struct modversion_info_ext *ver);
+#define for_each_modversion_info_ext(ver, info) \
+	for (modversion_ext_start(info, &ver); ver.remaining > 0; modversion_ext_advance(&ver))
 #else /* !CONFIG_MODVERSIONS */
 static inline int check_version(const struct load_info *info,
 				const char *symname,
kernel/module/main.c +84 -8

116 lines not on this page · raw message

kernel/module/version.c +45 -0

62 lines not on this page · raw message

Signature · 1 line
2.47.0.rc1.288.g06298d1525-goog
maintainer
#6 lore raw

Re: [PATCH v6 2/5] modules: Support extended MODVERSIONS info

Quoted 11 lines · Matthew Maurer

Adds a new format for MODVERSIONS which stores each field in a separate ELF section. This initially adds support for variable length names, but could later be used to add additional fields to MODVERSIONS in a backwards compatible way if needed. Any new fields will be ignored by old user tooling, unlike the current format where user tooling cannot tolerate adjustments to the format (for example making the name field longer).

Since PPC munges its version records to strip leading dots, we reproduce the munging for the new format. Other architectures do not appear to have architecture-specific usage of this information.

I'd like some PPC person to review this, and also please document this under dedotify_ext_version_names().

Luis

#7 lore raw

PATCH v6 3/5 export_report: Tolerate additional `.mod.c` content

In reply to Matthew Maurer

Currently, export_report.pl will error out if it sees a hex number not in the context of the original __versions array. This adds a "finished" state so that it does not attempt to parse content past the end of the array, and requires the array to be terminated.

This is prepwork for the subsequent extended modversions information, which would not be parseable otherwise.

---
 scripts/export_report.pl | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
scripts/export_report.pl +5 -1
index 30b5f7819086..dcef915405f3 100755
@@ -125,6 +125,10 @@ foreach my $thismod (@allcfiles) {
 			next;
 		}
 		if ($state == 2) {
+			if ( $_ =~ /};/ ) {
+				$state = 3;
+				next;
+			}
 			if ( $_ !~ /0x[0-9a-f]+,/ ) {
 				next;
 			}
@@ -134,7 +138,7 @@ foreach my $thismod (@allcfiles) {
 			push(@{$MODULE{$thismod}} , $sym);
 		}
 	}
-	if ($state != 2) {
+	if ($state != 3) {
 		warn "WARNING:$thismod is not built with CONFIG_MODVERSIONS enabled\n";
 		$modversion_warnings++;
 	}
Signature · 1 line
2.47.0.rc1.288.g06298d1525-goog
#8 lore raw

PATCH v6 4/5 modpost: Produce extended MODVERSIONS information

In reply to Matthew Maurer

Generate both the existing modversions format and the new extended one when running modpost. Presence of this metadata in the final .ko is guarded by CONFIG_EXTENDED_MODVERSIONS.

We no longer generate an error on long symbols in modpost if CONFIG_EXTENDED_MODVERSIONS is set, as they can now be appropriately encoded in the extended section. These symbols will be skipped in the previous encoding. An error will still be generated if CONFIG_EXTENDED_MODVERSIONS is not set.

---
 kernel/module/Kconfig |  8 ++++++++
 scripts/mod/modpost.c | 41 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)
kernel/module/Kconfig +8 -0
index 7c6588148d42..a5de2b7f2758 100644
@@ -177,6 +177,14 @@ config ASM_MODVERSIONS
 	  assembly. This can be enabled only when the target architecture
 	  supports it.
 
+config EXTENDED_MODVERSIONS
+	bool "Extended Module Versioning Support"
+	depends on MODVERSIONS
+	help
+	  This enables extended MODVERSIONs support, allowing long symbol
+	  names to be versioned. The most likely reasons you would enable
+	  this are for Rust usage or aggressive LTO configurations.
+
 config MODULE_SRCVERSION_ALL
 	bool "Source checksum for all modules"
 	help
scripts/mod/modpost.c +41 -0

56 lines not on this page · raw message

Signature · 1 line
2.47.0.rc1.288.g06298d1525-goog
maintainer
#9 lore raw

Re: [PATCH v6 4/5] modpost: Produce extended MODVERSIONS information

Hi Matt,

Quoted 7 lines · Matthew Maurer
scripts/mod/modpost.c +0 -0
index 107393a8c48a..d18ff8a1109a 100644
@@ -1840,15 +1840,56 @@ static void add_versions(struct buffer *b, struct module *mod)
                        continue;

scripts/mod/modpost.c static void add_versions(struct buffer *b, struct module *mod) · line 1850

Quoted 10 lines
scripts/mod/modpost.c +5 -0
                }
                if (strlen(s->name) >= MODULE_NAME_LEN) {
+#ifdef CONFIG_EXTENDED_MODVERSIONS
+                       /* this symbol will only be in the extended info */
+                       continue;
+#else
                        error("too long symbol \"%s\" [%s.ko]\n",
                              s->name, mod->name);
                        break;
+#endif

I applied these patches to my test tree[1] and have the following options enabled in my .config:

$ grep -E 'MODVERSIONS|GEN.*KSYMS' .config
CONFIG_HAVE_ASM_MODVERSIONS=y
CONFIG_MODVERSIONS=y
# CONFIG_GENKSYMS is not set
CONFIG_GENDWARFKSYMS=y
CONFIG_ASM_MODVERSIONS=y
CONFIG_EXTENDED_MODVERSIONS=y

When I try to build a Rust module, I still get the following error:

ERROR: modpost: too long symbol "_RNvXs2_NtNtNtCsivAAjSnxUpM_4core3fmt3num3implNtB9_7Display3fmt" [samples/rust/rust_print.ko]

I suspect gating this behind a config would require you to add a new command line flag to modpost and check for CONFIG_EXTENDED_MODVERSIONS in scripts/Makefile.modpost instead.

[1] https://github.com/samitolvanen/linux/commits/rustmodversions

Sami

maintainer
#10 lore raw

Re: [PATCH v6 4/5] modpost: Produce extended MODVERSIONS information

In reply to Matthew Maurer
Quoted 21 lines · Matthew Maurer

Generate both the existing modversions format and the new extended one when running modpost. Presence of this metadata in the final .ko is guarded by CONFIG_EXTENDED_MODVERSIONS.

We no longer generate an error on long symbols in modpost if CONFIG_EXTENDED_MODVERSIONS is set, as they can now be appropriately encoded in the extended section. These symbols will be skipped in the previous encoding. An error will still be generated if CONFIG_EXTENDED_MODVERSIONS is not set.

---
 kernel/module/Kconfig |  8 ++++++++
 scripts/mod/modpost.c | 41 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)
kernel/module/Kconfig +0 -0
index 7c6588148d42..a5de2b7f2758 100644

kernel/module/Kconfig config ASM_MODVERSIONS · line 186

Quoted 10 lines
kernel/module/Kconfig +7 -0
@@ -177,6 +177,14 @@ config ASM_MODVERSIONS
 	  assembly. This can be enabled only when the target architecture
 	  supports it.
 
+config EXTENDED_MODVERSIONS
+	bool "Extended Module Versioning Support"
+	depends on MODVERSIONS
+	help
+	  This enables extended MODVERSIONs support, allowing long symbol
+	  names to be versioned. The most likely reasons you would enable
+	  this are for Rust usage or aggressive LTO configurations.

What is "aggressive LTO configurations" please elaborate. Can we infer on that through configuration?

Quoted 11 lines
kernel/module/Kconfig +1 -0
+
 config MODULE_SRCVERSION_ALL
 	bool "Source checksum for all modules"
 	help
scripts/mod/modpost.c +0 -0
index 107393a8c48a..d18ff8a1109a 100644
@@ -1840,15 +1840,56 @@ static void add_versions(struct buffer *b, struct module *mod)
 			continue;
 		}

scripts/mod/modpost.c static void add_versions(struct buffer *b, struct module *mod) · line 1851

Quoted 10 lines
scripts/mod/modpost.c +5 -0
 		if (strlen(s->name) >= MODULE_NAME_LEN) {
+#ifdef CONFIG_EXTENDED_MODVERSIONS
+			/* this symbol will only be in the extended info */
+			continue;
+#else
 			error("too long symbol \"%s\" [%s.ko]\n",
 			      s->name, mod->name);
 			break;
+#endif
 		}

Using #ifdefs on a loop like this seems fragile, even if its more code make the code clearer and use separate routines for both worlds. Make the code easy to review and maintain.

scripts/mod/modpost.c static void add_versions(struct buffer *b, struct module *mod) · line 1858

Quoted 7 lines
scripts/mod/modpost.c +2 -0
 		buf_printf(b, "\t{ %#8x, \"%s\" },\n",
 			   s->crc, s->name);
 	}
 
 	buf_printf(b, "};\n");
+
+	buf_printf(b, "#ifdef CONFIG_EXTENDED_MODVERSIONS\n");

Why not *in-code* rather than the output? And if possible why not two routines as above.

Luis

maintainer
#11 lore raw

Re: [PATCH v6 4/5] modpost: Produce extended MODVERSIONS information

In reply to Matthew Maurer
Quoted 11 lines · Matthew Maurer

Generate both the existing modversions format and the new extended one when running modpost. Presence of this metadata in the final .ko is guarded by CONFIG_EXTENDED_MODVERSIONS.

We no longer generate an error on long symbols in modpost if CONFIG_EXTENDED_MODVERSIONS is set, as they can now be appropriately encoded in the extended section. These symbols will be skipped in the previous encoding. An error will still be generated if CONFIG_EXTENDED_MODVERSIONS is not set.

If nothing is selecting EXTENDED_MODVERSIONS then this is dead code and should not be submitted unless we have a user, if that's the case then this series should be folded into Sami's.

Luis

#12 lore raw

PATCH v6 5/5 export_report: Use new version info format

In reply to Matthew Maurer

The new version info format has a superset of symbols in the old format. Since this is a tool for in-tree modules, we don't need to parse the old one with this tool any longer.

Even if we build without CONFIG_EXTENDED_MODVERSIONS, these are still in the .mod.c file. Their presence in the final module is controlled by conditional compilation inside the at file.

---
 scripts/export_report.pl | 22 ++++++++++------------
 1 file changed, 10 insertions(+), 12 deletions(-)
scripts/export_report.pl +10 -12
index dcef915405f3..6a37df6f947f 100755
@@ -114,31 +114,29 @@ foreach my $thismod (@allcfiles) {
 	}
 
 	my $state=0;
+	# State map:
+	# 0 - Looking for names
+	# 1 - Scanning names
+	# 2 - Done
 	while ( <$module> ) {
 		chomp;
 		if ($state == 0) {
-			$state = 1 if ($_ =~ /static const struct modversion_info/);
+			$state = 1 if ($_ =~ /__used __section\("__version_ext_names"\)/);
 			next;
 		}
 		if ($state == 1) {
-			$state = 2 if ($_ =~ /__used __section\("__versions"\)/);
-			next;
-		}
-		if ($state == 2) {
-			if ( $_ =~ /};/ ) {
-				$state = 3;
-				next;
-			}
-			if ( $_ !~ /0x[0-9a-f]+,/ ) {
+			if ( $_ =~ /;/ ) {
+				$state = 2;
 				next;
 			}
-			my $sym = (split /([,"])/,)[4];
+			$_ =~ /"(.*)\\0"/;
+			my $sym = $1;
 			my ($module, $value, $symbol, $gpl) = @{$SYMBOL{$sym}};
 			$SYMBOL{ $sym } =  [ $module, $value+1, $symbol, $gpl];
 			push(@{$MODULE{$thismod}} , $sym);
 		}
 	}
-	if ($state != 3) {
+	if ($state != 2) {
 		warn "WARNING:$thismod is not built with CONFIG_MODVERSIONS enabled\n";
 		$modversion_warnings++;
 	}
Signature · 1 line
2.47.0.rc1.288.g06298d1525-goog