blob: df782418b333eedea39cdb793c38ad623f96a707 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Jeff Layton84cbada2017-07-06 07:02:24 -04002#include <linux/err.h>
3#include <linux/bug.h>
4#include <linux/atomic.h>
5#include <linux/errseq.h>
6
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
35/* The low bits are designated for error code (max of MAX_ERRNO) */
36#define ERRSEQ_SHIFT ilog2(MAX_ERRNO + 1)
37
38/* This bit is used as a flag to indicate whether the value has been seen */
39#define ERRSEQ_SEEN (1 << ERRSEQ_SHIFT)
40
41/* The lowest bit of the counter */
42#define ERRSEQ_CTR_INC (1 << (ERRSEQ_SHIFT + 1))
43
44/**
Jeff Layton3acdfd22017-07-24 06:22:15 -040045 * errseq_set - set a errseq_t for later reporting
Jeff Layton84cbada2017-07-06 07:02:24 -040046 * @eseq: errseq_t field that should be set
Jeff Layton3acdfd22017-07-24 06:22:15 -040047 * @err: error to set (must be between -1 and -MAX_ERRNO)
Jeff Layton84cbada2017-07-06 07:02:24 -040048 *
Matthew Wilcox14ebc282017-12-22 06:32:16 -080049 * This function sets the error in @eseq, and increments the sequence counter
Jeff Layton84cbada2017-07-06 07:02:24 -040050 * if the last sequence was sampled at some point in the past.
51 *
52 * Any error set will always overwrite an existing error.
53 *
Matthew Wilcox14ebc282017-12-22 06:32:16 -080054 * Return: The previous value, primarily for debugging purposes. The
55 * return value should not be used as a previously sampled value in later
56 * calls as it will not have the SEEN flag set.
Jeff Layton84cbada2017-07-06 07:02:24 -040057 */
Jeff Layton3acdfd22017-07-24 06:22:15 -040058errseq_t errseq_set(errseq_t *eseq, int err)
Jeff Layton84cbada2017-07-06 07:02:24 -040059{
60 errseq_t cur, old;
61
62 /* MAX_ERRNO must be able to serve as a mask */
63 BUILD_BUG_ON_NOT_POWER_OF_2(MAX_ERRNO + 1);
64
65 /*
66 * Ensure the error code actually fits where we want it to go. If it
67 * doesn't then just throw a warning and don't record anything. We
68 * also don't accept zero here as that would effectively clear a
69 * previous error.
70 */
71 old = READ_ONCE(*eseq);
72
73 if (WARN(unlikely(err == 0 || (unsigned int)-err > MAX_ERRNO),
74 "err = %d\n", err))
75 return old;
76
77 for (;;) {
78 errseq_t new;
79
80 /* Clear out error bits and set new error */
81 new = (old & ~(MAX_ERRNO|ERRSEQ_SEEN)) | -err;
82
83 /* Only increment if someone has looked at it */
84 if (old & ERRSEQ_SEEN)
85 new += ERRSEQ_CTR_INC;
86
87 /* If there would be no change, then call it done */
88 if (new == old) {
89 cur = new;
90 break;
91 }
92
93 /* Try to swap the new value into place */
94 cur = cmpxchg(eseq, old, new);
95
96 /*
97 * Call it success if we did the swap or someone else beat us
98 * to it for the same value.
99 */
100 if (likely(cur == old || cur == new))
101 break;
102
103 /* Raced with an update, try again */
104 old = cur;
105 }
106 return cur;
107}
Jeff Layton3acdfd22017-07-24 06:22:15 -0400108EXPORT_SYMBOL(errseq_set);
Jeff Layton84cbada2017-07-06 07:02:24 -0400109
110/**
Matthew Wilcox14ebc282017-12-22 06:32:16 -0800111 * errseq_sample() - Grab current errseq_t value.
112 * @eseq: Pointer to errseq_t to be sampled.
Jeff Layton84cbada2017-07-06 07:02:24 -0400113 *
114 * This function allows callers to sample an errseq_t value, marking it as
115 * "seen" if required.
Matthew Wilcox14ebc282017-12-22 06:32:16 -0800116 *
117 * Return: The current errseq value.
Jeff Layton84cbada2017-07-06 07:02:24 -0400118 */
119errseq_t errseq_sample(errseq_t *eseq)
120{
121 errseq_t old = READ_ONCE(*eseq);
122 errseq_t new = old;
123
124 /*
125 * For the common case of no errors ever having been set, we can skip
126 * marking the SEEN bit. Once an error has been set, the value will
127 * never go back to zero.
128 */
129 if (old != 0) {
130 new |= ERRSEQ_SEEN;
131 if (old != new)
132 cmpxchg(eseq, old, new);
133 }
134 return new;
135}
136EXPORT_SYMBOL(errseq_sample);
137
138/**
Matthew Wilcox14ebc282017-12-22 06:32:16 -0800139 * errseq_check() - Has an error occurred since a particular sample point?
140 * @eseq: Pointer to errseq_t value to be checked.
141 * @since: Previously-sampled errseq_t from which to check.
Jeff Layton84cbada2017-07-06 07:02:24 -0400142 *
Matthew Wilcox14ebc282017-12-22 06:32:16 -0800143 * Grab the value that eseq points to, and see if it has changed @since
144 * the given value was sampled. The @since value is not advanced, so there
Jeff Layton84cbada2017-07-06 07:02:24 -0400145 * is no need to mark the value as seen.
146 *
Matthew Wilcox14ebc282017-12-22 06:32:16 -0800147 * Return: The latest error set in the errseq_t or 0 if it hasn't changed.
Jeff Layton84cbada2017-07-06 07:02:24 -0400148 */
149int errseq_check(errseq_t *eseq, errseq_t since)
150{
151 errseq_t cur = READ_ONCE(*eseq);
152
153 if (likely(cur == since))
154 return 0;
155 return -(cur & MAX_ERRNO);
156}
157EXPORT_SYMBOL(errseq_check);
158
159/**
Matthew Wilcox14ebc282017-12-22 06:32:16 -0800160 * errseq_check_and_advance() - Check an errseq_t and advance to current value.
161 * @eseq: Pointer to value being checked and reported.
162 * @since: Pointer to previously-sampled errseq_t to check against and advance.
Jeff Layton84cbada2017-07-06 07:02:24 -0400163 *
Matthew Wilcox14ebc282017-12-22 06:32:16 -0800164 * Grab the eseq value, and see whether it matches the value that @since
Jeff Layton84cbada2017-07-06 07:02:24 -0400165 * points to. If it does, then just return 0.
166 *
167 * If it doesn't, then the value has changed. Set the "seen" flag, and try to
168 * swap it into place as the new eseq value. Then, set that value as the new
169 * "since" value, and return whatever the error portion is set to.
170 *
171 * Note that no locking is provided here for concurrent updates to the "since"
172 * value. The caller must provide that if necessary. Because of this, callers
173 * may want to do a lockless errseq_check before taking the lock and calling
174 * this.
Matthew Wilcox14ebc282017-12-22 06:32:16 -0800175 *
176 * Return: Negative errno if one has been stored, or 0 if no new error has
177 * occurred.
Jeff Layton84cbada2017-07-06 07:02:24 -0400178 */
179int errseq_check_and_advance(errseq_t *eseq, errseq_t *since)
180{
181 int err = 0;
182 errseq_t old, new;
183
184 /*
185 * Most callers will want to use the inline wrapper to check this,
186 * so that the common case of no error is handled without needing
187 * to take the lock that protects the "since" value.
188 */
189 old = READ_ONCE(*eseq);
190 if (old != *since) {
191 /*
192 * Set the flag and try to swap it into place if it has
193 * changed.
194 *
195 * We don't care about the outcome of the swap here. If the
196 * swap doesn't occur, then it has either been updated by a
197 * writer who is altering the value in some way (updating
198 * counter or resetting the error), or another reader who is
199 * just setting the "seen" flag. Either outcome is OK, and we
200 * can advance "since" and return an error based on what we
201 * have.
202 */
203 new = old | ERRSEQ_SEEN;
204 if (new != old)
205 cmpxchg(eseq, old, new);
206 *since = new;
207 err = -(new & MAX_ERRNO);
208 }
209 return err;
210}
211EXPORT_SYMBOL(errseq_check_and_advance);