Skip to contentSkip to conversation

fs/resctrl: Fix three long-standing issues

PATCH Superseded Tony Luck 3 participants 19 messages May 8 – May 13 RDT - resource allocation

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Tony Luck

author maintainer
#0 lore raw

cover fs/resctrl: Fix three long-standing issues

Sashiko reported[0] 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.

Patch 1 just reorders some code so that fixes can be applied without adding additional forward declaration of functions.

Patch 2 fixes the memory leak found by Reinette

Patch 3 fixes the mount deadlock[1]

Patch 4 fixes issues with CPU offline.[2]

N.B. Reinette did all the work for patches 3 & 4, so I listed her as author an me as Co-developer. Those patches need her sign-off.

[0] Sashiko report:

[1] This is v3 of the deadlock fix. Previous version is here:

[2] This is v2 of the CPU offline fix. Previous version is here:

Reinette Chatre (2):
fs/resctrl: Fix deadlock for errors during mount fs/resctrl: Fix issues with worker threads when CPUs are taken offline

Tony Luck (2):
fs/resctrl: Move functions to avoid forward references in subsequent
fixes
fs/resctrl: Free mon_data structures on rdt_get_tree() failure

 fs/resctrl/monitor.c  |  55 ++++++
 fs/resctrl/rdtgroup.c | 440 ++++++++++++++++++++++--------------------
 2 files changed, 287 insertions(+), 208 deletions(-)
Signature · 1 line
2.54.0
author maintainer
#1 lore raw

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

No functional change. Just pull some functions before rdt_get_tree().

---
 fs/resctrl/rdtgroup.c | 376 +++++++++++++++++++++---------------------
 1 file changed, 188 insertions(+), 188 deletions(-)
fs/resctrl/rdtgroup.c +188 -188
index 5dfdaa6f9d8f..a6376a3fc4c3 100644
@@ -2782,6 +2782,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);
@@ -2981,194 +3169,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.54.0
author maintainer
#2 lore raw

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

In reply to Tony Luck

If mkdir_mondata_all() succeeds but 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_mondata error path to free them.

mon_get_kn_priv() and mon_put_kn_priv() moved so defined before used.

Fixes ee4f0ec938ad ("fs/resctrl: Simplify allocation of mon_data structures")
Assisted-by GitHub_Copilot_CLI:claude-sonnet-4.6
---
 fs/resctrl/rdtgroup.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/resctrl/rdtgroup.c +3 -1
index a6376a3fc4c3..0db1a92aefbe 100644
@@ -3067,8 +3067,10 @@ static int rdt_get_tree(struct fs_context *fc)
 out_psl:
 	rdt_pseudo_lock_release();
 out_mondata:
