Skip to content

Commit 746d0ac

Browse files
rwk-gitChristoph Hellwig
authored andcommitted
nvmet: pci-epf: Do not complete commands twice if nvmet_req_init() fails
Have nvmet_req_init() and req->execute() complete failed commands. Description of the problem: nvmet_req_init() calls __nvmet_req_complete() internally upon failure, e.g., unsupported opcode, which calls the "queue_response" callback, this results in nvmet_pci_epf_queue_response() being called, which will call nvmet_pci_epf_complete_iod() if data_len is 0 or if dma_dir is different from DMA_TO_DEVICE. This results in a double completion as nvmet_pci_epf_exec_iod_work() also calls nvmet_pci_epf_complete_iod() when nvmet_req_init() fails. Steps to reproduce: On the host send a command with an unsupported opcode with nvme-cli, For example the admin command "security receive" $ sudo nvme security-recv /dev/nvme0n1 -n1 -x4096 This triggers a double completion as nvmet_req_init() fails and nvmet_pci_epf_queue_response() is called, here iod->dma_dir is still in the default state of "DMA_NONE" as set by default in nvmet_pci_epf_alloc_iod(), so nvmet_pci_epf_complete_iod() is called. Because nvmet_req_init() failed nvmet_pci_epf_complete_iod() is also called in nvmet_pci_epf_exec_iod_work() leading to a double completion. This not only sends two completions to the host but also corrupts the state of the PCI NVMe target leading to kernel oops. This patch lets nvmet_req_init() and req->execute() complete all failed commands, and removes the double completion case in nvmet_pci_epf_exec_iod_work() therefore fixing the edge cases where double completions occurred. Fixes: 0faa0fe ("nvmet: New NVMe PCI endpoint function target driver") Signed-off-by: Rick Wertenbroek <[email protected]> Reviewed-by: Damien Le Moal <[email protected]> Reviewed-by: Chaitanya Kulkarni <[email protected]> Signed-off-by: Christoph Hellwig <[email protected]>
1 parent 5a58ac9 commit 746d0ac

1 file changed

Lines changed: 16 additions & 7 deletions

File tree

drivers/nvme/target/pci-epf.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,8 +1242,11 @@ static void nvmet_pci_epf_queue_response(struct nvmet_req *req)
12421242

12431243
iod->status = le16_to_cpu(req->cqe->status) >> 1;
12441244

1245-
/* If we have no data to transfer, directly complete the command. */
1246-
if (!iod->data_len || iod->dma_dir != DMA_TO_DEVICE) {
1245+
/*
1246+
* If the command failed or we have no data to transfer, complete the
1247+
* command immediately.
1248+
*/
1249+
if (iod->status || !iod->data_len || iod->dma_dir != DMA_TO_DEVICE) {
12471250
nvmet_pci_epf_complete_iod(iod);
12481251
return;
12491252
}
@@ -1604,8 +1607,13 @@ static void nvmet_pci_epf_exec_iod_work(struct work_struct *work)
16041607
goto complete;
16051608
}
16061609

1610+
/*
1611+
* If nvmet_req_init() fails (e.g., unsupported opcode) it will call
1612+
* __nvmet_req_complete() internally which will call
1613+
* nvmet_pci_epf_queue_response() and will complete the command directly.
1614+
*/
16071615
if (!nvmet_req_init(req, &iod->sq->nvme_sq, &nvmet_pci_epf_fabrics_ops))
1608-
goto complete;
1616+
return;
16091617

16101618
iod->data_len = nvmet_req_transfer_len(req);
16111619
if (iod->data_len) {
@@ -1643,10 +1651,11 @@ static void nvmet_pci_epf_exec_iod_work(struct work_struct *work)
16431651

16441652
wait_for_completion(&iod->done);
16451653

1646-
if (iod->status == NVME_SC_SUCCESS) {
1647-
WARN_ON_ONCE(!iod->data_len || iod->dma_dir != DMA_TO_DEVICE);
1648-
nvmet_pci_epf_transfer_iod_data(iod);
1649-
}
1654+
if (iod->status != NVME_SC_SUCCESS)
1655+
return;
1656+
1657+
WARN_ON_ONCE(!iod->data_len || iod->dma_dir != DMA_TO_DEVICE);
1658+
nvmet_pci_epf_transfer_iod_data(iod);
16501659

16511660
complete:
16521661
nvmet_pci_epf_complete_iod(iod);

0 commit comments

Comments
 (0)