blob: ba15dceee27fde3f04a8dbf6b6e59db4662549d8 [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{
Thomas Graf8d24c0b2015-01-02 23:00:14 +010066 return hash & (tbl->size - 1);
Thomas Graf7e1e7762014-08-02 11:47:44 +020067}
68
Herbert Xu988dfbd2015-03-10 09:27:55 +110069static u32 obj_raw_hashfn(struct rhashtable *ht, const void *ptr)
Thomas Graf8d24c0b2015-01-02 23:00:14 +010070{
Herbert Xu988dfbd2015-03-10 09:27:55 +110071 struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht);
Thomas Graf8d24c0b2015-01-02 23:00:14 +010072 u32 hash;
73
74 if (unlikely(!ht->p.key_len))
Herbert Xu988dfbd2015-03-10 09:27:55 +110075 hash = ht->p.obj_hashfn(ptr, tbl->hash_rnd);
Thomas Graf8d24c0b2015-01-02 23:00:14 +010076 else
77 hash = ht->p.hashfn(ptr + ht->p.key_offset, ht->p.key_len,
Herbert Xu988dfbd2015-03-10 09:27:55 +110078 tbl->hash_rnd);
Thomas Graf8d24c0b2015-01-02 23:00:14 +010079
Thomas Graff89bd6f2015-01-02 23:00:21 +010080 return hash >> HASH_RESERVED_SPACE;
Thomas Graf8d24c0b2015-01-02 23:00:14 +010081}
82
Thomas Graf97defe12015-01-02 23:00:20 +010083static u32 key_hashfn(struct rhashtable *ht, const void *key, u32 len)
Thomas Graf7e1e7762014-08-02 11:47:44 +020084{
Herbert Xu988dfbd2015-03-10 09:27:55 +110085 struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht);
86
87 return ht->p.hashfn(key, len, tbl->hash_rnd) >> HASH_RESERVED_SPACE;
Thomas Graf7e1e7762014-08-02 11:47:44 +020088}
Thomas Graf7e1e7762014-08-02 11:47:44 +020089
Herbert Xu988dfbd2015-03-10 09:27:55 +110090static u32 head_hashfn(struct rhashtable *ht,
Thomas Graf8d24c0b2015-01-02 23:00:14 +010091 const struct bucket_table *tbl,
92 const struct rhash_head *he)
Thomas Graf7e1e7762014-08-02 11:47:44 +020093{
Thomas Graf8d24c0b2015-01-02 23:00:14 +010094 return rht_bucket_index(tbl, obj_raw_hashfn(ht, rht_obj(ht, he)));
Thomas Graf7e1e7762014-08-02 11:47:44 +020095}
96
Thomas Grafa03eaec2015-02-05 02:03:34 +010097#ifdef CONFIG_PROVE_LOCKING
Herbert Xu988dfbd2015-03-10 09:27:55 +110098static void debug_dump_buckets(struct rhashtable *ht,
Thomas Grafa03eaec2015-02-05 02:03:34 +010099 const struct bucket_table *tbl)
100{
101 struct rhash_head *he;
102 unsigned int i, hash;
103
104 for (i = 0; i < tbl->size; i++) {
105 pr_warn(" [Bucket %d] ", i);
106 rht_for_each_rcu(he, tbl, i) {
107 hash = head_hashfn(ht, tbl, he);
108 pr_cont("[hash = %#x, lock = %p] ",
109 hash, bucket_lock(tbl, hash));
110 }
111 pr_cont("\n");
112 }
113
114}
115
116static void debug_dump_table(struct rhashtable *ht,
117 const struct bucket_table *tbl,
118 unsigned int hash)
119{
120 struct bucket_table *old_tbl, *future_tbl;
121
122 pr_emerg("BUG: lock for hash %#x in table %p not held\n",
123 hash, tbl);
124
125 rcu_read_lock();
126 future_tbl = rht_dereference_rcu(ht->future_tbl, ht);
127 old_tbl = rht_dereference_rcu(ht->tbl, ht);
128 if (future_tbl != old_tbl) {
129 pr_warn("Future table %p (size: %zd)\n",
130 future_tbl, future_tbl->size);
131 debug_dump_buckets(ht, future_tbl);
132 }
133
134 pr_warn("Table %p (size: %zd)\n", old_tbl, old_tbl->size);
135 debug_dump_buckets(ht, old_tbl);
136
137 rcu_read_unlock();
138}
139
140#define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
141#define ASSERT_BUCKET_LOCK(HT, TBL, HASH) \
142 do { \
143 if (unlikely(!lockdep_rht_bucket_is_held(TBL, HASH))) { \
144 debug_dump_table(HT, TBL, HASH); \
145 BUG(); \
146 } \
147 } while (0)
148
149int lockdep_rht_mutex_is_held(struct rhashtable *ht)
150{
151 return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1;
152}
153EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held);
154
155int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash)
156{
157 spinlock_t *lock = bucket_lock(tbl, hash);
158
159 return (debug_locks) ? lockdep_is_held(lock) : 1;
160}
161EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held);
162#else
163#define ASSERT_RHT_MUTEX(HT)
164#define ASSERT_BUCKET_LOCK(HT, TBL, HASH)
165#endif
166
167
Thomas Grafb8e19432015-01-02 23:00:17 +0100168static struct rhash_head __rcu **bucket_tail(struct bucket_table *tbl, u32 n)
169{
170 struct rhash_head __rcu **pprev;
171
172 for (pprev = &tbl->buckets[n];
Thomas Graff89bd6f2015-01-02 23:00:21 +0100173 !rht_is_a_nulls(rht_dereference_bucket(*pprev, tbl, n));
Thomas Grafb8e19432015-01-02 23:00:17 +0100174 pprev = &rht_dereference_bucket(*pprev, tbl, n)->next)
175 ;
176
177 return pprev;
178}
179
Thomas Graf97defe12015-01-02 23:00:20 +0100180static int alloc_bucket_locks(struct rhashtable *ht, struct bucket_table *tbl)
181{
182 unsigned int i, size;
183#if defined(CONFIG_PROVE_LOCKING)
184 unsigned int nr_pcpus = 2;
185#else
186 unsigned int nr_pcpus = num_possible_cpus();
187#endif
188
189 nr_pcpus = min_t(unsigned int, nr_pcpus, 32UL);
190 size = roundup_pow_of_two(nr_pcpus * ht->p.locks_mul);
191
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100192 /* Never allocate more than 0.5 locks per bucket */
193 size = min_t(unsigned int, size, tbl->size >> 1);
Thomas Graf97defe12015-01-02 23:00:20 +0100194
195 if (sizeof(spinlock_t) != 0) {
196#ifdef CONFIG_NUMA
197 if (size * sizeof(spinlock_t) > PAGE_SIZE)
198 tbl->locks = vmalloc(size * sizeof(spinlock_t));
199 else
200#endif
201 tbl->locks = kmalloc_array(size, sizeof(spinlock_t),
202 GFP_KERNEL);
203 if (!tbl->locks)
204 return -ENOMEM;
205 for (i = 0; i < size; i++)
206 spin_lock_init(&tbl->locks[i]);
207 }
208 tbl->locks_mask = size - 1;
209
210 return 0;
211}
212
213static void bucket_table_free(const struct bucket_table *tbl)
214{
215 if (tbl)
216 kvfree(tbl->locks);
217
218 kvfree(tbl);
219}
220
221static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
222 size_t nbuckets)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200223{
Daniel Borkmanneb6d1ab2015-02-20 00:53:38 +0100224 struct bucket_table *tbl = NULL;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200225 size_t size;
Thomas Graff89bd6f2015-01-02 23:00:21 +0100226 int i;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200227
228 size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]);
Daniel Borkmanneb6d1ab2015-02-20 00:53:38 +0100229 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
230 tbl = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200231 if (tbl == NULL)
232 tbl = vzalloc(size);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200233 if (tbl == NULL)
234 return NULL;
235
236 tbl->size = nbuckets;
237
Thomas Graf97defe12015-01-02 23:00:20 +0100238 if (alloc_bucket_locks(ht, tbl) < 0) {
239 bucket_table_free(tbl);
240 return NULL;
241 }
Thomas Graf7e1e7762014-08-02 11:47:44 +0200242
Thomas Graff89bd6f2015-01-02 23:00:21 +0100243 for (i = 0; i < nbuckets; i++)
244 INIT_RHT_NULLS_HEAD(tbl->buckets[i], ht, i);
245
Thomas Graf97defe12015-01-02 23:00:20 +0100246 return tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200247}
248
249/**
250 * rht_grow_above_75 - returns true if nelems > 0.75 * table-size
251 * @ht: hash table
252 * @new_size: new table size
253 */
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100254static bool rht_grow_above_75(const struct rhashtable *ht, size_t new_size)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200255{
256 /* Expand table when exceeding 75% load */
Ying Xuec0c09bf2015-01-07 13:41:56 +0800257 return atomic_read(&ht->nelems) > (new_size / 4 * 3) &&
Daniel Borkmann8331de72015-02-25 16:31:53 +0100258 (!ht->p.max_shift || atomic_read(&ht->shift) < ht->p.max_shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200259}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200260
261/**
262 * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size
263 * @ht: hash table
264 * @new_size: new table size
265 */
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100266static bool rht_shrink_below_30(const struct rhashtable *ht, size_t new_size)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200267{
268 /* Shrink table beneath 30% load */
Ying Xuec0c09bf2015-01-07 13:41:56 +0800269 return atomic_read(&ht->nelems) < (new_size * 3 / 10) &&
270 (atomic_read(&ht->shift) > ht->p.min_shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200271}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200272
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100273static void lock_buckets(struct bucket_table *new_tbl,
274 struct bucket_table *old_tbl, unsigned int hash)
275 __acquires(old_bucket_lock)
276{
277 spin_lock_bh(bucket_lock(old_tbl, hash));
278 if (new_tbl != old_tbl)
279 spin_lock_bh_nested(bucket_lock(new_tbl, hash),
280 RHT_LOCK_NESTED);
281}
282
283static void unlock_buckets(struct bucket_table *new_tbl,
284 struct bucket_table *old_tbl, unsigned int hash)
285 __releases(old_bucket_lock)
286{
287 if (new_tbl != old_tbl)
288 spin_unlock_bh(bucket_lock(new_tbl, hash));
289 spin_unlock_bh(bucket_lock(old_tbl, hash));
290}
291
292/**
293 * Unlink entries on bucket which hash to different bucket.
294 *
295 * Returns true if no more work needs to be performed on the bucket.
296 */
Thomas Grafa03eaec2015-02-05 02:03:34 +0100297static bool hashtable_chain_unzip(struct rhashtable *ht,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200298 const struct bucket_table *new_tbl,
Thomas Graf97defe12015-01-02 23:00:20 +0100299 struct bucket_table *old_tbl,
300 size_t old_hash)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200301{
302 struct rhash_head *he, *p, *next;
Thomas Graf97defe12015-01-02 23:00:20 +0100303 unsigned int new_hash, new_hash2;
304
Thomas Grafa03eaec2015-02-05 02:03:34 +0100305 ASSERT_BUCKET_LOCK(ht, old_tbl, old_hash);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200306
307 /* Old bucket empty, no work needed. */
Thomas Graf97defe12015-01-02 23:00:20 +0100308 p = rht_dereference_bucket(old_tbl->buckets[old_hash], old_tbl,
309 old_hash);
Thomas Graff89bd6f2015-01-02 23:00:21 +0100310 if (rht_is_a_nulls(p))
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100311 return false;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200312
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100313 new_hash = head_hashfn(ht, new_tbl, p);
Thomas Grafa03eaec2015-02-05 02:03:34 +0100314 ASSERT_BUCKET_LOCK(ht, new_tbl, new_hash);
Thomas Graf97defe12015-01-02 23:00:20 +0100315
Thomas Graf7e1e7762014-08-02 11:47:44 +0200316 /* Advance the old bucket pointer one or more times until it
317 * reaches a node that doesn't hash to the same bucket as the
318 * previous node p. Call the previous node p;
319 */
Thomas Graf97defe12015-01-02 23:00:20 +0100320 rht_for_each_continue(he, p->next, old_tbl, old_hash) {
321 new_hash2 = head_hashfn(ht, new_tbl, he);
Thomas Grafa03eaec2015-02-05 02:03:34 +0100322 ASSERT_BUCKET_LOCK(ht, new_tbl, new_hash2);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100323
Thomas Graf97defe12015-01-02 23:00:20 +0100324 if (new_hash != new_hash2)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200325 break;
326 p = he;
327 }
Thomas Graf97defe12015-01-02 23:00:20 +0100328 rcu_assign_pointer(old_tbl->buckets[old_hash], p->next);
329
Thomas Graf7e1e7762014-08-02 11:47:44 +0200330 /* Find the subsequent node which does hash to the same
331 * bucket as node P, or NULL if no such node exists.
332 */
Thomas Graff89bd6f2015-01-02 23:00:21 +0100333 INIT_RHT_NULLS_HEAD(next, ht, old_hash);
334 if (!rht_is_a_nulls(he)) {
Thomas Graf97defe12015-01-02 23:00:20 +0100335 rht_for_each_continue(he, he->next, old_tbl, old_hash) {
336 if (head_hashfn(ht, new_tbl, he) == new_hash) {
Thomas Graf7e1e7762014-08-02 11:47:44 +0200337 next = he;
338 break;
339 }
340 }
341 }
342
343 /* Set p's next pointer to that subsequent node pointer,
344 * bypassing the nodes which do not hash to p's bucket
345 */
Thomas Graf97defe12015-01-02 23:00:20 +0100346 rcu_assign_pointer(p->next, next);
347
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100348 p = rht_dereference_bucket(old_tbl->buckets[old_hash], old_tbl,
349 old_hash);
350
351 return !rht_is_a_nulls(p);
Thomas Graf97defe12015-01-02 23:00:20 +0100352}
353
Thomas Graf7cd10db2015-02-05 02:03:35 +0100354static void link_old_to_new(struct rhashtable *ht, struct bucket_table *new_tbl,
Thomas Graf97defe12015-01-02 23:00:20 +0100355 unsigned int new_hash, struct rhash_head *entry)
356{
Thomas Graf7cd10db2015-02-05 02:03:35 +0100357 ASSERT_BUCKET_LOCK(ht, new_tbl, new_hash);
358
Thomas Graf97defe12015-01-02 23:00:20 +0100359 rcu_assign_pointer(*bucket_tail(new_tbl, new_hash), entry);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200360}
361
362/**
363 * rhashtable_expand - Expand hash table while allowing concurrent lookups
364 * @ht: the hash table to expand
Thomas Graf7e1e7762014-08-02 11:47:44 +0200365 *
366 * A secondary bucket array is allocated and the hash entries are migrated
367 * while keeping them on both lists until the end of the RCU grace period.
368 *
369 * This function may only be called in a context where it is safe to call
370 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
371 *
Thomas Graf97defe12015-01-02 23:00:20 +0100372 * The caller must ensure that no concurrent resizing occurs by holding
373 * ht->mutex.
374 *
375 * It is valid to have concurrent insertions and deletions protected by per
376 * bucket locks or concurrent RCU protected lookups and traversals.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200377 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100378int rhashtable_expand(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200379{
380 struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
381 struct rhash_head *he;
Thomas Graf97defe12015-01-02 23:00:20 +0100382 unsigned int new_hash, old_hash;
383 bool complete = false;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200384
385 ASSERT_RHT_MUTEX(ht);
386
Thomas Graf97defe12015-01-02 23:00:20 +0100387 new_tbl = bucket_table_alloc(ht, old_tbl->size * 2);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200388 if (new_tbl == NULL)
389 return -ENOMEM;
390
Herbert Xu988dfbd2015-03-10 09:27:55 +1100391 new_tbl->hash_rnd = old_tbl->hash_rnd;
392
Ying Xuec0c09bf2015-01-07 13:41:56 +0800393 atomic_inc(&ht->shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200394
Thomas Graf97defe12015-01-02 23:00:20 +0100395 /* Make insertions go into the new, empty table right away. Deletions
396 * and lookups will be attempted in both tables until we synchronize.
397 * The synchronize_rcu() guarantees for the new table to be picked up
398 * so no new additions go into the old table while we relink.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200399 */
Thomas Graf97defe12015-01-02 23:00:20 +0100400 rcu_assign_pointer(ht->future_tbl, new_tbl);
401 synchronize_rcu();
402
403 /* For each new bucket, search the corresponding old bucket for the
404 * first entry that hashes to the new bucket, and link the end of
405 * newly formed bucket chain (containing entries added to future
406 * table) to that entry. Since all the entries which will end up in
407 * the new bucket appear in the same old bucket, this constructs an
408 * entirely valid new hash table, but with multiple buckets
409 * "zipped" together into a single imprecise chain.
410 */
411 for (new_hash = 0; new_hash < new_tbl->size; new_hash++) {
412 old_hash = rht_bucket_index(old_tbl, new_hash);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100413 lock_buckets(new_tbl, old_tbl, new_hash);
Thomas Graf97defe12015-01-02 23:00:20 +0100414 rht_for_each(he, old_tbl, old_hash) {
415 if (head_hashfn(ht, new_tbl, he) == new_hash) {
Thomas Graf7cd10db2015-02-05 02:03:35 +0100416 link_old_to_new(ht, new_tbl, new_hash, he);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200417 break;
418 }
419 }
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100420 unlock_buckets(new_tbl, old_tbl, new_hash);
Eric Dumazet5beb5c92015-02-26 07:20:34 -0800421 cond_resched();
Thomas Graf7e1e7762014-08-02 11:47:44 +0200422 }
423
Thomas Graf7e1e7762014-08-02 11:47:44 +0200424 /* Unzip interleaved hash chains */
Thomas Graf97defe12015-01-02 23:00:20 +0100425 while (!complete && !ht->being_destroyed) {
Thomas Graf7e1e7762014-08-02 11:47:44 +0200426 /* Wait for readers. All new readers will see the new
427 * table, and thus no references to the old table will
428 * remain.
429 */
430 synchronize_rcu();
431
432 /* For each bucket in the old table (each of which
433 * contains items from multiple buckets of the new
434 * table): ...
435 */
436 complete = true;
Thomas Graf97defe12015-01-02 23:00:20 +0100437 for (old_hash = 0; old_hash < old_tbl->size; old_hash++) {
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100438 lock_buckets(new_tbl, old_tbl, old_hash);
Thomas Graff89bd6f2015-01-02 23:00:21 +0100439
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100440 if (hashtable_chain_unzip(ht, new_tbl, old_tbl,
441 old_hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200442 complete = false;
Thomas Graf97defe12015-01-02 23:00:20 +0100443
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100444 unlock_buckets(new_tbl, old_tbl, old_hash);
Eric Dumazet5beb5c92015-02-26 07:20:34 -0800445 cond_resched();
Thomas Graf7e1e7762014-08-02 11:47:44 +0200446 }
Thomas Graf97defe12015-01-02 23:00:20 +0100447 }
Thomas Graf7e1e7762014-08-02 11:47:44 +0200448
Thomas Grafcf52d522015-02-05 02:03:36 +0100449 rcu_assign_pointer(ht->tbl, new_tbl);
Thomas Graf2af4b522015-02-05 02:03:33 +0100450 synchronize_rcu();
451
Thomas Graf7e1e7762014-08-02 11:47:44 +0200452 bucket_table_free(old_tbl);
453 return 0;
454}
455EXPORT_SYMBOL_GPL(rhashtable_expand);
456
457/**
458 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
459 * @ht: the hash table to shrink
Thomas Graf7e1e7762014-08-02 11:47:44 +0200460 *
461 * This function may only be called in a context where it is safe to call
462 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
463 *
Thomas Graf97defe12015-01-02 23:00:20 +0100464 * The caller must ensure that no concurrent resizing occurs by holding
465 * ht->mutex.
466 *
Thomas Graf7e1e7762014-08-02 11:47:44 +0200467 * The caller must ensure that no concurrent table mutations take place.
468 * It is however valid to have concurrent lookups if they are RCU protected.
Thomas Graf97defe12015-01-02 23:00:20 +0100469 *
470 * It is valid to have concurrent insertions and deletions protected by per
471 * bucket locks or concurrent RCU protected lookups and traversals.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200472 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100473int rhashtable_shrink(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200474{
Thomas Graf97defe12015-01-02 23:00:20 +0100475 struct bucket_table *new_tbl, *tbl = rht_dereference(ht->tbl, ht);
Thomas Graf97defe12015-01-02 23:00:20 +0100476 unsigned int new_hash;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200477
478 ASSERT_RHT_MUTEX(ht);
479
Thomas Graf97defe12015-01-02 23:00:20 +0100480 new_tbl = bucket_table_alloc(ht, tbl->size / 2);
481 if (new_tbl == NULL)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200482 return -ENOMEM;
483
Herbert Xu988dfbd2015-03-10 09:27:55 +1100484 new_tbl->hash_rnd = tbl->hash_rnd;
485
Thomas Graf97defe12015-01-02 23:00:20 +0100486 rcu_assign_pointer(ht->future_tbl, new_tbl);
487 synchronize_rcu();
Thomas Graf7e1e7762014-08-02 11:47:44 +0200488
Thomas Graf97defe12015-01-02 23:00:20 +0100489 /* Link the first entry in the old bucket to the end of the
490 * bucket in the new table. As entries are concurrently being
491 * added to the new table, lock down the new bucket. As we
492 * always divide the size in half when shrinking, each bucket
493 * in the new table maps to exactly two buckets in the old
494 * table.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200495 */
Thomas Graf97defe12015-01-02 23:00:20 +0100496 for (new_hash = 0; new_hash < new_tbl->size; new_hash++) {
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100497 lock_buckets(new_tbl, tbl, new_hash);
Thomas Graf97defe12015-01-02 23:00:20 +0100498
499 rcu_assign_pointer(*bucket_tail(new_tbl, new_hash),
500 tbl->buckets[new_hash]);
Thomas Graf7cd10db2015-02-05 02:03:35 +0100501 ASSERT_BUCKET_LOCK(ht, tbl, new_hash + new_tbl->size);
Thomas Graf97defe12015-01-02 23:00:20 +0100502 rcu_assign_pointer(*bucket_tail(new_tbl, new_hash),
503 tbl->buckets[new_hash + new_tbl->size]);
504
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100505 unlock_buckets(new_tbl, tbl, new_hash);
Eric Dumazet5beb5c92015-02-26 07:20:34 -0800506 cond_resched();
Thomas Graf7e1e7762014-08-02 11:47:44 +0200507 }
508
509 /* Publish the new, valid hash table */
Thomas Graf97defe12015-01-02 23:00:20 +0100510 rcu_assign_pointer(ht->tbl, new_tbl);
Ying Xuec0c09bf2015-01-07 13:41:56 +0800511 atomic_dec(&ht->shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200512
513 /* Wait for readers. No new readers will have references to the
514 * old hash table.
515 */
516 synchronize_rcu();
517
518 bucket_table_free(tbl);
519
520 return 0;
521}
522EXPORT_SYMBOL_GPL(rhashtable_shrink);
523
Thomas Graf97defe12015-01-02 23:00:20 +0100524static void rht_deferred_worker(struct work_struct *work)
525{
526 struct rhashtable *ht;
527 struct bucket_table *tbl;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100528 struct rhashtable_walker *walker;
Thomas Graf97defe12015-01-02 23:00:20 +0100529
Ying Xue57699a42015-01-16 11:13:09 +0800530 ht = container_of(work, struct rhashtable, run_work);
Thomas Graf97defe12015-01-02 23:00:20 +0100531 mutex_lock(&ht->mutex);
Herbert Xu28134a52015-02-04 07:33:22 +1100532 if (ht->being_destroyed)
533 goto unlock;
534
Thomas Graf97defe12015-01-02 23:00:20 +0100535 tbl = rht_dereference(ht->tbl, ht);
536
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100537 list_for_each_entry(walker, &ht->walkers, list)
538 walker->resize = true;
539
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100540 if (rht_grow_above_75(ht, tbl->size))
Thomas Graf97defe12015-01-02 23:00:20 +0100541 rhashtable_expand(ht);
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100542 else if (rht_shrink_below_30(ht, tbl->size))
Thomas Graf97defe12015-01-02 23:00:20 +0100543 rhashtable_shrink(ht);
Herbert Xu28134a52015-02-04 07:33:22 +1100544unlock:
Thomas Graf97defe12015-01-02 23:00:20 +0100545 mutex_unlock(&ht->mutex);
546}
547
Ying Xuedb304852015-01-07 13:41:54 +0800548static void __rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj,
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100549 struct bucket_table *tbl,
550 const struct bucket_table *old_tbl, u32 hash)
Ying Xuedb304852015-01-07 13:41:54 +0800551{
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100552 bool no_resize_running = tbl == old_tbl;
Thomas Graf020219a2015-02-06 16:08:43 +0000553 struct rhash_head *head;
554
555 hash = rht_bucket_index(tbl, hash);
556 head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash);
Ying Xuedb304852015-01-07 13:41:54 +0800557
Thomas Graf7cd10db2015-02-05 02:03:35 +0100558 ASSERT_BUCKET_LOCK(ht, tbl, hash);
559
Ying Xuedb304852015-01-07 13:41:54 +0800560 if (rht_is_a_nulls(head))
561 INIT_RHT_NULLS_HEAD(obj->next, ht, hash);
562 else
563 RCU_INIT_POINTER(obj->next, head);
564
565 rcu_assign_pointer(tbl->buckets[hash], obj);
566
567 atomic_inc(&ht->nelems);
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100568 if (no_resize_running && rht_grow_above_75(ht, tbl->size))
569 schedule_work(&ht->run_work);
Ying Xuedb304852015-01-07 13:41:54 +0800570}
571
Thomas Graf7e1e7762014-08-02 11:47:44 +0200572/**
Ying Xuedb304852015-01-07 13:41:54 +0800573 * rhashtable_insert - insert object into hash table
Thomas Graf7e1e7762014-08-02 11:47:44 +0200574 * @ht: hash table
575 * @obj: pointer to hash head inside object
Thomas Graf7e1e7762014-08-02 11:47:44 +0200576 *
Thomas Graf97defe12015-01-02 23:00:20 +0100577 * Will take a per bucket spinlock to protect against mutual mutations
578 * on the same bucket. Multiple insertions may occur in parallel unless
579 * they map to the same bucket lock.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200580 *
Thomas Graf97defe12015-01-02 23:00:20 +0100581 * It is safe to call this function from atomic context.
582 *
583 * Will trigger an automatic deferred table resizing if the size grows
584 * beyond the watermark indicated by grow_decision() which can be passed
585 * to rhashtable_init().
Thomas Graf7e1e7762014-08-02 11:47:44 +0200586 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100587void rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200588{
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100589 struct bucket_table *tbl, *old_tbl;
Thomas Graf97defe12015-01-02 23:00:20 +0100590 unsigned hash;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200591
Thomas Graf97defe12015-01-02 23:00:20 +0100592 rcu_read_lock();
Thomas Graf7e1e7762014-08-02 11:47:44 +0200593
Thomas Graf97defe12015-01-02 23:00:20 +0100594 tbl = rht_dereference_rcu(ht->future_tbl, ht);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100595 old_tbl = rht_dereference_rcu(ht->tbl, ht);
Thomas Graf020219a2015-02-06 16:08:43 +0000596 hash = obj_raw_hashfn(ht, rht_obj(ht, obj));
Thomas Graf97defe12015-01-02 23:00:20 +0100597
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100598 lock_buckets(tbl, old_tbl, hash);
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100599 __rhashtable_insert(ht, obj, tbl, old_tbl, hash);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100600 unlock_buckets(tbl, old_tbl, hash);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200601
Thomas Graf97defe12015-01-02 23:00:20 +0100602 rcu_read_unlock();
Thomas Graf7e1e7762014-08-02 11:47:44 +0200603}
604EXPORT_SYMBOL_GPL(rhashtable_insert);
605
606/**
Thomas Graf7e1e7762014-08-02 11:47:44 +0200607 * rhashtable_remove - remove object from hash table
608 * @ht: hash table
609 * @obj: pointer to hash head inside object
Thomas Graf7e1e7762014-08-02 11:47:44 +0200610 *
611 * Since the hash chain is single linked, the removal operation needs to
612 * walk the bucket chain upon removal. The removal operation is thus
613 * considerable slow if the hash table is not correctly sized.
614 *
Ying Xuedb304852015-01-07 13:41:54 +0800615 * Will automatically shrink the table via rhashtable_expand() if the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200616 * shrink_decision function specified at rhashtable_init() returns true.
617 *
618 * The caller must ensure that no concurrent table mutations occur. It is
619 * however valid to have concurrent lookups if they are RCU protected.
620 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100621bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *obj)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200622{
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100623 struct bucket_table *tbl, *new_tbl, *old_tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200624 struct rhash_head __rcu **pprev;
Thomas Grafcf52d522015-02-05 02:03:36 +0100625 struct rhash_head *he, *he2;
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100626 unsigned int hash, new_hash;
Thomas Graffe6a0432015-01-21 11:54:01 +0000627 bool ret = false;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200628
Thomas Graf97defe12015-01-02 23:00:20 +0100629 rcu_read_lock();
Thomas Graf020219a2015-02-06 16:08:43 +0000630 old_tbl = rht_dereference_rcu(ht->tbl, ht);
631 tbl = new_tbl = rht_dereference_rcu(ht->future_tbl, ht);
Thomas Grafcf52d522015-02-05 02:03:36 +0100632 new_hash = obj_raw_hashfn(ht, rht_obj(ht, obj));
Thomas Graf7e1e7762014-08-02 11:47:44 +0200633
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100634 lock_buckets(new_tbl, old_tbl, new_hash);
Thomas Graf97defe12015-01-02 23:00:20 +0100635restart:
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100636 hash = rht_bucket_index(tbl, new_hash);
Thomas Graf97defe12015-01-02 23:00:20 +0100637 pprev = &tbl->buckets[hash];
638 rht_for_each(he, tbl, hash) {
Thomas Graf7e1e7762014-08-02 11:47:44 +0200639 if (he != obj) {
640 pprev = &he->next;
641 continue;
642 }
643
Thomas Graf7cd10db2015-02-05 02:03:35 +0100644 ASSERT_BUCKET_LOCK(ht, tbl, hash);
Thomas Graf897362e2015-01-02 23:00:18 +0100645
Thomas Graf020219a2015-02-06 16:08:43 +0000646 if (old_tbl->size > new_tbl->size && tbl == old_tbl &&
647 !rht_is_a_nulls(obj->next) &&
648 head_hashfn(ht, tbl, obj->next) != hash) {
649 rcu_assign_pointer(*pprev, (struct rhash_head *) rht_marker(ht, hash));
650 } else if (unlikely(old_tbl->size < new_tbl->size && tbl == new_tbl)) {
651 rht_for_each_continue(he2, obj->next, tbl, hash) {
Thomas Grafcf52d522015-02-05 02:03:36 +0100652 if (head_hashfn(ht, tbl, he2) == hash) {
653 rcu_assign_pointer(*pprev, he2);
654 goto found;
655 }
656 }
657
Thomas Graf020219a2015-02-06 16:08:43 +0000658 rcu_assign_pointer(*pprev, (struct rhash_head *) rht_marker(ht, hash));
Thomas Grafcf52d522015-02-05 02:03:36 +0100659 } else {
660 rcu_assign_pointer(*pprev, obj->next);
661 }
662
663found:
Thomas Graffe6a0432015-01-21 11:54:01 +0000664 ret = true;
665 break;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200666 }
667
Thomas Graffe6a0432015-01-21 11:54:01 +0000668 /* The entry may be linked in either 'tbl', 'future_tbl', or both.
669 * 'future_tbl' only exists for a short period of time during
670 * resizing. Thus traversing both is fine and the added cost is
671 * very rare.
672 */
Thomas Graf020219a2015-02-06 16:08:43 +0000673 if (tbl != old_tbl) {
674 tbl = old_tbl;
Thomas Graf97defe12015-01-02 23:00:20 +0100675 goto restart;
676 }
677
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100678 unlock_buckets(new_tbl, old_tbl, new_hash);
Thomas Graffe6a0432015-01-21 11:54:01 +0000679
680 if (ret) {
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100681 bool no_resize_running = new_tbl == old_tbl;
682
Thomas Graffe6a0432015-01-21 11:54:01 +0000683 atomic_dec(&ht->nelems);
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100684 if (no_resize_running && rht_shrink_below_30(ht, new_tbl->size))
685 schedule_work(&ht->run_work);
Thomas Graffe6a0432015-01-21 11:54:01 +0000686 }
687
Thomas Graf97defe12015-01-02 23:00:20 +0100688 rcu_read_unlock();
689
Thomas Graffe6a0432015-01-21 11:54:01 +0000690 return ret;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200691}
692EXPORT_SYMBOL_GPL(rhashtable_remove);
693
Ying Xueefb975a62015-01-07 13:41:52 +0800694struct rhashtable_compare_arg {
695 struct rhashtable *ht;
696 const void *key;
697};
698
699static bool rhashtable_compare(void *ptr, void *arg)
700{
701 struct rhashtable_compare_arg *x = arg;
702 struct rhashtable *ht = x->ht;
703
704 return !memcmp(ptr + ht->p.key_offset, x->key, ht->p.key_len);
705}
706
Thomas Graf7e1e7762014-08-02 11:47:44 +0200707/**
708 * rhashtable_lookup - lookup key in hash table
709 * @ht: hash table
710 * @key: pointer to key
711 *
712 * Computes the hash value for the key and traverses the bucket chain looking
713 * for a entry with an identical key. The first matching entry is returned.
714 *
715 * This lookup function may only be used for fixed key hash table (key_len
Ying Xuedb304852015-01-07 13:41:54 +0800716 * parameter set). It will BUG() if used inappropriately.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200717 *
Thomas Graf97defe12015-01-02 23:00:20 +0100718 * Lookups may occur in parallel with hashtable mutations and resizing.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200719 */
Thomas Graf97defe12015-01-02 23:00:20 +0100720void *rhashtable_lookup(struct rhashtable *ht, const void *key)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200721{
Ying Xueefb975a62015-01-07 13:41:52 +0800722 struct rhashtable_compare_arg arg = {
723 .ht = ht,
724 .key = key,
725 };
Thomas Graf7e1e7762014-08-02 11:47:44 +0200726
727 BUG_ON(!ht->p.key_len);
728
Ying Xueefb975a62015-01-07 13:41:52 +0800729 return rhashtable_lookup_compare(ht, key, &rhashtable_compare, &arg);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200730}
731EXPORT_SYMBOL_GPL(rhashtable_lookup);
732
733/**
734 * rhashtable_lookup_compare - search hash table with compare function
735 * @ht: hash table
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100736 * @key: the pointer to the key
Thomas Graf7e1e7762014-08-02 11:47:44 +0200737 * @compare: compare function, must return true on match
738 * @arg: argument passed on to compare function
739 *
740 * Traverses the bucket chain behind the provided hash value and calls the
741 * specified compare function for each entry.
742 *
Thomas Graf97defe12015-01-02 23:00:20 +0100743 * Lookups may occur in parallel with hashtable mutations and resizing.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200744 *
745 * Returns the first entry on which the compare function returned true.
746 */
Thomas Graf97defe12015-01-02 23:00:20 +0100747void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200748 bool (*compare)(void *, void *), void *arg)
749{
Thomas Graf97defe12015-01-02 23:00:20 +0100750 const struct bucket_table *tbl, *old_tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200751 struct rhash_head *he;
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100752 u32 hash;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200753
Thomas Graf97defe12015-01-02 23:00:20 +0100754 rcu_read_lock();
755
756 old_tbl = rht_dereference_rcu(ht->tbl, ht);
757 tbl = rht_dereference_rcu(ht->future_tbl, ht);
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100758 hash = key_hashfn(ht, key, ht->p.key_len);
Thomas Graf97defe12015-01-02 23:00:20 +0100759restart:
760 rht_for_each_rcu(he, tbl, rht_bucket_index(tbl, hash)) {
Thomas Graf7e1e7762014-08-02 11:47:44 +0200761 if (!compare(rht_obj(ht, he), arg))
762 continue;
Thomas Graf97defe12015-01-02 23:00:20 +0100763 rcu_read_unlock();
Thomas Grafa4b18cd2015-01-02 23:00:15 +0100764 return rht_obj(ht, he);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200765 }
766
Thomas Graf97defe12015-01-02 23:00:20 +0100767 if (unlikely(tbl != old_tbl)) {
768 tbl = old_tbl;
769 goto restart;
770 }
771 rcu_read_unlock();
772
Thomas Graf7e1e7762014-08-02 11:47:44 +0200773 return NULL;
774}
775EXPORT_SYMBOL_GPL(rhashtable_lookup_compare);
776
Ying Xuedb304852015-01-07 13:41:54 +0800777/**
778 * rhashtable_lookup_insert - lookup and insert object into hash table
779 * @ht: hash table
780 * @obj: pointer to hash head inside object
781 *
782 * Locks down the bucket chain in both the old and new table if a resize
783 * is in progress to ensure that writers can't remove from the old table
784 * and can't insert to the new table during the atomic operation of search
785 * and insertion. Searches for duplicates in both the old and new table if
786 * a resize is in progress.
787 *
788 * This lookup function may only be used for fixed key hash table (key_len
789 * parameter set). It will BUG() if used inappropriately.
790 *
791 * It is safe to call this function from atomic context.
792 *
793 * Will trigger an automatic deferred table resizing if the size grows
794 * beyond the watermark indicated by grow_decision() which can be passed
795 * to rhashtable_init().
796 */
797bool rhashtable_lookup_insert(struct rhashtable *ht, struct rhash_head *obj)
798{
Ying Xue7a868d12015-01-12 14:52:22 +0800799 struct rhashtable_compare_arg arg = {
800 .ht = ht,
801 .key = rht_obj(ht, obj) + ht->p.key_offset,
802 };
803
804 BUG_ON(!ht->p.key_len);
805
806 return rhashtable_lookup_compare_insert(ht, obj, &rhashtable_compare,
807 &arg);
808}
809EXPORT_SYMBOL_GPL(rhashtable_lookup_insert);
810
811/**
812 * rhashtable_lookup_compare_insert - search and insert object to hash table
813 * with compare function
814 * @ht: hash table
815 * @obj: pointer to hash head inside object
816 * @compare: compare function, must return true on match
817 * @arg: argument passed on to compare function
818 *
819 * Locks down the bucket chain in both the old and new table if a resize
820 * is in progress to ensure that writers can't remove from the old table
821 * and can't insert to the new table during the atomic operation of search
822 * and insertion. Searches for duplicates in both the old and new table if
823 * a resize is in progress.
824 *
825 * Lookups may occur in parallel with hashtable mutations and resizing.
826 *
827 * Will trigger an automatic deferred table resizing if the size grows
828 * beyond the watermark indicated by grow_decision() which can be passed
829 * to rhashtable_init().
830 */
831bool rhashtable_lookup_compare_insert(struct rhashtable *ht,
832 struct rhash_head *obj,
833 bool (*compare)(void *, void *),
834 void *arg)
835{
Ying Xuedb304852015-01-07 13:41:54 +0800836 struct bucket_table *new_tbl, *old_tbl;
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100837 u32 new_hash;
Ying Xuedb304852015-01-07 13:41:54 +0800838 bool success = true;
839
840 BUG_ON(!ht->p.key_len);
841
842 rcu_read_lock();
Ying Xuedb304852015-01-07 13:41:54 +0800843 old_tbl = rht_dereference_rcu(ht->tbl, ht);
Ying Xuedb304852015-01-07 13:41:54 +0800844 new_tbl = rht_dereference_rcu(ht->future_tbl, ht);
Thomas Graf020219a2015-02-06 16:08:43 +0000845 new_hash = obj_raw_hashfn(ht, rht_obj(ht, obj));
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100846
847 lock_buckets(new_tbl, old_tbl, new_hash);
Ying Xuedb304852015-01-07 13:41:54 +0800848
Ying Xue7a868d12015-01-12 14:52:22 +0800849 if (rhashtable_lookup_compare(ht, rht_obj(ht, obj) + ht->p.key_offset,
850 compare, arg)) {
Ying Xuedb304852015-01-07 13:41:54 +0800851 success = false;
852 goto exit;
853 }
854
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100855 __rhashtable_insert(ht, obj, new_tbl, old_tbl, new_hash);
Ying Xuedb304852015-01-07 13:41:54 +0800856
857exit:
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100858 unlock_buckets(new_tbl, old_tbl, new_hash);
Ying Xuedb304852015-01-07 13:41:54 +0800859 rcu_read_unlock();
860
861 return success;
862}
Ying Xue7a868d12015-01-12 14:52:22 +0800863EXPORT_SYMBOL_GPL(rhashtable_lookup_compare_insert);
Ying Xuedb304852015-01-07 13:41:54 +0800864
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100865/**
866 * rhashtable_walk_init - Initialise an iterator
867 * @ht: Table to walk over
868 * @iter: Hash table Iterator
869 *
870 * This function prepares a hash table walk.
871 *
872 * Note that if you restart a walk after rhashtable_walk_stop you
873 * may see the same object twice. Also, you may miss objects if
874 * there are removals in between rhashtable_walk_stop and the next
875 * call to rhashtable_walk_start.
876 *
877 * For a completely stable walk you should construct your own data
878 * structure outside the hash table.
879 *
880 * This function may sleep so you must not call it from interrupt
881 * context or with spin locks held.
882 *
883 * You must call rhashtable_walk_exit if this function returns
884 * successfully.
885 */
886int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter)
887{
888 iter->ht = ht;
889 iter->p = NULL;
890 iter->slot = 0;
891 iter->skip = 0;
892
893 iter->walker = kmalloc(sizeof(*iter->walker), GFP_KERNEL);
894 if (!iter->walker)
895 return -ENOMEM;
896
Sasha Levin71bb0012015-02-23 04:35:06 -0500897 INIT_LIST_HEAD(&iter->walker->list);
898 iter->walker->resize = false;
899
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100900 mutex_lock(&ht->mutex);
901 list_add(&iter->walker->list, &ht->walkers);
902 mutex_unlock(&ht->mutex);
903
904 return 0;
905}
906EXPORT_SYMBOL_GPL(rhashtable_walk_init);
907
908/**
909 * rhashtable_walk_exit - Free an iterator
910 * @iter: Hash table Iterator
911 *
912 * This function frees resources allocated by rhashtable_walk_init.
913 */
914void rhashtable_walk_exit(struct rhashtable_iter *iter)
915{
916 mutex_lock(&iter->ht->mutex);
917 list_del(&iter->walker->list);
918 mutex_unlock(&iter->ht->mutex);
919 kfree(iter->walker);
920}
921EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
922
923/**
924 * rhashtable_walk_start - Start a hash table walk
925 * @iter: Hash table iterator
926 *
927 * Start a hash table walk. Note that we take the RCU lock in all
928 * cases including when we return an error. So you must always call
929 * rhashtable_walk_stop to clean up.
930 *
931 * Returns zero if successful.
932 *
933 * Returns -EAGAIN if resize event occured. Note that the iterator
934 * will rewind back to the beginning and you may use it immediately
935 * by calling rhashtable_walk_next.
936 */
937int rhashtable_walk_start(struct rhashtable_iter *iter)
938{
939 rcu_read_lock();
940
941 if (iter->walker->resize) {
942 iter->slot = 0;
943 iter->skip = 0;
944 iter->walker->resize = false;
945 return -EAGAIN;
946 }
947
948 return 0;
949}
950EXPORT_SYMBOL_GPL(rhashtable_walk_start);
951
952/**
953 * rhashtable_walk_next - Return the next object and advance the iterator
954 * @iter: Hash table iterator
955 *
956 * Note that you must call rhashtable_walk_stop when you are finished
957 * with the walk.
958 *
959 * Returns the next object or NULL when the end of the table is reached.
960 *
961 * Returns -EAGAIN if resize event occured. Note that the iterator
962 * will rewind back to the beginning and you may continue to use it.
963 */
964void *rhashtable_walk_next(struct rhashtable_iter *iter)
965{
966 const struct bucket_table *tbl;
967 struct rhashtable *ht = iter->ht;
968 struct rhash_head *p = iter->p;
969 void *obj = NULL;
970
971 tbl = rht_dereference_rcu(ht->tbl, ht);
972
973 if (p) {
974 p = rht_dereference_bucket_rcu(p->next, tbl, iter->slot);
975 goto next;
976 }
977
978 for (; iter->slot < tbl->size; iter->slot++) {
979 int skip = iter->skip;
980
981 rht_for_each_rcu(p, tbl, iter->slot) {
982 if (!skip)
983 break;
984 skip--;
985 }
986
987next:
988 if (!rht_is_a_nulls(p)) {
989 iter->skip++;
990 iter->p = p;
991 obj = rht_obj(ht, p);
992 goto out;
993 }
994
995 iter->skip = 0;
996 }
997
998 iter->p = NULL;
999
1000out:
1001 if (iter->walker->resize) {
1002 iter->p = NULL;
1003 iter->slot = 0;
1004 iter->skip = 0;
1005 iter->walker->resize = false;
1006 return ERR_PTR(-EAGAIN);
1007 }
1008
1009 return obj;
1010}
1011EXPORT_SYMBOL_GPL(rhashtable_walk_next);
1012
1013/**
1014 * rhashtable_walk_stop - Finish a hash table walk
1015 * @iter: Hash table iterator
1016 *
1017 * Finish a hash table walk.
1018 */
1019void rhashtable_walk_stop(struct rhashtable_iter *iter)
1020{
1021 rcu_read_unlock();
1022 iter->p = NULL;
1023}
1024EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
1025
Ying Xue94000172014-09-03 09:22:36 +08001026static size_t rounded_hashtable_size(struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +02001027{
Ying Xue94000172014-09-03 09:22:36 +08001028 return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
1029 1UL << params->min_shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +02001030}
1031
1032/**
1033 * rhashtable_init - initialize a new hash table
1034 * @ht: hash table to be initialized
1035 * @params: configuration parameters
1036 *
1037 * Initializes a new hash table based on the provided configuration
1038 * parameters. A table can be configured either with a variable or
1039 * fixed length key:
1040 *
1041 * Configuration Example 1: Fixed length keys
1042 * struct test_obj {
1043 * int key;
1044 * void * my_member;
1045 * struct rhash_head node;
1046 * };
1047 *
1048 * struct rhashtable_params params = {
1049 * .head_offset = offsetof(struct test_obj, node),
1050 * .key_offset = offsetof(struct test_obj, key),
1051 * .key_len = sizeof(int),
Daniel Borkmann87545892014-12-10 16:33:11 +01001052 * .hashfn = jhash,
Thomas Graff89bd6f2015-01-02 23:00:21 +01001053 * .nulls_base = (1U << RHT_BASE_SHIFT),
Thomas Graf7e1e7762014-08-02 11:47:44 +02001054 * };
1055 *
1056 * Configuration Example 2: Variable length keys
1057 * struct test_obj {
1058 * [...]
1059 * struct rhash_head node;
1060 * };
1061 *
1062 * u32 my_hash_fn(const void *data, u32 seed)
1063 * {
1064 * struct test_obj *obj = data;
1065 *
1066 * return [... hash ...];
1067 * }
1068 *
1069 * struct rhashtable_params params = {
1070 * .head_offset = offsetof(struct test_obj, node),
Daniel Borkmann87545892014-12-10 16:33:11 +01001071 * .hashfn = jhash,
Thomas Graf7e1e7762014-08-02 11:47:44 +02001072 * .obj_hashfn = my_hash_fn,
Thomas Graf7e1e7762014-08-02 11:47:44 +02001073 * };
1074 */
1075int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params)
1076{
1077 struct bucket_table *tbl;
1078 size_t size;
1079
1080 size = HASH_DEFAULT_SIZE;
1081
1082 if ((params->key_len && !params->hashfn) ||
1083 (!params->key_len && !params->obj_hashfn))
1084 return -EINVAL;
1085
Thomas Graff89bd6f2015-01-02 23:00:21 +01001086 if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT))
1087 return -EINVAL;
1088
Ying Xue94000172014-09-03 09:22:36 +08001089 params->min_shift = max_t(size_t, params->min_shift,
1090 ilog2(HASH_MIN_SIZE));
1091
Thomas Graf7e1e7762014-08-02 11:47:44 +02001092 if (params->nelem_hint)
Ying Xue94000172014-09-03 09:22:36 +08001093 size = rounded_hashtable_size(params);
Thomas Graf7e1e7762014-08-02 11:47:44 +02001094
Thomas Graf97defe12015-01-02 23:00:20 +01001095 memset(ht, 0, sizeof(*ht));
1096 mutex_init(&ht->mutex);
1097 memcpy(&ht->p, params, sizeof(*params));
Herbert Xuf2dba9c2015-02-04 07:33:23 +11001098 INIT_LIST_HEAD(&ht->walkers);
Thomas Graf97defe12015-01-02 23:00:20 +01001099
1100 if (params->locks_mul)
1101 ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
1102 else
1103 ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
1104
1105 tbl = bucket_table_alloc(ht, size);
Thomas Graf7e1e7762014-08-02 11:47:44 +02001106 if (tbl == NULL)
1107 return -ENOMEM;
1108
Herbert Xu988dfbd2015-03-10 09:27:55 +11001109 get_random_bytes(&tbl->hash_rnd, sizeof(tbl->hash_rnd));
1110
Ying Xue545a1482015-01-07 13:41:57 +08001111 atomic_set(&ht->nelems, 0);
Ying Xuec0c09bf2015-01-07 13:41:56 +08001112 atomic_set(&ht->shift, ilog2(tbl->size));
Thomas Graf7e1e7762014-08-02 11:47:44 +02001113 RCU_INIT_POINTER(ht->tbl, tbl);
Thomas Graf97defe12015-01-02 23:00:20 +01001114 RCU_INIT_POINTER(ht->future_tbl, tbl);
Thomas Graf7e1e7762014-08-02 11:47:44 +02001115
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +01001116 INIT_WORK(&ht->run_work, rht_deferred_worker);
Thomas Graf97defe12015-01-02 23:00:20 +01001117
Thomas Graf7e1e7762014-08-02 11:47:44 +02001118 return 0;
1119}
1120EXPORT_SYMBOL_GPL(rhashtable_init);
1121
1122/**
1123 * rhashtable_destroy - destroy hash table
1124 * @ht: the hash table to destroy
1125 *
Pablo Neira Ayusoae82ddc2014-09-02 00:26:05 +02001126 * Frees the bucket array. This function is not rcu safe, therefore the caller
1127 * has to make sure that no resizing may happen by unpublishing the hashtable
1128 * and waiting for the quiescent cycle before releasing the bucket array.
Thomas Graf7e1e7762014-08-02 11:47:44 +02001129 */
Thomas Graf97defe12015-01-02 23:00:20 +01001130void rhashtable_destroy(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +02001131{
Thomas Graf97defe12015-01-02 23:00:20 +01001132 ht->being_destroyed = true;
1133
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +01001134 cancel_work_sync(&ht->run_work);
Ying Xue57699a42015-01-16 11:13:09 +08001135
Thomas Graf97defe12015-01-02 23:00:20 +01001136 mutex_lock(&ht->mutex);
Thomas Graf97defe12015-01-02 23:00:20 +01001137 bucket_table_free(rht_dereference(ht->tbl, ht));
Thomas Graf97defe12015-01-02 23:00:20 +01001138 mutex_unlock(&ht->mutex);
Thomas Graf7e1e7762014-08-02 11:47:44 +02001139}
1140EXPORT_SYMBOL_GPL(rhashtable_destroy);