1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
284cbadadSJeff Layton #include <linux/err.h>
384cbadadSJeff Layton #include <linux/bug.h>
484cbadadSJeff Layton #include <linux/atomic.h>
584cbadadSJeff Layton #include <linux/errseq.h>
6aa6159abSAndy Shevchenko #include <linux/log2.h>
784cbadadSJeff Layton
884cbadadSJeff Layton /*
984cbadadSJeff Layton * An errseq_t is a way of recording errors in one place, and allowing any
1084cbadadSJeff Layton * number of "subscribers" to tell whether it has changed since a previous
1184cbadadSJeff Layton * point where it was sampled.
1284cbadadSJeff Layton *
1384cbadadSJeff Layton * It's implemented as an unsigned 32-bit value. The low order bits are
1484cbadadSJeff Layton * designated to hold an error code (between 0 and -MAX_ERRNO). The upper bits
1584cbadadSJeff Layton * are used as a counter. This is done with atomics instead of locking so that
1684cbadadSJeff Layton * these functions can be called from any context.
1784cbadadSJeff Layton *
1884cbadadSJeff Layton * The general idea is for consumers to sample an errseq_t value. That value
1984cbadadSJeff Layton * can later be used to tell whether any new errors have occurred since that
2084cbadadSJeff Layton * sampling was done.
2184cbadadSJeff Layton *
2284cbadadSJeff Layton * Note that there is a risk of collisions if new errors are being recorded
2384cbadadSJeff Layton * frequently, since we have so few bits to use as a counter.
2484cbadadSJeff Layton *
2584cbadadSJeff Layton * To mitigate this, one bit is used as a flag to tell whether the value has
2684cbadadSJeff Layton * been sampled since a new value was recorded. That allows us to avoid bumping
2784cbadadSJeff Layton * the counter if no one has sampled it since the last time an error was
2884cbadadSJeff Layton * recorded.
2984cbadadSJeff Layton *
3084cbadadSJeff Layton * A new errseq_t should always be zeroed out. A errseq_t value of all zeroes
3184cbadadSJeff Layton * is the special (but common) case where there has never been an error. An all
3284cbadadSJeff Layton * zero value thus serves as the "epoch" if one wishes to know whether there
3384cbadadSJeff Layton * has ever been an error set since it was first initialized.
3484cbadadSJeff Layton */
3584cbadadSJeff Layton
3684cbadadSJeff Layton /* The low bits are designated for error code (max of MAX_ERRNO) */
37*3eff6a3eSZijun Hu #define ERRSEQ_SHIFT (ilog2(MAX_ERRNO) + 1)
3884cbadadSJeff Layton
3984cbadadSJeff Layton /* This bit is used as a flag to indicate whether the value has been seen */
4084cbadadSJeff Layton #define ERRSEQ_SEEN (1 << ERRSEQ_SHIFT)
4184cbadadSJeff Layton
42*3eff6a3eSZijun Hu /* Leverage macro ERRSEQ_SEEN to define errno mask macro here */
43*3eff6a3eSZijun Hu #define ERRNO_MASK (ERRSEQ_SEEN - 1)
44*3eff6a3eSZijun Hu
4584cbadadSJeff Layton /* The lowest bit of the counter */
4684cbadadSJeff Layton #define ERRSEQ_CTR_INC (1 << (ERRSEQ_SHIFT + 1))
4784cbadadSJeff Layton
4884cbadadSJeff Layton /**
493acdfd28SJeff Layton * errseq_set - set a errseq_t for later reporting
5084cbadadSJeff Layton * @eseq: errseq_t field that should be set
513acdfd28SJeff Layton * @err: error to set (must be between -1 and -MAX_ERRNO)
5284cbadadSJeff Layton *
5314ebc28eSMatthew Wilcox * This function sets the error in @eseq, and increments the sequence counter
5484cbadadSJeff Layton * if the last sequence was sampled at some point in the past.
5584cbadadSJeff Layton *
5684cbadadSJeff Layton * Any error set will always overwrite an existing error.
5784cbadadSJeff Layton *
5814ebc28eSMatthew Wilcox * Return: The previous value, primarily for debugging purposes. The
5914ebc28eSMatthew Wilcox * return value should not be used as a previously sampled value in later
6014ebc28eSMatthew Wilcox * calls as it will not have the SEEN flag set.
6184cbadadSJeff Layton */
errseq_set(errseq_t * eseq,int err)623acdfd28SJeff Layton errseq_t errseq_set(errseq_t *eseq, int err)
6384cbadadSJeff Layton {
6484cbadadSJeff Layton errseq_t cur, old;
6584cbadadSJeff Layton
6684cbadadSJeff Layton
6784cbadadSJeff Layton /*
6884cbadadSJeff Layton * Ensure the error code actually fits where we want it to go. If it
6984cbadadSJeff Layton * doesn't then just throw a warning and don't record anything. We
7084cbadadSJeff Layton * also don't accept zero here as that would effectively clear a
7184cbadadSJeff Layton * previous error.
7284cbadadSJeff Layton */
7384cbadadSJeff Layton old = READ_ONCE(*eseq);
7484cbadadSJeff Layton
7584cbadadSJeff Layton if (WARN(unlikely(err == 0 || (unsigned int)-err > MAX_ERRNO),
7684cbadadSJeff Layton "err = %d\n", err))
7784cbadadSJeff Layton return old;
7884cbadadSJeff Layton
7984cbadadSJeff Layton for (;;) {
8084cbadadSJeff Layton errseq_t new;
8184cbadadSJeff Layton
8284cbadadSJeff Layton /* Clear out error bits and set new error */
83*3eff6a3eSZijun Hu new = (old & ~(ERRNO_MASK | ERRSEQ_SEEN)) | -err;
8484cbadadSJeff Layton
8584cbadadSJeff Layton /* Only increment if someone has looked at it */
8684cbadadSJeff Layton if (old & ERRSEQ_SEEN)
8784cbadadSJeff Layton new += ERRSEQ_CTR_INC;
8884cbadadSJeff Layton
8984cbadadSJeff Layton /* If there would be no change, then call it done */
9084cbadadSJeff Layton if (new == old) {
9184cbadadSJeff Layton cur = new;
9284cbadadSJeff Layton break;
9384cbadadSJeff Layton }
9484cbadadSJeff Layton
9584cbadadSJeff Layton /* Try to swap the new value into place */
9684cbadadSJeff Layton cur = cmpxchg(eseq, old, new);
9784cbadadSJeff Layton
9884cbadadSJeff Layton /*
9984cbadadSJeff Layton * Call it success if we did the swap or someone else beat us
10084cbadadSJeff Layton * to it for the same value.
10184cbadadSJeff Layton */
10284cbadadSJeff Layton if (likely(cur == old || cur == new))
10384cbadadSJeff Layton break;
10484cbadadSJeff Layton
10584cbadadSJeff Layton /* Raced with an update, try again */
10684cbadadSJeff Layton old = cur;
10784cbadadSJeff Layton }
10884cbadadSJeff Layton return cur;
10984cbadadSJeff Layton }
1103acdfd28SJeff Layton EXPORT_SYMBOL(errseq_set);
11184cbadadSJeff Layton
11284cbadadSJeff Layton /**
11314ebc28eSMatthew Wilcox * errseq_sample() - Grab current errseq_t value.
11414ebc28eSMatthew Wilcox * @eseq: Pointer to errseq_t to be sampled.
11584cbadadSJeff Layton *
116b4678df1SMatthew Wilcox * This function allows callers to initialise their errseq_t variable.
117b4678df1SMatthew Wilcox * If the error has been "seen", new callers will not see an old error.
118b4678df1SMatthew Wilcox * If there is an unseen error in @eseq, the caller of this function will
119b4678df1SMatthew Wilcox * see it the next time it checks for an error.
12014ebc28eSMatthew Wilcox *
121b4678df1SMatthew Wilcox * Context: Any context.
12214ebc28eSMatthew Wilcox * Return: The current errseq value.
12384cbadadSJeff Layton */
errseq_sample(errseq_t * eseq)12484cbadadSJeff Layton errseq_t errseq_sample(errseq_t *eseq)
12584cbadadSJeff Layton {
12684cbadadSJeff Layton errseq_t old = READ_ONCE(*eseq);
12784cbadadSJeff Layton
128b4678df1SMatthew Wilcox /* If nobody has seen this error yet, then we can be the first. */
129b4678df1SMatthew Wilcox if (!(old & ERRSEQ_SEEN))
130b4678df1SMatthew Wilcox old = 0;
131b4678df1SMatthew Wilcox return old;
13284cbadadSJeff Layton }
13384cbadadSJeff Layton EXPORT_SYMBOL(errseq_sample);
13484cbadadSJeff Layton
13584cbadadSJeff Layton /**
13614ebc28eSMatthew Wilcox * errseq_check() - Has an error occurred since a particular sample point?
13714ebc28eSMatthew Wilcox * @eseq: Pointer to errseq_t value to be checked.
13814ebc28eSMatthew Wilcox * @since: Previously-sampled errseq_t from which to check.
13984cbadadSJeff Layton *
14014ebc28eSMatthew Wilcox * Grab the value that eseq points to, and see if it has changed @since
14114ebc28eSMatthew Wilcox * the given value was sampled. The @since value is not advanced, so there
14284cbadadSJeff Layton * is no need to mark the value as seen.
14384cbadadSJeff Layton *
14414ebc28eSMatthew Wilcox * Return: The latest error set in the errseq_t or 0 if it hasn't changed.
14584cbadadSJeff Layton */
errseq_check(errseq_t * eseq,errseq_t since)14684cbadadSJeff Layton int errseq_check(errseq_t *eseq, errseq_t since)
14784cbadadSJeff Layton {
14884cbadadSJeff Layton errseq_t cur = READ_ONCE(*eseq);
14984cbadadSJeff Layton
15084cbadadSJeff Layton if (likely(cur == since))
15184cbadadSJeff Layton return 0;
152*3eff6a3eSZijun Hu return -(cur & ERRNO_MASK);
15384cbadadSJeff Layton }
15484cbadadSJeff Layton EXPORT_SYMBOL(errseq_check);
15584cbadadSJeff Layton
15684cbadadSJeff Layton /**
15714ebc28eSMatthew Wilcox * errseq_check_and_advance() - Check an errseq_t and advance to current value.
15814ebc28eSMatthew Wilcox * @eseq: Pointer to value being checked and reported.
15914ebc28eSMatthew Wilcox * @since: Pointer to previously-sampled errseq_t to check against and advance.
16084cbadadSJeff Layton *
16114ebc28eSMatthew Wilcox * Grab the eseq value, and see whether it matches the value that @since
16284cbadadSJeff Layton * points to. If it does, then just return 0.
16384cbadadSJeff Layton *
16484cbadadSJeff Layton * If it doesn't, then the value has changed. Set the "seen" flag, and try to
16584cbadadSJeff Layton * swap it into place as the new eseq value. Then, set that value as the new
16684cbadadSJeff Layton * "since" value, and return whatever the error portion is set to.
16784cbadadSJeff Layton *
16884cbadadSJeff Layton * Note that no locking is provided here for concurrent updates to the "since"
16984cbadadSJeff Layton * value. The caller must provide that if necessary. Because of this, callers
17084cbadadSJeff Layton * may want to do a lockless errseq_check before taking the lock and calling
17184cbadadSJeff Layton * this.
17214ebc28eSMatthew Wilcox *
17314ebc28eSMatthew Wilcox * Return: Negative errno if one has been stored, or 0 if no new error has
17414ebc28eSMatthew Wilcox * occurred.
17584cbadadSJeff Layton */
errseq_check_and_advance(errseq_t * eseq,errseq_t * since)17684cbadadSJeff Layton int errseq_check_and_advance(errseq_t *eseq, errseq_t *since)
17784cbadadSJeff Layton {
17884cbadadSJeff Layton int err = 0;
17984cbadadSJeff Layton errseq_t old, new;
18084cbadadSJeff Layton
18184cbadadSJeff Layton /*
18284cbadadSJeff Layton * Most callers will want to use the inline wrapper to check this,
18384cbadadSJeff Layton * so that the common case of no error is handled without needing
18484cbadadSJeff Layton * to take the lock that protects the "since" value.
18584cbadadSJeff Layton */
18684cbadadSJeff Layton old = READ_ONCE(*eseq);
18784cbadadSJeff Layton if (old != *since) {
18884cbadadSJeff Layton /*
18984cbadadSJeff Layton * Set the flag and try to swap it into place if it has
19084cbadadSJeff Layton * changed.
19184cbadadSJeff Layton *
19284cbadadSJeff Layton * We don't care about the outcome of the swap here. If the
19384cbadadSJeff Layton * swap doesn't occur, then it has either been updated by a
19484cbadadSJeff Layton * writer who is altering the value in some way (updating
19584cbadadSJeff Layton * counter or resetting the error), or another reader who is
19684cbadadSJeff Layton * just setting the "seen" flag. Either outcome is OK, and we
19784cbadadSJeff Layton * can advance "since" and return an error based on what we
19884cbadadSJeff Layton * have.
19984cbadadSJeff Layton */
20084cbadadSJeff Layton new = old | ERRSEQ_SEEN;
20184cbadadSJeff Layton if (new != old)
20284cbadadSJeff Layton cmpxchg(eseq, old, new);
20384cbadadSJeff Layton *since = new;
204*3eff6a3eSZijun Hu err = -(new & ERRNO_MASK);
20584cbadadSJeff Layton }
20684cbadadSJeff Layton return err;
20784cbadadSJeff Layton }
20884cbadadSJeff Layton EXPORT_SYMBOL(errseq_check_and_advance);
209