Skip to content

Commit 6bb19f4

Browse files
Shrusti-pklgirdwood
authored andcommitted
telemetry: performance_monitor: bound 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. Through a VENDOR_CONFIG_PARAM get the same getter is reached with data pointing after earlier TLV replies inside comp_data, so the whole buffer size is not a safe bound there. Pass the buffer size to get_extended_performance_data() and refuse the request with -ENOBUFS, reported as IPC4_OUT_OF_MEMORY, when the items do not all fit, instead of returning a report the host cannot tell apart from a complete one. The size is the in/out data_off_size, the host max reply length that the vendor config path narrows to the room left after its TLV header, capped at SOF_IPC_MSG_MAX_SIZE. Reports that fit are unchanged. The 16-byte global and I/O perf reports cannot exceed the buffer with the configured slot counts. Signed-off-by: Shrushti P K <shrusthi@labs.digiscrypt.com>
1 parent d3bceb8 commit 6bb19f4

3 files changed

Lines changed: 33 additions & 5 deletions

File tree

‎src/include/sof/debug/telemetry/performance_monitor.h‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,12 @@ int get_performance_data(struct global_perf_data * const global_perf_data);
121121
* \brief Retrieve extended global performance telemetry data with per-core breakdown.
122122
* \ingroup telemetry_perf_api
123123
* @param[out] ext_global_perf_data Destination structure to populate.
124-
* @return 0 on success, or negative error code on failure.
124+
* @param[in] size Size of the destination buffer in bytes.
125+
* @return 0 on success, -ENOBUFS when the report does not fit in size bytes, or another
126+
* negative error code on failure.
125127
*/
126-
int get_extended_performance_data(struct extended_global_perf_data * const ext_global_perf_data);
128+
int get_extended_performance_data(struct extended_global_perf_data * const ext_global_perf_data,
129+
uint32_t size);
127130

128131
/**
129132
* \brief Reset all performance measurement counters across all components.
@@ -176,7 +179,8 @@ static inline int get_performance_data(struct global_perf_data * const global_pe
176179
}
177180

178181
static inline
179-
int get_extended_performance_data(struct extended_global_perf_data * const ext_global_perf_data)
182+
int get_extended_performance_data(struct extended_global_perf_data * const ext_global_perf_data,
183+
uint32_t size)
180184
{
181185
return 0;
182186
}

‎src/kernel/audio/base_fw.c‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Copyright(c) 2024 Intel Corporation.
44
//
55

6+
#include <errno.h>
67
#include <stdint.h>
78

89
#include <sof/audio/component.h>
@@ -20,6 +21,7 @@
2021
#include <sof/lib/cpu.h>
2122
#include <sof/lib/mailbox.h>
2223
#include <sof/lib/memory.h>
24+
#include <sof/math/numbers.h>
2325
#include <sof/kernel/platform.h>
2426
#include <sof/lib_manager.h>
2527
#include <rtos/clk.h>
@@ -739,8 +741,15 @@ __cold static int extended_global_perf_data_get(uint32_t *data_off_size, char *d
739741
#ifdef CONFIG_SOF_TELEMETRY_PERFORMANCE_MEASUREMENTS
740742
int ret;
741743
struct extended_global_perf_data *perf_data = (struct extended_global_perf_data *)data;
744+
/* data_off_size is in/out: the host max reply length comes in, the
745+
* vendor config path sets it to what is left of the reply buffer, and
746+
* neither may exceed the IPC buffer data points into.
747+
*/
748+
uint32_t size = MIN(*data_off_size, SOF_IPC_MSG_MAX_SIZE);
742749

743-
ret = get_extended_performance_data(perf_data);
750+
ret = get_extended_performance_data(perf_data, size);
751+
if (ret == -ENOBUFS)
752+
return IPC4_OUT_OF_MEMORY;
744753
if (ret < 0)
745754
return IPC4_ERROR_INVALID_PARAM;
746755
*data_off_size = sizeof(*perf_data)

‎src/kernel/debug/telemetry/performance_monitor.c‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,8 @@ __cold int get_performance_data(struct global_perf_data * const global_perf_data
400400
}
401401

402402
__cold int
403-
get_extended_performance_data(struct extended_global_perf_data * const ext_global_perf_data)
403+
get_extended_performance_data(struct extended_global_perf_data * const ext_global_perf_data,
404+
uint32_t size)
404405
{
405406
assert_can_be_cold();
406407

@@ -413,6 +414,20 @@ get_extended_performance_data(struct extended_global_perf_data * const ext_globa
413414
size_t slot_idx = 0;
414415
uint64_t total_dsp_cycles[CONFIG_MAX_CORE_COUNT];
415416

417+
/* One item per enabled core plus one per occupied perf slot. Refuse the
418+
* request when they do not all fit rather than return a report with
419+
* items silently dropped, the host cannot tell a short report apart
420+
* from a complete one.
421+
*/
422+
slots_count = popcount(cpu_enabled_cores()) +
423+
perf_bitmap_get_occupied(&performance_data_bitmap);
424+
if (size < sizeof(*ext_global_perf_data) ||
425+
slots_count > (size - sizeof(*ext_global_perf_data)) /
426+
sizeof(struct ext_perf_data_item)) {
427+
tr_err(&ipc_tr, "%zu perf items do not fit in %u bytes", slots_count, size);
428+
return -ENOBUFS;
429+
}
430+
416431
/* TODO
417432
* Setting temporary values here.
418433
* Replace this with actual total dsp cycles info once it is available.

0 commit comments

Comments
 (0)