Skip to contentSkip to conversation

x86/resctrl: Move the resctrl filesystem code to /fs/resctrl

PATCH Superseded James Morse 10 participants 149 messages Feb 28, 2025 – Apr 4, 2025 1 message not in the archive Page 1 of 2

Subsystems (4)

v7 of 12 Superseded Latest is v12 →

lore
Share & tools
lore

James Morse

author reviewer
#0 lore raw

v7 cover x86/resctrl: Move the resctrl filesystem code to /fs/resctrl

Changes since v6?:
* All the excitement is in patch 37, turns out there are two rmdir() paths that don't join up.
The last eight patches are new:
* The python script has been replaced with the patch that it generates, see the bare branch below if you want to regenerate it.
* There have been comments on the followup to the generated patch, those are included here - I suggest they be squashed into the generated patch.
* This version includes some checkpatch linting from Dave.

---
This series renames functions and moves code around. With the exception of invalid configurations for the configurable-events, there should be no changes in behaviour caused by this series. It is now possible for throttle_mode to report 'undefined', but no known platform will do this.

The driving pattern is to make things like struct rdtgroup private to resctrl. Features like pseudo-lock aren't going to work on arm64, the ability to disable it at compile time is added.

After this, I can start posting the MPAM driver to make use of resctrl on arm64. (What's MPAM? See the cover letter of the first series. [1])

This series is based on v6.14-rc3 and can be retrieved from:
https://git.kernel.org/pub/scm/linux/kernel/git/morse/linux.git mpam/move_to_fs/v7 or for those who want to regnerate the patch that moves all the code:
https://git.kernel.org/pub/scm/linux/kernel/git/morse/linux.git mpam/move_to_fs/v7_bare

As ever - bugs welcome,
Thanks,

James

[v6] https://lore.kernel.org/lkml/[email protected]/
[v5] https://lore.kernel.org/r/[email protected]
[v4] https://lore.kernel.org/all/[email protected]/
[v3] https://lore.kernel.org/r/[email protected]
[v2] https://lore.kernel.org/r/[email protected]
[v1] https://lore.kernel.org/r/[email protected]
[1] https://lore.kernel.org/lkml/[email protected]/

Amit Singh Tomar (1):
x86/resctrl: Remove the limit on the number of CLOSID

Dave Martin (3):
x86/resctrl: Squelch whitespace anomalies in resctrl core code x86/resctrl: Prefer alloc(sizeof(*foo)) idiom in rdt_init_fs_context() x86/resctrl: Relax some asm #includes

James Morse (45):
x86/resctrl: Fix allocation of cleanest CLOSID on platforms with no
monitors
x86/resctrl: Add a helper to avoid reaching into the arch code
resource list
x86/resctrl: Remove fflags from struct rdt_resource x86/resctrl: Use schema type to determine how to parse schema values x86/resctrl: Use schema type to determine the schema format string x86/resctrl: Remove data_width and the tabular format x86/resctrl: Add max_bw to struct resctrl_membw x86/resctrl: Generate default_ctrl instead of sharing it x86/resctrl: Add helper for setting CPU default properties x86/resctrl: Remove rdtgroup from update_cpu_closid_rmid() x86/resctrl: Expose resctrl fs's init function to the rest of the
kernel
x86/resctrl: Move rdt_find_domain() to be visible to arch and fs code x86/resctrl: Move resctrl types to a separate header x86/resctrl: Add an arch helper to reset one resource x86/resctrl: Move monitor exit work to a resctrl exit call x86/resctrl: Move monitor init work to a resctrl init call x86/resctrl: Rewrite and move the for_each_*_rdt_resource() walkers x86/resctrl: Move the is_mbm_*_enabled() helpers to asm/resctrl.h x86/resctrl: Add resctrl_arch_is_evt_configurable() to abstract BMEC x86/resctrl: Change mon_event_config_{read,write}() to be arch helpers x86/resctrl: Move mba_mbps_default_event init to filesystem code x86/resctrl: Move mbm_cfg_mask to struct rdt_resource x86/resctrl: Add resctrl_arch_ prefix to pseudo lock functions x86/resctrl: Allow an architecture to disable pseudo lock x86/resctrl: Make prefetch_disable_bits belong to the arch code x86/resctrl: Make resctrl_arch_pseudo_lock_fn() take a plr x86/resctrl: Move RFTYPE flags to be managed by resctrl x86/resctrl: Handle throttle_mode for SMBA resources x86/resctrl: Move get_config_index() to a header x86/resctrl: Move get_{mon,ctrl}_domain_from_cpu() to live with their
callers
x86/resctrl: Rename resctrl_sched_in() to begin with "resctrl_arch_" x86/resctrl: resctrl_exit() teardown resctrl but leave the mount point x86/resctrl: Drop __init/__exit on assorted symbols x86/resctrl: Move is_mba_sc() out of core.c
x86/resctrl: Add end-marker to the resctrl_event_id enum x86/resctrl: Expand the width of dom_id by replacing mon_data_bits x86/resctrl: Remove a newline to avoid confusing the code move script x86/resctrl: Split trace.h
fs/resctrl: Add boiler plate for external resctrl code x86/resctrl: Move the filesystem bits to headers visible to fs/resctrl x86,fs/resctrl: Move the resctrl filesystem code to live in
/fs/resctrl
x86,fs/resctrl: Remove duplicated trace header files fs/resctrl: Remove unnecessary includes
fs/resctrl: Change internal.h's header guard macros x86,fs/resctrl: Move resctrl.rst to live under
Documentation/filesystems

 Documentation/arch/x86/index.rst              |    1 -
 Documentation/filesystems/index.rst           |    1 +
 .../{arch/x86 => filesystems}/resctrl.rst     |    0
 MAINTAINERS                                   |    4 +-
 arch/Kconfig                                  |    8 +
 arch/x86/Kconfig                              |    6 +-
 arch/x86/include/asm/resctrl.h                |   43 +-
 arch/x86/kernel/cpu/resctrl/Makefile          |    8 +-
 arch/x86/kernel/cpu/resctrl/core.c            |  214 +-
 arch/x86/kernel/cpu/resctrl/ctrlmondata.c     |  606 +--
 arch/x86/kernel/cpu/resctrl/internal.h        |  504 +-
 arch/x86/kernel/cpu/resctrl/monitor.c         |  863 +---
 arch/x86/kernel/cpu/resctrl/pseudo_lock.c     | 1120 +----
 .../resctrl/{trace.h => pseudo_lock_trace.h}  |   26 +-
 arch/x86/kernel/cpu/resctrl/rdtgroup.c        | 4031 +--------------
 arch/x86/kernel/process_32.c                  |    2 +-
 arch/x86/kernel/process_64.c                  |    2 +-
 fs/Kconfig                                    |    1 +
 fs/Makefile                                   |    1 +
 fs/resctrl/Kconfig                            |   39 +
 fs/resctrl/Makefile                           |    6 +
 fs/resctrl/ctrlmondata.c                      |  660 +++
 fs/resctrl/internal.h                         |  435 ++
 fs/resctrl/monitor.c                          |  933 ++++
 fs/resctrl/monitor_trace.h                    |   33 +
 fs/resctrl/pseudo_lock.c                      | 1104 +++++
 fs/resctrl/rdtgroup.c                         | 4329 +++++++++++++++++
 include/linux/resctrl.h                       |  216 +-
 include/linux/resctrl_types.h                 |   59 +
 29 files changed, 7992 insertions(+), 7263 deletions(-)
 rename Documentation/{arch/x86 => filesystems}/resctrl.rst (100%)
 rename arch/x86/kernel/cpu/resctrl/{trace.h => pseudo_lock_trace.h} (56%)
 create mode 100644 fs/resctrl/Kconfig
 create mode 100644 fs/resctrl/Makefile
 create mode 100644 fs/resctrl/ctrlmondata.c
 create mode 100644 fs/resctrl/internal.h
 create mode 100644 fs/resctrl/monitor.c
 create mode 100644 fs/resctrl/monitor_trace.h
 create mode 100644 fs/resctrl/pseudo_lock.c
 create mode 100644 fs/resctrl/rdtgroup.c
 create mode 100644 include/linux/resctrl_types.h
Signature · 1 line
2.39.5
author reviewer
#1 lore raw

PATCH v7 1/49 x86/resctrl: Fix allocation of cleanest CLOSID on platforms with no monitors

commit 6eac36bb9eb0 ("x86/resctrl: Allocate the cleanest CLOSID by searching closid_num_dirty_rmid") added logic that causes resctrl to search for the CLOSID with the fewest dirty cache lines when creating a new control group, if requested by the arch code. This depends on the values read from the llc_occupancy counters. The logic is applicable to architectures where the CLOSID effectively forms part of the monitoring identifier and so do not allow complete freedom to choose an unused monitoring identifier for a given CLOSID.

This support missed that some platforms may not have these counters. This causes a NULL pointer dereference when creating a new control group as the array was not allocated by dom_data_init().

As this feature isn't necessary on platforms that don't have cache occupancy monitors, add this to the check that occurs when a new control group is allocated.

Fixes 6eac36bb9eb0 ("x86/resctrl: Allocate the cleanest CLOSID by searching closid_num_dirty_rmid")

---
The existing code is not selected by any upstream platform, it makes no sense to backport this patch to stable.

Changes since v1:
* [Commit message only] Reword the first paragraph to make it clear that the issue being fixed wasn't directly associated with addition of a Kconfig option. (Actually, the option is not in Kconfig yet, and gets added later in this series.)

---
 arch/x86/kernel/cpu/resctrl/rdtgroup.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/resctrl/rdtgroup.c +2 -1
index 6419e04d8a7b..04b653d613e8 100644
@@ -157,7 +157,8 @@ static int closid_alloc(void)
 
 	lockdep_assert_held(&rdtgroup_mutex);
 
-	if (IS_ENABLED(CONFIG_RESCTRL_RMID_DEPENDS_ON_CLOSID)) {
+	if (IS_ENABLED(CONFIG_RESCTRL_RMID_DEPENDS_ON_CLOSID) &&
+	    is_llc_occupancy_enabled()) {
 		cleanest_closid = resctrl_find_cleanest_closid();
 		if (cleanest_closid < 0)
 			return cleanest_closid;
Signature · 1 line
2.39.5
Reviewed-by on v7 1/49
#2 lore raw

Re: [PATCH v7 01/49] x86/resctrl: Fix allocation of cleanest CLOSID on platforms with no monitors

Quoted 17 lines · James Morse

commit 6eac36bb9eb0 ("x86/resctrl: Allocate the cleanest CLOSID by searching closid_num_dirty_rmid") added logic that causes resctrl to search for the CLOSID with the fewest dirty cache lines when creating a new control group, if requested by the arch code. This depends on the values read from the llc_occupancy counters. The logic is applicable to architectures where the CLOSID effectively forms part of the monitoring identifier and so do not allow complete freedom to choose an unused monitoring identifier for a given CLOSID.

This support missed that some platforms may not have these counters. This causes a NULL pointer dereference when creating a new control group as the array was not allocated by dom_data_init().

As this feature isn't necessary on platforms that don't have cache occupancy monitors, add this to the check that occurs when a new control group is allocated.

Quoted 8 lines
Fixes 6eac36bb9eb0 ("x86/resctrl: Allocate the cleanest CLOSID by searching closid_num_dirty_rmid")

Thanks.

-Fenghua

author reviewer
#3 lore raw

PATCH v7 2/49 x86/resctrl: Add a helper to avoid reaching into the arch code resource list

In reply to James Morse

Resctrl occasionally wants to know something about a specific resource, in these cases it reaches into the arch code's rdt_resources_all[] array.

Once the filesystem parts of resctrl are moved to /fs/, this means it will need visibility of the architecture specific struct rdt_hw_resource definition, and the array of all resources. All architectures would also need a r_resctrl member in this struct.

Instead, abstract this via a helper to allow architectures to do different things here. Move the level enum to the resctrl header and add a helper to retrieve the struct rdt_resource by 'rid'.

resctrl_arch_get_resource() should not return NULL for any value in the enum, it may instead return a dummy resource that is !alloc_enabled && !mon_enabled.

---
Change since v5:
* Ensure rdt_resources_all[] is always padded to RDT_NUM_RESOURCES

Changes since v1:
* Backed out non-functional renaming of "r" to "l3" in rdt_get_tree(), and unhoisted the assignment of r (as now is) back into the if () where it started out. There seem to be no uses of this variable outside this if().
* [Commit message only] Typo fix:
s/resctrl_hw_resource/rdt_hw_resource/g

---
 arch/x86/kernel/cpu/resctrl/core.c        | 12 ++++++++++--
 arch/x86/kernel/cpu/resctrl/ctrlmondata.c |  2 +-
 arch/x86/kernel/cpu/resctrl/internal.h    | 10 ----------
 arch/x86/kernel/cpu/resctrl/monitor.c     |  8 ++++----
 arch/x86/kernel/cpu/resctrl/rdtgroup.c    | 14 +++++++-------
 include/linux/resctrl.h                   | 17 +++++++++++++++++
 6 files changed, 39 insertions(+), 24 deletions(-)
arch/x86/kernel/cpu/resctrl/core.c +10 -2
index 3d1735ed8d1f..12b41316d8f7 100644
@@ -62,7 +62,7 @@ static void mba_wrmsr_amd(struct msr_param *m);
 #define ctrl_domain_init(id) LIST_HEAD_INIT(rdt_resources_all[id].r_resctrl.ctrl_domains)
 #define mon_domain_init(id) LIST_HEAD_INIT(rdt_resources_all[id].r_resctrl.mon_domains)
 
-struct rdt_hw_resource rdt_resources_all[] = {
+struct rdt_hw_resource rdt_resources_all[RDT_NUM_RESOURCES] = {
 	[RDT_RESOURCE_L3] =
 	{
 		.r_resctrl = {
@@ -127,6 +127,14 @@ u32 resctrl_arch_system_num_rmid_idx(void)
 	return r->num_rmid;
 }
 
+struct rdt_resource *resctrl_arch_get_resource(enum resctrl_res_level l)
+{
+	if (l >= RDT_NUM_RESOURCES)
+		return NULL;
+
+	return &rdt_resources_all[l].r_resctrl;
+}
+
 /*
  * cache_alloc_hsw_probe() - Have to probe for Intel haswell server CPUs
  * as they do not have CPUID enumeration support for Cache allocation.
@@ -174,7 +182,7 @@ static inline void cache_alloc_hsw_probe(void)
 bool is_mba_sc(struct rdt_resource *r)
 {
 	if (!r)
-		return rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl.membw.mba_sc;
+		r = resctrl_arch_get_resource(RDT_RESOURCE_MBA);
 
 	/*
 	 * The software controller support is only applicable to MBA resource.
arch/x86/kernel/cpu/resctrl/ctrlmondata.c +1 -1
index 536351159cc2..4af27ef5a8a1 100644
@@ -649,7 +649,7 @@ int rdtgroup_mondata_show(struct seq_file *m, void *arg)
 	resid = md.u.rid;
 	domid = md.u.domid;
 	evtid = md.u.evtid;
-	r = &rdt_resources_all[resid].r_resctrl;
+	r = resctrl_arch_get_resource(resid);
 
 	if (md.u.sum) {
 		/*
arch/x86/kernel/cpu/resctrl/internal.h +0 -10
index 20c898f09b7e..75252a7e1ebc 100644
@@ -512,16 +512,6 @@ extern struct rdtgroup rdtgroup_default;
 extern struct dentry *debugfs_resctrl;
 extern enum resctrl_event_id mba_mbps_default_event;
 
-enum resctrl_res_level {
-	RDT_RESOURCE_L3,
-	RDT_RESOURCE_L2,
-	RDT_RESOURCE_MBA,
-	RDT_RESOURCE_SMBA,
-
-	/* Must be the last */
-	RDT_NUM_RESOURCES,
-};
-
 static inline struct rdt_resource *resctrl_inc(struct rdt_resource *res)
 {
 	struct rdt_hw_resource *hw_res = resctrl_to_arch_res(res);
arch/x86/kernel/cpu/resctrl/monitor.c +4 -4
index 94a1d9780461..58b5b21349a8 100644
@@ -365,7 +365,7 @@ static void limbo_release_entry(struct rmid_entry *entry)
  */
 void __check_limbo(struct rdt_mon_domain *d, bool force_free)
 {
-	struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
+	struct rdt_resource *r = resctrl_arch_get_resource(RDT_RESOURCE_L3);
 	u32 idx_limit = resctrl_arch_system_num_rmid_idx();
 	struct rmid_entry *entry;
 	u32 idx, cur_idx = 1;
@@ -521,7 +521,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_resource *r = resctrl_arch_get_resource(RDT_RESOURCE_L3);
 	struct rdt_mon_domain *d;
 	u32 idx;
 
@@ -761,7 +761,7 @@ static void update_mba_bw(struct rdtgroup *rgrp, struct rdt_mon_domain *dom_mbm)
 	struct rdtgroup *entry;
 	u32 cur_bw, user_bw;
 
-	r_mba = &rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl;
+	r_mba = resctrl_arch_get_resource(RDT_RESOURCE_MBA);
 	evt_id = rgrp->mba_mbps_event;
 
 	closid = rgrp->closid;
@@ -925,7 +925,7 @@ void mbm_handle_overflow(struct work_struct *work)
 	if (!resctrl_mounted || !resctrl_arch_mon_capable())
 		goto out_unlock;
 
-	r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
+	r = resctrl_arch_get_resource(RDT_RESOURCE_L3);
 	d = container_of(work, struct rdt_mon_domain, mbm_over.work);
 
 	list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
arch/x86/kernel/cpu/resctrl/rdtgroup.c +7 -7
index 04b653d613e8..45093b9e8e63 100644
@@ -2256,7 +2256,7 @@ static void l2_qos_cfg_update(void *arg)
 
 static inline bool is_mba_linear(void)
 {
-	return rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl.membw.delay_linear;
+	return resctrl_arch_get_resource(RDT_RESOURCE_MBA)->membw.delay_linear;
 }
 
 static int set_cache_qos_cfg(int level, bool enable)
@@ -2346,8 +2346,8 @@ static void mba_sc_domain_destroy(struct rdt_resource *r,
  */
 static bool supports_mba_mbps(void)
 {
-	struct rdt_resource *rmbm = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
-	struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl;
+	struct rdt_resource *rmbm = resctrl_arch_get_resource(RDT_RESOURCE_L3);
+	struct rdt_resource *r = resctrl_arch_get_resource(RDT_RESOURCE_MBA);
 
 	return (is_mbm_enabled() &&
 		r->alloc_capable && is_mba_linear() &&
@@ -2360,7 +2360,7 @@ static bool supports_mba_mbps(void)
  */
 static int set_mba_sc(bool mba_sc)
 {
-	struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl;
+	struct rdt_resource *r = resctrl_arch_get_resource(RDT_RESOURCE_MBA);
 	u32 num_closid = resctrl_arch_get_num_closid(r);
 	struct rdt_ctrl_domain *d;
 	unsigned long fflags;
@@ -2714,7 +2714,7 @@ static int rdt_get_tree(struct fs_context *fc)
 		resctrl_mounted = true;
 
 	if (is_mbm_enabled()) {
-		r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
+		r = resctrl_arch_get_resource(RDT_RESOURCE_L3);
 		list_for_each_entry(dom, &r->mon_domains, hdr.list)
 			mbm_setup_overflow_handler(dom, MBM_OVERFLOW_INTERVAL,
 						   RESCTRL_PICK_ANY_CPU);
@@ -3951,7 +3951,7 @@ static int rdtgroup_show_options(struct seq_file *seq, struct kernfs_root *kf)
 	if (resctrl_arch_get_cdp_enabled(RDT_RESOURCE_L2))
 		seq_puts(seq, ",cdpl2");
 
-	if (is_mba_sc(&rdt_resources_all[RDT_RESOURCE_MBA].r_resctrl))
+	if (is_mba_sc(resctrl_arch_get_resource(RDT_RESOURCE_MBA)))
 		seq_puts(seq, ",mba_MBps");
 
 	if (resctrl_debug)
@@ -4151,7 +4151,7 @@ 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_resource *l3 = resctrl_arch_get_resource(RDT_RESOURCE_L3);
 	struct rdt_mon_domain *d;
 	struct rdtgroup *rdtgrp;
 
include/linux/resctrl.h +17 -0
index d94abba1c716..37279e2a89da 100644
@@ -37,6 +37,16 @@ enum resctrl_conf_type {
 	CDP_DATA,
 };
 
+enum resctrl_res_level {
+	RDT_RESOURCE_L3,
+	RDT_RESOURCE_L2,
+	RDT_RESOURCE_MBA,
+	RDT_RESOURCE_SMBA,
+
+	/* Must be the last */
+	RDT_NUM_RESOURCES,
+};
+
 #define CDP_NUM_TYPES	(CDP_DATA + 1)
 
 /*
@@ -226,6 +236,13 @@ struct rdt_resource {
 	bool			cdp_capable;
 };
 
+/*
+ * Get the resource that exists at this level. If the level is not supported
+ * a dummy/not-capable resource can be returned. Levels >= RDT_NUM_RESOURCES
+ * will return NULL.
+ */
+struct rdt_resource *resctrl_arch_get_resource(enum resctrl_res_level l);
+
 /**
  * struct resctrl_schema - configuration abilities of a resource presented to
  *			   user-space
Signature · 1 line
2.39.5
Reviewed-by on v7 2/49
#4 lore raw

Re: [PATCH v7 02/49] x86/resctrl: Add a helper to avoid reaching into the arch code resource list

Quoted 17 lines · James Morse

Resctrl occasionally wants to know something about a specific resource, in these cases it reaches into the arch code's rdt_resources_all[] array.

Once the filesystem parts of resctrl are moved to /fs/, this means it will need visibility of the architecture specific struct rdt_hw_resource definition, and the array of all resources. All architectures would also need a r_resctrl member in this struct.

Instead, abstract this via a helper to allow architectures to do different things here. Move the level enum to the resctrl header and add a helper to retrieve the struct rdt_resource by 'rid'.

resctrl_arch_get_resource() should not return NULL for any value in the enum, it may instead return a dummy resource that is !alloc_enabled && !mon_enabled.

Quoted 9 lines

Thanks.

-Fenghua

author reviewer
#5 lore raw

PATCH v7 3/49 x86/resctrl: Remove fflags from struct rdt_resource

In reply to James Morse

The resctrl arch code specifies whether a resource controls a cache or memory using the fflags field. This field is then used by resctrl to determine which files should be exposed in the filesystem.

Allowing the architecture to pick this value means the RFTYPE_ flags have to be in a shared header, and allows an architecture to create a combination that resctrl does not support.

Remove the fflags field, and pick the value based on the resource id.

---
Changes since v5:
* Made fflags_from_resource() return an unsigned long.
* Removed a space.

Changes since v4:
* Removed an extra space
* Fixed a typo

---
 arch/x86/kernel/cpu/resctrl/core.c     |  4 ----
 arch/x86/kernel/cpu/resctrl/rdtgroup.c | 18 ++++++++++++++++--
 include/linux/resctrl.h                |  2 --
 3 files changed, 16 insertions(+), 8 deletions(-)
arch/x86/kernel/cpu/resctrl/core.c +0 -4
index 12b41316d8f7..8ef2e449b465 100644
@@ -74,7 +74,6 @@ struct rdt_hw_resource rdt_resources_all[RDT_NUM_RESOURCES] = {
 			.mon_domains		= mon_domain_init(RDT_RESOURCE_L3),
 			.parse_ctrlval		= parse_cbm,
 			.format_str		= "%d=%0*x",
-			.fflags			= RFTYPE_RES_CACHE,
 		},
 		.msr_base		= MSR_IA32_L3_CBM_BASE,
 		.msr_update		= cat_wrmsr,
@@ -88,7 +87,6 @@ struct rdt_hw_resource rdt_resources_all[RDT_NUM_RESOURCES] = {
 			.ctrl_domains		= ctrl_domain_init(RDT_RESOURCE_L2),
 			.parse_ctrlval		= parse_cbm,
 			.format_str		= "%d=%0*x",
-			.fflags			= RFTYPE_RES_CACHE,
 		},
 		.msr_base		= MSR_IA32_L2_CBM_BASE,
 		.msr_update		= cat_wrmsr,
@@ -102,7 +100,6 @@ struct rdt_hw_resource rdt_resources_all[RDT_NUM_RESOURCES] = {
 			.ctrl_domains		= ctrl_domain_init(RDT_RESOURCE_MBA),
 			.parse_ctrlval		= parse_bw,
 			.format_str		= "%d=%*u",
-			.fflags			= RFTYPE_RES_MB,
 		},
 	},
 	[RDT_RESOURCE_SMBA] =
@@ -114,7 +111,6 @@ struct rdt_hw_resource rdt_resources_all[RDT_NUM_RESOURCES] = {
 			.ctrl_domains		= ctrl_domain_init(RDT_RESOURCE_SMBA),
 			.parse_ctrlval		= parse_bw,
 			.format_str		= "%d=%*u",
-			.fflags			= RFTYPE_RES_MB,
 		},
 	},
 };
arch/x86/kernel/cpu/resctrl/rdtgroup.c +16 -2
index 45093b9e8e63..3391ac8ecb2d 100644
@@ -2165,6 +2165,20 @@ static int rdtgroup_mkdir_info_resdir(void *priv, char *name,
 	return ret;
 }
 
+static unsigned long fflags_from_resource(struct rdt_resource *r)
+{
+	switch (r->rid) {
+	case RDT_RESOURCE_L3:
+	case RDT_RESOURCE_L2:
+		return RFTYPE_RES_CACHE;
+	case RDT_RESOURCE_MBA:
+	case RDT_RESOURCE_SMBA:
+		return RFTYPE_RES_MB;
+	}
+
+	return WARN_ON_ONCE(1);
+}
+
 static int rdtgroup_create_info_dir(struct kernfs_node *parent_kn)
 {
 	struct resctrl_schema *s;
@@ -2185,14 +2199,14 @@ static int rdtgroup_create_info_dir(struct kernfs_node *parent_kn)
 	/* loop over enabled controls, these are all alloc_capable */
 	list_for_each_entry(s, &resctrl_schema_all, list) {
 		r = s->res;
-		fflags = r->fflags | RFTYPE_CTRL_INFO;
+		fflags = fflags_from_resource(r) | RFTYPE_CTRL_INFO;
 		ret = rdtgroup_mkdir_info_resdir(s, s->name, fflags);
 		if (ret)
 			goto out_destroy;
 	}
 
 	for_each_mon_capable_rdt_resource(r) {
-		fflags = r->fflags | RFTYPE_MON_INFO;
+		fflags = fflags_from_resource(r) | RFTYPE_MON_INFO;
 		sprintf(name, "%s_MON", r->name);
 		ret = rdtgroup_mkdir_info_resdir(r, name, fflags);
 		if (ret)
include/linux/resctrl.h +0 -2
index 37279e2a89da..496ddcaa4ecf 100644
@@ -210,7 +210,6 @@ enum resctrl_scope {
  * @format_str:		Per resource format string to show domain value
  * @parse_ctrlval:	Per resource function pointer to parse control values
  * @evt_list:		List of monitoring events
- * @fflags:		flags to choose base and info files
  * @cdp_capable:	Is the CDP feature available on this resource
  */
 struct rdt_resource {
@@ -232,7 +231,6 @@ struct rdt_resource {
 						 struct resctrl_schema *s,
 						 struct rdt_ctrl_domain *d);
 	struct list_head	evt_list;
-	unsigned long		fflags;
 	bool			cdp_capable;
 };
 
Signature · 1 line
2.39.5
Reviewed-by on v7 3/49
#6 lore raw

Re: [PATCH v7 03/49] x86/resctrl: Remove fflags from struct rdt_resource

Quoted 8 lines · James Morse

The resctrl arch code specifies whether a resource controls a cache or memory using the fflags field. This field is then used by resctrl to determine which files should be exposed in the filesystem.

Allowing the architecture to pick this value means the RFTYPE_ flags have to be in a shared header, and allows an architecture to create a combination that resctrl does not support.

Quoted 8 lines

Remove the fflags field, and pick the value based on the resource id.

Thanks.

-Fenghua

author reviewer
#7 lore raw

PATCH v7 4/49 x86/resctrl: Use schema type to determine how to parse schema values

In reply to James Morse

Resctrl's architecture code gets to specify a function pointer that is used when parsing schema entries. This is expected to be one of two helpers from the filesystem code.

Setting this function pointer allows the architecture code to change the ABI resctrl presents to user-space, and forces resctrl to expose these helpers.

Instead, add a schema format enum to choose which schema parser to use. This allows the helpers to be made static and the structs used for passing arguments moved out of shared headers.

---
Change since v6:
* Added a full stop.

Changes since v5:
* Merged the contents of get_parser() with its only caller.
* Removed the description of what the range schema is used for.
* Waggled some whitespace.

Changes since v4:
* Creation of the enum moves into this patch - review tags not picked up.
* Removed some whitespace.

Changes since v3:
* Removed a spurious semicolon

Changes since v2:
* This patch is new

---
 arch/x86/kernel/cpu/resctrl/core.c        |  8 +++---
 arch/x86/kernel/cpu/resctrl/ctrlmondata.c | 32 +++++++++++++++++++----
 arch/x86/kernel/cpu/resctrl/internal.h    | 10 -------
 include/linux/resctrl.h                   | 17 ++++++++----
 4 files changed, 43 insertions(+), 24 deletions(-)
arch/x86/kernel/cpu/resctrl/core.c +4 -4
index 8ef2e449b465..e9fe129a02f8 100644
@@ -72,7 +72,7 @@ struct rdt_hw_resource rdt_resources_all[RDT_NUM_RESOURCES] = {
 			.mon_scope		= RESCTRL_L3_CACHE,
 			.ctrl_domains		= ctrl_domain_init(RDT_RESOURCE_L3),
 			.mon_domains		= mon_domain_init(RDT_RESOURCE_L3),
-			.parse_ctrlval		= parse_cbm,
+			.schema_fmt		= RESCTRL_SCHEMA_BITMAP,
 			.format_str		= "%d=%0*x",
 		},
 		.msr_base		= MSR_IA32_L3_CBM_BASE,
@@ -85,7 +85,7 @@ struct rdt_hw_resource rdt_resources_all[RDT_NUM_RESOURCES] = {
 			.name			= "L2",
 			.ctrl_scope		= RESCTRL_L2_CACHE,
 			.ctrl_domains		= ctrl_domain_init(RDT_RESOURCE_L2),
-			.parse_ctrlval		= parse_cbm,
+			.schema_fmt		= RESCTRL_SCHEMA_BITMAP,
 			.format_str		= "%d=%0*x",
 		},
 		.msr_base		= MSR_IA32_L2_CBM_BASE,
@@ -98,7 +98,7 @@ struct rdt_hw_resource rdt_resources_all[RDT_NUM_RESOURCES] = {
 			.name			= "MB",
 			.ctrl_scope		= RESCTRL_L3_CACHE,
 			.ctrl_domains		= ctrl_domain_init(RDT_RESOURCE_MBA),
-			.parse_ctrlval		= parse_bw,
+			.schema_fmt		= RESCTRL_SCHEMA_RANGE,
 			.format_str		= "%d=%*u",
 		},
 	},
@@ -109,7 +109,7 @@ struct rdt_hw_resource rdt_resources_all[RDT_NUM_RESOURCES] = {
 			.name			= "SMBA",
 			.ctrl_scope		= RESCTRL_L3_CACHE,
 			.ctrl_domains		= ctrl_domain_init(RDT_RESOURCE_SMBA),
-			.parse_ctrlval		= parse_bw,
+			.schema_fmt		= RESCTRL_SCHEMA_RANGE,
 			.format_str		= "%d=%*u",
 		},
 	},
arch/x86/kernel/cpu/resctrl/ctrlmondata.c +27 -5
index 4af27ef5a8a1..f4334f437ffc 100644
@@ -23,6 +23,15 @@
 
 #include "internal.h"
 
+struct rdt_parse_data {
+	struct rdtgroup		*rdtgrp;
+	char			*buf;
+};
+
+typedef int (ctrlval_parser_t)(struct rdt_parse_data *data,
+			       struct resctrl_schema *s,
+			       struct rdt_ctrl_domain *d);
+
 /*
  * Check whether MBA bandwidth percentage value is correct. The value is
  * checked against the minimum and max bandwidth values specified by the
@@ -64,8 +73,8 @@ static bool bw_validate(char *buf, u32 *data, struct rdt_resource *r)
 	return true;
 }
 
-int parse_bw(struct rdt_parse_data *data, struct resctrl_schema *s,
-	     struct rdt_ctrl_domain *d)
+static int parse_bw(struct rdt_parse_data *data, struct resctrl_schema *s,
+		    struct rdt_ctrl_domain *d)
 {
 	struct resctrl_staged_config *cfg;
 	u32 closid = data->rdtgrp->closid;
@@ -143,8 +152,8 @@ static bool cbm_validate(char *buf, u32 *data, struct rdt_resource *r)
  * Read one cache bit mask (hex). Check that it is valid for the current
  * resource type.
  */
-int parse_cbm(struct rdt_parse_data *data, struct resctrl_schema *s,
-	      struct rdt_ctrl_domain *d)
+static int parse_cbm(struct rdt_parse_data *data, struct resctrl_schema *s,
+		     struct rdt_ctrl_domain *d)
 {
 	struct rdtgroup *rdtgrp = data->rdtgrp;
 	struct resctrl_staged_config *cfg;
@@ -210,6 +219,7 @@ static int parse_line(char *line, struct resctrl_schema *s,
 		      struct rdtgroup *rdtgrp)
 {
 	enum resctrl_conf_type t = s->conf_type;
+	ctrlval_parser_t *parse_ctrlval = NULL;
 	struct resctrl_staged_config *cfg;
 	struct rdt_resource *r = s->res;
 	struct rdt_parse_data data;
@@ -220,6 +230,18 @@ static int parse_line(char *line, struct resctrl_schema *s,
 	/* Walking r->domains, ensure it can't race with cpuhp */
 	lockdep_assert_cpus_held();
 
+	switch (r->schema_fmt) {
+	case RESCTRL_SCHEMA_BITMAP:
+		parse_ctrlval = &parse_cbm;
+		break;
+	case RESCTRL_SCHEMA_RANGE:
+		parse_ctrlval = &parse_bw;
+		break;
+	}
+
+	if (WARN_ON_ONCE(!parse_ctrlval))
+		return -EINVAL;
+
 	if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP &&
 	    (r->rid == RDT_RESOURCE_MBA || r->rid == RDT_RESOURCE_SMBA)) {
 		rdt_last_cmd_puts("Cannot pseudo-lock MBA resource\n");
@@ -240,7 +262,7 @@ static int parse_line(char *line, struct resctrl_schema *s,
 		if (d->hdr.id == dom_id) {
 			data.buf = dom;
 			data.rdtgrp = rdtgrp;
-			if (r->parse_ctrlval(&data, s, d))
+			if (parse_ctrlval(&data, s, d))
 				return -EINVAL;
 			if (rdtgrp->mode ==  RDT_MODE_PSEUDO_LOCKSETUP) {
 				cfg = &d->staged_config[t];
arch/x86/kernel/cpu/resctrl/internal.h +0 -10
index 75252a7e1ebc..b5543bd506c3 100644
@@ -459,11 +459,6 @@ static inline bool is_mbm_event(int e)
 		e <= QOS_L3_MBM_LOCAL_EVENT_ID);
 }
 
-struct rdt_parse_data {
-	struct rdtgroup		*rdtgrp;
-	char			*buf;
-};
-
 /**
  * struct rdt_hw_resource - arch private attributes of a resctrl resource
  * @r_resctrl:		Attributes of the resource used directly by resctrl.
@@ -500,11 +495,6 @@ static inline struct rdt_hw_resource *resctrl_to_arch_res(struct rdt_resource *r
 	return container_of(r, struct rdt_hw_resource, r_resctrl);
 }
 
-int parse_cbm(struct rdt_parse_data *data, struct resctrl_schema *s,
-	      struct rdt_ctrl_domain *d);
-int parse_bw(struct rdt_parse_data *data, struct resctrl_schema *s,
-	     struct rdt_ctrl_domain *d);
-
 extern struct mutex rdtgroup_mutex;
 
 extern struct rdt_hw_resource rdt_resources_all[];
include/linux/resctrl.h +12 -5
index 496ddcaa4ecf..3e1a41356b2a 100644
@@ -183,7 +183,6 @@ struct resctrl_membw {
 	u32				*mb_map;
 };
 
-struct rdt_parse_data;
 struct resctrl_schema;
 
 enum resctrl_scope {
@@ -192,6 +191,16 @@ enum resctrl_scope {
 	RESCTRL_L3_NODE,
 };
 
+/**
+ * enum resctrl_schema_fmt - The format user-space provides for a schema.
+ * @RESCTRL_SCHEMA_BITMAP:	The schema is a bitmap in hex.
+ * @RESCTRL_SCHEMA_RANGE:	The schema is a decimal number.
+ */
+enum resctrl_schema_fmt {
+	RESCTRL_SCHEMA_BITMAP,
+	RESCTRL_SCHEMA_RANGE,
+};
+
 /**
  * struct rdt_resource - attributes of a resctrl resource
  * @rid:		The index of the resource
@@ -208,7 +217,7 @@ enum resctrl_scope {
  * @data_width:		Character width of data when displaying
  * @default_ctrl:	Specifies default cache cbm or memory B/W percent.
  * @format_str:		Per resource format string to show domain value
- * @parse_ctrlval:	Per resource function pointer to parse control values
+ * @schema_fmt:		Which format string and parser is used for this schema.
  * @evt_list:		List of monitoring events
  * @cdp_capable:	Is the CDP feature available on this resource
  */
@@ -227,9 +236,7 @@ struct rdt_resource {
 	int			data_width;
 	u32			default_ctrl;
 	const char		*format_str;
-	int			(*parse_ctrlval)(struct rdt_parse_data *data,
-						 struct resctrl_schema *s,
-						 struct rdt_ctrl_domain *d);
+	enum resctrl_schema_fmt	schema_fmt;
 	struct list_head	evt_list;
 	bool			cdp_capable;
 };
Signature · 1 line
2.39.5
Reviewed-by on v7 4/49
#8 lore raw

Re: [PATCH v7 04/49] x86/resctrl: Use schema type to determine how to parse schema values

Quoted 12 lines · James Morse

Resctrl's architecture code gets to specify a function pointer that is used when parsing schema entries. This is expected to be one of two helpers from the filesystem code.

Setting this function pointer allows the architecture code to change the ABI resctrl presents to user-space, and forces resctrl to expose these helpers.

Instead, add a schema format enum to choose which schema parser to use. This allows the helpers to be made static and the structs used for passing arguments moved out of shared headers.

Quoted 6 lines

Thanks.

-Fenghua

author reviewer
#9 lore raw

PATCH v7 5/49 x86/resctrl: Use schema type to determine the schema format string

In reply to James Morse

Resctrl's architecture code gets to specify a format string that is used when printing schema entries. This is expected to be one of two values that the filesystem code supports.

Setting this format string allows the architecture code to change the ABI resctrl presents to user-space.

Instead, use the schema format enum to choose which format string to use.

---
Changes since v5:
* Added runtime check for resource schema_format being junk.

Change since v4:
* Added a stop to a struct comment.

Changes since v2:
* This patch is new.

---
 arch/x86/kernel/cpu/resctrl/core.c        |  4 ----
 arch/x86/kernel/cpu/resctrl/ctrlmondata.c |  2 +-
 arch/x86/kernel/cpu/resctrl/rdtgroup.c    | 14 ++++++++++++++
 include/linux/resctrl.h                   |  4 ++--
 4 files changed, 17 insertions(+), 7 deletions(-)
arch/x86/kernel/cpu/resctrl/core.c +0 -4
index e9fe129a02f8..542503a8c953 100644
@@ -73,7 +73,6 @@ struct rdt_hw_resource rdt_resources_all[RDT_NUM_RESOURCES] = {
 			.ctrl_domains		= ctrl_domain_init(RDT_RESOURCE_L3),
 			.mon_domains		= mon_domain_init(RDT_RESOURCE_L3),
 			.schema_fmt		= RESCTRL_SCHEMA_BITMAP,
-			.format_str		= "%d=%0*x",
 		},
 		.msr_base		= MSR_IA32_L3_CBM_BASE,
 		.msr_update		= cat_wrmsr,
@@ -86,7 +85,6 @@ struct rdt_hw_resource rdt_resources_all[RDT_NUM_RESOURCES] = {
 			.ctrl_scope		= RESCTRL_L2_CACHE,
 			.ctrl_domains		= ctrl_domain_init(RDT_RESOURCE_L2),
 			.schema_fmt		= RESCTRL_SCHEMA_BITMAP,
-			.format_str		= "%d=%0*x",
 		},
 		.msr_base		= MSR_IA32_L2_CBM_BASE,
 		.msr_update		= cat_wrmsr,
@@ -99,7 +97,6 @@ struct rdt_hw_resource rdt_resources_all[RDT_NUM_RESOURCES] = {
 			.ctrl_scope		= RESCTRL_L3_CACHE,
 			.ctrl_domains		= ctrl_domain_init(RDT_RESOURCE_MBA),
 			.schema_fmt		= RESCTRL_SCHEMA_RANGE,
-			.format_str		= "%d=%*u",
 		},
 	},
 	[RDT_RESOURCE_SMBA] =
@@ -110,7 +107,6 @@ struct rdt_hw_resource rdt_resources_all[RDT_NUM_RESOURCES] = {
 			.ctrl_scope		= RESCTRL_L3_CACHE,
 			.ctrl_domains		= ctrl_domain_init(RDT_RESOURCE_SMBA),
 			.schema_fmt		= RESCTRL_SCHEMA_RANGE,
-			.format_str		= "%d=%*u",
 		},
 	},
 };
arch/x86/kernel/cpu/resctrl/ctrlmondata.c +1 -1
index f4334f437ffc..c763cb4fb1a8 100644
@@ -487,7 +487,7 @@ static void show_doms(struct seq_file *s, struct resctrl_schema *schema, int clo
 			ctrl_val = resctrl_arch_get_config(r, dom, closid,
 							   schema->conf_type);
 
-		seq_printf(s, r->format_str, dom->hdr.id, max_data_width,
+		seq_printf(s, schema->fmt_str, dom->hdr.id, max_data_width,
 			   ctrl_val);
 		sep = true;
 	}
arch/x86/kernel/cpu/resctrl/rdtgroup.c +14 -0
index 3391ac8ecb2d..e7862d0936c9 100644
@@ -2611,6 +2611,20 @@ static int schemata_list_add(struct rdt_resource *r, enum resctrl_conf_type type
 	if (cl > max_name_width)
 		max_name_width = cl;
 
+	switch (r->schema_fmt) {
+	case RESCTRL_SCHEMA_BITMAP:
+		s->fmt_str = "%d=%0*x";
+		break;
+	case RESCTRL_SCHEMA_RANGE:
+		s->fmt_str = "%d=%0*u";
+		break;
+	}
+
+	if (WARN_ON_ONCE(!s->fmt_str)) {
+		kfree(s);
+		return -EINVAL;
+	}
+
 	INIT_LIST_HEAD(&s->list);
 	list_add(&s->list, &resctrl_schema_all);
 
include/linux/resctrl.h +2 -2
index 3e1a41356b2a..736b9a9a9464 100644
@@ -216,7 +216,6 @@ enum resctrl_schema_fmt {
  * @name:		Name to use in "schemata" file.
  * @data_width:		Character width of data when displaying
  * @default_ctrl:	Specifies default cache cbm or memory B/W percent.
- * @format_str:		Per resource format string to show domain value
  * @schema_fmt:		Which format string and parser is used for this schema.
  * @evt_list:		List of monitoring events
  * @cdp_capable:	Is the CDP feature available on this resource
@@ -235,7 +234,6 @@ struct rdt_resource {
 	char			*name;
 	int			data_width;
 	u32			default_ctrl;
-	const char		*format_str;
 	enum resctrl_schema_fmt	schema_fmt;
 	struct list_head	evt_list;
 	bool			cdp_capable;
@@ -253,6 +251,7 @@ struct rdt_resource *resctrl_arch_get_resource(enum resctrl_res_level l);
  *			   user-space
  * @list:	Member of resctrl_schema_all.
  * @name:	The name to use in the "schemata" file.
+ * @fmt_str:	Format string to show domain value.
  * @conf_type:	Whether this schema is specific to code/data.
  * @res:	The resource structure exported by the architecture to describe
  *		the hardware that is configured by this schema.
@@ -263,6 +262,7 @@ struct rdt_resource *resctrl_arch_get_resource(enum resctrl_res_level l);
 struct resctrl_schema {
 	struct list_head		list;
 	char				name[8];
+	const char			*fmt_str;
 	enum resctrl_conf_type		conf_type;
 	struct rdt_resource		*res;
 	u32				num_closid;
Signature · 1 line
2.39.5
Reviewed-by on v7 5/49
#10 lore raw

Re: [PATCH v7 05/49] x86/resctrl: Use schema type to determine the schema format string

Quoted 10 lines · James Morse

Resctrl's architecture code gets to specify a format string that is used when printing schema entries. This is expected to be one of two values that the filesystem code supports.

Setting this format string allows the architecture code to change the ABI resctrl presents to user-space.

Instead, use the schema format enum to choose which format string to use.

Quoted 6 lines

Thanks.

-Fenghua

author reviewer
#11 lore raw

PATCH v7 6/49 x86/resctrl: Remove data_width and the tabular format

In reply to James Morse

The resctrl architecture code provides a data_width for the controls of each resource. This is used to zero pad all control values in the schemata file so they appear in columns. The same is done with the resource names to complete the visual effect. e.g.
| SMBA:0=2048
| L3:0=00ff

AMD platforms discover their maximum bandwidth for the MB resource from firmware, but hard-code the data_width to 4. If the maximum bandwidth requires more digits - the tabular format is silently broken. This is also broken when the mba_MBps mount option is used as the field width isn't updated. If new schema are added resctrl will need to be able to determine the maximum width. The benefit of this pretty-printing is questionable.

Instead of handling runtime discovery of the data_width for AMD platforms, remove the feature. These fields are always zero padded so should be harmless to remove if the whole field has been treated as a number. In the above example, this would now look like this:
| SMBA:0=2048
| L3:0=ff

---
 arch/x86/kernel/cpu/resctrl/core.c        | 26 -----------------------
 arch/x86/kernel/cpu/resctrl/ctrlmondata.c |  3 +--
 arch/x86/kernel/cpu/resctrl/internal.h    |  2 +-
 arch/x86/kernel/cpu/resctrl/rdtgroup.c    | 10 +++++++--
 include/linux/resctrl.h                   |  2 --
 5 files changed, 10 insertions(+), 33 deletions(-)
arch/x86/kernel/cpu/resctrl/core.c +0 -26
index 542503a8c953..754fb65565ec 100644
@@ -43,12 +43,6 @@ static DEFINE_MUTEX(domain_list_lock);
  */
 DEFINE_PER_CPU(struct resctrl_pqr_state, pqr_state);
 
-/*
- * Used to store the max resource name width and max resource data width
- * to display the schemata in a tabular format
- */
-int max_name_width, max_data_width;
-
 /*
  * Global boolean for rdt_alloc which is true if any
  * resource allocation is enabled.
@@ -228,7 +222,6 @@ static __init bool __get_mem_config_intel(struct rdt_resource *r)
 			return false;
 		r->membw.arch_needs_linear = false;
 	}
-	r->data_width = 3;
 
 	if (boot_cpu_has(X86_FEATURE_PER_THREAD_MBA))
 		r->membw.throttle_mode = THREAD_THROTTLE_PER_THREAD;
@@ -269,8 +262,6 @@ static __init bool __rdt_get_mem_config_amd(struct rdt_resource *r)
 	r->membw.throttle_mode = THREAD_THROTTLE_UNDEFINED;
 	r->membw.min_bw = 0;
 	r->membw.bw_gran = 1;
-	/* Max value is 2048, Data width should be 4 in decimal */
-	r->data_width = 4;
 
 	r->alloc_capable = true;
 
@@ -290,7 +281,6 @@ static void rdt_get_cache_alloc_cfg(int idx, struct rdt_resource *r)
 	r->cache.cbm_len = eax.split.cbm_len + 1;
 	r->default_ctrl = BIT_MASK(eax.split.cbm_len + 1) - 1;
 	r->cache.shareable_bits = ebx & r->default_ctrl;
-	r->data_width = (r->cache.cbm_len + 3) / 4;
 	if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
 		r->cache.arch_has_sparse_bitmasks = ecx.split.noncont;
 	r->alloc_capable = true;
@@ -786,20 +776,6 @@ static int resctrl_arch_offline_cpu(unsigned int cpu)
 	return 0;
 }
 
-/*
- * Choose a width for the resource name and resource data based on the
- * resource that has widest name and cbm.
- */
-static __init void rdt_init_padding(void)
-{
-	struct rdt_resource *r;
-
-	for_each_alloc_capable_rdt_resource(r) {
-		if (r->data_width > max_data_width)
-			max_data_width = r->data_width;
-	}
-}
-
 enum {
 	RDT_FLAG_CMT,
 	RDT_FLAG_MBM_TOTAL,
@@ -1102,8 +1078,6 @@ static int __init resctrl_late_init(void)
 	if (!get_rdt_resources())
 		return -ENODEV;
 
-	rdt_init_padding();
-
 	state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
 				  "x86/resctrl/cat:online:",
 				  resctrl_arch_online_cpu,
arch/x86/kernel/cpu/resctrl/ctrlmondata.c +1 -2
index c763cb4fb1a8..59610b209b4e 100644
@@ -487,8 +487,7 @@ static void show_doms(struct seq_file *s, struct resctrl_schema *schema, int clo
 			ctrl_val = resctrl_arch_get_config(r, dom, closid,
 							   schema->conf_type);
 
-		seq_printf(s, schema->fmt_str, dom->hdr.id, max_data_width,
-			   ctrl_val);
+		seq_printf(s, schema->fmt_str, dom->hdr.id, ctrl_val);
 		sep = true;
 	}
 	seq_puts(s, "\n");
arch/x86/kernel/cpu/resctrl/internal.h +1 -1
index b5543bd506c3..f975cd6cfe61 100644
@@ -326,7 +326,7 @@ struct rdtgroup {
 /* List of all resource groups */
 extern struct list_head rdt_all_groups;
 
-extern int max_name_width, max_data_width;
+extern int max_name_width;
 
 int __init rdtgroup_init(void);
 void __exit rdtgroup_exit(void);
arch/x86/kernel/cpu/resctrl/rdtgroup.c +8 -2
index e7862d0936c9..1e0bae1a9d95 100644
@@ -57,6 +57,12 @@ static struct kernfs_node *kn_mongrp;
 /* Kernel fs node for "mon_data" directory under root */
 static struct kernfs_node *kn_mondata;
 
+/*
+ * Used to store the max resource name width to display the schemata names in
+ * a tabular format.
+ */
+int max_name_width;
+
 static struct seq_buf last_cmd_status;
 static char last_cmd_status_buf[512];
 
@@ -2613,10 +2619,10 @@ static int schemata_list_add(struct rdt_resource *r, enum resctrl_conf_type type
 
 	switch (r->schema_fmt) {
 	case RESCTRL_SCHEMA_BITMAP:
-		s->fmt_str = "%d=%0*x";
+		s->fmt_str = "%d=%x";
 		break;
 	case RESCTRL_SCHEMA_RANGE:
-		s->fmt_str = "%d=%0*u";
+		s->fmt_str = "%d=%u";
 		break;
 	}
 
include/linux/resctrl.h +0 -2
index 736b9a9a9464..e1a982adef45 100644
@@ -214,7 +214,6 @@ enum resctrl_schema_fmt {
  * @ctrl_domains:	RCU list of all control domains for this resource
  * @mon_domains:	RCU list of all monitor domains for this resource
  * @name:		Name to use in "schemata" file.
- * @data_width:		Character width of data when displaying
  * @default_ctrl:	Specifies default cache cbm or memory B/W percent.
  * @schema_fmt:		Which format string and parser is used for this schema.
  * @evt_list:		List of monitoring events
@@ -232,7 +231,6 @@ struct rdt_resource {
 	struct list_head	ctrl_domains;
 	struct list_head	mon_domains;
 	char			*name;
-	int			data_width;
 	u32			default_ctrl;
 	enum resctrl_schema_fmt	schema_fmt;
 	struct list_head	evt_list;
Signature · 1 line
2.39.5
Reviewed-by on v7 6/49
#12 lore raw

Re: [PATCH v7 06/49] x86/resctrl: Remove data_width and the tabular format

Quoted 22 lines · James Morse

The resctrl architecture code provides a data_width for the controls of each resource. This is used to zero pad all control values in the schemata file so they appear in columns. The same is done with the resource names to complete the visual effect. e.g.
| SMBA:0=2048
| L3:0=00ff

AMD platforms discover their maximum bandwidth for the MB resource from firmware, but hard-code the data_width to 4. If the maximum bandwidth requires more digits - the tabular format is silently broken. This is also broken when the mba_MBps mount option is used as the field width isn't updated. If new schema are added resctrl will need to be able to determine the maximum width. The benefit of this pretty-printing is questionable.

Instead of handling runtime discovery of the data_width for AMD platforms, remove the feature. These fields are always zero padded so should be harmless to remove if the whole field has been treated as a number. In the above example, this would now look like this:
| SMBA:0=2048
| L3:0=ff

Quoted 5 lines

Thanks.

-Fenghua

author reviewer
#13 lore raw

PATCH v7 7/49 x86/resctrl: Add max_bw to struct resctrl_membw

In reply to James Morse

__rdt_get_mem_config_amd() and __get_mem_config_intel() both use the default_ctrl property as a maximum value. This is because the MBA schema works differently between these platforms. Doing this complicates determining whether the default_ctrl property belongs to the arch code, or can be derived from the schema format.

Deriving the maximum or default value from the schema format would avoid the architecture code having to tell resctrl such obvious things as the maximum percentage is 100, and the maximum bitmap is all ones.

Maximum bandwidth is always going to vary per platform. Add max_bw as a special case. This is currently used for the maximum MBA percentage on Intel platforms, but can be removed from the architecture code if 'percentage' becomes a schema format resctrl supports directly.

This value isn't needed for other schema formats.

This will allow the default_ctrl to be generated from the schema properties when it is needed.

---
Changes since v5:
* Removed redundant setting of schema_fmt on AMD platforms.
* Fixed off by one in cbm_validate().

Changes since v2:
* This patch is new.

---
 arch/x86/kernel/cpu/resctrl/core.c        | 2 ++
 arch/x86/kernel/cpu/resctrl/ctrlmondata.c | 4 ++--
 include/linux/resctrl.h                   | 2 ++
 3 files changed, 6 insertions(+), 2 deletions(-)
arch/x86/kernel/cpu/resctrl/core.c +2 -0
index 754fb65565ec..4504a12efc97 100644
@@ -212,6 +212,7 @@ static __init bool __get_mem_config_intel(struct rdt_resource *r)
 	hw_res->num_closid = edx.split.cos_max + 1;
 	max_delay = eax.split.max_delay + 1;
 	r->default_ctrl = MAX_MBA_BW;
+	r->membw.max_bw = MAX_MBA_BW;
 	r->membw.arch_needs_linear = true;
 	if (ecx & MBA_IS_LINEAR) {
 		r->membw.delay_linear = true;
@@ -250,6 +251,7 @@ static __init bool __rdt_get_mem_config_amd(struct rdt_resource *r)
 	cpuid_count(0x80000020, subleaf, &eax, &ebx, &ecx, &edx);
 	hw_res->num_closid = edx + 1;
 	r->default_ctrl = 1 << eax;
+	r->membw.max_bw = 1 << eax;
 
 	/* AMD does not use delay */
 	r->membw.delay_linear = false;
arch/x86/kernel/cpu/resctrl/ctrlmondata.c +2 -2
index 59610b209b4e..23a01eaebd58 100644
@@ -63,9 +63,9 @@ static bool bw_validate(char *buf, u32 *data, struct rdt_resource *r)
 		return true;
 	}
 
-	if (bw < r->membw.min_bw || bw > r->default_ctrl) {
+	if (bw < r->membw.min_bw || bw > r->membw.max_bw) {
 		rdt_last_cmd_printf("MB value %u out of range [%d,%d]\n",
-				    bw, r->membw.min_bw, r->default_ctrl);
+				    bw, r->membw.min_bw, r->membw.max_bw);
 		return false;
 	}
 
include/linux/resctrl.h +2 -0
index e1a982adef45..465f3cf8c4bc 100644
@@ -165,6 +165,7 @@ enum membw_throttle_mode {
 /**
  * struct resctrl_membw - Memory bandwidth allocation related data
  * @min_bw:		Minimum memory bandwidth percentage user can request
+ * @max_bw:		Maximum memory bandwidth value, used as the reset value
  * @bw_gran:		Granularity at which the memory bandwidth is allocated
  * @delay_linear:	True if memory B/W delay is in linear scale
  * @arch_needs_linear:	True if we can't configure non-linear resources
@@ -175,6 +176,7 @@ enum membw_throttle_mode {
  */
 struct resctrl_membw {
 	u32				min_bw;
+	u32				max_bw;
 	u32				bw_gran;
 	u32				delay_linear;
 	bool				arch_needs_linear;
Signature · 1 line
2.39.5
Reviewed-by on v7 7/49
#14 lore raw

Re: [PATCH v7 07/49] x86/resctrl: Add max_bw to struct resctrl_membw

Quoted 22 lines · James Morse

__rdt_get_mem_config_amd() and __get_mem_config_intel() both use the default_ctrl property as a maximum value. This is because the MBA schema works differently between these platforms. Doing this complicates determining whether the default_ctrl property belongs to the arch code, or can be derived from the schema format.

Deriving the maximum or default value from the schema format would avoid the architecture code having to tell resctrl such obvious things as the maximum percentage is 100, and the maximum bitmap is all ones.

Maximum bandwidth is always going to vary per platform. Add max_bw as a special case. This is currently used for the maximum MBA percentage on Intel platforms, but can be removed from the architecture code if 'percentage' becomes a schema format resctrl supports directly.

This value isn't needed for other schema formats.

This will allow the default_ctrl to be generated from the schema properties when it is needed.

Quoted 6 lines

Thanks.

-Fenghua

author reviewer
#15 lore raw

PATCH v7 8/49 x86/resctrl: Generate default_ctrl instead of sharing it

In reply to James Morse

The struct rdt_resource default_ctrl is used by both the architecture code for resetting the hardware controls, and sometimes by the filesystem code as the default value for the schema, unless the bandwidth software controller is in use.

Having the default exposed by the architecture code causes unnecessary duplication for each architecture as the default value must be specified, but can be derived from other schema properties. Now that the maximum bandwidth is explicitly described, resctrl can derive the default value from the schema format and the other resource properties.

---
Changes since v6:
* Return MBA_BW_MAX from delay_bw_map() for improved readability.
* Generate the bitmap directly in rdt_get_cache_alloc_cfg() to make it clear shareable_bits is being generated correctly.

Changes since v5:
* Rewrote commit message.

Changes since v2:
* This patch is new.

---
 arch/x86/kernel/cpu/resctrl/core.c        | 19 +++++++++----------
 arch/x86/kernel/cpu/resctrl/ctrlmondata.c |  5 +++--
 arch/x86/kernel/cpu/resctrl/rdtgroup.c    |  6 +++---
 include/linux/resctrl.h                   | 19 +++++++++++++++++--
 4 files changed, 32 insertions(+), 17 deletions(-)
arch/x86/kernel/cpu/resctrl/core.c +9 -10
index 4504a12efc97..5280a2819760 100644
@@ -143,7 +143,10 @@ static inline void cache_alloc_hsw_probe(void)
 {
 	struct rdt_hw_resource *hw_res = &rdt_resources_all[RDT_RESOURCE_L3];
 	struct rdt_resource *r  = &hw_res->r_resctrl;
-	u64 max_cbm = BIT_ULL_MASK(20) - 1, l3_cbm_0;
+	u64 max_cbm, l3_cbm_0;
+
+	r->cache.cbm_len = 20;
+	max_cbm = resctrl_get_default_ctrl(r);
 
 	if (wrmsrl_safe(MSR_IA32_L3_CBM_BASE, max_cbm))
 		return;
@@ -155,8 +158,6 @@ static inline void cache_alloc_hsw_probe(void)
 		return;
 
 	hw_res->num_closid = 4;
-	r->default_ctrl = max_cbm;
-	r->cache.cbm_len = 20;
 	r->cache.shareable_bits = 0xc0000;
 	r->cache.min_cbm_bits = 2;
 	r->cache.arch_has_sparse_bitmasks = false;
@@ -211,7 +212,6 @@ static __init bool __get_mem_config_intel(struct rdt_resource *r)
 	cpuid_count(0x00000010, 3, &eax.full, &ebx, &ecx, &edx.full);
 	hw_res->num_closid = edx.split.cos_max + 1;
 	max_delay = eax.split.max_delay + 1;
-	r->default_ctrl = MAX_MBA_BW;
 	r->membw.max_bw = MAX_MBA_BW;
 	r->membw.arch_needs_linear = true;
 	if (ecx & MBA_IS_LINEAR) {
@@ -250,7 +250,6 @@ static __init bool __rdt_get_mem_config_amd(struct rdt_resource *r)
 
 	cpuid_count(0x80000020, subleaf, &eax, &ebx, &ecx, &edx);
 	hw_res->num_closid = edx + 1;
-	r->default_ctrl = 1 << eax;
 	r->membw.max_bw = 1 << eax;
 
 	/* AMD does not use delay */
@@ -276,13 +275,13 @@ static void rdt_get_cache_alloc_cfg(int idx, struct rdt_resource *r)
 	union cpuid_0x10_1_eax eax;
 	union cpuid_0x10_x_ecx ecx;
 	union cpuid_0x10_x_edx edx;
-	u32 ebx;
+	u32 ebx, default_ctrl;
 
 	cpuid_count(0x00000010, idx, &eax.full, &ebx, &ecx.full, &edx.full);
 	hw_res->num_closid = edx.split.cos_max + 1;
 	r->cache.cbm_len = eax.split.cbm_len + 1;
-	r->default_ctrl = BIT_MASK(eax.split.cbm_len + 1) - 1;
-	r->cache.shareable_bits = ebx & r->default_ctrl;
+	default_ctrl = BIT_MASK(eax.split.cbm_len + 1) - 1;
+	r->cache.shareable_bits = ebx & default_ctrl;
 	if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
 		r->cache.arch_has_sparse_bitmasks = ecx.split.noncont;
 	r->alloc_capable = true;
@@ -329,7 +328,7 @@ static u32 delay_bw_map(unsigned long bw, struct rdt_resource *r)
 		return MAX_MBA_BW - bw;
 
 	pr_warn_once("Non Linear delay-bw map not supported but queried\n");
-	return r->default_ctrl;
+	return MAX_MBA_BW;
 }
 
 static void mba_wrmsr_intel(struct msr_param *m)
@@ -438,7 +437,7 @@ static void setup_default_ctrlval(struct rdt_resource *r, u32 *dc)
 	 * For Memory Allocation: Set b/w requested to 100%
 	 */
 	for (i = 0; i < hw_res->num_closid; i++, dc++)
-		*dc = r->default_ctrl;
+		*dc = resctrl_get_default_ctrl(r);
 }
 
 static void ctrl_domain_free(struct rdt_hw_ctrl_domain *hw_dom)
arch/x86/kernel/cpu/resctrl/ctrlmondata.c +3 -2
index 23a01eaebd58..5d87f279085f 100644
@@ -113,8 +113,9 @@ static int parse_bw(struct rdt_parse_data *data, struct resctrl_schema *s,
  */
 static bool cbm_validate(char *buf, u32 *data, struct rdt_resource *r)
 {
-	unsigned long first_bit, zero_bit, val;
+	u32 supported_bits = BIT_MASK(r->cache.cbm_len) - 1;
 	unsigned int cbm_len = r->cache.cbm_len;
+	unsigned long first_bit, zero_bit, val;
 	int ret;
 
 	ret = kstrtoul(buf, 16, &val);
@@ -123,7 +124,7 @@ static bool cbm_validate(char *buf, u32 *data, struct rdt_resource *r)
 		return false;
 	}
 
-	if ((r->cache.min_cbm_bits > 0 && val == 0) || val > r->default_ctrl) {
+	if ((r->cache.min_cbm_bits > 0 && val == 0) || val > supported_bits) {
 		rdt_last_cmd_puts("Mask out of range\n");
 		return false;
 	}
arch/x86/kernel/cpu/resctrl/rdtgroup.c +3 -3
index 1e0bae1a9d95..cd8f65c12124 100644
@@ -978,7 +978,7 @@ static int rdt_default_ctrl_show(struct kernfs_open_file *of,
 	struct resctrl_schema *s = of->kn->parent->priv;
 	struct rdt_resource *r = s->res;
 
-	seq_printf(seq, "%x\n", r->default_ctrl);
+	seq_printf(seq, "%x\n", resctrl_get_default_ctrl(r));
 	return 0;
 }
 
@@ -2882,7 +2882,7 @@ static int reset_all_ctrls(struct rdt_resource *r)
 		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;
+			hw_dom->ctrl_val[i] = resctrl_get_default_ctrl(r);
 		msr_param.dom = d;
 		smp_call_function_any(&d->hdr.cpu_mask, rdt_ctrl_update, &msr_param, 1);
 	}
@@ -3417,7 +3417,7 @@ static void rdtgroup_init_mba(struct rdt_resource *r, u32 closid)
 		}
 
 		cfg = &d->staged_config[CDP_NONE];
-		cfg->new_ctrl = r->default_ctrl;
+		cfg->new_ctrl = resctrl_get_default_ctrl(r);
 		cfg->have_new_ctrl = true;
 	}
 }
include/linux/resctrl.h +17 -2
index 465f3cf8c4bc..d16dc960f1fc 100644
@@ -216,7 +216,6 @@ enum resctrl_schema_fmt {
  * @ctrl_domains:	RCU list of all control domains for this resource
  * @mon_domains:	RCU list of all monitor domains for this resource
  * @name:		Name to use in "schemata" file.
- * @default_ctrl:	Specifies default cache cbm or memory B/W percent.
  * @schema_fmt:		Which format string and parser is used for this schema.
  * @evt_list:		List of monitoring events
  * @cdp_capable:	Is the CDP feature available on this resource
@@ -233,7 +232,6 @@ struct rdt_resource {
 	struct list_head	ctrl_domains;
 	struct list_head	mon_domains;
 	char			*name;
-	u32			default_ctrl;
 	enum resctrl_schema_fmt	schema_fmt;
 	struct list_head	evt_list;
 	bool			cdp_capable;
@@ -268,6 +266,23 @@ struct resctrl_schema {
 	u32				num_closid;
 };
 
+/**
+ * resctrl_get_default_ctrl() - Return the default control value for this
+ *                              resource.
+ * @r:		The resource whose default control type is queried.
+ */
+static inline u32 resctrl_get_default_ctrl(struct rdt_resource *r)
+{
+	switch (r->schema_fmt) {
+	case RESCTRL_SCHEMA_BITMAP:
+		return BIT_MASK(r->cache.cbm_len) - 1;
+	case RESCTRL_SCHEMA_RANGE:
+		return r->membw.max_bw;
+	}
+
+	return WARN_ON_ONCE(1);
+}
+
 /* The number of closid supported by this resource regardless of CDP */
 u32 resctrl_arch_get_num_closid(struct rdt_resource *r);
 u32 resctrl_arch_system_num_rmid_idx(void);
Signature · 1 line
2.39.5
Reviewed-by on v7 8/49
#16 lore raw

Re: [PATCH v7 08/49] x86/resctrl: Generate default_ctrl instead of sharing it

Quoted 11 lines · James Morse

The struct rdt_resource default_ctrl is used by both the architecture code for resetting the hardware controls, and sometimes by the filesystem code as the default value for the schema, unless the bandwidth software controller is in use.

Having the default exposed by the architecture code causes unnecessary duplication for each architecture as the default value must be specified, but can be derived from other schema properties. Now that the maximum bandwidth is explicitly described, resctrl can derive the default value from the schema format and the other resource properties.

Quoted 5 lines

Thanks.

-Fenghua

maintainer
#17 lore raw

Re: [PATCH v7 08/49] x86/resctrl: Generate default_ctrl instead of sharing it

In reply to James Morse

Hi James,

Quoted 40 lines · James Morse

The struct rdt_resource default_ctrl is used by both the architecture code for resetting the hardware controls, and sometimes by the filesystem code as the default value for the schema, unless the bandwidth software controller is in use.

Having the default exposed by the architecture code causes unnecessary duplication for each architecture as the default value must be specified, but can be derived from other schema properties. Now that the maximum bandwidth is explicitly described, resctrl can derive the default value from the schema format and the other resource properties.

---
Changes since v6:
* Return MBA_BW_MAX from delay_bw_map() for improved readability.
* Generate the bitmap directly in rdt_get_cache_alloc_cfg() to make it clear shareable_bits is being generated correctly.

Changes since v5:
* Rewrote commit message.

Changes since v2:
* This patch is new.

---
 arch/x86/kernel/cpu/resctrl/core.c        | 19 +++++++++----------
 arch/x86/kernel/cpu/resctrl/ctrlmondata.c |  5 +++--
 arch/x86/kernel/cpu/resctrl/rdtgroup.c    |  6 +++---
 include/linux/resctrl.h                   | 19 +++++++++++++++++--
 4 files changed, 32 insertions(+), 17 deletions(-)
arch/x86/kernel/cpu/resctrl/core.c +0 -0
index 4504a12efc97..5280a2819760 100644
@@ -143,7 +143,10 @@ static inline void cache_alloc_hsw_probe(void)
 {

arch/x86/kernel/cpu/resctrl/core.c static inline void cache_alloc_hsw_probe(void) · line 152

Quoted 10 lines
arch/x86/kernel/cpu/resctrl/core.c +4 -1
 	struct rdt_hw_resource *hw_res = &rdt_resources_all[RDT_RESOURCE_L3];
 	struct rdt_resource *r  = &hw_res->r_resctrl;
-	u64 max_cbm = BIT_ULL_MASK(20) - 1, l3_cbm_0;
+	u64 max_cbm, l3_cbm_0;
+
+	r->cache.cbm_len = 20;
+	max_cbm = resctrl_get_default_ctrl(r);
 
 	if (wrmsrl_safe(MSR_IA32_L3_CBM_BASE, max_cbm))
 		return;

It is unclear to me why this architecture code continues to use resctrl_get_default_ctrl() while you switched away from it in the other architecture code.
As resctrl_get_default_ctrl() is "intended for callers that don't know or care what the format is" [1], here it clearly is required to be a bitmask. Using resctrl_get_default_ctrl() here also seems to contradict your argument for not using it in cbm_validate(). [1]

Reinette

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

author reviewer
#18 lore raw

Re: [PATCH v7 08/49] x86/resctrl: Generate default_ctrl instead of sharing it

Hi Reinette,

Quoted 26 lines · Reinette Chatre
Quoted 25 lines · James Morse

The struct rdt_resource default_ctrl is used by both the architecture code for resetting the hardware controls, and sometimes by the filesystem code as the default value for the schema, unless the bandwidth software controller is in use.

Having the default exposed by the architecture code causes unnecessary duplication for each architecture as the default value must be specified, but can be derived from other schema properties. Now that the maximum bandwidth is explicitly described, resctrl can derive the default value from the schema format and the other resource properties.

arch/x86/kernel/cpu/resctrl/core.c +4 -1
index 4504a12efc97..5280a2819760 100644
@@ -143,7 +143,10 @@ static inline void cache_alloc_hsw_probe(void)
 {
 	struct rdt_hw_resource *hw_res = &rdt_resources_all[RDT_RESOURCE_L3];
 	struct rdt_resource *r  = &hw_res->r_resctrl;
-	u64 max_cbm = BIT_ULL_MASK(20) - 1, l3_cbm_0;
+	u64 max_cbm, l3_cbm_0;
+
+	r->cache.cbm_len = 20;
+	max_cbm = resctrl_get_default_ctrl(r);
 
Quoted 10 lines

arch/x86/kernel/cpu/resctrl/core.c static inline void cache_alloc_hsw_probe(void) · line 152

Quoted 2 lines
arch/x86/kernel/cpu/resctrl/core.c +0 -0
 	if (wrmsrl_safe(MSR_IA32_L3_CBM_BASE, max_cbm))
 		return;

It is unclear to me why this architecture code continues to use resctrl_get_default_ctrl() while you switched away from it in the other architecture code.
As resctrl_get_default_ctrl() is "intended for callers that don't know or care what the format is" [1], here it clearly is required to be a bitmask. Using resctrl_get_default_ctrl() here also seems to contradict your argument for not using it in cbm_validate(). [1]

Sure, I'll drop this hunk on the same reasoning.

Thanks,

James

author reviewer
#19 lore raw

PATCH v7 9/49 x86/resctrl: Add helper for setting CPU default properties

In reply to James Morse

rdtgroup_rmdir_ctrl() and rdtgroup_rmdir_mon() set the per-CPU pqr_state for CPUs that were part of the rmdir()'d group.

Another architecture might not have a 'pqr_state', its hardware may need the values in a different format. MPAM's equivalent of RMID values are not unique, and always need the CLOSID to be provided too.

There is only one caller that modifies a single value, (rdtgroup_rmdir_mon()). MPAM always needs both CLOSID and RMID for the hardware value as these are written to the same system register.

As rdtgroup_rmdir_mon() has the CLOSID on hand, only provide a helper to set both values. These values are read by __resctrl_sched_in(), but may be written by a different CPU without any locking, add READ/WRTE_ONCE() to avoid torn values.

---
Changes since v1:
* In rdtgroup_rmdir_mon(), (re)set CPU default closid based on the parent control group, to avoid the appearance of referencing something that we're in the process of destroying (even if it doesn't make a difference because the victim mon group necessarily has the same closid as the parent control group).

Update comment to match.

No (intentional) functional change.

---
 arch/x86/include/asm/resctrl.h         | 14 +++++++++++---
 arch/x86/kernel/cpu/resctrl/rdtgroup.c | 20 ++++++++++++++------
 2 files changed, 25 insertions(+), 9 deletions(-)
arch/x86/include/asm/resctrl.h +11 -3
index 8b1b6ce1e51b..6908cd0e6e40 100644
@@ -4,8 +4,9 @@
 
 #ifdef CONFIG_X86_CPU_RESCTRL
 
-#include <linux/sched.h>
 #include <linux/jump_label.h>
+#include <linux/percpu.h>
+#include <linux/sched.h>
 
 /*
  * This value can never be a valid CLOSID, and is used when mapping a
@@ -96,8 +97,8 @@ static inline void resctrl_arch_disable_mon(void)
 static inline void __resctrl_sched_in(struct task_struct *tsk)
 {
 	struct resctrl_pqr_state *state = this_cpu_ptr(&pqr_state);
-	u32 closid = state->default_closid;
-	u32 rmid = state->default_rmid;
+	u32 closid = READ_ONCE(state->default_closid);
+	u32 rmid = READ_ONCE(state->default_rmid);
 	u32 tmp;
 
 	/*
@@ -132,6 +133,13 @@ static inline unsigned int resctrl_arch_round_mon_val(unsigned int val)
 	return val * scale;
 }
 
+static inline void resctrl_arch_set_cpu_default_closid_rmid(int cpu, u32 closid,
+							    u32 rmid)
+{
+	WRITE_ONCE(per_cpu(pqr_state.default_closid, cpu), closid);
+	WRITE_ONCE(per_cpu(pqr_state.default_rmid, cpu), rmid);
+}
+
 static inline void resctrl_arch_set_closid_rmid(struct task_struct *tsk,
 						u32 closid, u32 rmid)
 {
arch/x86/kernel/cpu/resctrl/rdtgroup.c +14 -6
index cd8f65c12124..f706e5a288b1 100644
@@ -3731,14 +3731,21 @@ static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
 static int rdtgroup_rmdir_mon(struct rdtgroup *rdtgrp, cpumask_var_t tmpmask)
 {
 	struct rdtgroup *prdtgrp = rdtgrp->mon.parent;
+	u32 closid, rmid;
 	int cpu;
 
 	/* Give any tasks back to the parent group */
 	rdt_move_group_tasks(rdtgrp, prdtgrp, tmpmask);
 
-	/* Update per cpu rmid of the moved CPUs first */
+	/*
+	 * Update per cpu closid/rmid of the moved CPUs first.
+	 * Note: the closid will not change, but the arch code still needs it.
+	 */
+	closid = prdtgrp->closid;
+	rmid = prdtgrp->mon.rmid;
 	for_each_cpu(cpu, &rdtgrp->cpu_mask)
-		per_cpu(pqr_state.default_rmid, cpu) = prdtgrp->mon.rmid;
+		resctrl_arch_set_cpu_default_closid_rmid(cpu, closid, rmid);
+
 	/*
 	 * Update the MSR on moved CPUs and CPUs which have moved
 	 * task running on them.
@@ -3771,6 +3778,7 @@ static int rdtgroup_ctrl_remove(struct rdtgroup *rdtgrp)
 
 static int rdtgroup_rmdir_ctrl(struct rdtgroup *rdtgrp, cpumask_var_t tmpmask)
 {
+	u32 closid, rmid;
 	int cpu;
 
 	/* Give any tasks back to the default group */
@@ -3781,10 +3789,10 @@ static int rdtgroup_rmdir_ctrl(struct rdtgroup *rdtgrp, cpumask_var_t tmpmask)
 		   &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
 
 	/* Update per cpu closid and rmid of the moved CPUs first */
-	for_each_cpu(cpu, &rdtgrp->cpu_mask) {
-		per_cpu(pqr_state.default_closid, cpu) = rdtgroup_default.closid;
-		per_cpu(pqr_state.default_rmid, cpu) = rdtgroup_default.mon.rmid;
-	}
+	closid = rdtgroup_default.closid;
+	rmid = rdtgroup_default.mon.rmid;
+	for_each_cpu(cpu, &rdtgrp->cpu_mask)
+		resctrl_arch_set_cpu_default_closid_rmid(cpu, closid, rmid);
 
 	/*
 	 * Update the MSR on moved CPUs and CPUs which have moved
Signature · 1 line
2.39.5
Reviewed-by on v7 9/49
#20 lore raw

Re: [PATCH v7 09/49] x86/resctrl: Add helper for setting CPU default properties

Quoted 17 lines · James Morse

rdtgroup_rmdir_ctrl() and rdtgroup_rmdir_mon() set the per-CPU pqr_state for CPUs that were part of the rmdir()'d group.

Another architecture might not have a 'pqr_state', its hardware may need the values in a different format. MPAM's equivalent of RMID values are not unique, and always need the CLOSID to be provided too.

There is only one caller that modifies a single value, (rdtgroup_rmdir_mon()). MPAM always needs both CLOSID and RMID for the hardware value as these are written to the same system register.

As rdtgroup_rmdir_mon() has the CLOSID on hand, only provide a helper to set both values. These values are read by __resctrl_sched_in(), but may be written by a different CPU without any locking, add READ/WRTE_ONCE() to avoid torn values.

Quoted 8 lines

Thanks.

-Fenghua

author reviewer
#21 lore raw

PATCH v7 10/49 x86/resctrl: Remove rdtgroup from update_cpu_closid_rmid()

In reply to James Morse

update_cpu_closid_rmid() takes a struct rdtgroup as an argument, which it uses to update the local CPUs default pqr values. This is a problem once the resctrl parts move out to /fs/, as the arch code cannot poke around inside struct rdtgroup.

Rename update_cpu_closid_rmid() as resctrl_arch_sync_cpus_defaults() to be used as the target of an IPI, and pass the effective CLOSID and RMID in a new struct.

---
Changes since v1:
* To clarify the meanings of the new helper and struct:

   Rename resctrl_arch_sync_cpu_default() to
   resctrl_arch_sync_cpu_closid_rmid();

Rename struct resctrl_cpu_sync to struct resctrl_cpu_defaults;

Flesh out the comment block in <linux/resctrl.h>.

No functional change.

---
 arch/x86/kernel/cpu/resctrl/rdtgroup.c | 17 +++++++++++++----
 include/linux/resctrl.h                | 22 ++++++++++++++++++++++
 2 files changed, 35 insertions(+), 4 deletions(-)
arch/x86/kernel/cpu/resctrl/rdtgroup.c +13 -4
index f706e5a288b1..62d9a50c7bba 100644
@@ -355,13 +355,13 @@ static int rdtgroup_cpus_show(struct kernfs_open_file *of,
  * from update_closid_rmid() is protected against __switch_to() because
  * preemption is disabled.
  */
-static void update_cpu_closid_rmid(void *info)
+void resctrl_arch_sync_cpu_closid_rmid(void *info)
 {
-	struct rdtgroup *r = info;
+	struct resctrl_cpu_defaults *r = info;
 
 	if (r) {
 		this_cpu_write(pqr_state.default_closid, r->closid);
-		this_cpu_write(pqr_state.default_rmid, r->mon.rmid);
+		this_cpu_write(pqr_state.default_rmid, r->rmid);
 	}
 
 	/*
@@ -376,11 +376,20 @@ static void update_cpu_closid_rmid(void *info)
  * Update the PGR_ASSOC MSR on all cpus in @cpu_mask,
  *
  * Per task closids/rmids must have been set up before calling this function.
+ * @r may be NULL.
  */
 static void
 update_closid_rmid(const struct cpumask *cpu_mask, struct rdtgroup *r)
 {
-	on_each_cpu_mask(cpu_mask, update_cpu_closid_rmid, r, 1);
+	struct resctrl_cpu_defaults defaults, *p = NULL;
+
+	if (r) {
+		defaults.closid = r->closid;
+		defaults.rmid = r->mon.rmid;
+		p = &defaults;
+	}
+
+	on_each_cpu_mask(cpu_mask, resctrl_arch_sync_cpu_closid_rmid, p, 1);
 }
 
 static int cpus_mon_write(struct rdtgroup *rdtgrp, cpumask_var_t newmask,
include/linux/resctrl.h +22 -0
index d16dc960f1fc..31808b3ddecb 100644
@@ -266,6 +266,28 @@ struct resctrl_schema {
 	u32				num_closid;
 };
 
+struct resctrl_cpu_defaults {
+	u32 closid;
+	u32 rmid;
+};
+
+/**
+ * resctrl_arch_sync_cpu_closid_rmid() - Refresh this CPU's CLOSID and RMID.
+ *					 Call via IPI.
+ * @info:	If non-NULL, a pointer to a struct resctrl_cpu_defaults
+ *		specifying the new CLOSID and RMID for tasks in the default
+ *		resctrl ctrl and mon group when running on this CPU.  If NULL,
+ *		this CPU is not re-assigned to a different default group.
+ *
+ * Propagates reassignment of CPUs and/or tasks to different resctrl groups
+ * when requested by the resctrl core code.
+ *
+ * This function records the per-cpu defaults specified by @info (if any),
+ * and then reconfigures the CPU's hardware CLOSID and RMID for subsequent
+ * execution based on @current, in the same way as during a task switch.
+ */
+void resctrl_arch_sync_cpu_closid_rmid(void *info);
+
 /**
  * resctrl_get_default_ctrl() - Return the default control value for this
  *                              resource.
Signature · 1 line
2.39.5
Reviewed-by on v7 10/49
#22 lore raw

Re: [PATCH v7 10/49] x86/resctrl: Remove rdtgroup from update_cpu_closid_rmid()

Quoted 9 lines · James Morse

update_cpu_closid_rmid() takes a struct rdtgroup as an argument, which it uses to update the local CPUs default pqr values. This is a problem once the resctrl parts move out to /fs/, as the arch code cannot poke around inside struct rdtgroup.

Rename update_cpu_closid_rmid() as resctrl_arch_sync_cpus_defaults() to be used as the target of an IPI, and pass the effective CLOSID and RMID in a new struct.

Quoted 8 lines

Thanks.

-Fenghua

author reviewer
#23 lore raw

PATCH v7 11/49 x86/resctrl: Expose resctrl fs's init function to the rest of the kernel

In reply to James Morse

rdtgroup_init() needs exposing to the rest of the kernel so that arch code can call it once it lives in core code. As this is one of the few functions exposed, rename it to have "resctrl" in the name. The same goes for the exit call.

Rename x86's arch code init functions for RDT to have an arch prefix to make it clear these are part of the architecture code.

---
Changes since v5:
* Tweaked the word 'export'

Changes since v4:
* Changed the voice of some of the commit message.

Changes since v1:
* Rename stale rdtgroup_init() to resctrl_init() in arch/x86/kernel/cpu/resctrl/monitor.c comments.

No functional change.

* [Commit message only] Minor rewording to avoid "impersonating code".

* [Commit message only] Typo fix:
s/to have the resctrl/to have resctrl/ in commit message.

---
 arch/x86/kernel/cpu/resctrl/core.c     | 12 ++++++------
 arch/x86/kernel/cpu/resctrl/internal.h |  3 ---
 arch/x86/kernel/cpu/resctrl/monitor.c  |  2 +-
 arch/x86/kernel/cpu/resctrl/rdtgroup.c |  8 ++++----
 include/linux/resctrl.h                |  3 +++
 5 files changed, 14 insertions(+), 14 deletions(-)
arch/x86/kernel/cpu/resctrl/core.c +6 -6
index 5280a2819760..ce50a7d3230c 100644
@@ -1063,7 +1063,7 @@ void resctrl_cpu_detect(struct cpuinfo_x86 *c)
 	}
 }
 
-static int __init resctrl_late_init(void)
+static int __init resctrl_arch_late_init(void)
 {
 	struct rdt_resource *r;
 	int state, ret;
@@ -1086,7 +1086,7 @@ static int __init resctrl_late_init(void)
 	if (state < 0)
 		return state;
 
-	ret = rdtgroup_init();
+	ret = resctrl_init();
 	if (ret) {
 		cpuhp_remove_state(state);
 		return ret;
@@ -1102,18 +1102,18 @@ static int __init resctrl_late_init(void)
 	return 0;
 }
 
-late_initcall(resctrl_late_init);
+late_initcall(resctrl_arch_late_init);
 
-static void __exit resctrl_exit(void)
+static void __exit resctrl_arch_exit(void)
 {
 	struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
 
 	cpuhp_remove_state(rdt_online);
 
-	rdtgroup_exit();
+	resctrl_exit();
 
 	if (r->mon_capable)
 		rdt_put_mon_l3_config();
 }
 
-__exitcall(resctrl_exit);
+__exitcall(resctrl_arch_exit);
arch/x86/kernel/cpu/resctrl/internal.h +0 -3
index f975cd6cfe61..8291f1b59981 100644
@@ -328,9 +328,6 @@ extern struct list_head rdt_all_groups;
 
 extern int max_name_width;
 
-int __init rdtgroup_init(void);
-void __exit rdtgroup_exit(void);
-
 /**
  * struct rftype - describe each file in the resctrl file system
  * @name:	File name
arch/x86/kernel/cpu/resctrl/monitor.c +1 -1
index 58b5b21349a8..e8388d19a579 100644
@@ -1027,7 +1027,7 @@ static int dom_data_init(struct rdt_resource *r)
 	/*
 	 * RESCTRL_RESERVED_CLOSID and RESCTRL_RESERVED_RMID are special and
 	 * are always allocated. These are used for the rdtgroup_default
-	 * control group, which will be setup later in rdtgroup_init().
+	 * control group, which will be setup later in resctrl_init().
 	 */
 	idx = resctrl_arch_rmid_idx_encode(RESCTRL_RESERVED_CLOSID,
 					   RESCTRL_RESERVED_RMID);
arch/x86/kernel/cpu/resctrl/rdtgroup.c +4 -4
index 62d9a50c7bba..b2dad689e780 100644
@@ -4235,14 +4235,14 @@ void resctrl_offline_cpu(unsigned int cpu)
 }
 
 /*
- * rdtgroup_init - rdtgroup initialization
+ * resctrl_init - resctrl filesystem initialization
  *
  * Setup resctrl file system including set up root, create mount point,
- * register rdtgroup filesystem, and initialize files under root directory.
+ * register resctrl filesystem, and initialize files under root directory.
  *
  * Return: 0 on success or -errno
  */
-int __init rdtgroup_init(void)
+int __init resctrl_init(void)
 {
 	int ret = 0;
 
@@ -4290,7 +4290,7 @@ int __init rdtgroup_init(void)
 	return ret;
 }
 
-void __exit rdtgroup_exit(void)
+void __exit resctrl_exit(void)
 {
 	debugfs_remove_recursive(debugfs_resctrl);
 	unregister_filesystem(&rdt_fs_type);
include/linux/resctrl.h +3 -0
index 31808b3ddecb..f1979e375da9 100644
@@ -402,4 +402,7 @@ void resctrl_arch_reset_rmid_all(struct rdt_resource *r, struct rdt_mon_domain *
 extern unsigned int resctrl_rmid_realloc_threshold;
 extern unsigned int resctrl_rmid_realloc_limit;
 
+int __init resctrl_init(void);
+void __exit resctrl_exit(void);
+
 #endif /* _RESCTRL_H */
Signature · 1 line
2.39.5
Reviewed-by on v7 11/49
#24 lore raw

Re: [PATCH v7 11/49] x86/resctrl: Expose resctrl fs's init function to the rest of the kernel

Quoted 8 lines · James Morse

rdtgroup_init() needs exposing to the rest of the kernel so that arch code can call it once it lives in core code. As this is one of the few functions exposed, rename it to have "resctrl" in the name. The same goes for the exit call.

Rename x86's arch code init functions for RDT to have an arch prefix to make it clear these are part of the architecture code.

Quoted 8 lines

Thanks.

-Fenghua

author reviewer
#25 lore raw

PATCH v7 12/49 x86/resctrl: Move rdt_find_domain() to be visible to arch and fs code

In reply to James Morse

rdt_find_domain() finds a domain given a resource and a cache-id. This is used by both the architecture code and the filesystem code.

After the filesystem code moves to live in /fs/, this helper is either duplicated by all architectures, or needs exposing by the filesystem code.

Add the definition to the global header file. As its now globally visible, and has only a handful of callers, swap the 'rdt' for 'resctrl'. Move the function to live with its caller in ctrlmondata.c as the filesystem code will not have anything corresponding to core.c.

---
Changes since v6:
* Don't inline this helper.
* Moved some words from the discussion into the commit message.

Changes since v5:
* This patch replaced one that split off the 'new entry to insert' behaviour.

---
 arch/x86/kernel/cpu/resctrl/core.c        | 38 +++--------------------
 arch/x86/kernel/cpu/resctrl/ctrlmondata.c | 24 +++++++++++++-
 arch/x86/kernel/cpu/resctrl/internal.h    |  2 --
 include/linux/resctrl.h                   | 14 +++++++++
 4 files changed, 41 insertions(+), 37 deletions(-)
arch/x86/kernel/cpu/resctrl/core.c +4 -34
index ce50a7d3230c..26ae43659c6c 100644
@@ -396,36 +396,6 @@ void rdt_ctrl_update(void *arg)
 	hw_res->msr_update(m);
 }
 
-/*
- * rdt_find_domain - Search for a domain id in a resource domain list.
- *
- * Search the domain list to find the domain id. If the domain id is
- * found, return the domain. NULL otherwise.  If the domain id is not
- * found (and NULL returned) then the first domain with id bigger than
- * the input id can be returned to the caller via @pos.
- */
-struct rdt_domain_hdr *rdt_find_domain(struct list_head *h, int id,
-				       struct list_head **pos)
-{
-	struct rdt_domain_hdr *d;
-	struct list_head *l;
-
-	list_for_each(l, h) {
-		d = list_entry(l, struct rdt_domain_hdr, list);
-		/* When id is found, return its domain. */
-		if (id == d->id)
-			return d;
-		/* Stop searching when finding id's position in sorted list. */
-		if (id < d->id)
-			break;
-	}
-
-	if (pos)
-		*pos = l;
-
-	return NULL;
-}
-
 static void setup_default_ctrlval(struct rdt_resource *r, u32 *dc)
 {
 	struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
@@ -536,7 +506,7 @@ static void domain_add_cpu_ctrl(int cpu, struct rdt_resource *r)
 		return;
 	}
 
-	hdr = rdt_find_domain(&r->ctrl_domains, id, &add_pos);
+	hdr = resctrl_find_domain(&r->ctrl_domains, id, &add_pos);
 	if (hdr) {
 		if (WARN_ON_ONCE(hdr->type != RESCTRL_CTRL_DOMAIN))
 			return;
@@ -591,7 +561,7 @@ static void domain_add_cpu_mon(int cpu, struct rdt_resource *r)
 		return;
 	}
 
-	hdr = rdt_find_domain(&r->mon_domains, id, &add_pos);
+	hdr = resctrl_find_domain(&r->mon_domains, id, &add_pos);
 	if (hdr) {
 		if (WARN_ON_ONCE(hdr->type != RESCTRL_MON_DOMAIN))
 			return;
@@ -656,7 +626,7 @@ static void domain_remove_cpu_ctrl(int cpu, struct rdt_resource *r)
 		return;
 	}
 
-	hdr = rdt_find_domain(&r->ctrl_domains, id, NULL);
+	hdr = resctrl_find_domain(&r->ctrl_domains, id, NULL);
 	if (!hdr) {
 		pr_warn("Can't find control domain for id=%d for CPU %d for resource %s\n",
 			id, cpu, r->name);
@@ -702,7 +672,7 @@ static void domain_remove_cpu_mon(int cpu, struct rdt_resource *r)
 		return;
 	}
 
-	hdr = rdt_find_domain(&r->mon_domains, id, NULL);
+	hdr = resctrl_find_domain(&r->mon_domains, id, NULL);
 	if (!hdr) {
 		pr_warn("Can't find monitor domain for id=%d for CPU %d for resource %s\n",
 			id, cpu, r->name);
arch/x86/kernel/cpu/resctrl/ctrlmondata.c +23 -1
index 5d87f279085f..763317ea2256 100644
@@ -610,6 +610,28 @@ int rdtgroup_mba_mbps_event_show(struct kernfs_open_file *of,
 	return ret;
 }
 
+struct rdt_domain_hdr *resctrl_find_domain(struct list_head *h, int id,
+					   struct list_head **pos)
+{
+	struct rdt_domain_hdr *d;
+	struct list_head *l;
+
+	list_for_each(l, h) {
+		d = list_entry(l, struct rdt_domain_hdr, list);
+		/* When id is found, return its domain. */
+		if (id == d->id)
+			return d;
+		/* Stop searching when finding id's position in sorted list. */
+		if (id < d->id)
+			break;
+	}
+
+	if (pos)
+		*pos = l;
+
+	return NULL;
+}
+
 void mon_event_read(struct rmid_read *rr, struct rdt_resource *r,
 		    struct rdt_mon_domain *d, struct rdtgroup *rdtgrp,
 		    cpumask_t *cpumask, int evtid, int first)
@@ -695,7 +717,7 @@ int rdtgroup_mondata_show(struct seq_file *m, void *arg)
 		 * This file provides data from a single domain. Search
 		 * the resource to find the domain with "domid".
 		 */
-		hdr = rdt_find_domain(&r->mon_domains, domid, NULL);
+		hdr = resctrl_find_domain(&r->mon_domains, domid, NULL);
 		if (!hdr || WARN_ON_ONCE(hdr->type != RESCTRL_MON_DOMAIN)) {
 			ret = -ENOENT;
 			goto out;
arch/x86/kernel/cpu/resctrl/internal.h +0 -2
index 8291f1b59981..da73404183da 100644
@@ -581,8 +581,6 @@ void rdtgroup_kn_unlock(struct kernfs_node *kn);
 int rdtgroup_kn_mode_restrict(struct rdtgroup *r, const char *name);
 int rdtgroup_kn_mode_restore(struct rdtgroup *r, const char *name,
 			     umode_t mask);
-struct rdt_domain_hdr *rdt_find_domain(struct list_head *h, int id,
-				       struct list_head **pos);
 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,
include/linux/resctrl.h +14 -0
index f1979e375da9..93d9a435f035 100644
@@ -372,6 +372,20 @@ static inline void resctrl_arch_rmid_read_context_check(void)
 		might_sleep();
 }
 
+/**
+ * resctrl_find_domain() - Search for a domain id in a resource domain list.
+ * @h:		The domain list to search.
+ * @id:		The domain id to search for.
+ * @pos:	A pointer to position in the list id should be inserted.
+ *
+ * Search the domain list to find the domain id. If the domain id is
+ * found, return the domain. NULL otherwise.  If the domain id is not
+ * found (and NULL returned) then the first domain with id bigger than
+ * the input id can be returned to the caller via @pos.
+ */
+struct rdt_domain_hdr *resctrl_find_domain(struct list_head *h, int id,
+					   struct list_head **pos);
+
 /**
  * resctrl_arch_reset_rmid() - Reset any private state associated with rmid
  *			       and eventid.
Signature · 1 line
2.39.5
Reviewed-by on v7 12/49
#26 lore raw

Re: [PATCH v7 12/49] x86/resctrl: Move rdt_find_domain() to be visible to arch and fs code

Quoted 6 lines · James Morse

rdt_find_domain() finds a domain given a resource and a cache-id. This is used by both the architecture code and the filesystem code.

After the filesystem code moves to live in /fs/, this helper is either duplicated by all architectures, or needs exposing by the filesystem code.

Quoted 6 lines

Add the definition to the global header file. As its now globally visible, and has only a handful of callers, swap the 'rdt' for 'resctrl'. Move the function to live with its caller in ctrlmondata.c as the filesystem code will not have anything corresponding to core.c.

Thanks.

-Fenghua

maintainer
#27 lore raw

Re: [PATCH v7 12/49] x86/resctrl: Move rdt_find_domain() to be visible to arch and fs code

In reply to James Morse

Hi James,

Quoted 7 lines · James Morse

rdt_find_domain() finds a domain given a resource and a cache-id. This is used by both the architecture code and the filesystem code.

After the filesystem code moves to live in /fs/, this helper is either duplicated by all architectures, or needs exposing by the filesystem code.

Add the definition to the global header file. As its now globally visible,

"definition" -> "declaration"?

Quoted 7 lines

and has only a handful of callers, swap the 'rdt' for 'resctrl'. Move the function to live with its caller in ctrlmondata.c as the filesystem code will not have anything corresponding to core.c.

---

| Reviewed-by: Reinette Chatre <[email protected]>

Reinette

author reviewer
#28 lore raw

Re: [PATCH v7 12/49] x86/resctrl: Move rdt_find_domain() to be visible to arch and fs code

Hi Reinette,

Quoted 10 lines · Reinette Chatre
Quoted 7 lines · James Morse

rdt_find_domain() finds a domain given a resource and a cache-id. This is used by both the architecture code and the filesystem code.

After the filesystem code moves to live in /fs/, this helper is either duplicated by all architectures, or needs exposing by the filesystem code.

Add the definition to the global header file. As its now globally visible,

"definition" -> "declaration"?

Ugh, I always get those the wrong way round.

Quoted 6 lines
Quoted 3 lines

and has only a handful of callers, swap the 'rdt' for 'resctrl'. Move the function to live with its caller in ctrlmondata.c as the filesystem code will not have anything corresponding to core.c.

| Reviewed-by: Reinette Chatre <[email protected]>

Thanks!

James

author reviewer
#29 lore raw

PATCH v7 13/49 x86/resctrl: Move resctrl types to a separate header

In reply to James Morse

When resctrl is fully factored into core and per-arch code, each arch will need to use some resctrl common definitions in order to define its own specializations and helpers. Following conventional practice, it would be desirable to put the dependent arch definitions in an <asm/resctrl.h> header that is included by the common <linux/resctrl.h> header. However, this can make it awkward to avoid a circular dependency between <linux/resctrl.h> and the arch header.

To avoid such dependencies, move the affected common types and constants into a new header that does not need to depend on <linux/resctrl.h> or on the arch headers.

The same logic applies to the monitor-configuration defines, move these too.

Some kind of enumeration for events is needed between the filesystem and architecture code. Take the x86 definition as its convenient for x86.

The definition of enum resctrl_event_id is needed to allow the architecture code to define resctrl_arch_mon_ctx_alloc() and resctrl_arch_mon_ctx_free().

The definition of enum resctrl_res_level is needed to allow the architecture code to define resctrl_arch_set_cdp_enabled() and resctrl_arch_get_cdp_enabled().

The bits for mbm_local_bytes_config et al are ABI, and must be the same on all architectures. These are documented in Documentation/arch/x86/resctrl.rst

The maintainers entry for these headers was missed when resctrl.h was created. Add a wildcard entry to match both resctrl.h and resctrl_types.h.

---
Changes since v6:
* Used a different year in the boiler-plate copyright notice.

Change since v3:
* Added header include.
* Corrected lists in the commit message.

Changes since v2:
* Added to the commit message why each of these things is necessary.
* Moved the enum resctrl_conf_type back to resctrl.h - this week arm's CDP emulation code gets away without this...

Changes since v1:
* [Commit message only] Rewrite commit message to clarify the the rationale for refactoring the headers in this way.

---
 MAINTAINERS                            |  1 +
 arch/x86/include/asm/resctrl.h         |  1 +
 arch/x86/kernel/cpu/resctrl/internal.h | 24 ------------
 include/linux/resctrl.h                | 21 +---------
 include/linux/resctrl_types.h          | 54 ++++++++++++++++++++++++++
 5 files changed, 57 insertions(+), 44 deletions(-)
 create mode 100644 include/linux/resctrl_types.h
MAINTAINERS +1 -0
index efee40ea589f..3487ac429a9a 100644
@@ -19881,6 +19881,7 @@ S:	Supported
 F:	Documentation/arch/x86/resctrl*
 F:	arch/x86/include/asm/resctrl.h
 F:	arch/x86/kernel/cpu/resctrl/
+F:	include/linux/resctrl*.h
 F:	tools/testing/selftests/resctrl/
 
 READ-COPY UPDATE (RCU)
arch/x86/include/asm/resctrl.h +1 -0
index 6908cd0e6e40..52f2326e2b1e 100644
@@ -6,6 +6,7 @@
 
 #include <linux/jump_label.h>
 #include <linux/percpu.h>
+#include <linux/resctrl_types.h>
 #include <linux/sched.h>
 
 /*
arch/x86/kernel/cpu/resctrl/internal.h +0 -24
index da73404183da..5f3713fb2eaf 100644
@@ -32,30 +32,6 @@
  */
 #define MBM_CNTR_WIDTH_OFFSET_MAX (62 - MBM_CNTR_WIDTH_BASE)
 
-/* Reads to Local DRAM Memory */
-#define READS_TO_LOCAL_MEM		BIT(0)
-
-/* Reads to Remote DRAM Memory */
-#define READS_TO_REMOTE_MEM		BIT(1)
-
-/* Non-Temporal Writes to Local Memory */
-#define NON_TEMP_WRITE_TO_LOCAL_MEM	BIT(2)
-
-/* Non-Temporal Writes to Remote Memory */
-#define NON_TEMP_WRITE_TO_REMOTE_MEM	BIT(3)
-
-/* Reads to Local Memory the system identifies as "Slow Memory" */
-#define READS_TO_LOCAL_S_MEM		BIT(4)
-
-/* Reads to Remote Memory the system identifies as "Slow Memory" */
-#define READS_TO_REMOTE_S_MEM		BIT(5)
-
-/* Dirty Victims to All Types of Memory */
-#define DIRTY_VICTIMS_TO_ALL_MEM	BIT(6)
-
-/* Max event bits supported */
-#define MAX_EVT_CONFIG_BITS		GENMASK(6, 0)
-
 /**
  * cpumask_any_housekeeping() - Choose any CPU in @mask, preferring those that
  *			        aren't marked nohz_full
include/linux/resctrl.h +1 -20
index 93d9a435f035..326f7ba220e7 100644
@@ -6,6 +6,7 @@
 #include <linux/kernel.h>
 #include <linux/list.h>
 #include <linux/pid.h>
+#include <linux/resctrl_types.h>
 
 /* CLOSID, RMID value used by the default control group */
 #define RESCTRL_RESERVED_CLOSID		0
@@ -37,28 +38,8 @@ enum resctrl_conf_type {
 	CDP_DATA,
 };
 
-enum resctrl_res_level {
-	RDT_RESOURCE_L3,
-	RDT_RESOURCE_L2,
-	RDT_RESOURCE_MBA,
-	RDT_RESOURCE_SMBA,
-
-	/* Must be the last */
-	RDT_NUM_RESOURCES,
-};
-
 #define CDP_NUM_TYPES	(CDP_DATA + 1)
 
-/*
- * Event IDs, the values match those used to program IA32_QM_EVTSEL before
- * reading IA32_QM_CTR on RDT systems.
- */
-enum resctrl_event_id {
-	QOS_L3_OCCUP_EVENT_ID		= 0x01,
-	QOS_L3_MBM_TOTAL_EVENT_ID	= 0x02,
-	QOS_L3_MBM_LOCAL_EVENT_ID	= 0x03,
-};
-
 /**
  * struct resctrl_staged_config - parsed configuration to be applied
  * @new_ctrl:		new ctrl value to be loaded
include/linux/resctrl_types.h new file +54 -0

54 lines not on this page · raw message

Signature · 1 line
2.39.5
Reviewed-by on v7 13/49
#30 lore raw

Re: [PATCH v7 13/49] x86/resctrl: Move resctrl types to a separate header

Quoted 35 lines · James Morse

When resctrl is fully factored into core and per-arch code, each arch will need to use some resctrl common definitions in order to define its own specializations and helpers. Following conventional practice, it would be desirable to put the dependent arch definitions in an <asm/resctrl.h> header that is included by the common <linux/resctrl.h> header. However, this can make it awkward to avoid a circular dependency between <linux/resctrl.h> and the arch header.

To avoid such dependencies, move the affected common types and constants into a new header that does not need to depend on <linux/resctrl.h> or on the arch headers.

The same logic applies to the monitor-configuration defines, move these too.

Some kind of enumeration for events is needed between the filesystem and architecture code. Take the x86 definition as its convenient for x86.

The definition of enum resctrl_event_id is needed to allow the architecture code to define resctrl_arch_mon_ctx_alloc() and resctrl_arch_mon_ctx_free().

The definition of enum resctrl_res_level is needed to allow the architecture code to define resctrl_arch_set_cdp_enabled() and resctrl_arch_get_cdp_enabled().

The bits for mbm_local_bytes_config et al are ABI, and must be the same on all architectures. These are documented in Documentation/arch/x86/resctrl.rst

The maintainers entry for these headers was missed when resctrl.h was created. Add a wildcard entry to match both resctrl.h and resctrl_types.h.

Quoted 6 lines

Thanks.

-Fenghua

author reviewer
#31 lore raw

PATCH v7 14/49 x86/resctrl: Add an arch helper to reset one resource

In reply to James Morse

On umount(), resctrl resets each resource back to its default configuration. It only ever does this for all resources in one go.

reset_all_ctrls() is architecture specific as it works with struct rdt_hw_resource.

Make reset_all_ctrls() an arch helper that resets one resource.

---
Changes since v5:
* Move the arch/fs split into the for-each loop at Reinette's suggestion.
* Dropped a bunch of tags and rewrote the commit message.

Changes since v1:
* Rename the for_each_capable_rdt_resource() introduced in the new function resctrl_arch_reset_resources(), back to for_each_alloc_capable_rdt_resource() as it was in the original code.

The change looked unintentional; and presumably a resource that does not support resource allocation doesn't have any properties to reset...

---
 arch/x86/kernel/cpu/resctrl/rdtgroup.c | 9 +++++----
 include/linux/resctrl.h                | 9 +++++++++
 2 files changed, 14 insertions(+), 4 deletions(-)
arch/x86/kernel/cpu/resctrl/rdtgroup.c +5 -4
index b2dad689e780..9eb57ebb36c6 100644
@@ -2867,7 +2867,7 @@ static int rdt_init_fs_context(struct fs_context *fc)
 	return 0;
 }
 
-static int reset_all_ctrls(struct rdt_resource *r)
+void resctrl_arch_reset_all_ctrls(struct rdt_resource *r)
 {
 	struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
 	struct rdt_hw_ctrl_domain *hw_dom;
@@ -2896,7 +2896,7 @@ static int reset_all_ctrls(struct rdt_resource *r)
 		smp_call_function_any(&d->hdr.cpu_mask, rdt_ctrl_update, &msr_param, 1);
 	}
 
-	return 0;
+	return;
 }
 
 /*
@@ -3015,9 +3015,10 @@ static void rdt_kill_sb(struct super_block *sb)
 
 	rdt_disable_ctx();
 
-	/*Put everything back to default values. */
+	/* Put everything back to default values. */
 	for_each_alloc_capable_rdt_resource(r)
-		reset_all_ctrls(r);
+		resctrl_arch_reset_all_ctrls(r);
+
 	rmdir_all_sub();
 	rdt_pseudo_lock_release();
 	rdtgroup_default.mode = RDT_MODE_SHAREABLE;
include/linux/resctrl.h +9 -0
index 326f7ba220e7..487b9657a7b5 100644
@@ -394,6 +394,15 @@ void resctrl_arch_reset_rmid(struct rdt_resource *r, struct rdt_mon_domain *d,
  */
 void resctrl_arch_reset_rmid_all(struct rdt_resource *r, struct rdt_mon_domain *d);
 
+/**
+ * resctrl_arch_reset_all_ctrls() - Reset the control for each CLOSID to its
+ *				    default.
+ * @r:		The resctrl resource to reset.
+ *
+ * This can be called from any CPU.
+ */
+void resctrl_arch_reset_all_ctrls(struct rdt_resource *r);
+
 extern unsigned int resctrl_rmid_realloc_threshold;
 extern unsigned int resctrl_rmid_realloc_limit;
 
Signature · 1 line
2.39.5
Reviewed-by on v7 14/49
#32 lore raw

Re: [PATCH v7 14/49] x86/resctrl: Add an arch helper to reset one resource

Quoted 11 lines · James Morse

On umount(), resctrl resets each resource back to its default configuration. It only ever does this for all resources in one go.

reset_all_ctrls() is architecture specific as it works with struct rdt_hw_resource.

Make reset_all_ctrls() an arch helper that resets one resource.

Thanks.

-Fenghua

author reviewer
#33 lore raw

PATCH v7 15/49 x86/resctrl: Move monitor exit work to a resctrl exit call

In reply to James Morse

rdt_put_mon_l3_config() is called via the architecture's resctrl_arch_exit() call, and appears to free the rmid_ptrs[] and closid_num_dirty_rmid[] arrays. In reality this code is marked __exit, and is removed by the linker as resctrl can't be built as a module.

To separate the filesystem and architecture parts of resctrl, this free()ing work needs to be triggered by the filesystem, as these structures belong to the filesystem code.

Rename rdt_put_mon_l3_config() resctrl_mon_resource_exit() and call it from resctrl_exit(). The kfree() is currently dependent on r->mon_capable.

---
Changes since v4:
* Added __exit so it can be removed in the next patch.

Changes since v3:
* Moved r->mon_capable check under the lock.
* Dropped references to resctrl_mon_resource_init() from the commit message.
* Fixed more resctrl typos,

Changes since v2:
* Dropped __exit as needed in the next patch.

Change since v1:
* [Commit message only] Typo fixes:
s/restrl/resctrl/g
s/resctl/resctrl/g

* [Commit message only] Reword second paragraph to remove reference to the MPAM error interrupt, which provides background rationale for a later patch rather than for this patch, and so it is not really relevant here.

---
 arch/x86/kernel/cpu/resctrl/core.c     |  5 -----
 arch/x86/kernel/cpu/resctrl/internal.h |  2 +-
 arch/x86/kernel/cpu/resctrl/monitor.c  | 12 +++++++++---
 arch/x86/kernel/cpu/resctrl/rdtgroup.c |  2 ++
 4 files changed, 12 insertions(+), 9 deletions(-)
arch/x86/kernel/cpu/resctrl/core.c +0 -5
index 26ae43659c6c..d04d65754491 100644
@@ -1076,14 +1076,9 @@ late_initcall(resctrl_arch_late_init);
 
 static void __exit resctrl_arch_exit(void)
 {
-	struct rdt_resource *r = &rdt_resources_all[RDT_RESOURCE_L3].r_resctrl;
-
 	cpuhp_remove_state(rdt_online);
 
 	resctrl_exit();
-
-	if (r->mon_capable)
-		rdt_put_mon_l3_config();
 }
 
 __exitcall(resctrl_arch_exit);
arch/x86/kernel/cpu/resctrl/internal.h +1 -1
index 5f3713fb2eaf..73005ca2dda1 100644
@@ -586,7 +586,7 @@ void closid_free(int closid);
 int alloc_rmid(u32 closid);
 void free_rmid(u32 closid, u32 rmid);
 int rdt_get_mon_l3_config(struct rdt_resource *r);
-void __exit rdt_put_mon_l3_config(void);
+void __exit resctrl_mon_resource_exit(void);
 bool __init rdt_cpu_has(int flag);
 void mon_event_count(void *info);
 int rdtgroup_mondata_show(struct seq_file *m, void *arg);
arch/x86/kernel/cpu/resctrl/monitor.c +9 -3
index e8388d19a579..15e8c0190bfc 100644
@@ -1040,10 +1040,13 @@ static int dom_data_init(struct rdt_resource *r)
 	return err;
 }
 
-static void __exit dom_data_exit(void)
+static void __exit dom_data_exit(struct rdt_resource *r)
 {
 	mutex_lock(&rdtgroup_mutex);
 
+	if (!r->mon_capable)
+		goto out_unlock;
+
 	if (IS_ENABLED(CONFIG_RESCTRL_RMID_DEPENDS_ON_CLOSID)) {
 		kfree(closid_num_dirty_rmid);
 		closid_num_dirty_rmid = NULL;
@@ -1052,6 +1055,7 @@ static void __exit dom_data_exit(void)
 	kfree(rmid_ptrs);
 	rmid_ptrs = NULL;
 
+out_unlock:
 	mutex_unlock(&rdtgroup_mutex);
 }
 
@@ -1237,9 +1241,11 @@ int __init rdt_get_mon_l3_config(struct rdt_resource *r)
 	return 0;
 }
 
-void __exit rdt_put_mon_l3_config(void)
+void __exit resctrl_mon_resource_exit(void)
 {
-	dom_data_exit();
+	struct rdt_resource *r = resctrl_arch_get_resource(RDT_RESOURCE_L3);
+
+	dom_data_exit(r);
 }
 
 void __init intel_rdt_mbm_apply_quirk(void)
arch/x86/kernel/cpu/resctrl/rdtgroup.c +2 -0
index 9eb57ebb36c6..42c48e79364d 100644
@@ -4296,4 +4296,6 @@ void __exit resctrl_exit(void)
 	debugfs_remove_recursive(debugfs_resctrl);
 	unregister_filesystem(&rdt_fs_type);
 	sysfs_remove_mount_point(fs_kobj, "resctrl");
+
+	resctrl_mon_resource_exit();
 }
Signature · 1 line
2.39.5
Reviewed-by on v7 15/49
#34 lore raw

Re: [PATCH v7 15/49] x86/resctrl: Move monitor exit work to a resctrl exit call

Quoted 14 lines · James Morse

rdt_put_mon_l3_config() is called via the architecture's resctrl_arch_exit() call, and appears to free the rmid_ptrs[] and closid_num_dirty_rmid[] arrays. In reality this code is marked __exit, and is removed by the linker as resctrl can't be built as a module.

To separate the filesystem and architecture parts of resctrl, this free()ing work needs to be triggered by the filesystem, as these structures belong to the filesystem code.

Rename rdt_put_mon_l3_config() resctrl_mon_resource_exit() and call it from resctrl_exit(). The kfree() is currently dependent on r->mon_capable.

Quoted 6 lines

Thanks.

-Fenghua

author reviewer
#35 lore raw

PATCH v7 16/49 x86/resctrl: Move monitor init work to a resctrl init call

In reply to James Morse

rdt_get_mon_l3_config() is called from the architecture's resctrl_arch_late_init(), and initialises both architecture specific fields, such as hw_res->mon_scale and resctrl filesystem fields by calling dom_data_init().

To separate the filesystem and architecture parts of resctrl, this function needs splitting up.

Add resctrl_mon_resource_init() to do the filesystem specific work, and call it from resctrl_init(). This runs later, but is still before the filesystem is mounted and the rmid_ptrs[] array can be used.

---
Changes since v4:
* Removed __exit markers

Changes since v3:
* Added a comment over resctrl_mon_resource_init().
* Added a comment over domain_setup_mon_state() to warn of cpuhp ordering.
* Added __init to resctrl_mon_resource_init().

Changes since v2:
* Added error handling for the case sysfs files can't be created.

---
 arch/x86/kernel/cpu/resctrl/internal.h |  3 +-
 arch/x86/kernel/cpu/resctrl/monitor.c  | 40 ++++++++++++++++++++------
 arch/x86/kernel/cpu/resctrl/rdtgroup.c | 22 +++++++++++++-
 3 files changed, 54 insertions(+), 11 deletions(-)
arch/x86/kernel/cpu/resctrl/internal.h +2 -1
index 73005ca2dda1..70fbb902e85e 100644
@@ -586,13 +586,14 @@ void closid_free(int closid);
 int alloc_rmid(u32 closid);
 void free_rmid(u32 closid, u32 rmid);
 int rdt_get_mon_l3_config(struct rdt_resource *r);
-void __exit resctrl_mon_resource_exit(void);
+void resctrl_mon_resource_exit(void);
 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_mon_domain *d, struct rdtgroup *rdtgrp,
 		    cpumask_t *cpumask, int evtid, int first);
+int __init resctrl_mon_resource_init(void);
 void mbm_setup_overflow_handler(struct rdt_mon_domain *dom,
 				unsigned long delay_ms,
 				int exclude_cpu);
arch/x86/kernel/cpu/resctrl/monitor.c +31 -9
index 15e8c0190bfc..1730ba814834 100644
@@ -1040,7 +1040,7 @@ static int dom_data_init(struct rdt_resource *r)
 	return err;
 }
 
-static void __exit dom_data_exit(struct rdt_resource *r)
+static void dom_data_exit(struct rdt_resource *r)
 {
 	mutex_lock(&rdtgroup_mutex);
 
@@ -1176,12 +1176,40 @@ static __init int snc_get_config(void)
 	return ret;
 }
 
+/**
+ * resctrl_mon_resource_init() - Initialise global monitoring structures.
+ *
+ * Allocate and initialise global monitor resources that do not belong to a
+ * specific domain. i.e. the rmid_ptrs[] used for the limbo and free lists.
+ * Called once during boot after the struct rdt_resource's have been configured
+ * but before the filesystem is mounted.
+ * Resctrl's cpuhp callbacks may be called before this point to bring a domain
+ * online.
+ *
+ * Returns 0 for success, or -ENOMEM.
+ */
+int __init resctrl_mon_resource_init(void)
+{
+	struct rdt_resource *r = resctrl_arch_get_resource(RDT_RESOURCE_L3);
+	int ret;
+
+	if (!r->mon_capable)
+		return 0;
+
+	ret = dom_data_init(r);
+	if (ret)
+		return ret;
+
+	l3_mon_evt_init(r);
+
+	return 0;
+}
+
 int __init rdt_get_mon_l3_config(struct rdt_resource *r)
 {
 	unsigned int mbm_offset = boot_cpu_data.x86_cache_mbm_width_offset;
 	struct rdt_hw_resource *hw_res = resctrl_to_arch_res(r);
 	unsigned int threshold;
-	int ret;
 
 	snc_nodes_per_l3_cache = snc_get_config();
 
@@ -1211,10 +1239,6 @@ int __init rdt_get_mon_l3_config(struct rdt_resource *r)
 	 */
 	resctrl_rmid_realloc_threshold = resctrl_arch_round_mon_val(threshold);
 
-	ret = dom_data_init(r);
-	if (ret)
-		return ret;
-
 	if (rdt_cpu_has(X86_FEATURE_BMEC)) {
 		u32 eax, ebx, ecx, edx;
 
@@ -1234,14 +1258,12 @@ int __init rdt_get_mon_l3_config(struct rdt_resource *r)
 		}
 	}
 
-	l3_mon_evt_init(r);
-
 	r->mon_capable = true;
 
 	return 0;
 }
 
-void __exit resctrl_mon_resource_exit(void)
+void resctrl_mon_resource_exit(void)
 {
 	struct rdt_resource *r = resctrl_arch_get_resource(RDT_RESOURCE_L3);
 
arch/x86/kernel/cpu/resctrl/rdtgroup.c +21 -1
index 42c48e79364d..badac3f5da72 100644
@@ -4102,6 +4102,19 @@ void resctrl_offline_mon_domain(struct rdt_resource *r, struct rdt_mon_domain *d
 	mutex_unlock(&rdtgroup_mutex);
 }
 
+/**
+ * domain_setup_mon_state() -  Initialise domain monitoring structures.
+ * @r:	The resource for the newly online domain.
+ * @d:	The newly online domain.
+ *
+ * Allocate monitor resources that belong to this domain.
+ * Called when the first CPU of a domain comes online, regardless of whether
+ * the filesystem is mounted.
+ * During boot this may be called before global allocations have been made by
+ * resctrl_mon_resource_init().
+ *
+ * Returns 0 for success, or -ENOMEM.
+ */
 static int domain_setup_mon_state(struct rdt_resource *r, struct rdt_mon_domain *d)
 {
 	u32 idx_limit = resctrl_arch_system_num_rmid_idx();
@@ -4252,9 +4265,15 @@ int __init resctrl_init(void)
 
 	rdtgroup_setup_default();
 
+	ret = resctrl_mon_resource_init();
+	if (ret)
+		return ret;
+
 	ret = sysfs_create_mount_point(fs_kobj, "resctrl");
-	if (ret)
+	if (ret) {
+		resctrl_mon_resource_exit();
 		return ret;
+	}
 
 	ret = register_filesystem(&rdt_fs_type);
 	if (ret)
@@ -4287,6 +4306,7 @@ int __init resctrl_init(void)
 
 cleanup_mountpoint:
 	sysfs_remove_mount_point(fs_kobj, "resctrl");
+	resctrl_mon_resource_exit();
 
 	return ret;
 }
Signature · 1 line
2.39.5
Reviewed-by on v7 16/49
#36 lore raw

Re: [PATCH v7 16/49] x86/resctrl: Move monitor init work to a resctrl init call

Quoted 12 lines · James Morse

rdt_get_mon_l3_config() is called from the architecture's resctrl_arch_late_init(), and initialises both architecture specific fields, such as hw_res->mon_scale and resctrl filesystem fields by calling dom_data_init().

To separate the filesystem and architecture parts of resctrl, this function needs splitting up.

Add resctrl_mon_resource_init() to do the filesystem specific work, and call it from resctrl_init(). This runs later, but is still before the filesystem is mounted and the rmid_ptrs[] array can be used.

Quoted 6 lines

Thanks.

-Fenghua

author reviewer
#37 lore raw

PATCH v7 17/49 x86/resctrl: Rewrite and move the for_each_*_rdt_resource() walkers

In reply to James Morse

The for_each_*_rdt_resource() helpers walk the architecture's array of structures, using the resctrl visible part as an iterator. These became over-complex when the structures were split into a filesystem and architecture-specific struct. This approach avoided the need to touch every call site, and was done before there was a helper to retrieve a resource by rid.

Once the filesystem parts of resctrl are moved to /fs/, both the architecture's resource array, and the definition of those structures is no longer accessible. To support resctrl, each architecture would have to provide equally complex macros.

Rewrite the macro to make use of resctrl_arch_get_resource(), and move these to include/linux/resctrl.h so existing x86 arch code continues to use them.

---
Changes since v5:
* Fixed an off by one in for_each_rdt_resource().
* Added header file path to commit message.

Changes since v3:
* Restructure the existing macros instead of open-coding the for loop.

Changes since v1:
* [Whitespace only] Fix bogus whitespace introduced in rdtgroup_create_info_dir().

* [Commit message only] Typo fix:
s/architectures/architecture's/g

---
 arch/x86/kernel/cpu/resctrl/internal.h | 29 --------------------------
 include/linux/resctrl.h                | 18 ++++++++++++++++
 2 files changed, 18 insertions(+), 29 deletions(-)
arch/x86/kernel/cpu/resctrl/internal.h +0 -29
index 70fbb902e85e..82dbc1606663 100644
@@ -475,14 +475,6 @@ extern struct rdtgroup rdtgroup_default;
 extern struct dentry *debugfs_resctrl;
 extern enum resctrl_event_id mba_mbps_default_event;
 
-static inline struct rdt_resource *resctrl_inc(struct rdt_resource *res)
-{
-	struct rdt_hw_resource *hw_res = resctrl_to_arch_res(res);
-
-	hw_res++;
-	return &hw_res->r_resctrl;
-}
-
 static inline bool resctrl_arch_get_cdp_enabled(enum resctrl_res_level l)
 {
 	return rdt_resources_all[l].cdp_enabled;
@@ -492,27 +484,6 @@ int resctrl_arch_set_cdp_enabled(enum resctrl_res_level l, bool enable);
 
 void arch_mon_domain_online(struct rdt_resource *r, struct rdt_mon_domain *d);
 
-/*
- * To return the common struct rdt_resource, which is contained in struct
- * rdt_hw_resource, walk the resctrl member of struct rdt_hw_resource.
- */
-#define for_each_rdt_resource(r)					      \
-	for (r = &rdt_resources_all[0].r_resctrl;			      \
-	     r <= &rdt_resources_all[RDT_NUM_RESOURCES - 1].r_resctrl;	      \
-	     r = resctrl_inc(r))
-
-#define for_each_capable_rdt_resource(r)				      \
-	for_each_rdt_resource(r)					      \
-		if (r->alloc_capable || r->mon_capable)
-
-#define for_each_alloc_capable_rdt_resource(r)				      \
-	for_each_rdt_resource(r)					      \
-		if (r->alloc_capable)
-
-#define for_each_mon_capable_rdt_resource(r)				      \
-	for_each_rdt_resource(r)					      \
-		if (r->mon_capable)
-
 /* CPUID.(EAX=10H, ECX=ResID=1).EAX */
 union cpuid_0x10_1_eax {
 	struct {
include/linux/resctrl.h +18 -0
index 487b9657a7b5..a392480dc4b6 100644
@@ -26,6 +26,24 @@ int proc_resctrl_show(struct seq_file *m,
 /* max value for struct rdt_domain's mbps_val */
 #define MBA_MAX_MBPS   U32_MAX
 
+/* Walk all possible resources, with variants for only controls or monitors. */
+#define for_each_rdt_resource(_r)						\
+	for ((_r) = resctrl_arch_get_resource(0);				\
+	     (_r) && (_r)->rid < RDT_NUM_RESOURCES;				\
+	     (_r) = resctrl_arch_get_resource((_r)->rid + 1))
+
+#define for_each_capable_rdt_resource(r)				      \
+	for_each_rdt_resource((r))					      \
+		if ((r)->alloc_capable || (r)->mon_capable)
+
+#define for_each_alloc_capable_rdt_resource(r)				      \
+	for_each_rdt_resource((r))					      \
+		if ((r)->alloc_capable)
+
+#define for_each_mon_capable_rdt_resource(r)				      \
+	for_each_rdt_resource((r))					      \
+		if ((r)->mon_capable)
+
 /**
  * enum resctrl_conf_type - The type of configuration.
  * @CDP_NONE:	No prioritisation, both code and data are controlled or monitored.
Signature · 1 line
2.39.5
Reviewed-by on v7 17/49
#38 lore raw

Re: [PATCH v7 17/49] x86/resctrl: Rewrite and move the for_each_*_rdt_resource() walkers

Quoted 16 lines · James Morse

The for_each_*_rdt_resource() helpers walk the architecture's array of structures, using the resctrl visible part as an iterator. These became over-complex when the structures were split into a filesystem and architecture-specific struct. This approach avoided the need to touch every call site, and was done before there was a helper to retrieve a resource by rid.

Once the filesystem parts of resctrl are moved to /fs/, both the architecture's resource array, and the definition of those structures is no longer accessible. To support resctrl, each architecture would have to provide equally complex macros.

Rewrite the macro to make use of resctrl_arch_get_resource(), and move these to include/linux/resctrl.h so existing x86 arch code continues to use them.

Quoted 5 lines

Thanks.

-Fenghua

author reviewer
#39 lore raw

PATCH v7 18/49 x86/resctrl: Move the is_mbm_*_enabled() helpers to asm/resctrl.h

In reply to James Morse

The architecture specific parts of resctrl provide helpers like is_mbm_total_enabled() and is_mbm_local_enabled() to hide accesses to the rdt_mon_features bitmap.

Exposing a group of helpers between the architecture and filesystem code is preferable to a single unsigned-long like rdt_mon_features. Helpers can be more readable and have a well defined behaviour, while allowing architectures to hide more complex behaviour.

Once the filesystem parts of resctrl are moved, these existing helpers can no longer live in internal.h. Move them to include/linux/resctrl.h Once these are exposed to the wider kernel, they should have a 'resctrl_arch_' prefix, to fit the rest of the arch<->fs interface.

Move and rename the helpers that touch rdt_mon_features directly. is_mbm_event() and is_mbm_enabled() are only called from rdtgroup.c, so can be moved into that file.

---
Chagnes since v6:
* Dropped a spurious check re-introduced by a bad rebase conflict.

Changes since v5:
* Removed the word 'export' due to its kernel-specific meaning.
* Reworded commit message.

---
 arch/x86/include/asm/resctrl.h            | 16 +++++++++
 arch/x86/kernel/cpu/resctrl/core.c        |  8 ++---
 arch/x86/kernel/cpu/resctrl/ctrlmondata.c |  4 +--
 arch/x86/kernel/cpu/resctrl/internal.h    | 27 ---------------
 arch/x86/kernel/cpu/resctrl/monitor.c     | 16 ++++-----
 arch/x86/kernel/cpu/resctrl/rdtgroup.c    | 40 +++++++++++++++--------
 6 files changed, 56 insertions(+), 55 deletions(-)
arch/x86/include/asm/resctrl.h +16 -0
index 52f2326e2b1e..6d4c7ea2c9e3 100644
@@ -42,6 +42,7 @@ DECLARE_PER_CPU(struct resctrl_pqr_state, pqr_state);
 
 extern bool rdt_alloc_capable;
 extern bool rdt_mon_capable;
+extern unsigned int rdt_mon_features;
 
 DECLARE_STATIC_KEY_FALSE(rdt_enable_key);
 DECLARE_STATIC_KEY_FALSE(rdt_alloc_enable_key);
@@ -81,6 +82,21 @@ static inline void resctrl_arch_disable_mon(void)
 	static_branch_dec_cpuslocked(&rdt_enable_key);
 }
 
+static inline bool resctrl_arch_is_llc_occupancy_enabled(void)
+{
+	return (rdt_mon_features & (1 << QOS_L3_OCCUP_EVENT_ID));
+}
+
+static inline bool resctrl_arch_is_mbm_total_enabled(void)
+{
+	return (rdt_mon_features & (1 << QOS_L3_MBM_TOTAL_EVENT_ID));
+}
+
+static inline bool resctrl_arch_is_mbm_local_enabled(void)
+{
+	return (rdt_mon_features & (1 << QOS_L3_MBM_LOCAL_EVENT_ID));
+}
+
 /*
  * __resctrl_sched_in() - Writes the task's CLOSid/RMID to IA32_PQR_MSR
  *
arch/x86/kernel/cpu/resctrl/core.c +4 -4
index d04d65754491..5464dc1611e9 100644
@@ -455,13 +455,13 @@ static int arch_domain_mbm_alloc(u32 num_rmid, struct rdt_hw_mon_domain *hw_dom)
 {
 	size_t tsize;
 
-	if (is_mbm_total_enabled()) {
+	if (resctrl_arch_is_mbm_total_enabled()) {
 		tsize = sizeof(*hw_dom->arch_mbm_total);
 		hw_dom->arch_mbm_total = kcalloc(num_rmid, tsize, GFP_KERNEL);
 		if (!hw_dom->arch_mbm_total)
 			return -ENOMEM;
 	}
-	if (is_mbm_local_enabled()) {
+	if (resctrl_arch_is_mbm_local_enabled()) {
 		tsize = sizeof(*hw_dom->arch_mbm_local);
 		hw_dom->arch_mbm_local = kcalloc(num_rmid, tsize, GFP_KERNEL);
 		if (!hw_dom->arch_mbm_local) {
@@ -910,9 +910,9 @@ static __init bool get_rdt_mon_resources(void)
 	if (!rdt_mon_features)
 		return false;
 
-	if (is_mbm_local_enabled())
+	if (resctrl_arch_is_mbm_local_enabled())
 		mba_mbps_default_event = QOS_L3_MBM_LOCAL_EVENT_ID;
-	else if (is_mbm_total_enabled())
+	else if (resctrl_arch_is_mbm_total_enabled())
 		mba_mbps_default_event = QOS_L3_MBM_TOTAL_EVENT_ID;
 
 	return !rdt_get_mon_l3_config(r);
arch/x86/kernel/cpu/resctrl/ctrlmondata.c +2 -2
index 763317ea2256..1ecc93282b7d 100644
@@ -559,12 +559,12 @@ ssize_t rdtgroup_mba_mbps_event_write(struct kernfs_open_file *of,
 	rdt_last_cmd_clear();
 
 	if (!strcmp(buf, "mbm_local_bytes")) {
-		if (is_mbm_local_enabled())
+		if (resctrl_arch_is_mbm_local_enabled())
 			rdtgrp->mba_mbps_event = QOS_L3_MBM_LOCAL_EVENT_ID;
 		else
 			ret = -EINVAL;
 	} else if (!strcmp(buf, "mbm_total_bytes")) {
-		if (is_mbm_total_enabled())
+		if (resctrl_arch_is_mbm_total_enabled())
 			rdtgrp->mba_mbps_event = QOS_L3_MBM_TOTAL_EVENT_ID;
 		else
 			ret = -EINVAL;
arch/x86/kernel/cpu/resctrl/internal.h +0 -27
index 82dbc1606663..4a5996d1e060 100644
@@ -156,7 +156,6 @@ struct rmid_read {
 	void			*arch_mon_ctx;
 };
 
-extern unsigned int rdt_mon_features;
 extern struct list_head resctrl_schema_all;
 extern bool resctrl_mounted;
 
@@ -406,32 +405,6 @@ struct msr_param {
 	u32			high;
 };
 
-static inline bool is_llc_occupancy_enabled(void)
-{
-	return (rdt_mon_features & (1 << QOS_L3_OCCUP_EVENT_ID));
-}
-
-static inline bool is_mbm_total_enabled(void)
-{
-	return (rdt_mon_features & (1 << QOS_L3_MBM_TOTAL_EVENT_ID));
-}
-
-static inline bool is_mbm_local_enabled(void)
-{
-	return (rdt_mon_features & (1 << QOS_L3_MBM_LOCAL_EVENT_ID));
-}
-
-static inline bool is_mbm_enabled(void)
-{
-	return (is_mbm_total_enabled() || is_mbm_local_enabled());
-}
-
-static inline bool is_mbm_event(int e)
-{
-	return (e >= QOS_L3_MBM_TOTAL_EVENT_ID &&
-		e <= QOS_L3_MBM_LOCAL_EVENT_ID);
-}
-
 /**
  * struct rdt_hw_resource - arch private attributes of a resctrl resource
  * @r_resctrl:		Attributes of the resource used directly by resctrl.
arch/x86/kernel/cpu/resctrl/monitor.c +8 -8
index 1730ba814834..d883ed56ec90 100644
@@ -295,11 +295,11 @@ void resctrl_arch_reset_rmid_all(struct rdt_resource *r, struct rdt_mon_domain *
 {
 	struct rdt_hw_mon_domain *hw_dom = resctrl_to_arch_mon_dom(d);
 
-	if (is_mbm_total_enabled())
+	if (resctrl_arch_is_mbm_total_enabled())
 		memset(hw_dom->arch_mbm_total, 0,
 		       sizeof(*hw_dom->arch_mbm_total) * r->num_rmid);
 
-	if (is_mbm_local_enabled())
+	if (resctrl_arch_is_mbm_local_enabled())
 		memset(hw_dom->arch_mbm_local, 0,
 		       sizeof(*hw_dom->arch_mbm_local) * r->num_rmid);
 }
@@ -569,7 +569,7 @@ void free_rmid(u32 closid, u32 rmid)
 
 	entry = __rmid_entry(idx);
 
-	if (is_llc_occupancy_enabled())
+	if (resctrl_arch_is_llc_occupancy_enabled())
 		add_rmid_to_limbo(entry);
 	else
 		list_add_tail(&entry->list, &rmid_free_lru);
@@ -852,10 +852,10 @@ static void mbm_update(struct rdt_resource *r, struct rdt_mon_domain *d,
 	 * This is protected from concurrent reads from user as both
 	 * the user and overflow handler hold the global mutex.
 	 */
-	if (is_mbm_total_enabled())
+	if (resctrl_arch_is_mbm_total_enabled())
 		mbm_update_one_event(r, d, closid, rmid, QOS_L3_MBM_TOTAL_EVENT_ID);
 
-	if (is_mbm_local_enabled())
+	if (resctrl_arch_is_mbm_local_enabled())
 		mbm_update_one_event(r, d, closid, rmid, QOS_L3_MBM_LOCAL_EVENT_ID);
 }
 
@@ -1085,11 +1085,11 @@ static void l3_mon_evt_init(struct rdt_resource *r)
 {
 	INIT_LIST_HEAD(&r->evt_list);
 
-	if (is_llc_occupancy_enabled())
+	if (resctrl_arch_is_llc_occupancy_enabled())
 		list_add_tail(&llc_occupancy_event.list, &r->evt_list);
-	if (is_mbm_total_enabled())
+	if (resctrl_arch_is_mbm_total_enabled())
 		list_add_tail(&mbm_total_event.list, &r->evt_list);
-	if (is_mbm_local_enabled())
+	if (resctrl_arch_is_mbm_local_enabled())
 		list_add_tail(&mbm_local_event.list, &r->evt_list);
 }
 
arch/x86/kernel/cpu/resctrl/rdtgroup.c +26 -14
index badac3f5da72..eb32fbc3abea 100644
@@ -117,6 +117,18 @@ void rdt_staged_configs_clear(void)
 	}
 }
 
+static bool resctrl_is_mbm_enabled(void)
+{
+	return (resctrl_arch_is_mbm_total_enabled() ||
+		resctrl_arch_is_mbm_local_enabled());
+}
+
+static bool resctrl_is_mbm_event(int e)
+{
+	return (e >= QOS_L3_MBM_TOTAL_EVENT_ID &&
+		e <= QOS_L3_MBM_LOCAL_EVENT_ID);
+}
+
 /*
  * Trivial allocator for CLOSIDs. Since h/w only supports a small number,
  * we can keep a bitmap of free CLOSIDs in a single integer.
@@ -164,7 +176,7 @@ static int closid_alloc(void)
 	lockdep_assert_held(&rdtgroup_mutex);
 
 	if (IS_ENABLED(CONFIG_RESCTRL_RMID_DEPENDS_ON_CLOSID) &&
-	    is_llc_occupancy_enabled()) {
+	    resctrl_arch_is_llc_occupancy_enabled()) {
 		cleanest_closid = resctrl_find_cleanest_closid();
 		if (cleanest_closid < 0)
 			return cleanest_closid;
@@ -2378,7 +2390,7 @@ static bool supports_mba_mbps(void)
 	struct rdt_resource *rmbm = resctrl_arch_get_resource(RDT_RESOURCE_L3);
 	struct rdt_resource *r = resctrl_arch_get_resource(RDT_RESOURCE_MBA);
 
-	return (is_mbm_enabled() &&
+	return (resctrl_is_mbm_enabled() &&
 		r->alloc_capable && is_mba_linear() &&
 		r->ctrl_scope == rmbm->mon_scope);
 }
@@ -2756,7 +2768,7 @@ static int rdt_get_tree(struct fs_context *fc)
 	if (resctrl_arch_alloc_capable() || resctrl_arch_mon_capable())
 		resctrl_mounted = true;
 
-	if (is_mbm_enabled()) {
+	if (resctrl_is_mbm_enabled()) {
 		r = resctrl_arch_get_resource(RDT_RESOURCE_L3);
 		list_for_each_entry(dom, &r->mon_domains, hdr.list)
 			mbm_setup_overflow_handler(dom, MBM_OVERFLOW_INTERVAL,
@@ -3125,7 +3137,7 @@ static int mon_add_all_files(struct kernfs_node *kn, struct rdt_mon_domain *d,
 		if (ret)
 			return ret;
 
-		if (!do_sum && is_mbm_event(mevt->evtid))
+		if (!do_sum && resctrl_is_mbm_event(mevt->evtid))
 			mon_event_read(&rr, r, d, prgrp, &d->hdr.cpu_mask, mevt->evtid, true);
 	}
 
@@ -4082,9 +4094,9 @@ void resctrl_offline_mon_domain(struct rdt_resource *r, struct rdt_mon_domain *d
 	if (resctrl_mounted && resctrl_arch_mon_capable())
 		rmdir_mondata_subdir_allrdtgrp(r, d);
 
-	if (is_mbm_enabled())
+	if (resctrl_is_mbm_enabled())
 		cancel_delayed_work(&d->mbm_over);
-	if (is_llc_occupancy_enabled() && has_busy_rmid(d)) {
+	if (resctrl_arch_is_llc_occupancy_enabled() && has_busy_rmid(d)) {
 		/*
 		 * When a package is going down, forcefully
 		 * decrement rmid->ebusy. There is no way to know
@@ -4120,12 +4132,12 @@ static int domain_setup_mon_state(struct rdt_resource *r, struct rdt_mon_domain
 	u32 idx_limit = resctrl_arch_system_num_rmid_idx();
 	size_t tsize;
 
-	if (is_llc_occupancy_enabled()) {
+	if (resctrl_arch_is_llc_occupancy_enabled()) {
 		d->rmid_busy_llc = bitmap_zalloc(idx_limit, GFP_KERNEL);
 		if (!d->rmid_busy_llc)
 			return -ENOMEM;
 	}
-	if (is_mbm_total_enabled()) {
+	if (resctrl_arch_is_mbm_total_enabled()) {
 		tsize = sizeof(*d->mbm_total);
 		d->mbm_total = kcalloc(idx_limit, tsize, GFP_KERNEL);
 		if (!d->mbm_total) {
@@ -4133,7 +4145,7 @@ static int domain_setup_mon_state(struct rdt_resource *r, struct rdt_mon_domain
 			return -ENOMEM;
 		}
 	}
-	if (is_mbm_local_enabled()) {
+	if (resctrl_arch_is_mbm_local_enabled()) {
 		tsize = sizeof(*d->mbm_local);
 		d->mbm_local = kcalloc(idx_limit, tsize, GFP_KERNEL);
 		if (!d->mbm_local) {
@@ -4172,13 +4184,13 @@ int resctrl_online_mon_domain(struct rdt_resource *r, struct rdt_mon_domain *d)
 	if (err)
 		goto out_unlock;
 
-	if (is_mbm_enabled()) {
+	if (resctrl_is_mbm_enabled()) {
 		INIT_DELAYED_WORK(&d->mbm_over, mbm_handle_overflow);
 		mbm_setup_overflow_handler(d, MBM_OVERFLOW_INTERVAL,
 					   RESCTRL_PICK_ANY_CPU);
 	}
 
-	if (is_llc_occupancy_enabled())
+	if (resctrl_arch_is_llc_occupancy_enabled())
 		INIT_DELAYED_WORK(&d->cqm_limbo, cqm_handle_limbo);
 
 	/*
@@ -4233,12 +4245,12 @@ void resctrl_offline_cpu(unsigned int cpu)
 
 	d = get_mon_domain_from_cpu(cpu, l3);
 	if (d) {
-		if (is_mbm_enabled() && cpu == d->mbm_work_cpu) {
+		if (resctrl_is_mbm_enabled() && cpu == d->mbm_work_cpu) {
 			cancel_delayed_work(&d->mbm_over);
 			mbm_setup_overflow_handler(d, 0, cpu);
 		}
-		if (is_llc_occupancy_enabled() && cpu == d->cqm_work_cpu &&
-		    has_busy_rmid(d)) {
+		if (resctrl_arch_is_llc_occupancy_enabled() &&
+		    cpu == d->cqm_work_cpu && has_busy_rmid(d)) {
 			cancel_delayed_work(&d->cqm_limbo);
 			cqm_setup_limbo_handler(d, 0, cpu);
 		}
Signature · 1 line
2.39.5
Reviewed-by on v7 18/49
#40 lore raw

Re: [PATCH v7 18/49] x86/resctrl: Move the is_mbm_*_enabled() helpers to asm/resctrl.h

Quoted 14 lines · James Morse

The architecture specific parts of resctrl provide helpers like is_mbm_total_enabled() and is_mbm_local_enabled() to hide accesses to the rdt_mon_features bitmap.

Exposing a group of helpers between the architecture and filesystem code is preferable to a single unsigned-long like rdt_mon_features. Helpers can be more readable and have a well defined behaviour, while allowing architectures to hide more complex behaviour.

Once the filesystem parts of resctrl are moved, these existing helpers can no longer live in internal.h. Move them to include/linux/resctrl.h Once these are exposed to the wider kernel, they should have a 'resctrl_arch_' prefix, to fit the rest of the arch<->fs interface.

Quoted 8 lines

Move and rename the helpers that touch rdt_mon_features directly. is_mbm_event() and is_mbm_enabled() are only called from rdtgroup.c, so can be moved into that file.

Thanks.

-Fenghua

maintainer Reviewed-by on v7 18/49
#41 lore raw

Re: [PATCH v7 18/49] x86/resctrl: Move the is_mbm_*_enabled() helpers to asm/resctrl.h

In reply to James Morse

Hi James,

Quoted 18 lines · James Morse

The architecture specific parts of resctrl provide helpers like is_mbm_total_enabled() and is_mbm_local_enabled() to hide accesses to the rdt_mon_features bitmap.

Exposing a group of helpers between the architecture and filesystem code is preferable to a single unsigned-long like rdt_mon_features. Helpers can be more readable and have a well defined behaviour, while allowing architectures to hide more complex behaviour.

Once the filesystem parts of resctrl are moved, these existing helpers can no longer live in internal.h. Move them to include/linux/resctrl.h Once these are exposed to the wider kernel, they should have a 'resctrl_arch_' prefix, to fit the rest of the arch<->fs interface.

Move and rename the helpers that touch rdt_mon_features directly. is_mbm_event() and is_mbm_enabled() are only called from rdtgroup.c, so can be moved into that file.

Quoted 5 lines

---

Reinette

author reviewer
#42 lore raw

PATCH v7 19/49 x86/resctrl: Add resctrl_arch_is_evt_configurable() to abstract BMEC

In reply to James Morse

When BMEC is supported the resctrl event can be configured in a number of ways. This depends on architecture support. rdt_get_mon_l3_config() modifies the struct mon_evt and calls mbm_config_rftype_init() to create the files that allow the configuration.

Splitting this into separate architecture and filesystem parts would require the struct mon_evt and mbm_config_rftype_init() to be exposed.

Instead, add resctrl_arch_is_evt_configurable(), and use this from resctrl_mon_resource_init() to initialise struct mon_evt and call resctrl_file_fflags_init() resctrl_arch_is_evt_configurable() calls rdt_cpu_has() so it doesn't obviously benefit from being inlined. Putting it in core.c will allow rdt_cpu_has() to eventually become static.

---
Changes since v6:
* Fixed stale function name in the commit message.
* Moved __init around in the function prototypes.

Changes since v4:
* Moved all the __init changes to a later patch now that the exit gubbins comes first.

---
 arch/x86/kernel/cpu/resctrl/core.c    | 15 +++++++++++++++
 arch/x86/kernel/cpu/resctrl/monitor.c | 22 +++++++++++-----------
 include/linux/resctrl.h               |  2 ++
 3 files changed, 28 insertions(+), 11 deletions(-)
arch/x86/kernel/cpu/resctrl/core.c +15 -0
index 5464dc1611e9..943e4c56077b 100644
@@ -832,6 +832,21 @@ bool __init rdt_cpu_has(int flag)
 	return ret;
 }
 
+__init bool resctrl_arch_is_evt_configurable(enum resctrl_event_id evt)
+{
+	if (!rdt_cpu_has(X86_FEATURE_BMEC))
+		return false;
+
+	switch (evt) {
+	case QOS_L3_MBM_TOTAL_EVENT_ID:
+		return rdt_cpu_has(X86_FEATURE_CQM_MBM_TOTAL);
+	case QOS_L3_MBM_LOCAL_EVENT_ID:
+		return rdt_cpu_has(X86_FEATURE_CQM_MBM_LOCAL);
+	default:
+		return false;
+	}
+}
+
 static __init bool get_mem_config(void)
 {
 	struct rdt_hw_resource *hw_res = &rdt_resources_all[RDT_RESOURCE_MBA];
arch/x86/kernel/cpu/resctrl/monitor.c +11 -11
index d883ed56ec90..27d983121b0a 100644
@@ -1202,6 +1202,17 @@ int __init resctrl_mon_resource_init(void)
 
 	l3_mon_evt_init(r);
 
+	if (resctrl_arch_is_evt_configurable(QOS_L3_MBM_TOTAL_EVENT_ID)) {
+		mbm_total_event.configurable = true;
+		resctrl_file_fflags_init("mbm_total_bytes_config",
+					 RFTYPE_MON_INFO | RFTYPE_RES_CACHE);
+	}
+	if (resctrl_arch_is_evt_configurable(QOS_L3_MBM_LOCAL_EVENT_ID)) {
+		mbm_local_event.configurable = true;
+		resctrl_file_fflags_init("mbm_local_bytes_config",
+					 RFTYPE_MON_INFO | RFTYPE_RES_CACHE);
+	}
+
 	return 0;
 }
 
@@ -1245,17 +1256,6 @@ int __init rdt_get_mon_l3_config(struct rdt_resource *r)
 		/* Detect list of bandwidth sources that can be tracked */
 		cpuid_count(0x80000020, 3, &eax, &ebx, &ecx, &edx);
 		hw_res->mbm_cfg_mask = ecx & MAX_EVT_CONFIG_BITS;
-
-		if (rdt_cpu_has(X86_FEATURE_CQM_MBM_TOTAL)) {
-			mbm_total_event.configurable = true;
-			resctrl_file_fflags_init("mbm_total_bytes_config",
-						 RFTYPE_MON_INFO | RFTYPE_RES_CACHE);
-		}
-		if (rdt_cpu_has(X86_FEATURE_CQM_MBM_LOCAL)) {
-			mbm_local_event.configurable = true;
-			resctrl_file_fflags_init("mbm_local_bytes_config",
-						 RFTYPE_MON_INFO | RFTYPE_RES_CACHE);
-		}
 	}
 
 	r->mon_capable = true;
include/linux/resctrl.h +2 -0
index a392480dc4b6..cc76f308e6f3 100644
@@ -309,6 +309,8 @@ u32 resctrl_arch_get_num_closid(struct rdt_resource *r);
 u32 resctrl_arch_system_num_rmid_idx(void);
 int resctrl_arch_update_domains(struct rdt_resource *r, u32 closid);
 
+__init bool resctrl_arch_is_evt_configurable(enum resctrl_event_id evt);
+
 /*
  * Update the ctrl_val and apply this config right now.
  * Must be called on one of the domain's CPUs.
Signature · 1 line
2.39.5
Reviewed-by on v7 19/49
#43 lore raw

Re: [PATCH v7 19/49] x86/resctrl: Add resctrl_arch_is_evt_configurable() to abstract BMEC

Quoted 15 lines · James Morse

When BMEC is supported the resctrl event can be configured in a number of ways. This depends on architecture support. rdt_get_mon_l3_config() modifies the struct mon_evt and calls mbm_config_rftype_init() to create the files that allow the configuration.

Splitting this into separate architecture and filesystem parts would require the struct mon_evt and mbm_config_rftype_init() to be exposed.

Instead, add resctrl_arch_is_evt_configurable(), and use this from resctrl_mon_resource_init() to initialise struct mon_evt and call resctrl_file_fflags_init() resctrl_arch_is_evt_configurable() calls rdt_cpu_has() so it doesn't obviously benefit from being inlined. Putting it in core.c will allow rdt_cpu_has() to eventually become static.

Quoted 5 lines

Thanks.

-Fenghua

maintainer
#44 lore raw

Re: [PATCH v7 19/49] x86/resctrl: Add resctrl_arch_is_evt_configurable() to abstract BMEC

In reply to James Morse

Hi James,

Quoted 7 lines · James Morse

When BMEC is supported the resctrl event can be configured in a number of ways. This depends on architecture support. rdt_get_mon_l3_config() modifies the struct mon_evt and calls mbm_config_rftype_init() to create the files that allow the configuration.

Splitting this into separate architecture and filesystem parts would require the struct mon_evt and mbm_config_rftype_init() to be exposed.

Same comment as for v6 [1]:
"Please replace all instances of mbm_config_rftype_init() with resctrl_file_fflags_init()."

Quoted 8 lines

Instead, add resctrl_arch_is_evt_configurable(), and use this from resctrl_mon_resource_init() to initialise struct mon_evt and call resctrl_file_fflags_init() resctrl_arch_is_evt_configurable() calls rdt_cpu_has() so it doesn't obviously benefit from being inlined. Putting it in core.c will allow rdt_cpu_has() to eventually become static.

Quoted 8 lines

---
Changes since v6:
* Fixed stale function name in the commit message.

Fixes seem to have been lost.

With changelog fixed:
| Reviewed-by: Reinette Chatre <[email protected]>

Reinette

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

author reviewer
#45 lore raw

Re: [PATCH v7 19/49] x86/resctrl: Add resctrl_arch_is_evt_configurable() to abstract BMEC

Hi Reinette,

Quoted 6 lines · Reinette Chatre
Quoted 5 lines · James Morse

When BMEC is supported the resctrl event can be configured in a number of ways. This depends on architecture support. rdt_get_mon_l3_config() modifies the struct mon_evt and calls mbm_config_rftype_init() to create the files that allow the configuration.

Quoted 6 lines
Quoted 2 lines

Splitting this into separate architecture and filesystem parts would require the struct mon_evt and mbm_config_rftype_init() to be exposed.

Same comment as for v6 [1]:
"Please replace all instances of mbm_config_rftype_init() with resctrl_file_fflags_init()."

Ah - I didn't spot there was more than on in the commit message. (and git-grep didn't bring it back either!)

Fixed.

Quoted 15 lines
Quoted 7 lines

Instead, add resctrl_arch_is_evt_configurable(), and use this from resctrl_mon_resource_init() to initialise struct mon_evt and call resctrl_file_fflags_init() resctrl_arch_is_evt_configurable() calls rdt_cpu_has() so it doesn't obviously benefit from being inlined. Putting it in core.c will allow rdt_cpu_has() to eventually become static.

Quoted 8 lines

---
Changes since v6:
* Fixed stale function name in the commit message.

Quoted 4 lines

Fixes seem to have been lost.

With changelog fixed:
| Reviewed-by: Reinette Chatre <[email protected]>

Thanks!

James

author reviewer
#46 lore raw

PATCH v7 20/49 x86/resctrl: Change mon_event_config_{read,write}() to be arch helpers

In reply to James Morse

mon_event_config_{read,write}() are called via IPI and access model specific registers to do their work.

To support another architecture, this needs abstracting.

Rename mon_event_config_{read,write}() to have a "resctrl_arch_" prefix, and move their struct mon_config_info parameter into <linux/resctrl.h>. This allows another architecture to supply an implementation of these.

As struct mon_config_info is now exposed globally, give it a 'resctrl_' prefix. MPAM systems need access to the domain to do this work, add the resource and domain to struct resctrl_mon_config_info.

---
Changes since v5:
* Renamed info to config_info.
* Added description of which fields are read and written in the structure.
* Clarified comment about which CPU this is called on for both kinds of reader.

Changes since v3:
* Added comments over the read/write helper to explain the type of the void pointer.

Changes since v1:
* [Whitespace only] Re-tabbed struct resctrl_mon_config_info in <linux/resctrl.h> to fit the prevailing style.

Non-functional change.

* [Commit message only] Reword to align with the actual naming of the definitions and destination header file.

---
 arch/x86/kernel/cpu/resctrl/rdtgroup.c | 46 +++++++++++++-------------
 include/linux/resctrl.h                | 31 +++++++++++++++++
 2 files changed, 54 insertions(+), 23 deletions(-)
arch/x86/kernel/cpu/resctrl/rdtgroup.c +23 -23
index eb32fbc3abea..e7d1d8b6983d 100644
@@ -1580,11 +1580,6 @@ static int rdtgroup_size_show(struct kernfs_open_file *of,
 	return ret;
 }
 
-struct mon_config_info {
-	u32 evtid;
-	u32 mon_config;
-};
-
 #define INVALID_CONFIG_INDEX   UINT_MAX
 
 /**
@@ -1609,31 +1604,32 @@ static inline unsigned int mon_event_config_index_get(u32 evtid)
 	}
 }
 
-static void mon_event_config_read(void *info)
+void resctrl_arch_mon_event_config_read(void *_config_info)
 {
-	struct mon_config_info *mon_info = info;
+	struct resctrl_mon_config_info *config_info = _config_info;
 	unsigned int index;
 	u64 msrval;
 
-	index = mon_event_config_index_get(mon_info->evtid);
+	index = mon_event_config_index_get(config_info->evtid);
 	if (index == INVALID_CONFIG_INDEX) {
-		pr_warn_once("Invalid event id %d\n", mon_info->evtid);
+		pr_warn_once("Invalid event id %d\n", config_info->evtid);
 		return;
 	}
 	rdmsrl(MSR_IA32_EVT_CFG_BASE + index, msrval);
 
 	/* Report only the valid event configuration bits */
-	mon_info->mon_config = msrval & MAX_EVT_CONFIG_BITS;
+	config_info->mon_config = msrval & MAX_EVT_CONFIG_BITS;
 }
 
-static void mondata_config_read(struct rdt_mon_domain *d, struct mon_config_info *mon_info)
+static void mondata_config_read(struct resctrl_mon_config_info *mon_info)
 {
-	smp_call_function_any(&d->hdr.cpu_mask, mon_event_config_read, mon_info, 1);
+	smp_call_function_any(&mon_info->d->hdr.cpu_mask,
+			      resctrl_arch_mon_event_config_read, mon_info, 1);
 }
 
 static int mbm_config_show(struct seq_file *s, struct rdt_resource *r, u32 evtid)
 {
-	struct mon_config_info mon_info;
+	struct resctrl_mon_config_info mon_info;
 	struct rdt_mon_domain *dom;
 	bool sep = false;
 
@@ -1644,9 +1640,11 @@ static int mbm_config_show(struct seq_file *s, struct rdt_resource *r, u32 evtid
 		if (sep)
 			seq_puts(s, ";");
 
-		memset(&mon_info, 0, sizeof(struct mon_config_info));
+		memset(&mon_info, 0, sizeof(struct resctrl_mon_config_info));
+		mon_info.r = r;
+		mon_info.d = dom;
 		mon_info.evtid = evtid;
-		mondata_config_read(dom, &mon_info);
+		mondata_config_read(&mon_info);
 
 		seq_printf(s, "%d=0x%02x", dom->hdr.id, mon_info.mon_config);
 		sep = true;
@@ -1679,30 +1677,32 @@ static int mbm_local_bytes_config_show(struct kernfs_open_file *of,
 	return 0;
 }
 
-static void mon_event_config_write(void *info)
+void resctrl_arch_mon_event_config_write(void *_config_info)
 {
-	struct mon_config_info *mon_info = info;
+	struct resctrl_mon_config_info *config_info = _config_info;
 	unsigned int index;
 
-	index = mon_event_config_index_get(mon_info->evtid);
+	index = mon_event_config_index_get(config_info->evtid);
 	if (index == INVALID_CONFIG_INDEX) {
-		pr_warn_once("Invalid event id %d\n", mon_info->evtid);
+		pr_warn_once("Invalid event id %d\n", config_info->evtid);
 		return;
 	}
-	wrmsr(MSR_IA32_EVT_CFG_BASE + index, mon_info->mon_config, 0);
+	wrmsr(MSR_IA32_EVT_CFG_BASE + index, config_info->mon_config, 0);
 }
 
 static void mbm_config_write_domain(struct rdt_resource *r,
 				    struct rdt_mon_domain *d, u32 evtid, u32 val)
 {
-	struct mon_config_info mon_info = {0};
+	struct resctrl_mon_config_info mon_info = {0};
 
 	/*
 	 * Read the current config value first. If both are the same then
 	 * no need to write it again.
 	 */
+	mon_info.r = r;
+	mon_info.d = d;
 	mon_info.evtid = evtid;
-	mondata_config_read(d, &mon_info);
+	mondata_config_read(&mon_info);
 	if (mon_info.mon_config == val)
 		return;
 
@@ -1714,7 +1714,7 @@ static void mbm_config_write_domain(struct rdt_resource *r,
 	 * are scoped at the domain level. Writing any of these MSRs
 	 * on one CPU is observed by all the CPUs in the domain.
 	 */
-	smp_call_function_any(&d->hdr.cpu_mask, mon_event_config_write,
+	smp_call_function_any(&d->hdr.cpu_mask, resctrl_arch_mon_event_config_write,
 			      &mon_info, 1);
 
 	/*
include/linux/resctrl.h +31 -0
index cc76f308e6f3..90b6563cf532 100644
@@ -270,6 +270,13 @@ struct resctrl_cpu_defaults {
 	u32 rmid;
 };
 
+struct resctrl_mon_config_info {
+	struct rdt_resource	*r;
+	struct rdt_mon_domain	*d;
+	u32			evtid;
+	u32			mon_config;
+};
+
 /**
  * resctrl_arch_sync_cpu_closid_rmid() - Refresh this CPU's CLOSID and RMID.
  *					 Call via IPI.
@@ -311,6 +318,30 @@ int resctrl_arch_update_domains(struct rdt_resource *r, u32 closid);
 
 __init bool resctrl_arch_is_evt_configurable(enum resctrl_event_id evt);
 
+/**
+ * resctrl_arch_mon_event_config_write() - Write the config for an event.
+ * @config_info: struct resctrl_mon_config_info describing the resource, domain
+ *		 and event.
+ *
+ * Reads resource, domain and eventid from @config_info and writes the
+ * event config_info->mon_config into hardware.
+ *
+ * Called via IPI to reach a CPU that is a member of the specified domain.
+ */
+void resctrl_arch_mon_event_config_write(void *config_info);
+
+/**
+ * resctrl_arch_mon_event_config_read() - Read the config for an event.
+ * @config_info: struct resctrl_mon_config_info describing the resource, domain
+ *		 and event.
+ *
+ * Reads resource, domain and eventid from @config_info and reads the
+ * hardware config value into config_info->mon_config.
+ *
+ * Called via IPI to reach a CPU that is a member of the specified domain.
+ */
+void resctrl_arch_mon_event_config_read(void *config_info);
+
 /*
  * Update the ctrl_val and apply this config right now.
  * Must be called on one of the domain's CPUs.
Signature · 1 line
2.39.5
Reviewed-by on v7 20/49
#47 lore raw

Re: [PATCH v7 20/49] x86/resctrl: Change mon_event_config_{read,write}() to be arch helpers

Quoted 14 lines · James Morse

mon_event_config_{read,write}() are called via IPI and access model specific registers to do their work.

To support another architecture, this needs abstracting.

Rename mon_event_config_{read,write}() to have a "resctrl_arch_" prefix, and move their struct mon_config_info parameter into <linux/resctrl.h>. This allows another architecture to supply an implementation of these.

As struct mon_config_info is now exposed globally, give it a 'resctrl_' prefix. MPAM systems need access to the domain to do this work, add the resource and domain to struct resctrl_mon_config_info.

Quoted 8 lines

Thanks.

-Fenghua

author reviewer
#48 lore raw

PATCH v7 21/49 x86/resctrl: Move mba_mbps_default_event init to filesystem code

In reply to James Morse

mba_mbps_default_event is initialised base on whether mbm_local or mbm_total is supported. In the case of both, it is initialised to mbm_local. mba_mbps_default_event is initialised in core.c's get_rdt_mon_resources(), while all the readers are in rdtgroup.c.

After this code is split into architecture specific and filesystem code, get_rdt_mon_resources() remains part of the architecture code, which would mean mba_mbps_default_event has to be exposed by the filesystem code.

Move the initialisation to the filesystem's resctrl_mon_resource_init()

---
 arch/x86/kernel/cpu/resctrl/core.c    | 5 -----
 arch/x86/kernel/cpu/resctrl/monitor.c | 5 +++++
 2 files changed, 5 insertions(+), 5 deletions(-)
arch/x86/kernel/cpu/resctrl/core.c +0 -5
index 943e4c56077b..2c352938d18d 100644
@@ -925,11 +925,6 @@ static __init bool get_rdt_mon_resources(void)
 	if (!rdt_mon_features)
 		return false;
 
-	if (resctrl_arch_is_mbm_local_enabled())
-		mba_mbps_default_event = QOS_L3_MBM_LOCAL_EVENT_ID;
-	else if (resctrl_arch_is_mbm_total_enabled())
-		mba_mbps_default_event = QOS_L3_MBM_TOTAL_EVENT_ID;
-
 	return !rdt_get_mon_l3_config(r);
 }
 
arch/x86/kernel/cpu/resctrl/monitor.c +5 -0
index 27d983121b0a..306b06ba32db 100644
@@ -1213,6 +1213,11 @@ int __init resctrl_mon_resource_init(void)
 					 RFTYPE_MON_INFO | RFTYPE_RES_CACHE);
 	}
 
+	if (resctrl_arch_is_mbm_local_enabled())
+		mba_mbps_default_event = QOS_L3_MBM_LOCAL_EVENT_ID;
+	else if (resctrl_arch_is_mbm_total_enabled())
+		mba_mbps_default_event = QOS_L3_MBM_TOTAL_EVENT_ID;
+
 	return 0;
 }
 
Signature · 1 line
2.39.5
Reviewed-by on v7 21/49
#49 lore raw

Re: [PATCH v7 21/49] x86/resctrl: Move mba_mbps_default_event init to filesystem code

Quoted 10 lines · James Morse

mba_mbps_default_event is initialised base on whether mbm_local or mbm_total is supported. In the case of both, it is initialised to mbm_local. mba_mbps_default_event is initialised in core.c's get_rdt_mon_resources(), while all the readers are in rdtgroup.c.

After this code is split into architecture specific and filesystem code, get_rdt_mon_resources() remains part of the architecture code, which would mean mba_mbps_default_event has to be exposed by the filesystem code.

Quoted 4 lines

Move the initialisation to the filesystem's resctrl_mon_resource_init()

Thanks.

-Fenghua

author reviewer
#50 lore raw

PATCH v7 22/49 x86/resctrl: Move mbm_cfg_mask to struct rdt_resource

In reply to James Morse

The mbm_cfg_mask field lists the bits that user-space can set when configuring an event. This value is output via the last_cmd_status file.

Once the filesystem parts of resctrl are moved to live in /fs/, the struct rdt_hw_resource is inaccessible to the filesystem code. Because this value is output to user-space, it has to be accessible to the filesystem code.

Move it to struct rdt_resource.

---
Changes since v5:
* Rephrased a comment to remove one vendors marketing name for the feature.

Change since v1:
* Reword comments to avoid being overly arch-specific.

---
 arch/x86/kernel/cpu/resctrl/internal.h | 3 ---
 arch/x86/kernel/cpu/resctrl/monitor.c  | 2 +-
 arch/x86/kernel/cpu/resctrl/rdtgroup.c | 5 ++---
 include/linux/resctrl.h                | 3 +++
 4 files changed, 6 insertions(+), 7 deletions(-)
arch/x86/kernel/cpu/resctrl/internal.h +0 -3
index 4a5996d1e060..725f223ea07b 100644
@@ -417,8 +417,6 @@ struct msr_param {
  * @msr_update:		Function pointer to update QOS MSRs
  * @mon_scale:		cqm counter * mon_scale = occupancy in bytes
  * @mbm_width:		Monitor width, to detect and correct for overflow.
- * @mbm_cfg_mask:	Bandwidth sources that can be tracked when Bandwidth
- *			Monitoring Event Configuration (BMEC) is supported.
  * @cdp_enabled:	CDP state of this resource
  *
  * Members of this structure are either private to the architecture
@@ -432,7 +430,6 @@ struct rdt_hw_resource {
 	void			(*msr_update)(struct msr_param *m);
 	unsigned int		mon_scale;
 	unsigned int		mbm_width;
-	unsigned int		mbm_cfg_mask;
 	bool			cdp_enabled;
 };
 
arch/x86/kernel/cpu/resctrl/monitor.c +1 -1
index 306b06ba32db..83f90128d768 100644
@@ -1260,7 +1260,7 @@ int __init rdt_get_mon_l3_config(struct rdt_resource *r)
 
 		/* Detect list of bandwidth sources that can be tracked */
 		cpuid_count(0x80000020, 3, &eax, &ebx, &ecx, &edx);
-		hw_res->mbm_cfg_mask = ecx & MAX_EVT_CONFIG_BITS;
+		r->mbm_cfg_mask = ecx & MAX_EVT_CONFIG_BITS;
 	}
 
 	r->mon_capable = true;
arch/x86/kernel/cpu/resctrl/rdtgroup.c +2 -3
index e7d1d8b6983d..a388ef66ef4c 100644
@@ -1731,7 +1731,6 @@ static void mbm_config_write_domain(struct rdt_resource *r,
 
 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_mon_domain *d;
@@ -1758,9 +1757,9 @@ static int mon_config_write(struct rdt_resource *r, char *tok, u32 evtid)
 	}
 
 	/* Value from user cannot be more than the supported set of events */
-	if ((val & hw_res->mbm_cfg_mask) != val) {
+	if ((val & r->mbm_cfg_mask) != val) {
 		rdt_last_cmd_printf("Invalid event configuration: max valid mask is 0x%02x\n",
-				    hw_res->mbm_cfg_mask);
+				    r->mbm_cfg_mask);
 		return -EINVAL;
 	}
 
include/linux/resctrl.h +3 -0
index 90b6563cf532..914df24ffe48 100644
@@ -217,6 +217,8 @@ enum resctrl_schema_fmt {
  * @name:		Name to use in "schemata" file.
  * @schema_fmt:		Which format string and parser is used for this schema.
  * @evt_list:		List of monitoring events
+ * @mbm_cfg_mask:	Bandwidth sources that can be tracked when bandwidth
+ *			monitoring events can be configured.
  * @cdp_capable:	Is the CDP feature available on this resource
  */
 struct rdt_resource {
@@ -233,6 +235,7 @@ struct rdt_resource {
 	char			*name;
 	enum resctrl_schema_fmt	schema_fmt;
 	struct list_head	evt_list;
+	unsigned int		mbm_cfg_mask;
 	bool			cdp_capable;
 };
 
Signature · 1 line
2.39.5
Reviewed-by on v7 22/49
#51 lore raw

Re: [PATCH v7 22/49] x86/resctrl: Move mbm_cfg_mask to struct rdt_resource

Quoted 9 lines · James Morse

The mbm_cfg_mask field lists the bits that user-space can set when configuring an event. This value is output via the last_cmd_status file.

Once the filesystem parts of resctrl are moved to live in /fs/, the struct rdt_hw_resource is inaccessible to the filesystem code. Because this value is output to user-space, it has to be accessible to the filesystem code.

Quoted 8 lines

Move it to struct rdt_resource.

Thanks.

-Fenghua

author reviewer
#52 lore raw

PATCH v7 23/49 x86/resctrl: Add resctrl_arch_ prefix to pseudo lock functions

In reply to James Morse

resctrl's pseudo lock has some copy-to-cache and measurement functions that are micro-architecture specific.

For example, pseudo_lock_fn() is not at all portable.

Label these 'resctrl_arch_' so they stay under /arch/x86. To expose these functions to the filesystem code they need an entry in a header file, and can't be marked static.

---
Changes since v6:
* Removed a space.

Changes since v5:
* Expanded commit message.

---
 arch/x86/include/asm/resctrl.h            |  5 ++++
 arch/x86/kernel/cpu/resctrl/pseudo_lock.c | 36 ++++++++++++-----------
 2 files changed, 24 insertions(+), 17 deletions(-)
arch/x86/include/asm/resctrl.h +5 -0
index 6d4c7ea2c9e3..86407dbde583 100644
@@ -203,6 +203,11 @@ static inline void *resctrl_arch_mon_ctx_alloc(struct rdt_resource *r, int evtid
 static inline void resctrl_arch_mon_ctx_free(struct rdt_resource *r, int evtid,
 					     void *ctx) { };
 
+u64 resctrl_arch_get_prefetch_disable_bits(void);
+int resctrl_arch_pseudo_lock_fn(void *_rdtgrp);
+int resctrl_arch_measure_cycles_lat_fn(void *_plr);
+int resctrl_arch_measure_l2_residency(void *_plr);
+int resctrl_arch_measure_l3_residency(void *_plr);
 void resctrl_cpu_detect(struct cpuinfo_x86 *c);
 
 #else
arch/x86/kernel/cpu/resctrl/pseudo_lock.c +19 -17
index 42cc162f7fc9..1f42c1190d26 100644
@@ -61,7 +61,8 @@ static const struct class pseudo_lock_class = {
 };
 
 /**
- * get_prefetch_disable_bits - prefetch disable bits of supported platforms
+ * resctrl_arch_get_prefetch_disable_bits - prefetch disable bits of supported
+ *                                          platforms
  * @void: It takes no parameters.
  *
  * Capture the list of platforms that have been validated to support
@@ -75,13 +76,13 @@ static const struct class pseudo_lock_class = {
  * in the SDM.
  *
  * When adding a platform here also add support for its cache events to
- * measure_cycles_perf_fn()
+ * resctrl_arch_measure_l*_residency()
  *
  * Return:
  * If platform is supported, the bits to disable hardware prefetchers, 0
  * if platform is not supported.
  */
-static u64 get_prefetch_disable_bits(void)
+u64 resctrl_arch_get_prefetch_disable_bits(void)
 {
 	if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL ||
 	    boot_cpu_data.x86 != 6)
@@ -408,7 +409,7 @@ static void pseudo_lock_free(struct rdtgroup *rdtgrp)
 }
 
 /**
- * pseudo_lock_fn - Load kernel memory into cache
+ * resctrl_arch_pseudo_lock_fn - Load kernel memory into cache
  * @_rdtgrp: resource group to which pseudo-lock region belongs
  *
  * This is the core pseudo-locking flow.
@@ -426,7 +427,7 @@ static void pseudo_lock_free(struct rdtgroup *rdtgrp)
  *
  * Return: 0. Waiter on waitqueue will be woken on completion.
  */
-static int pseudo_lock_fn(void *_rdtgrp)
+int resctrl_arch_pseudo_lock_fn(void *_rdtgrp)
 {
 	struct rdtgroup *rdtgrp = _rdtgrp;
 	struct pseudo_lock_region *plr = rdtgrp->plr;
@@ -712,7 +713,7 @@ int rdtgroup_locksetup_enter(struct rdtgroup *rdtgrp)
 	 * Not knowing the bits to disable prefetching implies that this
 	 * platform does not support Cache Pseudo-Locking.
 	 */
-	prefetch_disable_bits = get_prefetch_disable_bits();
+	prefetch_disable_bits = resctrl_arch_get_prefetch_disable_bits();
 	if (prefetch_disable_bits == 0) {
 		rdt_last_cmd_puts("Pseudo-locking not supported\n");
 		return -EINVAL;
@@ -872,7 +873,8 @@ bool rdtgroup_pseudo_locked_in_hierarchy(struct rdt_ctrl_domain *d)
 }
 
 /**
- * measure_cycles_lat_fn - Measure cycle latency to read pseudo-locked memory
+ * resctrl_arch_measure_cycles_lat_fn - Measure cycle latency to read
+ *                                      pseudo-locked memory
  * @_plr: pseudo-lock region to measure
  *
  * There is no deterministic way to test if a memory region is cached. One
@@ -885,7 +887,7 @@ bool rdtgroup_pseudo_locked_in_hierarchy(struct rdt_ctrl_domain *d)
  *
  * Return: 0. Waiter on waitqueue will be woken on completion.
  */
-static int measure_cycles_lat_fn(void *_plr)
+int resctrl_arch_measure_cycles_lat_fn(void *_plr)
 {
 	struct pseudo_lock_region *plr = _plr;
 	u32 saved_low, saved_high;
@@ -1069,7 +1071,7 @@ static int measure_residency_fn(struct perf_event_attr *miss_attr,
 	return 0;
 }
 
-static int measure_l2_residency(void *_plr)
+int resctrl_arch_measure_l2_residency(void *_plr)
 {
 	struct pseudo_lock_region *plr = _plr;
 	struct residency_counts counts = {0};
@@ -1107,7 +1109,7 @@ static int measure_l2_residency(void *_plr)
 	return 0;
 }
 
-static int measure_l3_residency(void *_plr)
+int resctrl_arch_measure_l3_residency(void *_plr)
 {
 	struct pseudo_lock_region *plr = _plr;
 	struct residency_counts counts = {0};
@@ -1205,14 +1207,14 @@ static int pseudo_lock_measure_cycles(struct rdtgroup *rdtgrp, int sel)
 	plr->cpu = cpu;
 
 	if (sel == 1)
-		thread = kthread_run_on_cpu(measure_cycles_lat_fn, plr,
-					    cpu, "pseudo_lock_measure/%u");
+		thread = kthread_run_on_cpu(resctrl_arch_measure_cycles_lat_fn,
+					    plr, cpu, "pseudo_lock_measure/%u");
 	else if (sel == 2)
-		thread = kthread_run_on_cpu(measure_l2_residency, plr,
-					    cpu, "pseudo_lock_measure/%u");
+		thread = kthread_run_on_cpu(resctrl_arch_measure_l2_residency,
+					    plr, cpu, "pseudo_lock_measure/%u");
 	else if (sel == 3)
-		thread = kthread_run_on_cpu(measure_l3_residency, plr,
-					    cpu, "pseudo_lock_measure/%u");
+		thread = kthread_run_on_cpu(resctrl_arch_measure_l3_residency,
+					    plr, cpu, "pseudo_lock_measure/%u");
 	else
 		goto out;
 
@@ -1307,7 +1309,7 @@ int rdtgroup_pseudo_lock_create(struct rdtgroup *rdtgrp)
 
 	plr->thread_done = 0;
 
-	thread = kthread_run_on_cpu(pseudo_lock_fn, rdtgrp,
+	thread = kthread_run_on_cpu(resctrl_arch_pseudo_lock_fn, rdtgrp,
 				    plr->cpu, "pseudo_lock/%u");
 	if (IS_ERR(thread)) {
 		ret = PTR_ERR(thread);
Signature · 1 line
2.39.5
Reviewed-by on v7 23/49
#53 lore raw

Re: [PATCH v7 23/49] x86/resctrl: Add resctrl_arch_ prefix to pseudo lock functions

Quoted 9 lines · James Morse

resctrl's pseudo lock has some copy-to-cache and measurement functions that are micro-architecture specific.

For example, pseudo_lock_fn() is not at all portable.

Label these 'resctrl_arch_' so they stay under /arch/x86. To expose these functions to the filesystem code they need an entry in a header file, and can't be marked static.

Quoted 6 lines

Thanks.

-Fenghua

author reviewer
#54 lore raw

PATCH v7 24/49 x86/resctrl: Allow an architecture to disable pseudo lock

In reply to James Morse

Pseudo-lock relies on knowledge of the micro-architecture to disable prefetchers etc.

On arm64 these controls are typically secure only, meaning linux can't access them. Arm's cache-lockdown feature works in a very different way. Resctrl's pseudo-lock isn't going to be used on arm64 platforms.

Add a Kconfig symbol that can be selected by the architecture. This enables or disables building of the pseudo_lock.c file, and replaces the functions with stubs. An additional IS_ENABLED() check is needed in rdtgroup_mode_write() so that attempting to enable pseudo-lock reports an "Unknown or unsupported mode" to user-space via the last_cmd_status file.

---
Changes since v4:
* "last_cmd file" -> "last_cmd_status file"

Changes since v2:
* Clarified the commit message as to where the error string is printed.

Changes since v1:
* [Commit message only] Typo fix:
s/psuedo/pseudo/g

---
 arch/x86/Kconfig                       |  7 ++++
 arch/x86/kernel/cpu/resctrl/Makefile   |  5 +--
 arch/x86/kernel/cpu/resctrl/internal.h | 49 +++++++++++++++++++++-----
 arch/x86/kernel/cpu/resctrl/rdtgroup.c |  3 +-
 4 files changed, 53 insertions(+), 11 deletions(-)
arch/x86/Kconfig +7 -0
index be2c311f5118..1a89574a58f7 100644
@@ -505,6 +505,7 @@ config X86_CPU_RESCTRL
 	depends on X86 && (CPU_SUP_INTEL || CPU_SUP_AMD)
 	select KERNFS
 	select PROC_CPU_RESCTRL		if PROC_FS
+	select RESCTRL_FS_PSEUDO_LOCK
 	help
 	  Enable x86 CPU resource control support.
 
@@ -521,6 +522,12 @@ config X86_CPU_RESCTRL
 
 	  Say N if unsure.
 
+config RESCTRL_FS_PSEUDO_LOCK
+	bool
+	help
+	  Software mechanism to pin data in a cache portion using
+	  micro-architecture specific knowledge.
+
 config X86_FRED
 	bool "Flexible Return and Event Delivery"
 	depends on X86_64
arch/x86/kernel/cpu/resctrl/Makefile +3 -2
index 4a06c37b9cf1..0c13b0befd8a 100644
@@ -1,4 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0
-obj-$(CONFIG_X86_CPU_RESCTRL)	+= core.o rdtgroup.o monitor.o
-obj-$(CONFIG_X86_CPU_RESCTRL)	+= ctrlmondata.o pseudo_lock.o
+obj-$(CONFIG_X86_CPU_RESCTRL)		+= core.o rdtgroup.o monitor.o
+obj-$(CONFIG_X86_CPU_RESCTRL)		+= ctrlmondata.o
+obj-$(CONFIG_RESCTRL_FS_PSEUDO_LOCK)	+= pseudo_lock.o
 CFLAGS_pseudo_lock.o = -I$(src)
arch/x86/kernel/cpu/resctrl/internal.h +41 -8

59 lines not on this page · raw message

arch/x86/kernel/cpu/resctrl/rdtgroup.c +2 -1
index a388ef66ef4c..e59271515a46 100644
@@ -1453,7 +1453,8 @@ static ssize_t rdtgroup_mode_write(struct kernfs_open_file *of,
 				goto out;
 		}
 		rdtgrp->mode = RDT_MODE_EXCLUSIVE;
-	} else if (!strcmp(buf, "pseudo-locksetup")) {
+	} else if (IS_ENABLED(CONFIG_RESCTRL_FS_PSEUDO_LOCK) &&
+		   !strcmp(buf, "pseudo-locksetup")) {
 		ret = rdtgroup_locksetup_enter(rdtgrp);
 		if (ret)
 			goto out;
Signature · 1 line
2.39.5
Reviewed-by on v7 24/49
#55 lore raw

Re: [PATCH v7 24/49] x86/resctrl: Allow an architecture to disable pseudo lock

Quoted 14 lines · James Morse

Pseudo-lock relies on knowledge of the micro-architecture to disable prefetchers etc.

On arm64 these controls are typically secure only, meaning linux can't access them. Arm's cache-lockdown feature works in a very different way. Resctrl's pseudo-lock isn't going to be used on arm64 platforms.

Add a Kconfig symbol that can be selected by the architecture. This enables or disables building of the pseudo_lock.c file, and replaces the functions with stubs. An additional IS_ENABLED() check is needed in rdtgroup_mode_write() so that attempting to enable pseudo-lock reports an "Unknown or unsupported mode" to user-space via the last_cmd_status file.

Quoted 6 lines

Thanks.

-Fenghua

author reviewer
#56 lore raw

PATCH v7 25/49 x86/resctrl: Make prefetch_disable_bits belong to the arch code

In reply to James Morse

prefetch_disable_bits is set by rdtgroup_locksetup_enter() from a value provided by the architecture, but is largely read by other architecture helpers.

Make resctrl_arch_get_prefetch_disable_bits() set prefetch_disable_bits so that it can be isolated to arch-code from where the other arch-code helpers can use its cached-value.

---
Changes since v5:
* Tweaked the word 'export'.
* Swapped the second paragraph for Reinette's version.

---
 arch/x86/kernel/cpu/resctrl/pseudo_lock.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
arch/x86/kernel/cpu/resctrl/pseudo_lock.c +8 -5
index 1f42c1190d26..90044a01d000 100644
@@ -84,6 +84,8 @@ static const struct class pseudo_lock_class = {
  */
 u64 resctrl_arch_get_prefetch_disable_bits(void)
 {
+	prefetch_disable_bits = 0;
+
 	if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL ||
 	    boot_cpu_data.x86 != 6)
 		return 0;
@@ -99,7 +101,8 @@ u64 resctrl_arch_get_prefetch_disable_bits(void)
 		 * 3    DCU IP Prefetcher Disable (R/W)
 		 * 63:4 Reserved
 		 */
-		return 0xF;
+		prefetch_disable_bits = 0xF;
+		break;
 	case INTEL_ATOM_GOLDMONT:
 	case INTEL_ATOM_GOLDMONT_PLUS:
 		/*
@@ -110,10 +113,11 @@ u64 resctrl_arch_get_prefetch_disable_bits(void)
 		 * 2     DCU Hardware Prefetcher Disable (R/W)
 		 * 63:3  Reserved
 		 */
-		return 0x5;
+		prefetch_disable_bits = 0x5;
+		break;
 	}
 
-	return 0;
+	return prefetch_disable_bits;
 }
 
 /**
@@ -713,8 +717,7 @@ int rdtgroup_locksetup_enter(struct rdtgroup *rdtgrp)
 	 * Not knowing the bits to disable prefetching implies that this
 	 * platform does not support Cache Pseudo-Locking.
 	 */
-	prefetch_disable_bits = resctrl_arch_get_prefetch_disable_bits();
-	if (prefetch_disable_bits == 0) {
+	if (resctrl_arch_get_prefetch_disable_bits() == 0) {
 		rdt_last_cmd_puts("Pseudo-locking not supported\n");
 		return -EINVAL;
 	}
Signature · 1 line
2.39.5
Reviewed-by on v7 25/49
#57 lore raw

Re: [PATCH v7 25/49] x86/resctrl: Make prefetch_disable_bits belong to the arch code

Quoted 8 lines · James Morse

prefetch_disable_bits is set by rdtgroup_locksetup_enter() from a value provided by the architecture, but is largely read by other architecture helpers.

Make resctrl_arch_get_prefetch_disable_bits() set prefetch_disable_bits so that it can be isolated to arch-code from where the other arch-code helpers can use its cached-value.

Quoted 6 lines

Thanks.

-Fenghua

author reviewer
#58 lore raw

PATCH v7 26/49 x86/resctrl: Make resctrl_arch_pseudo_lock_fn() take a plr

In reply to James Morse

resctrl_arch_pseudo_lock_fn() has architecture specific behaviour, and takes a struct rdtgroup as an argument.

After the filesystem code moves to /fs/, the definition of struct rdtgroup will not be available to the architecture code.

The only reason resctrl_arch_pseudo_lock_fn() wants the rdtgroup is for the CLOSID. Embed that in the pseudo_lock_region as a closid, and move the definition of struct pseudo_lock_region to resctrl.h.

---
Change since v1:
* [Commit message only] Typo fix:
s/hw_closid/closid/g

---
 arch/x86/include/asm/resctrl.h            |  2 +-
 arch/x86/kernel/cpu/resctrl/internal.h    | 37 ---------------------
 arch/x86/kernel/cpu/resctrl/pseudo_lock.c | 12 +++----
 include/linux/resctrl.h                   | 39 +++++++++++++++++++++++
 4 files changed, 46 insertions(+), 44 deletions(-)
arch/x86/include/asm/resctrl.h +1 -1
index 86407dbde583..011bf67a1866 100644
@@ -204,7 +204,7 @@ static inline void resctrl_arch_mon_ctx_free(struct rdt_resource *r, int evtid,
 					     void *ctx) { };
 
 u64 resctrl_arch_get_prefetch_disable_bits(void);
-int resctrl_arch_pseudo_lock_fn(void *_rdtgrp);
+int resctrl_arch_pseudo_lock_fn(void *_plr);
 int resctrl_arch_measure_cycles_lat_fn(void *_plr);
 int resctrl_arch_measure_l2_residency(void *_plr);
 int resctrl_arch_measure_l3_residency(void *_plr);
arch/x86/kernel/cpu/resctrl/internal.h +0 -37
index 8d35bb423aad..0d13006e920b 100644
@@ -208,43 +208,6 @@ struct mongroup {
 	u32			rmid;
 };
 
-/**
- * struct pseudo_lock_region - pseudo-lock region information
- * @s:			Resctrl schema for the resource to which this
- *			pseudo-locked region belongs
- * @d:			RDT domain to which this pseudo-locked region
- *			belongs
- * @cbm:		bitmask of the pseudo-locked region
- * @lock_thread_wq:	waitqueue used to wait on the pseudo-locking thread
- *			completion
- * @thread_done:	variable used by waitqueue to test if pseudo-locking
- *			thread completed
- * @cpu:		core associated with the cache on which the setup code
- *			will be run
- * @line_size:		size of the cache lines
- * @size:		size of pseudo-locked region in bytes
- * @kmem:		the kernel memory associated with pseudo-locked region
- * @minor:		minor number of character device associated with this
- *			region
- * @debugfs_dir:	pointer to this region's directory in the debugfs
- *			filesystem
- * @pm_reqs:		Power management QoS requests related to this region
- */
-struct pseudo_lock_region {
-	struct resctrl_schema	*s;
-	struct rdt_ctrl_domain	*d;
-	u32			cbm;
-	wait_queue_head_t	lock_thread_wq;
-	int			thread_done;
-	int			cpu;
-	unsigned int		line_size;
-	unsigned int		size;
-	void			*kmem;
-	unsigned int		minor;
-	struct dentry		*debugfs_dir;
-	struct list_head	pm_reqs;
-};
-
 /**
  * struct rdtgroup - store rdtgroup's data in resctrl file system.
  * @kn:				kernfs node
arch/x86/kernel/cpu/resctrl/pseudo_lock.c +6 -6
index 90044a01d000..01fa7890b43f 100644
@@ -414,7 +414,7 @@ static void pseudo_lock_free(struct rdtgroup *rdtgrp)
 
 /**
  * resctrl_arch_pseudo_lock_fn - Load kernel memory into cache
- * @_rdtgrp: resource group to which pseudo-lock region belongs
+ * @_plr: the pseudo-lock region descriptor
  *
  * This is the core pseudo-locking flow.
  *
@@ -431,10 +431,9 @@ static void pseudo_lock_free(struct rdtgroup *rdtgrp)
  *
  * Return: 0. Waiter on waitqueue will be woken on completion.
  */
-int resctrl_arch_pseudo_lock_fn(void *_rdtgrp)
+int resctrl_arch_pseudo_lock_fn(void *_plr)
 {
-	struct rdtgroup *rdtgrp = _rdtgrp;
-	struct pseudo_lock_region *plr = rdtgrp->plr;
+	struct pseudo_lock_region *plr = _plr;
 	u32 rmid_p, closid_p;
 	unsigned long i;
 	u64 saved_msr;
@@ -494,7 +493,8 @@ int resctrl_arch_pseudo_lock_fn(void *_rdtgrp)
 	 * pseudo-locked followed by reading of kernel memory to load it
 	 * into the cache.
 	 */
-	__wrmsr(MSR_IA32_PQR_ASSOC, rmid_p, rdtgrp->closid);
+	__wrmsr(MSR_IA32_PQR_ASSOC, rmid_p, plr->closid);
+
 	/*
 	 * Cache was flushed earlier. Now access kernel memory to read it
 	 * into cache region associated with just activated plr->closid.
@@ -1312,7 +1312,7 @@ int rdtgroup_pseudo_lock_create(struct rdtgroup *rdtgrp)
 
 	plr->thread_done = 0;
 
-	thread = kthread_run_on_cpu(resctrl_arch_pseudo_lock_fn, rdtgrp,
+	thread = kthread_run_on_cpu(resctrl_arch_pseudo_lock_fn, plr,
 				    plr->cpu, "pseudo_lock/%u");
 	if (IS_ERR(thread)) {
 		ret = PTR_ERR(thread);
include/linux/resctrl.h +39 -0
index 914df24ffe48..226cc5c0d765 100644
@@ -58,6 +58,45 @@ enum resctrl_conf_type {
 
 #define CDP_NUM_TYPES	(CDP_DATA + 1)
 
+/*
+ * struct pseudo_lock_region - pseudo-lock region information
+ * @s:			Resctrl schema for the resource to which this
+ *			pseudo-locked region belongs
+ * @closid:		The closid that this pseudo-locked region uses
+ * @d:			RDT domain to which this pseudo-locked region
+ *			belongs
+ * @cbm:		bitmask of the pseudo-locked region
+ * @lock_thread_wq:	waitqueue used to wait on the pseudo-locking thread
+ *			completion
+ * @thread_done:	variable used by waitqueue to test if pseudo-locking
+ *			thread completed
+ * @cpu:		core associated with the cache on which the setup code
+ *			will be run
+ * @line_size:		size of the cache lines
+ * @size:		size of pseudo-locked region in bytes
+ * @kmem:		the kernel memory associated with pseudo-locked region
+ * @minor:		minor number of character device associated with this
+ *			region
+ * @debugfs_dir:	pointer to this region's directory in the debugfs
+ *			filesystem
+ * @pm_reqs:		Power management QoS requests related to this region
+ */
+struct pseudo_lock_region {
+	struct resctrl_schema	*s;
+	u32			closid;
+	struct rdt_ctrl_domain	*d;
+	u32			cbm;
+	wait_queue_head_t	lock_thread_wq;
+	int			thread_done;
+	int			cpu;
+	unsigned int		line_size;
+	unsigned int		size;
+	void			*kmem;
+	unsigned int		minor;
+	struct dentry		*debugfs_dir;
+	struct list_head	pm_reqs;
+};
+
 /**
  * struct resctrl_staged_config - parsed configuration to be applied
  * @new_ctrl:		new ctrl value to be loaded
Signature · 1 line
2.39.5
Reviewed-by on v7 26/49
#59 lore raw

Re: [PATCH v7 26/49] x86/resctrl: Make resctrl_arch_pseudo_lock_fn() take a plr

Quoted 10 lines · James Morse

resctrl_arch_pseudo_lock_fn() has architecture specific behaviour, and takes a struct rdtgroup as an argument.

After the filesystem code moves to /fs/, the definition of struct rdtgroup will not be available to the architecture code.

The only reason resctrl_arch_pseudo_lock_fn() wants the rdtgroup is for the CLOSID. Embed that in the pseudo_lock_region as a closid, and move the definition of struct pseudo_lock_region to resctrl.h.

Quoted 6 lines

Thanks.

-Fenghua

author reviewer
#60 lore raw

PATCH v7 27/49 x86/resctrl: Move RFTYPE flags to be managed by resctrl

In reply to James Morse

resctrl_file_fflags_init() is called from the architecture specific code to make the 'thread_throttle_mode' file visible. The architecture specific code has already set the membw.throttle_mode in the rdt_resource.

This forces the RFTYPE flags used by resctrl to be exposed to the architecture specific code.

This doesn't need to be specific to the architecture, the throttle_mode can be used by resctrl to determine if the 'thread_throttle_mode' file should be visible. This allows the RFTYPE flags to be private to resctrl.

Add thread_throttle_mode_init(), and use it to call resctrl_file_fflags_init() from resctrl_init(). This avoids publishing an extra function between the architecture and filesystem code.

---
Changes since v6:
* Refer to resctrl_init() in the commit message.
* Remove a space.

Changes since v5:
* Added checking for SMBA.
* Added printing of undefined to rdt_thread_throttle_mode_show().
* Major juggling around commit 2937f9c361f7 ("x86/resctrl: Introduce resctrl_file_fflags_init() to initialize fflags")
* Dropped tags, split patch

---
 arch/x86/kernel/cpu/resctrl/core.c     |  3 ---
 arch/x86/kernel/cpu/resctrl/rdtgroup.c | 12 ++++++++++++
 2 files changed, 12 insertions(+), 3 deletions(-)
arch/x86/kernel/cpu/resctrl/core.c +0 -3
index 2c352938d18d..70421d52eceb 100644
@@ -229,9 +229,6 @@ static __init bool __get_mem_config_intel(struct rdt_resource *r)
 	else
 		r->membw.throttle_mode = THREAD_THROTTLE_MAX;
 
-	resctrl_file_fflags_init("thread_throttle_mode",
-				 RFTYPE_CTRL_INFO | RFTYPE_RES_MB);
-
 	r->alloc_capable = true;
 
 	return true;
arch/x86/kernel/cpu/resctrl/rdtgroup.c +12 -0
index e59271515a46..58feba3feefd 100644
@@ -2064,6 +2064,16 @@ static struct rftype *rdtgroup_get_rftype_by_name(const char *name)
 	return NULL;
 }
 
+static void thread_throttle_mode_init(void)
+{
+	struct rdt_resource *r_mba;
+
+	r_mba = resctrl_arch_get_resource(RDT_RESOURCE_MBA);
+	if (r_mba->membw.throttle_mode != THREAD_THROTTLE_UNDEFINED)
+		resctrl_file_fflags_init("thread_throttle_mode",
+					 RFTYPE_CTRL_INFO | RFTYPE_RES_MB);
+}
+
 void resctrl_file_fflags_init(const char *config, unsigned long fflags)
 {
 	struct rftype *rft;
@@ -4277,6 +4287,8 @@ int __init resctrl_init(void)
 
 	rdtgroup_setup_default();
 
+	thread_throttle_mode_init();
+
 	ret = resctrl_mon_resource_init();
 	if (ret)
 		return ret;
Signature · 1 line
2.39.5
Reviewed-by on v7 27/49
#61 lore raw

Re: [PATCH v7 27/49] x86/resctrl: Move RFTYPE flags to be managed by resctrl

Quoted 11 lines · James Morse

resctrl_file_fflags_init() is called from the architecture specific code to make the 'thread_throttle_mode' file visible. The architecture specific code has already set the membw.throttle_mode in the rdt_resource.

This forces the RFTYPE flags used by resctrl to be exposed to the architecture specific code.

This doesn't need to be specific to the architecture, the throttle_mode can be used by resctrl to determine if the 'thread_throttle_mode' file should be visible. This allows the RFTYPE flags to be private to resctrl.

Quoted 7 lines

Add thread_throttle_mode_init(), and use it to call resctrl_file_fflags_init() from resctrl_init(). This avoids publishing an extra function between the architecture and filesystem code.

Thanks.

-Fenghua

author reviewer
#62 lore raw

PATCH v7 28/49 x86/resctrl: Handle throttle_mode for SMBA resources

In reply to James Morse

Now that the visibility of throttle_mode is being managed by resctrl, it should consider resources other than MBA that may have a throttle_mode. SMBA is one such resource.

Extend thread_throttle_mode_init() to check SMBA for a throttle_mode.

Adding support for multiple resources means it is possible for a platform with both MBA and SMBA, but an undefined throttle_mode on one of them to make the file visible.

Add the 'undefined' case to rdt_thread_throttle_mode_show().

---
Changes since v6:
* Refer to thread_throttle_mode_init() in the commit message.

Changes since v5:
* This change split out of the previous patch.

---
 arch/x86/kernel/cpu/resctrl/rdtgroup.c | 33 +++++++++++++++++++++-----
 1 file changed, 27 insertions(+), 6 deletions(-)
arch/x86/kernel/cpu/resctrl/rdtgroup.c +27 -6
index 58feba3feefd..5fc60c9ce28f 100644
@@ -1188,10 +1188,19 @@ static int rdt_thread_throttle_mode_show(struct kernfs_open_file *of,
 	struct resctrl_schema *s = of->kn->parent->priv;
 	struct rdt_resource *r = s->res;
 
-	if (r->membw.throttle_mode == THREAD_THROTTLE_PER_THREAD)
+	switch (r->membw.throttle_mode) {
+	case THREAD_THROTTLE_PER_THREAD:
 		seq_puts(seq, "per-thread\n");
-	else
+		return 0;
+	case THREAD_THROTTLE_MAX:
 		seq_puts(seq, "max\n");
+		return 0;
+	case THREAD_THROTTLE_UNDEFINED:
+		seq_puts(seq, "undefined\n");
+		return 0;
+	}
+
+	WARN_ON_ONCE(1);
 
 	return 0;
 }
@@ -2066,12 +2075,24 @@ static struct rftype *rdtgroup_get_rftype_by_name(const char *name)
 
 static void thread_throttle_mode_init(void)
 {
-	struct rdt_resource *r_mba;
+	enum membw_throttle_mode throttle_mode = THREAD_THROTTLE_UNDEFINED;
+	struct rdt_resource *r_mba, *r_smba;
 
 	r_mba = resctrl_arch_get_resource(RDT_RESOURCE_MBA);
-	if (r_mba->membw.throttle_mode != THREAD_THROTTLE_UNDEFINED)
-		resctrl_file_fflags_init("thread_throttle_mode",
-					 RFTYPE_CTRL_INFO | RFTYPE_RES_MB);
+	if (r_mba->alloc_capable &&
+	    r_mba->membw.throttle_mode != THREAD_THROTTLE_UNDEFINED)
+		throttle_mode = r_mba->membw.throttle_mode;
+
+	r_smba = resctrl_arch_get_resource(RDT_RESOURCE_SMBA);
+	if (r_smba->alloc_capable &&
+	    r_smba->membw.throttle_mode != THREAD_THROTTLE_UNDEFINED)
+		throttle_mode = r_smba->membw.throttle_mode;
+
+	if (throttle_mode == THREAD_THROTTLE_UNDEFINED)
+		return;
+
+	resctrl_file_fflags_init("thread_throttle_mode",
+				 RFTYPE_CTRL_INFO | RFTYPE_RES_MB);
 }
 
 void resctrl_file_fflags_init(const char *config, unsigned long fflags)
Signature · 1 line
2.39.5
Reviewed-by on v7 28/49
#63 lore raw

Re: [PATCH v7 28/49] x86/resctrl: Handle throttle_mode for SMBA resources

Quoted 6 lines · James Morse

Now that the visibility of throttle_mode is being managed by resctrl, it should consider resources other than MBA that may have a throttle_mode. SMBA is one such resource.

Extend thread_throttle_mode_init() to check SMBA for a throttle_mode.

Quoted 8 lines

Adding support for multiple resources means it is possible for a platform with both MBA and SMBA, but an undefined throttle_mode on one of them to make the file visible.

Add the 'undefined' case to rdt_thread_throttle_mode_show().

Thanks.

-Fenghua

author reviewer
#64 lore raw

PATCH v7 29/49 x86/resctrl: Move get_config_index() to a header

In reply to James Morse

get_config_index() is used by the architecture specific code to map a CLOSID+type pair to an index in the configuration arrays.

MPAM needs to do this too to preserve the ABI to user-space, there is no reason to do it differently.

Move the helper to a header file to allow all architectures that either use or emulate CDP to use the same pattern of CLOSID values. Moving this to a header file means it must be marked inline, which matches the existing compiler choice for this static function.

---
Changes since v6:
* Expanded the last paragraph of the commit message.

Changes since v1:
* Reindent resctrl_get_config_index() as per coding-style.rst rules.

---
 arch/x86/kernel/cpu/resctrl/ctrlmondata.c | 19 +++----------------
 include/linux/resctrl.h                   | 15 +++++++++++++++
 2 files changed, 18 insertions(+), 16 deletions(-)
arch/x86/kernel/cpu/resctrl/ctrlmondata.c +3 -16
index 1ecc93282b7d..0a0ac5f6112e 100644
@@ -287,25 +287,12 @@ static int parse_line(char *line, struct resctrl_schema *s,
 	return -EINVAL;
 }
 
-static u32 get_config_index(u32 closid, enum resctrl_conf_type type)
-{
-	switch (type) {
-	default:
-	case CDP_NONE:
-		return closid;
-	case CDP_CODE:
-		return closid * 2 + 1;
-	case CDP_DATA:
-		return closid * 2;
-	}
-}
-
 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);
-	u32 idx = get_config_index(closid, t);
+	u32 idx = resctrl_get_config_index(closid, t);
 	struct msr_param msr_param;
 
 	if (!cpumask_test_cpu(smp_processor_id(), &d->hdr.cpu_mask))
@@ -342,7 +329,7 @@ int resctrl_arch_update_domains(struct rdt_resource *r, u32 closid)
 			if (!cfg->have_new_ctrl)
 				continue;
 
-			idx = get_config_index(closid, t);
+			idx = resctrl_get_config_index(closid, t);
 			if (cfg->new_ctrl == hw_dom->ctrl_val[idx])
 				continue;
 			hw_dom->ctrl_val[idx] = cfg->new_ctrl;
@@ -462,7 +449,7 @@ u32 resctrl_arch_get_config(struct rdt_resource *r, struct rdt_ctrl_domain *d,
 			    u32 closid, enum resctrl_conf_type type)
 {
 	struct rdt_hw_ctrl_domain *hw_dom = resctrl_to_arch_ctrl_dom(d);
-	u32 idx = get_config_index(closid, type);
+	u32 idx = resctrl_get_config_index(closid, type);
 
 	return hw_dom->ctrl_val[idx];
 }
include/linux/resctrl.h +15 -0
index 226cc5c0d765..880351ca3dfc 100644
@@ -384,6 +384,21 @@ void resctrl_arch_mon_event_config_write(void *config_info);
  */
 void resctrl_arch_mon_event_config_read(void *config_info);
 
+/* For use by arch code to remap resctrl's smaller CDP CLOSID range */
+static inline u32 resctrl_get_config_index(u32 closid,
+					   enum resctrl_conf_type type)
+{
+	switch (type) {
+	default:
+	case CDP_NONE:
+		return closid;
+	case CDP_CODE:
+		return closid * 2 + 1;
+	case CDP_DATA:
+		return closid * 2;
+	}
+}
+
 /*
  * Update the ctrl_val and apply this config right now.
  * Must be called on one of the domain's CPUs.
Signature · 1 line
2.39.5
Reviewed-by on v7 29/49
#65 lore raw

Re: [PATCH v7 29/49] x86/resctrl: Move get_config_index() to a header

Quoted 11 lines · James Morse

get_config_index() is used by the architecture specific code to map a CLOSID+type pair to an index in the configuration arrays.

MPAM needs to do this too to preserve the ABI to user-space, there is no reason to do it differently.

Move the helper to a header file to allow all architectures that either use or emulate CDP to use the same pattern of CLOSID values. Moving this to a header file means it must be marked inline, which matches the existing compiler choice for this static function.

Quoted 7 lines

Thanks.

-Fenghua

maintainer Reviewed-by on v7 29/49
#66 lore raw

Re: [PATCH v7 29/49] x86/resctrl: Move get_config_index() to a header

In reply to James Morse

Hi James,

Quoted 11 lines · James Morse

get_config_index() is used by the architecture specific code to map a CLOSID+type pair to an index in the configuration arrays.

MPAM needs to do this too to preserve the ABI to user-space, there is no reason to do it differently.

Move the helper to a header file to allow all architectures that either use or emulate CDP to use the same pattern of CLOSID values. Moving this to a header file means it must be marked inline, which matches the existing compiler choice for this static function.

Quoted 8 lines

---

Reinette

author reviewer
#67 lore raw

PATCH v7 30/49 x86/resctrl: Move get_{mon,ctrl}_domain_from_cpu() to live with their callers

In reply to James Morse

Each of get_{mon,ctrl}_domain_from_cpu() only has one caller.

Once the filesystem code is moved to /fs/, there is no equivalent to core.c.

Move these functions to each live next to their caller. This allows them to be made static and teh header file entries to be removed.

---
Changes since v6:
* This patch replaces one that moved these to a header, any arch code will need its own way of finding the domain to online/offline from a cpu number.

---
 arch/x86/kernel/cpu/resctrl/core.c     | 30 --------------------------
 arch/x86/kernel/cpu/resctrl/internal.h |  2 --
 arch/x86/kernel/cpu/resctrl/monitor.c  | 16 ++++++++++++++
 arch/x86/kernel/cpu/resctrl/rdtgroup.c | 16 ++++++++++++++
 4 files changed, 32 insertions(+), 32 deletions(-)
arch/x86/kernel/cpu/resctrl/core.c +0 -30
index 70421d52eceb..b7ce578bffa5 100644
@@ -349,36 +349,6 @@ static void cat_wrmsr(struct msr_param *m)
 		wrmsrl(hw_res->msr_base + i, hw_dom->ctrl_val[i]);
 }
 
-struct rdt_ctrl_domain *get_ctrl_domain_from_cpu(int cpu, struct rdt_resource *r)
-{
-	struct rdt_ctrl_domain *d;
-
-	lockdep_assert_cpus_held();
-
-	list_for_each_entry(d, &r->ctrl_domains, hdr.list) {
-		/* Find the domain that contains this CPU */
-		if (cpumask_test_cpu(cpu, &d->hdr.cpu_mask))
-			return d;
-	}
-
-	return NULL;
-}
-
-struct rdt_mon_domain *get_mon_domain_from_cpu(int cpu, struct rdt_resource *r)
-{
-	struct rdt_mon_domain *d;
-
-	lockdep_assert_cpus_held();
-
-	list_for_each_entry(d, &r->mon_domains, hdr.list) {
-		/* Find the domain that contains this CPU */
-		if (cpumask_test_cpu(cpu, &d->hdr.cpu_mask))
-			return d;
-	}
-
-	return NULL;
-}
-
 u32 resctrl_arch_get_num_closid(struct rdt_resource *r)
 {
 	return resctrl_to_arch_res(r)->num_closid;
arch/x86/kernel/cpu/resctrl/internal.h +0 -2
index 0d13006e920b..c44c5b496355 100644
@@ -475,8 +475,6 @@ unsigned int rdtgroup_cbm_to_size(struct rdt_resource *r, struct rdt_ctrl_domain
 				  unsigned long cbm);
 enum rdtgrp_mode rdtgroup_mode_by_closid(int closid);
 int rdtgroup_tasks_assigned(struct rdtgroup *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);
arch/x86/kernel/cpu/resctrl/monitor.c +16 -0
index 83f90128d768..a93ed7d2a160 100644
@@ -718,6 +718,22 @@ void mon_event_count(void *info)
 		rr->err = 0;
 }
 
+static struct rdt_ctrl_domain *get_ctrl_domain_from_cpu(int cpu,
+							struct rdt_resource *r)
+{
+	struct rdt_ctrl_domain *d;
+
+	lockdep_assert_cpus_held();
+
+	list_for_each_entry(d, &r->ctrl_domains, hdr.list) {
+		/* Find the domain that contains this CPU */
+		if (cpumask_test_cpu(cpu, &d->hdr.cpu_mask))
+			return d;
+	}
+
+	return NULL;
+}
+
 /*
  * Feedback loop for MBA software controller (mba_sc)
  *
arch/x86/kernel/cpu/resctrl/rdtgroup.c +16 -0
index 5fc60c9ce28f..c6274d40b217 100644
@@ -4257,6 +4257,22 @@ static void clear_childcpus(struct rdtgroup *r, unsigned int cpu)
 	}
 }
 
+static struct rdt_mon_domain *get_mon_domain_from_cpu(int cpu,
+						      struct rdt_resource *r)
+{
+	struct rdt_mon_domain *d;
+
+	lockdep_assert_cpus_held();
+
+	list_for_each_entry(d, &r->mon_domains, hdr.list) {
+		/* Find the domain that contains this CPU */
+		if (cpumask_test_cpu(cpu, &d->hdr.cpu_mask))
+			return d;
+	}
+
+	return NULL;
+}
+
 void resctrl_offline_cpu(unsigned int cpu)
 {
 	struct rdt_resource *l3 = resctrl_arch_get_resource(RDT_RESOURCE_L3);
Signature · 1 line
2.39.5
Reviewed-by on v7 30/49
#68 lore raw

Re: [PATCH v7 30/49] x86/resctrl: Move get_{mon,ctrl}_domain_from_cpu() to live with their callers

Quoted 9 lines · James Morse

Each of get_{mon,ctrl}_domain_from_cpu() only has one caller.

Once the filesystem code is moved to /fs/, there is no equivalent to core.c.

Move these functions to each live next to their caller. This allows them to be made static and teh header file entries to be removed.

Thanks.

-Fenghua

maintainer
#69 lore raw

Re: [PATCH v7 30/49] x86/resctrl: Move get_{mon,ctrl}_domain_from_cpu() to live with their callers

In reply to James Morse

Hi James,

Quoted 7 lines · James Morse

Each of get_{mon,ctrl}_domain_from_cpu() only has one caller.

Once the filesystem code is moved to /fs/, there is no equivalent to core.c.

Move these functions to each live next to their caller. This allows them to be made static and teh header file entries to be removed.

"teh" -> "the"

(found by checkpatch.pl --codespell ...)

Quoted 3 lines

---

| Reviewed-by: Reinette Chatre <[email protected]>

Reinette

author reviewer
#70 lore raw

PATCH v7 31/49 x86/resctrl: Remove the limit on the number of CLOSID

In reply to James Morse

From: Amit Singh Tomar <[email protected]>

Resctrl allocates and finds free CLOSID values using the bits of a u32. This restricts the number of control groups that can be created by user-space.

MPAM has an architectural limit of 2^16 CLOSID values, Intel x86 could be extended beyond 32 values. There is at least one MPAM platform which supports more than 32 CLOSID values.

Replace the fixed size bitmap with calls to the bitmap API to allocate an array of a sufficient size.

ffs() returns '1' for bit 0, hence the existing code subtracts 1 from the index to get the CLOSID value. find_first_bit() returns the bit number which does not need adjusting.

[ morse: fixed the off-by-one in the allocator and the wrong
not-found value. Removed the limit. Rephrase the commit message. ]

---
Changes since v6:
* Set variable to NULL after kfree()ing it.
* Call closid_exit() from rdt_kill_sb() to prevent a memory leak.

Changes since v5:
* This patch got pulled into this series.

---
 arch/x86/kernel/cpu/resctrl/rdtgroup.c | 47 +++++++++++++++++---------
 1 file changed, 31 insertions(+), 16 deletions(-)
arch/x86/kernel/cpu/resctrl/rdtgroup.c +31 -16
index c6274d40b217..5f391e8b5746 100644
@@ -130,8 +130,8 @@ static bool resctrl_is_mbm_event(int e)
 }
 
 /*
- * Trivial allocator for CLOSIDs. Since h/w only supports a small number,
- * we can keep a bitmap of free CLOSIDs in a single integer.
+ * Trivial allocator for CLOSIDs. Use BITMAP APIs to manipulate a bitmap
+ * of free CLOSIDs.
  *
  * Using a global CLOSID across all resources has some advantages and
  * some drawbacks:
@@ -144,7 +144,7 @@ static bool resctrl_is_mbm_event(int e)
  * - Our choices on how to configure each resource become progressively more
  *   limited as the number of resources grows.
  */
-static unsigned long closid_free_map;
+static unsigned long *closid_free_map;
 static int closid_free_map_len;
 
 int closids_supported(void)
@@ -152,20 +152,31 @@ int closids_supported(void)
 	return closid_free_map_len;
 }
 
-static void closid_init(void)
+static int closid_init(void)
 {
 	struct resctrl_schema *s;
-	u32 rdt_min_closid = 32;
+	u32 rdt_min_closid = ~0;
 
 	/* Compute rdt_min_closid across all resources */
 	list_for_each_entry(s, &resctrl_schema_all, list)
 		rdt_min_closid = min(rdt_min_closid, s->num_closid);
 
-	closid_free_map = BIT_MASK(rdt_min_closid) - 1;
+	closid_free_map = bitmap_alloc(rdt_min_closid, GFP_KERNEL);
+	if (!closid_free_map)
+		return -ENOMEM;
+	bitmap_fill(closid_free_map, rdt_min_closid);
 
 	/* RESCTRL_RESERVED_CLOSID is always reserved for the default group */
-	__clear_bit(RESCTRL_RESERVED_CLOSID, &closid_free_map);
+	__clear_bit(RESCTRL_RESERVED_CLOSID, closid_free_map);
 	closid_free_map_len = rdt_min_closid;
+
+	return 0;
+}
+
+static void closid_exit(void)
+{
+	bitmap_free(closid_free_map);
+	closid_free_map = NULL;
 }
 
 static int closid_alloc(void)
@@ -182,12 +193,11 @@ static int closid_alloc(void)
 			return cleanest_closid;
 		closid = cleanest_closid;
 	} else {
-		closid = ffs(closid_free_map);
-		if (closid == 0)
+		closid = find_first_bit(closid_free_map, closid_free_map_len);
+		if (closid == closid_free_map_len)
 			return -ENOSPC;
-		closid--;
 	}
-	__clear_bit(closid, &closid_free_map);
+	__clear_bit(closid, closid_free_map);
 
 	return closid;
 }
@@ -196,7 +206,7 @@ void closid_free(int closid)
 {
 	lockdep_assert_held(&rdtgroup_mutex);
 
-	__set_bit(closid, &closid_free_map);
+	__set_bit(closid, closid_free_map);
 }
 
 /**
@@ -210,7 +220,7 @@ bool closid_allocated(unsigned int closid)
 {
 	lockdep_assert_held(&rdtgroup_mutex);
 
-	return !test_bit(closid, &closid_free_map);
+	return !test_bit(closid, closid_free_map);
 }
 
 /**
@@ -2754,20 +2764,22 @@ static int rdt_get_tree(struct fs_context *fc)
 		goto out_ctx;
 	}
 
-	closid_init();
+	ret = closid_init();
+	if (ret)
+		goto out_schemata_free;
 
 	if (resctrl_arch_mon_capable())
 		flags |= RFTYPE_MON;
 
 	ret = rdtgroup_add_files(rdtgroup_default.kn, flags);
 	if (ret)
-		goto out_schemata_free;
+		goto out_closid_exit;
 
 	kernfs_activate(rdtgroup_default.kn);
 
 	ret = rdtgroup_create_info_dir(rdtgroup_default.kn);
 	if (ret < 0)
-		goto out_schemata_free;
+		goto out_closid_exit;
 
 	if (resctrl_arch_mon_capable()) {
 		ret = mongroup_create_dir(rdtgroup_default.kn,
@@ -2818,6 +2830,8 @@ static int rdt_get_tree(struct fs_context *fc)
 		kernfs_remove(kn_mongrp);
 out_info:
 	kernfs_remove(kn_info);
+out_closid_exit:
+	closid_exit();
 out_schemata_free:
 	schemata_list_destroy();
 out_ctx:
@@ -3071,6 +3085,7 @@ static void rdt_kill_sb(struct super_block *sb)
 		resctrl_arch_disable_alloc();
 	if (resctrl_arch_mon_capable())
 		resctrl_arch_disable_mon();
+	closid_exit();
 	resctrl_mounted = false;
 	kernfs_kill_sb(sb);
 	mutex_unlock(&rdtgroup_mutex);
Signature · 1 line
2.39.5
Reviewed-by on v7 31/49
#71 lore raw

Re: [PATCH v7 31/49] x86/resctrl: Remove the limit on the number of CLOSID

Quoted 13 lines · James Morse

From: Amit Singh Tomar <[email protected]>

Resctrl allocates and finds free CLOSID values using the bits of a u32. This restricts the number of control groups that can be created by user-space.

MPAM has an architectural limit of 2^16 CLOSID values, Intel x86 could be extended beyond 32 values. There is at least one MPAM platform which supports more than 32 CLOSID values.

Replace the fixed size bitmap with calls to the bitmap API to allocate an array of a sufficient size.

Quoted 8 lines

ffs() returns '1' for bit 0, hence the existing code subtracts 1 from the index to get the CLOSID value. find_first_bit() returns the bit number which does not need adjusting.

[ morse: fixed the off-by-one in the allocator and the wrong
not-found value. Removed the limit. Rephrase the commit message. ]

Thanks.

-Fenghua

maintainer
#72 lore raw

Re: [PATCH v7 31/49] x86/resctrl: Remove the limit on the number of CLOSID

In reply to James Morse

Hi James,

Quoted 21 lines · James Morse

From: Amit Singh Tomar <[email protected]>

Resctrl allocates and finds free CLOSID values using the bits of a u32. This restricts the number of control groups that can be created by user-space.

MPAM has an architectural limit of 2^16 CLOSID values, Intel x86 could be extended beyond 32 values. There is at least one MPAM platform which supports more than 32 CLOSID values.

Replace the fixed size bitmap with calls to the bitmap API to allocate an array of a sufficient size.

ffs() returns '1' for bit 0, hence the existing code subtracts 1 from the index to get the CLOSID value. find_first_bit() returns the bit number which does not need adjusting.

[ morse: fixed the off-by-one in the allocator and the wrong
not-found value. Removed the limit. Rephrase the commit message. ]

...

arch/x86/kernel/cpu/resctrl/rdtgroup.c static void rdt_kill_sb(struct super_block *sb) · line 3091

Quoted 8 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +1 -0
@@ -3071,6 +3085,7 @@ static void rdt_kill_sb(struct super_block *sb)
 		resctrl_arch_disable_alloc();
 	if (resctrl_arch_mon_capable())
 		resctrl_arch_disable_mon();
+	closid_exit();
 	resctrl_mounted = false;
 	kernfs_kill_sb(sb);
 	mutex_unlock(&rdtgroup_mutex);

Above is the new change in this patch ... I am trying to understand the choice in ordering since I expect that freeing resources is done in opposite order from what it was allocated. I thus expected it to be before schemata_list_destroy() but it is instead done as the last thing before removing the superblock.

The changelog does not mention dependencies that need to be kept in mind. I thought that there may be something going on with open files ... for example if user kept "bit_usage" (that calls closid_allocated() that depends on the closid_free_map) but a quick test confirmed that if a file is open then an attempt to unmount will get a resource busy error. So rdt_kill_sb() will not even start while a file is open. Specifically, user sees a "umount: /sys/fs/resctrl: target is busy"

What am I missing?

Reinette

author reviewer
#73 lore raw

Re: [PATCH v7 31/49] x86/resctrl: Remove the limit on the number of CLOSID

Hi Reinette,

Quoted 41 lines · Reinette Chatre
Quoted 18 lines · James Morse

From: Amit Singh Tomar <[email protected]>

Resctrl allocates and finds free CLOSID values using the bits of a u32. This restricts the number of control groups that can be created by user-space.

MPAM has an architectural limit of 2^16 CLOSID values, Intel x86 could be extended beyond 32 values. There is at least one MPAM platform which supports more than 32 CLOSID values.

Replace the fixed size bitmap with calls to the bitmap API to allocate an array of a sufficient size.

ffs() returns '1' for bit 0, hence the existing code subtracts 1 from the index to get the CLOSID value. find_first_bit() returns the bit number which does not need adjusting.

arch/x86/kernel/cpu/resctrl/rdtgroup.c static void rdt_kill_sb(struct super_block *sb) · line 3091

Quoted 7 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +1 -0
@@ -3071,6 +3085,7 @@ static void rdt_kill_sb(struct super_block *sb)
 		resctrl_arch_disable_alloc();
 	if (resctrl_arch_mon_capable())
 		resctrl_arch_disable_mon();
+	closid_exit();
 	resctrl_mounted = false;
 	kernfs_kill_sb(sb);
 	mutex_unlock(&rdtgroup_mutex);

Above is the new change in this patch ... I am trying to understand the choice in ordering since I expect that freeing resources is done in opposite order from what it was allocated. I thus expected it to be before schemata_list_destroy() but it is instead done as the last thing before removing the superblock.

The changelog does not mention dependencies that need to be kept in mind. I thought that there may be something going on with open files ... for example if user kept "bit_usage" (that calls closid_allocated() that depends on the closid_free_map) but a quick test confirmed that if a file is open then an attempt to unmount will get a resource busy error. So rdt_kill_sb() will not even start while a file is open. Specifically, user sees a "umount: /sys/fs/resctrl: target is busy"

Quoted 1 line

What am I missing?

I just shoved it at the end of the list, but before anything 'outside' resctrl. I'll change it to be the opposite of the order in rdt_get_tree().

Thanks,

James

author reviewer
#74 lore raw

PATCH v7 32/49 x86/resctrl: Rename resctrl_sched_in() to begin with "resctrl_arch_"

In reply to James Morse

resctrl_sched_in() loads the architecture specific CPU MSRs with the CLOSID and RMID values. This function was named before resctrl was split to have architecture specific code, and generic filesystem code.

This function is obviously architecture specific, but does not begin with 'resctrl_arch_', making it the odd one out in the functions an architecture needs to support to enable resctrl.

Rename it for consistency. This is purely cosmetic.

---
 arch/x86/include/asm/resctrl.h         |  4 ++--
 arch/x86/kernel/cpu/resctrl/rdtgroup.c | 12 ++++++------
 arch/x86/kernel/process_32.c           |  2 +-
 arch/x86/kernel/process_64.c           |  2 +-
 4 files changed, 10 insertions(+), 10 deletions(-)
arch/x86/include/asm/resctrl.h +2 -2
index 011bf67a1866..7a39728b0743 100644
@@ -175,7 +175,7 @@ static inline bool resctrl_arch_match_rmid(struct task_struct *tsk, u32 ignored,
 	return READ_ONCE(tsk->rmid) == rmid;
 }
 
-static inline void resctrl_sched_in(struct task_struct *tsk)
+static inline void resctrl_arch_sched_in(struct task_struct *tsk)
 {
 	if (static_branch_likely(&rdt_enable_key))
 		__resctrl_sched_in(tsk);
@@ -212,7 +212,7 @@ void resctrl_cpu_detect(struct cpuinfo_x86 *c);
 
 #else
 
-static inline void resctrl_sched_in(struct task_struct *tsk) {}
+static inline void resctrl_arch_sched_in(struct task_struct *tsk) {}
 static inline void resctrl_cpu_detect(struct cpuinfo_x86 *c) {}
 
 #endif /* CONFIG_X86_CPU_RESCTRL */
arch/x86/kernel/cpu/resctrl/rdtgroup.c +6 -6
index 5f391e8b5746..2f34b7215679 100644
@@ -372,7 +372,7 @@ static int rdtgroup_cpus_show(struct kernfs_open_file *of,
 }
 
 /*
- * This is safe against resctrl_sched_in() called from __switch_to()
+ * This is safe against resctrl_arch_sched_in() called from __switch_to()
  * because __switch_to() is executed with interrupts disabled. A local call
  * from update_closid_rmid() is protected against __switch_to() because
  * preemption is disabled.
@@ -391,7 +391,7 @@ void resctrl_arch_sync_cpu_closid_rmid(void *info)
 	 * executing task might have its own closid selected. Just reuse
 	 * the context switch code.
 	 */
-	resctrl_sched_in(current);
+	resctrl_arch_sched_in(current);
 }
 
 /*
@@ -616,7 +616,7 @@ static void _update_task_closid_rmid(void *task)
 	 * Otherwise, the MSR is updated when the task is scheduled in.
 	 */
 	if (task == current)
-		resctrl_sched_in(task);
+		resctrl_arch_sched_in(task);
 }
 
 static void update_task_closid_rmid(struct task_struct *t)
@@ -674,7 +674,7 @@ static int __rdtgroup_move_task(struct task_struct *tsk,
 	 * Ensure the task's closid and rmid are written before determining if
 	 * the task is current that will decide if it will be interrupted.
 	 * This pairs with the full barrier between the rq->curr update and
-	 * resctrl_sched_in() during context switch.
+	 * resctrl_arch_sched_in() during context switch.
 	 */
 	smp_mb();
 
@@ -2979,8 +2979,8 @@ static void rdt_move_group_tasks(struct rdtgroup *from, struct rdtgroup *to,
 			/*
 			 * Order the closid/rmid stores above before the loads
 			 * in task_curr(). This pairs with the full barrier
-			 * between the rq->curr update and resctrl_sched_in()
-			 * during context switch.
+			 * between the rq->curr update and
+			 * resctrl_arch_sched_in() during context switch.
 			 */
 			smp_mb();
 
arch/x86/kernel/process_32.c +1 -1
index 0917c7f25720..8697b02dabf1 100644
@@ -211,7 +211,7 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
 	switch_fpu_finish(next_p);
 
 	/* Load the Intel cache allocation PQR MSR. */
-	resctrl_sched_in(next_p);
+	resctrl_arch_sched_in(next_p);
 
 	return prev_p;
 }
arch/x86/kernel/process_64.c +1 -1
index 226472332a70..3f1235d3bf1d 100644
@@ -707,7 +707,7 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
 	}
 
 	/* Load the Intel cache allocation PQR MSR. */
-	resctrl_sched_in(next_p);
+	resctrl_arch_sched_in(next_p);
 
 	return prev_p;
 }
Signature · 1 line
2.39.5
Reviewed-by on v7 32/49
#75 lore raw

Re: [PATCH v7 32/49] x86/resctrl: Rename resctrl_sched_in() to begin with "resctrl_arch_"

Quoted 8 lines · James Morse

resctrl_sched_in() loads the architecture specific CPU MSRs with the CLOSID and RMID values. This function was named before resctrl was split to have architecture specific code, and generic filesystem code.

This function is obviously architecture specific, but does not begin with 'resctrl_arch_', making it the odd one out in the functions an architecture needs to support to enable resctrl.

Quoted 8 lines

Rename it for consistency. This is purely cosmetic.

Thanks.

-Fenghua

author reviewer
#76 lore raw

PATCH v7 33/49 x86/resctrl: resctrl_exit() teardown resctrl but leave the mount point

In reply to James Morse

resctrl_exit() was intended for use when the 'resctrl' module was unloaded. resctrl can't be built as a module, and the kernfs helpers are not exported so this is unlikely to change. MPAM has an error interrupt which indicates the MPAM driver has gone haywire. Should this occur tasks could run with the wrong control values, leading to bad performance for important tasks. The MPAM driver needs a way to tell resctrl that no further configuration should be attempted.

Using resctrl_exit() for this leaves the system in a funny state as resctrl is still mounted, but cannot be un-mounted because the sysfs directory that is typically used has been removed. Dave Martin suggests this may cause systemd trouble in the future as not all filesystems can be unmounted.

Add calls to remove all the files and directories in resctrl, and remove the sysfs_remove_mount_point() call that leaves the system in a funny state. When triggered, this causes all the resctrl files to disappear. resctrl can be unmounted, but not mounted again.

---
Changes since v6:
* Added kdoc and comment to resctrl_exit().

Changes since v5:
* Serialise rdtgroup_destroy_root() against umount().
* Check rdtgroup_default.kn to protect against duplicate calls.

---
 arch/x86/kernel/cpu/resctrl/rdtgroup.c | 25 ++++++++++++++++++++++---
 1 file changed, 22 insertions(+), 3 deletions(-)
arch/x86/kernel/cpu/resctrl/rdtgroup.c +22 -3
index 2f34b7215679..0d74a6d98dba 100644
@@ -4094,8 +4094,12 @@ static int rdtgroup_setup_root(struct rdt_fs_context *ctx)
 
 static void rdtgroup_destroy_root(void)
 {
-	kernfs_destroy_root(rdt_root);
-	rdtgroup_default.kn = NULL;
+	lockdep_assert_held(&rdtgroup_mutex);
+
+	if (rdtgroup_default.kn) {
+		kernfs_destroy_root(rdt_root);
+		rdtgroup_default.kn = NULL;
+	}
 }
 
 static void __init rdtgroup_setup_default(void)
@@ -4387,11 +4391,26 @@ int __init resctrl_init(void)
 	return ret;
 }
 
+/**
+ * resctrl_exit() - Remove the resctrl filesystem and free resources.
+ *
+ * Called by the architecture code in response to a fatal error.
+ * Resctrl files and structures are removed from kernfs to prevent further
+ * configuration.
+ */
 void __exit resctrl_exit(void)
 {
+	mutex_lock(&rdtgroup_mutex);
+	rdtgroup_destroy_root();
+	mutex_unlock(&rdtgroup_mutex);
+
 	debugfs_remove_recursive(debugfs_resctrl);
 	unregister_filesystem(&rdt_fs_type);
-	sysfs_remove_mount_point(fs_kobj, "resctrl");
+
+	/*
+	 * The sysfs mount point added by resctrl_init() is not removed so that
+	 * it can be used to umount resctrl.
+	 */
 
 	resctrl_mon_resource_exit();
 }
Signature · 1 line
2.39.5
maintainer
#77 lore raw

Re: [PATCH v7 33/49] x86/resctrl: resctrl_exit() teardown resctrl but leave the mount point

Hi James,

Quoted 72 lines · James Morse

resctrl_exit() was intended for use when the 'resctrl' module was unloaded. resctrl can't be built as a module, and the kernfs helpers are not exported so this is unlikely to change. MPAM has an error interrupt which indicates the MPAM driver has gone haywire. Should this occur tasks could run with the wrong control values, leading to bad performance for important tasks. The MPAM driver needs a way to tell resctrl that no further configuration should be attempted.

Using resctrl_exit() for this leaves the system in a funny state as resctrl is still mounted, but cannot be un-mounted because the sysfs directory that is typically used has been removed. Dave Martin suggests this may cause systemd trouble in the future as not all filesystems can be unmounted.

Add calls to remove all the files and directories in resctrl, and remove the sysfs_remove_mount_point() call that leaves the system in a funny state. When triggered, this causes all the resctrl files to disappear. resctrl can be unmounted, but not mounted again.

---
Changes since v6:
* Added kdoc and comment to resctrl_exit().

Changes since v5:
* Serialise rdtgroup_destroy_root() against umount().
* Check rdtgroup_default.kn to protect against duplicate calls.

---
 arch/x86/kernel/cpu/resctrl/rdtgroup.c | 25 ++++++++++++++++++++++---
 1 file changed, 22 insertions(+), 3 deletions(-)
arch/x86/kernel/cpu/resctrl/rdtgroup.c +17 -2
index 2f34b7215679..0d74a6d98dba 100644
@@ -4094,8 +4094,12 @@ static int rdtgroup_setup_root(struct rdt_fs_context *ctx)
 
 static void rdtgroup_destroy_root(void)
 {
-	kernfs_destroy_root(rdt_root);
-	rdtgroup_default.kn = NULL;
+	lockdep_assert_held(&rdtgroup_mutex);
+
+	if (rdtgroup_default.kn) {
+		kernfs_destroy_root(rdt_root);
+		rdtgroup_default.kn = NULL;
+	}
 }
 
 static void __init rdtgroup_setup_default(void)
@@ -4387,11 +4391,26 @@ int __init resctrl_init(void)
 	return ret;
 }
 
+/**
+ * resctrl_exit() - Remove the resctrl filesystem and free resources.
+ *
+ * Called by the architecture code in response to a fatal error.
+ * Resctrl files and structures are removed from kernfs to prevent further
+ * configuration.
+ */
 void __exit resctrl_exit(void)
 {
+	mutex_lock(&rdtgroup_mutex);
+	rdtgroup_destroy_root();
+	mutex_unlock(&rdtgroup_mutex);
+
 	debugfs_remove_recursive(debugfs_resctrl);

arch/x86/kernel/cpu/resctrl/rdtgroup.c int __init resctrl_init(void) · line 4416

Quoted 10 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +5 -1
 	unregister_filesystem(&rdt_fs_type);
-	sysfs_remove_mount_point(fs_kobj, "resctrl");
+
+	/*
+	 * The sysfs mount point added by resctrl_init() is not removed so that
+	 * it can be used to umount resctrl.
+	 */
 
 	resctrl_mon_resource_exit();
 }

(copying v6 discussion here)

Quoted 18 lines · James Morse
Quoted 17 lines · Reinette Chatre
Quoted 9 lines · James Morse
Quoted 4 lines · Reinette Chatre

It is difficult for me to follow the kernfs reference counting required to make this work. Specifically, the root kn is "destroyed" here but it is required to stick around until unmount when the rest of the files are removed.

This drops resctrl's reference to all of the files, which would make the files disappear. unmount is what calls kernfs_kill_sb(), which gets rid of the root of the filesystem.

My concern is mostly with the kernfs_remove() calls in the rdt_kill_sb()->rmdir_all_sub() flow. For example:

	kernfs_remove(kn_info);
	kernfs_remove(kn_mongrp);
	kernfs_remove(kn_mondata);
Quoted 5 lines
Quoted 1 line

As I understand the above require the destroyed root to still be around.

Right - because rdt_get_tree() has these global pointers into the hierarchy, but doesn't take a reference. rmdir_all_sub() relies on always being called before rdtgroup_destroy_root().

Is this a known issue then? Since I am not able to use your test I created something new after thinking there would be no response to my comment and indeed on unmount:

[  293.707228] BUG: KASAN: slab-use-after-free in kernfs_remove+0x87/0xa0
[  293.714718] Read of size 8 at addr ff11000309d88f30 by task umount/3793
Quoted 8 lines

The point hack would be for rdtgroup_destroy_root() to NULL out those global pointers, (I note they are left dangling) - that would make a subsequent call to rmdir_all_sub() harmless.

A better fix would be to pull out all the filesystem relevant parts from rdt_kill_sb(), make that safe for multiple calls and get resctrl_exit() to call that.
A call to rdt_kill_sb() after resctrl_exit() would just cleanup the super-block. This will leave things in a more predictable state.

Why just the filesystem relevant parts? Although, you also state "resctrl_exit() would just cleanup the super-block" that sounds like you are thinking about pulling out all reset work. This sounds reasonable to me. It really feels more appropriate to do proper cleanup and not just wipe the root while leaving everything else underneath it.

Reinette

author reviewer
#78 lore raw

Re: [PATCH v7 33/49] x86/resctrl: resctrl_exit() teardown resctrl but leave the mount point

Hi Reinette,

Quoted 41 lines · Reinette Chatre
Quoted 14 lines · James Morse

resctrl_exit() was intended for use when the 'resctrl' module was unloaded. resctrl can't be built as a module, and the kernfs helpers are not exported so this is unlikely to change. MPAM has an error interrupt which indicates the MPAM driver has gone haywire. Should this occur tasks could run with the wrong control values, leading to bad performance for important tasks. The MPAM driver needs a way to tell resctrl that no further configuration should be attempted.

Using resctrl_exit() for this leaves the system in a funny state as resctrl is still mounted, but cannot be un-mounted because the sysfs directory that is typically used has been removed. Dave Martin suggests this may cause systemd trouble in the future as not all filesystems can be unmounted.

Quoted 4 lines

Add calls to remove all the files and directories in resctrl, and remove the sysfs_remove_mount_point() call that leaves the system in a funny state. When triggered, this causes all the resctrl files to disappear. resctrl can be unmounted, but not mounted again.

(copying v6 discussion here)

Quoted 18 lines · James Morse
Quoted 17 lines · Reinette Chatre
Quoted 9 lines · James Morse
Quoted 4 lines · Reinette Chatre

It is difficult for me to follow the kernfs reference counting required to make this work. Specifically, the root kn is "destroyed" here but it is required to stick around until unmount when the rest of the files are removed.

This drops resctrl's reference to all of the files, which would make the files disappear. unmount is what calls kernfs_kill_sb(), which gets rid of the root of the filesystem.

My concern is mostly with the kernfs_remove() calls in the rdt_kill_sb()->rmdir_all_sub() flow. For example:

	kernfs_remove(kn_info);
	kernfs_remove(kn_mongrp);
	kernfs_remove(kn_mondata);
Quoted 7 lines
Quoted 5 lines
Quoted 1 line

As I understand the above require the destroyed root to still be around.

Right - because rdt_get_tree() has these global pointers into the hierarchy, but doesn't take a reference. rmdir_all_sub() relies on always being called before rdtgroup_destroy_root().

Is this a known issue then?

Its just a subtle thing that resctrl was relying on, and I didn't spot. Thanks for pointing it out!

Quoted 7 lines

Since I am not able to use your test I created something new after thinking there would be no response to my comment and indeed on unmount:

[  293.707228] BUG: KASAN: slab-use-after-free in kernfs_remove+0x87/0xa0
[  293.714718] Read of size 8 at addr ff11000309d88f30 by task umount/3793
Quoted 3 lines

The point hack would be for rdtgroup_destroy_root() to NULL out those global pointers, (I note they are left dangling) - that would make a subsequent call to rmdir_all_sub() harmless.

Quoted 7 lines
Quoted 4 lines

A better fix would be to pull out all the filesystem relevant parts from rdt_kill_sb(), make that safe for multiple calls and get resctrl_exit() to call that.
A call to rdt_kill_sb() after resctrl_exit() would just cleanup the super-block. This will leave things in a more predictable state.

Why just the filesystem relevant parts?

The stated aim is to prevent any further configuration of the hardware.
We can change that to 'unmount as much as possible'. I didn't think of it like that as I couldn't find an in-kernel way of triggering a umount(). (and the mount may be copied into numerous namespaces)

Quoted 4 lines

Although, you also state "resctrl_exit() would just
cleanup the super-block" that sounds like you are thinking about pulling out all reset work. This sounds reasonable to me. It really feels more appropriate to do proper cleanup and not just wipe the root while leaving everything else underneath it.

After this discussion, my new proposal is to do this:
-------%<-------

arch/x86/kernel/cpu/resctrl/rdtgroup.c +16 -0
index 0d74a6d98dba..cee4604a59c0 100644
@@ -3063,6 +3063,21 @@ static void rmdir_all_sub(void)
        kernfs_remove(kn_mondata);
 }
 
+static void resctrl_fs_teardown(void)
+{
+       lockdep_assert_held(&rdtgroup_mutex);
+
+       /* Cleared by rdtgroup_destroy_root() */
+       if (!rdtgroup_default.kn)
+               return;
+
+       rmdir_all_sub();
+       rdt_pseudo_lock_release();
+       rdtgroup_default.mode = RDT_MODE_SHAREABLE;
+       schemata_list_destroy();
+       closid_exit();
+       rdtgroup_destroy_root();
+}
+
 static void rdt_kill_sb(struct super_block *sb)
 {
        struct rdt_resource *r;
@@ -3076,11 +3091,8 @@ static void rdt_kill_sb(struct super_block *sb)
        for_each_alloc_capable_rdt_resource(r)
                resctrl_arch_reset_all_ctrls(r);
-       rmdir_all_sub();
-       rdt_pseudo_lock_release();
-       rdtgroup_default.mode = RDT_MODE_SHAREABLE;
-       schemata_list_destroy();
-       rdtgroup_destroy_root();
+       resctrl_fs_teardown();
+
        if (resctrl_arch_alloc_capable())
                resctrl_arch_disable_alloc();
        if (resctrl_arch_mon_capable())
                resctrl_arch_disable_mon();
-       closid_exit();
        resctrl_mounted = false;
        kernfs_kill_sb(sb);
        mutex_unlock(&rdtgroup_mutex);

-------%<-------

and call resctrl_fs_teardown() from resctrl_exit().

This leaves a bunch of resctrl_arch_ calls behind, the reason is its the arch code that calls resctrl_exit(), so it probably doesn't need to be told to disable things. For MPAM, the work of resctrl_arch_reset_all_ctrls() will already have been done - and all these calls will fail because the MPAM driver is blocking further access to the hardware.

Thanks,

James

maintainer
#79 lore raw

Re: [PATCH v7 33/49] x86/resctrl: resctrl_exit() teardown resctrl but leave the mount point

Hi James,

Quoted 69 lines · James Morse

Hi Reinette,

Quoted 41 lines · Reinette Chatre
Quoted 14 lines · James Morse

resctrl_exit() was intended for use when the 'resctrl' module was unloaded. resctrl can't be built as a module, and the kernfs helpers are not exported so this is unlikely to change. MPAM has an error interrupt which indicates the MPAM driver has gone haywire. Should this occur tasks could run with the wrong control values, leading to bad performance for important tasks. The MPAM driver needs a way to tell resctrl that no further configuration should be attempted.

Using resctrl_exit() for this leaves the system in a funny state as resctrl is still mounted, but cannot be un-mounted because the sysfs directory that is typically used has been removed. Dave Martin suggests this may cause systemd trouble in the future as not all filesystems can be unmounted.

Quoted 4 lines

Add calls to remove all the files and directories in resctrl, and remove the sysfs_remove_mount_point() call that leaves the system in a funny state. When triggered, this causes all the resctrl files to disappear. resctrl can be unmounted, but not mounted again.

(copying v6 discussion here)

Quoted 18 lines · James Morse
Quoted 17 lines · Reinette Chatre
Quoted 9 lines · James Morse
Quoted 4 lines · Reinette Chatre

It is difficult for me to follow the kernfs reference counting required to make this work. Specifically, the root kn is "destroyed" here but it is required to stick around until unmount when the rest of the files are removed.

This drops resctrl's reference to all of the files, which would make the files disappear. unmount is what calls kernfs_kill_sb(), which gets rid of the root of the filesystem.

My concern is mostly with the kernfs_remove() calls in the rdt_kill_sb()->rmdir_all_sub() flow. For example:

	kernfs_remove(kn_info);
	kernfs_remove(kn_mongrp);
	kernfs_remove(kn_mondata);
Quoted 7 lines
Quoted 5 lines
Quoted 1 line

As I understand the above require the destroyed root to still be around.

Right - because rdt_get_tree() has these global pointers into the hierarchy, but doesn't take a reference. rmdir_all_sub() relies on always being called before rdtgroup_destroy_root().

Is this a known issue then?

Its just a subtle thing that resctrl was relying on, and I didn't spot. Thanks for pointing it out!

Quoted 11 lines

Since I am not able to use your test I created something new after thinking there would be no response to my comment and indeed on unmount:

[  293.707228] BUG: KASAN: slab-use-after-free in kernfs_remove+0x87/0xa0
[  293.714718] Read of size 8 at addr ff11000309d88f30 by task umount/3793
Quoted 7 lines

The point hack would be for rdtgroup_destroy_root() to NULL out those global pointers, (I note they are left dangling) - that would make a subsequent call to rmdir_all_sub() harmless.

A better fix would be to pull out all the filesystem relevant parts from rdt_kill_sb(), make that safe for multiple calls and get resctrl_exit() to call that.
A call to rdt_kill_sb() after resctrl_exit() would just cleanup the super-block. This will leave things in a more predictable state.

Quoted 6 lines
Quoted 1 line

Why just the filesystem relevant parts?

The stated aim is to prevent any further configuration of the hardware.
We can change that to 'unmount as much as possible'. I didn't think of it like that as I couldn't find an in-kernel way of triggering a umount(). (and the mount may be copied into numerous namespaces)

deactivate_locked_super() looked promising when I started digging but I quickly found myself in unfamiliar territory.

Quoted 55 lines
Quoted 6 lines

Although, you also state "resctrl_exit() would just
cleanup the super-block" that sounds like you are thinking about pulling out all reset work. This sounds reasonable to me. It really feels more appropriate to do proper cleanup and not just wipe the root while leaving everything else underneath it.

After this discussion, my new proposal is to do this:
-------%<-------

arch/x86/kernel/cpu/resctrl/rdtgroup.c +18 -5
index 0d74a6d98dba..cee4604a59c0 100644
@@ -3063,6 +3063,21 @@ static void rmdir_all_sub(void)
        kernfs_remove(kn_mondata);
 }
 
+static void resctrl_fs_teardown(void)
+{
+       lockdep_assert_held(&rdtgroup_mutex);
+
+       /* Cleared by rdtgroup_destroy_root() */
+       if (!rdtgroup_default.kn)
+               return;
+
+       rmdir_all_sub();
+       rdt_pseudo_lock_release();
+       rdtgroup_default.mode = RDT_MODE_SHAREABLE;
+       schemata_list_destroy();
+       closid_exit();
+       rdtgroup_destroy_root();
+}
+
 static void rdt_kill_sb(struct super_block *sb)
 {
        struct rdt_resource *r;
@@ -3076,11 +3091,8 @@ static void rdt_kill_sb(struct super_block *sb)
        for_each_alloc_capable_rdt_resource(r)
                resctrl_arch_reset_all_ctrls(r);
 
-       rmdir_all_sub();
-       rdt_pseudo_lock_release();
-       rdtgroup_default.mode = RDT_MODE_SHAREABLE;
-       schemata_list_destroy();
-       rdtgroup_destroy_root();
+       resctrl_fs_teardown();
+
        if (resctrl_arch_alloc_capable())
                resctrl_arch_disable_alloc();
        if (resctrl_arch_mon_capable())
                resctrl_arch_disable_mon();
-       closid_exit();
        resctrl_mounted = false;
        kernfs_kill_sb(sb);
        mutex_unlock(&rdtgroup_mutex);
Quoted 8 lines

-------%<-------

and call resctrl_fs_teardown() from resctrl_exit().

This leaves a bunch of resctrl_arch_ calls behind, the reason is its the arch code that calls resctrl_exit(), so it probably doesn't need to be told to disable things. For MPAM, the work of resctrl_arch_reset_all_ctrls() will already have been done - and all these calls will fail because the MPAM driver is blocking further access to the hardware.

As I understand it the overflow and limbo handlers will keep running after a resctrl fs teardown done on error interrupt. As you mention in changelog this work is done because "The MPAM driver needs a way to tell resctrl that no further configuration should be attempted.". I believe these handlers may thus still try to interact (but not technically "configure"?) with the hardware at this point ... is this ok?

rdt_disable_ctx() is an odd one to keep with the resctrl_arch_ calls that remain in rdt_kill_sb(). It may be a candidate for resctrl_fs_teardown() but unfortunately rdt_disable_ctx() blurs fs/arch parts. What I am focusing on is the set_mba_sc(false) within it. Seems like this may mean that the software controller will keep running unnecessarily.

Thank you

Reinette

author reviewer
#80 lore raw

Re: [PATCH v7 33/49] x86/resctrl: resctrl_exit() teardown resctrl but leave the mount point

Hi Reinette,

Quoted 70 lines · Reinette Chatre
Quoted 69 lines · James Morse
Quoted 41 lines · Reinette Chatre
Quoted 14 lines · James Morse

resctrl_exit() was intended for use when the 'resctrl' module was unloaded. resctrl can't be built as a module, and the kernfs helpers are not exported so this is unlikely to change. MPAM has an error interrupt which indicates the MPAM driver has gone haywire. Should this occur tasks could run with the wrong control values, leading to bad performance for important tasks. The MPAM driver needs a way to tell resctrl that no further configuration should be attempted.

Using resctrl_exit() for this leaves the system in a funny state as resctrl is still mounted, but cannot be un-mounted because the sysfs directory that is typically used has been removed. Dave Martin suggests this may cause systemd trouble in the future as not all filesystems can be unmounted.

Quoted 4 lines

Add calls to remove all the files and directories in resctrl, and remove the sysfs_remove_mount_point() call that leaves the system in a funny state. When triggered, this causes all the resctrl files to disappear. resctrl can be unmounted, but not mounted again.

(copying v6 discussion here)

Quoted 18 lines · James Morse
Quoted 17 lines · Reinette Chatre
Quoted 9 lines · James Morse
Quoted 4 lines · Reinette Chatre

It is difficult for me to follow the kernfs reference counting required to make this work. Specifically, the root kn is "destroyed" here but it is required to stick around until unmount when the rest of the files are removed.

This drops resctrl's reference to all of the files, which would make the files disappear. unmount is what calls kernfs_kill_sb(), which gets rid of the root of the filesystem.

My concern is mostly with the kernfs_remove() calls in the rdt_kill_sb()->rmdir_all_sub() flow. For example:

	kernfs_remove(kn_info);
	kernfs_remove(kn_mongrp);
	kernfs_remove(kn_mondata);
Quoted 7 lines
Quoted 5 lines
Quoted 1 line

As I understand the above require the destroyed root to still be around.

Right - because rdt_get_tree() has these global pointers into the hierarchy, but doesn't take a reference. rmdir_all_sub() relies on always being called before rdtgroup_destroy_root().

Is this a known issue then?

Its just a subtle thing that resctrl was relying on, and I didn't spot. Thanks for pointing it out!

Quoted 14 lines

Since I am not able to use your test I created something new after thinking there would be no response to my comment and indeed on unmount:

[  293.707228] BUG: KASAN: slab-use-after-free in kernfs_remove+0x87/0xa0
[  293.714718] Read of size 8 at addr ff11000309d88f30 by task umount/3793
Quoted 7 lines

The point hack would be for rdtgroup_destroy_root() to NULL out those global pointers, (I note they are left dangling) - that would make a subsequent call to rmdir_all_sub() harmless.

A better fix would be to pull out all the filesystem relevant parts from rdt_kill_sb(), make that safe for multiple calls and get resctrl_exit() to call that.
A call to rdt_kill_sb() after resctrl_exit() would just cleanup the super-block. This will leave things in a more predictable state.

Why just the filesystem relevant parts?

Quoted 7 lines
Quoted 4 lines

The stated aim is to prevent any further configuration of the hardware.
We can change that to 'unmount as much as possible'. I didn't think of it like that as I couldn't find an in-kernel way of triggering a umount(). (and the mount may be copied into numerous namespaces)

deactivate_locked_super() looked promising when I started digging but I quickly found myself in unfamiliar territory.

It's not just the super-block, struct vfsmount maps parts of the directory hierarchy to mounts of filesystems, so a umount would have to take a path - but mount namespaces means its a set of paths ... I quickly gave up on this approach!

On-disk filesystems respond with some IO-error for any access if the backend storage goes away. This is what the MPAM driver does - it would be noisy to get resctrl to do the same, but the existing resctrl_exit() does pretty much everything needed...

Quoted 57 lines
Quoted 57 lines
Quoted 4 lines

Although, you also state "resctrl_exit() would just
cleanup the super-block" that sounds like you are thinking about pulling out all reset work. This sounds reasonable to me. It really feels more appropriate to do proper cleanup and not just wipe the root while leaving everything else underneath it.

After this discussion, my new proposal is to do this:
-------%<-------

arch/x86/kernel/cpu/resctrl/rdtgroup.c +18 -5
index 0d74a6d98dba..cee4604a59c0 100644
@@ -3063,6 +3063,21 @@ static void rmdir_all_sub(void)
        kernfs_remove(kn_mondata);
 }
 
+static void resctrl_fs_teardown(void)
+{
+       lockdep_assert_held(&rdtgroup_mutex);
+
+       /* Cleared by rdtgroup_destroy_root() */
+       if (!rdtgroup_default.kn)
+               return;
+
+       rmdir_all_sub();
+       rdt_pseudo_lock_release();
+       rdtgroup_default.mode = RDT_MODE_SHAREABLE;
+       schemata_list_destroy();
+       closid_exit();
+       rdtgroup_destroy_root();
+}
+
 static void rdt_kill_sb(struct super_block *sb)
 {
        struct rdt_resource *r;
@@ -3076,11 +3091,8 @@ static void rdt_kill_sb(struct super_block *sb)
        for_each_alloc_capable_rdt_resource(r)
                resctrl_arch_reset_all_ctrls(r);
 
-       rmdir_all_sub();
-       rdt_pseudo_lock_release();
-       rdtgroup_default.mode = RDT_MODE_SHAREABLE;
-       schemata_list_destroy();
-       rdtgroup_destroy_root();
+       resctrl_fs_teardown();
+
        if (resctrl_arch_alloc_capable())
                resctrl_arch_disable_alloc();
        if (resctrl_arch_mon_capable())
                resctrl_arch_disable_mon();
-       closid_exit();
        resctrl_mounted = false;
        kernfs_kill_sb(sb);
        mutex_unlock(&rdtgroup_mutex);

-------%<-------

and call resctrl_fs_teardown() from resctrl_exit().

Quoted 10 lines
Quoted 4 lines

This leaves a bunch of resctrl_arch_ calls behind, the reason is its the arch code that calls resctrl_exit(), so it probably doesn't need to be told to disable things. For MPAM, the work of resctrl_arch_reset_all_ctrls() will already have been done - and all these calls will fail because the MPAM driver is blocking further access to the hardware.

As I understand it the overflow and limbo handlers will keep running after a resctrl fs teardown done on error interrupt. As you mention in changelog this work is done because "The MPAM driver needs a way to tell resctrl that no further configuration should be attempted.". I believe these handlers may thus still try to interact (but not technically "configure"?) with the hardware at this point ... is this ok?

For MPAM this is fine because all the resctrl_arch_ calls are going to start returning errors. I have a followup patch that makes the pr_warn_ratelimited() for the monitor context a pr_warn_once() as once this thing goes off, the log starts to get filled up, albeit with ratelimited messages. (that patch isn't part of this series because its not possible to hit on x86).

I don't think its enough to just leave resctrl in place and report an error on any configuration change - user-space may have created control groups at boot, then just move tasks between them. Nothing about moving a task between existing control groups interacts with the arch code, so it isn't possible for user-space to know that its requests are being ignored. Hence the attempt to teardown the filesystem as far as possible.

(I'll add some of this thinking to the commit message for posterity)

Quoted 4 lines

rdt_disable_ctx() is an odd one to keep with the resctrl_arch_ calls that remain in rdt_kill_sb(). It may be a candidate for resctrl_fs_teardown() but unfortunately rdt_disable_ctx() blurs fs/arch parts. What I am focusing on is the set_mba_sc(false) within it. Seems like this may mean that the software controller will keep running unnecessarily.

Yes, the limbo and mbm_overflow 'threads' keep ticking after this call, but all their accesses return an error.

The other trick the MPAM driver has here is to tell resctrl that all the CPUs (and therefore domains) are offline. This is a prerequisite for free-ing any of the arch allocated structures.

(if resctrl ever became a module, the lifetime of the cpuhp calls is something that would
have to belong to resctrl - but for now its up to the arch code to call)

Thanks,

James

author reviewer
#81 lore raw

PATCH v7 34/49 x86/resctrl: Drop __init/__exit on assorted symbols

In reply to James Morse

Because ARM's MPAM controls are probed using MMIO, resctrl can't be initialised until enough CPUs are online to have determined the system-wide supported num_closid. Arm64 also supports 'late onlined secondaries', where only a subset of CPUs are online during boot.

These two combine to mean the MPAM driver may not be able to initialise resctrl until user-space has brought 'enough' CPUs online.

To allow MPAM to initialise resctrl after __init text has been free'd, remove all the __init markings from resctrl.

The existing __exit markings cause these functions to be removed by the linker as it has never been possible to build resctrl as a module. MPAM has an error interrupt which causes the driver to reset and disable itself. Remove the __exit markings to allow the MPAM driver to tear down resctrl when an error occurs.

---
Changes since v4:
* Earlier __init marker removal migrated here.

---
 arch/x86/kernel/cpu/resctrl/core.c     | 6 +++---
 arch/x86/kernel/cpu/resctrl/internal.h | 4 ++--
 arch/x86/kernel/cpu/resctrl/monitor.c  | 2 +-
 arch/x86/kernel/cpu/resctrl/rdtgroup.c | 6 +++---
 include/linux/resctrl.h                | 6 +++---
 5 files changed, 12 insertions(+), 12 deletions(-)
arch/x86/kernel/cpu/resctrl/core.c +3 -3
index b7ce578bffa5..579a6caf76e5 100644
@@ -739,7 +739,7 @@ struct rdt_options {
 	bool	force_off, force_on;
 };
 
-static struct rdt_options rdt_options[]  __initdata = {
+static struct rdt_options rdt_options[]  __ro_after_init = {
 	RDT_OPT(RDT_FLAG_CMT,	    "cmt",	X86_FEATURE_CQM_OCCUP_LLC),
 	RDT_OPT(RDT_FLAG_MBM_TOTAL, "mbmtotal", X86_FEATURE_CQM_MBM_TOTAL),
 	RDT_OPT(RDT_FLAG_MBM_LOCAL, "mbmlocal", X86_FEATURE_CQM_MBM_LOCAL),
@@ -779,7 +779,7 @@ static int __init set_rdt_options(char *str)
 }
 __setup("rdt", set_rdt_options);
 
-bool __init rdt_cpu_has(int flag)
+bool rdt_cpu_has(int flag)
 {
 	bool ret = boot_cpu_has(flag);
 	struct rdt_options *o;
@@ -799,7 +799,7 @@ bool __init rdt_cpu_has(int flag)
 	return ret;
 }
 
-__init bool resctrl_arch_is_evt_configurable(enum resctrl_event_id evt)
+bool resctrl_arch_is_evt_configurable(enum resctrl_event_id evt)
 {
 	if (!rdt_cpu_has(X86_FEATURE_BMEC))
 		return false;
arch/x86/kernel/cpu/resctrl/internal.h +2 -2
index c44c5b496355..32ed9aeffb90 100644
@@ -481,13 +481,13 @@ int alloc_rmid(u32 closid);
 void free_rmid(u32 closid, u32 rmid);
 int rdt_get_mon_l3_config(struct rdt_resource *r);
 void resctrl_mon_resource_exit(void);
-bool __init rdt_cpu_has(int flag);
+bool 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_mon_domain *d, struct rdtgroup *rdtgrp,
 		    cpumask_t *cpumask, int evtid, int first);
-int __init resctrl_mon_resource_init(void);
+int resctrl_mon_resource_init(void);
 void mbm_setup_overflow_handler(struct rdt_mon_domain *dom,
 				unsigned long delay_ms,
 				int exclude_cpu);
arch/x86/kernel/cpu/resctrl/monitor.c +1 -1
index a93ed7d2a160..73e3fe4f4c87 100644
@@ -1204,7 +1204,7 @@ static __init int snc_get_config(void)
  *
  * Returns 0 for success, or -ENOMEM.
  */
-int __init resctrl_mon_resource_init(void)
+int resctrl_mon_resource_init(void)
 {
 	struct rdt_resource *r = resctrl_arch_get_resource(RDT_RESOURCE_L3);
 	int ret;
arch/x86/kernel/cpu/resctrl/rdtgroup.c +3 -3
index 0d74a6d98dba..3145079eb043 100644
@@ -4102,7 +4102,7 @@ static void rdtgroup_destroy_root(void)
 	}
 }
 
-static void __init rdtgroup_setup_default(void)
+static void rdtgroup_setup_default(void)
 {
 	mutex_lock(&rdtgroup_mutex);
 
@@ -4334,7 +4334,7 @@ void resctrl_offline_cpu(unsigned int cpu)
  *
  * Return: 0 on success or -errno
  */
-int __init resctrl_init(void)
+int resctrl_init(void)
 {
 	int ret = 0;
 
@@ -4398,7 +4398,7 @@ int __init resctrl_init(void)
  * Resctrl files and structures are removed from kernfs to prevent further
  * configuration.
  */
-void __exit resctrl_exit(void)
+void resctrl_exit(void)
 {
 	mutex_lock(&rdtgroup_mutex);
 	rdtgroup_destroy_root();
include/linux/resctrl.h +3 -3
index 880351ca3dfc..b8f8240050b4 100644
@@ -358,7 +358,7 @@ u32 resctrl_arch_get_num_closid(struct rdt_resource *r);
 u32 resctrl_arch_system_num_rmid_idx(void);
 int resctrl_arch_update_domains(struct rdt_resource *r, u32 closid);
 
-__init bool resctrl_arch_is_evt_configurable(enum resctrl_event_id evt);
+bool resctrl_arch_is_evt_configurable(enum resctrl_event_id evt);
 
 /**
  * resctrl_arch_mon_event_config_write() - Write the config for an event.
@@ -514,7 +514,7 @@ void resctrl_arch_reset_all_ctrls(struct rdt_resource *r);
 extern unsigned int resctrl_rmid_realloc_threshold;
 extern unsigned int resctrl_rmid_realloc_limit;
 
-int __init resctrl_init(void);
-void __exit resctrl_exit(void);
+int resctrl_init(void);
+void resctrl_exit(void);
 
 #endif /* _RESCTRL_H */
Signature · 1 line
2.39.5
Reviewed-by on v7 34/49
#82 lore raw

Re: [PATCH v7 34/49] x86/resctrl: Drop __init/__exit on assorted symbols

Quoted 17 lines · James Morse

Because ARM's MPAM controls are probed using MMIO, resctrl can't be initialised until enough CPUs are online to have determined the system-wide supported num_closid. Arm64 also supports 'late onlined secondaries', where only a subset of CPUs are online during boot.

These two combine to mean the MPAM driver may not be able to initialise resctrl until user-space has brought 'enough' CPUs online.

To allow MPAM to initialise resctrl after __init text has been free'd, remove all the __init markings from resctrl.

The existing __exit markings cause these functions to be removed by the linker as it has never been possible to build resctrl as a module. MPAM has an error interrupt which causes the driver to reset and disable itself. Remove the __exit markings to allow the MPAM driver to tear down resctrl when an error occurs.

Quoted 6 lines

Thanks.

-Fenghua

author reviewer
#83 lore raw

PATCH v7 35/49 x86/resctrl: Move is_mba_sc() out of core.c

In reply to James Morse

is_mba_sc() is defined in core.c, but has no callers there. It does not access any architecture private structures.

Move this to rdtgroup.c where the majority of callers are. This makes the move of the filesystem code to /fs/ cleaner.

---
Changes since v2:
* This patch is new.

---
 arch/x86/kernel/cpu/resctrl/core.c     | 15 ---------------
 arch/x86/kernel/cpu/resctrl/rdtgroup.c | 15 +++++++++++++++
 2 files changed, 15 insertions(+), 15 deletions(-)
arch/x86/kernel/cpu/resctrl/core.c +0 -15
index 579a6caf76e5..93b51d522bc7 100644
@@ -166,21 +166,6 @@ static inline void cache_alloc_hsw_probe(void)
 	rdt_alloc_capable = true;
 }
 
-bool is_mba_sc(struct rdt_resource *r)
-{
-	if (!r)
-		r = resctrl_arch_get_resource(RDT_RESOURCE_MBA);
-
-	/*
-	 * The software controller support is only applicable to MBA resource.
-	 * Make sure to check for resource type.
-	 */
-	if (r->rid != RDT_RESOURCE_MBA)
-		return false;
-
-	return r->membw.mba_sc;
-}
-
 /*
  * rdt_get_mb_table() - get a mapping of bandwidth(b/w) percentage values
  * exposed to user interface and the h/w understandable delay values.
arch/x86/kernel/cpu/resctrl/rdtgroup.c +15 -0
index 3145079eb043..aecd3fa734cd 100644
@@ -1521,6 +1521,21 @@ unsigned int rdtgroup_cbm_to_size(struct rdt_resource *r,
 	return size;
 }
 
+bool is_mba_sc(struct rdt_resource *r)
+{
+	if (!r)
+		r = resctrl_arch_get_resource(RDT_RESOURCE_MBA);
+
+	/*
+	 * The software controller support is only applicable to MBA resource.
+	 * Make sure to check for resource type.
+	 */
+	if (r->rid != RDT_RESOURCE_MBA)
+		return false;
+
+	return r->membw.mba_sc;
+}
+
 /*
  * rdtgroup_size_show - Display size in bytes of allocated regions
  *
Signature · 1 line
2.39.5
Reviewed-by on v7 35/49
#84 lore raw

Re: [PATCH v7 35/49] x86/resctrl: Move is_mba_sc() out of core.c

Quoted 6 lines · James Morse

is_mba_sc() is defined in core.c, but has no callers there. It does not access any architecture private structures.

Move this to rdtgroup.c where the majority of callers are. This makes the move of the filesystem code to /fs/ cleaner.

Quoted 6 lines

Thanks.

-Fenghua

author reviewer
#85 lore raw

PATCH v7 36/49 x86/resctrl: Add end-marker to the resctrl_event_id enum

In reply to James Morse

The resctrl_event_id enum gives names to the counter event numbers on x86. These are used directly by resctrl.

To allow the MPAM driver to keep an array of these the size of the enum needs to be known.

Add a 'num_events' define which can be used to size an array. This isn't a member of the enum to avoid updating switch statements that would otherwise be missing a case.

---
 include/linux/resctrl_types.h | 2 ++
 1 file changed, 2 insertions(+)
include/linux/resctrl_types.h +2 -0
index f26450b3326b..654323066174 100644
@@ -51,4 +51,6 @@ enum resctrl_event_id {
 	QOS_L3_MBM_LOCAL_EVENT_ID	= 0x03,
 };
 
+#define QOS_NUM_EVENTS		(QOS_L3_MBM_LOCAL_EVENT_ID + 1)
+
 #endif /* __LINUX_RESCTRL_TYPES_H */
Signature · 1 line
2.39.5
Reviewed-by on v7 36/49
#86 lore raw

Re: [PATCH v7 36/49] x86/resctrl: Add end-marker to the resctrl_event_id enum

Quoted 10 lines · James Morse

The resctrl_event_id enum gives names to the counter event numbers on x86. These are used directly by resctrl.

To allow the MPAM driver to keep an array of these the size of the enum needs to be known.

Add a 'num_events' define which can be used to size an array. This isn't a member of the enum to avoid updating switch statements that would otherwise be missing a case.

Quoted 6 lines

Thanks.

-Fenghua

author reviewer
#87 lore raw

PATCH v7 37/49 x86/resctrl: Expand the width of dom_id by replacing mon_data_bits

In reply to James Morse

MPAM platforms retrieve the cache-id property from the ACPI PPTT table. The cache-id field is 32 bits wide. Under resctrl, the cache-id becomes the domain-id, and is packed into the mon_data_bits union bitfield. The width of cache-id in this field is 14 bits.

Expanding the union would break 32bit x86 platforms as this union is stored as the kernfs kn->priv pointer. This saved allocating memory for the priv data storage.

The firmware on MPAM platforms have used the PPTT cache-id field to expose the interconnect's id for the cache, which is sparse and uses more than 14 bits. Use of this id is to enable PCIe direct cache injection hints. Using this feature with VFIO means the value provided by the ACPI table should be exposed to user-space.

To support cache-id values greater than 14 bits, convert the mon_data_bits union to a structure. This is allocated for the default control group when the kernfs event files are created, and free'd when the monitor directory is rmdir'd when the domain goes offline. All other control and monitor groups lookup the struct mon_data allocated for the default control group, and use this.
This simplifies the lifecycle of this structure as the default control group cannot be rmdir()d by user-space, so only needs to consider domain-offline, which removes all the event files corresponding to a domain while holding rdtgroup_mutex - which prevents concurrent readers. mkdir_mondata_subdir_allrdtgrp() must special case the default control group to ensure it is created first.

---
Previously the MPAM tree repainted the cache-id to compact them, argue-ing there was no other user. With VFIO use of this PCIe feature, this is no longer an option.

Changes since v6:
* Added the get/put helpers.
* Special case the creation of the mondata files for the default control group.
* Removed wording about files living longer than expected, the corresponding error handling is wrapped in WARN_ON_ONCE() as this indicates a bug.

---
 arch/x86/kernel/cpu/resctrl/ctrlmondata.c |  19 +--
 arch/x86/kernel/cpu/resctrl/internal.h    |  37 +++---
 arch/x86/kernel/cpu/resctrl/monitor.c     |   3 +
 arch/x86/kernel/cpu/resctrl/rdtgroup.c    | 142 ++++++++++++++++++++--
 4 files changed, 162 insertions(+), 39 deletions(-)
arch/x86/kernel/cpu/resctrl/ctrlmondata.c +12 -7
index 0a0ac5f6112e..159972c3fe73 100644
@@ -667,7 +667,7 @@ int rdtgroup_mondata_show(struct seq_file *m, void *arg)
 	u32 resid, evtid, domid;
 	struct rdtgroup *rdtgrp;
 	struct rdt_resource *r;
-	union mon_data_bits md;
+	struct mon_data *md;
 	int ret = 0;
 
 	rdtgrp = rdtgroup_kn_lock_live(of->kn);
@@ -676,17 +676,22 @@ int rdtgroup_mondata_show(struct seq_file *m, void *arg)
 		goto out;
 	}
 
-	md.priv = of->kn->priv;
-	resid = md.u.rid;
-	domid = md.u.domid;
-	evtid = md.u.evtid;
+	md = of->kn->priv;
+	if (WARN_ON_ONCE(!md)) {
+		ret = -EIO;
+		goto out;
+	}
+
+	resid = md->rid;
+	domid = md->domid;
+	evtid = md->evtid;
 	r = resctrl_arch_get_resource(resid);
 
-	if (md.u.sum) {
+	if (md->sum) {
 		/*
 		 * This file requires summing across all domains that share
 		 * the L3 cache id that was provided in the "domid" field of the
-		 * mon_data_bits union. Search all domains in the resource for
+		 * struct mon_data. Search all domains in the resource for
 		 * one that matches this cache id.
 		 */
 		list_for_each_entry(d, &r->mon_domains, hdr.list) {
arch/x86/kernel/cpu/resctrl/internal.h +17 -20
index 32ed9aeffb90..16c1a391d012 100644
@@ -103,27 +103,24 @@ struct mon_evt {
 };
 
 /**
- * union mon_data_bits - Monitoring details for each event file.
- * @priv:              Used to store monitoring event data in @u
- *                     as kernfs private data.
- * @u.rid:             Resource id associated with the event file.
- * @u.evtid:           Event id associated with the event file.
- * @u.sum:             Set when event must be summed across multiple
- *                     domains.
- * @u.domid:           When @u.sum is zero this is the domain to which
- *                     the event file belongs. When @sum is one this
- *                     is the id of the L3 cache that all domains to be
- *                     summed share.
- * @u:                 Name of the bit fields struct.
+ * struct mon_data - Monitoring details for each event file.
+ * @rid:             Resource id associated with the event file.
+ * @evtid:           Event id associated with the event file.
+ * @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 id of the L3 cache that all domains to be
+ *                   summed share.
+ *
+ * Stored in the kernfs kn->priv field, readers and writers must hold
+ * rdtgroup_mutex.
  */
-union mon_data_bits {
-	void *priv;
-	struct {
-		unsigned int rid		: 10;
-		enum resctrl_event_id evtid	: 7;
-		unsigned int sum		: 1;
-		unsigned int domid		: 14;
-	} u;
+struct mon_data {
+	unsigned int rid;
+	enum resctrl_event_id evtid;
+	unsigned int sum;
+	unsigned int domid;
 };
 
 /**
arch/x86/kernel/cpu/resctrl/monitor.c +3 -0
index 73e3fe4f4c87..5bf67b429dd3 100644
@@ -1096,6 +1096,9 @@ static struct mon_evt mbm_local_event = {
  * Note that MBM events are also part of RDT_RESOURCE_L3 resource
  * because as per the SDM the total and local memory bandwidth
  * are enumerated as part of L3 monitoring.
+ *
+ * mon_put_default_kn_priv_all() also assumes monitor events are only supported
+ * on the L3 resource.
  */
 static void l3_mon_evt_init(struct rdt_resource *r)
 {
arch/x86/kernel/cpu/resctrl/rdtgroup.c +130 -12

193 lines not on this page · raw message

Signature · 1 line
2.39.5
maintainer
#88 lore raw

Re: [PATCH v7 37/49] x86/resctrl: Expand the width of dom_id by replacing mon_data_bits

Hi James,

Quoted 15 lines · James Morse

MPAM platforms retrieve the cache-id property from the ACPI PPTT table. The cache-id field is 32 bits wide. Under resctrl, the cache-id becomes the domain-id, and is packed into the mon_data_bits union bitfield. The width of cache-id in this field is 14 bits.

Expanding the union would break 32bit x86 platforms as this union is stored as the kernfs kn->priv pointer. This saved allocating memory for the priv data storage.

The firmware on MPAM platforms have used the PPTT cache-id field to expose the interconnect's id for the cache, which is sparse and uses more than 14 bits. Use of this id is to enable PCIe direct cache injection hints. Using this feature with VFIO means the value provided by the ACPI table should be exposed to user-space.

Quoted 12 lines

To support cache-id values greater than 14 bits, convert the mon_data_bits union to a structure. This is allocated for the default control group when the kernfs event files are created, and free'd when the monitor directory is rmdir'd when the domain goes offline. All other control and monitor groups lookup the struct mon_data allocated for the default control group, and use this.
This simplifies the lifecycle of this structure as the default control group cannot be rmdir()d by user-space, so only needs to consider domain-offline, which removes all the event files corresponding to a domain while holding rdtgroup_mutex - which prevents concurrent readers. mkdir_mondata_subdir_allrdtgrp() must special case the default control group to ensure it is created first.

This is novel.

Quoted 14 lines

---
Previously the MPAM tree repainted the cache-id to compact them, argue-ing there was no other user. With VFIO use of this PCIe feature, this is no longer an option.

Changes since v6:
* Added the get/put helpers.
* Special case the creation of the mondata files for the default control group.
* Removed wording about files living longer than expected, the corresponding error handling is wrapped in WARN_ON_ONCE() as this indicates a bug.
---

...

arch/x86/kernel/cpu/resctrl/rdtgroup.c static struct file_system_type rdt_fs_type = { · line 3119

Quoted 11 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +3 -0
index aecd3fa734cd..443635d195f0 100644
@@ -3114,6 +3114,110 @@ static struct file_system_type rdt_fs_type = {
 	.kill_sb		= rdt_kill_sb,
 };
 
+/**
+ * mon_get_default_kn_priv() - Get the mon_data priv data for this event from
+ *                             the default control group.

Since this involves monitoring this would technically be the default "monitoring" group (throughout).

arch/x86/kernel/cpu/resctrl/rdtgroup.c static struct file_system_type rdt_fs_type = { · line 3121

Quoted 2 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +2 -0
+ * Called when monitor event files are created for a domain.
+ * When called with the default control group, the structure will be allocated.

A bit difficult to parse. Assuming there is a re-spin, how about something like:
"Allocate the structure when @rdtgrp is the default group."

arch/x86/kernel/cpu/resctrl/rdtgroup.c static struct file_system_type rdt_fs_type = { · line 3129

Quoted 8 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +8 -0
+ * This happens at mount time, before other control or monitor groups are
+ * created.
+ * This simplifies the lifetime management for rmdir() versus domain-offline
+ * as the default control group lives forever, and only one group needs to be
+ * special cased.
+ *
+ * @r:      The resource for the event type being created.
+ * @d:	    The domain for the event type being created.

Stray tab makes for inconsisent spacing.

arch/x86/kernel/cpu/resctrl/rdtgroup.c static struct file_system_type rdt_fs_type = { · line 3133

Quoted 4 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +4 -0
+ * @mevt:   The event type being created.
+ * @rdtgrp: The rdtgroup for which the monitor file is being created,
+ *          used to determine if this is the default control group.
+ * @do_sum: Whether the SNC sub-numa node monitors are being created.

do_sum can be true or false when it comes to the SNC files (more below).

Quoted 7 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +7 -0
+ */
+static struct mon_data *mon_get_default_kn_priv(struct rdt_resource *r,
+						struct rdt_mon_domain *d,
+						struct mon_evt *mevt,
+						struct rdtgroup *rdtgrp,
+						bool do_sum)
+{

arch/x86/kernel/cpu/resctrl/rdtgroup.c static struct file_system_type rdt_fs_type = { · line 3150

Quoted 10 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +10 -0
+	struct kernfs_node *kn_dom, *kn_evt;
+	struct mon_data *priv;
+	bool snc_mode;
+	char name[32];
+
+	lockdep_assert_held(&rdtgroup_mutex);
+
+	snc_mode = r->mon_scope == RESCTRL_L3_NODE;
+	if (!do_sum)
+		sprintf(name, "mon_%s_%02d", r->name, snc_mode ? d->ci->id : d->hdr.id);

When in SNC mode the "mon_<resource name>_ files always use d->ci->id as the domain id.

arch/x86/kernel/cpu/resctrl/rdtgroup.c static struct file_system_type rdt_fs_type = { · line 3153

Quoted 3 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +3 -0
+	else
+		sprintf(name, "mon_sub_%s_%02d", r->name, d->hdr.id);
+

The mon_sub_<resource name>_ directories are always created when in SNC mode, they do not exist on non SNC enabled systems. The mon_<resource name>_ directories exists on both SNC enabled and non-SNC/SNC disabled systems. The mon_<resource name>_ directories on SNC enabled system will have "do_sum" set. I think what you may be trying to do here is something like:

	if (!snc_mode) { /* do_sum is not relevant */
		sprintf(name, "mon_%s_%02d", r->name, d->hdr.id);
	} else if (snc_mode && do_sum) {
		sprintf(name, "mon_%s_%02d", r->name, d->ci->id);
	} else { /* snc_mode && !do_sum */
		sprintf(name, "mon_sub_%s_%02d", r->name, d->hdr.id);
	}

arch/x86/kernel/cpu/resctrl/rdtgroup.c static struct file_system_type rdt_fs_type = { · line 3156

Quoted 3 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +3 -0
+	kn_dom = kernfs_find_and_get(kn_mondata, name);
+	if (!kn_dom)
+		return NULL;

It seems like this either assumes the directories are on the same level or assumes kernfs_find_and_get() does a recursive find. As I understand kernfs_find_and_get() does not do a recursive find. On SNC enabled systems the mon_sub_<resource name>_ directories are subdirectories of the mon_<resource name>_ directories. Example of how hierarchy may look is at:
https://lore.kernel.org/all/[email protected]/

With all of the above I do not think this will work on an SNC enabled system ... to confirm this I tried it out and it is not possible to mount resctrl on an SNC enabled system and the WARN_ON_ONCE() this patch adds to mon_add_all_files() is hit.

arch/x86/kernel/cpu/resctrl/rdtgroup.c static struct file_system_type rdt_fs_type = { · line 3158

Quoted 2 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +2 -0
+
+	kn_evt = kernfs_find_and_get(kn_dom, mevt->name);

Note the "...and_get..." in kernfs_find_and_get() that gets a reference to the kn before returning it. I expect that this work will have symmetry when it comes to get/put of references but I see four new calls to kernfs_find_and_get() but no new matching kernfs_put() to release the new references. It looks like kernfs_find_and_get() is just used to figure out what the kn is so the references need not be kept around for long.

Quoted 24 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +24 -0
+
+	/* Is this the creation of the default groups monitor files? */
+	if (!kn_evt && rdtgrp == &rdtgroup_default) {
+		priv = kzalloc(sizeof(*priv), GFP_KERNEL);
+		if (!priv)
+			return NULL;
+		priv->rid = r->rid;
+		priv->domid = do_sum ? d->ci->id : d->hdr.id;
+		priv->sum = do_sum;
+		priv->evtid = mevt->evtid;
+		return priv;
+	}
+
+	if (!kn_evt)
+		return NULL;
+
+	return kn_evt->priv;
+}
+
+/**
+ * mon_put_default_kn_priv_all() - Potentially free the mon_data priv data for
+ *                                 all events from the default control group.
+ * Put the mon_data priv data for all events for a particular domain.
+ * When called with the default control group, the priv structure previously

arch/x86/kernel/cpu/resctrl/rdtgroup.c static struct file_system_type rdt_fs_type = { · line 3192

Quoted 10 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +10 -0
+ * allocated will be kfree()d. This should only be done as part of taking a
+ * domain offline.
+ * Only a domain offline will 'rmdir' monitor files in the default control
+ * group. After domain offline releases rdtgrp_mutex, all references will
+ * have been removed.
+ *
+ * @rdtgrp:  The rdtgroup for which the monitor files are being removed,
+ *           used to determine if this is the default control group.
+ * @name:    The name of the domain or SNC sub-numa domain which is being
+ *           taken offline.

This is a bit confusing since domains do not have names. How about (please feel free to improve):
"Name of directory containing monitoring files that is in process of being removed."

Quoted 5 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +5 -0
+ */
+static void mon_put_default_kn_priv_all(struct rdtgroup *rdtgrp, char *name)
+{
+	struct rdt_resource *r = resctrl_arch_get_resource(RDT_RESOURCE_L3);
+	struct kernfs_node *kn_dom, *kn_evt;

arch/x86/kernel/cpu/resctrl/rdtgroup.c static struct file_system_type rdt_fs_type = { · line 3207

Quoted 10 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +10 -0
+	struct mon_evt *mevt;
+
+	lockdep_assert_held(&rdtgroup_mutex);
+
+	if (rdtgrp != &rdtgroup_default)
+		return;
+
+	kn_dom = kernfs_find_and_get(kn_mondata, name);
+	if (!kn_dom)
+		return;

I expect this will always fail when @name is a mon_sub_* directory.

Quoted 70 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +25 -6
+
+	list_for_each_entry(mevt, &r->evt_list, list) {
+		kn_evt = kernfs_find_and_get(kn_dom, mevt->name);
+		if (!kn_evt)
+			continue;
+		if (!kn_evt->priv)
+			continue;
+
+		kfree(kn_evt->priv);
+		kn_evt->priv = NULL;
+	}
+}
+
 static int mon_addfile(struct kernfs_node *parent_kn, const char *name,
 		       void *priv)
 {
@@ -3135,19 +3239,25 @@ static int mon_addfile(struct kernfs_node *parent_kn, const char *name,
 	return ret;
 }
 
-static void mon_rmdir_one_subdir(struct kernfs_node *pkn, char *name, char *subname)
+static void mon_rmdir_one_subdir(struct rdtgroup *rdtgrp, char *name, char *subname)
 {
+	struct kernfs_node *pkn = rdtgrp->mon.mon_data_kn;
 	struct kernfs_node *kn;
 
 	kn = kernfs_find_and_get(pkn, name);
 	if (!kn)
 		return;
+
+	mon_put_default_kn_priv_all(rdtgrp, name);
+
 	kernfs_put(kn);
 
-	if (kn->dir.subdirs <= 1)
+	if (kn->dir.subdirs <= 1) {
 		kernfs_remove(kn);
-	else
+	} else {
+		mon_put_default_kn_priv_all(rdtgrp, subname);
 		kernfs_remove_by_name(kn, subname);
+	}
 }
 
 /*
@@ -3170,10 +3280,10 @@ static void rmdir_mondata_subdir_allrdtgrp(struct rdt_resource *r,
 		sprintf(subname, "mon_sub_%s_%02d", r->name, d->hdr.id);
 
 	list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
-		mon_rmdir_one_subdir(prgrp->mon.mon_data_kn, name, subname);
+		mon_rmdir_one_subdir(prgrp, name, subname);
 
 		list_for_each_entry(crgrp, &prgrp->mon.crdtgrp_list, mon.crdtgrp_list)
-			mon_rmdir_one_subdir(crgrp->mon.mon_data_kn, name, subname);
+			mon_rmdir_one_subdir(crgrp, name, subname);
 	}
 }
 
@@ -3182,19 +3292,19 @@ static int mon_add_all_files(struct kernfs_node *kn, struct rdt_mon_domain *d,
 			     bool do_sum)
 {
 	struct rmid_read rr = {0};
-	union mon_data_bits priv;
+	struct mon_data *priv;
 	struct mon_evt *mevt;
 	int ret;
 
 	if (WARN_ON(list_empty(&r->evt_list)))
 		return -EPERM;
 

arch/x86/kernel/cpu/resctrl/rdtgroup.c static int mon_add_all_files(struct kernfs_node *kn, struct rdt_mon_domain *d, · line 3306

Quoted 10 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +4 -5
-	priv.u.rid = r->rid;
-	priv.u.domid = do_sum ? d->ci->id : 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);
+		priv = mon_get_default_kn_priv(r, d, mevt, prgrp, do_sum);
+		if (WARN_ON_ONCE(!priv))
+			return -EINVAL;
+

This is the warning I hit on the SNC system.

Quoted 13 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +6 -0
+		ret = mon_addfile(kn, mevt->name, priv);
 		if (ret)
 			return ret;
 
@@ -3274,9 +3384,17 @@ static void mkdir_mondata_subdir_allrdtgrp(struct rdt_resource *r,
 	struct rdtgroup *prgrp, *crgrp;
 	struct list_head *head;
 
+	/*
+	 * During domain-online create the default control group first
+	 * so that mon_get_default_kn_priv() can find the allocated structure
+	 * on subsequent calls.
+	 */

arch/x86/kernel/cpu/resctrl/rdtgroup.c static void mkdir_mondata_subdir_allrdtgrp(struct rdt_resource *r, · line 3400

Quoted 10 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +4 -1
+	mkdir_mondata_subdir(kn_mondata, d, r, &rdtgroup_default);
+
 	list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
 		parent_kn = prgrp->mon.mon_data_kn;
-		mkdir_mondata_subdir(parent_kn, d, r, prgrp);
+		if (prgrp != &rdtgroup_default)
+			mkdir_mondata_subdir(parent_kn, d, r, prgrp);
 
 		head = &prgrp->mon.crdtgrp_list;
 		list_for_each_entry(crgrp, head, mon.crdtgrp_list) {

Reinette

author reviewer
#89 lore raw

Re: [PATCH v7 37/49] x86/resctrl: Expand the width of dom_id by replacing mon_data_bits

Hi Reinette,

Quoted 37 lines · Reinette Chatre
Quoted 36 lines · James Morse

MPAM platforms retrieve the cache-id property from the ACPI PPTT table. The cache-id field is 32 bits wide. Under resctrl, the cache-id becomes the domain-id, and is packed into the mon_data_bits union bitfield. The width of cache-id in this field is 14 bits.

Expanding the union would break 32bit x86 platforms as this union is stored as the kernfs kn->priv pointer. This saved allocating memory for the priv data storage.

The firmware on MPAM platforms have used the PPTT cache-id field to expose the interconnect's id for the cache, which is sparse and uses more than 14 bits. Use of this id is to enable PCIe direct cache injection hints. Using this feature with VFIO means the value provided by the ACPI table should be exposed to user-space.

To support cache-id values greater than 14 bits, convert the mon_data_bits union to a structure. This is allocated for the default control group when the kernfs event files are created, and free'd when the monitor directory is rmdir'd when the domain goes offline. All other control and monitor groups lookup the struct mon_data allocated for the default control group, and use this.
This simplifies the lifecycle of this structure as the default control group cannot be rmdir()d by user-space, so only needs to consider domain-offline, which removes all the event files corresponding to a domain while holding rdtgroup_mutex - which prevents concurrent readers. mkdir_mondata_subdir_allrdtgrp() must special case the default control group to ensure it is created first.

arch/x86/kernel/cpu/resctrl/rdtgroup.c +0 -0
index aecd3fa734cd..443635d195f0 100644
@@ -3114,6 +3114,110 @@ static struct file_system_type rdt_fs_type = {
 	.kill_sb		= rdt_kill_sb,
 };
 
Quoted 6 lines

arch/x86/kernel/cpu/resctrl/rdtgroup.c static struct file_system_type rdt_fs_type = { · line 3119

Quoted 3 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +3 -0
+/**
+ * mon_get_default_kn_priv() - Get the mon_data priv data for this event from
+ *                             the default control group.

Since this involves monitoring this would technically be the default "monitoring" group (throughout).

Makes sense - fixed.
This has all leaked out of the observation 'you can't rmdir /'.

Quoted 5 lines

arch/x86/kernel/cpu/resctrl/rdtgroup.c static struct file_system_type rdt_fs_type = { · line 3121

Quoted 2 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +2 -0
+ * Called when monitor event files are created for a domain.
+ * When called with the default control group, the structure will be allocated.

A bit difficult to parse. Assuming there is a re-spin, how about something like:
"Allocate the structure when @rdtgrp is the default group."

Done,

Quoted 10 lines

arch/x86/kernel/cpu/resctrl/rdtgroup.c static struct file_system_type rdt_fs_type = { · line 3129

Quoted 8 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +8 -0
+ * This happens at mount time, before other control or monitor groups are
+ * created.
+ * This simplifies the lifetime management for rmdir() versus domain-offline
+ * as the default control group lives forever, and only one group needs to be
+ * special cased.
+ *
+ * @r:      The resource for the event type being created.
+ * @d:	    The domain for the event type being created.

Stray tab makes for inconsisent spacing.

Fixed,

Quoted 6 lines

arch/x86/kernel/cpu/resctrl/rdtgroup.c static struct file_system_type rdt_fs_type = { · line 3133

Quoted 4 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +4 -0
+ * @mevt:   The event type being created.
+ * @rdtgrp: The rdtgroup for which the monitor file is being created,
+ *          used to determine if this is the default control group.
+ * @do_sum: Whether the SNC sub-numa node monitors are being created.

do_sum can be true or false when it comes to the SNC files (more below).

I mis-understood what the hierarchy looks like on SNC systems.

Quoted 11 lines
Quoted 11 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +11 -0
+ */
+static struct mon_data *mon_get_default_kn_priv(struct rdt_resource *r,
+						struct rdt_mon_domain *d,
+						struct mon_evt *mevt,
+						struct rdtgroup *rdtgrp,
+						bool do_sum)
+{
+	struct kernfs_node *kn_dom, *kn_evt;
+	struct mon_data *priv;
+	bool snc_mode;
+	char name[32];
Quoted 8 lines

arch/x86/kernel/cpu/resctrl/rdtgroup.c static struct file_system_type rdt_fs_type = { · line 3150

Quoted 6 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +6 -0
+
+	lockdep_assert_held(&rdtgroup_mutex);
+
+	snc_mode = r->mon_scope == RESCTRL_L3_NODE;
+	if (!do_sum)
+		sprintf(name, "mon_%s_%02d", r->name, snc_mode ? d->ci->id : d->hdr.id);

When in SNC mode the "mon_<resource name>_ files always use d->ci->id as the domain id.

(that makes sense - I'd conflated it with the next problem)

Quoted 10 lines

arch/x86/kernel/cpu/resctrl/rdtgroup.c static struct file_system_type rdt_fs_type = { · line 3153

Quoted 3 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +3 -0
+	else
+		sprintf(name, "mon_sub_%s_%02d", r->name, d->hdr.id);
+

The mon_sub_<resource name>_ directories are always created when in SNC mode, they do not exist on non SNC enabled systems. The mon_<resource name>_ directories exists on both SNC enabled and non-SNC/SNC disabled systems. The mon_<resource name>_ directories on SNC enabled system will have "do_sum" set. I think what you may be trying to do here is something like:

Quoted 7 lines
	if (!snc_mode) { /* do_sum is not relevant */
		sprintf(name, "mon_%s_%02d", r->name, d->hdr.id);
	} else if (snc_mode && do_sum) {
		sprintf(name, "mon_%s_%02d", r->name, d->ci->id);
	} else { /* snc_mode && !do_sum */
		sprintf(name, "mon_sub_%s_%02d", r->name, d->hdr.id);
	}

Right - that makes sense. Thanks for explaining it.

Quoted 11 lines

arch/x86/kernel/cpu/resctrl/rdtgroup.c static struct file_system_type rdt_fs_type = { · line 3156

Quoted 3 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +3 -0
+	kn_dom = kernfs_find_and_get(kn_mondata, name);
+	if (!kn_dom)
+		return NULL;

It seems like this either assumes the directories are on the same level or assumes kernfs_find_and_get() does a recursive find. As I understand kernfs_find_and_get() does not do a recursive find. On SNC enabled systems the mon_sub_<resource name>_ directories are subdirectories of the mon_<resource name>_ directories.

Example of how hierarchy may look is at:
https://lore.kernel.org/all/[email protected]/

(aha!)

Quoted 4 lines

With all of the above I do not think this will work on an SNC enabled system ... to confirm this I tried it out and it is not possible to mount resctrl on an SNC enabled system and the WARN_ON_ONCE() this patch adds to mon_add_all_files() is hit.

I hadn't realised the mon_sub directories for SNC weren't all directly under mon_data. Searching from mon_data will need the parent name too. What I've come up with is:
-------%<-------

	snc_mode = r->mon_scope == RESCTRL_L3_NODE;
	if (!snc_mode) {
		sprintf(name, "mon_%s_%02d", r->name, d->hdr.id);
		kn_target_dir = kernfs_find_and_get(kn_mondata, name);
	} else {
		sprintf(name, "mon_%s_%02d", r->name, d->ci->id);
		kn_target_dir = kernfs_find_and_get(kn_mondata, name);

		if (snc_mode && !do_sum) {
			sprintf(name, "mon_sub_%s_%02d", r->name, d->hdr.id);
			kernfs_put(kn_target_dir);
			kn_target_dir = kernfs_find_and_get(kn_target_dir, name);
		}
	}
	kernfs_put(kn_target_dir);
	if (!kn_target_dir)
		return NULL;

-------%<-------

Quoted 9 lines

arch/x86/kernel/cpu/resctrl/rdtgroup.c static struct file_system_type rdt_fs_type = { · line 3158

Quoted 2 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +2 -0
+
+	kn_evt = kernfs_find_and_get(kn_dom, mevt->name);

Note the "...and_get..." in kernfs_find_and_get() that gets a reference to the kn before returning it. I expect that this work will have symmetry when it comes to get/put of references but I see four new calls to kernfs_find_and_get() but no new matching kernfs_put() to release the new references. It looks like kernfs_find_and_get() is just used to figure out what the kn is so the references need not be kept around for long.

Not sure how I missed that. There is no need to hold an extra reference as rdtgroup_mutex is being held, I'll add a kernfs_put() call after each involuntary _get(). (and a comment above the lockdep assert)

Quoted 30 lines
Quoted 30 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +30 -0
+
+	/* Is this the creation of the default groups monitor files? */
+	if (!kn_evt && rdtgrp == &rdtgroup_default) {
+		priv = kzalloc(sizeof(*priv), GFP_KERNEL);
+		if (!priv)
+			return NULL;
+		priv->rid = r->rid;
+		priv->domid = do_sum ? d->ci->id : d->hdr.id;
+		priv->sum = do_sum;
+		priv->evtid = mevt->evtid;
+		return priv;
+	}
+
+	if (!kn_evt)
+		return NULL;
+
+	return kn_evt->priv;
+}
+
+/**
+ * mon_put_default_kn_priv_all() - Potentially free the mon_data priv data for
+ *                                 all events from the default control group.
+ * Put the mon_data priv data for all events for a particular domain.
+ * When called with the default control group, the priv structure previously
+ * allocated will be kfree()d. This should only be done as part of taking a
+ * domain offline.
+ * Only a domain offline will 'rmdir' monitor files in the default control
+ * group. After domain offline releases rdtgrp_mutex, all references will
+ * have been removed.
+ *
Quoted 8 lines

arch/x86/kernel/cpu/resctrl/rdtgroup.c static struct file_system_type rdt_fs_type = { · line 3192

Quoted 4 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +4 -0
+ * @rdtgrp:  The rdtgroup for which the monitor files are being removed,
+ *           used to determine if this is the default control group.
+ * @name:    The name of the domain or SNC sub-numa domain which is being
+ *           taken offline.

This is a bit confusing since domains do not have names. How about (please feel free to improve):
"Name of directory containing monitoring files that is in process of being removed."

That's clearer, thanks!

Quoted 8 lines
Quoted 8 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +8 -0
+static void mon_put_default_kn_priv_all(struct rdtgroup *rdtgrp, char *name)
+{
+	struct rdt_resource *r = resctrl_arch_get_resource(RDT_RESOURCE_L3);
+	struct kernfs_node *kn_dom, *kn_evt;
+	struct mon_evt *mevt;
+
+	lockdep_assert_held(&rdtgroup_mutex);
+
Quoted 8 lines

arch/x86/kernel/cpu/resctrl/rdtgroup.c static struct file_system_type rdt_fs_type = { · line 3207

Quoted 6 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +6 -0
+	if (rdtgrp != &rdtgroup_default)
+		return;
+
+	kn_dom = kernfs_find_and_get(kn_mondata, name);
+	if (!kn_dom)
+		return;

I expect this will always fail when @name is a mon_sub_* directory.

Yup, fixed with a smiliar pattern to above - only the 'subname' is available to the caller, so it can be passed in.

Quoted 25 lines
Quoted 25 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +13 -1
+
+	list_for_each_entry(mevt, &r->evt_list, list) {
+		kn_evt = kernfs_find_and_get(kn_dom, mevt->name);
+		if (!kn_evt)
+			continue;
+		if (!kn_evt->priv)
+			continue;
+
+		kfree(kn_evt->priv);
+		kn_evt->priv = NULL;
+	}
+}
 
@@ -3182,19 +3292,19 @@ static int mon_add_all_files(struct kernfs_node *kn, struct rdt_mon_domain *d,
 			     bool do_sum)
 {
 	struct rmid_read rr = {0};
-	union mon_data_bits priv;
+	struct mon_data *priv;
 	struct mon_evt *mevt;
 	int ret;
 
 	if (WARN_ON(list_empty(&r->evt_list)))
 		return -EPERM;
 
Quoted 12 lines

arch/x86/kernel/cpu/resctrl/rdtgroup.c static int mon_add_all_files(struct kernfs_node *kn, struct rdt_mon_domain *d, · line 3306

Quoted 10 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +4 -5
-	priv.u.rid = r->rid;
-	priv.u.domid = do_sum ? d->ci->id : 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);
+		priv = mon_get_default_kn_priv(r, d, mevt, prgrp, do_sum);
+		if (WARN_ON_ONCE(!priv))
+			return -EINVAL;
+

This is the warning I hit on the SNC system.

Thanks for testing that, this is a 'should never happen - its a bug'.

Thanks!

James

maintainer
#90 lore raw

Re: [PATCH v7 37/49] x86/resctrl: Expand the width of dom_id by replacing mon_data_bits

Hi James,

Quoted 2 lines · James Morse
Quoted 1 line · Reinette Chatre

On 2/28/25 11:59 AM, James Morse wrote:

...

Quoted 8 lines
Quoted 4 lines

With all of the above I do not think this will work on an SNC enabled system ... to confirm this I tried it out and it is not possible to mount resctrl on an SNC enabled system and the WARN_ON_ONCE() this patch adds to mon_add_all_files() is hit.

I hadn't realised the mon_sub directories for SNC weren't all directly under mon_data. Searching from mon_data will need the parent name too. What I've come up with is:
-------%<-------

Quoted 9 lines
	snc_mode = r->mon_scope == RESCTRL_L3_NODE;
	if (!snc_mode) {
		sprintf(name, "mon_%s_%02d", r->name, d->hdr.id);
		kn_target_dir = kernfs_find_and_get(kn_mondata, name);
	} else {
		sprintf(name, "mon_%s_%02d", r->name, d->ci->id);
		kn_target_dir = kernfs_find_and_get(kn_mondata, name);

		if (snc_mode && !do_sum) {

snc_mode should always be true here?

Quoted 2 lines
			sprintf(name, "mon_sub_%s_%02d", r->name, d->hdr.id);
			kernfs_put(kn_target_dir);

I think this needs some extra guardrails. If kn_target_dir is NULL here it looks like that the kernfs_put() above will be fine, but from what I can tell the kernfs_find_and_get() below will not be.

Quoted 8 lines
			kn_target_dir = kernfs_find_and_get(kn_target_dir, name);
		}
	}
	kernfs_put(kn_target_dir);
	if (!kn_target_dir)
		return NULL;

-------%<-------

This looks good to me. In original patch a NULL kn within mon_get_default_kn_priv() was used as prompt to create the private data. It is thus not obvious to me from this snippet what is being returned "to", but I do not think that was your point of sharing this snippet.

Reinette

maintainer
#91 lore raw

Re: [PATCH v7 37/49] x86/resctrl: Expand the width of dom_id by replacing mon_data_bits

Quoted 44 lines · Reinette Chatre

Hi James,

Quoted 2 lines · James Morse
Quoted 1 line · Reinette Chatre

On 2/28/25 11:59 AM, James Morse wrote:

...

Quoted 8 lines
Quoted 4 lines

With all of the above I do not think this will work on an SNC enabled system ... to confirm this I tried it out and it is not possible to mount resctrl on an SNC enabled system and the WARN_ON_ONCE() this patch adds to mon_add_all_files() is hit.

I hadn't realised the mon_sub directories for SNC weren't all directly under mon_data. Searching from mon_data will need the parent name too. What I've come up with is:
-------%<-------

Quoted 9 lines
	snc_mode = r->mon_scope == RESCTRL_L3_NODE;
	if (!snc_mode) {
		sprintf(name, "mon_%s_%02d", r->name, d->hdr.id);
		kn_target_dir = kernfs_find_and_get(kn_mondata, name);
	} else {
		sprintf(name, "mon_%s_%02d", r->name, d->ci->id);
		kn_target_dir = kernfs_find_and_get(kn_mondata, name);

		if (snc_mode && !do_sum) {

snc_mode should always be true here?

Quoted 2 lines
			sprintf(name, "mon_sub_%s_%02d", r->name, d->hdr.id);
			kernfs_put(kn_target_dir);

I think this needs some extra guardrails. If kn_target_dir is NULL here it looks like that the kernfs_put() above will be fine, but from what I can tell the kernfs_find_and_get() below will not be.

Quoted 8 lines
			kn_target_dir = kernfs_find_and_get(kn_target_dir, name);
		}
	}
	kernfs_put(kn_target_dir);
	if (!kn_target_dir)
		return NULL;

-------%<-------

Quoted 4 lines

This looks good to me. In original patch a NULL kn within mon_get_default_kn_priv() was used as prompt to create the private data. It is thus not obvious to me from this snippet what is being returned "to", but I do not think that was your point of sharing this snippet.

Is this all overly complex trying to re-use the "priv" fields from the default resctrl group? Would it be easier to just keep a list of each combinations of region id, domain id, sum, and event id that have already been allocated and re-use existing ones, or add to the list for new ones. Scanning this list may be less overhead that all the sprintf() and kernfs_find_and_get() searches.

Something like:

static LIST_HEAD(kn_priv_list);

static struct mon_data *mon_get_kn_priv(struct rdt_resource *r,
struct rdt_mon_domain *d, struct mon_evt *mevt, struct rdtgroup *rdtgrp, bool do_sum)

{
	struct mon_data *priv;

	list_for_each_entry(priv, &kn_priv_list, list) {
		if (priv->rid == r->rid && priv->domid == (do_sum ? d->ci->id : d->hdr.id) &&
		    priv->sum == do_sum && priv->evtid == mevt->evtid)
			return priv;
		}
	}

	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return NULL;

	priv->rid = r->rid;
	priv->domid = do_sum ? d->ci->id : d->hdr.id;
	priv->sum = do_sum;
	priv->evtid = mevt->evtid;
	list_add_tail(&priv->list, &kn_priv_list);

	return priv;
}

Maybe just ignore the "domain goes offline case" there's not much memory tied up in these structures (and the domain may come back).

Just free the whole list when resctrl is unmounted.

static void mon_put_kn_priv(void)

{
	struct mon_data *priv, *tmp;

	list_for_each_entry_safe(priv, tmp, &kn_priv_list, list)
		kfree(priv);
}

[Lightly tested the "get" path. Haven't tried the "put" code.]

-Tony

maintainer
#92 lore raw

Re: [PATCH v7 37/49] x86/resctrl: Expand the width of dom_id by replacing mon_data_bits

Quoted 50 lines · Tony
Quoted 44 lines · Reinette Chatre

Hi James,

Quoted 2 lines · James Morse
Quoted 1 line · Reinette Chatre

On 2/28/25 11:59 AM, James Morse wrote:

...

Quoted 8 lines
Quoted 4 lines

With all of the above I do not think this will work on an SNC enabled system ... to confirm this I tried it out and it is not possible to mount resctrl on an SNC enabled system and the WARN_ON_ONCE() this patch adds to mon_add_all_files() is hit.

I hadn't realised the mon_sub directories for SNC weren't all directly under mon_data. Searching from mon_data will need the parent name too. What I've come up with is:
-------%<-------

Quoted 9 lines
	snc_mode = r->mon_scope == RESCTRL_L3_NODE;
	if (!snc_mode) {
		sprintf(name, "mon_%s_%02d", r->name, d->hdr.id);
		kn_target_dir = kernfs_find_and_get(kn_mondata, name);
	} else {
		sprintf(name, "mon_%s_%02d", r->name, d->ci->id);
		kn_target_dir = kernfs_find_and_get(kn_mondata, name);

		if (snc_mode && !do_sum) {

snc_mode should always be true here?

Quoted 2 lines
			sprintf(name, "mon_sub_%s_%02d", r->name, d->hdr.id);
			kernfs_put(kn_target_dir);

I think this needs some extra guardrails. If kn_target_dir is NULL here it looks like that the kernfs_put() above will be fine, but from what I can tell the kernfs_find_and_get() below will not be.

Quoted 8 lines
			kn_target_dir = kernfs_find_and_get(kn_target_dir, name);
		}
	}
	kernfs_put(kn_target_dir);
	if (!kn_target_dir)
		return NULL;

-------%<-------

Quoted 4 lines

This looks good to me. In original patch a NULL kn within mon_get_default_kn_priv() was used as prompt to create the private data. It is thus not obvious to me from this snippet what is being returned "to", but I do not think that was your point of sharing this snippet.

Quoted 6 lines

Is this all overly complex trying to re-use the "priv" fields from the default resctrl group? Would it be easier to just keep a list of each combinations of region id, domain id, sum, and event id that have already been allocated and re-use existing ones, or add to the list for new ones. Scanning this list may be less overhead that all the sprintf() and kernfs_find_and_get() searches.

James,

I played around with the simplification some more and tested on both normal and SNC systems. Below is a patch against:

git://git.kernel.org/pub/scm/linux/kernel/git/morse/linux.git mpam/move_to_fs/v7

Note that this is *after* the move to fs/resctrl as I was too chicken to try applying this before the move and then re-run the script to move things. But if you take this suggestion, just mash it into your "Expand the width of dom_id by replacing mon_data_bits" patch. Maybe give me Co-developed-by credit, but that's not important.

Note that your expansion of mon_data is going to be very useful going forward. I want to add extra information to struct mon_data:

1) Flag to note that an event counter can be read from any CPU, not just the ones in the domain specified by the mon_data/mon_L3_XX/* file.

2) Type field to specify how to display the value of each counter (since I want floating point instead of integer for the energy counters).

-Tony

From e2689c7439572608ce03a525c71c3fb88379057c Mon Sep 17 00:00:00 2001
From: Tony Luck <[email protected]>
Date: Thu, 3 Apr 2025 10:53:55 -0700
Subject: [PATCH] fs/resctrl: Simplify allocation of mon_data structures

Instead of making a special case to allocate and attach these structures to kernfs files in the default control group, simply allocate a structure when a new combination of <rid, domain, mevt, do_sum> is needed and re-use existing structures when possible.

Free all structures when resctrl filesystem is unmounted.

Partial revert of commit fa563b5171e9 ("x86/resctrl: Expand the width of dom_id by replacing mon_data_bits")

---
 fs/resctrl/internal.h |   2 +
 fs/resctrl/rdtgroup.c | 138 ++++++++++++------------------------------
 2 files changed, 40 insertions(+), 100 deletions(-)
fs/resctrl/internal.h +2 -0
index ec3863d18f68..e5976bd52a35 100644
@@ -83,6 +83,7 @@ struct mon_evt {
 
 /**
  * struct mon_data - Monitoring details for each event file.
+ * @list:            List of all allocated structures.
  * @rid:             Resource id associated with the event file.
  * @evtid:           Event id associated with the event file.
  * @sum:             Set when event must be summed across multiple
@@ -96,6 +97,7 @@ struct mon_evt {
  * rdtgroup_mutex.
  */
 struct mon_data {
+	struct list_head list;
 	unsigned int rid;
 	enum resctrl_event_id evtid;
 	unsigned int sum;
fs/resctrl/rdtgroup.c +38 -100

197 lines not on this page · raw message

Signature · 1 line
2.48.1
author reviewer
#93 lore raw

Re: [PATCH v7 37/49] x86/resctrl: Expand the width of dom_id by replacing mon_data_bits

Hi Tony,

Quoted 3 lines · Tony
Quoted 2 lines · Tony

Is this all overly complex trying to re-use the "priv" fields from the default resctrl group?

I was trying to keep the kernfs hierarchy as the source of truth for this, and keep the extra memory allocated to a minimum.

Quoted 4 lines
Quoted 4 lines

Would it be easier to just keep a list
of each combinations of region id, domain id, sum, and event id that have already been allocated and re-use existing ones, or add to the list for new ones.

I agree this is much simpler!

Quoted 7 lines
Quoted 2 lines

Scanning this list may be less overhead that all the sprintf() and kernfs_find_and_get() searches.

I played around with the simplification some more and tested on both normal and SNC systems. Below is a patch against:

Quoted 7 lines

git://git.kernel.org/pub/scm/linux/kernel/git/morse/linux.git mpam/move_to_fs/v7

Note that this is *after* the move to fs/resctrl as I was too chicken to try applying this before the move and then re-run the script to move things. But if you take this suggestion, just mash it into your "Expand the width of dom_id by replacing mon_data_bits" patch. Maybe give me Co-developed-by credit, but that's not important.

Done!

Quoted 6 lines

Note that your expansion of mon_data is going to be very useful going forward. I want to add extra information to struct mon_data:

1) Flag to note that an event counter can be read from any CPU, not just the ones in the domain specified by the mon_data/mon_L3_XX/* file.

Awesome - (some) arm64 and risc-v have this too.

Quoted 3 lines

2) Type field to specify how to display the value of each counter (since I want floating point instead of integer for the energy counters).

Thanks,

James

#94 lore raw

PATCH v7 37/49 x86/resctrl: Expand the width of dom_id by replacing mon_data_bits

In reply to James Morse

Hi James,

Quoted 184 lines

MPAM platforms retrieve the cache-id property from the ACPI PPTT table. The cache-id field is 32 bits wide. Under resctrl, the cache-id becomes the domain-id, and is packed into the mon_data_bits union bitfield. The width of cache-id in this field is 14 bits.

Expanding the union would break 32bit x86 platforms as this union is stored as the kernfs kn->priv pointer. This saved allocating memory for the priv data storage.

The firmware on MPAM platforms have used the PPTT cache-id field to expose the interconnect's id for the cache, which is sparse and uses more than 14 bits. Use of this id is to enable PCIe direct cache injection hints. Using this feature with VFIO means the value provided by the ACPI table should be exposed to user-space.

To support cache-id values greater than 14 bits, convert the mon_data_bits union to a structure. This is allocated for the default control group when the kernfs event files are created, and free'd when the monitor directory is rmdir'd when the domain goes offline. All other control and monitor groups lookup the struct mon_data allocated for the default control group, and use this.
This simplifies the lifecycle of this structure as the default control group cannot be rmdir()d by user-space, so only needs to consider domain-offline, which removes all the event files corresponding to a domain while holding rdtgroup_mutex - which prevents concurrent readers. mkdir_mondata_subdir_allrdtgrp() must special case the default control group to ensure it is created first.

---
Previously the MPAM tree repainted the cache-id to compact them, argue-ing there was no other user. With VFIO use of this PCIe feature, this is no longer an option.

Changes since v6:
* Added the get/put helpers.
* Special case the creation of the mondata files for the default control group.
* Removed wording about files living longer than expected, the corresponding error handling is wrapped in WARN_ON_ONCE() as this indicates a bug.

---
  arch/x86/kernel/cpu/resctrl/ctrlmondata.c |  19 +--
  arch/x86/kernel/cpu/resctrl/internal.h    |  37 +++---
  arch/x86/kernel/cpu/resctrl/monitor.c     |   3 +
  arch/x86/kernel/cpu/resctrl/rdtgroup.c    | 142 ++++++++++++++++++++--
  4 files changed, 162 insertions(+), 39 deletions(-)
arch/x86/kernel/cpu/resctrl/ctrlmondata.c +12 -7
index 0a0ac5f6112e..159972c3fe73 100644
@@ -667,7 +667,7 @@ int rdtgroup_mondata_show(struct seq_file *m, void *arg)
  	u32 resid, evtid, domid;
  	struct rdtgroup *rdtgrp;
  	struct rdt_resource *r;
-	union mon_data_bits md;
+	struct mon_data *md;
  	int ret = 0;
 
  	rdtgrp = rdtgroup_kn_lock_live(of->kn);
@@ -676,17 +676,22 @@ int rdtgroup_mondata_show(struct seq_file *m, void *arg)
  		goto out;
  	}
 
-	md.priv = of->kn->priv;
-	resid = md.u.rid;
-	domid = md.u.domid;
-	evtid = md.u.evtid;
+	md = of->kn->priv;
+	if (WARN_ON_ONCE(!md)) {
+		ret = -EIO;
+		goto out;
+	}
+
+	resid = md->rid;
+	domid = md->domid;
+	evtid = md->evtid;
  	r = resctrl_arch_get_resource(resid);
 
-	if (md.u.sum) {
+	if (md->sum) {
  		/*
  		 * This file requires summing across all domains that share
  		 * the L3 cache id that was provided in the "domid" field of the
-		 * mon_data_bits union. Search all domains in the resource for
+		 * struct mon_data. Search all domains in the resource for
  		 * one that matches this cache id.
  		 */
  		list_for_each_entry(d, &r->mon_domains, hdr.list) {
arch/x86/kernel/cpu/resctrl/internal.h +17 -20
index 32ed9aeffb90..16c1a391d012 100644
@@ -103,27 +103,24 @@ struct mon_evt {
  };
 
  /**
- * union mon_data_bits - Monitoring details for each event file.
- * @priv:              Used to store monitoring event data in @u
- *                     as kernfs private data.
- * @u.rid:             Resource id associated with the event file.
- * @u.evtid:           Event id associated with the event file.
- * @u.sum:             Set when event must be summed across multiple
- *                     domains.
- * @u.domid:           When @u.sum is zero this is the domain to which
- *                     the event file belongs. When @sum is one this
- *                     is the id of the L3 cache that all domains to be
- *                     summed share.
- * @u:                 Name of the bit fields struct.
+ * struct mon_data - Monitoring details for each event file.
+ * @rid:             Resource id associated with the event file.
+ * @evtid:           Event id associated with the event file.
+ * @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 id of the L3 cache that all domains to be
+ *                   summed share.
+ *
+ * Stored in the kernfs kn->priv field, readers and writers must hold
+ * rdtgroup_mutex.
   */
-union mon_data_bits {
-	void *priv;
-	struct {
-		unsigned int rid		: 10;
-		enum resctrl_event_id evtid	: 7;
-		unsigned int sum		: 1;
-		unsigned int domid		: 14;
-	} u;
+struct mon_data {
+	unsigned int rid;
+	enum resctrl_event_id evtid;
+	unsigned int sum;
+	unsigned int domid;
  };
 
  /**
arch/x86/kernel/cpu/resctrl/monitor.c +3 -0
index 73e3fe4f4c87..5bf67b429dd3 100644
@@ -1096,6 +1096,9 @@ static struct mon_evt mbm_local_event = {
   * Note that MBM events are also part of RDT_RESOURCE_L3 resource
   * because as per the SDM the total and local memory bandwidth
   * are enumerated as part of L3 monitoring.
+ *
+ * mon_put_default_kn_priv_all() also assumes monitor events are only supported
+ * on the L3 resource.
   */
  static void l3_mon_evt_init(struct rdt_resource *r)
  {
arch/x86/kernel/cpu/resctrl/rdtgroup.c +24 -0
index aecd3fa734cd..443635d195f0 100644
@@ -3114,6 +3114,110 @@ static struct file_system_type rdt_fs_type = {
  	.kill_sb		= rdt_kill_sb,
  };
 
+/**
+ * mon_get_default_kn_priv() - Get the mon_data priv data for this event from
+ *                             the default control group.
+ * Called when monitor event files are created for a domain.
+ * When called with the default control group, the structure will be allocated.
+ * This happens at mount time, before other control or monitor groups are
+ * created.
+ * This simplifies the lifetime management for rmdir() versus domain-offline
+ * as the default control group lives forever, and only one group needs to be
+ * special cased.
+ *
+ * @r:      The resource for the event type being created.
+ * @d:	    The domain for the event type being created.
+ * @mevt:   The event type being created.
+ * @rdtgrp: The rdtgroup for which the monitor file is being created,
+ *          used to determine if this is the default control group.
+ * @do_sum: Whether the SNC sub-numa node monitors are being created.
+ */
+static struct mon_data *mon_get_default_kn_priv(struct rdt_resource *r,
+						struct rdt_mon_domain *d,
+						struct mon_evt *mevt,
+						struct rdtgroup *rdtgrp,
+						bool do_sum)
+{

arch/x86/kernel/cpu/resctrl/rdtgroup.c static struct file_system_type rdt_fs_type = { · line 3150

Quoted 10 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +10 -0
+	struct kernfs_node *kn_dom, *kn_evt;
+	struct mon_data *priv;
+	bool snc_mode;
+	char name[32];
+
+	lockdep_assert_held(&rdtgroup_mutex);
+
+	snc_mode = r->mon_scope == RESCTRL_L3_NODE;
+	if (!do_sum)
+		sprintf(name, "mon_%s_%02d", r->name, snc_mode ? d->ci->id : d->hdr.id);

This change triggered a minor report during compilation.

fs/resctrl/rdtgroup.c: In function ‘mon_get_default_kn_priv’:
fs/resctrl/rdtgroup.c:2931:28: warning: format ‘%d’ expects argument of type ‘int’, but argument 4 has type ‘long unsigned int’ [-Wformat=] 2931 |   sprintf(name, "mon_%s_%02d", r->name, snc_mode ? d->ci->id : d->hdr.id);

      |                         ~~~^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      | |                                 |
      | int                               long unsigned int
      |                         %02ld
Quoted 163 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +96 -12
+	else
+		sprintf(name, "mon_sub_%s_%02d", r->name, d->hdr.id);
+
+	kn_dom = kernfs_find_and_get(kn_mondata, name);
+	if (!kn_dom)
+		return NULL;
+
+	kn_evt = kernfs_find_and_get(kn_dom, mevt->name);
+
+	/* Is this the creation of the default groups monitor files? */
+	if (!kn_evt && rdtgrp == &rdtgroup_default) {
+		priv = kzalloc(sizeof(*priv), GFP_KERNEL);
+		if (!priv)
+			return NULL;
+		priv->rid = r->rid;
+		priv->domid = do_sum ? d->ci->id : d->hdr.id;
+		priv->sum = do_sum;
+		priv->evtid = mevt->evtid;
+		return priv;
+	}
+
+	if (!kn_evt)
+		return NULL;
+
+	return kn_evt->priv;
+}
+
+/**
+ * mon_put_default_kn_priv_all() - Potentially free the mon_data priv data for
+ *                                 all events from the default control group.
+ * Put the mon_data priv data for all events for a particular domain.
+ * When called with the default control group, the priv structure previously
+ * allocated will be kfree()d. This should only be done as part of taking a
+ * domain offline.
+ * Only a domain offline will 'rmdir' monitor files in the default control
+ * group. After domain offline releases rdtgrp_mutex, all references will
+ * have been removed.
+ *
+ * @rdtgrp:  The rdtgroup for which the monitor files are being removed,
+ *           used to determine if this is the default control group.
+ * @name:    The name of the domain or SNC sub-numa domain which is being
+ *           taken offline.
+ */
+static void mon_put_default_kn_priv_all(struct rdtgroup *rdtgrp, char *name)
+{
+	struct rdt_resource *r = resctrl_arch_get_resource(RDT_RESOURCE_L3);
+	struct kernfs_node *kn_dom, *kn_evt;
+	struct mon_evt *mevt;
+
+	lockdep_assert_held(&rdtgroup_mutex);
+
+	if (rdtgrp != &rdtgroup_default)
+		return;
+
+	kn_dom = kernfs_find_and_get(kn_mondata, name);
+	if (!kn_dom)
+		return;
+
+	list_for_each_entry(mevt, &r->evt_list, list) {
+		kn_evt = kernfs_find_and_get(kn_dom, mevt->name);
+		if (!kn_evt)
+			continue;
+		if (!kn_evt->priv)
+			continue;
+
+		kfree(kn_evt->priv);
+		kn_evt->priv = NULL;
+	}
+}
+
  static int mon_addfile(struct kernfs_node *parent_kn, const char *name,
  		       void *priv)
  {
@@ -3135,19 +3239,25 @@ static int mon_addfile(struct kernfs_node *parent_kn, const char *name,
  	return ret;
  }
 
-static void mon_rmdir_one_subdir(struct kernfs_node *pkn, char *name, char *subname)
+static void mon_rmdir_one_subdir(struct rdtgroup *rdtgrp, char *name, char *subname)
  {
+	struct kernfs_node *pkn = rdtgrp->mon.mon_data_kn;
  	struct kernfs_node *kn;
 
  	kn = kernfs_find_and_get(pkn, name);
  	if (!kn)
  		return;
+
+	mon_put_default_kn_priv_all(rdtgrp, name);
+
  	kernfs_put(kn);
 
-	if (kn->dir.subdirs <= 1)
+	if (kn->dir.subdirs <= 1) {
  		kernfs_remove(kn);
-	else
+	} else {
+		mon_put_default_kn_priv_all(rdtgrp, subname);
  		kernfs_remove_by_name(kn, subname);
+	}
  }
 
  /*
@@ -3170,10 +3280,10 @@ static void rmdir_mondata_subdir_allrdtgrp(struct rdt_resource *r,
  		sprintf(subname, "mon_sub_%s_%02d", r->name, d->hdr.id);
 
  	list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
-		mon_rmdir_one_subdir(prgrp->mon.mon_data_kn, name, subname);
+		mon_rmdir_one_subdir(prgrp, name, subname);
 
  		list_for_each_entry(crgrp, &prgrp->mon.crdtgrp_list, mon.crdtgrp_list)
-			mon_rmdir_one_subdir(crgrp->mon.mon_data_kn, name, subname);
+			mon_rmdir_one_subdir(crgrp, name, subname);
  	}
  }
 
@@ -3182,19 +3292,19 @@ static int mon_add_all_files(struct kernfs_node *kn, struct rdt_mon_domain *d,
  			     bool do_sum)
  {
  	struct rmid_read rr = {0};
-	union mon_data_bits priv;
+	struct mon_data *priv;
  	struct mon_evt *mevt;
  	int ret;
 
  	if (WARN_ON(list_empty(&r->evt_list)))
  		return -EPERM;
 
-	priv.u.rid = r->rid;
-	priv.u.domid = do_sum ? d->ci->id : 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);
+		priv = mon_get_default_kn_priv(r, d, mevt, prgrp, do_sum);
+		if (WARN_ON_ONCE(!priv))
+			return -EINVAL;
+
+		ret = mon_addfile(kn, mevt->name, priv);
  		if (ret)
  			return ret;
 
@@ -3274,9 +3384,17 @@ static void mkdir_mondata_subdir_allrdtgrp(struct rdt_resource *r,
  	struct rdtgroup *prgrp, *crgrp;
  	struct list_head *head;
 
+	/*
+	 * During domain-online create the default control group first
+	 * so that mon_get_default_kn_priv() can find the allocated structure
+	 * on subsequent calls.
+	 */
+	mkdir_mondata_subdir(kn_mondata, d, r, &rdtgroup_default);
+
  	list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
  		parent_kn = prgrp->mon.mon_data_kn;
-		mkdir_mondata_subdir(parent_kn, d, r, prgrp);
+		if (prgrp != &rdtgroup_default)
+			mkdir_mondata_subdir(parent_kn, d, r, prgrp);
 
  		head = &prgrp->mon.crdtgrp_list;
  		list_for_each_entry(crgrp, head, mon.crdtgrp_list) {
Signature · 1 line
2.39.5
Quoted 359 lines · James Morse

MPAM platforms retrieve the cache-id property from the ACPI PPTT table. The cache-id field is 32 bits wide. Under resctrl, the cache-id becomes the domain-id, and is packed into the mon_data_bits union bitfield. The width of cache-id in this

MPAM platforms retrieve the cache-id property from the ACPI PPTT table. The cache-id field is 32 bits wide. Under resctrl, the cache-id becomes the domain-id, and is packed into the mon_data_bits union bitfield. The width of cache-id in this field is 14 bits.

Expanding the union would break 32bit x86 platforms as this union is stored as the kernfs kn->priv pointer. This saved allocating memory for the priv data storage.

The firmware on MPAM platforms have used the PPTT cache-id field to expose the interconnect's id for the cache, which is sparse and uses more than 14 bits. Use of this id is to enable PCIe direct cache injection hints. Using this feature with VFIO means the value provided by the ACPI table should be exposed to user-space.

To support cache-id values greater than 14 bits, convert the mon_data_bits union to a structure. This is allocated for the default control group when the kernfs event files are created, and free'd when the monitor directory is rmdir'd when the domain goes offline. All other control and monitor groups lookup the struct mon_data allocated for the default control group, and use this.
This simplifies the lifecycle of this structure as the default control group cannot be rmdir()d by user-space, so only needs to consider domain-offline, which removes all the event files corresponding to a domain while holding rdtgroup_mutex - which prevents concurrent readers. mkdir_mondata_subdir_allrdtgrp() must special case the default control group to ensure it is created first.

---
Previously the MPAM tree repainted the cache-id to compact them, argue-ing there was no other user. With VFIO use of this PCIe feature, this is no longer an option.

Changes since v6:
* Added the get/put helpers.
* Special case the creation of the mondata files for the default control group.
* Removed wording about files living longer than expected, the corresponding error handling is wrapped in WARN_ON_ONCE() as this indicates a bug.

---
  arch/x86/kernel/cpu/resctrl/ctrlmondata.c |  19 +--
  arch/x86/kernel/cpu/resctrl/internal.h    |  37 +++---
  arch/x86/kernel/cpu/resctrl/monitor.c     |   3 +
  arch/x86/kernel/cpu/resctrl/rdtgroup.c    | 142 ++++++++++++++++++++--
  4 files changed, 162 insertions(+), 39 deletions(-)
arch/x86/kernel/cpu/resctrl/ctrlmondata.c +12 -7
index 0a0ac5f6112e..159972c3fe73 100644
@@ -667,7 +667,7 @@ int rdtgroup_mondata_show(struct seq_file *m, void *arg)
  	u32 resid, evtid, domid;
  	struct rdtgroup *rdtgrp;
  	struct rdt_resource *r;
-	union mon_data_bits md;
+	struct mon_data *md;
  	int ret = 0;
 
  	rdtgrp = rdtgroup_kn_lock_live(of->kn);
@@ -676,17 +676,22 @@ int rdtgroup_mondata_show(struct seq_file *m, void *arg)
  		goto out;
  	}
 
-	md.priv = of->kn->priv;
-	resid = md.u.rid;
-	domid = md.u.domid;
-	evtid = md.u.evtid;
+	md = of->kn->priv;
+	if (WARN_ON_ONCE(!md)) {
+		ret = -EIO;
+		goto out;
+	}
+
+	resid = md->rid;
+	domid = md->domid;
+	evtid = md->evtid;
  	r = resctrl_arch_get_resource(resid);
 
-	if (md.u.sum) {
+	if (md->sum) {
  		/*
  		 * This file requires summing across all domains that share
  		 * the L3 cache id that was provided in the "domid" field of the
-		 * mon_data_bits union. Search all domains in the resource for
+		 * struct mon_data. Search all domains in the resource for
  		 * one that matches this cache id.
  		 */
  		list_for_each_entry(d, &r->mon_domains, hdr.list) {
arch/x86/kernel/cpu/resctrl/internal.h +17 -20
index 32ed9aeffb90..16c1a391d012 100644
@@ -103,27 +103,24 @@ struct mon_evt {
  };
 
  /**
- * union mon_data_bits - Monitoring details for each event file.
- * @priv:              Used to store monitoring event data in @u
- *                     as kernfs private data.
- * @u.rid:             Resource id associated with the event file.
- * @u.evtid:           Event id associated with the event file.
- * @u.sum:             Set when event must be summed across multiple
- *                     domains.
- * @u.domid:           When @u.sum is zero this is the domain to which
- *                     the event file belongs. When @sum is one this
- *                     is the id of the L3 cache that all domains to be
- *                     summed share.
- * @u:                 Name of the bit fields struct.
+ * struct mon_data - Monitoring details for each event file.
+ * @rid:             Resource id associated with the event file.
+ * @evtid:           Event id associated with the event file.
+ * @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 id of the L3 cache that all domains to be
+ *                   summed share.
+ *
+ * Stored in the kernfs kn->priv field, readers and writers must hold
+ * rdtgroup_mutex.
   */
-union mon_data_bits {
-	void *priv;
-	struct {
-		unsigned int rid		: 10;
-		enum resctrl_event_id evtid	: 7;
-		unsigned int sum		: 1;
-		unsigned int domid		: 14;
-	} u;
+struct mon_data {
+	unsigned int rid;
+	enum resctrl_event_id evtid;
+	unsigned int sum;
+	unsigned int domid;
  };
 
  /**
arch/x86/kernel/cpu/resctrl/monitor.c +3 -0
index 73e3fe4f4c87..5bf67b429dd3 100644
@@ -1096,6 +1096,9 @@ static struct mon_evt mbm_local_event = {
   * Note that MBM events are also part of RDT_RESOURCE_L3 resource
   * because as per the SDM the total and local memory bandwidth
   * are enumerated as part of L3 monitoring.
+ *
+ * mon_put_default_kn_priv_all() also assumes monitor events are only supported
+ * on the L3 resource.
   */
  static void l3_mon_evt_init(struct rdt_resource *r)
  {
arch/x86/kernel/cpu/resctrl/rdtgroup.c +130 -12
index aecd3fa734cd..443635d195f0 100644
@@ -3114,6 +3114,110 @@ static struct file_system_type rdt_fs_type = {
  	.kill_sb		= rdt_kill_sb,
  };
 
+/**
+ * mon_get_default_kn_priv() - Get the mon_data priv data for this event from
+ *                             the default control group.
+ * Called when monitor event files are created for a domain.
+ * When called with the default control group, the structure will be allocated.
+ * This happens at mount time, before other control or monitor groups are
+ * created.
+ * This simplifies the lifetime management for rmdir() versus domain-offline
+ * as the default control group lives forever, and only one group needs to be
+ * special cased.
+ *
+ * @r:      The resource for the event type being created.
+ * @d:	    The domain for the event type being created.
+ * @mevt:   The event type being created.
+ * @rdtgrp: The rdtgroup for which the monitor file is being created,
+ *          used to determine if this is the default control group.
+ * @do_sum: Whether the SNC sub-numa node monitors are being created.
+ */
+static struct mon_data *mon_get_default_kn_priv(struct rdt_resource *r,
+						struct rdt_mon_domain *d,
+						struct mon_evt *mevt,
+						struct rdtgroup *rdtgrp,
+						bool do_sum)
+{
+	struct kernfs_node *kn_dom, *kn_evt;
+	struct mon_data *priv;
+	bool snc_mode;
+	char name[32];
+
+	lockdep_assert_held(&rdtgroup_mutex);
+
+	snc_mode = r->mon_scope == RESCTRL_L3_NODE;
+	if (!do_sum)
+		sprintf(name, "mon_%s_%02d", r->name, snc_mode ? d->ci->id : d->hdr.id);
+	else
+		sprintf(name, "mon_sub_%s_%02d", r->name, d->hdr.id);
+
+	kn_dom = kernfs_find_and_get(kn_mondata, name);
+	if (!kn_dom)
+		return NULL;
+
+	kn_evt = kernfs_find_and_get(kn_dom, mevt->name);
+
+	/* Is this the creation of the default groups monitor files? */
+	if (!kn_evt && rdtgrp == &rdtgroup_default) {
+		priv = kzalloc(sizeof(*priv), GFP_KERNEL);
+		if (!priv)
+			return NULL;
+		priv->rid = r->rid;
+		priv->domid = do_sum ? d->ci->id : d->hdr.id;
+		priv->sum = do_sum;
+		priv->evtid = mevt->evtid;
+		return priv;
+	}
+
+	if (!kn_evt)
+		return NULL;
+
+	return kn_evt->priv;
+}
+
+/**
+ * mon_put_default_kn_priv_all() - Potentially free the mon_data priv data for
+ *                                 all events from the default control group.
+ * Put the mon_data priv data for all events for a particular domain.
+ * When called with the default control group, the priv structure previously
+ * allocated will be kfree()d. This should only be done as part of taking a
+ * domain offline.
+ * Only a domain offline will 'rmdir' monitor files in the default control
+ * group. After domain offline releases rdtgrp_mutex, all references will
+ * have been removed.
+ *
+ * @rdtgrp:  The rdtgroup for which the monitor files are being removed,
+ *           used to determine if this is the default control group.
+ * @name:    The name of the domain or SNC sub-numa domain which is being
+ *           taken offline.
+ */
+static void mon_put_default_kn_priv_all(struct rdtgroup *rdtgrp, char *name)
+{
+	struct rdt_resource *r = resctrl_arch_get_resource(RDT_RESOURCE_L3);
+	struct kernfs_node *kn_dom, *kn_evt;
+	struct mon_evt *mevt;
+
+	lockdep_assert_held(&rdtgroup_mutex);
+
+	if (rdtgrp != &rdtgroup_default)
+		return;
+
+	kn_dom = kernfs_find_and_get(kn_mondata, name);
+	if (!kn_dom)
+		return;
+
+	list_for_each_entry(mevt, &r->evt_list, list) {
+		kn_evt = kernfs_find_and_get(kn_dom, mevt->name);
+		if (!kn_evt)
+			continue;
+		if (!kn_evt->priv)
+			continue;
+
+		kfree(kn_evt->priv);
+		kn_evt->priv = NULL;
+	}
+}
+
  static int mon_addfile(struct kernfs_node *parent_kn, const char *name,
  		       void *priv)
  {
@@ -3135,19 +3239,25 @@ static int mon_addfile(struct kernfs_node *parent_kn, const char *name,
  	return ret;
  }
 
-static void mon_rmdir_one_subdir(struct kernfs_node *pkn, char *name, char *subname)
+static void mon_rmdir_one_subdir(struct rdtgroup *rdtgrp, char *name, char *subname)
  {
+	struct kernfs_node *pkn = rdtgrp->mon.mon_data_kn;
  	struct kernfs_node *kn;
 
  	kn = kernfs_find_and_get(pkn, name);
  	if (!kn)
  		return;
+
+	mon_put_default_kn_priv_all(rdtgrp, name);
+
  	kernfs_put(kn);
 
-	if (kn->dir.subdirs <= 1)
+	if (kn->dir.subdirs <= 1) {
  		kernfs_remove(kn);
-	else
+	} else {
+		mon_put_default_kn_priv_all(rdtgrp, subname);
  		kernfs_remove_by_name(kn, subname);
+	}
  }
 
  /*
@@ -3170,10 +3280,10 @@ static void rmdir_mondata_subdir_allrdtgrp(struct rdt_resource *r,
  		sprintf(subname, "mon_sub_%s_%02d", r->name, d->hdr.id);
 
  	list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
-		mon_rmdir_one_subdir(prgrp->mon.mon_data_kn, name, subname);
+		mon_rmdir_one_subdir(prgrp, name, subname);
 
  		list_for_each_entry(crgrp, &prgrp->mon.crdtgrp_list, mon.crdtgrp_list)
-			mon_rmdir_one_subdir(crgrp->mon.mon_data_kn, name, subname);
+			mon_rmdir_one_subdir(crgrp, name, subname);
  	}
  }
 
@@ -3182,19 +3292,19 @@ static int mon_add_all_files(struct kernfs_node *kn, struct rdt_mon_domain *d,
  			     bool do_sum)
  {
  	struct rmid_read rr = {0};
-	union mon_data_bits priv;
+	struct mon_data *priv;
  	struct mon_evt *mevt;
  	int ret;
 
  	if (WARN_ON(list_empty(&r->evt_list)))
  		return -EPERM;
 
-	priv.u.rid = r->rid;
-	priv.u.domid = do_sum ? d->ci->id : 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);
+		priv = mon_get_default_kn_priv(r, d, mevt, prgrp, do_sum);
+		if (WARN_ON_ONCE(!priv))
+			return -EINVAL;
+
+		ret = mon_addfile(kn, mevt->name, priv);
  		if (ret)
  			return ret;
 
@@ -3274,9 +3384,17 @@ static void mkdir_mondata_subdir_allrdtgrp(struct rdt_resource *r,
  	struct rdtgroup *prgrp, *crgrp;
  	struct list_head *head;
 
+	/*
+	 * During domain-online create the default control group first
+	 * so that mon_get_default_kn_priv() can find the allocated structure
+	 * on subsequent calls.
+	 */
+	mkdir_mondata_subdir(kn_mondata, d, r, &rdtgroup_default);
+
  	list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
  		parent_kn = prgrp->mon.mon_data_kn;
-		mkdir_mondata_subdir(parent_kn, d, r, prgrp);
+		if (prgrp != &rdtgroup_default)
+			mkdir_mondata_subdir(parent_kn, d, r, prgrp);
 
  		head = &prgrp->mon.crdtgrp_list;
  		list_for_each_entry(crgrp, head, mon.crdtgrp_list) {
Signature · 1 line
2.39.5
author reviewer
#95 lore raw

Re: [PATCH v7 37/49] x86/resctrl: Expand the width of dom_id by replacing mon_data_bits

Hi Amit,

Quoted 74 lines · Amit Singh Tomar
Quoted 61 lines

MPAM platforms retrieve the cache-id property from the ACPI PPTT table. The cache-id field is 32 bits wide. Under resctrl, the cache-id becomes the domain-id, and is packed into the mon_data_bits union bitfield. The width of cache-id in this field is 14 bits.

Expanding the union would break 32bit x86 platforms as this union is stored as the kernfs kn->priv pointer. This saved allocating memory for the priv data storage.

The firmware on MPAM platforms have used the PPTT cache-id field to expose the interconnect's id for the cache, which is sparse and uses more than 14 bits. Use of this id is to enable PCIe direct cache injection hints. Using this feature with VFIO means the value provided by the ACPI table should be exposed to user-space.

To support cache-id values greater than 14 bits, convert the mon_data_bits union to a structure. This is allocated for the default control group when the kernfs event files are created, and free'd when the monitor directory is rmdir'd when the domain goes offline. All other control and monitor groups lookup the struct mon_data allocated for the default control group, and use this.
This simplifies the lifecycle of this structure as the default control group cannot be rmdir()d by user-space, so only needs to consider domain-offline, which removes all the event files corresponding to a domain while holding rdtgroup_mutex - which prevents concurrent readers. mkdir_mondata_subdir_allrdtgrp() must special case the default control group to ensure it is created first.

arch/x86/kernel/cpu/resctrl/ renamed from arch/x86/kernel/cpu/resctrl/rdtgroup.c +0 -0

rdtgroup.c
index aecd3fa734cd..443635d195f0 100644

arch/x86/kernel/cpu/resctrl/rdtgroup.c +0 -0
@@ -3114,6 +3114,110 @@ static struct file_system_type rdt_fs_type = {
       .kill_sb        = rdt_kill_sb,
   };

  +/**
+ * mon_get_default_kn_priv() - Get the mon_data priv data for this event from
+ *                             the default control group.
+ * Called when monitor event files are created for a domain.
+ * When called with the default control group, the structure will be allocated.
+ * This happens at mount time, before other control or monitor groups are
+ * created.
+ * This simplifies the lifetime management for rmdir() versus domain-offline
+ * as the default control group lives forever, and only one group needs to be
+ * special cased.
+ *
+ * @r:      The resource for the event type being created.
+ * @d:        The domain for the event type being created.
+ * @mevt:   The event type being created.
+ * @rdtgrp: The rdtgroup for which the monitor file is being created,
+ *          used to determine if this is the default control group.
+ * @do_sum: Whether the SNC sub-numa node monitors are being created.
+ */

arch/x86/kernel/cpu/resctrl/rdtgroup.c +6 -0
+static struct mon_data *mon_get_default_kn_priv(struct rdt_resource *r,
+                        struct rdt_mon_domain *d,
+                        struct mon_evt *mevt,
+                        struct rdtgroup *rdtgrp,
+                        bool do_sum)
+{

arch/x86/kernel/cpu/resctrl/rdtgroup.c static struct file_system_type rdt_fs_type = { · line 3150

Quoted 10 lines
arch/x86/kernel/cpu/resctrl/rdtgroup.c +10 -0
+    struct kernfs_node *kn_dom, *kn_evt;
+    struct mon_data *priv;
+    bool snc_mode;
+    char name[32];
+
+    lockdep_assert_held(&rdtgroup_mutex);
+
+    snc_mode = r->mon_scope == RESCTRL_L3_NODE;
+    if (!do_sum)
+        sprintf(name, "mon_%s_%02d", r->name, snc_mode ? d->ci->id : d->hdr.id);

This change triggered a minor report during compilation.

Quoted 8 lines

fs/resctrl/rdtgroup.c: In function ‘mon_get_default_kn_priv’:
fs/resctrl/rdtgroup.c:2931:28: warning: format ‘%d’ expects argument of type ‘int’, but argument 4 has type ‘long unsigned int’ [-Wformat=]
 2931 |   sprintf(name, "mon_%s_%02d", r->name, snc_mode ? d->ci->id : d->hdr.id);

      |                         ~~~^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      | |                                 |
      | int                               long unsigned int
      |                         %02ld

Heh, not yet its not! You must have rebased the MPAM tree on-top, its a patch in there that causes this:
This is because of the device-tree folk want to make cache-id an unsigned long so they can use the arm CPU's affinity id as a cache-id. That patch already has to cleanup this pattern elsewhere in resctrl, I need to add this one to it.

That thing is a discussion for the DT folk to drive ... I think they could just as easily use the CPU number - only it wouldn't be a hardware-derived value. (the upshot is cache-ids could change over a firmware update - which I think is fine)

Thanks,

James

author reviewer
#96 lore raw

PATCH v7 38/49 x86/resctrl: Remove a newline to avoid confusing the code move script

In reply to James Morse

The resctrl filesystem code will shortly be moved to /fs/. This involves splitting all the existing files, with some functions remaining under arch/x86, and others moving to fs/resctrl.

To make this reproducible, a python script does the heavy lif^W copy-and-paste. This involves some clunky parsing of C code.

The parser gets confused by the newline after this #ifdef. Just remove it.

---
 arch/x86/kernel/cpu/resctrl/rdtgroup.c | 1 -
 1 file changed, 1 deletion(-)
arch/x86/kernel/cpu/resctrl/rdtgroup.c +0 -1
index 443635d195f0..4209d78b7aae 100644
@@ -893,7 +893,6 @@ static int rdtgroup_rmid_show(struct kernfs_open_file *of,
 }
 
 #ifdef CONFIG_PROC_CPU_RESCTRL
-
 /*
  * A task can only be part of one resctrl control group and of one monitor
  * group which is associated to that control group.
Signature · 1 line
2.39.5
Reviewed-by on v7 38/49
#97 lore raw

Re: [PATCH v7 38/49] x86/resctrl: Remove a newline to avoid confusing the code move script

Quoted 10 lines · James Morse

The resctrl filesystem code will shortly be moved to /fs/. This involves splitting all the existing files, with some functions remaining under arch/x86, and others moving to fs/resctrl.

To make this reproducible, a python script does the heavy lif^W copy-and-paste. This involves some clunky parsing of C code.

The parser gets confused by the newline after this #ifdef. Just remove it.

Quoted 6 lines

Thanks.

-Fenghua

author reviewer
#98 lore raw

PATCH v7 39/49 x86/resctrl: Split trace.h

In reply to James Morse

trace.h contains all the tracepoints. After the move to /fs/resctrl, some of these will be left behind. All the pseudo_lock tracepoints remain part of the architecture. The lone tracepoint in monitor.c moves to /fs/resctrl.

Split trace.h so that each C file includes a different trace header file. This means the trace header files are not modified when they are moved.

---
 arch/x86/kernel/cpu/resctrl/Makefile          |  3 ++
 arch/x86/kernel/cpu/resctrl/monitor.c         |  4 ++-
 arch/x86/kernel/cpu/resctrl/monitor_trace.h   | 31 +++++++++++++++++++
 arch/x86/kernel/cpu/resctrl/pseudo_lock.c     |  2 +-
 .../resctrl/{trace.h => pseudo_lock_trace.h}  | 24 +++-----------
 5 files changed, 42 insertions(+), 22 deletions(-)
 create mode 100644 arch/x86/kernel/cpu/resctrl/monitor_trace.h
 rename arch/x86/kernel/cpu/resctrl/{trace.h => pseudo_lock_trace.h} (56%)
arch/x86/kernel/cpu/resctrl/Makefile +3 -0
index 0c13b0befd8a..909be78ec6da 100644
@@ -2,4 +2,7 @@
 obj-$(CONFIG_X86_CPU_RESCTRL)		+= core.o rdtgroup.o monitor.o
 obj-$(CONFIG_X86_CPU_RESCTRL)		+= ctrlmondata.o
 obj-$(CONFIG_RESCTRL_FS_PSEUDO_LOCK)	+= pseudo_lock.o
+
+# To allow define_trace.h's recursive include:
 CFLAGS_pseudo_lock.o = -I$(src)
+CFLAGS_monitor.o = -I$(src)
arch/x86/kernel/cpu/resctrl/monitor.c +3 -1
index 5bf67b429dd3..90e7f8754b91 100644
@@ -26,7 +26,9 @@
 #include <asm/resctrl.h>
 
 #include "internal.h"
-#include "trace.h"
+
+#define CREATE_TRACE_POINTS
+#include "monitor_trace.h"
 
 /**
  * struct rmid_entry - dirty tracking for all RMID.
arch/x86/kernel/cpu/resctrl/monitor_trace.h new file +31 -0
new file mode 100644
index 000000000000..ade67daf42c2
@@ -0,0 +1,31 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#undef TRACE_SYSTEM
+#define TRACE_SYSTEM resctrl
+
+#if !defined(_FS_RESCTRL_MONITOR_TRACE_H) || defined(TRACE_HEADER_MULTI_READ)
+#define _FS_RESCTRL_MONITOR_TRACE_H
+
+#include <linux/tracepoint.h>
+
+TRACE_EVENT(mon_llc_occupancy_limbo,
+	    TP_PROTO(u32 ctrl_hw_id, u32 mon_hw_id, int domain_id, u64 llc_occupancy_bytes),
+	    TP_ARGS(ctrl_hw_id, mon_hw_id, domain_id, llc_occupancy_bytes),
+	    TP_STRUCT__entry(__field(u32, ctrl_hw_id)
+			     __field(u32, mon_hw_id)
+			     __field(int, domain_id)
+			     __field(u64, llc_occupancy_bytes)),
+	    TP_fast_assign(__entry->ctrl_hw_id = ctrl_hw_id;
+			   __entry->mon_hw_id = mon_hw_id;
+			   __entry->domain_id = domain_id;
+			   __entry->llc_occupancy_bytes = llc_occupancy_bytes;),
+	    TP_printk("ctrl_hw_id=%u mon_hw_id=%u domain_id=%d llc_occupancy_bytes=%llu",
+		      __entry->ctrl_hw_id, __entry->mon_hw_id, __entry->domain_id,
+		      __entry->llc_occupancy_bytes)
+	   );
+
+#endif /* _FS_RESCTRL_MONITOR_TRACE_H */
+
+#undef TRACE_INCLUDE_PATH
+#define TRACE_INCLUDE_PATH .
+#define TRACE_INCLUDE_FILE monitor_trace
+#include <trace/define_trace.h>
arch/x86/kernel/cpu/resctrl/pseudo_lock.c +1 -1
index 01fa7890b43f..5c14f4999d55 100644
@@ -30,7 +30,7 @@
 #include "internal.h"
 
 #define CREATE_TRACE_POINTS
-#include "trace.h"
+#include "pseudo_lock_trace.h"
 
 /*
  * The bits needed to disable hardware prefetching varies based on the
arch/x86/kernel/cpu/resctrl/pseudo_lock_trace.h renamed from arch/x86/kernel/cpu/resctrl/trace.h +4 -20
similarity index 56%
rename from arch/x86/kernel/cpu/resctrl/trace.h
rename to arch/x86/kernel/cpu/resctrl/pseudo_lock_trace.h
index 2a506316b303..5a0fae61d3ee 100644
@@ -2,8 +2,8 @@
 #undef TRACE_SYSTEM
 #define TRACE_SYSTEM resctrl
 
-#if !defined(_TRACE_RESCTRL_H) || defined(TRACE_HEADER_MULTI_READ)
-#define _TRACE_RESCTRL_H
+#if !defined(_X86_RESCTRL_PSEUDO_LOCK_TRACE_H) || defined(TRACE_HEADER_MULTI_READ)
+#define _X86_RESCTRL_PSEUDO_LOCK_TRACE_H
 
 #include <linux/tracepoint.h>
 
@@ -35,25 +35,9 @@ TRACE_EVENT(pseudo_lock_l3,
 	    TP_printk("hits=%llu miss=%llu",
 		      __entry->l3_hits, __entry->l3_miss));
 
-TRACE_EVENT(mon_llc_occupancy_limbo,
-	    TP_PROTO(u32 ctrl_hw_id, u32 mon_hw_id, int domain_id, u64 llc_occupancy_bytes),
-	    TP_ARGS(ctrl_hw_id, mon_hw_id, domain_id, llc_occupancy_bytes),
-	    TP_STRUCT__entry(__field(u32, ctrl_hw_id)
-			     __field(u32, mon_hw_id)
-			     __field(int, domain_id)
-			     __field(u64, llc_occupancy_bytes)),
-	    TP_fast_assign(__entry->ctrl_hw_id = ctrl_hw_id;
-			   __entry->mon_hw_id = mon_hw_id;
-			   __entry->domain_id = domain_id;
-			   __entry->llc_occupancy_bytes = llc_occupancy_bytes;),
-	    TP_printk("ctrl_hw_id=%u mon_hw_id=%u domain_id=%d llc_occupancy_bytes=%llu",
-		      __entry->ctrl_hw_id, __entry->mon_hw_id, __entry->domain_id,
-		      __entry->llc_occupancy_bytes)
-	   );
-
-#endif /* _TRACE_RESCTRL_H */
+#endif /* _X86_RESCTRL_PSEUDO_LOCK_TRACE_H */
 
 #undef TRACE_INCLUDE_PATH
 #define TRACE_INCLUDE_PATH .
-#define TRACE_INCLUDE_FILE trace
+#define TRACE_INCLUDE_FILE pseudo_lock_trace
 #include <trace/define_trace.h>
Signature · 1 line
2.39.5
maintainer Reviewed-by on v7 39/49
#99 lore raw

Re: [PATCH v7 39/49] x86/resctrl: Split trace.h

Hi James,

Quoted 7 lines · James Morse

trace.h contains all the tracepoints. After the move to /fs/resctrl, some of these will be left behind. All the pseudo_lock tracepoints remain part of the architecture. The lone tracepoint in monitor.c moves to /fs/resctrl.

Split trace.h so that each C file includes a different trace header file. This means the trace header files are not modified when they are moved.

Quoted 6 lines

---

Reinette

author reviewer
#100 lore raw

PATCH v7 40/49 fs/resctrl: Add boiler plate for external resctrl code

In reply to James Morse

Add Makefile and Kconfig for fs/resctrl. Add ARCH_HAS_CPU_RESCTRL for the common parts of the resctrl interface and make X86_CPU_RESCTRL select this.

Adding an include of asm/resctrl.h to linux/resctrl.h allows the /fs/resctrl files to switch over to using this header instead.

---
Changes since v6:
* Added 'depends on RESCTRL_FS' to the hidden kconfig options.

Changes since v5:
* Removed double include.
* Removed duplicate pseudo lock Kconfig define
* Grammar fix and closid capitalisation change.

Changes since v4:
* Tweaking of the commit message.

Changes since v3:
* Reworded 'if unsure say N' from the Kconfig text, the user doesn't have the choice anyway at this point.
* Added PWD to monitor.o's CFLAGS for the ftrace rube-goldberg build machine.
* Added split trace files.

Changes since v2:
* Dropped KERNFS dependency from arch side Kconfig.
* Added empty trace.h file.
* Merged asm->linux includes from Dave's patch to decouple those patches from this series.

Changes since v1:
* Rename new file psuedo_lock.c to pseudo_lock.c, to match the name of the original file (and to be less surprising).

* [Whitespace only] Under RESCTRL_FS in fs/resctrl/Kconfig, delete alignment space in orphaned select ... if (which has nothing to line up with any more).

* [Whitespace only] Reflow and re-tab Kconfig additions.

---
 MAINTAINERS                               |  1 +
 arch/Kconfig                              |  8 +++++
 arch/x86/Kconfig                          | 11 ++-----
 arch/x86/kernel/cpu/resctrl/internal.h    |  2 --
 arch/x86/kernel/cpu/resctrl/monitor.c     |  2 +-
 arch/x86/kernel/cpu/resctrl/pseudo_lock.c |  2 +-
 arch/x86/kernel/cpu/resctrl/rdtgroup.c    |  2 +-
 fs/Kconfig                                |  1 +
 fs/Makefile                               |  1 +
 fs/resctrl/Kconfig                        | 39 +++++++++++++++++++++++
 fs/resctrl/Makefile                       |  6 ++++
 fs/resctrl/ctrlmondata.c                  |  0
 fs/resctrl/internal.h                     |  0
 fs/resctrl/monitor.c                      |  0
 fs/resctrl/monitor_trace.h                |  0
 fs/resctrl/pseudo_lock.c                  |  0
 fs/resctrl/pseudo_lock_trace.h            |  0
 fs/resctrl/rdtgroup.c                     |  0
 include/linux/resctrl.h                   |  4 +++
 19 files changed, 66 insertions(+), 13 deletions(-)
 create mode 100644 fs/resctrl/Kconfig
 create mode 100644 fs/resctrl/Makefile
 create mode 100644 fs/resctrl/ctrlmondata.c
 create mode 100644 fs/resctrl/internal.h
 create mode 100644 fs/resctrl/monitor.c
 create mode 100644 fs/resctrl/monitor_trace.h
 create mode 100644 fs/resctrl/pseudo_lock.c
 create mode 100644 fs/resctrl/pseudo_lock_trace.h
 create mode 100644 fs/resctrl/rdtgroup.c
MAINTAINERS +1 -0
index 3487ac429a9a..433ba4a5a84a 100644
@@ -19881,6 +19881,7 @@ S:	Supported
 F:	Documentation/arch/x86/resctrl*
 F:	arch/x86/include/asm/resctrl.h
 F:	arch/x86/kernel/cpu/resctrl/
+F:	fs/resctrl/
 F:	include/linux/resctrl*.h
 F:	tools/testing/selftests/resctrl/
 
arch/Kconfig +8 -0
index b8a4ff365582..2778a7859c11 100644
@@ -1518,6 +1518,14 @@ config STRICT_MODULE_RWX
 config ARCH_HAS_PHYS_TO_DMA
 	bool
 
+config ARCH_HAS_CPU_RESCTRL
+	bool
+	help
+	  An architecture selects this option to indicate that the necessary
+	  hooks are provided to support the common memory system usage
+	  monitoring and control interfaces provided by the 'resctrl'
+	  filesystem (see RESCTRL_FS).
+
 config HAVE_ARCH_COMPILER_H
 	bool
 	help
arch/x86/Kconfig +3 -8
index 1a89574a58f7..ea29d22a621f 100644
@@ -503,8 +503,9 @@ config X86_MPPARSE
 config X86_CPU_RESCTRL
 	bool "x86 CPU resource control support"
 	depends on X86 && (CPU_SUP_INTEL || CPU_SUP_AMD)
-	select KERNFS
-	select PROC_CPU_RESCTRL		if PROC_FS
+	depends on MISC_FILESYSTEMS
+	select ARCH_HAS_CPU_RESCTRL
+	select RESCTRL_FS
 	select RESCTRL_FS_PSEUDO_LOCK
 	help
 	  Enable x86 CPU resource control support.
@@ -522,12 +523,6 @@ config X86_CPU_RESCTRL
 
 	  Say N if unsure.
 
-config RESCTRL_FS_PSEUDO_LOCK
-	bool
-	help
-	  Software mechanism to pin data in a cache portion using
-	  micro-architecture specific knowledge.
-
 config X86_FRED
 	bool "Flexible Return and Event Delivery"
 	depends on X86_64
arch/x86/kernel/cpu/resctrl/internal.h +0 -2
index 16c1a391d012..ee50b7375717 100644
@@ -9,8 +9,6 @@
 #include <linux/jump_label.h>
 #include <linux/tick.h>
 
-#include <asm/resctrl.h>
-
 #define L3_QOS_CDP_ENABLE		0x01ULL
 
 #define L2_QOS_CDP_ENABLE		0x01ULL
arch/x86/kernel/cpu/resctrl/monitor.c +1 -1
index 90e7f8754b91..fcd3c41db554 100644
@@ -19,11 +19,11 @@
 
 #include <linux/cpu.h>
 #include <linux/module.h>
+#include <linux/resctrl.h>
 #include <linux/sizes.h>
 #include <linux/slab.h>
 
 #include <asm/cpu_device_id.h>
-#include <asm/resctrl.h>
 
 #include "internal.h"
 
arch/x86/kernel/cpu/resctrl/pseudo_lock.c +1 -1
index 5c14f4999d55..69dff2bb2c09 100644
@@ -18,12 +18,12 @@
 #include <linux/mman.h>
 #include <linux/perf_event.h>
 #include <linux/pm_qos.h>
+#include <linux/resctrl.h>
 #include <linux/slab.h>
 #include <linux/uaccess.h>
 
 #include <asm/cacheflush.h>
 #include <asm/cpu_device_id.h>
-#include <asm/resctrl.h>
 #include <asm/perf_event.h>
 
 #include "../../events/perf_event.h" /* For X86_CONFIG() */
arch/x86/kernel/cpu/resctrl/rdtgroup.c +1 -1
index 4209d78b7aae..4108c0de298c 100644
@@ -18,6 +18,7 @@
 #include <linux/fs_parser.h>
 #include <linux/sysfs.h>
 #include <linux/kernfs.h>
+#include <linux/resctrl.h>
 #include <linux/seq_buf.h>
 #include <linux/seq_file.h>
 #include <linux/sched/signal.h>
@@ -28,7 +29,6 @@
 
 #include <uapi/linux/magic.h>
 
-#include <asm/resctrl.h>
 #include "internal.h"
 
 DEFINE_STATIC_KEY_FALSE(rdt_enable_key);
fs/Kconfig +1 -0
index 64d420e3c475..709e4d6656e2 100644
@@ -334,6 +334,7 @@ source "fs/omfs/Kconfig"
 source "fs/hpfs/Kconfig"
 source "fs/qnx4/Kconfig"
 source "fs/qnx6/Kconfig"
+source "fs/resctrl/Kconfig"
 source "fs/romfs/Kconfig"
 source "fs/pstore/Kconfig"
 source "fs/sysv/Kconfig"
fs/Makefile +1 -0
index 15df0a923d3a..73512f13e969 100644
@@ -129,3 +129,4 @@ obj-$(CONFIG_EROFS_FS)		+= erofs/
 obj-$(CONFIG_VBOXSF_FS)		+= vboxsf/
 obj-$(CONFIG_ZONEFS_FS)		+= zonefs/
 obj-$(CONFIG_BPF_LSM)		+= bpf_fs_kfuncs.o
+obj-$(CONFIG_RESCTRL_FS)	+= resctrl/
fs/resctrl/Kconfig new file +39 -0
new file mode 100644
index 000000000000..478a8e2ad99f
@@ -0,0 +1,39 @@
+config RESCTRL_FS
+	bool "CPU Resource Control Filesystem (resctrl)"
+	depends on ARCH_HAS_CPU_RESCTRL
+	select KERNFS
+	select PROC_CPU_RESCTRL if PROC_FS
+	help
+	  Some architectures provide hardware facilities to group tasks and
+	  monitor and control their usage of memory system resources such as
+	  caches and memory bandwidth.  Examples of such facilities include
+	  Intel's Resource Director Technology (Intel(R) RDT) and AMD's
+	  Platform Quality of Service (AMD QoS).
+
+	  If your system has the necessary support and you want to be able to
+	  assign tasks to groups and manipulate the associated resource
+	  monitors and controls from userspace, say Y here to get a mountable
+	  'resctrl' filesystem that lets you do just that.
+
+	  If nothing mounts or prods the 'resctrl' filesystem, resource
+	  controls and monitors are left in a quiescent, permissive state.
+
+	  On architectures where this can be disabled independently, it is
+	  safe to say N.
+
+	  See <file:Documentation/arch/x86/resctrl.rst> for more information.
+
+config RESCTRL_FS_PSEUDO_LOCK
+	bool
+	depends on RESCTRL_FS
+	help
+	  Software mechanism to pin data in a cache portion using
+	  micro-architecture specific knowledge.
+
+config RESCTRL_RMID_DEPENDS_ON_CLOSID
+	bool
+	depends on RESCTRL_FS
+	help
+	  Enabled by the architecture when the RMID values depend on the CLOSID.
+	  This causes the CLOSID allocator to search for CLOSID with clean
+	  RMID.
fs/resctrl/Makefile new file +6 -0
new file mode 100644
index 000000000000..e67f34d2236a
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0
+obj-$(CONFIG_RESCTRL_FS)		+= rdtgroup.o ctrlmondata.o monitor.o
+obj-$(CONFIG_RESCTRL_FS_PSEUDO_LOCK)	+= pseudo_lock.o
+
+# To allow define_trace.h's recursive include:
+CFLAGS_monitor.o = -I$(src)
fs/resctrl/ctrlmondata.c new file +0 -0
new file mode 100644
index 000000000000..e69de29bb2d1
fs/resctrl/internal.h new file +0 -0
new file mode 100644
index 000000000000..e69de29bb2d1
fs/resctrl/monitor.c new file +0 -0
new file mode 100644
index 000000000000..e69de29bb2d1
fs/resctrl/monitor_trace.h new file +0 -0
new file mode 100644
index 000000000000..e69de29bb2d1
fs/resctrl/pseudo_lock.c new file +0 -0
new file mode 100644
index 000000000000..e69de29bb2d1
fs/resctrl/pseudo_lock_trace.h new file +0 -0
new file mode 100644
index 000000000000..e69de29bb2d1
fs/resctrl/rdtgroup.c new file +0 -0
new file mode 100644
index 000000000000..e69de29bb2d1
include/linux/resctrl.h +4 -0
index b8f8240050b4..5c7c8bf2c47f 100644
@@ -8,6 +8,10 @@
 #include <linux/pid.h>
 #include <linux/resctrl_types.h>
 
+#ifdef CONFIG_ARCH_HAS_CPU_RESCTRL
+#include <asm/resctrl.h>
+#endif
+
 /* CLOSID, RMID value used by the default control group */
 #define RESCTRL_RESERVED_CLOSID		0
 #define RESCTRL_RESERVED_RMID		0
Signature · 1 line
2.39.5
Reviewed-by on v7 40/49
#101 lore raw

Re: [PATCH v7 40/49] fs/resctrl: Add boiler plate for external resctrl code

fs/resctrl/pseudo_lock_trace.h

Quoted 302 lines · James Morse

Add Makefile and Kconfig for fs/resctrl. Add ARCH_HAS_CPU_RESCTRL for the common parts of the resctrl interface and make X86_CPU_RESCTRL select this.

Adding an include of asm/resctrl.h to linux/resctrl.h allows the /fs/resctrl files to switch over to using this header instead.

---
Changes since v6:
* Added 'depends on RESCTRL_FS' to the hidden kconfig options.

Changes since v5:
* Removed double include.
* Removed duplicate pseudo lock Kconfig define
* Grammar fix and closid capitalisation change.

Changes since v4:
* Tweaking of the commit message.

Changes since v3:
* Reworded 'if unsure say N' from the Kconfig text, the user doesn't have the choice anyway at this point.
* Added PWD to monitor.o's CFLAGS for the ftrace rube-goldberg build machine.
* Added split trace files.

Changes since v2:
* Dropped KERNFS dependency from arch side Kconfig.
* Added empty trace.h file.
* Merged asm->linux includes from Dave's patch to decouple those patches from this series.

Changes since v1:
* Rename new file psuedo_lock.c to pseudo_lock.c, to match the name of the original file (and to be less surprising).

* [Whitespace only] Under RESCTRL_FS in fs/resctrl/Kconfig, delete alignment space in orphaned select ... if (which has nothing to line up with any more).

* [Whitespace only] Reflow and re-tab Kconfig additions.

---
  MAINTAINERS                               |  1 +
  arch/Kconfig                              |  8 +++++
  arch/x86/Kconfig                          | 11 ++-----
  arch/x86/kernel/cpu/resctrl/internal.h    |  2 --
  arch/x86/kernel/cpu/resctrl/monitor.c     |  2 +-
  arch/x86/kernel/cpu/resctrl/pseudo_lock.c |  2 +-
  arch/x86/kernel/cpu/resctrl/rdtgroup.c    |  2 +-
  fs/Kconfig                                |  1 +
  fs/Makefile                               |  1 +
  fs/resctrl/Kconfig                        | 39 +++++++++++++++++++++++
  fs/resctrl/Makefile                       |  6 ++++
  fs/resctrl/ctrlmondata.c                  |  0
  fs/resctrl/internal.h                     |  0
  fs/resctrl/monitor.c                      |  0
  fs/resctrl/monitor_trace.h                |  0
  fs/resctrl/pseudo_lock.c                  |  0
  fs/resctrl/pseudo_lock_trace.h            |  0
  fs/resctrl/rdtgroup.c                     |  0
  include/linux/resctrl.h                   |  4 +++
  19 files changed, 66 insertions(+), 13 deletions(-)

create mode 100644 fs/resctrl/Kconfig create mode 100644 fs/resctrl/Makefile create mode 100644 fs/resctrl/ctrlmondata.c create mode 100644 fs/resctrl/internal.h create mode 100644 fs/resctrl/monitor.c create mode 100644 fs/resctrl/monitor_trace.h create mode 100644 fs/resctrl/pseudo_lock.c create mode 100644 fs/resctrl/pseudo_lock_trace.h create mode 100644 fs/resctrl/rdtgroup.c

MAINTAINERS +1 -0
index 3487ac429a9a..433ba4a5a84a 100644
@@ -19881,6 +19881,7 @@ S:	Supported
  F:	Documentation/arch/x86/resctrl*
  F:	arch/x86/include/asm/resctrl.h
  F:	arch/x86/kernel/cpu/resctrl/
+F:	fs/resctrl/
  F:	include/linux/resctrl*.h
  F:	tools/testing/selftests/resctrl/
 
arch/Kconfig +8 -0
index b8a4ff365582..2778a7859c11 100644
@@ -1518,6 +1518,14 @@ config STRICT_MODULE_RWX
  config ARCH_HAS_PHYS_TO_DMA
  	bool
 
+config ARCH_HAS_CPU_RESCTRL
+	bool
+	help
+	  An architecture selects this option to indicate that the necessary
+	  hooks are provided to support the common memory system usage
+	  monitoring and control interfaces provided by the 'resctrl'
+	  filesystem (see RESCTRL_FS).
+
  config HAVE_ARCH_COMPILER_H
  	bool
  	help
arch/x86/Kconfig +3 -8
index 1a89574a58f7..ea29d22a621f 100644
@@ -503,8 +503,9 @@ config X86_MPPARSE
  config X86_CPU_RESCTRL
  	bool "x86 CPU resource control support"
  	depends on X86 && (CPU_SUP_INTEL || CPU_SUP_AMD)
-	select KERNFS
-	select PROC_CPU_RESCTRL		if PROC_FS
+	depends on MISC_FILESYSTEMS
+	select ARCH_HAS_CPU_RESCTRL
+	select RESCTRL_FS
  	select RESCTRL_FS_PSEUDO_LOCK
  	help
  	  Enable x86 CPU resource control support.
@@ -522,12 +523,6 @@ config X86_CPU_RESCTRL
 
  	  Say N if unsure.
 
-config RESCTRL_FS_PSEUDO_LOCK
-	bool
-	help
-	  Software mechanism to pin data in a cache portion using
-	  micro-architecture specific knowledge.
-
  config X86_FRED
  	bool "Flexible Return and Event Delivery"
  	depends on X86_64
arch/x86/kernel/cpu/resctrl/internal.h +0 -2
index 16c1a391d012..ee50b7375717 100644
@@ -9,8 +9,6 @@
  #include <linux/jump_label.h>
  #include <linux/tick.h>
 
-#include <asm/resctrl.h>
-
  #define L3_QOS_CDP_ENABLE		0x01ULL
 
  #define L2_QOS_CDP_ENABLE		0x01ULL
arch/x86/kernel/cpu/resctrl/monitor.c +1 -1
index 90e7f8754b91..fcd3c41db554 100644
@@ -19,11 +19,11 @@
 
  #include <linux/cpu.h>
  #include <linux/module.h>
+#include <linux/resctrl.h>
  #include <linux/sizes.h>
  #include <linux/slab.h>
 
  #include <asm/cpu_device_id.h>
-#include <asm/resctrl.h>
 
  #include "internal.h"
 
arch/x86/kernel/cpu/resctrl/pseudo_lock.c +1 -1
index 5c14f4999d55..69dff2bb2c09 100644
@@ -18,12 +18,12 @@
  #include <linux/mman.h>
  #include <linux/perf_event.h>
  #include <linux/pm_qos.h>
+#include <linux/resctrl.h>
  #include <linux/slab.h>
  #include <linux/uaccess.h>
 
  #include <asm/cacheflush.h>
  #include <asm/cpu_device_id.h>
-#include <asm/resctrl.h>
  #include <asm/perf_event.h>
 
  #include "../../events/perf_event.h" /* For X86_CONFIG() */
arch/x86/kernel/cpu/resctrl/rdtgroup.c +1 -1
index 4209d78b7aae..4108c0de298c 100644
@@ -18,6 +18,7 @@
  #include <linux/fs_parser.h>
  #include <linux/sysfs.h>
  #include <linux/kernfs.h>
+#include <linux/resctrl.h>
  #include <linux/seq_buf.h>
  #include <linux/seq_file.h>
  #include <linux/sched/signal.h>
@@ -28,7 +29,6 @@
 
  #include <uapi/linux/magic.h>
 
-#include <asm/resctrl.h>
  #include "internal.h"
 
  DEFINE_STATIC_KEY_FALSE(rdt_enable_key);
fs/Kconfig +1 -0
index 64d420e3c475..709e4d6656e2 100644
@@ -334,6 +334,7 @@ source "fs/omfs/Kconfig"
  source "fs/hpfs/Kconfig"
  source "fs/qnx4/Kconfig"
  source "fs/qnx6/Kconfig"
+source "fs/resctrl/Kconfig"
  source "fs/romfs/Kconfig"
  source "fs/pstore/Kconfig"
  source "fs/sysv/Kconfig"
fs/Makefile +1 -0
index 15df0a923d3a..73512f13e969 100644
@@ -129,3 +129,4 @@ obj-$(CONFIG_EROFS_FS)		+= erofs/
  obj-$(CONFIG_VBOXSF_FS)		+= vboxsf/
  obj-$(CONFIG_ZONEFS_FS)		+= zonefs/
  obj-$(CONFIG_BPF_LSM)		+= bpf_fs_kfuncs.o
+obj-$(CONFIG_RESCTRL_FS)	+= resctrl/
fs/resctrl/Kconfig new file +39 -0
new file mode 100644
index 000000000000..478a8e2ad99f
@@ -0,0 +1,39 @@
+config RESCTRL_FS
+	bool "CPU Resource Control Filesystem (resctrl)"
+	depends on ARCH_HAS_CPU_RESCTRL
+	select KERNFS
+	select PROC_CPU_RESCTRL if PROC_FS
+	help
+	  Some architectures provide hardware facilities to group tasks and
+	  monitor and control their usage of memory system resources such as
+	  caches and memory bandwidth.  Examples of such facilities include
+	  Intel's Resource Director Technology (Intel(R) RDT) and AMD's
+	  Platform Quality of Service (AMD QoS).
+
+	  If your system has the necessary support and you want to be able to
+	  assign tasks to groups and manipulate the associated resource
+	  monitors and controls from userspace, say Y here to get a mountable
+	  'resctrl' filesystem that lets you do just that.
+
+	  If nothing mounts or prods the 'resctrl' filesystem, resource
+	  controls and monitors are left in a quiescent, permissive state.
+
+	  On architectures where this can be disabled independently, it is
+	  safe to say N.
+
+	  See <file:Documentation/arch/x86/resctrl.rst> for more information.
+
+config RESCTRL_FS_PSEUDO_LOCK
+	bool
+	depends on RESCTRL_FS
+	help
+	  Software mechanism to pin data in a cache portion using
+	  micro-architecture specific knowledge.
+
+config RESCTRL_RMID_DEPENDS_ON_CLOSID
+	bool
+	depends on RESCTRL_FS
+	help
+	  Enabled by the architecture when the RMID values depend on the CLOSID.
+	  This causes the CLOSID allocator to search for CLOSID with clean
+	  RMID.
fs/resctrl/Makefile new file +6 -0
new file mode 100644
index 000000000000..e67f34d2236a
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0
+obj-$(CONFIG_RESCTRL_FS)		+= rdtgroup.o ctrlmondata.o monitor.o
+obj-$(CONFIG_RESCTRL_FS_PSEUDO_LOCK)	+= pseudo_lock.o
+
+# To allow define_trace.h's recursive include:
+CFLAGS_monitor.o = -I$(src)
fs/resctrl/ctrlmondata.c new file +0 -0
new file mode 100644
index 000000000000..e69de29bb2d1
fs/resctrl/internal.h new file +0 -0
new file mode 100644
index 000000000000..e69de29bb2d1
fs/resctrl/monitor.c new file +0 -0
new file mode 100644
index 000000000000..e69de29bb2d1
fs/resctrl/monitor_trace.h new file +0 -0
new file mode 100644
index 000000000000..e69de29bb2d1
fs/resctrl/pseudo_lock.c new file +0 -0
new file mode 100644
index 000000000000..e69de29bb2d1
fs/resctrl/pseudo_lock_trace.h new file +0 -0
new file mode 100644
index 000000000000..e69de29bb2d1

If merge patch 45 and 46 together to fix the pseudo_lock_trace.h compilation fatal issue, it's unnecessary to create pseudo_lock_trace.h here (and then the empty pseudo_lock_trace.h file will be deleted later in merged patch 45).

Quoted 8 lines
fs/resctrl/rdtgroup.c new file +0 -0
new file mode 100644
index 000000000000..e69de29bb2d1
include/linux/resctrl.h +0 -0
index b8f8240050b4..5c7c8bf2c47f 100644

include/linux/resctrl.h line 17

Quoted 10 lines
include/linux/resctrl.h +4 -0
@@ -8,6 +8,10 @@
  #include <linux/pid.h>
  #include <linux/resctrl_types.h>
 
+#ifdef CONFIG_ARCH_HAS_CPU_RESCTRL
+#include <asm/resctrl.h>
+#endif
+
  /* CLOSID, RMID value used by the default control group */
  #define RESCTRL_RESERVED_CLOSID		0
  #define RESCTRL_RESERVED_RMID		0

Other than that,

Thanks.

-Fenghua

author reviewer
#102 lore raw

Re: [PATCH v7 40/49] fs/resctrl: Add boiler plate for external resctrl code

Hi Fenghua,

Quoted 8 lines · Fenghua Yu
Quoted 6 lines · James Morse

Add Makefile and Kconfig for fs/resctrl. Add ARCH_HAS_CPU_RESCTRL for the common parts of the resctrl interface and make X86_CPU_RESCTRL select this.

Adding an include of asm/resctrl.h to linux/resctrl.h allows the /fs/resctrl files to switch over to using this header instead.

Quoted 7 lines

fs/resctrl/pseudo_lock_trace.h

Quoted 3 lines
fs/resctrl/pseudo_lock_trace.h new file +0 -0
new file mode 100644
index 000000000000..e69de29bb2d1

If merge patch 45 and 46 together to fix the pseudo_lock_trace.h compilation fatal issue, it's unnecessary to create pseudo_lock_trace.h here (and then the empty pseudo_lock_trace.h file will be deleted later in merged patch 45).

Sure, I 'll try and remember to note that on the cover letter for whoever choses to squash those patches together.

[..]

Quoted 3 lines

Other than that,

Thanks,

James

maintainer Reviewed-by on v7 40/49
#103 lore raw

Re: [PATCH v7 40/49] fs/resctrl: Add boiler plate for external resctrl code

In reply to James Morse

Hi James,

Quoted 7 lines · James Morse

Add Makefile and Kconfig for fs/resctrl. Add ARCH_HAS_CPU_RESCTRL for the common parts of the resctrl interface and make X86_CPU_RESCTRL select this.

Adding an include of asm/resctrl.h to linux/resctrl.h allows the /fs/resctrl files to switch over to using this header instead.

Quoted 8 lines

---

Reinette

author reviewer
#104 lore raw

PATCH v7 41/49 x86/resctrl: Move the filesystem bits to headers visible to fs/resctrl

In reply to James Morse

Once the filesystem parts of resctrl move to fs/resctrl, it cannot rely on definitions in x86's internal.h.

Move definitions in internal.h that need to be shared between the filesystem and architecture code to header files that fs/resctrl can include.

Doing this separately means the filesystem code only moves between files of the same name, instead of having these changes mixed in too.

---
Changes since v6:
* Moved resctrl_arch_set_cdp_enabled() to rdtgroup.c instead of core.c

Changes since v3:
* Changed the number of hyphens at the end of the commit message.

Changes since v2:
* Dropped the rfflags and some other defines from being moved.

Changes since v1:
* Revert apparently unintentional duplication of a couple of variable declarations in <linux/resctrl.h>.

No functional change.

---
 arch/x86/include/asm/resctrl.h         | 3 +++
 arch/x86/kernel/cpu/resctrl/internal.h | 9 ---------
 arch/x86/kernel/cpu/resctrl/rdtgroup.c | 5 +++++
 include/linux/resctrl_types.h          | 3 +++
 4 files changed, 11 insertions(+), 9 deletions(-)
arch/x86/include/asm/resctrl.h +3 -0
index 7a39728b0743..6eb7d5c94c7a 100644
@@ -210,6 +210,9 @@ int resctrl_arch_measure_l2_residency(void *_plr);
 int resctrl_arch_measure_l3_residency(void *_plr);
 void resctrl_cpu_detect(struct cpuinfo_x86 *c);
 
+bool resctrl_arch_get_cdp_enabled(enum resctrl_res_level l);
+int resctrl_arch_set_cdp_enabled(enum resctrl_res_level l, bool enable);
+
 #else
 
 static inline void resctrl_arch_sched_in(struct task_struct *tsk) {}
arch/x86/kernel/cpu/resctrl/internal.h +0 -9
index ee50b7375717..a569e31d75f6 100644
@@ -16,8 +16,6 @@
 #define CQM_LIMBOCHECK_INTERVAL	1000
 
 #define MBM_CNTR_WIDTH_BASE		24
-#define MBM_OVERFLOW_INTERVAL		1000
-#define MAX_MBA_BW			100u
 #define MBA_IS_LINEAR			0x4
 #define MBM_CNTR_WIDTH_OFFSET_AMD	20
 
@@ -403,13 +401,6 @@ extern struct rdtgroup rdtgroup_default;
 extern struct dentry *debugfs_resctrl;
 extern enum resctrl_event_id mba_mbps_default_event;
 
-static inline bool resctrl_arch_get_cdp_enabled(enum resctrl_res_level l)
-{
-	return rdt_resources_all[l].cdp_enabled;
-}
-
-int resctrl_arch_set_cdp_enabled(enum resctrl_res_level l, bool enable);
-
 void arch_mon_domain_online(struct rdt_resource *r, struct rdt_mon_domain *d);
 
 /* CPUID.(EAX=10H, ECX=ResID=1).EAX */
arch/x86/kernel/cpu/resctrl/rdtgroup.c +5 -0
index 4108c0de298c..3d90e9334b1f 100644
@@ -2520,6 +2520,11 @@ int resctrl_arch_set_cdp_enabled(enum resctrl_res_level l, bool enable)
 	return 0;
 }
 
+bool resctrl_arch_get_cdp_enabled(enum resctrl_res_level l)
+{
+	return rdt_resources_all[l].cdp_enabled;
+}
+
 /*
  * We don't allow rdtgroup directories to be created anywhere
  * except the root directory. Thus when looking for the rdtgroup
include/linux/resctrl_types.h +3 -0
index 654323066174..a7faf2cd5406 100644
@@ -7,6 +7,9 @@
 #ifndef __LINUX_RESCTRL_TYPES_H
 #define __LINUX_RESCTRL_TYPES_H
 
+#define MAX_MBA_BW			100u
+#define MBM_OVERFLOW_INTERVAL		1000
+
 /* Reads to Local DRAM Memory */
 #define READS_TO_LOCAL_MEM		BIT(0)
 
Signature · 1 line
2.39.5
Reviewed-by on v7 41/49
#105 lore raw

Re: [PATCH v7 41/49] x86/resctrl: Move the filesystem bits to headers visible to fs/resctrl

Quoted 10 lines · James Morse

Once the filesystem parts of resctrl move to fs/resctrl, it cannot rely on definitions in x86's internal.h.

Move definitions in internal.h that need to be shared between the filesystem and architecture code to header files that fs/resctrl can include.

Doing this separately means the filesystem code only moves between files of the same name, instead of having these changes mixed in too.

Quoted 7 lines

Thanks.

-Fenghua

maintainer Reviewed-by on v7 41/49
#106 lore raw

Re: [PATCH v7 41/49] x86/resctrl: Move the filesystem bits to headers visible to fs/resctrl

In reply to James Morse

Hi James,

Quoted 10 lines · James Morse

Once the filesystem parts of resctrl move to fs/resctrl, it cannot rely on definitions in x86's internal.h.

Move definitions in internal.h that need to be shared between the filesystem and architecture code to header files that fs/resctrl can include.

Doing this separately means the filesystem code only moves between files of the same name, instead of having these changes mixed in too.

Quoted 8 lines

---

Reinette

author reviewer
#107 lore raw

PATCH v7 42/49 x86/resctrl: Squelch whitespace anomalies in resctrl core code

In reply to James Morse

From: Dave Martin <[email protected]>

checkpatch.pl complains about some whitespace anomalies in the resctrl core code.

This doesn't matter, but since this code is about to be factored out and made generic, this is a good opportunity to fix these issues and so reduce future checkpatch fuzz.

Fix them.

No functional change.

---
Changes since v6:
* This patch is new.

---
 arch/x86/kernel/cpu/resctrl/rdtgroup.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
arch/x86/kernel/cpu/resctrl/rdtgroup.c +5 -7
index 3d90e9334b1f..f03432e1b1f6 100644
@@ -1003,7 +1003,7 @@ static int rdt_num_closids_show(struct kernfs_open_file *of,
 }
 
 static int rdt_default_ctrl_show(struct kernfs_open_file *of,
-			     struct seq_file *seq, void *v)
+				 struct seq_file *seq, void *v)
 {
 	struct resctrl_schema *s = of->kn->parent->priv;
 	struct rdt_resource *r = s->res;
@@ -1013,7 +1013,7 @@ static int rdt_default_ctrl_show(struct kernfs_open_file *of,
 }
 
 static int rdt_min_cbm_bits_show(struct kernfs_open_file *of,
-			     struct seq_file *seq, void *v)
+				 struct seq_file *seq, void *v)
 {
 	struct resctrl_schema *s = of->kn->parent->priv;
 	struct rdt_resource *r = s->res;
@@ -1129,7 +1129,7 @@ static int rdt_bit_usage_show(struct kernfs_open_file *of,
 }
 
 static int rdt_min_bw_show(struct kernfs_open_file *of,
-			     struct seq_file *seq, void *v)
+			   struct seq_file *seq, void *v)
 {
 	struct resctrl_schema *s = of->kn->parent->priv;
 	struct rdt_resource *r = s->res;
@@ -1164,7 +1164,7 @@ static int rdt_mon_features_show(struct kernfs_open_file *of,
 }
 
 static int rdt_bw_gran_show(struct kernfs_open_file *of,
-			     struct seq_file *seq, void *v)
+			    struct seq_file *seq, void *v)
 {
 	struct resctrl_schema *s = of->kn->parent->priv;
 	struct rdt_resource *r = s->res;
@@ -1174,7 +1174,7 @@ static int rdt_bw_gran_show(struct kernfs_open_file *of,
 }
 
 static int rdt_delay_linear_show(struct kernfs_open_file *of,
-			     struct seq_file *seq, void *v)
+				 struct seq_file *seq, void *v)
 {
 	struct resctrl_schema *s = of->kn->parent->priv;
 	struct rdt_resource *r = s->res;
@@ -2047,7 +2047,6 @@ static struct rftype res_common_files[] = {
 		.seq_show	= rdtgroup_closid_show,
 		.fflags		= RFTYPE_CTRL_BASE | RFTYPE_DEBUG,
 	},
-
 };
 
 static int rdtgroup_add_files(struct kernfs_node *kn, unsigned long fflags)
@@ -3654,7 +3653,6 @@ static int rdtgroup_init_alloc(struct rdtgroup *rdtgrp)
 			rdt_last_cmd_puts("Failed to initialize allocations\n");
 			goto out;
 		}
-
 	}
 
 	rdtgrp->mode = RDT_MODE_SHAREABLE;
Signature · 1 line
2.39.5
Reviewed-by on v7 42/49
#108 lore raw

Re: [PATCH v7 42/49] x86/resctrl: Squelch whitespace anomalies in resctrl core code

Quoted 9 lines · James Morse

From: Dave Martin <[email protected]>

checkpatch.pl complains about some whitespace anomalies in the resctrl core code.

This doesn't matter, but since this code is about to be factored out and made generic, this is a good opportunity to fix these issues and so reduce future checkpatch fuzz.

Quoted 6 lines

Fix them.

No functional change.

Thanks.

-Fenghua

maintainer Reviewed-by on v7 42/49
#109 lore raw

Re: [PATCH v7 42/49] x86/resctrl: Squelch whitespace anomalies in resctrl core code

In reply to James Morse

Hi James,

Quoted 9 lines · James Morse

From: Dave Martin <[email protected]>

checkpatch.pl complains about some whitespace anomalies in the resctrl core code.

This doesn't matter, but since this code is about to be factored out and made generic, this is a good opportunity to fix these issues and so reduce future checkpatch fuzz.

Quoted 8 lines

Fix them.

No functional change.

---

Reinette

author reviewer
#110 lore raw

PATCH v7 43/49 x86/resctrl: Prefer alloc(sizeof(*foo)) idiom in rdt_init_fs_context()

In reply to James Morse

From: Dave Martin <[email protected]>

rdt_init_fs_context() sizes a typed allocation using an explicit sizeof(type) expression, which checkpatch.pl complains about.

Since this code is about to be factored out and made generic, this is a good opportunity to fix the code to size the allocation based on the target pointer instead, to reduce the chance of future mis- maintenance.

Fix it.

No functional change.

---
Changes since v6:
* This patch is new.

---
 arch/x86/kernel/cpu/resctrl/rdtgroup.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/cpu/resctrl/rdtgroup.c +1 -1
index f03432e1b1f6..0976cee041c2 100644
@@ -2929,7 +2929,7 @@ static int rdt_init_fs_context(struct fs_context *fc)
 {
 	struct rdt_fs_context *ctx;
 
-	ctx = kzalloc(sizeof(struct rdt_fs_context), GFP_KERNEL);
+	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
 	if (!ctx)
 		return -ENOMEM;
 
Signature · 1 line
2.39.5
Reviewed-by on v7 43/49
#111 lore raw

Re: [PATCH v7 43/49] x86/resctrl: Prefer alloc(sizeof(*foo)) idiom in rdt_init_fs_context()

Quoted 10 lines · James Morse

From: Dave Martin <[email protected]>

rdt_init_fs_context() sizes a typed allocation using an explicit sizeof(type) expression, which checkpatch.pl complains about.

Since this code is about to be factored out and made generic, this is a good opportunity to fix the code to size the allocation based on the target pointer instead, to reduce the chance of future mis- maintenance.

Quoted 6 lines

Fix it.

No functional change.

Thanks.

-Fenghua

maintainer Reviewed-by on v7 43/49
#112 lore raw

Re: [PATCH v7 43/49] x86/resctrl: Prefer alloc(sizeof(*foo)) idiom in rdt_init_fs_context()

In reply to James Morse

Hi James,

Quoted 10 lines · James Morse

From: Dave Martin <[email protected]>

rdt_init_fs_context() sizes a typed allocation using an explicit sizeof(type) expression, which checkpatch.pl complains about.

Since this code is about to be factored out and made generic, this is a good opportunity to fix the code to size the allocation based on the target pointer instead, to reduce the chance of future mis- maintenance.

Quoted 7 lines

Fix it.

No functional change.

---

Reinette

author reviewer
#113 lore raw

PATCH v7 44/49 x86/resctrl: Relax some asm #includes

In reply to James Morse

From: Dave Martin <[email protected]>

checkpatch.pl identifies some direct #includes of asm headers that can be satisfied by including the corresponding <linux/...> header instead.

Fix them.

No intentional functional change.

---
Changes since v6:
* This patch is new.

Changes since v2:
* asm->linux for resctrl.h moved into an earlier patch.

---
 arch/x86/kernel/cpu/resctrl/core.c        | 10 +++++-----
 arch/x86/kernel/cpu/resctrl/pseudo_lock.c |  3 ++-
 2 files changed, 7 insertions(+), 6 deletions(-)
arch/x86/kernel/cpu/resctrl/core.c +5 -5
index 93b51d522bc7..6ed0d4f5d6a3 100644
@@ -60,7 +60,6 @@ struct rdt_hw_resource rdt_resources_all[RDT_NUM_RESOURCES] = {
 	[RDT_RESOURCE_L3] =
 	{
 		.r_resctrl = {
-			.rid			= RDT_RESOURCE_L3,
 			.name			= "L3",
 			.ctrl_scope		= RESCTRL_L3_CACHE,
 			.mon_scope		= RESCTRL_L3_CACHE,
@@ -74,7 +73,6 @@ struct rdt_hw_resource rdt_resources_all[RDT_NUM_RESOURCES] = {
 	[RDT_RESOURCE_L2] =
 	{
 		.r_resctrl = {
-			.rid			= RDT_RESOURCE_L2,
 			.name			= "L2",
 			.ctrl_scope		= RESCTRL_L2_CACHE,
 			.ctrl_domains		= ctrl_domain_init(RDT_RESOURCE_L2),
@@ -86,7 +84,6 @@ struct rdt_hw_resource rdt_resources_all[RDT_NUM_RESOURCES] = {
 	[RDT_RESOURCE_MBA] =
 	{
 		.r_resctrl = {
-			.rid			= RDT_RESOURCE_MBA,
 			.name			= "MB",
 			.ctrl_scope		= RESCTRL_L3_CACHE,
 			.ctrl_domains		= ctrl_domain_init(RDT_RESOURCE_MBA),
@@ -96,7 +93,6 @@ struct rdt_hw_resource rdt_resources_all[RDT_NUM_RESOURCES] = {
 	[RDT_RESOURCE_SMBA] =
 	{
 		.r_resctrl = {
-			.rid			= RDT_RESOURCE_SMBA,
 			.name			= "SMBA",
 			.ctrl_scope		= RESCTRL_L3_CACHE,
 			.ctrl_domains		= ctrl_domain_init(RDT_RESOURCE_SMBA),
@@ -998,7 +994,11 @@ void resctrl_cpu_detect(struct cpuinfo_x86 *c)
 static int __init resctrl_arch_late_init(void)
 {
 	struct rdt_resource *r;
-	int state, ret;
+	int state, ret, i;
+
+	/* Initialise all rid values for_each_rdt_resource() */
+	for (i = 0; i < RDT_NUM_RESOURCES; i++)
+		rdt_resources_all[i].r_resctrl.rid = i;
 
 	/*
 	 * Initialize functions(or definitions) that are different
arch/x86/kernel/cpu/resctrl/pseudo_lock.c +2 -1
index 69dff2bb2c09..5ed9ece12de0 100644
@@ -11,6 +11,8 @@
 
 #define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
 
+#include <linux/cacheflush.h>
+#include <linux/cacheinfo.h>
 #include <linux/cpu.h>
 #include <linux/cpumask.h>
 #include <linux/debugfs.h>
@@ -22,7 +24,6 @@
 #include <linux/slab.h>
 #include <linux/uaccess.h>
 
-#include <asm/cacheflush.h>
 #include <asm/cpu_device_id.h>
 #include <asm/perf_event.h>
 
Signature · 1 line
2.39.5
#114 lore raw

Re: [PATCH v7 44/49] x86/resctrl: Relax some asm #includes

Hi, James and Dave,

Quoted 9 lines · James Morse

From: Dave Martin <[email protected]>

checkpatch.pl identifies some direct #includes of asm headers that can be satisfied by including the corresponding <linux/...> header instead.

Fix them.

No intentional functional change.

The commit message doesn't mention the rid code changes? Maybe add the message or remove the rid code changes (which are not essential in this patch set any way)?

Quoted 76 lines

---
Changes since v6:
* This patch is new.

Changes since v2:
* asm->linux for resctrl.h moved into an earlier patch.

---
  arch/x86/kernel/cpu/resctrl/core.c        | 10 +++++-----
  arch/x86/kernel/cpu/resctrl/pseudo_lock.c |  3 ++-
  2 files changed, 7 insertions(+), 6 deletions(-)
arch/x86/kernel/cpu/resctrl/core.c +5 -5
index 93b51d522bc7..6ed0d4f5d6a3 100644
@@ -60,7 +60,6 @@ struct rdt_hw_resource rdt_resources_all[RDT_NUM_RESOURCES] = {
  	[RDT_RESOURCE_L3] =
  	{
  		.r_resctrl = {
-			.rid			= RDT_RESOURCE_L3,
  			.name			= "L3",
  			.ctrl_scope		= RESCTRL_L3_CACHE,
  			.mon_scope		= RESCTRL_L3_CACHE,
@@ -74,7 +73,6 @@ struct rdt_hw_resource rdt_resources_all[RDT_NUM_RESOURCES] = {
  	[RDT_RESOURCE_L2] =
  	{
  		.r_resctrl = {
-			.rid			= RDT_RESOURCE_L2,
  			.name			= "L2",
  			.ctrl_scope		= RESCTRL_L2_CACHE,
  			.ctrl_domains		= ctrl_domain_init(RDT_RESOURCE_L2),
@@ -86,7 +84,6 @@ struct rdt_hw_resource rdt_resources_all[RDT_NUM_RESOURCES] = {
  	[RDT_RESOURCE_MBA] =
  	{
  		.r_resctrl = {
-			.rid			= RDT_RESOURCE_MBA,
  			.name			= "MB",
  			.ctrl_scope		= RESCTRL_L3_CACHE,
  			.ctrl_domains		= ctrl_domain_init(RDT_RESOURCE_MBA),
@@ -96,7 +93,6 @@ struct rdt_hw_resource rdt_resources_all[RDT_NUM_RESOURCES] = {
  	[RDT_RESOURCE_SMBA] =
  	{
  		.r_resctrl = {
-			.rid			= RDT_RESOURCE_SMBA,
  			.name			= "SMBA",
  			.ctrl_scope		= RESCTRL_L3_CACHE,
  			.ctrl_domains		= ctrl_domain_init(RDT_RESOURCE_SMBA),
@@ -998,7 +994,11 @@ void resctrl_cpu_detect(struct cpuinfo_x86 *c)
  static int __init resctrl_arch_late_init(void)
  {
  	struct rdt_resource *r;
-	int state, ret;
+	int state, ret, i;
+
+	/* Initialise all rid values for_each_rdt_resource() */
+	for (i = 0; i < RDT_NUM_RESOURCES; i++)
+		rdt_resources_all[i].r_resctrl.rid = i;
 
  	/*
  	 * Initialize functions(or definitions) that are different
arch/x86/kernel/cpu/resctrl/pseudo_lock.c +2 -0
index 69dff2bb2c09..5ed9ece12de0 100644
@@ -11,6 +11,8 @@
 
  #define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
 
+#include <linux/cacheflush.h>
+#include <linux/cacheinfo.h>
  #include <linux/cpu.h>
  #include <linux/cpumask.h>
  #include <linux/debugfs.h>

arch/x86/kernel/cpu/resctrl/pseudo_lock.c line 29

Quoted 7 lines
arch/x86/kernel/cpu/resctrl/pseudo_lock.c +0 -1
@@ -22,7 +24,6 @@
  #include <linux/slab.h>
  #include <linux/uaccess.h>
 
-#include <asm/cacheflush.h>
  #include <asm/cpu_device_id.h>
  #include <asm/perf_event.h>
 

Thanks.

-Fenghua

A message that is not in the archive (sent off-list or not yet received): [email protected]

author reviewer
#116 lore raw

Re: [PATCH v7 44/49] x86/resctrl: Relax some asm #includes

Hi Fenghua,

Quoted 7 lines · Fenghua Yu
Quoted 6 lines · James Morse

From: Dave Martin<[email protected]>

checkpatch.pl identifies some direct #includes of asm headers that can be satisfied by including the corresponding <linux/...> header instead.

Quoted 5 lines
Quoted 3 lines

Fix them.

No intentional functional change.

The commit message doesn't mention the rid initialization code changes?

That looks like another patch has been squashed in here - this was something Tony identified as rid would be un-initialised if the rdt resource array grows behind x86's back.

[...]

Quoted 3 lines

Other than that,

Thanks,

James

author reviewer
#117 lore raw

PATCH v7 45/49 x86,fs/resctrl: Move the resctrl filesystem code to live in /fs/resctrl

In reply to James Morse

Resctrl is a filesystem interface to hardware that provides cache allocation policy and bandwidth control for groups of tasks or CPUs.

To support more than one architecture, resctrl needs to live in /fs/.

Move the code that is concerned with the filesystem interface to /fs/resctrl.

---
Changes since v6:
* This patch is new.

---
 arch/x86/kernel/cpu/resctrl/ctrlmondata.c     |  636 ---
 arch/x86/kernel/cpu/resctrl/internal.h        |  373 +-
 arch/x86/kernel/cpu/resctrl/monitor.c         |  905 +---
 arch/x86/kernel/cpu/resctrl/monitor_trace.h   |   18 +-
 arch/x86/kernel/cpu/resctrl/pseudo_lock.c     | 1074 +---
 .../kernel/cpu/resctrl/pseudo_lock_trace.h    |    2 +
 arch/x86/kernel/cpu/resctrl/rdtgroup.c        | 4294 +---------------
 fs/resctrl/ctrlmondata.c                      |  660 +++
 fs/resctrl/internal.h                         |  435 ++
 fs/resctrl/monitor.c                          |  935 ++++
 fs/resctrl/monitor_trace.h                    |   33 +
 fs/resctrl/pseudo_lock.c                      | 1109 +++++
 fs/resctrl/pseudo_lock_trace.h                |   17 +
 fs/resctrl/rdtgroup.c                         | 4329 +++++++++++++++++
 14 files changed, 7535 insertions(+), 7285 deletions(-)
arch/x86/kernel/cpu/resctrl/ctrlmondata.c +0 -636
index 159972c3fe73..0164d769aeca 100644
@@ -23,270 +23,6 @@
 
 #include "internal.h"
 
-struct rdt_parse_data {
-	struct rdtgroup		*rdtgrp;
-	char			*buf;
-};
-
-typedef int (ctrlval_parser_t)(struct rdt_parse_data *data,
-			       struct resctrl_schema *s,
-			       struct rdt_ctrl_domain *d);
-
-/*
- * Check whether MBA bandwidth percentage value is correct. The value is
- * checked against the minimum and max bandwidth values specified by the
- * hardware. The allocated bandwidth percentage is rounded to the next
- * control step available on the hardware.
- */
-static bool bw_validate(char *buf, u32 *data, struct rdt_resource *r)
-{
-	int ret;
-	u32 bw;
-
-	/*
-	 * Only linear delay values is supported for current Intel SKUs.
-	 */
-	if (!r->membw.delay_linear && r->membw.arch_needs_linear) {
-		rdt_last_cmd_puts("No support for non-linear MB domains\n");
-		return false;
-	}
-
-	ret = kstrtou32(buf, 10, &bw);
-	if (ret) {
-		rdt_last_cmd_printf("Invalid MB value %s\n", buf);
-		return false;
-	}
-
-	/* Nothing else to do if software controller is enabled. */
-	if (is_mba_sc(r)) {
-		*data = bw;
-		return true;
-	}
-
-	if (bw < r->membw.min_bw || bw > r->membw.max_bw) {
-		rdt_last_cmd_printf("MB value %u out of range [%d,%d]\n",
-				    bw, r->membw.min_bw, r->membw.max_bw);
-		return false;
-	}
-
-	*data = roundup(bw, (unsigned long)r->membw.bw_gran);
-	return true;
-}
-
-static int parse_bw(struct rdt_parse_data *data, struct resctrl_schema *s,
-		    struct rdt_ctrl_domain *d)
-{
-	struct resctrl_staged_config *cfg;
-	u32 closid = data->rdtgrp->closid;
-	struct rdt_resource *r = s->res;
-	u32 bw_val;
-
-	cfg = &d->staged_config[s->conf_type];
-	if (cfg->have_new_ctrl) {
-		rdt_last_cmd_printf("Duplicate domain %d\n", d->hdr.id);
-		return -EINVAL;
-	}
-
-	if (!bw_validate(data->buf, &bw_val, r))
-		return -EINVAL;
-
-	if (is_mba_sc(r)) {
-		d->mbps_val[closid] = bw_val;
-		return 0;
-	}
-
-	cfg->new_ctrl = bw_val;
-	cfg->have_new_ctrl = true;
-
-	return 0;
-}
-
-/*
- * Check whether a cache bit mask is valid.
- * On Intel CPUs, non-contiguous 1s value support is indicated by CPUID:
- *   - CPUID.0x10.1:ECX[3]: L3 non-contiguous 1s value supported if 1
- *   - CPUID.0x10.2:ECX[3]: L2 non-contiguous 1s value supported if 1
- *
- * Haswell does not support a non-contiguous 1s value and additionally
- * requires at least two bits set.
- * AMD allows non-contiguous bitmasks.
- */
-static bool cbm_validate(char *buf, u32 *data, struct rdt_resource *r)
-{
-	u32 supported_bits = BIT_MASK(r->cache.cbm_len) - 1;
-	unsigned int cbm_len = r->cache.cbm_len;
-	unsigned long first_bit, zero_bit, val;
-	int ret;
-
-	ret = kstrtoul(buf, 16, &val);
-	if (ret) {
-		rdt_last_cmd_printf("Non-hex character in the mask %s\n", buf);
-		return false;
-	}
-
-	if ((r->cache.min_cbm_bits > 0 && val == 0) || val > supported_bits) {
-		rdt_last_cmd_puts("Mask out of range\n");
-		return false;
-	}
-
-	first_bit = find_first_bit(&val, cbm_len);
-	zero_bit = find_next_zero_bit(&val, cbm_len, first_bit);
-
-	/* Are non-contiguous bitmasks allowed? */
-	if (!r->cache.arch_has_sparse_bitmasks &&
-	    (find_next_bit(&val, cbm_len, zero_bit) < cbm_len)) {
-		rdt_last_cmd_printf("The mask %lx has non-consecutive 1-bits\n", val);
-		return false;
-	}
-
-	if ((zero_bit - first_bit) < r->cache.min_cbm_bits) {
-		rdt_last_cmd_printf("Need at least %d bits in the mask\n",
-				    r->cache.min_cbm_bits);
-		return false;
-	}
-
-	*data = val;
-	return true;
-}
-
-/*
- * Read one cache bit mask (hex). Check that it is valid for the current
- * resource type.
- */
-static int parse_cbm(struct rdt_parse_data *data, struct resctrl_schema *s,
-		     struct rdt_ctrl_domain *d)
-{
-	struct rdtgroup *rdtgrp = data->rdtgrp;
-	struct resctrl_staged_config *cfg;
-	struct rdt_resource *r = s->res;
-	u32 cbm_val;
-
-	cfg = &d->staged_config[s->conf_type];
-	if (cfg->have_new_ctrl) {
-		rdt_last_cmd_printf("Duplicate domain %d\n", d->hdr.id);
-		return -EINVAL;
-	}
-
-	/*
-	 * Cannot set up more than one pseudo-locked region in a cache
-	 * hierarchy.
-	 */
-	if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP &&
-	    rdtgroup_pseudo_locked_in_hierarchy(d)) {
-		rdt_last_cmd_puts("Pseudo-locked region in hierarchy\n");
-		return -EINVAL;
-	}
-
-	if (!cbm_validate(data->buf, &cbm_val, r))
-		return -EINVAL;
-
-	if ((rdtgrp->mode == RDT_MODE_EXCLUSIVE ||
-	     rdtgrp->mode == RDT_MODE_SHAREABLE) &&
-	    rdtgroup_cbm_overlaps_pseudo_locked(d, cbm_val)) {
-		rdt_last_cmd_puts("CBM overlaps with pseudo-locked region\n");
-		return -EINVAL;
-	}
-
-	/*
-	 * The CBM may not overlap with the CBM of another closid if
-	 * either is exclusive.
-	 */
-	if (rdtgroup_cbm_overlaps(s, d, cbm_val, rdtgrp->closid, true)) {
-		rdt_last_cmd_puts("Overlaps with exclusive group\n");
-		return -EINVAL;
-	}
-
-	if (rdtgroup_cbm_overlaps(s, d, cbm_val, rdtgrp->closid, false)) {
-		if (rdtgrp->mode == RDT_MODE_EXCLUSIVE ||
-		    rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
-			rdt_last_cmd_puts("Overlaps with other group\n");
-			return -EINVAL;
-		}
-	}
-
-	cfg->new_ctrl = cbm_val;
-	cfg->have_new_ctrl = true;
-
-	return 0;
-}
-
-/*
- * For each domain in this resource we expect to find a series of:
- *	id=mask
- * separated by ";". The "id" is in decimal, and must match one of
- * the "id"s for this resource.
- */
-static int parse_line(char *line, struct resctrl_schema *s,
-		      struct rdtgroup *rdtgrp)
-{
-	enum resctrl_conf_type t = s->conf_type;
-	ctrlval_parser_t *parse_ctrlval = NULL;
-	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;
-	unsigned long dom_id;
-
-	/* Walking r->domains, ensure it can't race with cpuhp */
-	lockdep_assert_cpus_held();
-
-	switch (r->schema_fmt) {
-	case RESCTRL_SCHEMA_BITMAP:
-		parse_ctrlval = &parse_cbm;
-		break;
-	case RESCTRL_SCHEMA_RANGE:
-		parse_ctrlval = &parse_bw;
-		break;
-	}
-
-	if (WARN_ON_ONCE(!parse_ctrlval))
-		return -EINVAL;
-
-	if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP &&
-	    (r->rid == RDT_RESOURCE_MBA || r->rid == RDT_RESOURCE_SMBA)) {
-		rdt_last_cmd_puts("Cannot pseudo-lock MBA resource\n");
-		return -EINVAL;
-	}
-
-next:
-	if (!line || line[0] == '\0')
-		return 0;
-	dom = strsep(&line, ";");
-	id = strsep(&dom, "=");
-	if (!dom || kstrtoul(id, 10, &dom_id)) {
-		rdt_last_cmd_puts("Missing '=' or non-numeric domain\n");
-		return -EINVAL;
-	}
-	dom = strim(dom);
-	list_for_each_entry(d, &r->ctrl_domains, hdr.list) {
-		if (d->hdr.id == dom_id) {
-			data.buf = dom;
-			data.rdtgrp = rdtgrp;
-			if (parse_ctrlval(&data, s, d))
-				return -EINVAL;
-			if (rdtgrp->mode ==  RDT_MODE_PSEUDO_LOCKSETUP) {
-				cfg = &d->staged_config[t];
-				/*
-				 * In pseudo-locking setup mode and just
-				 * parsed a valid CBM that should be
-				 * pseudo-locked. Only one locked region per
-				 * resource group and domain so just do
-				 * the required initialization for single
-				 * region and return.
-				 */
-				rdtgrp->plr->s = s;
-				rdtgrp->plr->d = d;
-				rdtgrp->plr->cbm = cfg->new_ctrl;
-				d->plr = rdtgrp->plr;
-				return 0;
-			}
-			goto next;
-		}
-	}
-	return -EINVAL;
-}
-
 int resctrl_arch_update_one(struct rdt_resource *r, struct rdt_ctrl_domain *d,
 			    u32 closid, enum resctrl_conf_type t, u32 cfg_val)
 {
@@ -351,100 +87,6 @@ int resctrl_arch_update_domains(struct rdt_resource *r, u32 closid)
 	return 0;
 }
 
-static int rdtgroup_parse_resource(char *resname, char *tok,
-				   struct rdtgroup *rdtgrp)
-{
-	struct resctrl_schema *s;
-
-	list_for_each_entry(s, &resctrl_schema_all, list) {
-		if (!strcmp(resname, s->name) && rdtgrp->closid < s->num_closid)
-			return parse_line(tok, s, rdtgrp);
-	}
-	rdt_last_cmd_printf("Unknown or unsupported resource name '%s'\n", resname);
-	return -EINVAL;
-}
-
-ssize_t rdtgroup_schemata_write(struct kernfs_open_file *of,
-				char *buf, size_t nbytes, loff_t off)
-{
-	struct resctrl_schema *s;
-	struct rdtgroup *rdtgrp;
-	struct rdt_resource *r;
-	char *tok, *resname;
-	int ret = 0;
-
-	/* Valid input requires a trailing newline */
-	if (nbytes == 0 || buf[nbytes - 1] != '\n')
-		return -EINVAL;
-	buf[nbytes - 1] = '\0';
-
-	rdtgrp = rdtgroup_kn_lock_live(of->kn);
-	if (!rdtgrp) {
-		rdtgroup_kn_unlock(of->kn);
-		return -ENOENT;
-	}
-	rdt_last_cmd_clear();
-
-	/*
-	 * No changes to pseudo-locked region allowed. It has to be removed
-	 * and re-created instead.
-	 */
-	if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
-		ret = -EINVAL;
-		rdt_last_cmd_puts("Resource group is pseudo-locked\n");
-		goto out;
-	}
-
-	rdt_staged_configs_clear();
-
-	while ((tok = strsep(&buf, "\n")) != NULL) {
-		resname = strim(strsep(&tok, ":"));
-		if (!tok) {
-			rdt_last_cmd_puts("Missing ':'\n");
-			ret = -EINVAL;
-			goto out;
-		}
-		if (tok[0] == '\0') {
-			rdt_last_cmd_printf("Missing '%s' value\n", resname);
-			ret = -EINVAL;
-			goto out;
-		}
-		ret = rdtgroup_parse_resource(resname, tok, rdtgrp);
-		if (ret)
-			goto out;
-	}
-
-	list_for_each_entry(s, &resctrl_schema_all, list) {
-		r = s->res;
-
-		/*
-		 * Writes to mba_sc resources update the software controller,
-		 * not the control MSR.
-		 */
-		if (is_mba_sc(r))
-			continue;
-
-		ret = resctrl_arch_update_domains(r, rdtgrp->closid);
-		if (ret)
-			goto out;
-	}
-
-	if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
-		/*
-		 * If pseudo-locking fails we keep the resource group in
-		 * mode RDT_MODE_PSEUDO_LOCKSETUP with its class of service
-		 * active and updated for just the domain the pseudo-locked
-		 * region was requested for.
-		 */
-		ret = rdtgroup_pseudo_lock_create(rdtgrp);
-	}
-
-out:
-	rdt_staged_configs_clear();
-	rdtgroup_kn_unlock(of->kn);
-	return ret ?: nbytes;
-}
-
 u32 resctrl_arch_get_config(struct rdt_resource *r, struct rdt_ctrl_domain *d,
 			    u32 closid, enum resctrl_conf_type type)
 {
@@ -453,281 +95,3 @@ u32 resctrl_arch_get_config(struct rdt_resource *r, struct rdt_ctrl_domain *d,
 
 	return hw_dom->ctrl_val[idx];
 }
-
-static void show_doms(struct seq_file *s, struct resctrl_schema *schema, int closid)
-{
-	struct rdt_resource *r = schema->res;
-	struct rdt_ctrl_domain *dom;
-	bool sep = false;
-	u32 ctrl_val;
-
-	/* Walking r->domains, ensure it can't race with cpuhp */
-	lockdep_assert_cpus_held();
-
-	seq_printf(s, "%*s:", max_name_width, schema->name);
-	list_for_each_entry(dom, &r->ctrl_domains, hdr.list) {
-		if (sep)
-			seq_puts(s, ";");
-
-		if (is_mba_sc(r))
-			ctrl_val = dom->mbps_val[closid];
-		else
-			ctrl_val = resctrl_arch_get_config(r, dom, closid,
-							   schema->conf_type);
-
-		seq_printf(s, schema->fmt_str, dom->hdr.id, ctrl_val);
-		sep = true;
-	}
-	seq_puts(s, "\n");
-}
-
-int rdtgroup_schemata_show(struct kernfs_open_file *of,
-			   struct seq_file *s, void *v)
-{
-	struct resctrl_schema *schema;
-	struct rdtgroup *rdtgrp;
-	int ret = 0;
-	u32 closid;
-
-	rdtgrp = rdtgroup_kn_lock_live(of->kn);
-	if (rdtgrp) {
-		if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
-			list_for_each_entry(schema, &resctrl_schema_all, list) {
-				seq_printf(s, "%s:uninitialized\n", schema->name);
-			}
-		} else if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
-			if (!rdtgrp->plr->d) {
-				rdt_last_cmd_clear();
-				rdt_last_cmd_puts("Cache domain offline\n");
-				ret = -ENODEV;
-			} else {
-				seq_printf(s, "%s:%d=%x\n",
-					   rdtgrp->plr->s->res->name,
-					   rdtgrp->plr->d->hdr.id,
-					   rdtgrp->plr->cbm);
-			}
-		} else {
-			closid = rdtgrp->closid;
-			list_for_each_entry(schema, &resctrl_schema_all, list) {
-				if (closid < schema->num_closid)
-					show_doms(s, schema, closid);
-			}
-		}
-	} else {
-		ret = -ENOENT;
-	}
-	rdtgroup_kn_unlock(of->kn);
-	return ret;
-}
-
-static int smp_mon_event_count(void *arg)
-{
-	mon_event_count(arg);
-
-	return 0;
-}
-
-ssize_t rdtgroup_mba_mbps_event_write(struct kernfs_open_file *of,
-				      char *buf, size_t nbytes, loff_t off)
-{
-	struct rdtgroup *rdtgrp;
-	int ret = 0;
-
-	/* Valid input requires a trailing newline */
-	if (nbytes == 0 || buf[nbytes - 1] != '\n')
-		return -EINVAL;
-	buf[nbytes - 1] = '\0';
-
-	rdtgrp = rdtgroup_kn_lock_live(of->kn);
-	if (!rdtgrp) {
-		rdtgroup_kn_unlock(of->kn);
-		return -ENOENT;
-	}
-	rdt_last_cmd_clear();
-
-	if (!strcmp(buf, "mbm_local_bytes")) {
-		if (resctrl_arch_is_mbm_local_enabled())
-			rdtgrp->mba_mbps_event = QOS_L3_MBM_LOCAL_EVENT_ID;
-		else
-			ret = -EINVAL;
-	} else if (!strcmp(buf, "mbm_total_bytes")) {
-		if (resctrl_arch_is_mbm_total_enabled())
-			rdtgrp->mba_mbps_event = QOS_L3_MBM_TOTAL_EVENT_ID;
-		else
-			ret = -EINVAL;
-	} else {
-		ret = -EINVAL;
-	}
-
-	if (ret)
-		rdt_last_cmd_printf("Unsupported event id '%s'\n", buf);
-
-	rdtgroup_kn_unlock(of->kn);
-
-	return ret ?: nbytes;
-}
-
-int rdtgroup_mba_mbps_event_show(struct kernfs_open_file *of,
-				 struct seq_file *s, void *v)
-{
-	struct rdtgroup *rdtgrp;
-	int ret = 0;
-
-	rdtgrp = rdtgroup_kn_lock_live(of->kn);
-
-	if (rdtgrp) {
-		switch (rdtgrp->mba_mbps_event) {
-		case QOS_L3_MBM_LOCAL_EVENT_ID:
-			seq_puts(s, "mbm_local_bytes\n");
-			break;
-		case QOS_L3_MBM_TOTAL_EVENT_ID:
-			seq_puts(s, "mbm_total_bytes\n");
-			break;
-		default:
-			pr_warn_once("Bad event %d\n", rdtgrp->mba_mbps_event);
-			ret = -EINVAL;
-			break;
-		}
-	} else {
-		ret = -ENOENT;
-	}
-
-	rdtgroup_kn_unlock(of->kn);
-
-	return ret;
-}
-
-struct rdt_domain_hdr *resctrl_find_domain(struct list_head *h, int id,
-					   struct list_head **pos)
-{
-	struct rdt_domain_hdr *d;
-	struct list_head *l;
-
-	list_for_each(l, h) {
-		d = list_entry(l, struct rdt_domain_hdr, list);
-		/* When id is found, return its domain. */
-		if (id == d->id)
-			return d;
-		/* Stop searching when finding id's position in sorted list. */
-		if (id < d->id)
-			break;
-	}
-
-	if (pos)
-		*pos = l;
-
-	return NULL;
-}
-
-void mon_event_read(struct rmid_read *rr, struct rdt_resource *r,
-		    struct rdt_mon_domain *d, struct rdtgroup *rdtgrp,
-		    cpumask_t *cpumask, int evtid, int first)
-{
-	int cpu;
-
-	/* When picking a CPU from cpu_mask, ensure it can't race with cpuhp */
-	lockdep_assert_cpus_held();
-
-	/*
-	 * Setup the parameters to pass to mon_event_count() to read the data.
-	 */
-	rr->rgrp = rdtgrp;
-	rr->evtid = evtid;
-	rr->r = r;
-	rr->d = d;
-	rr->first = first;
-	rr->arch_mon_ctx = resctrl_arch_mon_ctx_alloc(r, evtid);
-	if (IS_ERR(rr->arch_mon_ctx)) {
-		rr->err = -EINVAL;
-		return;
-	}
-
-	cpu = cpumask_any_housekeeping(cpumask, RESCTRL_PICK_ANY_CPU);
-
-	/*
-	 * cpumask_any_housekeeping() prefers housekeeping CPUs, but
-	 * are all the CPUs nohz_full? If yes, pick a CPU to IPI.
-	 * MPAM's resctrl_arch_rmid_read() is unable to read the
-	 * counters on some platforms if its called in IRQ context.
-	 */
-	if (tick_nohz_full_cpu(cpu))
-		smp_call_function_any(cpumask, mon_event_count, rr, 1);
-	else
-		smp_call_on_cpu(cpu, smp_mon_event_count, rr, false);
-
-	resctrl_arch_mon_ctx_free(r, evtid, rr->arch_mon_ctx);
-}
-
-int rdtgroup_mondata_show(struct seq_file *m, void *arg)
-{
-	struct kernfs_open_file *of = m->private;
-	struct rdt_domain_hdr *hdr;
-	struct rmid_read rr = {0};
-	struct rdt_mon_domain *d;
-	u32 resid, evtid, domid;
-	struct rdtgroup *rdtgrp;
-	struct rdt_resource *r;
-	struct mon_data *md;
-	int ret = 0;
-
-	rdtgrp = rdtgroup_kn_lock_live(of->kn);
-	if (!rdtgrp) {
-		ret = -ENOENT;
-		goto out;
-	}
-
-	md = of->kn->priv;
-	if (WARN_ON_ONCE(!md)) {
-		ret = -EIO;
-		goto out;
-	}
-
-	resid = md->rid;
-	domid = md->domid;
-	evtid = md->evtid;
-	r = resctrl_arch_get_resource(resid);
-
-	if (md->sum) {
-		/*
-		 * This file requires summing across all domains that share
-		 * the L3 cache id that was provided in the "domid" field of the
-		 * struct mon_data. Search all domains in the resource for
-		 * one that matches this cache id.
-		 */
-		list_for_each_entry(d, &r->mon_domains, hdr.list) {
-			if (d->ci->id == domid) {
-				rr.ci = d->ci;
-				mon_event_read(&rr, r, NULL, rdtgrp,
-					       &d->ci->shared_cpu_map, evtid, false);
-				goto checkresult;
-			}
-		}
-		ret = -ENOENT;
-		goto out;
-	} else {
-		/*
-		 * This file provides data from a single domain. Search
-		 * the resource to find the domain with "domid".
-		 */
-		hdr = resctrl_find_domain(&r->mon_domains, domid, NULL);
-		if (!hdr || WARN_ON_ONCE(hdr->type != RESCTRL_MON_DOMAIN)) {
-			ret = -ENOENT;
-			goto out;
-		}
-		d = container_of(hdr, struct rdt_mon_domain, hdr);
-		mon_event_read(&rr, r, d, rdtgrp, &d->hdr.cpu_mask, evtid, false);
-	}
-
-checkresult:
-
-	if (rr.err == -EIO)
-		seq_puts(m, "Error\n");
-	else if (rr.err == -EINVAL)
-		seq_puts(m, "Unavailable\n");
-	else
-		seq_printf(m, "%llu\n", rr.val);
-
-out:
-	rdtgroup_kn_unlock(of->kn);
-	return ret;
-}
arch/x86/kernel/cpu/resctrl/internal.h +8 -365

408 lines not on this page · raw message

arch/x86/kernel/cpu/resctrl/monitor.c +2 -903

949 lines not on this page · raw message

arch/x86/kernel/cpu/resctrl/monitor_trace.h +2 -16
index ade67daf42c2..b5a142dd0f0e 100644
@@ -7,25 +7,11 @@
 
 #include <linux/tracepoint.h>
 
-TRACE_EVENT(mon_llc_occupancy_limbo,
-	    TP_PROTO(u32 ctrl_hw_id, u32 mon_hw_id, int domain_id, u64 llc_occupancy_bytes),
-	    TP_ARGS(ctrl_hw_id, mon_hw_id, domain_id, llc_occupancy_bytes),
-	    TP_STRUCT__entry(__field(u32, ctrl_hw_id)
-			     __field(u32, mon_hw_id)
-			     __field(int, domain_id)
-			     __field(u64, llc_occupancy_bytes)),
-	    TP_fast_assign(__entry->ctrl_hw_id = ctrl_hw_id;
-			   __entry->mon_hw_id = mon_hw_id;
-			   __entry->domain_id = domain_id;
-			   __entry->llc_occupancy_bytes = llc_occupancy_bytes;),
-	    TP_printk("ctrl_hw_id=%u mon_hw_id=%u domain_id=%d llc_occupancy_bytes=%llu",
-		      __entry->ctrl_hw_id, __entry->mon_hw_id, __entry->domain_id,
-		      __entry->llc_occupancy_bytes)
-	   );
-
 #endif /* _FS_RESCTRL_MONITOR_TRACE_H */
 
 #undef TRACE_INCLUDE_PATH
 #define TRACE_INCLUDE_PATH .
+
 #define TRACE_INCLUDE_FILE monitor_trace
+
 #include <trace/define_trace.h>
arch/x86/kernel/cpu/resctrl/pseudo_lock.c +1 -1073

965 lines not on this page · raw message

arch/x86/kernel/cpu/resctrl/pseudo_lock_trace.h +2 -0
index 5a0fae61d3ee..7c8aef08010f 100644

Lines of this file are not shown. View the raw message.

arch/x86/kernel/cpu/resctrl/rdtgroup.c +2 -4292

4,294 lines not on this page · raw message

fs/resctrl/ctrlmondata.c +660 -0

660 lines not on this page · raw message

fs/resctrl/internal.h +435 -0

435 lines not on this page · raw message

fs/resctrl/monitor.c +935 -0

935 lines not on this page · raw message

fs/resctrl/monitor_trace.h +33 -0
index e69de29bb2d1..fdf49f22576a 100644

Lines of this file are not shown. View the raw message.

fs/resctrl/pseudo_lock.c +1109 -0

1,109 lines not on this page · raw message

fs/resctrl/pseudo_lock_trace.h +17 -0
index e69de29bb2d1..7a6a1983953a 100644

Lines of this file are not shown. View the raw message.

fs/resctrl/rdtgroup.c +4329 -0

4,329 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.39.5

Part of the diff is not shown here. View the raw message.