freelist.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause */
  2. #ifndef FREELIST_H
  3. #define FREELIST_H
  4. #include <linux/atomic.h>
  5. /*
  6. * Copyright: [email protected]
  7. *
  8. * A simple CAS-based lock-free free list. Not the fastest thing in the world
  9. * under heavy contention, but simple and correct (assuming nodes are never
  10. * freed until after the free list is destroyed), and fairly speedy under low
  11. * contention.
  12. *
  13. * Adapted from: https://moodycamel.com/blog/2014/solving-the-aba-problem-for-lock-free-free-lists
  14. */
  15. struct freelist_node {
  16. atomic_t refs;
  17. struct freelist_node *next;
  18. };
  19. struct freelist_head {
  20. struct freelist_node *head;
  21. };
  22. #define REFS_ON_FREELIST 0x80000000
  23. #define REFS_MASK 0x7FFFFFFF
  24. static inline void __freelist_add(struct freelist_node *node, struct freelist_head *list)
  25. {
  26. /*
  27. * Since the refcount is zero, and nobody can increase it once it's
  28. * zero (except us, and we run only one copy of this method per node at
  29. * a time, i.e. the single thread case), then we know we can safely
  30. * change the next pointer of the node; however, once the refcount is
  31. * back above zero, then other threads could increase it (happens under
  32. * heavy contention, when the refcount goes to zero in between a load
  33. * and a refcount increment of a node in try_get, then back up to
  34. * something non-zero, then the refcount increment is done by the other
  35. * thread) -- so if the CAS to add the node to the actual list fails,
  36. * decrese the refcount and leave the add operation to the next thread
  37. * who puts the refcount back to zero (which could be us, hence the
  38. * loop).
  39. */
  40. struct freelist_node *head = READ_ONCE(list->head);
  41. for (;;) {
  42. WRITE_ONCE(node->next, head);
  43. atomic_set_release(&node->refs, 1);
  44. if (!try_cmpxchg_release(&list->head, &head, node)) {
  45. /*
  46. * Hmm, the add failed, but we can only try again when
  47. * the refcount goes back to zero.
  48. */
  49. if (atomic_fetch_add_release(REFS_ON_FREELIST - 1, &node->refs) == 1)
  50. continue;
  51. }
  52. return;
  53. }
  54. }
  55. static inline void freelist_add(struct freelist_node *node, struct freelist_head *list)
  56. {
  57. /*
  58. * We know that the should-be-on-freelist bit is 0 at this point, so
  59. * it's safe to set it using a fetch_add.
  60. */
  61. if (!atomic_fetch_add_release(REFS_ON_FREELIST, &node->refs)) {
  62. /*
  63. * Oh look! We were the last ones referencing this node, and we
  64. * know we want to add it to the free list, so let's do it!
  65. */
  66. __freelist_add(node, list);
  67. }
  68. }
  69. static inline struct freelist_node *freelist_try_get(struct freelist_head *list)
  70. {
  71. struct freelist_node *prev, *next, *head = smp_load_acquire(&list->head);
  72. unsigned int refs;
  73. while (head) {
  74. prev = head;
  75. refs = atomic_read(&head->refs);
  76. if ((refs & REFS_MASK) == 0 ||
  77. !atomic_try_cmpxchg_acquire(&head->refs, &refs, refs+1)) {
  78. head = smp_load_acquire(&list->head);
  79. continue;
  80. }
  81. /*
  82. * Good, reference count has been incremented (it wasn't at
  83. * zero), which means we can read the next and not worry about
  84. * it changing between now and the time we do the CAS.
  85. */
  86. next = READ_ONCE(head->next);
  87. if (try_cmpxchg_acquire(&list->head, &head, next)) {
  88. /*
  89. * Yay, got the node. This means it was on the list,
  90. * which means should-be-on-freelist must be false no
  91. * matter the refcount (because nobody else knows it's
  92. * been taken off yet, it can't have been put back on).
  93. */
  94. WARN_ON_ONCE(atomic_read(&head->refs) & REFS_ON_FREELIST);
  95. /*
  96. * Decrease refcount twice, once for our ref, and once
  97. * for the list's ref.
  98. */
  99. atomic_fetch_add(-2, &head->refs);
  100. return head;
  101. }
  102. /*
  103. * OK, the head must have changed on us, but we still need to decrement
  104. * the refcount we increased.
  105. */
  106. refs = atomic_fetch_add(-1, &prev->refs);
  107. if (refs == REFS_ON_FREELIST + 1)
  108. __freelist_add(prev, list);
  109. }
  110. return NULL;
  111. }
  112. #endif /* FREELIST_H */