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