blob: 5f8fe3e8821988d0014ffa07ca4cd2ab3a03e8d8 [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_size || tbl->size < ht->p.max_size);
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) &&
Herbert Xuc2e213c2015-03-18 20:01:16 +1100204 tbl->size > ht->p.min_size;
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 *old_tbl = rht_dereference(ht->tbl, ht);
Herbert Xuc4db8842015-03-14 13:57:25 +1100210 struct bucket_table *new_tbl =
211 rht_dereference(old_tbl->future_tbl, ht) ?: old_tbl;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100212 struct rhash_head __rcu **pprev = &old_tbl->buckets[old_hash];
213 int err = -ENOENT;
214 struct rhash_head *head, *next, *entry;
215 spinlock_t *new_bucket_lock;
216 unsigned new_hash;
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100217
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100218 rht_for_each(entry, old_tbl, old_hash) {
219 err = 0;
220 next = rht_dereference_bucket(entry->next, old_tbl, old_hash);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100221
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100222 if (rht_is_a_nulls(next))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200223 break;
Thomas Graf97defe12015-01-02 23:00:20 +0100224
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100225 pprev = &entry->next;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200226 }
227
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100228 if (err)
229 goto out;
Thomas Graf97defe12015-01-02 23:00:20 +0100230
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100231 new_hash = head_hashfn(ht, new_tbl, entry);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100232
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100233 new_bucket_lock = bucket_lock(new_tbl, new_hash);
234
Herbert Xu8f2484b2015-03-14 13:57:21 +1100235 spin_lock_nested(new_bucket_lock, SINGLE_DEPTH_NESTING);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100236 head = rht_dereference_bucket(new_tbl->buckets[new_hash],
237 new_tbl, new_hash);
238
239 if (rht_is_a_nulls(head))
240 INIT_RHT_NULLS_HEAD(entry->next, ht, new_hash);
241 else
242 RCU_INIT_POINTER(entry->next, head);
243
244 rcu_assign_pointer(new_tbl->buckets[new_hash], entry);
245 spin_unlock(new_bucket_lock);
246
247 rcu_assign_pointer(*pprev, next);
248
249out:
250 return err;
Thomas Graf97defe12015-01-02 23:00:20 +0100251}
252
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100253static void rhashtable_rehash_chain(struct rhashtable *ht, unsigned old_hash)
Thomas Graf97defe12015-01-02 23:00:20 +0100254{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100255 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
256 spinlock_t *old_bucket_lock;
Thomas Graf7cd10db2015-02-05 02:03:35 +0100257
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100258 old_bucket_lock = bucket_lock(old_tbl, old_hash);
259
260 spin_lock_bh(old_bucket_lock);
261 while (!rhashtable_rehash_one(ht, old_hash))
262 ;
Herbert Xu63d512d2015-03-14 13:57:24 +1100263 old_tbl->rehash++;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100264 spin_unlock_bh(old_bucket_lock);
265}
266
267static void rhashtable_rehash(struct rhashtable *ht,
268 struct bucket_table *new_tbl)
269{
270 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100271 struct rhashtable_walker *walker;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100272 unsigned old_hash;
273
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100274 /* Make insertions go into the new, empty table right away. Deletions
275 * and lookups will be attempted in both tables until we synchronize.
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100276 */
Herbert Xuc4db8842015-03-14 13:57:25 +1100277 rcu_assign_pointer(old_tbl->future_tbl, new_tbl);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100278
Herbert Xu9497df82015-03-12 22:07:49 +1100279 /* Ensure the new table is visible to readers. */
280 smp_wmb();
281
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100282 for (old_hash = 0; old_hash < old_tbl->size; old_hash++)
283 rhashtable_rehash_chain(ht, old_hash);
284
285 /* Publish the new table pointer. */
286 rcu_assign_pointer(ht->tbl, new_tbl);
287
Herbert Xueddee5ba2015-03-14 13:57:20 +1100288 list_for_each_entry(walker, &old_tbl->walkers, list)
289 walker->tbl = NULL;
290
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100291 /* Wait for readers. All new readers will see the new
292 * table, and thus no references to the old table will
293 * remain.
294 */
Herbert Xu9d901bc2015-03-14 13:57:23 +1100295 call_rcu(&old_tbl->rcu, bucket_table_free_rcu);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200296}
297
298/**
299 * rhashtable_expand - Expand hash table while allowing concurrent lookups
300 * @ht: the hash table to expand
Thomas Graf7e1e7762014-08-02 11:47:44 +0200301 *
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100302 * A secondary bucket array is allocated and the hash entries are migrated.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200303 *
304 * This function may only be called in a context where it is safe to call
305 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
306 *
Thomas Graf97defe12015-01-02 23:00:20 +0100307 * The caller must ensure that no concurrent resizing occurs by holding
308 * ht->mutex.
309 *
310 * It is valid to have concurrent insertions and deletions protected by per
311 * bucket locks or concurrent RCU protected lookups and traversals.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200312 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100313int rhashtable_expand(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200314{
315 struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200316
317 ASSERT_RHT_MUTEX(ht);
318
Herbert Xu5269b532015-03-14 13:57:22 +1100319 new_tbl = bucket_table_alloc(ht, old_tbl->size * 2);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200320 if (new_tbl == NULL)
321 return -ENOMEM;
322
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100323 rhashtable_rehash(ht, new_tbl);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200324 return 0;
325}
326EXPORT_SYMBOL_GPL(rhashtable_expand);
327
328/**
329 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
330 * @ht: the hash table to shrink
Thomas Graf7e1e7762014-08-02 11:47:44 +0200331 *
332 * This function may only be called in a context where it is safe to call
333 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
334 *
Thomas Graf97defe12015-01-02 23:00:20 +0100335 * The caller must ensure that no concurrent resizing occurs by holding
336 * ht->mutex.
337 *
Thomas Graf7e1e7762014-08-02 11:47:44 +0200338 * The caller must ensure that no concurrent table mutations take place.
339 * It is however valid to have concurrent lookups if they are RCU protected.
Thomas Graf97defe12015-01-02 23:00:20 +0100340 *
341 * It is valid to have concurrent insertions and deletions protected by per
342 * bucket locks or concurrent RCU protected lookups and traversals.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200343 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100344int rhashtable_shrink(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200345{
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100346 struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200347
348 ASSERT_RHT_MUTEX(ht);
349
Herbert Xu5269b532015-03-14 13:57:22 +1100350 new_tbl = bucket_table_alloc(ht, old_tbl->size / 2);
Thomas Graf97defe12015-01-02 23:00:20 +0100351 if (new_tbl == NULL)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200352 return -ENOMEM;
353
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100354 rhashtable_rehash(ht, new_tbl);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200355 return 0;
356}
357EXPORT_SYMBOL_GPL(rhashtable_shrink);
358
Thomas Graf97defe12015-01-02 23:00:20 +0100359static void rht_deferred_worker(struct work_struct *work)
360{
361 struct rhashtable *ht;
362 struct bucket_table *tbl;
363
Ying Xue57699a42015-01-16 11:13:09 +0800364 ht = container_of(work, struct rhashtable, run_work);
Thomas Graf97defe12015-01-02 23:00:20 +0100365 mutex_lock(&ht->mutex);
Herbert Xu28134a52015-02-04 07:33:22 +1100366 if (ht->being_destroyed)
367 goto unlock;
368
Thomas Graf97defe12015-01-02 23:00:20 +0100369 tbl = rht_dereference(ht->tbl, ht);
370
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100371 if (rht_grow_above_75(ht, tbl))
Thomas Graf97defe12015-01-02 23:00:20 +0100372 rhashtable_expand(ht);
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100373 else if (rht_shrink_below_30(ht, tbl))
Thomas Graf97defe12015-01-02 23:00:20 +0100374 rhashtable_shrink(ht);
Herbert Xu28134a52015-02-04 07:33:22 +1100375unlock:
Thomas Graf97defe12015-01-02 23:00:20 +0100376 mutex_unlock(&ht->mutex);
377}
378
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100379static bool __rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj,
380 bool (*compare)(void *, void *), void *arg)
Ying Xuedb304852015-01-07 13:41:54 +0800381{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100382 struct bucket_table *tbl, *old_tbl;
Thomas Graf020219a2015-02-06 16:08:43 +0000383 struct rhash_head *head;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100384 bool no_resize_running;
385 unsigned hash;
Thomas Graf617011e2015-03-16 10:42:26 +0100386 spinlock_t *old_lock;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100387 bool success = true;
388
389 rcu_read_lock();
390
391 old_tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xueca84932015-03-12 14:49:39 +1100392 hash = head_hashfn(ht, old_tbl, obj);
Thomas Graf617011e2015-03-16 10:42:26 +0100393 old_lock = bucket_lock(old_tbl, hash);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100394
Thomas Graf617011e2015-03-16 10:42:26 +0100395 spin_lock_bh(old_lock);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100396
397 /* Because we have already taken the bucket lock in old_tbl,
398 * if we find that future_tbl is not yet visible then that
399 * guarantees all other insertions of the same entry will
400 * also grab the bucket lock in old_tbl because until the
401 * rehash completes ht->tbl won't be changed.
402 */
Herbert Xuc4db8842015-03-14 13:57:25 +1100403 tbl = rht_dereference_rcu(old_tbl->future_tbl, ht) ?: old_tbl;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100404 if (tbl != old_tbl) {
Herbert Xueca84932015-03-12 14:49:39 +1100405 hash = head_hashfn(ht, tbl, obj);
Herbert Xu8f2484b2015-03-14 13:57:21 +1100406 spin_lock_nested(bucket_lock(tbl, hash), SINGLE_DEPTH_NESTING);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100407 }
408
409 if (compare &&
410 rhashtable_lookup_compare(ht, rht_obj(ht, obj) + ht->p.key_offset,
411 compare, arg)) {
412 success = false;
413 goto exit;
414 }
415
416 no_resize_running = tbl == old_tbl;
Thomas Graf020219a2015-02-06 16:08:43 +0000417
Thomas Graf020219a2015-02-06 16:08:43 +0000418 head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash);
Ying Xuedb304852015-01-07 13:41:54 +0800419
420 if (rht_is_a_nulls(head))
421 INIT_RHT_NULLS_HEAD(obj->next, ht, hash);
422 else
423 RCU_INIT_POINTER(obj->next, head);
424
425 rcu_assign_pointer(tbl->buckets[hash], obj);
426
427 atomic_inc(&ht->nelems);
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100428 if (no_resize_running && rht_grow_above_75(ht, tbl))
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100429 schedule_work(&ht->run_work);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100430
431exit:
Thomas Graf617011e2015-03-16 10:42:26 +0100432 if (tbl != old_tbl)
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100433 spin_unlock(bucket_lock(tbl, hash));
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100434
Thomas Graf617011e2015-03-16 10:42:26 +0100435 spin_unlock_bh(old_lock);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100436
437 rcu_read_unlock();
438
439 return success;
Ying Xuedb304852015-01-07 13:41:54 +0800440}
441
Thomas Graf7e1e7762014-08-02 11:47:44 +0200442/**
Ying Xuedb304852015-01-07 13:41:54 +0800443 * rhashtable_insert - insert object into hash table
Thomas Graf7e1e7762014-08-02 11:47:44 +0200444 * @ht: hash table
445 * @obj: pointer to hash head inside object
Thomas Graf7e1e7762014-08-02 11:47:44 +0200446 *
Thomas Graf97defe12015-01-02 23:00:20 +0100447 * Will take a per bucket spinlock to protect against mutual mutations
448 * on the same bucket. Multiple insertions may occur in parallel unless
449 * they map to the same bucket lock.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200450 *
Thomas Graf97defe12015-01-02 23:00:20 +0100451 * It is safe to call this function from atomic context.
452 *
453 * Will trigger an automatic deferred table resizing if the size grows
454 * beyond the watermark indicated by grow_decision() which can be passed
455 * to rhashtable_init().
Thomas Graf7e1e7762014-08-02 11:47:44 +0200456 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100457void rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200458{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100459 __rhashtable_insert(ht, obj, NULL, NULL);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200460}
461EXPORT_SYMBOL_GPL(rhashtable_insert);
462
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100463static bool __rhashtable_remove(struct rhashtable *ht,
464 struct bucket_table *tbl,
465 struct rhash_head *obj)
466{
467 struct rhash_head __rcu **pprev;
468 struct rhash_head *he;
469 spinlock_t * lock;
470 unsigned hash;
471 bool ret = false;
472
Herbert Xueca84932015-03-12 14:49:39 +1100473 hash = head_hashfn(ht, tbl, obj);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100474 lock = bucket_lock(tbl, hash);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100475
476 spin_lock_bh(lock);
477
478 pprev = &tbl->buckets[hash];
479 rht_for_each(he, tbl, hash) {
480 if (he != obj) {
481 pprev = &he->next;
482 continue;
483 }
484
485 rcu_assign_pointer(*pprev, obj->next);
486 ret = true;
487 break;
488 }
489
490 spin_unlock_bh(lock);
491
492 return ret;
493}
494
Thomas Graf7e1e7762014-08-02 11:47:44 +0200495/**
Thomas Graf7e1e7762014-08-02 11:47:44 +0200496 * rhashtable_remove - remove object from hash table
497 * @ht: hash table
498 * @obj: pointer to hash head inside object
Thomas Graf7e1e7762014-08-02 11:47:44 +0200499 *
500 * Since the hash chain is single linked, the removal operation needs to
501 * walk the bucket chain upon removal. The removal operation is thus
502 * considerable slow if the hash table is not correctly sized.
503 *
Ying Xuedb304852015-01-07 13:41:54 +0800504 * Will automatically shrink the table via rhashtable_expand() if the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200505 * shrink_decision function specified at rhashtable_init() returns true.
506 *
507 * The caller must ensure that no concurrent table mutations occur. It is
508 * however valid to have concurrent lookups if they are RCU protected.
509 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100510bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *obj)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200511{
Herbert Xu565e8642015-03-15 21:12:05 +1100512 struct bucket_table *tbl;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100513 bool ret;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200514
Thomas Graf97defe12015-01-02 23:00:20 +0100515 rcu_read_lock();
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100516
Herbert Xu565e8642015-03-15 21:12:05 +1100517 tbl = rht_dereference_rcu(ht->tbl, ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200518
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100519 /* Because we have already taken (and released) the bucket
520 * lock in old_tbl, if we find that future_tbl is not yet
521 * visible then that guarantees the entry to still be in
Herbert Xu565e8642015-03-15 21:12:05 +1100522 * the old tbl if it exists.
Thomas Graffe6a0432015-01-21 11:54:01 +0000523 */
Herbert Xu565e8642015-03-15 21:12:05 +1100524 while (!(ret = __rhashtable_remove(ht, tbl, obj)) &&
525 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
526 ;
Thomas Graffe6a0432015-01-21 11:54:01 +0000527
528 if (ret) {
529 atomic_dec(&ht->nelems);
Herbert Xu565e8642015-03-15 21:12:05 +1100530 if (rht_shrink_below_30(ht, tbl))
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100531 schedule_work(&ht->run_work);
Thomas Graffe6a0432015-01-21 11:54:01 +0000532 }
533
Thomas Graf97defe12015-01-02 23:00:20 +0100534 rcu_read_unlock();
535
Thomas Graffe6a0432015-01-21 11:54:01 +0000536 return ret;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200537}
538EXPORT_SYMBOL_GPL(rhashtable_remove);
539
Ying Xueefb975a62015-01-07 13:41:52 +0800540struct rhashtable_compare_arg {
541 struct rhashtable *ht;
542 const void *key;
543};
544
545static bool rhashtable_compare(void *ptr, void *arg)
546{
547 struct rhashtable_compare_arg *x = arg;
548 struct rhashtable *ht = x->ht;
549
550 return !memcmp(ptr + ht->p.key_offset, x->key, ht->p.key_len);
551}
552
Thomas Graf7e1e7762014-08-02 11:47:44 +0200553/**
554 * rhashtable_lookup - lookup key in hash table
555 * @ht: hash table
556 * @key: pointer to key
557 *
558 * Computes the hash value for the key and traverses the bucket chain looking
559 * for a entry with an identical key. The first matching entry is returned.
560 *
561 * This lookup function may only be used for fixed key hash table (key_len
Ying Xuedb304852015-01-07 13:41:54 +0800562 * parameter set). It will BUG() if used inappropriately.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200563 *
Thomas Graf97defe12015-01-02 23:00:20 +0100564 * Lookups may occur in parallel with hashtable mutations and resizing.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200565 */
Thomas Graf97defe12015-01-02 23:00:20 +0100566void *rhashtable_lookup(struct rhashtable *ht, const void *key)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200567{
Ying Xueefb975a62015-01-07 13:41:52 +0800568 struct rhashtable_compare_arg arg = {
569 .ht = ht,
570 .key = key,
571 };
Thomas Graf7e1e7762014-08-02 11:47:44 +0200572
573 BUG_ON(!ht->p.key_len);
574
Ying Xueefb975a62015-01-07 13:41:52 +0800575 return rhashtable_lookup_compare(ht, key, &rhashtable_compare, &arg);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200576}
577EXPORT_SYMBOL_GPL(rhashtable_lookup);
578
579/**
580 * rhashtable_lookup_compare - search hash table with compare function
581 * @ht: hash table
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100582 * @key: the pointer to the key
Thomas Graf7e1e7762014-08-02 11:47:44 +0200583 * @compare: compare function, must return true on match
584 * @arg: argument passed on to compare function
585 *
586 * Traverses the bucket chain behind the provided hash value and calls the
587 * specified compare function for each entry.
588 *
Thomas Graf97defe12015-01-02 23:00:20 +0100589 * Lookups may occur in parallel with hashtable mutations and resizing.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200590 *
591 * Returns the first entry on which the compare function returned true.
592 */
Thomas Graf97defe12015-01-02 23:00:20 +0100593void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200594 bool (*compare)(void *, void *), void *arg)
595{
Herbert Xuc4db8842015-03-14 13:57:25 +1100596 const struct bucket_table *tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200597 struct rhash_head *he;
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100598 u32 hash;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200599
Thomas Graf97defe12015-01-02 23:00:20 +0100600 rcu_read_lock();
601
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100602 tbl = rht_dereference_rcu(ht->tbl, ht);
Thomas Graf97defe12015-01-02 23:00:20 +0100603restart:
Herbert Xu39361942015-03-13 12:54:10 +1100604 hash = key_hashfn(ht, tbl, key);
Herbert Xu8d2b1872015-03-12 14:49:38 +1100605 rht_for_each_rcu(he, tbl, hash) {
Thomas Graf7e1e7762014-08-02 11:47:44 +0200606 if (!compare(rht_obj(ht, he), arg))
607 continue;
Thomas Graf97defe12015-01-02 23:00:20 +0100608 rcu_read_unlock();
Thomas Grafa4b18cd2015-01-02 23:00:15 +0100609 return rht_obj(ht, he);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200610 }
611
Herbert Xu9497df82015-03-12 22:07:49 +1100612 /* Ensure we see any new tables. */
613 smp_rmb();
614
Herbert Xuc4db8842015-03-14 13:57:25 +1100615 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
616 if (unlikely(tbl))
Thomas Graf97defe12015-01-02 23:00:20 +0100617 goto restart;
Thomas Graf97defe12015-01-02 23:00:20 +0100618 rcu_read_unlock();
619
Thomas Graf7e1e7762014-08-02 11:47:44 +0200620 return NULL;
621}
622EXPORT_SYMBOL_GPL(rhashtable_lookup_compare);
623
Ying Xuedb304852015-01-07 13:41:54 +0800624/**
625 * rhashtable_lookup_insert - lookup and insert object into hash table
626 * @ht: hash table
627 * @obj: pointer to hash head inside object
628 *
629 * Locks down the bucket chain in both the old and new table if a resize
630 * is in progress to ensure that writers can't remove from the old table
631 * and can't insert to the new table during the atomic operation of search
632 * and insertion. Searches for duplicates in both the old and new table if
633 * a resize is in progress.
634 *
635 * This lookup function may only be used for fixed key hash table (key_len
636 * parameter set). It will BUG() if used inappropriately.
637 *
638 * It is safe to call this function from atomic context.
639 *
640 * Will trigger an automatic deferred table resizing if the size grows
641 * beyond the watermark indicated by grow_decision() which can be passed
642 * to rhashtable_init().
643 */
644bool rhashtable_lookup_insert(struct rhashtable *ht, struct rhash_head *obj)
645{
Ying Xue7a868d12015-01-12 14:52:22 +0800646 struct rhashtable_compare_arg arg = {
647 .ht = ht,
648 .key = rht_obj(ht, obj) + ht->p.key_offset,
649 };
650
651 BUG_ON(!ht->p.key_len);
652
653 return rhashtable_lookup_compare_insert(ht, obj, &rhashtable_compare,
654 &arg);
655}
656EXPORT_SYMBOL_GPL(rhashtable_lookup_insert);
657
658/**
659 * rhashtable_lookup_compare_insert - search and insert object to hash table
660 * with compare function
661 * @ht: hash table
662 * @obj: pointer to hash head inside object
663 * @compare: compare function, must return true on match
664 * @arg: argument passed on to compare function
665 *
666 * Locks down the bucket chain in both the old and new table if a resize
667 * is in progress to ensure that writers can't remove from the old table
668 * and can't insert to the new table during the atomic operation of search
669 * and insertion. Searches for duplicates in both the old and new table if
670 * a resize is in progress.
671 *
672 * Lookups may occur in parallel with hashtable mutations and resizing.
673 *
674 * Will trigger an automatic deferred table resizing if the size grows
675 * beyond the watermark indicated by grow_decision() which can be passed
676 * to rhashtable_init().
677 */
678bool rhashtable_lookup_compare_insert(struct rhashtable *ht,
679 struct rhash_head *obj,
680 bool (*compare)(void *, void *),
681 void *arg)
682{
Ying Xuedb304852015-01-07 13:41:54 +0800683 BUG_ON(!ht->p.key_len);
684
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100685 return __rhashtable_insert(ht, obj, compare, arg);
Ying Xuedb304852015-01-07 13:41:54 +0800686}
Ying Xue7a868d12015-01-12 14:52:22 +0800687EXPORT_SYMBOL_GPL(rhashtable_lookup_compare_insert);
Ying Xuedb304852015-01-07 13:41:54 +0800688
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100689/**
690 * rhashtable_walk_init - Initialise an iterator
691 * @ht: Table to walk over
692 * @iter: Hash table Iterator
693 *
694 * This function prepares a hash table walk.
695 *
696 * Note that if you restart a walk after rhashtable_walk_stop you
697 * may see the same object twice. Also, you may miss objects if
698 * there are removals in between rhashtable_walk_stop and the next
699 * call to rhashtable_walk_start.
700 *
701 * For a completely stable walk you should construct your own data
702 * structure outside the hash table.
703 *
704 * This function may sleep so you must not call it from interrupt
705 * context or with spin locks held.
706 *
707 * You must call rhashtable_walk_exit if this function returns
708 * successfully.
709 */
710int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter)
711{
712 iter->ht = ht;
713 iter->p = NULL;
714 iter->slot = 0;
715 iter->skip = 0;
716
717 iter->walker = kmalloc(sizeof(*iter->walker), GFP_KERNEL);
718 if (!iter->walker)
719 return -ENOMEM;
720
721 mutex_lock(&ht->mutex);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100722 iter->walker->tbl = rht_dereference(ht->tbl, ht);
723 list_add(&iter->walker->list, &iter->walker->tbl->walkers);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100724 mutex_unlock(&ht->mutex);
725
726 return 0;
727}
728EXPORT_SYMBOL_GPL(rhashtable_walk_init);
729
730/**
731 * rhashtable_walk_exit - Free an iterator
732 * @iter: Hash table Iterator
733 *
734 * This function frees resources allocated by rhashtable_walk_init.
735 */
736void rhashtable_walk_exit(struct rhashtable_iter *iter)
737{
738 mutex_lock(&iter->ht->mutex);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100739 if (iter->walker->tbl)
740 list_del(&iter->walker->list);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100741 mutex_unlock(&iter->ht->mutex);
742 kfree(iter->walker);
743}
744EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
745
746/**
747 * rhashtable_walk_start - Start a hash table walk
748 * @iter: Hash table iterator
749 *
750 * Start a hash table walk. Note that we take the RCU lock in all
751 * cases including when we return an error. So you must always call
752 * rhashtable_walk_stop to clean up.
753 *
754 * Returns zero if successful.
755 *
756 * Returns -EAGAIN if resize event occured. Note that the iterator
757 * will rewind back to the beginning and you may use it immediately
758 * by calling rhashtable_walk_next.
759 */
760int rhashtable_walk_start(struct rhashtable_iter *iter)
Thomas Grafdb4374f2015-03-16 10:42:27 +0100761 __acquires(RCU)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100762{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100763 struct rhashtable *ht = iter->ht;
764
765 mutex_lock(&ht->mutex);
766
767 if (iter->walker->tbl)
768 list_del(&iter->walker->list);
769
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100770 rcu_read_lock();
771
Herbert Xueddee5ba2015-03-14 13:57:20 +1100772 mutex_unlock(&ht->mutex);
773
774 if (!iter->walker->tbl) {
775 iter->walker->tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100776 return -EAGAIN;
777 }
778
779 return 0;
780}
781EXPORT_SYMBOL_GPL(rhashtable_walk_start);
782
783/**
784 * rhashtable_walk_next - Return the next object and advance the iterator
785 * @iter: Hash table iterator
786 *
787 * Note that you must call rhashtable_walk_stop when you are finished
788 * with the walk.
789 *
790 * Returns the next object or NULL when the end of the table is reached.
791 *
792 * Returns -EAGAIN if resize event occured. Note that the iterator
793 * will rewind back to the beginning and you may continue to use it.
794 */
795void *rhashtable_walk_next(struct rhashtable_iter *iter)
796{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100797 struct bucket_table *tbl = iter->walker->tbl;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100798 struct rhashtable *ht = iter->ht;
799 struct rhash_head *p = iter->p;
800 void *obj = NULL;
801
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100802 if (p) {
803 p = rht_dereference_bucket_rcu(p->next, tbl, iter->slot);
804 goto next;
805 }
806
807 for (; iter->slot < tbl->size; iter->slot++) {
808 int skip = iter->skip;
809
810 rht_for_each_rcu(p, tbl, iter->slot) {
811 if (!skip)
812 break;
813 skip--;
814 }
815
816next:
817 if (!rht_is_a_nulls(p)) {
818 iter->skip++;
819 iter->p = p;
820 obj = rht_obj(ht, p);
821 goto out;
822 }
823
824 iter->skip = 0;
825 }
826
Herbert Xuc4db8842015-03-14 13:57:25 +1100827 iter->walker->tbl = rht_dereference_rcu(tbl->future_tbl, ht);
828 if (iter->walker->tbl) {
Herbert Xueddee5ba2015-03-14 13:57:20 +1100829 iter->slot = 0;
830 iter->skip = 0;
831 return ERR_PTR(-EAGAIN);
832 }
833
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100834 iter->p = NULL;
835
836out:
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100837
838 return obj;
839}
840EXPORT_SYMBOL_GPL(rhashtable_walk_next);
841
842/**
843 * rhashtable_walk_stop - Finish a hash table walk
844 * @iter: Hash table iterator
845 *
846 * Finish a hash table walk.
847 */
848void rhashtable_walk_stop(struct rhashtable_iter *iter)
Thomas Grafdb4374f2015-03-16 10:42:27 +0100849 __releases(RCU)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100850{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100851 struct rhashtable *ht;
852 struct bucket_table *tbl = iter->walker->tbl;
853
Herbert Xueddee5ba2015-03-14 13:57:20 +1100854 if (!tbl)
Herbert Xu963ecbd2015-03-15 21:12:04 +1100855 goto out;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100856
857 ht = iter->ht;
858
859 mutex_lock(&ht->mutex);
Herbert Xuc4db8842015-03-14 13:57:25 +1100860 if (tbl->rehash < tbl->size)
Herbert Xueddee5ba2015-03-14 13:57:20 +1100861 list_add(&iter->walker->list, &tbl->walkers);
862 else
863 iter->walker->tbl = NULL;
864 mutex_unlock(&ht->mutex);
865
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100866 iter->p = NULL;
Herbert Xu963ecbd2015-03-15 21:12:04 +1100867
868out:
869 rcu_read_unlock();
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100870}
871EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
872
Ying Xue94000172014-09-03 09:22:36 +0800873static size_t rounded_hashtable_size(struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200874{
Ying Xue94000172014-09-03 09:22:36 +0800875 return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
Herbert Xue2e21c12015-03-18 20:01:21 +1100876 (unsigned long)params->min_size);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200877}
878
879/**
880 * rhashtable_init - initialize a new hash table
881 * @ht: hash table to be initialized
882 * @params: configuration parameters
883 *
884 * Initializes a new hash table based on the provided configuration
885 * parameters. A table can be configured either with a variable or
886 * fixed length key:
887 *
888 * Configuration Example 1: Fixed length keys
889 * struct test_obj {
890 * int key;
891 * void * my_member;
892 * struct rhash_head node;
893 * };
894 *
895 * struct rhashtable_params params = {
896 * .head_offset = offsetof(struct test_obj, node),
897 * .key_offset = offsetof(struct test_obj, key),
898 * .key_len = sizeof(int),
Daniel Borkmann87545892014-12-10 16:33:11 +0100899 * .hashfn = jhash,
Thomas Graff89bd6f2015-01-02 23:00:21 +0100900 * .nulls_base = (1U << RHT_BASE_SHIFT),
Thomas Graf7e1e7762014-08-02 11:47:44 +0200901 * };
902 *
903 * Configuration Example 2: Variable length keys
904 * struct test_obj {
905 * [...]
906 * struct rhash_head node;
907 * };
908 *
909 * u32 my_hash_fn(const void *data, u32 seed)
910 * {
911 * struct test_obj *obj = data;
912 *
913 * return [... hash ...];
914 * }
915 *
916 * struct rhashtable_params params = {
917 * .head_offset = offsetof(struct test_obj, node),
Daniel Borkmann87545892014-12-10 16:33:11 +0100918 * .hashfn = jhash,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200919 * .obj_hashfn = my_hash_fn,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200920 * };
921 */
922int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params)
923{
924 struct bucket_table *tbl;
925 size_t size;
926
927 size = HASH_DEFAULT_SIZE;
928
929 if ((params->key_len && !params->hashfn) ||
930 (!params->key_len && !params->obj_hashfn))
931 return -EINVAL;
932
Thomas Graff89bd6f2015-01-02 23:00:21 +0100933 if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT))
934 return -EINVAL;
935
Herbert Xuc2e213c2015-03-18 20:01:16 +1100936 params->min_size = max(params->min_size, HASH_MIN_SIZE);
Ying Xue94000172014-09-03 09:22:36 +0800937
Thomas Graf7e1e7762014-08-02 11:47:44 +0200938 if (params->nelem_hint)
Ying Xue94000172014-09-03 09:22:36 +0800939 size = rounded_hashtable_size(params);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200940
Thomas Graf97defe12015-01-02 23:00:20 +0100941 memset(ht, 0, sizeof(*ht));
942 mutex_init(&ht->mutex);
943 memcpy(&ht->p, params, sizeof(*params));
944
945 if (params->locks_mul)
946 ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
947 else
948 ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
949
Herbert Xu5269b532015-03-14 13:57:22 +1100950 tbl = bucket_table_alloc(ht, size);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200951 if (tbl == NULL)
952 return -ENOMEM;
953
Ying Xue545a1482015-01-07 13:41:57 +0800954 atomic_set(&ht->nelems, 0);
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100955
Thomas Graf7e1e7762014-08-02 11:47:44 +0200956 RCU_INIT_POINTER(ht->tbl, tbl);
957
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100958 INIT_WORK(&ht->run_work, rht_deferred_worker);
Thomas Graf97defe12015-01-02 23:00:20 +0100959
Thomas Graf7e1e7762014-08-02 11:47:44 +0200960 return 0;
961}
962EXPORT_SYMBOL_GPL(rhashtable_init);
963
964/**
965 * rhashtable_destroy - destroy hash table
966 * @ht: the hash table to destroy
967 *
Pablo Neira Ayusoae82ddc2014-09-02 00:26:05 +0200968 * Frees the bucket array. This function is not rcu safe, therefore the caller
969 * has to make sure that no resizing may happen by unpublishing the hashtable
970 * and waiting for the quiescent cycle before releasing the bucket array.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200971 */
Thomas Graf97defe12015-01-02 23:00:20 +0100972void rhashtable_destroy(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200973{
Thomas Graf97defe12015-01-02 23:00:20 +0100974 ht->being_destroyed = true;
975
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100976 cancel_work_sync(&ht->run_work);
Ying Xue57699a42015-01-16 11:13:09 +0800977
Thomas Graf97defe12015-01-02 23:00:20 +0100978 mutex_lock(&ht->mutex);
Thomas Graf97defe12015-01-02 23:00:20 +0100979 bucket_table_free(rht_dereference(ht->tbl, ht));
Thomas Graf97defe12015-01-02 23:00:20 +0100980 mutex_unlock(&ht->mutex);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200981}
982EXPORT_SYMBOL_GPL(rhashtable_destroy);