1 From 31fa985b4196f8a66f027672e9bf2b81fea0417c Mon Sep 17 00:00:00 2001
2 From: Zqiang <qiang1.zhang@intel.com>
3 Date: Wed, 27 Apr 2022 12:41:56 -0700
4 Subject: kasan: prevent cpu_quarantine corruption when CPU offline and cache shrink occur at same time
6 From: Zqiang <qiang1.zhang@intel.com>
8 commit 31fa985b4196f8a66f027672e9bf2b81fea0417c upstream.
10 kasan_quarantine_remove_cache() is called in kmem_cache_shrink()/
11 destroy(). The kasan_quarantine_remove_cache() call is protected by
12 cpuslock in kmem_cache_destroy() to ensure serialization with
15 However the kasan_quarantine_remove_cache() call is not protected by
16 cpuslock in kmem_cache_shrink(). When a CPU is going offline and cache
17 shrink occurs at same time, the cpu_quarantine may be corrupted by
18 interrupt (per_cpu_remove_cache operation).
20 So add a cpu_quarantine offline flags check in per_cpu_remove_cache().
22 [akpm@linux-foundation.org: add comment, per Zqiang]
24 Link: https://lkml.kernel.org/r/20220414025925.2423818-1-qiang1.zhang@intel.com
25 Signed-off-by: Zqiang <qiang1.zhang@intel.com>
26 Reviewed-by: Dmitry Vyukov <dvyukov@google.com>
27 Cc: Andrey Ryabinin <ryabinin.a.a@gmail.com>
28 Cc: Alexander Potapenko <glider@google.com>
29 Cc: Andrey Konovalov <andreyknvl@gmail.com>
30 Cc: <stable@vger.kernel.org>
31 Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
32 Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
33 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
35 mm/kasan/quarantine.c | 7 +++++++
36 1 file changed, 7 insertions(+)
38 --- a/mm/kasan/quarantine.c
39 +++ b/mm/kasan/quarantine.c
40 @@ -299,6 +299,13 @@ static void per_cpu_remove_cache(void *a
43 q = this_cpu_ptr(&cpu_quarantine);
45 + * Ensure the ordering between the writing to q->offline and
46 + * per_cpu_remove_cache. Prevent cpu_quarantine from being corrupted
49 + if (READ_ONCE(q->offline))
51 qlist_move_cache(q, &to_free, cache);
52 qlist_free_all(&to_free, cache);