Technical Mechanism & Root Cause
The report describes a Kernel Concurrency Sanitizer (KCSAN) data-race warning between _find_next_zero_bit() and sbitmap_find_bit() / __sbitmap_get_word().
1. Functions, Structs, and Fields Involved
2. Detailed Bug Trigger and Concurrency Flow
-
Thread 0 (task 6224):
-
Thread 1 (task 6223):
-
KCSAN Flag:
- KCSAN observes an unmarked plain read on
map->word concurrently with an atomic read-modify-write modification to the exact same 8-byte location, flagging it as a data race.
3. Invariant Verification and Benign Nature
- Design Pattern: This is a classic optimistic/speculative lookup pattern in lockless algorithms.
find_next_zero_bit() is strictly used as an un-synchronized hint search to find a candidate zero bit in map->word.
- Exclusion Guarantees: Exclusive bit reservation is enforced by
test_and_set_bit_lock(nr, word). If another CPU (e.g., Task 6223) claimed that bit in the interim, test_and_set_bit_lock() returns non-zero (true), rejecting the claim and causing the loop to advance hint = nr + 1 and retry.
- Exhaustion Handling: If no zero bits are found (
nr >= depth), __sbitmap_get_word() returns -1, prompting sbitmap_find_bit_in_word() to invoke sbitmap_deferred_clear(), which flushes deferred cleared bits under map->swap_lock.
- Eliminating the Warning: Similar to how
__sbitmap_queue_get_batch() was cleaned up in commit 6ad0d7e0f4b6 ("sbitmap: use READ_ONCE to access map->word"), this KCSAN report can be eliminated by taking a marked local snapshot via unsigned long val = READ_ONCE(*word); and passing &val to find_next_zero_bit().