mm/percpu: prevent concurrency problem for pcpu_nr_populated read with spin lock
Subsystems (2)
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.
---
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
@@ -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); }/*
--
Quoted 22 lines · Jeongjun Park
pcpu_nr_pages()readspcpu_nr_populatedwithout any protection, which causes a data race between read/write.However, since this is an intended race, we should add a
data_raceannotation instead of add a spin lock.Reported-by [email protected]Fixes 7e8a6304d541 ("/proc/meminfo: add percpu populated pages count")Suggested-by Shakeel Butt <[email protected]>Signed-off-by Jeongjun Park <[email protected]>---
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
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}/* --
Andrew Morton
maintainerQuoted 18 lines · On Fri, 11 Jul 2025 17:56:34 +0200 Vlastimil Babka <[email protected]> wrote:
Quoted 17 lines · Jeongjun Park
pcpu_nr_pages()readspcpu_nr_populatedwithout any protection, which causes a data race between read/write.However, since this is an intended race, we should add a
data_raceannotation instead of add a spin lock.Reported-by [email protected]Fixes 7e8a6304d541 ("/proc/meminfo: add percpu populated pages count")Suggested-by Shakeel Butt <[email protected]>Signed-off-by Jeongjun Park <[email protected]>---
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@@ -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_unitsshould be excluded from the scope ofdata_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; }/*
_
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()readspcpu_nr_populatedwithout any protection, which causes a data race between read/write.However, since this is an intended race, we should add a
data_raceannotation instead of add a spin lock.Reported-by [email protected]Fixes 7e8a6304d541 ("/proc/meminfo: add percpu populated pages count")Suggested-by Shakeel Butt <[email protected]>Signed-off-by Jeongjun Park <[email protected]>---
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@@ -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_unitsshould be excluded from the scope ofdata_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}/* _