Skip to content

Commit 106a307

Browse files
melverpaulmckrcu
authored andcommitted
kcsan: Skew delay to be longer for certain access types
For compound instrumentation and assert accesses, skew the watchpoint delay to be longer if randomized. This is useful to improve race detection for such accesses. For compound accesses we should increase the delay as we've aggregated both read and write instrumentation. By giving up 1 call into the runtime, we're less likely to set up a watchpoint and thus less likely to detect a race. We can balance this by increasing the watchpoint delay. For assert accesses, we know these are of increased interest, and we wish to increase our chances of detecting races for such checks. Note that, kcsan_udelay_{task,interrupt} define the upper bound delays. When randomized, delays are uniformly distributed between [0, delay]. Skewing the delay does not break this promise as long as the defined upper bounds are still adhered to. The current skew results in delays uniformly distributed between [delay/2, delay]. Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Marco Elver <elver@google.com> Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
1 parent a81b375 commit 106a307

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

kernel/kcsan/core.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,15 @@ static __always_inline bool kcsan_is_enabled(void)
283283
return READ_ONCE(kcsan_enabled) && get_ctx()->disable_count == 0;
284284
}
285285

286-
static inline unsigned int get_delay(void)
286+
static inline unsigned int get_delay(int type)
287287
{
288288
unsigned int delay = in_task() ? kcsan_udelay_task : kcsan_udelay_interrupt;
289+
/* For certain access types, skew the random delay to be longer. */
290+
unsigned int skew_delay_order =
291+
(type & (KCSAN_ACCESS_COMPOUND | KCSAN_ACCESS_ASSERT)) ? 1 : 0;
292+
289293
return delay - (IS_ENABLED(CONFIG_KCSAN_DELAY_RANDOMIZE) ?
290-
prandom_u32_max(delay) :
294+
prandom_u32_max(delay >> skew_delay_order) :
291295
0);
292296
}
293297

@@ -470,7 +474,7 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type)
470474
* Delay this thread, to increase probability of observing a racy
471475
* conflicting access.
472476
*/
473-
udelay(get_delay());
477+
udelay(get_delay(type));
474478

475479
/*
476480
* Re-read value, and check if it is as expected; if not, we infer a

0 commit comments

Comments
 (0)