blob: f7c76079f8f1eb227ddc84c50ad228352dcfeb98 [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 +010036enum {
37 RHT_LOCK_NORMAL,
38 RHT_LOCK_NESTED,
Thomas Graf97defe12015-01-02 23:00:20 +010039};
40
41/* The bucket lock is selected based on the hash and protects mutations
42 * on a group of hash buckets.
43 *
Thomas Grafa5ec68e2015-02-05 02:03:32 +010044 * A maximum of tbl->size/2 bucket locks is allocated. This ensures that
45 * a single lock always covers both buckets which may both contains
46 * entries which link to the same bucket of the old table during resizing.
47 * This allows to simplify the locking as locking the bucket in both
48 * tables during resize always guarantee protection.
49 *
Thomas Graf97defe12015-01-02 23:00:20 +010050 * IMPORTANT: When holding the bucket lock of both the old and new table
51 * during expansions and shrinking, the old bucket lock must always be
52 * acquired first.
53 */
54static spinlock_t *bucket_lock(const struct bucket_table *tbl, u32 hash)
55{
56 return &tbl->locks[hash & tbl->locks_mask];
57}
Thomas Graf7e1e7762014-08-02 11:47:44 +020058
Thomas Grafc91eee52014-08-13 16:38:30 +020059static void *rht_obj(const struct rhashtable *ht, const struct rhash_head *he)
Thomas Graf7e1e7762014-08-02 11:47:44 +020060{
61 return (void *) he - ht->p.head_offset;
62}
Thomas Graf7e1e7762014-08-02 11:47:44 +020063
Thomas Graf8d24c0b2015-01-02 23:00:14 +010064static u32 rht_bucket_index(const struct bucket_table *tbl, u32 hash)
Thomas Graf7e1e7762014-08-02 11:47:44 +020065{
Herbert Xuec9f71c2015-03-12 14:49:41 +110066 return (hash >> HASH_RESERVED_SPACE) & (tbl->size - 1);
Thomas Graf8d24c0b2015-01-02 23:00:14 +010067}
68
Herbert Xuaa34a6cb02015-03-11 09:43:48 +110069static u32 key_hashfn(struct rhashtable *ht, const struct bucket_table *tbl,
Herbert Xucffaa9c2015-03-12 14:49:40 +110070 const void *key)
Thomas Graf7e1e7762014-08-02 11:47:44 +020071{
Herbert Xucffaa9c2015-03-12 14:49:40 +110072 return rht_bucket_index(tbl, ht->p.hashfn(key, ht->p.key_len,
Herbert Xuec9f71c2015-03-12 14:49:41 +110073 tbl->hash_rnd));
Thomas Graf7e1e7762014-08-02 11:47:44 +020074}
Thomas Graf7e1e7762014-08-02 11:47:44 +020075
Herbert Xu988dfbd2015-03-10 09:27:55 +110076static u32 head_hashfn(struct rhashtable *ht,
Thomas Graf8d24c0b2015-01-02 23:00:14 +010077 const struct bucket_table *tbl,
78 const struct rhash_head *he)
Thomas Graf7e1e7762014-08-02 11:47:44 +020079{
Herbert Xuec9f71c2015-03-12 14:49:41 +110080 const char *ptr = rht_obj(ht, he);
81
82 return likely(ht->p.key_len) ?
83 key_hashfn(ht, tbl, ptr + ht->p.key_offset) :
84 rht_bucket_index(tbl, ht->p.obj_hashfn(ptr, tbl->hash_rnd));
Thomas Graf7e1e7762014-08-02 11:47:44 +020085}
86
Thomas Grafa03eaec2015-02-05 02:03:34 +010087#ifdef CONFIG_PROVE_LOCKING
Thomas Grafa03eaec2015-02-05 02:03:34 +010088#define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
Thomas Grafa03eaec2015-02-05 02:03:34 +010089
90int lockdep_rht_mutex_is_held(struct rhashtable *ht)
91{
92 return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1;
93}
94EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held);
95
96int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash)
97{
98 spinlock_t *lock = bucket_lock(tbl, hash);
99
100 return (debug_locks) ? lockdep_is_held(lock) : 1;
101}
102EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held);
103#else
104#define ASSERT_RHT_MUTEX(HT)
Thomas Grafa03eaec2015-02-05 02:03:34 +0100105#endif
106
107
Thomas Graf97defe12015-01-02 23:00:20 +0100108static int alloc_bucket_locks(struct rhashtable *ht, struct bucket_table *tbl)
109{
110 unsigned int i, size;
111#if defined(CONFIG_PROVE_LOCKING)
112 unsigned int nr_pcpus = 2;
113#else
114 unsigned int nr_pcpus = num_possible_cpus();
115#endif
116
117 nr_pcpus = min_t(unsigned int, nr_pcpus, 32UL);
118 size = roundup_pow_of_two(nr_pcpus * ht->p.locks_mul);
119
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100120 /* Never allocate more than 0.5 locks per bucket */
121 size = min_t(unsigned int, size, tbl->size >> 1);
Thomas Graf97defe12015-01-02 23:00:20 +0100122
123 if (sizeof(spinlock_t) != 0) {
124#ifdef CONFIG_NUMA
125 if (size * sizeof(spinlock_t) > PAGE_SIZE)
126 tbl->locks = vmalloc(size * sizeof(spinlock_t));
127 else
128#endif
129 tbl->locks = kmalloc_array(size, sizeof(spinlock_t),
130 GFP_KERNEL);
131 if (!tbl->locks)
132 return -ENOMEM;
133 for (i = 0; i < size; i++)
134 spin_lock_init(&tbl->locks[i]);
135 }
136 tbl->locks_mask = size - 1;
137
138 return 0;
139}
140
141static void bucket_table_free(const struct bucket_table *tbl)
142{
143 if (tbl)
144 kvfree(tbl->locks);
145
146 kvfree(tbl);
147}
148
149static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100150 size_t nbuckets, u32 hash_rnd)
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);
166 tbl->hash_rnd = hash_rnd;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200167
Thomas Graf97defe12015-01-02 23:00:20 +0100168 if (alloc_bucket_locks(ht, tbl) < 0) {
169 bucket_table_free(tbl);
170 return NULL;
171 }
Thomas Graf7e1e7762014-08-02 11:47:44 +0200172
Herbert Xueddee5ba2015-03-14 13:57:20 +1100173 INIT_LIST_HEAD(&tbl->walkers);
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) &&
191 (!ht->p.max_shift || tbl->shift < ht->p.max_shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200192}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200193
194/**
195 * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size
196 * @ht: hash table
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100197 * @tbl: current table
Thomas Graf7e1e7762014-08-02 11:47:44 +0200198 */
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100199static bool rht_shrink_below_30(const struct rhashtable *ht,
200 const struct bucket_table *tbl)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200201{
202 /* Shrink table beneath 30% load */
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100203 return atomic_read(&ht->nelems) < (tbl->size * 3 / 10) &&
204 tbl->shift > ht->p.min_shift;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200205}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200206
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100207static int rhashtable_rehash_one(struct rhashtable *ht, unsigned old_hash)
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100208{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100209 struct bucket_table *new_tbl = rht_dereference(ht->future_tbl, ht);
210 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
211 struct rhash_head __rcu **pprev = &old_tbl->buckets[old_hash];
212 int err = -ENOENT;
213 struct rhash_head *head, *next, *entry;
214 spinlock_t *new_bucket_lock;
215 unsigned new_hash;
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100216
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100217 rht_for_each(entry, old_tbl, old_hash) {
218 err = 0;
219 next = rht_dereference_bucket(entry->next, old_tbl, old_hash);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100220
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100221 if (rht_is_a_nulls(next))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200222 break;
Thomas Graf97defe12015-01-02 23:00:20 +0100223
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100224 pprev = &entry->next;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200225 }
226
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100227 if (err)
228 goto out;
Thomas Graf97defe12015-01-02 23:00:20 +0100229
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100230 new_hash = head_hashfn(ht, new_tbl, entry);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100231
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100232 new_bucket_lock = bucket_lock(new_tbl, new_hash);
233
Herbert Xu84ed82b2015-03-12 14:47:13 +1100234 spin_lock_nested(new_bucket_lock, RHT_LOCK_NESTED);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100235 head = rht_dereference_bucket(new_tbl->buckets[new_hash],
236 new_tbl, new_hash);
237
238 if (rht_is_a_nulls(head))
239 INIT_RHT_NULLS_HEAD(entry->next, ht, new_hash);
240 else
241 RCU_INIT_POINTER(entry->next, head);
242
243 rcu_assign_pointer(new_tbl->buckets[new_hash], entry);
244 spin_unlock(new_bucket_lock);
245
246 rcu_assign_pointer(*pprev, next);
247
248out:
249 return err;
Thomas Graf97defe12015-01-02 23:00:20 +0100250}
251
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100252static void rhashtable_rehash_chain(struct rhashtable *ht, unsigned old_hash)
Thomas Graf97defe12015-01-02 23:00:20 +0100253{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100254 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
255 spinlock_t *old_bucket_lock;
Thomas Graf7cd10db2015-02-05 02:03:35 +0100256
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100257 old_bucket_lock = bucket_lock(old_tbl, old_hash);
258
259 spin_lock_bh(old_bucket_lock);
260 while (!rhashtable_rehash_one(ht, old_hash))
261 ;
262 spin_unlock_bh(old_bucket_lock);
263}
264
265static void rhashtable_rehash(struct rhashtable *ht,
266 struct bucket_table *new_tbl)
267{
268 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100269 struct rhashtable_walker *walker;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100270 unsigned old_hash;
271
272 get_random_bytes(&new_tbl->hash_rnd, sizeof(new_tbl->hash_rnd));
273
274 /* Make insertions go into the new, empty table right away. Deletions
275 * and lookups will be attempted in both tables until we synchronize.
276 * The synchronize_rcu() guarantees for the new table to be picked up
277 * so no new additions go into the old table while we relink.
278 */
279 rcu_assign_pointer(ht->future_tbl, new_tbl);
280
Herbert Xu9497df82015-03-12 22:07:49 +1100281 /* Ensure the new table is visible to readers. */
282 smp_wmb();
283
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100284 for (old_hash = 0; old_hash < old_tbl->size; old_hash++)
285 rhashtable_rehash_chain(ht, old_hash);
286
287 /* Publish the new table pointer. */
288 rcu_assign_pointer(ht->tbl, new_tbl);
289
Herbert Xueddee5ba2015-03-14 13:57:20 +1100290 list_for_each_entry(walker, &old_tbl->walkers, list)
291 walker->tbl = NULL;
292
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100293 /* Wait for readers. All new readers will see the new
294 * table, and thus no references to the old table will
295 * remain.
296 */
297 synchronize_rcu();
298
299 bucket_table_free(old_tbl);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200300}
301
302/**
303 * rhashtable_expand - Expand hash table while allowing concurrent lookups
304 * @ht: the hash table to expand
Thomas Graf7e1e7762014-08-02 11:47:44 +0200305 *
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100306 * A secondary bucket array is allocated and the hash entries are migrated.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200307 *
308 * This function may only be called in a context where it is safe to call
309 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
310 *
Thomas Graf97defe12015-01-02 23:00:20 +0100311 * The caller must ensure that no concurrent resizing occurs by holding
312 * ht->mutex.
313 *
314 * It is valid to have concurrent insertions and deletions protected by per
315 * bucket locks or concurrent RCU protected lookups and traversals.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200316 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100317int rhashtable_expand(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200318{
319 struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200320
321 ASSERT_RHT_MUTEX(ht);
322
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100323 new_tbl = bucket_table_alloc(ht, old_tbl->size * 2, old_tbl->hash_rnd);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200324 if (new_tbl == NULL)
325 return -ENOMEM;
326
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100327 rhashtable_rehash(ht, new_tbl);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200328 return 0;
329}
330EXPORT_SYMBOL_GPL(rhashtable_expand);
331
332/**
333 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
334 * @ht: the hash table to shrink
Thomas Graf7e1e7762014-08-02 11:47:44 +0200335 *
336 * This function may only be called in a context where it is safe to call
337 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
338 *
Thomas Graf97defe12015-01-02 23:00:20 +0100339 * The caller must ensure that no concurrent resizing occurs by holding
340 * ht->mutex.
341 *
Thomas Graf7e1e7762014-08-02 11:47:44 +0200342 * The caller must ensure that no concurrent table mutations take place.
343 * It is however valid to have concurrent lookups if they are RCU protected.
Thomas Graf97defe12015-01-02 23:00:20 +0100344 *
345 * It is valid to have concurrent insertions and deletions protected by per
346 * bucket locks or concurrent RCU protected lookups and traversals.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200347 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100348int rhashtable_shrink(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200349{
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100350 struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200351
352 ASSERT_RHT_MUTEX(ht);
353
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100354 new_tbl = bucket_table_alloc(ht, old_tbl->size / 2, old_tbl->hash_rnd);
Thomas Graf97defe12015-01-02 23:00:20 +0100355 if (new_tbl == NULL)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200356 return -ENOMEM;
357
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100358 rhashtable_rehash(ht, new_tbl);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200359 return 0;
360}
361EXPORT_SYMBOL_GPL(rhashtable_shrink);
362
Thomas Graf97defe12015-01-02 23:00:20 +0100363static void rht_deferred_worker(struct work_struct *work)
364{
365 struct rhashtable *ht;
366 struct bucket_table *tbl;
367
Ying Xue57699a42015-01-16 11:13:09 +0800368 ht = container_of(work, struct rhashtable, run_work);
Thomas Graf97defe12015-01-02 23:00:20 +0100369 mutex_lock(&ht->mutex);
Herbert Xu28134a52015-02-04 07:33:22 +1100370 if (ht->being_destroyed)
371 goto unlock;
372
Thomas Graf97defe12015-01-02 23:00:20 +0100373 tbl = rht_dereference(ht->tbl, ht);
374
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100375 if (rht_grow_above_75(ht, tbl))
Thomas Graf97defe12015-01-02 23:00:20 +0100376 rhashtable_expand(ht);
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100377 else if (rht_shrink_below_30(ht, tbl))
Thomas Graf97defe12015-01-02 23:00:20 +0100378 rhashtable_shrink(ht);
Herbert Xu28134a52015-02-04 07:33:22 +1100379unlock:
Thomas Graf97defe12015-01-02 23:00:20 +0100380 mutex_unlock(&ht->mutex);
381}
382
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100383static bool __rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj,
384 bool (*compare)(void *, void *), void *arg)
Ying Xuedb304852015-01-07 13:41:54 +0800385{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100386 struct bucket_table *tbl, *old_tbl;
Thomas Graf020219a2015-02-06 16:08:43 +0000387 struct rhash_head *head;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100388 bool no_resize_running;
389 unsigned hash;
390 bool success = true;
391
392 rcu_read_lock();
393
394 old_tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xueca84932015-03-12 14:49:39 +1100395 hash = head_hashfn(ht, old_tbl, obj);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100396
397 spin_lock_bh(bucket_lock(old_tbl, hash));
398
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 */
405 tbl = rht_dereference_rcu(ht->future_tbl, ht);
406 if (tbl != old_tbl) {
Herbert Xueca84932015-03-12 14:49:39 +1100407 hash = head_hashfn(ht, tbl, obj);
Herbert Xu84ed82b2015-03-12 14:47:13 +1100408 spin_lock_nested(bucket_lock(tbl, hash), RHT_LOCK_NESTED);
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:
434 if (tbl != old_tbl) {
Herbert Xueca84932015-03-12 14:49:39 +1100435 hash = head_hashfn(ht, tbl, obj);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100436 spin_unlock(bucket_lock(tbl, hash));
437 }
438
Herbert Xueca84932015-03-12 14:49:39 +1100439 hash = head_hashfn(ht, old_tbl, obj);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100440 spin_unlock_bh(bucket_lock(old_tbl, hash));
441
442 rcu_read_unlock();
443
444 return success;
Ying Xuedb304852015-01-07 13:41:54 +0800445}
446
Thomas Graf7e1e7762014-08-02 11:47:44 +0200447/**
Ying Xuedb304852015-01-07 13:41:54 +0800448 * rhashtable_insert - insert object into hash table
Thomas Graf7e1e7762014-08-02 11:47:44 +0200449 * @ht: hash table
450 * @obj: pointer to hash head inside object
Thomas Graf7e1e7762014-08-02 11:47:44 +0200451 *
Thomas Graf97defe12015-01-02 23:00:20 +0100452 * Will take a per bucket spinlock to protect against mutual mutations
453 * on the same bucket. Multiple insertions may occur in parallel unless
454 * they map to the same bucket lock.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200455 *
Thomas Graf97defe12015-01-02 23:00:20 +0100456 * It is safe to call this function from atomic context.
457 *
458 * Will trigger an automatic deferred table resizing if the size grows
459 * beyond the watermark indicated by grow_decision() which can be passed
460 * to rhashtable_init().
Thomas Graf7e1e7762014-08-02 11:47:44 +0200461 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100462void rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200463{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100464 __rhashtable_insert(ht, obj, NULL, NULL);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200465}
466EXPORT_SYMBOL_GPL(rhashtable_insert);
467
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100468static bool __rhashtable_remove(struct rhashtable *ht,
469 struct bucket_table *tbl,
470 struct rhash_head *obj)
471{
472 struct rhash_head __rcu **pprev;
473 struct rhash_head *he;
474 spinlock_t * lock;
475 unsigned hash;
476 bool ret = false;
477
Herbert Xueca84932015-03-12 14:49:39 +1100478 hash = head_hashfn(ht, tbl, obj);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100479 lock = bucket_lock(tbl, hash);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100480
481 spin_lock_bh(lock);
482
483 pprev = &tbl->buckets[hash];
484 rht_for_each(he, tbl, hash) {
485 if (he != obj) {
486 pprev = &he->next;
487 continue;
488 }
489
490 rcu_assign_pointer(*pprev, obj->next);
491 ret = true;
492 break;
493 }
494
495 spin_unlock_bh(lock);
496
497 return ret;
498}
499
Thomas Graf7e1e7762014-08-02 11:47:44 +0200500/**
Thomas Graf7e1e7762014-08-02 11:47:44 +0200501 * rhashtable_remove - remove object from hash table
502 * @ht: hash table
503 * @obj: pointer to hash head inside object
Thomas Graf7e1e7762014-08-02 11:47:44 +0200504 *
505 * Since the hash chain is single linked, the removal operation needs to
506 * walk the bucket chain upon removal. The removal operation is thus
507 * considerable slow if the hash table is not correctly sized.
508 *
Ying Xuedb304852015-01-07 13:41:54 +0800509 * Will automatically shrink the table via rhashtable_expand() if the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200510 * shrink_decision function specified at rhashtable_init() returns true.
511 *
512 * The caller must ensure that no concurrent table mutations occur. It is
513 * however valid to have concurrent lookups if they are RCU protected.
514 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100515bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *obj)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200516{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100517 struct bucket_table *tbl, *old_tbl;
518 bool ret;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200519
Thomas Graf97defe12015-01-02 23:00:20 +0100520 rcu_read_lock();
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100521
Thomas Graf020219a2015-02-06 16:08:43 +0000522 old_tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100523 ret = __rhashtable_remove(ht, old_tbl, obj);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200524
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100525 /* Because we have already taken (and released) the bucket
526 * lock in old_tbl, if we find that future_tbl is not yet
527 * visible then that guarantees the entry to still be in
528 * old_tbl if it exists.
Thomas Graffe6a0432015-01-21 11:54:01 +0000529 */
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100530 tbl = rht_dereference_rcu(ht->future_tbl, ht);
531 if (!ret && old_tbl != tbl)
532 ret = __rhashtable_remove(ht, tbl, obj);
Thomas Graffe6a0432015-01-21 11:54:01 +0000533
534 if (ret) {
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100535 bool no_resize_running = tbl == old_tbl;
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100536
Thomas Graffe6a0432015-01-21 11:54:01 +0000537 atomic_dec(&ht->nelems);
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100538 if (no_resize_running && rht_shrink_below_30(ht, tbl))
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100539 schedule_work(&ht->run_work);
Thomas Graffe6a0432015-01-21 11:54:01 +0000540 }
541
Thomas Graf97defe12015-01-02 23:00:20 +0100542 rcu_read_unlock();
543
Thomas Graffe6a0432015-01-21 11:54:01 +0000544 return ret;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200545}
546EXPORT_SYMBOL_GPL(rhashtable_remove);
547
Ying Xueefb975a62015-01-07 13:41:52 +0800548struct rhashtable_compare_arg {
549 struct rhashtable *ht;
550 const void *key;
551};
552
553static bool rhashtable_compare(void *ptr, void *arg)
554{
555 struct rhashtable_compare_arg *x = arg;
556 struct rhashtable *ht = x->ht;
557
558 return !memcmp(ptr + ht->p.key_offset, x->key, ht->p.key_len);
559}
560
Thomas Graf7e1e7762014-08-02 11:47:44 +0200561/**
562 * rhashtable_lookup - lookup key in hash table
563 * @ht: hash table
564 * @key: pointer to key
565 *
566 * Computes the hash value for the key and traverses the bucket chain looking
567 * for a entry with an identical key. The first matching entry is returned.
568 *
569 * This lookup function may only be used for fixed key hash table (key_len
Ying Xuedb304852015-01-07 13:41:54 +0800570 * parameter set). It will BUG() if used inappropriately.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200571 *
Thomas Graf97defe12015-01-02 23:00:20 +0100572 * Lookups may occur in parallel with hashtable mutations and resizing.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200573 */
Thomas Graf97defe12015-01-02 23:00:20 +0100574void *rhashtable_lookup(struct rhashtable *ht, const void *key)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200575{
Ying Xueefb975a62015-01-07 13:41:52 +0800576 struct rhashtable_compare_arg arg = {
577 .ht = ht,
578 .key = key,
579 };
Thomas Graf7e1e7762014-08-02 11:47:44 +0200580
581 BUG_ON(!ht->p.key_len);
582
Ying Xueefb975a62015-01-07 13:41:52 +0800583 return rhashtable_lookup_compare(ht, key, &rhashtable_compare, &arg);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200584}
585EXPORT_SYMBOL_GPL(rhashtable_lookup);
586
587/**
588 * rhashtable_lookup_compare - search hash table with compare function
589 * @ht: hash table
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100590 * @key: the pointer to the key
Thomas Graf7e1e7762014-08-02 11:47:44 +0200591 * @compare: compare function, must return true on match
592 * @arg: argument passed on to compare function
593 *
594 * Traverses the bucket chain behind the provided hash value and calls the
595 * specified compare function for each entry.
596 *
Thomas Graf97defe12015-01-02 23:00:20 +0100597 * Lookups may occur in parallel with hashtable mutations and resizing.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200598 *
599 * Returns the first entry on which the compare function returned true.
600 */
Thomas Graf97defe12015-01-02 23:00:20 +0100601void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200602 bool (*compare)(void *, void *), void *arg)
603{
Thomas Graf97defe12015-01-02 23:00:20 +0100604 const struct bucket_table *tbl, *old_tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200605 struct rhash_head *he;
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100606 u32 hash;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200607
Thomas Graf97defe12015-01-02 23:00:20 +0100608 rcu_read_lock();
609
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100610 tbl = rht_dereference_rcu(ht->tbl, ht);
Thomas Graf97defe12015-01-02 23:00:20 +0100611restart:
Herbert Xu39361942015-03-13 12:54:10 +1100612 hash = key_hashfn(ht, tbl, key);
Herbert Xu8d2b1872015-03-12 14:49:38 +1100613 rht_for_each_rcu(he, tbl, hash) {
Thomas Graf7e1e7762014-08-02 11:47:44 +0200614 if (!compare(rht_obj(ht, he), arg))
615 continue;
Thomas Graf97defe12015-01-02 23:00:20 +0100616 rcu_read_unlock();
Thomas Grafa4b18cd2015-01-02 23:00:15 +0100617 return rht_obj(ht, he);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200618 }
619
Herbert Xu9497df82015-03-12 22:07:49 +1100620 /* Ensure we see any new tables. */
621 smp_rmb();
622
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100623 old_tbl = tbl;
624 tbl = rht_dereference_rcu(ht->future_tbl, ht);
625 if (unlikely(tbl != old_tbl))
Thomas Graf97defe12015-01-02 23:00:20 +0100626 goto restart;
Thomas Graf97defe12015-01-02 23:00:20 +0100627 rcu_read_unlock();
628
Thomas Graf7e1e7762014-08-02 11:47:44 +0200629 return NULL;
630}
631EXPORT_SYMBOL_GPL(rhashtable_lookup_compare);
632
Ying Xuedb304852015-01-07 13:41:54 +0800633/**
634 * rhashtable_lookup_insert - lookup and insert object into hash table
635 * @ht: hash table
636 * @obj: pointer to hash head inside object
637 *
638 * Locks down the bucket chain in both the old and new table if a resize
639 * is in progress to ensure that writers can't remove from the old table
640 * and can't insert to the new table during the atomic operation of search
641 * and insertion. Searches for duplicates in both the old and new table if
642 * a resize is in progress.
643 *
644 * This lookup function may only be used for fixed key hash table (key_len
645 * parameter set). It will BUG() if used inappropriately.
646 *
647 * It is safe to call this function from atomic context.
648 *
649 * Will trigger an automatic deferred table resizing if the size grows
650 * beyond the watermark indicated by grow_decision() which can be passed
651 * to rhashtable_init().
652 */
653bool rhashtable_lookup_insert(struct rhashtable *ht, struct rhash_head *obj)
654{
Ying Xue7a868d12015-01-12 14:52:22 +0800655 struct rhashtable_compare_arg arg = {
656 .ht = ht,
657 .key = rht_obj(ht, obj) + ht->p.key_offset,
658 };
659
660 BUG_ON(!ht->p.key_len);
661
662 return rhashtable_lookup_compare_insert(ht, obj, &rhashtable_compare,
663 &arg);
664}
665EXPORT_SYMBOL_GPL(rhashtable_lookup_insert);
666
667/**
668 * rhashtable_lookup_compare_insert - search and insert object to hash table
669 * with compare function
670 * @ht: hash table
671 * @obj: pointer to hash head inside object
672 * @compare: compare function, must return true on match
673 * @arg: argument passed on to compare function
674 *
675 * Locks down the bucket chain in both the old and new table if a resize
676 * is in progress to ensure that writers can't remove from the old table
677 * and can't insert to the new table during the atomic operation of search
678 * and insertion. Searches for duplicates in both the old and new table if
679 * a resize is in progress.
680 *
681 * Lookups may occur in parallel with hashtable mutations and resizing.
682 *
683 * Will trigger an automatic deferred table resizing if the size grows
684 * beyond the watermark indicated by grow_decision() which can be passed
685 * to rhashtable_init().
686 */
687bool rhashtable_lookup_compare_insert(struct rhashtable *ht,
688 struct rhash_head *obj,
689 bool (*compare)(void *, void *),
690 void *arg)
691{
Ying Xuedb304852015-01-07 13:41:54 +0800692 BUG_ON(!ht->p.key_len);
693
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100694 return __rhashtable_insert(ht, obj, compare, arg);
Ying Xuedb304852015-01-07 13:41:54 +0800695}
Ying Xue7a868d12015-01-12 14:52:22 +0800696EXPORT_SYMBOL_GPL(rhashtable_lookup_compare_insert);
Ying Xuedb304852015-01-07 13:41:54 +0800697
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100698/**
699 * rhashtable_walk_init - Initialise an iterator
700 * @ht: Table to walk over
701 * @iter: Hash table Iterator
702 *
703 * This function prepares a hash table walk.
704 *
705 * Note that if you restart a walk after rhashtable_walk_stop you
706 * may see the same object twice. Also, you may miss objects if
707 * there are removals in between rhashtable_walk_stop and the next
708 * call to rhashtable_walk_start.
709 *
710 * For a completely stable walk you should construct your own data
711 * structure outside the hash table.
712 *
713 * This function may sleep so you must not call it from interrupt
714 * context or with spin locks held.
715 *
716 * You must call rhashtable_walk_exit if this function returns
717 * successfully.
718 */
719int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter)
720{
721 iter->ht = ht;
722 iter->p = NULL;
723 iter->slot = 0;
724 iter->skip = 0;
725
726 iter->walker = kmalloc(sizeof(*iter->walker), GFP_KERNEL);
727 if (!iter->walker)
728 return -ENOMEM;
729
730 mutex_lock(&ht->mutex);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100731 iter->walker->tbl = rht_dereference(ht->tbl, ht);
732 list_add(&iter->walker->list, &iter->walker->tbl->walkers);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100733 mutex_unlock(&ht->mutex);
734
735 return 0;
736}
737EXPORT_SYMBOL_GPL(rhashtable_walk_init);
738
739/**
740 * rhashtable_walk_exit - Free an iterator
741 * @iter: Hash table Iterator
742 *
743 * This function frees resources allocated by rhashtable_walk_init.
744 */
745void rhashtable_walk_exit(struct rhashtable_iter *iter)
746{
747 mutex_lock(&iter->ht->mutex);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100748 if (iter->walker->tbl)
749 list_del(&iter->walker->list);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100750 mutex_unlock(&iter->ht->mutex);
751 kfree(iter->walker);
752}
753EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
754
755/**
756 * rhashtable_walk_start - Start a hash table walk
757 * @iter: Hash table iterator
758 *
759 * Start a hash table walk. Note that we take the RCU lock in all
760 * cases including when we return an error. So you must always call
761 * rhashtable_walk_stop to clean up.
762 *
763 * Returns zero if successful.
764 *
765 * Returns -EAGAIN if resize event occured. Note that the iterator
766 * will rewind back to the beginning and you may use it immediately
767 * by calling rhashtable_walk_next.
768 */
769int rhashtable_walk_start(struct rhashtable_iter *iter)
770{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100771 struct rhashtable *ht = iter->ht;
772
773 mutex_lock(&ht->mutex);
774
775 if (iter->walker->tbl)
776 list_del(&iter->walker->list);
777
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100778 rcu_read_lock();
779
Herbert Xueddee5ba2015-03-14 13:57:20 +1100780 mutex_unlock(&ht->mutex);
781
782 if (!iter->walker->tbl) {
783 iter->walker->tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100784 return -EAGAIN;
785 }
786
787 return 0;
788}
789EXPORT_SYMBOL_GPL(rhashtable_walk_start);
790
791/**
792 * rhashtable_walk_next - Return the next object and advance the iterator
793 * @iter: Hash table iterator
794 *
795 * Note that you must call rhashtable_walk_stop when you are finished
796 * with the walk.
797 *
798 * Returns the next object or NULL when the end of the table is reached.
799 *
800 * Returns -EAGAIN if resize event occured. Note that the iterator
801 * will rewind back to the beginning and you may continue to use it.
802 */
803void *rhashtable_walk_next(struct rhashtable_iter *iter)
804{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100805 struct bucket_table *tbl = iter->walker->tbl;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100806 struct rhashtable *ht = iter->ht;
807 struct rhash_head *p = iter->p;
808 void *obj = NULL;
809
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100810 if (p) {
811 p = rht_dereference_bucket_rcu(p->next, tbl, iter->slot);
812 goto next;
813 }
814
815 for (; iter->slot < tbl->size; iter->slot++) {
816 int skip = iter->skip;
817
818 rht_for_each_rcu(p, tbl, iter->slot) {
819 if (!skip)
820 break;
821 skip--;
822 }
823
824next:
825 if (!rht_is_a_nulls(p)) {
826 iter->skip++;
827 iter->p = p;
828 obj = rht_obj(ht, p);
829 goto out;
830 }
831
832 iter->skip = 0;
833 }
834
Herbert Xueddee5ba2015-03-14 13:57:20 +1100835 iter->walker->tbl = rht_dereference_rcu(ht->future_tbl, ht);
836 if (iter->walker->tbl != tbl) {
837 iter->slot = 0;
838 iter->skip = 0;
839 return ERR_PTR(-EAGAIN);
840 }
841
842 iter->walker->tbl = NULL;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100843 iter->p = NULL;
844
845out:
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100846
847 return obj;
848}
849EXPORT_SYMBOL_GPL(rhashtable_walk_next);
850
851/**
852 * rhashtable_walk_stop - Finish a hash table walk
853 * @iter: Hash table iterator
854 *
855 * Finish a hash table walk.
856 */
857void rhashtable_walk_stop(struct rhashtable_iter *iter)
858{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100859 struct rhashtable *ht;
860 struct bucket_table *tbl = iter->walker->tbl;
861
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100862 rcu_read_unlock();
Herbert Xueddee5ba2015-03-14 13:57:20 +1100863
864 if (!tbl)
865 return;
866
867 ht = iter->ht;
868
869 mutex_lock(&ht->mutex);
870 if (rht_dereference(ht->tbl, ht) == tbl ||
871 rht_dereference(ht->future_tbl, ht) == tbl)
872 list_add(&iter->walker->list, &tbl->walkers);
873 else
874 iter->walker->tbl = NULL;
875 mutex_unlock(&ht->mutex);
876
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100877 iter->p = NULL;
878}
879EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
880
Ying Xue94000172014-09-03 09:22:36 +0800881static size_t rounded_hashtable_size(struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200882{
Ying Xue94000172014-09-03 09:22:36 +0800883 return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
884 1UL << params->min_shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200885}
886
887/**
888 * rhashtable_init - initialize a new hash table
889 * @ht: hash table to be initialized
890 * @params: configuration parameters
891 *
892 * Initializes a new hash table based on the provided configuration
893 * parameters. A table can be configured either with a variable or
894 * fixed length key:
895 *
896 * Configuration Example 1: Fixed length keys
897 * struct test_obj {
898 * int key;
899 * void * my_member;
900 * struct rhash_head node;
901 * };
902 *
903 * struct rhashtable_params params = {
904 * .head_offset = offsetof(struct test_obj, node),
905 * .key_offset = offsetof(struct test_obj, key),
906 * .key_len = sizeof(int),
Daniel Borkmann87545892014-12-10 16:33:11 +0100907 * .hashfn = jhash,
Thomas Graff89bd6f2015-01-02 23:00:21 +0100908 * .nulls_base = (1U << RHT_BASE_SHIFT),
Thomas Graf7e1e7762014-08-02 11:47:44 +0200909 * };
910 *
911 * Configuration Example 2: Variable length keys
912 * struct test_obj {
913 * [...]
914 * struct rhash_head node;
915 * };
916 *
917 * u32 my_hash_fn(const void *data, u32 seed)
918 * {
919 * struct test_obj *obj = data;
920 *
921 * return [... hash ...];
922 * }
923 *
924 * struct rhashtable_params params = {
925 * .head_offset = offsetof(struct test_obj, node),
Daniel Borkmann87545892014-12-10 16:33:11 +0100926 * .hashfn = jhash,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200927 * .obj_hashfn = my_hash_fn,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200928 * };
929 */
930int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params)
931{
932 struct bucket_table *tbl;
933 size_t size;
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100934 u32 hash_rnd;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200935
936 size = HASH_DEFAULT_SIZE;
937
938 if ((params->key_len && !params->hashfn) ||
939 (!params->key_len && !params->obj_hashfn))
940 return -EINVAL;
941
Thomas Graff89bd6f2015-01-02 23:00:21 +0100942 if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT))
943 return -EINVAL;
944
Ying Xue94000172014-09-03 09:22:36 +0800945 params->min_shift = max_t(size_t, params->min_shift,
946 ilog2(HASH_MIN_SIZE));
947
Thomas Graf7e1e7762014-08-02 11:47:44 +0200948 if (params->nelem_hint)
Ying Xue94000172014-09-03 09:22:36 +0800949 size = rounded_hashtable_size(params);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200950
Thomas Graf97defe12015-01-02 23:00:20 +0100951 memset(ht, 0, sizeof(*ht));
952 mutex_init(&ht->mutex);
953 memcpy(&ht->p, params, sizeof(*params));
954
955 if (params->locks_mul)
956 ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
957 else
958 ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
959
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100960 get_random_bytes(&hash_rnd, sizeof(hash_rnd));
961
962 tbl = bucket_table_alloc(ht, size, hash_rnd);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200963 if (tbl == NULL)
964 return -ENOMEM;
965
Ying Xue545a1482015-01-07 13:41:57 +0800966 atomic_set(&ht->nelems, 0);
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100967
Thomas Graf7e1e7762014-08-02 11:47:44 +0200968 RCU_INIT_POINTER(ht->tbl, tbl);
Thomas Graf97defe12015-01-02 23:00:20 +0100969 RCU_INIT_POINTER(ht->future_tbl, tbl);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200970
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100971 INIT_WORK(&ht->run_work, rht_deferred_worker);
Thomas Graf97defe12015-01-02 23:00:20 +0100972
Thomas Graf7e1e7762014-08-02 11:47:44 +0200973 return 0;
974}
975EXPORT_SYMBOL_GPL(rhashtable_init);
976
977/**
978 * rhashtable_destroy - destroy hash table
979 * @ht: the hash table to destroy
980 *
Pablo Neira Ayusoae82ddc2014-09-02 00:26:05 +0200981 * Frees the bucket array. This function is not rcu safe, therefore the caller
982 * has to make sure that no resizing may happen by unpublishing the hashtable
983 * and waiting for the quiescent cycle before releasing the bucket array.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200984 */
Thomas Graf97defe12015-01-02 23:00:20 +0100985void rhashtable_destroy(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200986{
Thomas Graf97defe12015-01-02 23:00:20 +0100987 ht->being_destroyed = true;
988
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100989 cancel_work_sync(&ht->run_work);
Ying Xue57699a42015-01-16 11:13:09 +0800990
Thomas Graf97defe12015-01-02 23:00:20 +0100991 mutex_lock(&ht->mutex);
Thomas Graf97defe12015-01-02 23:00:20 +0100992 bucket_table_free(rht_dereference(ht->tbl, ht));
Thomas Graf97defe12015-01-02 23:00:20 +0100993 mutex_unlock(&ht->mutex);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200994}
995EXPORT_SYMBOL_GPL(rhashtable_destroy);