| @@ -20,9 +20,9 @@ · 19 unmodified lines |
| 20 | 20 | #include <linux/sched.h> |
| 21 | 21 | #include <linux/uaccess.h> |
| 22 | 22 | |
| 23 | | −#include "atomic.h" |
| 24 | 23 | #include "encoding.h" |
| 25 | 24 | #include "kcsan.h" |
| 25 | +#include "permissive.h" |
| 26 | 26 | |
| 27 | 27 | static bool kcsan_early_enable = IS_ENABLED(CONFIG_KCSAN_EARLY_ENABLE); |
| 28 | 28 | unsigned int kcsan_udelay_task = CONFIG_KCSAN_UDELAY_TASK; |
| @@ -301,9 +301,9 @@ static inline void reset_kcsan_skip(void) · 272 unmodified lines |
| 301 | 301 | this_cpu_write(kcsan_skip, skip_count); |
| 302 | 302 | } |
| 303 | 303 | |
| 304 | | −static __always_inline bool kcsan_is_enabled(void) |
| 304 | +static __always_inline bool kcsan_is_enabled(struct kcsan_ctx *ctx) |
| 305 | 305 | { |
| 306 | | − return READ_ONCE(kcsan_enabled) && get_ctx()->disable_count == 0; |
| 306 | + return READ_ONCE(kcsan_enabled) && !ctx->disable_count; |
| 307 | 307 | } |
| 308 | 308 | |
| 309 | 309 | /* Introduce delay depending on context and configuration. */ |
| @@ -353,10 +353,18 @@ static noinline void kcsan_found_watchpoint(const volatile void *ptr, · 43 unmodified lines |
| 353 | 353 | atomic_long_t *watchpoint, |
| 354 | 354 | long encoded_watchpoint) |
| 355 | 355 | { |
| 356 | + const bool is_assert = (type & KCSAN_ACCESS_ASSERT) != 0; |
| 357 | + struct kcsan_ctx *ctx = get_ctx(); |
| 356 | 358 | unsigned long flags; |
| 357 | 359 | bool consumed; |
| 358 | 360 | |
| 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)) |
| 360 | 368 | return; |
| 361 | 369 | |
| 362 | 370 | /* |
| @@ -364,14 +372,22 @@ static noinline void kcsan_found_watchpoint(const volatile void *ptr, · 1 unmodified line |
| 364 | 372 | * reporting a race where e.g. the writer set up the watchpoint, but the |
| 365 | 373 | * reader has access_mask!=0, we have to ignore the found watchpoint. |
| 366 | 374 | */ |
| 367 | | − if (get_ctx()->access_mask != 0) |
| 375 | + if (ctx->access_mask) |
| 368 | 376 | return; |
| 369 | 377 | |
| 370 | 378 | /* |
| 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. |
| 375 | 391 | */ |
| 376 | 392 | consumed = try_consume_watchpoint(watchpoint, encoded_watchpoint); |
| 377 | 393 | |
| @@ -391,7 +407,7 @@ static noinline void kcsan_found_watchpoint(const volatile void *ptr, · 13 unmodified lines |
| 391 | 407 | atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_REPORT_RACES]); |
| 392 | 408 | } |
| 393 | 409 | |
| 394 | | − if ((type & KCSAN_ACCESS_ASSERT) != 0) |
| 410 | + if (is_assert) |
| 395 | 411 | atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_ASSERT_FAILURES]); |
| 396 | 412 | else |
| 397 | 413 | 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) · 11 unmodified lines |
| 409 | 425 | unsigned long access_mask; |
| 410 | 426 | enum kcsan_value_change value_change = KCSAN_VALUE_CHANGE_MAYBE; |
| 411 | 427 | unsigned long ua_flags = user_access_save(); |
| 428 | + struct kcsan_ctx *ctx = get_ctx(); |
| 412 | 429 | unsigned long irq_flags = 0; |
| 413 | 430 | |
| 414 | 431 | /* |
| @@ -417,16 +434,14 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type) · 2 unmodified lines |
| 417 | 434 | */ |
| 418 | 435 | reset_kcsan_skip(); |
| 419 | 436 | |
| 420 | | − if (!kcsan_is_enabled()) |
| 437 | + if (!kcsan_is_enabled(ctx)) |
| 421 | 438 | goto out; |
| 422 | 439 | |
| 423 | 440 | /* |
| 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. |
| 428 | 443 | */ |
| 429 | | − if (!is_assert && kcsan_is_atomic_special(ptr)) |
| 444 | + if (!is_assert && kcsan_ignore_address(ptr)) |
| 430 | 445 | goto out; |
| 431 | 446 | |
| 432 | 447 | if (!check_encodable((unsigned long)ptr, size)) { |
| @@ -479,15 +494,6 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type) · 46 unmodified lines |
| 479 | 494 | break; /* ignore; we do not diff the values */ |
| 480 | 495 | } |
| 481 | 496 | |
| 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 | | − |
| 491 | 497 | /* |
| 492 | 498 | * Delay this thread, to increase probability of observing a racy |
| 493 | 499 | * conflicting access. |
| @@ -498,7 +504,7 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type) · 4 unmodified lines |
| 498 | 504 | * Re-read value, and check if it is as expected; if not, we infer a |
| 499 | 505 | * racy access. |
| 500 | 506 | */ |
| 501 | | − access_mask = get_ctx()->access_mask; |
| 507 | + access_mask = ctx->access_mask; |
| 502 | 508 | new = 0; |
| 503 | 509 | switch (size) { |
| 504 | 510 | case 1: |
| @@ -521,8 +527,14 @@ kcsan_setup_watchpoint(const volatile void *ptr, size_t size, int type) · 16 unmodified lines |
| 521 | 527 | if (access_mask) |
| 522 | 528 | diff &= access_mask; |
| 523 | 529 | |
| 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)) |
| 526 | 538 | value_change = KCSAN_VALUE_CHANGE_TRUE; |
| 527 | 539 | |
| 528 | 540 | /* Check if this access raced with another. */ |
| @@ -644,6 +656,15 @@ void __init kcsan_init(void) · 115 unmodified lines |
| 644 | 656 | pr_info("enabled early\n"); |
| 645 | 657 | WRITE_ONCE(kcsan_enabled, true); |
| 646 | 658 | } |
| 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 | + } |
| 647 | 668 | } |
| 648 | 669 | |
| 649 | 670 | /* === Exported interface =================================================== */ |