linux/mm/kasan
Hui Su bd1ad3cf07 kasan: fix quarantine_size accounting during cache removal
quarantine_size tracks the total number of bytes stored in
global_quarantine[].  It is incremented when per-CPU quarantine objects
are moved into the global quarantine and decremented when a global batch
is evicted by kasan_quarantine_reduce().

kasan_quarantine_remove_cache() also removes objects from the global
quarantine.  qlist_move_cache() rebuilds the source batch and updates its
.bytes field, but quarantine_size is not adjusted accordingly.

As a result, quarantine_size remains over-counted by the size of the
removed objects.  The stale accounting accumulates across cache removals. 
Once the inflated value exceeds quarantine_max_size,
kasan_quarantine_reduce() can evict a batch even though the actual number
of bytes in global_quarantine[] is still below quarantine_max_size,
shortening the quarantine window.

Fix the accounting by recording each batch's size before
qlist_move_cache() and subtracting the number of bytes actually removed
from quarantine_size while holding quarantine_lock.

A KUnit reproducer used during testing observed the over-count grow by
4698864 bytes after one kasan_quarantine_remove_cache() call with the fix
reverted.  With this change applied, the over-count did not grow.

Link: https://lore.kernel.org/20260811073332.1351893-1-sh_def@163.com
Fixes: 64abdcb243 ("kasan: eliminate long stalls during quarantine reduction")
Signed-off-by: Hui Su <sh_def@163.com>
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/patchset/20260808031459.3032812-1-sh_def%40163.com
Reviewed-by: Andrey Ryabinin <ryabinin.a.a@gmail.com>
Cc: Alexander Potapenko <glider@google.com>
Cc: Andrey Konovalov <andreyknvl@gmail.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Vincenzo Frascino <vincenzo.frascino@arm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
2026-08-24 18:43:21 -07:00
..
common.c mm/kasan: fix KASAN poisoning in vrealloc() 2026-01-26 19:03:45 -08:00
generic.c kasan: cleanup of kasan_enabled() checks 2025-11-16 17:28:01 -08:00
hw_tags.c mm/kasan: remove redundant initialization for kasan_flag_write_only 2026-07-28 21:11:44 -07:00
init.c kasan: Move generic KASAN page tables out of BSS too 2026-06-05 11:44:10 +01:00
kasan_test_c.c mm.git review status for mm-hotfixes-stable..mm-stable 2026-06-19 10:14:34 -07:00
kasan_test_rust.rs rust: treewide: switch to the kernel Vec type 2024-10-15 23:10:32 +02:00
kasan.h kasan: remove __kasan_save_free_info wrapper 2025-11-16 17:28:01 -08:00
Makefile kbuild: Switch from -Wvla to -Wvla-larger-than=1 2025-05-08 09:42:06 -07:00
quarantine.c kasan: fix quarantine_size accounting during cache removal 2026-08-24 18:43:21 -07:00
report_generic.c kasan: stop leaking stack trace handles 2024-01-05 10:17:45 -08:00
report_hw_tags.c kasan: use internal prototypes matching gcc-13 builtins 2023-06-09 16:25:19 -07:00
report_sw_tags.c kasan: use internal prototypes matching gcc-13 builtins 2023-06-09 16:25:19 -07:00
report_tags.c kasan: simplify kasan_complete_mode_report_info for tag-based modes 2023-12-29 11:58:47 -08:00
report.c kasan: fix bug type classification for SW_TAGS mode 2026-04-05 13:53:18 -07:00
shadow.c mm: introduce generic lazy_mmu helpers 2026-01-20 19:24:33 -08:00
sw_tags.c kasan: introduce ARCH_DEFER_KASAN and unify static key across modes 2025-09-21 14:21:58 -07:00
tags.c kasan: remove __kasan_save_free_info wrapper 2025-11-16 17:28:01 -08:00