From e033e3b751fc907c2530edc14f475e2a5d114d5d Mon Sep 17 00:00:00 2001 From: Shrushti P K Date: Tue, 17 Nov 2026 19:49:13 +0530 Subject: [PATCH] telemetry: performance_monitor: cap extended perf data reply get_extended_performance_data() writes one 48-byte ext_perf_data_item for every enabled core and every occupied performance slot, and never looks at the size of the buffer it writes to. The IPC4 BaseFW LARGE_CONFIG_GET for IPC4_EXTENDED_GLOBAL_PERF_DATA hands it ipc->comp_data, a SOF_IPC_MSG_MAX_SIZE (4096 byte) heap block, which holds 85 items after the count word. A slot is taken for every component the host creates, and while measurements are started it stays taken after the component is freed. CONFIG_SOF_TELEMETRY_MAX_COMP_ENTRIES defaults to 88 (170 without SOF_TELEMETRY_MODULE_RESOURCES), so 85 components on one core are enough for the report to run past the end of comp_data: up to 372 bytes with 5 cores and 88 slots, about 4 KiB with 170 slots. The reply size check in ipc4_get_large_config_module_instance() runs only after the getter has written. Pass the buffer size to get_extended_performance_data() and report only the items that fit, with a warning. Reports that fit are unchanged. The 16-byte global and I/O perf reports cannot exceed the buffer with the configured slot counts. --- .../sof/debug/telemetry/performance_monitor.h | 7 +++++-- src/kernel/audio/base_fw.c | 2 +- .../debug/telemetry/performance_monitor.c | 18 ++++++++++++++++-- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/include/sof/debug/telemetry/performance_monitor.h b/src/include/sof/debug/telemetry/performance_monitor.h index fe081c8bd4ef..7b9e24772794 100644 --- a/src/include/sof/debug/telemetry/performance_monitor.h +++ b/src/include/sof/debug/telemetry/performance_monitor.h @@ -121,9 +121,11 @@ int get_performance_data(struct global_perf_data * const global_perf_data); * \brief Retrieve extended global performance telemetry data with per-core breakdown. * \ingroup telemetry_perf_api * @param[out] ext_global_perf_data Destination structure to populate. + * @param[in] size Size of the destination buffer in bytes, items beyond it are not reported. * @return 0 on success, or negative error code on failure. */ -int get_extended_performance_data(struct extended_global_perf_data * const ext_global_perf_data); +int get_extended_performance_data(struct extended_global_perf_data * const ext_global_perf_data, + uint32_t size); /** * \brief Reset all performance measurement counters across all components. @@ -176,7 +178,8 @@ static inline int get_performance_data(struct global_perf_data * const global_pe } static inline -int get_extended_performance_data(struct extended_global_perf_data * const ext_global_perf_data) +int get_extended_performance_data(struct extended_global_perf_data * const ext_global_perf_data, + uint32_t size) { return 0; } diff --git a/src/kernel/audio/base_fw.c b/src/kernel/audio/base_fw.c index 880306a4a03b..136755296c09 100644 --- a/src/kernel/audio/base_fw.c +++ b/src/kernel/audio/base_fw.c @@ -697,7 +697,7 @@ __cold static int extended_global_perf_data_get(uint32_t *data_off_size, char *d int ret; struct extended_global_perf_data *perf_data = (struct extended_global_perf_data *)data; - ret = get_extended_performance_data(perf_data); + ret = get_extended_performance_data(perf_data, SOF_IPC_MSG_MAX_SIZE); if (ret < 0) return IPC4_ERROR_INVALID_PARAM; *data_off_size = sizeof(*perf_data) diff --git a/src/kernel/debug/telemetry/performance_monitor.c b/src/kernel/debug/telemetry/performance_monitor.c index 65adaba88bdb..fb598f986bcf 100644 --- a/src/kernel/debug/telemetry/performance_monitor.c +++ b/src/kernel/debug/telemetry/performance_monitor.c @@ -400,7 +400,8 @@ __cold int get_performance_data(struct global_perf_data * const global_perf_data } __cold int -get_extended_performance_data(struct extended_global_perf_data * const ext_global_perf_data) +get_extended_performance_data(struct extended_global_perf_data * const ext_global_perf_data, + uint32_t size) { assert_can_be_cold(); @@ -409,6 +410,14 @@ get_extended_performance_data(struct extended_global_perf_data * const ext_globa return -EINVAL; } + if (size < sizeof(*ext_global_perf_data)) { + tr_err(&ipc_tr, "IPC data size %u too small", size); + return -EINVAL; + } + + /* the buffer cannot hold an item for every core and every perf slot */ + const size_t max_slots = + (size - sizeof(*ext_global_perf_data)) / sizeof(struct ext_perf_data_item); size_t slots_count; size_t slot_idx = 0; uint64_t total_dsp_cycles[CONFIG_MAX_CORE_COUNT]; @@ -421,7 +430,7 @@ get_extended_performance_data(struct extended_global_perf_data * const ext_globa total_dsp_cycles[core_id] = 1; /* Fill one performance record per core with total dsp cycles */ - for (int core_id = 0; core_id < CONFIG_MAX_CORE_COUNT; ++core_id) { + for (int core_id = 0; core_id < CONFIG_MAX_CORE_COUNT && slot_idx < max_slots; ++core_id) { if (!(cpu_enabled_cores() & BIT(core_id))) continue; @@ -435,6 +444,11 @@ get_extended_performance_data(struct extended_global_perf_data * const ext_globa } slots_count = perf_bitmap_get_occupied(&performance_data_bitmap) + slot_idx; + if (slots_count > max_slots) { + tr_warn(&ipc_tr, "%zu perf items do not fit, reporting %zu", slots_count, + max_slots); + slots_count = max_slots; + } ext_global_perf_data->perf_item_count = slots_count; /* fill the rest of the IPC records with data from