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 11/11] selftests/riscv: add perf user-stack SSE copy regression test
Date: Thu, 19 Nov 2026 12:34:25 +0000	[thread overview]
Message-ID: <sashiko-outbox-223093@kernel.org> (raw)
In-Reply-To: <20261119120824.13969-24-zhangzhanpeng.jasper@bytedance.com>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Medium] selftests/riscv: perf_event_open() EINVAL fails instead of
  skipping
- [Medium] selftests/riscv: test_ustack_records_wellformed() accepts
  0-byte copy
- [Medium] selftests/riscv: burn_stack() overflows default musl pthread
  stack
- [Low] selftests/riscv: false skip on setup failure in
  test_sse_stress_no_crash()
- [Low] selftests/riscv: unused stress_thread fields and missing
  .gitignore entry
--

commit e1bf6fbd4261f2eb37c1aa64007c8a76c7256f6e
Author: Zhanpeng Zhang <zhangzhanpeng.jasper@bytedance.com>

selftests/riscv: add perf user-stack SSE copy regression test

This commit adds a userspace kselftest under tools/testing/selftests/riscv/sse
to exercise PERF_SAMPLE_STACK_USER user-stack copying from RISC-V SBI
Supervisor Software Events (SSE) context across a guard-page boundary and
under multi-CPU stress.

> diff --git a/tools/testing/selftests/riscv/sse/sse_perf_ustack.c b/tools/testing/selftests/riscv/sse/sse_perf_ustack.c
> new file mode 100644
> index 0000000000000..9535b6d7ba4c1
> --- /dev/null
> +++ b/tools/testing/selftests/riscv/sse/sse_perf_ustack.c
> @@ -0,0 +1,564 @@

[ ... ]

> +static struct perf_event_attr sampling_attr(unsigned long freq)
> +{
> +	struct perf_event_attr attr = {
> +		.type		= PERF_TYPE_HARDWARE,
> +		.size		= sizeof(attr),
> +		.config		= PERF_COUNT_HW_INSTRUCTIONS,
> +		.sample_type	= PERF_SAMPLE_STACK_USER,
> +		.sample_stack_user = STACK_DUMP_SIZE,
> +		.freq		= 1,
> +		.sample_freq	= freq,
> +		.disabled	= 1,
> +		.exclude_kernel	= 1,
> +		.exclude_hv	= 1,
> +	};
> +
> +	return attr;
> +}
> +
> +static bool open_skip_reason(int err, const char **why)
> +{
> +	switch (err) {
> +	case EACCES:
> +	case EPERM:
> +		*why = "insufficient privilege for PMU sampling (perf_event_paranoid)";
> +		return true;
> +	case ENOENT:
> +	case ENODEV:
> +	case EOPNOTSUPP:
> +		*why = "hardware PMU sampling event not available";
> +		return true;
> +	default:
> +		return false;
> +	}
> +}

[Severity: Medium]
Does perf_event_open() fail with EINVAL instead of skipping on RISC-V
systems without PMU overflow support?

When neither SBI SSE nor Sscofpmf IRQ overflow delivery is available,
pmu_sbi_device_probe() and riscv_pmu_legacy set both
PERF_PMU_CAP_NO_INTERRUPT and PERF_PMU_CAP_NO_EXCLUDE without registering a
riscv-pmu entry in /proc/interrupts.  Because sampling_attr() sets
exclude_kernel and exclude_hv, perf_try_init_event() rejects the event with
-EINVAL on PERF_PMU_CAP_NO_EXCLUDE before perf_event_open() reaches the
PERF_PMU_CAP_NO_INTERRUPT check that returns -EOPNOTSUPP, causing
test_ustack_records_wellformed() to report FAIL instead of SKIP.

[ ... ]

> +static int truncation_child(void *arg)
> +{
> +	int ready_fd = (intptr_t)arg;
> +	char ready = 1;
> +
> +	if (write(ready_fd, &ready, sizeof(ready)) != 1)
> +		return 1;
> +
> +	for (;;)
> +		__asm__ __volatile__("" : : : "memory");
> +}

[ ... ]

> +					if (dyn_size > dump_size) {
> +						ok = false;
> +						break;
> +					}
> +					if (dyn_size < dump_size)
> +						truncated++;

[Severity: Medium]
Does test_ustack_records_wellformed() copy 0 bytes on every sample and count
dyn_size == 0 as a valid guard-page truncation?

start_truncation_child() passes stack + page_size (immediately below the
PROT_NONE page) to clone(), and truncation_child() only allocates ready_fd
and ready before spinning, leaving sp within 16 to 48 bytes of the guard
page.  On RV64, __copy_from_user_inatomic() enters .Lword_copy in
arch/riscv/lib/uaccess.S and issues eight unrolled REG_L loads (64 bytes)
before storing anything or advancing the destination pointer, so the copy
faults during the first burst and returns a remaining count equal to
dump_size (dyn_size == 0).  Since any sample with dyn_size < dump_size
increments truncated, the subtest passes without verifying that a non-zero
partial stack copy occurred across the boundary.

Giving truncation_child() a live stack frame larger than 64 bytes and
requiring 0 < dyn_size && dyn_size < dump_size would ensure a partial copy
is actually tested.

[ ... ]

> +struct stress_thread {
> +	pthread_t tid;
> +	int cpu;
> +	int *stop;
> +	int fd;
> +	void *rb;
> +	size_t rb_bytes;
> +};

[ ... ]

> +		st->fd = -1;
> +		st->cpu = cpu;
> +		st->stop = &stop;
> +		st->rb_bytes = rb_bytes;

[Severity: Low]
This isn't a bug, but stress_thread::cpu and stress_thread::rb_bytes are
written here and never read anywhere.  Also, tools/testing/selftests/riscv/sse
is missing a .gitignore entry for the new sse_perf_ustack binary added to
TEST_GEN_PROGS.

[ ... ]

> +		CPU_ZERO(&set);
> +		CPU_SET(cpu, &set);
> +		pthread_attr_init(&thread_attr);
> +		ret = pthread_attr_setaffinity_np(&thread_attr, sizeof(set), &set);
> +		if (!ret)
> +			ret = pthread_create(&st->tid, &thread_attr,
> +					     stress_worker, st);
> +		pthread_attr_destroy(&thread_attr);

[Severity: Medium]
Can stress_worker() overflow the default pthread stack on musl libc?

burn_stack() is noinline and keeps unsigned long frame[32] (256 bytes on
64-bit RISC-V) live across RECURSE_DEPTH (512) recursive calls, consuming
about 145 KiB of stack.  Because thread_attr uses the default stack size
 without pthread_attr_setstacksize(), this exceeds musl's 128 KiB default
pthread stack and crashes the test with SIGSEGV.

[ ... ]

> +	if (started == 0) {
> +		free(threads);
> +		if (why)
> +			ksft_test_result_skip("sse stress: %s\n", why);
> +		else
> +			ksft_test_result_skip("sse stress: could not start any sampler\n");
> +		return;
> +	}

[Severity: Low]
Should the else branch report a failure or error rather than
ksft_test_result_skip() when started == 0 and why is NULL?

open_skip_reason() sets why when perf_event_open() fails due to missing
privileges or unavailable PMU support, so reaching started == 0 with
why == NULL means every sampler failed due to an unexpected
perf_event_open(), mmap(), or pthread creation error.

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

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

Thread overview: 21+ 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: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
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 [this message]

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-223093@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