Skip to contentSkip to conversation

x86,fs/resctrl: Fix long-standing issues

PATCH Superseded Reinette Chatre 6 participants 40 messages May 22 – Jun 1

Subsystems (3)

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Reinette Chatre

author maintainer
#0 lore raw

v3 cover x86,fs/resctrl: Fix long-standing issues

v2: https://lore.kernel.org/lkml/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/

While reviewing the AET series [1] Sashiko reported a deadlock during mount, and a use-after-free when an L3 domain is removed during CPU offline. Reinette found a memory leak in the mount error path while refactoring code for a solution to the mount hang.

During review of V1 of this series Sashiko found a new UAF on unmount issue that was fixed in V2.

During review of V2 Sashiko uncovered a couple more new issues: TOCTOU involving rdtgroup_kn_put() that may lead to UAF or double-free, double free of pseudo-locked regions, potential deadlock between resctrl unmount and info file readers. Sashiko also found that the CPU offline fix in V2 is flawed in its use of is_percpu_thread().

Address all issues identified. This version is significantly different from V2 because of the additional fixes and reworking of the CPU offline fix. I do not consider this version quite "polished" but after all changes made to address all the issues identified by Sashiko I would like to check-in with folks (and Sashiko) on where the fixes are headed and would appreciate any feedback.

Applies against tip/master to ensure it considers pending x86/cache changes.

[1] https://sashiko.dev/#/patchset/20260429184858.36423-1-tony.luck%40intel.com

Reinette Chatre (6):
fs/resctrl: Fix deadlock for errors during mount fs/resctrl: Prevent use-after-free in rdtgroup_kn_put() fs/resctrl: Fix pseudo-locking lifetime handling fs/resctrl: Prevent deadlock and use-after-free in info file handlers x86/resctrl: Ensure domain fully initialized before placed on RCU list fs/resctrl: Fix UAF from worker threads when domains are removed

Tony Luck (3):
fs/resctrl: Move functions to avoid forward references in subsequent
fixes
fs/resctrl: Free mon_data structures on rdt_get_tree() failure fs/resctrl: Fix use-after-free during unmount

 arch/x86/kernel/cpu/resctrl/core.c      |  18 +-
 arch/x86/kernel/cpu/resctrl/intel_aet.c |   5 +-
 fs/resctrl/ctrlmondata.c                |  38 +-
 fs/resctrl/internal.h                   |  15 +-
 fs/resctrl/monitor.c                    | 100 ++-
 fs/resctrl/pseudo_lock.c                |  44 +-
 fs/resctrl/rdtgroup.c                   | 847 +++++++++++++++---------
 7 files changed, 680 insertions(+), 387 deletions(-)
Signature · 1 line
2.50.1
author maintainer
#1 lore raw

PATCH v3 1/9 fs/resctrl: Move functions to avoid forward references in subsequent fixes

From: Tony Luck <[email protected]>

rdt_get_tree() manages resctrl fs mount and rdt_kill_sb() manages resctrl fs unmount.

There is significant overlap between error cleanup during resctrl mount failure and cleanup on resctrl unmount yet the cleanup is not done consistently in these two flows.

Pull some cleanup functions before rdt_get_tree() in preparation for a new helper that can be shared between mount and unmount.

---
Changes since V2:
- Rewrite changelog.

---
 fs/resctrl/rdtgroup.c | 376 +++++++++++++++++++++---------------------
 1 file changed, 188 insertions(+), 188 deletions(-)
fs/resctrl/rdtgroup.c +188 -188
index af2cbab14497..91922fe1ea08 100644
@@ -2792,6 +2792,194 @@ static void schemata_list_destroy(void)
 	}
 }
 
+/*
+ * Move tasks from one to the other group. If @from is NULL, then all tasks
+ * in the systems are moved unconditionally (used for teardown).
+ *
+ * If @mask is not NULL the cpus on which moved tasks are running are set
+ * in that mask so the update smp function call is restricted to affected
+ * cpus.
+ */
+static void rdt_move_group_tasks(struct rdtgroup *from, struct rdtgroup *to,
+				 struct cpumask *mask)
+{
+	struct task_struct *p, *t;
+
+	read_lock(&tasklist_lock);
+	for_each_process_thread(p, t) {
+		if (!from || is_closid_match(t, from) ||
+		    is_rmid_match(t, from)) {
+			resctrl_arch_set_closid_rmid(t, to->closid,
+						     to->mon.rmid);
+
+			/*
+			 * 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_arch_sched_in() during context switch.
+			 */
+			smp_mb();
+
+			/*
+			 * If the task is on a CPU, set the CPU in the mask.
+			 * The detection is inaccurate as tasks might move or
+			 * schedule before the smp function call takes place.
+			 * In such a case the function call is pointless, but
+			 * there is no other side effect.
+			 */
+			if (IS_ENABLED(CONFIG_SMP) && mask && task_curr(t))
+				cpumask_set_cpu(task_cpu(t), mask);
+		}
+	}
+	read_unlock(&tasklist_lock);
+}
+
+static void free_all_child_rdtgrp(struct rdtgroup *rdtgrp)
+{
+	struct rdtgroup *sentry, *stmp;
+	struct list_head *head;
+
+	head = &rdtgrp->mon.crdtgrp_list;
+	list_for_each_entry_safe(sentry, stmp, head, mon.crdtgrp_list) {
+		rdtgroup_unassign_cntrs(sentry);
+		free_rmid(sentry->closid, sentry->mon.rmid);
+		list_del(&sentry->mon.crdtgrp_list);
+
+		if (atomic_read(&sentry->waitcount) != 0)
+			sentry->flags = RDT_DELETED;
+		else
+			rdtgroup_remove(sentry);
+	}
+}
+
+/*
+ * Forcibly remove all of subdirectories under root.
+ */
+static void rmdir_all_sub(void)
+{
+	struct rdtgroup *rdtgrp, *tmp;
+
+	/* Move all tasks to the default resource group */
+	rdt_move_group_tasks(NULL, &rdtgroup_default, NULL);
+
+	list_for_each_entry_safe(rdtgrp, tmp, &rdt_all_groups, rdtgroup_list) {
+		/* Free any child rmids */
+		free_all_child_rdtgrp(rdtgrp);
+
+		/* Remove each rdtgroup other than root */
+		if (rdtgrp == &rdtgroup_default)
+			continue;
+
+		if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
+		    rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)
+			rdtgroup_pseudo_lock_remove(rdtgrp);
+
+		/*
+		 * Give any CPUs back to the default group. We cannot copy
+		 * cpu_online_mask because a CPU might have executed the
+		 * offline callback already, but is still marked online.
+		 */
+		cpumask_or(&rdtgroup_default.cpu_mask,
+			   &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
+
+		rdtgroup_unassign_cntrs(rdtgrp);
+
+		free_rmid(rdtgrp->closid, rdtgrp->mon.rmid);
+
+		kernfs_remove(rdtgrp->kn);
+		list_del(&rdtgrp->rdtgroup_list);
+
+		if (atomic_read(&rdtgrp->waitcount) != 0)
+			rdtgrp->flags = RDT_DELETED;
+		else
+			rdtgroup_remove(rdtgrp);
+	}
+	/* Notify online CPUs to update per cpu storage and PQR_ASSOC MSR */
+	update_closid_rmid(cpu_online_mask, &rdtgroup_default);
+
+	kernfs_remove(kn_info);
+	kernfs_remove(kn_mongrp);
+	kernfs_remove(kn_mondata);
+}
+
+/**
+ * mon_get_kn_priv() - Get the mon_data priv data for this event.
+ *
+ * The same values are used across the mon_data directories of all control and
+ * monitor groups for the same event in the same domain. Keep a list of
+ * allocated structures and re-use an existing one with the same values for
+ * @rid, @domid, etc.
+ *
+ * @rid:    The resource id for the event file being created.
+ * @domid:  The domain id for the event file being created.
+ * @mevt:   The type of event file being created.
+ * @do_sum: Whether SNC summing monitors are being created. Only set
+ *	    when @rid == RDT_RESOURCE_L3.
+ *
+ * Return: Pointer to mon_data private data of the event, NULL on failure.
+ */
+static struct mon_data *mon_get_kn_priv(enum resctrl_res_level rid, int domid,
+					struct mon_evt *mevt,
+					bool do_sum)
+{
+	struct mon_data *priv;
+
+	lockdep_assert_held(&rdtgroup_mutex);
+
+	list_for_each_entry(priv, &mon_data_kn_priv_list, list) {
+		if (priv->rid == rid && priv->domid == domid &&
+		    priv->sum == do_sum && priv->evt == mevt)
+			return priv;
+	}
+
+	priv = kzalloc_obj(*priv);
+	if (!priv)
+		return NULL;
+
+	priv->rid = rid;
+	priv->domid = domid;
+	priv->sum = do_sum;
+	priv->evt = mevt;
+	list_add_tail(&priv->list, &mon_data_kn_priv_list);
+
+	return priv;
+}
+
+/**
+ * mon_put_kn_priv() - Free all allocated mon_data structures.
+ *
+ * Called when resctrl file system is unmounted.
+ */
+static void mon_put_kn_priv(void)
+{
+	struct mon_data *priv, *tmp;
+
+	lockdep_assert_held(&rdtgroup_mutex);
+
+	list_for_each_entry_safe(priv, tmp, &mon_data_kn_priv_list, list) {
+		list_del(&priv->list);
+		kfree(priv);
+	}
+}
+
+static void resctrl_fs_teardown(void)
+{
+	lockdep_assert_held(&rdtgroup_mutex);
+
+	/* Cleared by rdtgroup_destroy_root() */
+	if (!rdtgroup_default.kn)
+		return;
+
+	rmdir_all_sub();
+	rdtgroup_unassign_cntrs(&rdtgroup_default);
+	mon_put_kn_priv();
+	rdt_pseudo_lock_release();
+	rdtgroup_default.mode = RDT_MODE_SHAREABLE;
+	closid_exit();
+	schemata_list_destroy();
+	rdtgroup_destroy_root();
+}
+
 static int rdt_get_tree(struct fs_context *fc)
 {
 	struct rdt_fs_context *ctx = rdt_fc2context(fc);
@@ -2991,194 +3179,6 @@ static int rdt_init_fs_context(struct fs_context *fc)
 	return 0;
 }
 
-/*
- * Move tasks from one to the other group. If @from is NULL, then all tasks
- * in the systems are moved unconditionally (used for teardown).
- *
- * If @mask is not NULL the cpus on which moved tasks are running are set
- * in that mask so the update smp function call is restricted to affected
- * cpus.
- */
-static void rdt_move_group_tasks(struct rdtgroup *from, struct rdtgroup *to,
-				 struct cpumask *mask)
-{
-	struct task_struct *p, *t;
-
-	read_lock(&tasklist_lock);
-	for_each_process_thread(p, t) {
-		if (!from || is_closid_match(t, from) ||
-		    is_rmid_match(t, from)) {
-			resctrl_arch_set_closid_rmid(t, to->closid,
-						     to->mon.rmid);
-
-			/*
-			 * 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_arch_sched_in() during context switch.
-			 */
-			smp_mb();
-
-			/*
-			 * If the task is on a CPU, set the CPU in the mask.
-			 * The detection is inaccurate as tasks might move or
-			 * schedule before the smp function call takes place.
-			 * In such a case the function call is pointless, but
-			 * there is no other side effect.
-			 */
-			if (IS_ENABLED(CONFIG_SMP) && mask && task_curr(t))
-				cpumask_set_cpu(task_cpu(t), mask);
-		}
-	}
-	read_unlock(&tasklist_lock);
-}
-
-static void free_all_child_rdtgrp(struct rdtgroup *rdtgrp)
-{
-	struct rdtgroup *sentry, *stmp;
-	struct list_head *head;
-
-	head = &rdtgrp->mon.crdtgrp_list;
-	list_for_each_entry_safe(sentry, stmp, head, mon.crdtgrp_list) {
-		rdtgroup_unassign_cntrs(sentry);
-		free_rmid(sentry->closid, sentry->mon.rmid);
-		list_del(&sentry->mon.crdtgrp_list);
-
-		if (atomic_read(&sentry->waitcount) != 0)
-			sentry->flags = RDT_DELETED;
-		else
-			rdtgroup_remove(sentry);
-	}
-}
-
-/*
- * Forcibly remove all of subdirectories under root.
- */
-static void rmdir_all_sub(void)
-{
-	struct rdtgroup *rdtgrp, *tmp;
-
-	/* Move all tasks to the default resource group */
-	rdt_move_group_tasks(NULL, &rdtgroup_default, NULL);
-
-	list_for_each_entry_safe(rdtgrp, tmp, &rdt_all_groups, rdtgroup_list) {
-		/* Free any child rmids */
-		free_all_child_rdtgrp(rdtgrp);
-
-		/* Remove each rdtgroup other than root */
-		if (rdtgrp == &rdtgroup_default)
-			continue;
-
-		if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
-		    rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)
-			rdtgroup_pseudo_lock_remove(rdtgrp);
-
-		/*
-		 * Give any CPUs back to the default group. We cannot copy
-		 * cpu_online_mask because a CPU might have executed the
-		 * offline callback already, but is still marked online.
-		 */
-		cpumask_or(&rdtgroup_default.cpu_mask,
-			   &rdtgroup_default.cpu_mask, &rdtgrp->cpu_mask);
-
-		rdtgroup_unassign_cntrs(rdtgrp);
-
-		free_rmid(rdtgrp->closid, rdtgrp->mon.rmid);
-
-		kernfs_remove(rdtgrp->kn);
-		list_del(&rdtgrp->rdtgroup_list);
-
-		if (atomic_read(&rdtgrp->waitcount) != 0)
-			rdtgrp->flags = RDT_DELETED;
-		else
-			rdtgroup_remove(rdtgrp);
-	}
-	/* Notify online CPUs to update per cpu storage and PQR_ASSOC MSR */
-	update_closid_rmid(cpu_online_mask, &rdtgroup_default);
-
-	kernfs_remove(kn_info);
-	kernfs_remove(kn_mongrp);
-	kernfs_remove(kn_mondata);
-}
-
-/**
- * mon_get_kn_priv() - Get the mon_data priv data for this event.
- *
- * The same values are used across the mon_data directories of all control and
- * monitor groups for the same event in the same domain. Keep a list of
- * allocated structures and re-use an existing one with the same values for
- * @rid, @domid, etc.
- *
- * @rid:    The resource id for the event file being created.
- * @domid:  The domain id for the event file being created.
- * @mevt:   The type of event file being created.
- * @do_sum: Whether SNC summing monitors are being created. Only set
- *	    when @rid == RDT_RESOURCE_L3.
- *
- * Return: Pointer to mon_data private data of the event, NULL on failure.
- */
-static struct mon_data *mon_get_kn_priv(enum resctrl_res_level rid, int domid,
-					struct mon_evt *mevt,
-					bool do_sum)
-{
-	struct mon_data *priv;
-
-	lockdep_assert_held(&rdtgroup_mutex);
-
-	list_for_each_entry(priv, &mon_data_kn_priv_list, list) {
-		if (priv->rid == rid && priv->domid == domid &&
-		    priv->sum == do_sum && priv->evt == mevt)
-			return priv;
-	}
-
-	priv = kzalloc_obj(*priv);
-	if (!priv)
-		return NULL;
-
-	priv->rid = rid;
-	priv->domid = domid;
-	priv->sum = do_sum;
-	priv->evt = mevt;
-	list_add_tail(&priv->list, &mon_data_kn_priv_list);
-
-	return priv;
-}
-
-/**
- * mon_put_kn_priv() - Free all allocated mon_data structures.
- *
- * Called when resctrl file system is unmounted.
- */
-static void mon_put_kn_priv(void)
-{
-	struct mon_data *priv, *tmp;
-
-	lockdep_assert_held(&rdtgroup_mutex);
-
-	list_for_each_entry_safe(priv, tmp, &mon_data_kn_priv_list, list) {
-		list_del(&priv->list);
-		kfree(priv);
-	}
-}
-
-static void resctrl_fs_teardown(void)
-{
-	lockdep_assert_held(&rdtgroup_mutex);
-
-	/* Cleared by rdtgroup_destroy_root() */
-	if (!rdtgroup_default.kn)
-		return;
-
-	rmdir_all_sub();
-	rdtgroup_unassign_cntrs(&rdtgroup_default);
-	mon_put_kn_priv();
-	rdt_pseudo_lock_release();
-	rdtgroup_default.mode = RDT_MODE_SHAREABLE;
-	closid_exit();
-	schemata_list_destroy();
-	rdtgroup_destroy_root();
-}
-
 static void rdt_kill_sb(struct super_block *sb)
 {
 	struct rdt_resource *r;
Signature · 1 line
2.50.1
maintainer Reviewed-by on v3 1/9
#2 lore raw

Re: [PATCH v3 1/9] fs/resctrl: Move functions to avoid forward references in subsequent fixes

Hi Reinette,

Quoted 9 lines · Reinette Chatre

From: Tony Luck <[email protected]>

rdt_get_tree() manages resctrl fs mount and rdt_kill_sb() manages resctrl fs unmount.

There is significant overlap between error cleanup during resctrl mount failure and cleanup on resctrl unmount yet the cleanup is not done consistently in these two flows.

Quoted 5 lines

Pull some cleanup functions before rdt_get_tree() in preparation for a new helper that can be shared between mount and unmount.

Thanks,

Ben

author maintainer
#3 lore raw

PATCH v3 2/9 fs/resctrl: Free mon_data structures on rdt_get_tree() failure

In reply to Reinette Chatre

From: Tony Luck <[email protected]>

If mkdir_mondata_all() or a subsequent call in rdt_get_tree() fails, the mon_data structures allocated by mon_get_kn_priv() are leaked.

Add mon_put_kn_priv() to the out_mongrp error path to free the mon_data structures.

Fixes 2a6566038544 ("x86/resctrl: Expand the width of domid by replacing mon_data_bits")

---
Changes since V2:
- Reword changelog.

---
 fs/resctrl/rdtgroup.c | 1 +
 1 file changed, 1 insertion(+)
fs/resctrl/rdtgroup.c +1 -0
index 91922fe1ea08..f573db9e6e84 100644
@@ -3081,6 +3081,7 @@ static int rdt_get_tree(struct fs_context *fc)
 		kernfs_remove(kn_mondata);
 out_mongrp:
 	if (resctrl_arch_mon_capable()) {
+		mon_put_kn_priv();
 		rdtgroup_unassign_cntrs(&rdtgroup_default);
 		kernfs_remove(kn_mongrp);
 	}
Signature · 1 line
2.50.1
maintainer Reviewed-by on v3 2/9
#4 lore raw

Re: [PATCH v3 2/9] fs/resctrl: Free mon_data structures on rdt_get_tree() failure

Hi Reinette,

Quoted 5 lines · Reinette Chatre

From: Tony Luck <[email protected]>

If mkdir_mondata_all() or a subsequent call in rdt_get_tree() fails, the mon_data structures allocated by mon_get_kn_priv() are leaked.

Quoted 7 lines

Add mon_put_kn_priv() to the out_mongrp error path to free the mon_data structures.

Fixes 2a6566038544 ("x86/resctrl: Expand the width of domid by replacing mon_data_bits")

Looks good to me.

Quoted 19 lines

---
Changes since V2:
- Reword changelog.

---
 fs/resctrl/rdtgroup.c | 1 +
 1 file changed, 1 insertion(+)
fs/resctrl/rdtgroup.c +1 -0
index 91922fe1ea08..f573db9e6e84 100644
@@ -3081,6 +3081,7 @@ static int rdt_get_tree(struct fs_context *fc)
 		kernfs_remove(kn_mondata);
 out_mongrp:
 	if (resctrl_arch_mon_capable()) {
+		mon_put_kn_priv();
 		rdtgroup_unassign_cntrs(&rdtgroup_default);
 		kernfs_remove(kn_mongrp);
 	}
author maintainer
#5 lore raw

PATCH v3 3/9 fs/resctrl: Fix use-after-free during unmount

In reply to Reinette Chatre

From: Tony Luck <[email protected]>

During unmount or failure teardown all mon_data structures that contain monitoring event file private data are freed after which kernfs nodes are removed. However, the RDT_DELETED flag is never set for the statically allocated default resource group.

A concurrent reader of an event file associated with the default resource group may, after dropping kernfs active protection, block on rdtgroup_mutex while unmount proceeds to free the file private data and destroy the kernfs node without waiting for the reader.

When the mutex is released, the reader wakes up, observes that RDT_DELETED is not set for the default group, and dereferences the already-freed file private data.

Set RDT_DELETED for the default group unconditionally since the flag does not lead to free of this statically allocated group.

Do not allow a new resctrl mount if there are any waiters on default group of previous mount. A new mount will re-initialize the default group that would appear to waiters from previous mount as though the default group is accessible causing them to access the mon_data structures from the previous mount that have been removed.

Fixes 2a6566038544 ("x86/resctrl: Expand the width of domid by replacing mon_data_bits")

---
Changes since V2:
- Rewrite changelog to not describe code as much.
- Rework changelog to switch to "Reported-by/Closes".
- Merge the duplicate rdtgroup_remove() comment with the function comment.
- Fix changelog to not mention that RDT_DELETED flag is set conditionally.
- Change "Fixes:" tag to point to commit that introduced dynamically allocated mon_data this bug involves.

---
 fs/resctrl/rdtgroup.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
fs/resctrl/rdtgroup.c +16 -2
index f573db9e6e84..8a1457825919 100644
@@ -585,14 +585,20 @@ static ssize_t rdtgroup_cpus_write(struct kernfs_open_file *of,
  *
  * On resource group creation via a mkdir, an extra kernfs_node reference is
  * taken to ensure that the rdtgroup structure remains accessible for the
- * rdtgroup_kn_unlock() calls where it is removed.
+ * rdtgroup_kn_unlock() calls where it is removed. The default group is
+ * statically allocated: it does not have an extra reference but will have
+ * RDT_DELETED set on unmount to support safe access to its associated files
+ * via rdtgroup_kn_lock_live/rdtgroup_kn_unlock().
  *
- * Drop the extra reference here, then free the rdtgroup structure.
+ * For all but the default group: drop the extra reference, then free the
+ * rdtgroup structure.
  *
  * Return: void
  */
 static void rdtgroup_remove(struct rdtgroup *rdtgrp)
 {
+	if (rdtgrp == &rdtgroup_default)
+		return;
 	kernfs_put(rdtgrp->kn);
 	kfree(rdtgrp);
 }
@@ -2975,6 +2981,7 @@ static void resctrl_fs_teardown(void)
 	mon_put_kn_priv();
 	rdt_pseudo_lock_release();
 	rdtgroup_default.mode = RDT_MODE_SHAREABLE;
+	rdtgroup_default.flags = RDT_DELETED;
 	closid_exit();
 	schemata_list_destroy();
 	rdtgroup_destroy_root();
@@ -3000,6 +3007,12 @@ static int rdt_get_tree(struct fs_context *fc)
 		goto out;
 	}
 
+	/* Avoid races from pending operations from a previous mount */
+	if (atomic_read(&rdtgroup_default.waitcount) != 0) {
+		ret = -EBUSY;
+		goto out;
+	}
+
 	ret = setup_rmid_lru_list();
 	if (ret)
 		goto out;
@@ -4275,6 +4288,7 @@ static int rdtgroup_setup_root(struct rdt_fs_context *ctx)
 
 	ctx->kfc.root = rdt_root;
 	rdtgroup_default.kn = kernfs_root_to_node(rdt_root);
+	rdtgroup_default.flags = 0;
 
 	return 0;
 }
