Linux Trace Kernel
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Mathieu Desnoyers" <mathieu.desnoyers@efficios.com>
Cc: bpf@vger.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 19:15:03 +0000	[thread overview]
Message-ID: <sashiko-outbox-238151@kernel.org> (raw)
In-Reply-To: <20261127185822.158216-16-mathieu.desnoyers@efficios.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] selftests/membarrier: run_target() reads missed-barrier stat
  before sched-out
--

commit 2ae7bc75d862920739dabb87323389a8eebbf751
Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>

selftests/membarrier: Check the memory barriers of each thread

This commit adds membarrier_test_barrier, a BPF- and kprobe-based selftest
that counts per-thread memory barriers executed in the membarrier syscall,
IPI handlers, and scheduler paths.

> diff --git a/tools/testing/selftests/membarrier/membarrier_test_barrier.c b/tools/testing/selftests/membarrier/membarrier_test_barrier.c
> new file mode 100644
> index 0000000000000..88c5dbf5d7c79
> --- /dev/null
> +++ b/tools/testing/selftests/membarrier/membarrier_test_barrier.c
> @@ -0,0 +1,1270 @@

[ ... ]

> +/* Run the checks of a target. */
> +static void run_target(const struct target *target)
> +{
> +	struct target_stats *stats = &shared->stats[target->idx];
> +	const uint64_t *counters;
> +	long deadline = now_ms() + target->ms;
> +	unsigned long checks = 0;
> +	uint64_t first_ipi, first_sched_in;
> +
> +	/*
> +	 * Register before moving to the CPU. The BPF program then sees
> +	 * the target scheduled in there, as in the lazy TLB test case.
> +	 */
> +	counters = map_counters();
> +	if (!counters) {
> +		setup_error("register_thread");
> +		atomic_fetch_add(&shared->ready, 1);
> +		return;
> +	}
> +	if (target->cpu >= 0 && pin_to_cpu(target->cpu))
> +		setup_error("sched_setaffinity");
> +	atomic_fetch_add(&shared->ready, 1);
> +	while (!atomic_load(&shared->registered) && !atomic_load(&shared->stop))
> +		sched_yield();
> +	first_ipi = counter(counters, MBT_IPI);
> +	first_sched_in = counter(counters, MBT_SCHED_IN);
> +	while (!atomic_load(&shared->stop)) {
> +		if (check_call(counters, stats) && target->thread_exit &&
> +		    !(++checks % 16)) {
> +			pthread_t thread;
> +
> +			if (pthread_create(&thread, NULL, thread_nop, NULL) ||
> +			    pthread_join(thread, NULL))
> +				setup_error("thread exit");
> +		}
> +		if (target->ms && now_ms() >= deadline)
> +			break;
> +	}
> +	atomic_fetch_add(&stats->ipis, counter(counters, MBT_IPI) - first_ipi);
> +	atomic_fetch_add(&stats->sched_in,
> +			 counter(counters, MBT_SCHED_IN) - first_sched_in);
> +	atomic_fetch_add(&stats->sched_in_missed,
> +			 counter(counters, MBT_SCHED_IN_MISSED));
> +}

[Severity: Medium]
Can run_target() read MBT_SCHED_IN_MISSED before the target thread has
scheduled out to flush state->owed?

In membarrier_test_barrier.bpf.c, sched_switch only increments
MBT_SCHED_IN_MISSED when the CPU leaves the thread on a subsequent
context switch:

tools/testing/selftests/membarrier/membarrier_test_barrier.bpf.c:sched_switch() {
	...
	/* The CPU leaves the thread which it last switched to. */
	if (state->owed & OWES_BARRIER)
		count(state->slot, 1U << MBT_SCHED_IN_MISSED);
	count(slot_of(prev), 1U << MBT_SCHED_OUT);
	...
}

In global_expedited_lazy_tlb, shared->registered is already 1 when
run_target() is called, so after pin_to_cpu(target->cpu) switches the
child in after the idle thread on cpus[1], the child spins in user space
for ROUND_MS and reads MBT_SCHED_IN_MISSED while still running on cpus[1].
If the child is not preempted during that round, the schedule-out on
cpus[1] only happens at _exit(0) after run_target() has already recorded
stats->sched_in_missed, so a missing scheduler barrier on that switch-in
will go undetected.

Also, sampling first_sched_in after pin_to_cpu(target->cpu) subtracts out
the MBT_SCHED_IN increment from that initial migration onto target->cpu
when computing stats->sched_in.  Should first_sched_in be read before
pin_to_cpu(), and should run_target() force a schedule-out (e.g. via
sched_yield() or migrating off target->cpu) before reading
MBT_SCHED_IN_MISSED?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261127185822.158216-1-mathieu.desnoyers@efficios.com?part=15

  reply	other threads:[~2026-11-27 19:15 UTC|newest]

Thread overview: 5+ 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 ` [PATCH v2 15/16] selftests/membarrier: Check the memory barriers of each thread Mathieu Desnoyers
2026-11-27 19:15   ` sashiko-bot [this message]
2026-11-27 19:27     ` Mathieu Desnoyers
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=sashiko-outbox-238151@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /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