1
2
3
4
5
6
7
8
9
10
11
12
13
14
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/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>
29
30struct percpu_ida_cpu {
31
32
33
34
35 spinlock_t lock;
36
37
38 unsigned nr_free;
39 unsigned freelist[];
40};
41
42static inline void move_tags(unsigned *dst, unsigned *dst_nr,
43 unsigned *src, unsigned *src_nr,
44 unsigned nr)
45{
46 *src_nr -= nr;
47 memcpy(dst + *dst_nr, src + *src_nr, sizeof(unsigned) * nr);
48 *dst_nr += nr;
49}
50
51
52
53
54
55
56
57
58
59
60static inline void steal_tags(struct percpu_ida *pool,
61 struct percpu_ida_cpu *tags)
62{
63 unsigned cpus_have_tags, cpu = pool->cpu_last_stolen;
64 struct percpu_ida_cpu *remote;
65
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);
69
70 if (cpu >= nr_cpu_ids) {
71 cpu = cpumask_first(&pool->cpus_have_tags);
72 if (cpu >= nr_cpu_ids)
73 BUG();
74 }
75
76 pool->cpu_last_stolen = cpu;
77 remote = per_cpu_ptr(pool->tag_cpu, cpu);
78
79 cpumask_clear_cpu(cpu, &pool->cpus_have_tags);
80
81 if (remote == tags)
82 continue;
83
84 spin_lock(&remote->lock);
85
86 if (remote->nr_free) {
87 memcpy(tags->freelist,
88 remote->freelist,
89 sizeof(unsigned) * remote->nr_free);
90
91 tags->nr_free = remote->nr_free;
92 remote->nr_free = 0;
93 }
94
95 spin_unlock(&remote->lock);
96
97 if (tags->nr_free)
98 break;
99 }
100}
101
102
103
104
105
106static inline void alloc_global_tags(struct percpu_ida *pool,
107 struct percpu_ida_cpu *tags)
108{
109 move_tags(tags->freelist, &tags->nr_free,
110 pool->freelist, &pool->nr_free,
111 min(pool->nr_free, pool->percpu_batch_size));
112}
113
114static inline unsigned alloc_local_tag(struct percpu_ida_cpu *tags)
115{
116 int tag = -ENOSPC;
117
118 spin_lock(&tags->lock);
119 if (tags->nr_free)
120 tag = tags->freelist[--tags->nr_free];
121 spin_unlock(&tags->lock);
122
123 return tag;
124}
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144int percpu_ida_alloc(struct percpu_ida *pool, int state)
145{
146 DEFINE_WAIT(wait);
147 struct percpu_ida_cpu *tags;
148 unsigned long flags;
149 int tag;
150
151 local_irq_save(flags);
152 tags = this_cpu_ptr(pool->tag_cpu);
153
154
155 tag = alloc_local_tag(tags);
156 if (likely(tag >= 0)) {
157 local_irq_restore(flags);
158 return tag;
159 }
160
161 while (1) {
162 spin_lock(&pool->lock);
163
164
165
166
167
168
169
170
171 if (state != TASK_RUNNING)
172 prepare_to_wait(&pool->wait, &wait, state);
173
174 if (!tags->nr_free)
175 alloc_global_tags(pool, tags);
176 if (!tags->nr_free)
177 steal_tags(pool, tags);
178
179 if (tags->nr_free) {
180 tag = tags->freelist[--tags->nr_free];
181 if (tags->nr_free)
182 cpumask_set_cpu(smp_processor_id(),
183 &pool->cpus_have_tags);
184 }
185
186 spin_unlock(&pool->lock);
187 local_irq_restore(flags);
188
189 if (tag >= 0 || state == TASK_RUNNING)
190 break;
191
192 if (signal_pending_state(state, current)) {
193 tag = -ERESTARTSYS;
194 break;
195 }
196
197 schedule();
198
199 local_irq_save(flags);
200 tags = this_cpu_ptr(pool->tag_cpu);
201 }
202 if (state != TASK_RUNNING)
203 finish_wait(&pool->wait, &wait);
204
205 return tag;
206}
207EXPORT_SYMBOL_GPL(percpu_ida_alloc);
208
209
210
211
212
213
214
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
239 if (nr_free == pool->percpu_max_size) {
240 spin_lock(&pool->lock);
241
242
243
244
245
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);
250
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
262
263
264
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
276
277
278
279
280
281
282
283
284
285
286int __percpu_ida_init(struct percpu_ida *pool, unsigned long nr_tags,
287 unsigned long max_size, unsigned long batch_size)
288{
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;
296 pool->percpu_max_size = max_size;
297 pool->percpu_batch_size = batch_size;
298
299
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) +
316 pool->percpu_max_size * sizeof(unsigned),
317 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}
329EXPORT_SYMBOL_GPL(__percpu_ida_init);
330
331
332
333
334
335
336
337
338
339
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);
374
375
376
377
378
379
380
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);
391