Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

x86: call instrumentation hooks from copy_mc.c

Memory accesses in copy_mc_to_kernel() and copy_mc_to_user() are performed
by assembly routines and are invisible to KASAN, KCSAN, and KMSAN. Add
hooks from instrumentation.h to tell the tools these functions have
memcpy/copy_from_user semantics.

The call to copy_mc_fragile() in copy_mc_fragile_handle_tail() is left
intact, because the latter is only called from the assembly implementation
of copy_mc_fragile(), so the memory accesses in it are covered by the
instrumentation in copy_mc_to_kernel() and copy_mc_to_user().

Link: https://lore.kernel.org/all/3b7dbd88-0861-4638-b2d2-911c97a4cadf@I-love.SAKURA.ne.jp/
Link: https://lkml.kernel.org/r/20240320101851.2589698-3-glider@google.com
Signed-off-by: Alexander Potapenko <glider@google.com>
Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Marco Elver <elver@google.com>
Cc: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Alexander Potapenko and committed by
Andrew Morton
61b258b0 922621a6

+17 -4
+17 -4
arch/x86/lib/copy_mc.c
··· 4 4 #include <linux/jump_label.h> 5 5 #include <linux/uaccess.h> 6 6 #include <linux/export.h> 7 + #include <linux/instrumented.h> 7 8 #include <linux/string.h> 8 9 #include <linux/types.h> 9 10 ··· 62 61 */ 63 62 unsigned long __must_check copy_mc_to_kernel(void *dst, const void *src, unsigned len) 64 63 { 65 - if (copy_mc_fragile_enabled) 66 - return copy_mc_fragile(dst, src, len); 67 - if (static_cpu_has(X86_FEATURE_ERMS)) 68 - return copy_mc_enhanced_fast_string(dst, src, len); 64 + unsigned long ret; 65 + 66 + if (copy_mc_fragile_enabled) { 67 + instrument_memcpy_before(dst, src, len); 68 + ret = copy_mc_fragile(dst, src, len); 69 + instrument_memcpy_after(dst, src, len, ret); 70 + return ret; 71 + } 72 + if (static_cpu_has(X86_FEATURE_ERMS)) { 73 + instrument_memcpy_before(dst, src, len); 74 + ret = copy_mc_enhanced_fast_string(dst, src, len); 75 + instrument_memcpy_after(dst, src, len, ret); 76 + return ret; 77 + } 69 78 memcpy(dst, src, len); 70 79 return 0; 71 80 } ··· 86 75 unsigned long ret; 87 76 88 77 if (copy_mc_fragile_enabled) { 78 + instrument_copy_to_user(dst, src, len); 89 79 __uaccess_begin(); 90 80 ret = copy_mc_fragile((__force void *)dst, src, len); 91 81 __uaccess_end(); ··· 94 82 } 95 83 96 84 if (static_cpu_has(X86_FEATURE_ERMS)) { 85 + instrument_copy_to_user(dst, src, len); 97 86 __uaccess_begin(); 98 87 ret = copy_mc_enhanced_fast_string((__force void *)dst, src, len); 99 88 __uaccess_end();