Re: [PATCH 2/3] selftests/resctrl: Replace array-based IMC counter management with linked lists

From: Reinette Chatre

Date: Thu Apr 02 2026 - 13:49:33 EST


Hi Yifan,

On 3/24/26 5:50 AM, Yifan Wu wrote:
> Convert IMC counter management from static array to dynamic
> linked list allocation.

Could you please split this patch into two? One patch where utilities
receive pointer to array element instead of index as parameter and
another patch that switches the code to use a list?

>
> Signed-off-by: Yifan Wu <wuyifan50@xxxxxxxxxx>
> ---
> tools/testing/selftests/resctrl/resctrl_val.c | 134 +++++++++---------
> 1 file changed, 66 insertions(+), 68 deletions(-)
>
> diff --git a/tools/testing/selftests/resctrl/resctrl_val.c b/tools/testing/selftests/resctrl/resctrl_val.c
> index ac58d3862281..417d87ba368a 100644
> --- a/tools/testing/selftests/resctrl/resctrl_val.c
> +++ b/tools/testing/selftests/resctrl/resctrl_val.c
> @@ -14,7 +14,6 @@
> #define READ_FILE_NAME "cas_count_read"
> #define DYN_PMU_PATH "/sys/bus/event_source/devices"
> #define SCALE 0.00006103515625
> -#define MAX_IMCS 40
> #define MAX_TOKENS 5
>
> #define CON_MBM_LOCAL_BYTES_PATH \
> @@ -38,36 +37,37 @@ struct imc_counter_config {
>
> static char mbm_total_path[1024];
> static int imcs;
> -static struct imc_counter_config imc_counters_config[MAX_IMCS];
> LIST_HEAD(imc_counters_configs);
> static const struct resctrl_test *current_test;
>
> -static void read_mem_bw_initialize_perf_event_attr(int i)
> +static void read_mem_bw_initialize_perf_event_attr(struct imc_counter_config *imc_counters_config)

In parameters also, please use a variable name used for element that is further
away from list header name. How about just "imc_counter" for the function parameter?

...

> @@ -112,10 +113,10 @@ static int open_perf_read_event(int i, int cpu_no)
> }
>
> static int parse_imc_read_bw_events(char *imc_dir, unsigned int type,
> - unsigned int *count)
> + struct imc_counter_config *imc_counters_config)
> {
> char imc_events_dir[PATH_MAX], imc_counter_cfg[PATH_MAX];
> - unsigned int orig_count = *count;
> + unsigned int orig_count = imcs;

Why is global imcs used/needed here? The intention behind orig_count is just to
check if any iMC counters were added by this function. Original code checked
by comparing the "before" and "after" array index but with a switch to a list this
can just be done locally, for example, with a boolean.

> char cas_count_cfg[1024];
> struct dirent *ep;
> int path_len;
> @@ -165,17 +166,13 @@ static int parse_imc_read_bw_events(char *imc_dir, unsigned int type,
> ksft_perror("Could not get iMC cas count read");
> goto out_close;
> }
> - if (*count >= MAX_IMCS) {
> - ksft_print_msg("Maximum iMC count exceeded\n");
> - goto out_close;
> - }
>
> - imc_counters_config[*count].type = type;
> - get_read_event_and_umask(cas_count_cfg, *count);
> - /* Do not fail after incrementing *count. */
> - *count += 1;
> + imc_counters_config->type = type;
> + get_read_event_and_umask(cas_count_cfg, imc_counters_config);
> + /* Do not fail after incrementing count. */
> + imcs++;

Note that this is a loop that may initialize more than one counter and since it
uses the single element provided as function parameter each new counter will just
overwrite the previous's settings.

As mentioned in patch #1 it looks more appropriate to allocate and initialize
new list entry here within parse_imc_read_bw_events().

> @@ -239,7 +236,7 @@ static int num_of_imcs(void)
> {
> struct imc_counter_config *imc_counters_config;
> char imc_dir[512], *temp;
> - unsigned int count = 0;
> + imcs = 0;
> struct dirent *ep;
> int ret;
> DIR *dp;
> @@ -275,7 +272,7 @@ static int num_of_imcs(void)
> memset(imc_counters_config, 0, sizeof(struct imc_counter_config));
> sprintf(imc_dir, "%s/%s/", DYN_PMU_PATH,
> ep->d_name);
> - ret = read_from_imc_dir(imc_dir, &count);
> + ret = read_from_imc_dir(imc_dir, imc_counters_config);
> if (ret) {
> free(imc_counters_config);
> closedir(dp);
> @@ -286,7 +283,7 @@ static int num_of_imcs(void)
> }
> }
> closedir(dp);
> - if (count == 0) {
> + if (imcs == 0) {

Is this global necessary? How about list_empty() instead?

> ksft_print_msg("Unable to find iMC counters\n");
>
> return -1;
> @@ -297,20 +294,22 @@ static int num_of_imcs(void)
> return -1;
> }
>
> - return count;
> + return imcs;

Looking at how the caller, initialize_read_mem_bw_imc() below, uses the return
value it does not seem necessary to track the number of entries anymore. Could the
global imcs just be dropped?

> }
>
> int initialize_read_mem_bw_imc(void)
> {
> - int imc;
> + int ret;
> + struct imc_counter_config *imc_counters_config;
>
> - imcs = num_of_imcs();
> - if (imcs <= 0)
> - return imcs;
> + ret = num_of_imcs();
> + if (ret <= 0)
> + return ret;
>
> /* Initialize perf_event_attr structures for all iMC's */
> - for (imc = 0; imc < imcs; imc++)
> - read_mem_bw_initialize_perf_event_attr(imc);
> + list_for_each_entry(imc_counters_config, &imc_counters_configs, imc_list) {
> + read_mem_bw_initialize_perf_event_attr(imc_counters_config);
> + }
>
> return 0;
> }
Reinette