blob: de7cac753b0920c6e8876fc16bbdd5144a6a3605 [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>
Thomas Graf7e1e7762014-08-02 11:47:44 +020023
Thomas Graff89bd6f2015-01-02 23:00:21 +010024/*
25 * The end of the chain is marked with a special nulls marks which has
26 * the following format:
27 *
28 * +-------+-----------------------------------------------------+-+
29 * | Base | Hash |1|
30 * +-------+-----------------------------------------------------+-+
31 *
32 * Base (4 bits) : Reserved to distinguish between multiple tables.
33 * Specified via &struct rhashtable_params.nulls_base.
34 * Hash (27 bits): Full hash (unmasked) of first element added to bucket
35 * 1 (1 bit) : Nulls marker (always set)
36 *
37 * The remaining bits of the next pointer remain unused for now.
38 */
39#define RHT_BASE_BITS 4
40#define RHT_HASH_BITS 27
41#define RHT_BASE_SHIFT RHT_HASH_BITS
42
Thomas Graf7e1e7762014-08-02 11:47:44 +020043struct rhash_head {
Thomas Graf5300fdc2014-08-13 16:38:29 +020044 struct rhash_head __rcu *next;
Thomas Graf7e1e7762014-08-02 11:47:44 +020045};
46
Thomas Graf97defe12015-01-02 23:00:20 +010047/**
48 * struct bucket_table - Table of hash buckets
49 * @size: Number of hash buckets
50 * @locks_mask: Mask to apply before accessing locks[]
51 * @locks: Array of spinlocks protecting individual buckets
52 * @buckets: size * hash buckets
53 */
Thomas Graf7e1e7762014-08-02 11:47:44 +020054struct bucket_table {
55 size_t size;
Thomas Graf97defe12015-01-02 23:00:20 +010056 unsigned int locks_mask;
57 spinlock_t *locks;
Thomas Graf7e1e7762014-08-02 11:47:44 +020058 struct rhash_head __rcu *buckets[];
59};
60
61typedef u32 (*rht_hashfn_t)(const void *data, u32 len, u32 seed);
62typedef u32 (*rht_obj_hashfn_t)(const void *data, u32 seed);
63
64struct rhashtable;
65
66/**
67 * struct rhashtable_params - Hash table construction parameters
68 * @nelem_hint: Hint on number of elements, should be 75% of desired size
69 * @key_len: Length of key
70 * @key_offset: Offset of key in struct to be hashed
71 * @head_offset: Offset of rhash_head in struct to be hashed
72 * @hash_rnd: Seed to use while hashing
73 * @max_shift: Maximum number of shifts while expanding
Ying Xue94000172014-09-03 09:22:36 +080074 * @min_shift: Minimum number of shifts while shrinking
Thomas Graff89bd6f2015-01-02 23:00:21 +010075 * @nulls_base: Base value to generate nulls marker
Thomas Graf97defe12015-01-02 23:00:20 +010076 * @locks_mul: Number of bucket locks to allocate per cpu (default: 128)
Thomas Graf7e1e7762014-08-02 11:47:44 +020077 * @hashfn: Function to hash key
78 * @obj_hashfn: Function to hash object
79 * @grow_decision: If defined, may return true if table should expand
80 * @shrink_decision: If defined, may return true if table should shrink
Thomas Graf7e1e7762014-08-02 11:47:44 +020081 */
82struct rhashtable_params {
83 size_t nelem_hint;
84 size_t key_len;
85 size_t key_offset;
86 size_t head_offset;
87 u32 hash_rnd;
88 size_t max_shift;
Ying Xue94000172014-09-03 09:22:36 +080089 size_t min_shift;
Thomas Graff89bd6f2015-01-02 23:00:21 +010090 u32 nulls_base;
Thomas Graf97defe12015-01-02 23:00:20 +010091 size_t locks_mul;
Thomas Graf7e1e7762014-08-02 11:47:44 +020092 rht_hashfn_t hashfn;
93 rht_obj_hashfn_t obj_hashfn;
94 bool (*grow_decision)(const struct rhashtable *ht,
95 size_t new_size);
96 bool (*shrink_decision)(const struct rhashtable *ht,
97 size_t new_size);
Thomas Graf7e1e7762014-08-02 11:47:44 +020098};
99
100/**
101 * struct rhashtable - Hash table handle
102 * @tbl: Bucket table
Thomas Graf97defe12015-01-02 23:00:20 +0100103 * @future_tbl: Table under construction during expansion/shrinking
Thomas Graf7e1e7762014-08-02 11:47:44 +0200104 * @nelems: Number of elements in table
105 * @shift: Current size (1 << shift)
106 * @p: Configuration parameters
Thomas Graf97defe12015-01-02 23:00:20 +0100107 * @run_work: Deferred worker to expand/shrink asynchronously
108 * @mutex: Mutex to protect current/future table swapping
109 * @being_destroyed: True if table is set up for destruction
Thomas Graf7e1e7762014-08-02 11:47:44 +0200110 */
111struct rhashtable {
112 struct bucket_table __rcu *tbl;
Thomas Graf97defe12015-01-02 23:00:20 +0100113 struct bucket_table __rcu *future_tbl;
114 atomic_t nelems;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200115 size_t shift;
116 struct rhashtable_params p;
Thomas Graf97defe12015-01-02 23:00:20 +0100117 struct delayed_work run_work;
118 struct mutex mutex;
119 bool being_destroyed;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200120};
121
Thomas Graff89bd6f2015-01-02 23:00:21 +0100122static inline unsigned long rht_marker(const struct rhashtable *ht, u32 hash)
123{
124 return NULLS_MARKER(ht->p.nulls_base + hash);
125}
126
127#define INIT_RHT_NULLS_HEAD(ptr, ht, hash) \
128 ((ptr) = (typeof(ptr)) rht_marker(ht, hash))
129
130static inline bool rht_is_a_nulls(const struct rhash_head *ptr)
131{
132 return ((unsigned long) ptr & 1);
133}
134
135static inline unsigned long rht_get_nulls_value(const struct rhash_head *ptr)
136{
137 return ((unsigned long) ptr) >> 1;
138}
139
Thomas Graf7e1e7762014-08-02 11:47:44 +0200140#ifdef CONFIG_PROVE_LOCKING
Thomas Graf97defe12015-01-02 23:00:20 +0100141int lockdep_rht_mutex_is_held(struct rhashtable *ht);
Thomas Graf88d6ed12015-01-02 23:00:16 +0100142int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200143#else
Thomas Graf97defe12015-01-02 23:00:20 +0100144static inline int lockdep_rht_mutex_is_held(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200145{
146 return 1;
147}
Thomas Graf88d6ed12015-01-02 23:00:16 +0100148
149static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl,
150 u32 hash)
151{
152 return 1;
153}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200154#endif /* CONFIG_PROVE_LOCKING */
155
156int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params);
157
Thomas Graf6eba8222014-11-13 13:45:46 +0100158void rhashtable_insert(struct rhashtable *ht, struct rhash_head *node);
159bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *node);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200160
161bool rht_grow_above_75(const struct rhashtable *ht, size_t new_size);
162bool rht_shrink_below_30(const struct rhashtable *ht, size_t new_size);
163
Thomas Graf6eba8222014-11-13 13:45:46 +0100164int rhashtable_expand(struct rhashtable *ht);
165int rhashtable_shrink(struct rhashtable *ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200166
Thomas Graf97defe12015-01-02 23:00:20 +0100167void *rhashtable_lookup(struct rhashtable *ht, const void *key);
168void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200169 bool (*compare)(void *, void *), void *arg);
170
Thomas Graf97defe12015-01-02 23:00:20 +0100171void rhashtable_destroy(struct rhashtable *ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200172
173#define rht_dereference(p, ht) \
174 rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht))
175
176#define rht_dereference_rcu(p, ht) \
177 rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht))
178
Thomas Graf88d6ed12015-01-02 23:00:16 +0100179#define rht_dereference_bucket(p, tbl, hash) \
180 rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200181
Thomas Graf88d6ed12015-01-02 23:00:16 +0100182#define rht_dereference_bucket_rcu(p, tbl, hash) \
183 rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash))
184
185#define rht_entry(tpos, pos, member) \
186 ({ tpos = container_of(pos, typeof(*tpos), member); 1; })
187
188/**
189 * rht_for_each_continue - continue iterating over hash chain
190 * @pos: the &struct rhash_head to use as a loop cursor.
191 * @head: the previous &struct rhash_head to continue from
192 * @tbl: the &struct bucket_table
193 * @hash: the hash value / bucket index
194 */
195#define rht_for_each_continue(pos, head, tbl, hash) \
196 for (pos = rht_dereference_bucket(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100197 !rht_is_a_nulls(pos); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100198 pos = rht_dereference_bucket((pos)->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200199
200/**
201 * rht_for_each - iterate over hash chain
Thomas Graf88d6ed12015-01-02 23:00:16 +0100202 * @pos: the &struct rhash_head to use as a loop cursor.
203 * @tbl: the &struct bucket_table
204 * @hash: the hash value / bucket index
Thomas Graf7e1e7762014-08-02 11:47:44 +0200205 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100206#define rht_for_each(pos, tbl, hash) \
207 rht_for_each_continue(pos, (tbl)->buckets[hash], tbl, hash)
208
209/**
210 * rht_for_each_entry_continue - continue iterating over hash chain
211 * @tpos: the type * to use as a loop cursor.
212 * @pos: the &struct rhash_head to use as a loop cursor.
213 * @head: the previous &struct rhash_head to continue from
214 * @tbl: the &struct bucket_table
215 * @hash: the hash value / bucket index
216 * @member: name of the &struct rhash_head within the hashable struct.
217 */
218#define rht_for_each_entry_continue(tpos, pos, head, tbl, hash, member) \
219 for (pos = rht_dereference_bucket(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100220 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100221 pos = rht_dereference_bucket((pos)->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200222
223/**
224 * rht_for_each_entry - iterate over hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100225 * @tpos: the type * to use as a loop cursor.
226 * @pos: the &struct rhash_head to use as a loop cursor.
227 * @tbl: the &struct bucket_table
228 * @hash: the hash value / bucket index
229 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200230 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100231#define rht_for_each_entry(tpos, pos, tbl, hash, member) \
232 rht_for_each_entry_continue(tpos, pos, (tbl)->buckets[hash], \
233 tbl, hash, member)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200234
235/**
236 * rht_for_each_entry_safe - safely iterate over hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100237 * @tpos: the type * to use as a loop cursor.
238 * @pos: the &struct rhash_head to use as a loop cursor.
239 * @next: the &struct rhash_head to use as next in loop cursor.
240 * @tbl: the &struct bucket_table
241 * @hash: the hash value / bucket index
242 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200243 *
244 * This hash chain list-traversal primitive allows for the looped code to
245 * remove the loop cursor from the list.
246 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100247#define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \
248 for (pos = rht_dereference_bucket((tbl)->buckets[hash], tbl, hash), \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100249 next = !rht_is_a_nulls(pos) ? \
250 rht_dereference_bucket(pos->next, tbl, hash) : NULL; \
251 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100252 pos = next)
253
254/**
255 * rht_for_each_rcu_continue - continue iterating over rcu hash chain
256 * @pos: the &struct rhash_head to use as a loop cursor.
257 * @head: the previous &struct rhash_head to continue from
258 * @tbl: the &struct bucket_table
259 * @hash: the hash value / bucket index
260 *
261 * This hash chain list-traversal primitive may safely run concurrently with
262 * the _rcu mutation primitives such as rhashtable_insert() as long as the
263 * traversal is guarded by rcu_read_lock().
264 */
265#define rht_for_each_rcu_continue(pos, head, tbl, hash) \
266 for (({barrier(); }), \
267 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100268 !rht_is_a_nulls(pos); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100269 pos = rcu_dereference_raw(pos->next))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200270
271/**
272 * rht_for_each_rcu - iterate over rcu hash chain
Thomas Graf88d6ed12015-01-02 23:00:16 +0100273 * @pos: the &struct rhash_head to use as a loop cursor.
274 * @tbl: the &struct bucket_table
275 * @hash: the hash value / bucket index
Thomas Graf7e1e7762014-08-02 11:47:44 +0200276 *
277 * This hash chain list-traversal primitive may safely run concurrently with
Thomas Graf88d6ed12015-01-02 23:00:16 +0100278 * the _rcu mutation primitives such as rhashtable_insert() as long as the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200279 * traversal is guarded by rcu_read_lock().
280 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100281#define rht_for_each_rcu(pos, tbl, hash) \
282 rht_for_each_rcu_continue(pos, (tbl)->buckets[hash], tbl, hash)
283
284/**
285 * rht_for_each_entry_rcu_continue - continue iterating over rcu hash chain
286 * @tpos: the type * to use as a loop cursor.
287 * @pos: the &struct rhash_head to use as a loop cursor.
288 * @head: the previous &struct rhash_head to continue from
289 * @tbl: the &struct bucket_table
290 * @hash: the hash value / bucket index
291 * @member: name of the &struct rhash_head within the hashable struct.
292 *
293 * This hash chain list-traversal primitive may safely run concurrently with
294 * the _rcu mutation primitives such as rhashtable_insert() as long as the
295 * traversal is guarded by rcu_read_lock().
296 */
297#define rht_for_each_entry_rcu_continue(tpos, pos, head, tbl, hash, member) \
298 for (({barrier(); }), \
299 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
Thomas Graff89bd6f2015-01-02 23:00:21 +0100300 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
Thomas Graf88d6ed12015-01-02 23:00:16 +0100301 pos = rht_dereference_bucket_rcu(pos->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200302
303/**
304 * rht_for_each_entry_rcu - iterate over rcu hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100305 * @tpos: the type * to use as a loop cursor.
306 * @pos: the &struct rhash_head to use as a loop cursor.
307 * @tbl: the &struct bucket_table
308 * @hash: the hash value / bucket index
309 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200310 *
311 * This hash chain list-traversal primitive may safely run concurrently with
Thomas Graf88d6ed12015-01-02 23:00:16 +0100312 * the _rcu mutation primitives such as rhashtable_insert() as long as the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200313 * traversal is guarded by rcu_read_lock().
314 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100315#define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \
316 rht_for_each_entry_rcu_continue(tpos, pos, (tbl)->buckets[hash],\
317 tbl, hash, member)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200318
319#endif /* _LINUX_RHASHTABLE_H */