1 From sj@kernel.org Tue Jul 16 20:33:46 2024
2 From: SeongJae Park <sj@kernel.org>
3 Date: Tue, 16 Jul 2024 11:33:32 -0700
4 Subject: minmax: relax check to allow comparison between unsigned arguments and signed constants
5 To: stable@vger.kernel.org, gregkh@linuxfoundation.org
6 Cc: David Laight <David.Laight@ACULAB.COM>, linux-kernel@vger.kernel.org, David Laight <david.laight@aculab.com>, Andy Shevchenko <andriy.shevchenko@linux.intel.com>, Christoph Hellwig <hch@infradead.org>, "Jason A . Donenfeld" <Jason@zx2c4.com>, Linus Torvalds <torvalds@linux-foundation.org>, Matthew Wilcox <willy@infradead.org>, Andrew Morton <akpm@linux-foundation.org>, SeongJae Park <sj@kernel.org>
7 Message-ID: <20240716183333.138498-8-sj@kernel.org>
9 From: David Laight <David.Laight@ACULAB.COM>
11 commit 867046cc7027703f60a46339ffde91a1970f2901 upstream.
13 Allow (for example) min(unsigned_var, 20).
15 The opposite min(signed_var, 20u) is still errored.
17 Since a comparison between signed and unsigned never makes the unsigned
18 value negative it is only necessary to adjust the __types_ok() test.
20 Link: https://lkml.kernel.org/r/633b64e2f39e46bb8234809c5595b8c7@AcuMS.aculab.com
21 Signed-off-by: David Laight <david.laight@aculab.com>
22 Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
23 Cc: Christoph Hellwig <hch@infradead.org>
24 Cc: Jason A. Donenfeld <Jason@zx2c4.com>
25 Cc: Linus Torvalds <torvalds@linux-foundation.org>
26 Cc: Matthew Wilcox (Oracle) <willy@infradead.org>
27 Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
28 (cherry picked from commit 867046cc7027703f60a46339ffde91a1970f2901)
29 Signed-off-by: SeongJae Park <sj@kernel.org>
30 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
32 include/linux/minmax.h | 24 +++++++++++++++++-------
33 1 file changed, 17 insertions(+), 7 deletions(-)
35 --- a/include/linux/minmax.h
36 +++ b/include/linux/minmax.h
39 * min()/max()/clamp() macros must accomplish three things:
41 - * - avoid multiple evaluations of the arguments (so side-effects like
42 + * - Avoid multiple evaluations of the arguments (so side-effects like
43 * "x++" happen only once) when non-constant.
44 - * - perform signed v unsigned type-checking (to generate compile
45 - * errors instead of nasty runtime surprises).
46 - * - retain result as a constant expressions when called with only
47 + * - Retain result as a constant expressions when called with only
48 * constant expressions (to avoid tripping VLA warnings in stack
50 + * - Perform signed v unsigned type-checking (to generate compile
51 + * errors instead of nasty runtime surprises).
52 + * - Unsigned char/short are always promoted to signed int and can be
53 + * compared against signed or unsigned arguments.
54 + * - Unsigned arguments can be compared against non-negative signed constants.
55 + * - Comparison of a signed argument against an unsigned constant fails
56 + * even if the constant is below __INT_MAX__ and could be cast to int.
58 #define __typecheck(x, y) \
59 (!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))
61 __builtin_choose_expr(__is_constexpr(is_signed_type(typeof(x))), \
62 is_signed_type(typeof(x)), 0)
64 -#define __types_ok(x, y) \
65 - (__is_signed(x) == __is_signed(y) || \
66 - __is_signed((x) + 0) == __is_signed((y) + 0))
67 +/* True for a non-negative signed int constant */
68 +#define __is_noneg_int(x) \
69 + (__builtin_choose_expr(__is_constexpr(x) && __is_signed(x), x, -1) >= 0)
71 +#define __types_ok(x, y) \
72 + (__is_signed(x) == __is_signed(y) || \
73 + __is_signed((x) + 0) == __is_signed((y) + 0) || \
74 + __is_noneg_int(x) || __is_noneg_int(y))
76 #define __cmp_op_min <
77 #define __cmp_op_max >