Root Cause Analysis
1. Involved Components
- Source File:
drivers/block/loop.c
- Data Structure:
struct loop_device (field int lo_flags)
- Functions:
- Reader:
loop_queue_rq() (line 1792)
- Writer:
loop_clr_fd() (line 1134, called via lo_ioctl() line 1467)
2. Technical Mechanism
In drivers/block/loop.c, struct loop_device tracks operational configuration flags in the 32-bit field lo_flags (such as LO_FLAGS_READ_ONLY, LO_FLAGS_AUTOCLEAR, LO_FLAGS_PARTSCAN, and LO_FLAGS_DIRECT_IO).
-
The Read Side:
When a block I/O request is dispatched to the loop device, the blk-mq driver callback loop_queue_rq() is invoked on the request submission fast path. In loop_queue_rq():
switch (req_op(rq)) {
case REQ_OP_FLUSH:
case REQ_OP_DISCARD:
case REQ_OP_WRITE_ZEROES:
cmd->use_aio = false;
break;
default:
cmd->use_aio = lo->lo_flags & LO_FLAGS_DIRECT_IO;
break;
}
loop_queue_rq() does not acquire lo->lo_mutex (nor could it sleep or take device mutexes during request queueing). It directly reads lo->lo_flags using a plain memory access without READ_ONCE() or data_race() annotations.
-
The Write Side:
Concurrently, user space issues the LOOP_CLR_FD ioctl on the loop device. This invokes lo_ioctl() -> loop_clr_fd(). Inside loop_clr_fd():
err = loop_global_lock_killable(lo, true);
if (err)
return err;
...
lo->lo_flags |= LO_FLAGS_AUTOCLEAR;
...
loop_global_unlock(lo, true);
loop_clr_fd() performs a non-atomic read-modify-write operation (|=) on lo->lo_flags under lo->lo_mutex and loop_ctl_mutex. However, because loop_queue_rq() does not hold these mutexes and the block queue is not frozen during loop_clr_fd(), the write runs concurrently with the read in loop_queue_rq().
3. Invariant Violation
Linux concurrency rules require that any shared variable accessed concurrently by multiple threads where at least one thread writes must either use proper locking, atomic operations (e.g. atomic bitops such as set_bit() / test_bit()), or marked accesses (READ_ONCE(), WRITE_ONCE(), or data_race()). Here, lo->lo_flags is an ordinary int manipulated with plain C read and bitwise OR assignment without synchronization against the lockless queue dispatch path, triggering the KCSAN data-race report.