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