Skip to content

Commit

Permalink
rhashtable: Fix reader/rehash race
Browse files Browse the repository at this point in the history
There is a potential race condition between readers and the rehasher.
In particular, the rehasher could have started a rehash while the
reader finishes a scan of the old table but fails to see the new
table pointer.

This patch closes this window by adding smp_wmb/smp_rmb.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Herbert Xu authored and David S. Miller committed Mar 13, 2015
1 parent 5ff0d16 commit 9497df8
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/rhashtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,9 @@ static void rhashtable_rehash(struct rhashtable *ht,
*/
rcu_assign_pointer(ht->future_tbl, new_tbl);

/* Ensure the new table is visible to readers. */
smp_wmb();

for (old_hash = 0; old_hash < old_tbl->size; old_hash++)
rhashtable_rehash_chain(ht, old_hash);

Expand Down Expand Up @@ -618,6 +621,9 @@ void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key,
return rht_obj(ht, he);
}

/* Ensure we see any new tables. */
smp_rmb();

old_tbl = tbl;
tbl = rht_dereference_rcu(ht->future_tbl, ht);
if (unlikely(tbl != old_tbl))
Expand Down

0 comments on commit 9497df8

Please sign in to comment.