From mboxrd@z Thu Jan 1 00:00:00 1970 From: NeilBrown Subject: [PATCH 1/5] rhashtable: use cmpxchg() in nested_table_alloc() Date: Fri, 06 Jul 2018 17:22:30 +1000 Message-ID: <153086175005.24852.16109495489833855391.stgit@noble> References: <153086169828.24852.10332573315056854948.stgit@noble> Mime-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org To: Thomas Graf , Herbert Xu Return-path: In-Reply-To: <153086169828.24852.10332573315056854948.stgit@noble> Sender: linux-kernel-owner@vger.kernel.org List-Id: netdev.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,