lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <c6924533f157497b836bff24073934a6@AcuMS.aculab.com>
Date: Sun, 25 Feb 2024 16:53:40 +0000
From: David Laight <David.Laight@...LAB.COM>
To: "'linux-kernel@...r.kernel.org'" <linux-kernel@...r.kernel.org>, "'Linus
 Torvalds'" <torvalds@...ux-foundation.org>, 'Netdev'
	<netdev@...r.kernel.org>, "'dri-devel@...ts.freedesktop.org'"
	<dri-devel@...ts.freedesktop.org>
CC: 'Jens Axboe' <axboe@...nel.dk>, "'Matthew Wilcox (Oracle)'"
	<willy@...radead.org>, 'Christoph Hellwig' <hch@...radead.org>,
	"'linux-btrfs@...r.kernel.org'" <linux-btrfs@...r.kernel.org>, "'Andrew
 Morton'" <akpm@...ux-foundation.org>, 'Andy Shevchenko'
	<andriy.shevchenko@...ux.intel.com>, "'David S . Miller'"
	<davem@...emloft.net>, 'Dan Carpenter' <dan.carpenter@...aro.org>, "'Jani
 Nikula'" <jani.nikula@...ux.intel.com>
Subject: [PATCH next v2 08/11] minmax: Add min_const() and max_const()

The expansions of min() and max() contain statement expressions so are
not valid for static intialisers.
min_const() and max_const() are expressions so can be used for static
initialisers.
The arguments are checked for being constant and for negative signed
values being converted to large unsigned values.

Using these to size on-stack arrays lets min/max be simplified.
Zero is added before the compare to convert enum values to integers
avoinding the need for casts when enums have been used for constants.

Signed-off-by: David Laight <david.laight@...lab.com>
---
 include/linux/minmax.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

Changes for v2:
- Typographical and spelling corrections to the commit messages.
  Patches unchanged.

diff --git a/include/linux/minmax.h b/include/linux/minmax.h
index 278a390b8a4c..c08916588425 100644
--- a/include/linux/minmax.h
+++ b/include/linux/minmax.h
@@ -60,19 +60,34 @@
 			#op "(" #x ", " #y ") signedness error, fix types or consider u" #op "() before " #op "_t()"); \
 		__cmp_once(op, x, y, uniq); }))
 
+#define __careful_cmp_const(op, x, y)				\
+	(BUILD_BUG_ON_ZERO(!__is_constexpr((x) - (y))) +	\
+		BUILD_BUG_ON_ZERO(!__types_ok(x, y)) +		\
+		__cmp(op, (x) + 0, (y) + 0))
+
 /**
  * min - return minimum of two values of the same or compatible types
  * @x: first value
  * @y: second value
+ *
+ * If @x and @y are constants the return value is constant, but not 'constant
+ * enough' for things like static initialisers.
+ * min_const(@x, @y) is a constant expression for constant inputs.
  */
 #define min(x, y)	__careful_cmp(min, x, y, __COUNTER__)
+#define min_const(x, y)	__careful_cmp_const(min, x, y)
 
 /**
  * max - return maximum of two values of the same or compatible types
  * @x: first value
  * @y: second value
+ *
+ * If @x and @y are constants the return value is constant, but not 'constant
+ * enough' for things like static initialisers.
+ * max_const(@x, @y) is a constant expression for constant inputs.
  */
 #define max(x, y)	__careful_cmp(max, x, y, __COUNTER__)
+#define max_const(x, y)	__careful_cmp_const(max, x, y)
 
 /**
  * umin - return minimum of two non-negative values
-- 
2.17.1

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