Skip to content

Commit ff302db

Browse files
NeilBrowndavem330
authored andcommitted
rhashtable: allow rht_bucket_var to return NULL.
Rather than returning a pointer to a static nulls, rht_bucket_var() now returns NULL if the bucket doesn't exist. This will make the next patch, which stores a bitlock in the bucket pointer, somewhat cleaner. This change involves introducing __rht_bucket_nested() which is like rht_bucket_nested(), but doesn't provide the static nulls, and changing rht_bucket_nested() to call this and possible provide a static nulls - as is still needed for the non-var case. Signed-off-by: NeilBrown <neilb@suse.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 7a41c29 commit ff302db

2 files changed

Lines changed: 29 additions & 11 deletions

File tree

‎include/linux/rhashtable.h‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,8 @@ void rhashtable_destroy(struct rhashtable *ht);
265265

266266
struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl,
267267
unsigned int hash);
268+
struct rhash_head __rcu **__rht_bucket_nested(const struct bucket_table *tbl,
269+
unsigned int hash);
268270
struct rhash_head __rcu **rht_bucket_nested_insert(struct rhashtable *ht,
269271
struct bucket_table *tbl,
270272
unsigned int hash);
@@ -294,7 +296,7 @@ static inline struct rhash_head __rcu *const *rht_bucket(
294296
static inline struct rhash_head __rcu **rht_bucket_var(
295297
struct bucket_table *tbl, unsigned int hash)
296298
{
297-
return unlikely(tbl->nest) ? rht_bucket_nested(tbl, hash) :
299+
return unlikely(tbl->nest) ? __rht_bucket_nested(tbl, hash) :
298300
&tbl->buckets[hash];
299301
}
300302

@@ -890,6 +892,8 @@ static inline int __rhashtable_remove_fast_one(
890892
spin_lock_bh(lock);
891893

892894
pprev = rht_bucket_var(tbl, hash);
895+
if (!pprev)
896+
goto out;
893897
rht_for_each_from(he, *pprev, tbl, hash) {
894898
struct rhlist_head *list;
895899

@@ -934,6 +938,7 @@ static inline int __rhashtable_remove_fast_one(
934938
break;
935939
}
936940

941+
out:
937942
spin_unlock_bh(lock);
938943

939944
if (err > 0) {
@@ -1042,6 +1047,8 @@ static inline int __rhashtable_replace_fast(
10421047
spin_lock_bh(lock);
10431048

10441049
pprev = rht_bucket_var(tbl, hash);
1050+
if (!pprev)
1051+
goto out;
10451052
rht_for_each_from(he, *pprev, tbl, hash) {
10461053
if (he != obj_old) {
10471054
pprev = &he->next;
@@ -1053,7 +1060,7 @@ static inline int __rhashtable_replace_fast(
10531060
err = 0;
10541061
break;
10551062
}
1056-
1063+
out:
10571064
spin_unlock_bh(lock);
10581065

10591066
return err;

‎lib/rhashtable.c‎

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,10 @@ static int rhashtable_rehash_one(struct rhashtable *ht, unsigned int old_hash)
237237
goto out;
238238

239239
err = -ENOENT;
240+
if (!pprev)
241+
goto out;
240242

241-
rht_for_each(entry, old_tbl, old_hash) {
243+
rht_for_each_from(entry, *pprev, old_tbl, old_hash) {
242244
err = 0;
243245
next = rht_dereference_bucket(entry->next, old_tbl, old_hash);
244246

@@ -496,6 +498,8 @@ static void *rhashtable_lookup_one(struct rhashtable *ht,
496498

497499
elasticity = RHT_ELASTICITY;
498500
pprev = rht_bucket_var(tbl, hash);
501+
if (!pprev)
502+
return ERR_PTR(-ENOENT);
499503
rht_for_each_from(head, *pprev, tbl, hash) {
500504
struct rhlist_head *list;
501505
struct rhlist_head *plist;
@@ -1161,11 +1165,10 @@ void rhashtable_destroy(struct rhashtable *ht)
11611165
}
11621166
EXPORT_SYMBOL_GPL(rhashtable_destroy);
11631167

1164-
struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl,
1165-
unsigned int hash)
1168+
struct rhash_head __rcu **__rht_bucket_nested(const struct bucket_table *tbl,
1169+
unsigned int hash)
11661170
{
11671171
const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
1168-
static struct rhash_head __rcu *rhnull;
11691172
unsigned int index = hash & ((1 << tbl->nest) - 1);
11701173
unsigned int size = tbl->size >> tbl->nest;
11711174
unsigned int subhash = hash;
@@ -1183,15 +1186,23 @@ struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl,
11831186
subhash >>= shift;
11841187
}
11851188

1186-
if (!ntbl) {
1187-
if (!rhnull)
1188-
INIT_RHT_NULLS_HEAD(rhnull);
1189-
return &rhnull;
1190-
}
1189+
if (!ntbl)
1190+
return NULL;
11911191

11921192
return &ntbl[subhash].bucket;
11931193

11941194
}
1195+
EXPORT_SYMBOL_GPL(__rht_bucket_nested);
1196+
1197+
struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl,
1198+
unsigned int hash)
1199+
{
1200+
static struct rhash_head __rcu *rhnull;
1201+
1202+
if (!rhnull)
1203+
INIT_RHT_NULLS_HEAD(rhnull);
1204+
return __rht_bucket_nested(tbl, hash) ?: &rhnull;
1205+
}
11951206
EXPORT_SYMBOL_GPL(rht_bucket_nested);
11961207

11971208
struct rhash_head __rcu **rht_bucket_nested_insert(struct rhashtable *ht,

0 commit comments

Comments
 (0)