blob: 09a7ada89ade49f91108187f7ca8dcc1e52ede8c [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 *old_tbl = rht_dereference(ht->tbl, ht);
Herbert Xuc4db8842015-03-14 13:57:25 +1100211 struct bucket_table *new_tbl =
212 rht_dereference(old_tbl->future_tbl, ht) ?: old_tbl;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100213 struct rhash_head __rcu **pprev = &old_tbl->buckets[old_hash];
214 int err = -ENOENT;
215 struct rhash_head *head, *next, *entry;
216 spinlock_t *new_bucket_lock;
217 unsigned new_hash;
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100218
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100219 rht_for_each(entry, old_tbl, old_hash) {
220 err = 0;
221 next = rht_dereference_bucket(entry->next, old_tbl, old_hash);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100222
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100223 if (rht_is_a_nulls(next))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200224 break;
Thomas Graf97defe12015-01-02 23:00:20 +0100225
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100226 pprev = &entry->next;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200227 }
228
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100229 if (err)
230 goto out;
Thomas Graf97defe12015-01-02 23:00:20 +0100231
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100232 new_hash = head_hashfn(ht, new_tbl, entry);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100233
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100234 new_bucket_lock = bucket_lock(new_tbl, new_hash);
235
Herbert Xu8f2484b2015-03-14 13:57:21 +1100236 spin_lock_nested(new_bucket_lock, SINGLE_DEPTH_NESTING);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100237 head = rht_dereference_bucket(new_tbl->buckets[new_hash],
238 new_tbl, new_hash);
239
240 if (rht_is_a_nulls(head))
241 INIT_RHT_NULLS_HEAD(entry->next, ht, new_hash);
242 else
243 RCU_INIT_POINTER(entry->next, head);
244
245 rcu_assign_pointer(new_tbl->buckets[new_hash], entry);
246 spin_unlock(new_bucket_lock);
247
248 rcu_assign_pointer(*pprev, next);
249
250out:
251 return err;
Thomas Graf97defe12015-01-02 23:00:20 +0100252}
253
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100254static void rhashtable_rehash_chain(struct rhashtable *ht, unsigned old_hash)
Thomas Graf97defe12015-01-02 23:00:20 +0100255{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100256 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
257 spinlock_t *old_bucket_lock;
Thomas Graf7cd10db2015-02-05 02:03:35 +0100258
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100259 old_bucket_lock = bucket_lock(old_tbl, old_hash);
260
261 spin_lock_bh(old_bucket_lock);
262 while (!rhashtable_rehash_one(ht, old_hash))
263 ;
Herbert Xu63d512d2015-03-14 13:57:24 +1100264 old_tbl->rehash++;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100265 spin_unlock_bh(old_bucket_lock);
266}
267
268static void rhashtable_rehash(struct rhashtable *ht,
269 struct bucket_table *new_tbl)
270{
271 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100272 struct rhashtable_walker *walker;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100273 unsigned old_hash;
274
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100275 /* Make insertions go into the new, empty table right away. Deletions
276 * and lookups will be attempted in both tables until we synchronize.
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100277 */
Herbert Xuc4db8842015-03-14 13:57:25 +1100278 rcu_assign_pointer(old_tbl->future_tbl, new_tbl);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100279
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;
Thomas Graf617011e2015-03-16 10:42:26 +0100387 spinlock_t *old_lock;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100388 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);
Thomas Graf617011e2015-03-16 10:42:26 +0100394 old_lock = bucket_lock(old_tbl, hash);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100395
Thomas Graf617011e2015-03-16 10:42:26 +0100396 spin_lock_bh(old_lock);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100397
398 /* Because we have already taken the bucket lock in old_tbl,
399 * if we find that future_tbl is not yet visible then that
400 * guarantees all other insertions of the same entry will
401 * also grab the bucket lock in old_tbl because until the
402 * rehash completes ht->tbl won't be changed.
403 */
Herbert Xuc4db8842015-03-14 13:57:25 +1100404 tbl = rht_dereference_rcu(old_tbl->future_tbl, ht) ?: old_tbl;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100405 if (tbl != old_tbl) {
Herbert Xueca84932015-03-12 14:49:39 +1100406 hash = head_hashfn(ht, tbl, obj);
Herbert Xu8f2484b2015-03-14 13:57:21 +1100407 spin_lock_nested(bucket_lock(tbl, hash), SINGLE_DEPTH_NESTING);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100408 }
409
410 if (compare &&
411 rhashtable_lookup_compare(ht, rht_obj(ht, obj) + ht->p.key_offset,
412 compare, arg)) {
413 success = false;
414 goto exit;
415 }
416
417 no_resize_running = tbl == old_tbl;
Thomas Graf020219a2015-02-06 16:08:43 +0000418
Thomas Graf020219a2015-02-06 16:08:43 +0000419 head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash);
Ying Xuedb304852015-01-07 13:41:54 +0800420
421 if (rht_is_a_nulls(head))
422 INIT_RHT_NULLS_HEAD(obj->next, ht, hash);
423 else
424 RCU_INIT_POINTER(obj->next, head);
425
426 rcu_assign_pointer(tbl->buckets[hash], obj);
427
428 atomic_inc(&ht->nelems);
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100429 if (no_resize_running && rht_grow_above_75(ht, tbl))
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100430 schedule_work(&ht->run_work);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100431
432exit:
Thomas Graf617011e2015-03-16 10:42:26 +0100433 if (tbl != old_tbl)
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100434 spin_unlock(bucket_lock(tbl, hash));
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100435
Thomas Graf617011e2015-03-16 10:42:26 +0100436 spin_unlock_bh(old_lock);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100437
438 rcu_read_unlock();
439
440 return success;
Ying Xuedb304852015-01-07 13:41:54 +0800441}
442
Thomas Graf7e1e7762014-08-02 11:47:44 +0200443/**
Ying Xuedb304852015-01-07 13:41:54 +0800444 * rhashtable_insert - insert object into hash table
Thomas Graf7e1e7762014-08-02 11:47:44 +0200445 * @ht: hash table
446 * @obj: pointer to hash head inside object
Thomas Graf7e1e7762014-08-02 11:47:44 +0200447 *
Thomas Graf97defe12015-01-02 23:00:20 +0100448 * Will take a per bucket spinlock to protect against mutual mutations
449 * on the same bucket. Multiple insertions may occur in parallel unless
450 * they map to the same bucket lock.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200451 *
Thomas Graf97defe12015-01-02 23:00:20 +0100452 * It is safe to call this function from atomic context.
453 *
454 * Will trigger an automatic deferred table resizing if the size grows
455 * beyond the watermark indicated by grow_decision() which can be passed
456 * to rhashtable_init().
Thomas Graf7e1e7762014-08-02 11:47:44 +0200457 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100458void rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200459{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100460 __rhashtable_insert(ht, obj, NULL, NULL);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200461}
462EXPORT_SYMBOL_GPL(rhashtable_insert);
463
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100464static bool __rhashtable_remove(struct rhashtable *ht,
465 struct bucket_table *tbl,
466 struct rhash_head *obj)
467{
468 struct rhash_head __rcu **pprev;
469 struct rhash_head *he;
470 spinlock_t * lock;
471 unsigned hash;
472 bool ret = false;
473
Herbert Xueca84932015-03-12 14:49:39 +1100474 hash = head_hashfn(ht, tbl, obj);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100475 lock = bucket_lock(tbl, hash);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100476
477 spin_lock_bh(lock);
478
479 pprev = &tbl->buckets[hash];
480 rht_for_each(he, tbl, hash) {
481 if (he != obj) {
482 pprev = &he->next;
483 continue;
484 }
485
486 rcu_assign_pointer(*pprev, obj->next);
487 ret = true;
488 break;
489 }
490
491 spin_unlock_bh(lock);
492
493 return ret;
494}
495
Thomas Graf7e1e7762014-08-02 11:47:44 +0200496/**
Thomas Graf7e1e7762014-08-02 11:47:44 +0200497 * rhashtable_remove - remove object from hash table
498 * @ht: hash table
499 * @obj: pointer to hash head inside object
Thomas Graf7e1e7762014-08-02 11:47:44 +0200500 *
501 * Since the hash chain is single linked, the removal operation needs to
502 * walk the bucket chain upon removal. The removal operation is thus
503 * considerable slow if the hash table is not correctly sized.
504 *
Ying Xuedb304852015-01-07 13:41:54 +0800505 * Will automatically shrink the table via rhashtable_expand() if the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200506 * shrink_decision function specified at rhashtable_init() returns true.
507 *
508 * The caller must ensure that no concurrent table mutations occur. It is
509 * however valid to have concurrent lookups if they are RCU protected.
510 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100511bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *obj)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200512{
Herbert Xu565e8642015-03-15 21:12:05 +1100513 struct bucket_table *tbl;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100514 bool ret;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200515
Thomas Graf97defe12015-01-02 23:00:20 +0100516 rcu_read_lock();
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100517
Herbert Xu565e8642015-03-15 21:12:05 +1100518 tbl = rht_dereference_rcu(ht->tbl, ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200519
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100520 /* Because we have already taken (and released) the bucket
521 * lock in old_tbl, if we find that future_tbl is not yet
522 * visible then that guarantees the entry to still be in
Herbert Xu565e8642015-03-15 21:12:05 +1100523 * the old tbl if it exists.
Thomas Graffe6a0432015-01-21 11:54:01 +0000524 */
Herbert Xu565e8642015-03-15 21:12:05 +1100525 while (!(ret = __rhashtable_remove(ht, tbl, obj)) &&
526 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
527 ;
Thomas Graffe6a0432015-01-21 11:54:01 +0000528
529 if (ret) {
530 atomic_dec(&ht->nelems);
Herbert Xu565e8642015-03-15 21:12:05 +1100531 if (rht_shrink_below_30(ht, tbl))
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100532 schedule_work(&ht->run_work);
Thomas Graffe6a0432015-01-21 11:54:01 +0000533 }
534
Thomas Graf97defe12015-01-02 23:00:20 +0100535 rcu_read_unlock();
536
Thomas Graffe6a0432015-01-21 11:54:01 +0000537 return ret;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200538}
539EXPORT_SYMBOL_GPL(rhashtable_remove);
540
Ying Xueefb975a62015-01-07 13:41:52 +0800541struct rhashtable_compare_arg {
542 struct rhashtable *ht;
543 const void *key;
544};
545
546static bool rhashtable_compare(void *ptr, void *arg)
547{
548 struct rhashtable_compare_arg *x = arg;
549 struct rhashtable *ht = x->ht;
550
551 return !memcmp(ptr + ht->p.key_offset, x->key, ht->p.key_len);
552}
553
Thomas Graf7e1e7762014-08-02 11:47:44 +0200554/**
555 * rhashtable_lookup - lookup key in hash table
556 * @ht: hash table
557 * @key: pointer to key
558 *
559 * Computes the hash value for the key and traverses the bucket chain looking
560 * for a entry with an identical key. The first matching entry is returned.
561 *
562 * This lookup function may only be used for fixed key hash table (key_len
Ying Xuedb304852015-01-07 13:41:54 +0800563 * parameter set). It will BUG() if used inappropriately.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200564 *
Thomas Graf97defe12015-01-02 23:00:20 +0100565 * Lookups may occur in parallel with hashtable mutations and resizing.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200566 */
Thomas Graf97defe12015-01-02 23:00:20 +0100567void *rhashtable_lookup(struct rhashtable *ht, const void *key)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200568{
Ying Xueefb975a62015-01-07 13:41:52 +0800569 struct rhashtable_compare_arg arg = {
570 .ht = ht,
571 .key = key,
572 };
Thomas Graf7e1e7762014-08-02 11:47:44 +0200573
574 BUG_ON(!ht->p.key_len);
575
Ying Xueefb975a62015-01-07 13:41:52 +0800576 return rhashtable_lookup_compare(ht, key, &rhashtable_compare, &arg);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200577}
578EXPORT_SYMBOL_GPL(rhashtable_lookup);
579
580/**
581 * rhashtable_lookup_compare - search hash table with compare function
582 * @ht: hash table
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100583 * @key: the pointer to the key
Thomas Graf7e1e7762014-08-02 11:47:44 +0200584 * @compare: compare function, must return true on match
585 * @arg: argument passed on to compare function
586 *
587 * Traverses the bucket chain behind the provided hash value and calls the
588 * specified compare function for each entry.
589 *
Thomas Graf97defe12015-01-02 23:00:20 +0100590 * Lookups may occur in parallel with hashtable mutations and resizing.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200591 *
592 * Returns the first entry on which the compare function returned true.
593 */
Thomas Graf97defe12015-01-02 23:00:20 +0100594void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200595 bool (*compare)(void *, void *), void *arg)
596{
Herbert Xuc4db8842015-03-14 13:57:25 +1100597 const struct bucket_table *tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200598 struct rhash_head *he;
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100599 u32 hash;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200600
Thomas Graf97defe12015-01-02 23:00:20 +0100601 rcu_read_lock();
602
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100603 tbl = rht_dereference_rcu(ht->tbl, ht);
Thomas Graf97defe12015-01-02 23:00:20 +0100604restart:
Herbert Xu39361942015-03-13 12:54:10 +1100605 hash = key_hashfn(ht, tbl, key);
Herbert Xu8d2b1872015-03-12 14:49:38 +1100606 rht_for_each_rcu(he, tbl, hash) {
Thomas Graf7e1e7762014-08-02 11:47:44 +0200607 if (!compare(rht_obj(ht, he), arg))
608 continue;
Thomas Graf97defe12015-01-02 23:00:20 +0100609 rcu_read_unlock();
Thomas Grafa4b18cd2015-01-02 23:00:15 +0100610 return rht_obj(ht, he);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200611 }
612
Herbert Xu9497df82015-03-12 22:07:49 +1100613 /* Ensure we see any new tables. */
614 smp_rmb();
615
Herbert Xuc4db8842015-03-14 13:57:25 +1100616 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
617 if (unlikely(tbl))
Thomas Graf97defe12015-01-02 23:00:20 +0100618 goto restart;
Thomas Graf97defe12015-01-02 23:00:20 +0100619 rcu_read_unlock();
620
Thomas Graf7e1e7762014-08-02 11:47:44 +0200621 return NULL;
622}
623EXPORT_SYMBOL_GPL(rhashtable_lookup_compare);
624
Ying Xuedb304852015-01-07 13:41:54 +0800625/**
626 * rhashtable_lookup_insert - lookup and insert object into hash table
627 * @ht: hash table
628 * @obj: pointer to hash head inside object
629 *
630 * Locks down the bucket chain in both the old and new table if a resize
631 * is in progress to ensure that writers can't remove from the old table
632 * and can't insert to the new table during the atomic operation of search
633 * and insertion. Searches for duplicates in both the old and new table if
634 * a resize is in progress.
635 *
636 * This lookup function may only be used for fixed key hash table (key_len
637 * parameter set). It will BUG() if used inappropriately.
638 *
639 * It is safe to call this function from atomic context.
640 *
641 * Will trigger an automatic deferred table resizing if the size grows
642 * beyond the watermark indicated by grow_decision() which can be passed
643 * to rhashtable_init().
644 */
645bool rhashtable_lookup_insert(struct rhashtable *ht, struct rhash_head *obj)
646{
Ying Xue7a868d12015-01-12 14:52:22 +0800647 struct rhashtable_compare_arg arg = {
648 .ht = ht,
649 .key = rht_obj(ht, obj) + ht->p.key_offset,
650 };
651
652 BUG_ON(!ht->p.key_len);
653
654 return rhashtable_lookup_compare_insert(ht, obj, &rhashtable_compare,
655 &arg);
656}
657EXPORT_SYMBOL_GPL(rhashtable_lookup_insert);
658
659/**
660 * rhashtable_lookup_compare_insert - search and insert object to hash table
661 * with compare function
662 * @ht: hash table
663 * @obj: pointer to hash head inside object
664 * @compare: compare function, must return true on match
665 * @arg: argument passed on to compare function
666 *
667 * Locks down the bucket chain in both the old and new table if a resize
668 * is in progress to ensure that writers can't remove from the old table
669 * and can't insert to the new table during the atomic operation of search
670 * and insertion. Searches for duplicates in both the old and new table if
671 * a resize is in progress.
672 *
673 * Lookups may occur in parallel with hashtable mutations and resizing.
674 *
675 * Will trigger an automatic deferred table resizing if the size grows
676 * beyond the watermark indicated by grow_decision() which can be passed
677 * to rhashtable_init().
678 */
679bool rhashtable_lookup_compare_insert(struct rhashtable *ht,
680 struct rhash_head *obj,
681 bool (*compare)(void *, void *),
682 void *arg)
683{
Ying Xuedb304852015-01-07 13:41:54 +0800684 BUG_ON(!ht->p.key_len);
685
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100686 return __rhashtable_insert(ht, obj, compare, arg);
Ying Xuedb304852015-01-07 13:41:54 +0800687}
Ying Xue7a868d12015-01-12 14:52:22 +0800688EXPORT_SYMBOL_GPL(rhashtable_lookup_compare_insert);
Ying Xuedb304852015-01-07 13:41:54 +0800689
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100690/**
691 * rhashtable_walk_init - Initialise an iterator
692 * @ht: Table to walk over
693 * @iter: Hash table Iterator
694 *
695 * This function prepares a hash table walk.
696 *
697 * Note that if you restart a walk after rhashtable_walk_stop you
698 * may see the same object twice. Also, you may miss objects if
699 * there are removals in between rhashtable_walk_stop and the next
700 * call to rhashtable_walk_start.
701 *
702 * For a completely stable walk you should construct your own data
703 * structure outside the hash table.
704 *
705 * This function may sleep so you must not call it from interrupt
706 * context or with spin locks held.
707 *
708 * You must call rhashtable_walk_exit if this function returns
709 * successfully.
710 */
711int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter)
712{
713 iter->ht = ht;
714 iter->p = NULL;
715 iter->slot = 0;
716 iter->skip = 0;
717
718 iter->walker = kmalloc(sizeof(*iter->walker), GFP_KERNEL);
719 if (!iter->walker)
720 return -ENOMEM;
721
722 mutex_lock(&ht->mutex);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100723 iter->walker->tbl = rht_dereference(ht->tbl, ht);
724 list_add(&iter->walker->list, &iter->walker->tbl->walkers);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100725 mutex_unlock(&ht->mutex);
726
727 return 0;
728}
729EXPORT_SYMBOL_GPL(rhashtable_walk_init);
730
731/**
732 * rhashtable_walk_exit - Free an iterator
733 * @iter: Hash table Iterator
734 *
735 * This function frees resources allocated by rhashtable_walk_init.
736 */
737void rhashtable_walk_exit(struct rhashtable_iter *iter)
738{
739 mutex_lock(&iter->ht->mutex);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100740 if (iter->walker->tbl)
741 list_del(&iter->walker->list);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100742 mutex_unlock(&iter->ht->mutex);
743 kfree(iter->walker);
744}
745EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
746
747/**
748 * rhashtable_walk_start - Start a hash table walk
749 * @iter: Hash table iterator
750 *
751 * Start a hash table walk. Note that we take the RCU lock in all
752 * cases including when we return an error. So you must always call
753 * rhashtable_walk_stop to clean up.
754 *
755 * Returns zero if successful.
756 *
757 * Returns -EAGAIN if resize event occured. Note that the iterator
758 * will rewind back to the beginning and you may use it immediately
759 * by calling rhashtable_walk_next.
760 */
761int rhashtable_walk_start(struct rhashtable_iter *iter)
Thomas Grafdb4374f2015-03-16 10:42:27 +0100762 __acquires(RCU)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100763{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100764 struct rhashtable *ht = iter->ht;
765
766 mutex_lock(&ht->mutex);
767
768 if (iter->walker->tbl)
769 list_del(&iter->walker->list);
770
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100771 rcu_read_lock();
772
Herbert Xueddee5ba2015-03-14 13:57:20 +1100773 mutex_unlock(&ht->mutex);
774
775 if (!iter->walker->tbl) {
776 iter->walker->tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100777 return -EAGAIN;
778 }
779
780 return 0;
781}
782EXPORT_SYMBOL_GPL(rhashtable_walk_start);
783
784/**
785 * rhashtable_walk_next - Return the next object and advance the iterator
786 * @iter: Hash table iterator
787 *
788 * Note that you must call rhashtable_walk_stop when you are finished
789 * with the walk.
790 *
791 * Returns the next object or NULL when the end of the table is reached.
792 *
793 * Returns -EAGAIN if resize event occured. Note that the iterator
794 * will rewind back to the beginning and you may continue to use it.
795 */
796void *rhashtable_walk_next(struct rhashtable_iter *iter)
797{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100798 struct bucket_table *tbl = iter->walker->tbl;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100799 struct rhashtable *ht = iter->ht;
800 struct rhash_head *p = iter->p;
801 void *obj = NULL;
802
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100803 if (p) {
804 p = rht_dereference_bucket_rcu(p->next, tbl, iter->slot);
805 goto next;
806 }
807
808 for (; iter->slot < tbl->size; iter->slot++) {
809 int skip = iter->skip;
810
811 rht_for_each_rcu(p, tbl, iter->slot) {
812 if (!skip)
813 break;
814 skip--;
815 }
816
817next:
818 if (!rht_is_a_nulls(p)) {
819 iter->skip++;
820 iter->p = p;
821 obj = rht_obj(ht, p);
822 goto out;
823 }
824
825 iter->skip = 0;
826 }
827
Herbert Xuc4db8842015-03-14 13:57:25 +1100828 iter->walker->tbl = rht_dereference_rcu(tbl->future_tbl, ht);
829 if (iter->walker->tbl) {
Herbert Xueddee5ba2015-03-14 13:57:20 +1100830 iter->slot = 0;
831 iter->skip = 0;
832 return ERR_PTR(-EAGAIN);
833 }
834
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100835 iter->p = NULL;
836
837out:
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100838
839 return obj;
840}
841EXPORT_SYMBOL_GPL(rhashtable_walk_next);
842
843/**
844 * rhashtable_walk_stop - Finish a hash table walk
845 * @iter: Hash table iterator
846 *
847 * Finish a hash table walk.
848 */
849void rhashtable_walk_stop(struct rhashtable_iter *iter)
Thomas Grafdb4374f2015-03-16 10:42:27 +0100850 __releases(RCU)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100851{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100852 struct rhashtable *ht;
853 struct bucket_table *tbl = iter->walker->tbl;
854
Herbert Xueddee5ba2015-03-14 13:57:20 +1100855 if (!tbl)
Herbert Xu963ecbd2015-03-15 21:12:04 +1100856 goto out;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100857
858 ht = iter->ht;
859
860 mutex_lock(&ht->mutex);
Herbert Xuc4db8842015-03-14 13:57:25 +1100861 if (tbl->rehash < tbl->size)
Herbert Xueddee5ba2015-03-14 13:57:20 +1100862 list_add(&iter->walker->list, &tbl->walkers);
863 else
864 iter->walker->tbl = NULL;
865 mutex_unlock(&ht->mutex);
866
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100867 iter->p = NULL;
Herbert Xu963ecbd2015-03-15 21:12:04 +1100868
869out:
870 rcu_read_unlock();
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100871}
872EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
873
Ying Xue94000172014-09-03 09:22:36 +0800874static size_t rounded_hashtable_size(struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200875{
Ying Xue94000172014-09-03 09:22:36 +0800876 return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
877 1UL << params->min_shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200878}
879
880/**
881 * rhashtable_init - initialize a new hash table
882 * @ht: hash table to be initialized
883 * @params: configuration parameters
884 *
885 * Initializes a new hash table based on the provided configuration
886 * parameters. A table can be configured either with a variable or
887 * fixed length key:
888 *
889 * Configuration Example 1: Fixed length keys
890 * struct test_obj {
891 * int key;
892 * void * my_member;
893 * struct rhash_head node;
894 * };
895 *
896 * struct rhashtable_params params = {
897 * .head_offset = offsetof(struct test_obj, node),
898 * .key_offset = offsetof(struct test_obj, key),
899 * .key_len = sizeof(int),
Daniel Borkmann87545892014-12-10 16:33:11 +0100900 * .hashfn = jhash,
Thomas Graff89bd6f2015-01-02 23:00:21 +0100901 * .nulls_base = (1U << RHT_BASE_SHIFT),
Thomas Graf7e1e7762014-08-02 11:47:44 +0200902 * };
903 *
904 * Configuration Example 2: Variable length keys
905 * struct test_obj {
906 * [...]
907 * struct rhash_head node;
908 * };
909 *
910 * u32 my_hash_fn(const void *data, u32 seed)
911 * {
912 * struct test_obj *obj = data;
913 *
914 * return [... hash ...];
915 * }
916 *
917 * struct rhashtable_params params = {
918 * .head_offset = offsetof(struct test_obj, node),
Daniel Borkmann87545892014-12-10 16:33:11 +0100919 * .hashfn = jhash,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200920 * .obj_hashfn = my_hash_fn,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200921 * };
922 */
923int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params)
924{
925 struct bucket_table *tbl;
926 size_t size;
927
928 size = HASH_DEFAULT_SIZE;
929
930 if ((params->key_len && !params->hashfn) ||
931 (!params->key_len && !params->obj_hashfn))
932 return -EINVAL;
933
Thomas Graff89bd6f2015-01-02 23:00:21 +0100934 if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT))
935 return -EINVAL;
936
Ying Xue94000172014-09-03 09:22:36 +0800937 params->min_shift = max_t(size_t, params->min_shift,
938 ilog2(HASH_MIN_SIZE));
939
Thomas Graf7e1e7762014-08-02 11:47:44 +0200940 if (params->nelem_hint)
Ying Xue94000172014-09-03 09:22:36 +0800941 size = rounded_hashtable_size(params);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200942
Thomas Graf97defe12015-01-02 23:00:20 +0100943 memset(ht, 0, sizeof(*ht));
944 mutex_init(&ht->mutex);
945 memcpy(&ht->p, params, sizeof(*params));
946
947 if (params->locks_mul)
948 ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
949 else
950 ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
951
Herbert Xu5269b532015-03-14 13:57:22 +1100952 tbl = bucket_table_alloc(ht, size);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200953 if (tbl == NULL)
954 return -ENOMEM;
955
Ying Xue545a1482015-01-07 13:41:57 +0800956 atomic_set(&ht->nelems, 0);
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100957
Thomas Graf7e1e7762014-08-02 11:47:44 +0200958 RCU_INIT_POINTER(ht->tbl, tbl);
959
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100960 INIT_WORK(&ht->run_work, rht_deferred_worker);
Thomas Graf97defe12015-01-02 23:00:20 +0100961
Thomas Graf7e1e7762014-08-02 11:47:44 +0200962 return 0;
963}
964EXPORT_SYMBOL_GPL(rhashtable_init);
965
966/**
967 * rhashtable_destroy - destroy hash table
968 * @ht: the hash table to destroy
969 *
Pablo Neira Ayusoae82ddc2014-09-02 00:26:05 +0200970 * Frees the bucket array. This function is not rcu safe, therefore the caller
971 * has to make sure that no resizing may happen by unpublishing the hashtable
972 * and waiting for the quiescent cycle before releasing the bucket array.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200973 */
Thomas Graf97defe12015-01-02 23:00:20 +0100974void rhashtable_destroy(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200975{
Thomas Graf97defe12015-01-02 23:00:20 +0100976 ht->being_destroyed = true;
977
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100978 cancel_work_sync(&ht->run_work);
Ying Xue57699a42015-01-16 11:13:09 +0800979
Thomas Graf97defe12015-01-02 23:00:20 +0100980 mutex_lock(&ht->mutex);
Thomas Graf97defe12015-01-02 23:00:20 +0100981 bucket_table_free(rht_dereference(ht->tbl, ht));
Thomas Graf97defe12015-01-02 23:00:20 +0100982 mutex_unlock(&ht->mutex);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200983}
984EXPORT_SYMBOL_GPL(rhashtable_destroy);