Linux Perf Users
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Zhanpeng Zhang" <zhangzhanpeng.jasper@bytedance.com>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH v13 10/11] selftests/riscv: add SSE test module
Date: Thu, 19 Nov 2026 12:37:13 +0000	[thread overview]
Message-ID: <sashiko-outbox-223098@kernel.org> (raw)
In-Reply-To: <20261119120824.13969-23-zhangzhanpeng.jasper@bytedance.com>

Thank you for your contribution! Sashiko AI review found 7 potential
issues to consider:
- [Medium] selftests/riscv: sse: deadlock between migrate_disable() and
  cpus_read_lock()
- [Medium] selftests/riscv: sse: fast test ignores errors and races on
  1-jiffy timeout
- [Medium] selftests/riscv: sse: stress test treats -EEXIST as failure
  instead of skip
- [Medium] selftests/riscv: sse module Makefile defaults KDIR to host
  /lib/modules
- [Low] selftests/riscv: sse: stale pointers left in priority_test_arg
  on teardown
- [Low] selftests/riscv: sse: swapped log args in sse_test_handler() and
  dead field
- [Low] selftests/riscv: run_sse_test.sh added with non-executable mode
  0644
--

commit e938d689e90e8ec77bc093b9edf15fcf64552952
Author: Clément Léger <cleger@rivosinc.com>

selftests/riscv: add SSE test module

This commit adds a kernel module and kselftest runner script to test the
RISC-V SBI Supervisor Software Events (SSE) framework, including fast
injection, event priority chaining, and stress modes.

