* [PATCH v2] sched/topology: Avoid unsequenced accesses to jiffies in sd_init()
@ 2026-11-16 20:55 Nathan Chancellor
2026-11-17 0:39 ` Nick Desaulniers
2026-11-17 18:30 ` Peter Zijlstra
0 siblings, 2 replies; 7+ messages in thread
From: Nathan Chancellor @ 2026-11-16 20:55 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,
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.
Add a local variable to hold the value of jiffies before the designated
initializer so that there are no unsequenced side effects while using a
consistent value, 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]
Signed-off-by: Nathan Chancellor <nathan@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: d7e670601af6c564ccf6d3b2ddf70ee4f41a6d1c
base-tree: git://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git master
change-id: 20261114-topology-wunsequenced-18f1d3b9167c
Best regards,
--
Cheers,
Nathan
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH v2] sched/topology: Avoid unsequenced accesses to jiffies in sd_init()
2026-11-16 20:55 [PATCH v2] sched/topology: Avoid unsequenced accesses to jiffies in sd_init() Nathan Chancellor
@ 2026-11-17 0:39 ` Nick Desaulniers
2026-11-17 18:30 ` Peter Zijlstra
1 sibling, 0 replies; 7+ messages in thread
From: Nick Desaulniers @ 2026-11-17 0:39 UTC (permalink / raw)
To: Nathan Chancellor
Cc: Ingo Molnar, Peter Zijlstra, Juri Lelli, Vincent Guittot,
Dietmar Eggemann, Steven Rostedt, Ben Segall, Mel Gorman,
Valentin Schneider, K Prateek Nayak, linux-kernel, llvm, stable
On Mon, Nov 16, 2026 at 12:55 PM Nathan Chancellor <nathan@kernel.org> 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.
>
> Add a local variable to hold the value of jiffies before the designated
> initializer so that there are no unsequenced side effects while using a
> consistent value, 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]
> Signed-off-by: Nathan Chancellor <nathan@kernel.org>
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
> ---
> 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: d7e670601af6c564ccf6d3b2ddf70ee4f41a6d1c
> base-tree: git://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git master
> change-id: 20261114-topology-wunsequenced-18f1d3b9167c
>
> Best regards,
> --
> Cheers,
> Nathan
>
--
Thanks,
~Nick Desaulniers
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH v2] sched/topology: Avoid unsequenced accesses to jiffies in sd_init()
2026-11-16 20:55 [PATCH v2] sched/topology: Avoid unsequenced accesses to jiffies in sd_init() Nathan Chancellor
2026-11-17 0:39 ` Nick Desaulniers
@ 2026-11-17 18:30 ` Peter Zijlstra
2026-11-17 18:39 ` Nick Desaulniers
1 sibling, 1 reply; 7+ messages in thread
From: Peter Zijlstra @ 2026-11-17 18:30 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, linux-kernel, llvm, stable
On Mon, Nov 16, 2026 at 01:55:26PM -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.
>
> Add a local variable to hold the value of jiffies before the designated
> initializer so that there are no unsequenced side effects while using a
> consistent value, 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]
> Signed-off-by: Nathan Chancellor <nathan@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,
> };
I don't speak enough C legal to actually understand the Changelog, but
the actual change seems daft. Is this really needed?
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH v2] sched/topology: Avoid unsequenced accesses to jiffies in sd_init()
2026-11-17 18:30 ` Peter Zijlstra
@ 2026-11-17 18:39 ` Nick Desaulniers
2026-11-18 7:57 ` Nathan Chancellor
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: Nick Desaulniers @ 2026-11-17 18:39 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, linux-kernel, llvm, stable
On Tue, Nov 17, 2026 at 10:30 AM Peter Zijlstra <peterz@infradead.org> wrote:
>
> I don't speak enough C legal to actually understand the Changelog, but
> the actual change seems daft. Is this really needed?
I would imagine a delta of the disassembly here before/after the
change would make it more obvious (one less volatile access, which be
have arbitrary ordering between initializing other members).
But I guess I'm also curious why jiffies is defined as volatile
anyway? I thought the usual guidance was to use
cast-to-volatile-qualified pointer during access, rather than declare
as volatile?
--
Thanks,
~Nick Desaulniers
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH v2] sched/topology: Avoid unsequenced accesses to jiffies in sd_init()
2026-11-17 18:39 ` Nick Desaulniers
@ 2026-11-18 7:57 ` Nathan Chancellor
2026-11-18 9:47 ` Peter Zijlstra
2026-11-18 10:03 ` David Laight
2 siblings, 0 replies; 7+ messages in thread
From: Nathan Chancellor @ 2026-11-18 7:57 UTC (permalink / raw)
To: Nick Desaulniers, Peter Zijlstra
Cc: Ingo Molnar, Juri Lelli, Vincent Guittot, Dietmar Eggemann,
Steven Rostedt, Ben Segall, Mel Gorman, Valentin Schneider,
K Prateek Nayak, linux-kernel, llvm, stable
On Tue, Nov 17, 2026 at 10:39:15AM -0800, Nick Desaulniers wrote:
> On Tue, Nov 17, 2026 at 10:30 AM Peter Zijlstra <peterz@infradead.org> wrote:
> >
> > I don't speak enough C legal to actually understand the Changelog, but
> > the actual change seems daft. Is this really needed?
Yes, I would say this is needed. I am far from a C lawyer myself but I
think this is like any other undefined behavior: it might not be a real
problem now but it could always become one later.
> I would imagine a delta of the disassembly here before/after the
> change would make it more obvious (one less volatile access, which be
> have arbitrary ordering between initializing other members).
>
> But I guess I'm also curious why jiffies is defined as volatile
> anyway? I thought the usual guidance was to use
> cast-to-volatile-qualified pointer during access, rather than declare
> as volatile?
Maybe because jiffies is accessed a lot across the tree?
--
Cheers,
Nathan
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2] sched/topology: Avoid unsequenced accesses to jiffies in sd_init()
2026-11-17 18:39 ` Nick Desaulniers
2026-11-18 7:57 ` Nathan Chancellor
@ 2026-11-18 9:47 ` Peter Zijlstra
2026-11-18 10:03 ` David Laight
2 siblings, 0 replies; 7+ messages in thread
From: Peter Zijlstra @ 2026-11-18 9:47 UTC (permalink / raw)
To: Nick Desaulniers
Cc: Nathan Chancellor, Ingo Molnar, Juri Lelli, Vincent Guittot,
Dietmar Eggemann, Steven Rostedt, Ben Segall, Mel Gorman,
Valentin Schneider, K Prateek Nayak, linux-kernel, llvm, stable
On Tue, Nov 17, 2026 at 10:39:15AM -0800, Nick Desaulniers wrote:
> On Tue, Nov 17, 2026 at 10:30 AM Peter Zijlstra <peterz@infradead.org> wrote:
> >
> > I don't speak enough C legal to actually understand the Changelog, but
> > the actual change seems daft. Is this really needed?
>
> I would imagine a delta of the disassembly here before/after the
> change would make it more obvious (one less volatile access, which be
> have arbitrary ordering between initializing other members).
Could it be fixed by adding an explicit cast? Say:
.last_balance = (unsigned long)jiffies,
> But I guess I'm also curious why jiffies is defined as volatile
> anyway? I thought the usual guidance was to use
> cast-to-volatile-qualified pointer during access, rather than declare
> as volatile?
jiffies is ancient; it stems from before we had READ_ONCE() and friends.
It used to be that task_struct::state was also declared volatile, but
I've since fixed that.
Mostly, you want to always read time, and not have time optimized
'away'. Like lift a jiffies read out of a loop and break the timeout
condition and all that.
Also, the C people are daft and want to get rid of volatile,
irrespective of its many uses, so perhaps this is a subtle act of
sabotage or something and we should get clang to revert this weirdness.
I think the thinking was that with _Atomic there is no further need of
volatile and its fine to break the many billions of lines of
pre-existing C code or something stupid like that :/
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2] sched/topology: Avoid unsequenced accesses to jiffies in sd_init()
2026-11-17 18:39 ` Nick Desaulniers
2026-11-18 7:57 ` Nathan Chancellor
2026-11-18 9:47 ` Peter Zijlstra
@ 2026-11-18 10:03 ` David Laight
2 siblings, 0 replies; 7+ messages in thread
From: David Laight @ 2026-11-18 10:03 UTC (permalink / raw)
To: Nick Desaulniers
Cc: Peter Zijlstra, Nathan Chancellor, Ingo Molnar, Juri Lelli,
Vincent Guittot, Dietmar Eggemann, Steven Rostedt, Ben Segall,
Mel Gorman, Valentin Schneider, K Prateek Nayak, linux-kernel,
llvm, stable
On Tue, 17 Nov 2026 10:39:15 -0800
Nick Desaulniers <ndesaulniers@google.com> wrote:
> On Tue, Nov 17, 2026 at 10:30 AM Peter Zijlstra <peterz@infradead.org> wrote:
> >
> > I don't speak enough C legal to actually understand the Changelog, but
> > the actual change seems daft. Is this really needed?
A better description might be saying something about ensuring the two
jiffies values are the same.
As coded they can be different with either larger.
> I would imagine a delta of the disassembly here before/after the
> change would make it more obvious (one less volatile access, which be
> have arbitrary ordering between initializing other members).
>
> But I guess I'm also curious why jiffies is defined as volatile
> anyway? I thought the usual guidance was to use
> cast-to-volatile-qualified pointer during access, rather than declare
> as volatile?
I'm sure that guidance was based on the fact that not all accesses needed
to be volatile.
But now the tools are getting picky and needing WRITE_ONCE() as well as
READ_ONCE() and bleating about plain accesses maybe it is less relevant.
So if all access have to be volatile why not make the variable volatile?
Doesn't this initialiser also generate 'really crap code'?
IIRC it is likely to generate an on-stack structure and then do a memcpy().
David
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-11-18 10:03 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-16 20:55 [PATCH v2] sched/topology: Avoid unsequenced accesses to jiffies in sd_init() Nathan Chancellor
2026-11-17 0:39 ` Nick Desaulniers
2026-11-17 18:30 ` Peter Zijlstra
2026-11-17 18:39 ` Nick Desaulniers
2026-11-18 7:57 ` Nathan Chancellor
2026-11-18 9:47 ` Peter Zijlstra
2026-11-18 10:03 ` David Laight
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox