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 1C1081B7AA for ; Thu, 10 May 2018 00:43:27 +0200 (CEST) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id A997820C3A; Wed, 9 May 2018 18:43:26 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute1.internal (MEProxy); Wed, 09 May 2018 18:43:26 -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=Oy5qeX0Q96Vj49 TQGDBf+mHcAiKnQ+nOpC6sza4wu7g=; b=g1X12Q95WrQTuF5XHTrMPtoE7wjnAe nFSmHl1YJ216CpnLkWhR8IWV4z+6TGxGmawh3vZEZT3WO0I9zRR6CuUEbMyvepUf xQBLAYE36FpRqtf3BAxer/rsZuk0x8n9otlNEBBZHYE9YeryH/CT6kkQkxn4lXKo mB3wLpVV8uV7o= 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=Oy5qeX0Q96Vj49TQGDBf+mHcAiKnQ+nOpC6sza4wu7g=; b=l6hxdwYW E9X1/j1gct/hPojv+Y7zZZF17WqkBi/hTUHGw7HueRMc7hAk7okxP0OW6RR4QKgI 0uQVaxmcsz2yni0uMOwJpx1SeJzPDB9YmW04Vljh6Lvk/svm/B8nQLZ63UM5DShR b1mpz1484bgSNZz7AT+TOu5wQ6T2MqoC6/XWMLlkTFb9raoQu9uusAmQDxGR2vtq leVa9VvNRd3ZMi9gcQ7MVnXE09OzdaOgzD/gW8vUY79XX7PqYuJKz8QmuzmbruMY tD3EUF29fBV3oiFRPECFNMzYNAqVf+T+PU26jJNqZ0guuuKUIrpaQz3w/ruBGM1G 4gWRvA4Pjq8FnQ== 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 04F72E5096; Wed, 9 May 2018 18:43:25 -0400 (EDT) From: Thomas Monjalon To: dev@dpdk.org Cc: Matan Azrad Date: Thu, 10 May 2018 00:43:09 +0200 Message-Id: <20180509224313.27289-8-thomas@monjalon.net> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180509224313.27289-1-thomas@monjalon.net> References: <20180509094337.26112-1-thomas@monjalon.net> <20180509224313.27289-1-thomas@monjalon.net> Subject: [dpdk-dev] [PATCH v2 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: Wed, 09 May 2018 22:43:27 -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 --- 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 6ddae9960..4fa7400d5 100644 --- a/lib/librte_ethdev/rte_ethdev.c +++ b/lib/librte_ethdev/rte_ethdev.c @@ -227,8 +227,8 @@ rte_eth_dev_shared_data_prepare(void) rte_spinlock_unlock(&rte_eth_shared_data_lock); } -struct rte_eth_dev * -rte_eth_dev_allocated(const char *name) +static struct rte_eth_dev * +rte_eth_dev_allocated_nolock(const char *name) { unsigned i; @@ -240,6 +240,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_nolock(name); + + rte_spinlock_unlock(&rte_eth_dev_shared_data->ownership_lock); + + return ethdev; +} + static uint16_t rte_eth_dev_find_free_port(void) { @@ -286,7 +302,7 @@ rte_eth_dev_allocate(const char *name) goto unlock; } - if (rte_eth_dev_allocated(name) != NULL) { + if (rte_eth_dev_allocated_nolock(name) != NULL) { ethdev_log(ERR, "Ethernet Device with name %s already allocated!", name); -- 2.16.2