Linux Trace Kernel
 help / color / mirror / Atom feed
* [PATCH] objpool: fix missing acquire when reading slot->last
@ 2026-11-28 12:49 Gui-Dong Han
  2026-12-01 10:28 ` wuqiang.matt
  0 siblings, 1 reply; 2+ messages in thread
From: Gui-Dong Han @ 2026-11-28 12:49 UTC (permalink / raw)
  To: Masami Hiramatsu, Matt Wu
  Cc: Andrew Morton, linux-trace-kernel, linux-kernel, Gui-Dong Han,
	baijiaju1990

__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
-- 
2.34.1


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

* Re: [PATCH] objpool: fix missing acquire when reading slot->last
  2026-11-28 12:49 [PATCH] objpool: fix missing acquire when reading slot->last Gui-Dong Han
@ 2026-12-01 10:28 ` wuqiang.matt
  0 siblings, 0 replies; 2+ messages in thread
From: wuqiang.matt @ 2026-12-01 10:28 UTC (permalink / raw)
  To: Gui-Dong Han, Masami Hiramatsu
  Cc: Andrew Morton, linux-trace-kernel, linux-kernel, baijiaju1990

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

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

end of thread, other threads:[~2026-12-01 10:28 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox