llist.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_del_first - delete the first entry of lock-less list
  18. * @head: the head for your lock-less list
  19. *
  20. * If list is empty, return NULL, otherwise, return the first entry
  21. * deleted, this is the newest added one.
  22. *
  23. * Only one llist_del_first user can be used simultaneously with
  24. * multiple llist_add users without lock. Because otherwise
  25. * llist_del_first, llist_add, llist_add (or llist_del_all, llist_add,
  26. * llist_add) sequence in another user may change @head->first->next,
  27. * but keep @head->first. If multiple consumers are needed, please
  28. * use llist_del_all or use lock between consumers.
  29. */
  30. struct llist_node *llist_del_first(struct llist_head *head)
  31. {
  32. struct llist_node *entry, *next;
  33. entry = smp_load_acquire(&head->first);
  34. do {
  35. if (entry == NULL)
  36. return NULL;
  37. next = READ_ONCE(entry->next);
  38. } while (!try_cmpxchg(&head->first, &entry, next));
  39. return entry;
  40. }
  41. EXPORT_SYMBOL_GPL(llist_del_first);
  42. /**
  43. * llist_del_first_this - delete given entry of lock-less list if it is first
  44. * @head: the head for your lock-less list
  45. * @this: a list entry.
  46. *
  47. * If head of the list is given entry, delete and return %true else
  48. * return %false.
  49. *
  50. * Multiple callers can safely call this concurrently with multiple
  51. * llist_add() callers, providing all the callers offer a different @this.
  52. */
  53. bool llist_del_first_this(struct llist_head *head,
  54. struct llist_node *this)
  55. {
  56. struct llist_node *entry, *next;
  57. /* acquire ensures orderig wrt try_cmpxchg() is llist_del_first() */
  58. entry = smp_load_acquire(&head->first);
  59. do {
  60. if (entry != this)
  61. return false;
  62. next = READ_ONCE(entry->next);
  63. } while (!try_cmpxchg(&head->first, &entry, next));
  64. return true;
  65. }
  66. EXPORT_SYMBOL_GPL(llist_del_first_this);
  67. /**
  68. * llist_reverse_order - reverse order of a llist chain
  69. * @head: first item of the list to be reversed
  70. *
  71. * Reverse the order of a chain of llist entries and return the
  72. * new first entry.
  73. */
  74. struct llist_node *llist_reverse_order(struct llist_node *head)
  75. {
  76. struct llist_node *new_head = NULL;
  77. while (head) {
  78. struct llist_node *tmp = head;
  79. head = head->next;
  80. tmp->next = new_head;
  81. new_head = tmp;
  82. }
  83. return new_head;
  84. }
  85. EXPORT_SYMBOL_GPL(llist_reverse_order);