mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v3] sched/topology: Avoid unsequenced accesses to jiffies in sd_init()
@ 2026-11-25  7:23 Nathan Chancellor
  2026-11-25  9:46 ` Peter Zijlstra
  0 siblings, 1 reply; 4+ messages in thread
From: Nathan Chancellor @ 2026-11-25  7:23 UTC (permalink / raw)
  To: Ingo Molnar, Peter Zijlstra, Juri Lelli, Vincent Guittot
  Cc: Dietmar Eggemann, Steven Rostedt, Ben Segall, Mel Gorman,
	Valentin Schneider, K Prateek Nayak, Nick Desaulniers,
	Bill Wendling, Justin Stitt, linux-kernel, llvm, stable,
	Nathan Chancellor

After a recent improvement in clang to warn on unsequenced accesses to
volatile variables [1], an instance appears in sd_init(), which can
break the build with CONFIG_WERROR=y / W=e:

  In file included from kernel/sched/build_utility.c:86:
  kernel/sched/topology.c:2008:29: error: unsequenced accesses to volatile qualified 'jiffies' [-Werror,-Wunsequenced]
   1998 |                 .last_balance           = jiffies,
        |                                           ~~~~~~~
   1999 |                 .balance_interval       = sd_weight,
   2000 |
   2001 |                 /* 50% success rate */
   2002 |                 .newidle_call           = 512,
   2003 |                 .newidle_success        = 256,
   2004 |                 .newidle_ratio          = 512,
   2005 |                 .newidle_stamp          = now,
   2006 |
   2007 |                 .max_newidle_lb_cost    = 0,
   2008 |                 .last_decay_max_lb_cost = jiffies,
        |                                           ^

Per C11 5.1.2.3.2, accessing a volatile object is a side effect. Per C11
6.5.2, unsequenced side effects on the same scalar object are undefined
behavior. Per C11 6.7.9.23, the evaluation of the initialization list
expressions in a designated initializer is indeterminately sequenced
relative to each other, meaning any side effects in the initialization
list are unsequenced.

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.

Cc: stable@vger.kernel.org
Closes: https://github.com/ClangBuiltLinux/linux/issues/2190
Fixes: 2b4cfe64dee0 ("sched/numa: Initialize newidle balance stats in sd_numa_init()")
Link: https://github.com/llvm/llvm-project/commit/955715460eaf9188c241c74750637fc82dfcc423 [1]
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
Signed-off-by: Nathan Chancellor <nathan@kernel.org>
---
Changes in v3:
- Improve commit message to give more context into warning
- Carry forward Nick's review tag
- Link to v2: https://patch.msgid.link/20261116-topology-wunsequenced-v2-1-54611119172b@kernel.org

Changes in v2:
- Use a local variable with one access to jiffies before designated
  initializer (Nick)
- Link to v1: https://patch.msgid.link/20261114-topology-wunsequenced-v1-1-686a36fe61f5@kernel.org
---
 kernel/sched/topology.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c
index 99878da03bce..60fad1690894 100644
--- a/kernel/sched/topology.c
+++ b/kernel/sched/topology.c
@@ -1956,6 +1956,7 @@ sd_init(struct sched_domain_topology_level *tl,
 	int sd_id, sd_weight, sd_flags = 0;
 	struct cpumask *sd_span;
 	u64 now = sched_clock();
+	unsigned long j;
 
 	sd_span = sched_domain_span(sd);
 	cpumask_and(sd_span, cpu_map, tl->mask(tl, cpu));
@@ -1973,6 +1974,7 @@ sd_init(struct sched_domain_topology_level *tl,
 #endif
 	sd_flags |= asym_cpu_capacity_classify(sd_span, cpu_map);
 
+	j = jiffies;
 	*sd = (struct sched_domain){
 		.min_interval		= sd_weight,
 		.max_interval		= 2*sd_weight,
@@ -1994,7 +1996,7 @@ sd_init(struct sched_domain_topology_level *tl,
 					| sd_flags
 					,
 
-		.last_balance		= jiffies,
+		.last_balance		= j,
 		.balance_interval	= sd_weight,
 
 		/* 50% success rate */
@@ -2004,7 +2006,7 @@ sd_init(struct sched_domain_topology_level *tl,
 		.newidle_stamp		= now,
 
 		.max_newidle_lb_cost	= 0,
-		.last_decay_max_lb_cost	= jiffies,
+		.last_decay_max_lb_cost	= j,
 		.child			= child,
 		.name			= tl->name,
 	};

---
base-commit: 3dd8c790a7b7027abdd568e36db5e82ffaea7c77
base-tree: git://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git master
change-id: 20261125-topology-wunsequenced-41b30ec6bc95

Best regards,
--  
Cheers,
Nathan


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

* Re: [PATCH v3] sched/topology: Avoid unsequenced accesses to jiffies in sd_init()
  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
  0 siblings, 1 reply; 4+ messages in thread
From: Peter Zijlstra @ 2026-11-25  9:46 UTC (permalink / raw)
  To: Nathan Chancellor
  Cc: Ingo Molnar, Juri Lelli, Vincent Guittot, Dietmar Eggemann,
	Steven Rostedt, Ben Segall, Mel Gorman, Valentin Schneider,
	K Prateek Nayak, Nick Desaulniers, Bill Wendling, Justin Stitt,
	linux-kernel, llvm, stable

On Wed, Nov 25, 2026 at 12:23:35AM -0700, Nathan Chancellor wrote:
> After a recent improvement in clang to warn on unsequenced accesses to
> volatile variables [1], an instance appears in sd_init(), which can
> break the build with CONFIG_WERROR=y / W=e:
> 
>   In file included from kernel/sched/build_utility.c:86:
>   kernel/sched/topology.c:2008:29: error: unsequenced accesses to volatile qualified 'jiffies' [-Werror,-Wunsequenced]
>    1998 |                 .last_balance           = jiffies,
>         |                                           ~~~~~~~
>    1999 |                 .balance_interval       = sd_weight,
>    2000 |
>    2001 |                 /* 50% success rate */
>    2002 |                 .newidle_call           = 512,
>    2003 |                 .newidle_success        = 256,
>    2004 |                 .newidle_ratio          = 512,
>    2005 |                 .newidle_stamp          = now,
>    2006 |
>    2007 |                 .max_newidle_lb_cost    = 0,
>    2008 |                 .last_decay_max_lb_cost = jiffies,
>         |                                           ^
> 
> Per C11 5.1.2.3.2, accessing a volatile object is a side effect. Per C11
> 6.5.2, unsequenced side effects on the same scalar object are undefined
> behavior. Per C11 6.7.9.23, the evaluation of the initialization list
> expressions in a designated initializer is indeterminately sequenced
> relative to each other, meaning any side effects in the initialization
> list are unsequenced.
> 
> 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.

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

* Re: [PATCH v3] sched/topology: Avoid unsequenced accesses to jiffies in sd_init()
  2026-11-25  9:46 ` Peter Zijlstra
