Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp179838imm; Thu, 31 May 2018 21:47:02 -0700 (PDT) X-Google-Smtp-Source: ADUXVKLjPvUVUUof8S2Ws5aDYpwR/Wp/6ct/jou6/ZVfqKUezXxsIrFkNvZmL30dx5P1TxVkZzjF X-Received: by 2002:a17:902:8d85:: with SMTP id v5-v6mr9706909plo.93.1527828422929; Thu, 31 May 2018 21:47:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527828422; cv=none; d=google.com; s=arc-20160816; b=owGR4d4ldeDZXEvIMAoDbvSr3/eELpSGbHitKcyFD1JDCgJVOMgg5C5lKldxDmQLOm 2KYuMsdPzWF+M8KKxQpq6Bkz+hln2i/V+cX42ZXkGC0iU3i/rgg4FX6ruMq/0IRQY+Qa L7Kv+/Wsw/I9cBNOubQjI0NClBvk0zAO0/eDZR56KI2X03YlSlymIcAeD0H69/QGZusE Jf1YvKHZXHhy0keYyWcVqTqes2Px18gD3P1y367AfDP0nKRdPesGzEcEKNUxiTCBj0ZH U75FGLsBEbrqmTRVgEw0cWY3j+/3ai8se1NiaSAlhSaVIyuo/ij+4qLW7bTO0puIdr1+ 4luw== 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=ND7dZdv/xyduLnKiqexbo7r+t/k61Lv6MGgVT9PQngQ=; b=nS28ssttdkCWgjomUFkWYT53CDsiEGoY6RfJ0gYquGF+DV2BhcMJ3I28OG8M/HeWOZ TBbp2vy1QEhLFip+o5Qj2fJdgY/W83Bri92X7Z+mWeHWOn0gTarAXDiKHrtKjSXHNwbk fUS3mz885cqTg09EXt+IjRtWXFElXQt9P52H245j13twcNSMaJCjoMH+Zg5j4459yzUA Fmo6JtKpN1CL727prWlhhKL2MWWkmft58l7DosSglWewPok9AyW+Mqh+qvECeTpZ7d2K nwFW+r1wobasRIREd1XAoKjiwcl6BhmrubtLeto5ltui796gDJlSFmMYUx3dislapoTJ qzlw== 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 s24-v6si1807283pgv.494.2018.05.31.21.46.48; Thu, 31 May 2018 21:47:02 -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 S1751582AbeFAEqF (ORCPT + 99 others); Fri, 1 Jun 2018 00:46:05 -0400 Received: from mx2.suse.de ([195.135.220.15]:34105 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751441AbeFAEqB (ORCPT ); Fri, 1 Jun 2018 00:46:01 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay1.suse.de (charybdis-ext-too.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 3C2E8AD02; Fri, 1 Jun 2018 04:46:00 +0000 (UTC) From: NeilBrown To: Thomas Graf , Herbert Xu Date: Fri, 01 Jun 2018 14:44:09 +1000 Subject: [PATCH 09/18] rhashtable: use cmpxchg() in nested_table_alloc() Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org Message-ID: <152782824961.30340.16484931507108689498.stgit@noble> In-Reply-To: <152782754287.30340.4395718227884933670.stgit@noble> References: <152782754287.30340.4395718227884933670.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 1737fbd049da..86c801d04d4a 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,