Skip to contentSkip to conversation

kbuild: enable objtool for more objects and re-enable KCSAN for *.mod.o

PATCH In review Masahiro Yamada 2 participants 7 messages Nov 13, 2024 – Nov 19, 2024 Kbuild

lore
Share & tools
lore
#0 lore raw

cover kbuild: enable objtool for more objects and re-enable KCSAN for *.mod.o

Now, objtool will have better coverage, and we can revert 54babdc0343f.

Masahiro Yamada (3):
kbuild: move cmd_cc_o_c and cmd_as_o_S to scripts/Malefile.lib
kbuild: enable objtool for *.mod.o and additional kernel objects
kbuild: re-enable KCSAN for autogenerated *.mod.c intermediaries

 scripts/Makefile.build    | 25 -------------------------
 scripts/Makefile.lib      | 31 +++++++++++++++++++++++++++++++
 scripts/Makefile.modfinal | 11 ++++-------
 scripts/Makefile.vmlinux  | 12 +++---------
 4 files changed, 38 insertions(+), 41 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/3 kbuild: move cmd_cc_o_c and cmd_as_o_S to scripts/Malefile.lib

The cmd_cc_o_c and cmd_as_o_S macros are duplicated in scripts/Makefile.{build,modfinal,vmlinux}.

This commit factors them out to scripts/Makefile.lib.

No functional changes are intended.

---

 scripts/Makefile.build    |  8 --------
 scripts/Makefile.lib      | 12 ++++++++++++
 scripts/Makefile.modfinal |  6 ++----
 scripts/Makefile.vmlinux  |  8 +-------
 4 files changed, 15 insertions(+), 19 deletions(-)
scripts/Makefile.build +0 -8
index 1aa928a6fb4f..24e10c821461 100644
@@ -141,11 +141,6 @@ ifdef CONFIG_LTO_CLANG
 cmd_ld_single_m = $(if $(is-single-obj-m), ; $(LD) $(ld_flags) -r -o $(tmp-target) $@; mv $(tmp-target) $@)
 endif
 
-quiet_cmd_cc_o_c = CC $(quiet_modtag)  $@
-      cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $< \
-		$(cmd_ld_single_m) \
-		$(cmd_objtool)
-
 ifdef CONFIG_MODVERSIONS
 # When module versioning is enabled the following steps are executed:
 # o compile a <file>.o from <file>.c
@@ -336,9 +331,6 @@ cmd_cpp_s_S       = $(CPP) $(a_flags) -o $@ $<
 $(obj)/%.s: $(obj)/%.S FORCE
 	$(call if_changed_dep,cpp_s_S)
 
-quiet_cmd_as_o_S = AS $(quiet_modtag)  $@
-      cmd_as_o_S = $(CC) $(a_flags) -c -o $@ $< $(cmd_objtool)
-
 ifdef CONFIG_ASM_MODVERSIONS
 
 # versioning matches the C process described above, with difference that
scripts/Makefile.lib +12 -0
index 5660dfc9ed36..73e385946855 100644
@@ -298,6 +298,18 @@ $(foreach m, $1, \
 	$(addprefix $(obj)/, $(call suffix-search, $(patsubst $(obj)/%,%,$m), $2, $3))))
 endef
 
+# Build commads
+# ===========================================================================
+# These are shared by some Makefile.* files.
+
+quiet_cmd_cc_o_c = CC $(quiet_modtag)  $@
+      cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $< \
+		$(cmd_ld_single_m) \
+		$(cmd_objtool)
+
+quiet_cmd_as_o_S = AS $(quiet_modtag)  $@
+      cmd_as_o_S = $(CC) $(a_flags) -c -o $@ $< $(cmd_objtool)
+
 # Copy a file
 # ===========================================================================
 # 'cp' preserves permissions. If you use it to copy a file in read-only srctree,
scripts/Makefile.modfinal +2 -4
index bab53884f7e3..d0153d033bbb 100644
@@ -9,7 +9,7 @@ __modfinal:
 include $(objtree)/include/config/auto.conf
 include $(srctree)/scripts/Kbuild.include
 
-# for c_flags
+# include some build commands
 include $(srctree)/scripts/Makefile.lib
 
 # find all modules listed in modules.order
@@ -23,9 +23,7 @@ modname = $(notdir $(@:.mod.o=))
 part-of-module = y
 GCOV_PROFILE := n
 KCSAN_SANITIZE := n
-
-quiet_cmd_cc_o_c = CC [M]  $@
-      cmd_cc_o_c = $(CC) $(filter-out $(CC_FLAGS_CFI), $(c_flags)) -c -o $@ $<
+ccflags-remove-y := $(CC_FLAGS_CFI)
 
 %.mod.o: %.mod.c FORCE
 	$(call if_changed_dep,cc_o_c)
