blob: 71fd0dd45ce35a219dbeb7edfc17833f577ce973 [file] [log] [blame]
Thomas Graf7e1e7762014-08-02 11:47:44 +02001/*
2 * Resizable, Scalable, Concurrent Hash Table
3 *
4 * Copyright (c) 2014 Thomas Graf <tgraf@suug.ch>
5 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
6 *
7 * Based on the following paper:
8 * https://www.usenix.org/legacy/event/atc11/tech/final_files/Triplett.pdf
9 *
10 * Code partially derived from nft_hash
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
17#include <linux/kernel.h>
18#include <linux/init.h>
19#include <linux/log2.h>
20#include <linux/slab.h>
21#include <linux/vmalloc.h>
22#include <linux/mm.h>
Daniel Borkmann87545892014-12-10 16:33:11 +010023#include <linux/jhash.h>
Thomas Graf7e1e7762014-08-02 11:47:44 +020024#include <linux/random.h>
25#include <linux/rhashtable.h>
Thomas Graf7e1e7762014-08-02 11:47:44 +020026
27#define HASH_DEFAULT_SIZE 64UL
28#define HASH_MIN_SIZE 4UL
Thomas Graf97defe12015-01-02 23:00:20 +010029#define BUCKET_LOCKS_PER_CPU 128UL
30
Thomas Graff89bd6f2015-01-02 23:00:21 +010031/* Base bits plus 1 bit for nulls marker */
32#define HASH_RESERVED_SPACE (RHT_BASE_BITS + 1)
33
Thomas Graf97defe12015-01-02 23:00:20 +010034enum {
35 RHT_LOCK_NORMAL,
36 RHT_LOCK_NESTED,
37 RHT_LOCK_NESTED2,
38};
39
40/* The bucket lock is selected based on the hash and protects mutations
41 * on a group of hash buckets.
42 *
43 * IMPORTANT: When holding the bucket lock of both the old and new table
44 * during expansions and shrinking, the old bucket lock must always be
45 * acquired first.
46 */
47static spinlock_t *bucket_lock(const struct bucket_table *tbl, u32 hash)
48{
49 return &tbl->locks[hash & tbl->locks_mask];
50}
Thomas Graf7e1e7762014-08-02 11:47:44 +020051
52#define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
Thomas Graf97defe12015-01-02 23:00:20 +010053#define ASSERT_BUCKET_LOCK(TBL, HASH) \
54 BUG_ON(!lockdep_rht_bucket_is_held(TBL, HASH))
Thomas Graf7e1e7762014-08-02 11:47:44 +020055
56#ifdef CONFIG_PROVE_LOCKING
Thomas Graf97defe12015-01-02 23:00:20 +010057int lockdep_rht_mutex_is_held(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +020058{
Thomas Graf97defe12015-01-02 23:00:20 +010059 return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1;
Thomas Graf7e1e7762014-08-02 11:47:44 +020060}
61EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held);
Thomas Graf88d6ed12015-01-02 23:00:16 +010062
63int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash)
64{
Thomas Graf97defe12015-01-02 23:00:20 +010065 spinlock_t *lock = bucket_lock(tbl, hash);
66
67 return (debug_locks) ? lockdep_is_held(lock) : 1;
Thomas Graf88d6ed12015-01-02 23:00:16 +010068}
69EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held);
Thomas Graf7e1e7762014-08-02 11:47:44 +020070#endif
71
Thomas Grafc91eee52014-08-13 16:38:30 +020072static void *rht_obj(const struct rhashtable *ht, const struct rhash_head *he)
Thomas Graf7e1e7762014-08-02 11:47:44 +020073{
74 return (void *) he - ht->p.head_offset;
75}
Thomas Graf7e1e7762014-08-02 11:47:44 +020076
Thomas Graf8d24c0b2015-01-02 23:00:14 +010077static u32 rht_bucket_index(const struct bucket_table *tbl, u32 hash)
Thomas Graf7e1e7762014-08-02 11:47:44 +020078{
Thomas Graf8d24c0b2015-01-02 23:00:14 +010079 return hash & (tbl->size - 1);
Thomas Graf7e1e7762014-08-02 11:47:44 +020080}
81
Thomas Graf8d24c0b2015-01-02 23:00:14 +010082static u32 obj_raw_hashfn(const struct rhashtable *ht, const void *ptr)
83{
84 u32 hash;
85
86 if (unlikely(!ht->p.key_len))
87 hash = ht->p.obj_hashfn(ptr, ht->p.hash_rnd);
88 else
89 hash = ht->p.hashfn(ptr + ht->p.key_offset, ht->p.key_len,
90 ht->p.hash_rnd);
91
Thomas Graff89bd6f2015-01-02 23:00:21 +010092 return hash >> HASH_RESERVED_SPACE;
Thomas Graf8d24c0b2015-01-02 23:00:14 +010093}
94
Thomas Graf97defe12015-01-02 23:00:20 +010095static u32 key_hashfn(struct rhashtable *ht, const void *key, u32 len)
Thomas Graf7e1e7762014-08-02 11:47:44 +020096{
Thomas Grafc88455c2015-02-05 02:03:31 +010097 return ht->p.hashfn(key, len, ht->p.hash_rnd) >> HASH_RESERVED_SPACE;
Thomas Graf7e1e7762014-08-02 11:47:44 +020098}
Thomas Graf7e1e7762014-08-02 11:47:44 +020099
100static u32 head_hashfn(const struct rhashtable *ht,
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100101 const struct bucket_table *tbl,
102 const struct rhash_head *he)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200103{
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100104 return rht_bucket_index(tbl, obj_raw_hashfn(ht, rht_obj(ht, he)));
Thomas Graf7e1e7762014-08-02 11:47:44 +0200105}
106
Thomas Grafb8e19432015-01-02 23:00:17 +0100107static struct rhash_head __rcu **bucket_tail(struct bucket_table *tbl, u32 n)
108{
109 struct rhash_head __rcu **pprev;
110
111 for (pprev = &tbl->buckets[n];
Thomas Graff89bd6f2015-01-02 23:00:21 +0100112 !rht_is_a_nulls(rht_dereference_bucket(*pprev, tbl, n));
Thomas Grafb8e19432015-01-02 23:00:17 +0100113 pprev = &rht_dereference_bucket(*pprev, tbl, n)->next)
114 ;
115
116 return pprev;
117}
118
Thomas Graf97defe12015-01-02 23:00:20 +0100119static int alloc_bucket_locks(struct rhashtable *ht, struct bucket_table *tbl)
120{
121 unsigned int i, size;
122#if defined(CONFIG_PROVE_LOCKING)
123 unsigned int nr_pcpus = 2;
124#else
125 unsigned int nr_pcpus = num_possible_cpus();
126#endif
127
128 nr_pcpus = min_t(unsigned int, nr_pcpus, 32UL);
129 size = roundup_pow_of_two(nr_pcpus * ht->p.locks_mul);
130
131 /* Never allocate more than one lock per bucket */
132 size = min_t(unsigned int, size, tbl->size);
133
134 if (sizeof(spinlock_t) != 0) {
135#ifdef CONFIG_NUMA
136 if (size * sizeof(spinlock_t) > PAGE_SIZE)
137 tbl->locks = vmalloc(size * sizeof(spinlock_t));
138 else
139#endif
140 tbl->locks = kmalloc_array(size, sizeof(spinlock_t),
141 GFP_KERNEL);
142 if (!tbl->locks)
143 return -ENOMEM;
144 for (i = 0; i < size; i++)
145 spin_lock_init(&tbl->locks[i]);
146 }
147 tbl->locks_mask = size - 1;
148
149 return 0;
150}
151
152static void bucket_table_free(const struct bucket_table *tbl)
153{
154 if (tbl)
155 kvfree(tbl->locks);
156
157 kvfree(tbl);
158}
159
160static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
161 size_t nbuckets)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200162{
163 struct bucket_table *tbl;
164 size_t size;
Thomas Graff89bd6f2015-01-02 23:00:21 +0100165 int i;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200166
167 size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]);
Thomas Graf6eba8222014-11-13 13:45:46 +0100168 tbl = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200169 if (tbl == NULL)
170 tbl = vzalloc(size);
171
172 if (tbl == NULL)
173 return NULL;
174
175 tbl->size = nbuckets;
176
Thomas Graf97defe12015-01-02 23:00:20 +0100177 if (alloc_bucket_locks(ht, tbl) < 0) {
178 bucket_table_free(tbl);
179 return NULL;
180 }
Thomas Graf7e1e7762014-08-02 11:47:44 +0200181
Thomas Graff89bd6f2015-01-02 23:00:21 +0100182 for (i = 0; i < nbuckets; i++)
183 INIT_RHT_NULLS_HEAD(tbl->buckets[i], ht, i);
184
Thomas Graf97defe12015-01-02 23:00:20 +0100185 return tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200186}
187
188/**
189 * rht_grow_above_75 - returns true if nelems > 0.75 * table-size
190 * @ht: hash table
191 * @new_size: new table size
192 */
193bool rht_grow_above_75(const struct rhashtable *ht, size_t new_size)
194{
195 /* Expand table when exceeding 75% load */
Ying Xuec0c09bf2015-01-07 13:41:56 +0800196 return atomic_read(&ht->nelems) > (new_size / 4 * 3) &&
197 (ht->p.max_shift && atomic_read(&ht->shift) < ht->p.max_shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200198}
199EXPORT_SYMBOL_GPL(rht_grow_above_75);
200
201/**
202 * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size
203 * @ht: hash table
204 * @new_size: new table size
205 */
206bool rht_shrink_below_30(const struct rhashtable *ht, size_t new_size)
207{
208 /* Shrink table beneath 30% load */
Ying Xuec0c09bf2015-01-07 13:41:56 +0800209 return atomic_read(&ht->nelems) < (new_size * 3 / 10) &&
210 (atomic_read(&ht->shift) > ht->p.min_shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200211}
212EXPORT_SYMBOL_GPL(rht_shrink_below_30);
213
214static void hashtable_chain_unzip(const struct rhashtable *ht,
215 const struct bucket_table *new_tbl,
Thomas Graf97defe12015-01-02 23:00:20 +0100216 struct bucket_table *old_tbl,
217 size_t old_hash)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200218{
219 struct rhash_head *he, *p, *next;
Thomas Graf97defe12015-01-02 23:00:20 +0100220 spinlock_t *new_bucket_lock, *new_bucket_lock2 = NULL;
221 unsigned int new_hash, new_hash2;
222
223 ASSERT_BUCKET_LOCK(old_tbl, old_hash);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200224
225 /* Old bucket empty, no work needed. */
Thomas Graf97defe12015-01-02 23:00:20 +0100226 p = rht_dereference_bucket(old_tbl->buckets[old_hash], old_tbl,
227 old_hash);
Thomas Graff89bd6f2015-01-02 23:00:21 +0100228 if (rht_is_a_nulls(p))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200229 return;
230
Thomas Graf97defe12015-01-02 23:00:20 +0100231 new_hash = new_hash2 = head_hashfn(ht, new_tbl, p);
232 new_bucket_lock = bucket_lock(new_tbl, new_hash);
233
Thomas Graf7e1e7762014-08-02 11:47:44 +0200234 /* Advance the old bucket pointer one or more times until it
235 * reaches a node that doesn't hash to the same bucket as the
236 * previous node p. Call the previous node p;
237 */
Thomas Graf97defe12015-01-02 23:00:20 +0100238 rht_for_each_continue(he, p->next, old_tbl, old_hash) {
239 new_hash2 = head_hashfn(ht, new_tbl, he);
240 if (new_hash != new_hash2)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200241 break;
242 p = he;
243 }
Thomas Graf97defe12015-01-02 23:00:20 +0100244 rcu_assign_pointer(old_tbl->buckets[old_hash], p->next);
245
246 spin_lock_bh_nested(new_bucket_lock, RHT_LOCK_NESTED);
247
248 /* If we have encountered an entry that maps to a different bucket in
249 * the new table, lock down that bucket as well as we might cut off
250 * the end of the chain.
251 */
252 new_bucket_lock2 = bucket_lock(new_tbl, new_hash);
253 if (new_bucket_lock != new_bucket_lock2)
254 spin_lock_bh_nested(new_bucket_lock2, RHT_LOCK_NESTED2);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200255
256 /* Find the subsequent node which does hash to the same
257 * bucket as node P, or NULL if no such node exists.
258 */
Thomas Graff89bd6f2015-01-02 23:00:21 +0100259 INIT_RHT_NULLS_HEAD(next, ht, old_hash);
260 if (!rht_is_a_nulls(he)) {
Thomas Graf97defe12015-01-02 23:00:20 +0100261 rht_for_each_continue(he, he->next, old_tbl, old_hash) {
262 if (head_hashfn(ht, new_tbl, he) == new_hash) {
Thomas Graf7e1e7762014-08-02 11:47:44 +0200263 next = he;
264 break;
265 }
266 }
267 }
268
269 /* Set p's next pointer to that subsequent node pointer,
270 * bypassing the nodes which do not hash to p's bucket
271 */
Thomas Graf97defe12015-01-02 23:00:20 +0100272 rcu_assign_pointer(p->next, next);
273
274 if (new_bucket_lock != new_bucket_lock2)
275 spin_unlock_bh(new_bucket_lock2);
276 spin_unlock_bh(new_bucket_lock);
277}
278
279static void link_old_to_new(struct bucket_table *new_tbl,
280 unsigned int new_hash, struct rhash_head *entry)
281{
282 spinlock_t *new_bucket_lock;
283
284 new_bucket_lock = bucket_lock(new_tbl, new_hash);
285
286 spin_lock_bh_nested(new_bucket_lock, RHT_LOCK_NESTED);
287 rcu_assign_pointer(*bucket_tail(new_tbl, new_hash), entry);
288 spin_unlock_bh(new_bucket_lock);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200289}
290
291/**
292 * rhashtable_expand - Expand hash table while allowing concurrent lookups
293 * @ht: the hash table to expand
Thomas Graf7e1e7762014-08-02 11:47:44 +0200294 *
295 * A secondary bucket array is allocated and the hash entries are migrated
296 * while keeping them on both lists until the end of the RCU grace period.
297 *
298 * This function may only be called in a context where it is safe to call
299 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
300 *
Thomas Graf97defe12015-01-02 23:00:20 +0100301 * The caller must ensure that no concurrent resizing occurs by holding
302 * ht->mutex.
303 *
304 * It is valid to have concurrent insertions and deletions protected by per
305 * bucket locks or concurrent RCU protected lookups and traversals.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200306 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100307int rhashtable_expand(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200308{
309 struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
310 struct rhash_head *he;
Thomas Graf97defe12015-01-02 23:00:20 +0100311 spinlock_t *old_bucket_lock;
312 unsigned int new_hash, old_hash;
313 bool complete = false;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200314
315 ASSERT_RHT_MUTEX(ht);
316
Thomas Graf97defe12015-01-02 23:00:20 +0100317 new_tbl = bucket_table_alloc(ht, old_tbl->size * 2);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200318 if (new_tbl == NULL)
319 return -ENOMEM;
320
Ying Xuec0c09bf2015-01-07 13:41:56 +0800321 atomic_inc(&ht->shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200322
Thomas Graf97defe12015-01-02 23:00:20 +0100323 /* Make insertions go into the new, empty table right away. Deletions
324 * and lookups will be attempted in both tables until we synchronize.
325 * The synchronize_rcu() guarantees for the new table to be picked up
326 * so no new additions go into the old table while we relink.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200327 */
Thomas Graf97defe12015-01-02 23:00:20 +0100328 rcu_assign_pointer(ht->future_tbl, new_tbl);
329 synchronize_rcu();
330
331 /* For each new bucket, search the corresponding old bucket for the
332 * first entry that hashes to the new bucket, and link the end of
333 * newly formed bucket chain (containing entries added to future
334 * table) to that entry. Since all the entries which will end up in
335 * the new bucket appear in the same old bucket, this constructs an
336 * entirely valid new hash table, but with multiple buckets
337 * "zipped" together into a single imprecise chain.
338 */
339 for (new_hash = 0; new_hash < new_tbl->size; new_hash++) {
340 old_hash = rht_bucket_index(old_tbl, new_hash);
341 old_bucket_lock = bucket_lock(old_tbl, old_hash);
342
343 spin_lock_bh(old_bucket_lock);
344 rht_for_each(he, old_tbl, old_hash) {
345 if (head_hashfn(ht, new_tbl, he) == new_hash) {
346 link_old_to_new(new_tbl, new_hash, he);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200347 break;
348 }
349 }
Thomas Graf97defe12015-01-02 23:00:20 +0100350 spin_unlock_bh(old_bucket_lock);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200351 }
352
353 /* Publish the new table pointer. Lookups may now traverse
Herbert Xu0c828f22014-11-13 13:10:48 +0800354 * the new table, but they will not benefit from any
355 * additional efficiency until later steps unzip the buckets.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200356 */
357 rcu_assign_pointer(ht->tbl, new_tbl);
358
359 /* Unzip interleaved hash chains */
Thomas Graf97defe12015-01-02 23:00:20 +0100360 while (!complete && !ht->being_destroyed) {
Thomas Graf7e1e7762014-08-02 11:47:44 +0200361 /* Wait for readers. All new readers will see the new
362 * table, and thus no references to the old table will
363 * remain.
364 */
365 synchronize_rcu();
366
367 /* For each bucket in the old table (each of which
368 * contains items from multiple buckets of the new
369 * table): ...
370 */
371 complete = true;
Thomas Graf97defe12015-01-02 23:00:20 +0100372 for (old_hash = 0; old_hash < old_tbl->size; old_hash++) {
Thomas Graff89bd6f2015-01-02 23:00:21 +0100373 struct rhash_head *head;
374
Thomas Graf97defe12015-01-02 23:00:20 +0100375 old_bucket_lock = bucket_lock(old_tbl, old_hash);
376 spin_lock_bh(old_bucket_lock);
377
378 hashtable_chain_unzip(ht, new_tbl, old_tbl, old_hash);
Thomas Graff89bd6f2015-01-02 23:00:21 +0100379 head = rht_dereference_bucket(old_tbl->buckets[old_hash],
380 old_tbl, old_hash);
381 if (!rht_is_a_nulls(head))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200382 complete = false;
Thomas Graf97defe12015-01-02 23:00:20 +0100383
384 spin_unlock_bh(old_bucket_lock);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200385 }
Thomas Graf97defe12015-01-02 23:00:20 +0100386 }
Thomas Graf7e1e7762014-08-02 11:47:44 +0200387
388 bucket_table_free(old_tbl);
389 return 0;
390}
391EXPORT_SYMBOL_GPL(rhashtable_expand);
392
393/**
394 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
395 * @ht: the hash table to shrink
Thomas Graf7e1e7762014-08-02 11:47:44 +0200396 *
397 * This function may only be called in a context where it is safe to call
398 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
399 *
Thomas Graf97defe12015-01-02 23:00:20 +0100400 * The caller must ensure that no concurrent resizing occurs by holding
401 * ht->mutex.
402 *
Thomas Graf7e1e7762014-08-02 11:47:44 +0200403 * The caller must ensure that no concurrent table mutations take place.
404 * It is however valid to have concurrent lookups if they are RCU protected.
Thomas Graf97defe12015-01-02 23:00:20 +0100405 *
406 * It is valid to have concurrent insertions and deletions protected by per
407 * bucket locks or concurrent RCU protected lookups and traversals.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200408 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100409int rhashtable_shrink(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200410{
Thomas Graf97defe12015-01-02 23:00:20 +0100411 struct bucket_table *new_tbl, *tbl = rht_dereference(ht->tbl, ht);
412 spinlock_t *new_bucket_lock, *old_bucket_lock1, *old_bucket_lock2;
413 unsigned int new_hash;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200414
415 ASSERT_RHT_MUTEX(ht);
416
Thomas Graf97defe12015-01-02 23:00:20 +0100417 new_tbl = bucket_table_alloc(ht, tbl->size / 2);
418 if (new_tbl == NULL)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200419 return -ENOMEM;
420
Thomas Graf97defe12015-01-02 23:00:20 +0100421 rcu_assign_pointer(ht->future_tbl, new_tbl);
422 synchronize_rcu();
Thomas Graf7e1e7762014-08-02 11:47:44 +0200423
Thomas Graf97defe12015-01-02 23:00:20 +0100424 /* Link the first entry in the old bucket to the end of the
425 * bucket in the new table. As entries are concurrently being
426 * added to the new table, lock down the new bucket. As we
427 * always divide the size in half when shrinking, each bucket
428 * in the new table maps to exactly two buckets in the old
429 * table.
430 *
431 * As removals can occur concurrently on the old table, we need
432 * to lock down both matching buckets in the old table.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200433 */
Thomas Graf97defe12015-01-02 23:00:20 +0100434 for (new_hash = 0; new_hash < new_tbl->size; new_hash++) {
435 old_bucket_lock1 = bucket_lock(tbl, new_hash);
436 old_bucket_lock2 = bucket_lock(tbl, new_hash + new_tbl->size);
437 new_bucket_lock = bucket_lock(new_tbl, new_hash);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200438
Thomas Graf97defe12015-01-02 23:00:20 +0100439 spin_lock_bh(old_bucket_lock1);
Thomas Graf80ca8c32015-01-12 23:58:21 +0000440
441 /* Depending on the lock per buckets mapping, the bucket in
442 * the lower and upper region may map to the same lock.
443 */
444 if (old_bucket_lock1 != old_bucket_lock2) {
445 spin_lock_bh_nested(old_bucket_lock2, RHT_LOCK_NESTED);
446 spin_lock_bh_nested(new_bucket_lock, RHT_LOCK_NESTED2);
447 } else {
448 spin_lock_bh_nested(new_bucket_lock, RHT_LOCK_NESTED);
449 }
Thomas Graf97defe12015-01-02 23:00:20 +0100450
451 rcu_assign_pointer(*bucket_tail(new_tbl, new_hash),
452 tbl->buckets[new_hash]);
453 rcu_assign_pointer(*bucket_tail(new_tbl, new_hash),
454 tbl->buckets[new_hash + new_tbl->size]);
455
456 spin_unlock_bh(new_bucket_lock);
Thomas Graf80ca8c32015-01-12 23:58:21 +0000457 if (old_bucket_lock1 != old_bucket_lock2)
458 spin_unlock_bh(old_bucket_lock2);
Thomas Graf97defe12015-01-02 23:00:20 +0100459 spin_unlock_bh(old_bucket_lock1);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200460 }
461
462 /* Publish the new, valid hash table */
Thomas Graf97defe12015-01-02 23:00:20 +0100463 rcu_assign_pointer(ht->tbl, new_tbl);
Ying Xuec0c09bf2015-01-07 13:41:56 +0800464 atomic_dec(&ht->shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200465
466 /* Wait for readers. No new readers will have references to the
467 * old hash table.
468 */
469 synchronize_rcu();
470
471 bucket_table_free(tbl);
472
473 return 0;
474}
475EXPORT_SYMBOL_GPL(rhashtable_shrink);
476
Thomas Graf97defe12015-01-02 23:00:20 +0100477static void rht_deferred_worker(struct work_struct *work)
478{
479 struct rhashtable *ht;
480 struct bucket_table *tbl;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100481 struct rhashtable_walker *walker;
Thomas Graf97defe12015-01-02 23:00:20 +0100482
Ying Xue57699a42015-01-16 11:13:09 +0800483 ht = container_of(work, struct rhashtable, run_work);
Thomas Graf97defe12015-01-02 23:00:20 +0100484 mutex_lock(&ht->mutex);
Herbert Xu28134a52015-02-04 07:33:22 +1100485 if (ht->being_destroyed)
486 goto unlock;
487
Thomas Graf97defe12015-01-02 23:00:20 +0100488 tbl = rht_dereference(ht->tbl, ht);
489
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100490 list_for_each_entry(walker, &ht->walkers, list)
491 walker->resize = true;
492
Thomas Graf97defe12015-01-02 23:00:20 +0100493 if (ht->p.grow_decision && ht->p.grow_decision(ht, tbl->size))
494 rhashtable_expand(ht);
495 else if (ht->p.shrink_decision && ht->p.shrink_decision(ht, tbl->size))
496 rhashtable_shrink(ht);
497
Herbert Xu28134a52015-02-04 07:33:22 +1100498unlock:
Thomas Graf97defe12015-01-02 23:00:20 +0100499 mutex_unlock(&ht->mutex);
500}
501
Ying Xue54c5b7d2015-01-07 13:41:53 +0800502static void rhashtable_wakeup_worker(struct rhashtable *ht)
503{
504 struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht);
505 struct bucket_table *new_tbl = rht_dereference_rcu(ht->future_tbl, ht);
506 size_t size = tbl->size;
507
508 /* Only adjust the table if no resizing is currently in progress. */
509 if (tbl == new_tbl &&
510 ((ht->p.grow_decision && ht->p.grow_decision(ht, size)) ||
511 (ht->p.shrink_decision && ht->p.shrink_decision(ht, size))))
Ying Xue57699a42015-01-16 11:13:09 +0800512 schedule_work(&ht->run_work);
Ying Xue54c5b7d2015-01-07 13:41:53 +0800513}
514
Ying Xuedb304852015-01-07 13:41:54 +0800515static void __rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj,
516 struct bucket_table *tbl, u32 hash)
517{
518 struct rhash_head *head = rht_dereference_bucket(tbl->buckets[hash],
519 tbl, hash);
520
521 if (rht_is_a_nulls(head))
522 INIT_RHT_NULLS_HEAD(obj->next, ht, hash);
523 else
524 RCU_INIT_POINTER(obj->next, head);
525
526 rcu_assign_pointer(tbl->buckets[hash], obj);
527
528 atomic_inc(&ht->nelems);
529
530 rhashtable_wakeup_worker(ht);
531}
532
Thomas Graf7e1e7762014-08-02 11:47:44 +0200533/**
Ying Xuedb304852015-01-07 13:41:54 +0800534 * rhashtable_insert - insert object into hash table
Thomas Graf7e1e7762014-08-02 11:47:44 +0200535 * @ht: hash table
536 * @obj: pointer to hash head inside object
Thomas Graf7e1e7762014-08-02 11:47:44 +0200537 *
Thomas Graf97defe12015-01-02 23:00:20 +0100538 * Will take a per bucket spinlock to protect against mutual mutations
539 * on the same bucket. Multiple insertions may occur in parallel unless
540 * they map to the same bucket lock.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200541 *
Thomas Graf97defe12015-01-02 23:00:20 +0100542 * It is safe to call this function from atomic context.
543 *
544 * Will trigger an automatic deferred table resizing if the size grows
545 * beyond the watermark indicated by grow_decision() which can be passed
546 * to rhashtable_init().
Thomas Graf7e1e7762014-08-02 11:47:44 +0200547 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100548void rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200549{
Thomas Graf97defe12015-01-02 23:00:20 +0100550 struct bucket_table *tbl;
551 spinlock_t *lock;
552 unsigned hash;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200553
Thomas Graf97defe12015-01-02 23:00:20 +0100554 rcu_read_lock();
Thomas Graf7e1e7762014-08-02 11:47:44 +0200555
Thomas Graf97defe12015-01-02 23:00:20 +0100556 tbl = rht_dereference_rcu(ht->future_tbl, ht);
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100557 hash = head_hashfn(ht, tbl, obj);
Thomas Graf97defe12015-01-02 23:00:20 +0100558 lock = bucket_lock(tbl, hash);
559
560 spin_lock_bh(lock);
Ying Xuedb304852015-01-07 13:41:54 +0800561 __rhashtable_insert(ht, obj, tbl, hash);
Thomas Graf97defe12015-01-02 23:00:20 +0100562 spin_unlock_bh(lock);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200563
Thomas Graf97defe12015-01-02 23:00:20 +0100564 rcu_read_unlock();
Thomas Graf7e1e7762014-08-02 11:47:44 +0200565}
566EXPORT_SYMBOL_GPL(rhashtable_insert);
567
568/**
Thomas Graf7e1e7762014-08-02 11:47:44 +0200569 * rhashtable_remove - remove object from hash table
570 * @ht: hash table
571 * @obj: pointer to hash head inside object
Thomas Graf7e1e7762014-08-02 11:47:44 +0200572 *
573 * Since the hash chain is single linked, the removal operation needs to
574 * walk the bucket chain upon removal. The removal operation is thus
575 * considerable slow if the hash table is not correctly sized.
576 *
Ying Xuedb304852015-01-07 13:41:54 +0800577 * Will automatically shrink the table via rhashtable_expand() if the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200578 * shrink_decision function specified at rhashtable_init() returns true.
579 *
580 * The caller must ensure that no concurrent table mutations occur. It is
581 * however valid to have concurrent lookups if they are RCU protected.
582 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100583bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *obj)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200584{
Thomas Graf97defe12015-01-02 23:00:20 +0100585 struct bucket_table *tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200586 struct rhash_head __rcu **pprev;
587 struct rhash_head *he;
Thomas Graf97defe12015-01-02 23:00:20 +0100588 spinlock_t *lock;
589 unsigned int hash;
Thomas Graffe6a0432015-01-21 11:54:01 +0000590 bool ret = false;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200591
Thomas Graf97defe12015-01-02 23:00:20 +0100592 rcu_read_lock();
593 tbl = rht_dereference_rcu(ht->tbl, ht);
594 hash = head_hashfn(ht, tbl, obj);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200595
Thomas Graf97defe12015-01-02 23:00:20 +0100596 lock = bucket_lock(tbl, hash);
597 spin_lock_bh(lock);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200598
Thomas Graf97defe12015-01-02 23:00:20 +0100599restart:
600 pprev = &tbl->buckets[hash];
601 rht_for_each(he, tbl, hash) {
Thomas Graf7e1e7762014-08-02 11:47:44 +0200602 if (he != obj) {
603 pprev = &he->next;
604 continue;
605 }
606
Thomas Graf97defe12015-01-02 23:00:20 +0100607 rcu_assign_pointer(*pprev, obj->next);
Thomas Graf897362e2015-01-02 23:00:18 +0100608
Thomas Graffe6a0432015-01-21 11:54:01 +0000609 ret = true;
610 break;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200611 }
612
Thomas Graffe6a0432015-01-21 11:54:01 +0000613 /* The entry may be linked in either 'tbl', 'future_tbl', or both.
614 * 'future_tbl' only exists for a short period of time during
615 * resizing. Thus traversing both is fine and the added cost is
616 * very rare.
617 */
Ying Xuebd6d4db2015-01-07 13:41:55 +0800618 if (tbl != rht_dereference_rcu(ht->future_tbl, ht)) {
Thomas Graf97defe12015-01-02 23:00:20 +0100619 spin_unlock_bh(lock);
620
Ying Xuebd6d4db2015-01-07 13:41:55 +0800621 tbl = rht_dereference_rcu(ht->future_tbl, ht);
Thomas Graf97defe12015-01-02 23:00:20 +0100622 hash = head_hashfn(ht, tbl, obj);
623
624 lock = bucket_lock(tbl, hash);
625 spin_lock_bh(lock);
626 goto restart;
627 }
628
629 spin_unlock_bh(lock);
Thomas Graffe6a0432015-01-21 11:54:01 +0000630
631 if (ret) {
632 atomic_dec(&ht->nelems);
633 rhashtable_wakeup_worker(ht);
634 }
635
Thomas Graf97defe12015-01-02 23:00:20 +0100636 rcu_read_unlock();
637
Thomas Graffe6a0432015-01-21 11:54:01 +0000638 return ret;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200639}
640EXPORT_SYMBOL_GPL(rhashtable_remove);
641
Ying Xueefb975a62015-01-07 13:41:52 +0800642struct rhashtable_compare_arg {
643 struct rhashtable *ht;
644 const void *key;
645};
646
647static bool rhashtable_compare(void *ptr, void *arg)
648{
649 struct rhashtable_compare_arg *x = arg;
650 struct rhashtable *ht = x->ht;
651
652 return !memcmp(ptr + ht->p.key_offset, x->key, ht->p.key_len);
653}
654
Thomas Graf7e1e7762014-08-02 11:47:44 +0200655/**
656 * rhashtable_lookup - lookup key in hash table
657 * @ht: hash table
658 * @key: pointer to key
659 *
660 * Computes the hash value for the key and traverses the bucket chain looking
661 * for a entry with an identical key. The first matching entry is returned.
662 *
663 * This lookup function may only be used for fixed key hash table (key_len
Ying Xuedb304852015-01-07 13:41:54 +0800664 * parameter set). It will BUG() if used inappropriately.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200665 *
Thomas Graf97defe12015-01-02 23:00:20 +0100666 * Lookups may occur in parallel with hashtable mutations and resizing.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200667 */
Thomas Graf97defe12015-01-02 23:00:20 +0100668void *rhashtable_lookup(struct rhashtable *ht, const void *key)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200669{
Ying Xueefb975a62015-01-07 13:41:52 +0800670 struct rhashtable_compare_arg arg = {
671 .ht = ht,
672 .key = key,
673 };
Thomas Graf7e1e7762014-08-02 11:47:44 +0200674
675 BUG_ON(!ht->p.key_len);
676
Ying Xueefb975a62015-01-07 13:41:52 +0800677 return rhashtable_lookup_compare(ht, key, &rhashtable_compare, &arg);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200678}
679EXPORT_SYMBOL_GPL(rhashtable_lookup);
680
681/**
682 * rhashtable_lookup_compare - search hash table with compare function
683 * @ht: hash table
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100684 * @key: the pointer to the key
Thomas Graf7e1e7762014-08-02 11:47:44 +0200685 * @compare: compare function, must return true on match
686 * @arg: argument passed on to compare function
687 *
688 * Traverses the bucket chain behind the provided hash value and calls the
689 * specified compare function for each entry.
690 *
Thomas Graf97defe12015-01-02 23:00:20 +0100691 * Lookups may occur in parallel with hashtable mutations and resizing.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200692 *
693 * Returns the first entry on which the compare function returned true.
694 */
Thomas Graf97defe12015-01-02 23:00:20 +0100695void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200696 bool (*compare)(void *, void *), void *arg)
697{
Thomas Graf97defe12015-01-02 23:00:20 +0100698 const struct bucket_table *tbl, *old_tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200699 struct rhash_head *he;
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100700 u32 hash;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200701
Thomas Graf97defe12015-01-02 23:00:20 +0100702 rcu_read_lock();
703
704 old_tbl = rht_dereference_rcu(ht->tbl, ht);
705 tbl = rht_dereference_rcu(ht->future_tbl, ht);
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100706 hash = key_hashfn(ht, key, ht->p.key_len);
Thomas Graf97defe12015-01-02 23:00:20 +0100707restart:
708 rht_for_each_rcu(he, tbl, rht_bucket_index(tbl, hash)) {
Thomas Graf7e1e7762014-08-02 11:47:44 +0200709 if (!compare(rht_obj(ht, he), arg))
710 continue;
Thomas Graf97defe12015-01-02 23:00:20 +0100711 rcu_read_unlock();
Thomas Grafa4b18cd2015-01-02 23:00:15 +0100712 return rht_obj(ht, he);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200713 }
714
Thomas Graf97defe12015-01-02 23:00:20 +0100715 if (unlikely(tbl != old_tbl)) {
716 tbl = old_tbl;
717 goto restart;
718 }
719 rcu_read_unlock();
720
Thomas Graf7e1e7762014-08-02 11:47:44 +0200721 return NULL;
722}
723EXPORT_SYMBOL_GPL(rhashtable_lookup_compare);
724
Ying Xuedb304852015-01-07 13:41:54 +0800725/**
726 * rhashtable_lookup_insert - lookup and insert object into hash table
727 * @ht: hash table
728 * @obj: pointer to hash head inside object
729 *
730 * Locks down the bucket chain in both the old and new table if a resize
731 * is in progress to ensure that writers can't remove from the old table
732 * and can't insert to the new table during the atomic operation of search
733 * and insertion. Searches for duplicates in both the old and new table if
734 * a resize is in progress.
735 *
736 * This lookup function may only be used for fixed key hash table (key_len
737 * parameter set). It will BUG() if used inappropriately.
738 *
739 * It is safe to call this function from atomic context.
740 *
741 * Will trigger an automatic deferred table resizing if the size grows
742 * beyond the watermark indicated by grow_decision() which can be passed
743 * to rhashtable_init().
744 */
745bool rhashtable_lookup_insert(struct rhashtable *ht, struct rhash_head *obj)
746{
Ying Xue7a868d12015-01-12 14:52:22 +0800747 struct rhashtable_compare_arg arg = {
748 .ht = ht,
749 .key = rht_obj(ht, obj) + ht->p.key_offset,
750 };
751
752 BUG_ON(!ht->p.key_len);
753
754 return rhashtable_lookup_compare_insert(ht, obj, &rhashtable_compare,
755 &arg);
756}
757EXPORT_SYMBOL_GPL(rhashtable_lookup_insert);
758
759/**
760 * rhashtable_lookup_compare_insert - search and insert object to hash table
761 * with compare function
762 * @ht: hash table
763 * @obj: pointer to hash head inside object
764 * @compare: compare function, must return true on match
765 * @arg: argument passed on to compare function
766 *
767 * Locks down the bucket chain in both the old and new table if a resize
768 * is in progress to ensure that writers can't remove from the old table
769 * and can't insert to the new table during the atomic operation of search
770 * and insertion. Searches for duplicates in both the old and new table if
771 * a resize is in progress.
772 *
773 * Lookups may occur in parallel with hashtable mutations and resizing.
774 *
775 * Will trigger an automatic deferred table resizing if the size grows
776 * beyond the watermark indicated by grow_decision() which can be passed
777 * to rhashtable_init().
778 */
779bool rhashtable_lookup_compare_insert(struct rhashtable *ht,
780 struct rhash_head *obj,
781 bool (*compare)(void *, void *),
782 void *arg)
783{
Ying Xuedb304852015-01-07 13:41:54 +0800784 struct bucket_table *new_tbl, *old_tbl;
785 spinlock_t *new_bucket_lock, *old_bucket_lock;
786 u32 new_hash, old_hash;
787 bool success = true;
788
789 BUG_ON(!ht->p.key_len);
790
791 rcu_read_lock();
792
793 old_tbl = rht_dereference_rcu(ht->tbl, ht);
794 old_hash = head_hashfn(ht, old_tbl, obj);
795 old_bucket_lock = bucket_lock(old_tbl, old_hash);
796 spin_lock_bh(old_bucket_lock);
797
798 new_tbl = rht_dereference_rcu(ht->future_tbl, ht);
799 new_hash = head_hashfn(ht, new_tbl, obj);
800 new_bucket_lock = bucket_lock(new_tbl, new_hash);
801 if (unlikely(old_tbl != new_tbl))
802 spin_lock_bh_nested(new_bucket_lock, RHT_LOCK_NESTED);
803
Ying Xue7a868d12015-01-12 14:52:22 +0800804 if (rhashtable_lookup_compare(ht, rht_obj(ht, obj) + ht->p.key_offset,
805 compare, arg)) {
Ying Xuedb304852015-01-07 13:41:54 +0800806 success = false;
807 goto exit;
808 }
809
810 __rhashtable_insert(ht, obj, new_tbl, new_hash);
811
812exit:
813 if (unlikely(old_tbl != new_tbl))
814 spin_unlock_bh(new_bucket_lock);
815 spin_unlock_bh(old_bucket_lock);
816
817 rcu_read_unlock();
818
819 return success;
820}
Ying Xue7a868d12015-01-12 14:52:22 +0800821EXPORT_SYMBOL_GPL(rhashtable_lookup_compare_insert);
Ying Xuedb304852015-01-07 13:41:54 +0800822
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100823/**
824 * rhashtable_walk_init - Initialise an iterator
825 * @ht: Table to walk over
826 * @iter: Hash table Iterator
827 *
828 * This function prepares a hash table walk.
829 *
830 * Note that if you restart a walk after rhashtable_walk_stop you
831 * may see the same object twice. Also, you may miss objects if
832 * there are removals in between rhashtable_walk_stop and the next
833 * call to rhashtable_walk_start.
834 *
835 * For a completely stable walk you should construct your own data
836 * structure outside the hash table.
837 *
838 * This function may sleep so you must not call it from interrupt
839 * context or with spin locks held.
840 *
841 * You must call rhashtable_walk_exit if this function returns
842 * successfully.
843 */
844int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter)
845{
846 iter->ht = ht;
847 iter->p = NULL;
848 iter->slot = 0;
849 iter->skip = 0;
850
851 iter->walker = kmalloc(sizeof(*iter->walker), GFP_KERNEL);
852 if (!iter->walker)
853 return -ENOMEM;
854
855 mutex_lock(&ht->mutex);
856 list_add(&iter->walker->list, &ht->walkers);
857 mutex_unlock(&ht->mutex);
858
859 return 0;
860}
861EXPORT_SYMBOL_GPL(rhashtable_walk_init);
862
863/**
864 * rhashtable_walk_exit - Free an iterator
865 * @iter: Hash table Iterator
866 *
867 * This function frees resources allocated by rhashtable_walk_init.
868 */
869void rhashtable_walk_exit(struct rhashtable_iter *iter)
870{
871 mutex_lock(&iter->ht->mutex);
872 list_del(&iter->walker->list);
873 mutex_unlock(&iter->ht->mutex);
874 kfree(iter->walker);
875}
876EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
877
878/**
879 * rhashtable_walk_start - Start a hash table walk
880 * @iter: Hash table iterator
881 *
882 * Start a hash table walk. Note that we take the RCU lock in all
883 * cases including when we return an error. So you must always call
884 * rhashtable_walk_stop to clean up.
885 *
886 * Returns zero if successful.
887 *
888 * Returns -EAGAIN if resize event occured. Note that the iterator
889 * will rewind back to the beginning and you may use it immediately
890 * by calling rhashtable_walk_next.
891 */
892int rhashtable_walk_start(struct rhashtable_iter *iter)
893{
894 rcu_read_lock();
895
896 if (iter->walker->resize) {
897 iter->slot = 0;
898 iter->skip = 0;
899 iter->walker->resize = false;
900 return -EAGAIN;
901 }
902
903 return 0;
904}
905EXPORT_SYMBOL_GPL(rhashtable_walk_start);
906
907/**
908 * rhashtable_walk_next - Return the next object and advance the iterator
909 * @iter: Hash table iterator
910 *
911 * Note that you must call rhashtable_walk_stop when you are finished
912 * with the walk.
913 *
914 * Returns the next object or NULL when the end of the table is reached.
915 *
916 * Returns -EAGAIN if resize event occured. Note that the iterator
917 * will rewind back to the beginning and you may continue to use it.
918 */
919void *rhashtable_walk_next(struct rhashtable_iter *iter)
920{
921 const struct bucket_table *tbl;
922 struct rhashtable *ht = iter->ht;
923 struct rhash_head *p = iter->p;
924 void *obj = NULL;
925
926 tbl = rht_dereference_rcu(ht->tbl, ht);
927
928 if (p) {
929 p = rht_dereference_bucket_rcu(p->next, tbl, iter->slot);
930 goto next;
931 }
932
933 for (; iter->slot < tbl->size; iter->slot++) {
934 int skip = iter->skip;
935
936 rht_for_each_rcu(p, tbl, iter->slot) {
937 if (!skip)
938 break;
939 skip--;
940 }
941
942next:
943 if (!rht_is_a_nulls(p)) {
944 iter->skip++;
945 iter->p = p;
946 obj = rht_obj(ht, p);
947 goto out;
948 }
949
950 iter->skip = 0;
951 }
952
953 iter->p = NULL;
954
955out:
956 if (iter->walker->resize) {
957 iter->p = NULL;
958 iter->slot = 0;
959 iter->skip = 0;
960 iter->walker->resize = false;
961 return ERR_PTR(-EAGAIN);
962 }
963
964 return obj;
965}
966EXPORT_SYMBOL_GPL(rhashtable_walk_next);
967
968/**
969 * rhashtable_walk_stop - Finish a hash table walk
970 * @iter: Hash table iterator
971 *
972 * Finish a hash table walk.
973 */
974void rhashtable_walk_stop(struct rhashtable_iter *iter)
975{
976 rcu_read_unlock();
977 iter->p = NULL;
978}
979EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
980
Ying Xue94000172014-09-03 09:22:36 +0800981static size_t rounded_hashtable_size(struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200982{
Ying Xue94000172014-09-03 09:22:36 +0800983 return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
984 1UL << params->min_shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200985}
986
987/**
988 * rhashtable_init - initialize a new hash table
989 * @ht: hash table to be initialized
990 * @params: configuration parameters
991 *
992 * Initializes a new hash table based on the provided configuration
993 * parameters. A table can be configured either with a variable or
994 * fixed length key:
995 *
996 * Configuration Example 1: Fixed length keys
997 * struct test_obj {
998 * int key;
999 * void * my_member;
1000 * struct rhash_head node;
1001 * };
1002 *
1003 * struct rhashtable_params params = {
1004 * .head_offset = offsetof(struct test_obj, node),
1005 * .key_offset = offsetof(struct test_obj, key),
1006 * .key_len = sizeof(int),
Daniel Borkmann87545892014-12-10 16:33:11 +01001007 * .hashfn = jhash,
Thomas Graff89bd6f2015-01-02 23:00:21 +01001008 * .nulls_base = (1U << RHT_BASE_SHIFT),
Thomas Graf7e1e7762014-08-02 11:47:44 +02001009 * };
1010 *
1011 * Configuration Example 2: Variable length keys
1012 * struct test_obj {
1013 * [...]
1014 * struct rhash_head node;
1015 * };
1016 *
1017 * u32 my_hash_fn(const void *data, u32 seed)
1018 * {
1019 * struct test_obj *obj = data;
1020 *
1021 * return [... hash ...];
1022 * }
1023 *
1024 * struct rhashtable_params params = {
1025 * .head_offset = offsetof(struct test_obj, node),
Daniel Borkmann87545892014-12-10 16:33:11 +01001026 * .hashfn = jhash,
Thomas Graf7e1e7762014-08-02 11:47:44 +02001027 * .obj_hashfn = my_hash_fn,
Thomas Graf7e1e7762014-08-02 11:47:44 +02001028 * };
1029 */
1030int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params)
1031{
1032 struct bucket_table *tbl;
1033 size_t size;
1034
1035 size = HASH_DEFAULT_SIZE;
1036
1037 if ((params->key_len && !params->hashfn) ||
1038 (!params->key_len && !params->obj_hashfn))
1039 return -EINVAL;
1040
Thomas Graff89bd6f2015-01-02 23:00:21 +01001041 if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT))
1042 return -EINVAL;
1043
Ying Xue94000172014-09-03 09:22:36 +08001044 params->min_shift = max_t(size_t, params->min_shift,
1045 ilog2(HASH_MIN_SIZE));
1046
Thomas Graf7e1e7762014-08-02 11:47:44 +02001047 if (params->nelem_hint)
Ying Xue94000172014-09-03 09:22:36 +08001048 size = rounded_hashtable_size(params);
Thomas Graf7e1e7762014-08-02 11:47:44 +02001049
Thomas Graf97defe12015-01-02 23:00:20 +01001050 memset(ht, 0, sizeof(*ht));
1051 mutex_init(&ht->mutex);
1052 memcpy(&ht->p, params, sizeof(*params));
Herbert Xuf2dba9c2015-02-04 07:33:23 +11001053 INIT_LIST_HEAD(&ht->walkers);
Thomas Graf97defe12015-01-02 23:00:20 +01001054
1055 if (params->locks_mul)
1056 ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
1057 else
1058 ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
1059
1060 tbl = bucket_table_alloc(ht, size);
Thomas Graf7e1e7762014-08-02 11:47:44 +02001061 if (tbl == NULL)
1062 return -ENOMEM;
1063
Ying Xue545a1482015-01-07 13:41:57 +08001064 atomic_set(&ht->nelems, 0);
Ying Xuec0c09bf2015-01-07 13:41:56 +08001065 atomic_set(&ht->shift, ilog2(tbl->size));
Thomas Graf7e1e7762014-08-02 11:47:44 +02001066 RCU_INIT_POINTER(ht->tbl, tbl);
Thomas Graf97defe12015-01-02 23:00:20 +01001067 RCU_INIT_POINTER(ht->future_tbl, tbl);
Thomas Graf7e1e7762014-08-02 11:47:44 +02001068
1069 if (!ht->p.hash_rnd)
1070 get_random_bytes(&ht->p.hash_rnd, sizeof(ht->p.hash_rnd));
1071
Thomas Graf97defe12015-01-02 23:00:20 +01001072 if (ht->p.grow_decision || ht->p.shrink_decision)
Ying Xue57699a42015-01-16 11:13:09 +08001073 INIT_WORK(&ht->run_work, rht_deferred_worker);
Thomas Graf97defe12015-01-02 23:00:20 +01001074
Thomas Graf7e1e7762014-08-02 11:47:44 +02001075 return 0;
1076}
1077EXPORT_SYMBOL_GPL(rhashtable_init);
1078
1079/**
1080 * rhashtable_destroy - destroy hash table
1081 * @ht: the hash table to destroy
1082 *
Pablo Neira Ayusoae82ddc2014-09-02 00:26:05 +02001083 * Frees the bucket array. This function is not rcu safe, therefore the caller
1084 * has to make sure that no resizing may happen by unpublishing the hashtable
1085 * and waiting for the quiescent cycle before releasing the bucket array.
Thomas Graf7e1e7762014-08-02 11:47:44 +02001086 */
Thomas Graf97defe12015-01-02 23:00:20 +01001087void rhashtable_destroy(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +02001088{
Thomas Graf97defe12015-01-02 23:00:20 +01001089 ht->being_destroyed = true;
1090
Ying Xue57699a42015-01-16 11:13:09 +08001091 if (ht->p.grow_decision || ht->p.shrink_decision)
1092 cancel_work_sync(&ht->run_work);
1093
Thomas Graf97defe12015-01-02 23:00:20 +01001094 mutex_lock(&ht->mutex);
Thomas Graf97defe12015-01-02 23:00:20 +01001095 bucket_table_free(rht_dereference(ht->tbl, ht));
Thomas Graf97defe12015-01-02 23:00:20 +01001096 mutex_unlock(&ht->mutex);
Thomas Graf7e1e7762014-08-02 11:47:44 +02001097}
1098EXPORT_SYMBOL_GPL(rhashtable_destroy);