vhost: fix deadlock when handling user messages

Message ID 20220505134008.2865-1-wenwux.ma@intel.com (mailing list archive)
State Superseded, archived
Delegated to: Maxime Coquelin
Headers
Series vhost: fix deadlock when handling user messages |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation success Compilation OK
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-mellanox-Performance success Performance Testing PASS
ci/intel-Testing success Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/github-robot: build success github build: passed
ci/iol-x86_64-unit-testing success Testing PASS
ci/iol-aarch64-compile-testing success Testing PASS
ci/iol-x86_64-compile-testing success Testing PASS

Commit Message

Ma, WenwuX May 5, 2022, 1:40 p.m. UTC
  In vhost_user_msg_handler(), if vhost message handling
failed, we should check whether the queue is locked and
release the lock before returning. Or, it will cause a
deadlock later.

Signed-off-by: Wenwu Ma <wenwux.ma@intel.com>
---
 lib/vhost/vhost_user.c | 2 ++
 1 file changed, 2 insertions(+)
  

Comments

David Marchand May 5, 2022, 7:21 a.m. UTC | #1
Hello,

On Thu, May 5, 2022 at 7:42 AM Wenwu Ma <wenwux.ma@intel.com> wrote:
>
> In vhost_user_msg_handler(), if vhost message handling
> failed, we should check whether the queue is locked and
> release the lock before returning. Or, it will cause a
> deadlock later.

Fixes: 7f31d4ea05ca ("vhost: fix lock on device readiness notification")
Cc: stable@dpdk.org

>
> Signed-off-by: Wenwu Ma <wenwux.ma@intel.com>
> ---
>  lib/vhost/vhost_user.c | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/lib/vhost/vhost_user.c b/lib/vhost/vhost_user.c
> index 1d390677fa..80a5df6e9d 100644
> --- a/lib/vhost/vhost_user.c
> +++ b/lib/vhost/vhost_user.c
> @@ -3113,6 +3113,8 @@ vhost_user_msg_handler(int vid, int fd)
>                 send_vhost_reply(dev, fd, &ctx);
>         } else if (ret == RTE_VHOST_MSG_RESULT_ERR) {
>                 VHOST_LOG_CONFIG(ERR, "(%s) vhost message handling failed.\n", dev->ifname);
> +               if (unlock_required)
> +                       vhost_user_unlock_all_queue_pairs(dev);
>                 return -1;
>         }
>

This fixes the issue, but my concern is that changes in the future
might introduce a new return statement (forgetting to unlock).
I suggest having a single return statement, like:

diff --git a/lib/vhost/vhost_user.c b/lib/vhost/vhost_user.c
index 1d390677fa..4baf969ee0 100644
--- a/lib/vhost/vhost_user.c
+++ b/lib/vhost/vhost_user.c
@@ -2976,7 +2976,6 @@ vhost_user_msg_handler(int vid, int fd)
                return -1;
        }

-       ret = 0;
        request = ctx.msg.request.master;
        if (request > VHOST_USER_NONE && request < VHOST_USER_MAX &&
                        vhost_message_str[request]) {
@@ -3113,9 +3112,11 @@ vhost_user_msg_handler(int vid, int fd)
                send_vhost_reply(dev, fd, &ctx);
        } else if (ret == RTE_VHOST_MSG_RESULT_ERR) {
                VHOST_LOG_CONFIG(ERR, "(%s) vhost message handling
failed.\n", dev->ifname);
-               return -1;
+               ret = -1;
+               goto unlock;
        }

+       ret = 0;
        for (i = 0; i < dev->nr_vring; i++) {
                struct vhost_virtqueue *vq = dev->virtqueue[i];
                bool cur_ready = vq_is_ready(dev, vq);
@@ -3126,10 +3127,11 @@ vhost_user_msg_handler(int vid, int fd)
                }
        }

+unlock:
        if (unlock_required)
                vhost_user_unlock_all_queue_pairs(dev);

-       if (!virtio_is_ready(dev))
+       if (ret != 0 || !virtio_is_ready(dev))
                goto out;

        /*
@@ -3156,7 +3158,7 @@ vhost_user_msg_handler(int vid, int fd)
        }

 out:
-       return 0;
+       return ret;
 }

 static int process_slave_message_reply(struct virtio_net *dev,
  

Patch

diff --git a/lib/vhost/vhost_user.c b/lib/vhost/vhost_user.c
index 1d390677fa..80a5df6e9d 100644
--- a/lib/vhost/vhost_user.c
+++ b/lib/vhost/vhost_user.c
@@ -3113,6 +3113,8 @@  vhost_user_msg_handler(int vid, int fd)
 		send_vhost_reply(dev, fd, &ctx);
 	} else if (ret == RTE_VHOST_MSG_RESULT_ERR) {
 		VHOST_LOG_CONFIG(ERR, "(%s) vhost message handling failed.\n", dev->ifname);
+		if (unlock_required)
+			vhost_user_unlock_all_queue_pairs(dev);
 		return -1;
 	}