linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm/kmemleak: silence KCSAN splats in checksum
@ 2020-03-17 18:27 Qian Cai
  2020-03-17 18:41 ` Marco Elver
  2020-03-18  8:51 ` Catalin Marinas
  0 siblings, 2 replies; 3+ messages in thread
From: Qian Cai @ 2020-03-17 18:27 UTC (permalink / raw)
  To: akpm; +Cc: elver, catalin.marinas, linux-mm, linux-kernel, Qian Cai

Even if KCSAN is disabled for kmemleak, update_checksum() could still
call crc32() (which is outside of kmemleak.c) to dereference
object->pointer. Thus, the value of object->pointer could be accessed
concurrently as noticed by KCSAN,

 BUG: KCSAN: data-race in crc32_le_base / do_raw_spin_lock

 write to 0xffffb0ea683a7d50 of 4 bytes by task 23575 on cpu 12:
  do_raw_spin_lock+0x114/0x200
  debug_spin_lock_after at kernel/locking/spinlock_debug.c:91
  (inlined by) do_raw_spin_lock at kernel/locking/spinlock_debug.c:115
  _raw_spin_lock+0x40/0x50
  __handle_mm_fault+0xa9e/0xd00
  handle_mm_fault+0xfc/0x2f0
  do_page_fault+0x263/0x6f9
  page_fault+0x34/0x40

 read to 0xffffb0ea683a7d50 of 4 bytes by task 839 on cpu 60:
  crc32_le_base+0x67/0x350
  crc32_le_base+0x67/0x350:
  crc32_body at lib/crc32.c:106
  (inlined by) crc32_le_generic at lib/crc32.c:179
  (inlined by) crc32_le at lib/crc32.c:197
  kmemleak_scan+0x528/0xd90
  update_checksum at mm/kmemleak.c:1172
  (inlined by) kmemleak_scan at mm/kmemleak.c:1497
  kmemleak_scan_thread+0xcc/0xfa
  kthread+0x1e0/0x200
  ret_from_fork+0x27/0x50

If a shattered value was returned due to a data race, it will be
corrected in the next scan. Thus, let KCSAN ignore all reads in the
region to silence KCSAN in case the write side is non-atomic.

Suggested-by: Marco Elver <elver@google.com>
Signed-off-by: Qian Cai <cai@lca.pw>
---
 mm/kmemleak.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/mm/kmemleak.c b/mm/kmemleak.c
index e362dc3d2028..5e252d91eb14 100644
--- a/mm/kmemleak.c
+++ b/mm/kmemleak.c
@@ -1169,8 +1169,10 @@ static bool update_checksum(struct kmemleak_object *object)
 	u32 old_csum = object->checksum;
 
 	kasan_disable_current();
+	kcsan_disable_current();
 	object->checksum = crc32(0, (void *)object->pointer, object->size);
 	kasan_enable_current();
+	kcsan_enable_current();
 
 	return object->checksum != old_csum;
 }
-- 
2.21.0 (Apple Git-122.2)



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] mm/kmemleak: silence KCSAN splats in checksum
  2020-03-17 18:27 [PATCH] mm/kmemleak: silence KCSAN splats in checksum Qian Cai
@ 2020-03-17 18:41 ` Marco Elver
  2020-03-18  8:51 ` Catalin Marinas
  1 sibling, 0 replies; 3+ messages in thread
From: Marco Elver @ 2020-03-17 18:41 UTC (permalink / raw)
  To: Qian Cai
  Cc: Andrew Morton, catalin.marinas, Linux Memory Management List, LKML

On Tue, 17 Mar 2020 at 19:28, Qian Cai <cai@lca.pw> wrote:
>
> Even if KCSAN is disabled for kmemleak, update_checksum() could still
> call crc32() (which is outside of kmemleak.c) to dereference
> object->pointer. Thus, the value of object->pointer could be accessed
> concurrently as noticed by KCSAN,
>
>  BUG: KCSAN: data-race in crc32_le_base / do_raw_spin_lock
>
>  write to 0xffffb0ea683a7d50 of 4 bytes by task 23575 on cpu 12:
>   do_raw_spin_lock+0x114/0x200
>   debug_spin_lock_after at kernel/locking/spinlock_debug.c:91
>   (inlined by) do_raw_spin_lock at kernel/locking/spinlock_debug.c:115
>   _raw_spin_lock+0x40/0x50
>   __handle_mm_fault+0xa9e/0xd00
>   handle_mm_fault+0xfc/0x2f0
>   do_page_fault+0x263/0x6f9
>   page_fault+0x34/0x40
>
>  read to 0xffffb0ea683a7d50 of 4 bytes by task 839 on cpu 60:
>   crc32_le_base+0x67/0x350
>   crc32_le_base+0x67/0x350:
>   crc32_body at lib/crc32.c:106
>   (inlined by) crc32_le_generic at lib/crc32.c:179
>   (inlined by) crc32_le at lib/crc32.c:197
>   kmemleak_scan+0x528/0xd90
>   update_checksum at mm/kmemleak.c:1172
>   (inlined by) kmemleak_scan at mm/kmemleak.c:1497
>   kmemleak_scan_thread+0xcc/0xfa
>   kthread+0x1e0/0x200
>   ret_from_fork+0x27/0x50
>
> If a shattered value was returned due to a data race, it will be
> corrected in the next scan. Thus, let KCSAN ignore all reads in the
> region to silence KCSAN in case the write side is non-atomic.
>
> Suggested-by: Marco Elver <elver@google.com>
> Signed-off-by: Qian Cai <cai@lca.pw>

