Skip to contentSkip to conversation

x86/resctrl : Support AMD Assignable Bandwidth Monitoring Counters (ABMC)

PATCH Superseded Babu Moger 7 participants 117 messages Oct 29, 2024 – Feb 3, 2025 Page 2 of 2

Subsystems (3)

v9 of 18 Superseded Latest is v18 →

lore
Share & tools
lore
#85 lore raw

Re: [PATCH v9 18/26] x86/resctrl: Add the interface to assign/update counter assignment

In reply to Babu Moger

Hi, Babu,

Quoted 110 lines · Babu Moger

The mbm_cntr_assign mode offers several hardware counters that can be assigned to an RMID, event pair and monitor the bandwidth as long as it is assigned.

Counters are managed at two levels. The global assignment is tracked using the mbm_cntr_free_map field in the struct resctrl_mon, while domain-specific assignments are tracked using the mbm_cntr_map field in the struct rdt_mon_domain. Allocation begins at the global level and is then applied individually to each domain.

Introduce an interface to allocate these counters and update the corresponding domains accordingly.

---
v9: Introduced new function resctrl_config_cntr to assign the counter, update
the bitmap and reset the architectural state.
Taken care of error handling(freeing the counter) when assignment fails. Moved mbm_cntr_assigned_to_domain here as it used in this patch. Minor text changes.

v8: Renamed rdtgroup_assign_cntr() to rdtgroup_assign_cntr_event().
Added the code to return the error if rdtgroup_assign_cntr_event fails. Moved definition of MBM_EVENT_ARRAY_INDEX to resctrl/internal.h. Updated typo in the comments.

v7: New patch. Moved all the FS code here.
Merged rdtgroup_assign_cntr and rdtgroup_alloc_cntr. Adde new #define MBM_EVENT_ARRAY_INDEX.

---
  arch/x86/kernel/cpu/resctrl/internal.h |  2 +
  arch/x86/kernel/cpu/resctrl/rdtgroup.c | 87 ++++++++++++++++++++++++++
  2 files changed, 89 insertions(+)
arch/x86/kernel/cpu/resctrl/internal.h +2 -0
index 00f7bf60e16a..cb496bd97007 100644
@@ -717,6 +717,8 @@ unsigned int mon_event_config_index_get(u32 evtid);
  int resctrl_arch_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);
+int rdtgroup_assign_cntr_event(struct rdt_resource *r, struct rdtgroup *rdtgrp,
+			       struct rdt_mon_domain *d, enum resctrl_event_id evtid);
  void rdt_staged_configs_clear(void);
  bool closid_allocated(unsigned int closid);
  int resctrl_find_cleanest_closid(void);
arch/x86/kernel/cpu/resctrl/rdtgroup.c +55 -0
index 1b5529c212f5..bc3752967c44 100644
@@ -1924,6 +1924,93 @@ int resctrl_arch_config_cntr(struct rdt_resource *r, struct rdt_mon_domain *d,
  	return 0;
  }
 
