Skip to contentSkip to conversation

cxl/core: Skip einj_inject creation when devm_add_action_or_reset() fails

PATCH Applied Guixin Liu 4 participants 4 messages Sep 15 – Sep 18 CXL

lore
Share & tools
lore

Guixin Liu

author
#0 lore raw

PATCH 1/1 cxl/core: Skip einj_inject creation when devm_add_action_or_reset() fails

cxl_debugfs_create_dport_dir() ignores the devm_add_action_or_reset() result and creates the einj_inject file below @dir unconditionally.

When the devres allocation fails, remove_debugfs() has already run before devm_add_action_or_reset() returns, i.e. debugfs_remove() has released @dir. The subsequent debugfs_create_file() would then use that dentry as its parent although its final reference was dropped in the removal and its memory is subject to call_rcu() delayed freeing.

Skip the file creation in that case, consistent with how the rest of the helper tolerates debugfs failures.

Fixes 9185b1a3043c ("cxl/core: Fix dport use-after-free via the einj_inject debugfs file")
---
 drivers/cxl/core/port.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/cxl/core/port.c +2 -1
index 8d715739995b..95c0be57ed53 100644
@@ -837,7 +837,8 @@ static void cxl_debugfs_create_dport_dir(struct cxl_dport *dport)
 
 	dir = cxl_debugfs_create_dir(dev_name(dport->dport_dev));
 
-	devm_add_action_or_reset(dport_to_host(dport), remove_debugfs, dir);
+	if (devm_add_action_or_reset(dport_to_host(dport), remove_debugfs, dir))
+		return;
 
 	debugfs_create_file("einj_inject", 0200, dir, dport,
 			    &cxl_einj_inject_fops);
Signature · 1 line
2.43.7
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · On Tue, 15 Sep 2026 14:01:38 +0800 Guixin Liu <[email protected]> wrote:

cxl_debugfs_create_dport_dir() ignores the devm_add_action_or_reset() result and creates the einj_inject file below @dir unconditionally.

When the devres allocation fails, remove_debugfs() has already run before devm_add_action_or_reset() returns, i.e. debugfs_remove() has released @dir. The subsequent debugfs_create_file() would then use that dentry as its parent although its final reference was dropped in the removal and its memory is subject to call_rcu() delayed freeing.

Quoted 5 lines

Skip the file creation in that case, consistent with how the rest of the helper tolerates debugfs failures.

Fixes 9185b1a3043c ("cxl/core: Fix dport use-after-free via the einj_inject debugfs file")
reviewer Reviewed-by on v1 1/1
#2 lore raw
In reply to Guixin Liu
Quoted 9 lines · Guixin Liu

cxl_debugfs_create_dport_dir() ignores the devm_add_action_or_reset() result and creates the einj_inject file below @dir unconditionally.

When the devres allocation fails, remove_debugfs() has already run before devm_add_action_or_reset() returns, i.e. debugfs_remove() has released @dir. The subsequent debugfs_create_file() would then use that dentry as its parent although its final reference was dropped in the removal and its memory is subject to call_rcu() delayed freeing.

Quoted 5 lines

Skip the file creation in that case, consistent with how the rest of the helper tolerates debugfs failures.

Fixes 9185b1a3043c ("cxl/core: Fix dport use-after-free via the einj_inject debugfs file")
Quoted 18 lines
---
 drivers/cxl/core/port.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/cxl/core/port.c +2 -1
index 8d715739995b..95c0be57ed53 100644
@@ -837,7 +837,8 @@ static void cxl_debugfs_create_dport_dir(struct cxl_dport *dport)
 
 	dir = cxl_debugfs_create_dir(dev_name(dport->dport_dev));
 
-	devm_add_action_or_reset(dport_to_host(dport), remove_debugfs, dir);
+	if (devm_add_action_or_reset(dport_to_host(dport), remove_debugfs, dir))
+		return;
 
 	debugfs_create_file("einj_inject", 0200, dir, dport,
 			    &cxl_einj_inject_fops);
maintainer Applied
#3 lore raw
In reply to Guixin Liu
Quoted 9 lines · Guixin Liu

cxl_debugfs_create_dport_dir() ignores the devm_add_action_or_reset() result and creates the einj_inject file below @dir unconditionally.

When the devres allocation fails, remove_debugfs() has already run before devm_add_action_or_reset() returns, i.e. debugfs_remove() has released @dir. The subsequent debugfs_create_file() would then use that dentry as its parent although its final reference was dropped in the removal and its memory is subject to call_rcu() delayed freeing.

Quoted 5 lines

Skip the file creation in that case, consistent with how the rest of the helper tolerates debugfs failures.

Fixes 9185b1a3043c ("cxl/core: Fix dport use-after-free via the einj_inject debugfs file")

Applied to cxl/next:
b25af6afd2f2

Quoted 18 lines
---
 drivers/cxl/core/port.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/cxl/core/port.c +2 -1
index 8d715739995b..95c0be57ed53 100644
@@ -837,7 +837,8 @@ static void cxl_debugfs_create_dport_dir(struct cxl_dport *dport)
 
 	dir = cxl_debugfs_create_dir(dev_name(dport->dport_dev));
 
-	devm_add_action_or_reset(dport_to_host(dport), remove_debugfs, dir);
+	if (devm_add_action_or_reset(dport_to_host(dport), remove_debugfs, dir))
+		return;
 
 	debugfs_create_file("einj_inject", 0200, dir, dport,
 			    &cxl_einj_inject_fops);