errseq.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/err.h>
  3. #include <linux/bug.h>
  4. #include <linux/atomic.h>
  5. #include <linux/errseq.h>
  6. #include <linux/log2.h>
  7. /*
  8. * An errseq_t is a way of recording errors in one place, and allowing any
  9. * number of "subscribers" to tell whether it has changed since a previous
  10. * point where it was sampled.
  11. *
  12. * It's implemented as an unsigned 32-bit value. The low order bits are
  13. * designated to hold an error code (between 0 and -MAX_ERRNO). The upper bits
  14. * are used as a counter. This is done with atomics instead of locking so that
  15. * these functions can be called from any context.
  16. *
  17. * The general idea is for consumers to sample an errseq_t value. That value
  18. * can later be used to tell whether any new errors have occurred since that
  19. * sampling was done.
  20. *
  21. * Note that there is a risk of collisions if new errors are being recorded
  22. * frequently, since we have so few bits to use as a counter.
  23. *
  24. * To mitigate this, one bit is used as a flag to tell whether the value has
  25. * been sampled since a new value was recorded. That allows us to avoid bumping
  26. * the counter if no one has sampled it since the last time an error was
  27. * recorded.
  28. *
  29. * A new errseq_t should always be zeroed out. A errseq_t value of all zeroes
  30. * is the special (but common) case where there has never been an error. An all
  31. * zero value thus serves as the "epoch" if one wishes to know whether there
  32. * has ever been an error set since it was first initialized.
  33. */
  34. /* The low bits are designated for error code (max of MAX_ERRNO) */
  35. #define ERRSEQ_SHIFT ilog2(MAX_ERRNO + 1)
  36. /* This bit is used as a flag to indicate whether the value has been seen */
  37. #define ERRSEQ_SEEN (1 << ERRSEQ_SHIFT)
  38. /* The lowest bit of the counter */
  39. #define ERRSEQ_CTR_INC (1 << (ERRSEQ_SHIFT + 1))
  40. /**
  41. * errseq_set - set a errseq_t for later reporting
  42. * @eseq: errseq_t field that should be set
  43. * @err: error to set (must be between -1 and -MAX_ERRNO)
  44. *
  45. * This function sets the error in @eseq, and increments the sequence counter
  46. * if the last sequence was sampled at some point in the past.
  47. *
  48. * Any error set will always overwrite an existing error.
  49. *
  50. * Return: The previous value, primarily for debugging purposes. The
  51. * return value should not be used as a previously sampled value in later
  52. * calls as it will not have the SEEN flag set.
  53. */
  54. errseq_t errseq_set(errseq_t *eseq, int err)
  55. {
  56. errseq_t cur, old;
  57. /* MAX_ERRNO must be able to serve as a mask */
  58. BUILD_BUG_ON_NOT_POWER_OF_2(MAX_ERRNO + 1);
  59. /*
  60. * Ensure the error code actually fits where we want it to go. If it
  61. * doesn't then just throw a warning and don't record anything. We
  62. * also don't accept zero here as that would effectively clear a
  63. * previous error.
  64. */
  65. old = READ_ONCE(*eseq);
  66. if (WARN(unlikely(err == 0 || (unsigned int)-err > MAX_ERRNO),
  67. "err = %d\n", err))
  68. return old;
  69. for (;;) {
  70. errseq_t new;
  71. /* Clear out error bits and set new error */
  72. new = (old & ~(MAX_ERRNO|ERRSEQ_SEEN)) | -err;
  73. /* Only increment if someone has looked at it */
  74. if (old & ERRSEQ_SEEN)
  75. new += ERRSEQ_CTR_INC;
  76. /* If there would be no change, then call it done */
  77. if (new == old) {
  78. cur = new;
  79. break;
  80. }
  81. /* Try to swap the new value into place */
  82. cur = cmpxchg(eseq, old, new);
  83. /*
  84. * Call it success if we did the swap or someone else beat us
  85. * to it for the same value.
  86. */
  87. if (likely(cur == old || cur == new))
  88. break;
  89. /* Raced with an update, try again */
  90. old = cur;
  91. }
  92. return cur;
  93. }
  94. EXPORT_SYMBOL(errseq_set);
  95. /**
  96. * errseq_sample() - Grab current errseq_t value.
  97. * @eseq: Pointer to errseq_t to be sampled.
  98. *
  99. * This function allows callers to initialise their errseq_t variable.
  100. * If the error has been "seen", new callers will not see an old error.
  101. * If there is an unseen error in @eseq, the caller of this function will
  102. * see it the next time it checks for an error.
  103. *
  104. * Context: Any context.
  105. * Return: The current errseq value.
  106. */
  107. errseq_t errseq_sample(errseq_t *eseq)
  108. {
  109. errseq_t old = READ_ONCE(*eseq);
  110. /* If nobody has seen this error yet, then we can be the first. */
  111. if (!(old & ERRSEQ_SEEN))
  112. old = 0;
  113. return old;
  114. }
  115. EXPORT_SYMBOL(errseq_sample);
  116. /**
  117. * errseq_check() - Has an error occurred since a particular sample point?
  118. * @eseq: Pointer to errseq_t value to be checked.
  119. * @since: Previously-sampled errseq_t from which to check.
  120. *
  121. * Grab the value that eseq points to, and see if it has changed @since
  122. * the given value was sampled. The @since value is not advanced, so there
  123. * is no need to mark the value as seen.
  124. *
  125. * Return: The latest error set in the errseq_t or 0 if it hasn't changed.
  126. */
  127. int errseq_check(errseq_t *eseq, errseq_t since)
  128. {
  129. errseq_t cur = READ_ONCE(*eseq);
  130. if (likely(cur == since))
  131. return 0;
  132. return -(cur & MAX_ERRNO);
  133. }
  134. EXPORT_SYMBOL(errseq_check);
  135. /**
  136. * errseq_check_and_advance() - Check an errseq_t and advance to current value.
  137. * @eseq: Pointer to value being checked and reported.
  138. * @since: Pointer to previously-sampled errseq_t to check against and advance.
  139. *
  140. * Grab the eseq value, and see whether it matches the value that @since
  141. * points to. If it does, then just return 0.
  142. *
  143. * If it doesn't, then the value has changed. Set the "seen" flag, and try to
  144. * swap it into place as the new eseq value. Then, set that value as the new
  145. * "since" value, and return whatever the error portion is set to.
  146. *
  147. * Note that no locking is provided here for concurrent updates to the "since"
  148. * value. The caller must provide that if necessary. Because of this, callers
  149. * may want to do a lockless errseq_check before taking the lock and calling
  150. * this.
  151. *
  152. * Return: Negative errno if one has been stored, or 0 if no new error has
  153. * occurred.
  154. */
  155. int errseq_check_and_advance(errseq_t *eseq, errseq_t *since)
  156. {
  157. int err = 0;
  158. errseq_t old, new;
  159. /*
  160. * Most callers will want to use the inline wrapper to check this,
  161. * so that the common case of no error is handled without needing
  162. * to take the lock that protects the "since" value.
  163. */
  164. old = READ_ONCE(*eseq);
  165. if (old != *since) {
  166. /*
  167. * Set the flag and try to swap it into place if it has
  168. * changed.
  169. *
  170. * We don't care about the outcome of the swap here. If the
  171. * swap doesn't occur, then it has either been updated by a
  172. * writer who is altering the value in some way (updating
  173. * counter or resetting the error), or another reader who is
  174. * just setting the "seen" flag. Either outcome is OK, and we
  175. * can advance "since" and return an error based on what we
  176. * have.
  177. */
  178. new = old | ERRSEQ_SEEN;
  179. if (new != old)
  180. cmpxchg(eseq, old, new);
  181. *since = new;
  182. err = -(new & MAX_ERRNO);
  183. }
  184. return err;
  185. }
  186. EXPORT_SYMBOL(errseq_check_and_advance);