public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] scsi: libsas: Add rollback handling in the sas_register_phys() and sas_register_ha() when an error occurs.
@ 2025-12-06  6:06 wdhh6
  2025-12-08  9:08 ` John Garry
  2025-12-09  3:09 ` Martin K. Petersen
  0 siblings, 2 replies; 3+ messages in thread
From: wdhh6 @ 2025-12-06  6:06 UTC (permalink / raw)
  To: john.g.garry, yanaijie, James.Bottomley, martin.petersen, wdhh6,
	dlemoal
  Cc: linux-scsi, linux-kernel

From: Chaohai Chen <wdhh6@aliyun.com>

In sas_register_phys(), if an error is triggered in the loop process,
we need to rollback the resources that have already been requested.

Add the sas_unregister_phys() when an error occurs in
sas_register_ha().

Signed-off-by: Chaohai Chen <wdhh6@aliyun.com>
---
v1: https://lore.kernel.org/r/20251205080252.1020028-1-wdhh6@aliyun.com
v2->v1: 
- Call the label using a descriptive name in sas_register_phys() (Damien Le Moal);
- Complete Undo_phys label in sas_register_ha (Jason Yan);
- Fix compile probelm.
---
 drivers/scsi/libsas/sas_init.c     |  1 +
 drivers/scsi/libsas/sas_internal.h |  1 +
 drivers/scsi/libsas/sas_phy.c      | 33 +++++++++++++++++++++++++++---
 3 files changed, 32 insertions(+), 3 deletions(-)

diff --git a/drivers/scsi/libsas/sas_init.c b/drivers/scsi/libsas/sas_init.c
index 8566bb120..6b15ad1bc 100644
--- a/drivers/scsi/libsas/sas_init.c
+++ b/drivers/scsi/libsas/sas_init.c
@@ -141,6 +141,7 @@ int sas_register_ha(struct sas_ha_struct *sas_ha)
 Undo_ports:
 	sas_unregister_ports(sas_ha);
 Undo_phys:
+	sas_unregister_phys(sas_ha);
 
 	return error;
 }
diff --git a/drivers/scsi/libsas/sas_internal.h b/drivers/scsi/libsas/sas_internal.h
index 6706f2be8..2211fbdfc 100644
--- a/drivers/scsi/libsas/sas_internal.h
+++ b/drivers/scsi/libsas/sas_internal.h
@@ -54,6 +54,7 @@ void sas_unregister_dev(struct asd_sas_port *port, struct domain_device *dev);
 void sas_scsi_recover_host(struct Scsi_Host *shost);
 
 int  sas_register_phys(struct sas_ha_struct *sas_ha);
+void sas_unregister_phys(struct sas_ha_struct *sas_ha);
 
 struct asd_sas_event *sas_alloc_event(struct asd_sas_phy *phy, gfp_t gfp_flags);
 void sas_free_event(struct asd_sas_event *event);
