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