From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dev-bounces@dpdk.org>
Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124])
	by inbox.dpdk.org (Postfix) with ESMTP id 762B4A00C4;
	Thu,  5 May 2022 09:22:16 +0200 (CEST)
Received: from [217.70.189.124] (localhost [127.0.0.1])
	by mails.dpdk.org (Postfix) with ESMTP id 5B08640C35;
	Thu,  5 May 2022 09:22:16 +0200 (CEST)
Received: from us-smtp-delivery-74.mimecast.com
 (us-smtp-delivery-74.mimecast.com [170.10.133.74])
 by mails.dpdk.org (Postfix) with ESMTP id 7D8FE40042
 for <dev@dpdk.org>; Thu,  5 May 2022 09:22:14 +0200 (CEST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;
 s=mimecast20190719; t=1651735333;
 h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
 to:to:cc:cc:mime-version:mime-version:content-type:content-type:
 in-reply-to:in-reply-to:references:references;
 bh=lffcWg1CvRUGco497jgMGTUbgJFCd9vSS22Lm3WjPWY=;
 b=Cl6RBESr6i7XvUxUx8Vb8rzTnqGeVeCWNIelPHqXPsefjOiaX2qJOhkiYaCsI2gekBxnRu
 KMceB8/SQucl4t0rqFC7bJL/GZom9fvQVlvPP5AzwR6Oo+qr/OTKoY+99wX1tjKfH7LwO+
 usADaxTFn0/4uoWJdXeyJqfHbe3cLNQ=
Received: from mail-lj1-f200.google.com (mail-lj1-f200.google.com
 [209.85.208.200]) by relay.mimecast.com with ESMTP with STARTTLS
 (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id
 us-mta-78-vZGqGUqSOpWfhkdwUjSrqQ-1; Thu, 05 May 2022 03:22:12 -0400
X-MC-Unique: vZGqGUqSOpWfhkdwUjSrqQ-1
Received: by mail-lj1-f200.google.com with SMTP id
 l13-20020a2e868d000000b0024f078d7ea0so1106845lji.4
 for <dev@dpdk.org>; Thu, 05 May 2022 00:22:11 -0700 (PDT)
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20210112;
 h=x-gm-message-state:mime-version:references:in-reply-to:from:date
 :message-id:subject:to:cc;
 bh=lffcWg1CvRUGco497jgMGTUbgJFCd9vSS22Lm3WjPWY=;
 b=uqvZK6kOt+/8zjKPJ1qjAwQYbB6rKYXGrJf18ttUh+jkwHV5LHRGobLwzlvp0MGi/H
 esuzB0laCn/iGNEVOpGqogyA0dBXgDIgIrCktujGvIIAmcEzDRyjrvnAGM9IpT8WadSt
 JQQzzAggMonP1uVqkP5djncTI0hCAzPTeifkDcDfQFYxLHnZOApWIa0yhhlnMIjFeKsS
 KwCquzApfHYDnvuiUsL2ZMsynkwwS2z0KKFVWU94k2fcEHqhDcASSjagaZxsT71M8kvu
 aStJaqTU1jG92R5hhR1maIzWWEbLJ04SV6UGypdZ/QN6v5t7BiNAV8oHVqFh1bEAAiiU
 HC6Q==
X-Gm-Message-State: AOAM5313wmkC2jd0h5ifoOy8qEN0evkmGR+B3VlAl2SZdjcrjJh5OdBG
 NKXMn1OBSnwuwdaz9bTXmr2e8y+mXQ1mdI66Ruvn6WGrlFAKCy7/yv561aXqErpoUKzcGMEN0ED
 XOg4SFhTVLnjHY9nS4+0=
X-Received: by 2002:a2e:9d90:0:b0:24f:224:8dfa with SMTP id
 c16-20020a2e9d90000000b0024f02248dfamr15098874ljj.46.1651735330527; 
 Thu, 05 May 2022 00:22:10 -0700 (PDT)
X-Google-Smtp-Source: ABdhPJwRkrsE1wRdfES2gQXTwRbx77Kfr8dCHzH0zbbCsUK2gq8vuGcKcYhYs45xU5sURuwZq1170dYhSkrxysEuH68=
X-Received: by 2002:a2e:9d90:0:b0:24f:224:8dfa with SMTP id
 c16-20020a2e9d90000000b0024f02248dfamr15098857ljj.46.1651735330305; Thu, 05
 May 2022 00:22:10 -0700 (PDT)
MIME-Version: 1.0
References: <20220505134008.2865-1-wenwux.ma@intel.com>
In-Reply-To: <20220505134008.2865-1-wenwux.ma@intel.com>
From: David Marchand <david.marchand@redhat.com>
Date: Thu, 5 May 2022 09:21:59 +0200
Message-ID: <CAJFAV8wE4UNt08QZ_OxNoy6fBYrvi1S=9O2r=svaOQL1gKJavw@mail.gmail.com>
Subject: Re: [PATCH] vhost: fix deadlock when handling user messages
To: Wenwu Ma <wenwux.ma@intel.com>
Cc: Maxime Coquelin <maxime.coquelin@redhat.com>, "Xia,
 Chenbo" <chenbo.xia@intel.com>, dev <dev@dpdk.org>, 
 Jiayu Hu <jiayu.hu@intel.com>, "Wang, Yinan" <yinan.wang@intel.com>,
 xingguang.he@intel.com
Authentication-Results: relay.mimecast.com;
 auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=dmarchan@redhat.com
X-Mimecast-Spam-Score: 0
X-Mimecast-Originator: redhat.com
Content-Type: text/plain; charset="UTF-8"
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: DPDK patches and discussions <dev.dpdk.org>
List-Unsubscribe: <https://mails.dpdk.org/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://mails.dpdk.org/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <https://mails.dpdk.org/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
Errors-To: dev-bounces@dpdk.org

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,


-- 
David Marchand