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