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 50388A09E4; Fri, 29 Jan 2021 10:45:44 +0100 (CET) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 8907524015B; Fri, 29 Jan 2021 10:45:32 +0100 (CET) Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) by mails.dpdk.org (Postfix) with ESMTP id 03F2140395 for ; Fri, 29 Jan 2021 08:35:53 +0100 (CET) Received: by mail-pl1-f181.google.com with SMTP id j11so2174743plt.11 for ; Thu, 28 Jan 2021 23:35:52 -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:mime-version :content-transfer-encoding; bh=x//LGnjS9tV5afScQZZJfapVPBaAF8m0eLdk5PA+tMQ=; b=ipSFNoRb3I0QOH0zCYNeJz4OWHze9sylpx2SfLG59vFB1NYEgPzSTPMQ+OcRD0AUow 0mOlAgkjKjAupqWptXLKvH7Bx2NzvFKCM2ehK240UCCzw8iPox4h0z+sxM89Kqx4OOVZ VAcGvK7WWd0uxzGJ2IGPxgexAxLTI+Fo8dj0rjfjhCXBJOw73YWjaSW+Tkkut7hEsfLT bgaY7nZd/fFfhzaLesNxRuviu2SH7V0zQZqk9ZCDV5GpUorWouqGDISndIe1oJUZpyTZ lBaqfoO6ll9jnlv5PVKXjmYfG8gsAjNmKYnGYHYn5unOL37uUM2AmRjf1M3i/zYlC7/8 RFjw== 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:mime-version :content-transfer-encoding; bh=x//LGnjS9tV5afScQZZJfapVPBaAF8m0eLdk5PA+tMQ=; b=tSY1SLw0Twpjz26togcq9JZviVEYQtj77l2h3Io2LwZDdYwlROjZk3/ZYSF4AM7E3r ZZZsCiNO956W9IB+AL8P6g9xXV6T2Ua7NteaA20bkEwWt/og08xHC4J2zAyou/KnE95t eJd7AfCy4zsqHSMrdPLRbxYysJtqyrDqymK57RNKP1MokPRTm/aLGLhhtkLX+SpPjFk1 pOb0vaGPYetSQ71YWbF/9XNqHyMt5oZdhm/dVy4ftE/t+KiuAj/IxE4M3zmcE0mmtYVe iGBFWfmVQFNRF7f+8Cbefl9XT9yLRfTS/H4z4UvYgsjAMIgFP/byjqXP4M9ADsOgfeRX dB2g== X-Gm-Message-State: AOAM530W7Utq+hXFEwDmmfy5sLHE7yu0LUm28M0gwZ+BNZVJ3yuCa4iv N8Ff4NoE6QqY7vCMweCVM/GUiKw8YR2vfg== X-Google-Smtp-Source: ABdhPJz+Yto8lyctmFD7aKbJgBNaUrQ45hmV+XNJ2R6bXBQXBEJ0yhSgBdPjbkiP2Lywb1wiJAOVpQ== X-Received: by 2002:a17:90b:2286:: with SMTP id kx6mr3307163pjb.92.1611905751870; Thu, 28 Jan 2021 23:35:51 -0800 (PST) Received: from localhost ([61.120.150.71]) by smtp.gmail.com with ESMTPSA id o14sm7138519pjf.12.2021.01.28.23.35.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Jan 2021 23:35:51 -0800 (PST) From: Peng He X-Google-Original-From: Peng He To: dev@dpdk.org Cc: maxime.coquelin@redhat.com Date: Fri, 29 Jan 2021 15:35:47 +0800 Message-Id: <20210129073547.80108-1-hepeng.0320@bytedance.com> X-Mailer: git-send-email 2.23.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Mailman-Approved-At: Fri, 29 Jan 2021 10:45:28 +0100 Subject: [dpdk-dev] [PATCH] lib/librte_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" From: "chenwei.0515" vhost_new_devcie 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. 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..db11d293d2 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