perf: support zstd-compressed kernel modules
Subsystems (2)
cover perf: support zstd-compressed kernel modules
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).
---
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,
Signature · 1 line
Ivan Shapovalov <[email protected]>
PATCH 1/4 perf: zstd: zstd_decompress_stream -> zstd_decompress_buffer
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.
--- 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(-)
tools/perf/util/compress.h +2 -2
@@ -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 ) {
tools/perf/util/tool.c +1 -1
@@ -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);
tools/perf/util/zstd.c +1 -1
@@ -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;
Signature · 1 line
2.55.0.8.g37e2a0019f
[email protected]
AI reviewRe: [PATCH 1/4] perf: zstd: zstd_decompress_stream -> zstd_decompress_buffer
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] perf: stale function name in comment for zstd_decompress_buffer --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] perf: stale function name in comment for zstd_decompress_buffer
--
commit a8489356f2f1a85e8397e7dba79a95d1bfde0cb1
Author: Ivan Shapovalov <[email protected]>
perf: zstd: zstd_decompress_stream -> zstd_decompress_buffer
This commit renames zstd_decompress_stream() to zstd_decompress_buffer() to free up the old name for an actual streaming decompression function.
Quoted 4 lines
tools/perf/util/tool.c +0
-0
[ ... ]
tools/perf/util/tool.c static int perf_session__process_compressed_event(const struct perf_tool *tool _ · line 99
Quoted 9 lines
tools/perf/util/tool.c +0
-0@@ -92,15 +92,15 @@ static int perf_session__process_compressed_event(const struct perf_tool *tool _ src = (void *)event +sizeof (struct perf_record_compressed2 ); src_size = event->pack2.data_size;/* * 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(). */
[Severity: Low]
Could this comment in perf_session__process_compressed_event() be updated to reference the renamed zstd_decompress_buffer() function?
Since the commit message indicates the old name will be reused for a different function, leaving this stale comment could be confusing when the new function is introduced.
Quoted 9 lines
tools/perf/util/tool.c +1
-1if (src_size > event->header.size -sizeof (struct perf_record_compressed2 )) goto err_decomp; }else { 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);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH 2/4 perf: zstd: factor out zstd_init_{c,d}stream()
In reply to Ivan ShapovalovFactor out functions to manage CStream/DStream in struct zstd_data for reuse across multiple zstd (de)compression entry points.
--- tools/perf/util/zstd.c | 82 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 52 insertions(+), 30 deletions(-)
tools/perf/util/zstd.c +52 -30
@@ -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;
Signature · 1 line
2.55.0.8.g37e2a0019f
[email protected]
AI reviewRe: [PATCH 2/4] perf: zstd: factor out zstd_init_{c,d}stream()
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] perf: zstd_decompress_buffer() returns SIZE_MAX instead of 0 on error --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] perf: zstd_decompress_buffer() returns SIZE_MAX instead of 0 on error --
commit 1d761c8689bb9c7209c96548d100b2308a92f2a5
Author: Ivan Shapovalov <[email protected]>
perf: zstd: factor out zstd_init_{c,d}stream()
Factor out functions to manage CStream/DStream in struct zstd_data for reuse across multiple zstd (de)compression entry points.
Quoted 4 lines
tools/perf/util/zstd.c +0
-0
[ ... ]
Quoted 8 lines
tools/perf/util/zstd.c +1
-1@@ -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 };
tools/perf/util/zstd.c ssize_t zstd_compress_stream_to_records(struct zstd_data *data, void *dst, size_ · line 148
Quoted 10 lines
tools/perf/util/zstd.c +3
-6-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;
[Severity: High]
Since zstd_decompress_buffer() returns a size_t, will returning a negative error code here implicitly cast -1 to SIZE_MAX?
It looks like callers such as perf_session__process_compressed_event() check for a 0 return to detect failure. If zstd_init_dstream() fails, wouldn't returning SIZE_MAX cause the caller to treat it as a huge success, potentially leading to massive out-of-bounds size increments?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH 3/4 perf: zstd: add streaming decompression and format probe functions
In reply to Ivan ShapovalovAdd 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.
--- tools/perf/util/compress.h | 18 ++++++++ tools/perf/util/zstd.c | 104 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+)
tools/perf/util/compress.h +18 -0
@@ -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 */
tools/perf/util/zstd.c +104 -0
114 lines not on this page · raw message
Signature · 1 line
2.55.0.8.g37e2a0019f
[email protected]
AI reviewRe: [PATCH 3/4] perf: zstd: add streaming decompression and format probe functions
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] perf tools: zstd_is_compressed() returns true on file open failure - [Low] perf: missing…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] perf tools: zstd_is_compressed() returns true on file open failure
- [Low] perf: missing explicit <stdint.h> and <endian.h> for musl compatibility --
commit a4c94e50df0d36cb48138075042eb1c0c1527f18
Author: Ivan Shapovalov <[email protected]>
perf: zstd: add streaming decompression and format probe functions
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.Quoted 4 lines
tools/perf/util/zstd.c +0
-0
[ ... ]
Quoted 1 line
tools/perf/util/zstd.c +0
-0@@ -172,3 +176,103 @@ size_t zstd_decompress_buffer(struct zstd_data *data, void *src, size_t src_size
[ ... ]
tools/perf/util/zstd.c size_t zstd_decompress_buffer(struct zstd_data *data, void *src, size_t src_size · line 265
Quoted 4 lines
tools/perf/util/zstd.c +4
-0+bool zstd_is_compressed (const char *file) +{ +int fd =open (file,O_RDONLY ); +const uint32_t magic_le =htole32 (ZSTD_MAGICNUMBER );
[Severity: Low]
Does this code need explicit inclusions for <stdint.h> and <endian.h>?
It appears compiling perf tools with musl libc could result in a build failure here, since musl enforces strict POSIX header namespace separation and does not leak uint32_t and htole32 through other standard library headers.
tools/perf/util/zstd.c size_t zstd_decompress_buffer(struct zstd_data *data, void *src, size_t src_size · line 271
Quoted 6 lines
tools/perf/util/zstd.c +6
-0+char buf[4 ]; +ssize_t rc; + +if (fd <0 ) { +pr_err ("zstd: failed to open(%s): %s\n" , file,strerror (errno)); +return -1 ;
[Severity: Medium]
Since zstd_is_compressed() returns a bool, does returning -1 here implicitly evaluate to true?
If open() fails, returning true could cause callers probing file formats to erroneously identify missing or unreadable files as valid ZSTD-compressed archives. This could break fallback logic and lead to downstream decompression attempts on missing files.
Quoted 7 lines
tools/perf/util/zstd.c +7
-0+ } + + rc =read (fd, buf,sizeof (buf)); +close (fd); +return rc ==sizeof (buf) ? +memcmp (buf, (const char *) &magic_le,sizeof (buf)) ==0 : false; +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
PATCH 4/4 perf: support zstd-compressed kernel modules
In reply to Ivan ShapovalovSupport 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.
--- tools/perf/Makefile.config | 2 +- tools/perf/util/dso.c | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-)
tools/perf/Makefile.config +1 -1
@@ -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
tools/perf/util/dso.c +4 -1
@@ -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)
Signature · 1 line
2.55.0.8.g37e2a0019f
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] perf: Incorrect pointer arithmetic breaks gzip/xz module detection --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] perf: Incorrect pointer arithmetic breaks gzip/xz module detection --
commit db2ed0262b65a0b2dfef65f9c73099d856ec9244
Author: Ivan Shapovalov <[email protected]>
perf: support zstd-compressed kernel modules
This commit adds support for handling zstd-compressed DSOs in perf. It allows perf to load zstd-compressed kernel modules by identifying the .zst extension.
Quoted 4 lines
tools/perf/util/dso.c +0
-0
[ ... ]
tools/perf/util/dso.c int __kmod_path__parse(struct kmod_path *m, const char *path, · line 466
Quoted 6 lines
tools/perf/util/dso.c +1
-1@@ -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);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[Severity: High]
Does this pointer arithmetic break the detection of gzip and xz compressed modules?
When parsing a compressed module path like foo.ko.gz, ext initially points to the last dot (for example, the dot before gz).
To check for the .ko extension, the code must shift the pointer backward by 3 bytes to point to the dot in .ko. The original code subtracted 3 because the string .ko is exactly 3 bytes long.
By subtracting the length of the compression format string instead, this now subtracts 2 for gz and xz formats. This shifts the pointer to the character o in .ko rather than the dot.
The subsequent check in __kmod_path__parse():
if (ext > name) m->kmod = !strncmp(ext, ".ko", 3);
will then compare o.g or o.x to .ko, which fails. This causes perf to silently ignore .ko.gz and .ko.xz files and incorrectly flag them as not being kernel modules.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
kernel test robot
build botkernel test robot noticed "perf-sanity-tests.kmod_path_parse.fail" on:
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/[email protected]/
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 <[email protected]>
| Closes: https://lore.kernel.org/oe-lkp/[email protected]
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/[email protected]
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Namhyung Kim
maintainerHello,
Quoted 4 lines · 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.cto 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
Quoted 23 lines
Signed-off-by Ivan Shapovalov <[email protected]>---
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 modulestools/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-279017105eb3Best regards,
Signature · 1 line
Ivan Shapovalov <[email protected]>
Quoted 1 line · Namhyung Kim
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
--- 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(-)
tools/perf/Makefile.perf +2 -2
@@ -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. #
tools/perf/tests/kmod-path.c +21 -0
@@ -96,6 +96,27 @@ static int test__kmod_path__parse(struct test_suite *t __maybe_unused, int subteM ("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 );
tools/perf/tests/shell/buildid.sh +40 -0
@@ -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}
tools/perf/util/compress.h +6 -3
@@ -92,17 +92,20 @@ static inline size_t zstd_decompress_buffer(struct zstd_data *data __maybe_unusereturn 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; }
tools/perf/util/dso.c +1 -1
@@ -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)
tools/perf/util/tool.c +1 -1
@@ -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;
tools/perf/util/zstd.c +18 -13
@@ -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_sizereturn 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; }