blob: c5bf21261bb196d575a3fa11d80da30689e11f84 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Resizable, Scalable, Concurrent Hash Table
4 *
5 * Copyright (c) 2015-2016 Herbert Xu <herbert@gondor.apana.org.au>
6 * Copyright (c) 2014-2015 Thomas Graf <tgraf@suug.ch>
7 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
8 *
9 * Code partially derived from nft_hash
10 * Rewritten with rehash code from br_multicast plus single list
11 * pointer as suggested by Josh Triplett
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
16 */
17
18#ifndef _LINUX_RHASHTABLE_H
19#define _LINUX_RHASHTABLE_H
20
21#include <linux/err.h>
22#include <linux/errno.h>
23#include <linux/jhash.h>
24#include <linux/list_nulls.h>
25#include <linux/workqueue.h>
26#include <linux/rculist.h>
David Brazdil0f672f62019-12-10 10:32:29 +000027#include <linux/bit_spinlock.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000028
29#include <linux/rhashtable-types.h>
30/*
David Brazdil0f672f62019-12-10 10:32:29 +000031 * 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.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000034 * The end of the chain is marked with a special nulls marks which has
David Brazdil0f672f62019-12-10 10:32:29 +000035 * the least significant bit set but otherwise stores the address of
36 * the hash bucket. This allows us to be be sure we've found the end
37 * of the right list.
38 * The value stored in the hash bucket has BIT(0) used as a lock bit.
39 * This bit must be atomically set before any changes are made to
40 * the chain. To avoid dereferencing this pointer without clearing
41 * the bit first, we use an opaque 'struct rhash_lock_head *' for the
42 * pointer stored in the bucket. This struct needs to be defined so
43 * that rcu_dereference() works on it, but it has no content so a
44 * cast is needed for it to be useful. This ensures it isn't
45 * used by mistake with clearing the lock bit first.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000046 */
David Brazdil0f672f62019-12-10 10:32:29 +000047struct rhash_lock_head {};
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000048
49/* Maximum chain length before rehash
50 *
51 * The maximum (not average) chain length grows with the size of the hash
52 * table, at a rate of (log N)/(log log N).
53 *
54 * The value of 16 is selected so that even if the hash table grew to
55 * 2^32 you would not expect the maximum chain length to exceed it
56 * unless we are under attack (or extremely unlucky).
57 *
58 * As this limit is only to detect attacks, we don't need to set it to a
59 * lower value as you'd need the chain length to vastly exceed 16 to have
60 * any real effect on the system.
61 */
62#define RHT_ELASTICITY 16u
63
64/**
65 * struct bucket_table - Table of hash buckets
66 * @size: Number of hash buckets
67 * @nest: Number of bits of first-level nested table.
68 * @rehash: Current bucket being rehashed
69 * @hash_rnd: Random seed to fold into hash
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000070 * @walkers: List of active walkers
71 * @rcu: RCU structure for freeing the table
72 * @future_tbl: Table under construction during rehashing
73 * @ntbl: Nested table used when out of memory.
74 * @buckets: size * hash buckets
75 */
76struct bucket_table {
77 unsigned int size;
78 unsigned int nest;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000079 u32 hash_rnd;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000080 struct list_head walkers;
81 struct rcu_head rcu;
82
83 struct bucket_table __rcu *future_tbl;
84
David Brazdil0f672f62019-12-10 10:32:29 +000085 struct lockdep_map dep_map;
86
87 struct rhash_lock_head *buckets[] ____cacheline_aligned_in_smp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000088};
89
David Brazdil0f672f62019-12-10 10:32:29 +000090/*
91 * NULLS_MARKER() expects a hash value with the low
92 * bits mostly likely to be significant, and it discards
93 * the msb.
94 * We give it an address, in which the bottom bit is
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.
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
102 */
103#define RHT_NULLS_MARKER(ptr) \
104 ((void *)NULLS_MARKER(((unsigned long) (ptr)) >> 1))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000105#define INIT_RHT_NULLS_HEAD(ptr) \
David Brazdil0f672f62019-12-10 10:32:29 +0000106 ((ptr) = NULL)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000107
108static inline bool rht_is_a_nulls(const struct rhash_head *ptr)
109{
110 return ((unsigned long) ptr & 1);
111}
112
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{
122 return hash & (tbl->size - 1);
123}
124
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)
128{
129 unsigned int hash;
130
131 /* params must be equal to ht->p if it isn't constant. */
132 if (!__builtin_constant_p(params.key_len))
133 hash = ht->p.hashfn(key, ht->key_len, hash_rnd);
134 else if (params.key_len) {
135 unsigned int key_len = params.key_len;
136
137 if (params.hashfn)
138 hash = params.hashfn(key, key_len, hash_rnd);
139 else if (key_len & (sizeof(u32) - 1))
140 hash = jhash(key, key_len, hash_rnd);
141 else
142 hash = jhash2(key, key_len / sizeof(u32), hash_rnd);
143 } else {
144 unsigned int key_len = ht->p.key_len;
145
146 if (params.hashfn)
147 hash = params.hashfn(key, key_len, hash_rnd);
148 else
149 hash = jhash(key, key_len, hash_rnd);
150 }
151
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
161 return rht_bucket_index(tbl, hash);
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) ?
171 rht_bucket_index(tbl, params.obj_hashfn(ptr, params.key_len ?:
172 ht->p.key_len,
173 tbl->hash_rnd)) :
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
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{
211 return atomic_read(&ht->nelems) > tbl->size &&
212 (!ht->p.max_size || tbl->size < ht->p.max_size);
213}
214
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{
223 return atomic_read(&ht->nelems) >= ht->max_elems;
224}
225
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000226#ifdef CONFIG_PROVE_LOCKING
227int lockdep_rht_mutex_is_held(struct rhashtable *ht);
228int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash);
229#else
230static inline int lockdep_rht_mutex_is_held(struct rhashtable *ht)
231{
232 return 1;
233}
234
235static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl,
236 u32 hash)
237{
238 return 1;
239}
240#endif /* CONFIG_PROVE_LOCKING */
241
242void *rhashtable_insert_slow(struct rhashtable *ht, const void *key,
243 struct rhash_head *obj);
244
245void rhashtable_walk_enter(struct rhashtable *ht,
246 struct rhashtable_iter *iter);
247void rhashtable_walk_exit(struct rhashtable_iter *iter);
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
255void *rhashtable_walk_next(struct rhashtable_iter *iter);
256void *rhashtable_walk_peek(struct rhashtable_iter *iter);
257void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU);
258
259void rhashtable_free_and_destroy(struct rhashtable *ht,
260 void (*free_fn)(void *ptr, void *arg),
261 void *arg);
262void rhashtable_destroy(struct rhashtable *ht);
263
David Brazdil0f672f62019-12-10 10:32:29 +0000264struct 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);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000271
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
278#define rht_dereference_bucket(p, tbl, hash) \
279 rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash))
280
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; })
286
David Brazdil0f672f62019-12-10 10:32:29 +0000287static inline struct rhash_lock_head *const *rht_bucket(
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000288 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
David Brazdil0f672f62019-12-10 10:32:29 +0000294static inline struct rhash_lock_head **rht_bucket_var(
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000295 struct bucket_table *tbl, unsigned int hash)
296{
David Brazdil0f672f62019-12-10 10:32:29 +0000297 return unlikely(tbl->nest) ? __rht_bucket_nested(tbl, hash) :
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000298 &tbl->buckets[hash];
299}
300
David Brazdil0f672f62019-12-10 10:32:29 +0000301static inline struct rhash_lock_head **rht_bucket_insert(
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000302 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
David Brazdil0f672f62019-12-10 10:32:29 +0000308/*
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.
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.
324 * When we write to a bucket without unlocking, we use rht_assign_locked().
325 */
326
327static inline void rht_lock(struct bucket_table *tbl,
328 struct rhash_lock_head **bkt)
329{
330 local_bh_disable();
331 bit_spin_lock(0, (unsigned long *)bkt);
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();
340 bit_spin_lock(0, (unsigned long *)bucket);
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);
348 bit_spin_unlock(0, (unsigned long *)bkt);
349 local_bh_enable();
350}
351
Olivier Deprez0e641232021-09-23 10:07:05 +0200352static inline struct rhash_head *__rht_ptr(
353 struct rhash_lock_head *p, struct rhash_lock_head __rcu *const *bkt)
David Brazdil0f672f62019-12-10 10:32:29 +0000354{
Olivier Deprez0e641232021-09-23 10:07:05 +0200355 return (struct rhash_head *)
356 ((unsigned long)p & ~BIT(0) ?:
David Brazdil0f672f62019-12-10 10:32:29 +0000357 (unsigned long)RHT_NULLS_MARKER(bkt));
358}
359
360/*
361 * Where 'bkt' is a bucket and might be locked:
362 * rht_ptr_rcu() dereferences that pointer and clears the lock bit.
363 * rht_ptr() dereferences in a context where the bucket is locked.
364 * rht_ptr_exclusive() dereferences in a context where exclusive
365 * access is guaranteed, such as when destroying the table.
366 */
367static inline struct rhash_head *rht_ptr_rcu(
Olivier Deprez0e641232021-09-23 10:07:05 +0200368 struct rhash_lock_head *const *p)
David Brazdil0f672f62019-12-10 10:32:29 +0000369{
Olivier Deprez0e641232021-09-23 10:07:05 +0200370 struct rhash_lock_head __rcu *const *bkt = (void *)p;
371 return __rht_ptr(rcu_dereference(*bkt), bkt);
David Brazdil0f672f62019-12-10 10:32:29 +0000372}
373
374static inline struct rhash_head *rht_ptr(
Olivier Deprez0e641232021-09-23 10:07:05 +0200375 struct rhash_lock_head *const *p,
David Brazdil0f672f62019-12-10 10:32:29 +0000376 struct bucket_table *tbl,
377 unsigned int hash)
378{
Olivier Deprez0e641232021-09-23 10:07:05 +0200379 struct rhash_lock_head __rcu *const *bkt = (void *)p;
380 return __rht_ptr(rht_dereference_bucket(*bkt, tbl, hash), bkt);
David Brazdil0f672f62019-12-10 10:32:29 +0000381}
382
383static inline struct rhash_head *rht_ptr_exclusive(
Olivier Deprez0e641232021-09-23 10:07:05 +0200384 struct rhash_lock_head *const *p)
David Brazdil0f672f62019-12-10 10:32:29 +0000385{
Olivier Deprez0e641232021-09-23 10:07:05 +0200386 struct rhash_lock_head __rcu *const *bkt = (void *)p;
387 return __rht_ptr(rcu_dereference_protected(*bkt, 1), bkt);
David Brazdil0f672f62019-12-10 10:32:29 +0000388}
389
390static inline void rht_assign_locked(struct rhash_lock_head **bkt,
391 struct rhash_head *obj)
392{
393 struct rhash_head __rcu **p = (struct rhash_head __rcu **)bkt;
394
395 if (rht_is_a_nulls(obj))
396 obj = NULL;
397 rcu_assign_pointer(*p, (void *)((unsigned long)obj | BIT(0)));
398}
399
400static inline void rht_assign_unlock(struct bucket_table *tbl,
401 struct rhash_lock_head **bkt,
402 struct rhash_head *obj)
403{
404 struct rhash_head __rcu **p = (struct rhash_head __rcu **)bkt;
405
406 if (rht_is_a_nulls(obj))
407 obj = NULL;
408 lock_map_release(&tbl->dep_map);
409 rcu_assign_pointer(*p, obj);
410 preempt_enable();
411 __release(bitlock);
412 local_bh_enable();
413}
414
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000415/**
David Brazdil0f672f62019-12-10 10:32:29 +0000416 * rht_for_each_from - iterate over hash chain from given head
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000417 * @pos: the &struct rhash_head to use as a loop cursor.
David Brazdil0f672f62019-12-10 10:32:29 +0000418 * @head: the &struct rhash_head to start from
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000419 * @tbl: the &struct bucket_table
420 * @hash: the hash value / bucket index
421 */
David Brazdil0f672f62019-12-10 10:32:29 +0000422#define rht_for_each_from(pos, head, tbl, hash) \
423 for (pos = head; \
424 !rht_is_a_nulls(pos); \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000425 pos = rht_dereference_bucket((pos)->next, tbl, hash))
426
427/**
428 * rht_for_each - iterate over hash chain
429 * @pos: the &struct rhash_head to use as a loop cursor.
430 * @tbl: the &struct bucket_table
431 * @hash: the hash value / bucket index
432 */
433#define rht_for_each(pos, tbl, hash) \
David Brazdil0f672f62019-12-10 10:32:29 +0000434 rht_for_each_from(pos, rht_ptr(rht_bucket(tbl, hash), tbl, hash), \
435 tbl, hash)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000436
437/**
David Brazdil0f672f62019-12-10 10:32:29 +0000438 * rht_for_each_entry_from - iterate over hash chain from given head
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000439 * @tpos: the type * to use as a loop cursor.
440 * @pos: the &struct rhash_head to use as a loop cursor.
David Brazdil0f672f62019-12-10 10:32:29 +0000441 * @head: the &struct rhash_head to start from
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000442 * @tbl: the &struct bucket_table
443 * @hash: the hash value / bucket index
444 * @member: name of the &struct rhash_head within the hashable struct.
445 */
David Brazdil0f672f62019-12-10 10:32:29 +0000446#define rht_for_each_entry_from(tpos, pos, head, tbl, hash, member) \
447 for (pos = head; \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000448 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
449 pos = rht_dereference_bucket((pos)->next, tbl, hash))
450
451/**
452 * rht_for_each_entry - iterate over hash chain of given type
453 * @tpos: the type * to use as a loop cursor.
454 * @pos: the &struct rhash_head to use as a loop cursor.
455 * @tbl: the &struct bucket_table
456 * @hash: the hash value / bucket index
457 * @member: name of the &struct rhash_head within the hashable struct.
458 */
459#define rht_for_each_entry(tpos, pos, tbl, hash, member) \
David Brazdil0f672f62019-12-10 10:32:29 +0000460 rht_for_each_entry_from(tpos, pos, \
461 rht_ptr(rht_bucket(tbl, hash), tbl, hash), \
462 tbl, hash, member)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000463
464/**
465 * rht_for_each_entry_safe - safely iterate over hash chain of given type
466 * @tpos: the type * to use as a loop cursor.
467 * @pos: the &struct rhash_head to use as a loop cursor.
468 * @next: the &struct rhash_head to use as next in loop cursor.
469 * @tbl: the &struct bucket_table
470 * @hash: the hash value / bucket index
471 * @member: name of the &struct rhash_head within the hashable struct.
472 *
473 * This hash chain list-traversal primitive allows for the looped code to
474 * remove the loop cursor from the list.
475 */
476#define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \
David Brazdil0f672f62019-12-10 10:32:29 +0000477 for (pos = rht_ptr(rht_bucket(tbl, hash), tbl, hash), \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000478 next = !rht_is_a_nulls(pos) ? \
479 rht_dereference_bucket(pos->next, tbl, hash) : NULL; \
480 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
481 pos = next, \
482 next = !rht_is_a_nulls(pos) ? \
483 rht_dereference_bucket(pos->next, tbl, hash) : NULL)
484
485/**
David Brazdil0f672f62019-12-10 10:32:29 +0000486 * rht_for_each_rcu_from - iterate over rcu hash chain from given head
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000487 * @pos: the &struct rhash_head to use as a loop cursor.
David Brazdil0f672f62019-12-10 10:32:29 +0000488 * @head: the &struct rhash_head to start from
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000489 * @tbl: the &struct bucket_table
490 * @hash: the hash value / bucket index
491 *
492 * This hash chain list-traversal primitive may safely run concurrently with
493 * the _rcu mutation primitives such as rhashtable_insert() as long as the
494 * traversal is guarded by rcu_read_lock().
495 */
David Brazdil0f672f62019-12-10 10:32:29 +0000496#define rht_for_each_rcu_from(pos, head, tbl, hash) \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000497 for (({barrier(); }), \
David Brazdil0f672f62019-12-10 10:32:29 +0000498 pos = head; \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000499 !rht_is_a_nulls(pos); \
500 pos = rcu_dereference_raw(pos->next))
501
502/**
503 * rht_for_each_rcu - iterate over rcu hash chain
504 * @pos: the &struct rhash_head to use as a loop cursor.
505 * @tbl: the &struct bucket_table
506 * @hash: the hash value / bucket index
507 *
508 * This hash chain list-traversal primitive may safely run concurrently with
509 * the _rcu mutation primitives such as rhashtable_insert() as long as the
510 * traversal is guarded by rcu_read_lock().
511 */
David Brazdil0f672f62019-12-10 10:32:29 +0000512#define rht_for_each_rcu(pos, tbl, hash) \
513 for (({barrier(); }), \
514 pos = rht_ptr_rcu(rht_bucket(tbl, hash)); \
515 !rht_is_a_nulls(pos); \
516 pos = rcu_dereference_raw(pos->next))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000517
518/**
David Brazdil0f672f62019-12-10 10:32:29 +0000519 * rht_for_each_entry_rcu_from - iterated over rcu hash chain from given head
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000520 * @tpos: the type * to use as a loop cursor.
521 * @pos: the &struct rhash_head to use as a loop cursor.
David Brazdil0f672f62019-12-10 10:32:29 +0000522 * @head: the &struct rhash_head to start from
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000523 * @tbl: the &struct bucket_table
524 * @hash: the hash value / bucket index
525 * @member: name of the &struct rhash_head within the hashable struct.
526 *
527 * This hash chain list-traversal primitive may safely run concurrently with
528 * the _rcu mutation primitives such as rhashtable_insert() as long as the
529 * traversal is guarded by rcu_read_lock().
530 */
David Brazdil0f672f62019-12-10 10:32:29 +0000531#define rht_for_each_entry_rcu_from(tpos, pos, head, tbl, hash, member) \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000532 for (({barrier(); }), \
David Brazdil0f672f62019-12-10 10:32:29 +0000533 pos = head; \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000534 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
535 pos = rht_dereference_bucket_rcu(pos->next, tbl, hash))
536
537/**
538 * rht_for_each_entry_rcu - iterate over rcu hash chain of given type
539 * @tpos: the type * to use as a loop cursor.
540 * @pos: the &struct rhash_head to use as a loop cursor.
541 * @tbl: the &struct bucket_table
542 * @hash: the hash value / bucket index
543 * @member: name of the &struct rhash_head within the hashable struct.
544 *
545 * This hash chain list-traversal primitive may safely run concurrently with
546 * the _rcu mutation primitives such as rhashtable_insert() as long as the
547 * traversal is guarded by rcu_read_lock().
548 */
549#define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \
David Brazdil0f672f62019-12-10 10:32:29 +0000550 rht_for_each_entry_rcu_from(tpos, pos, \
551 rht_ptr_rcu(rht_bucket(tbl, hash)), \
552 tbl, hash, member)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000553
554/**
555 * rhl_for_each_rcu - iterate over rcu hash table list
556 * @pos: the &struct rlist_head to use as a loop cursor.
557 * @list: the head of the list
558 *
559 * This hash chain list-traversal primitive should be used on the
560 * list returned by rhltable_lookup.
561 */
562#define rhl_for_each_rcu(pos, list) \
563 for (pos = list; pos; pos = rcu_dereference_raw(pos->next))
564
565/**
566 * rhl_for_each_entry_rcu - iterate over rcu hash table list of given type
567 * @tpos: the type * to use as a loop cursor.
568 * @pos: the &struct rlist_head to use as a loop cursor.
569 * @list: the head of the list
570 * @member: name of the &struct rlist_head within the hashable struct.
571 *
572 * This hash chain list-traversal primitive should be used on the
573 * list returned by rhltable_lookup.
574 */
575#define rhl_for_each_entry_rcu(tpos, pos, list, member) \
576 for (pos = list; pos && rht_entry(tpos, pos, member); \
577 pos = rcu_dereference_raw(pos->next))
578
579static inline int rhashtable_compare(struct rhashtable_compare_arg *arg,
580 const void *obj)
581{
582 struct rhashtable *ht = arg->ht;
583 const char *ptr = obj;
584
585 return memcmp(ptr + ht->p.key_offset, arg->key, ht->p.key_len);
586}
587
588/* Internal function, do not use. */
589static inline struct rhash_head *__rhashtable_lookup(
590 struct rhashtable *ht, const void *key,
591 const struct rhashtable_params params)
592{
593 struct rhashtable_compare_arg arg = {
594 .ht = ht,
595 .key = key,
596 };
David Brazdil0f672f62019-12-10 10:32:29 +0000597 struct rhash_lock_head *const *bkt;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000598 struct bucket_table *tbl;
599 struct rhash_head *he;
600 unsigned int hash;
601
602 tbl = rht_dereference_rcu(ht->tbl, ht);
603restart:
604 hash = rht_key_hashfn(ht, tbl, key, params);
David Brazdil0f672f62019-12-10 10:32:29 +0000605 bkt = rht_bucket(tbl, hash);
606 do {
607 rht_for_each_rcu_from(he, rht_ptr_rcu(bkt), tbl, hash) {
608 if (params.obj_cmpfn ?
609 params.obj_cmpfn(&arg, rht_obj(ht, he)) :
610 rhashtable_compare(&arg, rht_obj(ht, he)))
611 continue;
612 return he;
613 }
614 /* An object might have been moved to a different hash chain,
615 * while we walk along it - better check and retry.
616 */
617 } while (he != RHT_NULLS_MARKER(bkt));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000618
619 /* Ensure we see any new tables. */
620 smp_rmb();
621
622 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
623 if (unlikely(tbl))
624 goto restart;
625
626 return NULL;
627}
628
629/**
630 * rhashtable_lookup - search hash table
631 * @ht: hash table
632 * @key: the pointer to the key
633 * @params: hash table parameters
634 *
635 * Computes the hash value for the key and traverses the bucket chain looking
636 * for a entry with an identical key. The first matching entry is returned.
637 *
638 * This must only be called under the RCU read lock.
639 *
640 * Returns the first entry on which the compare function returned true.
641 */
642static inline void *rhashtable_lookup(
643 struct rhashtable *ht, const void *key,
644 const struct rhashtable_params params)
645{
646 struct rhash_head *he = __rhashtable_lookup(ht, key, params);
647
648 return he ? rht_obj(ht, he) : NULL;
649}
650
651/**
652 * rhashtable_lookup_fast - search hash table, without RCU read lock
653 * @ht: hash table
654 * @key: the pointer to the key
655 * @params: hash table parameters
656 *
657 * Computes the hash value for the key and traverses the bucket chain looking
658 * for a entry with an identical key. The first matching entry is returned.
659 *
660 * Only use this function when you have other mechanisms guaranteeing
661 * that the object won't go away after the RCU read lock is released.
662 *
663 * Returns the first entry on which the compare function returned true.
664 */
665static inline void *rhashtable_lookup_fast(
666 struct rhashtable *ht, const void *key,
667 const struct rhashtable_params params)
668{
669 void *obj;
670
671 rcu_read_lock();
672 obj = rhashtable_lookup(ht, key, params);
673 rcu_read_unlock();
674
675 return obj;
676}
677
678/**
679 * rhltable_lookup - search hash list table
680 * @hlt: hash table
681 * @key: the pointer to the key
682 * @params: hash table parameters
683 *
684 * Computes the hash value for the key and traverses the bucket chain looking
685 * for a entry with an identical key. All matching entries are returned
686 * in a list.
687 *
688 * This must only be called under the RCU read lock.
689 *
690 * Returns the list of entries that match the given key.
691 */
692static inline struct rhlist_head *rhltable_lookup(
693 struct rhltable *hlt, const void *key,
694 const struct rhashtable_params params)
695{
696 struct rhash_head *he = __rhashtable_lookup(&hlt->ht, key, params);
697
698 return he ? container_of(he, struct rhlist_head, rhead) : NULL;
699}
700
701/* Internal function, please use rhashtable_insert_fast() instead. This
702 * function returns the existing element already in hashes in there is a clash,
703 * otherwise it returns an error via ERR_PTR().
704 */
705static inline void *__rhashtable_insert_fast(
706 struct rhashtable *ht, const void *key, struct rhash_head *obj,
707 const struct rhashtable_params params, bool rhlist)
708{
709 struct rhashtable_compare_arg arg = {
710 .ht = ht,
711 .key = key,
712 };
David Brazdil0f672f62019-12-10 10:32:29 +0000713 struct rhash_lock_head **bkt;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000714 struct rhash_head __rcu **pprev;
715 struct bucket_table *tbl;
716 struct rhash_head *head;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000717 unsigned int hash;
718 int elasticity;
719 void *data;
720
721 rcu_read_lock();
722
723 tbl = rht_dereference_rcu(ht->tbl, ht);
724 hash = rht_head_hashfn(ht, tbl, obj, params);
David Brazdil0f672f62019-12-10 10:32:29 +0000725 elasticity = RHT_ELASTICITY;
726 bkt = rht_bucket_insert(ht, tbl, hash);
727 data = ERR_PTR(-ENOMEM);
728 if (!bkt)
729 goto out;
730 pprev = NULL;
731 rht_lock(tbl, bkt);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000732
733 if (unlikely(rcu_access_pointer(tbl->future_tbl))) {
734slow_path:
David Brazdil0f672f62019-12-10 10:32:29 +0000735 rht_unlock(tbl, bkt);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000736 rcu_read_unlock();
737 return rhashtable_insert_slow(ht, key, obj);
738 }
739
David Brazdil0f672f62019-12-10 10:32:29 +0000740 rht_for_each_from(head, rht_ptr(bkt, tbl, hash), tbl, hash) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000741 struct rhlist_head *plist;
742 struct rhlist_head *list;
743
744 elasticity--;
745 if (!key ||
746 (params.obj_cmpfn ?
747 params.obj_cmpfn(&arg, rht_obj(ht, head)) :
748 rhashtable_compare(&arg, rht_obj(ht, head)))) {
749 pprev = &head->next;
750 continue;
751 }
752
753 data = rht_obj(ht, head);
754
755 if (!rhlist)
David Brazdil0f672f62019-12-10 10:32:29 +0000756 goto out_unlock;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000757
758
759 list = container_of(obj, struct rhlist_head, rhead);
760 plist = container_of(head, struct rhlist_head, rhead);
761
762 RCU_INIT_POINTER(list->next, plist);
763 head = rht_dereference_bucket(head->next, tbl, hash);
764 RCU_INIT_POINTER(list->rhead.next, head);
David Brazdil0f672f62019-12-10 10:32:29 +0000765 if (pprev) {
766 rcu_assign_pointer(*pprev, obj);
767 rht_unlock(tbl, bkt);
768 } else
769 rht_assign_unlock(tbl, bkt, obj);
770 data = NULL;
771 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000772 }
773
774 if (elasticity <= 0)
775 goto slow_path;
776
777 data = ERR_PTR(-E2BIG);
778 if (unlikely(rht_grow_above_max(ht, tbl)))
David Brazdil0f672f62019-12-10 10:32:29 +0000779 goto out_unlock;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000780
781 if (unlikely(rht_grow_above_100(ht, tbl)))
782 goto slow_path;
783
David Brazdil0f672f62019-12-10 10:32:29 +0000784 /* Inserting at head of list makes unlocking free. */
785 head = rht_ptr(bkt, tbl, hash);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000786
787 RCU_INIT_POINTER(obj->next, head);
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 }
794
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000795 atomic_inc(&ht->nelems);
David Brazdil0f672f62019-12-10 10:32:29 +0000796 rht_assign_unlock(tbl, bkt, obj);
797
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000798 if (rht_grow_above_75(ht, tbl))
799 schedule_work(&ht->run_work);
800
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000801 data = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000802out:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000803 rcu_read_unlock();
804
805 return data;
David Brazdil0f672f62019-12-10 10:32:29 +0000806
807out_unlock:
808 rht_unlock(tbl, bkt);
809 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000810}
811
812/**
813 * rhashtable_insert_fast - insert object into hash table
814 * @ht: hash table
815 * @obj: pointer to hash head inside object
816 * @params: hash table parameters
817 *
David Brazdil0f672f62019-12-10 10:32:29 +0000818 * Will take the per bucket bitlock to protect against mutual mutations
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000819 * on the same bucket. Multiple insertions may occur in parallel unless
David Brazdil0f672f62019-12-10 10:32:29 +0000820 * they map to the same bucket.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000821 *
822 * It is safe to call this function from atomic context.
823 *
824 * Will trigger an automatic deferred table resizing if residency in the
825 * table grows beyond 70%.
826 */
827static inline int rhashtable_insert_fast(
828 struct rhashtable *ht, struct rhash_head *obj,
829 const struct rhashtable_params params)
830{
831 void *ret;
832
833 ret = __rhashtable_insert_fast(ht, NULL, obj, params, false);
834 if (IS_ERR(ret))
835 return PTR_ERR(ret);
836
837 return ret == NULL ? 0 : -EEXIST;
838}
839
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 *
David Brazdil0f672f62019-12-10 10:32:29 +0000847 * Will take the per bucket bitlock to protect against mutual mutations
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000848 * on the same bucket. Multiple insertions may occur in parallel unless
David Brazdil0f672f62019-12-10 10:32:29 +0000849 * they map to the same bucket.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000850 *
851 * It is safe to call this function from atomic context.
852 *
853 * Will trigger an automatic deferred table resizing if residency in the
854 * table grows beyond 70%.
855 */
856static inline int rhltable_insert_key(
857 struct rhltable *hlt, const void *key, struct rhlist_head *list,
858 const struct rhashtable_params params)
859{
860 return PTR_ERR(__rhashtable_insert_fast(&hlt->ht, key, &list->rhead,
861 params, true));
862}
863
864/**
865 * rhltable_insert - insert object into hash list table
866 * @hlt: hash list table
867 * @list: pointer to hash list head inside object
868 * @params: hash table parameters
869 *
David Brazdil0f672f62019-12-10 10:32:29 +0000870 * Will take the per bucket bitlock to protect against mutual mutations
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000871 * on the same bucket. Multiple insertions may occur in parallel unless
David Brazdil0f672f62019-12-10 10:32:29 +0000872 * they map to the same bucket.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000873 *
874 * It is safe to call this function from atomic context.
875 *
876 * Will trigger an automatic deferred table resizing if residency in the
877 * table grows beyond 70%.
878 */
879static inline int rhltable_insert(
880 struct rhltable *hlt, struct rhlist_head *list,
881 const struct rhashtable_params params)
882{
883 const char *key = rht_obj(&hlt->ht, &list->rhead);
884
885 key += params.key_offset;
886
887 return rhltable_insert_key(hlt, key, list, params);
888}
889
890/**
891 * rhashtable_lookup_insert_fast - lookup and insert object into hash table
892 * @ht: hash table
893 * @obj: pointer to hash head inside object
894 * @params: hash table parameters
895 *
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000896 * This lookup function may only be used for fixed key hash table (key_len
897 * parameter set). It will BUG() if used inappropriately.
898 *
899 * It is safe to call this function from atomic context.
900 *
901 * Will trigger an automatic deferred table resizing if residency in the
902 * table grows beyond 70%.
903 */
904static inline int rhashtable_lookup_insert_fast(
905 struct rhashtable *ht, struct rhash_head *obj,
906 const struct rhashtable_params params)
907{
908 const char *key = rht_obj(ht, obj);
909 void *ret;
910
911 BUG_ON(ht->p.obj_hashfn);
912
913 ret = __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params,
914 false);
915 if (IS_ERR(ret))
916 return PTR_ERR(ret);
917
918 return ret == NULL ? 0 : -EEXIST;
919}
920
921/**
922 * rhashtable_lookup_get_insert_fast - lookup and insert object into hash table
923 * @ht: hash table
924 * @obj: pointer to hash head inside object
925 * @params: hash table parameters
926 *
927 * Just like rhashtable_lookup_insert_fast(), but this function returns the
928 * object if it exists, NULL if it did not and the insertion was successful,
929 * and an ERR_PTR otherwise.
930 */
931static inline void *rhashtable_lookup_get_insert_fast(
932 struct rhashtable *ht, struct rhash_head *obj,
933 const struct rhashtable_params params)
934{
935 const char *key = rht_obj(ht, obj);
936
937 BUG_ON(ht->p.obj_hashfn);
938
939 return __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params,
940 false);
941}
942
943/**
944 * rhashtable_lookup_insert_key - search and insert object to hash table
945 * with explicit key
946 * @ht: hash table
947 * @key: key
948 * @obj: pointer to hash head inside object
949 * @params: hash table parameters
950 *
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000951 * Lookups may occur in parallel with hashtable mutations and resizing.
952 *
953 * Will trigger an automatic deferred table resizing if residency in the
954 * table grows beyond 70%.
955 *
956 * Returns zero on success.
957 */
958static inline int rhashtable_lookup_insert_key(
959 struct rhashtable *ht, const void *key, struct rhash_head *obj,
960 const struct rhashtable_params params)
961{
962 void *ret;
963
964 BUG_ON(!ht->p.obj_hashfn || !key);
965
966 ret = __rhashtable_insert_fast(ht, key, obj, params, false);
967 if (IS_ERR(ret))
968 return PTR_ERR(ret);
969
970 return ret == NULL ? 0 : -EEXIST;
971}
972
973/**
974 * rhashtable_lookup_get_insert_key - lookup and insert object into hash table
975 * @ht: hash table
976 * @obj: pointer to hash head inside object
977 * @params: hash table parameters
978 * @data: pointer to element data already in hashes
979 *
980 * Just like rhashtable_lookup_insert_key(), but this function returns the
981 * object if it exists, NULL if it does not and the insertion was successful,
982 * and an ERR_PTR otherwise.
983 */
984static inline void *rhashtable_lookup_get_insert_key(
985 struct rhashtable *ht, const void *key, struct rhash_head *obj,
986 const struct rhashtable_params params)
987{
988 BUG_ON(!ht->p.obj_hashfn || !key);
989
990 return __rhashtable_insert_fast(ht, key, obj, params, false);
991}
992
993/* Internal function, please use rhashtable_remove_fast() instead */
994static inline int __rhashtable_remove_fast_one(
995 struct rhashtable *ht, struct bucket_table *tbl,
996 struct rhash_head *obj, const struct rhashtable_params params,
997 bool rhlist)
998{
David Brazdil0f672f62019-12-10 10:32:29 +0000999 struct rhash_lock_head **bkt;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001000 struct rhash_head __rcu **pprev;
1001 struct rhash_head *he;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001002 unsigned int hash;
1003 int err = -ENOENT;
1004
1005 hash = rht_head_hashfn(ht, tbl, obj, params);
David Brazdil0f672f62019-12-10 10:32:29 +00001006 bkt = rht_bucket_var(tbl, hash);
1007 if (!bkt)
1008 return -ENOENT;
1009 pprev = NULL;
1010 rht_lock(tbl, bkt);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001011
David Brazdil0f672f62019-12-10 10:32:29 +00001012 rht_for_each_from(he, rht_ptr(bkt, tbl, hash), tbl, hash) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001013 struct rhlist_head *list;
1014
1015 list = container_of(he, struct rhlist_head, rhead);
1016
1017 if (he != obj) {
1018 struct rhlist_head __rcu **lpprev;
1019
1020 pprev = &he->next;
1021
1022 if (!rhlist)
1023 continue;
1024
1025 do {
1026 lpprev = &list->next;
1027 list = rht_dereference_bucket(list->next,
1028 tbl, hash);
1029 } while (list && obj != &list->rhead);
1030
1031 if (!list)
1032 continue;
1033
1034 list = rht_dereference_bucket(list->next, tbl, hash);
1035 RCU_INIT_POINTER(*lpprev, list);
1036 err = 0;
1037 break;
1038 }
1039
1040 obj = rht_dereference_bucket(obj->next, tbl, hash);
1041 err = 1;
1042
1043 if (rhlist) {
1044 list = rht_dereference_bucket(list->next, tbl, hash);
1045 if (list) {
1046 RCU_INIT_POINTER(list->rhead.next, obj);
1047 obj = &list->rhead;
1048 err = 0;
1049 }
1050 }
1051
David Brazdil0f672f62019-12-10 10:32:29 +00001052 if (pprev) {
1053 rcu_assign_pointer(*pprev, obj);
1054 rht_unlock(tbl, bkt);
1055 } else {
1056 rht_assign_unlock(tbl, bkt, obj);
1057 }
1058 goto unlocked;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001059 }
1060
David Brazdil0f672f62019-12-10 10:32:29 +00001061 rht_unlock(tbl, bkt);
1062unlocked:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001063 if (err > 0) {
1064 atomic_dec(&ht->nelems);
1065 if (unlikely(ht->p.automatic_shrinking &&
1066 rht_shrink_below_30(ht, tbl)))
1067 schedule_work(&ht->run_work);
1068 err = 0;
1069 }
1070
1071 return err;
1072}
1073
1074/* Internal function, please use rhashtable_remove_fast() instead */
1075static inline int __rhashtable_remove_fast(
1076 struct rhashtable *ht, struct rhash_head *obj,
1077 const struct rhashtable_params params, bool rhlist)
1078{
1079 struct bucket_table *tbl;
1080 int err;
1081
1082 rcu_read_lock();
1083
1084 tbl = rht_dereference_rcu(ht->tbl, ht);
1085
1086 /* Because we have already taken (and released) the bucket
1087 * lock in old_tbl, if we find that future_tbl is not yet
1088 * visible then that guarantees the entry to still be in
1089 * the old tbl if it exists.
1090 */
1091 while ((err = __rhashtable_remove_fast_one(ht, tbl, obj, params,
1092 rhlist)) &&
1093 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
1094 ;
1095
1096 rcu_read_unlock();
1097
1098 return err;
1099}
1100
1101/**
1102 * rhashtable_remove_fast - remove object from hash table
1103 * @ht: hash table
1104 * @obj: pointer to hash head inside object
1105 * @params: hash table parameters
1106 *
1107 * Since the hash chain is single linked, the removal operation needs to
1108 * walk the bucket chain upon removal. The removal operation is thus
1109 * considerable slow if the hash table is not correctly sized.
1110 *
1111 * Will automatically shrink the table if permitted when residency drops
1112 * below 30%.
1113 *
1114 * Returns zero on success, -ENOENT if the entry could not be found.
1115 */
1116static inline int rhashtable_remove_fast(
1117 struct rhashtable *ht, struct rhash_head *obj,
1118 const struct rhashtable_params params)
1119{
1120 return __rhashtable_remove_fast(ht, obj, params, false);
1121}
1122
1123/**
1124 * rhltable_remove - remove object from hash list table
1125 * @hlt: hash list table
1126 * @list: pointer to hash list head inside object
1127 * @params: hash table parameters
1128 *
1129 * Since the hash chain is single linked, the removal operation needs to
1130 * walk the bucket chain upon removal. The removal operation is thus
1131 * considerable slow if the hash table is not correctly sized.
1132 *
1133 * Will automatically shrink the table if permitted when residency drops
1134 * below 30%
1135 *
1136 * Returns zero on success, -ENOENT if the entry could not be found.
1137 */
1138static inline int rhltable_remove(
1139 struct rhltable *hlt, struct rhlist_head *list,
1140 const struct rhashtable_params params)
1141{
1142 return __rhashtable_remove_fast(&hlt->ht, &list->rhead, params, true);
1143}
1144
1145/* Internal function, please use rhashtable_replace_fast() instead */
1146static inline int __rhashtable_replace_fast(
1147 struct rhashtable *ht, struct bucket_table *tbl,
1148 struct rhash_head *obj_old, struct rhash_head *obj_new,
1149 const struct rhashtable_params params)
1150{
David Brazdil0f672f62019-12-10 10:32:29 +00001151 struct rhash_lock_head **bkt;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001152 struct rhash_head __rcu **pprev;
1153 struct rhash_head *he;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001154 unsigned int hash;
1155 int err = -ENOENT;
1156
1157 /* Minimally, the old and new objects must have same hash
1158 * (which should mean identifiers are the same).
1159 */
1160 hash = rht_head_hashfn(ht, tbl, obj_old, params);
1161 if (hash != rht_head_hashfn(ht, tbl, obj_new, params))
1162 return -EINVAL;
1163
David Brazdil0f672f62019-12-10 10:32:29 +00001164 bkt = rht_bucket_var(tbl, hash);
1165 if (!bkt)
1166 return -ENOENT;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001167
David Brazdil0f672f62019-12-10 10:32:29 +00001168 pprev = NULL;
1169 rht_lock(tbl, bkt);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001170
David Brazdil0f672f62019-12-10 10:32:29 +00001171 rht_for_each_from(he, rht_ptr(bkt, tbl, hash), tbl, hash) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001172 if (he != obj_old) {
1173 pprev = &he->next;
1174 continue;
1175 }
1176
1177 rcu_assign_pointer(obj_new->next, obj_old->next);
David Brazdil0f672f62019-12-10 10:32:29 +00001178 if (pprev) {
1179 rcu_assign_pointer(*pprev, obj_new);
1180 rht_unlock(tbl, bkt);
1181 } else {
1182 rht_assign_unlock(tbl, bkt, obj_new);
1183 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001184 err = 0;
David Brazdil0f672f62019-12-10 10:32:29 +00001185 goto unlocked;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001186 }
1187
David Brazdil0f672f62019-12-10 10:32:29 +00001188 rht_unlock(tbl, bkt);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001189
David Brazdil0f672f62019-12-10 10:32:29 +00001190unlocked:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001191 return err;
1192}
1193
1194/**
1195 * rhashtable_replace_fast - replace an object in hash table
1196 * @ht: hash table
1197 * @obj_old: pointer to hash head inside object being replaced
1198 * @obj_new: pointer to hash head inside object which is new
1199 * @params: hash table parameters
1200 *
1201 * Replacing an object doesn't affect the number of elements in the hash table
1202 * or bucket, so we don't need to worry about shrinking or expanding the
1203 * table here.
1204 *
1205 * Returns zero on success, -ENOENT if the entry could not be found,
1206 * -EINVAL if hash is not the same for the old and new objects.
1207 */
1208static inline int rhashtable_replace_fast(
1209 struct rhashtable *ht, struct rhash_head *obj_old,
1210 struct rhash_head *obj_new,
1211 const struct rhashtable_params params)
1212{
1213 struct bucket_table *tbl;
1214 int err;
1215
1216 rcu_read_lock();
1217
1218 tbl = rht_dereference_rcu(ht->tbl, ht);
1219
1220 /* Because we have already taken (and released) the bucket
1221 * lock in old_tbl, if we find that future_tbl is not yet
1222 * visible then that guarantees the entry to still be in
1223 * the old tbl if it exists.
1224 */
1225 while ((err = __rhashtable_replace_fast(ht, tbl, obj_old,
1226 obj_new, params)) &&
1227 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
1228 ;
1229
1230 rcu_read_unlock();
1231
1232 return err;
1233}
1234
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001235/**
1236 * rhltable_walk_enter - Initialise an iterator
1237 * @hlt: Table to walk over
1238 * @iter: Hash table Iterator
1239 *
1240 * This function prepares a hash table walk.
1241 *
1242 * Note that if you restart a walk after rhashtable_walk_stop you
1243 * may see the same object twice. Also, you may miss objects if
1244 * there are removals in between rhashtable_walk_stop and the next
1245 * call to rhashtable_walk_start.
1246 *
1247 * For a completely stable walk you should construct your own data
1248 * structure outside the hash table.
1249 *
1250 * This function may be called from any process context, including
1251 * non-preemptable context, but cannot be called from softirq or
1252 * hardirq context.
1253 *
1254 * You must call rhashtable_walk_exit after this function returns.
1255 */
1256static inline void rhltable_walk_enter(struct rhltable *hlt,
1257 struct rhashtable_iter *iter)
1258{
1259 return rhashtable_walk_enter(&hlt->ht, iter);
1260}
1261
1262/**
1263 * rhltable_free_and_destroy - free elements and destroy hash list table
1264 * @hlt: the hash list table to destroy
1265 * @free_fn: callback to release resources of element
1266 * @arg: pointer passed to free_fn
1267 *
1268 * See documentation for rhashtable_free_and_destroy.
1269 */
1270static inline void rhltable_free_and_destroy(struct rhltable *hlt,
1271 void (*free_fn)(void *ptr,
1272 void *arg),
1273 void *arg)
1274{
1275 return rhashtable_free_and_destroy(&hlt->ht, free_fn, arg);
1276}
1277
1278static inline void rhltable_destroy(struct rhltable *hlt)
1279{
1280 return rhltable_free_and_destroy(hlt, NULL, NULL);
1281}
1282
1283#endif /* _LINUX_RHASHTABLE_H */