[v2,10/17] x86/resctrl: Add interface to display monitor state of the group

Message ID a9b55e14539614dd16dd8f1f74f82f01b14a9e3f.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
  The ABMC feature provides an option to the user to assign an RMID to
the hardware counter and monitor the bandwidth for the longer duration.
The assigned RMID will be active until user unassigns the RMID.

Add a new field monitor_state in resctrl group interface to display the
assignment state of the group. This field is available when ABMC feature
is supported on the system.

By default the monitor_state is initialized to unassigned state when
ABMC is enabled.
    $cat /sys/fs/resctrl/monitor_state
    total=unassign;local=unassign

Signed-off-by: Babu Moger <babu.moger@amd.com>
---
v2: Added check to display "Unsupported" when user tries to access
    monitor state when ABMC is not enabled.
---
 Documentation/arch/x86/resctrl.rst     | 20 ++++++++++++
 arch/x86/kernel/cpu/resctrl/internal.h |  8 +++++
 arch/x86/kernel/cpu/resctrl/monitor.c  |  2 ++
 arch/x86/kernel/cpu/resctrl/rdtgroup.c | 45 ++++++++++++++++++++++++++
 4 files changed, 75 insertions(+)
  

Patch

diff --git a/Documentation/arch/x86/resctrl.rst b/Documentation/arch/x86/resctrl.rst
index f09239cb93e8..4f89d5d1b61f 100644
--- a/Documentation/arch/x86/resctrl.rst
+++ b/Documentation/arch/x86/resctrl.rst
@@ -400,6 +400,26 @@  When monitoring is enabled all MON groups will also contain:
 	the sum for all tasks in the CTRL_MON group and all tasks in
 	MON groups. Please see example section for more details on usage.
 
+"monitor_state":
+	Available when ABMC feature is supported. ABMC feature provides an
+	option to the user to assign an RMID to hardware counter and
+	monitor the bandwidth for the longer duration. The RMID will
+	be active until user unassigns it manually. Each group will have
+	two events that are assignable. By default, the events are
+	unassigned. Index 0 holds the monitor_state for MBM total bytes.
+	Index 1 holds the monitor_state for MBM local bytes.
+
+	Example::
+
+		# cat /sys/fs/resctrl/monitor_state
+		total=unassign;local=unassign
+
+	When the events are assigned, the output will look like below.
+	Example::
+
+		# cat /sys/fs/resctrl/monitor_state
+		total=assign;local=assign
+
 "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 3467221f2af5..865101c5e1c2 100644
--- a/arch/x86/kernel/cpu/resctrl/internal.h
+++ b/arch/x86/kernel/cpu/resctrl/internal.h
@@ -57,6 +57,12 @@ 
 /* ABMC ENABLE */
 #define ABMC_ENABLE			BIT(0)
 
+/*
+ * monitor group's state when ABMC is enabled
+ */
+#define TOTAL_ASSIGN			BIT(0)
+#define LOCAL_ASSIGN			BIT(1)
+
 struct rdt_fs_context {
 	struct kernfs_fs_context	kfc;
 	bool				enable_cdpl2;
@@ -163,12 +169,14 @@  enum rdtgrp_mode {
  * @parent:			parent rdtgrp
  * @crdtgrp_list:		child rdtgroup node list
  * @rmid:			rmid for this rdtgroup
+ * @monitor_state:		Assignment state of the group
  */
 struct mongroup {
 	struct kernfs_node	*mon_data_kn;
 	struct rdtgroup		*parent;
 	struct list_head	crdtgrp_list;
 	u32			rmid;
+	u32			monitor_state;
 };
 
 /**
diff --git a/arch/x86/kernel/cpu/resctrl/monitor.c b/arch/x86/kernel/cpu/resctrl/monitor.c
index a45084e30738..de43be2252cc 100644
--- a/arch/x86/kernel/cpu/resctrl/monitor.c
+++ b/arch/x86/kernel/cpu/resctrl/monitor.c
@@ -829,6 +829,8 @@  int __init rdt_get_mon_l3_config(struct rdt_resource *r)
 						 RFTYPE_MON_INFO);
 			resctrl_file_fflags_init("mbm_assign_enable",
 						 RFTYPE_MON_INFO);
+			resctrl_file_fflags_init("monitor_state",
+						 RFTYPE_MON_BASE);
 		}
 	}
 
diff --git a/arch/x86/kernel/cpu/resctrl/rdtgroup.c b/arch/x86/kernel/cpu/resctrl/rdtgroup.c
index 9c8db9562c91..7cae6ac13954 100644
--- a/arch/x86/kernel/cpu/resctrl/rdtgroup.c
+++ b/arch/x86/kernel/cpu/resctrl/rdtgroup.c
@@ -779,6 +779,36 @@  static int rdtgroup_tasks_show(struct kernfs_open_file *of,
 	return ret;
 }
 
+static int rdtgroup_monitor_state_show(struct kernfs_open_file *of,
+				       struct seq_file *s, void *v)
+{
+	struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
+	struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
+	struct rdtgroup *rdtgrp;
+	int ret = 0;
+
+	if (!hw_res->abmc_enabled) {
+		rdt_last_cmd_puts("Assignable Bandwidth Monitoring is not enabled\n");
+		seq_printf(s, "Unsuppoted\n");
+		return ret;
+	}
+
+	rdt_last_cmd_clear();
+
+	rdtgrp = rdtgroup_kn_lock_live(of->kn);
+	if (rdtgrp)
+		seq_printf(s, "total=%s;local=%s\n",
+			   rdtgrp->mon.monitor_state & TOTAL_ASSIGN ?
+			   "assign" : "unassign",
+			   rdtgrp->mon.monitor_state & LOCAL_ASSIGN ?
+			   "assign" : "unassign");
+	else
+		ret = -EINVAL;
+	rdtgroup_kn_unlock(of->kn);
+
+	return ret;
+}
+
 static int rdtgroup_closid_show(struct kernfs_open_file *of,
 				struct seq_file *s, void *v)
 {
@@ -1944,6 +1974,12 @@  static struct rftype res_common_files[] = {
 		.flags		= RFTYPE_FLAGS_CPUS_LIST,
 		.fflags		= RFTYPE_BASE,
 	},
+	{
+		.name		= "monitor_state",
+		.mode		= 0444,
+		.kf_ops		= &rdtgroup_kf_single_ops,
+		.seq_show	= rdtgroup_monitor_state_show,
+	},
 	{
 		.name		= "tasks",
 		.mode		= 0644,
@@ -2439,6 +2475,7 @@  static void resctrl_abmc_msrwrite(void *arg)
 static int resctrl_abmc_setup(enum resctrl_res_level l, bool enable)
 {
 	struct rdt_resource *r = &rdt_resources_all[l].r_resctrl;
+	struct rdtgroup *prgrp, *crgrp;
 	struct rdt_domain *d;
 
 	/* Update QOS_CFG MSR on all the CPUs in cpu_mask */
@@ -2447,6 +2484,14 @@  static int resctrl_abmc_setup(enum resctrl_res_level l, bool enable)
 		resctrl_arch_reset_rmid_all(r, d);
 	}
 
+	/* Reset monitor state for all the monitor groups */
+	list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
+		memset(&prgrp->mon.monitor_state, 0, sizeof(prgrp->mon.monitor_state));
+
+		list_for_each_entry(crgrp, &prgrp->mon.crdtgrp_list, mon.crdtgrp_list)
+			memset(&crgrp->mon.monitor_state, 0, sizeof(crgrp->mon.monitor_state));
+	}
+
 	return 0;
 }