From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 2B7AECCA476 for ; Mon, 6 Oct 2025 12:41:58 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 89FA710E368; Mon, 6 Oct 2025 12:41:52 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="BgEg1mQy"; dkim-atps=neutral Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by gabe.freedesktop.org (Postfix) with ESMTPS id C72C710E3F0; Mon, 6 Oct 2025 10:40:06 +0000 (UTC) Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sea.source.kernel.org (Postfix) with ESMTP id 17AD944FED; Mon, 6 Oct 2025 10:40:06 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 67220C4CEF5; Mon, 6 Oct 2025 10:40:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1759747205; bh=Oy0x0l9dvNGSK/4iWDLpJJ1Oq4vRllDkRttPvYtvK6s=; h=Subject:To:Cc:From:Date:In-Reply-To:From; b=BgEg1mQyT0hghjEHgtGYQxEA2fduEpMljyfzV3idljRdylDHKJtAkLh+wuyonvVHi GcYJNsfT/aLgxoeYajfgNfWwB8VzjV8t3kQFVQ1fCQ+7ZZ0S1xSdxfumUi/AoAvY7C LL7WsXKp29k3/Mzn5uB16ooRAWKObnyTTCVpm9mw= Subject: Patch "minmax: fix up min3() and max3() too" has been added to the 6.1-stable tree To: David.Laight@ACULAB.COM, David.Laight@aculab.com, agk@redhat.com, airlied@gmail.com, akpm@linux-foundation.org, alexander.deucher@amd.com, amd-gfx@lists.freedesktop.org, andriy.shevchenko@linux.intel.com, arnd@kernel.org, asad.kamal@amd.com, christian.koenig@amd.com, clm@fb.com, dm-devel@lists.linux.dev, dmitry.torokhov@gmail.com, dri-devel@lists.freedesktop.org, dsterba@suse.com, farbere@amazon.com, gregkh@linuxfoundation.org, jernej.skrabec@gmail.com, kenneth.feng@amd.com, kevinyang.wang@amd.com, lijo.lazar@amd.com, linus.walleij@linaro.org, linux-arm-kernel@lists.infradead.org, linux-sunxi@lists.linux.dev, linux@rasmusvillemoes.dk, luc.vanoostenryck@gmail.com, mpatocka@redhat.com, pmladek@suse.com, rostedt@goodmis.org, samuel@sholland.org, senozhatsky@chromium.org, simona@ffwll.ch, snitzer@kernel.org, tglx@linutronix.de, torvalds@linux-foundation.org, wens@csie.org Cc: From: Date: Mon, 06 Oct 2025 12:39:09 +0200 In-Reply-To: <20251003121520.8176-5-farbere@amazon.com> Message-ID: <2025100609-exposable-subside-4c76@gregkh> MIME-Version: 1.0 Content-Type: text/plain; charset=ANSI_X3.4-1968 Content-Transfer-Encoding: 8bit X-stable: commit X-Patchwork-Hint: ignore X-Mailman-Approved-At: Mon, 06 Oct 2025 12:41:50 +0000 X-BeenThere: amd-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Discussion list for AMD gfx List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: amd-gfx-bounces@lists.freedesktop.org Sender: "amd-gfx" This is a note to let you know that I've just added the patch titled minmax: fix up min3() and max3() too to the 6.1-stable tree which can be found at: http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary The filename of the patch is: minmax-fix-up-min3-and-max3-too.patch and it can be found in the queue-6.1 subdirectory. If you, or anyone else, feels it should not be added to the stable tree, please let know about it. >From stable+bounces-183175-greg=kroah.com@vger.kernel.org Fri Oct 3 14:17:52 2025 From: Eliav Farber Date: Fri, 3 Oct 2025 12:15:13 +0000 Subject: minmax: fix up min3() and max3() too To: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Cc: Linus Torvalds , David Laight , Arnd Bergmann Message-ID: <20251003121520.8176-5-farbere@amazon.com> From: Linus Torvalds [ Upstream commit 21b136cc63d2a9ddd60d4699552b69c214b32964 ] David Laight pointed out that we should deal with the min3() and max3() mess too, which still does excessive expansion. And our current macros are actually rather broken. In particular, the macros did this: #define min3(x, y, z) min((typeof(x))min(x, y), z) #define max3(x, y, z) max((typeof(x))max(x, y), z) and that not only is a nested expansion of possibly very complex arguments with all that involves, the typing with that "typeof()" cast is completely wrong. For example, imagine what happens in max3() if 'x' happens to be a 'unsigned char', but 'y' and 'z' are 'unsigned long'. The types are compatible, and there's no warning - but the result is just random garbage. No, I don't think we've ever hit that issue in practice, but since we now have sane infrastructure for doing this right, let's just use it. It fixes any excessive expansion, and also avoids these kinds of broken type issues. Requested-by: David Laight Acked-by: Arnd Bergmann Signed-off-by: Linus Torvalds Signed-off-by: Eliav Farber Signed-off-by: Greg Kroah-Hartman --- include/linux/minmax.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -152,13 +152,20 @@ #define umax(x, y) \ __careful_cmp(max, (x) + 0u + 0ul + 0ull, (y) + 0u + 0ul + 0ull) +#define __careful_op3(op, x, y, z, ux, uy, uz) ({ \ + __auto_type ux = (x); __auto_type uy = (y);__auto_type uz = (z);\ + BUILD_BUG_ON_MSG(!__types_ok3(x,y,z,ux,uy,uz), \ + #op"3("#x", "#y", "#z") signedness error"); \ + __cmp(op, ux, __cmp(op, uy, uz)); }) + /** * min3 - return minimum of three values * @x: first value * @y: second value * @z: third value */ -#define min3(x, y, z) min((typeof(x))min(x, y), z) +#define min3(x, y, z) \ + __careful_op3(min, x, y, z, __UNIQUE_ID(x_), __UNIQUE_ID(y_), __UNIQUE_ID(z_)) /** * max3 - return maximum of three values @@ -166,7 +173,8 @@ * @y: second value * @z: third value */ -#define max3(x, y, z) max((typeof(x))max(x, y), z) +#define max3(x, y, z) \ + __careful_op3(max, x, y, z, __UNIQUE_ID(x_), __UNIQUE_ID(y_), __UNIQUE_ID(z_)) /** * min_not_zero - return the minimum that is _not_ zero, unless both are zero Patches currently in stable-queue which might be from farbere@amazon.com are queue-6.1/minmax-improve-macro-expansion-and-type-checking.patch queue-6.1/minmax.h-simplify-the-variants-of-clamp.patch queue-6.1/minmax.h-move-all-the-clamp-definitions-after-the-min-max-ones.patch queue-6.1/minmax-don-t-use-max-in-situations-that-want-a-c-constant-expression.patch queue-6.1/minmax.h-remove-some-defines-that-are-only-expanded-once.patch queue-6.1/minmax.h-use-build_bug_on_msg-for-the-lo-hi-test-in-clamp.patch queue-6.1/minmax-simplify-min-max-clamp-implementation.patch queue-6.1/minmax.h-add-whitespace-around-operators-and-after-commas.patch queue-6.1/minmax-fix-up-min3-and-max3-too.patch queue-6.1/minmax.h-reduce-the-define-expansion-of-min-max-and-clamp.patch queue-6.1/minmax.h-update-some-comments.patch