Building the Linux kernel with Clang and LLVM
 help / color / mirror / Atom feed
From: Nathan Chancellor <nathan@kernel.org>
To: Nick Desaulniers <ndesaulniers@google.com>
Cc: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Juri Lelli <juri.lelli@redhat.com>,
	Vincent Guittot <vincent.guittot@linaro.org>,
	Dietmar Eggemann <dietmar.eggemann@arm.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	Ben Segall <bsegall@google.com>, Mel Gorman <mgorman@suse.de>,
	Valentin Schneider <vschneid@redhat.com>,
	K Prateek Nayak <kprateek.nayak@amd.com>,
	Bill Wendling <morbo@google.com>,
	Justin Stitt <justinstitt@google.com>,
	linux-kernel@vger.kernel.org, llvm@lists.linux.dev,
	stable@vger.kernel.org
Subject: Re: [PATCH v3] sched/topology: Avoid unsequenced accesses to jiffies in sd_init()
Date: Wed, 25 Nov 2026 13:03:11 -0700	[thread overview]
Message-ID: <20261125200311.GA3625068@ax162> (raw)
In-Reply-To: <CAKwvOdmyGdn4ES6Ku+Yovd7a5imzmtmYtd7KB=OMCqKpv1X8rg@mail.gmail.com>

On Wed, Nov 25, 2026 at 11:32:31AM -0800, Nick Desaulniers wrote:
> On Wed, Nov 25, 2026 at 1:46 AM Peter Zijlstra <peterz@infradead.org> wrote:
> >
> > On Wed, Nov 25, 2026 at 12:23:35AM -0700, Nathan Chancellor wrote:
> > > Clang's warning is ultimately pointing out that jiffies may have two
> > > separate values in this initializer because it is volatile and accessed
> > > twice and the order of these accesses is not specified. This could
> > > result in unexpected or inconsistent behavior depending on the code
> > > author's intent and whether they realized the variable was volatile or
> > > not.
> > >
> > > As two separate accesses is not necessary here, add a local variable to
> > > hold the value of jiffies before the designated initializer so that a
> > > consistent value is used for initializing both members, clearing up the
> > > warning.
> >
> > None of this tells me why we should not just disable that warning and
> > get on with life.

See below, I believe this warning has general value (but maybe not
here).

> (Could also do ugly pragma's around this code;

Comments could obviously be adjusted.

diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c
index 99878da03bce..729171ccc9b2 100644
--- a/kernel/sched/topology.c
+++ b/kernel/sched/topology.c
@@ -1973,6 +1973,9 @@ sd_init(struct sched_domain_topology_level *tl,
 #endif
 	sd_flags |= asym_cpu_capacity_classify(sd_span, cpu_map);
 
+	__diag_push();
+	__diag_ignore(clang, all, "-Wunknown-warning-option", "Next warning might be unsupported");
+	__diag_ignore(clang, all, "-Wunsequenced-volatile", "Doesn't matter for jiffies");
 	*sd = (struct sched_domain){
 		.min_interval		= sd_weight,
 		.max_interval		= 2*sd_weight,
@@ -2008,6 +2011,7 @@ sd_init(struct sched_domain_topology_level *tl,
 		.child			= child,
 		.name			= tl->name,
 	};
+	__diag_pop();
 
 	WARN_ONCE((sd->flags & (SD_SHARE_CPUCAPACITY | SD_ASYM_CPUCAPACITY)) ==
 		  (SD_SHARE_CPUCAPACITY | SD_ASYM_CPUCAPACITY),

> disable it for this entire source file via -Wno-unsequenced-volatile in the Makefile

diff --git a/kernel/sched/Makefile b/kernel/sched/Makefile
index b1f1a367034f..c1b2062d7356 100644
--- a/kernel/sched/Makefile
+++ b/kernel/sched/Makefile
@@ -30,6 +30,8 @@ ifdef CONFIG_TRACE_BRANCH_PROFILING
 CFLAGS_build_policy.o += -DDISABLE_BRANCH_PROFILING
 CFLAGS_build_utility.o += -DDISABLE_BRANCH_PROFILING
 endif
+
+CFLAGS_build_utility.o += $(CONFIG_CC_OPT_WNO_UNSEQUENCED_VOLATILE)
 #
 # Build efficiency:
 #
diff --git a/scripts/Kconfig.toolchain b/scripts/Kconfig.toolchain
index 1d68075f0469..dfd76b05b6bb 100644
--- a/scripts/Kconfig.toolchain
+++ b/scripts/Kconfig.toolchain
@@ -236,6 +236,9 @@ config CC_OPT_WNO_STRINGOP_TRUNCATION
 config CC_OPT_WNO_CONSTANT_LOGICAL_OPERAND
 	def_string "$(cc-option-str,-Wno-constant-logical-operand)"
 
+config CC_OPT_WNO_UNSEQUENCED_VOLATILE
+	def_string "$(cc-option-str,-Wno-unsequenced-volatile)"
+
 config CC_HAS_COUNTED_BY
 	bool
 	# clang needs to be at least 20.1.0 to avoid potential crashes

> and finally this diagnostic was forked into a group; it's now
> -Wunsequenced-volatile under the -Wunsequenced group, that could go in
> W=1).

Moving this to W=1 won't really solve the problem because many people
build with W=1 by default so you'll still get reports and patches trying
to fix it. It either needs to be this patch or one of the ones above.

> I think the warning has value generally across the tree, even if this
> particular single instance "isn't a bug, per se."

Hard agree. Just because this particular instance is not a bug does not
mean future instances won't be. We pay a small cost to silence the
initial instances then we get to see what it flags in the future. If it
turns out there are never bugs, we can just turn the warning off
globally (we've done it before) but I'd rather have hard evidence of
that first; to me, these initial instances don't count. No maintainer
has objected to the other patches I have sent for the other instances of
this warning and there were not all that many for how much code the
kernel has.

-- 
Cheers,
Nathan

  reply	other threads:[~2026-11-25 20:03 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25  7:23 [PATCH v3] sched/topology: Avoid unsequenced accesses to jiffies in sd_init() Nathan Chancellor
2026-11-25  9:46 ` Peter Zijlstra
2026-11-25 19:32   ` Nick Desaulniers
2026-11-25 20:03     ` Nathan Chancellor [this message]
2026-11-26 11:50       ` Peter Zijlstra
2026-11-26 12:08         ` Peter Zijlstra
2026-11-26 12:27           ` Peter Zijlstra

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=20261125200311.GA3625068@ax162 \
    --to=nathan@kernel.org \
    --cc=bsegall@google.com \
    --cc=dietmar.eggemann@arm.com \
    --cc=juri.lelli@redhat.com \
    --cc=justinstitt@google.com \
    --cc=kprateek.nayak@amd.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --cc=morbo@google.com \
    --cc=ndesaulniers@google.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=stable@vger.kernel.org \
    --cc=vincent.guittot@linaro.org \
    --cc=vschneid@redhat.com \
    /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