-	if (resctrl_arch_mon_capable())
+	if (resctrl_arch_mon_capable()) {
+		mon_put_kn_priv();
 		kernfs_remove(kn_mondata);
+	}
 out_mongrp:
 	if (resctrl_arch_mon_capable()) {
 		rdtgroup_unassign_cntrs(&rdtgroup_default);
Signature · 1 line
2.54.0
author maintainer
#3 lore raw

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

Quoted 6 lines · Tony Luck

If mkdir_mondata_all() succeeds but 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_mondata error path to free them.

mon_get_kn_priv() and mon_put_kn_priv() moved so defined before used.

My bad, leaving this stale commit comment here. Code rearrangement in patch 1 covered this.

Quoted 3 lines
Fixes ee4f0ec938ad ("fs/resctrl: Simplify allocation of mon_data structures")
Assisted-by GitHub_Copilot_CLI:claude-sonnet-4.6

Hmmm. I don't think I was assisted at all. Actively sabotaged with a patch that looks plausible, but it actually wrong seems more accurate.

Quoted 10 lines
---
 fs/resctrl/rdtgroup.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
fs/resctrl/rdtgroup.c +0 -0
index a6376a3fc4c3..0db1a92aefbe 100644

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

Quoted 6 lines
fs/resctrl/rdtgroup.c +2 -1
@@ -3067,8 +3067,10 @@ static int rdt_get_tree(struct fs_context *fc)
 out_psl:
 	rdt_pseudo_lock_release();
 out_mondata:
-	if (resctrl_arch_mon_capable())
+	if (resctrl_arch_mon_capable()) {
+		mon_put_kn_priv();

Claude put this call here ...

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

Quoted 4 lines
fs/resctrl/rdtgroup.c +1 -0
 		kernfs_remove(kn_mondata);
+	}
 out_mongrp:
 	if (resctrl_arch_mon_capable()) {

But it really ought to be here.

Quoted 3 lines
fs/resctrl/rdtgroup.c +0 -0
 		rdtgroup_unassign_cntrs(&rdtgroup_default);
Signature · 1 line
2.54.0

Claude is fired. Replacement patch below.

-Tony

From 0263035539f805f5d4bddcef8968b551354cb86d Mon Sep 17 00:00:00 2001
From: Tony Luck <[email protected]>
Date: Thu, 7 May 2026 13:48:51 -0700
Subject: [PATCH] fs/resctrl: Free mon_data structures on rdt_get_tree()
failure

If mkdir_mondata_all() succeeds but 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 them.

Fixes ee4f0ec938ad ("fs/resctrl: Simplify allocation of mon_data structures")
---
 fs/resctrl/rdtgroup.c | 1 +
 1 file changed, 1 insertion(+)
fs/resctrl/rdtgroup.c +1 -0
index a6376a3fc4c3..506b40dc9430 100644
@@ -3071,6 +3071,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.54.0
Quoted 1 line
#4 lore raw

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

Quoted 8 lines · Tony

On Fri, May 08, 2026 at 11:21:41AM -0700, Tony Luck wrote:

From 0263035539f805f5d4bddcef8968b551354cb86d Mon Sep 17 00:00:00 2001
From: Tony Luck <[email protected]>
Date: Thu, 7 May 2026 13:48:51 -0700
Subject: [PATCH] fs/resctrl: Free mon_data structures on rdt_get_tree()
failure

Quoted 6 lines

If mkdir_mondata_all() succeeds but 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 them.

Fixes ee4f0ec938ad ("fs/resctrl: Simplify allocation of mon_data structures")

I did not find above commit in vanilla kernel, should it be 2a6566038544 ("x86/resctrl:
Expand the width of domid by replacing mon_data_bits") where mon_put_kn_priv() was introduced?

thanks,
Chenyu

author maintainer
#5 lore raw

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

Quoted 14 lines · Yu C
Quoted 13 lines · Tony

On Fri, May 08, 2026 at 11:21:41AM -0700, Tony Luck wrote:

From 0263035539f805f5d4bddcef8968b551354cb86d Mon Sep 17 00:00:00 2001
From: Tony Luck <[email protected]>
Date: Thu, 7 May 2026 13:48:51 -0700
Subject: [PATCH] fs/resctrl: Free mon_data structures on rdt_get_tree()
failure

If mkdir_mondata_all() succeeds but 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 them.

Quoted 6 lines
Quoted 1 line
Fixes ee4f0ec938ad ("fs/resctrl: Simplify allocation of mon_data structures")

I did not find above commit in vanilla kernel, should it be 2a6566038544 ("x86/resctrl:
Expand the width of domid by replacing mon_data_bits") where mon_put_kn_priv() was introduced?

Thanks for the catch. Another strike against Claude. It didn't hallucinate this commit for the fixes tag, that commit does appear in my local repo. But in a dead-end branch I created to write the patch. I posted it here in v3 of my telemetry series:

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

But James Morse picked it up in v11 of his "Move resctrl filessytem code to fs/resctrl" series:

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

where it was applied upstream in the 2a6566038544 commit you found.

I'm somewhat confused that Claude dug through my other branches instead of just looking for ancestors of the current branch. Maybe I need an explicit instruction to AI agents on how to find commits to use for
Fixes: tags?

Claude is now double-fired.

-Tony

#6 lore raw

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

Quoted 32 lines · Tony
Quoted 14 lines · Yu C
Quoted 13 lines · Tony

On Fri, May 08, 2026 at 11:21:41AM -0700, Tony Luck wrote:

From 0263035539f805f5d4bddcef8968b551354cb86d Mon Sep 17 00:00:00 2001
From: Tony Luck <[email protected]>
Date: Thu, 7 May 2026 13:48:51 -0700
Subject: [PATCH] fs/resctrl: Free mon_data structures on rdt_get_tree()
failure

If mkdir_mondata_all() succeeds but 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 them.

Quoted 6 lines
Quoted 1 line
Fixes ee4f0ec938ad ("fs/resctrl: Simplify allocation of mon_data structures")

I did not find above commit in vanilla kernel, should it be 2a6566038544 ("x86/resctrl:
Expand the width of domid by replacing mon_data_bits") where mon_put_kn_priv() was introduced?

Thanks for the catch. Another strike against Claude. It didn't hallucinate this commit for the fixes tag, that commit does appear in my local repo. But in a dead-end branch I created to write the patch. I posted it here in v3 of my telemetry series:

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

But James Morse picked it up in v11 of his "Move resctrl filessytem code to fs/resctrl" series:

Quoted 8 lines

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

where it was applied upstream in the 2a6566038544 commit you found.

I'm somewhat confused that Claude dug through my other branches instead of just looking for ancestors of the current branch. Maybe I need an explicit instruction to AI agents on how to find commits to use for
Fixes: tags?

I guess so, Opus 4.6 suggested the following prompt:
"When generating Fixes: tags, only consider commits reachable from the current branch (git log). Do not search other local branches."

thanks,
Chenyu

Quoted 4 lines

Claude is now double-fired.

-Tony

author maintainer
#7 lore raw

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

In reply to Tony Luck

From: Reinette Chatre <[email protected]>

Sashiko noticed[1] a deadlock in the resctrl mount code.

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.

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

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.

Fixes 5ff193fbde20 ("x86/intel_rdt: Add basic resctrl filesystem support")
---
 fs/resctrl/rdtgroup.c | 63 ++++++++++++++++++++++++-------------------
 1 file changed, 36 insertions(+), 27 deletions(-)
fs/resctrl/rdtgroup.c +36 -27
index 0db1a92aefbe..62e1e4c30f78 100644
@@ -2970,6 +2970,29 @@ 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);
@@ -3043,10 +3066,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())
@@ -3062,10 +3081,19 @@ static int rdt_get_tree(struct fs_context *fc)
 						   RESCTRL_PICK_ANY_CPU);
 	}
 
-	goto out;
+	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();
+	return ret;
 
-out_psl:
-	rdt_pseudo_lock_release();
 out_mondata:
 	if (resctrl_arch_mon_capable()) {
 		mon_put_kn_priv();
@@ -3086,7 +3114,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;
@@ -3173,26 +3200,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.54.0
#8 lore raw

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

Hi Tony,

Quoted 17 lines · Tony Luck

From: Reinette Chatre <[email protected]>

Sashiko noticed[1] a deadlock in the resctrl mount code.

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.

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

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.

[ ... ]

Quoted 19 lines
fs/resctrl/rdtgroup.c +1 -4
+
  static int rdt_get_tree(struct fs_context *fc)
  {
  	struct rdt_fs_context *ctx = rdt_fc2context(fc);
@@ -3043,10 +3066,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())
@@ -3062,10 +3081,19 @@ static int rdt_get_tree(struct fs_context *fc)
  						   RESCTRL_PICK_ANY_CPU);
  	}
 

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

