blob: 6745545e0b22539ca38ab4e80c175e36bd6377c4 [file] [log] [blame]
Omar Sandoval88459642016-09-17 08:38:44 -06001/*
2 * Fast and scalable bitmaps.
3 *
4 * Copyright (C) 2016 Facebook
5 * Copyright (C) 2013-2014 Jens Axboe
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public
9 * License v2 as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#ifndef __LINUX_SCALE_BITMAP_H
21#define __LINUX_SCALE_BITMAP_H
22
23#include <linux/kernel.h>
24#include <linux/slab.h>
25
26/**
27 * struct sbitmap_word - Word in a &struct sbitmap.
28 */
29struct sbitmap_word {
30 /**
31 * @word: The bitmap word itself.
32 */
33 unsigned long word;
34
35 /**
36 * @depth: Number of bits being used in @word.
37 */
38 unsigned long depth;
39} ____cacheline_aligned_in_smp;
40
41/**
42 * struct sbitmap - Scalable bitmap.
43 *
44 * A &struct sbitmap is spread over multiple cachelines to avoid ping-pong. This
45 * trades off higher memory usage for better scalability.
46 */
47struct sbitmap {
48 /**
49 * @depth: Number of bits used in the whole bitmap.
50 */
51 unsigned int depth;
52
53 /**
54 * @shift: log2(number of bits used per word)
55 */
56 unsigned int shift;
57
58 /**
59 * @map_nr: Number of words (cachelines) being used for the bitmap.
60 */
61 unsigned int map_nr;
62
63 /**
64 * @map: Allocated bitmap.
65 */
66 struct sbitmap_word *map;
67};
68
69#define SBQ_WAIT_QUEUES 8
70#define SBQ_WAKE_BATCH 8
71
72/**
73 * struct sbq_wait_state - Wait queue in a &struct sbitmap_queue.
74 */
75struct sbq_wait_state {
76 /**
77 * @wait_cnt: Number of frees remaining before we wake up.
78 */
79 atomic_t wait_cnt;
80
81 /**
82 * @wait: Wait queue.
83 */
84 wait_queue_head_t wait;
85} ____cacheline_aligned_in_smp;
86
87/**
88 * struct sbitmap_queue - Scalable bitmap with the added ability to wait on free
89 * bits.
90 *
91 * A &struct sbitmap_queue uses multiple wait queues and rolling wakeups to
92 * avoid contention on the wait queue spinlock. This ensures that we don't hit a
93 * scalability wall when we run out of free bits and have to start putting tasks
94 * to sleep.
95 */
96struct sbitmap_queue {
97 /**
98 * @sb: Scalable bitmap.
99 */
100 struct sbitmap sb;
101
Omar Sandoval40aabb62016-09-17 01:28:23 -0700102 /*
103 * @alloc_hint: Cache of last successfully allocated or freed bit.
104 *
105 * This is per-cpu, which allows multiple users to stick to different
106 * cachelines until the map is exhausted.
107 */
108 unsigned int __percpu *alloc_hint;
109
Omar Sandoval88459642016-09-17 08:38:44 -0600110 /**
111 * @wake_batch: Number of bits which must be freed before we wake up any
112 * waiters.
113 */
114 unsigned int wake_batch;
115
116 /**
117 * @wake_index: Next wait queue in @ws to wake up.
118 */
119 atomic_t wake_index;
120
121 /**
122 * @ws: Wait queues.
123 */
124 struct sbq_wait_state *ws;
125};
126
127/**
128 * sbitmap_init_node() - Initialize a &struct sbitmap on a specific memory node.
129 * @sb: Bitmap to initialize.
130 * @depth: Number of bits to allocate.
131 * @shift: Use 2^@shift bits per word in the bitmap; if a negative number if
132 * given, a good default is chosen.
133 * @flags: Allocation flags.
134 * @node: Memory node to allocate on.
135 *
136 * Return: Zero on success or negative errno on failure.
137 */
138int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift,
139 gfp_t flags, int node);
140
141/**
142 * sbitmap_free() - Free memory used by a &struct sbitmap.
143 * @sb: Bitmap to free.
144 */
145static inline void sbitmap_free(struct sbitmap *sb)
146{
147 kfree(sb->map);
148 sb->map = NULL;
149}
150
151/**
152 * sbitmap_resize() - Resize a &struct sbitmap.
153 * @sb: Bitmap to resize.
154 * @depth: New number of bits to resize to.
155 *
156 * Doesn't reallocate anything. It's up to the caller to ensure that the new
157 * depth doesn't exceed the depth that the sb was initialized with.
158 */
159void sbitmap_resize(struct sbitmap *sb, unsigned int depth);
160
161/**
162 * sbitmap_get() - Try to allocate a free bit from a &struct sbitmap.
163 * @sb: Bitmap to allocate from.
164 * @alloc_hint: Hint for where to start searching for a free bit.
165 * @round_robin: If true, be stricter about allocation order; always allocate
166 * starting from the last allocated bit. This is less efficient
167 * than the default behavior (false).
168 *
169 * Return: Non-negative allocated bit number if successful, -1 otherwise.
170 */
171int sbitmap_get(struct sbitmap *sb, unsigned int alloc_hint, bool round_robin);
172
173/**
174 * sbitmap_any_bit_set() - Check for a set bit in a &struct sbitmap.
175 * @sb: Bitmap to check.
176 *
177 * Return: true if any bit in the bitmap is set, false otherwise.
178 */
179bool sbitmap_any_bit_set(const struct sbitmap *sb);
180
181/**
182 * sbitmap_any_bit_clear() - Check for an unset bit in a &struct
183 * sbitmap.
184 * @sb: Bitmap to check.
185 *
186 * Return: true if any bit in the bitmap is clear, false otherwise.
187 */
188bool sbitmap_any_bit_clear(const struct sbitmap *sb);
189
190typedef bool (*sb_for_each_fn)(struct sbitmap *, unsigned int, void *);
191
192/**
193 * sbitmap_for_each_set() - Iterate over each set bit in a &struct sbitmap.
194 * @sb: Bitmap to iterate over.
195 * @fn: Callback. Should return true to continue or false to break early.
196 * @data: Pointer to pass to callback.
197 *
198 * This is inline even though it's non-trivial so that the function calls to the
199 * callback will hopefully get optimized away.
200 */
201static inline void sbitmap_for_each_set(struct sbitmap *sb, sb_for_each_fn fn,
202 void *data)
203{
204 unsigned int i;
205
206 for (i = 0; i < sb->map_nr; i++) {
207 struct sbitmap_word *word = &sb->map[i];
208 unsigned int off, nr;
209
210 if (!word->word)
211 continue;
212
213 nr = 0;
214 off = i << sb->shift;
215 while (1) {
216 nr = find_next_bit(&word->word, word->depth, nr);
217 if (nr >= word->depth)
218 break;
219
220 if (!fn(sb, off + nr, data))
221 return;
222
223 nr++;
224 }
225 }
226}
227
228#define SB_NR_TO_INDEX(sb, bitnr) ((bitnr) >> (sb)->shift)
229#define SB_NR_TO_BIT(sb, bitnr) ((bitnr) & ((1U << (sb)->shift) - 1U))
230
231static inline unsigned long *__sbitmap_word(struct sbitmap *sb,
232 unsigned int bitnr)
233{
234 return &sb->map[SB_NR_TO_INDEX(sb, bitnr)].word;
235}
236
237/* Helpers equivalent to the operations in asm/bitops.h and linux/bitmap.h */
238
239static inline void sbitmap_set_bit(struct sbitmap *sb, unsigned int bitnr)
240{
241 set_bit(SB_NR_TO_BIT(sb, bitnr), __sbitmap_word(sb, bitnr));
242}
243
244static inline void sbitmap_clear_bit(struct sbitmap *sb, unsigned int bitnr)
245{
246 clear_bit(SB_NR_TO_BIT(sb, bitnr), __sbitmap_word(sb, bitnr));
247}
248
249static inline int sbitmap_test_bit(struct sbitmap *sb, unsigned int bitnr)
250{
251 return test_bit(SB_NR_TO_BIT(sb, bitnr), __sbitmap_word(sb, bitnr));
252}
253
254unsigned int sbitmap_weight(const struct sbitmap *sb);
255
256/**
257 * sbitmap_queue_init_node() - Initialize a &struct sbitmap_queue on a specific
258 * memory node.
259 * @sbq: Bitmap queue to initialize.
260 * @depth: See sbitmap_init_node().
261 * @shift: See sbitmap_init_node().
262 * @flags: Allocation flags.
263 * @node: Memory node to allocate on.
264 *
265 * Return: Zero on success or negative errno on failure.
266 */
267int sbitmap_queue_init_node(struct sbitmap_queue *sbq, unsigned int depth,
268 int shift, gfp_t flags, int node);
269
270/**
271 * sbitmap_queue_free() - Free memory used by a &struct sbitmap_queue.
272 *
273 * @sbq: Bitmap queue to free.
274 */
275static inline void sbitmap_queue_free(struct sbitmap_queue *sbq)
276{
277 kfree(sbq->ws);
Omar Sandoval40aabb62016-09-17 01:28:23 -0700278 free_percpu(sbq->alloc_hint);
Omar Sandoval88459642016-09-17 08:38:44 -0600279 sbitmap_free(&sbq->sb);
280}
281
282/**
283 * sbitmap_queue_resize() - Resize a &struct sbitmap_queue.
284 * @sbq: Bitmap queue to resize.
285 * @depth: New number of bits to resize to.
286 *
287 * Like sbitmap_resize(), this doesn't reallocate anything. It has to do
288 * some extra work on the &struct sbitmap_queue, so it's not safe to just
289 * resize the underlying &struct sbitmap.
290 */
291void sbitmap_queue_resize(struct sbitmap_queue *sbq, unsigned int depth);
292
293/**
Omar Sandoval40aabb62016-09-17 01:28:23 -0700294 * __sbitmap_queue_get() - Try to allocate a free bit from a &struct
295 * sbitmap_queue with preemption already disabled.
296 * @sbq: Bitmap queue to allocate from.
297 * @round_robin: See sbitmap_get().
298 *
299 * Return: Non-negative allocated bit number if successful, -1 otherwise.
300 */
301int __sbitmap_queue_get(struct sbitmap_queue *sbq, bool round_robin);
302
303/**
304 * sbitmap_queue_get() - Try to allocate a free bit from a &struct
305 * sbitmap_queue.
306 * @sbq: Bitmap queue to allocate from.
307 * @round_robin: See sbitmap_get().
308 * @cpu: Output parameter; will contain the CPU we ran on (e.g., to be passed to
309 * sbitmap_queue_clear()).
310 *
311 * Return: Non-negative allocated bit number if successful, -1 otherwise.
312 */
313static inline int sbitmap_queue_get(struct sbitmap_queue *sbq, bool round_robin,
314 unsigned int *cpu)
315{
316 int nr;
317
318 *cpu = get_cpu();
319 nr = __sbitmap_queue_get(sbq, round_robin);
320 put_cpu();
321 return nr;
322}
323
324/**
Omar Sandoval88459642016-09-17 08:38:44 -0600325 * sbitmap_queue_clear() - Free an allocated bit and wake up waiters on a
326 * &struct sbitmap_queue.
327 * @sbq: Bitmap to free from.
328 * @nr: Bit number to free.
Omar Sandoval40aabb62016-09-17 01:28:23 -0700329 * @round_robin: See sbitmap_get().
330 * @cpu: CPU the bit was allocated on.
Omar Sandoval88459642016-09-17 08:38:44 -0600331 */
Omar Sandoval40aabb62016-09-17 01:28:23 -0700332void sbitmap_queue_clear(struct sbitmap_queue *sbq, unsigned int nr,
333 bool round_robin, unsigned int cpu);
Omar Sandoval88459642016-09-17 08:38:44 -0600334
335static inline int sbq_index_inc(int index)
336{
337 return (index + 1) & (SBQ_WAIT_QUEUES - 1);
338}
339
340static inline void sbq_index_atomic_inc(atomic_t *index)
341{
342 int old = atomic_read(index);
343 int new = sbq_index_inc(old);
344 atomic_cmpxchg(index, old, new);
345}
346
347/**
348 * sbq_wait_ptr() - Get the next wait queue to use for a &struct
349 * sbitmap_queue.
350 * @sbq: Bitmap queue to wait on.
351 * @wait_index: A counter per "user" of @sbq.
352 */
353static inline struct sbq_wait_state *sbq_wait_ptr(struct sbitmap_queue *sbq,
354 atomic_t *wait_index)
355{
356 struct sbq_wait_state *ws;
357
358 ws = &sbq->ws[atomic_read(wait_index)];
359 sbq_index_atomic_inc(wait_index);
360 return ws;
361}
362
363/**
364 * sbitmap_queue_wake_all() - Wake up everything waiting on a &struct
365 * sbitmap_queue.
366 * @sbq: Bitmap queue to wake up.
367 */
368void sbitmap_queue_wake_all(struct sbitmap_queue *sbq);
369
370#endif /* __LINUX_SCALE_BITMAP_H */