[PATCH v3 06/26] fs-x86/rectrl: Improve domain type checking

Tony Luck
2025-04-07T23:40:50Z

The rdt_domain_hdr structure is used in both control and monitor
domain structures to provide common methods for operations such as
adding a CPU to a domain, removing a CPU from a domain, accessing
the mask of all CPUs in a domain.

The "type" field provides a simple check whether a domain is a
control or monitor domain so that programming errors operating
on domains will be quickly caught.

To prepare for additional domain types that depend on the rdt_resource
to which they are connected add the resource id into the type.

Signed-off-by: Tony Luck <tony.luck@intel.com>
---
include/linux/resctrl.h | 19 ++++++++++---------
arch/x86/kernel/cpu/resctrl/core.c | 12 ++++++------
fs/resctrl/ctrlmondata.c | 2 +-
3 files changed, 17 insertions(+), 16 deletions(-)

diff --git a/include/linux/resctrl.h b/include/linux/resctrl.h
index d6a926b6fc0e..177f9879bae1 100644
--- a/include/linux/resctrl.h
+++ b/include/linux/resctrl.h
@@ -111,11 +111,6 @@ struct resctrl_staged_config {
bool have_new_ctrl;
};

-enum resctrl_domain_type {
- RESCTRL_CTRL_DOMAIN,
- RESCTRL_MON_DOMAIN,
-};
-
/**
* struct rdt_domain_hdr - common header for different domain types
* @list: all instances of this resource
@@ -124,12 +119,18 @@ enum resctrl_domain_type {
* @cpu_mask: which CPUs share this resource
*/
struct rdt_domain_hdr {
- struct list_head list;
- int id;
- enum resctrl_domain_type type;
- struct cpumask cpu_mask;
+ struct list_head list;
+ int id;
+ u32 type;
+ struct cpumask cpu_mask;
};

+/* Bitfields in rdt_domain_hdr.type */
+#define DOMTYPE_RID GENMASK(8, 0)
+#define DOMTYPE_CTRL BIT(9)
+#define DOMTYPE_MON BIT(10)
+#define DOMTYPE(rid, type) (((rid) & DOMTYPE_RID) | (type))
+
/**
* struct rdt_ctrl_domain - group of CPUs sharing a resctrl control resource
* @hdr: common header for different domain types
diff --git a/arch/x86/kernel/cpu/resctrl/core.c b/arch/x86/kernel/cpu/resctrl/core.c
index 6f4a3bd02a42..d82a4a2db699 100644
--- a/arch/x86/kernel/cpu/resctrl/core.c
+++ b/arch/x86/kernel/cpu/resctrl/core.c
@@ -457,7 +457,7 @@ static void domain_add_cpu_ctrl(int cpu, struct rdt_resource *r)

hdr = resctrl_find_domain(&r->ctrl_domains, id, &add_pos);
if (hdr) {
- if (WARN_ON_ONCE(hdr->type != RESCTRL_CTRL_DOMAIN))
+ if (WARN_ON_ONCE(hdr->type != DOMTYPE(r->rid, DOMTYPE_CTRL)))
return;
d = container_of(hdr, struct rdt_ctrl_domain, hdr);

@@ -473,7 +473,7 @@ static void domain_add_cpu_ctrl(int cpu, struct rdt_resource *r)

d = &hw_dom->d_resctrl;
d->hdr.id = id;
- d->hdr.type = RESCTRL_CTRL_DOMAIN;
+ d->hdr.type = DOMTYPE(r->rid, DOMTYPE_CTRL);
cpumask_set_cpu(cpu, &d->hdr.cpu_mask);

rdt_domain_reconfigure_cdp(r);
@@ -512,7 +512,7 @@ static void domain_add_cpu_mon(int cpu, struct rdt_resource *r)

hdr = resctrl_find_domain(&r->mon_domains, id, &add_pos);
if (hdr) {
- if (WARN_ON_ONCE(hdr->type != RESCTRL_MON_DOMAIN))
+ if (WARN_ON_ONCE(hdr->type != DOMTYPE(r->rid, DOMTYPE_MON)))
return;
d = container_of(hdr, struct rdt_mon_domain, hdr);

@@ -526,7 +526,7 @@ static void domain_add_cpu_mon(int cpu, struct rdt_resource *r)

d = &hw_dom->d_resctrl;
d->hdr.id = id;
- d->hdr.type = RESCTRL_MON_DOMAIN;
+ d->hdr.type = DOMTYPE(r->rid, DOMTYPE_MON);
d->ci = get_cpu_cacheinfo_level(cpu, RESCTRL_L3_CACHE);
if (!d->ci) {
pr_warn_once("Can't find L3 cache for CPU:%d resource %s\n", cpu, r->name);
@@ -582,7 +582,7 @@ static void domain_remove_cpu_ctrl(int cpu, struct rdt_resource *r)
return;
}

- if (WARN_ON_ONCE(hdr->type != RESCTRL_CTRL_DOMAIN))
+ if (WARN_ON_ONCE(hdr->type != DOMTYPE(r->rid, DOMTYPE_CTRL)))
return;

d = container_of(hdr, struct rdt_ctrl_domain, hdr);
@@ -628,7 +628,7 @@ static void domain_remove_cpu_mon(int cpu, struct rdt_resource *r)
return;
}

- if (WARN_ON_ONCE(hdr->type != RESCTRL_MON_DOMAIN))
+ if (WARN_ON_ONCE(hdr->type != DOMTYPE(r->rid, DOMTYPE_MON)))
return;

d = container_of(hdr, struct rdt_mon_domain, hdr);
diff --git a/fs/resctrl/ctrlmondata.c b/fs/resctrl/ctrlmondata.c
index ce02e961a6c3..0c245af0ff42 100644
--- a/fs/resctrl/ctrlmondata.c
+++ b/fs/resctrl/ctrlmondata.c
@@ -614,7 +614,7 @@ int rdtgroup_mondata_show(struct seq_file *m, void *arg)
* the resource to find the domain with "domid".
*/
hdr = resctrl_find_domain(&r->mon_domains, domid, NULL);
- if (!hdr || WARN_ON_ONCE(hdr->type != RESCTRL_MON_DOMAIN)) {
+ if (!hdr || WARN_ON_ONCE(hdr->type != DOMTYPE(r->rid, DOMTYPE_MON))) {
ret = -ENOENT;
goto out;
}
--
2.48.1