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

From: Gui-Dong Han

Date: Sat Nov 28 2026 - 07:49:31 EST


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