Quoted 10 lines
fs/resctrl/rdtgroup.c +9 -1
-	goto out;
+	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.
+	 */

I gradually came to understand the purpose of new_sb_created,
and how it indicates whether the resctrl state should be released
or has already been released. Initially, I thought that if ctx->kfc.new_sb_created is false, this could happen either due to a failure to create a superblock or a reference to an existing superblock. In the latter case, the code tears down all resctrl state via resctrl_unmount() while still returning success. As a result, VFS might complete the mount with an invalid resctrl state. I later realized this scenario cannot occur, as mounting resctrl twice is impossible - kernfs_get_tree() will not return 0 in such cases. It might be worthwhile adding a comment here to clarify the rationale/scenario behind this for future reference IMO : )

thanks,
Chenyu

Quoted 4 lines
fs/resctrl/rdtgroup.c +3 -0
+	if (!ctx->kfc.new_sb_created)
+		resctrl_unmount();
+	return ret;
 
maintainer
#9 lore raw

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

In reply to Tony Luck

Hi Tony,

Quoted 18 lines · Tony Luck
(quoted diff) +0 -14
@@ -3173,26 +3200,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();

Review static int rdt_init_fs_context(struct fs_context *fc) · line 3207

Quoted 10 lines
(quoted diff) +1 -5
-	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 = {

sashiko reports [1] that this fix unmasks a use-after-free. This new issue looks to be legitimate. I am not comfortable with sashiko's proposed fix since it flips the current teardown ordering that is based on kernfs guidance per kernfs_kill_sb() function comments:
* This can be used directly for file_system_type->kill_sb(). If a kernfs
* user needs extra cleanup, it can implement its own kill_sb() and call
* this function at the end.

Something else to also consider is how interference from resctrl_exit() may occur now that its caller has been merged. What do you instead think of something like below? The main idea is that it takes an extra reference to the root kn before calling kernfs_get_tree(), which it releases upon return. I find such a symmetric solution relying on reference counts instead of code ordering easier to reason about and by taking the extra reference with rdtgroup_mutex held it can handle interference from resctrl_exit(). What do you think?

fs/resctrl/rdtgroup.c +15 -0
index 1ec7ff0389d2..4c4f98ebf4a9 100644
@@ -3035,6 +3035,7 @@ static int rdt_get_tree(struct fs_context *fc)
 {
 	struct rdt_fs_context *ctx = rdt_fc2context(fc);
 	unsigned long flags = RFTYPE_CTRL_BASE;
+	struct kernfs_node *rdt_root_kn;
 	struct rdt_l3_mon_domain *dom;
 	struct rdt_resource *r;
 	int ret;
@@ -3119,6 +3120,19 @@ static int rdt_get_tree(struct fs_context *fc)
 						   RESCTRL_PICK_ANY_CPU);
 	}
 
+	rdt_root_kn = rdtgroup_default.kn;
+	/*
+	 * Keep root kn alive across kernfs_get_tree(). If kernfs_get_tree()
+	 * fails after superblock is created (ctx->kfc.new_sb_created is true)
+	 * kernfs_get_tree() will call rdt_kill_sb() itself.
+	 * rdt_kill_sb()->resctrl_unmount()->kernfs_destroy_root() is followed
+	 * by kernfs_kill_sb() that still needs to dereference root kn
+	 * after kernfs_destroy_root().
+	 *
+	 * Obtain reference with locks held to protect against interference
+	 * from resctrl_exit().
+	*/
+	kernfs_get(rdt_root_kn);
 	rdt_last_cmd_clear();
 	mutex_unlock(&rdtgroup_mutex);
 	cpus_read_unlock();
@@ -3130,6 +3144,7 @@ static int rdt_get_tree(struct fs_context *fc)
 	 */
 	if (!ctx->kfc.new_sb_created)
 		resctrl_unmount();
+	kernfs_put(rdt_root_kn);
 	return ret;
 
 out_mondata:

Reinette

[1] https://sashiko.dev/#/patchset/20260508182143.14592-1-tony.luck%40intel.com?part=3

#10 lore raw

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

Quoted 8 lines · Reinette Chatre

Hi Tony,

On 5/8/26 11:21 AM, Tony Luck wrote:

fs/resctrl/rdtgroup.c +4 -0
+	 * Obtain reference with locks held to protect against interference
+	 * from resctrl_exit().
+	*/
+	kernfs_get(rdt_root_kn);

[ ... ]

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

Quoted 5 lines
fs/resctrl/rdtgroup.c +1 -0
@@ -3130,6 +3144,7 @@ static int rdt_get_tree(struct fs_context *fc)
  	 */
  	if (!ctx->kfc.new_sb_created)
  		resctrl_unmount();
+	kernfs_put(rdt_root_kn);

