rolling_buffer.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Rolling buffer helpers
  3. *
  4. * Copyright (C) 2024 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #include <linux/bitops.h>
  8. #include <linux/pagemap.h>
  9. #include <linux/rolling_buffer.h>
  10. #include <linux/slab.h>
  11. #include "internal.h"
  12. static atomic_t debug_ids;
  13. /**
  14. * netfs_folioq_alloc - Allocate a folio_queue struct
  15. * @rreq_id: Associated debugging ID for tracing purposes
  16. * @gfp: Allocation constraints
  17. * @trace: Trace tag to indicate the purpose of the allocation
  18. *
  19. * Allocate, initialise and account the folio_queue struct and log a trace line
  20. * to mark the allocation.
  21. */
  22. struct folio_queue *netfs_folioq_alloc(unsigned int rreq_id, gfp_t gfp,
  23. unsigned int /*enum netfs_folioq_trace*/ trace)
  24. {
  25. struct folio_queue *fq;
  26. fq = kmalloc_obj(*fq, gfp);
  27. if (fq) {
  28. netfs_stat(&netfs_n_folioq);
  29. folioq_init(fq, rreq_id);
  30. fq->debug_id = atomic_inc_return(&debug_ids);
  31. trace_netfs_folioq(fq, trace);
  32. }
  33. return fq;
  34. }
  35. EXPORT_SYMBOL(netfs_folioq_alloc);
  36. /**
  37. * netfs_folioq_free - Free a folio_queue struct
  38. * @folioq: The object to free
  39. * @trace: Trace tag to indicate which free
  40. *
  41. * Free and unaccount the folio_queue struct.
  42. */
  43. void netfs_folioq_free(struct folio_queue *folioq,
  44. unsigned int /*enum netfs_trace_folioq*/ trace)
  45. {
  46. trace_netfs_folioq(folioq, trace);
  47. netfs_stat_d(&netfs_n_folioq);
  48. kfree(folioq);
  49. }
  50. EXPORT_SYMBOL(netfs_folioq_free);
  51. /*
  52. * Initialise a rolling buffer. We allocate an empty folio queue struct to so
  53. * that the pointers can be independently driven by the producer and the
  54. * consumer.
  55. */
  56. int rolling_buffer_init(struct rolling_buffer *roll, unsigned int rreq_id,
  57. unsigned int direction)
  58. {
  59. struct folio_queue *fq;
  60. fq = netfs_folioq_alloc(rreq_id, GFP_NOFS, netfs_trace_folioq_rollbuf_init);
  61. if (!fq)
  62. return -ENOMEM;
  63. roll->head = fq;
  64. roll->tail = fq;
  65. iov_iter_folio_queue(&roll->iter, direction, fq, 0, 0, 0);
  66. return 0;
  67. }
  68. /*
  69. * Add another folio_queue to a rolling buffer if there's no space left.
  70. */
  71. int rolling_buffer_make_space(struct rolling_buffer *roll)
  72. {
  73. struct folio_queue *fq, *head = roll->head;
  74. if (!folioq_full(head))
  75. return 0;
  76. fq = netfs_folioq_alloc(head->rreq_id, GFP_NOFS, netfs_trace_folioq_make_space);
  77. if (!fq)
  78. return -ENOMEM;
  79. fq->prev = head;
  80. roll->head = fq;
  81. if (folioq_full(head)) {
  82. /* Make sure we don't leave the master iterator pointing to a
  83. * block that might get immediately consumed.
  84. */
  85. if (roll->iter.folioq == head &&
  86. roll->iter.folioq_slot == folioq_nr_slots(head)) {
  87. roll->iter.folioq = fq;
  88. roll->iter.folioq_slot = 0;
  89. }
  90. }
  91. /* Make sure the initialisation is stored before the next pointer.
  92. *
  93. * [!] NOTE: After we set head->next, the consumer is at liberty to
  94. * immediately delete the old head.
  95. */
  96. smp_store_release(&head->next, fq);
  97. return 0;
  98. }
  99. /*
  100. * Decant the list of folios to read into a rolling buffer.
  101. */
  102. ssize_t rolling_buffer_load_from_ra(struct rolling_buffer *roll,
  103. struct readahead_control *ractl,
  104. struct folio_batch *put_batch)
  105. {
  106. struct folio_queue *fq;
  107. struct page **vec;
  108. int nr, ix, to;
  109. ssize_t size = 0;
  110. if (rolling_buffer_make_space(roll) < 0)
  111. return -ENOMEM;
  112. fq = roll->head;
  113. vec = (struct page **)fq->vec.folios;
  114. nr = __readahead_batch(ractl, vec + folio_batch_count(&fq->vec),
  115. folio_batch_space(&fq->vec));
  116. ix = fq->vec.nr;
  117. to = ix + nr;
  118. fq->vec.nr = to;
  119. for (; ix < to; ix++) {
  120. struct folio *folio = folioq_folio(fq, ix);
  121. unsigned int order = folio_order(folio);
  122. fq->orders[ix] = order;
  123. size += PAGE_SIZE << order;
  124. trace_netfs_folio(folio, netfs_folio_trace_read);
  125. if (!folio_batch_add(put_batch, folio))
  126. folio_batch_release(put_batch);
  127. }
  128. WRITE_ONCE(roll->iter.count, roll->iter.count + size);
  129. /* Store the counter after setting the slot. */
  130. smp_store_release(&roll->next_head_slot, to);
  131. return size;
  132. }
  133. /*
  134. * Append a folio to the rolling buffer.
  135. */
  136. ssize_t rolling_buffer_append(struct rolling_buffer *roll, struct folio *folio,
  137. unsigned int flags)
  138. {
  139. ssize_t size = folio_size(folio);
  140. int slot;
  141. if (rolling_buffer_make_space(roll) < 0)
  142. return -ENOMEM;
  143. slot = folioq_append(roll->head, folio);
  144. if (flags & ROLLBUF_MARK_1)
  145. folioq_mark(roll->head, slot);
  146. if (flags & ROLLBUF_MARK_2)
  147. folioq_mark2(roll->head, slot);
  148. WRITE_ONCE(roll->iter.count, roll->iter.count + size);
  149. /* Store the counter after setting the slot. */
  150. smp_store_release(&roll->next_head_slot, slot);
  151. return size;
  152. }
  153. /*
  154. * Delete a spent buffer from a rolling queue and return the next in line. We
  155. * don't return the last buffer to keep the pointers independent, but return
  156. * NULL instead.
  157. */
  158. struct folio_queue *rolling_buffer_delete_spent(struct rolling_buffer *roll)
  159. {
  160. struct folio_queue *spent = roll->tail, *next = READ_ONCE(spent->next);
  161. if (!next)
  162. return NULL;
  163. next->prev = NULL;
  164. netfs_folioq_free(spent, netfs_trace_folioq_delete);
  165. roll->tail = next;
  166. return next;
  167. }
  168. /*
  169. * Clear out a rolling queue. Folios that have mark 1 set are put.
  170. */
  171. void rolling_buffer_clear(struct rolling_buffer *roll)
  172. {
  173. struct folio_batch fbatch;
  174. struct folio_queue *p;
  175. folio_batch_init(&fbatch);
  176. while ((p = roll->tail)) {
  177. roll->tail = p->next;
  178. for (int slot = 0; slot < folioq_count(p); slot++) {
  179. struct folio *folio = folioq_folio(p, slot);
  180. if (!folio)
  181. continue;
  182. if (folioq_is_marked(p, slot)) {
  183. trace_netfs_folio(folio, netfs_folio_trace_put);
  184. if (!folio_batch_add(&fbatch, folio))
  185. folio_batch_release(&fbatch);
  186. }
  187. }
  188. netfs_folioq_free(p, netfs_trace_folioq_clear);
  189. }
  190. folio_batch_release(&fbatch);
  191. }