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 28005265292; Fri, 17 Oct 2025 08:05:45 +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=1760688346; cv=none; b=kQ955Ubk+gkKuKmScgSVS06drGjcbdQY44PoNqB4lszbowh1DwqIpWCso/BQT/g85jdbre4D710lMr3by9RzB+fud9Qvv522EzhiIFgA7INOfGL+lShcSehy7liQ52ldwGl89LR6eVOQ0cpXWTPEYCf3IsPXLrHy+Sxa/zQAC2g= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760688346; c=relaxed/simple; bh=jQhy/ox3fIOhCY7knzkIZUyacLZTGHWrr3u52zYJAkI=; h=Subject:To:Cc:From:Date:In-Reply-To:Message-ID:MIME-Version: Content-Type; b=jtQlsKW4HflgBGSB3svF4gKwWoOx1LtJUhoT+aqOhZ8Li73ukEq+SQvSVCD1gzeF9nU/8BiZjDqOj09t8ZYbu5x8o4QglzfJRODyqJlZ0oxQgxCCYYcZD6pMdUaFCNvdUy6IaN1TAM+YgkmH8T4V7MeByZxfSBzJC1U/civ8mBQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=jfsQ3+mn; 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="jfsQ3+mn" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 34845C4CEE7; Fri, 17 Oct 2025 08:05:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1760688345; bh=jQhy/ox3fIOhCY7knzkIZUyacLZTGHWrr3u52zYJAkI=; h=Subject:To:Cc:From:Date:In-Reply-To:From; b=jfsQ3+mnT/1hXaKowyNh4ESs5gv7XWT4llLVtqMbxf719r6AbXjDWmR8zBVavKXTr DspwYEGBxW2V4PZWTnFyaExmgP7lqPD2tc7Ax5IkWv9QpHXRROMLLI3Ym8cvq16VYc dXU+JcHgFA2fnbNWSnt0GfCEyA/6Q/jr15A9yGug= Subject: Patch "minmax: avoid overly complicated constant expressions in VM code" has been added to the 5.15-stable tree To: David.Laight@ACULAB.COM,David.Laight@aculab.com,adilger.kernel@dilger.ca,agk@redhat.com,airlied@linux.ie,akpm@linux-foundation.org,amd-gfx@lists.freedesktop.org,andriy.shevchenko@linux.intel.com,anton.ivanov@cambridgegreys.com,bp@alien8.de,clm@fb.com,coreteam@netfilter.org,daniel@ffwll.ch,dave.hansen@linux.intel.com,davem@davemloft.net,dm-devel@redhat.com,dmitry.torokhov@gmail.com,dri-devel@lists.freedesktop.org,dsahern@kernel.org,dsterba@suse.com,dushistov@mail.ru,farbere@amazon.com,freedreno@lists.freedesktop.org,fw@strlen.de,gregkh@linuxfoundation.org,hdegoede@redhat.com,herve.codina@bootlin.com,hpa@zytor.com,jack@suse.com,james.morse@arm.com,jdelvare@suse.com,jdike@addtoit.com,jejb@linux.ibm.com,jernej.skrabec@gmail.com,jmaloy@redhat.com,josef@toxicpanda.com,kadlec@netfilter.org,krzysztof.kozlowski@canonical.com,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,lorenzo.stoakes@oracle.com,luc.vanoostenryck@gmail.com,luto@kernel.org,maarten.lankhorst@linux.intel.com,malattia@linux.it,martin.petersen@oracle.com,maz@kernel.org,mcoquelin.stm32@gmail.com,mgross@linux.intel.com,minchan@kernel.org,mingo@redhat.com,mripard@kernel.org,ngupta@vflare.org,pablo@netfilter.org,peterz@infradead.org,pmladek@suse.com,qiuxu.zhuo@intel.com,quic_akhilpo@quicinc.com,richard@nod.at,robdclark@gmail.com,rostedt@goodmis.org,rric@kernel.org,ruanjinjie@huawei.com,sakari.ailus@linux.intel.com,sashal@kernel.org,sean@poorly.run,senozhatsky@chromium.org,shuah@kernel.org,snitzer@redhat.com,tglx@linutronix.de,tipc-discussion@lists.sourceforge.net,tony.luck@intel.com,torvalds@linux-foundation.org,tytso@mit.edu,tzimmermann@suse.de,wens@csie.org,willy@infradead.org,x86@kernel.org,ying.xue@windriver.com,yoshfuji@linux-ipv6.org Cc: From: Date: Fri, 17 Oct 2025 10:05:09 +0200 In-Reply-To: <20251008152946.29285-6-farbere@amazon.com> Message-ID: <2025101709-handcart-hunger-c414@gregkh> Precedence: bulk X-Mailing-List: linux-staging@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: avoid overly complicated constant expressions in VM code to the 5.15-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-avoid-overly-complicated-constant-expressions-in-vm-code.patch and it can be found in the queue-5.15 subdirectory. If you, or anyone else, feels it should not be added to the stable tree, please let know about it. >From linux-staging+bounces-34820-greg=kroah.com@lists.linux.dev Wed Oct 8 17:33:38 2025 From: Eliav Farber Date: Wed, 8 Oct 2025 15:29:30 +0000 Subject: minmax: avoid overly complicated constant expressions in VM code To: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Cc: Linus Torvalds , Lorenzo Stoakes , David Laight Message-ID: <20251008152946.29285-6-farbere@amazon.com> From: Linus Torvalds [ Upstream commit 3a7e02c040b130b5545e4b115aada7bacd80a2b6 ] The minmax infrastructure is overkill for simple constants, and can cause huge expansions because those simple constants are then used by other things. For example, 'pageblock_order' is a core VM constant, but because it was implemented using 'min_t()' and all the type-checking that involves, it actually expanded to something like 2.5kB of preprocessor noise. And when that simple constant was then used inside other expansions: #define pageblock_nr_pages (1UL << pageblock_order) #define pageblock_start_pfn(pfn) ALIGN_DOWN((pfn), pageblock_nr_pages) and we then use that inside a 'max()' macro: case ISOLATE_SUCCESS: update_cached = false; last_migrated_pfn = max(cc->zone->zone_start_pfn, pageblock_start_pfn(cc->migrate_pfn - 1)); the end result was that one statement expanding to 253kB in size. There are probably other cases of this, but this one case certainly stood out. I've added 'MIN_T()' and 'MAX_T()' macros for this kind of "core simple constant with specific type" use. These macros skip the type checking, and as such need to be very sparingly used only for obvious cases that have active issues like this. Reported-by: Lorenzo Stoakes Link: https://lore.kernel.org/all/36aa2cad-1db1-4abf-8dd2-fb20484aabc3@lucifer.local/ Cc: David Laight Signed-off-by: Linus Torvalds Signed-off-by: Eliav Farber Signed-off-by: Greg Kroah-Hartman --- include/linux/minmax.h | 7 +++++++ 1 file changed, 7 insertions(+) --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -270,4 +270,11 @@ static inline bool in_range32(u32 val, u #define swap(a, b) \ 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. + */ +#define MIN_T(type,a,b) __cmp(min,(type)(a),(type)(b)) +#define MAX_T(type,a,b) __cmp(max,(type)(a),(type)(b)) + #endif /* _LINUX_MINMAX_H */ Patches currently in stable-queue which might be from farbere@amazon.com are queue-5.15/minmax-fix-indentation-of-__cmp_once-and-__clamp_once.patch queue-5.15/minmax-add-in_range-macro.patch queue-5.15/minmax-deduplicate-__unconst_integer_typeof.patch queue-5.15/minmax-avoid-overly-complicated-constant-expressions-in-vm-code.patch queue-5.15/minmax-introduce-min-max-_array.patch