Linux Trace Kernel
 help / color / mirror / Atom feed
From: "wuqiang.matt" <wuqiang.matt@bytedance.com>
To: "Gui-Dong Han" <hanguidong02@gmail.com>,
	 "Masami Hiramatsu" <mhiramat@kernel.org>
Cc: "Andrew Morton" <akpm@linux-foundation.org>,
	 <linux-trace-kernel@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>,  <baijiaju1990@gmail.com>
Subject: Re: [PATCH] objpool: fix missing acquire when reading slot->last
Date: Tue, 1 Dec 2026 18:28:04 +0800	[thread overview]
Message-ID: <a576ab33-c2c6-4cf8-b5ad-fd99b2b9abff@bytedance.com> (raw)
In-Reply-To: <20261128124922.3900189-1-hanguidong02@gmail.com>

Indeed, 'obj = READ_ONCE()' could be re-ordered before the if-branch. And
smp_load_acquire() can make sure the correct order.

Has this issue been reproduced on real hardware? If you don’t mind sharing,
what CPU/architecture is it?

Masami, please help review and include this patch into your patch set if
it's appropriate to you. Thanks.

Reviewed-by: Matt Wu <wuqiang.matt@bytedance.com>

On 2026/11/28 20:49, Gui-Dong Han wrote:
> __objpool_try_add_slot() writes slot->entries[idx] and publishes it with
> smp_store_release(&slot->last, tail + 1). __objpool_try_get_slot()
> checks slot->last with plain READ_ONCE() before loading
> slot->entries[idx].
> 
> Because idx is derived from head rather than slot->last, there is no
> address dependency to order the entry load after the slot->last checks.
> On weakly ordered architectures, the entry load can be reordered after
> smp_load_acquire(&slot->head) but before the slot->last checks, reading
> a stale entry from the previous ring cycle:
> 
>    // Initially: head == last == k, entries[idx] holds old_obj
> 
>    CPU 0                         CPU 1
>    -----                         -----
>    // __objpool_try_add_slot()   // __objpool_try_get_slot()
>                                  head = smp_load_acquire(&slot->head);
>                                  // reordered before slot->last checks:
>                                  obj = READ_ONCE(slot->entries[idx]);
>                                    // reads old_obj
>    WRITE_ONCE(slot->entries[idx], new_obj);
>    smp_store_release(&slot->last, k + 1);
>                                  while (head != READ_ONCE(slot->last))
>                                    // sees k + 1
>                                  if (READ_ONCE(slot->last) - head - 1
>                                      >= pool->nr_objs)
>                                    // sees k + 1
>                                  try_cmpxchg_release(&slot->head,
>                                                      &head, k + 1);
>                                  return obj;
> 
> CPU 1 then returns old_obj while new_obj is lost.
> 
> Use smp_load_acquire(&slot->last) in the range check to pair with
> smp_store_release(&slot->last, ...). The initial loop condition can
> remain READ_ONCE().
> 
> Fixes: b4edb8d2d464 ("lib: objpool added: ring-array based lockless MPMC")
> Signed-off-by: Gui-Dong Han <hanguidong02@gmail.com>
> ---
>   include/linux/objpool.h | 3 ++-
>   1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/include/linux/objpool.h b/include/linux/objpool.h
> index b713a1fe7521..8350302d6e15 100644
> --- a/include/linux/objpool.h
> +++ b/include/linux/objpool.h
> @@ -144,7 +144,8 @@ static inline void *__objpool_try_get_slot(struct objpool_head *pool, int cpu)
>   		 * that is equivalent to 'last - head - 1 < nr_objs' as
>   		 * 'last' and 'head' are both unsigned int32
>   		 */
> -		if (READ_ONCE(slot->last) - head - 1 >= pool->nr_objs) {
> +		/* pair with smp_store_release() in __objpool_try_add_slot() */
> +		if (smp_load_acquire(&slot->last) - head - 1 >= pool->nr_objs) {
>   			head = READ_ONCE(slot->head);
>   			continue;
>   		}
> 
> base-commit: 8d7fe9885c62fc9a95bf55d9db244d9672a8aadc

      reply	other threads:[~2026-12-01 10:28 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-28 12:49 [PATCH] objpool: fix missing acquire when reading slot->last Gui-Dong Han
2026-12-01 10:28 ` wuqiang.matt [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=a576ab33-c2c6-4cf8-b5ad-fd99b2b9abff@bytedance.com \
    --to=wuqiang.matt@bytedance.com \
    --cc=akpm@linux-foundation.org \
    --cc=baijiaju1990@gmail.com \
    --cc=hanguidong02@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mhiramat@kernel.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