From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from out1-smtp.messagingengine.com (out1-smtp.messagingengine.com [66.111.4.25]) by dpdk.org (Postfix) with ESMTP id 5B0481BBC3 for ; Fri, 11 May 2018 01:58:51 +0200 (CEST) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id C440C227CC; Thu, 10 May 2018 19:58:50 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute1.internal (MEProxy); Thu, 10 May 2018 19:58:50 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=monjalon.net; h= cc:date:from:in-reply-to:message-id:references:subject:to :x-me-sender:x-me-sender:x-sasl-enc; s=mesmtp; bh=hHS7InawXylEpt XW9hpbNSFnqmBmU0twh9rfdSwYFWg=; b=j+uZBWmkfoONmF8r73LDv19VBHp1wG ooZJyPJzhcqH3iWPpNqXDBRuo3MUAvQG+2TvBnNWymhFDREHSa8suDJQlmqMoa73 39x9yEnVeE4D4FeyMiltz9Op/b7EjlEOwWMTX1m2MO4BoZWJZwHOw2A4CKToRrBO t/tSAH9DS4ExQ= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:date:from:in-reply-to:message-id :references:subject:to:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; bh=hHS7InawXylEptXW9hpbNSFnqmBmU0twh9rfdSwYFWg=; b=QtRmAKR4 v1qycryXFgPNfZH0Yjc+T/OYDL3fGt2m68ojbpgtBBwqvGX7UD5v2HbnokMGb3iq Nms0bRX225HUCzgRktVCOfeik4yc+oibaJQLXo58mm5l9iTMNXibzWbWQYeN2Ybq 9Vgmq8dqC+jKSwvRoaoPoKc/XJfRKO71OHkzpclvz1AYfDKzh5wpsV2bvNqlKYiG SBp5VB0oDfrXmbY7XgIUKWTHkrfpZ1QVoT6H5bGPpSSolTPInAR8RPPOZDLXIMNP /ywaTxtDVzT8YiRNOnAzULropCkW8h2RXvDd0WfvEuhL/KHS5jH+wliNzEPWurR4 na3pYtJJ7jQRJQ== X-ME-Sender: Received: from xps.monjalon.net (184.203.134.77.rev.sfr.net [77.134.203.184]) by mail.messagingengine.com (Postfix) with ESMTPA id DC5A4E4393; Thu, 10 May 2018 19:58:49 -0400 (EDT) From: Thomas Monjalon To: dev@dpdk.org Cc: matan@mellanox.com, arybchenko@solarflare.com, stephen@networkplumber.org, ferruh.yigit@intel.com Date: Fri, 11 May 2018 01:58:32 +0200 Message-Id: <20180510235836.1099-8-thomas@monjalon.net> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180510235836.1099-1-thomas@monjalon.net> References: <20180509094337.26112-1-thomas@monjalon.net> <20180510235836.1099-1-thomas@monjalon.net> Subject: [dpdk-dev] [PATCH v3 07/11] ethdev: add lock to port allocation check X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 10 May 2018 23:58:51 -0000 From: Matan Azrad When comparing the port name, there can be a race condition with a thread allocating a new port and writing the name at the same time. It can lead to match with a partial name by error. The check of the port is now considered as a critical section protected with locks. This fix will be even more required for multi-process when the port availability will rely only on the name, in a following patch. Fixes: 84934303a17c ("ethdev: synchronize port allocation") Cc: stable@dpdk.org Signed-off-by: Matan Azrad Acked-by: Thomas Monjalon Reviewed-by: Andrew Rybchenko Reviewed-by: Stephen Hemminger --- lib/librte_ethdev/rte_ethdev.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/librte_ethdev/rte_ethdev.c b/lib/librte_ethdev/rte_ethdev.c index 9bd397b66..fa6d4940e 100644 --- a/lib/librte_ethdev/rte_ethdev.c +++ b/lib/librte_ethdev/rte_ethdev.c @@ -234,8 +234,8 @@ is_allocated(const struct rte_eth_dev *ethdev) return ethdev->data->name[0] != '\0'; } -struct rte_eth_dev * -rte_eth_dev_allocated(const char *name) +static struct rte_eth_dev * +_rte_eth_dev_allocated(const char *name) { unsigned i; @@ -247,6 +247,22 @@ rte_eth_dev_allocated(const char *name) return NULL; } +struct rte_eth_dev * +rte_eth_dev_allocated(const char *name) +{ + struct rte_eth_dev *ethdev; + + rte_eth_dev_shared_data_prepare(); + + rte_spinlock_lock(&rte_eth_dev_shared_data->ownership_lock); + + ethdev = _rte_eth_dev_allocated(name); + + rte_spinlock_unlock(&rte_eth_dev_shared_data->ownership_lock); + + return ethdev; +} + static uint16_t rte_eth_dev_find_free_port(void) { @@ -293,7 +309,7 @@ rte_eth_dev_allocate(const char *name) goto unlock; } - if (rte_eth_dev_allocated(name) != NULL) { + if (_rte_eth_dev_allocated(name) != NULL) { ethdev_log(ERR, "Ethernet Device with name %s already allocated!", name); -- 2.16.2