[PATCH v3 2/3] scsi: mvumi: quarantine a timed-out internal command's buffers
From: Yehyeong Lee
Date: Tue Nov 17 2026 - 08:43:04 EST
mvumi_issue_blocked_cmd() gives up on an internal command after 45
seconds and returns 0, as on success, without aborting it on the
controller. The callers then free the command frame and data buffer in
mvumi_delete_internal_cmd(), although the controller still holds both
addresses and can write into them when it answers late.
Put both buffers on mhba->res_list instead, so that they are freed at
unload, after the preceding patch has stopped bus-mastering. The cost
is one or two buffers per timed-out command until unload.
mvumi_probe_devices() uses one command for every target id, so after a
timeout the next inquiry would hand the same buffers back to the
controller. Stop the loop there and return -1; mvumi_rescan_bus()
retries on the next event. Both -1 exits now share one exit, which also
frees the command the duplicate-wwid exit leaked (posted on its own by
Zilin Guan [1]) and the mhba_dev_list entries that mvumi_rescan_bus()
drains only on success.
Checked with a KUnit model, without mvumi hardware: a late write to the
buffers gives a KASAN use-after-free in 5 of 5 runs without this patch
and in none of 5 with it. The reuse after a timeout, the leaked command
and the stranded list entries go from 5 of 5 to none of 5 as well.
[1] https://lore.kernel.org/all/20251113150934.758067-1-zilin@xxxxxxxxxx/
Fixes: f0c568a478f0 ("[SCSI] mvumi: Add Marvell UMI driver")
Signed-off-by: Yehyeong Lee <yhlee@xxxxxxxxxxxxxxxxxx>
---
drivers/scsi/mvumi.c | 80 ++++++++++++++++++++++++++++++++++++--------
drivers/scsi/mvumi.h | 1 +
2 files changed, 67 insertions(+), 14 deletions(-)
diff --git a/drivers/scsi/mvumi.c b/drivers/scsi/mvumi.c
index 8d22d370d8608f..fe3f63a1088dc1 100644
--- a/drivers/scsi/mvumi.c
+++ b/drivers/scsi/mvumi.c
@@ -177,6 +177,29 @@ static void mvumi_release_mem_resource(struct mvumi_hba *mhba)
mhba->fw_flag &= ~MVUMI_FW_ALLOC;
}
+/* The controller may still DMA here; free it once the device is disabled. */
+static void mvumi_quarantine_buf(struct mvumi_hba *mhba, void *virt_addr,
+ dma_addr_t bus_addr, unsigned int size)
+{
+ struct mvumi_res *res = kzalloc_obj(*res);
+ unsigned long flags;
+
+ if (!res) {
+ dev_err(&mhba->pdev->dev,
+ "leaking %u bytes the controller may still write to\n",
+ size);
+ return;
+ }
+ res->type = RESOURCE_UNCACHED_MEMORY;
+ res->virt_addr = virt_addr;
+ res->bus_addr = bus_addr;
+ res->size = size;
+ INIT_LIST_HEAD(&res->entry);
+ spin_lock_irqsave(&mhba->shost->host_lock, flags);
+ list_add_tail(&res->entry, &mhba->res_list);
+ spin_unlock_irqrestore(&mhba->shost->host_lock, flags);
+}
+
/**
* mvumi_make_sgl - Prepares SGL
* @mhba: Adapter soft state
@@ -298,11 +321,19 @@ static void mvumi_delete_internal_cmd(struct mvumi_hba *mhba,
phy_addr = (dma_addr_t) m_sg->baseaddr_l |
(dma_addr_t) ((m_sg->baseaddr_h << 16) << 16);
- dma_free_coherent(&mhba->pdev->dev, size, cmd->data_buf,
- phy_addr);
+ if (cmd->timed_out)
+ mvumi_quarantine_buf(mhba, cmd->data_buf,
+ phy_addr, size);
+ else
+ dma_free_coherent(&mhba->pdev->dev, size,
+ cmd->data_buf, phy_addr);
}
- dma_free_coherent(&mhba->pdev->dev, mhba->ib_max_size,
- cmd->frame, cmd->frame_phys);
+ if (cmd->timed_out)
+ mvumi_quarantine_buf(mhba, cmd->frame, cmd->frame_phys,
+ mhba->ib_max_size);
+ else
+ dma_free_coherent(&mhba->pdev->dev, mhba->ib_max_size,
+ cmd->frame, cmd->frame_phys);
kfree(cmd);
}
}
@@ -734,6 +765,7 @@ static int mvumi_issue_blocked_cmd(struct mvumi_hba *mhba,
if (atomic_read(&cmd->sync_cmd)) {
spin_lock_irqsave(&mhba->shost->host_lock, flags);
atomic_dec(&cmd->sync_cmd);
+ cmd->timed_out = 1;
if (mhba->tag_cmd[cmd->frame->tag]) {
mhba->tag_cmd[cmd->frame->tag] = NULL;
dev_warn(&mhba->pdev->dev, "TIMEOUT:release tag [%d]\n",
@@ -1458,6 +1490,18 @@ static u64 mvumi_inquiry(struct mvumi_hba *mhba,
return wwid;
}
+static void mvumi_free_dev_list(struct mvumi_hba *mhba, struct list_head *head)
+{
+ struct mvumi_device *mv_dev, *dev_next;
+
+ list_for_each_entry_safe(mv_dev, dev_next, head, list) {
+ list_del_init(&mv_dev->list);
+ dev_dbg(&mhba->pdev->dev, "release device(0:%d:0) wwid(%llx)\n",
+ mv_dev->id, mv_dev->wwid);
+ kfree(mv_dev);
+ }
+}
+
static void mvumi_detach_devices(struct mvumi_hba *mhba)
{
struct mvumi_device *mv_dev = NULL , *dev_next;
@@ -1474,12 +1518,7 @@ static void mvumi_detach_devices(struct mvumi_hba *mhba)
mv_dev->id, mv_dev->wwid);
kfree(mv_dev);
}
- list_for_each_entry_safe(mv_dev, dev_next, &mhba->mhba_dev_list, list) {
- list_del_init(&mv_dev->list);
- dev_dbg(&mhba->pdev->dev, "release device(0:%d:0) wwid(%llx)\n",
- mv_dev->id, mv_dev->wwid);
- kfree(mv_dev);
- }
+ mvumi_free_dev_list(mhba, &mhba->mhba_dev_list);
/* detach virtual device */
if (mhba->pdev->device == PCI_DEVICE_ID_MARVELL_MV9580)
@@ -1552,6 +1591,7 @@ static int mvumi_probe_devices(struct mvumi_hba *mhba)
struct mvumi_device *mv_dev = NULL;
struct mvumi_cmd *cmd = NULL;
int found = 0;
+ int ret;
cmd = mvumi_create_internal_cmd(mhba, 64);
if (!cmd)
@@ -1564,6 +1604,11 @@ static int mvumi_probe_devices(struct mvumi_hba *mhba)
for (id = 0; id < maxid; id++) {
wwid = mvumi_inquiry(mhba, id, cmd);
+ /* the controller still owns this command's buffers */
+ if (cmd->timed_out) {
+ ret = -1;
+ goto out;
+ }
if (!wwid) {
/* device no response, remove it */
mvumi_remove_devices(mhba, id);
@@ -1588,17 +1633,24 @@ static int mvumi_probe_devices(struct mvumi_hba *mhba)
dev_dbg(&mhba->pdev->dev,
"probe a new device(0:%d:0)"
" wwid(%llx)\n", id, mv_dev->wwid);
- } else if (found == -1)
- return -1;
- else
+ } else if (found == -1) {
+ ret = -1;
+ goto out;
+ } else {
continue;
+ }
}
}
+ ret = 0;
+out:
if (cmd)
mvumi_delete_internal_cmd(mhba, cmd);
+ /* mvumi_rescan_bus() only drains this list when we return 0 */
+ if (ret)
+ mvumi_free_dev_list(mhba, &mhba->mhba_dev_list);
- return 0;
+ return ret;
}
static int mvumi_rescan_bus(void *data)
diff --git a/drivers/scsi/mvumi.h b/drivers/scsi/mvumi.h
index 1306a4abf19ad2..e248dfc1dd1bb7 100644
--- a/drivers/scsi/mvumi.h
+++ b/drivers/scsi/mvumi.h
@@ -252,6 +252,7 @@ struct mvumi_cmd {
void *data_buf;
unsigned short request_id;
unsigned char cmd_status;
+ unsigned char timed_out;
};
struct mvumi_cmd_priv {
--
2.43.0