[v2,16/17] x86/resctrl: Add the interface unassign the RMID

Message ID 1014d6620a263cf4b606ed453a3ba48b581d2fee.1705688539.git.babu.moger@amd.com
State New
Headers
Series x86/resctrl : Support AMD Assignable Bandwidth Monitoring Counters (ABMC) |

Commit Message

Moger, Babu Jan. 19, 2024, 6:22 p.m. UTC
  With the support of ABMC (Assignable Bandwidth Monitoring Counters)
feature, the user has the option to assign or unassign a RMID to
hardware counter and monitor the bandwidth for the longer duration.

Provide the interface to unassign the RMID.

Signed-off-by: Babu Moger <babu.moger@amd.com>
---
v2: No changes.
---
 Documentation/arch/x86/resctrl.rst     | 11 ++++++++
 arch/x86/kernel/cpu/resctrl/internal.h |  1 +
 arch/x86/kernel/cpu/resctrl/rdtgroup.c | 36 ++++++++++++++++++++++++++
 3 files changed, 48 insertions(+)
  

Patch

diff --git a/Documentation/arch/x86/resctrl.rst b/Documentation/arch/x86/resctrl.rst
index 2729c6fe6127..4ba9b1275a2b 100644
--- a/Documentation/arch/x86/resctrl.rst
+++ b/Documentation/arch/x86/resctrl.rst
@@ -427,6 +427,17 @@  When monitoring is enabled all MON groups will also contain:
 		# echo total=assign > /sys/fs/resctrl/monitor_state
 		# echo total=assign;local=assign > /sys/fs/resctrl/monitor_state
 
+	The user needs to unassign counter to release it.
+	Example::
+
+		# echo total=unassign > /sys/fs/resctrl/monitor_state
+		# cat /sys/fs/resctrl/monitor_state
+		total=unassign;local=assign
+
+		# echo total=unassign;local=unassign > /sys/fs/resctrl/monitor_state
+		# cat /sys/fs/resctrl/monitor_state
+		total=unassign;local=unassign
+
 "mon_hw_id":
 	Available only with debug option. The identifier used by hardware
 	for the monitor group. On x86 this is the RMID.
diff --git a/arch/x86/kernel/cpu/resctrl/internal.h b/arch/x86/kernel/cpu/resctrl/internal.h
index e109c0388762..ca3193986b4f 100644
--- a/arch/x86/kernel/cpu/resctrl/internal.h
+++ b/arch/x86/kernel/cpu/resctrl/internal.h
@@ -614,5 +614,6 @@  void __init resctrl_file_fflags_init(const char *config,
 				     unsigned long fflags);
 void rdt_staged_configs_clear(void);
 void arch_domain_mbm_evt_config(struct rdt_hw_domain *hw_dom);
+void assignable_counters_free(int counterid);
 
 #endif /* _ASM_X86_RESCTRL_INTERNAL_H */
diff --git a/arch/x86/kernel/cpu/resctrl/rdtgroup.c b/arch/x86/kernel/cpu/resctrl/rdtgroup.c
index 3447fc4ff2e9..869fab878087 100644
--- a/arch/x86/kernel/cpu/resctrl/rdtgroup.c
+++ b/arch/x86/kernel/cpu/resctrl/rdtgroup.c
@@ -192,6 +192,11 @@  static int assignable_counters_alloc(void)
 	return counterid;
 }
 
+void assignable_counters_free(int counterid)
+{
+	assignable_counter_free_map |= 1 << counterid;
+}
+
 /**
  * rdtgroup_mode_by_closid - Return mode of resource group with closid
  * @closid: closid if the resource group
@@ -1723,6 +1728,31 @@  static ssize_t rdtgroup_assign_abmc(struct rdtgroup *rdtgrp,
 	return 0;
 }
 
+static ssize_t rdtgroup_unassign_abmc(struct rdtgroup *rdtgrp,
+				      struct rdt_resource *r,
+				      u32 evtid, int mon_state)
+{
+	struct rdt_domain *d;
+	int index;
+
+	index = mon_event_config_index_get(evtid);
+	if (index == INVALID_CONFIG_INDEX) {
+		pr_warn_once("Invalid event id %d\n", evtid);
+		return -EINVAL;
+	}
+
+	if (rdtgrp->mon.monitor_state & mon_state) {
+		list_for_each_entry(d, &r->domains, list)
+			rdtgroup_abmc_domain(d, rdtgrp, evtid, index, 0);
+
+		assignable_counters_free(rdtgrp->mon.abmc_ctr_id[index]);
+	}
+
+	rdtgrp->mon.monitor_state &= ~mon_state;
+
+	return 0;
+}
+
 /**
  * rdtgroup_monitor_state_write - Interface to assign/unassign an RMID.
  *
@@ -1776,6 +1806,12 @@  static ssize_t rdtgroup_monitor_state_write(struct kernfs_open_file *of,
 					rdt_last_cmd_puts("ABMC assign failed\n");
 					break;
 				}
+			} else if (!strcmp(abmc_str, "unassign")) {
+				ret = rdtgroup_unassign_abmc(rdtgrp, r, evtid, mon_state);
+				if (ret) {
+					rdt_last_cmd_puts("ABMC unassign failed\n");
+					break;
+				}
 			} else {
 				rdt_last_cmd_puts("Invalid ABMC event\n");
 				ret = -EINVAL;