Re: [PATCH v2 15/16] selftests/membarrier: Check the memory barriers of each thread

From: Alexei Starovoitov

Date: Fri Nov 27 2026 - 18:45:47 EST


On Fri, Nov 27, 2026 at 10:58 AM Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxxxx> wrote:
> - The test lists the kernel functions which hold the instructions,
> and the kind of instruction to look for in each. It finds them by
> their bytes in the kernel text, which it reads from /proc/kcore.
> The kernel refuses a kprobe which is not on the first byte of an
> instruction, so a wrong match is dropped.

A bunch of x86 specific kprobes?
Wow. I haven't seen such a heavy duty selftest before.
I'm glad that it works, but I asked my bot for a simpler solution...

ipi_mb(), ipi_sync_core() and ipi_rseq() are straight-line code.
smp_mb() is their first statement. A call of the handler is an
executed barrier.
csd_do_func() has trace_csd_function_entry(func, csd) right before
func(info).

count the IPIs there instead:

kprobe on the insn csd_function_entry
--------------- -------------------------- ----------------------
finds the place kallsyms, kcore, opcodes __ksym, libbpf does it
arch x86-64 all
lost hits nested kprobe, none, irqs are off
bpf_prog_active
global state kprobes-optimization = 0 none

read_kernel(), scan_function(), probe_sites(), sites[], the cookies,
set_optprobes(), the signal handlers and the "probe hits were lost"
warning go away.

diff --git a/tools/testing/selftests/membarrier/membarrier_test_barrier.bpf.c b/tools/testing/selftests/membarrier/membarrier_test_barrier.bpf.c
new file mode 100644
index 0000000..ffaa899
--- /dev/null
+++ b/tools/testing/selftests/membarrier/membarrier_test_barrier.bpf.c
@@ -0,0 +1,104 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Count the membarrier IPIs and the context switches of each registered
+ * thread.
+ *
+ * See membarrier_test_barrier.c.
+ */
+#include <stdbool.h>
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+#include "membarrier_test_barrier.h"
+
+struct mm_struct;
+
+/* The fields which this program reads. CO-RE relocates them. */
+struct task_struct {
+ struct mm_struct *mm;
+} __attribute__((preserve_access_index));
+
+/*
+ * The IPI handlers. libbpf finds them in /proc/kallsyms. The address is 0
+ * when the kernel has no such handler.
+ */
+extern const void ipi_mb __ksym __weak;
+extern const void ipi_sync_core __ksym __weak;
+extern const void ipi_rseq __ksym __weak;
+
+/* Slot of a registered thread. */
+struct {
+ __uint(type, BPF_MAP_TYPE_TASK_STORAGE);
+ __uint(map_flags, BPF_F_NO_PREALLOC);
+ __type(key, int);
+ __type(value, __u32);
+} task_slot SEC(".maps");
+
+/* The counters of each slot. User-space maps them. */
+struct {
+ __uint(type, BPF_MAP_TYPE_ARRAY);
+ __uint(map_flags, BPF_F_MMAPABLE);
+ __uint(max_entries, MBT_MAX_SLOTS);
+ __type(key, __u32);
+ __type(value, struct mbt_counters);
+} counters SEC(".maps");
+
+/*
+ * Only the CPU which runs a thread, or which switches to it, counts for
+ * that thread. So the increments need no lock.
+ */
+static __always_inline struct mbt_counters *counters_of(struct task_struct *task)
+{
+ __u32 *slot = bpf_task_storage_get(&task_slot, task, 0, 0);
+
+ return slot ? bpf_map_lookup_elem(&counters, slot) : NULL;
+}
+
+/* csd_do_func() is about to call func(). Interrupts are disabled. */
+SEC("tp_btf/csd_function_entry")
+int BPF_PROG(csd_function_entry, void *func)
+{
+ struct mbt_counters *c;
+
+ if (func != &ipi_mb && func != &ipi_sync_core && func != &ipi_rseq)
+ return 0;
+ c = counters_of(bpf_get_current_task_btf());
+ if (!c)
+ return 0;
+ c->c[MBT_IPI]++;
+ if (func == &ipi_sync_core)
+ c->c[MBT_SYNC_CORE]++;
+ return 0;
+}
+
+SEC("tp_btf/sched_switch")
+int BPF_PROG(sched_switch, bool preempt, struct task_struct *prev,
+ struct task_struct *next)
+{
+ struct mbt_counters *c;
+
+ c = counters_of(prev);
+ if (c)
+ c->c[MBT_SCHED_OUT]++;
+ if (!next->mm || prev->mm == next->mm)
+ return 0;
+ c = counters_of(next);
+ if (c)
+ c->c[MBT_SCHED_IN]++;
+ return 0;
+}
+
+/* Give a slot to the calling thread. User-space runs this program. */
+SEC("syscall")
+int register_thread(struct mbt_register *ctx)
+{
+ __u32 slot = ctx->slot;
+
+ if (!bpf_task_storage_get(&task_slot, bpf_get_current_task_btf(), &slot,
+ BPF_LOCAL_STORAGE_GET_F_CREATE))
+ return -1;
+ return 0;
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/membarrier/membarrier_test_barrier.h b/tools/testing/selftests/membarrier/membarrier_test_barrier.h
new file mode 100644
index 0000000..da8edfd
--- /dev/null
+++ b/tools/testing/selftests/membarrier/membarrier_test_barrier.h
@@ -0,0 +1,34 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef MEMBARRIER_TEST_BARRIER_H
+#define MEMBARRIER_TEST_BARRIER_H
+
+#include <linux/types.h>
+
+/* Counters of a thread. */
+enum {
+ /* A membarrier IPI handler interrupted the thread. */
+ MBT_IPI,
+ /* The handler was ipi_sync_core(). */
+ MBT_SYNC_CORE,
+ /* The thread was scheduled out. */
+ MBT_SCHED_OUT,
+ /*
+ * The thread was scheduled in after a task of another mm, or
+ * after a kernel thread.
+ */
+ MBT_SCHED_IN,
+ MBT_NR_COUNTERS,
+};
+
+struct mbt_counters {
+ __u64 c[16];
+};
+
+/* Argument of the register_thread program. */
+struct mbt_register {
+ __u32 slot;
+};
+
+#define MBT_MAX_SLOTS 512
+
+#endif /* MEMBARRIER_TEST_BARRIER_H */

pw-bot: cr