]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
nbd: fix race between timeout and normal completion
authorMing Lei <ming.lei@redhat.com>
Fri, 30 Aug 2024 03:41:45 +0000 (11:41 +0800)
committerJens Axboe <axboe@kernel.dk>
Fri, 30 Aug 2024 20:46:59 +0000 (14:46 -0600)
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 <josef@toxicpanda.com>
Cc: Yu Kuai <yukuai3@huawei.com>
Fixes: 2895f1831e91 ("nbd: don't clear 'NBD_CMD_INFLIGHT' flag if request is not completed")
Signed-off-by: Ming Lei <ming.lei@redhat.com>
Reviewed-by: Yu Kuai <yukuai3@huawei.com>
Link: https://lore.kernel.org/r/20240830034145.1827742-1-ming.lei@redhat.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
drivers/block/nbd.c

index 5b1811b1ba5fc280099564fc3dbdab6ff5cffca3..4d06472bf11228312812032ce0608f2e544589f1 100644 (file)
@@ -181,6 +181,17 @@ static void nbd_requeue_cmd(struct nbd_cmd *cmd)
 {
        struct request *req = blk_mq_rq_from_pdu(cmd);
 
+       lockdep_assert_held(&cmd->lock);
+
+       /*
+        * Clear INFLIGHT flag so that this cmd won't be completed in
+        * normal completion path
+        *
+        * INFLIGHT flag will be set when the cmd is queued to nbd next
+        * time.
+        */
+       __clear_bit(NBD_CMD_INFLIGHT, &cmd->flags);
+
        if (!test_and_set_bit(NBD_CMD_REQUEUED, &cmd->flags))
                blk_mq_requeue_request(req, true);
 }
@@ -491,8 +502,8 @@ static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req)
                                        nbd_mark_nsock_dead(nbd, nsock, 1);
                                mutex_unlock(&nsock->tx_lock);
                        }
-                       mutex_unlock(&cmd->lock);
                        nbd_requeue_cmd(cmd);
+                       mutex_unlock(&cmd->lock);
                        nbd_config_put(nbd);
                        return BLK_EH_DONE;
                }