Re: [PATCH v15 17/34] fs/resctrl: Add the functionality to assign MBM events

From: Reinette Chatre
Date: Thu Jul 17 2025 - 23:47:58 EST


Hi Babu,

On 7/8/25 3:17 PM, Babu Moger wrote:
> When supported, "mbm_event" counter assignment mode offers "num_mbm_cntrs"
> number of counters that can be assigned to RMID, event pairs and monitor
> bandwidth usage as long as it is assigned.
>
> Add the functionality to allocate and assign a counter to an RMID, event
> pair in the domain.
>
> If all the counters are in use, kernel will log the error message "Unable
> to allocate counter in domain" in /sys/fs/resctrl/info/last_cmd_status
> when a new assignment is requested. Exit on the first failure when
> assigning counters across all the domains.
>
> Signed-off-by: Babu Moger <babu.moger@xxxxxxx>
> ---

...

> ---
> fs/resctrl/internal.h | 3 +
> fs/resctrl/monitor.c | 131 ++++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 134 insertions(+)
>
> diff --git a/fs/resctrl/internal.h b/fs/resctrl/internal.h
> index ea5c9fa932aa..8879e127a8b8 100644
> --- a/fs/resctrl/internal.h
> +++ b/fs/resctrl/internal.h
> @@ -387,6 +387,9 @@ bool closid_allocated(unsigned int closid);
>
> int resctrl_find_cleanest_closid(void);
>
> +int rdtgroup_assign_cntr_event(struct rdt_mon_domain *d, struct rdtgroup *rdtgrp,
> + struct mon_evt *mevt);
> +
> #ifdef CONFIG_RESCTRL_FS_PSEUDO_LOCK
> int rdtgroup_locksetup_enter(struct rdtgroup *rdtgrp);
>
> diff --git a/fs/resctrl/monitor.c b/fs/resctrl/monitor.c
> index 11327bd8cf72..bb074773420d 100644
> --- a/fs/resctrl/monitor.c
> +++ b/fs/resctrl/monitor.c
> @@ -952,3 +952,134 @@ void resctrl_mon_resource_exit(void)
>
> dom_data_exit(r);
> }
> +
> +/*
> + * resctrl_config_cntr() - Configure the counter ID for the event, RMID pair in
> + * the domain.
> + *
> + * Assign the counter if @assign is true else unassign the counter. Reset the
> + * associated non-architectural state.

Is this API only for assignment? It seems so. Looks like resctrl_config_cntr() is used
for assign/unassign while rdtgroup_update_cntr_event()/resctrl_update_cntr_allrdtgrp() is
for re-configure.

I think this will be easier to understand if the function names and comments match this
usage. To help make clear when a counter is assigned/unassigned or when an assigned
counter is re-configured.

resctrl_config_cntr() can be renamed to rdtgroup_assign_cntr() with its description
matching what it does: (a) it does not reconfigure a counter but assign/unassign it,
and (b) it operates on the rdtgroup.
It is only the underlying arch API that uses "configure" for assign, unassign, as
well as configure.


> + */
> +static void resctrl_config_cntr(struct rdt_resource *r, struct rdt_mon_domain *d,
> + enum resctrl_event_id evtid, u32 rmid, u32 closid,
> + u32 cntr_id, bool assign)
> +{
> + struct mbm_state *m;
> +
> + resctrl_arch_config_cntr(r, d, evtid, rmid, closid, cntr_id, assign);
> +
> + m = get_mbm_state(d, closid, rmid, evtid);
> + if (m)
> + memset(m, 0, sizeof(*m));
> +}
> +
> +/*
> + * mbm_cntr_get() - Return the counter ID for the matching @evtid and @rdtgrp.
> + *
> + * Return:
> + * Valid counter ID on success, or -ENOENT on failure.
> + */
> +static int mbm_cntr_get(struct rdt_resource *r, struct rdt_mon_domain *d,
> + struct rdtgroup *rdtgrp, enum resctrl_event_id evtid)
> +{
> + int cntr_id;
> +
> + if (!r->mon.mbm_cntr_assignable)
> + return -ENOENT;
> +
> + if (!resctrl_is_mbm_event(evtid))
> + return -ENOENT;
> +
> + for (cntr_id = 0; cntr_id < r->mon.num_mbm_cntrs; cntr_id++) {
> + if (d->cntr_cfg[cntr_id].rdtgrp == rdtgrp &&
> + d->cntr_cfg[cntr_id].evtid == evtid)
> + return cntr_id;
> + }
> +
> + return -ENOENT;
> +}
> +
> +/*
> + * mbm_cntr_alloc() - Initialize and return a new counter ID in the domain @d.
> + * Caller must ensure that the specified event is not assigned already.
> + *
> + * Return:
> + * Valid counter ID on success, or -ENOSPC on failure.
> + */
> +static int mbm_cntr_alloc(struct rdt_resource *r, struct rdt_mon_domain *d,
> + struct rdtgroup *rdtgrp, enum resctrl_event_id evtid)
> +{
> + int cntr_id;
> +
> + for (cntr_id = 0; cntr_id < r->mon.num_mbm_cntrs; cntr_id++) {
> + if (!d->cntr_cfg[cntr_id].rdtgrp) {
> + d->cntr_cfg[cntr_id].rdtgrp = rdtgrp;
> + d->cntr_cfg[cntr_id].evtid = evtid;
> + return cntr_id;
> + }
> + }
> +
> + return -ENOSPC;
> +}
> +
> +/*
> + * rdtgroup_alloc_config_cntr() - Allocate a counter ID and configure it for the
> + * event pointed to by @mevt and the resctrl group @rdtgrp within the domain @d.
> + *
> + * Return:
> + * 0 on success, < 0 on failure.
> + */
> +static int rdtgroup_alloc_config_cntr(struct rdt_resource *r, struct rdt_mon_domain *d,
> + struct rdtgroup *rdtgrp, enum resctrl_event_id evtid)

