blob: 090641db4c0d25648951a8078ba1945c8ebc1487 [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>
Stephen Rothwell61d7b092015-02-09 14:04:03 +110026#include <linux/err.h>
Thomas Graf7e1e7762014-08-02 11:47:44 +020027
28#define HASH_DEFAULT_SIZE 64UL
29#define HASH_MIN_SIZE 4UL
Thomas Graf97defe12015-01-02 23:00:20 +010030#define BUCKET_LOCKS_PER_CPU 128UL
31
Thomas Graff89bd6f2015-01-02 23:00:21 +010032/* Base bits plus 1 bit for nulls marker */
33#define HASH_RESERVED_SPACE (RHT_BASE_BITS + 1)
34
Thomas Graf97defe12015-01-02 23:00:20 +010035enum {
36 RHT_LOCK_NORMAL,
37 RHT_LOCK_NESTED,
Thomas Graf97defe12015-01-02 23:00:20 +010038};
39
40/* The bucket lock is selected based on the hash and protects mutations
41 * on a group of hash buckets.
42 *
Thomas Grafa5ec68e2015-02-05 02:03:32 +010043 * A maximum of tbl->size/2 bucket locks is allocated. This ensures that
44 * a single lock always covers both buckets which may both contains
45 * entries which link to the same bucket of the old table during resizing.
46 * This allows to simplify the locking as locking the bucket in both
47 * tables during resize always guarantee protection.
48 *
Thomas Graf97defe12015-01-02 23:00:20 +010049 * IMPORTANT: When holding the bucket lock of both the old and new table
50 * during expansions and shrinking, the old bucket lock must always be
51 * acquired first.
52 */
53static spinlock_t *bucket_lock(const struct bucket_table *tbl, u32 hash)
54{
55 return &tbl->locks[hash & tbl->locks_mask];
56}
Thomas Graf7e1e7762014-08-02 11:47:44 +020057
Thomas Grafc91eee52014-08-13 16:38:30 +020058static void *rht_obj(const struct rhashtable *ht, const struct rhash_head *he)
Thomas Graf7e1e7762014-08-02 11:47:44 +020059{
60 return (void *) he - ht->p.head_offset;
61}
Thomas Graf7e1e7762014-08-02 11:47:44 +020062
Thomas Graf8d24c0b2015-01-02 23:00:14 +010063static u32 rht_bucket_index(const struct bucket_table *tbl, u32 hash)
Thomas Graf7e1e7762014-08-02 11:47:44 +020064{
Thomas Graf8d24c0b2015-01-02 23:00:14 +010065 return hash & (tbl->size - 1);
Thomas Graf7e1e7762014-08-02 11:47:44 +020066}
67
Thomas Graf8d24c0b2015-01-02 23:00:14 +010068static u32 obj_raw_hashfn(const struct rhashtable *ht, const void *ptr)
69{
70 u32 hash;
71
72 if (unlikely(!ht->p.key_len))
73 hash = ht->p.obj_hashfn(ptr, ht->p.hash_rnd);
74 else
75 hash = ht->p.hashfn(ptr + ht->p.key_offset, ht->p.key_len,
76 ht->p.hash_rnd);
77
Thomas Graff89bd6f2015-01-02 23:00:21 +010078 return hash >> HASH_RESERVED_SPACE;
Thomas Graf8d24c0b2015-01-02 23:00:14 +010079}
80
Thomas Graf97defe12015-01-02 23:00:20 +010081static u32 key_hashfn(struct rhashtable *ht, const void *key, u32 len)
Thomas Graf7e1e7762014-08-02 11:47:44 +020082{
Thomas Grafc88455c2015-02-05 02:03:31 +010083 return ht->p.hashfn(key, len, ht->p.hash_rnd) >> HASH_RESERVED_SPACE;
Thomas Graf7e1e7762014-08-02 11:47:44 +020084}
Thomas Graf7e1e7762014-08-02 11:47:44 +020085
86static u32 head_hashfn(const struct rhashtable *ht,
Thomas Graf8d24c0b2015-01-02 23:00:14 +010087 const struct bucket_table *tbl,
88 const struct rhash_head *he)
Thomas Graf7e1e7762014-08-02 11:47:44 +020089{
Thomas Graf8d24c0b2015-01-02 23:00:14 +010090 return rht_bucket_index(tbl, obj_raw_hashfn(ht, rht_obj(ht, he)));
Thomas Graf7e1e7762014-08-02 11:47:44 +020091}
92
Thomas Grafa03eaec2015-02-05 02:03:34 +010093#ifdef CONFIG_PROVE_LOCKING
94static void debug_dump_buckets(const struct rhashtable *ht,
95 const struct bucket_table *tbl)
96{
97 struct rhash_head *he;
98 unsigned int i, hash;
99
100 for (i = 0; i < tbl->size; i++) {
101 pr_warn(" [Bucket %d] ", i);
102 rht_for_each_rcu(he, tbl, i) {
103 hash = head_hashfn(ht, tbl, he);
104 pr_cont("[hash = %#x, lock = %p] ",
105 hash, bucket_lock(tbl, hash));
106 }
107 pr_cont("\n");
108 }
109
110}
111
112static void debug_dump_table(struct rhashtable *ht,
113 const struct bucket_table *tbl,
114 unsigned int hash)
115{
116 struct bucket_table *old_tbl, *future_tbl;
117
118 pr_emerg("BUG: lock for hash %#x in table %p not held\n",
119 hash, tbl);
120
121 rcu_read_lock();
122 future_tbl = rht_dereference_rcu(ht->future_tbl, ht);
123 old_tbl = rht_dereference_rcu(ht->tbl, ht);
124 if (future_tbl != old_tbl) {
125 pr_warn("Future table %p (size: %zd)\n",
126 future_tbl, future_tbl->size);
127 debug_dump_buckets(ht, future_tbl);
128 }
129
130 pr_warn("Table %p (size: %zd)\n", old_tbl, old_tbl->size);
131 debug_dump_buckets(ht, old_tbl);
132
133 rcu_read_unlock();
134}
135
136#define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
137#define ASSERT_BUCKET_LOCK(HT, TBL, HASH) \
138 do { \
139 if (unlikely(!lockdep_rht_bucket_is_held(TBL, HASH))) { \
140 debug_dump_table(HT, TBL, HASH); \
141 BUG(); \
142 } \
143 } while (0)
144
145int lockdep_rht_mutex_is_held(struct rhashtable *ht)
146{
147 return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1;
148}
149EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held);
150
151int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash)
152{
153 spinlock_t *lock = bucket_lock(tbl, hash);
154
155 return (debug_locks) ? lockdep_is_held(lock) : 1;
156}
157EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held);
158#else
159#define ASSERT_RHT_MUTEX(HT)
160#define ASSERT_BUCKET_LOCK(HT, TBL, HASH)
161#endif
162
163
Thomas Grafb8e19432015-01-02 23:00:17 +0100164static struct rhash_head __rcu **bucket_tail(struct bucket_table *tbl, u32 n)
165{
166 struct rhash_head __rcu **pprev;
167
168 for (pprev = &tbl->buckets[n];
Thomas Graff89bd6f2015-01-02 23:00:21 +0100169 !rht_is_a_nulls(rht_dereference_bucket(*pprev, tbl, n));
Thomas Grafb8e19432015-01-02 23:00:17 +0100170 pprev = &rht_dereference_bucket(*pprev, tbl, n)->next)
171 ;
172
173 return pprev;
174}
175
Thomas Graf97defe12015-01-02 23:00:20 +0100176static int alloc_bucket_locks(struct rhashtable *ht, struct bucket_table *tbl)
177{
178 unsigned int i, size;
179#if defined(CONFIG_PROVE_LOCKING)
180 unsigned int nr_pcpus = 2;
181#else
182 unsigned int nr_pcpus = num_possible_cpus();
183#endif
184
185 nr_pcpus = min_t(unsigned int, nr_pcpus, 32UL);
186 size = roundup_pow_of_two(nr_pcpus * ht->p.locks_mul);
187
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100188 /* Never allocate more than 0.5 locks per bucket */
189 size = min_t(unsigned int, size, tbl->size >> 1);
Thomas Graf97defe12015-01-02 23:00:20 +0100190
191 if (sizeof(spinlock_t) != 0) {
192#ifdef CONFIG_NUMA
193 if (size * sizeof(spinlock_t) > PAGE_SIZE)
194 tbl->locks = vmalloc(size * sizeof(spinlock_t));
195 else
196#endif
197 tbl->locks = kmalloc_array(size, sizeof(spinlock_t),
198 GFP_KERNEL);
199 if (!tbl->locks)
200 return -ENOMEM;
201 for (i = 0; i < size; i++)
202 spin_lock_init(&tbl->locks[i]);
203 }
204 tbl->locks_mask = size - 1;
205
206 return 0;
207}
208
209static void bucket_table_free(const struct bucket_table *tbl)
210{
211 if (tbl)
212 kvfree(tbl->locks);
213
214 kvfree(tbl);
215}
216
217static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
218 size_t nbuckets)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200219{
Daniel Borkmanneb6d1ab2015-02-20 00:53:38 +0100220 struct bucket_table *tbl = NULL;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200221 size_t size;
Thomas Graff89bd6f2015-01-02 23:00:21 +0100222 int i;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200223
224 size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]);
Daniel Borkmanneb6d1ab2015-02-20 00:53:38 +0100225 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
226 tbl = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200227 if (tbl == NULL)
228 tbl = vzalloc(size);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200229 if (tbl == NULL)
230 return NULL;
231
232 tbl->size = nbuckets;
233
Thomas Graf97defe12015-01-02 23:00:20 +0100234 if (alloc_bucket_locks(ht, tbl) < 0) {
235 bucket_table_free(tbl);
236 return NULL;
237 }
Thomas Graf7e1e7762014-08-02 11:47:44 +0200238
Thomas Graff89bd6f2015-01-02 23:00:21 +0100239 for (i = 0; i < nbuckets; i++)
240 INIT_RHT_NULLS_HEAD(tbl->buckets[i], ht, i);
241
Thomas Graf97defe12015-01-02 23:00:20 +0100242 return tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200243}
244
245/**
246 * rht_grow_above_75 - returns true if nelems > 0.75 * table-size
247 * @ht: hash table
248 * @new_size: new table size
249 */
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100250static bool rht_grow_above_75(const struct rhashtable *ht, size_t new_size)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200251{
252 /* Expand table when exceeding 75% load */
Ying Xuec0c09bf2015-01-07 13:41:56 +0800253 return atomic_read(&ht->nelems) > (new_size / 4 * 3) &&
Daniel Borkmann8331de72015-02-25 16:31:53 +0100254 (!ht->p.max_shift || atomic_read(&ht->shift) < ht->p.max_shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200255}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200256
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 */
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100262static bool rht_shrink_below_30(const struct rhashtable *ht, size_t new_size)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200263{
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}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200268
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100269static void lock_buckets(struct bucket_table *new_tbl,
270 struct bucket_table *old_tbl, unsigned int hash)
271 __acquires(old_bucket_lock)
272{
273 spin_lock_bh(bucket_lock(old_tbl, hash));
274 if (new_tbl != old_tbl)
275 spin_lock_bh_nested(bucket_lock(new_tbl, hash),
276 RHT_LOCK_NESTED);
277}
278
279static void unlock_buckets(struct bucket_table *new_tbl,
280 struct bucket_table *old_tbl, unsigned int hash)
281 __releases(old_bucket_lock)
282{
283 if (new_tbl != old_tbl)
284 spin_unlock_bh(bucket_lock(new_tbl, hash));
285 spin_unlock_bh(bucket_lock(old_tbl, hash));
286}
287
288/**
289 * Unlink entries on bucket which hash to different bucket.
290 *
291 * Returns true if no more work needs to be performed on the bucket.
292 */
Thomas Grafa03eaec2015-02-05 02:03:34 +0100293static bool hashtable_chain_unzip(struct rhashtable *ht,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200294 const struct bucket_table *new_tbl,
Thomas Graf97defe12015-01-02 23:00:20 +0100295 struct bucket_table *old_tbl,
296 size_t old_hash)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200297{
298 struct rhash_head *he, *p, *next;
Thomas Graf97defe12015-01-02 23:00:20 +0100299 unsigned int new_hash, new_hash2;
300
Thomas Grafa03eaec2015-02-05 02:03:34 +0100301 ASSERT_BUCKET_LOCK(ht, old_tbl, old_hash);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200302
303 /* Old bucket empty, no work needed. */
Thomas Graf97defe12015-01-02 23:00:20 +0100304 p = rht_dereference_bucket(old_tbl->buckets[old_hash], old_tbl,
305 old_hash);
Thomas Graff89bd6f2015-01-02 23:00:21 +0100306 if (rht_is_a_nulls(p))
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100307 return false;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200308
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100309 new_hash = head_hashfn(ht, new_tbl, p);
Thomas Grafa03eaec2015-02-05 02:03:34 +0100310 ASSERT_BUCKET_LOCK(ht, new_tbl, new_hash);
Thomas Graf97defe12015-01-02 23:00:20 +0100311
Thomas Graf7e1e7762014-08-02 11:47:44 +0200312 /* Advance the old bucket pointer one or more times until it
313 * reaches a node that doesn't hash to the same bucket as the
314 * previous node p. Call the previous node p;
315 */
Thomas Graf97defe12015-01-02 23:00:20 +0100316 rht_for_each_continue(he, p->next, old_tbl, old_hash) {
317 new_hash2 = head_hashfn(ht, new_tbl, he);
Thomas Grafa03eaec2015-02-05 02:03:34 +0100318 ASSERT_BUCKET_LOCK(ht, new_tbl, new_hash2);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100319
Thomas Graf97defe12015-01-02 23:00:20 +0100320 if (new_hash != new_hash2)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200321 break;
322 p = he;
323 }
Thomas Graf97defe12015-01-02 23:00:20 +0100324 rcu_assign_pointer(old_tbl->buckets[old_hash], p->next);
325
Thomas Graf7e1e7762014-08-02 11:47:44 +0200326 /* Find the subsequent node which does hash to the same
327 * bucket as node P, or NULL if no such node exists.
328 */
Thomas Graff89bd6f2015-01-02 23:00:21 +0100329 INIT_RHT_NULLS_HEAD(next, ht, old_hash);
330 if (!rht_is_a_nulls(he)) {
Thomas Graf97defe12015-01-02 23:00:20 +0100331 rht_for_each_continue(he, he->next, old_tbl, old_hash) {
332 if (head_hashfn(ht, new_tbl, he) == new_hash) {
Thomas Graf7e1e7762014-08-02 11:47:44 +0200333 next = he;
334 break;
335 }
336 }
337 }
338
339 /* Set p's next pointer to that subsequent node pointer,
340 * bypassing the nodes which do not hash to p's bucket
341 */
Thomas Graf97defe12015-01-02 23:00:20 +0100342 rcu_assign_pointer(p->next, next);
343
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100344 p = rht_dereference_bucket(old_tbl->buckets[old_hash], old_tbl,
345 old_hash);
346
347 return !rht_is_a_nulls(p);
Thomas Graf97defe12015-01-02 23:00:20 +0100348}
349
Thomas Graf7cd10db2015-02-05 02:03:35 +0100350static void link_old_to_new(struct rhashtable *ht, struct bucket_table *new_tbl,
Thomas Graf97defe12015-01-02 23:00:20 +0100351 unsigned int new_hash, struct rhash_head *entry)
352{
Thomas Graf7cd10db2015-02-05 02:03:35 +0100353 ASSERT_BUCKET_LOCK(ht, new_tbl, new_hash);
354
Thomas Graf97defe12015-01-02 23:00:20 +0100355 rcu_assign_pointer(*bucket_tail(new_tbl, new_hash), entry);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200356}
357
358/**
359 * rhashtable_expand - Expand hash table while allowing concurrent lookups
360 * @ht: the hash table to expand
Thomas Graf7e1e7762014-08-02 11:47:44 +0200361 *
362 * A secondary bucket array is allocated and the hash entries are migrated
363 * while keeping them on both lists until the end of the RCU grace period.
364 *
365 * This function may only be called in a context where it is safe to call
366 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
367 *
Thomas Graf97defe12015-01-02 23:00:20 +0100368 * The caller must ensure that no concurrent resizing occurs by holding
369 * ht->mutex.
370 *
371 * It is valid to have concurrent insertions and deletions protected by per
372 * bucket locks or concurrent RCU protected lookups and traversals.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200373 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100374int rhashtable_expand(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200375{
376 struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
377 struct rhash_head *he;
Thomas Graf97defe12015-01-02 23:00:20 +0100378 unsigned int new_hash, old_hash;
379 bool complete = false;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200380
381 ASSERT_RHT_MUTEX(ht);
382
Thomas Graf97defe12015-01-02 23:00:20 +0100383 new_tbl = bucket_table_alloc(ht, old_tbl->size * 2);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200384 if (new_tbl == NULL)
385 return -ENOMEM;
386
Ying Xuec0c09bf2015-01-07 13:41:56 +0800387 atomic_inc(&ht->shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200388
Thomas Graf97defe12015-01-02 23:00:20 +0100389 /* Make insertions go into the new, empty table right away. Deletions
390 * and lookups will be attempted in both tables until we synchronize.
391 * The synchronize_rcu() guarantees for the new table to be picked up
392 * so no new additions go into the old table while we relink.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200393 */
Thomas Graf97defe12015-01-02 23:00:20 +0100394 rcu_assign_pointer(ht->future_tbl, new_tbl);
395 synchronize_rcu();
396
397 /* For each new bucket, search the corresponding old bucket for the
398 * first entry that hashes to the new bucket, and link the end of
399 * newly formed bucket chain (containing entries added to future
400 * table) to that entry. Since all the entries which will end up in
401 * the new bucket appear in the same old bucket, this constructs an
402 * entirely valid new hash table, but with multiple buckets
403 * "zipped" together into a single imprecise chain.
404 */
405 for (new_hash = 0; new_hash < new_tbl->size; new_hash++) {
406 old_hash = rht_bucket_index(old_tbl, new_hash);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100407 lock_buckets(new_tbl, old_tbl, new_hash);
Thomas Graf97defe12015-01-02 23:00:20 +0100408 rht_for_each(he, old_tbl, old_hash) {
409 if (head_hashfn(ht, new_tbl, he) == new_hash) {
Thomas Graf7cd10db2015-02-05 02:03:35 +0100410 link_old_to_new(ht, new_tbl, new_hash, he);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200411 break;
412 }
413 }
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100414 unlock_buckets(new_tbl, old_tbl, new_hash);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200415 }
416
Thomas Graf7e1e7762014-08-02 11:47:44 +0200417 /* Unzip interleaved hash chains */
Thomas Graf97defe12015-01-02 23:00:20 +0100418 while (!complete && !ht->being_destroyed) {
Thomas Graf7e1e7762014-08-02 11:47:44 +0200419 /* Wait for readers. All new readers will see the new
420 * table, and thus no references to the old table will
421 * remain.
422 */
423 synchronize_rcu();
424
425 /* For each bucket in the old table (each of which
426 * contains items from multiple buckets of the new
427 * table): ...
428 */
429 complete = true;
Thomas Graf97defe12015-01-02 23:00:20 +0100430 for (old_hash = 0; old_hash < old_tbl->size; old_hash++) {
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100431 lock_buckets(new_tbl, old_tbl, old_hash);
Thomas Graff89bd6f2015-01-02 23:00:21 +0100432
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100433 if (hashtable_chain_unzip(ht, new_tbl, old_tbl,
434 old_hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200435 complete = false;
Thomas Graf97defe12015-01-02 23:00:20 +0100436
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100437 unlock_buckets(new_tbl, old_tbl, old_hash);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200438 }
Thomas Graf97defe12015-01-02 23:00:20 +0100439 }
Thomas Graf7e1e7762014-08-02 11:47:44 +0200440
Thomas Grafcf52d522015-02-05 02:03:36 +0100441 rcu_assign_pointer(ht->tbl, new_tbl);
Thomas Graf2af4b522015-02-05 02:03:33 +0100442 synchronize_rcu();
443
Thomas Graf7e1e7762014-08-02 11:47:44 +0200444 bucket_table_free(old_tbl);
445 return 0;
446}
447EXPORT_SYMBOL_GPL(rhashtable_expand);
448
449/**
450 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
451 * @ht: the hash table to shrink
Thomas Graf7e1e7762014-08-02 11:47:44 +0200452 *
453 * This function may only be called in a context where it is safe to call
454 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
455 *
Thomas Graf97defe12015-01-02 23:00:20 +0100456 * The caller must ensure that no concurrent resizing occurs by holding
457 * ht->mutex.
458 *
Thomas Graf7e1e7762014-08-02 11:47:44 +0200459 * The caller must ensure that no concurrent table mutations take place.
460 * It is however valid to have concurrent lookups if they are RCU protected.
Thomas Graf97defe12015-01-02 23:00:20 +0100461 *
462 * It is valid to have concurrent insertions and deletions protected by per
463 * bucket locks or concurrent RCU protected lookups and traversals.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200464 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100465int rhashtable_shrink(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200466{
Thomas Graf97defe12015-01-02 23:00:20 +0100467 struct bucket_table *new_tbl, *tbl = rht_dereference(ht->tbl, ht);
Thomas Graf97defe12015-01-02 23:00:20 +0100468 unsigned int new_hash;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200469
470 ASSERT_RHT_MUTEX(ht);
471
Thomas Graf97defe12015-01-02 23:00:20 +0100472 new_tbl = bucket_table_alloc(ht, tbl->size / 2);
473 if (new_tbl == NULL)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200474 return -ENOMEM;
475
Thomas Graf97defe12015-01-02 23:00:20 +0100476 rcu_assign_pointer(ht->future_tbl, new_tbl);
477 synchronize_rcu();
Thomas Graf7e1e7762014-08-02 11:47:44 +0200478
Thomas Graf97defe12015-01-02 23:00:20 +0100479 /* Link the first entry in the old bucket to the end of the
480 * bucket in the new table. As entries are concurrently being
481 * added to the new table, lock down the new bucket. As we
482 * always divide the size in half when shrinking, each bucket
483 * in the new table maps to exactly two buckets in the old
484 * table.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200485 */
Thomas Graf97defe12015-01-02 23:00:20 +0100486 for (new_hash = 0; new_hash < new_tbl->size; new_hash++) {
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100487 lock_buckets(new_tbl, tbl, new_hash);
Thomas Graf97defe12015-01-02 23:00:20 +0100488
489 rcu_assign_pointer(*bucket_tail(new_tbl, new_hash),
490 tbl->buckets[new_hash]);
Thomas Graf7cd10db2015-02-05 02:03:35 +0100491 ASSERT_BUCKET_LOCK(ht, tbl, new_hash + new_tbl->size);
Thomas Graf97defe12015-01-02 23:00:20 +0100492 rcu_assign_pointer(*bucket_tail(new_tbl, new_hash),
493 tbl->buckets[new_hash + new_tbl->size]);
494
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100495 unlock_buckets(new_tbl, tbl, new_hash);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200496 }
497
498 /* Publish the new, valid hash table */
Thomas Graf97defe12015-01-02 23:00:20 +0100499 rcu_assign_pointer(ht->tbl, new_tbl);
Ying Xuec0c09bf2015-01-07 13:41:56 +0800500 atomic_dec(&ht->shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200501
502 /* Wait for readers. No new readers will have references to the
503 * old hash table.
504 */
505 synchronize_rcu();
506
507 bucket_table_free(tbl);
508
509 return 0;
510}
511EXPORT_SYMBOL_GPL(rhashtable_shrink);
512
Thomas Graf97defe12015-01-02 23:00:20 +0100513static void rht_deferred_worker(struct work_struct *work)
514{
515 struct rhashtable *ht;
516 struct bucket_table *tbl;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100517 struct rhashtable_walker *walker;
Thomas Graf97defe12015-01-02 23:00:20 +0100518
Ying Xue57699a42015-01-16 11:13:09 +0800519 ht = container_of(work, struct rhashtable, run_work);
Thomas Graf97defe12015-01-02 23:00:20 +0100520 mutex_lock(&ht->mutex);
Herbert Xu28134a52015-02-04 07:33:22 +1100521 if (ht->being_destroyed)
522 goto unlock;
523
Thomas Graf97defe12015-01-02 23:00:20 +0100524 tbl = rht_dereference(ht->tbl, ht);
525
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100526 list_for_each_entry(walker, &ht->walkers, list)
527 walker->resize = true;
528
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100529 if (rht_grow_above_75(ht, tbl->size))
Thomas Graf97defe12015-01-02 23:00:20 +0100530 rhashtable_expand(ht);
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100531 else if (rht_shrink_below_30(ht, tbl->size))
Thomas Graf97defe12015-01-02 23:00:20 +0100532 rhashtable_shrink(ht);
Herbert Xu28134a52015-02-04 07:33:22 +1100533unlock:
Thomas Graf97defe12015-01-02 23:00:20 +0100534 mutex_unlock(&ht->mutex);
535}
536
Ying Xuedb304852015-01-07 13:41:54 +0800537static void __rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj,
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100538 struct bucket_table *tbl,
539 const struct bucket_table *old_tbl, u32 hash)
Ying Xuedb304852015-01-07 13:41:54 +0800540{
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100541 bool no_resize_running = tbl == old_tbl;
Thomas Graf020219a2015-02-06 16:08:43 +0000542 struct rhash_head *head;
543
544 hash = rht_bucket_index(tbl, hash);
545 head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash);
Ying Xuedb304852015-01-07 13:41:54 +0800546
Thomas Graf7cd10db2015-02-05 02:03:35 +0100547 ASSERT_BUCKET_LOCK(ht, tbl, hash);
548
Ying Xuedb304852015-01-07 13:41:54 +0800549 if (rht_is_a_nulls(head))
550 INIT_RHT_NULLS_HEAD(obj->next, ht, hash);
551 else
552 RCU_INIT_POINTER(obj->next, head);
553
554 rcu_assign_pointer(tbl->buckets[hash], obj);
555
556 atomic_inc(&ht->nelems);
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100557 if (no_resize_running && rht_grow_above_75(ht, tbl->size))
558 schedule_work(&ht->run_work);
Ying Xuedb304852015-01-07 13:41:54 +0800559}
560
Thomas Graf7e1e7762014-08-02 11:47:44 +0200561/**
Ying Xuedb304852015-01-07 13:41:54 +0800562 * rhashtable_insert - insert object into hash table
Thomas Graf7e1e7762014-08-02 11:47:44 +0200563 * @ht: hash table
564 * @obj: pointer to hash head inside object
Thomas Graf7e1e7762014-08-02 11:47:44 +0200565 *
Thomas Graf97defe12015-01-02 23:00:20 +0100566 * Will take a per bucket spinlock to protect against mutual mutations
567 * on the same bucket. Multiple insertions may occur in parallel unless
568 * they map to the same bucket lock.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200569 *
Thomas Graf97defe12015-01-02 23:00:20 +0100570 * It is safe to call this function from atomic context.
571 *
572 * Will trigger an automatic deferred table resizing if the size grows
573 * beyond the watermark indicated by grow_decision() which can be passed
574 * to rhashtable_init().
Thomas Graf7e1e7762014-08-02 11:47:44 +0200575 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100576void rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200577{
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100578 struct bucket_table *tbl, *old_tbl;
Thomas Graf97defe12015-01-02 23:00:20 +0100579 unsigned hash;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200580
Thomas Graf97defe12015-01-02 23:00:20 +0100581 rcu_read_lock();
Thomas Graf7e1e7762014-08-02 11:47:44 +0200582
Thomas Graf97defe12015-01-02 23:00:20 +0100583 tbl = rht_dereference_rcu(ht->future_tbl, ht);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100584 old_tbl = rht_dereference_rcu(ht->tbl, ht);
Thomas Graf020219a2015-02-06 16:08:43 +0000585 hash = obj_raw_hashfn(ht, rht_obj(ht, obj));
Thomas Graf97defe12015-01-02 23:00:20 +0100586
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100587 lock_buckets(tbl, old_tbl, hash);
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100588 __rhashtable_insert(ht, obj, tbl, old_tbl, hash);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100589 unlock_buckets(tbl, old_tbl, hash);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200590
Thomas Graf97defe12015-01-02 23:00:20 +0100591 rcu_read_unlock();
Thomas Graf7e1e7762014-08-02 11:47:44 +0200592}
593EXPORT_SYMBOL_GPL(rhashtable_insert);
594
595/**
Thomas Graf7e1e7762014-08-02 11:47:44 +0200596 * rhashtable_remove - remove object from hash table
597 * @ht: hash table
598 * @obj: pointer to hash head inside object
Thomas Graf7e1e7762014-08-02 11:47:44 +0200599 *
600 * Since the hash chain is single linked, the removal operation needs to
601 * walk the bucket chain upon removal. The removal operation is thus
602 * considerable slow if the hash table is not correctly sized.
603 *
Ying Xuedb304852015-01-07 13:41:54 +0800604 * Will automatically shrink the table via rhashtable_expand() if the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200605 * shrink_decision function specified at rhashtable_init() returns true.
606 *
607 * The caller must ensure that no concurrent table mutations occur. It is
608 * however valid to have concurrent lookups if they are RCU protected.
609 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100610bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *obj)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200611{
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100612 struct bucket_table *tbl, *new_tbl, *old_tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200613 struct rhash_head __rcu **pprev;
Thomas Grafcf52d522015-02-05 02:03:36 +0100614 struct rhash_head *he, *he2;
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100615 unsigned int hash, new_hash;
Thomas Graffe6a0432015-01-21 11:54:01 +0000616 bool ret = false;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200617
Thomas Graf97defe12015-01-02 23:00:20 +0100618 rcu_read_lock();
Thomas Graf020219a2015-02-06 16:08:43 +0000619 old_tbl = rht_dereference_rcu(ht->tbl, ht);
620 tbl = new_tbl = rht_dereference_rcu(ht->future_tbl, ht);
Thomas Grafcf52d522015-02-05 02:03:36 +0100621 new_hash = obj_raw_hashfn(ht, rht_obj(ht, obj));
Thomas Graf7e1e7762014-08-02 11:47:44 +0200622
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100623 lock_buckets(new_tbl, old_tbl, new_hash);
Thomas Graf97defe12015-01-02 23:00:20 +0100624restart:
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100625 hash = rht_bucket_index(tbl, new_hash);
Thomas Graf97defe12015-01-02 23:00:20 +0100626 pprev = &tbl->buckets[hash];
627 rht_for_each(he, tbl, hash) {
Thomas Graf7e1e7762014-08-02 11:47:44 +0200628 if (he != obj) {
629 pprev = &he->next;
630 continue;
631 }
632
Thomas Graf7cd10db2015-02-05 02:03:35 +0100633 ASSERT_BUCKET_LOCK(ht, tbl, hash);
Thomas Graf897362e2015-01-02 23:00:18 +0100634
Thomas Graf020219a2015-02-06 16:08:43 +0000635 if (old_tbl->size > new_tbl->size && tbl == old_tbl &&
636 !rht_is_a_nulls(obj->next) &&
637 head_hashfn(ht, tbl, obj->next) != hash) {
638 rcu_assign_pointer(*pprev, (struct rhash_head *) rht_marker(ht, hash));
639 } else if (unlikely(old_tbl->size < new_tbl->size && tbl == new_tbl)) {
640 rht_for_each_continue(he2, obj->next, tbl, hash) {
Thomas Grafcf52d522015-02-05 02:03:36 +0100641 if (head_hashfn(ht, tbl, he2) == hash) {
642 rcu_assign_pointer(*pprev, he2);
643 goto found;
644 }
645 }
646
Thomas Graf020219a2015-02-06 16:08:43 +0000647 rcu_assign_pointer(*pprev, (struct rhash_head *) rht_marker(ht, hash));
Thomas Grafcf52d522015-02-05 02:03:36 +0100648 } else {
649 rcu_assign_pointer(*pprev, obj->next);
650 }
651
652found:
Thomas Graffe6a0432015-01-21 11:54:01 +0000653 ret = true;
654 break;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200655 }
656
Thomas Graffe6a0432015-01-21 11:54:01 +0000657 /* The entry may be linked in either 'tbl', 'future_tbl', or both.
658 * 'future_tbl' only exists for a short period of time during
659 * resizing. Thus traversing both is fine and the added cost is
660 * very rare.
661 */
Thomas Graf020219a2015-02-06 16:08:43 +0000662 if (tbl != old_tbl) {
663 tbl = old_tbl;
Thomas Graf97defe12015-01-02 23:00:20 +0100664 goto restart;
665 }
666
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100667 unlock_buckets(new_tbl, old_tbl, new_hash);
Thomas Graffe6a0432015-01-21 11:54:01 +0000668
669 if (ret) {
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100670 bool no_resize_running = new_tbl == old_tbl;
671
Thomas Graffe6a0432015-01-21 11:54:01 +0000672 atomic_dec(&ht->nelems);
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100673 if (no_resize_running && rht_shrink_below_30(ht, new_tbl->size))
674 schedule_work(&ht->run_work);
Thomas Graffe6a0432015-01-21 11:54:01 +0000675 }
676
Thomas Graf97defe12015-01-02 23:00:20 +0100677 rcu_read_unlock();
678
Thomas Graffe6a0432015-01-21 11:54:01 +0000679 return ret;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200680}
681EXPORT_SYMBOL_GPL(rhashtable_remove);
682
Ying Xueefb975a62015-01-07 13:41:52 +0800683struct rhashtable_compare_arg {
684 struct rhashtable *ht;
685 const void *key;
686};
687
688static bool rhashtable_compare(void *ptr, void *arg)
689{
690 struct rhashtable_compare_arg *x = arg;
691 struct rhashtable *ht = x->ht;
692
693 return !memcmp(ptr + ht->p.key_offset, x->key, ht->p.key_len);
694}
695
Thomas Graf7e1e7762014-08-02 11:47:44 +0200696/**
697 * rhashtable_lookup - lookup key in hash table
698 * @ht: hash table
699 * @key: pointer to key
700 *
701 * Computes the hash value for the key and traverses the bucket chain looking
702 * for a entry with an identical key. The first matching entry is returned.
703 *
704 * This lookup function may only be used for fixed key hash table (key_len
Ying Xuedb304852015-01-07 13:41:54 +0800705 * parameter set). It will BUG() if used inappropriately.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200706 *
Thomas Graf97defe12015-01-02 23:00:20 +0100707 * Lookups may occur in parallel with hashtable mutations and resizing.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200708 */
Thomas Graf97defe12015-01-02 23:00:20 +0100709void *rhashtable_lookup(struct rhashtable *ht, const void *key)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200710{
Ying Xueefb975a62015-01-07 13:41:52 +0800711 struct rhashtable_compare_arg arg = {
712 .ht = ht,
713 .key = key,
714 };
Thomas Graf7e1e7762014-08-02 11:47:44 +0200715
716 BUG_ON(!ht->p.key_len);
717
Ying Xueefb975a62015-01-07 13:41:52 +0800718 return rhashtable_lookup_compare(ht, key, &rhashtable_compare, &arg);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200719}
720EXPORT_SYMBOL_GPL(rhashtable_lookup);
721
722/**
723 * rhashtable_lookup_compare - search hash table with compare function
724 * @ht: hash table
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100725 * @key: the pointer to the key
Thomas Graf7e1e7762014-08-02 11:47:44 +0200726 * @compare: compare function, must return true on match
727 * @arg: argument passed on to compare function
728 *
729 * Traverses the bucket chain behind the provided hash value and calls the
730 * specified compare function for each entry.
731 *
Thomas Graf97defe12015-01-02 23:00:20 +0100732 * Lookups may occur in parallel with hashtable mutations and resizing.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200733 *
734 * Returns the first entry on which the compare function returned true.
735 */
Thomas Graf97defe12015-01-02 23:00:20 +0100736void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200737 bool (*compare)(void *, void *), void *arg)
738{
Thomas Graf97defe12015-01-02 23:00:20 +0100739 const struct bucket_table *tbl, *old_tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200740 struct rhash_head *he;
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100741 u32 hash;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200742
Thomas Graf97defe12015-01-02 23:00:20 +0100743 rcu_read_lock();
744
745 old_tbl = rht_dereference_rcu(ht->tbl, ht);
746 tbl = rht_dereference_rcu(ht->future_tbl, ht);
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100747 hash = key_hashfn(ht, key, ht->p.key_len);
Thomas Graf97defe12015-01-02 23:00:20 +0100748restart:
749 rht_for_each_rcu(he, tbl, rht_bucket_index(tbl, hash)) {
Thomas Graf7e1e7762014-08-02 11:47:44 +0200750 if (!compare(rht_obj(ht, he), arg))
751 continue;
Thomas Graf97defe12015-01-02 23:00:20 +0100752 rcu_read_unlock();
Thomas Grafa4b18cd2015-01-02 23:00:15 +0100753 return rht_obj(ht, he);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200754 }
755
Thomas Graf97defe12015-01-02 23:00:20 +0100756 if (unlikely(tbl != old_tbl)) {
757 tbl = old_tbl;
758 goto restart;
759 }
760 rcu_read_unlock();
761
Thomas Graf7e1e7762014-08-02 11:47:44 +0200762 return NULL;
763}
764EXPORT_SYMBOL_GPL(rhashtable_lookup_compare);
765
Ying Xuedb304852015-01-07 13:41:54 +0800766/**
767 * rhashtable_lookup_insert - lookup and insert object into hash table
768 * @ht: hash table
769 * @obj: pointer to hash head inside object
770 *
771 * Locks down the bucket chain in both the old and new table if a resize
772 * is in progress to ensure that writers can't remove from the old table
773 * and can't insert to the new table during the atomic operation of search
774 * and insertion. Searches for duplicates in both the old and new table if
775 * a resize is in progress.
776 *
777 * This lookup function may only be used for fixed key hash table (key_len
778 * parameter set). It will BUG() if used inappropriately.
779 *
780 * It is safe to call this function from atomic context.
781 *
782 * Will trigger an automatic deferred table resizing if the size grows
783 * beyond the watermark indicated by grow_decision() which can be passed
784 * to rhashtable_init().
785 */
786bool rhashtable_lookup_insert(struct rhashtable *ht, struct rhash_head *obj)
787{
Ying Xue7a868d12015-01-12 14:52:22 +0800788 struct rhashtable_compare_arg arg = {
789 .ht = ht,
790 .key = rht_obj(ht, obj) + ht->p.key_offset,
791 };
792
793 BUG_ON(!ht->p.key_len);
794
795 return rhashtable_lookup_compare_insert(ht, obj, &rhashtable_compare,
796 &arg);
797}
798EXPORT_SYMBOL_GPL(rhashtable_lookup_insert);
799
800/**
801 * rhashtable_lookup_compare_insert - search and insert object to hash table
802 * with compare function
803 * @ht: hash table
804 * @obj: pointer to hash head inside object
805 * @compare: compare function, must return true on match
806 * @arg: argument passed on to compare function
807 *
808 * Locks down the bucket chain in both the old and new table if a resize
809 * is in progress to ensure that writers can't remove from the old table
810 * and can't insert to the new table during the atomic operation of search
811 * and insertion. Searches for duplicates in both the old and new table if
812 * a resize is in progress.
813 *
814 * Lookups may occur in parallel with hashtable mutations and resizing.
815 *
816 * Will trigger an automatic deferred table resizing if the size grows
817 * beyond the watermark indicated by grow_decision() which can be passed
818 * to rhashtable_init().
819 */
820bool rhashtable_lookup_compare_insert(struct rhashtable *ht,
821 struct rhash_head *obj,
822 bool (*compare)(void *, void *),
823 void *arg)
824{
Ying Xuedb304852015-01-07 13:41:54 +0800825 struct bucket_table *new_tbl, *old_tbl;
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100826 u32 new_hash;
Ying Xuedb304852015-01-07 13:41:54 +0800827 bool success = true;
828
829 BUG_ON(!ht->p.key_len);
830
831 rcu_read_lock();
Ying Xuedb304852015-01-07 13:41:54 +0800832 old_tbl = rht_dereference_rcu(ht->tbl, ht);
Ying Xuedb304852015-01-07 13:41:54 +0800833 new_tbl = rht_dereference_rcu(ht->future_tbl, ht);
Thomas Graf020219a2015-02-06 16:08:43 +0000834 new_hash = obj_raw_hashfn(ht, rht_obj(ht, obj));
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100835
836 lock_buckets(new_tbl, old_tbl, new_hash);
Ying Xuedb304852015-01-07 13:41:54 +0800837
Ying Xue7a868d12015-01-12 14:52:22 +0800838 if (rhashtable_lookup_compare(ht, rht_obj(ht, obj) + ht->p.key_offset,
839 compare, arg)) {
Ying Xuedb304852015-01-07 13:41:54 +0800840 success = false;
841 goto exit;
842 }
843
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100844 __rhashtable_insert(ht, obj, new_tbl, old_tbl, new_hash);
Ying Xuedb304852015-01-07 13:41:54 +0800845
846exit:
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100847 unlock_buckets(new_tbl, old_tbl, new_hash);
Ying Xuedb304852015-01-07 13:41:54 +0800848 rcu_read_unlock();
849
850 return success;
851}
Ying Xue7a868d12015-01-12 14:52:22 +0800852EXPORT_SYMBOL_GPL(rhashtable_lookup_compare_insert);
Ying Xuedb304852015-01-07 13:41:54 +0800853
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100854/**
855 * rhashtable_walk_init - Initialise an iterator
856 * @ht: Table to walk over
857 * @iter: Hash table Iterator
858 *
859 * This function prepares a hash table walk.
860 *
861 * Note that if you restart a walk after rhashtable_walk_stop you
862 * may see the same object twice. Also, you may miss objects if
863 * there are removals in between rhashtable_walk_stop and the next
864 * call to rhashtable_walk_start.
865 *
866 * For a completely stable walk you should construct your own data
867 * structure outside the hash table.
868 *
869 * This function may sleep so you must not call it from interrupt
870 * context or with spin locks held.
871 *
872 * You must call rhashtable_walk_exit if this function returns
873 * successfully.
874 */
875int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter)
876{
877 iter->ht = ht;
878 iter->p = NULL;
879 iter->slot = 0;
880 iter->skip = 0;
881
882 iter->walker = kmalloc(sizeof(*iter->walker), GFP_KERNEL);
883 if (!iter->walker)
884 return -ENOMEM;
885
Sasha Levin71bb0012015-02-23 04:35:06 -0500886 INIT_LIST_HEAD(&iter->walker->list);
887 iter->walker->resize = false;
888
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100889 mutex_lock(&ht->mutex);
890 list_add(&iter->walker->list, &ht->walkers);
891 mutex_unlock(&ht->mutex);
892
893 return 0;
894}
895EXPORT_SYMBOL_GPL(rhashtable_walk_init);
896
897/**
898 * rhashtable_walk_exit - Free an iterator
899 * @iter: Hash table Iterator
900 *
901 * This function frees resources allocated by rhashtable_walk_init.
902 */
903void rhashtable_walk_exit(struct rhashtable_iter *iter)
904{
905 mutex_lock(&iter->ht->mutex);
906 list_del(&iter->walker->list);
907 mutex_unlock(&iter->ht->mutex);
908 kfree(iter->walker);
909}
910EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
911
912/**
913 * rhashtable_walk_start - Start a hash table walk
914 * @iter: Hash table iterator
915 *
916 * Start a hash table walk. Note that we take the RCU lock in all
917 * cases including when we return an error. So you must always call
918 * rhashtable_walk_stop to clean up.
919 *
920 * Returns zero if successful.
921 *
922 * Returns -EAGAIN if resize event occured. Note that the iterator
923 * will rewind back to the beginning and you may use it immediately
924 * by calling rhashtable_walk_next.
925 */
926int rhashtable_walk_start(struct rhashtable_iter *iter)
927{
928 rcu_read_lock();
929
930 if (iter->walker->resize) {
931 iter->slot = 0;
932 iter->skip = 0;
933 iter->walker->resize = false;
934 return -EAGAIN;
935 }
936
937 return 0;
938}
939EXPORT_SYMBOL_GPL(rhashtable_walk_start);
940
941/**
942 * rhashtable_walk_next - Return the next object and advance the iterator
943 * @iter: Hash table iterator
944 *
945 * Note that you must call rhashtable_walk_stop when you are finished
946 * with the walk.
947 *
948 * Returns the next object or NULL when the end of the table is reached.
949 *
950 * Returns -EAGAIN if resize event occured. Note that the iterator
951 * will rewind back to the beginning and you may continue to use it.
952 */
953void *rhashtable_walk_next(struct rhashtable_iter *iter)
954{
955 const struct bucket_table *tbl;
956 struct rhashtable *ht = iter->ht;
957 struct rhash_head *p = iter->p;
958 void *obj = NULL;
959
960 tbl = rht_dereference_rcu(ht->tbl, ht);
961
962 if (p) {
963 p = rht_dereference_bucket_rcu(p->next, tbl, iter->slot);
964 goto next;
965 }
966
967 for (; iter->slot < tbl->size; iter->slot++) {
968 int skip = iter->skip;
969
970 rht_for_each_rcu(p, tbl, iter->slot) {
971 if (!skip)
972 break;
973 skip--;
974 }
975
976next:
977 if (!rht_is_a_nulls(p)) {
978 iter->skip++;
979 iter->p = p;
980 obj = rht_obj(ht, p);
981 goto out;
982 }
983
984 iter->skip = 0;
985 }
986
987 iter->p = NULL;
988
989out:
990 if (iter->walker->resize) {
991 iter->p = NULL;
992 iter->slot = 0;
993 iter->skip = 0;
994 iter->walker->resize = false;
995 return ERR_PTR(-EAGAIN);
996 }
997
998 return obj;
999}
1000EXPORT_SYMBOL_GPL(rhashtable_walk_next);
1001
1002/**
1003 * rhashtable_walk_stop - Finish a hash table walk
1004 * @iter: Hash table iterator
1005 *
1006 * Finish a hash table walk.
1007 */
1008void rhashtable_walk_stop(struct rhashtable_iter *iter)
1009{
1010 rcu_read_unlock();
1011 iter->p = NULL;
1012}
1013EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
1014
Ying Xue94000172014-09-03 09:22:36 +08001015static size_t rounded_hashtable_size(struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +02001016{
Ying Xue94000172014-09-03 09:22:36 +08001017 return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
1018 1UL << params->min_shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +02001019}
1020
1021/**
1022 * rhashtable_init - initialize a new hash table
1023 * @ht: hash table to be initialized
1024 * @params: configuration parameters
1025 *
1026 * Initializes a new hash table based on the provided configuration
1027 * parameters. A table can be configured either with a variable or
1028 * fixed length key:
1029 *
1030 * Configuration Example 1: Fixed length keys
1031 * struct test_obj {
1032 * int key;
1033 * void * my_member;
1034 * struct rhash_head node;
1035 * };
1036 *
1037 * struct rhashtable_params params = {
1038 * .head_offset = offsetof(struct test_obj, node),
1039 * .key_offset = offsetof(struct test_obj, key),
1040 * .key_len = sizeof(int),
Daniel Borkmann87545892014-12-10 16:33:11 +01001041 * .hashfn = jhash,
Thomas Graff89bd6f2015-01-02 23:00:21 +01001042 * .nulls_base = (1U << RHT_BASE_SHIFT),
Thomas Graf7e1e7762014-08-02 11:47:44 +02001043 * };
1044 *
1045 * Configuration Example 2: Variable length keys
1046 * struct test_obj {
1047 * [...]
1048 * struct rhash_head node;
1049 * };
1050 *
1051 * u32 my_hash_fn(const void *data, u32 seed)
1052 * {
1053 * struct test_obj *obj = data;
1054 *
1055 * return [... hash ...];
1056 * }
1057 *
1058 * struct rhashtable_params params = {
1059 * .head_offset = offsetof(struct test_obj, node),
Daniel Borkmann87545892014-12-10 16:33:11 +01001060 * .hashfn = jhash,
Thomas Graf7e1e7762014-08-02 11:47:44 +02001061 * .obj_hashfn = my_hash_fn,
Thomas Graf7e1e7762014-08-02 11:47:44 +02001062 * };
1063 */
1064int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params)
1065{
1066 struct bucket_table *tbl;
1067 size_t size;
1068
1069 size = HASH_DEFAULT_SIZE;
1070
1071 if ((params->key_len && !params->hashfn) ||
1072 (!params->key_len && !params->obj_hashfn))
1073 return -EINVAL;
1074
Thomas Graff89bd6f2015-01-02 23:00:21 +01001075 if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT))
1076 return -EINVAL;
1077
Ying Xue94000172014-09-03 09:22:36 +08001078 params->min_shift = max_t(size_t, params->min_shift,
1079 ilog2(HASH_MIN_SIZE));
1080
Thomas Graf7e1e7762014-08-02 11:47:44 +02001081 if (params->nelem_hint)
Ying Xue94000172014-09-03 09:22:36 +08001082 size = rounded_hashtable_size(params);
Thomas Graf7e1e7762014-08-02 11:47:44 +02001083
Thomas Graf97defe12015-01-02 23:00:20 +01001084 memset(ht, 0, sizeof(*ht));
1085 mutex_init(&ht->mutex);
1086 memcpy(&ht->p, params, sizeof(*params));
Herbert Xuf2dba9c2015-02-04 07:33:23 +11001087 INIT_LIST_HEAD(&ht->walkers);
Thomas Graf97defe12015-01-02 23:00:20 +01001088
1089 if (params->locks_mul)
1090 ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
1091 else
1092 ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
1093
1094 tbl = bucket_table_alloc(ht, size);
Thomas Graf7e1e7762014-08-02 11:47:44 +02001095 if (tbl == NULL)
1096 return -ENOMEM;
1097
Ying Xue545a1482015-01-07 13:41:57 +08001098 atomic_set(&ht->nelems, 0);
Ying Xuec0c09bf2015-01-07 13:41:56 +08001099 atomic_set(&ht->shift, ilog2(tbl->size));
Thomas Graf7e1e7762014-08-02 11:47:44 +02001100 RCU_INIT_POINTER(ht->tbl, tbl);
Thomas Graf97defe12015-01-02 23:00:20 +01001101 RCU_INIT_POINTER(ht->future_tbl, tbl);
Thomas Graf7e1e7762014-08-02 11:47:44 +02001102
1103 if (!ht->p.hash_rnd)
1104 get_random_bytes(&ht->p.hash_rnd, sizeof(ht->p.hash_rnd));
1105
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +01001106 INIT_WORK(&ht->run_work, rht_deferred_worker);
Thomas Graf97defe12015-01-02 23:00:20 +01001107
Thomas Graf7e1e7762014-08-02 11:47:44 +02001108 return 0;
1109}
1110EXPORT_SYMBOL_GPL(rhashtable_init);
1111
1112/**
1113 * rhashtable_destroy - destroy hash table
1114 * @ht: the hash table to destroy
1115 *
Pablo Neira Ayusoae82ddc2014-09-02 00:26:05 +02001116 * Frees the bucket array. This function is not rcu safe, therefore the caller
1117 * has to make sure that no resizing may happen by unpublishing the hashtable
1118 * and waiting for the quiescent cycle before releasing the bucket array.
Thomas Graf7e1e7762014-08-02 11:47:44 +02001119 */
Thomas Graf97defe12015-01-02 23:00:20 +01001120void rhashtable_destroy(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +02001121{
Thomas Graf97defe12015-01-02 23:00:20 +01001122 ht->being_destroyed = true;
1123
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +01001124 cancel_work_sync(&ht->run_work);
Ying Xue57699a42015-01-16 11:13:09 +08001125
Thomas Graf97defe12015-01-02 23:00:20 +01001126 mutex_lock(&ht->mutex);
Thomas Graf97defe12015-01-02 23:00:20 +01001127 bucket_table_free(rht_dereference(ht->tbl, ht));
Thomas Graf97defe12015-01-02 23:00:20 +01001128 mutex_unlock(&ht->mutex);
Thomas Graf7e1e7762014-08-02 11:47:44 +02001129}
1130EXPORT_SYMBOL_GPL(rhashtable_destroy);