[PATCH 05/23] resctrl: Add nested resource_schemata support for emulated controls

Fenghua Yu fenghuay at nvidia.com
Thu Jul 16 14:02:55 PDT 2026


Some architectures expose an alternate, hardware-native control that
emulates a legacy control (for example a node-scoped bandwidth control
emulating the L3-shaped MB control). Represent this relationship with a
new resctrl_ctrl::emulated_by pointer stored on the emulated (default)
control, pointing at the control that emulates it, and add a
RESCTRL_CTRL_NAME_NODE control name for the node-scoped variant.

Build the resource_schemata tree accordingly: create the default control
directory first, then nest each emulating control beneath the default
control it emulates, while non-emulated controls remain directly under
resource_schemata. Creating the default directory up front makes the
layout independent of the order controls appear in the list.

Architecture backends set emulated_by during control init; until then the
nesting branch is never taken and all controls remain directly under
resource_schemata.

Factor the per-control directory creation into
resctrl_ctrl_create_subdir() and fix its error and ownership handling so
the newly created directory (not its parent) is validated and gets its
uid/gid applied.

Signed-off-by: Fenghua Yu <fenghuay at nvidia.com>
---
 arch/x86/kernel/cpu/resctrl/core.c |  2 +
 fs/resctrl/ctrlmondata.c           |  1 +
 fs/resctrl/rdtgroup.c              | 67 ++++++++++++++++++++++++------
 include/linux/resctrl.h            |  7 +++-
 4 files changed, 63 insertions(+), 14 deletions(-)

diff --git a/arch/x86/kernel/cpu/resctrl/core.c b/arch/x86/kernel/cpu/resctrl/core.c
index 07c6ebb7fc43..5575a16caeab 100644
--- a/arch/x86/kernel/cpu/resctrl/core.c
+++ b/arch/x86/kernel/cpu/resctrl/core.c
@@ -214,6 +214,8 @@ static __init bool __temporary_multiple_mba_intel_controls(struct rdt_resource *
 	case RESCTRL_CTRL_NAME_MAX:
 		hw_ctrl->msr_update = update_temporary_max;
 		break;
+	default:
+		break;
 	}
 
 	return true;
diff --git a/fs/resctrl/ctrlmondata.c b/fs/resctrl/ctrlmondata.c
index d95ab8ad36e2..1f832a838115 100644
--- a/fs/resctrl/ctrlmondata.c
+++ b/fs/resctrl/ctrlmondata.c
@@ -304,6 +304,7 @@ static const char * const resctrl_ctrl_name[] = {
 	[RESCTRL_CTRL_NAME_DEF]		= "",
 	[RESCTRL_CTRL_NAME_MIN]		= "MIN",
 	[RESCTRL_CTRL_NAME_MAX]		= "MAX",
+	[RESCTRL_CTRL_NAME_NODE]	= "NODE",
 };
 
 const char *resctrl_ctrl_name_str(enum resctrl_ctrl_name name)
diff --git a/fs/resctrl/rdtgroup.c b/fs/resctrl/rdtgroup.c
index a3aad9b1ff6e..3bec23728847 100644
--- a/fs/resctrl/rdtgroup.c
+++ b/fs/resctrl/rdtgroup.c
@@ -2781,6 +2781,26 @@ static int resctrl_ctrl_add_files(struct kernfs_node *kn)
 	return ret;
 }
 
