Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] xarray: add READ_ONCE() in xas_find_chunk()
@ 2026-11-29  7:01 Shuai Liu
  2026-11-30  7:01 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Shuai Liu @ 2026-11-29  7:01 UTC (permalink / raw)
  To: willy, linux-fsdevel
  Cc: linux-mm, elver, syzbot+cc64b6bdf649b54047e0, Shuai Liu

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().

Load the marks word once via READ_ONCE(*addr) into a local variable,
then do all bit operations on that snapshot. This silences KCSAN,
forces a fresh load on each call, and prevents the compiler from
caching the stale value across iterations.

Reported-by: syzbot+cc64b6bdf649b54047e0@syzkaller.appspotmail.com
Signed-off-by: Shuai Liu <847807867@qq.com>
---
 include/linux/xarray.h | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/include/linux/xarray.h b/include/linux/xarray.h
index be850174e802e..8c898b846bde8 100644
--- a/include/linux/xarray.h
+++ b/include/linux/xarray.h
@@ -1744,19 +1744,21 @@ static inline unsigned int xas_find_chunk(struct xa_state *xas, bool advance,
 {
 	unsigned long *addr = xas->xa_node->marks[(__force unsigned)mark];
 	unsigned int offset = xas->xa_offset;
+	unsigned long marks;
 
 	if (advance)
 		offset++;
+	marks = READ_ONCE(*addr);
 	if (XA_CHUNK_SIZE == BITS_PER_LONG) {
 		if (offset < XA_CHUNK_SIZE) {
-			unsigned long data = *addr & (~0UL << offset);
+			unsigned long data = marks & (~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



^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-30  7:01 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-29  7:01 [PATCH] xarray: add READ_ONCE() in xas_find_chunk() Shuai Liu
2026-11-30  7:01 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox