llist.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Lock-less NULL terminated single linked list
  4. *
  5. * The basic atomic operation of this list is cmpxchg on long. On
  6. * architectures that don't have NMI-safe cmpxchg implementation, the
  7. * list can NOT be used in NMI handlers. So code that uses the list in
  8. * an NMI handler should depend on CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG.
  9. *
  10. * Copyright 2010,2011 Intel Corp.
  11. * Author: Huang Ying <ying.huang@intel.com>
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/export.h>
  15. #include <linux/llist.h>
  16. /**
  17. * llist_add_batch - add several linked entries in batch
  18. * @new_first: first entry in batch to be added
  19. * @new_last: last entry in batch to be added
  20. * @head: the head for your lock-less list
  21. *
  22. * Return whether list is empty before adding.
  23. */
  24. bool llist_add_batch(struct llist_node *new_first, struct llist_node *new_last,
  25. struct llist_head *head)
  26. {
  27. struct llist_node *first = READ_ONCE(head->first);
  28. do {
  29. new_last->next = first;
  30. } while (!try_cmpxchg(&head->first, &first, new_first));
  31. return !first;
  32. }
  33. EXPORT_SYMBOL_GPL(llist_add_batch);
  34. /**
  35. * llist_del_first - delete the first entry of lock-less list
  36. * @head: the head for your lock-less list
  37. *
  38. * If list is empty, return NULL, otherwise, return the first entry
  39. * deleted, this is the newest added one.
  40. *
  41. * Only one llist_del_first user can be used simultaneously with
  42. * multiple llist_add users without lock. Because otherwise
  43. * llist_del_first, llist_add, llist_add (or llist_del_all, llist_add,
  44. * llist_add) sequence in another user may change @head->first->next,
  45. * but keep @head->first. If multiple consumers are needed, please
  46. * use llist_del_all or use lock between consumers.
  47. */
  48. struct llist_node *llist_del_first(struct llist_head *head)
  49. {
  50. struct llist_node *entry, *next;
  51. entry = smp_load_acquire(&head->first);
  52. do {
  53. if (entry == NULL)
  54. return NULL;
  55. next = READ_ONCE(entry->next);
  56. } while (!try_cmpxchg(&head->first, &entry, next));
  57. return entry;
  58. }
  59. EXPORT_SYMBOL_GPL(llist_del_first);
  60. /**
  61. * llist_del_first_this - delete given entry of lock-less list if it is first
  62. * @head: the head for your lock-less list
  63. * @this: a list entry.
  64. *
  65. * If head of the list is given entry, delete and return %true else
  66. * return %false.
  67. *
  68. * Multiple callers can safely call this concurrently with multiple
  69. * llist_add() callers, providing all the callers offer a different @this.
  70. */
  71. bool llist_del_first_this(struct llist_head *head,
  72. struct llist_node *this)
  73. {
  74. struct llist_node *entry, *next;
  75. /* acquire ensures orderig wrt try_cmpxchg() is llist_del_first() */
  76. entry = smp_load_acquire(&head->first);
  77. do {
  78. if (entry != this)
  79. return false;
  80. next = READ_ONCE(entry->next);
  81. } while (!try_cmpxchg(&head->first, &entry, next));
  82. return true;
  83. }
  84. EXPORT_SYMBOL_GPL(llist_del_first_this);
  85. /**
  86. * llist_reverse_order - reverse order of a llist chain
  87. * @head: first item of the list to be reversed
  88. *
  89. * Reverse the order of a chain of llist entries and return the
  90. * new first entry.
  91. */
  92. struct llist_node *llist_reverse_order(struct llist_node *head)
  93. {
  94. struct llist_node *new_head = NULL;
  95. while (head) {
  96. struct llist_node *tmp = head;
  97. head = head->next;
  98. tmp->next = new_head;
  99. new_head = tmp;
  100. }
  101. return new_head;
  102. }
  103. EXPORT_SYMBOL_GPL(llist_reverse_order);