Signature · 1 line
2.50.1
maintainer
#6 lore raw

Re: [PATCH v3 3/9] fs/resctrl: Fix use-after-free during unmount

Hi Reinette,

Quoted 70 lines · Reinette Chatre

From: Tony Luck <[email protected]>

During unmount or failure teardown all mon_data structures that contain monitoring event file private data are freed after which kernfs nodes are removed. However, the RDT_DELETED flag is never set for the statically allocated default resource group.

A concurrent reader of an event file associated with the default resource group may, after dropping kernfs active protection, block on rdtgroup_mutex while unmount proceeds to free the file private data and destroy the kernfs node without waiting for the reader.

When the mutex is released, the reader wakes up, observes that RDT_DELETED is not set for the default group, and dereferences the already-freed file private data.

Set RDT_DELETED for the default group unconditionally since the flag does not lead to free of this statically allocated group.

Do not allow a new resctrl mount if there are any waiters on default group of previous mount. A new mount will re-initialize the default group that would appear to waiters from previous mount as though the default group is accessible causing them to access the mon_data structures from the previous mount that have been removed.

Fixes 2a6566038544 ("x86/resctrl: Expand the width of domid by replacing mon_data_bits")

---
Changes since V2:
- Rewrite changelog to not describe code as much.
- Rework changelog to switch to "Reported-by/Closes".
- Merge the duplicate rdtgroup_remove() comment with the function comment.
- Fix changelog to not mention that RDT_DELETED flag is set conditionally.
- Change "Fixes:" tag to point to commit that introduced dynamically allocated mon_data this bug involves.

---
 fs/resctrl/rdtgroup.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
fs/resctrl/rdtgroup.c +8 -2
index f573db9e6e84..8a1457825919 100644
@@ -585,14 +585,20 @@ static ssize_t rdtgroup_cpus_write(struct kernfs_open_file *of,
  *
  * On resource group creation via a mkdir, an extra kernfs_node reference is
  * taken to ensure that the rdtgroup structure remains accessible for the
- * rdtgroup_kn_unlock() calls where it is removed.
+ * rdtgroup_kn_unlock() calls where it is removed. The default group is
+ * statically allocated: it does not have an extra reference but will have
+ * RDT_DELETED set on unmount to support safe access to its associated files
+ * via rdtgroup_kn_lock_live/rdtgroup_kn_unlock().
  *
- * Drop the extra reference here, then free the rdtgroup structure.
+ * For all but the default group: drop the extra reference, then free the
+ * rdtgroup structure.
  *
  * Return: void
  */
 static void rdtgroup_remove(struct rdtgroup *rdtgrp)
 {
+	if (rdtgrp == &rdtgroup_default)
+		return;
 	kernfs_put(rdtgrp->kn);
 	kfree(rdtgrp);
 }

fs/resctrl/rdtgroup.c static void resctrl_fs_teardown(void) · line 2984

Quoted 4 lines
fs/resctrl/rdtgroup.c +1 -0
@@ -2975,6 +2981,7 @@ static void resctrl_fs_teardown(void)
 	mon_put_kn_priv();
 	rdt_pseudo_lock_release();
 	rdtgroup_default.mode = RDT_MODE_SHAREABLE;
+	rdtgroup_default.flags = RDT_DELETED;

Would it be better to use |= here?
I expect it's not functionally different but I'm unsure of the interaction with RDT_DELETED_PLR and it seems best in general unless clearing other possible flags is intended.

Thanks,

Ben

Quoted 24 lines
fs/resctrl/rdtgroup.c +7 -0
 	closid_exit();
 	schemata_list_destroy();
 	rdtgroup_destroy_root();
@@ -3000,6 +3007,12 @@ static int rdt_get_tree(struct fs_context *fc)
 		goto out;
 	}
 
+	/* Avoid races from pending operations from a previous mount */
+	if (atomic_read(&rdtgroup_default.waitcount) != 0) {
+		ret = -EBUSY;
+		goto out;
+	}
+
 	ret = setup_rmid_lru_list();
 	if (ret)
 		goto out;
@@ -4275,6 +4288,7 @@ static int rdtgroup_setup_root(struct rdt_fs_context *ctx)
 
 	ctx->kfc.root = rdt_root;
 	rdtgroup_default.kn = kernfs_root_to_node(rdt_root);
+	rdtgroup_default.flags = 0;
 
 	return 0;
 }
author maintainer
#7 lore raw

Re: [PATCH v3 3/9] fs/resctrl: Fix use-after-free during unmount

Hi Ben,

Quoted 1 line · Ben Horgan

On 5/22/26 20:15, Reinette Chatre wrote:

..

Quoted 29 lines
Quoted 29 lines
fs/resctrl/rdtgroup.c +8 -2
index f573db9e6e84..8a1457825919 100644
@@ -585,14 +585,20 @@ static ssize_t rdtgroup_cpus_write(struct kernfs_open_file *of,
  *
  * On resource group creation via a mkdir, an extra kernfs_node reference is
  * taken to ensure that the rdtgroup structure remains accessible for the
- * rdtgroup_kn_unlock() calls where it is removed.
+ * rdtgroup_kn_unlock() calls where it is removed. The default group is
+ * statically allocated: it does not have an extra reference but will have
+ * RDT_DELETED set on unmount to support safe access to its associated files
+ * via rdtgroup_kn_lock_live/rdtgroup_kn_unlock().
  *
- * Drop the extra reference here, then free the rdtgroup structure.
+ * For all but the default group: drop the extra reference, then free the
+ * rdtgroup structure.
  *
  * Return: void
  */
 static void rdtgroup_remove(struct rdtgroup *rdtgrp)
 {
+	if (rdtgrp == &rdtgroup_default)
+		return;
 	kernfs_put(rdtgrp->kn);
 	kfree(rdtgrp);
 }
@@ -2975,6 +2981,7 @@ static void resctrl_fs_teardown(void)
 	mon_put_kn_priv();
Quoted 8 lines

fs/resctrl/rdtgroup.c static void resctrl_fs_teardown(void) · line 2984

Quoted 3 lines
fs/resctrl/rdtgroup.c +1 -0
 	rdt_pseudo_lock_release();
 	rdtgroup_default.mode = RDT_MODE_SHAREABLE;
+	rdtgroup_default.flags = RDT_DELETED;

Would it be better to use |= here?
I expect it's not functionally different but I'm unsure of the interaction with RDT_DELETED_PLR and it seems best in general unless clearing other possible flags is intended.

Thank you very much for taking a look and for the other tags.

I see this initialization as assigning a "reset" value that reflects the cleanup done by resctrl_fs_teardown() as opposed to recording/adding another state during runtime.
If resctrl does add RDT_DELETED_PLR then there is no interaction to be concerned about here since pseudo-locking is not supported on the default group. You can confirm this by considering the first "if (rdtgrp == &rdtgroup_default)" check within fs/resctrl/pseudo_lock.c:rdtgroup_locksetup_enter().

Since there is only one flag, RDT_DELETED, possible for the default group you are correct that using |= is not functionally different but my preference would be to use assignment in this spot just to make it clear that this is a reset value.

Reinette

Reviewed-by on v3 3/9
#8 lore raw

Re: [PATCH v3 3/9] fs/resctrl: Fix use-after-free during unmount

In reply to Reinette Chatre
Quoted 3 lines · Reinette Chatre

When the mutex is released, the reader wakes up, observes that RDT_DELETED is not set for the default group, and dereferences the already-freed file private data.

I wonder if a code call sequence could be added in the commit log, which would be helpful to quickly understand the race condition:

CPU 0 (read mon_data file) CPU 1 (umount)
-------------------------- --------------

rdtgroup_kn_lock_live()
rdtgrp = &rdtgroup_default
waitcount++
break_active_protection()

                                    mutex_lock()
                                    mon_put_kn_priv()
                                      kfree(mon_data)
                                    mutex_unlock()
  mutex_lock()
  rdtgrp->flags & RDT_DELETED?
    // no -- never set for default

return rdtgrp

md = of->kn->priv // UAF: freed

I did not find issues in current implementation,

author maintainer
#9 lore raw

Re: [PATCH v3 3/9] fs/resctrl: Fix use-after-free during unmount

Hi Chenyu,

Quoted 22 lines · Chen Yu
Quoted 3 lines · Reinette Chatre

When the mutex is released, the reader wakes up, observes that RDT_DELETED is not set for the default group, and dereferences the already-freed file private data.

I wonder if a code call sequence could be added in the commit log, which would be helpful to quickly understand the race condition:

CPU 0 (read mon_data file) CPU 1 (umount)
-------------------------- --------------

rdtgroup_kn_lock_live()
rdtgrp = &rdtgroup_default
waitcount++
break_active_protection()

                                    mutex_lock()
                                    mon_put_kn_priv()
                                      kfree(mon_data)
                                    mutex_unlock()
  mutex_lock()
  rdtgrp->flags & RDT_DELETED?
    // no -- never set for default
Quoted 4 lines

return rdtgrp

md = of->kn->priv // UAF: freed

Yes, something like this will make the issue easier to see. Will do.

Quoted 4 lines

I did not find issues in current implementation,

Thank you very much.
I just realized that I omitted your other tag from patch #2 [1]. I am very sorry and will fix that it next posting.

Reinette

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

author maintainer
#10 lore raw

PATCH v3 4/9 fs/resctrl: Fix deadlock for errors during mount

In reply to Reinette Chatre

rdt_get_tree() acquires rdtgroup_mutex before calling kernfs_get_tree(). If superblock setup fails inside kernfs_get_tree(), the VFS calls kill_sb on the same thread before the call returns. rdt_kill_sb() unconditionally attempts to acquire rdtgroup_mutex and deadlock occurs.

Move the call to kernfs_get_tree() outside of locks. If kernfs_get_tree() fails and ctx->kfc.new_sb_created is set, then rdt_kill_sb() has already been called and no further cleanup is needed.

Add an extra hold in this error path on rdtgroup_default.kn to defend against other races destroying the root which is then dereferenced in kernfs_kill_sb()

Add resctrl_unmount() helper to keep code consistent between the rdt_get_tree() failure path and a normal unmount.

Fixes 5ff193fbde20 ("x86/intel_rdt: Add basic resctrl filesystem support")

---
Changes since V2:
- Switch to "Reported-by/Closes" in changelog

---
 fs/resctrl/rdtgroup.c | 82 +++++++++++++++++++++++++++++--------------
 1 file changed, 55 insertions(+), 27 deletions(-)
fs/resctrl/rdtgroup.c +55 -27
index 8a1457825919..d323b515060b 100644
@@ -2987,10 +2987,34 @@ static void resctrl_fs_teardown(void)
 	rdtgroup_destroy_root();
 }
 
+static void resctrl_unmount(void)
+{
+	struct rdt_resource *r;
+
+	cpus_read_lock();
+	mutex_lock(&rdtgroup_mutex);
+
+	rdt_disable_ctx();
+
+	/* Put everything back to default values. */
+	for_each_alloc_capable_rdt_resource(r)
+		resctrl_arch_reset_all_ctrls(r);
+
+	resctrl_fs_teardown();
+	if (resctrl_arch_alloc_capable())
+		resctrl_arch_disable_alloc();
+	if (resctrl_arch_mon_capable())
+		resctrl_arch_disable_mon();
+	resctrl_mounted = false;
+	mutex_unlock(&rdtgroup_mutex);
+	cpus_read_unlock();
+}
+
 static int rdt_get_tree(struct fs_context *fc)
 {
 	struct rdt_fs_context *ctx = rdt_fc2context(fc);
 	unsigned long flags = RFTYPE_CTRL_BASE;
+	struct kernfs_node *rdt_root_kn;
 	struct rdt_l3_mon_domain *dom;
 	struct rdt_resource *r;
 	int ret;
@@ -3066,10 +3090,6 @@ static int rdt_get_tree(struct fs_context *fc)
 	if (ret)
 		goto out_mondata;
 
-	ret = kernfs_get_tree(fc);
-	if (ret < 0)
-		goto out_psl;
-
 	if (resctrl_arch_alloc_capable())
 		resctrl_arch_enable_alloc();
 	if (resctrl_arch_mon_capable())
@@ -3085,10 +3105,37 @@ static int rdt_get_tree(struct fs_context *fc)
 						   RESCTRL_PICK_ANY_CPU);
 	}
 
