blob: 13ccc483738d0316ceec8ec8cf9d12d3f9de3e1a [file] [log] [blame]
Thomas Graf7e1e7762014-08-02 11:47:44 +02001/*
2 * Resizable, Scalable, Concurrent Hash Table
3 *
Herbert Xuca268932016-09-19 19:00:09 +08004 * Copyright (c) 2015-2016 Herbert Xu <herbert@gondor.apana.org.au>
Thomas Grafb5e2c152015-03-24 20:42:19 +00005 * Copyright (c) 2014-2015 Thomas Graf <tgraf@suug.ch>
Thomas Graf7e1e7762014-08-02 11:47:44 +02006 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
7 *
Thomas Graf7e1e7762014-08-02 11:47:44 +02008 * Code partially derived from nft_hash
Herbert Xudc0ee262015-03-20 21:57:06 +11009 * Rewritten with rehash code from br_multicast plus single list
10 * pointer as suggested by Josh Triplett
Thomas Graf7e1e7762014-08-02 11:47:44 +020011 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 */
16
17#ifndef _LINUX_RHASHTABLE_H
18#define _LINUX_RHASHTABLE_H
19
Herbert Xu07ee0722015-05-15 11:30:47 +080020#include <linux/atomic.h>
Herbert Xuf2dba9c2015-02-04 07:33:23 +110021#include <linux/compiler.h>
Herbert Xu3cf92222015-12-03 20:41:29 +080022#include <linux/err.h>
Herbert Xu6626af62015-03-20 18:18:45 -040023#include <linux/errno.h>
Herbert Xu31ccde22015-03-24 00:50:21 +110024#include <linux/jhash.h>
Thomas Graff89bd6f2015-01-02 23:00:21 +010025#include <linux/list_nulls.h>
Thomas Graf97defe12015-01-02 23:00:20 +010026#include <linux/workqueue.h>
Ying Xue86b35b62015-01-04 15:25:09 +080027#include <linux/mutex.h>
Ingo Molnarb2d09102017-02-04 01:27:20 +010028#include <linux/rculist.h>
Thomas Graf7e1e7762014-08-02 11:47:44 +020029
Thomas Graff89bd6f2015-01-02 23:00:21 +010030/*
31 * The end of the chain is marked with a special nulls marks which has
32 * the following format:
33 *
34 * +-------+-----------------------------------------------------+-+
35 * | Base | Hash |1|
36 * +-------+-----------------------------------------------------+-+
37 *
38 * Base (4 bits) : Reserved to distinguish between multiple tables.
39 * Specified via &struct rhashtable_params.nulls_base.
40 * Hash (27 bits): Full hash (unmasked) of first element added to bucket
41 * 1 (1 bit) : Nulls marker (always set)
42 *
43 * The remaining bits of the next pointer remain unused for now.
44 */
45#define RHT_BASE_BITS 4
46#define RHT_HASH_BITS 27
47#define RHT_BASE_SHIFT RHT_HASH_BITS
48
Herbert Xu02fd97c2015-03-20 21:57:00 +110049/* Base bits plus 1 bit for nulls marker */
50#define RHT_HASH_RESERVED_SPACE (RHT_BASE_BITS + 1)
51
Florian Westphal5f8ddea2017-04-16 02:55:09 +020052/* Maximum chain length before rehash
53 *
54 * The maximum (not average) chain length grows with the size of the hash
55 * table, at a rate of (log N)/(log log N).
56 *
57 * The value of 16 is selected so that even if the hash table grew to
58 * 2^32 you would not expect the maximum chain length to exceed it
59 * unless we are under attack (or extremely unlucky).
60 *
61 * As this limit is only to detect attacks, we don't need to set it to a
62 * lower value as you'd need the chain length to vastly exceed 16 to have
63 * any real effect on the system.
64 */
65#define RHT_ELASTICITY 16u
66
Thomas Graf7e1e7762014-08-02 11:47:44 +020067struct rhash_head {
Thomas Graf5300fdc2014-08-13 16:38:29 +020068 struct rhash_head __rcu *next;
Thomas Graf7e1e7762014-08-02 11:47:44 +020069};
70
Herbert Xuca268932016-09-19 19:00:09 +080071struct rhlist_head {
72 struct rhash_head rhead;
73 struct rhlist_head __rcu *next;
74};
75
Thomas Graf97defe12015-01-02 23:00:20 +010076/**
77 * struct bucket_table - Table of hash buckets
78 * @size: Number of hash buckets
Herbert Xuda204202017-02-11 19:26:47 +080079 * @nest: Number of bits of first-level nested table.
Herbert Xu63d512d2015-03-14 13:57:24 +110080 * @rehash: Current bucket being rehashed
Herbert Xu988dfbd2015-03-10 09:27:55 +110081 * @hash_rnd: Random seed to fold into hash
Thomas Graf97defe12015-01-02 23:00:20 +010082 * @locks_mask: Mask to apply before accessing locks[]
83 * @locks: Array of spinlocks protecting individual buckets
Herbert Xueddee5ba2015-03-14 13:57:20 +110084 * @walkers: List of active walkers
Herbert Xu9d901bc2015-03-14 13:57:23 +110085 * @rcu: RCU structure for freeing the table
Herbert Xuc4db8842015-03-14 13:57:25 +110086 * @future_tbl: Table under construction during rehashing
Herbert Xuda204202017-02-11 19:26:47 +080087 * @ntbl: Nested table used when out of memory.
Thomas Graf97defe12015-01-02 23:00:20 +010088 * @buckets: size * hash buckets
89 */
Thomas Graf7e1e7762014-08-02 11:47:44 +020090struct bucket_table {
Herbert Xu63d512d2015-03-14 13:57:24 +110091 unsigned int size;
Herbert Xuda204202017-02-11 19:26:47 +080092 unsigned int nest;
Herbert Xu63d512d2015-03-14 13:57:24 +110093 unsigned int rehash;
Herbert Xu988dfbd2015-03-10 09:27:55 +110094 u32 hash_rnd;
Eric Dumazetb9ebafb2015-02-20 06:48:57 -080095 unsigned int locks_mask;
96 spinlock_t *locks;
Herbert Xueddee5ba2015-03-14 13:57:20 +110097 struct list_head walkers;
Herbert Xu9d901bc2015-03-14 13:57:23 +110098 struct rcu_head rcu;
Eric Dumazetb9ebafb2015-02-20 06:48:57 -080099
Herbert Xuc4db8842015-03-14 13:57:25 +1100100 struct bucket_table __rcu *future_tbl;
101
Herbert Xuda204202017-02-11 19:26:47 +0800102 struct rhash_head __rcu *buckets[] ____cacheline_aligned_in_smp;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200103};
104
Herbert Xu02fd97c2015-03-20 21:57:00 +1100105/**
106 * struct rhashtable_compare_arg - Key for the function rhashtable_compare
107 * @ht: Hash table
108 * @key: Key to compare against
109 */
110struct rhashtable_compare_arg {
111 struct rhashtable *ht;
112 const void *key;
113};
114
Thomas Graf7e1e7762014-08-02 11:47:44 +0200115typedef u32 (*rht_hashfn_t)(const void *data, u32 len, u32 seed);
Patrick McHardy49f7b332015-03-25 13:07:45 +0000116typedef u32 (*rht_obj_hashfn_t)(const void *data, u32 len, u32 seed);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100117typedef int (*rht_obj_cmpfn_t)(struct rhashtable_compare_arg *arg,
118 const void *obj);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200119
120struct rhashtable;
121
122/**
123 * struct rhashtable_params - Hash table construction parameters
124 * @nelem_hint: Hint on number of elements, should be 75% of desired size
125 * @key_len: Length of key
126 * @key_offset: Offset of key in struct to be hashed
127 * @head_offset: Offset of rhash_head in struct to be hashed
Herbert Xuc2e213c2015-03-18 20:01:16 +1100128 * @max_size: Maximum size while expanding
129 * @min_size: Minimum size while shrinking
Davidlohr Bueso895a6072017-09-08 16:15:45 -0700130 * @locks_mul: Number of bucket locks to allocate per cpu (default: 32)
Florian Westphal48e75b432017-05-01 22:18:01 +0200131 * @automatic_shrinking: Enable automatic shrinking of tables
132 * @nulls_base: Base value to generate nulls marker
Herbert Xu31ccde22015-03-24 00:50:21 +1100133 * @hashfn: Hash function (default: jhash2 if !(key_len % 4), or jhash)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200134 * @obj_hashfn: Function to hash object
Herbert Xu02fd97c2015-03-20 21:57:00 +1100135 * @obj_cmpfn: Function to compare key with object
Thomas Graf7e1e7762014-08-02 11:47:44 +0200136 */
137struct rhashtable_params {
Florian Westphal48e75b432017-05-01 22:18:01 +0200138 u16 nelem_hint;
139 u16 key_len;
140 u16 key_offset;
141 u16 head_offset;
Herbert Xuc2e213c2015-03-18 20:01:16 +1100142 unsigned int max_size;
Florian Westphal48e75b432017-05-01 22:18:01 +0200143 u16 min_size;
Thomas Grafb5e2c152015-03-24 20:42:19 +0000144 bool automatic_shrinking;
Florian Westphal48e75b432017-05-01 22:18:01 +0200145 u8 locks_mul;
146 u32 nulls_base;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200147 rht_hashfn_t hashfn;
148 rht_obj_hashfn_t obj_hashfn;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100149 rht_obj_cmpfn_t obj_cmpfn;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200150};
151
152/**
153 * struct rhashtable - Hash table handle
154 * @tbl: Bucket table
155 * @nelems: Number of elements in table
Herbert Xu31ccde22015-03-24 00:50:21 +1100156 * @key_len: Key length for hashfn
Thomas Graf7e1e7762014-08-02 11:47:44 +0200157 * @p: Configuration parameters
Herbert Xu6d684e52017-04-27 13:44:51 +0800158 * @max_elems: Maximum number of elements in table
Herbert Xuca268932016-09-19 19:00:09 +0800159 * @rhlist: True if this is an rhltable
Thomas Graf97defe12015-01-02 23:00:20 +0100160 * @run_work: Deferred worker to expand/shrink asynchronously
161 * @mutex: Mutex to protect current/future table swapping
Herbert Xuba7c95e2015-03-24 09:53:17 +1100162 * @lock: Spin lock to protect walker list
Thomas Graf7e1e7762014-08-02 11:47:44 +0200163 */
164struct rhashtable {
165 struct bucket_table __rcu *tbl;
Thomas Graf97defe12015-01-02 23:00:20 +0100166 atomic_t nelems;
Herbert Xu31ccde22015-03-24 00:50:21 +1100167 unsigned int key_len;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200168 struct rhashtable_params p;
Herbert Xu6d684e52017-04-27 13:44:51 +0800169 unsigned int max_elems;
Herbert Xuca268932016-09-19 19:00:09 +0800170 bool rhlist;
Ying Xue57699a42015-01-16 11:13:09 +0800171 struct work_struct run_work;
Thomas Graf97defe12015-01-02 23:00:20 +0100172 struct mutex mutex;
Herbert Xuba7c95e2015-03-24 09:53:17 +1100173 spinlock_t lock;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200174};
175
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100176/**
Herbert Xuca268932016-09-19 19:00:09 +0800177 * struct rhltable - Hash table with duplicate objects in a list
178 * @ht: Underlying rhtable
179 */
180struct rhltable {
181 struct rhashtable ht;
182};
183
184/**
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100185 * struct rhashtable_walker - Hash table walker
186 * @list: List entry on list of walkers
Herbert Xueddee5ba2015-03-14 13:57:20 +1100187 * @tbl: The table that we were walking over
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100188 */
189struct rhashtable_walker {
190 struct list_head list;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100191 struct bucket_table *tbl;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100192};
193
194/**
Herbert Xuca268932016-09-19 19:00:09 +0800195 * struct rhashtable_iter - Hash table iterator
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100196 * @ht: Table to iterate through
197 * @p: Current pointer
Herbert Xuca268932016-09-19 19:00:09 +0800198 * @list: Current hash list pointer
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100199 * @walker: Associated rhashtable walker
200 * @slot: Current slot
201 * @skip: Number of entries to skip in slot
202 */
203struct rhashtable_iter {
204 struct rhashtable *ht;
205 struct rhash_head *p;
Herbert Xuca268932016-09-19 19:00:09 +0800206 struct rhlist_head *list;
Herbert Xu246779d2016-08-18 16:50:56 +0800207 struct rhashtable_walker walker;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100208 unsigned int slot;
209 unsigned int skip;
210};
211
Thomas Graff89bd6f2015-01-02 23:00:21 +0100212static inline unsigned long rht_marker(const struct rhashtable *ht, u32 hash)
213{
214 return NULLS_MARKER(ht->p.nulls_base + hash);
215}
216
217#define INIT_RHT_NULLS_HEAD(ptr, ht, hash) \
218 ((ptr) = (typeof(ptr)) rht_marker(ht, hash))
219
220static inline bool rht_is_a_nulls(const struct rhash_head *ptr)
221{
222 return ((unsigned long) ptr & 1);
223}
224
225static inline unsigned long rht_get_nulls_value(const struct rhash_head *ptr)
226{
227 return ((unsigned long) ptr) >> 1;
228}
229
Herbert Xu02fd97c2015-03-20 21:57:00 +1100230static inline void *rht_obj(const struct rhashtable *ht,
231 const struct rhash_head *he)
232{
233 return (char *)he - ht->p.head_offset;
234}
235
236static inline unsigned int rht_bucket_index(const struct bucket_table *tbl,
237 unsigned int hash)
238{
239 return (hash >> RHT_HASH_RESERVED_SPACE) & (tbl->size - 1);
240}
241
242static inline unsigned int rht_key_hashfn(
243 struct rhashtable *ht, const struct bucket_table *tbl,
244 const void *key, const struct rhashtable_params params)
245{
Thomas Graf299e5c32015-03-24 14:18:17 +0100246 unsigned int hash;
Herbert Xude91b252015-03-24 00:50:20 +1100247
Herbert Xu31ccde22015-03-24 00:50:21 +1100248 /* params must be equal to ht->p if it isn't constant. */
249 if (!__builtin_constant_p(params.key_len))
250 hash = ht->p.hashfn(key, ht->key_len, tbl->hash_rnd);
251 else if (params.key_len) {
Thomas Graf299e5c32015-03-24 14:18:17 +0100252 unsigned int key_len = params.key_len;
Herbert Xu31ccde22015-03-24 00:50:21 +1100253
254 if (params.hashfn)
255 hash = params.hashfn(key, key_len, tbl->hash_rnd);
256 else if (key_len & (sizeof(u32) - 1))
257 hash = jhash(key, key_len, tbl->hash_rnd);
258 else
259 hash = jhash2(key, key_len / sizeof(u32),
260 tbl->hash_rnd);
261 } else {
Thomas Graf299e5c32015-03-24 14:18:17 +0100262 unsigned int key_len = ht->p.key_len;
Herbert Xu31ccde22015-03-24 00:50:21 +1100263
264 if (params.hashfn)
265 hash = params.hashfn(key, key_len, tbl->hash_rnd);
266 else
267 hash = jhash(key, key_len, tbl->hash_rnd);
268 }
269
270 return rht_bucket_index(tbl, hash);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100271}
272
273static inline unsigned int rht_head_hashfn(
274 struct rhashtable *ht, const struct bucket_table *tbl,
275 const struct rhash_head *he, const struct rhashtable_params params)
276{
277 const char *ptr = rht_obj(ht, he);
278
279 return likely(params.obj_hashfn) ?
Patrick McHardy49f7b332015-03-25 13:07:45 +0000280 rht_bucket_index(tbl, params.obj_hashfn(ptr, params.key_len ?:
281 ht->p.key_len,
282 tbl->hash_rnd)) :
Herbert Xu02fd97c2015-03-20 21:57:00 +1100283 rht_key_hashfn(ht, tbl, ptr + params.key_offset, params);
284}
285
286/**
287 * rht_grow_above_75 - returns true if nelems > 0.75 * table-size
288 * @ht: hash table
289 * @tbl: current table
290 */
291static inline bool rht_grow_above_75(const struct rhashtable *ht,
292 const struct bucket_table *tbl)
293{
294 /* Expand table when exceeding 75% load */
295 return atomic_read(&ht->nelems) > (tbl->size / 4 * 3) &&
296 (!ht->p.max_size || tbl->size < ht->p.max_size);
297}
298
299/**
300 * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size
301 * @ht: hash table
302 * @tbl: current table
303 */
304static inline bool rht_shrink_below_30(const struct rhashtable *ht,
305 const struct bucket_table *tbl)
306{
307 /* Shrink table beneath 30% load */
308 return atomic_read(&ht->nelems) < (tbl->size * 3 / 10) &&
309 tbl->size > ht->p.min_size;
310}
311
Herbert Xuccd57b12015-03-24 00:50:28 +1100312/**
313 * rht_grow_above_100 - returns true if nelems > table-size
314 * @ht: hash table
315 * @tbl: current table
316 */
317static inline bool rht_grow_above_100(const struct rhashtable *ht,
318 const struct bucket_table *tbl)
319{
Johannes Berg1d8dc3d2015-04-23 16:38:43 +0200320 return atomic_read(&ht->nelems) > tbl->size &&
321 (!ht->p.max_size || tbl->size < ht->p.max_size);
Herbert Xuccd57b12015-03-24 00:50:28 +1100322}
323
Herbert Xu07ee0722015-05-15 11:30:47 +0800324/**
325 * rht_grow_above_max - returns true if table is above maximum
326 * @ht: hash table
327 * @tbl: current table
328 */
329static inline bool rht_grow_above_max(const struct rhashtable *ht,
330 const struct bucket_table *tbl)
331{
Herbert Xu6d684e52017-04-27 13:44:51 +0800332 return atomic_read(&ht->nelems) >= ht->max_elems;
Herbert Xu07ee0722015-05-15 11:30:47 +0800333}
334
Herbert Xu02fd97c2015-03-20 21:57:00 +1100335/* The bucket lock is selected based on the hash and protects mutations
336 * on a group of hash buckets.
337 *
338 * A maximum of tbl->size/2 bucket locks is allocated. This ensures that
339 * a single lock always covers both buckets which may both contains
340 * entries which link to the same bucket of the old table during resizing.
341 * This allows to simplify the locking as locking the bucket in both
342 * tables during resize always guarantee protection.
343 *
344 * IMPORTANT: When holding the bucket lock of both the old and new table
345 * during expansions and shrinking, the old bucket lock must always be
346 * acquired first.
347 */
348static inline spinlock_t *rht_bucket_lock(const struct bucket_table *tbl,
349 unsigned int hash)
350{
351 return &tbl->locks[hash & tbl->locks_mask];
352}
353
Thomas Graf7e1e7762014-08-02 11:47:44 +0200354#ifdef CONFIG_PROVE_LOCKING
Thomas Graf97defe12015-01-02 23:00:20 +0100355int lockdep_rht_mutex_is_held(struct rhashtable *ht);
Thomas Graf88d6ed12015-01-02 23:00:16 +0100356int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200357#else
Thomas Graf97defe12015-01-02 23:00:20 +0100358static inline int lockdep_rht_mutex_is_held(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200359{
360 return 1;
361}
Thomas Graf88d6ed12015-01-02 23:00:16 +0100362
363static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl,
364 u32 hash)
365{
366 return 1;
367}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200368#endif /* CONFIG_PROVE_LOCKING */
369
Herbert Xu488fb86e2015-03-20 21:56:59 +1100370int rhashtable_init(struct rhashtable *ht,
371 const struct rhashtable_params *params);
Herbert Xuca268932016-09-19 19:00:09 +0800372int rhltable_init(struct rhltable *hlt,
373 const struct rhashtable_params *params);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200374
Herbert Xuca268932016-09-19 19:00:09 +0800375void *rhashtable_insert_slow(struct rhashtable *ht, const void *key,
376 struct rhash_head *obj);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200377
Herbert Xu246779d2016-08-18 16:50:56 +0800378void rhashtable_walk_enter(struct rhashtable *ht,
379 struct rhashtable_iter *iter);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100380void rhashtable_walk_exit(struct rhashtable_iter *iter);
Tom Herbert97a6ec42017-12-04 10:31:41 -0800381int rhashtable_walk_start_check(struct rhashtable_iter *iter) __acquires(RCU);
382
383static inline void rhashtable_walk_start(struct rhashtable_iter *iter)
384{
385 (void)rhashtable_walk_start_check(iter);
386}
387
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100388void *rhashtable_walk_next(struct rhashtable_iter *iter);
389void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU);
390
Thomas Graf6b6f3022015-03-24 14:18:20 +0100391void rhashtable_free_and_destroy(struct rhashtable *ht,
392 void (*free_fn)(void *ptr, void *arg),
393 void *arg);
Thomas Graf97defe12015-01-02 23:00:20 +0100394void rhashtable_destroy(struct rhashtable *ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200395
Herbert Xuda204202017-02-11 19:26:47 +0800396struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl,
397 unsigned int hash);
398struct rhash_head __rcu **rht_bucket_nested_insert(struct rhashtable *ht,
399 struct bucket_table *tbl,
400 unsigned int hash);
401
Thomas Graf7e1e7762014-08-02 11:47:44 +0200402#define rht_dereference(p, ht) \
403 rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht))
404
405#define rht_dereference_rcu(p, ht) \
406 rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht))
407
Thomas Graf88d6ed12015-01-02 23:00:16 +0100408#define rht_dereference_bucket(p, tbl, hash) \
409 rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200410
Thomas Graf88d6ed12015-01-02 23:00:16 +0100411#define rht_dereference_bucket_rcu(p, tbl, hash) \
412 rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash))
413
414#define rht_entry(tpos, pos, member) \
415 ({ tpos = container_of(pos, typeof(*tpos), member); 1; })
416
Herbert Xuda204202017-02-11 19:26:47 +0800417static inline struct rhash_head __rcu *const *rht_bucket(
418 const struct bucket_table *tbl, unsigned int hash)
419{
420 return unlikely(tbl->nest) ? rht_bucket_nested(tbl, hash) :
421 &tbl->buckets[hash];
422}
423
424static inline struct rhash_head __rcu **rht_bucket_var(
425 struct bucket_table *tbl, unsigned int hash)
426{
427 return unlikely(tbl->nest) ? rht_bucket_nested(tbl, hash) :
428 &tbl->buckets[hash];
429}
430
431static inline struct rhash_head __rcu **rht_bucket_insert(
432 struct rhashtable *ht, struct bucket_table *tbl, unsigned int hash)
433{
434 return unlikely(tbl->nest) ? rht_bucket_nested_insert(ht, tbl, hash) :
435 &tbl->buckets[hash];
436}
437
Thomas Graf88d6ed12015-01-02 23:00:16 +0100438/**
439 * rht_for_each_continue - continue iterating over hash chain
440 * @pos: the &struct rhash_head to use as a loop cursor.
441 * @head: the previous &struct rhash_head to continue from
442 * @tbl: the &struct bucket_table
443 * @hash: the hash value / bucket index
444 */
445#define rht_for_each_continue(pos, head, tbl, hash) \
446 for (pos = rht_dereference_bucket(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100447 !rht_is_a_nulls(pos); \
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 - iterate over hash chain
Thomas Graf88d6ed12015-01-02 23:00:16 +0100452 * @pos: the &struct rhash_head to use as a loop cursor.
453 * @tbl: the &struct bucket_table
454 * @hash: the hash value / bucket index
Thomas Graf7e1e7762014-08-02 11:47:44 +0200455 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100456#define rht_for_each(pos, tbl, hash) \
Herbert Xuda204202017-02-11 19:26:47 +0800457 rht_for_each_continue(pos, *rht_bucket(tbl, hash), tbl, hash)
Thomas Graf88d6ed12015-01-02 23:00:16 +0100458
459/**
460 * rht_for_each_entry_continue - continue iterating over hash chain
461 * @tpos: the type * to use as a loop cursor.
462 * @pos: the &struct rhash_head to use as a loop cursor.
463 * @head: the previous &struct rhash_head to continue from
464 * @tbl: the &struct bucket_table
465 * @hash: the hash value / bucket index
466 * @member: name of the &struct rhash_head within the hashable struct.
467 */
468#define rht_for_each_entry_continue(tpos, pos, head, tbl, hash, member) \
469 for (pos = rht_dereference_bucket(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100470 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100471 pos = rht_dereference_bucket((pos)->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200472
473/**
474 * rht_for_each_entry - iterate over hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100475 * @tpos: the type * to use as a loop cursor.
476 * @pos: the &struct rhash_head to use as a loop cursor.
477 * @tbl: the &struct bucket_table
478 * @hash: the hash value / bucket index
479 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200480 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100481#define rht_for_each_entry(tpos, pos, tbl, hash, member) \
Herbert Xuda204202017-02-11 19:26:47 +0800482 rht_for_each_entry_continue(tpos, pos, *rht_bucket(tbl, hash), \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100483 tbl, hash, member)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200484
485/**
486 * rht_for_each_entry_safe - safely iterate over hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100487 * @tpos: the type * to use as a loop cursor.
488 * @pos: the &struct rhash_head to use as a loop cursor.
489 * @next: the &struct rhash_head to use as next in loop cursor.
490 * @tbl: the &struct bucket_table
491 * @hash: the hash value / bucket index
492 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200493 *
494 * This hash chain list-traversal primitive allows for the looped code to
495 * remove the loop cursor from the list.
496 */
Herbert Xuda204202017-02-11 19:26:47 +0800497#define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \
498 for (pos = rht_dereference_bucket(*rht_bucket(tbl, hash), tbl, hash), \
499 next = !rht_is_a_nulls(pos) ? \
500 rht_dereference_bucket(pos->next, tbl, hash) : NULL; \
501 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
502 pos = next, \
503 next = !rht_is_a_nulls(pos) ? \
Patrick McHardy607954b2015-01-21 11:12:13 +0000504 rht_dereference_bucket(pos->next, tbl, hash) : NULL)
Thomas Graf88d6ed12015-01-02 23:00:16 +0100505
506/**
507 * rht_for_each_rcu_continue - continue iterating over rcu hash chain
508 * @pos: the &struct rhash_head to use as a loop cursor.
509 * @head: the previous &struct rhash_head to continue from
510 * @tbl: the &struct bucket_table
511 * @hash: the hash value / bucket index
512 *
513 * This hash chain list-traversal primitive may safely run concurrently with
514 * the _rcu mutation primitives such as rhashtable_insert() as long as the
515 * traversal is guarded by rcu_read_lock().
516 */
517#define rht_for_each_rcu_continue(pos, head, tbl, hash) \
518 for (({barrier(); }), \
519 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100520 !rht_is_a_nulls(pos); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100521 pos = rcu_dereference_raw(pos->next))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200522
523/**
524 * rht_for_each_rcu - iterate over rcu hash chain
Thomas Graf88d6ed12015-01-02 23:00:16 +0100525 * @pos: the &struct rhash_head to use as a loop cursor.
526 * @tbl: the &struct bucket_table
527 * @hash: the hash value / bucket index
Thomas Graf7e1e7762014-08-02 11:47:44 +0200528 *
529 * This hash chain list-traversal primitive may safely run concurrently with
Thomas Graf88d6ed12015-01-02 23:00:16 +0100530 * the _rcu mutation primitives such as rhashtable_insert() as long as the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200531 * traversal is guarded by rcu_read_lock().
532 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100533#define rht_for_each_rcu(pos, tbl, hash) \
Herbert Xuda204202017-02-11 19:26:47 +0800534 rht_for_each_rcu_continue(pos, *rht_bucket(tbl, hash), tbl, hash)
Thomas Graf88d6ed12015-01-02 23:00:16 +0100535
536/**
537 * rht_for_each_entry_rcu_continue - continue iterating over rcu hash chain
538 * @tpos: the type * to use as a loop cursor.
539 * @pos: the &struct rhash_head to use as a loop cursor.
540 * @head: the previous &struct rhash_head to continue from
541 * @tbl: the &struct bucket_table
542 * @hash: the hash value / bucket index
543 * @member: name of the &struct rhash_head within the hashable struct.
544 *
545 * This hash chain list-traversal primitive may safely run concurrently with
546 * the _rcu mutation primitives such as rhashtable_insert() as long as the
547 * traversal is guarded by rcu_read_lock().
548 */
549#define rht_for_each_entry_rcu_continue(tpos, pos, head, tbl, hash, member) \
550 for (({barrier(); }), \
551 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100552 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100553 pos = rht_dereference_bucket_rcu(pos->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200554
555/**
556 * rht_for_each_entry_rcu - iterate over rcu hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100557 * @tpos: the type * to use as a loop cursor.
558 * @pos: the &struct rhash_head to use as a loop cursor.
559 * @tbl: the &struct bucket_table
560 * @hash: the hash value / bucket index
561 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200562 *
563 * This hash chain list-traversal primitive may safely run concurrently with
Thomas Graf88d6ed12015-01-02 23:00:16 +0100564 * the _rcu mutation primitives such as rhashtable_insert() as long as the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200565 * traversal is guarded by rcu_read_lock().
566 */
Herbert Xuda204202017-02-11 19:26:47 +0800567#define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \
568 rht_for_each_entry_rcu_continue(tpos, pos, *rht_bucket(tbl, hash), \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100569 tbl, hash, member)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200570
Herbert Xuca268932016-09-19 19:00:09 +0800571/**
572 * rhl_for_each_rcu - iterate over rcu hash table list
573 * @pos: the &struct rlist_head to use as a loop cursor.
574 * @list: the head of the list
575 *
576 * This hash chain list-traversal primitive should be used on the
577 * list returned by rhltable_lookup.
578 */
579#define rhl_for_each_rcu(pos, list) \
580 for (pos = list; pos; pos = rcu_dereference_raw(pos->next))
581
582/**
583 * rhl_for_each_entry_rcu - iterate over rcu hash table list of given type
584 * @tpos: the type * to use as a loop cursor.
585 * @pos: the &struct rlist_head to use as a loop cursor.
586 * @list: the head of the list
587 * @member: name of the &struct rlist_head within the hashable struct.
588 *
589 * This hash chain list-traversal primitive should be used on the
590 * list returned by rhltable_lookup.
591 */
592#define rhl_for_each_entry_rcu(tpos, pos, list, member) \
593 for (pos = list; pos && rht_entry(tpos, pos, member); \
594 pos = rcu_dereference_raw(pos->next))
595
Herbert Xu02fd97c2015-03-20 21:57:00 +1100596static inline int rhashtable_compare(struct rhashtable_compare_arg *arg,
597 const void *obj)
598{
599 struct rhashtable *ht = arg->ht;
600 const char *ptr = obj;
601
602 return memcmp(ptr + ht->p.key_offset, arg->key, ht->p.key_len);
603}
604
Herbert Xuca268932016-09-19 19:00:09 +0800605/* Internal function, do not use. */
606static inline struct rhash_head *__rhashtable_lookup(
Herbert Xu02fd97c2015-03-20 21:57:00 +1100607 struct rhashtable *ht, const void *key,
608 const struct rhashtable_params params)
609{
610 struct rhashtable_compare_arg arg = {
611 .ht = ht,
612 .key = key,
613 };
Herbert Xuda204202017-02-11 19:26:47 +0800614 struct bucket_table *tbl;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100615 struct rhash_head *he;
Thomas Graf299e5c32015-03-24 14:18:17 +0100616 unsigned int hash;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100617
Herbert Xu02fd97c2015-03-20 21:57:00 +1100618 tbl = rht_dereference_rcu(ht->tbl, ht);
619restart:
620 hash = rht_key_hashfn(ht, tbl, key, params);
621 rht_for_each_rcu(he, tbl, hash) {
622 if (params.obj_cmpfn ?
623 params.obj_cmpfn(&arg, rht_obj(ht, he)) :
624 rhashtable_compare(&arg, rht_obj(ht, he)))
625 continue;
Herbert Xuca268932016-09-19 19:00:09 +0800626 return he;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100627 }
628
629 /* Ensure we see any new tables. */
630 smp_rmb();
631
632 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
633 if (unlikely(tbl))
634 goto restart;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100635
636 return NULL;
637}
638
Herbert Xuca268932016-09-19 19:00:09 +0800639/**
640 * rhashtable_lookup - search hash table
641 * @ht: hash table
642 * @key: the pointer to the key
643 * @params: hash table parameters
644 *
645 * Computes the hash value for the key and traverses the bucket chain looking
646 * for a entry with an identical key. The first matching entry is returned.
647 *
648 * This must only be called under the RCU read lock.
649 *
650 * Returns the first entry on which the compare function returned true.
651 */
652static inline void *rhashtable_lookup(
653 struct rhashtable *ht, const void *key,
654 const struct rhashtable_params params)
655{
656 struct rhash_head *he = __rhashtable_lookup(ht, key, params);
657
658 return he ? rht_obj(ht, he) : NULL;
659}
660
661/**
662 * rhashtable_lookup_fast - search hash table, without RCU read lock
663 * @ht: hash table
664 * @key: the pointer to the key
665 * @params: hash table parameters
666 *
667 * Computes the hash value for the key and traverses the bucket chain looking
668 * for a entry with an identical key. The first matching entry is returned.
669 *
670 * Only use this function when you have other mechanisms guaranteeing
671 * that the object won't go away after the RCU read lock is released.
672 *
673 * Returns the first entry on which the compare function returned true.
674 */
675static inline void *rhashtable_lookup_fast(
676 struct rhashtable *ht, const void *key,
677 const struct rhashtable_params params)
678{
679 void *obj;
680
681 rcu_read_lock();
682 obj = rhashtable_lookup(ht, key, params);
683 rcu_read_unlock();
684
685 return obj;
686}
687
688/**
689 * rhltable_lookup - search hash list table
690 * @hlt: hash table
691 * @key: the pointer to the key
692 * @params: hash table parameters
693 *
694 * Computes the hash value for the key and traverses the bucket chain looking
695 * for a entry with an identical key. All matching entries are returned
696 * in a list.
697 *
698 * This must only be called under the RCU read lock.
699 *
700 * Returns the list of entries that match the given key.
701 */
702static inline struct rhlist_head *rhltable_lookup(
703 struct rhltable *hlt, const void *key,
704 const struct rhashtable_params params)
705{
706 struct rhash_head *he = __rhashtable_lookup(&hlt->ht, key, params);
707
708 return he ? container_of(he, struct rhlist_head, rhead) : NULL;
709}
710
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200711/* Internal function, please use rhashtable_insert_fast() instead. This
712 * function returns the existing element already in hashes in there is a clash,
713 * otherwise it returns an error via ERR_PTR().
714 */
715static inline void *__rhashtable_insert_fast(
Herbert Xu02fd97c2015-03-20 21:57:00 +1100716 struct rhashtable *ht, const void *key, struct rhash_head *obj,
Herbert Xuca268932016-09-19 19:00:09 +0800717 const struct rhashtable_params params, bool rhlist)
Herbert Xu02fd97c2015-03-20 21:57:00 +1100718{
719 struct rhashtable_compare_arg arg = {
720 .ht = ht,
721 .key = key,
722 };
Herbert Xuca268932016-09-19 19:00:09 +0800723 struct rhash_head __rcu **pprev;
724 struct bucket_table *tbl;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100725 struct rhash_head *head;
726 spinlock_t *lock;
Thomas Graf299e5c32015-03-24 14:18:17 +0100727 unsigned int hash;
Herbert Xuca268932016-09-19 19:00:09 +0800728 int elasticity;
729 void *data;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100730
731 rcu_read_lock();
732
733 tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xuca268932016-09-19 19:00:09 +0800734 hash = rht_head_hashfn(ht, tbl, obj, params);
735 lock = rht_bucket_lock(tbl, hash);
736 spin_lock_bh(lock);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100737
Herbert Xuca268932016-09-19 19:00:09 +0800738 if (unlikely(rht_dereference_bucket(tbl->future_tbl, tbl, hash))) {
739slow_path:
Herbert Xub8244782015-03-24 00:50:26 +1100740 spin_unlock_bh(lock);
Herbert Xuca268932016-09-19 19:00:09 +0800741 rcu_read_unlock();
742 return rhashtable_insert_slow(ht, key, obj);
Herbert Xub8244782015-03-24 00:50:26 +1100743 }
744
Florian Westphal5f8ddea2017-04-16 02:55:09 +0200745 elasticity = RHT_ELASTICITY;
Herbert Xuda204202017-02-11 19:26:47 +0800746 pprev = rht_bucket_insert(ht, tbl, hash);
747 data = ERR_PTR(-ENOMEM);
748 if (!pprev)
749 goto out;
750
751 rht_for_each_continue(head, *pprev, tbl, hash) {
Herbert Xuca268932016-09-19 19:00:09 +0800752 struct rhlist_head *plist;
753 struct rhlist_head *list;
Herbert Xu3cf92222015-12-03 20:41:29 +0800754
Herbert Xuca268932016-09-19 19:00:09 +0800755 elasticity--;
756 if (!key ||
757 (params.obj_cmpfn ?
758 params.obj_cmpfn(&arg, rht_obj(ht, head)) :
759 rhashtable_compare(&arg, rht_obj(ht, head))))
760 continue;
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200761
Herbert Xuca268932016-09-19 19:00:09 +0800762 data = rht_obj(ht, head);
763
764 if (!rhlist)
765 goto out;
766
767
768 list = container_of(obj, struct rhlist_head, rhead);
769 plist = container_of(head, struct rhlist_head, rhead);
770
771 RCU_INIT_POINTER(list->next, plist);
772 head = rht_dereference_bucket(head->next, tbl, hash);
773 RCU_INIT_POINTER(list->rhead.next, head);
774 rcu_assign_pointer(*pprev, obj);
775
776 goto good;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100777 }
778
Herbert Xuca268932016-09-19 19:00:09 +0800779 if (elasticity <= 0)
780 goto slow_path;
781
782 data = ERR_PTR(-E2BIG);
Herbert Xu07ee0722015-05-15 11:30:47 +0800783 if (unlikely(rht_grow_above_max(ht, tbl)))
784 goto out;
785
Herbert Xuca268932016-09-19 19:00:09 +0800786 if (unlikely(rht_grow_above_100(ht, tbl)))
787 goto slow_path;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100788
Herbert Xuda204202017-02-11 19:26:47 +0800789 head = rht_dereference_bucket(*pprev, tbl, hash);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100790
791 RCU_INIT_POINTER(obj->next, head);
Herbert Xuca268932016-09-19 19:00:09 +0800792 if (rhlist) {
793 struct rhlist_head *list;
794
795 list = container_of(obj, struct rhlist_head, rhead);
796 RCU_INIT_POINTER(list->next, NULL);
797 }
Herbert Xu02fd97c2015-03-20 21:57:00 +1100798
Herbert Xuda204202017-02-11 19:26:47 +0800799 rcu_assign_pointer(*pprev, obj);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100800
801 atomic_inc(&ht->nelems);
802 if (rht_grow_above_75(ht, tbl))
803 schedule_work(&ht->run_work);
804
Herbert Xuca268932016-09-19 19:00:09 +0800805good:
806 data = NULL;
807
Herbert Xu02fd97c2015-03-20 21:57:00 +1100808out:
809 spin_unlock_bh(lock);
810 rcu_read_unlock();
811
Herbert Xuca268932016-09-19 19:00:09 +0800812 return data;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100813}
814
815/**
816 * rhashtable_insert_fast - insert object into hash table
817 * @ht: hash table
818 * @obj: pointer to hash head inside object
819 * @params: hash table parameters
820 *
821 * Will take a per bucket spinlock to protect against mutual mutations
822 * on the same bucket. Multiple insertions may occur in parallel unless
823 * they map to the same bucket lock.
824 *
825 * It is safe to call this function from atomic context.
826 *
827 * Will trigger an automatic deferred table resizing if the size grows
828 * beyond the watermark indicated by grow_decision() which can be passed
829 * to rhashtable_init().
830 */
831static inline int rhashtable_insert_fast(
832 struct rhashtable *ht, struct rhash_head *obj,
833 const struct rhashtable_params params)
834{
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200835 void *ret;
836
Herbert Xuca268932016-09-19 19:00:09 +0800837 ret = __rhashtable_insert_fast(ht, NULL, obj, params, false);
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200838 if (IS_ERR(ret))
839 return PTR_ERR(ret);
840
841 return ret == NULL ? 0 : -EEXIST;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100842}
843
844/**
Herbert Xuca268932016-09-19 19:00:09 +0800845 * rhltable_insert_key - insert object into hash list table
846 * @hlt: hash list table
847 * @key: the pointer to the key
848 * @list: pointer to hash list head inside object
849 * @params: hash table parameters
850 *
851 * Will take a per bucket spinlock to protect against mutual mutations
852 * on the same bucket. Multiple insertions may occur in parallel unless
853 * they map to the same bucket lock.
854 *
855 * It is safe to call this function from atomic context.
856 *
857 * Will trigger an automatic deferred table resizing if the size grows
858 * beyond the watermark indicated by grow_decision() which can be passed
859 * to rhashtable_init().
860 */
861static inline int rhltable_insert_key(
862 struct rhltable *hlt, const void *key, struct rhlist_head *list,
863 const struct rhashtable_params params)
864{
865 return PTR_ERR(__rhashtable_insert_fast(&hlt->ht, key, &list->rhead,
866 params, true));
867}
868
869/**
870 * rhltable_insert - insert object into hash list table
871 * @hlt: hash list table
872 * @list: pointer to hash list head inside object
873 * @params: hash table parameters
874 *
875 * Will take a per bucket spinlock to protect against mutual mutations
876 * on the same bucket. Multiple insertions may occur in parallel unless
877 * they map to the same bucket lock.
878 *
879 * It is safe to call this function from atomic context.
880 *
881 * Will trigger an automatic deferred table resizing if the size grows
882 * beyond the watermark indicated by grow_decision() which can be passed
883 * to rhashtable_init().
884 */
885static inline int rhltable_insert(
886 struct rhltable *hlt, struct rhlist_head *list,
887 const struct rhashtable_params params)
888{
889 const char *key = rht_obj(&hlt->ht, &list->rhead);
890
891 key += params.key_offset;
892
893 return rhltable_insert_key(hlt, key, list, params);
894}
895
896/**
Herbert Xu02fd97c2015-03-20 21:57:00 +1100897 * rhashtable_lookup_insert_fast - lookup and insert object into hash table
898 * @ht: hash table
899 * @obj: pointer to hash head inside object
900 * @params: hash table parameters
901 *
902 * Locks down the bucket chain in both the old and new table if a resize
903 * is in progress to ensure that writers can't remove from the old table
904 * and can't insert to the new table during the atomic operation of search
905 * and insertion. Searches for duplicates in both the old and new table if
906 * a resize is in progress.
907 *
908 * This lookup function may only be used for fixed key hash table (key_len
909 * parameter set). It will BUG() if used inappropriately.
910 *
911 * It is safe to call this function from atomic context.
912 *
913 * Will trigger an automatic deferred table resizing if the size grows
914 * beyond the watermark indicated by grow_decision() which can be passed
915 * to rhashtable_init().
916 */
917static inline int rhashtable_lookup_insert_fast(
918 struct rhashtable *ht, struct rhash_head *obj,
919 const struct rhashtable_params params)
920{
921 const char *key = rht_obj(ht, obj);
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200922 void *ret;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100923
924 BUG_ON(ht->p.obj_hashfn);
925
Herbert Xuca268932016-09-19 19:00:09 +0800926 ret = __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params,
927 false);
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200928 if (IS_ERR(ret))
929 return PTR_ERR(ret);
930
931 return ret == NULL ? 0 : -EEXIST;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100932}
933
934/**
Andreas Gruenbacherf9fe1c12017-03-18 00:36:15 +0100935 * rhashtable_lookup_get_insert_fast - lookup and insert object into hash table
936 * @ht: hash table
937 * @obj: pointer to hash head inside object
938 * @params: hash table parameters
939 *
940 * Just like rhashtable_lookup_insert_fast(), but this function returns the
941 * object if it exists, NULL if it did not and the insertion was successful,
942 * and an ERR_PTR otherwise.
943 */
944static inline void *rhashtable_lookup_get_insert_fast(
945 struct rhashtable *ht, struct rhash_head *obj,
946 const struct rhashtable_params params)
947{
948 const char *key = rht_obj(ht, obj);
949
950 BUG_ON(ht->p.obj_hashfn);
951
952 return __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params,
953 false);
954}
955
956/**
Herbert Xu02fd97c2015-03-20 21:57:00 +1100957 * rhashtable_lookup_insert_key - search and insert object to hash table
958 * with explicit key
959 * @ht: hash table
960 * @key: key
961 * @obj: pointer to hash head inside object
962 * @params: hash table parameters
963 *
964 * Locks down the bucket chain in both the old and new table if a resize
965 * is in progress to ensure that writers can't remove from the old table
966 * and can't insert to the new table during the atomic operation of search
967 * and insertion. Searches for duplicates in both the old and new table if
968 * a resize is in progress.
969 *
970 * Lookups may occur in parallel with hashtable mutations and resizing.
971 *
972 * Will trigger an automatic deferred table resizing if the size grows
973 * beyond the watermark indicated by grow_decision() which can be passed
974 * to rhashtable_init().
975 *
976 * Returns zero on success.
977 */
978static inline int rhashtable_lookup_insert_key(
979 struct rhashtable *ht, const void *key, struct rhash_head *obj,
980 const struct rhashtable_params params)
981{
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200982 void *ret;
983
984 BUG_ON(!ht->p.obj_hashfn || !key);
985
Herbert Xuca268932016-09-19 19:00:09 +0800986 ret = __rhashtable_insert_fast(ht, key, obj, params, false);
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200987 if (IS_ERR(ret))
988 return PTR_ERR(ret);
989
990 return ret == NULL ? 0 : -EEXIST;
991}
992
993/**
994 * rhashtable_lookup_get_insert_key - lookup and insert object into hash table
995 * @ht: hash table
996 * @obj: pointer to hash head inside object
997 * @params: hash table parameters
998 * @data: pointer to element data already in hashes
999 *
1000 * Just like rhashtable_lookup_insert_key(), but this function returns the
1001 * object if it exists, NULL if it does not and the insertion was successful,
1002 * and an ERR_PTR otherwise.
1003 */
1004static inline void *rhashtable_lookup_get_insert_key(
1005 struct rhashtable *ht, const void *key, struct rhash_head *obj,
1006 const struct rhashtable_params params)
1007{
Herbert Xu02fd97c2015-03-20 21:57:00 +11001008 BUG_ON(!ht->p.obj_hashfn || !key);
1009
Herbert Xuca268932016-09-19 19:00:09 +08001010 return __rhashtable_insert_fast(ht, key, obj, params, false);
Herbert Xu02fd97c2015-03-20 21:57:00 +11001011}
1012
Thomas Grafac833bd2015-03-24 14:18:18 +01001013/* Internal function, please use rhashtable_remove_fast() instead */
Herbert Xuca268932016-09-19 19:00:09 +08001014static inline int __rhashtable_remove_fast_one(
Herbert Xu02fd97c2015-03-20 21:57:00 +11001015 struct rhashtable *ht, struct bucket_table *tbl,
Herbert Xuca268932016-09-19 19:00:09 +08001016 struct rhash_head *obj, const struct rhashtable_params params,
1017 bool rhlist)
Herbert Xu02fd97c2015-03-20 21:57:00 +11001018{
1019 struct rhash_head __rcu **pprev;
1020 struct rhash_head *he;
1021 spinlock_t * lock;
Thomas Graf299e5c32015-03-24 14:18:17 +01001022 unsigned int hash;
Herbert Xu02fd97c2015-03-20 21:57:00 +11001023 int err = -ENOENT;
1024
1025 hash = rht_head_hashfn(ht, tbl, obj, params);
1026 lock = rht_bucket_lock(tbl, hash);
1027
1028 spin_lock_bh(lock);
1029
Herbert Xuda204202017-02-11 19:26:47 +08001030 pprev = rht_bucket_var(tbl, hash);
1031 rht_for_each_continue(he, *pprev, tbl, hash) {
Herbert Xuca268932016-09-19 19:00:09 +08001032 struct rhlist_head *list;
1033
1034 list = container_of(he, struct rhlist_head, rhead);
1035
Herbert Xu02fd97c2015-03-20 21:57:00 +11001036 if (he != obj) {
Herbert Xuca268932016-09-19 19:00:09 +08001037 struct rhlist_head __rcu **lpprev;
1038
Herbert Xu02fd97c2015-03-20 21:57:00 +11001039 pprev = &he->next;
Herbert Xuca268932016-09-19 19:00:09 +08001040
1041 if (!rhlist)
1042 continue;
1043
1044 do {
1045 lpprev = &list->next;
1046 list = rht_dereference_bucket(list->next,
1047 tbl, hash);
1048 } while (list && obj != &list->rhead);
1049
1050 if (!list)
1051 continue;
1052
1053 list = rht_dereference_bucket(list->next, tbl, hash);
1054 RCU_INIT_POINTER(*lpprev, list);
1055 err = 0;
1056 break;
Herbert Xu02fd97c2015-03-20 21:57:00 +11001057 }
1058
Herbert Xuca268932016-09-19 19:00:09 +08001059 obj = rht_dereference_bucket(obj->next, tbl, hash);
1060 err = 1;
1061
1062 if (rhlist) {
1063 list = rht_dereference_bucket(list->next, tbl, hash);
1064 if (list) {
1065 RCU_INIT_POINTER(list->rhead.next, obj);
1066 obj = &list->rhead;
1067 err = 0;
1068 }
1069 }
1070
1071 rcu_assign_pointer(*pprev, obj);
Herbert Xu02fd97c2015-03-20 21:57:00 +11001072 break;
1073 }
1074
1075 spin_unlock_bh(lock);
1076
Herbert Xuca268932016-09-19 19:00:09 +08001077 if (err > 0) {
1078 atomic_dec(&ht->nelems);
1079 if (unlikely(ht->p.automatic_shrinking &&
1080 rht_shrink_below_30(ht, tbl)))
1081 schedule_work(&ht->run_work);
1082 err = 0;
1083 }
1084
1085 return err;
1086}
1087
1088/* Internal function, please use rhashtable_remove_fast() instead */
1089static inline int __rhashtable_remove_fast(
1090 struct rhashtable *ht, struct rhash_head *obj,
1091 const struct rhashtable_params params, bool rhlist)
1092{
1093 struct bucket_table *tbl;
1094 int err;
1095
1096 rcu_read_lock();
1097
1098 tbl = rht_dereference_rcu(ht->tbl, ht);
1099
1100 /* Because we have already taken (and released) the bucket
1101 * lock in old_tbl, if we find that future_tbl is not yet
1102 * visible then that guarantees the entry to still be in
1103 * the old tbl if it exists.
1104 */
1105 while ((err = __rhashtable_remove_fast_one(ht, tbl, obj, params,
1106 rhlist)) &&
1107 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
1108 ;
1109
1110 rcu_read_unlock();
1111
Herbert Xu02fd97c2015-03-20 21:57:00 +11001112 return err;
1113}
1114
1115/**
1116 * rhashtable_remove_fast - remove object from hash table
1117 * @ht: hash table
1118 * @obj: pointer to hash head inside object
1119 * @params: hash table parameters
1120 *
1121 * Since the hash chain is single linked, the removal operation needs to
1122 * walk the bucket chain upon removal. The removal operation is thus
1123 * considerable slow if the hash table is not correctly sized.
1124 *
1125 * Will automatically shrink the table via rhashtable_expand() if the
1126 * shrink_decision function specified at rhashtable_init() returns true.
1127 *
1128 * Returns zero on success, -ENOENT if the entry could not be found.
1129 */
1130static inline int rhashtable_remove_fast(
1131 struct rhashtable *ht, struct rhash_head *obj,
1132 const struct rhashtable_params params)
1133{
Herbert Xuca268932016-09-19 19:00:09 +08001134 return __rhashtable_remove_fast(ht, obj, params, false);
1135}
Herbert Xu02fd97c2015-03-20 21:57:00 +11001136
Herbert Xuca268932016-09-19 19:00:09 +08001137/**
1138 * rhltable_remove - remove object from hash list table
1139 * @hlt: hash list table
1140 * @list: pointer to hash list head inside object
1141 * @params: hash table parameters
1142 *
1143 * Since the hash chain is single linked, the removal operation needs to
1144 * walk the bucket chain upon removal. The removal operation is thus
1145 * considerable slow if the hash table is not correctly sized.
1146 *
1147 * Will automatically shrink the table via rhashtable_expand() if the
1148 * shrink_decision function specified at rhashtable_init() returns true.
1149 *
1150 * Returns zero on success, -ENOENT if the entry could not be found.
1151 */
1152static inline int rhltable_remove(
1153 struct rhltable *hlt, struct rhlist_head *list,
1154 const struct rhashtable_params params)
1155{
1156 return __rhashtable_remove_fast(&hlt->ht, &list->rhead, params, true);
Herbert Xu02fd97c2015-03-20 21:57:00 +11001157}
1158
Tom Herbert3502cad2015-12-15 15:41:36 -08001159/* Internal function, please use rhashtable_replace_fast() instead */
1160static inline int __rhashtable_replace_fast(
1161 struct rhashtable *ht, struct bucket_table *tbl,
1162 struct rhash_head *obj_old, struct rhash_head *obj_new,
1163 const struct rhashtable_params params)
1164{
1165 struct rhash_head __rcu **pprev;
1166 struct rhash_head *he;
1167 spinlock_t *lock;
1168 unsigned int hash;
1169 int err = -ENOENT;
1170
1171 /* Minimally, the old and new objects must have same hash
1172 * (which should mean identifiers are the same).
1173 */
1174 hash = rht_head_hashfn(ht, tbl, obj_old, params);
1175 if (hash != rht_head_hashfn(ht, tbl, obj_new, params))
1176 return -EINVAL;
1177
1178 lock = rht_bucket_lock(tbl, hash);
1179
1180 spin_lock_bh(lock);
1181
Herbert Xuda204202017-02-11 19:26:47 +08001182 pprev = rht_bucket_var(tbl, hash);
1183 rht_for_each_continue(he, *pprev, tbl, hash) {
Tom Herbert3502cad2015-12-15 15:41:36 -08001184 if (he != obj_old) {
1185 pprev = &he->next;
1186 continue;
1187 }
1188
1189 rcu_assign_pointer(obj_new->next, obj_old->next);
1190 rcu_assign_pointer(*pprev, obj_new);
1191 err = 0;
1192 break;
1193 }
1194
1195 spin_unlock_bh(lock);
1196
1197 return err;
1198}
1199
1200/**
1201 * rhashtable_replace_fast - replace an object in hash table
1202 * @ht: hash table
1203 * @obj_old: pointer to hash head inside object being replaced
1204 * @obj_new: pointer to hash head inside object which is new
1205 * @params: hash table parameters
1206 *
1207 * Replacing an object doesn't affect the number of elements in the hash table
1208 * or bucket, so we don't need to worry about shrinking or expanding the
1209 * table here.
1210 *
1211 * Returns zero on success, -ENOENT if the entry could not be found,
1212 * -EINVAL if hash is not the same for the old and new objects.
1213 */
1214static inline int rhashtable_replace_fast(
1215 struct rhashtable *ht, struct rhash_head *obj_old,
1216 struct rhash_head *obj_new,
1217 const struct rhashtable_params params)
1218{
1219 struct bucket_table *tbl;
1220 int err;
1221
1222 rcu_read_lock();
1223
1224 tbl = rht_dereference_rcu(ht->tbl, ht);
1225
1226 /* Because we have already taken (and released) the bucket
1227 * lock in old_tbl, if we find that future_tbl is not yet
1228 * visible then that guarantees the entry to still be in
1229 * the old tbl if it exists.
1230 */
1231 while ((err = __rhashtable_replace_fast(ht, tbl, obj_old,
1232 obj_new, params)) &&
1233 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
1234 ;
1235
1236 rcu_read_unlock();
1237
1238 return err;
1239}
1240
Herbert Xu246779d2016-08-18 16:50:56 +08001241/* Obsolete function, do not use in new code. */
1242static inline int rhashtable_walk_init(struct rhashtable *ht,
1243 struct rhashtable_iter *iter, gfp_t gfp)
1244{
1245 rhashtable_walk_enter(ht, iter);
1246 return 0;
1247}
1248
Herbert Xuca268932016-09-19 19:00:09 +08001249/**
1250 * rhltable_walk_enter - Initialise an iterator
1251 * @hlt: Table to walk over
1252 * @iter: Hash table Iterator
1253 *
1254 * This function prepares a hash table walk.
1255 *
1256 * Note that if you restart a walk after rhashtable_walk_stop you
1257 * may see the same object twice. Also, you may miss objects if
1258 * there are removals in between rhashtable_walk_stop and the next
1259 * call to rhashtable_walk_start.
1260 *
1261 * For a completely stable walk you should construct your own data
1262 * structure outside the hash table.
1263 *
1264 * This function may sleep so you must not call it from interrupt
1265 * context or with spin locks held.
1266 *
1267 * You must call rhashtable_walk_exit after this function returns.
1268 */
1269static inline void rhltable_walk_enter(struct rhltable *hlt,
1270 struct rhashtable_iter *iter)
1271{
1272 return rhashtable_walk_enter(&hlt->ht, iter);
1273}
1274
1275/**
1276 * rhltable_free_and_destroy - free elements and destroy hash list table
1277 * @hlt: the hash list table to destroy
1278 * @free_fn: callback to release resources of element
1279 * @arg: pointer passed to free_fn
1280 *
1281 * See documentation for rhashtable_free_and_destroy.
1282 */
1283static inline void rhltable_free_and_destroy(struct rhltable *hlt,
1284 void (*free_fn)(void *ptr,
1285 void *arg),
1286 void *arg)
1287{
1288 return rhashtable_free_and_destroy(&hlt->ht, free_fn, arg);
1289}
1290
1291static inline void rhltable_destroy(struct rhltable *hlt)
1292{
1293 return rhltable_free_and_destroy(hlt, NULL, NULL);
1294}
1295
Thomas Graf7e1e7762014-08-02 11:47:44 +02001296#endif /* _LINUX_RHASHTABLE_H */