Skip to content

Commit e0ee8d9

Browse files
YongjiXiegregkh
authored andcommitted
nbd: Aovid double completion of a request
[ Upstream commit cddce01 ] There is a race between iterating over requests in nbd_clear_que() and completing requests in recv_work(), which can lead to double completion of a request. To fix it, flush the recv worker before iterating over the requests and don't abort the completed request while iterating. Fixes: 96d97e1 ("nbd: clear_sock on netlink disconnect") Reported-by: Jiang Yadong <jiangyadong@bytedance.com> Signed-off-by: Xie Yongji <xieyongji@bytedance.com> Reviewed-by: Josef Bacik <josef@toxicpanda.com> Link: https://lore.kernel.org/r/20210813151330.96-1-xieyongji@bytedance.com Signed-off-by: Jens Axboe <axboe@kernel.dk> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent f5cefe9 commit e0ee8d9

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

‎drivers/block/nbd.c‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,10 @@ static bool nbd_clear_req(struct request *req, void *data, bool reserved)
812812
{
813813
struct nbd_cmd *cmd = blk_mq_rq_to_pdu(req);
814814

815+
/* don't abort one completed request */
816+
if (blk_mq_request_completed(req))
817+
return true;
818+
815819
mutex_lock(&cmd->lock);
816820
cmd->status = BLK_STS_IOERR;
817821
mutex_unlock(&cmd->lock);
@@ -2024,15 +2028,19 @@ static void nbd_disconnect_and_put(struct nbd_device *nbd)
20242028
{
20252029
mutex_lock(&nbd->config_lock);
20262030
nbd_disconnect(nbd);
2027-
nbd_clear_sock(nbd);
2028-
mutex_unlock(&nbd->config_lock);
2031+
sock_shutdown(nbd);
20292032
/*
20302033
* Make sure recv thread has finished, so it does not drop the last
20312034
* config ref and try to destroy the workqueue from inside the work
2032-
* queue.
2035+
* queue. And this also ensure that we can safely call nbd_clear_que()
2036+
* to cancel the inflight I/Os.
20332037
*/
20342038
if (nbd->recv_workq)
20352039
flush_workqueue(nbd->recv_workq);
2040+
nbd_clear_que(nbd);
2041+
nbd->task_setup = NULL;
2042+
mutex_unlock(&nbd->config_lock);
2043+
20362044
if (test_and_clear_bit(NBD_RT_HAS_CONFIG_REF,
20372045
&nbd->config->runtime_flags))
20382046
nbd_config_put(nbd);

0 commit comments

Comments
 (0)