mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Damien Le Moal <dlemoal@kernel.org>
To: Haoxiang Li <haoxiang_li2024@163.com>,
	john.g.garry@oracle.com, yanaijie@huawei.com,
	James.Bottomley@HansenPartnership.com,
	martin.petersen@oracle.com, cassel@kernel.org, kees@kernel.org
Cc: linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] scsi: libsas: Handle expander discovery allocation failures
Date: Sat, 27 Jun 2026 07:11:06 +0900	[thread overview]
Message-ID: <218133be-eca7-44ac-a9f6-81283c55185d@kernel.org> (raw)
In-Reply-To: <20260623112909.2172701-1-haoxiang_li2024@163.com>

On 6/23/26 20:29, Haoxiang Li wrote:
> sas_ex_discover_expander() allocates a domain device and SAS port before
> allocating the expander rphy, but it does not check all allocation and
> registration failures. In particular, sas_expander_alloc() can return
> NULL and the returned rphy is dereferenced unconditionally.
> 
> Add error handling for sas_port_alloc(), sas_port_add(), and
> sas_expander_alloc(), and unwind the resources allocated on each path.
> Use sas_port_free() before a port has been added and sas_port_delete()
> after it has been added.
> 
> Free the child device directly on these early failures because child->rphy
> has not been initialized yet, and sas_put_device() would dereference it.
> 
> Signed-off-by: Haoxiang Li <haoxiang_li2024@163.com>

Looks OK. A couple of nits below.

> ---
>  drivers/scsi/libsas/sas_expander.c | 24 +++++++++++++++++++++---
>  1 file changed, 21 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/scsi/libsas/sas_expander.c b/drivers/scsi/libsas/sas_expander.c
> index f471ab464a78..56c04c4ae818 100644
> --- a/drivers/scsi/libsas/sas_expander.c
> +++ b/drivers/scsi/libsas/sas_expander.c
> @@ -909,9 +909,11 @@ 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)
> +		goto out_free_child;

For readability, a blank line would be nice here.

> +	res = sas_port_add(phy->port);
> +	if (res)
> +		goto out_free_port;
>  
>  	switch (phy->attached_dev_type) {
>  	case SAS_EDGE_EXPANDER_DEVICE:
> @@ -926,6 +928,9 @@ static struct domain_device *sas_ex_discover_expander(
>  		rphy = NULL;	/* shut gcc up */
>  		BUG();

can we drop this BUG() too so that instead of crashing we properly error unwind?

>  	}
> +	if (!rphy)
> +		goto out_delete_port;
> +
>  	port = parent->port;
>  	child->rphy = rphy;
>  	get_device(&rphy->dev);
> @@ -963,6 +968,19 @@ static struct domain_device *sas_ex_discover_expander(
>  	}
>  	list_add_tail(&child->siblings, &parent->ex_dev.children);
>  	return child;
> +
> +out_delete_port:
> +	sas_port_delete(phy->port);
> +	phy->port = NULL;
> +	kfree(child);
> +	return NULL;
> +
> +out_free_port:
> +	sas_port_free(phy->port);
> +	phy->port = NULL;
> +out_free_child:
> +	kfree(child);
> +	return NULL;
>  }
>  
>  static int sas_ex_discover_dev(struct domain_device *dev, int phy_id)


-- 
Damien Le Moal
Western Digital Research

      reply	other threads:[~2026-06-26 22:11 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-23 11:29 Haoxiang Li
2026-06-26 22:11 ` Damien Le Moal [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=218133be-eca7-44ac-a9f6-81283c55185d@kernel.org \
    --to=dlemoal@kernel.org \
    --cc=James.Bottomley@HansenPartnership.com \
    --cc=cassel@kernel.org \
    --cc=haoxiang_li2024@163.com \
    --cc=john.g.garry@oracle.com \
    --cc=kees@kernel.org \
    --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