162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci#include <linux/err.h>
362306a36Sopenharmony_ci#include <linux/bug.h>
462306a36Sopenharmony_ci#include <linux/atomic.h>
562306a36Sopenharmony_ci#include <linux/errseq.h>
662306a36Sopenharmony_ci#include <linux/log2.h>
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci/*
962306a36Sopenharmony_ci * An errseq_t is a way of recording errors in one place, and allowing any
1062306a36Sopenharmony_ci * number of "subscribers" to tell whether it has changed since a previous
1162306a36Sopenharmony_ci * point where it was sampled.
1262306a36Sopenharmony_ci *
1362306a36Sopenharmony_ci * It's implemented as an unsigned 32-bit value. The low order bits are
1462306a36Sopenharmony_ci * designated to hold an error code (between 0 and -MAX_ERRNO). The upper bits
1562306a36Sopenharmony_ci * are used as a counter. This is done with atomics instead of locking so that
1662306a36Sopenharmony_ci * these functions can be called from any context.
1762306a36Sopenharmony_ci *
1862306a36Sopenharmony_ci * The general idea is for consumers to sample an errseq_t value. That value
1962306a36Sopenharmony_ci * can later be used to tell whether any new errors have occurred since that
2062306a36Sopenharmony_ci * sampling was done.
2162306a36Sopenharmony_ci *
2262306a36Sopenharmony_ci * Note that there is a risk of collisions if new errors are being recorded
2362306a36Sopenharmony_ci * frequently, since we have so few bits to use as a counter.
2462306a36Sopenharmony_ci *
2562306a36Sopenharmony_ci * To mitigate this, one bit is used as a flag to tell whether the value has
2662306a36Sopenharmony_ci * been sampled since a new value was recorded. That allows us to avoid bumping
2762306a36Sopenharmony_ci * the counter if no one has sampled it since the last time an error was
2862306a36Sopenharmony_ci * recorded.
2962306a36Sopenharmony_ci *
3062306a36Sopenharmony_ci * A new errseq_t should always be zeroed out.  A errseq_t value of all zeroes
3162306a36Sopenharmony_ci * is the special (but common) case where there has never been an error. An all
3262306a36Sopenharmony_ci * zero value thus serves as the "epoch" if one wishes to know whether there
3362306a36Sopenharmony_ci * has ever been an error set since it was first initialized.
3462306a36Sopenharmony_ci */
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci/* The low bits are designated for error code (max of MAX_ERRNO) */
3762306a36Sopenharmony_ci#define ERRSEQ_SHIFT		ilog2(MAX_ERRNO + 1)
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci/* This bit is used as a flag to indicate whether the value has been seen */
4062306a36Sopenharmony_ci#define ERRSEQ_SEEN		(1 << ERRSEQ_SHIFT)
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci/* The lowest bit of the counter */
4362306a36Sopenharmony_ci#define ERRSEQ_CTR_INC		(1 << (ERRSEQ_SHIFT + 1))
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci/**
4662306a36Sopenharmony_ci * errseq_set - set a errseq_t for later reporting
4762306a36Sopenharmony_ci * @eseq: errseq_t field that should be set
4862306a36Sopenharmony_ci * @err: error to set (must be between -1 and -MAX_ERRNO)
4962306a36Sopenharmony_ci *
5062306a36Sopenharmony_ci * This function sets the error in @eseq, and increments the sequence counter
5162306a36Sopenharmony_ci * if the last sequence was sampled at some point in the past.
5262306a36Sopenharmony_ci *
5362306a36Sopenharmony_ci * Any error set will always overwrite an existing error.
5462306a36Sopenharmony_ci *
5562306a36Sopenharmony_ci * Return: The previous value, primarily for debugging purposes. The
5662306a36Sopenharmony_ci * return value should not be used as a previously sampled value in later
5762306a36Sopenharmony_ci * calls as it will not have the SEEN flag set.
5862306a36Sopenharmony_ci */
5962306a36Sopenharmony_cierrseq_t errseq_set(errseq_t *eseq, int err)
6062306a36Sopenharmony_ci{
6162306a36Sopenharmony_ci	errseq_t cur, old;
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci	/* MAX_ERRNO must be able to serve as a mask */
6462306a36Sopenharmony_ci	BUILD_BUG_ON_NOT_POWER_OF_2(MAX_ERRNO + 1);
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci	/*
6762306a36Sopenharmony_ci	 * Ensure the error code actually fits where we want it to go. If it
6862306a36Sopenharmony_ci	 * doesn't then just throw a warning and don't record anything. We
6962306a36Sopenharmony_ci	 * also don't accept zero here as that would effectively clear a
7062306a36Sopenharmony_ci	 * previous error.
7162306a36Sopenharmony_ci	 */
7262306a36Sopenharmony_ci	old = READ_ONCE(*eseq);
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci	if (WARN(unlikely(err == 0 || (unsigned int)-err > MAX_ERRNO),
7562306a36Sopenharmony_ci				"err = %d\n", err))
7662306a36Sopenharmony_ci		return old;
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci	for (;;) {
7962306a36Sopenharmony_ci		errseq_t new;
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci		/* Clear out error bits and set new error */
8262306a36Sopenharmony_ci		new = (old & ~(MAX_ERRNO|ERRSEQ_SEEN)) | -err;
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci		/* Only increment if someone has looked at it */
8562306a36Sopenharmony_ci		if (old & ERRSEQ_SEEN)
8662306a36Sopenharmony_ci			new += ERRSEQ_CTR_INC;
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci		/* If there would be no change, then call it done */
8962306a36Sopenharmony_ci		if (new == old) {
9062306a36Sopenharmony_ci			cur = new;
9162306a36Sopenharmony_ci			break;
9262306a36Sopenharmony_ci		}
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci		/* Try to swap the new value into place */
9562306a36Sopenharmony_ci		cur = cmpxchg(eseq, old, new);
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci		/*
9862306a36Sopenharmony_ci		 * Call it success if we did the swap or someone else beat us
9962306a36Sopenharmony_ci		 * to it for the same value.
10062306a36Sopenharmony_ci		 */
10162306a36Sopenharmony_ci		if (likely(cur == old || cur == new))
10262306a36Sopenharmony_ci			break;
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci		/* Raced with an update, try again */
10562306a36Sopenharmony_ci		old = cur;
10662306a36Sopenharmony_ci	}
10762306a36Sopenharmony_ci	return cur;
10862306a36Sopenharmony_ci}
10962306a36Sopenharmony_ciEXPORT_SYMBOL(errseq_set);
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci/**
11262306a36Sopenharmony_ci * errseq_sample() - Grab current errseq_t value.
11362306a36Sopenharmony_ci * @eseq: Pointer to errseq_t to be sampled.
11462306a36Sopenharmony_ci *
11562306a36Sopenharmony_ci * This function allows callers to initialise their errseq_t variable.
11662306a36Sopenharmony_ci * If the error has been "seen", new callers will not see an old error.
11762306a36Sopenharmony_ci * If there is an unseen error in @eseq, the caller of this function will
11862306a36Sopenharmony_ci * see it the next time it checks for an error.
11962306a36Sopenharmony_ci *
12062306a36Sopenharmony_ci * Context: Any context.
12162306a36Sopenharmony_ci * Return: The current errseq value.
12262306a36Sopenharmony_ci */
12362306a36Sopenharmony_cierrseq_t errseq_sample(errseq_t *eseq)
12462306a36Sopenharmony_ci{
12562306a36Sopenharmony_ci	errseq_t old = READ_ONCE(*eseq);
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci	/* If nobody has seen this error yet, then we can be the first. */
12862306a36Sopenharmony_ci	if (!(old & ERRSEQ_SEEN))
12962306a36Sopenharmony_ci		old = 0;
13062306a36Sopenharmony_ci	return old;
13162306a36Sopenharmony_ci}
13262306a36Sopenharmony_ciEXPORT_SYMBOL(errseq_sample);
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci/**
13562306a36Sopenharmony_ci * errseq_check() - Has an error occurred since a particular sample point?
13662306a36Sopenharmony_ci * @eseq: Pointer to errseq_t value to be checked.
13762306a36Sopenharmony_ci * @since: Previously-sampled errseq_t from which to check.
13862306a36Sopenharmony_ci *
13962306a36Sopenharmony_ci * Grab the value that eseq points to, and see if it has changed @since
14062306a36Sopenharmony_ci * the given value was sampled. The @since value is not advanced, so there
14162306a36Sopenharmony_ci * is no need to mark the value as seen.
14262306a36Sopenharmony_ci *
14362306a36Sopenharmony_ci * Return: The latest error set in the errseq_t or 0 if it hasn't changed.
14462306a36Sopenharmony_ci */
14562306a36Sopenharmony_ciint errseq_check(errseq_t *eseq, errseq_t since)
14662306a36Sopenharmony_ci{
14762306a36Sopenharmony_ci	errseq_t cur = READ_ONCE(*eseq);
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci	if (likely(cur == since))
15062306a36Sopenharmony_ci		return 0;
15162306a36Sopenharmony_ci	return -(cur & MAX_ERRNO);
15262306a36Sopenharmony_ci}
15362306a36Sopenharmony_ciEXPORT_SYMBOL(errseq_check);
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci/**
15662306a36Sopenharmony_ci * errseq_check_and_advance() - Check an errseq_t and advance to current value.
15762306a36Sopenharmony_ci * @eseq: Pointer to value being checked and reported.
15862306a36Sopenharmony_ci * @since: Pointer to previously-sampled errseq_t to check against and advance.
15962306a36Sopenharmony_ci *
16062306a36Sopenharmony_ci * Grab the eseq value, and see whether it matches the value that @since
16162306a36Sopenharmony_ci * points to. If it does, then just return 0.
16262306a36Sopenharmony_ci *
16362306a36Sopenharmony_ci * If it doesn't, then the value has changed. Set the "seen" flag, and try to
16462306a36Sopenharmony_ci * swap it into place as the new eseq value. Then, set that value as the new
16562306a36Sopenharmony_ci * "since" value, and return whatever the error portion is set to.
16662306a36Sopenharmony_ci *
16762306a36Sopenharmony_ci * Note that no locking is provided here for concurrent updates to the "since"
16862306a36Sopenharmony_ci * value. The caller must provide that if necessary. Because of this, callers
16962306a36Sopenharmony_ci * may want to do a lockless errseq_check before taking the lock and calling
17062306a36Sopenharmony_ci * this.
17162306a36Sopenharmony_ci *
17262306a36Sopenharmony_ci * Return: Negative errno if one has been stored, or 0 if no new error has
17362306a36Sopenharmony_ci * occurred.
17462306a36Sopenharmony_ci */
17562306a36Sopenharmony_ciint errseq_check_and_advance(errseq_t *eseq, errseq_t *since)
17662306a36Sopenharmony_ci{
17762306a36Sopenharmony_ci	int err = 0;
17862306a36Sopenharmony_ci	errseq_t old, new;
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci	/*
18162306a36Sopenharmony_ci	 * Most callers will want to use the inline wrapper to check this,
18262306a36Sopenharmony_ci	 * so that the common case of no error is handled without needing
18362306a36Sopenharmony_ci	 * to take the lock that protects the "since" value.
18462306a36Sopenharmony_ci	 */
18562306a36Sopenharmony_ci	old = READ_ONCE(*eseq);
18662306a36Sopenharmony_ci	if (old != *since) {
18762306a36Sopenharmony_ci		/*
18862306a36Sopenharmony_ci		 * Set the flag and try to swap it into place if it has
18962306a36Sopenharmony_ci		 * changed.
19062306a36Sopenharmony_ci		 *
19162306a36Sopenharmony_ci		 * We don't care about the outcome of the swap here. If the
19262306a36Sopenharmony_ci		 * swap doesn't occur, then it has either been updated by a
19362306a36Sopenharmony_ci		 * writer who is altering the value in some way (updating
19462306a36Sopenharmony_ci		 * counter or resetting the error), or another reader who is
19562306a36Sopenharmony_ci		 * just setting the "seen" flag. Either outcome is OK, and we
19662306a36Sopenharmony_ci		 * can advance "since" and return an error based on what we
19762306a36Sopenharmony_ci		 * have.
19862306a36Sopenharmony_ci		 */
19962306a36Sopenharmony_ci		new = old | ERRSEQ_SEEN;
20062306a36Sopenharmony_ci		if (new != old)
20162306a36Sopenharmony_ci			cmpxchg(eseq, old, new);
20262306a36Sopenharmony_ci		*since = new;
20362306a36Sopenharmony_ci		err = -(new & MAX_ERRNO);
20462306a36Sopenharmony_ci	}
20562306a36Sopenharmony_ci	return err;
20662306a36Sopenharmony_ci}
20762306a36Sopenharmony_ciEXPORT_SYMBOL(errseq_check_and_advance);
208