block: mark bio_add_folio as __must_check
[linux-block.git] / lib / rhashtable.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
7e1e7763
TG
2/*
3 * Resizable, Scalable, Concurrent Hash Table
4 *
02fd97c3 5 * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
a5ec68e3 6 * Copyright (c) 2014-2015 Thomas Graf <tgraf@suug.ch>
7e1e7763
TG
7 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
8 *
7e1e7763 9 * Code partially derived from nft_hash
02fd97c3
HX
10 * Rewritten with rehash code from br_multicast plus single list
11 * pointer as suggested by Josh Triplett
7e1e7763
TG
12 */
13
07ee0722 14#include <linux/atomic.h>
7e1e7763
TG
15#include <linux/kernel.h>
16#include <linux/init.h>
17#include <linux/log2.h>
5beb5c90 18#include <linux/sched.h>
b2d09103 19#include <linux/rculist.h>
7e1e7763
TG
20#include <linux/slab.h>
21#include <linux/vmalloc.h>
22#include <linux/mm.h>
87545899 23#include <linux/jhash.h>
7e1e7763
TG
24#include <linux/random.h>
25#include <linux/rhashtable.h>
61d7b097 26#include <linux/err.h>
6d795413 27#include <linux/export.h>
7e1e7763
TG
28
29#define HASH_DEFAULT_SIZE 64UL
c2e213cf 30#define HASH_MIN_SIZE 4U
97defe1e 31
da20420f
HX
32union nested_table {
33 union nested_table __rcu *table;
ce9b362b 34 struct rhash_lock_head __rcu *bucket;
da20420f
HX
35};
36
988dfbd7 37static u32 head_hashfn(struct rhashtable *ht,
8d24c0b4
TG
38 const struct bucket_table *tbl,
39 const struct rhash_head *he)
7e1e7763 40{
02fd97c3 41 return rht_head_hashfn(ht, tbl, he, ht->p);
7e1e7763
TG
42}
43
a03eaec0 44#ifdef CONFIG_PROVE_LOCKING
a03eaec0 45#define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
a03eaec0
TG
46
47int lockdep_rht_mutex_is_held(struct rhashtable *ht)
48{
49 return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1;
50}
51EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held);
52
53int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash)
54{
8f0db018
N
55 if (!debug_locks)
56 return 1;
57 if (unlikely(tbl->nest))
58 return 1;
ca0b709d 59 return bit_spin_is_locked(0, (unsigned long *)&tbl->buckets[hash]);
a03eaec0
TG
60}
61EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held);
62#else
63#define ASSERT_RHT_MUTEX(HT)
a03eaec0
TG
64#endif
65
4a3084aa
HX
66static inline union nested_table *nested_table_top(
67 const struct bucket_table *tbl)
68{
69 /* The top-level bucket entry does not need RCU protection
70 * because it's set at the same time as tbl->nest.
71 */
72 return (void *)rcu_dereference_protected(tbl->buckets[0], 1);
73}
74
da20420f
HX
75static void nested_table_free(union nested_table *ntbl, unsigned int size)
76{
77 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
78 const unsigned int len = 1 << shift;
79 unsigned int i;
80
4a3084aa 81 ntbl = rcu_dereference_protected(ntbl->table, 1);
da20420f
HX
82 if (!ntbl)
83 return;
84
85 if (size > len) {
86 size >>= shift;
87 for (i = 0; i < len; i++)
88 nested_table_free(ntbl + i, size);
89 }
90
91 kfree(ntbl);
92}
93
94static void nested_bucket_table_free(const struct bucket_table *tbl)
95{
96 unsigned int size = tbl->size >> tbl->nest;
97 unsigned int len = 1 << tbl->nest;
98 union nested_table *ntbl;
99 unsigned int i;
100
4a3084aa 101 ntbl = nested_table_top(tbl);
da20420f
HX
102
103 for (i = 0; i < len; i++)
104 nested_table_free(ntbl + i, size);
105
106 kfree(ntbl);
107}
108
97defe1e
TG
109static void bucket_table_free(const struct bucket_table *tbl)
110{
da20420f
HX
111 if (tbl->nest)
112 nested_bucket_table_free(tbl);
113
97defe1e
TG
114 kvfree(tbl);
115}
116
9d901bc0
HX
117static void bucket_table_free_rcu(struct rcu_head *head)
118{
119 bucket_table_free(container_of(head, struct bucket_table, rcu));
120}
121
da20420f
HX
122static union nested_table *nested_table_alloc(struct rhashtable *ht,
123 union nested_table __rcu **prev,
5af68ef7 124 bool leaf)
da20420f
HX
125{
126 union nested_table *ntbl;
127 int i;
128
129 ntbl = rcu_dereference(*prev);
130 if (ntbl)
131 return ntbl;
132
133 ntbl = kzalloc(PAGE_SIZE, GFP_ATOMIC);
134
5af68ef7
N
135 if (ntbl && leaf) {
136 for (i = 0; i < PAGE_SIZE / sizeof(ntbl[0]); i++)
9b4f64a2 137 INIT_RHT_NULLS_HEAD(ntbl[i].bucket);
da20420f
HX
138 }
139
e9458a4e 140 if (cmpxchg((union nested_table **)prev, NULL, ntbl) == NULL)
7a41c294
N
141 return ntbl;
142 /* Raced with another thread. */
143 kfree(ntbl);
144 return rcu_dereference(*prev);
da20420f
HX
145}
146
147static struct bucket_table *nested_bucket_table_alloc(struct rhashtable *ht,
148 size_t nbuckets,
149 gfp_t gfp)
150{
151 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
152 struct bucket_table *tbl;
153 size_t size;
154
155 if (nbuckets < (1 << (shift + 1)))
156 return NULL;
157
158 size = sizeof(*tbl) + sizeof(tbl->buckets[0]);
159
160 tbl = kzalloc(size, gfp);
161 if (!tbl)
162 return NULL;
163
164 if (!nested_table_alloc(ht, (union nested_table __rcu **)tbl->buckets,
5af68ef7 165 false)) {
da20420f
HX
166 kfree(tbl);
167 return NULL;
168 }
169
170 tbl->nest = (ilog2(nbuckets) - 1) % shift + 1;
171
172 return tbl;
173}
174
97defe1e 175static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
b9ecfdaa
HX
176 size_t nbuckets,
177 gfp_t gfp)
7e1e7763 178{
eb6d1abf 179 struct bucket_table *tbl = NULL;
8f0db018 180 size_t size;
f89bd6f8 181 int i;
149212f0 182 static struct lock_class_key __key;
7e1e7763 183
c252aa3e 184 tbl = kvzalloc(struct_size(tbl, buckets, nbuckets), gfp);
da20420f
HX
185
186 size = nbuckets;
187
2d22ecf6 188 if (tbl == NULL && (gfp & ~__GFP_NOFAIL) != GFP_KERNEL) {
da20420f
HX
189 tbl = nested_bucket_table_alloc(ht, nbuckets, gfp);
190 nbuckets = 0;
191 }
2d22ecf6 192
7e1e7763
TG
193 if (tbl == NULL)
194 return NULL;
195
149212f0
N
196 lockdep_init_map(&tbl->dep_map, "rhashtable_bucket", &__key, 0);
197
da20420f 198 tbl->size = size;
7e1e7763 199
4feb7c7a 200 rcu_head_init(&tbl->rcu);
eddee5ba
HX
201 INIT_LIST_HEAD(&tbl->walkers);
202
d48ad080 203 tbl->hash_rnd = get_random_u32();
5269b53d 204
f89bd6f8 205 for (i = 0; i < nbuckets; i++)
9b4f64a2 206 INIT_RHT_NULLS_HEAD(tbl->buckets[i]);
f89bd6f8 207
97defe1e 208 return tbl;
7e1e7763
TG
209}
210
b824478b
HX
211static struct bucket_table *rhashtable_last_table(struct rhashtable *ht,
212 struct bucket_table *tbl)
213{
214 struct bucket_table *new_tbl;
215
216 do {
217 new_tbl = tbl;
218 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
219 } while (tbl);
220
221 return new_tbl;
222}
223
8f0db018 224static int rhashtable_rehash_one(struct rhashtable *ht,
ce9b362b 225 struct rhash_lock_head __rcu **bkt,
8f0db018 226 unsigned int old_hash)
a5ec68e3 227{
aa34a6cb 228 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
c0690016 229 struct bucket_table *new_tbl = rhashtable_last_table(ht, old_tbl);
da20420f 230 int err = -EAGAIN;
aa34a6cb 231 struct rhash_head *head, *next, *entry;
e4edbe3c 232 struct rhash_head __rcu **pprev = NULL;
299e5c32 233 unsigned int new_hash;
e47877c7 234 unsigned long flags;
aa34a6cb 235
da20420f
HX
236 if (new_tbl->nest)
237 goto out;
238
239 err = -ENOENT;
240
adc6a3ab
N
241 rht_for_each_from(entry, rht_ptr(bkt, old_tbl, old_hash),
242 old_tbl, old_hash) {
aa34a6cb
HX
243 err = 0;
244 next = rht_dereference_bucket(entry->next, old_tbl, old_hash);
245
246 if (rht_is_a_nulls(next))
247 break;
a5ec68e3 248
aa34a6cb
HX
249 pprev = &entry->next;
250 }
a5ec68e3 251
aa34a6cb
HX
252 if (err)
253 goto out;
97defe1e 254
aa34a6cb 255 new_hash = head_hashfn(ht, new_tbl, entry);
7e1e7763 256
e47877c7
TH
257 flags = rht_lock_nested(new_tbl, &new_tbl->buckets[new_hash],
258 SINGLE_DEPTH_NESTING);
7e1e7763 259
adc6a3ab 260 head = rht_ptr(new_tbl->buckets + new_hash, new_tbl, new_hash);
97defe1e 261
7def0f95 262 RCU_INIT_POINTER(entry->next, head);
a5ec68e3 263
e47877c7 264 rht_assign_unlock(new_tbl, &new_tbl->buckets[new_hash], entry, flags);
97defe1e 265
8f0db018
N
266 if (pprev)
267 rcu_assign_pointer(*pprev, next);
268 else
269 /* Need to preserved the bit lock. */
f4712b46 270 rht_assign_locked(bkt, next);
7e1e7763 271
aa34a6cb
HX
272out:
273 return err;
274}
97defe1e 275
da20420f 276static int rhashtable_rehash_chain(struct rhashtable *ht,
299e5c32 277 unsigned int old_hash)
aa34a6cb
HX
278{
279 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
ce9b362b 280 struct rhash_lock_head __rcu **bkt = rht_bucket_var(old_tbl, old_hash);
e47877c7 281 unsigned long flags;
da20420f 282 int err;
aa34a6cb 283
8f0db018
N
284 if (!bkt)
285 return 0;
e47877c7 286 flags = rht_lock(old_tbl, bkt);
a5ec68e3 287
8f0db018 288 while (!(err = rhashtable_rehash_one(ht, bkt, old_hash)))
aa34a6cb 289 ;
da20420f 290
4feb7c7a 291 if (err == -ENOENT)
da20420f 292 err = 0;
e47877c7 293 rht_unlock(old_tbl, bkt, flags);
da20420f
HX
294
295 return err;
97defe1e
TG
296}
297
b824478b
HX
298static int rhashtable_rehash_attach(struct rhashtable *ht,
299 struct bucket_table *old_tbl,
300 struct bucket_table *new_tbl)
97defe1e 301{
aa34a6cb
HX
302 /* Make insertions go into the new, empty table right away. Deletions
303 * and lookups will be attempted in both tables until we synchronize.
0ad66449
N
304 * As cmpxchg() provides strong barriers, we do not need
305 * rcu_assign_pointer().
aa34a6cb 306 */
aa34a6cb 307
e9458a4e
HX
308 if (cmpxchg((struct bucket_table **)&old_tbl->future_tbl, NULL,
309 new_tbl) != NULL)
0ad66449 310 return -EEXIST;
b824478b
HX
311
312 return 0;
313}
314
315static int rhashtable_rehash_table(struct rhashtable *ht)
316{
317 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
318 struct bucket_table *new_tbl;
319 struct rhashtable_walker *walker;
299e5c32 320 unsigned int old_hash;
da20420f 321 int err;
b824478b
HX
322
323 new_tbl = rht_dereference(old_tbl->future_tbl, ht);
324 if (!new_tbl)
325 return 0;
326
da20420f
HX
327 for (old_hash = 0; old_hash < old_tbl->size; old_hash++) {
328 err = rhashtable_rehash_chain(ht, old_hash);
329 if (err)
330 return err;
ae6da1f5 331 cond_resched();
da20420f 332 }
aa34a6cb
HX
333
334 /* Publish the new table pointer. */
335 rcu_assign_pointer(ht->tbl, new_tbl);
336
ba7c95ea 337 spin_lock(&ht->lock);
eddee5ba
HX
338 list_for_each_entry(walker, &old_tbl->walkers, list)
339 walker->tbl = NULL;
340
aa34a6cb
HX
341 /* Wait for readers. All new readers will see the new
342 * table, and thus no references to the old table will
343 * remain.
4feb7c7a
N
344 * We do this inside the locked region so that
345 * rhashtable_walk_stop() can use rcu_head_after_call_rcu()
346 * to check if it should not re-link the table.
aa34a6cb 347 */
9d901bc0 348 call_rcu(&old_tbl->rcu, bucket_table_free_rcu);
4feb7c7a 349 spin_unlock(&ht->lock);
b824478b
HX
350
351 return rht_dereference(new_tbl->future_tbl, ht) ? -EAGAIN : 0;
7e1e7763
TG
352}
353
da20420f
HX
354static int rhashtable_rehash_alloc(struct rhashtable *ht,
355 struct bucket_table *old_tbl,
356 unsigned int size)
7e1e7763 357{
da20420f 358 struct bucket_table *new_tbl;
b824478b 359 int err;
7e1e7763
TG
360
361 ASSERT_RHT_MUTEX(ht);
362
da20420f 363 new_tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
7e1e7763
TG
364 if (new_tbl == NULL)
365 return -ENOMEM;
366
b824478b
HX
367 err = rhashtable_rehash_attach(ht, old_tbl, new_tbl);
368 if (err)
369 bucket_table_free(new_tbl);
370
371 return err;
7e1e7763 372}
7e1e7763
TG
373
374/**
375 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
376 * @ht: the hash table to shrink
7e1e7763 377 *
18093d1c
HX
378 * This function shrinks the hash table to fit, i.e., the smallest
379 * size would not cause it to expand right away automatically.
7e1e7763 380 *
97defe1e
TG
381 * The caller must ensure that no concurrent resizing occurs by holding
382 * ht->mutex.
383 *
7e1e7763
TG
384 * The caller must ensure that no concurrent table mutations take place.
385 * It is however valid to have concurrent lookups if they are RCU protected.
97defe1e
TG
386 *
387 * It is valid to have concurrent insertions and deletions protected by per
388 * bucket locks or concurrent RCU protected lookups and traversals.
7e1e7763 389 */
b824478b 390static int rhashtable_shrink(struct rhashtable *ht)
7e1e7763 391{
da20420f 392 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
12311959
VN
393 unsigned int nelems = atomic_read(&ht->nelems);
394 unsigned int size = 0;
7e1e7763 395
12311959
VN
396 if (nelems)
397 size = roundup_pow_of_two(nelems * 3 / 2);
18093d1c
HX
398 if (size < ht->p.min_size)
399 size = ht->p.min_size;
400
401 if (old_tbl->size <= size)
402 return 0;
403
b824478b
HX
404 if (rht_dereference(old_tbl->future_tbl, ht))
405 return -EEXIST;
406
da20420f 407 return rhashtable_rehash_alloc(ht, old_tbl, size);
7e1e7763 408}
7e1e7763 409
97defe1e
TG
410static void rht_deferred_worker(struct work_struct *work)
411{
412 struct rhashtable *ht;
413 struct bucket_table *tbl;
b824478b 414 int err = 0;
97defe1e 415
57699a40 416 ht = container_of(work, struct rhashtable, run_work);
97defe1e 417 mutex_lock(&ht->mutex);
28134a53 418
97defe1e 419 tbl = rht_dereference(ht->tbl, ht);
b824478b 420 tbl = rhashtable_last_table(ht, tbl);
97defe1e 421
a5b6846f 422 if (rht_grow_above_75(ht, tbl))
da20420f 423 err = rhashtable_rehash_alloc(ht, tbl, tbl->size * 2);
b5e2c150 424 else if (ht->p.automatic_shrinking && rht_shrink_below_30(ht, tbl))
da20420f
HX
425 err = rhashtable_shrink(ht);
426 else if (tbl->nest)
427 err = rhashtable_rehash_alloc(ht, tbl, tbl->size);
b824478b 428
408f13ef
HX
429 if (!err || err == -EEXIST) {
430 int nerr;
431
432 nerr = rhashtable_rehash_table(ht);
433 err = err ?: nerr;
434 }
b824478b 435
97defe1e 436 mutex_unlock(&ht->mutex);
b824478b
HX
437
438 if (err)
439 schedule_work(&ht->run_work);
97defe1e
TG
440}
441
ca26893f
HX
442static int rhashtable_insert_rehash(struct rhashtable *ht,
443 struct bucket_table *tbl)
ccd57b1b
HX
444{
445 struct bucket_table *old_tbl;
446 struct bucket_table *new_tbl;
ccd57b1b
HX
447 unsigned int size;
448 int err;
449
450 old_tbl = rht_dereference_rcu(ht->tbl, ht);
ccd57b1b
HX
451
452 size = tbl->size;
453
3cf92222
HX
454 err = -EBUSY;
455
ccd57b1b
HX
456 if (rht_grow_above_75(ht, tbl))
457 size *= 2;
a87b9ebf
TG
458 /* Do not schedule more than one rehash */
459 else if (old_tbl != tbl)
3cf92222
HX
460 goto fail;
461
462 err = -ENOMEM;
ccd57b1b 463
93f976b5 464 new_tbl = bucket_table_alloc(ht, size, GFP_ATOMIC | __GFP_NOWARN);
3cf92222
HX
465 if (new_tbl == NULL)
466 goto fail;
ccd57b1b
HX
467
468 err = rhashtable_rehash_attach(ht, tbl, new_tbl);
469 if (err) {
470 bucket_table_free(new_tbl);
471 if (err == -EEXIST)
472 err = 0;
473 } else
474 schedule_work(&ht->run_work);
475
476 return err;
3cf92222
HX
477
478fail:
479 /* Do not fail the insert if someone else did a rehash. */
c0690016 480 if (likely(rcu_access_pointer(tbl->future_tbl)))
3cf92222
HX
481 return 0;
482
483 /* Schedule async rehash to retry allocation in process context. */
484 if (err == -ENOMEM)
485 schedule_work(&ht->run_work);
486
487 return err;
ccd57b1b 488}
ccd57b1b 489
ca26893f 490static void *rhashtable_lookup_one(struct rhashtable *ht,
ce9b362b 491 struct rhash_lock_head __rcu **bkt,
ca26893f
HX
492 struct bucket_table *tbl, unsigned int hash,
493 const void *key, struct rhash_head *obj)
02fd97c3 494{
ca26893f
HX
495 struct rhashtable_compare_arg arg = {
496 .ht = ht,
497 .key = key,
498 };
e4edbe3c 499 struct rhash_head __rcu **pprev = NULL;
02fd97c3 500 struct rhash_head *head;
ca26893f 501 int elasticity;
02fd97c3 502
5f8ddeab 503 elasticity = RHT_ELASTICITY;
adc6a3ab 504 rht_for_each_from(head, rht_ptr(bkt, tbl, hash), tbl, hash) {
ca26893f
HX
505 struct rhlist_head *list;
506 struct rhlist_head *plist;
507
508 elasticity--;
509 if (!key ||
510 (ht->p.obj_cmpfn ?
511 ht->p.obj_cmpfn(&arg, rht_obj(ht, head)) :
d3dcf8eb
PB
512 rhashtable_compare(&arg, rht_obj(ht, head)))) {
513 pprev = &head->next;
ca26893f 514 continue;
d3dcf8eb 515 }
ca26893f
HX
516
517 if (!ht->rhlist)
518 return rht_obj(ht, head);
519
520 list = container_of(obj, struct rhlist_head, rhead);
521 plist = container_of(head, struct rhlist_head, rhead);
522
523 RCU_INIT_POINTER(list->next, plist);
524 head = rht_dereference_bucket(head->next, tbl, hash);
525 RCU_INIT_POINTER(list->rhead.next, head);
8f0db018
N
526 if (pprev)
527 rcu_assign_pointer(*pprev, obj);
528 else
529 /* Need to preserve the bit lock */
f4712b46 530 rht_assign_locked(bkt, obj);
ca26893f
HX
531
532 return NULL;
5ca8cc5b 533 }
02fd97c3 534
ca26893f
HX
535 if (elasticity <= 0)
536 return ERR_PTR(-EAGAIN);
537
538 return ERR_PTR(-ENOENT);
539}
540
ce9b362b
HX
541static struct bucket_table *rhashtable_insert_one(
542 struct rhashtable *ht, struct rhash_lock_head __rcu **bkt,
543 struct bucket_table *tbl, unsigned int hash, struct rhash_head *obj,
544 void *data)
ca26893f
HX
545{
546 struct bucket_table *new_tbl;
547 struct rhash_head *head;
548
549 if (!IS_ERR_OR_NULL(data))
550 return ERR_PTR(-EEXIST);
07ee0722 551
ca26893f
HX
552 if (PTR_ERR(data) != -EAGAIN && PTR_ERR(data) != -ENOENT)
553 return ERR_CAST(data);
ccd57b1b 554
c0690016 555 new_tbl = rht_dereference_rcu(tbl->future_tbl, ht);
ca26893f
HX
556 if (new_tbl)
557 return new_tbl;
558
559 if (PTR_ERR(data) != -ENOENT)
560 return ERR_CAST(data);
561
562 if (unlikely(rht_grow_above_max(ht, tbl)))
563 return ERR_PTR(-E2BIG);
564
565 if (unlikely(rht_grow_above_100(ht, tbl)))
566 return ERR_PTR(-EAGAIN);
02fd97c3 567
adc6a3ab 568 head = rht_ptr(bkt, tbl, hash);
02fd97c3
HX
569
570 RCU_INIT_POINTER(obj->next, head);
ca26893f
HX
571 if (ht->rhlist) {
572 struct rhlist_head *list;
573
574 list = container_of(obj, struct rhlist_head, rhead);
575 RCU_INIT_POINTER(list->next, NULL);
576 }
02fd97c3 577
8f0db018
N
578 /* bkt is always the head of the list, so it holds
579 * the lock, which we need to preserve
580 */
f4712b46 581 rht_assign_locked(bkt, obj);
02fd97c3
HX
582
583 atomic_inc(&ht->nelems);
ca26893f
HX
584 if (rht_grow_above_75(ht, tbl))
585 schedule_work(&ht->run_work);
02fd97c3 586
ca26893f
HX
587 return NULL;
588}
02fd97c3 589
ca26893f
HX
590static void *rhashtable_try_insert(struct rhashtable *ht, const void *key,
591 struct rhash_head *obj)
592{
593 struct bucket_table *new_tbl;
594 struct bucket_table *tbl;
ce9b362b 595 struct rhash_lock_head __rcu **bkt;
e47877c7 596 unsigned long flags;
ca26893f 597 unsigned int hash;
ca26893f
HX
598 void *data;
599
4feb7c7a 600 new_tbl = rcu_dereference(ht->tbl);
ca26893f 601
4feb7c7a 602 do {
ca26893f
HX
603 tbl = new_tbl;
604 hash = rht_head_hashfn(ht, tbl, obj, ht->p);
8f0db018
N
605 if (rcu_access_pointer(tbl->future_tbl))
606 /* Failure is OK */
607 bkt = rht_bucket_var(tbl, hash);
608 else
609 bkt = rht_bucket_insert(ht, tbl, hash);
610 if (bkt == NULL) {
611 new_tbl = rht_dereference_rcu(tbl->future_tbl, ht);
612 data = ERR_PTR(-EAGAIN);
613 } else {
e47877c7 614 flags = rht_lock(tbl, bkt);
8f0db018
N
615 data = rhashtable_lookup_one(ht, bkt, tbl,
616 hash, key, obj);
617 new_tbl = rhashtable_insert_one(ht, bkt, tbl,
618 hash, obj, data);
619 if (PTR_ERR(new_tbl) != -EEXIST)
620 data = ERR_CAST(new_tbl);
621
e47877c7 622 rht_unlock(tbl, bkt, flags);
8f0db018 623 }
4feb7c7a 624 } while (!IS_ERR_OR_NULL(new_tbl));
ca26893f
HX
625
626 if (PTR_ERR(data) == -EAGAIN)
627 data = ERR_PTR(rhashtable_insert_rehash(ht, tbl) ?:
628 -EAGAIN);
629
630 return data;
631}
632
633void *rhashtable_insert_slow(struct rhashtable *ht, const void *key,
634 struct rhash_head *obj)
635{
636 void *data;
637
638 do {
639 rcu_read_lock();
640 data = rhashtable_try_insert(ht, key, obj);
641 rcu_read_unlock();
642 } while (PTR_ERR(data) == -EAGAIN);
643
644 return data;
02fd97c3
HX
645}
646EXPORT_SYMBOL_GPL(rhashtable_insert_slow);
647
f2dba9c6 648/**
246779dd 649 * rhashtable_walk_enter - Initialise an iterator
f2dba9c6
HX
650 * @ht: Table to walk over
651 * @iter: Hash table Iterator
652 *
653 * This function prepares a hash table walk.
654 *
655 * Note that if you restart a walk after rhashtable_walk_stop you
656 * may see the same object twice. Also, you may miss objects if
657 * there are removals in between rhashtable_walk_stop and the next
658 * call to rhashtable_walk_start.
659 *
660 * For a completely stable walk you should construct your own data
661 * structure outside the hash table.
662 *
82266e98
N
663 * This function may be called from any process context, including
664 * non-preemptable context, but cannot be called from softirq or
665 * hardirq context.
f2dba9c6 666 *
246779dd 667 * You must call rhashtable_walk_exit after this function returns.
f2dba9c6 668 */
246779dd 669void rhashtable_walk_enter(struct rhashtable *ht, struct rhashtable_iter *iter)
f2dba9c6
HX
670{
671 iter->ht = ht;
672 iter->p = NULL;
673 iter->slot = 0;
674 iter->skip = 0;
2db54b47 675 iter->end_of_table = 0;
f2dba9c6 676
c6ff5268 677 spin_lock(&ht->lock);
246779dd 678 iter->walker.tbl =
179ccc0a 679 rcu_dereference_protected(ht->tbl, lockdep_is_held(&ht->lock));
246779dd 680 list_add(&iter->walker.list, &iter->walker.tbl->walkers);
c6ff5268 681 spin_unlock(&ht->lock);
f2dba9c6 682}
246779dd 683EXPORT_SYMBOL_GPL(rhashtable_walk_enter);
f2dba9c6
HX
684
685/**
686 * rhashtable_walk_exit - Free an iterator
687 * @iter: Hash table Iterator
688 *
6c4128f6 689 * This function frees resources allocated by rhashtable_walk_enter.
f2dba9c6
HX
690 */
691void rhashtable_walk_exit(struct rhashtable_iter *iter)
692{
c6ff5268 693 spin_lock(&iter->ht->lock);
246779dd
HX
694 if (iter->walker.tbl)
695 list_del(&iter->walker.list);
c6ff5268 696 spin_unlock(&iter->ht->lock);
f2dba9c6
HX
697}
698EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
699
700/**
97a6ec4a 701 * rhashtable_walk_start_check - Start a hash table walk
f2dba9c6
HX
702 * @iter: Hash table iterator
703 *
0647169c
AG
704 * Start a hash table walk at the current iterator position. Note that we take
705 * the RCU lock in all cases including when we return an error. So you must
706 * always call rhashtable_walk_stop to clean up.
f2dba9c6
HX
707 *
708 * Returns zero if successful.
709 *
9dbbc3b9 710 * Returns -EAGAIN if resize event occurred. Note that the iterator
f2dba9c6
HX
711 * will rewind back to the beginning and you may use it immediately
712 * by calling rhashtable_walk_next.
97a6ec4a
TH
713 *
714 * rhashtable_walk_start is defined as an inline variant that returns
715 * void. This is preferred in cases where the caller would ignore
716 * resize events and always continue.
f2dba9c6 717 */
97a6ec4a 718int rhashtable_walk_start_check(struct rhashtable_iter *iter)
db4374f4 719 __acquires(RCU)
f2dba9c6 720{
eddee5ba 721 struct rhashtable *ht = iter->ht;
5d240a89 722 bool rhlist = ht->rhlist;
eddee5ba 723
c6ff5268 724 rcu_read_lock();
eddee5ba 725
c6ff5268 726 spin_lock(&ht->lock);
246779dd
HX
727 if (iter->walker.tbl)
728 list_del(&iter->walker.list);
c6ff5268 729 spin_unlock(&ht->lock);
eddee5ba 730
5d240a89
N
731 if (iter->end_of_table)
732 return 0;
733 if (!iter->walker.tbl) {
246779dd 734 iter->walker.tbl = rht_dereference_rcu(ht->tbl, ht);
b41cc04b
N
735 iter->slot = 0;
736 iter->skip = 0;
f2dba9c6
HX
737 return -EAGAIN;
738 }
739
5d240a89
N
740 if (iter->p && !rhlist) {
741 /*
742 * We need to validate that 'p' is still in the table, and
743 * if so, update 'skip'
744 */
745 struct rhash_head *p;
746 int skip = 0;
747 rht_for_each_rcu(p, iter->walker.tbl, iter->slot) {
748 skip++;
749 if (p == iter->p) {
750 iter->skip = skip;
751 goto found;
752 }
753 }
754 iter->p = NULL;
755 } else if (iter->p && rhlist) {
756 /* Need to validate that 'list' is still in the table, and
757 * if so, update 'skip' and 'p'.
758 */
759 struct rhash_head *p;
760 struct rhlist_head *list;
761 int skip = 0;
762 rht_for_each_rcu(p, iter->walker.tbl, iter->slot) {
763 for (list = container_of(p, struct rhlist_head, rhead);
764 list;
765 list = rcu_dereference(list->next)) {
766 skip++;
767 if (list == iter->list) {
768 iter->p = p;
c643ecf3 769 iter->skip = skip;
5d240a89
N
770 goto found;
771 }
772 }
773 }
774 iter->p = NULL;
775 }
776found:
f2dba9c6
HX
777 return 0;
778}
97a6ec4a 779EXPORT_SYMBOL_GPL(rhashtable_walk_start_check);
f2dba9c6
HX
780
781/**
2db54b47
TH
782 * __rhashtable_walk_find_next - Find the next element in a table (or the first
783 * one in case of a new walk).
784 *
f2dba9c6
HX
785 * @iter: Hash table iterator
786 *
2db54b47 787 * Returns the found object or NULL when the end of the table is reached.
f2dba9c6 788 *
2db54b47 789 * Returns -EAGAIN if resize event occurred.
f2dba9c6 790 */
2db54b47 791static void *__rhashtable_walk_find_next(struct rhashtable_iter *iter)
f2dba9c6 792{
246779dd 793 struct bucket_table *tbl = iter->walker.tbl;
ca26893f 794 struct rhlist_head *list = iter->list;
f2dba9c6
HX
795 struct rhashtable *ht = iter->ht;
796 struct rhash_head *p = iter->p;
ca26893f 797 bool rhlist = ht->rhlist;
f2dba9c6 798
2db54b47
TH
799 if (!tbl)
800 return NULL;
f2dba9c6
HX
801
802 for (; iter->slot < tbl->size; iter->slot++) {
803 int skip = iter->skip;
804
805 rht_for_each_rcu(p, tbl, iter->slot) {
ca26893f
HX
806 if (rhlist) {
807 list = container_of(p, struct rhlist_head,
808 rhead);
809 do {
810 if (!skip)
811 goto next;
812 skip--;
813 list = rcu_dereference(list->next);
814 } while (list);
815
816 continue;
817 }
f2dba9c6
HX
818 if (!skip)
819 break;
820 skip--;
821 }
822
823next:
824 if (!rht_is_a_nulls(p)) {
825 iter->skip++;
826 iter->p = p;
ca26893f
HX
827 iter->list = list;
828 return rht_obj(ht, rhlist ? &list->rhead : p);
f2dba9c6
HX
829 }
830
831 iter->skip = 0;
832 }
833
142b942a
PS
834 iter->p = NULL;
835
d88252f9
HX
836 /* Ensure we see any new tables. */
837 smp_rmb();
838
246779dd
HX
839 iter->walker.tbl = rht_dereference_rcu(tbl->future_tbl, ht);
840 if (iter->walker.tbl) {
f2dba9c6
HX
841 iter->slot = 0;
842 iter->skip = 0;
f2dba9c6 843 return ERR_PTR(-EAGAIN);
2db54b47
TH
844 } else {
845 iter->end_of_table = true;
f2dba9c6
HX
846 }
847
c936a79f 848 return NULL;
f2dba9c6 849}
2db54b47
TH
850
851/**
852 * rhashtable_walk_next - Return the next object and advance the iterator
853 * @iter: Hash table iterator
854 *
855 * Note that you must call rhashtable_walk_stop when you are finished
856 * with the walk.
857 *
858 * Returns the next object or NULL when the end of the table is reached.
859 *
860 * Returns -EAGAIN if resize event occurred. Note that the iterator
861 * will rewind back to the beginning and you may continue to use it.
862 */
863void *rhashtable_walk_next(struct rhashtable_iter *iter)
864{
865 struct rhlist_head *list = iter->list;
866 struct rhashtable *ht = iter->ht;
867 struct rhash_head *p = iter->p;
868 bool rhlist = ht->rhlist;
869
870 if (p) {
871 if (!rhlist || !(list = rcu_dereference(list->next))) {
872 p = rcu_dereference(p->next);
873 list = container_of(p, struct rhlist_head, rhead);
874 }
875 if (!rht_is_a_nulls(p)) {
876 iter->skip++;
877 iter->p = p;
878 iter->list = list;
879 return rht_obj(ht, rhlist ? &list->rhead : p);
880 }
881
882 /* At the end of this slot, switch to next one and then find
883 * next entry from that point.
884 */
885 iter->skip = 0;
886 iter->slot++;
887 }
888
889 return __rhashtable_walk_find_next(iter);
890}
f2dba9c6
HX
891EXPORT_SYMBOL_GPL(rhashtable_walk_next);
892
2db54b47
TH
893/**
894 * rhashtable_walk_peek - Return the next object but don't advance the iterator
895 * @iter: Hash table iterator
896 *
897 * Returns the next object or NULL when the end of the table is reached.
898 *
899 * Returns -EAGAIN if resize event occurred. Note that the iterator
900 * will rewind back to the beginning and you may continue to use it.
901 */
902void *rhashtable_walk_peek(struct rhashtable_iter *iter)
903{
904 struct rhlist_head *list = iter->list;
905 struct rhashtable *ht = iter->ht;
906 struct rhash_head *p = iter->p;
907
908 if (p)
909 return rht_obj(ht, ht->rhlist ? &list->rhead : p);
910
911 /* No object found in current iter, find next one in the table. */
912
913 if (iter->skip) {
914 /* A nonzero skip value points to the next entry in the table
915 * beyond that last one that was found. Decrement skip so
916 * we find the current value. __rhashtable_walk_find_next
917 * will restore the original value of skip assuming that
918 * the table hasn't changed.
919 */
920 iter->skip--;
921 }
922
923 return __rhashtable_walk_find_next(iter);
924}
925EXPORT_SYMBOL_GPL(rhashtable_walk_peek);
926
f2dba9c6
HX
927/**
928 * rhashtable_walk_stop - Finish a hash table walk
929 * @iter: Hash table iterator
930 *
0647169c
AG
931 * Finish a hash table walk. Does not reset the iterator to the start of the
932 * hash table.
f2dba9c6
HX
933 */
934void rhashtable_walk_stop(struct rhashtable_iter *iter)
db4374f4 935 __releases(RCU)
f2dba9c6 936{
eddee5ba 937 struct rhashtable *ht;
246779dd 938 struct bucket_table *tbl = iter->walker.tbl;
eddee5ba 939
eddee5ba 940 if (!tbl)
963ecbd4 941 goto out;
eddee5ba
HX
942
943 ht = iter->ht;
944
ba7c95ea 945 spin_lock(&ht->lock);
4feb7c7a
N
946 if (rcu_head_after_call_rcu(&tbl->rcu, bucket_table_free_rcu))
947 /* This bucket table is being freed, don't re-link it. */
246779dd 948 iter->walker.tbl = NULL;
4feb7c7a
N
949 else
950 list_add(&iter->walker.list, &tbl->walkers);
ba7c95ea 951 spin_unlock(&ht->lock);
eddee5ba 952
963ecbd4
HX
953out:
954 rcu_read_unlock();
f2dba9c6
HX
955}
956EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
957
488fb86e 958static size_t rounded_hashtable_size(const struct rhashtable_params *params)
7e1e7763 959{
107d01f5
DB
960 size_t retsize;
961
962 if (params->nelem_hint)
963 retsize = max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
964 (unsigned long)params->min_size);
965 else
966 retsize = max(HASH_DEFAULT_SIZE,
967 (unsigned long)params->min_size);
968
969 return retsize;
7e1e7763
TG
970}
971
31ccde2d
HX
972static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed)
973{
974 return jhash2(key, length, seed);
975}
976
7e1e7763
TG
977/**
978 * rhashtable_init - initialize a new hash table
979 * @ht: hash table to be initialized
980 * @params: configuration parameters
981 *
982 * Initializes a new hash table based on the provided configuration
983 * parameters. A table can be configured either with a variable or
984 * fixed length key:
985 *
986 * Configuration Example 1: Fixed length keys
987 * struct test_obj {
988 * int key;
989 * void * my_member;
990 * struct rhash_head node;
991 * };
992 *
993 * struct rhashtable_params params = {
994 * .head_offset = offsetof(struct test_obj, node),
995 * .key_offset = offsetof(struct test_obj, key),
996 * .key_len = sizeof(int),
87545899 997 * .hashfn = jhash,
7e1e7763
TG
998 * };
999 *
1000 * Configuration Example 2: Variable length keys
1001 * struct test_obj {
1002 * [...]
1003 * struct rhash_head node;
1004 * };
1005 *
49f7b33e 1006 * u32 my_hash_fn(const void *data, u32 len, u32 seed)
7e1e7763
TG
1007 * {
1008 * struct test_obj *obj = data;
1009 *
1010 * return [... hash ...];
1011 * }
1012 *
1013 * struct rhashtable_params params = {
1014 * .head_offset = offsetof(struct test_obj, node),
87545899 1015 * .hashfn = jhash,
7e1e7763 1016 * .obj_hashfn = my_hash_fn,
7e1e7763
TG
1017 * };
1018 */
488fb86e
HX
1019int rhashtable_init(struct rhashtable *ht,
1020 const struct rhashtable_params *params)
7e1e7763
TG
1021{
1022 struct bucket_table *tbl;
1023 size_t size;
1024
31ccde2d 1025 if ((!params->key_len && !params->obj_hashfn) ||
02fd97c3 1026 (params->obj_hashfn && !params->obj_cmpfn))
7e1e7763
TG
1027 return -EINVAL;
1028
97defe1e
TG
1029 memset(ht, 0, sizeof(*ht));
1030 mutex_init(&ht->mutex);
ba7c95ea 1031 spin_lock_init(&ht->lock);
97defe1e
TG
1032 memcpy(&ht->p, params, sizeof(*params));
1033
a998f712
TG
1034 if (params->min_size)
1035 ht->p.min_size = roundup_pow_of_two(params->min_size);
1036
6d684e54
HX
1037 /* Cap total entries at 2^31 to avoid nelems overflow. */
1038 ht->max_elems = 1u << 31;
2d2ab658
HX
1039
1040 if (params->max_size) {
1041 ht->p.max_size = rounddown_pow_of_two(params->max_size);
1042 if (ht->p.max_size < ht->max_elems / 2)
1043 ht->max_elems = ht->p.max_size * 2;
1044 }
6d684e54 1045
48e75b43 1046 ht->p.min_size = max_t(u16, ht->p.min_size, HASH_MIN_SIZE);
a998f712 1047
107d01f5 1048 size = rounded_hashtable_size(&ht->p);
3a324606 1049
31ccde2d
HX
1050 ht->key_len = ht->p.key_len;
1051 if (!params->hashfn) {
1052 ht->p.hashfn = jhash;
1053
1054 if (!(ht->key_len & (sizeof(u32) - 1))) {
1055 ht->key_len /= sizeof(u32);
1056 ht->p.hashfn = rhashtable_jhash2;
1057 }
1058 }
1059
2d22ecf6
DB
1060 /*
1061 * This is api initialization and thus we need to guarantee the
1062 * initial rhashtable allocation. Upon failure, retry with the
1063 * smallest possible size with __GFP_NOFAIL semantics.
1064 */
b9ecfdaa 1065 tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
2d22ecf6
DB
1066 if (unlikely(tbl == NULL)) {
1067 size = max_t(u16, ht->p.min_size, HASH_MIN_SIZE);
1068 tbl = bucket_table_alloc(ht, size, GFP_KERNEL | __GFP_NOFAIL);
1069 }
7e1e7763 1070
545a148e 1071 atomic_set(&ht->nelems, 0);
a5b6846f 1072
7e1e7763
TG
1073 RCU_INIT_POINTER(ht->tbl, tbl);
1074
4c4b52d9 1075 INIT_WORK(&ht->run_work, rht_deferred_worker);
97defe1e 1076
7e1e7763
TG
1077 return 0;
1078}
1079EXPORT_SYMBOL_GPL(rhashtable_init);
1080
ca26893f
HX
1081/**
1082 * rhltable_init - initialize a new hash list table
1083 * @hlt: hash list table to be initialized
1084 * @params: configuration parameters
1085 *
1086 * Initializes a new hash list table.
1087 *
1088 * See documentation for rhashtable_init.
1089 */
1090int rhltable_init(struct rhltable *hlt, const struct rhashtable_params *params)
1091{
1092 int err;
1093
ca26893f
HX
1094 err = rhashtable_init(&hlt->ht, params);
1095 hlt->ht.rhlist = true;
1096 return err;
1097}
1098EXPORT_SYMBOL_GPL(rhltable_init);
1099
1100static void rhashtable_free_one(struct rhashtable *ht, struct rhash_head *obj,
1101 void (*free_fn)(void *ptr, void *arg),
1102 void *arg)
1103{
1104 struct rhlist_head *list;
1105
1106 if (!ht->rhlist) {
1107 free_fn(rht_obj(ht, obj), arg);
1108 return;
1109 }
1110
1111 list = container_of(obj, struct rhlist_head, rhead);
1112 do {
1113 obj = &list->rhead;
1114 list = rht_dereference(list->next, ht);
1115 free_fn(rht_obj(ht, obj), arg);
1116 } while (list);
1117}
1118
7e1e7763 1119/**
6b6f302c 1120 * rhashtable_free_and_destroy - free elements and destroy hash table
7e1e7763 1121 * @ht: the hash table to destroy
6b6f302c
TG
1122 * @free_fn: callback to release resources of element
1123 * @arg: pointer passed to free_fn
7e1e7763 1124 *
6b6f302c
TG
1125 * Stops an eventual async resize. If defined, invokes free_fn for each
1126 * element to releasal resources. Please note that RCU protected
1127 * readers may still be accessing the elements. Releasing of resources
1128 * must occur in a compatible manner. Then frees the bucket array.
1129 *
1130 * This function will eventually sleep to wait for an async resize
1131 * to complete. The caller is responsible that no further write operations
1132 * occurs in parallel.
7e1e7763 1133 */
6b6f302c
TG
1134void rhashtable_free_and_destroy(struct rhashtable *ht,
1135 void (*free_fn)(void *ptr, void *arg),
1136 void *arg)
7e1e7763 1137{
0026129c 1138 struct bucket_table *tbl, *next_tbl;
6b6f302c 1139 unsigned int i;
97defe1e 1140
4c4b52d9 1141 cancel_work_sync(&ht->run_work);
97defe1e 1142
57699a40 1143 mutex_lock(&ht->mutex);
6b6f302c 1144 tbl = rht_dereference(ht->tbl, ht);
0026129c 1145restart:
6b6f302c
TG
1146 if (free_fn) {
1147 for (i = 0; i < tbl->size; i++) {
1148 struct rhash_head *pos, *next;
1149
ae6da1f5 1150 cond_resched();
adc6a3ab 1151 for (pos = rht_ptr_exclusive(rht_bucket(tbl, i)),
6b6f302c
TG
1152 next = !rht_is_a_nulls(pos) ?
1153 rht_dereference(pos->next, ht) : NULL;
1154 !rht_is_a_nulls(pos);
1155 pos = next,
1156 next = !rht_is_a_nulls(pos) ?
1157 rht_dereference(pos->next, ht) : NULL)
ca26893f 1158 rhashtable_free_one(ht, pos, free_fn, arg);
6b6f302c
TG
1159 }
1160 }
1161
0026129c 1162 next_tbl = rht_dereference(tbl->future_tbl, ht);
6b6f302c 1163 bucket_table_free(tbl);
0026129c
TY
1164 if (next_tbl) {
1165 tbl = next_tbl;
1166 goto restart;
1167 }
97defe1e 1168 mutex_unlock(&ht->mutex);
7e1e7763 1169}
6b6f302c
TG
1170EXPORT_SYMBOL_GPL(rhashtable_free_and_destroy);
1171
1172void rhashtable_destroy(struct rhashtable *ht)
1173{
1174 return rhashtable_free_and_destroy(ht, NULL, NULL);
1175}
7e1e7763 1176EXPORT_SYMBOL_GPL(rhashtable_destroy);
da20420f 1177
ce9b362b
HX
1178struct rhash_lock_head __rcu **__rht_bucket_nested(
1179 const struct bucket_table *tbl, unsigned int hash)
da20420f
HX
1180{
1181 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
da20420f
HX
1182 unsigned int index = hash & ((1 << tbl->nest) - 1);
1183 unsigned int size = tbl->size >> tbl->nest;
1184 unsigned int subhash = hash;
1185 union nested_table *ntbl;
1186
4a3084aa 1187 ntbl = nested_table_top(tbl);
c4d2603d 1188 ntbl = rht_dereference_bucket_rcu(ntbl[index].table, tbl, hash);
da20420f
HX
1189 subhash >>= tbl->nest;
1190
1191 while (ntbl && size > (1 << shift)) {
1192 index = subhash & ((1 << shift) - 1);
c4d2603d
HX
1193 ntbl = rht_dereference_bucket_rcu(ntbl[index].table,
1194 tbl, hash);
da20420f
HX
1195 size >>= shift;
1196 subhash >>= shift;
1197 }
1198
ff302db9
N
1199 if (!ntbl)
1200 return NULL;
da20420f
HX
1201
1202 return &ntbl[subhash].bucket;
1203
1204}
ff302db9
N
1205EXPORT_SYMBOL_GPL(__rht_bucket_nested);
1206
ce9b362b
HX
1207struct rhash_lock_head __rcu **rht_bucket_nested(
1208 const struct bucket_table *tbl, unsigned int hash)
ff302db9 1209{
ce9b362b 1210 static struct rhash_lock_head __rcu *rhnull;
ff302db9
N
1211
1212 if (!rhnull)
1213 INIT_RHT_NULLS_HEAD(rhnull);
1214 return __rht_bucket_nested(tbl, hash) ?: &rhnull;
1215}
da20420f
HX
1216EXPORT_SYMBOL_GPL(rht_bucket_nested);
1217
ce9b362b
HX
1218struct rhash_lock_head __rcu **rht_bucket_nested_insert(
1219 struct rhashtable *ht, struct bucket_table *tbl, unsigned int hash)
da20420f
HX
1220{
1221 const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
1222 unsigned int index = hash & ((1 << tbl->nest) - 1);
1223 unsigned int size = tbl->size >> tbl->nest;
1224 union nested_table *ntbl;
da20420f 1225
4a3084aa 1226 ntbl = nested_table_top(tbl);
da20420f 1227 hash >>= tbl->nest;
da20420f 1228 ntbl = nested_table_alloc(ht, &ntbl[index].table,
5af68ef7 1229 size <= (1 << shift));
da20420f
HX
1230
1231 while (ntbl && size > (1 << shift)) {
1232 index = hash & ((1 << shift) - 1);
1233 size >>= shift;
1234 hash >>= shift;
da20420f 1235 ntbl = nested_table_alloc(ht, &ntbl[index].table,
5af68ef7 1236 size <= (1 << shift));
da20420f
HX
1237 }
1238
1239 if (!ntbl)
1240 return NULL;
1241
1242 return &ntbl[hash].bucket;
1243
1244}
1245EXPORT_SYMBOL_GPL(rht_bucket_nested_insert);