Skip to contentSkip to conversation

mm/percpu: prevent concurrency problem for pcpu_nr_populated read with spin lock

PATCH Applied Jeongjun Park 3 participants 4 messages Jul 3, 2025 – Jul 14, 2025

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

PATCH v2 1/1 mm/percpu: prevent concurrency problem for pcpu_nr_populated read with spin lock

pcpu_nr_pages() reads pcpu_nr_populated without any protection, which causes a data race between read/write.

However, since this is an intended race, we should add a data_race annotation instead of add a spin lock.

Reported-by [email protected]
Fixes 7e8a6304d541 ("/proc/meminfo: add percpu populated pages count")

---
v2: Change it as suggested by Shakeel Butt
- Link to v1: https://lore.kernel.org/all/[email protected]/

---
 mm/percpu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/percpu.c +1 -1
index b35494c8ede2..782cc148b39c 100644
@@ -3355,7 +3355,7 @@ void __init setup_per_cpu_areas(void)
  */
 unsigned long pcpu_nr_pages(void)
 {
-	return pcpu_nr_populated * pcpu_nr_units;
+	return data_race(READ_ONCE(pcpu_nr_populated) * pcpu_nr_units);
 }
 
 /*

--

#1 lore raw
Quoted 22 lines · Jeongjun Park

pcpu_nr_pages() reads pcpu_nr_populated without any protection, which causes a data race between read/write.

However, since this is an intended race, we should add a data_race annotation instead of add a spin lock.

Reported-by [email protected]
Fixes 7e8a6304d541 ("/proc/meminfo: add percpu populated pages count")

---
v2: Change it as suggested by Shakeel Butt
- Link to v1: https://lore.kernel.org/all/[email protected]/

---
 mm/percpu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/percpu.c +0 -0
index b35494c8ede2..782cc148b39c 100644

mm/percpu.c void __init setup_per_cpu_areas(void) · line 3358

Quoted 5 lines
mm/percpu.c +1 -1
@@ -3355,7 +3355,7 @@ void __init setup_per_cpu_areas(void)
  */
 unsigned long pcpu_nr_pages(void)
 {
-	return pcpu_nr_populated * pcpu_nr_units;
+	return data_race(READ_ONCE(pcpu_nr_populated) * pcpu_nr_units);

Nit: pcpu_nr_units should be excluded from the scope of data_race() as no race can happen there.

Quoted 4 lines
mm/percpu.c +0 -1
 }
 
 /*
--
maintainer
#2 lore raw
Quoted 18 lines · On Fri, 11 Jul 2025 17:56:34 +0200 Vlastimil Babka <[email protected]> wrote:
Quoted 17 lines · Jeongjun Park

pcpu_nr_pages() reads pcpu_nr_populated without any protection, which causes a data race between read/write.

However, since this is an intended race, we should add a data_race annotation instead of add a spin lock.

Reported-by [email protected]
Fixes 7e8a6304d541 ("/proc/meminfo: add percpu populated pages count")

---
v2: Change it as suggested by Shakeel Butt
- Link to v1: https://lore.kernel.org/all/[email protected]/

---
 mm/percpu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 13 lines

mm/percpu.c void __init setup_per_cpu_areas(void) · line 3358

Quoted 10 lines
mm/percpu.c +1 -1
index b35494c8ede2..782cc148b39c 100644
@@ -3355,7 +3355,7 @@ void __init setup_per_cpu_areas(void)
  */
 unsigned long pcpu_nr_pages(void)
 {
-	return pcpu_nr_populated * pcpu_nr_units;
+	return data_race(READ_ONCE(pcpu_nr_populated) * pcpu_nr_units);

Nit: pcpu_nr_units should be excluded from the scope of data_race() as no race can happen there.

This?

mm/percpu.c renamed from mm/percpu.c~mm-percpu-prevent-concurrency-problem-for-pcpu_nr_populated-read-with-spin-lock-fix +1 -1
@@ -3355,7 +3355,7 @@ void __init setup_per_cpu_areas(void)
  */
 unsigned long pcpu_nr_pages(void)
 {
-	return data_race(READ_ONCE(pcpu_nr_populated) * pcpu_nr_units);
+	return data_race(READ_ONCE(pcpu_nr_populated)) * pcpu_nr_units;
 }
 
 /*

_

#3 lore raw
Quoted 39 lines · Andrew Morton
Quoted 18 lines · On Fri, 11 Jul 2025 17:56:34 +0200 Vlastimil Babka <[email protected]> wrote:
Quoted 17 lines · Jeongjun Park

pcpu_nr_pages() reads pcpu_nr_populated without any protection, which causes a data race between read/write.

However, since this is an intended race, we should add a data_race annotation instead of add a spin lock.

Reported-by [email protected]
Fixes 7e8a6304d541 ("/proc/meminfo: add percpu populated pages count")

---
v2: Change it as suggested by Shakeel Butt
- Link to v1: https://lore.kernel.org/all/[email protected]/

---
 mm/percpu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 13 lines

mm/percpu.c void __init setup_per_cpu_areas(void) · line 3358

Quoted 10 lines
mm/percpu.c +1 -1
index b35494c8ede2..782cc148b39c 100644
@@ -3355,7 +3355,7 @@ void __init setup_per_cpu_areas(void)
  */
 unsigned long pcpu_nr_pages(void)
 {
-	return pcpu_nr_populated * pcpu_nr_units;
+	return data_race(READ_ONCE(pcpu_nr_populated) * pcpu_nr_units);

Nit: pcpu_nr_units should be excluded from the scope of data_race() as no race can happen there.

This?

mm/percpu.c void __init setup_per_cpu_areas(void) · line 3358

Quoted 5 lines
mm/percpu.c renamed from mm/percpu.c~mm-percpu-prevent-concurrency-problem-for-pcpu_nr_populated-read-with-spin-lock-fix +1 -1
@@ -3355,7 +3355,7 @@ void __init setup_per_cpu_areas(void)
  */
 unsigned long pcpu_nr_pages(void)
 {
-	return data_race(READ_ONCE(pcpu_nr_populated) * pcpu_nr_units);
+	return data_race(READ_ONCE(pcpu_nr_populated)) * pcpu_nr_units;

Yes, thanks!

Quoted 5 lines
mm/percpu.c renamed from mm/percpu.c~mm-percpu-prevent-concurrency-problem-for-pcpu_nr_populated-read-with-spin-lock-fix +0 -0
 }
 
 /*

_