From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 6CB8FA0A0D; Tue, 2 Feb 2021 00:46:22 +0100 (CET) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id EA68E240209; Tue, 2 Feb 2021 00:46:21 +0100 (CET) Received: from mail-pg1-f170.google.com (mail-pg1-f170.google.com [209.85.215.170]) by mails.dpdk.org (Postfix) with ESMTP id C9B7140693; Mon, 1 Feb 2021 09:48:49 +0100 (CET) Received: by mail-pg1-f170.google.com with SMTP id r38so11590524pgk.13; Mon, 01 Feb 2021 00:48:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=pyZBK+xplcqms6xWIA4U14kSrTsc1obcmoctqCKwmxs=; b=t+pRv5jgQkvZ3FBgqbOyIfwIFgTTBte+NnTvWkjx5Z14gXnfvPxh6NqjarVXFMSzah ZfKLCx5XINw7Y//ZYloE4eMboshu8OV1HmtwecUetb+6IanJjyED4aKuJSsPfUOfvWBz gERSj5IyXfnf2OCBB9iaKiU9qLnev+vBSmVUoQDFCla/y78EVC6mb8UWvp3bpa1FCywf TluQqOp7POtgyZwOq683SI/Bh1H8e03z116Zom69EajXfLZCID3s+zbIxf/zTYB7QNJx KkP7YubvjL1xi8HHoWgEaKA/FqvO5gjklJAEmcJCC9ZKrBFzb6DhtYc4gjbkwigimz5S VrEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=pyZBK+xplcqms6xWIA4U14kSrTsc1obcmoctqCKwmxs=; b=iuJxrr3AQY8OrMCJsDBttFUlaPidnIVER8fnr1Ph3SkvavP4074SIfhMF8zTNk633+ hbeLvSaGtkHNSZrpfd/DEwoz5AyguAM4WI8e5MnWGdaBb7rEGHgaONiKpWWEdr7Tqwt+ O96l/G3MCDba1m8MCQC9oNSao434ITG8DRl+qr1LzUYRqRKbKgj//TM1h/sX2a23nlgE IkcmyPBenv7G1Kg4N5WkgxIL3M6/g3tO5mEwbOv6on7egM58IxucxBllOvtlsyZ2ji8a m2lBEGYD/cp/qdxzy1rJfzJQ/A996JbybmPFgUhBNHpUUrnujgBslZZbbHgw5zvb5fS8 rz0w== X-Gm-Message-State: AOAM530j7WoGVhVd5+1EXxMgrL2b3ME8xyNvDUoc3i1pyGRoXjSeUX9q RLAmpTCIUNiLAcKZgRef6o9UiC6Kcskq8P8p X-Google-Smtp-Source: ABdhPJzfaOi07qWw8s7kcilUJY+KlhT3JPawkV1QTxkAN89zkROZPwMLT4R5ZUW+R2Q2Kn22IyJRow== X-Received: by 2002:aa7:9d1a:0:b029:1c8:8148:a89d with SMTP id k26-20020aa79d1a0000b02901c88148a89dmr15492466pfp.66.1612169328635; Mon, 01 Feb 2021 00:48:48 -0800 (PST) Received: from localhost ([61.120.150.71]) by smtp.gmail.com with ESMTPSA id e3sm16670751pgs.60.2021.02.01.00.48.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Feb 2021 00:48:48 -0800 (PST) From: Peng He X-Google-Original-From: Peng He To: dev@dpdk.org, chenbo.xia@intel.com Cc: stable@dpdk.org Date: Mon, 1 Feb 2021 16:48:44 +0800 Message-Id: <20210201084844.2434-1-hepeng.0320@bytedance.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Mailman-Approved-At: Tue, 02 Feb 2021 00:46:20 +0100 Subject: [dpdk-dev] [PATCH v2] vhost: fix vid allocation race X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" vhost_new_device might be called in different threads at the same time. thread 1(config thread) rte_vhost_driver_start ->vhost_user_start_client ->vhost_user_add_connection -> vhost_new_device thread 2(vhost-events) vhost_user_read_cb ->vhost_user_msg_handler (return value < 0) -> vhost_user_start_client -> vhost_new_device So there could be a case that a same vid has been allocated twice, or some vid might be lost in DPDK lib however still held by the upper applications. Another place where race would happen is at the func *vhost_destroy_device*, but after a detailed investigation, the race does not exist as long as no two devices have the same vid: Calling vhost_destroy_devices in different threads with different vids is actually safe. Fixes: a277c715987 ("vhost: refactor code structure") Reported-by: Peng He Signed-off-by: Fei Chen Reviewed-by: Zhihong Wang --- lib/librte_vhost/vhost.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/librte_vhost/vhost.c b/lib/librte_vhost/vhost.c index efb136edd1..52ab93d1ec 100644 --- a/lib/librte_vhost/vhost.c +++ b/lib/librte_vhost/vhost.c @@ -26,6 +26,7 @@ #include "vhost_user.h" struct virtio_net *vhost_devices[MAX_VHOST_DEVICE]; +pthread_mutex_t vhost_dev_lock = PTHREAD_MUTEX_INITIALIZER; /* Called with iotlb_lock read-locked */ uint64_t @@ -645,6 +646,7 @@ vhost_new_device(void) struct virtio_net *dev; int i; + pthread_mutex_lock(&vhost_dev_lock); for (i = 0; i < MAX_VHOST_DEVICE; i++) { if (vhost_devices[i] == NULL) break; @@ -653,6 +655,7 @@ vhost_new_device(void) if (i == MAX_VHOST_DEVICE) { VHOST_LOG_CONFIG(ERR, "Failed to find a free slot for new device.\n"); + pthread_mutex_unlock(&vhost_dev_lock); return -1; } @@ -660,10 +663,13 @@ vhost_new_device(void) if (dev == NULL) { VHOST_LOG_CONFIG(ERR, "Failed to allocate memory for new dev.\n"); + pthread_mutex_unlock(&vhost_dev_lock); return -1; } vhost_devices[i] = dev; + pthread_mutex_unlock(&vhost_dev_lock); + dev->vid = i; dev->flags = VIRTIO_DEV_BUILTIN_VIRTIO_NET; dev->slave_req_fd = -1; -- 2.23.0