xref: /linux/lib/errseq.c (revision aa6159ab)
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>
6*aa6159abSAndy 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) */
3784cbadadSJeff Layton #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 
4284cbadadSJeff Layton /* The lowest bit of the counter */
4384cbadadSJeff Layton #define ERRSEQ_CTR_INC		(1 << (ERRSEQ_SHIFT + 1))
4484cbadadSJeff Layton 
4584cbadadSJeff Layton /**
463acdfd28SJeff Layton  * errseq_set - set a errseq_t for later reporting
4784cbadadSJeff Layton  * @eseq: errseq_t field that should be set
483acdfd28SJeff Layton  * @err: error to set (must be between -1 and -MAX_ERRNO)
4984cbadadSJeff Layton  *
5014ebc28eSMatthew Wilcox  * This function sets the error in @eseq, and increments the sequence counter
5184cbadadSJeff Layton  * if the last sequence was sampled at some point in the past.
5284cbadadSJeff Layton  *
5384cbadadSJeff Layton  * Any error set will always overwrite an existing error.
5484cbadadSJeff Layton  *
5514ebc28eSMatthew Wilcox  * Return: The previous value, primarily for debugging purposes. The
5614ebc28eSMatthew Wilcox  * return value should not be used as a previously sampled value in later
5714ebc28eSMatthew Wilcox  * calls as it will not have the SEEN flag set.
5884cbadadSJeff Layton  */
errseq_set(errseq_t * eseq,int err)593acdfd28SJeff Layton errseq_t errseq_set(errseq_t *eseq, int err)
6084cbadadSJeff Layton {
6184cbadadSJeff Layton 	errseq_t cur, old;
6284cbadadSJeff Layton 
6384cbadadSJeff Layton 	/* MAX_ERRNO must be able to serve as a mask */
6484cbadadSJeff Layton 	BUILD_BUG_ON_NOT_POWER_OF_2(MAX_ERRNO + 1);
6584cbadadSJeff Layton 
6684cbadadSJeff Layton 	/*
6784cbadadSJeff Layton 	 * Ensure the error code actually fits where we want it to go. If it
6884cbadadSJeff Layton 	 * doesn't then just throw a warning and don't record anything. We
6984cbadadSJeff Layton 	 * also don't accept zero here as that would effectively clear a
7084cbadadSJeff Layton 	 * previous error.
7184cbadadSJeff Layton 	 */
7284cbadadSJeff Layton 	old = READ_ONCE(*eseq);
7384cbadadSJeff Layton 
7484cbadadSJeff Layton 	if (WARN(unlikely(err == 0 || (unsigned int)-err > MAX_ERRNO),
7584cbadadSJeff Layton 				"err = %d\n", err))
7684cbadadSJeff Layton 		return old;
7784cbadadSJeff Layton 
7884cbadadSJeff Layton 	for (;;) {
7984cbadadSJeff Layton 		errseq_t new;
8084cbadadSJeff Layton 
8184cbadadSJeff Layton 		/* Clear out error bits and set new error */
8284cbadadSJeff Layton 		new = (old & ~(MAX_ERRNO|ERRSEQ_SEEN)) | -err;
8384cbadadSJeff Layton 
8484cbadadSJeff Layton 		/* Only increment if someone has looked at it */
8584cbadadSJeff Layton 		if (old & ERRSEQ_SEEN)
8684cbadadSJeff Layton 			new += ERRSEQ_CTR_INC;
8784cbadadSJeff Layton 
8884cbadadSJeff Layton 		/* If there would be no change, then call it done */
8984cbadadSJeff Layton 		if (new == old) {
9084cbadadSJeff Layton 			cur = new;
9184cbadadSJeff Layton 			break;
9284cbadadSJeff Layton 		}
9384cbadadSJeff Layton 
9484cbadadSJeff Layton 		/* Try to swap the new value into place */
9584cbadadSJeff Layton 		cur = cmpxchg(eseq, old, new);
9684cbadadSJeff Layton 
9784cbadadSJeff Layton 		/*
9884cbadadSJeff Layton 		 * Call it success if we did the swap or someone else beat us
9984cbadadSJeff Layton 		 * to it for the same value.
10084cbadadSJeff Layton 		 */
10184cbadadSJeff Layton 		if (likely(cur == old || cur == new))
10284cbadadSJeff Layton 			break;
10384cbadadSJeff Layton 
10484cbadadSJeff Layton 		/* Raced with an update, try again */
10584cbadadSJeff Layton 		old = cur;
10684cbadadSJeff Layton 	}
10784cbadadSJeff Layton 	return cur;
10884cbadadSJeff Layton }
1093acdfd28SJeff Layton EXPORT_SYMBOL(errseq_set);
11084cbadadSJeff Layton 
11184cbadadSJeff Layton /**
11214ebc28eSMatthew Wilcox  * errseq_sample() - Grab current errseq_t value.
11314ebc28eSMatthew Wilcox  * @eseq: Pointer to errseq_t to be sampled.
11484cbadadSJeff Layton  *
115b4678df1SMatthew Wilcox  * This function allows callers to initialise their errseq_t variable.
116b4678df1SMatthew Wilcox  * If the error has been "seen", new callers will not see an old error.
117b4678df1SMatthew Wilcox  * If there is an unseen error in @eseq, the caller of this function will
118b4678df1SMatthew Wilcox  * see it the next time it checks for an error.
11914ebc28eSMatthew Wilcox  *
120b4678df1SMatthew Wilcox  * Context: Any context.
12114ebc28eSMatthew Wilcox  * Return: The current errseq value.
12284cbadadSJeff Layton  */
errseq_sample(errseq_t * eseq)12384cbadadSJeff Layton errseq_t errseq_sample(errseq_t *eseq)
12484cbadadSJeff Layton {
12584cbadadSJeff Layton 	errseq_t old = READ_ONCE(*eseq);
12684cbadadSJeff Layton 
127b4678df1SMatthew Wilcox 	/* If nobody has seen this error yet, then we can be the first. */
128b4678df1SMatthew Wilcox 	if (!(old & ERRSEQ_SEEN))
129b4678df1SMatthew Wilcox 		old = 0;
130b4678df1SMatthew Wilcox 	return old;
13184cbadadSJeff Layton }
13284cbadadSJeff Layton EXPORT_SYMBOL(errseq_sample);
13384cbadadSJeff Layton 
13484cbadadSJeff Layton /**
13514ebc28eSMatthew Wilcox  * errseq_check() - Has an error occurred since a particular sample point?
13614ebc28eSMatthew Wilcox  * @eseq: Pointer to errseq_t value to be checked.
13714ebc28eSMatthew Wilcox  * @since: Previously-sampled errseq_t from which to check.
13884cbadadSJeff Layton  *
13914ebc28eSMatthew Wilcox  * Grab the value that eseq points to, and see if it has changed @since
14014ebc28eSMatthew Wilcox  * the given value was sampled. The @since value is not advanced, so there
14184cbadadSJeff Layton  * is no need to mark the value as seen.
14284cbadadSJeff Layton  *
14314ebc28eSMatthew Wilcox  * Return: The latest error set in the errseq_t or 0 if it hasn't changed.
14484cbadadSJeff Layton  */
errseq_check(errseq_t * eseq,errseq_t since)14584cbadadSJeff Layton int errseq_check(errseq_t *eseq, errseq_t since)
14684cbadadSJeff Layton {
14784cbadadSJeff Layton 	errseq_t cur = READ_ONCE(*eseq);
14884cbadadSJeff Layton 
14984cbadadSJeff Layton 	if (likely(cur == since))
15084cbadadSJeff Layton 		return 0;
15184cbadadSJeff Layton 	return -(cur & MAX_ERRNO);
15284cbadadSJeff Layton }
15384cbadadSJeff Layton EXPORT_SYMBOL(errseq_check);
15484cbadadSJeff Layton 
15584cbadadSJeff Layton /**
15614ebc28eSMatthew Wilcox  * errseq_check_and_advance() - Check an errseq_t and advance to current value.
15714ebc28eSMatthew Wilcox  * @eseq: Pointer to value being checked and reported.
15814ebc28eSMatthew Wilcox  * @since: Pointer to previously-sampled errseq_t to check against and advance.
15984cbadadSJeff Layton  *
16014ebc28eSMatthew Wilcox  * Grab the eseq value, and see whether it matches the value that @since
16184cbadadSJeff Layton  * points to. If it does, then just return 0.
16284cbadadSJeff Layton  *
16384cbadadSJeff Layton  * If it doesn't, then the value has changed. Set the "seen" flag, and try to
16484cbadadSJeff Layton  * swap it into place as the new eseq value. Then, set that value as the new
16584cbadadSJeff Layton  * "since" value, and return whatever the error portion is set to.
16684cbadadSJeff Layton  *
16784cbadadSJeff Layton  * Note that no locking is provided here for concurrent updates to the "since"
16884cbadadSJeff Layton  * value. The caller must provide that if necessary. Because of this, callers
16984cbadadSJeff Layton  * may want to do a lockless errseq_check before taking the lock and calling
17084cbadadSJeff Layton  * this.
17114ebc28eSMatthew Wilcox  *
17214ebc28eSMatthew Wilcox  * Return: Negative errno if one has been stored, or 0 if no new error has
17314ebc28eSMatthew Wilcox  * occurred.
17484cbadadSJeff Layton  */
errseq_check_and_advance(errseq_t * eseq,errseq_t * since)17584cbadadSJeff Layton int errseq_check_and_advance(errseq_t *eseq, errseq_t *since)
17684cbadadSJeff Layton {
17784cbadadSJeff Layton 	int err = 0;
17884cbadadSJeff Layton 	errseq_t old, new;
17984cbadadSJeff Layton 
18084cbadadSJeff Layton 	/*
18184cbadadSJeff Layton 	 * Most callers will want to use the inline wrapper to check this,
18284cbadadSJeff Layton 	 * so that the common case of no error is handled without needing
18384cbadadSJeff Layton 	 * to take the lock that protects the "since" value.
18484cbadadSJeff Layton 	 */
18584cbadadSJeff Layton 	old = READ_ONCE(*eseq);
18684cbadadSJeff Layton 	if (old != *since) {
18784cbadadSJeff Layton 		/*
18884cbadadSJeff Layton 		 * Set the flag and try to swap it into place if it has
18984cbadadSJeff Layton 		 * changed.
19084cbadadSJeff Layton 		 *
19184cbadadSJeff Layton 		 * We don't care about the outcome of the swap here. If the
19284cbadadSJeff Layton 		 * swap doesn't occur, then it has either been updated by a
19384cbadadSJeff Layton 		 * writer who is altering the value in some way (updating
19484cbadadSJeff Layton 		 * counter or resetting the error), or another reader who is
19584cbadadSJeff Layton 		 * just setting the "seen" flag. Either outcome is OK, and we
19684cbadadSJeff Layton 		 * can advance "since" and return an error based on what we
19784cbadadSJeff Layton 		 * have.
19884cbadadSJeff Layton 		 */
19984cbadadSJeff Layton 		new = old | ERRSEQ_SEEN;
20084cbadadSJeff Layton 		if (new != old)
20184cbadadSJeff Layton 			cmpxchg(eseq, old, new);
20284cbadadSJeff Layton 		*since = new;
20384cbadadSJeff Layton 		err = -(new & MAX_ERRNO);
20484cbadadSJeff Layton 	}
20584cbadadSJeff Layton 	return err;
20684cbadadSJeff Layton }
20784cbadadSJeff Layton EXPORT_SYMBOL(errseq_check_and_advance);
208