-	goto out;
+	/*
+	 * Ensure root kn remains accessible after mutex is unlocked so that
+	 * kernfs_kill_sb() can run safely if called by kernfs_get_tree()'s
+	 * failure path after creating a superblock but before taking reference
+	 * on root kn.
+	 */
+	kernfs_get(rdtgroup_default.kn);
+
+	/*
+	 * Make backup of the current root kn being created to be used in kernfs_put().
+	 * The additional reference taken above will prevent the kn from being freed
+	 * before kernfs_kill_sb() can run but rdtgroup_default.kn may be set to NULL
+	 * via rdtgroup_destroy_root() and its backing root (rdt_root) could be overwritten
+	 * before kernfs_put() can run.
+	 */
+	rdt_root_kn = rdtgroup_default.kn;
+
+	rdt_last_cmd_clear();
+	mutex_unlock(&rdtgroup_mutex);
+	cpus_read_unlock();
+
+	ret = kernfs_get_tree(fc);
+	/*
+	 * resctrl can only be mounted once, new superblock only expected
+	 * to be created once.
+	 */
+	if (!ctx->kfc.new_sb_created)
+		resctrl_unmount();
+	kernfs_put(rdt_root_kn);
+	return ret;
 
-out_psl:
-	rdt_pseudo_lock_release();
 out_mondata:
 	if (resctrl_arch_mon_capable())
 		kernfs_remove(kn_mondata);
@@ -3108,7 +3155,6 @@ static int rdt_get_tree(struct fs_context *fc)
 out_root:
 	rdtgroup_destroy_root();
 out:
-	rdt_last_cmd_clear();
 	mutex_unlock(&rdtgroup_mutex);
 	cpus_read_unlock();
 	return ret;
@@ -3195,26 +3241,8 @@ static int rdt_init_fs_context(struct fs_context *fc)
 
 static void rdt_kill_sb(struct super_block *sb)
 {
-	struct rdt_resource *r;
-
-	cpus_read_lock();
-	mutex_lock(&rdtgroup_mutex);
-
-	rdt_disable_ctx();
-
-	/* Put everything back to default values. */
-	for_each_alloc_capable_rdt_resource(r)
-		resctrl_arch_reset_all_ctrls(r);
-
-	resctrl_fs_teardown();
-	if (resctrl_arch_alloc_capable())
-		resctrl_arch_disable_alloc();
-	if (resctrl_arch_mon_capable())
-		resctrl_arch_disable_mon();
-	resctrl_mounted = false;
+	resctrl_unmount();
 	kernfs_kill_sb(sb);
-	mutex_unlock(&rdtgroup_mutex);
-	cpus_read_unlock();
 }
 
 static struct file_system_type rdt_fs_type = {
Signature · 1 line
2.50.1
maintainer Reviewed-by on v3 4/9
#11 lore raw

Re: [PATCH v3 4/9] fs/resctrl: Fix deadlock for errors during mount

Hi Reinette,

Quoted 15 lines · Reinette Chatre

rdt_get_tree() acquires rdtgroup_mutex before calling kernfs_get_tree(). If superblock setup fails inside kernfs_get_tree(), the VFS calls kill_sb on the same thread before the call returns. rdt_kill_sb() unconditionally attempts to acquire rdtgroup_mutex and deadlock occurs.

Move the call to kernfs_get_tree() outside of locks. If kernfs_get_tree() fails and ctx->kfc.new_sb_created is set, then rdt_kill_sb() has already been called and no further cleanup is needed.

Add an extra hold in this error path on rdtgroup_default.kn to defend against other races destroying the root which is then dereferenced in kernfs_kill_sb()

Add resctrl_unmount() helper to keep code consistent between the rdt_get_tree() failure path and a normal unmount.

Quoted 6 lines
Fixes 5ff193fbde20 ("x86/intel_rdt: Add basic resctrl filesystem support")

Looks good to me.

Thanks,

Ben

Reviewed-by on v3 4/9
#12 lore raw

Re: [PATCH v3 4/9] fs/resctrl: Fix deadlock for errors during mount

In reply to Reinette Chatre

fs/resctrl/rdtgroup.c static int rdt_get_tree(struct fs_context *fc) · line 3109

Quoted 7 lines · Reinette Chatre
fs/resctrl/rdtgroup.c +2 -1
@@ -3085,10 +3105,37 @@ static int rdt_get_tree(struct fs_context *fc)
  						   RESCTRL_PICK_ANY_CPU);
  	}
 
-	goto out;
+	/*
+	 * Ensure root kn remains accessible after mutex is unlocked so that

Maybe a little more accurate to say "Ensure rdt_root remains accessible"? Here we increase reference for rdtgroup_default.kn, and protect against UAF of
kernfs_kill_sb(sb) ->
info = kernfs_info(sb) ->

    kernfs_put(info->root->kn)

where the info->root is UAF rather than the kn.

Other looks good to me.

thanks,
Chenyu

Quoted 5 lines
fs/resctrl/rdtgroup.c +5 -0
+	 * kernfs_kill_sb() can run safely if called by kernfs_get_tree()'s
+	 * failure path after creating a superblock but before taking reference
+	 * on root kn.
+	 */
+	kernfs_get(rdtgroup_default.kn);
author maintainer
#13 lore raw

Re: [PATCH v3 4/9] fs/resctrl: Fix deadlock for errors during mount

Hi Chenyu,

Quoted 15 lines · Yu C

fs/resctrl/rdtgroup.c static int rdt_get_tree(struct fs_context *fc) · line 3109

Quoted 6 lines · Reinette Chatre
fs/resctrl/rdtgroup.c +2 -0
@@ -3085,10 +3105,37 @@ static int rdt_get_tree(struct fs_context *fc)
                              RESCTRL_PICK_ANY_CPU);
       }
   -    goto out;
+    /*
+     * Ensure root kn remains accessible after mutex is unlocked so that

Maybe a little more accurate to say "Ensure rdt_root remains accessible"? Here we increase reference for rdtgroup_default.kn, and protect against UAF of
kernfs_kill_sb(sb) ->
  info = kernfs_info(sb) ->
    kernfs_put(info->root->kn)

Quoted 1 line

where the info->root is UAF rather than the kn.

Right. The UAF is indeed on the root self while its lifetime is controlled by references to its kn (root->kn). Dropping the last reference on root->kn causes root to be freed.

rdt_root is the name of a variable though and its value can actually change in the flow involved here so I'd prefer not to phrase it exactly like that. How about just "Ensure root remains accessible ..."?

Quoted 4 lines

Other looks good to me.

Thank you very much.

Reinette

#14 lore raw

Re: [PATCH v3 4/9] fs/resctrl: Fix deadlock for errors during mount

Quoted 18 lines · Reinette Chatre

Hi Chenyu,

Quoted 15 lines · Yu C

fs/resctrl/rdtgroup.c static int rdt_get_tree(struct fs_context *fc) · line 3109

Quoted 6 lines · Reinette Chatre
fs/resctrl/rdtgroup.c +2 -0
@@ -3085,10 +3105,37 @@ static int rdt_get_tree(struct fs_context *fc)
                              RESCTRL_PICK_ANY_CPU);
       }
   -    goto out;
+    /*
+     * Ensure root kn remains accessible after mutex is unlocked so that

Maybe a little more accurate to say "Ensure rdt_root remains accessible"? Here we increase reference for rdtgroup_default.kn, and protect against UAF of
kernfs_kill_sb(sb) ->
  info = kernfs_info(sb) ->
    kernfs_put(info->root->kn)

Quoted 8 lines
Quoted 1 line

where the info->root is UAF rather than the kn.

Right. The UAF is indeed on the root self while its lifetime is controlled by references to its kn (root->kn). Dropping the last reference on root->kn causes root to be freed.

rdt_root is the name of a variable though and its value can actually change in the flow involved here so I'd prefer not to phrase it exactly like that. How about just "Ensure root remains accessible ..."?

Yes, this looks good to me.

thanks,
Chenyu

author maintainer
#15 lore raw

PATCH v3 5/9 fs/resctrl: Prevent use-after-free in rdtgroup_kn_put()

In reply to Reinette Chatre

A struct rdtgroup is reference counted via rdtgroup::waitcount. Callers that need the structure to remain valid across a sleep (while waiting on acquiring rdtgroup_mutex) take a reference with rdtgroup_kn_get() and release it with rdtgroup_kn_put(). The release path is intended to serve as the fallback freer: if the count drops to zero and the group has already been marked RDT_DELETED, rdtgroup_kn_put() frees the structure. The bulk teardown paths free_all_child_rdtgrp() and rmdir_all_sub() resulting from a resctrl directory remove or resctrl fs unmount act as the primary freer: they hold rdtgroup_mutex and free each rdtgroup whose waitcount is zero, otherwise they set RDT_DELETED and leave the freeing to the last waiter.

These two freers race. rdtgroup_kn_put() commits waitcount == 0 with atomic_dec_and_test() outside rdtgroup_mutex, then reads rdtgroup::flags. Between those two operations a concurrent caller of free_all_child_rdtgrp() or rmdir_all_sub() (which holds the mutex) can observe waitcount == 0 via atomic_read(), call rdtgroup_remove(), and kfree() the structure. The subsequent read of rdtgroup::flags in rdtgroup_kn_put() is then a use-after-free, and the structure may even be freed twice if the freed memory happens to satisfy the RDT_DELETED flag check.

Replace the bare atomic_dec_and_test() with atomic_dec_and_mutex_lock() so that the decrement-to-zero takes rdtgroup_mutex before the count becomes globally visible. The inspection of rdtgroup::flags then runs under the same mutex held by the bulk freers, making the two paths mutually exclusive. The common case where the count does not reach zero remains lock-free. Defer kernfs_unbreak_active_protection() until after the mutex is dropped since kernfs active protections functionally wrap rdtgroup_mutex. Remove resource group, which in turn drops its kernfs reference, after kernfs protection is restored.

Fixes b8511ccc75c0 ("x86/resctrl: Fix use-after-free when deleting resource groups")
Assisted-by GitHub_Copilot:gemini-3.1-pro

---
Changes since V2:
- New patch

---
 fs/resctrl/rdtgroup.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
fs/resctrl/rdtgroup.c +14 -5
index d323b515060b..6418395877cf 100644
@@ -2606,15 +2606,24 @@ static void rdtgroup_kn_get(struct rdtgroup *rdtgrp, struct kernfs_node *kn)
 
 static void rdtgroup_kn_put(struct rdtgroup *rdtgrp, struct kernfs_node *kn)
 {
-	if (atomic_dec_and_test(&rdtgrp->waitcount) &&
-	    (rdtgrp->flags & RDT_DELETED)) {
+	bool needs_free;
+
+	if (!atomic_dec_and_mutex_lock(&rdtgrp->waitcount, &rdtgroup_mutex)) {
+		kernfs_unbreak_active_protection(kn);
+		return;
+	}
+
+	needs_free = rdtgrp->flags & RDT_DELETED;
+
+	mutex_unlock(&rdtgroup_mutex);
+
+	kernfs_unbreak_active_protection(kn);
+
+	if (needs_free) {
 		if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
 		    rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)
 			rdtgroup_pseudo_lock_remove(rdtgrp);
-		kernfs_unbreak_active_protection(kn);
 		rdtgroup_remove(rdtgrp);
-	} else {
-		kernfs_unbreak_active_protection(kn);
 	}
 }
 
Signature · 1 line
2.50.1
maintainer Reviewed-by on v3 5/9
#16 lore raw

Re: [PATCH v3 5/9] fs/resctrl: Prevent use-after-free in rdtgroup_kn_put()

Hi Reinette,

Quoted 31 lines · Reinette Chatre

A struct rdtgroup is reference counted via rdtgroup::waitcount. Callers that need the structure to remain valid across a sleep (while waiting on acquiring rdtgroup_mutex) take a reference with rdtgroup_kn_get() and release it with rdtgroup_kn_put(). The release path is intended to serve as the fallback freer: if the count drops to zero and the group has already been marked RDT_DELETED, rdtgroup_kn_put() frees the structure. The bulk teardown paths free_all_child_rdtgrp() and rmdir_all_sub() resulting from a resctrl directory remove or resctrl fs unmount act as the primary freer: they hold rdtgroup_mutex and free each rdtgroup whose waitcount is zero, otherwise they set RDT_DELETED and leave the freeing to the last waiter.

These two freers race. rdtgroup_kn_put() commits waitcount == 0 with atomic_dec_and_test() outside rdtgroup_mutex, then reads rdtgroup::flags. Between those two operations a concurrent caller of free_all_child_rdtgrp() or rmdir_all_sub() (which holds the mutex) can observe waitcount == 0 via atomic_read(), call rdtgroup_remove(), and kfree() the structure. The subsequent read of rdtgroup::flags in rdtgroup_kn_put() is then a use-after-free, and the structure may even be freed twice if the freed memory happens to satisfy the RDT_DELETED flag check.

Replace the bare atomic_dec_and_test() with atomic_dec_and_mutex_lock() so that the decrement-to-zero takes rdtgroup_mutex before the count becomes globally visible. The inspection of rdtgroup::flags then runs under the same mutex held by the bulk freers, making the two paths mutually exclusive. The common case where the count does not reach zero remains lock-free. Defer kernfs_unbreak_active_protection() until after the mutex is dropped since kernfs active protections functionally wrap rdtgroup_mutex. Remove resource group, which in turn drops its kernfs reference, after kernfs protection is restored.

Quoted 5 lines
Fixes b8511ccc75c0 ("x86/resctrl: Fix use-after-free when deleting resource groups")
Assisted-by GitHub_Copilot:gemini-3.1-pro

Seems good to me.

Quoted 41 lines

---
Changes since V2:
- New patch

---
 fs/resctrl/rdtgroup.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
fs/resctrl/rdtgroup.c +14 -5
index d323b515060b..6418395877cf 100644
@@ -2606,15 +2606,24 @@ static void rdtgroup_kn_get(struct rdtgroup *rdtgrp, struct kernfs_node *kn)
 
 static void rdtgroup_kn_put(struct rdtgroup *rdtgrp, struct kernfs_node *kn)
 {
-	if (atomic_dec_and_test(&rdtgrp->waitcount) &&
-	    (rdtgrp->flags & RDT_DELETED)) {
+	bool needs_free;
+
+	if (!atomic_dec_and_mutex_lock(&rdtgrp->waitcount, &rdtgroup_mutex)) {
+		kernfs_unbreak_active_protection(kn);
+		return;
+	}
+
+	needs_free = rdtgrp->flags & RDT_DELETED;
+
+	mutex_unlock(&rdtgroup_mutex);
+
+	kernfs_unbreak_active_protection(kn);
+
+	if (needs_free) {
 		if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
 		    rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)
 			rdtgroup_pseudo_lock_remove(rdtgrp);
-		kernfs_unbreak_active_protection(kn);
 		rdtgroup_remove(rdtgrp);
-	} else {
-		kernfs_unbreak_active_protection(kn);
 	}
 }
 
author maintainer
#17 lore raw

PATCH v3 6/9 fs/resctrl: Fix pseudo-locking lifetime handling

In reply to Reinette Chatre

rmdir_all_sub() implements resctrl unmount teardown by iterating through all resource groups. Memory cleanup is coordinated by deferring resource group removal to rdtgroup_kn_put() if there are lingering waiters.

A couple of issues exist in the pseudo-locking lifetime management:

1) rmdir_all_sub() unconditionally invokes rdtgroup_pseudo_lock_remove() on pseudo-locked groups, even if active references remain. When the deferred rdtgroup_kn_put() is finally executed by the last waiter, it erroneously calls rdtgroup_pseudo_lock_remove() a second time, resulting in a NULL-pointer dereference as the pseudo-lock structures have already been freed.

2) pseudo_lock_dev_release() drops the waitcount reference but misses checking if the resource group has been flagged for deletion. If resctrl is unmounted while a pseudo-lock device file is open, rmdir_all_sub() will observe the active waitcount and defer the deletion to the last reference holder. When the device file descriptor is subsequently closed, pseudo_lock_dev_release() decrements the waitcount without inspecting nor acting on the RDT_DELETED flag. Neither the pseudo-lock memory cleanup nor the final resource group removal are executed.

3) rdtgroup_pseudo_lock_remove() calls debugfs_remove_recursive() that will wait for any existing debugfs users to complete before it can do cleanup. Since the pseudo-locking debugfs handlers take rdtgroup_mutex it is required that this debugfs_remove_recursive() is not called with rdtgroup_mutex held, yet rmdir_all_sub() does so.

4) A pseudo-locked group's RMID is freed when it is created. On unmount rmdir_all_sub() unconditionally frees all RMID of all groups, resulting in a double-free of the pseudo-locked group's RMID. The additional consequence of this is that the original free results in the pseudo-locked group's RMID being added to the rmid_free_lru linked list and the second free then attempts to add the same RMID entry to the rmid_free_lru again.

Fix pseudo-locking lifetime handling by separating the pseudo-locking infrastructure removal from the pseudo-locking region memory teardown, and splitting pseudo-locking infrastructure removal into what needs rdtgroup_mutex protection and what does not. With this separation an unmount of resctrl fs can proceed to remove the pseudo-locking infrastructure of a pseudo-locked region since the global infrastructure it depends on will be removed soon after (resctrl_unmount()-> resctrl_fs_teardown()->rdt_pseudo_lock_release()). Any active users of the pseudo-locked region via an open file can complete safely afterwards and only need to do the pseudo-locked region specific memory teardown.

The new RDT_DELETED_PLR resource group flag communicates to waiters whether the pseudo-locked region's infrastructure has already been removed.

Fixes 746e08590b86 ("x86/intel_rdt: Create character device exposing pseudo-locked region")
Fixes e0bdfe8e36f3 ("x86/intel_rdt: Support creation/removal of pseudo-locked region")
Assisted-by GitHub_Copilot:gemini-3.1-pro

---
Changes since V2:
- New patch

---
 fs/resctrl/internal.h    | 12 +++++++++++
 fs/resctrl/pseudo_lock.c | 44 +++++++++++++++++++++++++++++++++-------
 fs/resctrl/rdtgroup.c    | 35 ++++++++++++++++++--------------
 3 files changed, 69 insertions(+), 22 deletions(-)
fs/resctrl/internal.h +12 -0
index 48af75b9dc85..e7e415ee7766 100644
@@ -234,6 +234,15 @@ struct rdtgroup {
 
 /* rdtgroup.flags */
 #define	RDT_DELETED		1
