Skip to content

Commit c9ea57c

Browse files
Ming Leiaxboe
authored andcommitted
nbd: fix race between timeout and normal completion
If request timetout is handled by nbd_requeue_cmd(), normal completion has to be stopped for avoiding to complete this requeued request, other use-after-free can be triggered. Fix the race by clearing NBD_CMD_INFLIGHT in nbd_requeue_cmd(), meantime make sure that cmd->lock is grabbed for clearing the flag and the requeue. Cc: Josef Bacik <[email protected]> Cc: Yu Kuai <[email protected]> Fixes: 2895f18 ("nbd: don't clear 'NBD_CMD_INFLIGHT' flag if request is not completed") Signed-off-by: Ming Lei <[email protected]> Reviewed-by: Yu Kuai <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 12c612e commit c9ea57c

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

drivers/block/nbd.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,17 @@ static void nbd_requeue_cmd(struct nbd_cmd *cmd)
181181
{
182182
struct request *req = blk_mq_rq_from_pdu(cmd);
183183

184+
lockdep_assert_held(&cmd->lock);
185+
186+
/*
187+
* Clear INFLIGHT flag so that this cmd won't be completed in
188+
* normal completion path
189+
*
190+
* INFLIGHT flag will be set when the cmd is queued to nbd next
191+
* time.
192+
*/
193+
__clear_bit(NBD_CMD_INFLIGHT, &cmd->flags);
194+
184195
if (!test_and_set_bit(NBD_CMD_REQUEUED, &cmd->flags))
185196
blk_mq_requeue_request(req, true);
186197
}
@@ -491,8 +502,8 @@ static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req)
491502
nbd_mark_nsock_dead(nbd, nsock, 1);
492503
mutex_unlock(&nsock->tx_lock);
493504
}
494-
mutex_unlock(&cmd->lock);
495505
nbd_requeue_cmd(cmd);
506+
mutex_unlock(&cmd->lock);
496507
nbd_config_put(nbd);
497508
return BLK_EH_DONE;
498509
}

0 commit comments

Comments
 (0)