[PATCH] scsi: megaraid_sas: Fix debugfs setup error handling

From: Lei Chen

Date: Wed Oct 07 2026 - 10:18:42 EST


debugfs_create_dir() and debugfs_create_file() return error pointers on
failure, but megasas_setup_debugfs() only checks for NULL. As a result,
setup continues after directory creation fails, and a failure to create
raidmap_dump leaves the per-adapter directory behind.

Skip per-adapter setup if the driver debugfs root is unavailable, and
use IS_ERR_OR_NULL() to check directory and file creation results so
that the existing error paths also handle error pointers.

With the corrected error check, file creation failure removes the
per-adapter directory. Clear the stored directory pointer as well so
teardown does not attempt to remove the freed dentry again.

Fixes: ba53572bf02d ("scsi: megaraid_sas: Export RAID map through debugfs")
Signed-off-by: Lei Chen <lei.chen@xxxxxxxxxx>
---
drivers/scsi/megaraid/megaraid_sas_debugfs.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/drivers/scsi/megaraid/megaraid_sas_debugfs.c b/drivers/scsi/megaraid/megaraid_sas_debugfs.c
index 81698cd83f5a..556e01fc25e8 100644
--- a/drivers/scsi/megaraid/megaraid_sas_debugfs.c
+++ b/drivers/scsi/megaraid/megaraid_sas_debugfs.c
@@ -126,13 +126,16 @@ megasas_setup_debugfs(struct megasas_instance *instance)

fusion = instance->ctrl_context;

+ if (IS_ERR_OR_NULL(megasas_debugfs_root))
+ return;
+
if (fusion) {
snprintf(name, sizeof(name),
"scsi_host%d", instance->host->host_no);
if (!instance->debugfs_root) {
instance->debugfs_root =
debugfs_create_dir(name, megasas_debugfs_root);
- if (!instance->debugfs_root) {
+ if (IS_ERR_OR_NULL(instance->debugfs_root)) {
dev_err(&instance->pdev->dev,
"Cannot create per adapter debugfs directory\n");
return;
@@ -144,10 +147,11 @@ megasas_setup_debugfs(struct megasas_instance *instance)
debugfs_create_file(name, S_IRUGO,
instance->debugfs_root, instance,
&megasas_debugfs_raidmap_fops);
- if (!instance->raidmap_dump) {
+ if (IS_ERR_OR_NULL(instance->raidmap_dump)) {
dev_err(&instance->pdev->dev,
"Cannot create raidmap debugfs file\n");
debugfs_remove(instance->debugfs_root);
+ instance->debugfs_root = NULL;
return;
}
}
--
2.43.0