blob: c4061bbd011373e4b7476e02796da09531002d18 [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
Herbert Xuc2e213c2015-03-18 20:01:16 +110030#define HASH_MIN_SIZE 4U
Thomas Graf97defe12015-01-02 23:00:20 +010031#define BUCKET_LOCKS_PER_CPU 128UL
32
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;
165
Thomas Graf97defe12015-01-02 23:00:20 +0100166 if (alloc_bucket_locks(ht, tbl) < 0) {
167 bucket_table_free(tbl);
168 return NULL;
169 }
Thomas Graf7e1e7762014-08-02 11:47:44 +0200170
Herbert Xueddee5ba2015-03-14 13:57:20 +1100171 INIT_LIST_HEAD(&tbl->walkers);
172
Herbert Xu5269b532015-03-14 13:57:22 +1100173 get_random_bytes(&tbl->hash_rnd, sizeof(tbl->hash_rnd));
174
Thomas Graff89bd6f2015-01-02 23:00:21 +0100175 for (i = 0; i < nbuckets; i++)
176 INIT_RHT_NULLS_HEAD(tbl->buckets[i], ht, i);
177
Thomas Graf97defe12015-01-02 23:00:20 +0100178 return tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200179}
180
181/**
182 * rht_grow_above_75 - returns true if nelems > 0.75 * table-size
183 * @ht: hash table
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100184 * @tbl: current table
Thomas Graf7e1e7762014-08-02 11:47:44 +0200185 */
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100186static bool rht_grow_above_75(const struct rhashtable *ht,
187 const struct bucket_table *tbl)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200188{
189 /* Expand table when exceeding 75% load */
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100190 return atomic_read(&ht->nelems) > (tbl->size / 4 * 3) &&
Herbert Xuc2e213c2015-03-18 20:01:16 +1100191 (!ht->p.max_shift || tbl->size < (1 << ht->p.max_shift)) &&
192 (!ht->p.max_size || tbl->size < ht->p.max_size);
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) &&
Herbert Xuc2e213c2015-03-18 20:01:16 +1100205 tbl->size > (1 << ht->p.min_shift) &&
206 tbl->size > ht->p.min_size;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200207}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200208
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100209static int rhashtable_rehash_one(struct rhashtable *ht, unsigned old_hash)
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100210{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100211 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
Herbert Xuc4db8842015-03-14 13:57:25 +1100212 struct bucket_table *new_tbl =
213 rht_dereference(old_tbl->future_tbl, ht) ?: old_tbl;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100214 struct rhash_head __rcu **pprev = &old_tbl->buckets[old_hash];
215 int err = -ENOENT;
216 struct rhash_head *head, *next, *entry;
217 spinlock_t *new_bucket_lock;
218 unsigned new_hash;
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100219
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100220 rht_for_each(entry, old_tbl, old_hash) {
221 err = 0;
222 next = rht_dereference_bucket(entry->next, old_tbl, old_hash);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100223
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100224 if (rht_is_a_nulls(next))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200225 break;
Thomas Graf97defe12015-01-02 23:00:20 +0100226
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100227 pprev = &entry->next;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200228 }
229
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100230 if (err)
231 goto out;
Thomas Graf97defe12015-01-02 23:00:20 +0100232
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100233 new_hash = head_hashfn(ht, new_tbl, entry);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100234
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100235 new_bucket_lock = bucket_lock(new_tbl, new_hash);
236
Herbert Xu8f2484b2015-03-14 13:57:21 +1100237 spin_lock_nested(new_bucket_lock, SINGLE_DEPTH_NESTING);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100238 head = rht_dereference_bucket(new_tbl->buckets[new_hash],
239 new_tbl, new_hash);
240
241 if (rht_is_a_nulls(head))
242 INIT_RHT_NULLS_HEAD(entry->next, ht, new_hash);
243 else
244 RCU_INIT_POINTER(entry->next, head);
245
246 rcu_assign_pointer(new_tbl->buckets[new_hash], entry);
247 spin_unlock(new_bucket_lock);
248
249 rcu_assign_pointer(*pprev, next);
250
251out:
252 return err;
Thomas Graf97defe12015-01-02 23:00:20 +0100253}
254
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100255static void rhashtable_rehash_chain(struct rhashtable *ht, unsigned old_hash)
Thomas Graf97defe12015-01-02 23:00:20 +0100256{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100257 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
258 spinlock_t *old_bucket_lock;
Thomas Graf7cd10db2015-02-05 02:03:35 +0100259
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100260 old_bucket_lock = bucket_lock(old_tbl, old_hash);
261
262 spin_lock_bh(old_bucket_lock);
263 while (!rhashtable_rehash_one(ht, old_hash))
264 ;
Herbert Xu63d512d2015-03-14 13:57:24 +1100265 old_tbl->rehash++;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100266 spin_unlock_bh(old_bucket_lock);
267}
268
269static void rhashtable_rehash(struct rhashtable *ht,
270 struct bucket_table *new_tbl)
271{
272 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100273 struct rhashtable_walker *walker;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100274 unsigned old_hash;
275
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100276 /* Make insertions go into the new, empty table right away. Deletions
277 * and lookups will be attempted in both tables until we synchronize.
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100278 */
Herbert Xuc4db8842015-03-14 13:57:25 +1100279 rcu_assign_pointer(old_tbl->future_tbl, new_tbl);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100280
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;
Thomas Graf617011e2015-03-16 10:42:26 +0100388 spinlock_t *old_lock;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100389 bool success = true;
390
391 rcu_read_lock();
392
393 old_tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xueca84932015-03-12 14:49:39 +1100394 hash = head_hashfn(ht, old_tbl, obj);
Thomas Graf617011e2015-03-16 10:42:26 +0100395 old_lock = bucket_lock(old_tbl, hash);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100396
Thomas Graf617011e2015-03-16 10:42:26 +0100397 spin_lock_bh(old_lock);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100398
399 /* Because we have already taken the bucket lock in old_tbl,
400 * if we find that future_tbl is not yet visible then that
401 * guarantees all other insertions of the same entry will
402 * also grab the bucket lock in old_tbl because until the
403 * rehash completes ht->tbl won't be changed.
404 */
Herbert Xuc4db8842015-03-14 13:57:25 +1100405 tbl = rht_dereference_rcu(old_tbl->future_tbl, ht) ?: old_tbl;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100406 if (tbl != old_tbl) {
Herbert Xueca84932015-03-12 14:49:39 +1100407 hash = head_hashfn(ht, tbl, obj);
Herbert Xu8f2484b2015-03-14 13:57:21 +1100408 spin_lock_nested(bucket_lock(tbl, hash), SINGLE_DEPTH_NESTING);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100409 }
410
411 if (compare &&
412 rhashtable_lookup_compare(ht, rht_obj(ht, obj) + ht->p.key_offset,
413 compare, arg)) {
414 success = false;
415 goto exit;
416 }
417
418 no_resize_running = tbl == old_tbl;
Thomas Graf020219a2015-02-06 16:08:43 +0000419
Thomas Graf020219a2015-02-06 16:08:43 +0000420 head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash);
Ying Xuedb304852015-01-07 13:41:54 +0800421
422 if (rht_is_a_nulls(head))
423 INIT_RHT_NULLS_HEAD(obj->next, ht, hash);
424 else
425 RCU_INIT_POINTER(obj->next, head);
426
427 rcu_assign_pointer(tbl->buckets[hash], obj);
428
429 atomic_inc(&ht->nelems);
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100430 if (no_resize_running && rht_grow_above_75(ht, tbl))
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100431 schedule_work(&ht->run_work);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100432
433exit:
Thomas Graf617011e2015-03-16 10:42:26 +0100434 if (tbl != old_tbl)
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100435 spin_unlock(bucket_lock(tbl, hash));
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100436
Thomas Graf617011e2015-03-16 10:42:26 +0100437 spin_unlock_bh(old_lock);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100438
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 Xu565e8642015-03-15 21:12:05 +1100514 struct bucket_table *tbl;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100515 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
Herbert Xu565e8642015-03-15 21:12:05 +1100519 tbl = rht_dereference_rcu(ht->tbl, ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200520
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100521 /* Because we have already taken (and released) the bucket
522 * lock in old_tbl, if we find that future_tbl is not yet
523 * visible then that guarantees the entry to still be in
Herbert Xu565e8642015-03-15 21:12:05 +1100524 * the old tbl if it exists.
Thomas Graffe6a0432015-01-21 11:54:01 +0000525 */
Herbert Xu565e8642015-03-15 21:12:05 +1100526 while (!(ret = __rhashtable_remove(ht, tbl, obj)) &&
527 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
528 ;
Thomas Graffe6a0432015-01-21 11:54:01 +0000529
530 if (ret) {
531 atomic_dec(&ht->nelems);
Herbert Xu565e8642015-03-15 21:12:05 +1100532 if (rht_shrink_below_30(ht, tbl))
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100533 schedule_work(&ht->run_work);
Thomas Graffe6a0432015-01-21 11:54:01 +0000534 }
535
Thomas Graf97defe12015-01-02 23:00:20 +0100536 rcu_read_unlock();
537
Thomas Graffe6a0432015-01-21 11:54:01 +0000538 return ret;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200539}
540EXPORT_SYMBOL_GPL(rhashtable_remove);
541
Ying Xueefb975a62015-01-07 13:41:52 +0800542struct rhashtable_compare_arg {
543 struct rhashtable *ht;
544 const void *key;
545};
546
547static bool rhashtable_compare(void *ptr, void *arg)
548{
549 struct rhashtable_compare_arg *x = arg;
550 struct rhashtable *ht = x->ht;
551
552 return !memcmp(ptr + ht->p.key_offset, x->key, ht->p.key_len);
553}
554
Thomas Graf7e1e7762014-08-02 11:47:44 +0200555/**
556 * rhashtable_lookup - lookup key in hash table
557 * @ht: hash table
558 * @key: pointer to key
559 *
560 * Computes the hash value for the key and traverses the bucket chain looking
561 * for a entry with an identical key. The first matching entry is returned.
562 *
563 * This lookup function may only be used for fixed key hash table (key_len
Ying Xuedb304852015-01-07 13:41:54 +0800564 * parameter set). It will BUG() if used inappropriately.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200565 *
Thomas Graf97defe12015-01-02 23:00:20 +0100566 * Lookups may occur in parallel with hashtable mutations and resizing.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200567 */
Thomas Graf97defe12015-01-02 23:00:20 +0100568void *rhashtable_lookup(struct rhashtable *ht, const void *key)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200569{
Ying Xueefb975a62015-01-07 13:41:52 +0800570 struct rhashtable_compare_arg arg = {
571 .ht = ht,
572 .key = key,
573 };
Thomas Graf7e1e7762014-08-02 11:47:44 +0200574
575 BUG_ON(!ht->p.key_len);
576
Ying Xueefb975a62015-01-07 13:41:52 +0800577 return rhashtable_lookup_compare(ht, key, &rhashtable_compare, &arg);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200578}
579EXPORT_SYMBOL_GPL(rhashtable_lookup);
580
581/**
582 * rhashtable_lookup_compare - search hash table with compare function
583 * @ht: hash table
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100584 * @key: the pointer to the key
Thomas Graf7e1e7762014-08-02 11:47:44 +0200585 * @compare: compare function, must return true on match
586 * @arg: argument passed on to compare function
587 *
588 * Traverses the bucket chain behind the provided hash value and calls the
589 * specified compare function for each entry.
590 *
Thomas Graf97defe12015-01-02 23:00:20 +0100591 * Lookups may occur in parallel with hashtable mutations and resizing.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200592 *
593 * Returns the first entry on which the compare function returned true.
594 */
Thomas Graf97defe12015-01-02 23:00:20 +0100595void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200596 bool (*compare)(void *, void *), void *arg)
597{
Herbert Xuc4db8842015-03-14 13:57:25 +1100598 const struct bucket_table *tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200599 struct rhash_head *he;
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100600 u32 hash;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200601
Thomas Graf97defe12015-01-02 23:00:20 +0100602 rcu_read_lock();
603
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100604 tbl = rht_dereference_rcu(ht->tbl, ht);
Thomas Graf97defe12015-01-02 23:00:20 +0100605restart:
Herbert Xu39361942015-03-13 12:54:10 +1100606 hash = key_hashfn(ht, tbl, key);
Herbert Xu8d2b1872015-03-12 14:49:38 +1100607 rht_for_each_rcu(he, tbl, hash) {
Thomas Graf7e1e7762014-08-02 11:47:44 +0200608 if (!compare(rht_obj(ht, he), arg))
609 continue;
Thomas Graf97defe12015-01-02 23:00:20 +0100610 rcu_read_unlock();
Thomas Grafa4b18cd2015-01-02 23:00:15 +0100611 return rht_obj(ht, he);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200612 }
613
Herbert Xu9497df82015-03-12 22:07:49 +1100614 /* Ensure we see any new tables. */
615 smp_rmb();
616
Herbert Xuc4db8842015-03-14 13:57:25 +1100617 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
618 if (unlikely(tbl))
Thomas Graf97defe12015-01-02 23:00:20 +0100619 goto restart;
Thomas Graf97defe12015-01-02 23:00:20 +0100620 rcu_read_unlock();
621
Thomas Graf7e1e7762014-08-02 11:47:44 +0200622 return NULL;
623}
624EXPORT_SYMBOL_GPL(rhashtable_lookup_compare);
625
Ying Xuedb304852015-01-07 13:41:54 +0800626/**
627 * rhashtable_lookup_insert - lookup and insert object into hash table
628 * @ht: hash table
629 * @obj: pointer to hash head inside object
630 *
631 * Locks down the bucket chain in both the old and new table if a resize
632 * is in progress to ensure that writers can't remove from the old table
633 * and can't insert to the new table during the atomic operation of search
634 * and insertion. Searches for duplicates in both the old and new table if
635 * a resize is in progress.
636 *
637 * This lookup function may only be used for fixed key hash table (key_len
638 * parameter set). It will BUG() if used inappropriately.
639 *
640 * It is safe to call this function from atomic context.
641 *
642 * Will trigger an automatic deferred table resizing if the size grows
643 * beyond the watermark indicated by grow_decision() which can be passed
644 * to rhashtable_init().
645 */
646bool rhashtable_lookup_insert(struct rhashtable *ht, struct rhash_head *obj)
647{
Ying Xue7a868d12015-01-12 14:52:22 +0800648 struct rhashtable_compare_arg arg = {
649 .ht = ht,
650 .key = rht_obj(ht, obj) + ht->p.key_offset,
651 };
652
653 BUG_ON(!ht->p.key_len);
654
655 return rhashtable_lookup_compare_insert(ht, obj, &rhashtable_compare,
656 &arg);
657}
658EXPORT_SYMBOL_GPL(rhashtable_lookup_insert);
659
660/**
661 * rhashtable_lookup_compare_insert - search and insert object to hash table
662 * with compare function
663 * @ht: hash table
664 * @obj: pointer to hash head inside object
665 * @compare: compare function, must return true on match
666 * @arg: argument passed on to compare function
667 *
668 * Locks down the bucket chain in both the old and new table if a resize
669 * is in progress to ensure that writers can't remove from the old table
670 * and can't insert to the new table during the atomic operation of search
671 * and insertion. Searches for duplicates in both the old and new table if
672 * a resize is in progress.
673 *
674 * Lookups may occur in parallel with hashtable mutations and resizing.
675 *
676 * Will trigger an automatic deferred table resizing if the size grows
677 * beyond the watermark indicated by grow_decision() which can be passed
678 * to rhashtable_init().
679 */
680bool rhashtable_lookup_compare_insert(struct rhashtable *ht,
681 struct rhash_head *obj,
682 bool (*compare)(void *, void *),
683 void *arg)
684{
Ying Xuedb304852015-01-07 13:41:54 +0800685 BUG_ON(!ht->p.key_len);
686
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100687 return __rhashtable_insert(ht, obj, compare, arg);
Ying Xuedb304852015-01-07 13:41:54 +0800688}
Ying Xue7a868d12015-01-12 14:52:22 +0800689EXPORT_SYMBOL_GPL(rhashtable_lookup_compare_insert);
Ying Xuedb304852015-01-07 13:41:54 +0800690
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100691/**
692 * rhashtable_walk_init - Initialise an iterator
693 * @ht: Table to walk over
694 * @iter: Hash table Iterator
695 *
696 * This function prepares a hash table walk.
697 *
698 * Note that if you restart a walk after rhashtable_walk_stop you
699 * may see the same object twice. Also, you may miss objects if
700 * there are removals in between rhashtable_walk_stop and the next
701 * call to rhashtable_walk_start.
702 *
703 * For a completely stable walk you should construct your own data
704 * structure outside the hash table.
705 *
706 * This function may sleep so you must not call it from interrupt
707 * context or with spin locks held.
708 *
709 * You must call rhashtable_walk_exit if this function returns
710 * successfully.
711 */
712int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter)
713{
714 iter->ht = ht;
715 iter->p = NULL;
716 iter->slot = 0;
717 iter->skip = 0;
718
719 iter->walker = kmalloc(sizeof(*iter->walker), GFP_KERNEL);
720 if (!iter->walker)
721 return -ENOMEM;
722
723 mutex_lock(&ht->mutex);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100724 iter->walker->tbl = rht_dereference(ht->tbl, ht);
725 list_add(&iter->walker->list, &iter->walker->tbl->walkers);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100726 mutex_unlock(&ht->mutex);
727
728 return 0;
729}
730EXPORT_SYMBOL_GPL(rhashtable_walk_init);
731
732/**
733 * rhashtable_walk_exit - Free an iterator
734 * @iter: Hash table Iterator
735 *
736 * This function frees resources allocated by rhashtable_walk_init.
737 */
738void rhashtable_walk_exit(struct rhashtable_iter *iter)
739{
740 mutex_lock(&iter->ht->mutex);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100741 if (iter->walker->tbl)
742 list_del(&iter->walker->list);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100743 mutex_unlock(&iter->ht->mutex);
744 kfree(iter->walker);
745}
746EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
747
748/**
749 * rhashtable_walk_start - Start a hash table walk
750 * @iter: Hash table iterator
751 *
752 * Start a hash table walk. Note that we take the RCU lock in all
753 * cases including when we return an error. So you must always call
754 * rhashtable_walk_stop to clean up.
755 *
756 * Returns zero if successful.
757 *
758 * Returns -EAGAIN if resize event occured. Note that the iterator
759 * will rewind back to the beginning and you may use it immediately
760 * by calling rhashtable_walk_next.
761 */
762int rhashtable_walk_start(struct rhashtable_iter *iter)
Thomas Grafdb4374f2015-03-16 10:42:27 +0100763 __acquires(RCU)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100764{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100765 struct rhashtable *ht = iter->ht;
766
767 mutex_lock(&ht->mutex);
768
769 if (iter->walker->tbl)
770 list_del(&iter->walker->list);
771
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100772 rcu_read_lock();
773
Herbert Xueddee5ba2015-03-14 13:57:20 +1100774 mutex_unlock(&ht->mutex);
775
776 if (!iter->walker->tbl) {
777 iter->walker->tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100778 return -EAGAIN;
779 }
780
781 return 0;
782}
783EXPORT_SYMBOL_GPL(rhashtable_walk_start);
784
785/**
786 * rhashtable_walk_next - Return the next object and advance the iterator
787 * @iter: Hash table iterator
788 *
789 * Note that you must call rhashtable_walk_stop when you are finished
790 * with the walk.
791 *
792 * Returns the next object or NULL when the end of the table is reached.
793 *
794 * Returns -EAGAIN if resize event occured. Note that the iterator
795 * will rewind back to the beginning and you may continue to use it.
796 */
797void *rhashtable_walk_next(struct rhashtable_iter *iter)
798{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100799 struct bucket_table *tbl = iter->walker->tbl;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100800 struct rhashtable *ht = iter->ht;
801 struct rhash_head *p = iter->p;
802 void *obj = NULL;
803
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100804 if (p) {
805 p = rht_dereference_bucket_rcu(p->next, tbl, iter->slot);
806 goto next;
807 }
808
809 for (; iter->slot < tbl->size; iter->slot++) {
810 int skip = iter->skip;
811
812 rht_for_each_rcu(p, tbl, iter->slot) {
813 if (!skip)
814 break;
815 skip--;
816 }
817
818next:
819 if (!rht_is_a_nulls(p)) {
820 iter->skip++;
821 iter->p = p;
822 obj = rht_obj(ht, p);
823 goto out;
824 }
825
826 iter->skip = 0;
827 }
828
Herbert Xuc4db8842015-03-14 13:57:25 +1100829 iter->walker->tbl = rht_dereference_rcu(tbl->future_tbl, ht);
830 if (iter->walker->tbl) {
Herbert Xueddee5ba2015-03-14 13:57:20 +1100831 iter->slot = 0;
832 iter->skip = 0;
833 return ERR_PTR(-EAGAIN);
834 }
835
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100836 iter->p = NULL;
837
838out:
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100839
840 return obj;
841}
842EXPORT_SYMBOL_GPL(rhashtable_walk_next);
843
844/**
845 * rhashtable_walk_stop - Finish a hash table walk
846 * @iter: Hash table iterator
847 *
848 * Finish a hash table walk.
849 */
850void rhashtable_walk_stop(struct rhashtable_iter *iter)
Thomas Grafdb4374f2015-03-16 10:42:27 +0100851 __releases(RCU)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100852{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100853 struct rhashtable *ht;
854 struct bucket_table *tbl = iter->walker->tbl;
855
Herbert Xueddee5ba2015-03-14 13:57:20 +1100856 if (!tbl)
Herbert Xu963ecbd2015-03-15 21:12:04 +1100857 goto out;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100858
859 ht = iter->ht;
860
861 mutex_lock(&ht->mutex);
Herbert Xuc4db8842015-03-14 13:57:25 +1100862 if (tbl->rehash < tbl->size)
Herbert Xueddee5ba2015-03-14 13:57:20 +1100863 list_add(&iter->walker->list, &tbl->walkers);
864 else
865 iter->walker->tbl = NULL;
866 mutex_unlock(&ht->mutex);
867
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100868 iter->p = NULL;
Herbert Xu963ecbd2015-03-15 21:12:04 +1100869
870out:
871 rcu_read_unlock();
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100872}
873EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
874
Ying Xue94000172014-09-03 09:22:36 +0800875static size_t rounded_hashtable_size(struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200876{
Ying Xue94000172014-09-03 09:22:36 +0800877 return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
Herbert Xuc2e213c2015-03-18 20:01:16 +1100878 max(1UL << params->min_shift,
879 (unsigned long)params->min_size));
Thomas Graf7e1e7762014-08-02 11:47:44 +0200880}
881
882/**
883 * rhashtable_init - initialize a new hash table
884 * @ht: hash table to be initialized
885 * @params: configuration parameters
886 *
887 * Initializes a new hash table based on the provided configuration
888 * parameters. A table can be configured either with a variable or
889 * fixed length key:
890 *
891 * Configuration Example 1: Fixed length keys
892 * struct test_obj {
893 * int key;
894 * void * my_member;
895 * struct rhash_head node;
896 * };
897 *
898 * struct rhashtable_params params = {
899 * .head_offset = offsetof(struct test_obj, node),
900 * .key_offset = offsetof(struct test_obj, key),
901 * .key_len = sizeof(int),
Daniel Borkmann87545892014-12-10 16:33:11 +0100902 * .hashfn = jhash,
Thomas Graff89bd6f2015-01-02 23:00:21 +0100903 * .nulls_base = (1U << RHT_BASE_SHIFT),
Thomas Graf7e1e7762014-08-02 11:47:44 +0200904 * };
905 *
906 * Configuration Example 2: Variable length keys
907 * struct test_obj {
908 * [...]
909 * struct rhash_head node;
910 * };
911 *
912 * u32 my_hash_fn(const void *data, u32 seed)
913 * {
914 * struct test_obj *obj = data;
915 *
916 * return [... hash ...];
917 * }
918 *
919 * struct rhashtable_params params = {
920 * .head_offset = offsetof(struct test_obj, node),
Daniel Borkmann87545892014-12-10 16:33:11 +0100921 * .hashfn = jhash,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200922 * .obj_hashfn = my_hash_fn,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200923 * };
924 */
925int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params)
926{
927 struct bucket_table *tbl;
928 size_t size;
929
930 size = HASH_DEFAULT_SIZE;
931
932 if ((params->key_len && !params->hashfn) ||
933 (!params->key_len && !params->obj_hashfn))
934 return -EINVAL;
935
Thomas Graff89bd6f2015-01-02 23:00:21 +0100936 if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT))
937 return -EINVAL;
938
Ying Xue94000172014-09-03 09:22:36 +0800939 params->min_shift = max_t(size_t, params->min_shift,
940 ilog2(HASH_MIN_SIZE));
Herbert Xuc2e213c2015-03-18 20:01:16 +1100941 params->min_size = max(params->min_size, HASH_MIN_SIZE);
Ying Xue94000172014-09-03 09:22:36 +0800942
Thomas Graf7e1e7762014-08-02 11:47:44 +0200943 if (params->nelem_hint)
Ying Xue94000172014-09-03 09:22:36 +0800944 size = rounded_hashtable_size(params);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200945
Thomas Graf97defe12015-01-02 23:00:20 +0100946 memset(ht, 0, sizeof(*ht));
947 mutex_init(&ht->mutex);
948 memcpy(&ht->p, params, sizeof(*params));
949
950 if (params->locks_mul)
951 ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
952 else
953 ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
954
Herbert Xu5269b532015-03-14 13:57:22 +1100955 tbl = bucket_table_alloc(ht, size);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200956 if (tbl == NULL)
957 return -ENOMEM;
958
Ying Xue545a1482015-01-07 13:41:57 +0800959 atomic_set(&ht->nelems, 0);
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100960
Thomas Graf7e1e7762014-08-02 11:47:44 +0200961 RCU_INIT_POINTER(ht->tbl, tbl);
962
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100963 INIT_WORK(&ht->run_work, rht_deferred_worker);
Thomas Graf97defe12015-01-02 23:00:20 +0100964
Thomas Graf7e1e7762014-08-02 11:47:44 +0200965 return 0;
966}
967EXPORT_SYMBOL_GPL(rhashtable_init);
968
969/**
970 * rhashtable_destroy - destroy hash table
971 * @ht: the hash table to destroy
972 *
Pablo Neira Ayusoae82ddc2014-09-02 00:26:05 +0200973 * Frees the bucket array. This function is not rcu safe, therefore the caller
974 * has to make sure that no resizing may happen by unpublishing the hashtable
975 * and waiting for the quiescent cycle before releasing the bucket array.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200976 */
Thomas Graf97defe12015-01-02 23:00:20 +0100977void rhashtable_destroy(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200978{
Thomas Graf97defe12015-01-02 23:00:20 +0100979 ht->being_destroyed = true;
980
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100981 cancel_work_sync(&ht->run_work);
Ying Xue57699a42015-01-16 11:13:09 +0800982
Thomas Graf97defe12015-01-02 23:00:20 +0100983 mutex_lock(&ht->mutex);
Thomas Graf97defe12015-01-02 23:00:20 +0100984 bucket_table_free(rht_dereference(ht->tbl, ht));
Thomas Graf97defe12015-01-02 23:00:20 +0100985 mutex_unlock(&ht->mutex);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200986}
987EXPORT_SYMBOL_GPL(rhashtable_destroy);