[PATCH v8 01/21] x86/resctrl: Fix rdtgroup_mkdir()'s unlocked use of kernfs_node::name

James Morse posted 21 patches 8 months, 1 week ago
There is a newer version of this series
[PATCH v8 01/21] x86/resctrl: Fix rdtgroup_mkdir()'s unlocked use of kernfs_node::name
Posted by James Morse 8 months, 1 week ago
Since commit 741c10b096bc ("kernfs: Use RCU to access kernfs_node::name."),
a helper rdt_kn_name() that checks that rdtgroup_mutex is held has been
used for all accesses to the kernfs node name.

rdtgroup_mkdir() uses the name to determine if a valid monitor group is
being created by checking the parent name is "mon_groups". This is done
without holding rdtgroup_mutex, and now triggers the following warning:
| WARNING: suspicious RCU usage
| 6.15.0-rc1 #4465 Tainted: G            E
| -----------------------------
| arch/x86/kernel/cpu/resctrl/internal.h:408 suspicious rcu_dereference_check() usage!
[...]
| Call Trace:
|  <TASK>
|  dump_stack_lvl+0x6c/0xa0
|  lockdep_rcu_suspicious.cold+0x4e/0x96
|  is_mon_groups+0xba/0xd0
|  rdtgroup_mkdir+0x118/0x1970
|  kernfs_iop_mkdir+0xfa/0x1a0
|  vfs_mkdir+0x456/0x760
|  do_mkdirat+0x257/0x310
|  __x64_sys_mkdir+0xd4/0x120
|  do_syscall_64+0x6d/0x150
|  entry_SYSCALL_64_after_hwframe+0x76/0x7e

Creating a control or monitor group calls mkdir_rdt_prepare(), which uses
rdtgroup_kn_lock_live() to take the rdtgroup_mutex.

To avoid taking and dropping the lock, move the check for the monitor group
name and position into mkdir_rdt_prepare() so that it occurs under
rdtgroup_mutex. Hoist is_mon_groups() earlier in the file.

CC: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Fixes: 741c10b096bc ("kernfs: Use RCU to access kernfs_node::name.")
Signed-off-by: James Morse <james.morse@arm.com>
Acked-by: Ingo Molnar <mingo@kernel.org>
Reviewed-by: Reinette Chatre <reinette.chatre@intel.com>

---
Changes since v1:
 * Add a word to a comment.
---
 arch/x86/kernel/cpu/resctrl/rdtgroup.c | 48 +++++++++++++++-----------
 1 file changed, 27 insertions(+), 21 deletions(-)

diff --git a/arch/x86/kernel/cpu/resctrl/rdtgroup.c b/arch/x86/kernel/cpu/resctrl/rdtgroup.c
index 93ec829015f1..776c8e347654 100644
--- a/arch/x86/kernel/cpu/resctrl/rdtgroup.c
+++ b/arch/x86/kernel/cpu/resctrl/rdtgroup.c
@@ -3553,6 +3553,22 @@ static void mkdir_rdt_prepare_rmid_free(struct rdtgroup *rgrp)
 		free_rmid(rgrp->closid, rgrp->mon.rmid);
 }
 
+/*
+ * We allow creating mon groups only with in a directory called "mon_groups"
+ * which is present in every ctrl_mon group. Check if this is a valid
+ * "mon_groups" directory.
+ *
+ * 1. The directory should be named "mon_groups".
+ * 2. The mon group itself should "not" be named "mon_groups".
+ *   This makes sure "mon_groups" directory always has a ctrl_mon group
+ *   as parent.
+ */
+static bool is_mon_groups(struct kernfs_node *kn, const char *name)
+{
+	return (!strcmp(rdt_kn_name(kn), "mon_groups") &&
+		strcmp(name, "mon_groups"));
+}
+
 static int mkdir_rdt_prepare(struct kernfs_node *parent_kn,
 			     const char *name, umode_t mode,
 			     enum rdt_group_type rtype, struct rdtgroup **r)
@@ -3568,6 +3584,15 @@ static int mkdir_rdt_prepare(struct kernfs_node *parent_kn,
 		goto out_unlock;
 	}
 
