Skip to content

Commit

Permalink
rhashtable: involve rhashtable_lookup_compare_insert routine
Browse files Browse the repository at this point in the history
Introduce a new function called rhashtable_lookup_compare_insert()
which is very similar to rhashtable_lookup_insert(). But the former
makes use of users' given compare function to look for an object,
and then inserts it into hash table if found. As the entire process
of search and insertion is under protection of per bucket lock, this
can help users to avoid the involvement of extra lock.

Signed-off-by: Ying Xue <[email protected]>
Cc: Thomas Graf <[email protected]>
Acked-by: Thomas Graf <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
  • Loading branch information
ying-xue authored and davem330 committed Jan 13, 2015
1 parent d2c60b1 commit 7a868d1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
5 changes: 5 additions & 0 deletions include/linux/rhashtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,12 @@ int rhashtable_shrink(struct rhashtable *ht);
void *rhashtable_lookup(struct rhashtable *ht, const void *key);
void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key,
bool (*compare)(void *, void *), void *arg);

bool rhashtable_lookup_insert(struct rhashtable *ht, struct rhash_head *obj);
bool rhashtable_lookup_compare_insert(struct rhashtable *ht,
struct rhash_head *obj,
bool (*compare)(void *, void *),
void *arg);

void rhashtable_destroy(struct rhashtable *ht);

Expand Down
42 changes: 40 additions & 2 deletions lib/rhashtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,43 @@ EXPORT_SYMBOL_GPL(rhashtable_lookup_compare);
* to rhashtable_init().
*/
bool rhashtable_lookup_insert(struct rhashtable *ht, struct rhash_head *obj)
{
struct rhashtable_compare_arg arg = {
.ht = ht,
.key = rht_obj(ht, obj) + ht->p.key_offset,
};

BUG_ON(!ht->p.key_len);

return rhashtable_lookup_compare_insert(ht, obj, &rhashtable_compare,
&arg);
}
EXPORT_SYMBOL_GPL(rhashtable_lookup_insert);

/**
* rhashtable_lookup_compare_insert - search and insert object to hash table
* with compare function
* @ht: hash table
* @obj: pointer to hash head inside object
* @compare: compare function, must return true on match
* @arg: argument passed on to compare function
*
* Locks down the bucket chain in both the old and new table if a resize
* is in progress to ensure that writers can't remove from the old table
* and can't insert to the new table during the atomic operation of search
* and insertion. Searches for duplicates in both the old and new table if
* a resize is in progress.
*
* Lookups may occur in parallel with hashtable mutations and resizing.
*
* Will trigger an automatic deferred table resizing if the size grows
* beyond the watermark indicated by grow_decision() which can be passed
* to rhashtable_init().
*/
bool rhashtable_lookup_compare_insert(struct rhashtable *ht,
struct rhash_head *obj,
bool (*compare)(void *, void *),
void *arg)
{
struct bucket_table *new_tbl, *old_tbl;
spinlock_t *new_bucket_lock, *old_bucket_lock;
Expand All @@ -747,7 +784,8 @@ bool rhashtable_lookup_insert(struct rhashtable *ht, struct rhash_head *obj)
if (unlikely(old_tbl != new_tbl))
spin_lock_bh_nested(new_bucket_lock, RHT_LOCK_NESTED);

if (rhashtable_lookup(ht, rht_obj(ht, obj) + ht->p.key_offset)) {
if (rhashtable_lookup_compare(ht, rht_obj(ht, obj) + ht->p.key_offset,
compare, arg)) {
success = false;
goto exit;
}
Expand All @@ -763,7 +801,7 @@ bool rhashtable_lookup_insert(struct rhashtable *ht, struct rhash_head *obj)

return success;
}
EXPORT_SYMBOL_GPL(rhashtable_lookup_insert);
EXPORT_SYMBOL_GPL(rhashtable_lookup_compare_insert);

static size_t rounded_hashtable_size(struct rhashtable_params *params)
{
Expand Down

0 comments on commit 7a868d1

Please sign in to comment.