blob: ccbbafdf5547c8994d0153b2d4cdad840bb5c74f [file] [log] [blame]
NeilBrown0eb71a92018-06-18 12:52:50 +10001/* SPDX-License-Identifier: GPL-2.0 */
Thomas Graf7e1e7762014-08-02 11:47:44 +02002/*
3 * Resizable, Scalable, Concurrent Hash Table
4 *
Herbert Xuca268932016-09-19 19:00:09 +08005 * Copyright (c) 2015-2016 Herbert Xu <herbert@gondor.apana.org.au>
Thomas Grafb5e2c152015-03-24 20:42:19 +00006 * Copyright (c) 2014-2015 Thomas Graf <tgraf@suug.ch>
Thomas Graf7e1e7762014-08-02 11:47:44 +02007 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
8 *
Thomas Graf7e1e7762014-08-02 11:47:44 +02009 * Code partially derived from nft_hash
Herbert Xudc0ee262015-03-20 21:57:06 +110010 * Rewritten with rehash code from br_multicast plus single list
11 * pointer as suggested by Josh Triplett
Thomas Graf7e1e7762014-08-02 11:47:44 +020012 *
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
Herbert Xu3cf92222015-12-03 20:41:29 +080021#include <linux/err.h>
Herbert Xu6626af62015-03-20 18:18:45 -040022#include <linux/errno.h>
Herbert Xu31ccde22015-03-24 00:50:21 +110023#include <linux/jhash.h>
Thomas Graff89bd6f2015-01-02 23:00:21 +010024#include <linux/list_nulls.h>
Thomas Graf97defe12015-01-02 23:00:20 +010025#include <linux/workqueue.h>
Ingo Molnarb2d09102017-02-04 01:27:20 +010026#include <linux/rculist.h>
NeilBrown8f0db012019-04-02 10:07:45 +110027#include <linux/bit_spinlock.h>
Thomas Graf7e1e7762014-08-02 11:47:44 +020028
NeilBrown0eb71a92018-06-18 12:52:50 +100029#include <linux/rhashtable-types.h>
Thomas Graff89bd6f2015-01-02 23:00:21 +010030/*
NeilBrown8f0db012019-04-02 10:07:45 +110031 * 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.
Thomas Graff89bd6f2015-01-02 23:00:21 +010034 * The end of the chain is marked with a special nulls marks which has
NeilBrown8f0db012019-04-02 10:07:45 +110035 * the least significant bit set but otherwise stores the address of
36 * the hash bucket. This allows us to be be sure we've found the end
37 * of the right list.
38 * The value stored in the hash bucket has BIT(2) used as a lock bit.
39 * This bit must be atomically set before any changes are made to
40 * the chain. To avoid dereferencing this pointer without clearing
41 * the bit first, we use an opaque 'struct rhash_lock_head *' for the
42 * pointer stored in the bucket. This struct needs to be defined so
43 * that rcu_derefernce() works on it, but it has no content so a
44 * cast is needed for it to be useful. This ensures it isn't
45 * used by mistake with clearing the lock bit first.
Thomas Graff89bd6f2015-01-02 23:00:21 +010046 */
NeilBrown8f0db012019-04-02 10:07:45 +110047struct rhash_lock_head {};
Herbert Xu02fd97c2015-03-20 21:57:00 +110048
Florian Westphal5f8ddea2017-04-16 02:55:09 +020049/* 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
Thomas Graf97defe12015-01-02 23:00:20 +010064/**
65 * struct bucket_table - Table of hash buckets
66 * @size: Number of hash buckets
Herbert Xuda204202017-02-11 19:26:47 +080067 * @nest: Number of bits of first-level nested table.
Herbert Xu63d512d2015-03-14 13:57:24 +110068 * @rehash: Current bucket being rehashed
Herbert Xu988dfbd2015-03-10 09:27:55 +110069 * @hash_rnd: Random seed to fold into hash
Herbert Xueddee5ba2015-03-14 13:57:20 +110070 * @walkers: List of active walkers
Herbert Xu9d901bc2015-03-14 13:57:23 +110071 * @rcu: RCU structure for freeing the table
Herbert Xuc4db8842015-03-14 13:57:25 +110072 * @future_tbl: Table under construction during rehashing
Herbert Xuda204202017-02-11 19:26:47 +080073 * @ntbl: Nested table used when out of memory.
Thomas Graf97defe12015-01-02 23:00:20 +010074 * @buckets: size * hash buckets
75 */
Thomas Graf7e1e7762014-08-02 11:47:44 +020076struct bucket_table {
Herbert Xu63d512d2015-03-14 13:57:24 +110077 unsigned int size;
Herbert Xuda204202017-02-11 19:26:47 +080078 unsigned int nest;
Herbert Xu988dfbd2015-03-10 09:27:55 +110079 u32 hash_rnd;
Herbert Xueddee5ba2015-03-14 13:57:20 +110080 struct list_head walkers;
Herbert Xu9d901bc2015-03-14 13:57:23 +110081 struct rcu_head rcu;
Eric Dumazetb9ebafb2015-02-20 06:48:57 -080082
Herbert Xuc4db8842015-03-14 13:57:25 +110083 struct bucket_table __rcu *future_tbl;
84
NeilBrown8f0db012019-04-02 10:07:45 +110085 struct rhash_lock_head __rcu *buckets[] ____cacheline_aligned_in_smp;
Thomas Graf7e1e7762014-08-02 11:47:44 +020086};
87
NeilBrown82208d0d2018-11-30 10:26:50 +110088/*
NeilBrown8f0db012019-04-02 10:07:45 +110089 * We lock a bucket by setting BIT(1) in the pointer - this is always
90 * zero in real pointers and in the nulls marker.
91 * bit_spin_locks do not handle contention well, but the whole point
92 * of the hashtable design is to achieve minimum per-bucket contention.
93 * A nested hash table might not have a bucket pointer. In that case
94 * we cannot get a lock. For remove and replace the bucket cannot be
95 * interesting and doesn't need locking.
96 * For insert we allocate the bucket if this is the last bucket_table,
97 * and then take the lock.
98 * Sometimes we unlock a bucket by writing a new pointer there. In that
99 * case we don't need to unlock, but we do need to reset state such as
100 * local_bh. For that we have rht_assign_unlock(). As rcu_assign_pointer()
101 * provides the same release semantics that bit_spin_unlock() provides,
102 * this is safe.
103 */
104
105static inline void rht_lock(struct rhash_lock_head **bkt)
106{
107 local_bh_disable();
108 bit_spin_lock(1, (unsigned long *)bkt);
109}
110
111static inline void rht_unlock(struct rhash_lock_head **bkt)
112{
113 bit_spin_unlock(1, (unsigned long *)bkt);
114 local_bh_enable();
115}
116
117static inline void rht_assign_unlock(struct rhash_lock_head **bkt,
118 struct rhash_head *obj)
119{
120 struct rhash_head **p = (struct rhash_head **)bkt;
121
122 rcu_assign_pointer(*p, obj);
123 preempt_enable();
124 __release(bitlock);
125 local_bh_enable();
126}
127
128/*
129 * If 'p' is a bucket head and might be locked:
130 * rht_ptr() returns the address without the lock bit.
131 * rht_ptr_locked() returns the address WITH the lock bit.
132 */
133static inline struct rhash_head __rcu *rht_ptr(const struct rhash_lock_head *p)
134{
135 return (void *)(((unsigned long)p) & ~BIT(1));
136}
137
138static inline struct rhash_lock_head __rcu *rht_ptr_locked(const
139 struct rhash_head *p)
140{
141 return (void *)(((unsigned long)p) | BIT(1));
142}
143
144/*
NeilBrown82208d0d2018-11-30 10:26:50 +1100145 * NULLS_MARKER() expects a hash value with the low
146 * bits mostly likely to be significant, and it discards
147 * the msb.
148 * We git it an address, in which the bottom 2 bits are
149 * always 0, and the msb might be significant.
150 * So we shift the address down one bit to align with
151 * expectations and avoid losing a significant bit.
152 */
153#define RHT_NULLS_MARKER(ptr) \
154 ((void *)NULLS_MARKER(((unsigned long) (ptr)) >> 1))
NeilBrown9b4f64a2018-06-18 12:52:50 +1000155#define INIT_RHT_NULLS_HEAD(ptr) \
NeilBrown82208d0d2018-11-30 10:26:50 +1100156 ((ptr) = RHT_NULLS_MARKER(&(ptr)))
Thomas Graff89bd6f2015-01-02 23:00:21 +0100157
158static inline bool rht_is_a_nulls(const struct rhash_head *ptr)
159{
160 return ((unsigned long) ptr & 1);
161}
162
Herbert Xu02fd97c2015-03-20 21:57:00 +1100163static inline void *rht_obj(const struct rhashtable *ht,
164 const struct rhash_head *he)
165{
166 return (char *)he - ht->p.head_offset;
167}
168
169static inline unsigned int rht_bucket_index(const struct bucket_table *tbl,
170 unsigned int hash)
171{
NeilBrown9f9a7072018-06-18 12:52:50 +1000172 return hash & (tbl->size - 1);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100173}
174
Tom Herbert2b860932017-12-04 10:31:43 -0800175static inline unsigned int rht_key_get_hash(struct rhashtable *ht,
176 const void *key, const struct rhashtable_params params,
177 unsigned int hash_rnd)
Herbert Xu02fd97c2015-03-20 21:57:00 +1100178{
Thomas Graf299e5c32015-03-24 14:18:17 +0100179 unsigned int hash;
Herbert Xude91b252015-03-24 00:50:20 +1100180
Herbert Xu31ccde22015-03-24 00:50:21 +1100181 /* params must be equal to ht->p if it isn't constant. */
182 if (!__builtin_constant_p(params.key_len))
Tom Herbert2b860932017-12-04 10:31:43 -0800183 hash = ht->p.hashfn(key, ht->key_len, hash_rnd);
Herbert Xu31ccde22015-03-24 00:50:21 +1100184 else if (params.key_len) {
Thomas Graf299e5c32015-03-24 14:18:17 +0100185 unsigned int key_len = params.key_len;
Herbert Xu31ccde22015-03-24 00:50:21 +1100186
187 if (params.hashfn)
Tom Herbert2b860932017-12-04 10:31:43 -0800188 hash = params.hashfn(key, key_len, hash_rnd);
Herbert Xu31ccde22015-03-24 00:50:21 +1100189 else if (key_len & (sizeof(u32) - 1))
Tom Herbert2b860932017-12-04 10:31:43 -0800190 hash = jhash(key, key_len, hash_rnd);
Herbert Xu31ccde22015-03-24 00:50:21 +1100191 else
Tom Herbert2b860932017-12-04 10:31:43 -0800192 hash = jhash2(key, key_len / sizeof(u32), hash_rnd);
Herbert Xu31ccde22015-03-24 00:50:21 +1100193 } else {
Thomas Graf299e5c32015-03-24 14:18:17 +0100194 unsigned int key_len = ht->p.key_len;
Herbert Xu31ccde22015-03-24 00:50:21 +1100195
196 if (params.hashfn)
Tom Herbert2b860932017-12-04 10:31:43 -0800197 hash = params.hashfn(key, key_len, hash_rnd);
Herbert Xu31ccde22015-03-24 00:50:21 +1100198 else
Tom Herbert2b860932017-12-04 10:31:43 -0800199 hash = jhash(key, key_len, hash_rnd);
Herbert Xu31ccde22015-03-24 00:50:21 +1100200 }
201
Tom Herbert2b860932017-12-04 10:31:43 -0800202 return hash;
203}
204
205static inline unsigned int rht_key_hashfn(
206 struct rhashtable *ht, const struct bucket_table *tbl,
207 const void *key, const struct rhashtable_params params)
208{
209 unsigned int hash = rht_key_get_hash(ht, key, params, tbl->hash_rnd);
210
Herbert Xu31ccde22015-03-24 00:50:21 +1100211 return rht_bucket_index(tbl, hash);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100212}
213
214static inline unsigned int rht_head_hashfn(
215 struct rhashtable *ht, const struct bucket_table *tbl,
216 const struct rhash_head *he, const struct rhashtable_params params)
217{
218 const char *ptr = rht_obj(ht, he);
219
220 return likely(params.obj_hashfn) ?
Patrick McHardy49f7b332015-03-25 13:07:45 +0000221 rht_bucket_index(tbl, params.obj_hashfn(ptr, params.key_len ?:
222 ht->p.key_len,
223 tbl->hash_rnd)) :
Herbert Xu02fd97c2015-03-20 21:57:00 +1100224 rht_key_hashfn(ht, tbl, ptr + params.key_offset, params);
225}
226
227/**
228 * rht_grow_above_75 - returns true if nelems > 0.75 * table-size
229 * @ht: hash table
230 * @tbl: current table
231 */
232static inline bool rht_grow_above_75(const struct rhashtable *ht,
233 const struct bucket_table *tbl)
234{
235 /* Expand table when exceeding 75% load */
236 return atomic_read(&ht->nelems) > (tbl->size / 4 * 3) &&
237 (!ht->p.max_size || tbl->size < ht->p.max_size);
238}
239
240/**
241 * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size
242 * @ht: hash table
243 * @tbl: current table
244 */
245static inline bool rht_shrink_below_30(const struct rhashtable *ht,
246 const struct bucket_table *tbl)
247{
248 /* Shrink table beneath 30% load */
249 return atomic_read(&ht->nelems) < (tbl->size * 3 / 10) &&
250 tbl->size > ht->p.min_size;
251}
252
Herbert Xuccd57b12015-03-24 00:50:28 +1100253/**
254 * rht_grow_above_100 - returns true if nelems > table-size
255 * @ht: hash table
256 * @tbl: current table
257 */
258static inline bool rht_grow_above_100(const struct rhashtable *ht,
259 const struct bucket_table *tbl)
260{
Johannes Berg1d8dc3d2015-04-23 16:38:43 +0200261 return atomic_read(&ht->nelems) > tbl->size &&
262 (!ht->p.max_size || tbl->size < ht->p.max_size);
Herbert Xuccd57b12015-03-24 00:50:28 +1100263}
264
Herbert Xu07ee0722015-05-15 11:30:47 +0800265/**
266 * rht_grow_above_max - returns true if table is above maximum
267 * @ht: hash table
268 * @tbl: current table
269 */
270static inline bool rht_grow_above_max(const struct rhashtable *ht,
271 const struct bucket_table *tbl)
272{
Herbert Xu6d684e52017-04-27 13:44:51 +0800273 return atomic_read(&ht->nelems) >= ht->max_elems;
Herbert Xu07ee0722015-05-15 11:30:47 +0800274}
275
Thomas Graf7e1e7762014-08-02 11:47:44 +0200276#ifdef CONFIG_PROVE_LOCKING
Thomas Graf97defe12015-01-02 23:00:20 +0100277int lockdep_rht_mutex_is_held(struct rhashtable *ht);
Thomas Graf88d6ed12015-01-02 23:00:16 +0100278int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200279#else
Thomas Graf97defe12015-01-02 23:00:20 +0100280static inline int lockdep_rht_mutex_is_held(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200281{
282 return 1;
283}
Thomas Graf88d6ed12015-01-02 23:00:16 +0100284
285static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl,
286 u32 hash)
287{
288 return 1;
289}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200290#endif /* CONFIG_PROVE_LOCKING */
291
Herbert Xuca268932016-09-19 19:00:09 +0800292void *rhashtable_insert_slow(struct rhashtable *ht, const void *key,
293 struct rhash_head *obj);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200294
Herbert Xu246779d2016-08-18 16:50:56 +0800295void rhashtable_walk_enter(struct rhashtable *ht,
296 struct rhashtable_iter *iter);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100297void rhashtable_walk_exit(struct rhashtable_iter *iter);
Tom Herbert97a6ec42017-12-04 10:31:41 -0800298int rhashtable_walk_start_check(struct rhashtable_iter *iter) __acquires(RCU);
299
300static inline void rhashtable_walk_start(struct rhashtable_iter *iter)
301{
302 (void)rhashtable_walk_start_check(iter);
303}
304
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100305void *rhashtable_walk_next(struct rhashtable_iter *iter);
Tom Herbert2db54b42017-12-04 10:31:42 -0800306void *rhashtable_walk_peek(struct rhashtable_iter *iter);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100307void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU);
308
Thomas Graf6b6f3022015-03-24 14:18:20 +0100309void rhashtable_free_and_destroy(struct rhashtable *ht,
310 void (*free_fn)(void *ptr, void *arg),
311 void *arg);
Thomas Graf97defe12015-01-02 23:00:20 +0100312void rhashtable_destroy(struct rhashtable *ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200313
NeilBrown8f0db012019-04-02 10:07:45 +1100314struct rhash_lock_head __rcu **rht_bucket_nested(const struct bucket_table *tbl,
315 unsigned int hash);
316struct rhash_lock_head __rcu **__rht_bucket_nested(const struct bucket_table *tbl,
Herbert Xuda204202017-02-11 19:26:47 +0800317 unsigned int hash);
NeilBrown8f0db012019-04-02 10:07:45 +1100318struct rhash_lock_head __rcu **rht_bucket_nested_insert(struct rhashtable *ht,
319 struct bucket_table *tbl,
320 unsigned int hash);
Herbert Xuda204202017-02-11 19:26:47 +0800321
Thomas Graf7e1e7762014-08-02 11:47:44 +0200322#define rht_dereference(p, ht) \
323 rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht))
324
325#define rht_dereference_rcu(p, ht) \
326 rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht))
327
Thomas Graf88d6ed12015-01-02 23:00:16 +0100328#define rht_dereference_bucket(p, tbl, hash) \
329 rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200330
Thomas Graf88d6ed12015-01-02 23:00:16 +0100331#define rht_dereference_bucket_rcu(p, tbl, hash) \
332 rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash))
333
334#define rht_entry(tpos, pos, member) \
335 ({ tpos = container_of(pos, typeof(*tpos), member); 1; })
336
NeilBrown8f0db012019-04-02 10:07:45 +1100337static inline struct rhash_lock_head __rcu *const *rht_bucket(
Herbert Xuda204202017-02-11 19:26:47 +0800338 const struct bucket_table *tbl, unsigned int hash)
339{
340 return unlikely(tbl->nest) ? rht_bucket_nested(tbl, hash) :
341 &tbl->buckets[hash];
342}
343
NeilBrown8f0db012019-04-02 10:07:45 +1100344static inline struct rhash_lock_head __rcu **rht_bucket_var(
Herbert Xuda204202017-02-11 19:26:47 +0800345 struct bucket_table *tbl, unsigned int hash)
346{
NeilBrownff302db2019-04-02 10:07:45 +1100347 return unlikely(tbl->nest) ? __rht_bucket_nested(tbl, hash) :
Herbert Xuda204202017-02-11 19:26:47 +0800348 &tbl->buckets[hash];
349}
350
NeilBrown8f0db012019-04-02 10:07:45 +1100351static inline struct rhash_lock_head __rcu **rht_bucket_insert(
Herbert Xuda204202017-02-11 19:26:47 +0800352 struct rhashtable *ht, struct bucket_table *tbl, unsigned int hash)
353{
354 return unlikely(tbl->nest) ? rht_bucket_nested_insert(ht, tbl, hash) :
355 &tbl->buckets[hash];
356}
357
Thomas Graf88d6ed12015-01-02 23:00:16 +0100358/**
NeilBrownf7ad68b2019-03-21 14:42:40 +1100359 * rht_for_each_from - iterate over hash chain from given head
Thomas Graf88d6ed12015-01-02 23:00:16 +0100360 * @pos: the &struct rhash_head to use as a loop cursor.
NeilBrownf7ad68b2019-03-21 14:42:40 +1100361 * @head: the &struct rhash_head to start from
Thomas Graf88d6ed12015-01-02 23:00:16 +0100362 * @tbl: the &struct bucket_table
363 * @hash: the hash value / bucket index
364 */
NeilBrownf7ad68b2019-03-21 14:42:40 +1100365#define rht_for_each_from(pos, head, tbl, hash) \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100366 for (pos = rht_dereference_bucket(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100367 !rht_is_a_nulls(pos); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100368 pos = rht_dereference_bucket((pos)->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200369
370/**
371 * rht_for_each - iterate over hash chain
Thomas Graf88d6ed12015-01-02 23:00:16 +0100372 * @pos: the &struct rhash_head to use as a loop cursor.
373 * @tbl: the &struct bucket_table
374 * @hash: the hash value / bucket index
Thomas Graf7e1e7762014-08-02 11:47:44 +0200375 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100376#define rht_for_each(pos, tbl, hash) \
NeilBrown8f0db012019-04-02 10:07:45 +1100377 rht_for_each_from(pos, rht_ptr(*rht_bucket(tbl, hash)), tbl, hash)
Thomas Graf88d6ed12015-01-02 23:00:16 +0100378
379/**
NeilBrownf7ad68b2019-03-21 14:42:40 +1100380 * rht_for_each_entry_from - iterate over hash chain from given head
Thomas Graf88d6ed12015-01-02 23:00:16 +0100381 * @tpos: the type * to use as a loop cursor.
382 * @pos: the &struct rhash_head to use as a loop cursor.
NeilBrownf7ad68b2019-03-21 14:42:40 +1100383 * @head: the &struct rhash_head to start from
Thomas Graf88d6ed12015-01-02 23:00:16 +0100384 * @tbl: the &struct bucket_table
385 * @hash: the hash value / bucket index
386 * @member: name of the &struct rhash_head within the hashable struct.
387 */
NeilBrownf7ad68b2019-03-21 14:42:40 +1100388#define rht_for_each_entry_from(tpos, pos, head, tbl, hash, member) \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100389 for (pos = rht_dereference_bucket(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100390 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100391 pos = rht_dereference_bucket((pos)->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200392
393/**
394 * rht_for_each_entry - iterate over hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100395 * @tpos: the type * to use as a loop cursor.
396 * @pos: the &struct rhash_head to use as a loop cursor.
397 * @tbl: the &struct bucket_table
398 * @hash: the hash value / bucket index
399 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200400 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100401#define rht_for_each_entry(tpos, pos, tbl, hash, member) \
NeilBrown8f0db012019-04-02 10:07:45 +1100402 rht_for_each_entry_from(tpos, pos, rht_ptr(*rht_bucket(tbl, hash)), \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100403 tbl, hash, member)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200404
405/**
406 * rht_for_each_entry_safe - safely iterate over hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100407 * @tpos: the type * to use as a loop cursor.
408 * @pos: the &struct rhash_head to use as a loop cursor.
409 * @next: the &struct rhash_head to use as next in loop cursor.
410 * @tbl: the &struct bucket_table
411 * @hash: the hash value / bucket index
412 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200413 *
414 * This hash chain list-traversal primitive allows for the looped code to
415 * remove the loop cursor from the list.
416 */
Herbert Xuda204202017-02-11 19:26:47 +0800417#define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \
NeilBrown8f0db012019-04-02 10:07:45 +1100418 for (pos = rht_dereference_bucket(rht_ptr(*rht_bucket(tbl, hash)), \
419 tbl, hash), \
Herbert Xuda204202017-02-11 19:26:47 +0800420 next = !rht_is_a_nulls(pos) ? \
421 rht_dereference_bucket(pos->next, tbl, hash) : NULL; \
422 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
423 pos = next, \
424 next = !rht_is_a_nulls(pos) ? \
Patrick McHardy607954b2015-01-21 11:12:13 +0000425 rht_dereference_bucket(pos->next, tbl, hash) : NULL)
Thomas Graf88d6ed12015-01-02 23:00:16 +0100426
427/**
NeilBrownf7ad68b2019-03-21 14:42:40 +1100428 * rht_for_each_rcu_from - iterate over rcu hash chain from given head
Thomas Graf88d6ed12015-01-02 23:00:16 +0100429 * @pos: the &struct rhash_head to use as a loop cursor.
NeilBrownf7ad68b2019-03-21 14:42:40 +1100430 * @head: the &struct rhash_head to start from
Thomas Graf88d6ed12015-01-02 23:00:16 +0100431 * @tbl: the &struct bucket_table
432 * @hash: the hash value / bucket index
433 *
434 * This hash chain list-traversal primitive may safely run concurrently with
435 * the _rcu mutation primitives such as rhashtable_insert() as long as the
436 * traversal is guarded by rcu_read_lock().
437 */
NeilBrownf7ad68b2019-03-21 14:42:40 +1100438#define rht_for_each_rcu_from(pos, head, tbl, hash) \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100439 for (({barrier(); }), \
440 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100441 !rht_is_a_nulls(pos); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100442 pos = rcu_dereference_raw(pos->next))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200443
444/**
445 * rht_for_each_rcu - iterate over rcu hash chain
Thomas Graf88d6ed12015-01-02 23:00:16 +0100446 * @pos: the &struct rhash_head to use as a loop cursor.
447 * @tbl: the &struct bucket_table
448 * @hash: the hash value / bucket index
Thomas Graf7e1e7762014-08-02 11:47:44 +0200449 *
450 * This hash chain list-traversal primitive may safely run concurrently with
Thomas Graf88d6ed12015-01-02 23:00:16 +0100451 * the _rcu mutation primitives such as rhashtable_insert() as long as the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200452 * traversal is guarded by rcu_read_lock().
453 */
NeilBrown8f0db012019-04-02 10:07:45 +1100454#define rht_for_each_rcu(pos, tbl, hash) \
455 for (({barrier(); }), \
456 pos = rht_ptr(rht_dereference_bucket_rcu( \
457 *rht_bucket(tbl, hash), tbl, hash)); \
458 !rht_is_a_nulls(pos); \
459 pos = rcu_dereference_raw(pos->next))
Thomas Graf88d6ed12015-01-02 23:00:16 +0100460
461/**
NeilBrownf7ad68b2019-03-21 14:42:40 +1100462 * rht_for_each_entry_rcu_from - iterated over rcu hash chain from given head
Thomas Graf88d6ed12015-01-02 23:00:16 +0100463 * @tpos: the type * to use as a loop cursor.
464 * @pos: the &struct rhash_head to use as a loop cursor.
NeilBrownf7ad68b2019-03-21 14:42:40 +1100465 * @head: the &struct rhash_head to start from
Thomas Graf88d6ed12015-01-02 23:00:16 +0100466 * @tbl: the &struct bucket_table
467 * @hash: the hash value / bucket index
468 * @member: name of the &struct rhash_head within the hashable struct.
469 *
470 * This hash chain list-traversal primitive may safely run concurrently with
471 * the _rcu mutation primitives such as rhashtable_insert() as long as the
472 * traversal is guarded by rcu_read_lock().
473 */
NeilBrownf7ad68b2019-03-21 14:42:40 +1100474#define rht_for_each_entry_rcu_from(tpos, pos, head, tbl, hash, member) \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100475 for (({barrier(); }), \
476 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100477 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100478 pos = rht_dereference_bucket_rcu(pos->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200479
480/**
481 * rht_for_each_entry_rcu - iterate over rcu hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100482 * @tpos: the type * to use as a loop cursor.
483 * @pos: the &struct rhash_head to use as a loop cursor.
484 * @tbl: the &struct bucket_table
485 * @hash: the hash value / bucket index
486 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200487 *
488 * This hash chain list-traversal primitive may safely run concurrently with
Thomas Graf88d6ed12015-01-02 23:00:16 +0100489 * the _rcu mutation primitives such as rhashtable_insert() as long as the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200490 * traversal is guarded by rcu_read_lock().
491 */
Herbert Xuda204202017-02-11 19:26:47 +0800492#define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \
NeilBrown8f0db012019-04-02 10:07:45 +1100493 rht_for_each_entry_rcu_from(tpos, pos, \
494 rht_ptr(*rht_bucket(tbl, hash)), \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100495 tbl, hash, member)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200496
Herbert Xuca268932016-09-19 19:00:09 +0800497/**
498 * rhl_for_each_rcu - iterate over rcu hash table list
499 * @pos: the &struct rlist_head to use as a loop cursor.
500 * @list: the head of the list
501 *
502 * This hash chain list-traversal primitive should be used on the
503 * list returned by rhltable_lookup.
504 */
505#define rhl_for_each_rcu(pos, list) \
506 for (pos = list; pos; pos = rcu_dereference_raw(pos->next))
507
508/**
509 * rhl_for_each_entry_rcu - iterate over rcu hash table list of given type
510 * @tpos: the type * to use as a loop cursor.
511 * @pos: the &struct rlist_head to use as a loop cursor.
512 * @list: the head of the list
513 * @member: name of the &struct rlist_head within the hashable struct.
514 *
515 * This hash chain list-traversal primitive should be used on the
516 * list returned by rhltable_lookup.
517 */
518#define rhl_for_each_entry_rcu(tpos, pos, list, member) \
519 for (pos = list; pos && rht_entry(tpos, pos, member); \
520 pos = rcu_dereference_raw(pos->next))
521
Herbert Xu02fd97c2015-03-20 21:57:00 +1100522static inline int rhashtable_compare(struct rhashtable_compare_arg *arg,
523 const void *obj)
524{
525 struct rhashtable *ht = arg->ht;
526 const char *ptr = obj;
527
528 return memcmp(ptr + ht->p.key_offset, arg->key, ht->p.key_len);
529}
530
Herbert Xuca268932016-09-19 19:00:09 +0800531/* Internal function, do not use. */
532static inline struct rhash_head *__rhashtable_lookup(
Herbert Xu02fd97c2015-03-20 21:57:00 +1100533 struct rhashtable *ht, const void *key,
534 const struct rhashtable_params params)
535{
536 struct rhashtable_compare_arg arg = {
537 .ht = ht,
538 .key = key,
539 };
NeilBrown8f0db012019-04-02 10:07:45 +1100540 struct rhash_lock_head __rcu * const *bkt;
Herbert Xuda204202017-02-11 19:26:47 +0800541 struct bucket_table *tbl;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100542 struct rhash_head *he;
Thomas Graf299e5c32015-03-24 14:18:17 +0100543 unsigned int hash;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100544
Herbert Xu02fd97c2015-03-20 21:57:00 +1100545 tbl = rht_dereference_rcu(ht->tbl, ht);
546restart:
547 hash = rht_key_hashfn(ht, tbl, key, params);
NeilBrown8f0db012019-04-02 10:07:45 +1100548 bkt = rht_bucket(tbl, hash);
NeilBrown82208d0d2018-11-30 10:26:50 +1100549 do {
NeilBrown8f0db012019-04-02 10:07:45 +1100550 he = rht_ptr(rht_dereference_bucket_rcu(*bkt, tbl, hash));
551 rht_for_each_rcu_from(he, he, tbl, hash) {
NeilBrown82208d0d2018-11-30 10:26:50 +1100552 if (params.obj_cmpfn ?
553 params.obj_cmpfn(&arg, rht_obj(ht, he)) :
554 rhashtable_compare(&arg, rht_obj(ht, he)))
555 continue;
556 return he;
557 }
558 /* An object might have been moved to a different hash chain,
559 * while we walk along it - better check and retry.
560 */
NeilBrown8f0db012019-04-02 10:07:45 +1100561 } while (he != RHT_NULLS_MARKER(bkt));
Herbert Xu02fd97c2015-03-20 21:57:00 +1100562
563 /* Ensure we see any new tables. */
564 smp_rmb();
565
566 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
567 if (unlikely(tbl))
568 goto restart;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100569
570 return NULL;
571}
572
Herbert Xuca268932016-09-19 19:00:09 +0800573/**
574 * rhashtable_lookup - search hash table
575 * @ht: hash table
576 * @key: the pointer to the key
577 * @params: hash table parameters
578 *
579 * Computes the hash value for the key and traverses the bucket chain looking
580 * for a entry with an identical key. The first matching entry is returned.
581 *
582 * This must only be called under the RCU read lock.
583 *
584 * Returns the first entry on which the compare function returned true.
585 */
586static inline void *rhashtable_lookup(
587 struct rhashtable *ht, const void *key,
588 const struct rhashtable_params params)
589{
590 struct rhash_head *he = __rhashtable_lookup(ht, key, params);
591
592 return he ? rht_obj(ht, he) : NULL;
593}
594
595/**
596 * rhashtable_lookup_fast - search hash table, without RCU read lock
597 * @ht: hash table
598 * @key: the pointer to the key
599 * @params: hash table parameters
600 *
601 * Computes the hash value for the key and traverses the bucket chain looking
602 * for a entry with an identical key. The first matching entry is returned.
603 *
604 * Only use this function when you have other mechanisms guaranteeing
605 * that the object won't go away after the RCU read lock is released.
606 *
607 * Returns the first entry on which the compare function returned true.
608 */
609static inline void *rhashtable_lookup_fast(
610 struct rhashtable *ht, const void *key,
611 const struct rhashtable_params params)
612{
613 void *obj;
614
615 rcu_read_lock();
616 obj = rhashtable_lookup(ht, key, params);
617 rcu_read_unlock();
618
619 return obj;
620}
621
622/**
623 * rhltable_lookup - search hash list table
624 * @hlt: hash table
625 * @key: the pointer to the key
626 * @params: hash table parameters
627 *
628 * Computes the hash value for the key and traverses the bucket chain looking
629 * for a entry with an identical key. All matching entries are returned
630 * in a list.
631 *
632 * This must only be called under the RCU read lock.
633 *
634 * Returns the list of entries that match the given key.
635 */
636static inline struct rhlist_head *rhltable_lookup(
637 struct rhltable *hlt, const void *key,
638 const struct rhashtable_params params)
639{
640 struct rhash_head *he = __rhashtable_lookup(&hlt->ht, key, params);
641
642 return he ? container_of(he, struct rhlist_head, rhead) : NULL;
643}
644
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200645/* Internal function, please use rhashtable_insert_fast() instead. This
646 * function returns the existing element already in hashes in there is a clash,
647 * otherwise it returns an error via ERR_PTR().
648 */
649static inline void *__rhashtable_insert_fast(
Herbert Xu02fd97c2015-03-20 21:57:00 +1100650 struct rhashtable *ht, const void *key, struct rhash_head *obj,
Herbert Xuca268932016-09-19 19:00:09 +0800651 const struct rhashtable_params params, bool rhlist)
Herbert Xu02fd97c2015-03-20 21:57:00 +1100652{
653 struct rhashtable_compare_arg arg = {
654 .ht = ht,
655 .key = key,
656 };
NeilBrown8f0db012019-04-02 10:07:45 +1100657 struct rhash_lock_head __rcu **bkt;
Herbert Xuca268932016-09-19 19:00:09 +0800658 struct rhash_head __rcu **pprev;
659 struct bucket_table *tbl;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100660 struct rhash_head *head;
Thomas Graf299e5c32015-03-24 14:18:17 +0100661 unsigned int hash;
Herbert Xuca268932016-09-19 19:00:09 +0800662 int elasticity;
663 void *data;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100664
665 rcu_read_lock();
666
667 tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xuca268932016-09-19 19:00:09 +0800668 hash = rht_head_hashfn(ht, tbl, obj, params);
NeilBrown8f0db012019-04-02 10:07:45 +1100669 elasticity = RHT_ELASTICITY;
670 bkt = rht_bucket_insert(ht, tbl, hash);
671 data = ERR_PTR(-ENOMEM);
672 if (!bkt)
673 goto out;
674 pprev = NULL;
675 rht_lock(bkt);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100676
NeilBrownc0690012018-06-18 12:52:50 +1000677 if (unlikely(rcu_access_pointer(tbl->future_tbl))) {
Herbert Xuca268932016-09-19 19:00:09 +0800678slow_path:
NeilBrown8f0db012019-04-02 10:07:45 +1100679 rht_unlock(bkt);
Herbert Xuca268932016-09-19 19:00:09 +0800680 rcu_read_unlock();
681 return rhashtable_insert_slow(ht, key, obj);
Herbert Xub8244782015-03-24 00:50:26 +1100682 }
683
NeilBrown8f0db012019-04-02 10:07:45 +1100684 rht_for_each_from(head, rht_ptr(*bkt), tbl, hash) {
Herbert Xuca268932016-09-19 19:00:09 +0800685 struct rhlist_head *plist;
686 struct rhlist_head *list;
Herbert Xu3cf92222015-12-03 20:41:29 +0800687
Herbert Xuca268932016-09-19 19:00:09 +0800688 elasticity--;
689 if (!key ||
690 (params.obj_cmpfn ?
691 params.obj_cmpfn(&arg, rht_obj(ht, head)) :
Paul Blakeyd3dcf8e2018-03-04 17:29:48 +0200692 rhashtable_compare(&arg, rht_obj(ht, head)))) {
693 pprev = &head->next;
Herbert Xuca268932016-09-19 19:00:09 +0800694 continue;
Paul Blakeyd3dcf8e2018-03-04 17:29:48 +0200695 }
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200696
Herbert Xuca268932016-09-19 19:00:09 +0800697 data = rht_obj(ht, head);
698
699 if (!rhlist)
NeilBrown8f0db012019-04-02 10:07:45 +1100700 goto out_unlock;
Herbert Xuca268932016-09-19 19:00:09 +0800701
702
703 list = container_of(obj, struct rhlist_head, rhead);
704 plist = container_of(head, struct rhlist_head, rhead);
705
706 RCU_INIT_POINTER(list->next, plist);
707 head = rht_dereference_bucket(head->next, tbl, hash);
708 RCU_INIT_POINTER(list->rhead.next, head);
NeilBrown8f0db012019-04-02 10:07:45 +1100709 if (pprev) {
710 rcu_assign_pointer(*pprev, obj);
711 rht_unlock(bkt);
712 } else
713 rht_assign_unlock(bkt, obj);
714 data = NULL;
715 goto out;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100716 }
717
Herbert Xuca268932016-09-19 19:00:09 +0800718 if (elasticity <= 0)
719 goto slow_path;
720
721 data = ERR_PTR(-E2BIG);
Herbert Xu07ee0722015-05-15 11:30:47 +0800722 if (unlikely(rht_grow_above_max(ht, tbl)))
NeilBrown8f0db012019-04-02 10:07:45 +1100723 goto out_unlock;
Herbert Xu07ee0722015-05-15 11:30:47 +0800724
Herbert Xuca268932016-09-19 19:00:09 +0800725 if (unlikely(rht_grow_above_100(ht, tbl)))
726 goto slow_path;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100727
NeilBrown8f0db012019-04-02 10:07:45 +1100728 /* Inserting at head of list makes unlocking free. */
729 head = rht_ptr(rht_dereference_bucket(*bkt, tbl, hash));
Herbert Xu02fd97c2015-03-20 21:57:00 +1100730
731 RCU_INIT_POINTER(obj->next, head);
Herbert Xuca268932016-09-19 19:00:09 +0800732 if (rhlist) {
733 struct rhlist_head *list;
734
735 list = container_of(obj, struct rhlist_head, rhead);
736 RCU_INIT_POINTER(list->next, NULL);
737 }
Herbert Xu02fd97c2015-03-20 21:57:00 +1100738
Herbert Xu02fd97c2015-03-20 21:57:00 +1100739 atomic_inc(&ht->nelems);
NeilBrown8f0db012019-04-02 10:07:45 +1100740 rht_assign_unlock(bkt, obj);
741
Herbert Xu02fd97c2015-03-20 21:57:00 +1100742 if (rht_grow_above_75(ht, tbl))
743 schedule_work(&ht->run_work);
744
Herbert Xuca268932016-09-19 19:00:09 +0800745 data = NULL;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100746out:
Herbert Xu02fd97c2015-03-20 21:57:00 +1100747 rcu_read_unlock();
748
Herbert Xuca268932016-09-19 19:00:09 +0800749 return data;
NeilBrown8f0db012019-04-02 10:07:45 +1100750
751out_unlock:
752 rht_unlock(bkt);
753 goto out;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100754}
755
756/**
757 * rhashtable_insert_fast - insert object into hash table
758 * @ht: hash table
759 * @obj: pointer to hash head inside object
760 * @params: hash table parameters
761 *
NeilBrown8f0db012019-04-02 10:07:45 +1100762 * Will take the per bucket bitlock to protect against mutual mutations
Herbert Xu02fd97c2015-03-20 21:57:00 +1100763 * on the same bucket. Multiple insertions may occur in parallel unless
NeilBrown8f0db012019-04-02 10:07:45 +1100764 * they map to the same bucket.
Herbert Xu02fd97c2015-03-20 21:57:00 +1100765 *
766 * It is safe to call this function from atomic context.
767 *
NeilBrown0c6f69a2018-04-24 08:29:13 +1000768 * Will trigger an automatic deferred table resizing if residency in the
769 * table grows beyond 70%.
Herbert Xu02fd97c2015-03-20 21:57:00 +1100770 */
771static inline int rhashtable_insert_fast(
772 struct rhashtable *ht, struct rhash_head *obj,
773 const struct rhashtable_params params)
774{
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200775 void *ret;
776
Herbert Xuca268932016-09-19 19:00:09 +0800777 ret = __rhashtable_insert_fast(ht, NULL, obj, params, false);
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200778 if (IS_ERR(ret))
779 return PTR_ERR(ret);
780
781 return ret == NULL ? 0 : -EEXIST;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100782}
783
784/**
Herbert Xuca268932016-09-19 19:00:09 +0800785 * rhltable_insert_key - insert object into hash list table
786 * @hlt: hash list table
787 * @key: the pointer to the key
788 * @list: pointer to hash list head inside object
789 * @params: hash table parameters
790 *
NeilBrown8f0db012019-04-02 10:07:45 +1100791 * Will take the per bucket bitlock to protect against mutual mutations
Herbert Xuca268932016-09-19 19:00:09 +0800792 * on the same bucket. Multiple insertions may occur in parallel unless
NeilBrown8f0db012019-04-02 10:07:45 +1100793 * they map to the same bucket.
Herbert Xuca268932016-09-19 19:00:09 +0800794 *
795 * It is safe to call this function from atomic context.
796 *
NeilBrown0c6f69a2018-04-24 08:29:13 +1000797 * Will trigger an automatic deferred table resizing if residency in the
798 * table grows beyond 70%.
Herbert Xuca268932016-09-19 19:00:09 +0800799 */
800static inline int rhltable_insert_key(
801 struct rhltable *hlt, const void *key, struct rhlist_head *list,
802 const struct rhashtable_params params)
803{
804 return PTR_ERR(__rhashtable_insert_fast(&hlt->ht, key, &list->rhead,
805 params, true));
806}
807
808/**
809 * rhltable_insert - insert object into hash list table
810 * @hlt: hash list table
811 * @list: pointer to hash list head inside object
812 * @params: hash table parameters
813 *
NeilBrown8f0db012019-04-02 10:07:45 +1100814 * Will take the per bucket bitlock to protect against mutual mutations
Herbert Xuca268932016-09-19 19:00:09 +0800815 * on the same bucket. Multiple insertions may occur in parallel unless
NeilBrown8f0db012019-04-02 10:07:45 +1100816 * they map to the same bucket.
Herbert Xuca268932016-09-19 19:00:09 +0800817 *
818 * It is safe to call this function from atomic context.
819 *
NeilBrown0c6f69a2018-04-24 08:29:13 +1000820 * Will trigger an automatic deferred table resizing if residency in the
821 * table grows beyond 70%.
Herbert Xuca268932016-09-19 19:00:09 +0800822 */
823static inline int rhltable_insert(
824 struct rhltable *hlt, struct rhlist_head *list,
825 const struct rhashtable_params params)
826{
827 const char *key = rht_obj(&hlt->ht, &list->rhead);
828
829 key += params.key_offset;
830
831 return rhltable_insert_key(hlt, key, list, params);
832}
833
834/**
Herbert Xu02fd97c2015-03-20 21:57:00 +1100835 * rhashtable_lookup_insert_fast - lookup and insert object into hash table
836 * @ht: hash table
837 * @obj: pointer to hash head inside object
838 * @params: hash table parameters
839 *
Herbert Xu02fd97c2015-03-20 21:57:00 +1100840 * This lookup function may only be used for fixed key hash table (key_len
841 * parameter set). It will BUG() if used inappropriately.
842 *
843 * It is safe to call this function from atomic context.
844 *
NeilBrown0c6f69a2018-04-24 08:29:13 +1000845 * Will trigger an automatic deferred table resizing if residency in the
846 * table grows beyond 70%.
Herbert Xu02fd97c2015-03-20 21:57:00 +1100847 */
848static inline int rhashtable_lookup_insert_fast(
849 struct rhashtable *ht, struct rhash_head *obj,
850 const struct rhashtable_params params)
851{
852 const char *key = rht_obj(ht, obj);
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200853 void *ret;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100854
855 BUG_ON(ht->p.obj_hashfn);
856
Herbert Xuca268932016-09-19 19:00:09 +0800857 ret = __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params,
858 false);
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200859 if (IS_ERR(ret))
860 return PTR_ERR(ret);
861
862 return ret == NULL ? 0 : -EEXIST;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100863}
864
865/**
Andreas Gruenbacherf9fe1c12017-03-18 00:36:15 +0100866 * rhashtable_lookup_get_insert_fast - lookup and insert object into hash table
867 * @ht: hash table
868 * @obj: pointer to hash head inside object
869 * @params: hash table parameters
870 *
871 * Just like rhashtable_lookup_insert_fast(), but this function returns the
872 * object if it exists, NULL if it did not and the insertion was successful,
873 * and an ERR_PTR otherwise.
874 */
875static inline void *rhashtable_lookup_get_insert_fast(
876 struct rhashtable *ht, struct rhash_head *obj,
877 const struct rhashtable_params params)
878{
879 const char *key = rht_obj(ht, obj);
880
881 BUG_ON(ht->p.obj_hashfn);
882
883 return __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params,
884 false);
885}
886
887/**
Herbert Xu02fd97c2015-03-20 21:57:00 +1100888 * rhashtable_lookup_insert_key - search and insert object to hash table
889 * with explicit key
890 * @ht: hash table
891 * @key: key
892 * @obj: pointer to hash head inside object
893 * @params: hash table parameters
894 *
Herbert Xu02fd97c2015-03-20 21:57:00 +1100895 * Lookups may occur in parallel with hashtable mutations and resizing.
896 *
NeilBrown0c6f69a2018-04-24 08:29:13 +1000897 * Will trigger an automatic deferred table resizing if residency in the
898 * table grows beyond 70%.
Herbert Xu02fd97c2015-03-20 21:57:00 +1100899 *
900 * Returns zero on success.
901 */
902static inline int rhashtable_lookup_insert_key(
903 struct rhashtable *ht, const void *key, struct rhash_head *obj,
904 const struct rhashtable_params params)
905{
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200906 void *ret;
907
908 BUG_ON(!ht->p.obj_hashfn || !key);
909
Herbert Xuca268932016-09-19 19:00:09 +0800910 ret = __rhashtable_insert_fast(ht, key, obj, params, false);
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200911 if (IS_ERR(ret))
912 return PTR_ERR(ret);
913
914 return ret == NULL ? 0 : -EEXIST;
915}
916
917/**
918 * rhashtable_lookup_get_insert_key - lookup and insert object into hash table
919 * @ht: hash table
920 * @obj: pointer to hash head inside object
921 * @params: hash table parameters
922 * @data: pointer to element data already in hashes
923 *
924 * Just like rhashtable_lookup_insert_key(), but this function returns the
925 * object if it exists, NULL if it does not and the insertion was successful,
926 * and an ERR_PTR otherwise.
927 */
928static inline void *rhashtable_lookup_get_insert_key(
929 struct rhashtable *ht, const void *key, struct rhash_head *obj,
930 const struct rhashtable_params params)
931{
Herbert Xu02fd97c2015-03-20 21:57:00 +1100932 BUG_ON(!ht->p.obj_hashfn || !key);
933
Herbert Xuca268932016-09-19 19:00:09 +0800934 return __rhashtable_insert_fast(ht, key, obj, params, false);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100935}
936
Thomas Grafac833bd2015-03-24 14:18:18 +0100937/* Internal function, please use rhashtable_remove_fast() instead */
Herbert Xuca268932016-09-19 19:00:09 +0800938static inline int __rhashtable_remove_fast_one(
Herbert Xu02fd97c2015-03-20 21:57:00 +1100939 struct rhashtable *ht, struct bucket_table *tbl,
Herbert Xuca268932016-09-19 19:00:09 +0800940 struct rhash_head *obj, const struct rhashtable_params params,
941 bool rhlist)
Herbert Xu02fd97c2015-03-20 21:57:00 +1100942{
NeilBrown8f0db012019-04-02 10:07:45 +1100943 struct rhash_lock_head __rcu **bkt;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100944 struct rhash_head __rcu **pprev;
945 struct rhash_head *he;
Thomas Graf299e5c32015-03-24 14:18:17 +0100946 unsigned int hash;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100947 int err = -ENOENT;
948
949 hash = rht_head_hashfn(ht, tbl, obj, params);
NeilBrown8f0db012019-04-02 10:07:45 +1100950 bkt = rht_bucket_var(tbl, hash);
951 if (!bkt)
952 return -ENOENT;
953 pprev = NULL;
954 rht_lock(bkt);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100955
NeilBrown8f0db012019-04-02 10:07:45 +1100956 rht_for_each_from(he, rht_ptr(*bkt), tbl, hash) {
Herbert Xuca268932016-09-19 19:00:09 +0800957 struct rhlist_head *list;
958
959 list = container_of(he, struct rhlist_head, rhead);
960
Herbert Xu02fd97c2015-03-20 21:57:00 +1100961 if (he != obj) {
Herbert Xuca268932016-09-19 19:00:09 +0800962 struct rhlist_head __rcu **lpprev;
963
Herbert Xu02fd97c2015-03-20 21:57:00 +1100964 pprev = &he->next;
Herbert Xuca268932016-09-19 19:00:09 +0800965
966 if (!rhlist)
967 continue;
968
969 do {
970 lpprev = &list->next;
971 list = rht_dereference_bucket(list->next,
972 tbl, hash);
973 } while (list && obj != &list->rhead);
974
975 if (!list)
976 continue;
977
978 list = rht_dereference_bucket(list->next, tbl, hash);
979 RCU_INIT_POINTER(*lpprev, list);
980 err = 0;
981 break;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100982 }
983
Herbert Xuca268932016-09-19 19:00:09 +0800984 obj = rht_dereference_bucket(obj->next, tbl, hash);
985 err = 1;
986
987 if (rhlist) {
988 list = rht_dereference_bucket(list->next, tbl, hash);
989 if (list) {
990 RCU_INIT_POINTER(list->rhead.next, obj);
991 obj = &list->rhead;
992 err = 0;
993 }
994 }
995
NeilBrown8f0db012019-04-02 10:07:45 +1100996 if (pprev) {
997 rcu_assign_pointer(*pprev, obj);
998 rht_unlock(bkt);
999 } else {
1000 rht_assign_unlock(bkt, obj);
1001 }
1002 goto unlocked;
Herbert Xu02fd97c2015-03-20 21:57:00 +11001003 }
1004
NeilBrown8f0db012019-04-02 10:07:45 +11001005 rht_unlock(bkt);
1006unlocked:
Herbert Xuca268932016-09-19 19:00:09 +08001007 if (err > 0) {
1008 atomic_dec(&ht->nelems);
1009 if (unlikely(ht->p.automatic_shrinking &&
1010 rht_shrink_below_30(ht, tbl)))
1011 schedule_work(&ht->run_work);
1012 err = 0;
1013 }
1014
1015 return err;
1016}
1017
1018/* Internal function, please use rhashtable_remove_fast() instead */
1019static inline int __rhashtable_remove_fast(
1020 struct rhashtable *ht, struct rhash_head *obj,
1021 const struct rhashtable_params params, bool rhlist)
1022{
1023 struct bucket_table *tbl;
1024 int err;
1025
1026 rcu_read_lock();
1027
1028 tbl = rht_dereference_rcu(ht->tbl, ht);
1029
1030 /* Because we have already taken (and released) the bucket
1031 * lock in old_tbl, if we find that future_tbl is not yet
1032 * visible then that guarantees the entry to still be in
1033 * the old tbl if it exists.
1034 */
1035 while ((err = __rhashtable_remove_fast_one(ht, tbl, obj, params,
1036 rhlist)) &&
1037 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
1038 ;
1039
1040 rcu_read_unlock();
1041
Herbert Xu02fd97c2015-03-20 21:57:00 +11001042 return err;
1043}
1044
1045/**
1046 * rhashtable_remove_fast - remove object from hash table
1047 * @ht: hash table
1048 * @obj: pointer to hash head inside object
1049 * @params: hash table parameters
1050 *
1051 * Since the hash chain is single linked, the removal operation needs to
1052 * walk the bucket chain upon removal. The removal operation is thus
1053 * considerable slow if the hash table is not correctly sized.
1054 *
NeilBrown0c6f69a2018-04-24 08:29:13 +10001055 * Will automatically shrink the table if permitted when residency drops
1056 * below 30%.
Herbert Xu02fd97c2015-03-20 21:57:00 +11001057 *
1058 * Returns zero on success, -ENOENT if the entry could not be found.
1059 */
1060static inline int rhashtable_remove_fast(
1061 struct rhashtable *ht, struct rhash_head *obj,
1062 const struct rhashtable_params params)
1063{
Herbert Xuca268932016-09-19 19:00:09 +08001064 return __rhashtable_remove_fast(ht, obj, params, false);
1065}
Herbert Xu02fd97c2015-03-20 21:57:00 +11001066
Herbert Xuca268932016-09-19 19:00:09 +08001067/**
1068 * rhltable_remove - remove object from hash list table
1069 * @hlt: hash list table
1070 * @list: pointer to hash list head inside object
1071 * @params: hash table parameters
1072 *
1073 * Since the hash chain is single linked, the removal operation needs to
1074 * walk the bucket chain upon removal. The removal operation is thus
1075 * considerable slow if the hash table is not correctly sized.
1076 *
NeilBrown0c6f69a2018-04-24 08:29:13 +10001077 * Will automatically shrink the table if permitted when residency drops
1078 * below 30%
Herbert Xuca268932016-09-19 19:00:09 +08001079 *
1080 * Returns zero on success, -ENOENT if the entry could not be found.
1081 */
1082static inline int rhltable_remove(
1083 struct rhltable *hlt, struct rhlist_head *list,
1084 const struct rhashtable_params params)
1085{
1086 return __rhashtable_remove_fast(&hlt->ht, &list->rhead, params, true);
Herbert Xu02fd97c2015-03-20 21:57:00 +11001087}
1088
Tom Herbert3502cad2015-12-15 15:41:36 -08001089/* Internal function, please use rhashtable_replace_fast() instead */
1090static inline int __rhashtable_replace_fast(
1091 struct rhashtable *ht, struct bucket_table *tbl,
1092 struct rhash_head *obj_old, struct rhash_head *obj_new,
1093 const struct rhashtable_params params)
1094{
NeilBrown8f0db012019-04-02 10:07:45 +11001095 struct rhash_lock_head __rcu **bkt;
Tom Herbert3502cad2015-12-15 15:41:36 -08001096 struct rhash_head __rcu **pprev;
1097 struct rhash_head *he;
Tom Herbert3502cad2015-12-15 15:41:36 -08001098 unsigned int hash;
1099 int err = -ENOENT;
1100
1101 /* Minimally, the old and new objects must have same hash
1102 * (which should mean identifiers are the same).
1103 */
1104 hash = rht_head_hashfn(ht, tbl, obj_old, params);
1105 if (hash != rht_head_hashfn(ht, tbl, obj_new, params))
1106 return -EINVAL;
1107
NeilBrown8f0db012019-04-02 10:07:45 +11001108 bkt = rht_bucket_var(tbl, hash);
1109 if (!bkt)
1110 return -ENOENT;
Tom Herbert3502cad2015-12-15 15:41:36 -08001111
NeilBrown8f0db012019-04-02 10:07:45 +11001112 pprev = NULL;
1113 rht_lock(bkt);
Tom Herbert3502cad2015-12-15 15:41:36 -08001114
NeilBrown8f0db012019-04-02 10:07:45 +11001115 rht_for_each_from(he, rht_ptr(*bkt), tbl, hash) {
Tom Herbert3502cad2015-12-15 15:41:36 -08001116 if (he != obj_old) {
1117 pprev = &he->next;
1118 continue;
1119 }
1120
1121 rcu_assign_pointer(obj_new->next, obj_old->next);
NeilBrown8f0db012019-04-02 10:07:45 +11001122 if (pprev) {
1123 rcu_assign_pointer(*pprev, obj_new);
1124 rht_unlock(bkt);
1125 } else {
1126 rht_assign_unlock(bkt, obj_new);
1127 }
Tom Herbert3502cad2015-12-15 15:41:36 -08001128 err = 0;
NeilBrown8f0db012019-04-02 10:07:45 +11001129 goto unlocked;
Tom Herbert3502cad2015-12-15 15:41:36 -08001130 }
Tom Herbert3502cad2015-12-15 15:41:36 -08001131
NeilBrown8f0db012019-04-02 10:07:45 +11001132 rht_unlock(bkt);
1133
1134unlocked:
Tom Herbert3502cad2015-12-15 15:41:36 -08001135 return err;
1136}
1137
1138/**
1139 * rhashtable_replace_fast - replace an object in hash table
1140 * @ht: hash table
1141 * @obj_old: pointer to hash head inside object being replaced
1142 * @obj_new: pointer to hash head inside object which is new
1143 * @params: hash table parameters
1144 *
1145 * Replacing an object doesn't affect the number of elements in the hash table
1146 * or bucket, so we don't need to worry about shrinking or expanding the
1147 * table here.
1148 *
1149 * Returns zero on success, -ENOENT if the entry could not be found,
1150 * -EINVAL if hash is not the same for the old and new objects.
1151 */
1152static inline int rhashtable_replace_fast(
1153 struct rhashtable *ht, struct rhash_head *obj_old,
1154 struct rhash_head *obj_new,
1155 const struct rhashtable_params params)
1156{
1157 struct bucket_table *tbl;
1158 int err;
1159
1160 rcu_read_lock();
1161
1162 tbl = rht_dereference_rcu(ht->tbl, ht);
1163
1164 /* Because we have already taken (and released) the bucket
1165 * lock in old_tbl, if we find that future_tbl is not yet
1166 * visible then that guarantees the entry to still be in
1167 * the old tbl if it exists.
1168 */
1169 while ((err = __rhashtable_replace_fast(ht, tbl, obj_old,
1170 obj_new, params)) &&
1171 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
1172 ;
1173
1174 rcu_read_unlock();
1175
1176 return err;
1177}
1178
Herbert Xuca268932016-09-19 19:00:09 +08001179/**
1180 * rhltable_walk_enter - Initialise an iterator
1181 * @hlt: Table to walk over
1182 * @iter: Hash table Iterator
1183 *
1184 * This function prepares a hash table walk.
1185 *
1186 * Note that if you restart a walk after rhashtable_walk_stop you
1187 * may see the same object twice. Also, you may miss objects if
1188 * there are removals in between rhashtable_walk_stop and the next
1189 * call to rhashtable_walk_start.
1190 *
1191 * For a completely stable walk you should construct your own data
1192 * structure outside the hash table.
1193 *
NeilBrown82266e92018-04-24 08:29:13 +10001194 * This function may be called from any process context, including
1195 * non-preemptable context, but cannot be called from softirq or
1196 * hardirq context.
Herbert Xuca268932016-09-19 19:00:09 +08001197 *
1198 * You must call rhashtable_walk_exit after this function returns.
1199 */
1200static inline void rhltable_walk_enter(struct rhltable *hlt,
1201 struct rhashtable_iter *iter)
1202{
1203 return rhashtable_walk_enter(&hlt->ht, iter);
1204}
1205
1206/**
1207 * rhltable_free_and_destroy - free elements and destroy hash list table
1208 * @hlt: the hash list table to destroy
1209 * @free_fn: callback to release resources of element
1210 * @arg: pointer passed to free_fn
1211 *
1212 * See documentation for rhashtable_free_and_destroy.
1213 */
1214static inline void rhltable_free_and_destroy(struct rhltable *hlt,
1215 void (*free_fn)(void *ptr,
1216 void *arg),
1217 void *arg)
1218{
1219 return rhashtable_free_and_destroy(&hlt->ht, free_fn, arg);
1220}
1221
1222static inline void rhltable_destroy(struct rhltable *hlt)
1223{
1224 return rhltable_free_and_destroy(hlt, NULL, NULL);
1225}
1226
Thomas Graf7e1e7762014-08-02 11:47:44 +02001227#endif /* _LINUX_RHASHTABLE_H */