]> git.sesse.net Git - bcachefs-tools-debian/blob - include/linux/rhashtable.h
adeef32932fa8aaee85a792c0965490ac6b52658
[bcachefs-tools-debian] / include / linux / rhashtable.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Resizable, Scalable, Concurrent Hash Table
4  *
5  * Copyright (c) 2015-2016 Herbert Xu <herbert@gondor.apana.org.au>
6  * Copyright (c) 2014-2015 Thomas Graf <tgraf@suug.ch>
7  * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
8  *
9  * Code partially derived from nft_hash
10  * Rewritten with rehash code from br_multicast plus single list
11  * pointer as suggested by Josh Triplett
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License version 2 as
15  * published by the Free Software Foundation.
16  */
17
18 #ifndef _LINUX_RHASHTABLE_H
19 #define _LINUX_RHASHTABLE_H
20
21 #include <linux/err.h>
22 #include <linux/errno.h>
23 #include <linux/jhash.h>
24 #include <linux/list_nulls.h>
25 #include <linux/rcupdate.h>
26 #include <linux/workqueue.h>
27 #include <linux/rculist.h>
28 #include <linux/bit_spinlock.h>
29
30 #define BIT(nr)                 (1UL << (nr))
31 #define BIT_ULL(nr)             (1ULL << (nr))
32
33 #include <linux/rhashtable-types.h>
34 /*
35  * Objects in an rhashtable have an embedded struct rhash_head
36  * which is linked into as hash chain from the hash table - or one
37  * of two or more hash tables when the rhashtable is being resized.
38  * The end of the chain is marked with a special nulls marks which has
39  * the least significant bit set but otherwise stores the address of
40  * the hash bucket.  This allows us to be sure we've found the end
41  * of the right list.
42  * The value stored in the hash bucket has BIT(0) used as a lock bit.
43  * This bit must be atomically set before any changes are made to
44  * the chain.  To avoid dereferencing this pointer without clearing
45  * the bit first, we use an opaque 'struct rhash_lock_head *' for the
46  * pointer stored in the bucket.  This struct needs to be defined so
47  * that rcu_dereference() works on it, but it has no content so a
48  * cast is needed for it to be useful.  This ensures it isn't
49  * used by mistake with clearing the lock bit first.
50  */
51 struct rhash_lock_head {};
52
53 /* Maximum chain length before rehash
54  *
55  * The maximum (not average) chain length grows with the size of the hash
56  * table, at a rate of (log N)/(log log N).
57  *
58  * The value of 16 is selected so that even if the hash table grew to
59  * 2^32 you would not expect the maximum chain length to exceed it
60  * unless we are under attack (or extremely unlucky).
61  *
62  * As this limit is only to detect attacks, we don't need to set it to a
63  * lower value as you'd need the chain length to vastly exceed 16 to have
64  * any real effect on the system.
65  */
66 #define RHT_ELASTICITY  16u
67
68 /**
69  * struct bucket_table - Table of hash buckets
70  * @size: Number of hash buckets
71  * @nest: Number of bits of first-level nested table.
72  * @rehash: Current bucket being rehashed
73  * @hash_rnd: Random seed to fold into hash
74  * @walkers: List of active walkers
75  * @rcu: RCU structure for freeing the table
76  * @future_tbl: Table under construction during rehashing
77  * @ntbl: Nested table used when out of memory.
78  * @buckets: size * hash buckets
79  */
80 struct bucket_table {
81         unsigned int            size;
82         unsigned int            nest;
83         u32                     hash_rnd;
84         struct list_head        walkers;
85         struct rcu_head         rcu;
86
87         struct bucket_table __rcu *future_tbl;
88
89         struct rhash_lock_head __rcu *buckets[] ____cacheline_aligned_in_smp;
90 };
91
92 /*
93  * NULLS_MARKER() expects a hash value with the low
94  * bits mostly likely to be significant, and it discards
95  * the msb.
96  * We give it an address, in which the bottom bit is
97  * always 0, and the msb might be significant.
98  * So we shift the address down one bit to align with
99  * expectations and avoid losing a significant bit.
100  *
101  * We never store the NULLS_MARKER in the hash table
102  * itself as we need the lsb for locking.
103  * Instead we store a NULL
104  */
105 #define RHT_NULLS_MARKER(ptr)   \
106         ((void *)NULLS_MARKER(((unsigned long) (ptr)) >> 1))
107 #define INIT_RHT_NULLS_HEAD(ptr)        \
108         ((ptr) = NULL)
109
110 static inline bool rht_is_a_nulls(const struct rhash_head *ptr)
111 {
112         return ((unsigned long) ptr & 1);
113 }
114
115 static inline void *rht_obj(const struct rhashtable *ht,
116                             const struct rhash_head *he)
117 {
118         return (char *)he - ht->p.head_offset;
119 }
120
121 static inline unsigned int rht_bucket_index(const struct bucket_table *tbl,
122                                             unsigned int hash)
123 {
124         return hash & (tbl->size - 1);
125 }
126
127 static inline unsigned int rht_key_get_hash(struct rhashtable *ht,
128         const void *key, const struct rhashtable_params params,
129         unsigned int hash_rnd)
130 {
131         unsigned int hash;
132
133         /* params must be equal to ht->p if it isn't constant. */
134         if (!__builtin_constant_p(params.key_len))
135                 hash = ht->p.hashfn(key, ht->key_len, hash_rnd);
136         else if (params.key_len) {
137                 unsigned int key_len = params.key_len;
138
139                 if (params.hashfn)
140                         hash = params.hashfn(key, key_len, hash_rnd);
141                 else if (key_len & (sizeof(u32) - 1))
142                         hash = jhash(key, key_len, hash_rnd);
143                 else
144                         hash = jhash2(key, key_len / sizeof(u32), hash_rnd);
145         } else {
146                 unsigned int key_len = ht->p.key_len;
147
148                 if (params.hashfn)
149                         hash = params.hashfn(key, key_len, hash_rnd);
150                 else
151                         hash = jhash(key, key_len, hash_rnd);
152         }
153
154         return hash;
155 }
156
157 static inline unsigned int rht_key_hashfn(
158         struct rhashtable *ht, const struct bucket_table *tbl,
159         const void *key, const struct rhashtable_params params)
160 {
161         unsigned int hash = rht_key_get_hash(ht, key, params, tbl->hash_rnd);
162
163         return rht_bucket_index(tbl, hash);
164 }
165
166 static inline unsigned int rht_head_hashfn(
167         struct rhashtable *ht, const struct bucket_table *tbl,
168         const struct rhash_head *he, const struct rhashtable_params params)
169 {
170         const char *ptr = rht_obj(ht, he);
171
172         return likely(params.obj_hashfn) ?
173                rht_bucket_index(tbl, params.obj_hashfn(ptr, params.key_len ?:
174                                                             ht->p.key_len,
175                                                        tbl->hash_rnd)) :
176                rht_key_hashfn(ht, tbl, ptr + params.key_offset, params);
177 }
178
179 /**
180  * rht_grow_above_75 - returns true if nelems > 0.75 * table-size
181  * @ht:         hash table
182  * @tbl:        current table
183  */
184 static inline bool rht_grow_above_75(const struct rhashtable *ht,
185                                      const struct bucket_table *tbl)
186 {
187         /* Expand table when exceeding 75% load */
188         return atomic_read(&ht->nelems) > (tbl->size / 4 * 3) &&
189                (!ht->p.max_size || tbl->size < ht->p.max_size);
190 }
191
192 /**
193  * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size
194  * @ht:         hash table
195  * @tbl:        current table
196  */
197 static inline bool rht_shrink_below_30(const struct rhashtable *ht,
198                                        const struct bucket_table *tbl)
199 {
200         /* Shrink table beneath 30% load */
201         return atomic_read(&ht->nelems) < (tbl->size * 3 / 10) &&
202                tbl->size > ht->p.min_size;
203 }
204
205 /**
206  * rht_grow_above_100 - returns true if nelems > table-size
207  * @ht:         hash table
208  * @tbl:        current table
209  */
210 static inline bool rht_grow_above_100(const struct rhashtable *ht,
211                                       const struct bucket_table *tbl)
212 {
213         return atomic_read(&ht->nelems) > tbl->size &&
214                 (!ht->p.max_size || tbl->size < ht->p.max_size);
215 }
216
217 /**
218  * rht_grow_above_max - returns true if table is above maximum
219  * @ht:         hash table
220  * @tbl:        current table
221  */
222 static inline bool rht_grow_above_max(const struct rhashtable *ht,
223                                       const struct bucket_table *tbl)
224 {
225         return atomic_read(&ht->nelems) >= ht->max_elems;
226 }
227
228 #ifdef CONFIG_PROVE_LOCKING
229 int lockdep_rht_mutex_is_held(struct rhashtable *ht);
230 int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash);
231 #else
232 static inline int lockdep_rht_mutex_is_held(struct rhashtable *ht)
233 {
234         return 1;
235 }
236
237 static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl,
238                                              u32 hash)
239 {
240         return 1;
241 }
242 #endif /* CONFIG_PROVE_LOCKING */
243
244 void *rhashtable_insert_slow(struct rhashtable *ht, const void *key,
245                              struct rhash_head *obj);
246
247 void rhashtable_walk_enter(struct rhashtable *ht,
248                            struct rhashtable_iter *iter);
249 void rhashtable_walk_exit(struct rhashtable_iter *iter);
250 int rhashtable_walk_start_check(struct rhashtable_iter *iter) __acquires(RCU);
251
252 static inline void rhashtable_walk_start(struct rhashtable_iter *iter)
253 {
254         (void)rhashtable_walk_start_check(iter);
255 }
256
257 void *rhashtable_walk_next(struct rhashtable_iter *iter);
258 void *rhashtable_walk_peek(struct rhashtable_iter *iter);
259 void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU);
260
261 void rhashtable_free_and_destroy(struct rhashtable *ht,
262                                  void (*free_fn)(void *ptr, void *arg),
263                                  void *arg);
264 void rhashtable_destroy(struct rhashtable *ht);
265
266 struct rhash_lock_head __rcu **rht_bucket_nested(
267         const struct bucket_table *tbl, unsigned int hash);
268 struct rhash_lock_head __rcu **__rht_bucket_nested(
269         const struct bucket_table *tbl, unsigned int hash);
270 struct rhash_lock_head __rcu **rht_bucket_nested_insert(
271         struct rhashtable *ht, struct bucket_table *tbl, unsigned int hash);
272
273 #define rht_dereference(p, ht) \
274         rcu_dereference(p)
275
276 #define rht_dereference_rcu(p, ht) \
277         rcu_dereference(p)
278
279 #define rht_dereference_bucket(p, tbl, hash) \
280         rcu_dereference(p)
281
282 #define rht_dereference_bucket_rcu(p, tbl, hash) \
283         rcu_dereference(p)
284
285 #define rht_entry(tpos, pos, member) \
286         ({ tpos = container_of(pos, typeof(*tpos), member); 1; })
287
288 static inline struct rhash_lock_head __rcu *const *rht_bucket(
289         const struct bucket_table *tbl, unsigned int hash)
290 {
291         return unlikely(tbl->nest) ? rht_bucket_nested(tbl, hash) :
292                                      &tbl->buckets[hash];
293 }
294
295 static inline struct rhash_lock_head __rcu **rht_bucket_var(
296         struct bucket_table *tbl, unsigned int hash)
297 {
298         return unlikely(tbl->nest) ? __rht_bucket_nested(tbl, hash) :
299                                      &tbl->buckets[hash];
300 }
301
302 static inline struct rhash_lock_head __rcu **rht_bucket_insert(
303         struct rhashtable *ht, struct bucket_table *tbl, unsigned int hash)
304 {
305         return unlikely(tbl->nest) ? rht_bucket_nested_insert(ht, tbl, hash) :
306                                      &tbl->buckets[hash];
307 }
308
309 /*
310  * We lock a bucket by setting BIT(0) in the pointer - this is always
311  * zero in real pointers.  The NULLS mark is never stored in the bucket,
312  * rather we store NULL if the bucket is empty.
313  * bit_spin_locks do not handle contention well, but the whole point
314  * of the hashtable design is to achieve minimum per-bucket contention.
315  * A nested hash table might not have a bucket pointer.  In that case
316  * we cannot get a lock.  For remove and replace the bucket cannot be
317  * interesting and doesn't need locking.
318  * For insert we allocate the bucket if this is the last bucket_table,
319  * and then take the lock.
320  * Sometimes we unlock a bucket by writing a new pointer there.  In that
321  * case we don't need to unlock, but we do need to reset state such as
322  * local_bh. For that we have rht_assign_unlock().  As rcu_assign_pointer()
323  * provides the same release semantics that bit_spin_unlock() provides,
324  * this is safe.
325  * When we write to a bucket without unlocking, we use rht_assign_locked().
326  */
327
328 static inline void rht_lock(struct bucket_table *tbl,
329                             struct rhash_lock_head __rcu **bkt)
330 {
331         bit_spin_lock(0, (unsigned long *)bkt);
332 }
333
334 static inline void rht_lock_nested(struct bucket_table *tbl,
335                                    struct rhash_lock_head __rcu **bucket,
336                                    unsigned int subclass)
337 {
338         bit_spin_lock(0, (unsigned long *)bucket);
339 }
340
341 static inline void rht_unlock(struct bucket_table *tbl,
342                               struct rhash_lock_head __rcu **bkt)
343 {
344         bit_spin_unlock(0, (unsigned long *)bkt);
345 }
346
347 static inline struct rhash_head *__rht_ptr(
348         struct rhash_lock_head *p, struct rhash_lock_head __rcu *const *bkt)
349 {
350         return (struct rhash_head *)
351                 ((unsigned long)p & ~BIT(0) ?:
352                  (unsigned long)RHT_NULLS_MARKER(bkt));
353 }
354
355 /*
356  * Where 'bkt' is a bucket and might be locked:
357  *   rht_ptr_rcu() dereferences that pointer and clears the lock bit.
358  *   rht_ptr() dereferences in a context where the bucket is locked.
359  *   rht_ptr_exclusive() dereferences in a context where exclusive
360  *            access is guaranteed, such as when destroying the table.
361  */
362 static inline struct rhash_head *rht_ptr_rcu(
363         struct rhash_lock_head __rcu *const *bkt)
364 {
365         return __rht_ptr(rcu_dereference(*bkt), bkt);
366 }
367
368 static inline struct rhash_head *rht_ptr(
369         struct rhash_lock_head __rcu *const *bkt,
370         struct bucket_table *tbl,
371         unsigned int hash)
372 {
373         return __rht_ptr(rht_dereference_bucket(*bkt, tbl, hash), bkt);
374 }
375
376 static inline struct rhash_head *rht_ptr_exclusive(
377         struct rhash_lock_head __rcu *const *bkt)
378 {
379         return __rht_ptr(rcu_dereference(*bkt), bkt);
380 }
381
382 static inline void rht_assign_locked(struct rhash_lock_head __rcu **bkt,
383                                      struct rhash_head *obj)
384 {
385         if (rht_is_a_nulls(obj))
386                 obj = NULL;
387         rcu_assign_pointer(*bkt, (void *)((unsigned long)obj | BIT(0)));
388 }
389
390 static inline void rht_assign_unlock(struct bucket_table *tbl,
391                                      struct rhash_lock_head __rcu **bkt,
392                                      struct rhash_head *obj)
393 {
394         if (rht_is_a_nulls(obj))
395                 obj = NULL;
396         rcu_assign_pointer(*bkt, (void *)obj);
397         preempt_enable();
398         __release(bitlock);
399         bit_spin_wake(0, (unsigned long *) bkt);
400 }
401
402 /**
403  * rht_for_each_from - iterate over hash chain from given head
404  * @pos:        the &struct rhash_head to use as a loop cursor.
405  * @head:       the &struct rhash_head to start from
406  * @tbl:        the &struct bucket_table
407  * @hash:       the hash value / bucket index
408  */
409 #define rht_for_each_from(pos, head, tbl, hash) \
410         for (pos = head;                        \
411              !rht_is_a_nulls(pos);              \
412              pos = rht_dereference_bucket((pos)->next, tbl, hash))
413
414 /**
415  * rht_for_each - iterate over hash chain
416  * @pos:        the &struct rhash_head to use as a loop cursor.
417  * @tbl:        the &struct bucket_table
418  * @hash:       the hash value / bucket index
419  */
420 #define rht_for_each(pos, tbl, hash) \
421         rht_for_each_from(pos, rht_ptr(rht_bucket(tbl, hash), tbl, hash),  \
422                           tbl, hash)
423
424 /**
425  * rht_for_each_entry_from - iterate over hash chain from given head
426  * @tpos:       the type * to use as a loop cursor.
427  * @pos:        the &struct rhash_head to use as a loop cursor.
428  * @head:       the &struct rhash_head to start from
429  * @tbl:        the &struct bucket_table
430  * @hash:       the hash value / bucket index
431  * @member:     name of the &struct rhash_head within the hashable struct.
432  */
433 #define rht_for_each_entry_from(tpos, pos, head, tbl, hash, member)     \
434         for (pos = head;                                                \
435              (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member);    \
436              pos = rht_dereference_bucket((pos)->next, tbl, hash))
437
438 /**
439  * rht_for_each_entry - iterate over hash chain of given type
440  * @tpos:       the type * to use as a loop cursor.
441  * @pos:        the &struct rhash_head to use as a loop cursor.
442  * @tbl:        the &struct bucket_table
443  * @hash:       the hash value / bucket index
444  * @member:     name of the &struct rhash_head within the hashable struct.
445  */
446 #define rht_for_each_entry(tpos, pos, tbl, hash, member)                \
447         rht_for_each_entry_from(tpos, pos,                              \
448                                 rht_ptr(rht_bucket(tbl, hash), tbl, hash), \
449                                 tbl, hash, member)
450
451 /**
452  * rht_for_each_entry_safe - safely iterate over hash chain of given type
453  * @tpos:       the type * to use as a loop cursor.
454  * @pos:        the &struct rhash_head to use as a loop cursor.
455  * @next:       the &struct rhash_head to use as next in loop cursor.
456  * @tbl:        the &struct bucket_table
457  * @hash:       the hash value / bucket index
458  * @member:     name of the &struct rhash_head within the hashable struct.
459  *
460  * This hash chain list-traversal primitive allows for the looped code to
461  * remove the loop cursor from the list.
462  */
463 #define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member)           \
464         for (pos = rht_ptr(rht_bucket(tbl, hash), tbl, hash),                 \
465              next = !rht_is_a_nulls(pos) ?                                    \
466                        rht_dereference_bucket(pos->next, tbl, hash) : NULL;   \
467              (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member);          \
468              pos = next,                                                      \
469              next = !rht_is_a_nulls(pos) ?                                    \
470                        rht_dereference_bucket(pos->next, tbl, hash) : NULL)
471
472 /**
473  * rht_for_each_rcu_from - iterate over rcu hash chain from given head
474  * @pos:        the &struct rhash_head to use as a loop cursor.
475  * @head:       the &struct rhash_head to start from
476  * @tbl:        the &struct bucket_table
477  * @hash:       the hash value / bucket index
478  *
479  * This hash chain list-traversal primitive may safely run concurrently with
480  * the _rcu mutation primitives such as rhashtable_insert() as long as the
481  * traversal is guarded by rcu_read_lock().
482  */
483 #define rht_for_each_rcu_from(pos, head, tbl, hash)                     \
484         for (({barrier(); }),                                           \
485              pos = head;                                                \
486              !rht_is_a_nulls(pos);                                      \
487              pos = rcu_dereference_raw(pos->next))
488
489 /**
490  * rht_for_each_rcu - iterate over rcu hash chain
491  * @pos:        the &struct rhash_head to use as a loop cursor.
492  * @tbl:        the &struct bucket_table
493  * @hash:       the hash value / bucket index
494  *
495  * This hash chain list-traversal primitive may safely run concurrently with
496  * the _rcu mutation primitives such as rhashtable_insert() as long as the
497  * traversal is guarded by rcu_read_lock().
498  */
499 #define rht_for_each_rcu(pos, tbl, hash)                        \
500         for (({barrier(); }),                                   \
501              pos = rht_ptr_rcu(rht_bucket(tbl, hash));          \
502              !rht_is_a_nulls(pos);                              \
503              pos = rcu_dereference_raw(pos->next))
504
505 /**
506  * rht_for_each_entry_rcu_from - iterated over rcu hash chain from given head
507  * @tpos:       the type * to use as a loop cursor.
508  * @pos:        the &struct rhash_head to use as a loop cursor.
509  * @head:       the &struct rhash_head to start from
510  * @tbl:        the &struct bucket_table
511  * @hash:       the hash value / bucket index
512  * @member:     name of the &struct rhash_head within the hashable struct.
513  *
514  * This hash chain list-traversal primitive may safely run concurrently with
515  * the _rcu mutation primitives such as rhashtable_insert() as long as the
516  * traversal is guarded by rcu_read_lock().
517  */
518 #define rht_for_each_entry_rcu_from(tpos, pos, head, tbl, hash, member) \
519         for (({barrier(); }),                                               \
520              pos = head;                                                    \
521              (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member);        \
522              pos = rht_dereference_bucket_rcu(pos->next, tbl, hash))
523
524 /**
525  * rht_for_each_entry_rcu - iterate over rcu hash chain of given type
526  * @tpos:       the type * to use as a loop cursor.
527  * @pos:        the &struct rhash_head to use as a loop cursor.
528  * @tbl:        the &struct bucket_table
529  * @hash:       the hash value / bucket index
530  * @member:     name of the &struct rhash_head within the hashable struct.
531  *
532  * This hash chain list-traversal primitive may safely run concurrently with
533  * the _rcu mutation primitives such as rhashtable_insert() as long as the
534  * traversal is guarded by rcu_read_lock().
535  */
536 #define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member)               \
537         rht_for_each_entry_rcu_from(tpos, pos,                             \
538                                     rht_ptr_rcu(rht_bucket(tbl, hash)),    \
539                                     tbl, hash, member)
540
541 /**
542  * rhl_for_each_rcu - iterate over rcu hash table list
543  * @pos:        the &struct rlist_head to use as a loop cursor.
544  * @list:       the head of the list
545  *
546  * This hash chain list-traversal primitive should be used on the
547  * list returned by rhltable_lookup.
548  */
549 #define rhl_for_each_rcu(pos, list)                                     \
550         for (pos = list; pos; pos = rcu_dereference_raw(pos->next))
551
552 /**
553  * rhl_for_each_entry_rcu - iterate over rcu hash table list of given type
554  * @tpos:       the type * to use as a loop cursor.
555  * @pos:        the &struct rlist_head to use as a loop cursor.
556  * @list:       the head of the list
557  * @member:     name of the &struct rlist_head within the hashable struct.
558  *
559  * This hash chain list-traversal primitive should be used on the
560  * list returned by rhltable_lookup.
561  */
562 #define rhl_for_each_entry_rcu(tpos, pos, list, member)                 \
563         for (pos = list; pos && rht_entry(tpos, pos, member);           \
564              pos = rcu_dereference_raw(pos->next))
565
566 static inline int rhashtable_compare(struct rhashtable_compare_arg *arg,
567                                      const void *obj)
568 {
569         struct rhashtable *ht = arg->ht;
570         const char *ptr = obj;
571
572         return memcmp(ptr + ht->p.key_offset, arg->key, ht->p.key_len);
573 }
574
575 /* Internal function, do not use. */
576 static inline struct rhash_head *__rhashtable_lookup(
577         struct rhashtable *ht, const void *key,
578         const struct rhashtable_params params)
579 {
580         struct rhashtable_compare_arg arg = {
581                 .ht = ht,
582                 .key = key,
583         };
584         struct rhash_lock_head __rcu *const *bkt;
585         struct bucket_table *tbl;
586         struct rhash_head *he;
587         unsigned int hash;
588
589         tbl = rht_dereference_rcu(ht->tbl, ht);
590 restart:
591         hash = rht_key_hashfn(ht, tbl, key, params);
592         bkt = rht_bucket(tbl, hash);
593         do {
594                 rht_for_each_rcu_from(he, rht_ptr_rcu(bkt), tbl, hash) {
595                         if (params.obj_cmpfn ?
596                             params.obj_cmpfn(&arg, rht_obj(ht, he)) :
597                             rhashtable_compare(&arg, rht_obj(ht, he)))
598                                 continue;
599                         return he;
600                 }
601                 /* An object might have been moved to a different hash chain,
602                  * while we walk along it - better check and retry.
603                  */
604         } while (he != RHT_NULLS_MARKER(bkt));
605
606         /* Ensure we see any new tables. */
607         smp_rmb();
608
609         tbl = rht_dereference_rcu(tbl->future_tbl, ht);
610         if (unlikely(tbl))
611                 goto restart;
612
613         return NULL;
614 }
615
616 /**
617  * rhashtable_lookup - search hash table
618  * @ht:         hash table
619  * @key:        the pointer to the key
620  * @params:     hash table parameters
621  *
622  * Computes the hash value for the key and traverses the bucket chain looking
623  * for a entry with an identical key. The first matching entry is returned.
624  *
625  * This must only be called under the RCU read lock.
626  *
627  * Returns the first entry on which the compare function returned true.
628  */
629 static inline void *rhashtable_lookup(
630         struct rhashtable *ht, const void *key,
631         const struct rhashtable_params params)
632 {
633         struct rhash_head *he = __rhashtable_lookup(ht, key, params);
634
635         return he ? rht_obj(ht, he) : NULL;
636 }
637
638 /**
639  * rhashtable_lookup_fast - search hash table, without RCU read lock
640  * @ht:         hash table
641  * @key:        the pointer to the key
642  * @params:     hash table parameters
643  *
644  * Computes the hash value for the key and traverses the bucket chain looking
645  * for a entry with an identical key. The first matching entry is returned.
646  *
647  * Only use this function when you have other mechanisms guaranteeing
648  * that the object won't go away after the RCU read lock is released.
649  *
650  * Returns the first entry on which the compare function returned true.
651  */
652 static inline void *rhashtable_lookup_fast(
653         struct rhashtable *ht, const void *key,
654         const struct rhashtable_params params)
655 {
656         void *obj;
657
658         rcu_read_lock();
659         obj = rhashtable_lookup(ht, key, params);
660         rcu_read_unlock();
661
662         return obj;
663 }
664
665 /**
666  * rhltable_lookup - search hash list table
667  * @hlt:        hash table
668  * @key:        the pointer to the key
669  * @params:     hash table parameters
670  *
671  * Computes the hash value for the key and traverses the bucket chain looking
672  * for a entry with an identical key.  All matching entries are returned
673  * in a list.
674  *
675  * This must only be called under the RCU read lock.
676  *
677  * Returns the list of entries that match the given key.
678  */
679 static inline struct rhlist_head *rhltable_lookup(
680         struct rhltable *hlt, const void *key,
681         const struct rhashtable_params params)
682 {
683         struct rhash_head *he = __rhashtable_lookup(&hlt->ht, key, params);
684
685         return he ? container_of(he, struct rhlist_head, rhead) : NULL;
686 }
687
688 /* Internal function, please use rhashtable_insert_fast() instead. This
689  * function returns the existing element already in hashes in there is a clash,
690  * otherwise it returns an error via ERR_PTR().
691  */
692 static inline void *__rhashtable_insert_fast(
693         struct rhashtable *ht, const void *key, struct rhash_head *obj,
694         const struct rhashtable_params params, bool rhlist)
695 {
696         struct rhashtable_compare_arg arg = {
697                 .ht = ht,
698                 .key = key,
699         };
700         struct rhash_lock_head __rcu **bkt;
701         struct rhash_head __rcu **pprev;
702         struct bucket_table *tbl;
703         struct rhash_head *head;
704         unsigned int hash;
705         int elasticity;
706         void *data;
707
708         rcu_read_lock();
709
710         tbl = rht_dereference_rcu(ht->tbl, ht);
711         hash = rht_head_hashfn(ht, tbl, obj, params);
712         elasticity = RHT_ELASTICITY;
713         bkt = rht_bucket_insert(ht, tbl, hash);
714         data = ERR_PTR(-ENOMEM);
715         if (!bkt)
716                 goto out;
717         pprev = NULL;
718         rht_lock(tbl, bkt);
719
720         if (unlikely(rcu_access_pointer(tbl->future_tbl))) {
721 slow_path:
722                 rht_unlock(tbl, bkt);
723                 rcu_read_unlock();
724                 return rhashtable_insert_slow(ht, key, obj);
725         }
726
727         rht_for_each_from(head, rht_ptr(bkt, tbl, hash), tbl, hash) {
728                 struct rhlist_head *plist;
729                 struct rhlist_head *list;
730
731                 elasticity--;
732                 if (!key ||
733                     (params.obj_cmpfn ?
734                      params.obj_cmpfn(&arg, rht_obj(ht, head)) :
735                      rhashtable_compare(&arg, rht_obj(ht, head)))) {
736                         pprev = &head->next;
737                         continue;
738                 }
739
740                 data = rht_obj(ht, head);
741
742                 if (!rhlist)
743                         goto out_unlock;
744
745
746                 list = container_of(obj, struct rhlist_head, rhead);
747                 plist = container_of(head, struct rhlist_head, rhead);
748
749                 RCU_INIT_POINTER(list->next, plist);
750                 head = rht_dereference_bucket(head->next, tbl, hash);
751                 RCU_INIT_POINTER(list->rhead.next, head);
752                 if (pprev) {
753                         rcu_assign_pointer(*pprev, obj);
754                         rht_unlock(tbl, bkt);
755                 } else
756                         rht_assign_unlock(tbl, bkt, obj);
757                 data = NULL;
758                 goto out;
759         }
760
761         if (elasticity <= 0)
762                 goto slow_path;
763
764         data = ERR_PTR(-E2BIG);
765         if (unlikely(rht_grow_above_max(ht, tbl)))
766                 goto out_unlock;
767
768         if (unlikely(rht_grow_above_100(ht, tbl)))
769                 goto slow_path;
770
771         /* Inserting at head of list makes unlocking free. */
772         head = rht_ptr(bkt, tbl, hash);
773
774         RCU_INIT_POINTER(obj->next, head);
775         if (rhlist) {
776                 struct rhlist_head *list;
777
778                 list = container_of(obj, struct rhlist_head, rhead);
779                 RCU_INIT_POINTER(list->next, NULL);
780         }
781
782         atomic_inc(&ht->nelems);
783         rht_assign_unlock(tbl, bkt, obj);
784
785         if (rht_grow_above_75(ht, tbl))
786                 schedule_work(&ht->run_work);
787
788         data = NULL;
789 out:
790         rcu_read_unlock();
791
792         return data;
793
794 out_unlock:
795         rht_unlock(tbl, bkt);
796         goto out;
797 }
798
799 /**
800  * rhashtable_insert_fast - insert object into hash table
801  * @ht:         hash table
802  * @obj:        pointer to hash head inside object
803  * @params:     hash table parameters
804  *
805  * Will take the per bucket bitlock to protect against mutual mutations
806  * on the same bucket. Multiple insertions may occur in parallel unless
807  * they map to the same bucket.
808  *
809  * It is safe to call this function from atomic context.
810  *
811  * Will trigger an automatic deferred table resizing if residency in the
812  * table grows beyond 70%.
813  */
814 static inline int rhashtable_insert_fast(
815         struct rhashtable *ht, struct rhash_head *obj,
816         const struct rhashtable_params params)
817 {
818         void *ret;
819
820         ret = __rhashtable_insert_fast(ht, NULL, obj, params, false);
821         if (IS_ERR(ret))
822                 return PTR_ERR(ret);
823
824         return ret == NULL ? 0 : -EEXIST;
825 }
826
827 /**
828  * rhltable_insert_key - insert object into hash list table
829  * @hlt:        hash list table
830  * @key:        the pointer to the key
831  * @list:       pointer to hash list head inside object
832  * @params:     hash table parameters
833  *
834  * Will take the per bucket bitlock to protect against mutual mutations
835  * on the same bucket. Multiple insertions may occur in parallel unless
836  * they map to the same bucket.
837  *
838  * It is safe to call this function from atomic context.
839  *
840  * Will trigger an automatic deferred table resizing if residency in the
841  * table grows beyond 70%.
842  */
843 static inline int rhltable_insert_key(
844         struct rhltable *hlt, const void *key, struct rhlist_head *list,
845         const struct rhashtable_params params)
846 {
847         return PTR_ERR(__rhashtable_insert_fast(&hlt->ht, key, &list->rhead,
848                                                 params, true));
849 }
850
851 /**
852  * rhltable_insert - insert object into hash list table
853  * @hlt:        hash list table
854  * @list:       pointer to hash list head inside object
855  * @params:     hash table parameters
856  *
857  * Will take the per bucket bitlock to protect against mutual mutations
858  * on the same bucket. Multiple insertions may occur in parallel unless
859  * they map to the same bucket.
860  *
861  * It is safe to call this function from atomic context.
862  *
863  * Will trigger an automatic deferred table resizing if residency in the
864  * table grows beyond 70%.
865  */
866 static inline int rhltable_insert(
867         struct rhltable *hlt, struct rhlist_head *list,
868         const struct rhashtable_params params)
869 {
870         const char *key = rht_obj(&hlt->ht, &list->rhead);
871
872         key += params.key_offset;
873
874         return rhltable_insert_key(hlt, key, list, params);
875 }
876
877 /**
878  * rhashtable_lookup_insert_fast - lookup and insert object into hash table
879  * @ht:         hash table
880  * @obj:        pointer to hash head inside object
881  * @params:     hash table parameters
882  *
883  * This lookup function may only be used for fixed key hash table (key_len
884  * parameter set). It will BUG() if used inappropriately.
885  *
886  * It is safe to call this function from atomic context.
887  *
888  * Will trigger an automatic deferred table resizing if residency in the
889  * table grows beyond 70%.
890  */
891 static inline int rhashtable_lookup_insert_fast(
892         struct rhashtable *ht, struct rhash_head *obj,
893         const struct rhashtable_params params)
894 {
895         const char *key = rht_obj(ht, obj);
896         void *ret;
897
898         BUG_ON(ht->p.obj_hashfn);
899
900         ret = __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params,
901                                        false);
902         if (IS_ERR(ret))
903                 return PTR_ERR(ret);
904
905         return ret == NULL ? 0 : -EEXIST;
906 }
907
908 /**
909  * rhashtable_lookup_get_insert_fast - lookup and insert object into hash table
910  * @ht:         hash table
911  * @obj:        pointer to hash head inside object
912  * @params:     hash table parameters
913  *
914  * Just like rhashtable_lookup_insert_fast(), but this function returns the
915  * object if it exists, NULL if it did not and the insertion was successful,
916  * and an ERR_PTR otherwise.
917  */
918 static inline void *rhashtable_lookup_get_insert_fast(
919         struct rhashtable *ht, struct rhash_head *obj,
920         const struct rhashtable_params params)
921 {
922         const char *key = rht_obj(ht, obj);
923
924         BUG_ON(ht->p.obj_hashfn);
925
926         return __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params,
927                                         false);
928 }
929
930 /**
931  * rhashtable_lookup_insert_key - search and insert object to hash table
932  *                                with explicit key
933  * @ht:         hash table
934  * @key:        key
935  * @obj:        pointer to hash head inside object
936  * @params:     hash table parameters
937  *
938  * Lookups may occur in parallel with hashtable mutations and resizing.
939  *
940  * Will trigger an automatic deferred table resizing if residency in the
941  * table grows beyond 70%.
942  *
943  * Returns zero on success.
944  */
945 static inline int rhashtable_lookup_insert_key(
946         struct rhashtable *ht, const void *key, struct rhash_head *obj,
947         const struct rhashtable_params params)
948 {
949         void *ret;
950
951         BUG_ON(!ht->p.obj_hashfn || !key);
952
953         ret = __rhashtable_insert_fast(ht, key, obj, params, false);
954         if (IS_ERR(ret))
955                 return PTR_ERR(ret);
956
957         return ret == NULL ? 0 : -EEXIST;
958 }
959
960 /**
961  * rhashtable_lookup_get_insert_key - lookup and insert object into hash table
962  * @ht:         hash table
963  * @key:        key
964  * @obj:        pointer to hash head inside object
965  * @params:     hash table parameters
966  *
967  * Just like rhashtable_lookup_insert_key(), but this function returns the
968  * object if it exists, NULL if it does not and the insertion was successful,
969  * and an ERR_PTR otherwise.
970  */
971 static inline void *rhashtable_lookup_get_insert_key(
972         struct rhashtable *ht, const void *key, struct rhash_head *obj,
973         const struct rhashtable_params params)
974 {
975         BUG_ON(!ht->p.obj_hashfn || !key);
976
977         return __rhashtable_insert_fast(ht, key, obj, params, false);
978 }
979
980 /* Internal function, please use rhashtable_remove_fast() instead */
981 static inline int __rhashtable_remove_fast_one(
982         struct rhashtable *ht, struct bucket_table *tbl,
983         struct rhash_head *obj, const struct rhashtable_params params,
984         bool rhlist)
985 {
986         struct rhash_lock_head __rcu **bkt;
987         struct rhash_head __rcu **pprev;
988         struct rhash_head *he;
989         unsigned int hash;
990         int err = -ENOENT;
991
992         hash = rht_head_hashfn(ht, tbl, obj, params);
993         bkt = rht_bucket_var(tbl, hash);
994         if (!bkt)
995                 return -ENOENT;
996         pprev = NULL;
997         rht_lock(tbl, bkt);
998
999         rht_for_each_from(he, rht_ptr(bkt, tbl, hash), tbl, hash) {
1000                 struct rhlist_head *list;
1001
1002                 list = container_of(he, struct rhlist_head, rhead);
1003
1004                 if (he != obj) {
1005                         struct rhlist_head __rcu **lpprev;
1006
1007                         pprev = &he->next;
1008
1009                         if (!rhlist)
1010                                 continue;
1011
1012                         do {
1013                                 lpprev = &list->next;
1014                                 list = rht_dereference_bucket(list->next,
1015                                                               tbl, hash);
1016                         } while (list && obj != &list->rhead);
1017
1018                         if (!list)
1019                                 continue;
1020
1021                         list = rht_dereference_bucket(list->next, tbl, hash);
1022                         RCU_INIT_POINTER(*lpprev, list);
1023                         err = 0;
1024                         break;
1025                 }
1026
1027                 obj = rht_dereference_bucket(obj->next, tbl, hash);
1028                 err = 1;
1029
1030                 if (rhlist) {
1031                         list = rht_dereference_bucket(list->next, tbl, hash);
1032                         if (list) {
1033                                 RCU_INIT_POINTER(list->rhead.next, obj);
1034                                 obj = &list->rhead;
1035                                 err = 0;
1036                         }
1037                 }
1038
1039                 if (pprev) {
1040                         rcu_assign_pointer(*pprev, obj);
1041                         rht_unlock(tbl, bkt);
1042                 } else {
1043                         rht_assign_unlock(tbl, bkt, obj);
1044                 }
1045                 goto unlocked;
1046         }
1047
1048         rht_unlock(tbl, bkt);
1049 unlocked:
1050         if (err > 0) {
1051                 atomic_dec(&ht->nelems);
1052                 if (unlikely(ht->p.automatic_shrinking &&
1053                              rht_shrink_below_30(ht, tbl)))
1054                         schedule_work(&ht->run_work);
1055                 err = 0;
1056         }
1057
1058         return err;
1059 }
1060
1061 /* Internal function, please use rhashtable_remove_fast() instead */
1062 static inline int __rhashtable_remove_fast(
1063         struct rhashtable *ht, struct rhash_head *obj,
1064         const struct rhashtable_params params, bool rhlist)
1065 {
1066         struct bucket_table *tbl;
1067         int err;
1068
1069         rcu_read_lock();
1070
1071         tbl = rht_dereference_rcu(ht->tbl, ht);
1072
1073         /* Because we have already taken (and released) the bucket
1074          * lock in old_tbl, if we find that future_tbl is not yet
1075          * visible then that guarantees the entry to still be in
1076          * the old tbl if it exists.
1077          */
1078         while ((err = __rhashtable_remove_fast_one(ht, tbl, obj, params,
1079                                                    rhlist)) &&
1080                (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
1081                 ;
1082
1083         rcu_read_unlock();
1084
1085         return err;
1086 }
1087
1088 /**
1089  * rhashtable_remove_fast - remove object from hash table
1090  * @ht:         hash table
1091  * @obj:        pointer to hash head inside object
1092  * @params:     hash table parameters
1093  *
1094  * Since the hash chain is single linked, the removal operation needs to
1095  * walk the bucket chain upon removal. The removal operation is thus
1096  * considerable slow if the hash table is not correctly sized.
1097  *
1098  * Will automatically shrink the table if permitted when residency drops
1099  * below 30%.
1100  *
1101  * Returns zero on success, -ENOENT if the entry could not be found.
1102  */
1103 static inline int rhashtable_remove_fast(
1104         struct rhashtable *ht, struct rhash_head *obj,
1105         const struct rhashtable_params params)
1106 {
1107         return __rhashtable_remove_fast(ht, obj, params, false);
1108 }
1109
1110 /**
1111  * rhltable_remove - remove object from hash list table
1112  * @hlt:        hash list table
1113  * @list:       pointer to hash list head inside object
1114  * @params:     hash table parameters
1115  *
1116  * Since the hash chain is single linked, the removal operation needs to
1117  * walk the bucket chain upon removal. The removal operation is thus
1118  * considerable slow if the hash table is not correctly sized.
1119  *
1120  * Will automatically shrink the table if permitted when residency drops
1121  * below 30%
1122  *
1123  * Returns zero on success, -ENOENT if the entry could not be found.
1124  */
1125 static inline int rhltable_remove(
1126         struct rhltable *hlt, struct rhlist_head *list,
1127         const struct rhashtable_params params)
1128 {
1129         return __rhashtable_remove_fast(&hlt->ht, &list->rhead, params, true);
1130 }
1131
1132 /* Internal function, please use rhashtable_replace_fast() instead */
1133 static inline int __rhashtable_replace_fast(
1134         struct rhashtable *ht, struct bucket_table *tbl,
1135         struct rhash_head *obj_old, struct rhash_head *obj_new,
1136         const struct rhashtable_params params)
1137 {
1138         struct rhash_lock_head __rcu **bkt;
1139         struct rhash_head __rcu **pprev;
1140         struct rhash_head *he;
1141         unsigned int hash;
1142         int err = -ENOENT;
1143
1144         /* Minimally, the old and new objects must have same hash
1145          * (which should mean identifiers are the same).
1146          */
1147         hash = rht_head_hashfn(ht, tbl, obj_old, params);
1148         if (hash != rht_head_hashfn(ht, tbl, obj_new, params))
1149                 return -EINVAL;
1150
1151         bkt = rht_bucket_var(tbl, hash);
1152         if (!bkt)
1153                 return -ENOENT;
1154
1155         pprev = NULL;
1156         rht_lock(tbl, bkt);
1157
1158         rht_for_each_from(he, rht_ptr(bkt, tbl, hash), tbl, hash) {
1159                 if (he != obj_old) {
1160                         pprev = &he->next;
1161                         continue;
1162                 }
1163
1164                 rcu_assign_pointer(obj_new->next, obj_old->next);
1165                 if (pprev) {
1166                         rcu_assign_pointer(*pprev, obj_new);
1167                         rht_unlock(tbl, bkt);
1168                 } else {
1169                         rht_assign_unlock(tbl, bkt, obj_new);
1170                 }
1171                 err = 0;
1172                 goto unlocked;
1173         }
1174
1175         rht_unlock(tbl, bkt);
1176
1177 unlocked:
1178         return err;
1179 }
1180
1181 /**
1182  * rhashtable_replace_fast - replace an object in hash table
1183  * @ht:         hash table
1184  * @obj_old:    pointer to hash head inside object being replaced
1185  * @obj_new:    pointer to hash head inside object which is new
1186  * @params:     hash table parameters
1187  *
1188  * Replacing an object doesn't affect the number of elements in the hash table
1189  * or bucket, so we don't need to worry about shrinking or expanding the
1190  * table here.
1191  *
1192  * Returns zero on success, -ENOENT if the entry could not be found,
1193  * -EINVAL if hash is not the same for the old and new objects.
1194  */
1195 static inline int rhashtable_replace_fast(
1196         struct rhashtable *ht, struct rhash_head *obj_old,
1197         struct rhash_head *obj_new,
1198         const struct rhashtable_params params)
1199 {
1200         struct bucket_table *tbl;
1201         int err;
1202
1203         rcu_read_lock();
1204
1205         tbl = rht_dereference_rcu(ht->tbl, ht);
1206
1207         /* Because we have already taken (and released) the bucket
1208          * lock in old_tbl, if we find that future_tbl is not yet
1209          * visible then that guarantees the entry to still be in
1210          * the old tbl if it exists.
1211          */
1212         while ((err = __rhashtable_replace_fast(ht, tbl, obj_old,
1213                                                 obj_new, params)) &&
1214                (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
1215                 ;
1216
1217         rcu_read_unlock();
1218
1219         return err;
1220 }
1221
1222 /**
1223  * rhltable_walk_enter - Initialise an iterator
1224  * @hlt:        Table to walk over
1225  * @iter:       Hash table Iterator
1226  *
1227  * This function prepares a hash table walk.
1228  *
1229  * Note that if you restart a walk after rhashtable_walk_stop you
1230  * may see the same object twice.  Also, you may miss objects if
1231  * there are removals in between rhashtable_walk_stop and the next
1232  * call to rhashtable_walk_start.
1233  *
1234  * For a completely stable walk you should construct your own data
1235  * structure outside the hash table.
1236  *
1237  * This function may be called from any process context, including
1238  * non-preemptable context, but cannot be called from softirq or
1239  * hardirq context.
1240  *
1241  * You must call rhashtable_walk_exit after this function returns.
1242  */
1243 static inline void rhltable_walk_enter(struct rhltable *hlt,
1244                                        struct rhashtable_iter *iter)
1245 {
1246         return rhashtable_walk_enter(&hlt->ht, iter);
1247 }
1248
1249 /**
1250  * rhltable_free_and_destroy - free elements and destroy hash list table
1251  * @hlt:        the hash list table to destroy
1252  * @free_fn:    callback to release resources of element
1253  * @arg:        pointer passed to free_fn
1254  *
1255  * See documentation for rhashtable_free_and_destroy.
1256  */
1257 static inline void rhltable_free_and_destroy(struct rhltable *hlt,
1258                                              void (*free_fn)(void *ptr,
1259                                                              void *arg),
1260                                              void *arg)
1261 {
1262         return rhashtable_free_and_destroy(&hlt->ht, free_fn, arg);
1263 }
1264
1265 static inline void rhltable_destroy(struct rhltable *hlt)
1266 {
1267         return rhltable_free_and_destroy(hlt, NULL, NULL);
1268 }
1269
1270 #endif /* _LINUX_RHASHTABLE_H */