Linux Modules
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Lorenzo Stoakes (ARM)" <ljs@kernel.org>,
	"Linus Torvalds" <torvalds@linux-foundation.org>,
	"Nathan Chancellor" <nathan@kernel.org>,
	"Nicolas Schier" <nsc@kernel.org>,
	"Nick Desaulniers" <ndesaulniers@google.com>,
	"Bill Wendling" <morbo@google.com>,
	"Justin Stitt" <justinstitt@google.com>,
	"Geert Uytterhoeven" <geert@linux-m68k.org>,
	"Luis Chamberlain" <mcgrof@kernel.org>,
	"Petr Pavlu" <petr.pavlu@suse.com>,
	"Daniel Gomez" <da.gomez@kernel.org>,
	"Sami Tolvanen" <samitolvanen@google.com>,
	"Aaron Tomlin" <atomlin@atomlin.com>,
	"Josh Poimboeuf" <jpoimboe@kernel.org>,
	"Peter Zijlstra" <peterz@infradead.org>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Tamir Duberstein" <tamird@kernel.org>,
	"Alexandre Courbot" <acourbot@nvidia.com>,
	"Onur Özkan" <work@onurozkan.dev>,
	"Nicolas Schier" <n.schier@fritz.com>,
	"Kees Cook" <kees@kernel.org>
Cc: linux-kbuild@vger.kernel.org, linux-m68k@lists.linux-m68k.org,
	linux-modules@vger.kernel.org
Subject: Re: [PATCH v7 2/7] kbuild: implement and use depcheck to check dependency timestamps
Date: Sat, 05 Dec 2026 20:29:01 +0000	[thread overview]
Message-ID: <sashiko-outbox-251886@kernel.org> (raw)
In-Reply-To: <20261204-build-speedup-v7-2-19a89034d8d1@kernel.org>

Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [Medium] kbuild: depcheck ignores MAKEFLAGS, breaking -W/-L and
  writing on -n/-q
- [Medium] kbuild: depcheck process() concatenates truncated .cmd files
  without fallback
- [Medium] kbuild: depcheck shell invocation hits 128 KiB MAX_ARG_STRLEN
  via /bin/sh -c
- [Medium] kbuild: depcheck treats truncated or unclosed deps blocks as
  fresh
- [Low] kbuild: depcheck invocation spawns /bin/sh and runs on dep-less
  directories
- [Low] kbuild: depcheck process() omits newline/continuation break
  after .cmd files
--

commit d40b5bf6ef4883f8af87fca5ed44b1b1cea0e1ba
Author: Lorenzo Stoakes (ARM) <ljs@kernel.org>

kbuild: implement and use depcheck to check dependency timestamps

This commit adds scripts/basic/depcheck to check .cmd file dependency
timestamps in C and emit a pruned .depcheck.mk fragment for
scripts/Makefile.build to include in place of individual .cmd files.

> diff --git a/scripts/Makefile.build b/scripts/Makefile.build
> index bbe9555c47a8b..0c4ec5b71658b 100644
> --- a/scripts/Makefile.build
> +++ b/scripts/Makefile.build
> @@ -613,7 +613,24 @@ $(obj)/: $(if $(KBUILD_BUILTIN), $(targets-for-builtin)) \
>  
>  existing-targets := $(wildcard $(sort $(targets)))
>  
> --include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd)
> +cmd-files := $(wildcard $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd))
> +
> +# depcheck checks timestamps first and outputs only saved command lines of
> +# up-to-date targets, falling back to .cmd files if it fails. A directory
> +# whose targets all lie elsewhere may not exist in the output tree.
> +depcheck := $(wildcard $(objtree)/scripts/basic/depcheck)
> +
> +# A single target such as foo.s may depend on a header depcheck cannot see
> +# being remade, so leave single-target builds to make.
> +ifeq ($(MAKECMDGOALS),)

[Severity: Medium]
Should this also check MAKEFLAGS before invoking depcheck?

GNU Make propagates flags such as -W (--what-if) and -L
(--check-symlink-times) via MAKEFLAGS while MAKECMDGOALS stays empty in
recursive Makefile.build runs.  Because lookup_dep() only inspects
on-disk mtimes via stat(), depcheck treats objects depending on a -W
header as fresh and strips their dependency rules, so make silently skips
rebuilding or reporting them.

In addition, make still evaluates $(shell ...) under -n (--dry-run) and
-q (--question), so depcheck runs mkstemp() and rename() to create or
overwrite $(obj)/.depcheck.mk in every visited directory (or prints
perror() errors on read-only build trees).

> +ifneq ($(and $(depcheck),$(cmd-files),$(wildcard $(obj))),)
> +ifeq ($(shell $(depcheck) $(obj)/.depcheck.mk $(cmd-files) && echo ok),ok)

