blob: 70ebef866cc82ada0c6c1f69cc2a57e23cb732ba [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.
NeilBrownca0b7092019-04-12 11:52:08 +100038 * The value stored in the hash bucket has BIT(0) used as a lock bit.
NeilBrown8f0db012019-04-02 10:07:45 +110039 * 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
NeilBrowne4edbe32019-04-12 11:52:07 +100043 * that rcu_dereference() works on it, but it has no content so a
NeilBrown8f0db012019-04-02 10:07:45 +110044 * 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
NeilBrown149212f2019-04-02 10:07:45 +110085 struct lockdep_map dep_map;
86
Herbert Xuba6306e2019-05-16 15:19:46 +080087 struct rhash_lock_head *buckets[] ____cacheline_aligned_in_smp;
Thomas Graf7e1e7762014-08-02 11:47:44 +020088};
89
NeilBrown82208d0d2018-11-30 10:26:50 +110090/*
91 * NULLS_MARKER() expects a hash value with the low
92 * bits mostly likely to be significant, and it discards
93 * the msb.
NeilBrownca0b7092019-04-12 11:52:08 +100094 * We give it an address, in which the bottom bit is
NeilBrown82208d0d2018-11-30 10:26:50 +110095 * 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.
NeilBrownca0b7092019-04-12 11:52:08 +100098 *
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
NeilBrown82208d0d2018-11-30 10:26:50 +1100102 */
103#define RHT_NULLS_MARKER(ptr) \
104 ((void *)NULLS_MARKER(((unsigned long) (ptr)) >> 1))
NeilBrown9b4f64a2018-06-18 12:52:50 +1000105#define INIT_RHT_NULLS_HEAD(ptr) \
NeilBrownca0b7092019-04-12 11:52:08 +1000106 ((ptr) = NULL)
Thomas Graff89bd6f2015-01-02 23:00:21 +0100107
108static inline bool rht_is_a_nulls(const struct rhash_head *ptr)
109{
110 return ((unsigned long) ptr & 1);
111}
112
Herbert Xu02fd97c2015-03-20 21:57:00 +1100113static 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{
NeilBrown9f9a7072018-06-18 12:52:50 +1000122 return hash & (tbl->size - 1);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100123}
124
Tom Herbert2b860932017-12-04 10:31:43 -0800125static inline unsigned int rht_key_get_hash(struct rhashtable *ht,
126 const void *key, const struct rhashtable_params params,
127 unsigned int hash_rnd)
Herbert Xu02fd97c2015-03-20 21:57:00 +1100128{
Thomas Graf299e5c32015-03-24 14:18:17 +0100129 unsigned int hash;
Herbert Xude91b252015-03-24 00:50:20 +1100130
Herbert Xu31ccde22015-03-24 00:50:21 +1100131 /* params must be equal to ht->p if it isn't constant. */
132 if (!__builtin_constant_p(params.key_len))
Tom Herbert2b860932017-12-04 10:31:43 -0800133 hash = ht->p.hashfn(key, ht->key_len, hash_rnd);
Herbert Xu31ccde22015-03-24 00:50:21 +1100134 else if (params.key_len) {
Thomas Graf299e5c32015-03-24 14:18:17 +0100135 unsigned int key_len = params.key_len;
Herbert Xu31ccde22015-03-24 00:50:21 +1100136
137 if (params.hashfn)
Tom Herbert2b860932017-12-04 10:31:43 -0800138 hash = params.hashfn(key, key_len, hash_rnd);
Herbert Xu31ccde22015-03-24 00:50:21 +1100139 else if (key_len & (sizeof(u32) - 1))
Tom Herbert2b860932017-12-04 10:31:43 -0800140 hash = jhash(key, key_len, hash_rnd);
Herbert Xu31ccde22015-03-24 00:50:21 +1100141 else
Tom Herbert2b860932017-12-04 10:31:43 -0800142 hash = jhash2(key, key_len / sizeof(u32), hash_rnd);
Herbert Xu31ccde22015-03-24 00:50:21 +1100143 } else {
Thomas Graf299e5c32015-03-24 14:18:17 +0100144 unsigned int key_len = ht->p.key_len;
Herbert Xu31ccde22015-03-24 00:50:21 +1100145
146 if (params.hashfn)
Tom Herbert2b860932017-12-04 10:31:43 -0800147 hash = params.hashfn(key, key_len, hash_rnd);
Herbert Xu31ccde22015-03-24 00:50:21 +1100148 else
Tom Herbert2b860932017-12-04 10:31:43 -0800149 hash = jhash(key, key_len, hash_rnd);
Herbert Xu31ccde22015-03-24 00:50:21 +1100150 }
151
Tom Herbert2b860932017-12-04 10:31:43 -0800152 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
Herbert Xu31ccde22015-03-24 00:50:21 +1100161 return rht_bucket_index(tbl, hash);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100162}
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) ?
Patrick McHardy49f7b332015-03-25 13:07:45 +0000171 rht_bucket_index(tbl, params.obj_hashfn(ptr, params.key_len ?:
172 ht->p.key_len,
173 tbl->hash_rnd)) :
Herbert Xu02fd97c2015-03-20 21:57:00 +1100174 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
Herbert Xuccd57b12015-03-24 00:50:28 +1100203/**
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{
Johannes Berg1d8dc3d2015-04-23 16:38:43 +0200211 return atomic_read(&ht->nelems) > tbl->size &&
212 (!ht->p.max_size || tbl->size < ht->p.max_size);
Herbert Xuccd57b12015-03-24 00:50:28 +1100213}
214
Herbert Xu07ee0722015-05-15 11:30:47 +0800215/**
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{
Herbert Xu6d684e52017-04-27 13:44:51 +0800223 return atomic_read(&ht->nelems) >= ht->max_elems;
Herbert Xu07ee0722015-05-15 11:30:47 +0800224}
225
Thomas Graf7e1e7762014-08-02 11:47:44 +0200226#ifdef CONFIG_PROVE_LOCKING
Thomas Graf97defe12015-01-02 23:00:20 +0100227int lockdep_rht_mutex_is_held(struct rhashtable *ht);
Thomas Graf88d6ed12015-01-02 23:00:16 +0100228int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200229#else
Thomas Graf97defe12015-01-02 23:00:20 +0100230static inline int lockdep_rht_mutex_is_held(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200231{
232 return 1;
233}
Thomas Graf88d6ed12015-01-02 23:00:16 +0100234
235static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl,
236 u32 hash)
237{
238 return 1;
239}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200240#endif /* CONFIG_PROVE_LOCKING */
241
Herbert Xuca268932016-09-19 19:00:09 +0800242void *rhashtable_insert_slow(struct rhashtable *ht, const void *key,
243 struct rhash_head *obj);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200244
Herbert Xu246779d2016-08-18 16:50:56 +0800245void rhashtable_walk_enter(struct rhashtable *ht,
246 struct rhashtable_iter *iter);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100247void rhashtable_walk_exit(struct rhashtable_iter *iter);
Tom Herbert97a6ec42017-12-04 10:31:41 -0800248int 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
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100255void *rhashtable_walk_next(struct rhashtable_iter *iter);
Tom Herbert2db54b42017-12-04 10:31:42 -0800256void *rhashtable_walk_peek(struct rhashtable_iter *iter);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100257void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU);
258
Thomas Graf6b6f3022015-03-24 14:18:20 +0100259void rhashtable_free_and_destroy(struct rhashtable *ht,
260 void (*free_fn)(void *ptr, void *arg),
261 void *arg);
Thomas Graf97defe12015-01-02 23:00:20 +0100262void rhashtable_destroy(struct rhashtable *ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200263
Herbert Xuba6306e2019-05-16 15:19:46 +0800264struct 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);
Herbert Xuda204202017-02-11 19:26:47 +0800271
Thomas Graf7e1e7762014-08-02 11:47:44 +0200272#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
Thomas Graf88d6ed12015-01-02 23:00:16 +0100278#define rht_dereference_bucket(p, tbl, hash) \
279 rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200280
Thomas Graf88d6ed12015-01-02 23:00:16 +0100281#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
Herbert Xuba6306e2019-05-16 15:19:46 +0800287static inline struct rhash_lock_head *const *rht_bucket(
Herbert Xuda204202017-02-11 19:26:47 +0800288 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
Herbert Xuba6306e2019-05-16 15:19:46 +0800294static inline struct rhash_lock_head **rht_bucket_var(
Herbert Xuda204202017-02-11 19:26:47 +0800295 struct bucket_table *tbl, unsigned int hash)
296{
NeilBrownff302db2019-04-02 10:07:45 +1100297 return unlikely(tbl->nest) ? __rht_bucket_nested(tbl, hash) :
Herbert Xuda204202017-02-11 19:26:47 +0800298 &tbl->buckets[hash];
299}
300
Herbert Xuba6306e2019-05-16 15:19:46 +0800301static inline struct rhash_lock_head **rht_bucket_insert(
Herbert Xuda204202017-02-11 19:26:47 +0800302 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
NeilBrownc5783312019-04-12 11:52:08 +1000308/*
NeilBrownca0b7092019-04-12 11:52:08 +1000309 * 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.
NeilBrownc5783312019-04-12 11:52:08 +1000312 * 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.
NeilBrownf4712b42019-04-12 11:52:08 +1000324 * When we write to a bucket without unlocking, we use rht_assign_locked().
NeilBrownc5783312019-04-12 11:52:08 +1000325 */
326
327static inline void rht_lock(struct bucket_table *tbl,
328 struct rhash_lock_head **bkt)
329{
330 local_bh_disable();
NeilBrownca0b7092019-04-12 11:52:08 +1000331 bit_spin_lock(0, (unsigned long *)bkt);
NeilBrownc5783312019-04-12 11:52:08 +1000332 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();
NeilBrownca0b7092019-04-12 11:52:08 +1000340 bit_spin_lock(0, (unsigned long *)bucket);
NeilBrownc5783312019-04-12 11:52:08 +1000341 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);
NeilBrownca0b7092019-04-12 11:52:08 +1000348 bit_spin_unlock(0, (unsigned long *)bkt);
NeilBrownc5783312019-04-12 11:52:08 +1000349 local_bh_enable();
350}
351
Herbert Xuba6306e2019-05-16 15:19:46 +0800352static inline struct rhash_head __rcu *__rht_ptr(
353 struct rhash_lock_head *const *bkt)
354{
Herbert Xu279758f2019-05-28 15:02:31 +0800355 return (struct rhash_head __rcu *)
356 ((unsigned long)*bkt & ~BIT(0) ?:
357 (unsigned long)RHT_NULLS_MARKER(bkt));
Herbert Xuba6306e2019-05-16 15:19:46 +0800358}
359
NeilBrownc5783312019-04-12 11:52:08 +1000360/*
NeilBrownadc6a3a2019-04-12 11:52:08 +1000361 * Where 'bkt' is a bucket and might be locked:
Herbert Xu279758f2019-05-28 15:02:31 +0800362 * rht_ptr_rcu() dereferences that pointer and clears the lock bit.
363 * rht_ptr() dereferences in a context where the bucket is locked.
NeilBrownadc6a3a2019-04-12 11:52:08 +1000364 * rht_ptr_exclusive() dereferences in a context where exclusive
365 * access is guaranteed, such as when destroying the table.
NeilBrownc5783312019-04-12 11:52:08 +1000366 */
Herbert Xu279758f2019-05-28 15:02:31 +0800367static inline struct rhash_head *rht_ptr_rcu(
368 struct rhash_lock_head *const *bkt)
369{
370 struct rhash_head __rcu *p = __rht_ptr(bkt);
371
372 return rcu_dereference(p);
373}
374
NeilBrownadc6a3a2019-04-12 11:52:08 +1000375static inline struct rhash_head *rht_ptr(
Herbert Xuba6306e2019-05-16 15:19:46 +0800376 struct rhash_lock_head *const *bkt,
NeilBrownadc6a3a2019-04-12 11:52:08 +1000377 struct bucket_table *tbl,
378 unsigned int hash)
NeilBrownc5783312019-04-12 11:52:08 +1000379{
Herbert Xu279758f2019-05-28 15:02:31 +0800380 return rht_dereference_bucket(__rht_ptr(bkt), tbl, hash);
NeilBrownc5783312019-04-12 11:52:08 +1000381}
382
Herbert Xuba6306e2019-05-16 15:19:46 +0800383static inline struct rhash_head *rht_ptr_exclusive(
384 struct rhash_lock_head *const *bkt)
385{
Herbert Xu279758f2019-05-28 15:02:31 +0800386 return rcu_dereference_protected(__rht_ptr(bkt), 1);
Herbert Xuba6306e2019-05-16 15:19:46 +0800387}
388
389static inline void rht_assign_locked(struct rhash_lock_head **bkt,
NeilBrownf4712b42019-04-12 11:52:08 +1000390 struct rhash_head *obj)
NeilBrownc5783312019-04-12 11:52:08 +1000391{
NeilBrownf4712b42019-04-12 11:52:08 +1000392 struct rhash_head __rcu **p = (struct rhash_head __rcu **)bkt;
393
NeilBrownca0b7092019-04-12 11:52:08 +1000394 if (rht_is_a_nulls(obj))
395 obj = NULL;
396 rcu_assign_pointer(*p, (void *)((unsigned long)obj | BIT(0)));
NeilBrownc5783312019-04-12 11:52:08 +1000397}
398
399static inline void rht_assign_unlock(struct bucket_table *tbl,
Herbert Xuba6306e2019-05-16 15:19:46 +0800400 struct rhash_lock_head **bkt,
NeilBrownc5783312019-04-12 11:52:08 +1000401 struct rhash_head *obj)
402{
403 struct rhash_head __rcu **p = (struct rhash_head __rcu **)bkt;
404
NeilBrownca0b7092019-04-12 11:52:08 +1000405 if (rht_is_a_nulls(obj))
406 obj = NULL;
NeilBrownc5783312019-04-12 11:52:08 +1000407 lock_map_release(&tbl->dep_map);
408 rcu_assign_pointer(*p, obj);
409 preempt_enable();
410 __release(bitlock);
411 local_bh_enable();
412}
413
Thomas Graf88d6ed12015-01-02 23:00:16 +0100414/**
NeilBrownf7ad68b2019-03-21 14:42:40 +1100415 * rht_for_each_from - iterate over hash chain from given head
Thomas Graf88d6ed12015-01-02 23:00:16 +0100416 * @pos: the &struct rhash_head to use as a loop cursor.
NeilBrownf7ad68b2019-03-21 14:42:40 +1100417 * @head: the &struct rhash_head to start from
Thomas Graf88d6ed12015-01-02 23:00:16 +0100418 * @tbl: the &struct bucket_table
419 * @hash: the hash value / bucket index
420 */
NeilBrownf7ad68b2019-03-21 14:42:40 +1100421#define rht_for_each_from(pos, head, tbl, hash) \
NeilBrownadc6a3a2019-04-12 11:52:08 +1000422 for (pos = head; \
423 !rht_is_a_nulls(pos); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100424 pos = rht_dereference_bucket((pos)->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200425
426/**
427 * rht_for_each - iterate over hash chain
Thomas Graf88d6ed12015-01-02 23:00:16 +0100428 * @pos: the &struct rhash_head to use as a loop cursor.
429 * @tbl: the &struct bucket_table
430 * @hash: the hash value / bucket index
Thomas Graf7e1e7762014-08-02 11:47:44 +0200431 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100432#define rht_for_each(pos, tbl, hash) \
NeilBrownadc6a3a2019-04-12 11:52:08 +1000433 rht_for_each_from(pos, rht_ptr(rht_bucket(tbl, hash), tbl, hash), \
434 tbl, hash)
Thomas Graf88d6ed12015-01-02 23:00:16 +0100435
436/**
NeilBrownf7ad68b2019-03-21 14:42:40 +1100437 * rht_for_each_entry_from - iterate over hash chain from given head
Thomas Graf88d6ed12015-01-02 23:00:16 +0100438 * @tpos: the type * to use as a loop cursor.
439 * @pos: the &struct rhash_head to use as a loop cursor.
NeilBrownf7ad68b2019-03-21 14:42:40 +1100440 * @head: the &struct rhash_head to start from
Thomas Graf88d6ed12015-01-02 23:00:16 +0100441 * @tbl: the &struct bucket_table
442 * @hash: the hash value / bucket index
443 * @member: name of the &struct rhash_head within the hashable struct.
444 */
NeilBrownf7ad68b2019-03-21 14:42:40 +1100445#define rht_for_each_entry_from(tpos, pos, head, tbl, hash, member) \
NeilBrownadc6a3a2019-04-12 11:52:08 +1000446 for (pos = head; \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100447 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100448 pos = rht_dereference_bucket((pos)->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200449
450/**
451 * rht_for_each_entry - iterate over hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100452 * @tpos: the type * to use as a loop cursor.
453 * @pos: the &struct rhash_head to use as a loop cursor.
454 * @tbl: the &struct bucket_table
455 * @hash: the hash value / bucket index
456 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200457 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100458#define rht_for_each_entry(tpos, pos, tbl, hash, member) \
NeilBrownadc6a3a2019-04-12 11:52:08 +1000459 rht_for_each_entry_from(tpos, pos, \
460 rht_ptr(rht_bucket(tbl, hash), tbl, hash), \
461 tbl, hash, member)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200462
463/**
464 * rht_for_each_entry_safe - safely iterate over hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100465 * @tpos: the type * to use as a loop cursor.
466 * @pos: the &struct rhash_head to use as a loop cursor.
467 * @next: the &struct rhash_head to use as next in loop cursor.
468 * @tbl: the &struct bucket_table
469 * @hash: the hash value / bucket index
470 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200471 *
472 * This hash chain list-traversal primitive allows for the looped code to
473 * remove the loop cursor from the list.
474 */
Herbert Xuda204202017-02-11 19:26:47 +0800475#define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \
NeilBrownadc6a3a2019-04-12 11:52:08 +1000476 for (pos = rht_ptr(rht_bucket(tbl, hash), tbl, hash), \
Herbert Xuda204202017-02-11 19:26:47 +0800477 next = !rht_is_a_nulls(pos) ? \
478 rht_dereference_bucket(pos->next, tbl, hash) : NULL; \
479 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
480 pos = next, \
481 next = !rht_is_a_nulls(pos) ? \
Patrick McHardy607954b2015-01-21 11:12:13 +0000482 rht_dereference_bucket(pos->next, tbl, hash) : NULL)
Thomas Graf88d6ed12015-01-02 23:00:16 +0100483
484/**
NeilBrownf7ad68b2019-03-21 14:42:40 +1100485 * rht_for_each_rcu_from - iterate over rcu hash chain from given head
Thomas Graf88d6ed12015-01-02 23:00:16 +0100486 * @pos: the &struct rhash_head to use as a loop cursor.
NeilBrownf7ad68b2019-03-21 14:42:40 +1100487 * @head: the &struct rhash_head to start from
Thomas Graf88d6ed12015-01-02 23:00:16 +0100488 * @tbl: the &struct bucket_table
489 * @hash: the hash value / bucket index
490 *
491 * This hash chain list-traversal primitive may safely run concurrently with
492 * the _rcu mutation primitives such as rhashtable_insert() as long as the
493 * traversal is guarded by rcu_read_lock().
494 */
NeilBrownf7ad68b2019-03-21 14:42:40 +1100495#define rht_for_each_rcu_from(pos, head, tbl, hash) \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100496 for (({barrier(); }), \
NeilBrownadc6a3a2019-04-12 11:52:08 +1000497 pos = head; \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100498 !rht_is_a_nulls(pos); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100499 pos = rcu_dereference_raw(pos->next))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200500
501/**
502 * rht_for_each_rcu - iterate over rcu hash chain
Thomas Graf88d6ed12015-01-02 23:00:16 +0100503 * @pos: the &struct rhash_head to use as a loop cursor.
504 * @tbl: the &struct bucket_table
505 * @hash: the hash value / bucket index
Thomas Graf7e1e7762014-08-02 11:47:44 +0200506 *
507 * This hash chain list-traversal primitive may safely run concurrently with
Thomas Graf88d6ed12015-01-02 23:00:16 +0100508 * the _rcu mutation primitives such as rhashtable_insert() as long as the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200509 * traversal is guarded by rcu_read_lock().
510 */
NeilBrown8f0db012019-04-02 10:07:45 +1100511#define rht_for_each_rcu(pos, tbl, hash) \
NeilBrownadc6a3a2019-04-12 11:52:08 +1000512 for (({barrier(); }), \
Herbert Xu279758f2019-05-28 15:02:31 +0800513 pos = rht_ptr_rcu(rht_bucket(tbl, hash)); \
NeilBrownadc6a3a2019-04-12 11:52:08 +1000514 !rht_is_a_nulls(pos); \
NeilBrown8f0db012019-04-02 10:07:45 +1100515 pos = rcu_dereference_raw(pos->next))
Thomas Graf88d6ed12015-01-02 23:00:16 +0100516
517/**
NeilBrownf7ad68b2019-03-21 14:42:40 +1100518 * rht_for_each_entry_rcu_from - iterated over rcu hash chain from given head
Thomas Graf88d6ed12015-01-02 23:00:16 +0100519 * @tpos: the type * to use as a loop cursor.
520 * @pos: the &struct rhash_head to use as a loop cursor.
NeilBrownf7ad68b2019-03-21 14:42:40 +1100521 * @head: the &struct rhash_head to start from
Thomas Graf88d6ed12015-01-02 23:00:16 +0100522 * @tbl: the &struct bucket_table
523 * @hash: the hash value / bucket index
524 * @member: name of the &struct rhash_head within the hashable struct.
525 *
526 * This hash chain list-traversal primitive may safely run concurrently with
527 * the _rcu mutation primitives such as rhashtable_insert() as long as the
528 * traversal is guarded by rcu_read_lock().
529 */
NeilBrownf7ad68b2019-03-21 14:42:40 +1100530#define rht_for_each_entry_rcu_from(tpos, pos, head, tbl, hash, member) \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100531 for (({barrier(); }), \
NeilBrownadc6a3a2019-04-12 11:52:08 +1000532 pos = head; \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100533 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100534 pos = rht_dereference_bucket_rcu(pos->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200535
536/**
537 * rht_for_each_entry_rcu - iterate over rcu hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100538 * @tpos: the type * to use as a loop cursor.
539 * @pos: the &struct rhash_head to use as a loop cursor.
540 * @tbl: the &struct bucket_table
541 * @hash: the hash value / bucket index
542 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200543 *
544 * This hash chain list-traversal primitive may safely run concurrently with
Thomas Graf88d6ed12015-01-02 23:00:16 +0100545 * the _rcu mutation primitives such as rhashtable_insert() as long as the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200546 * traversal is guarded by rcu_read_lock().
547 */
Herbert Xuda204202017-02-11 19:26:47 +0800548#define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \
NeilBrown8f0db012019-04-02 10:07:45 +1100549 rht_for_each_entry_rcu_from(tpos, pos, \
Herbert Xu279758f2019-05-28 15:02:31 +0800550 rht_ptr_rcu(rht_bucket(tbl, hash)), \
NeilBrownadc6a3a2019-04-12 11:52:08 +1000551 tbl, hash, member)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200552
Herbert Xuca268932016-09-19 19:00:09 +0800553/**
554 * rhl_for_each_rcu - iterate over rcu hash table list
555 * @pos: the &struct rlist_head to use as a loop cursor.
556 * @list: the head of the list
557 *
558 * This hash chain list-traversal primitive should be used on the
559 * list returned by rhltable_lookup.
560 */
561#define rhl_for_each_rcu(pos, list) \
562 for (pos = list; pos; pos = rcu_dereference_raw(pos->next))
563
564/**
565 * rhl_for_each_entry_rcu - iterate over rcu hash table list of given type
566 * @tpos: the type * to use as a loop cursor.
567 * @pos: the &struct rlist_head to use as a loop cursor.
568 * @list: the head of the list
569 * @member: name of the &struct rlist_head within the hashable struct.
570 *
571 * This hash chain list-traversal primitive should be used on the
572 * list returned by rhltable_lookup.
573 */
574#define rhl_for_each_entry_rcu(tpos, pos, list, member) \
575 for (pos = list; pos && rht_entry(tpos, pos, member); \
576 pos = rcu_dereference_raw(pos->next))
577
Herbert Xu02fd97c2015-03-20 21:57:00 +1100578static inline int rhashtable_compare(struct rhashtable_compare_arg *arg,
579 const void *obj)
580{
581 struct rhashtable *ht = arg->ht;
582 const char *ptr = obj;
583
584 return memcmp(ptr + ht->p.key_offset, arg->key, ht->p.key_len);
585}
586
Herbert Xuca268932016-09-19 19:00:09 +0800587/* Internal function, do not use. */
588static inline struct rhash_head *__rhashtable_lookup(
Herbert Xu02fd97c2015-03-20 21:57:00 +1100589 struct rhashtable *ht, const void *key,
590 const struct rhashtable_params params)
591{
592 struct rhashtable_compare_arg arg = {
593 .ht = ht,
594 .key = key,
595 };
Herbert Xuba6306e2019-05-16 15:19:46 +0800596 struct rhash_lock_head *const *bkt;
Herbert Xuda204202017-02-11 19:26:47 +0800597 struct bucket_table *tbl;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100598 struct rhash_head *he;
Thomas Graf299e5c32015-03-24 14:18:17 +0100599 unsigned int hash;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100600
Herbert Xu02fd97c2015-03-20 21:57:00 +1100601 tbl = rht_dereference_rcu(ht->tbl, ht);
602restart:
603 hash = rht_key_hashfn(ht, tbl, key, params);
NeilBrown8f0db012019-04-02 10:07:45 +1100604 bkt = rht_bucket(tbl, hash);
NeilBrown82208d0d2018-11-30 10:26:50 +1100605 do {
Herbert Xu279758f2019-05-28 15:02:31 +0800606 rht_for_each_rcu_from(he, rht_ptr_rcu(bkt), tbl, hash) {
NeilBrown82208d0d2018-11-30 10:26:50 +1100607 if (params.obj_cmpfn ?
608 params.obj_cmpfn(&arg, rht_obj(ht, he)) :
609 rhashtable_compare(&arg, rht_obj(ht, he)))
610 continue;
611 return he;
612 }
613 /* An object might have been moved to a different hash chain,
614 * while we walk along it - better check and retry.
615 */
NeilBrown8f0db012019-04-02 10:07:45 +1100616 } while (he != RHT_NULLS_MARKER(bkt));
Herbert Xu02fd97c2015-03-20 21:57:00 +1100617
618 /* Ensure we see any new tables. */
619 smp_rmb();
620
621 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
622 if (unlikely(tbl))
623 goto restart;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100624
625 return NULL;
626}
627
Herbert Xuca268932016-09-19 19:00:09 +0800628/**
629 * rhashtable_lookup - search hash table
630 * @ht: hash table
631 * @key: the pointer to the key
632 * @params: hash table parameters
633 *
634 * Computes the hash value for the key and traverses the bucket chain looking
635 * for a entry with an identical key. The first matching entry is returned.
636 *
637 * This must only be called under the RCU read lock.
638 *
639 * Returns the first entry on which the compare function returned true.
640 */
641static inline void *rhashtable_lookup(
642 struct rhashtable *ht, const void *key,
643 const struct rhashtable_params params)
644{
645 struct rhash_head *he = __rhashtable_lookup(ht, key, params);
646
647 return he ? rht_obj(ht, he) : NULL;
648}
649
650/**
651 * rhashtable_lookup_fast - search hash table, without RCU read lock
652 * @ht: hash table
653 * @key: the pointer to the key
654 * @params: hash table parameters
655 *
656 * Computes the hash value for the key and traverses the bucket chain looking
657 * for a entry with an identical key. The first matching entry is returned.
658 *
659 * Only use this function when you have other mechanisms guaranteeing
660 * that the object won't go away after the RCU read lock is released.
661 *
662 * Returns the first entry on which the compare function returned true.
663 */
664static inline void *rhashtable_lookup_fast(
665 struct rhashtable *ht, const void *key,
666 const struct rhashtable_params params)
667{
668 void *obj;
669
670 rcu_read_lock();
671 obj = rhashtable_lookup(ht, key, params);
672 rcu_read_unlock();
673
674 return obj;
675}
676
677/**
678 * rhltable_lookup - search hash list table
679 * @hlt: hash table
680 * @key: the pointer to the key
681 * @params: hash table parameters
682 *
683 * Computes the hash value for the key and traverses the bucket chain looking
684 * for a entry with an identical key. All matching entries are returned
685 * in a list.
686 *
687 * This must only be called under the RCU read lock.
688 *
689 * Returns the list of entries that match the given key.
690 */
691static inline struct rhlist_head *rhltable_lookup(
692 struct rhltable *hlt, const void *key,
693 const struct rhashtable_params params)
694{
695 struct rhash_head *he = __rhashtable_lookup(&hlt->ht, key, params);
696
697 return he ? container_of(he, struct rhlist_head, rhead) : NULL;
698}
699
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200700/* Internal function, please use rhashtable_insert_fast() instead. This
701 * function returns the existing element already in hashes in there is a clash,
702 * otherwise it returns an error via ERR_PTR().
703 */
704static inline void *__rhashtable_insert_fast(
Herbert Xu02fd97c2015-03-20 21:57:00 +1100705 struct rhashtable *ht, const void *key, struct rhash_head *obj,
Herbert Xuca268932016-09-19 19:00:09 +0800706 const struct rhashtable_params params, bool rhlist)
Herbert Xu02fd97c2015-03-20 21:57:00 +1100707{
708 struct rhashtable_compare_arg arg = {
709 .ht = ht,
710 .key = key,
711 };
Herbert Xuba6306e2019-05-16 15:19:46 +0800712 struct rhash_lock_head **bkt;
Herbert Xuca268932016-09-19 19:00:09 +0800713 struct rhash_head __rcu **pprev;
714 struct bucket_table *tbl;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100715 struct rhash_head *head;
Thomas Graf299e5c32015-03-24 14:18:17 +0100716 unsigned int hash;
Herbert Xuca268932016-09-19 19:00:09 +0800717 int elasticity;
718 void *data;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100719
720 rcu_read_lock();
721
722 tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xuca268932016-09-19 19:00:09 +0800723 hash = rht_head_hashfn(ht, tbl, obj, params);
NeilBrown8f0db012019-04-02 10:07:45 +1100724 elasticity = RHT_ELASTICITY;
725 bkt = rht_bucket_insert(ht, tbl, hash);
726 data = ERR_PTR(-ENOMEM);
727 if (!bkt)
728 goto out;
729 pprev = NULL;
NeilBrown149212f2019-04-02 10:07:45 +1100730 rht_lock(tbl, bkt);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100731
NeilBrownc0690012018-06-18 12:52:50 +1000732 if (unlikely(rcu_access_pointer(tbl->future_tbl))) {
Herbert Xuca268932016-09-19 19:00:09 +0800733slow_path:
NeilBrown149212f2019-04-02 10:07:45 +1100734 rht_unlock(tbl, bkt);
Herbert Xuca268932016-09-19 19:00:09 +0800735 rcu_read_unlock();
736 return rhashtable_insert_slow(ht, key, obj);
Herbert Xub8244782015-03-24 00:50:26 +1100737 }
738
NeilBrownadc6a3a2019-04-12 11:52:08 +1000739 rht_for_each_from(head, rht_ptr(bkt, tbl, hash), tbl, hash) {
Herbert Xuca268932016-09-19 19:00:09 +0800740 struct rhlist_head *plist;
741 struct rhlist_head *list;
Herbert Xu3cf92222015-12-03 20:41:29 +0800742
Herbert Xuca268932016-09-19 19:00:09 +0800743 elasticity--;
744 if (!key ||
745 (params.obj_cmpfn ?
746 params.obj_cmpfn(&arg, rht_obj(ht, head)) :
Paul Blakeyd3dcf8e2018-03-04 17:29:48 +0200747 rhashtable_compare(&arg, rht_obj(ht, head)))) {
748 pprev = &head->next;
Herbert Xuca268932016-09-19 19:00:09 +0800749 continue;
Paul Blakeyd3dcf8e2018-03-04 17:29:48 +0200750 }
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200751
Herbert Xuca268932016-09-19 19:00:09 +0800752 data = rht_obj(ht, head);
753
754 if (!rhlist)
NeilBrown8f0db012019-04-02 10:07:45 +1100755 goto out_unlock;
Herbert Xuca268932016-09-19 19:00:09 +0800756
757
758 list = container_of(obj, struct rhlist_head, rhead);
759 plist = container_of(head, struct rhlist_head, rhead);
760
761 RCU_INIT_POINTER(list->next, plist);
762 head = rht_dereference_bucket(head->next, tbl, hash);
763 RCU_INIT_POINTER(list->rhead.next, head);
NeilBrown8f0db012019-04-02 10:07:45 +1100764 if (pprev) {
765 rcu_assign_pointer(*pprev, obj);
NeilBrown149212f2019-04-02 10:07:45 +1100766 rht_unlock(tbl, bkt);
NeilBrown8f0db012019-04-02 10:07:45 +1100767 } else
NeilBrown149212f2019-04-02 10:07:45 +1100768 rht_assign_unlock(tbl, bkt, obj);
NeilBrown8f0db012019-04-02 10:07:45 +1100769 data = NULL;
770 goto out;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100771 }
772
Herbert Xuca268932016-09-19 19:00:09 +0800773 if (elasticity <= 0)
774 goto slow_path;
775
776 data = ERR_PTR(-E2BIG);
Herbert Xu07ee0722015-05-15 11:30:47 +0800777 if (unlikely(rht_grow_above_max(ht, tbl)))
NeilBrown8f0db012019-04-02 10:07:45 +1100778 goto out_unlock;
Herbert Xu07ee0722015-05-15 11:30:47 +0800779
Herbert Xuca268932016-09-19 19:00:09 +0800780 if (unlikely(rht_grow_above_100(ht, tbl)))
781 goto slow_path;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100782
NeilBrown8f0db012019-04-02 10:07:45 +1100783 /* Inserting at head of list makes unlocking free. */
NeilBrownadc6a3a2019-04-12 11:52:08 +1000784 head = rht_ptr(bkt, tbl, hash);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100785
786 RCU_INIT_POINTER(obj->next, head);
Herbert Xuca268932016-09-19 19:00:09 +0800787 if (rhlist) {
788 struct rhlist_head *list;
789
790 list = container_of(obj, struct rhlist_head, rhead);
791 RCU_INIT_POINTER(list->next, NULL);
792 }
Herbert Xu02fd97c2015-03-20 21:57:00 +1100793
Herbert Xu02fd97c2015-03-20 21:57:00 +1100794 atomic_inc(&ht->nelems);
NeilBrown149212f2019-04-02 10:07:45 +1100795 rht_assign_unlock(tbl, bkt, obj);
NeilBrown8f0db012019-04-02 10:07:45 +1100796
Herbert Xu02fd97c2015-03-20 21:57:00 +1100797 if (rht_grow_above_75(ht, tbl))
798 schedule_work(&ht->run_work);
799
Herbert Xuca268932016-09-19 19:00:09 +0800800 data = NULL;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100801out:
Herbert Xu02fd97c2015-03-20 21:57:00 +1100802 rcu_read_unlock();
803
Herbert Xuca268932016-09-19 19:00:09 +0800804 return data;
NeilBrown8f0db012019-04-02 10:07:45 +1100805
806out_unlock:
NeilBrown149212f2019-04-02 10:07:45 +1100807 rht_unlock(tbl, bkt);
NeilBrown8f0db012019-04-02 10:07:45 +1100808 goto out;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100809}
810
811/**
812 * rhashtable_insert_fast - insert object into hash table
813 * @ht: hash table
814 * @obj: pointer to hash head inside object
815 * @params: hash table parameters
816 *
NeilBrown8f0db012019-04-02 10:07:45 +1100817 * Will take the per bucket bitlock to protect against mutual mutations
Herbert Xu02fd97c2015-03-20 21:57:00 +1100818 * on the same bucket. Multiple insertions may occur in parallel unless
NeilBrown8f0db012019-04-02 10:07:45 +1100819 * they map to the same bucket.
Herbert Xu02fd97c2015-03-20 21:57:00 +1100820 *
821 * It is safe to call this function from atomic context.
822 *
NeilBrown0c6f69a2018-04-24 08:29:13 +1000823 * Will trigger an automatic deferred table resizing if residency in the
824 * table grows beyond 70%.
Herbert Xu02fd97c2015-03-20 21:57:00 +1100825 */
826static inline int rhashtable_insert_fast(
827 struct rhashtable *ht, struct rhash_head *obj,
828 const struct rhashtable_params params)
829{
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200830 void *ret;
831
Herbert Xuca268932016-09-19 19:00:09 +0800832 ret = __rhashtable_insert_fast(ht, NULL, obj, params, false);
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200833 if (IS_ERR(ret))
834 return PTR_ERR(ret);
835
836 return ret == NULL ? 0 : -EEXIST;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100837}
838
839/**
Herbert Xuca268932016-09-19 19:00:09 +0800840 * rhltable_insert_key - insert object into hash list table
841 * @hlt: hash list table
842 * @key: the pointer to the key
843 * @list: pointer to hash list head inside object
844 * @params: hash table parameters
845 *
NeilBrown8f0db012019-04-02 10:07:45 +1100846 * Will take the per bucket bitlock to protect against mutual mutations
Herbert Xuca268932016-09-19 19:00:09 +0800847 * on the same bucket. Multiple insertions may occur in parallel unless
NeilBrown8f0db012019-04-02 10:07:45 +1100848 * they map to the same bucket.
Herbert Xuca268932016-09-19 19:00:09 +0800849 *
850 * It is safe to call this function from atomic context.
851 *
NeilBrown0c6f69a2018-04-24 08:29:13 +1000852 * Will trigger an automatic deferred table resizing if residency in the
853 * table grows beyond 70%.
Herbert Xuca268932016-09-19 19:00:09 +0800854 */
855static inline int rhltable_insert_key(
856 struct rhltable *hlt, const void *key, struct rhlist_head *list,
857 const struct rhashtable_params params)
858{
859 return PTR_ERR(__rhashtable_insert_fast(&hlt->ht, key, &list->rhead,
860 params, true));
861}
862
863/**
864 * rhltable_insert - insert object into hash list table
865 * @hlt: hash list table
866 * @list: pointer to hash list head inside object
867 * @params: hash table parameters
868 *
NeilBrown8f0db012019-04-02 10:07:45 +1100869 * Will take the per bucket bitlock to protect against mutual mutations
Herbert Xuca268932016-09-19 19:00:09 +0800870 * on the same bucket. Multiple insertions may occur in parallel unless
NeilBrown8f0db012019-04-02 10:07:45 +1100871 * they map to the same bucket.
Herbert Xuca268932016-09-19 19:00:09 +0800872 *
873 * It is safe to call this function from atomic context.
874 *
NeilBrown0c6f69a2018-04-24 08:29:13 +1000875 * Will trigger an automatic deferred table resizing if residency in the
876 * table grows beyond 70%.
Herbert Xuca268932016-09-19 19:00:09 +0800877 */
878static inline int rhltable_insert(
879 struct rhltable *hlt, struct rhlist_head *list,
880 const struct rhashtable_params params)
881{
882 const char *key = rht_obj(&hlt->ht, &list->rhead);
883
884 key += params.key_offset;
885
886 return rhltable_insert_key(hlt, key, list, params);
887}
888
889/**
Herbert Xu02fd97c2015-03-20 21:57:00 +1100890 * rhashtable_lookup_insert_fast - lookup and insert object into hash table
891 * @ht: hash table
892 * @obj: pointer to hash head inside object
893 * @params: hash table parameters
894 *
Herbert Xu02fd97c2015-03-20 21:57:00 +1100895 * This lookup function may only be used for fixed key hash table (key_len
896 * parameter set). It will BUG() if used inappropriately.
897 *
898 * It is safe to call this function from atomic context.
899 *
NeilBrown0c6f69a2018-04-24 08:29:13 +1000900 * Will trigger an automatic deferred table resizing if residency in the
901 * table grows beyond 70%.
Herbert Xu02fd97c2015-03-20 21:57:00 +1100902 */
903static inline int rhashtable_lookup_insert_fast(
904 struct rhashtable *ht, struct rhash_head *obj,
905 const struct rhashtable_params params)
906{
907 const char *key = rht_obj(ht, obj);
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200908 void *ret;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100909
910 BUG_ON(ht->p.obj_hashfn);
911
Herbert Xuca268932016-09-19 19:00:09 +0800912 ret = __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params,
913 false);
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200914 if (IS_ERR(ret))
915 return PTR_ERR(ret);
916
917 return ret == NULL ? 0 : -EEXIST;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100918}
919
920/**
Andreas Gruenbacherf9fe1c12017-03-18 00:36:15 +0100921 * rhashtable_lookup_get_insert_fast - lookup and insert object into hash table
922 * @ht: hash table
923 * @obj: pointer to hash head inside object
924 * @params: hash table parameters
925 *
926 * Just like rhashtable_lookup_insert_fast(), but this function returns the
927 * object if it exists, NULL if it did not and the insertion was successful,
928 * and an ERR_PTR otherwise.
929 */
930static inline void *rhashtable_lookup_get_insert_fast(
931 struct rhashtable *ht, struct rhash_head *obj,
932 const struct rhashtable_params params)
933{
934 const char *key = rht_obj(ht, obj);
935
936 BUG_ON(ht->p.obj_hashfn);
937
938 return __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params,
939 false);
940}
941
942/**
Herbert Xu02fd97c2015-03-20 21:57:00 +1100943 * rhashtable_lookup_insert_key - search and insert object to hash table
944 * with explicit key
945 * @ht: hash table
946 * @key: key
947 * @obj: pointer to hash head inside object
948 * @params: hash table parameters
949 *
Herbert Xu02fd97c2015-03-20 21:57:00 +1100950 * Lookups may occur in parallel with hashtable mutations and resizing.
951 *
NeilBrown0c6f69a2018-04-24 08:29:13 +1000952 * Will trigger an automatic deferred table resizing if residency in the
953 * table grows beyond 70%.
Herbert Xu02fd97c2015-03-20 21:57:00 +1100954 *
955 * Returns zero on success.
956 */
957static inline int rhashtable_lookup_insert_key(
958 struct rhashtable *ht, const void *key, struct rhash_head *obj,
959 const struct rhashtable_params params)
960{
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200961 void *ret;
962
963 BUG_ON(!ht->p.obj_hashfn || !key);
964
Herbert Xuca268932016-09-19 19:00:09 +0800965 ret = __rhashtable_insert_fast(ht, key, obj, params, false);
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200966 if (IS_ERR(ret))
967 return PTR_ERR(ret);
968
969 return ret == NULL ? 0 : -EEXIST;
970}
971
972/**
973 * rhashtable_lookup_get_insert_key - lookup and insert object into hash table
974 * @ht: hash table
Jonathan Neuschäferaeaa9252020-03-05 17:05:16 +0100975 * @key: key
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200976 * @obj: pointer to hash head inside object
977 * @params: hash table parameters
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200978 *
979 * Just like rhashtable_lookup_insert_key(), but this function returns the
980 * object if it exists, NULL if it does not and the insertion was successful,
981 * and an ERR_PTR otherwise.
982 */
983static inline void *rhashtable_lookup_get_insert_key(
984 struct rhashtable *ht, const void *key, struct rhash_head *obj,
985 const struct rhashtable_params params)
986{
Herbert Xu02fd97c2015-03-20 21:57:00 +1100987 BUG_ON(!ht->p.obj_hashfn || !key);
988
Herbert Xuca268932016-09-19 19:00:09 +0800989 return __rhashtable_insert_fast(ht, key, obj, params, false);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100990}
991
Thomas Grafac833bd2015-03-24 14:18:18 +0100992/* Internal function, please use rhashtable_remove_fast() instead */
Herbert Xuca268932016-09-19 19:00:09 +0800993static inline int __rhashtable_remove_fast_one(
Herbert Xu02fd97c2015-03-20 21:57:00 +1100994 struct rhashtable *ht, struct bucket_table *tbl,
Herbert Xuca268932016-09-19 19:00:09 +0800995 struct rhash_head *obj, const struct rhashtable_params params,
996 bool rhlist)
Herbert Xu02fd97c2015-03-20 21:57:00 +1100997{
Herbert Xuba6306e2019-05-16 15:19:46 +0800998 struct rhash_lock_head **bkt;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100999 struct rhash_head __rcu **pprev;
1000 struct rhash_head *he;
Thomas Graf299e5c32015-03-24 14:18:17 +01001001 unsigned int hash;
Herbert Xu02fd97c2015-03-20 21:57:00 +11001002 int err = -ENOENT;
1003
1004 hash = rht_head_hashfn(ht, tbl, obj, params);
NeilBrown8f0db012019-04-02 10:07:45 +11001005 bkt = rht_bucket_var(tbl, hash);
1006 if (!bkt)
1007 return -ENOENT;
1008 pprev = NULL;
NeilBrown149212f2019-04-02 10:07:45 +11001009 rht_lock(tbl, bkt);
Herbert Xu02fd97c2015-03-20 21:57:00 +11001010
NeilBrownadc6a3a2019-04-12 11:52:08 +10001011 rht_for_each_from(he, rht_ptr(bkt, tbl, hash), tbl, hash) {
Herbert Xuca268932016-09-19 19:00:09 +08001012 struct rhlist_head *list;
1013
1014 list = container_of(he, struct rhlist_head, rhead);
1015
Herbert Xu02fd97c2015-03-20 21:57:00 +11001016 if (he != obj) {
Herbert Xuca268932016-09-19 19:00:09 +08001017 struct rhlist_head __rcu **lpprev;
1018
Herbert Xu02fd97c2015-03-20 21:57:00 +11001019 pprev = &he->next;
Herbert Xuca268932016-09-19 19:00:09 +08001020
1021 if (!rhlist)
1022 continue;
1023
1024 do {
1025 lpprev = &list->next;
1026 list = rht_dereference_bucket(list->next,
1027 tbl, hash);
1028 } while (list && obj != &list->rhead);
1029
1030 if (!list)
1031 continue;
1032
1033 list = rht_dereference_bucket(list->next, tbl, hash);
1034 RCU_INIT_POINTER(*lpprev, list);
1035 err = 0;
1036 break;
Herbert Xu02fd97c2015-03-20 21:57:00 +11001037 }
1038
Herbert Xuca268932016-09-19 19:00:09 +08001039 obj = rht_dereference_bucket(obj->next, tbl, hash);
1040 err = 1;
1041
1042 if (rhlist) {
1043 list = rht_dereference_bucket(list->next, tbl, hash);
1044 if (list) {
1045 RCU_INIT_POINTER(list->rhead.next, obj);
1046 obj = &list->rhead;
1047 err = 0;
1048 }
1049 }
1050
NeilBrown8f0db012019-04-02 10:07:45 +11001051 if (pprev) {
1052 rcu_assign_pointer(*pprev, obj);
NeilBrown149212f2019-04-02 10:07:45 +11001053 rht_unlock(tbl, bkt);
NeilBrown8f0db012019-04-02 10:07:45 +11001054 } else {
NeilBrown149212f2019-04-02 10:07:45 +11001055 rht_assign_unlock(tbl, bkt, obj);
NeilBrown8f0db012019-04-02 10:07:45 +11001056 }
1057 goto unlocked;
Herbert Xu02fd97c2015-03-20 21:57:00 +11001058 }
1059
NeilBrown149212f2019-04-02 10:07:45 +11001060 rht_unlock(tbl, bkt);
NeilBrown8f0db012019-04-02 10:07:45 +11001061unlocked:
Herbert Xuca268932016-09-19 19:00:09 +08001062 if (err > 0) {
1063 atomic_dec(&ht->nelems);
1064 if (unlikely(ht->p.automatic_shrinking &&
1065 rht_shrink_below_30(ht, tbl)))
1066 schedule_work(&ht->run_work);
1067 err = 0;
1068 }
1069
1070 return err;
1071}
1072
1073/* Internal function, please use rhashtable_remove_fast() instead */
1074static inline int __rhashtable_remove_fast(
1075 struct rhashtable *ht, struct rhash_head *obj,
1076 const struct rhashtable_params params, bool rhlist)
1077{
1078 struct bucket_table *tbl;
1079 int err;
1080
1081 rcu_read_lock();
1082
1083 tbl = rht_dereference_rcu(ht->tbl, ht);
1084
1085 /* Because we have already taken (and released) the bucket
1086 * lock in old_tbl, if we find that future_tbl is not yet
1087 * visible then that guarantees the entry to still be in
1088 * the old tbl if it exists.
1089 */
1090 while ((err = __rhashtable_remove_fast_one(ht, tbl, obj, params,
1091 rhlist)) &&
1092 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
1093 ;
1094
1095 rcu_read_unlock();
1096
Herbert Xu02fd97c2015-03-20 21:57:00 +11001097 return err;
1098}
1099
1100/**
1101 * rhashtable_remove_fast - remove object from hash table
1102 * @ht: hash table
1103 * @obj: pointer to hash head inside object
1104 * @params: hash table parameters
1105 *
1106 * Since the hash chain is single linked, the removal operation needs to
1107 * walk the bucket chain upon removal. The removal operation is thus
1108 * considerable slow if the hash table is not correctly sized.
1109 *
NeilBrown0c6f69a2018-04-24 08:29:13 +10001110 * Will automatically shrink the table if permitted when residency drops
1111 * below 30%.
Herbert Xu02fd97c2015-03-20 21:57:00 +11001112 *
1113 * Returns zero on success, -ENOENT if the entry could not be found.
1114 */
1115static inline int rhashtable_remove_fast(
1116 struct rhashtable *ht, struct rhash_head *obj,
1117 const struct rhashtable_params params)
1118{
Herbert Xuca268932016-09-19 19:00:09 +08001119 return __rhashtable_remove_fast(ht, obj, params, false);
1120}
Herbert Xu02fd97c2015-03-20 21:57:00 +11001121
Herbert Xuca268932016-09-19 19:00:09 +08001122/**
1123 * rhltable_remove - remove object from hash list table
1124 * @hlt: hash list table
1125 * @list: pointer to hash list head inside object
1126 * @params: hash table parameters
1127 *
1128 * Since the hash chain is single linked, the removal operation needs to
1129 * walk the bucket chain upon removal. The removal operation is thus
1130 * considerable slow if the hash table is not correctly sized.
1131 *
NeilBrown0c6f69a2018-04-24 08:29:13 +10001132 * Will automatically shrink the table if permitted when residency drops
1133 * below 30%
Herbert Xuca268932016-09-19 19:00:09 +08001134 *
1135 * Returns zero on success, -ENOENT if the entry could not be found.
1136 */
1137static inline int rhltable_remove(
1138 struct rhltable *hlt, struct rhlist_head *list,
1139 const struct rhashtable_params params)
1140{
1141 return __rhashtable_remove_fast(&hlt->ht, &list->rhead, params, true);
Herbert Xu02fd97c2015-03-20 21:57:00 +11001142}
1143
Tom Herbert3502cad2015-12-15 15:41:36 -08001144/* Internal function, please use rhashtable_replace_fast() instead */
1145static inline int __rhashtable_replace_fast(
1146 struct rhashtable *ht, struct bucket_table *tbl,
1147 struct rhash_head *obj_old, struct rhash_head *obj_new,
1148 const struct rhashtable_params params)
1149{
Herbert Xuba6306e2019-05-16 15:19:46 +08001150 struct rhash_lock_head **bkt;
Tom Herbert3502cad2015-12-15 15:41:36 -08001151 struct rhash_head __rcu **pprev;
1152 struct rhash_head *he;
Tom Herbert3502cad2015-12-15 15:41:36 -08001153 unsigned int hash;
1154 int err = -ENOENT;
1155
1156 /* Minimally, the old and new objects must have same hash
1157 * (which should mean identifiers are the same).
1158 */
1159 hash = rht_head_hashfn(ht, tbl, obj_old, params);
1160 if (hash != rht_head_hashfn(ht, tbl, obj_new, params))
1161 return -EINVAL;
1162
NeilBrown8f0db012019-04-02 10:07:45 +11001163 bkt = rht_bucket_var(tbl, hash);
1164 if (!bkt)
1165 return -ENOENT;
Tom Herbert3502cad2015-12-15 15:41:36 -08001166
NeilBrown8f0db012019-04-02 10:07:45 +11001167 pprev = NULL;
NeilBrown149212f2019-04-02 10:07:45 +11001168 rht_lock(tbl, bkt);
Tom Herbert3502cad2015-12-15 15:41:36 -08001169
NeilBrownadc6a3a2019-04-12 11:52:08 +10001170 rht_for_each_from(he, rht_ptr(bkt, tbl, hash), tbl, hash) {
Tom Herbert3502cad2015-12-15 15:41:36 -08001171 if (he != obj_old) {
1172 pprev = &he->next;
1173 continue;
1174 }
1175
1176 rcu_assign_pointer(obj_new->next, obj_old->next);
NeilBrown8f0db012019-04-02 10:07:45 +11001177 if (pprev) {
1178 rcu_assign_pointer(*pprev, obj_new);
NeilBrown149212f2019-04-02 10:07:45 +11001179 rht_unlock(tbl, bkt);
NeilBrown8f0db012019-04-02 10:07:45 +11001180 } else {
NeilBrown149212f2019-04-02 10:07:45 +11001181 rht_assign_unlock(tbl, bkt, obj_new);
NeilBrown8f0db012019-04-02 10:07:45 +11001182 }
Tom Herbert3502cad2015-12-15 15:41:36 -08001183 err = 0;
NeilBrown8f0db012019-04-02 10:07:45 +11001184 goto unlocked;
Tom Herbert3502cad2015-12-15 15:41:36 -08001185 }
Tom Herbert3502cad2015-12-15 15:41:36 -08001186
NeilBrown149212f2019-04-02 10:07:45 +11001187 rht_unlock(tbl, bkt);
NeilBrown8f0db012019-04-02 10:07:45 +11001188
1189unlocked:
Tom Herbert3502cad2015-12-15 15:41:36 -08001190 return err;
1191}
1192
1193/**
1194 * rhashtable_replace_fast - replace an object in hash table
1195 * @ht: hash table
1196 * @obj_old: pointer to hash head inside object being replaced
1197 * @obj_new: pointer to hash head inside object which is new
1198 * @params: hash table parameters
1199 *
1200 * Replacing an object doesn't affect the number of elements in the hash table
1201 * or bucket, so we don't need to worry about shrinking or expanding the
1202 * table here.
1203 *
1204 * Returns zero on success, -ENOENT if the entry could not be found,
1205 * -EINVAL if hash is not the same for the old and new objects.
1206 */
1207static inline int rhashtable_replace_fast(
1208 struct rhashtable *ht, struct rhash_head *obj_old,
1209 struct rhash_head *obj_new,
1210 const struct rhashtable_params params)
1211{
1212 struct bucket_table *tbl;
1213 int err;
1214
1215 rcu_read_lock();
1216
1217 tbl = rht_dereference_rcu(ht->tbl, ht);
1218
1219 /* Because we have already taken (and released) the bucket
1220 * lock in old_tbl, if we find that future_tbl is not yet
1221 * visible then that guarantees the entry to still be in
1222 * the old tbl if it exists.
1223 */
1224 while ((err = __rhashtable_replace_fast(ht, tbl, obj_old,
1225 obj_new, params)) &&
1226 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
1227 ;
1228
1229 rcu_read_unlock();
1230
1231 return err;
1232}
1233
Herbert Xuca268932016-09-19 19:00:09 +08001234/**
1235 * rhltable_walk_enter - Initialise an iterator
1236 * @hlt: Table to walk over
1237 * @iter: Hash table Iterator
1238 *
1239 * This function prepares a hash table walk.
1240 *
1241 * Note that if you restart a walk after rhashtable_walk_stop you
1242 * may see the same object twice. Also, you may miss objects if
1243 * there are removals in between rhashtable_walk_stop and the next
1244 * call to rhashtable_walk_start.
1245 *
1246 * For a completely stable walk you should construct your own data
1247 * structure outside the hash table.
1248 *
NeilBrown82266e92018-04-24 08:29:13 +10001249 * This function may be called from any process context, including
1250 * non-preemptable context, but cannot be called from softirq or
1251 * hardirq context.
Herbert Xuca268932016-09-19 19:00:09 +08001252 *
1253 * You must call rhashtable_walk_exit after this function returns.
1254 */
1255static inline void rhltable_walk_enter(struct rhltable *hlt,
1256 struct rhashtable_iter *iter)
1257{
1258 return rhashtable_walk_enter(&hlt->ht, iter);
1259}
1260
1261/**
1262 * rhltable_free_and_destroy - free elements and destroy hash list table
1263 * @hlt: the hash list table to destroy
1264 * @free_fn: callback to release resources of element
1265 * @arg: pointer passed to free_fn
1266 *
1267 * See documentation for rhashtable_free_and_destroy.
1268 */
1269static inline void rhltable_free_and_destroy(struct rhltable *hlt,
1270 void (*free_fn)(void *ptr,
1271 void *arg),
1272 void *arg)
1273{
1274 return rhashtable_free_and_destroy(&hlt->ht, free_fn, arg);
1275}
1276
1277static inline void rhltable_destroy(struct rhltable *hlt)
1278{
1279 return rhltable_free_and_destroy(hlt, NULL, NULL);
1280}
1281
Thomas Graf7e1e7762014-08-02 11:47:44 +02001282#endif /* _LINUX_RHASHTABLE_H */