blob: 542b1b265ac4f9de7be11f84deaeacc971fcc7d9 [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;
Tom Herbert2db54b42017-12-04 10:31:42 -0800210 bool end_of_table;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100211};
212
Thomas Graff89bd6f2015-01-02 23:00:21 +0100213static inline unsigned long rht_marker(const struct rhashtable *ht, u32 hash)
214{
215 return NULLS_MARKER(ht->p.nulls_base + hash);
216}
217
218#define INIT_RHT_NULLS_HEAD(ptr, ht, hash) \
219 ((ptr) = (typeof(ptr)) rht_marker(ht, hash))
220
221static inline bool rht_is_a_nulls(const struct rhash_head *ptr)
222{
223 return ((unsigned long) ptr & 1);
224}
225
226static inline unsigned long rht_get_nulls_value(const struct rhash_head *ptr)
227{
228 return ((unsigned long) ptr) >> 1;
229}
230
Herbert Xu02fd97c2015-03-20 21:57:00 +1100231static inline void *rht_obj(const struct rhashtable *ht,
232 const struct rhash_head *he)
233{
234 return (char *)he - ht->p.head_offset;
235}
236
237static inline unsigned int rht_bucket_index(const struct bucket_table *tbl,
238 unsigned int hash)
239{
240 return (hash >> RHT_HASH_RESERVED_SPACE) & (tbl->size - 1);
241}
242
243static inline unsigned int rht_key_hashfn(
244 struct rhashtable *ht, const struct bucket_table *tbl,
245 const void *key, const struct rhashtable_params params)
246{
Thomas Graf299e5c32015-03-24 14:18:17 +0100247 unsigned int hash;
Herbert Xude91b252015-03-24 00:50:20 +1100248
Herbert Xu31ccde22015-03-24 00:50:21 +1100249 /* params must be equal to ht->p if it isn't constant. */
250 if (!__builtin_constant_p(params.key_len))
251 hash = ht->p.hashfn(key, ht->key_len, tbl->hash_rnd);
252 else if (params.key_len) {
Thomas Graf299e5c32015-03-24 14:18:17 +0100253 unsigned int key_len = params.key_len;
Herbert Xu31ccde22015-03-24 00:50:21 +1100254
255 if (params.hashfn)
256 hash = params.hashfn(key, key_len, tbl->hash_rnd);
257 else if (key_len & (sizeof(u32) - 1))
258 hash = jhash(key, key_len, tbl->hash_rnd);
259 else
260 hash = jhash2(key, key_len / sizeof(u32),
261 tbl->hash_rnd);
262 } else {
Thomas Graf299e5c32015-03-24 14:18:17 +0100263 unsigned int key_len = ht->p.key_len;
Herbert Xu31ccde22015-03-24 00:50:21 +1100264
265 if (params.hashfn)
266 hash = params.hashfn(key, key_len, tbl->hash_rnd);
267 else
268 hash = jhash(key, key_len, tbl->hash_rnd);
269 }
270
271 return rht_bucket_index(tbl, hash);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100272}
273
274static inline unsigned int rht_head_hashfn(
275 struct rhashtable *ht, const struct bucket_table *tbl,
276 const struct rhash_head *he, const struct rhashtable_params params)
277{
278 const char *ptr = rht_obj(ht, he);
279
280 return likely(params.obj_hashfn) ?
Patrick McHardy49f7b332015-03-25 13:07:45 +0000281 rht_bucket_index(tbl, params.obj_hashfn(ptr, params.key_len ?:
282 ht->p.key_len,
283 tbl->hash_rnd)) :
Herbert Xu02fd97c2015-03-20 21:57:00 +1100284 rht_key_hashfn(ht, tbl, ptr + params.key_offset, params);
285}
286
287/**
288 * rht_grow_above_75 - returns true if nelems > 0.75 * table-size
289 * @ht: hash table
290 * @tbl: current table
291 */
292static inline bool rht_grow_above_75(const struct rhashtable *ht,
293 const struct bucket_table *tbl)
294{
295 /* Expand table when exceeding 75% load */
296 return atomic_read(&ht->nelems) > (tbl->size / 4 * 3) &&
297 (!ht->p.max_size || tbl->size < ht->p.max_size);
298}
299
300/**
301 * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size
302 * @ht: hash table
303 * @tbl: current table
304 */
305static inline bool rht_shrink_below_30(const struct rhashtable *ht,
306 const struct bucket_table *tbl)
307{
308 /* Shrink table beneath 30% load */
309 return atomic_read(&ht->nelems) < (tbl->size * 3 / 10) &&
310 tbl->size > ht->p.min_size;
311}
312
Herbert Xuccd57b12015-03-24 00:50:28 +1100313/**
314 * rht_grow_above_100 - returns true if nelems > table-size
315 * @ht: hash table
316 * @tbl: current table
317 */
318static inline bool rht_grow_above_100(const struct rhashtable *ht,
319 const struct bucket_table *tbl)
320{
Johannes Berg1d8dc3d2015-04-23 16:38:43 +0200321 return atomic_read(&ht->nelems) > tbl->size &&
322 (!ht->p.max_size || tbl->size < ht->p.max_size);
Herbert Xuccd57b12015-03-24 00:50:28 +1100323}
324
Herbert Xu07ee0722015-05-15 11:30:47 +0800325/**
326 * rht_grow_above_max - returns true if table is above maximum
327 * @ht: hash table
328 * @tbl: current table
329 */
330static inline bool rht_grow_above_max(const struct rhashtable *ht,
331 const struct bucket_table *tbl)
332{
Herbert Xu6d684e52017-04-27 13:44:51 +0800333 return atomic_read(&ht->nelems) >= ht->max_elems;
Herbert Xu07ee0722015-05-15 11:30:47 +0800334}
335
Herbert Xu02fd97c2015-03-20 21:57:00 +1100336/* The bucket lock is selected based on the hash and protects mutations
337 * on a group of hash buckets.
338 *
339 * A maximum of tbl->size/2 bucket locks is allocated. This ensures that
340 * a single lock always covers both buckets which may both contains
341 * entries which link to the same bucket of the old table during resizing.
342 * This allows to simplify the locking as locking the bucket in both
343 * tables during resize always guarantee protection.
344 *
345 * IMPORTANT: When holding the bucket lock of both the old and new table
346 * during expansions and shrinking, the old bucket lock must always be
347 * acquired first.
348 */
349static inline spinlock_t *rht_bucket_lock(const struct bucket_table *tbl,
350 unsigned int hash)
351{
352 return &tbl->locks[hash & tbl->locks_mask];
353}
354
Thomas Graf7e1e7762014-08-02 11:47:44 +0200355#ifdef CONFIG_PROVE_LOCKING
Thomas Graf97defe12015-01-02 23:00:20 +0100356int lockdep_rht_mutex_is_held(struct rhashtable *ht);
Thomas Graf88d6ed12015-01-02 23:00:16 +0100357int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200358#else
Thomas Graf97defe12015-01-02 23:00:20 +0100359static inline int lockdep_rht_mutex_is_held(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200360{
361 return 1;
362}
Thomas Graf88d6ed12015-01-02 23:00:16 +0100363
364static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl,
365 u32 hash)
366{
367 return 1;
368}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200369#endif /* CONFIG_PROVE_LOCKING */
370
Herbert Xu488fb86e2015-03-20 21:56:59 +1100371int rhashtable_init(struct rhashtable *ht,
372 const struct rhashtable_params *params);
Herbert Xuca268932016-09-19 19:00:09 +0800373int rhltable_init(struct rhltable *hlt,
374 const struct rhashtable_params *params);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200375
Herbert Xuca268932016-09-19 19:00:09 +0800376void *rhashtable_insert_slow(struct rhashtable *ht, const void *key,
377 struct rhash_head *obj);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200378
Herbert Xu246779d2016-08-18 16:50:56 +0800379void rhashtable_walk_enter(struct rhashtable *ht,
380 struct rhashtable_iter *iter);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100381void rhashtable_walk_exit(struct rhashtable_iter *iter);
Tom Herbert97a6ec42017-12-04 10:31:41 -0800382int rhashtable_walk_start_check(struct rhashtable_iter *iter) __acquires(RCU);
383
384static inline void rhashtable_walk_start(struct rhashtable_iter *iter)
385{
386 (void)rhashtable_walk_start_check(iter);
387}
388
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100389void *rhashtable_walk_next(struct rhashtable_iter *iter);
Tom Herbert2db54b42017-12-04 10:31:42 -0800390void *rhashtable_walk_peek(struct rhashtable_iter *iter);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100391void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU);
392
Thomas Graf6b6f3022015-03-24 14:18:20 +0100393void rhashtable_free_and_destroy(struct rhashtable *ht,
394 void (*free_fn)(void *ptr, void *arg),
395 void *arg);
Thomas Graf97defe12015-01-02 23:00:20 +0100396void rhashtable_destroy(struct rhashtable *ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200397
Herbert Xuda204202017-02-11 19:26:47 +0800398struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl,
399 unsigned int hash);
400struct rhash_head __rcu **rht_bucket_nested_insert(struct rhashtable *ht,
401 struct bucket_table *tbl,
402 unsigned int hash);
403
Thomas Graf7e1e7762014-08-02 11:47:44 +0200404#define rht_dereference(p, ht) \
405 rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht))
406
407#define rht_dereference_rcu(p, ht) \
408 rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht))
409
Thomas Graf88d6ed12015-01-02 23:00:16 +0100410#define rht_dereference_bucket(p, tbl, hash) \
411 rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200412
Thomas Graf88d6ed12015-01-02 23:00:16 +0100413#define rht_dereference_bucket_rcu(p, tbl, hash) \
414 rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash))
415
416#define rht_entry(tpos, pos, member) \
417 ({ tpos = container_of(pos, typeof(*tpos), member); 1; })
418
Herbert Xuda204202017-02-11 19:26:47 +0800419static inline struct rhash_head __rcu *const *rht_bucket(
420 const struct bucket_table *tbl, unsigned int hash)
421{
422 return unlikely(tbl->nest) ? rht_bucket_nested(tbl, hash) :
423 &tbl->buckets[hash];
424}
425
426static inline struct rhash_head __rcu **rht_bucket_var(
427 struct bucket_table *tbl, unsigned int hash)
428{
429 return unlikely(tbl->nest) ? rht_bucket_nested(tbl, hash) :
430 &tbl->buckets[hash];
431}
432
433static inline struct rhash_head __rcu **rht_bucket_insert(
434 struct rhashtable *ht, struct bucket_table *tbl, unsigned int hash)
435{
436 return unlikely(tbl->nest) ? rht_bucket_nested_insert(ht, tbl, hash) :
437 &tbl->buckets[hash];
438}
439
Thomas Graf88d6ed12015-01-02 23:00:16 +0100440/**
441 * rht_for_each_continue - continue iterating over hash chain
442 * @pos: the &struct rhash_head to use as a loop cursor.
443 * @head: the previous &struct rhash_head to continue from
444 * @tbl: the &struct bucket_table
445 * @hash: the hash value / bucket index
446 */
447#define rht_for_each_continue(pos, head, tbl, hash) \
448 for (pos = rht_dereference_bucket(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100449 !rht_is_a_nulls(pos); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100450 pos = rht_dereference_bucket((pos)->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200451
452/**
453 * rht_for_each - iterate over hash chain
Thomas Graf88d6ed12015-01-02 23:00:16 +0100454 * @pos: the &struct rhash_head to use as a loop cursor.
455 * @tbl: the &struct bucket_table
456 * @hash: the hash value / bucket index
Thomas Graf7e1e7762014-08-02 11:47:44 +0200457 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100458#define rht_for_each(pos, tbl, hash) \
Herbert Xuda204202017-02-11 19:26:47 +0800459 rht_for_each_continue(pos, *rht_bucket(tbl, hash), tbl, hash)
Thomas Graf88d6ed12015-01-02 23:00:16 +0100460
461/**
462 * rht_for_each_entry_continue - continue iterating over hash chain
463 * @tpos: the type * to use as a loop cursor.
464 * @pos: the &struct rhash_head to use as a loop cursor.
465 * @head: the previous &struct rhash_head to continue from
466 * @tbl: the &struct bucket_table
467 * @hash: the hash value / bucket index
468 * @member: name of the &struct rhash_head within the hashable struct.
469 */
470#define rht_for_each_entry_continue(tpos, pos, head, tbl, hash, member) \
471 for (pos = rht_dereference_bucket(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100472 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100473 pos = rht_dereference_bucket((pos)->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200474
475/**
476 * rht_for_each_entry - iterate over hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100477 * @tpos: the type * to use as a loop cursor.
478 * @pos: the &struct rhash_head to use as a loop cursor.
479 * @tbl: the &struct bucket_table
480 * @hash: the hash value / bucket index
481 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200482 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100483#define rht_for_each_entry(tpos, pos, tbl, hash, member) \
Herbert Xuda204202017-02-11 19:26:47 +0800484 rht_for_each_entry_continue(tpos, pos, *rht_bucket(tbl, hash), \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100485 tbl, hash, member)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200486
487/**
488 * rht_for_each_entry_safe - safely iterate over hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100489 * @tpos: the type * to use as a loop cursor.
490 * @pos: the &struct rhash_head to use as a loop cursor.
491 * @next: the &struct rhash_head to use as next in loop cursor.
492 * @tbl: the &struct bucket_table
493 * @hash: the hash value / bucket index
494 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200495 *
496 * This hash chain list-traversal primitive allows for the looped code to
497 * remove the loop cursor from the list.
498 */
Herbert Xuda204202017-02-11 19:26:47 +0800499#define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \
500 for (pos = rht_dereference_bucket(*rht_bucket(tbl, hash), tbl, hash), \
501 next = !rht_is_a_nulls(pos) ? \
502 rht_dereference_bucket(pos->next, tbl, hash) : NULL; \
503 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
504 pos = next, \
505 next = !rht_is_a_nulls(pos) ? \
Patrick McHardy607954b2015-01-21 11:12:13 +0000506 rht_dereference_bucket(pos->next, tbl, hash) : NULL)
Thomas Graf88d6ed12015-01-02 23:00:16 +0100507
508/**
509 * rht_for_each_rcu_continue - continue iterating over rcu hash chain
510 * @pos: the &struct rhash_head to use as a loop cursor.
511 * @head: the previous &struct rhash_head to continue from
512 * @tbl: the &struct bucket_table
513 * @hash: the hash value / bucket index
514 *
515 * This hash chain list-traversal primitive may safely run concurrently with
516 * the _rcu mutation primitives such as rhashtable_insert() as long as the
517 * traversal is guarded by rcu_read_lock().
518 */
519#define rht_for_each_rcu_continue(pos, head, tbl, hash) \
520 for (({barrier(); }), \
521 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100522 !rht_is_a_nulls(pos); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100523 pos = rcu_dereference_raw(pos->next))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200524
525/**
526 * rht_for_each_rcu - iterate over rcu hash chain
Thomas Graf88d6ed12015-01-02 23:00:16 +0100527 * @pos: the &struct rhash_head to use as a loop cursor.
528 * @tbl: the &struct bucket_table
529 * @hash: the hash value / bucket index
Thomas Graf7e1e7762014-08-02 11:47:44 +0200530 *
531 * This hash chain list-traversal primitive may safely run concurrently with
Thomas Graf88d6ed12015-01-02 23:00:16 +0100532 * the _rcu mutation primitives such as rhashtable_insert() as long as the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200533 * traversal is guarded by rcu_read_lock().
534 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100535#define rht_for_each_rcu(pos, tbl, hash) \
Herbert Xuda204202017-02-11 19:26:47 +0800536 rht_for_each_rcu_continue(pos, *rht_bucket(tbl, hash), tbl, hash)
Thomas Graf88d6ed12015-01-02 23:00:16 +0100537
538/**
539 * rht_for_each_entry_rcu_continue - continue iterating over rcu hash chain
540 * @tpos: the type * to use as a loop cursor.
541 * @pos: the &struct rhash_head to use as a loop cursor.
542 * @head: the previous &struct rhash_head to continue from
543 * @tbl: the &struct bucket_table
544 * @hash: the hash value / bucket index
545 * @member: name of the &struct rhash_head within the hashable struct.
546 *
547 * This hash chain list-traversal primitive may safely run concurrently with
548 * the _rcu mutation primitives such as rhashtable_insert() as long as the
549 * traversal is guarded by rcu_read_lock().
550 */
551#define rht_for_each_entry_rcu_continue(tpos, pos, head, tbl, hash, member) \
552 for (({barrier(); }), \
553 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100554 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100555 pos = rht_dereference_bucket_rcu(pos->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200556
557/**
558 * rht_for_each_entry_rcu - iterate over rcu hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100559 * @tpos: the type * to use as a loop cursor.
560 * @pos: the &struct rhash_head to use as a loop cursor.
561 * @tbl: the &struct bucket_table
562 * @hash: the hash value / bucket index
563 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200564 *
565 * This hash chain list-traversal primitive may safely run concurrently with
Thomas Graf88d6ed12015-01-02 23:00:16 +0100566 * the _rcu mutation primitives such as rhashtable_insert() as long as the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200567 * traversal is guarded by rcu_read_lock().
568 */
Herbert Xuda204202017-02-11 19:26:47 +0800569#define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \
570 rht_for_each_entry_rcu_continue(tpos, pos, *rht_bucket(tbl, hash), \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100571 tbl, hash, member)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200572
Herbert Xuca268932016-09-19 19:00:09 +0800573/**
574 * rhl_for_each_rcu - iterate over rcu hash table list
575 * @pos: the &struct rlist_head to use as a loop cursor.
576 * @list: the head of the list
577 *
578 * This hash chain list-traversal primitive should be used on the
579 * list returned by rhltable_lookup.
580 */
581#define rhl_for_each_rcu(pos, list) \
582 for (pos = list; pos; pos = rcu_dereference_raw(pos->next))
583
584/**
585 * rhl_for_each_entry_rcu - iterate over rcu hash table list of given type
586 * @tpos: the type * to use as a loop cursor.
587 * @pos: the &struct rlist_head to use as a loop cursor.
588 * @list: the head of the list
589 * @member: name of the &struct rlist_head within the hashable struct.
590 *
591 * This hash chain list-traversal primitive should be used on the
592 * list returned by rhltable_lookup.
593 */
594#define rhl_for_each_entry_rcu(tpos, pos, list, member) \
595 for (pos = list; pos && rht_entry(tpos, pos, member); \
596 pos = rcu_dereference_raw(pos->next))
597
Herbert Xu02fd97c2015-03-20 21:57:00 +1100598static inline int rhashtable_compare(struct rhashtable_compare_arg *arg,
599 const void *obj)
600{
601 struct rhashtable *ht = arg->ht;
602 const char *ptr = obj;
603
604 return memcmp(ptr + ht->p.key_offset, arg->key, ht->p.key_len);
605}
606
Herbert Xuca268932016-09-19 19:00:09 +0800607/* Internal function, do not use. */
608static inline struct rhash_head *__rhashtable_lookup(
Herbert Xu02fd97c2015-03-20 21:57:00 +1100609 struct rhashtable *ht, const void *key,
610 const struct rhashtable_params params)
611{
612 struct rhashtable_compare_arg arg = {
613 .ht = ht,
614 .key = key,
615 };
Herbert Xuda204202017-02-11 19:26:47 +0800616 struct bucket_table *tbl;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100617 struct rhash_head *he;
Thomas Graf299e5c32015-03-24 14:18:17 +0100618 unsigned int hash;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100619
Herbert Xu02fd97c2015-03-20 21:57:00 +1100620 tbl = rht_dereference_rcu(ht->tbl, ht);
621restart:
622 hash = rht_key_hashfn(ht, tbl, key, params);
623 rht_for_each_rcu(he, tbl, hash) {
624 if (params.obj_cmpfn ?
625 params.obj_cmpfn(&arg, rht_obj(ht, he)) :
626 rhashtable_compare(&arg, rht_obj(ht, he)))
627 continue;
Herbert Xuca268932016-09-19 19:00:09 +0800628 return he;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100629 }
630
631 /* Ensure we see any new tables. */
632 smp_rmb();
633
634 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
635 if (unlikely(tbl))
636 goto restart;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100637
638 return NULL;
639}
640
Herbert Xuca268932016-09-19 19:00:09 +0800641/**
642 * rhashtable_lookup - search hash table
643 * @ht: hash table
644 * @key: the pointer to the key
645 * @params: hash table parameters
646 *
647 * Computes the hash value for the key and traverses the bucket chain looking
648 * for a entry with an identical key. The first matching entry is returned.
649 *
650 * This must only be called under the RCU read lock.
651 *
652 * Returns the first entry on which the compare function returned true.
653 */
654static inline void *rhashtable_lookup(
655 struct rhashtable *ht, const void *key,
656 const struct rhashtable_params params)
657{
658 struct rhash_head *he = __rhashtable_lookup(ht, key, params);
659
660 return he ? rht_obj(ht, he) : NULL;
661}
662
663/**
664 * rhashtable_lookup_fast - search hash table, without RCU read lock
665 * @ht: hash table
666 * @key: the pointer to the key
667 * @params: hash table parameters
668 *
669 * Computes the hash value for the key and traverses the bucket chain looking
670 * for a entry with an identical key. The first matching entry is returned.
671 *
672 * Only use this function when you have other mechanisms guaranteeing
673 * that the object won't go away after the RCU read lock is released.
674 *
675 * Returns the first entry on which the compare function returned true.
676 */
677static inline void *rhashtable_lookup_fast(
678 struct rhashtable *ht, const void *key,
679 const struct rhashtable_params params)
680{
681 void *obj;
682
683 rcu_read_lock();
684 obj = rhashtable_lookup(ht, key, params);
685 rcu_read_unlock();
686
687 return obj;
688}
689
690/**
691 * rhltable_lookup - search hash list table
692 * @hlt: hash table
693 * @key: the pointer to the key
694 * @params: hash table parameters
695 *
696 * Computes the hash value for the key and traverses the bucket chain looking
697 * for a entry with an identical key. All matching entries are returned
698 * in a list.
699 *
700 * This must only be called under the RCU read lock.
701 *
702 * Returns the list of entries that match the given key.
703 */
704static inline struct rhlist_head *rhltable_lookup(
705 struct rhltable *hlt, const void *key,
706 const struct rhashtable_params params)
707{
708 struct rhash_head *he = __rhashtable_lookup(&hlt->ht, key, params);
709
710 return he ? container_of(he, struct rhlist_head, rhead) : NULL;
711}
712
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200713/* Internal function, please use rhashtable_insert_fast() instead. This
714 * function returns the existing element already in hashes in there is a clash,
715 * otherwise it returns an error via ERR_PTR().
716 */
717static inline void *__rhashtable_insert_fast(
Herbert Xu02fd97c2015-03-20 21:57:00 +1100718 struct rhashtable *ht, const void *key, struct rhash_head *obj,
Herbert Xuca268932016-09-19 19:00:09 +0800719 const struct rhashtable_params params, bool rhlist)
Herbert Xu02fd97c2015-03-20 21:57:00 +1100720{
721 struct rhashtable_compare_arg arg = {
722 .ht = ht,
723 .key = key,
724 };
Herbert Xuca268932016-09-19 19:00:09 +0800725 struct rhash_head __rcu **pprev;
726 struct bucket_table *tbl;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100727 struct rhash_head *head;
728 spinlock_t *lock;
Thomas Graf299e5c32015-03-24 14:18:17 +0100729 unsigned int hash;
Herbert Xuca268932016-09-19 19:00:09 +0800730 int elasticity;
731 void *data;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100732
733 rcu_read_lock();
734
735 tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xuca268932016-09-19 19:00:09 +0800736 hash = rht_head_hashfn(ht, tbl, obj, params);
737 lock = rht_bucket_lock(tbl, hash);
738 spin_lock_bh(lock);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100739
Herbert Xuca268932016-09-19 19:00:09 +0800740 if (unlikely(rht_dereference_bucket(tbl->future_tbl, tbl, hash))) {
741slow_path:
Herbert Xub8244782015-03-24 00:50:26 +1100742 spin_unlock_bh(lock);
Herbert Xuca268932016-09-19 19:00:09 +0800743 rcu_read_unlock();
744 return rhashtable_insert_slow(ht, key, obj);
Herbert Xub8244782015-03-24 00:50:26 +1100745 }
746
Florian Westphal5f8ddea2017-04-16 02:55:09 +0200747 elasticity = RHT_ELASTICITY;
Herbert Xuda204202017-02-11 19:26:47 +0800748 pprev = rht_bucket_insert(ht, tbl, hash);
749 data = ERR_PTR(-ENOMEM);
750 if (!pprev)
751 goto out;
752
753 rht_for_each_continue(head, *pprev, tbl, hash) {
Herbert Xuca268932016-09-19 19:00:09 +0800754 struct rhlist_head *plist;
755 struct rhlist_head *list;
Herbert Xu3cf92222015-12-03 20:41:29 +0800756
Herbert Xuca268932016-09-19 19:00:09 +0800757 elasticity--;
758 if (!key ||
759 (params.obj_cmpfn ?
760 params.obj_cmpfn(&arg, rht_obj(ht, head)) :
761 rhashtable_compare(&arg, rht_obj(ht, head))))
762 continue;
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200763
Herbert Xuca268932016-09-19 19:00:09 +0800764 data = rht_obj(ht, head);
765
766 if (!rhlist)
767 goto out;
768
769
770 list = container_of(obj, struct rhlist_head, rhead);
771 plist = container_of(head, struct rhlist_head, rhead);
772
773 RCU_INIT_POINTER(list->next, plist);
774 head = rht_dereference_bucket(head->next, tbl, hash);
775 RCU_INIT_POINTER(list->rhead.next, head);
776 rcu_assign_pointer(*pprev, obj);
777
778 goto good;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100779 }
780
Herbert Xuca268932016-09-19 19:00:09 +0800781 if (elasticity <= 0)
782 goto slow_path;
783
784 data = ERR_PTR(-E2BIG);
Herbert Xu07ee0722015-05-15 11:30:47 +0800785 if (unlikely(rht_grow_above_max(ht, tbl)))
786 goto out;
787
Herbert Xuca268932016-09-19 19:00:09 +0800788 if (unlikely(rht_grow_above_100(ht, tbl)))
789 goto slow_path;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100790
Herbert Xuda204202017-02-11 19:26:47 +0800791 head = rht_dereference_bucket(*pprev, tbl, hash);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100792
793 RCU_INIT_POINTER(obj->next, head);
Herbert Xuca268932016-09-19 19:00:09 +0800794 if (rhlist) {
795 struct rhlist_head *list;
796
797 list = container_of(obj, struct rhlist_head, rhead);
798 RCU_INIT_POINTER(list->next, NULL);
799 }
Herbert Xu02fd97c2015-03-20 21:57:00 +1100800
Herbert Xuda204202017-02-11 19:26:47 +0800801 rcu_assign_pointer(*pprev, obj);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100802
803 atomic_inc(&ht->nelems);
804 if (rht_grow_above_75(ht, tbl))
805 schedule_work(&ht->run_work);
806
Herbert Xuca268932016-09-19 19:00:09 +0800807good:
808 data = NULL;
809
Herbert Xu02fd97c2015-03-20 21:57:00 +1100810out:
811 spin_unlock_bh(lock);
812 rcu_read_unlock();
813
Herbert Xuca268932016-09-19 19:00:09 +0800814 return data;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100815}
816
817/**
818 * rhashtable_insert_fast - insert object into hash table
819 * @ht: hash table
820 * @obj: pointer to hash head inside object
821 * @params: hash table parameters
822 *
823 * Will take a per bucket spinlock to protect against mutual mutations
824 * on the same bucket. Multiple insertions may occur in parallel unless
825 * they map to the same bucket lock.
826 *
827 * It is safe to call this function from atomic context.
828 *
829 * Will trigger an automatic deferred table resizing if the size grows
830 * beyond the watermark indicated by grow_decision() which can be passed
831 * to rhashtable_init().
832 */
833static inline int rhashtable_insert_fast(
834 struct rhashtable *ht, struct rhash_head *obj,
835 const struct rhashtable_params params)
836{
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200837 void *ret;
838
Herbert Xuca268932016-09-19 19:00:09 +0800839 ret = __rhashtable_insert_fast(ht, NULL, obj, params, false);
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200840 if (IS_ERR(ret))
841 return PTR_ERR(ret);
842
843 return ret == NULL ? 0 : -EEXIST;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100844}
845
846/**
Herbert Xuca268932016-09-19 19:00:09 +0800847 * rhltable_insert_key - insert object into hash list table
848 * @hlt: hash list table
849 * @key: the pointer to the key
850 * @list: pointer to hash list head inside object
851 * @params: hash table parameters
852 *
853 * Will take a per bucket spinlock to protect against mutual mutations
854 * on the same bucket. Multiple insertions may occur in parallel unless
855 * they map to the same bucket lock.
856 *
857 * It is safe to call this function from atomic context.
858 *
859 * Will trigger an automatic deferred table resizing if the size grows
860 * beyond the watermark indicated by grow_decision() which can be passed
861 * to rhashtable_init().
862 */
863static inline int rhltable_insert_key(
864 struct rhltable *hlt, const void *key, struct rhlist_head *list,
865 const struct rhashtable_params params)
866{
867 return PTR_ERR(__rhashtable_insert_fast(&hlt->ht, key, &list->rhead,
868 params, true));
869}
870
871/**
872 * rhltable_insert - insert object into hash list table
873 * @hlt: hash list table
874 * @list: pointer to hash list head inside object
875 * @params: hash table parameters
876 *
877 * Will take a per bucket spinlock to protect against mutual mutations
878 * on the same bucket. Multiple insertions may occur in parallel unless
879 * they map to the same bucket lock.
880 *
881 * It is safe to call this function from atomic context.
882 *
883 * Will trigger an automatic deferred table resizing if the size grows
884 * beyond the watermark indicated by grow_decision() which can be passed
885 * to rhashtable_init().
886 */
887static inline int rhltable_insert(
888 struct rhltable *hlt, struct rhlist_head *list,
889 const struct rhashtable_params params)
890{
891 const char *key = rht_obj(&hlt->ht, &list->rhead);
892
893 key += params.key_offset;
894
895 return rhltable_insert_key(hlt, key, list, params);
896}
897
898/**
Herbert Xu02fd97c2015-03-20 21:57:00 +1100899 * rhashtable_lookup_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 * Locks down the bucket chain in both the old and new table if a resize
905 * is in progress to ensure that writers can't remove from the old table
906 * and can't insert to the new table during the atomic operation of search
907 * and insertion. Searches for duplicates in both the old and new table if
908 * a resize is in progress.
909 *
910 * This lookup function may only be used for fixed key hash table (key_len
911 * parameter set). It will BUG() if used inappropriately.
912 *
913 * It is safe to call this function from atomic context.
914 *
915 * Will trigger an automatic deferred table resizing if the size grows
916 * beyond the watermark indicated by grow_decision() which can be passed
917 * to rhashtable_init().
918 */
919static inline int rhashtable_lookup_insert_fast(
920 struct rhashtable *ht, struct rhash_head *obj,
921 const struct rhashtable_params params)
922{
923 const char *key = rht_obj(ht, obj);
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200924 void *ret;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100925
926 BUG_ON(ht->p.obj_hashfn);
927
Herbert Xuca268932016-09-19 19:00:09 +0800928 ret = __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params,
929 false);
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200930 if (IS_ERR(ret))
931 return PTR_ERR(ret);
932
933 return ret == NULL ? 0 : -EEXIST;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100934}
935
936/**
Andreas Gruenbacherf9fe1c12017-03-18 00:36:15 +0100937 * rhashtable_lookup_get_insert_fast - lookup and insert object into hash table
938 * @ht: hash table
939 * @obj: pointer to hash head inside object
940 * @params: hash table parameters
941 *
942 * Just like rhashtable_lookup_insert_fast(), but this function returns the
943 * object if it exists, NULL if it did not and the insertion was successful,
944 * and an ERR_PTR otherwise.
945 */
946static inline void *rhashtable_lookup_get_insert_fast(
947 struct rhashtable *ht, struct rhash_head *obj,
948 const struct rhashtable_params params)
949{
950 const char *key = rht_obj(ht, obj);
951
952 BUG_ON(ht->p.obj_hashfn);
953
954 return __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params,
955 false);
956}
957
958/**
Herbert Xu02fd97c2015-03-20 21:57:00 +1100959 * rhashtable_lookup_insert_key - search and insert object to hash table
960 * with explicit key
961 * @ht: hash table
962 * @key: key
963 * @obj: pointer to hash head inside object
964 * @params: hash table parameters
965 *
966 * Locks down the bucket chain in both the old and new table if a resize
967 * is in progress to ensure that writers can't remove from the old table
968 * and can't insert to the new table during the atomic operation of search
969 * and insertion. Searches for duplicates in both the old and new table if
970 * a resize is in progress.
971 *
972 * Lookups may occur in parallel with hashtable mutations and resizing.
973 *
974 * Will trigger an automatic deferred table resizing if the size grows
975 * beyond the watermark indicated by grow_decision() which can be passed
976 * to rhashtable_init().
977 *
978 * Returns zero on success.
979 */
980static inline int rhashtable_lookup_insert_key(
981 struct rhashtable *ht, const void *key, struct rhash_head *obj,
982 const struct rhashtable_params params)
983{
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200984 void *ret;
985
986 BUG_ON(!ht->p.obj_hashfn || !key);
987
Herbert Xuca268932016-09-19 19:00:09 +0800988 ret = __rhashtable_insert_fast(ht, key, obj, params, false);
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200989 if (IS_ERR(ret))
990 return PTR_ERR(ret);
991
992 return ret == NULL ? 0 : -EEXIST;
993}
994
995/**
996 * rhashtable_lookup_get_insert_key - lookup and insert object into hash table
997 * @ht: hash table
998 * @obj: pointer to hash head inside object
999 * @params: hash table parameters
1000 * @data: pointer to element data already in hashes
1001 *
1002 * Just like rhashtable_lookup_insert_key(), but this function returns the
1003 * object if it exists, NULL if it does not and the insertion was successful,
1004 * and an ERR_PTR otherwise.
1005 */
1006static inline void *rhashtable_lookup_get_insert_key(
1007 struct rhashtable *ht, const void *key, struct rhash_head *obj,
1008 const struct rhashtable_params params)
1009{
Herbert Xu02fd97c2015-03-20 21:57:00 +11001010 BUG_ON(!ht->p.obj_hashfn || !key);
1011
Herbert Xuca268932016-09-19 19:00:09 +08001012 return __rhashtable_insert_fast(ht, key, obj, params, false);
Herbert Xu02fd97c2015-03-20 21:57:00 +11001013}
1014
Thomas Grafac833bd2015-03-24 14:18:18 +01001015/* Internal function, please use rhashtable_remove_fast() instead */
Herbert Xuca268932016-09-19 19:00:09 +08001016static inline int __rhashtable_remove_fast_one(
Herbert Xu02fd97c2015-03-20 21:57:00 +11001017 struct rhashtable *ht, struct bucket_table *tbl,
Herbert Xuca268932016-09-19 19:00:09 +08001018 struct rhash_head *obj, const struct rhashtable_params params,
1019 bool rhlist)
Herbert Xu02fd97c2015-03-20 21:57:00 +11001020{
1021 struct rhash_head __rcu **pprev;
1022 struct rhash_head *he;
1023 spinlock_t * lock;
Thomas Graf299e5c32015-03-24 14:18:17 +01001024 unsigned int hash;
Herbert Xu02fd97c2015-03-20 21:57:00 +11001025 int err = -ENOENT;
1026
1027 hash = rht_head_hashfn(ht, tbl, obj, params);
1028 lock = rht_bucket_lock(tbl, hash);
1029
1030 spin_lock_bh(lock);
1031
Herbert Xuda204202017-02-11 19:26:47 +08001032 pprev = rht_bucket_var(tbl, hash);
1033 rht_for_each_continue(he, *pprev, tbl, hash) {
Herbert Xuca268932016-09-19 19:00:09 +08001034 struct rhlist_head *list;
1035
1036 list = container_of(he, struct rhlist_head, rhead);
1037
Herbert Xu02fd97c2015-03-20 21:57:00 +11001038 if (he != obj) {
Herbert Xuca268932016-09-19 19:00:09 +08001039 struct rhlist_head __rcu **lpprev;
1040
Herbert Xu02fd97c2015-03-20 21:57:00 +11001041 pprev = &he->next;
Herbert Xuca268932016-09-19 19:00:09 +08001042
1043 if (!rhlist)
1044 continue;
1045
1046 do {
1047 lpprev = &list->next;
1048 list = rht_dereference_bucket(list->next,
1049 tbl, hash);
1050 } while (list && obj != &list->rhead);
1051
1052 if (!list)
1053 continue;
1054
1055 list = rht_dereference_bucket(list->next, tbl, hash);
1056 RCU_INIT_POINTER(*lpprev, list);
1057 err = 0;
1058 break;
Herbert Xu02fd97c2015-03-20 21:57:00 +11001059 }
1060
Herbert Xuca268932016-09-19 19:00:09 +08001061 obj = rht_dereference_bucket(obj->next, tbl, hash);
1062 err = 1;
1063
1064 if (rhlist) {
1065 list = rht_dereference_bucket(list->next, tbl, hash);
1066 if (list) {
1067 RCU_INIT_POINTER(list->rhead.next, obj);
1068 obj = &list->rhead;
1069 err = 0;
1070 }
1071 }
1072
1073 rcu_assign_pointer(*pprev, obj);
Herbert Xu02fd97c2015-03-20 21:57:00 +11001074 break;
1075 }
1076
1077 spin_unlock_bh(lock);
1078
Herbert Xuca268932016-09-19 19:00:09 +08001079 if (err > 0) {
1080 atomic_dec(&ht->nelems);
1081 if (unlikely(ht->p.automatic_shrinking &&
1082 rht_shrink_below_30(ht, tbl)))
1083 schedule_work(&ht->run_work);
1084 err = 0;
1085 }
1086
1087 return err;
1088}
1089
1090/* Internal function, please use rhashtable_remove_fast() instead */
1091static inline int __rhashtable_remove_fast(
1092 struct rhashtable *ht, struct rhash_head *obj,
1093 const struct rhashtable_params params, bool rhlist)
1094{
1095 struct bucket_table *tbl;
1096 int err;
1097
1098 rcu_read_lock();
1099
1100 tbl = rht_dereference_rcu(ht->tbl, ht);
1101
1102 /* Because we have already taken (and released) the bucket
1103 * lock in old_tbl, if we find that future_tbl is not yet
1104 * visible then that guarantees the entry to still be in
1105 * the old tbl if it exists.
1106 */
1107 while ((err = __rhashtable_remove_fast_one(ht, tbl, obj, params,
1108 rhlist)) &&
1109 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
1110 ;
1111
1112 rcu_read_unlock();
1113
Herbert Xu02fd97c2015-03-20 21:57:00 +11001114 return err;
1115}
1116
1117/**
1118 * rhashtable_remove_fast - remove object from hash table
1119 * @ht: hash table
1120 * @obj: pointer to hash head inside object
1121 * @params: hash table parameters
1122 *
1123 * Since the hash chain is single linked, the removal operation needs to
1124 * walk the bucket chain upon removal. The removal operation is thus
1125 * considerable slow if the hash table is not correctly sized.
1126 *
1127 * Will automatically shrink the table via rhashtable_expand() if the
1128 * shrink_decision function specified at rhashtable_init() returns true.
1129 *
1130 * Returns zero on success, -ENOENT if the entry could not be found.
1131 */
1132static inline int rhashtable_remove_fast(
1133 struct rhashtable *ht, struct rhash_head *obj,
1134 const struct rhashtable_params params)
1135{
Herbert Xuca268932016-09-19 19:00:09 +08001136 return __rhashtable_remove_fast(ht, obj, params, false);
1137}
Herbert Xu02fd97c2015-03-20 21:57:00 +11001138
Herbert Xuca268932016-09-19 19:00:09 +08001139/**
1140 * rhltable_remove - remove object from hash list table
1141 * @hlt: hash list table
1142 * @list: pointer to hash list head inside object
1143 * @params: hash table parameters
1144 *
1145 * Since the hash chain is single linked, the removal operation needs to
1146 * walk the bucket chain upon removal. The removal operation is thus
1147 * considerable slow if the hash table is not correctly sized.
1148 *
1149 * Will automatically shrink the table via rhashtable_expand() if the
1150 * shrink_decision function specified at rhashtable_init() returns true.
1151 *
1152 * Returns zero on success, -ENOENT if the entry could not be found.
1153 */
1154static inline int rhltable_remove(
1155 struct rhltable *hlt, struct rhlist_head *list,
1156 const struct rhashtable_params params)
1157{
1158 return __rhashtable_remove_fast(&hlt->ht, &list->rhead, params, true);
Herbert Xu02fd97c2015-03-20 21:57:00 +11001159}
1160
Tom Herbert3502cad2015-12-15 15:41:36 -08001161/* Internal function, please use rhashtable_replace_fast() instead */
1162static inline int __rhashtable_replace_fast(
1163 struct rhashtable *ht, struct bucket_table *tbl,
1164 struct rhash_head *obj_old, struct rhash_head *obj_new,
1165 const struct rhashtable_params params)
1166{
1167 struct rhash_head __rcu **pprev;
1168 struct rhash_head *he;
1169 spinlock_t *lock;
1170 unsigned int hash;
1171 int err = -ENOENT;
1172
1173 /* Minimally, the old and new objects must have same hash
1174 * (which should mean identifiers are the same).
1175 */
1176 hash = rht_head_hashfn(ht, tbl, obj_old, params);
1177 if (hash != rht_head_hashfn(ht, tbl, obj_new, params))
1178 return -EINVAL;
1179
1180 lock = rht_bucket_lock(tbl, hash);
1181
1182 spin_lock_bh(lock);
1183
Herbert Xuda204202017-02-11 19:26:47 +08001184 pprev = rht_bucket_var(tbl, hash);
1185 rht_for_each_continue(he, *pprev, tbl, hash) {
Tom Herbert3502cad2015-12-15 15:41:36 -08001186 if (he != obj_old) {
1187 pprev = &he->next;
1188 continue;
1189 }
1190
1191 rcu_assign_pointer(obj_new->next, obj_old->next);
1192 rcu_assign_pointer(*pprev, obj_new);
1193 err = 0;
1194 break;
1195 }
1196
1197 spin_unlock_bh(lock);
1198
1199 return err;
1200}
1201
1202/**
1203 * rhashtable_replace_fast - replace an object in hash table
1204 * @ht: hash table
1205 * @obj_old: pointer to hash head inside object being replaced
1206 * @obj_new: pointer to hash head inside object which is new
1207 * @params: hash table parameters
1208 *
1209 * Replacing an object doesn't affect the number of elements in the hash table
1210 * or bucket, so we don't need to worry about shrinking or expanding the
1211 * table here.
1212 *
1213 * Returns zero on success, -ENOENT if the entry could not be found,
1214 * -EINVAL if hash is not the same for the old and new objects.
1215 */
1216static inline int rhashtable_replace_fast(
1217 struct rhashtable *ht, struct rhash_head *obj_old,
1218 struct rhash_head *obj_new,
1219 const struct rhashtable_params params)
1220{
1221 struct bucket_table *tbl;
1222 int err;
1223
1224 rcu_read_lock();
1225
1226 tbl = rht_dereference_rcu(ht->tbl, ht);
1227
1228 /* Because we have already taken (and released) the bucket
1229 * lock in old_tbl, if we find that future_tbl is not yet
1230 * visible then that guarantees the entry to still be in
1231 * the old tbl if it exists.
1232 */
1233 while ((err = __rhashtable_replace_fast(ht, tbl, obj_old,
1234 obj_new, params)) &&
1235 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
1236 ;
1237
1238 rcu_read_unlock();
1239
1240 return err;
1241}
1242
Herbert Xu246779d2016-08-18 16:50:56 +08001243/* Obsolete function, do not use in new code. */
1244static inline int rhashtable_walk_init(struct rhashtable *ht,
1245 struct rhashtable_iter *iter, gfp_t gfp)
1246{
1247 rhashtable_walk_enter(ht, iter);
1248 return 0;
1249}
1250
Herbert Xuca268932016-09-19 19:00:09 +08001251/**
1252 * rhltable_walk_enter - Initialise an iterator
1253 * @hlt: Table to walk over
1254 * @iter: Hash table Iterator
1255 *
1256 * This function prepares a hash table walk.
1257 *
1258 * Note that if you restart a walk after rhashtable_walk_stop you
1259 * may see the same object twice. Also, you may miss objects if
1260 * there are removals in between rhashtable_walk_stop and the next
1261 * call to rhashtable_walk_start.
1262 *
1263 * For a completely stable walk you should construct your own data
1264 * structure outside the hash table.
1265 *
1266 * This function may sleep so you must not call it from interrupt
1267 * context or with spin locks held.
1268 *
1269 * You must call rhashtable_walk_exit after this function returns.
1270 */
1271static inline void rhltable_walk_enter(struct rhltable *hlt,
1272 struct rhashtable_iter *iter)
1273{
1274 return rhashtable_walk_enter(&hlt->ht, iter);
1275}
1276
1277/**
1278 * rhltable_free_and_destroy - free elements and destroy hash list table
1279 * @hlt: the hash list table to destroy
1280 * @free_fn: callback to release resources of element
1281 * @arg: pointer passed to free_fn
1282 *
1283 * See documentation for rhashtable_free_and_destroy.
1284 */
1285static inline void rhltable_free_and_destroy(struct rhltable *hlt,
1286 void (*free_fn)(void *ptr,
1287 void *arg),
1288 void *arg)
1289{
1290 return rhashtable_free_and_destroy(&hlt->ht, free_fn, arg);
1291}
1292
1293static inline void rhltable_destroy(struct rhltable *hlt)
1294{
1295 return rhltable_free_and_destroy(hlt, NULL, NULL);
1296}
1297
Thomas Graf7e1e7762014-08-02 11:47:44 +02001298#endif /* _LINUX_RHASHTABLE_H */