Re: [PATCH] nvme: Move nvme_setup_cmd before hot_pathing

Kanchan Joshi
2026-03-20T13:44:15Z

On 3/20/2026 10:51 AM, 전민식 wrote:
> diff --git a/drivers/nvme/host/pci.c b/drivers/nvme/host/pci.c
> index b78ba239c8ea..ad0363f7e681 100644
> --- a/drivers/nvme/host/pci.c
> +++ b/drivers/nvme/host/pci.c
> @@ -1376,10 +1376,6 @@ static blk_status_t nvme_prep_rq(struct request *req)
> iod->meta_total_len = 0;
> iod->nr_dma_vecs = 0;
>
> - ret = nvme_setup_cmd(req->q->queuedata, req);
> - if (ret)
> - return ret;
> -
> if (blk_rq_nr_phys_segments(req)) {
> ret = nvme_map_data(req);
> if (ret)
> @@ -1418,6 +1414,10 @@ static blk_status_t nvme_queue_rq(struct blk_mq_hw_ctx *hctx,
> if (unlikely(!test_bit(NVMEQ_ENABLED, &nvmeq->flags)))
> return BLK_STS_IOERR;
>
> + ret = nvme_setup_cmd(req->q->queuedata, req);
> + if (ret)
> + return ret;
> +
> if (unlikely(!nvme_check_ready(&dev->ctrl, req, true)))
> return nvme_fail_nonready_command(&dev->ctrl, req);

The intent is to improve the observability in case controller was not
ready, but the patch may need rework so that it does not cause the
memory leak for this scenario.

Currently nvme_prep_rq() calls nvme_setup_cmd(), and in case failure
occurs after that, it also does explicit cleanup with
nvme_cleanup_cmd(). For discard request, this cleanup involves freeing
memory that was allocated.

This patch moves nvme_setup_cmd() to caller (i.e. nvme_queue_rq), checks
for controller not being ready and asks block-layer to retry without
ever freeing the resource.