I wonder if above should be protected against

	cpus_read_lock();
	mutex_lock(&rdtgroup_mutex);

like kernfs_get()?

thanks,
Chenyu

maintainer
#11 lore raw

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

Hi Chenyu,

Quoted 12 lines · Yu C
Quoted 8 lines · Reinette Chatre

Hi Tony,

On 5/8/26 11:21 AM, Tony Luck wrote:

+     * Obtain reference with locks held to protect against interference +     * from resctrl_exit().
+    */
+    kernfs_get(rdt_root_kn);

[ ... ]

Quoted 10 lines

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

Quoted 5 lines
fs/resctrl/rdtgroup.c +1 -0
@@ -3130,6 +3144,7 @@ static int rdt_get_tree(struct fs_context *fc)
        */
       if (!ctx->kfc.new_sb_created)
           resctrl_unmount();
+    kernfs_put(rdt_root_kn);

I wonder if above should be protected against
    cpus_read_lock();
    mutex_lock(&rdtgroup_mutex);
like kernfs_get()?

It is not obvious to me what this protection would be needed for. Do you have a troublesome scenario in mind?

rdt_root_kn is a local copy of rdtgroup_default.kn. The latter is indeed protected by the mutex. The reason why the kernfs_get() is protected by the mutex is to ensure what rdt_root_kn points to, rdtgroup_default.kn, remains accessible after the mutex is dropped. Nothing else modifies rdt_root_kn. I understand the appeal of symmetry but it is not clear to me what the extra locking is needed for here?

Could it perhaps make this flow easier to understand if the kernfs_get() is of the mutex protected rdtgroup_default.kn while the kernfs_put() is of the local backup copy? For example:

	/* Ensure root kn remains accessible after mutex is unlocked */
	kernfs_get(rdtgroup_default.kn);
	/*
	 * Make backup of rdtgroup_default.kn just in case one of the
	 * following flows (that sets rdtgroup_default.kn to NULL) run after
	 * the mutex is unlocked:
	 * resctrl_exit()->resctrl_fs_teardown()->rdtgroup_destroy_root()
	 * kernfs_get_tree()->deactivate_locked_super()->rdt_kill_sb()->resctrl_unmount()->resctrl_fs_teardown()->rdtgroup_destroy_root()
	 * These flows would not actually result in rdtgroup_default.kn
	 * being removed thanks to the additional reference.
	 /
	rdt_root_kn = rdtgroup_default.kn;
	mutex_unlock(&rdtgroup_mutex);
	cpus_read_unlock();

	ret = kernfs_get_tree(fc);
	if (!ctx->kfc.new_sb_created)
		resctrl_unmount();

	kernfs_put(rdt_root_kn);

Reinette

#12 lore raw

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

Hi Reinette,

Quoted 29 lines · Reinette Chatre

Hi Chenyu,

Quoted 12 lines · Yu C
Quoted 8 lines · Reinette Chatre

Hi Tony,

On 5/8/26 11:21 AM, Tony Luck wrote:

+     * Obtain reference with locks held to protect against interference +     * from resctrl_exit().
+    */
+    kernfs_get(rdt_root_kn);

[ ... ]

Quoted 10 lines

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

Quoted 5 lines
fs/resctrl/rdtgroup.c +1 -0
@@ -3130,6 +3144,7 @@ static int rdt_get_tree(struct fs_context *fc)
        */
       if (!ctx->kfc.new_sb_created)
           resctrl_unmount();
+    kernfs_put(rdt_root_kn);

I wonder if above should be protected against
    cpus_read_lock();
    mutex_lock(&rdtgroup_mutex);
like kernfs_get()?

It is not obvious to me what this protection would be needed for. Do you have a troublesome scenario in mind?

Quoted 7 lines

rdt_root_kn is a local copy of rdtgroup_default.kn. The latter is indeed protected by the mutex. The reason why the kernfs_get() is protected by the mutex is to ensure what rdt_root_kn points to, rdtgroup_default.kn, remains accessible after the mutex is dropped. Nothing else modifies rdt_root_kn. I understand the appeal of symmetry but it is not clear to me what the extra locking is needed for here?

Thanks for the detailed explanation. I now agree there is no need to protect kernfs_put() with a lock here only for symmetry reason. I previously thought racing conditions would occur if two code paths concurrently enter kernfs_put() and target the same data area. However, since kernfs_put() contains an atomic compare, only one code path can proceed, making the operation safe.

Quoted 4 lines

Could it perhaps make this flow easier to understand if the kernfs_get() is of the mutex protected rdtgroup_default.kn while the kernfs_put() is of the local backup copy? For example:

Quoted 11 lines

/* Ensure root kn remains accessible after mutex is unlocked */
kernfs_get(rdtgroup_default.kn);
/*
* Make backup of rdtgroup_default.kn just in case one of the
* following flows (that sets rdtgroup_default.kn to NULL) run after
* the mutex is unlocked:
* resctrl_exit()->resctrl_fs_teardown()->rdtgroup_destroy_root()
* kernfs_get_tree()->deactivate_locked_super()->rdt_kill_sb()->resctrl_unmount()->resctrl_fs_teardown()->rdtgroup_destroy_root()
* These flows would not actually result in rdtgroup_default.kn
* being removed thanks to the additional reference.
/

Yes, this comment is very clear and helpful.

thanks,
Chenyu

author maintainer
#13 lore raw

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

Quoted 62 lines · Yu C

Hi Reinette,

Quoted 29 lines · Reinette Chatre

Hi Chenyu,

Quoted 12 lines · Yu C
Quoted 8 lines · Reinette Chatre

Hi Tony,

On 5/8/26 11:21 AM, Tony Luck wrote:

+     * Obtain reference with locks held to protect against interference +     * from resctrl_exit().
+    */
+    kernfs_get(rdt_root_kn);

