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);
20 raw_spin_lock_init(&s->extralist.lock);
21 s->extralist.first = NULL;
25 void pcpu_freelist_destroy(struct pcpu_freelist *s)
27 free_percpu(s->freelist);
30 static inline void pcpu_freelist_push_node(struct pcpu_freelist_head *head,
31 struct pcpu_freelist_node *node)
33 node->next = head->first;
37 static inline void ___pcpu_freelist_push(struct pcpu_freelist_head *head,
38 struct pcpu_freelist_node *node)
40 raw_spin_lock(&head->lock);
41 pcpu_freelist_push_node(head, node);
42 raw_spin_unlock(&head->lock);
45 static inline bool pcpu_freelist_try_push_extra(struct pcpu_freelist *s,
46 struct pcpu_freelist_node *node)
48 if (!raw_spin_trylock(&s->extralist.lock))
51 pcpu_freelist_push_node(&s->extralist, node);
52 raw_spin_unlock(&s->extralist.lock);
56 static inline void ___pcpu_freelist_push_nmi(struct pcpu_freelist *s,
57 struct pcpu_freelist_node *node)
61 orig_cpu = cpu = raw_smp_processor_id();
63 struct pcpu_freelist_head *head;
65 head = per_cpu_ptr(s->freelist, cpu);
66 if (raw_spin_trylock(&head->lock)) {
67 pcpu_freelist_push_node(head, node);
68 raw_spin_unlock(&head->lock);
71 cpu = cpumask_next(cpu, cpu_possible_mask);
72 if (cpu >= nr_cpu_ids)
75 /* cannot lock any per cpu lock, try extralist */
76 if (cpu == orig_cpu &&
77 pcpu_freelist_try_push_extra(s, node))
82 void __pcpu_freelist_push(struct pcpu_freelist *s,
83 struct pcpu_freelist_node *node)
86 ___pcpu_freelist_push_nmi(s, node);
88 ___pcpu_freelist_push(this_cpu_ptr(s->freelist), node);
91 void pcpu_freelist_push(struct pcpu_freelist *s,
92 struct pcpu_freelist_node *node)
96 local_irq_save(flags);
97 __pcpu_freelist_push(s, node);
98 local_irq_restore(flags);
101 void pcpu_freelist_populate(struct pcpu_freelist *s, void *buf, u32 elem_size,
104 struct pcpu_freelist_head *head;
105 unsigned int cpu, cpu_idx, i, j, n, m;
107 n = nr_elems / num_possible_cpus();
108 m = nr_elems % num_possible_cpus();
111 for_each_possible_cpu(cpu) {
112 head = per_cpu_ptr(s->freelist, cpu);
113 j = n + (cpu_idx < m ? 1 : 0);
114 for (i = 0; i < j; i++) {
115 /* No locking required as this is not visible yet. */
116 pcpu_freelist_push_node(head, buf);
123 static struct pcpu_freelist_node *___pcpu_freelist_pop(struct pcpu_freelist *s)
125 struct pcpu_freelist_head *head;
126 struct pcpu_freelist_node *node;
129 orig_cpu = cpu = raw_smp_processor_id();
131 head = per_cpu_ptr(s->freelist, cpu);
132 raw_spin_lock(&head->lock);
135 head->first = node->next;
136 raw_spin_unlock(&head->lock);
139 raw_spin_unlock(&head->lock);
140 cpu = cpumask_next(cpu, cpu_possible_mask);
141 if (cpu >= nr_cpu_ids)
147 /* per cpu lists are all empty, try extralist */
148 raw_spin_lock(&s->extralist.lock);
149 node = s->extralist.first;
151 s->extralist.first = node->next;
152 raw_spin_unlock(&s->extralist.lock);
156 static struct pcpu_freelist_node *
157 ___pcpu_freelist_pop_nmi(struct pcpu_freelist *s)
159 struct pcpu_freelist_head *head;
160 struct pcpu_freelist_node *node;
163 orig_cpu = cpu = raw_smp_processor_id();
165 head = per_cpu_ptr(s->freelist, cpu);
166 if (raw_spin_trylock(&head->lock)) {
169 head->first = node->next;
170 raw_spin_unlock(&head->lock);
173 raw_spin_unlock(&head->lock);
175 cpu = cpumask_next(cpu, cpu_possible_mask);
176 if (cpu >= nr_cpu_ids)
182 /* cannot pop from per cpu lists, try extralist */
183 if (!raw_spin_trylock(&s->extralist.lock))
185 node = s->extralist.first;
187 s->extralist.first = node->next;
188 raw_spin_unlock(&s->extralist.lock);
192 struct pcpu_freelist_node *__pcpu_freelist_pop(struct pcpu_freelist *s)
195 return ___pcpu_freelist_pop_nmi(s);
196 return ___pcpu_freelist_pop(s);
199 struct pcpu_freelist_node *pcpu_freelist_pop(struct pcpu_freelist *s)
201 struct pcpu_freelist_node *ret;
204 local_irq_save(flags);
205 ret = __pcpu_freelist_pop(s);
206 local_irq_restore(flags);