+/*
+ * Configure the counter for the event, RMID pair for the domain.
+ * Update the bitmap and reset the architectural state.
+ */
+static int 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)
+{
+	int ret;
+
+	ret = resctrl_arch_config_cntr(r, d, evtid, rmid, closid, cntr_id, assign);
+	if (ret)
+		return ret;
+
+	if (assign)
+		__set_bit(cntr_id, d->mbm_cntr_map);
+	else
+		__clear_bit(cntr_id, d->mbm_cntr_map);
+
+	/*
+	 * Reset the architectural state so that reading of hardware
+	 * counter is not considered as an overflow in next update.
+	 */
+	resctrl_arch_reset_rmid(r, d, closid, rmid, evtid);
+
+	return ret;
+}
+
+static bool mbm_cntr_assigned_to_domain(struct rdt_resource *r, u32 cntr_id)
+{
+	struct rdt_mon_domain *d;
+
+	list_for_each_entry(d, &r->mon_domains, hdr.list)
+		if (test_bit(cntr_id, d->mbm_cntr_map))
+			return 1;
+
+	return 0;
+}
+
+/*
+ * Assign a hardware counter to event @evtid of group @rdtgrp.
+ * Counter will be assigned to all the domains if rdt_mon_domain is NULL
+ * else the counter will be assigned to specific domain.
+ */
+int rdtgroup_assign_cntr_event(struct rdt_resource *r, struct rdtgroup *rdtgrp,
+			       struct rdt_mon_domain *d, enum resctrl_event_id evtid)
+{
+	int index = MBM_EVENT_ARRAY_INDEX(evtid);
+	int cntr_id = rdtgrp->mon.cntr_id[index];
+	int ret;
+
+	/*
+	 * Allocate a new counter id to the event if the counter is not
+	 * assigned already.
+	 */

arch/x86/kernel/cpu/resctrl/rdtgroup.c int resctrl_arch_config_cntr(struct rdt_resource *r, struct rdt_mon_domain *d, · line 1991

Quoted 10 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +10 -0
+	if (cntr_id == MON_CNTR_UNSET) {
+		cntr_id = mbm_cntr_alloc(r);
+		if (cntr_id < 0) {
+			rdt_last_cmd_puts("Out of MBM assignable counters\n");
+			return -ENOSPC;
+		}
+		rdtgrp->mon.cntr_id[index] = cntr_id;
+	}
+
+	if (!d) {

Should assert cpus are locked here?

        /* Walking r->domains, ensure it can't race with cpuhp */
        lockdep_assert_cpus_held();

Please see more comments on patch #20.

Quoted 15 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +15 -0
+		list_for_each_entry(d, &r->mon_domains, hdr.list) {
+			ret = resctrl_config_cntr(r, d, evtid, rdtgrp->mon.rmid,
+						  rdtgrp->closid, cntr_id, true);
+			if (ret)
+				goto out_done_assign;
+		}
+	} else {
+		ret = resctrl_config_cntr(r, d, evtid, rdtgrp->mon.rmid,
+					  rdtgrp->closid, cntr_id, true);
+		if (ret)
+			goto out_done_assign;
+	}
+
+out_done_assign:
+	if (ret && !mbm_cntr_assigned_to_domain(r, cntr_id)) {

arch/x86/kernel/cpu/resctrl/rdtgroup.c int resctrl_arch_config_cntr(struct rdt_resource *r, struct rdt_mon_domain *d, · line 2016

Quoted 10 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +7 -0
+		mbm_cntr_free(r, cntr_id);
+		rdtgroup_cntr_id_init(rdtgrp, evtid);
+	}
+
+	return ret;
+}
+
  /* rdtgroup information files for one cache resource. */
  static struct rftype res_common_files[] = {
  	{

Thanks.

-Fenghua

#97 lore raw

Re: [PATCH v9 20/26] x86/resctrl: Auto assign/unassign counters when mbm_cntr_assign is enabled

In reply to Babu Moger

Hi, Babu,

Quoted 58 lines · Babu Moger

Assign/unassign counters on resctrl group creation/deletion. Two counters are required per group, one for MBM total event and one for MBM local event.

There are a limited number of counters available for assignment. If these counters are exhausted, the kernel will display the error message: "Out of MBM assignable counters". However, it is not necessary to fail the creation of a group due to assignment failures. Users have the flexibility to modify the assignments at a later time.

---
v9: Changed rdtgroup_assign_cntrs() and rdtgroup_unassign_cntrs() to return void.
Updated couple of rdtgroup_unassign_cntrs() calls properly. Updated function comments.

v8: Renamed rdtgroup_assign_grp to rdtgroup_assign_cntrs.
Renamed rdtgroup_unassign_grp to rdtgroup_unassign_cntrs. Fixed the problem with unassigning the child MON groups of CTRL_MON group.

v7: Reworded the commit message.
Removed the reference of ABMC with mbm_cntr_assign. Renamed the function rdtgroup_assign_cntrs to rdtgroup_assign_grp.

v6: Removed the redundant comments on all the calls of
rdtgroup_assign_cntrs. Updated the commit message.
Dropped printing error message on every call of rdtgroup_assign_cntrs.

v5: Removed the code to enable/disable ABMC during the mount.
That will be another patch.
Added arch callers to get the arch specific data. Renamed fuctions to match the other abmc function. Added code comments for assignment failures.

v4: Few name changes based on the upstream discussion.
Commit message update.

v3: This is a new patch. Patch addresses the upstream comment to enable
ABMC feature by default if the feature is available.

---
  arch/x86/kernel/cpu/resctrl/rdtgroup.c | 61 +++++++++++++++++++++++++-
  1 file changed, 60 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/resctrl/rdtgroup.c +7 -0
index b0cce3dfd062..a8d21b0b2054 100644
@@ -2932,6 +2932,46 @@ static void schemata_list_destroy(void)
  	}
  }
 
+/*
+ * Called when a new group is created. If "mbm_cntr_assign" mode is enabled,
+ * counters are automatically assigned. Each group can accommodate two counters:
+ * one for the total event and one for the local event. Assignments may fail
+ * due to the limited number of counters. However, it is not necessary to fail
+ * the group creation and thus no failure is returned. Users have the option
+ * to modify the counter assignments after the group has been created.

arch/x86/kernel/cpu/resctrl/rdtgroup.c static void schemata_list_destroy(void) · line 2951

Quoted 10 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +10 -0
+ */
+static void rdtgroup_assign_cntrs(struct rdtgroup *rdtgrp)
+{
+	struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
+
+	if (!resctrl_arch_mbm_cntr_assign_enabled(r))
+		return;
+
+	if (is_mbm_total_enabled())
+		rdtgroup_assign_cntr_event(r, rdtgrp, NULL, QOS_L3_MBM_TOTAL_EVENT_ID);

In this code path, resctrl_mkdir()->resctrl_mkdir_ctrl_mon()->rdtgroup_assign_cntrs()->rdtgroup_assign_cntr_event()

CPUs are not protected by read lock while rdtgroup_assign_cntr_event() walks r->mon_domains and run assing counters code on CPUs in the domains. Without CPU protection, r->mon_domains may race with CPU hotplug.

In another patch (i.e. rdt_get_tree()), rdtgroup_assign_cntrs() is protected by cpus_read_lock()/unlock().

So maybe define two helpers:

// Called when caller takes cpus_read_lock()
rdtgroup_assign_cntrs_locked()

{
	lockdep_assert_cpus_held();

	then the current rdtgroup_assign_cntrs() code
}

// Called when caller doesn't take cpus_read_lock() rdtgroup_assign_cntrs()

{
	cpus_read_lock();
	rdtgroup_assign_cntrs_locked();
	cpus_read_unlock();
}
Quoted 13 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +13 -0
+
+	if (is_mbm_local_enabled())
+		rdtgroup_assign_cntr_event(r, rdtgrp, NULL, QOS_L3_MBM_LOCAL_EVENT_ID);
+}
+
+/*
+ * Called when a group is deleted. Counters are unassigned if it was in
+ * assigned state.
+ */
+static void rdtgroup_unassign_cntrs(struct rdtgroup *rdtgrp)
+{
+	struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
+

arch/x86/kernel/cpu/resctrl/rdtgroup.c static void schemata_list_destroy(void) · line 2974

Quoted 10 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +10 -0
+	if (!resctrl_arch_mbm_cntr_assign_enabled(r))
+		return;
+
+	if (is_mbm_total_enabled())
+		rdtgroup_unassign_cntr_event(r, rdtgrp, NULL, QOS_L3_MBM_TOTAL_EVENT_ID);
+
+	if (is_mbm_local_enabled())
+		rdtgroup_unassign_cntr_event(r, rdtgrp, NULL, QOS_L3_MBM_LOCAL_EVENT_ID);
+}
+

Seems rdtgroup_unassign_cntrs() is always protected by cpus_read_lock()/unlock(). So it's good.

arch/x86/kernel/cpu/resctrl/rdtgroup.c static int rdt_get_tree(struct fs_context *fc) · line 3035

Quoted 9 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +2 -0
  static int rdt_get_tree(struct fs_context *fc)
  {
  	struct rdt_fs_context *ctx = rdt_fc2context(fc);
@@ -2991,6 +3031,8 @@ static int rdt_get_tree(struct fs_context *fc)
  		if (ret < 0)
  			goto out_mongrp;
  		rdtgroup_default.mon.mon_data_kn = kn_mondata;
+
+		rdtgroup_assign_cntrs(&rdtgroup_default);

In this case, cpus_read_lock() was called earlier. Change to rdtgroup_assign_cntrs_locked().

Quoted 41 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +7 -1
  	}
 
  	ret = rdt_pseudo_lock_init();
@@ -3021,8 +3063,10 @@ static int rdt_get_tree(struct fs_context *fc)
  out_psl:
  	rdt_pseudo_lock_release();
  out_mondata:
-	if (resctrl_arch_mon_capable())
+	if (resctrl_arch_mon_capable()) {
+		rdtgroup_unassign_cntrs(&rdtgroup_default);
  		kernfs_remove(kn_mondata);
+	}
  out_mongrp:
  	if (resctrl_arch_mon_capable())
  		kernfs_remove(kn_mongrp);
@@ -3201,6 +3245,7 @@ static void free_all_child_rdtgrp(struct rdtgroup *rdtgrp)
 
  	head = &rdtgrp->mon.crdtgrp_list;
  	list_for_each_entry_safe(sentry, stmp, head, mon.crdtgrp_list) {
+		rdtgroup_unassign_cntrs(sentry);
  		free_rmid(sentry->closid, sentry->mon.rmid);
  		list_del(&sentry->mon.crdtgrp_list);
 
@@ -3241,6 +3286,8 @@ static void rmdir_all_sub(void)
  		cpumask_or(&rdtgroup_default.cpu_mask,
  			   &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
 
+		rdtgroup_unassign_cntrs(rdtgrp);
+
  		free_rmid(rdtgrp->closid, rdtgrp->mon.rmid);
 
  		kernfs_remove(rdtgrp->kn);
@@ -3272,6 +3319,7 @@ static void rdt_kill_sb(struct super_block *sb)
  	for_each_alloc_capable_rdt_resource(r)
  		reset_all_ctrls(r);
  	rmdir_all_sub();
+	rdtgroup_unassign_cntrs(&rdtgroup_default);
  	rdt_pseudo_lock_release();
  	rdtgroup_default.mode = RDT_MODE_SHAREABLE;
  	schemata_list_destroy();

arch/x86/kernel/cpu/resctrl/rdtgroup.c static void rdt_kill_sb(struct super_block *sb) · line 3331

Quoted 4 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +1 -0
@@ -3280,6 +3328,7 @@ static void rdt_kill_sb(struct super_block *sb)
  		resctrl_arch_disable_alloc();
  	if (resctrl_arch_mon_capable())
  		resctrl_arch_disable_mon();
+

Unnecessary change.

Quoted 39 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +8 -0
  	resctrl_mounted = false;
  	kernfs_kill_sb(sb);
  	mutex_unlock(&rdtgroup_mutex);
@@ -3871,6 +3920,8 @@ static int rdtgroup_mkdir_mon(struct kernfs_node *parent_kn,
  		goto out_unlock;
  	}
 
+	rdtgroup_assign_cntrs(rdtgrp);
+ >   	kernfs_activate(rdtgrp->kn);
 
  	/*
@@ -3915,6 +3966,8 @@ static int rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn,
  	if (ret)
  		goto out_closid_free;
 
+	rdtgroup_assign_cntrs(rdtgrp);
+
  	kernfs_activate(rdtgrp->kn);
 
  	ret = rdtgroup_init_alloc(rdtgrp);
@@ -3940,6 +3993,7 @@ static int rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn,
  out_del_list:
  	list_del(&rdtgrp->rdtgroup_list);
  out_rmid_free:
+	rdtgroup_unassign_cntrs(rdtgrp);
  	mkdir_rdt_prepare_rmid_free(rdtgrp);
  out_closid_free:
  	closid_free(closid);
@@ -4010,6 +4064,9 @@ static int rdtgroup_rmdir_mon(struct rdtgroup *rdtgrp, cpumask_var_t tmpmask)
  	update_closid_rmid(tmpmask, NULL);
 
  	rdtgrp->flags = RDT_DELETED;
+
+	rdtgroup_unassign_cntrs(rdtgrp);
+
  	free_rmid(rdtgrp->closid, rdtgrp->mon.rmid);
 
  	/*

arch/x86/kernel/cpu/resctrl/rdtgroup.c static int rdtgroup_rmdir_ctrl(struct rdtgroup *rdtgrp, cpumask_var_t tmpmask) · line 4120

Quoted 8 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +2 -0
@@ -4056,6 +4113,8 @@ static int rdtgroup_rmdir_ctrl(struct rdtgroup *rdtgrp, cpumask_var_t tmpmask)
  	cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask);
  	update_closid_rmid(tmpmask, NULL);
 
+	rdtgroup_unassign_cntrs(rdtgrp);
+
  	free_rmid(rdtgrp->closid, rdtgrp->mon.rmid);
  	closid_free(rdtgrp->closid);
 

Thanks.

-Fenghua

maintainer
#98 lore raw

Re: [PATCH v9 20/26] x86/resctrl: Auto assign/unassign counters when mbm_cntr_assign is enabled

Hi Fenghua,

Quoted 74 lines · Fenghua Yu

Hi, Babu,

Quoted 58 lines · Babu Moger

Assign/unassign counters on resctrl group creation/deletion. Two counters are required per group, one for MBM total event and one for MBM local event.

There are a limited number of counters available for assignment. If these counters are exhausted, the kernel will display the error message: "Out of MBM assignable counters". However, it is not necessary to fail the creation of a group due to assignment failures. Users have the flexibility to modify the assignments at a later time.

---
v9: Changed rdtgroup_assign_cntrs() and rdtgroup_unassign_cntrs() to return void. Updated couple of rdtgroup_unassign_cntrs() calls properly. Updated function comments.

v8: Renamed rdtgroup_assign_grp to rdtgroup_assign_cntrs.
     Renamed rdtgroup_unassign_grp to rdtgroup_unassign_cntrs. Fixed the problem with unassigning the child MON groups of CTRL_MON group.

v7: Reworded the commit message.
     Removed the reference of ABMC with mbm_cntr_assign. Renamed the function rdtgroup_assign_cntrs to rdtgroup_assign_grp.

v6: Removed the redundant comments on all the calls of rdtgroup_assign_cntrs. Updated the commit message.
     Dropped printing error message on every call of rdtgroup_assign_cntrs.

v5: Removed the code to enable/disable ABMC during the mount. That will be another patch.
     Added arch callers to get the arch specific data. Renamed fuctions to match the other abmc function. Added code comments for assignment failures.

v4: Few name changes based on the upstream discussion. Commit message update.

v3: This is a new patch. Patch addresses the upstream comment to enable ABMC feature by default if the feature is available.
---
  arch/x86/kernel/cpu/resctrl/rdtgroup.c | 61 +++++++++++++++++++++++++-

  1 file changed, 60 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/resctrl/rdtgroup.c +0 -0
index b0cce3dfd062..a8d21b0b2054 100644
@@ -2932,6 +2932,46 @@ static void schemata_list_destroy(void)
       }
   }

  +/*
+ * Called when a new group is created. If "mbm_cntr_assign" mode is enabled,
+ * counters are automatically assigned. Each group can accommodate two counters:
+ * one for the total event and one for the local event. Assignments may fail
+ * due to the limited number of counters. However, it is not necessary to fail
+ * the group creation and thus no failure is returned. Users have the option
+ * to modify the counter assignments after the group has been created.
+ */

arch/x86/kernel/cpu/resctrl/rdtgroup.c static void schemata_list_destroy(void) · line 2951

Quoted 9 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +9 -0
+static void rdtgroup_assign_cntrs(struct rdtgroup *rdtgrp)
+{
+    struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
+
+    if (!resctrl_arch_mbm_cntr_assign_enabled(r))
+        return;
+
+    if (is_mbm_total_enabled())
+        rdtgroup_assign_cntr_event(r, rdtgrp, NULL, QOS_L3_MBM_TOTAL_EVENT_ID);

In this code path, resctrl_mkdir()->resctrl_mkdir_ctrl_mon()->rdtgroup_assign_cntrs()->rdtgroup_assign_cntr_event()

Quoted 1 line

CPUs are not protected by read lock while rdtgroup_assign_cntr_event() walks r->mon_domains and run assing counters code on CPUs in the domains. Without CPU protection, r->mon_domains may race with CPU hotplug.

From what I can tell rdtgroup_assign_cntrs() is called with CPU hotplug lock held:

rdtgroup_mkdir_ctrl_mon()
{

	ret = mkdir_rdt_prepare(...);
	/* mkdir_rdt_prepare()->rdtgroup_kn_lock_live()->cpus_read_lock() */
	...
	rdtgroup_assign_cntrs(rdtgrp);
	...
	rdtgroup_kn_unlock(parent_kn);
	/* rdtgroup_kn_unlock()->cpus_read_unlock() */
	return ret;
}

Reinette

author reviewer
#99 lore raw

Re: [PATCH v9 20/26] x86/resctrl: Auto assign/unassign counters when mbm_cntr_assign is enabled

In reply to Fenghua Yu

Hi Fenghua,

Thanks for the review.

Quoted 101 lines · Fenghua Yu

Hi, Babu,

Quoted 76 lines · Babu Moger

Assign/unassign counters on resctrl group creation/deletion. Two counters are required per group, one for MBM total event and one for MBM local event.

There are a limited number of counters available for assignment. If these counters are exhausted, the kernel will display the error message: "Out of MBM assignable counters". However, it is not necessary to fail the creation of a group due to assignment failures. Users have the flexibility to modify the assignments at a later time.

---
v9: Changed rdtgroup_assign_cntrs() and rdtgroup_unassign_cntrs() to return void.
     Updated couple of rdtgroup_unassign_cntrs() calls properly. Updated function comments.

v8: Renamed rdtgroup_assign_grp to rdtgroup_assign_cntrs. Renamed rdtgroup_unassign_grp to rdtgroup_unassign_cntrs. Fixed the problem with unassigning the child MON groups of CTRL_MON group.

v7: Reworded the commit message.
     Removed the reference of ABMC with mbm_cntr_assign. Renamed the function rdtgroup_assign_cntrs to rdtgroup_assign_grp.

v6: Removed the redundant comments on all the calls of rdtgroup_assign_cntrs. Updated the commit message.
     Dropped printing error message on every call of rdtgroup_assign_cntrs.

v5: Removed the code to enable/disable ABMC during the mount. That will be another patch.
     Added arch callers to get the arch specific data. Renamed fuctions to match the other abmc function. Added code comments for assignment failures.

v4: Few name changes based on the upstream discussion. Commit message update.

v3: This is a new patch. Patch addresses the upstream comment to enable ABMC feature by default if the feature is available.
---
  arch/x86/kernel/cpu/resctrl/rdtgroup.c | 61 +++++++++++++++++++++++++-

  1 file changed, 60 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/resctrl/rdtgroup.c +0 -0
index b0cce3dfd062..a8d21b0b2054 100644
@@ -2932,6 +2932,46 @@ static void schemata_list_destroy(void)
       }
   }

  +/*
+ * Called when a new group is created. If "mbm_cntr_assign" mode is enabled,
+ * counters are automatically assigned. Each group can accommodate two counters:
+ * one for the total event and one for the local event. Assignments may fail
+ * due to the limited number of counters. However, it is not necessary to fail
+ * the group creation and thus no failure is returned. Users have the option
+ * to modify the counter assignments after the group has been created.
+ */

arch/x86/kernel/cpu/resctrl/rdtgroup.c +3 -0
+static void rdtgroup_assign_cntrs(struct rdtgroup *rdtgrp)
+{
+    struct rdt_resource *r =

&rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;

arch/x86/kernel/cpu/resctrl/rdtgroup.c +6 -0
+
+    if (!resctrl_arch_mbm_cntr_assign_enabled(r))
+        return;
+
+    if (is_mbm_total_enabled())
+        rdtgroup_assign_cntr_event(r, rdtgrp, NULL,
Quoted 1 line

QOS_L3_MBM_TOTAL_EVENT_ID);

In this code path, resctrl_mkdir()->resctrl_mkdir_ctrl_mon()->rdtgroup_assign_cntrs()->rdtgroup_assign_cntr_event()

CPUs are not protected by read lock while rdtgroup_assign_cntr_event() walks r->mon_domains and run assing counters code on CPUs in the domains. Without CPU protection, r->mon_domains may race with CPU hotplug.

In another patch (i.e. rdt_get_tree()), rdtgroup_assign_cntrs() is protected by cpus_read_lock()/unlock().

So maybe define two helpers:

// Called when caller takes cpus_read_lock()
rdtgroup_assign_cntrs_locked()
{
    lockdep_assert_cpus_held();

    then the current rdtgroup_assign_cntrs() code
}

Quoted 8 lines

// Called when caller doesn't take cpus_read_lock() rdtgroup_assign_cntrs()
{
    cpus_read_lock(); rdtgroup_assign_cntrs_locked(); cpus_read_unlock();
}

Good observation. Agree. There is a problem. Some of this code will change with earlier comments.

We know couple of paths are affected here. Why not just add the lock before calling in affected paths instead of adding new helpers?

/*
* Walking r->domains in rdtgroup_assign_cntrs, ensure it can't race
* with cpuhp
*/
cpus_read_lock();
rdtgroup_assign_cntrs()
cpus_read_unlock();

Quoted 22 lines
Quoted 22 lines

+
+    if (is_mbm_local_enabled())
+        rdtgroup_assign_cntr_event(r, rdtgrp, NULL, QOS_L3_MBM_LOCAL_EVENT_ID);

arch/x86/kernel/cpu/resctrl/rdtgroup.c +9 -0
+}
+
+/*
+ * Called when a group is deleted. Counters are unassigned if it was in
+ * assigned state.
+ */
+static void rdtgroup_unassign_cntrs(struct rdtgroup *rdtgrp)
+{
+    struct rdt_resource *r =

&rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;

arch/x86/kernel/cpu/resctrl/rdtgroup.c +6 -0
+
+    if (!resctrl_arch_mbm_cntr_assign_enabled(r))
+        return;
+
+    if (is_mbm_total_enabled())
+        rdtgroup_unassign_cntr_event(r, rdtgrp, NULL,

QOS_L3_MBM_TOTAL_EVENT_ID);
+

Quoted 8 lines

arch/x86/kernel/cpu/resctrl/rdtgroup.c

Quoted 5 lines

+    if (is_mbm_local_enabled())
+        rdtgroup_unassign_cntr_event(r, rdtgrp, NULL, QOS_L3_MBM_LOCAL_EVENT_ID);

arch/x86/kernel/cpu/resctrl/rdtgroup.c +2 -0
+}
+

Seems rdtgroup_unassign_cntrs() is always protected by cpus_read_lock()/unlock(). So it's good.

ok

Quoted 49 lines

arch/x86/kernel/cpu/resctrl/rdtgroup.c static int rdt_get_tree(struct fs_context *fc) · line 3035

Quoted 10 lines

  static int rdt_get_tree(struct fs_context *fc) {
      struct rdt_fs_context *ctx = rdt_fc2context(fc);

arch/x86/kernel/cpu/resctrl/rdtgroup.c +2 -0
@@ -2991,6 +3031,8 @@ static int rdt_get_tree(struct fs_context *fc)
           if (ret < 0)
               goto out_mongrp;
           rdtgroup_default.mon.mon_data_kn = kn_mondata;
+
+        rdtgroup_assign_cntrs(&rdtgroup_default);

In this case, cpus_read_lock() was called earlier. Change to rdtgroup_assign_cntrs_locked().

Quoted 35 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +0 -0
       }
         ret = rdt_pseudo_lock_init();
@@ -3021,8 +3063,10 @@ static int rdt_get_tree(struct fs_context *fc)

  out_psl:
      rdt_pseudo_lock_release(); out_mondata:
-    if (resctrl_arch_mon_capable())
+    if (resctrl_arch_mon_capable()) {
+        rdtgroup_unassign_cntrs(&rdtgroup_default); kernfs_remove(kn_mondata);
+    }
  out_mongrp:
      if (resctrl_arch_mon_capable()) kernfs_remove(kn_mongrp);

arch/x86/kernel/cpu/resctrl/rdtgroup.c +1 -0
@@ -3201,6 +3245,7 @@ static void free_all_child_rdtgrp(struct rdtgroup
 *rdtgrp)
         head = &rdtgrp->mon.crdtgrp_list;
       list_for_each_entry_safe(sentry, stmp, head, mon.crdtgrp_list) {
+        rdtgroup_unassign_cntrs(sentry);
           free_rmid(sentry->closid, sentry->mon.rmid);
           list_del(&sentry->mon.crdtgrp_list);
   @@ -3241,6 +3286,8 @@ static void rmdir_all_sub(void)

          cpumask_or(&rdtgroup_default.cpu_mask, &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask); +        rdtgroup_unassign_cntrs(rdtgrp);
+
          free_rmid(rdtgrp->closid, rdtgrp->mon.rmid); kernfs_remove(rdtgrp->kn);

arch/x86/kernel/cpu/resctrl/rdtgroup.c +1 -0
@@ -3272,6 +3319,7 @@ static void rdt_kill_sb(struct super_block *sb)
       for_each_alloc_capable_rdt_resource(r)
           reset_all_ctrls(r);
       rmdir_all_sub();
+    rdtgroup_unassign_cntrs(&rdtgroup_default);
       rdt_pseudo_lock_release();
       rdtgroup_default.mode = RDT_MODE_SHAREABLE;
Quoted 8 lines

arch/x86/kernel/cpu/resctrl/rdtgroup.c static void rdt_kill_sb(struct super_block *sb) · line 3331

Quoted 6 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +1 -0
       schemata_list_destroy();
@@ -3280,6 +3328,7 @@ static void rdt_kill_sb(struct super_block *sb)
           resctrl_arch_disable_alloc();
       if (resctrl_arch_mon_capable())
           resctrl_arch_disable_mon();
+

Unnecessary change.

ok.

Quoted 51 lines
Quoted 45 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +2 -0
       resctrl_mounted = false;
       kernfs_kill_sb(sb);
       mutex_unlock(&rdtgroup_mutex);
@@ -3871,6 +3920,8 @@ static int rdtgroup_mkdir_mon(struct kernfs_node
 *parent_kn,
           goto out_unlock;
       }
   +    rdtgroup_assign_cntrs(rdtgrp);
+ >       kernfs_activate(rdtgrp->kn);
         /*
@@ -3915,6 +3966,8 @@ static int rdtgroup_mkdir_ctrl_mon(struct
 kernfs_node *parent_kn,
       if (ret)
           goto out_closid_free;
   +    rdtgroup_assign_cntrs(rdtgrp);
+
       kernfs_activate(rdtgrp->kn);
         ret = rdtgroup_init_alloc(rdtgrp);
@@ -3940,6 +3993,7 @@ static int rdtgroup_mkdir_ctrl_mon(struct
 kernfs_node *parent_kn,

  out_del_list:
      list_del(&rdtgrp->rdtgroup_list);
  out_rmid_free:
+    rdtgroup_unassign_cntrs(rdtgrp);
      mkdir_rdt_prepare_rmid_free(rdtgrp);
  out_closid_free:
      closid_free(closid);

arch/x86/kernel/cpu/resctrl/rdtgroup.c +4 -0
@@ -4010,6 +4064,9 @@ static int rdtgroup_rmdir_mon(struct rdtgroup
 *rdtgrp, cpumask_var_t tmpmask)
       update_closid_rmid(tmpmask, NULL);
         rdtgrp->flags = RDT_DELETED;
+
+    rdtgroup_unassign_cntrs(rdtgrp);
+
       free_rmid(rdtgrp->closid, rdtgrp->mon.rmid);
         /*
@@ -4056,6 +4113,8 @@ static int rdtgroup_rmdir_ctrl(struct rdtgroup
 *rdtgrp, cpumask_var_t tmpmask)
       cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask);
       update_closid_rmid(tmpmask, NULL);
   +    rdtgroup_unassign_cntrs(rdtgrp);
+
       free_rmid(rdtgrp->closid, rdtgrp->mon.rmid);
       closid_free(rdtgrp->closid);
Quoted 1 line

Thanks.

-Fenghua

Signature · 2 lines
Thanks
Babu Moger
author reviewer
#100 lore raw

Re: [PATCH v9 20/26] x86/resctrl: Auto assign/unassign counters when mbm_cntr_assign is enabled

Hi Fenghua,

Quoted 121 lines · Babu

Hi Fenghua,

Thanks for the review.

Quoted 101 lines · Fenghua Yu

Hi, Babu,

Quoted 76 lines · Babu Moger

Assign/unassign counters on resctrl group creation/deletion. Two counters are required per group, one for MBM total event and one for MBM local event.

There are a limited number of counters available for assignment. If these counters are exhausted, the kernel will display the error message: "Out of MBM assignable counters". However, it is not necessary to fail the creation of a group due to assignment failures. Users have the flexibility to modify the assignments at a later time.

---
v9: Changed rdtgroup_assign_cntrs() and rdtgroup_unassign_cntrs() to return void.
     Updated couple of rdtgroup_unassign_cntrs() calls properly. Updated function comments.

v8: Renamed rdtgroup_assign_grp to rdtgroup_assign_cntrs. Renamed rdtgroup_unassign_grp to rdtgroup_unassign_cntrs. Fixed the problem with unassigning the child MON groups of CTRL_MON group.

v7: Reworded the commit message.
     Removed the reference of ABMC with mbm_cntr_assign. Renamed the function rdtgroup_assign_cntrs to rdtgroup_assign_grp.

v6: Removed the redundant comments on all the calls of rdtgroup_assign_cntrs. Updated the commit message.
     Dropped printing error message on every call of rdtgroup_assign_cntrs.

v5: Removed the code to enable/disable ABMC during the mount. That will be another patch.
     Added arch callers to get the arch specific data. Renamed fuctions to match the other abmc function. Added code comments for assignment failures.

v4: Few name changes based on the upstream discussion. Commit message update.

v3: This is a new patch. Patch addresses the upstream comment to enable ABMC feature by default if the feature is available.
---
  arch/x86/kernel/cpu/resctrl/rdtgroup.c | 61 +++++++++++++++++++++++++-

  1 file changed, 60 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/resctrl/rdtgroup.c +0 -0
index b0cce3dfd062..a8d21b0b2054 100644
@@ -2932,6 +2932,46 @@ static void schemata_list_destroy(void)
       }
   }

  +/*
+ * Called when a new group is created. If "mbm_cntr_assign" mode is enabled,
+ * counters are automatically assigned. Each group can accommodate two counters:
+ * one for the total event and one for the local event. Assignments may fail
+ * due to the limited number of counters. However, it is not necessary to fail
+ * the group creation and thus no failure is returned. Users have the option
+ * to modify the counter assignments after the group has been created.
+ */

arch/x86/kernel/cpu/resctrl/rdtgroup.c +3 -0
+static void rdtgroup_assign_cntrs(struct rdtgroup *rdtgrp)
+{
+    struct rdt_resource *r =

&rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;

arch/x86/kernel/cpu/resctrl/rdtgroup.c +6 -0
+
+    if (!resctrl_arch_mbm_cntr_assign_enabled(r))
+        return;
+
+    if (is_mbm_total_enabled())
+        rdtgroup_assign_cntr_event(r, rdtgrp, NULL,
Quoted 1 line

QOS_L3_MBM_TOTAL_EVENT_ID);

In this code path, resctrl_mkdir()->resctrl_mkdir_ctrl_mon()->rdtgroup_assign_cntrs()->rdtgroup_assign_cntr_event()

CPUs are not protected by read lock while rdtgroup_assign_cntr_event() walks r->mon_domains and run assing counters code on CPUs in the domains. Without CPU protection, r->mon_domains may race with CPU hotplug.

In another patch (i.e. rdt_get_tree()), rdtgroup_assign_cntrs() is protected by cpus_read_lock()/unlock().

So maybe define two helpers:

// Called when caller takes cpus_read_lock()
rdtgroup_assign_cntrs_locked()
{
    lockdep_assert_cpus_held();

    then the current rdtgroup_assign_cntrs() code
}

Quoted 8 lines

// Called when caller doesn't take cpus_read_lock() rdtgroup_assign_cntrs()
{
    cpus_read_lock(); rdtgroup_assign_cntrs_locked(); cpus_read_unlock();
}

Good observation. Agree. There is a problem. Some of this code will change with earlier comments.

We know couple of paths are affected here. Why not just add the lock before calling in affected paths instead of adding new helpers?

Quoted 8 lines

/*
* Walking r->domains in rdtgroup_assign_cntrs, ensure it can't race
* with cpuhp
*/
cpus_read_lock();
rdtgroup_assign_cntrs()
cpus_read_unlock();

Oh no. Looks like we are good here.

Looks at Reinette's response. Thanks Reinette.

https://lore.kernel.org/lkml/[email protected]/

Quoted 147 lines
Quoted 24 lines
Quoted 24 lines

+
+    if (is_mbm_local_enabled())
+        rdtgroup_assign_cntr_event(r, rdtgrp, NULL, QOS_L3_MBM_LOCAL_EVENT_ID);

arch/x86/kernel/cpu/resctrl/rdtgroup.c +9 -0
+}
+
+/*
+ * Called when a group is deleted. Counters are unassigned if it was in
+ * assigned state.
+ */
+static void rdtgroup_unassign_cntrs(struct rdtgroup *rdtgrp)
+{
+    struct rdt_resource *r =

&rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;

arch/x86/kernel/cpu/resctrl/rdtgroup.c +6 -0
+
+    if (!resctrl_arch_mbm_cntr_assign_enabled(r))
+        return;
+
+    if (is_mbm_total_enabled())
+        rdtgroup_unassign_cntr_event(r, rdtgrp, NULL,

QOS_L3_MBM_TOTAL_EVENT_ID);
+

Quoted 8 lines

arch/x86/kernel/cpu/resctrl/rdtgroup.c

Quoted 5 lines

+    if (is_mbm_local_enabled())
+        rdtgroup_unassign_cntr_event(r, rdtgrp, NULL, QOS_L3_MBM_LOCAL_EVENT_ID);

arch/x86/kernel/cpu/resctrl/rdtgroup.c +2 -0
+}
+

Seems rdtgroup_unassign_cntrs() is always protected by cpus_read_lock()/unlock(). So it's good.

ok

Quoted 49 lines

arch/x86/kernel/cpu/resctrl/rdtgroup.c static int rdt_get_tree(struct fs_context *fc) · line 3035

Quoted 10 lines

  static int rdt_get_tree(struct fs_context *fc) {
      struct rdt_fs_context *ctx = rdt_fc2context(fc);

arch/x86/kernel/cpu/resctrl/rdtgroup.c +2 -0
@@ -2991,6 +3031,8 @@ static int rdt_get_tree(struct fs_context *fc)
           if (ret < 0)
               goto out_mongrp;
           rdtgroup_default.mon.mon_data_kn = kn_mondata;
+
+        rdtgroup_assign_cntrs(&rdtgroup_default);

In this case, cpus_read_lock() was called earlier. Change to rdtgroup_assign_cntrs_locked().

Quoted 35 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +0 -0
       }
         ret = rdt_pseudo_lock_init();
@@ -3021,8 +3063,10 @@ static int rdt_get_tree(struct fs_context *fc)

  out_psl:
      rdt_pseudo_lock_release(); out_mondata:
-    if (resctrl_arch_mon_capable())
+    if (resctrl_arch_mon_capable()) {
+        rdtgroup_unassign_cntrs(&rdtgroup_default); kernfs_remove(kn_mondata);
+    }
  out_mongrp:
      if (resctrl_arch_mon_capable()) kernfs_remove(kn_mongrp);

arch/x86/kernel/cpu/resctrl/rdtgroup.c +1 -0
@@ -3201,6 +3245,7 @@ static void free_all_child_rdtgrp(struct rdtgroup
 *rdtgrp)
         head = &rdtgrp->mon.crdtgrp_list;
       list_for_each_entry_safe(sentry, stmp, head, mon.crdtgrp_list) {
+        rdtgroup_unassign_cntrs(sentry);
           free_rmid(sentry->closid, sentry->mon.rmid);
           list_del(&sentry->mon.crdtgrp_list);
   @@ -3241,6 +3286,8 @@ static void rmdir_all_sub(void)

          cpumask_or(&rdtgroup_default.cpu_mask, &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask); +        rdtgroup_unassign_cntrs(rdtgrp);
+
          free_rmid(rdtgrp->closid, rdtgrp->mon.rmid); kernfs_remove(rdtgrp->kn);

arch/x86/kernel/cpu/resctrl/rdtgroup.c +1 -0
@@ -3272,6 +3319,7 @@ static void rdt_kill_sb(struct super_block *sb)
       for_each_alloc_capable_rdt_resource(r)
           reset_all_ctrls(r);
       rmdir_all_sub();
+    rdtgroup_unassign_cntrs(&rdtgroup_default);
       rdt_pseudo_lock_release();
       rdtgroup_default.mode = RDT_MODE_SHAREABLE;
Quoted 8 lines

arch/x86/kernel/cpu/resctrl/rdtgroup.c static void rdt_kill_sb(struct super_block *sb) · line 3331

Quoted 6 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +1 -0
       schemata_list_destroy();
@@ -3280,6 +3328,7 @@ static void rdt_kill_sb(struct super_block *sb)
           resctrl_arch_disable_alloc();
       if (resctrl_arch_mon_capable())
           resctrl_arch_disable_mon();
+

Unnecessary change.

ok.

Quoted 51 lines
Quoted 45 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +2 -0
       resctrl_mounted = false;
       kernfs_kill_sb(sb);
       mutex_unlock(&rdtgroup_mutex);
@@ -3871,6 +3920,8 @@ static int rdtgroup_mkdir_mon(struct kernfs_node
 *parent_kn,
           goto out_unlock;
       }
   +    rdtgroup_assign_cntrs(rdtgrp);
+ >       kernfs_activate(rdtgrp->kn);
         /*
@@ -3915,6 +3966,8 @@ static int rdtgroup_mkdir_ctrl_mon(struct
 kernfs_node *parent_kn,
       if (ret)
           goto out_closid_free;
   +    rdtgroup_assign_cntrs(rdtgrp);
+
       kernfs_activate(rdtgrp->kn);
         ret = rdtgroup_init_alloc(rdtgrp);
@@ -3940,6 +3993,7 @@ static int rdtgroup_mkdir_ctrl_mon(struct
 kernfs_node *parent_kn,

  out_del_list:
      list_del(&rdtgrp->rdtgroup_list);
  out_rmid_free:
+    rdtgroup_unassign_cntrs(rdtgrp);
      mkdir_rdt_prepare_rmid_free(rdtgrp);
  out_closid_free:
      closid_free(closid);

arch/x86/kernel/cpu/resctrl/rdtgroup.c +4 -0
@@ -4010,6 +4064,9 @@ static int rdtgroup_rmdir_mon(struct rdtgroup
 *rdtgrp, cpumask_var_t tmpmask)
       update_closid_rmid(tmpmask, NULL);
         rdtgrp->flags = RDT_DELETED;
+
+    rdtgroup_unassign_cntrs(rdtgrp);
+
       free_rmid(rdtgrp->closid, rdtgrp->mon.rmid);
         /*
@@ -4056,6 +4113,8 @@ static int rdtgroup_rmdir_ctrl(struct rdtgroup
 *rdtgrp, cpumask_var_t tmpmask)
       cpumask_or(tmpmask, tmpmask, &rdtgrp->cpu_mask);
       update_closid_rmid(tmpmask, NULL);
   +    rdtgroup_unassign_cntrs(rdtgrp);
+
       free_rmid(rdtgrp->closid, rdtgrp->mon.rmid);
       closid_free(rdtgrp->closid);
Quoted 1 line

Thanks.

-Fenghua

Signature · 2 lines
Thanks
Babu Moger
#26 lore raw

Re: [PATCH v9 09/26] x86/resctrl: Introduce interface to display number of monitoring counters

In reply to Moger, Babu

Hi Babu,

Quoted 22 lines · Babu

Hi Reinette,

Quoted 13 lines · Reinette Chatre

Hi Babu,

Quoted 9 lines · Babu Moger

The mbm_cntr_assign mode provides an option to the user to assign a counter to an RMID, event pair and monitor the bandwidth as long as the counter is assigned. Number of assignments depend on number of monitoring counters available.

Provide the interface to display the number of monitoring counters supported. The interface file 'num_mbm_cntrs' is available when an architecture supports mbm_cntr_assign mode.

Quoted 5 lines

As mentioned in previous patch, do you think it may be possible to have a value for num_mbm_cntrs for non-ABMC AMD systems? If that is available and always exposed to user space (irrespective of mbm_cntr_assign mode) then it would be clear to user space on benefits/risks of running a "default" mode.

Quoted 6 lines

I am trying the work-around to get the number of max active RMIDs in default mode. The method is to loop through all of the recently assigned RMID's to see if any of their QM_CTR.U bits transition from 0->1.

I am not successful in getting it to work so far. I remember Peter was trying this before in soft-ABMC. Peter, Any success with that?

Sorry I missed this question before. By now maybe you've already debugged your own implementation...

Here's what I've been using:

-- >8 --
Subject: [PATCH] x86/resctrl: Detect MBM counters on pre-ABMC AMD
implementations

This procedure is based on information provided directly by AMD which cannot currently be corroborated by any public documentation.

In particular, it assumes that deallocation of MBM counters for RMIDs is driven directly by writing a new RMID value into PQR_ASSOC.

---
 arch/x86/kernel/cpu/resctrl/internal.h |  1 +
 arch/x86/kernel/cpu/resctrl/monitor.c  | 83 ++++++++++++++++++++++++++
 2 files changed, 84 insertions(+)
arch/x86/kernel/cpu/resctrl/internal.h +1 -0
index 377b5db667930..4a13eab110510 100644
@@ -705,6 +705,7 @@ int closids_supported(void);
 void closid_free(int closid);
 int alloc_rmid(u32 closid);
 void free_rmid(u32 closid, u32 rmid);
+int amd_detect_mbm_counters(void);
 int __init rdt_get_mon_l3_config(struct rdt_resource *r);
 void __exit rdt_put_mon_l3_config(void);
 bool __init rdt_cpu_has(int flag);
arch/x86/kernel/cpu/resctrl/monitor.c +83 -0

89 lines not on this page · raw message