blob: 85f43b1718d4451115909cb6aa5e20d387c3fd6e [file] [log] [blame]
Kent Overstreet798ab482013-08-16 22:04:37 +00001/*
2 * Percpu IDA library
3 *
4 * Copyright (C) 2013 Datera, Inc. Kent Overstreet
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2, or (at
9 * your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * 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
17#include <linux/bitmap.h>
18#include <linux/bitops.h>
19#include <linux/bug.h>
20#include <linux/err.h>
21#include <linux/export.h>
22#include <linux/hardirq.h>
23#include <linux/idr.h>
24#include <linux/init.h>
25#include <linux/kernel.h>
26#include <linux/percpu.h>
27#include <linux/sched.h>
28#include <linux/slab.h>
29#include <linux/string.h>
30#include <linux/spinlock.h>
31#include <linux/percpu_ida.h>
32
Kent Overstreet798ab482013-08-16 22:04:37 +000033struct percpu_ida_cpu {
34 /*
35 * Even though this is percpu, we need a lock for tag stealing by remote
36 * CPUs:
37 */
38 spinlock_t lock;
39
40 /* nr_free/freelist form a stack of free IDs */
41 unsigned nr_free;
42 unsigned freelist[];
43};
44
45static inline void move_tags(unsigned *dst, unsigned *dst_nr,
46 unsigned *src, unsigned *src_nr,
47 unsigned nr)
48{
49 *src_nr -= nr;
50 memcpy(dst + *dst_nr, src + *src_nr, sizeof(unsigned) * nr);
51 *dst_nr += nr;
52}
53
54/*
55 * Try to steal tags from a remote cpu's percpu freelist.
56 *
Shaohua Lid8355022013-12-31 11:38:27 +080057 * We first check how many percpu freelists have tags
Kent Overstreet798ab482013-08-16 22:04:37 +000058 *
59 * Then we iterate through the cpus until we find some tags - we don't attempt
60 * to find the "best" cpu to steal from, to keep cacheline bouncing to a
61 * minimum.
62 */
63static inline void steal_tags(struct percpu_ida *pool,
64 struct percpu_ida_cpu *tags)
65{
66 unsigned cpus_have_tags, cpu = pool->cpu_last_stolen;
67 struct percpu_ida_cpu *remote;
68
69 for (cpus_have_tags = cpumask_weight(&pool->cpus_have_tags);
Shaohua Lid8355022013-12-31 11:38:27 +080070 cpus_have_tags; cpus_have_tags--) {
Kent Overstreet798ab482013-08-16 22:04:37 +000071 cpu = cpumask_next(cpu, &pool->cpus_have_tags);
72
73 if (cpu >= nr_cpu_ids) {
74 cpu = cpumask_first(&pool->cpus_have_tags);
75 if (cpu >= nr_cpu_ids)
76 BUG();
77 }
78
79 pool->cpu_last_stolen = cpu;
80 remote = per_cpu_ptr(pool->tag_cpu, cpu);
81
82 cpumask_clear_cpu(cpu, &pool->cpus_have_tags);
83
84 if (remote == tags)
85 continue;
86
87 spin_lock(&remote->lock);
88
89 if (remote->nr_free) {
90 memcpy(tags->freelist,
91 remote->freelist,
92 sizeof(unsigned) * remote->nr_free);
93
94 tags->nr_free = remote->nr_free;
95 remote->nr_free = 0;
96 }
97
98 spin_unlock(&remote->lock);
99
100 if (tags->nr_free)
101 break;
102 }
103}
104
105/*
106 * Pop up to IDA_PCPU_BATCH_MOVE IDs off the global freelist, and push them onto
107 * our percpu freelist:
108 */
109static inline void alloc_global_tags(struct percpu_ida *pool,
110 struct percpu_ida_cpu *tags)
111{
112 move_tags(tags->freelist, &tags->nr_free,
113 pool->freelist, &pool->nr_free,
Shaohua Lie26b53d2013-10-15 09:05:01 +0800114 min(pool->nr_free, pool->percpu_batch_size));
Kent Overstreet798ab482013-08-16 22:04:37 +0000115}
116
Nick Swenson6b378382013-10-02 17:55:45 -0700117static inline unsigned alloc_local_tag(struct percpu_ida_cpu *tags)
Kent Overstreet798ab482013-08-16 22:04:37 +0000118{
119 int tag = -ENOSPC;
120
121 spin_lock(&tags->lock);
122 if (tags->nr_free)
123 tag = tags->freelist[--tags->nr_free];
124 spin_unlock(&tags->lock);
125
126 return tag;
127}
128
129/**
130 * percpu_ida_alloc - allocate a tag
131 * @pool: pool to allocate from
132 * @gfp: gfp flags
133 *
134 * Returns a tag - an integer in the range [0..nr_tags) (passed to
135 * tag_pool_init()), or otherwise -ENOSPC on allocation failure.
136 *
137 * Safe to be called from interrupt context (assuming it isn't passed
138 * __GFP_WAIT, of course).
139 *
140 * @gfp indicates whether or not to wait until a free id is available (it's not
141 * used for internal memory allocations); thus if passed __GFP_WAIT we may sleep
142 * however long it takes until another thread frees an id (same semantics as a
143 * mempool).
144 *
145 * Will not fail if passed __GFP_WAIT.
146 */
147int percpu_ida_alloc(struct percpu_ida *pool, gfp_t gfp)
148{
149 DEFINE_WAIT(wait);
150 struct percpu_ida_cpu *tags;
151 unsigned long flags;
152 int tag;
153
154 local_irq_save(flags);
155 tags = this_cpu_ptr(pool->tag_cpu);
156
157 /* Fastpath */
Nick Swenson6b378382013-10-02 17:55:45 -0700158 tag = alloc_local_tag(tags);
Kent Overstreet798ab482013-08-16 22:04:37 +0000159 if (likely(tag >= 0)) {
160 local_irq_restore(flags);
161 return tag;
162 }
163
164 while (1) {
165 spin_lock(&pool->lock);
166
167 /*
168 * prepare_to_wait() must come before steal_tags(), in case
169 * percpu_ida_free() on another cpu flips a bit in
170 * cpus_have_tags
171 *
172 * global lock held and irqs disabled, don't need percpu lock
173 */
174 prepare_to_wait(&pool->wait, &wait, TASK_UNINTERRUPTIBLE);
175
176 if (!tags->nr_free)
177 alloc_global_tags(pool, tags);
178 if (!tags->nr_free)
179 steal_tags(pool, tags);
180
181 if (tags->nr_free) {
182 tag = tags->freelist[--tags->nr_free];
183 if (tags->nr_free)
184 cpumask_set_cpu(smp_processor_id(),
185 &pool->cpus_have_tags);
186 }
187
188 spin_unlock(&pool->lock);
189 local_irq_restore(flags);
190
191 if (tag >= 0 || !(gfp & __GFP_WAIT))
192 break;
193
194 schedule();
195
196 local_irq_save(flags);
197 tags = this_cpu_ptr(pool->tag_cpu);
198 }
199
200 finish_wait(&pool->wait, &wait);
201 return tag;
202}
203EXPORT_SYMBOL_GPL(percpu_ida_alloc);
204
205/**
206 * percpu_ida_free - free a tag
207 * @pool: pool @tag was allocated from
208 * @tag: a tag previously allocated with percpu_ida_alloc()
209 *
210 * Safe to be called from interrupt context.
211 */
212void percpu_ida_free(struct percpu_ida *pool, unsigned tag)
213{
214 struct percpu_ida_cpu *tags;
215 unsigned long flags;
216 unsigned nr_free;
217
218 BUG_ON(tag >= pool->nr_tags);
219
220 local_irq_save(flags);
221 tags = this_cpu_ptr(pool->tag_cpu);
222
223 spin_lock(&tags->lock);
224 tags->freelist[tags->nr_free++] = tag;
225
226 nr_free = tags->nr_free;
227 spin_unlock(&tags->lock);
228
229 if (nr_free == 1) {
230 cpumask_set_cpu(smp_processor_id(),
231 &pool->cpus_have_tags);
232 wake_up(&pool->wait);
233 }
234
Shaohua Lie26b53d2013-10-15 09:05:01 +0800235 if (nr_free == pool->percpu_max_size) {
Kent Overstreet798ab482013-08-16 22:04:37 +0000236 spin_lock(&pool->lock);
237
238 /*
239 * Global lock held and irqs disabled, don't need percpu
240 * lock
241 */
Shaohua Lie26b53d2013-10-15 09:05:01 +0800242 if (tags->nr_free == pool->percpu_max_size) {
Kent Overstreet798ab482013-08-16 22:04:37 +0000243 move_tags(pool->freelist, &pool->nr_free,
244 tags->freelist, &tags->nr_free,
Shaohua Lie26b53d2013-10-15 09:05:01 +0800245 pool->percpu_batch_size);
Kent Overstreet798ab482013-08-16 22:04:37 +0000246
247 wake_up(&pool->wait);
248 }
249 spin_unlock(&pool->lock);
250 }
251
252 local_irq_restore(flags);
253}
254EXPORT_SYMBOL_GPL(percpu_ida_free);
255
256/**
257 * percpu_ida_destroy - release a tag pool's resources
258 * @pool: pool to free
259 *
260 * Frees the resources allocated by percpu_ida_init().
261 */
262void percpu_ida_destroy(struct percpu_ida *pool)
263{
264 free_percpu(pool->tag_cpu);
265 free_pages((unsigned long) pool->freelist,
266 get_order(pool->nr_tags * sizeof(unsigned)));
267}
268EXPORT_SYMBOL_GPL(percpu_ida_destroy);
269
270/**
271 * percpu_ida_init - initialize a percpu tag pool
272 * @pool: pool to initialize
273 * @nr_tags: number of tags that will be available for allocation
274 *
275 * Initializes @pool so that it can be used to allocate tags - integers in the
276 * range [0, nr_tags). Typically, they'll be used by driver code to refer to a
277 * preallocated array of tag structures.
278 *
279 * Allocation is percpu, but sharding is limited by nr_tags - for best
280 * performance, the workload should not span more cpus than nr_tags / 128.
281 */
Shaohua Lie26b53d2013-10-15 09:05:01 +0800282int __percpu_ida_init(struct percpu_ida *pool, unsigned long nr_tags,
283 unsigned long max_size, unsigned long batch_size)
Kent Overstreet798ab482013-08-16 22:04:37 +0000284{
285 unsigned i, cpu, order;
286
287 memset(pool, 0, sizeof(*pool));
288
289 init_waitqueue_head(&pool->wait);
290 spin_lock_init(&pool->lock);
291 pool->nr_tags = nr_tags;
Shaohua Lie26b53d2013-10-15 09:05:01 +0800292 pool->percpu_max_size = max_size;
293 pool->percpu_batch_size = batch_size;
Kent Overstreet798ab482013-08-16 22:04:37 +0000294
295 /* Guard against overflow */
296 if (nr_tags > (unsigned) INT_MAX + 1) {
297 pr_err("percpu_ida_init(): nr_tags too large\n");
298 return -EINVAL;
299 }
300
301 order = get_order(nr_tags * sizeof(unsigned));
302 pool->freelist = (void *) __get_free_pages(GFP_KERNEL, order);
303 if (!pool->freelist)
304 return -ENOMEM;
305
306 for (i = 0; i < nr_tags; i++)
307 pool->freelist[i] = i;
308
309 pool->nr_free = nr_tags;
310
311 pool->tag_cpu = __alloc_percpu(sizeof(struct percpu_ida_cpu) +
Shaohua Lie26b53d2013-10-15 09:05:01 +0800312 pool->percpu_max_size * sizeof(unsigned),
Kent Overstreet798ab482013-08-16 22:04:37 +0000313 sizeof(unsigned));
314 if (!pool->tag_cpu)
315 goto err;
316
317 for_each_possible_cpu(cpu)
318 spin_lock_init(&per_cpu_ptr(pool->tag_cpu, cpu)->lock);
319
320 return 0;
321err:
322 percpu_ida_destroy(pool);
323 return -ENOMEM;
324}
Shaohua Lie26b53d2013-10-15 09:05:01 +0800325EXPORT_SYMBOL_GPL(__percpu_ida_init);
Shaohua Li7fc2ba12013-10-15 09:05:02 +0800326
327/**
328 * percpu_ida_for_each_free - iterate free ids of a pool
329 * @pool: pool to iterate
330 * @fn: interate callback function
331 * @data: parameter for @fn
332 *
333 * Note, this doesn't guarantee to iterate all free ids restrictly. Some free
334 * ids might be missed, some might be iterated duplicated, and some might
335 * be iterated and not free soon.
336 */
337int percpu_ida_for_each_free(struct percpu_ida *pool, percpu_ida_cb fn,
338 void *data)
339{
340 unsigned long flags;
341 struct percpu_ida_cpu *remote;
342 unsigned cpu, i, err = 0;
343
344 local_irq_save(flags);
345 for_each_possible_cpu(cpu) {
346 remote = per_cpu_ptr(pool->tag_cpu, cpu);
347 spin_lock(&remote->lock);
348 for (i = 0; i < remote->nr_free; i++) {
349 err = fn(remote->freelist[i], data);
350 if (err)
351 break;
352 }
353 spin_unlock(&remote->lock);
354 if (err)
355 goto out;
356 }
357
358 spin_lock(&pool->lock);
359 for (i = 0; i < pool->nr_free; i++) {
360 err = fn(pool->freelist[i], data);
361 if (err)
362 break;
363 }
364 spin_unlock(&pool->lock);
365out:
366 local_irq_restore(flags);
367 return err;
368}
369EXPORT_SYMBOL_GPL(percpu_ida_for_each_free);
Shaohua Li1dddc012013-10-15 09:05:03 +0800370
371/**
372 * percpu_ida_free_tags - return free tags number of a specific cpu or global pool
373 * @pool: pool related
374 * @cpu: specific cpu or global pool if @cpu == nr_cpu_ids
375 *
376 * Note: this just returns a snapshot of free tags number.
377 */
378unsigned percpu_ida_free_tags(struct percpu_ida *pool, int cpu)
379{
380 struct percpu_ida_cpu *remote;
381 if (cpu == nr_cpu_ids)
382 return pool->nr_free;
383 remote = per_cpu_ptr(pool->tag_cpu, cpu);
384 return remote->nr_free;
385}
386EXPORT_SYMBOL_GPL(percpu_ida_free_tags);