mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
To: Ivan Shapovalov <intelfx@intelfx.name>,
	Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	James Clark <james.clark@linaro.org>,
	Nick Terrell <terrelln@fb.com>, David Sterba <dsterba@suse.com>,
	linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 0/4] perf: support zstd-compressed kernel modules
Date: Tue, 24 Nov 2026 22:24:39 -0300	[thread overview]
Message-ID: <20261125012441.391214-1-jonathan.goyvaerts@gmail.com> (raw)
In-Reply-To: <auwQRrlxzuTg4dDN@z2>

On Wed, Nov 04, 2026 at 09:12:38PM -0800, Namhyung Kim wrote:
> Can you please address comments from Sashiko and the bot?

Ivan, in case it saves you time: the diff below goes on top of v1 and
addresses those comments, plus a few more things I ran into. Please
fold it into v2 as you see fit; if you take it as is, a
Co-developed-by: with my Signed-off-by: is fine with me.

For the Sashiko and LKP reports:

- 4/4: step back by 3 (".ko") again, so .ko.gz and .ko.xz are modules
  again. This is also the LKP kmod_path__parse failure.
- 2/4: zstd_decompress_buffer() returns 0, not SIZE_MAX, when the
  stream can't be set up.
- 3/4: zstd_is_compressed() returns false when open() fails, and
  compares the magic as bytes, like gzip_is_compressed() does, so it
  needs no htole32() or <endian.h>.
- 1/4: the comment in perf_session__process_compressed_event() names
  zstd_decompress_buffer().

Not in those reports:

- 3/4: the !HAVE_ZSTD_SUPPORT stubs need __maybe_unused, or the
  make_minimal build (NO_LIBZSTD=1) fails with
  -Werror=unused-parameter.
- 3/4: the ZSTD_DStream{In,Out}Size() buffers are 256 KiB of VLAs on
  the stack. perf top decompresses modules on process_thread, which
  gets the default thread stack: 128 KiB on musl. With the stack
  limited by "ulimit -s 256", "perf buildid-cache -a asahi.ko.zst"
  segfaults with v1. With fixed 16 KiB buffers it works down to
  "ulimit -s 128".
- Tests: .ko.zst cases for kmod_path__parse, and a .ko.zst case in
  the build id cache shell test. The latter fails without the series
  and also feeds it a truncated file.
- Makefile.perf: the NO_LIBZSTD comment mentions modules.

Tested on arm64 (Arch Linux ARM, glibc, libzstd 1.5.7), where every
module is .ko.zst, with tools/perf as at v1's base-commit
0fcb05b72344f plus v1 and this diff:

- "perf buildid-cache -a asahi.ko.zst" adds the module.
- A "perf record -a -g" stores the .ko.zst paths and the module build
  ids, and perf script resolves all 3054 frames in the asahi module,
  also with an empty build id cache. Without the series all of them
  are [unknown].
- perf test: DSO data, kmod_path__parse, Symbols, build id cache
  operations and both Zstd tests pass.
- The default build and the make_minimal flag set from tests/make
  build.

Not done:

- Only arm64: no x86, 32-bit or big-endian build.
- Not tried on current perf-tools-next.
- .ko.gz and .ko.xz only through kmod_path__parse, not at runtime.
- Not the full perf test suite.
- The musl stack overflow is shown with ulimit on glibc, not on musl.
- On a truncated .ko.zst the loop ends with libzstd's "no progress"
  error; only libzstd 1.5.7 was tried.

An LLM coding assistant wrote this diff and drafted this mail. I asked
it to make perf read the .ko.zst modules on my machine. It found v1 on
lore, applied it, worked through the Sashiko and LKP reports, found
the rest with checkpatch.pl, perf's own tests and a live profile, and
wrote the fixes and tests. I have reviewed the diff and the test
results.

Thanks for the series,
Jonathan

Assisted-by: LLM
Signed-off-by: Jonathan Goyvaerts <jonathan.goyvaerts@gmail.com>
---
 tools/perf/Makefile.perf          |  4 ++--
 tools/perf/tests/kmod-path.c      | 21 ++++++++++++++++
 tools/perf/tests/shell/buildid.sh | 40 +++++++++++++++++++++++++++++++
 tools/perf/util/compress.h        |  9 ++++---
 tools/perf/util/dso.c             |  2 +-
 tools/perf/util/tool.c            |  2 +-
 tools/perf/util/zstd.c            | 31 ++++++++++++++----------
 7 files changed, 89 insertions(+), 20 deletions(-)

diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf
index 29cfd44c427f3..0d77f1aa0731e 100644
--- a/tools/perf/Makefile.perf
+++ b/tools/perf/Makefile.perf
@@ -106,8 +106,8 @@ include ../scripts/utilities.mak
 # streaming for record mode. Currently Posix AIO trace streaming is
 # supported only when linking with glibc.
 #
-# Define NO_LIBZSTD if you do not want support of Zstandard based runtime
-# trace compression in record mode.
+# Define NO_LIBZSTD if you do not want support of Zstandard based kernel
+# module decompression and runtime trace compression in record mode.
 #
 # Define TCMALLOC to enable tcmalloc heap profiling.
 #
diff --git a/tools/perf/tests/kmod-path.c b/tools/perf/tests/kmod-path.c
index dfe1bd5dabaa8..aacb49fbc9e4a 100644
--- a/tools/perf/tests/kmod-path.c
+++ b/tools/perf/tests/kmod-path.c
@@ -96,6 +96,27 @@ static int test__kmod_path__parse(struct test_suite *t __maybe_unused, int subte
 	M("x.ko.gz", PERF_RECORD_MISC_USER, false);
 #endif
 
+#ifdef HAVE_ZSTD_SUPPORT
+	/* The zst id is its index in compressions[], after gz and xz */
+#if defined(HAVE_ZLIB_SUPPORT) && defined(HAVE_LZMA_SUPPORT)
+#define ZST_COMP 3
+#elif defined(HAVE_ZLIB_SUPPORT) || defined(HAVE_LZMA_SUPPORT)
+#define ZST_COMP 2
+#else
+#define ZST_COMP 1
+#endif
+	/* path                    alloc_name kmod   comp      name */
+	T("/xxxx/xxxx/x-x.ko.zst", true,      true,  ZST_COMP, "[x_x]");
+	T("/xxxx/xxxx/x-x.ko.zst", false,     true,  ZST_COMP, NULL);
+	M("/xxxx/xxxx/x-x.ko.zst", PERF_RECORD_MISC_CPUMODE_UNKNOWN, true);
+	M("/xxxx/xxxx/x-x.ko.zst", PERF_RECORD_MISC_KERNEL, true);
+	M("/xxxx/xxxx/x-x.ko.zst", PERF_RECORD_MISC_USER, false);
+
+	T("/xxxx/xxxx/x.zst",      true,      false, ZST_COMP, "x.zst");
+	T("/xxxx/xxxx/x.zst",      false,     false, ZST_COMP, NULL);
+	M("/xxxx/xxxx/x.zst",      PERF_RECORD_MISC_KERNEL, false);
+#endif
+
 	/* path            alloc_name  kmod  comp   name           */
 	T("[test_module]", true      , true, false, "[test_module]");
 	T("[test_module]", false     , true, false, NULL           );
diff --git a/tools/perf/tests/shell/buildid.sh b/tools/perf/tests/shell/buildid.sh
index 102808cca9db3..2aca6dfc11b6f 100755
--- a/tools/perf/tests/shell/buildid.sh
+++ b/tools/perf/tests/shell/buildid.sh
@@ -44,9 +44,11 @@ ex_pe=$(dirname $0)/../pe-file.exe
 data=$(mktemp /tmp/perf_buildid_test.data.XXX)
 log_out=$(mktemp /tmp/perf_buildid_test.log.out.XXX)
 log_err=$(mktemp /tmp/perf_buildid_test.log.err.XXX)
+kmod_dir=$(mktemp -d /tmp/perf_buildid_test.kmod.XXX)
 
 cleanup() {
 	rm -f ${ex_source} ${ex_md5} ${ex_sha1} ${data} ${log_out} ${log_err}
+	rm -rf ${kmod_dir}
 	if [ ${run_pe} -eq 1 ]; then
 		rm -r ${wineprefix}
 	fi
@@ -198,6 +200,43 @@ test_add()
 	rm -rf ${build_id_dir}
 }
 
+# perf has to decompress a .ko.zst module to read its build id
+test_add_zst()
+{
+	if ! perf version --build-options | grep -q 'zstd: .* on '; then
+		echo "WARNING: perf not built with zstd. .ko.zst modules will not be tested."
+		return
+	fi
+	if ! [ -x "$(command -v zstd)" ]; then
+		echo "WARNING: zstd not found. .ko.zst modules will not be tested."
+		return
+	fi
+
+	build_id_dir=$(mktemp -d /tmp/perf_buildid_test.debug.XXX)
+	perf="perf --buildid-dir ${build_id_dir}"
+
+	# any ELF file with a build id will do as the module
+	zstd -q -c ${ex_sha1} > ${kmod_dir}/ex.ko.zst
+	head -c 100 ${kmod_dir}/ex.ko.zst > ${kmod_dir}/truncated.ko.zst
+
+	id=$(get_build_id ${ex_sha1})
+	${perf} buildid-cache -v -a ${kmod_dir}/ex.ko.zst
+	if ! ${perf} buildid-cache -l | grep -q ${id}; then
+		echo "failed: ${id} of ${kmod_dir}/ex.ko.zst not in cache"
+		exit 1
+	fi
+
+	# must fail without hanging; buildid-cache -a only warns on failure
+	${perf} buildid-cache -v -a ${kmod_dir}/truncated.ko.zst
+	if [ "$(${perf} buildid-cache -l | wc -l)" -ne 1 ]; then
+		echo "failed: truncated ${kmod_dir}/truncated.ko.zst was added"
+		exit 1
+	fi
+
+	echo "add .ko.zst: OK"
+	rm -rf ${build_id_dir}
+}
+
 test_remove()
 {
 	build_id_dir=$(mktemp -d /tmp/perf_buildid_test.debug.XXX)
@@ -304,6 +343,7 @@ test_add ${ex_md5}
 if [ ${add_pe} -eq 1 ]; then
 	test_add ${ex_pe}
 fi
+test_add_zst
 
 # add binaries via perf record post processing
 test_record ${ex_sha1}
diff --git a/tools/perf/util/compress.h b/tools/perf/util/compress.h
index fd6b541358bb6..c455686653907 100644
--- a/tools/perf/util/compress.h
+++ b/tools/perf/util/compress.h
@@ -92,17 +92,20 @@ static inline size_t zstd_decompress_buffer(struct zstd_data *data __maybe_unuse
 	return 0;
 }
 
-static inline int zstd_decompress_stream(struct zstd_data *data, int input_fd, int output_fd)
+static inline int zstd_decompress_stream(struct zstd_data *data __maybe_unused,
+					 int input_fd __maybe_unused,
+					 int output_fd __maybe_unused)
 {
 	return -1;
 }
 
-static inline int zstd_decompress_to_fd(const char *input, int output_fd)
+static inline int zstd_decompress_to_fd(const char *input __maybe_unused,
+					int output_fd __maybe_unused)
 {
 	return -1;
 }
 
-static inline bool zstd_is_compressed(const char *input)
+static inline bool zstd_is_compressed(const char *input __maybe_unused)
 {
 	return false;
 }
diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c
index 0e8886172e239..fbc58283ef658 100644
--- a/tools/perf/util/dso.c
+++ b/tools/perf/util/dso.c
@@ -463,7 +463,7 @@ int __kmod_path__parse(struct kmod_path *m, const char *path,
 
 	m->comp = is_supported_compression(ext + 1);
 	if (m->comp > COMP_ID__NONE)
-		ext -= strlen(compressions[m->comp].fmt);
+		ext -= 3;
 
 	/* Check .ko extension only if there's enough name left. */
 	if (ext > name)
diff --git a/tools/perf/util/tool.c b/tools/perf/util/tool.c
index d7956cd75c26d..dd8be27f57209 100644
--- a/tools/perf/util/tool.c
+++ b/tools/perf/util/tool.c
@@ -93,7 +93,7 @@ static int perf_session__process_compressed_event(const struct perf_tool *tool _
 		 * data_size is independent of header.size (which
 		 * includes padding); verify it doesn't exceed the
 		 * actual payload to prevent out-of-bounds reads in
-		 * zstd_decompress_stream().
+		 * zstd_decompress_buffer().
 		 */
 		if (src_size > event->header.size - sizeof(struct perf_record_compressed2))
 			goto err_decomp;
diff --git a/tools/perf/util/zstd.c b/tools/perf/util/zstd.c
index 982655a1f1a51..a063a680518cd 100644
--- a/tools/perf/util/zstd.c
+++ b/tools/perf/util/zstd.c
@@ -2,6 +2,7 @@
 
 #include <errno.h>
 #include <fcntl.h>
+#include <stdint.h>
 #include <string.h>
 #include <unistd.h>
 #include <linux/perf_event.h>
@@ -143,13 +144,12 @@ ssize_t zstd_compress_stream_to_records(struct zstd_data *data, void *dst, size_
 size_t zstd_decompress_buffer(struct zstd_data *data, void *src, size_t src_size,
 			      void *dst, size_t dst_size)
 {
-	ssize_t ret;
+	size_t ret;
 	ZSTD_inBuffer input = { src, src_size, 0 };
 	ZSTD_outBuffer output = { dst, dst_size, 0 };
 
-	ret = zstd_init_dstream(data);
-	if (ret < 0)
-		return ret;
+	if (zstd_init_dstream(data) < 0)
+		return 0;
 
 	while (input.pos < input.size) {
 		size_t prev_in = input.pos;
@@ -177,12 +177,18 @@ size_t zstd_decompress_buffer(struct zstd_data *data, void *src, size_t src_size
 	return output.pos;
 }
 
+/*
+ * Not ZSTD_DStream{In,Out}Size(): 256 KiB of stack is more than a musl
+ * thread gets, and perf top decompresses modules on a thread.
+ */
+#define CHUNK_SIZE 16384
+
 int zstd_decompress_stream(struct zstd_data *data, int input_fd, int output_fd)
 {
 	ssize_t ret;
 	bool unflushed = false;
-	char buf_in[ZSTD_DStreamInSize()];
-	char buf_out[ZSTD_DStreamOutSize()];
+	char buf_in[CHUNK_SIZE];
+	char buf_out[CHUNK_SIZE];
 	ZSTD_inBuffer in = {};
 	ZSTD_outBuffer out = {};
 
@@ -262,17 +268,16 @@ int zstd_decompress_to_fd(const char *input, int output_fd)
 bool zstd_is_compressed(const char *file)
 {
 	int fd = open(file, O_RDONLY);
-	const uint32_t magic_le = htole32(ZSTD_MAGICNUMBER);
-	char buf[4];
+	/* ZSTD_MAGICNUMBER as stored in the file (little-endian) */
+	const uint8_t magic[4] = { 0x28, 0xb5, 0x2f, 0xfd };
+	char buf[4] = { 0 };
 	ssize_t rc;
 
-	if (fd < 0) {
-		pr_err("zstd: failed to open(%s): %s\n", file, strerror(errno));
-		return -1;
-	}
+	if (fd < 0)
+		return false;
 
 	rc = read(fd, buf, sizeof(buf));
 	close(fd);
 	return rc == sizeof(buf) ?
-	       memcmp(buf, (const char *) &magic_le, sizeof(buf)) == 0 : false;
+	       memcmp(buf, magic, sizeof(buf)) == 0 : false;
 }

      reply	other threads:[~2026-11-25  1:25 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-14 17:34 Ivan Shapovalov
2026-10-14 17:34 ` [PATCH 1/4] perf: zstd: zstd_decompress_stream -> zstd_decompress_buffer Ivan Shapovalov
2026-10-14 17:34 ` [PATCH 2/4] perf: zstd: factor out zstd_init_{c,d}stream() Ivan Shapovalov
2026-10-14 17:34 ` [PATCH 3/4] perf: zstd: add streaming decompression and format probe functions Ivan Shapovalov
2026-10-14 17:34 ` [PATCH 4/4] perf: support zstd-compressed kernel modules Ivan Shapovalov
2026-10-27  2:58   ` kernel test robot
2026-11-05  5:12 ` [PATCH 0/4] " Namhyung Kim
2026-11-25  1:24   ` Jonathan Goyvaerts [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=20261125012441.391214-1-jonathan.goyvaerts@gmail.com \
    --to=jonathan.goyvaerts@gmail.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=dsterba@suse.com \
    --cc=intelfx@intelfx.name \
    --cc=irogers@google.com \
    --cc=james.clark@linaro.org \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    --cc=terrelln@fb.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

all inboxes | Powered by JetHome®