]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - include/linux/rhashtable.h
rhashtable: Per bucket locks & deferred expansion/shrinking
[mirror_ubuntu-bionic-kernel.git] / include / linux / rhashtable.h
CommitLineData
7e1e7763
TG
1/*
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
21#include <linux/rculist.h>
97defe1e 22#include <linux/workqueue.h>
7e1e7763
TG
23
24struct rhash_head {
5300fdcb 25 struct rhash_head __rcu *next;
7e1e7763
TG
26};
27
28#define INIT_HASH_HEAD(ptr) ((ptr)->next = NULL)
29
97defe1e
TG
30/**
31 * struct bucket_table - Table of hash buckets
32 * @size: Number of hash buckets
33 * @locks_mask: Mask to apply before accessing locks[]
34 * @locks: Array of spinlocks protecting individual buckets
35 * @buckets: size * hash buckets
36 */
7e1e7763
TG
37struct bucket_table {
38 size_t size;
97defe1e
TG
39 unsigned int locks_mask;
40 spinlock_t *locks;
7e1e7763
TG
41 struct rhash_head __rcu *buckets[];
42};
43
44typedef u32 (*rht_hashfn_t)(const void *data, u32 len, u32 seed);
45typedef u32 (*rht_obj_hashfn_t)(const void *data, u32 seed);
46
47struct rhashtable;
48
49/**
50 * struct rhashtable_params - Hash table construction parameters
51 * @nelem_hint: Hint on number of elements, should be 75% of desired size
52 * @key_len: Length of key
53 * @key_offset: Offset of key in struct to be hashed
54 * @head_offset: Offset of rhash_head in struct to be hashed
55 * @hash_rnd: Seed to use while hashing
56 * @max_shift: Maximum number of shifts while expanding
94000176 57 * @min_shift: Minimum number of shifts while shrinking
97defe1e 58 * @locks_mul: Number of bucket locks to allocate per cpu (default: 128)
7e1e7763
TG
59 * @hashfn: Function to hash key
60 * @obj_hashfn: Function to hash object
61 * @grow_decision: If defined, may return true if table should expand
62 * @shrink_decision: If defined, may return true if table should shrink
7e1e7763
TG
63 */
64struct rhashtable_params {
65 size_t nelem_hint;
66 size_t key_len;
67 size_t key_offset;
68 size_t head_offset;
69 u32 hash_rnd;
70 size_t max_shift;
94000176 71 size_t min_shift;
97defe1e 72 size_t locks_mul;
7e1e7763
TG
73 rht_hashfn_t hashfn;
74 rht_obj_hashfn_t obj_hashfn;
75 bool (*grow_decision)(const struct rhashtable *ht,
76 size_t new_size);
77 bool (*shrink_decision)(const struct rhashtable *ht,
78 size_t new_size);
7e1e7763
TG
79};
80
81/**
82 * struct rhashtable - Hash table handle
83 * @tbl: Bucket table
97defe1e 84 * @future_tbl: Table under construction during expansion/shrinking
7e1e7763
TG
85 * @nelems: Number of elements in table
86 * @shift: Current size (1 << shift)
87 * @p: Configuration parameters
97defe1e
TG
88 * @run_work: Deferred worker to expand/shrink asynchronously
89 * @mutex: Mutex to protect current/future table swapping
90 * @being_destroyed: True if table is set up for destruction
7e1e7763
TG
91 */
92struct rhashtable {
93 struct bucket_table __rcu *tbl;
97defe1e
TG
94 struct bucket_table __rcu *future_tbl;
95 atomic_t nelems;
7e1e7763
TG
96 size_t shift;
97 struct rhashtable_params p;
97defe1e
TG
98 struct delayed_work run_work;
99 struct mutex mutex;
100 bool being_destroyed;
7e1e7763
TG
101};
102
103#ifdef CONFIG_PROVE_LOCKING
97defe1e 104int lockdep_rht_mutex_is_held(struct rhashtable *ht);
88d6ed15 105int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash);
7e1e7763 106#else
97defe1e 107static inline int lockdep_rht_mutex_is_held(struct rhashtable *ht)
7e1e7763
TG
108{
109 return 1;
110}
88d6ed15
TG
111
112static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl,
113 u32 hash)
114{
115 return 1;
116}
7e1e7763
TG
117#endif /* CONFIG_PROVE_LOCKING */
118
119int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params);
120
6eba8224
TG
121void rhashtable_insert(struct rhashtable *ht, struct rhash_head *node);
122bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *node);
7e1e7763
TG
123
124bool rht_grow_above_75(const struct rhashtable *ht, size_t new_size);
125bool rht_shrink_below_30(const struct rhashtable *ht, size_t new_size);
126
6eba8224
TG
127int rhashtable_expand(struct rhashtable *ht);
128int rhashtable_shrink(struct rhashtable *ht);
7e1e7763 129
97defe1e
TG
130void *rhashtable_lookup(struct rhashtable *ht, const void *key);
131void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key,
7e1e7763
TG
132 bool (*compare)(void *, void *), void *arg);
133
97defe1e 134void rhashtable_destroy(struct rhashtable *ht);
7e1e7763
TG
135
136#define rht_dereference(p, ht) \
137 rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht))
138
139#define rht_dereference_rcu(p, ht) \
140 rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht))
141
88d6ed15
TG
142#define rht_dereference_bucket(p, tbl, hash) \
143 rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash))
7e1e7763 144
88d6ed15
TG
145#define rht_dereference_bucket_rcu(p, tbl, hash) \
146 rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash))
147
148#define rht_entry(tpos, pos, member) \
149 ({ tpos = container_of(pos, typeof(*tpos), member); 1; })
7e1e7763
TG
150
151/**
88d6ed15
TG
152 * rht_for_each_continue - continue iterating over hash chain
153 * @pos: the &struct rhash_head to use as a loop cursor.
154 * @head: the previous &struct rhash_head to continue from
155 * @tbl: the &struct bucket_table
156 * @hash: the hash value / bucket index
7e1e7763 157 */
88d6ed15
TG
158#define rht_for_each_continue(pos, head, tbl, hash) \
159 for (pos = rht_dereference_bucket(head, tbl, hash); \
7e1e7763 160 pos; \
88d6ed15
TG
161 pos = rht_dereference_bucket((pos)->next, tbl, hash))
162
163/**
164 * rht_for_each - iterate over hash chain
165 * @pos: the &struct rhash_head to use as a loop cursor.
166 * @tbl: the &struct bucket_table
167 * @hash: the hash value / bucket index
168 */
169#define rht_for_each(pos, tbl, hash) \
170 rht_for_each_continue(pos, (tbl)->buckets[hash], tbl, hash)
171
172/**
173 * rht_for_each_entry_continue - continue iterating over hash chain
174 * @tpos: the type * to use as a loop cursor.
175 * @pos: the &struct rhash_head to use as a loop cursor.
176 * @head: the previous &struct rhash_head to continue from
177 * @tbl: the &struct bucket_table
178 * @hash: the hash value / bucket index
179 * @member: name of the &struct rhash_head within the hashable struct.
180 */
181#define rht_for_each_entry_continue(tpos, pos, head, tbl, hash, member) \
182 for (pos = rht_dereference_bucket(head, tbl, hash); \
183 pos && rht_entry(tpos, pos, member); \
184 pos = rht_dereference_bucket((pos)->next, tbl, hash))
7e1e7763
TG
185
186/**
187 * rht_for_each_entry - iterate over hash chain of given type
88d6ed15
TG
188 * @tpos: the type * to use as a loop cursor.
189 * @pos: the &struct rhash_head to use as a loop cursor.
190 * @tbl: the &struct bucket_table
191 * @hash: the hash value / bucket index
192 * @member: name of the &struct rhash_head within the hashable struct.
7e1e7763 193 */
88d6ed15
TG
194#define rht_for_each_entry(tpos, pos, tbl, hash, member) \
195 rht_for_each_entry_continue(tpos, pos, (tbl)->buckets[hash], \
196 tbl, hash, member)
7e1e7763
TG
197
198/**
199 * rht_for_each_entry_safe - safely iterate over hash chain of given type
88d6ed15
TG
200 * @tpos: the type * to use as a loop cursor.
201 * @pos: the &struct rhash_head to use as a loop cursor.
202 * @next: the &struct rhash_head to use as next in loop cursor.
203 * @tbl: the &struct bucket_table
204 * @hash: the hash value / bucket index
205 * @member: name of the &struct rhash_head within the hashable struct.
7e1e7763
TG
206 *
207 * This hash chain list-traversal primitive allows for the looped code to
208 * remove the loop cursor from the list.
209 */
88d6ed15
TG
210#define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \
211 for (pos = rht_dereference_bucket((tbl)->buckets[hash], tbl, hash), \
212 next = pos ? rht_dereference_bucket(pos->next, tbl, hash) \
213 : NULL; \
214 pos && rht_entry(tpos, pos, member); \
215 pos = next)
216
217/**
218 * rht_for_each_rcu_continue - continue iterating over rcu hash chain
219 * @pos: the &struct rhash_head to use as a loop cursor.
220 * @head: the previous &struct rhash_head to continue from
221 * @tbl: the &struct bucket_table
222 * @hash: the hash value / bucket index
223 *
224 * This hash chain list-traversal primitive may safely run concurrently with
225 * the _rcu mutation primitives such as rhashtable_insert() as long as the
226 * traversal is guarded by rcu_read_lock().
227 */
228#define rht_for_each_rcu_continue(pos, head, tbl, hash) \
229 for (({barrier(); }), \
230 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
231 pos; \
232 pos = rcu_dereference_raw(pos->next))
7e1e7763
TG
233
234/**
235 * rht_for_each_rcu - iterate over rcu hash chain
88d6ed15
TG
236 * @pos: the &struct rhash_head to use as a loop cursor.
237 * @tbl: the &struct bucket_table
238 * @hash: the hash value / bucket index
7e1e7763
TG
239 *
240 * This hash chain list-traversal primitive may safely run concurrently with
88d6ed15 241 * the _rcu mutation primitives such as rhashtable_insert() as long as the
7e1e7763
TG
242 * traversal is guarded by rcu_read_lock().
243 */
88d6ed15
TG
244#define rht_for_each_rcu(pos, tbl, hash) \
245 rht_for_each_rcu_continue(pos, (tbl)->buckets[hash], tbl, hash)
246
247/**
248 * rht_for_each_entry_rcu_continue - continue iterating over rcu hash chain
249 * @tpos: the type * to use as a loop cursor.
250 * @pos: the &struct rhash_head to use as a loop cursor.
251 * @head: the previous &struct rhash_head to continue from
252 * @tbl: the &struct bucket_table
253 * @hash: the hash value / bucket index
254 * @member: name of the &struct rhash_head within the hashable struct.
255 *
256 * This hash chain list-traversal primitive may safely run concurrently with
257 * the _rcu mutation primitives such as rhashtable_insert() as long as the
258 * traversal is guarded by rcu_read_lock().
259 */
260#define rht_for_each_entry_rcu_continue(tpos, pos, head, tbl, hash, member) \
261 for (({barrier(); }), \
262 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
263 pos && rht_entry(tpos, pos, member); \
264 pos = rht_dereference_bucket_rcu(pos->next, tbl, hash))
7e1e7763
TG
265
266/**
267 * rht_for_each_entry_rcu - iterate over rcu hash chain of given type
88d6ed15
TG
268 * @tpos: the type * to use as a loop cursor.
269 * @pos: the &struct rhash_head to use as a loop cursor.
270 * @tbl: the &struct bucket_table
271 * @hash: the hash value / bucket index
272 * @member: name of the &struct rhash_head within the hashable struct.
7e1e7763
TG
273 *
274 * This hash chain list-traversal primitive may safely run concurrently with
88d6ed15 275 * the _rcu mutation primitives such as rhashtable_insert() as long as the
7e1e7763
TG
276 * traversal is guarded by rcu_read_lock().
277 */
88d6ed15
TG
278#define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \
279 rht_for_each_entry_rcu_continue(tpos, pos, (tbl)->buckets[hash],\
280 tbl, hash, member)
7e1e7763
TG
281
282#endif /* _LINUX_RHASHTABLE_H */