The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH] cpumask: Ensure the visibility of set_nr_cpu_ids
@ 2024-08-11  9:25 I Hsin Cheng
  2024-08-11 15:18 ` Yury Norov
  0 siblings, 1 reply; 3+ messages in thread
From: I Hsin Cheng @ 2024-08-11  9:25 UTC (permalink / raw)
  To: yury.norov; +Cc: andriy.shevchenko, linux, linux-kernel, I Hsin Cheng

The variable "nr_cpu_ids" is a system-wide variable which should be seen
as consistent all the time. For example it's set in one of the kernel
setup procedure "prefill_possible_map", the operations here should
happens before all the code after setup, which means the operations here
should be visible to all the code after setup.

set_cpu_possible() ensure it's visibility because it eventually falls
into an atomic instruction, however the function "set_nr_cpu_ids()"
fails to make the guarantee since it only performs a normal write
operations.

Adding the macro "WRITE_ONCE()" will prevent the compiler from re-order
the instruction of the write operation for "nr_cpu_ids", so we can
guarantee the operation is visible to all the codes coming after it.

Signed-off-by: I Hsin Cheng <richard120310@gmail.com>
---
 include/linux/cpumask.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h
index f10fb87d4..3731f5e43 100644
--- a/include/linux/cpumask.h
+++ b/include/linux/cpumask.h
@@ -46,7 +46,7 @@ static inline void set_nr_cpu_ids(unsigned int nr)
 #if (NR_CPUS == 1) || defined(CONFIG_FORCE_NR_CPUS)
 	WARN_ON(nr != nr_cpu_ids);
 #else
-	nr_cpu_ids = nr;
+	WRITE_ONCE(nr_cpu_ids, nr);
 #endif
 }
 
-- 
2.34.1


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

end of thread, other threads:[~2024-08-11 17:37 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-11  9:25 [PATCH] cpumask: Ensure the visibility of set_nr_cpu_ids I Hsin Cheng
2024-08-11 15:18 ` Yury Norov
2024-08-11 17:37   ` I Hsin Cheng

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