blob: b54e6436547e281c65d0c5bd0e84df08cd6dfaad [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
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
NeilBrown8f0db012019-04-02 10:07:45 +110087 struct rhash_lock_head __rcu *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.
94 * We git it an address, in which the bottom 2 bits are
95 * always 0, and the msb might be significant.
96 * So we shift the address down one bit to align with
97 * expectations and avoid losing a significant bit.
98 */
99#define RHT_NULLS_MARKER(ptr) \
100 ((void *)NULLS_MARKER(((unsigned long) (ptr)) >> 1))
NeilBrown9b4f64a2018-06-18 12:52:50 +1000101#define INIT_RHT_NULLS_HEAD(ptr) \
NeilBrown82208d0d2018-11-30 10:26:50 +1100102 ((ptr) = RHT_NULLS_MARKER(&(ptr)))
Thomas Graff89bd6f2015-01-02 23:00:21 +0100103
104static inline bool rht_is_a_nulls(const struct rhash_head *ptr)
105{
106 return ((unsigned long) ptr & 1);
107}
108
Herbert Xu02fd97c2015-03-20 21:57:00 +1100109static inline void *rht_obj(const struct rhashtable *ht,
110 const struct rhash_head *he)
111{
112 return (char *)he - ht->p.head_offset;
113}
114
115static inline unsigned int rht_bucket_index(const struct bucket_table *tbl,
116 unsigned int hash)
117{
NeilBrown9f9a7072018-06-18 12:52:50 +1000118 return hash & (tbl->size - 1);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100119}
120
Tom Herbert2b860932017-12-04 10:31:43 -0800121static inline unsigned int rht_key_get_hash(struct rhashtable *ht,
122 const void *key, const struct rhashtable_params params,
123 unsigned int hash_rnd)
Herbert Xu02fd97c2015-03-20 21:57:00 +1100124{
Thomas Graf299e5c32015-03-24 14:18:17 +0100125 unsigned int hash;
Herbert Xude91b252015-03-24 00:50:20 +1100126
Herbert Xu31ccde22015-03-24 00:50:21 +1100127 /* params must be equal to ht->p if it isn't constant. */
128 if (!__builtin_constant_p(params.key_len))
Tom Herbert2b860932017-12-04 10:31:43 -0800129 hash = ht->p.hashfn(key, ht->key_len, hash_rnd);
Herbert Xu31ccde22015-03-24 00:50:21 +1100130 else if (params.key_len) {
Thomas Graf299e5c32015-03-24 14:18:17 +0100131 unsigned int key_len = params.key_len;
Herbert Xu31ccde22015-03-24 00:50:21 +1100132
133 if (params.hashfn)
Tom Herbert2b860932017-12-04 10:31:43 -0800134 hash = params.hashfn(key, key_len, hash_rnd);
Herbert Xu31ccde22015-03-24 00:50:21 +1100135 else if (key_len & (sizeof(u32) - 1))
Tom Herbert2b860932017-12-04 10:31:43 -0800136 hash = jhash(key, key_len, hash_rnd);
Herbert Xu31ccde22015-03-24 00:50:21 +1100137 else
Tom Herbert2b860932017-12-04 10:31:43 -0800138 hash = jhash2(key, key_len / sizeof(u32), hash_rnd);
Herbert Xu31ccde22015-03-24 00:50:21 +1100139 } else {
Thomas Graf299e5c32015-03-24 14:18:17 +0100140 unsigned int key_len = ht->p.key_len;
Herbert Xu31ccde22015-03-24 00:50:21 +1100141
142 if (params.hashfn)
Tom Herbert2b860932017-12-04 10:31:43 -0800143 hash = params.hashfn(key, key_len, hash_rnd);
Herbert Xu31ccde22015-03-24 00:50:21 +1100144 else
Tom Herbert2b860932017-12-04 10:31:43 -0800145 hash = jhash(key, key_len, hash_rnd);
Herbert Xu31ccde22015-03-24 00:50:21 +1100146 }
147
Tom Herbert2b860932017-12-04 10:31:43 -0800148 return hash;
149}
150
151static inline unsigned int rht_key_hashfn(
152 struct rhashtable *ht, const struct bucket_table *tbl,
153 const void *key, const struct rhashtable_params params)
154{
155 unsigned int hash = rht_key_get_hash(ht, key, params, tbl->hash_rnd);
156
Herbert Xu31ccde22015-03-24 00:50:21 +1100157 return rht_bucket_index(tbl, hash);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100158}
159
160static inline unsigned int rht_head_hashfn(
161 struct rhashtable *ht, const struct bucket_table *tbl,
162 const struct rhash_head *he, const struct rhashtable_params params)
163{
164 const char *ptr = rht_obj(ht, he);
165
166 return likely(params.obj_hashfn) ?
Patrick McHardy49f7b332015-03-25 13:07:45 +0000167 rht_bucket_index(tbl, params.obj_hashfn(ptr, params.key_len ?:
168 ht->p.key_len,
169 tbl->hash_rnd)) :
Herbert Xu02fd97c2015-03-20 21:57:00 +1100170 rht_key_hashfn(ht, tbl, ptr + params.key_offset, params);
171}
172
173/**
174 * rht_grow_above_75 - returns true if nelems > 0.75 * table-size
175 * @ht: hash table
176 * @tbl: current table
177 */
178static inline bool rht_grow_above_75(const struct rhashtable *ht,
179 const struct bucket_table *tbl)
180{
181 /* Expand table when exceeding 75% load */
182 return atomic_read(&ht->nelems) > (tbl->size / 4 * 3) &&
183 (!ht->p.max_size || tbl->size < ht->p.max_size);
184}
185
186/**
187 * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size
188 * @ht: hash table
189 * @tbl: current table
190 */
191static inline bool rht_shrink_below_30(const struct rhashtable *ht,
192 const struct bucket_table *tbl)
193{
194 /* Shrink table beneath 30% load */
195 return atomic_read(&ht->nelems) < (tbl->size * 3 / 10) &&
196 tbl->size > ht->p.min_size;
197}
198
Herbert Xuccd57b12015-03-24 00:50:28 +1100199/**
200 * rht_grow_above_100 - returns true if nelems > table-size
201 * @ht: hash table
202 * @tbl: current table
203 */
204static inline bool rht_grow_above_100(const struct rhashtable *ht,
205 const struct bucket_table *tbl)
206{
Johannes Berg1d8dc3d2015-04-23 16:38:43 +0200207 return atomic_read(&ht->nelems) > tbl->size &&
208 (!ht->p.max_size || tbl->size < ht->p.max_size);
Herbert Xuccd57b12015-03-24 00:50:28 +1100209}
210
Herbert Xu07ee0722015-05-15 11:30:47 +0800211/**
212 * rht_grow_above_max - returns true if table is above maximum
213 * @ht: hash table
214 * @tbl: current table
215 */
216static inline bool rht_grow_above_max(const struct rhashtable *ht,
217 const struct bucket_table *tbl)
218{
Herbert Xu6d684e52017-04-27 13:44:51 +0800219 return atomic_read(&ht->nelems) >= ht->max_elems;
Herbert Xu07ee0722015-05-15 11:30:47 +0800220}
221
Thomas Graf7e1e7762014-08-02 11:47:44 +0200222#ifdef CONFIG_PROVE_LOCKING
Thomas Graf97defe12015-01-02 23:00:20 +0100223int lockdep_rht_mutex_is_held(struct rhashtable *ht);
Thomas Graf88d6ed12015-01-02 23:00:16 +0100224int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200225#else
Thomas Graf97defe12015-01-02 23:00:20 +0100226static inline int lockdep_rht_mutex_is_held(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200227{
228 return 1;
229}
Thomas Graf88d6ed12015-01-02 23:00:16 +0100230
231static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl,
232 u32 hash)
233{
234 return 1;
235}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200236#endif /* CONFIG_PROVE_LOCKING */
237
Herbert Xuca268932016-09-19 19:00:09 +0800238void *rhashtable_insert_slow(struct rhashtable *ht, const void *key,
239 struct rhash_head *obj);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200240
Herbert Xu246779d2016-08-18 16:50:56 +0800241void rhashtable_walk_enter(struct rhashtable *ht,
242 struct rhashtable_iter *iter);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100243void rhashtable_walk_exit(struct rhashtable_iter *iter);
Tom Herbert97a6ec42017-12-04 10:31:41 -0800244int rhashtable_walk_start_check(struct rhashtable_iter *iter) __acquires(RCU);
245
246static inline void rhashtable_walk_start(struct rhashtable_iter *iter)
247{
248 (void)rhashtable_walk_start_check(iter);
249}
250
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100251void *rhashtable_walk_next(struct rhashtable_iter *iter);
Tom Herbert2db54b42017-12-04 10:31:42 -0800252void *rhashtable_walk_peek(struct rhashtable_iter *iter);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100253void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU);
254
Thomas Graf6b6f3022015-03-24 14:18:20 +0100255void rhashtable_free_and_destroy(struct rhashtable *ht,
256 void (*free_fn)(void *ptr, void *arg),
257 void *arg);
Thomas Graf97defe12015-01-02 23:00:20 +0100258void rhashtable_destroy(struct rhashtable *ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200259
NeilBrown8f0db012019-04-02 10:07:45 +1100260struct rhash_lock_head __rcu **rht_bucket_nested(const struct bucket_table *tbl,
261 unsigned int hash);
262struct rhash_lock_head __rcu **__rht_bucket_nested(const struct bucket_table *tbl,
Herbert Xuda204202017-02-11 19:26:47 +0800263 unsigned int hash);
NeilBrown8f0db012019-04-02 10:07:45 +1100264struct rhash_lock_head __rcu **rht_bucket_nested_insert(struct rhashtable *ht,
265 struct bucket_table *tbl,
266 unsigned int hash);
Herbert Xuda204202017-02-11 19:26:47 +0800267
Thomas Graf7e1e7762014-08-02 11:47:44 +0200268#define rht_dereference(p, ht) \
269 rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht))
270
271#define rht_dereference_rcu(p, ht) \
272 rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht))
273
Thomas Graf88d6ed12015-01-02 23:00:16 +0100274#define rht_dereference_bucket(p, tbl, hash) \
275 rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200276
Thomas Graf88d6ed12015-01-02 23:00:16 +0100277#define rht_dereference_bucket_rcu(p, tbl, hash) \
278 rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash))
279
280#define rht_entry(tpos, pos, member) \
281 ({ tpos = container_of(pos, typeof(*tpos), member); 1; })
282
NeilBrown8f0db012019-04-02 10:07:45 +1100283static inline struct rhash_lock_head __rcu *const *rht_bucket(
Herbert Xuda204202017-02-11 19:26:47 +0800284 const struct bucket_table *tbl, unsigned int hash)
285{
286 return unlikely(tbl->nest) ? rht_bucket_nested(tbl, hash) :
287 &tbl->buckets[hash];
288}
289
NeilBrown8f0db012019-04-02 10:07:45 +1100290static inline struct rhash_lock_head __rcu **rht_bucket_var(
Herbert Xuda204202017-02-11 19:26:47 +0800291 struct bucket_table *tbl, unsigned int hash)
292{
NeilBrownff302db2019-04-02 10:07:45 +1100293 return unlikely(tbl->nest) ? __rht_bucket_nested(tbl, hash) :
Herbert Xuda204202017-02-11 19:26:47 +0800294 &tbl->buckets[hash];
295}
296
NeilBrown8f0db012019-04-02 10:07:45 +1100297static inline struct rhash_lock_head __rcu **rht_bucket_insert(
Herbert Xuda204202017-02-11 19:26:47 +0800298 struct rhashtable *ht, struct bucket_table *tbl, unsigned int hash)
299{
300 return unlikely(tbl->nest) ? rht_bucket_nested_insert(ht, tbl, hash) :
301 &tbl->buckets[hash];
302}
303
NeilBrownc5783312019-04-12 11:52:08 +1000304/*
305 * We lock a bucket by setting BIT(1) in the pointer - this is always
306 * zero in real pointers and in the nulls marker.
307 * bit_spin_locks do not handle contention well, but the whole point
308 * of the hashtable design is to achieve minimum per-bucket contention.
309 * A nested hash table might not have a bucket pointer. In that case
310 * we cannot get a lock. For remove and replace the bucket cannot be
311 * interesting and doesn't need locking.
312 * For insert we allocate the bucket if this is the last bucket_table,
313 * and then take the lock.
314 * Sometimes we unlock a bucket by writing a new pointer there. In that
315 * case we don't need to unlock, but we do need to reset state such as
316 * local_bh. For that we have rht_assign_unlock(). As rcu_assign_pointer()
317 * provides the same release semantics that bit_spin_unlock() provides,
318 * this is safe.
319 */
320
321static inline void rht_lock(struct bucket_table *tbl,
322 struct rhash_lock_head **bkt)
323{
324 local_bh_disable();
325 bit_spin_lock(1, (unsigned long *)bkt);
326 lock_map_acquire(&tbl->dep_map);
327}
328
329static inline void rht_lock_nested(struct bucket_table *tbl,
330 struct rhash_lock_head **bucket,
331 unsigned int subclass)
332{
333 local_bh_disable();
334 bit_spin_lock(1, (unsigned long *)bucket);
335 lock_acquire_exclusive(&tbl->dep_map, subclass, 0, NULL, _THIS_IP_);
336}
337
338static inline void rht_unlock(struct bucket_table *tbl,
339 struct rhash_lock_head **bkt)
340{
341 lock_map_release(&tbl->dep_map);
342 bit_spin_unlock(1, (unsigned long *)bkt);
343 local_bh_enable();
344}
345
346/*
NeilBrownadc6a3a2019-04-12 11:52:08 +1000347 * Where 'bkt' is a bucket and might be locked:
348 * rht_ptr() dereferences that pointer and clears the lock bit.
349 * rht_ptr_exclusive() dereferences in a context where exclusive
350 * access is guaranteed, such as when destroying the table.
NeilBrownc5783312019-04-12 11:52:08 +1000351 */
NeilBrownadc6a3a2019-04-12 11:52:08 +1000352static inline struct rhash_head *rht_ptr(
353 struct rhash_lock_head __rcu * const *bkt,
354 struct bucket_table *tbl,
355 unsigned int hash)
NeilBrownc5783312019-04-12 11:52:08 +1000356{
NeilBrownadc6a3a2019-04-12 11:52:08 +1000357 const struct rhash_lock_head *p =
358 rht_dereference_bucket_rcu(*bkt, tbl, hash);
359
360 return (void *)(((unsigned long)p) & ~BIT(1));
361}
362
363static inline struct rhash_head *rht_ptr_exclusive(
364 struct rhash_lock_head __rcu * const *bkt)
365{
366 const struct rhash_lock_head *p =
367 rcu_dereference_protected(*bkt, 1);
368
NeilBrownc5783312019-04-12 11:52:08 +1000369 return (void *)(((unsigned long)p) & ~BIT(1));
370}
371
372static inline struct rhash_lock_head __rcu *rht_ptr_locked(const
373 struct rhash_head *p)
374{
375 return (void *)(((unsigned long)p) | BIT(1));
376}
377
378static inline void rht_assign_unlock(struct bucket_table *tbl,
379 struct rhash_lock_head __rcu **bkt,
380 struct rhash_head *obj)
381{
382 struct rhash_head __rcu **p = (struct rhash_head __rcu **)bkt;
383
384 lock_map_release(&tbl->dep_map);
385 rcu_assign_pointer(*p, obj);
386 preempt_enable();
387 __release(bitlock);
388 local_bh_enable();
389}
390
Thomas Graf88d6ed12015-01-02 23:00:16 +0100391/**
NeilBrownf7ad68b2019-03-21 14:42:40 +1100392 * rht_for_each_from - iterate over hash chain from given head
Thomas Graf88d6ed12015-01-02 23:00:16 +0100393 * @pos: the &struct rhash_head to use as a loop cursor.
NeilBrownf7ad68b2019-03-21 14:42:40 +1100394 * @head: the &struct rhash_head to start from
Thomas Graf88d6ed12015-01-02 23:00:16 +0100395 * @tbl: the &struct bucket_table
396 * @hash: the hash value / bucket index
397 */
NeilBrownf7ad68b2019-03-21 14:42:40 +1100398#define rht_for_each_from(pos, head, tbl, hash) \
NeilBrownadc6a3a2019-04-12 11:52:08 +1000399 for (pos = head; \
400 !rht_is_a_nulls(pos); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100401 pos = rht_dereference_bucket((pos)->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200402
403/**
404 * rht_for_each - iterate over hash chain
Thomas Graf88d6ed12015-01-02 23:00:16 +0100405 * @pos: the &struct rhash_head to use as a loop cursor.
406 * @tbl: the &struct bucket_table
407 * @hash: the hash value / bucket index
Thomas Graf7e1e7762014-08-02 11:47:44 +0200408 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100409#define rht_for_each(pos, tbl, hash) \
NeilBrownadc6a3a2019-04-12 11:52:08 +1000410 rht_for_each_from(pos, rht_ptr(rht_bucket(tbl, hash), tbl, hash), \
411 tbl, hash)
Thomas Graf88d6ed12015-01-02 23:00:16 +0100412
413/**
NeilBrownf7ad68b2019-03-21 14:42:40 +1100414 * rht_for_each_entry_from - iterate over hash chain from given head
Thomas Graf88d6ed12015-01-02 23:00:16 +0100415 * @tpos: the type * to use as a loop cursor.
416 * @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 * @member: name of the &struct rhash_head within the hashable struct.
421 */
NeilBrownf7ad68b2019-03-21 14:42:40 +1100422#define rht_for_each_entry_from(tpos, pos, head, tbl, hash, member) \
NeilBrownadc6a3a2019-04-12 11:52:08 +1000423 for (pos = head; \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100424 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100425 pos = rht_dereference_bucket((pos)->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200426
427/**
428 * rht_for_each_entry - iterate over hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100429 * @tpos: the type * to use as a loop cursor.
430 * @pos: the &struct rhash_head to use as a loop cursor.
431 * @tbl: the &struct bucket_table
432 * @hash: the hash value / bucket index
433 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200434 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100435#define rht_for_each_entry(tpos, pos, tbl, hash, member) \
NeilBrownadc6a3a2019-04-12 11:52:08 +1000436 rht_for_each_entry_from(tpos, pos, \
437 rht_ptr(rht_bucket(tbl, hash), tbl, hash), \
438 tbl, hash, member)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200439
440/**
441 * rht_for_each_entry_safe - safely iterate over hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100442 * @tpos: the type * to use as a loop cursor.
443 * @pos: the &struct rhash_head to use as a loop cursor.
444 * @next: the &struct rhash_head to use as next in loop cursor.
445 * @tbl: the &struct bucket_table
446 * @hash: the hash value / bucket index
447 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200448 *
449 * This hash chain list-traversal primitive allows for the looped code to
450 * remove the loop cursor from the list.
451 */
Herbert Xuda204202017-02-11 19:26:47 +0800452#define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \
NeilBrownadc6a3a2019-04-12 11:52:08 +1000453 for (pos = rht_ptr(rht_bucket(tbl, hash), tbl, hash), \
Herbert Xuda204202017-02-11 19:26:47 +0800454 next = !rht_is_a_nulls(pos) ? \
455 rht_dereference_bucket(pos->next, tbl, hash) : NULL; \
456 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
457 pos = next, \
458 next = !rht_is_a_nulls(pos) ? \
Patrick McHardy607954b2015-01-21 11:12:13 +0000459 rht_dereference_bucket(pos->next, tbl, hash) : NULL)
Thomas Graf88d6ed12015-01-02 23:00:16 +0100460
461/**
NeilBrownf7ad68b2019-03-21 14:42:40 +1100462 * rht_for_each_rcu_from - iterate over rcu hash chain from given head
Thomas Graf88d6ed12015-01-02 23:00:16 +0100463 * @pos: the &struct rhash_head to use as a loop cursor.
NeilBrownf7ad68b2019-03-21 14:42:40 +1100464 * @head: the &struct rhash_head to start from
Thomas Graf88d6ed12015-01-02 23:00:16 +0100465 * @tbl: the &struct bucket_table
466 * @hash: the hash value / bucket index
467 *
468 * This hash chain list-traversal primitive may safely run concurrently with
469 * the _rcu mutation primitives such as rhashtable_insert() as long as the
470 * traversal is guarded by rcu_read_lock().
471 */
NeilBrownf7ad68b2019-03-21 14:42:40 +1100472#define rht_for_each_rcu_from(pos, head, tbl, hash) \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100473 for (({barrier(); }), \
NeilBrownadc6a3a2019-04-12 11:52:08 +1000474 pos = head; \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100475 !rht_is_a_nulls(pos); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100476 pos = rcu_dereference_raw(pos->next))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200477
478/**
479 * rht_for_each_rcu - iterate over rcu hash chain
Thomas Graf88d6ed12015-01-02 23:00:16 +0100480 * @pos: the &struct rhash_head to use as a loop cursor.
481 * @tbl: the &struct bucket_table
482 * @hash: the hash value / bucket index
Thomas Graf7e1e7762014-08-02 11:47:44 +0200483 *
484 * This hash chain list-traversal primitive may safely run concurrently with
Thomas Graf88d6ed12015-01-02 23:00:16 +0100485 * the _rcu mutation primitives such as rhashtable_insert() as long as the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200486 * traversal is guarded by rcu_read_lock().
487 */
NeilBrown8f0db012019-04-02 10:07:45 +1100488#define rht_for_each_rcu(pos, tbl, hash) \
NeilBrownadc6a3a2019-04-12 11:52:08 +1000489 for (({barrier(); }), \
490 pos = rht_ptr(rht_bucket(tbl, hash), tbl, hash); \
491 !rht_is_a_nulls(pos); \
NeilBrown8f0db012019-04-02 10:07:45 +1100492 pos = rcu_dereference_raw(pos->next))
Thomas Graf88d6ed12015-01-02 23:00:16 +0100493
494/**
NeilBrownf7ad68b2019-03-21 14:42:40 +1100495 * rht_for_each_entry_rcu_from - iterated over rcu hash chain from given head
Thomas Graf88d6ed12015-01-02 23:00:16 +0100496 * @tpos: the type * to use as a loop cursor.
497 * @pos: the &struct rhash_head to use as a loop cursor.
NeilBrownf7ad68b2019-03-21 14:42:40 +1100498 * @head: the &struct rhash_head to start from
Thomas Graf88d6ed12015-01-02 23:00:16 +0100499 * @tbl: the &struct bucket_table
500 * @hash: the hash value / bucket index
501 * @member: name of the &struct rhash_head within the hashable struct.
502 *
503 * This hash chain list-traversal primitive may safely run concurrently with
504 * the _rcu mutation primitives such as rhashtable_insert() as long as the
505 * traversal is guarded by rcu_read_lock().
506 */
NeilBrownf7ad68b2019-03-21 14:42:40 +1100507#define rht_for_each_entry_rcu_from(tpos, pos, head, tbl, hash, member) \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100508 for (({barrier(); }), \
NeilBrownadc6a3a2019-04-12 11:52:08 +1000509 pos = head; \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100510 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100511 pos = rht_dereference_bucket_rcu(pos->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200512
513/**
514 * rht_for_each_entry_rcu - iterate over rcu hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100515 * @tpos: the type * to use as a loop cursor.
516 * @pos: the &struct rhash_head to use as a loop cursor.
517 * @tbl: the &struct bucket_table
518 * @hash: the hash value / bucket index
519 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200520 *
521 * This hash chain list-traversal primitive may safely run concurrently with
Thomas Graf88d6ed12015-01-02 23:00:16 +0100522 * the _rcu mutation primitives such as rhashtable_insert() as long as the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200523 * traversal is guarded by rcu_read_lock().
524 */
Herbert Xuda204202017-02-11 19:26:47 +0800525#define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \
NeilBrown8f0db012019-04-02 10:07:45 +1100526 rht_for_each_entry_rcu_from(tpos, pos, \
NeilBrownadc6a3a2019-04-12 11:52:08 +1000527 rht_ptr(rht_bucket(tbl, hash), \
528 tbl, hash), \
529 tbl, hash, member)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200530
Herbert Xuca268932016-09-19 19:00:09 +0800531/**
532 * rhl_for_each_rcu - iterate over rcu hash table list
533 * @pos: the &struct rlist_head to use as a loop cursor.
534 * @list: the head of the list
535 *
536 * This hash chain list-traversal primitive should be used on the
537 * list returned by rhltable_lookup.
538 */
539#define rhl_for_each_rcu(pos, list) \
540 for (pos = list; pos; pos = rcu_dereference_raw(pos->next))
541
542/**
543 * rhl_for_each_entry_rcu - iterate over rcu hash table list of given type
544 * @tpos: the type * to use as a loop cursor.
545 * @pos: the &struct rlist_head to use as a loop cursor.
546 * @list: the head of the list
547 * @member: name of the &struct rlist_head within the hashable struct.
548 *
549 * This hash chain list-traversal primitive should be used on the
550 * list returned by rhltable_lookup.
551 */
552#define rhl_for_each_entry_rcu(tpos, pos, list, member) \
553 for (pos = list; pos && rht_entry(tpos, pos, member); \
554 pos = rcu_dereference_raw(pos->next))
555
Herbert Xu02fd97c2015-03-20 21:57:00 +1100556static inline int rhashtable_compare(struct rhashtable_compare_arg *arg,
557 const void *obj)
558{
559 struct rhashtable *ht = arg->ht;
560 const char *ptr = obj;
561
562 return memcmp(ptr + ht->p.key_offset, arg->key, ht->p.key_len);
563}
564
Herbert Xuca268932016-09-19 19:00:09 +0800565/* Internal function, do not use. */
566static inline struct rhash_head *__rhashtable_lookup(
Herbert Xu02fd97c2015-03-20 21:57:00 +1100567 struct rhashtable *ht, const void *key,
568 const struct rhashtable_params params)
569{
570 struct rhashtable_compare_arg arg = {
571 .ht = ht,
572 .key = key,
573 };
NeilBrown8f0db012019-04-02 10:07:45 +1100574 struct rhash_lock_head __rcu * const *bkt;
Herbert Xuda204202017-02-11 19:26:47 +0800575 struct bucket_table *tbl;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100576 struct rhash_head *he;
Thomas Graf299e5c32015-03-24 14:18:17 +0100577 unsigned int hash;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100578
Herbert Xu02fd97c2015-03-20 21:57:00 +1100579 tbl = rht_dereference_rcu(ht->tbl, ht);
580restart:
581 hash = rht_key_hashfn(ht, tbl, key, params);
NeilBrown8f0db012019-04-02 10:07:45 +1100582 bkt = rht_bucket(tbl, hash);
NeilBrown82208d0d2018-11-30 10:26:50 +1100583 do {
NeilBrownadc6a3a2019-04-12 11:52:08 +1000584 rht_for_each_rcu_from(he, rht_ptr(bkt, tbl, hash), tbl, hash) {
NeilBrown82208d0d2018-11-30 10:26:50 +1100585 if (params.obj_cmpfn ?
586 params.obj_cmpfn(&arg, rht_obj(ht, he)) :
587 rhashtable_compare(&arg, rht_obj(ht, he)))
588 continue;
589 return he;
590 }
591 /* An object might have been moved to a different hash chain,
592 * while we walk along it - better check and retry.
593 */
NeilBrown8f0db012019-04-02 10:07:45 +1100594 } while (he != RHT_NULLS_MARKER(bkt));
Herbert Xu02fd97c2015-03-20 21:57:00 +1100595
596 /* Ensure we see any new tables. */
597 smp_rmb();
598
599 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
600 if (unlikely(tbl))
601 goto restart;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100602
603 return NULL;
604}
605
Herbert Xuca268932016-09-19 19:00:09 +0800606/**
607 * rhashtable_lookup - search hash table
608 * @ht: hash table
609 * @key: the pointer to the key
610 * @params: hash table parameters
611 *
612 * Computes the hash value for the key and traverses the bucket chain looking
613 * for a entry with an identical key. The first matching entry is returned.
614 *
615 * This must only be called under the RCU read lock.
616 *
617 * Returns the first entry on which the compare function returned true.
618 */
619static inline void *rhashtable_lookup(
620 struct rhashtable *ht, const void *key,
621 const struct rhashtable_params params)
622{
623 struct rhash_head *he = __rhashtable_lookup(ht, key, params);
624
625 return he ? rht_obj(ht, he) : NULL;
626}
627
628/**
629 * rhashtable_lookup_fast - search hash table, without RCU read lock
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 * Only use this function when you have other mechanisms guaranteeing
638 * that the object won't go away after the RCU read lock is released.
639 *
640 * Returns the first entry on which the compare function returned true.
641 */
642static inline void *rhashtable_lookup_fast(
643 struct rhashtable *ht, const void *key,
644 const struct rhashtable_params params)
645{
646 void *obj;
647
648 rcu_read_lock();
649 obj = rhashtable_lookup(ht, key, params);
650 rcu_read_unlock();
651
652 return obj;
653}
654
655/**
656 * rhltable_lookup - search hash list table
657 * @hlt: hash table
658 * @key: the pointer to the key
659 * @params: hash table parameters
660 *
661 * Computes the hash value for the key and traverses the bucket chain looking
662 * for a entry with an identical key. All matching entries are returned
663 * in a list.
664 *
665 * This must only be called under the RCU read lock.
666 *
667 * Returns the list of entries that match the given key.
668 */
669static inline struct rhlist_head *rhltable_lookup(
670 struct rhltable *hlt, const void *key,
671 const struct rhashtable_params params)
672{
673 struct rhash_head *he = __rhashtable_lookup(&hlt->ht, key, params);
674
675 return he ? container_of(he, struct rhlist_head, rhead) : NULL;
676}
677
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200678/* Internal function, please use rhashtable_insert_fast() instead. This
679 * function returns the existing element already in hashes in there is a clash,
680 * otherwise it returns an error via ERR_PTR().
681 */
682static inline void *__rhashtable_insert_fast(
Herbert Xu02fd97c2015-03-20 21:57:00 +1100683 struct rhashtable *ht, const void *key, struct rhash_head *obj,
Herbert Xuca268932016-09-19 19:00:09 +0800684 const struct rhashtable_params params, bool rhlist)
Herbert Xu02fd97c2015-03-20 21:57:00 +1100685{
686 struct rhashtable_compare_arg arg = {
687 .ht = ht,
688 .key = key,
689 };
NeilBrown8f0db012019-04-02 10:07:45 +1100690 struct rhash_lock_head __rcu **bkt;
Herbert Xuca268932016-09-19 19:00:09 +0800691 struct rhash_head __rcu **pprev;
692 struct bucket_table *tbl;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100693 struct rhash_head *head;
Thomas Graf299e5c32015-03-24 14:18:17 +0100694 unsigned int hash;
Herbert Xuca268932016-09-19 19:00:09 +0800695 int elasticity;
696 void *data;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100697
698 rcu_read_lock();
699
700 tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xuca268932016-09-19 19:00:09 +0800701 hash = rht_head_hashfn(ht, tbl, obj, params);
NeilBrown8f0db012019-04-02 10:07:45 +1100702 elasticity = RHT_ELASTICITY;
703 bkt = rht_bucket_insert(ht, tbl, hash);
704 data = ERR_PTR(-ENOMEM);
705 if (!bkt)
706 goto out;
707 pprev = NULL;
NeilBrown149212f2019-04-02 10:07:45 +1100708 rht_lock(tbl, bkt);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100709
NeilBrownc0690012018-06-18 12:52:50 +1000710 if (unlikely(rcu_access_pointer(tbl->future_tbl))) {
Herbert Xuca268932016-09-19 19:00:09 +0800711slow_path:
NeilBrown149212f2019-04-02 10:07:45 +1100712 rht_unlock(tbl, bkt);
Herbert Xuca268932016-09-19 19:00:09 +0800713 rcu_read_unlock();
714 return rhashtable_insert_slow(ht, key, obj);
Herbert Xub8244782015-03-24 00:50:26 +1100715 }
716
NeilBrownadc6a3a2019-04-12 11:52:08 +1000717 rht_for_each_from(head, rht_ptr(bkt, tbl, hash), tbl, hash) {
Herbert Xuca268932016-09-19 19:00:09 +0800718 struct rhlist_head *plist;
719 struct rhlist_head *list;
Herbert Xu3cf92222015-12-03 20:41:29 +0800720
Herbert Xuca268932016-09-19 19:00:09 +0800721 elasticity--;
722 if (!key ||
723 (params.obj_cmpfn ?
724 params.obj_cmpfn(&arg, rht_obj(ht, head)) :
Paul Blakeyd3dcf8e2018-03-04 17:29:48 +0200725 rhashtable_compare(&arg, rht_obj(ht, head)))) {
726 pprev = &head->next;
Herbert Xuca268932016-09-19 19:00:09 +0800727 continue;
Paul Blakeyd3dcf8e2018-03-04 17:29:48 +0200728 }
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200729
Herbert Xuca268932016-09-19 19:00:09 +0800730 data = rht_obj(ht, head);
731
732 if (!rhlist)
NeilBrown8f0db012019-04-02 10:07:45 +1100733 goto out_unlock;
Herbert Xuca268932016-09-19 19:00:09 +0800734
735
736 list = container_of(obj, struct rhlist_head, rhead);
737 plist = container_of(head, struct rhlist_head, rhead);
738
739 RCU_INIT_POINTER(list->next, plist);
740 head = rht_dereference_bucket(head->next, tbl, hash);
741 RCU_INIT_POINTER(list->rhead.next, head);
NeilBrown8f0db012019-04-02 10:07:45 +1100742 if (pprev) {
743 rcu_assign_pointer(*pprev, obj);
NeilBrown149212f2019-04-02 10:07:45 +1100744 rht_unlock(tbl, bkt);
NeilBrown8f0db012019-04-02 10:07:45 +1100745 } else
NeilBrown149212f2019-04-02 10:07:45 +1100746 rht_assign_unlock(tbl, bkt, obj);
NeilBrown8f0db012019-04-02 10:07:45 +1100747 data = NULL;
748 goto out;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100749 }
750
Herbert Xuca268932016-09-19 19:00:09 +0800751 if (elasticity <= 0)
752 goto slow_path;
753
754 data = ERR_PTR(-E2BIG);
Herbert Xu07ee0722015-05-15 11:30:47 +0800755 if (unlikely(rht_grow_above_max(ht, tbl)))
NeilBrown8f0db012019-04-02 10:07:45 +1100756 goto out_unlock;
Herbert Xu07ee0722015-05-15 11:30:47 +0800757
Herbert Xuca268932016-09-19 19:00:09 +0800758 if (unlikely(rht_grow_above_100(ht, tbl)))
759 goto slow_path;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100760
NeilBrown8f0db012019-04-02 10:07:45 +1100761 /* Inserting at head of list makes unlocking free. */
NeilBrownadc6a3a2019-04-12 11:52:08 +1000762 head = rht_ptr(bkt, tbl, hash);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100763
764 RCU_INIT_POINTER(obj->next, head);
Herbert Xuca268932016-09-19 19:00:09 +0800765 if (rhlist) {
766 struct rhlist_head *list;
767
768 list = container_of(obj, struct rhlist_head, rhead);
769 RCU_INIT_POINTER(list->next, NULL);
770 }
Herbert Xu02fd97c2015-03-20 21:57:00 +1100771
Herbert Xu02fd97c2015-03-20 21:57:00 +1100772 atomic_inc(&ht->nelems);
NeilBrown149212f2019-04-02 10:07:45 +1100773 rht_assign_unlock(tbl, bkt, obj);
NeilBrown8f0db012019-04-02 10:07:45 +1100774
Herbert Xu02fd97c2015-03-20 21:57:00 +1100775 if (rht_grow_above_75(ht, tbl))
776 schedule_work(&ht->run_work);
777
Herbert Xuca268932016-09-19 19:00:09 +0800778 data = NULL;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100779out:
Herbert Xu02fd97c2015-03-20 21:57:00 +1100780 rcu_read_unlock();
781
Herbert Xuca268932016-09-19 19:00:09 +0800782 return data;
NeilBrown8f0db012019-04-02 10:07:45 +1100783
784out_unlock:
NeilBrown149212f2019-04-02 10:07:45 +1100785 rht_unlock(tbl, bkt);
NeilBrown8f0db012019-04-02 10:07:45 +1100786 goto out;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100787}
788
789/**
790 * rhashtable_insert_fast - insert object into hash table
791 * @ht: hash table
792 * @obj: pointer to hash head inside object
793 * @params: hash table parameters
794 *
NeilBrown8f0db012019-04-02 10:07:45 +1100795 * Will take the per bucket bitlock to protect against mutual mutations
Herbert Xu02fd97c2015-03-20 21:57:00 +1100796 * on the same bucket. Multiple insertions may occur in parallel unless
NeilBrown8f0db012019-04-02 10:07:45 +1100797 * they map to the same bucket.
Herbert Xu02fd97c2015-03-20 21:57:00 +1100798 *
799 * It is safe to call this function from atomic context.
800 *
NeilBrown0c6f69a2018-04-24 08:29:13 +1000801 * Will trigger an automatic deferred table resizing if residency in the
802 * table grows beyond 70%.
Herbert Xu02fd97c2015-03-20 21:57:00 +1100803 */
804static inline int rhashtable_insert_fast(
805 struct rhashtable *ht, struct rhash_head *obj,
806 const struct rhashtable_params params)
807{
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200808 void *ret;
809
Herbert Xuca268932016-09-19 19:00:09 +0800810 ret = __rhashtable_insert_fast(ht, NULL, obj, params, false);
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200811 if (IS_ERR(ret))
812 return PTR_ERR(ret);
813
814 return ret == NULL ? 0 : -EEXIST;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100815}
816
817/**
Herbert Xuca268932016-09-19 19:00:09 +0800818 * rhltable_insert_key - insert object into hash list table
819 * @hlt: hash list table
820 * @key: the pointer to the key
821 * @list: pointer to hash list head inside object
822 * @params: hash table parameters
823 *
NeilBrown8f0db012019-04-02 10:07:45 +1100824 * Will take the per bucket bitlock to protect against mutual mutations
Herbert Xuca268932016-09-19 19:00:09 +0800825 * on the same bucket. Multiple insertions may occur in parallel unless
NeilBrown8f0db012019-04-02 10:07:45 +1100826 * they map to the same bucket.
Herbert Xuca268932016-09-19 19:00:09 +0800827 *
828 * It is safe to call this function from atomic context.
829 *
NeilBrown0c6f69a2018-04-24 08:29:13 +1000830 * Will trigger an automatic deferred table resizing if residency in the
831 * table grows beyond 70%.
Herbert Xuca268932016-09-19 19:00:09 +0800832 */
833static inline int rhltable_insert_key(
834 struct rhltable *hlt, const void *key, struct rhlist_head *list,
835 const struct rhashtable_params params)
836{
837 return PTR_ERR(__rhashtable_insert_fast(&hlt->ht, key, &list->rhead,
838 params, true));
839}
840
841/**
842 * rhltable_insert - insert object into hash list table
843 * @hlt: hash list table
844 * @list: pointer to hash list head inside object
845 * @params: hash table parameters
846 *
NeilBrown8f0db012019-04-02 10:07:45 +1100847 * Will take the per bucket bitlock to protect against mutual mutations
Herbert Xuca268932016-09-19 19:00:09 +0800848 * on the same bucket. Multiple insertions may occur in parallel unless
NeilBrown8f0db012019-04-02 10:07:45 +1100849 * they map to the same bucket.
Herbert Xuca268932016-09-19 19:00:09 +0800850 *
851 * It is safe to call this function from atomic context.
852 *
NeilBrown0c6f69a2018-04-24 08:29:13 +1000853 * Will trigger an automatic deferred table resizing if residency in the
854 * table grows beyond 70%.
Herbert Xuca268932016-09-19 19:00:09 +0800855 */
856static inline int rhltable_insert(
857 struct rhltable *hlt, struct rhlist_head *list,
858 const struct rhashtable_params params)
859{
860 const char *key = rht_obj(&hlt->ht, &list->rhead);
861
862 key += params.key_offset;
863
864 return rhltable_insert_key(hlt, key, list, params);
865}
866
867/**
Herbert Xu02fd97c2015-03-20 21:57:00 +1100868 * rhashtable_lookup_insert_fast - lookup and insert object into hash table
869 * @ht: hash table
870 * @obj: pointer to hash head inside object
871 * @params: hash table parameters
872 *
Herbert Xu02fd97c2015-03-20 21:57:00 +1100873 * This lookup function may only be used for fixed key hash table (key_len
874 * parameter set). It will BUG() if used inappropriately.
875 *
876 * It is safe to call this function from atomic context.
877 *
NeilBrown0c6f69a2018-04-24 08:29:13 +1000878 * Will trigger an automatic deferred table resizing if residency in the
879 * table grows beyond 70%.
Herbert Xu02fd97c2015-03-20 21:57:00 +1100880 */
881static inline int rhashtable_lookup_insert_fast(
882 struct rhashtable *ht, struct rhash_head *obj,
883 const struct rhashtable_params params)
884{
885 const char *key = rht_obj(ht, obj);
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200886 void *ret;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100887
888 BUG_ON(ht->p.obj_hashfn);
889
Herbert Xuca268932016-09-19 19:00:09 +0800890 ret = __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params,
891 false);
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200892 if (IS_ERR(ret))
893 return PTR_ERR(ret);
894
895 return ret == NULL ? 0 : -EEXIST;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100896}
897
898/**
Andreas Gruenbacherf9fe1c12017-03-18 00:36:15 +0100899 * rhashtable_lookup_get_insert_fast - lookup and insert object into hash table
900 * @ht: hash table
901 * @obj: pointer to hash head inside object
902 * @params: hash table parameters
903 *
904 * Just like rhashtable_lookup_insert_fast(), but this function returns the
905 * object if it exists, NULL if it did not and the insertion was successful,
906 * and an ERR_PTR otherwise.
907 */
908static inline void *rhashtable_lookup_get_insert_fast(
909 struct rhashtable *ht, struct rhash_head *obj,
910 const struct rhashtable_params params)
911{
912 const char *key = rht_obj(ht, obj);
913
914 BUG_ON(ht->p.obj_hashfn);
915
916 return __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params,
917 false);
918}
919
920/**
Herbert Xu02fd97c2015-03-20 21:57:00 +1100921 * rhashtable_lookup_insert_key - search and insert object to hash table
922 * with explicit key
923 * @ht: hash table
924 * @key: key
925 * @obj: pointer to hash head inside object
926 * @params: hash table parameters
927 *
Herbert Xu02fd97c2015-03-20 21:57:00 +1100928 * Lookups may occur in parallel with hashtable mutations and resizing.
929 *
NeilBrown0c6f69a2018-04-24 08:29:13 +1000930 * Will trigger an automatic deferred table resizing if residency in the
931 * table grows beyond 70%.
Herbert Xu02fd97c2015-03-20 21:57:00 +1100932 *
933 * Returns zero on success.
934 */
935static inline int rhashtable_lookup_insert_key(
936 struct rhashtable *ht, const void *key, struct rhash_head *obj,
937 const struct rhashtable_params params)
938{
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200939 void *ret;
940
941 BUG_ON(!ht->p.obj_hashfn || !key);
942
Herbert Xuca268932016-09-19 19:00:09 +0800943 ret = __rhashtable_insert_fast(ht, key, obj, params, false);
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200944 if (IS_ERR(ret))
945 return PTR_ERR(ret);
946
947 return ret == NULL ? 0 : -EEXIST;
948}
949
950/**
951 * rhashtable_lookup_get_insert_key - lookup and insert object into hash table
952 * @ht: hash table
953 * @obj: pointer to hash head inside object
954 * @params: hash table parameters
955 * @data: pointer to element data already in hashes
956 *
957 * Just like rhashtable_lookup_insert_key(), but this function returns the
958 * object if it exists, NULL if it does not and the insertion was successful,
959 * and an ERR_PTR otherwise.
960 */
961static inline void *rhashtable_lookup_get_insert_key(
962 struct rhashtable *ht, const void *key, struct rhash_head *obj,
963 const struct rhashtable_params params)
964{
Herbert Xu02fd97c2015-03-20 21:57:00 +1100965 BUG_ON(!ht->p.obj_hashfn || !key);
966
Herbert Xuca268932016-09-19 19:00:09 +0800967 return __rhashtable_insert_fast(ht, key, obj, params, false);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100968}
969
Thomas Grafac833bd2015-03-24 14:18:18 +0100970/* Internal function, please use rhashtable_remove_fast() instead */
Herbert Xuca268932016-09-19 19:00:09 +0800971static inline int __rhashtable_remove_fast_one(
Herbert Xu02fd97c2015-03-20 21:57:00 +1100972 struct rhashtable *ht, struct bucket_table *tbl,
Herbert Xuca268932016-09-19 19:00:09 +0800973 struct rhash_head *obj, const struct rhashtable_params params,
974 bool rhlist)
Herbert Xu02fd97c2015-03-20 21:57:00 +1100975{
NeilBrown8f0db012019-04-02 10:07:45 +1100976 struct rhash_lock_head __rcu **bkt;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100977 struct rhash_head __rcu **pprev;
978 struct rhash_head *he;
Thomas Graf299e5c32015-03-24 14:18:17 +0100979 unsigned int hash;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100980 int err = -ENOENT;
981
982 hash = rht_head_hashfn(ht, tbl, obj, params);
NeilBrown8f0db012019-04-02 10:07:45 +1100983 bkt = rht_bucket_var(tbl, hash);
984 if (!bkt)
985 return -ENOENT;
986 pprev = NULL;
NeilBrown149212f2019-04-02 10:07:45 +1100987 rht_lock(tbl, bkt);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100988
NeilBrownadc6a3a2019-04-12 11:52:08 +1000989 rht_for_each_from(he, rht_ptr(bkt, tbl, hash), tbl, hash) {
Herbert Xuca268932016-09-19 19:00:09 +0800990 struct rhlist_head *list;
991
992 list = container_of(he, struct rhlist_head, rhead);
993
Herbert Xu02fd97c2015-03-20 21:57:00 +1100994 if (he != obj) {
Herbert Xuca268932016-09-19 19:00:09 +0800995 struct rhlist_head __rcu **lpprev;
996
Herbert Xu02fd97c2015-03-20 21:57:00 +1100997 pprev = &he->next;
Herbert Xuca268932016-09-19 19:00:09 +0800998
999 if (!rhlist)
1000 continue;
1001
1002 do {
1003 lpprev = &list->next;
1004 list = rht_dereference_bucket(list->next,
1005 tbl, hash);
1006 } while (list && obj != &list->rhead);
1007
1008 if (!list)
1009 continue;
1010
1011 list = rht_dereference_bucket(list->next, tbl, hash);
1012 RCU_INIT_POINTER(*lpprev, list);
1013 err = 0;
1014 break;
Herbert Xu02fd97c2015-03-20 21:57:00 +11001015 }
1016
Herbert Xuca268932016-09-19 19:00:09 +08001017 obj = rht_dereference_bucket(obj->next, tbl, hash);
1018 err = 1;
1019
1020 if (rhlist) {
1021 list = rht_dereference_bucket(list->next, tbl, hash);
1022 if (list) {
1023 RCU_INIT_POINTER(list->rhead.next, obj);
1024 obj = &list->rhead;
1025 err = 0;
1026 }
1027 }
1028
NeilBrown8f0db012019-04-02 10:07:45 +11001029 if (pprev) {
1030 rcu_assign_pointer(*pprev, obj);
NeilBrown149212f2019-04-02 10:07:45 +11001031 rht_unlock(tbl, bkt);
NeilBrown8f0db012019-04-02 10:07:45 +11001032 } else {
NeilBrown149212f2019-04-02 10:07:45 +11001033 rht_assign_unlock(tbl, bkt, obj);
NeilBrown8f0db012019-04-02 10:07:45 +11001034 }
1035 goto unlocked;
Herbert Xu02fd97c2015-03-20 21:57:00 +11001036 }
1037
NeilBrown149212f2019-04-02 10:07:45 +11001038 rht_unlock(tbl, bkt);
NeilBrown8f0db012019-04-02 10:07:45 +11001039unlocked:
Herbert Xuca268932016-09-19 19:00:09 +08001040 if (err > 0) {
1041 atomic_dec(&ht->nelems);
1042 if (unlikely(ht->p.automatic_shrinking &&
1043 rht_shrink_below_30(ht, tbl)))
1044 schedule_work(&ht->run_work);
1045 err = 0;
1046 }
1047
1048 return err;
1049}
1050
1051/* Internal function, please use rhashtable_remove_fast() instead */
1052static inline int __rhashtable_remove_fast(
1053 struct rhashtable *ht, struct rhash_head *obj,
1054 const struct rhashtable_params params, bool rhlist)
1055{
1056 struct bucket_table *tbl;
1057 int err;
1058
1059 rcu_read_lock();
1060
1061 tbl = rht_dereference_rcu(ht->tbl, ht);
1062
1063 /* Because we have already taken (and released) the bucket
1064 * lock in old_tbl, if we find that future_tbl is not yet
1065 * visible then that guarantees the entry to still be in
1066 * the old tbl if it exists.
1067 */
1068 while ((err = __rhashtable_remove_fast_one(ht, tbl, obj, params,
1069 rhlist)) &&
1070 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
1071 ;
1072
1073 rcu_read_unlock();
1074
Herbert Xu02fd97c2015-03-20 21:57:00 +11001075 return err;
1076}
1077
1078/**
1079 * rhashtable_remove_fast - remove object from hash table
1080 * @ht: hash table
1081 * @obj: pointer to hash head inside object
1082 * @params: hash table parameters
1083 *
1084 * Since the hash chain is single linked, the removal operation needs to
1085 * walk the bucket chain upon removal. The removal operation is thus
1086 * considerable slow if the hash table is not correctly sized.
1087 *
NeilBrown0c6f69a2018-04-24 08:29:13 +10001088 * Will automatically shrink the table if permitted when residency drops
1089 * below 30%.
Herbert Xu02fd97c2015-03-20 21:57:00 +11001090 *
1091 * Returns zero on success, -ENOENT if the entry could not be found.
1092 */
1093static inline int rhashtable_remove_fast(
1094 struct rhashtable *ht, struct rhash_head *obj,
1095 const struct rhashtable_params params)
1096{
Herbert Xuca268932016-09-19 19:00:09 +08001097 return __rhashtable_remove_fast(ht, obj, params, false);
1098}
Herbert Xu02fd97c2015-03-20 21:57:00 +11001099
Herbert Xuca268932016-09-19 19:00:09 +08001100/**
1101 * rhltable_remove - remove object from hash list table
1102 * @hlt: hash list table
1103 * @list: pointer to hash list 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 Xuca268932016-09-19 19:00:09 +08001112 *
1113 * Returns zero on success, -ENOENT if the entry could not be found.
1114 */
1115static inline int rhltable_remove(
1116 struct rhltable *hlt, struct rhlist_head *list,
1117 const struct rhashtable_params params)
1118{
1119 return __rhashtable_remove_fast(&hlt->ht, &list->rhead, params, true);
Herbert Xu02fd97c2015-03-20 21:57:00 +11001120}
1121
Tom Herbert3502cad2015-12-15 15:41:36 -08001122/* Internal function, please use rhashtable_replace_fast() instead */
1123static inline int __rhashtable_replace_fast(
1124 struct rhashtable *ht, struct bucket_table *tbl,
1125 struct rhash_head *obj_old, struct rhash_head *obj_new,
1126 const struct rhashtable_params params)
1127{
NeilBrown8f0db012019-04-02 10:07:45 +11001128 struct rhash_lock_head __rcu **bkt;
Tom Herbert3502cad2015-12-15 15:41:36 -08001129 struct rhash_head __rcu **pprev;
1130 struct rhash_head *he;
Tom Herbert3502cad2015-12-15 15:41:36 -08001131 unsigned int hash;
1132 int err = -ENOENT;
1133
1134 /* Minimally, the old and new objects must have same hash
1135 * (which should mean identifiers are the same).
1136 */
1137 hash = rht_head_hashfn(ht, tbl, obj_old, params);
1138 if (hash != rht_head_hashfn(ht, tbl, obj_new, params))
1139 return -EINVAL;
1140
NeilBrown8f0db012019-04-02 10:07:45 +11001141 bkt = rht_bucket_var(tbl, hash);
1142 if (!bkt)
1143 return -ENOENT;
Tom Herbert3502cad2015-12-15 15:41:36 -08001144
NeilBrown8f0db012019-04-02 10:07:45 +11001145 pprev = NULL;
NeilBrown149212f2019-04-02 10:07:45 +11001146 rht_lock(tbl, bkt);
Tom Herbert3502cad2015-12-15 15:41:36 -08001147
NeilBrownadc6a3a2019-04-12 11:52:08 +10001148 rht_for_each_from(he, rht_ptr(bkt, tbl, hash), tbl, hash) {
Tom Herbert3502cad2015-12-15 15:41:36 -08001149 if (he != obj_old) {
1150 pprev = &he->next;
1151 continue;
1152 }
1153
1154 rcu_assign_pointer(obj_new->next, obj_old->next);
NeilBrown8f0db012019-04-02 10:07:45 +11001155 if (pprev) {
1156 rcu_assign_pointer(*pprev, obj_new);
NeilBrown149212f2019-04-02 10:07:45 +11001157 rht_unlock(tbl, bkt);
NeilBrown8f0db012019-04-02 10:07:45 +11001158 } else {
NeilBrown149212f2019-04-02 10:07:45 +11001159 rht_assign_unlock(tbl, bkt, obj_new);
NeilBrown8f0db012019-04-02 10:07:45 +11001160 }
Tom Herbert3502cad2015-12-15 15:41:36 -08001161 err = 0;
NeilBrown8f0db012019-04-02 10:07:45 +11001162 goto unlocked;
Tom Herbert3502cad2015-12-15 15:41:36 -08001163 }
Tom Herbert3502cad2015-12-15 15:41:36 -08001164
NeilBrown149212f2019-04-02 10:07:45 +11001165 rht_unlock(tbl, bkt);
NeilBrown8f0db012019-04-02 10:07:45 +11001166
1167unlocked:
Tom Herbert3502cad2015-12-15 15:41:36 -08001168 return err;
1169}
1170
1171/**
1172 * rhashtable_replace_fast - replace an object in hash table
1173 * @ht: hash table
1174 * @obj_old: pointer to hash head inside object being replaced
1175 * @obj_new: pointer to hash head inside object which is new
1176 * @params: hash table parameters
1177 *
1178 * Replacing an object doesn't affect the number of elements in the hash table
1179 * or bucket, so we don't need to worry about shrinking or expanding the
1180 * table here.
1181 *
1182 * Returns zero on success, -ENOENT if the entry could not be found,
1183 * -EINVAL if hash is not the same for the old and new objects.
1184 */
1185static inline int rhashtable_replace_fast(
1186 struct rhashtable *ht, struct rhash_head *obj_old,
1187 struct rhash_head *obj_new,
1188 const struct rhashtable_params params)
1189{
1190 struct bucket_table *tbl;
1191 int err;
1192
1193 rcu_read_lock();
1194
1195 tbl = rht_dereference_rcu(ht->tbl, ht);
1196
1197 /* Because we have already taken (and released) the bucket
1198 * lock in old_tbl, if we find that future_tbl is not yet
1199 * visible then that guarantees the entry to still be in
1200 * the old tbl if it exists.
1201 */
1202 while ((err = __rhashtable_replace_fast(ht, tbl, obj_old,
1203 obj_new, params)) &&
1204 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
1205 ;
1206
1207 rcu_read_unlock();
1208
1209 return err;
1210}
1211
Herbert Xuca268932016-09-19 19:00:09 +08001212/**
1213 * rhltable_walk_enter - Initialise an iterator
1214 * @hlt: Table to walk over
1215 * @iter: Hash table Iterator
1216 *
1217 * This function prepares a hash table walk.
1218 *
1219 * Note that if you restart a walk after rhashtable_walk_stop you
1220 * may see the same object twice. Also, you may miss objects if
1221 * there are removals in between rhashtable_walk_stop and the next
1222 * call to rhashtable_walk_start.
1223 *
1224 * For a completely stable walk you should construct your own data
1225 * structure outside the hash table.
1226 *
NeilBrown82266e92018-04-24 08:29:13 +10001227 * This function may be called from any process context, including
1228 * non-preemptable context, but cannot be called from softirq or
1229 * hardirq context.
Herbert Xuca268932016-09-19 19:00:09 +08001230 *
1231 * You must call rhashtable_walk_exit after this function returns.
1232 */
1233static inline void rhltable_walk_enter(struct rhltable *hlt,
1234 struct rhashtable_iter *iter)
1235{
1236 return rhashtable_walk_enter(&hlt->ht, iter);
1237}
1238
1239/**
1240 * rhltable_free_and_destroy - free elements and destroy hash list table
1241 * @hlt: the hash list table to destroy
1242 * @free_fn: callback to release resources of element
1243 * @arg: pointer passed to free_fn
1244 *
1245 * See documentation for rhashtable_free_and_destroy.
1246 */
1247static inline void rhltable_free_and_destroy(struct rhltable *hlt,
1248 void (*free_fn)(void *ptr,
1249 void *arg),
1250 void *arg)
1251{
1252 return rhashtable_free_and_destroy(&hlt->ht, free_fn, arg);
1253}
1254
1255static inline void rhltable_destroy(struct rhltable *hlt)
1256{
1257 return rhltable_free_and_destroy(hlt, NULL, NULL);
1258}
1259
Thomas Graf7e1e7762014-08-02 11:47:44 +02001260#endif /* _LINUX_RHASHTABLE_H */