How about rdtgroup_alloc_config_cntr() -> rdtgroup_alloc_assign_cntr()?

> +{
> + int cntr_id;
> +
> + /* No action required if the counter is assigned already. */
> + cntr_id = mbm_cntr_get(r, d, rdtgrp, evtid);
> + if (cntr_id >= 0)
> + return 0;
> +
> + cntr_id = mbm_cntr_alloc(r, d, rdtgrp, evtid);
> + if (cntr_id < 0) {
> + rdt_last_cmd_printf("Unable to allocate counter in domain %d\n",
> + d->hdr.id);
> + return cntr_id;
> + }
> +
> + resctrl_config_cntr(r, d, evtid, rdtgrp->mon.rmid, rdtgrp->closid,
> + cntr_id, true);
> +
> + return 0;
> +}
> +
> +/*
> + * rdtgroup_assign_cntr_event() - Assign a hardware counter for the event in
> + * @mevt to the resctrl group @rdtgrp. Assign counters to all domains if @d is
> + * NULL; otherwise, assign the counter to the specified domain @d.
> + *
> + * If all counters in a domain are already in use, resctrl_alloc_config_cntr()

resctrl_alloc_config_cntr() needs update to match function name

> + * will fail. The assignment process will abort at the first failure encountered
> + * during domain traversal, which may result in the event being only partially
> + * assigned.
> + *
> + * Return:
> + * 0 on success, < 0 on failure.
> + */
> +int rdtgroup_assign_cntr_event(struct rdt_mon_domain *d, struct rdtgroup *rdtgrp,
> + struct mon_evt *mevt)
> +{
> + struct rdt_resource *r = resctrl_arch_get_resource(mevt->rid);
> + int ret = 0;
> +
> + if (!d) {
> + list_for_each_entry(d, &r->mon_domains, hdr.list) {
> + ret = rdtgroup_alloc_config_cntr(r, d, rdtgrp, mevt->evtid);
> + if (ret)
> + return ret;
> + }
> + } else {
> + ret = rdtgroup_alloc_config_cntr(r, d, rdtgrp, mevt->evtid);
> + }
> +
> + return ret;
> +}

Reinette