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/init.h>
23 #include <linux/kernel.h>
24 #include <linux/percpu.h>
25 #include <linux/sched.h>
26 #include <linux/string.h>
27 #include <linux/spinlock.h>
28 #include <linux/percpu_ida.h>
30 struct percpu_ida_cpu {
32 * Even though this is percpu, we need a lock for tag stealing by remote
37 /* nr_free/freelist form a stack of free IDs */
42 static inline void move_tags(unsigned *dst, unsigned *dst_nr,
43 unsigned *src, unsigned *src_nr,
47 memcpy(dst + *dst_nr, src + *src_nr, sizeof(unsigned) * nr);
52 * Try to steal tags from a remote cpu's percpu freelist.
54 * We first check how many percpu freelists have tags
56 * Then we iterate through the cpus until we find some tags - we don't attempt
57 * to find the "best" cpu to steal from, to keep cacheline bouncing to a
60 static inline void steal_tags(struct percpu_ida *pool,
61 struct percpu_ida_cpu *tags)
63 unsigned cpus_have_tags, cpu = pool->cpu_last_stolen;
64 struct percpu_ida_cpu *remote;
66 for (cpus_have_tags = cpumask_weight(&pool->cpus_have_tags);
67 cpus_have_tags; cpus_have_tags--) {
68 cpu = cpumask_next(cpu, &pool->cpus_have_tags);
70 if (cpu >= nr_cpu_ids) {
71 cpu = cpumask_first(&pool->cpus_have_tags);
72 if (cpu >= nr_cpu_ids)
76 pool->cpu_last_stolen = cpu;
77 remote = per_cpu_ptr(pool->tag_cpu, cpu);
79 cpumask_clear_cpu(cpu, &pool->cpus_have_tags);
84 spin_lock(&remote->lock);
86 if (remote->nr_free) {
87 memcpy(tags->freelist,
89 sizeof(unsigned) * remote->nr_free);
91 tags->nr_free = remote->nr_free;
95 spin_unlock(&remote->lock);
103 * Pop up to IDA_PCPU_BATCH_MOVE IDs off the global freelist, and push them onto
104 * our percpu freelist:
106 static inline void alloc_global_tags(struct percpu_ida *pool,
107 struct percpu_ida_cpu *tags)
109 move_tags(tags->freelist, &tags->nr_free,
110 pool->freelist, &pool->nr_free,
111 min(pool->nr_free, pool->percpu_batch_size));
114 static inline unsigned alloc_local_tag(struct percpu_ida_cpu *tags)
118 spin_lock(&tags->lock);
120 tag = tags->freelist[--tags->nr_free];
121 spin_unlock(&tags->lock);
127 * percpu_ida_alloc - allocate a tag
128 * @pool: pool to allocate from
129 * @state: task state for prepare_to_wait
131 * Returns a tag - an integer in the range [0..nr_tags) (passed to
132 * tag_pool_init()), or otherwise -ENOSPC on allocation failure.
134 * Safe to be called from interrupt context (assuming it isn't passed
135 * TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, of course).
137 * @gfp indicates whether or not to wait until a free id is available (it's not
138 * used for internal memory allocations); thus if passed __GFP_WAIT we may sleep
139 * however long it takes until another thread frees an id (same semantics as a
142 * Will not fail if passed TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE.
144 int percpu_ida_alloc(struct percpu_ida *pool, int state)
147 struct percpu_ida_cpu *tags;
151 local_irq_save(flags);
152 tags = this_cpu_ptr(pool->tag_cpu);
155 tag = alloc_local_tag(tags);
156 if (likely(tag >= 0)) {
157 local_irq_restore(flags);
162 spin_lock(&pool->lock);
165 * prepare_to_wait() must come before steal_tags(), in case
166 * percpu_ida_free() on another cpu flips a bit in
169 * global lock held and irqs disabled, don't need percpu lock
171 if (state != TASK_RUNNING)
172 prepare_to_wait(&pool->wait, &wait, state);
175 alloc_global_tags(pool, tags);
177 steal_tags(pool, tags);
180 tag = tags->freelist[--tags->nr_free];
182 cpumask_set_cpu(smp_processor_id(),
183 &pool->cpus_have_tags);
186 spin_unlock(&pool->lock);
187 local_irq_restore(flags);
189 if (tag >= 0 || state == TASK_RUNNING)
192 if (signal_pending_state(state, current)) {
199 local_irq_save(flags);
200 tags = this_cpu_ptr(pool->tag_cpu);
202 if (state != TASK_RUNNING)
203 finish_wait(&pool->wait, &wait);
207 EXPORT_SYMBOL_GPL(percpu_ida_alloc);
210 * percpu_ida_free - free a tag
211 * @pool: pool @tag was allocated from
212 * @tag: a tag previously allocated with percpu_ida_alloc()
214 * Safe to be called from interrupt context.
216 void percpu_ida_free(struct percpu_ida *pool, unsigned tag)
218 struct percpu_ida_cpu *tags;
222 BUG_ON(tag >= pool->nr_tags);
224 local_irq_save(flags);
225 tags = this_cpu_ptr(pool->tag_cpu);
227 spin_lock(&tags->lock);
228 tags->freelist[tags->nr_free++] = tag;
230 nr_free = tags->nr_free;
231 spin_unlock(&tags->lock);
234 cpumask_set_cpu(smp_processor_id(),
235 &pool->cpus_have_tags);
236 wake_up(&pool->wait);
239 if (nr_free == pool->percpu_max_size) {
240 spin_lock(&pool->lock);
243 * Global lock held and irqs disabled, don't need percpu
246 if (tags->nr_free == pool->percpu_max_size) {
247 move_tags(pool->freelist, &pool->nr_free,
248 tags->freelist, &tags->nr_free,
249 pool->percpu_batch_size);
251 wake_up(&pool->wait);
253 spin_unlock(&pool->lock);
256 local_irq_restore(flags);
258 EXPORT_SYMBOL_GPL(percpu_ida_free);
261 * percpu_ida_destroy - release a tag pool's resources
262 * @pool: pool to free
264 * Frees the resources allocated by percpu_ida_init().
266 void percpu_ida_destroy(struct percpu_ida *pool)
268 free_percpu(pool->tag_cpu);
269 free_pages((unsigned long) pool->freelist,
270 get_order(pool->nr_tags * sizeof(unsigned)));
272 EXPORT_SYMBOL_GPL(percpu_ida_destroy);
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
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.
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.
286 int __percpu_ida_init(struct percpu_ida *pool, unsigned long nr_tags,
287 unsigned long max_size, unsigned long batch_size)
289 unsigned i, cpu, order;
291 memset(pool, 0, sizeof(*pool));
293 init_waitqueue_head(&pool->wait);
294 spin_lock_init(&pool->lock);
295 pool->nr_tags = nr_tags;
296 pool->percpu_max_size = max_size;
297 pool->percpu_batch_size = batch_size;
299 /* Guard against overflow */
300 if (nr_tags > (unsigned) INT_MAX + 1) {
301 pr_err("percpu_ida_init(): nr_tags too large\n");
305 order = get_order(nr_tags * sizeof(unsigned));
306 pool->freelist = (void *) __get_free_pages(GFP_KERNEL, order);
310 for (i = 0; i < nr_tags; i++)
311 pool->freelist[i] = i;
313 pool->nr_free = nr_tags;
315 pool->tag_cpu = __alloc_percpu(sizeof(struct percpu_ida_cpu) +
316 pool->percpu_max_size * sizeof(unsigned),
321 for_each_possible_cpu(cpu)
322 spin_lock_init(&per_cpu_ptr(pool->tag_cpu, cpu)->lock);
326 percpu_ida_destroy(pool);
329 EXPORT_SYMBOL_GPL(__percpu_ida_init);
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
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.
341 int percpu_ida_for_each_free(struct percpu_ida *pool, percpu_ida_cb fn,
345 struct percpu_ida_cpu *remote;
346 unsigned cpu, i, err = 0;
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);
357 spin_unlock(&remote->lock);
362 spin_lock(&pool->lock);
363 for (i = 0; i < pool->nr_free; i++) {
364 err = fn(pool->freelist[i], data);
368 spin_unlock(&pool->lock);
370 local_irq_restore(flags);
373 EXPORT_SYMBOL_GPL(percpu_ida_for_each_free);
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
380 * Note: this just returns a snapshot of free tags number.
382 unsigned percpu_ida_free_tags(struct percpu_ida *pool, int cpu)
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;
390 EXPORT_SYMBOL_GPL(percpu_ida_free_tags);