Re: [PATCH] objpool: fix missing acquire when reading slot->last

From: wuqiang.matt

Date: Tue Dec 01 2026 - 05:28:39 EST


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@xxxxxxxxxxxxx>

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@xxxxxxxxx>
> ---
> 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