blob: a0abddd226b382d93062369cc87ae309ea4a5450 [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 Xu988dfbd2015-03-10 09:27:55 +110052 * @hash_rnd: Random seed to fold into hash
Daniel Borkmanna5b68462015-03-12 15:28:40 +010053 * @shift: Current size (1 << shift)
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
Thomas Graf97defe12015-01-02 23:00:20 +010058 * @buckets: size * hash buckets
59 */
Thomas Graf7e1e7762014-08-02 11:47:44 +020060struct bucket_table {
Eric Dumazetb9ebafb2015-02-20 06:48:57 -080061 size_t size;
Herbert Xu988dfbd2015-03-10 09:27:55 +110062 u32 hash_rnd;
Daniel Borkmanna5b68462015-03-12 15:28:40 +010063 u32 shift;
Eric Dumazetb9ebafb2015-02-20 06:48:57 -080064 unsigned int locks_mask;
65 spinlock_t *locks;
Herbert Xueddee5ba2015-03-14 13:57:20 +110066 struct list_head walkers;
Herbert Xu9d901bc2015-03-14 13:57:23 +110067 struct rcu_head rcu;
Eric Dumazetb9ebafb2015-02-20 06:48:57 -080068
69 struct rhash_head __rcu *buckets[] ____cacheline_aligned_in_smp;
Thomas Graf7e1e7762014-08-02 11:47:44 +020070};
71
72typedef u32 (*rht_hashfn_t)(const void *data, u32 len, u32 seed);
73typedef u32 (*rht_obj_hashfn_t)(const void *data, u32 seed);
74
75struct rhashtable;
76
77/**
78 * struct rhashtable_params - Hash table construction parameters
79 * @nelem_hint: Hint on number of elements, should be 75% of desired size
80 * @key_len: Length of key
81 * @key_offset: Offset of key in struct to be hashed
82 * @head_offset: Offset of rhash_head in struct to be hashed
Thomas Graf7e1e7762014-08-02 11:47:44 +020083 * @max_shift: Maximum number of shifts while expanding
Ying Xue94000172014-09-03 09:22:36 +080084 * @min_shift: Minimum number of shifts while shrinking
Thomas Graff89bd6f2015-01-02 23:00:21 +010085 * @nulls_base: Base value to generate nulls marker
Thomas Graf97defe12015-01-02 23:00:20 +010086 * @locks_mul: Number of bucket locks to allocate per cpu (default: 128)
Thomas Graf7e1e7762014-08-02 11:47:44 +020087 * @hashfn: Function to hash key
88 * @obj_hashfn: Function to hash object
Thomas Graf7e1e7762014-08-02 11:47:44 +020089 */
90struct rhashtable_params {
91 size_t nelem_hint;
92 size_t key_len;
93 size_t key_offset;
94 size_t head_offset;
Thomas Graf7e1e7762014-08-02 11:47:44 +020095 size_t max_shift;
Ying Xue94000172014-09-03 09:22:36 +080096 size_t min_shift;
Thomas Graff89bd6f2015-01-02 23:00:21 +010097 u32 nulls_base;
Thomas Graf97defe12015-01-02 23:00:20 +010098 size_t locks_mul;
Thomas Graf7e1e7762014-08-02 11:47:44 +020099 rht_hashfn_t hashfn;
100 rht_obj_hashfn_t obj_hashfn;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200101};
102
103/**
104 * struct rhashtable - Hash table handle
105 * @tbl: Bucket table
Thomas Graf97defe12015-01-02 23:00:20 +0100106 * @future_tbl: Table under construction during expansion/shrinking
Thomas Graf7e1e7762014-08-02 11:47:44 +0200107 * @nelems: Number of elements in table
Thomas Graf7e1e7762014-08-02 11:47:44 +0200108 * @p: Configuration parameters
Thomas Graf97defe12015-01-02 23:00:20 +0100109 * @run_work: Deferred worker to expand/shrink asynchronously
110 * @mutex: Mutex to protect current/future table swapping
111 * @being_destroyed: True if table is set up for destruction
Thomas Graf7e1e7762014-08-02 11:47:44 +0200112 */
113struct rhashtable {
114 struct bucket_table __rcu *tbl;
Thomas Graf97defe12015-01-02 23:00:20 +0100115 struct bucket_table __rcu *future_tbl;
116 atomic_t nelems;
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100117 bool being_destroyed;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200118 struct rhashtable_params p;
Ying Xue57699a42015-01-16 11:13:09 +0800119 struct work_struct run_work;
Thomas Graf97defe12015-01-02 23:00:20 +0100120 struct mutex mutex;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200121};
122
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100123/**
124 * struct rhashtable_walker - Hash table walker
125 * @list: List entry on list of walkers
Herbert Xueddee5ba2015-03-14 13:57:20 +1100126 * @tbl: The table that we were walking over
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100127 */
128struct rhashtable_walker {
129 struct list_head list;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100130 struct bucket_table *tbl;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100131};
132
133/**
134 * struct rhashtable_iter - Hash table iterator, fits into netlink cb
135 * @ht: Table to iterate through
136 * @p: Current pointer
137 * @walker: Associated rhashtable walker
138 * @slot: Current slot
139 * @skip: Number of entries to skip in slot
140 */
141struct rhashtable_iter {
142 struct rhashtable *ht;
143 struct rhash_head *p;
144 struct rhashtable_walker *walker;
145 unsigned int slot;
146 unsigned int skip;
147};
148
Thomas Graff89bd6f2015-01-02 23:00:21 +0100149static inline unsigned long rht_marker(const struct rhashtable *ht, u32 hash)
150{
151 return NULLS_MARKER(ht->p.nulls_base + hash);
152}
153
154#define INIT_RHT_NULLS_HEAD(ptr, ht, hash) \
155 ((ptr) = (typeof(ptr)) rht_marker(ht, hash))
156
157static inline bool rht_is_a_nulls(const struct rhash_head *ptr)
158{
159 return ((unsigned long) ptr & 1);
160}
161
162static inline unsigned long rht_get_nulls_value(const struct rhash_head *ptr)
163{
164 return ((unsigned long) ptr) >> 1;
165}
166
Thomas Graf7e1e7762014-08-02 11:47:44 +0200167#ifdef CONFIG_PROVE_LOCKING
Thomas Graf97defe12015-01-02 23:00:20 +0100168int lockdep_rht_mutex_is_held(struct rhashtable *ht);
Thomas Graf88d6ed12015-01-02 23:00:16 +0100169int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200170#else
Thomas Graf97defe12015-01-02 23:00:20 +0100171static inline int lockdep_rht_mutex_is_held(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200172{
173 return 1;
174}
Thomas Graf88d6ed12015-01-02 23:00:16 +0100175
176static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl,
177 u32 hash)
178{
179 return 1;
180}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200181#endif /* CONFIG_PROVE_LOCKING */
182
183int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params);
184
Thomas Graf6eba8222014-11-13 13:45:46 +0100185void rhashtable_insert(struct rhashtable *ht, struct rhash_head *node);
186bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *node);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200187
Thomas Graf6eba8222014-11-13 13:45:46 +0100188int rhashtable_expand(struct rhashtable *ht);
189int rhashtable_shrink(struct rhashtable *ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200190
Thomas Graf97defe12015-01-02 23:00:20 +0100191void *rhashtable_lookup(struct rhashtable *ht, const void *key);
192void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200193 bool (*compare)(void *, void *), void *arg);
Ying Xue7a868d12015-01-12 14:52:22 +0800194
Ying Xuedb304852015-01-07 13:41:54 +0800195bool rhashtable_lookup_insert(struct rhashtable *ht, struct rhash_head *obj);
Ying Xue7a868d12015-01-12 14:52:22 +0800196bool rhashtable_lookup_compare_insert(struct rhashtable *ht,
197 struct rhash_head *obj,
198 bool (*compare)(void *, void *),
199 void *arg);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200200
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100201int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter);
202void rhashtable_walk_exit(struct rhashtable_iter *iter);
203int rhashtable_walk_start(struct rhashtable_iter *iter) __acquires(RCU);
204void *rhashtable_walk_next(struct rhashtable_iter *iter);
205void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU);
206
Thomas Graf97defe12015-01-02 23:00:20 +0100207void rhashtable_destroy(struct rhashtable *ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200208
209#define rht_dereference(p, ht) \
210 rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht))
211
212#define rht_dereference_rcu(p, ht) \
213 rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht))
214
Thomas Graf88d6ed12015-01-02 23:00:16 +0100215#define rht_dereference_bucket(p, tbl, hash) \
216 rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200217
Thomas Graf88d6ed12015-01-02 23:00:16 +0100218#define rht_dereference_bucket_rcu(p, tbl, hash) \
219 rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash))
220
221#define rht_entry(tpos, pos, member) \
222 ({ tpos = container_of(pos, typeof(*tpos), member); 1; })
223
224/**
225 * rht_for_each_continue - continue iterating over hash chain
226 * @pos: the &struct rhash_head to use as a loop cursor.
227 * @head: the previous &struct rhash_head to continue from
228 * @tbl: the &struct bucket_table
229 * @hash: the hash value / bucket index
230 */
231#define rht_for_each_continue(pos, head, tbl, hash) \
232 for (pos = rht_dereference_bucket(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100233 !rht_is_a_nulls(pos); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100234 pos = rht_dereference_bucket((pos)->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200235
236/**
237 * rht_for_each - iterate over hash chain
Thomas Graf88d6ed12015-01-02 23:00:16 +0100238 * @pos: the &struct rhash_head to use as a loop cursor.
239 * @tbl: the &struct bucket_table
240 * @hash: the hash value / bucket index
Thomas Graf7e1e7762014-08-02 11:47:44 +0200241 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100242#define rht_for_each(pos, tbl, hash) \
243 rht_for_each_continue(pos, (tbl)->buckets[hash], tbl, hash)
244
245/**
246 * rht_for_each_entry_continue - continue iterating over hash chain
247 * @tpos: the type * to use as a loop cursor.
248 * @pos: the &struct rhash_head to use as a loop cursor.
249 * @head: the previous &struct rhash_head to continue from
250 * @tbl: the &struct bucket_table
251 * @hash: the hash value / bucket index
252 * @member: name of the &struct rhash_head within the hashable struct.
253 */
254#define rht_for_each_entry_continue(tpos, pos, head, tbl, hash, member) \
255 for (pos = rht_dereference_bucket(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100256 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100257 pos = rht_dereference_bucket((pos)->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200258
259/**
260 * rht_for_each_entry - iterate over hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100261 * @tpos: the type * to use as a loop cursor.
262 * @pos: the &struct rhash_head to use as a loop cursor.
263 * @tbl: the &struct bucket_table
264 * @hash: the hash value / bucket index
265 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200266 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100267#define rht_for_each_entry(tpos, pos, tbl, hash, member) \
268 rht_for_each_entry_continue(tpos, pos, (tbl)->buckets[hash], \
269 tbl, hash, member)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200270
271/**
272 * rht_for_each_entry_safe - safely iterate over hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100273 * @tpos: the type * to use as a loop cursor.
274 * @pos: the &struct rhash_head to use as a loop cursor.
275 * @next: the &struct rhash_head to use as next in loop cursor.
276 * @tbl: the &struct bucket_table
277 * @hash: the hash value / bucket index
278 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200279 *
280 * This hash chain list-traversal primitive allows for the looped code to
281 * remove the loop cursor from the list.
282 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100283#define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \
284 for (pos = rht_dereference_bucket((tbl)->buckets[hash], tbl, hash), \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100285 next = !rht_is_a_nulls(pos) ? \
286 rht_dereference_bucket(pos->next, tbl, hash) : NULL; \
287 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
Patrick McHardy607954b2015-01-21 11:12:13 +0000288 pos = next, \
289 next = !rht_is_a_nulls(pos) ? \
290 rht_dereference_bucket(pos->next, tbl, hash) : NULL)
Thomas Graf88d6ed12015-01-02 23:00:16 +0100291
292/**
293 * rht_for_each_rcu_continue - continue iterating over rcu hash chain
294 * @pos: the &struct rhash_head to use as a loop cursor.
295 * @head: the previous &struct rhash_head to continue from
296 * @tbl: the &struct bucket_table
297 * @hash: the hash value / bucket index
298 *
299 * This hash chain list-traversal primitive may safely run concurrently with
300 * the _rcu mutation primitives such as rhashtable_insert() as long as the
301 * traversal is guarded by rcu_read_lock().
302 */
303#define rht_for_each_rcu_continue(pos, head, tbl, hash) \
304 for (({barrier(); }), \
305 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100306 !rht_is_a_nulls(pos); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100307 pos = rcu_dereference_raw(pos->next))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200308
309/**
310 * rht_for_each_rcu - iterate over rcu hash chain
Thomas Graf88d6ed12015-01-02 23:00:16 +0100311 * @pos: the &struct rhash_head to use as a loop cursor.
312 * @tbl: the &struct bucket_table
313 * @hash: the hash value / bucket index
Thomas Graf7e1e7762014-08-02 11:47:44 +0200314 *
315 * This hash chain list-traversal primitive may safely run concurrently with
Thomas Graf88d6ed12015-01-02 23:00:16 +0100316 * the _rcu mutation primitives such as rhashtable_insert() as long as the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200317 * traversal is guarded by rcu_read_lock().
318 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100319#define rht_for_each_rcu(pos, tbl, hash) \
320 rht_for_each_rcu_continue(pos, (tbl)->buckets[hash], tbl, hash)
321
322/**
323 * rht_for_each_entry_rcu_continue - continue iterating over rcu hash chain
324 * @tpos: the type * to use as a loop cursor.
325 * @pos: the &struct rhash_head to use as a loop cursor.
326 * @head: the previous &struct rhash_head to continue from
327 * @tbl: the &struct bucket_table
328 * @hash: the hash value / bucket index
329 * @member: name of the &struct rhash_head within the hashable struct.
330 *
331 * This hash chain list-traversal primitive may safely run concurrently with
332 * the _rcu mutation primitives such as rhashtable_insert() as long as the
333 * traversal is guarded by rcu_read_lock().
334 */
335#define rht_for_each_entry_rcu_continue(tpos, pos, head, tbl, hash, member) \
336 for (({barrier(); }), \
337 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100338 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100339 pos = rht_dereference_bucket_rcu(pos->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200340
341/**
342 * rht_for_each_entry_rcu - iterate over rcu hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100343 * @tpos: the type * to use as a loop cursor.
344 * @pos: the &struct rhash_head to use as a loop cursor.
345 * @tbl: the &struct bucket_table
346 * @hash: the hash value / bucket index
347 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200348 *
349 * This hash chain list-traversal primitive may safely run concurrently with
Thomas Graf88d6ed12015-01-02 23:00:16 +0100350 * the _rcu mutation primitives such as rhashtable_insert() as long as the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200351 * traversal is guarded by rcu_read_lock().
352 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100353#define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \
354 rht_for_each_entry_rcu_continue(tpos, pos, (tbl)->buckets[hash],\
355 tbl, hash, member)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200356
357#endif /* _LINUX_RHASHTABLE_H */