1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <linux/mm.h>
18#include <linux/bitmap.h>
19#include <linux/bitops.h>
20#include <linux/bug.h>
21#include <linux/err.h>
22#include <linux/export.h>
23#include <linux/init.h>
24#include <linux/kernel.h>
25#include <linux/percpu.h>
26#include <linux/sched/signal.h>
27#include <linux/string.h>
28#include <linux/spinlock.h>
29#include <linux/percpu_ida.h>
30
31struct percpu_ida_cpu {
32
33
34
35
36 spinlock_t lock;
37
38
39 unsigned nr_free;
40 unsigned freelist[];
41};
42
43static inline void move_tags(unsigned *dst, unsigned *dst_nr,
44 unsigned *src, unsigned *src_nr,
45 unsigned nr)
46{
47 *src_nr -= nr;
48 memcpy(dst + *dst_nr, src + *src_nr, sizeof(unsigned) * nr);
49 *dst_nr += nr;
50}
51
52
53
54
55
56
57
58
59
60
61static inline void steal_tags(struct percpu_ida *pool,
62 struct percpu_ida_cpu *tags)
63{
64 unsigned cpus_have_tags, cpu = pool->cpu_last_stolen;
65 struct percpu_ida_cpu *remote;
66
67 for (cpus_have_tags = cpumask_weight(&pool->cpus_have_tags);
68 cpus_have_tags; cpus_have_tags--) {
69 cpu = cpumask_next(cpu, &pool->cpus_have_tags);
70
71 if (cpu >= nr_cpu_ids) {
72 cpu = cpumask_first(&pool->cpus_have_tags);
73 if (cpu >= nr_cpu_ids)
74 BUG();
75 }
76
77 pool->cpu_last_stolen = cpu;
78 remote = per_cpu_ptr(pool->tag_cpu, cpu);
79
80 cpumask_clear_cpu(cpu, &pool->cpus_have_tags);
81
82 if (remote == tags)
83 continue;
84
85 spin_lock(&remote->lock);
86
87 if (remote->nr_free) {
88 memcpy(tags->freelist,
89 remote->freelist,
90 sizeof(unsigned) * remote->nr_free);
91
92 tags->nr_free = remote->nr_free;
93 remote->nr_free = 0;
94 }
95
96 spin_unlock(&remote->lock);
97
98 if (tags->nr_free)
99 break;
100 }
101}
102
103
104
105
106
107static inline void alloc_global_tags(struct percpu_ida *pool,
108 struct percpu_ida_cpu *tags)
109{
110 move_tags(tags->freelist, &tags->nr_free,
111 pool->freelist, &pool->nr_free,
112 min(pool->nr_free, pool->percpu_batch_size));
113}
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133int percpu_ida_alloc(struct percpu_ida *pool, int state)
134{
135 DEFINE_WAIT(wait);
136 struct percpu_ida_cpu *tags;
137 unsigned long flags;
138 int tag = -ENOSPC;
139
140 tags = raw_cpu_ptr(pool->tag_cpu);
141 spin_lock_irqsave(&tags->lock, flags);
142
143
144 if (likely(tags->nr_free)) {
145 tag = tags->freelist[--tags->nr_free];
146 spin_unlock_irqrestore(&tags->lock, flags);
147 return tag;
148 }
149 spin_unlock_irqrestore(&tags->lock, flags);
150
151 while (1) {
152 spin_lock_irqsave(&pool->lock, flags);
153 tags = this_cpu_ptr(pool->tag_cpu);
154
155
156
157
158
159
160
161
162 if (state != TASK_RUNNING)
163 prepare_to_wait(&pool->wait, &wait, state);
164
165 if (!tags->nr_free)
166 alloc_global_tags(pool, tags);
167 if (!tags->nr_free)
168 steal_tags(pool, tags);
169
170 if (tags->nr_free) {
171 tag = tags->freelist[--tags->nr_free];
172 if (tags->nr_free)
173 cpumask_set_cpu(smp_processor_id(),
174 &pool->cpus_have_tags);
175 }
176
177 spin_unlock_irqrestore(&pool->lock, flags);
178
179 if (tag >= 0 || state == TASK_RUNNING)
180 break;
181
182 if (signal_pending_state(state, current)) {
183 tag = -ERESTARTSYS;
184 break;
185 }
186
187 schedule();
188 }
189 if (state != TASK_RUNNING)
190 finish_wait(&pool->wait, &wait);
191
192 return tag;
193}
194EXPORT_SYMBOL_GPL(percpu_ida_alloc);
195
196
197
198
199
200
201
202
203void percpu_ida_free(struct percpu_ida *pool, unsigned tag)
204{
205 struct percpu_ida_cpu *tags;
206 unsigned long flags;
207 unsigned nr_free;
208
209 BUG_ON(tag >= pool->nr_tags);
210
211 tags = raw_cpu_ptr(pool->tag_cpu);
212
213 spin_lock_irqsave(&tags->lock, flags);
214 tags->freelist[tags->nr_free++] = tag;
215
216 nr_free = tags->nr_free;
217
218 if (nr_free == 1) {
219 cpumask_set_cpu(smp_processor_id(),
220 &pool->cpus_have_tags);
221 wake_up(&pool->wait);
222 }
223 spin_unlock_irqrestore(&tags->lock, flags);
224
225 if (nr_free == pool->percpu_max_size) {
226 spin_lock_irqsave(&pool->lock, flags);
227 spin_lock(&tags->lock);
228
229 if (tags->nr_free == pool->percpu_max_size) {
230 move_tags(pool->freelist, &pool->nr_free,
231 tags->freelist, &tags->nr_free,
232 pool->percpu_batch_size);
233
234 wake_up(&pool->wait);
235 }
236 spin_unlock(&tags->lock);
237 spin_unlock_irqrestore(&pool->lock, flags);
238 }
239}
240EXPORT_SYMBOL_GPL(percpu_ida_free);
241
242
243
244
245
246
247
248void percpu_ida_destroy(struct percpu_ida *pool)
249{
250 free_percpu(pool->tag_cpu);
251 free_pages((unsigned long) pool->freelist,
252 get_order(pool->nr_tags * sizeof(unsigned)));
253}
254EXPORT_SYMBOL_GPL(percpu_ida_destroy);
255
256
257
258
259
260
261
262
263
264
265
266
267
268int __percpu_ida_init(struct percpu_ida *pool, unsigned long nr_tags,
269 unsigned long max_size, unsigned long batch_size)
270{
271 unsigned i, cpu, order;
272
273 memset(pool, 0, sizeof(*pool));
274
275 init_waitqueue_head(&pool->wait);
276 spin_lock_init(&pool->lock);
277 pool->nr_tags = nr_tags;
278 pool->percpu_max_size = max_size;
279 pool->percpu_batch_size = batch_size;
280
281
282 if (nr_tags > (unsigned) INT_MAX + 1) {
283 pr_err("percpu_ida_init(): nr_tags too large\n");
284 return -EINVAL;
285 }
286
287 order = get_order(nr_tags * sizeof(unsigned));
288 pool->freelist = (void *) __get_free_pages(GFP_KERNEL, order);
289 if (!pool->freelist)
290 return -ENOMEM;
291
292 for (i = 0; i < nr_tags; i++)
293 pool->freelist[i] = i;
294
295 pool->nr_free = nr_tags;
296
297 pool->tag_cpu = __alloc_percpu(sizeof(struct percpu_ida_cpu) +
298 pool->percpu_max_size * sizeof(unsigned),
299 sizeof(unsigned));
300 if (!pool->tag_cpu)
301 goto err;
302
303 for_each_possible_cpu(cpu)
304 spin_lock_init(&per_cpu_ptr(pool->tag_cpu, cpu)->lock);
305
306 return 0;
307err:
308 percpu_ida_destroy(pool);
309 return -ENOMEM;
310}
311EXPORT_SYMBOL_GPL(__percpu_ida_init);
312
313
314
315
316
317
318
319
320
321
322
323int percpu_ida_for_each_free(struct percpu_ida *pool, percpu_ida_cb fn,
324 void *data)
325{
326 unsigned long flags;
327 struct percpu_ida_cpu *remote;
328 unsigned cpu, i, err = 0;
329
330 for_each_possible_cpu(cpu) {
331 remote = per_cpu_ptr(pool->tag_cpu, cpu);
332 spin_lock_irqsave(&remote->lock, flags);
333 for (i = 0; i < remote->nr_free; i++) {
334 err = fn(remote->freelist[i], data);
335 if (err)
336 break;
337 }
338 spin_unlock_irqrestore(&remote->lock, flags);
339 if (err)
340 goto out;
341 }
342
343 spin_lock_irqsave(&pool->lock, flags);
344 for (i = 0; i < pool->nr_free; i++) {
345 err = fn(pool->freelist[i], data);
346 if (err)
347 break;
348 }
349 spin_unlock_irqrestore(&pool->lock, flags);
350out:
351 return err;
352}
353EXPORT_SYMBOL_GPL(percpu_ida_for_each_free);
354
355
356
357
358
359
360
361
362unsigned percpu_ida_free_tags(struct percpu_ida *pool, int cpu)
363{
364 struct percpu_ida_cpu *remote;
365 if (cpu == nr_cpu_ids)
366 return pool->nr_free;
367 remote = per_cpu_ptr(pool->tag_cpu, cpu);
368 return remote->nr_free;
369}
370EXPORT_SYMBOL_GPL(percpu_ida_free_tags);
371