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