* [PATCH 0/4] perf: support zstd-compressed kernel modules
@ 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
` (4 more replies)
0 siblings, 5 replies; 8+ messages in thread
From: Ivan Shapovalov @ 2026-10-14 17:34 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark, Nick Terrell,
David Sterba
Cc: linux-perf-users, linux-kernel, Ivan Shapovalov
This extends in-perf zstd code to add an interface for streaming
decompression of an entire file, and uses that interface in dso.c to
support handling of zstd-compressed DSOs (e.g., zstd-compressed kernel
modules, as shipped by various distros including Arch).
Signed-off-by: Ivan Shapovalov <intelfx@intelfx.name>
---
Ivan Shapovalov (4):
perf: zstd: zstd_decompress_stream -> zstd_decompress_buffer
perf: zstd: factor out zstd_init_{c,d}stream()
perf: zstd: add streaming decompression and format probe functions
perf: support zstd-compressed kernel modules
tools/perf/Makefile.config | 2 +-
tools/perf/util/compress.h | 22 +++++-
tools/perf/util/dso.c | 5 +-
tools/perf/util/tool.c | 2 +-
tools/perf/util/zstd.c | 188 +++++++++++++++++++++++++++++++++++++--------
5 files changed, 183 insertions(+), 36 deletions(-)
---
base-commit: 0fcb05b72344f40806cbe810de8872f1698bf053
change-id: 20261014-perf-zstd-279017105eb3
Best regards,
--
Ivan Shapovalov <intelfx@intelfx.name>
^ permalink raw reply [flat|nested] 8+ messages in thread* [PATCH 1/4] perf: zstd: zstd_decompress_stream -> zstd_decompress_buffer 2026-10-14 17:34 [PATCH 0/4] perf: support zstd-compressed kernel modules Ivan Shapovalov @ 2026-10-14 17:34 ` Ivan Shapovalov 2026-10-14 17:34 ` [PATCH 2/4] perf: zstd: factor out zstd_init_{c,d}stream() Ivan Shapovalov ` (3 subsequent siblings) 4 siblings, 0 replies; 8+ messages in thread From: Ivan Shapovalov @ 2026-10-14 17:34 UTC (permalink / raw) To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo, Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa, Ian Rogers, Adrian Hunter, James Clark, Nick Terrell, David Sterba Cc: linux-perf-users, linux-kernel, Ivan Shapovalov Existing zstd_decompress_stream() function is a misnomer. Rename that to zstd_decompress_buffer() to free the name for a future _actual_ streaming decompression function. Signed-off-by: Ivan Shapovalov <intelfx@intelfx.name> --- tools/perf/util/compress.h | 4 ++-- tools/perf/util/tool.c | 2 +- tools/perf/util/zstd.c | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tools/perf/util/compress.h b/tools/perf/util/compress.h index ec6c38129e248..e7aa89d31fc6f 100644 --- a/tools/perf/util/compress.h +++ b/tools/perf/util/compress.h @@ -57,7 +57,7 @@ ssize_t zstd_compress_stream_to_records(struct zstd_data *data, void *dst, size_ ssize_t process_header(void *record, size_t dst_size, size_t data_size)); -size_t zstd_decompress_stream(struct zstd_data *data, void *src, size_t src_size, +size_t zstd_decompress_buffer(struct zstd_data *data, void *src, size_t src_size, void *dst, size_t dst_size); #else /* !HAVE_ZSTD_SUPPORT */ @@ -82,7 +82,7 @@ ssize_t zstd_compress_stream_to_records(struct zstd_data *data __maybe_unused, return 0; } -static inline size_t zstd_decompress_stream(struct zstd_data *data __maybe_unused, void *src __maybe_unused, +static inline size_t zstd_decompress_buffer(struct zstd_data *data __maybe_unused, void *src __maybe_unused, size_t src_size __maybe_unused, void *dst __maybe_unused, size_t dst_size __maybe_unused) { diff --git a/tools/perf/util/tool.c b/tools/perf/util/tool.c index 25c9b378aa163..d7956cd75c26d 100644 --- a/tools/perf/util/tool.c +++ b/tools/perf/util/tool.c @@ -101,7 +101,7 @@ static int perf_session__process_compressed_event(const struct perf_tool *tool _ goto err_decomp; } - decomp_size = zstd_decompress_stream(session->active_decomp->zstd_decomp, src, src_size, + decomp_size = zstd_decompress_buffer(session->active_decomp->zstd_decomp, src, src_size, &(decomp->data[decomp_last_rem]), decomp_len - decomp_last_rem); if (!decomp_size) { munmap(decomp, mmap_len); diff --git a/tools/perf/util/zstd.c b/tools/perf/util/zstd.c index d98014902f012..af113660d1ce0 100644 --- a/tools/perf/util/zstd.c +++ b/tools/perf/util/zstd.c @@ -104,7 +104,7 @@ ssize_t zstd_compress_stream_to_records(struct zstd_data *data, void *dst, size_ return -1; } -size_t zstd_decompress_stream(struct zstd_data *data, void *src, size_t src_size, +size_t zstd_decompress_buffer(struct zstd_data *data, void *src, size_t src_size, void *dst, size_t dst_size) { size_t ret; -- 2.55.0.8.g37e2a0019f ^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH 2/4] perf: zstd: factor out zstd_init_{c,d}stream() 2026-10-14 17:34 [PATCH 0/4] perf: support zstd-compressed kernel modules 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 ` Ivan Shapovalov 2026-10-14 17:34 ` [PATCH 3/4] perf: zstd: add streaming decompression and format probe functions Ivan Shapovalov ` (2 subsequent siblings) 4 siblings, 0 replies; 8+ messages in thread From: Ivan Shapovalov @ 2026-10-14 17:34 UTC (permalink / raw) To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo, Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa, Ian Rogers, Adrian Hunter, James Clark, Nick Terrell, David Sterba Cc: linux-perf-users, linux-kernel, Ivan Shapovalov Factor out functions to manage CStream/DStream in `struct zstd_data` for reuse across multiple zstd (de)compression entry points. Signed-off-by: Ivan Shapovalov <intelfx@intelfx.name> --- tools/perf/util/zstd.c | 82 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 52 insertions(+), 30 deletions(-) diff --git a/tools/perf/util/zstd.c b/tools/perf/util/zstd.c index af113660d1ce0..3af8910bb87cf 100644 --- a/tools/perf/util/zstd.c +++ b/tools/perf/util/zstd.c @@ -14,6 +14,50 @@ int zstd_init(struct zstd_data *data, int level) return 0; } +static int zstd_init_cstream(struct zstd_data *data) +{ + size_t ret; + + if (!data->cstream) { + data->cstream = ZSTD_createCStream(); + if (data->cstream == NULL) { + pr_err("zstd: failed to create compression stream\n"); + return -1; + } + + ret = ZSTD_initCStream(data->cstream, data->comp_level); + if (ZSTD_isError(ret)) { + pr_err("zstd: failed to initialize compression stream: %s\n", + ZSTD_getErrorName(ret)); + return -1; + } + } + + return 0; +} + +static int zstd_init_dstream(struct zstd_data *data) +{ + size_t ret; + + if (!data->dstream) { + data->dstream = ZSTD_createDStream(); + if (data->dstream == NULL) { + pr_err("zstd: failed to create decompression stream\n"); + return -1; + } + + ret = ZSTD_initDStream(data->dstream); + if (ZSTD_isError(ret)) { + pr_err("zstd: failed to initialize decompression stream: %s\n", + ZSTD_getErrorName(ret)); + return -1; + } + } + + return 0; +} + int zstd_fini(struct zstd_data *data) { if (data->dstream) { @@ -34,26 +78,14 @@ ssize_t zstd_compress_stream_to_records(struct zstd_data *data, void *dst, size_ ssize_t process_header(void *record, size_t dst_size, size_t data_size)) { - size_t ret, compressed = 0; - ssize_t size; + ssize_t ret, size, compressed = 0; ZSTD_inBuffer input = { src, src_size, 0 }; ZSTD_outBuffer output; void *record; - if (!data->cstream) { - data->cstream = ZSTD_createCStream(); - if (data->cstream == NULL) { - pr_err("Couldn't create compression stream.\n"); - return -1; - } - - ret = ZSTD_initCStream(data->cstream, data->comp_level); - if (ZSTD_isError(ret)) { - pr_err("Failed to initialize compression stream: %s\n", - ZSTD_getErrorName(ret)); - return -1; - } - } + ret = zstd_init_cstream(data); + if (ret < 0) + return ret; while (input.pos < input.size) { record = dst; @@ -107,24 +139,14 @@ 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) { - size_t ret; + ssize_t ret; ZSTD_inBuffer input = { src, src_size, 0 }; ZSTD_outBuffer output = { dst, dst_size, 0 }; - if (!data->dstream) { - data->dstream = ZSTD_createDStream(); - if (data->dstream == NULL) { - pr_err("Couldn't create decompression stream.\n"); - return 0; - } + ret = zstd_init_dstream(data); + if (ret < 0) + return ret; - ret = ZSTD_initDStream(data->dstream); - if (ZSTD_isError(ret)) { - pr_err("Failed to initialize decompression stream: %s\n", - ZSTD_getErrorName(ret)); - return 0; - } - } while (input.pos < input.size) { size_t prev_in = input.pos; size_t prev_out = output.pos; -- 2.55.0.8.g37e2a0019f ^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH 3/4] perf: zstd: add streaming decompression and format probe functions 2026-10-14 17:34 [PATCH 0/4] perf: support zstd-compressed kernel modules 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 ` Ivan Shapovalov 2026-10-14 17:34 ` [PATCH 4/4] perf: support zstd-compressed kernel modules Ivan Shapovalov 2026-11-05 5:12 ` [PATCH 0/4] " Namhyung Kim 4 siblings, 0 replies; 8+ messages in thread From: Ivan Shapovalov @ 2026-10-14 17:34 UTC (permalink / raw) To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo, Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa, Ian Rogers, Adrian Hunter, James Clark, Nick Terrell, David Sterba Cc: linux-perf-users, linux-kernel, Ivan Shapovalov Add an actual streaming zstd decompression (fd->fd) entry point, plus a file->fd wrapper and a helper to detect zstd magic on a file for compressions[] in dso.c. Signed-off-by: Ivan Shapovalov <intelfx@intelfx.name> --- tools/perf/util/compress.h | 18 ++++++++ tools/perf/util/zstd.c | 104 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+) diff --git a/tools/perf/util/compress.h b/tools/perf/util/compress.h index e7aa89d31fc6f..fd6b541358bb6 100644 --- a/tools/perf/util/compress.h +++ b/tools/perf/util/compress.h @@ -59,6 +59,9 @@ 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); +int zstd_decompress_stream(struct zstd_data *data, int input_fd, int output_fd); +int zstd_decompress_to_fd(const char *input, int output_fd); +bool zstd_is_compressed(const char *input); #else /* !HAVE_ZSTD_SUPPORT */ static inline int zstd_init(struct zstd_data *data __maybe_unused, int level __maybe_unused) @@ -88,6 +91,21 @@ 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) +{ + return -1; +} + +static inline int zstd_decompress_to_fd(const char *input, int output_fd) +{ + return -1; +} + +static inline bool zstd_is_compressed(const char *input) +{ + return false; +} #endif #endif /* PERF_COMPRESS_H */ diff --git a/tools/perf/util/zstd.c b/tools/perf/util/zstd.c index 3af8910bb87cf..982655a1f1a51 100644 --- a/tools/perf/util/zstd.c +++ b/tools/perf/util/zstd.c @@ -1,7 +1,11 @@ // SPDX-License-Identifier: GPL-2.0 +#include <errno.h> +#include <fcntl.h> #include <string.h> +#include <unistd.h> #include <linux/perf_event.h> +#include <internal/lib.h> #include "util/compress.h" #include "util/debug.h" @@ -172,3 +176,103 @@ size_t zstd_decompress_buffer(struct zstd_data *data, void *src, size_t src_size return output.pos; } + +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()]; + ZSTD_inBuffer in = {}; + ZSTD_outBuffer out = {}; + + ret = zstd_init_dstream(data); + if (ret < 0) + return ret; + + for (;;) { + if (in.pos == in.size) { + /* out of input, read more */ + ret = read(input_fd, buf_in, sizeof(buf_in)); + if (ret < 0) { + pr_err("zstd: failed to read() %zu bytes: %s\n", + sizeof(buf_in), strerror(errno)); + return ret; + } + in = (ZSTD_inBuffer) { + .src = &buf_in, + .pos = 0, + .size = ret, + }; + } + + if (in.pos == in.size && !unflushed) { + /* out of input, out of file, and nothing to flush, exit */ + break; + } + + out = (ZSTD_outBuffer) { + .dst = &buf_out, + .pos = 0, + .size = sizeof(buf_out), + }; + ret = ZSTD_decompressStream(data->dstream, &out, &in); + if (ZSTD_isError(ret)) { + pr_err("zstd: failed to decompress %zu bytes into buffer of %zu bytes (wrote %zu): %s\n", + in.size, out.size, out.pos, ZSTD_getErrorName(ret)); + return -1; + } + unflushed = ret > 0; + + ret = writen(output_fd, out.dst, out.pos); + if (ret < 0) { + pr_err("zstd: failed to write() %zu bytes: %s\n", + out.pos, strerror(errno)); + return ret; + } + } + + return 0; +} + +int zstd_decompress_to_fd(const char *input, int output_fd) +{ + int input_fd = -1; + int ret; + struct zstd_data data; + + input_fd = open(input, O_RDONLY); + if (input_fd < 0) { + pr_err("zstd: failed to open(%s): %s\n", input, strerror(errno)); + return -1; + } + + ret = zstd_init(&data, ZSTD_CLEVEL_DEFAULT); + if (ret < 0) + goto err; + + ret = zstd_decompress_stream(&data, input_fd, output_fd); + +err: + zstd_fini(&data); + close(input_fd); + return ret; +} + +bool zstd_is_compressed(const char *file) +{ + int fd = open(file, O_RDONLY); + const uint32_t magic_le = htole32(ZSTD_MAGICNUMBER); + char buf[4]; + ssize_t rc; + + if (fd < 0) { + pr_err("zstd: failed to open(%s): %s\n", file, strerror(errno)); + return -1; + } + + rc = read(fd, buf, sizeof(buf)); + close(fd); + return rc == sizeof(buf) ? + memcmp(buf, (const char *) &magic_le, sizeof(buf)) == 0 : false; +} -- 2.55.0.8.g37e2a0019f ^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH 4/4] perf: support zstd-compressed kernel modules 2026-10-14 17:34 [PATCH 0/4] perf: support zstd-compressed kernel modules Ivan Shapovalov ` (2 preceding siblings ...) 2026-10-14 17:34 ` [PATCH 3/4] perf: zstd: add streaming decompression and format probe functions Ivan Shapovalov @ 2026-10-14 17:34 ` Ivan Shapovalov 2026-10-27 2:58 ` kernel test robot 2026-11-05 5:12 ` [PATCH 0/4] " Namhyung Kim 4 siblings, 1 reply; 8+ messages in thread From: Ivan Shapovalov @ 2026-10-14 17:34 UTC (permalink / raw) To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo, Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa, Ian Rogers, Adrian Hunter, James Clark, Nick Terrell, David Sterba Cc: linux-perf-users, linux-kernel, Ivan Shapovalov Support handling loading zstd-compressed DSOs in perf (in addition to existing support for gzip/xz formats). This allows loading of e.g. zstd-compressed kernel modules, as shipped by various distros including Arch. Signed-off-by: Ivan Shapovalov <intelfx@intelfx.name> --- tools/perf/Makefile.config | 2 +- tools/perf/util/dso.c | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/tools/perf/Makefile.config b/tools/perf/Makefile.config index 4d5993da9f945..dfdda22becc39 100644 --- a/tools/perf/Makefile.config +++ b/tools/perf/Makefile.config @@ -982,7 +982,7 @@ ifndef NO_LIBZSTD EXTLIBS += -lzstd $(call detected,CONFIG_ZSTD) else - $(warning No libzstd found, disables trace compression, please install libzstd-dev[el] and/or set LIBZSTD_DIR) + $(warning No libzstd found, disables zstd kernel module decompression and trace compression, please install libzstd-dev[el] and/or set LIBZSTD_DIR) NO_LIBZSTD := 1 endif endif diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c index 42bfe30a3b518..0e8886172e239 100644 --- a/tools/perf/util/dso.c +++ b/tools/perf/util/dso.c @@ -277,6 +277,9 @@ static const struct { #endif #ifdef HAVE_LZMA_SUPPORT { "xz", lzma_decompress_to_file, lzma_is_compressed }, +#endif +#ifdef HAVE_ZSTD_SUPPORT + { "zst", zstd_decompress_to_fd, zstd_is_compressed }, #endif { NULL, NULL, NULL }, }; @@ -460,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 -= 3; + ext -= strlen(compressions[m->comp].fmt); /* Check .ko extension only if there's enough name left. */ if (ext > name) -- 2.55.0.8.g37e2a0019f ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 4/4] perf: support zstd-compressed kernel modules 2026-10-14 17:34 ` [PATCH 4/4] perf: support zstd-compressed kernel modules Ivan Shapovalov @ 2026-10-27 2:58 ` kernel test robot 0 siblings, 0 replies; 8+ messages in thread From: kernel test robot @ 2026-10-27 2:58 UTC (permalink / raw) To: Ivan Shapovalov Cc: oe-lkp, lkp, linux-perf-users, linux-kernel, Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo, Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa, Ian Rogers, Adrian Hunter, James Clark, Nick Terrell, David Sterba, Ivan Shapovalov, yi1.lai Hello, kernel test robot noticed "perf-sanity-tests.kmod_path_parse.fail" on: commit: de7d5e445f2f9dfc4186dc2bd86287d27c9e00bd ("[PATCH 4/4] perf: support zstd-compressed kernel modules") url: https://github.com/intel-lab-lkp/linux/commits/Ivan-Shapovalov/perf-zstd-zstd_decompress_stream-zstd_decompress_buffer/20261014-203407 patch link: https://lore.kernel.org/all/20261014-perf-zstd-v1-4-9652727886fa@intelfx.name/ patch subject: [PATCH 4/4] perf: support zstd-compressed kernel modules in testcase: perf-sanity-tests with following parameters: perf_compiler: gcc group: group-01 config: x86_64-rhel-9.4-bpf compiler: gcc-14 test machine: 16 threads 1 sockets Intel(R) Xeon(R) E-2278G CPU @ 3.40GHz (Coffee Lake-E) with 32G memory (please refer to attached dmesg/kmsg for entire log/backtrace) If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags | Reported-by: kernel test robot <yi1.lai@intel.com> | Closes: https://lore.kernel.org/oe-lkp/202610271022.fab706ea-lkp@intel.com 2026-10-25 22:43:28 sudo ASAN_OPTIONS=fast_unwind_on_malloc=0 /usr/src/linux-perf-x86_64-rhel-9.4-bpf-de7d5e445f2f9dfc4186dc2bd86287d27c9e00bd/tools/perf/perf test 34 -v 34: kmod_path__parse : Running (1 active) ---- start ---- ... FAILED tests/kmod-path.c:23 wrong kmod FAILED tests/kmod-path.c:63 failed ---- end(-1) ---- ... ==11697==ERROR: LeakSanitizer: detected memory leaks ... #17 0x7f7a0d1b9d64 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29d64) (BuildId: 5e5b51f4c39af9ac589408c1dc432ba390a273cc) #18 0x5569eccabb90 in _start (/usr/src/perf_selftests-x86_64-rhel-9.4-bpf-de7d5e445f2f9dfc4186dc2bd86287d27c9e00bd/tools/perf/perf+0x11edb90) (BuildId: c1567a8473607a54274bd80d073619ea9cdc5d77) SUMMARY: AddressSanitizer: 8 byte(s) leaked in 1 allocation(s). 34: kmod_path__parse : FAILED! The kernel config and materials to reproduce are available at: https://download.01.org/0day-ci/archive/20261027/202610271022.fab706ea-lkp@intel.com -- 0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 0/4] perf: support zstd-compressed kernel modules 2026-10-14 17:34 [PATCH 0/4] perf: support zstd-compressed kernel modules Ivan Shapovalov ` (3 preceding siblings ...) 2026-10-14 17:34 ` [PATCH 4/4] perf: support zstd-compressed kernel modules Ivan Shapovalov @ 2026-11-05 5:12 ` Namhyung Kim 2026-11-25 1:24 ` Jonathan Goyvaerts 4 siblings, 1 reply; 8+ messages in thread From: Namhyung Kim @ 2026-11-05 5:12 UTC (permalink / raw) To: Ivan Shapovalov Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo, Mark Rutland, Alexander Shishkin, Jiri Olsa, Ian Rogers, Adrian Hunter, James Clark, Nick Terrell, David Sterba, linux-perf-users, linux-kernel Hello, On Wed, Oct 14, 2026 at 08:34:06PM +0300, Ivan Shapovalov wrote: > This extends in-perf zstd code to add an interface for streaming > decompression of an entire file, and uses that interface in dso.c to > support handling of zstd-compressed DSOs (e.g., zstd-compressed kernel > modules, as shipped by various distros including Arch). Can you please address comments from Sashiko and the bot? Thanks, Namhyung > > Signed-off-by: Ivan Shapovalov <intelfx@intelfx.name> > --- > Ivan Shapovalov (4): > perf: zstd: zstd_decompress_stream -> zstd_decompress_buffer > perf: zstd: factor out zstd_init_{c,d}stream() > perf: zstd: add streaming decompression and format probe functions > perf: support zstd-compressed kernel modules > > tools/perf/Makefile.config | 2 +- > tools/perf/util/compress.h | 22 +++++- > tools/perf/util/dso.c | 5 +- > tools/perf/util/tool.c | 2 +- > tools/perf/util/zstd.c | 188 +++++++++++++++++++++++++++++++++++++-------- > 5 files changed, 183 insertions(+), 36 deletions(-) > --- > base-commit: 0fcb05b72344f40806cbe810de8872f1698bf053 > change-id: 20261014-perf-zstd-279017105eb3 > > Best regards, > -- > Ivan Shapovalov <intelfx@intelfx.name> > ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH 0/4] perf: support zstd-compressed kernel modules 2026-11-05 5:12 ` [PATCH 0/4] " Namhyung Kim @ 2026-11-25 1:24 ` Jonathan Goyvaerts 0 siblings, 0 replies; 8+ messages in thread From: Jonathan Goyvaerts @ 2026-11-25 1:24 UTC (permalink / raw) To: Ivan Shapovalov, Namhyung Kim Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo, Mark Rutland, Alexander Shishkin, Jiri Olsa, Ian Rogers, Adrian Hunter, James Clark, Nick Terrell, David Sterba, linux-perf-users, linux-kernel 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; } ^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-11-25 1:25 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-14 17:34 [PATCH 0/4] perf: support zstd-compressed kernel modules 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 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®