[PATCH v12 24/26] x86/resctrl: Introduce the interface to modify assignments in a group

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


Introduce an interface to modify assignments within a group.

Modifications follow this format:
<Event configuration>:<Domain id>=<Assignment type>

The assignment type can be one of the following:

_ : No event configuration assigned

e : Event configuration assigned in exclusive mode

Domain id can be any valid domain ID number or '*' to update all the
domains.

Example:
$cd /sys/fs/resctrl
$cat mbm_L3_assignments
mbm_total_bytes:0=e;1=e
mbm_local_bytes:0=e;1=e

To unassign the configuration of mbm_total_bytes on domain 0:

$echo "mbm_total_bytes:0=_" > mbm_L3_assignments
$cat mbm_L3_assignments
mbm_total_bytes:0=_;1=e
mbm_local_bytes:0=e;1=e

To unassign the mbm_total_bytes configuration on all domains:

$echo "mbm_total_bytes:*=_" > mbm_L3_assignments
$cat mbm_L3_assignments
mbm_total_bytes:0=_;1=_
mbm_local_bytes:0=e;1=e

Signed-off-by: Babu Moger <babu.moger@xxxxxxx>
---
v12: New patch:
Assignment interface moved inside the group based the discussion
https://lore.kernel.org/lkml/CALPaoCiii0vXOF06mfV=kVLBzhfNo0SFqt4kQGwGSGVUqvr2Dg@xxxxxxxxxxxxxx/#t
---
Documentation/arch/x86/resctrl.rst | 29 ++++-
arch/x86/kernel/cpu/resctrl/internal.h | 9 ++
arch/x86/kernel/cpu/resctrl/rdtgroup.c | 162 ++++++++++++++++++++++++-
3 files changed, 198 insertions(+), 2 deletions(-)

diff --git a/Documentation/arch/x86/resctrl.rst b/Documentation/arch/x86/resctrl.rst
index 31085b4e15f6..ad35c38eed34 100644
--- a/Documentation/arch/x86/resctrl.rst
+++ b/Documentation/arch/x86/resctrl.rst
@@ -514,7 +514,7 @@ When the "mba_MBps" mount option is used all CTRL_MON groups will also contain:
Event configuration: A valid event configuration listed in the
/sys/fs/resctrl/info/L3_MON/counter_configs directory.

- Domain ID: A valid domain ID number.
+ Domain ID: A valid domain ID number or '*' to update all the domains.

Assignment types:

@@ -530,6 +530,33 @@ When the "mba_MBps" mount option is used all CTRL_MON groups will also contain:
mbm_total_bytes:0=e;1=e
mbm_local_bytes:0=e;1=e

+ The assignments can be modified by writing to the interface.
+
+ Example:
+ To unassign the configuration of mbm_total_bytes on domain 0:
+ ::
+
+ # echo "mbm_total_bytes:0=_" > mbm_L3_assignments
+ # cat mbm_L3_assignments
+ mbm_total_bytes:0=_;1=e
+ mbm_local_bytes:0=e;1=e
+
+ To unassign the mbm_total_bytes configuration on all domains:
+ ::
+
+ # echo "mbm_total_bytes:*=_" > mbm_L3_assignments
+ # cat mbm_L3_assignments
+ mbm_total_bytes:0=_;1=_
+ mbm_local_bytes:0=e;1=e
+
+ To assign the mbm_total_bytes configuration on all domains in exclusive mode:
+ ::
+
+ # echo "mbm_total_bytes:*=e" > mbm_L3_assignments
+ # cat mbm_L3_assignments
+ mbm_total_bytes:0=e;1=e
+ mbm_local_bytes:0=e;1=e
+
Resource allocation rules
-------------------------

diff --git a/arch/x86/kernel/cpu/resctrl/internal.h b/arch/x86/kernel/cpu/resctrl/internal.h
index a943450bf2c8..2020a2fe7135 100644
--- a/arch/x86/kernel/cpu/resctrl/internal.h
+++ b/arch/x86/kernel/cpu/resctrl/internal.h
@@ -44,6 +44,15 @@
#define ABMC_EXTENDED_EVT_ID BIT(31)
#define ABMC_EVT_ID 1

