blob: 5e04403e25f5503f2372633b7c765285503ad908 [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 Xub8244782015-03-24 00:50:26 +1100139static struct bucket_table *rhashtable_last_table(struct rhashtable *ht,
140 struct bucket_table *tbl)
141{
142 struct bucket_table *new_tbl;
143
144 do {
145 new_tbl = tbl;
146 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
147 } while (tbl);
148
149 return new_tbl;
150}
151
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100152static int rhashtable_rehash_one(struct rhashtable *ht, unsigned old_hash)
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100153{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100154 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
Herbert Xub8244782015-03-24 00:50:26 +1100155 struct bucket_table *new_tbl = rhashtable_last_table(ht,
156 rht_dereference_rcu(old_tbl->future_tbl, ht));
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100157 struct rhash_head __rcu **pprev = &old_tbl->buckets[old_hash];
158 int err = -ENOENT;
159 struct rhash_head *head, *next, *entry;
160 spinlock_t *new_bucket_lock;
161 unsigned new_hash;
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100162
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100163 rht_for_each(entry, old_tbl, old_hash) {
164 err = 0;
165 next = rht_dereference_bucket(entry->next, old_tbl, old_hash);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100166
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100167 if (rht_is_a_nulls(next))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200168 break;
Thomas Graf97defe12015-01-02 23:00:20 +0100169
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100170 pprev = &entry->next;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200171 }
172
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100173 if (err)
174 goto out;
Thomas Graf97defe12015-01-02 23:00:20 +0100175
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100176 new_hash = head_hashfn(ht, new_tbl, entry);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100177
Herbert Xu02fd97c2015-03-20 21:57:00 +1100178 new_bucket_lock = rht_bucket_lock(new_tbl, new_hash);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100179
Herbert Xu8f2484b2015-03-14 13:57:21 +1100180 spin_lock_nested(new_bucket_lock, SINGLE_DEPTH_NESTING);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100181 head = rht_dereference_bucket(new_tbl->buckets[new_hash],
182 new_tbl, new_hash);
183
184 if (rht_is_a_nulls(head))
185 INIT_RHT_NULLS_HEAD(entry->next, ht, new_hash);
186 else
187 RCU_INIT_POINTER(entry->next, head);
188
189 rcu_assign_pointer(new_tbl->buckets[new_hash], entry);
190 spin_unlock(new_bucket_lock);
191
192 rcu_assign_pointer(*pprev, next);
193
194out:
195 return err;
Thomas Graf97defe12015-01-02 23:00:20 +0100196}
197
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100198static void rhashtable_rehash_chain(struct rhashtable *ht, unsigned old_hash)
Thomas Graf97defe12015-01-02 23:00:20 +0100199{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100200 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
201 spinlock_t *old_bucket_lock;
Thomas Graf7cd10db2015-02-05 02:03:35 +0100202
Herbert Xu02fd97c2015-03-20 21:57:00 +1100203 old_bucket_lock = rht_bucket_lock(old_tbl, old_hash);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100204
205 spin_lock_bh(old_bucket_lock);
206 while (!rhashtable_rehash_one(ht, old_hash))
207 ;
Herbert Xu63d512d2015-03-14 13:57:24 +1100208 old_tbl->rehash++;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100209 spin_unlock_bh(old_bucket_lock);
210}
211
Herbert Xub8244782015-03-24 00:50:26 +1100212static int rhashtable_rehash_attach(struct rhashtable *ht,
213 struct bucket_table *old_tbl,
214 struct bucket_table *new_tbl)
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100215{
Herbert Xub8244782015-03-24 00:50:26 +1100216 /* Protect future_tbl using the first bucket lock. */
217 spin_lock_bh(old_tbl->locks);
218
219 /* Did somebody beat us to it? */
220 if (rcu_access_pointer(old_tbl->future_tbl)) {
221 spin_unlock_bh(old_tbl->locks);
222 return -EEXIST;
223 }
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100224
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100225 /* Make insertions go into the new, empty table right away. Deletions
226 * and lookups will be attempted in both tables until we synchronize.
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100227 */
Herbert Xuc4db8842015-03-14 13:57:25 +1100228 rcu_assign_pointer(old_tbl->future_tbl, new_tbl);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100229
Herbert Xu9497df82015-03-12 22:07:49 +1100230 /* Ensure the new table is visible to readers. */
231 smp_wmb();
232
Herbert Xub8244782015-03-24 00:50:26 +1100233 spin_unlock_bh(old_tbl->locks);
234
235 return 0;
236}
237
238static int rhashtable_rehash_table(struct rhashtable *ht)
239{
240 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
241 struct bucket_table *new_tbl;
242 struct rhashtable_walker *walker;
243 unsigned old_hash;
244
245 new_tbl = rht_dereference(old_tbl->future_tbl, ht);
246 if (!new_tbl)
247 return 0;
248
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100249 for (old_hash = 0; old_hash < old_tbl->size; old_hash++)
250 rhashtable_rehash_chain(ht, old_hash);
251
252 /* Publish the new table pointer. */
253 rcu_assign_pointer(ht->tbl, new_tbl);
254
Herbert Xueddee5ba2015-03-14 13:57:20 +1100255 list_for_each_entry(walker, &old_tbl->walkers, list)
256 walker->tbl = NULL;
257
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100258 /* Wait for readers. All new readers will see the new
259 * table, and thus no references to the old table will
260 * remain.
261 */
Herbert Xu9d901bc2015-03-14 13:57:23 +1100262 call_rcu(&old_tbl->rcu, bucket_table_free_rcu);
Herbert Xub8244782015-03-24 00:50:26 +1100263
264 return rht_dereference(new_tbl->future_tbl, ht) ? -EAGAIN : 0;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200265}
266
267/**
268 * rhashtable_expand - Expand hash table while allowing concurrent lookups
269 * @ht: the hash table to expand
Thomas Graf7e1e7762014-08-02 11:47:44 +0200270 *
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100271 * A secondary bucket array is allocated and the hash entries are migrated.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200272 *
273 * This function may only be called in a context where it is safe to call
274 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
275 *
Thomas Graf97defe12015-01-02 23:00:20 +0100276 * The caller must ensure that no concurrent resizing occurs by holding
277 * ht->mutex.
278 *
279 * It is valid to have concurrent insertions and deletions protected by per
280 * bucket locks or concurrent RCU protected lookups and traversals.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200281 */
Herbert Xub8244782015-03-24 00:50:26 +1100282static int rhashtable_expand(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200283{
284 struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
Herbert Xub8244782015-03-24 00:50:26 +1100285 int err;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200286
287 ASSERT_RHT_MUTEX(ht);
288
Herbert Xub8244782015-03-24 00:50:26 +1100289 old_tbl = rhashtable_last_table(ht, old_tbl);
290
Herbert Xu5269b532015-03-14 13:57:22 +1100291 new_tbl = bucket_table_alloc(ht, old_tbl->size * 2);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200292 if (new_tbl == NULL)
293 return -ENOMEM;
294
Herbert Xub8244782015-03-24 00:50:26 +1100295 err = rhashtable_rehash_attach(ht, old_tbl, new_tbl);
296 if (err)
297 bucket_table_free(new_tbl);
298
299 return err;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200300}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200301
302/**
303 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
304 * @ht: the hash table to shrink
Thomas Graf7e1e7762014-08-02 11:47:44 +0200305 *
Herbert Xu18093d12015-03-24 00:50:25 +1100306 * This function shrinks the hash table to fit, i.e., the smallest
307 * size would not cause it to expand right away automatically.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200308 *
Thomas Graf97defe12015-01-02 23:00:20 +0100309 * The caller must ensure that no concurrent resizing occurs by holding
310 * ht->mutex.
311 *
Thomas Graf7e1e7762014-08-02 11:47:44 +0200312 * The caller must ensure that no concurrent table mutations take place.
313 * It is however valid to have concurrent lookups if they are RCU protected.
Thomas Graf97defe12015-01-02 23:00:20 +0100314 *
315 * It is valid to have concurrent insertions and deletions protected by per
316 * bucket locks or concurrent RCU protected lookups and traversals.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200317 */
Herbert Xub8244782015-03-24 00:50:26 +1100318static int rhashtable_shrink(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200319{
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100320 struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
Herbert Xu18093d12015-03-24 00:50:25 +1100321 unsigned size = roundup_pow_of_two(atomic_read(&ht->nelems) * 3 / 2);
Herbert Xub8244782015-03-24 00:50:26 +1100322 int err;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200323
324 ASSERT_RHT_MUTEX(ht);
325
Herbert Xu18093d12015-03-24 00:50:25 +1100326 if (size < ht->p.min_size)
327 size = ht->p.min_size;
328
329 if (old_tbl->size <= size)
330 return 0;
331
Herbert Xub8244782015-03-24 00:50:26 +1100332 if (rht_dereference(old_tbl->future_tbl, ht))
333 return -EEXIST;
334
Herbert Xu18093d12015-03-24 00:50:25 +1100335 new_tbl = bucket_table_alloc(ht, size);
Thomas Graf97defe12015-01-02 23:00:20 +0100336 if (new_tbl == NULL)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200337 return -ENOMEM;
338
Herbert Xub8244782015-03-24 00:50:26 +1100339 err = rhashtable_rehash_attach(ht, old_tbl, new_tbl);
340 if (err)
341 bucket_table_free(new_tbl);
342
343 return err;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200344}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200345
Thomas Graf97defe12015-01-02 23:00:20 +0100346static void rht_deferred_worker(struct work_struct *work)
347{
348 struct rhashtable *ht;
349 struct bucket_table *tbl;
Herbert Xub8244782015-03-24 00:50:26 +1100350 int err = 0;
Thomas Graf97defe12015-01-02 23:00:20 +0100351
Ying Xue57699a42015-01-16 11:13:09 +0800352 ht = container_of(work, struct rhashtable, run_work);
Thomas Graf97defe12015-01-02 23:00:20 +0100353 mutex_lock(&ht->mutex);
Herbert Xu28134a52015-02-04 07:33:22 +1100354 if (ht->being_destroyed)
355 goto unlock;
356
Thomas Graf97defe12015-01-02 23:00:20 +0100357 tbl = rht_dereference(ht->tbl, ht);
Herbert Xub8244782015-03-24 00:50:26 +1100358 tbl = rhashtable_last_table(ht, tbl);
Thomas Graf97defe12015-01-02 23:00:20 +0100359
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100360 if (rht_grow_above_75(ht, tbl))
Thomas Graf97defe12015-01-02 23:00:20 +0100361 rhashtable_expand(ht);
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100362 else if (rht_shrink_below_30(ht, tbl))
Thomas Graf97defe12015-01-02 23:00:20 +0100363 rhashtable_shrink(ht);
Herbert Xub8244782015-03-24 00:50:26 +1100364
365 err = rhashtable_rehash_table(ht);
366
Herbert Xu28134a52015-02-04 07:33:22 +1100367unlock:
Thomas Graf97defe12015-01-02 23:00:20 +0100368 mutex_unlock(&ht->mutex);
Herbert Xub8244782015-03-24 00:50:26 +1100369
370 if (err)
371 schedule_work(&ht->run_work);
Thomas Graf97defe12015-01-02 23:00:20 +0100372}
373
Herbert Xu02fd97c2015-03-20 21:57:00 +1100374int rhashtable_insert_slow(struct rhashtable *ht, const void *key,
375 struct rhash_head *obj,
376 struct bucket_table *tbl)
377{
378 struct rhash_head *head;
379 unsigned hash;
380 int err = -EEXIST;
381
Herbert Xub8244782015-03-24 00:50:26 +1100382 tbl = rhashtable_last_table(ht, tbl);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100383 hash = head_hashfn(ht, tbl, obj);
384 spin_lock_nested(rht_bucket_lock(tbl, hash), SINGLE_DEPTH_NESTING);
385
386 if (key && rhashtable_lookup_fast(ht, key, ht->p))
387 goto exit;
388
389 err = 0;
390
391 head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash);
392
393 RCU_INIT_POINTER(obj->next, head);
394
395 rcu_assign_pointer(tbl->buckets[hash], obj);
396
397 atomic_inc(&ht->nelems);
398
399exit:
400 spin_unlock(rht_bucket_lock(tbl, hash));
401
402 return err;
403}
404EXPORT_SYMBOL_GPL(rhashtable_insert_slow);
405
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100406/**
407 * rhashtable_walk_init - Initialise an iterator
408 * @ht: Table to walk over
409 * @iter: Hash table Iterator
410 *
411 * This function prepares a hash table walk.
412 *
413 * Note that if you restart a walk after rhashtable_walk_stop you
414 * may see the same object twice. Also, you may miss objects if
415 * there are removals in between rhashtable_walk_stop and the next
416 * call to rhashtable_walk_start.
417 *
418 * For a completely stable walk you should construct your own data
419 * structure outside the hash table.
420 *
421 * This function may sleep so you must not call it from interrupt
422 * context or with spin locks held.
423 *
424 * You must call rhashtable_walk_exit if this function returns
425 * successfully.
426 */
427int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter)
428{
429 iter->ht = ht;
430 iter->p = NULL;
431 iter->slot = 0;
432 iter->skip = 0;
433
434 iter->walker = kmalloc(sizeof(*iter->walker), GFP_KERNEL);
435 if (!iter->walker)
436 return -ENOMEM;
437
438 mutex_lock(&ht->mutex);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100439 iter->walker->tbl = rht_dereference(ht->tbl, ht);
440 list_add(&iter->walker->list, &iter->walker->tbl->walkers);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100441 mutex_unlock(&ht->mutex);
442
443 return 0;
444}
445EXPORT_SYMBOL_GPL(rhashtable_walk_init);
446
447/**
448 * rhashtable_walk_exit - Free an iterator
449 * @iter: Hash table Iterator
450 *
451 * This function frees resources allocated by rhashtable_walk_init.
452 */
453void rhashtable_walk_exit(struct rhashtable_iter *iter)
454{
455 mutex_lock(&iter->ht->mutex);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100456 if (iter->walker->tbl)
457 list_del(&iter->walker->list);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100458 mutex_unlock(&iter->ht->mutex);
459 kfree(iter->walker);
460}
461EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
462
463/**
464 * rhashtable_walk_start - Start a hash table walk
465 * @iter: Hash table iterator
466 *
467 * Start a hash table walk. Note that we take the RCU lock in all
468 * cases including when we return an error. So you must always call
469 * rhashtable_walk_stop to clean up.
470 *
471 * Returns zero if successful.
472 *
473 * Returns -EAGAIN if resize event occured. Note that the iterator
474 * will rewind back to the beginning and you may use it immediately
475 * by calling rhashtable_walk_next.
476 */
477int rhashtable_walk_start(struct rhashtable_iter *iter)
Thomas Grafdb4374f2015-03-16 10:42:27 +0100478 __acquires(RCU)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100479{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100480 struct rhashtable *ht = iter->ht;
481
482 mutex_lock(&ht->mutex);
483
484 if (iter->walker->tbl)
485 list_del(&iter->walker->list);
486
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100487 rcu_read_lock();
488
Herbert Xueddee5ba2015-03-14 13:57:20 +1100489 mutex_unlock(&ht->mutex);
490
491 if (!iter->walker->tbl) {
492 iter->walker->tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100493 return -EAGAIN;
494 }
495
496 return 0;
497}
498EXPORT_SYMBOL_GPL(rhashtable_walk_start);
499
500/**
501 * rhashtable_walk_next - Return the next object and advance the iterator
502 * @iter: Hash table iterator
503 *
504 * Note that you must call rhashtable_walk_stop when you are finished
505 * with the walk.
506 *
507 * Returns the next object or NULL when the end of the table is reached.
508 *
509 * Returns -EAGAIN if resize event occured. Note that the iterator
510 * will rewind back to the beginning and you may continue to use it.
511 */
512void *rhashtable_walk_next(struct rhashtable_iter *iter)
513{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100514 struct bucket_table *tbl = iter->walker->tbl;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100515 struct rhashtable *ht = iter->ht;
516 struct rhash_head *p = iter->p;
517 void *obj = NULL;
518
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100519 if (p) {
520 p = rht_dereference_bucket_rcu(p->next, tbl, iter->slot);
521 goto next;
522 }
523
524 for (; iter->slot < tbl->size; iter->slot++) {
525 int skip = iter->skip;
526
527 rht_for_each_rcu(p, tbl, iter->slot) {
528 if (!skip)
529 break;
530 skip--;
531 }
532
533next:
534 if (!rht_is_a_nulls(p)) {
535 iter->skip++;
536 iter->p = p;
537 obj = rht_obj(ht, p);
538 goto out;
539 }
540
541 iter->skip = 0;
542 }
543
Herbert Xud88252f2015-03-24 00:50:19 +1100544 /* Ensure we see any new tables. */
545 smp_rmb();
546
Herbert Xuc4db8842015-03-14 13:57:25 +1100547 iter->walker->tbl = rht_dereference_rcu(tbl->future_tbl, ht);
548 if (iter->walker->tbl) {
Herbert Xueddee5ba2015-03-14 13:57:20 +1100549 iter->slot = 0;
550 iter->skip = 0;
551 return ERR_PTR(-EAGAIN);
552 }
553
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100554 iter->p = NULL;
555
556out:
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100557
558 return obj;
559}
560EXPORT_SYMBOL_GPL(rhashtable_walk_next);
561
562/**
563 * rhashtable_walk_stop - Finish a hash table walk
564 * @iter: Hash table iterator
565 *
566 * Finish a hash table walk.
567 */
568void rhashtable_walk_stop(struct rhashtable_iter *iter)
Thomas Grafdb4374f2015-03-16 10:42:27 +0100569 __releases(RCU)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100570{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100571 struct rhashtable *ht;
572 struct bucket_table *tbl = iter->walker->tbl;
573
Herbert Xueddee5ba2015-03-14 13:57:20 +1100574 if (!tbl)
Herbert Xu963ecbd2015-03-15 21:12:04 +1100575 goto out;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100576
577 ht = iter->ht;
578
579 mutex_lock(&ht->mutex);
Herbert Xuc4db8842015-03-14 13:57:25 +1100580 if (tbl->rehash < tbl->size)
Herbert Xueddee5ba2015-03-14 13:57:20 +1100581 list_add(&iter->walker->list, &tbl->walkers);
582 else
583 iter->walker->tbl = NULL;
584 mutex_unlock(&ht->mutex);
585
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100586 iter->p = NULL;
Herbert Xu963ecbd2015-03-15 21:12:04 +1100587
588out:
589 rcu_read_unlock();
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100590}
591EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
592
Herbert Xu488fb86e2015-03-20 21:56:59 +1100593static size_t rounded_hashtable_size(const struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200594{
Ying Xue94000172014-09-03 09:22:36 +0800595 return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
Herbert Xue2e21c12015-03-18 20:01:21 +1100596 (unsigned long)params->min_size);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200597}
598
Herbert Xu31ccde22015-03-24 00:50:21 +1100599static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed)
600{
601 return jhash2(key, length, seed);
602}
603
Thomas Graf7e1e7762014-08-02 11:47:44 +0200604/**
605 * rhashtable_init - initialize a new hash table
606 * @ht: hash table to be initialized
607 * @params: configuration parameters
608 *
609 * Initializes a new hash table based on the provided configuration
610 * parameters. A table can be configured either with a variable or
611 * fixed length key:
612 *
613 * Configuration Example 1: Fixed length keys
614 * struct test_obj {
615 * int key;
616 * void * my_member;
617 * struct rhash_head node;
618 * };
619 *
620 * struct rhashtable_params params = {
621 * .head_offset = offsetof(struct test_obj, node),
622 * .key_offset = offsetof(struct test_obj, key),
623 * .key_len = sizeof(int),
Daniel Borkmann87545892014-12-10 16:33:11 +0100624 * .hashfn = jhash,
Thomas Graff89bd6f2015-01-02 23:00:21 +0100625 * .nulls_base = (1U << RHT_BASE_SHIFT),
Thomas Graf7e1e7762014-08-02 11:47:44 +0200626 * };
627 *
628 * Configuration Example 2: Variable length keys
629 * struct test_obj {
630 * [...]
631 * struct rhash_head node;
632 * };
633 *
634 * u32 my_hash_fn(const void *data, u32 seed)
635 * {
636 * struct test_obj *obj = data;
637 *
638 * return [... hash ...];
639 * }
640 *
641 * struct rhashtable_params params = {
642 * .head_offset = offsetof(struct test_obj, node),
Daniel Borkmann87545892014-12-10 16:33:11 +0100643 * .hashfn = jhash,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200644 * .obj_hashfn = my_hash_fn,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200645 * };
646 */
Herbert Xu488fb86e2015-03-20 21:56:59 +1100647int rhashtable_init(struct rhashtable *ht,
648 const struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200649{
650 struct bucket_table *tbl;
651 size_t size;
652
653 size = HASH_DEFAULT_SIZE;
654
Herbert Xu31ccde22015-03-24 00:50:21 +1100655 if ((!params->key_len && !params->obj_hashfn) ||
Herbert Xu02fd97c2015-03-20 21:57:00 +1100656 (params->obj_hashfn && !params->obj_cmpfn))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200657 return -EINVAL;
658
Thomas Graff89bd6f2015-01-02 23:00:21 +0100659 if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT))
660 return -EINVAL;
661
Thomas Graf7e1e7762014-08-02 11:47:44 +0200662 if (params->nelem_hint)
Ying Xue94000172014-09-03 09:22:36 +0800663 size = rounded_hashtable_size(params);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200664
Thomas Graf97defe12015-01-02 23:00:20 +0100665 memset(ht, 0, sizeof(*ht));
666 mutex_init(&ht->mutex);
667 memcpy(&ht->p, params, sizeof(*params));
668
Thomas Grafa998f712015-03-19 22:31:13 +0000669 if (params->min_size)
670 ht->p.min_size = roundup_pow_of_two(params->min_size);
671
672 if (params->max_size)
673 ht->p.max_size = rounddown_pow_of_two(params->max_size);
674
Herbert Xu488fb86e2015-03-20 21:56:59 +1100675 ht->p.min_size = max(ht->p.min_size, HASH_MIN_SIZE);
Thomas Grafa998f712015-03-19 22:31:13 +0000676
Thomas Graf97defe12015-01-02 23:00:20 +0100677 if (params->locks_mul)
678 ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
679 else
680 ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
681
Herbert Xu31ccde22015-03-24 00:50:21 +1100682 ht->key_len = ht->p.key_len;
683 if (!params->hashfn) {
684 ht->p.hashfn = jhash;
685
686 if (!(ht->key_len & (sizeof(u32) - 1))) {
687 ht->key_len /= sizeof(u32);
688 ht->p.hashfn = rhashtable_jhash2;
689 }
690 }
691
Herbert Xu5269b532015-03-14 13:57:22 +1100692 tbl = bucket_table_alloc(ht, size);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200693 if (tbl == NULL)
694 return -ENOMEM;
695
Ying Xue545a1482015-01-07 13:41:57 +0800696 atomic_set(&ht->nelems, 0);
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100697
Thomas Graf7e1e7762014-08-02 11:47:44 +0200698 RCU_INIT_POINTER(ht->tbl, tbl);
699
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100700 INIT_WORK(&ht->run_work, rht_deferred_worker);
Thomas Graf97defe12015-01-02 23:00:20 +0100701
Thomas Graf7e1e7762014-08-02 11:47:44 +0200702 return 0;
703}
704EXPORT_SYMBOL_GPL(rhashtable_init);
705
706/**
707 * rhashtable_destroy - destroy hash table
708 * @ht: the hash table to destroy
709 *
Pablo Neira Ayusoae82ddc2014-09-02 00:26:05 +0200710 * Frees the bucket array. This function is not rcu safe, therefore the caller
711 * has to make sure that no resizing may happen by unpublishing the hashtable
712 * and waiting for the quiescent cycle before releasing the bucket array.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200713 */
Thomas Graf97defe12015-01-02 23:00:20 +0100714void rhashtable_destroy(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200715{
Thomas Graf97defe12015-01-02 23:00:20 +0100716 ht->being_destroyed = true;
717
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100718 cancel_work_sync(&ht->run_work);
Ying Xue57699a42015-01-16 11:13:09 +0800719
Thomas Graf97defe12015-01-02 23:00:20 +0100720 mutex_lock(&ht->mutex);
Thomas Graf97defe12015-01-02 23:00:20 +0100721 bucket_table_free(rht_dereference(ht->tbl, ht));
Thomas Graf97defe12015-01-02 23:00:20 +0100722 mutex_unlock(&ht->mutex);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200723}
724EXPORT_SYMBOL_GPL(rhashtable_destroy);