[PATCH v12 21/26] x86/resctrl: Introduce mbm_assign_on_mkdir to configure assignments

From: Babu Moger
Date: Thu Apr 03 2025 - 20:37:40 EST


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.

Introduce a configuration option to automatically assign counter IDs
when a resctrl group is created, provided the counters are available.
By default, this option is enabled at boot.

Signed-off-by: Babu Moger <babu.moger@xxxxxxx>
---
v12: New patch. Added after the discussion on the list.
https://lore.kernel.org/lkml/CALPaoCh8siZKjL_3yvOYGL4cF_n_38KpUFgHVGbQ86nD+Q2_SA@xxxxxxxxxxxxxx/
---
Documentation/arch/x86/resctrl.rst | 10 +++++++
arch/x86/kernel/cpu/resctrl/monitor.c | 1 +
arch/x86/kernel/cpu/resctrl/rdtgroup.c | 40 ++++++++++++++++++++++++++
include/linux/resctrl.h | 2 ++
4 files changed, 53 insertions(+)

diff --git a/Documentation/arch/x86/resctrl.rst b/Documentation/arch/x86/resctrl.rst
index 4e6feba6fb08..a67f09323da0 100644
--- a/Documentation/arch/x86/resctrl.rst
+++ b/Documentation/arch/x86/resctrl.rst
@@ -345,6 +345,16 @@ with the following files:
# cat /sys/fs/resctrl/info/L3_MON/counter_configs/mbm_total_bytes/event_filter
local_reads, local_non_temporal_writes

+"mbm_assign_on_mkdir":
+ Automatically assign the monitoring counters on resctrl group creation
+ if the counters are available. It is enabled by default on boot and users
+ can disable by writing to the interface.
+ ::
+
+ # echo 0 > /sys/fs/resctrl/info/L3_MON/mbm_assign_on_mkdir
+ # cat /sys/fs/resctrl/info/L3_MON/mbm_assign_on_mkdir
+ 0
+
"max_threshold_occupancy":
Read/write file provides the largest value (in
bytes) at which a previously used LLC_occupancy
diff --git a/arch/x86/kernel/cpu/resctrl/monitor.c b/arch/x86/kernel/cpu/resctrl/monitor.c
index 4525295b1725..ee31dfe2c224 100644
--- a/arch/x86/kernel/cpu/resctrl/monitor.c
+++ b/arch/x86/kernel/cpu/resctrl/monitor.c
@@ -1265,6 +1265,7 @@ int __init resctrl_mon_resource_init(void)
resctrl_file_fflags_init("num_mbm_cntrs", RFTYPE_MON_INFO);
resctrl_file_fflags_init("available_mbm_cntrs", RFTYPE_MON_INFO);
resctrl_file_fflags_init("event_filter", RFTYPE_MON_CONFIG);
+ resctrl_file_fflags_init("mbm_assign_on_mkdir", RFTYPE_MON_INFO);
}

return 0;
diff --git a/arch/x86/kernel/cpu/resctrl/rdtgroup.c b/arch/x86/kernel/cpu/resctrl/rdtgroup.c
index 7792455f0b26..592a9dc5b404 100644
--- a/arch/x86/kernel/cpu/resctrl/rdtgroup.c
+++ b/arch/x86/kernel/cpu/resctrl/rdtgroup.c
@@ -993,6 +993,39 @@ static int resctrl_available_mbm_cntrs_show(struct kernfs_open_file *of,
return ret;
}

+static int resctrl_mbm_assign_on_mkdir_show(struct kernfs_open_file *of,
+ struct seq_file *s, void *v)
+{
+ struct rdt_resource *r = of->kn->parent->priv;
+
+ seq_printf(s, "%u\n", r->mon.mbm_assign_on_mkdir);
+
+ return 0;
+}
+
+static ssize_t resctrl_mbm_assign_on_mkdir_write(struct kernfs_open_file *of,
+ char *buf, size_t nbytes, loff_t off)
+{
+ struct rdt_resource *r = of->kn->parent->priv;
+ bool value;
+ int ret;
+
+ ret = kstrtobool(buf, &value);
+ if (ret)
+ return ret;
+
+ cpus_read_lock();
+ mutex_lock(&rdtgroup_mutex);
+ rdt_last_cmd_clear();
+
+ r->mon.mbm_assign_on_mkdir = value;
+
+ mutex_unlock(&rdtgroup_mutex);
+ cpus_read_unlock();
+
+ return ret ?: nbytes;
+}
+
#ifdef CONFIG_PROC_CPU_RESCTRL

/*
@@ -2176,6 +2209,13 @@ static struct rftype res_common_files[] = {
.kf_ops = &rdtgroup_kf_single_ops,
.seq_show = resctrl_available_mbm_cntrs_show,
},
+ {
+ .name = "mbm_assign_on_mkdir",
+ .mode = 0644,
+ .kf_ops = &rdtgroup_kf_single_ops,
+ .seq_show = resctrl_mbm_assign_on_mkdir_show,
+ .write = resctrl_mbm_assign_on_mkdir_write,
+ },
{
.name = "cpus",
.mode = 0644,
diff --git a/include/linux/resctrl.h b/include/linux/resctrl.h
index 107cb14a0db2..ad3d986c4ea1 100644
--- a/include/linux/resctrl.h
+++ b/include/linux/resctrl.h
@@ -250,6 +250,7 @@ enum resctrl_schema_fmt {
* monitoring events can be configured.
* @num_mbm_cntrs: Number of assignable monitoring counters
* @mbm_cntr_assignable:Is system capable of supporting monitor assignment?
+ * @mbm_assign_on_mkdir:Auto enable monitor assignment on mkdir?
* @evt_list: List of monitoring events
*/
struct resctrl_mon {
@@ -257,6 +258,7 @@ struct resctrl_mon {
unsigned int mbm_cfg_mask;
int num_mbm_cntrs;
bool mbm_cntr_assignable;
+ bool mbm_assign_on_mkdir;
struct list_head evt_list;
};

--
2.34.1