[ ... ]

Quoted 10 lines

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

Quoted 5 lines
fs/resctrl/rdtgroup.c +1 -0
@@ -3130,6 +3144,7 @@ static int rdt_get_tree(struct fs_context *fc)
        */
       if (!ctx->kfc.new_sb_created)
           resctrl_unmount();
+    kernfs_put(rdt_root_kn);

I wonder if above should be protected against
    cpus_read_lock();
    mutex_lock(&rdtgroup_mutex);
like kernfs_get()?

It is not obvious to me what this protection would be needed for. Do you have a troublesome scenario in mind?

Quoted 7 lines

rdt_root_kn is a local copy of rdtgroup_default.kn. The latter is indeed protected by the mutex. The reason why the kernfs_get() is protected by the mutex is to ensure what rdt_root_kn points to, rdtgroup_default.kn, remains accessible after the mutex is dropped. Nothing else modifies rdt_root_kn. I understand the appeal of symmetry but it is not clear to me what the extra locking is needed for here?

Thanks for the detailed explanation. I now agree there is no need to protect kernfs_put() with a lock here only for symmetry reason. I previously thought racing conditions would occur if two code paths concurrently enter kernfs_put() and target the same data area. However, since kernfs_put() contains an atomic compare, only one code path can proceed, making the operation safe.

Quoted 4 lines

Could it perhaps make this flow easier to understand if the kernfs_get() is of the mutex protected rdtgroup_default.kn while the kernfs_put() is of the local backup copy? For example:

Quoted 11 lines

/* Ensure root kn remains accessible after mutex is unlocked */
kernfs_get(rdtgroup_default.kn);
/*
* Make backup of rdtgroup_default.kn just in case one of the
* following flows (that sets rdtgroup_default.kn to NULL) run after
* the mutex is unlocked:
* resctrl_exit()->resctrl_fs_teardown()->rdtgroup_destroy_root()
* kernfs_get_tree()->deactivate_locked_super()->rdt_kill_sb()->resctrl_unmount()->resctrl_fs_teardown()->rdtgroup_destroy_root()
* These flows would not actually result in rdtgroup_default.kn
* being removed thanks to the additional reference.
/

Quoted 4 lines

Yes, this comment is very clear and helpful.

thanks,
Chenyu

Are we out of the woods yet? Applying these suggestions I now have:

	/* Ensure root kn remains accessible after mutex is unlocked */
	kernfs_get(rdtgroup_default.kn);

	/*
	 * Make backup of rdtgroup_default.kn just in case one of the
	 * following flows (that sets rdtgroup_default.kn to NULL) run after
	 * the mutex is unlocked:
	 * resctrl_exit()->resctrl_fs_teardown()->rdtgroup_destroy_root()
	 * kernfs_get_tree()->deactivate_locked_super()->rdt_kill_sb()->
	 *	resctrl_unmount()->resctrl_fs_teardown()->rdtgroup_destroy_root()
	 * These flows would not actually result in rdtgroup_default.kn
	 * being removed thanks to the additional reference.
	 */
	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();

resctrl_unmount() clears resctrl_mounted, so as soon locks are released a new mount attempt (maybe started a while ago, but blocked waiting for the mutex) can begin. I just want to confirm that won't stomp on anything left over from this failed mount that was waiting for this kernfs_put() to happen. I think it is OK, because the new mount is going to allocate all new structures. But there's been enough layers to this onion that I'd like to confirm.

	kernfs_put(rdt_root_kn);
	return ret;

-Tony

maintainer
#14 lore raw

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

Hi Tony,

On 5/13/26 12:51 PM, Luck, Tony wrote:
...

Quoted 37 lines

Are we out of the woods yet? Applying these suggestions I now have:

	/* Ensure root kn remains accessible after mutex is unlocked */
	kernfs_get(rdtgroup_default.kn);

	/*
	 * Make backup of rdtgroup_default.kn just in case one of the
	 * following flows (that sets rdtgroup_default.kn to NULL) run after
	 * the mutex is unlocked:
	 * resctrl_exit()->resctrl_fs_teardown()->rdtgroup_destroy_root()
	 * kernfs_get_tree()->deactivate_locked_super()->rdt_kill_sb()->
	 *	resctrl_unmount()->resctrl_fs_teardown()->rdtgroup_destroy_root()
	 * These flows would not actually result in rdtgroup_default.kn
	 * being removed thanks to the additional reference.
	 */
	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();

resctrl_unmount() clears resctrl_mounted, so as soon locks are released a new mount attempt (maybe started a while ago, but blocked waiting for the mutex) can begin. I just want to confirm that won't stomp on anything left over from this failed mount that was waiting for this kernfs_put() to happen. I think it is OK, because the new mount is going to allocate all new structures. But there's been enough layers to this onion that I'd like to confirm.

Quoted 2 lines
	kernfs_put(rdt_root_kn);
	return ret;

I agree with your analysis. I also think it highlights a sharp corner that may benefit from a comment. The rdt_root_kn is intentionally a local variable and the comments above explain that it is needed because of some flows that may set rdtgroup_default.kn to NULL. Based on the existing comments a reader may wonder why this cannot be optimized by using kernfs_root_to_node(rdt_root) instead of a local variable and that would be a problem in the scenario you describe.