diff --git a/drivers/scsi/libsas/sas_phy.c b/drivers/scsi/libsas/sas_phy.c
index 635835c28..4e26b0f23 100644
--- a/drivers/scsi/libsas/sas_phy.c
+++ b/drivers/scsi/libsas/sas_phy.c
@@ -116,6 +116,7 @@ static void sas_phye_shutdown(struct work_struct *work)
 int sas_register_phys(struct sas_ha_struct *sas_ha)
 {
 	int i;
+	int err;
 
 	/* Now register the phys. */
 	for (i = 0; i < sas_ha->num_phys; i++) {
@@ -132,8 +133,10 @@ int sas_register_phys(struct sas_ha_struct *sas_ha)
 		phy->frame_rcvd_size = 0;
 
 		phy->phy = sas_phy_alloc(&sas_ha->shost->shost_gendev, i);
-		if (!phy->phy)
-			return -ENOMEM;
+		if (!phy->phy) {
+			err = -ENOMEM;
+			goto rollback;
+		}
 
 		phy->phy->identify.initiator_port_protocols =
 			phy->iproto;
@@ -146,10 +149,34 @@ int sas_register_phys(struct sas_ha_struct *sas_ha)
 		phy->phy->maximum_linkrate = SAS_LINK_RATE_UNKNOWN;
 		phy->phy->negotiated_linkrate = SAS_LINK_RATE_UNKNOWN;
 
-		sas_phy_add(phy->phy);
+		err = sas_phy_add(phy->phy);
+		if (err) {
+			sas_phy_free(phy->phy);
+			goto rollback;
+		}
 	}
 
 	return 0;
+rollback:
+	for (i--; i >= 0; i--) {
+		struct asd_sas_phy *phy = sas_ha->sas_phy[i];
+
+		sas_phy_delete(phy->phy);
+		sas_phy_free(phy->phy);
+	}
+	return err;
+}
+
+void sas_unregister_phys(struct sas_ha_struct *sas_ha)
+{
+	int i;
+	struct asd_sas_phy *phy;
+
+	for (i = 0; i < sas_ha->num_phys; i++) {
+		phy = sas_ha->sas_phy[i];
+		sas_phy_delete(phy->phy);
+		sas_phy_free(phy->phy);
+	}
 }
 
 const work_func_t sas_phy_event_fns[PHY_NUM_EVENTS] = {
-- 
2.34.1


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

* Re: [PATCH v2] scsi: libsas: Add rollback handling in the sas_register_phys() and sas_register_ha() when an error occurs.
  2025-12-06  6:06 [PATCH v2] scsi: libsas: Add rollback handling in the sas_register_phys() and sas_register_ha() when an error occurs wdhh6
@ 2025-12-08  9:08 ` John Garry
  2025-12-09  3:09 ` Martin K. Petersen
  1 sibling, 0 replies; 3+ messages in thread
From: John Garry @ 2025-12-08  9:08 UTC (permalink / raw)
  To: wdhh6, yanaijie, James.Bottomley, martin.petersen, dlemoal
  Cc: linux-scsi, linux-kernel

On 06/12/2025 06:06, wdhh6 wrote:
> From: Chaohai Chen <wdhh6@aliyun.com>
> 
> In sas_register_phys(), if an error is triggered in the loop process,
> we need to rollback the resources that have already been requested.
> 
> Add the sas_unregister_phys() when an error occurs in
> sas_register_ha().
> 
> Signed-off-by: Chaohai Chen <wdhh6@aliyun.com>

Reviewed-by: John Garry <john.g.garry@oracle.com>

> +}
> +
> +void sas_unregister_phys(struct sas_ha_struct *sas_ha)
> +{
> +	int i;
> +	struct asd_sas_phy *phy;
> +
> +	for (i = 0; i < sas_ha->num_phys; i++) {
> +		phy = sas_ha->sas_phy[i];


nit: I think that it would be nicer to declare @phy within the loop, 
like how it is done at the roolback label in sas_register_phys().

> +		sas_phy_delete(phy->phy);
> +		sas_phy_free(phy->phy);
> +	}
>   }
>   
>   const work_func_t sas_phy_event_fns[PHY_NUM_EVENTS] = {


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

* Re: [PATCH v2] scsi: libsas: Add rollback handling in the sas_register_phys() and sas_register_ha() when an error occurs.
  2025-12-06  6:06 [PATCH v2] scsi: libsas: Add rollback handling in the sas_register_phys() and sas_register_ha() when an error occurs wdhh6
  2025-12-08  9:08 ` John Garry
@ 2025-12-09  3:09 ` Martin K. Petersen
  1 sibling, 0 replies; 3+ messages in thread
From: Martin K. Petersen @ 2025-12-09  3:09 UTC (permalink / raw)
  To: wdhh6
  Cc: john.g.garry, yanaijie, James.Bottomley, martin.petersen, dlemoal,
	linux-scsi, linux-kernel


> In sas_register_phys(), if an error is triggered in the loop process,
> we need to rollback the resources that have already been requested.

Applied to 6.19/scsi-staging, thanks!

-- 
Martin K. Petersen

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

end of thread, other threads:[~2025-12-09  3:10 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-06  6:06 [PATCH v2] scsi: libsas: Add rollback handling in the sas_register_phys() and sas_register_ha() when an error occurs wdhh6
2025-12-08  9:08 ` John Garry
2025-12-09  3:09 ` Martin K. Petersen

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