public inbox for linux-scsi@vger.kernel.org
 help / color / mirror / Atom feed
From: Sasha Levin <sashal@kernel.org>
To: patches@lists.linux.dev, stable@vger.kernel.org
Cc: Francisco Gutierrez <frankramirez@google.com>,
	Jack Wang <jinpu.wang@ionos.com>,
	"Martin K . Petersen" <martin.petersen@oracle.com>,
	Sasha Levin <sashal@kernel.org>,
	jinpu.wang@cloud.ionos.com, linux-scsi@vger.kernel.org
Subject: [PATCH AUTOSEL 6.16-6.15] scsi: pm80xx: Free allocated tags after failure
Date: Tue,  5 Aug 2025 09:09:03 -0400	[thread overview]
Message-ID: <20250805130945.471732-28-sashal@kernel.org> (raw)
In-Reply-To: <20250805130945.471732-1-sashal@kernel.org>

From: Francisco Gutierrez <frankramirez@google.com>

[ Upstream commit 258a0a19621793b811356fc9d1849f950629d669 ]

This change frees resources after an error is detected.

Signed-off-by: Francisco Gutierrez <frankramirez@google.com>
Link: https://lore.kernel.org/r/20250617210443.989058-1-frankramirez@google.com
Acked-by: Jack Wang <jinpu.wang@ionos.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---

LLM Generated explanations, may be completely bogus:

**Backport Status: YES**

This commit should be backported to stable kernel trees for the
following reasons:

## Bug Fix Analysis

1. **Clear Resource Leak Bug**: The commit fixes a resource leak where
   tags allocated via `pm8001_tag_alloc()` are not freed when
   `pm8001_mpi_build_cmd()` fails. Looking at the code:
   - In `pm80xx_chip_phy_start_req()` at line 4652-4656, a tag is
     allocated
   - If `pm8001_mpi_build_cmd()` fails (returns error), the function
     returns directly without freeing the allocated tag
   - The same pattern exists in `pm80xx_chip_phy_stop_req()`

2. **Actual Runtime Impact**: The `pm8001_mpi_build_cmd()` function can
   fail with `-ENOMEM` when no free MPI buffers are available (as shown
   in the function implementation). This is a realistic failure scenario
   under memory pressure or high I/O load, making this a real-world bug.

3. **Resource Exhaustion Risk**: The driver uses a limited tag pool
   (PM8001_RESERVE_SLOT tags). Each leaked tag reduces the available
   pool, potentially leading to:
   - Tag exhaustion over time
   - Inability to issue new PHY start/stop commands
   - Degraded SCSI controller functionality

4. **Consistent Pattern Fix**: The codebase already has established
   patterns for properly freeing tags on error paths, as evidenced by:
   - Multiple existing instances where `pm8001_tag_free()` is called
     after `pm8001_mpi_build_cmd()` failures
   - Previous similar fix in commit c13e73317458 for tag leaks in
     `OPC_INB_SET_CONTROLLER_CONFIG` command

5. **Small and Contained Fix**: The changes are minimal (4 lines added
   in total), localized to two functions, and follow existing error
   handling patterns in the driver. This minimizes regression risk.

6. **No New Features or Architecture Changes**: The commit purely fixes
   a resource leak without introducing new functionality or changing
   driver behavior.

7. **Maintainer Acknowledgment**: The fix is acknowledged by the
   subsystem maintainer (Jack Wang), indicating it's a legitimate issue
   that needed addressing.

The fix aligns perfectly with stable kernel criteria - it's a clear bug
fix for a resource leak that can impact system stability over time, with
minimal code changes and low regression risk.

 drivers/scsi/pm8001/pm80xx_hwi.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/drivers/scsi/pm8001/pm80xx_hwi.c b/drivers/scsi/pm8001/pm80xx_hwi.c
index 5b373c53c036..c4074f062d93 100644
--- a/drivers/scsi/pm8001/pm80xx_hwi.c
+++ b/drivers/scsi/pm8001/pm80xx_hwi.c
@@ -4677,8 +4677,12 @@ pm80xx_chip_phy_start_req(struct pm8001_hba_info *pm8001_ha, u8 phy_id)
 		&pm8001_ha->phy[phy_id].dev_sas_addr, SAS_ADDR_SIZE);
 	payload.sas_identify.phy_id = phy_id;
 
-	return pm8001_mpi_build_cmd(pm8001_ha, 0, opcode, &payload,
+	ret = pm8001_mpi_build_cmd(pm8001_ha, 0, opcode, &payload,
 				    sizeof(payload), 0);
+	if (ret < 0)
+		pm8001_tag_free(pm8001_ha, tag);
+
+	return ret;
 }
 
 /**
@@ -4704,8 +4708,12 @@ static int pm80xx_chip_phy_stop_req(struct pm8001_hba_info *pm8001_ha,
 	payload.tag = cpu_to_le32(tag);
 	payload.phy_id = cpu_to_le32(phy_id);
 
-	return pm8001_mpi_build_cmd(pm8001_ha, 0, opcode, &payload,
+	ret = pm8001_mpi_build_cmd(pm8001_ha, 0, opcode, &payload,
 				    sizeof(payload), 0);
+	if (ret < 0)
+		pm8001_tag_free(pm8001_ha, tag);
+
+	return ret;
 }
 
 /*
-- 
2.39.5


  parent reply	other threads:[~2025-08-05 13:10 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20250805130945.471732-1-sashal@kernel.org>
2025-08-05 13:08 ` [PATCH AUTOSEL 6.16-5.4] scsi: bfa: Double-free fix Sasha Levin
2025-08-05 13:08 ` [PATCH AUTOSEL 6.16-6.12] scsi: lpfc: Ensure HBA_SETUP flag is used only for SLI4 in dev_loss_tmo_callbk Sasha Levin
2025-08-05 13:09 ` [PATCH AUTOSEL 6.16-5.4] scsi: lpfc: Check for hdwq null ptr when cleaning up lpfc_vport structure Sasha Levin
2025-08-05 13:09 ` Sasha Levin [this message]
2025-08-05 13:09 ` [PATCH AUTOSEL 6.16-6.1] scsi: mpi3mr: Correctly handle ATA device errors Sasha Levin
2025-08-05 13:09 ` [PATCH AUTOSEL 6.16-5.4] scsi: mpt3sas: " Sasha Levin
2025-08-05 13:09 ` [PATCH AUTOSEL 6.16-5.4] scsi: libiscsi: Initialize iscsi_conn->dd_data only if memory is allocated Sasha Levin

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=20250805130945.471732-28-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=frankramirez@google.com \
    --cc=jinpu.wang@cloud.ionos.com \
    --cc=jinpu.wang@ionos.com \
    --cc=linux-scsi@vger.kernel.org \
    --cc=martin.petersen@oracle.com \
    --cc=patches@lists.linux.dev \
    --cc=stable@vger.kernel.org \
    /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