@ 2026-11-25 19:32   ` Nick Desaulniers
  2026-11-25 20:03     ` Nathan Chancellor
  0 siblings, 1 reply; 4+ messages in thread
From: Nick Desaulniers @ 2026-11-25 19:32 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: Nathan Chancellor, Ingo Molnar, Juri Lelli, Vincent Guittot,
	Dietmar Eggemann, Steven Rostedt, Ben Segall, Mel Gorman,
	Valentin Schneider, K Prateek Nayak, Bill Wendling, Justin Stitt,
	linux-kernel, llvm, stable

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.

Nathan has provided you 3 different ways to do so. (Could also do ugly
pragma's around this code; disable it for this entire source file via
-Wno-unsequenced-volatile in the Makefile, and finally this diagnostic
was forked into a group; it's now -Wunsequenced-volatile under the
-Wunsequenced group, that could go in W=1).  Pick one?

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

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

* Re: [PATCH v3] sched/topology: Avoid unsequenced accesses to jiffies in sd_init()
  2026-11-25 19:32   ` Nick Desaulniers
@ 2026-11-25 20:03     ` Nathan Chancellor
  0 siblings, 0 replies; 4+ messages in thread
From: Nathan Chancellor @ 2026-11-25 20:03 UTC (permalink / raw)
  To: Nick Desaulniers
  Cc: Peter Zijlstra, Ingo Molnar, Juri Lelli, Vincent Guittot,
	Dietmar Eggemann, Steven Rostedt, Ben Segall, Mel Gorman,
	Valentin Schneider, K Prateek Nayak, Bill Wendling, Justin Stitt,
	linux-kernel, llvm, stable

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

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

end of thread, other threads:[~2026-11-25 20:03 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 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®