diff mbox

[net-next] rhashtable: Fix race between rehashing and readers

Message ID 20150312163348.GD5378@casper.infradead.org
State Rejected, archived
Delegated to: David Miller
Headers show

Commit Message

Thomas Graf March 12, 2015, 4:33 p.m. UTC
Rehashing can occur in parallel to readers. Readers are aware of this
situation so new readers are allowed while the rehashing is in
process. Existing readers may have already fetched the tbl and
future_tbl and may thus assume no rehashing is in progress. The
rehashing must thus wait for all existing readers to complete after
publihsing the future_tbl to make sure all parallel readers see the
table before we start moving entries.

Fixes: aa34a6cb0478 ("rhashtable: Add arbitrary rehash function")
Signed-off-by: Thomas Graf <tgraf@suug.ch>
---
Herbert: This is what I think is needed to fix this properly.

 lib/rhashtable.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

Comments

Herbert Xu March 12, 2015, 9:50 p.m. UTC | #1
On Thu, Mar 12, 2015 at 04:33:48PM +0000, Thomas Graf wrote:
> Rehashing can occur in parallel to readers. Readers are aware of this
> situation so new readers are allowed while the rehashing is in
> process. Existing readers may have already fetched the tbl and
> future_tbl and may thus assume no rehashing is in progress. The
> rehashing must thus wait for all existing readers to complete after
> publihsing the future_tbl to make sure all parallel readers see the
> table before we start moving entries.
> 
> Fixes: aa34a6cb0478 ("rhashtable: Add arbitrary rehash function")
> Signed-off-by: Thomas Graf <tgraf@suug.ch>
> ---
> Herbert: This is what I think is needed to fix this properly.

Nack.  Please see my previous email.
diff mbox

Patch

diff --git a/lib/rhashtable.c b/lib/rhashtable.c
index d7f3db5..624cf59 100644
--- a/lib/rhashtable.c
+++ b/lib/rhashtable.c
@@ -275,10 +275,12 @@  static void rhashtable_rehash(struct rhashtable *ht,
 
 	/* Make insertions go into the new, empty table right away. Deletions
 	 * and lookups will be attempted in both tables until we synchronize.
-	 * The synchronize_rcu() guarantees for the new table to be picked up
-	 * so no new additions go into the old table while we relink.
+	 * The synchronize_rcu() guarantees that readers have either completed
+	 * or are aware of both the new and old table before we start moving
+	 * entries to the new table.
 	 */
 	rcu_assign_pointer(ht->future_tbl, new_tbl);
+	synchronize_rcu();
 
 	for (old_hash = 0; old_hash < old_tbl->size; old_hash++)
 		rhashtable_rehash_chain(ht, old_hash);