On Wed, Mar 20, 2024 at 4:54 AM Tetsuo Handa <penguin-kernel@xxxxxxxxxxxxxxxxxxx> wrote: > > On 2024/03/20 1:36, Alexander Potapenko wrote: > > @@ -61,10 +62,20 @@ unsigned long copy_mc_enhanced_fast_string(void *dst, const void *src, unsigned > > */ > > unsigned long __must_check copy_mc_to_kernel(void *dst, const void *src, unsigned len) > > { > > - if (copy_mc_fragile_enabled) > > - return copy_mc_fragile(dst, src, len); > > - if (static_cpu_has(X86_FEATURE_ERMS)) > > - return copy_mc_enhanced_fast_string(dst, src, len); > > + unsigned long ret; > > + > > + if (copy_mc_fragile_enabled) { > > + instrument_memcpy_before(dst, src, len); > > I feel that instrument_memcpy_before() needs to be called *after* > copy_mc_fragile() etc. , for we can't predict how many bytes will > copy_mc_fragile() etc. actually copy. That's why we have both _before() and _after(). We can discuss what checks need to be done before and after the memcpy call, but calling instrument_memcpy_before() after copy_mc_fragile() is counterintuitive. For KMSAN it is indeed important to only handle `len-ret` bytes that were actually copied. We want the instrumentation to update the metadata without triggering an immediate error report, so the update better be consistent with what the kernel actually did with the memory. But for KASAN/KCSAN we can afford more aggressive checks. First, if we postpone them after the actual memory accesses happen, the kernel may panic on the invalid access without a decent error report. Second, even if in a particular case only `len-ret` bytes were copied, the caller probably expected both `src` and `dst` to have `len` addressable bytes. Checking for the whole length in this case is more likely to detect a real error than produce a false positive.