Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp742669yba; Mon, 1 Apr 2019 16:09:15 -0700 (PDT) X-Google-Smtp-Source: APXvYqy4e920IgNM+7LqrSflfF+l97YfA3bbajM74qmPrg0cVtHg24WU4fBzMksVJYvClP5vWhHE X-Received: by 2002:a63:ff1d:: with SMTP id k29mr64206717pgi.258.1554160155669; Mon, 01 Apr 2019 16:09:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554160155; cv=none; d=google.com; s=arc-20160816; b=sr+LpW4xZJvzj7sLzfM7+gKWKXxkpiItHSrXJ6ezMtEokcqJBBzByDvjN67+5rRKiq BdqocJ5aW808yWLnNF1dZ2wckWTqlRthm+WSCGgOvEek7lgdKRoO+uaW37VYiD11KXoO Pes+0jLTbM65FmHMzKcuwonc1uSR2ZCFim7NGK3Tpta0oVZ0uM7o3nqXdy0EWlm28EJQ jMcOYGMwtMwSTfiA9WWtit9cIW7p8R2YsGZked/SQDhSSZdC8zRtDf2yPXgMzQa1Lx6S 8tGCxKjRFTXKEgZgsy6kqG80U0dm5IjYGoS45wyFJXeeNuBWRGwXKRE23pdS6YGzDUZT 9wHQ== 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=i9Wp0bCEqtggdWlNEmfEBZijoyA54dNSSetKQvIvT+w=; b=AwPuk67y2FCmvMz8k482gIioKCVGT0wnW9mnfcNojBFTvQ9LoULLEZqVUjALCVNcZk GMXap49B/Bfcdcm9X+gFpJZoQrmIM1NIi1hYIS3gTByavK4m3fU8APhLmiYrSq9dNpW5 YnuREr5eX5oz4hpR2LopZMQfdsKg6AUI/Zz2Jy1PO/42isMMIwTKFZvhMBB1g5V/NHyn gzae4im/GDMIszVUQhx6T/hhNAVuySEKICDKKDxXg/ALUl1JqnypCUi3VKVoTtlJCArI A8EMi21M3rhIfATEUktAnCGMfqvqBmjeAGG7yi5ZGxHpofLqPgYFwMQDfN97yNCHk1ej QAow== 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 73si1468095pfs.186.2019.04.01.16.09.00; Mon, 01 Apr 2019 16:09:15 -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 S1728681AbfDAXIU (ORCPT + 99 others); Mon, 1 Apr 2019 19:08:20 -0400 Received: from mx2.suse.de ([195.135.220.15]:54320 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725891AbfDAXIS (ORCPT ); Mon, 1 Apr 2019 19:08:18 -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 2AC9DAFFA; Mon, 1 Apr 2019 23:08:17 +0000 (UTC) From: NeilBrown To: Thomas Graf , Herbert Xu Date: Tue, 02 Apr 2019 10:07:45 +1100 Subject: [PATCH 1/4] rhashtable: use cmpxchg() in nested_table_alloc() Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org Message-ID: <155416006508.9540.3368212864327714370.stgit@noble.brown> In-Reply-To: <155416000985.9540.14182958463813560577.stgit@noble.brown> References: <155416000985.9540.14182958463813560577.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 811d51b7cb86..6c4f5c8e9baa 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,