blob: 36fb0910bec21085b6f23bfe1847f05e816c398a [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 ;
263 spin_unlock_bh(old_bucket_lock);
264}
265
266static void rhashtable_rehash(struct rhashtable *ht,
267 struct bucket_table *new_tbl)
268{
269 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100270 struct rhashtable_walker *walker;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100271 unsigned old_hash;
272
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100273 /* Make insertions go into the new, empty table right away. Deletions
274 * and lookups will be attempted in both tables until we synchronize.
275 * The synchronize_rcu() guarantees for the new table to be picked up
276 * so no new additions go into the old table while we relink.
277 */
278 rcu_assign_pointer(ht->future_tbl, new_tbl);
279
Herbert Xu9497df82015-03-12 22:07:49 +1100280 /* Ensure the new table is visible to readers. */
281 smp_wmb();
282
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100283 for (old_hash = 0; old_hash < old_tbl->size; old_hash++)
284 rhashtable_rehash_chain(ht, old_hash);
285
286 /* Publish the new table pointer. */
287 rcu_assign_pointer(ht->tbl, new_tbl);
288
Herbert Xueddee5ba2015-03-14 13:57:20 +1100289 list_for_each_entry(walker, &old_tbl->walkers, list)
290 walker->tbl = NULL;
291
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100292 /* Wait for readers. All new readers will see the new
293 * table, and thus no references to the old table will
294 * remain.
295 */
Herbert Xu9d901bc2015-03-14 13:57:23 +1100296 call_rcu(&old_tbl->rcu, bucket_table_free_rcu);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200297}
298
299/**
300 * rhashtable_expand - Expand hash table while allowing concurrent lookups
301 * @ht: the hash table to expand
Thomas Graf7e1e7762014-08-02 11:47:44 +0200302 *
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100303 * A secondary bucket array is allocated and the hash entries are migrated.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200304 *
305 * This function may only be called in a context where it is safe to call
306 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
307 *
Thomas Graf97defe12015-01-02 23:00:20 +0100308 * The caller must ensure that no concurrent resizing occurs by holding
309 * ht->mutex.
310 *
311 * It is valid to have concurrent insertions and deletions protected by per
312 * bucket locks or concurrent RCU protected lookups and traversals.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200313 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100314int rhashtable_expand(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200315{
316 struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200317
318 ASSERT_RHT_MUTEX(ht);
319
Herbert Xu5269b532015-03-14 13:57:22 +1100320 new_tbl = bucket_table_alloc(ht, old_tbl->size * 2);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200321 if (new_tbl == NULL)
322 return -ENOMEM;
323
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100324 rhashtable_rehash(ht, new_tbl);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200325 return 0;
326}
327EXPORT_SYMBOL_GPL(rhashtable_expand);
328
329/**
330 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
331 * @ht: the hash table to shrink
Thomas Graf7e1e7762014-08-02 11:47:44 +0200332 *
333 * This function may only be called in a context where it is safe to call
334 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
335 *
Thomas Graf97defe12015-01-02 23:00:20 +0100336 * The caller must ensure that no concurrent resizing occurs by holding
337 * ht->mutex.
338 *
Thomas Graf7e1e7762014-08-02 11:47:44 +0200339 * The caller must ensure that no concurrent table mutations take place.
340 * It is however valid to have concurrent lookups if they are RCU protected.
Thomas Graf97defe12015-01-02 23:00:20 +0100341 *
342 * It is valid to have concurrent insertions and deletions protected by per
343 * bucket locks or concurrent RCU protected lookups and traversals.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200344 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100345int rhashtable_shrink(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200346{
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100347 struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200348
349 ASSERT_RHT_MUTEX(ht);
350
Herbert Xu5269b532015-03-14 13:57:22 +1100351 new_tbl = bucket_table_alloc(ht, old_tbl->size / 2);
Thomas Graf97defe12015-01-02 23:00:20 +0100352 if (new_tbl == NULL)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200353 return -ENOMEM;
354
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100355 rhashtable_rehash(ht, new_tbl);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200356 return 0;
357}
358EXPORT_SYMBOL_GPL(rhashtable_shrink);
359
Thomas Graf97defe12015-01-02 23:00:20 +0100360static void rht_deferred_worker(struct work_struct *work)
361{
362 struct rhashtable *ht;
363 struct bucket_table *tbl;
364
Ying Xue57699a42015-01-16 11:13:09 +0800365 ht = container_of(work, struct rhashtable, run_work);
Thomas Graf97defe12015-01-02 23:00:20 +0100366 mutex_lock(&ht->mutex);
Herbert Xu28134a52015-02-04 07:33:22 +1100367 if (ht->being_destroyed)
368 goto unlock;
369
Thomas Graf97defe12015-01-02 23:00:20 +0100370 tbl = rht_dereference(ht->tbl, ht);
371
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100372 if (rht_grow_above_75(ht, tbl))
Thomas Graf97defe12015-01-02 23:00:20 +0100373 rhashtable_expand(ht);
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100374 else if (rht_shrink_below_30(ht, tbl))
Thomas Graf97defe12015-01-02 23:00:20 +0100375 rhashtable_shrink(ht);
Herbert Xu28134a52015-02-04 07:33:22 +1100376unlock:
Thomas Graf97defe12015-01-02 23:00:20 +0100377 mutex_unlock(&ht->mutex);
378}
379
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100380static bool __rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj,
381 bool (*compare)(void *, void *), void *arg)
Ying Xuedb304852015-01-07 13:41:54 +0800382{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100383 struct bucket_table *tbl, *old_tbl;
Thomas Graf020219a2015-02-06 16:08:43 +0000384 struct rhash_head *head;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100385 bool no_resize_running;
386 unsigned hash;
387 bool success = true;
388
389 rcu_read_lock();
390
391 old_tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xueca84932015-03-12 14:49:39 +1100392 hash = head_hashfn(ht, old_tbl, obj);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100393
394 spin_lock_bh(bucket_lock(old_tbl, hash));
395
396 /* Because we have already taken the bucket lock in old_tbl,
397 * if we find that future_tbl is not yet visible then that
398 * guarantees all other insertions of the same entry will
399 * also grab the bucket lock in old_tbl because until the
400 * rehash completes ht->tbl won't be changed.
401 */
402 tbl = rht_dereference_rcu(ht->future_tbl, ht);
403 if (tbl != old_tbl) {
Herbert Xueca84932015-03-12 14:49:39 +1100404 hash = head_hashfn(ht, tbl, obj);
Herbert Xu8f2484b2015-03-14 13:57:21 +1100405 spin_lock_nested(bucket_lock(tbl, hash), SINGLE_DEPTH_NESTING);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100406 }
407
408 if (compare &&
409 rhashtable_lookup_compare(ht, rht_obj(ht, obj) + ht->p.key_offset,
410 compare, arg)) {
411 success = false;
412 goto exit;
413 }
414
415 no_resize_running = tbl == old_tbl;
Thomas Graf020219a2015-02-06 16:08:43 +0000416
Thomas Graf020219a2015-02-06 16:08:43 +0000417 head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash);
Ying Xuedb304852015-01-07 13:41:54 +0800418
419 if (rht_is_a_nulls(head))
420 INIT_RHT_NULLS_HEAD(obj->next, ht, hash);
421 else
422 RCU_INIT_POINTER(obj->next, head);
423
424 rcu_assign_pointer(tbl->buckets[hash], obj);
425
426 atomic_inc(&ht->nelems);
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100427 if (no_resize_running && rht_grow_above_75(ht, tbl))
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100428 schedule_work(&ht->run_work);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100429
430exit:
431 if (tbl != old_tbl) {
Herbert Xueca84932015-03-12 14:49:39 +1100432 hash = head_hashfn(ht, tbl, obj);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100433 spin_unlock(bucket_lock(tbl, hash));
434 }
435
Herbert Xueca84932015-03-12 14:49:39 +1100436 hash = head_hashfn(ht, old_tbl, obj);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100437 spin_unlock_bh(bucket_lock(old_tbl, hash));
438
439 rcu_read_unlock();
440
441 return success;
Ying Xuedb304852015-01-07 13:41:54 +0800442}
443
Thomas Graf7e1e7762014-08-02 11:47:44 +0200444/**
Ying Xuedb304852015-01-07 13:41:54 +0800445 * rhashtable_insert - insert object into hash table
Thomas Graf7e1e7762014-08-02 11:47:44 +0200446 * @ht: hash table
447 * @obj: pointer to hash head inside object
Thomas Graf7e1e7762014-08-02 11:47:44 +0200448 *
Thomas Graf97defe12015-01-02 23:00:20 +0100449 * Will take a per bucket spinlock to protect against mutual mutations
450 * on the same bucket. Multiple insertions may occur in parallel unless
451 * they map to the same bucket lock.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200452 *
Thomas Graf97defe12015-01-02 23:00:20 +0100453 * It is safe to call this function from atomic context.
454 *
455 * Will trigger an automatic deferred table resizing if the size grows
456 * beyond the watermark indicated by grow_decision() which can be passed
457 * to rhashtable_init().
Thomas Graf7e1e7762014-08-02 11:47:44 +0200458 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100459void rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200460{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100461 __rhashtable_insert(ht, obj, NULL, NULL);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200462}
463EXPORT_SYMBOL_GPL(rhashtable_insert);
464
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100465static bool __rhashtable_remove(struct rhashtable *ht,
466 struct bucket_table *tbl,
467 struct rhash_head *obj)
468{
469 struct rhash_head __rcu **pprev;
470 struct rhash_head *he;
471 spinlock_t * lock;
472 unsigned hash;
473 bool ret = false;
474
Herbert Xueca84932015-03-12 14:49:39 +1100475 hash = head_hashfn(ht, tbl, obj);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100476 lock = bucket_lock(tbl, hash);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100477
478 spin_lock_bh(lock);
479
480 pprev = &tbl->buckets[hash];
481 rht_for_each(he, tbl, hash) {
482 if (he != obj) {
483 pprev = &he->next;
484 continue;
485 }
486
487 rcu_assign_pointer(*pprev, obj->next);
488 ret = true;
489 break;
490 }
491
492 spin_unlock_bh(lock);
493
494 return ret;
495}
496
Thomas Graf7e1e7762014-08-02 11:47:44 +0200497/**
Thomas Graf7e1e7762014-08-02 11:47:44 +0200498 * rhashtable_remove - remove object from hash table
499 * @ht: hash table
500 * @obj: pointer to hash head inside object
Thomas Graf7e1e7762014-08-02 11:47:44 +0200501 *
502 * Since the hash chain is single linked, the removal operation needs to
503 * walk the bucket chain upon removal. The removal operation is thus
504 * considerable slow if the hash table is not correctly sized.
505 *
Ying Xuedb304852015-01-07 13:41:54 +0800506 * Will automatically shrink the table via rhashtable_expand() if the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200507 * shrink_decision function specified at rhashtable_init() returns true.
508 *
509 * The caller must ensure that no concurrent table mutations occur. It is
510 * however valid to have concurrent lookups if they are RCU protected.
511 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100512bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *obj)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200513{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100514 struct bucket_table *tbl, *old_tbl;
515 bool ret;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200516
Thomas Graf97defe12015-01-02 23:00:20 +0100517 rcu_read_lock();
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100518
Thomas Graf020219a2015-02-06 16:08:43 +0000519 old_tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100520 ret = __rhashtable_remove(ht, old_tbl, obj);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200521
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100522 /* Because we have already taken (and released) the bucket
523 * lock in old_tbl, if we find that future_tbl is not yet
524 * visible then that guarantees the entry to still be in
525 * old_tbl if it exists.
Thomas Graffe6a0432015-01-21 11:54:01 +0000526 */
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100527 tbl = rht_dereference_rcu(ht->future_tbl, ht);
528 if (!ret && old_tbl != tbl)
529 ret = __rhashtable_remove(ht, tbl, obj);
Thomas Graffe6a0432015-01-21 11:54:01 +0000530
531 if (ret) {
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100532 bool no_resize_running = tbl == old_tbl;
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100533
Thomas Graffe6a0432015-01-21 11:54:01 +0000534 atomic_dec(&ht->nelems);
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100535 if (no_resize_running && rht_shrink_below_30(ht, tbl))
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100536 schedule_work(&ht->run_work);
Thomas Graffe6a0432015-01-21 11:54:01 +0000537 }
538
Thomas Graf97defe12015-01-02 23:00:20 +0100539 rcu_read_unlock();
540
Thomas Graffe6a0432015-01-21 11:54:01 +0000541 return ret;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200542}
543EXPORT_SYMBOL_GPL(rhashtable_remove);
544
Ying Xueefb975a62015-01-07 13:41:52 +0800545struct rhashtable_compare_arg {
546 struct rhashtable *ht;
547 const void *key;
548};
549
550static bool rhashtable_compare(void *ptr, void *arg)
551{
552 struct rhashtable_compare_arg *x = arg;
553 struct rhashtable *ht = x->ht;
554
555 return !memcmp(ptr + ht->p.key_offset, x->key, ht->p.key_len);
556}
557
Thomas Graf7e1e7762014-08-02 11:47:44 +0200558/**
559 * rhashtable_lookup - lookup key in hash table
560 * @ht: hash table
561 * @key: pointer to key
562 *
563 * Computes the hash value for the key and traverses the bucket chain looking
564 * for a entry with an identical key. The first matching entry is returned.
565 *
566 * This lookup function may only be used for fixed key hash table (key_len
Ying Xuedb304852015-01-07 13:41:54 +0800567 * parameter set). It will BUG() if used inappropriately.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200568 *
Thomas Graf97defe12015-01-02 23:00:20 +0100569 * Lookups may occur in parallel with hashtable mutations and resizing.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200570 */
Thomas Graf97defe12015-01-02 23:00:20 +0100571void *rhashtable_lookup(struct rhashtable *ht, const void *key)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200572{
Ying Xueefb975a62015-01-07 13:41:52 +0800573 struct rhashtable_compare_arg arg = {
574 .ht = ht,
575 .key = key,
576 };
Thomas Graf7e1e7762014-08-02 11:47:44 +0200577
578 BUG_ON(!ht->p.key_len);
579
Ying Xueefb975a62015-01-07 13:41:52 +0800580 return rhashtable_lookup_compare(ht, key, &rhashtable_compare, &arg);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200581}
582EXPORT_SYMBOL_GPL(rhashtable_lookup);
583
584/**
585 * rhashtable_lookup_compare - search hash table with compare function
586 * @ht: hash table
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100587 * @key: the pointer to the key
Thomas Graf7e1e7762014-08-02 11:47:44 +0200588 * @compare: compare function, must return true on match
589 * @arg: argument passed on to compare function
590 *
591 * Traverses the bucket chain behind the provided hash value and calls the
592 * specified compare function for each entry.
593 *
Thomas Graf97defe12015-01-02 23:00:20 +0100594 * Lookups may occur in parallel with hashtable mutations and resizing.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200595 *
596 * Returns the first entry on which the compare function returned true.
597 */
Thomas Graf97defe12015-01-02 23:00:20 +0100598void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200599 bool (*compare)(void *, void *), void *arg)
600{
Thomas Graf97defe12015-01-02 23:00:20 +0100601 const struct bucket_table *tbl, *old_tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200602 struct rhash_head *he;
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100603 u32 hash;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200604
Thomas Graf97defe12015-01-02 23:00:20 +0100605 rcu_read_lock();
606
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100607 tbl = rht_dereference_rcu(ht->tbl, ht);
Thomas Graf97defe12015-01-02 23:00:20 +0100608restart:
Herbert Xu39361942015-03-13 12:54:10 +1100609 hash = key_hashfn(ht, tbl, key);
Herbert Xu8d2b1872015-03-12 14:49:38 +1100610 rht_for_each_rcu(he, tbl, hash) {
Thomas Graf7e1e7762014-08-02 11:47:44 +0200611 if (!compare(rht_obj(ht, he), arg))
612 continue;
Thomas Graf97defe12015-01-02 23:00:20 +0100613 rcu_read_unlock();
Thomas Grafa4b18cd2015-01-02 23:00:15 +0100614 return rht_obj(ht, he);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200615 }
616
Herbert Xu9497df82015-03-12 22:07:49 +1100617 /* Ensure we see any new tables. */
618 smp_rmb();
619
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100620 old_tbl = tbl;
621 tbl = rht_dereference_rcu(ht->future_tbl, ht);
622 if (unlikely(tbl != old_tbl))
Thomas Graf97defe12015-01-02 23:00:20 +0100623 goto restart;
Thomas Graf97defe12015-01-02 23:00:20 +0100624 rcu_read_unlock();
625
Thomas Graf7e1e7762014-08-02 11:47:44 +0200626 return NULL;
627}
628EXPORT_SYMBOL_GPL(rhashtable_lookup_compare);
629
Ying Xuedb304852015-01-07 13:41:54 +0800630/**
631 * rhashtable_lookup_insert - lookup and insert object into hash table
632 * @ht: hash table
633 * @obj: pointer to hash head inside object
634 *
635 * Locks down the bucket chain in both the old and new table if a resize
636 * is in progress to ensure that writers can't remove from the old table
637 * and can't insert to the new table during the atomic operation of search
638 * and insertion. Searches for duplicates in both the old and new table if
639 * a resize is in progress.
640 *
641 * This lookup function may only be used for fixed key hash table (key_len
642 * parameter set). It will BUG() if used inappropriately.
643 *
644 * It is safe to call this function from atomic context.
645 *
646 * Will trigger an automatic deferred table resizing if the size grows
647 * beyond the watermark indicated by grow_decision() which can be passed
648 * to rhashtable_init().
649 */
650bool rhashtable_lookup_insert(struct rhashtable *ht, struct rhash_head *obj)
651{
Ying Xue7a868d12015-01-12 14:52:22 +0800652 struct rhashtable_compare_arg arg = {
653 .ht = ht,
654 .key = rht_obj(ht, obj) + ht->p.key_offset,
655 };
656
657 BUG_ON(!ht->p.key_len);
658
659 return rhashtable_lookup_compare_insert(ht, obj, &rhashtable_compare,
660 &arg);
661}
662EXPORT_SYMBOL_GPL(rhashtable_lookup_insert);
663
664/**
665 * rhashtable_lookup_compare_insert - search and insert object to hash table
666 * with compare function
667 * @ht: hash table
668 * @obj: pointer to hash head inside object
669 * @compare: compare function, must return true on match
670 * @arg: argument passed on to compare function
671 *
672 * Locks down the bucket chain in both the old and new table if a resize
673 * is in progress to ensure that writers can't remove from the old table
674 * and can't insert to the new table during the atomic operation of search
675 * and insertion. Searches for duplicates in both the old and new table if
676 * a resize is in progress.
677 *
678 * Lookups may occur in parallel with hashtable mutations and resizing.
679 *
680 * Will trigger an automatic deferred table resizing if the size grows
681 * beyond the watermark indicated by grow_decision() which can be passed
682 * to rhashtable_init().
683 */
684bool rhashtable_lookup_compare_insert(struct rhashtable *ht,
685 struct rhash_head *obj,
686 bool (*compare)(void *, void *),
687 void *arg)
688{
Ying Xuedb304852015-01-07 13:41:54 +0800689 BUG_ON(!ht->p.key_len);
690
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100691 return __rhashtable_insert(ht, obj, compare, arg);
Ying Xuedb304852015-01-07 13:41:54 +0800692}
Ying Xue7a868d12015-01-12 14:52:22 +0800693EXPORT_SYMBOL_GPL(rhashtable_lookup_compare_insert);
Ying Xuedb304852015-01-07 13:41:54 +0800694
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100695/**
696 * rhashtable_walk_init - Initialise an iterator
697 * @ht: Table to walk over
698 * @iter: Hash table Iterator
699 *
700 * This function prepares a hash table walk.
701 *
702 * Note that if you restart a walk after rhashtable_walk_stop you
703 * may see the same object twice. Also, you may miss objects if
704 * there are removals in between rhashtable_walk_stop and the next
705 * call to rhashtable_walk_start.
706 *
707 * For a completely stable walk you should construct your own data
708 * structure outside the hash table.
709 *
710 * This function may sleep so you must not call it from interrupt
711 * context or with spin locks held.
712 *
713 * You must call rhashtable_walk_exit if this function returns
714 * successfully.
715 */
716int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter)
717{
718 iter->ht = ht;
719 iter->p = NULL;
720 iter->slot = 0;
721 iter->skip = 0;
722
723 iter->walker = kmalloc(sizeof(*iter->walker), GFP_KERNEL);
724 if (!iter->walker)
725 return -ENOMEM;
726
727 mutex_lock(&ht->mutex);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100728 iter->walker->tbl = rht_dereference(ht->tbl, ht);
729 list_add(&iter->walker->list, &iter->walker->tbl->walkers);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100730 mutex_unlock(&ht->mutex);
731
732 return 0;
733}
734EXPORT_SYMBOL_GPL(rhashtable_walk_init);
735
736/**
737 * rhashtable_walk_exit - Free an iterator
738 * @iter: Hash table Iterator
739 *
740 * This function frees resources allocated by rhashtable_walk_init.
741 */
742void rhashtable_walk_exit(struct rhashtable_iter *iter)
743{
744 mutex_lock(&iter->ht->mutex);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100745 if (iter->walker->tbl)
746 list_del(&iter->walker->list);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100747 mutex_unlock(&iter->ht->mutex);
748 kfree(iter->walker);
749}
750EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
751
752/**
753 * rhashtable_walk_start - Start a hash table walk
754 * @iter: Hash table iterator
755 *
756 * Start a hash table walk. Note that we take the RCU lock in all
757 * cases including when we return an error. So you must always call
758 * rhashtable_walk_stop to clean up.
759 *
760 * Returns zero if successful.
761 *
762 * Returns -EAGAIN if resize event occured. Note that the iterator
763 * will rewind back to the beginning and you may use it immediately
764 * by calling rhashtable_walk_next.
765 */
766int rhashtable_walk_start(struct rhashtable_iter *iter)
767{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100768 struct rhashtable *ht = iter->ht;
769
770 mutex_lock(&ht->mutex);
771
772 if (iter->walker->tbl)
773 list_del(&iter->walker->list);
774
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100775 rcu_read_lock();
776
Herbert Xueddee5ba2015-03-14 13:57:20 +1100777 mutex_unlock(&ht->mutex);
778
779 if (!iter->walker->tbl) {
780 iter->walker->tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100781 return -EAGAIN;
782 }
783
784 return 0;
785}
786EXPORT_SYMBOL_GPL(rhashtable_walk_start);
787
788/**
789 * rhashtable_walk_next - Return the next object and advance the iterator
790 * @iter: Hash table iterator
791 *
792 * Note that you must call rhashtable_walk_stop when you are finished
793 * with the walk.
794 *
795 * Returns the next object or NULL when the end of the table is reached.
796 *
797 * Returns -EAGAIN if resize event occured. Note that the iterator
798 * will rewind back to the beginning and you may continue to use it.
799 */
800void *rhashtable_walk_next(struct rhashtable_iter *iter)
801{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100802 struct bucket_table *tbl = iter->walker->tbl;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100803 struct rhashtable *ht = iter->ht;
804 struct rhash_head *p = iter->p;
805 void *obj = NULL;
806
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100807 if (p) {
808 p = rht_dereference_bucket_rcu(p->next, tbl, iter->slot);
809 goto next;
810 }
811
812 for (; iter->slot < tbl->size; iter->slot++) {
813 int skip = iter->skip;
814
815 rht_for_each_rcu(p, tbl, iter->slot) {
816 if (!skip)
817 break;
818 skip--;
819 }
820
821next:
822 if (!rht_is_a_nulls(p)) {
823 iter->skip++;
824 iter->p = p;
825 obj = rht_obj(ht, p);
826 goto out;
827 }
828
829 iter->skip = 0;
830 }
831
Herbert Xueddee5ba2015-03-14 13:57:20 +1100832 iter->walker->tbl = rht_dereference_rcu(ht->future_tbl, ht);
833 if (iter->walker->tbl != tbl) {
834 iter->slot = 0;
835 iter->skip = 0;
836 return ERR_PTR(-EAGAIN);
837 }
838
839 iter->walker->tbl = NULL;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100840 iter->p = NULL;
841
842out:
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100843
844 return obj;
845}
846EXPORT_SYMBOL_GPL(rhashtable_walk_next);
847
848/**
849 * rhashtable_walk_stop - Finish a hash table walk
850 * @iter: Hash table iterator
851 *
852 * Finish a hash table walk.
853 */
854void rhashtable_walk_stop(struct rhashtable_iter *iter)
855{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100856 struct rhashtable *ht;
857 struct bucket_table *tbl = iter->walker->tbl;
858
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100859 rcu_read_unlock();
Herbert Xueddee5ba2015-03-14 13:57:20 +1100860
861 if (!tbl)
862 return;
863
864 ht = iter->ht;
865
866 mutex_lock(&ht->mutex);
867 if (rht_dereference(ht->tbl, ht) == tbl ||
868 rht_dereference(ht->future_tbl, ht) == tbl)
869 list_add(&iter->walker->list, &tbl->walkers);
870 else
871 iter->walker->tbl = NULL;
872 mutex_unlock(&ht->mutex);
873
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100874 iter->p = NULL;
875}
876EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
877
Ying Xue94000172014-09-03 09:22:36 +0800878static size_t rounded_hashtable_size(struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200879{
Ying Xue94000172014-09-03 09:22:36 +0800880 return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
881 1UL << params->min_shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200882}
883
884/**
885 * rhashtable_init - initialize a new hash table
886 * @ht: hash table to be initialized
887 * @params: configuration parameters
888 *
889 * Initializes a new hash table based on the provided configuration
890 * parameters. A table can be configured either with a variable or
891 * fixed length key:
892 *
893 * Configuration Example 1: Fixed length keys
894 * struct test_obj {
895 * int key;
896 * void * my_member;
897 * struct rhash_head node;
898 * };
899 *
900 * struct rhashtable_params params = {
901 * .head_offset = offsetof(struct test_obj, node),
902 * .key_offset = offsetof(struct test_obj, key),
903 * .key_len = sizeof(int),
Daniel Borkmann87545892014-12-10 16:33:11 +0100904 * .hashfn = jhash,
Thomas Graff89bd6f2015-01-02 23:00:21 +0100905 * .nulls_base = (1U << RHT_BASE_SHIFT),
Thomas Graf7e1e7762014-08-02 11:47:44 +0200906 * };
907 *
908 * Configuration Example 2: Variable length keys
909 * struct test_obj {
910 * [...]
911 * struct rhash_head node;
912 * };
913 *
914 * u32 my_hash_fn(const void *data, u32 seed)
915 * {
916 * struct test_obj *obj = data;
917 *
918 * return [... hash ...];
919 * }
920 *
921 * struct rhashtable_params params = {
922 * .head_offset = offsetof(struct test_obj, node),
Daniel Borkmann87545892014-12-10 16:33:11 +0100923 * .hashfn = jhash,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200924 * .obj_hashfn = my_hash_fn,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200925 * };
926 */
927int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params)
928{
929 struct bucket_table *tbl;
930 size_t size;
931
932 size = HASH_DEFAULT_SIZE;
933
934 if ((params->key_len && !params->hashfn) ||
935 (!params->key_len && !params->obj_hashfn))
936 return -EINVAL;
937
Thomas Graff89bd6f2015-01-02 23:00:21 +0100938 if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT))
939 return -EINVAL;
940
Ying Xue94000172014-09-03 09:22:36 +0800941 params->min_shift = max_t(size_t, params->min_shift,
942 ilog2(HASH_MIN_SIZE));
943
Thomas Graf7e1e7762014-08-02 11:47:44 +0200944 if (params->nelem_hint)
Ying Xue94000172014-09-03 09:22:36 +0800945 size = rounded_hashtable_size(params);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200946
Thomas Graf97defe12015-01-02 23:00:20 +0100947 memset(ht, 0, sizeof(*ht));
948 mutex_init(&ht->mutex);
949 memcpy(&ht->p, params, sizeof(*params));
950
951 if (params->locks_mul)
952 ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
953 else
954 ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
955
Herbert Xu5269b532015-03-14 13:57:22 +1100956 tbl = bucket_table_alloc(ht, size);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200957 if (tbl == NULL)
958 return -ENOMEM;
959
Ying Xue545a1482015-01-07 13:41:57 +0800960 atomic_set(&ht->nelems, 0);
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100961
Thomas Graf7e1e7762014-08-02 11:47:44 +0200962 RCU_INIT_POINTER(ht->tbl, tbl);
Thomas Graf97defe12015-01-02 23:00:20 +0100963 RCU_INIT_POINTER(ht->future_tbl, tbl);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200964
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100965 INIT_WORK(&ht->run_work, rht_deferred_worker);
Thomas Graf97defe12015-01-02 23:00:20 +0100966
Thomas Graf7e1e7762014-08-02 11:47:44 +0200967 return 0;
968}
969EXPORT_SYMBOL_GPL(rhashtable_init);
970
971/**
972 * rhashtable_destroy - destroy hash table
973 * @ht: the hash table to destroy
974 *
Pablo Neira Ayusoae82ddc2014-09-02 00:26:05 +0200975 * Frees the bucket array. This function is not rcu safe, therefore the caller
976 * has to make sure that no resizing may happen by unpublishing the hashtable
977 * and waiting for the quiescent cycle before releasing the bucket array.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200978 */
Thomas Graf97defe12015-01-02 23:00:20 +0100979void rhashtable_destroy(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200980{
Thomas Graf97defe12015-01-02 23:00:20 +0100981 ht->being_destroyed = true;
982
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100983 cancel_work_sync(&ht->run_work);
Ying Xue57699a42015-01-16 11:13:09 +0800984
Thomas Graf97defe12015-01-02 23:00:20 +0100985 mutex_lock(&ht->mutex);
Thomas Graf97defe12015-01-02 23:00:20 +0100986 bucket_table_free(rht_dereference(ht->tbl, ht));
Thomas Graf97defe12015-01-02 23:00:20 +0100987 mutex_unlock(&ht->mutex);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200988}
989EXPORT_SYMBOL_GPL(rhashtable_destroy);