1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2016 Facebook
4 #include "percpu_freelist.h"
6 int pcpu_freelist_init(struct pcpu_freelist
*s
)
10 s
->freelist
= alloc_percpu(struct pcpu_freelist_head
);
14 for_each_possible_cpu(cpu
) {
15 struct pcpu_freelist_head
*head
= per_cpu_ptr(s
->freelist
, cpu
);
17 raw_spin_lock_init(&head
->lock
);
23 void pcpu_freelist_destroy(struct pcpu_freelist
*s
)
25 free_percpu(s
->freelist
);
28 static inline void ___pcpu_freelist_push(struct pcpu_freelist_head
*head
,
29 struct pcpu_freelist_node
*node
)
31 raw_spin_lock(&head
->lock
);
32 node
->next
= head
->first
;
34 raw_spin_unlock(&head
->lock
);
37 void __pcpu_freelist_push(struct pcpu_freelist
*s
,
38 struct pcpu_freelist_node
*node
)
40 struct pcpu_freelist_head
*head
= this_cpu_ptr(s
->freelist
);
42 ___pcpu_freelist_push(head
, node
);
45 void pcpu_freelist_push(struct pcpu_freelist
*s
,
46 struct pcpu_freelist_node
*node
)
50 local_irq_save(flags
);
51 __pcpu_freelist_push(s
, node
);
52 local_irq_restore(flags
);
55 void pcpu_freelist_populate(struct pcpu_freelist
*s
, void *buf
, u32 elem_size
,
58 struct pcpu_freelist_head
*head
;
60 int i
, cpu
, pcpu_entries
;
62 pcpu_entries
= nr_elems
/ num_possible_cpus() + 1;
65 /* disable irq to workaround lockdep false positive
66 * in bpf usage pcpu_freelist_populate() will never race
67 * with pcpu_freelist_push()
69 local_irq_save(flags
);
70 for_each_possible_cpu(cpu
) {
72 head
= per_cpu_ptr(s
->freelist
, cpu
);
73 ___pcpu_freelist_push(head
, buf
);
81 local_irq_restore(flags
);
84 struct pcpu_freelist_node
*__pcpu_freelist_pop(struct pcpu_freelist
*s
)
86 struct pcpu_freelist_head
*head
;
87 struct pcpu_freelist_node
*node
;
90 orig_cpu
= cpu
= raw_smp_processor_id();
92 head
= per_cpu_ptr(s
->freelist
, cpu
);
93 raw_spin_lock(&head
->lock
);
96 head
->first
= node
->next
;
97 raw_spin_unlock(&head
->lock
);
100 raw_spin_unlock(&head
->lock
);
101 cpu
= cpumask_next(cpu
, cpu_possible_mask
);
102 if (cpu
>= nr_cpu_ids
)
109 struct pcpu_freelist_node
*pcpu_freelist_pop(struct pcpu_freelist
*s
)
111 struct pcpu_freelist_node
*ret
;
114 local_irq_save(flags
);
115 ret
= __pcpu_freelist_pop(s
);
116 local_irq_restore(flags
);