Skip to contentSkip to conversation

sched/topology: Avoid unsequenced accesses to jiffies in sd_init()

PATCH In review Nathan Chancellor 2 participants 2 messages Nov 25

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 sched/topology: Avoid unsequenced accesses to jiffies in sd_init()

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.

---
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/[email protected]

Changes in v2:
- Use a local variable with one access to jiffies before designated
initializer (Nick)
- Link to v1: https://patch.msgid.link/[email protected]

---
 kernel/sched/topology.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/sched/topology.c +4 -2
index 99878da03bce..60fad1690894 100644
@@ -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,

Signature · 2 lines
Cheers,
Nathan
maintainer
#1 lore raw
Quoted 34 lines · 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.

Quoted 4 lines

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.