[PATCH] scsi: mpi3mr: Handle SAS port and rphy allocation failures

Triet Hoang <[email protected]>
Newsgroups gmane.linux.kernel,gmane.linux.scsi
Message-ID <[email protected]>
Check the return value of sas_port_alloc_num() and handle failures
before attempting to add the port.

Free the SAS port if sas_port_add() fails. Also handle failures from
sas_end_device_alloc() and sas_expander_alloc() by removing the PHYs
from the port, deleting the SAS port, and returning failure.

This prevents NULL pointer dereferences and ensures resources allocated
during SAS port creation are properly cleaned up on error paths.

Signed-off-by: Triet Hoang <[email protected]>
---
 drivers/scsi/mpi3mr/mpi3mr_transport.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/drivers/scsi/mpi3mr/mpi3mr_transport.c b/drivers/scsi/mpi3mr/mpi3mr_transport.c
index 240f67a8e2e3..2fbfd178c539 100644
--- a/drivers/scsi/mpi3mr/mpi3mr_transport.c
+++ b/drivers/scsi/mpi3mr/mpi3mr_transport.c
@@ -1428,9 +1428,17 @@ static struct mpi3mr_sas_port *mpi3mr_sas_port_add(struct mpi3mr_ioc *mrioc,
 	}
 
 	port = sas_port_alloc_num(mr_sas_node->parent_dev);
+	if (!port) {
+		ioc_err(mrioc, "failure at %s:%d/%s()!\n",
+		    __FILE__, __LINE__, __func__);
+		goto out_fail;
+	}
+
 	if ((sas_port_add(port))) {
 		ioc_err(mrioc, "failure at %s:%d/%s()!\n",
 		    __FILE__, __LINE__, __func__);
+		sas_port_free(port);
+		port = NULL;
 		goto out_fail;
 	}
 
@@ -1455,6 +1463,19 @@ static struct mpi3mr_sas_port *mpi3mr_sas_port_add(struct mpi3mr_ioc *mrioc,
 		rphy = sas_expander_alloc(port,
 		    mr_sas_port->remote_identify.device_type);
 	}
+
+	if (!rphy) {
+		ioc_err(mrioc, "failure at %s:%d/%s()!\n",
+		    __FILE__, __LINE__, __func__);
+		list_for_each_entry(mr_sas_phy, &mr_sas_port->phy_list, port_siblings) {
+			mr_sas_phy->phy_belongs_to_port = 0;
+			mr_sas_phy->hba_port = NULL;
+			sas_port_delete_phy(port, mr_sas_phy->phy);
+		}
+		sas_port_delete(port);
+		goto out_fail;
+	}
+
 	rphy->identify = mr_sas_port->remote_identify;
 
 	if (mrioc->current_event)
-- 
2.53.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.