Could comment changes below help to clarify the motivation for the reference and provide enough information to support future changes?

/*
* 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;
	...

Reinette

author maintainer
#15 lore raw

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

Quoted 69 lines · Reinette Chatre

Hi Tony,

On 5/13/26 12:51 PM, Luck, Tony wrote:
...

Quoted 37 lines

Are we out of the woods yet? Applying these suggestions I now have:

	/* Ensure root kn remains accessible after mutex is unlocked */
	kernfs_get(rdtgroup_default.kn);

	/*
	 * Make backup of rdtgroup_default.kn just in case one of the
	 * following flows (that sets rdtgroup_default.kn to NULL) run after
	 * the mutex is unlocked:
	 * resctrl_exit()->resctrl_fs_teardown()->rdtgroup_destroy_root()
	 * kernfs_get_tree()->deactivate_locked_super()->rdt_kill_sb()->
	 *	resctrl_unmount()->resctrl_fs_teardown()->rdtgroup_destroy_root()
	 * These flows would not actually result in rdtgroup_default.kn
	 * being removed thanks to the additional reference.
	 */
	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();

resctrl_unmount() clears resctrl_mounted, so as soon locks are released a new mount attempt (maybe started a while ago, but blocked waiting for the mutex) can begin. I just want to confirm that won't stomp on anything left over from this failed mount that was waiting for this kernfs_put() to happen. I think it is OK, because the new mount is going to allocate all new structures. But there's been enough layers to this onion that I'd like to confirm.

Quoted 2 lines
	kernfs_put(rdt_root_kn);
	return ret;

I agree with your analysis. I also think it highlights a sharp corner that may benefit from a comment. The rdt_root_kn is intentionally a local variable and the comments above explain that it is needed because of some flows that may set rdtgroup_default.kn to NULL. Based on the existing comments a reader may wonder why this cannot be optimized by using kernfs_root_to_node(rdt_root) instead of a local variable and that would be a problem in the scenario you describe.

Could comment changes below help to clarify the motivation for the reference and provide enough information to support future changes?

/*
* 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);

(quoted diff) +0 -0
 	/*
 	 * 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.

Review

Quoted 3 lines
(quoted diff) +0 -0
 	 */
 	rdt_root_kn = rdtgroup_default.kn;
...

Looks good. Replaced previous comments with these new ones. Thanks.

Quoted 3 lines

Reinette

-Tony

author maintainer
#16 lore raw

PATCH 4/4 fs/resctrl: Fix issues with worker threads when CPUs are taken offline

In reply to Tony Luck

From: Reinette Chatre <[email protected]>

Sashiko noticed[1] a user-after-free in the resctrl worker thread code where the rdt_l3_mon_domain structure was freed while the worker was blocked waiting for locks.

The root issue is that cancel_delayed_work() does not block in the case where the worker thread is executing. This results in the race that Sashiko noticed, but also causes problems when the CPU that has been chosen to service the worker thread is taken offline.

Note that worker threads are allowed to delete their own work_struct (see comment in kernel/workqueue.c:process_one_work()) so there can't be any problems on the return path from the worker in this case where the work_struct was deleted by other code while the worker was executing.

Indicate failure of cancel_delayed_work() calls in resctrl_offline_cpu() by setting d->mbm_work_cpu or d->cqm_work_cpu to nr_cpu_ids. Make the worker threads check to see if they are no longer bound to the right CPU. In this case search the L3 domain list for any domain(s) with the work cpu set to nr_cpu_ids. In the case where the last CPU was removed from a domain, the domain has been removed from the list and there is nothing to do. If the domain still exists, then restart the worker on any of the remaining CPUs.

Remove redundant cancel_delayed_work() calls from resctrl_offline_mon_domain().

Fixes 24247aeeabe9 ("x86/intel_rdt/cqm: Improve limbo list processing")
---
 fs/resctrl/monitor.c  | 55 +++++++++++++++++++++++++++++++++++++++++++
 fs/resctrl/rdtgroup.c | 27 +++++++++++++++------
 2 files changed, 75 insertions(+), 7 deletions(-)
fs/resctrl/monitor.c +55 -0
index 9fd901c78dc6..02434d11e024 100644
@@ -791,12 +791,38 @@ static void mbm_update(struct rdt_resource *r, struct rdt_l3_mon_domain *d,
  */
 void cqm_handle_limbo(struct work_struct *work)
 {
+	struct rdt_resource *r = resctrl_arch_get_resource(RDT_RESOURCE_L3);
 	unsigned long delay = msecs_to_jiffies(CQM_LIMBOCHECK_INTERVAL);
 	struct rdt_l3_mon_domain *d;
 
 	cpus_read_lock();
 	mutex_lock(&rdtgroup_mutex);
 
+	/*
+	 * Worker was blocked waiting for the CPU it was running on to go
+	 * offline. Handle two scenarios:
+	 * - Worker was running on the last CPU of a domain. The domain and
+	 *   thus the work_struct has been freed so do not attempt to obtain
+	 *   domain via container_of(). All remaining domains have limbo
+	 *   handlers so the loop will not find any domains needing a
+	 *   limbo handler. Just exit.
+	 * - Worker was running on CPU that just went offline with other
+	 *   CPUs in domain still running and available to take over the
+	 *   worker. Offline handler could not schedule a new worker on
+	 *   another CPU in the domain but signaled that this needs to be
+	 *   done by setting mbm_work_cpu to nr_cpu_ids. Find the domain
+	 *   that needs a worker and schedule it after the normal CQM
+	 *   interval.
+	 */
+	if (!is_percpu_thread()) {
+		list_for_each_entry(d, &r->mon_domains, hdr.list) {
+			if (d->cqm_work_cpu == nr_cpu_ids)
+				cqm_setup_limbo_handler(d, CQM_LIMBOCHECK_INTERVAL,
+							RESCTRL_PICK_ANY_CPU);
+		}
+		goto out_unlock;
+	}
+
 	d = container_of(work, struct rdt_l3_mon_domain, cqm_limbo.work);
 
 	__check_limbo(d, false);
@@ -808,6 +834,7 @@ void cqm_handle_limbo(struct work_struct *work)
 					 delay);
 	}
 
