[PATCH v2 2/3] scsi: mvumi: quarantine a timed-out internal command's buffers
From: Yehyeong Lee
Date: Sun Nov 15 2026 - 07:22:16 EST
mvumi_issue_blocked_cmd() waits 45 seconds for an internal command and,
if it has not completed, tidies up its own bookkeeping and returns 0, the
same value as success. It writes nothing to the controller, so there is
no abort; the SCSI error handler is not involved either, because an
internal command has scmd == NULL.
The callers - mvumi_flush_cache(), mvumi_inquiry() and mvumi_get_event()
- cannot tell, and each goes on to mvumi_delete_internal_cmd(), which
frees the command frame and the data buffer. The controller still has
both addresses: the frame as the source of a dynamic list entry on an
adapter with HS_CAPABILITY_SUPPORT_DYN_SRC, and the data buffer as the
target of the data-in transfer the frame's SGL describes. One that
answers late writes into memory handed back to the page allocator.
Put both buffers on mhba->res_list instead. The address and length are
taken where mvumi_delete_internal_cmd() takes them today, so nothing new
is trusted; mvumi_release_mem_resource() frees the list at unload, which
the preceding patch makes run after pci_disable_device(). The mark
cannot come from cmd_status, which is assigned from ob_frame->req_status
and can be left at REQ_STATUS_PENDING (0x80) by the controller. The cost
is the frame and data buffer of each timed-out command, held until unload.
Not handing the buffers back means not reusing them either.
mvumi_probe_devices() creates one internal command and lends it to
mvumi_inquiry() for every target id, and mvumi_inquiry() only frees a
command it allocated itself, so after a timeout the next id is asked with
that same frame and data buffer, zeroed and filled in again while the
controller may still be writing to them. Stop the loop there. Its only
caller, mvumi_rescan_bus(), already treats -1 as "not this round" and
comes back on the next event, and once the controller has gone quiet for
45 seconds the remaining ids would cost 45 seconds each. That also
leaves no caller that submits a second command on a struct mvumi_cmd
which has already timed out: mvumi_flush_cache() and mvumi_get_event()
create and delete one per use.
The found == -1 exit, where a wwid turns up on a second id, returned
without the mvumi_delete_internal_cmd() the normal exit does, so it also
leaked the command it was holding. Give the function one exit.
Checked with a KUnit model, a fake controller that answers inline or
stays quiet, and no mvumi hardware. Writing to the buffers after a
timeout, standing in for the controller, gets a KASAN use-after-free in
5 of 5 runs without this patch and none in 5 with it. With one target id
left unanswered, the probe loop submitted 2 further commands on that
command's buffers in 5 of 5 runs and none in 5. On the duplicate-wwid
exit the command was still allocated in 5 of 5 runs and in none of 5.
Fixes: f0c568a478f0 ("[SCSI] mvumi: Add Marvell UMI driver")
Signed-off-by: Yehyeong Lee <yhlee@xxxxxxxxxxxxxxxxxx>
---
drivers/scsi/mvumi.c | 58 ++++++++++++++++++++++++++++++++++++++------
drivers/scsi/mvumi.h | 1 +
2 files changed, 51 insertions(+), 8 deletions(-)
diff --git a/drivers/scsi/mvumi.c b/drivers/scsi/mvumi.c
index 8d22d370d8608f..48652640afc188 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",
@@ -1552,6 +1584,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 +1597,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 +1626,21 @@ 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);
- 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