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