mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 0/3] scsi: libsas: Handle errors in 3 functions
@ 2026-08-11 16:22 Eshaan Deshmukh
  2026-08-11 16:22 ` [PATCH v2 1/3] scsi: libsas: Handle errors in sas_ex_add_parent_port() Eshaan Deshmukh
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Eshaan Deshmukh @ 2026-08-11 16:22 UTC (permalink / raw)
  To: Martin K . Petersen, James E . J . Bottomley
  Cc: John Garry, Jason Yan, linux-scsi, linux-kernel, Eshaan Deshmukh

These 3 functions include several uses of BUG_ON() and lack error
management. Change all 3 functions to handle their errors in better
ways.

Eshaan Deshmukh (3):
  scsi: libsas: Handle errors in sas_ex_add_parent_port()
  scsi: libsas: Handle errors in sas_set_ex_phy()
  scsi: libsas: Handle errors in  sas_ex_discover_expander()

 drivers/scsi/libsas/sas_expander.c | 71 +++++++++++++++++++++---------
 1 file changed, 49 insertions(+), 22 deletions(-)

-- 
2.55.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2 1/3] scsi: libsas: Handle errors in sas_ex_add_parent_port()
  2026-08-11 16:22 [PATCH v2 0/3] scsi: libsas: Handle errors in 3 functions Eshaan Deshmukh
@ 2026-08-11 16:22 ` Eshaan Deshmukh
  2026-08-11 16:22 ` [PATCH v2 2/3] scsi: libsas: Handle errors in sas_set_ex_phy() Eshaan Deshmukh
  2026-08-11 16:22 ` [PATCH v2 3/3] scsi: libsas: Handle errors in sas_ex_discover_expander() Eshaan Deshmukh
  2 siblings, 0 replies; 6+ messages in thread
From: Eshaan Deshmukh @ 2026-08-11 16:22 UTC (permalink / raw)
  To: Martin K . Petersen, James E . J . Bottomley
  Cc: John Garry, Jason Yan, linux-scsi, linux-kernel, Eshaan Deshmukh

The function sas_ex_add_parent_port() uses BUG_ON() if sas_port_alloc()
or sas_port_add() fails. Change sas_ex_add_parent_port() to return error
codes to the caller and free the sas_port_alloc() allocation. Also
change sas_ex_discover_dev() to handle errors returned by
sas_ex_add_parent_port().

Signed-off-by: Eshaan Deshmukh <eshaan2031@icloud.com>
---
 drivers/scsi/libsas/sas_expander.c | 30 +++++++++++++++++++++---------
 1 file changed, 21 insertions(+), 9 deletions(-)

diff --git a/drivers/scsi/libsas/sas_expander.c b/drivers/scsi/libsas/sas_expander.c
index f471ab464..f1a1417b0 100644
--- a/drivers/scsi/libsas/sas_expander.c
+++ b/drivers/scsi/libsas/sas_expander.c
@@ -33,19 +33,29 @@ static void sas_port_add_ex_phy(struct sas_port *port, struct ex_phy *ex_phy)
 	ex_phy->phy_state = PHY_DEVICE_DISCOVERED;
 }
 
-static void sas_ex_add_parent_port(struct domain_device *dev, int phy_id)
+static int sas_ex_add_parent_port(struct domain_device *dev, int phy_id)
 {
 	struct expander_device *ex = &dev->ex_dev;
 	struct ex_phy *ex_phy = &ex->ex_phy[phy_id];
+	int error;
 
 	if (!ex->parent_port) {
 		ex->parent_port = sas_port_alloc(&dev->rphy->dev, phy_id);
-		/* FIXME: error handling */
-		BUG_ON(!ex->parent_port);
-		BUG_ON(sas_port_add(ex->parent_port));
+		if (!ex->parent_port)
+			return -ENOMEM;
+
+		error = sas_port_add(ex->parent_port);
+		if (error) {
+			sas_port_free(ex->parent_port);
+			ex->parent_port = NULL;
+			return error;
+		}
+
 		sas_port_mark_backlink(ex->parent_port);
 	}
 	sas_port_add_ex_phy(ex->parent_port, ex_phy);
+
+	return 0;
 }
 
 /* ---------- SMP task management ---------- */
