Linux Audio

Check our new training course

Embedded Linux training

Mar 31-Apr 8, 2025
Register
Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0
  2#include <linux/err.h>
  3#include <linux/bug.h>
  4#include <linux/atomic.h>
  5#include <linux/errseq.h>
  6#include <linux/log2.h>
  7
  8/*
  9 * An errseq_t is a way of recording errors in one place, and allowing any
 10 * number of "subscribers" to tell whether it has changed since a previous
 11 * point where it was sampled.
 12 *
 13 * It's implemented as an unsigned 32-bit value. The low order bits are
 14 * designated to hold an error code (between 0 and -MAX_ERRNO). The upper bits
 15 * are used as a counter. This is done with atomics instead of locking so that
 16 * these functions can be called from any context.
 17 *
 18 * The general idea is for consumers to sample an errseq_t value. That value
 19 * can later be used to tell whether any new errors have occurred since that
 20 * sampling was done.
 21 *
 22 * Note that there is a risk of collisions if new errors are being recorded
 23 * frequently, since we have so few bits to use as a counter.
 24 *
 25 * To mitigate this, one bit is used as a flag to tell whether the value has
 26 * been sampled since a new value was recorded. That allows us to avoid bumping
 27 * the counter if no one has sampled it since the last time an error was
 28 * recorded.
 29 *
 30 * A new errseq_t should always be zeroed out.  A errseq_t value of all zeroes
 31 * is the special (but common) case where there has never been an error. An all
 32 * zero value thus serves as the "epoch" if one wishes to know whether there
 33 * has ever been an error set since it was first initialized.
 34 */
 35
 36/* The low bits are designated for error code (max of MAX_ERRNO) */
 37#define ERRSEQ_SHIFT		ilog2(MAX_ERRNO + 1)
 38
 39/* This bit is used as a flag to indicate whether the value has been seen */
 40#define ERRSEQ_SEEN		(1 << ERRSEQ_SHIFT)
 41
 42/* The lowest bit of the counter */
 43#define ERRSEQ_CTR_INC		(1 << (ERRSEQ_SHIFT + 1))
 44
 45/**
 46 * errseq_set - set a errseq_t for later reporting
 47 * @eseq: errseq_t field that should be set
 48 * @err: error to set (must be between -1 and -MAX_ERRNO)
 49 *
 50 * This function sets the error in @eseq, and increments the sequence counter
 51 * if the last sequence was sampled at some point in the past.
 52 *
 53 * Any error set will always overwrite an existing error.
 54 *
 55 * Return: The previous value, primarily for debugging purposes. The
 56 * return value should not be used as a previously sampled value in later
 57 * calls as it will not have the SEEN flag set.
 58 */
 59errseq_t errseq_set(errseq_t *eseq, int err)
 60{
 61	errseq_t cur, old;
 62
 63	/* MAX_ERRNO must be able to serve as a mask */
 64	BUILD_BUG_ON_NOT_POWER_OF_2(MAX_ERRNO + 1);
 65
 66	/*
 67	 * Ensure the error code actually fits where we want it to go. If it
 68	 * doesn't then just throw a warning and don't record anything. We
 69	 * also don't accept zero here as that would effectively clear a
 70	 * previous error.
 71	 */
 72	old = READ_ONCE(*eseq);
 73
 74	if (WARN(unlikely(err == 0 || (unsigned int)-err > MAX_ERRNO),
 75				"err = %d\n", err))
 76		return old;
 77
 78	for (;;) {
 79		errseq_t new;
 80
 81		/* Clear out error bits and set new error */
 82		new = (old & ~(MAX_ERRNO|ERRSEQ_SEEN)) | -err;
 83
 84		/* Only increment if someone has looked at it */
 85		if (old & ERRSEQ_SEEN)
 86			new += ERRSEQ_CTR_INC;
 87
 88		/* If there would be no change, then call it done */
 89		if (new == old) {
 90			cur = new;
 91			break;
 92		}
 93
 94		/* Try to swap the new value into place */
 95		cur = cmpxchg(eseq, old, new);
 96
 97		/*
 98		 * Call it success if we did the swap or someone else beat us
 99		 * to it for the same value.
100		 */
101		if (likely(cur == old || cur == new))
102			break;
103
104		/* Raced with an update, try again */
105		old = cur;
106	}
107	return cur;
108}
109EXPORT_SYMBOL(errseq_set);
110
111/**
112 * errseq_sample() - Grab current errseq_t value.
113 * @eseq: Pointer to errseq_t to be sampled.
114 *
115 * This function allows callers to initialise their errseq_t variable.
116 * If the error has been "seen", new callers will not see an old error.
117 * If there is an unseen error in @eseq, the caller of this function will
118 * see it the next time it checks for an error.
119 *
120 * Context: Any context.
121 * Return: The current errseq value.
122 */
123errseq_t errseq_sample(errseq_t *eseq)
124{
125	errseq_t old = READ_ONCE(*eseq);
126
127	/* If nobody has seen this error yet, then we can be the first. */
128	if (!(old & ERRSEQ_SEEN))
129		old = 0;
130	return old;
131}
132EXPORT_SYMBOL(errseq_sample);
133
134/**
135 * errseq_check() - Has an error occurred since a particular sample point?
136 * @eseq: Pointer to errseq_t value to be checked.
137 * @since: Previously-sampled errseq_t from which to check.
138 *
139 * Grab the value that eseq points to, and see if it has changed @since
140 * the given value was sampled. The @since value is not advanced, so there
141 * is no need to mark the value as seen.
142 *
143 * Return: The latest error set in the errseq_t or 0 if it hasn't changed.
144 */
145int errseq_check(errseq_t *eseq, errseq_t since)
146{
147	errseq_t cur = READ_ONCE(*eseq);
148
149	if (likely(cur == since))
150		return 0;
151	return -(cur & MAX_ERRNO);
152}
153EXPORT_SYMBOL(errseq_check);
154
155/**
156 * errseq_check_and_advance() - Check an errseq_t and advance to current value.
157 * @eseq: Pointer to value being checked and reported.
158 * @since: Pointer to previously-sampled errseq_t to check against and advance.
159 *
160 * Grab the eseq value, and see whether it matches the value that @since
161 * points to. If it does, then just return 0.
162 *
163 * If it doesn't, then the value has changed. Set the "seen" flag, and try to
164 * swap it into place as the new eseq value. Then, set that value as the new
165 * "since" value, and return whatever the error portion is set to.
166 *
167 * Note that no locking is provided here for concurrent updates to the "since"
168 * value. The caller must provide that if necessary. Because of this, callers
169 * may want to do a lockless errseq_check before taking the lock and calling
170 * this.
171 *
172 * Return: Negative errno if one has been stored, or 0 if no new error has
173 * occurred.
174 */
175int errseq_check_and_advance(errseq_t *eseq, errseq_t *since)
176{
177	int err = 0;
178	errseq_t old, new;
179
180	/*
181	 * Most callers will want to use the inline wrapper to check this,
182	 * so that the common case of no error is handled without needing
183	 * to take the lock that protects the "since" value.
184	 */
185	old = READ_ONCE(*eseq);
186	if (old != *since) {
187		/*
188		 * Set the flag and try to swap it into place if it has
189		 * changed.
190		 *
191		 * We don't care about the outcome of the swap here. If the
192		 * swap doesn't occur, then it has either been updated by a
193		 * writer who is altering the value in some way (updating
194		 * counter or resetting the error), or another reader who is
195		 * just setting the "seen" flag. Either outcome is OK, and we
196		 * can advance "since" and return an error based on what we
197		 * have.
198		 */
199		new = old | ERRSEQ_SEEN;
200		if (new != old)
201			cmpxchg(eseq, old, new);
202		*since = new;
203		err = -(new & MAX_ERRNO);
204	}
205	return err;
206}
207EXPORT_SYMBOL(errseq_check_and_advance);
v4.17
  1// SPDX-License-Identifier: GPL-2.0
  2#include <linux/err.h>
  3#include <linux/bug.h>
  4#include <linux/atomic.h>
  5#include <linux/errseq.h>
 
  6
  7/*
  8 * An errseq_t is a way of recording errors in one place, and allowing any
  9 * number of "subscribers" to tell whether it has changed since a previous
 10 * point where it was sampled.
 11 *
 12 * It's implemented as an unsigned 32-bit value. The low order bits are
 13 * designated to hold an error code (between 0 and -MAX_ERRNO). The upper bits
 14 * are used as a counter. This is done with atomics instead of locking so that
 15 * these functions can be called from any context.
 16 *
 17 * The general idea is for consumers to sample an errseq_t value. That value
 18 * can later be used to tell whether any new errors have occurred since that
 19 * sampling was done.
 20 *
 21 * Note that there is a risk of collisions if new errors are being recorded
 22 * frequently, since we have so few bits to use as a counter.
 23 *
 24 * To mitigate this, one bit is used as a flag to tell whether the value has
 25 * been sampled since a new value was recorded. That allows us to avoid bumping
 26 * the counter if no one has sampled it since the last time an error was
 27 * recorded.
 28 *
 29 * A new errseq_t should always be zeroed out.  A errseq_t value of all zeroes
 30 * is the special (but common) case where there has never been an error. An all
 31 * zero value thus serves as the "epoch" if one wishes to know whether there
 32 * has ever been an error set since it was first initialized.
 33 */
 34
 35/* The low bits are designated for error code (max of MAX_ERRNO) */
 36#define ERRSEQ_SHIFT		ilog2(MAX_ERRNO + 1)
 37
 38/* This bit is used as a flag to indicate whether the value has been seen */
 39#define ERRSEQ_SEEN		(1 << ERRSEQ_SHIFT)
 40
 41/* The lowest bit of the counter */
 42#define ERRSEQ_CTR_INC		(1 << (ERRSEQ_SHIFT + 1))
 43
 44/**
 45 * errseq_set - set a errseq_t for later reporting
 46 * @eseq: errseq_t field that should be set
 47 * @err: error to set (must be between -1 and -MAX_ERRNO)
 48 *
 49 * This function sets the error in @eseq, and increments the sequence counter
 50 * if the last sequence was sampled at some point in the past.
 51 *
 52 * Any error set will always overwrite an existing error.
 53 *
 54 * Return: The previous value, primarily for debugging purposes. The
 55 * return value should not be used as a previously sampled value in later
 56 * calls as it will not have the SEEN flag set.
 57 */
 58errseq_t errseq_set(errseq_t *eseq, int err)
 59{
 60	errseq_t cur, old;
 61
 62	/* MAX_ERRNO must be able to serve as a mask */
 63	BUILD_BUG_ON_NOT_POWER_OF_2(MAX_ERRNO + 1);
 64
 65	/*
 66	 * Ensure the error code actually fits where we want it to go. If it
 67	 * doesn't then just throw a warning and don't record anything. We
 68	 * also don't accept zero here as that would effectively clear a
 69	 * previous error.
 70	 */
 71	old = READ_ONCE(*eseq);
 72
 73	if (WARN(unlikely(err == 0 || (unsigned int)-err > MAX_ERRNO),
 74				"err = %d\n", err))
 75		return old;
 76
 77	for (;;) {
 78		errseq_t new;
 79
 80		/* Clear out error bits and set new error */
 81		new = (old & ~(MAX_ERRNO|ERRSEQ_SEEN)) | -err;
 82
 83		/* Only increment if someone has looked at it */
 84		if (old & ERRSEQ_SEEN)
 85			new += ERRSEQ_CTR_INC;
 86
 87		/* If there would be no change, then call it done */
 88		if (new == old) {
 89			cur = new;
 90			break;
 91		}
 92
 93		/* Try to swap the new value into place */
 94		cur = cmpxchg(eseq, old, new);
 95
 96		/*
 97		 * Call it success if we did the swap or someone else beat us
 98		 * to it for the same value.
 99		 */
100		if (likely(cur == old || cur == new))
101			break;
102
103		/* Raced with an update, try again */
104		old = cur;
105	}
106	return cur;
107}
108EXPORT_SYMBOL(errseq_set);
109
110/**
111 * errseq_sample() - Grab current errseq_t value.
112 * @eseq: Pointer to errseq_t to be sampled.
113 *
114 * This function allows callers to initialise their errseq_t variable.
115 * If the error has been "seen", new callers will not see an old error.
116 * If there is an unseen error in @eseq, the caller of this function will
117 * see it the next time it checks for an error.
118 *
119 * Context: Any context.
120 * Return: The current errseq value.
121 */
122errseq_t errseq_sample(errseq_t *eseq)
123{
124	errseq_t old = READ_ONCE(*eseq);
125
126	/* If nobody has seen this error yet, then we can be the first. */
127	if (!(old & ERRSEQ_SEEN))
128		old = 0;
129	return old;
130}
131EXPORT_SYMBOL(errseq_sample);
132
133/**
134 * errseq_check() - Has an error occurred since a particular sample point?
135 * @eseq: Pointer to errseq_t value to be checked.
136 * @since: Previously-sampled errseq_t from which to check.
137 *
138 * Grab the value that eseq points to, and see if it has changed @since
139 * the given value was sampled. The @since value is not advanced, so there
140 * is no need to mark the value as seen.
141 *
142 * Return: The latest error set in the errseq_t or 0 if it hasn't changed.
143 */
144int errseq_check(errseq_t *eseq, errseq_t since)
145{
146	errseq_t cur = READ_ONCE(*eseq);
147
148	if (likely(cur == since))
149		return 0;
150	return -(cur & MAX_ERRNO);
151}
152EXPORT_SYMBOL(errseq_check);
153
154/**
155 * errseq_check_and_advance() - Check an errseq_t and advance to current value.
156 * @eseq: Pointer to value being checked and reported.
157 * @since: Pointer to previously-sampled errseq_t to check against and advance.
158 *
159 * Grab the eseq value, and see whether it matches the value that @since
160 * points to. If it does, then just return 0.
161 *
162 * If it doesn't, then the value has changed. Set the "seen" flag, and try to
163 * swap it into place as the new eseq value. Then, set that value as the new
164 * "since" value, and return whatever the error portion is set to.
165 *
166 * Note that no locking is provided here for concurrent updates to the "since"
167 * value. The caller must provide that if necessary. Because of this, callers
168 * may want to do a lockless errseq_check before taking the lock and calling
169 * this.
170 *
171 * Return: Negative errno if one has been stored, or 0 if no new error has
172 * occurred.
173 */
174int errseq_check_and_advance(errseq_t *eseq, errseq_t *since)
175{
176	int err = 0;
177	errseq_t old, new;
178
179	/*
180	 * Most callers will want to use the inline wrapper to check this,
181	 * so that the common case of no error is handled without needing
182	 * to take the lock that protects the "since" value.
183	 */
184	old = READ_ONCE(*eseq);
185	if (old != *since) {
186		/*
187		 * Set the flag and try to swap it into place if it has
188		 * changed.
189		 *
190		 * We don't care about the outcome of the swap here. If the
191		 * swap doesn't occur, then it has either been updated by a
192		 * writer who is altering the value in some way (updating
193		 * counter or resetting the error), or another reader who is
194		 * just setting the "seen" flag. Either outcome is OK, and we
195		 * can advance "since" and return an error based on what we
196		 * have.
197		 */
198		new = old | ERRSEQ_SEEN;
199		if (new != old)
200			cmpxchg(eseq, old, new);
201		*since = new;
202		err = -(new & MAX_ERRNO);
203	}
204	return err;
205}
206EXPORT_SYMBOL(errseq_check_and_advance);