Skip to content

Commit c0c09bf

Browse files
ying-xuedavem330
authored andcommitted
rhashtable: avoid unnecessary wakeup for worker queue
Move condition statements of verifying whether hash table size exceeds its maximum threshold or reaches its minimum threshold from resizing functions to resizing decision functions, avoiding unnecessary wakeup for worker queue thread. 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]>
1 parent bd6d4db commit c0c09bf

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

include/linux/rhashtable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ struct rhashtable {
113113
struct bucket_table __rcu *tbl;
114114
struct bucket_table __rcu *future_tbl;
115115
atomic_t nelems;
116-
size_t shift;
116+
atomic_t shift;
117117
struct rhashtable_params p;
118118
struct delayed_work run_work;
119119
struct mutex mutex;

lib/rhashtable.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@ static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
199199
bool rht_grow_above_75(const struct rhashtable *ht, size_t new_size)
200200
{
201201
/* Expand table when exceeding 75% load */
202-
return atomic_read(&ht->nelems) > (new_size / 4 * 3);
202+
return atomic_read(&ht->nelems) > (new_size / 4 * 3) &&
203+
(ht->p.max_shift && atomic_read(&ht->shift) < ht->p.max_shift);
203204
}
204205
EXPORT_SYMBOL_GPL(rht_grow_above_75);
205206

@@ -211,7 +212,8 @@ EXPORT_SYMBOL_GPL(rht_grow_above_75);
211212
bool rht_shrink_below_30(const struct rhashtable *ht, size_t new_size)
212213
{
213214
/* Shrink table beneath 30% load */
214-
return atomic_read(&ht->nelems) < (new_size * 3 / 10);
215+
return atomic_read(&ht->nelems) < (new_size * 3 / 10) &&
216+
(atomic_read(&ht->shift) > ht->p.min_shift);
215217
}
216218
EXPORT_SYMBOL_GPL(rht_shrink_below_30);
217219

@@ -318,14 +320,11 @@ int rhashtable_expand(struct rhashtable *ht)
318320

319321
ASSERT_RHT_MUTEX(ht);
320322

321-
if (ht->p.max_shift && ht->shift >= ht->p.max_shift)
322-
return 0;
323-
324323
new_tbl = bucket_table_alloc(ht, old_tbl->size * 2);
325324
if (new_tbl == NULL)
326325
return -ENOMEM;
327326

328-
ht->shift++;
327+
atomic_inc(&ht->shift);
329328

330329
/* Make insertions go into the new, empty table right away. Deletions
331330
* and lookups will be attempted in both tables until we synchronize.
@@ -421,9 +420,6 @@ int rhashtable_shrink(struct rhashtable *ht)
421420

422421
ASSERT_RHT_MUTEX(ht);
423422

424-
if (ht->shift <= ht->p.min_shift)
425-
return 0;
426-
427423
new_tbl = bucket_table_alloc(ht, tbl->size / 2);
428424
if (new_tbl == NULL)
429425
return -ENOMEM;
@@ -462,7 +458,7 @@ int rhashtable_shrink(struct rhashtable *ht)
462458

463459
/* Publish the new, valid hash table */
464460
rcu_assign_pointer(ht->tbl, new_tbl);
465-
ht->shift--;
461+
atomic_dec(&ht->shift);
466462

467463
/* Wait for readers. No new readers will have references to the
468464
* old hash table.
@@ -851,7 +847,7 @@ int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params)
851847
if (tbl == NULL)
852848
return -ENOMEM;
853849

854-
ht->shift = ilog2(tbl->size);
850+
atomic_set(&ht->shift, ilog2(tbl->size));
855851
RCU_INIT_POINTER(ht->tbl, tbl);
856852
RCU_INIT_POINTER(ht->future_tbl, tbl);
857853

0 commit comments

Comments
 (0)