nbd: don't handle response without a corresponding request message
[platform/kernel/linux-starfive.git] / drivers / block / nbd.c
index 1183f78..d18ba55 100644 (file)
@@ -131,6 +131,12 @@ struct nbd_device {
 };
 
 #define NBD_CMD_REQUEUED       1
+/*
+ * This flag will be set if nbd_queue_rq() succeed, and will be checked and
+ * cleared in completion. Both setting and clearing of the flag are protected
+ * by cmd->lock.
+ */
+#define NBD_CMD_INFLIGHT       2
 
 struct nbd_cmd {
        struct nbd_device *nbd;
@@ -405,6 +411,7 @@ static enum blk_eh_timer_return nbd_xmit_timeout(struct request *req,
        if (!mutex_trylock(&cmd->lock))
                return BLK_EH_RESET_TIMER;
 
+       __clear_bit(NBD_CMD_INFLIGHT, &cmd->flags);
        if (!refcount_inc_not_zero(&nbd->config_refs)) {
                cmd->status = BLK_STS_TIMEOUT;
                mutex_unlock(&cmd->lock);
@@ -734,6 +741,12 @@ static struct nbd_cmd *nbd_read_stat(struct nbd_device *nbd, int index)
        cmd = blk_mq_rq_to_pdu(req);
 
        mutex_lock(&cmd->lock);
+       if (!__test_and_clear_bit(NBD_CMD_INFLIGHT, &cmd->flags)) {
+               dev_err(disk_to_dev(nbd->disk), "Suspicious reply %d (status %u flags %lu)",
+                       tag, cmd->status, cmd->flags);
+               ret = -ENOENT;
+               goto out;
+       }
        if (cmd->cmd_cookie != nbd_handle_to_cookie(handle)) {
                dev_err(disk_to_dev(nbd->disk), "Double reply on req %p, cmd_cookie %u, handle cookie %u\n",
                        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)
                return true;
 
        mutex_lock(&cmd->lock);
+       __clear_bit(NBD_CMD_INFLIGHT, &cmd->flags);
        cmd->status = BLK_STS_IOERR;
        mutex_unlock(&cmd->lock);
 
@@ -969,7 +983,13 @@ again:
         * returns EAGAIN can be retried on a different socket.
         */
        ret = nbd_send_cmd(nbd, cmd, index);
-       if (ret == -EAGAIN) {
+       /*
+        * Access to this flag is protected by cmd->lock, thus it's safe to set
+        * the flag after nbd_send_cmd() succeed to send request to server.
+        */
+       if (!ret)
+               __set_bit(NBD_CMD_INFLIGHT, &cmd->flags);
+       else if (ret == -EAGAIN) {
                dev_err_ratelimited(disk_to_dev(nbd->disk),
                                    "Request send failed, requeueing\n");
                nbd_mark_nsock_dead(nbd, nsock, 1);
@@ -1762,7 +1782,9 @@ static struct nbd_device *nbd_dev_add(int index, unsigned int refs)
        disk->fops = &nbd_fops;
        disk->private_data = nbd;
        sprintf(disk->disk_name, "nbd%d", index);
-       add_disk(disk);
+       err = add_disk(disk);
+       if (err)
+               goto out_err_disk;
 
        /*
         * Now publish the device.
@@ -1771,6 +1793,8 @@ static struct nbd_device *nbd_dev_add(int index, unsigned int refs)
        nbd_total_devices++;
        return nbd;
 
+out_err_disk:
+       blk_cleanup_disk(disk);
 out_free_idr:
        mutex_lock(&nbd_index_mutex);
        idr_remove(&nbd_index_idr, index);