+/*
+ * RDT_DELETED_PLR is set when the pseudo-locked group's infrastructure
+ * (its associated device, debugfs files, etc.) has been deleted via
+ * rdtgroup_pseudo_lock_remove(). This can be done while there are
+ * references to the pseudo-locked region since the pseudo-locked region
+ * self is freed separately via pseudo_lock_free() after there are no more
+ * references.
+ */
+#define	RDT_DELETED_PLR		2
 
 /* rftype.flags */
 #define RFTYPE_FLAGS_CPUS_LIST	1
@@ -334,6 +343,7 @@ void rdt_last_cmd_puts(const char *s);
 __printf(1, 2)
 void rdt_last_cmd_printf(const char *fmt, ...);
 
+void rdtgroup_remove(struct rdtgroup *rdtgrp);
 struct rdtgroup *rdtgroup_kn_lock_live(struct kernfs_node *kn);
 
 void rdtgroup_kn_unlock(struct kernfs_node *kn);
@@ -484,6 +494,7 @@ void rdt_pseudo_lock_release(void);
 int rdtgroup_pseudo_lock_create(struct rdtgroup *rdtgrp);
 
 void rdtgroup_pseudo_lock_remove(struct rdtgroup *rdtgrp);
+void pseudo_lock_free(struct rdtgroup *rdtgrp);
 
 #else
 static inline int rdtgroup_locksetup_enter(struct rdtgroup *rdtgrp)
@@ -514,6 +525,7 @@ static inline int rdtgroup_pseudo_lock_create(struct rdtgroup *rdtgrp)
 }
 
 static inline void rdtgroup_pseudo_lock_remove(struct rdtgroup *rdtgrp) { }
+static inline void pseudo_lock_free(struct rdtgroup *rdtgrp) { }
 #endif /* CONFIG_RESCTRL_FS_PSEUDO_LOCK */
 
 #endif /* _FS_RESCTRL_INTERNAL_H */
fs/resctrl/pseudo_lock.c +37 -7

81 lines not on this page · raw message

fs/resctrl/rdtgroup.c +20 -15
index 6418395877cf..a8b4ac7dd823 100644
@@ -595,7 +595,7 @@ static ssize_t rdtgroup_cpus_write(struct kernfs_open_file *of,
  *
  * Return: void
  */
-static void rdtgroup_remove(struct rdtgroup *rdtgrp)
+void rdtgroup_remove(struct rdtgroup *rdtgrp)
 {
 	if (rdtgrp == &rdtgroup_default)
 		return;
@@ -2606,23 +2606,24 @@ static void rdtgroup_kn_get(struct rdtgroup *rdtgrp, struct kernfs_node *kn)
 
 static void rdtgroup_kn_put(struct rdtgroup *rdtgrp, struct kernfs_node *kn)
 {
-	bool needs_free;
+	bool needs_free = false;
 
 	if (!atomic_dec_and_mutex_lock(&rdtgrp->waitcount, &rdtgroup_mutex)) {
 		kernfs_unbreak_active_protection(kn);
 		return;
 	}
 
-	needs_free = rdtgrp->flags & RDT_DELETED;
+	if (rdtgrp->flags & RDT_DELETED) {
+		needs_free = true;
+		rdtgroup_pseudo_lock_remove(rdtgrp);
+	}
 
 	mutex_unlock(&rdtgroup_mutex);
 
 	kernfs_unbreak_active_protection(kn);
 
 	if (needs_free) {
-		if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
-		    rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)
-			rdtgroup_pseudo_lock_remove(rdtgrp);
+		pseudo_lock_free(rdtgrp);
 		rdtgroup_remove(rdtgrp);
 	}
 }
@@ -2885,10 +2886,6 @@ static void rmdir_all_sub(void)
 		if (rdtgrp == &rdtgroup_default)
 			continue;
 
-		if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
-		    rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)
-			rdtgroup_pseudo_lock_remove(rdtgrp);
-
 		/*
 		 * Give any CPUs back to the default group. We cannot copy
 		 * cpu_online_mask because a CPU might have executed the
@@ -2899,15 +2896,23 @@ static void rmdir_all_sub(void)
 
 		rdtgroup_unassign_cntrs(rdtgrp);
 
-		free_rmid(rdtgrp->closid, rdtgrp->mon.rmid);
-
 		kernfs_remove(rdtgrp->kn);
 		list_del(&rdtgrp->rdtgroup_list);
 
-		if (atomic_read(&rdtgrp->waitcount) != 0)
-			rdtgrp->flags = RDT_DELETED;
-		else
+		if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
+		    rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
+			rdtgroup_pseudo_lock_remove(rdtgrp);
+		} else {
+			/* Pseudo-locked group's RMID is freed during setup. */
+			free_rmid(rdtgrp->closid, rdtgrp->mon.rmid);
+		}
+
+		if (atomic_read(&rdtgrp->waitcount) != 0) {
+			rdtgrp->flags |= RDT_DELETED;
+		} else {
+			pseudo_lock_free(rdtgrp);
 			rdtgroup_remove(rdtgrp);
+		}
 	}
 	/* Notify online CPUs to update per cpu storage and PQR_ASSOC MSR */
 	update_closid_rmid(cpu_online_mask, &rdtgroup_default);
Signature · 1 line
2.50.1
maintainer
#18 lore raw

Re: [PATCH v3 6/9] fs/resctrl: Fix pseudo-locking lifetime handling

Hi Reinette,

Quoted 74 lines · Reinette Chatre

rmdir_all_sub() implements resctrl unmount teardown by iterating through all resource groups. Memory cleanup is coordinated by deferring resource group removal to rdtgroup_kn_put() if there are lingering waiters.

A couple of issues exist in the pseudo-locking lifetime management:

1) rmdir_all_sub() unconditionally invokes rdtgroup_pseudo_lock_remove() on pseudo-locked groups, even if active references remain. When the deferred rdtgroup_kn_put() is finally executed by the last waiter, it erroneously calls rdtgroup_pseudo_lock_remove() a second time, resulting in a NULL-pointer dereference as the pseudo-lock structures have already been freed.

2) pseudo_lock_dev_release() drops the waitcount reference but misses checking if the resource group has been flagged for deletion. If resctrl is unmounted while a pseudo-lock device file is open, rmdir_all_sub() will observe the active waitcount and defer the deletion to the last reference holder. When the device file descriptor is subsequently closed, pseudo_lock_dev_release() decrements the waitcount without inspecting nor acting on the RDT_DELETED flag. Neither the pseudo-lock memory cleanup nor the final resource group removal are executed.

3) rdtgroup_pseudo_lock_remove() calls debugfs_remove_recursive() that will wait for any existing debugfs users to complete before it can do cleanup. Since the pseudo-locking debugfs handlers take rdtgroup_mutex it is required that this debugfs_remove_recursive() is not called with rdtgroup_mutex held, yet rmdir_all_sub() does so.

4) A pseudo-locked group's RMID is freed when it is created. On unmount rmdir_all_sub() unconditionally frees all RMID of all groups, resulting in a double-free of the pseudo-locked group's RMID. The additional consequence of this is that the original free results in the pseudo-locked group's RMID being added to the rmid_free_lru linked list and the second free then attempts to add the same RMID entry to the rmid_free_lru again.

Fix pseudo-locking lifetime handling by separating the pseudo-locking infrastructure removal from the pseudo-locking region memory teardown, and splitting pseudo-locking infrastructure removal into what needs rdtgroup_mutex protection and what does not. With this separation an unmount of resctrl fs can proceed to remove the pseudo-locking infrastructure of a pseudo-locked region since the global infrastructure it depends on will be removed soon after (resctrl_unmount()-> resctrl_fs_teardown()->rdt_pseudo_lock_release()). Any active users of the pseudo-locked region via an open file can complete safely afterwards and only need to do the pseudo-locked region specific memory teardown.

The new RDT_DELETED_PLR resource group flag communicates to waiters whether the pseudo-locked region's infrastructure has already been removed.

Fixes 746e08590b86 ("x86/intel_rdt: Create character device exposing pseudo-locked region")
Fixes e0bdfe8e36f3 ("x86/intel_rdt: Support creation/removal of pseudo-locked region")
Assisted-by GitHub_Copilot:gemini-3.1-pro

---
Changes since V2:
- New patch

---
 fs/resctrl/internal.h    | 12 +++++++++++
 fs/resctrl/pseudo_lock.c | 44 +++++++++++++++++++++++++++++++++-------
 fs/resctrl/rdtgroup.c    | 35 ++++++++++++++++++--------------
 3 files changed, 69 insertions(+), 22 deletions(-)
fs/resctrl/internal.h +0 -0
index 48af75b9dc85..e7e415ee7766 100644
@@ -234,6 +234,15 @@ struct rdtgroup {
 
 /* rdtgroup.flags */

fs/resctrl/internal.h struct rdtgroup { · line 245

Quoted 10 lines
fs/resctrl/internal.h +9 -0
 #define	RDT_DELETED		1
+/*
+ * RDT_DELETED_PLR is set when the pseudo-locked group's infrastructure
+ * (its associated device, debugfs files, etc.) has been deleted via
+ * rdtgroup_pseudo_lock_remove(). This can be done while there are
+ * references to the pseudo-locked region since the pseudo-locked region
+ * self is freed separately via pseudo_lock_free() after there are no more
+ * references.
+ */
+#define	RDT_DELETED_PLR		2

I haven't had a proper look at this patch but there are a few places where 'flags = RDT_DELETED' is used rather than 'flags |= RDT_DELETED'. Are these all fine?

Thanks,

Ben

Quoted 199 lines
fs/resctrl/internal.h +3 -0
 /* rftype.flags */
 #define RFTYPE_FLAGS_CPUS_LIST	1
@@ -334,6 +343,7 @@ void rdt_last_cmd_puts(const char *s);
 __printf(1, 2)
 void rdt_last_cmd_printf(const char *fmt, ...);
 
+void rdtgroup_remove(struct rdtgroup *rdtgrp);
 struct rdtgroup *rdtgroup_kn_lock_live(struct kernfs_node *kn);
 
 void rdtgroup_kn_unlock(struct kernfs_node *kn);
@@ -484,6 +494,7 @@ void rdt_pseudo_lock_release(void);
 int rdtgroup_pseudo_lock_create(struct rdtgroup *rdtgrp);
 
 void rdtgroup_pseudo_lock_remove(struct rdtgroup *rdtgrp);
+void pseudo_lock_free(struct rdtgroup *rdtgrp);
 
 #else
 static inline int rdtgroup_locksetup_enter(struct rdtgroup *rdtgrp)
@@ -514,6 +525,7 @@ static inline int rdtgroup_pseudo_lock_create(struct rdtgroup *rdtgrp)
 }
 
 static inline void rdtgroup_pseudo_lock_remove(struct rdtgroup *rdtgrp) { }
+static inline void pseudo_lock_free(struct rdtgroup *rdtgrp) { }
 #endif /* CONFIG_RESCTRL_FS_PSEUDO_LOCK */
 
 #endif /* _FS_RESCTRL_INTERNAL_H */
fs/resctrl/pseudo_lock.c +37 -7
index d1cb0986006e..f9d180eb699e 100644
@@ -333,8 +333,10 @@ static int pseudo_lock_region_alloc(struct pseudo_lock_region *plr)
  *
  * Return: void
  */
-static void pseudo_lock_free(struct rdtgroup *rdtgrp)
+void pseudo_lock_free(struct rdtgroup *rdtgrp)
 {
+	if (!rdtgrp->plr)
+		return;
 	pseudo_lock_region_clear(rdtgrp->plr);
 	kfree(rdtgrp->plr);
 	rdtgrp->plr = NULL;
@@ -928,22 +930,37 @@ void rdtgroup_pseudo_lock_remove(struct rdtgroup *rdtgrp)
 {
 	struct pseudo_lock_region *plr = rdtgrp->plr;
 
+	lockdep_assert_held(&rdtgroup_mutex);
+
+	if (rdtgrp->mode != RDT_MODE_PSEUDO_LOCKSETUP &&
+	    rdtgrp->mode != RDT_MODE_PSEUDO_LOCKED)
+		return;
+
+	if (rdtgrp->flags & RDT_DELETED_PLR)
+		return;
+
+	rdtgrp->flags |= RDT_DELETED_PLR;
+
 	if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP) {
 		/*
 		 * Default group cannot be a pseudo-locked region so we can
 		 * free closid here.
 		 */
 		closid_free(rdtgrp->closid);
-		goto free;
+		return;
 	}
 
 	pseudo_lock_cstates_relax(plr);
-	debugfs_remove_recursive(rdtgrp->plr->debugfs_dir);
+	/*
+	 * Drop rdtgroup_mutex to enable debugfs_remove_recursive() to
+	 * complete as it waits for active users that may be blocked
+	 * waiting on rdtgroup_mutex to complete.
+	 */
+	mutex_unlock(&rdtgroup_mutex);
+	debugfs_remove_recursive(plr->debugfs_dir);
+	mutex_lock(&rdtgroup_mutex);
 	device_destroy(&pseudo_lock_class, MKDEV(pseudo_lock_major, plr->minor));
 	pseudo_lock_minor_release(plr->minor);
-
-free:
-	pseudo_lock_free(rdtgrp);
 }
 
 static int pseudo_lock_dev_open(struct inode *inode, struct file *filp)
@@ -971,6 +988,7 @@ static int pseudo_lock_dev_open(struct inode *inode, struct file *filp)
 static int pseudo_lock_dev_release(struct inode *inode, struct file *filp)
 {
 	struct rdtgroup *rdtgrp;
+	bool needs_free = false;
 
 	mutex_lock(&rdtgroup_mutex);
 	rdtgrp = filp->private_data;
@@ -980,8 +998,20 @@ static int pseudo_lock_dev_release(struct inode *inode, struct file *filp)
 		return -ENODEV;
 	}
 	filp->private_data = NULL;
-	atomic_dec(&rdtgrp->waitcount);
+
+	if (atomic_dec_and_test(&rdtgrp->waitcount) &&
+	    (rdtgrp->flags & RDT_DELETED)) {
+		needs_free = true;
+		rdtgroup_pseudo_lock_remove(rdtgrp);
+	}
+
 	mutex_unlock(&rdtgroup_mutex);
+
+	if (needs_free) {
+		pseudo_lock_free(rdtgrp);
+		rdtgroup_remove(rdtgrp);
+	}
+
 	return 0;
 }
 
fs/resctrl/rdtgroup.c +20 -15
index 6418395877cf..a8b4ac7dd823 100644
@@ -595,7 +595,7 @@ static ssize_t rdtgroup_cpus_write(struct kernfs_open_file *of,
  *
  * Return: void
  */
-static void rdtgroup_remove(struct rdtgroup *rdtgrp)
+void rdtgroup_remove(struct rdtgroup *rdtgrp)
 {
 	if (rdtgrp == &rdtgroup_default)
 		return;
@@ -2606,23 +2606,24 @@ static void rdtgroup_kn_get(struct rdtgroup *rdtgrp, struct kernfs_node *kn)
 
 static void rdtgroup_kn_put(struct rdtgroup *rdtgrp, struct kernfs_node *kn)
 {
-	bool needs_free;
+	bool needs_free = false;
 
 	if (!atomic_dec_and_mutex_lock(&rdtgrp->waitcount, &rdtgroup_mutex)) {
 		kernfs_unbreak_active_protection(kn);
 		return;
 	}
 
-	needs_free = rdtgrp->flags & RDT_DELETED;
+	if (rdtgrp->flags & RDT_DELETED) {
+		needs_free = true;
+		rdtgroup_pseudo_lock_remove(rdtgrp);
+	}
 
 	mutex_unlock(&rdtgroup_mutex);
 
 	kernfs_unbreak_active_protection(kn);
 
 	if (needs_free) {
-		if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
-		    rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)
-			rdtgroup_pseudo_lock_remove(rdtgrp);
+		pseudo_lock_free(rdtgrp);
 		rdtgroup_remove(rdtgrp);
 	}
 }
@@ -2885,10 +2886,6 @@ static void rmdir_all_sub(void)
 		if (rdtgrp == &rdtgroup_default)
 			continue;
 
-		if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
-		    rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)
-			rdtgroup_pseudo_lock_remove(rdtgrp);
-
 		/*
 		 * Give any CPUs back to the default group. We cannot copy
 		 * cpu_online_mask because a CPU might have executed the
@@ -2899,15 +2896,23 @@ static void rmdir_all_sub(void)
 
 		rdtgroup_unassign_cntrs(rdtgrp);
 
-		free_rmid(rdtgrp->closid, rdtgrp->mon.rmid);
-
 		kernfs_remove(rdtgrp->kn);
 		list_del(&rdtgrp->rdtgroup_list);
 
-		if (atomic_read(&rdtgrp->waitcount) != 0)
-			rdtgrp->flags = RDT_DELETED;
-		else
+		if (rdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
+		    rdtgrp->mode == RDT_MODE_PSEUDO_LOCKED) {
+			rdtgroup_pseudo_lock_remove(rdtgrp);
+		} else {
+			/* Pseudo-locked group's RMID is freed during setup. */
+			free_rmid(rdtgrp->closid, rdtgrp->mon.rmid);
+		}
+
+		if (atomic_read(&rdtgrp->waitcount) != 0) {
+			rdtgrp->flags |= RDT_DELETED;
+		} else {
+			pseudo_lock_free(rdtgrp);
 			rdtgroup_remove(rdtgrp);
+		}
 	}
 	/* Notify online CPUs to update per cpu storage and PQR_ASSOC MSR */
 	update_closid_rmid(cpu_online_mask, &rdtgroup_default);
author maintainer
#19 lore raw

Re: [PATCH v3 6/9] fs/resctrl: Fix pseudo-locking lifetime handling

Hi Ben,

Quoted 1 line · Ben Horgan

On 5/22/26 20:15, Reinette Chatre wrote:

...

