All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Ranjan Kumar" <ranjan.kumar@broadcom.com>
Cc: linux-scsi@vger.kernel.org
Subject: Re: [PATCH v3 08/10] mpi3mr: Fix SAS port allocation and registration error handling
Date: Fri, 24 Jul 2026 10:58:55 +0000	[thread overview]
Message-ID: <20260724105856.0F5051F01570@smtp.kernel.org> (raw)
In-Reply-To: <20260724102505.115136-9-ranjan.kumar@broadcom.com>

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Pre-existing issues:
- [Critical] Calling `sas_port_delete()` when `sas_rphy_add()` fails causes a fatal `device_del()` on an un-added device.
- [High] The target device structure retains a dangling pointer to a freed `rphy` and a stale `pend_sas_rphy_add` flag if `sas_rphy_add()` fails.
- [High] Failure to clear `mr_sas_phy->phy_belongs_to_port` on error paths causes phys to be permanently orphaned.
--

--- Patch [8]: [PATCH v3 08/10] mpi3mr: Fix SAS port allocation and registration error handling ---
commit 196b5015246a90c5d75f6d4437141992643236da
Author: Ranjan Kumar <ranjan.kumar@broadcom.com>

mpi3mr: Fix SAS port allocation and registration error handling
    
During SAS port creation, the driver does not verify successful port
allocation before attempting registration, which can lead to a NULL
pointer dereference. Additionally, if registration fails, the allocated
port is not freed, resulting in a memory leak.

Closes: https://sashiko.dev/#/patchset/20260626114109.43685-1-ranjan.kumar@broadcom.com?part=8
Closes: https://sashiko.dev/#/patchset/20260708183305.244485-1-ranjan.kumar@broadcom.com?part=8

