lwq.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Light-weight single-linked queue.
  4. *
  5. * Entries are enqueued to the head of an llist, with no blocking.
  6. * This can happen in any context.
  7. *
  8. * Entries are dequeued using a spinlock to protect against multiple
  9. * access. The llist is staged in reverse order, and refreshed
  10. * from the llist when it exhausts.
  11. *
  12. * This is particularly suitable when work items are queued in BH or
  13. * IRQ context, and where work items are handled one at a time by
  14. * dedicated threads.
  15. */
  16. #include <linux/rcupdate.h>
  17. #include <linux/lwq.h>
  18. struct llist_node *__lwq_dequeue(struct lwq *q)
  19. {
  20. struct llist_node *this;
  21. if (lwq_empty(q))
  22. return NULL;
  23. spin_lock(&q->lock);
  24. this = q->ready;
  25. if (!this && !llist_empty(&q->new)) {
  26. /* ensure queue doesn't appear transiently lwq_empty */
  27. smp_store_release(&q->ready, (void *)1);
  28. this = llist_reverse_order(llist_del_all(&q->new));
  29. if (!this)
  30. q->ready = NULL;
  31. }
  32. if (this)
  33. q->ready = llist_next(this);
  34. spin_unlock(&q->lock);
  35. return this;
  36. }
  37. EXPORT_SYMBOL_GPL(__lwq_dequeue);
  38. /**
  39. * lwq_dequeue_all - dequeue all currently enqueued objects
  40. * @q: the queue to dequeue from
  41. *
  42. * Remove and return a linked list of llist_nodes of all the objects that were
  43. * in the queue. The first on the list will be the object that was least
  44. * recently enqueued.
  45. */
  46. struct llist_node *lwq_dequeue_all(struct lwq *q)
  47. {
  48. struct llist_node *r, *t, **ep;
  49. if (lwq_empty(q))
  50. return NULL;
  51. spin_lock(&q->lock);
  52. r = q->ready;
  53. q->ready = NULL;
  54. t = llist_del_all(&q->new);
  55. spin_unlock(&q->lock);
  56. ep = &r;
  57. while (*ep)
  58. ep = &(*ep)->next;
  59. *ep = llist_reverse_order(t);
  60. return r;
  61. }
  62. EXPORT_SYMBOL_GPL(lwq_dequeue_all);
  63. #if IS_ENABLED(CONFIG_LWQ_TEST)
  64. #include <linux/module.h>
  65. #include <linux/slab.h>
  66. #include <linux/wait_bit.h>
  67. #include <linux/kthread.h>
  68. #include <linux/delay.h>
  69. struct tnode {
  70. struct lwq_node n;
  71. int i;
  72. int c;
  73. };
  74. static int lwq_exercise(void *qv)
  75. {
  76. struct lwq *q = qv;
  77. int cnt;
  78. struct tnode *t;
  79. for (cnt = 0; cnt < 10000; cnt++) {
  80. wait_var_event(q, (t = lwq_dequeue(q, struct tnode, n)) != NULL);
  81. t->c++;
  82. if (lwq_enqueue(&t->n, q))
  83. wake_up_var(q);
  84. }
  85. while (!kthread_should_stop())
  86. schedule_timeout_idle(1);
  87. return 0;
  88. }
  89. static int lwq_test(void)
  90. {
  91. int i;
  92. struct lwq q;
  93. struct llist_node *l, **t1, *t2;
  94. struct tnode *t;
  95. struct task_struct *threads[8];
  96. printk(KERN_INFO "testing lwq....\n");
  97. lwq_init(&q);
  98. printk(KERN_INFO " lwq: run some threads\n");
  99. for (i = 0; i < ARRAY_SIZE(threads); i++)
  100. threads[i] = kthread_run(lwq_exercise, &q, "lwq-test-%d", i);
  101. for (i = 0; i < 100; i++) {
  102. t = kmalloc_obj(*t);
  103. if (!t)
  104. break;
  105. t->i = i;
  106. t->c = 0;
  107. if (lwq_enqueue(&t->n, &q))
  108. wake_up_var(&q);
  109. }
  110. /* wait for threads to exit */
  111. for (i = 0; i < ARRAY_SIZE(threads); i++)
  112. if (!IS_ERR_OR_NULL(threads[i]))
  113. kthread_stop(threads[i]);
  114. printk(KERN_INFO " lwq: dequeue first 50:");
  115. for (i = 0; i < 50 ; i++) {
  116. if (i && (i % 10) == 0) {
  117. printk(KERN_CONT "\n");
  118. printk(KERN_INFO " lwq: ... ");
  119. }
  120. t = lwq_dequeue(&q, struct tnode, n);
  121. if (t)
  122. printk(KERN_CONT " %d(%d)", t->i, t->c);
  123. kfree(t);
  124. }
  125. printk(KERN_CONT "\n");
  126. l = lwq_dequeue_all(&q);
  127. printk(KERN_INFO " lwq: delete the multiples of 3 (test lwq_for_each_safe())\n");
  128. lwq_for_each_safe(t, t1, t2, &l, n) {
  129. if ((t->i % 3) == 0) {
  130. t->i = -1;
  131. kfree(t);
  132. t = NULL;
  133. }
  134. }
  135. if (l)
  136. lwq_enqueue_batch(l, &q);
  137. printk(KERN_INFO " lwq: dequeue remaining:");
  138. while ((t = lwq_dequeue(&q, struct tnode, n)) != NULL) {
  139. printk(KERN_CONT " %d", t->i);
  140. kfree(t);
  141. }
  142. printk(KERN_CONT "\n");
  143. return 0;
  144. }
  145. module_init(lwq_test);
  146. #endif /* CONFIG_LWQ_TEST*/