mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] kbuild: prevent building single targets twice
@ 2026-09-23 12:39 Mathias Krause
  2026-10-21  6:53 ` Mathias Krause
  0 siblings, 1 reply; 6+ messages in thread
From: Mathias Krause @ 2026-09-23 12:39 UTC (permalink / raw)
  To: Nathan Chancellor, Nicolas Schier, linux-kbuild
  Cc: linux-kernel, 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 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.

Fixes: cc306abd19e8 ("kbuild: fix and refactor single target build")
Assisted-by: LLM
Signed-off-by: Mathias Krause <minipli@grsecurity.net>
---
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(-)

diff --git a/scripts/Makefile.build b/scripts/Makefile.build
index 0f4fdc119e58..d5f58513d02a 100644
--- a/scripts/Makefile.build
+++ b/scripts/Makefile.build
@@ -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.
 # ---------------------------------------------------------------------------
-- 
2.47.3


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] kbuild: prevent building single targets twice
  2026-09-23 12:39 [PATCH] kbuild: prevent building single targets twice Mathias Krause
@ 2026-10-21  6:53 ` Mathias Krause
  2026-10-21 17:04   ` Nicolas Schier
  0 siblings, 1 reply; 6+ messages in thread
From: Mathias Krause @ 2026-10-21  6:53 UTC (permalink / raw)
  To: Nathan Chancellor, Nicolas Schier, linux-kbuild; +Cc: linux-kernel

On 23.09.26 14:39, Mathias Krause wrote:
> 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.
> 
> Fixes: cc306abd19e8 ("kbuild: fix and refactor single target build")
> Assisted-by: LLM
> Signed-off-by: Mathias Krause <minipli@grsecurity.net>
> ---
> 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(-)
> 
> diff --git a/scripts/Makefile.build b/scripts/Makefile.build
> index 0f4fdc119e58..d5f58513d02a 100644
> --- a/scripts/Makefile.build
> +++ b/scripts/Makefile.build
> @@ -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.
>  # ---------------------------------------------------------------------------

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] kbuild: prevent building single targets twice
  2026-10-21  6:53 ` Mathias Krause
@ 2026-10-21 17:04   ` Nicolas Schier
  2026-10-21 19:03     ` Nathan Chancellor
  0 siblings, 1 reply; 6+ messages in thread
From: Nicolas Schier @ 2026-10-21 17:04 UTC (permalink / raw)
  To: Mathias Krause; +Cc: Nathan Chancellor, linux-kbuild, linux-kernel

On Wed, Oct 21, 2026 at 08:53:56AM +0200, Mathias Krause wrote:
> On 23.09.26 14:39, Mathias Krause wrote:
> > 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.
> > 
> > Fixes: cc306abd19e8 ("kbuild: fix and refactor single target build")
> > Assisted-by: LLM
> > Signed-off-by: Mathias Krause <minipli@grsecurity.net>
> > ---
> > 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(-)
> > 
> > diff --git a/scripts/Makefile.build b/scripts/Makefile.build
> > index 0f4fdc119e58..d5f58513d02a 100644
> > --- a/scripts/Makefile.build
> > +++ b/scripts/Makefile.build
> > @@ -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.
> >  # ---------------------------------------------------------------------------
> 
> 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.

-- 
Nicolas

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] kbuild: prevent building single targets twice
  2026-10-21 17:04   ` Nicolas Schier
@ 2026-10-21 19:03     ` Nathan Chancellor
  2026-10-21 21:25       ` Mathias Krause
  0 siblings, 1 reply; 6+ messages in thread
From: Nathan Chancellor @ 2026-10-21 19:03 UTC (permalink / raw)
  To: Nicolas Schier, Mathias Krause, linux-kbuild, linux-kernel

On Wed, Oct 21, 2026 at 07:04:33PM +0200, Nicolas Schier wrote:
> On Wed, Oct 21, 2026 at 08:53:56AM +0200, Mathias Krause wrote:
> > 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.

-- 
Cheers,
Nathan

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] kbuild: prevent building single targets twice
  2026-10-21 19:03     ` Nathan Chancellor
@ 2026-10-21 21:25       ` Mathias Krause
  2026-10-21 21:55         ` Nathan Chancellor
  0 siblings, 1 reply; 6+ messages in thread
From: Mathias Krause @ 2026-10-21 21:25 UTC (permalink / raw)
  To: Nathan Chancellor, Nicolas Schier, linux-kbuild, linux-kernel

On 21.10.26 21:03, Nathan Chancellor wrote:
> On Wed, Oct 21, 2026 at 07:04:33PM +0200, Nicolas Schier wrote:
>> On Wed, Oct 21, 2026 at 08:53:56AM +0200, Mathias Krause wrote:
>>> 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.
> 

Thanks, guys!

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] kbuild: prevent building single targets twice
  2026-10-21 21:25       ` Mathias Krause
@ 2026-10-21 21:55         ` Nathan Chancellor
  0 siblings, 0 replies; 6+ messages in thread
From: Nathan Chancellor @ 2026-10-21 21:55 UTC (permalink / raw)
  To: Mathias Krause; +Cc: Nicolas Schier, linux-kbuild, linux-kernel

On Wed, Oct 21, 2026 at 11:25:13PM +0200, Mathias Krause wrote:
> On 21.10.26 21:03, Nathan Chancellor wrote:
> > On Wed, Oct 21, 2026 at 07:04:33PM +0200, Nicolas Schier wrote:
> >> On Wed, Oct 21, 2026 at 08:53:56AM +0200, Mathias Krause wrote:
> >>> 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.

I forgot to mention, it looks like this is a known corner case in Kbuild
that has come up before:

  https://lore.kernel.org/7b663b86-4df0-c3df-18a7-e4f47f900be4@kernel.org/
  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.

diff --git a/arch/x86/Kbuild b/arch/x86/Kbuild
index 36b985d0e7bf..bd0621207c0c 100644
--- a/arch/x86/Kbuild
+++ b/arch/x86/Kbuild
@@ -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
diff --git a/arch/x86/Makefile b/arch/x86/Makefile
index b7e08519b8c1..9a872bd9520e 100644
--- a/arch/x86/Makefile
+++ b/arch/x86/Makefile
@@ -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
 
-- 
Cheers,
Nathan

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-10-21 21:55 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-23 12:39 [PATCH] kbuild: prevent building single targets twice Mathias Krause
2026-10-21  6:53 ` Mathias Krause
2026-10-21 17:04   ` Nicolas Schier
2026-10-21 19:03     ` Nathan Chancellor
2026-10-21 21:25       ` Mathias Krause
2026-10-21 21:55         ` Nathan Chancellor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®