The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH] scsi: mpi3mr: Handle SAS port and rphy allocation failures
@ 2026-08-17  5:26 Triet Hoang
  0 siblings, 0 replies; only message in thread
From: Triet Hoang @ 2026-08-17  5:26 UTC (permalink / raw)
  To: sathya.prakash
  Cc: kashyap.desai, sumit.saxena, sreekanth.reddy, James.Bottomley,
	martin.petersen, mpi3mr-linuxdrv.pdl, linux-scsi, linux-kernel,
	Triet Hoang

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 <triet.hoang.dev@gmail.com>
---
 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


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-08-17  5:26 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-17  5:26 [PATCH] scsi: mpi3mr: Handle SAS port and rphy allocation failures Triet Hoang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox