blob: 5919d63f58e4a4c4bb555044a2c66c4a60f1bf52 [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>
20#include <linux/slab.h>
21#include <linux/vmalloc.h>
22#include <linux/mm.h>
Daniel Borkmann87545892014-12-10 16:33:11 +010023#include <linux/jhash.h>
Thomas Graf7e1e7762014-08-02 11:47:44 +020024#include <linux/random.h>
25#include <linux/rhashtable.h>
Thomas Graf7e1e7762014-08-02 11:47:44 +020026
27#define HASH_DEFAULT_SIZE 64UL
28#define HASH_MIN_SIZE 4UL
Thomas Graf97defe12015-01-02 23:00:20 +010029#define BUCKET_LOCKS_PER_CPU 128UL
30
Thomas Graff89bd6f2015-01-02 23:00:21 +010031/* Base bits plus 1 bit for nulls marker */
32#define HASH_RESERVED_SPACE (RHT_BASE_BITS + 1)
33
Thomas Graf97defe12015-01-02 23:00:20 +010034enum {
35 RHT_LOCK_NORMAL,
36 RHT_LOCK_NESTED,
Thomas Graf97defe12015-01-02 23:00:20 +010037};
38
39/* The bucket lock is selected based on the hash and protects mutations
40 * on a group of hash buckets.
41 *
Thomas Grafa5ec68e2015-02-05 02:03:32 +010042 * A maximum of tbl->size/2 bucket locks is allocated. This ensures that
43 * a single lock always covers both buckets which may both contains
44 * entries which link to the same bucket of the old table during resizing.
45 * This allows to simplify the locking as locking the bucket in both
46 * tables during resize always guarantee protection.
47 *
Thomas Graf97defe12015-01-02 23:00:20 +010048 * IMPORTANT: When holding the bucket lock of both the old and new table
49 * during expansions and shrinking, the old bucket lock must always be
50 * acquired first.
51 */
52static spinlock_t *bucket_lock(const struct bucket_table *tbl, u32 hash)
53{
54 return &tbl->locks[hash & tbl->locks_mask];
55}
Thomas Graf7e1e7762014-08-02 11:47:44 +020056
Thomas Grafc91eee52014-08-13 16:38:30 +020057static void *rht_obj(const struct rhashtable *ht, const struct rhash_head *he)
Thomas Graf7e1e7762014-08-02 11:47:44 +020058{
59 return (void *) he - ht->p.head_offset;
60}
Thomas Graf7e1e7762014-08-02 11:47:44 +020061
Thomas Graf8d24c0b2015-01-02 23:00:14 +010062static u32 rht_bucket_index(const struct bucket_table *tbl, u32 hash)
Thomas Graf7e1e7762014-08-02 11:47:44 +020063{
Thomas Graf8d24c0b2015-01-02 23:00:14 +010064 return hash & (tbl->size - 1);
Thomas Graf7e1e7762014-08-02 11:47:44 +020065}
66
Thomas Graf8d24c0b2015-01-02 23:00:14 +010067static u32 obj_raw_hashfn(const struct rhashtable *ht, const void *ptr)
68{
69 u32 hash;
70
71 if (unlikely(!ht->p.key_len))
72 hash = ht->p.obj_hashfn(ptr, ht->p.hash_rnd);
73 else
74 hash = ht->p.hashfn(ptr + ht->p.key_offset, ht->p.key_len,
75 ht->p.hash_rnd);
76
Thomas Graff89bd6f2015-01-02 23:00:21 +010077 return hash >> HASH_RESERVED_SPACE;
Thomas Graf8d24c0b2015-01-02 23:00:14 +010078}
79
Thomas Graf97defe12015-01-02 23:00:20 +010080static u32 key_hashfn(struct rhashtable *ht, const void *key, u32 len)
Thomas Graf7e1e7762014-08-02 11:47:44 +020081{
Thomas Grafc88455c2015-02-05 02:03:31 +010082 return ht->p.hashfn(key, len, ht->p.hash_rnd) >> HASH_RESERVED_SPACE;
Thomas Graf7e1e7762014-08-02 11:47:44 +020083}
Thomas Graf7e1e7762014-08-02 11:47:44 +020084
85static u32 head_hashfn(const struct rhashtable *ht,
Thomas Graf8d24c0b2015-01-02 23:00:14 +010086 const struct bucket_table *tbl,
87 const struct rhash_head *he)
Thomas Graf7e1e7762014-08-02 11:47:44 +020088{
Thomas Graf8d24c0b2015-01-02 23:00:14 +010089 return rht_bucket_index(tbl, obj_raw_hashfn(ht, rht_obj(ht, he)));
Thomas Graf7e1e7762014-08-02 11:47:44 +020090}
91
Thomas Grafa03eaec2015-02-05 02:03:34 +010092#ifdef CONFIG_PROVE_LOCKING
93static void debug_dump_buckets(const struct rhashtable *ht,
94 const struct bucket_table *tbl)
95{
96 struct rhash_head *he;
97 unsigned int i, hash;
98
99 for (i = 0; i < tbl->size; i++) {
100 pr_warn(" [Bucket %d] ", i);
101 rht_for_each_rcu(he, tbl, i) {
102 hash = head_hashfn(ht, tbl, he);
103 pr_cont("[hash = %#x, lock = %p] ",
104 hash, bucket_lock(tbl, hash));
105 }
106 pr_cont("\n");
107 }
108
109}
110
111static void debug_dump_table(struct rhashtable *ht,
112 const struct bucket_table *tbl,
113 unsigned int hash)
114{
115 struct bucket_table *old_tbl, *future_tbl;
116
117 pr_emerg("BUG: lock for hash %#x in table %p not held\n",
118 hash, tbl);
119
120 rcu_read_lock();
121 future_tbl = rht_dereference_rcu(ht->future_tbl, ht);
122 old_tbl = rht_dereference_rcu(ht->tbl, ht);
123 if (future_tbl != old_tbl) {
124 pr_warn("Future table %p (size: %zd)\n",
125 future_tbl, future_tbl->size);
126 debug_dump_buckets(ht, future_tbl);
127 }
128
129 pr_warn("Table %p (size: %zd)\n", old_tbl, old_tbl->size);
130 debug_dump_buckets(ht, old_tbl);
131
132 rcu_read_unlock();
133}
134
135#define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
136#define ASSERT_BUCKET_LOCK(HT, TBL, HASH) \
137 do { \
138 if (unlikely(!lockdep_rht_bucket_is_held(TBL, HASH))) { \
139 debug_dump_table(HT, TBL, HASH); \
140 BUG(); \
141 } \
142 } while (0)
143
144int lockdep_rht_mutex_is_held(struct rhashtable *ht)
145{
146 return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1;
147}
148EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held);
149
150int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash)
151{
152 spinlock_t *lock = bucket_lock(tbl, hash);
153
154 return (debug_locks) ? lockdep_is_held(lock) : 1;
155}
156EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held);
157#else
158#define ASSERT_RHT_MUTEX(HT)
159#define ASSERT_BUCKET_LOCK(HT, TBL, HASH)
160#endif
161
162
Thomas Grafb8e19432015-01-02 23:00:17 +0100163static struct rhash_head __rcu **bucket_tail(struct bucket_table *tbl, u32 n)
164{
165 struct rhash_head __rcu **pprev;
166
167 for (pprev = &tbl->buckets[n];
Thomas Graff89bd6f2015-01-02 23:00:21 +0100168 !rht_is_a_nulls(rht_dereference_bucket(*pprev, tbl, n));
Thomas Grafb8e19432015-01-02 23:00:17 +0100169 pprev = &rht_dereference_bucket(*pprev, tbl, n)->next)
170 ;
171
172 return pprev;
173}
174
Thomas Graf97defe12015-01-02 23:00:20 +0100175static int alloc_bucket_locks(struct rhashtable *ht, struct bucket_table *tbl)
176{
177 unsigned int i, size;
178#if defined(CONFIG_PROVE_LOCKING)
179 unsigned int nr_pcpus = 2;
180#else
181 unsigned int nr_pcpus = num_possible_cpus();
182#endif
183
184 nr_pcpus = min_t(unsigned int, nr_pcpus, 32UL);
185 size = roundup_pow_of_two(nr_pcpus * ht->p.locks_mul);
186
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100187 /* Never allocate more than 0.5 locks per bucket */
188 size = min_t(unsigned int, size, tbl->size >> 1);
Thomas Graf97defe12015-01-02 23:00:20 +0100189
190 if (sizeof(spinlock_t) != 0) {
191#ifdef CONFIG_NUMA
192 if (size * sizeof(spinlock_t) > PAGE_SIZE)
193 tbl->locks = vmalloc(size * sizeof(spinlock_t));
194 else
195#endif
196 tbl->locks = kmalloc_array(size, sizeof(spinlock_t),
197 GFP_KERNEL);
198 if (!tbl->locks)
199 return -ENOMEM;
200 for (i = 0; i < size; i++)
201 spin_lock_init(&tbl->locks[i]);
202 }
203 tbl->locks_mask = size - 1;
204
205 return 0;
206}
207
208static void bucket_table_free(const struct bucket_table *tbl)
209{
210 if (tbl)
211 kvfree(tbl->locks);
212
213 kvfree(tbl);
214}
215
216static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
217 size_t nbuckets)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200218{
219 struct bucket_table *tbl;
220 size_t size;
Thomas Graff89bd6f2015-01-02 23:00:21 +0100221 int i;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200222
223 size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]);
Thomas Graf6eba8222014-11-13 13:45:46 +0100224 tbl = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200225 if (tbl == NULL)
226 tbl = vzalloc(size);
227
228 if (tbl == NULL)
229 return NULL;
230
231 tbl->size = nbuckets;
232
Thomas Graf97defe12015-01-02 23:00:20 +0100233 if (alloc_bucket_locks(ht, tbl) < 0) {
234 bucket_table_free(tbl);
235 return NULL;
236 }
Thomas Graf7e1e7762014-08-02 11:47:44 +0200237
Thomas Graff89bd6f2015-01-02 23:00:21 +0100238 for (i = 0; i < nbuckets; i++)
239 INIT_RHT_NULLS_HEAD(tbl->buckets[i], ht, i);
240
Thomas Graf97defe12015-01-02 23:00:20 +0100241 return tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200242}
243
244/**
245 * rht_grow_above_75 - returns true if nelems > 0.75 * table-size
246 * @ht: hash table
247 * @new_size: new table size
248 */
249bool rht_grow_above_75(const struct rhashtable *ht, size_t new_size)
250{
251 /* Expand table when exceeding 75% load */
Ying Xuec0c09bf2015-01-07 13:41:56 +0800252 return atomic_read(&ht->nelems) > (new_size / 4 * 3) &&
253 (ht->p.max_shift && atomic_read(&ht->shift) < ht->p.max_shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200254}
255EXPORT_SYMBOL_GPL(rht_grow_above_75);
256
257/**
258 * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size
259 * @ht: hash table
260 * @new_size: new table size
261 */
262bool rht_shrink_below_30(const struct rhashtable *ht, size_t new_size)
263{
264 /* Shrink table beneath 30% load */
Ying Xuec0c09bf2015-01-07 13:41:56 +0800265 return atomic_read(&ht->nelems) < (new_size * 3 / 10) &&
266 (atomic_read(&ht->shift) > ht->p.min_shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200267}
268EXPORT_SYMBOL_GPL(rht_shrink_below_30);
269
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100270static void lock_buckets(struct bucket_table *new_tbl,
271 struct bucket_table *old_tbl, unsigned int hash)
272 __acquires(old_bucket_lock)
273{
274 spin_lock_bh(bucket_lock(old_tbl, hash));
275 if (new_tbl != old_tbl)
276 spin_lock_bh_nested(bucket_lock(new_tbl, hash),
277 RHT_LOCK_NESTED);
278}
279
280static void unlock_buckets(struct bucket_table *new_tbl,
281 struct bucket_table *old_tbl, unsigned int hash)
282 __releases(old_bucket_lock)
283{
284 if (new_tbl != old_tbl)
285 spin_unlock_bh(bucket_lock(new_tbl, hash));
286 spin_unlock_bh(bucket_lock(old_tbl, hash));
287}
288
289/**
290 * Unlink entries on bucket which hash to different bucket.
291 *
292 * Returns true if no more work needs to be performed on the bucket.
293 */
Thomas Grafa03eaec2015-02-05 02:03:34 +0100294static bool hashtable_chain_unzip(struct rhashtable *ht,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200295 const struct bucket_table *new_tbl,
Thomas Graf97defe12015-01-02 23:00:20 +0100296 struct bucket_table *old_tbl,
297 size_t old_hash)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200298{
299 struct rhash_head *he, *p, *next;
Thomas Graf97defe12015-01-02 23:00:20 +0100300 unsigned int new_hash, new_hash2;
301
Thomas Grafa03eaec2015-02-05 02:03:34 +0100302 ASSERT_BUCKET_LOCK(ht, old_tbl, old_hash);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200303
304 /* Old bucket empty, no work needed. */
Thomas Graf97defe12015-01-02 23:00:20 +0100305 p = rht_dereference_bucket(old_tbl->buckets[old_hash], old_tbl,
306 old_hash);
Thomas Graff89bd6f2015-01-02 23:00:21 +0100307 if (rht_is_a_nulls(p))
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100308 return false;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200309
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100310 new_hash = head_hashfn(ht, new_tbl, p);
Thomas Grafa03eaec2015-02-05 02:03:34 +0100311 ASSERT_BUCKET_LOCK(ht, new_tbl, new_hash);
Thomas Graf97defe12015-01-02 23:00:20 +0100312
Thomas Graf7e1e7762014-08-02 11:47:44 +0200313 /* Advance the old bucket pointer one or more times until it
314 * reaches a node that doesn't hash to the same bucket as the
315 * previous node p. Call the previous node p;
316 */
Thomas Graf97defe12015-01-02 23:00:20 +0100317 rht_for_each_continue(he, p->next, old_tbl, old_hash) {
318 new_hash2 = head_hashfn(ht, new_tbl, he);
Thomas Grafa03eaec2015-02-05 02:03:34 +0100319 ASSERT_BUCKET_LOCK(ht, new_tbl, new_hash2);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100320
Thomas Graf97defe12015-01-02 23:00:20 +0100321 if (new_hash != new_hash2)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200322 break;
323 p = he;
324 }
Thomas Graf97defe12015-01-02 23:00:20 +0100325 rcu_assign_pointer(old_tbl->buckets[old_hash], p->next);
326
Thomas Graf7e1e7762014-08-02 11:47:44 +0200327 /* Find the subsequent node which does hash to the same
328 * bucket as node P, or NULL if no such node exists.
329 */
Thomas Graff89bd6f2015-01-02 23:00:21 +0100330 INIT_RHT_NULLS_HEAD(next, ht, old_hash);
331 if (!rht_is_a_nulls(he)) {
Thomas Graf97defe12015-01-02 23:00:20 +0100332 rht_for_each_continue(he, he->next, old_tbl, old_hash) {
333 if (head_hashfn(ht, new_tbl, he) == new_hash) {
Thomas Graf7e1e7762014-08-02 11:47:44 +0200334 next = he;
335 break;
336 }
337 }
338 }
339
340 /* Set p's next pointer to that subsequent node pointer,
341 * bypassing the nodes which do not hash to p's bucket
342 */
Thomas Graf97defe12015-01-02 23:00:20 +0100343 rcu_assign_pointer(p->next, next);
344
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100345 p = rht_dereference_bucket(old_tbl->buckets[old_hash], old_tbl,
346 old_hash);
347
348 return !rht_is_a_nulls(p);
Thomas Graf97defe12015-01-02 23:00:20 +0100349}
350
Thomas Graf7cd10db2015-02-05 02:03:35 +0100351static void link_old_to_new(struct rhashtable *ht, struct bucket_table *new_tbl,
Thomas Graf97defe12015-01-02 23:00:20 +0100352 unsigned int new_hash, struct rhash_head *entry)
353{
Thomas Graf7cd10db2015-02-05 02:03:35 +0100354 ASSERT_BUCKET_LOCK(ht, new_tbl, new_hash);
355
Thomas Graf97defe12015-01-02 23:00:20 +0100356 rcu_assign_pointer(*bucket_tail(new_tbl, new_hash), entry);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200357}
358
359/**
360 * rhashtable_expand - Expand hash table while allowing concurrent lookups
361 * @ht: the hash table to expand
Thomas Graf7e1e7762014-08-02 11:47:44 +0200362 *
363 * A secondary bucket array is allocated and the hash entries are migrated
364 * while keeping them on both lists until the end of the RCU grace period.
365 *
366 * This function may only be called in a context where it is safe to call
367 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
368 *
Thomas Graf97defe12015-01-02 23:00:20 +0100369 * The caller must ensure that no concurrent resizing occurs by holding
370 * ht->mutex.
371 *
372 * It is valid to have concurrent insertions and deletions protected by per
373 * bucket locks or concurrent RCU protected lookups and traversals.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200374 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100375int rhashtable_expand(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200376{
377 struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
378 struct rhash_head *he;
Thomas Graf97defe12015-01-02 23:00:20 +0100379 unsigned int new_hash, old_hash;
380 bool complete = false;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200381
382 ASSERT_RHT_MUTEX(ht);
383
Thomas Graf97defe12015-01-02 23:00:20 +0100384 new_tbl = bucket_table_alloc(ht, old_tbl->size * 2);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200385 if (new_tbl == NULL)
386 return -ENOMEM;
387
Ying Xuec0c09bf2015-01-07 13:41:56 +0800388 atomic_inc(&ht->shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200389
Thomas Graf97defe12015-01-02 23:00:20 +0100390 /* Make insertions go into the new, empty table right away. Deletions
391 * and lookups will be attempted in both tables until we synchronize.
392 * The synchronize_rcu() guarantees for the new table to be picked up
393 * so no new additions go into the old table while we relink.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200394 */
Thomas Graf97defe12015-01-02 23:00:20 +0100395 rcu_assign_pointer(ht->future_tbl, new_tbl);
396 synchronize_rcu();
397
398 /* For each new bucket, search the corresponding old bucket for the
399 * first entry that hashes to the new bucket, and link the end of
400 * newly formed bucket chain (containing entries added to future
401 * table) to that entry. Since all the entries which will end up in
402 * the new bucket appear in the same old bucket, this constructs an
403 * entirely valid new hash table, but with multiple buckets
404 * "zipped" together into a single imprecise chain.
405 */
406 for (new_hash = 0; new_hash < new_tbl->size; new_hash++) {
407 old_hash = rht_bucket_index(old_tbl, new_hash);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100408 lock_buckets(new_tbl, old_tbl, new_hash);
Thomas Graf97defe12015-01-02 23:00:20 +0100409 rht_for_each(he, old_tbl, old_hash) {
410 if (head_hashfn(ht, new_tbl, he) == new_hash) {
Thomas Graf7cd10db2015-02-05 02:03:35 +0100411 link_old_to_new(ht, new_tbl, new_hash, he);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200412 break;
413 }
414 }
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100415 unlock_buckets(new_tbl, old_tbl, new_hash);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200416 }
417
Thomas Graf7e1e7762014-08-02 11:47:44 +0200418 /* Unzip interleaved hash chains */
Thomas Graf97defe12015-01-02 23:00:20 +0100419 while (!complete && !ht->being_destroyed) {
Thomas Graf7e1e7762014-08-02 11:47:44 +0200420 /* Wait for readers. All new readers will see the new
421 * table, and thus no references to the old table will
422 * remain.
423 */
424 synchronize_rcu();
425
426 /* For each bucket in the old table (each of which
427 * contains items from multiple buckets of the new
428 * table): ...
429 */
430 complete = true;
Thomas Graf97defe12015-01-02 23:00:20 +0100431 for (old_hash = 0; old_hash < old_tbl->size; old_hash++) {
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100432 lock_buckets(new_tbl, old_tbl, old_hash);
Thomas Graff89bd6f2015-01-02 23:00:21 +0100433
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100434 if (hashtable_chain_unzip(ht, new_tbl, old_tbl,
435 old_hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200436 complete = false;
Thomas Graf97defe12015-01-02 23:00:20 +0100437
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100438 unlock_buckets(new_tbl, old_tbl, old_hash);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200439 }
Thomas Graf97defe12015-01-02 23:00:20 +0100440 }
Thomas Graf7e1e7762014-08-02 11:47:44 +0200441
Thomas Grafcf52d522015-02-05 02:03:36 +0100442 rcu_assign_pointer(ht->tbl, new_tbl);
Thomas Graf2af4b522015-02-05 02:03:33 +0100443 synchronize_rcu();
444
Thomas Graf7e1e7762014-08-02 11:47:44 +0200445 bucket_table_free(old_tbl);
446 return 0;
447}
448EXPORT_SYMBOL_GPL(rhashtable_expand);
449
450/**
451 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
452 * @ht: the hash table to shrink
Thomas Graf7e1e7762014-08-02 11:47:44 +0200453 *
454 * This function may only be called in a context where it is safe to call
455 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
456 *
Thomas Graf97defe12015-01-02 23:00:20 +0100457 * The caller must ensure that no concurrent resizing occurs by holding
458 * ht->mutex.
459 *
Thomas Graf7e1e7762014-08-02 11:47:44 +0200460 * The caller must ensure that no concurrent table mutations take place.
461 * It is however valid to have concurrent lookups if they are RCU protected.
Thomas Graf97defe12015-01-02 23:00:20 +0100462 *
463 * It is valid to have concurrent insertions and deletions protected by per
464 * bucket locks or concurrent RCU protected lookups and traversals.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200465 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100466int rhashtable_shrink(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200467{
Thomas Graf97defe12015-01-02 23:00:20 +0100468 struct bucket_table *new_tbl, *tbl = rht_dereference(ht->tbl, ht);
Thomas Graf97defe12015-01-02 23:00:20 +0100469 unsigned int new_hash;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200470
471 ASSERT_RHT_MUTEX(ht);
472
Thomas Graf97defe12015-01-02 23:00:20 +0100473 new_tbl = bucket_table_alloc(ht, tbl->size / 2);
474 if (new_tbl == NULL)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200475 return -ENOMEM;
476
Thomas Graf97defe12015-01-02 23:00:20 +0100477 rcu_assign_pointer(ht->future_tbl, new_tbl);
478 synchronize_rcu();
Thomas Graf7e1e7762014-08-02 11:47:44 +0200479
Thomas Graf97defe12015-01-02 23:00:20 +0100480 /* Link the first entry in the old bucket to the end of the
481 * bucket in the new table. As entries are concurrently being
482 * added to the new table, lock down the new bucket. As we
483 * always divide the size in half when shrinking, each bucket
484 * in the new table maps to exactly two buckets in the old
485 * table.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200486 */
Thomas Graf97defe12015-01-02 23:00:20 +0100487 for (new_hash = 0; new_hash < new_tbl->size; new_hash++) {
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100488 lock_buckets(new_tbl, tbl, new_hash);
Thomas Graf97defe12015-01-02 23:00:20 +0100489
490 rcu_assign_pointer(*bucket_tail(new_tbl, new_hash),
491 tbl->buckets[new_hash]);
Thomas Graf7cd10db2015-02-05 02:03:35 +0100492 ASSERT_BUCKET_LOCK(ht, tbl, new_hash + new_tbl->size);
Thomas Graf97defe12015-01-02 23:00:20 +0100493 rcu_assign_pointer(*bucket_tail(new_tbl, new_hash),
494 tbl->buckets[new_hash + new_tbl->size]);
495
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100496 unlock_buckets(new_tbl, tbl, new_hash);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200497 }
498
499 /* Publish the new, valid hash table */
Thomas Graf97defe12015-01-02 23:00:20 +0100500 rcu_assign_pointer(ht->tbl, new_tbl);
Ying Xuec0c09bf2015-01-07 13:41:56 +0800501 atomic_dec(&ht->shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200502
503 /* Wait for readers. No new readers will have references to the
504 * old hash table.
505 */
506 synchronize_rcu();
507
508 bucket_table_free(tbl);
509
510 return 0;
511}
512EXPORT_SYMBOL_GPL(rhashtable_shrink);
513
Thomas Graf97defe12015-01-02 23:00:20 +0100514static void rht_deferred_worker(struct work_struct *work)
515{
516 struct rhashtable *ht;
517 struct bucket_table *tbl;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100518 struct rhashtable_walker *walker;
Thomas Graf97defe12015-01-02 23:00:20 +0100519
Ying Xue57699a42015-01-16 11:13:09 +0800520 ht = container_of(work, struct rhashtable, run_work);
Thomas Graf97defe12015-01-02 23:00:20 +0100521 mutex_lock(&ht->mutex);
Herbert Xu28134a52015-02-04 07:33:22 +1100522 if (ht->being_destroyed)
523 goto unlock;
524
Thomas Graf97defe12015-01-02 23:00:20 +0100525 tbl = rht_dereference(ht->tbl, ht);
526
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100527 list_for_each_entry(walker, &ht->walkers, list)
528 walker->resize = true;
529
Thomas Graf97defe12015-01-02 23:00:20 +0100530 if (ht->p.grow_decision && ht->p.grow_decision(ht, tbl->size))
531 rhashtable_expand(ht);
532 else if (ht->p.shrink_decision && ht->p.shrink_decision(ht, tbl->size))
533 rhashtable_shrink(ht);
534
Herbert Xu28134a52015-02-04 07:33:22 +1100535unlock:
Thomas Graf97defe12015-01-02 23:00:20 +0100536 mutex_unlock(&ht->mutex);
537}
538
Ying Xue54c5b7d2015-01-07 13:41:53 +0800539static void rhashtable_wakeup_worker(struct rhashtable *ht)
540{
541 struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht);
542 struct bucket_table *new_tbl = rht_dereference_rcu(ht->future_tbl, ht);
543 size_t size = tbl->size;
544
545 /* Only adjust the table if no resizing is currently in progress. */
546 if (tbl == new_tbl &&
547 ((ht->p.grow_decision && ht->p.grow_decision(ht, size)) ||
548 (ht->p.shrink_decision && ht->p.shrink_decision(ht, size))))
Ying Xue57699a42015-01-16 11:13:09 +0800549 schedule_work(&ht->run_work);
Ying Xue54c5b7d2015-01-07 13:41:53 +0800550}
551
Ying Xuedb304852015-01-07 13:41:54 +0800552static void __rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj,
553 struct bucket_table *tbl, u32 hash)
554{
555 struct rhash_head *head = rht_dereference_bucket(tbl->buckets[hash],
556 tbl, hash);
557
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);
568
569 rhashtable_wakeup_worker(ht);
570}
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 Graf8d24c0b2015-01-02 23:00:14 +0100596 hash = head_hashfn(ht, tbl, obj);
Thomas Graf97defe12015-01-02 23:00:20 +0100597
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100598 lock_buckets(tbl, old_tbl, hash);
Ying Xuedb304852015-01-07 13:41:54 +0800599 __rhashtable_insert(ht, obj, 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 Grafa5ec68e2015-02-05 02:03:32 +0100630 tbl = old_tbl = rht_dereference_rcu(ht->tbl, ht);
631 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 Grafcf52d522015-02-05 02:03:36 +0100646 if (unlikely(new_tbl != tbl)) {
647 rht_for_each_continue(he2, he->next, tbl, hash) {
648 if (head_hashfn(ht, tbl, he2) == hash) {
649 rcu_assign_pointer(*pprev, he2);
650 goto found;
651 }
652 }
653
654 INIT_RHT_NULLS_HEAD(*pprev, ht, hash);
655 } else {
656 rcu_assign_pointer(*pprev, obj->next);
657 }
658
659found:
Thomas Graffe6a0432015-01-21 11:54:01 +0000660 ret = true;
661 break;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200662 }
663
Thomas Graffe6a0432015-01-21 11:54:01 +0000664 /* The entry may be linked in either 'tbl', 'future_tbl', or both.
665 * 'future_tbl' only exists for a short period of time during
666 * resizing. Thus traversing both is fine and the added cost is
667 * very rare.
668 */
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100669 if (tbl != new_tbl) {
670 tbl = new_tbl;
Thomas Graf97defe12015-01-02 23:00:20 +0100671 goto restart;
672 }
673
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100674 unlock_buckets(new_tbl, old_tbl, new_hash);
Thomas Graffe6a0432015-01-21 11:54:01 +0000675
676 if (ret) {
677 atomic_dec(&ht->nelems);
678 rhashtable_wakeup_worker(ht);
679 }
680
Thomas Graf97defe12015-01-02 23:00:20 +0100681 rcu_read_unlock();
682
Thomas Graffe6a0432015-01-21 11:54:01 +0000683 return ret;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200684}
685EXPORT_SYMBOL_GPL(rhashtable_remove);
686
Ying Xueefb975a62015-01-07 13:41:52 +0800687struct rhashtable_compare_arg {
688 struct rhashtable *ht;
689 const void *key;
690};
691
692static bool rhashtable_compare(void *ptr, void *arg)
693{
694 struct rhashtable_compare_arg *x = arg;
695 struct rhashtable *ht = x->ht;
696
697 return !memcmp(ptr + ht->p.key_offset, x->key, ht->p.key_len);
698}
699
Thomas Graf7e1e7762014-08-02 11:47:44 +0200700/**
701 * rhashtable_lookup - lookup key in hash table
702 * @ht: hash table
703 * @key: pointer to key
704 *
705 * Computes the hash value for the key and traverses the bucket chain looking
706 * for a entry with an identical key. The first matching entry is returned.
707 *
708 * This lookup function may only be used for fixed key hash table (key_len
Ying Xuedb304852015-01-07 13:41:54 +0800709 * parameter set). It will BUG() if used inappropriately.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200710 *
Thomas Graf97defe12015-01-02 23:00:20 +0100711 * Lookups may occur in parallel with hashtable mutations and resizing.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200712 */
Thomas Graf97defe12015-01-02 23:00:20 +0100713void *rhashtable_lookup(struct rhashtable *ht, const void *key)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200714{
Ying Xueefb975a62015-01-07 13:41:52 +0800715 struct rhashtable_compare_arg arg = {
716 .ht = ht,
717 .key = key,
718 };
Thomas Graf7e1e7762014-08-02 11:47:44 +0200719
720 BUG_ON(!ht->p.key_len);
721
Ying Xueefb975a62015-01-07 13:41:52 +0800722 return rhashtable_lookup_compare(ht, key, &rhashtable_compare, &arg);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200723}
724EXPORT_SYMBOL_GPL(rhashtable_lookup);
725
726/**
727 * rhashtable_lookup_compare - search hash table with compare function
728 * @ht: hash table
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100729 * @key: the pointer to the key
Thomas Graf7e1e7762014-08-02 11:47:44 +0200730 * @compare: compare function, must return true on match
731 * @arg: argument passed on to compare function
732 *
733 * Traverses the bucket chain behind the provided hash value and calls the
734 * specified compare function for each entry.
735 *
Thomas Graf97defe12015-01-02 23:00:20 +0100736 * Lookups may occur in parallel with hashtable mutations and resizing.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200737 *
738 * Returns the first entry on which the compare function returned true.
739 */
Thomas Graf97defe12015-01-02 23:00:20 +0100740void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200741 bool (*compare)(void *, void *), void *arg)
742{
Thomas Graf97defe12015-01-02 23:00:20 +0100743 const struct bucket_table *tbl, *old_tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200744 struct rhash_head *he;
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100745 u32 hash;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200746
Thomas Graf97defe12015-01-02 23:00:20 +0100747 rcu_read_lock();
748
749 old_tbl = rht_dereference_rcu(ht->tbl, ht);
750 tbl = rht_dereference_rcu(ht->future_tbl, ht);
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100751 hash = key_hashfn(ht, key, ht->p.key_len);
Thomas Graf97defe12015-01-02 23:00:20 +0100752restart:
753 rht_for_each_rcu(he, tbl, rht_bucket_index(tbl, hash)) {
Thomas Graf7e1e7762014-08-02 11:47:44 +0200754 if (!compare(rht_obj(ht, he), arg))
755 continue;
Thomas Graf97defe12015-01-02 23:00:20 +0100756 rcu_read_unlock();
Thomas Grafa4b18cd2015-01-02 23:00:15 +0100757 return rht_obj(ht, he);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200758 }
759
Thomas Graf97defe12015-01-02 23:00:20 +0100760 if (unlikely(tbl != old_tbl)) {
761 tbl = old_tbl;
762 goto restart;
763 }
764 rcu_read_unlock();
765
Thomas Graf7e1e7762014-08-02 11:47:44 +0200766 return NULL;
767}
768EXPORT_SYMBOL_GPL(rhashtable_lookup_compare);
769
Ying Xuedb304852015-01-07 13:41:54 +0800770/**
771 * rhashtable_lookup_insert - lookup and insert object into hash table
772 * @ht: hash table
773 * @obj: pointer to hash head inside object
774 *
775 * Locks down the bucket chain in both the old and new table if a resize
776 * is in progress to ensure that writers can't remove from the old table
777 * and can't insert to the new table during the atomic operation of search
778 * and insertion. Searches for duplicates in both the old and new table if
779 * a resize is in progress.
780 *
781 * This lookup function may only be used for fixed key hash table (key_len
782 * parameter set). It will BUG() if used inappropriately.
783 *
784 * It is safe to call this function from atomic context.
785 *
786 * Will trigger an automatic deferred table resizing if the size grows
787 * beyond the watermark indicated by grow_decision() which can be passed
788 * to rhashtable_init().
789 */
790bool rhashtable_lookup_insert(struct rhashtable *ht, struct rhash_head *obj)
791{
Ying Xue7a868d12015-01-12 14:52:22 +0800792 struct rhashtable_compare_arg arg = {
793 .ht = ht,
794 .key = rht_obj(ht, obj) + ht->p.key_offset,
795 };
796
797 BUG_ON(!ht->p.key_len);
798
799 return rhashtable_lookup_compare_insert(ht, obj, &rhashtable_compare,
800 &arg);
801}
802EXPORT_SYMBOL_GPL(rhashtable_lookup_insert);
803
804/**
805 * rhashtable_lookup_compare_insert - search and insert object to hash table
806 * with compare function
807 * @ht: hash table
808 * @obj: pointer to hash head inside object
809 * @compare: compare function, must return true on match
810 * @arg: argument passed on to compare function
811 *
812 * Locks down the bucket chain in both the old and new table if a resize
813 * is in progress to ensure that writers can't remove from the old table
814 * and can't insert to the new table during the atomic operation of search
815 * and insertion. Searches for duplicates in both the old and new table if
816 * a resize is in progress.
817 *
818 * Lookups may occur in parallel with hashtable mutations and resizing.
819 *
820 * Will trigger an automatic deferred table resizing if the size grows
821 * beyond the watermark indicated by grow_decision() which can be passed
822 * to rhashtable_init().
823 */
824bool rhashtable_lookup_compare_insert(struct rhashtable *ht,
825 struct rhash_head *obj,
826 bool (*compare)(void *, void *),
827 void *arg)
828{
Ying Xuedb304852015-01-07 13:41:54 +0800829 struct bucket_table *new_tbl, *old_tbl;
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100830 u32 new_hash;
Ying Xuedb304852015-01-07 13:41:54 +0800831 bool success = true;
832
833 BUG_ON(!ht->p.key_len);
834
835 rcu_read_lock();
Ying Xuedb304852015-01-07 13:41:54 +0800836 old_tbl = rht_dereference_rcu(ht->tbl, ht);
Ying Xuedb304852015-01-07 13:41:54 +0800837 new_tbl = rht_dereference_rcu(ht->future_tbl, ht);
838 new_hash = head_hashfn(ht, new_tbl, obj);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100839
840 lock_buckets(new_tbl, old_tbl, new_hash);
Ying Xuedb304852015-01-07 13:41:54 +0800841
Ying Xue7a868d12015-01-12 14:52:22 +0800842 if (rhashtable_lookup_compare(ht, rht_obj(ht, obj) + ht->p.key_offset,
843 compare, arg)) {
Ying Xuedb304852015-01-07 13:41:54 +0800844 success = false;
845 goto exit;
846 }
847
848 __rhashtable_insert(ht, obj, new_tbl, new_hash);
849
850exit:
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100851 unlock_buckets(new_tbl, old_tbl, new_hash);
Ying Xuedb304852015-01-07 13:41:54 +0800852 rcu_read_unlock();
853
854 return success;
855}
Ying Xue7a868d12015-01-12 14:52:22 +0800856EXPORT_SYMBOL_GPL(rhashtable_lookup_compare_insert);
Ying Xuedb304852015-01-07 13:41:54 +0800857
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100858/**
859 * rhashtable_walk_init - Initialise an iterator
860 * @ht: Table to walk over
861 * @iter: Hash table Iterator
862 *
863 * This function prepares a hash table walk.
864 *
865 * Note that if you restart a walk after rhashtable_walk_stop you
866 * may see the same object twice. Also, you may miss objects if
867 * there are removals in between rhashtable_walk_stop and the next
868 * call to rhashtable_walk_start.
869 *
870 * For a completely stable walk you should construct your own data
871 * structure outside the hash table.
872 *
873 * This function may sleep so you must not call it from interrupt
874 * context or with spin locks held.
875 *
876 * You must call rhashtable_walk_exit if this function returns
877 * successfully.
878 */
879int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter)
880{
881 iter->ht = ht;
882 iter->p = NULL;
883 iter->slot = 0;
884 iter->skip = 0;
885
886 iter->walker = kmalloc(sizeof(*iter->walker), GFP_KERNEL);
887 if (!iter->walker)
888 return -ENOMEM;
889
890 mutex_lock(&ht->mutex);
891 list_add(&iter->walker->list, &ht->walkers);
892 mutex_unlock(&ht->mutex);
893
894 return 0;
895}
896EXPORT_SYMBOL_GPL(rhashtable_walk_init);
897
898/**
899 * rhashtable_walk_exit - Free an iterator
900 * @iter: Hash table Iterator
901 *
902 * This function frees resources allocated by rhashtable_walk_init.
903 */
904void rhashtable_walk_exit(struct rhashtable_iter *iter)
905{
906 mutex_lock(&iter->ht->mutex);
907 list_del(&iter->walker->list);
908 mutex_unlock(&iter->ht->mutex);
909 kfree(iter->walker);
910}
911EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
912
913/**
914 * rhashtable_walk_start - Start a hash table walk
915 * @iter: Hash table iterator
916 *
917 * Start a hash table walk. Note that we take the RCU lock in all
918 * cases including when we return an error. So you must always call
919 * rhashtable_walk_stop to clean up.
920 *
921 * Returns zero if successful.
922 *
923 * Returns -EAGAIN if resize event occured. Note that the iterator
924 * will rewind back to the beginning and you may use it immediately
925 * by calling rhashtable_walk_next.
926 */
927int rhashtable_walk_start(struct rhashtable_iter *iter)
928{
929 rcu_read_lock();
930
931 if (iter->walker->resize) {
932 iter->slot = 0;
933 iter->skip = 0;
934 iter->walker->resize = false;
935 return -EAGAIN;
936 }
937
938 return 0;
939}
940EXPORT_SYMBOL_GPL(rhashtable_walk_start);
941
942/**
943 * rhashtable_walk_next - Return the next object and advance the iterator
944 * @iter: Hash table iterator
945 *
946 * Note that you must call rhashtable_walk_stop when you are finished
947 * with the walk.
948 *
949 * Returns the next object or NULL when the end of the table is reached.
950 *
951 * Returns -EAGAIN if resize event occured. Note that the iterator
952 * will rewind back to the beginning and you may continue to use it.
953 */
954void *rhashtable_walk_next(struct rhashtable_iter *iter)
955{
956 const struct bucket_table *tbl;
957 struct rhashtable *ht = iter->ht;
958 struct rhash_head *p = iter->p;
959 void *obj = NULL;
960
961 tbl = rht_dereference_rcu(ht->tbl, ht);
962
963 if (p) {
964 p = rht_dereference_bucket_rcu(p->next, tbl, iter->slot);
965 goto next;
966 }
967
968 for (; iter->slot < tbl->size; iter->slot++) {
969 int skip = iter->skip;
970
971 rht_for_each_rcu(p, tbl, iter->slot) {
972 if (!skip)
973 break;
974 skip--;
975 }
976
977next:
978 if (!rht_is_a_nulls(p)) {
979 iter->skip++;
980 iter->p = p;
981 obj = rht_obj(ht, p);
982 goto out;
983 }
984
985 iter->skip = 0;
986 }
987
988 iter->p = NULL;
989
990out:
991 if (iter->walker->resize) {
992 iter->p = NULL;
993 iter->slot = 0;
994 iter->skip = 0;
995 iter->walker->resize = false;
996 return ERR_PTR(-EAGAIN);
997 }
998
999 return obj;
1000}
1001EXPORT_SYMBOL_GPL(rhashtable_walk_next);
1002
1003/**
1004 * rhashtable_walk_stop - Finish a hash table walk
1005 * @iter: Hash table iterator
1006 *
1007 * Finish a hash table walk.
1008 */
1009void rhashtable_walk_stop(struct rhashtable_iter *iter)
1010{
1011 rcu_read_unlock();
1012 iter->p = NULL;
1013}
1014EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
1015
Ying Xue94000172014-09-03 09:22:36 +08001016static size_t rounded_hashtable_size(struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +02001017{
Ying Xue94000172014-09-03 09:22:36 +08001018 return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
1019 1UL << params->min_shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +02001020}
1021
1022/**
1023 * rhashtable_init - initialize a new hash table
1024 * @ht: hash table to be initialized
1025 * @params: configuration parameters
1026 *
1027 * Initializes a new hash table based on the provided configuration
1028 * parameters. A table can be configured either with a variable or
1029 * fixed length key:
1030 *
1031 * Configuration Example 1: Fixed length keys
1032 * struct test_obj {
1033 * int key;
1034 * void * my_member;
1035 * struct rhash_head node;
1036 * };
1037 *
1038 * struct rhashtable_params params = {
1039 * .head_offset = offsetof(struct test_obj, node),
1040 * .key_offset = offsetof(struct test_obj, key),
1041 * .key_len = sizeof(int),
Daniel Borkmann87545892014-12-10 16:33:11 +01001042 * .hashfn = jhash,
Thomas Graff89bd6f2015-01-02 23:00:21 +01001043 * .nulls_base = (1U << RHT_BASE_SHIFT),
Thomas Graf7e1e7762014-08-02 11:47:44 +02001044 * };
1045 *
1046 * Configuration Example 2: Variable length keys
1047 * struct test_obj {
1048 * [...]
1049 * struct rhash_head node;
1050 * };
1051 *
1052 * u32 my_hash_fn(const void *data, u32 seed)
1053 * {
1054 * struct test_obj *obj = data;
1055 *
1056 * return [... hash ...];
1057 * }
1058 *
1059 * struct rhashtable_params params = {
1060 * .head_offset = offsetof(struct test_obj, node),
Daniel Borkmann87545892014-12-10 16:33:11 +01001061 * .hashfn = jhash,
Thomas Graf7e1e7762014-08-02 11:47:44 +02001062 * .obj_hashfn = my_hash_fn,
Thomas Graf7e1e7762014-08-02 11:47:44 +02001063 * };
1064 */
1065int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params)
1066{
1067 struct bucket_table *tbl;
1068 size_t size;
1069
1070 size = HASH_DEFAULT_SIZE;
1071
1072 if ((params->key_len && !params->hashfn) ||
1073 (!params->key_len && !params->obj_hashfn))
1074 return -EINVAL;
1075
Thomas Graff89bd6f2015-01-02 23:00:21 +01001076 if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT))
1077 return -EINVAL;
1078
Ying Xue94000172014-09-03 09:22:36 +08001079 params->min_shift = max_t(size_t, params->min_shift,
1080 ilog2(HASH_MIN_SIZE));
1081
Thomas Graf7e1e7762014-08-02 11:47:44 +02001082 if (params->nelem_hint)
Ying Xue94000172014-09-03 09:22:36 +08001083 size = rounded_hashtable_size(params);
Thomas Graf7e1e7762014-08-02 11:47:44 +02001084
Thomas Graf97defe12015-01-02 23:00:20 +01001085 memset(ht, 0, sizeof(*ht));
1086 mutex_init(&ht->mutex);
1087 memcpy(&ht->p, params, sizeof(*params));
Herbert Xuf2dba9c2015-02-04 07:33:23 +11001088 INIT_LIST_HEAD(&ht->walkers);
Thomas Graf97defe12015-01-02 23:00:20 +01001089
1090 if (params->locks_mul)
1091 ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
1092 else
1093 ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
1094
1095 tbl = bucket_table_alloc(ht, size);
Thomas Graf7e1e7762014-08-02 11:47:44 +02001096 if (tbl == NULL)
1097 return -ENOMEM;
1098
Ying Xue545a1482015-01-07 13:41:57 +08001099 atomic_set(&ht->nelems, 0);
Ying Xuec0c09bf2015-01-07 13:41:56 +08001100 atomic_set(&ht->shift, ilog2(tbl->size));
Thomas Graf7e1e7762014-08-02 11:47:44 +02001101 RCU_INIT_POINTER(ht->tbl, tbl);
Thomas Graf97defe12015-01-02 23:00:20 +01001102 RCU_INIT_POINTER(ht->future_tbl, tbl);
Thomas Graf7e1e7762014-08-02 11:47:44 +02001103
1104 if (!ht->p.hash_rnd)
1105 get_random_bytes(&ht->p.hash_rnd, sizeof(ht->p.hash_rnd));
1106
Thomas Graf97defe12015-01-02 23:00:20 +01001107 if (ht->p.grow_decision || ht->p.shrink_decision)
Ying Xue57699a42015-01-16 11:13:09 +08001108 INIT_WORK(&ht->run_work, rht_deferred_worker);
Thomas Graf97defe12015-01-02 23:00:20 +01001109
Thomas Graf7e1e7762014-08-02 11:47:44 +02001110 return 0;
1111}
1112EXPORT_SYMBOL_GPL(rhashtable_init);
1113
1114/**
1115 * rhashtable_destroy - destroy hash table
1116 * @ht: the hash table to destroy
1117 *
Pablo Neira Ayusoae82ddc2014-09-02 00:26:05 +02001118 * Frees the bucket array. This function is not rcu safe, therefore the caller
1119 * has to make sure that no resizing may happen by unpublishing the hashtable
1120 * and waiting for the quiescent cycle before releasing the bucket array.
Thomas Graf7e1e7762014-08-02 11:47:44 +02001121 */
Thomas Graf97defe12015-01-02 23:00:20 +01001122void rhashtable_destroy(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +02001123{
Thomas Graf97defe12015-01-02 23:00:20 +01001124 ht->being_destroyed = true;
1125
Ying Xue57699a42015-01-16 11:13:09 +08001126 if (ht->p.grow_decision || ht->p.shrink_decision)
1127 cancel_work_sync(&ht->run_work);
1128
Thomas Graf97defe12015-01-02 23:00:20 +01001129 mutex_lock(&ht->mutex);
Thomas Graf97defe12015-01-02 23:00:20 +01001130 bucket_table_free(rht_dereference(ht->tbl, ht));
Thomas Graf97defe12015-01-02 23:00:20 +01001131 mutex_unlock(&ht->mutex);
Thomas Graf7e1e7762014-08-02 11:47:44 +02001132}
1133EXPORT_SYMBOL_GPL(rhashtable_destroy);