Quoted 6 lines
Quoted 6 lines
fs/resctrl/internal.h +0 -0
index 48af75b9dc85..e7e415ee7766 100644
@@ -234,6 +234,15 @@ struct rdtgroup {
 
Quoted 15 lines

fs/resctrl/internal.h struct rdtgroup { · line 245

Quoted 11 lines
fs/resctrl/internal.h +9 -0
 /* rdtgroup.flags */
 #define	RDT_DELETED		1
+/*
+ * RDT_DELETED_PLR is set when the pseudo-locked group's infrastructure
+ * (its associated device, debugfs files, etc.) has been deleted via
+ * rdtgroup_pseudo_lock_remove(). This can be done while there are
+ * references to the pseudo-locked region since the pseudo-locked region
+ * self is freed separately via pseudo_lock_free() after there are no more
+ * references.
+ */
+#define	RDT_DELETED_PLR		2

I haven't had a proper look at this patch but there are a few places where 'flags = RDT_DELETED' is used rather than 'flags |= RDT_DELETED'. Are these all fine?

These "deleted" flags are not independent. The pseudo-locking infrastructure can/should only be deleted if the resource group has already been deleted or is about to be deleted in the same flow. This relationship is clear when looking at the RDT_DELETED_PLR assignment in pseudo_lock_dev_release() and rdtgroup_kn_put() where RDT_DELETED_PLR setting depends on RDT_DELETED already being set. There is one place in rmdir_all_sub() where the order is swapped with pseudo-locking infrastructure is torn down first, thus setting RDT_DELETED_PLR _before_ RDT_DELETED. This is why rmdir_all_sub() uses flags |= RDT_DELETED instead of assignment.

Even so, the issue [1] reported by Sashiko is real and how to fix that one is not obvious to me at this time. These issues uncovered so far are races that can be triggered by user space stress usage of the pseudo-locking files that is only possible on some very specific ten-year old hardware. Such usage is contrary to the pseudo-locking usage model so I am currently considering pulling this fix from the series. To add to this Sashiko reported another [2] issue in this area while reviewing the resubmission aimed to get review feedback on the last three patches.

Reinette

[1] https://sashiko.dev/#/patchset/cover.1779476724.git.reinette.chatre%40intel.com?part=6
[2] https://sashiko.dev/#/patchset/cover.1779834897.git.reinette.chatre%40intel.com?part=2

author maintainer
#20 lore raw

PATCH v3 7/9 fs/resctrl: Prevent deadlock and use-after-free in info file handlers

In reply to Reinette Chatre

resctrl provides files under the info/ directory to expose global configuration and capabilities to userspace. These files are instantiated statically during filesystem mount and expose data associated with internal schema structures via kernfs private pointers.

A potential deadlock exists between userspace readers of these info files and the unmount filesystem teardown process. Reading an info file invokes kernfs which acquires an active reference, after which the handler typically attempts to acquire the rdtgroup_mutex. Concurrently, unmounting the filesystem holds the rdtgroup_mutex and then attempts to recursively remove the info kernfs nodes involving kernfs_drain() which blocks until all active references are released. Another problem exists where info files might be accessed from an outdated mount if the filesystem is unmounted and remounted during a reader's execution, leading to a use-after-free when reading the now-deleted private schema data.

Introduce info_kn_lock() and info_kn_unlock() helpers to coordinate locking across all info handlers. These helpers mirror similar logic used by resource group handlers by deliberately breaking the kernfs active protection before attempting to acquire the rdtgroup_mutex, preventing the deadlock. To guard against the vulnerability from rapid mount cycling, info_kn_lock() securely walks the parent lineage of the kernfs node under an RCU section to confirm the node belongs to the globally active root before permitting the operation to proceed. Convert all info file handlers to use this helper and only de-reference the schema after it determined safe to do so.

Make no attempt to output error message to last_cmd_status on failure since failure implies there is no filesystem with which to display error to user space.

---
Changes since V2:
- New patch

---
 fs/resctrl/ctrlmondata.c |  38 ++++----
 fs/resctrl/internal.h    |   3 +-
 fs/resctrl/monitor.c     |  48 +++++-----
 fs/resctrl/rdtgroup.c    | 192 ++++++++++++++++++++++++++++++++-------
 4 files changed, 203 insertions(+), 78 deletions(-)
fs/resctrl/ctrlmondata.c +20 -18
index 9a7dfc48cb2e..b95bf6208be2 100644
@@ -769,10 +769,12 @@ int rdtgroup_mondata_show(struct seq_file *m, void *arg)
 int resctrl_io_alloc_show(struct kernfs_open_file *of, struct seq_file *seq, void *v)
 {
 	struct resctrl_schema *s = rdt_kn_parent_priv(of->kn);
-	struct rdt_resource *r = s->res;
+	struct rdt_resource *r;
 
-	mutex_lock(&rdtgroup_mutex);
+	if (!info_kn_lock(of->kn))
+		return -ENOENT;
 
+	r = s->res;
 	if (r->cache.io_alloc_capable) {
 		if (resctrl_arch_get_io_alloc_enabled(r))
 			seq_puts(seq, "enabled\n");
@@ -782,7 +784,7 @@ int resctrl_io_alloc_show(struct kernfs_open_file *of, struct seq_file *seq, voi
 		seq_puts(seq, "not supported\n");
 	}
 
-	mutex_unlock(&rdtgroup_mutex);
+	info_kn_unlock(of->kn);
 
 	return 0;
 }
@@ -847,7 +849,7 @@ ssize_t resctrl_io_alloc_write(struct kernfs_open_file *of, char *buf,
 			       size_t nbytes, loff_t off)
 {
 	struct resctrl_schema *s = rdt_kn_parent_priv(of->kn);
-	struct rdt_resource *r = s->res;
+	struct rdt_resource *r;
 	char const *grp_name;
 	u32 io_alloc_closid;
 	bool enable;
@@ -857,9 +859,10 @@ ssize_t resctrl_io_alloc_write(struct kernfs_open_file *of, char *buf,
 	if (ret)
 		return ret;
 
-	cpus_read_lock();
-	mutex_lock(&rdtgroup_mutex);
+	if (!info_kn_lock(of->kn))
+		return -ENOENT;
 
+	r = s->res;
 	rdt_last_cmd_clear();
 
 	if (!r->cache.io_alloc_capable) {
@@ -907,8 +910,7 @@ ssize_t resctrl_io_alloc_write(struct kernfs_open_file *of, char *buf,
 	}
 
 out_unlock:
-	mutex_unlock(&rdtgroup_mutex);
-	cpus_read_unlock();
+	info_kn_unlock(of->kn);
 
 	return ret ?: nbytes;
 }
@@ -916,14 +918,15 @@ ssize_t resctrl_io_alloc_write(struct kernfs_open_file *of, char *buf,
 int resctrl_io_alloc_cbm_show(struct kernfs_open_file *of, struct seq_file *seq, void *v)
 {
 	struct resctrl_schema *s = rdt_kn_parent_priv(of->kn);
-	struct rdt_resource *r = s->res;
+	struct rdt_resource *r;
 	int ret = 0;
 
-	cpus_read_lock();
-	mutex_lock(&rdtgroup_mutex);
+	if (!info_kn_lock(of->kn))
+		return -ENOENT;
 
 	rdt_last_cmd_clear();
 
+	r = s->res;
 	if (!r->cache.io_alloc_capable) {
 		rdt_last_cmd_printf("io_alloc is not supported on %s\n", s->name);
 		ret = -ENODEV;
@@ -945,8 +948,7 @@ int resctrl_io_alloc_cbm_show(struct kernfs_open_file *of, struct seq_file *seq,
 	show_doms(seq, s, NULL, resctrl_io_alloc_closid(r));
 
 out_unlock:
-	mutex_unlock(&rdtgroup_mutex);
-	cpus_read_unlock();
+	info_kn_unlock(of->kn);
 	return ret;
 }
 
@@ -1013,7 +1015,7 @@ ssize_t resctrl_io_alloc_cbm_write(struct kernfs_open_file *of, char *buf,
 				   size_t nbytes, loff_t off)
 {
 	struct resctrl_schema *s = rdt_kn_parent_priv(of->kn);
-	struct rdt_resource *r = s->res;
+	struct rdt_resource *r;
 	u32 io_alloc_closid;
 	int ret = 0;
 
@@ -1023,10 +1025,11 @@ ssize_t resctrl_io_alloc_cbm_write(struct kernfs_open_file *of, char *buf,
 
 	buf[nbytes - 1] = '\0';
 
-	cpus_read_lock();
-	mutex_lock(&rdtgroup_mutex);
+	if (!info_kn_lock(of->kn))
+		return -ENOENT;
 	rdt_last_cmd_clear();
 
+	r = s->res;
 	if (!r->cache.io_alloc_capable) {
 		rdt_last_cmd_printf("io_alloc is not supported on %s\n", s->name);
 		ret = -ENODEV;
@@ -1051,8 +1054,7 @@ ssize_t resctrl_io_alloc_cbm_write(struct kernfs_open_file *of, char *buf,
 out_clear_configs:
 	rdt_staged_configs_clear();
 out_unlock:
-	mutex_unlock(&rdtgroup_mutex);
-	cpus_read_unlock();
+	info_kn_unlock(of->kn);
 
 	return ret ?: nbytes;
 }
fs/resctrl/internal.h +2 -1
index e7e415ee7766..4e3173f25e92 100644
@@ -345,8 +345,9 @@ void rdt_last_cmd_printf(const char *fmt, ...);
 
 void rdtgroup_remove(struct rdtgroup *rdtgrp);
 struct rdtgroup *rdtgroup_kn_lock_live(struct kernfs_node *kn);
-
 void rdtgroup_kn_unlock(struct kernfs_node *kn);
+bool info_kn_lock(struct kernfs_node *kn);
+void info_kn_unlock(struct kernfs_node *kn);
 
 int rdtgroup_kn_mode_restrict(struct rdtgroup *r, const char *name);
 
fs/resctrl/monitor.c +24 -24

144 lines not on this page · raw message

fs/resctrl/rdtgroup.c +157 -35

395 lines not on this page · raw message

Signature · 1 line
2.50.1
author maintainer
#21 lore raw

PATCH v3 8/9 x86/resctrl: Ensure domain fully initialized before placed on RCU list

In reply to Reinette Chatre

A resctrl domain consists of the domain structure self that includes pointers to dynamically allocated filesystem as well as architecture specific data. For example, the L3 monitoring domain structure consists of the architecture specific struct rdt_hw_l3_mon_domain that contains the dynamically allocated rdt_hw_l3_mon_domain::arch_mbm_states architectural state and the embedded struct rdt_l3_mon_domain contains the dynamically allocated rdt_l3_mon_domain::mbm_states resctrl fs state.

The domains are placed on an RCU protected list so that readers could access domains via cpus_read_lock() or from an RCU read-side critical section.

A reader accessing a domain via the RCU list expects that the domain and all its dynamically allocated data is accessible. Only place domain on RCU list when all its dynamically allocated data is ready, similarly unlink from RCU list before removing any of its dynamically allocated data.

There are no readers accessing a domain via RCU list. Ensure safety of access when such reader arrives.

---
Changes since V2:
- New patch

---
 arch/x86/kernel/cpu/resctrl/core.c      | 18 +++++++-----------
 arch/x86/kernel/cpu/resctrl/intel_aet.c |  5 ++---
 2 files changed, 9 insertions(+), 14 deletions(-)
arch/x86/kernel/cpu/resctrl/core.c +7 -11
index 9c01d2562b7a..bca782050198 100644
@@ -515,14 +515,12 @@ static void domain_add_cpu_ctrl(int cpu, struct rdt_resource *r)
 		return;
 	}
 
-	list_add_tail_rcu(&d->hdr.list, add_pos);
-
 	err = resctrl_online_ctrl_domain(r, d);
 	if (err) {
-		list_del_rcu(&d->hdr.list);
-		synchronize_rcu();
 		ctrl_domain_free(hw_dom);
+		return;
 	}
+	list_add_tail_rcu(&d->hdr.list, add_pos);
 }
 
 static void l3_mon_domain_setup(int cpu, int id, struct rdt_resource *r, struct list_head *add_pos)
@@ -556,14 +554,12 @@ static void l3_mon_domain_setup(int cpu, int id, struct rdt_resource *r, struct
 		return;
 	}
 
-	list_add_tail_rcu(&d->hdr.list, add_pos);
-
 	err = resctrl_online_mon_domain(r, &d->hdr);
 	if (err) {
-		list_del_rcu(&d->hdr.list);
-		synchronize_rcu();
 		l3_mon_domain_free(hw_dom);
+		return;
 	}
+	list_add_tail_rcu(&d->hdr.list, add_pos);
 }
 
 static void domain_add_cpu_mon(int cpu, struct rdt_resource *r)
@@ -642,9 +638,9 @@ static void domain_remove_cpu_ctrl(int cpu, struct rdt_resource *r)
 	d = container_of(hdr, struct rdt_ctrl_domain, hdr);
 	hw_dom = resctrl_to_arch_ctrl_dom(d);
 
-	resctrl_offline_ctrl_domain(r, d);
 	list_del_rcu(&hdr->list);
 	synchronize_rcu();
+	resctrl_offline_ctrl_domain(r, d);
 
 	/*
 	 * rdt_ctrl_domain "d" is going to be freed below, so clear
@@ -689,9 +685,9 @@ static void domain_remove_cpu_mon(int cpu, struct rdt_resource *r)
 
 		d = container_of(hdr, struct rdt_l3_mon_domain, hdr);
 		hw_dom = resctrl_to_arch_mon_dom(d);
-		resctrl_offline_mon_domain(r, hdr);
 		list_del_rcu(&hdr->list);
 		synchronize_rcu();
+		resctrl_offline_mon_domain(r, hdr);
 		l3_mon_domain_free(hw_dom);
 		break;
 	}
@@ -702,9 +698,9 @@ static void domain_remove_cpu_mon(int cpu, struct rdt_resource *r)
 			return;
 
 		pkgd = container_of(hdr, struct rdt_perf_pkg_mon_domain, hdr);
-		resctrl_offline_mon_domain(r, hdr);
 		list_del_rcu(&hdr->list);
 		synchronize_rcu();
+		resctrl_offline_mon_domain(r, hdr);
 		kfree(pkgd);
 		break;
 	}
arch/x86/kernel/cpu/resctrl/intel_aet.c +2 -3
index 89b8b619d5d5..c22c3cf5167d 100644
@@ -398,12 +398,11 @@ void intel_aet_mon_domain_setup(int cpu, int id, struct rdt_resource *r,
 	d->hdr.type = RESCTRL_MON_DOMAIN;
 	d->hdr.rid = RDT_RESOURCE_PERF_PKG;
 	cpumask_set_cpu(cpu, &d->hdr.cpu_mask);
-	list_add_tail_rcu(&d->hdr.list, add_pos);
 
 	err = resctrl_online_mon_domain(r, &d->hdr);
 	if (err) {
-		list_del_rcu(&d->hdr.list);
-		synchronize_rcu();
 		kfree(d);
+		return;
 	}
+	list_add_tail_rcu(&d->hdr.list, add_pos);
 }
Signature · 1 line
2.50.1
author maintainer
#22 lore raw

Re: [PATCH v3 8/9] x86/resctrl: Ensure domain fully initialized before placed on RCU list

Quoted 8 lines · Reinette Chatre

static void l3_mon_domain_setup(int cpu, int id, struct rdt_resource *r, struct list_head *add_pos)

arch/x86/kernel/cpu/resctrl/core.c +0 -2
@@ -556,14 +554,12 @@ static void l3_mon_domain_setup(int cpu, int id, struct rdt_resource *r, struct
 		return;
 	}
 
-	list_add_tail_rcu(&d->hdr.list, add_pos);
-
 	err = resctrl_online_mon_domain(r, &d->hdr);

arch/x86/kernel/cpu/resctrl/core.c static void l3_mon_domain_setup(int cpu, int id, struct rdt_resource *r, struct · line 565

Quoted 10 lines
arch/x86/kernel/cpu/resctrl/core.c +2 -2
 	if (err) {
-		list_del_rcu(&d->hdr.list);
-		synchronize_rcu();
 		l3_mon_domain_free(hw_dom);
+		return;
 	}
+	list_add_tail_rcu(&d->hdr.list, add_pos);
 }
 
 static void domain_add_cpu_mon(int cpu, struct rdt_resource *r)

I resubmitted the last three patches of series to obtain Sashiko review [1] and respond to that feedback here:

a) Sashiko: "Does this reordering expose the monitor directories to userspace before the domain is actually added to the RCU list?"

Yes. As pointed out by Sashiko there is a short time where the monitoring data files may be exposed to user space before the monitoring domain is added to the RCU list. Also pointed out by Sashiko, if user attempts to read from such file it will return -ENOENT.

This behavior looks correct and acceptable to me.

b) Sashiko: "This is a pre-existing issue, but does resctrl_find_domain() safely traverse the RCU list?
Yes, this is safe because resctrl_find_domain() is run with cpus_read_lock() that ensures the list can be traversed safely because the list can only be modified with CPU write lock held.
One improvement to resctrl that would help support this is to replace the "list_for_each()" domain list traversals with something like:
list_for_each_entry_rcu(pos, head, member, lockdep_is_cpus_held())
Doing something like above would help document why such list traversal outside of RCU read-side critical section is safe.

Reinette

[1] https://sashiko.dev/#/patchset/cover.1779834897.git.reinette.chatre%40intel.com

reviewer
#23 lore raw

Re: [PATCH v3 8/9] x86/resctrl: Ensure domain fully initialized before placed on RCU list

Hi Reinette,

Quoted 28 lines · Reinette Chatre
Quoted 8 lines · Reinette Chatre

static void l3_mon_domain_setup(int cpu, int id, struct rdt_resource *r, struct list_head *add_pos)

arch/x86/kernel/cpu/resctrl/core.c +0 -2
@@ -556,14 +554,12 @@ static void l3_mon_domain_setup(int cpu, int id, struct rdt_resource *r, struct
  		return;
  	}
 
-	list_add_tail_rcu(&d->hdr.list, add_pos);
-
  	err = resctrl_online_mon_domain(r, &d->hdr);

arch/x86/kernel/cpu/resctrl/core.c static void l3_mon_domain_setup(int cpu, int id, struct rdt_resource *r, struct · line 565

Quoted 10 lines
arch/x86/kernel/cpu/resctrl/core.c +2 -2
  	if (err) {
-		list_del_rcu(&d->hdr.list);
-		synchronize_rcu();
  		l3_mon_domain_free(hw_dom);
+		return;
  	}
+	list_add_tail_rcu(&d->hdr.list, add_pos);
  }
 
  static void domain_add_cpu_mon(int cpu, struct rdt_resource *r)

I resubmitted the last three patches of series to obtain Sashiko review [1] and respond to that feedback here:

a) Sashiko: "Does this reordering expose the monitor directories to userspace before the domain is actually added to the RCU list?"

Quoted 6 lines

Yes. As pointed out by Sashiko there is a short time where the monitoring data files may be exposed to user space before the monitoring domain is added to the RCU list. Also pointed out by Sashiko, if user attempts to read from such file it will return -ENOENT.

This behavior looks correct and acceptable to me.

Yes. I agree,

Quoted 11 lines

b) Sashiko: "This is a pre-existing issue, but does resctrl_find_domain() safely traverse the RCU list?
Yes, this is safe because resctrl_find_domain() is run with cpus_read_lock() that ensures the list can be traversed safely because the list can only be modified with CPU write lock held.
One improvement to resctrl that would help support this is to replace the "list_for_each()" domain list traversals with something like:
list_for_each_entry_rcu(pos, head, member, lockdep_is_cpus_held())
Doing something like above would help document why such list traversal outside of RCU read-side critical section is safe.

Are you planning to make this change at this time? It appears to be a corner case, and we haven’t observed it in real-world scenarios.

Thanks
Babu

author maintainer
#24 lore raw

Re: [PATCH v3 8/9] x86/resctrl: Ensure domain fully initialized before placed on RCU list

Hi Babu,

Quoted 27 lines · Babu Moger
Quoted 26 lines · Reinette Chatre
Quoted 6 lines · Reinette Chatre

  static void l3_mon_domain_setup(int cpu, int id, struct rdt_resource *r, struct list_head *add_pos)

arch/x86/kernel/cpu/resctrl/core.c +0 -1
@@ -556,14 +554,12 @@ static void l3_mon_domain_setup(int cpu, int id, struct rdt_resource *r, struct
           return;
       }
   -    list_add_tail_rcu(&d->hdr.list, add_pos);
-

arch/x86/kernel/cpu/resctrl/core.c static void l3_mon_domain_setup(int cpu, int id, struct rdt_resource *r, struct · line 564

Quoted 10 lines
arch/x86/kernel/cpu/resctrl/core.c +2 -2
       err = resctrl_online_mon_domain(r, &d->hdr);
       if (err) {
-        list_del_rcu(&d->hdr.list);
-        synchronize_rcu();
           l3_mon_domain_free(hw_dom);
+        return;
       }
+    list_add_tail_rcu(&d->hdr.list, add_pos);
   }
     static void domain_add_cpu_mon(int cpu, struct rdt_resource *r)

I resubmitted the last three patches of series to obtain Sashiko review [1] and respond to that feedback here:

a) Sashiko: "Does this reordering expose the monitor directories to userspace before the domain is actually added to the RCU list?"

Quoted 8 lines
Quoted 6 lines

    Yes. As pointed out by Sashiko there is a short time where the monitoring data files may be exposed to user space before the monitoring domain is added to the RCU list. Also pointed out by Sashiko, if user attempts to read from such file it will return -ENOENT.

    This behavior looks correct and acceptable to me.

Yes. I agree,

Thank you for taking a look.

Quoted 13 lines
Quoted 12 lines

b) Sashiko: "This is a pre-existing issue, but does resctrl_find_domain() safely traverse the RCU list?
    Yes, this is safe because resctrl_find_domain() is run with cpus_read_lock() that ensures the list can be traversed safely because the list can only be modified with CPU write lock held.
    One improvement to resctrl that would help support this is to replace the "list_for_each()" domain list traversals with something like:
        list_for_each_entry_rcu(pos, head, member, lockdep_is_cpus_held())
    Doing something like above would help document why such list traversal outside of RCU read-side critical section is safe.

Quoted 2 lines

Are you planning to make this change at this time? It appears to be a corner case, and we haven’t observed it in real-world scenarios.

I am considering it, yes, and was planning to make the change first to determine the impact on this series before deciding. This is not a functional change but instead making the code conform to best practice that implicitly documents why it is safe to traverse the list outside of an RCU read-side critical section. I think this would be a nice addition to resctrl. Including it in this series may actually help reviewers consider all the flows involved in these races. Do you think this should be deferred? resctrl cleanup work is not popular [2] ...

Reinette

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

#25 lore raw

Re: [PATCH v3 8/9] x86/resctrl: Ensure domain fully initialized before placed on RCU list

Hi Reinette,

Quoted 57 lines · Reinette Chatre

Hi Babu,

Quoted 27 lines · Babu Moger
Quoted 26 lines · Reinette Chatre
Quoted 6 lines · Reinette Chatre

  static void l3_mon_domain_setup(int cpu, int id, struct rdt_resource *r, struct list_head *add_pos)

arch/x86/kernel/cpu/resctrl/core.c +0 -1
@@ -556,14 +554,12 @@ static void l3_mon_domain_setup(int cpu, int id, struct rdt_resource *r, struct
           return;
       }
   -    list_add_tail_rcu(&d->hdr.list, add_pos);
-

arch/x86/kernel/cpu/resctrl/core.c static void l3_mon_domain_setup(int cpu, int id, struct rdt_resource *r, struct · line 564

Quoted 10 lines
arch/x86/kernel/cpu/resctrl/core.c +2 -2
       err = resctrl_online_mon_domain(r, &d->hdr);
       if (err) {
-        list_del_rcu(&d->hdr.list);
-        synchronize_rcu();
           l3_mon_domain_free(hw_dom);
+        return;
       }
+    list_add_tail_rcu(&d->hdr.list, add_pos);
   }
     static void domain_add_cpu_mon(int cpu, struct rdt_resource *r)

I resubmitted the last three patches of series to obtain Sashiko review [1] and respond to that feedback here:

a) Sashiko: "Does this reordering expose the monitor directories to userspace before the domain is actually added to the RCU list?"

Quoted 8 lines
Quoted 6 lines

    Yes. As pointed out by Sashiko there is a short time where the monitoring data files may be exposed to user space before the monitoring domain is added to the RCU list. Also pointed out by Sashiko, if user attempts to read from such file it will return -ENOENT.

    This behavior looks correct and acceptable to me.

Yes. I agree,

Thank you for taking a look.

Quoted 13 lines
Quoted 12 lines

b) Sashiko: "This is a pre-existing issue, but does resctrl_find_domain() safely traverse the RCU list?
    Yes, this is safe because resctrl_find_domain() is run with cpus_read_lock() that ensures the list can be traversed safely because the list can only be modified with CPU write lock held.
    One improvement to resctrl that would help support this is to replace the "list_for_each()" domain list traversals with something like:
        list_for_each_entry_rcu(pos, head, member, lockdep_is_cpus_held()) Doing something like above would help document why such list traversal outside of RCU read-side critical section is safe.

Quoted 2 lines

Are you planning to make this change at this time? It appears to be a corner case, and we haven’t observed it in real-world scenarios.

Quoted 6 lines

I am considering it, yes, and was planning to make the change first to determine the impact on this series before deciding. This is not a functional change but instead making the code conform to best practice that implicitly documents why it is safe to traverse the list outside of an RCU read-side critical section. I think this would be a nice addition to resctrl. Including it in this series may actually help reviewers consider all the flows involved in these races. Do you think this should be deferred? resctrl cleanup work is not popular [2] ...

If it’s as simple as just using the new call (list_for_each_entry_rcu()), then that should be fine.

I think this is a good candidate for deferral.

Sashiko appears to be reporting every possible scenario, so we may need to decide what should be prioritized in such cases. Finally, it is your(maintainers) call.

Thanks,
Babu

Reviewed-by
#26 lore raw

Re: [PATCH v3 8/9] x86/resctrl: Ensure domain fully initialized before placed on RCU list

In reply to Reinette Chatre
Quoted 28 lines · Reinette Chatre
Quoted 8 lines · Reinette Chatre

static void l3_mon_domain_setup(int cpu, int id, struct rdt_resource *r, struct list_head *add_pos)

arch/x86/kernel/cpu/resctrl/core.c +0 -2
@@ -556,14 +554,12 @@ static void l3_mon_domain_setup(int cpu, int id, struct rdt_resource *r, struct
  		return;
  	}
 
-	list_add_tail_rcu(&d->hdr.list, add_pos);
-
  	err = resctrl_online_mon_domain(r, &d->hdr);

arch/x86/kernel/cpu/resctrl/core.c static void l3_mon_domain_setup(int cpu, int id, struct rdt_resource *r, struct · line 565

Quoted 10 lines
arch/x86/kernel/cpu/resctrl/core.c +2 -2
  	if (err) {
-		list_del_rcu(&d->hdr.list);
-		synchronize_rcu();
  		l3_mon_domain_free(hw_dom);
+		return;
  	}
+	list_add_tail_rcu(&d->hdr.list, add_pos);
  }
 
  static void domain_add_cpu_mon(int cpu, struct rdt_resource *r)

I resubmitted the last three patches of series to obtain Sashiko review [1] and respond to that feedback here:

a) Sashiko: "Does this reordering expose the monitor directories to userspace before the domain is actually added to the RCU list?"

Quoted 6 lines

Yes. As pointed out by Sashiko there is a short time where the monitoring data files may be exposed to user space before the monitoring domain is added to the RCU list. Also pointed out by Sashiko, if user attempts to read from such file it will return -ENOENT.

This behavior looks correct and acceptable to me.

It might not return -ENOENT because the read will be blocked by cpus_read_lock() and after the reader has grabed the cpus_read_lock(), the reader will see the newly added domain on the list:

CPU 0 (writer)                            CPU 1 (reader)
==========================                ==========================
cpus_write_lock held                      cpus_read_lock() **BLOCKED**

mkdir mondata files
list_add_tail_rcu()
cpus_write_unlock cpus_read_lock() returns
lock(rdtgroup_mutex) resctrl_find_domain() **see
a new complete domain**
unlock(rdtgroup_mutex)
cpus_read_unlock()

thanks,
Chenyu

author maintainer
#27 lore raw

Re: [PATCH v3 8/9] x86/resctrl: Ensure domain fully initialized before placed on RCU list

Hi Chenyu,

Quoted 38 lines · Yu C
Quoted 26 lines · Reinette Chatre
Quoted 6 lines · Reinette Chatre

  static void l3_mon_domain_setup(int cpu, int id, struct rdt_resource *r, struct list_head *add_pos)

arch/x86/kernel/cpu/resctrl/core.c +0 -1
@@ -556,14 +554,12 @@ static void l3_mon_domain_setup(int cpu, int id, struct rdt_resource *r, struct
           return;
       }
   -    list_add_tail_rcu(&d->hdr.list, add_pos);
-

arch/x86/kernel/cpu/resctrl/core.c static void l3_mon_domain_setup(int cpu, int id, struct rdt_resource *r, struct · line 564

Quoted 10 lines
arch/x86/kernel/cpu/resctrl/core.c +2 -2
       err = resctrl_online_mon_domain(r, &d->hdr);
       if (err) {
-        list_del_rcu(&d->hdr.list);
-        synchronize_rcu();
           l3_mon_domain_free(hw_dom);
+        return;
       }
+    list_add_tail_rcu(&d->hdr.list, add_pos);
   }
     static void domain_add_cpu_mon(int cpu, struct rdt_resource *r)

I resubmitted the last three patches of series to obtain Sashiko review [1] and respond to that feedback here:

a) Sashiko: "Does this reordering expose the monitor directories to userspace before the domain is actually added to the RCU list?"

Quoted 6 lines

    Yes. As pointed out by Sashiko there is a short time where the monitoring data files may be exposed to user space before the monitoring domain is added to the RCU list. Also pointed out by Sashiko, if user attempts to read from such file it will return -ENOENT.

    This behavior looks correct and acceptable to me.

It might not return -ENOENT because the read will be blocked by cpus_read_lock() and after the reader has grabed the cpus_read_lock(), the reader will see the newly added domain on the list:

Quoted 10 lines

CPU 0 (writer)                            CPU 1 (reader) ==========================                ========================== cpus_write_lock held                      cpus_read_lock() **BLOCKED**
  mkdir mondata files
  list_add_tail_rcu()
cpus_write_unlock                         cpus_read_lock() returns lock(rdtgroup_mutex) resctrl_find_domain() **see a new complete domain** unlock(rdtgroup_mutex) cpus_read_unlock()

Indeed. Thank you very much for the correction.

Quoted 3 lines

Thank you very much.

Reinette

author maintainer
#28 lore raw

PATCH v3 9/9 fs/resctrl: Fix UAF from worker threads when domains are removed

In reply to Reinette Chatre

The mbm_handle_overflow() and cqm_handle_limbo() workers read event counters and may sleep while doing so. They are scheduled via delayed_work embedded in struct rdt_l3_mon_domain. Architecture allocates and frees these domains from CPU hotplug callbacks under cpus_write_lock(), and the workers acquire cpus_read_lock() to keep the domain alive across their access.

A use-after-free can occur when a worker is blocked waiting for cpus_read_lock() while the hotplug core holds cpus_write_lock():
the architecture frees the rdt_l3_mon_domain that contains the worker's work_struct. When the worker unblocks, the container_of() it performs on the embedded work pointer dereferences freed memory.

Drop cpus_read_lock() from the workers and instead drain pending and in-flight work synchronously before the architecture can free the domain. Since architecture offlines the domain under cpus_write_lock() after it has been unlinked from the RCU list and a grace period has elapsed no new work can be scheduled. The cancel only needs to wait out existing work. Drop rdtgroup_mutex during CPU offline around cancel_delayed_work_sync() so that a worker waiting on the mutex can complete before re-pinning the work on a different CPU.

Fixes 24247aeeabe9 ("x86/intel_rdt/cqm: Improve limbo list processing")

---
Changes since v2:
- Rewrite changelog
- v2 attempted to solve the issue by using is_percpu_thread() within the
worker to learn if CPU worker was running on is going offline. A
Sashiko (https://sashiko.dev/#/patchset/20260515193944.15114-1-tony.luck%40intel.com?part=5)
pointed out that this would not be able to handle the scenario if one
of the hotplug handlers following the resctrl offline handlers failed.
- Some other fixes attempted that failed:
- Switch to accessing domain structure in handler via RCU so that CPU
hotplug lock no longer needed. Use cancel_delayed_work_sync() with
mutex dropped to cancel worker. Running worker from RCU read-side
critical section is a problem since the worker needs to be
able to sleep (mbm_handle_overflow()->mbm_update()->
mbm_update_one_event()->resctrl_arch_mon_ctx_alloc()-> might_sleep())
- Adding a reference count to the domain structure to avoid the worker needing to take CPU hotplug lock. This ended up being very complicated
with the architecture needing new APIs to manage the reference count
which cannot cleanly integrate into MPAM since it uses a single architecture domain structure to contain both the control and monitoring domain structures. Managing the references across mount, unmount,
online, offline, as well as worker self exit resulted in several asymmetrical and complicated paths that were error prone. Locking also proved to be complicated since architecture would need to initiate
domain free that will need to call back into resctrl that will take rdtgroup_mutex which means that references need to be taken/released without locking.

---
 fs/resctrl/monitor.c  | 52 ++++++++++++++++++++++++++++++++++---------
 fs/resctrl/rdtgroup.c | 52 ++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 89 insertions(+), 15 deletions(-)
fs/resctrl/monitor.c +42 -10
index 4565b9864a9e..37df65229109 100644
@@ -623,14 +623,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)
+/*
+ * Find the software controller's ctrl domain that contains @cpu on resource @r.
+ *
+ * Only called from the mbm_over worker via update_mba_bw() where the returned
+ * domain is kept alive by cancel_delayed_work_sync() in
+ * resctrl_offline_ctrl_domain(). This drains this worker and then waits on
+ * rdtgroup_mutex held here before the architecture can free the ctrl domain.
+ *
+ * Context: Call from RCU read-side critical section.
+ */
+static struct rdt_ctrl_domain *get_sc_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) {
+	list_for_each_entry_rcu(d, &r->ctrl_domains, hdr.list) {
 		/* Find the domain that contains this CPU */
 		if (cpumask_test_cpu(cpu, &d->hdr.cpu_mask))
 			return d;
@@ -691,7 +699,8 @@ static void update_mba_bw(struct rdtgroup *rgrp, struct rdt_l3_mon_domain *dom_m
 	if (WARN_ON_ONCE(!pmbm_data))
 		return;
 
-	dom_mba = get_ctrl_domain_from_cpu(smp_processor_id(), r_mba);
+	guard(rcu)();
+	dom_mba = get_sc_ctrl_domain_from_cpu(smp_processor_id(), r_mba);
 	if (!dom_mba) {
 		pr_warn_once("Failure to get domain for MBA update\n");
 		return;
@@ -794,9 +803,19 @@ void cqm_handle_limbo(struct work_struct *work)
 	unsigned long delay = msecs_to_jiffies(CQM_LIMBOCHECK_INTERVAL);
 	struct rdt_l3_mon_domain *d;
 
-	cpus_read_lock();
+	/*
+	 * Safe to run without CPU hotplug lock. Work is guaranteed to be
+	 * canceled before the domain structure is removed.
+	 */
 	mutex_lock(&rdtgroup_mutex);
 
+	/*
+	 * Ensure the worker is dedicated to a CPU as intended and not
+	 * relocated by workqueue subsystem as part of CPU going offline.
+	 */
+	if (!is_percpu_thread())
+		goto out_unlock;
+
 	d = container_of(work, struct rdt_l3_mon_domain, cqm_limbo.work);
 
 	__check_limbo(d, false);
@@ -808,8 +827,8 @@ void cqm_handle_limbo(struct work_struct *work)
 					 delay);
 	}
 
+out_unlock:
 	mutex_unlock(&rdtgroup_mutex);
-	cpus_read_unlock();
 }
 
 /**
@@ -841,7 +860,10 @@ void mbm_handle_overflow(struct work_struct *work)
 	struct list_head *head;
 	struct rdt_resource *r;
 
-	cpus_read_lock();
+	/*
+	 * Safe to run without CPU hotplug lock. Work is guaranteed to be
+	 * canceled before the domain structure is removed.
+	 */
 	mutex_lock(&rdtgroup_mutex);
 
 	/*
@@ -851,6 +873,17 @@ void mbm_handle_overflow(struct work_struct *work)
 	if (!resctrl_mounted || !resctrl_arch_mon_capable())
 		goto out_unlock;
 
+	/*
+	 * Ensure the worker is dedicated to a CPU and not relocated by
+	 * workqueue subsystem as part of CPU going offline since reading
+	 * events depend on smp_processor_id(). After passing this check
+	 * smp_processor_id() is valid for entire duration of this worker
+	 * since it runs with rdtgroup_mutex held and the offline handler needs
+	 * rdtgroup_mutex to offline the CPU being run on here.
+	 */
+	if (!is_percpu_thread())
+		goto out_unlock;
+
 	r = resctrl_arch_get_resource(RDT_RESOURCE_L3);
 	d = container_of(work, struct rdt_l3_mon_domain, mbm_over.work);
 
@@ -875,7 +908,6 @@ void mbm_handle_overflow(struct work_struct *work)
 
 out_unlock:
 	mutex_unlock(&rdtgroup_mutex);
-	cpus_read_unlock();
 }
 
 /**
fs/resctrl/rdtgroup.c +47 -5

86 lines not on this page · raw message

Signature · 1 line
2.50.1
maintainer
#29 lore raw

Re: [PATCH v3 9/9] fs/resctrl: Fix UAF from worker threads when domains are removed

Quoted 4 lines · Reinette Chatre

- Adding a reference count to the domain structure to avoid the worker needing to take CPU hotplug lock. This ended up being very complicated with the architecture needing new APIs to manage the reference count which cannot cleanly integrate into MPAM since it uses a single

Quoted 8 lines

architecture domain structure to contain both the control and monitoring domain structures. Managing the references across mount, unmount, online, offline, as well as worker self exit resulted in several asymmetrical and complicated paths that were error prone. Locking also proved to be complicated since architecture would need to initiate domain free that will need to call back into resctrl that will take rdtgroup_mutex which means that references need to be taken/released without locking.

I'd been working on a reference count approach too. The MPAM combined domain for control and monitoring doesn't seem insurmountable. Mostly because it seems unlikely that the problem with worker threads would ever apply to control domains. Maybe I missed something, but just adding an architecture *release() function that can be used by file system code to drop reference counts on the domain when worker threads exit seems enough.

My patch below.

-Tony

From 611fd8ad816abd37ef9a65b39175ce05907a1d41 Mon Sep 17 00:00:00 2001
From: Tony Luck <[email protected]>
Date: Thu, 21 May 2026 15:14:27 -0700
Subject: [PATCH] fs,mpam,x86/resctrl: Track reference count for L3 monitor
domains

There are race conditions[1] when the last CPU of a domain is taken offline and a worker thread may access the domain structure after it is freed.

Add a rdt_l3_mon_domain::kref to track users of the domain. Don't try to cancel worker threads when CPUs are taken offline. Just set the target CPU for the thread to nr_cpu_ids to indicate the worker needs to take action next time it runs.

---
 include/linux/resctrl.h            |  4 +++
 arch/x86/kernel/cpu/resctrl/core.c | 16 +++++++++--
 drivers/resctrl/mpam_resctrl.c     | 15 +++++++++-
 fs/resctrl/monitor.c               | 46 +++++++++++++++++++++++-------
 fs/resctrl/rdtgroup.c              | 16 +++--------
 5 files changed, 71 insertions(+), 26 deletions(-)
include/linux/resctrl.h +4 -0
index 006e57fd7ca5..715cf62a5864 100644
@@ -4,6 +4,7 @@
 
 #include <linux/cacheinfo.h>
 #include <linux/kernel.h>
+#include <linux/kref.h>
 #include <linux/list.h>
 #include <linux/pid.h>
 #include <linux/resctrl_types.h>
@@ -181,6 +182,7 @@ struct mbm_cntr_cfg {
 /**
  * struct rdt_l3_mon_domain - group of CPUs sharing RDT_RESOURCE_L3 monitoring
  * @hdr:		common header for different domain types
+ * @kref:		count of active users
  * @ci_id:		cache info id for this domain
  * @rmid_busy_llc:	bitmap of which limbo RMIDs are above threshold
  * @mbm_states:		Per-event pointer to the MBM event's saved state.
@@ -195,6 +197,7 @@ struct mbm_cntr_cfg {
  */
 struct rdt_l3_mon_domain {
 	struct rdt_domain_hdr		hdr;
+	struct kref			kref;
 	unsigned int			ci_id;
 	unsigned long			*rmid_busy_llc;
 	struct mbm_state		*mbm_states[QOS_NUM_L3_MBM_EVENTS];
@@ -515,6 +518,7 @@ void resctrl_offline_ctrl_domain(struct rdt_resource *r, struct rdt_ctrl_domain
 void resctrl_offline_mon_domain(struct rdt_resource *r, struct rdt_domain_hdr *hdr);
 void resctrl_online_cpu(unsigned int cpu);
 void resctrl_offline_cpu(unsigned int cpu);
+void resctrl_arch_l3_mon_domain_release(struct kref *kref);
 
 /*
  * Architecture hook called at beginning of first file system mount attempt.
arch/x86/kernel/cpu/resctrl/core.c +13 -3
index 7667cf7c4e94..438016f3097c 100644
@@ -396,6 +396,17 @@ static void l3_mon_domain_free(struct rdt_hw_l3_mon_domain *hw_dom)
 	kfree(hw_dom);
 }
 
+void resctrl_arch_l3_mon_domain_release(struct kref *kref)
+{
+	struct rdt_hw_l3_mon_domain *hw_dom;
+	struct rdt_l3_mon_domain *d;
+
+	d = container_of(kref, struct rdt_l3_mon_domain, kref);
+	hw_dom = resctrl_to_arch_mon_dom(d);
+
+	l3_mon_domain_free(hw_dom);
+}
+
 static int domain_setup_ctrlval(struct rdt_resource *r, struct rdt_ctrl_domain *d)
 {
 	struct rdt_hw_ctrl_domain *hw_dom = resctrl_to_arch_ctrl_dom(d);
@@ -539,6 +550,7 @@ static void l3_mon_domain_setup(int cpu, int id, struct rdt_resource *r, struct
 	d->hdr.id = id;
 	d->hdr.type = RESCTRL_MON_DOMAIN;
 	d->hdr.rid = RDT_RESOURCE_L3;
+	kref_init(&d->kref);
 	ci = get_cpu_cacheinfo_level(cpu, RESCTRL_L3_CACHE);
 	if (!ci) {
 		pr_warn_once("Can't find L3 cache for CPU:%d resource %s\n", cpu, r->name);
@@ -680,18 +692,16 @@ static void domain_remove_cpu_mon(int cpu, struct rdt_resource *r)
 
 	switch (r->rid) {
 	case RDT_RESOURCE_L3: {
-		struct rdt_hw_l3_mon_domain *hw_dom;
 		struct rdt_l3_mon_domain *d;
 
 		if (!domain_header_is_valid(hdr, RESCTRL_MON_DOMAIN, RDT_RESOURCE_L3))
 			return;
 
 		d = container_of(hdr, struct rdt_l3_mon_domain, hdr);
-		hw_dom = resctrl_to_arch_mon_dom(d);
 		resctrl_offline_mon_domain(r, hdr);
 		list_del_rcu(&hdr->list);
 		synchronize_rcu();
-		l3_mon_domain_free(hw_dom);
+		kref_put(&d->kref, resctrl_arch_l3_mon_domain_release);
 		break;
 	}
 	case RDT_RESOURCE_PERF_PKG: {
drivers/resctrl/mpam_resctrl.c +14 -1
index 226ff6f532fa..15f688f18fb6 100644
@@ -1391,6 +1391,8 @@ mpam_resctrl_alloc_domain(unsigned int cpu, struct mpam_resctrl_res *res)
 	if (!dom)
 		return ERR_PTR(-ENOMEM);
 
+	kref_init(&dom->resctrl_mon_dom.kref);
+
 	if (r->alloc_capable) {
 		dom->ctrl_comp = ctrl_comp;
 
@@ -1548,6 +1550,17 @@ int mpam_resctrl_online_cpu(unsigned int cpu)
 	return 0;
 }
 
+void resctrl_arch_l3_mon_domain_release(struct kref *kref)
+{
+	struct mpam_resctrl_dom *dom;
+	struct rdt_l3_mon_domain *d;
+
+	d = container_of(kref, struct rdt_l3_mon_domain, kref);
+	dom = container_of(d, struct mpam_resctrl_dom, resctrl_mon_dom);
+
+	kfree(dom);
+}
+
 void mpam_resctrl_offline_cpu(unsigned int cpu)
 {
 	struct mpam_resctrl_res *res;
@@ -1589,7 +1602,7 @@ void mpam_resctrl_offline_cpu(unsigned int cpu)
 		}
 
 		if (ctrl_dom_empty && mon_dom_empty)
-			kfree(dom);
+			kref_put(&dom->resctrl_mon_dom.kref, resctrl_arch_l3_mon_domain_release);
 	}
 }
 
fs/resctrl/monitor.c +36 -10

91 lines not on this page · raw message

fs/resctrl/rdtgroup.c +4 -12
index 5dfdaa6f9d8f..a4830a2364da 100644
@@ -4332,8 +4332,6 @@ void resctrl_offline_mon_domain(struct rdt_resource *r, struct rdt_domain_hdr *h
 		goto out_unlock;
 
 	d = container_of(hdr, struct rdt_l3_mon_domain, hdr);
-	if (resctrl_is_mbm_enabled())
-		cancel_delayed_work(&d->mbm_over);
 	if (resctrl_is_mon_event_enabled(QOS_L3_OCCUP_EVENT_ID) && has_busy_rmid(d)) {
 		/*
 		 * When a package is going down, forcefully
@@ -4344,7 +4342,6 @@ void resctrl_offline_mon_domain(struct rdt_resource *r, struct rdt_domain_hdr *h
 		 * package never comes back.
 		 */
 		__check_limbo(d, true);
-		cancel_delayed_work(&d->cqm_limbo);
 	}
 
 	domain_destroy_l3_mon_state(d);
@@ -4524,15 +4521,10 @@ void resctrl_offline_cpu(unsigned int cpu)
 
 	d = get_mon_domain_from_cpu(cpu, l3);
 	if (d) {
-		if (resctrl_is_mbm_enabled() && cpu == d->mbm_work_cpu) {
-			cancel_delayed_work(&d->mbm_over);
-			mbm_setup_overflow_handler(d, 0, cpu);
-		}
-		if (resctrl_is_mon_event_enabled(QOS_L3_OCCUP_EVENT_ID) &&
-		    cpu == d->cqm_work_cpu && has_busy_rmid(d)) {
-			cancel_delayed_work(&d->cqm_limbo);
-			cqm_setup_limbo_handler(d, 0, cpu);
-		}
+		if (resctrl_is_mbm_enabled() && cpu == d->mbm_work_cpu)
+			d->mbm_work_cpu = nr_cpu_ids;
+		if (resctrl_is_mon_event_enabled(QOS_L3_OCCUP_EVENT_ID) && cpu == d->cqm_work_cpu)
+			d->cqm_work_cpu = nr_cpu_ids;
 	}
 
 out_unlock:
Signature · 1 line
2.54.0
author maintainer
#30 lore raw

Re: [PATCH v3 9/9] fs/resctrl: Fix UAF from worker threads when domains are removed

Hi Tony,

On 5/26/26 8:32 AM, Luck, Tony wrote:

Instead of deleting the patch without any comments, could you please provide some insight to what problems it has and why your solution is better?

Quoted 14 lines · Reinette Chatre
Quoted 4 lines

- Adding a reference count to the domain structure to avoid the worker needing to take CPU hotplug lock. This ended up being very complicated with the architecture needing new APIs to manage the reference count which cannot cleanly integrate into MPAM since it uses a single

Quoted 8 lines

architecture domain structure to contain both the control and monitoring domain structures. Managing the references across mount, unmount, online, offline, as well as worker self exit resulted in several asymmetrical and complicated paths that were error prone. Locking also proved to be complicated since architecture would need to initiate domain free that will need to call back into resctrl that will take rdtgroup_mutex which means that references need to be taken/released without locking.

Quoted 2 lines

I'd been working on a reference count approach too. The MPAM combined domain for control and monitoring doesn't seem insurmountable. Mostly

While technically possible I do not think it is a clean solution to have the lifetime of the control domain be controlled with a reference in the monitoring domain.

Quoted 5 lines

because it seems unlikely that the problem with worker threads would ever apply to control domains. Maybe I missed something, but just adding an architecture *release() function that can be used by file system code to drop reference counts on the domain when worker threads exit seems enough.

Did you consider the locking implications that I mention in the description you quoted? More below ...

Quoted 2 lines

My patch below.

heh

Quoted 10 lines

-Tony

From 611fd8ad816abd37ef9a65b39175ce05907a1d41 Mon Sep 17 00:00:00 2001
From: Tony Luck <[email protected]>
Date: Thu, 21 May 2026 15:14:27 -0700
Subject: [PATCH] fs,mpam,x86/resctrl: Track reference count for L3 monitor
domains

Quoted 7 lines

There are race conditions[1] when the last CPU of a domain is taken offline and a worker thread may access the domain structure after it is freed.

Add a rdt_l3_mon_domain::kref to track users of the domain. Don't try to cancel worker threads when CPUs are taken offline. Just set the target CPU for the thread to nr_cpu_ids to indicate the worker needs to take action next time it runs.

One test I have found to be useful when digging into this is to offline all CPUs of a domain starting with lowest number. Since overflow worker runs on lowest number and then is moved to next CPU when it goes offline this stresses this new mechanics. Have you tried something similar or could you try this test with this solution?

...

Quoted 9 lines
arch/x86/kernel/cpu/resctrl/core.c +0 -1
@@ -680,18 +692,16 @@ static void domain_remove_cpu_mon(int cpu, struct rdt_resource *r)
 
 	switch (r->rid) {
 	case RDT_RESOURCE_L3: {
-		struct rdt_hw_l3_mon_domain *hw_dom;
 		struct rdt_l3_mon_domain *d;
 
 		if (!domain_header_is_valid(hdr, RESCTRL_MON_DOMAIN, RDT_RESOURCE_L3))
 			return;

arch/x86/kernel/cpu/resctrl/core.c static void domain_remove_cpu_mon(int cpu, struct rdt_resource *r) · line 706

Quoted 10 lines
arch/x86/kernel/cpu/resctrl/core.c +1 -2
 
 		d = container_of(hdr, struct rdt_l3_mon_domain, hdr);
-		hw_dom = resctrl_to_arch_mon_dom(d);
 		resctrl_offline_mon_domain(r, hdr);
 		list_del_rcu(&hdr->list);
 		synchronize_rcu();
-		l3_mon_domain_free(hw_dom);
+		kref_put(&d->kref, resctrl_arch_l3_mon_domain_release);
 		break;
 	}

To me the idea behind a "domain reference count" is to provide guarantee to any holder of a reference that the domain *and* its data remains accessible while it holds the reference. There is the domain structure itself and then the architecture specific, for example, rdt_hw_l3_mon_domain::arch_mbm_states, and the fs state, for example rdt_l3_mon_domain::mbm_states.

Above snippet moves the freeing of the *architecture* state to be called on kref_put() while *always* freeing the fs state (resctrl_offline_mon_domain()->domain_destroy_l3_mon_state()).

A worker may thus have a reference to the domain but when it runs it runs without fs state which is just a new use-after-free.

As I mentioned in the description the release managed by architecture implies that reference needs to be dropped without rdtgroup_mutex held since the architecture should also call resctrl_offline_mon_domain() as part of release. Locking needs to be reworked and needs to adhere to kref rules on kref_get()/kref_put() without locking.

Reinette

maintainer
#31 lore raw

Re: [PATCH v3 9/9] fs/resctrl: Fix UAF from worker threads when domains are removed

Quoted 28 lines · Reinette Chatre

Hi Tony,

On 5/26/26 8:32 AM, Luck, Tony wrote:

Instead of deleting the patch without any comments, could you please provide some insight to what problems it has and why your solution is better?

Quoted 14 lines · Reinette Chatre
Quoted 4 lines

- Adding a reference count to the domain structure to avoid the worker needing to take CPU hotplug lock. This ended up being very complicated with the architecture needing new APIs to manage the reference count which cannot cleanly integrate into MPAM since it uses a single

Quoted 8 lines

architecture domain structure to contain both the control and monitoring domain structures. Managing the references across mount, unmount, online, offline, as well as worker self exit resulted in several asymmetrical and complicated paths that were error prone. Locking also proved to be complicated since architecture would need to initiate domain free that will need to call back into resctrl that will take rdtgroup_mutex which means that references need to be taken/released without locking.

Quoted 2 lines

I'd been working on a reference count approach too. The MPAM combined domain for control and monitoring doesn't seem insurmountable. Mostly

While technically possible I do not think it is a clean solution to have the lifetime of the control domain be controlled with a reference in the monitoring domain.

Quoted 8 lines
Quoted 5 lines

because it seems unlikely that the problem with worker threads would ever apply to control domains. Maybe I missed something, but just adding an architecture *release() function that can be used by file system code to drop reference counts on the domain when worker threads exit seems enough.

Did you consider the locking implications that I mention in the description you quoted? More below ...

I didn't run into any lockdep splats during testing. But maybe didn't have enough code coverage to poke into corner cases.

Quoted 24 lines
Quoted 3 lines

My patch below.

heh

Quoted 10 lines

-Tony

From 611fd8ad816abd37ef9a65b39175ce05907a1d41 Mon Sep 17 00:00:00 2001
From: Tony Luck <[email protected]>
Date: Thu, 21 May 2026 15:14:27 -0700
Subject: [PATCH] fs,mpam,x86/resctrl: Track reference count for L3 monitor
domains

Quoted 7 lines

There are race conditions[1] when the last CPU of a domain is taken offline and a worker thread may access the domain structure after it is freed.

Add a rdt_l3_mon_domain::kref to track users of the domain. Don't try to cancel worker threads when CPUs are taken offline. Just set the target CPU for the thread to nr_cpu_ids to indicate the worker needs to take action next time it runs.

Quoted 4 lines

One test I have found to be useful when digging into this is to offline all CPUs of a domain starting with lowest number. Since overflow worker runs on lowest number and then is moved to next CPU when it goes offline this stresses this new mechanics. Have you tried something similar or could you try this test with this solution?

Yes. My test case ran through all CPUs in the domain in order. That showed an interesting artifact that when CPU 36 goes offline, the worker next runs on CPU37, which is the place it needs to be, but it isn't bound to CPU37. But since d->mbm_work_cpu (or d->cqm_work_cpu) is set to nr_cpus_id the worker calls schedule_delayed_work_on() to get itself properly bound to CPU37.

Quoted 13 lines

...

Quoted 10 lines
arch/x86/kernel/cpu/resctrl/core.c +0 -1
@@ -680,18 +692,16 @@ static void domain_remove_cpu_mon(int cpu, struct rdt_resource *r)
 
 	switch (r->rid) {
 	case RDT_RESOURCE_L3: {
-		struct rdt_hw_l3_mon_domain *hw_dom;
 		struct rdt_l3_mon_domain *d;
 
 		if (!domain_header_is_valid(hdr, RESCTRL_MON_DOMAIN, RDT_RESOURCE_L3))
 			return;
 
Quoted 14 lines

arch/x86/kernel/cpu/resctrl/core.c static void domain_remove_cpu_mon(int cpu, struct rdt_resource *r) · line 706

Quoted 9 lines
arch/x86/kernel/cpu/resctrl/core.c +1 -2
 		d = container_of(hdr, struct rdt_l3_mon_domain, hdr);
-		hw_dom = resctrl_to_arch_mon_dom(d);
 		resctrl_offline_mon_domain(r, hdr);
 		list_del_rcu(&hdr->list);
 		synchronize_rcu();
-		l3_mon_domain_free(hw_dom);
+		kref_put(&d->kref, resctrl_arch_l3_mon_domain_release);
 		break;
 	}

To me the idea behind a "domain reference count" is to provide guarantee to any holder of a reference that the domain *and* its data remains accessible while it holds the reference. There is the domain structure itself and then the architecture specific, for example, rdt_hw_l3_mon_domain::arch_mbm_states, and the fs state, for example rdt_l3_mon_domain::mbm_states.

Agreed. I'm not guaranteeing that the whole of the domain structure (with all the substructures that it points to) are valid. This reference only promises that the mbm_over, cqm_limbo, mbm_work_cpu, and cqm_work_cpu fields are still valid. That's enough to solve this issue, but if we were to adopt this patch would need some comments so that future readers were not led astray.

Quoted 5 lines

Above snippet moves the freeing of the *architecture* state to be called on kref_put() while *always* freeing the fs state (resctrl_offline_mon_domain()->domain_destroy_l3_mon_state()).

A worker may thus have a reference to the domain but when it runs it runs without fs state which is just a new use-after-free.

For this specific case, the worker sees "nr_cpus_id" and avoids use of other fields that may no longer be valid.

Quoted 5 lines

As I mentioned in the description the release managed by architecture implies that reference needs to be dropped without rdtgroup_mutex held since the architecture should also call resctrl_offline_mon_domain() as part of release. Locking needs to be reworked and needs to adhere to kref rules on kref_get()/kref_put() without locking.

That's all handled in the existing (unchanged) offline path. The worker threads only need to call kref_put() to release their hold on the domain structure that has been mostly freed (and likely has no hold from the file system by this point).

The release functions in both X86 and MPAM don't need locks, they are just calling kfree()

Quoted 2 lines

Reinette

I see that sashiko found no issues in parts 1-5 of your series (Yay!) Grumbled about some issues in part 6. And then gave up before getting to your latest solution to the domain offline problem.

Can you repost just part 9 (or maybe parts 7-9) on top of latest -rc to see if sashiko is happy at last?

-Tony

author maintainer
#32 lore raw

Re: [PATCH v3 9/9] fs/resctrl: Fix UAF from worker threads when domains are removed

Hi Tony,

Quoted 35 lines · Tony
Quoted 34 lines · Reinette Chatre

Hi Tony,

On 5/26/26 8:32 AM, Luck, Tony wrote:

Instead of deleting the patch without any comments, could you please provide some insight to what problems it has and why your solution is better?

Quoted 14 lines · Reinette Chatre
Quoted 4 lines

- Adding a reference count to the domain structure to avoid the worker needing to take CPU hotplug lock. This ended up being very complicated with the architecture needing new APIs to manage the reference count which cannot cleanly integrate into MPAM since it uses a single

Quoted 8 lines

architecture domain structure to contain both the control and monitoring domain structures. Managing the references across mount, unmount, online, offline, as well as worker self exit resulted in several asymmetrical and complicated paths that were error prone. Locking also proved to be complicated since architecture would need to initiate domain free that will need to call back into resctrl that will take rdtgroup_mutex which means that references need to be taken/released without locking.

Quoted 2 lines

I'd been working on a reference count approach too. The MPAM combined domain for control and monitoring doesn't seem insurmountable. Mostly

While technically possible I do not think it is a clean solution to have the lifetime of the control domain be controlled with a reference in the monitoring domain.

Quoted 5 lines

because it seems unlikely that the problem with worker threads would ever apply to control domains. Maybe I missed something, but just adding an architecture *release() function that can be used by file system code to drop reference counts on the domain when worker threads exit seems enough.

Quoted 5 lines
Quoted 2 lines

Did you consider the locking implications that I mention in the description you quoted? More below ...

I didn't run into any lockdep splats during testing. But maybe didn't have enough code coverage to poke into corner cases.

Your patch avoids lockdep splats by using *resctrl fs* domain reference to protect only the *architecture* domain data that can be removed without rdtgroup_mutex while removing the resctrl fs domain data unconditionally. I do not find this to be a sane usage of the reference count. Since the reference is attached to the resctrl fs domain (rdt_l3_mon_domain::kref) then I find it reasonable to expect it to protect struct rdt_l3_mon_domain's data. Could you please elaborate why you disagree?

Quoted 63 lines
Quoted 24 lines
Quoted 3 lines

My patch below.

heh

Quoted 10 lines

-Tony

From 611fd8ad816abd37ef9a65b39175ce05907a1d41 Mon Sep 17 00:00:00 2001
From: Tony Luck <[email protected]>
Date: Thu, 21 May 2026 15:14:27 -0700
Subject: [PATCH] fs,mpam,x86/resctrl: Track reference count for L3 monitor
domains

Quoted 7 lines

There are race conditions[1] when the last CPU of a domain is taken offline and a worker thread may access the domain structure after it is freed.

Add a rdt_l3_mon_domain::kref to track users of the domain. Don't try to cancel worker threads when CPUs are taken offline. Just set the target CPU for the thread to nr_cpu_ids to indicate the worker needs to take action next time it runs.

Quoted 4 lines

One test I have found to be useful when digging into this is to offline all CPUs of a domain starting with lowest number. Since overflow worker runs on lowest number and then is moved to next CPU when it goes offline this stresses this new mechanics. Have you tried something similar or could you try this test with this solution?

Yes. My test case ran through all CPUs in the domain in order. That showed an interesting artifact that when CPU 36 goes offline, the worker next runs on CPU37, which is the place it needs to be, but it isn't bound to CPU37. But since d->mbm_work_cpu (or d->cqm_work_cpu) is set to nr_cpus_id the worker calls schedule_delayed_work_on() to get itself properly bound to CPU37.

Quoted 13 lines

...

Quoted 10 lines
arch/x86/kernel/cpu/resctrl/core.c +0 -1
@@ -680,18 +692,16 @@ static void domain_remove_cpu_mon(int cpu, struct rdt_resource *r)
 
 	switch (r->rid) {
 	case RDT_RESOURCE_L3: {
-		struct rdt_hw_l3_mon_domain *hw_dom;
 		struct rdt_l3_mon_domain *d;
 
 		if (!domain_header_is_valid(hdr, RESCTRL_MON_DOMAIN, RDT_RESOURCE_L3))
 			return;
 
Quoted 14 lines

arch/x86/kernel/cpu/resctrl/core.c static void domain_remove_cpu_mon(int cpu, struct rdt_resource *r) · line 706

Quoted 9 lines
arch/x86/kernel/cpu/resctrl/core.c +1 -2
 		d = container_of(hdr, struct rdt_l3_mon_domain, hdr);
-		hw_dom = resctrl_to_arch_mon_dom(d);
 		resctrl_offline_mon_domain(r, hdr);
 		list_del_rcu(&hdr->list);
 		synchronize_rcu();
-		l3_mon_domain_free(hw_dom);
+		kref_put(&d->kref, resctrl_arch_l3_mon_domain_release);
 		break;
 	}

To me the idea behind a "domain reference count" is to provide guarantee to any holder of a reference that the domain *and* its data remains accessible while it holds the reference. There is the domain structure itself and then the architecture specific, for example, rdt_hw_l3_mon_domain::arch_mbm_states, and the fs state, for example rdt_l3_mon_domain::mbm_states.

Quoted 6 lines

Agreed. I'm not guaranteeing that the whole of the domain structure (with all the substructures that it points to) are valid. This reference only promises that the mbm_over, cqm_limbo, mbm_work_cpu, and cqm_work_cpu fields are still valid. That's enough to solve this issue, but if we were to adopt this patch would need some comments so that future readers were not led astray.

Adding reference counting to the domain structure that does not actually protect the domain structure but instead introduces additional corner cases created just to fix one issue is not something I am comfortable with. I also do not see adding comments to explain all the sharp corners as "fixing" it.

Quoted 9 lines
Quoted 6 lines

Above snippet moves the freeing of the *architecture* state to be called on kref_put() while *always* freeing the fs state (resctrl_offline_mon_domain()->domain_destroy_l3_mon_state()).

A worker may thus have a reference to the domain but when it runs it runs without fs state which is just a new use-after-free.

For this specific case, the worker sees "nr_cpus_id" and avoids use of other fields that may no longer be valid.

This does not sound like proper reference counting to me.

Quoted 7 lines
Quoted 6 lines

As I mentioned in the description the release managed by architecture implies that reference needs to be dropped without rdtgroup_mutex held since the architecture should also call resctrl_offline_mon_domain() as part of release. Locking needs to be reworked and needs to adhere to kref rules on kref_get()/kref_put() without locking.

Quoted 7 lines

That's all handled in the existing (unchanged) offline path. The worker threads only need to call kref_put() to release their hold on the domain structure that has been mostly freed (and likely has no hold from the file system by this point).

The release functions in both X86 and MPAM don't need locks, they are just calling kfree()

Right. Not needing locks is possible if the reference does not actually protect domain state.

... and just ignore the design comments.

Quoted 9 lines
Quoted 2 lines

Reinette

I see that sashiko found no issues in parts 1-5 of your series (Yay!) Grumbled about some issues in part 6. And then gave up before getting to your latest solution to the domain offline problem.

Can you repost just part 9 (or maybe parts 7-9) on top of latest -rc to see if sashiko is happy at last?

... and then will you take a look?

Reinette

maintainer
#33 lore raw

RE: [PATCH v3 9/9] fs/resctrl: Fix UAF from worker threads when domains are removed

Quoted 4 lines
Quoted 2 lines

Can you repost just part 9 (or maybe parts 7-9) on top of latest -rc to see if sashiko is happy at last?

... and then will you take a look?

Yes. Your approach is much cleaner ... I'm just worried that there will be yet another layer of the onion to peel when sashiko looks at it.

There should be a limit to how much effort we spend trying to fix a bug that's technically possible, but so far away from real usage models.

-Tony

author maintainer
#35 lore raw

Re: [PATCH v3 9/9] fs/resctrl: Fix UAF from worker threads when domains are removed

In reply to Reinette Chatre

fs/resctrl/monitor.c void mbm_handle_overflow(struct work_struct *work) · line 869

Quoted 12 lines · Reinette Chatre
fs/resctrl/monitor.c +4 -1
@@ -841,7 +860,10 @@ void mbm_handle_overflow(struct work_struct *work)
 	struct list_head *head;
 	struct rdt_resource *r;
 
-	cpus_read_lock();
+	/*
+	 * Safe to run without CPU hotplug lock. Work is guaranteed to be
+	 * canceled before the domain structure is removed.
+	 */
 	mutex_lock(&rdtgroup_mutex);
 
 	/*

I resubmitted the last three patches of series to obtain Sashiko review [1] and respond to that feedback here:

Sashiko: "Could running this worker without the hotplug lock trigger lockdep splats and expose the architecture backend to races against CPU hotplug?"

No.

Sashiko points out that the MPAM resctrl_arch_get_config() and resctrl_arch_update_one() hooks contain lockdep_assert_cpus_held() and since these hooks are called via mbm_handle_overflow() it seems that this could trigger lockdep splats.

Recent commit f52abe650241 ("fs/resctrl: Disallow the software controller when MBM counters are assignable") established that MPAM does not support the software controller and these hooks will
thus not be called from overflow handler.

Reinette

[1] https://sashiko.dev/#/patchset/cover.1779834897.git.reinette.chatre%40intel.com

maintainer
#36 lore raw
In reply to Reinette Chatre
Quoted 17 lines · Reinette Chatre

v2: https://lore.kernel.org/lkml/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/

While reviewing the AET series [1] Sashiko reported a deadlock during mount, and a use-after-free when an L3 domain is removed during CPU offline. Reinette found a memory leak in the mount error path while refactoring code for a solution to the mount hang.

During review of V1 of this series Sashiko found a new UAF on unmount issue that was fixed in V2.

During review of V2 Sashiko uncovered a couple more new issues: TOCTOU involving rdtgroup_kn_put() that may lead to UAF or double-free, double free of pseudo-locked regions, potential deadlock between resctrl unmount and info file readers. Sashiko also found that the CPU offline fix in V2 is flawed in its use of is_percpu_thread().

Quoted 5 lines

Address all issues identified. This version is significantly different from V2 because of the additional fixes and reworking of the CPU offline fix. I do not consider this version quite "polished" but after all changes made to address all the issues identified by Sashiko I would like to check-in with folks (and Sashiko) on where the fixes are headed and would appreciate any feedback.

Several of these patches are either authored or co-authored by me, so I'm uncertain about the ethics of piling on a Reviewed-by tag.

Apart from patch 6 to fix sashiko reported issues in pseudo-locking everything looks good. I agree with Reinette's assessment[1] that pursuing the bizarre corner case races for pseudo-locking should not be a priority. That patch can be dropped.

-Tony

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

author maintainer
#37 lore raw

Hi Tony,

Quoted 24 lines · Tony
Quoted 17 lines · Reinette Chatre

v2: https://lore.kernel.org/lkml/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/

While reviewing the AET series [1] Sashiko reported a deadlock during mount, and a use-after-free when an L3 domain is removed during CPU offline. Reinette found a memory leak in the mount error path while refactoring code for a solution to the mount hang.

During review of V1 of this series Sashiko found a new UAF on unmount issue that was fixed in V2.

During review of V2 Sashiko uncovered a couple more new issues: TOCTOU involving rdtgroup_kn_put() that may lead to UAF or double-free, double free of pseudo-locked regions, potential deadlock between resctrl unmount and info file readers. Sashiko also found that the CPU offline fix in V2 is flawed in its use of is_percpu_thread().

Quoted 5 lines

Address all issues identified. This version is significantly different from V2 because of the additional fixes and reworking of the CPU offline fix. I do not consider this version quite "polished" but after all changes made to address all the issues identified by Sashiko I would like to check-in with folks (and Sashiko) on where the fixes are headed and would appreciate any feedback.

Quoted 7 lines

Several of these patches are either authored or co-authored by me, so I'm uncertain about the ethics of piling on a Reviewed-by tag.

Apart from patch 6 to fix sashiko reported issues in pseudo-locking everything looks good. I agree with Reinette's assessment[1] that pursuing the bizarre corner case races for pseudo-locking should not be a priority. That patch can be dropped.

Thank you for taking a look.

After thinking about patch 6 more I plan to drop the fixes surrounding the races but keep the fix for the RMID double-add. Since this will touch pseudo-locking region lifetime management I expect Sashiko would bring up the pseudo-locking races during its review but I believe this is a worthy fix.

As a summary of your tags for this series I see:
- patches 1, 2, 3, 4, and 9 have your Signed-off-by.
- patches 5, 7, and 8 do not have any tags from you.

Reinette

maintainer
#38 lore raw
Quoted 11 lines
Quoted 4 lines

Apart from patch 6 to fix sashiko reported issues in pseudo-locking everything looks good. I agree with Reinette's assessment[1] that pursuing the bizarre corner case races for pseudo-locking should not be a priority. That patch can be dropped.

Thank you for taking a look.

After thinking about patch 6 more I plan to drop the fixes surrounding the races but keep the fix for the RMID double-add. Since this will touch pseudo-locking region lifetime management I expect Sashiko would bring up the pseudo-locking races during its review but I believe this is a worthy fix.

Seems good.

Quoted 3 lines

As a summary of your tags for this series I see:
- patches 1, 2, 3, 4, and 9 have your Signed-off-by.
- patches 5, 7, and 8 do not have any tags from you.

You can add my Reviewed-by: Tony Luck <[email protected]> to parts 5,7,8

-Tony

author maintainer
#39 lore raw
Quoted 12 lines · Tony
Quoted 11 lines
Quoted 4 lines

Apart from patch 6 to fix sashiko reported issues in pseudo-locking everything looks good. I agree with Reinette's assessment[1] that pursuing the bizarre corner case races for pseudo-locking should not be a priority. That patch can be dropped.

Thank you for taking a look.

After thinking about patch 6 more I plan to drop the fixes surrounding the races but keep the fix for the RMID double-add. Since this will touch pseudo-locking region lifetime management I expect Sashiko would bring up the pseudo-locking races during its review but I believe this is a worthy fix.

Quoted 8 lines

Seems good.

Quoted 3 lines

As a summary of your tags for this series I see:
- patches 1, 2, 3, 4, and 9 have your Signed-off-by.
- patches 5, 7, and 8 do not have any tags from you.

You can add my Reviewed-by: Tony Luck <[email protected]> to parts 5,7,8

Thank you very much Tony.

Reinette