[PATCH v2 1/2] uprobes: Add missing read memory barrier in prepare_uprobe()

From: Masami Hiramatsu (Google)

Date: Fri Dec 04 2026 - 01:02:10 EST


From: Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx>

prepare_uprobe() uses double-checked locking around UPROBE_COPY_INSN to
serialize concurrent callers (for example, register_for_each_vma() and
uprobe_mmap()). The writer populates uprobe->arch, issues a memory
barrier, and sets UPROBE_COPY_INSN in uprobe->flags.

However, the lockless fast path at the start of prepare_uprobe() tests
UPROBE_COPY_INSN and returns 0 without a paired read/acquire memory
barrier. On weakly-ordered architectures, a concurrent caller taking the
fast path can observe UPROBE_COPY_INSN set while subsequently reading
stale or uninitialized fields from uprobe->arch (for instance,
auprobe->bpinsn in ARM's set_swbp()).

Use test_bit_acquire(UPROBE_COPY_INSN, &uprobe->flags) on the fast path
of prepare_uprobe() and switch the writer side from smp_wmb() to
smp_mb__before_atomic() before set_bit(UPROBE_COPY_INSN, &uprobe->flags)
to form a standard release/acquire pair. Also convert handle_swbp() from
test_bit() + smp_rmb() to test_bit_acquire() for consistency.

Reported-by: Sashiko <sashiko-bot@xxxxxxxxxx>
Closes: https://sashiko.dev/#/bug/linux-68084e34-bdf9-4e4a-8740-e4208f044e15
Fixes: 4710f05fd146 ("uprobes: Fix prepare_uprobe() race with itself")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx>
Acked-by: Oleg Nesterov <oleg@xxxxxxxxxx>
---
Changes in v2:
- Use test_bit_acquire(UPROBE_COPY_INSN, &uprobe->flags) on the fast
path of prepare_uprobe() and smp_mb__before_atomic() before
set_bit(UPROBE_COPY_INSN, &uprobe->flags), as suggested by Oleg.
- Convert handle_swbp() from test_bit() + smp_rmb() to
test_bit_acquire() for consistency.
---
kernel/events/uprobes.c | 24 ++++++++++++++----------
1 file changed, 14 insertions(+), 10 deletions(-)

diff --git a/kernel/events/uprobes.c b/kernel/events/uprobes.c
index c47b1ee266d5..2c04fa96fcd0 100644
--- a/kernel/events/uprobes.c
+++ b/kernel/events/uprobes.c
@@ -1101,7 +1101,11 @@ static int prepare_uprobe(struct uprobe *uprobe, struct file *file,
{
int ret = 0;

- if (test_bit(UPROBE_COPY_INSN, &uprobe->flags))
+ /*
+ * Pairs with smp_mb__before_atomic() + set_bit(UPROBE_COPY_INSN, ...)
+ * below so stores to &uprobe->arch are visible to callers.
+ */
+ if (test_bit_acquire(UPROBE_COPY_INSN, &uprobe->flags))
return ret;

/* TODO: move this into _register, until then we abuse this sem. */
@@ -1121,7 +1125,11 @@ static int prepare_uprobe(struct uprobe *uprobe, struct file *file,
if (ret)
goto out;

- smp_wmb(); /* pairs with the smp_rmb() in handle_swbp() */
+ /*
+ * Pairs with test_bit_acquire(UPROBE_COPY_INSN, ...) in
+ * prepare_uprobe() and handle_swbp().
+ */
+ smp_mb__before_atomic();
set_bit(UPROBE_COPY_INSN, &uprobe->flags);

out:
@@ -2754,18 +2762,14 @@ static void handle_swbp(struct pt_regs *regs)
* TODO: move copy_insn/etc into _register and remove this hack.
* After we hit the bp, _unregister + _register can install the
* new and not-yet-analyzed uprobe at the same address, restart.
- */
- if (unlikely(!test_bit(UPROBE_COPY_INSN, &uprobe->flags)))
- goto out;
-
- /*
- * Pairs with the smp_wmb() in prepare_uprobe().
*
- * Guarantees that if we see the UPROBE_COPY_INSN bit set, then
+ * Pairs with smp_mb__before_atomic() + set_bit(UPROBE_COPY_INSN, ...)
+ * in prepare_uprobe(). Guarantees that if we see UPROBE_COPY_INSN set,
* we must also see the stores to &uprobe->arch performed by the
* prepare_uprobe() call.
*/
- smp_rmb();
+ if (unlikely(!test_bit_acquire(UPROBE_COPY_INSN, &uprobe->flags)))
+ goto out;

/* Tracing handlers use ->utask to communicate with fetch methods */
if (!get_utask())