Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Shuai Liu" <847807867@qq.com>, willy@infradead.org
Cc: linux-mm@kvack.org, elver@google.com,
	syzbot+cc64b6bdf649b54047e0@syzkaller.appspotmail.com
Subject: Re: [PATCH] xarray: add READ_ONCE() in xas_find_chunk()
Date: Mon, 30 Nov 2026 07:01:25 +0000	[thread overview]
Message-ID: <sashiko-outbox-240545@kernel.org> (raw)
In-Reply-To: <tencent_26261313EE14D2462CBFF2E64D2AB8226208@qq.com>

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


      reply	other threads:[~2026-11-30  7:01 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29  7:01 [PATCH] xarray: add READ_ONCE() in xas_find_chunk() Shuai Liu
2026-11-30  7:01 ` sashiko-bot [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-240545@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=847807867@qq.com \
    --cc=elver@google.com \
    --cc=linux-mm@kvack.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=syzbot+cc64b6bdf649b54047e0@syzkaller.appspotmail.com \
    --cc=willy@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox