blob: 798f01d64ab0135e602a518f93801d7be34ab3f7 [file] [log] [blame]
Thomas Graf7e1e7762014-08-02 11:47:44 +02001/*
2 * Resizable, Scalable, Concurrent Hash Table
3 *
Herbert Xu02fd97c2015-03-20 21:57:00 +11004 * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
Thomas Grafa5ec68e2015-02-05 02:03:32 +01005 * Copyright (c) 2014-2015 Thomas Graf <tgraf@suug.ch>
Thomas Graf7e1e7762014-08-02 11:47:44 +02006 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
7 *
Thomas Graf7e1e7762014-08-02 11:47:44 +02008 * Code partially derived from nft_hash
Herbert Xu02fd97c2015-03-20 21:57:00 +11009 * Rewritten with rehash code from br_multicast plus single list
10 * pointer as suggested by Josh Triplett
Thomas Graf7e1e7762014-08-02 11:47:44 +020011 *
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
17#include <linux/kernel.h>
18#include <linux/init.h>
19#include <linux/log2.h>
Eric Dumazet5beb5c92015-02-26 07:20:34 -080020#include <linux/sched.h>
Thomas Graf7e1e7762014-08-02 11:47:44 +020021#include <linux/slab.h>
22#include <linux/vmalloc.h>
23#include <linux/mm.h>
Daniel Borkmann87545892014-12-10 16:33:11 +010024#include <linux/jhash.h>
Thomas Graf7e1e7762014-08-02 11:47:44 +020025#include <linux/random.h>
26#include <linux/rhashtable.h>
Stephen Rothwell61d7b092015-02-09 14:04:03 +110027#include <linux/err.h>
Thomas Graf7e1e7762014-08-02 11:47:44 +020028
29#define HASH_DEFAULT_SIZE 64UL
Herbert Xuc2e213c2015-03-18 20:01:16 +110030#define HASH_MIN_SIZE 4U
Thomas Graf97defe12015-01-02 23:00:20 +010031#define BUCKET_LOCKS_PER_CPU 128UL
32
Herbert Xu988dfbd2015-03-10 09:27:55 +110033static u32 head_hashfn(struct rhashtable *ht,
Thomas Graf8d24c0b2015-01-02 23:00:14 +010034 const struct bucket_table *tbl,
35 const struct rhash_head *he)
Thomas Graf7e1e7762014-08-02 11:47:44 +020036{
Herbert Xu02fd97c2015-03-20 21:57:00 +110037 return rht_head_hashfn(ht, tbl, he, ht->p);
Thomas Graf7e1e7762014-08-02 11:47:44 +020038}
39
Thomas Grafa03eaec2015-02-05 02:03:34 +010040#ifdef CONFIG_PROVE_LOCKING
Thomas Grafa03eaec2015-02-05 02:03:34 +010041#define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
Thomas Grafa03eaec2015-02-05 02:03:34 +010042
43int lockdep_rht_mutex_is_held(struct rhashtable *ht)
44{
45 return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1;
46}
47EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held);
48
49int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash)
50{
Herbert Xu02fd97c2015-03-20 21:57:00 +110051 spinlock_t *lock = rht_bucket_lock(tbl, hash);
Thomas Grafa03eaec2015-02-05 02:03:34 +010052
53 return (debug_locks) ? lockdep_is_held(lock) : 1;
54}
55EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held);
56#else
57#define ASSERT_RHT_MUTEX(HT)
Thomas Grafa03eaec2015-02-05 02:03:34 +010058#endif
59
60
Thomas Graf97defe12015-01-02 23:00:20 +010061static int alloc_bucket_locks(struct rhashtable *ht, struct bucket_table *tbl)
62{
63 unsigned int i, size;
64#if defined(CONFIG_PROVE_LOCKING)
65 unsigned int nr_pcpus = 2;
66#else
67 unsigned int nr_pcpus = num_possible_cpus();
68#endif
69
70 nr_pcpus = min_t(unsigned int, nr_pcpus, 32UL);
71 size = roundup_pow_of_two(nr_pcpus * ht->p.locks_mul);
72
Thomas Grafa5ec68e2015-02-05 02:03:32 +010073 /* Never allocate more than 0.5 locks per bucket */
74 size = min_t(unsigned int, size, tbl->size >> 1);
Thomas Graf97defe12015-01-02 23:00:20 +010075
76 if (sizeof(spinlock_t) != 0) {
77#ifdef CONFIG_NUMA
78 if (size * sizeof(spinlock_t) > PAGE_SIZE)
79 tbl->locks = vmalloc(size * sizeof(spinlock_t));
80 else
81#endif
82 tbl->locks = kmalloc_array(size, sizeof(spinlock_t),
83 GFP_KERNEL);
84 if (!tbl->locks)
85 return -ENOMEM;
86 for (i = 0; i < size; i++)
87 spin_lock_init(&tbl->locks[i]);
88 }
89 tbl->locks_mask = size - 1;
90
91 return 0;
92}
93
94static void bucket_table_free(const struct bucket_table *tbl)
95{
96 if (tbl)
97 kvfree(tbl->locks);
98
99 kvfree(tbl);
100}
101
Herbert Xu9d901bc2015-03-14 13:57:23 +1100102static void bucket_table_free_rcu(struct rcu_head *head)
103{
104 bucket_table_free(container_of(head, struct bucket_table, rcu));
105}
106
Thomas Graf97defe12015-01-02 23:00:20 +0100107static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
Herbert Xu5269b532015-03-14 13:57:22 +1100108 size_t nbuckets)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200109{
Daniel Borkmanneb6d1ab2015-02-20 00:53:38 +0100110 struct bucket_table *tbl = NULL;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200111 size_t size;
Thomas Graff89bd6f2015-01-02 23:00:21 +0100112 int i;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200113
114 size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]);
Daniel Borkmanneb6d1ab2015-02-20 00:53:38 +0100115 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
116 tbl = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200117 if (tbl == NULL)
118 tbl = vzalloc(size);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200119 if (tbl == NULL)
120 return NULL;
121
122 tbl->size = nbuckets;
123
Thomas Graf97defe12015-01-02 23:00:20 +0100124 if (alloc_bucket_locks(ht, tbl) < 0) {
125 bucket_table_free(tbl);
126 return NULL;
127 }
Thomas Graf7e1e7762014-08-02 11:47:44 +0200128
Herbert Xueddee5ba2015-03-14 13:57:20 +1100129 INIT_LIST_HEAD(&tbl->walkers);
130
Herbert Xu5269b532015-03-14 13:57:22 +1100131 get_random_bytes(&tbl->hash_rnd, sizeof(tbl->hash_rnd));
132
Thomas Graff89bd6f2015-01-02 23:00:21 +0100133 for (i = 0; i < nbuckets; i++)
134 INIT_RHT_NULLS_HEAD(tbl->buckets[i], ht, i);
135
Thomas Graf97defe12015-01-02 23:00:20 +0100136 return tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200137}
138
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100139static int rhashtable_rehash_one(struct rhashtable *ht, unsigned old_hash)
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100140{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100141 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
Herbert Xuc4db8842015-03-14 13:57:25 +1100142 struct bucket_table *new_tbl =
143 rht_dereference(old_tbl->future_tbl, ht) ?: old_tbl;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100144 struct rhash_head __rcu **pprev = &old_tbl->buckets[old_hash];
145 int err = -ENOENT;
146 struct rhash_head *head, *next, *entry;
147 spinlock_t *new_bucket_lock;
148 unsigned new_hash;
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100149
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100150 rht_for_each(entry, old_tbl, old_hash) {
151 err = 0;
152 next = rht_dereference_bucket(entry->next, old_tbl, old_hash);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100153
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100154 if (rht_is_a_nulls(next))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200155 break;
Thomas Graf97defe12015-01-02 23:00:20 +0100156
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100157 pprev = &entry->next;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200158 }
159
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100160 if (err)
161 goto out;
Thomas Graf97defe12015-01-02 23:00:20 +0100162
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100163 new_hash = head_hashfn(ht, new_tbl, entry);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100164
Herbert Xu02fd97c2015-03-20 21:57:00 +1100165 new_bucket_lock = rht_bucket_lock(new_tbl, new_hash);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100166
Herbert Xu8f2484b2015-03-14 13:57:21 +1100167 spin_lock_nested(new_bucket_lock, SINGLE_DEPTH_NESTING);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100168 head = rht_dereference_bucket(new_tbl->buckets[new_hash],
169 new_tbl, new_hash);
170
171 if (rht_is_a_nulls(head))
172 INIT_RHT_NULLS_HEAD(entry->next, ht, new_hash);
173 else
174 RCU_INIT_POINTER(entry->next, head);
175
176 rcu_assign_pointer(new_tbl->buckets[new_hash], entry);
177 spin_unlock(new_bucket_lock);
178
179 rcu_assign_pointer(*pprev, next);
180
181out:
182 return err;
Thomas Graf97defe12015-01-02 23:00:20 +0100183}
184
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100185static void rhashtable_rehash_chain(struct rhashtable *ht, unsigned old_hash)
Thomas Graf97defe12015-01-02 23:00:20 +0100186{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100187 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
188 spinlock_t *old_bucket_lock;
Thomas Graf7cd10db2015-02-05 02:03:35 +0100189
Herbert Xu02fd97c2015-03-20 21:57:00 +1100190 old_bucket_lock = rht_bucket_lock(old_tbl, old_hash);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100191
192 spin_lock_bh(old_bucket_lock);
193 while (!rhashtable_rehash_one(ht, old_hash))
194 ;
Herbert Xu63d512d2015-03-14 13:57:24 +1100195 old_tbl->rehash++;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100196 spin_unlock_bh(old_bucket_lock);
197}
198
199static void rhashtable_rehash(struct rhashtable *ht,
200 struct bucket_table *new_tbl)
201{
202 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100203 struct rhashtable_walker *walker;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100204 unsigned old_hash;
205
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100206 /* Make insertions go into the new, empty table right away. Deletions
207 * and lookups will be attempted in both tables until we synchronize.
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100208 */
Herbert Xuc4db8842015-03-14 13:57:25 +1100209 rcu_assign_pointer(old_tbl->future_tbl, new_tbl);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100210
Herbert Xu9497df82015-03-12 22:07:49 +1100211 /* Ensure the new table is visible to readers. */
212 smp_wmb();
213
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100214 for (old_hash = 0; old_hash < old_tbl->size; old_hash++)
215 rhashtable_rehash_chain(ht, old_hash);
216
217 /* Publish the new table pointer. */
218 rcu_assign_pointer(ht->tbl, new_tbl);
219
Herbert Xueddee5ba2015-03-14 13:57:20 +1100220 list_for_each_entry(walker, &old_tbl->walkers, list)
221 walker->tbl = NULL;
222
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100223 /* Wait for readers. All new readers will see the new
224 * table, and thus no references to the old table will
225 * remain.
226 */
Herbert Xu9d901bc2015-03-14 13:57:23 +1100227 call_rcu(&old_tbl->rcu, bucket_table_free_rcu);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200228}
229
230/**
231 * rhashtable_expand - Expand hash table while allowing concurrent lookups
232 * @ht: the hash table to expand
Thomas Graf7e1e7762014-08-02 11:47:44 +0200233 *
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100234 * A secondary bucket array is allocated and the hash entries are migrated.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200235 *
236 * This function may only be called in a context where it is safe to call
237 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
238 *
Thomas Graf97defe12015-01-02 23:00:20 +0100239 * The caller must ensure that no concurrent resizing occurs by holding
240 * ht->mutex.
241 *
242 * It is valid to have concurrent insertions and deletions protected by per
243 * bucket locks or concurrent RCU protected lookups and traversals.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200244 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100245int rhashtable_expand(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200246{
247 struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200248
249 ASSERT_RHT_MUTEX(ht);
250
Herbert Xu5269b532015-03-14 13:57:22 +1100251 new_tbl = bucket_table_alloc(ht, old_tbl->size * 2);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200252 if (new_tbl == NULL)
253 return -ENOMEM;
254
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100255 rhashtable_rehash(ht, new_tbl);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200256 return 0;
257}
258EXPORT_SYMBOL_GPL(rhashtable_expand);
259
260/**
261 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
262 * @ht: the hash table to shrink
Thomas Graf7e1e7762014-08-02 11:47:44 +0200263 *
264 * This function may only be called in a context where it is safe to call
265 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
266 *
Thomas Graf97defe12015-01-02 23:00:20 +0100267 * The caller must ensure that no concurrent resizing occurs by holding
268 * ht->mutex.
269 *
Thomas Graf7e1e7762014-08-02 11:47:44 +0200270 * The caller must ensure that no concurrent table mutations take place.
271 * It is however valid to have concurrent lookups if they are RCU protected.
Thomas Graf97defe12015-01-02 23:00:20 +0100272 *
273 * It is valid to have concurrent insertions and deletions protected by per
274 * bucket locks or concurrent RCU protected lookups and traversals.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200275 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100276int rhashtable_shrink(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200277{
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100278 struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200279
280 ASSERT_RHT_MUTEX(ht);
281
Herbert Xu5269b532015-03-14 13:57:22 +1100282 new_tbl = bucket_table_alloc(ht, old_tbl->size / 2);
Thomas Graf97defe12015-01-02 23:00:20 +0100283 if (new_tbl == NULL)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200284 return -ENOMEM;
285
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100286 rhashtable_rehash(ht, new_tbl);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200287 return 0;
288}
289EXPORT_SYMBOL_GPL(rhashtable_shrink);
290
Thomas Graf97defe12015-01-02 23:00:20 +0100291static void rht_deferred_worker(struct work_struct *work)
292{
293 struct rhashtable *ht;
294 struct bucket_table *tbl;
295
Ying Xue57699a42015-01-16 11:13:09 +0800296 ht = container_of(work, struct rhashtable, run_work);
Thomas Graf97defe12015-01-02 23:00:20 +0100297 mutex_lock(&ht->mutex);
Herbert Xu28134a52015-02-04 07:33:22 +1100298 if (ht->being_destroyed)
299 goto unlock;
300
Thomas Graf97defe12015-01-02 23:00:20 +0100301 tbl = rht_dereference(ht->tbl, ht);
302
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100303 if (rht_grow_above_75(ht, tbl))
Thomas Graf97defe12015-01-02 23:00:20 +0100304 rhashtable_expand(ht);
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100305 else if (rht_shrink_below_30(ht, tbl))
Thomas Graf97defe12015-01-02 23:00:20 +0100306 rhashtable_shrink(ht);
Herbert Xu28134a52015-02-04 07:33:22 +1100307unlock:
Thomas Graf97defe12015-01-02 23:00:20 +0100308 mutex_unlock(&ht->mutex);
309}
310
Herbert Xu02fd97c2015-03-20 21:57:00 +1100311int rhashtable_insert_slow(struct rhashtable *ht, const void *key,
312 struct rhash_head *obj,
313 struct bucket_table *tbl)
314{
315 struct rhash_head *head;
316 unsigned hash;
317 int err = -EEXIST;
318
319 hash = head_hashfn(ht, tbl, obj);
320 spin_lock_nested(rht_bucket_lock(tbl, hash), SINGLE_DEPTH_NESTING);
321
322 if (key && rhashtable_lookup_fast(ht, key, ht->p))
323 goto exit;
324
325 err = 0;
326
327 head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash);
328
329 RCU_INIT_POINTER(obj->next, head);
330
331 rcu_assign_pointer(tbl->buckets[hash], obj);
332
333 atomic_inc(&ht->nelems);
334
335exit:
336 spin_unlock(rht_bucket_lock(tbl, hash));
337
338 return err;
339}
340EXPORT_SYMBOL_GPL(rhashtable_insert_slow);
341
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100342/**
343 * rhashtable_walk_init - Initialise an iterator
344 * @ht: Table to walk over
345 * @iter: Hash table Iterator
346 *
347 * This function prepares a hash table walk.
348 *
349 * Note that if you restart a walk after rhashtable_walk_stop you
350 * may see the same object twice. Also, you may miss objects if
351 * there are removals in between rhashtable_walk_stop and the next
352 * call to rhashtable_walk_start.
353 *
354 * For a completely stable walk you should construct your own data
355 * structure outside the hash table.
356 *
357 * This function may sleep so you must not call it from interrupt
358 * context or with spin locks held.
359 *
360 * You must call rhashtable_walk_exit if this function returns
361 * successfully.
362 */
363int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter)
364{
365 iter->ht = ht;
366 iter->p = NULL;
367 iter->slot = 0;
368 iter->skip = 0;
369
370 iter->walker = kmalloc(sizeof(*iter->walker), GFP_KERNEL);
371 if (!iter->walker)
372 return -ENOMEM;
373
374 mutex_lock(&ht->mutex);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100375 iter->walker->tbl = rht_dereference(ht->tbl, ht);
376 list_add(&iter->walker->list, &iter->walker->tbl->walkers);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100377 mutex_unlock(&ht->mutex);
378
379 return 0;
380}
381EXPORT_SYMBOL_GPL(rhashtable_walk_init);
382
383/**
384 * rhashtable_walk_exit - Free an iterator
385 * @iter: Hash table Iterator
386 *
387 * This function frees resources allocated by rhashtable_walk_init.
388 */
389void rhashtable_walk_exit(struct rhashtable_iter *iter)
390{
391 mutex_lock(&iter->ht->mutex);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100392 if (iter->walker->tbl)
393 list_del(&iter->walker->list);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100394 mutex_unlock(&iter->ht->mutex);
395 kfree(iter->walker);
396}
397EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
398
399/**
400 * rhashtable_walk_start - Start a hash table walk
401 * @iter: Hash table iterator
402 *
403 * Start a hash table walk. Note that we take the RCU lock in all
404 * cases including when we return an error. So you must always call
405 * rhashtable_walk_stop to clean up.
406 *
407 * Returns zero if successful.
408 *
409 * Returns -EAGAIN if resize event occured. Note that the iterator
410 * will rewind back to the beginning and you may use it immediately
411 * by calling rhashtable_walk_next.
412 */
413int rhashtable_walk_start(struct rhashtable_iter *iter)
Thomas Grafdb4374f2015-03-16 10:42:27 +0100414 __acquires(RCU)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100415{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100416 struct rhashtable *ht = iter->ht;
417
418 mutex_lock(&ht->mutex);
419
420 if (iter->walker->tbl)
421 list_del(&iter->walker->list);
422
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100423 rcu_read_lock();
424
Herbert Xueddee5ba2015-03-14 13:57:20 +1100425 mutex_unlock(&ht->mutex);
426
427 if (!iter->walker->tbl) {
428 iter->walker->tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100429 return -EAGAIN;
430 }
431
432 return 0;
433}
434EXPORT_SYMBOL_GPL(rhashtable_walk_start);
435
436/**
437 * rhashtable_walk_next - Return the next object and advance the iterator
438 * @iter: Hash table iterator
439 *
440 * Note that you must call rhashtable_walk_stop when you are finished
441 * with the walk.
442 *
443 * Returns the next object or NULL when the end of the table is reached.
444 *
445 * Returns -EAGAIN if resize event occured. Note that the iterator
446 * will rewind back to the beginning and you may continue to use it.
447 */
448void *rhashtable_walk_next(struct rhashtable_iter *iter)
449{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100450 struct bucket_table *tbl = iter->walker->tbl;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100451 struct rhashtable *ht = iter->ht;
452 struct rhash_head *p = iter->p;
453 void *obj = NULL;
454
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100455 if (p) {
456 p = rht_dereference_bucket_rcu(p->next, tbl, iter->slot);
457 goto next;
458 }
459
460 for (; iter->slot < tbl->size; iter->slot++) {
461 int skip = iter->skip;
462
463 rht_for_each_rcu(p, tbl, iter->slot) {
464 if (!skip)
465 break;
466 skip--;
467 }
468
469next:
470 if (!rht_is_a_nulls(p)) {
471 iter->skip++;
472 iter->p = p;
473 obj = rht_obj(ht, p);
474 goto out;
475 }
476
477 iter->skip = 0;
478 }
479
Herbert Xud88252f2015-03-24 00:50:19 +1100480 /* Ensure we see any new tables. */
481 smp_rmb();
482
Herbert Xuc4db8842015-03-14 13:57:25 +1100483 iter->walker->tbl = rht_dereference_rcu(tbl->future_tbl, ht);
484 if (iter->walker->tbl) {
Herbert Xueddee5ba2015-03-14 13:57:20 +1100485 iter->slot = 0;
486 iter->skip = 0;
487 return ERR_PTR(-EAGAIN);
488 }
489
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100490 iter->p = NULL;
491
492out:
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100493
494 return obj;
495}
496EXPORT_SYMBOL_GPL(rhashtable_walk_next);
497
498/**
499 * rhashtable_walk_stop - Finish a hash table walk
500 * @iter: Hash table iterator
501 *
502 * Finish a hash table walk.
503 */
504void rhashtable_walk_stop(struct rhashtable_iter *iter)
Thomas Grafdb4374f2015-03-16 10:42:27 +0100505 __releases(RCU)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100506{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100507 struct rhashtable *ht;
508 struct bucket_table *tbl = iter->walker->tbl;
509
Herbert Xueddee5ba2015-03-14 13:57:20 +1100510 if (!tbl)
Herbert Xu963ecbd2015-03-15 21:12:04 +1100511 goto out;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100512
513 ht = iter->ht;
514
515 mutex_lock(&ht->mutex);
Herbert Xuc4db8842015-03-14 13:57:25 +1100516 if (tbl->rehash < tbl->size)
Herbert Xueddee5ba2015-03-14 13:57:20 +1100517 list_add(&iter->walker->list, &tbl->walkers);
518 else
519 iter->walker->tbl = NULL;
520 mutex_unlock(&ht->mutex);
521
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100522 iter->p = NULL;
Herbert Xu963ecbd2015-03-15 21:12:04 +1100523
524out:
525 rcu_read_unlock();
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100526}
527EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
528
Herbert Xu488fb86e2015-03-20 21:56:59 +1100529static size_t rounded_hashtable_size(const struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200530{
Ying Xue94000172014-09-03 09:22:36 +0800531 return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
Herbert Xue2e21c12015-03-18 20:01:21 +1100532 (unsigned long)params->min_size);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200533}
534
Herbert Xu31ccde22015-03-24 00:50:21 +1100535static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed)
536{
537 return jhash2(key, length, seed);
538}
539
Thomas Graf7e1e7762014-08-02 11:47:44 +0200540/**
541 * rhashtable_init - initialize a new hash table
542 * @ht: hash table to be initialized
543 * @params: configuration parameters
544 *
545 * Initializes a new hash table based on the provided configuration
546 * parameters. A table can be configured either with a variable or
547 * fixed length key:
548 *
549 * Configuration Example 1: Fixed length keys
550 * struct test_obj {
551 * int key;
552 * void * my_member;
553 * struct rhash_head node;
554 * };
555 *
556 * struct rhashtable_params params = {
557 * .head_offset = offsetof(struct test_obj, node),
558 * .key_offset = offsetof(struct test_obj, key),
559 * .key_len = sizeof(int),
Daniel Borkmann87545892014-12-10 16:33:11 +0100560 * .hashfn = jhash,
Thomas Graff89bd6f2015-01-02 23:00:21 +0100561 * .nulls_base = (1U << RHT_BASE_SHIFT),
Thomas Graf7e1e7762014-08-02 11:47:44 +0200562 * };
563 *
564 * Configuration Example 2: Variable length keys
565 * struct test_obj {
566 * [...]
567 * struct rhash_head node;
568 * };
569 *
570 * u32 my_hash_fn(const void *data, u32 seed)
571 * {
572 * struct test_obj *obj = data;
573 *
574 * return [... hash ...];
575 * }
576 *
577 * struct rhashtable_params params = {
578 * .head_offset = offsetof(struct test_obj, node),
Daniel Borkmann87545892014-12-10 16:33:11 +0100579 * .hashfn = jhash,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200580 * .obj_hashfn = my_hash_fn,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200581 * };
582 */
Herbert Xu488fb86e2015-03-20 21:56:59 +1100583int rhashtable_init(struct rhashtable *ht,
584 const struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200585{
586 struct bucket_table *tbl;
587 size_t size;
588
589 size = HASH_DEFAULT_SIZE;
590
Herbert Xu31ccde22015-03-24 00:50:21 +1100591 if ((!params->key_len && !params->obj_hashfn) ||
Herbert Xu02fd97c2015-03-20 21:57:00 +1100592 (params->obj_hashfn && !params->obj_cmpfn))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200593 return -EINVAL;
594
Thomas Graff89bd6f2015-01-02 23:00:21 +0100595 if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT))
596 return -EINVAL;
597
Thomas Graf7e1e7762014-08-02 11:47:44 +0200598 if (params->nelem_hint)
Ying Xue94000172014-09-03 09:22:36 +0800599 size = rounded_hashtable_size(params);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200600
Thomas Graf97defe12015-01-02 23:00:20 +0100601 memset(ht, 0, sizeof(*ht));
602 mutex_init(&ht->mutex);
603 memcpy(&ht->p, params, sizeof(*params));
604
Thomas Grafa998f712015-03-19 22:31:13 +0000605 if (params->min_size)
606 ht->p.min_size = roundup_pow_of_two(params->min_size);
607
608 if (params->max_size)
609 ht->p.max_size = rounddown_pow_of_two(params->max_size);
610
Herbert Xu488fb86e2015-03-20 21:56:59 +1100611 ht->p.min_size = max(ht->p.min_size, HASH_MIN_SIZE);
Thomas Grafa998f712015-03-19 22:31:13 +0000612
Thomas Graf97defe12015-01-02 23:00:20 +0100613 if (params->locks_mul)
614 ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
615 else
616 ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
617
Herbert Xu31ccde22015-03-24 00:50:21 +1100618 ht->key_len = ht->p.key_len;
619 if (!params->hashfn) {
620 ht->p.hashfn = jhash;
621
622 if (!(ht->key_len & (sizeof(u32) - 1))) {
623 ht->key_len /= sizeof(u32);
624 ht->p.hashfn = rhashtable_jhash2;
625 }
626 }
627
Herbert Xu5269b532015-03-14 13:57:22 +1100628 tbl = bucket_table_alloc(ht, size);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200629 if (tbl == NULL)
630 return -ENOMEM;
631
Ying Xue545a1482015-01-07 13:41:57 +0800632 atomic_set(&ht->nelems, 0);
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100633
Thomas Graf7e1e7762014-08-02 11:47:44 +0200634 RCU_INIT_POINTER(ht->tbl, tbl);
635
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100636 INIT_WORK(&ht->run_work, rht_deferred_worker);
Thomas Graf97defe12015-01-02 23:00:20 +0100637
Thomas Graf7e1e7762014-08-02 11:47:44 +0200638 return 0;
639}
640EXPORT_SYMBOL_GPL(rhashtable_init);
641
642/**
643 * rhashtable_destroy - destroy hash table
644 * @ht: the hash table to destroy
645 *
Pablo Neira Ayusoae82ddc2014-09-02 00:26:05 +0200646 * Frees the bucket array. This function is not rcu safe, therefore the caller
647 * has to make sure that no resizing may happen by unpublishing the hashtable
648 * and waiting for the quiescent cycle before releasing the bucket array.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200649 */
Thomas Graf97defe12015-01-02 23:00:20 +0100650void rhashtable_destroy(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200651{
Thomas Graf97defe12015-01-02 23:00:20 +0100652 ht->being_destroyed = true;
653
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100654 cancel_work_sync(&ht->run_work);
Ying Xue57699a42015-01-16 11:13:09 +0800655
Thomas Graf97defe12015-01-02 23:00:20 +0100656 mutex_lock(&ht->mutex);
Thomas Graf97defe12015-01-02 23:00:20 +0100657 bucket_table_free(rht_dereference(ht->tbl, ht));
Thomas Graf97defe12015-01-02 23:00:20 +0100658 mutex_unlock(&ht->mutex);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200659}
660EXPORT_SYMBOL_GPL(rhashtable_destroy);