Skip to contentSkip to conversation

kernel/smp: Fix an off by one in csd_lock_wait_toolong()

PATCH In review Dan Carpenter 4 participants 6 messages Jul 9, 2020 Kernel janitors

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kernel/smp: Fix an off by one in csd_lock_wait_toolong()

The __per_cpu_offset[] array has "nr_cpu_ids" elements so change the >

Quoted 1 line

= to prevent a read one element beyond the end of the array.

Fixes 0504bc41a62c ("kernel/smp: Provide CSD lock timeout diagnostics")
---
 kernel/smp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/smp.c +1 -1
index 78b602cae6c2..f49966713ac3 100644
@@ -171,7 +171,7 @@ static __always_inline bool csd_lock_wait_toolong(call_single_data_t *csd, u64 t
 		*bug_id = atomic_inc_return(&csd_bug_count);
 	cpu = csd_lock_wait_getcpu(csd);
 	smp_mb(); // No stale cur_csd values!
-	if (WARN_ONCE(cpu < 0 || cpu > nr_cpu_ids, "%s: cpu = %d\n", __func__, cpu))
+	if (WARN_ONCE(cpu < 0 || cpu >= nr_cpu_ids, "%s: cpu = %d\n", __func__, cpu))
 		cpu_cur_csd = READ_ONCE(per_cpu(cur_csd, 0));
 	else
 		cpu_cur_csd = READ_ONCE(per_cpu(cur_csd, cpu));
Signature · 1 line
2.27.0
#1 lore raw
Quoted 4 lines · Dan Carpenter

The __per_cpu_offset[] array has "nr_cpu_ids" elements so change the >

Quoted 1 line

= to prevent a read one element beyond the end of the array.

Fixes 0504bc41a62c ("kernel/smp: Provide CSD lock timeout diagnostics")

I don't have a copy of that patch in my inbox, even though it says Cc:
me.

Paul, where do you expect that patch to go? The version I see from my next tree needs a _lot_ of work.

Quoted 21 lines
---
 kernel/smp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/smp.c +1 -1
index 78b602cae6c2..f49966713ac3 100644
@@ -171,7 +171,7 @@ static __always_inline bool csd_lock_wait_toolong(call_single_data_t *csd, u64 t
 		*bug_id = atomic_inc_return(&csd_bug_count);
 	cpu = csd_lock_wait_getcpu(csd);
 	smp_mb(); // No stale cur_csd values!
-	if (WARN_ONCE(cpu < 0 || cpu > nr_cpu_ids, "%s: cpu = %d\n", __func__, cpu))
+	if (WARN_ONCE(cpu < 0 || cpu >= nr_cpu_ids, "%s: cpu = %d\n", __func__, cpu))
 		cpu_cur_csd = READ_ONCE(per_cpu(cur_csd, 0));
 	else
 		cpu_cur_csd = READ_ONCE(per_cpu(cur_csd, cpu));
Signature · 1 line
2.27.0
#2 lore raw
Quoted 11 lines · Peter Zijlstra
Quoted 4 lines · Dan Carpenter

The __per_cpu_offset[] array has "nr_cpu_ids" elements so change the >

Quoted 1 line

= to prevent a read one element beyond the end of the array.

Fixes 0504bc41a62c ("kernel/smp: Provide CSD lock timeout diagnostics")

I don't have a copy of that patch in my inbox, even though it says Cc:
me.

Paul, where do you expect that patch to go? The version I see from my next tree needs a _lot_ of work.

There is also

https://lkml.kernel.org/r/20200705082603.GX3874@shao2-debian
https://lkml.kernel.org/r/[email protected]

it might be the same thing.

Sebastian

#4 lore raw
In reply to Peter Zijlstra
Quoted 5 lines · Peter Zijlstra
Quoted 4 lines · Dan Carpenter

The __per_cpu_offset[] array has "nr_cpu_ids" elements so change the >

Quoted 1 line

= to prevent a read one element beyond the end of the array.

Fixes 0504bc41a62c ("kernel/smp: Provide CSD lock timeout diagnostics")

Good catch, will apply, thank you!

Quoted 2 lines

I don't have a copy of that patch in my inbox, even though it says Cc:
me.

I wasn't going to bother you with it until a bit after v5.9-rc1. But it appears that this don't-bother-you strategy failed.

Quoted 2 lines

Paul, where do you expect that patch to go? The version I see from my next tree needs a _lot_ of work.

I expect it to go nowhere until at least the v5.10 merge window.

Thanx, Paul

Quoted 21 lines
Quoted 21 lines
---
 kernel/smp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/smp.c +1 -1
index 78b602cae6c2..f49966713ac3 100644
@@ -171,7 +171,7 @@ static __always_inline bool csd_lock_wait_toolong(call_single_data_t *csd, u64 t
 		*bug_id = atomic_inc_return(&csd_bug_count);
 	cpu = csd_lock_wait_getcpu(csd);
 	smp_mb(); // No stale cur_csd values!
-	if (WARN_ONCE(cpu < 0 || cpu > nr_cpu_ids, "%s: cpu = %d\n", __func__, cpu))
+	if (WARN_ONCE(cpu < 0 || cpu >= nr_cpu_ids, "%s: cpu = %d\n", __func__, cpu))
 		cpu_cur_csd = READ_ONCE(per_cpu(cur_csd, 0));
 	else
 		cpu_cur_csd = READ_ONCE(per_cpu(cur_csd, cpu));
Signature · 1 line
2.27.0
#3 lore raw
In reply to Sebastian Andrzej Siewior
Quoted 13 lines · Sebastian Andrzej Siewior
Quoted 11 lines · Peter Zijlstra
Quoted 4 lines · Dan Carpenter

The __per_cpu_offset[] array has "nr_cpu_ids" elements so change the >

Quoted 1 line

= to prevent a read one element beyond the end of the array.

Fixes 0504bc41a62c ("kernel/smp: Provide CSD lock timeout diagnostics")

I don't have a copy of that patch in my inbox, even though it says Cc:
me.

Paul, where do you expect that patch to go? The version I see from my next tree needs a _lot_ of work.

Quoted 6 lines

There is also

https://lkml.kernel.org/r/20200705082603.GX3874@shao2-debian
https://lkml.kernel.org/r/[email protected]

it might be the same thing.

Same commit, different bug, but the fix should be in -next by now. For these two reports, the problem was that I had debug-recording code on the wrong side of a csd_unlock().

Thanx, Paul

#5 lore raw
In reply to Dan Carpenter
Quoted 5 lines · Dan Carpenter

The __per_cpu_offset[] array has "nr_cpu_ids" elements so change the >

Quoted 1 line

= to prevent a read one element beyond the end of the array.

Fixes 0504bc41a62c ("kernel/smp: Provide CSD lock timeout diagnostics")

Good eyes, thank you! Folding this into the original with attribution.

Thanx, Paul

Quoted 20 lines
---
 kernel/smp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/smp.c +1 -1
index 78b602cae6c2..f49966713ac3 100644
@@ -171,7 +171,7 @@ static __always_inline bool csd_lock_wait_toolong(call_single_data_t *csd, u64 t
 		*bug_id = atomic_inc_return(&csd_bug_count);
 	cpu = csd_lock_wait_getcpu(csd);
 	smp_mb(); // No stale cur_csd values!
-	if (WARN_ONCE(cpu < 0 || cpu > nr_cpu_ids, "%s: cpu = %d\n", __func__, cpu))
+	if (WARN_ONCE(cpu < 0 || cpu >= nr_cpu_ids, "%s: cpu = %d\n", __func__, cpu))
 		cpu_cur_csd = READ_ONCE(per_cpu(cur_csd, 0));
 	else
 		cpu_cur_csd = READ_ONCE(per_cpu(cur_csd, cpu));
Signature · 1 line
2.27.0