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