blob: 26b7a059c65e9e296e72e0f7e2eb2312339288d3 [file] [log] [blame]
Thomas Graf7e1e7762014-08-02 11:47:44 +02001/*
2 * Resizable, Scalable, Concurrent Hash Table
3 *
Herbert Xudc0ee262015-03-20 21:57:06 +11004 * Copyright (c) 2015 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>
Herbert Xu02fd97c2015-03-20 21:57:00 +110028#include <linux/rcupdate.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
Thomas Graf7e1e7762014-08-02 11:47:44 +020052struct rhash_head {
Thomas Graf5300fdc2014-08-13 16:38:29 +020053 struct rhash_head __rcu *next;
Thomas Graf7e1e7762014-08-02 11:47:44 +020054};
55
Thomas Graf97defe12015-01-02 23:00:20 +010056/**
57 * struct bucket_table - Table of hash buckets
58 * @size: Number of hash buckets
Herbert Xu63d512d2015-03-14 13:57:24 +110059 * @rehash: Current bucket being rehashed
Herbert Xu988dfbd2015-03-10 09:27:55 +110060 * @hash_rnd: Random seed to fold into hash
Thomas Graf97defe12015-01-02 23:00:20 +010061 * @locks_mask: Mask to apply before accessing locks[]
62 * @locks: Array of spinlocks protecting individual buckets
Herbert Xueddee5ba2015-03-14 13:57:20 +110063 * @walkers: List of active walkers
Herbert Xu9d901bc2015-03-14 13:57:23 +110064 * @rcu: RCU structure for freeing the table
Herbert Xuc4db8842015-03-14 13:57:25 +110065 * @future_tbl: Table under construction during rehashing
Thomas Graf97defe12015-01-02 23:00:20 +010066 * @buckets: size * hash buckets
67 */
Thomas Graf7e1e7762014-08-02 11:47:44 +020068struct bucket_table {
Herbert Xu63d512d2015-03-14 13:57:24 +110069 unsigned int size;
70 unsigned int rehash;
Herbert Xu988dfbd2015-03-10 09:27:55 +110071 u32 hash_rnd;
Eric Dumazetb9ebafb2015-02-20 06:48:57 -080072 unsigned int locks_mask;
73 spinlock_t *locks;
Herbert Xueddee5ba2015-03-14 13:57:20 +110074 struct list_head walkers;
Herbert Xu9d901bc2015-03-14 13:57:23 +110075 struct rcu_head rcu;
Eric Dumazetb9ebafb2015-02-20 06:48:57 -080076
Herbert Xuc4db8842015-03-14 13:57:25 +110077 struct bucket_table __rcu *future_tbl;
78
Eric Dumazetb9ebafb2015-02-20 06:48:57 -080079 struct rhash_head __rcu *buckets[] ____cacheline_aligned_in_smp;
Thomas Graf7e1e7762014-08-02 11:47:44 +020080};
81
Herbert Xu02fd97c2015-03-20 21:57:00 +110082/**
83 * struct rhashtable_compare_arg - Key for the function rhashtable_compare
84 * @ht: Hash table
85 * @key: Key to compare against
86 */
87struct rhashtable_compare_arg {
88 struct rhashtable *ht;
89 const void *key;
90};
91
Thomas Graf7e1e7762014-08-02 11:47:44 +020092typedef u32 (*rht_hashfn_t)(const void *data, u32 len, u32 seed);
Patrick McHardy49f7b332015-03-25 13:07:45 +000093typedef u32 (*rht_obj_hashfn_t)(const void *data, u32 len, u32 seed);
Herbert Xu02fd97c2015-03-20 21:57:00 +110094typedef int (*rht_obj_cmpfn_t)(struct rhashtable_compare_arg *arg,
95 const void *obj);
Thomas Graf7e1e7762014-08-02 11:47:44 +020096
97struct rhashtable;
98
99/**
100 * struct rhashtable_params - Hash table construction parameters
101 * @nelem_hint: Hint on number of elements, should be 75% of desired size
102 * @key_len: Length of key
103 * @key_offset: Offset of key in struct to be hashed
104 * @head_offset: Offset of rhash_head in struct to be hashed
Herbert Xu07ee0722015-05-15 11:30:47 +0800105 * @insecure_max_entries: Maximum number of entries (may be exceeded)
Herbert Xuc2e213c2015-03-18 20:01:16 +1100106 * @max_size: Maximum size while expanding
107 * @min_size: Minimum size while shrinking
Thomas Graff89bd6f2015-01-02 23:00:21 +0100108 * @nulls_base: Base value to generate nulls marker
Herbert Xuccd57b12015-03-24 00:50:28 +1100109 * @insecure_elasticity: Set to true to disable chain length checks
Thomas Grafb5e2c152015-03-24 20:42:19 +0000110 * @automatic_shrinking: Enable automatic shrinking of tables
Thomas Graf97defe12015-01-02 23:00:20 +0100111 * @locks_mul: Number of bucket locks to allocate per cpu (default: 128)
Herbert Xu31ccde22015-03-24 00:50:21 +1100112 * @hashfn: Hash function (default: jhash2 if !(key_len % 4), or jhash)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200113 * @obj_hashfn: Function to hash object
Herbert Xu02fd97c2015-03-20 21:57:00 +1100114 * @obj_cmpfn: Function to compare key with object
Thomas Graf7e1e7762014-08-02 11:47:44 +0200115 */
116struct rhashtable_params {
117 size_t nelem_hint;
118 size_t key_len;
119 size_t key_offset;
120 size_t head_offset;
Herbert Xu07ee0722015-05-15 11:30:47 +0800121 unsigned int insecure_max_entries;
Herbert Xuc2e213c2015-03-18 20:01:16 +1100122 unsigned int max_size;
123 unsigned int min_size;
Thomas Graff89bd6f2015-01-02 23:00:21 +0100124 u32 nulls_base;
Herbert Xuccd57b12015-03-24 00:50:28 +1100125 bool insecure_elasticity;
Thomas Grafb5e2c152015-03-24 20:42:19 +0000126 bool automatic_shrinking;
Thomas Graf97defe12015-01-02 23:00:20 +0100127 size_t locks_mul;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200128 rht_hashfn_t hashfn;
129 rht_obj_hashfn_t obj_hashfn;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100130 rht_obj_cmpfn_t obj_cmpfn;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200131};
132
133/**
134 * struct rhashtable - Hash table handle
135 * @tbl: Bucket table
136 * @nelems: Number of elements in table
Herbert Xu31ccde22015-03-24 00:50:21 +1100137 * @key_len: Key length for hashfn
Herbert Xuccd57b12015-03-24 00:50:28 +1100138 * @elasticity: Maximum chain length before rehash
Thomas Graf7e1e7762014-08-02 11:47:44 +0200139 * @p: Configuration parameters
Thomas Graf97defe12015-01-02 23:00:20 +0100140 * @run_work: Deferred worker to expand/shrink asynchronously
141 * @mutex: Mutex to protect current/future table swapping
Herbert Xuba7c95e2015-03-24 09:53:17 +1100142 * @lock: Spin lock to protect walker list
Thomas Graf7e1e7762014-08-02 11:47:44 +0200143 */
144struct rhashtable {
145 struct bucket_table __rcu *tbl;
Thomas Graf97defe12015-01-02 23:00:20 +0100146 atomic_t nelems;
Herbert Xu31ccde22015-03-24 00:50:21 +1100147 unsigned int key_len;
Herbert Xuccd57b12015-03-24 00:50:28 +1100148 unsigned int elasticity;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200149 struct rhashtable_params p;
Ying Xue57699a42015-01-16 11:13:09 +0800150 struct work_struct run_work;
Thomas Graf97defe12015-01-02 23:00:20 +0100151 struct mutex mutex;
Herbert Xuba7c95e2015-03-24 09:53:17 +1100152 spinlock_t lock;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200153};
154
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100155/**
156 * struct rhashtable_walker - Hash table walker
157 * @list: List entry on list of walkers
Herbert Xueddee5ba2015-03-14 13:57:20 +1100158 * @tbl: The table that we were walking over
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100159 */
160struct rhashtable_walker {
161 struct list_head list;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100162 struct bucket_table *tbl;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100163};
164
165/**
166 * struct rhashtable_iter - Hash table iterator, fits into netlink cb
167 * @ht: Table to iterate through
168 * @p: Current pointer
169 * @walker: Associated rhashtable walker
170 * @slot: Current slot
171 * @skip: Number of entries to skip in slot
172 */
173struct rhashtable_iter {
174 struct rhashtable *ht;
175 struct rhash_head *p;
176 struct rhashtable_walker *walker;
177 unsigned int slot;
178 unsigned int skip;
179};
180
Thomas Graff89bd6f2015-01-02 23:00:21 +0100181static inline unsigned long rht_marker(const struct rhashtable *ht, u32 hash)
182{
183 return NULLS_MARKER(ht->p.nulls_base + hash);
184}
185
186#define INIT_RHT_NULLS_HEAD(ptr, ht, hash) \
187 ((ptr) = (typeof(ptr)) rht_marker(ht, hash))
188
189static inline bool rht_is_a_nulls(const struct rhash_head *ptr)
190{
191 return ((unsigned long) ptr & 1);
192}
193
194static inline unsigned long rht_get_nulls_value(const struct rhash_head *ptr)
195{
196 return ((unsigned long) ptr) >> 1;
197}
198
Herbert Xu02fd97c2015-03-20 21:57:00 +1100199static inline void *rht_obj(const struct rhashtable *ht,
200 const struct rhash_head *he)
201{
202 return (char *)he - ht->p.head_offset;
203}
204
205static inline unsigned int rht_bucket_index(const struct bucket_table *tbl,
206 unsigned int hash)
207{
208 return (hash >> RHT_HASH_RESERVED_SPACE) & (tbl->size - 1);
209}
210
211static inline unsigned int rht_key_hashfn(
212 struct rhashtable *ht, const struct bucket_table *tbl,
213 const void *key, const struct rhashtable_params params)
214{
Thomas Graf299e5c32015-03-24 14:18:17 +0100215 unsigned int hash;
Herbert Xude91b252015-03-24 00:50:20 +1100216
Herbert Xu31ccde22015-03-24 00:50:21 +1100217 /* params must be equal to ht->p if it isn't constant. */
218 if (!__builtin_constant_p(params.key_len))
219 hash = ht->p.hashfn(key, ht->key_len, tbl->hash_rnd);
220 else if (params.key_len) {
Thomas Graf299e5c32015-03-24 14:18:17 +0100221 unsigned int key_len = params.key_len;
Herbert Xu31ccde22015-03-24 00:50:21 +1100222
223 if (params.hashfn)
224 hash = params.hashfn(key, key_len, tbl->hash_rnd);
225 else if (key_len & (sizeof(u32) - 1))
226 hash = jhash(key, key_len, tbl->hash_rnd);
227 else
228 hash = jhash2(key, key_len / sizeof(u32),
229 tbl->hash_rnd);
230 } else {
Thomas Graf299e5c32015-03-24 14:18:17 +0100231 unsigned int key_len = ht->p.key_len;
Herbert Xu31ccde22015-03-24 00:50:21 +1100232
233 if (params.hashfn)
234 hash = params.hashfn(key, key_len, tbl->hash_rnd);
235 else
236 hash = jhash(key, key_len, tbl->hash_rnd);
237 }
238
239 return rht_bucket_index(tbl, hash);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100240}
241
242static inline unsigned int rht_head_hashfn(
243 struct rhashtable *ht, const struct bucket_table *tbl,
244 const struct rhash_head *he, const struct rhashtable_params params)
245{
246 const char *ptr = rht_obj(ht, he);
247
248 return likely(params.obj_hashfn) ?
Patrick McHardy49f7b332015-03-25 13:07:45 +0000249 rht_bucket_index(tbl, params.obj_hashfn(ptr, params.key_len ?:
250 ht->p.key_len,
251 tbl->hash_rnd)) :
Herbert Xu02fd97c2015-03-20 21:57:00 +1100252 rht_key_hashfn(ht, tbl, ptr + params.key_offset, params);
253}
254
255/**
256 * rht_grow_above_75 - returns true if nelems > 0.75 * table-size
257 * @ht: hash table
258 * @tbl: current table
259 */
260static inline bool rht_grow_above_75(const struct rhashtable *ht,
261 const struct bucket_table *tbl)
262{
263 /* Expand table when exceeding 75% load */
264 return atomic_read(&ht->nelems) > (tbl->size / 4 * 3) &&
265 (!ht->p.max_size || tbl->size < ht->p.max_size);
266}
267
268/**
269 * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size
270 * @ht: hash table
271 * @tbl: current table
272 */
273static inline bool rht_shrink_below_30(const struct rhashtable *ht,
274 const struct bucket_table *tbl)
275{
276 /* Shrink table beneath 30% load */
277 return atomic_read(&ht->nelems) < (tbl->size * 3 / 10) &&
278 tbl->size > ht->p.min_size;
279}
280
Herbert Xuccd57b12015-03-24 00:50:28 +1100281/**
282 * rht_grow_above_100 - returns true if nelems > table-size
283 * @ht: hash table
284 * @tbl: current table
285 */
286static inline bool rht_grow_above_100(const struct rhashtable *ht,
287 const struct bucket_table *tbl)
288{
Johannes Berg1d8dc3d2015-04-23 16:38:43 +0200289 return atomic_read(&ht->nelems) > tbl->size &&
290 (!ht->p.max_size || tbl->size < ht->p.max_size);
Herbert Xuccd57b12015-03-24 00:50:28 +1100291}
292
Herbert Xu07ee0722015-05-15 11:30:47 +0800293/**
294 * rht_grow_above_max - returns true if table is above maximum
295 * @ht: hash table
296 * @tbl: current table
297 */
298static inline bool rht_grow_above_max(const struct rhashtable *ht,
299 const struct bucket_table *tbl)
300{
301 return ht->p.insecure_max_entries &&
302 atomic_read(&ht->nelems) >= ht->p.insecure_max_entries;
303}
304
Herbert Xu02fd97c2015-03-20 21:57:00 +1100305/* The bucket lock is selected based on the hash and protects mutations
306 * on a group of hash buckets.
307 *
308 * A maximum of tbl->size/2 bucket locks is allocated. This ensures that
309 * a single lock always covers both buckets which may both contains
310 * entries which link to the same bucket of the old table during resizing.
311 * This allows to simplify the locking as locking the bucket in both
312 * tables during resize always guarantee protection.
313 *
314 * IMPORTANT: When holding the bucket lock of both the old and new table
315 * during expansions and shrinking, the old bucket lock must always be
316 * acquired first.
317 */
318static inline spinlock_t *rht_bucket_lock(const struct bucket_table *tbl,
319 unsigned int hash)
320{
321 return &tbl->locks[hash & tbl->locks_mask];
322}
323
Thomas Graf7e1e7762014-08-02 11:47:44 +0200324#ifdef CONFIG_PROVE_LOCKING
Thomas Graf97defe12015-01-02 23:00:20 +0100325int lockdep_rht_mutex_is_held(struct rhashtable *ht);
Thomas Graf88d6ed12015-01-02 23:00:16 +0100326int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200327#else
Thomas Graf97defe12015-01-02 23:00:20 +0100328static inline int lockdep_rht_mutex_is_held(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200329{
330 return 1;
331}
Thomas Graf88d6ed12015-01-02 23:00:16 +0100332
333static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl,
334 u32 hash)
335{
336 return 1;
337}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200338#endif /* CONFIG_PROVE_LOCKING */
339
Herbert Xu488fb86e2015-03-20 21:56:59 +1100340int rhashtable_init(struct rhashtable *ht,
341 const struct rhashtable_params *params);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200342
Herbert Xu3cf92222015-12-03 20:41:29 +0800343struct bucket_table *rhashtable_insert_slow(struct rhashtable *ht,
344 const void *key,
345 struct rhash_head *obj,
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200346 struct bucket_table *old_tbl,
347 void **data);
Herbert Xu3cf92222015-12-03 20:41:29 +0800348int rhashtable_insert_rehash(struct rhashtable *ht, struct bucket_table *tbl);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200349
Bob Copeland8f6fd832016-03-02 10:09:19 -0500350int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter,
351 gfp_t gfp);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100352void rhashtable_walk_exit(struct rhashtable_iter *iter);
353int rhashtable_walk_start(struct rhashtable_iter *iter) __acquires(RCU);
354void *rhashtable_walk_next(struct rhashtable_iter *iter);
355void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU);
356
Thomas Graf6b6f3022015-03-24 14:18:20 +0100357void rhashtable_free_and_destroy(struct rhashtable *ht,
358 void (*free_fn)(void *ptr, void *arg),
359 void *arg);
Thomas Graf97defe12015-01-02 23:00:20 +0100360void rhashtable_destroy(struct rhashtable *ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200361
362#define rht_dereference(p, ht) \
363 rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht))
364
365#define rht_dereference_rcu(p, ht) \
366 rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht))
367
Thomas Graf88d6ed12015-01-02 23:00:16 +0100368#define rht_dereference_bucket(p, tbl, hash) \
369 rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200370
Thomas Graf88d6ed12015-01-02 23:00:16 +0100371#define rht_dereference_bucket_rcu(p, tbl, hash) \
372 rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash))
373
374#define rht_entry(tpos, pos, member) \
375 ({ tpos = container_of(pos, typeof(*tpos), member); 1; })
376
377/**
378 * rht_for_each_continue - continue iterating over hash chain
379 * @pos: the &struct rhash_head to use as a loop cursor.
380 * @head: the previous &struct rhash_head to continue from
381 * @tbl: the &struct bucket_table
382 * @hash: the hash value / bucket index
383 */
384#define rht_for_each_continue(pos, head, tbl, hash) \
385 for (pos = rht_dereference_bucket(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100386 !rht_is_a_nulls(pos); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100387 pos = rht_dereference_bucket((pos)->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200388
389/**
390 * rht_for_each - iterate over hash chain
Thomas Graf88d6ed12015-01-02 23:00:16 +0100391 * @pos: the &struct rhash_head to use as a loop cursor.
392 * @tbl: the &struct bucket_table
393 * @hash: the hash value / bucket index
Thomas Graf7e1e7762014-08-02 11:47:44 +0200394 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100395#define rht_for_each(pos, tbl, hash) \
396 rht_for_each_continue(pos, (tbl)->buckets[hash], tbl, hash)
397
398/**
399 * rht_for_each_entry_continue - continue iterating over hash chain
400 * @tpos: the type * to use as a loop cursor.
401 * @pos: the &struct rhash_head to use as a loop cursor.
402 * @head: the previous &struct rhash_head to continue from
403 * @tbl: the &struct bucket_table
404 * @hash: the hash value / bucket index
405 * @member: name of the &struct rhash_head within the hashable struct.
406 */
407#define rht_for_each_entry_continue(tpos, pos, head, tbl, hash, member) \
408 for (pos = rht_dereference_bucket(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100409 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100410 pos = rht_dereference_bucket((pos)->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200411
412/**
413 * rht_for_each_entry - iterate over hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100414 * @tpos: the type * to use as a loop cursor.
415 * @pos: the &struct rhash_head to use as a loop cursor.
416 * @tbl: the &struct bucket_table
417 * @hash: the hash value / bucket index
418 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200419 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100420#define rht_for_each_entry(tpos, pos, tbl, hash, member) \
421 rht_for_each_entry_continue(tpos, pos, (tbl)->buckets[hash], \
422 tbl, hash, member)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200423
424/**
425 * rht_for_each_entry_safe - safely iterate over hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100426 * @tpos: the type * to use as a loop cursor.
427 * @pos: the &struct rhash_head to use as a loop cursor.
428 * @next: the &struct rhash_head to use as next in loop cursor.
429 * @tbl: the &struct bucket_table
430 * @hash: the hash value / bucket index
431 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200432 *
433 * This hash chain list-traversal primitive allows for the looped code to
434 * remove the loop cursor from the list.
435 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100436#define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \
437 for (pos = rht_dereference_bucket((tbl)->buckets[hash], tbl, hash), \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100438 next = !rht_is_a_nulls(pos) ? \
439 rht_dereference_bucket(pos->next, tbl, hash) : NULL; \
440 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
Patrick McHardy607954b2015-01-21 11:12:13 +0000441 pos = next, \
442 next = !rht_is_a_nulls(pos) ? \
443 rht_dereference_bucket(pos->next, tbl, hash) : NULL)
Thomas Graf88d6ed12015-01-02 23:00:16 +0100444
445/**
446 * rht_for_each_rcu_continue - continue iterating over rcu hash chain
447 * @pos: the &struct rhash_head to use as a loop cursor.
448 * @head: the previous &struct rhash_head to continue from
449 * @tbl: the &struct bucket_table
450 * @hash: the hash value / bucket index
451 *
452 * This hash chain list-traversal primitive may safely run concurrently with
453 * the _rcu mutation primitives such as rhashtable_insert() as long as the
454 * traversal is guarded by rcu_read_lock().
455 */
456#define rht_for_each_rcu_continue(pos, head, tbl, hash) \
457 for (({barrier(); }), \
458 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100459 !rht_is_a_nulls(pos); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100460 pos = rcu_dereference_raw(pos->next))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200461
462/**
463 * rht_for_each_rcu - iterate over rcu hash chain
Thomas Graf88d6ed12015-01-02 23:00:16 +0100464 * @pos: the &struct rhash_head to use as a loop cursor.
465 * @tbl: the &struct bucket_table
466 * @hash: the hash value / bucket index
Thomas Graf7e1e7762014-08-02 11:47:44 +0200467 *
468 * This hash chain list-traversal primitive may safely run concurrently with
Thomas Graf88d6ed12015-01-02 23:00:16 +0100469 * the _rcu mutation primitives such as rhashtable_insert() as long as the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200470 * traversal is guarded by rcu_read_lock().
471 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100472#define rht_for_each_rcu(pos, tbl, hash) \
473 rht_for_each_rcu_continue(pos, (tbl)->buckets[hash], tbl, hash)
474
475/**
476 * rht_for_each_entry_rcu_continue - continue iterating over rcu hash chain
477 * @tpos: the type * to use as a loop cursor.
478 * @pos: the &struct rhash_head to use as a loop cursor.
479 * @head: the previous &struct rhash_head to continue from
480 * @tbl: the &struct bucket_table
481 * @hash: the hash value / bucket index
482 * @member: name of the &struct rhash_head within the hashable struct.
483 *
484 * This hash chain list-traversal primitive may safely run concurrently with
485 * the _rcu mutation primitives such as rhashtable_insert() as long as the
486 * traversal is guarded by rcu_read_lock().
487 */
488#define rht_for_each_entry_rcu_continue(tpos, pos, head, tbl, hash, member) \
489 for (({barrier(); }), \
490 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100491 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100492 pos = rht_dereference_bucket_rcu(pos->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200493
494/**
495 * rht_for_each_entry_rcu - iterate over rcu hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100496 * @tpos: the type * to use as a loop cursor.
497 * @pos: the &struct rhash_head to use as a loop cursor.
498 * @tbl: the &struct bucket_table
499 * @hash: the hash value / bucket index
500 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200501 *
502 * This hash chain list-traversal primitive may safely run concurrently with
Thomas Graf88d6ed12015-01-02 23:00:16 +0100503 * the _rcu mutation primitives such as rhashtable_insert() as long as the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200504 * traversal is guarded by rcu_read_lock().
505 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100506#define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \
507 rht_for_each_entry_rcu_continue(tpos, pos, (tbl)->buckets[hash],\
508 tbl, hash, member)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200509
Herbert Xu02fd97c2015-03-20 21:57:00 +1100510static inline int rhashtable_compare(struct rhashtable_compare_arg *arg,
511 const void *obj)
512{
513 struct rhashtable *ht = arg->ht;
514 const char *ptr = obj;
515
516 return memcmp(ptr + ht->p.key_offset, arg->key, ht->p.key_len);
517}
518
519/**
520 * rhashtable_lookup_fast - search hash table, inlined version
521 * @ht: hash table
522 * @key: the pointer to the key
523 * @params: hash table parameters
524 *
525 * Computes the hash value for the key and traverses the bucket chain looking
526 * for a entry with an identical key. The first matching entry is returned.
527 *
528 * Returns the first entry on which the compare function returned true.
529 */
530static inline void *rhashtable_lookup_fast(
531 struct rhashtable *ht, const void *key,
532 const struct rhashtable_params params)
533{
534 struct rhashtable_compare_arg arg = {
535 .ht = ht,
536 .key = key,
537 };
538 const struct bucket_table *tbl;
539 struct rhash_head *he;
Thomas Graf299e5c32015-03-24 14:18:17 +0100540 unsigned int hash;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100541
542 rcu_read_lock();
543
544 tbl = rht_dereference_rcu(ht->tbl, ht);
545restart:
546 hash = rht_key_hashfn(ht, tbl, key, params);
547 rht_for_each_rcu(he, tbl, hash) {
548 if (params.obj_cmpfn ?
549 params.obj_cmpfn(&arg, rht_obj(ht, he)) :
550 rhashtable_compare(&arg, rht_obj(ht, he)))
551 continue;
552 rcu_read_unlock();
553 return rht_obj(ht, he);
554 }
555
556 /* Ensure we see any new tables. */
557 smp_rmb();
558
559 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
560 if (unlikely(tbl))
561 goto restart;
562 rcu_read_unlock();
563
564 return NULL;
565}
566
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200567/* Internal function, please use rhashtable_insert_fast() instead. This
568 * function returns the existing element already in hashes in there is a clash,
569 * otherwise it returns an error via ERR_PTR().
570 */
571static inline void *__rhashtable_insert_fast(
Herbert Xu02fd97c2015-03-20 21:57:00 +1100572 struct rhashtable *ht, const void *key, struct rhash_head *obj,
573 const struct rhashtable_params params)
574{
575 struct rhashtable_compare_arg arg = {
576 .ht = ht,
577 .key = key,
578 };
Herbert Xu02fd97c2015-03-20 21:57:00 +1100579 struct bucket_table *tbl, *new_tbl;
580 struct rhash_head *head;
581 spinlock_t *lock;
Thomas Graf299e5c32015-03-24 14:18:17 +0100582 unsigned int elasticity;
583 unsigned int hash;
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200584 void *data = NULL;
Herbert Xuccd57b12015-03-24 00:50:28 +1100585 int err;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100586
Herbert Xuccd57b12015-03-24 00:50:28 +1100587restart:
Herbert Xu02fd97c2015-03-20 21:57:00 +1100588 rcu_read_lock();
589
590 tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100591
Herbert Xub8244782015-03-24 00:50:26 +1100592 /* All insertions must grab the oldest table containing
593 * the hashed bucket that is yet to be rehashed.
Herbert Xu02fd97c2015-03-20 21:57:00 +1100594 */
Herbert Xub8244782015-03-24 00:50:26 +1100595 for (;;) {
596 hash = rht_head_hashfn(ht, tbl, obj, params);
597 lock = rht_bucket_lock(tbl, hash);
598 spin_lock_bh(lock);
599
600 if (tbl->rehash <= hash)
601 break;
602
603 spin_unlock_bh(lock);
604 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
605 }
606
Herbert Xu02fd97c2015-03-20 21:57:00 +1100607 new_tbl = rht_dereference_rcu(tbl->future_tbl, ht);
608 if (unlikely(new_tbl)) {
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200609 tbl = rhashtable_insert_slow(ht, key, obj, new_tbl, &data);
Herbert Xu3cf92222015-12-03 20:41:29 +0800610 if (!IS_ERR_OR_NULL(tbl))
Herbert Xuccd57b12015-03-24 00:50:28 +1100611 goto slow_path;
Herbert Xu3cf92222015-12-03 20:41:29 +0800612
613 err = PTR_ERR(tbl);
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200614 if (err == -EEXIST)
615 err = 0;
616
Herbert Xu02fd97c2015-03-20 21:57:00 +1100617 goto out;
618 }
619
Herbert Xu07ee0722015-05-15 11:30:47 +0800620 err = -E2BIG;
621 if (unlikely(rht_grow_above_max(ht, tbl)))
622 goto out;
623
Herbert Xuccd57b12015-03-24 00:50:28 +1100624 if (unlikely(rht_grow_above_100(ht, tbl))) {
625slow_path:
626 spin_unlock_bh(lock);
Herbert Xu3cf92222015-12-03 20:41:29 +0800627 err = rhashtable_insert_rehash(ht, tbl);
Thomas Graf58be8a52015-03-24 14:18:16 +0100628 rcu_read_unlock();
Herbert Xuccd57b12015-03-24 00:50:28 +1100629 if (err)
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200630 return ERR_PTR(err);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100631
Herbert Xuccd57b12015-03-24 00:50:28 +1100632 goto restart;
633 }
634
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200635 err = 0;
Herbert Xuccd57b12015-03-24 00:50:28 +1100636 elasticity = ht->elasticity;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100637 rht_for_each(head, tbl, hash) {
Herbert Xuccd57b12015-03-24 00:50:28 +1100638 if (key &&
639 unlikely(!(params.obj_cmpfn ?
Herbert Xu02fd97c2015-03-20 21:57:00 +1100640 params.obj_cmpfn(&arg, rht_obj(ht, head)) :
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200641 rhashtable_compare(&arg, rht_obj(ht, head))))) {
642 data = rht_obj(ht, head);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100643 goto out;
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200644 }
Herbert Xuccd57b12015-03-24 00:50:28 +1100645 if (!--elasticity)
646 goto slow_path;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100647 }
648
Herbert Xu02fd97c2015-03-20 21:57:00 +1100649 head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash);
650
651 RCU_INIT_POINTER(obj->next, head);
652
653 rcu_assign_pointer(tbl->buckets[hash], obj);
654
655 atomic_inc(&ht->nelems);
656 if (rht_grow_above_75(ht, tbl))
657 schedule_work(&ht->run_work);
658
659out:
660 spin_unlock_bh(lock);
661 rcu_read_unlock();
662
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200663 return err ? ERR_PTR(err) : data;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100664}
665
666/**
667 * rhashtable_insert_fast - insert object into hash table
668 * @ht: hash table
669 * @obj: pointer to hash head inside object
670 * @params: hash table parameters
671 *
672 * Will take a per bucket spinlock to protect against mutual mutations
673 * on the same bucket. Multiple insertions may occur in parallel unless
674 * they map to the same bucket lock.
675 *
676 * It is safe to call this function from atomic context.
677 *
678 * Will trigger an automatic deferred table resizing if the size grows
679 * beyond the watermark indicated by grow_decision() which can be passed
680 * to rhashtable_init().
681 */
682static inline int rhashtable_insert_fast(
683 struct rhashtable *ht, struct rhash_head *obj,
684 const struct rhashtable_params params)
685{
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200686 void *ret;
687
688 ret = __rhashtable_insert_fast(ht, NULL, obj, params);
689 if (IS_ERR(ret))
690 return PTR_ERR(ret);
691
692 return ret == NULL ? 0 : -EEXIST;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100693}
694
695/**
696 * rhashtable_lookup_insert_fast - lookup and insert object into hash table
697 * @ht: hash table
698 * @obj: pointer to hash head inside object
699 * @params: hash table parameters
700 *
701 * Locks down the bucket chain in both the old and new table if a resize
702 * is in progress to ensure that writers can't remove from the old table
703 * and can't insert to the new table during the atomic operation of search
704 * and insertion. Searches for duplicates in both the old and new table if
705 * a resize is in progress.
706 *
707 * This lookup function may only be used for fixed key hash table (key_len
708 * parameter set). It will BUG() if used inappropriately.
709 *
710 * It is safe to call this function from atomic context.
711 *
712 * Will trigger an automatic deferred table resizing if the size grows
713 * beyond the watermark indicated by grow_decision() which can be passed
714 * to rhashtable_init().
715 */
716static inline int rhashtable_lookup_insert_fast(
717 struct rhashtable *ht, struct rhash_head *obj,
718 const struct rhashtable_params params)
719{
720 const char *key = rht_obj(ht, obj);
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200721 void *ret;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100722
723 BUG_ON(ht->p.obj_hashfn);
724
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200725 ret = __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params);
726 if (IS_ERR(ret))
727 return PTR_ERR(ret);
728
729 return ret == NULL ? 0 : -EEXIST;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100730}
731
732/**
733 * rhashtable_lookup_insert_key - search and insert object to hash table
734 * with explicit key
735 * @ht: hash table
736 * @key: key
737 * @obj: pointer to hash head inside object
738 * @params: hash table parameters
739 *
740 * Locks down the bucket chain in both the old and new table if a resize
741 * is in progress to ensure that writers can't remove from the old table
742 * and can't insert to the new table during the atomic operation of search
743 * and insertion. Searches for duplicates in both the old and new table if
744 * a resize is in progress.
745 *
746 * Lookups may occur in parallel with hashtable mutations and resizing.
747 *
748 * Will trigger an automatic deferred table resizing if the size grows
749 * beyond the watermark indicated by grow_decision() which can be passed
750 * to rhashtable_init().
751 *
752 * Returns zero on success.
753 */
754static inline int rhashtable_lookup_insert_key(
755 struct rhashtable *ht, const void *key, struct rhash_head *obj,
756 const struct rhashtable_params params)
757{
Pablo Neira Ayuso5ca8cc52016-08-24 12:31:31 +0200758 void *ret;
759
760 BUG_ON(!ht->p.obj_hashfn || !key);
761
762 ret = __rhashtable_insert_fast(ht, key, obj, params);
763 if (IS_ERR(ret))
764 return PTR_ERR(ret);
765
766 return ret == NULL ? 0 : -EEXIST;
767}
768
769/**
770 * rhashtable_lookup_get_insert_key - lookup and insert object into hash table
771 * @ht: hash table
772 * @obj: pointer to hash head inside object
773 * @params: hash table parameters
774 * @data: pointer to element data already in hashes
775 *
776 * Just like rhashtable_lookup_insert_key(), but this function returns the
777 * object if it exists, NULL if it does not and the insertion was successful,
778 * and an ERR_PTR otherwise.
779 */
780static inline void *rhashtable_lookup_get_insert_key(
781 struct rhashtable *ht, const void *key, struct rhash_head *obj,
782 const struct rhashtable_params params)
783{
Herbert Xu02fd97c2015-03-20 21:57:00 +1100784 BUG_ON(!ht->p.obj_hashfn || !key);
785
786 return __rhashtable_insert_fast(ht, key, obj, params);
787}
788
Thomas Grafac833bd2015-03-24 14:18:18 +0100789/* Internal function, please use rhashtable_remove_fast() instead */
Herbert Xu02fd97c2015-03-20 21:57:00 +1100790static inline int __rhashtable_remove_fast(
791 struct rhashtable *ht, struct bucket_table *tbl,
792 struct rhash_head *obj, const struct rhashtable_params params)
793{
794 struct rhash_head __rcu **pprev;
795 struct rhash_head *he;
796 spinlock_t * lock;
Thomas Graf299e5c32015-03-24 14:18:17 +0100797 unsigned int hash;
Herbert Xu02fd97c2015-03-20 21:57:00 +1100798 int err = -ENOENT;
799
800 hash = rht_head_hashfn(ht, tbl, obj, params);
801 lock = rht_bucket_lock(tbl, hash);
802
803 spin_lock_bh(lock);
804
805 pprev = &tbl->buckets[hash];
806 rht_for_each(he, tbl, hash) {
807 if (he != obj) {
808 pprev = &he->next;
809 continue;
810 }
811
812 rcu_assign_pointer(*pprev, obj->next);
813 err = 0;
814 break;
815 }
816
817 spin_unlock_bh(lock);
818
819 return err;
820}
821
822/**
823 * rhashtable_remove_fast - remove object from hash table
824 * @ht: hash table
825 * @obj: pointer to hash head inside object
826 * @params: hash table parameters
827 *
828 * Since the hash chain is single linked, the removal operation needs to
829 * walk the bucket chain upon removal. The removal operation is thus
830 * considerable slow if the hash table is not correctly sized.
831 *
832 * Will automatically shrink the table via rhashtable_expand() if the
833 * shrink_decision function specified at rhashtable_init() returns true.
834 *
835 * Returns zero on success, -ENOENT if the entry could not be found.
836 */
837static inline int rhashtable_remove_fast(
838 struct rhashtable *ht, struct rhash_head *obj,
839 const struct rhashtable_params params)
840{
841 struct bucket_table *tbl;
842 int err;
843
844 rcu_read_lock();
845
846 tbl = rht_dereference_rcu(ht->tbl, ht);
847
848 /* Because we have already taken (and released) the bucket
849 * lock in old_tbl, if we find that future_tbl is not yet
850 * visible then that guarantees the entry to still be in
851 * the old tbl if it exists.
852 */
853 while ((err = __rhashtable_remove_fast(ht, tbl, obj, params)) &&
854 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
855 ;
856
857 if (err)
858 goto out;
859
860 atomic_dec(&ht->nelems);
Thomas Grafb5e2c152015-03-24 20:42:19 +0000861 if (unlikely(ht->p.automatic_shrinking &&
862 rht_shrink_below_30(ht, tbl)))
Herbert Xu02fd97c2015-03-20 21:57:00 +1100863 schedule_work(&ht->run_work);
864
865out:
866 rcu_read_unlock();
867
868 return err;
869}
870
Tom Herbert3502cad2015-12-15 15:41:36 -0800871/* Internal function, please use rhashtable_replace_fast() instead */
872static inline int __rhashtable_replace_fast(
873 struct rhashtable *ht, struct bucket_table *tbl,
874 struct rhash_head *obj_old, struct rhash_head *obj_new,
875 const struct rhashtable_params params)
876{
877 struct rhash_head __rcu **pprev;
878 struct rhash_head *he;
879 spinlock_t *lock;
880 unsigned int hash;
881 int err = -ENOENT;
882
883 /* Minimally, the old and new objects must have same hash
884 * (which should mean identifiers are the same).
885 */
886 hash = rht_head_hashfn(ht, tbl, obj_old, params);
887 if (hash != rht_head_hashfn(ht, tbl, obj_new, params))
888 return -EINVAL;
889
890 lock = rht_bucket_lock(tbl, hash);
891
892 spin_lock_bh(lock);
893
894 pprev = &tbl->buckets[hash];
895 rht_for_each(he, tbl, hash) {
896 if (he != obj_old) {
897 pprev = &he->next;
898 continue;
899 }
900
901 rcu_assign_pointer(obj_new->next, obj_old->next);
902 rcu_assign_pointer(*pprev, obj_new);
903 err = 0;
904 break;
905 }
906
907 spin_unlock_bh(lock);
908
909 return err;
910}
911
912/**
913 * rhashtable_replace_fast - replace an object in hash table
914 * @ht: hash table
915 * @obj_old: pointer to hash head inside object being replaced
916 * @obj_new: pointer to hash head inside object which is new
917 * @params: hash table parameters
918 *
919 * Replacing an object doesn't affect the number of elements in the hash table
920 * or bucket, so we don't need to worry about shrinking or expanding the
921 * table here.
922 *
923 * Returns zero on success, -ENOENT if the entry could not be found,
924 * -EINVAL if hash is not the same for the old and new objects.
925 */
926static inline int rhashtable_replace_fast(
927 struct rhashtable *ht, struct rhash_head *obj_old,
928 struct rhash_head *obj_new,
929 const struct rhashtable_params params)
930{
931 struct bucket_table *tbl;
932 int err;
933
934 rcu_read_lock();
935
936 tbl = rht_dereference_rcu(ht->tbl, ht);
937
938 /* Because we have already taken (and released) the bucket
939 * lock in old_tbl, if we find that future_tbl is not yet
940 * visible then that guarantees the entry to still be in
941 * the old tbl if it exists.
942 */
943 while ((err = __rhashtable_replace_fast(ht, tbl, obj_old,
944 obj_new, params)) &&
945 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
946 ;
947
948 rcu_read_unlock();
949
950 return err;
951}
952
Thomas Graf7e1e7762014-08-02 11:47:44 +0200953#endif /* _LINUX_RHASHTABLE_H */