Received: by 2002:ac0:950c:0:0:0:0:0 with SMTP id f12csp3524905imc; Wed, 13 Mar 2019 22:07:29 -0700 (PDT) X-Google-Smtp-Source: APXvYqw56sollkkiwuDPz6JUISvKc9wvYRpXHM2v67B+m9zi8CFn0+dkDYB5up5/ApcJbOeEzY2D X-Received: by 2002:a63:6e8d:: with SMTP id j135mr43203034pgc.160.1552540049801; Wed, 13 Mar 2019 22:07:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1552540049; cv=none; d=google.com; s=arc-20160816; b=qRqdlX+wYcsKGuV7HEFeaSJzibxTzYYRaq+p/zoM5rmNR4Q0ibasw1bL3gUTV/X9Nj YY/LhNGPouB4AjhIxkqzUEmdPXFpsO0m2xtUdAYwQKU0XXR0xrsNrAdjtvSnF8AvDu7j gNJDcZer5P9ZZmL8UVRPpktovER3pVHBkAR291SrUfADyHk7eOe792XyKgCH/o/47uXu EPmOjbTlJtq8eaVL7on/gc2mcRmg4KSdAz6KagfmBHrrP7cYUB6Lg34oVLn0o0imKLKM XXVc1iYU25iDxALpeia2nMr6mNXVLKNezih1LK/8qStgN5zcKaHhZ+KN7kAvQ8+dBOoA X+rA== 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=Si2jZzvO+dPG1hZnoUIeGMzuJWx7VJxuqPQ3fnoN8Wk=; b=UfR3PQ8ximQMAfNhWm5QayAkU4wPQ5gnisQ+41NOBany1cyS1gv6QNKNM+UsFNQE5f aDCCYGvWJ7q3ev1HDv1mcp2G46K6iXP4TPuWQZOjY43WknLnK0pZBU/PPocOWn1Ed3CC +259Os/7KvAHrfR2cqsf0NfBsmnGJWu+MSr6cQ8uB4WhPr9pjThKRGsMOnAf5NziR8Wr Sc1NkiqMBUFWhPr3RKoqLZn3M3tlRNXrEpHCBrdq4/BTqyJkRF/C8XLyCDLLHLqM2kkN MZ5Aw6YV0oKM8id1vc/Y/2YKayzUieF/5vKi33VxQAhTF4Qu5nUPWFRWrxdaUDntC1Er zENg== 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 u24si12426369pfa.78.2019.03.13.22.07.14; Wed, 13 Mar 2019 22:07:29 -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 S1727117AbfCNFGq (ORCPT + 99 others); Thu, 14 Mar 2019 01:06:46 -0400 Received: from mx2.suse.de ([195.135.220.15]:55832 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726103AbfCNFGp (ORCPT ); Thu, 14 Mar 2019 01:06:45 -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 6B7EDAE12; Thu, 14 Mar 2019 05:06:44 +0000 (UTC) From: NeilBrown To: Thomas Graf , Herbert Xu Date: Thu, 14 Mar 2019 16:05:28 +1100 Subject: [PATCH 1/3] rhashtable: use cmpxchg() in nested_table_alloc() Cc: netdev@vger.kernel.org, "Paul E. McKenney" , linux-kernel@vger.kernel.org Message-ID: <155253992824.5022.10318044793639169265.stgit@noble.brown> In-Reply-To: <155253979234.5022.1840929790507376038.stgit@noble.brown> References: <155253979234.5022.1840929790507376038.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. We can instead protect against a race using cmpxchg() - 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 0a105d4af166..c983c0ee15d5 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,