Skip to content

Commit 6e73b94

Browse files
Ming Leigregkh
authored andcommitted
nbd: fix race between timeout and normal completion
[ Upstream commit c9ea57c ] 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]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 75a5e59 commit 6e73b94

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
}
@@ -461,8 +472,8 @@ static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req)
461472
nbd_mark_nsock_dead(nbd, nsock, 1);
462473
mutex_unlock(&nsock->tx_lock);
463474
}
464-
mutex_unlock(&cmd->lock);
465475
nbd_requeue_cmd(cmd);
476+
mutex_unlock(&cmd->lock);
466477
nbd_config_put(nbd);
467478
return BLK_EH_DONE;
468479
}

0 commit comments

Comments
 (0)