linux/mm/kmsan
Aleksei Nikiforov 7e76b75e5a mm/kmsan: fix kmsan kmalloc hook when no stack depots are allocated yet
If no stack depot is allocated yet, due to masking out __GFP_RECLAIM flags
kmsan called from kmalloc cannot allocate stack depot.  kmsan fails to
record origin and report issues.  This may result in KMSAN failing to
report issues.

Reusing flags from kmalloc without modifying them should be safe for kmsan.
For example, such chain of calls is possible:
test_uninit_kmalloc -> kmalloc -> __kmalloc_cache_noprof ->
slab_alloc_node -> slab_post_alloc_hook ->
kmsan_slab_alloc -> kmsan_internal_poison_memory.

Only when it is called in a context without flags present should
__GFP_RECLAIM flags be masked.

With this change all kmsan tests start working reliably.

Eric reported:

: Yes, KMSAN seems to be at least partially broken currently.  Besides the
: fact that the kmsan KUnit test is currently failing (which I reported at
: https://lore.kernel.org/r/20250911175145.GA1376@sol), I've confirmed that
: the poly1305 KUnit test causes a KMSAN warning with Aleksei's patch
: applied but does not cause a warning without it.  The warning did get
: reached via syzbot somehow
: (https://lore.kernel.org/r/751b3d80293a6f599bb07770afcef24f623c7da0.1761026343.git.xiaopei01@kylinos.cn/),
: so KMSAN must still work in some cases.  But it didn't work for me.

Link: https://lkml.kernel.org/r/20250930115600.709776-2-aleksei.nikiforov@linux.ibm.com
Link: https://lkml.kernel.org/r/20251022030213.GA35717@sol
Fixes: 97769a53f1 ("mm, bpf: Introduce try_alloc_pages() for opportunistic page allocation")
Signed-off-by: Aleksei Nikiforov <aleksei.nikiforov@linux.ibm.com>
Reviewed-by: Alexander Potapenko <glider@google.com>
Tested-by: Eric Biggers <ebiggers@kernel.org>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Dmitriy Vyukov <dvyukov@google.com>
Cc: Ilya Leoshkevich <iii@linux.ibm.com>
Cc: Marco Elver <elver@google.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2025-11-09 21:19:42 -08:00
..
Makefile kmsan: disable ftrace in kmsan core code 2023-02-20 12:46:16 -08:00
core.c mm/kmsan: fix kmsan kmalloc hook when no stack depots are allocated yet 2025-11-09 21:19:42 -08:00
hooks.c mm/kmsan: fix kmsan kmalloc hook when no stack depots are allocated yet 2025-11-09 21:19:42 -08:00
init.c kmsan: apply clang-format to files mm/kmsan/ 2025-05-21 09:55:15 -07:00
instrumentation.c kmsan: rework kmsan_in_runtime() handling in kmsan_report() 2025-05-21 09:55:16 -07:00
kmsan.h kmsan: drop the declaration of kmsan_save_stack() 2025-05-21 09:55:16 -07:00
kmsan_test.c kmsan: fix out-of-bounds access to shadow memory 2025-09-25 16:10:34 -07:00
report.c kmsan: rework kmsan_in_runtime() handling in kmsan_report() 2025-05-21 09:55:16 -07:00
shadow.c mm/kmsan: fix kmsan kmalloc hook when no stack depots are allocated yet 2025-11-09 21:19:42 -08:00