Received: by 2002:ac0:a591:0:0:0:0:0 with SMTP id m17-v6csp1476511imm; Fri, 6 Jul 2018 00:24:43 -0700 (PDT) X-Google-Smtp-Source: AAOMgpeKykUCB49+5/mIP8jvR8j2sLUgACZznoxKyqBUBnNlJU2DwuEHeMQgQP1aHgrsyT0xHfaT X-Received: by 2002:a62:b29c:: with SMTP id z28-v6mr9658019pfl.8.1530861883124; Fri, 06 Jul 2018 00:24:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530861883; cv=none; d=google.com; s=arc-20160816; b=W73SqQm9oNbF/PCnLD2KBd9P+HFblDVZYL/DupE2UeJs3F9XO+JQjbQ3nxPsW0xXhd QwCGQNdtHULKqdmJ4dVAhpbp+qIBGedFR98L5dtibWDVGLE+aTp9kpeWgkL/0AM4s+IT BWI6TYRlSZgJy6520H2fBqdxhgHC53DnlGmdQSFgE2BT6t6tAjG5T9kkXOBJtrnPwlcP RuEG0rcnXEX2P7lPuJ7bW/EOO6RJRRt+RUypo0P0J+3ldWKoOLb24iUjxxJ8ZKbc6jTo Cew6saNkdcOvyXyRSTov5IUJh0hpfQjOoRkZPkKUH9OpkZhlhKbzROeU8CyJ0vRkBHJ8 TteQ== 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:arc-authentication-results; bh=y2IKCga0vAcpLtviAZ6Y9isK7s4Q75Jl6LgAOOc7EWo=; b=EvMLfR7/k+51QcO7fy4405WgtpliQVcferp+ydBD40tmkxSk74TRcovNvU8zyeSC3Q CS3Ah6mmBzZ3/lWmLyXkpGlBr9KTuHuLFz/YPkxEPTD5AUW9NOn95vEQFObVi93sJKbf hLnvIq3Q3ilU31qyuIUBNUJ/Cp+/gVp2445x6/UjRA+/5ur50HdMkTNEXFOPOtx8p5Z4 pr/14mcLssc9DpLlST/cZV6SRQhCuAhiYbFMRFY3TUTFXVCZkPzaZZaLcssecE0ipX3K rOSGdUvEjm/V+0fyxFP1hpQTE+4zofWMNp5g1Ut+5RKrpE4LvYVfqf+FJos9bM6VyajL Nysg== 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 t3-v6si7188602pgf.236.2018.07.06.00.24.28; Fri, 06 Jul 2018 00:24:43 -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 S1754158AbeGFHXP (ORCPT + 99 others); Fri, 6 Jul 2018 03:23:15 -0400 Received: from mx2.suse.de ([195.135.220.15]:48864 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753476AbeGFHXM (ORCPT ); Fri, 6 Jul 2018 03:23:12 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay1.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 89E3FAFCC; Fri, 6 Jul 2018 07:23:11 +0000 (UTC) From: NeilBrown To: Thomas Graf , Herbert Xu Date: Fri, 06 Jul 2018 17:22:30 +1000 Subject: [PATCH 1/5] rhashtable: use cmpxchg() in nested_table_alloc() Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org Message-ID: <153086175005.24852.16109495489833855391.stgit@noble> In-Reply-To: <153086169828.24852.10332573315056854948.stgit@noble> References: <153086169828.24852.10332573315056854948.stgit@noble> 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. A future patch will change the locking and invalidate this assumption. So change the code to protect against a race using cmpxchg() - if it loses, it frees the table that it 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 9ddb7134285e..c6eaeaff16d1 100644 --- a/lib/rhashtable.c +++ b/lib/rhashtable.c @@ -132,9 +132,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,