From: "Alexei Starovoitov" <alexei.starovoitov@gmail.com>
To: "Mathieu Desnoyers" <mathieu.desnoyers@efficios.com>,
"Peter Zijlstra" <peterz@infradead.org>,
"Ingo Molnar" <mingo@redhat.com>
Cc: "Shuah Khan" <shuah@kernel.org>,
"Aniket Gattani" <aniketgattani@google.com>,
<linux-kernel@vger.kernel.org>, <linux-kselftest@vger.kernel.org>,
"Paul E. McKenney" <paulmck@kernel.org>, <bpf@vger.kernel.org>,
"Naveen N Rao" <naveen@kernel.org>,
"David S. Miller" <davem@davemloft.net>,
"Masami Hiramatsu" <mhiramat@kernel.org>,
<linux-trace-kernel@vger.kernel.org>
Subject: Re: [PATCH v2 15/16] selftests/membarrier: Check the memory barriers of each thread
Date: Fri, 27 Nov 2026 23:45:42 +0000 [thread overview]
Message-ID: <DN6E6AI3KW0Y.GIGIAPTDZFS@gmail.com> (raw)
In-Reply-To: <20261127185822.158216-16-mathieu.desnoyers@efficios.com>
On Fri, Nov 27, 2026 at 10:58 AM Mathieu Desnoyers <mathieu.desnoyers@efficios.com> 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
next prev parent reply other threads:[~2026-11-27 23:45 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-27 18:58 [PATCH v2 00/16] membarrier: fixes, selftests and documentation Mathieu Desnoyers
2026-11-27 18:58 ` Mathieu Desnoyers
2026-11-27 18:58 ` [PATCH v2 01/16] sched/membarrier: Fix stale runqueue state after a lazy mm switch Mathieu Desnoyers
2026-11-27 18:58 ` [PATCH v2 02/16] sched/membarrier: Report only completed registrations Mathieu Desnoyers
2026-11-27 18:58 ` [PATCH v2 03/16] selftests/membarrier: Rebuild the tests when their header changes Mathieu Desnoyers
2026-11-27 18:58 ` [PATCH v2 04/16] selftests/membarrier: Issue the sync core command in its test Mathieu Desnoyers
2026-11-27 18:58 ` [PATCH v2 05/16] selftests/membarrier: Run the tests on nohz_full kernels Mathieu Desnoyers
2026-11-27 18:58 ` [PATCH v2 06/16] selftests/membarrier: Test which CPUs the expedited commands interrupt Mathieu Desnoyers
2026-11-27 18:58 ` [PATCH v2 07/16] selftests/membarrier: Test command arguments and registration lifetime Mathieu Desnoyers
2026-11-27 18:58 ` [PATCH v2 08/16] LoongArch: membarrier: Add full memory barrier in switch_mm() Mathieu Desnoyers
2026-11-29 4:00 ` Huacai Chen
2026-11-27 18:58 ` [PATCH v2 09/16] parisc: " Mathieu Desnoyers
2026-11-27 18:58 ` [PATCH v2 10/16] Documentation/membarrier: Update the per-architecture barrier list Mathieu Desnoyers
2026-11-27 18:58 ` Mathieu Desnoyers
2026-11-27 18:58 ` [PATCH v2 11/16] Documentation/features: Update the membarrier sync core requirements Mathieu Desnoyers
2026-11-27 18:58 ` [PATCH v2 12/16] x86/entry: Remove stale membarrier sync core comments Mathieu Desnoyers
2026-11-27 18:58 ` [PATCH v2 13/16] selftests/membarrier: Add litmus tests of the memory ordering Mathieu Desnoyers
2026-11-27 18:58 ` [PATCH v2 14/16] MAINTAINERS: Add the membarrier selftests to MEMBARRIER SUPPORT Mathieu Desnoyers
2026-11-27 18:58 ` [PATCH v2 15/16] selftests/membarrier: Check the memory barriers of each thread Mathieu Desnoyers
2026-11-27 19:15 ` sashiko-bot
2026-11-27 19:27 ` Mathieu Desnoyers
2026-11-27 23:45 ` Alexei Starovoitov [this message]
2026-11-27 18:58 ` [PATCH v2 16/16] selftests/membarrier: Check the core serialization of the sync core command Mathieu Desnoyers
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=DN6E6AI3KW0Y.GIGIAPTDZFS@gmail.com \
--to=alexei.starovoitov@gmail.com \
--cc=aniketgattani@google.com \
--cc=bpf@vger.kernel.org \
--cc=davem@davemloft.net \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=mathieu.desnoyers@efficios.com \
--cc=mhiramat@kernel.org \
--cc=mingo@redhat.com \
--cc=naveen@kernel.org \
--cc=paulmck@kernel.org \
--cc=peterz@infradead.org \
--cc=shuah@kernel.org \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.