[Severity: Medium]
Can this $(shell ...) invocation hit the 128 KiB MAX_ARG_STRLEN limit?

The && operator forces GNU Make to run /bin/sh -c with all $(cmd-files)
packed into a single argv[2] string, which execve() caps at 128 KiB on
4 KiB-page Linux hosts.  In Documentation/devicetree/bindings/, targets
includes over 10,400 .example.dts and .example.dtb files whose .cmd paths
expand to roughly 800 KiB, causing execve() to fail with E2BIG on
incremental dt_binding_check runs and fall back to make.

Having depcheck print ok itself allows dropping && echo ok so make can
exec depcheck directly, and passing the list via stdin or $(file >...)
would also avoid total argument limits.

> +cmd-files := $(obj)/.depcheck.mk
> +endif
> +endif
> +endif

[Severity: Low]
Does this spawn /bin/sh and run depcheck even on directories that have no
dependency lists to prune?

Because && echo ok prevents make's direct execvp() fast path, every
visited directory pays an extra /bin/sh fork and exec.  Also, rules using
if_changed such as built-in.a and modules.order always populate cmd-files
with one-line .cmd files that contain no deps_ block, so pure parent and
empty-config directories still fork depcheck, create a temp file, copy
the lines verbatim, and rename it to .depcheck.mk.

Printing ok from depcheck and filtering out known if_changed-only .cmd
files before invoking it would avoid that overhead.

> diff --git a/scripts/basic/depcheck.c b/scripts/basic/depcheck.c
> new file mode 100644
> index 0000000000000..339c058ba1602
> --- /dev/null
> +++ b/scripts/basic/depcheck.c

[ ... ]

> +static bool deps_are_fresh(const char *pos, const char *end,
> +			   const struct timespec *target)
> +{
> +	struct line line;
> +
> +	while (next_line(&pos, end, &line)) {
> +		if (line_is_blank(&line))
> +			return true;
> +		if (!dep_is_fresh(line, target))
> +			return false;
> +		if (!line_is_continued(&line))
> +			return true;
> +	}
> +
> +	/* The list was cut off by the end of the file. */
> +	return false;
> +}

[ ... ]

> +static bool find_deps(const char *buf, size_t len, size_t *deps_off,
> +		      const char **list)
> +{
> +	const char *pos = buf, *end = buf + len;
> +	struct line line;
> +
> +	while (next_line(&pos, end, &line)) {
> +		if (!line_starts_with(&line, DEPS_PREFIX))
> +			continue;
> +
> +		*deps_off = line.text - buf;
> +		*list = line_is_continued(&line) ? pos : NULL;
> +		return true;
> +	}
> +
> +	return false;
> +}

[ ... ]

> +static bool target_is_fresh(const char *cmd_path, const char *buf, size_t len,
> +			    size_t *deps_off)
> +{
> +	char target[PATH_MAX];
> +	struct stat st;
> +	const char *list;
> +
> +	if (!target_of(cmd_path, target, sizeof(target)) || stat(target, &st))
> +		return false;
> +	if (!find_deps(buf, len, deps_off, &list))
> +		return false;
> +	if (!list)
> +		return true;
> +
> +	return deps_are_fresh(list, buf + len, &st.st_mtim);
> +}

[ ... ]

> +static size_t deps_block_end(const char *buf, size_t len, size_t deps_off)
> +{
> +	const char *pos = buf + deps_off, *end = buf + len;
> +	struct line line;
> +
> +	while (next_line(&pos, end, &line)) {
> +		if (line_starts_with(&line, DEPS_RULE_PREFIX) &&
> +		    line_ends_with(&line, RULE_SUFFIX))
> +			return pos - buf;
> +	}
> +
> +	return len;
> +}

[Severity: Medium]
Can truncated or unclosed deps_ blocks be misclassified as fresh here?

If a build is killed after creating the target while cmd_and_fixdep is
still writing $(dot-target).cmd, the .cmd file can be cut off mid-write:

- A deps_ header truncated before its trailing " \" causes find_deps()
  to set *list to NULL and target_is_fresh() to return true immediately.
- A dependency line truncated before its trailing " \" returns true at
  !line_is_continued(&line) in deps_are_fresh(), bypassing the
  end-of-file fallback.
- A file truncated after the blank line without its closing $(deps_...):
  rule causes deps_block_end() to return len, so write_without_deps()
  silently discards everything from deps_off to the end of the file.

In all three cases, depcheck strips the broken block and hides errors
that make would otherwise catch.  Requiring a continued deps_ header,
continued dependency lines, and a matching closing $(deps_...): line in
target_is_fresh() would avoid this.