Acked-by: Marco Elver <elver@google.com>

Thanks,
-- Marco

> ---
>  mm/kmemleak.c | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/mm/kmemleak.c b/mm/kmemleak.c
> index e362dc3d2028..5e252d91eb14 100644
> --- a/mm/kmemleak.c
> +++ b/mm/kmemleak.c
> @@ -1169,8 +1169,10 @@ static bool update_checksum(struct kmemleak_object *object)
>         u32 old_csum = object->checksum;
>
>         kasan_disable_current();
> +       kcsan_disable_current();
>         object->checksum = crc32(0, (void *)object->pointer, object->size);
>         kasan_enable_current();
> +       kcsan_enable_current();
>
>         return object->checksum != old_csum;
>  }
> --
> 2.21.0 (Apple Git-122.2)
>


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] mm/kmemleak: silence KCSAN splats in checksum
  2020-03-17 18:27 [PATCH] mm/kmemleak: silence KCSAN splats in checksum Qian Cai
  2020-03-17 18:41 ` Marco Elver
@ 2020-03-18  8:51 ` Catalin Marinas
  1 sibling, 0 replies; 3+ messages in thread
From: Catalin Marinas @ 2020-03-18  8:51 UTC (permalink / raw)
  To: Qian Cai; +Cc: akpm, elver, linux-mm, linux-kernel

On Tue, Mar 17, 2020 at 02:27:54PM -0400, Qian Cai wrote:
> Even if KCSAN is disabled for kmemleak, update_checksum() could still
> call crc32() (which is outside of kmemleak.c) to dereference
> object->pointer. Thus, the value of object->pointer could be accessed
> concurrently as noticed by KCSAN,
> 
>  BUG: KCSAN: data-race in crc32_le_base / do_raw_spin_lock
> 
>  write to 0xffffb0ea683a7d50 of 4 bytes by task 23575 on cpu 12:
>   do_raw_spin_lock+0x114/0x200
>   debug_spin_lock_after at kernel/locking/spinlock_debug.c:91
>   (inlined by) do_raw_spin_lock at kernel/locking/spinlock_debug.c:115
>   _raw_spin_lock+0x40/0x50
>   __handle_mm_fault+0xa9e/0xd00
>   handle_mm_fault+0xfc/0x2f0
>   do_page_fault+0x263/0x6f9
>   page_fault+0x34/0x40
> 
>  read to 0xffffb0ea683a7d50 of 4 bytes by task 839 on cpu 60:
>   crc32_le_base+0x67/0x350
>   crc32_le_base+0x67/0x350:
>   crc32_body at lib/crc32.c:106
>   (inlined by) crc32_le_generic at lib/crc32.c:179
>   (inlined by) crc32_le at lib/crc32.c:197
>   kmemleak_scan+0x528/0xd90
>   update_checksum at mm/kmemleak.c:1172
>   (inlined by) kmemleak_scan at mm/kmemleak.c:1497
>   kmemleak_scan_thread+0xcc/0xfa
>   kthread+0x1e0/0x200
>   ret_from_fork+0x27/0x50
> 
> If a shattered value was returned due to a data race, it will be
> corrected in the next scan. Thus, let KCSAN ignore all reads in the
> region to silence KCSAN in case the write side is non-atomic.
> 
> Suggested-by: Marco Elver <elver@google.com>
> Signed-off-by: Qian Cai <cai@lca.pw>

Acked-by: Catalin Marinas <catalin.marinas@arm.com>


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-03-18  8:51 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-17 18:27 [PATCH] mm/kmemleak: silence KCSAN splats in checksum Qian Cai
2020-03-17 18:41 ` Marco Elver
2020-03-18  8:51 ` Catalin Marinas

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).