2020#include <linux/sched.h>
2121#include <linux/uaccess.h>
2222
23- #include "atomic.h"
2423#include "encoding.h"
2524#include "kcsan.h"
25+ #include "permissive.h"
2626
2727static bool kcsan_early_enable = IS_ENABLED (CONFIG_KCSAN_EARLY_ENABLE );
2828unsigned int kcsan_udelay_task = CONFIG_KCSAN_UDELAY_TASK ;
@@ -301,9 +301,9 @@ static inline void reset_kcsan_skip(void)
301301 this_cpu_write (kcsan_skip , skip_count );
302302}
303303
304- static __always_inline bool kcsan_is_enabled (void )
304+ static __always_inline bool kcsan_is_enabled (struct kcsan_ctx * ctx )
305305{
306- return READ_ONCE (kcsan_enabled ) && get_ctx () -> disable_count == 0 ;
306+ return READ_ONCE (kcsan_enabled ) && ! ctx -> disable_count ;
307307}
308308
309309/* Introduce delay depending on context and configuration. */
@@ -353,25 +353,41 @@ static noinline void kcsan_found_watchpoint(const volatile void *ptr,
353353 atomic_long_t * watchpoint ,
354354 long encoded_watchpoint )
355355{
356+ const bool is_assert = (type & KCSAN_ACCESS_ASSERT ) != 0 ;
357+ struct kcsan_ctx * ctx = get_ctx ();
356358 unsigned long flags ;
357359 bool consumed ;
358360
359- if (!kcsan_is_enabled ())
361+ /*
362+ * We know a watchpoint exists. Let's try to keep the race-window
363+ * between here and finally consuming the watchpoint below as small as
364+ * possible -- avoid unneccessarily complex code until consumed.
365+ */
366+
367+ if (!kcsan_is_enabled (ctx ))
360368 return ;
361369
362370 /*
363371 * The access_mask check relies on value-change comparison. To avoid
364372 * reporting a race where e.g. the writer set up the watchpoint, but the
365373 * reader has access_mask!=0, we have to ignore the found watchpoint.
366374 */
367- if (get_ctx () -> access_mask != 0 )
375+ if (ctx -> access_mask )
368376 return ;
369377
370378 /*
371- * Consume the watchpoint as soon as possible, to minimize the chances
372- * of !consumed. Consuming the watchpoint must always be guarded by
373- * kcsan_is_enabled() check, as otherwise we might erroneously
374- * triggering reports when disabled.
379+ * If the other thread does not want to ignore the access, and there was
380+ * a value change as a result of this thread's operation, we will still
381+ * generate a report of unknown origin.
382+ *
383+ * Use CONFIG_KCSAN_REPORT_RACE_UNKNOWN_ORIGIN=n to filter.
384+ */
385+ if (!is_assert && kcsan_ignore_address (ptr ))
386+ return ;
387+
388+ /*
389+ * Consuming the watchpoint must be guarded by kcsan_is_enabled() to
390+ * avoid erroneously triggering reports if the context is disabled.
375391 */
376392 consumed = try_consume_watchpoint (watchpoint , encoded_watchpoint );
377393
@@ -391,7 +407,7 @@ static noinline void kcsan_found_watchpoint(const volatile void *ptr,
391407 atomic_long_inc (& kcsan_counters [KCSAN_COUNTER_REPORT_RACES ]);
392408 }
393409
394- if (( type & KCSAN_ACCESS_ASSERT ) != 0 )
410+ if (is_assert )
395411 atomic_long_inc (& kcsan_counters [KCSAN_COUNTER_ASSERT_FAILURES ]);
396412 else
397413 atomic_long_inc (& kcsan_counters [KCSAN_COUNTER_DATA_RACES ]);
@@ -409,6 +425,7 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type)
409425 unsigned long access_mask ;
410426 enum kcsan_value_change value_change = KCSAN_VALUE_CHANGE_MAYBE ;
411427 unsigned long ua_flags = user_access_save ();
428+ struct kcsan_ctx * ctx = get_ctx ();
412429 unsigned long irq_flags = 0 ;
413430
414431 /*
@@ -417,16 +434,14 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type)
417434 */
418435 reset_kcsan_skip ();
419436
420- if (!kcsan_is_enabled ())
437+ if (!kcsan_is_enabled (ctx ))
421438 goto out ;
422439
423440 /*
424- * Special atomic rules: unlikely to be true, so we check them here in
425- * the slow-path, and not in the fast-path in is_atomic(). Call after
426- * kcsan_is_enabled(), as we may access memory that is not yet
427- * initialized during early boot.
441+ * Check to-ignore addresses after kcsan_is_enabled(), as we may access
442+ * memory that is not yet initialized during early boot.
428443 */
429- if (!is_assert && kcsan_is_atomic_special (ptr ))
444+ if (!is_assert && kcsan_ignore_address (ptr ))
430445 goto out ;
431446
432447 if (!check_encodable ((unsigned long )ptr , size )) {
@@ -479,15 +494,6 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type)
479494 break ; /* ignore; we do not diff the values */
480495 }
481496
482- if (IS_ENABLED (CONFIG_KCSAN_DEBUG )) {
483- kcsan_disable_current ();
484- pr_err ("watching %s, size: %zu, addr: %px [slot: %d, encoded: %lx]\n" ,
485- is_write ? "write" : "read" , size , ptr ,
486- watchpoint_slot ((unsigned long )ptr ),
487- encode_watchpoint ((unsigned long )ptr , size , is_write ));
488- kcsan_enable_current ();
489- }
490-
491497 /*
492498 * Delay this thread, to increase probability of observing a racy
493499 * conflicting access.
@@ -498,7 +504,7 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type)
498504 * Re-read value, and check if it is as expected; if not, we infer a
499505 * racy access.
500506 */
501- access_mask = get_ctx () -> access_mask ;
507+ access_mask = ctx -> access_mask ;
502508 new = 0 ;
503509 switch (size ) {
504510 case 1 :
@@ -521,8 +527,14 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type)
521527 if (access_mask )
522528 diff &= access_mask ;
523529
524- /* Were we able to observe a value-change? */
525- if (diff != 0 )
530+ /*
531+ * Check if we observed a value change.
532+ *
533+ * Also check if the data race should be ignored (the rules depend on
534+ * non-zero diff); if it is to be ignored, the below rules for
535+ * KCSAN_VALUE_CHANGE_MAYBE apply.
536+ */
537+ if (diff && !kcsan_ignore_data_race (size , type , old , new , diff ))
526538 value_change = KCSAN_VALUE_CHANGE_TRUE ;
527539
528540 /* Check if this access raced with another. */
@@ -644,6 +656,15 @@ void __init kcsan_init(void)
644656 pr_info ("enabled early\n" );
645657 WRITE_ONCE (kcsan_enabled , true);
646658 }
659+
660+ if (IS_ENABLED (CONFIG_KCSAN_REPORT_VALUE_CHANGE_ONLY ) ||
661+ IS_ENABLED (CONFIG_KCSAN_ASSUME_PLAIN_WRITES_ATOMIC ) ||
662+ IS_ENABLED (CONFIG_KCSAN_PERMISSIVE ) ||
663+ IS_ENABLED (CONFIG_KCSAN_IGNORE_ATOMICS )) {
664+ pr_warn ("non-strict mode configured - use CONFIG_KCSAN_STRICT=y to see all data races\n" );
665+ } else {
666+ pr_info ("strict mode configured\n" );
667+ }
647668}
648669
649670/* === Exported interface =================================================== */
0 commit comments