[PATCH 3/4] nvmet: snapshot the passthru command before completing the request
Guixin Liu
kanie at linux.alibaba.com
Tue Dec 1 21:45:14 PST 2026
在 2026/12/2 11:59, sashiko-bot at kernel.org 写道:
> Thank you for your contribution! Sashiko AI review found 1 potential
> issue to consider:
> - [Medium] nvmet: premature nvmet_req_complete() in
> nvmet_passthru_execute_cmd_work()
> --
>
> commit 188f07fd6a69f723caceb270fb65872dac163f43
> Author: Guixin Liu <kanie at linux.alibaba.com>
>
> nvmet: snapshot the passthru command before completing the request
>
> This commit copies req->cmd onto the stack before nvmet_req_complete() in
> nvmet_passthru_execute_cmd_work() so nvme_passthru_end() does not read
> req->cmd after the target request may have been freed.
>
>> diff --git a/drivers/nvme/target/passthru.c b/drivers/nvme/target/passthru.c
>> index 32ae4b6b59bf0..791540bc8fce0 100644
>> --- a/drivers/nvme/target/passthru.c
>> +++ b/drivers/nvme/target/passthru.c
>> @@ -226,6 +226,7 @@ static void nvmet_passthru_execute_cmd_work(struct work_struct *w)
>> struct request *rq = req->p.rq;
>> struct nvme_ctrl *ctrl = nvme_req(rq)->ctrl;
>> struct nvme_ns *ns = rq->q->queuedata;
>> + struct nvme_command cmd;
>> u32 effects;
>> int status;
>>
>> @@ -247,12 +248,19 @@ static void nvmet_passthru_execute_cmd_work(struct work_struct *w)
>> } else if (status < 0)
>> status = NVME_SC_INTERNAL;
>>
>> + /*
>> + * nvmet_req_complete() may free req synchronously (e.g. the loop
>> + * transport), so snapshot the command for nvme_passthru_end(),
>> + * which must run after completion to avoid deadlocking against a
>> + * concurrent reset.
>> + */
>> + cmd = *req->cmd;
>> req->cqe->result = nvme_req(rq)->result;
>> nvmet_req_complete(req, status);
>> blk_mq_free_request(rq);
>>
>> if (effects)
>> - nvme_passthru_end(ctrl, ns, effects, req->cmd, status);
>> + nvme_passthru_end(ctrl, ns, effects, &cmd, status);
>> }
> [Severity: Medium]
> Should nvmet_req_complete(req, status) be moved after
> nvme_passthru_end() instead of copying req->cmd?
>
> Commit bc8fb906b0ff ("nvme: handle effects after freeing the request")
> required nvme_passthru_end() to run after blk_mq_free_request(rq) so the
> host request rq transitions to MQ_RQ_IDLE before flush_work(&ctrl->scan_work)
> waits on a quiesced admin queue during a reset of ctrl. It did not require
> nvmet_req_complete() to run before nvme_passthru_end().
>
> Calling nvmet_req_complete() early drops passthru->ref via
> __nvmet_req_complete() -> nvmet_put_passthru_ref() while ctrl is still in
> use, and signals completion to the initiator while ctrl is still frozen or
> before ctrl->scan_work has finished updating namespaces:
>
> CPU0 (nvmet_wq) CPU1
> nvmet_passthru_execute_cmd_work()
> nvmet_req_complete(req, status)
> ...
> nvmet_put_passthru_ref()
> __nvmet_passthru_ctrl_disable()
> wait_for_completion(&passthru->disable_done)
> ...
> nvme_put_ctrl(passthru->ctrl)
> blk_mq_free_request(rq)
> /* host ctrl removal frees ctrl */
> nvme_passthru_end(ctrl, ...) <-- use-after-free on ctrl
>
> Moving nvmet_req_complete(req, status) after blk_mq_free_request(rq) and
> nvme_passthru_end() avoids the reset deadlock on rq, keeps req->cmd and
> passthru->ref alive across nvme_passthru_end(), and completes the target
> request only after command effects have been applied.
This is better than mine, changed in v2.
>
More information about the Linux-nvme
mailing list