18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci#include <linux/err.h>
38c2ecf20Sopenharmony_ci#include <linux/bug.h>
48c2ecf20Sopenharmony_ci#include <linux/atomic.h>
58c2ecf20Sopenharmony_ci#include <linux/errseq.h>
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci/*
88c2ecf20Sopenharmony_ci * An errseq_t is a way of recording errors in one place, and allowing any
98c2ecf20Sopenharmony_ci * number of "subscribers" to tell whether it has changed since a previous
108c2ecf20Sopenharmony_ci * point where it was sampled.
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * It's implemented as an unsigned 32-bit value. The low order bits are
138c2ecf20Sopenharmony_ci * designated to hold an error code (between 0 and -MAX_ERRNO). The upper bits
148c2ecf20Sopenharmony_ci * are used as a counter. This is done with atomics instead of locking so that
158c2ecf20Sopenharmony_ci * these functions can be called from any context.
168c2ecf20Sopenharmony_ci *
178c2ecf20Sopenharmony_ci * The general idea is for consumers to sample an errseq_t value. That value
188c2ecf20Sopenharmony_ci * can later be used to tell whether any new errors have occurred since that
198c2ecf20Sopenharmony_ci * sampling was done.
208c2ecf20Sopenharmony_ci *
218c2ecf20Sopenharmony_ci * Note that there is a risk of collisions if new errors are being recorded
228c2ecf20Sopenharmony_ci * frequently, since we have so few bits to use as a counter.
238c2ecf20Sopenharmony_ci *
248c2ecf20Sopenharmony_ci * To mitigate this, one bit is used as a flag to tell whether the value has
258c2ecf20Sopenharmony_ci * been sampled since a new value was recorded. That allows us to avoid bumping
268c2ecf20Sopenharmony_ci * the counter if no one has sampled it since the last time an error was
278c2ecf20Sopenharmony_ci * recorded.
288c2ecf20Sopenharmony_ci *
298c2ecf20Sopenharmony_ci * A new errseq_t should always be zeroed out.  A errseq_t value of all zeroes
308c2ecf20Sopenharmony_ci * is the special (but common) case where there has never been an error. An all
318c2ecf20Sopenharmony_ci * zero value thus serves as the "epoch" if one wishes to know whether there
328c2ecf20Sopenharmony_ci * has ever been an error set since it was first initialized.
338c2ecf20Sopenharmony_ci */
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci/* The low bits are designated for error code (max of MAX_ERRNO) */
368c2ecf20Sopenharmony_ci#define ERRSEQ_SHIFT		ilog2(MAX_ERRNO + 1)
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci/* This bit is used as a flag to indicate whether the value has been seen */
398c2ecf20Sopenharmony_ci#define ERRSEQ_SEEN		(1 << ERRSEQ_SHIFT)
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci/* The lowest bit of the counter */
428c2ecf20Sopenharmony_ci#define ERRSEQ_CTR_INC		(1 << (ERRSEQ_SHIFT + 1))
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci/**
458c2ecf20Sopenharmony_ci * errseq_set - set a errseq_t for later reporting
468c2ecf20Sopenharmony_ci * @eseq: errseq_t field that should be set
478c2ecf20Sopenharmony_ci * @err: error to set (must be between -1 and -MAX_ERRNO)
488c2ecf20Sopenharmony_ci *
498c2ecf20Sopenharmony_ci * This function sets the error in @eseq, and increments the sequence counter
508c2ecf20Sopenharmony_ci * if the last sequence was sampled at some point in the past.
518c2ecf20Sopenharmony_ci *
528c2ecf20Sopenharmony_ci * Any error set will always overwrite an existing error.
538c2ecf20Sopenharmony_ci *
548c2ecf20Sopenharmony_ci * Return: The previous value, primarily for debugging purposes. The
558c2ecf20Sopenharmony_ci * return value should not be used as a previously sampled value in later
568c2ecf20Sopenharmony_ci * calls as it will not have the SEEN flag set.
578c2ecf20Sopenharmony_ci */
588c2ecf20Sopenharmony_cierrseq_t errseq_set(errseq_t *eseq, int err)
598c2ecf20Sopenharmony_ci{
608c2ecf20Sopenharmony_ci	errseq_t cur, old;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	/* MAX_ERRNO must be able to serve as a mask */
638c2ecf20Sopenharmony_ci	BUILD_BUG_ON_NOT_POWER_OF_2(MAX_ERRNO + 1);
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	/*
668c2ecf20Sopenharmony_ci	 * Ensure the error code actually fits where we want it to go. If it
678c2ecf20Sopenharmony_ci	 * doesn't then just throw a warning and don't record anything. We
688c2ecf20Sopenharmony_ci	 * also don't accept zero here as that would effectively clear a
698c2ecf20Sopenharmony_ci	 * previous error.
708c2ecf20Sopenharmony_ci	 */
718c2ecf20Sopenharmony_ci	old = READ_ONCE(*eseq);
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	if (WARN(unlikely(err == 0 || (unsigned int)-err > MAX_ERRNO),
748c2ecf20Sopenharmony_ci				"err = %d\n", err))
758c2ecf20Sopenharmony_ci		return old;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	for (;;) {
788c2ecf20Sopenharmony_ci		errseq_t new;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci		/* Clear out error bits and set new error */
818c2ecf20Sopenharmony_ci		new = (old & ~(MAX_ERRNO|ERRSEQ_SEEN)) | -err;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci		/* Only increment if someone has looked at it */
848c2ecf20Sopenharmony_ci		if (old & ERRSEQ_SEEN)
858c2ecf20Sopenharmony_ci			new += ERRSEQ_CTR_INC;
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci		/* If there would be no change, then call it done */
888c2ecf20Sopenharmony_ci		if (new == old) {
898c2ecf20Sopenharmony_ci			cur = new;
908c2ecf20Sopenharmony_ci			break;
918c2ecf20Sopenharmony_ci		}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci		/* Try to swap the new value into place */
948c2ecf20Sopenharmony_ci		cur = cmpxchg(eseq, old, new);
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci		/*
978c2ecf20Sopenharmony_ci		 * Call it success if we did the swap or someone else beat us
988c2ecf20Sopenharmony_ci		 * to it for the same value.
998c2ecf20Sopenharmony_ci		 */
1008c2ecf20Sopenharmony_ci		if (likely(cur == old || cur == new))
1018c2ecf20Sopenharmony_ci			break;
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci		/* Raced with an update, try again */
1048c2ecf20Sopenharmony_ci		old = cur;
1058c2ecf20Sopenharmony_ci	}
1068c2ecf20Sopenharmony_ci	return cur;
1078c2ecf20Sopenharmony_ci}
1088c2ecf20Sopenharmony_ciEXPORT_SYMBOL(errseq_set);
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci/**
1118c2ecf20Sopenharmony_ci * errseq_sample() - Grab current errseq_t value.
1128c2ecf20Sopenharmony_ci * @eseq: Pointer to errseq_t to be sampled.
1138c2ecf20Sopenharmony_ci *
1148c2ecf20Sopenharmony_ci * This function allows callers to initialise their errseq_t variable.
1158c2ecf20Sopenharmony_ci * If the error has been "seen", new callers will not see an old error.
1168c2ecf20Sopenharmony_ci * If there is an unseen error in @eseq, the caller of this function will
1178c2ecf20Sopenharmony_ci * see it the next time it checks for an error.
1188c2ecf20Sopenharmony_ci *
1198c2ecf20Sopenharmony_ci * Context: Any context.
1208c2ecf20Sopenharmony_ci * Return: The current errseq value.
1218c2ecf20Sopenharmony_ci */
1228c2ecf20Sopenharmony_cierrseq_t errseq_sample(errseq_t *eseq)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	errseq_t old = READ_ONCE(*eseq);
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	/* If nobody has seen this error yet, then we can be the first. */
1278c2ecf20Sopenharmony_ci	if (!(old & ERRSEQ_SEEN))
1288c2ecf20Sopenharmony_ci		old = 0;
1298c2ecf20Sopenharmony_ci	return old;
1308c2ecf20Sopenharmony_ci}
1318c2ecf20Sopenharmony_ciEXPORT_SYMBOL(errseq_sample);
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci/**
1348c2ecf20Sopenharmony_ci * errseq_check() - Has an error occurred since a particular sample point?
1358c2ecf20Sopenharmony_ci * @eseq: Pointer to errseq_t value to be checked.
1368c2ecf20Sopenharmony_ci * @since: Previously-sampled errseq_t from which to check.
1378c2ecf20Sopenharmony_ci *
1388c2ecf20Sopenharmony_ci * Grab the value that eseq points to, and see if it has changed @since
1398c2ecf20Sopenharmony_ci * the given value was sampled. The @since value is not advanced, so there
1408c2ecf20Sopenharmony_ci * is no need to mark the value as seen.
1418c2ecf20Sopenharmony_ci *
1428c2ecf20Sopenharmony_ci * Return: The latest error set in the errseq_t or 0 if it hasn't changed.
1438c2ecf20Sopenharmony_ci */
1448c2ecf20Sopenharmony_ciint errseq_check(errseq_t *eseq, errseq_t since)
1458c2ecf20Sopenharmony_ci{
1468c2ecf20Sopenharmony_ci	errseq_t cur = READ_ONCE(*eseq);
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	if (likely(cur == since))
1498c2ecf20Sopenharmony_ci		return 0;
1508c2ecf20Sopenharmony_ci	return -(cur & MAX_ERRNO);
1518c2ecf20Sopenharmony_ci}
1528c2ecf20Sopenharmony_ciEXPORT_SYMBOL(errseq_check);
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci/**
1558c2ecf20Sopenharmony_ci * errseq_check_and_advance() - Check an errseq_t and advance to current value.
1568c2ecf20Sopenharmony_ci * @eseq: Pointer to value being checked and reported.
1578c2ecf20Sopenharmony_ci * @since: Pointer to previously-sampled errseq_t to check against and advance.
1588c2ecf20Sopenharmony_ci *
1598c2ecf20Sopenharmony_ci * Grab the eseq value, and see whether it matches the value that @since
1608c2ecf20Sopenharmony_ci * points to. If it does, then just return 0.
1618c2ecf20Sopenharmony_ci *
1628c2ecf20Sopenharmony_ci * If it doesn't, then the value has changed. Set the "seen" flag, and try to
1638c2ecf20Sopenharmony_ci * swap it into place as the new eseq value. Then, set that value as the new
1648c2ecf20Sopenharmony_ci * "since" value, and return whatever the error portion is set to.
1658c2ecf20Sopenharmony_ci *
1668c2ecf20Sopenharmony_ci * Note that no locking is provided here for concurrent updates to the "since"
1678c2ecf20Sopenharmony_ci * value. The caller must provide that if necessary. Because of this, callers
1688c2ecf20Sopenharmony_ci * may want to do a lockless errseq_check before taking the lock and calling
1698c2ecf20Sopenharmony_ci * this.
1708c2ecf20Sopenharmony_ci *
1718c2ecf20Sopenharmony_ci * Return: Negative errno if one has been stored, or 0 if no new error has
1728c2ecf20Sopenharmony_ci * occurred.
1738c2ecf20Sopenharmony_ci */
1748c2ecf20Sopenharmony_ciint errseq_check_and_advance(errseq_t *eseq, errseq_t *since)
1758c2ecf20Sopenharmony_ci{
1768c2ecf20Sopenharmony_ci	int err = 0;
1778c2ecf20Sopenharmony_ci	errseq_t old, new;
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	/*
1808c2ecf20Sopenharmony_ci	 * Most callers will want to use the inline wrapper to check this,
1818c2ecf20Sopenharmony_ci	 * so that the common case of no error is handled without needing
1828c2ecf20Sopenharmony_ci	 * to take the lock that protects the "since" value.
1838c2ecf20Sopenharmony_ci	 */
1848c2ecf20Sopenharmony_ci	old = READ_ONCE(*eseq);
1858c2ecf20Sopenharmony_ci	if (old != *since) {
1868c2ecf20Sopenharmony_ci		/*
1878c2ecf20Sopenharmony_ci		 * Set the flag and try to swap it into place if it has
1888c2ecf20Sopenharmony_ci		 * changed.
1898c2ecf20Sopenharmony_ci		 *
1908c2ecf20Sopenharmony_ci		 * We don't care about the outcome of the swap here. If the
1918c2ecf20Sopenharmony_ci		 * swap doesn't occur, then it has either been updated by a
1928c2ecf20Sopenharmony_ci		 * writer who is altering the value in some way (updating
1938c2ecf20Sopenharmony_ci		 * counter or resetting the error), or another reader who is
1948c2ecf20Sopenharmony_ci		 * just setting the "seen" flag. Either outcome is OK, and we
1958c2ecf20Sopenharmony_ci		 * can advance "since" and return an error based on what we
1968c2ecf20Sopenharmony_ci		 * have.
1978c2ecf20Sopenharmony_ci		 */
1988c2ecf20Sopenharmony_ci		new = old | ERRSEQ_SEEN;
1998c2ecf20Sopenharmony_ci		if (new != old)
2008c2ecf20Sopenharmony_ci			cmpxchg(eseq, old, new);
2018c2ecf20Sopenharmony_ci		*since = new;
2028c2ecf20Sopenharmony_ci		err = -(new & MAX_ERRNO);
2038c2ecf20Sopenharmony_ci	}
2048c2ecf20Sopenharmony_ci	return err;
2058c2ecf20Sopenharmony_ci}
2068c2ecf20Sopenharmony_ciEXPORT_SYMBOL(errseq_check_and_advance);
207