ARM: EXYNOS: Remove hardware.h file
[cascardo/linux.git] / lib / percpu_ida.c
1 /*
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
33 struct 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
45 static 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  */
65 static 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);
72              cpus_have_tags * pool->percpu_max_size > pool->nr_tags / 2;
73              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  */
112 static 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,
117                   min(pool->nr_free, pool->percpu_batch_size));
118 }
119
120 static inline unsigned alloc_local_tag(struct percpu_ida_cpu *tags)
121 {
122         int tag = -ENOSPC;
123
124         spin_lock(&tags->lock);
125         if (tags->nr_free)
126                 tag = tags->freelist[--tags->nr_free];
127         spin_unlock(&tags->lock);
128
129         return tag;
130 }
131
132 /**
133  * percpu_ida_alloc - allocate a tag
134  * @pool: pool to allocate from
135  * @state: task state for prepare_to_wait
136  *
137  * Returns a tag - an integer in the range [0..nr_tags) (passed to
138  * tag_pool_init()), or otherwise -ENOSPC on allocation failure.
139  *
140  * Safe to be called from interrupt context (assuming it isn't passed
141  * TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, of course).
142  *
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
146  * mempool).
147  *
148  * Will not fail if passed TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE.
149  */
150 int percpu_ida_alloc(struct percpu_ida *pool, int state)
151 {
152         DEFINE_WAIT(wait);
153         struct percpu_ida_cpu *tags;
154         unsigned long flags;
155         int tag;
156
157         local_irq_save(flags);
158         tags = this_cpu_ptr(pool->tag_cpu);
159
160         /* Fastpath */
161         tag = alloc_local_tag(tags);
162         if (likely(tag >= 0)) {
163                 local_irq_restore(flags);
164                 return tag;
165         }
166
167         while (1) {
168                 spin_lock(&pool->lock);
169
170                 /*
171                  * prepare_to_wait() must come before steal_tags(), in case
172                  * percpu_ida_free() on another cpu flips a bit in
173                  * cpus_have_tags
174                  *
175                  * global lock held and irqs disabled, don't need percpu lock
176                  */
177                 if (state != TASK_RUNNING)
178                         prepare_to_wait(&pool->wait, &wait, state);
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 || state == TASK_RUNNING)
196                         break;
197
198                 if (signal_pending_state(state, current)) {
199                         tag = -ERESTARTSYS;
200                         break;
201                 }
202
203                 schedule();
204
205                 local_irq_save(flags);
206                 tags = this_cpu_ptr(pool->tag_cpu);
207         }
208         if (state != TASK_RUNNING)
209                 finish_wait(&pool->wait, &wait);
210
211         return tag;
212 }
213 EXPORT_SYMBOL_GPL(percpu_ida_alloc);
214
215 /**
216  * percpu_ida_free - free a tag
217  * @pool: pool @tag was allocated from
218  * @tag: a tag previously allocated with percpu_ida_alloc()
219  *
220  * Safe to be called from interrupt context.
221  */
222 void percpu_ida_free(struct percpu_ida *pool, unsigned tag)
223 {
224         struct percpu_ida_cpu *tags;
225         unsigned long flags;
226         unsigned nr_free;
227
228         BUG_ON(tag >= pool->nr_tags);
229
230         local_irq_save(flags);
231         tags = this_cpu_ptr(pool->tag_cpu);
232
233         spin_lock(&tags->lock);
234         tags->freelist[tags->nr_free++] = tag;
235
236         nr_free = tags->nr_free;
237         spin_unlock(&tags->lock);
238
239         if (nr_free == 1) {
240                 cpumask_set_cpu(smp_processor_id(),
241                                 &pool->cpus_have_tags);
242                 wake_up(&pool->wait);
243         }
244
245         if (nr_free == pool->percpu_max_size) {
246                 spin_lock(&pool->lock);
247
248                 /*
249                  * Global lock held and irqs disabled, don't need percpu
250                  * lock
251                  */
252                 if (tags->nr_free == pool->percpu_max_size) {
253                         move_tags(pool->freelist, &pool->nr_free,
254                                   tags->freelist, &tags->nr_free,
255                                   pool->percpu_batch_size);
256
257                         wake_up(&pool->wait);
258                 }
259                 spin_unlock(&pool->lock);
260         }
261
262         local_irq_restore(flags);
263 }
264 EXPORT_SYMBOL_GPL(percpu_ida_free);
265
266 /**
267  * percpu_ida_destroy - release a tag pool's resources
268  * @pool: pool to free
269  *
270  * Frees the resources allocated by percpu_ida_init().
271  */
272 void percpu_ida_destroy(struct percpu_ida *pool)
273 {
274         free_percpu(pool->tag_cpu);
275         free_pages((unsigned long) pool->freelist,
276                    get_order(pool->nr_tags * sizeof(unsigned)));
277 }
278 EXPORT_SYMBOL_GPL(percpu_ida_destroy);
279
280 /**
281  * percpu_ida_init - initialize a percpu tag pool
282  * @pool: pool to initialize
283  * @nr_tags: number of tags that will be available for allocation
284  *
285  * Initializes @pool so that it can be used to allocate tags - integers in the
286  * range [0, nr_tags). Typically, they'll be used by driver code to refer to a
287  * preallocated array of tag structures.
288  *
289  * Allocation is percpu, but sharding is limited by nr_tags - for best
290  * performance, the workload should not span more cpus than nr_tags / 128.
291  */
292 int __percpu_ida_init(struct percpu_ida *pool, unsigned long nr_tags,
293         unsigned long max_size, unsigned long batch_size)
294 {
295         unsigned i, cpu, order;
296
297         memset(pool, 0, sizeof(*pool));
298
299         init_waitqueue_head(&pool->wait);
300         spin_lock_init(&pool->lock);
301         pool->nr_tags = nr_tags;
302         pool->percpu_max_size = max_size;
303         pool->percpu_batch_size = batch_size;
304
305         /* Guard against overflow */
306         if (nr_tags > (unsigned) INT_MAX + 1) {
307                 pr_err("percpu_ida_init(): nr_tags too large\n");
308                 return -EINVAL;
309         }
310
311         order = get_order(nr_tags * sizeof(unsigned));
312         pool->freelist = (void *) __get_free_pages(GFP_KERNEL, order);
313         if (!pool->freelist)
314                 return -ENOMEM;
315
316         for (i = 0; i < nr_tags; i++)
317                 pool->freelist[i] = i;
318
319         pool->nr_free = nr_tags;
320
321         pool->tag_cpu = __alloc_percpu(sizeof(struct percpu_ida_cpu) +
322                                        pool->percpu_max_size * sizeof(unsigned),
323                                        sizeof(unsigned));
324         if (!pool->tag_cpu)
325                 goto err;
326
327         for_each_possible_cpu(cpu)
328                 spin_lock_init(&per_cpu_ptr(pool->tag_cpu, cpu)->lock);
329
330         return 0;
331 err:
332         percpu_ida_destroy(pool);
333         return -ENOMEM;
334 }
335 EXPORT_SYMBOL_GPL(__percpu_ida_init);
336
337 /**
338  * percpu_ida_for_each_free - iterate free ids of a pool
339  * @pool: pool to iterate
340  * @fn: interate callback function
341  * @data: parameter for @fn
342  *
343  * Note, this doesn't guarantee to iterate all free ids restrictly. Some free
344  * ids might be missed, some might be iterated duplicated, and some might
345  * be iterated and not free soon.
346  */
347 int percpu_ida_for_each_free(struct percpu_ida *pool, percpu_ida_cb fn,
348         void *data)
349 {
350         unsigned long flags;
351         struct percpu_ida_cpu *remote;
352         unsigned cpu, i, err = 0;
353
354         local_irq_save(flags);
355         for_each_possible_cpu(cpu) {
356                 remote = per_cpu_ptr(pool->tag_cpu, cpu);
357                 spin_lock(&remote->lock);
358                 for (i = 0; i < remote->nr_free; i++) {
359                         err = fn(remote->freelist[i], data);
360                         if (err)
361                                 break;
362                 }
363                 spin_unlock(&remote->lock);
364                 if (err)
365                         goto out;
366         }
367
368         spin_lock(&pool->lock);
369         for (i = 0; i < pool->nr_free; i++) {
370                 err = fn(pool->freelist[i], data);
371                 if (err)
372                         break;
373         }
374         spin_unlock(&pool->lock);
375 out:
376         local_irq_restore(flags);
377         return err;
378 }
379 EXPORT_SYMBOL_GPL(percpu_ida_for_each_free);
380
381 /**
382  * percpu_ida_free_tags - return free tags number of a specific cpu or global pool
383  * @pool: pool related
384  * @cpu: specific cpu or global pool if @cpu == nr_cpu_ids
385  *
386  * Note: this just returns a snapshot of free tags number.
387  */
388 unsigned percpu_ida_free_tags(struct percpu_ida *pool, int cpu)
389 {
390         struct percpu_ida_cpu *remote;
391         if (cpu == nr_cpu_ids)
392                 return pool->nr_free;
393         remote = per_cpu_ptr(pool->tag_cpu, cpu);
394         return remote->nr_free;
395 }
396 EXPORT_SYMBOL_GPL(percpu_ida_free_tags);