From: John Garry <john.g.garry@oracle.com>
To: Eshaan Deshmukh <eshaan2031@icloud.com>,
"Martin K . Petersen" <martin.petersen@oracle.com>,
"James E . J . Bottomley" <James.Bottomley@HansenPartnership.com>
Cc: Jason Yan <yanaijie@huawei.com>,
linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3 3/3] scsi: libsas: Handle errors in sas_ex_discover_expander()
Date: Fri, 14 Aug 2026 17:45:42 +0100 [thread overview]
Message-ID: <ea58d4fd-11a7-4707-8115-0ab0d305dc52@oracle.com> (raw)
In-Reply-To: <20260812194808.576586-4-eshaan2031@icloud.com>
On 12/08/2026 20:48, Eshaan Deshmukh wrote:
> 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
>
>
> 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 | 41 ++++++++++++++++++++----------
> 1 file changed, 27 insertions(+), 14 deletions(-)
>
> diff --git a/drivers/scsi/libsas/sas_expander.c b/drivers/scsi/libsas/sas_expander.c
> index ab6afbad3..a83493f57 100644
> --- a/drivers/scsi/libsas/sas_expander.c
> +++ b/drivers/scsi/libsas/sas_expander.c
> @@ -911,7 +911,6 @@ static struct domain_device *sas_ex_discover_expander(
> struct sas_rphy *rphy;
> struct sas_expander_device *edev;
> struct asd_sas_port *port;
> - int res;
>
> if (phy->routing_attr == DIRECT_ROUTING) {
> pr_warn("ex %016llx:%02d:D <--> ex %016llx:0x%x is not allowed\n",
> @@ -925,9 +924,13 @@ 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) {
no need for {}, however please see comment further down.
> + goto out_put_device;
> + }
>
> + if (sas_port_add(phy->port)) {
> + goto out_free_port;
ditto
> + }
>
> switch (phy->attached_dev_type) {
> case SAS_EDGE_EXPANDER_DEVICE:
> @@ -966,19 +969,29 @@ static struct domain_device *sas_ex_discover_expander(
> list_add_tail(&child->dev_list_node, &parent->port->dev_list);
> spin_unlock_irq(&parent->port->dev_list_lock);
>
> - res = sas_discover_expander(child);
> - if (res) {
> - sas_rphy_delete(rphy);
> - spin_lock_irq(&parent->port->dev_list_lock);
> - list_del(&child->dev_list_node);
> - spin_unlock_irq(&parent->port->dev_list_lock);
> - sas_put_device(child);
> - sas_port_delete(phy->port);
I missed that this was a sas_port_delete() call and not sas_port_free(),
so not common to other error paths.
So it does not look like we can make much error paths common, so maybe
it was better as previously.
> - phy->port = NULL;
> - return NULL;
> - }
> + if (sas_discover_expander(child))
> + goto out_delete_rphy;
> +
> list_add_tail(&child->siblings, &parent->ex_dev.children);
> return child;
> +
> +out_free_port:
> + sas_port_free(phy->port);
> + phy->port = NULL;
> +
> +out_put_device:
> + sas_put_device(child);
> + return NULL;
> +
> +out_delete_rphy:
> + sas_rphy_delete(rphy);
> + spin_lock_irq(&parent->port->dev_list_lock);
> + list_del(&child->dev_list_node);
> + spin_unlock_irq(&parent->port->dev_list_lock);
> + sas_put_device(child);
> + sas_port_delete(phy->port);
> + phy->port = NULL;
> + return NULL;
> }
>
> static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)
> --
> 2.55.0
>
prev parent reply other threads:[~2026-08-14 16:46 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-12 19:48 [PATCH v3 0/3] scsi: libsas: Handle errors in 3 functions Eshaan Deshmukh
2026-08-12 19:48 ` [PATCH v3 1/3] scsi: libsas: Handle errors in sas_ex_add_parent_port() Eshaan Deshmukh
2026-08-12 19:48 ` [PATCH v3 2/3] scsi: libsas: Handle errors in sas_set_ex_phy() Eshaan Deshmukh
2026-08-12 19:48 ` [PATCH v3 3/3] scsi: libsas: Handle errors in sas_ex_discover_expander() Eshaan Deshmukh
2026-08-14 16:45 ` John Garry [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ea58d4fd-11a7-4707-8115-0ab0d305dc52@oracle.com \
--to=john.g.garry@oracle.com \
--cc=James.Bottomley@HansenPartnership.com \
--cc=eshaan2031@icloud.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-scsi@vger.kernel.org \
--cc=martin.petersen@oracle.com \
--cc=yanaijie@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®