+out_unlock:
 	mutex_unlock(&rdtgroup_mutex);
 	cpus_read_unlock();
 }
@@ -852,6 +879,34 @@ void mbm_handle_overflow(struct work_struct *work)
 		goto out_unlock;
 
 	r = resctrl_arch_get_resource(RDT_RESOURCE_L3);
+
+	/*
+	 * Worker was blocked waiting for the CPU it was running on to go
+	 * offline. Handle two scenarios:
+	 * - Worker was running on the last CPU of a domain. The domain and
+	 *   thus the work_struct has been freed so do not attempt to obtain
+	 *   domain via container_of(). All remaining domains have overflow
+	 *   handlers so the loop will not find any domains needing an
+	 *   overflow handler. Just exit.
+	 * - Worker was running on CPU that just went offline with other
+	 *   CPUs in domain still running and available to take over the
+	 *   worker. Offline handler could not schedule a new worker on
+	 *   another CPU in the domain but signaled that this needs to be
+	 *   done by setting mbm_work_cpu to nr_cpu_ids. Find the domain
+	 *   that needs a worker and schedule it to run after the normal
+	 *   MBM interval. This is completely safe on CPUs with wide MBM
+	 *   counters. Likely OK for old CPUs with narrow counters as the
+	 *   MBM_OVERFLOW_INTERVAL was picked conservatively.
+	 */
+	if (!is_percpu_thread()) {
+		list_for_each_entry(d, &r->mon_domains, hdr.list) {
+			if (d->mbm_work_cpu == nr_cpu_ids)
+				mbm_setup_overflow_handler(d, MBM_OVERFLOW_INTERVAL,
+							   RESCTRL_PICK_ANY_CPU);
+		}
+		goto out_unlock;
+	}
+
 	d = container_of(work, struct rdt_l3_mon_domain, mbm_over.work);
 
 	list_for_each_entry(prgrp, &rdt_all_groups, rdtgroup_list) {
fs/resctrl/rdtgroup.c +20 -7
index 62e1e4c30f78..bab9afd5066e 100644
@@ -4343,8 +4343,7 @@ 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
@@ -4355,7 +4354,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);
@@ -4536,13 +4534,28 @@ 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 (cancel_delayed_work(&d->mbm_over)) {
+				mbm_setup_overflow_handler(d, 0, cpu);
+			} else {
+				/*
+				 * Unable to schedule work on new CPU if it
+				 * is currently running since the re-schedule
+				 * will just force new work to run on
+				 * current CPU. Mark domain's worker as
+				 * needing to be rescheduled to be handled
+				 * by worker itself.
+				 */
+				d->mbm_work_cpu = nr_cpu_ids;
+			}
 		}
 		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 (cancel_delayed_work(&d->cqm_limbo)) {
+				cqm_setup_limbo_handler(d, 0, cpu);
+			} else {
+				/* Same as mbm_work_cpu case above */
+				d->cqm_work_cpu = nr_cpu_ids;
+			}
 		}
 	}
 
Signature · 1 line
2.54.0
maintainer
#17 lore raw

Re: [PATCH 4/4] fs/resctrl: Fix issues with worker threads when CPUs are taken offline

Hi Tony,