@@ -981,12 +991,14 @@ static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)
 	}
 
 	/* Parent and domain coherency */
-	if (!dev->parent && sas_phy_match_port_addr(dev->port, ex_phy)) {
-		sas_ex_add_parent_port(dev, phy_id);
-		return 0;
-	}
+	if (!dev->parent && sas_phy_match_port_addr(dev->port, ex_phy))
+		return sas_ex_add_parent_port(dev, phy_id);
+
 	if (dev->parent && sas_phy_match_dev_addr(dev->parent, ex_phy)) {
-		sas_ex_add_parent_port(dev, phy_id);
+		res = sas_ex_add_parent_port(dev, phy_id);
+		if (res)
+			return res;
+
 		if (ex_phy->routing_attr == TABLE_ROUTING)
 			sas_configure_phy(dev, phy_id, dev->port->sas_addr, 1);
 		return 0;
-- 
2.55.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2 2/3] scsi: libsas: Handle errors in sas_set_ex_phy()
  2026-08-11 16:22 [PATCH v2 0/3] scsi: libsas: Handle errors in 3 functions Eshaan Deshmukh
  2026-08-11 16:22 ` [PATCH v2 1/3] scsi: libsas: Handle errors in sas_ex_add_parent_port() Eshaan Deshmukh
@ 2026-08-11 16:22 ` Eshaan Deshmukh
  2026-08-12 13:28   ` John Garry
  2026-08-11 16:22 ` [PATCH v2 3/3] scsi: libsas: Handle errors in sas_ex_discover_expander() Eshaan Deshmukh
  2 siblings, 1 reply; 6+ messages in thread
From: Eshaan Deshmukh @ 2026-08-11 16:22 UTC (permalink / raw)
  To: Martin K . Petersen, James E . J . Bottomley
  Cc: John Garry, Jason Yan, linux-scsi, linux-kernel, Eshaan Deshmukh

The function sas_set_ex_phy() uses BUG_ON() if sas_phy_alloc() fails.
Change it to return -ENOMEM error code if that happens. Change all the
other returns in sas_set_ex_phy() to return 0 to preserve current
functionality, or to return an error. Change all uses of sas_set_ex_phy
to handle the returned error codes.

Signed-off-by: Eshaan Deshmukh <eshaan2031@icloud.com>
---
 drivers/scsi/libsas/sas_expander.c | 28 +++++++++++++++++-----------
 1 file changed, 17 insertions(+), 11 deletions(-)

diff --git a/drivers/scsi/libsas/sas_expander.c b/drivers/scsi/libsas/sas_expander.c
index f1a1417b0..1aa99e7c7 100644
--- a/drivers/scsi/libsas/sas_expander.c
+++ b/drivers/scsi/libsas/sas_expander.c
@@ -207,7 +207,7 @@ static enum sas_device_type to_dev_type(struct discover_resp *dr)
 		return dr->attached_dev_type;
 }
 
