blob: ff4ea1704546a25c9a2f88e0f387b0de4c6f5dd3 [file] [log] [blame]
Thomas Graf7e1e7762014-08-02 11:47:44 +02001/*
2 * Resizable, Scalable, Concurrent Hash Table
3 *
Thomas Grafa5ec68e2015-02-05 02:03:32 +01004 * Copyright (c) 2014-2015 Thomas Graf <tgraf@suug.ch>
Thomas Graf7e1e7762014-08-02 11:47:44 +02005 * 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>
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
30#define HASH_MIN_SIZE 4UL
Thomas Graf97defe12015-01-02 23:00:20 +010031#define BUCKET_LOCKS_PER_CPU 128UL
32
Thomas Graff89bd6f2015-01-02 23:00:21 +010033/* Base bits plus 1 bit for nulls marker */
34#define HASH_RESERVED_SPACE (RHT_BASE_BITS + 1)
35
Thomas Graf97defe12015-01-02 23:00:20 +010036/* The bucket lock is selected based on the hash and protects mutations
37 * on a group of hash buckets.
38 *
Thomas Grafa5ec68e2015-02-05 02:03:32 +010039 * A maximum of tbl->size/2 bucket locks is allocated. This ensures that
40 * a single lock always covers both buckets which may both contains
41 * entries which link to the same bucket of the old table during resizing.
42 * This allows to simplify the locking as locking the bucket in both
43 * tables during resize always guarantee protection.
44 *
Thomas Graf97defe12015-01-02 23:00:20 +010045 * IMPORTANT: When holding the bucket lock of both the old and new table
46 * during expansions and shrinking, the old bucket lock must always be
47 * acquired first.
48 */
49static spinlock_t *bucket_lock(const struct bucket_table *tbl, u32 hash)
50{
51 return &tbl->locks[hash & tbl->locks_mask];
52}
Thomas Graf7e1e7762014-08-02 11:47:44 +020053
Thomas Grafc91eee52014-08-13 16:38:30 +020054static void *rht_obj(const struct rhashtable *ht, const struct rhash_head *he)
Thomas Graf7e1e7762014-08-02 11:47:44 +020055{
56 return (void *) he - ht->p.head_offset;
57}
Thomas Graf7e1e7762014-08-02 11:47:44 +020058
Thomas Graf8d24c0b2015-01-02 23:00:14 +010059static u32 rht_bucket_index(const struct bucket_table *tbl, u32 hash)
Thomas Graf7e1e7762014-08-02 11:47:44 +020060{
Herbert Xuec9f71c2015-03-12 14:49:41 +110061 return (hash >> HASH_RESERVED_SPACE) & (tbl->size - 1);
Thomas Graf8d24c0b2015-01-02 23:00:14 +010062}
63
Herbert Xuaa34a6cb02015-03-11 09:43:48 +110064static u32 key_hashfn(struct rhashtable *ht, const struct bucket_table *tbl,
Herbert Xucffaa9c2015-03-12 14:49:40 +110065 const void *key)
Thomas Graf7e1e7762014-08-02 11:47:44 +020066{
Herbert Xucffaa9c2015-03-12 14:49:40 +110067 return rht_bucket_index(tbl, ht->p.hashfn(key, ht->p.key_len,
Herbert Xuec9f71c2015-03-12 14:49:41 +110068 tbl->hash_rnd));
Thomas Graf7e1e7762014-08-02 11:47:44 +020069}
Thomas Graf7e1e7762014-08-02 11:47:44 +020070
Herbert Xu988dfbd2015-03-10 09:27:55 +110071static u32 head_hashfn(struct rhashtable *ht,
Thomas Graf8d24c0b2015-01-02 23:00:14 +010072 const struct bucket_table *tbl,
73 const struct rhash_head *he)
Thomas Graf7e1e7762014-08-02 11:47:44 +020074{
Herbert Xuec9f71c2015-03-12 14:49:41 +110075 const char *ptr = rht_obj(ht, he);
76
77 return likely(ht->p.key_len) ?
78 key_hashfn(ht, tbl, ptr + ht->p.key_offset) :
79 rht_bucket_index(tbl, ht->p.obj_hashfn(ptr, tbl->hash_rnd));
Thomas Graf7e1e7762014-08-02 11:47:44 +020080}
81
Thomas Grafa03eaec2015-02-05 02:03:34 +010082#ifdef CONFIG_PROVE_LOCKING
Thomas Grafa03eaec2015-02-05 02:03:34 +010083#define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
Thomas Grafa03eaec2015-02-05 02:03:34 +010084
85int lockdep_rht_mutex_is_held(struct rhashtable *ht)
86{
87 return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1;
88}
89EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held);
90
91int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash)
92{
93 spinlock_t *lock = bucket_lock(tbl, hash);
94
95 return (debug_locks) ? lockdep_is_held(lock) : 1;
96}
97EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held);
98#else
99#define ASSERT_RHT_MUTEX(HT)
Thomas Grafa03eaec2015-02-05 02:03:34 +0100100#endif
101
102
Thomas Graf97defe12015-01-02 23:00:20 +0100103static int alloc_bucket_locks(struct rhashtable *ht, struct bucket_table *tbl)
104{
105 unsigned int i, size;
106#if defined(CONFIG_PROVE_LOCKING)
107 unsigned int nr_pcpus = 2;
108#else
109 unsigned int nr_pcpus = num_possible_cpus();
110#endif
111
112 nr_pcpus = min_t(unsigned int, nr_pcpus, 32UL);
113 size = roundup_pow_of_two(nr_pcpus * ht->p.locks_mul);
114
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100115 /* Never allocate more than 0.5 locks per bucket */
116 size = min_t(unsigned int, size, tbl->size >> 1);
Thomas Graf97defe12015-01-02 23:00:20 +0100117
118 if (sizeof(spinlock_t) != 0) {
119#ifdef CONFIG_NUMA
120 if (size * sizeof(spinlock_t) > PAGE_SIZE)
121 tbl->locks = vmalloc(size * sizeof(spinlock_t));
122 else
123#endif
124 tbl->locks = kmalloc_array(size, sizeof(spinlock_t),
125 GFP_KERNEL);
126 if (!tbl->locks)
127 return -ENOMEM;
128 for (i = 0; i < size; i++)
129 spin_lock_init(&tbl->locks[i]);
130 }
131 tbl->locks_mask = size - 1;
132
133 return 0;
134}
135
136static void bucket_table_free(const struct bucket_table *tbl)
137{
138 if (tbl)
139 kvfree(tbl->locks);
140
141 kvfree(tbl);
142}
143
Herbert Xu9d901bc2015-03-14 13:57:23 +1100144static void bucket_table_free_rcu(struct rcu_head *head)
145{
146 bucket_table_free(container_of(head, struct bucket_table, rcu));
147}
148
Thomas Graf97defe12015-01-02 23:00:20 +0100149static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
Herbert Xu5269b532015-03-14 13:57:22 +1100150 size_t nbuckets)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200151{
Daniel Borkmanneb6d1ab2015-02-20 00:53:38 +0100152 struct bucket_table *tbl = NULL;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200153 size_t size;
Thomas Graff89bd6f2015-01-02 23:00:21 +0100154 int i;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200155
156 size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]);
Daniel Borkmanneb6d1ab2015-02-20 00:53:38 +0100157 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
158 tbl = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200159 if (tbl == NULL)
160 tbl = vzalloc(size);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200161 if (tbl == NULL)
162 return NULL;
163
164 tbl->size = nbuckets;
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100165 tbl->shift = ilog2(nbuckets);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200166
Thomas Graf97defe12015-01-02 23:00:20 +0100167 if (alloc_bucket_locks(ht, tbl) < 0) {
168 bucket_table_free(tbl);
169 return NULL;
170 }
Thomas Graf7e1e7762014-08-02 11:47:44 +0200171
Herbert Xueddee5ba2015-03-14 13:57:20 +1100172 INIT_LIST_HEAD(&tbl->walkers);
173
Herbert Xu5269b532015-03-14 13:57:22 +1100174 get_random_bytes(&tbl->hash_rnd, sizeof(tbl->hash_rnd));
175
Thomas Graff89bd6f2015-01-02 23:00:21 +0100176 for (i = 0; i < nbuckets; i++)
177 INIT_RHT_NULLS_HEAD(tbl->buckets[i], ht, i);
178
Thomas Graf97defe12015-01-02 23:00:20 +0100179 return tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200180}
181
182/**
183 * rht_grow_above_75 - returns true if nelems > 0.75 * table-size
184 * @ht: hash table
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100185 * @tbl: current table
Thomas Graf7e1e7762014-08-02 11:47:44 +0200186 */
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100187static bool rht_grow_above_75(const struct rhashtable *ht,
188 const struct bucket_table *tbl)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200189{
190 /* Expand table when exceeding 75% load */
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100191 return atomic_read(&ht->nelems) > (tbl->size / 4 * 3) &&
192 (!ht->p.max_shift || tbl->shift < ht->p.max_shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200193}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200194
195/**
196 * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size
197 * @ht: hash table
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100198 * @tbl: current table
Thomas Graf7e1e7762014-08-02 11:47:44 +0200199 */
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100200static bool rht_shrink_below_30(const struct rhashtable *ht,
201 const struct bucket_table *tbl)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200202{
203 /* Shrink table beneath 30% load */
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100204 return atomic_read(&ht->nelems) < (tbl->size * 3 / 10) &&
205 tbl->shift > ht->p.min_shift;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200206}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200207
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100208static int rhashtable_rehash_one(struct rhashtable *ht, unsigned old_hash)
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100209{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100210 struct bucket_table *new_tbl = rht_dereference(ht->future_tbl, ht);
211 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
212 struct rhash_head __rcu **pprev = &old_tbl->buckets[old_hash];
213 int err = -ENOENT;
214 struct rhash_head *head, *next, *entry;
215 spinlock_t *new_bucket_lock;
216 unsigned new_hash;
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100217
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100218 rht_for_each(entry, old_tbl, old_hash) {
219 err = 0;
220 next = rht_dereference_bucket(entry->next, old_tbl, old_hash);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100221
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100222 if (rht_is_a_nulls(next))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200223 break;
Thomas Graf97defe12015-01-02 23:00:20 +0100224
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100225 pprev = &entry->next;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200226 }
227
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100228 if (err)
229 goto out;
Thomas Graf97defe12015-01-02 23:00:20 +0100230
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100231 new_hash = head_hashfn(ht, new_tbl, entry);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100232
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100233 new_bucket_lock = bucket_lock(new_tbl, new_hash);
234
Herbert Xu8f2484b2015-03-14 13:57:21 +1100235 spin_lock_nested(new_bucket_lock, SINGLE_DEPTH_NESTING);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100236 head = rht_dereference_bucket(new_tbl->buckets[new_hash],
237 new_tbl, new_hash);
238
239 if (rht_is_a_nulls(head))
240 INIT_RHT_NULLS_HEAD(entry->next, ht, new_hash);
241 else
242 RCU_INIT_POINTER(entry->next, head);
243
244 rcu_assign_pointer(new_tbl->buckets[new_hash], entry);
245 spin_unlock(new_bucket_lock);
246
247 rcu_assign_pointer(*pprev, next);
248
249out:
250 return err;
Thomas Graf97defe12015-01-02 23:00:20 +0100251}
252
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100253static void rhashtable_rehash_chain(struct rhashtable *ht, unsigned old_hash)
Thomas Graf97defe12015-01-02 23:00:20 +0100254{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100255 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
256 spinlock_t *old_bucket_lock;
Thomas Graf7cd10db2015-02-05 02:03:35 +0100257
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100258 old_bucket_lock = bucket_lock(old_tbl, old_hash);
259
260 spin_lock_bh(old_bucket_lock);
261 while (!rhashtable_rehash_one(ht, old_hash))
262 ;
Herbert Xu63d512d2015-03-14 13:57:24 +1100263 old_tbl->rehash++;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100264 spin_unlock_bh(old_bucket_lock);
265}
266
267static void rhashtable_rehash(struct rhashtable *ht,
268 struct bucket_table *new_tbl)
269{
270 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100271 struct rhashtable_walker *walker;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100272 unsigned old_hash;
273
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100274 /* Make insertions go into the new, empty table right away. Deletions
275 * and lookups will be attempted in both tables until we synchronize.
276 * The synchronize_rcu() guarantees for the new table to be picked up
277 * so no new additions go into the old table while we relink.
278 */
279 rcu_assign_pointer(ht->future_tbl, new_tbl);
280
Herbert Xu9497df82015-03-12 22:07:49 +1100281 /* Ensure the new table is visible to readers. */
282 smp_wmb();
283
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100284 for (old_hash = 0; old_hash < old_tbl->size; old_hash++)
285 rhashtable_rehash_chain(ht, old_hash);
286
287 /* Publish the new table pointer. */
288 rcu_assign_pointer(ht->tbl, new_tbl);
289
Herbert Xueddee5ba2015-03-14 13:57:20 +1100290 list_for_each_entry(walker, &old_tbl->walkers, list)
291 walker->tbl = NULL;
292
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100293 /* Wait for readers. All new readers will see the new
294 * table, and thus no references to the old table will
295 * remain.
296 */
Herbert Xu9d901bc2015-03-14 13:57:23 +1100297 call_rcu(&old_tbl->rcu, bucket_table_free_rcu);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200298}
299
300/**
301 * rhashtable_expand - Expand hash table while allowing concurrent lookups
302 * @ht: the hash table to expand
Thomas Graf7e1e7762014-08-02 11:47:44 +0200303 *
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100304 * A secondary bucket array is allocated and the hash entries are migrated.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200305 *
306 * This function may only be called in a context where it is safe to call
307 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
308 *
Thomas Graf97defe12015-01-02 23:00:20 +0100309 * The caller must ensure that no concurrent resizing occurs by holding
310 * ht->mutex.
311 *
312 * It is valid to have concurrent insertions and deletions protected by per
313 * bucket locks or concurrent RCU protected lookups and traversals.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200314 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100315int rhashtable_expand(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200316{
317 struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200318
319 ASSERT_RHT_MUTEX(ht);
320
Herbert Xu5269b532015-03-14 13:57:22 +1100321 new_tbl = bucket_table_alloc(ht, old_tbl->size * 2);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200322 if (new_tbl == NULL)
323 return -ENOMEM;
324
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100325 rhashtable_rehash(ht, new_tbl);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200326 return 0;
327}
328EXPORT_SYMBOL_GPL(rhashtable_expand);
329
330/**
331 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
332 * @ht: the hash table to shrink
Thomas Graf7e1e7762014-08-02 11:47:44 +0200333 *
334 * This function may only be called in a context where it is safe to call
335 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
336 *
Thomas Graf97defe12015-01-02 23:00:20 +0100337 * The caller must ensure that no concurrent resizing occurs by holding
338 * ht->mutex.
339 *
Thomas Graf7e1e7762014-08-02 11:47:44 +0200340 * The caller must ensure that no concurrent table mutations take place.
341 * It is however valid to have concurrent lookups if they are RCU protected.
Thomas Graf97defe12015-01-02 23:00:20 +0100342 *
343 * It is valid to have concurrent insertions and deletions protected by per
344 * bucket locks or concurrent RCU protected lookups and traversals.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200345 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100346int rhashtable_shrink(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200347{
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100348 struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200349
350 ASSERT_RHT_MUTEX(ht);
351
Herbert Xu5269b532015-03-14 13:57:22 +1100352 new_tbl = bucket_table_alloc(ht, old_tbl->size / 2);
Thomas Graf97defe12015-01-02 23:00:20 +0100353 if (new_tbl == NULL)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200354 return -ENOMEM;
355
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100356 rhashtable_rehash(ht, new_tbl);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200357 return 0;
358}
359EXPORT_SYMBOL_GPL(rhashtable_shrink);
360
Thomas Graf97defe12015-01-02 23:00:20 +0100361static void rht_deferred_worker(struct work_struct *work)
362{
363 struct rhashtable *ht;
364 struct bucket_table *tbl;
365
Ying Xue57699a42015-01-16 11:13:09 +0800366 ht = container_of(work, struct rhashtable, run_work);
Thomas Graf97defe12015-01-02 23:00:20 +0100367 mutex_lock(&ht->mutex);
Herbert Xu28134a52015-02-04 07:33:22 +1100368 if (ht->being_destroyed)
369 goto unlock;
370
Thomas Graf97defe12015-01-02 23:00:20 +0100371 tbl = rht_dereference(ht->tbl, ht);
372
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100373 if (rht_grow_above_75(ht, tbl))
Thomas Graf97defe12015-01-02 23:00:20 +0100374 rhashtable_expand(ht);
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100375 else if (rht_shrink_below_30(ht, tbl))
Thomas Graf97defe12015-01-02 23:00:20 +0100376 rhashtable_shrink(ht);
Herbert Xu28134a52015-02-04 07:33:22 +1100377unlock:
Thomas Graf97defe12015-01-02 23:00:20 +0100378 mutex_unlock(&ht->mutex);
379}
380
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100381static bool __rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj,
382 bool (*compare)(void *, void *), void *arg)
Ying Xuedb304852015-01-07 13:41:54 +0800383{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100384 struct bucket_table *tbl, *old_tbl;
Thomas Graf020219a2015-02-06 16:08:43 +0000385 struct rhash_head *head;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100386 bool no_resize_running;
387 unsigned hash;
388 bool success = true;
389
390 rcu_read_lock();
391
392 old_tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xueca84932015-03-12 14:49:39 +1100393 hash = head_hashfn(ht, old_tbl, obj);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100394
395 spin_lock_bh(bucket_lock(old_tbl, hash));
396
397 /* Because we have already taken the bucket lock in old_tbl,
398 * if we find that future_tbl is not yet visible then that
399 * guarantees all other insertions of the same entry will
400 * also grab the bucket lock in old_tbl because until the
401 * rehash completes ht->tbl won't be changed.
402 */
403 tbl = rht_dereference_rcu(ht->future_tbl, ht);
404 if (tbl != old_tbl) {
Herbert Xueca84932015-03-12 14:49:39 +1100405 hash = head_hashfn(ht, tbl, obj);
Herbert Xu8f2484b2015-03-14 13:57:21 +1100406 spin_lock_nested(bucket_lock(tbl, hash), SINGLE_DEPTH_NESTING);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100407 }
408
409 if (compare &&
410 rhashtable_lookup_compare(ht, rht_obj(ht, obj) + ht->p.key_offset,
411 compare, arg)) {
412 success = false;
413 goto exit;
414 }
415
416 no_resize_running = tbl == old_tbl;
Thomas Graf020219a2015-02-06 16:08:43 +0000417
Thomas Graf020219a2015-02-06 16:08:43 +0000418 head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash);
Ying Xuedb304852015-01-07 13:41:54 +0800419
420 if (rht_is_a_nulls(head))
421 INIT_RHT_NULLS_HEAD(obj->next, ht, hash);
422 else
423 RCU_INIT_POINTER(obj->next, head);
424
425 rcu_assign_pointer(tbl->buckets[hash], obj);
426
427 atomic_inc(&ht->nelems);
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100428 if (no_resize_running && rht_grow_above_75(ht, tbl))
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100429 schedule_work(&ht->run_work);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100430
431exit:
432 if (tbl != old_tbl) {
Herbert Xueca84932015-03-12 14:49:39 +1100433 hash = head_hashfn(ht, tbl, obj);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100434 spin_unlock(bucket_lock(tbl, hash));
435 }
436
Herbert Xueca84932015-03-12 14:49:39 +1100437 hash = head_hashfn(ht, old_tbl, obj);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100438 spin_unlock_bh(bucket_lock(old_tbl, hash));
439
440 rcu_read_unlock();
441
442 return success;
Ying Xuedb304852015-01-07 13:41:54 +0800443}
444
Thomas Graf7e1e7762014-08-02 11:47:44 +0200445/**
Ying Xuedb304852015-01-07 13:41:54 +0800446 * rhashtable_insert - insert object into hash table
Thomas Graf7e1e7762014-08-02 11:47:44 +0200447 * @ht: hash table
448 * @obj: pointer to hash head inside object
Thomas Graf7e1e7762014-08-02 11:47:44 +0200449 *
Thomas Graf97defe12015-01-02 23:00:20 +0100450 * Will take a per bucket spinlock to protect against mutual mutations
451 * on the same bucket. Multiple insertions may occur in parallel unless
452 * they map to the same bucket lock.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200453 *
Thomas Graf97defe12015-01-02 23:00:20 +0100454 * It is safe to call this function from atomic context.
455 *
456 * Will trigger an automatic deferred table resizing if the size grows
457 * beyond the watermark indicated by grow_decision() which can be passed
458 * to rhashtable_init().
Thomas Graf7e1e7762014-08-02 11:47:44 +0200459 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100460void rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200461{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100462 __rhashtable_insert(ht, obj, NULL, NULL);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200463}
464EXPORT_SYMBOL_GPL(rhashtable_insert);
465
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100466static bool __rhashtable_remove(struct rhashtable *ht,
467 struct bucket_table *tbl,
468 struct rhash_head *obj)
469{
470 struct rhash_head __rcu **pprev;
471 struct rhash_head *he;
472 spinlock_t * lock;
473 unsigned hash;
474 bool ret = false;
475
Herbert Xueca84932015-03-12 14:49:39 +1100476 hash = head_hashfn(ht, tbl, obj);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100477 lock = bucket_lock(tbl, hash);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100478
479 spin_lock_bh(lock);
480
481 pprev = &tbl->buckets[hash];
482 rht_for_each(he, tbl, hash) {
483 if (he != obj) {
484 pprev = &he->next;
485 continue;
486 }
487
488 rcu_assign_pointer(*pprev, obj->next);
489 ret = true;
490 break;
491 }
492
493 spin_unlock_bh(lock);
494
495 return ret;
496}
497
Thomas Graf7e1e7762014-08-02 11:47:44 +0200498/**
Thomas Graf7e1e7762014-08-02 11:47:44 +0200499 * rhashtable_remove - remove object from hash table
500 * @ht: hash table
501 * @obj: pointer to hash head inside object
Thomas Graf7e1e7762014-08-02 11:47:44 +0200502 *
503 * Since the hash chain is single linked, the removal operation needs to
504 * walk the bucket chain upon removal. The removal operation is thus
505 * considerable slow if the hash table is not correctly sized.
506 *
Ying Xuedb304852015-01-07 13:41:54 +0800507 * Will automatically shrink the table via rhashtable_expand() if the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200508 * shrink_decision function specified at rhashtable_init() returns true.
509 *
510 * The caller must ensure that no concurrent table mutations occur. It is
511 * however valid to have concurrent lookups if they are RCU protected.
512 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100513bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *obj)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200514{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100515 struct bucket_table *tbl, *old_tbl;
516 bool ret;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200517
Thomas Graf97defe12015-01-02 23:00:20 +0100518 rcu_read_lock();
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100519
Thomas Graf020219a2015-02-06 16:08:43 +0000520 old_tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100521 ret = __rhashtable_remove(ht, old_tbl, obj);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200522
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100523 /* Because we have already taken (and released) the bucket
524 * lock in old_tbl, if we find that future_tbl is not yet
525 * visible then that guarantees the entry to still be in
526 * old_tbl if it exists.
Thomas Graffe6a0432015-01-21 11:54:01 +0000527 */
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100528 tbl = rht_dereference_rcu(ht->future_tbl, ht);
529 if (!ret && old_tbl != tbl)
530 ret = __rhashtable_remove(ht, tbl, obj);
Thomas Graffe6a0432015-01-21 11:54:01 +0000531
532 if (ret) {
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100533 bool no_resize_running = tbl == old_tbl;
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100534
Thomas Graffe6a0432015-01-21 11:54:01 +0000535 atomic_dec(&ht->nelems);
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100536 if (no_resize_running && rht_shrink_below_30(ht, tbl))
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100537 schedule_work(&ht->run_work);
Thomas Graffe6a0432015-01-21 11:54:01 +0000538 }
539
Thomas Graf97defe12015-01-02 23:00:20 +0100540 rcu_read_unlock();
541
Thomas Graffe6a0432015-01-21 11:54:01 +0000542 return ret;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200543}
544EXPORT_SYMBOL_GPL(rhashtable_remove);
545
Ying Xueefb975a62015-01-07 13:41:52 +0800546struct rhashtable_compare_arg {
547 struct rhashtable *ht;
548 const void *key;
549};
550
551static bool rhashtable_compare(void *ptr, void *arg)
552{
553 struct rhashtable_compare_arg *x = arg;
554 struct rhashtable *ht = x->ht;
555
556 return !memcmp(ptr + ht->p.key_offset, x->key, ht->p.key_len);
557}
558
Thomas Graf7e1e7762014-08-02 11:47:44 +0200559/**
560 * rhashtable_lookup - lookup key in hash table
561 * @ht: hash table
562 * @key: pointer to key
563 *
564 * Computes the hash value for the key and traverses the bucket chain looking
565 * for a entry with an identical key. The first matching entry is returned.
566 *
567 * This lookup function may only be used for fixed key hash table (key_len
Ying Xuedb304852015-01-07 13:41:54 +0800568 * parameter set). It will BUG() if used inappropriately.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200569 *
Thomas Graf97defe12015-01-02 23:00:20 +0100570 * Lookups may occur in parallel with hashtable mutations and resizing.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200571 */
Thomas Graf97defe12015-01-02 23:00:20 +0100572void *rhashtable_lookup(struct rhashtable *ht, const void *key)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200573{
Ying Xueefb975a62015-01-07 13:41:52 +0800574 struct rhashtable_compare_arg arg = {
575 .ht = ht,
576 .key = key,
577 };
Thomas Graf7e1e7762014-08-02 11:47:44 +0200578
579 BUG_ON(!ht->p.key_len);
580
Ying Xueefb975a62015-01-07 13:41:52 +0800581 return rhashtable_lookup_compare(ht, key, &rhashtable_compare, &arg);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200582}
583EXPORT_SYMBOL_GPL(rhashtable_lookup);
584
585/**
586 * rhashtable_lookup_compare - search hash table with compare function
587 * @ht: hash table
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100588 * @key: the pointer to the key
Thomas Graf7e1e7762014-08-02 11:47:44 +0200589 * @compare: compare function, must return true on match
590 * @arg: argument passed on to compare function
591 *
592 * Traverses the bucket chain behind the provided hash value and calls the
593 * specified compare function for each entry.
594 *
Thomas Graf97defe12015-01-02 23:00:20 +0100595 * Lookups may occur in parallel with hashtable mutations and resizing.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200596 *
597 * Returns the first entry on which the compare function returned true.
598 */
Thomas Graf97defe12015-01-02 23:00:20 +0100599void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200600 bool (*compare)(void *, void *), void *arg)
601{
Thomas Graf97defe12015-01-02 23:00:20 +0100602 const struct bucket_table *tbl, *old_tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200603 struct rhash_head *he;
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100604 u32 hash;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200605
Thomas Graf97defe12015-01-02 23:00:20 +0100606 rcu_read_lock();
607
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100608 tbl = rht_dereference_rcu(ht->tbl, ht);
Thomas Graf97defe12015-01-02 23:00:20 +0100609restart:
Herbert Xu39361942015-03-13 12:54:10 +1100610 hash = key_hashfn(ht, tbl, key);
Herbert Xu8d2b1872015-03-12 14:49:38 +1100611 rht_for_each_rcu(he, tbl, hash) {
Thomas Graf7e1e7762014-08-02 11:47:44 +0200612 if (!compare(rht_obj(ht, he), arg))
613 continue;
Thomas Graf97defe12015-01-02 23:00:20 +0100614 rcu_read_unlock();
Thomas Grafa4b18cd2015-01-02 23:00:15 +0100615 return rht_obj(ht, he);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200616 }
617
Herbert Xu9497df82015-03-12 22:07:49 +1100618 /* Ensure we see any new tables. */
619 smp_rmb();
620
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100621 old_tbl = tbl;
622 tbl = rht_dereference_rcu(ht->future_tbl, ht);
623 if (unlikely(tbl != old_tbl))
Thomas Graf97defe12015-01-02 23:00:20 +0100624 goto restart;
Thomas Graf97defe12015-01-02 23:00:20 +0100625 rcu_read_unlock();
626
Thomas Graf7e1e7762014-08-02 11:47:44 +0200627 return NULL;
628}
629EXPORT_SYMBOL_GPL(rhashtable_lookup_compare);
630
Ying Xuedb304852015-01-07 13:41:54 +0800631/**
632 * rhashtable_lookup_insert - lookup and insert object into hash table
633 * @ht: hash table
634 * @obj: pointer to hash head inside object
635 *
636 * Locks down the bucket chain in both the old and new table if a resize
637 * is in progress to ensure that writers can't remove from the old table
638 * and can't insert to the new table during the atomic operation of search
639 * and insertion. Searches for duplicates in both the old and new table if
640 * a resize is in progress.
641 *
642 * This lookup function may only be used for fixed key hash table (key_len
643 * parameter set). It will BUG() if used inappropriately.
644 *
645 * It is safe to call this function from atomic context.
646 *
647 * Will trigger an automatic deferred table resizing if the size grows
648 * beyond the watermark indicated by grow_decision() which can be passed
649 * to rhashtable_init().
650 */
651bool rhashtable_lookup_insert(struct rhashtable *ht, struct rhash_head *obj)
652{
Ying Xue7a868d12015-01-12 14:52:22 +0800653 struct rhashtable_compare_arg arg = {
654 .ht = ht,
655 .key = rht_obj(ht, obj) + ht->p.key_offset,
656 };
657
658 BUG_ON(!ht->p.key_len);
659
660 return rhashtable_lookup_compare_insert(ht, obj, &rhashtable_compare,
661 &arg);
662}
663EXPORT_SYMBOL_GPL(rhashtable_lookup_insert);
664
665/**
666 * rhashtable_lookup_compare_insert - search and insert object to hash table
667 * with compare function
668 * @ht: hash table
669 * @obj: pointer to hash head inside object
670 * @compare: compare function, must return true on match
671 * @arg: argument passed on to compare function
672 *
673 * Locks down the bucket chain in both the old and new table if a resize
674 * is in progress to ensure that writers can't remove from the old table
675 * and can't insert to the new table during the atomic operation of search
676 * and insertion. Searches for duplicates in both the old and new table if
677 * a resize is in progress.
678 *
679 * Lookups may occur in parallel with hashtable mutations and resizing.
680 *
681 * Will trigger an automatic deferred table resizing if the size grows
682 * beyond the watermark indicated by grow_decision() which can be passed
683 * to rhashtable_init().
684 */
685bool rhashtable_lookup_compare_insert(struct rhashtable *ht,
686 struct rhash_head *obj,
687 bool (*compare)(void *, void *),
688 void *arg)
689{
Ying Xuedb304852015-01-07 13:41:54 +0800690 BUG_ON(!ht->p.key_len);
691
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100692 return __rhashtable_insert(ht, obj, compare, arg);
Ying Xuedb304852015-01-07 13:41:54 +0800693}
Ying Xue7a868d12015-01-12 14:52:22 +0800694EXPORT_SYMBOL_GPL(rhashtable_lookup_compare_insert);
Ying Xuedb304852015-01-07 13:41:54 +0800695
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100696/**
697 * rhashtable_walk_init - Initialise an iterator
698 * @ht: Table to walk over
699 * @iter: Hash table Iterator
700 *
701 * This function prepares a hash table walk.
702 *
703 * Note that if you restart a walk after rhashtable_walk_stop you
704 * may see the same object twice. Also, you may miss objects if
705 * there are removals in between rhashtable_walk_stop and the next
706 * call to rhashtable_walk_start.
707 *
708 * For a completely stable walk you should construct your own data
709 * structure outside the hash table.
710 *
711 * This function may sleep so you must not call it from interrupt
712 * context or with spin locks held.
713 *
714 * You must call rhashtable_walk_exit if this function returns
715 * successfully.
716 */
717int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter)
718{
719 iter->ht = ht;
720 iter->p = NULL;
721 iter->slot = 0;
722 iter->skip = 0;
723
724 iter->walker = kmalloc(sizeof(*iter->walker), GFP_KERNEL);
725 if (!iter->walker)
726 return -ENOMEM;
727
728 mutex_lock(&ht->mutex);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100729 iter->walker->tbl = rht_dereference(ht->tbl, ht);
730 list_add(&iter->walker->list, &iter->walker->tbl->walkers);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100731 mutex_unlock(&ht->mutex);
732
733 return 0;
734}
735EXPORT_SYMBOL_GPL(rhashtable_walk_init);
736
737/**
738 * rhashtable_walk_exit - Free an iterator
739 * @iter: Hash table Iterator
740 *
741 * This function frees resources allocated by rhashtable_walk_init.
742 */
743void rhashtable_walk_exit(struct rhashtable_iter *iter)
744{
745 mutex_lock(&iter->ht->mutex);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100746 if (iter->walker->tbl)
747 list_del(&iter->walker->list);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100748 mutex_unlock(&iter->ht->mutex);
749 kfree(iter->walker);
750}
751EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
752
753/**
754 * rhashtable_walk_start - Start a hash table walk
755 * @iter: Hash table iterator
756 *
757 * Start a hash table walk. Note that we take the RCU lock in all
758 * cases including when we return an error. So you must always call
759 * rhashtable_walk_stop to clean up.
760 *
761 * Returns zero if successful.
762 *
763 * Returns -EAGAIN if resize event occured. Note that the iterator
764 * will rewind back to the beginning and you may use it immediately
765 * by calling rhashtable_walk_next.
766 */
767int rhashtable_walk_start(struct rhashtable_iter *iter)
768{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100769 struct rhashtable *ht = iter->ht;
770
771 mutex_lock(&ht->mutex);
772
773 if (iter->walker->tbl)
774 list_del(&iter->walker->list);
775
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100776 rcu_read_lock();
777
Herbert Xueddee5ba2015-03-14 13:57:20 +1100778 mutex_unlock(&ht->mutex);
779
780 if (!iter->walker->tbl) {
781 iter->walker->tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100782 return -EAGAIN;
783 }
784
785 return 0;
786}
787EXPORT_SYMBOL_GPL(rhashtable_walk_start);
788
789/**
790 * rhashtable_walk_next - Return the next object and advance the iterator
791 * @iter: Hash table iterator
792 *
793 * Note that you must call rhashtable_walk_stop when you are finished
794 * with the walk.
795 *
796 * Returns the next object or NULL when the end of the table is reached.
797 *
798 * Returns -EAGAIN if resize event occured. Note that the iterator
799 * will rewind back to the beginning and you may continue to use it.
800 */
801void *rhashtable_walk_next(struct rhashtable_iter *iter)
802{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100803 struct bucket_table *tbl = iter->walker->tbl;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100804 struct rhashtable *ht = iter->ht;
805 struct rhash_head *p = iter->p;
806 void *obj = NULL;
807
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100808 if (p) {
809 p = rht_dereference_bucket_rcu(p->next, tbl, iter->slot);
810 goto next;
811 }
812
813 for (; iter->slot < tbl->size; iter->slot++) {
814 int skip = iter->skip;
815
816 rht_for_each_rcu(p, tbl, iter->slot) {
817 if (!skip)
818 break;
819 skip--;
820 }
821
822next:
823 if (!rht_is_a_nulls(p)) {
824 iter->skip++;
825 iter->p = p;
826 obj = rht_obj(ht, p);
827 goto out;
828 }
829
830 iter->skip = 0;
831 }
832
Herbert Xueddee5ba2015-03-14 13:57:20 +1100833 iter->walker->tbl = rht_dereference_rcu(ht->future_tbl, ht);
834 if (iter->walker->tbl != tbl) {
835 iter->slot = 0;
836 iter->skip = 0;
837 return ERR_PTR(-EAGAIN);
838 }
839
840 iter->walker->tbl = NULL;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100841 iter->p = NULL;
842
843out:
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100844
845 return obj;
846}
847EXPORT_SYMBOL_GPL(rhashtable_walk_next);
848
849/**
850 * rhashtable_walk_stop - Finish a hash table walk
851 * @iter: Hash table iterator
852 *
853 * Finish a hash table walk.
854 */
855void rhashtable_walk_stop(struct rhashtable_iter *iter)
856{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100857 struct rhashtable *ht;
858 struct bucket_table *tbl = iter->walker->tbl;
859
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100860 rcu_read_unlock();
Herbert Xueddee5ba2015-03-14 13:57:20 +1100861
862 if (!tbl)
863 return;
864
865 ht = iter->ht;
866
867 mutex_lock(&ht->mutex);
868 if (rht_dereference(ht->tbl, ht) == tbl ||
869 rht_dereference(ht->future_tbl, ht) == tbl)
870 list_add(&iter->walker->list, &tbl->walkers);
871 else
872 iter->walker->tbl = NULL;
873 mutex_unlock(&ht->mutex);
874
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100875 iter->p = NULL;
876}
877EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
878
Ying Xue94000172014-09-03 09:22:36 +0800879static size_t rounded_hashtable_size(struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200880{
Ying Xue94000172014-09-03 09:22:36 +0800881 return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
882 1UL << params->min_shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200883}
884
885/**
886 * rhashtable_init - initialize a new hash table
887 * @ht: hash table to be initialized
888 * @params: configuration parameters
889 *
890 * Initializes a new hash table based on the provided configuration
891 * parameters. A table can be configured either with a variable or
892 * fixed length key:
893 *
894 * Configuration Example 1: Fixed length keys
895 * struct test_obj {
896 * int key;
897 * void * my_member;
898 * struct rhash_head node;
899 * };
900 *
901 * struct rhashtable_params params = {
902 * .head_offset = offsetof(struct test_obj, node),
903 * .key_offset = offsetof(struct test_obj, key),
904 * .key_len = sizeof(int),
Daniel Borkmann87545892014-12-10 16:33:11 +0100905 * .hashfn = jhash,
Thomas Graff89bd6f2015-01-02 23:00:21 +0100906 * .nulls_base = (1U << RHT_BASE_SHIFT),
Thomas Graf7e1e7762014-08-02 11:47:44 +0200907 * };
908 *
909 * Configuration Example 2: Variable length keys
910 * struct test_obj {
911 * [...]
912 * struct rhash_head node;
913 * };
914 *
915 * u32 my_hash_fn(const void *data, u32 seed)
916 * {
917 * struct test_obj *obj = data;
918 *
919 * return [... hash ...];
920 * }
921 *
922 * struct rhashtable_params params = {
923 * .head_offset = offsetof(struct test_obj, node),
Daniel Borkmann87545892014-12-10 16:33:11 +0100924 * .hashfn = jhash,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200925 * .obj_hashfn = my_hash_fn,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200926 * };
927 */
928int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params)
929{
930 struct bucket_table *tbl;
931 size_t size;
932
933 size = HASH_DEFAULT_SIZE;
934
935 if ((params->key_len && !params->hashfn) ||
936 (!params->key_len && !params->obj_hashfn))
937 return -EINVAL;
938
Thomas Graff89bd6f2015-01-02 23:00:21 +0100939 if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT))
940 return -EINVAL;
941
Ying Xue94000172014-09-03 09:22:36 +0800942 params->min_shift = max_t(size_t, params->min_shift,
943 ilog2(HASH_MIN_SIZE));
944
Thomas Graf7e1e7762014-08-02 11:47:44 +0200945 if (params->nelem_hint)
Ying Xue94000172014-09-03 09:22:36 +0800946 size = rounded_hashtable_size(params);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200947
Thomas Graf97defe12015-01-02 23:00:20 +0100948 memset(ht, 0, sizeof(*ht));
949 mutex_init(&ht->mutex);
950 memcpy(&ht->p, params, sizeof(*params));
951
952 if (params->locks_mul)
953 ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
954 else
955 ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
956
Herbert Xu5269b532015-03-14 13:57:22 +1100957 tbl = bucket_table_alloc(ht, size);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200958 if (tbl == NULL)
959 return -ENOMEM;
960
Ying Xue545a1482015-01-07 13:41:57 +0800961 atomic_set(&ht->nelems, 0);
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100962
Thomas Graf7e1e7762014-08-02 11:47:44 +0200963 RCU_INIT_POINTER(ht->tbl, tbl);
Thomas Graf97defe12015-01-02 23:00:20 +0100964 RCU_INIT_POINTER(ht->future_tbl, tbl);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200965
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100966 INIT_WORK(&ht->run_work, rht_deferred_worker);
Thomas Graf97defe12015-01-02 23:00:20 +0100967
Thomas Graf7e1e7762014-08-02 11:47:44 +0200968 return 0;
969}
970EXPORT_SYMBOL_GPL(rhashtable_init);
971
972/**
973 * rhashtable_destroy - destroy hash table
974 * @ht: the hash table to destroy
975 *
Pablo Neira Ayusoae82ddc2014-09-02 00:26:05 +0200976 * Frees the bucket array. This function is not rcu safe, therefore the caller
977 * has to make sure that no resizing may happen by unpublishing the hashtable
978 * and waiting for the quiescent cycle before releasing the bucket array.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200979 */
Thomas Graf97defe12015-01-02 23:00:20 +0100980void rhashtable_destroy(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200981{
Thomas Graf97defe12015-01-02 23:00:20 +0100982 ht->being_destroyed = true;
983
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100984 cancel_work_sync(&ht->run_work);
Ying Xue57699a42015-01-16 11:13:09 +0800985
Thomas Graf97defe12015-01-02 23:00:20 +0100986 mutex_lock(&ht->mutex);
Thomas Graf97defe12015-01-02 23:00:20 +0100987 bucket_table_free(rht_dereference(ht->tbl, ht));
Thomas Graf97defe12015-01-02 23:00:20 +0100988 mutex_unlock(&ht->mutex);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200989}
990EXPORT_SYMBOL_GPL(rhashtable_destroy);