Skip to content

Commit 82208d0

Browse files
NeilBrowndavem330
authored andcommitted
rhashtable: detect when object movement between tables might have invalidated a lookup
Some users of rhashtables might need to move an object from one table to another - this appears to be the reason for the incomplete usage of NULLS markers. To support these, we store a unique NULLS_MARKER at the end of each chain, and when a search fails to find a match, we check if the NULLS marker found was the expected one. If not, the search may not have examined all objects in the target bucket, so it is repeated. The unique NULLS_MARKER is derived from the address of the head of the chain. As this cannot be derived at load-time the static rhnull in rht_bucket_nested() needs to be initialised at run time. Any caller of a lookup function must still be prepared for the possibility that the object returned is in a different table - it might have been there for some time. Note that this does NOT provide support for other uses of NULLS_MARKERs such as allocating with SLAB_TYPESAFE_BY_RCU or changing the key of an object and re-inserting it in the same table. These could only be done safely if new objects were inserted at the *start* of a hash chain, and that is not currently the case. Signed-off-by: NeilBrown <neilb@suse.com> Acked-by: Herbert Xu <herbert@gondor.apana.org.au> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 77ac327 commit 82208d0

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

include/linux/rhashtable.h

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,19 @@ struct bucket_table {
7575
struct rhash_head __rcu *buckets[] ____cacheline_aligned_in_smp;
7676
};
7777

78+
/*
79+
* NULLS_MARKER() expects a hash value with the low
80+
* bits mostly likely to be significant, and it discards
81+
* the msb.
82+
* We git it an address, in which the bottom 2 bits are
83+
* always 0, and the msb might be significant.
84+
* So we shift the address down one bit to align with
85+
* expectations and avoid losing a significant bit.
86+
*/
87+
#define RHT_NULLS_MARKER(ptr) \
88+
((void *)NULLS_MARKER(((unsigned long) (ptr)) >> 1))
7889
#define INIT_RHT_NULLS_HEAD(ptr) \
79-
((ptr) = (typeof(ptr)) NULLS_MARKER(0))
90+
((ptr) = RHT_NULLS_MARKER(&(ptr)))
8091

8192
static inline bool rht_is_a_nulls(const struct rhash_head *ptr)
8293
{
@@ -471,20 +482,27 @@ static inline struct rhash_head *__rhashtable_lookup(
471482
.ht = ht,
472483
.key = key,
473484
};
485+
struct rhash_head __rcu * const *head;
474486
struct bucket_table *tbl;
475487
struct rhash_head *he;
476488
unsigned int hash;
477489

478490
tbl = rht_dereference_rcu(ht->tbl, ht);
479491
restart:
480492
hash = rht_key_hashfn(ht, tbl, key, params);
481-
rht_for_each_rcu(he, tbl, hash) {
482-
if (params.obj_cmpfn ?
483-
params.obj_cmpfn(&arg, rht_obj(ht, he)) :
484-
rhashtable_compare(&arg, rht_obj(ht, he)))
485-
continue;
486-
return he;
487-
}
493+
head = rht_bucket(tbl, hash);
494+
do {
495+
rht_for_each_rcu_continue(he, *head, tbl, hash) {
496+
if (params.obj_cmpfn ?
497+
params.obj_cmpfn(&arg, rht_obj(ht, he)) :
498+
rhashtable_compare(&arg, rht_obj(ht, he)))
499+
continue;
500+
return he;
501+
}
502+
/* An object might have been moved to a different hash chain,
503+
* while we walk along it - better check and retry.
504+
*/
505+
} while (he != RHT_NULLS_MARKER(head));
488506

489507
/* Ensure we see any new tables. */
490508
smp_rmb();

lib/rhashtable.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,8 +1179,7 @@ struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl,
11791179
unsigned int hash)
11801180
{
11811181
const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
1182-
static struct rhash_head __rcu *rhnull =
1183-
(struct rhash_head __rcu *)NULLS_MARKER(0);
1182+
static struct rhash_head __rcu *rhnull;
11841183
unsigned int index = hash & ((1 << tbl->nest) - 1);
11851184
unsigned int size = tbl->size >> tbl->nest;
11861185
unsigned int subhash = hash;
@@ -1198,8 +1197,11 @@ struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl,
11981197
subhash >>= shift;
11991198
}
12001199

1201-
if (!ntbl)
1200+
if (!ntbl) {
1201+
if (!rhnull)
1202+
INIT_RHT_NULLS_HEAD(rhnull);
12021203
return &rhnull;
1204+
}
12031205

12041206
return &ntbl[subhash].bucket;
12051207

0 commit comments

Comments
 (0)