Commit 9bb713a
crypto: rng - Fix double percpu offset in local_unlock() calls
local_unlock() is supposed to take a percpu pointer, which it then offsets
for the current CPU. The problem is that the per-CPU DRBG code passes
local_unlock() a pointer that has *already been offset for the current
CPU*, so the percpu offset gets added twice. `pcri` is the percpu pointer
and `cri` is the offset `pcri` pointer for the current CPU; passing `cri`
to local_unlock() results in the current CPU's offset getting added again
and thus the resulting pointer is garbage.
This went unnoticed at runtime because local_unlock() only dereferences
that pointer on PREEMPT_RT or CONFIG_DEBUG_LOCK_ALLOC=y kernels. The
pointer is never actually used otherwise.
This also went unnoticed at compile time because, for x86_64, __percpu only
expands to something on GCC 14+; __percpu is otherwise an empty macro. So
there's no pointer type mismatch detected at compile time on older GCC
versions. On GCC 14+, the following compile error occurs:
../crypto/rng.c: In function 'lock_default_rng':
../include/linux/percpu-defs.h:221:45: error: initialization from pointer to non-enclosed address space
Fix it by using __local_unlock() instead, which eliminates the duplicate
per-CPU offset addition. While using local_unlock() with `pcri` also works,
using `cri` with __local_unlock() is leaner because it doesn't need to redo
the offset addition, saving an instruction on kernels that actually use the
pointer.
Signed-off-by: Sultan Alsawaf <sultan@ciq.com>1 parent d4027d1 commit 9bb713a
1 file changed
Lines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
338 | 338 | | |
339 | 339 | | |
340 | 340 | | |
341 | | - | |
| 341 | + | |
342 | 342 | | |
343 | 343 | | |
344 | 344 | | |
| |||
411 | 411 | | |
412 | 412 | | |
413 | 413 | | |
414 | | - | |
| 414 | + | |
415 | 415 | | |
416 | 416 | | |
417 | 417 | | |
| |||
0 commit comments