> diff --git a/drivers/scsi/mpi3mr/mpi3mr_transport.c b/drivers/scsi/mpi3mr/mpi3mr_transport.c
> --- a/drivers/scsi/mpi3mr/mpi3mr_transport.c
> +++ b/drivers/scsi/mpi3mr/mpi3mr_transport.c
[ ... ]
> @@ -1450,10 +1456,22 @@ static struct mpi3mr_sas_port *mpi3mr_sas_port_add(struct mpi3mr_ioc *mrioc,
>  	mr_sas_port->port = port;
>  	if (mr_sas_port->remote_identify.device_type == SAS_END_DEVICE) {
>  		rphy = sas_end_device_alloc(port);
> +		if (!rphy) {
> +			ioc_err(mrioc, "failure at %s:%d/%s() (sas_end_device_alloc)!\n",
> +			    __FILE__, __LINE__, __func__);
> +			sas_port_delete(port);
> +			goto out_fail;
> +		}
>  		tgtdev->dev_spec.sas_sata_inf.rphy = rphy;
>  	} else {
>  		rphy = sas_expander_alloc(port,
>  		    mr_sas_port->remote_identify.device_type);
> +		if (!rphy) {
> +			ioc_err(mrioc, "failure at %s:%d/%s() (sas_expander_alloc)!\n",
> +			    __FILE__, __LINE__, __func__);
> +			sas_port_delete(port);
> +			goto out_fail;
> +		}
>  	}
>  	rphy->identify = mr_sas_port->remote_identify;
>  
> @@ -1463,6 +1481,8 @@ static struct mpi3mr_sas_port *mpi3mr_sas_port_add(struct mpi3mr_ioc *mrioc,
>  	if ((sas_rphy_add(rphy))) {
>  		ioc_err(mrioc, "failure at %s:%d/%s()!\n",
>  		    __FILE__, __LINE__, __func__);
> +		sas_port_delete(port);
> +		goto out_fail;

[Severity: Critical]
This is a pre-existing issue, but does calling sas_port_delete() here cause a
fatal device_del() on an un-added device?

When sas_rphy_add() fails, it will have already internally set port->rphy = rphy
before failing during device_add(). Because port->rphy is set,
sas_port_delete() invokes sas_rphy_delete(), which explicitly calls
sas_rphy_remove() and device_del(dev) on the rphy.

Calling device_del() on a device that was never successfully added via
device_add() violates the kobject API and can trigger a panic. Should we
call sas_rphy_unlink(rphy) and sas_rphy_free(rphy) before
sas_port_delete(port) instead?

[Severity: High]
This isn't a bug introduced by this patch, but could this error path leave
dangling pointers in the tgtdev structure?

Before calling sas_rphy_add(), the rphy pointer is cached in the target
device structure:

mpi3mr_transport.c:mpi3mr_sas_port_add() {
    ...
    if (mr_sas_port->remote_identify.device_type == SAS_END_DEVICE) {
        ...
        tgtdev->dev_spec.sas_sata_inf.rphy = rphy;
    }
    ...
}

If we jump to out_fail when sas_rphy_add() fails, we clean up the rphy
indirectly via sas_port_delete(), but tgtdev->dev_spec.sas_sata_inf.rphy
still points to the freed memory, and pend_sas_rphy_add remains set to 1.

Could a future allocation reuse this memory and cause functions like
__mpi3mr_get_tgtdev_by_addr_and_rphy() to incorrectly match the corrupted
state?

>  	}
>  	if (mr_sas_port->remote_identify.device_type == SAS_END_DEVICE) {
>  		tgtdev->dev_spec.sas_sata_inf.pend_sas_rphy_add = 0;
> @@ -1498,6 +1518,9 @@ static struct mpi3mr_sas_port *mpi3mr_sas_port_add(struct mpi3mr_ioc *mrioc,
>  	return mr_sas_port;
>  
>   out_fail:
> +	if (tgtdev)
> +		mpi3mr_tgtdev_put(tgtdev);
> +
>  	list_for_each_entry_safe(mr_sas_phy, next, &mr_sas_port->phy_list,
>  	    port_siblings)
>  		list_del(&mr_sas_phy->port_siblings);

[Severity: High]
This is a pre-existing issue, but might this error path fail to clear the
phy_belongs_to_port flag, causing phys to be permanently orphaned?

During port addition, mr_sas_phy->phy_belongs_to_port is set to 1 for all
elements in mr_sas_port->phy_list. When we reach out_fail, we remove the
PHYs from the sibling list:

mpi3mr_transport.c:mpi3mr_sas_port_add() {
    ...
 out_fail:
    ...
    list_for_each_entry_safe(mr_sas_phy, next, &mr_sas_port->phy_list,
        port_siblings)
        list_del(&mr_sas_phy->port_siblings);
    ...
}

Because the underlying mr_sas_phy structures persist in mr_sas_node->phy[],
failing to reset the flag means they will permanently claim to belong to a
port. Will future attempts to add these PHYs fail as a result?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260724102505.115136-1-ranjan.kumar@broadcom.com?part=8

  reply	other threads:[~2026-07-24 10:58 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-24 10:24 [PATCH v3 00/10] mpi3mr: Few Enhancements and minor fixes Ranjan Kumar
2026-07-24 10:24 ` [PATCH v3 01/10] mpi3mr: Skip device shutdown during unload per controller configuration Ranjan Kumar
2026-07-24 10:59   ` sashiko-bot
2026-07-24 10:24 ` [PATCH v3 02/10] mpi3mr: Update MPI Headers to revision 41 Ranjan Kumar
2026-07-24 10:24 ` [PATCH v3 03/10] mpi3mr: Add early timestamp synchronization after driver load Ranjan Kumar
2026-07-24 10:24 ` [PATCH v3 04/10] mpi3mr: Fix NVMe page size caching for non-operational devices Ranjan Kumar
2026-07-24 10:44   ` sashiko-bot
2026-07-24 10:25 ` [PATCH v3 05/10] mpi3mr: Fix performance regression caused by extended IRQ poll sleep Ranjan Kumar
2026-07-24 10:50   ` sashiko-bot
2026-07-24 10:25 ` [PATCH v3 06/10] mpi3mr: Fix memory leak on operational queue creation failure Ranjan Kumar
2026-07-24 10:51   ` sashiko-bot
2026-07-24 10:25 ` [PATCH v3 07/10] mpi3mr: Fix firmware event reference leak during cleanup Ranjan Kumar
2026-07-24 11:02   ` sashiko-bot
2026-07-24 10:25 ` [PATCH v3 08/10] mpi3mr: Fix SAS port allocation and registration error handling Ranjan Kumar
2026-07-24 10:58   ` sashiko-bot [this message]
2026-07-24 10:25 ` [PATCH v3 09/10] mpi3mr: Fix SAS PHY cleanup in host addition error paths Ranjan Kumar
2026-07-24 10:25 ` [PATCH v3 10/10] mpi3mr: Driver version update to 8.18.0.8.50 Ranjan Kumar

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=20260724105856.0F5051F01570@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=ranjan.kumar@broadcom.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.