percpu_freelist.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2016 Facebook
  3. */
  4. #include "percpu_freelist.h"
  5. int pcpu_freelist_init(struct pcpu_freelist *s)
  6. {
  7. int cpu;
  8. s->freelist = alloc_percpu(struct pcpu_freelist_head);
  9. if (!s->freelist)
  10. return -ENOMEM;
  11. for_each_possible_cpu(cpu) {
  12. struct pcpu_freelist_head *head = per_cpu_ptr(s->freelist, cpu);
  13. raw_spin_lock_init(&head->lock);
  14. head->first = NULL;
  15. }
  16. raw_spin_lock_init(&s->extralist.lock);
  17. s->extralist.first = NULL;
  18. return 0;
  19. }
  20. void pcpu_freelist_destroy(struct pcpu_freelist *s)
  21. {
  22. free_percpu(s->freelist);
  23. }
  24. static inline void pcpu_freelist_push_node(struct pcpu_freelist_head *head,
  25. struct pcpu_freelist_node *node)
  26. {
  27. node->next = head->first;
  28. WRITE_ONCE(head->first, node);
  29. }
  30. static inline void ___pcpu_freelist_push(struct pcpu_freelist_head *head,
  31. struct pcpu_freelist_node *node)
  32. {
  33. raw_spin_lock(&head->lock);
  34. pcpu_freelist_push_node(head, node);
  35. raw_spin_unlock(&head->lock);
  36. }
  37. static inline bool pcpu_freelist_try_push_extra(struct pcpu_freelist *s,
  38. struct pcpu_freelist_node *node)
  39. {
  40. if (!raw_spin_trylock(&s->extralist.lock))
  41. return false;
  42. pcpu_freelist_push_node(&s->extralist, node);
  43. raw_spin_unlock(&s->extralist.lock);
  44. return true;
  45. }
  46. static inline void ___pcpu_freelist_push_nmi(struct pcpu_freelist *s,
  47. struct pcpu_freelist_node *node)
  48. {
  49. int cpu, orig_cpu;
  50. orig_cpu = raw_smp_processor_id();
  51. while (1) {
  52. for_each_cpu_wrap(cpu, cpu_possible_mask, orig_cpu) {
  53. struct pcpu_freelist_head *head;
  54. head = per_cpu_ptr(s->freelist, cpu);
  55. if (raw_spin_trylock(&head->lock)) {
  56. pcpu_freelist_push_node(head, node);
  57. raw_spin_unlock(&head->lock);
  58. return;
  59. }
  60. }
  61. /* cannot lock any per cpu lock, try extralist */
  62. if (pcpu_freelist_try_push_extra(s, node))
  63. return;
  64. }
  65. }
  66. void __pcpu_freelist_push(struct pcpu_freelist *s,
  67. struct pcpu_freelist_node *node)
  68. {
  69. if (in_nmi())
  70. ___pcpu_freelist_push_nmi(s, node);
  71. else
  72. ___pcpu_freelist_push(this_cpu_ptr(s->freelist), node);
  73. }
  74. void pcpu_freelist_push(struct pcpu_freelist *s,
  75. struct pcpu_freelist_node *node)
  76. {
  77. unsigned long flags;
  78. local_irq_save(flags);
  79. __pcpu_freelist_push(s, node);
  80. local_irq_restore(flags);
  81. }
  82. void pcpu_freelist_populate(struct pcpu_freelist *s, void *buf, u32 elem_size,
  83. u32 nr_elems)
  84. {
  85. struct pcpu_freelist_head *head;
  86. unsigned int cpu, cpu_idx, i, j, n, m;
  87. n = nr_elems / num_possible_cpus();
  88. m = nr_elems % num_possible_cpus();
  89. cpu_idx = 0;
  90. for_each_possible_cpu(cpu) {
  91. head = per_cpu_ptr(s->freelist, cpu);
  92. j = n + (cpu_idx < m ? 1 : 0);
  93. for (i = 0; i < j; i++) {
  94. /* No locking required as this is not visible yet. */
  95. pcpu_freelist_push_node(head, buf);
  96. buf += elem_size;
  97. }
  98. cpu_idx++;
  99. }
  100. }
  101. static struct pcpu_freelist_node *___pcpu_freelist_pop(struct pcpu_freelist *s)
  102. {
  103. struct pcpu_freelist_head *head;
  104. struct pcpu_freelist_node *node;
  105. int cpu;
  106. for_each_cpu_wrap(cpu, cpu_possible_mask, raw_smp_processor_id()) {
  107. head = per_cpu_ptr(s->freelist, cpu);
  108. if (!READ_ONCE(head->first))
  109. continue;
  110. raw_spin_lock(&head->lock);
  111. node = head->first;
  112. if (node) {
  113. WRITE_ONCE(head->first, node->next);
  114. raw_spin_unlock(&head->lock);
  115. return node;
  116. }
  117. raw_spin_unlock(&head->lock);
  118. }
  119. /* per cpu lists are all empty, try extralist */
  120. if (!READ_ONCE(s->extralist.first))
  121. return NULL;
  122. raw_spin_lock(&s->extralist.lock);
  123. node = s->extralist.first;
  124. if (node)
  125. WRITE_ONCE(s->extralist.first, node->next);
  126. raw_spin_unlock(&s->extralist.lock);
  127. return node;
  128. }
  129. static struct pcpu_freelist_node *
  130. ___pcpu_freelist_pop_nmi(struct pcpu_freelist *s)
  131. {
  132. struct pcpu_freelist_head *head;
  133. struct pcpu_freelist_node *node;
  134. int cpu;
  135. for_each_cpu_wrap(cpu, cpu_possible_mask, raw_smp_processor_id()) {
  136. head = per_cpu_ptr(s->freelist, cpu);
  137. if (!READ_ONCE(head->first))
  138. continue;
  139. if (raw_spin_trylock(&head->lock)) {
  140. node = head->first;
  141. if (node) {
  142. WRITE_ONCE(head->first, node->next);
  143. raw_spin_unlock(&head->lock);
  144. return node;
  145. }
  146. raw_spin_unlock(&head->lock);
  147. }
  148. }
  149. /* cannot pop from per cpu lists, try extralist */
  150. if (!READ_ONCE(s->extralist.first) || !raw_spin_trylock(&s->extralist.lock))
  151. return NULL;
  152. node = s->extralist.first;
  153. if (node)
  154. WRITE_ONCE(s->extralist.first, node->next);
  155. raw_spin_unlock(&s->extralist.lock);
  156. return node;
  157. }
  158. struct pcpu_freelist_node *__pcpu_freelist_pop(struct pcpu_freelist *s)
  159. {
  160. if (in_nmi())
  161. return ___pcpu_freelist_pop_nmi(s);
  162. return ___pcpu_freelist_pop(s);
  163. }
  164. struct pcpu_freelist_node *pcpu_freelist_pop(struct pcpu_freelist *s)
  165. {
  166. struct pcpu_freelist_node *ret;
  167. unsigned long flags;
  168. local_irq_save(flags);
  169. ret = __pcpu_freelist_pop(s);
  170. local_irq_restore(flags);
  171. return ret;
  172. }