Skip to contentSkip to conversation

bpf: add config to allow loading modules with BTF mismatches

PATCH Applied Connor O'Brien 5 participants 5 messages Feb 23, 2022 – Feb 28, 2022

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

PATCH 1/1 bpf: add config to allow loading modules with BTF mismatches

BTF mismatch can occur for a separately-built module even when the ABI is otherwise compatible and nothing else would prevent successfully loading. Add a new config to control how mismatches are handled. By default, preserve the current behavior of refusing to load the module. If MODULE_ALLOW_BTF_MISMATCH is enabled, load the module but ignore its BTF information.

---
Hello,

In the discussion regarding BTF compatibility & modules, there seemed to be broad agreement that an option to ignore mismatches would be reasonable. Currently the only option for handling this problem seems to be to disable BTF entirely, so this would at least be an incremental improvement.

Thanks,
Connor

 kernel/bpf/btf.c  |  3 ++-
 lib/Kconfig.debug | 10 ++++++++++
 2 files changed, 12 insertions(+), 1 deletion(-)
kernel/bpf/btf.c +2 -1
index 11740b300de9..1a21f24105b3 100644
@@ -6397,7 +6397,8 @@ static int btf_module_notify(struct notifier_block *nb, unsigned long op,
 			pr_warn("failed to validate module [%s] BTF: %ld\n",
 				mod->name, PTR_ERR(btf));
 			kfree(btf_mod);
-			err = PTR_ERR(btf);
+			if (!IS_ENABLED(CONFIG_MODULE_ALLOW_BTF_MISMATCH))
+				err = PTR_ERR(btf);
 			goto out;
 		}
 		err = btf_alloc_id(btf);
lib/Kconfig.debug +10 -0
index 1555da672275..ff857bb7d633 100644
@@ -339,6 +339,16 @@ config DEBUG_INFO_BTF_MODULES
 	help
 	  Generate compact split BTF type information for kernel modules.
 
+config MODULE_ALLOW_BTF_MISMATCH
+	bool "Allow loading modules with non-matching BTF type info"
+	depends on DEBUG_INFO_BTF_MODULES
+	help
+	  For modules whose split BTF does not match vmlinux, load without
+	  BTF rather than refusing to load. The default behavior with
+	  module BTF enabled is to reject modules with such mismatches;
+	  this option will still load module BTF where possible but ignore
+	  it when a mismatch is found.
+
 config GDB_SCRIPTS
 	bool "Provide GDB scripts for kernel debugging"
 	help
Signature · 1 line
2.35.1.473.g83b2b277ed-goog
Acked-by on v1 1/1
#1 lore raw
Quoted 10 lines · Connor O'Brien

BTF mismatch can occur for a separately-built module even when the ABI is otherwise compatible and nothing else would prevent successfully loading. Add a new config to control how mismatches are handled. By default, preserve the current behavior of refusing to load the module. If MODULE_ALLOW_BTF_MISMATCH is enabled, load the module but ignore its BTF information.

Maybe reference the discussion thread as well?

Otherwise

Quoted 11 lines

---
Hello,

In the discussion regarding BTF compatibility & modules, there seemed to be broad agreement that an option to ignore mismatches would be reasonable. Currently the only option for handling this problem seems to be to disable BTF entirely, so this would at least be an incremental improvement.

Thanks,
Connor

Acked-by
#2 lore raw
Quoted 16 lines · Shung-Hsi Yu
Quoted 10 lines · Connor O'Brien

BTF mismatch can occur for a separately-built module even when the ABI is otherwise compatible and nothing else would prevent successfully loading. Add a new config to control how mismatches are handled. By default, preserve the current behavior of refusing to load the module. If MODULE_ALLOW_BTF_MISMATCH is enabled, load the module but ignore its BTF information.

Quoted 7 lines

Maybe reference the discussion thread as well?

Otherwise

maintainer
#3 lore raw
In reply to Shung-Hsi Yu
Quoted 8 lines · Shung-Hsi Yu
Quoted 7 lines · Connor O'Brien

BTF mismatch can occur for a separately-built module even when the ABI is otherwise compatible and nothing else would prevent successfully loading. Add a new config to control how mismatches are handled. By default, preserve the current behavior of refusing to load the module. If MODULE_ALLOW_BTF_MISMATCH is enabled, load the module but ignore its BTF information.

Quoted 7 lines
Quoted 3 lines

Maybe reference the discussion thread as well?

LGTM, and added above into the commit log while applying. Thanks everyone!

#4 lore raw
In reply to Connor O'Brien
This patch was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:

Quoted 8 lines · On Wed, 23 Feb 2022 01:28:14 +0000 you wrote:

BTF mismatch can occur for a separately-built module even when the ABI is otherwise compatible and nothing else would prevent successfully loading. Add a new config to control how mismatches are handled. By default, preserve the current behavior of refusing to load the module. If MODULE_ALLOW_BTF_MISMATCH is enabled, load the module but ignore its BTF information.

[...]

Here is the summary with links:
- [bpf-next] bpf: add config to allow loading modules with BTF mismatches

    https://git.kernel.org/bpf/bpf-next/c/5e214f2e43e4

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html