kbuild: prevent building single targets twice
PATCH 1/1 kbuild: prevent building single targets twice
When building individual targets via 'make path/to/target.o', target.o may be build multiple times concurrently, leading to build errors like below:
$ make O=~/O/vanilla-7.1/ -j$(nproc) arch/x86/pci/intel_mid.o make[1]: Entering directory '/scratch/minipli/obj/vanilla-7.1' DESCEND objtool DESCEND bpf/resolve_btfids CC arch/x86/pci/intel_mid.o CC arch/x86/pci/intel_mid.o fixdep: error opening file: arch/x86/pci/.intel_mid.o.d: No such file or directory make[4]: *** [/scratch/minipli/src/linux/scripts/Makefile.build:290: arch/x86/pci/intel_mid.o] Error 2 make[4]: *** Deleting file 'arch/x86/pci/intel_mid.o' make[3]: *** [/scratch/minipli/src/linux/scripts/Makefile.build:551: arch/x86] Error 2 make[3]: *** Waiting for unfinished jobs.... make[2]: *** [/scratch/minipli/src/linux/Makefile:2229: .] Error 2 make[1]: *** [/scratch/minipli/src/linux/Makefile:248: __sub-make] Error 2 make[1]: Leaving directory '/scratch/minipli/obj/vanilla-7.1' make: *** [Makefile:248: __sub-make] Error 2
Please note that arch/x86/pci/intel_mid.o gets output twice and, in fact, tried to get built twice, causing the two parallel builds to interfere with each other.
The reason for the double build attempt is that the target can be reached twice, via arch/x86/'s Makefile and via arch/x86/pci/'s which both end up in '$(single-subdirs)' as both fit the prefix match.
Only the latter should be used as it contains a cflags override for CONFIG_PCI_DEBUG=y which would be skipped when using arch/x86/Makefile to build arch/x86/pci/intel_mid.o.
Fix the multi-match issue by using only the deepest match instead of all possible ones, which requires two changes:
1. Limiting 'single-subdirs' to a single match per target and
2. filtering out deeper targets in the recursive make invocation.
Also de-duplicate the list of target subdirectories to simplify the use of '$(single-subdirs)' in filter expressions for multiple targets in the same directory.
---
I ran into this quite a few times in the past and only now gave the LLM a try to dig into it. The bug only affects certain targets and only when mentioned on the command line, so regular 'make -j$(nproc)' isn't affected.
scripts/Makefile.build | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
scripts/Makefile.build +6 -2
@@ -534,7 +534,10 @@ endif # Single targets # --------------------------------------------------------------------------- -single-subdirs := $(foreach d, $(subdir-ym), $(if $(filter $d/%, $(MAKECMDGOALS)), $d)) +# A single target may match nested entries in subdir-ym. Descend through only +# the deepest match so that two recursive makes do not build the same target. +single-subdir = $(lastword $(sort $(foreach d, $(subdir-ym), $(if $(filter $d/%, $(1)), $d)))) +single-subdirs := $(sort $(foreach g, $(MAKECMDGOALS), $(call single-subdir, $g))) single-subdir-goals := $(filter $(addsuffix /%, $(single-subdirs)), $(MAKECMDGOALS)) $(single-subdir-goals): $(single-subdirs)
@@ -548,7 +551,8 @@ $(subdir-ym): $(Q)$(MAKE) $(build)=$@ \ need-builtin=$(if $(filter $@/built-in.a, $(subdir-builtin)),1) \ need-modorder=$(if $(filter $@/modules.order, $(subdir-modorder)),1) \ - $(filter $@/%, $(single-subdir-goals)) + $(filter-out $(addsuffix /%, $(filter $@/%, $(single-subdirs))), \ + $(filter $@/%, $(single-subdir-goals))) # Add FORCE to the prerequisites of a target to force it to be always rebuilt. # ---------------------------------------------------------------------------
Signature · 1 line
2.47.3
Quoted 70 lines · Mathias Krause
When building individual targets via 'make
path/to/target.o', target.o may be build multiple times concurrently, leading to build errors like below:$ make O=~/O/vanilla-7.1/ -j$(nproc) arch/x86/pci/intel_mid.o make[1]: Entering directory '/scratch/minipli/obj/vanilla-7.1' DESCEND objtool DESCEND bpf/resolve_btfids CC arch/x86/pci/intel_mid.o CC arch/x86/pci/intel_mid.o fixdep: error opening file: arch/x86/pci/.intel_mid.o.d: No such file or directory make[4]: *** [/scratch/minipli/src/linux/scripts/Makefile.build:290: arch/x86/pci/intel_mid.o] Error 2 make[4]: *** Deleting file 'arch/x86/pci/intel_mid.o' make[3]: *** [/scratch/minipli/src/linux/scripts/Makefile.build:551: arch/x86] Error 2 make[3]: *** Waiting for unfinished jobs.... make[2]: *** [/scratch/minipli/src/linux/Makefile:2229: .] Error 2 make[1]: *** [/scratch/minipli/src/linux/Makefile:248: __sub-make] Error 2 make[1]: Leaving directory '/scratch/minipli/obj/vanilla-7.1' make: *** [Makefile:248: __sub-make] Error 2Please note that
arch/x86/pci/intel_mid.ogets output twice and, in fact, tried to get built twice, causing the two parallel builds to interfere with each other.The reason for the double build attempt is that the target can be reached twice, via arch/x86/'s
Makefileand via arch/x86/pci/'s which both end up in '$(single-subdirs)' as both fit the prefix match.Only the latter should be used as it contains a cflags override for
CONFIG_PCI_DEBUG=ywhich would be skipped when usingarch/x86/Makefileto buildarch/x86/pci/intel_mid.o.Fix the multi-match issue by using only the deepest match instead of all possible ones, which requires two changes:
1. Limiting 'single-subdirs' to a single match per target and
2. filtering out deeper targets in the recursive make invocation.Also de-duplicate the list of target subdirectories to simplify the use of '$(single-subdirs)' in filter expressions for multiple targets in the same directory.
Fixes cc306abd19e8 ("kbuild: fix and refactor single target build")Assisted-by LLMSigned-off-by Mathias Krause <[email protected]>---
I ran into this quite a few times in the past and only now gave the LLM a try to dig into it. The bug only affects certain targets and only when mentioned on the command line, so regular 'make -j$(nproc)' isn't affected.scripts/Makefile.build | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)scripts/Makefile.build +4
-1@@ -534,7 +534,10 @@ endif # Single targets # --------------------------------------------------------------------------- -single-subdirs := $(foreach d, $(subdir-ym), $(if $(filter $d/%, $(MAKECMDGOALS)), $d)) +# A single target may match nested entries in subdir-ym. Descend through only +# the deepest match so that two recursive makes do not build the same target. +single-subdir = $(lastword $(sort $(foreach d, $(subdir-ym), $(if $(filter $d/%, $(1)), $d)))) +single-subdirs := $(sort $(foreach g, $(MAKECMDGOALS), $(call single-subdir, $g))) single-subdir-goals := $(filter $(addsuffix /%, $(single-subdirs)), $(MAKECMDGOALS)) $(single-subdir-goals): $(single-subdirs)
scripts/Makefile.build $(subdir-ym): · line 558
Quoted 9 lines
scripts/Makefile.build +2
-1@@ -548,7 +551,8 @@ $(subdir-ym): $(Q)$(MAKE) $(build)=$@ \ need-builtin=$(if $(filter $@/built-in.a, $(subdir-builtin)),1) \ need-modorder=$(if $(filter $@/modules.order, $(subdir-modorder)),1) \ - $(filter $@/%, $(single-subdir-goals)) + $(filter-out $(addsuffix /%, $(filter $@/%, $(single-subdirs))), \ + $(filter $@/%, $(single-subdir-goals))) # Add FORCE to the prerequisites of a target to force it to be always rebuilt. # ---------------------------------------------------------------------------
Ping! I know, it's the merge window but this patch may have fallen through the cracks as there was no response so far. Just trying to take care it doesn't get lost.
Thanks,
Mathias
Nicolas Schier
maintainerQuoted 78 lines · Mathias Krause
Quoted 77 lines · Mathias Krause
When building individual targets via 'make
path/to/target.o', target.o may be build multiple times concurrently, leading to build errors like below:$ make O=~/O/vanilla-7.1/ -j$(nproc) arch/x86/pci/intel_mid.o make[1]: Entering directory '/scratch/minipli/obj/vanilla-7.1' DESCEND objtool DESCEND bpf/resolve_btfids CC arch/x86/pci/intel_mid.o CC arch/x86/pci/intel_mid.o fixdep: error opening file: arch/x86/pci/.intel_mid.o.d: No such file or directory make[4]: *** [/scratch/minipli/src/linux/scripts/Makefile.build:290: arch/x86/pci/intel_mid.o] Error 2 make[4]: *** Deleting file 'arch/x86/pci/intel_mid.o' make[3]: *** [/scratch/minipli/src/linux/scripts/Makefile.build:551: arch/x86] Error 2 make[3]: *** Waiting for unfinished jobs.... make[2]: *** [/scratch/minipli/src/linux/Makefile:2229: .] Error 2 make[1]: *** [/scratch/minipli/src/linux/Makefile:248: __sub-make] Error 2 make[1]: Leaving directory '/scratch/minipli/obj/vanilla-7.1' make: *** [Makefile:248: __sub-make] Error 2Please note that
arch/x86/pci/intel_mid.ogets output twice and, in fact, tried to get built twice, causing the two parallel builds to interfere with each other.The reason for the double build attempt is that the target can be reached twice, via arch/x86/'s
Makefileand via arch/x86/pci/'s which both end up in '$(single-subdirs)' as both fit the prefix match.Only the latter should be used as it contains a cflags override for
CONFIG_PCI_DEBUG=ywhich would be skipped when usingarch/x86/Makefileto buildarch/x86/pci/intel_mid.o.Fix the multi-match issue by using only the deepest match instead of all possible ones, which requires two changes:
1. Limiting 'single-subdirs' to a single match per target and
2. filtering out deeper targets in the recursive make invocation.Also de-duplicate the list of target subdirectories to simplify the use of '$(single-subdirs)' in filter expressions for multiple targets in the same directory.
Fixes cc306abd19e8 ("kbuild: fix and refactor single target build")Assisted-by LLMSigned-off-by Mathias Krause <[email protected]>---
I ran into this quite a few times in the past and only now gave the LLM a try to dig into it. The bug only affects certain targets and only when mentioned on the command line, so regular 'make -j$(nproc)' isn't affected.scripts/Makefile.build | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)scripts/Makefile.build +6
-2@@ -534,7 +534,10 @@ endif # Single targets # --------------------------------------------------------------------------- -single-subdirs := $(foreach d, $(subdir-ym), $(if $(filter $d/%, $(MAKECMDGOALS)), $d)) +# A single target may match nested entries in subdir-ym. Descend through only +# the deepest match so that two recursive makes do not build the same target. +single-subdir = $(lastword $(sort $(foreach d, $(subdir-ym), $(if $(filter $d/%, $(1)), $d)))) +single-subdirs := $(sort $(foreach g, $(MAKECMDGOALS), $(call single-subdir, $g))) single-subdir-goals := $(filter $(addsuffix /%, $(single-subdirs)), $(MAKECMDGOALS)) $(single-subdir-goals): $(single-subdirs)@@ -548,7 +551,8 @@ $(subdir-ym): $(Q)$(MAKE) $(build)=$@ \ need-builtin=$(if $(filter $@/built-in.a, $(subdir-builtin)),1) \ need-modorder=$(if $(filter $@/modules.order, $(subdir-modorder)),1) \ - $(filter $@/%, $(single-subdir-goals)) + $(filter-out $(addsuffix /%, $(filter $@/%, $(single-subdirs))), \ + $(filter $@/%, $(single-subdir-goals)))
Quoted 6 lines
scripts/Makefile.build $(subdir-ym): · line 558
Quoted 2 lines
scripts/Makefile.build +0
-0# Add FORCE to the prerequisites of a target to force it to be always rebuilt. # ---------------------------------------------------------------------------Ping! I know, it's the merge window but this patch may have fallen through the cracks as there was no response so far. Just trying to take care it doesn't get lost.
thanks for the ping. The patch is in my review queue for the upcoming cycle.
Signature · 1 line
Nicolas
Nathan Chancellor
maintainerQuoted 7 lines · Nicolas Schier
Quoted 3 lines · Mathias Krause
Ping! I know, it's the merge window but this patch may have fallen through the cracks as there was no response so far. Just trying to take care it doesn't get lost.
thanks for the ping. The patch is in my review queue for the upcoming cycle.
Same, I will try to review it this week or next once I clear my tip of tree clang regressions backlog and Kbuild merge window madness.
Signature · 2 lines
Cheers, Nathan
Quoted 6 lines · Nathan Chancellor
Quoted 5 lines · Nicolas Schier
Quoted 3 lines · Mathias Krause
Ping! I know, it's the merge window but this patch may have fallen through the cracks as there was no response so far. Just trying to take care it doesn't get lost.
Quoted 6 lines
Quoted 2 lines
thanks for the ping. The patch is in my review queue for the upcoming cycle.
Same, I will try to review it this week or next once I clear my tip of tree clang regressions backlog and
Kbuildmerge window madness.
Thanks, guys!
Nathan Chancellor
maintainerQuoted 7 lines · Mathias Krause
Quoted 6 lines · Nathan Chancellor
Quoted 5 lines · Nicolas Schier
Quoted 3 lines · Mathias Krause
Ping! I know, it's the merge window but this patch may have fallen through the cracks as there was no response so far. Just trying to take care it doesn't get lost.
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
thanks for the ping. The patch is in my review queue for the upcoming cycle.
Same, I will try to review it this week or next once I clear my tip of tree clang regressions backlog and
Kbuildmerge window madness.
I forgot to mention, it looks like this is a known corner case in Kbuild that has come up before:
https://lore.kernel.org/[email protected]/
https://lore.kernel.org/CAK7LNAQhORSPEuB=XFa_i5EcQfsJ6uXy0MM5YWo_84eLGcoxNQ@mail.gmail.com/
This patch might be the fix that Masahiro could not figure out but it might be better to just forsake drivers-y here and use obj-y properly, especially since it looks like core-y is not even used for x86 anymore (but maybe these objects still need to linked towards the end of the kernel?). This diff appears to resolve the reported issue but I did not try running the result.
arch/x86/Kbuild +7 -0
@@ -36,5 +36,12 @@ obj-$(CONFIG_KEXEC_FILE) += purgatory/ obj-y += virt/ +obj-$(CONFIG_PCI) += pci/ + +# suspend and hibernation support +obj-$(CONFIG_PM) += power/ + +obj-$(CONFIG_VIDEO) += video/ + # for cleaning subdir- += boot tools
arch/x86/Makefile +0 -8
@@ -279,14 +279,6 @@ archprepare: $(cpufeaturemasks.hdr) libs-y += arch/x86/lib/ -# drivers-y are linked after core-y -drivers-$(CONFIG_PCI) += arch/x86/pci/ - -# suspend and hibernation support -drivers-$(CONFIG_PM) += arch/x86/power/ - -drivers-$(CONFIG_VIDEO) += arch/x86/video/ - #### # boot loader support. Several targets are kept for legacy purposes
Signature · 2 lines
Cheers, Nathan
Quoted 6 lines · Nathan Chancellor
[...]
I forgot to mention, it looks like this is a known corner case inKbuildthat has come up before:https://lore.kernel.org/[email protected]/
https://lore.kernel.org/CAK7LNAQhORSPEuB=XFa_i5EcQfsJ6uXy0MM5YWo_84eLGcoxNQ@mail.gmail.com/
Yeah, I saw these too when looking for related issues and fixes. However, even Masahiro admitted, that this is just working around the issue instead of solving the more fundamental kbuild bug.
Quoted 34 lines
This patch might be the fix that Masahiro could not figure out but it might be better to just forsake drivers-y here and use obj-y properly, especially since it looks like core-y is not even used for x86 anymore (but maybe these objects still need to linked towards the end of the kernel?). This diff appears to resolve the reported issue but I did not try running the result.
arch/x86/Kbuild +7
-0@@ -36,5 +36,12 @@ obj-$(CONFIG_KEXEC_FILE) += purgatory/ obj-y += virt/ +obj-$(CONFIG_PCI) += pci/ + +# suspend and hibernation support +obj-$(CONFIG_PM) += power/ + +obj-$(CONFIG_VIDEO) += video/ + # for cleaning subdir- += boot toolsarch/x86/Makefile +0
-1@@ -279,14 +279,6 @@ archprepare: $(cpufeaturemasks.hdr) libs-y += arch/x86/lib/ -# drivers-y are linked after core-y
arch/x86/Makefile archprepare: $(cpufeaturemasks.hdr) · line 284
Quoted 10 lines
arch/x86/Makefile +0
-7-drivers-$(CONFIG_PCI) += arch/x86/pci/ - -# suspend and hibernation support -drivers-$(CONFIG_PM) += arch/x86/power/ - -drivers-$(CONFIG_VIDEO) += arch/x86/video/ - #### # boot loader support. Several targets are kept for legacy purposes
This only "fixes" (read on why in quotes) this one instance but leaves others unsolved.
Already visible in the diff context is 'libs-y' which has the same issue and a real behavioral change, depending on which compiler invocation runs last for arch/x86/lib/delay.o, it'll either be compiled with or without -pg under KCSAN.
Others are:
- arch/x86/platform/pvh/enlighten.o (via arch/x86/Kbuild),
- drivers/char/ipmi/ipmi_msghandler.o (via drivers/Makefile, intentionally there for init ordering),
- drivers/input/serio/i8042.o (also via drivers/Makefile)
But what's worse, your change also affects link order which also defines runtime initialization order. I don't want to crawl into that rabbit hole and look for all the implicit dependencies that'd be broken by that change...
One obvious, however, would be pci_subsys_init() which would then run before acpi_init(), leaving out ACPI-discovered PCI roots from getting discovered.
So no, I don't like your change and prefer the more fundamental kbuild fix to get this sorted out once for all.
Thanks,
Mathias
Nathan Chancellor
maintainerQuoted 4 lines · Mathias Krause
Already visible in the diff context is 'libs-y' which has the same issue and a real behavioral change, depending on which compiler invocation runs last for
arch/x86/lib/delay.o, it'll either be compiled with or without -pg under KCSAN.
Ah, I had not realized that libs-y suffered from the same problem.
Quoted 11 lines
Others are:
-arch/x86/platform/pvh/enlighten.o(viaarch/x86/Kbuild),
-drivers/char/ipmi/ipmi_msghandler.o(viadrivers/Makefile, intentionally there for init ordering),
-drivers/input/serio/i8042.o(also viadrivers/Makefile)But what's worse, your change also affects link order which also defines runtime initialization order. I don't want to crawl into that rabbit hole and look for all the implicit dependencies that'd be broken by that change...
Quoted 6 lines
One obvious, however, would be
pci_subsys_init()which would then run beforeacpi_init(), leaving out ACPI-discovered PCI roots from getting discovered.So no, I don't like your change and prefer the more fundamental kbuild fix to get this sorted out once for all.
Totally fair, I had briefly thought about that but did not fully consider all implications, so thanks for pointing that out.
I'll keep your change in my review queue and study it soon.
Signature · 2 lines
Cheers, Nathan
Quoted 44 lines
When building individual targets via 'make
path/to/target.o', target.o may be build multiple times concurrently, leading to build errors like below:$ make O=~/O/vanilla-7.1/ -j$(nproc) arch/x86/pci/intel_mid.o make[1]: Entering directory '/scratch/minipli/obj/vanilla-7.1' DESCEND objtool DESCEND bpf/resolve_btfids CC arch/x86/pci/intel_mid.o CC arch/x86/pci/intel_mid.o fixdep: error opening file: arch/x86/pci/.intel_mid.o.d: No such file or directory make[4]: *** [/scratch/minipli/src/linux/scripts/Makefile.build:290: arch/x86/pci/intel_mid.o] Error 2 make[4]: *** Deleting file 'arch/x86/pci/intel_mid.o' make[3]: *** [/scratch/minipli/src/linux/scripts/Makefile.build:551: arch/x86] Error 2 make[3]: *** Waiting for unfinished jobs.... make[2]: *** [/scratch/minipli/src/linux/Makefile:2229: .] Error 2 make[1]: *** [/scratch/minipli/src/linux/Makefile:248: __sub-make] Error 2 make[1]: Leaving directory '/scratch/minipli/obj/vanilla-7.1' make: *** [Makefile:248: __sub-make] Error 2Please note that
arch/x86/pci/intel_mid.ogets output twice and, in fact, tried to get built twice, causing the two parallel builds to interfere with each other.The reason for the double build attempt is that the target can be reached twice, via arch/x86/'s
Makefileand via arch/x86/pci/'s which both end up in '$(single-subdirs)' as both fit the prefix match.Only the latter should be used as it contains a cflags override for
CONFIG_PCI_DEBUG=ywhich would be skipped when usingarch/x86/Makefileto buildarch/x86/pci/intel_mid.o.Fix the multi-match issue by using only the deepest match instead of all possible ones, which requires two changes:
1. Limiting 'single-subdirs' to a single match per target and
2. filtering out deeper targets in the recursive make invocation.Also de-duplicate the list of target subdirectories to simplify the use of '$(single-subdirs)' in filter expressions for multiple targets in the same directory.
Fixes cc306abd19e8 ("kbuild: fix and refactor single target build")Assisted-by LLMSigned-off-by Mathias Krause <[email protected]>
Quoted 9 lines
---
I ran into this quite a few times in the past and only now gave the LLM a try to dig into it. The bug only affects certain targets and only when mentioned on the command line, so regular 'make -j$(nproc)' isn't affected.scripts/Makefile.build | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
Thanks a lot for the patch! I will happily take it for the v7.5 cycle, and add a
Signature · 1 line
Nicolas
Nicolas Schier
maintainerQuoted 1 line · Mathias Krause
kbuild: prevent building single targets twice
Applied to kbuild/linux.git (kbuild-next-unstable), thanks!
[1/1] kbuild: prevent building single targets twice
https://git.kernel.org/kbuild/c/329d8e7d
Please look out for regression or issue reports or other follow up comments, as they may result in the patch/series getting dropped, reverted or modified (e.g. trailers).
Patches applied to a kbuild-*-unstable branch are accepted pending wider testing in linux-next and any post-commit review; they will generally be moved to the main branch (kbuild-next or kbuild-fixes) in about a week if no issues are found.
Best regards,
Signature · 1 line
Nicolas