Skip to content

Commit 4e6eef5

Browse files
YuKuai-huaweiaxboe
authored andcommitted
nbd: don't handle response without a corresponding request message
While handling a response message from server, nbd_read_stat() will try to get request by tag, and then complete the request. However, this is problematic if nbd haven't sent a corresponding request message: t1 t2 submit_bio nbd_queue_rq blk_mq_start_request recv_work nbd_read_stat blk_mq_tag_to_rq blk_mq_complete_request nbd_send_cmd Thus add a new cmd flag 'NBD_CMD_INFLIGHT', it will be set in nbd_send_cmd() and checked in nbd_read_stat(). Noted that this patch can't fix that blk_mq_tag_to_rq() might return a freed request, and this will be fixed in following patches. Signed-off-by: Yu Kuai <yukuai3@huawei.com> Reviewed-by: Ming Lei <ming.lei@redhat.com> Reviewed-by: Josef Bacik <josef@toxicpanda.com> Link: https://lore.kernel.org/r/20210916093350.1410403-2-yukuai3@huawei.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent c573d58 commit 4e6eef5

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

drivers/block/nbd.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ struct nbd_device {
131131
};
132132

133133
#define NBD_CMD_REQUEUED 1
134+
/*
135+
* This flag will be set if nbd_queue_rq() succeed, and will be checked and
136+
* cleared in completion. Both setting and clearing of the flag are protected
137+
* by cmd->lock.
138+
*/
139+
#define NBD_CMD_INFLIGHT 2
134140

135141
struct nbd_cmd {
136142
struct nbd_device *nbd;
@@ -405,6 +411,7 @@ static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req,
405411
if (!mutex_trylock(&cmd->lock))
406412
return BLK_EH_RESET_TIMER;
407413

414+
__clear_bit(NBD_CMD_INFLIGHT, &cmd->flags);
408415
if (!refcount_inc_not_zero(&nbd->config_refs)) {
409416
cmd->status = BLK_STS_TIMEOUT;
410417
mutex_unlock(&cmd->lock);
@@ -734,6 +741,12 @@ static struct nbd_cmd *nbd_read_stat(struct nbd_device *nbd, int index)
734741
cmd = blk_mq_rq_to_pdu(req);
735742

736743
mutex_lock(&cmd->lock);
744+
if (!__test_and_clear_bit(NBD_CMD_INFLIGHT, &cmd->flags)) {
745+
dev_err(disk_to_dev(nbd->disk), "Suspicious reply %d (status %u flags %lu)",
746+
tag, cmd->status, cmd->flags);
747+
ret = -ENOENT;
748+
goto out;
749+
}
737750
if (cmd->cmd_cookie != nbd_handle_to_cookie(handle)) {
738751
dev_err(disk_to_dev(nbd->disk), "Double reply on req %p, cmd_cookie %u, handle cookie %u\n",
739752
req, cmd->cmd_cookie, nbd_handle_to_cookie(handle));
@@ -833,6 +846,7 @@ static bool nbd_clear_req(struct request *req, void *data, bool reserved)
833846
return true;
834847

835848
mutex_lock(&cmd->lock);
849+
__clear_bit(NBD_CMD_INFLIGHT, &cmd->flags);
836850
cmd->status = BLK_STS_IOERR;
837851
mutex_unlock(&cmd->lock);
838852

@@ -969,7 +983,13 @@ static int nbd_handle_cmd(struct nbd_cmd *cmd, int index)
969983
* returns EAGAIN can be retried on a different socket.
970984
*/
971985
ret = nbd_send_cmd(nbd, cmd, index);
972-
if (ret == -EAGAIN) {
986+
/*
987+
* Access to this flag is protected by cmd->lock, thus it's safe to set
988+
* the flag after nbd_send_cmd() succeed to send request to server.
989+
*/
990+
if (!ret)
991+
__set_bit(NBD_CMD_INFLIGHT, &cmd->flags);
992+
else if (ret == -EAGAIN) {
973993
dev_err_ratelimited(disk_to_dev(nbd->disk),
974994
"Request send failed, requeueing\n");
975995
nbd_mark_nsock_dead(nbd, nsock, 1);

0 commit comments

Comments
 (0)