blob: 9570832ab07cbf633c409abdf7f1bc2ef5be5d4f [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
Thomas Graff89bd6f2015-01-02 23:00:21 +010021#include <linux/list_nulls.h>
Thomas Graf97defe12015-01-02 23:00:20 +010022#include <linux/workqueue.h>
Ying Xue86b35b62015-01-04 15:25:09 +080023#include <linux/mutex.h>
Thomas Graf7e1e7762014-08-02 11:47:44 +020024
Thomas Graff89bd6f2015-01-02 23:00:21 +010025/*
26 * The end of the chain is marked with a special nulls marks which has
27 * the following format:
28 *
29 * +-------+-----------------------------------------------------+-+
30 * | Base | Hash |1|
31 * +-------+-----------------------------------------------------+-+
32 *
33 * Base (4 bits) : Reserved to distinguish between multiple tables.
34 * Specified via &struct rhashtable_params.nulls_base.
35 * Hash (27 bits): Full hash (unmasked) of first element added to bucket
36 * 1 (1 bit) : Nulls marker (always set)
37 *
38 * The remaining bits of the next pointer remain unused for now.
39 */
40#define RHT_BASE_BITS 4
41#define RHT_HASH_BITS 27
42#define RHT_BASE_SHIFT RHT_HASH_BITS
43
Thomas Graf7e1e7762014-08-02 11:47:44 +020044struct rhash_head {
Thomas Graf5300fdc2014-08-13 16:38:29 +020045 struct rhash_head __rcu *next;
Thomas Graf7e1e7762014-08-02 11:47:44 +020046};
47
Thomas Graf97defe12015-01-02 23:00:20 +010048/**
49 * struct bucket_table - Table of hash buckets
50 * @size: Number of hash buckets
51 * @locks_mask: Mask to apply before accessing locks[]
52 * @locks: Array of spinlocks protecting individual buckets
53 * @buckets: size * hash buckets
54 */
Thomas Graf7e1e7762014-08-02 11:47:44 +020055struct bucket_table {
56 size_t size;
Thomas Graf97defe12015-01-02 23:00:20 +010057 unsigned int locks_mask;
58 spinlock_t *locks;
Thomas Graf7e1e7762014-08-02 11:47:44 +020059 struct rhash_head __rcu *buckets[];
60};
61
62typedef u32 (*rht_hashfn_t)(const void *data, u32 len, u32 seed);
63typedef u32 (*rht_obj_hashfn_t)(const void *data, u32 seed);
64
65struct rhashtable;
66
67/**
68 * struct rhashtable_params - Hash table construction parameters
69 * @nelem_hint: Hint on number of elements, should be 75% of desired size
70 * @key_len: Length of key
71 * @key_offset: Offset of key in struct to be hashed
72 * @head_offset: Offset of rhash_head in struct to be hashed
73 * @hash_rnd: Seed to use while hashing
74 * @max_shift: Maximum number of shifts while expanding
Ying Xue94000172014-09-03 09:22:36 +080075 * @min_shift: Minimum number of shifts while shrinking
Thomas Graff89bd6f2015-01-02 23:00:21 +010076 * @nulls_base: Base value to generate nulls marker
Thomas Graf97defe12015-01-02 23:00:20 +010077 * @locks_mul: Number of bucket locks to allocate per cpu (default: 128)
Thomas Graf7e1e7762014-08-02 11:47:44 +020078 * @hashfn: Function to hash key
79 * @obj_hashfn: Function to hash object
80 * @grow_decision: If defined, may return true if table should expand
81 * @shrink_decision: If defined, may return true if table should shrink
Ying Xue6f73d3b2015-01-12 14:52:24 +080082 *
83 * Note: when implementing the grow and shrink decision function, min/max
84 * shift must be enforced, otherwise, resizing watermarks they set may be
85 * useless.
Thomas Graf7e1e7762014-08-02 11:47:44 +020086 */
87struct rhashtable_params {
88 size_t nelem_hint;
89 size_t key_len;
90 size_t key_offset;
91 size_t head_offset;
92 u32 hash_rnd;
93 size_t max_shift;
Ying Xue94000172014-09-03 09:22:36 +080094 size_t min_shift;
Thomas Graff89bd6f2015-01-02 23:00:21 +010095 u32 nulls_base;
Thomas Graf97defe12015-01-02 23:00:20 +010096 size_t locks_mul;
Thomas Graf7e1e7762014-08-02 11:47:44 +020097 rht_hashfn_t hashfn;
98 rht_obj_hashfn_t obj_hashfn;
99 bool (*grow_decision)(const struct rhashtable *ht,
100 size_t new_size);
101 bool (*shrink_decision)(const struct rhashtable *ht,
102 size_t new_size);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200103};
104
105/**
106 * struct rhashtable - Hash table handle
107 * @tbl: Bucket table
Thomas Graf97defe12015-01-02 23:00:20 +0100108 * @future_tbl: Table under construction during expansion/shrinking
Thomas Graf7e1e7762014-08-02 11:47:44 +0200109 * @nelems: Number of elements in table
110 * @shift: Current size (1 << shift)
111 * @p: Configuration parameters
Thomas Graf97defe12015-01-02 23:00:20 +0100112 * @run_work: Deferred worker to expand/shrink asynchronously
113 * @mutex: Mutex to protect current/future table swapping
114 * @being_destroyed: True if table is set up for destruction
Thomas Graf7e1e7762014-08-02 11:47:44 +0200115 */
116struct rhashtable {
117 struct bucket_table __rcu *tbl;
Thomas Graf97defe12015-01-02 23:00:20 +0100118 struct bucket_table __rcu *future_tbl;
119 atomic_t nelems;
Ying Xuec0c09bf2015-01-07 13:41:56 +0800120 atomic_t shift;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200121 struct rhashtable_params p;
Thomas Graf97defe12015-01-02 23:00:20 +0100122 struct delayed_work run_work;
123 struct mutex mutex;
124 bool being_destroyed;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200125};
126
Thomas Graff89bd6f2015-01-02 23:00:21 +0100127static inline unsigned long rht_marker(const struct rhashtable *ht, u32 hash)
128{
129 return NULLS_MARKER(ht->p.nulls_base + hash);
130}
131
132#define INIT_RHT_NULLS_HEAD(ptr, ht, hash) \
133 ((ptr) = (typeof(ptr)) rht_marker(ht, hash))
134
135static inline bool rht_is_a_nulls(const struct rhash_head *ptr)
136{
137 return ((unsigned long) ptr & 1);
138}
139
140static inline unsigned long rht_get_nulls_value(const struct rhash_head *ptr)
141{
142 return ((unsigned long) ptr) >> 1;
143}
144
Thomas Graf7e1e7762014-08-02 11:47:44 +0200145#ifdef CONFIG_PROVE_LOCKING
Thomas Graf97defe12015-01-02 23:00:20 +0100146int lockdep_rht_mutex_is_held(struct rhashtable *ht);
Thomas Graf88d6ed12015-01-02 23:00:16 +0100147int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200148#else
Thomas Graf97defe12015-01-02 23:00:20 +0100149static inline int lockdep_rht_mutex_is_held(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200150{
151 return 1;
152}
Thomas Graf88d6ed12015-01-02 23:00:16 +0100153
154static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl,
155 u32 hash)
156{
157 return 1;
158}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200159#endif /* CONFIG_PROVE_LOCKING */
160
161int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params);
162
Thomas Graf6eba8222014-11-13 13:45:46 +0100163void rhashtable_insert(struct rhashtable *ht, struct rhash_head *node);
164bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *node);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200165
166bool rht_grow_above_75(const struct rhashtable *ht, size_t new_size);
167bool rht_shrink_below_30(const struct rhashtable *ht, size_t new_size);
168
Thomas Graf6eba8222014-11-13 13:45:46 +0100169int rhashtable_expand(struct rhashtable *ht);
170int rhashtable_shrink(struct rhashtable *ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200171
Thomas Graf97defe12015-01-02 23:00:20 +0100172void *rhashtable_lookup(struct rhashtable *ht, const void *key);
173void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200174 bool (*compare)(void *, void *), void *arg);
Ying Xue7a868d12015-01-12 14:52:22 +0800175
Ying Xuedb304852015-01-07 13:41:54 +0800176bool rhashtable_lookup_insert(struct rhashtable *ht, struct rhash_head *obj);
Ying Xue7a868d12015-01-12 14:52:22 +0800177bool rhashtable_lookup_compare_insert(struct rhashtable *ht,
178 struct rhash_head *obj,
179 bool (*compare)(void *, void *),
180 void *arg);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200181
Thomas Graf97defe12015-01-02 23:00:20 +0100182void rhashtable_destroy(struct rhashtable *ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200183
184#define rht_dereference(p, ht) \
185 rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht))
186
187#define rht_dereference_rcu(p, ht) \
188 rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht))
189
Thomas Graf88d6ed12015-01-02 23:00:16 +0100190#define rht_dereference_bucket(p, tbl, hash) \
191 rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200192
Thomas Graf88d6ed12015-01-02 23:00:16 +0100193#define rht_dereference_bucket_rcu(p, tbl, hash) \
194 rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash))
195
196#define rht_entry(tpos, pos, member) \
197 ({ tpos = container_of(pos, typeof(*tpos), member); 1; })
198
199/**
200 * rht_for_each_continue - continue iterating over hash chain
201 * @pos: the &struct rhash_head to use as a loop cursor.
202 * @head: the previous &struct rhash_head to continue from
203 * @tbl: the &struct bucket_table
204 * @hash: the hash value / bucket index
205 */
206#define rht_for_each_continue(pos, head, tbl, hash) \
207 for (pos = rht_dereference_bucket(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100208 !rht_is_a_nulls(pos); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100209 pos = rht_dereference_bucket((pos)->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200210
211/**
212 * rht_for_each - iterate over hash chain
Thomas Graf88d6ed12015-01-02 23:00:16 +0100213 * @pos: the &struct rhash_head to use as a loop cursor.
214 * @tbl: the &struct bucket_table
215 * @hash: the hash value / bucket index
Thomas Graf7e1e7762014-08-02 11:47:44 +0200216 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100217#define rht_for_each(pos, tbl, hash) \
218 rht_for_each_continue(pos, (tbl)->buckets[hash], tbl, hash)
219
220/**
221 * rht_for_each_entry_continue - continue iterating over hash chain
222 * @tpos: the type * to use as a loop cursor.
223 * @pos: the &struct rhash_head to use as a loop cursor.
224 * @head: the previous &struct rhash_head to continue from
225 * @tbl: the &struct bucket_table
226 * @hash: the hash value / bucket index
227 * @member: name of the &struct rhash_head within the hashable struct.
228 */
229#define rht_for_each_entry_continue(tpos, pos, head, tbl, hash, member) \
230 for (pos = rht_dereference_bucket(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100231 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100232 pos = rht_dereference_bucket((pos)->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200233
234/**
235 * rht_for_each_entry - iterate over hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100236 * @tpos: the type * to use as a loop cursor.
237 * @pos: the &struct rhash_head to use as a loop cursor.
238 * @tbl: the &struct bucket_table
239 * @hash: the hash value / bucket index
240 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200241 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100242#define rht_for_each_entry(tpos, pos, tbl, hash, member) \
243 rht_for_each_entry_continue(tpos, pos, (tbl)->buckets[hash], \
244 tbl, hash, member)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200245
246/**
247 * rht_for_each_entry_safe - safely iterate over hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100248 * @tpos: the type * to use as a loop cursor.
249 * @pos: the &struct rhash_head to use as a loop cursor.
250 * @next: the &struct rhash_head to use as next in loop cursor.
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.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200254 *
255 * This hash chain list-traversal primitive allows for the looped code to
256 * remove the loop cursor from the list.
257 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100258#define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \
259 for (pos = rht_dereference_bucket((tbl)->buckets[hash], tbl, hash), \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100260 next = !rht_is_a_nulls(pos) ? \
261 rht_dereference_bucket(pos->next, tbl, hash) : NULL; \
262 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100263 pos = next)
264
265/**
266 * rht_for_each_rcu_continue - continue iterating over rcu hash chain
267 * @pos: the &struct rhash_head to use as a loop cursor.
268 * @head: the previous &struct rhash_head to continue from
269 * @tbl: the &struct bucket_table
270 * @hash: the hash value / bucket index
271 *
272 * This hash chain list-traversal primitive may safely run concurrently with
273 * the _rcu mutation primitives such as rhashtable_insert() as long as the
274 * traversal is guarded by rcu_read_lock().
275 */
276#define rht_for_each_rcu_continue(pos, head, tbl, hash) \
277 for (({barrier(); }), \
278 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100279 !rht_is_a_nulls(pos); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100280 pos = rcu_dereference_raw(pos->next))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200281
282/**
283 * rht_for_each_rcu - iterate over rcu hash chain
Thomas Graf88d6ed12015-01-02 23:00:16 +0100284 * @pos: the &struct rhash_head to use as a loop cursor.
285 * @tbl: the &struct bucket_table
286 * @hash: the hash value / bucket index
Thomas Graf7e1e7762014-08-02 11:47:44 +0200287 *
288 * This hash chain list-traversal primitive may safely run concurrently with
Thomas Graf88d6ed12015-01-02 23:00:16 +0100289 * the _rcu mutation primitives such as rhashtable_insert() as long as the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200290 * traversal is guarded by rcu_read_lock().
291 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100292#define rht_for_each_rcu(pos, tbl, hash) \
293 rht_for_each_rcu_continue(pos, (tbl)->buckets[hash], tbl, hash)
294
295/**
296 * rht_for_each_entry_rcu_continue - continue iterating over rcu hash chain
297 * @tpos: the type * to use as a loop cursor.
298 * @pos: the &struct rhash_head to use as a loop cursor.
299 * @head: the previous &struct rhash_head to continue from
300 * @tbl: the &struct bucket_table
301 * @hash: the hash value / bucket index
302 * @member: name of the &struct rhash_head within the hashable struct.
303 *
304 * This hash chain list-traversal primitive may safely run concurrently with
305 * the _rcu mutation primitives such as rhashtable_insert() as long as the
306 * traversal is guarded by rcu_read_lock().
307 */
308#define rht_for_each_entry_rcu_continue(tpos, pos, head, tbl, hash, member) \
309 for (({barrier(); }), \
310 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100311 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100312 pos = rht_dereference_bucket_rcu(pos->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200313
314/**
315 * rht_for_each_entry_rcu - iterate over rcu hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100316 * @tpos: the type * to use as a loop cursor.
317 * @pos: the &struct rhash_head to use as a loop cursor.
318 * @tbl: the &struct bucket_table
319 * @hash: the hash value / bucket index
320 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200321 *
322 * This hash chain list-traversal primitive may safely run concurrently with
Thomas Graf88d6ed12015-01-02 23:00:16 +0100323 * the _rcu mutation primitives such as rhashtable_insert() as long as the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200324 * traversal is guarded by rcu_read_lock().
325 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100326#define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \
327 rht_for_each_entry_rcu_continue(tpos, pos, (tbl)->buckets[hash],\
328 tbl, hash, member)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200329
330#endif /* _LINUX_RHASHTABLE_H */