4 * Copyright (C) 2013 Datera, Inc. Kent Overstreet
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.
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.
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>
33 struct percpu_ida_cpu {
35 * Even though this is percpu, we need a lock for tag stealing by remote
40 /* nr_free/freelist form a stack of free IDs */
45 static inline void move_tags(unsigned *dst, unsigned *dst_nr,
46 unsigned *src, unsigned *src_nr,
50 memcpy(dst + *dst_nr, src + *src_nr, sizeof(unsigned) * nr);
55 * Try to steal tags from a remote cpu's percpu freelist.
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.
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
65 static inline void steal_tags(struct percpu_ida *pool,
66 struct percpu_ida_cpu *tags)
68 unsigned cpus_have_tags, cpu = pool->cpu_last_stolen;
69 struct percpu_ida_cpu *remote;
71 for (cpus_have_tags = cpumask_weight(&pool->cpus_have_tags);
72 cpus_have_tags * pool->percpu_max_size > pool->nr_tags / 2;
74 cpu = cpumask_next(cpu, &pool->cpus_have_tags);
76 if (cpu >= nr_cpu_ids) {
77 cpu = cpumask_first(&pool->cpus_have_tags);
78 if (cpu >= nr_cpu_ids)
82 pool->cpu_last_stolen = cpu;
83 remote = per_cpu_ptr(pool->tag_cpu, cpu);
85 cpumask_clear_cpu(cpu, &pool->cpus_have_tags);
90 spin_lock(&remote->lock);
92 if (remote->nr_free) {
93 memcpy(tags->freelist,
95 sizeof(unsigned) * remote->nr_free);
97 tags->nr_free = remote->nr_free;
101 spin_unlock(&remote->lock);
109 * Pop up to IDA_PCPU_BATCH_MOVE IDs off the global freelist, and push them onto
110 * our percpu freelist:
112 static inline void alloc_global_tags(struct percpu_ida *pool,
113 struct percpu_ida_cpu *tags)
115 move_tags(tags->freelist, &tags->nr_free,
116 pool->freelist, &pool->nr_free,
117 min(pool->nr_free, pool->percpu_batch_size));
120 static inline unsigned alloc_local_tag(struct percpu_ida_cpu *tags)
124 spin_lock(&tags->lock);
126 tag = tags->freelist[--tags->nr_free];
127 spin_unlock(&tags->lock);
133 * percpu_ida_alloc - allocate a tag
134 * @pool: pool to allocate from
135 * @state: task state for prepare_to_wait
137 * Returns a tag - an integer in the range [0..nr_tags) (passed to
138 * tag_pool_init()), or otherwise -ENOSPC on allocation failure.
140 * Safe to be called from interrupt context (assuming it isn't passed
141 * TASK_UNINTERRUPTIBLE, of course).
143 * @gfp indicates whether or not to wait until a free id is available (it's not
144 * used for internal memory allocations); thus if passed __GFP_WAIT we may sleep
145 * however long it takes until another thread frees an id (same semantics as a
148 * Will not fail if passed TASK_UNINTERRUPTIBLE.
150 int percpu_ida_alloc(struct percpu_ida *pool, int state)
153 struct percpu_ida_cpu *tags;
157 local_irq_save(flags);
158 tags = this_cpu_ptr(pool->tag_cpu);
161 tag = alloc_local_tag(tags);
162 if (likely(tag >= 0)) {
163 local_irq_restore(flags);
168 spin_lock(&pool->lock);
171 * prepare_to_wait() must come before steal_tags(), in case
172 * percpu_ida_free() on another cpu flips a bit in
175 * global lock held and irqs disabled, don't need percpu lock
177 if (state != TASK_RUNNING)
178 prepare_to_wait(&pool->wait, &wait, state);
181 alloc_global_tags(pool, tags);
183 steal_tags(pool, tags);
186 tag = tags->freelist[--tags->nr_free];
188 cpumask_set_cpu(smp_processor_id(),
189 &pool->cpus_have_tags);
192 spin_unlock(&pool->lock);
193 local_irq_restore(flags);
195 if (tag >= 0 || state == TASK_RUNNING)
200 local_irq_save(flags);
201 tags = this_cpu_ptr(pool->tag_cpu);
203 if (state != TASK_RUNNING)
204 finish_wait(&pool->wait, &wait);
208 EXPORT_SYMBOL_GPL(percpu_ida_alloc);
211 * percpu_ida_free - free a tag
212 * @pool: pool @tag was allocated from
213 * @tag: a tag previously allocated with percpu_ida_alloc()
215 * Safe to be called from interrupt context.
217 void percpu_ida_free(struct percpu_ida *pool, unsigned tag)
219 struct percpu_ida_cpu *tags;
223 BUG_ON(tag >= pool->nr_tags);
225 local_irq_save(flags);
226 tags = this_cpu_ptr(pool->tag_cpu);
228 spin_lock(&tags->lock);
229 tags->freelist[tags->nr_free++] = tag;
231 nr_free = tags->nr_free;
232 spin_unlock(&tags->lock);
235 cpumask_set_cpu(smp_processor_id(),
236 &pool->cpus_have_tags);
237 wake_up(&pool->wait);
240 if (nr_free == pool->percpu_max_size) {
241 spin_lock(&pool->lock);
244 * Global lock held and irqs disabled, don't need percpu
247 if (tags->nr_free == pool->percpu_max_size) {
248 move_tags(pool->freelist, &pool->nr_free,
249 tags->freelist, &tags->nr_free,
250 pool->percpu_batch_size);
252 wake_up(&pool->wait);
254 spin_unlock(&pool->lock);
257 local_irq_restore(flags);
259 EXPORT_SYMBOL_GPL(percpu_ida_free);
262 * percpu_ida_destroy - release a tag pool's resources
263 * @pool: pool to free
265 * Frees the resources allocated by percpu_ida_init().
267 void percpu_ida_destroy(struct percpu_ida *pool)
269 free_percpu(pool->tag_cpu);
270 free_pages((unsigned long) pool->freelist,
271 get_order(pool->nr_tags * sizeof(unsigned)));
273 EXPORT_SYMBOL_GPL(percpu_ida_destroy);
276 * percpu_ida_init - initialize a percpu tag pool
277 * @pool: pool to initialize
278 * @nr_tags: number of tags that will be available for allocation
280 * Initializes @pool so that it can be used to allocate tags - integers in the
281 * range [0, nr_tags). Typically, they'll be used by driver code to refer to a
282 * preallocated array of tag structures.
284 * Allocation is percpu, but sharding is limited by nr_tags - for best
285 * performance, the workload should not span more cpus than nr_tags / 128.
287 int __percpu_ida_init(struct percpu_ida *pool, unsigned long nr_tags,
288 unsigned long max_size, unsigned long batch_size)
290 unsigned i, cpu, order;
292 memset(pool, 0, sizeof(*pool));
294 init_waitqueue_head(&pool->wait);
295 spin_lock_init(&pool->lock);
296 pool->nr_tags = nr_tags;
297 pool->percpu_max_size = max_size;
298 pool->percpu_batch_size = batch_size;
300 /* Guard against overflow */
301 if (nr_tags > (unsigned) INT_MAX + 1) {
302 pr_err("percpu_ida_init(): nr_tags too large\n");
306 order = get_order(nr_tags * sizeof(unsigned));
307 pool->freelist = (void *) __get_free_pages(GFP_KERNEL, order);
311 for (i = 0; i < nr_tags; i++)
312 pool->freelist[i] = i;
314 pool->nr_free = nr_tags;
316 pool->tag_cpu = __alloc_percpu(sizeof(struct percpu_ida_cpu) +
317 pool->percpu_max_size * sizeof(unsigned),
322 for_each_possible_cpu(cpu)
323 spin_lock_init(&per_cpu_ptr(pool->tag_cpu, cpu)->lock);
327 percpu_ida_destroy(pool);
330 EXPORT_SYMBOL_GPL(__percpu_ida_init);
333 * percpu_ida_for_each_free - iterate free ids of a pool
334 * @pool: pool to iterate
335 * @fn: interate callback function
336 * @data: parameter for @fn
338 * Note, this doesn't guarantee to iterate all free ids restrictly. Some free
339 * ids might be missed, some might be iterated duplicated, and some might
340 * be iterated and not free soon.
342 int percpu_ida_for_each_free(struct percpu_ida *pool, percpu_ida_cb fn,
346 struct percpu_ida_cpu *remote;
347 unsigned cpu, i, err = 0;
349 local_irq_save(flags);
350 for_each_possible_cpu(cpu) {
351 remote = per_cpu_ptr(pool->tag_cpu, cpu);
352 spin_lock(&remote->lock);
353 for (i = 0; i < remote->nr_free; i++) {
354 err = fn(remote->freelist[i], data);
358 spin_unlock(&remote->lock);
363 spin_lock(&pool->lock);
364 for (i = 0; i < pool->nr_free; i++) {
365 err = fn(pool->freelist[i], data);
369 spin_unlock(&pool->lock);
371 local_irq_restore(flags);
374 EXPORT_SYMBOL_GPL(percpu_ida_for_each_free);
377 * percpu_ida_free_tags - return free tags number of a specific cpu or global pool
378 * @pool: pool related
379 * @cpu: specific cpu or global pool if @cpu == nr_cpu_ids
381 * Note: this just returns a snapshot of free tags number.
383 unsigned percpu_ida_free_tags(struct percpu_ida *pool, int cpu)
385 struct percpu_ida_cpu *remote;
386 if (cpu == nr_cpu_ids)
387 return pool->nr_free;
388 remote = per_cpu_ptr(pool->tag_cpu, cpu);
389 return remote->nr_free;
391 EXPORT_SYMBOL_GPL(percpu_ida_free_tags);