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