xref: /linux/lib/errseq.c (revision 3acdfd28)
184cbadadSJeff Layton #include <linux/err.h>
284cbadadSJeff Layton #include <linux/bug.h>
384cbadadSJeff Layton #include <linux/atomic.h>
484cbadadSJeff Layton #include <linux/errseq.h>
584cbadadSJeff Layton 
684cbadadSJeff Layton /*
784cbadadSJeff Layton  * An errseq_t is a way of recording errors in one place, and allowing any
884cbadadSJeff Layton  * number of "subscribers" to tell whether it has changed since a previous
984cbadadSJeff Layton  * point where it was sampled.
1084cbadadSJeff Layton  *
1184cbadadSJeff Layton  * It's implemented as an unsigned 32-bit value. The low order bits are
1284cbadadSJeff Layton  * designated to hold an error code (between 0 and -MAX_ERRNO). The upper bits
1384cbadadSJeff Layton  * are used as a counter. This is done with atomics instead of locking so that
1484cbadadSJeff Layton  * these functions can be called from any context.
1584cbadadSJeff Layton  *
1684cbadadSJeff Layton  * The general idea is for consumers to sample an errseq_t value. That value
1784cbadadSJeff Layton  * can later be used to tell whether any new errors have occurred since that
1884cbadadSJeff Layton  * sampling was done.
1984cbadadSJeff Layton  *
2084cbadadSJeff Layton  * Note that there is a risk of collisions if new errors are being recorded
2184cbadadSJeff Layton  * frequently, since we have so few bits to use as a counter.
2284cbadadSJeff Layton  *
2384cbadadSJeff Layton  * To mitigate this, one bit is used as a flag to tell whether the value has
2484cbadadSJeff Layton  * been sampled since a new value was recorded. That allows us to avoid bumping
2584cbadadSJeff Layton  * the counter if no one has sampled it since the last time an error was
2684cbadadSJeff Layton  * recorded.
2784cbadadSJeff Layton  *
2884cbadadSJeff Layton  * A new errseq_t should always be zeroed out.  A errseq_t value of all zeroes
2984cbadadSJeff Layton  * is the special (but common) case where there has never been an error. An all
3084cbadadSJeff Layton  * zero value thus serves as the "epoch" if one wishes to know whether there
3184cbadadSJeff Layton  * has ever been an error set since it was first initialized.
3284cbadadSJeff Layton  */
3384cbadadSJeff Layton 
3484cbadadSJeff Layton /* The low bits are designated for error code (max of MAX_ERRNO) */
3584cbadadSJeff Layton #define ERRSEQ_SHIFT		ilog2(MAX_ERRNO + 1)
3684cbadadSJeff Layton 
3784cbadadSJeff Layton /* This bit is used as a flag to indicate whether the value has been seen */
3884cbadadSJeff Layton #define ERRSEQ_SEEN		(1 << ERRSEQ_SHIFT)
3984cbadadSJeff Layton 
4084cbadadSJeff Layton /* The lowest bit of the counter */
4184cbadadSJeff Layton #define ERRSEQ_CTR_INC		(1 << (ERRSEQ_SHIFT + 1))
4284cbadadSJeff Layton 
4384cbadadSJeff Layton /**
44*3acdfd28SJeff Layton  * errseq_set - set a errseq_t for later reporting
4584cbadadSJeff Layton  * @eseq: errseq_t field that should be set
46*3acdfd28SJeff Layton  * @err: error to set (must be between -1 and -MAX_ERRNO)
4784cbadadSJeff Layton  *
4884cbadadSJeff Layton  * This function sets the error in *eseq, and increments the sequence counter
4984cbadadSJeff Layton  * if the last sequence was sampled at some point in the past.
5084cbadadSJeff Layton  *
5184cbadadSJeff Layton  * Any error set will always overwrite an existing error.
5284cbadadSJeff Layton  *
53*3acdfd28SJeff Layton  * We do return the latest value here, primarily for debugging purposes. The
54*3acdfd28SJeff Layton  * return value should not be used as a previously sampled value in later calls
55*3acdfd28SJeff Layton  * as it will not have the SEEN flag set.
5684cbadadSJeff Layton  */
57*3acdfd28SJeff Layton errseq_t errseq_set(errseq_t *eseq, int err)
5884cbadadSJeff Layton {
5984cbadadSJeff Layton 	errseq_t cur, old;
6084cbadadSJeff Layton 
6184cbadadSJeff Layton 	/* MAX_ERRNO must be able to serve as a mask */
6284cbadadSJeff Layton 	BUILD_BUG_ON_NOT_POWER_OF_2(MAX_ERRNO + 1);
6384cbadadSJeff Layton 
6484cbadadSJeff Layton 	/*
6584cbadadSJeff Layton 	 * Ensure the error code actually fits where we want it to go. If it
6684cbadadSJeff Layton 	 * doesn't then just throw a warning and don't record anything. We
6784cbadadSJeff Layton 	 * also don't accept zero here as that would effectively clear a
6884cbadadSJeff Layton 	 * previous error.
6984cbadadSJeff Layton 	 */
7084cbadadSJeff Layton 	old = READ_ONCE(*eseq);
7184cbadadSJeff Layton 
7284cbadadSJeff Layton 	if (WARN(unlikely(err == 0 || (unsigned int)-err > MAX_ERRNO),
7384cbadadSJeff Layton 				"err = %d\n", err))
7484cbadadSJeff Layton 		return old;
7584cbadadSJeff Layton 
7684cbadadSJeff Layton 	for (;;) {
7784cbadadSJeff Layton 		errseq_t new;
7884cbadadSJeff Layton 
7984cbadadSJeff Layton 		/* Clear out error bits and set new error */
8084cbadadSJeff Layton 		new = (old & ~(MAX_ERRNO|ERRSEQ_SEEN)) | -err;
8184cbadadSJeff Layton 
8284cbadadSJeff Layton 		/* Only increment if someone has looked at it */
8384cbadadSJeff Layton 		if (old & ERRSEQ_SEEN)
8484cbadadSJeff Layton 			new += ERRSEQ_CTR_INC;
8584cbadadSJeff Layton 
8684cbadadSJeff Layton 		/* If there would be no change, then call it done */
8784cbadadSJeff Layton 		if (new == old) {
8884cbadadSJeff Layton 			cur = new;
8984cbadadSJeff Layton 			break;
9084cbadadSJeff Layton 		}
9184cbadadSJeff Layton 
9284cbadadSJeff Layton 		/* Try to swap the new value into place */
9384cbadadSJeff Layton 		cur = cmpxchg(eseq, old, new);
9484cbadadSJeff Layton 
9584cbadadSJeff Layton 		/*
9684cbadadSJeff Layton 		 * Call it success if we did the swap or someone else beat us
9784cbadadSJeff Layton 		 * to it for the same value.
9884cbadadSJeff Layton 		 */
9984cbadadSJeff Layton 		if (likely(cur == old || cur == new))
10084cbadadSJeff Layton 			break;
10184cbadadSJeff Layton 
10284cbadadSJeff Layton 		/* Raced with an update, try again */
10384cbadadSJeff Layton 		old = cur;
10484cbadadSJeff Layton 	}
10584cbadadSJeff Layton 	return cur;
10684cbadadSJeff Layton }
107*3acdfd28SJeff Layton EXPORT_SYMBOL(errseq_set);
10884cbadadSJeff Layton 
10984cbadadSJeff Layton /**
11084cbadadSJeff Layton  * errseq_sample - grab current errseq_t value
11184cbadadSJeff Layton  * @eseq: pointer to errseq_t to be sampled
11284cbadadSJeff Layton  *
11384cbadadSJeff Layton  * This function allows callers to sample an errseq_t value, marking it as
11484cbadadSJeff Layton  * "seen" if required.
11584cbadadSJeff Layton  */
11684cbadadSJeff Layton errseq_t errseq_sample(errseq_t *eseq)
11784cbadadSJeff Layton {
11884cbadadSJeff Layton 	errseq_t old = READ_ONCE(*eseq);
11984cbadadSJeff Layton 	errseq_t new = old;
12084cbadadSJeff Layton 
12184cbadadSJeff Layton 	/*
12284cbadadSJeff Layton 	 * For the common case of no errors ever having been set, we can skip
12384cbadadSJeff Layton 	 * marking the SEEN bit. Once an error has been set, the value will
12484cbadadSJeff Layton 	 * never go back to zero.
12584cbadadSJeff Layton 	 */
12684cbadadSJeff Layton 	if (old != 0) {
12784cbadadSJeff Layton 		new |= ERRSEQ_SEEN;
12884cbadadSJeff Layton 		if (old != new)
12984cbadadSJeff Layton 			cmpxchg(eseq, old, new);
13084cbadadSJeff Layton 	}
13184cbadadSJeff Layton 	return new;
13284cbadadSJeff Layton }
13384cbadadSJeff Layton EXPORT_SYMBOL(errseq_sample);
13484cbadadSJeff Layton 
13584cbadadSJeff Layton /**
13684cbadadSJeff Layton  * errseq_check - has an error occurred since a particular sample point?
13784cbadadSJeff Layton  * @eseq: pointer to errseq_t value to be checked
13884cbadadSJeff Layton  * @since: previously-sampled errseq_t from which to check
13984cbadadSJeff Layton  *
14084cbadadSJeff Layton  * Grab the value that eseq points to, and see if it has changed "since"
14184cbadadSJeff Layton  * 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  *
14484cbadadSJeff Layton  * Returns the latest error set in the errseq_t or 0 if it hasn't changed.
14584cbadadSJeff Layton  */
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;
15284cbadadSJeff Layton 	return -(cur & MAX_ERRNO);
15384cbadadSJeff Layton }
15484cbadadSJeff Layton EXPORT_SYMBOL(errseq_check);
15584cbadadSJeff Layton 
15684cbadadSJeff Layton /**
15784cbadadSJeff Layton  * errseq_check_and_advance - check an errseq_t and advance to current value
15884cbadadSJeff Layton  * @eseq: pointer to value being checked and reported
15984cbadadSJeff Layton  * @since: pointer to previously-sampled errseq_t to check against and advance
16084cbadadSJeff Layton  *
16184cbadadSJeff Layton  * 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.
17284cbadadSJeff Layton  */
17384cbadadSJeff Layton int errseq_check_and_advance(errseq_t *eseq, errseq_t *since)
17484cbadadSJeff Layton {
17584cbadadSJeff Layton 	int err = 0;
17684cbadadSJeff Layton 	errseq_t old, new;
17784cbadadSJeff Layton 
17884cbadadSJeff Layton 	/*
17984cbadadSJeff Layton 	 * Most callers will want to use the inline wrapper to check this,
18084cbadadSJeff Layton 	 * so that the common case of no error is handled without needing
18184cbadadSJeff Layton 	 * to take the lock that protects the "since" value.
18284cbadadSJeff Layton 	 */
18384cbadadSJeff Layton 	old = READ_ONCE(*eseq);
18484cbadadSJeff Layton 	if (old != *since) {
18584cbadadSJeff Layton 		/*
18684cbadadSJeff Layton 		 * Set the flag and try to swap it into place if it has
18784cbadadSJeff Layton 		 * changed.
18884cbadadSJeff Layton 		 *
18984cbadadSJeff Layton 		 * We don't care about the outcome of the swap here. If the
19084cbadadSJeff Layton 		 * swap doesn't occur, then it has either been updated by a
19184cbadadSJeff Layton 		 * writer who is altering the value in some way (updating
19284cbadadSJeff Layton 		 * counter or resetting the error), or another reader who is
19384cbadadSJeff Layton 		 * just setting the "seen" flag. Either outcome is OK, and we
19484cbadadSJeff Layton 		 * can advance "since" and return an error based on what we
19584cbadadSJeff Layton 		 * have.
19684cbadadSJeff Layton 		 */
19784cbadadSJeff Layton 		new = old | ERRSEQ_SEEN;
19884cbadadSJeff Layton 		if (new != old)
19984cbadadSJeff Layton 			cmpxchg(eseq, old, new);
20084cbadadSJeff Layton 		*since = new;
20184cbadadSJeff Layton 		err = -(new & MAX_ERRNO);
20284cbadadSJeff Layton 	}
20384cbadadSJeff Layton 	return err;
20484cbadadSJeff Layton }
20584cbadadSJeff Layton EXPORT_SYMBOL(errseq_check_and_advance);
206