+static int resctrl_ctrl_create_subdir(struct kernfs_node *kn_dir,
+				      struct rdt_resource_final *f,
+				      struct resctrl_ctrl *ctrl, struct kernfs_node **kn_ctrl)
+{
+	char ctrl_full_name[20];
+	int ret;
+
+	ret = snprintf(ctrl_full_name, sizeof(ctrl_full_name), "%s%s%s",
+		       f->name, resctrl_ctrl_is_default(ctrl) ? "" : "_",
+		       resctrl_ctrl_is_default(ctrl) ? "" : resctrl_ctrl_name_str(ctrl->name));
+	if (ret >= sizeof(ctrl_full_name))
+		return -ENOSPC;
+
+	*kn_ctrl = kernfs_create_dir(kn_dir, ctrl_full_name, kn_dir->mode, ctrl);
+	if (IS_ERR(*kn_ctrl))
+		return PTR_ERR(*kn_ctrl);
+
+	return rdtgroup_kn_set_ugid(*kn_ctrl);
+}
+
 /*
  * No need to cleanup on exit - caller calls the recursive kernfs_remove()
  * on failure.
@@ -2788,10 +2808,9 @@ static int resctrl_ctrl_add_files(struct kernfs_node *kn)
 static int resctrl_mkdir_schemata_dir(struct kernfs_node *kn,
 				      struct rdt_resource_final *f)
 {
-	struct kernfs_node *kn_subdir, *kn_ctrl;
+	struct kernfs_node *kn_subdir, *kn_ctrl, *kn_ctrl_def = NULL;
+	struct resctrl_ctrl *ctrl, *ctrl_def = NULL;
 	struct rdt_resource *r = f->res;
-	struct resctrl_ctrl *ctrl;
-	char ctrl_full_name[20];
 	int ret;
 
 	kn_subdir = kernfs_create_dir(kn, "resource_schemata", kn->mode, f);
@@ -2808,19 +2827,41 @@ static int resctrl_mkdir_schemata_dir(struct kernfs_node *kn,
 			return ret;
 	}
 
+	/*
+	 * Create the default control sub-dir first. Emulated controls are
+	 * nested underneath it and may be iterated before it, so it has to
+	 * exist before the rest of the controls are created.
+	 */
 	for_each_resource_ctrl(ctrl, f->res) {
-		ret = snprintf(ctrl_full_name, sizeof(ctrl_full_name), "%s%s%s",
-			       f->name, resctrl_ctrl_is_default(ctrl) ? "" : "_",
-			       resctrl_ctrl_is_default(ctrl) ? "" : resctrl_ctrl_name_str(ctrl->name));
-		if (ret >= sizeof(ctrl_full_name))
-			return -ENOSPC;
+		if (ctrl->name != RESCTRL_CTRL_NAME_DEF)
+			continue;
 
-		kn_ctrl = kernfs_create_dir(kn_subdir, ctrl_full_name, kn_subdir->mode,
-					    ctrl);
-		if (IS_ERR(kn_ctrl))
-			return PTR_ERR(kn_ctrl);
+		ret = resctrl_ctrl_create_subdir(kn_subdir, f, ctrl, &kn_ctrl_def);
+		if (ret)
+			return ret;
+		ret = resctrl_add_ctrl_files(kn_ctrl_def, ctrl);
+		if (ret)
+			return ret;
+		ctrl_def = ctrl;
+		break;
+	}
 
-		ret = rdtgroup_kn_set_ugid(kn_ctrl);
+	for_each_resource_ctrl(ctrl, f->res) {
+		if (ctrl->name == RESCTRL_CTRL_NAME_DEF)
+			continue;
+
+		/*
+		 * In legacy mode the emulating control is nested under the
+		 * default control it emulates (the default control's
+		 * emulated_by points at it). In native mode there is no
+		 * emulation, so every control sits directly under
+		 * resource_schemata.
+		 */
+		if (r->mode == RESCTRL_CTRL_LEGACY &&
+		    ctrl_def && ctrl_def->emulated_by == ctrl)
+			ret = resctrl_ctrl_create_subdir(kn_ctrl_def, f, ctrl, &kn_ctrl);
+		else
+			ret = resctrl_ctrl_create_subdir(kn_subdir, f, ctrl, &kn_ctrl);
 		if (ret)
 			return ret;
 
diff --git a/include/linux/resctrl.h b/include/linux/resctrl.h
index 5d8ea12ae16b..06482e766087 100644
--- a/include/linux/resctrl.h
+++ b/include/linux/resctrl.h
@@ -362,12 +362,14 @@ struct resctrl_mon {
  *				has the same name as the resource.
  * @RESCTRL_CTRL_NAME_MIN:	"MIN"
  * @RESCTRL_CTRL_NAME_MAX:	"MAX"
+ * @RESCTRL_CTRL_NAME_NODE:	"NODE"
  */
 enum resctrl_ctrl_name {
 	RESCTRL_CTRL_NAME_DEF,
 	RESCTRL_CTRL_NAME_MIN,
 	RESCTRL_CTRL_NAME_MAX,
-	RESCTRL_CTRL_NAME_LAST = RESCTRL_CTRL_NAME_MAX
+	RESCTRL_CTRL_NAME_NODE,
+	RESCTRL_CTRL_NAME_LAST = RESCTRL_CTRL_NAME_NODE
 };
 
 /**
@@ -383,6 +385,8 @@ enum resctrl_ctrl_name {
  *		Specifically, "rdt_resource_final::name"_"resctrl_ctrl::name".
  *		For example, with resource name "MB" and control name "MAX" the
  *		schema entry will be "MB_MAX".
+ * @emulated_by: For an emulated control, points at the control that emulates
+ *		it; NULL if this control is not emulated.
  * @cache:	Cache allocation control properties.
  * @membw:	Bandwidth control properties.
  */
@@ -392,6 +396,7 @@ struct resctrl_ctrl {
 	struct list_head	domains;
 	enum resctrl_ctrl_type	type;
 	enum resctrl_ctrl_name	name;
+	struct resctrl_ctrl	*emulated_by;
 	union {
 		struct resctrl_cache	cache;
 		struct resctrl_membw	membw;
-- 
2.43.0




More information about the linux-arm-kernel mailing list