1 /* Copyright (c) 2016 Facebook
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of version 2 of the GNU General Public
5 * License as published by the Free Software Foundation.
7 #include "percpu_freelist.h"
9 int pcpu_freelist_init(struct pcpu_freelist
*s
)
13 s
->freelist
= alloc_percpu(struct pcpu_freelist_head
);
17 for_each_possible_cpu(cpu
) {
18 struct pcpu_freelist_head
*head
= per_cpu_ptr(s
->freelist
, cpu
);
20 raw_spin_lock_init(&head
->lock
);
26 void pcpu_freelist_destroy(struct pcpu_freelist
*s
)
28 free_percpu(s
->freelist
);
31 static inline void __pcpu_freelist_push(struct pcpu_freelist_head
*head
,
32 struct pcpu_freelist_node
*node
)
34 raw_spin_lock(&head
->lock
);
35 node
->next
= head
->first
;
37 raw_spin_unlock(&head
->lock
);
40 void pcpu_freelist_push(struct pcpu_freelist
*s
,
41 struct pcpu_freelist_node
*node
)
43 struct pcpu_freelist_head
*head
= this_cpu_ptr(s
->freelist
);
45 __pcpu_freelist_push(head
, node
);
48 void pcpu_freelist_populate(struct pcpu_freelist
*s
, void *buf
, u32 elem_size
,
51 struct pcpu_freelist_head
*head
;
53 int i
, cpu
, pcpu_entries
;
55 pcpu_entries
= nr_elems
/ num_possible_cpus() + 1;
58 /* disable irq to workaround lockdep false positive
59 * in bpf usage pcpu_freelist_populate() will never race
60 * with pcpu_freelist_push()
62 local_irq_save(flags
);
63 for_each_possible_cpu(cpu
) {
65 head
= per_cpu_ptr(s
->freelist
, cpu
);
66 __pcpu_freelist_push(head
, buf
);
74 local_irq_restore(flags
);
77 struct pcpu_freelist_node
*pcpu_freelist_pop(struct pcpu_freelist
*s
)
79 struct pcpu_freelist_head
*head
;
80 struct pcpu_freelist_node
*node
;
84 local_irq_save(flags
);
85 orig_cpu
= cpu
= raw_smp_processor_id();
87 head
= per_cpu_ptr(s
->freelist
, cpu
);
88 raw_spin_lock(&head
->lock
);
91 head
->first
= node
->next
;
92 raw_spin_unlock_irqrestore(&head
->lock
, flags
);
95 raw_spin_unlock(&head
->lock
);
96 cpu
= cpumask_next(cpu
, cpu_possible_mask
);
97 if (cpu
>= nr_cpu_ids
)
99 if (cpu
== orig_cpu
) {
100 local_irq_restore(flags
);