From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 56625306B3C; Mon, 22 Sep 2025 12:27:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758544039; cv=none; b=Ggh2C7fTtfbu6PgiuhyLIUm6/7cEx6wMhIL5cISofdL41Rn8j8X6UJKMUCERQsky3wNlxQKyLXlX7QRcT69gTLgCluNAuoJs/d17VuFxJaaqD6uuSfQZDH2uDiN6JP006mYVonxN2WG5G4Xicv+SLyURXR8NA/0EhY600NtUfUY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758544039; c=relaxed/simple; bh=92DBN3bMcFz8vIRdBZNpPrekIoZC1bozepvmjOs0AUA=; h=Subject:To:Cc:From:Date:In-Reply-To:Message-ID:MIME-Version: Content-Type; b=NIKNc3sVD6ODhWsaW5oDnRTiYDxsnFTPwEKFTDOmzMERdMfisSUzcWgNV9fyjpvrqdEQJkYJBc9aDawWWYBVmGNhldpFd+h5KQXKbfANnUuc7LzdzB1/6NTP9NeBP/bhhFHkuudcjXlJTZQMj6pQ1AsDCTBe+QDB39hpMeadM2k= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=V84LpWZQ; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="V84LpWZQ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 162ECC4CEF7; Mon, 22 Sep 2025 12:27:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1758544038; bh=92DBN3bMcFz8vIRdBZNpPrekIoZC1bozepvmjOs0AUA=; h=Subject:To:Cc:From:Date:In-Reply-To:From; b=V84LpWZQKz0fN+oNTS7CQG+5BaEaSfOrRCwA02aLtlfFAJAXI3Lfnb55+cE1RtZAe TuEEGyHcpkKxuUx+8hQ8++E0yiQ/OlSgOmgOy0cGiSqN/PNkCtehWG2L8Un/Zz4F8E QXWtUhRm+hUsIfl9CZn9LmoqUgFDEnD6xm89KgqM= Subject: Patch "minmax: fix up min3() and max3() too" has been added to the 6.6-stable tree To: David.Laight@ACULAB.COM,David.Laight@aculab.com,Rodrigo.Siqueira@amd.com,Xinhui.Pan@amd.com,agk@redhat.com,airlied@gmail.com,akpm@linux-foundation.org,alexander.deucher@amd.com,alexandre.torgue@foss.st.com,amd-gfx@lists.freedesktop.org,andriy.shevchenko@linux.intel.com,anton.ivanov@cambridgegreys.com,arnd@kernel.org,artur.paszkiewicz@intel.com,bp@alien8.de,christian.koenig@amd.com,clm@fb.com,daniel@ffwll.ch,dave.hansen@linux.intel.com,davem@davemloft.net,dm-devel@lists.linux.dev,dmitry.torokhov@gmail.com,dri-devel@lists.freedesktop.org,dsahern@kernel.org,dsterba@suse.com,edumazet@google.com,evan.quan@amd.com,farbere@amazon.com,gregkh@linuxfoundation.org,harry.wentland@amd.com,hdegoede@redhat.com,hpa@zytor.com,ilpo.jarvinen@linux.intel.com,james.morse@arm.com,jdelvare@suse.com,jejb@linux.ibm.com,jernej.skrabec@gmail.com,joabreu@synopsys.com,johannes@sipsolutions.net,josef@toxicpanda.com,keescook@chromium.org,krzysztof.kozlowski@linaro.org,kuba@kernel.org,linus.walleij@linaro.org,linux-arm-kernel@lists.infradead.org,linux-mm@kvack.org,linux-staging@lists.linux.dev,linux-stm32@st-md-mailman.stormreply.com,linux-sunxi@lists.linux.dev,linux-um@lists.infradead.org,linux@rasmusvillemoes.dk,linux@roeck-us.net,luc.vanoostenryck@gmail.com,luto@kernel.org,maarten.lankhorst@linux.intel.com,mailhol.vincent@wanadoo.fr,malattia@linux.it,markgross@kernel.org,martin.petersen@oracle.com,mchehab@kernel.org,mcoquelin.stm32@gmail.com,mhiramat@kernel.org,minchan@kernel.org,mingo@redhat.com,mkl@pengutronix.de,mripard@kernel.org,pabeni@redhat.com,peterz@infradead.org,pmladek@suse.com,qiuxu.zhuo@intel.com,richard@nod.at,rostedt@goodmis.org,rric@kernel.org,sakari.ailus@linux.intel.com,samuel@sholland.org,senozhatsky@chromium.org,shuah@kernel.org,snitzer@kernel.org,sunpeng.li@amd.com,tglx@linutronix.de,tony.luck@intel.com,torvalds@linux-foundation.org,tzimmermann@suse.de,wad@chromium.org,wens@csie.org,wg@grandegger.com,x86@kernel.org Cc: From: Date: Mon, 22 Sep 2025 14:26:59 +0200 In-Reply-To: <20250922103241.16213-9-farbere@amazon.com> Message-ID: <2025092259-reprocess-reward-329d@gregkh> Precedence: bulk X-Mailing-List: dm-devel@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=ANSI_X3.4-1968 Content-Transfer-Encoding: 8bit X-stable: commit X-Patchwork-Hint: ignore 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.6-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.6 subdirectory. If you, or anyone else, feels it should not be added to the stable tree, please let know about it. >From prvs=353d6d59a=farbere@amazon.com Mon Sep 22 12:37:42 2025 From: Eliav Farber Date: Mon, 22 Sep 2025 10:32:34 +0000 Subject: minmax: fix up min3() and max3() too To: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Cc: Linus Torvalds , David Laight Message-ID: <20250922103241.16213-9-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.6/minmax-don-t-use-max-in-situations-that-want-a-c-constant-expression.patch queue-6.6/minmax-make-generic-min-and-max-macros-available-everywhere.patch queue-6.6/minmax-fix-up-min3-and-max3-too.patch queue-6.6/minmax-add-a-few-more-min_t-max_t-users.patch queue-6.6/minmax-improve-macro-expansion-and-type-checking.patch queue-6.6/minmax-avoid-overly-complicated-constant-expressions-in-vm-code.patch queue-6.6/minmax-simplify-min-max-clamp-implementation.patch queue-6.6/minmax-simplify-and-clarify-min_t-max_t-implementation.patch