Quoted 32 lines · Tony Luck
fs/resctrl/monitor.c +18 -0
index 9fd901c78dc6..02434d11e024 100644
@@ -791,12 +791,38 @@ static void mbm_update(struct rdt_resource *r, struct rdt_l3_mon_domain *d,
  */
 void cqm_handle_limbo(struct work_struct *work)
 {
+	struct rdt_resource *r = resctrl_arch_get_resource(RDT_RESOURCE_L3);
 	unsigned long delay = msecs_to_jiffies(CQM_LIMBOCHECK_INTERVAL);
 	struct rdt_l3_mon_domain *d;
 
 	cpus_read_lock();
 	mutex_lock(&rdtgroup_mutex);
 
+	/*
+	 * Worker was blocked waiting for the CPU it was running on to go
+	 * offline. Handle two scenarios:
+	 * - Worker was running on the last CPU of a domain. The domain and
+	 *   thus the work_struct has been freed so do not attempt to obtain
+	 *   domain via container_of(). All remaining domains have limbo
+	 *   handlers so the loop will not find any domains needing a
+	 *   limbo handler. Just exit.
+	 * - Worker was running on CPU that just went offline with other
+	 *   CPUs in domain still running and available to take over the
+	 *   worker. Offline handler could not schedule a new worker on
+	 *   another CPU in the domain but signaled that this needs to be
+	 *   done by setting mbm_work_cpu to nr_cpu_ids. Find the domain
+	 *   that needs a worker and schedule it after the normal CQM
+	 *   interval.
+	 */
+	if (!is_percpu_thread()) {

fs/resctrl/monitor.c static void mbm_update(struct rdt_resource *r, struct rdt_l3_mon_domain *d, · line 827

Quoted 10 lines
fs/resctrl/monitor.c +8 -0
+		list_for_each_entry(d, &r->mon_domains, hdr.list) {
+			if (d->cqm_work_cpu == nr_cpu_ids)
+				cqm_setup_limbo_handler(d, CQM_LIMBOCHECK_INTERVAL,
+							RESCTRL_PICK_ANY_CPU);
+		}
+		goto out_unlock;
+	}
+
 	d = container_of(work, struct rdt_l3_mon_domain, cqm_limbo.work);
 

The issue reported by sashiko [1] is not clear to me. The claim is that if above worker is running on last CPU of a domain and is blocked at cpus_read_lock() at the time the CPU it is running on is rapidly offlined and then onlined, then when the worker can run it will find is_percpu_thread() to be true but the domain structure will be freed.
I am not familiar with the CPU hotplug locking but from what I can tell, in this scenario, the cpus_write_lock() in _cpu_up() will block since there is a pending reader and the worker will be able to run before the CPU online work is done. The scenario presented thus seems to be defeated by percpu-rwsem semantics. What do you think of the scenario presented in [1]?

Reinette

[1] https://sashiko.dev/#/patchset/20260508182143.14592-1-tony.luck%40intel.com?part=4

author maintainer
#18 lore raw

Re: [PATCH 4/4] fs/resctrl: Fix issues with worker threads when CPUs are taken offline

Quoted 48 lines · Reinette Chatre

Hi Tony,

Quoted 32 lines · Tony Luck
fs/resctrl/monitor.c +18 -0
index 9fd901c78dc6..02434d11e024 100644
@@ -791,12 +791,38 @@ static void mbm_update(struct rdt_resource *r, struct rdt_l3_mon_domain *d,
  */
 void cqm_handle_limbo(struct work_struct *work)
 {
+	struct rdt_resource *r = resctrl_arch_get_resource(RDT_RESOURCE_L3);
 	unsigned long delay = msecs_to_jiffies(CQM_LIMBOCHECK_INTERVAL);
 	struct rdt_l3_mon_domain *d;
 
 	cpus_read_lock();
 	mutex_lock(&rdtgroup_mutex);
 
+	/*
+	 * Worker was blocked waiting for the CPU it was running on to go
+	 * offline. Handle two scenarios:
+	 * - Worker was running on the last CPU of a domain. The domain and
+	 *   thus the work_struct has been freed so do not attempt to obtain
+	 *   domain via container_of(). All remaining domains have limbo
+	 *   handlers so the loop will not find any domains needing a
+	 *   limbo handler. Just exit.
+	 * - Worker was running on CPU that just went offline with other
+	 *   CPUs in domain still running and available to take over the
+	 *   worker. Offline handler could not schedule a new worker on
+	 *   another CPU in the domain but signaled that this needs to be
+	 *   done by setting mbm_work_cpu to nr_cpu_ids. Find the domain
+	 *   that needs a worker and schedule it after the normal CQM
+	 *   interval.
+	 */
+	if (!is_percpu_thread()) {

fs/resctrl/monitor.c static void mbm_update(struct rdt_resource *r, struct rdt_l3_mon_domain *d, · line 827

Quoted 10 lines
fs/resctrl/monitor.c +8 -0
+		list_for_each_entry(d, &r->mon_domains, hdr.list) {
+			if (d->cqm_work_cpu == nr_cpu_ids)
+				cqm_setup_limbo_handler(d, CQM_LIMBOCHECK_INTERVAL,
+							RESCTRL_PICK_ANY_CPU);
+		}
+		goto out_unlock;
+	}
+
 	d = container_of(work, struct rdt_l3_mon_domain, cqm_limbo.work);
 

The issue reported by sashiko [1] is not clear to me. The claim is that if above worker is running on last CPU of a domain and is blocked at cpus_read_lock() at the time

Quoted 8 lines

the CPU it is running on is rapidly offlined and then onlined, then when the worker can run it will find is_percpu_thread() to be true but the domain structure will be freed.
I am not familiar with the CPU hotplug locking but from what I can tell, in this scenario, the cpus_write_lock() in _cpu_up() will block since there is a pending reader and the worker will be able to run before the CPU online work is done. The scenario presented thus seems to be defeated by percpu-rwsem semantics. What do you think of the scenario presented in [1]?

I'm also not familiar with the fine details of the CPU offline/online flow ... but this claim by sashiko seems fishy:

4. Before the worker thread resumes, a CPU online operation occurs for the same CPU. The workqueue subsystem rebinds the worker thread to the CPU (restoring nr_cpus_allowed == 1).

That sounds like a thing that could be done, but did code actually get written for this obscure corner case?

I asked Gemini about races between readers and writers for the cpu_lock, it says that queued readers will run before queued writers because in this case cpus_write_lock() is considered an infrequent heavy weight operation, so priority is given to readers (who can execute in parallel). This matches your analysis above.

Quoted 5 lines

Reinette

[1] https://sashiko.dev/#/patchset/20260508182143.14592-1-tony.luck%40intel.com?part=4

-Tony

Quoted 1 line