History

rhashtable: don't hold lock on first table throughout insertion.

rhashtable_try_insert() currently holds a lock on the bucket in the first table, while also locking buckets in subsequent tables. This is unnecessary and looks like a hold-over from some earlier version of the implementation. As insert and remove always lock a bucket in each table in turn, and as insert only inserts in the final table, there cannot be any races that are not covered by simply locking a bucket in each table in turn. When an insert call reaches that last table it can be sure that there is no matchinf entry in any other table as it has searched them all, and insertion never happens anywhere but in the last table. The fact that code tests for the existence of future_tbl while holding a lock on the relevant bucket ensures that two threads inserting the same key will make compatible decisions about which is the "last" table. This simplifies the code and allows the ->rehash field to be discarded. We still need a way to ensure that a dead bucket_table is never re-linked by rhashtable_walk_stop(). This can be achieved by calling call_rcu() inside the locked region, and checking with rcu_head_after_call_rcu() in rhashtable_walk_stop() to see if the bucket table is empty and dead.

NeilBrown committed 7 years ago

Commit 4feb7c71 parent 83b038d2 files · +16 −49Browse files

Showing the changes to include/linux (1 of 2 files). Show all

Unified · Split

include/linux/rhashtable.h+0−13
@@ -63,7 +63,6 @@ · 62 unmodified lines
6363 struct bucket_table {
6464 unsigned int size;
6565 unsigned int nest;
66− unsigned int rehash;
6766 u32 hash_rnd;
6867 unsigned int locks_mask;
6968 spinlock_t *locks;
@@ -776,12 +775,6 @@ static inline int rhltable_insert( · 706 unmodified lines
776775 * @obj: pointer to hash head inside object
777776 * @params: hash table parameters
778777 *
779− * Locks down the bucket chain in both the old and new table if a resize
780− * is in progress to ensure that writers can't remove from the old table
781− * and can't insert to the new table during the atomic operation of search
782− * and insertion. Searches for duplicates in both the old and new table if
783− * a resize is in progress.
784− *
785778 * This lookup function may only be used for fixed key hash table (key_len
786779 * parameter set). It will BUG() if used inappropriately.
787780 *
@@ -837,12 +830,6 @@ static inline void *rhashtable_lookup_get_insert_fast( · 49 unmodified lines
837830 * @obj: pointer to hash head inside object
838831 * @params: hash table parameters
839832 *
840− * Locks down the bucket chain in both the old and new table if a resize
841− * is in progress to ensure that writers can't remove from the old table
842− * and can't insert to the new table during the atomic operation of search
843− * and insertion. Searches for duplicates in both the old and new table if
844− * a resize is in progress.
845− *
846833 * Lookups may occur in parallel with hashtable mutations and resizing.
847834 *
848835 * Will trigger an automatic deferred table resizing if residency in the

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t