* [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
* Re: [PATCH] xarray: add READ_ONCE() in xas_find_chunk()
2026-11-29 7:01 [PATCH] xarray: add READ_ONCE() in xas_find_chunk() Shuai Liu
@ 2026-11-30 7:01 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-30 7:01 UTC (permalink / raw)
To: Shuai Liu, willy; +Cc: linux-mm, elver, syzbot+cc64b6bdf649b54047e0
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Critical] xarray: stack out-of-bounds read and broken 32-bit mark
search in xas_find_chunk()
- [Low] xarray: missing Fixes: trailer for xas_find_chunk() data race
fix
--
commit c0ac2830e45356e29b88c9ec8263075b4152d250
Author: Shuai Liu <847807867@qq.com>
> xarray: add READ_ONCE() in xas_find_chunk()
>
> 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>
[Severity: Low]
Should this bug fix include a Fixes: tag for commit b803b42823d0 ("xarray:
Add XArray iterators"), which introduced xas_find_chunk()?
> 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);
> }
[Severity: Critical]
Does passing &marks to find_next_bit() read out-of-bounds from the stack on
32-bit architectures?
When CONFIG_BASE_SMALL is disabled, XA_CHUNK_SIZE is 64, so on 32-bit
systems XA_MARK_LONGS is 2 and xas->xa_node->marks[mark] spans two 32-bit
words. Because marks is a single unsigned long initialized with only
READ_ONCE(*addr), find_next_bit(&marks, 64, offset) reads (&marks)[1] four
bytes past the stack variable and never inspects addr[1] (slots 32..63).
Besides missing marked entries in slots 32..63, if (&marks)[1] on the stack
has a bit set at an offset where node->slots[offset] is NULL,
xas_find_marked() sets advance = false and xas->xa_offset = offset, sees no
entry, and loops forever under rcu_read_lock() because xas_find_chunk()
keeps returning the same offset from stack memory.
--
Sashiko AI review · https://sashiko.dev/#/patchset/tencent_26261313EE14D2462CBFF2E64D2AB8226208@qq.com?part=1
^ permalink raw reply [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