blob: b0698ea972c65025976e87d6e1a5bb5beac92747 [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 *
57 * We first check how many percpu freelists have tags - we don't steal tags
58 * unless enough percpu freelists have tags on them that it's possible more than
59 * half the total tags could be stuck on remote percpu freelists.
60 *
61 * Then we iterate through the cpus until we find some tags - we don't attempt
62 * to find the "best" cpu to steal from, to keep cacheline bouncing to a
63 * minimum.
64 */
65static inline void steal_tags(struct percpu_ida *pool,
66 struct percpu_ida_cpu *tags)
67{
68 unsigned cpus_have_tags, cpu = pool->cpu_last_stolen;
69 struct percpu_ida_cpu *remote;
70
71 for (cpus_have_tags = cpumask_weight(&pool->cpus_have_tags);
Shaohua Lie26b53d2013-10-15 09:05:01 +080072 cpus_have_tags * pool->percpu_max_size > pool->nr_tags / 2;
Kent Overstreet798ab482013-08-16 22:04:37 +000073 cpus_have_tags--) {
74 cpu = cpumask_next(cpu, &pool->cpus_have_tags);
75
76 if (cpu >= nr_cpu_ids) {
77 cpu = cpumask_first(&pool->cpus_have_tags);
78 if (cpu >= nr_cpu_ids)
79 BUG();
80 }
81
82 pool->cpu_last_stolen = cpu;
83 remote = per_cpu_ptr(pool->tag_cpu, cpu);
84
85 cpumask_clear_cpu(cpu, &pool->cpus_have_tags);
86
87 if (remote == tags)
88 continue;
89
90 spin_lock(&remote->lock);
91
92 if (remote->nr_free) {
93 memcpy(tags->freelist,
94 remote->freelist,
95 sizeof(unsigned) * remote->nr_free);
96
97 tags->nr_free = remote->nr_free;
98 remote->nr_free = 0;
99 }
100
101 spin_unlock(&remote->lock);
102
103 if (tags->nr_free)
104 break;
105 }
106}
107
108/*
109 * Pop up to IDA_PCPU_BATCH_MOVE IDs off the global freelist, and push them onto
110 * our percpu freelist:
111 */
112static inline void alloc_global_tags(struct percpu_ida *pool,
113 struct percpu_ida_cpu *tags)
114{
115 move_tags(tags->freelist, &tags->nr_free,
116 pool->freelist, &pool->nr_free,
Shaohua Lie26b53d2013-10-15 09:05:01 +0800117 min(pool->nr_free, pool->percpu_batch_size));
Kent Overstreet798ab482013-08-16 22:04:37 +0000118}
119
120static inline unsigned alloc_local_tag(struct percpu_ida *pool,
121 struct percpu_ida_cpu *tags)
122{
123 int tag = -ENOSPC;
124
125 spin_lock(&tags->lock);
126 if (tags->nr_free)
127 tag = tags->freelist[--tags->nr_free];
128 spin_unlock(&tags->lock);
129
130 return tag;
131}
132
133/**
134 * percpu_ida_alloc - allocate a tag
135 * @pool: pool to allocate from
136 * @gfp: gfp flags
137 *
138 * Returns a tag - an integer in the range [0..nr_tags) (passed to
139 * tag_pool_init()), or otherwise -ENOSPC on allocation failure.
140 *
141 * Safe to be called from interrupt context (assuming it isn't passed
142 * __GFP_WAIT, of course).
143 *
144 * @gfp indicates whether or not to wait until a free id is available (it's not
145 * used for internal memory allocations); thus if passed __GFP_WAIT we may sleep
146 * however long it takes until another thread frees an id (same semantics as a
147 * mempool).
148 *
149 * Will not fail if passed __GFP_WAIT.
150 */
151int percpu_ida_alloc(struct percpu_ida *pool, gfp_t gfp)
152{
153 DEFINE_WAIT(wait);
154 struct percpu_ida_cpu *tags;
155 unsigned long flags;
156 int tag;
157
158 local_irq_save(flags);
159 tags = this_cpu_ptr(pool->tag_cpu);
160
161 /* Fastpath */
162 tag = alloc_local_tag(pool, tags);
163 if (likely(tag >= 0)) {
164 local_irq_restore(flags);
165 return tag;
166 }
167
168 while (1) {
169 spin_lock(&pool->lock);
170
171 /*
172 * prepare_to_wait() must come before steal_tags(), in case
173 * percpu_ida_free() on another cpu flips a bit in
174 * cpus_have_tags
175 *
176 * global lock held and irqs disabled, don't need percpu lock
177 */
178 prepare_to_wait(&pool->wait, &wait, TASK_UNINTERRUPTIBLE);
179
180 if (!tags->nr_free)
181 alloc_global_tags(pool, tags);
182 if (!tags->nr_free)
183 steal_tags(pool, tags);
184
185 if (tags->nr_free) {
186 tag = tags->freelist[--tags->nr_free];
187 if (tags->nr_free)
188 cpumask_set_cpu(smp_processor_id(),
189 &pool->cpus_have_tags);
190 }
191
192 spin_unlock(&pool->lock);
193 local_irq_restore(flags);
194
195 if (tag >= 0 || !(gfp & __GFP_WAIT))
196 break;
197
198 schedule();
199
200 local_irq_save(flags);
201 tags = this_cpu_ptr(pool->tag_cpu);
202 }
203
204 finish_wait(&pool->wait, &wait);
205 return tag;
206}
207EXPORT_SYMBOL_GPL(percpu_ida_alloc);
208
209/**
210 * percpu_ida_free - free a tag
211 * @pool: pool @tag was allocated from
212 * @tag: a tag previously allocated with percpu_ida_alloc()
213 *
214 * Safe to be called from interrupt context.
215 */
216void percpu_ida_free(struct percpu_ida *pool, unsigned tag)
217{
218 struct percpu_ida_cpu *tags;
219 unsigned long flags;
220 unsigned nr_free;
221
222 BUG_ON(tag >= pool->nr_tags);
223
224 local_irq_save(flags);
225 tags = this_cpu_ptr(pool->tag_cpu);
226
227 spin_lock(&tags->lock);
228 tags->freelist[tags->nr_free++] = tag;
229
230 nr_free = tags->nr_free;
231 spin_unlock(&tags->lock);
232
233 if (nr_free == 1) {
234 cpumask_set_cpu(smp_processor_id(),
235 &pool->cpus_have_tags);
236 wake_up(&pool->wait);
237 }
238
Shaohua Lie26b53d2013-10-15 09:05:01 +0800239 if (nr_free == pool->percpu_max_size) {
Kent Overstreet798ab482013-08-16 22:04:37 +0000240 spin_lock(&pool->lock);
241
242 /*
243 * Global lock held and irqs disabled, don't need percpu
244 * lock
245 */
Shaohua Lie26b53d2013-10-15 09:05:01 +0800246 if (tags->nr_free == pool->percpu_max_size) {
Kent Overstreet798ab482013-08-16 22:04:37 +0000247 move_tags(pool->freelist, &pool->nr_free,
248 tags->freelist, &tags->nr_free,
Shaohua Lie26b53d2013-10-15 09:05:01 +0800249 pool->percpu_batch_size);
Kent Overstreet798ab482013-08-16 22:04:37 +0000250
251 wake_up(&pool->wait);
252 }
253 spin_unlock(&pool->lock);
254 }
255
256 local_irq_restore(flags);
257}
258EXPORT_SYMBOL_GPL(percpu_ida_free);
259
260/**
261 * percpu_ida_destroy - release a tag pool's resources
262 * @pool: pool to free
263 *
264 * Frees the resources allocated by percpu_ida_init().
265 */
266void percpu_ida_destroy(struct percpu_ida *pool)
267{
268 free_percpu(pool->tag_cpu);
269 free_pages((unsigned long) pool->freelist,
270 get_order(pool->nr_tags * sizeof(unsigned)));
271}
272EXPORT_SYMBOL_GPL(percpu_ida_destroy);
273
274/**
275 * percpu_ida_init - initialize a percpu tag pool
276 * @pool: pool to initialize
277 * @nr_tags: number of tags that will be available for allocation
278 *
279 * Initializes @pool so that it can be used to allocate tags - integers in the
280 * range [0, nr_tags). Typically, they'll be used by driver code to refer to a
281 * preallocated array of tag structures.
282 *
283 * Allocation is percpu, but sharding is limited by nr_tags - for best
284 * performance, the workload should not span more cpus than nr_tags / 128.
285 */
Shaohua Lie26b53d2013-10-15 09:05:01 +0800286int __percpu_ida_init(struct percpu_ida *pool, unsigned long nr_tags,
287 unsigned long max_size, unsigned long batch_size)
Kent Overstreet798ab482013-08-16 22:04:37 +0000288{
289 unsigned i, cpu, order;
290
291 memset(pool, 0, sizeof(*pool));
292
293 init_waitqueue_head(&pool->wait);
294 spin_lock_init(&pool->lock);
295 pool->nr_tags = nr_tags;
Shaohua Lie26b53d2013-10-15 09:05:01 +0800296 pool->percpu_max_size = max_size;
297 pool->percpu_batch_size = batch_size;
Kent Overstreet798ab482013-08-16 22:04:37 +0000298
299 /* Guard against overflow */
300 if (nr_tags > (unsigned) INT_MAX + 1) {
301 pr_err("percpu_ida_init(): nr_tags too large\n");
302 return -EINVAL;
303 }
304
305 order = get_order(nr_tags * sizeof(unsigned));
306 pool->freelist = (void *) __get_free_pages(GFP_KERNEL, order);
307 if (!pool->freelist)
308 return -ENOMEM;
309
310 for (i = 0; i < nr_tags; i++)
311 pool->freelist[i] = i;
312
313 pool->nr_free = nr_tags;
314
315 pool->tag_cpu = __alloc_percpu(sizeof(struct percpu_ida_cpu) +
Shaohua Lie26b53d2013-10-15 09:05:01 +0800316 pool->percpu_max_size * sizeof(unsigned),
Kent Overstreet798ab482013-08-16 22:04:37 +0000317 sizeof(unsigned));
318 if (!pool->tag_cpu)
319 goto err;
320
321 for_each_possible_cpu(cpu)
322 spin_lock_init(&per_cpu_ptr(pool->tag_cpu, cpu)->lock);
323
324 return 0;
325err:
326 percpu_ida_destroy(pool);
327 return -ENOMEM;
328}
Shaohua Lie26b53d2013-10-15 09:05:01 +0800329EXPORT_SYMBOL_GPL(__percpu_ida_init);
Shaohua Li7fc2ba12013-10-15 09:05:02 +0800330
331/**
332 * percpu_ida_for_each_free - iterate free ids of a pool
333 * @pool: pool to iterate
334 * @fn: interate callback function
335 * @data: parameter for @fn
336 *
337 * Note, this doesn't guarantee to iterate all free ids restrictly. Some free
338 * ids might be missed, some might be iterated duplicated, and some might
339 * be iterated and not free soon.
340 */
341int percpu_ida_for_each_free(struct percpu_ida *pool, percpu_ida_cb fn,
342 void *data)
343{
344 unsigned long flags;
345 struct percpu_ida_cpu *remote;
346 unsigned cpu, i, err = 0;
347
348 local_irq_save(flags);
349 for_each_possible_cpu(cpu) {
350 remote = per_cpu_ptr(pool->tag_cpu, cpu);
351 spin_lock(&remote->lock);
352 for (i = 0; i < remote->nr_free; i++) {
353 err = fn(remote->freelist[i], data);
354 if (err)
355 break;
356 }
357 spin_unlock(&remote->lock);
358 if (err)
359 goto out;
360 }
361
362 spin_lock(&pool->lock);
363 for (i = 0; i < pool->nr_free; i++) {
364 err = fn(pool->freelist[i], data);
365 if (err)
366 break;
367 }
368 spin_unlock(&pool->lock);
369out:
370 local_irq_restore(flags);
371 return err;
372}
373EXPORT_SYMBOL_GPL(percpu_ida_for_each_free);
Shaohua Li1dddc012013-10-15 09:05:03 +0800374
375/**
376 * percpu_ida_free_tags - return free tags number of a specific cpu or global pool
377 * @pool: pool related
378 * @cpu: specific cpu or global pool if @cpu == nr_cpu_ids
379 *
380 * Note: this just returns a snapshot of free tags number.
381 */
382unsigned percpu_ida_free_tags(struct percpu_ida *pool, int cpu)
383{
384 struct percpu_ida_cpu *remote;
385 if (cpu == nr_cpu_ids)
386 return pool->nr_free;
387 remote = per_cpu_ptr(pool->tag_cpu, cpu);
388 return remote->nr_free;
389}
390EXPORT_SYMBOL_GPL(percpu_ida_free_tags);