Commit 342100d9 authored by Daniel Borkmann's avatar Daniel Borkmann Committed by David S. Miller

rhashtable: don't test for shrink on insert, expansion on delete

Restore pre 54c5b7d3 behaviour and only probe for expansions on inserts
and shrinks on deletes. Currently, it will happen that on initial inserts
into a sparse hash table, we may i.e. shrink it first simply because it's
not fully populated yet, only to later realize that we need to grow again.

This however is counter intuitive, e.g. an initial default size of 64
elements is already small enough, and in case an elements size hint is given
to the hash table by a user, we should avoid unnecessary expansion steps,
so a shrink is clearly unintended here.

Fixes: 54c5b7d3 ("rhashtable: introduce rhashtable_wakeup_worker helper function")
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Cc: Ying Xue <ying.xue@windriver.com>
Acked-by: default avatarThomas Graf <tgraf@suug.ch>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent ee922598
...@@ -537,16 +537,25 @@ static void rht_deferred_worker(struct work_struct *work) ...@@ -537,16 +537,25 @@ static void rht_deferred_worker(struct work_struct *work)
mutex_unlock(&ht->mutex); mutex_unlock(&ht->mutex);
} }
static void rhashtable_wakeup_worker(struct rhashtable *ht) static void rhashtable_probe_expand(struct rhashtable *ht)
{ {
struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht); const struct bucket_table *new_tbl = rht_dereference_rcu(ht->future_tbl, ht);
struct bucket_table *new_tbl = rht_dereference_rcu(ht->future_tbl, ht); const struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht);
size_t size = tbl->size;
/* Only adjust the table if no resizing is currently in progress. */ /* Only adjust the table if no resizing is currently in progress. */
if (tbl == new_tbl && if (tbl == new_tbl && ht->p.grow_decision &&
((ht->p.grow_decision && ht->p.grow_decision(ht, size)) || ht->p.grow_decision(ht, tbl->size))
(ht->p.shrink_decision && ht->p.shrink_decision(ht, size)))) schedule_work(&ht->run_work);
}
static void rhashtable_probe_shrink(struct rhashtable *ht)
{
const struct bucket_table *new_tbl = rht_dereference_rcu(ht->future_tbl, ht);
const struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht);
/* Only adjust the table if no resizing is currently in progress. */
if (tbl == new_tbl && ht->p.shrink_decision &&
ht->p.shrink_decision(ht, tbl->size))
schedule_work(&ht->run_work); schedule_work(&ht->run_work);
} }
...@@ -569,7 +578,7 @@ static void __rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj, ...@@ -569,7 +578,7 @@ static void __rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj,
atomic_inc(&ht->nelems); atomic_inc(&ht->nelems);
rhashtable_wakeup_worker(ht); rhashtable_probe_expand(ht);
} }
/** /**
...@@ -682,7 +691,7 @@ bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *obj) ...@@ -682,7 +691,7 @@ bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *obj)
if (ret) { if (ret) {
atomic_dec(&ht->nelems); atomic_dec(&ht->nelems);
rhashtable_wakeup_worker(ht); rhashtable_probe_shrink(ht);
} }
rcu_read_unlock(); rcu_read_unlock();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment