blob: 99425f2be7080123567223cf3727577d15ce29a0 [file] [log] [blame]
Thomas Graf7e1e7762014-08-02 11:47:44 +02001/*
2 * Resizable, Scalable, Concurrent Hash Table
3 *
4 * Copyright (c) 2014 Thomas Graf <tgraf@suug.ch>
5 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
6 *
7 * Based on the following paper by Josh Triplett, Paul E. McKenney
8 * and Jonathan Walpole:
9 * https://www.usenix.org/legacy/event/atc11/tech/final_files/Triplett.pdf
10 *
11 * Code partially derived from nft_hash
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
16 */
17
18#ifndef _LINUX_RHASHTABLE_H
19#define _LINUX_RHASHTABLE_H
20
Herbert Xuf2dba9c2015-02-04 07:33:23 +110021#include <linux/compiler.h>
Thomas Graff89bd6f2015-01-02 23:00:21 +010022#include <linux/list_nulls.h>
Thomas Graf97defe12015-01-02 23:00:20 +010023#include <linux/workqueue.h>
Ying Xue86b35b62015-01-04 15:25:09 +080024#include <linux/mutex.h>
Thomas Graf7e1e7762014-08-02 11:47:44 +020025
Thomas Graff89bd6f2015-01-02 23:00:21 +010026/*
27 * The end of the chain is marked with a special nulls marks which has
28 * the following format:
29 *
30 * +-------+-----------------------------------------------------+-+
31 * | Base | Hash |1|
32 * +-------+-----------------------------------------------------+-+
33 *
34 * Base (4 bits) : Reserved to distinguish between multiple tables.
35 * Specified via &struct rhashtable_params.nulls_base.
36 * Hash (27 bits): Full hash (unmasked) of first element added to bucket
37 * 1 (1 bit) : Nulls marker (always set)
38 *
39 * The remaining bits of the next pointer remain unused for now.
40 */
41#define RHT_BASE_BITS 4
42#define RHT_HASH_BITS 27
43#define RHT_BASE_SHIFT RHT_HASH_BITS
44
Thomas Graf7e1e7762014-08-02 11:47:44 +020045struct rhash_head {
Thomas Graf5300fdc2014-08-13 16:38:29 +020046 struct rhash_head __rcu *next;
Thomas Graf7e1e7762014-08-02 11:47:44 +020047};
48
Thomas Graf97defe12015-01-02 23:00:20 +010049/**
50 * struct bucket_table - Table of hash buckets
51 * @size: Number of hash buckets
Herbert Xu63d512d2015-03-14 13:57:24 +110052 * @rehash: Current bucket being rehashed
Herbert Xu988dfbd2015-03-10 09:27:55 +110053 * @hash_rnd: Random seed to fold into hash
Thomas Graf97defe12015-01-02 23:00:20 +010054 * @locks_mask: Mask to apply before accessing locks[]
55 * @locks: Array of spinlocks protecting individual buckets
Herbert Xueddee5ba2015-03-14 13:57:20 +110056 * @walkers: List of active walkers
Herbert Xu9d901bc2015-03-14 13:57:23 +110057 * @rcu: RCU structure for freeing the table
Herbert Xuc4db8842015-03-14 13:57:25 +110058 * @future_tbl: Table under construction during rehashing
Thomas Graf97defe12015-01-02 23:00:20 +010059 * @buckets: size * hash buckets
60 */
Thomas Graf7e1e7762014-08-02 11:47:44 +020061struct bucket_table {
Herbert Xu63d512d2015-03-14 13:57:24 +110062 unsigned int size;
63 unsigned int rehash;
Herbert Xu988dfbd2015-03-10 09:27:55 +110064 u32 hash_rnd;
Eric Dumazetb9ebafb2015-02-20 06:48:57 -080065 unsigned int locks_mask;
66 spinlock_t *locks;
Herbert Xueddee5ba2015-03-14 13:57:20 +110067 struct list_head walkers;
Herbert Xu9d901bc2015-03-14 13:57:23 +110068 struct rcu_head rcu;
Eric Dumazetb9ebafb2015-02-20 06:48:57 -080069
Herbert Xuc4db8842015-03-14 13:57:25 +110070 struct bucket_table __rcu *future_tbl;
71
Eric Dumazetb9ebafb2015-02-20 06:48:57 -080072 struct rhash_head __rcu *buckets[] ____cacheline_aligned_in_smp;
Thomas Graf7e1e7762014-08-02 11:47:44 +020073};
74
75typedef u32 (*rht_hashfn_t)(const void *data, u32 len, u32 seed);
76typedef u32 (*rht_obj_hashfn_t)(const void *data, u32 seed);
77
78struct rhashtable;
79
80/**
81 * struct rhashtable_params - Hash table construction parameters
82 * @nelem_hint: Hint on number of elements, should be 75% of desired size
83 * @key_len: Length of key
84 * @key_offset: Offset of key in struct to be hashed
85 * @head_offset: Offset of rhash_head in struct to be hashed
Herbert Xuc2e213c2015-03-18 20:01:16 +110086 * @max_size: Maximum size while expanding
87 * @min_size: Minimum size while shrinking
Thomas Graff89bd6f2015-01-02 23:00:21 +010088 * @nulls_base: Base value to generate nulls marker
Thomas Graf97defe12015-01-02 23:00:20 +010089 * @locks_mul: Number of bucket locks to allocate per cpu (default: 128)
Thomas Graf7e1e7762014-08-02 11:47:44 +020090 * @hashfn: Function to hash key
91 * @obj_hashfn: Function to hash object
Thomas Graf7e1e7762014-08-02 11:47:44 +020092 */
93struct rhashtable_params {
94 size_t nelem_hint;
95 size_t key_len;
96 size_t key_offset;
97 size_t head_offset;
Herbert Xuc2e213c2015-03-18 20:01:16 +110098 unsigned int max_size;
99 unsigned int min_size;
Thomas Graff89bd6f2015-01-02 23:00:21 +0100100 u32 nulls_base;
Thomas Graf97defe12015-01-02 23:00:20 +0100101 size_t locks_mul;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200102 rht_hashfn_t hashfn;
103 rht_obj_hashfn_t obj_hashfn;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200104};
105
106/**
107 * struct rhashtable - Hash table handle
108 * @tbl: Bucket table
109 * @nelems: Number of elements in table
Thomas Graf7e1e7762014-08-02 11:47:44 +0200110 * @p: Configuration parameters
Thomas Graf97defe12015-01-02 23:00:20 +0100111 * @run_work: Deferred worker to expand/shrink asynchronously
112 * @mutex: Mutex to protect current/future table swapping
113 * @being_destroyed: True if table is set up for destruction
Thomas Graf7e1e7762014-08-02 11:47:44 +0200114 */
115struct rhashtable {
116 struct bucket_table __rcu *tbl;
Thomas Graf97defe12015-01-02 23:00:20 +0100117 atomic_t nelems;
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100118 bool being_destroyed;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200119 struct rhashtable_params p;
Ying Xue57699a42015-01-16 11:13:09 +0800120 struct work_struct run_work;
Thomas Graf97defe12015-01-02 23:00:20 +0100121 struct mutex mutex;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200122};
123
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100124/**
125 * struct rhashtable_walker - Hash table walker
126 * @list: List entry on list of walkers
Herbert Xueddee5ba2015-03-14 13:57:20 +1100127 * @tbl: The table that we were walking over
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100128 */
129struct rhashtable_walker {
130 struct list_head list;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100131 struct bucket_table *tbl;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100132};
133
134/**
135 * struct rhashtable_iter - Hash table iterator, fits into netlink cb
136 * @ht: Table to iterate through
137 * @p: Current pointer
138 * @walker: Associated rhashtable walker
139 * @slot: Current slot
140 * @skip: Number of entries to skip in slot
141 */
142struct rhashtable_iter {
143 struct rhashtable *ht;
144 struct rhash_head *p;
145 struct rhashtable_walker *walker;
146 unsigned int slot;
147 unsigned int skip;
148};
149
Thomas Graff89bd6f2015-01-02 23:00:21 +0100150static inline unsigned long rht_marker(const struct rhashtable *ht, u32 hash)
151{
152 return NULLS_MARKER(ht->p.nulls_base + hash);
153}
154
155#define INIT_RHT_NULLS_HEAD(ptr, ht, hash) \
156 ((ptr) = (typeof(ptr)) rht_marker(ht, hash))
157
158static inline bool rht_is_a_nulls(const struct rhash_head *ptr)
159{
160 return ((unsigned long) ptr & 1);
161}
162
163static inline unsigned long rht_get_nulls_value(const struct rhash_head *ptr)
164{
165 return ((unsigned long) ptr) >> 1;
166}
167
Thomas Graf7e1e7762014-08-02 11:47:44 +0200168#ifdef CONFIG_PROVE_LOCKING
Thomas Graf97defe12015-01-02 23:00:20 +0100169int lockdep_rht_mutex_is_held(struct rhashtable *ht);
Thomas Graf88d6ed12015-01-02 23:00:16 +0100170int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200171#else
Thomas Graf97defe12015-01-02 23:00:20 +0100172static inline int lockdep_rht_mutex_is_held(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200173{
174 return 1;
175}
Thomas Graf88d6ed12015-01-02 23:00:16 +0100176
177static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl,
178 u32 hash)
179{
180 return 1;
181}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200182#endif /* CONFIG_PROVE_LOCKING */
183
184int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params);
185
Thomas Graf6eba8222014-11-13 13:45:46 +0100186void rhashtable_insert(struct rhashtable *ht, struct rhash_head *node);
187bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *node);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200188
Thomas Graf6eba8222014-11-13 13:45:46 +0100189int rhashtable_expand(struct rhashtable *ht);
190int rhashtable_shrink(struct rhashtable *ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200191
Thomas Graf97defe12015-01-02 23:00:20 +0100192void *rhashtable_lookup(struct rhashtable *ht, const void *key);
193void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200194 bool (*compare)(void *, void *), void *arg);
Ying Xue7a868d12015-01-12 14:52:22 +0800195
Ying Xuedb304852015-01-07 13:41:54 +0800196bool rhashtable_lookup_insert(struct rhashtable *ht, struct rhash_head *obj);
Ying Xue7a868d12015-01-12 14:52:22 +0800197bool rhashtable_lookup_compare_insert(struct rhashtable *ht,
198 struct rhash_head *obj,
199 bool (*compare)(void *, void *),
200 void *arg);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200201
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100202int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter);
203void rhashtable_walk_exit(struct rhashtable_iter *iter);
204int rhashtable_walk_start(struct rhashtable_iter *iter) __acquires(RCU);
205void *rhashtable_walk_next(struct rhashtable_iter *iter);
206void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU);
207
Thomas Graf97defe12015-01-02 23:00:20 +0100208void rhashtable_destroy(struct rhashtable *ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200209
210#define rht_dereference(p, ht) \
211 rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht))
212
213#define rht_dereference_rcu(p, ht) \
214 rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht))
215
Thomas Graf88d6ed12015-01-02 23:00:16 +0100216#define rht_dereference_bucket(p, tbl, hash) \
217 rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200218
Thomas Graf88d6ed12015-01-02 23:00:16 +0100219#define rht_dereference_bucket_rcu(p, tbl, hash) \
220 rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash))
221
222#define rht_entry(tpos, pos, member) \
223 ({ tpos = container_of(pos, typeof(*tpos), member); 1; })
224
225/**
226 * rht_for_each_continue - continue iterating over hash chain
227 * @pos: the &struct rhash_head to use as a loop cursor.
228 * @head: the previous &struct rhash_head to continue from
229 * @tbl: the &struct bucket_table
230 * @hash: the hash value / bucket index
231 */
232#define rht_for_each_continue(pos, head, tbl, hash) \
233 for (pos = rht_dereference_bucket(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100234 !rht_is_a_nulls(pos); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100235 pos = rht_dereference_bucket((pos)->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200236
237/**
238 * rht_for_each - iterate over hash chain
Thomas Graf88d6ed12015-01-02 23:00:16 +0100239 * @pos: the &struct rhash_head to use as a loop cursor.
240 * @tbl: the &struct bucket_table
241 * @hash: the hash value / bucket index
Thomas Graf7e1e7762014-08-02 11:47:44 +0200242 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100243#define rht_for_each(pos, tbl, hash) \
244 rht_for_each_continue(pos, (tbl)->buckets[hash], tbl, hash)
245
246/**
247 * rht_for_each_entry_continue - continue iterating over hash chain
248 * @tpos: the type * to use as a loop cursor.
249 * @pos: the &struct rhash_head to use as a loop cursor.
250 * @head: the previous &struct rhash_head to continue from
251 * @tbl: the &struct bucket_table
252 * @hash: the hash value / bucket index
253 * @member: name of the &struct rhash_head within the hashable struct.
254 */
255#define rht_for_each_entry_continue(tpos, pos, head, tbl, hash, member) \
256 for (pos = rht_dereference_bucket(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100257 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100258 pos = rht_dereference_bucket((pos)->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200259
260/**
261 * rht_for_each_entry - iterate over hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100262 * @tpos: the type * to use as a loop cursor.
263 * @pos: the &struct rhash_head to use as a loop cursor.
264 * @tbl: the &struct bucket_table
265 * @hash: the hash value / bucket index
266 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200267 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100268#define rht_for_each_entry(tpos, pos, tbl, hash, member) \
269 rht_for_each_entry_continue(tpos, pos, (tbl)->buckets[hash], \
270 tbl, hash, member)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200271
272/**
273 * rht_for_each_entry_safe - safely iterate over hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100274 * @tpos: the type * to use as a loop cursor.
275 * @pos: the &struct rhash_head to use as a loop cursor.
276 * @next: the &struct rhash_head to use as next in loop cursor.
277 * @tbl: the &struct bucket_table
278 * @hash: the hash value / bucket index
279 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200280 *
281 * This hash chain list-traversal primitive allows for the looped code to
282 * remove the loop cursor from the list.
283 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100284#define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \
285 for (pos = rht_dereference_bucket((tbl)->buckets[hash], tbl, hash), \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100286 next = !rht_is_a_nulls(pos) ? \
287 rht_dereference_bucket(pos->next, tbl, hash) : NULL; \
288 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
Patrick McHardy607954b2015-01-21 11:12:13 +0000289 pos = next, \
290 next = !rht_is_a_nulls(pos) ? \
291 rht_dereference_bucket(pos->next, tbl, hash) : NULL)
Thomas Graf88d6ed12015-01-02 23:00:16 +0100292
293/**
294 * rht_for_each_rcu_continue - continue iterating over rcu hash chain
295 * @pos: the &struct rhash_head to use as a loop cursor.
296 * @head: the previous &struct rhash_head to continue from
297 * @tbl: the &struct bucket_table
298 * @hash: the hash value / bucket index
299 *
300 * This hash chain list-traversal primitive may safely run concurrently with
301 * the _rcu mutation primitives such as rhashtable_insert() as long as the
302 * traversal is guarded by rcu_read_lock().
303 */
304#define rht_for_each_rcu_continue(pos, head, tbl, hash) \
305 for (({barrier(); }), \
306 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100307 !rht_is_a_nulls(pos); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100308 pos = rcu_dereference_raw(pos->next))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200309
310/**
311 * rht_for_each_rcu - iterate over rcu hash chain
Thomas Graf88d6ed12015-01-02 23:00:16 +0100312 * @pos: the &struct rhash_head to use as a loop cursor.
313 * @tbl: the &struct bucket_table
314 * @hash: the hash value / bucket index
Thomas Graf7e1e7762014-08-02 11:47:44 +0200315 *
316 * This hash chain list-traversal primitive may safely run concurrently with
Thomas Graf88d6ed12015-01-02 23:00:16 +0100317 * the _rcu mutation primitives such as rhashtable_insert() as long as the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200318 * traversal is guarded by rcu_read_lock().
319 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100320#define rht_for_each_rcu(pos, tbl, hash) \
321 rht_for_each_rcu_continue(pos, (tbl)->buckets[hash], tbl, hash)
322
323/**
324 * rht_for_each_entry_rcu_continue - continue iterating over rcu hash chain
325 * @tpos: the type * to use as a loop cursor.
326 * @pos: the &struct rhash_head to use as a loop cursor.
327 * @head: the previous &struct rhash_head to continue from
328 * @tbl: the &struct bucket_table
329 * @hash: the hash value / bucket index
330 * @member: name of the &struct rhash_head within the hashable struct.
331 *
332 * This hash chain list-traversal primitive may safely run concurrently with
333 * the _rcu mutation primitives such as rhashtable_insert() as long as the
334 * traversal is guarded by rcu_read_lock().
335 */
336#define rht_for_each_entry_rcu_continue(tpos, pos, head, tbl, hash, member) \
337 for (({barrier(); }), \
338 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100339 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100340 pos = rht_dereference_bucket_rcu(pos->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200341
342/**
343 * rht_for_each_entry_rcu - iterate over rcu hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100344 * @tpos: the type * to use as a loop cursor.
345 * @pos: the &struct rhash_head to use as a loop cursor.
346 * @tbl: the &struct bucket_table
347 * @hash: the hash value / bucket index
348 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200349 *
350 * This hash chain list-traversal primitive may safely run concurrently with
Thomas Graf88d6ed12015-01-02 23:00:16 +0100351 * the _rcu mutation primitives such as rhashtable_insert() as long as the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200352 * traversal is guarded by rcu_read_lock().
353 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100354#define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \
355 rht_for_each_entry_rcu_continue(tpos, pos, (tbl)->buckets[hash],\
356 tbl, hash, member)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200357
358#endif /* _LINUX_RHASHTABLE_H */