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

time/sched_clock: Broaden sched_clock()'s instrumentation coverage

Most of sched_clock()'s implementation is ineligible for instrumentation
due to relying on sched_clock_noinstr().

Split the implementation off into an __always_inline function
__sched_clock(), which is then used by the noinstr and instrumentable
version, to allow more of sched_clock() to be covered by various
instrumentation.

This will allow instrumentation with the various sanitizers (KASAN,
KCSAN, KMSAN, UBSAN). For KCSAN, we know that raw seqcount_latch usage
without annotations will result in false positive reports: tell it that
all of __sched_clock() is "atomic" for the latch reader; later changes
in this series will take care of the writers.

Co-developed-by: "Peter Zijlstra (Intel)" <peterz@infradead.org>
Signed-off-by: "Peter Zijlstra (Intel)" <peterz@infradead.org>
Signed-off-by: Marco Elver <elver@google.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://lore.kernel.org/r/20241104161910.780003-3-elver@google.com

authored by

Marco Elver and committed by
Peter Zijlstra
8ab40fc2 1139c71d

+14 -2
+14 -2
kernel/time/sched_clock.c
··· 80 80 return raw_read_seqcount_latch_retry(&cd.seq, seq); 81 81 } 82 82 83 - unsigned long long noinstr sched_clock_noinstr(void) 83 + static __always_inline unsigned long long __sched_clock(void) 84 84 { 85 85 struct clock_read_data *rd; 86 86 unsigned int seq; ··· 98 98 return res; 99 99 } 100 100 101 + unsigned long long noinstr sched_clock_noinstr(void) 102 + { 103 + return __sched_clock(); 104 + } 105 + 101 106 unsigned long long notrace sched_clock(void) 102 107 { 103 108 unsigned long long ns; 104 109 preempt_disable_notrace(); 105 - ns = sched_clock_noinstr(); 110 + /* 111 + * All of __sched_clock() is a seqcount_latch reader critical section, 112 + * but relies on the raw helpers which are uninstrumented. For KCSAN, 113 + * mark all accesses in __sched_clock() as atomic. 114 + */ 115 + kcsan_nestable_atomic_begin(); 116 + ns = __sched_clock(); 117 + kcsan_nestable_atomic_end(); 106 118 preempt_enable_notrace(); 107 119 return ns; 108 120 }