Received: by 10.192.165.148 with SMTP id m20csp305086imm; Thu, 3 May 2018 20:55:38 -0700 (PDT) X-Google-Smtp-Source: AB8JxZpSv634RVtDMRInklhwt5pBXCuCdKig/DADTeFbr+vE2jXVyLFej220p/l4nmFNDCqkyiSm X-Received: by 10.98.11.210 with SMTP id 79mr25302591pfl.4.1525406138194; Thu, 03 May 2018 20:55:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525406138; cv=none; d=google.com; s=arc-20160816; b=zVmAMhU85NYjpCGdUMb+yCQLPsK0MXIeEmooEPgRzXWo1UuBn+cV3WuBWFo1+fdXha RGR/BphD5dJHfL5+QtJGWAU/AYEHvLbZ+NMLJWIM1CiNMWWYUQXdfvWX7TIOv1RCEGF3 9rTCa7oXSC++yABwZ6KqZI0/5avJrR+zRUJ4da3eaZkE/EnCEkKboZ1hqTOZ3tHz1NrY LBfEsAqyIsmWJKBk/JivIsHo7CqysUtjLR3siIBD5IJ7fCMiXmc0fcphP0wC/HIXnvFn ZYEdPMo4jAZJv4dZwlrUyu2UJxIr8l6aNTU8KOsVmOGUC6zP0eac1Nqj7prIw6uxYdua jcjg== 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=iUfUewWKrHkAEF75iC/SnKT5IYJgcGfbUraYdh5bi/o=; b=EQVj+sbmxh5qtNhL2jFh/N4f1H3cOAEHZ/Jmth61Lsl8bggJxktpARSUA1oo+LCKZZ 5AzgBnowLDovgFL+idAaKMXonG66hVX8YT4rNEu5xjXSR3ol6o9htjpDSz+XI9MlgR4t 4VK97Chg+y6DBxU5OvFbSJa0gnGmxJ924k4q4cPCvxIBTT2FsG04/j2xki62TfBQh1x9 ZGxswfY6Vjs3ZJdcke3ABRLzrVJpvETFLfxyvPfwMQCsjATYjA/LnMTE441b/2TlAZLQ eA208u6Z2W1mgTCstXuqnGHWCUqj+xozNnuxWivvYzjLpjmRImgVE1Gp665O/DY03j17 kNZA== 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 b7-v6si15848887pla.345.2018.05.03.20.55.23; Thu, 03 May 2018 20:55:38 -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 S1751511AbeEDDzI (ORCPT + 99 others); Thu, 3 May 2018 23:55:08 -0400 Received: from mx2.suse.de ([195.135.220.15]:44986 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751236AbeEDDzF (ORCPT ); Thu, 3 May 2018 23:55:05 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 687DBAD0C; Fri, 4 May 2018 03:55:04 +0000 (UTC) From: NeilBrown To: Thomas Graf , Herbert Xu Date: Fri, 04 May 2018 13:54:14 +1000 Subject: [PATCH 4/8] rhashtable: fix race in nested_table_alloc() Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org Message-ID: <152540605432.18473.11813271279255176724.stgit@noble> In-Reply-To: <152540595840.18473.11298241115621799037.stgit@noble> References: <152540595840.18473.11298241115621799037.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 If two threads run nested_table_alloc() at the same time they could both allocate a new table. Best case is that one of them will never be freed, leaking memory. Worst case is hat entry get stored there before it leaks, and the are lost from the table. So use cmpxchg to detect the race and free the unused table. Fixes: da20420f83ea ("rhashtable: Add nested tables") Cc: stable@vger.kernel.org # 4.11+ Signed-off-by: NeilBrown --- lib/rhashtable.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/rhashtable.c b/lib/rhashtable.c index b73afe1dec7e..114e6090228a 100644 --- a/lib/rhashtable.c +++ b/lib/rhashtable.c @@ -119,6 +119,7 @@ static union nested_table *nested_table_alloc(struct rhashtable *ht, unsigned int nhash) { union nested_table *ntbl; + union nested_table *tmp; int i; ntbl = rcu_dereference(*prev); @@ -133,9 +134,12 @@ static union nested_table *nested_table_alloc(struct rhashtable *ht, (i << shifted) | nhash); } - rcu_assign_pointer(*prev, ntbl); - - return ntbl; + rcu_assign_pointer(tmp, ntbl); + if (cmpxchg(prev, NULL, tmp) == NULL) + return tmp; + /* Raced with another thread. */ + kfree(ntbl); + return rcu_dereference(*prev); } static struct bucket_table *nested_bucket_table_alloc(struct rhashtable *ht,