> diff --git a/tools/testing/selftests/riscv/sse/module/Makefile b/tools/testing/selftests/riscv/sse/module/Makefile
> new file mode 100644
> index 0000000000000..eac4b1c6228bb
> --- /dev/null
> +++ b/tools/testing/selftests/riscv/sse/module/Makefile
> @@ -0,0 +1,22 @@
> +ifneq ($(CONFIG_RISCV_SBI_SSE),)
> +obj-m += riscv_sse_test.o
> +endif
> +
> +ifndef KERNELRELEASE
> +
> +TESTMODS_DIR := $(realpath $(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
> +KDIR ?= /lib/modules/$(shell uname -r)/build
> +
> +# Ensure that KDIR exists, otherwise skip the compilation
> +modules:
> +ifneq ("$(wildcard $(KDIR))", "")
> +	$(Q)$(MAKE) -C $(KDIR) modules KBUILD_EXTMOD=$(TESTMODS_DIR)
> +endif

[Severity: Medium]
Does defaulting KDIR to /lib/modules/$(shell uname -r)/build break
cross-compilation and in-tree or O= builds?

When cross-compiling with ARCH=riscv on a non-RISC-V host where
/lib/modules/$(uname -r)/build exists, Kbuild runs against the host kernel
headers with ARCH=riscv and fails.  When /lib/modules/$(uname -r)/build is
absent or points to a running kernel without CONFIG_RISCV_SBI_SSE,
riscv_sse_test.ko is silently skipped instead of building against the
target kernel tree.  Other selftest modules (such as
tools/testing/selftests/mm/page_frag/Makefile) resolve KDIR from O,
KBUILD_OUTPUT, or $(abs_srctree).

> diff --git a/tools/testing/selftests/riscv/sse/module/riscv_sse_test.c b/tools/testing/selftests/riscv/sse/module/riscv_sse_test.c
> new file mode 100644
> index 0000000000000..cc5c2e46f2fdb
> --- /dev/null
> +++ b/tools/testing/selftests/riscv/sse/module/riscv_sse_test.c
[ ... ]
> +static int sse_test_handler(u32 evt, void *arg, struct pt_regs *regs)
> +{
> +	int ret = 0;
> +	struct fast_test_arg *targ = arg;
> +	u32 test_evt;
> +	int cpu;
> +
> +	atomic_inc(&sse_test_handler_count);
> +
> +	/* Pairs with the argument publication in sse_run_fast_test_cpu(). */
> +	if (!smp_load_acquire(&targ->args_ready)) {
> +		sse_err("Received SSE event %x before its test arguments were published\n",
> +			evt);
> +		ret = -EINVAL;
> +		goto complete;
> +	}
> +
> +	test_evt = READ_ONCE(targ->evt);
> +	cpu = READ_ONCE(targ->cpu);
> +
> +	if (evt != test_evt) {
> +		sse_err("Received SSE event id %x instead of %x\n", test_evt, evt);
> +		ret = -EINVAL;
> +	}

[Severity: Low]
Are test_evt and evt swapped in this sse_err() call?  evt is the received
event ID and test_evt is the expected one.

Also, struct priority_test_arg below defines an unused check_func member.

[ ... ]

> +static int sse_run_fast_test_cpu(struct fast_test_arg *test_arg,
> +				 struct sse_event *event, u32 evt, int cpu)
> +{
> +	unsigned long timeout;
> +	int ret;
> +
> +	WRITE_ONCE(test_arg->completion, false);
> +	WRITE_ONCE(test_arg->args_ready, false);
> +	WRITE_ONCE(test_arg->evt, evt);
> +	WRITE_ONCE(test_arg->cpu, cpu);
> +	/* Publish all arguments before firmware can inject on another hart. */
> +	smp_store_release(&test_arg->args_ready, true);
> +
> +	ret = sse_test_inject_event(event, evt, cpu);
> +	if (ret) {
> +		sse_err("event %s injection failed, err %d\n",
> +			sse_evt_name(evt), ret);
> +		return ret;
> +	}
> +
> +	timeout = jiffies + HZ / 100;
> +	/* We can not use <linux/completion.h> since they are not NMI safe */
> +	/* Pairs with the handler's completion publication. */
> +	while (!smp_load_acquire(&test_arg->completion) &&
> +	       time_before(jiffies, timeout))
> +		cpu_relax();
> +	/* Acquire the handler's checks even if the loop observed a timeout. */
> +	if (!smp_load_acquire(&test_arg->completion)) {
> +		sse_err("Failed to wait for event %s completion on CPU %d\n",
> +			sse_evt_name(evt), cpu);
> +		return -ETIMEDOUT;
> +	}
> +
> +	return sse_test_wait_not_running_on_cpu(evt, cpu);
> +}
> +
> +static void sse_run_fast_test(struct fast_test_arg *test_arg,
> +			      struct sse_event *event, u32 evt)
> +{
> +	int cpu;
> +
> +	if (sse_event_is_global(evt)) {
> +		/* Keep the selected target online through injection and completion. */
> +		cpu_hotplug_disable();
> +		for_each_online_cpu(cpu) {
> +			if (sse_run_fast_test_cpu(test_arg, event, evt, cpu))
> +				break;
> +		}
> +		cpu_hotplug_enable();
> +		return;
> +	}
> +
> +	guard(cpus_read_lock)();
> +	for_each_online_cpu(cpu) {
> +		if (sse_run_fast_test_cpu(test_arg, event, evt, cpu))
> +			return;
> +	}
> +}
> +
> +static void sse_test_injection_fast(void)
> +{
[ ... ]
> +		for (j = 0; j < RUN_LOOP_COUNT; j++)
> +			sse_run_fast_test(&fast_test_arg, event, evt);

[Severity: Medium]
Can a timeout in sse_run_fast_test_cpu() race with an in-flight handler on
fast_test_arg across the remaining RUN_LOOP_COUNT iterations?

With CONFIG_HZ=100, HZ / 100 is 1 jiffy, which can expire on the next
timer tick while waiting for a remote CPU.  On timeout,
sse_run_fast_test_cpu() returns without calling
sse_test_wait_not_running_on_cpu(), and sse_run_fast_test() discards the
error so sse_test_injection_fast() immediately starts the next iteration
and overwrites the shared static fast_test_arg while the delayed handler
may still be running (or stalls module load if all 1,000 iterations time
out).  In addition, non-timeout errors in
sse_test_wait_not_running_on_cpu() (-ENOMEM or smp_call_function_single()
and sse_event_attr_read() failures) return without logging sse_err(), so
run_sse_test.sh will not detect them.

[ ... ]

> +static void sse_test_injection_priority_arg(struct priority_test_arg *args, unsigned int args_size,
> +					    sse_event_handler_fn handler, const char *test_name)
> +{
> +	unsigned int i;
> +	unsigned long timeout;
> +	int ret;
> +	int target_cpu;
> +	struct sse_event *event;
> +	struct priority_test_arg *arg, *first_arg = NULL, *prev_arg = NULL;
> +
> +	pr_info("Starting SSE priority test (%s)\n", test_name);
> +	/* Keep the complete priority chain on one CPU. */
> +	migrate_disable();
> +	target_cpu = smp_processor_id();
> +
> +	for (i = 0; i < args_size; i++) {
> +		arg = &args[i];
> +
> +		if (!sse_test_can_inject_event(arg->evt))
> +			continue;
> +
> +		WRITE_ONCE(arg->called, false);
> +		WRITE_ONCE(arg->next_evt_arg, NULL);
> +		WRITE_ONCE(arg->event, NULL);
> +		WRITE_ONCE(arg->enable_attempted, false);
> +
> +		event = sse_test_event_register(arg->evt, arg->prio, handler,
> +						(void *)arg);

[Severity: Medium]
Can calling sse_test_event_register() and the other SSE helpers inside
migrate_disable() deadlock with CPU hotplug?

migrate_disable() pins the task to target_cpu without blocking CPU
hotplug, while sse_event_register(), sse_event_set_target_cpu(),
sse_event_enable(), sse_event_disable(), and sse_event_unregister() each
acquire cpus_read_lock() internally:

CPU0 (target_cpu)                 CPU1
-----------------                 ----
sse_test_injection_priority_arg()
  migrate_disable()
                                  _cpu_down(target_cpu)
                                    cpus_write_lock()
                                    ...
                                    sched_cpu_wait_empty()
                                      balance_hotplug_wait()
  sse_test_event_register()
    sse_event_register()
      mutex_lock(&sse_mutex)
      cpus_read_lock()

 *** DEADLOCK ***

CPU1 holds cpus_write_lock() and waits in balance_hotplug_wait() for
pinned tasks on target_cpu to call migrate_enable(), while CPU0 is pinned
to target_cpu and blocks in cpus_read_lock() (also holding sse_mutex).
Surrounding the migrate_disable() section with cpu_hotplug_disable() and
cpu_hotplug_enable() avoids the lock inversion and prevents
sse_cpu_teardown() from tearing down SSE on target_cpu mid-test.

[ ... ]

> +release_events:
> +
> +	for (i = 0; i < args_size; i++) {
> +		arg = &args[i];
> +		event = arg->event;
> +		if (!event)
> +			continue;
> +
> +		ret = sse_test_wait_not_running_on_cpu(arg->evt, target_cpu);
> +		if (ret)
> +			sse_err("Event %s did not complete, err %d\n",
> +				sse_evt_name(arg->evt), ret);
> +
> +		if (arg->enable_attempted) {
> +			ret = sse_event_disable(event);
> +			if (ret) {
> +				sse_err("Failed to disable event %s, err %d\n",
> +					sse_evt_name(arg->evt), ret);
> +				sse_test_queue_cleanup(event);
> +				WRITE_ONCE(arg->event, NULL);
> +				WRITE_ONCE(arg->enable_attempted, false);
> +				continue;
> +			}
> +		}
> +
> +		ret = sse_test_event_unregister(event);
> +		if (ret) {
> +			sse_err("Failed to unregister event %s, err %d\n",
> +				sse_evt_name(arg->evt), ret);
> +			continue;
> +		}
> +
> +		WRITE_ONCE(arg->event, NULL);
> +		WRITE_ONCE(arg->enable_attempted, false);
> +	}

[Severity: Low]
Does release_events leave stale pointers in priority_test_arg when disable
or unregister fails?

When sse_test_event_unregister() fails, it queues event for deferred
freeing via sse_test_queue_cleanup(), but the error branch jumps to the
next iteration before clearing arg->event and arg->enable_attempted.
Also, release_events never clears arg->next_evt_arg, so if an event fails
to disable or unregister and fires late after its successor in args[] has
been unregistered, the handler will read a non-NULL targ->next_evt_arg and
signal the unregistered successor event.

[ ... ]

> +static int sse_stress_test_layer(int layer)
> +{
[ ... ]
> +	event = sse_test_event_register(stress_ctx.evt_id, 0,
> +					stress_handlers[layer], &stress_ctx);
> +	if (IS_ERR(event)) {
> +		sse_err("Failed to register stress event, err %ld\n",
> +			PTR_ERR(event));
> +		return PTR_ERR(event);
> +	}
[ ... ]
> +static void sse_stress_test_all_layers(void)
> +{
> +	int i, ret;
> +
> +	pr_info("Starting SSE stress tests: duration=%d ms, interval=%d ns\n",
> +		STRESS_DURATION_MS, STRESS_INJECT_NS);
> +
> +	for (i = 0; i < ARRAY_SIZE(stress_handlers); i++) {
> +		ret = sse_stress_test_layer(i);
> +		if (ret) {
> +			sse_err("Stress layer %d failed, err %d\n", i, ret);
> +			break;
> +		}

[Severity: Medium]
Should sse_stress_test_layer() skip rather than fail when
sse_test_event_register() returns -EEXIST?

Unlike sse_test_injection_fast() and sse_test_injection_priority_arg(),
sse_stress_test_layer() and sse_stress_test_all_layers() log sse_err() on
-EEXIST.  Because run_sse_test.sh checks for FAILED: before SKIP:,
running with stress=1 or stress=2 when
SBI_SSE_EVENT_LOCAL_SOFTWARE_INJECTED is already registered exits with
failure (1) instead of ksft_skip (4).

> diff --git a/tools/testing/selftests/riscv/sse/run_sse_test.sh b/tools/testing/selftests/riscv/sse/run_sse_test.sh
> new file mode 100644
> index 0000000000000..e70a2fd14b05e
> --- /dev/null
> +++ b/tools/testing/selftests/riscv/sse/run_sse_test.sh
> @@ -0,0 +1,59 @@
> +#!/bin/bash
> +# SPDX-License-Identifier: GPL-2.0

[Severity: Low]
Should run_sse_test.sh have mode 0755 instead of 0644?

Because the script is not executable, invoking ./run_sse_test.sh directly
fails with permission denied, and tools/testing/selftests/kselftest/runner.sh
logs a warning and takes its non-executable fallback path, which drops
kselftest_cmd_args instead of forwarding module arguments such as stress=1
to insmod.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119120824.13969-13-zhangzhanpeng.jasper@bytedance.com?part=10

  reply	other threads:[~2026-11-19 12:37 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19 12:08 [PATCH v13 00/11] riscv: add SBI Supervisor Software Events support Zhanpeng Zhang
2026-11-19 12:08 ` [PATCH v13 01/11] riscv: add SBI SSE extension definitions Zhanpeng Zhang
2026-11-19 12:14   ` sashiko-bot
2026-11-19 12:08 ` [PATCH v13 02/11] riscv: add support for SBI Supervisor Software Events extension Zhanpeng Zhang
2026-11-19 12:36   ` sashiko-bot
2026-11-19 12:08 ` [PATCH v13 03/11] riscv: sse: mask events during shutdown and kexec Zhanpeng Zhang
2026-11-19 12:38   ` sashiko-bot
2026-11-19 12:08 ` [PATCH v13 04/11] drivers: firmware: add riscv SSE support Zhanpeng Zhang
2026-11-19 12:38   ` sashiko-bot
2026-11-19 12:08 ` [PATCH v13 05/11] riscv: mm: avoid enabling interrupts for nofault page faults Zhanpeng Zhang
2026-11-19 12:27   ` sashiko-bot
2026-11-19 12:08 ` [PATCH v13 06/11] riscv: enable NMI-safe lockdep irqflags tracking Zhanpeng Zhang
2026-11-19 12:21   ` sashiko-bot
2026-11-19 12:08 ` [PATCH v13 07/11] perf: RISC-V: support callchains with SSE delivery Zhanpeng Zhang
2026-11-19 12:45   ` sashiko-bot
2026-11-19 12:08 ` [PATCH v13 08/11] perf: RISC-V: add support for SSE event Zhanpeng Zhang
2026-11-19 12:34   ` sashiko-bot
2026-11-19 12:08 ` [PATCH v13 09/11] MAINTAINERS: add RISC-V SSE maintainers and file coverage Zhanpeng Zhang
2026-11-19 12:08 ` [PATCH v13 10/11] selftests/riscv: add SSE test module Zhanpeng Zhang
2026-11-19 12:37   ` sashiko-bot [this message]
2026-11-19 12:08 ` [PATCH v13 11/11] selftests/riscv: add perf user-stack SSE copy regression test Zhanpeng Zhang
2026-11-19 12:34   ` sashiko-bot

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-223098@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=zhangzhanpeng.jasper@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