scripts/Makefile.vmlinux +1 -7
index 9ef0480ed755..1652561896eb 100644
@@ -6,20 +6,14 @@ __default: vmlinux
 include include/config/auto.conf
 include $(srctree)/scripts/Kbuild.include
 
-# for c_flags
+# include some build commands
 include $(srctree)/scripts/Makefile.lib
 
 targets :=
 
-quiet_cmd_cc_o_c = CC      $@
-      cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<
-
 %.o: %.c FORCE
 	$(call if_changed_dep,cc_o_c)
 
-quiet_cmd_as_o_S = AS      $@
-      cmd_as_o_S = $(CC) $(a_flags) -c -o $@ $<
-
 %.o: %.S FORCE
 	$(call if_changed_dep,as_o_S)
 
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH 1/3] kbuild: move cmd_cc_o_c and cmd_as_o_S to scripts/Malefile.lib

Quoted 48 lines · Masahiro Yamada

The cmd_cc_o_c and cmd_as_o_S macros are duplicated in scripts/Makefile.{build,modfinal,vmlinux}.

This commit factors them out to scripts/Makefile.lib.

No functional changes are intended.

---

 scripts/Makefile.build    |  8 --------
 scripts/Makefile.lib      | 12 ++++++++++++
 scripts/Makefile.modfinal |  6 ++----
 scripts/Makefile.vmlinux  |  8 +-------
 4 files changed, 15 insertions(+), 19 deletions(-)
scripts/Makefile.build +0 -8
index 1aa928a6fb4f..24e10c821461 100644
@@ -141,11 +141,6 @@ ifdef CONFIG_LTO_CLANG
 cmd_ld_single_m = $(if $(is-single-obj-m), ; $(LD) $(ld_flags) -r -o $(tmp-target) $@; mv $(tmp-target) $@)
 endif
 
-quiet_cmd_cc_o_c = CC $(quiet_modtag)  $@
-      cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $< \
-               $(cmd_ld_single_m) \
-               $(cmd_objtool)
-
 ifdef CONFIG_MODVERSIONS
 # When module versioning is enabled the following steps are executed:
 # o compile a <file>.o from <file>.c
@@ -336,9 +331,6 @@ cmd_cpp_s_S       = $(CPP) $(a_flags) -o $@ $<
 $(obj)/%.s: $(obj)/%.S FORCE
        $(call if_changed_dep,cpp_s_S)
 
-quiet_cmd_as_o_S = AS $(quiet_modtag)  $@
-      cmd_as_o_S = $(CC) $(a_flags) -c -o $@ $< $(cmd_objtool)
-
 ifdef CONFIG_ASM_MODVERSIONS
 
 # versioning matches the C process described above, with difference that
scripts/Makefile.lib +0 -0
index 5660dfc9ed36..73e385946855 100644

scripts/Makefile.lib $(foreach m, $1, \ · line 301

Quoted 4 lines
scripts/Makefile.lib +1 -0
@@ -298,6 +298,18 @@ $(foreach m, $1, \
        $(addprefix $(obj)/, $(call suffix-search, $(patsubst $(obj)/%,%,$m), $2, $3))))
 endef
 
+# Build commads

I will fix the typo.

s/commads/commands/

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

PATCH 2/3 kbuild: enable objtool for *.mod.o and additional kernel objects

In reply to Masahiro Yamada

Currently, objtool is disabled in scripts/Makefile.{modfinal,vmlinux}.

This commit moves rule_cc_o_c and rule_as_o_S to scripts/Makefile.lib and set objtool-enabled to y there.

With this change, *.mod.o, .module-common.o, builtin-dtb.o, and vmlinux.export.o will now be covered by objtool.

---

 scripts/Makefile.build    | 17 -----------------
 scripts/Makefile.lib      | 19 +++++++++++++++++++
 scripts/Makefile.modfinal |  4 ++--
 scripts/Makefile.vmlinux  |  4 ++--
 4 files changed, 23 insertions(+), 21 deletions(-)
scripts/Makefile.build +0 -17
index 24e10c821461..18b76947fe96 100644
@@ -202,23 +202,6 @@ ifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),)
 cmd_warn_shared_object = $(if $(word 2, $(modname-multi)),$(warning $(kbuild-file): $*.o is added to multiple modules: $(modname-multi)))
 endif
 
