Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp2878805img; Sun, 24 Mar 2019 22:07:09 -0700 (PDT) X-Google-Smtp-Source: APXvYqw0tc4xvOHSUgK8jCvvJvdKdbF4ckiMENBaxBcmJzKn5dng5/u4eVO0swC9y4dmE6Xaxh02 X-Received: by 2002:a63:fd12:: with SMTP id d18mr21179217pgh.88.1553490429112; Sun, 24 Mar 2019 22:07:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553490429; cv=none; d=google.com; s=arc-20160816; b=0PdAjIibgeVRiwLmo1HUgfI/qxAxnqhy4W7yKNLqUikckx0zb4Tm58j8bYq/SWn3cK YhF7jingSRCIx3cHFz3p2XV3utnVrscpRShPeCq/ei+rdnqVlL1bohfmqS/myCV2Ql/p QGerQ1McxyisqlJUPfsLAPb+T5bCAFYE/ThnwbNls53zp2W4mmXJIPRU+y1Tj0Dz+B3e bs65jvivAhTp8Fse1ngp02TJCxYgBPjZRRkuJOLyd8q3LIBmFjDSqDvCAGzFnoTJLqV7 j/MGdJiyp91OcEa6YCZbiBBar5M8cIPoxsMnUaB+CzBtTX2xLKlmkzfB5BSw2P64e3Dw 4w0Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:cc:subject:date:to :from; bh=Y45d/bafIDQTR6FiuiMvUB4RJjHDUZRNutrIXuxu/xY=; b=GAbDuqkhO/S+BlV6Izp7Y7GRfwgxULK3pUJd4hTMuZqnlnQcXEugAeZQtAZP/FWtBr jU8qAQR7DSsa80mA45boO2QYvGdXwGU7kub37y2qJNSl8n/za66vS/T/+8K5ZaVK2r6Z HGGvrwP+/OeBaHxjChYSNcUB3WSxZggQ+ZqxKHy+tk8U2AsqApZl5EFOX+r14kigubK4 FDG2BMMGvwLON7/S/6xZKeH6twomruQAtUKRI7pR/Rf+eyx0pz9RpyixmCIZMNpDRSUh SyAHtdiQbsOTtsGWTJav1V5PmvX6+FFQo2ZUqAT8qSl05tEqpzG29iPZm+rQ/4chyRKw 288w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o16si13410578pgc.327.2019.03.24.22.06.53; Sun, 24 Mar 2019 22:07:09 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729380AbfCYFGM (ORCPT + 99 others); Mon, 25 Mar 2019 01:06:12 -0400 Received: from mx2.suse.de ([195.135.220.15]:42516 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726101AbfCYFGL (ORCPT ); Mon, 25 Mar 2019 01:06:11 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 9C803AC85; Mon, 25 Mar 2019 05:06:10 +0000 (UTC) From: NeilBrown To: Thomas Graf , Herbert Xu Date: Mon, 25 Mar 2019 16:05:39 +1100 Subject: [PATCH 1/4] rhashtable: use cmpxchg() in nested_table_alloc() Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org Message-ID: <155349033954.1111.16843256359207016881.stgit@noble.brown> In-Reply-To: <155349021177.1111.15681654355431465791.stgit@noble.brown> References: <155349021177.1111.15681654355431465791.stgit@noble.brown> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org nested_table_alloc() relies on the fact that there is at most one spinlock allocated for every slot in the top level nested table, so it is not possible for two threads to try to allocate the same table at the same time. This assumption is a little fragile (it is not explicit) and is unnecessary as cmpxchg() can be used instead. A future patch will replace the spinlocks by per-bucket bitlocks, and then we won't be able to protect the slot pointer with a spinlock. So replace rcu_assign_pointer() with cmpxchg() - which has equivalent barrier properties. If it the cmp fails, free the table that was just allocated. Signed-off-by: NeilBrown --- lib/rhashtable.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/rhashtable.c b/lib/rhashtable.c index f65e43fb1ff8..afe01a5048d7 100644 --- a/lib/rhashtable.c +++ b/lib/rhashtable.c @@ -131,9 +131,11 @@ static union nested_table *nested_table_alloc(struct rhashtable *ht, INIT_RHT_NULLS_HEAD(ntbl[i].bucket); } - rcu_assign_pointer(*prev, ntbl); - - return ntbl; + if (cmpxchg(prev, NULL, ntbl) == NULL) + return ntbl; + /* Raced with another thread. */ + kfree(ntbl); + return rcu_dereference(*prev); } static struct bucket_table *nested_bucket_table_alloc(struct rhashtable *ht,