Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] xarray: add READ_ONCE() in xas_find_chunk()
@ 2026-12-05  9:24 Shuai Liu
  2026-12-06  9:25 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Shuai Liu @ 2026-12-05  9:24 UTC (permalink / raw)
  To: Matthew Wilcox
  Cc: linux-fsdevel, linux-mm, syzbot+cc64b6bdf649b54047e0,
	kernel test robot

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 <yi1.lai@intel.com>
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



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

* Re: [PATCH v2] xarray: add READ_ONCE() in xas_find_chunk()
  2026-12-05  9:24 [PATCH v2] xarray: add READ_ONCE() in xas_find_chunk() Shuai Liu
@ 2026-12-06  9:25 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-06  9:25 UTC (permalink / raw)
  To: Shuai Liu, Matthew Wilcox
  Cc: linux-mm, syzbot+cc64b6bdf649b54047e0, kernel test robot

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] xarray: xas_find_chunk() loads marks before bounds check and
  spills on 32-bit
- [Low] xarray: commit message describes unmerged v1 32-bit OOB bug
--

commit 60ccda285c1568ab1c0c4681e1b2cd921d031e30
Author: Shuai Liu <847807867@qq.com>

xarray: add READ_ONCE() in xas_find_chunk()

This commit snapshots all XA_MARK_LONGS words of xas->xa_node->marks[] with
READ_ONCE() into a local array in xas_find_chunk() before scanning for the
next set mark bit, fixing a KCSAN data race against concurrent mark updates.

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

[Severity: Low]
Is the final clause accurate relative to the parent commit?

Before this patch, xas_find_chunk() passed addr directly to find_next_bit(),
which already covered all XA_MARK_LONGS words on 32-bit.  Reading past a
single word was only an issue in v1, so the permanent commit message should
not imply the parent commit had that bug.

> diff --git a/include/linux/xarray.h b/include/linux/xarray.h
> index 8c42d899d94cf..95c3fc228d3da 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);
>  }

[Severity: Medium]
Can this bounds check run before the READ_ONCE() loop, and can the 32-bit
path avoid the marks[] stack array?

Because READ_ONCE() emits volatile loads, the compiler cannot sink the loop
past the offset < XA_CHUNK_SIZE check.  Callers like xas_next_marked() pass
advance=true when xa_offset == XA_CHUNK_MASK, so every leaf-node boundary
now performs wasted volatile loads and discards the result.

On 32-bit (XA_CHUNK_SIZE == 64, BITS_PER_LONG == 32), small_const_nbits(64)
is false, so find_next_bit() passes marks[] to out-of-line _find_next_bit().
This forces a stack spill, loads both words even when offset >= 32, and adds
-fstack-protector-strong overhead to callers like xas_find_marked().

Returning XA_CHUNK_SIZE when offset >= XA_CHUNK_SIZE first, using
READ_ONCE(*addr) in the XA_CHUNK_SIZE == BITS_PER_LONG branch, and scanning
words inline from offset / BITS_PER_LONG with READ_ONCE(addr[i]) would avoid
both the dead loads and the 32-bit stack spill.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/tencent_13BC58D584B17B82F43C8BBF7C61D3096E06@qq.com?part=1


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

end of thread, other threads:[~2026-12-06  9:25 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05  9:24 [PATCH v2] xarray: add READ_ONCE() in xas_find_chunk() Shuai Liu
2026-12-06  9:25 ` sashiko-bot

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