Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH] sched/topology: Avoid unsequenced accesses to jiffies in sd_init()
@ 2026-11-15  4:19 Nathan Chancellor
  2026-11-16 18:36 ` Nick Desaulniers
  0 siblings, 1 reply; 3+ messages in thread
From: Nathan Chancellor @ 2026-11-15  4:19 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, Ingo Molnar, 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.

Move the initialization of these fields outside the designated
initializer to place sequence points between the accesses to jiffies,
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>
---
 kernel/sched/topology.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c
index 99878da03bce..f5975079f453 100644
--- a/kernel/sched/topology.c
+++ b/kernel/sched/topology.c
@@ -1994,7 +1994,6 @@ sd_init(struct sched_domain_topology_level *tl,
 					| sd_flags
 					,
 
-		.last_balance		= jiffies,
 		.balance_interval	= sd_weight,
 
 		/* 50% success rate */
@@ -2004,10 +2003,11 @@ sd_init(struct sched_domain_topology_level *tl,
 		.newidle_stamp		= now,
 
 		.max_newidle_lb_cost	= 0,
-		.last_decay_max_lb_cost	= jiffies,
 		.child			= child,
 		.name			= tl->name,
 	};
+	sd->last_balance = jiffies;
+	sd->last_decay_max_lb_cost = jiffies;
 
 	WARN_ONCE((sd->flags & (SD_SHARE_CPUCAPACITY | SD_ASYM_CPUCAPACITY)) ==
 		  (SD_SHARE_CPUCAPACITY | SD_ASYM_CPUCAPACITY),

---
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] 3+ messages in thread

* Re: [PATCH] sched/topology: Avoid unsequenced accesses to jiffies in sd_init()
  2026-11-15  4:19 [PATCH] sched/topology: Avoid unsequenced accesses to jiffies in sd_init() Nathan Chancellor
@ 2026-11-16 18:36 ` Nick Desaulniers
  2026-11-16 18:54   ` Nathan Chancellor
  0 siblings, 1 reply; 3+ messages in thread
From: Nick Desaulniers @ 2026-11-16 18:36 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, Ingo Molnar, linux-kernel,
	llvm, stable

On Sat, Nov 14, 2026 at 09:19:52PM -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.
> 
> Move the initialization of these fields outside the designated
> initializer to place sequence points between the accesses to jiffies,
> 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>
> ---
>  kernel/sched/topology.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c
> index 99878da03bce..f5975079f453 100644
> --- a/kernel/sched/topology.c
> +++ b/kernel/sched/topology.c
> @@ -1994,7 +1994,6 @@ sd_init(struct sched_domain_topology_level *tl,
>  					| sd_flags
>  					,
>  
> -		.last_balance		= jiffies,
>  		.balance_interval	= sd_weight,
>  
>  		/* 50% success rate */
> @@ -2004,10 +2003,11 @@ sd_init(struct sched_domain_topology_level *tl,
>  		.newidle_stamp		= now,
>  
>  		.max_newidle_lb_cost	= 0,
> -		.last_decay_max_lb_cost	= jiffies,
>  		.child			= child,
>  		.name			= tl->name,
>  	};
> +	sd->last_balance = jiffies;
> +	sd->last_decay_max_lb_cost = jiffies;

This pattern is going to wind up with two volatile (expensive) accesses,
(with potentially different values) rather than one access and the same
value by accessing `jiffies` once then storing the result in a temporary
local (before the designated initializer), then using that to initialize
the two fields in the designated initializer.

Example: https://godbolt.org/z/h8bvvKK6j

>  
>  	WARN_ONCE((sd->flags & (SD_SHARE_CPUCAPACITY | SD_ASYM_CPUCAPACITY)) ==
>  		  (SD_SHARE_CPUCAPACITY | SD_ASYM_CPUCAPACITY),
> 
> ---
> 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	[flat|nested] 3+ messages in thread

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

On Mon, Nov 16, 2026 at 10:36:45AM -0800, Nick Desaulniers wrote:
> On Sat, Nov 14, 2026 at 09:19:52PM -0700, Nathan Chancellor wrote:
> > +	sd->last_balance = jiffies;
> > +	sd->last_decay_max_lb_cost = jiffies;
> 
> This pattern is going to wind up with two volatile (expensive) accesses,
> (with potentially different values) rather than one access and the same
> value by accessing `jiffies` once then storing the result in a temporary
> local (before the designated initializer), then using that to initialize
> the two fields in the designated initializer.

Thanks, I should have revisited this solution once I had a wider view of
the scope of this warning (it was one of the first patches I wrote)
because I definitely would have done that. I'll respin.

-- 
Cheers,
Nathan

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

end of thread, other threads:[~2026-11-16 18:55 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-15  4:19 [PATCH] sched/topology: Avoid unsequenced accesses to jiffies in sd_init() Nathan Chancellor
2026-11-16 18:36 ` Nick Desaulniers
2026-11-16 18:54   ` Nathan Chancellor

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