> +
> +/* The target is fresh: pass on everything but the dependency block. */
> +static void write_without_deps(FILE *out, const char *buf, size_t len,
> +			       size_t deps_off)
> +{
> +	const size_t tail_off = deps_block_end(buf, len, deps_off);
> +
> +	fwrite(buf, 1, deps_off, out);
> +	fputc('\n', out);
> +	fwrite(buf + tail_off, 1, len - tail_off, out);
> +}

[Severity: Low]
Does concatenating .cmd files into .depcheck.mk need a newline or line
continuation break between entries?

write_without_deps() copies the tail after $(deps_...): verbatim, and
process() writes non-fresh .cmd files verbatim without ensuring a
trailing newline.  If a truncated .cmd file lacks a final newline or
ends with a backslash continuation, the next file's savedcmd_ line is
appended directly onto it rather than starting a new assignment, which
cannot happen when make includes each .cmd file separately.

> +
> +/*
> + * Generate a fragment for make from one .cmd file - without the dependency
> + * block if the target is fresh, otherwise all of it. Returns false if the file
> + * cannot be read.
> + */
> +static bool process(FILE *out, const char *cmd_path)
> +{
> +	size_t len, deps_off;
> +	char *buf;
> +
> +	buf = read_file(cmd_path, &len);
> +	if (!buf) {
> +		perror(cmd_path);
> +		return false;
> +	}
> +
> +	if (target_is_fresh(cmd_path, buf, len, &deps_off))
> +		write_without_deps(out, buf, len, deps_off);
> +	else
> +		fwrite(buf, 1, len, out);
> +
> +	free(buf);
> +	return true;
> +}

[Severity: Medium]
Should process() return false when a .cmd file is truncated instead of
writing it into .depcheck.mk?

When deps_are_fresh() detects a dependency list cut off by end of file,
it returns false and target_is_fresh() propagates that to process(),
which then writes the truncated buffer verbatim and returns true.
Because fixdep ends every dependency entry with a backslash continuation,
a truncated .cmd file swallows the next target's savedcmd_ line into
deps_ or causes a make parse error for the directory.

This can happen during a parallel build with CONFIG_HEADERS_INSTALL=y,
where scripts_unifdef rewrites scripts/.unifdef.cmd via shell
redirection in cmd_and_fixdep concurrently with depcheck reading it.
Returning false from process() on truncated input would let kbuild fall
back to the original .cmd files as intended.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204-build-speedup-v7-0-19a89034d8d1@kernel.org?part=2

  reply	other threads:[~2026-12-05 20:29 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 20:27 [PATCH v7 0/7] kbuild: significantly speed up kernel builds Lorenzo Stoakes (ARM)
2026-12-04 20:27 ` [PATCH v7 1/7] kbuild: calculate list, composite object state per object Lorenzo Stoakes (ARM)
2026-12-04 20:27 ` [PATCH v7 2/7] kbuild: implement and use depcheck to check dependency timestamps Lorenzo Stoakes (ARM)
2026-12-05 20:29   ` sashiko-bot [this message]
2026-12-04 20:27 ` [PATCH v7 3/7] modpost: cache section relocation mismatch state Lorenzo Stoakes (ARM)
2026-12-05 20:29   ` sashiko-bot
2026-12-04 20:27 ` [PATCH v7 4/7] modpost: emit module descriptors as assembly Lorenzo Stoakes (ARM)
2026-12-05 20:29   ` sashiko-bot
2026-12-04 20:27 ` [PATCH v7 5/7] kbuild: batch module finalisation Lorenzo Stoakes (ARM)
2026-12-04 20:27 ` [PATCH v7 6/7] rust: make exports.o depend on the headers generated for it Lorenzo Stoakes (ARM)
2026-12-04 20:27 ` [PATCH v7 7/7] kbuild: build rust crates in parallel with the rest of the build Lorenzo Stoakes (ARM)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-251886@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=aliceryhl@google.com \
    --cc=atomlin@atomlin.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=da.gomez@kernel.org \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=gary@garyguo.net \
    --cc=geert@linux-m68k.org \
    --cc=jpoimboe@kernel.org \
    --cc=justinstitt@google.com \
    --cc=kees@kernel.org \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-m68k@lists.linux-m68k.org \
    --cc=linux-modules@vger.kernel.org \
    --cc=ljs@kernel.org \
    --cc=lossin@kernel.org \
    --cc=mcgrof@kernel.org \
    --cc=morbo@google.com \
    --cc=n.schier@fritz.com \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=nsc@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=peterz@infradead.org \
    --cc=petr.pavlu@suse.com \
    --cc=samitolvanen@google.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=torvalds@linux-foundation.org \
    --cc=work@onurozkan.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox