From: Gui-Dong Han <hanguidong02@gmail.com>
To: Masami Hiramatsu <mhiramat@kernel.org>,
Matt Wu <wuqiang.matt@bytedance.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
linux-trace-kernel@vger.kernel.org, linux-kernel@vger.kernel.org,
Gui-Dong Han <hanguidong02@gmail.com>,
baijiaju1990@gmail.com
Subject: [PATCH] objpool: fix missing acquire when reading slot->last
Date: Sat, 28 Nov 2026 20:49:22 +0800 [thread overview]
Message-ID: <20261128124922.3900189-1-hanguidong02@gmail.com> (raw)
__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
reply other threads:[~2026-11-28 12:49 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20261128124922.3900189-1-hanguidong02@gmail.com \
--to=hanguidong02@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=baijiaju1990@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=mhiramat@kernel.org \
--cc=wuqiang.matt@bytedance.com \
/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