]> www.infradead.org Git - users/jedix/linux-maple.git/commit
mm/rodata_test: use READ_ONCE() to read const variable
authorPetr Tesarik <ptesarik@suse.com>
Tue, 19 Nov 2024 11:37:38 +0000 (12:37 +0100)
committerAndrew Morton <akpm@linux-foundation.org>
Tue, 14 Jan 2025 06:40:37 +0000 (22:40 -0800)
commitdb27ad8b0209e892a9b6294aadcf9d756f41a656
tree6fab075f46510d9466a71dff57e15752a1f3eb67
parent255ff62d1586e63b6a461f0d89088d60d54d9814
mm/rodata_test: use READ_ONCE() to read const variable

Patch series "Fix mm/rodata_test", v2.

Make sure that the test actually reads the read-only memory location.
Verify that the variable contains the expected value rather than any
non-zero value.

This patch (of 2):

The C compiler may optimize away the memory read of a const variable if
its value is known at compile time.

In particular, GCC14 with -O2 generates no code at all for test 1, and it
generates the following x86_64 instructions for test 3:

cmpl $195, 4(%rsp)
je .L14

That is, it replaces the read of rodata_test_data with an immediate value
and compares it to the value of the local variable "zero".

Use READ_ONCE() to undo any such compiler optimizations and enforce a
memory read.

Link: https://lkml.kernel.org/r/cover.1732016064.git.ptesarik@suse.com
Link: https://lkml.kernel.org/r/2a66dee010151b25cb143efb39091ef7530aa00a.1732016064.git.ptesarik@suse.com
Fixes: 2959a5f726f6 ("mm: add arch-independent testcases for RODATA")
Signed-off-by: Petr Tesarik <ptesarik@suse.com>
Reviewed-by: Kees Cook <kees@kernel.org>
Cc: Jinbum Park <jinb.park7@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
mm/rodata_test.c