Skip to content

Commit 2ce09be

Browse files
herbertxgregkh
authored andcommitted
rhashtable: Fix rhashtable_try_insert test
[ Upstream commit 9d4f8e5 ] The test on whether rhashtable_insert_one did an insertion relies on the value returned by rhashtable_lookup_one. Unfortunately that value is overwritten after rhashtable_insert_one returns. Fix this by moving the test before data gets overwritten. Simplify the test as only data == NULL matters. Finally move atomic_inc back within the lock as otherwise it may be reordered with the atomic_dec on the removal side, potentially leading to an underflow. Reported-by: Michael Kelley <[email protected]> Fixes: e1d3422 ("rhashtable: Fix potential deadlock by moving schedule_work outside lock") Signed-off-by: Herbert Xu <[email protected]> Tested-by: Michael Kelley <[email protected]> Reviewed-by: Breno Leitao <[email protected]> Tested-by: Mikhail Zaslonko <[email protected]> Signed-off-by: Herbert Xu <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent a54091c commit 2ce09be

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

lib/rhashtable.c

+7-5
Original file line numberDiff line numberDiff line change
@@ -611,21 +611,23 @@ static void *rhashtable_try_insert(struct rhashtable *ht, const void *key,
611611
new_tbl = rht_dereference_rcu(tbl->future_tbl, ht);
612612
data = ERR_PTR(-EAGAIN);
613613
} else {
614+
bool inserted;
615+
614616
flags = rht_lock(tbl, bkt);
615617
data = rhashtable_lookup_one(ht, bkt, tbl,
616618
hash, key, obj);
617619
new_tbl = rhashtable_insert_one(ht, bkt, tbl,
618620
hash, obj, data);
621+
inserted = data && !new_tbl;
622+
if (inserted)
623+
atomic_inc(&ht->nelems);
619624
if (PTR_ERR(new_tbl) != -EEXIST)
620625
data = ERR_CAST(new_tbl);
621626

622627
rht_unlock(tbl, bkt, flags);
623628

624-
if (PTR_ERR(data) == -ENOENT && !new_tbl) {
625-
atomic_inc(&ht->nelems);
626-
if (rht_grow_above_75(ht, tbl))
627-
schedule_work(&ht->run_work);
628-
}
629+
if (inserted && rht_grow_above_75(ht, tbl))
630+
schedule_work(&ht->run_work);
629631
}
630632
} while (!IS_ERR_OR_NULL(new_tbl));
631633

0 commit comments

Comments
 (0)