aboutsummaryrefslogtreecommitdiffstats
path: root/include/linux/overflow.h
diff options
context:
space:
mode:
authorTony Lindgren <tony@atomide.com>2018-08-28 12:58:03 -0400
committerTony Lindgren <tony@atomide.com>2018-08-28 12:58:03 -0400
commitea4d65f14f6aaa53e379b93c5544245ef081b3e7 (patch)
treea15485f4f1cf547a52b31fa8e16e14b9579b7200 /include/linux/overflow.h
parentce32d59ee2cd036f6e8a6ed17a06a0b0bec5c67c (diff)
parent496f3347d834aec91c38b45d6249ed00f58ad233 (diff)
Merge branch 'perm-fix' into omap-for-v4.19/fixes-v2
Diffstat (limited to 'include/linux/overflow.h')
-rw-r--r--include/linux/overflow.h31
1 files changed, 31 insertions, 0 deletions
diff --git a/include/linux/overflow.h b/include/linux/overflow.h
index 8712ff70995f..40b48e2133cb 100644
--- a/include/linux/overflow.h
+++ b/include/linux/overflow.h
@@ -202,6 +202,37 @@
202 202
203#endif /* COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW */ 203#endif /* COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW */
204 204
205/** check_shl_overflow() - Calculate a left-shifted value and check overflow
206 *
207 * @a: Value to be shifted
208 * @s: How many bits left to shift
209 * @d: Pointer to where to store the result
210 *
211 * Computes *@d = (@a << @s)
212 *
213 * Returns true if '*d' cannot hold the result or when 'a << s' doesn't
214 * make sense. Example conditions:
215 * - 'a << s' causes bits to be lost when stored in *d.
216 * - 's' is garbage (e.g. negative) or so large that the result of
217 * 'a << s' is guaranteed to be 0.
218 * - 'a' is negative.
219 * - 'a << s' sets the sign bit, if any, in '*d'.
220 *
221 * '*d' will hold the results of the attempted shift, but is not
222 * considered "safe for use" if false is returned.
223 */
224#define check_shl_overflow(a, s, d) ({ \
225 typeof(a) _a = a; \
226 typeof(s) _s = s; \
227 typeof(d) _d = d; \
228 u64 _a_full = _a; \
229 unsigned int _to_shift = \
230 _s >= 0 && _s < 8 * sizeof(*d) ? _s : 0; \
231 *_d = (_a_full << _to_shift); \
232 (_to_shift != _s || *_d < 0 || _a < 0 || \
233 (*_d >> _to_shift) != _a); \
234})
235
205/** 236/**
206 * array_size() - Calculate size of 2-dimensional array. 237 * array_size() - Calculate size of 2-dimensional array.
207 * 238 *