Skip to content

Commit 03eb578

Browse files
aeglbp3tk0v
authored andcommitted
x86,fs/resctrl: Improve domain type checking
Every resctrl resource has a list of domain structures. struct rdt_ctrl_domain and struct rdt_mon_domain both begin with struct rdt_domain_hdr with rdt_domain_hdr::type used in validity checks before accessing the domain of a particular type. Add the resource id to struct rdt_domain_hdr in preparation for a new monitoring domain structure that will be associated with a new monitoring resource. Improve existing domain validity checks with a new helper domain_header_is_valid() that checks both domain type and resource id. domain_header_is_valid() should be used before every call to container_of() that accesses a domain structure. Signed-off-by: Tony Luck <tony.luck@intel.com> Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de> Reviewed-by: Reinette Chatre <reinette.chatre@intel.com> Link: https://lore.kernel.org/20251217172121.12030-1-tony.luck@intel.com
1 parent f8f9c1f commit 03eb578

3 files changed

Lines changed: 16 additions & 5 deletions

File tree

arch/x86/kernel/cpu/resctrl/core.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ static void domain_add_cpu_ctrl(int cpu, struct rdt_resource *r)
464464

465465
hdr = resctrl_find_domain(&r->ctrl_domains, id, &add_pos);
466466
if (hdr) {
467-
if (WARN_ON_ONCE(hdr->type != RESCTRL_CTRL_DOMAIN))
467+
if (!domain_header_is_valid(hdr, RESCTRL_CTRL_DOMAIN, r->rid))
468468
return;
469469
d = container_of(hdr, struct rdt_ctrl_domain, hdr);
470470

@@ -481,6 +481,7 @@ static void domain_add_cpu_ctrl(int cpu, struct rdt_resource *r)
481481
d = &hw_dom->d_resctrl;
482482
d->hdr.id = id;
483483
d->hdr.type = RESCTRL_CTRL_DOMAIN;
484+
d->hdr.rid = r->rid;
484485
cpumask_set_cpu(cpu, &d->hdr.cpu_mask);
485486

486487
rdt_domain_reconfigure_cdp(r);
@@ -520,7 +521,7 @@ static void domain_add_cpu_mon(int cpu, struct rdt_resource *r)
520521

521522
hdr = resctrl_find_domain(&r->mon_domains, id, &add_pos);
522523
if (hdr) {
523-
if (WARN_ON_ONCE(hdr->type != RESCTRL_MON_DOMAIN))
524+
if (!domain_header_is_valid(hdr, RESCTRL_MON_DOMAIN, r->rid))
524525
return;
525526
d = container_of(hdr, struct rdt_mon_domain, hdr);
526527

@@ -538,6 +539,7 @@ static void domain_add_cpu_mon(int cpu, struct rdt_resource *r)
538539
d = &hw_dom->d_resctrl;
539540
d->hdr.id = id;
540541
d->hdr.type = RESCTRL_MON_DOMAIN;
542+
d->hdr.rid = r->rid;
541543
ci = get_cpu_cacheinfo_level(cpu, RESCTRL_L3_CACHE);
542544
if (!ci) {
543545
pr_warn_once("Can't find L3 cache for CPU:%d resource %s\n", cpu, r->name);
@@ -598,7 +600,7 @@ static void domain_remove_cpu_ctrl(int cpu, struct rdt_resource *r)
598600
return;
599601
}
600602

601-
if (WARN_ON_ONCE(hdr->type != RESCTRL_CTRL_DOMAIN))
603+
if (!domain_header_is_valid(hdr, RESCTRL_CTRL_DOMAIN, r->rid))
602604
return;
603605

604606
d = container_of(hdr, struct rdt_ctrl_domain, hdr);
@@ -644,7 +646,7 @@ static void domain_remove_cpu_mon(int cpu, struct rdt_resource *r)
644646
return;
645647
}
646648

647-
if (WARN_ON_ONCE(hdr->type != RESCTRL_MON_DOMAIN))
649+
if (!domain_header_is_valid(hdr, RESCTRL_MON_DOMAIN, r->rid))
648650
return;
649651

650652
d = container_of(hdr, struct rdt_mon_domain, hdr);

fs/resctrl/ctrlmondata.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ int rdtgroup_mondata_show(struct seq_file *m, void *arg)
653653
* the resource to find the domain with "domid".
654654
*/
655655
hdr = resctrl_find_domain(&r->mon_domains, domid, NULL);
656-
if (!hdr || WARN_ON_ONCE(hdr->type != RESCTRL_MON_DOMAIN)) {
656+
if (!hdr || !domain_header_is_valid(hdr, RESCTRL_MON_DOMAIN, resid)) {
657657
ret = -ENOENT;
658658
goto out;
659659
}

include/linux/resctrl.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,24 @@ enum resctrl_domain_type {
131131
* @list: all instances of this resource
132132
* @id: unique id for this instance
133133
* @type: type of this instance
134+
* @rid: resource id for this instance
134135
* @cpu_mask: which CPUs share this resource
135136
*/
136137
struct rdt_domain_hdr {
137138
struct list_head list;
138139
int id;
139140
enum resctrl_domain_type type;
141+
enum resctrl_res_level rid;
140142
struct cpumask cpu_mask;
141143
};
142144

145+
static inline bool domain_header_is_valid(struct rdt_domain_hdr *hdr,
146+
enum resctrl_domain_type type,
147+
enum resctrl_res_level rid)
148+
{
149+
return !WARN_ON_ONCE(hdr->type != type || hdr->rid != rid);
150+
}
151+
143152
/**
144153
* struct rdt_ctrl_domain - group of CPUs sharing a resctrl control resource
145154
* @hdr: common header for different domain types

0 commit comments

Comments
 (0)