blob: ae87dcdf52d297795c2949a5493eae55d367e74f [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 Xu07ee0722015-05-15 11:30:47 +0800128 * @insecure_max_entries: Maximum number of entries (may be exceeded)
Herbert Xuc2e213c2015-03-18 20:01:16 +1100129 * @max_size: Maximum size while expanding
130 * @min_size: Minimum size while shrinking
Thomas Graff89bd6f2015-01-02 23:00:21 +0100131 * @nulls_base: Base value to generate nulls marker
Thomas Grafb5e2c152015-03-24 20:42:19 +0000132 * @automatic_shrinking: Enable automatic shrinking of tables
Thomas Graf97defe12015-01-02 23:00:20 +0100133 * @locks_mul: Number of bucket locks to allocate per cpu (default: 128)
Herbert Xu31ccde22015-03-24 00:50:21 +1100134 * @hashfn: Hash function (default: jhash2 if !(key_len % 4), or jhash)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200135 * @obj_hashfn: Function to hash object
Herbert Xu02fd97c2015-03-20 21:57:00 +1100136 * @obj_cmpfn: Function to compare key with object
Thomas Graf7e1e7762014-08-02 11:47:44 +0200137 */
138struct rhashtable_params {
139 size_t nelem_hint;
140 size_t key_len;
141 size_t key_offset;
142 size_t head_offset;
Herbert Xu07ee0722015-05-15 11:30:47 +0800143 unsigned int insecure_max_entries;
Herbert Xuc2e213c2015-03-18 20:01:16 +1100144 unsigned int max_size;
145 unsigned int min_size;
Thomas Graff89bd6f2015-01-02 23:00:21 +0100146 u32 nulls_base;
Thomas Grafb5e2c152015-03-24 20:42:19 +0000147 bool automatic_shrinking;
Thomas Graf97defe12015-01-02 23:00:20 +0100148 size_t locks_mul;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200149 rht_hashfn_t hashfn;
150 rht_obj_hashfn_t obj_hashfn;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100151 rht_obj_cmpfn_t obj_cmpfn;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200152};
153
154/**
155 * struct rhashtable - Hash table handle
156 * @tbl: Bucket table
157 * @nelems: Number of elements in table
Herbert Xu31ccde22015-03-24 00:50:21 +1100158 * @key_len: Key length for hashfn
Thomas Graf7e1e7762014-08-02 11:47:44 +0200159 * @p: Configuration parameters
Herbert Xuca268932016-09-19 19:00:09 +0800160 * @rhlist: True if this is an rhltable
Thomas Graf97defe12015-01-02 23:00:20 +0100161 * @run_work: Deferred worker to expand/shrink asynchronously
162 * @mutex: Mutex to protect current/future table swapping
Herbert Xuba7c95e2015-03-24 09:53:17 +1100163 * @lock: Spin lock to protect walker list
Thomas Graf7e1e7762014-08-02 11:47:44 +0200164 */
165struct rhashtable {
166 struct bucket_table __rcu *tbl;
Thomas Graf97defe12015-01-02 23:00:20 +0100167 atomic_t nelems;
Herbert Xu31ccde22015-03-24 00:50:21 +1100168 unsigned int key_len;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200169 struct rhashtable_params p;
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{
332 return ht->p.insecure_max_entries &&
333 atomic_read(&ht->nelems) >= ht->p.insecure_max_entries;
334}
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);
382int rhashtable_walk_start(struct rhashtable_iter *iter) __acquires(RCU);
383void *rhashtable_walk_next(struct rhashtable_iter *iter);
384void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU);
385
Thomas Graf6b6f3022015-03-24 14:18:20 +0100386void rhashtable_free_and_destroy(struct rhashtable *ht,
387 void (*free_fn)(void *ptr, void *arg),
388 void *arg);
Thomas Graf97defe12015-01-02 23:00:20 +0100389void rhashtable_destroy(struct rhashtable *ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200390
Herbert Xuda204202017-02-11 19:26:47 +0800391struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl,
392 unsigned int hash);
393struct rhash_head __rcu **rht_bucket_nested_insert(struct rhashtable *ht,
394 struct bucket_table *tbl,
395 unsigned int hash);
396
Thomas Graf7e1e7762014-08-02 11:47:44 +0200397#define rht_dereference(p, ht) \
398 rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht))
399
400#define rht_dereference_rcu(p, ht) \
401 rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht))
402
Thomas Graf88d6ed12015-01-02 23:00:16 +0100403#define rht_dereference_bucket(p, tbl, hash) \
404 rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200405
Thomas Graf88d6ed12015-01-02 23:00:16 +0100406#define rht_dereference_bucket_rcu(p, tbl, hash) \
407 rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash))
408
409#define rht_entry(tpos, pos, member) \
410 ({ tpos = container_of(pos, typeof(*tpos), member); 1; })
411
Herbert Xuda204202017-02-11 19:26:47 +0800412static inline struct rhash_head __rcu *const *rht_bucket(
413 const struct bucket_table *tbl, unsigned int hash)
414{
415 return unlikely(tbl->nest) ? rht_bucket_nested(tbl, hash) :
416 &tbl->buckets[hash];
417}
418
419static inline struct rhash_head __rcu **rht_bucket_var(
420 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_insert(
427 struct rhashtable *ht, struct bucket_table *tbl, unsigned int hash)
428{
429 return unlikely(tbl->nest) ? rht_bucket_nested_insert(ht, tbl, hash) :
430 &tbl->buckets[hash];
431}
432
Thomas Graf88d6ed12015-01-02 23:00:16 +0100433/**
434 * rht_for_each_continue - continue iterating over hash chain
435 * @pos: the &struct rhash_head to use as a loop cursor.
436 * @head: the previous &struct rhash_head to continue from
437 * @tbl: the &struct bucket_table
438 * @hash: the hash value / bucket index
439 */
440#define rht_for_each_continue(pos, head, tbl, hash) \
441 for (pos = rht_dereference_bucket(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100442 !rht_is_a_nulls(pos); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100443 pos = rht_dereference_bucket((pos)->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200444
445/**
446 * rht_for_each - iterate over hash chain
Thomas Graf88d6ed12015-01-02 23:00:16 +0100447 * @pos: the &struct rhash_head to use as a loop cursor.
448 * @tbl: the &struct bucket_table
449 * @hash: the hash value / bucket index
Thomas Graf7e1e7762014-08-02 11:47:44 +0200450 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100451#define rht_for_each(pos, tbl, hash) \
Herbert Xuda204202017-02-11 19:26:47 +0800452 rht_for_each_continue(pos, *rht_bucket(tbl, hash), tbl, hash)
Thomas Graf88d6ed12015-01-02 23:00:16 +0100453
454/**
455 * rht_for_each_entry_continue - continue iterating over hash chain
456 * @tpos: the type * to use as a loop cursor.
457 * @pos: the &struct rhash_head to use as a loop cursor.
458 * @head: the previous &struct rhash_head to continue from
459 * @tbl: the &struct bucket_table
460 * @hash: the hash value / bucket index
461 * @member: name of the &struct rhash_head within the hashable struct.
462 */
463#define rht_for_each_entry_continue(tpos, pos, head, tbl, hash, member) \
464 for (pos = rht_dereference_bucket(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100465 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100466 pos = rht_dereference_bucket((pos)->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200467
468/**
469 * rht_for_each_entry - iterate over hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100470 * @tpos: the type * to use as a loop cursor.
471 * @pos: the &struct rhash_head to use as a loop cursor.
472 * @tbl: the &struct bucket_table
473 * @hash: the hash value / bucket index
474 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200475 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100476#define rht_for_each_entry(tpos, pos, tbl, hash, member) \
Herbert Xuda204202017-02-11 19:26:47 +0800477 rht_for_each_entry_continue(tpos, pos, *rht_bucket(tbl, hash), \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100478 tbl, hash, member)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200479
480/**
481 * rht_for_each_entry_safe - safely iterate over hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100482 * @tpos: the type * to use as a loop cursor.
483 * @pos: the &struct rhash_head to use as a loop cursor.
484 * @next: the &struct rhash_head to use as next in loop cursor.
485 * @tbl: the &struct bucket_table
486 * @hash: the hash value / bucket index
487 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200488 *
489 * This hash chain list-traversal primitive allows for the looped code to
490 * remove the loop cursor from the list.
491 */
Herbert Xuda204202017-02-11 19:26:47 +0800492#define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \
493 for (pos = rht_dereference_bucket(*rht_bucket(tbl, hash), tbl, hash), \
494 next = !rht_is_a_nulls(pos) ? \
495 rht_dereference_bucket(pos->next, tbl, hash) : NULL; \
496 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
497 pos = next, \
498 next = !rht_is_a_nulls(pos) ? \
Patrick McHardy607954b2015-01-21 11:12:13 +0000499 rht_dereference_bucket(pos->next, tbl, hash) : NULL)
Thomas Graf88d6ed12015-01-02 23:00:16 +0100500
501/**
502 * rht_for_each_rcu_continue - continue iterating over rcu hash chain
503 * @pos: the &struct rhash_head to use as a loop cursor.
504 * @head: the previous &struct rhash_head to continue from
505 * @tbl: the &struct bucket_table
506 * @hash: the hash value / bucket index
507 *
508 * This hash chain list-traversal primitive may safely run concurrently with
509 * the _rcu mutation primitives such as rhashtable_insert() as long as the
510 * traversal is guarded by rcu_read_lock().
511 */
512#define rht_for_each_rcu_continue(pos, head, tbl, hash) \
513 for (({barrier(); }), \
514 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100515 !rht_is_a_nulls(pos); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100516 pos = rcu_dereference_raw(pos->next))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200517
518/**
519 * rht_for_each_rcu - iterate over rcu hash chain
Thomas Graf88d6ed12015-01-02 23:00:16 +0100520 * @pos: the &struct rhash_head to use as a loop cursor.
521 * @tbl: the &struct bucket_table
522 * @hash: the hash value / bucket index
Thomas Graf7e1e7762014-08-02 11:47:44 +0200523 *
524 * This hash chain list-traversal primitive may safely run concurrently with
Thomas Graf88d6ed12015-01-02 23:00:16 +0100525 * the _rcu mutation primitives such as rhashtable_insert() as long as the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200526 * traversal is guarded by rcu_read_lock().
527 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100528#define rht_for_each_rcu(pos, tbl, hash) \
Herbert Xuda204202017-02-11 19:26:47 +0800529 rht_for_each_rcu_continue(pos, *rht_bucket(tbl, hash), tbl, hash)
Thomas Graf88d6ed12015-01-02 23:00:16 +0100530
531/**
532 * rht_for_each_entry_rcu_continue - continue iterating over rcu hash chain
533 * @tpos: the type * to use as a loop cursor.
534 * @pos: the &struct rhash_head to use as a loop cursor.
535 * @head: the previous &struct rhash_head to continue from
536 * @tbl: the &struct bucket_table
537 * @hash: the hash value / bucket index
538 * @member: name of the &struct rhash_head within the hashable struct.
539 *
540 * This hash chain list-traversal primitive may safely run concurrently with
541 * the _rcu mutation primitives such as rhashtable_insert() as long as the
542 * traversal is guarded by rcu_read_lock().
543 */
544#define rht_for_each_entry_rcu_continue(tpos, pos, head, tbl, hash, member) \
545 for (({barrier(); }), \
546 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100547 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100548 pos = rht_dereference_bucket_rcu(pos->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200549
550/**
551 * rht_for_each_entry_rcu - iterate over rcu hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100552 * @tpos: the type * to use as a loop cursor.
553 * @pos: the &struct rhash_head to use as a loop cursor.
554 * @tbl: the &struct bucket_table
555 * @hash: the hash value / bucket index
556 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200557 *
558 * This hash chain list-traversal primitive may safely run concurrently with
Thomas Graf88d6ed12015-01-02 23:00:16 +0100559 * the _rcu mutation primitives such as rhashtable_insert() as long as the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200560 * traversal is guarded by rcu_read_lock().
561 */
Herbert Xuda204202017-02-11 19:26:47 +0800562#define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \
563 rht_for_each_entry_rcu_continue(tpos, pos, *rht_bucket(tbl, hash), \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100564 tbl, hash, member)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200565
Herbert Xuca268932016-09-19 19:00:09 +0800566/**
567 * rhl_for_each_rcu - iterate over rcu hash table list
568 * @pos: the &struct rlist_head to use as a loop cursor.
569 * @list: the head of the list
570 *
571 * This hash chain list-traversal primitive should be used on the
572 * list returned by rhltable_lookup.
573 */
574#define rhl_for_each_rcu(pos, list) \
575 for (pos = list; pos; pos = rcu_dereference_raw(pos->next))
576
577/**
578 * rhl_for_each_entry_rcu - iterate over rcu hash table list of given type
579 * @tpos: the type * to use as a loop cursor.
580 * @pos: the &struct rlist_head to use as a loop cursor.
581 * @list: the head of the list
582 * @member: name of the &struct rlist_head within the hashable struct.
583 *
584 * This hash chain list-traversal primitive should be used on the
585 * list returned by rhltable_lookup.
586 */
587#define rhl_for_each_entry_rcu(tpos, pos, list, member) \
588 for (pos = list; pos && rht_entry(tpos, pos, member); \
589 pos = rcu_dereference_raw(pos->next))
590
Herbert Xu02fd97c2015-03-20 21:57:00 +1100591static inline int rhashtable_compare(struct rhashtable_compare_arg *arg,
592 const void *obj)
593{
594 struct rhashtable *ht = arg->ht;
595 const char *ptr = obj;
596
597 return memcmp(ptr + ht->p.key_offset, arg->key, ht->p.key_len);
598}
599
Herbert Xuca268932016-09-19 19:00:09 +0800600/* Internal function, do not use. */
601static inline struct rhash_head *__rhashtable_lookup(
Herbert Xu02fd97c2015-03-20 21:57:00 +1100602 struct rhashtable *ht, const void *key,
603 const struct rhashtable_params params)
604{
605 struct rhashtable_compare_arg arg = {
606 .ht = ht,
607 .key = key,
608 };
Herbert Xuda204202017-02-11 19:26:47 +0800609 struct bucket_table *tbl;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100610 struct rhash_head *he;
Thomas Graf299e5c32015-03-24 14:18:17 +0100611 unsigned int hash;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100612
Herbert Xu02fd97c2015-03-20 21:57:00 +1100613 tbl = rht_dereference_rcu(ht->tbl, ht);
614restart:
615 hash = rht_key_hashfn(ht, tbl, key, params);
616 rht_for_each_rcu(he, tbl, hash) {
617 if (params.obj_cmpfn ?
618 params.obj_cmpfn(&arg, rht_obj(ht, he)) :
619 rhashtable_compare(&arg, rht_obj(ht, he)))
620 continue;
Herbert Xuca268932016-09-19 19:00:09 +0800621 return he;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100622 }
623
624 /* Ensure we see any new tables. */
625 smp_rmb();
626
627 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
628 if (unlikely(tbl))
629 goto restart;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100630
631 return NULL;
632}
633
Herbert Xuca268932016-09-19 19:00:09 +0800634/**
635 * rhashtable_lookup - search hash table
636 * @ht: hash table
637 * @key: the pointer to the key
638 * @params: hash table parameters
639 *
640 * Computes the hash value for the key and traverses the bucket chain looking
641 * for a entry with an identical key. The first matching entry is returned.
642 *
643 * This must only be called under the RCU read lock.
644 *
645 * Returns the first entry on which the compare function returned true.
646 */
647static inline void *rhashtable_lookup(
648 struct rhashtable *ht, const void *key,
649 const struct rhashtable_params params)
650{
651 struct rhash_head *he = __rhashtable_lookup(ht, key, params);
652
653 return he ? rht_obj(ht, he) : NULL;
654}
655
656/**
657 * rhashtable_lookup_fast - search hash table, without RCU read lock
658 * @ht: hash table
659 * @key: the pointer to the key
660 * @params: hash table parameters
661 *
662 * Computes the hash value for the key and traverses the bucket chain looking
663 * for a entry with an identical key. The first matching entry is returned.
664 *
665 * Only use this function when you have other mechanisms guaranteeing
666 * that the object won't go away after the RCU read lock is released.
667 *
668 * Returns the first entry on which the compare function returned true.
669 */
670static inline void *rhashtable_lookup_fast(
671 struct rhashtable *ht, const void *key,
672 const struct rhashtable_params params)
673{
674 void *obj;
675
676 rcu_read_lock();
677 obj = rhashtable_lookup(ht, key, params);
678 rcu_read_unlock();
679
680 return obj;
681}
682
683/**
684 * rhltable_lookup - search hash list table
685 * @hlt: hash table
686 * @key: the pointer to the key
687 * @params: hash table parameters
688 *
689 * Computes the hash value for the key and traverses the bucket chain looking
690 * for a entry with an identical key. All matching entries are returned
691 * in a list.
692 *
693 * This must only be called under the RCU read lock.
694 *
695 * Returns the list of entries that match the given key.
696 */
697static inline struct rhlist_head *rhltable_lookup(
698 struct rhltable *hlt, const void *key,
699 const struct rhashtable_params params)
700{
701 struct rhash_head *he = __rhashtable_lookup(&hlt->ht, key, params);
702
703 return he ? container_of(he, struct rhlist_head, rhead) : NULL;
704}
705
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200706/* Internal function, please use rhashtable_insert_fast() instead. This
707 * function returns the existing element already in hashes in there is a clash,
708 * otherwise it returns an error via ERR_PTR().
709 */
710static inline void *__rhashtable_insert_fast(
Herbert Xu02fd97c2015-03-20 21:57:00 +1100711 struct rhashtable *ht, const void *key, struct rhash_head *obj,
Herbert Xuca268932016-09-19 19:00:09 +0800712 const struct rhashtable_params params, bool rhlist)
Herbert Xu02fd97c2015-03-20 21:57:00 +1100713{
714 struct rhashtable_compare_arg arg = {
715 .ht = ht,
716 .key = key,
717 };
Herbert Xuca268932016-09-19 19:00:09 +0800718 struct rhash_head __rcu **pprev;
719 struct bucket_table *tbl;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100720 struct rhash_head *head;
721 spinlock_t *lock;
Thomas Graf299e5c32015-03-24 14:18:17 +0100722 unsigned int hash;
Herbert Xuca268932016-09-19 19:00:09 +0800723 int elasticity;
724 void *data;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100725
726 rcu_read_lock();
727
728 tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xuca268932016-09-19 19:00:09 +0800729 hash = rht_head_hashfn(ht, tbl, obj, params);
730 lock = rht_bucket_lock(tbl, hash);
731 spin_lock_bh(lock);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100732
Herbert Xuca268932016-09-19 19:00:09 +0800733 if (unlikely(rht_dereference_bucket(tbl->future_tbl, tbl, hash))) {
734slow_path:
Herbert Xub8244782015-03-24 00:50:26 +1100735 spin_unlock_bh(lock);
Herbert Xuca268932016-09-19 19:00:09 +0800736 rcu_read_unlock();
737 return rhashtable_insert_slow(ht, key, obj);
Herbert Xub8244782015-03-24 00:50:26 +1100738 }
739
Florian Westphal5f8ddea2017-04-16 02:55:09 +0200740 elasticity = RHT_ELASTICITY;
Herbert Xuda204202017-02-11 19:26:47 +0800741 pprev = rht_bucket_insert(ht, tbl, hash);
742 data = ERR_PTR(-ENOMEM);
743 if (!pprev)
744 goto out;
745
746 rht_for_each_continue(head, *pprev, tbl, hash) {
Herbert Xuca268932016-09-19 19:00:09 +0800747 struct rhlist_head *plist;
748 struct rhlist_head *list;
Herbert Xu3cf92222015-12-03 20:41:29 +0800749
Herbert Xuca268932016-09-19 19:00:09 +0800750 elasticity--;
751 if (!key ||
752 (params.obj_cmpfn ?
753 params.obj_cmpfn(&arg, rht_obj(ht, head)) :
754 rhashtable_compare(&arg, rht_obj(ht, head))))
755 continue;
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200756
Herbert Xuca268932016-09-19 19:00:09 +0800757 data = rht_obj(ht, head);
758
759 if (!rhlist)
760 goto out;
761
762
763 list = container_of(obj, struct rhlist_head, rhead);
764 plist = container_of(head, struct rhlist_head, rhead);
765
766 RCU_INIT_POINTER(list->next, plist);
767 head = rht_dereference_bucket(head->next, tbl, hash);
768 RCU_INIT_POINTER(list->rhead.next, head);
769 rcu_assign_pointer(*pprev, obj);
770
771 goto good;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100772 }
773
Herbert Xuca268932016-09-19 19:00:09 +0800774 if (elasticity <= 0)
775 goto slow_path;
776
777 data = ERR_PTR(-E2BIG);
Herbert Xu07ee0722015-05-15 11:30:47 +0800778 if (unlikely(rht_grow_above_max(ht, tbl)))
779 goto out;
780
Herbert Xuca268932016-09-19 19:00:09 +0800781 if (unlikely(rht_grow_above_100(ht, tbl)))
782 goto slow_path;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100783
Herbert Xuda204202017-02-11 19:26:47 +0800784 head = rht_dereference_bucket(*pprev, tbl, hash);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100785
786 RCU_INIT_POINTER(obj->next, head);
Herbert Xuca268932016-09-19 19:00:09 +0800787 if (rhlist) {
788 struct rhlist_head *list;
789
790 list = container_of(obj, struct rhlist_head, rhead);
791 RCU_INIT_POINTER(list->next, NULL);
792 }
Herbert Xu02fd97c2015-03-20 21:57:00 +1100793
Herbert Xuda204202017-02-11 19:26:47 +0800794 rcu_assign_pointer(*pprev, obj);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100795
796 atomic_inc(&ht->nelems);
797 if (rht_grow_above_75(ht, tbl))
798 schedule_work(&ht->run_work);
799
Herbert Xuca268932016-09-19 19:00:09 +0800800good:
801 data = NULL;
802
Herbert Xu02fd97c2015-03-20 21:57:00 +1100803out:
804 spin_unlock_bh(lock);
805 rcu_read_unlock();
806
Herbert Xuca268932016-09-19 19:00:09 +0800807 return data;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100808}
809
810/**
811 * rhashtable_insert_fast - insert object into hash table
812 * @ht: hash table
813 * @obj: pointer to hash head inside object
814 * @params: hash table parameters
815 *
816 * Will take a per bucket spinlock to protect against mutual mutations
817 * on the same bucket. Multiple insertions may occur in parallel unless
818 * they map to the same bucket lock.
819 *
820 * It is safe to call this function from atomic context.
821 *
822 * Will trigger an automatic deferred table resizing if the size grows
823 * beyond the watermark indicated by grow_decision() which can be passed
824 * to rhashtable_init().
825 */
826static inline int rhashtable_insert_fast(
827 struct rhashtable *ht, struct rhash_head *obj,
828 const struct rhashtable_params params)
829{
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200830 void *ret;
831
Herbert Xuca268932016-09-19 19:00:09 +0800832 ret = __rhashtable_insert_fast(ht, NULL, obj, params, false);
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200833 if (IS_ERR(ret))
834 return PTR_ERR(ret);
835
836 return ret == NULL ? 0 : -EEXIST;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100837}
838
839/**
Herbert Xuca268932016-09-19 19:00:09 +0800840 * rhltable_insert_key - insert object into hash list table
841 * @hlt: hash list table
842 * @key: the pointer to the key
843 * @list: pointer to hash list head inside object
844 * @params: hash table parameters
845 *
846 * Will take a per bucket spinlock to protect against mutual mutations
847 * on the same bucket. Multiple insertions may occur in parallel unless
848 * they map to the same bucket lock.
849 *
850 * It is safe to call this function from atomic context.
851 *
852 * Will trigger an automatic deferred table resizing if the size grows
853 * beyond the watermark indicated by grow_decision() which can be passed
854 * to rhashtable_init().
855 */
856static inline int rhltable_insert_key(
857 struct rhltable *hlt, const void *key, struct rhlist_head *list,
858 const struct rhashtable_params params)
859{
860 return PTR_ERR(__rhashtable_insert_fast(&hlt->ht, key, &list->rhead,
861 params, true));
862}
863
864/**
865 * rhltable_insert - insert object into hash list table
866 * @hlt: hash list table
867 * @list: pointer to hash list head inside object
868 * @params: hash table parameters
869 *
870 * Will take a per bucket spinlock to protect against mutual mutations
871 * on the same bucket. Multiple insertions may occur in parallel unless
872 * they map to the same bucket lock.
873 *
874 * It is safe to call this function from atomic context.
875 *
876 * Will trigger an automatic deferred table resizing if the size grows
877 * beyond the watermark indicated by grow_decision() which can be passed
878 * to rhashtable_init().
879 */
880static inline int rhltable_insert(
881 struct rhltable *hlt, struct rhlist_head *list,
882 const struct rhashtable_params params)
883{
884 const char *key = rht_obj(&hlt->ht, &list->rhead);
885
886 key += params.key_offset;
887
888 return rhltable_insert_key(hlt, key, list, params);
889}
890
891/**
Herbert Xu02fd97c2015-03-20 21:57:00 +1100892 * rhashtable_lookup_insert_fast - lookup and insert object into hash table
893 * @ht: hash table
894 * @obj: pointer to hash head inside object
895 * @params: hash table parameters
896 *
897 * Locks down the bucket chain in both the old and new table if a resize
898 * is in progress to ensure that writers can't remove from the old table
899 * and can't insert to the new table during the atomic operation of search
900 * and insertion. Searches for duplicates in both the old and new table if
901 * a resize is in progress.
902 *
903 * This lookup function may only be used for fixed key hash table (key_len
904 * parameter set). It will BUG() if used inappropriately.
905 *
906 * It is safe to call this function from atomic context.
907 *
908 * Will trigger an automatic deferred table resizing if the size grows
909 * beyond the watermark indicated by grow_decision() which can be passed
910 * to rhashtable_init().
911 */
912static inline int rhashtable_lookup_insert_fast(
913 struct rhashtable *ht, struct rhash_head *obj,
914 const struct rhashtable_params params)
915{
916 const char *key = rht_obj(ht, obj);
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200917 void *ret;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100918
919 BUG_ON(ht->p.obj_hashfn);
920
Herbert Xuca268932016-09-19 19:00:09 +0800921 ret = __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params,
922 false);
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200923 if (IS_ERR(ret))
924 return PTR_ERR(ret);
925
926 return ret == NULL ? 0 : -EEXIST;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100927}
928
929/**
Andreas Gruenbacherf9fe1c12017-03-18 00:36:15 +0100930 * rhashtable_lookup_get_insert_fast - lookup and insert object into hash table
931 * @ht: hash table
932 * @obj: pointer to hash head inside object
933 * @params: hash table parameters
934 *
935 * Just like rhashtable_lookup_insert_fast(), but this function returns the
936 * object if it exists, NULL if it did not and the insertion was successful,
937 * and an ERR_PTR otherwise.
938 */
939static inline void *rhashtable_lookup_get_insert_fast(
940 struct rhashtable *ht, struct rhash_head *obj,
941 const struct rhashtable_params params)
942{
943 const char *key = rht_obj(ht, obj);
944
945 BUG_ON(ht->p.obj_hashfn);
946
947 return __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params,
948 false);
949}
950
951/**
Herbert Xu02fd97c2015-03-20 21:57:00 +1100952 * rhashtable_lookup_insert_key - search and insert object to hash table
953 * with explicit key
954 * @ht: hash table
955 * @key: key
956 * @obj: pointer to hash head inside object
957 * @params: hash table parameters
958 *
959 * Locks down the bucket chain in both the old and new table if a resize
960 * is in progress to ensure that writers can't remove from the old table
961 * and can't insert to the new table during the atomic operation of search
962 * and insertion. Searches for duplicates in both the old and new table if
963 * a resize is in progress.
964 *
965 * Lookups may occur in parallel with hashtable mutations and resizing.
966 *
967 * Will trigger an automatic deferred table resizing if the size grows
968 * beyond the watermark indicated by grow_decision() which can be passed
969 * to rhashtable_init().
970 *
971 * Returns zero on success.
972 */
973static inline int rhashtable_lookup_insert_key(
974 struct rhashtable *ht, const void *key, struct rhash_head *obj,
975 const struct rhashtable_params params)
976{
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200977 void *ret;
978
979 BUG_ON(!ht->p.obj_hashfn || !key);
980
Herbert Xuca268932016-09-19 19:00:09 +0800981 ret = __rhashtable_insert_fast(ht, key, obj, params, false);
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200982 if (IS_ERR(ret))
983 return PTR_ERR(ret);
984
985 return ret == NULL ? 0 : -EEXIST;
986}
987
988/**
989 * rhashtable_lookup_get_insert_key - lookup and insert object into hash table
990 * @ht: hash table
991 * @obj: pointer to hash head inside object
992 * @params: hash table parameters
993 * @data: pointer to element data already in hashes
994 *
995 * Just like rhashtable_lookup_insert_key(), but this function returns the
996 * object if it exists, NULL if it does not and the insertion was successful,
997 * and an ERR_PTR otherwise.
998 */
999static inline void *rhashtable_lookup_get_insert_key(
1000 struct rhashtable *ht, const void *key, struct rhash_head *obj,
1001 const struct rhashtable_params params)
1002{
Herbert Xu02fd97c2015-03-20 21:57:00 +11001003 BUG_ON(!ht->p.obj_hashfn || !key);
1004
Herbert Xuca268932016-09-19 19:00:09 +08001005 return __rhashtable_insert_fast(ht, key, obj, params, false);
Herbert Xu02fd97c2015-03-20 21:57:00 +11001006}
1007
Thomas Grafac833bd2015-03-24 14:18:18 +01001008/* Internal function, please use rhashtable_remove_fast() instead */
Herbert Xuca268932016-09-19 19:00:09 +08001009static inline int __rhashtable_remove_fast_one(
Herbert Xu02fd97c2015-03-20 21:57:00 +11001010 struct rhashtable *ht, struct bucket_table *tbl,
Herbert Xuca268932016-09-19 19:00:09 +08001011 struct rhash_head *obj, const struct rhashtable_params params,
1012 bool rhlist)
Herbert Xu02fd97c2015-03-20 21:57:00 +11001013{
1014 struct rhash_head __rcu **pprev;
1015 struct rhash_head *he;
1016 spinlock_t * lock;
Thomas Graf299e5c32015-03-24 14:18:17 +01001017 unsigned int hash;
Herbert Xu02fd97c2015-03-20 21:57:00 +11001018 int err = -ENOENT;
1019
1020 hash = rht_head_hashfn(ht, tbl, obj, params);
1021 lock = rht_bucket_lock(tbl, hash);
1022
1023 spin_lock_bh(lock);
1024
Herbert Xuda204202017-02-11 19:26:47 +08001025 pprev = rht_bucket_var(tbl, hash);
1026 rht_for_each_continue(he, *pprev, tbl, hash) {
Herbert Xuca268932016-09-19 19:00:09 +08001027 struct rhlist_head *list;
1028
1029 list = container_of(he, struct rhlist_head, rhead);
1030
Herbert Xu02fd97c2015-03-20 21:57:00 +11001031 if (he != obj) {
Herbert Xuca268932016-09-19 19:00:09 +08001032 struct rhlist_head __rcu **lpprev;
1033
Herbert Xu02fd97c2015-03-20 21:57:00 +11001034 pprev = &he->next;
Herbert Xuca268932016-09-19 19:00:09 +08001035
1036 if (!rhlist)
1037 continue;
1038
1039 do {
1040 lpprev = &list->next;
1041 list = rht_dereference_bucket(list->next,
1042 tbl, hash);
1043 } while (list && obj != &list->rhead);
1044
1045 if (!list)
1046 continue;
1047
1048 list = rht_dereference_bucket(list->next, tbl, hash);
1049 RCU_INIT_POINTER(*lpprev, list);
1050 err = 0;
1051 break;
Herbert Xu02fd97c2015-03-20 21:57:00 +11001052 }
1053
Herbert Xuca268932016-09-19 19:00:09 +08001054 obj = rht_dereference_bucket(obj->next, tbl, hash);
1055 err = 1;
1056
1057 if (rhlist) {
1058 list = rht_dereference_bucket(list->next, tbl, hash);
1059 if (list) {
1060 RCU_INIT_POINTER(list->rhead.next, obj);
1061 obj = &list->rhead;
1062 err = 0;
1063 }
1064 }
1065
1066 rcu_assign_pointer(*pprev, obj);
Herbert Xu02fd97c2015-03-20 21:57:00 +11001067 break;
1068 }
1069
1070 spin_unlock_bh(lock);
1071
Herbert Xuca268932016-09-19 19:00:09 +08001072 if (err > 0) {
1073 atomic_dec(&ht->nelems);
1074 if (unlikely(ht->p.automatic_shrinking &&
1075 rht_shrink_below_30(ht, tbl)))
1076 schedule_work(&ht->run_work);
1077 err = 0;
1078 }
1079
1080 return err;
1081}
1082
1083/* Internal function, please use rhashtable_remove_fast() instead */
1084static inline int __rhashtable_remove_fast(
1085 struct rhashtable *ht, struct rhash_head *obj,
1086 const struct rhashtable_params params, bool rhlist)
1087{
1088 struct bucket_table *tbl;
1089 int err;
1090
1091 rcu_read_lock();
1092
1093 tbl = rht_dereference_rcu(ht->tbl, ht);
1094
1095 /* Because we have already taken (and released) the bucket
1096 * lock in old_tbl, if we find that future_tbl is not yet
1097 * visible then that guarantees the entry to still be in
1098 * the old tbl if it exists.
1099 */
1100 while ((err = __rhashtable_remove_fast_one(ht, tbl, obj, params,
1101 rhlist)) &&
1102 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
1103 ;
1104
1105 rcu_read_unlock();
1106
Herbert Xu02fd97c2015-03-20 21:57:00 +11001107 return err;
1108}
1109
1110/**
1111 * rhashtable_remove_fast - remove object from hash table
1112 * @ht: hash table
1113 * @obj: pointer to hash head inside object
1114 * @params: hash table parameters
1115 *
1116 * Since the hash chain is single linked, the removal operation needs to
1117 * walk the bucket chain upon removal. The removal operation is thus
1118 * considerable slow if the hash table is not correctly sized.
1119 *
1120 * Will automatically shrink the table via rhashtable_expand() if the
1121 * shrink_decision function specified at rhashtable_init() returns true.
1122 *
1123 * Returns zero on success, -ENOENT if the entry could not be found.
1124 */
1125static inline int rhashtable_remove_fast(
1126 struct rhashtable *ht, struct rhash_head *obj,
1127 const struct rhashtable_params params)
1128{
Herbert Xuca268932016-09-19 19:00:09 +08001129 return __rhashtable_remove_fast(ht, obj, params, false);
1130}
Herbert Xu02fd97c2015-03-20 21:57:00 +11001131
Herbert Xuca268932016-09-19 19:00:09 +08001132/**
1133 * rhltable_remove - remove object from hash list table
1134 * @hlt: hash list table
1135 * @list: pointer to hash list head inside object
1136 * @params: hash table parameters
1137 *
1138 * Since the hash chain is single linked, the removal operation needs to
1139 * walk the bucket chain upon removal. The removal operation is thus
1140 * considerable slow if the hash table is not correctly sized.
1141 *
1142 * Will automatically shrink the table via rhashtable_expand() if the
1143 * shrink_decision function specified at rhashtable_init() returns true.
1144 *
1145 * Returns zero on success, -ENOENT if the entry could not be found.
1146 */
1147static inline int rhltable_remove(
1148 struct rhltable *hlt, struct rhlist_head *list,
1149 const struct rhashtable_params params)
1150{
1151 return __rhashtable_remove_fast(&hlt->ht, &list->rhead, params, true);
Herbert Xu02fd97c2015-03-20 21:57:00 +11001152}
1153
Tom Herbert3502cad2015-12-15 15:41:36 -08001154/* Internal function, please use rhashtable_replace_fast() instead */
1155static inline int __rhashtable_replace_fast(
1156 struct rhashtable *ht, struct bucket_table *tbl,
1157 struct rhash_head *obj_old, struct rhash_head *obj_new,
1158 const struct rhashtable_params params)
1159{
1160 struct rhash_head __rcu **pprev;
1161 struct rhash_head *he;
1162 spinlock_t *lock;
1163 unsigned int hash;
1164 int err = -ENOENT;
1165
1166 /* Minimally, the old and new objects must have same hash
1167 * (which should mean identifiers are the same).
1168 */
1169 hash = rht_head_hashfn(ht, tbl, obj_old, params);
1170 if (hash != rht_head_hashfn(ht, tbl, obj_new, params))
1171 return -EINVAL;
1172
1173 lock = rht_bucket_lock(tbl, hash);
1174
1175 spin_lock_bh(lock);
1176
Herbert Xuda204202017-02-11 19:26:47 +08001177 pprev = rht_bucket_var(tbl, hash);
1178 rht_for_each_continue(he, *pprev, tbl, hash) {
Tom Herbert3502cad2015-12-15 15:41:36 -08001179 if (he != obj_old) {
1180 pprev = &he->next;
1181 continue;
1182 }
1183
1184 rcu_assign_pointer(obj_new->next, obj_old->next);
1185 rcu_assign_pointer(*pprev, obj_new);
1186 err = 0;
1187 break;
1188 }
1189
1190 spin_unlock_bh(lock);
1191
1192 return err;
1193}
1194
1195/**
1196 * rhashtable_replace_fast - replace an object in hash table
1197 * @ht: hash table
1198 * @obj_old: pointer to hash head inside object being replaced
1199 * @obj_new: pointer to hash head inside object which is new
1200 * @params: hash table parameters
1201 *
1202 * Replacing an object doesn't affect the number of elements in the hash table
1203 * or bucket, so we don't need to worry about shrinking or expanding the
1204 * table here.
1205 *
1206 * Returns zero on success, -ENOENT if the entry could not be found,
1207 * -EINVAL if hash is not the same for the old and new objects.
1208 */
1209static inline int rhashtable_replace_fast(
1210 struct rhashtable *ht, struct rhash_head *obj_old,
1211 struct rhash_head *obj_new,
1212 const struct rhashtable_params params)
1213{
1214 struct bucket_table *tbl;
1215 int err;
1216
1217 rcu_read_lock();
1218
1219 tbl = rht_dereference_rcu(ht->tbl, ht);
1220
1221 /* Because we have already taken (and released) the bucket
1222 * lock in old_tbl, if we find that future_tbl is not yet
1223 * visible then that guarantees the entry to still be in
1224 * the old tbl if it exists.
1225 */
1226 while ((err = __rhashtable_replace_fast(ht, tbl, obj_old,
1227 obj_new, params)) &&
1228 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
1229 ;
1230
1231 rcu_read_unlock();
1232
1233 return err;
1234}
1235
Herbert Xu246779d2016-08-18 16:50:56 +08001236/* Obsolete function, do not use in new code. */
1237static inline int rhashtable_walk_init(struct rhashtable *ht,
1238 struct rhashtable_iter *iter, gfp_t gfp)
1239{
1240 rhashtable_walk_enter(ht, iter);
1241 return 0;
1242}
1243
Herbert Xuca268932016-09-19 19:00:09 +08001244/**
1245 * rhltable_walk_enter - Initialise an iterator
1246 * @hlt: Table to walk over
1247 * @iter: Hash table Iterator
1248 *
1249 * This function prepares a hash table walk.
1250 *
1251 * Note that if you restart a walk after rhashtable_walk_stop you
1252 * may see the same object twice. Also, you may miss objects if
1253 * there are removals in between rhashtable_walk_stop and the next
1254 * call to rhashtable_walk_start.
1255 *
1256 * For a completely stable walk you should construct your own data
1257 * structure outside the hash table.
1258 *
1259 * This function may sleep so you must not call it from interrupt
1260 * context or with spin locks held.
1261 *
1262 * You must call rhashtable_walk_exit after this function returns.
1263 */
1264static inline void rhltable_walk_enter(struct rhltable *hlt,
1265 struct rhashtable_iter *iter)
1266{
1267 return rhashtable_walk_enter(&hlt->ht, iter);
1268}
1269
1270/**
1271 * rhltable_free_and_destroy - free elements and destroy hash list table
1272 * @hlt: the hash list table to destroy
1273 * @free_fn: callback to release resources of element
1274 * @arg: pointer passed to free_fn
1275 *
1276 * See documentation for rhashtable_free_and_destroy.
1277 */
1278static inline void rhltable_free_and_destroy(struct rhltable *hlt,
1279 void (*free_fn)(void *ptr,
1280 void *arg),
1281 void *arg)
1282{
1283 return rhashtable_free_and_destroy(&hlt->ht, free_fn, arg);
1284}
1285
1286static inline void rhltable_destroy(struct rhltable *hlt)
1287{
1288 return rhltable_free_and_destroy(hlt, NULL, NULL);
1289}
1290
Thomas Graf7e1e7762014-08-02 11:47:44 +02001291#endif /* _LINUX_RHASHTABLE_H */