xas_find_chunk() reads xa_node->marks with a plain dereference while concurrent xa_set_mark()/xa_clear_mark() writers use non-atomic __test_and_set_bit()/__test_and_clear_bit() under xa_lock. RCU readers in xa_find() -> xas_find_marked() -> xas_find_chunk() call this without xa_lock, so the plain read races with the locked non-atomic write. KCSAN flags this as a data race, and Marco Elver notes the failure scenario is worse than benign: the compiler can hoist *addr into a register on the reader. When a concurrent writer later clears a mark bit, the cached stale value causes xas_find_chunk() to keep returning the cleared offset. xas_find_marked() then loops forever looking for an entry at that offset under rcu_read_lock(). Snapshot each marks word with READ_ONCE() into a local variable, then do all bit operations on that snapshot. This silences KCSAN, forces a fresh load on each call, prevents the compiler from caching a stale value across iterations, and covers all XA_MARK_LONGS words so the 32-bit path (XA_CHUNK_SIZE=64, BITS_PER_LONG=32) no longer reads past a single word. Fixes: b803b42823d0 ("xarray: Add XArray iterators") Reported-by: syzbot+cc64b6bdf649b54047e0@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=cc64b6bdf649b54047e0 Reported-by: kernel test robot Closes: https://lore.kernel.org/oe-lkp/202612020910.b06ad2ef-lkp@intel.com Signed-off-by: Shuai Liu <847807867@qq.com> --- Changes in v2: - Snapshot all XA_MARK_LONGS words with READ_ONCE(); v1 only read the first word, which missed marks in slots 32..63 and read out of bounds past a single stack variable on 32-bit architectures (Sashiko, kernel test robot) - Add Fixes: tag (Sashiko) - Add Closes: links for the syzbot and kernel test robot reports include/linux/xarray.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/include/linux/xarray.h b/include/linux/xarray.h index be850174e802e..09e2ca9e35e1a 100644 --- a/include/linux/xarray.h +++ b/include/linux/xarray.h @@ -1743,20 +1743,24 @@ static inline unsigned int xas_find_chunk(struct xa_state *xas, bool advance, xa_mark_t mark) { unsigned long *addr = xas->xa_node->marks[(__force unsigned)mark]; + unsigned long marks[XA_MARK_LONGS]; unsigned int offset = xas->xa_offset; + int i; if (advance) offset++; + for (i = 0; i < XA_MARK_LONGS; i++) + marks[i] = READ_ONCE(addr[i]); if (XA_CHUNK_SIZE == BITS_PER_LONG) { if (offset < XA_CHUNK_SIZE) { - unsigned long data = *addr & (~0UL << offset); + unsigned long data = marks[0] & (~0UL << offset); if (data) return __ffs(data); } return XA_CHUNK_SIZE; } - return find_next_bit(addr, XA_CHUNK_SIZE, offset); + return find_next_bit(marks, XA_CHUNK_SIZE, offset); } /** -- 2.43.0