-define rule_cc_o_c
-	$(call cmd_and_fixdep,cc_o_c)
-	$(call cmd,checksrc)
-	$(call cmd,checkdoc)
-	$(call cmd,gen_objtooldep)
-	$(call cmd,gen_symversions_c)
-	$(call cmd,record_mcount)
-	$(call cmd,warn_shared_object)
-endef
-
-define rule_as_o_S
-	$(call cmd_and_fixdep,as_o_S)
-	$(call cmd,gen_objtooldep)
-	$(call cmd,gen_symversions_S)
-	$(call cmd,warn_shared_object)
-endef
-
 # Built-in and composite module parts
 $(obj)/%.o: $(obj)/%.c $(recordmcount_source) FORCE
 	$(call if_changed_rule,cc_o_c)
scripts/Makefile.lib +19 -0
index 73e385946855..17c81c346e36 100644
@@ -302,14 +302,33 @@ endef
 # ===========================================================================
 # These are shared by some Makefile.* files.
 
+objtool-enabled := y
+
 quiet_cmd_cc_o_c = CC $(quiet_modtag)  $@
       cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $< \
 		$(cmd_ld_single_m) \
 		$(cmd_objtool)
 
+define rule_cc_o_c
+	$(call cmd_and_fixdep,cc_o_c)
+	$(call cmd,checksrc)
+	$(call cmd,checkdoc)
+	$(call cmd,gen_objtooldep)
+	$(call cmd,gen_symversions_c)
+	$(call cmd,record_mcount)
+	$(call cmd,warn_shared_object)
+endef
+
 quiet_cmd_as_o_S = AS $(quiet_modtag)  $@
       cmd_as_o_S = $(CC) $(a_flags) -c -o $@ $< $(cmd_objtool)
 
+define rule_as_o_S
+	$(call cmd_and_fixdep,as_o_S)
+	$(call cmd,gen_objtooldep)
+	$(call cmd,gen_symversions_S)
+	$(call cmd,warn_shared_object)
+endef
+
 # Copy a file
 # ===========================================================================
 # 'cp' preserves permissions. If you use it to copy a file in read-only srctree,
scripts/Makefile.modfinal +2 -2
index d0153d033bbb..0547a4b59f13 100644
@@ -26,10 +26,10 @@ KCSAN_SANITIZE := n
 ccflags-remove-y := $(CC_FLAGS_CFI)
 
 %.mod.o: %.mod.c FORCE
-	$(call if_changed_dep,cc_o_c)
+	$(call if_changed_rule,cc_o_c)
 
 .module-common.o: $(srctree)/scripts/module-common.c FORCE
-	$(call if_changed_dep,cc_o_c)
+	$(call if_changed_rule,cc_o_c)
 
 quiet_cmd_ld_ko_o = LD [M]  $@
       cmd_ld_ko_o =							\
scripts/Makefile.vmlinux +2 -2
index 1652561896eb..83fc1a861f41 100644
@@ -12,10 +12,10 @@ include $(srctree)/scripts/Makefile.lib
 targets :=
 
 %.o: %.c FORCE
-	$(call if_changed_dep,cc_o_c)
+	$(call if_changed_rule,cc_o_c)
 
 %.o: %.S FORCE
-	$(call if_changed_dep,as_o_S)
+	$(call if_changed_rule,as_o_S)
 
 # Built-in dtb
 # ---------------------------------------------------------------------------
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH 2/3] kbuild: enable objtool for *.mod.o and additional kernel objects

Hi Masahiro,

Quoted 9 lines · Masahiro Yamada

Currently, objtool is disabled in scripts/Makefile.{modfinal,vmlinux}.

This commit moves rule_cc_o_c and rule_as_o_S to scripts/Makefile.lib and set objtool-enabled to y there.

With this change, *.mod.o, .module-common.o, builtin-dtb.o, and vmlinux.export.o will now be covered by objtool.

