folio_queue.rst 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. .. SPDX-License-Identifier: GPL-2.0+
  2. ===========
  3. Folio Queue
  4. ===========
  5. :Author: David Howells <dhowells@redhat.com>
  6. .. Contents:
  7. * Overview
  8. * Initialisation
  9. * Adding and removing folios
  10. * Querying information about a folio
  11. * Querying information about a folio_queue
  12. * Folio queue iteration
  13. * Folio marks
  14. * Lockless simultaneous production/consumption issues
  15. Overview
  16. ========
  17. The folio_queue struct forms a single segment in a segmented list of folios
  18. that can be used to form an I/O buffer. As such, the list can be iterated over
  19. using the ITER_FOLIOQ iov_iter type.
  20. The publicly accessible members of the structure are::
  21. struct folio_queue {
  22. struct folio_queue *next;
  23. struct folio_queue *prev;
  24. ...
  25. };
  26. A pair of pointers are provided, ``next`` and ``prev``, that point to the
  27. segments on either side of the segment being accessed. Whilst this is a
  28. doubly-linked list, it is intentionally not a circular list; the outward
  29. sibling pointers in terminal segments should be NULL.
  30. Each segment in the list also stores:
  31. * an ordered sequence of folio pointers,
  32. * the size of each folio and
  33. * three 1-bit marks per folio,
  34. but these should not be accessed directly as the underlying data structure may
  35. change, but rather the access functions outlined below should be used.
  36. The facility can be made accessible by::
  37. #include <linux/folio_queue.h>
  38. and to use the iterator::
  39. #include <linux/uio.h>
  40. Initialisation
  41. ==============
  42. A segment should be initialised by calling::
  43. void folioq_init(struct folio_queue *folioq);
  44. with a pointer to the segment to be initialised. Note that this will not
  45. necessarily initialise all the folio pointers, so care must be taken to check
  46. the number of folios added.
  47. Adding and removing folios
  48. ==========================
  49. Folios can be set in the next unused slot in a segment struct by calling one
  50. of::
  51. unsigned int folioq_append(struct folio_queue *folioq,
  52. struct folio *folio);
  53. unsigned int folioq_append_mark(struct folio_queue *folioq,
  54. struct folio *folio);
  55. Both functions update the stored folio count, store the folio and note its
  56. size. The second function also sets the first mark for the folio added. Both
  57. functions return the number of the slot used. [!] Note that no attempt is made
  58. to check that the capacity wasn't overrun and the list will not be extended
  59. automatically.
  60. A folio can be excised by calling::
  61. void folioq_clear(struct folio_queue *folioq, unsigned int slot);
  62. This clears the slot in the array and also clears all the marks for that folio,
  63. but doesn't change the folio count - so future accesses of that slot must check
  64. if the slot is occupied.
  65. Querying information about a folio
  66. ==================================
  67. Information about the folio in a particular slot may be queried by the
  68. following function::
  69. struct folio *folioq_folio(const struct folio_queue *folioq,
  70. unsigned int slot);
  71. If a folio has not yet been set in that slot, this may yield an undefined
  72. pointer. The size of the folio in a slot may be queried with either of::
  73. unsigned int folioq_folio_order(const struct folio_queue *folioq,
  74. unsigned int slot);
  75. size_t folioq_folio_size(const struct folio_queue *folioq,
  76. unsigned int slot);
  77. The first function returns the size as an order and the second as a number of
  78. bytes.
  79. Querying information about a folio_queue
  80. ========================================
  81. Information may be retrieved about a particular segment with the following
  82. functions::
  83. unsigned int folioq_nr_slots(const struct folio_queue *folioq);
  84. unsigned int folioq_count(struct folio_queue *folioq);
  85. bool folioq_full(struct folio_queue *folioq);
  86. The first function returns the maximum capacity of a segment. It must not be
  87. assumed that this won't vary between segments. The second returns the number
  88. of folios added to a segments and the third is a shorthand to indicate if the
  89. segment has been filled to capacity.
  90. Not that the count and fullness are not affected by clearing folios from the
  91. segment. These are more about indicating how many slots in the array have been
  92. initialised, and it assumed that slots won't get reused, but rather the segment
  93. will get discarded as the queue is consumed.
  94. Folio marks
  95. ===========
  96. Folios within a queue can also have marks assigned to them. These marks can be
  97. used to note information such as if a folio needs folio_put() calling upon it.
  98. There are three marks available to be set for each folio.
  99. The marks can be set by::
  100. void folioq_mark(struct folio_queue *folioq, unsigned int slot);
  101. void folioq_mark2(struct folio_queue *folioq, unsigned int slot);
  102. Cleared by::
  103. void folioq_unmark(struct folio_queue *folioq, unsigned int slot);
  104. void folioq_unmark2(struct folio_queue *folioq, unsigned int slot);
  105. And the marks can be queried by::
  106. bool folioq_is_marked(const struct folio_queue *folioq, unsigned int slot);
  107. bool folioq_is_marked2(const struct folio_queue *folioq, unsigned int slot);
  108. The marks can be used for any purpose and are not interpreted by this API.
  109. Folio queue iteration
  110. =====================
  111. A list of segments may be iterated over using the I/O iterator facility using
  112. an ``iov_iter`` iterator of ``ITER_FOLIOQ`` type. The iterator may be
  113. initialised with::
  114. void iov_iter_folio_queue(struct iov_iter *i, unsigned int direction,
  115. const struct folio_queue *folioq,
  116. unsigned int first_slot, unsigned int offset,
  117. size_t count);
  118. This may be told to start at a particular segment, slot and offset within a
  119. queue. The iov iterator functions will follow the next pointers when advancing
  120. and prev pointers when reverting when needed.
  121. Lockless simultaneous production/consumption issues
  122. ===================================================
  123. If properly managed, the list can be extended by the producer at the head end
  124. and shortened by the consumer at the tail end simultaneously without the need
  125. to take locks. The ITER_FOLIOQ iterator inserts appropriate barriers to aid
  126. with this.
  127. Care must be taken when simultaneously producing and consuming a list. If the
  128. last segment is reached and the folios it refers to are entirely consumed by
  129. the IOV iterators, an iov_iter struct will be left pointing to the last segment
  130. with a slot number equal to the capacity of that segment. The iterator will
  131. try to continue on from this if there's another segment available when it is
  132. used again, but care must be taken lest the segment got removed and freed by
  133. the consumer before the iterator was advanced.
  134. It is recommended that the queue always contain at least one segment, even if
  135. that segment has never been filled or is entirely spent. This prevents the
  136. head and tail pointers from collapsing.
  137. API Function Reference
  138. ======================
  139. .. kernel-doc:: include/linux/folio_queue.h