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