Skip to content

Commit 440896a

Browse files
herbertxmehmetb0
authored andcommitted
rhashtable: Fix rhashtable_try_insert test
BugLink: https://bugs.launchpad.net/bugs/2103829 [ Upstream commit 9d4f8e54cef2c42e23ef258833dbd06a1eaff89b ] 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 <mhklinux@outlook.com> Fixes: e1d3422c95f0 ("rhashtable: Fix potential deadlock by moving schedule_work outside lock") Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> Tested-by: Michael Kelley <mhklinux@outlook.com> Reviewed-by: Breno Leitao <leitao@debian.org> Tested-by: Mikhail Zaslonko <zaslonko@linux.ibm.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Noah Wager <noah.wager@canonical.com> Signed-off-by: Mehmet Basaran <mehmet.basaran@canonical.com>
1 parent cec15d0 commit 440896a

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

lib/rhashtable.c

Lines changed: 7 additions & 5 deletions
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)