+/*
+ * Assignment types for mbm_cntr_assign mode
+ */
+enum {
+ ASSIGN_NONE = 0,
+ ASSIGN_EXCLUSIVE,
+ ASSIGN_INVALID,
+};
+
/**
* cpumask_any_housekeeping() - Choose any CPU in @mask, preferring those that
* aren't marked nohz_full
diff --git a/arch/x86/kernel/cpu/resctrl/rdtgroup.c b/arch/x86/kernel/cpu/resctrl/rdtgroup.c
index ee1c949c2436..5d9c4c216522 100644
--- a/arch/x86/kernel/cpu/resctrl/rdtgroup.c
+++ b/arch/x86/kernel/cpu/resctrl/rdtgroup.c
@@ -84,6 +84,18 @@ static struct mbm_assign_config *mbm_get_assign_config(enum resctrl_event_id evt
return NULL;
}

+static struct mbm_assign_config *mbm_get_assign_config_by_name(char *config)
+{
+ int i;
+
+ for (i = 0; i < NUM_MBM_ASSIGN_CONFIGS; i++) {
+ if (!strcmp(mbm_assign_configs[i].name, config))
+ return &mbm_assign_configs[i];
+ }
+
+ return NULL;
+}
+
/*
* Used to store the max resource name width to display the schemata names in
* a tabular format.
@@ -2125,6 +2137,153 @@ static int mbm_L3_assignments_show(struct kernfs_open_file *of, struct seq_file
return ret;
}

+static unsigned int resctrl_get_assing_type(char *assign)
+{
+ unsigned int mon_state = ASSIGN_NONE;
+ int len = strlen(assign);
+
+ if (!len || len > 1)
+ return ASSIGN_INVALID;
+
+ switch (*assign) {
+ case 'e':
+ mon_state = ASSIGN_EXCLUSIVE;
+ break;
+ case '_':
+ mon_state = ASSIGN_NONE;
+ break;
+ default:
+ mon_state = ASSIGN_INVALID;
+ break;
+ }
+
+ return mon_state;
+}
+
+static int resctrl_process_assign(struct rdt_resource *r, struct rdtgroup *rdtgrp,
+ char *config, char *tok)
+{
+ struct mbm_assign_config *assign_config;
+ struct rdt_mon_domain *d;
+ char *dom_str, *id_str;
+ unsigned long dom_id = 0;
+ int assign_type;
+ char domain[10];
+ bool found;
+ int ret;
+
+ assign_config = mbm_get_assign_config_by_name(config);
+ if (!assign_config) {
+ rdt_last_cmd_printf("Invalid assign configuration %s\n", config);
+ return -ENOENT;
+ }
+
+next:
+ if (!tok || tok[0] == '\0')
+ return 0;
+
+ /* Start processing the strings for each domain */
+ dom_str = strim(strsep(&tok, ";"));
+
+ id_str = strsep(&dom_str, "=");
+
+ /* Check for domain id '*' which means all domains */
+ if (id_str && *id_str == '*') {
+ d = NULL;
+ goto check_state;
+ } else if (!id_str || kstrtoul(id_str, 10, &dom_id)) {
+ rdt_last_cmd_puts("Missing domain id\n");
+ return -EINVAL;
+ }
+
+ /* Verify if the dom_id is valid */
+ found = false;
+ list_for_each_entry(d, &r->mon_domains, hdr.list) {
+ if (d->hdr.id == dom_id) {
+ found = true;
+ break;
+ }
+ }
+
+ if (!found) {
+ rdt_last_cmd_printf("Invalid domain id %ld\n", dom_id);
+ return -EINVAL;
+ }
+
+check_state:
+ assign_type = resctrl_get_assing_type(dom_str);
+
+ switch (assign_type) {
+ case ASSIGN_NONE:
+ ret = resctrl_unassign_cntr_event(r, d, rdtgrp, assign_config->evtid,
+ assign_config->val);
+ break;
+ case ASSIGN_EXCLUSIVE:
+ ret = resctrl_assign_cntr_event(r, d, rdtgrp, assign_config->evtid,
+ assign_config->val);
+ break;
+ case ASSIGN_INVALID:
+ ret = -EINVAL;
+ }
+
+ if (ret)
+ goto out_fail;
+
+ goto next;
+
+out_fail:
+ sprintf(domain, d ? "%ld" : "*", dom_id);
+
+ rdt_last_cmd_printf("Assign operation '%s:%s=%s' failed\n", config, domain, dom_str);
+
+ return ret;
+}
+
+static ssize_t mbm_L3_assignments_write(struct kernfs_open_file *of, char *buf,
+ size_t nbytes, loff_t off)
+{
+ struct rdt_resource *r = resctrl_arch_get_resource(RDT_RESOURCE_L3);
+ struct rdtgroup *rdtgrp;
+ char *token, *config;
+ int ret = 0;
+
+ /* Valid input requires a trailing newline */
+ if (nbytes == 0 || buf[nbytes - 1] != '\n')
+ return -EINVAL;
+
+ buf[nbytes - 1] = '\0';
+
+ rdtgrp = rdtgroup_kn_lock_live(of->kn);
+ if (!rdtgrp) {
+ rdtgroup_kn_unlock(of->kn);
+ return -ENOENT;
+ }
+ rdt_last_cmd_clear();
+
+ if (!resctrl_arch_mbm_cntr_assign_enabled(r)) {
+ rdt_last_cmd_puts("mbm_cntr_assign mode is not enabled\n");
+ rdtgroup_kn_unlock(of->kn);
+ return -EINVAL;
+ }
+
+ while ((token = strsep(&buf, "\n")) != NULL) {
+ /*
+ * The write command follows the following format:
+ * “<Assign config>:<domain_id>=<assign mode>”
+ * Extract Assign config first.
+ */
+ config = strsep(&token, ":");
+
+ ret = resctrl_process_assign(r, rdtgrp, config, token);
+ if (ret)
+ break;
+ }
+
+ rdtgroup_kn_unlock(of->kn);
+
+ return ret ?: nbytes;
+}
+
/* rdtgroup information files for one cache resource. */
static struct rftype res_common_files[] = {
{
@@ -2246,9 +2405,10 @@ static struct rftype res_common_files[] = {
},
{
.name = "mbm_L3_assignments",
- .mode = 0444,
+ .mode = 0644,
.kf_ops = &rdtgroup_kf_single_ops,
.seq_show = mbm_L3_assignments_show,
+ .write = mbm_L3_assignments_write,
},
{
.name = "mbm_assign_mode",
--
2.34.1