+	/*
+	 * Check that the parent directory for a monitor group is a
+	 * "mon_groups" directory.
+	 */
+	if (rtype == RDTMON_GROUP && !is_mon_groups(parent_kn, name)) {
+		ret = -EPERM;
+		goto out_unlock;
+	}
+
 	if (rtype == RDTMON_GROUP &&
 	    (prdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
 	     prdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)) {
@@ -3751,22 +3776,6 @@ static int rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn,
 	return ret;
 }
 
-/*
- * We allow creating mon groups only with in a directory called "mon_groups"
- * which is present in every ctrl_mon group. Check if this is a valid
- * "mon_groups" directory.
- *
- * 1. The directory should be named "mon_groups".
- * 2. The mon group itself should "not" be named "mon_groups".
- *   This makes sure "mon_groups" directory always has a ctrl_mon group
- *   as parent.
- */
-static bool is_mon_groups(struct kernfs_node *kn, const char *name)
-{
-	return (!strcmp(rdt_kn_name(kn), "mon_groups") &&
-		strcmp(name, "mon_groups"));
-}
-
 static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
 			  umode_t mode)
 {
@@ -3782,11 +3791,8 @@ static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
 	if (resctrl_arch_alloc_capable() && parent_kn == rdtgroup_default.kn)
 		return rdtgroup_mkdir_ctrl_mon(parent_kn, name, mode);
 
-	/*
-	 * If RDT monitoring is supported and the parent directory is a valid
-	 * "mon_groups" directory, add a monitoring subdirectory.
-	 */
-	if (resctrl_arch_mon_capable() && is_mon_groups(parent_kn, name))
+	/* Else, attempt to add a monitoring subdirectory. */
+	if (resctrl_arch_mon_capable())
 		return rdtgroup_mkdir_mon(parent_kn, name, mode);
 
 	return -EPERM;
-- 
2.20.1
Re: [PATCH v8 01/21] x86/resctrl: Fix rdtgroup_mkdir()'s unlocked use of kernfs_node::name
Posted by Fenghua Yu 8 months, 1 week ago
Hi, James,

On 4/11/25 09:42, James Morse wrote:
> Since commit 741c10b096bc ("kernfs: Use RCU to access kernfs_node::name."),
> a helper rdt_kn_name() that checks that rdtgroup_mutex is held has been
> used for all accesses to the kernfs node name.
>
> rdtgroup_mkdir() uses the name to determine if a valid monitor group is
> being created by checking the parent name is "mon_groups". This is done
> without holding rdtgroup_mutex, and now triggers the following warning:
> | WARNING: suspicious RCU usage
> | 6.15.0-rc1 #4465 Tainted: G            E
> | -----------------------------
> | arch/x86/kernel/cpu/resctrl/internal.h:408 suspicious rcu_dereference_check() usage!
> [...]
> | Call Trace:
> |  <TASK>
> |  dump_stack_lvl+0x6c/0xa0
> |  lockdep_rcu_suspicious.cold+0x4e/0x96
> |  is_mon_groups+0xba/0xd0
> |  rdtgroup_mkdir+0x118/0x1970
> |  kernfs_iop_mkdir+0xfa/0x1a0
> |  vfs_mkdir+0x456/0x760
> |  do_mkdirat+0x257/0x310
> |  __x64_sys_mkdir+0xd4/0x120
> |  do_syscall_64+0x6d/0x150
> |  entry_SYSCALL_64_after_hwframe+0x76/0x7e
>
> Creating a control or monitor group calls mkdir_rdt_prepare(), which uses
> rdtgroup_kn_lock_live() to take the rdtgroup_mutex.
>
> To avoid taking and dropping the lock, move the check for the monitor group
> name and position into mkdir_rdt_prepare() so that it occurs under
> rdtgroup_mutex. Hoist is_mon_groups() earlier in the file.
>
> CC: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
> Fixes: 741c10b096bc ("kernfs: Use RCU to access kernfs_node::name.")
> Signed-off-by: James Morse <james.morse@arm.com>
> Acked-by: Ingo Molnar <mingo@kernel.org>
> Reviewed-by: Reinette Chatre <reinette.chatre@intel.com>
>
> ---
> Changes since v1:
>   * Add a word to a comment.
> ---
>   arch/x86/kernel/cpu/resctrl/rdtgroup.c | 48 +++++++++++++++-----------
>   1 file changed, 27 insertions(+), 21 deletions(-)
>
> diff --git a/arch/x86/kernel/cpu/resctrl/rdtgroup.c b/arch/x86/kernel/cpu/resctrl/rdtgroup.c
> index 93ec829015f1..776c8e347654 100644
> --- a/arch/x86/kernel/cpu/resctrl/rdtgroup.c
> +++ b/arch/x86/kernel/cpu/resctrl/rdtgroup.c
> @@ -3553,6 +3553,22 @@ static void mkdir_rdt_prepare_rmid_free(struct rdtgroup *rgrp)
>   		free_rmid(rgrp->closid, rgrp->mon.rmid);
>   }
>   
> +/*
> + * We allow creating mon groups only with in a directory called "mon_groups"
> + * which is present in every ctrl_mon group. Check if this is a valid
> + * "mon_groups" directory.
> + *
> + * 1. The directory should be named "mon_groups".
> + * 2. The mon group itself should "not" be named "mon_groups".
> + *   This makes sure "mon_groups" directory always has a ctrl_mon group
> + *   as parent.
> + */
> +static bool is_mon_groups(struct kernfs_node *kn, const char *name)
> +{
> +	return (!strcmp(rdt_kn_name(kn), "mon_groups") &&
> +		strcmp(name, "mon_groups"));
> +}
> +
>   static int mkdir_rdt_prepare(struct kernfs_node *parent_kn,
>   			     const char *name, umode_t mode,
>   			     enum rdt_group_type rtype, struct rdtgroup **r)
> @@ -3568,6 +3584,15 @@ static int mkdir_rdt_prepare(struct kernfs_node *parent_kn,
>   		goto out_unlock;
>   	}
>   
> +	/*
> +	 * Check that the parent directory for a monitor group is a
> +	 * "mon_groups" directory.

This patch is in upstream now after 6.15-rc1. But the commit in upstream 
is slightly different from this patch:

+        * Check that the parent directory for a monitor group is a 
"mon_groups"
+        * directory.

This slight difference causes patch #17 merge conflict.

You may drop this patch and also fix patch #17 to fix the conflict? 
Please see my comment in patch #17.

> +	 */
> +	if (rtype == RDTMON_GROUP && !is_mon_groups(parent_kn, name)) {
> +		ret = -EPERM;
> +		goto out_unlock;
> +	}
> +
>   	if (rtype == RDTMON_GROUP &&
>   	    (prdtgrp->mode == RDT_MODE_PSEUDO_LOCKSETUP ||
>   	     prdtgrp->mode == RDT_MODE_PSEUDO_LOCKED)) {
> @@ -3751,22 +3776,6 @@ static int rdtgroup_mkdir_ctrl_mon(struct kernfs_node *parent_kn,
>   	return ret;
>   }
>   
> -/*
> - * We allow creating mon groups only with in a directory called "mon_groups"
> - * which is present in every ctrl_mon group. Check if this is a valid
> - * "mon_groups" directory.
> - *
> - * 1. The directory should be named "mon_groups".
> - * 2. The mon group itself should "not" be named "mon_groups".
> - *   This makes sure "mon_groups" directory always has a ctrl_mon group
> - *   as parent.
> - */
> -static bool is_mon_groups(struct kernfs_node *kn, const char *name)
> -{
> -	return (!strcmp(rdt_kn_name(kn), "mon_groups") &&
> -		strcmp(name, "mon_groups"));
> -}
> -
>   static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
>   			  umode_t mode)
>   {
> @@ -3782,11 +3791,8 @@ static int rdtgroup_mkdir(struct kernfs_node *parent_kn, const char *name,
>   	if (resctrl_arch_alloc_capable() && parent_kn == rdtgroup_default.kn)
>   		return rdtgroup_mkdir_ctrl_mon(parent_kn, name, mode);
>   
> -	/*
> -	 * If RDT monitoring is supported and the parent directory is a valid
> -	 * "mon_groups" directory, add a monitoring subdirectory.
> -	 */
> -	if (resctrl_arch_mon_capable() && is_mon_groups(parent_kn, name))
> +	/* Else, attempt to add a monitoring subdirectory. */
> +	if (resctrl_arch_mon_capable())
>   		return rdtgroup_mkdir_mon(parent_kn, name, mode);
>   
>   	return -EPERM;

Thanks.

-Fenghua