From: "Arnd Bergmann" <arnd@kernel.org>
To: "Linus Torvalds" <torvalds@linuxfoundation.org>,
"David Laight" <David.Laight@aculab.com>
Cc: "linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
"Jens Axboe" <axboe@kernel.dk>,
"Matthew Wilcox" <willy@infradead.org>,
"Christoph Hellwig" <hch@infradead.org>,
"Andrew Morton" <akpm@linux-foundation.org>,
"Andy Shevchenko" <andriy.shevchenko@linux.intel.com>,
"Dan Carpenter" <dan.carpenter@linaro.org>,
"Jason A . Donenfeld" <Jason@zx2c4.com>,
"pedro.falcato@gmail.com" <pedro.falcato@gmail.com>,
"Mateusz Guzik" <mjguzik@gmail.com>,
"linux-mm@kvack.org" <linux-mm@kvack.org>,
"Lorenzo Stoakes" <lorenzo.stoakes@oracle.com>
Subject: Re: [PATCH v2 1/8] minmax: Put all the clamp() definitions together
Date: Tue, 30 Jul 2024 00:25:24 +0200 [thread overview]
Message-ID: <e946e002-8ca8-4a09-a800-d117c89b39d3@app.fastmail.com> (raw)
In-Reply-To: <CAHk-=wj900Q3FtEWJFGADQ0EbmYwBHW8cWzB0p0nvFck=0+y6A@mail.gmail.com>
On Sun, Jul 28, 2024, at 22:13, Linus Torvalds wrote:
> On Sun, 28 Jul 2024 at 13:10, David Laight <David.Laight@aculab.com> wrote:
>>
>> I think they just need to be MIN_CONST() (without the casts).
>
> I'll just convert the existing cases of min_t/max_t to MIN_T/MAX_T,
> which I already added for other reasons anyway.
>
> That makes min_t/max_t not have to care about the nasty special cases
> (really just array sizes in these cases, and they all wanted MAX_T).
I had prototyped something similar end of last week but didn't manage
to get my version out to you before the weekend. Comparing mine with
what you ended up committing:
- You found exactly the same array index uses I found in
randconfig testing, so I'm not aware of anything missing
there.
- My macros use __builtin_choose_expr() instead of ?: to
ensure that the arguments are constant, this produces a
relatively clear compiler warning when they are not.
Without that, I would expect random drivers to start
using MIN()/MAX() in places where it's not safe.
- I went with the belts-and-suspenders version of MIN()/MAX()
that works when comparing a negative constant against
an unsigned one. This requires expanding each argument
four or five times instead of two, so you might still
want the simpler version (like MIN_T/MAX_T):
--- a/include/linux/minmax.h
+++ b/include/linux/minmax.h
@@ -295,12 +271,18 @@ static inline bool in_range32(u32 val, u32 start, u32 len)
do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
/*
- * Use these carefully: no type checking, and uses the arguments
- * multiple times. Use for obvious constants only.
+ * These only work on constant values but return a constant value that
+ * can be used as an array size
*/
-#define MIN(a,b) __cmp(min,a,b)
-#define MAX(a,b) __cmp(max,a,b)
-#define MIN_T(type,a,b) __cmp(min,(type)(a),(type)(b))
-#define MAX_T(type,a,b) __cmp(max,(type)(a),(type)(b))
+#define MIN(x, y) \
+ __builtin_choose_expr(((x) < 0 && (y) > 0), (x), \
+ __builtin_choose_expr((((y) < 0 && (x) > 0) || (y) < (x)), (y), (x)))
+
+#define MAX(x, y) \
+ __builtin_choose_expr(((x) > 0 && (y) < 0), (x), \
+ __builtin_choose_expr((((y) > 0 && (x) < 0) || (y) > (x)), (y), (x)))
+
+#define MIN_T(type,a,b) (type)__builtin_choose_expr((type)(a) < (type)(b), (a), (b))
+#define MAX_T(type,a,b) (type)__builtin_choose_expr((type)(a) > (type)(b), (a), (b))
#endif /* _LINUX_MINMAX_H */
- The change above requires changing a number of files that were
previously using their own MIN()/MAX() macros over to using
min()/max(), as they are passing non-constant values in:
drivers/gpu/drm/amd/display/dc/core/dc_stream.c | 12 ++++--------
.../drm/amd/display/dc/dio/dcn20/dcn20_link_encoder.c | 9 +--------
.../drm/amd/display/dc/dio/dcn31/dcn31_dio_link_encoder.c | 8 ++------
.../drm/amd/display/dc/dio/dcn32/dcn32_dio_link_encoder.c | 6 +-----
.../drm/amd/display/dc/dio/dcn321/dcn321_dio_link_encoder.c | 4 ----
.../drm/amd/display/dc/dio/dcn401/dcn401_dio_link_encoder.c | 8 --------
.../drm/amd/display/dc/dml/dcn20/dcn20_fpu.c | 13 +++----------
.../drm/amd/display/dc/dsc/dc_dsc.c | 9 +--------
.../drm/amd/display/dc/link/protocols/link_dp_capability.c | 13 +++----------
drivers/gpu/drm/amd/display/modules/hdcp/hdcp_ddc.c | 11 ++++-------
drivers/gpu/drm/radeon/evergreen_cs.c | 9 ++-------
drivers/platform/x86/sony-laptop.c | 4 ++--
kernel/trace/preemptirq_delay_test.c | 2 +-
lib/decompress_unlzma.c | 7 ++-----
14 files changed, 26 insertions(+), 89 deletions(-)
Changing these is probably a good idea regardless.
- I also tried simplifying __types_ok() further, which as
you already mentioned doesn't easily work with pointer
arguments. Again we could work around this with a separate
min_ptr()/max_ptr() helper. I only found 11 files that
actually compare pointers (on x86/arm/arm64 randconfig):
arch/arm64/kvm/hyp/nvhe/page_alloc.c | 2 +-
crypto/skcipher.c | 2 +-
drivers/gpu/drm/drm_modes.c | 2 +-
drivers/infiniband/hw/hfi1/pio_copy.c | 4 ++--
drivers/irqchip/irq-bcm7120-l2.c | 2 +-
drivers/spi/spi-cs42l43.c | 8 ++++----
fs/ntfs3/lznt.c | 2 +-
lib/lzo/lzo1x_compress.c | 2 +-
mm/kmemleak.c | 4 ++--
mm/percpu.c | 2 +-
net/ceph/osdmap.c | 6 +++---
11 files changed, 25 insertions(+), 18 deletions(-)
The simpler __types_ok() needs more testing across all
compiler versions, so that wouldn't be for 6.11 anyway.
I can send the min_ptr()/max_ptr() stuff anyway if
you think that's a good idea.
Arnd
next prev parent reply other threads:[~2024-07-29 22:25 UTC|newest]
Thread overview: 50+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-28 14:15 [PATCH v2 0/8] minmax: reduce compilation time David Laight
2024-07-28 14:17 ` [PATCH v2 1/8] minmax: Put all the clamp() definitions together David Laight
2024-07-28 17:24 ` Linus Torvalds
2024-07-28 18:11 ` David Laight
2024-07-28 19:55 ` Linus Torvalds
2024-07-28 20:09 ` David Laight
2024-07-28 20:13 ` Linus Torvalds
2024-07-28 20:22 ` David Laight
2024-07-28 20:31 ` Linus Torvalds
2024-07-28 22:13 ` David Laight
2024-07-28 22:22 ` Linus Torvalds
2024-07-29 8:01 ` David Laight
2024-07-28 21:01 ` Linus Torvalds
2024-07-28 21:53 ` David Laight
2024-07-29 4:15 ` Linus Torvalds
2024-07-29 22:25 ` Arnd Bergmann [this message]
2024-07-29 23:21 ` Linus Torvalds
2024-07-30 1:52 ` Linus Torvalds
2024-07-30 3:59 ` Linus Torvalds
2024-07-30 10:10 ` Arnd Bergmann
2024-07-30 14:14 ` Arnd Bergmann
2024-07-30 18:02 ` Linus Torvalds
2024-07-30 19:52 ` Linus Torvalds
2024-07-30 21:47 ` David Laight
2024-07-30 22:44 ` Linus Torvalds
2024-07-30 23:03 ` Linus Torvalds
2024-07-31 8:09 ` David Laight
2024-07-31 10:50 ` Arnd Bergmann
2024-07-31 15:38 ` Linus Torvalds
2024-07-31 15:56 ` David Laight
2024-07-31 16:04 ` Linus Torvalds
2024-12-04 13:15 ` Geert Uytterhoeven
2024-12-04 17:16 ` David Laight
2024-07-30 16:35 ` Linus Torvalds
2024-07-30 16:46 ` Linus Torvalds
2024-07-30 12:03 ` David Laight
2024-07-28 18:23 ` David Laight
2024-07-28 14:18 ` [PATCH v2 2/8] minmax: Use _Static_assert() instead of static_assert() David Laight
2024-07-28 17:51 ` Christophe JAILLET
2024-07-28 18:12 ` David Laight
2024-07-28 14:19 ` [PATCH v2 3/8] compiler.h: Add __if_constexpr(expr, if_const, if_not_const) David Laight
2024-07-28 14:20 ` [PATCH v2 4/8] minmax: Simplify signedness check David Laight
2024-07-28 16:57 ` Linus Torvalds
2024-07-28 18:14 ` David Laight
2024-07-28 20:13 ` David Laight
2024-07-28 14:21 ` [PATCH v2 5/8] minmax: Factor out the zero-extension logic from umin/umax David Laight
2024-07-28 14:22 ` [PATCH v2 6/8] minmax: Optimise _Static_assert() check in clamp() David Laight
2024-07-28 14:23 ` [PATCH v2 7/8] minmax: Use __auto_type David Laight
2024-07-28 16:59 ` Linus Torvalds
2024-07-28 14:24 ` [PATCH v2 8/8] minmax: minmax: Add __types_ok3() and optimise defines with 3 arguments David Laight
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=e946e002-8ca8-4a09-a800-d117c89b39d3@app.fastmail.com \
--to=arnd@kernel.org \
--cc=David.Laight@aculab.com \
--cc=Jason@zx2c4.com \
--cc=akpm@linux-foundation.org \
--cc=andriy.shevchenko@linux.intel.com \
--cc=axboe@kernel.dk \
--cc=dan.carpenter@linaro.org \
--cc=hch@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lorenzo.stoakes@oracle.com \
--cc=mjguzik@gmail.com \
--cc=pedro.falcato@gmail.com \
--cc=torvalds@linuxfoundation.org \
--cc=willy@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox