2024-05-03 20:34:27

by Luck, Tony

[permalink] [raw]
Subject: [PATCH v17 0/9] Add support for Sub-NUMA cluster (SNC) systems

Note: Jump straight to patch 7 for the new stuff. Just minor tweaks in
other patches.

This series based on top of TIP x86/cache branch:
931be446c6cb ("x86/resctrl: Add tracepoint for llc_occupancy tracking")

The Sub-NUMA cluster feature on some Intel processors partitions the CPUs
that share an L3 cache into two or more sets. This plays havoc with the
Resource Director Technology (RDT) monitoring features. Prior to this
patch Intel has advised that SNC and RDT are incompatible.

Some of these CPUs support an MSR that can partition the RMID counters
in the same way. This allows monitoring features to be used. Legacy
monitoring files provide the sum of counters from each SNC node for
backwards compatibility. Additional files per SNC node provide details
per node.

Cache and memory bandwidth allocation features continue to operate at
the scope of the L3 cache.

Signed-off-by: Tony Luck <[email protected]>

---
Changes since v16: https://lore.kernel.org/all/[email protected]/

Patch 1: Reinette pointed out that rdt_find_domain() no longer returns ERR_PTR()
but one of the callers was still checking return with IS_ERR().

Patch 2: Tip tree added a tracing patch. That needed s/d->id/d->hdr.id/

Patch 3: Reinette: Keep the "RCU" in the kerneldoc description of the
domain list fields after the split into separate ctrl/mon lists.

Patch 4: No change

Patch 5: No change

Patch 6: Drop the change that divided output of the resctrl "size" file
by the number of SNC domains per L3 cache. Now that this series
preserves the contents of the legacy llc_occupancy files this isn't
useful.

Patch 7: NEW in this series. Add per-SNC domain monitor files while
making the original files sum across SNC nodes.

Patch 8: (formerly 7) No change

Patch 9: (formerly 8) Add documentation for new per-SNC directories and files

Tony Luck (9):
x86/resctrl: Prepare for new domain scope
x86/resctrl: Prepare to split rdt_domain structure
x86/resctrl: Prepare for different scope for control/monitor
operations
x86/resctrl: Split the rdt_domain and rdt_hw_domain structures
x86/resctrl: Add node-scope to the options for feature scope
x86/resctrl: Introduce snc_nodes_per_l3_cache
x86/resctrl: Add new monitor files for Sub-NUMA cluster (SNC)
monitoring
x86/resctrl: Sub NUMA Cluster detection and enable
x86/resctrl: Update documentation with Sub-NUMA cluster changes

Documentation/arch/x86/resctrl.rst | 17 +
include/linux/resctrl.h | 89 +++--
arch/x86/include/asm/msr-index.h | 1 +
arch/x86/kernel/cpu/resctrl/internal.h | 72 ++--
arch/x86/kernel/cpu/resctrl/core.c | 430 ++++++++++++++++++----
arch/x86/kernel/cpu/resctrl/ctrlmondata.c | 57 +--
arch/x86/kernel/cpu/resctrl/monitor.c | 98 +++--
arch/x86/kernel/cpu/resctrl/pseudo_lock.c | 26 +-
arch/x86/kernel/cpu/resctrl/rdtgroup.c | 263 ++++++++-----
9 files changed, 759 insertions(+), 294 deletions(-)


base-commit: 931be446c6cbc15691dd499957e961f4e1d56afb
--
2.44.0



2024-05-03 20:34:37

by Luck, Tony

[permalink] [raw]
Subject: [PATCH v17 7/9] x86/resctrl: Add new monitor files for Sub-NUMA cluster (SNC) monitoring

Add a field to the rdt_resource structure to track whether monitoring
resources are tracked by hardware at a different scope (NODE) from
the legacy L3 scope.

Add a field to the rdt_mon_domain structure to track the L3 cache id
which can be used to find all the domains that need resource counts
summed to provide accurate values in the legacy monitoring files.

When SNC is enabled create extra directories and files in each mon_data
directory to report per-SNC node counts.

Signed-off-by: Tony Luck <[email protected]>
---
include/linux/resctrl.h | 4 +
arch/x86/kernel/cpu/resctrl/internal.h | 5 +-
arch/x86/kernel/cpu/resctrl/core.c | 2 +
arch/x86/kernel/cpu/resctrl/ctrlmondata.c | 1 +
arch/x86/kernel/cpu/resctrl/monitor.c | 52 +++++++---
arch/x86/kernel/cpu/resctrl/rdtgroup.c | 115 +++++++++++++++++-----
6 files changed, 137 insertions(+), 42 deletions(-)

diff --git a/include/linux/resctrl.h b/include/linux/resctrl.h
index 5c7775343c3e..2f8ac925bc18 100644
--- a/include/linux/resctrl.h
+++ b/include/linux/resctrl.h
@@ -96,6 +96,7 @@ struct rdt_ctrl_domain {
/**
* struct rdt_mon_domain - group of CPUs sharing a resctrl monitor resource
* @hdr: common header for different domain types
+ * @display_id: shared id used to identify domains to be summed for display
* @rmid_busy_llc: bitmap of which limbo RMIDs are above threshold
* @mbm_total: saved state for MBM total bandwidth
* @mbm_local: saved state for MBM local bandwidth
@@ -106,6 +107,7 @@ struct rdt_ctrl_domain {
*/
struct rdt_mon_domain {
struct rdt_domain_hdr hdr;
+ int display_id;
unsigned long *rmid_busy_llc;
struct mbm_state *mbm_total;
struct mbm_state *mbm_local;
@@ -187,6 +189,7 @@ enum resctrl_scope {
* @num_rmid: Number of RMIDs available
* @ctrl_scope: Scope of this resource for control functions
* @mon_scope: Scope of this resource for monitor functions
+ * @mon_display_scope: Scope for user reporting monitor functions
* @cache: Cache allocation related data
* @membw: If the component has bandwidth controls, their properties.
* @ctrl_domains: RCU list of all control domains for this resource
@@ -207,6 +210,7 @@ struct rdt_resource {
int num_rmid;
enum resctrl_scope ctrl_scope;
enum resctrl_scope mon_scope;
+ enum resctrl_scope mon_display_scope;
struct resctrl_cache cache;
struct resctrl_membw membw;
struct list_head ctrl_domains;
diff --git a/arch/x86/kernel/cpu/resctrl/internal.h b/arch/x86/kernel/cpu/resctrl/internal.h
index 49440f194253..d41b388bb499 100644
--- a/arch/x86/kernel/cpu/resctrl/internal.h
+++ b/arch/x86/kernel/cpu/resctrl/internal.h
@@ -132,6 +132,7 @@ struct mon_evt {
* as kernfs private data
* @rid: Resource id associated with the event file
* @evtid: Event id associated with the event file
+ * @sum: Sum across domains with same display_id
* @domid: The domain to which the event file belongs
* @u: Name of the bit fields struct
*/
@@ -139,7 +140,8 @@ union mon_data_bits {
void *priv;
struct {
unsigned int rid : 10;
- enum resctrl_event_id evtid : 8;
+ enum resctrl_event_id evtid : 7;
+ unsigned int sum : 1;
unsigned int domid : 14;
} u;
};
@@ -150,6 +152,7 @@ struct rmid_read {
struct rdt_mon_domain *d;
enum resctrl_event_id evtid;
bool first;
+ bool sumdomains;
int err;
u64 val;
void *arch_mon_ctx;
diff --git a/arch/x86/kernel/cpu/resctrl/core.c b/arch/x86/kernel/cpu/resctrl/core.c
index cb181796f73b..a949e69308cd 100644
--- a/arch/x86/kernel/cpu/resctrl/core.c
+++ b/arch/x86/kernel/cpu/resctrl/core.c
@@ -71,6 +71,7 @@ struct rdt_hw_resource rdt_resources_all[] = {
.name = "L3",
.ctrl_scope = RESCTRL_L3_CACHE,
.mon_scope = RESCTRL_L3_CACHE,
+ .mon_display_scope = RESCTRL_L3_CACHE,
.ctrl_domains = ctrl_domain_init(RDT_RESOURCE_L3),
.mon_domains = mon_domain_init(RDT_RESOURCE_L3),
.parse_ctrlval = parse_cbm,
@@ -613,6 +614,7 @@ static void domain_add_cpu_mon(int cpu, struct rdt_resource *r)

d = &hw_dom->d_resctrl;
d->hdr.id = id;
+ d->display_id = get_domain_id_from_scope(cpu, r->mon_display_scope);
d->hdr.type = RESCTRL_MON_DOMAIN;
cpumask_set_cpu(cpu, &d->hdr.cpu_mask);

diff --git a/arch/x86/kernel/cpu/resctrl/ctrlmondata.c b/arch/x86/kernel/cpu/resctrl/ctrlmondata.c
index 3b9383612c35..a4ead8ffbaf3 100644
--- a/arch/x86/kernel/cpu/resctrl/ctrlmondata.c
+++ b/arch/x86/kernel/cpu/resctrl/ctrlmondata.c
@@ -575,6 +575,7 @@ int rdtgroup_mondata_show(struct seq_file *m, void *arg)
resid = md.u.rid;
domid = md.u.domid;
evtid = md.u.evtid;
+ rr.sumdomains = md.u.sum;

r = &rdt_resources_all[resid].r_resctrl;
hdr = rdt_find_domain(&r->mon_domains, domid, NULL);
diff --git a/arch/x86/kernel/cpu/resctrl/monitor.c b/arch/x86/kernel/cpu/resctrl/monitor.c
index d0bbeb410750..2e795b261b6f 100644
--- a/arch/x86/kernel/cpu/resctrl/monitor.c
+++ b/arch/x86/kernel/cpu/resctrl/monitor.c
@@ -16,6 +16,7 @@
*/

#include <linux/cpu.h>
+#include <linux/cacheinfo.h>
#include <linux/module.h>
#include <linux/sizes.h>
#include <linux/slab.h>
@@ -187,18 +188,8 @@ static inline struct rmid_entry *__rmid_entry(u32 idx)

static int __rmid_read(u32 rmid, enum resctrl_event_id eventid, u64 *val)
{
- struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
- int cpu = smp_processor_id();
- int rmid_offset = 0;
u64 msr_val;

- /*
- * When SNC mode is on, need to compute the offset to read the
- * physical RMID counter for the node to which this CPU belongs.
- */
- if (snc_nodes_per_l3_cache > 1)
- rmid_offset = (cpu_to_node(cpu) % snc_nodes_per_l3_cache) * r->num_rmid;
-
/*
* As per the SDM, when IA32_QM_EVTSEL.EvtID (bits 7:0) is configured
* with a valid event code for supported resource type and the bits
@@ -207,7 +198,7 @@ static int __rmid_read(u32 rmid, enum resctrl_event_id eventid, u64 *val)
* IA32_QM_CTR.Error (bit 63) and IA32_QM_CTR.Unavailable (bit 62)
* are error bits.
*/
- wrmsr(MSR_IA32_QM_EVTSEL, eventid, rmid + rmid_offset);
+ wrmsr(MSR_IA32_QM_EVTSEL, eventid, rmid);
rdmsrl(MSR_IA32_QM_CTR, msr_val);

if (msr_val & RMID_VAL_ERROR)
@@ -291,7 +282,7 @@ int resctrl_arch_rmid_read(struct rdt_resource *r, struct rdt_mon_domain *d,

resctrl_arch_rmid_read_context_check();

- if (!cpumask_test_cpu(smp_processor_id(), &d->hdr.cpu_mask))
+ if (d->display_id != get_cpu_cacheinfo_id(smp_processor_id(), r->mon_display_scope))
return -EINVAL;

ret = __rmid_read(rmid, eventid, &msr_val);
@@ -556,7 +547,7 @@ static struct mbm_state *get_mbm_state(struct rdt_mon_domain *d, u32 closid,
}
}

-static int __mon_event_count(u32 closid, u32 rmid, struct rmid_read *rr)
+static int ___mon_event_count(u32 closid, u32 rmid, struct rmid_read *rr, u64 *rrval)
{
struct mbm_state *m;
u64 tval = 0;
@@ -574,11 +565,44 @@ static int __mon_event_count(u32 closid, u32 rmid, struct rmid_read *rr)
if (rr->err)
return rr->err;

- rr->val += tval;
+ *rrval += tval;

return 0;
}

+static u32 get_node_rmid(struct rdt_resource *r, struct rdt_mon_domain *d, u32 rmid)
+{
+ int cpu = cpumask_any(&d->hdr.cpu_mask);
+
+ return rmid + (cpu_to_node(cpu) % snc_nodes_per_l3_cache) * r->num_rmid;
+}
+
+static int __mon_event_count(u32 closid, u32 rmid, struct rmid_read *rr)
+{
+ struct rdt_mon_domain *d;
+ struct rmid_read tmp;
+ u32 node_rmid;
+ int ret = 0;
+
+ if (!rr->sumdomains) {
+ node_rmid = get_node_rmid(rr->r, rr->d, rmid);
+ return ___mon_event_count(closid, node_rmid, rr, &rr->val);
+ }
+
+ tmp = *rr;
+ list_for_each_entry(d, &rr->r->mon_domains, hdr.list) {
+ if (d->display_id == rr->d->display_id) {
+ tmp.d = d;
+ node_rmid = get_node_rmid(rr->r, d, rmid);
+ ret = ___mon_event_count(closid, node_rmid, &tmp, &rr->val);
+ if (ret)
+ break;
+ }
+ }
+
+ return ret;
+}
+
/*
* mbm_bw_count() - Update bw count from values previously read by
* __mon_event_count().
diff --git a/arch/x86/kernel/cpu/resctrl/rdtgroup.c b/arch/x86/kernel/cpu/resctrl/rdtgroup.c
index 0923492a8bd0..a56ae08ca255 100644
--- a/arch/x86/kernel/cpu/resctrl/rdtgroup.c
+++ b/arch/x86/kernel/cpu/resctrl/rdtgroup.c
@@ -3011,57 +3011,118 @@ static int mon_addfile(struct kernfs_node *parent_kn, const char *name,
* and monitor groups with given domain id.
*/
static void rmdir_mondata_subdir_allrdtgrp(struct rdt_resource *r,
- unsigned int dom_id)
+ struct rdt_mon_domain *d)
{
struct rdtgroup *prgrp, *crgrp;
+ struct rdt_mon_domain *dom;
+ bool remove_all = true;
+ struct kernfs_node *kn;
+ char subname[32];
char name[32];

+ sprintf(name, "mon_%s_%02d", r->name, d->display_id);
+ if (r->mon_scope != r->mon_display_scope) {
+ int count = 0;
+
+ list_for_each_entry(dom, &r->mon_domains, hdr.list)
+ if (d->display_id == dom->display_id)
+ count++;
+ if (count > 1) {
+ remove_all = false;
+ sprintf(subname, "mon_sub_%s_%02d", r->name, d->hdr.id);
+ }
+ }
+
list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
- sprintf(name, "mon_%s_%02d", r->name, dom_id);
- kernfs_remove_by_name(prgrp->mon.mon_data_kn, name);
+ if (remove_all) {
+ kernfs_remove_by_name(prgrp->mon.mon_data_kn, name);
+ } else {
+ kn = kernfs_find_and_get_ns(prgrp->mon.mon_data_kn, name, NULL);
+ if (kn)
+ kernfs_remove_by_name(kn, subname);
+ }

- list_for_each_entry(crgrp, &prgrp->mon.crdtgrp_list, mon.crdtgrp_list)
- kernfs_remove_by_name(crgrp->mon.mon_data_kn, name);
+ list_for_each_entry(crgrp, &prgrp->mon.crdtgrp_list, mon.crdtgrp_list) {
+ if (remove_all) {
+ kernfs_remove_by_name(crgrp->mon.mon_data_kn, name);
+ } else {
+ kn = kernfs_find_and_get_ns(prgrp->mon.mon_data_kn, name, NULL);
+ if (kn)
+ kernfs_remove_by_name(kn, subname);
+ }
+ }
}
}

-static int mkdir_mondata_subdir(struct kernfs_node *parent_kn,
- struct rdt_mon_domain *d,
- struct rdt_resource *r, struct rdtgroup *prgrp)
+static int mon_add_all_files(struct kernfs_node *kn, struct rdt_mon_domain *d,
+ struct rdt_resource *r, struct rdtgroup *prgrp,
+ bool do_sum)
{
union mon_data_bits priv;
- struct kernfs_node *kn;
struct mon_evt *mevt;
struct rmid_read rr;
- char name[32];
int ret;

- sprintf(name, "mon_%s_%02d", r->name, d->hdr.id);
- /* create the directory */
- kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
- if (IS_ERR(kn))
- return PTR_ERR(kn);
-
- ret = rdtgroup_kn_set_ugid(kn);
- if (ret)
- goto out_destroy;
-
- if (WARN_ON(list_empty(&r->evt_list))) {
- ret = -EPERM;
- goto out_destroy;
- }
+ if (WARN_ON(list_empty(&r->evt_list)))
+ return -EPERM;

priv.u.rid = r->rid;
priv.u.domid = d->hdr.id;
+ priv.u.sum = do_sum;
list_for_each_entry(mevt, &r->evt_list, list) {
priv.u.evtid = mevt->evtid;
ret = mon_addfile(kn, mevt->name, priv.priv);
if (ret)
- goto out_destroy;
+ return ret;

if (is_mbm_event(mevt->evtid))
mon_event_read(&rr, r, d, prgrp, mevt->evtid, true);
}
+
+ return 0;
+}
+
+static int mkdir_mondata_subdir(struct kernfs_node *parent_kn,
+ struct rdt_mon_domain *d,
+ struct rdt_resource *r, struct rdtgroup *prgrp)
+{
+ struct kernfs_node *kn, *ckn;
+ char name[32];
+ bool do_sum;
+ int ret;
+
+ do_sum = r->mon_scope != r->mon_display_scope;
+ sprintf(name, "mon_%s_%02d", r->name, d->display_id);
+ kn = kernfs_find_and_get_ns(parent_kn, name, NULL);
+ if (!kn) {
+ /* create the directory */
+ kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
+ if (IS_ERR(kn))
+ return PTR_ERR(kn);
+
+ ret = rdtgroup_kn_set_ugid(kn);
+ if (ret)
+ goto out_destroy;
+ ret = mon_add_all_files(kn, d, r, prgrp, do_sum);
+ if (ret)
+ goto out_destroy;
+ }
+
+ if (do_sum) {
+ sprintf(name, "mon_sub_%s_%02d", r->name, d->hdr.id);
+ ckn = kernfs_create_dir(kn, name, parent_kn->mode, prgrp);
+ if (IS_ERR(ckn))
+ goto out_destroy;
+
+ ret = rdtgroup_kn_set_ugid(ckn);
+ if (ret)
+ goto out_destroy;
+
+ ret = mon_add_all_files(ckn, d, r, prgrp, false);
+ if (ret)
+ goto out_destroy;
+ }
+
kernfs_activate(kn);
return 0;

@@ -3077,8 +3138,8 @@ static int mkdir_mondata_subdir(struct kernfs_node *parent_kn,
static void mkdir_mondata_subdir_allrdtgrp(struct rdt_resource *r,
struct rdt_mon_domain *d)
{
- struct kernfs_node *parent_kn;
struct rdtgroup *prgrp, *crgrp;
+ struct kernfs_node *parent_kn;
struct list_head *head;

list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
@@ -3950,7 +4011,7 @@ void resctrl_offline_mon_domain(struct rdt_resource *r, struct rdt_mon_domain *d
* per domain monitor data directories.
*/
if (resctrl_mounted && resctrl_arch_mon_capable())
- rmdir_mondata_subdir_allrdtgrp(r, d->hdr.id);
+ rmdir_mondata_subdir_allrdtgrp(r, d);

if (is_mbm_enabled())
cancel_delayed_work(&d->mbm_over);
--
2.44.0


2024-05-03 20:34:49

by Luck, Tony

[permalink] [raw]
Subject: [PATCH v17 8/9] x86/resctrl: Sub NUMA Cluster detection and enable

There isn't a simple hardware bit that indicates whether a CPU is
running in Sub NUMA Cluster (SNC) mode. Infer the state by comparing
the ratio of NUMA nodes to L3 cache instances.

When SNC mode is detected, reconfigure the RMID counters by updating
the MSR_RMID_SNC_CONFIG MSR on each socket as CPUs are seen.

Clearing bit zero of the MSR divides the RMIDs and renumbers the ones
on the second SNC node to start from zero.

Signed-off-by: Tony Luck <[email protected]>
---
arch/x86/include/asm/msr-index.h | 1 +
arch/x86/kernel/cpu/resctrl/core.c | 119 +++++++++++++++++++++++++++++
2 files changed, 120 insertions(+)

diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-index.h
index e72c2b872957..ce54a1ffe1e5 100644
--- a/arch/x86/include/asm/msr-index.h
+++ b/arch/x86/include/asm/msr-index.h
@@ -1165,6 +1165,7 @@
#define MSR_IA32_QM_CTR 0xc8e
#define MSR_IA32_PQR_ASSOC 0xc8f
#define MSR_IA32_L3_CBM_BASE 0xc90
+#define MSR_RMID_SNC_CONFIG 0xca0
#define MSR_IA32_L2_CBM_BASE 0xd10
#define MSR_IA32_MBA_THRTL_BASE 0xd50

diff --git a/arch/x86/kernel/cpu/resctrl/core.c b/arch/x86/kernel/cpu/resctrl/core.c
index a949e69308cd..6a1727ea1dfe 100644
--- a/arch/x86/kernel/cpu/resctrl/core.c
+++ b/arch/x86/kernel/cpu/resctrl/core.c
@@ -21,7 +21,9 @@
#include <linux/err.h>
#include <linux/cacheinfo.h>
#include <linux/cpuhotplug.h>
+#include <linux/mod_devicetable.h>

+#include <asm/cpu_device_id.h>
#include <asm/intel-family.h>
#include <asm/resctrl.h>
#include "internal.h"
@@ -746,11 +748,42 @@ static void clear_closid_rmid(int cpu)
RESCTRL_RESERVED_CLOSID);
}

+/*
+ * The power-on reset value of MSR_RMID_SNC_CONFIG is 0x1
+ * which indicates that RMIDs are configured in legacy mode.
+ * This mode is incompatible with Linux resctrl semantics
+ * as RMIDs are partitioned between SNC nodes, which requires
+ * a user to know which RMID is allocated to a task.
+ * Clearing bit 0 reconfigures the RMID counters for use
+ * in Sub NUMA Cluster mode. This mode is better for Linux.
+ * The RMID space is divided between all SNC nodes with the
+ * RMIDs renumbered to start from zero in each node when
+ * couning operations from tasks. Code to read the counters
+ * must adjust RMID counter numbers based on SNC node. See
+ * __rmid_read() for code that does this.
+ */
+static void snc_remap_rmids(int cpu)
+{
+ u64 val;
+
+ /* Only need to enable once per package. */
+ if (cpumask_first(topology_core_cpumask(cpu)) != cpu)
+ return;
+
+ rdmsrl(MSR_RMID_SNC_CONFIG, val);
+ val &= ~BIT_ULL(0);
+ wrmsrl(MSR_RMID_SNC_CONFIG, val);
+}
+
static int resctrl_arch_online_cpu(unsigned int cpu)
{
struct rdt_resource *r;

mutex_lock(&domain_list_lock);
+
+ if (snc_nodes_per_l3_cache > 1)
+ snc_remap_rmids(cpu);
+
for_each_capable_rdt_resource(r)
domain_add_cpu(cpu, r);
mutex_unlock(&domain_list_lock);
@@ -990,11 +1023,97 @@ static __init bool get_rdt_resources(void)
return (rdt_mon_capable || rdt_alloc_capable);
}

+/* CPU models that support MSR_RMID_SNC_CONFIG */
+static const struct x86_cpu_id snc_cpu_ids[] __initconst = {
+ X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_X, 0),
+ X86_MATCH_INTEL_FAM6_MODEL(SAPPHIRERAPIDS_X, 0),
+ X86_MATCH_INTEL_FAM6_MODEL(EMERALDRAPIDS_X, 0),
+ X86_MATCH_INTEL_FAM6_MODEL(GRANITERAPIDS_X, 0),
+ X86_MATCH_INTEL_FAM6_MODEL(ATOM_CRESTMONT_X, 0),
+ {}
+};
+
+/*
+ * There isn't a simple hardware bit that indicates whether a CPU is running
+ * in Sub NUMA Cluster (SNC) mode. Infer the state by comparing the
+ * ratio of NUMA nodes to L3 cache instances.
+ * It is not possible to accurately determine SNC state if the system is
+ * booted with a maxcpus=N parameter. That distorts the ratio of SNC nodes
+ * to L3 caches. It will be OK if system is booted with hyperthreading
+ * disabled (since this doesn't affect the ratio).
+ */
+static __init int snc_get_config(void)
+{
+ unsigned long *node_caches;
+ int mem_only_nodes = 0;
+ int cpu, node, ret;
+ int num_l3_caches;
+ int cache_id;
+
+ if (!x86_match_cpu(snc_cpu_ids))
+ return 1;
+
+ node_caches = bitmap_zalloc(num_possible_cpus(), GFP_KERNEL);
+ if (!node_caches)
+ return 1;
+
+ cpus_read_lock();
+
+ if (num_online_cpus() != num_present_cpus())
+ pr_warn("Some CPUs offline, SNC detection may be incorrect\n");
+
+ for_each_node(node) {
+ cpu = cpumask_first(cpumask_of_node(node));
+ if (cpu < nr_cpu_ids) {
+ cache_id = get_cpu_cacheinfo_id(cpu, 3);
+ if (cache_id != -1)
+ set_bit(cache_id, node_caches);
+ } else {
+ mem_only_nodes++;
+ }
+ }
+ cpus_read_unlock();
+
+ num_l3_caches = bitmap_weight(node_caches, num_possible_cpus());
+ kfree(node_caches);
+
+ if (!num_l3_caches)
+ goto insane;
+
+ /* sanity check #1: Number of CPU nodes must be multiple of num_l3_caches */
+ if ((nr_node_ids - mem_only_nodes) % num_l3_caches)
+ goto insane;
+
+ ret = (nr_node_ids - mem_only_nodes) / num_l3_caches;
+
+ /* sanity check #2: Only valid results are 1, 2, 3, 4 */
+ switch (ret) {
+ case 1:
+ break;
+ case 2:
+ case 3:
+ case 4:
+ pr_info("Sub-NUMA cluster detected with %d nodes per L3 cache\n", ret);
+ rdt_resources_all[RDT_RESOURCE_L3].r_resctrl.mon_scope = RESCTRL_NODE;
+ break;
+ default:
+ goto insane;
+ }
+
+ return ret;
+insane:
+ pr_warn("SNC insanity: CPU nodes = %d num_l3_caches = %d\n",
+ (nr_node_ids - mem_only_nodes), num_l3_caches);
+ return 1;
+}
+
static __init void rdt_init_res_defs_intel(void)
{
struct rdt_hw_resource *hw_res;
struct rdt_resource *r;

+ snc_nodes_per_l3_cache = snc_get_config();
+
for_each_rdt_resource(r) {
hw_res = resctrl_to_arch_res(r);

--
2.44.0


2024-05-03 20:35:56

by Luck, Tony

[permalink] [raw]
Subject: [PATCH v17 4/9] x86/resctrl: Split the rdt_domain and rdt_hw_domain structures

The same rdt_domain structure is used for both control and monitor
functions. But this results in wasted memory as some of the fields are
only used by control functions, while most are only used for monitor
functions.

Split into separate rdt_ctrl_domain and rdt_mon_domain structures with
just the fields required for control and monitoring respectively.

Similar split of the rdt_hw_domain structure into rdt_hw_ctrl_domain
and rdt_hw_mon_domain.

Signed-off-by: Tony Luck <[email protected]>
---
include/linux/resctrl.h | 48 ++++++++-------
arch/x86/kernel/cpu/resctrl/internal.h | 62 ++++++++++++--------
arch/x86/kernel/cpu/resctrl/core.c | 71 ++++++++++++-----------
arch/x86/kernel/cpu/resctrl/ctrlmondata.c | 28 ++++-----
arch/x86/kernel/cpu/resctrl/monitor.c | 40 ++++++-------
arch/x86/kernel/cpu/resctrl/pseudo_lock.c | 6 +-
arch/x86/kernel/cpu/resctrl/rdtgroup.c | 64 ++++++++++----------
7 files changed, 174 insertions(+), 145 deletions(-)

diff --git a/include/linux/resctrl.h b/include/linux/resctrl.h
index 96ddf9ff3183..aa2c22a8e37b 100644
--- a/include/linux/resctrl.h
+++ b/include/linux/resctrl.h
@@ -78,7 +78,23 @@ struct rdt_domain_hdr {
};

/**
- * struct rdt_domain - group of CPUs sharing a resctrl resource
+ * struct rdt_ctrl_domain - group of CPUs sharing a resctrl control resource
+ * @hdr: common header for different domain types
+ * @plr: pseudo-locked region (if any) associated with domain
+ * @staged_config: parsed configuration to be applied
+ * @mbps_val: When mba_sc is enabled, this holds the array of user
+ * specified control values for mba_sc in MBps, indexed
+ * by closid
+ */
+struct rdt_ctrl_domain {
+ struct rdt_domain_hdr hdr;
+ struct pseudo_lock_region *plr;
+ struct resctrl_staged_config staged_config[CDP_NUM_TYPES];
+ u32 *mbps_val;
+};
+
+/**
+ * struct rdt_mon_domain - group of CPUs sharing a resctrl monitor resource
* @hdr: common header for different domain types
* @rmid_busy_llc: bitmap of which limbo RMIDs are above threshold
* @mbm_total: saved state for MBM total bandwidth
@@ -87,13 +103,8 @@ struct rdt_domain_hdr {
* @cqm_limbo: worker to periodically read CQM h/w counters
* @mbm_work_cpu: worker CPU for MBM h/w counters
* @cqm_work_cpu: worker CPU for CQM h/w counters
- * @plr: pseudo-locked region (if any) associated with domain
- * @staged_config: parsed configuration to be applied
- * @mbps_val: When mba_sc is enabled, this holds the array of user
- * specified control values for mba_sc in MBps, indexed
- * by closid
*/
-struct rdt_domain {
+struct rdt_mon_domain {
struct rdt_domain_hdr hdr;
unsigned long *rmid_busy_llc;
struct mbm_state *mbm_total;
@@ -102,9 +113,6 @@ struct rdt_domain {
struct delayed_work cqm_limbo;
int mbm_work_cpu;
int cqm_work_cpu;
- struct pseudo_lock_region *plr;
- struct resctrl_staged_config staged_config[CDP_NUM_TYPES];
- u32 *mbps_val;
};

/**
@@ -208,7 +216,7 @@ struct rdt_resource {
const char *format_str;
int (*parse_ctrlval)(struct rdt_parse_data *data,
struct resctrl_schema *s,
- struct rdt_domain *d);
+ struct rdt_ctrl_domain *d);
struct list_head evt_list;
unsigned long fflags;
bool cdp_capable;
@@ -242,15 +250,15 @@ int resctrl_arch_update_domains(struct rdt_resource *r, u32 closid);
* Update the ctrl_val and apply this config right now.
* Must be called on one of the domain's CPUs.
*/
-int resctrl_arch_update_one(struct rdt_resource *r, struct rdt_domain *d,
+int resctrl_arch_update_one(struct rdt_resource *r, struct rdt_ctrl_domain *d,
u32 closid, enum resctrl_conf_type t, u32 cfg_val);

-u32 resctrl_arch_get_config(struct rdt_resource *r, struct rdt_domain *d,
+u32 resctrl_arch_get_config(struct rdt_resource *r, struct rdt_ctrl_domain *d,
u32 closid, enum resctrl_conf_type type);
-int resctrl_online_ctrl_domain(struct rdt_resource *r, struct rdt_domain *d);
-int resctrl_online_mon_domain(struct rdt_resource *r, struct rdt_domain *d);
-void resctrl_offline_ctrl_domain(struct rdt_resource *r, struct rdt_domain *d);
-void resctrl_offline_mon_domain(struct rdt_resource *r, struct rdt_domain *d);
+int resctrl_online_ctrl_domain(struct rdt_resource *r, struct rdt_ctrl_domain *d);
+int resctrl_online_mon_domain(struct rdt_resource *r, struct rdt_mon_domain *d);
+void resctrl_offline_ctrl_domain(struct rdt_resource *r, struct rdt_ctrl_domain *d);
+void resctrl_offline_mon_domain(struct rdt_resource *r, struct rdt_mon_domain *d);
void resctrl_online_cpu(unsigned int cpu);
void resctrl_offline_cpu(unsigned int cpu);

@@ -279,7 +287,7 @@ void resctrl_offline_cpu(unsigned int cpu);
* Return:
* 0 on success, or -EIO, -EINVAL etc on error.
*/
-int resctrl_arch_rmid_read(struct rdt_resource *r, struct rdt_domain *d,
+int resctrl_arch_rmid_read(struct rdt_resource *r, struct rdt_mon_domain *d,
u32 closid, u32 rmid, enum resctrl_event_id eventid,
u64 *val, void *arch_mon_ctx);

@@ -312,7 +320,7 @@ static inline void resctrl_arch_rmid_read_context_check(void)
*
* This can be called from any CPU.
*/
-void resctrl_arch_reset_rmid(struct rdt_resource *r, struct rdt_domain *d,
+void resctrl_arch_reset_rmid(struct rdt_resource *r, struct rdt_mon_domain *d,
u32 closid, u32 rmid,
enum resctrl_event_id eventid);

@@ -325,7 +333,7 @@ void resctrl_arch_reset_rmid(struct rdt_resource *r, struct rdt_domain *d,
*
* This can be called from any CPU.
*/
-void resctrl_arch_reset_rmid_all(struct rdt_resource *r, struct rdt_domain *d);
+void resctrl_arch_reset_rmid_all(struct rdt_resource *r, struct rdt_mon_domain *d);

extern unsigned int resctrl_rmid_realloc_threshold;
extern unsigned int resctrl_rmid_realloc_limit;
diff --git a/arch/x86/kernel/cpu/resctrl/internal.h b/arch/x86/kernel/cpu/resctrl/internal.h
index 377679b79919..135190e0711c 100644
--- a/arch/x86/kernel/cpu/resctrl/internal.h
+++ b/arch/x86/kernel/cpu/resctrl/internal.h
@@ -147,7 +147,7 @@ union mon_data_bits {
struct rmid_read {
struct rdtgroup *rgrp;
struct rdt_resource *r;
- struct rdt_domain *d;
+ struct rdt_mon_domain *d;
enum resctrl_event_id evtid;
bool first;
int err;
@@ -232,7 +232,7 @@ struct mongroup {
*/
struct pseudo_lock_region {
struct resctrl_schema *s;
- struct rdt_domain *d;
+ struct rdt_ctrl_domain *d;
u32 cbm;
wait_queue_head_t lock_thread_wq;
int thread_done;
@@ -355,25 +355,41 @@ struct arch_mbm_state {
};

/**
- * struct rdt_hw_domain - Arch private attributes of a set of CPUs that share
- * a resource
+ * struct rdt_hw_ctrl_domain - Arch private attributes of a set of CPUs that share
+ * a resource for a control function
* @d_resctrl: Properties exposed to the resctrl file system
* @ctrl_val: array of cache or mem ctrl values (indexed by CLOSID)
+ *
+ * Members of this structure are accessed via helpers that provide abstraction.
+ */
+struct rdt_hw_ctrl_domain {
+ struct rdt_ctrl_domain d_resctrl;
+ u32 *ctrl_val;
+};
+
+/**
+ * struct rdt_hw_mon_domain - Arch private attributes of a set of CPUs that share
+ * a resource for a monitor function
+ * @d_resctrl: Properties exposed to the resctrl file system
* @arch_mbm_total: arch private state for MBM total bandwidth
* @arch_mbm_local: arch private state for MBM local bandwidth
*
* Members of this structure are accessed via helpers that provide abstraction.
*/
-struct rdt_hw_domain {
- struct rdt_domain d_resctrl;
- u32 *ctrl_val;
+struct rdt_hw_mon_domain {
+ struct rdt_mon_domain d_resctrl;
struct arch_mbm_state *arch_mbm_total;
struct arch_mbm_state *arch_mbm_local;
};

-static inline struct rdt_hw_domain *resctrl_to_arch_dom(struct rdt_domain *r)
+static inline struct rdt_hw_ctrl_domain *resctrl_to_arch_ctrl_dom(struct rdt_ctrl_domain *r)
+{
+ return container_of(r, struct rdt_hw_ctrl_domain, d_resctrl);
+}
+
+static inline struct rdt_hw_mon_domain *resctrl_to_arch_mon_dom(struct rdt_mon_domain *r)
{
- return container_of(r, struct rdt_hw_domain, d_resctrl);
+ return container_of(r, struct rdt_hw_mon_domain, d_resctrl);
}

/**
@@ -385,7 +401,7 @@ static inline struct rdt_hw_domain *resctrl_to_arch_dom(struct rdt_domain *r)
*/
struct msr_param {
struct rdt_resource *res;
- struct rdt_domain *dom;
+ struct rdt_ctrl_domain *dom;
u32 low;
u32 high;
};
@@ -458,9 +474,9 @@ static inline struct rdt_hw_resource *resctrl_to_arch_res(struct rdt_resource *r
}

int parse_cbm(struct rdt_parse_data *data, struct resctrl_schema *s,
- struct rdt_domain *d);
+ struct rdt_ctrl_domain *d);
int parse_bw(struct rdt_parse_data *data, struct resctrl_schema *s,
- struct rdt_domain *d);
+ struct rdt_ctrl_domain *d);

extern struct mutex rdtgroup_mutex;

@@ -564,22 +580,22 @@ ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of,
char *buf, size_t nbytes, loff_t off);
int rdtgroup_schemata_show(struct kernfs_open_file *of,
struct seq_file *s, void *v);
-bool rdtgroup_cbm_overlaps(struct resctrl_schema *s, struct rdt_domain *d,
+bool rdtgroup_cbm_overlaps(struct resctrl_schema *s, struct rdt_ctrl_domain *d,
unsigned long cbm, int closid, bool exclusive);
-unsigned int rdtgroup_cbm_to_size(struct rdt_resource *r, struct rdt_domain *d,
+unsigned int rdtgroup_cbm_to_size(struct rdt_resource *r, struct rdt_ctrl_domain *d,
unsigned long cbm);
enum rdtgrp_mode rdtgroup_mode_by_closid(int closid);
int rdtgroup_tasks_assigned(struct rdtgroup *r);
int rdtgroup_locksetup_enter(struct rdtgroup *rdtgrp);
int rdtgroup_locksetup_exit(struct rdtgroup *rdtgrp);
-bool rdtgroup_cbm_overlaps_pseudo_locked(struct rdt_domain *d, unsigned long cbm);
-bool rdtgroup_pseudo_locked_in_hierarchy(struct rdt_domain *d);
+bool rdtgroup_cbm_overlaps_pseudo_locked(struct rdt_ctrl_domain *d, unsigned long cbm);
+bool rdtgroup_pseudo_locked_in_hierarchy(struct rdt_ctrl_domain *d);
int rdt_pseudo_lock_init(void);
void rdt_pseudo_lock_release(void);
int rdtgroup_pseudo_lock_create(struct rdtgroup *rdtgrp);
void rdtgroup_pseudo_lock_remove(struct rdtgroup *rdtgrp);
-struct rdt_domain *get_ctrl_domain_from_cpu(int cpu, struct rdt_resource *r);
-struct rdt_domain *get_mon_domain_from_cpu(int cpu, struct rdt_resource *r);
+struct rdt_ctrl_domain *get_ctrl_domain_from_cpu(int cpu, struct rdt_resource *r);
+struct rdt_mon_domain *get_mon_domain_from_cpu(int cpu, struct rdt_resource *r);
int closids_supported(void);
void closid_free(int closid);
int alloc_rmid(u32 closid);
@@ -590,19 +606,19 @@ bool __init rdt_cpu_has(int flag);
void mon_event_count(void *info);
int rdtgroup_mondata_show(struct seq_file *m, void *arg);
void mon_event_read(struct rmid_read *rr, struct rdt_resource *r,
- struct rdt_domain *d, struct rdtgroup *rdtgrp,
+ struct rdt_mon_domain *d, struct rdtgroup *rdtgrp,
int evtid, int first);
-void mbm_setup_overflow_handler(struct rdt_domain *dom,
+void mbm_setup_overflow_handler(struct rdt_mon_domain *dom,
unsigned long delay_ms,
int exclude_cpu);
void mbm_handle_overflow(struct work_struct *work);
void __init intel_rdt_mbm_apply_quirk(void);
bool is_mba_sc(struct rdt_resource *r);
-void cqm_setup_limbo_handler(struct rdt_domain *dom, unsigned long delay_ms,
+void cqm_setup_limbo_handler(struct rdt_mon_domain *dom, unsigned long delay_ms,
int exclude_cpu);
void cqm_handle_limbo(struct work_struct *work);
-bool has_busy_rmid(struct rdt_domain *d);
-void __check_limbo(struct rdt_domain *d, bool force_free);
+bool has_busy_rmid(struct rdt_mon_domain *d);
+void __check_limbo(struct rdt_mon_domain *d, bool force_free);
void rdt_domain_reconfigure_cdp(struct rdt_resource *r);
void __init thread_throttle_mode_init(void);
void __init mbm_config_rftype_init(const char *config);
diff --git a/arch/x86/kernel/cpu/resctrl/core.c b/arch/x86/kernel/cpu/resctrl/core.c
index 66a5a270d66f..cd58c9d4710f 100644
--- a/arch/x86/kernel/cpu/resctrl/core.c
+++ b/arch/x86/kernel/cpu/resctrl/core.c
@@ -309,8 +309,8 @@ static void rdt_get_cdp_l2_config(void)

static void mba_wrmsr_amd(struct msr_param *m)
{
+ struct rdt_hw_ctrl_domain *hw_dom = resctrl_to_arch_ctrl_dom(m->dom);
struct rdt_hw_resource *hw_res = resctrl_to_arch_res(m->res);
- struct rdt_hw_domain *hw_dom = resctrl_to_arch_dom(m->dom);
unsigned int i;

for (i = m->low; i < m->high; i++)
@@ -333,8 +333,8 @@ static u32 delay_bw_map(unsigned long bw, struct rdt_resource *r)

static void mba_wrmsr_intel(struct msr_param *m)
{
+ struct rdt_hw_ctrl_domain *hw_dom = resctrl_to_arch_ctrl_dom(m->dom);
struct rdt_hw_resource *hw_res = resctrl_to_arch_res(m->res);
- struct rdt_hw_domain *hw_dom = resctrl_to_arch_dom(m->dom);
unsigned int i;

/* Write the delay values for mba. */
@@ -344,17 +344,17 @@ static void mba_wrmsr_intel(struct msr_param *m)

static void cat_wrmsr(struct msr_param *m)
{
+ struct rdt_hw_ctrl_domain *hw_dom = resctrl_to_arch_ctrl_dom(m->dom);
struct rdt_hw_resource *hw_res = resctrl_to_arch_res(m->res);
- struct rdt_hw_domain *hw_dom = resctrl_to_arch_dom(m->dom);
unsigned int i;

for (i = m->low; i < m->high; i++)
wrmsrl(hw_res->msr_base + i, hw_dom->ctrl_val[i]);
}

-struct rdt_domain *get_ctrl_domain_from_cpu(int cpu, struct rdt_resource *r)
+struct rdt_ctrl_domain *get_ctrl_domain_from_cpu(int cpu, struct rdt_resource *r)
{
- struct rdt_domain *d;
+ struct rdt_ctrl_domain *d;

lockdep_assert_cpus_held();

@@ -367,9 +367,9 @@ struct rdt_domain *get_ctrl_domain_from_cpu(int cpu, struct rdt_resource *r)
return NULL;
}

-struct rdt_domain *get_mon_domain_from_cpu(int cpu, struct rdt_resource *r)
+struct rdt_mon_domain *get_mon_domain_from_cpu(int cpu, struct rdt_resource *r)
{
- struct rdt_domain *d;
+ struct rdt_mon_domain *d;

lockdep_assert_cpus_held();

@@ -440,18 +440,23 @@ static void setup_default_ctrlval(struct rdt_resource *r, u32 *dc)
*dc = r->default_ctrl;
}

-static void domain_free(struct rdt_hw_domain *hw_dom)
+static void ctrl_domain_free(struct rdt_hw_ctrl_domain *hw_dom)
+{
+ kfree(hw_dom->ctrl_val);
+ kfree(hw_dom);
+}
+
+static void mon_domain_free(struct rdt_hw_mon_domain *hw_dom)
{
kfree(hw_dom->arch_mbm_total);
kfree(hw_dom->arch_mbm_local);
- kfree(hw_dom->ctrl_val);
kfree(hw_dom);
}

-static int domain_setup_ctrlval(struct rdt_resource *r, struct rdt_domain *d)
+static int domain_setup_ctrlval(struct rdt_resource *r, struct rdt_ctrl_domain *d)
{
+ struct rdt_hw_ctrl_domain *hw_dom = resctrl_to_arch_ctrl_dom(d);
struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
- struct rdt_hw_domain *hw_dom = resctrl_to_arch_dom(d);
struct msr_param m;
u32 *dc;

@@ -476,7 +481,7 @@ static int domain_setup_ctrlval(struct rdt_resource *r, struct rdt_domain *d)
* @num_rmid: The size of the MBM counter array
* @hw_dom: The domain that owns the allocated arrays
*/
-static int arch_domain_mbm_alloc(u32 num_rmid, struct rdt_hw_domain *hw_dom)
+static int arch_domain_mbm_alloc(u32 num_rmid, struct rdt_hw_mon_domain *hw_dom)
{
size_t tsize;

@@ -515,10 +520,10 @@ static int get_domain_id_from_scope(int cpu, enum resctrl_scope scope)
static void domain_add_cpu_ctrl(int cpu, struct rdt_resource *r)
{
int id = get_domain_id_from_scope(cpu, r->ctrl_scope);
+ struct rdt_hw_ctrl_domain *hw_dom;
struct list_head *add_pos = NULL;
- struct rdt_hw_domain *hw_dom;
struct rdt_domain_hdr *hdr;
- struct rdt_domain *d;
+ struct rdt_ctrl_domain *d;
int err;

lockdep_assert_held(&domain_list_lock);
@@ -533,7 +538,7 @@ static void domain_add_cpu_ctrl(int cpu, struct rdt_resource *r)
if (hdr) {
if (WARN_ON_ONCE(hdr->type != RESCTRL_CTRL_DOMAIN))
return;
- d = container_of(hdr, struct rdt_domain, hdr);
+ d = container_of(hdr, struct rdt_ctrl_domain, hdr);

cpumask_set_cpu(cpu, &d->hdr.cpu_mask);
if (r->cache.arch_has_per_cpu_cfg)
@@ -553,7 +558,7 @@ static void domain_add_cpu_ctrl(int cpu, struct rdt_resource *r)
rdt_domain_reconfigure_cdp(r);

if (domain_setup_ctrlval(r, d)) {
- domain_free(hw_dom);
+ ctrl_domain_free(hw_dom);
return;
}

@@ -563,7 +568,7 @@ static void domain_add_cpu_ctrl(int cpu, struct rdt_resource *r)
if (err) {
list_del_rcu(&d->hdr.list);
synchronize_rcu();
- domain_free(hw_dom);
+ ctrl_domain_free(hw_dom);
}
}

@@ -571,9 +576,9 @@ static void domain_add_cpu_mon(int cpu, struct rdt_resource *r)
{
int id = get_domain_id_from_scope(cpu, r->mon_scope);
struct list_head *add_pos = NULL;
- struct rdt_hw_domain *hw_dom;
+ struct rdt_hw_mon_domain *hw_dom;
struct rdt_domain_hdr *hdr;
- struct rdt_domain *d;
+ struct rdt_mon_domain *d;
int err;

lockdep_assert_held(&domain_list_lock);
@@ -588,7 +593,7 @@ static void domain_add_cpu_mon(int cpu, struct rdt_resource *r)
if (hdr) {
if (WARN_ON_ONCE(hdr->type != RESCTRL_MON_DOMAIN))
return;
- d = container_of(hdr, struct rdt_domain, hdr);
+ d = container_of(hdr, struct rdt_mon_domain, hdr);

cpumask_set_cpu(cpu, &d->hdr.cpu_mask);
return;
@@ -604,7 +609,7 @@ static void domain_add_cpu_mon(int cpu, struct rdt_resource *r)
cpumask_set_cpu(cpu, &d->hdr.cpu_mask);

if (arch_domain_mbm_alloc(r->num_rmid, hw_dom)) {
- domain_free(hw_dom);
+ mon_domain_free(hw_dom);
return;
}

@@ -614,7 +619,7 @@ static void domain_add_cpu_mon(int cpu, struct rdt_resource *r)
if (err) {
list_del_rcu(&d->hdr.list);
synchronize_rcu();
- domain_free(hw_dom);
+ mon_domain_free(hw_dom);
}
}

@@ -629,9 +634,9 @@ static void domain_add_cpu(int cpu, struct rdt_resource *r)
static void domain_remove_cpu_ctrl(int cpu, struct rdt_resource *r)
{
int id = get_domain_id_from_scope(cpu, r->ctrl_scope);
- struct rdt_hw_domain *hw_dom;
+ struct rdt_hw_ctrl_domain *hw_dom;
struct rdt_domain_hdr *hdr;
- struct rdt_domain *d;
+ struct rdt_ctrl_domain *d;

lockdep_assert_held(&domain_list_lock);

@@ -651,8 +656,8 @@ static void domain_remove_cpu_ctrl(int cpu, struct rdt_resource *r)
if (WARN_ON_ONCE(hdr->type != RESCTRL_CTRL_DOMAIN))
return;

- d = container_of(hdr, struct rdt_domain, hdr);
- hw_dom = resctrl_to_arch_dom(d);
+ d = container_of(hdr, struct rdt_ctrl_domain, hdr);
+ hw_dom = resctrl_to_arch_ctrl_dom(d);

cpumask_clear_cpu(cpu, &d->hdr.cpu_mask);
if (cpumask_empty(&d->hdr.cpu_mask)) {
@@ -661,12 +666,12 @@ static void domain_remove_cpu_ctrl(int cpu, struct rdt_resource *r)
synchronize_rcu();

/*
- * rdt_domain "d" is going to be freed below, so clear
+ * rdt_ctrl_domain "d" is going to be freed below, so clear
* its pointer from pseudo_lock_region struct.
*/
if (d->plr)
d->plr->d = NULL;
- domain_free(hw_dom);
+ ctrl_domain_free(hw_dom);

return;
}
@@ -675,9 +680,9 @@ static void domain_remove_cpu_ctrl(int cpu, struct rdt_resource *r)
static void domain_remove_cpu_mon(int cpu, struct rdt_resource *r)
{
int id = get_domain_id_from_scope(cpu, r->mon_scope);
- struct rdt_hw_domain *hw_dom;
+ struct rdt_hw_mon_domain *hw_dom;
struct rdt_domain_hdr *hdr;
- struct rdt_domain *d;
+ struct rdt_mon_domain *d;

lockdep_assert_held(&domain_list_lock);

@@ -697,15 +702,15 @@ static void domain_remove_cpu_mon(int cpu, struct rdt_resource *r)
if (WARN_ON_ONCE(hdr->type != RESCTRL_MON_DOMAIN))
return;

- d = container_of(hdr, struct rdt_domain, hdr);
- hw_dom = resctrl_to_arch_dom(d);
+ d = container_of(hdr, struct rdt_mon_domain, hdr);
+ hw_dom = resctrl_to_arch_mon_dom(d);

cpumask_clear_cpu(cpu, &d->hdr.cpu_mask);
if (cpumask_empty(&d->hdr.cpu_mask)) {
resctrl_offline_mon_domain(r, d);
list_del_rcu(&d->hdr.list);
synchronize_rcu();
- domain_free(hw_dom);
+ mon_domain_free(hw_dom);

return;
}
diff --git a/arch/x86/kernel/cpu/resctrl/ctrlmondata.c b/arch/x86/kernel/cpu/resctrl/ctrlmondata.c
index 8cc36723f077..3b9383612c35 100644
--- a/arch/x86/kernel/cpu/resctrl/ctrlmondata.c
+++ b/arch/x86/kernel/cpu/resctrl/ctrlmondata.c
@@ -60,7 +60,7 @@ static bool bw_validate(char *buf, unsigned long *data, struct rdt_resource *r)
}

int parse_bw(struct rdt_parse_data *data, struct resctrl_schema *s,
- struct rdt_domain *d)
+ struct rdt_ctrl_domain *d)
{
struct resctrl_staged_config *cfg;
u32 closid = data->rdtgrp->closid;
@@ -139,7 +139,7 @@ static bool cbm_validate(char *buf, u32 *data, struct rdt_resource *r)
* resource type.
*/
int parse_cbm(struct rdt_parse_data *data, struct resctrl_schema *s,
- struct rdt_domain *d)
+ struct rdt_ctrl_domain *d)
{
struct rdtgroup *rdtgrp = data->rdtgrp;
struct resctrl_staged_config *cfg;
@@ -208,8 +208,8 @@ static int parse_line(char *line, struct resctrl_schema *s,
struct resctrl_staged_config *cfg;
struct rdt_resource *r = s->res;
struct rdt_parse_data data;
+ struct rdt_ctrl_domain *d;
char *dom = NULL, *id;
- struct rdt_domain *d;
unsigned long dom_id;

/* Walking r->domains, ensure it can't race with cpuhp */
@@ -272,11 +272,11 @@ static u32 get_config_index(u32 closid, enum resctrl_conf_type type)
}
}

-int resctrl_arch_update_one(struct rdt_resource *r, struct rdt_domain *d,
+int resctrl_arch_update_one(struct rdt_resource *r, struct rdt_ctrl_domain *d,
u32 closid, enum resctrl_conf_type t, u32 cfg_val)
{
+ struct rdt_hw_ctrl_domain *hw_dom = resctrl_to_arch_ctrl_dom(d);
struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
- struct rdt_hw_domain *hw_dom = resctrl_to_arch_dom(d);
u32 idx = get_config_index(closid, t);
struct msr_param msr_param;

@@ -297,17 +297,17 @@ int resctrl_arch_update_one(struct rdt_resource *r, struct rdt_domain *d,
int resctrl_arch_update_domains(struct rdt_resource *r, u32 closid)
{
struct resctrl_staged_config *cfg;
- struct rdt_hw_domain *hw_dom;
+ struct rdt_hw_ctrl_domain *hw_dom;
struct msr_param msr_param;
+ struct rdt_ctrl_domain *d;
enum resctrl_conf_type t;
- struct rdt_domain *d;
u32 idx;

/* Walking r->domains, ensure it can't race with cpuhp */
lockdep_assert_cpus_held();

list_for_each_entry(d, &r->ctrl_domains, hdr.list) {
- hw_dom = resctrl_to_arch_dom(d);
+ hw_dom = resctrl_to_arch_ctrl_dom(d);
msr_param.res = NULL;
for (t = 0; t < CDP_NUM_TYPES; t++) {
cfg = &hw_dom->d_resctrl.staged_config[t];
@@ -430,10 +430,10 @@ ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of,
return ret ?: nbytes;
}

-u32 resctrl_arch_get_config(struct rdt_resource *r, struct rdt_domain *d,
+u32 resctrl_arch_get_config(struct rdt_resource *r, struct rdt_ctrl_domain *d,
u32 closid, enum resctrl_conf_type type)
{
- struct rdt_hw_domain *hw_dom = resctrl_to_arch_dom(d);
+ struct rdt_hw_ctrl_domain *hw_dom = resctrl_to_arch_ctrl_dom(d);
u32 idx = get_config_index(closid, type);

return hw_dom->ctrl_val[idx];
@@ -442,7 +442,7 @@ u32 resctrl_arch_get_config(struct rdt_resource *r, struct rdt_domain *d,
static void show_doms(struct seq_file *s, struct resctrl_schema *schema, int closid)
{
struct rdt_resource *r = schema->res;
- struct rdt_domain *dom;
+ struct rdt_ctrl_domain *dom;
bool sep = false;
u32 ctrl_val;

@@ -514,7 +514,7 @@ static int smp_mon_event_count(void *arg)
}

void mon_event_read(struct rmid_read *rr, struct rdt_resource *r,
- struct rdt_domain *d, struct rdtgroup *rdtgrp,
+ struct rdt_mon_domain *d, struct rdtgroup *rdtgrp,
int evtid, int first)
{
int cpu;
@@ -557,11 +557,11 @@ int rdtgroup_mondata_show(struct seq_file *m, void *arg)
{
struct kernfs_open_file *of = m->private;
struct rdt_domain_hdr *hdr;
+ struct rdt_mon_domain *d;
u32 resid, evtid, domid;
struct rdtgroup *rdtgrp;
struct rdt_resource *r;
union mon_data_bits md;
- struct rdt_domain *d;
struct rmid_read rr;
int ret = 0;

@@ -582,7 +582,7 @@ int rdtgroup_mondata_show(struct seq_file *m, void *arg)
ret = -ENOENT;
goto out;
}
- d = container_of(hdr, struct rdt_domain, hdr);
+ d = container_of(hdr, struct rdt_mon_domain, hdr);

mon_event_read(&rr, r, d, rdtgrp, evtid, false);

diff --git a/arch/x86/kernel/cpu/resctrl/monitor.c b/arch/x86/kernel/cpu/resctrl/monitor.c
index 82a44de8136f..89d7e6fcbaa1 100644
--- a/arch/x86/kernel/cpu/resctrl/monitor.c
+++ b/arch/x86/kernel/cpu/resctrl/monitor.c
@@ -209,7 +209,7 @@ static int __rmid_read(u32 rmid, enum resctrl_event_id eventid, u64 *val)
return 0;
}

-static struct arch_mbm_state *get_arch_mbm_state(struct rdt_hw_domain *hw_dom,
+static struct arch_mbm_state *get_arch_mbm_state(struct rdt_hw_mon_domain *hw_dom,
u32 rmid,
enum resctrl_event_id eventid)
{
@@ -228,11 +228,11 @@ static struct arch_mbm_state *get_arch_mbm_state(struct rdt_hw_domain *hw_dom,
return NULL;
}

-void resctrl_arch_reset_rmid(struct rdt_resource *r, struct rdt_domain *d,
+void resctrl_arch_reset_rmid(struct rdt_resource *r, struct rdt_mon_domain *d,
u32 unused, u32 rmid,
enum resctrl_event_id eventid)
{
- struct rdt_hw_domain *hw_dom = resctrl_to_arch_dom(d);
+ struct rdt_hw_mon_domain *hw_dom = resctrl_to_arch_mon_dom(d);
struct arch_mbm_state *am;

am = get_arch_mbm_state(hw_dom, rmid, eventid);
@@ -248,9 +248,9 @@ void resctrl_arch_reset_rmid(struct rdt_resource *r, struct rdt_domain *d,
* Assumes that hardware counters are also reset and thus that there is
* no need to record initial non-zero counts.
*/
-void resctrl_arch_reset_rmid_all(struct rdt_resource *r, struct rdt_domain *d)
+void resctrl_arch_reset_rmid_all(struct rdt_resource *r, struct rdt_mon_domain *d)
{
- struct rdt_hw_domain *hw_dom = resctrl_to_arch_dom(d);
+ struct rdt_hw_mon_domain *hw_dom = resctrl_to_arch_mon_dom(d);

if (is_mbm_total_enabled())
memset(hw_dom->arch_mbm_total, 0,
@@ -269,12 +269,12 @@ static u64 mbm_overflow_count(u64 prev_msr, u64 cur_msr, unsigned int width)
return chunks >> shift;
}

-int resctrl_arch_rmid_read(struct rdt_resource *r, struct rdt_domain *d,
+int resctrl_arch_rmid_read(struct rdt_resource *r, struct rdt_mon_domain *d,
u32 unused, u32 rmid, enum resctrl_event_id eventid,
u64 *val, void *ignored)
{
+ struct rdt_hw_mon_domain *hw_dom = resctrl_to_arch_mon_dom(d);
struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
- struct rdt_hw_domain *hw_dom = resctrl_to_arch_dom(d);
struct arch_mbm_state *am;
u64 msr_val, chunks;
int ret;
@@ -320,7 +320,7 @@ static void limbo_release_entry(struct rmid_entry *entry)
* decrement the count. If the busy count gets to zero on an RMID, we
* free the RMID
*/
-void __check_limbo(struct rdt_domain *d, bool force_free)
+void __check_limbo(struct rdt_mon_domain *d, bool force_free)
{
struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
u32 idx_limit = resctrl_arch_system_num_rmid_idx();
@@ -378,7 +378,7 @@ void __check_limbo(struct rdt_domain *d, bool force_free)
resctrl_arch_mon_ctx_free(r, QOS_L3_OCCUP_EVENT_ID, arch_mon_ctx);
}

-bool has_busy_rmid(struct rdt_domain *d)
+bool has_busy_rmid(struct rdt_mon_domain *d)
{
u32 idx_limit = resctrl_arch_system_num_rmid_idx();

@@ -479,7 +479,7 @@ int alloc_rmid(u32 closid)
static void add_rmid_to_limbo(struct rmid_entry *entry)
{
struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
- struct rdt_domain *d;
+ struct rdt_mon_domain *d;
u32 idx;

lockdep_assert_held(&rdtgroup_mutex);
@@ -531,7 +531,7 @@ void free_rmid(u32 closid, u32 rmid)
list_add_tail(&entry->list, &rmid_free_lru);
}

-static struct mbm_state *get_mbm_state(struct rdt_domain *d, u32 closid,
+static struct mbm_state *get_mbm_state(struct rdt_mon_domain *d, u32 closid,
u32 rmid, enum resctrl_event_id evtid)
{
u32 idx = resctrl_arch_rmid_idx_encode(closid, rmid);
@@ -667,12 +667,12 @@ void mon_event_count(void *info)
* throttle MSRs already have low percentage values. To avoid
* unnecessarily restricting such rdtgroups, we also increase the bandwidth.
*/
-static void update_mba_bw(struct rdtgroup *rgrp, struct rdt_domain *dom_mbm)
+static void update_mba_bw(struct rdtgroup *rgrp, struct rdt_mon_domain *dom_mbm)
{
u32 closid, rmid, cur_msr_val, new_msr_val;
struct mbm_state *pmbm_data, *cmbm_data;
+ struct rdt_ctrl_domain *dom_mba;
struct rdt_resource *r_mba;
- struct rdt_domain *dom_mba;
u32 cur_bw, user_bw, idx;
struct list_head *head;
struct rdtgroup *entry;
@@ -733,7 +733,7 @@ static void update_mba_bw(struct rdtgroup *rgrp, struct rdt_domain *dom_mbm)
resctrl_arch_update_one(r_mba, dom_mba, closid, CDP_NONE, new_msr_val);
}

-static void mbm_update(struct rdt_resource *r, struct rdt_domain *d,
+static void mbm_update(struct rdt_resource *r, struct rdt_mon_domain *d,
u32 closid, u32 rmid)
{
struct rmid_read rr;
@@ -791,12 +791,12 @@ static void mbm_update(struct rdt_resource *r, struct rdt_domain *d,
void cqm_handle_limbo(struct work_struct *work)
{
unsigned long delay = msecs_to_jiffies(CQM_LIMBOCHECK_INTERVAL);
- struct rdt_domain *d;
+ struct rdt_mon_domain *d;

cpus_read_lock();
mutex_lock(&rdtgroup_mutex);

- d = container_of(work, struct rdt_domain, cqm_limbo.work);
+ d = container_of(work, struct rdt_mon_domain, cqm_limbo.work);

__check_limbo(d, false);

@@ -819,7 +819,7 @@ void cqm_handle_limbo(struct work_struct *work)
* @exclude_cpu: Which CPU the handler should not run on,
* RESCTRL_PICK_ANY_CPU to pick any CPU.
*/
-void cqm_setup_limbo_handler(struct rdt_domain *dom, unsigned long delay_ms,
+void cqm_setup_limbo_handler(struct rdt_mon_domain *dom, unsigned long delay_ms,
int exclude_cpu)
{
unsigned long delay = msecs_to_jiffies(delay_ms);
@@ -836,9 +836,9 @@ void mbm_handle_overflow(struct work_struct *work)
{
unsigned long delay = msecs_to_jiffies(MBM_OVERFLOW_INTERVAL);
struct rdtgroup *prgrp, *crgrp;
+ struct rdt_mon_domain *d;
struct list_head *head;
struct rdt_resource *r;
- struct rdt_domain *d;

cpus_read_lock();
mutex_lock(&rdtgroup_mutex);
@@ -851,7 +851,7 @@ void mbm_handle_overflow(struct work_struct *work)
goto out_unlock;

r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
- d = container_of(work, struct rdt_domain, mbm_over.work);
+ d = container_of(work, struct rdt_mon_domain, mbm_over.work);

list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
mbm_update(r, d, prgrp->closid, prgrp->mon.rmid);
@@ -885,7 +885,7 @@ void mbm_handle_overflow(struct work_struct *work)
* @exclude_cpu: Which CPU the handler should not run on,
* RESCTRL_PICK_ANY_CPU to pick any CPU.
*/
-void mbm_setup_overflow_handler(struct rdt_domain *dom, unsigned long delay_ms,
+void mbm_setup_overflow_handler(struct rdt_mon_domain *dom, unsigned long delay_ms,
int exclude_cpu)
{
unsigned long delay = msecs_to_jiffies(delay_ms);
diff --git a/arch/x86/kernel/cpu/resctrl/pseudo_lock.c b/arch/x86/kernel/cpu/resctrl/pseudo_lock.c
index 20617a1d8261..a4513d1d9f55 100644
--- a/arch/x86/kernel/cpu/resctrl/pseudo_lock.c
+++ b/arch/x86/kernel/cpu/resctrl/pseudo_lock.c
@@ -814,7 +814,7 @@ int rdtgroup_locksetup_exit(struct rdtgroup *rdtgrp)
* Return: true if @cbm overlaps with pseudo-locked region on @d, false
* otherwise.
*/
-bool rdtgroup_cbm_overlaps_pseudo_locked(struct rdt_domain *d, unsigned long cbm)
+bool rdtgroup_cbm_overlaps_pseudo_locked(struct rdt_ctrl_domain *d, unsigned long cbm)
{
unsigned int cbm_len;
unsigned long cbm_b;
@@ -841,11 +841,11 @@ bool rdtgroup_cbm_overlaps_pseudo_locked(struct rdt_domain *d, unsigned long cbm
* if it is not possible to test due to memory allocation issue,
* false otherwise.
*/
-bool rdtgroup_pseudo_locked_in_hierarchy(struct rdt_domain *d)
+bool rdtgroup_pseudo_locked_in_hierarchy(struct rdt_ctrl_domain *d)
{
+ struct rdt_ctrl_domain *d_i;
cpumask_var_t cpu_with_psl;
struct rdt_resource *r;
- struct rdt_domain *d_i;
bool ret = false;

/* Walking r->domains, ensure it can't race with cpuhp */
diff --git a/arch/x86/kernel/cpu/resctrl/rdtgroup.c b/arch/x86/kernel/cpu/resctrl/rdtgroup.c
index 7c1475f393ff..cc31ede1a1e7 100644
--- a/arch/x86/kernel/cpu/resctrl/rdtgroup.c
+++ b/arch/x86/kernel/cpu/resctrl/rdtgroup.c
@@ -92,8 +92,8 @@ void rdt_last_cmd_printf(const char *fmt, ...)

void rdt_staged_configs_clear(void)
{
+ struct rdt_ctrl_domain *dom;
struct rdt_resource *r;
- struct rdt_domain *dom;

lockdep_assert_held(&rdtgroup_mutex);

@@ -1012,7 +1012,7 @@ static int rdt_bit_usage_show(struct kernfs_open_file *of,
unsigned long sw_shareable = 0, hw_shareable = 0;
unsigned long exclusive = 0, pseudo_locked = 0;
struct rdt_resource *r = s->res;
- struct rdt_domain *dom;
+ struct rdt_ctrl_domain *dom;
int i, hwb, swb, excl, psl;
enum rdtgrp_mode mode;
bool sep = false;
@@ -1243,7 +1243,7 @@ static int rdt_has_sparse_bitmasks_show(struct kernfs_open_file *of,
*
* Return: false if CBM does not overlap, true if it does.
*/
-static bool __rdtgroup_cbm_overlaps(struct rdt_resource *r, struct rdt_domain *d,
+static bool __rdtgroup_cbm_overlaps(struct rdt_resource *r, struct rdt_ctrl_domain *d,
unsigned long cbm, int closid,
enum resctrl_conf_type type, bool exclusive)
{
@@ -1298,7 +1298,7 @@ static bool __rdtgroup_cbm_overlaps(struct rdt_resource *r, struct rdt_domain *d
*
* Return: true if CBM overlap detected, false if there is no overlap
*/
-bool rdtgroup_cbm_overlaps(struct resctrl_schema *s, struct rdt_domain *d,
+bool rdtgroup_cbm_overlaps(struct resctrl_schema *s, struct rdt_ctrl_domain *d,
unsigned long cbm, int closid, bool exclusive)
{
enum resctrl_conf_type peer_type = resctrl_peer_type(s->conf_type);
@@ -1329,10 +1329,10 @@ bool rdtgroup_cbm_overlaps(struct resctrl_schema *s, struct rdt_domain *d,
static bool rdtgroup_mode_test_exclusive(struct rdtgroup *rdtgrp)
{
int closid = rdtgrp->closid;
+ struct rdt_ctrl_domain *d;
struct resctrl_schema *s;
struct rdt_resource *r;
bool has_cache = false;
- struct rdt_domain *d;
u32 ctrl;

/* Walking r->domains, ensure it can't race with cpuhp */
@@ -1448,7 +1448,7 @@ static ssize_t rdtgroup_mode_write(struct kernfs_open_file *of,
* bitmap functions work correctly.
*/
unsigned int rdtgroup_cbm_to_size(struct rdt_resource *r,
- struct rdt_domain *d, unsigned long cbm)
+ struct rdt_ctrl_domain *d, unsigned long cbm)
{
struct cpu_cacheinfo *ci;
unsigned int size = 0;
@@ -1480,9 +1480,9 @@ static int rdtgroup_size_show(struct kernfs_open_file *of,
{
struct resctrl_schema *schema;
enum resctrl_conf_type type;
+ struct rdt_ctrl_domain *d;
struct rdtgroup *rdtgrp;
struct rdt_resource *r;
- struct rdt_domain *d;
unsigned int size;
int ret = 0;
u32 closid;
@@ -1594,7 +1594,7 @@ static void mon_event_config_read(void *info)
mon_info->mon_config = msrval & MAX_EVT_CONFIG_BITS;
}

-static void mondata_config_read(struct rdt_domain *d, struct mon_config_info *mon_info)
+static void mondata_config_read(struct rdt_mon_domain *d, struct mon_config_info *mon_info)
{
smp_call_function_any(&d->hdr.cpu_mask, mon_event_config_read, mon_info, 1);
}
@@ -1602,7 +1602,7 @@ static void mondata_config_read(struct rdt_domain *d, struct mon_config_info *mo
static int mbm_config_show(struct seq_file *s, struct rdt_resource *r, u32 evtid)
{
struct mon_config_info mon_info = {0};
- struct rdt_domain *dom;
+ struct rdt_mon_domain *dom;
bool sep = false;

cpus_read_lock();
@@ -1661,7 +1661,7 @@ static void mon_event_config_write(void *info)
}

static void mbm_config_write_domain(struct rdt_resource *r,
- struct rdt_domain *d, u32 evtid, u32 val)
+ struct rdt_mon_domain *d, u32 evtid, u32 val)
{
struct mon_config_info mon_info = {0};

@@ -1702,7 +1702,7 @@ static int mon_config_write(struct rdt_resource *r, char *tok, u32 evtid)
struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
char *dom_str = NULL, *id_str;
unsigned long dom_id, val;
- struct rdt_domain *d;
+ struct rdt_mon_domain *d;

/* Walking r->domains, ensure it can't race with cpuhp */
lockdep_assert_cpus_held();
@@ -2261,9 +2261,9 @@ static inline bool is_mba_linear(void)
static int set_cache_qos_cfg(int level, bool enable)
{
void (*update)(void *arg);
+ struct rdt_ctrl_domain *d;
struct rdt_resource *r_l;
cpumask_var_t cpu_mask;
- struct rdt_domain *d;
int cpu;

/* Walking r->domains, ensure it can't race with cpuhp */
@@ -2313,7 +2313,7 @@ void rdt_domain_reconfigure_cdp(struct rdt_resource *r)
l3_qos_cfg_update(&hw_res->cdp_enabled);
}

-static int mba_sc_domain_allocate(struct rdt_resource *r, struct rdt_domain *d)
+static int mba_sc_domain_allocate(struct rdt_resource *r, struct rdt_ctrl_domain *d)
{
u32 num_closid = resctrl_arch_get_num_closid(r);
int cpu = cpumask_any(&d->hdr.cpu_mask);
@@ -2331,7 +2331,7 @@ static int mba_sc_domain_allocate(struct rdt_resource *r, struct rdt_domain *d)
}

static void mba_sc_domain_destroy(struct rdt_resource *r,
- struct rdt_domain *d)
+ struct rdt_ctrl_domain *d)
{
kfree(d->mbps_val);
d->mbps_val = NULL;
@@ -2357,7 +2357,7 @@ static int set_mba_sc(bool mba_sc)
{
struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl;
u32 num_closid = resctrl_arch_get_num_closid(r);
- struct rdt_domain *d;
+ struct rdt_ctrl_domain *d;
int i;

if (!supports_mba_mbps() || mba_sc == is_mba_sc(r))
@@ -2629,7 +2629,7 @@ static int rdt_get_tree(struct fs_context *fc)
{
struct rdt_fs_context *ctx = rdt_fc2context(fc);
unsigned long flags = RFTYPE_CTRL_BASE;
- struct rdt_domain *dom;
+ struct rdt_mon_domain *dom;
struct rdt_resource *r;
int ret;

@@ -2814,9 +2814,9 @@ static int rdt_init_fs_context(struct fs_context *fc)
static int reset_all_ctrls(struct rdt_resource *r)
{
struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
- struct rdt_hw_domain *hw_dom;
+ struct rdt_hw_ctrl_domain *hw_dom;
struct msr_param msr_param;
- struct rdt_domain *d;
+ struct rdt_ctrl_domain *d;
int i;

/* Walking r->domains, ensure it can't race with cpuhp */
@@ -2832,7 +2832,7 @@ static int reset_all_ctrls(struct rdt_resource *r)
* from each domain to update the MSRs below.
*/
list_for_each_entry(d, &r->ctrl_domains, hdr.list) {
- hw_dom = resctrl_to_arch_dom(d);
+ hw_dom = resctrl_to_arch_ctrl_dom(d);

for (i = 0; i < hw_res->num_closid; i++)
hw_dom->ctrl_val[i] = r->default_ctrl;
@@ -3025,7 +3025,7 @@ static void rmdir_mondata_subdir_allrdtgrp(struct rdt_resource *r,
}

static int mkdir_mondata_subdir(struct kernfs_node *parent_kn,
- struct rdt_domain *d,
+ struct rdt_mon_domain *d,
struct rdt_resource *r, struct rdtgroup *prgrp)
{
union mon_data_bits priv;
@@ -3074,7 +3074,7 @@ static int mkdir_mondata_subdir(struct kernfs_node *parent_kn,
* and "monitor" groups with given domain id.
*/
static void mkdir_mondata_subdir_allrdtgrp(struct rdt_resource *r,
- struct rdt_domain *d)
+ struct rdt_mon_domain *d)
{
struct kernfs_node *parent_kn;
struct rdtgroup *prgrp, *crgrp;
@@ -3096,7 +3096,7 @@ static int mkdir_mondata_subdir_alldom(struct kernfs_node *parent_kn,
struct rdt_resource *r,
struct rdtgroup *prgrp)
{
- struct rdt_domain *dom;
+ struct rdt_mon_domain *dom;
int ret;

/* Walking r->domains, ensure it can't race with cpuhp */
@@ -3201,7 +3201,7 @@ static u32 cbm_ensure_valid(u32 _val, struct rdt_resource *r)
* Set the RDT domain up to start off with all usable allocations. That is,
* all shareable and unused bits. All-zero CBM is invalid.
*/
-static int __init_one_rdt_domain(struct rdt_domain *d, struct resctrl_schema *s,
+static int __init_one_rdt_domain(struct rdt_ctrl_domain *d, struct resctrl_schema *s,
u32 closid)
{
enum resctrl_conf_type peer_type = resctrl_peer_type(s->conf_type);
@@ -3281,7 +3281,7 @@ static int __init_one_rdt_domain(struct rdt_domain *d, struct resctrl_schema *s,
*/
static int rdtgroup_init_cat(struct resctrl_schema *s, u32 closid)
{
- struct rdt_domain *d;
+ struct rdt_ctrl_domain *d;
int ret;

list_for_each_entry(d, &s->res->ctrl_domains, hdr.list) {
@@ -3297,7 +3297,7 @@ static int rdtgroup_init_cat(struct resctrl_schema *s, u32 closid)
static void rdtgroup_init_mba(struct rdt_resource *r, u32 closid)
{
struct resctrl_staged_config *cfg;
- struct rdt_domain *d;
+ struct rdt_ctrl_domain *d;

list_for_each_entry(d, &r->ctrl_domains, hdr.list) {
if (is_mba_sc(r)) {
@@ -3923,14 +3923,14 @@ static void __init rdtgroup_setup_default(void)
mutex_unlock(&rdtgroup_mutex);
}

-static void domain_destroy_mon_state(struct rdt_domain *d)
+static void domain_destroy_mon_state(struct rdt_mon_domain *d)
{
bitmap_free(d->rmid_busy_llc);
kfree(d->mbm_total);
kfree(d->mbm_local);
}

-void resctrl_offline_ctrl_domain(struct rdt_resource *r, struct rdt_domain *d)
+void resctrl_offline_ctrl_domain(struct rdt_resource *r, struct rdt_ctrl_domain *d)
{
mutex_lock(&rdtgroup_mutex);

@@ -3940,7 +3940,7 @@ void resctrl_offline_ctrl_domain(struct rdt_resource *r, struct rdt_domain *d)
mutex_unlock(&rdtgroup_mutex);
}

-void resctrl_offline_mon_domain(struct rdt_resource *r, struct rdt_domain *d)
+void resctrl_offline_mon_domain(struct rdt_resource *r, struct rdt_mon_domain *d)
{
mutex_lock(&rdtgroup_mutex);

@@ -3971,7 +3971,7 @@ void resctrl_offline_mon_domain(struct rdt_resource *r, struct rdt_domain *d)
mutex_unlock(&rdtgroup_mutex);
}

-static int domain_setup_mon_state(struct rdt_resource *r, struct rdt_domain *d)
+static int domain_setup_mon_state(struct rdt_resource *r, struct rdt_mon_domain *d)
{
u32 idx_limit = resctrl_arch_system_num_rmid_idx();
size_t tsize;
@@ -4002,7 +4002,7 @@ static int domain_setup_mon_state(struct rdt_resource *r, struct rdt_domain *d)
return 0;
}

-int resctrl_online_ctrl_domain(struct rdt_resource *r, struct rdt_domain *d)
+int resctrl_online_ctrl_domain(struct rdt_resource *r, struct rdt_ctrl_domain *d)
{
int err = 0;

@@ -4018,7 +4018,7 @@ int resctrl_online_ctrl_domain(struct rdt_resource *r, struct rdt_domain *d)
return err;
}

-int resctrl_online_mon_domain(struct rdt_resource *r, struct rdt_domain *d)
+int resctrl_online_mon_domain(struct rdt_resource *r, struct rdt_mon_domain *d)
{
int err;

@@ -4073,8 +4073,8 @@ static void clear_childcpus(struct rdtgroup *r, unsigned int cpu)
void resctrl_offline_cpu(unsigned int cpu)
{
struct rdt_resource *l3 = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
+ struct rdt_mon_domain *d;
struct rdtgroup *rdtgrp;
- struct rdt_domain *d;

mutex_lock(&rdtgroup_mutex);
list_for_each_entry(rdtgrp, &rdt_all_groups, rdtgroup_list) {
--
2.44.0


2024-05-03 20:37:37

by Luck, Tony

[permalink] [raw]
Subject: [PATCH v17 5/9] x86/resctrl: Add node-scope to the options for feature scope

Currently supported resctrl features are all domain scoped the same as the
scope of the L2 or L3 caches.

Add RESCTRL_NODE as a new option for features that are scoped at the
same granularity as NUMA nodes. This is needed for Intel's Sub-NUMA
Cluster (SNC) feature where monitoring features are node scoped.

Signed-off-by: Tony Luck <[email protected]>
---
include/linux/resctrl.h | 1 +
arch/x86/kernel/cpu/resctrl/core.c | 2 ++
2 files changed, 3 insertions(+)

diff --git a/include/linux/resctrl.h b/include/linux/resctrl.h
index aa2c22a8e37b..5c7775343c3e 100644
--- a/include/linux/resctrl.h
+++ b/include/linux/resctrl.h
@@ -176,6 +176,7 @@ struct resctrl_schema;
enum resctrl_scope {
RESCTRL_L2_CACHE = 2,
RESCTRL_L3_CACHE = 3,
+ RESCTRL_NODE,
};

/**
diff --git a/arch/x86/kernel/cpu/resctrl/core.c b/arch/x86/kernel/cpu/resctrl/core.c
index cd58c9d4710f..c34ce367c456 100644
--- a/arch/x86/kernel/cpu/resctrl/core.c
+++ b/arch/x86/kernel/cpu/resctrl/core.c
@@ -510,6 +510,8 @@ static int get_domain_id_from_scope(int cpu, enum resctrl_scope scope)
case RESCTRL_L2_CACHE:
case RESCTRL_L3_CACHE:
return get_cpu_cacheinfo_id(cpu, scope);
+ case RESCTRL_NODE:
+ return cpu_to_node(cpu);
default:
break;
}
--
2.44.0


2024-05-03 20:39:22

by Luck, Tony

[permalink] [raw]
Subject: [PATCH v17 9/9] x86/resctrl: Update documentation with Sub-NUMA cluster changes

*** This patch needs updating for new files for monitoring ***

With Sub-NUMA Cluster mode enabled the scope of monitoring resources is
per-NODE instead of per-L3 cache. Suffixes of directories with "L3" in
their name refer to Sub-NUMA nodes instead of L3 cache ids.

Users should be aware that SNC mode also affects the amount of L3 cache
available for allocation within each SNC node.

Signed-off-by: Tony Luck <[email protected]>
---
Documentation/arch/x86/resctrl.rst | 17 +++++++++++++++++
1 file changed, 17 insertions(+)

diff --git a/Documentation/arch/x86/resctrl.rst b/Documentation/arch/x86/resctrl.rst
index 627e23869bca..401f6bfb4a3c 100644
--- a/Documentation/arch/x86/resctrl.rst
+++ b/Documentation/arch/x86/resctrl.rst
@@ -375,6 +375,10 @@ When monitoring is enabled all MON groups will also contain:
all tasks in the group. In CTRL_MON groups these files provide
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.
+ On systems with Sub-NUMA (SNC) cluster enabled there are extra
+ directories for each node (located within the "mon_L3_XX" directory
+ for the L3 cache they occupy). These are named "mon_sub_L3_YY"
+ where "YY" is the node number.

"mon_hw_id":
Available only with debug option. The identifier used by hardware
@@ -484,6 +488,19 @@ if non-contiguous 1s value is supported. On a system with a 20-bit mask
each bit represents 5% of the capacity of the cache. You could partition
the cache into four equal parts with masks: 0x1f, 0x3e0, 0x7c00, 0xf8000.

+Notes on Sub-NUMA Cluster mode
+==============================
+When SNC mode is enabled, Linux may load balance tasks between Sub-NUMA
+nodes much more readily than between regular NUMA nodes since the CPUs
+on Sub-NUMA nodes share the same L3 cache and the system may report
+the NUMA distance between Sub-NUMA nodes with a lower value than used
+for regular NUMA nodes.
+The top-level monitoring files in each "mon_L3_XX" directory provide
+the sum of data across all SNC nodes sharing an L3 cache instance.
+Users who bind tasks to the CPUs of a specific Sub-NUMA node can read
+the "llc_occupancy", "mbm_total_bytes", and "mbm_local_bytes" in the
+"mon_sub_L3_YY" directories to get node local data.
+
Memory bandwidth Allocation and monitoring
==========================================

--
2.44.0


2024-05-10 21:25:07

by Reinette Chatre

[permalink] [raw]
Subject: Re: [PATCH v17 7/9] x86/resctrl: Add new monitor files for Sub-NUMA cluster (SNC) monitoring

Hi Tony,

On 5/3/2024 1:33 PM, Tony Luck wrote:

(Could you please start the changelog with some context?)

> Add a field to the rdt_resource structure to track whether monitoring
> resources are tracked by hardware at a different scope (NODE) from
> the legacy L3 scope.

This seems to describe @mon_scope that was introduced in patch #3?

>
> Add a field to the rdt_mon_domain structure to track the L3 cache id
> which can be used to find all the domains that need resource counts
> summed to provide accurate values in the legacy monitoring files.

Why is this field necessary? Can this not be obtained dynamically?


>
> When SNC is enabled create extra directories and files in each mon_data
> directory to report per-SNC node counts.

The above cryptic sentence is the closest the changelog gets to explaining
what this patch aims to do. Could you please enhance the changelog to
describe what this patch aims to do and more importantly how it goes about
doing so? This patch contains a significant number of undocumented quirks
and between the cryptic changelog and undocumented quirks in the patch I find
it very hard to understand what it is trying to do and why.

>
> Signed-off-by: Tony Luck <[email protected]>
> ---
> include/linux/resctrl.h | 4 +
> arch/x86/kernel/cpu/resctrl/internal.h | 5 +-
> arch/x86/kernel/cpu/resctrl/core.c | 2 +
> arch/x86/kernel/cpu/resctrl/ctrlmondata.c | 1 +
> arch/x86/kernel/cpu/resctrl/monitor.c | 52 +++++++---
> arch/x86/kernel/cpu/resctrl/rdtgroup.c | 115 +++++++++++++++++-----
> 6 files changed, 137 insertions(+), 42 deletions(-)
>
> diff --git a/include/linux/resctrl.h b/include/linux/resctrl.h
> index 5c7775343c3e..2f8ac925bc18 100644
> --- a/include/linux/resctrl.h
> +++ b/include/linux/resctrl.h
> @@ -96,6 +96,7 @@ struct rdt_ctrl_domain {
> /**
> * struct rdt_mon_domain - group of CPUs sharing a resctrl monitor resource
> * @hdr: common header for different domain types
> + * @display_id: shared id used to identify domains to be summed for display
> * @rmid_busy_llc: bitmap of which limbo RMIDs are above threshold
> * @mbm_total: saved state for MBM total bandwidth
> * @mbm_local: saved state for MBM local bandwidth
> @@ -106,6 +107,7 @@ struct rdt_ctrl_domain {
> */
> struct rdt_mon_domain {
> struct rdt_domain_hdr hdr;
> + int display_id;

(it is not clear to me why this is needed)

> unsigned long *rmid_busy_llc;
> struct mbm_state *mbm_total;
> struct mbm_state *mbm_local;
> @@ -187,6 +189,7 @@ enum resctrl_scope {
> * @num_rmid: Number of RMIDs available
> * @ctrl_scope: Scope of this resource for control functions
> * @mon_scope: Scope of this resource for monitor functions
> + * @mon_display_scope: Scope for user reporting monitor functions
> * @cache: Cache allocation related data
> * @membw: If the component has bandwidth controls, their properties.
> * @ctrl_domains: RCU list of all control domains for this resource
> @@ -207,6 +210,7 @@ struct rdt_resource {
> int num_rmid;
> enum resctrl_scope ctrl_scope;
> enum resctrl_scope mon_scope;
> + enum resctrl_scope mon_display_scope;
> struct resctrl_cache cache;
> struct resctrl_membw membw;
> struct list_head ctrl_domains;
> diff --git a/arch/x86/kernel/cpu/resctrl/internal.h b/arch/x86/kernel/cpu/resctrl/internal.h
> index 49440f194253..d41b388bb499 100644
> --- a/arch/x86/kernel/cpu/resctrl/internal.h
> +++ b/arch/x86/kernel/cpu/resctrl/internal.h
> @@ -132,6 +132,7 @@ struct mon_evt {
> * as kernfs private data
> * @rid: Resource id associated with the event file
> * @evtid: Event id associated with the event file
> + * @sum: Sum across domains with same display_id
> * @domid: The domain to which the event file belongs
> * @u: Name of the bit fields struct
> */
> @@ -139,7 +140,8 @@ union mon_data_bits {
> void *priv;
> struct {
> unsigned int rid : 10;
> - enum resctrl_event_id evtid : 8;
> + enum resctrl_event_id evtid : 7;
> + unsigned int sum : 1;
> unsigned int domid : 14;
> } u;

(No explanation about why evtid had to shrink and why it is ok
to do so.)

> };
> @@ -150,6 +152,7 @@ struct rmid_read {
> struct rdt_mon_domain *d;
> enum resctrl_event_id evtid;
> bool first;
> + bool sumdomains;
> int err;
> u64 val;
> void *arch_mon_ctx;
> diff --git a/arch/x86/kernel/cpu/resctrl/core.c b/arch/x86/kernel/cpu/resctrl/core.c
> index cb181796f73b..a949e69308cd 100644
> --- a/arch/x86/kernel/cpu/resctrl/core.c
> +++ b/arch/x86/kernel/cpu/resctrl/core.c
> @@ -71,6 +71,7 @@ struct rdt_hw_resource rdt_resources_all[] = {
> .name = "L3",
> .ctrl_scope = RESCTRL_L3_CACHE,
> .mon_scope = RESCTRL_L3_CACHE,
> + .mon_display_scope = RESCTRL_L3_CACHE,
> .ctrl_domains = ctrl_domain_init(RDT_RESOURCE_L3),
> .mon_domains = mon_domain_init(RDT_RESOURCE_L3),
> .parse_ctrlval = parse_cbm,
> @@ -613,6 +614,7 @@ static void domain_add_cpu_mon(int cpu, struct rdt_resource *r)
>
> d = &hw_dom->d_resctrl;
> d->hdr.id = id;
> + d->display_id = get_domain_id_from_scope(cpu, r->mon_display_scope);
> d->hdr.type = RESCTRL_MON_DOMAIN;
> cpumask_set_cpu(cpu, &d->hdr.cpu_mask);
>
> diff --git a/arch/x86/kernel/cpu/resctrl/ctrlmondata.c b/arch/x86/kernel/cpu/resctrl/ctrlmondata.c
> index 3b9383612c35..a4ead8ffbaf3 100644
> --- a/arch/x86/kernel/cpu/resctrl/ctrlmondata.c
> +++ b/arch/x86/kernel/cpu/resctrl/ctrlmondata.c
> @@ -575,6 +575,7 @@ int rdtgroup_mondata_show(struct seq_file *m, void *arg)
> resid = md.u.rid;
> domid = md.u.domid;
> evtid = md.u.evtid;
> + rr.sumdomains = md.u.sum;
>
> r = &rdt_resources_all[resid].r_resctrl;
> hdr = rdt_find_domain(&r->mon_domains, domid, NULL);
> diff --git a/arch/x86/kernel/cpu/resctrl/monitor.c b/arch/x86/kernel/cpu/resctrl/monitor.c
> index d0bbeb410750..2e795b261b6f 100644
> --- a/arch/x86/kernel/cpu/resctrl/monitor.c
> +++ b/arch/x86/kernel/cpu/resctrl/monitor.c
> @@ -16,6 +16,7 @@
> */
>
> #include <linux/cpu.h>
> +#include <linux/cacheinfo.h>

Can this be alphabetical?

> #include <linux/module.h>
> #include <linux/sizes.h>
> #include <linux/slab.h>
> @@ -187,18 +188,8 @@ static inline struct rmid_entry *__rmid_entry(u32 idx)
>
> static int __rmid_read(u32 rmid, enum resctrl_event_id eventid, u64 *val)
> {
> - struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
> - int cpu = smp_processor_id();
> - int rmid_offset = 0;
> u64 msr_val;
>
> - /*
> - * When SNC mode is on, need to compute the offset to read the
> - * physical RMID counter for the node to which this CPU belongs.
> - */
> - if (snc_nodes_per_l3_cache > 1)
> - rmid_offset = (cpu_to_node(cpu) % snc_nodes_per_l3_cache) * r->num_rmid;
> -

This removes code that was just added in previous patch. Can the end goal
be reached without this churn? I expect doing so will make this patch easier to
follow.

> /*
> * As per the SDM, when IA32_QM_EVTSEL.EvtID (bits 7:0) is configured
> * with a valid event code for supported resource type and the bits
> @@ -207,7 +198,7 @@ static int __rmid_read(u32 rmid, enum resctrl_event_id eventid, u64 *val)
> * IA32_QM_CTR.Error (bit 63) and IA32_QM_CTR.Unavailable (bit 62)
> * are error bits.
> */
> - wrmsr(MSR_IA32_QM_EVTSEL, eventid, rmid + rmid_offset);
> + wrmsr(MSR_IA32_QM_EVTSEL, eventid, rmid);
> rdmsrl(MSR_IA32_QM_CTR, msr_val);
>
> if (msr_val & RMID_VAL_ERROR)
> @@ -291,7 +282,7 @@ int resctrl_arch_rmid_read(struct rdt_resource *r, struct rdt_mon_domain *d,
>
> resctrl_arch_rmid_read_context_check();
>
> - if (!cpumask_test_cpu(smp_processor_id(), &d->hdr.cpu_mask))
> + if (d->display_id != get_cpu_cacheinfo_id(smp_processor_id(), r->mon_display_scope))
> return -EINVAL;

Does this mean that when SNC is enabled then reading data for an event within a particular
monitor domain ("node scope") can read its data from any CPU within the L3 domain
("mon_display_scope") even if that CPU is not associated with the node for which it
is reading the data?

If so this really turns many resctrl assumptions and architecture on its head since the
resctrl expectation is that only CPUs within a domain's cpumask can be used to interact
with the domain. This in turn makes this seemingly general feature actually SNC specific.


> ret = __rmid_read(rmid, eventid, &msr_val);
> @@ -556,7 +547,7 @@ static struct mbm_state *get_mbm_state(struct rdt_mon_domain *d, u32 closid,
> }
> }
>
> -static int __mon_event_count(u32 closid, u32 rmid, struct rmid_read *rr)
> +static int ___mon_event_count(u32 closid, u32 rmid, struct rmid_read *rr, u64 *rrval)
> {
> struct mbm_state *m;
> u64 tval = 0;
> @@ -574,11 +565,44 @@ static int __mon_event_count(u32 closid, u32 rmid, struct rmid_read *rr)
> if (rr->err)
> return rr->err;
>
> - rr->val += tval;
> + *rrval += tval;
>

Why is rrval needed?

> return 0;
> }
>
> +static u32 get_node_rmid(struct rdt_resource *r, struct rdt_mon_domain *d, u32 rmid)
> +{
> + int cpu = cpumask_any(&d->hdr.cpu_mask);
> +
> + return rmid + (cpu_to_node(cpu) % snc_nodes_per_l3_cache) * r->num_rmid;
> +}
> +
> +static int __mon_event_count(u32 closid, u32 rmid, struct rmid_read *rr)
> +{
> + struct rdt_mon_domain *d;
> + struct rmid_read tmp;
> + u32 node_rmid;
> + int ret = 0;
> +
> + if (!rr->sumdomains) {
> + node_rmid = get_node_rmid(rr->r, rr->d, rmid);
> + return ___mon_event_count(closid, node_rmid, rr, &rr->val);
> + }
> +
> + tmp = *rr;
> + list_for_each_entry(d, &rr->r->mon_domains, hdr.list) {
> + if (d->display_id == rr->d->display_id) {
> + tmp.d = d;
> + node_rmid = get_node_rmid(rr->r, d, rmid);
> + ret = ___mon_event_count(closid, node_rmid, &tmp, &rr->val);

If I understand correctly this function is run per IPI on a CPU associated
with one of the monitor domains (depends on which one came online first),
and then it will read the monitor data of the other domains from the same
CPU? This is unexpected since the expectation is that monitor data
needs to be read from a CPU associated with the domain it is
reading data for.

Also, providing tmp as well as rr->val seems unnecessary?

> + if (ret)
> + break;
> + }
> + }
> +
> + return ret;
> +}
> +
> /*
> * mbm_bw_count() - Update bw count from values previously read by
> * __mon_event_count().
> diff --git a/arch/x86/kernel/cpu/resctrl/rdtgroup.c b/arch/x86/kernel/cpu/resctrl/rdtgroup.c
> index 0923492a8bd0..a56ae08ca255 100644
> --- a/arch/x86/kernel/cpu/resctrl/rdtgroup.c
> +++ b/arch/x86/kernel/cpu/resctrl/rdtgroup.c
> @@ -3011,57 +3011,118 @@ static int mon_addfile(struct kernfs_node *parent_kn, const char *name,
> * and monitor groups with given domain id.
> */
> static void rmdir_mondata_subdir_allrdtgrp(struct rdt_resource *r,
> - unsigned int dom_id)
> + struct rdt_mon_domain *d)
> {
> struct rdtgroup *prgrp, *crgrp;
> + struct rdt_mon_domain *dom;
> + bool remove_all = true;
> + struct kernfs_node *kn;
> + char subname[32];
> char name[32];
>
> + sprintf(name, "mon_%s_%02d", r->name, d->display_id);
> + if (r->mon_scope != r->mon_display_scope) {
> + int count = 0;
> +
> + list_for_each_entry(dom, &r->mon_domains, hdr.list)
> + if (d->display_id == dom->display_id)
> + count++;
> + if (count > 1) {
> + remove_all = false;
> + sprintf(subname, "mon_sub_%s_%02d", r->name, d->hdr.id);
> + }
> + }


This seems awkward. I wonder if it may not be simpler to just
remove the directory and on completion check if the parent has
any subdirectories left and remove the parent if there are no
subdirectories remaining. Something possible via reading the inode's
i_nlink that is accessible via kernfs_get_inode(). What do you think?

> +
> list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
> - sprintf(name, "mon_%s_%02d", r->name, dom_id);
> - kernfs_remove_by_name(prgrp->mon.mon_data_kn, name);
> + if (remove_all) {
> + kernfs_remove_by_name(prgrp->mon.mon_data_kn, name);
> + } else {
> + kn = kernfs_find_and_get_ns(prgrp->mon.mon_data_kn, name, NULL);
> + if (kn)
> + kernfs_remove_by_name(kn, subname);
> + }
>
> - list_for_each_entry(crgrp, &prgrp->mon.crdtgrp_list, mon.crdtgrp_list)
> - kernfs_remove_by_name(crgrp->mon.mon_data_kn, name);
> + list_for_each_entry(crgrp, &prgrp->mon.crdtgrp_list, mon.crdtgrp_list) {
> + if (remove_all) {
> + kernfs_remove_by_name(crgrp->mon.mon_data_kn, name);
> + } else {
> + kn = kernfs_find_and_get_ns(prgrp->mon.mon_data_kn, name, NULL);
> + if (kn)
> + kernfs_remove_by_name(kn, subname);
> + }
> + }
> }
> }
>
> -static int mkdir_mondata_subdir(struct kernfs_node *parent_kn,
> - struct rdt_mon_domain *d,
> - struct rdt_resource *r, struct rdtgroup *prgrp)
> +static int mon_add_all_files(struct kernfs_node *kn, struct rdt_mon_domain *d,
> + struct rdt_resource *r, struct rdtgroup *prgrp,
> + bool do_sum)
> {
> union mon_data_bits priv;
> - struct kernfs_node *kn;
> struct mon_evt *mevt;
> struct rmid_read rr;
> - char name[32];
> int ret;
>
> - sprintf(name, "mon_%s_%02d", r->name, d->hdr.id);
> - /* create the directory */
> - kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
> - if (IS_ERR(kn))
> - return PTR_ERR(kn);
> -
> - ret = rdtgroup_kn_set_ugid(kn);
> - if (ret)
> - goto out_destroy;
> -
> - if (WARN_ON(list_empty(&r->evt_list))) {
> - ret = -EPERM;
> - goto out_destroy;
> - }
> + if (WARN_ON(list_empty(&r->evt_list)))
> + return -EPERM;
>
> priv.u.rid = r->rid;
> priv.u.domid = d->hdr.id;
> + priv.u.sum = do_sum;
> list_for_each_entry(mevt, &r->evt_list, list) {
> priv.u.evtid = mevt->evtid;
> ret = mon_addfile(kn, mevt->name, priv.priv);
> if (ret)
> - goto out_destroy;
> + return ret;
>
> if (is_mbm_event(mevt->evtid))
> mon_event_read(&rr, r, d, prgrp, mevt->evtid, true);

I do not think that the "do_sum" file should be doing any initialization, this
will be repeated for the "real" mon domain, no?

> }
> +
> + return 0;
> +}
> +
> +static int mkdir_mondata_subdir(struct kernfs_node *parent_kn,
> + struct rdt_mon_domain *d,
> + struct rdt_resource *r, struct rdtgroup *prgrp)
> +{
> + struct kernfs_node *kn, *ckn;
> + char name[32];
> + bool do_sum;
> + int ret;
> +
> + do_sum = r->mon_scope != r->mon_display_scope;
> + sprintf(name, "mon_%s_%02d", r->name, d->display_id);
> + kn = kernfs_find_and_get_ns(parent_kn, name, NULL);
> + if (!kn) {
> + /* create the directory */
> + kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
> + if (IS_ERR(kn))
> + return PTR_ERR(kn);
> +
> + ret = rdtgroup_kn_set_ugid(kn);
> + if (ret)
> + goto out_destroy;
> + ret = mon_add_all_files(kn, d, r, prgrp, do_sum);

This does not look right. If I understand correctly the private data
of these event files will have whichever mon domain came up first as
its domain id. That seems completely arbitrary and does not reflect
accurate state for this file. Since "do_sum" is essentially a "flag"
on how this file can be treated, can its "dom_id" not rather be
the "monitor scope domain id"? Could that not help to eliminate
that per-domain "display_id"?

> + if (ret)
> + goto out_destroy;
> + }
> +
> + if (do_sum) {
> + sprintf(name, "mon_sub_%s_%02d", r->name, d->hdr.id);
> + ckn = kernfs_create_dir(kn, name, parent_kn->mode, prgrp);
> + if (IS_ERR(ckn))
> + goto out_destroy;
> +
> + ret = rdtgroup_kn_set_ugid(ckn);
> + if (ret)
> + goto out_destroy;
> +
> + ret = mon_add_all_files(ckn, d, r, prgrp, false);
> + if (ret)
> + goto out_destroy;
> + }
> +
> kernfs_activate(kn);
> return 0;
>
> @@ -3077,8 +3138,8 @@ static int mkdir_mondata_subdir(struct kernfs_node *parent_kn,
> static void mkdir_mondata_subdir_allrdtgrp(struct rdt_resource *r,
> struct rdt_mon_domain *d)
> {
> - struct kernfs_node *parent_kn;
> struct rdtgroup *prgrp, *crgrp;
> + struct kernfs_node *parent_kn;
> struct list_head *head;
>
> list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
> @@ -3950,7 +4011,7 @@ void resctrl_offline_mon_domain(struct rdt_resource *r, struct rdt_mon_domain *d
> * per domain monitor data directories.
> */
> if (resctrl_mounted && resctrl_arch_mon_capable())
> - rmdir_mondata_subdir_allrdtgrp(r, d->hdr.id);
> + rmdir_mondata_subdir_allrdtgrp(r, d);
>
> if (is_mbm_enabled())
> cancel_delayed_work(&d->mbm_over);

Reinette

2024-05-10 21:25:37

by Reinette Chatre

[permalink] [raw]
Subject: Re: [PATCH v17 8/9] x86/resctrl: Sub NUMA Cluster detection and enable

Hi Tony,

On 5/3/2024 1:33 PM, Tony Luck wrote:
> There isn't a simple hardware bit that indicates whether a CPU is
> running in Sub NUMA Cluster (SNC) mode. Infer the state by comparing
> the ratio of NUMA nodes to L3 cache instances.
>
> When SNC mode is detected, reconfigure the RMID counters by updating
> the MSR_RMID_SNC_CONFIG MSR on each socket as CPUs are seen.
>
> Clearing bit zero of the MSR divides the RMIDs and renumbers the ones
> on the second SNC node to start from zero.
>
> Signed-off-by: Tony Luck <[email protected]>
> ---
> arch/x86/include/asm/msr-index.h | 1 +
> arch/x86/kernel/cpu/resctrl/core.c | 119 +++++++++++++++++++++++++++++
> 2 files changed, 120 insertions(+)
>
> diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-index.h
> index e72c2b872957..ce54a1ffe1e5 100644
> --- a/arch/x86/include/asm/msr-index.h
> +++ b/arch/x86/include/asm/msr-index.h
> @@ -1165,6 +1165,7 @@
> #define MSR_IA32_QM_CTR 0xc8e
> #define MSR_IA32_PQR_ASSOC 0xc8f
> #define MSR_IA32_L3_CBM_BASE 0xc90
> +#define MSR_RMID_SNC_CONFIG 0xca0
> #define MSR_IA32_L2_CBM_BASE 0xd10
> #define MSR_IA32_MBA_THRTL_BASE 0xd50
>
> diff --git a/arch/x86/kernel/cpu/resctrl/core.c b/arch/x86/kernel/cpu/resctrl/core.c
> index a949e69308cd..6a1727ea1dfe 100644
> --- a/arch/x86/kernel/cpu/resctrl/core.c
> +++ b/arch/x86/kernel/cpu/resctrl/core.c
> @@ -21,7 +21,9 @@
> #include <linux/err.h>
> #include <linux/cacheinfo.h>
> #include <linux/cpuhotplug.h>
> +#include <linux/mod_devicetable.h>
>
> +#include <asm/cpu_device_id.h>
> #include <asm/intel-family.h>
> #include <asm/resctrl.h>
> #include "internal.h"
> @@ -746,11 +748,42 @@ static void clear_closid_rmid(int cpu)
> RESCTRL_RESERVED_CLOSID);
> }
>
> +/*
> + * The power-on reset value of MSR_RMID_SNC_CONFIG is 0x1
> + * which indicates that RMIDs are configured in legacy mode.
> + * This mode is incompatible with Linux resctrl semantics
> + * as RMIDs are partitioned between SNC nodes, which requires
> + * a user to know which RMID is allocated to a task.
> + * Clearing bit 0 reconfigures the RMID counters for use
> + * in Sub NUMA Cluster mode. This mode is better for Linux.
> + * The RMID space is divided between all SNC nodes with the
> + * RMIDs renumbered to start from zero in each node when
> + * couning operations from tasks. Code to read the counters
> + * must adjust RMID counter numbers based on SNC node. See
> + * __rmid_read() for code that does this.
> + */
> +static void snc_remap_rmids(int cpu)
> +{
> + u64 val;
> +
> + /* Only need to enable once per package. */
> + if (cpumask_first(topology_core_cpumask(cpu)) != cpu)
> + return;
> +
> + rdmsrl(MSR_RMID_SNC_CONFIG, val);
> + val &= ~BIT_ULL(0);
> + wrmsrl(MSR_RMID_SNC_CONFIG, val);
> +}
> +
> static int resctrl_arch_online_cpu(unsigned int cpu)
> {
> struct rdt_resource *r;
>
> mutex_lock(&domain_list_lock);
> +
> + if (snc_nodes_per_l3_cache > 1)
> + snc_remap_rmids(cpu);
> +
> for_each_capable_rdt_resource(r)
> domain_add_cpu(cpu, r);
> mutex_unlock(&domain_list_lock);
> @@ -990,11 +1023,97 @@ static __init bool get_rdt_resources(void)
> return (rdt_mon_capable || rdt_alloc_capable);
> }
>
> +/* CPU models that support MSR_RMID_SNC_CONFIG */
> +static const struct x86_cpu_id snc_cpu_ids[] __initconst = {
> + X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_X, 0),
> + X86_MATCH_INTEL_FAM6_MODEL(SAPPHIRERAPIDS_X, 0),
> + X86_MATCH_INTEL_FAM6_MODEL(EMERALDRAPIDS_X, 0),
> + X86_MATCH_INTEL_FAM6_MODEL(GRANITERAPIDS_X, 0),
> + X86_MATCH_INTEL_FAM6_MODEL(ATOM_CRESTMONT_X, 0),
> + {}
> +};
> +
> +/*
> + * There isn't a simple hardware bit that indicates whether a CPU is running
> + * in Sub NUMA Cluster (SNC) mode. Infer the state by comparing the
> + * ratio of NUMA nodes to L3 cache instances.
> + * It is not possible to accurately determine SNC state if the system is
> + * booted with a maxcpus=N parameter. That distorts the ratio of SNC nodes
> + * to L3 caches. It will be OK if system is booted with hyperthreading
> + * disabled (since this doesn't affect the ratio).
> + */
> +static __init int snc_get_config(void)
> +{
> + unsigned long *node_caches;
> + int mem_only_nodes = 0;
> + int cpu, node, ret;
> + int num_l3_caches;
> + int cache_id;
> +
> + if (!x86_match_cpu(snc_cpu_ids))
> + return 1;
> +
> + node_caches = bitmap_zalloc(num_possible_cpus(), GFP_KERNEL);
> + if (!node_caches)
> + return 1;
> +
> + cpus_read_lock();
> +
> + if (num_online_cpus() != num_present_cpus())
> + pr_warn("Some CPUs offline, SNC detection may be incorrect\n");
> +
> + for_each_node(node) {
> + cpu = cpumask_first(cpumask_of_node(node));
> + if (cpu < nr_cpu_ids) {
> + cache_id = get_cpu_cacheinfo_id(cpu, 3);
> + if (cache_id != -1)
> + set_bit(cache_id, node_caches);
> + } else {
> + mem_only_nodes++;
> + }
> + }
> + cpus_read_unlock();
> +
> + num_l3_caches = bitmap_weight(node_caches, num_possible_cpus());
> + kfree(node_caches);
> +
> + if (!num_l3_caches)
> + goto insane;
> +
> + /* sanity check #1: Number of CPU nodes must be multiple of num_l3_caches */
> + if ((nr_node_ids - mem_only_nodes) % num_l3_caches)
> + goto insane;
> +
> + ret = (nr_node_ids - mem_only_nodes) / num_l3_caches;
> +
> + /* sanity check #2: Only valid results are 1, 2, 3, 4 */
> + switch (ret) {
> + case 1:
> + break;
> + case 2:
> + case 3:
> + case 4:
> + pr_info("Sub-NUMA cluster detected with %d nodes per L3 cache\n", ret);
> + rdt_resources_all[RDT_RESOURCE_L3].r_resctrl.mon_scope = RESCTRL_NODE;
> + break;
> + default:
> + goto insane;
> + }
> +
> + return ret;
> +insane:
> + pr_warn("SNC insanity: CPU nodes = %d num_l3_caches = %d\n",
> + (nr_node_ids - mem_only_nodes), num_l3_caches);
> + return 1;
> +}

I find it confusing how dramatically this SNC detection code changed without
any explanations. This detection seems to match the SNC detection code from v16 but
after v16 you posted a new SNC detection implementation that did SNC detection totally
differently [1] from v16. Instead of keeping with the "new" detection this implements
what was in v16. Could you please help me understand what motivated the different
implementations and why the big differences?

Reinette


[1] https://lore.kernel.org/lkml/[email protected]/

2024-05-13 17:05:57

by Luck, Tony

[permalink] [raw]
Subject: Re: [PATCH v17 7/9] x86/resctrl: Add new monitor files for Sub-NUMA cluster (SNC) monitoring

On Fri, May 10, 2024 at 02:24:13PM -0700, Reinette Chatre wrote:
> Hi Tony,

Hi Reinette,

Thanks for the review. Detailed comments below. But overall I'm
going to split patch 7 into a bunch of smaller changes, each with
a better commit message.

> On 5/3/2024 1:33 PM, Tony Luck wrote:
>
> (Could you please start the changelog with some context?)
>
> > Add a field to the rdt_resource structure to track whether monitoring
> > resources are tracked by hardware at a different scope (NODE) from
> > the legacy L3 scope.
>
> This seems to describe @mon_scope that was introduced in patch #3?

Not really. Patch #3 made the change so that control an monitor
functions can have different scope. That's still needed as with SNC
enabled the underlying data collection is at the node level for
monitoring, while control stays at the L3 cache scope.

This new field describes the legacy scope of monitoring, so that
resctrl can provide correctly scoped monitor files for legacy
applications that aren't aware of SNC. So I'm using this both
to indicate when SNC is enabled (with mon_scope != mon_display_scope)
or disabled (when they are the same).

> >
> > Add a field to the rdt_mon_domain structure to track the L3 cache id
> > which can be used to find all the domains that need resource counts
> > summed to provide accurate values in the legacy monitoring files.
>
> Why is this field necessary? Can this not be obtained dynamically?

I could compute it each time I need it (when making/removing
directories, or finding which SNC domains share an L3 domain).

id = get_domain_id_from_scope(cpumask_any(&d->cpu_mask), r->mon_display_scope);
if (id < 0)
// error path

But it seemed better to just discover this once at domain creation time.

>
> >
> > When SNC is enabled create extra directories and files in each mon_data
> > directory to report per-SNC node counts.
>
> The above cryptic sentence is the closest the changelog gets to explaining
> what this patch aims to do. Could you please enhance the changelog to
> describe what this patch aims to do and more importantly how it goes about
> doing so? This patch contains a significant number of undocumented quirks
> and between the cryptic changelog and undocumented quirks in the patch I find
> it very hard to understand what it is trying to do and why.
>
> >
> > Signed-off-by: Tony Luck <[email protected]>
> > ---
> > include/linux/resctrl.h | 4 +
> > arch/x86/kernel/cpu/resctrl/internal.h | 5 +-
> > arch/x86/kernel/cpu/resctrl/core.c | 2 +
> > arch/x86/kernel/cpu/resctrl/ctrlmondata.c | 1 +
> > arch/x86/kernel/cpu/resctrl/monitor.c | 52 +++++++---
> > arch/x86/kernel/cpu/resctrl/rdtgroup.c | 115 +++++++++++++++++-----
> > 6 files changed, 137 insertions(+), 42 deletions(-)
> >
> > diff --git a/include/linux/resctrl.h b/include/linux/resctrl.h
> > index 5c7775343c3e..2f8ac925bc18 100644
> > --- a/include/linux/resctrl.h
> > +++ b/include/linux/resctrl.h
> > @@ -96,6 +96,7 @@ struct rdt_ctrl_domain {
> > /**
> > * struct rdt_mon_domain - group of CPUs sharing a resctrl monitor resource
> > * @hdr: common header for different domain types
> > + * @display_id: shared id used to identify domains to be summed for display
> > * @rmid_busy_llc: bitmap of which limbo RMIDs are above threshold
> > * @mbm_total: saved state for MBM total bandwidth
> > * @mbm_local: saved state for MBM local bandwidth
> > @@ -106,6 +107,7 @@ struct rdt_ctrl_domain {
> > */
> > struct rdt_mon_domain {
> > struct rdt_domain_hdr hdr;
> > + int display_id;
>
> (it is not clear to me why this is needed)

Described above. I will include that when I split this into its own
patch.

> > unsigned long *rmid_busy_llc;
> > struct mbm_state *mbm_total;
> > struct mbm_state *mbm_local;
> > @@ -187,6 +189,7 @@ enum resctrl_scope {
> > * @num_rmid: Number of RMIDs available
> > * @ctrl_scope: Scope of this resource for control functions
> > * @mon_scope: Scope of this resource for monitor functions
> > + * @mon_display_scope: Scope for user reporting monitor functions
> > * @cache: Cache allocation related data
> > * @membw: If the component has bandwidth controls, their properties.
> > * @ctrl_domains: RCU list of all control domains for this resource
> > @@ -207,6 +210,7 @@ struct rdt_resource {
> > int num_rmid;
> > enum resctrl_scope ctrl_scope;
> > enum resctrl_scope mon_scope;
> > + enum resctrl_scope mon_display_scope;
> > struct resctrl_cache cache;
> > struct resctrl_membw membw;
> > struct list_head ctrl_domains;
> > diff --git a/arch/x86/kernel/cpu/resctrl/internal.h b/arch/x86/kernel/cpu/resctrl/internal.h
> > index 49440f194253..d41b388bb499 100644
> > --- a/arch/x86/kernel/cpu/resctrl/internal.h
> > +++ b/arch/x86/kernel/cpu/resctrl/internal.h
> > @@ -132,6 +132,7 @@ struct mon_evt {
> > * as kernfs private data
> > * @rid: Resource id associated with the event file
> > * @evtid: Event id associated with the event file
> > + * @sum: Sum across domains with same display_id
> > * @domid: The domain to which the event file belongs
> > * @u: Name of the bit fields struct
> > */
> > @@ -139,7 +140,8 @@ union mon_data_bits {
> > void *priv;
> > struct {
> > unsigned int rid : 10;
> > - enum resctrl_event_id evtid : 8;
> > + enum resctrl_event_id evtid : 7;
> > + unsigned int sum : 1;
> > unsigned int domid : 14;
> > } u;
>
> (No explanation about why evtid had to shrink and why it is ok
> to do so.)

Will split this into its own patch and provide description of need
and safety.

> > };
> > @@ -150,6 +152,7 @@ struct rmid_read {
> > struct rdt_mon_domain *d;
> > enum resctrl_event_id evtid;
> > bool first;
> > + bool sumdomains;
> > int err;
> > u64 val;
> > void *arch_mon_ctx;
> > diff --git a/arch/x86/kernel/cpu/resctrl/core.c b/arch/x86/kernel/cpu/resctrl/core.c
> > index cb181796f73b..a949e69308cd 100644
> > --- a/arch/x86/kernel/cpu/resctrl/core.c
> > +++ b/arch/x86/kernel/cpu/resctrl/core.c
> > @@ -71,6 +71,7 @@ struct rdt_hw_resource rdt_resources_all[] = {
> > .name = "L3",
> > .ctrl_scope = RESCTRL_L3_CACHE,
> > .mon_scope = RESCTRL_L3_CACHE,
> > + .mon_display_scope = RESCTRL_L3_CACHE,
> > .ctrl_domains = ctrl_domain_init(RDT_RESOURCE_L3),
> > .mon_domains = mon_domain_init(RDT_RESOURCE_L3),
> > .parse_ctrlval = parse_cbm,
> > @@ -613,6 +614,7 @@ static void domain_add_cpu_mon(int cpu, struct rdt_resource *r)
> >
> > d = &hw_dom->d_resctrl;
> > d->hdr.id = id;
> > + d->display_id = get_domain_id_from_scope(cpu, r->mon_display_scope);
> > d->hdr.type = RESCTRL_MON_DOMAIN;
> > cpumask_set_cpu(cpu, &d->hdr.cpu_mask);
> >
> > diff --git a/arch/x86/kernel/cpu/resctrl/ctrlmondata.c b/arch/x86/kernel/cpu/resctrl/ctrlmondata.c
> > index 3b9383612c35..a4ead8ffbaf3 100644
> > --- a/arch/x86/kernel/cpu/resctrl/ctrlmondata.c
> > +++ b/arch/x86/kernel/cpu/resctrl/ctrlmondata.c
> > @@ -575,6 +575,7 @@ int rdtgroup_mondata_show(struct seq_file *m, void *arg)
> > resid = md.u.rid;
> > domid = md.u.domid;
> > evtid = md.u.evtid;
> > + rr.sumdomains = md.u.sum;
> >
> > r = &rdt_resources_all[resid].r_resctrl;
> > hdr = rdt_find_domain(&r->mon_domains, domid, NULL);
> > diff --git a/arch/x86/kernel/cpu/resctrl/monitor.c b/arch/x86/kernel/cpu/resctrl/monitor.c
> > index d0bbeb410750..2e795b261b6f 100644
> > --- a/arch/x86/kernel/cpu/resctrl/monitor.c
> > +++ b/arch/x86/kernel/cpu/resctrl/monitor.c
> > @@ -16,6 +16,7 @@
> > */
> >
> > #include <linux/cpu.h>
> > +#include <linux/cacheinfo.h>
>
> Can this be alphabetical?

Sure. Will fix.

> > #include <linux/module.h>
> > #include <linux/sizes.h>
> > #include <linux/slab.h>
> > @@ -187,18 +188,8 @@ static inline struct rmid_entry *__rmid_entry(u32 idx)
> >
> > static int __rmid_read(u32 rmid, enum resctrl_event_id eventid, u64 *val)
> > {
> > - struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
> > - int cpu = smp_processor_id();
> > - int rmid_offset = 0;
> > u64 msr_val;
> >
> > - /*
> > - * When SNC mode is on, need to compute the offset to read the
> > - * physical RMID counter for the node to which this CPU belongs.
> > - */
> > - if (snc_nodes_per_l3_cache > 1)
> > - rmid_offset = (cpu_to_node(cpu) % snc_nodes_per_l3_cache) * r->num_rmid;
> > -
>
> This removes code that was just added in previous patch. Can the end goal
> be reached without this churn? I expect doing so will make this patch easier to
> follow.

Oops, yes. I will delete this from patch 6 to avoid churn.

> > /*
> > * As per the SDM, when IA32_QM_EVTSEL.EvtID (bits 7:0) is configured
> > * with a valid event code for supported resource type and the bits
> > @@ -207,7 +198,7 @@ static int __rmid_read(u32 rmid, enum resctrl_event_id eventid, u64 *val)
> > * IA32_QM_CTR.Error (bit 63) and IA32_QM_CTR.Unavailable (bit 62)
> > * are error bits.
> > */
> > - wrmsr(MSR_IA32_QM_EVTSEL, eventid, rmid + rmid_offset);
> > + wrmsr(MSR_IA32_QM_EVTSEL, eventid, rmid);
> > rdmsrl(MSR_IA32_QM_CTR, msr_val);
> >
> > if (msr_val & RMID_VAL_ERROR)
> > @@ -291,7 +282,7 @@ int resctrl_arch_rmid_read(struct rdt_resource *r, struct rdt_mon_domain *d,
> >
> > resctrl_arch_rmid_read_context_check();
> >
> > - if (!cpumask_test_cpu(smp_processor_id(), &d->hdr.cpu_mask))
> > + if (d->display_id != get_cpu_cacheinfo_id(smp_processor_id(), r->mon_display_scope))
> > return -EINVAL;
>
> Does this mean that when SNC is enabled then reading data for an event within a particular
> monitor domain ("node scope") can read its data from any CPU within the L3 domain
> ("mon_display_scope") even if that CPU is not associated with the node for which it
> is reading the data?

Yes.

> If so this really turns many resctrl assumptions and architecture on its head since the
> resctrl expectation is that only CPUs within a domain's cpumask can be used to interact
> with the domain. This in turn makes this seemingly general feature actually SNC specific.

This is only an expectation for x86 features using IA32_QM_EVTSEL/IA32_QM_CTR
MSR method to read counters. ARM doesn't have the "CPU must be in
domain" restriction (as far as I can tell). Nor does the Intel IO RDT
(which uses MMIO space for control registers, these can be read/written
from any CPU).

We do know that those two MSRs can be read from any CPU that shares an
L3 cache. It would seem to be pointless overhead to force a cross
processor interrupt to read them from a different CPU just to satisfy
a "must be in same domain" non-requirement. I'l split this into its
own patch with suitable description.

> > ret = __rmid_read(rmid, eventid, &msr_val);
> > @@ -556,7 +547,7 @@ static struct mbm_state *get_mbm_state(struct rdt_mon_domain *d, u32 closid,
> > }
> > }
> >
> > -static int __mon_event_count(u32 closid, u32 rmid, struct rmid_read *rr)
> > +static int ___mon_event_count(u32 closid, u32 rmid, struct rmid_read *rr, u64 *rrval)
> > {
> > struct mbm_state *m;
> > u64 tval = 0;
> > @@ -574,11 +565,44 @@ static int __mon_event_count(u32 closid, u32 rmid, struct rmid_read *rr)
> > if (rr->err)
> > return rr->err;
> >
> > - rr->val += tval;
> > + *rrval += tval;
> >
>
> Why is rrval needed?

I don't think it is anymore. I think I wanted it while I was developing
this set of changes. But I will drop it.

> > return 0;
> > }
> >
> > +static u32 get_node_rmid(struct rdt_resource *r, struct rdt_mon_domain *d, u32 rmid)
> > +{
> > + int cpu = cpumask_any(&d->hdr.cpu_mask);
> > +
> > + return rmid + (cpu_to_node(cpu) % snc_nodes_per_l3_cache) * r->num_rmid;
> > +}
> > +
> > +static int __mon_event_count(u32 closid, u32 rmid, struct rmid_read *rr)
> > +{
> > + struct rdt_mon_domain *d;
> > + struct rmid_read tmp;
> > + u32 node_rmid;
> > + int ret = 0;
> > +
> > + if (!rr->sumdomains) {
> > + node_rmid = get_node_rmid(rr->r, rr->d, rmid);
> > + return ___mon_event_count(closid, node_rmid, rr, &rr->val);
> > + }
> > +
> > + tmp = *rr;
> > + list_for_each_entry(d, &rr->r->mon_domains, hdr.list) {
> > + if (d->display_id == rr->d->display_id) {
> > + tmp.d = d;
> > + node_rmid = get_node_rmid(rr->r, d, rmid);
> > + ret = ___mon_event_count(closid, node_rmid, &tmp, &rr->val);
>
> If I understand correctly this function is run per IPI on a CPU associated
> with one of the monitor domains (depends on which one came online first),
> and then it will read the monitor data of the other domains from the same
> CPU? This is unexpected since the expectation is that monitor data
> needs to be read from a CPU associated with the domain it is
> reading data for.

See earlier note. The counter can be read from any CPU sharing the same
L3. Adding unnecessary IPI is pointless overhead. But I will add
comments.

> Also, providing tmp as well as rr->val seems unnecessary?

I think I was unsure about modifying the domain field in the struct
rmid_read in the middle of the call chain. But the original caller
mon_event_read() doesn't look at rr->domain after the smp_call*()
function returns. I will drop "tmp".

> > + if (ret)
> > + break;
> > + }
> > + }
> > +
> > + return ret;
> > +}
> > +
> > /*
> > * mbm_bw_count() - Update bw count from values previously read by
> > * __mon_event_count().
> > diff --git a/arch/x86/kernel/cpu/resctrl/rdtgroup.c b/arch/x86/kernel/cpu/resctrl/rdtgroup.c
> > index 0923492a8bd0..a56ae08ca255 100644
> > --- a/arch/x86/kernel/cpu/resctrl/rdtgroup.c
> > +++ b/arch/x86/kernel/cpu/resctrl/rdtgroup.c
> > @@ -3011,57 +3011,118 @@ static int mon_addfile(struct kernfs_node *parent_kn, const char *name,
> > * and monitor groups with given domain id.
> > */
> > static void rmdir_mondata_subdir_allrdtgrp(struct rdt_resource *r,
> > - unsigned int dom_id)
> > + struct rdt_mon_domain *d)
> > {
> > struct rdtgroup *prgrp, *crgrp;
> > + struct rdt_mon_domain *dom;
> > + bool remove_all = true;
> > + struct kernfs_node *kn;
> > + char subname[32];
> > char name[32];
> >
> > + sprintf(name, "mon_%s_%02d", r->name, d->display_id);
> > + if (r->mon_scope != r->mon_display_scope) {
> > + int count = 0;
> > +
> > + list_for_each_entry(dom, &r->mon_domains, hdr.list)
> > + if (d->display_id == dom->display_id)
> > + count++;
> > + if (count > 1) {
> > + remove_all = false;
> > + sprintf(subname, "mon_sub_%s_%02d", r->name, d->hdr.id);
> > + }
> > + }
>
>
> This seems awkward. I wonder if it may not be simpler to just
> remove the directory and on completion check if the parent has
> any subdirectories left and remove the parent if there are no
> subdirectories remaining. Something possible via reading the inode's
> i_nlink that is accessible via kernfs_get_inode(). What do you think?

kernfs_get_inode() needs a pointer to the "struct super_block" for the
filesystem. Resctrl filesystem code doesn't seem to keep track of that
anywhere. Only mentioned in rdt_kill_sb() where core kernfs code passes
it in as the argument. When registering/mounting the resctrl filesystem
there's a "struct fs_context *fc" ... is there a function to get the
super block from that? Even if there is, I'd need to add a global to
save a copy of the fc_context.

> > +
> > list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
> > - sprintf(name, "mon_%s_%02d", r->name, dom_id);
> > - kernfs_remove_by_name(prgrp->mon.mon_data_kn, name);
> > + if (remove_all) {
> > + kernfs_remove_by_name(prgrp->mon.mon_data_kn, name);
> > + } else {
> > + kn = kernfs_find_and_get_ns(prgrp->mon.mon_data_kn, name, NULL);
> > + if (kn)
> > + kernfs_remove_by_name(kn, subname);
> > + }
> >
> > - list_for_each_entry(crgrp, &prgrp->mon.crdtgrp_list, mon.crdtgrp_list)
> > - kernfs_remove_by_name(crgrp->mon.mon_data_kn, name);
> > + list_for_each_entry(crgrp, &prgrp->mon.crdtgrp_list, mon.crdtgrp_list) {
> > + if (remove_all) {
> > + kernfs_remove_by_name(crgrp->mon.mon_data_kn, name);
> > + } else {
> > + kn = kernfs_find_and_get_ns(prgrp->mon.mon_data_kn, name, NULL);
> > + if (kn)
> > + kernfs_remove_by_name(kn, subname);
> > + }
> > + }
> > }
> > }
> >
> > -static int mkdir_mondata_subdir(struct kernfs_node *parent_kn,
> > - struct rdt_mon_domain *d,
> > - struct rdt_resource *r, struct rdtgroup *prgrp)
> > +static int mon_add_all_files(struct kernfs_node *kn, struct rdt_mon_domain *d,
> > + struct rdt_resource *r, struct rdtgroup *prgrp,
> > + bool do_sum)
> > {
> > union mon_data_bits priv;
> > - struct kernfs_node *kn;
> > struct mon_evt *mevt;
> > struct rmid_read rr;
> > - char name[32];
> > int ret;
> >
> > - sprintf(name, "mon_%s_%02d", r->name, d->hdr.id);
> > - /* create the directory */
> > - kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
> > - if (IS_ERR(kn))
> > - return PTR_ERR(kn);
> > -
> > - ret = rdtgroup_kn_set_ugid(kn);
> > - if (ret)
> > - goto out_destroy;
> > -
> > - if (WARN_ON(list_empty(&r->evt_list))) {
> > - ret = -EPERM;
> > - goto out_destroy;
> > - }
> > + if (WARN_ON(list_empty(&r->evt_list)))
> > + return -EPERM;
> >
> > priv.u.rid = r->rid;
> > priv.u.domid = d->hdr.id;
> > + priv.u.sum = do_sum;
> > list_for_each_entry(mevt, &r->evt_list, list) {
> > priv.u.evtid = mevt->evtid;
> > ret = mon_addfile(kn, mevt->name, priv.priv);
> > if (ret)
> > - goto out_destroy;
> > + return ret;
> >
> > if (is_mbm_event(mevt->evtid))
> > mon_event_read(&rr, r, d, prgrp, mevt->evtid, true);
>
> I do not think that the "do_sum" file should be doing any initialization, this
> will be repeated for the "real" mon domain, no?

Good point. I'll drop from the "sum" files and just run it for the
"real" ones.

> > }
> > +
> > + return 0;
> > +}
> > +
> > +static int mkdir_mondata_subdir(struct kernfs_node *parent_kn,
> > + struct rdt_mon_domain *d,
> > + struct rdt_resource *r, struct rdtgroup *prgrp)
> > +{
> > + struct kernfs_node *kn, *ckn;
> > + char name[32];
> > + bool do_sum;
> > + int ret;
> > +
> > + do_sum = r->mon_scope != r->mon_display_scope;
> > + sprintf(name, "mon_%s_%02d", r->name, d->display_id);
> > + kn = kernfs_find_and_get_ns(parent_kn, name, NULL);
> > + if (!kn) {
> > + /* create the directory */
> > + kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
> > + if (IS_ERR(kn))
> > + return PTR_ERR(kn);
> > +
> > + ret = rdtgroup_kn_set_ugid(kn);
> > + if (ret)
> > + goto out_destroy;
> > + ret = mon_add_all_files(kn, d, r, prgrp, do_sum);
>
> This does not look right. If I understand correctly the private data
> of these event files will have whichever mon domain came up first as
> its domain id. That seems completely arbitrary and does not reflect
> accurate state for this file. Since "do_sum" is essentially a "flag"
> on how this file can be treated, can its "dom_id" not rather be
> the "monitor scope domain id"? Could that not help to eliminate
> that per-domain "display_id"?

You are correct that this should be the "monitor scope domain id" rather
than the first SNC domain that appears. I'll change to use that. I don't
think it helps in removing the per-domain display_id.

> > + if (ret)
> > + goto out_destroy;
> > + }
> > +
> > + if (do_sum) {
> > + sprintf(name, "mon_sub_%s_%02d", r->name, d->hdr.id);
> > + ckn = kernfs_create_dir(kn, name, parent_kn->mode, prgrp);
> > + if (IS_ERR(ckn))
> > + goto out_destroy;
> > +
> > + ret = rdtgroup_kn_set_ugid(ckn);
> > + if (ret)
> > + goto out_destroy;
> > +
> > + ret = mon_add_all_files(ckn, d, r, prgrp, false);
> > + if (ret)
> > + goto out_destroy;
> > + }
> > +
> > kernfs_activate(kn);
> > return 0;
> >
> > @@ -3077,8 +3138,8 @@ static int mkdir_mondata_subdir(struct kernfs_node *parent_kn,
> > static void mkdir_mondata_subdir_allrdtgrp(struct rdt_resource *r,
> > struct rdt_mon_domain *d)
> > {
> > - struct kernfs_node *parent_kn;
> > struct rdtgroup *prgrp, *crgrp;
> > + struct kernfs_node *parent_kn;
> > struct list_head *head;
> >
> > list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
> > @@ -3950,7 +4011,7 @@ void resctrl_offline_mon_domain(struct rdt_resource *r, struct rdt_mon_domain *d
> > * per domain monitor data directories.
> > */
> > if (resctrl_mounted && resctrl_arch_mon_capable())
> > - rmdir_mondata_subdir_allrdtgrp(r, d->hdr.id);
> > + rmdir_mondata_subdir_allrdtgrp(r, d);
> >
> > if (is_mbm_enabled())
> > cancel_delayed_work(&d->mbm_over);
>
> Reinette

-Tony

2024-05-13 17:18:10

by Luck, Tony

[permalink] [raw]
Subject: Re: [PATCH v17 8/9] x86/resctrl: Sub NUMA Cluster detection and enable

On Fri, May 10, 2024 at 02:24:49PM -0700, Reinette Chatre wrote:
> Hi Tony,
>
> On 5/3/2024 1:33 PM, Tony Luck wrote:
> > There isn't a simple hardware bit that indicates whether a CPU is
> > running in Sub NUMA Cluster (SNC) mode. Infer the state by comparing
> > the ratio of NUMA nodes to L3 cache instances.
> >
> > When SNC mode is detected, reconfigure the RMID counters by updating
> > the MSR_RMID_SNC_CONFIG MSR on each socket as CPUs are seen.
> >
> > Clearing bit zero of the MSR divides the RMIDs and renumbers the ones
> > on the second SNC node to start from zero.
> >
> > Signed-off-by: Tony Luck <[email protected]>
> > ---
> > arch/x86/include/asm/msr-index.h | 1 +
> > arch/x86/kernel/cpu/resctrl/core.c | 119 +++++++++++++++++++++++++++++
> > 2 files changed, 120 insertions(+)
> >
> > diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-index.h
> > index e72c2b872957..ce54a1ffe1e5 100644
> > --- a/arch/x86/include/asm/msr-index.h
> > +++ b/arch/x86/include/asm/msr-index.h
> > @@ -1165,6 +1165,7 @@
> > #define MSR_IA32_QM_CTR 0xc8e
> > #define MSR_IA32_PQR_ASSOC 0xc8f
> > #define MSR_IA32_L3_CBM_BASE 0xc90
> > +#define MSR_RMID_SNC_CONFIG 0xca0
> > #define MSR_IA32_L2_CBM_BASE 0xd10
> > #define MSR_IA32_MBA_THRTL_BASE 0xd50
> >
> > diff --git a/arch/x86/kernel/cpu/resctrl/core.c b/arch/x86/kernel/cpu/resctrl/core.c
> > index a949e69308cd..6a1727ea1dfe 100644
> > --- a/arch/x86/kernel/cpu/resctrl/core.c
> > +++ b/arch/x86/kernel/cpu/resctrl/core.c
> > @@ -21,7 +21,9 @@
> > #include <linux/err.h>
> > #include <linux/cacheinfo.h>
> > #include <linux/cpuhotplug.h>
> > +#include <linux/mod_devicetable.h>
> >
> > +#include <asm/cpu_device_id.h>
> > #include <asm/intel-family.h>
> > #include <asm/resctrl.h>
> > #include "internal.h"
> > @@ -746,11 +748,42 @@ static void clear_closid_rmid(int cpu)
> > RESCTRL_RESERVED_CLOSID);
> > }
> >
> > +/*
> > + * The power-on reset value of MSR_RMID_SNC_CONFIG is 0x1
> > + * which indicates that RMIDs are configured in legacy mode.
> > + * This mode is incompatible with Linux resctrl semantics
> > + * as RMIDs are partitioned between SNC nodes, which requires
> > + * a user to know which RMID is allocated to a task.
> > + * Clearing bit 0 reconfigures the RMID counters for use
> > + * in Sub NUMA Cluster mode. This mode is better for Linux.
> > + * The RMID space is divided between all SNC nodes with the
> > + * RMIDs renumbered to start from zero in each node when
> > + * couning operations from tasks. Code to read the counters
> > + * must adjust RMID counter numbers based on SNC node. See
> > + * __rmid_read() for code that does this.
> > + */
> > +static void snc_remap_rmids(int cpu)
> > +{
> > + u64 val;
> > +
> > + /* Only need to enable once per package. */
> > + if (cpumask_first(topology_core_cpumask(cpu)) != cpu)
> > + return;
> > +
> > + rdmsrl(MSR_RMID_SNC_CONFIG, val);
> > + val &= ~BIT_ULL(0);
> > + wrmsrl(MSR_RMID_SNC_CONFIG, val);
> > +}
> > +
> > static int resctrl_arch_online_cpu(unsigned int cpu)
> > {
> > struct rdt_resource *r;
> >
> > mutex_lock(&domain_list_lock);
> > +
> > + if (snc_nodes_per_l3_cache > 1)
> > + snc_remap_rmids(cpu);
> > +
> > for_each_capable_rdt_resource(r)
> > domain_add_cpu(cpu, r);
> > mutex_unlock(&domain_list_lock);
> > @@ -990,11 +1023,97 @@ static __init bool get_rdt_resources(void)
> > return (rdt_mon_capable || rdt_alloc_capable);
> > }
> >
> > +/* CPU models that support MSR_RMID_SNC_CONFIG */
> > +static const struct x86_cpu_id snc_cpu_ids[] __initconst = {
> > + X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_X, 0),
> > + X86_MATCH_INTEL_FAM6_MODEL(SAPPHIRERAPIDS_X, 0),
> > + X86_MATCH_INTEL_FAM6_MODEL(EMERALDRAPIDS_X, 0),
> > + X86_MATCH_INTEL_FAM6_MODEL(GRANITERAPIDS_X, 0),
> > + X86_MATCH_INTEL_FAM6_MODEL(ATOM_CRESTMONT_X, 0),
> > + {}
> > +};
> > +
> > +/*
> > + * There isn't a simple hardware bit that indicates whether a CPU is running
> > + * in Sub NUMA Cluster (SNC) mode. Infer the state by comparing the
> > + * ratio of NUMA nodes to L3 cache instances.
> > + * It is not possible to accurately determine SNC state if the system is
> > + * booted with a maxcpus=N parameter. That distorts the ratio of SNC nodes
> > + * to L3 caches. It will be OK if system is booted with hyperthreading
> > + * disabled (since this doesn't affect the ratio).
> > + */
> > +static __init int snc_get_config(void)
> > +{
> > + unsigned long *node_caches;
> > + int mem_only_nodes = 0;
> > + int cpu, node, ret;
> > + int num_l3_caches;
> > + int cache_id;
> > +
> > + if (!x86_match_cpu(snc_cpu_ids))
> > + return 1;
> > +
> > + node_caches = bitmap_zalloc(num_possible_cpus(), GFP_KERNEL);
> > + if (!node_caches)
> > + return 1;
> > +
> > + cpus_read_lock();
> > +
> > + if (num_online_cpus() != num_present_cpus())
> > + pr_warn("Some CPUs offline, SNC detection may be incorrect\n");
> > +
> > + for_each_node(node) {
> > + cpu = cpumask_first(cpumask_of_node(node));
> > + if (cpu < nr_cpu_ids) {
> > + cache_id = get_cpu_cacheinfo_id(cpu, 3);
> > + if (cache_id != -1)
> > + set_bit(cache_id, node_caches);
> > + } else {
> > + mem_only_nodes++;
> > + }
> > + }
> > + cpus_read_unlock();
> > +
> > + num_l3_caches = bitmap_weight(node_caches, num_possible_cpus());
> > + kfree(node_caches);
> > +
> > + if (!num_l3_caches)
> > + goto insane;
> > +
> > + /* sanity check #1: Number of CPU nodes must be multiple of num_l3_caches */
> > + if ((nr_node_ids - mem_only_nodes) % num_l3_caches)
> > + goto insane;
> > +
> > + ret = (nr_node_ids - mem_only_nodes) / num_l3_caches;
> > +
> > + /* sanity check #2: Only valid results are 1, 2, 3, 4 */
> > + switch (ret) {
> > + case 1:
> > + break;
> > + case 2:
> > + case 3:
> > + case 4:
> > + pr_info("Sub-NUMA cluster detected with %d nodes per L3 cache\n", ret);
> > + rdt_resources_all[RDT_RESOURCE_L3].r_resctrl.mon_scope = RESCTRL_NODE;
> > + break;
> > + default:
> > + goto insane;
> > + }
> > +
> > + return ret;
> > +insane:
> > + pr_warn("SNC insanity: CPU nodes = %d num_l3_caches = %d\n",
> > + (nr_node_ids - mem_only_nodes), num_l3_caches);
> > + return 1;
> > +}
>
> I find it confusing how dramatically this SNC detection code changed without
> any explanations. This detection seems to match the SNC detection code from v16 but
> after v16 you posted a new SNC detection implementation that did SNC detection totally
> differently [1] from v16. Instead of keeping with the "new" detection this implements
> what was in v16. Could you please help me understand what motivated the different
> implementations and why the big differences?

Reinette,

Do you like the detection code in that version? You didn't make any
comments about it.

I switched back to the v16 code because that had survived review before
and I just wanted to make the modifications to add both per-L3 and
per-SNC node monitoring files.

I can pull that into the next iteration if you want.

-Tony
>
> [1] https://lore.kernel.org/lkml/[email protected]/

2024-05-13 18:53:39

by Reinette Chatre

[permalink] [raw]
Subject: Re: [PATCH v17 7/9] x86/resctrl: Add new monitor files for Sub-NUMA cluster (SNC) monitoring

Hi Tony,

On 5/13/2024 10:05 AM, Tony Luck wrote:
> On Fri, May 10, 2024 at 02:24:13PM -0700, Reinette Chatre wrote:
>> Hi Tony,
>
> Hi Reinette,
>
> Thanks for the review. Detailed comments below. But overall I'm
> going to split patch 7 into a bunch of smaller changes, each with
> a better commit message.
>
>> On 5/3/2024 1:33 PM, Tony Luck wrote:
>>
>> (Could you please start the changelog with some context?)
>>
>>> Add a field to the rdt_resource structure to track whether monitoring
>>> resources are tracked by hardware at a different scope (NODE) from
>>> the legacy L3 scope.
>>
>> This seems to describe @mon_scope that was introduced in patch #3?
>
> Not really. Patch #3 made the change so that control an monitor
> functions can have different scope. That's still needed as with SNC
> enabled the underlying data collection is at the node level for
> monitoring, while control stays at the L3 cache scope.
>
> This new field describes the legacy scope of monitoring, so that
> resctrl can provide correctly scoped monitor files for legacy
> applications that aren't aware of SNC. So I'm using this both
> to indicate when SNC is enabled (with mon_scope != mon_display_scope)
> or disabled (when they are the same).

This seems to enforce the idea that these new additions aim to be
generic on the surface but the only goal is to support SNC.

>
>>>
>>> Add a field to the rdt_mon_domain structure to track the L3 cache id
>>> which can be used to find all the domains that need resource counts
>>> summed to provide accurate values in the legacy monitoring files.
>>
>> Why is this field necessary? Can this not be obtained dynamically?
>
> I could compute it each time I need it (when making/removing
> directories, or finding which SNC domains share an L3 domain).
>
> id = get_domain_id_from_scope(cpumask_any(&d->cpu_mask), r->mon_display_scope);
> if (id < 0)
> // error path
>
> But it seemed better to just discover this once at domain creation time.

This may be more clear in the next version?

..

>>> /*
>>> * As per the SDM, when IA32_QM_EVTSEL.EvtID (bits 7:0) is configured
>>> * with a valid event code for supported resource type and the bits
>>> @@ -207,7 +198,7 @@ static int __rmid_read(u32 rmid, enum resctrl_event_id eventid, u64 *val)
>>> * IA32_QM_CTR.Error (bit 63) and IA32_QM_CTR.Unavailable (bit 62)
>>> * are error bits.
>>> */
>>> - wrmsr(MSR_IA32_QM_EVTSEL, eventid, rmid + rmid_offset);
>>> + wrmsr(MSR_IA32_QM_EVTSEL, eventid, rmid);
>>> rdmsrl(MSR_IA32_QM_CTR, msr_val);
>>>
>>> if (msr_val & RMID_VAL_ERROR)
>>> @@ -291,7 +282,7 @@ int resctrl_arch_rmid_read(struct rdt_resource *r, struct rdt_mon_domain *d,
>>>
>>> resctrl_arch_rmid_read_context_check();
>>>
>>> - if (!cpumask_test_cpu(smp_processor_id(), &d->hdr.cpu_mask))
>>> + if (d->display_id != get_cpu_cacheinfo_id(smp_processor_id(), r->mon_display_scope))
>>> return -EINVAL;
>>
>> Does this mean that when SNC is enabled then reading data for an event within a particular
>> monitor domain ("node scope") can read its data from any CPU within the L3 domain
>> ("mon_display_scope") even if that CPU is not associated with the node for which it
>> is reading the data?
>
> Yes.
>
>> If so this really turns many resctrl assumptions and architecture on its head since the
>> resctrl expectation is that only CPUs within a domain's cpumask can be used to interact
>> with the domain. This in turn makes this seemingly general feature actually SNC specific.
>
> This is only an expectation for x86 features using IA32_QM_EVTSEL/IA32_QM_CTR
> MSR method to read counters. ARM doesn't have the "CPU must be in
> domain" restriction (as far as I can tell). Nor does the Intel IO RDT
> (which uses MMIO space for control registers, these can be read/written
> from any CPU).
>
> We do know that those two MSRs can be read from any CPU that shares an
> L3 cache. It would seem to be pointless overhead to force a cross
> processor interrupt to read them from a different CPU just to satisfy
> a "must be in same domain" non-requirement. I'l split this into its
> own patch with suitable description.

I did not suggest that this should be done with multiple IPIs. My comment
was related to this addition that claims to be generic but really just focuses
on support for SNC. Any future addition that may want to build on this would
need to be aware of these expectations, which are not obvious at this time.

..


>>> return 0;
>>> }
>>>
>>> +static u32 get_node_rmid(struct rdt_resource *r, struct rdt_mon_domain *d, u32 rmid)
>>> +{
>>> + int cpu = cpumask_any(&d->hdr.cpu_mask);
>>> +
>>> + return rmid + (cpu_to_node(cpu) % snc_nodes_per_l3_cache) * r->num_rmid;
>>> +}
>>> +
>>> +static int __mon_event_count(u32 closid, u32 rmid, struct rmid_read *rr)
>>> +{
>>> + struct rdt_mon_domain *d;
>>> + struct rmid_read tmp;
>>> + u32 node_rmid;
>>> + int ret = 0;
>>> +
>>> + if (!rr->sumdomains) {
>>> + node_rmid = get_node_rmid(rr->r, rr->d, rmid);
>>> + return ___mon_event_count(closid, node_rmid, rr, &rr->val);
>>> + }
>>> +
>>> + tmp = *rr;
>>> + list_for_each_entry(d, &rr->r->mon_domains, hdr.list) {
>>> + if (d->display_id == rr->d->display_id) {
>>> + tmp.d = d;
>>> + node_rmid = get_node_rmid(rr->r, d, rmid);
>>> + ret = ___mon_event_count(closid, node_rmid, &tmp, &rr->val);
>>
>> If I understand correctly this function is run per IPI on a CPU associated
>> with one of the monitor domains (depends on which one came online first),
>> and then it will read the monitor data of the other domains from the same
>> CPU? This is unexpected since the expectation is that monitor data
>> needs to be read from a CPU associated with the domain it is
>> reading data for.
>
> See earlier note. The counter can be read from any CPU sharing the same
> L3. Adding unnecessary IPI is pointless overhead. But I will add
> comments.

I did not suggest to add extra IPIs, my comment was related to how this
feature wedges itself into resctrl.

>
>> Also, providing tmp as well as rr->val seems unnecessary?
>
> I think I was unsure about modifying the domain field in the struct
> rmid_read in the middle of the call chain. But the original caller
> mon_event_read() doesn't look at rr->domain after the smp_call*()
> function returns. I will drop "tmp".
>
>>> + if (ret)
>>> + break;
>>> + }
>>> + }
>>> +
>>> + return ret;
>>> +}
>>> +
>>> /*
>>> * mbm_bw_count() - Update bw count from values previously read by
>>> * __mon_event_count().
>>> diff --git a/arch/x86/kernel/cpu/resctrl/rdtgroup.c b/arch/x86/kernel/cpu/resctrl/rdtgroup.c
>>> index 0923492a8bd0..a56ae08ca255 100644
>>> --- a/arch/x86/kernel/cpu/resctrl/rdtgroup.c
>>> +++ b/arch/x86/kernel/cpu/resctrl/rdtgroup.c
>>> @@ -3011,57 +3011,118 @@ static int mon_addfile(struct kernfs_node *parent_kn, const char *name,
>>> * and monitor groups with given domain id.
>>> */
>>> static void rmdir_mondata_subdir_allrdtgrp(struct rdt_resource *r,
>>> - unsigned int dom_id)
>>> + struct rdt_mon_domain *d)
>>> {
>>> struct rdtgroup *prgrp, *crgrp;
>>> + struct rdt_mon_domain *dom;
>>> + bool remove_all = true;
>>> + struct kernfs_node *kn;
>>> + char subname[32];
>>> char name[32];
>>>
>>> + sprintf(name, "mon_%s_%02d", r->name, d->display_id);
>>> + if (r->mon_scope != r->mon_display_scope) {
>>> + int count = 0;
>>> +
>>> + list_for_each_entry(dom, &r->mon_domains, hdr.list)
>>> + if (d->display_id == dom->display_id)
>>> + count++;
>>> + if (count > 1) {
>>> + remove_all = false;
>>> + sprintf(subname, "mon_sub_%s_%02d", r->name, d->hdr.id);
>>> + }
>>> + }
>>
>>
>> This seems awkward. I wonder if it may not be simpler to just
>> remove the directory and on completion check if the parent has
>> any subdirectories left and remove the parent if there are no
>> subdirectories remaining. Something possible via reading the inode's
>> i_nlink that is accessible via kernfs_get_inode(). What do you think?
>
> kernfs_get_inode() needs a pointer to the "struct super_block" for the
> filesystem. Resctrl filesystem code doesn't seem to keep track of that
> anywhere. Only mentioned in rdt_kill_sb() where core kernfs code passes
> it in as the argument. When registering/mounting the resctrl filesystem
> there's a "struct fs_context *fc" ... is there a function to get the
> super block from that? Even if there is, I'd need to add a global to
> save a copy of the fc_context.

hmmm ... I expected that struct file or struct dentry may be reachable
from where sb can be obtained but I can only see that now for the
paths that provide struct kernfs_open_file.


..

>
>>> }
>>> +
>>> + return 0;
>>> +}
>>> +
>>> +static int mkdir_mondata_subdir(struct kernfs_node *parent_kn,
>>> + struct rdt_mon_domain *d,
>>> + struct rdt_resource *r, struct rdtgroup *prgrp)
>>> +{
>>> + struct kernfs_node *kn, *ckn;
>>> + char name[32];
>>> + bool do_sum;
>>> + int ret;
>>> +
>>> + do_sum = r->mon_scope != r->mon_display_scope;
>>> + sprintf(name, "mon_%s_%02d", r->name, d->display_id);
>>> + kn = kernfs_find_and_get_ns(parent_kn, name, NULL);
>>> + if (!kn) {
>>> + /* create the directory */
>>> + kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
>>> + if (IS_ERR(kn))
>>> + return PTR_ERR(kn);
>>> +
>>> + ret = rdtgroup_kn_set_ugid(kn);
>>> + if (ret)
>>> + goto out_destroy;
>>> + ret = mon_add_all_files(kn, d, r, prgrp, do_sum);
>>
>> This does not look right. If I understand correctly the private data
>> of these event files will have whichever mon domain came up first as
>> its domain id. That seems completely arbitrary and does not reflect
>> accurate state for this file. Since "do_sum" is essentially a "flag"
>> on how this file can be treated, can its "dom_id" not rather be
>> the "monitor scope domain id"? Could that not help to eliminate
>> that per-domain "display_id"?
>
> You are correct that this should be the "monitor scope domain id" rather
> than the first SNC domain that appears. I'll change to use that. I don't
> think it helps in removing the per-domain display_id.

Wouldn't the file metadata then be the "display_id"?

Reinette

2024-05-13 18:53:52

by Reinette Chatre

[permalink] [raw]
Subject: Re: [PATCH v17 8/9] x86/resctrl: Sub NUMA Cluster detection and enable

Hi Tony,

On 5/13/2024 10:17 AM, Tony Luck wrote:
> On Fri, May 10, 2024 at 02:24:49PM -0700, Reinette Chatre wrote:
>> Hi Tony,
>>
>> On 5/3/2024 1:33 PM, Tony Luck wrote:
>>> There isn't a simple hardware bit that indicates whether a CPU is
>>> running in Sub NUMA Cluster (SNC) mode. Infer the state by comparing
>>> the ratio of NUMA nodes to L3 cache instances.
>>>
>>> When SNC mode is detected, reconfigure the RMID counters by updating
>>> the MSR_RMID_SNC_CONFIG MSR on each socket as CPUs are seen.
>>>
>>> Clearing bit zero of the MSR divides the RMIDs and renumbers the ones
>>> on the second SNC node to start from zero.
>>>
>>> Signed-off-by: Tony Luck <[email protected]>
>>> ---
>>> arch/x86/include/asm/msr-index.h | 1 +
>>> arch/x86/kernel/cpu/resctrl/core.c | 119 +++++++++++++++++++++++++++++
>>> 2 files changed, 120 insertions(+)
>>>
>>> diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-index.h
>>> index e72c2b872957..ce54a1ffe1e5 100644
>>> --- a/arch/x86/include/asm/msr-index.h
>>> +++ b/arch/x86/include/asm/msr-index.h
>>> @@ -1165,6 +1165,7 @@
>>> #define MSR_IA32_QM_CTR 0xc8e
>>> #define MSR_IA32_PQR_ASSOC 0xc8f
>>> #define MSR_IA32_L3_CBM_BASE 0xc90
>>> +#define MSR_RMID_SNC_CONFIG 0xca0
>>> #define MSR_IA32_L2_CBM_BASE 0xd10
>>> #define MSR_IA32_MBA_THRTL_BASE 0xd50
>>>
>>> diff --git a/arch/x86/kernel/cpu/resctrl/core.c b/arch/x86/kernel/cpu/resctrl/core.c
>>> index a949e69308cd..6a1727ea1dfe 100644
>>> --- a/arch/x86/kernel/cpu/resctrl/core.c
>>> +++ b/arch/x86/kernel/cpu/resctrl/core.c
>>> @@ -21,7 +21,9 @@
>>> #include <linux/err.h>
>>> #include <linux/cacheinfo.h>
>>> #include <linux/cpuhotplug.h>
>>> +#include <linux/mod_devicetable.h>
>>>
>>> +#include <asm/cpu_device_id.h>
>>> #include <asm/intel-family.h>
>>> #include <asm/resctrl.h>
>>> #include "internal.h"
>>> @@ -746,11 +748,42 @@ static void clear_closid_rmid(int cpu)
>>> RESCTRL_RESERVED_CLOSID);
>>> }
>>>
>>> +/*
>>> + * The power-on reset value of MSR_RMID_SNC_CONFIG is 0x1
>>> + * which indicates that RMIDs are configured in legacy mode.
>>> + * This mode is incompatible with Linux resctrl semantics
>>> + * as RMIDs are partitioned between SNC nodes, which requires
>>> + * a user to know which RMID is allocated to a task.
>>> + * Clearing bit 0 reconfigures the RMID counters for use
>>> + * in Sub NUMA Cluster mode. This mode is better for Linux.
>>> + * The RMID space is divided between all SNC nodes with the
>>> + * RMIDs renumbered to start from zero in each node when
>>> + * couning operations from tasks. Code to read the counters
>>> + * must adjust RMID counter numbers based on SNC node. See
>>> + * __rmid_read() for code that does this.
>>> + */
>>> +static void snc_remap_rmids(int cpu)
>>> +{
>>> + u64 val;
>>> +
>>> + /* Only need to enable once per package. */
>>> + if (cpumask_first(topology_core_cpumask(cpu)) != cpu)
>>> + return;
>>> +
>>> + rdmsrl(MSR_RMID_SNC_CONFIG, val);
>>> + val &= ~BIT_ULL(0);
>>> + wrmsrl(MSR_RMID_SNC_CONFIG, val);
>>> +}
>>> +
>>> static int resctrl_arch_online_cpu(unsigned int cpu)
>>> {
>>> struct rdt_resource *r;
>>>
>>> mutex_lock(&domain_list_lock);
>>> +
>>> + if (snc_nodes_per_l3_cache > 1)
>>> + snc_remap_rmids(cpu);
>>> +
>>> for_each_capable_rdt_resource(r)
>>> domain_add_cpu(cpu, r);
>>> mutex_unlock(&domain_list_lock);
>>> @@ -990,11 +1023,97 @@ static __init bool get_rdt_resources(void)
>>> return (rdt_mon_capable || rdt_alloc_capable);
>>> }
>>>
>>> +/* CPU models that support MSR_RMID_SNC_CONFIG */
>>> +static const struct x86_cpu_id snc_cpu_ids[] __initconst = {
>>> + X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_X, 0),
>>> + X86_MATCH_INTEL_FAM6_MODEL(SAPPHIRERAPIDS_X, 0),
>>> + X86_MATCH_INTEL_FAM6_MODEL(EMERALDRAPIDS_X, 0),
>>> + X86_MATCH_INTEL_FAM6_MODEL(GRANITERAPIDS_X, 0),
>>> + X86_MATCH_INTEL_FAM6_MODEL(ATOM_CRESTMONT_X, 0),
>>> + {}
>>> +};
>>> +
>>> +/*
>>> + * There isn't a simple hardware bit that indicates whether a CPU is running
>>> + * in Sub NUMA Cluster (SNC) mode. Infer the state by comparing the
>>> + * ratio of NUMA nodes to L3 cache instances.
>>> + * It is not possible to accurately determine SNC state if the system is
>>> + * booted with a maxcpus=N parameter. That distorts the ratio of SNC nodes
>>> + * to L3 caches. It will be OK if system is booted with hyperthreading
>>> + * disabled (since this doesn't affect the ratio).
>>> + */
>>> +static __init int snc_get_config(void)
>>> +{
>>> + unsigned long *node_caches;
>>> + int mem_only_nodes = 0;
>>> + int cpu, node, ret;
>>> + int num_l3_caches;
>>> + int cache_id;
>>> +
>>> + if (!x86_match_cpu(snc_cpu_ids))
>>> + return 1;
>>> +
>>> + node_caches = bitmap_zalloc(num_possible_cpus(), GFP_KERNEL);
>>> + if (!node_caches)
>>> + return 1;
>>> +
>>> + cpus_read_lock();
>>> +
>>> + if (num_online_cpus() != num_present_cpus())
>>> + pr_warn("Some CPUs offline, SNC detection may be incorrect\n");
>>> +
>>> + for_each_node(node) {
>>> + cpu = cpumask_first(cpumask_of_node(node));
>>> + if (cpu < nr_cpu_ids) {
>>> + cache_id = get_cpu_cacheinfo_id(cpu, 3);
>>> + if (cache_id != -1)
>>> + set_bit(cache_id, node_caches);
>>> + } else {
>>> + mem_only_nodes++;
>>> + }
>>> + }
>>> + cpus_read_unlock();
>>> +
>>> + num_l3_caches = bitmap_weight(node_caches, num_possible_cpus());
>>> + kfree(node_caches);
>>> +
>>> + if (!num_l3_caches)
>>> + goto insane;
>>> +
>>> + /* sanity check #1: Number of CPU nodes must be multiple of num_l3_caches */
>>> + if ((nr_node_ids - mem_only_nodes) % num_l3_caches)
>>> + goto insane;
>>> +
>>> + ret = (nr_node_ids - mem_only_nodes) / num_l3_caches;
>>> +
>>> + /* sanity check #2: Only valid results are 1, 2, 3, 4 */
>>> + switch (ret) {
>>> + case 1:
>>> + break;
>>> + case 2:
>>> + case 3:
>>> + case 4:
>>> + pr_info("Sub-NUMA cluster detected with %d nodes per L3 cache\n", ret);
>>> + rdt_resources_all[RDT_RESOURCE_L3].r_resctrl.mon_scope = RESCTRL_NODE;
>>> + break;
>>> + default:
>>> + goto insane;
>>> + }
>>> +
>>> + return ret;
>>> +insane:
>>> + pr_warn("SNC insanity: CPU nodes = %d num_l3_caches = %d\n",
>>> + (nr_node_ids - mem_only_nodes), num_l3_caches);
>>> + return 1;
>>> +}
>>
>> I find it confusing how dramatically this SNC detection code changed without
>> any explanations. This detection seems to match the SNC detection code from v16 but
>> after v16 you posted a new SNC detection implementation that did SNC detection totally
>> differently [1] from v16. Instead of keeping with the "new" detection this implements
>> what was in v16. Could you please help me understand what motivated the different
>> implementations and why the big differences?
>
> Reinette,
>
> Do you like the detection code in that version? You didn't make any
> comments about it.

It was a drop-in replacement for a portion that was not relevant to the
architecture discussion that I focused on ... hence my surprise that it
just came and went without any comment.

> I switched back to the v16 code because that had survived review before
> and I just wanted to make the modifications to add both per-L3 and
> per-SNC node monitoring files.
>
> I can pull that into the next iteration if you want.

It is not clear to me why you switched back and forth between the detection
algorithms. I expect big changes to be accompanied with explanation of what changed,
why one is better than the other, or if they are considered "similar", what
are the pros/cons. Am I missing something so obvious that causes you to think
the work does not need the explanation I asked your help with?

Reinette

2024-05-14 00:22:37

by Luck, Tony

[permalink] [raw]
Subject: Re: [PATCH v17 7/9] x86/resctrl: Add new monitor files for Sub-NUMA cluster (SNC) monitoring

On Mon, May 13, 2024 at 11:53:17AM -0700, Reinette Chatre wrote:
> Hi Tony,
>
> On 5/13/2024 10:05 AM, Tony Luck wrote:
> > On Fri, May 10, 2024 at 02:24:13PM -0700, Reinette Chatre wrote:
> >> Hi Tony,
> >
> > Hi Reinette,
> >
> > Thanks for the review. Detailed comments below. But overall I'm
> > going to split patch 7 into a bunch of smaller changes, each with
> > a better commit message.
> >
> >> On 5/3/2024 1:33 PM, Tony Luck wrote:
> >>
> >> (Could you please start the changelog with some context?)
> >>
> >>> Add a field to the rdt_resource structure to track whether monitoring
> >>> resources are tracked by hardware at a different scope (NODE) from
> >>> the legacy L3 scope.
> >>
> >> This seems to describe @mon_scope that was introduced in patch #3?
> >
> > Not really. Patch #3 made the change so that control an monitor
> > functions can have different scope. That's still needed as with SNC
> > enabled the underlying data collection is at the node level for
> > monitoring, while control stays at the L3 cache scope.
> >
> > This new field describes the legacy scope of monitoring, so that
> > resctrl can provide correctly scoped monitor files for legacy
> > applications that aren't aware of SNC. So I'm using this both
> > to indicate when SNC is enabled (with mon_scope != mon_display_scope)
> > or disabled (when they are the same).
>
> This seems to enforce the idea that these new additions aim to be
> generic on the surface but the only goal is to support SNC.

If you have some more ideas on how to make this more generic and
less SNC specific I'm all ears.

> >
> >>>
> >>> Add a field to the rdt_mon_domain structure to track the L3 cache id
> >>> which can be used to find all the domains that need resource counts
> >>> summed to provide accurate values in the legacy monitoring files.
> >>
> >> Why is this field necessary? Can this not be obtained dynamically?
> >
> > I could compute it each time I need it (when making/removing
> > directories, or finding which SNC domains share an L3 domain).
> >
> > id = get_domain_id_from_scope(cpumask_any(&d->cpu_mask), r->mon_display_scope);
> > if (id < 0)
> > // error path
> >
> > But it seemed better to just discover this once at domain creation time.
>
> This may be more clear in the next version?

My goal is to be more clear next version.

> ...
>
> >>> /*
> >>> * As per the SDM, when IA32_QM_EVTSEL.EvtID (bits 7:0) is configured
> >>> * with a valid event code for supported resource type and the bits
> >>> @@ -207,7 +198,7 @@ static int __rmid_read(u32 rmid, enum resctrl_event_id eventid, u64 *val)
> >>> * IA32_QM_CTR.Error (bit 63) and IA32_QM_CTR.Unavailable (bit 62)
> >>> * are error bits.
> >>> */
> >>> - wrmsr(MSR_IA32_QM_EVTSEL, eventid, rmid + rmid_offset);
> >>> + wrmsr(MSR_IA32_QM_EVTSEL, eventid, rmid);
> >>> rdmsrl(MSR_IA32_QM_CTR, msr_val);
> >>>
> >>> if (msr_val & RMID_VAL_ERROR)
> >>> @@ -291,7 +282,7 @@ int resctrl_arch_rmid_read(struct rdt_resource *r, struct rdt_mon_domain *d,
> >>>
> >>> resctrl_arch_rmid_read_context_check();
> >>>
> >>> - if (!cpumask_test_cpu(smp_processor_id(), &d->hdr.cpu_mask))
> >>> + if (d->display_id != get_cpu_cacheinfo_id(smp_processor_id(), r->mon_display_scope))
> >>> return -EINVAL;
> >>
> >> Does this mean that when SNC is enabled then reading data for an event within a particular
> >> monitor domain ("node scope") can read its data from any CPU within the L3 domain
> >> ("mon_display_scope") even if that CPU is not associated with the node for which it
> >> is reading the data?
> >
> > Yes.
> >
> >> If so this really turns many resctrl assumptions and architecture on its head since the
> >> resctrl expectation is that only CPUs within a domain's cpumask can be used to interact
> >> with the domain. This in turn makes this seemingly general feature actually SNC specific.
> >
> > This is only an expectation for x86 features using IA32_QM_EVTSEL/IA32_QM_CTR
> > MSR method to read counters. ARM doesn't have the "CPU must be in
> > domain" restriction (as far as I can tell). Nor does the Intel IO RDT
> > (which uses MMIO space for control registers, these can be read/written
> > from any CPU).
> >
> > We do know that those two MSRs can be read from any CPU that shares an
> > L3 cache. It would seem to be pointless overhead to force a cross
> > processor interrupt to read them from a different CPU just to satisfy
> > a "must be in same domain" non-requirement. I'l split this into its
> > own patch with suitable description.
>
> I did not suggest that this should be done with multiple IPIs. My comment
> was related to this addition that claims to be generic but really just focuses
> on support for SNC. Any future addition that may want to build on this would
> need to be aware of these expectations, which are not obvious at this time.

I can add some more comments to make this more obvious.

> ...
>
>
> >>> return 0;
> >>> }
> >>>
> >>> +static u32 get_node_rmid(struct rdt_resource *r, struct rdt_mon_domain *d, u32 rmid)
> >>> +{
> >>> + int cpu = cpumask_any(&d->hdr.cpu_mask);
> >>> +
> >>> + return rmid + (cpu_to_node(cpu) % snc_nodes_per_l3_cache) * r->num_rmid;
> >>> +}
> >>> +
> >>> +static int __mon_event_count(u32 closid, u32 rmid, struct rmid_read *rr)
> >>> +{
> >>> + struct rdt_mon_domain *d;
> >>> + struct rmid_read tmp;
> >>> + u32 node_rmid;
> >>> + int ret = 0;
> >>> +
> >>> + if (!rr->sumdomains) {
> >>> + node_rmid = get_node_rmid(rr->r, rr->d, rmid);
> >>> + return ___mon_event_count(closid, node_rmid, rr, &rr->val);
> >>> + }
> >>> +
> >>> + tmp = *rr;
> >>> + list_for_each_entry(d, &rr->r->mon_domains, hdr.list) {
> >>> + if (d->display_id == rr->d->display_id) {
> >>> + tmp.d = d;
> >>> + node_rmid = get_node_rmid(rr->r, d, rmid);
> >>> + ret = ___mon_event_count(closid, node_rmid, &tmp, &rr->val);
> >>
> >> If I understand correctly this function is run per IPI on a CPU associated
> >> with one of the monitor domains (depends on which one came online first),
> >> and then it will read the monitor data of the other domains from the same
> >> CPU? This is unexpected since the expectation is that monitor data
> >> needs to be read from a CPU associated with the domain it is
> >> reading data for.
> >
> > See earlier note. The counter can be read from any CPU sharing the same
> > L3. Adding unnecessary IPI is pointless overhead. But I will add
> > comments.
>
> I did not suggest to add extra IPIs, my comment was related to how this
> feature wedges itself into resctrl.

Sorry for my misunderstanding.

> >
> >> Also, providing tmp as well as rr->val seems unnecessary?
> >
> > I think I was unsure about modifying the domain field in the struct
> > rmid_read in the middle of the call chain. But the original caller
> > mon_event_read() doesn't look at rr->domain after the smp_call*()
> > function returns. I will drop "tmp".
> >
> >>> + if (ret)
> >>> + break;
> >>> + }
> >>> + }
> >>> +
> >>> + return ret;
> >>> +}
> >>> +
> >>> /*
> >>> * mbm_bw_count() - Update bw count from values previously read by
> >>> * __mon_event_count().
> >>> diff --git a/arch/x86/kernel/cpu/resctrl/rdtgroup.c b/arch/x86/kernel/cpu/resctrl/rdtgroup.c
> >>> index 0923492a8bd0..a56ae08ca255 100644
> >>> --- a/arch/x86/kernel/cpu/resctrl/rdtgroup.c
> >>> +++ b/arch/x86/kernel/cpu/resctrl/rdtgroup.c
> >>> @@ -3011,57 +3011,118 @@ static int mon_addfile(struct kernfs_node *parent_kn, const char *name,
> >>> * and monitor groups with given domain id.
> >>> */
> >>> static void rmdir_mondata_subdir_allrdtgrp(struct rdt_resource *r,
> >>> - unsigned int dom_id)
> >>> + struct rdt_mon_domain *d)
> >>> {
> >>> struct rdtgroup *prgrp, *crgrp;
> >>> + struct rdt_mon_domain *dom;
> >>> + bool remove_all = true;
> >>> + struct kernfs_node *kn;
> >>> + char subname[32];
> >>> char name[32];
> >>>
> >>> + sprintf(name, "mon_%s_%02d", r->name, d->display_id);
> >>> + if (r->mon_scope != r->mon_display_scope) {
> >>> + int count = 0;
> >>> +
> >>> + list_for_each_entry(dom, &r->mon_domains, hdr.list)
> >>> + if (d->display_id == dom->display_id)
> >>> + count++;
> >>> + if (count > 1) {
> >>> + remove_all = false;
> >>> + sprintf(subname, "mon_sub_%s_%02d", r->name, d->hdr.id);
> >>> + }
> >>> + }
> >>
> >>
> >> This seems awkward. I wonder if it may not be simpler to just
> >> remove the directory and on completion check if the parent has
> >> any subdirectories left and remove the parent if there are no
> >> subdirectories remaining. Something possible via reading the inode's
> >> i_nlink that is accessible via kernfs_get_inode(). What do you think?
> >
> > kernfs_get_inode() needs a pointer to the "struct super_block" for the
> > filesystem. Resctrl filesystem code doesn't seem to keep track of that
> > anywhere. Only mentioned in rdt_kill_sb() where core kernfs code passes
> > it in as the argument. When registering/mounting the resctrl filesystem
> > there's a "struct fs_context *fc" ... is there a function to get the
> > super block from that? Even if there is, I'd need to add a global to
> > save a copy of the fc_context.
>
> hmmm ... I expected that struct file or struct dentry may be reachable
> from where sb can be obtained but I can only see that now for the
> paths that provide struct kernfs_open_file.

I'm going to keep this the same then. The "rmdir" call path doesn't have
any open files to plumb down to this function.

>
> ...
>
> >
> >>> }
> >>> +
> >>> + return 0;
> >>> +}
> >>> +
> >>> +static int mkdir_mondata_subdir(struct kernfs_node *parent_kn,
> >>> + struct rdt_mon_domain *d,
> >>> + struct rdt_resource *r, struct rdtgroup *prgrp)
> >>> +{
> >>> + struct kernfs_node *kn, *ckn;
> >>> + char name[32];
> >>> + bool do_sum;
> >>> + int ret;
> >>> +
> >>> + do_sum = r->mon_scope != r->mon_display_scope;
> >>> + sprintf(name, "mon_%s_%02d", r->name, d->display_id);
> >>> + kn = kernfs_find_and_get_ns(parent_kn, name, NULL);
> >>> + if (!kn) {
> >>> + /* create the directory */
> >>> + kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
> >>> + if (IS_ERR(kn))
> >>> + return PTR_ERR(kn);
> >>> +
> >>> + ret = rdtgroup_kn_set_ugid(kn);
> >>> + if (ret)
> >>> + goto out_destroy;
> >>> + ret = mon_add_all_files(kn, d, r, prgrp, do_sum);
> >>
> >> This does not look right. If I understand correctly the private data
> >> of these event files will have whichever mon domain came up first as
> >> its domain id. That seems completely arbitrary and does not reflect
> >> accurate state for this file. Since "do_sum" is essentially a "flag"
> >> on how this file can be treated, can its "dom_id" not rather be
> >> the "monitor scope domain id"? Could that not help to eliminate
> >> that per-domain "display_id"?
> >
> > You are correct that this should be the "monitor scope domain id" rather
> > than the first SNC domain that appears. I'll change to use that. I don't
> > think it helps in removing the per-domain display_id.
>
> Wouldn't the file metadata then be the "display_id"?

Yes. The metadata is the display_id for files that need to sum across
SNC nodes, but the domain id for ones where no summation is needed.

> Reinette

-Tony

2024-05-14 00:28:48

by Luck, Tony

[permalink] [raw]
Subject: Re: [PATCH v17 8/9] x86/resctrl: Sub NUMA Cluster detection and enable

On Mon, May 13, 2024 at 11:53:26AM -0700, Reinette Chatre wrote:
> Hi Tony,
>
> On 5/13/2024 10:17 AM, Tony Luck wrote:
> > On Fri, May 10, 2024 at 02:24:49PM -0700, Reinette Chatre wrote:
> >> Hi Tony,
> >>
> >> On 5/3/2024 1:33 PM, Tony Luck wrote:
> >>> There isn't a simple hardware bit that indicates whether a CPU is
> >>> running in Sub NUMA Cluster (SNC) mode. Infer the state by comparing
> >>> the ratio of NUMA nodes to L3 cache instances.
> >>>
> >>> When SNC mode is detected, reconfigure the RMID counters by updating
> >>> the MSR_RMID_SNC_CONFIG MSR on each socket as CPUs are seen.
> >>>
> >>> Clearing bit zero of the MSR divides the RMIDs and renumbers the ones
> >>> on the second SNC node to start from zero.
> >>>
> >>> Signed-off-by: Tony Luck <[email protected]>
> >>> ---
> >>> arch/x86/include/asm/msr-index.h | 1 +
> >>> arch/x86/kernel/cpu/resctrl/core.c | 119 +++++++++++++++++++++++++++++
> >>> 2 files changed, 120 insertions(+)
> >>>
> >>> diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-index.h
> >>> index e72c2b872957..ce54a1ffe1e5 100644
> >>> --- a/arch/x86/include/asm/msr-index.h
> >>> +++ b/arch/x86/include/asm/msr-index.h
> >>> @@ -1165,6 +1165,7 @@
> >>> #define MSR_IA32_QM_CTR 0xc8e
> >>> #define MSR_IA32_PQR_ASSOC 0xc8f
> >>> #define MSR_IA32_L3_CBM_BASE 0xc90
> >>> +#define MSR_RMID_SNC_CONFIG 0xca0
> >>> #define MSR_IA32_L2_CBM_BASE 0xd10
> >>> #define MSR_IA32_MBA_THRTL_BASE 0xd50
> >>>
> >>> diff --git a/arch/x86/kernel/cpu/resctrl/core.c b/arch/x86/kernel/cpu/resctrl/core.c
> >>> index a949e69308cd..6a1727ea1dfe 100644
> >>> --- a/arch/x86/kernel/cpu/resctrl/core.c
> >>> +++ b/arch/x86/kernel/cpu/resctrl/core.c
> >>> @@ -21,7 +21,9 @@
> >>> #include <linux/err.h>
> >>> #include <linux/cacheinfo.h>
> >>> #include <linux/cpuhotplug.h>
> >>> +#include <linux/mod_devicetable.h>
> >>>
> >>> +#include <asm/cpu_device_id.h>
> >>> #include <asm/intel-family.h>
> >>> #include <asm/resctrl.h>
> >>> #include "internal.h"
> >>> @@ -746,11 +748,42 @@ static void clear_closid_rmid(int cpu)
> >>> RESCTRL_RESERVED_CLOSID);
> >>> }
> >>>
> >>> +/*
> >>> + * The power-on reset value of MSR_RMID_SNC_CONFIG is 0x1
> >>> + * which indicates that RMIDs are configured in legacy mode.
> >>> + * This mode is incompatible with Linux resctrl semantics
> >>> + * as RMIDs are partitioned between SNC nodes, which requires
> >>> + * a user to know which RMID is allocated to a task.
> >>> + * Clearing bit 0 reconfigures the RMID counters for use
> >>> + * in Sub NUMA Cluster mode. This mode is better for Linux.
> >>> + * The RMID space is divided between all SNC nodes with the
> >>> + * RMIDs renumbered to start from zero in each node when
> >>> + * couning operations from tasks. Code to read the counters
> >>> + * must adjust RMID counter numbers based on SNC node. See
> >>> + * __rmid_read() for code that does this.
> >>> + */
> >>> +static void snc_remap_rmids(int cpu)
> >>> +{
> >>> + u64 val;
> >>> +
> >>> + /* Only need to enable once per package. */
> >>> + if (cpumask_first(topology_core_cpumask(cpu)) != cpu)
> >>> + return;
> >>> +
> >>> + rdmsrl(MSR_RMID_SNC_CONFIG, val);
> >>> + val &= ~BIT_ULL(0);
> >>> + wrmsrl(MSR_RMID_SNC_CONFIG, val);
> >>> +}
> >>> +
> >>> static int resctrl_arch_online_cpu(unsigned int cpu)
> >>> {
> >>> struct rdt_resource *r;
> >>>
> >>> mutex_lock(&domain_list_lock);
> >>> +
> >>> + if (snc_nodes_per_l3_cache > 1)
> >>> + snc_remap_rmids(cpu);
> >>> +
> >>> for_each_capable_rdt_resource(r)
> >>> domain_add_cpu(cpu, r);
> >>> mutex_unlock(&domain_list_lock);
> >>> @@ -990,11 +1023,97 @@ static __init bool get_rdt_resources(void)
> >>> return (rdt_mon_capable || rdt_alloc_capable);
> >>> }
> >>>
> >>> +/* CPU models that support MSR_RMID_SNC_CONFIG */
> >>> +static const struct x86_cpu_id snc_cpu_ids[] __initconst = {
> >>> + X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_X, 0),
> >>> + X86_MATCH_INTEL_FAM6_MODEL(SAPPHIRERAPIDS_X, 0),
> >>> + X86_MATCH_INTEL_FAM6_MODEL(EMERALDRAPIDS_X, 0),
> >>> + X86_MATCH_INTEL_FAM6_MODEL(GRANITERAPIDS_X, 0),
> >>> + X86_MATCH_INTEL_FAM6_MODEL(ATOM_CRESTMONT_X, 0),
> >>> + {}
> >>> +};
> >>> +
> >>> +/*
> >>> + * There isn't a simple hardware bit that indicates whether a CPU is running
> >>> + * in Sub NUMA Cluster (SNC) mode. Infer the state by comparing the
> >>> + * ratio of NUMA nodes to L3 cache instances.
> >>> + * It is not possible to accurately determine SNC state if the system is
> >>> + * booted with a maxcpus=N parameter. That distorts the ratio of SNC nodes
> >>> + * to L3 caches. It will be OK if system is booted with hyperthreading
> >>> + * disabled (since this doesn't affect the ratio).
> >>> + */
> >>> +static __init int snc_get_config(void)
> >>> +{
> >>> + unsigned long *node_caches;
> >>> + int mem_only_nodes = 0;
> >>> + int cpu, node, ret;
> >>> + int num_l3_caches;
> >>> + int cache_id;
> >>> +
> >>> + if (!x86_match_cpu(snc_cpu_ids))
> >>> + return 1;
> >>> +
> >>> + node_caches = bitmap_zalloc(num_possible_cpus(), GFP_KERNEL);
> >>> + if (!node_caches)
> >>> + return 1;
> >>> +
> >>> + cpus_read_lock();
> >>> +
> >>> + if (num_online_cpus() != num_present_cpus())
> >>> + pr_warn("Some CPUs offline, SNC detection may be incorrect\n");
> >>> +
> >>> + for_each_node(node) {
> >>> + cpu = cpumask_first(cpumask_of_node(node));
> >>> + if (cpu < nr_cpu_ids) {
> >>> + cache_id = get_cpu_cacheinfo_id(cpu, 3);
> >>> + if (cache_id != -1)
> >>> + set_bit(cache_id, node_caches);
> >>> + } else {
> >>> + mem_only_nodes++;
> >>> + }
> >>> + }
> >>> + cpus_read_unlock();
> >>> +
> >>> + num_l3_caches = bitmap_weight(node_caches, num_possible_cpus());
> >>> + kfree(node_caches);
> >>> +
> >>> + if (!num_l3_caches)
> >>> + goto insane;
> >>> +
> >>> + /* sanity check #1: Number of CPU nodes must be multiple of num_l3_caches */
> >>> + if ((nr_node_ids - mem_only_nodes) % num_l3_caches)
> >>> + goto insane;
> >>> +
> >>> + ret = (nr_node_ids - mem_only_nodes) / num_l3_caches;
> >>> +
> >>> + /* sanity check #2: Only valid results are 1, 2, 3, 4 */
> >>> + switch (ret) {
> >>> + case 1:
> >>> + break;
> >>> + case 2:
> >>> + case 3:
> >>> + case 4:
> >>> + pr_info("Sub-NUMA cluster detected with %d nodes per L3 cache\n", ret);
> >>> + rdt_resources_all[RDT_RESOURCE_L3].r_resctrl.mon_scope = RESCTRL_NODE;
> >>> + break;
> >>> + default:
> >>> + goto insane;
> >>> + }
> >>> +
> >>> + return ret;
> >>> +insane:
> >>> + pr_warn("SNC insanity: CPU nodes = %d num_l3_caches = %d\n",
> >>> + (nr_node_ids - mem_only_nodes), num_l3_caches);
> >>> + return 1;
> >>> +}
> >>
> >> I find it confusing how dramatically this SNC detection code changed without
> >> any explanations. This detection seems to match the SNC detection code from v16 but
> >> after v16 you posted a new SNC detection implementation that did SNC detection totally
> >> differently [1] from v16. Instead of keeping with the "new" detection this implements
> >> what was in v16. Could you please help me understand what motivated the different
> >> implementations and why the big differences?
> >
> > Reinette,
> >
> > Do you like the detection code in that version? You didn't make any
> > comments about it.
>
> It was a drop-in replacement for a portion that was not relevant to the
> architecture discussion that I focused on ... hence my surprise that it
> just came and went without any comment.

So it will be back again when I post v18 as it is somewhat simpler
(doesn't rely on allocating a bitmap to count L3 cache instances).

I'll update comments in that patch, in the code, and in the change
log in the cover letter.

> > I switched back to the v16 code because that had survived review before
> > and I just wanted to make the modifications to add both per-L3 and
> > per-SNC node monitoring files.
> >
> > I can pull that into the next iteration if you want.
>
> It is not clear to me why you switched back and forth between the detection
> algorithms. I expect big changes to be accompanied with explanation of what changed,
> why one is better than the other, or if they are considered "similar", what
> are the pros/cons. Am I missing something so obvious that causes you to think
> the work does not need the explanation I asked your help with?

The change deserved some comments when it suddenly appeared. One of the
many issues with my detour from the progression. It disappeared because
I reverted to the previously reviewed version.

> Reinette

-Tony

2024-05-14 15:02:42

by Maciej Wieczor-Retman

[permalink] [raw]
Subject: Re: [PATCH v17 0/9] Add support for Sub-NUMA cluster (SNC) systems

Hi,
thanks for adding the 7th patch, I tested the series on a Sapphire Rapids with
resctrl selftests and the summation works well on SNC-2.

Now I can also drop a few patches from my series on SNC in resctrl selftests and
only keep the part about discovering kernel support.

On 2024-05-03 at 13:33:16 -0700, Tony Luck wrote:
>Note: Jump straight to patch 7 for the new stuff. Just minor tweaks in
>other patches.
>
>This series based on top of TIP x86/cache branch:
> 931be446c6cb ("x86/resctrl: Add tracepoint for llc_occupancy tracking")
>
>The Sub-NUMA cluster feature on some Intel processors partitions the CPUs
>that share an L3 cache into two or more sets. This plays havoc with the
>Resource Director Technology (RDT) monitoring features. Prior to this
>patch Intel has advised that SNC and RDT are incompatible.
>
>Some of these CPUs support an MSR that can partition the RMID counters
>in the same way. This allows monitoring features to be used. Legacy
>monitoring files provide the sum of counters from each SNC node for
>backwards compatibility. Additional files per SNC node provide details
>per node.
>
>Cache and memory bandwidth allocation features continue to operate at
>the scope of the L3 cache.
>
>Signed-off-by: Tony Luck <[email protected]>
>
>---
>Changes since v16: https://lore.kernel.org/all/[email protected]/
>
>Patch 1: Reinette pointed out that rdt_find_domain() no longer returns ERR_PTR()
>but one of the callers was still checking return with IS_ERR().
>
>Patch 2: Tip tree added a tracing patch. That needed s/d->id/d->hdr.id/
>
>Patch 3: Reinette: Keep the "RCU" in the kerneldoc description of the
>domain list fields after the split into separate ctrl/mon lists.
>
>Patch 4: No change
>
>Patch 5: No change
>
>Patch 6: Drop the change that divided output of the resctrl "size" file
>by the number of SNC domains per L3 cache. Now that this series
>preserves the contents of the legacy llc_occupancy files this isn't
>useful.
>
>Patch 7: NEW in this series. Add per-SNC domain monitor files while
>making the original files sum across SNC nodes.
>
>Patch 8: (formerly 7) No change
>
>Patch 9: (formerly 8) Add documentation for new per-SNC directories and files
>
>Tony Luck (9):
> x86/resctrl: Prepare for new domain scope
> x86/resctrl: Prepare to split rdt_domain structure
> x86/resctrl: Prepare for different scope for control/monitor
> operations
> x86/resctrl: Split the rdt_domain and rdt_hw_domain structures
> x86/resctrl: Add node-scope to the options for feature scope
> x86/resctrl: Introduce snc_nodes_per_l3_cache
> x86/resctrl: Add new monitor files for Sub-NUMA cluster (SNC)
> monitoring
> x86/resctrl: Sub NUMA Cluster detection and enable
> x86/resctrl: Update documentation with Sub-NUMA cluster changes
>
> Documentation/arch/x86/resctrl.rst | 17 +
> include/linux/resctrl.h | 89 +++--
> arch/x86/include/asm/msr-index.h | 1 +
> arch/x86/kernel/cpu/resctrl/internal.h | 72 ++--
> arch/x86/kernel/cpu/resctrl/core.c | 430 ++++++++++++++++++----
> arch/x86/kernel/cpu/resctrl/ctrlmondata.c | 57 +--
> arch/x86/kernel/cpu/resctrl/monitor.c | 98 +++--
> arch/x86/kernel/cpu/resctrl/pseudo_lock.c | 26 +-
> arch/x86/kernel/cpu/resctrl/rdtgroup.c | 263 ++++++++-----
> 9 files changed, 759 insertions(+), 294 deletions(-)
>
>
>base-commit: 931be446c6cbc15691dd499957e961f4e1d56afb
>--
>2.44.0
>

Tested-by: Maciej Wieczor-Retman <[email protected]>

--
Kind regards
Maciej Wiecz?r-Retman

2024-05-14 15:08:39

by Reinette Chatre

[permalink] [raw]
Subject: Re: [PATCH v17 7/9] x86/resctrl: Add new monitor files for Sub-NUMA cluster (SNC) monitoring

Hi Tony,

On 5/13/2024 5:21 PM, Tony Luck wrote:
> On Mon, May 13, 2024 at 11:53:17AM -0700, Reinette Chatre wrote:
>> On 5/13/2024 10:05 AM, Tony Luck wrote:
>>> On Fri, May 10, 2024 at 02:24:13PM -0700, Reinette Chatre wrote:
>>> Thanks for the review. Detailed comments below. But overall I'm
>>> going to split patch 7 into a bunch of smaller changes, each with
>>> a better commit message.
>>>
>>>> On 5/3/2024 1:33 PM, Tony Luck wrote:
>>>>
>>>> (Could you please start the changelog with some context?)
>>>>
>>>>> Add a field to the rdt_resource structure to track whether monitoring
>>>>> resources are tracked by hardware at a different scope (NODE) from
>>>>> the legacy L3 scope.
>>>>
>>>> This seems to describe @mon_scope that was introduced in patch #3?
>>>
>>> Not really. Patch #3 made the change so that control an monitor
>>> functions can have different scope. That's still needed as with SNC
>>> enabled the underlying data collection is at the node level for
>>> monitoring, while control stays at the L3 cache scope.
>>>
>>> This new field describes the legacy scope of monitoring, so that
>>> resctrl can provide correctly scoped monitor files for legacy
>>> applications that aren't aware of SNC. So I'm using this both
>>> to indicate when SNC is enabled (with mon_scope != mon_display_scope)
>>> or disabled (when they are the same).
>>
>> This seems to enforce the idea that these new additions aim to be
>> generic on the surface but the only goal is to support SNC.
>
> If you have some more ideas on how to make this more generic and
> less SNC specific I'm all ears.

It may not end up being totally generic. It should not pretend to be
when it is not. It makes the flows difficult to follow when there are
these unexpected checks/quirks in what claims to be core code.

>>>>> }
>>>>> +
>>>>> + return 0;
>>>>> +}
>>>>> +
>>>>> +static int mkdir_mondata_subdir(struct kernfs_node *parent_kn,
>>>>> + struct rdt_mon_domain *d,
>>>>> + struct rdt_resource *r, struct rdtgroup *prgrp)
>>>>> +{
>>>>> + struct kernfs_node *kn, *ckn;
>>>>> + char name[32];
>>>>> + bool do_sum;
>>>>> + int ret;
>>>>> +
>>>>> + do_sum = r->mon_scope != r->mon_display_scope;
>>>>> + sprintf(name, "mon_%s_%02d", r->name, d->display_id);
>>>>> + kn = kernfs_find_and_get_ns(parent_kn, name, NULL);
>>>>> + if (!kn) {
>>>>> + /* create the directory */
>>>>> + kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
>>>>> + if (IS_ERR(kn))
>>>>> + return PTR_ERR(kn);
>>>>> +
>>>>> + ret = rdtgroup_kn_set_ugid(kn);
>>>>> + if (ret)
>>>>> + goto out_destroy;
>>>>> + ret = mon_add_all_files(kn, d, r, prgrp, do_sum);
>>>>
>>>> This does not look right. If I understand correctly the private data
>>>> of these event files will have whichever mon domain came up first as
>>>> its domain id. That seems completely arbitrary and does not reflect
>>>> accurate state for this file. Since "do_sum" is essentially a "flag"
>>>> on how this file can be treated, can its "dom_id" not rather be
>>>> the "monitor scope domain id"? Could that not help to eliminate
>>>> that per-domain "display_id"?
>>>
>>> You are correct that this should be the "monitor scope domain id" rather
>>> than the first SNC domain that appears. I'll change to use that. I don't
>>> think it helps in removing the per-domain display_id.
>>
>> Wouldn't the file metadata then be the "display_id"?
>
> Yes. The metadata is the display_id for files that need to sum across
> SNC nodes, but the domain id for ones where no summation is needed.

Right ... and there is a "sum" flag to tell which is which?

Reinette

2024-05-14 18:26:23

by Luck, Tony

[permalink] [raw]
Subject: RE: [PATCH v17 7/9] x86/resctrl: Add new monitor files for Sub-NUMA cluster (SNC) monitoring

> On 5/13/2024 5:21 PM, Tony Luck wrote:
> > On Mon, May 13, 2024 at 11:53:17AM -0700, Reinette Chatre wrote:
> >> On 5/13/2024 10:05 AM, Tony Luck wrote:
> >>> On Fri, May 10, 2024 at 02:24:13PM -0700, Reinette Chatre wrote:
> >>> Thanks for the review. Detailed comments below. But overall I'm
> >>> going to split patch 7 into a bunch of smaller changes, each with
> >>> a better commit message.
> >>>
> >>>> On 5/3/2024 1:33 PM, Tony Luck wrote:
> >>>>
> >>>> (Could you please start the changelog with some context?)
> >>>>
> >>>>> Add a field to the rdt_resource structure to track whether monitoring
> >>>>> resources are tracked by hardware at a different scope (NODE) from
> >>>>> the legacy L3 scope.
> >>>>
> >>>> This seems to describe @mon_scope that was introduced in patch #3?
> >>>
> >>> Not really. Patch #3 made the change so that control an monitor
> >>> functions can have different scope. That's still needed as with SNC
> >>> enabled the underlying data collection is at the node level for
> >>> monitoring, while control stays at the L3 cache scope.
> >>>
> >>> This new field describes the legacy scope of monitoring, so that
> >>> resctrl can provide correctly scoped monitor files for legacy
> >>> applications that aren't aware of SNC. So I'm using this both
> >>> to indicate when SNC is enabled (with mon_scope != mon_display_scope)
> >>> or disabled (when they are the same).
> >>
> >> This seems to enforce the idea that these new additions aim to be
> >> generic on the surface but the only goal is to support SNC.
> >
> > If you have some more ideas on how to make this more generic and
> > less SNC specific I'm all ears.
>
> It may not end up being totally generic. It should not pretend to be
> when it is not. It makes the flows difficult to follow when there are
> these unexpected checks/quirks in what claims to be core code.

Do you want some sort of warning comments in pieces of code
that are SNC specific?

>
> >>>>> }
> >>>>> +
> >>>>> + return 0;
> >>>>> +}
> >>>>> +
> >>>>> +static int mkdir_mondata_subdir(struct kernfs_node *parent_kn,
> >>>>> + struct rdt_mon_domain *d,
> >>>>> + struct rdt_resource *r, struct rdtgroup *prgrp)
> >>>>> +{
> >>>>> + struct kernfs_node *kn, *ckn;
> >>>>> + char name[32];
> >>>>> + bool do_sum;
> >>>>> + int ret;
> >>>>> +
> >>>>> + do_sum = r->mon_scope != r->mon_display_scope;
> >>>>> + sprintf(name, "mon_%s_%02d", r->name, d->display_id);
> >>>>> + kn = kernfs_find_and_get_ns(parent_kn, name, NULL);
> >>>>> + if (!kn) {
> >>>>> + /* create the directory */
> >>>>> + kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
> >>>>> + if (IS_ERR(kn))
> >>>>> + return PTR_ERR(kn);
> >>>>> +
> >>>>> + ret = rdtgroup_kn_set_ugid(kn);
> >>>>> + if (ret)
> >>>>> + goto out_destroy;
> >>>>> + ret = mon_add_all_files(kn, d, r, prgrp, do_sum);
> >>>>
> >>>> This does not look right. If I understand correctly the private data
> >>>> of these event files will have whichever mon domain came up first as
> >>>> its domain id. That seems completely arbitrary and does not reflect
> >>>> accurate state for this file. Since "do_sum" is essentially a "flag"
> >>>> on how this file can be treated, can its "dom_id" not rather be
> >>>> the "monitor scope domain id"? Could that not help to eliminate
> >>>
> >>> You are correct that this should be the "monitor scope domain id" rather
> >>> than the first SNC domain that appears. I'll change to use that. I don't
> >>> think it helps in removing the per-domain display_id.
> >>
> >> Wouldn't the file metadata then be the "display_id"?
> >
> > Yes. The metadata is the display_id for files that need to sum across
> > SNC nodes, but the domain id for ones where no summation is needed.
>
> Right ... and there is a "sum" flag to tell which is which?

Yes. sum==0 means the domid field is the one and only domain to
report for this resctrl monitor file. sum==1 means the domid field is
the display_id - all domains with this display_id must be summed to
provide the result to present to the user.

I've tried to capture that in the kerneldoc comment for struct mon_event.
Here's what I'm planning to include in v18 (Outlook will probably mangle
the formatting ... just imagine that the text lines up neatly):

diff --git a/arch/x86/kernel/cpu/resctrl/internal.h b/arch/x86/kernel/cpu/resctrl/internal.h
index 49440f194253..3411557d761a 100644
--- a/arch/x86/kernel/cpu/resctrl/internal.h
+++ b/arch/x86/kernel/cpu/resctrl/internal.h
@@ -132,14 +132,19 @@ struct mon_evt {
* as kernfs private data
* @rid: Resource id associated with the event file
* @evtid: Event id associated with the event file
- * @domid: The domain to which the event file belongs
+ * @sum: Set when event must be summed across multiple
+ * domains.
+ * @domid: When @sum is zero this is the domain to which
+ * the event file belongs. When sum is one this
+ * is the display_id of all domains to be summed
* @u: Name of the bit fields struct
*/
union mon_data_bits {
void *priv;
struct {
unsigned int rid : 10;
- enum resctrl_event_id evtid : 8;
+ enum resctrl_event_id evtid : 7;
+ unsigned int sum : 1;
unsigned int domid : 14;
} u;
};

-Tony

2024-05-14 20:30:22

by Reinette Chatre

[permalink] [raw]
Subject: Re: [PATCH v17 7/9] x86/resctrl: Add new monitor files for Sub-NUMA cluster (SNC) monitoring

Hi Tony,

On 5/14/2024 11:26 AM, Luck, Tony wrote:
>> On 5/13/2024 5:21 PM, Tony Luck wrote:
>>> On Mon, May 13, 2024 at 11:53:17AM -0700, Reinette Chatre wrote:
>>>> On 5/13/2024 10:05 AM, Tony Luck wrote:
>>>>> On Fri, May 10, 2024 at 02:24:13PM -0700, Reinette Chatre wrote:
>>>>> Thanks for the review. Detailed comments below. But overall I'm
>>>>> going to split patch 7 into a bunch of smaller changes, each with
>>>>> a better commit message.
>>>>>
>>>>>> On 5/3/2024 1:33 PM, Tony Luck wrote:
>>>>>>
>>>>>> (Could you please start the changelog with some context?)
>>>>>>
>>>>>>> Add a field to the rdt_resource structure to track whether monitoring
>>>>>>> resources are tracked by hardware at a different scope (NODE) from
>>>>>>> the legacy L3 scope.
>>>>>>
>>>>>> This seems to describe @mon_scope that was introduced in patch #3?
>>>>>
>>>>> Not really. Patch #3 made the change so that control an monitor
>>>>> functions can have different scope. That's still needed as with SNC
>>>>> enabled the underlying data collection is at the node level for
>>>>> monitoring, while control stays at the L3 cache scope.
>>>>>
>>>>> This new field describes the legacy scope of monitoring, so that
>>>>> resctrl can provide correctly scoped monitor files for legacy
>>>>> applications that aren't aware of SNC. So I'm using this both
>>>>> to indicate when SNC is enabled (with mon_scope != mon_display_scope)
>>>>> or disabled (when they are the same).
>>>>
>>>> This seems to enforce the idea that these new additions aim to be
>>>> generic on the surface but the only goal is to support SNC.
>>>
>>> If you have some more ideas on how to make this more generic and
>>> less SNC specific I'm all ears.
>>
>> It may not end up being totally generic. It should not pretend to be
>> when it is not. It makes the flows difficult to follow when there are
>> these unexpected checks/quirks in what claims to be core code.
>
> Do you want some sort of warning comments in pieces of code
> that are SNC specific?

I cannot think now where warnings will be appropriate but if you
find instances then please do. To start the quirks can at least be
documented. For example, "Only user of <feature> is SNC, which does
not require <custom> so simplify by <describe shortcut> ..."

>
>>
>>>>>>> }
>>>>>>> +
>>>>>>> + return 0;
>>>>>>> +}
>>>>>>> +
>>>>>>> +static int mkdir_mondata_subdir(struct kernfs_node *parent_kn,
>>>>>>> + struct rdt_mon_domain *d,
>>>>>>> + struct rdt_resource *r, struct rdtgroup *prgrp)
>>>>>>> +{
>>>>>>> + struct kernfs_node *kn, *ckn;
>>>>>>> + char name[32];
>>>>>>> + bool do_sum;
>>>>>>> + int ret;
>>>>>>> +
>>>>>>> + do_sum = r->mon_scope != r->mon_display_scope;
>>>>>>> + sprintf(name, "mon_%s_%02d", r->name, d->display_id);
>>>>>>> + kn = kernfs_find_and_get_ns(parent_kn, name, NULL);
>>>>>>> + if (!kn) {
>>>>>>> + /* create the directory */
>>>>>>> + kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
>>>>>>> + if (IS_ERR(kn))
>>>>>>> + return PTR_ERR(kn);
>>>>>>> +
>>>>>>> + ret = rdtgroup_kn_set_ugid(kn);
>>>>>>> + if (ret)
>>>>>>> + goto out_destroy;
>>>>>>> + ret = mon_add_all_files(kn, d, r, prgrp, do_sum);
>>>>>>
>>>>>> This does not look right. If I understand correctly the private data
>>>>>> of these event files will have whichever mon domain came up first as
>>>>>> its domain id. That seems completely arbitrary and does not reflect
>>>>>> accurate state for this file. Since "do_sum" is essentially a "flag"
>>>>>> on how this file can be treated, can its "dom_id" not rather be
>>>>>> the "monitor scope domain id"? Could that not help to eliminate
>>>>>
>>>>> You are correct that this should be the "monitor scope domain id" rather
>>>>> than the first SNC domain that appears. I'll change to use that. I don't
>>>>> think it helps in removing the per-domain display_id.
>>>>
>>>> Wouldn't the file metadata then be the "display_id"?
>>>
>>> Yes. The metadata is the display_id for files that need to sum across
>>> SNC nodes, but the domain id for ones where no summation is needed.
>>
>> Right ... and there is a "sum" flag to tell which is which?
>
> Yes. sum==0 means the domid field is the one and only domain to
> report for this resctrl monitor file. sum==1 means the domid field is
> the display_id - all domains with this display_id must be summed to
> provide the result to present to the user.
>
> I've tried to capture that in the kerneldoc comment for struct mon_event.
> Here's what I'm planning to include in v18 (Outlook will probably mangle
> the formatting ... just imagine that the text lines up neatly):
>
> diff --git a/arch/x86/kernel/cpu/resctrl/internal.h b/arch/x86/kernel/cpu/resctrl/internal.h
> index 49440f194253..3411557d761a 100644
> --- a/arch/x86/kernel/cpu/resctrl/internal.h
> +++ b/arch/x86/kernel/cpu/resctrl/internal.h
> @@ -132,14 +132,19 @@ struct mon_evt {
> * as kernfs private data
> * @rid: Resource id associated with the event file
> * @evtid: Event id associated with the event file
> - * @domid: The domain to which the event file belongs
> + * @sum: Set when event must be summed across multiple
> + * domains.
> + * @domid: When @sum is zero this is the domain to which
> + * the event file belongs. When sum is one this
> + * is the display_id of all domains to be summed

Here is where I would like to understand why it cannot just be
"When sum is one this is the domain id of the scope at which (for which?)
the events must be summed." Although, you already mentioned this will be
clear in next posting.

> * @u: Name of the bit fields struct
> */
> union mon_data_bits {
> void *priv;
> struct {
> unsigned int rid : 10;
> - enum resctrl_event_id evtid : 8;
> + enum resctrl_event_id evtid : 7;
> + unsigned int sum : 1;
> unsigned int domid : 14;
> } u;
> };
>
> -Tony

Reinette

2024-05-14 21:53:10

by Luck, Tony

[permalink] [raw]
Subject: Re: [PATCH v17 7/9] x86/resctrl: Add new monitor files for Sub-NUMA cluster (SNC) monitoring

On Tue, May 14, 2024 at 01:30:05PM -0700, Reinette Chatre wrote:
> Hi Tony,
>
> On 5/14/2024 11:26 AM, Luck, Tony wrote:
> >> On 5/13/2024 5:21 PM, Tony Luck wrote:
> >>> On Mon, May 13, 2024 at 11:53:17AM -0700, Reinette Chatre wrote:
> >>>> On 5/13/2024 10:05 AM, Tony Luck wrote:
> >>>>> On Fri, May 10, 2024 at 02:24:13PM -0700, Reinette Chatre wrote:
> >>>>> Thanks for the review. Detailed comments below. But overall I'm
> >>>>> going to split patch 7 into a bunch of smaller changes, each with
> >>>>> a better commit message.
> >>>>>
> >>>>>> On 5/3/2024 1:33 PM, Tony Luck wrote:
> >>>>>>
> >>>>>> (Could you please start the changelog with some context?)
> >>>>>>
> >>>>>>> Add a field to the rdt_resource structure to track whether monitoring
> >>>>>>> resources are tracked by hardware at a different scope (NODE) from
> >>>>>>> the legacy L3 scope.
> >>>>>>
> >>>>>> This seems to describe @mon_scope that was introduced in patch #3?
> >>>>>
> >>>>> Not really. Patch #3 made the change so that control an monitor
> >>>>> functions can have different scope. That's still needed as with SNC
> >>>>> enabled the underlying data collection is at the node level for
> >>>>> monitoring, while control stays at the L3 cache scope.
> >>>>>
> >>>>> This new field describes the legacy scope of monitoring, so that
> >>>>> resctrl can provide correctly scoped monitor files for legacy
> >>>>> applications that aren't aware of SNC. So I'm using this both
> >>>>> to indicate when SNC is enabled (with mon_scope != mon_display_scope)
> >>>>> or disabled (when they are the same).
> >>>>
> >>>> This seems to enforce the idea that these new additions aim to be
> >>>> generic on the surface but the only goal is to support SNC.
> >>>
> >>> If you have some more ideas on how to make this more generic and
> >>> less SNC specific I'm all ears.
> >>
> >> It may not end up being totally generic. It should not pretend to be
> >> when it is not. It makes the flows difficult to follow when there are
> >> these unexpected checks/quirks in what claims to be core code.
> >
> > Do you want some sort of warning comments in pieces of code
> > that are SNC specific?
>
> I cannot think now where warnings will be appropriate but if you
> find instances then please do. To start the quirks can at least be
> documented. For example, "Only user of <feature> is SNC, which does
> not require <custom> so simplify by <describe shortcut> ..."

The main spot that triggered this line of discussion was changing the
sanity check that operations to read monitors is being done from a
CPU within the right domain. I've added a short comment on the new
check:

- if (!cpumask_test_cpu(smp_processor_id(), &d->hdr.cpu_mask))
+ /* Event counts can only be read from a CPU on the same L3 cache */
+ if (d->display_id != get_cpu_cacheinfo_id(smp_processor_id(), r->mon_display_scope))
return -EINVAL;

But my change embeds the assumption that monitor events are L3 scoped.

Should it be something like this (to keep the non-SNC case generic):

if (r->mon_scope == r->mon_display_scope) {
if (!cpumask_test_cpu(smp_processor_id(), &d->hdr.cpu_mask))
return -EINVAL;
} else {
/*
* SNC: OK to read events on any CPU sharing same L3
* cache instance.
*/
if (d->display_id != get_cpu_cacheinfo_id(smp_processor_id(), r->mon_display_scope))
return -EINVAL;
}

>
> >
> >>
> >>>>>>> }
> >>>>>>> +
> >>>>>>> + return 0;
> >>>>>>> +}
> >>>>>>> +
> >>>>>>> +static int mkdir_mondata_subdir(struct kernfs_node *parent_kn,
> >>>>>>> + struct rdt_mon_domain *d,
> >>>>>>> + struct rdt_resource *r, struct rdtgroup *prgrp)
> >>>>>>> +{
> >>>>>>> + struct kernfs_node *kn, *ckn;
> >>>>>>> + char name[32];
> >>>>>>> + bool do_sum;
> >>>>>>> + int ret;
> >>>>>>> +
> >>>>>>> + do_sum = r->mon_scope != r->mon_display_scope;
> >>>>>>> + sprintf(name, "mon_%s_%02d", r->name, d->display_id);
> >>>>>>> + kn = kernfs_find_and_get_ns(parent_kn, name, NULL);
> >>>>>>> + if (!kn) {
> >>>>>>> + /* create the directory */
> >>>>>>> + kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
> >>>>>>> + if (IS_ERR(kn))
> >>>>>>> + return PTR_ERR(kn);
> >>>>>>> +
> >>>>>>> + ret = rdtgroup_kn_set_ugid(kn);
> >>>>>>> + if (ret)
> >>>>>>> + goto out_destroy;
> >>>>>>> + ret = mon_add_all_files(kn, d, r, prgrp, do_sum);
> >>>>>>
> >>>>>> This does not look right. If I understand correctly the private data
> >>>>>> of these event files will have whichever mon domain came up first as
> >>>>>> its domain id. That seems completely arbitrary and does not reflect
> >>>>>> accurate state for this file. Since "do_sum" is essentially a "flag"
> >>>>>> on how this file can be treated, can its "dom_id" not rather be
> >>>>>> the "monitor scope domain id"? Could that not help to eliminate
> >>>>>
> >>>>> You are correct that this should be the "monitor scope domain id" rather
> >>>>> than the first SNC domain that appears. I'll change to use that. I don't
> >>>>> think it helps in removing the per-domain display_id.
> >>>>
> >>>> Wouldn't the file metadata then be the "display_id"?
> >>>
> >>> Yes. The metadata is the display_id for files that need to sum across
> >>> SNC nodes, but the domain id for ones where no summation is needed.
> >>
> >> Right ... and there is a "sum" flag to tell which is which?
> >
> > Yes. sum==0 means the domid field is the one and only domain to
> > report for this resctrl monitor file. sum==1 means the domid field is
> > the display_id - all domains with this display_id must be summed to
> > provide the result to present to the user.
> >
> > I've tried to capture that in the kerneldoc comment for struct mon_event.
> > Here's what I'm planning to include in v18 (Outlook will probably mangle
> > the formatting ... just imagine that the text lines up neatly):
> >
> > diff --git a/arch/x86/kernel/cpu/resctrl/internal.h b/arch/x86/kernel/cpu/resctrl/internal.h
> > index 49440f194253..3411557d761a 100644
> > --- a/arch/x86/kernel/cpu/resctrl/internal.h
> > +++ b/arch/x86/kernel/cpu/resctrl/internal.h
> > @@ -132,14 +132,19 @@ struct mon_evt {
> > * as kernfs private data
> > * @rid: Resource id associated with the event file
> > * @evtid: Event id associated with the event file
> > - * @domid: The domain to which the event file belongs
> > + * @sum: Set when event must be summed across multiple
> > + * domains.
> > + * @domid: When @sum is zero this is the domain to which
> > + * the event file belongs. When sum is one this
> > + * is the display_id of all domains to be summed
>
> Here is where I would like to understand why it cannot just be
> "When sum is one this is the domain id of the scope at which (for which?)
> the events must be summed." Although, you already mentioned this will be
> clear in next posting.
>
> > * @u: Name of the bit fields struct
> > */
> > union mon_data_bits {
> > void *priv;
> > struct {
> > unsigned int rid : 10;
> > - enum resctrl_event_id evtid : 8;
> > + enum resctrl_event_id evtid : 7;
> > + unsigned int sum : 1;
> > unsigned int domid : 14;
> > } u;
> > };
> >
> > -Tony

Maybe an example might help. Assume an SNC system with two sockets,
three SNC nodes per socket, only supporting monitoring. The only domain
list created by resctrl is the mon_domains list on the RDT_RESOURCE_L3
resource. And it looks like this (with "disply_list" abbreviated to
"dspl" to keep the picture small):


<------ SNC NODES ON SOCKET 0 -----> <------ SNC NODES ON SOCKET 1 ------>
----> +----------+ +----------+ +----------+ +----------+ +----------+ +----------+
| id = 0 | | id = 1 | | id = 2 | | id = 3 | | id = 4 | | id = 5 |
| | | | | | | | | | | |
| dspl = 0 | | dspl = 0 | | dspl = 0 | | dspl = 1 | | dspl = 1 | | dspl = 1 |
| | | | | | | | | | | |
+----------+ +----------+ +----------+ +----------+ +----------+ +----------+

Reading the per-SNC node monitor values looks just the same as the
non-SNC case. The struct rmid_read passed across the smp_call*() has
the resource, domain, event, and reading the counters is essentially
unchanged.

Reading a file to sum event counts for SNC nodes on socket 1 needs to
find each of the "struct rdt_mon_domain" that are part of socket 1.
I'm doing that with meta data in the file that says sum=1 (need to add
up something) and domid=1 (the things to be added are those with
display_id = 1). So the code reads:

list_for_each_entry(d, &rr->r->mon_domains, hdr.list) {
if (d->display_id == rr->d->display_id) {
... call stuff to read and sum for domain "d"
}
}

The display_id is "the domain id of the scope at which (for which?)
the events must be summed." in your text above.

> Reinette

-Tony

2024-05-15 16:47:49

by Reinette Chatre

[permalink] [raw]
Subject: Re: [PATCH v17 7/9] x86/resctrl: Add new monitor files for Sub-NUMA cluster (SNC) monitoring

Hi Tony,

On 5/14/2024 2:53 PM, Tony Luck wrote:
> On Tue, May 14, 2024 at 01:30:05PM -0700, Reinette Chatre wrote:
>> Hi Tony,
>>
>> On 5/14/2024 11:26 AM, Luck, Tony wrote:
>>>> On 5/13/2024 5:21 PM, Tony Luck wrote:
>>>>> On Mon, May 13, 2024 at 11:53:17AM -0700, Reinette Chatre wrote:
>>>>>> On 5/13/2024 10:05 AM, Tony Luck wrote:
>>>>>>> On Fri, May 10, 2024 at 02:24:13PM -0700, Reinette Chatre wrote:
>>>>>>> Thanks for the review. Detailed comments below. But overall I'm
>>>>>>> going to split patch 7 into a bunch of smaller changes, each with
>>>>>>> a better commit message.
>>>>>>>
>>>>>>>> On 5/3/2024 1:33 PM, Tony Luck wrote:
>>>>>>>>
>>>>>>>> (Could you please start the changelog with some context?)
>>>>>>>>
>>>>>>>>> Add a field to the rdt_resource structure to track whether monitoring
>>>>>>>>> resources are tracked by hardware at a different scope (NODE) from
>>>>>>>>> the legacy L3 scope.
>>>>>>>>
>>>>>>>> This seems to describe @mon_scope that was introduced in patch #3?
>>>>>>>
>>>>>>> Not really. Patch #3 made the change so that control an monitor
>>>>>>> functions can have different scope. That's still needed as with SNC
>>>>>>> enabled the underlying data collection is at the node level for
>>>>>>> monitoring, while control stays at the L3 cache scope.
>>>>>>>
>>>>>>> This new field describes the legacy scope of monitoring, so that
>>>>>>> resctrl can provide correctly scoped monitor files for legacy
>>>>>>> applications that aren't aware of SNC. So I'm using this both
>>>>>>> to indicate when SNC is enabled (with mon_scope != mon_display_scope)
>>>>>>> or disabled (when they are the same).
>>>>>>
>>>>>> This seems to enforce the idea that these new additions aim to be
>>>>>> generic on the surface but the only goal is to support SNC.
>>>>>
>>>>> If you have some more ideas on how to make this more generic and
>>>>> less SNC specific I'm all ears.
>>>>
>>>> It may not end up being totally generic. It should not pretend to be
>>>> when it is not. It makes the flows difficult to follow when there are
>>>> these unexpected checks/quirks in what claims to be core code.
>>>
>>> Do you want some sort of warning comments in pieces of code
>>> that are SNC specific?
>>
>> I cannot think now where warnings will be appropriate but if you
>> find instances then please do. To start the quirks can at least be
>> documented. For example, "Only user of <feature> is SNC, which does
>> not require <custom> so simplify by <describe shortcut> ..."
>
> The main spot that triggered this line of discussion was changing the
> sanity check that operations to read monitors is being done from a
> CPU within the right domain. I've added a short comment on the new
> check:
>
> - if (!cpumask_test_cpu(smp_processor_id(), &d->hdr.cpu_mask))
> + /* Event counts can only be read from a CPU on the same L3 cache */
> + if (d->display_id != get_cpu_cacheinfo_id(smp_processor_id(), r->mon_display_scope))
> return -EINVAL;
>
> But my change embeds the assumption that monitor events are L3 scoped.
>
> Should it be something like this (to keep the non-SNC case generic):
>
> if (r->mon_scope == r->mon_display_scope) {
> if (!cpumask_test_cpu(smp_processor_id(), &d->hdr.cpu_mask))
> return -EINVAL;

Yes, keeping this check looks good to me ...

> } else {
> /*
> * SNC: OK to read events on any CPU sharing same L3
> * cache instance.
> */
> if (d->display_id != get_cpu_cacheinfo_id(smp_processor_id(), r->mon_display_scope))
> return -EINVAL;
> }

.. while I remain unsure about where "display_id" fits in.

>
>>
>>>
>>>>
>>>>>>>>> }
>>>>>>>>> +
>>>>>>>>> + return 0;
>>>>>>>>> +}
>>>>>>>>> +
>>>>>>>>> +static int mkdir_mondata_subdir(struct kernfs_node *parent_kn,
>>>>>>>>> + struct rdt_mon_domain *d,
>>>>>>>>> + struct rdt_resource *r, struct rdtgroup *prgrp)
>>>>>>>>> +{
>>>>>>>>> + struct kernfs_node *kn, *ckn;
>>>>>>>>> + char name[32];
>>>>>>>>> + bool do_sum;
>>>>>>>>> + int ret;
>>>>>>>>> +
>>>>>>>>> + do_sum = r->mon_scope != r->mon_display_scope;
>>>>>>>>> + sprintf(name, "mon_%s_%02d", r->name, d->display_id);
>>>>>>>>> + kn = kernfs_find_and_get_ns(parent_kn, name, NULL);
>>>>>>>>> + if (!kn) {
>>>>>>>>> + /* create the directory */
>>>>>>>>> + kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
>>>>>>>>> + if (IS_ERR(kn))
>>>>>>>>> + return PTR_ERR(kn);
>>>>>>>>> +
>>>>>>>>> + ret = rdtgroup_kn_set_ugid(kn);
>>>>>>>>> + if (ret)
>>>>>>>>> + goto out_destroy;
>>>>>>>>> + ret = mon_add_all_files(kn, d, r, prgrp, do_sum);
>>>>>>>>
>>>>>>>> This does not look right. If I understand correctly the private data
>>>>>>>> of these event files will have whichever mon domain came up first as
>>>>>>>> its domain id. That seems completely arbitrary and does not reflect
>>>>>>>> accurate state for this file. Since "do_sum" is essentially a "flag"
>>>>>>>> on how this file can be treated, can its "dom_id" not rather be
>>>>>>>> the "monitor scope domain id"? Could that not help to eliminate
>>>>>>>
>>>>>>> You are correct that this should be the "monitor scope domain id" rather
>>>>>>> than the first SNC domain that appears. I'll change to use that. I don't
>>>>>>> think it helps in removing the per-domain display_id.
>>>>>>
>>>>>> Wouldn't the file metadata then be the "display_id"?
>>>>>
>>>>> Yes. The metadata is the display_id for files that need to sum across
>>>>> SNC nodes, but the domain id for ones where no summation is needed.
>>>>
>>>> Right ... and there is a "sum" flag to tell which is which?
>>>
>>> Yes. sum==0 means the domid field is the one and only domain to
>>> report for this resctrl monitor file. sum==1 means the domid field is
>>> the display_id - all domains with this display_id must be summed to
>>> provide the result to present to the user.
>>>
>>> I've tried to capture that in the kerneldoc comment for struct mon_event.
>>> Here's what I'm planning to include in v18 (Outlook will probably mangle
>>> the formatting ... just imagine that the text lines up neatly):
>>>
>>> diff --git a/arch/x86/kernel/cpu/resctrl/internal.h b/arch/x86/kernel/cpu/resctrl/internal.h
>>> index 49440f194253..3411557d761a 100644
>>> --- a/arch/x86/kernel/cpu/resctrl/internal.h
>>> +++ b/arch/x86/kernel/cpu/resctrl/internal.h
>>> @@ -132,14 +132,19 @@ struct mon_evt {
>>> * as kernfs private data
>>> * @rid: Resource id associated with the event file
>>> * @evtid: Event id associated with the event file
>>> - * @domid: The domain to which the event file belongs
>>> + * @sum: Set when event must be summed across multiple
>>> + * domains.
>>> + * @domid: When @sum is zero this is the domain to which
>>> + * the event file belongs. When sum is one this
>>> + * is the display_id of all domains to be summed
>>
>> Here is where I would like to understand why it cannot just be
>> "When sum is one this is the domain id of the scope at which (for which?)
>> the events must be summed." Although, you already mentioned this will be
>> clear in next posting.
>>
>>> * @u: Name of the bit fields struct
>>> */
>>> union mon_data_bits {
>>> void *priv;
>>> struct {
>>> unsigned int rid : 10;
>>> - enum resctrl_event_id evtid : 8;
>>> + enum resctrl_event_id evtid : 7;
>>> + unsigned int sum : 1;
>>> unsigned int domid : 14;
>>> } u;
>>> };
>>>
>>> -Tony
>
> Maybe an example might help. Assume an SNC system with two sockets,
> three SNC nodes per socket, only supporting monitoring. The only domain
> list created by resctrl is the mon_domains list on the RDT_RESOURCE_L3
> resource. And it looks like this (with "disply_list" abbreviated to
> "dspl" to keep the picture small):
>
>
> <------ SNC NODES ON SOCKET 0 -----> <------ SNC NODES ON SOCKET 1 ------>
> ----> +----------+ +----------+ +----------+ +----------+ +----------+ +----------+
> | id = 0 | | id = 1 | | id = 2 | | id = 3 | | id = 4 | | id = 5 |
> | | | | | | | | | | | |
> | dspl = 0 | | dspl = 0 | | dspl = 0 | | dspl = 1 | | dspl = 1 | | dspl = 1 |
> | | | | | | | | | | | |
> +----------+ +----------+ +----------+ +----------+ +----------+ +----------+
>
> Reading the per-SNC node monitor values looks just the same as the
> non-SNC case. The struct rmid_read passed across the smp_call*() has
> the resource, domain, event, and reading the counters is essentially
> unchanged.
>
> Reading a file to sum event counts for SNC nodes on socket 1 needs to
> find each of the "struct rdt_mon_domain" that are part of socket 1.
> I'm doing that with meta data in the file that says sum=1 (need to add
> up something) and domid=1 (the things to be added are those with
> display_id = 1). So the code reads:
>
> list_for_each_entry(d, &rr->r->mon_domains, hdr.list) {
> if (d->display_id == rr->d->display_id) {
> ... call stuff to read and sum for domain "d"
> }
> }
>
> The display_id is "the domain id of the scope at which (for which?)
> the events must be summed." in your text above.

My point remains that it is not clear (to me) why it is required to
carry the display_id around.

list_for_each_entry(d, &rr->r->mon_domains, hdr.list) {
/* determine @id of @d at rr->r->mon_display_scope */
if (id == domid) {
... call stuff to read and sum for domain "d"
}
}

Reinette


2024-05-15 17:24:03

by Luck, Tony

[permalink] [raw]
Subject: Re: [PATCH v17 7/9] x86/resctrl: Add new monitor files for Sub-NUMA cluster (SNC) monitoring

On Wed, May 15, 2024 at 09:47:28AM -0700, Reinette Chatre wrote:
> Hi Tony,
>
> On 5/14/2024 2:53 PM, Tony Luck wrote:
> > On Tue, May 14, 2024 at 01:30:05PM -0700, Reinette Chatre wrote:
> >> Hi Tony,
> >>
> >> On 5/14/2024 11:26 AM, Luck, Tony wrote:
> >>>> On 5/13/2024 5:21 PM, Tony Luck wrote:
> >>>>> On Mon, May 13, 2024 at 11:53:17AM -0700, Reinette Chatre wrote:
> >>>>>> On 5/13/2024 10:05 AM, Tony Luck wrote:
> >>>>>>> On Fri, May 10, 2024 at 02:24:13PM -0700, Reinette Chatre wrote:
> >>>>>>> Thanks for the review. Detailed comments below. But overall I'm
> >>>>>>> going to split patch 7 into a bunch of smaller changes, each with
> >>>>>>> a better commit message.
> >>>>>>>
> >>>>>>>> On 5/3/2024 1:33 PM, Tony Luck wrote:
> >>>>>>>>
> >>>>>>>> (Could you please start the changelog with some context?)
> >>>>>>>>
> >>>>>>>>> Add a field to the rdt_resource structure to track whether monitoring
> >>>>>>>>> resources are tracked by hardware at a different scope (NODE) from
> >>>>>>>>> the legacy L3 scope.
> >>>>>>>>
> >>>>>>>> This seems to describe @mon_scope that was introduced in patch #3?
> >>>>>>>
> >>>>>>> Not really. Patch #3 made the change so that control an monitor
> >>>>>>> functions can have different scope. That's still needed as with SNC
> >>>>>>> enabled the underlying data collection is at the node level for
> >>>>>>> monitoring, while control stays at the L3 cache scope.
> >>>>>>>
> >>>>>>> This new field describes the legacy scope of monitoring, so that
> >>>>>>> resctrl can provide correctly scoped monitor files for legacy
> >>>>>>> applications that aren't aware of SNC. So I'm using this both
> >>>>>>> to indicate when SNC is enabled (with mon_scope != mon_display_scope)
> >>>>>>> or disabled (when they are the same).
> >>>>>>
> >>>>>> This seems to enforce the idea that these new additions aim to be
> >>>>>> generic on the surface but the only goal is to support SNC.
> >>>>>
> >>>>> If you have some more ideas on how to make this more generic and
> >>>>> less SNC specific I'm all ears.
> >>>>
> >>>> It may not end up being totally generic. It should not pretend to be
> >>>> when it is not. It makes the flows difficult to follow when there are
> >>>> these unexpected checks/quirks in what claims to be core code.
> >>>
> >>> Do you want some sort of warning comments in pieces of code
> >>> that are SNC specific?
> >>
> >> I cannot think now where warnings will be appropriate but if you
> >> find instances then please do. To start the quirks can at least be
> >> documented. For example, "Only user of <feature> is SNC, which does
> >> not require <custom> so simplify by <describe shortcut> ..."
> >
> > The main spot that triggered this line of discussion was changing the
> > sanity check that operations to read monitors is being done from a
> > CPU within the right domain. I've added a short comment on the new
> > check:
> >
> > - if (!cpumask_test_cpu(smp_processor_id(), &d->hdr.cpu_mask))
> > + /* Event counts can only be read from a CPU on the same L3 cache */
> > + if (d->display_id != get_cpu_cacheinfo_id(smp_processor_id(), r->mon_display_scope))
> > return -EINVAL;
> >
> > But my change embeds the assumption that monitor events are L3 scoped.
> >
> > Should it be something like this (to keep the non-SNC case generic):
> >
> > if (r->mon_scope == r->mon_display_scope) {
> > if (!cpumask_test_cpu(smp_processor_id(), &d->hdr.cpu_mask))
> > return -EINVAL;
>
> Yes, keeping this check looks good to me ...
>
> > } else {
> > /*
> > * SNC: OK to read events on any CPU sharing same L3
> > * cache instance.
> > */
> > if (d->display_id != get_cpu_cacheinfo_id(smp_processor_id(), r->mon_display_scope))
> > return -EINVAL;
> > }
>
> ... while I remain unsure about where "display_id" fits in.

See below.

> >
> >>
> >>>
> >>>>
> >>>>>>>>> }
> >>>>>>>>> +
> >>>>>>>>> + return 0;
> >>>>>>>>> +}
> >>>>>>>>> +
> >>>>>>>>> +static int mkdir_mondata_subdir(struct kernfs_node *parent_kn,
> >>>>>>>>> + struct rdt_mon_domain *d,
> >>>>>>>>> + struct rdt_resource *r, struct rdtgroup *prgrp)
> >>>>>>>>> +{
> >>>>>>>>> + struct kernfs_node *kn, *ckn;
> >>>>>>>>> + char name[32];
> >>>>>>>>> + bool do_sum;
> >>>>>>>>> + int ret;
> >>>>>>>>> +
> >>>>>>>>> + do_sum = r->mon_scope != r->mon_display_scope;
> >>>>>>>>> + sprintf(name, "mon_%s_%02d", r->name, d->display_id);
> >>>>>>>>> + kn = kernfs_find_and_get_ns(parent_kn, name, NULL);
> >>>>>>>>> + if (!kn) {
> >>>>>>>>> + /* create the directory */
> >>>>>>>>> + kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
> >>>>>>>>> + if (IS_ERR(kn))
> >>>>>>>>> + return PTR_ERR(kn);
> >>>>>>>>> +
> >>>>>>>>> + ret = rdtgroup_kn_set_ugid(kn);
> >>>>>>>>> + if (ret)
> >>>>>>>>> + goto out_destroy;
> >>>>>>>>> + ret = mon_add_all_files(kn, d, r, prgrp, do_sum);
> >>>>>>>>
> >>>>>>>> This does not look right. If I understand correctly the private data
> >>>>>>>> of these event files will have whichever mon domain came up first as
> >>>>>>>> its domain id. That seems completely arbitrary and does not reflect
> >>>>>>>> accurate state for this file. Since "do_sum" is essentially a "flag"
> >>>>>>>> on how this file can be treated, can its "dom_id" not rather be
> >>>>>>>> the "monitor scope domain id"? Could that not help to eliminate
> >>>>>>>
> >>>>>>> You are correct that this should be the "monitor scope domain id" rather
> >>>>>>> than the first SNC domain that appears. I'll change to use that. I don't
> >>>>>>> think it helps in removing the per-domain display_id.
> >>>>>>
> >>>>>> Wouldn't the file metadata then be the "display_id"?
> >>>>>
> >>>>> Yes. The metadata is the display_id for files that need to sum across
> >>>>> SNC nodes, but the domain id for ones where no summation is needed.
> >>>>
> >>>> Right ... and there is a "sum" flag to tell which is which?
> >>>
> >>> Yes. sum==0 means the domid field is the one and only domain to
> >>> report for this resctrl monitor file. sum==1 means the domid field is
> >>> the display_id - all domains with this display_id must be summed to
> >>> provide the result to present to the user.
> >>>
> >>> I've tried to capture that in the kerneldoc comment for struct mon_event.
> >>> Here's what I'm planning to include in v18 (Outlook will probably mangle
> >>> the formatting ... just imagine that the text lines up neatly):
> >>>
> >>> diff --git a/arch/x86/kernel/cpu/resctrl/internal.h b/arch/x86/kernel/cpu/resctrl/internal.h
> >>> index 49440f194253..3411557d761a 100644
> >>> --- a/arch/x86/kernel/cpu/resctrl/internal.h
> >>> +++ b/arch/x86/kernel/cpu/resctrl/internal.h
> >>> @@ -132,14 +132,19 @@ struct mon_evt {
> >>> * as kernfs private data
> >>> * @rid: Resource id associated with the event file
> >>> * @evtid: Event id associated with the event file
> >>> - * @domid: The domain to which the event file belongs
> >>> + * @sum: Set when event must be summed across multiple
> >>> + * domains.
> >>> + * @domid: When @sum is zero this is the domain to which
> >>> + * the event file belongs. When sum is one this
> >>> + * is the display_id of all domains to be summed
> >>
> >> Here is where I would like to understand why it cannot just be
> >> "When sum is one this is the domain id of the scope at which (for which?)
> >> the events must be summed." Although, you already mentioned this will be
> >> clear in next posting.
> >>
> >>> * @u: Name of the bit fields struct
> >>> */
> >>> union mon_data_bits {
> >>> void *priv;
> >>> struct {
> >>> unsigned int rid : 10;
> >>> - enum resctrl_event_id evtid : 8;
> >>> + enum resctrl_event_id evtid : 7;
> >>> + unsigned int sum : 1;
> >>> unsigned int domid : 14;
> >>> } u;
> >>> };
> >>>
> >>> -Tony
> >
> > Maybe an example might help. Assume an SNC system with two sockets,
> > three SNC nodes per socket, only supporting monitoring. The only domain
> > list created by resctrl is the mon_domains list on the RDT_RESOURCE_L3
> > resource. And it looks like this (with "disply_list" abbreviated to
> > "dspl" to keep the picture small):
> >
> >
> > <------ SNC NODES ON SOCKET 0 -----> <------ SNC NODES ON SOCKET 1 ------>
> > ----> +----------+ +----------+ +----------+ +----------+ +----------+ +----------+
> > | id = 0 | | id = 1 | | id = 2 | | id = 3 | | id = 4 | | id = 5 |
> > | | | | | | | | | | | |
> > | dspl = 0 | | dspl = 0 | | dspl = 0 | | dspl = 1 | | dspl = 1 | | dspl = 1 |
> > | | | | | | | | | | | |
> > +----------+ +----------+ +----------+ +----------+ +----------+ +----------+
> >
> > Reading the per-SNC node monitor values looks just the same as the
> > non-SNC case. The struct rmid_read passed across the smp_call*() has
> > the resource, domain, event, and reading the counters is essentially
> > unchanged.
> >
> > Reading a file to sum event counts for SNC nodes on socket 1 needs to
> > find each of the "struct rdt_mon_domain" that are part of socket 1.
> > I'm doing that with meta data in the file that says sum=1 (need to add
> > up something) and domid=1 (the things to be added are those with
> > display_id = 1). So the code reads:
> >
> > list_for_each_entry(d, &rr->r->mon_domains, hdr.list) {
> > if (d->display_id == rr->d->display_id) {
> > ... call stuff to read and sum for domain "d"
> > }
> > }
> >
> > The display_id is "the domain id of the scope at which (for which?)
> > the events must be summed." in your text above.
>
> My point remains that it is not clear (to me) why it is required to
> carry the display_id around.
>
> list_for_each_entry(d, &rr->r->mon_domains, hdr.list) {
> /* determine @id of @d at rr->r->mon_display_scope */
> if (id == domid) {
> ... call stuff to read and sum for domain "d"
> }
> }

That "determine @id of @d at rr->r->mon_display_scope" is:

display_id = get_domain_id_from_scope(cpumask_first(rr->d->hdr.cpu_mask), rr->r->mon_display_scope);
if (display_id < 0) {
take some error action
}

So it certainly isn't *required* to carry display_id around. But doing
so makes the code simpler. I could bury the long line into a helper
macro/function. But I can't bury the error check.

I'd also need to change get_domain_id_from_scope() from "static" to
global so it can be used in other files besides core.c

Note that there are several places where I need to use display_id,
computing it at run time in each place, but it seems so much easier to
do it once at domain creation time.

>
> Reinette

-Tony

2024-05-15 18:49:10

by Reinette Chatre

[permalink] [raw]
Subject: Re: [PATCH v17 7/9] x86/resctrl: Add new monitor files for Sub-NUMA cluster (SNC) monitoring

Hi Tony,

On 5/15/2024 10:23 AM, Tony Luck wrote:
> On Wed, May 15, 2024 at 09:47:28AM -0700, Reinette Chatre wrote:
>> Hi Tony,
>>
>> On 5/14/2024 2:53 PM, Tony Luck wrote:
>>> On Tue, May 14, 2024 at 01:30:05PM -0700, Reinette Chatre wrote:
>>>> Hi Tony,
>>>>
>>>> On 5/14/2024 11:26 AM, Luck, Tony wrote:
>>>>>> On 5/13/2024 5:21 PM, Tony Luck wrote:
>>>>>>> On Mon, May 13, 2024 at 11:53:17AM -0700, Reinette Chatre wrote:
>>>>>>>> On 5/13/2024 10:05 AM, Tony Luck wrote:
>>>>>>>>> On Fri, May 10, 2024 at 02:24:13PM -0700, Reinette Chatre wrote:
>>>>>>>>> Thanks for the review. Detailed comments below. But overall I'm
>>>>>>>>> going to split patch 7 into a bunch of smaller changes, each with
>>>>>>>>> a better commit message.
>>>>>>>>>
>>>>>>>>>> On 5/3/2024 1:33 PM, Tony Luck wrote:
>>>>>>>>>>
>>>>>>>>>> (Could you please start the changelog with some context?)
>>>>>>>>>>
>>>>>>>>>>> Add a field to the rdt_resource structure to track whether monitoring
>>>>>>>>>>> resources are tracked by hardware at a different scope (NODE) from
>>>>>>>>>>> the legacy L3 scope.
>>>>>>>>>>
>>>>>>>>>> This seems to describe @mon_scope that was introduced in patch #3?
>>>>>>>>>
>>>>>>>>> Not really. Patch #3 made the change so that control an monitor
>>>>>>>>> functions can have different scope. That's still needed as with SNC
>>>>>>>>> enabled the underlying data collection is at the node level for
>>>>>>>>> monitoring, while control stays at the L3 cache scope.
>>>>>>>>>
>>>>>>>>> This new field describes the legacy scope of monitoring, so that
>>>>>>>>> resctrl can provide correctly scoped monitor files for legacy
>>>>>>>>> applications that aren't aware of SNC. So I'm using this both
>>>>>>>>> to indicate when SNC is enabled (with mon_scope != mon_display_scope)
>>>>>>>>> or disabled (when they are the same).
>>>>>>>>
>>>>>>>> This seems to enforce the idea that these new additions aim to be
>>>>>>>> generic on the surface but the only goal is to support SNC.
>>>>>>>
>>>>>>> If you have some more ideas on how to make this more generic and
>>>>>>> less SNC specific I'm all ears.
>>>>>>
>>>>>> It may not end up being totally generic. It should not pretend to be
>>>>>> when it is not. It makes the flows difficult to follow when there are
>>>>>> these unexpected checks/quirks in what claims to be core code.
>>>>>
>>>>> Do you want some sort of warning comments in pieces of code
>>>>> that are SNC specific?
>>>>
>>>> I cannot think now where warnings will be appropriate but if you
>>>> find instances then please do. To start the quirks can at least be
>>>> documented. For example, "Only user of <feature> is SNC, which does
>>>> not require <custom> so simplify by <describe shortcut> ..."
>>>
>>> The main spot that triggered this line of discussion was changing the
>>> sanity check that operations to read monitors is being done from a
>>> CPU within the right domain. I've added a short comment on the new
>>> check:
>>>
>>> - if (!cpumask_test_cpu(smp_processor_id(), &d->hdr.cpu_mask))
>>> + /* Event counts can only be read from a CPU on the same L3 cache */
>>> + if (d->display_id != get_cpu_cacheinfo_id(smp_processor_id(), r->mon_display_scope))
>>> return -EINVAL;
>>>
>>> But my change embeds the assumption that monitor events are L3 scoped.
>>>
>>> Should it be something like this (to keep the non-SNC case generic):
>>>
>>> if (r->mon_scope == r->mon_display_scope) {
>>> if (!cpumask_test_cpu(smp_processor_id(), &d->hdr.cpu_mask))
>>> return -EINVAL;
>>
>> Yes, keeping this check looks good to me ...
>>
>>> } else {
>>> /*
>>> * SNC: OK to read events on any CPU sharing same L3
>>> * cache instance.
>>> */
>>> if (d->display_id != get_cpu_cacheinfo_id(smp_processor_id(), r->mon_display_scope))
>>> return -EINVAL;
>>> }
>>
>> ... while I remain unsure about where "display_id" fits in.
>
> See below.
>
>>>
>>>>
>>>>>
>>>>>>
>>>>>>>>>>> }
>>>>>>>>>>> +
>>>>>>>>>>> + return 0;
>>>>>>>>>>> +}
>>>>>>>>>>> +
>>>>>>>>>>> +static int mkdir_mondata_subdir(struct kernfs_node *parent_kn,
>>>>>>>>>>> + struct rdt_mon_domain *d,
>>>>>>>>>>> + struct rdt_resource *r, struct rdtgroup *prgrp)
>>>>>>>>>>> +{
>>>>>>>>>>> + struct kernfs_node *kn, *ckn;
>>>>>>>>>>> + char name[32];
>>>>>>>>>>> + bool do_sum;
>>>>>>>>>>> + int ret;
>>>>>>>>>>> +
>>>>>>>>>>> + do_sum = r->mon_scope != r->mon_display_scope;
>>>>>>>>>>> + sprintf(name, "mon_%s_%02d", r->name, d->display_id);
>>>>>>>>>>> + kn = kernfs_find_and_get_ns(parent_kn, name, NULL);
>>>>>>>>>>> + if (!kn) {
>>>>>>>>>>> + /* create the directory */
>>>>>>>>>>> + kn = kernfs_create_dir(parent_kn, name, parent_kn->mode, prgrp);
>>>>>>>>>>> + if (IS_ERR(kn))
>>>>>>>>>>> + return PTR_ERR(kn);
>>>>>>>>>>> +
>>>>>>>>>>> + ret = rdtgroup_kn_set_ugid(kn);
>>>>>>>>>>> + if (ret)
>>>>>>>>>>> + goto out_destroy;
>>>>>>>>>>> + ret = mon_add_all_files(kn, d, r, prgrp, do_sum);
>>>>>>>>>>
>>>>>>>>>> This does not look right. If I understand correctly the private data
>>>>>>>>>> of these event files will have whichever mon domain came up first as
>>>>>>>>>> its domain id. That seems completely arbitrary and does not reflect
>>>>>>>>>> accurate state for this file. Since "do_sum" is essentially a "flag"
>>>>>>>>>> on how this file can be treated, can its "dom_id" not rather be
>>>>>>>>>> the "monitor scope domain id"? Could that not help to eliminate
>>>>>>>>>
>>>>>>>>> You are correct that this should be the "monitor scope domain id" rather
>>>>>>>>> than the first SNC domain that appears. I'll change to use that. I don't
>>>>>>>>> think it helps in removing the per-domain display_id.
>>>>>>>>
>>>>>>>> Wouldn't the file metadata then be the "display_id"?
>>>>>>>
>>>>>>> Yes. The metadata is the display_id for files that need to sum across
>>>>>>> SNC nodes, but the domain id for ones where no summation is needed.
>>>>>>
>>>>>> Right ... and there is a "sum" flag to tell which is which?
>>>>>
>>>>> Yes. sum==0 means the domid field is the one and only domain to
>>>>> report for this resctrl monitor file. sum==1 means the domid field is
>>>>> the display_id - all domains with this display_id must be summed to
>>>>> provide the result to present to the user.
>>>>>
>>>>> I've tried to capture that in the kerneldoc comment for struct mon_event.
>>>>> Here's what I'm planning to include in v18 (Outlook will probably mangle
>>>>> the formatting ... just imagine that the text lines up neatly):
>>>>>
>>>>> diff --git a/arch/x86/kernel/cpu/resctrl/internal.h b/arch/x86/kernel/cpu/resctrl/internal.h
>>>>> index 49440f194253..3411557d761a 100644
>>>>> --- a/arch/x86/kernel/cpu/resctrl/internal.h
>>>>> +++ b/arch/x86/kernel/cpu/resctrl/internal.h
>>>>> @@ -132,14 +132,19 @@ struct mon_evt {
>>>>> * as kernfs private data
>>>>> * @rid: Resource id associated with the event file
>>>>> * @evtid: Event id associated with the event file
>>>>> - * @domid: The domain to which the event file belongs
>>>>> + * @sum: Set when event must be summed across multiple
>>>>> + * domains.
>>>>> + * @domid: When @sum is zero this is the domain to which
>>>>> + * the event file belongs. When sum is one this
>>>>> + * is the display_id of all domains to be summed
>>>>
>>>> Here is where I would like to understand why it cannot just be
>>>> "When sum is one this is the domain id of the scope at which (for which?)
>>>> the events must be summed." Although, you already mentioned this will be
>>>> clear in next posting.
>>>>
>>>>> * @u: Name of the bit fields struct
>>>>> */
>>>>> union mon_data_bits {
>>>>> void *priv;
>>>>> struct {
>>>>> unsigned int rid : 10;
>>>>> - enum resctrl_event_id evtid : 8;
>>>>> + enum resctrl_event_id evtid : 7;
>>>>> + unsigned int sum : 1;
>>>>> unsigned int domid : 14;
>>>>> } u;
>>>>> };
>>>>>
>>>>> -Tony
>>>
>>> Maybe an example might help. Assume an SNC system with two sockets,
>>> three SNC nodes per socket, only supporting monitoring. The only domain
>>> list created by resctrl is the mon_domains list on the RDT_RESOURCE_L3
>>> resource. And it looks like this (with "disply_list" abbreviated to
>>> "dspl" to keep the picture small):
>>>
>>>
>>> <------ SNC NODES ON SOCKET 0 -----> <------ SNC NODES ON SOCKET 1 ------>
>>> ----> +----------+ +----------+ +----------+ +----------+ +----------+ +----------+
>>> | id = 0 | | id = 1 | | id = 2 | | id = 3 | | id = 4 | | id = 5 |
>>> | | | | | | | | | | | |
>>> | dspl = 0 | | dspl = 0 | | dspl = 0 | | dspl = 1 | | dspl = 1 | | dspl = 1 |
>>> | | | | | | | | | | | |
>>> +----------+ +----------+ +----------+ +----------+ +----------+ +----------+
>>>
>>> Reading the per-SNC node monitor values looks just the same as the
>>> non-SNC case. The struct rmid_read passed across the smp_call*() has
>>> the resource, domain, event, and reading the counters is essentially
>>> unchanged.
>>>
>>> Reading a file to sum event counts for SNC nodes on socket 1 needs to
>>> find each of the "struct rdt_mon_domain" that are part of socket 1.
>>> I'm doing that with meta data in the file that says sum=1 (need to add
>>> up something) and domid=1 (the things to be added are those with
>>> display_id = 1). So the code reads:
>>>
>>> list_for_each_entry(d, &rr->r->mon_domains, hdr.list) {
>>> if (d->display_id == rr->d->display_id) {
>>> ... call stuff to read and sum for domain "d"
>>> }
>>> }
>>>
>>> The display_id is "the domain id of the scope at which (for which?)
>>> the events must be summed." in your text above.
>>
>> My point remains that it is not clear (to me) why it is required to
>> carry the display_id around.
>>
>> list_for_each_entry(d, &rr->r->mon_domains, hdr.list) {
>> /* determine @id of @d at rr->r->mon_display_scope */
>> if (id == domid) {
>> ... call stuff to read and sum for domain "d"
>> }
>> }
>
> That "determine @id of @d at rr->r->mon_display_scope" is:
>
> display_id = get_domain_id_from_scope(cpumask_first(rr->d->hdr.cpu_mask), rr->r->mon_display_scope);
> if (display_id < 0) {
> take some error action
> }
>
> So it certainly isn't *required* to carry display_id around. But doing
> so makes the code simpler. I could bury the long line into a helper

Is "if (d->display_id == rr->d->display_id)" really "simpler"? It is
shorter I agree, but I would argue that it is much harder to understand
what the code is trying to do. The reader needs to understand what
"display_id" means, how the state is maintained, how
the values propagated to this call site, etc. With a query like above
it should be obvious what the code does.

> macro/function. But I can't bury the error check.

If this is an error then it is a kernel bug and should be handled
appropriately.

>
> I'd also need to change get_domain_id_from_scope() from "static" to
> global so it can be used in other files besides core.c

Is this a problem?

> Note that there are several places where I need to use display_id,
> computing it at run time in each place, but it seems so much easier to
> do it once at domain creation time.

Easier to code perhaps but I do not see how it is "easy" to understand
and maintain.

I think we have now repeated the same conversation twice. Previously you
promised that your design would be clear to me in the next version and
I have already stated twice that I am ok with that.

Reinette