-static void sas_set_ex_phy(struct domain_device *dev, int phy_id,
+static int sas_set_ex_phy(struct domain_device *dev, int phy_id,
 			   struct smp_disc_resp *disc_resp)
 {
 	enum sas_device_type dev_type;
@@ -220,14 +220,15 @@ static void sas_set_ex_phy(struct domain_device *dev, int phy_id,
 	struct sas_rphy *rphy = dev->rphy;
 	bool new_phy = !phy->phy;
 	char *type;
+	int error;
 
 	if (new_phy) {
 		if (WARN_ON_ONCE(test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state)))
-			return;
+			return 0;
 		phy->phy = sas_phy_alloc(&rphy->dev, phy_id);
 
-		/* FIXME: error_handling */
-		BUG_ON(!phy->phy);
+		if (!phy->phy)
+			return -ENOMEM;
 	}
 
 	switch (disc_resp->result) {
@@ -296,11 +297,14 @@ static void sas_set_ex_phy(struct domain_device *dev, int phy_id,
 	phy->phy->enabled = (phy->linkrate != SAS_PHY_DISABLED);
 
  skip:
-	if (new_phy)
-		if (sas_phy_add(phy->phy)) {
+	if (new_phy) {
+		error = sas_phy_add(phy->phy);
+		if (error) {
 			sas_phy_free(phy->phy);
-			return;
+			phy->phy = NULL;
+			return error;
 		}
+	}
 
  out:
 	switch (phy->attached_dev_type) {
@@ -339,7 +343,7 @@ static void sas_set_ex_phy(struct domain_device *dev, int phy_id,
 	    SAS_ADDR(phy->attached_sas_addr) != SAS_ADDR(sas_addr))
 		/* pass */;
 	else
-		return;
+		return 0;
 
 	/* if the attached device type changed and ata_eh is active,
 	 * make sure we run revalidation when eh completes (see:
@@ -353,6 +357,8 @@ static void sas_set_ex_phy(struct domain_device *dev, int phy_id,
 		 SAS_ADDR(dev->sas_addr), phy->phy_id,
 		 sas_route_char(dev, phy), phy->linkrate,
 		 SAS_ADDR(phy->attached_sas_addr), type);
+
+	return 0;
 }
 
 /* check if we have an existing attached ata device on this expander phy */
@@ -397,8 +403,8 @@ static int sas_ex_phy_discover_helper(struct domain_device *dev, u8 *disc_req,
 		pr_notice("Found loopback topology, just ignore it!\n");
 		return 0;
 	}
-	sas_set_ex_phy(dev, single, disc_resp);
-	return 0;
+
+	return sas_set_ex_phy(dev, single, disc_resp);
 }
 
 int sas_ex_phy_discover(struct domain_device *dev, int single)
@@ -2021,7 +2027,7 @@ static int sas_rediscover_dev(struct domain_device *dev, int phy_id,
 		 * the PHY info, like negotiated linkrate.
 		 */
 		if (res == 0)
-			sas_set_ex_phy(dev, phy_id, disc_resp);
+			res = sas_set_ex_phy(dev, phy_id, disc_resp);
 		goto out_free_resp;
 	} else if (SAS_ADDR(sas_addr) == SAS_ADDR(phy->attached_sas_addr) &&
 		   dev_type_flutter(type, phy->attached_dev_type)) {
-- 
2.55.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2 3/3] scsi: libsas: Handle errors in  sas_ex_discover_expander()
  2026-08-11 16:22 [PATCH v2 0/3] scsi: libsas: Handle errors in 3 functions Eshaan Deshmukh
  2026-08-11 16:22 ` [PATCH v2 1/3] scsi: libsas: Handle errors in sas_ex_add_parent_port() Eshaan Deshmukh
  2026-08-11 16:22 ` [PATCH v2 2/3] scsi: libsas: Handle errors in sas_set_ex_phy() Eshaan Deshmukh
@ 2026-08-11 16:22 ` Eshaan Deshmukh
  2026-08-12 13:33   ` John Garry
  2 siblings, 1 reply; 6+ messages in thread
From: Eshaan Deshmukh @ 2026-08-11 16:22 UTC (permalink / raw)
  To: Martin K . Petersen, James E . J . Bottomley
  Cc: John Garry, Jason Yan, linux-scsi, linux-kernel, Eshaan Deshmukh

The function sas_ex_discover_expander() does not account for the
potential failure of sas_port_alloc() for phy->port. It also calls
BUG_ON in case sas_port_add fails for phy->port. Add a check for
phy->port after sas_port_alloc() where if it is NULL, it cleans up the
child allocated device and returns NULL. Add another check for
sas_port_add() where if it returns an error code it frees phy->port,
sets it to NULL, cleans up the child allocated device, and returns NULL.

Signed-off-by: Eshaan Deshmukh <eshaan2031@icloud.com>
---
 drivers/scsi/libsas/sas_expander.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/drivers/scsi/libsas/sas_expander.c b/drivers/scsi/libsas/sas_expander.c
index 1aa99e7c7..f9ae655a2 100644
--- a/drivers/scsi/libsas/sas_expander.c
+++ b/drivers/scsi/libsas/sas_expander.c
@@ -925,9 +925,18 @@ static struct domain_device *sas_ex_discover_expander(
 		return NULL;
 
 	phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
-	/* FIXME: better error handling */
-	BUG_ON(sas_port_add(phy->port) != 0);
+	if (!phy->port) {
+		sas_put_device(child);
+		return NULL;
+	}
 
+	res = sas_port_add(phy->port);
+	if (res) {
+		sas_port_free(phy->port);
+		phy->port = NULL;
+		sas_put_device(child);
+		return NULL;
+	}
 
 	switch (phy->attached_dev_type) {
 	case SAS_EDGE_EXPANDER_DEVICE:
-- 
2.55.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 2/3] scsi: libsas: Handle errors in sas_set_ex_phy()
  2026-08-11 16:22 ` [PATCH v2 2/3] scsi: libsas: Handle errors in sas_set_ex_phy() Eshaan Deshmukh
@ 2026-08-12 13:28   ` John Garry
  0 siblings, 0 replies; 6+ messages in thread
From: John Garry @ 2026-08-12 13:28 UTC (permalink / raw)
  To: Eshaan Deshmukh, Martin K . Petersen, James E . J . Bottomley
  Cc: Jason Yan, linux-scsi, linux-kernel

On 11/08/2026 17:22, Eshaan Deshmukh wrote:
>    	if (new_phy) {
>    		if (WARN_ON_ONCE(test_bit(SAS_HA_ATA_EH_ACTIVE, &ha->state)))
> -			return;
> +			return 0;

Even though we output the WARN, I am not sure that returning 0 here is 
proper. 0 means success, and we have not been successful.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 3/3] scsi: libsas: Handle errors in sas_ex_discover_expander()
  2026-08-11 16:22 ` [PATCH v2 3/3] scsi: libsas: Handle errors in sas_ex_discover_expander() Eshaan Deshmukh
@ 2026-08-12 13:33   ` John Garry
  0 siblings, 0 replies; 6+ messages in thread
From: John Garry @ 2026-08-12 13:33 UTC (permalink / raw)
  To: Eshaan Deshmukh, Martin K . Petersen, James E . J . Bottomley
  Cc: Jason Yan, linux-scsi, linux-kernel

On 11/08/2026 17:22, Eshaan Deshmukh wrote:
>    	phy->port = sas_port_alloc(&parent->rphy->dev, phy_id);
> -	/* FIXME: better error handling */
> -	BUG_ON(sas_port_add(phy->port) != 0);
> +	if (!phy->port) {
> +		sas_put_device(child);
> +		return NULL;
> +	}
>    
> +	res = sas_port_add(phy->port);
> +	if (res) {

We never pay attention to the specific value of res, so it's not 
required, i.e. we can have:
	
	if (sas_port_add(phy->port)) {

You can also change the sas_discover_expander() call (not shown) for this.

> +		sas_port_free(phy->port);
> +		phy->port = NULL;
> +		sas_put_device(child);
> +		return NULL;
> +	}

There is code later after the sas_discover_expander() call which does 
the same tidy-up and more, so maybe it's better to have tidy-up labels, 
like:

if (!phy->port)
	goto out_put_device;

...

res = sas_port_add(phy->port);
if (res)
	goto out_free_port;


You get the idea...

>    
>    	switch (phy->attached_dev_type) {
>    	case SAS_EDGE_EXPANDER_DEVICE:


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-08-12 13:34 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-08-11 16:22 [PATCH v2 0/3] scsi: libsas: Handle errors in 3 functions Eshaan Deshmukh
2026-08-11 16:22 ` [PATCH v2 1/3] scsi: libsas: Handle errors in sas_ex_add_parent_port() Eshaan Deshmukh
2026-08-11 16:22 ` [PATCH v2 2/3] scsi: libsas: Handle errors in sas_set_ex_phy() Eshaan Deshmukh
2026-08-12 13:28   ` John Garry
2026-08-11 16:22 ` [PATCH v2 3/3] scsi: libsas: Handle errors in sas_ex_discover_expander() Eshaan Deshmukh
2026-08-12 13:33   ` John Garry

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

all inboxes | Powered by JetHome®