[PATCH 4/8] rhashtable: fix race in nested_table_alloc()

From: NeilBrown
Date: Thu May 03 2018 - 23:55:11 EST


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@xxxxxxxxxxxxxxx # 4.11+
Signed-off-by: NeilBrown <neilb@xxxxxxxx>
---
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,