I am seeing some build failures when LTO is enabled with this change in -next as commit d8d3f6c6690c ("kbuild: enable objtool for *.mod.o and additional kernel objects").

  $ printf 'CONFIG_LTO_%s\n' NONE=n CLANG_THIN=y >kernel/configs/thinlto.config
  $ make -skj"$(nproc)" ARCH=x86_64 LLVM=1 mrproper {def,thinlto.}config all
  ...
  .vmlinux.export.o: warning: objtool: gelf_getehdr: invalid `Elf' handle
  make[4]: *** [scripts/Makefile.vmlinux:13: .vmlinux.export.o] Error 1
  ...

When LTO is enabled, these files are LLVM bitcode, not ELF, so objtool can't process them:

  $ file .vmlinux.export.o
  .vmlinux.export.o: LLVM IR bitcode

Cheers,
Nathan

#5 lore raw

Re: [PATCH 2/3] kbuild: enable objtool for *.mod.o and additional kernel objects

Quoted 26 lines · Nathan Chancellor

Hi Masahiro,

Quoted 9 lines · Masahiro Yamada

Currently, objtool is disabled in scripts/Makefile.{modfinal,vmlinux}.

This commit moves rule_cc_o_c and rule_as_o_S to scripts/Makefile.lib and set objtool-enabled to y there.

With this change, *.mod.o, .module-common.o, builtin-dtb.o, and vmlinux.export.o will now be covered by objtool.

I am seeing some build failures when LTO is enabled with this change in -next as commit d8d3f6c6690c ("kbuild: enable objtool for *.mod.o and additional kernel objects").

  $ printf 'CONFIG_LTO_%s\n' NONE=n CLANG_THIN=y >kernel/configs/thinlto.config
  $ make -skj"$(nproc)" ARCH=x86_64 LLVM=1 mrproper {def,thinlto.}config all
  ...
  .vmlinux.export.o: warning: objtool: gelf_getehdr: invalid `Elf' handle
  make[4]: *** [scripts/Makefile.vmlinux:13: .vmlinux.export.o] Error 1
  ...
Quoted 5 lines

When LTO is enabled, these files are LLVM bitcode, not ELF, so objtool can't process them:

  $ file .vmlinux.export.o
  .vmlinux.export.o: LLVM IR bitcode

Good catch!

I will squash the attached diff.

Thank you.

--
Best Regards
Masahiro Yamada

From 667464814cdd7cd4f62699be2f7e5871557af1fe Mon Sep 17 00:00:00 2001
From: Masahiro Yamada <[email protected]>
Date: Tue, 19 Nov 2024 17:29:42 +0900
Subject: [PATCH] fixup

---
 scripts/Makefile.build | 6 +-----
 scripts/Makefile.lib   | 9 ++++++++-
 2 files changed, 9 insertions(+), 6 deletions(-)
scripts/Makefile.build +1 -5
index 2538a925e04c..41649b8a6631 100644
@@ -129,11 +129,7 @@ $(obj)/%.ll: $(obj)/%.c FORCE
 
 is-single-obj-m = $(and $(part-of-module),$(filter $@, $(obj-m)),y)
 
-# When a module consists of a single object, there is no reason to keep LLVM IR.
-# Make $(LD) covert LLVM IR to ELF here.
-ifdef CONFIG_LTO_CLANG
-cmd_ld_single_m = $(if $(is-single-obj-m), ; $(LD) $(ld_flags) -r -o $(tmp-target) $@; mv $(tmp-target) $@)
-endif
+need-ld-single = $(is-single-obj-m)
 
 ifdef CONFIG_MODVERSIONS
 # When module versioning is enabled the following steps are executed:
scripts/Makefile.lib +8 -1
index ad6b8dfa5530..7ac65414699c 100644
@@ -303,10 +303,17 @@ endef
 # These are shared by some Makefile.* files.
 
 objtool-enabled := y
+need-ld-single := y
+
+# When a module consists of a single object, there is no reason to keep LLVM IR.
+# Make $(LD) covert LLVM IR to ELF here.
+ifdef CONFIG_LTO_CLANG
+cmd_ld_single = $(if $(need-ld-single), ; $(LD) $(ld_flags) -r -o $(tmp-target) $@; mv $(tmp-target) $@)
+endif
 
 quiet_cmd_cc_o_c = CC $(quiet_modtag)  $@
       cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $< \
-		$(cmd_ld_single_m) \
+		$(cmd_ld_single) \
 		$(cmd_objtool)
 
 define rule_cc_o_c
Signature · 1 line
2.43.0
#6 lore raw

PATCH 3/3 kbuild: re-enable KCSAN for autogenerated *.mod.c intermediaries

In reply to Masahiro Yamada

This reverts commit 54babdc0343f ("kbuild: Disable KCSAN for autogenerated *.mod.c intermediaries").

Now that objtool is enabled for *.mod.c, there is no need to filter out CFLAGS_KCSAN.

I no longer see "Unpatched return thunk in use. This should not happen!" error with KCSAN when loading a module.

---

 scripts/Makefile.modfinal | 1 -
 1 file changed, 1 deletion(-)
scripts/Makefile.modfinal +0 -1
index 0547a4b59f13..3e56a3f733e9 100644
@@ -22,7 +22,6 @@ __modfinal: $(modules:%.o=%.ko)
 modname = $(notdir $(@:.mod.o=))
 part-of-module = y
 GCOV_PROFILE := n
-KCSAN_SANITIZE := n
 ccflags-remove-y := $(CC_FLAGS_CFI)
 
 %.mod.o: %.mod.c FORCE
Signature · 1 line
2.43.0