From nobody Thu Oct 2 01:01:46 2025 Received: from fra-out-009.esa.eu-central-1.outbound.mail-perimeter.amazon.com (fra-out-009.esa.eu-central-1.outbound.mail-perimeter.amazon.com [3.64.237.68]) (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 9ABD930CDB8; Mon, 29 Sep 2025 17:18:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=3.64.237.68 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759166285; cv=none; b=eodcCcW2wOSpDFTMlfNM2ZbF4iExauSNTUMH1PtJWH1oViodYcV9keiePmMJbNn/g7TvctvxkpybdSLkm5PAGAYElxnfqXhFCORYWAIHQFfR41B4kDa0gjT6gKVgddcUO/7m9Xyrkqv/DMNrBrgagHNwEmWdiuTF1gTzw5GNgow= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759166285; c=relaxed/simple; bh=XRFqIo8nVhb9sGRI+OKBBMjkw9flg1Z1+BQl2snqXn8=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=J4idr8cfL3+Yv5Mq70U/JmsSATYnWm8QK1yyA2YCyS39IqZlZXmLLVHpg3Xi6llDpx2vD6yisEdafzCrYZMmfxeORbzT0vmHmcQaLQnaWAOxP/0ZGYXnfMyHQaG1QRYcySvfWg99RnXN8K1ZfFJWWCqelUZ4aICJUZyyL1SduHk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.com; dkim=pass (2048-bit key) header.d=amazon.com header.i=@amazon.com header.b=h973qAW7; arc=none smtp.client-ip=3.64.237.68 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.com header.i=@amazon.com header.b="h973qAW7" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazoncorp2; t=1759166282; x=1790702282; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=rqrd4HBIqCAHTza6BXrgC5ifSxp5e9NtPs/IrhJJ2vk=; b=h973qAW72sP7KKcjI+UD+Eg7jYVVRA4GbrxeKnwE0SCp5Izq4kO+wt6c jy/LF1RzyH5eqARxgrXp5iuogJ5p89CRAaDot4ne0Qdi09H2Ykv9Hxn1q sPJNs2mQttyfS7cgyY49ACgR3dVF5HJSnP/2cTnwqRo1Q85mThzXO4zLN OkVFJJMXjWuVX/0eaQHRVC322mIV728IQTkafBFoaHyAXT3aKV4SKshrS sUgOhdla5mApcK+DldZU6FFjydZSA1L51esRhOY1lmu2O0oCC7QqSI8dD k5G6OjlX6N10JiQussGONpoR82rg8sSjzQJlisUnwe6nX17IRUlCt2KGG Q==; X-CSE-ConnectionGUID: +tQpsMcCR7GoFhnyCGTQXg== X-CSE-MsgGUID: cdNuywwNT2S4xAFNMk/9dg== X-Ironport-Invalid-End-Of-Message: True X-IronPort-AV: E=Sophos;i="6.18,302,1751241600"; d="scan'208";a="2740057" Received: from ip-10-6-3-216.eu-central-1.compute.internal (HELO smtpout.naws.eu-central-1.prod.farcaster.email.amazon.dev) ([10.6.3.216]) by internal-fra-out-009.esa.eu-central-1.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Sep 2025 17:17:49 +0000 Received: from EX19MTAEUC001.ant.amazon.com [54.240.197.225:3806] by smtpin.naws.eu-central-1.prod.farcaster.email.amazon.dev [10.0.22.27:2525] with esmtp (Farcaster) id d42c28d1-17f7-4049-9fd3-7083080be307; Mon, 29 Sep 2025 17:17:49 +0000 (UTC) X-Farcaster-Flow-ID: d42c28d1-17f7-4049-9fd3-7083080be307 Received: from EX19D018EUA004.ant.amazon.com (10.252.50.85) by EX19MTAEUC001.ant.amazon.com (10.252.51.193) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Mon, 29 Sep 2025 17:17:47 +0000 Received: from dev-dsk-farbere-1a-46ecabed.eu-west-1.amazon.com (172.19.116.181) by EX19D018EUA004.ant.amazon.com (10.252.50.85) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Mon, 29 Sep 2025 17:17:43 +0000 From: Eliav Farber To: , , , , , , , , CC: Linus Torvalds , David Laight , Lorenzo Stoakes Subject: [PATCH v2 01/12 6.6.y] minmax: make generic MIN() and MAX() macros available everywhere Date: Mon, 29 Sep 2025 17:17:22 +0000 Message-ID: <20250929171733.20671-2-farbere@amazon.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20250929171733.20671-1-farbere@amazon.com> References: <20250929171733.20671-1-farbere@amazon.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: EX19D040UWA003.ant.amazon.com (10.13.139.6) To EX19D018EUA004.ant.amazon.com (10.252.50.85) Content-Type: text/plain; charset="utf-8" From: Linus Torvalds [ Upstream commit 1a251f52cfdc417c84411a056bc142cbd77baef4 ] This just standardizes the use of MIN() and MAX() macros, with the very traditional semantics. The goal is to use these for C constant expressions and for top-level / static initializers, and so be able to simplify the min()/max() macros. These macro names were used by various kernel code - they are very traditional, after all - and all such users have been fixed up, with a few different approaches: - trivial duplicated macro definitions have been removed Note that 'trivial' here means that it's obviously kernel code that already included all the major kernel headers, and thus gets the new generic MIN/MAX macros automatically. - non-trivial duplicated macro definitions are guarded with #ifndef This is the "yes, they define their own versions, but no, the include situation is not entirely obvious, and maybe they don't get the generic version automatically" case. - strange use case #1 A couple of drivers decided that the way they want to describe their versioning is with #define MAJ 1 #define MIN 2 #define DRV_VERSION __stringify(MAJ) "." __stringify(MIN) which adds zero value and I just did my Alexander the Great impersonation, and rewrote that pointless Gordian knot as #define DRV_VERSION "1.2" instead. - strange use case #2 A couple of drivers thought that it's a good idea to have a random 'MIN' or 'MAX' define for a value or index into a table, rather than the traditional macro that takes arguments. These values were re-written as C enum's instead. The new function-line macros only expand when followed by an open parenthesis, and thus don't clash with enum use. Happily, there weren't really all that many of these cases, and a lot of users already had the pattern of using '#ifndef' guarding (or in one case just using '#undef MIN') before defining their own private version that does the same thing. I left such cases alone. Cc: David Laight Cc: Lorenzo Stoakes Signed-off-by: Linus Torvalds Signed-off-by: Eliav Farber --- v2: - Add missing #ifndef guards around MIN/MAX macro definitions to avoid redefinition errors in: * drivers/gpu/drm/amd/amdgpu/amdgpu.h * drivers/gpu/drm/amd/pm/swsmu/smu11/sienna_cichlid_ppt.c * drivers/gpu/drm/amd/pm/swsmu/smu13/smu_v13_0_0_ppt.c * drivers/gpu/drm/amd/pm/swsmu/smu13/smu_v13_0_7_ppt.c arch/um/drivers/mconsole_user.c | 2 ++ drivers/edac/skx_common.h | 1 - drivers/gpu/drm/amd/amdgpu/amdgpu.h | 2 ++ .../drm/amd/display/modules/hdcp/hdcp_ddc.c | 2 ++ .../drm/amd/pm/powerplay/hwmgr/ppevvmath.h | 14 +++++++---- .../amd/pm/swsmu/smu11/sienna_cichlid_ppt.c | 2 ++ .../drm/amd/pm/swsmu/smu13/smu_v13_0_0_ppt.c | 3 +++ .../drm/amd/pm/swsmu/smu13/smu_v13_0_7_ppt.c | 3 +++ drivers/gpu/drm/radeon/evergreen_cs.c | 2 ++ drivers/hwmon/adt7475.c | 24 +++++++++---------- drivers/media/dvb-frontends/stv0367_priv.h | 3 +++ drivers/net/fjes/fjes_main.c | 4 +--- drivers/nfc/pn544/i2c.c | 2 -- drivers/platform/x86/sony-laptop.c | 1 - drivers/scsi/isci/init.c | 6 +---- .../pci/hive_isp_css_include/math_support.h | 5 ---- include/linux/minmax.h | 2 ++ kernel/trace/preemptirq_delay_test.c | 2 -- lib/btree.c | 1 - lib/decompress_unlzma.c | 2 ++ mm/zsmalloc.c | 2 -- tools/testing/selftests/mm/mremap_test.c | 2 ++ tools/testing/selftests/seccomp/seccomp_bpf.c | 2 ++ 23 files changed, 51 insertions(+), 38 deletions(-) diff --git a/arch/um/drivers/mconsole_user.c b/arch/um/drivers/mconsole_use= r.c index e24298a734be..a04cd13c6315 100644 --- a/arch/um/drivers/mconsole_user.c +++ b/arch/um/drivers/mconsole_user.c @@ -71,7 +71,9 @@ static struct mconsole_command *mconsole_parse(struct mc_= request *req) return NULL; } =20 +#ifndef MIN #define MIN(a,b) ((a)<(b) ? (a):(b)) +#endif =20 #define STRINGX(x) #x #define STRING(x) STRINGX(x) diff --git a/drivers/edac/skx_common.h b/drivers/edac/skx_common.h index 2ea4d1d1fbef..1fa31a6fdfcd 100644 --- a/drivers/edac/skx_common.h +++ b/drivers/edac/skx_common.h @@ -45,7 +45,6 @@ #define I10NM_NUM_CHANNELS MAX(I10NM_NUM_DDR_CHANNELS, I10NM_NUM_HBM_CHANN= ELS) #define I10NM_NUM_DIMMS MAX(I10NM_NUM_DDR_DIMMS, I10NM_NUM_HBM_DIMMS) =20 -#define MAX(a, b) ((a) > (b) ? (a) : (b)) #define NUM_IMC MAX(SKX_NUM_IMC, I10NM_NUM_IMC) #define NUM_CHANNELS MAX(SKX_NUM_CHANNELS, I10NM_NUM_CHANNELS) #define NUM_DIMMS MAX(SKX_NUM_DIMMS, I10NM_NUM_DIMMS) diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu.h b/drivers/gpu/drm/amd/amdg= pu/amdgpu.h index c5d706a4c7b4..78426f8c5420 100644 --- a/drivers/gpu/drm/amd/amdgpu/amdgpu.h +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu.h @@ -1285,7 +1285,9 @@ int emu_soc_asic_init(struct amdgpu_device *adev); for (i =3D ffs(inst_mask); i-- !=3D 0; \ i =3D ffs(inst_mask & BIT_MASK_UPPER(i + 1))) =20 +#ifndef MIN #define MIN(X, Y) ((X) < (Y) ? (X) : (Y)) +#endif =20 /* Common functions */ bool amdgpu_device_has_job_running(struct amdgpu_device *adev); diff --git a/drivers/gpu/drm/amd/display/modules/hdcp/hdcp_ddc.c b/drivers/= gpu/drm/amd/display/modules/hdcp/hdcp_ddc.c index 1b2df97226a3..40286e8dd4e1 100644 --- a/drivers/gpu/drm/amd/display/modules/hdcp/hdcp_ddc.c +++ b/drivers/gpu/drm/amd/display/modules/hdcp/hdcp_ddc.c @@ -25,7 +25,9 @@ =20 #include "hdcp.h" =20 +#ifndef MIN #define MIN(a, b) ((a) < (b) ? (a) : (b)) +#endif #define HDCP_I2C_ADDR 0x3a /* 0x74 >> 1*/ #define KSV_READ_SIZE 0xf /* 0x6803b - 0x6802c */ #define HDCP_MAX_AUX_TRANSACTION_SIZE 16 diff --git a/drivers/gpu/drm/amd/pm/powerplay/hwmgr/ppevvmath.h b/drivers/g= pu/drm/amd/pm/powerplay/hwmgr/ppevvmath.h index 6f54c410c2f9..409aeec6baa9 100644 --- a/drivers/gpu/drm/amd/pm/powerplay/hwmgr/ppevvmath.h +++ b/drivers/gpu/drm/amd/pm/powerplay/hwmgr/ppevvmath.h @@ -22,12 +22,18 @@ */ #include =20 -#define SHIFT_AMOUNT 16 /* We multiply all original integers with 2^SHIFT_= AMOUNT to get the fInt representation */ +enum ppevvmath_constants { + /* We multiply all original integers with 2^SHIFT_AMOUNT to get the fInt = representation */ + SHIFT_AMOUNT =3D 16, =20 -#define PRECISION 5 /* Change this value to change the number of decimal p= laces in the final output - 5 is a good default */ + /* Change this value to change the number of decimal places in the final = output - 5 is a good default */ + PRECISION =3D 5, =20 -#define SHIFTED_2 (2 << SHIFT_AMOUNT) -#define MAX (1 << (SHIFT_AMOUNT - 1)) - 1 /* 32767 - Might change in the f= uture */ + SHIFTED_2 =3D (2 << SHIFT_AMOUNT), + + /* 32767 - Might change in the future */ + MAX =3D (1 << (SHIFT_AMOUNT - 1)) - 1, +}; =20 /* -----------------------------------------------------------------------= -------- * NEW TYPE - fINT diff --git a/drivers/gpu/drm/amd/pm/swsmu/smu11/sienna_cichlid_ppt.c b/driv= ers/gpu/drm/amd/pm/swsmu/smu11/sienna_cichlid_ppt.c index a7f4f82d23b4..2fdb982e70ef 100644 --- a/drivers/gpu/drm/amd/pm/swsmu/smu11/sienna_cichlid_ppt.c +++ b/drivers/gpu/drm/amd/pm/swsmu/smu11/sienna_cichlid_ppt.c @@ -2082,7 +2082,9 @@ static int sienna_cichlid_display_disable_memory_cloc= k_switch(struct smu_context return ret; } =20 +#ifndef MAX #define MAX(a, b) ((a) > (b) ? (a) : (b)) +#endif =20 static int sienna_cichlid_update_pcie_parameters(struct smu_context *smu, uint8_t pcie_gen_cap, diff --git a/drivers/gpu/drm/amd/pm/swsmu/smu13/smu_v13_0_0_ppt.c b/drivers= /gpu/drm/amd/pm/swsmu/smu13/smu_v13_0_0_ppt.c index 4022dd44ebb2..fe6c42a736d0 100644 --- a/drivers/gpu/drm/amd/pm/swsmu/smu13/smu_v13_0_0_ppt.c +++ b/drivers/gpu/drm/amd/pm/swsmu/smu13/smu_v13_0_0_ppt.c @@ -1696,7 +1696,10 @@ static int smu_v13_0_0_get_thermal_temperature_range= (struct smu_context *smu, return 0; } =20 +#ifndef MAX #define MAX(a, b) ((a) > (b) ? (a) : (b)) +#endif + static ssize_t smu_v13_0_0_get_gpu_metrics(struct smu_context *smu, void **table) { diff --git a/drivers/gpu/drm/amd/pm/swsmu/smu13/smu_v13_0_7_ppt.c b/drivers= /gpu/drm/amd/pm/swsmu/smu13/smu_v13_0_7_ppt.c index e1521d3a5e0c..df99d185cf8e 100644 --- a/drivers/gpu/drm/amd/pm/swsmu/smu13/smu_v13_0_7_ppt.c +++ b/drivers/gpu/drm/amd/pm/swsmu/smu13/smu_v13_0_7_ppt.c @@ -1674,7 +1674,10 @@ static int smu_v13_0_7_get_thermal_temperature_range= (struct smu_context *smu, return 0; } =20 +#ifndef MAX #define MAX(a, b) ((a) > (b) ? (a) : (b)) +#endif + static ssize_t smu_v13_0_7_get_gpu_metrics(struct smu_context *smu, void **table) { diff --git a/drivers/gpu/drm/radeon/evergreen_cs.c b/drivers/gpu/drm/radeon= /evergreen_cs.c index 820c2c3641d3..1311f10fad66 100644 --- a/drivers/gpu/drm/radeon/evergreen_cs.c +++ b/drivers/gpu/drm/radeon/evergreen_cs.c @@ -33,8 +33,10 @@ #include "evergreen_reg_safe.h" #include "cayman_reg_safe.h" =20 +#ifndef MIN #define MAX(a,b) (((a)>(b))?(a):(b)) #define MIN(a,b) (((a)<(b))?(a):(b)) +#endif =20 #define REG_SAFE_BM_SIZE ARRAY_SIZE(evergreen_reg_safe_bm) =20 diff --git a/drivers/hwmon/adt7475.c b/drivers/hwmon/adt7475.c index 14b2547adae8..ce0ab4e4e41a 100644 --- a/drivers/hwmon/adt7475.c +++ b/drivers/hwmon/adt7475.c @@ -22,23 +22,23 @@ #include =20 /* Indexes for the sysfs hooks */ - -#define INPUT 0 -#define MIN 1 -#define MAX 2 -#define CONTROL 3 -#define OFFSET 3 -#define AUTOMIN 4 -#define THERM 5 -#define HYSTERSIS 6 - +enum adt_sysfs_id { + INPUT =3D 0, + MIN =3D 1, + MAX =3D 2, + CONTROL =3D 3, + OFFSET =3D 3, // Dup + AUTOMIN =3D 4, + THERM =3D 5, + HYSTERSIS =3D 6, /* * These are unique identifiers for the sysfs functions - unlike the * numbers above, these are not also indexes into an array */ + ALARM =3D 9, + FAULT =3D 10, +}; =20 -#define ALARM 9 -#define FAULT 10 =20 /* 7475 Common Registers */ =20 diff --git a/drivers/media/dvb-frontends/stv0367_priv.h b/drivers/media/dvb= -frontends/stv0367_priv.h index 617f605947b2..7f056d1cce82 100644 --- a/drivers/media/dvb-frontends/stv0367_priv.h +++ b/drivers/media/dvb-frontends/stv0367_priv.h @@ -25,8 +25,11 @@ #endif =20 /* MACRO definitions */ +#ifndef MIN #define MAX(X, Y) ((X) >=3D (Y) ? (X) : (Y)) #define MIN(X, Y) ((X) <=3D (Y) ? (X) : (Y)) +#endif + #define INRANGE(X, Y, Z) \ ((((X) <=3D (Y)) && ((Y) <=3D (Z))) || \ (((Z) <=3D (Y)) && ((Y) <=3D (X))) ? 1 : 0) diff --git a/drivers/net/fjes/fjes_main.c b/drivers/net/fjes/fjes_main.c index 2513be6d4e11..cfcf0f7d9d90 100644 --- a/drivers/net/fjes/fjes_main.c +++ b/drivers/net/fjes/fjes_main.c @@ -14,9 +14,7 @@ #include "fjes.h" #include "fjes_trace.h" =20 -#define MAJ 1 -#define MIN 2 -#define DRV_VERSION __stringify(MAJ) "." __stringify(MIN) +#define DRV_VERSION "1.2" #define DRV_NAME "fjes" char fjes_driver_name[] =3D DRV_NAME; char fjes_driver_version[] =3D DRV_VERSION; diff --git a/drivers/nfc/pn544/i2c.c b/drivers/nfc/pn544/i2c.c index 3f6d74832bac..be566ca7bae9 100644 --- a/drivers/nfc/pn544/i2c.c +++ b/drivers/nfc/pn544/i2c.c @@ -126,8 +126,6 @@ struct pn544_i2c_fw_secure_blob { #define PN544_FW_CMD_RESULT_COMMAND_REJECTED 0xE0 #define PN544_FW_CMD_RESULT_CHUNK_ERROR 0xE6 =20 -#define MIN(X, Y) ((X) < (Y) ? (X) : (Y)) - #define PN544_FW_WRITE_BUFFER_MAX_LEN 0x9f7 #define PN544_FW_I2C_MAX_PAYLOAD PN544_HCI_I2C_LLC_MAX_SIZE #define PN544_FW_I2C_WRITE_FRAME_HEADER_LEN 8 diff --git a/drivers/platform/x86/sony-laptop.c b/drivers/platform/x86/sony= -laptop.c index 9569f11dec8c..bd7323cf9a97 100644 --- a/drivers/platform/x86/sony-laptop.c +++ b/drivers/platform/x86/sony-laptop.c @@ -757,7 +757,6 @@ static union acpi_object *__call_snc_method(acpi_handle= handle, char *method, return result; } =20 -#define MIN(a, b) (a > b ? b : a) static int sony_nc_buffer_call(acpi_handle handle, char *name, u64 *value, void *buffer, size_t buflen) { diff --git a/drivers/scsi/isci/init.c b/drivers/scsi/isci/init.c index 6277162a028b..6eefa7e5f4cf 100644 --- a/drivers/scsi/isci/init.c +++ b/drivers/scsi/isci/init.c @@ -65,11 +65,7 @@ #include "task.h" #include "probe_roms.h" =20 -#define MAJ 1 -#define MIN 2 -#define BUILD 0 -#define DRV_VERSION __stringify(MAJ) "." __stringify(MIN) "." \ - __stringify(BUILD) +#define DRV_VERSION "1.2.0" =20 MODULE_VERSION(DRV_VERSION); =20 diff --git a/drivers/staging/media/atomisp/pci/hive_isp_css_include/math_su= pport.h b/drivers/staging/media/atomisp/pci/hive_isp_css_include/math_suppo= rt.h index a444ec14ff9d..1c17a87a8572 100644 --- a/drivers/staging/media/atomisp/pci/hive_isp_css_include/math_support.h +++ b/drivers/staging/media/atomisp/pci/hive_isp_css_include/math_support.h @@ -31,11 +31,6 @@ /* A =3D> B */ #define IMPLIES(a, b) (!(a) || (b)) =20 -/* for preprocessor and array sizing use MIN and MAX - otherwise use min and max */ -#define MAX(a, b) (((a) > (b)) ? (a) : (b)) -#define MIN(a, b) (((a) < (b)) ? (a) : (b)) - #define ROUND_DIV(a, b) (((b) !=3D 0) ? ((a) + ((b) >> 1)) / (b) : 0) #define CEIL_DIV(a, b) (((b) !=3D 0) ? ((a) + (b) - 1) / (b) : 0) #define CEIL_MUL(a, b) (CEIL_DIV(a, b) * (b)) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index 9c2848abc804..fc384714da45 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -277,6 +277,8 @@ static inline bool in_range32(u32 val, u32 start, u32 l= en) * Use these carefully: no type checking, and uses the arguments * multiple times. Use for obvious constants only. */ +#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)) =20 diff --git a/kernel/trace/preemptirq_delay_test.c b/kernel/trace/preemptirq= _delay_test.c index 8af92dbe98f0..acb0c971a408 100644 --- a/kernel/trace/preemptirq_delay_test.c +++ b/kernel/trace/preemptirq_delay_test.c @@ -34,8 +34,6 @@ MODULE_PARM_DESC(cpu_affinity, "Cpu num test is running o= n"); =20 static struct completion done; =20 -#define MIN(x, y) ((x) < (y) ? (x) : (y)) - static void busy_wait(ulong time) { u64 start, end; diff --git a/lib/btree.c b/lib/btree.c index 49420cae3a83..bb81d3393ac5 100644 --- a/lib/btree.c +++ b/lib/btree.c @@ -43,7 +43,6 @@ #include #include =20 -#define MAX(a, b) ((a) > (b) ? (a) : (b)) #define NODESIZE MAX(L1_CACHE_BYTES, 128) =20 struct btree_geo { diff --git a/lib/decompress_unlzma.c b/lib/decompress_unlzma.c index 20a858031f12..9d34d35908da 100644 --- a/lib/decompress_unlzma.c +++ b/lib/decompress_unlzma.c @@ -37,7 +37,9 @@ =20 #include =20 +#ifndef MIN #define MIN(a, b) (((a) < (b)) ? (a) : (b)) +#endif =20 static long long INIT read_int(unsigned char *ptr, int size) { diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c index c82070167d8a..14327fc34aa7 100644 --- a/mm/zsmalloc.c +++ b/mm/zsmalloc.c @@ -119,8 +119,6 @@ #define ISOLATED_BITS 5 #define MAGIC_VAL_BITS 8 =20 -#define MAX(a, b) ((a) >=3D (b) ? (a) : (b)) - #define ZS_MAX_PAGES_PER_ZSPAGE (_AC(CONFIG_ZSMALLOC_CHAIN_SIZE, UL)) =20 /* ZS_MIN_ALLOC_SIZE must be multiple of ZS_ALIGN */ diff --git a/tools/testing/selftests/mm/mremap_test.c b/tools/testing/selft= ests/mm/mremap_test.c index 5c3773de9f0f..944645f9d2b3 100644 --- a/tools/testing/selftests/mm/mremap_test.c +++ b/tools/testing/selftests/mm/mremap_test.c @@ -22,7 +22,9 @@ #define VALIDATION_DEFAULT_THRESHOLD 4 /* 4MB */ #define VALIDATION_NO_THRESHOLD 0 /* Verify the entire region */ =20 +#ifndef MIN #define MIN(X, Y) ((X) < (Y) ? (X) : (Y)) +#endif =20 struct config { unsigned long long src_alignment; diff --git a/tools/testing/selftests/seccomp/seccomp_bpf.c b/tools/testing/= selftests/seccomp/seccomp_bpf.c index 15325ca35f1e..4390965c84c4 100644 --- a/tools/testing/selftests/seccomp/seccomp_bpf.c +++ b/tools/testing/selftests/seccomp/seccomp_bpf.c @@ -60,7 +60,9 @@ #define SKIP(s, ...) XFAIL(s, ##__VA_ARGS__) #endif =20 +#ifndef MIN #define MIN(X, Y) ((X) < (Y) ? (X) : (Y)) +#endif =20 #ifndef PR_SET_PTRACER # define PR_SET_PTRACER 0x59616d61 --=20 2.47.3 From nobody Thu Oct 2 01:01:46 2025 Received: from fra-out-004.esa.eu-central-1.outbound.mail-perimeter.amazon.com (fra-out-004.esa.eu-central-1.outbound.mail-perimeter.amazon.com [3.74.81.189]) (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 058E030DD0A; Mon, 29 Sep 2025 17:18:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=3.74.81.189 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759166286; cv=none; b=BY3LmE6a9sLd1KHUNoFXHnaZ8Pb3ERhgRqQU2uD0O7TaQnQYfXQY2Rx3H56V7fmESsFg1AECvZkymkTZGikFWa3daskLvciD6+4XMLXHH3ycWBAAOAtzg7PlNKCxsKh/VsIZqm/bVObGHlkOBvNsq1TbwAZV7kGcFxiwQvmBIRs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759166286; c=relaxed/simple; bh=P0/5K4itgZIzRKJXbbY7AyT0CEFiidrkrNPidMshKPg=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=rTPGhdz3GNz6otmcKZYqxXDTegHLYRxSEiSuesRIpF0llR645Rqa+/mC/c49D6cWSIHJ/2OUV3hCviDDArYFNZujv0U/sZ0V592O8AwVE8D5vXLTZVACNuVazEi+1sXJT/7oO9iy51hThL0QkDxgsHgLVwj4dVCiRtkBNR1M1uI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.com; dkim=pass (2048-bit key) header.d=amazon.com header.i=@amazon.com header.b=JK9w16Go; arc=none smtp.client-ip=3.74.81.189 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.com header.i=@amazon.com header.b="JK9w16Go" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazoncorp2; t=1759166284; x=1790702284; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=xkEPvUKyQPfYOtTnd+JXsEAKoNLEM/0U49oHM5nqIDU=; b=JK9w16GohTOrxYClYJeEjgTS+R/QTiddLZour8VfCAIMNH1AKe/Iusln eRqtGEXckndcJSC1jVIdyqLzMz+FrP1ahnTz5N3fP1gQCGCJtsdvZmVis TiWYK6gHjKDG00oZ86mjIbNNSK1TsFiWTMUk+GvLCWMIguVwHOeda8Xyd NZMe0afzbnVRF9SbdpJJEvgAd0DlqW2VBBQKsvtW9SmlJIMQ51+K/QwIm ULEgFPhgSNXrT8mcckyVS6O+CD2MdktcWJ/ki++iqeKMTrElkaTjMaMkM sH6j9RlKxXN9EU5tXCSA9dTXEgOc8gDJJwES8PzHuJyEtMQ3sxfytDWAu g==; X-CSE-ConnectionGUID: OCpi9bEnRAqnF/S8t9UOjw== X-CSE-MsgGUID: BOfPB8N6SceQEjpb8xh/Cw== X-IronPort-AV: E=Sophos;i="6.18,302,1751241600"; d="scan'208";a="2837752" Received: from ip-10-6-6-97.eu-central-1.compute.internal (HELO smtpout.naws.eu-central-1.prod.farcaster.email.amazon.dev) ([10.6.6.97]) by internal-fra-out-004.esa.eu-central-1.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Sep 2025 17:17:54 +0000 Received: from EX19MTAEUA001.ant.amazon.com [54.240.197.233:30559] by smtpin.naws.eu-central-1.prod.farcaster.email.amazon.dev [10.0.44.161:2525] with esmtp (Farcaster) id 45410551-6ebb-4874-94fb-5ae3798b00c3; Mon, 29 Sep 2025 17:17:54 +0000 (UTC) X-Farcaster-Flow-ID: 45410551-6ebb-4874-94fb-5ae3798b00c3 Received: from EX19D018EUA004.ant.amazon.com (10.252.50.85) by EX19MTAEUA001.ant.amazon.com (10.252.50.50) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Mon, 29 Sep 2025 17:17:51 +0000 Received: from dev-dsk-farbere-1a-46ecabed.eu-west-1.amazon.com (172.19.116.181) by EX19D018EUA004.ant.amazon.com (10.252.50.85) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Mon, 29 Sep 2025 17:17:48 +0000 From: Eliav Farber To: , , , , , , , , CC: Linus Torvalds , David Laight , Lorenzo Stoakes Subject: [PATCH v2 02/12 6.6.y] minmax: simplify min()/max()/clamp() implementation Date: Mon, 29 Sep 2025 17:17:23 +0000 Message-ID: <20250929171733.20671-3-farbere@amazon.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20250929171733.20671-1-farbere@amazon.com> References: <20250929171733.20671-1-farbere@amazon.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: EX19D040UWA003.ant.amazon.com (10.13.139.6) To EX19D018EUA004.ant.amazon.com (10.252.50.85) Content-Type: text/plain; charset="utf-8" From: Linus Torvalds [ Upstream commit dc1c8034e31b14a2e5e212104ec508aec44ce1b9 ] Now that we no longer have any C constant expression contexts (ie array size declarations or static initializers) that use min() or max(), we can simpify the implementation by not having to worry about the result staying as a C constant expression. So now we can unconditionally just use temporary variables of the right type, and get rid of the excessive expansion that used to come from the use of __builtin_choose_expr(__is_constexpr(...), .. to pick the specialized code for constant expressions. Another expansion simplification is to pass the temporary variables (in addition to the original expression) to our __types_ok() macro. That may superficially look like it complicates the macro, but when we only want the type of the expression, expanding the temporary variable names is much simpler and smaller than expanding the potentially complicated original expression. As a result, on my machine, doing a $ time make drivers/staging/media/atomisp/pci/isp/kernels/ynr/ynr_1.0/ia_= css_ynr.host.i goes from real 0m16.621s user 0m15.360s sys 0m1.221s to real 0m2.532s user 0m2.091s sys 0m0.452s because the token expansion goes down dramatically. In particular, the longest line expansion (which was line 71 of that 'ia_css_ynr.host.c' file) shrinks from 23,338kB (yes, 23MB for one single line) to "just" 1,444kB (now "only" 1.4MB). And yes, that line is still the line from hell, because it's doing multiple levels of "min()/max()" expansion thanks to some of them being hidden inside the uDIGIT_FITTING() macro. Lorenzo has a nice cleanup patch that makes that driver use inline functions instead of macros for sDIGIT_FITTING() and uDIGIT_FITTING(), which will fix that line once and for all, but the 16-fold reduction in this case does show why we need to simplify these helpers. Cc: David Laight Cc: Lorenzo Stoakes Signed-off-by: Linus Torvalds Signed-off-by: Eliav Farber --- include/linux/minmax.h | 43 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index fc384714da45..e3e4353df983 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -35,10 +35,10 @@ #define __is_noneg_int(x) \ (__builtin_choose_expr(__is_constexpr(x) && __is_signed(x), x, -1) >=3D 0) =20 -#define __types_ok(x, y) \ - (__is_signed(x) =3D=3D __is_signed(y) || \ - __is_signed((x) + 0) =3D=3D __is_signed((y) + 0) || \ - __is_noneg_int(x) || __is_noneg_int(y)) +#define __types_ok(x, y, ux, uy) \ + (__is_signed(ux) =3D=3D __is_signed(uy) || \ + __is_signed((ux) + 0) =3D=3D __is_signed((uy) + 0) || \ + __is_noneg_int(x) || __is_noneg_int(y)) =20 #define __cmp_op_min < #define __cmp_op_max > @@ -51,34 +51,31 @@ #define __cmp_once(op, type, x, y) \ __cmp_once_unique(op, type, x, y, __UNIQUE_ID(x_), __UNIQUE_ID(y_)) =20 -#define __careful_cmp_once(op, x, y) ({ \ - static_assert(__types_ok(x, y), \ +#define __careful_cmp_once(op, x, y, ux, uy) ({ \ + __auto_type ux =3D (x); __auto_type uy =3D (y); \ + static_assert(__types_ok(x, y, ux, uy), \ #op "(" #x ", " #y ") signedness error, fix types or consider u" #op "()= before " #op "_t()"); \ - __cmp_once(op, __auto_type, x, y); }) + __cmp(op, ux, uy); }) =20 -#define __careful_cmp(op, x, y) \ - __builtin_choose_expr(__is_constexpr((x) - (y)), \ - __cmp(op, x, y), __careful_cmp_once(op, x, y)) +#define __careful_cmp(op, x, y) \ + __careful_cmp_once(op, x, y, __UNIQUE_ID(x_), __UNIQUE_ID(y_)) =20 #define __clamp(val, lo, hi) \ ((val) >=3D (hi) ? (hi) : ((val) <=3D (lo) ? (lo) : (val))) =20 -#define __clamp_once(val, lo, hi, unique_val, unique_lo, unique_hi) ({ \ - typeof(val) unique_val =3D (val); \ - typeof(lo) unique_lo =3D (lo); \ - typeof(hi) unique_hi =3D (hi); \ +#define __clamp_once(val, lo, hi, uval, ulo, uhi) ({ \ + __auto_type uval =3D (val); \ + __auto_type ulo =3D (lo); \ + __auto_type uhi =3D (hi); \ static_assert(__builtin_choose_expr(__is_constexpr((lo) > (hi)), \ (lo) <=3D (hi), true), \ "clamp() low limit " #lo " greater than high limit " #hi); \ - static_assert(__types_ok(val, lo), "clamp() 'lo' signedness error"); \ - static_assert(__types_ok(val, hi), "clamp() 'hi' signedness error"); \ - __clamp(unique_val, unique_lo, unique_hi); }) - -#define __careful_clamp(val, lo, hi) ({ \ - __builtin_choose_expr(__is_constexpr((val) - (lo) + (hi)), \ - __clamp(val, lo, hi), \ - __clamp_once(val, lo, hi, __UNIQUE_ID(__val), \ - __UNIQUE_ID(__lo), __UNIQUE_ID(__hi))); }) + static_assert(__types_ok(uval, lo, uval, ulo), "clamp() 'lo' signedness e= rror"); \ + static_assert(__types_ok(uval, hi, uval, uhi), "clamp() 'hi' signedness e= rror"); \ + __clamp(uval, ulo, uhi); }) + +#define __careful_clamp(val, lo, hi) \ + __clamp_once(val, lo, hi, __UNIQUE_ID(v_), __UNIQUE_ID(l_), __UNIQUE_ID(h= _)) =20 /** * min - return minimum of two values of the same or compatible types --=20 2.47.3 From nobody Thu Oct 2 01:01:46 2025 Received: from fra-out-009.esa.eu-central-1.outbound.mail-perimeter.amazon.com (fra-out-009.esa.eu-central-1.outbound.mail-perimeter.amazon.com [3.64.237.68]) (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 0D33626ACC; Mon, 29 Sep 2025 17:17:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=3.64.237.68 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759166282; cv=none; b=JE5EzH6/CQbtntngh2Yu/8+0NNwyUyZzMAM3TmnMAP6b5oHi8vKYb5bHco0pfDMclfa5OrQB8c7xsZYHTe4Jl9KwM3DLtZIQy5+QvJS4DQ5rtr27GZO13cqoBJ5AY4srnyA8Xa7+0asaKmaPJTnZ3fcE5vL7s9MDOrGW8OOy1Dg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759166282; c=relaxed/simple; bh=lzywvl0/2SbhGOuqVDdcgYucQs8SCzbDgO4M8OMRxcw=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=rC02mWfYeyfjMfJwXRydDfahvJOREx5LdLPTFX6VOllW/KckkDM61u7aJk0ZI/D/672T/Kj5MEgwEOdFznTNhBpeneW6zBBZJCvckmuMUrvfSWEBYW3bWTmW/N2ElPUjo6Cmi+ZC+BjdW4NfvQ2jXzlgh8nRE5KxACbBGNV3XmM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.com; dkim=pass (2048-bit key) header.d=amazon.com header.i=@amazon.com header.b=KpSpEJzg; arc=none smtp.client-ip=3.64.237.68 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.com header.i=@amazon.com header.b="KpSpEJzg" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazoncorp2; t=1759166280; x=1790702280; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=TDV161r/7qFlBicXIybWhTsXZSPWT6ddjPuQfDuVP8k=; b=KpSpEJzgcWOtvKkq70JFOEJXHx7tkJo6XkrquT/sp+ry6KsfXVSfiDAK LBVn9w6HgpVDvdXEHiQzZ6urNqzGrdPkMGfmbx0qDx59Wjt/T6ufvR31R CYewg7XUpi7oQNJ3CWLSHGuTWiYX1+tJb34YJuee/TuJEtffMEVFe45n3 3VqujDLNLlbIObhDi3oS1JNOA2HQf5Vdku/kH4yyJ2NPFqQk/z6TSbQQP eSPNXpDdwwx4yNvq8L8TeWM535Wz3krflcQiTESbULY0t19P40v3/c1UV gHw4BXrkx2de7ubJsM5sOjxfLiL148bZdBdbHNZVE79vNkfAfLSuPROhW g==; X-CSE-ConnectionGUID: CW+y+aigQ8CusFi4VtLkcA== X-CSE-MsgGUID: yF3oAc84Q/ytIOgFWVIuMA== X-IronPort-AV: E=Sophos;i="6.18,302,1751241600"; d="scan'208";a="2740067" Received: from ip-10-6-11-83.eu-central-1.compute.internal (HELO smtpout.naws.eu-central-1.prod.farcaster.email.amazon.dev) ([10.6.11.83]) by internal-fra-out-009.esa.eu-central-1.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Sep 2025 17:17:57 +0000 Received: from EX19MTAEUB001.ant.amazon.com [54.240.197.234:5274] by smtpin.naws.eu-central-1.prod.farcaster.email.amazon.dev [10.0.40.83:2525] with esmtp (Farcaster) id c1822a22-1090-48cd-a4f5-a1062bff3e7c; Mon, 29 Sep 2025 17:17:56 +0000 (UTC) X-Farcaster-Flow-ID: c1822a22-1090-48cd-a4f5-a1062bff3e7c Received: from EX19D018EUA004.ant.amazon.com (10.252.50.85) by EX19MTAEUB001.ant.amazon.com (10.252.51.26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Mon, 29 Sep 2025 17:17:55 +0000 Received: from dev-dsk-farbere-1a-46ecabed.eu-west-1.amazon.com (172.19.116.181) by EX19D018EUA004.ant.amazon.com (10.252.50.85) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Mon, 29 Sep 2025 17:17:51 +0000 From: Eliav Farber To: , , , , , , , , CC: Linus Torvalds , David Laight , Lorenzo Stoakes Subject: [PATCH v2 03/12 6.6.y] minmax: don't use max() in situations that want a C constant expression Date: Mon, 29 Sep 2025 17:17:24 +0000 Message-ID: <20250929171733.20671-4-farbere@amazon.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20250929171733.20671-1-farbere@amazon.com> References: <20250929171733.20671-1-farbere@amazon.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: EX19D040UWA003.ant.amazon.com (10.13.139.6) To EX19D018EUA004.ant.amazon.com (10.252.50.85) Content-Type: text/plain; charset="utf-8" From: Linus Torvalds [ Upstream commit cb04e8b1d2f24c4c2c92f7b7529031fc35a16fed ] We only had a couple of array[] declarations, and changing them to just use 'MAX()' instead of 'max()' fixes the issue. This will allow us to simplify our min/max macros enormously, since they can now unconditionally use temporary variables to avoid using the argument values multiple times. Cc: David Laight Cc: Lorenzo Stoakes Signed-off-by: Linus Torvalds Signed-off-by: Eliav Farber --- drivers/gpu/drm/amd/pm/swsmu/smu_cmn.c | 2 +- drivers/input/touchscreen/cyttsp4_core.c | 2 +- drivers/irqchip/irq-sun6i-r.c | 2 +- drivers/net/can/usb/etas_es58x/es58x_devlink.c | 2 +- fs/btrfs/tree-checker.c | 2 +- lib/vsprintf.c | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/drivers/gpu/drm/amd/pm/swsmu/smu_cmn.c b/drivers/gpu/drm/amd/p= m/swsmu/smu_cmn.c index 12618a583e97..c1962f1974c6 100644 --- a/drivers/gpu/drm/amd/pm/swsmu/smu_cmn.c +++ b/drivers/gpu/drm/amd/pm/swsmu/smu_cmn.c @@ -708,7 +708,7 @@ static const char *smu_get_feature_name(struct smu_cont= ext *smu, size_t smu_cmn_get_pp_feature_mask(struct smu_context *smu, char *buf) { - int8_t sort_feature[max(SMU_FEATURE_COUNT, SMU_FEATURE_MAX)]; + int8_t sort_feature[MAX(SMU_FEATURE_COUNT, SMU_FEATURE_MAX)]; uint64_t feature_mask; int i, feature_index; uint32_t count =3D 0; diff --git a/drivers/input/touchscreen/cyttsp4_core.c b/drivers/input/touch= screen/cyttsp4_core.c index 7cb26929dc73..9dc25eb2be44 100644 --- a/drivers/input/touchscreen/cyttsp4_core.c +++ b/drivers/input/touchscreen/cyttsp4_core.c @@ -871,7 +871,7 @@ static void cyttsp4_get_mt_touches(struct cyttsp4_mt_da= ta *md, int num_cur_tch) struct cyttsp4_touch tch; int sig; int i, j, t =3D 0; - int ids[max(CY_TMA1036_MAX_TCH, CY_TMA4XX_MAX_TCH)]; + int ids[MAX(CY_TMA1036_MAX_TCH, CY_TMA4XX_MAX_TCH)]; =20 memset(ids, 0, si->si_ofs.tch_abs[CY_TCH_T].max * sizeof(int)); for (i =3D 0; i < num_cur_tch; i++) { diff --git a/drivers/irqchip/irq-sun6i-r.c b/drivers/irqchip/irq-sun6i-r.c index a01e44049415..99958d470d62 100644 --- a/drivers/irqchip/irq-sun6i-r.c +++ b/drivers/irqchip/irq-sun6i-r.c @@ -270,7 +270,7 @@ static const struct irq_domain_ops sun6i_r_intc_domain_= ops =3D { =20 static int sun6i_r_intc_suspend(void) { - u32 buf[BITS_TO_U32(max(SUN6I_NR_TOP_LEVEL_IRQS, SUN6I_NR_MUX_BITS))]; + u32 buf[BITS_TO_U32(MAX(SUN6I_NR_TOP_LEVEL_IRQS, SUN6I_NR_MUX_BITS))]; int i; =20 /* Wake IRQs are enabled during system sleep and shutdown. */ diff --git a/drivers/net/can/usb/etas_es58x/es58x_devlink.c b/drivers/net/c= an/usb/etas_es58x/es58x_devlink.c index e763a9904bed..0d155eb1b9e9 100644 --- a/drivers/net/can/usb/etas_es58x/es58x_devlink.c +++ b/drivers/net/can/usb/etas_es58x/es58x_devlink.c @@ -215,7 +215,7 @@ static int es58x_devlink_info_get(struct devlink *devli= nk, struct es58x_sw_version *fw_ver =3D &es58x_dev->firmware_version; struct es58x_sw_version *bl_ver =3D &es58x_dev->bootloader_version; struct es58x_hw_revision *hw_rev =3D &es58x_dev->hardware_revision; - char buf[max(sizeof("xx.xx.xx"), sizeof("axxx/xxx"))]; + char buf[MAX(sizeof("xx.xx.xx"), sizeof("axxx/xxx"))]; int ret =3D 0; =20 if (es58x_sw_version_is_valid(fw_ver)) { diff --git a/fs/btrfs/tree-checker.c b/fs/btrfs/tree-checker.c index 6d16506bbdc0..6f4c42e66519 100644 --- a/fs/btrfs/tree-checker.c +++ b/fs/btrfs/tree-checker.c @@ -614,7 +614,7 @@ static int check_dir_item(struct extent_buffer *leaf, */ if (key->type =3D=3D BTRFS_DIR_ITEM_KEY || key->type =3D=3D BTRFS_XATTR_ITEM_KEY) { - char namebuf[max(BTRFS_NAME_LEN, XATTR_NAME_MAX)]; + char namebuf[MAX(BTRFS_NAME_LEN, XATTR_NAME_MAX)]; =20 read_extent_buffer(leaf, namebuf, (unsigned long)(di + 1), name_len); diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 2aa408441cd3..f4ab2750cfc1 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -1079,7 +1079,7 @@ char *resource_string(char *buf, char *end, struct re= source *res, #define FLAG_BUF_SIZE (2 * sizeof(res->flags)) #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]") #define RAW_BUF_SIZE sizeof("[mem - flags 0x]") - char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE, + char sym[MAX(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE, 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)]; =20 char *p =3D sym, *pend =3D sym + sizeof(sym); --=20 2.47.3 From nobody Thu Oct 2 01:01:46 2025 Received: from fra-out-009.esa.eu-central-1.outbound.mail-perimeter.amazon.com (fra-out-009.esa.eu-central-1.outbound.mail-perimeter.amazon.com [3.64.237.68]) (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 81DAB30C104; Mon, 29 Sep 2025 17:18:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=3.64.237.68 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759166300; cv=none; b=Kyxxp02Va/LbekNVSePpyBHyIi0mHoUfCcmplpSPjngVJu0IBXeoOs5DMbZ/aGfnU19W+aBCD5aJVtp+Bdw+G7OnVY61akzPIncJsUyVwfixsAEQ1fQDiBmFntBl0JR5cQihm9rQ9bO6HfsHiVIQCCRgLBg59B1QT87vNUPu4XI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759166300; c=relaxed/simple; bh=AQYGcvRqAwUHVwXTDa2uFBJ0Axtvzde68lZXY+PX4e8=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=EMtzCuPY31TsHS215jcJ1mJ08CQf5IoqB/pcVZNWV3wqOgQoQMGCASZiCMdQOgRr+UHBjvR2TXt3Yg1l8th5kwZAkrDuR/t7HMeWr2T82l3098uPtW/hpD90Npg2YaRsGdQy3AguCvdDz8U5NDhv1xvEvL1lXJwpvpIUfXdUv3Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.com; dkim=pass (2048-bit key) header.d=amazon.com header.i=@amazon.com header.b=RHQ+ht0V; arc=none smtp.client-ip=3.64.237.68 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.com header.i=@amazon.com header.b="RHQ+ht0V" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazoncorp2; t=1759166298; x=1790702298; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=tuDLxbukezbKvm9K9qJc6kdbP/O3m0CMIC8okLoytJg=; b=RHQ+ht0VVPKlohNs56mzszX6Z863Ir8a3ji6k21F+SA8gdHUtdvrNYLn dPRJ1/qUW+wGtHZlR/lddW0Fnaa6zSHEMbCEJbv5a0ObR8x3CLYiEW5Pd ZzF4KdNttiuChufQwaflj4/7DSOyo4qZahRH7rAJ5XmeOcLQK1Hildo+I WlOPezTLefr3Hrf9kJnlQd+6gNRXsORlTPCGX5nk4gLJzPkqfS/nhtLF0 RO0rDFij/bg2Zs+ZqKjFk67dg6WpNOmzMjEb4paiqOcIWxCcUuvXcEDj0 rNetfY2DRD6K89sa3YeSkP4OtazPthHw9HDiO709A3pQvDDN5qnpR0gQu w==; X-CSE-ConnectionGUID: 9VEhOidLRwK7k+CuqgH69g== X-CSE-MsgGUID: j16Z9SWcSaaIBvXhAxfl+Q== X-IronPort-AV: E=Sophos;i="6.18,302,1751241600"; d="scan'208";a="2740085" Received: from ip-10-6-6-97.eu-central-1.compute.internal (HELO smtpout.naws.eu-central-1.prod.farcaster.email.amazon.dev) ([10.6.6.97]) by internal-fra-out-009.esa.eu-central-1.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Sep 2025 17:18:16 +0000 Received: from EX19MTAEUB001.ant.amazon.com [54.240.197.234:2817] by smtpin.naws.eu-central-1.prod.farcaster.email.amazon.dev [10.0.21.15:2525] with esmtp (Farcaster) id c5354cf1-de8a-47cb-bdcf-6ea6b1f3298b; Mon, 29 Sep 2025 17:18:16 +0000 (UTC) X-Farcaster-Flow-ID: c5354cf1-de8a-47cb-bdcf-6ea6b1f3298b Received: from EX19D018EUA004.ant.amazon.com (10.252.50.85) by EX19MTAEUB001.ant.amazon.com (10.252.51.26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Mon, 29 Sep 2025 17:18:15 +0000 Received: from dev-dsk-farbere-1a-46ecabed.eu-west-1.amazon.com (172.19.116.181) by EX19D018EUA004.ant.amazon.com (10.252.50.85) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Mon, 29 Sep 2025 17:18:11 +0000 From: Eliav Farber To: , , , , , , , , CC: Linus Torvalds , David Laight , Lorenzo Stoakes Subject: [PATCH v2 04/12 6.6.y] minmax: improve macro expansion and type checking Date: Mon, 29 Sep 2025 17:17:25 +0000 Message-ID: <20250929171733.20671-5-farbere@amazon.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20250929171733.20671-1-farbere@amazon.com> References: <20250929171733.20671-1-farbere@amazon.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: EX19D040UWA001.ant.amazon.com (10.13.139.22) To EX19D018EUA004.ant.amazon.com (10.252.50.85) Content-Type: text/plain; charset="utf-8" From: Linus Torvalds [ Upstream commit 22f5468731491e53356ba7c028f0fdea20b18e2c ] This clarifies the rules for min()/max()/clamp() type checking and makes them a much more efficient macro expansion. In particular, we now look at the type and range of the inputs to see whether they work together, generating a mask of acceptable comparisons, and then just verifying that the inputs have a shared case: - an expression with a signed type can be used for (1) signed comparisons (2) unsigned comparisons if it is statically known to have a non-negative value - an expression with an unsigned type can be used for (3) unsigned comparison (4) signed comparisons if the type is smaller than 'int' and thus the C integer promotion rules will make it signed anyway Here rule (1) and (3) are obvious, and rule (2) is important in order to allow obvious trivial constants to be used together with unsigned values. Rule (4) is not necessarily a good idea, but matches what we used to do, and we have extant cases of this situation in the kernel. Notably with bcachefs having an expression like min(bch2_bucket_sectors_dirty(a), ca->mi.bucket_size) where bch2_bucket_sectors_dirty() returns an 's64', and 'ca->mi.bucket_size' is of type 'u16'. Technically that bcachefs comparison is clearly sensible on a C type level, because the 'u16' will go through the normal C integer promotion, and become 'int', and then we're comparing two signed values and everything looks sane. However, it's not entirely clear that a 'min(s64,u16)' operation makes a lot of conceptual sense, and it's possible that we will remove rule (4). After all, the _reason_ we have these complicated type checks is exactly that the C type promotion rules are not very intuitive. But at least for now the rule is in place for backwards compatibility. Also note that rule (2) existed before, but is hugely relaxed by this commit. It used to be true only for the simplest compile-time non-negative integer constants. The new macro model will allow cases where the compiler can trivially see that an expression is non-negative even if it isn't necessarily a constant. For example, the amdgpu driver does min_t(size_t, sizeof(fru_info->serial), pia[addr] & 0x3F)); because our old 'min()' macro would see that 'pia[addr] & 0x3F' is of type 'int' and clearly not a C constant expression, so doing a 'min()' with a 'size_t' is a signedness violation. Our new 'min()' macro still sees that 'pia[addr] & 0x3F' is of type 'int', but is smart enough to also see that it is clearly non-negative, and thus would allow that case without any complaints. Cc: Arnd Bergmann Cc: David Laight Cc: Lorenzo Stoakes Signed-off-by: Linus Torvalds Signed-off-by: Eliav Farber --- include/linux/compiler.h | 9 +++++ include/linux/minmax.h | 74 ++++++++++++++++++++++++++++++++-------- 2 files changed, 68 insertions(+), 15 deletions(-) diff --git a/include/linux/compiler.h b/include/linux/compiler.h index e84ed3a43f1f..1d4f23ea3105 100644 --- a/include/linux/compiler.h +++ b/include/linux/compiler.h @@ -244,6 +244,15 @@ static inline void *offset_to_ptr(const int *off) #define is_signed_type(type) (((type)(-1)) < (__force type)1) #define is_unsigned_type(type) (!is_signed_type(type)) =20 +/* + * Useful shorthand for "is this condition known at compile-time?" + * + * Note that the condition may involve non-constant values, + * but the compiler may know enough about the details of the + * values to determine that the condition is statically true. + */ +#define statically_true(x) (__builtin_constant_p(x) && (x)) + /* * This is needed in functions which generate the stack canary, see * arch/x86/kernel/smpboot.c::start_secondary() for an example. diff --git a/include/linux/minmax.h b/include/linux/minmax.h index e3e4353df983..41da6f85a407 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -26,19 +26,63 @@ #define __typecheck(x, y) \ (!!(sizeof((typeof(x) *)1 =3D=3D (typeof(y) *)1))) =20 -/* is_signed_type() isn't a constexpr for pointer types */ -#define __is_signed(x) \ - __builtin_choose_expr(__is_constexpr(is_signed_type(typeof(x))), \ - is_signed_type(typeof(x)), 0) +/* + * __sign_use for integer expressions: + * bit #0 set if ok for unsigned comparisons + * bit #1 set if ok for signed comparisons + * + * In particular, statically non-negative signed integer + * expressions are ok for both. + * + * NOTE! Unsigned types smaller than 'int' are implicitly + * converted to 'int' in expressions, and are accepted for + * signed conversions for now. This is debatable. + * + * Note that 'x' is the original expression, and 'ux' is + * the unique variable that contains the value. + * + * We use 'ux' for pure type checking, and 'x' for when + * we need to look at the value (but without evaluating + * it for side effects! Careful to only ever evaluate it + * with sizeof() or __builtin_constant_p() etc). + * + * Pointers end up being checked by the normal C type + * rules at the actual comparison, and these expressions + * only need to be careful to not cause warnings for + * pointer use. + */ +#define __signed_type_use(x,ux) (2+__is_nonneg(x,ux)) +#define __unsigned_type_use(x,ux) (1+2*(sizeof(ux)<4)) +#define __sign_use(x,ux) (is_signed_type(typeof(ux))? \ + __signed_type_use(x,ux):__unsigned_type_use(x,ux)) + +/* + * To avoid warnings about casting pointers to integers + * of different sizes, we need that special sign type. + * + * On 64-bit we can just always use 'long', since any + * integer or pointer type can just be cast to that. + * + * This does not work for 128-bit signed integers since + * the cast would truncate them, but we do not use s128 + * types in the kernel (we do use 'u128', but they will + * be handled by the !is_signed_type() case). + * + * NOTE! The cast is there only to avoid any warnings + * from when values that aren't signed integer types. + */ +#ifdef CONFIG_64BIT + #define __signed_type(ux) long +#else + #define __signed_type(ux) typeof(__builtin_choose_expr(sizeof(ux)>4,1LL,= 1L)) +#endif +#define __is_nonneg(x,ux) statically_true((__signed_type(ux))(x)>=3D0) =20 -/* True for a non-negative signed int constant */ -#define __is_noneg_int(x) \ - (__builtin_choose_expr(__is_constexpr(x) && __is_signed(x), x, -1) >=3D 0) +#define __types_ok(x,y,ux,uy) \ + (__sign_use(x,ux) & __sign_use(y,uy)) =20 -#define __types_ok(x, y, ux, uy) \ - (__is_signed(ux) =3D=3D __is_signed(uy) || \ - __is_signed((ux) + 0) =3D=3D __is_signed((uy) + 0) || \ - __is_noneg_int(x) || __is_noneg_int(y)) +#define __types_ok3(x,y,z,ux,uy,uz) \ + (__sign_use(x,ux) & __sign_use(y,uy) & __sign_use(z,uz)) =20 #define __cmp_op_min < #define __cmp_op_max > @@ -53,8 +97,8 @@ =20 #define __careful_cmp_once(op, x, y, ux, uy) ({ \ __auto_type ux =3D (x); __auto_type uy =3D (y); \ - static_assert(__types_ok(x, y, ux, uy), \ - #op "(" #x ", " #y ") signedness error, fix types or consider u" #op "()= before " #op "_t()"); \ + BUILD_BUG_ON_MSG(!__types_ok(x,y,ux,uy), \ + #op"("#x", "#y") signedness error"); \ __cmp(op, ux, uy); }) =20 #define __careful_cmp(op, x, y) \ @@ -70,8 +114,8 @@ static_assert(__builtin_choose_expr(__is_constexpr((lo) > (hi)), \ (lo) <=3D (hi), true), \ "clamp() low limit " #lo " greater than high limit " #hi); \ - static_assert(__types_ok(uval, lo, uval, ulo), "clamp() 'lo' signedness e= rror"); \ - static_assert(__types_ok(uval, hi, uval, uhi), "clamp() 'hi' signedness e= rror"); \ + BUILD_BUG_ON_MSG(!__types_ok3(val,lo,hi,uval,ulo,uhi), \ + "clamp("#val", "#lo", "#hi") signedness error"); \ __clamp(uval, ulo, uhi); }) =20 #define __careful_clamp(val, lo, hi) \ --=20 2.47.3 From nobody Thu Oct 2 01:01:46 2025 Received: from fra-out-002.esa.eu-central-1.outbound.mail-perimeter.amazon.com (fra-out-002.esa.eu-central-1.outbound.mail-perimeter.amazon.com [3.65.3.180]) (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 2DB7F30CD92; Mon, 29 Sep 2025 17:18:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=3.65.3.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759166313; cv=none; b=WWKfHs3V012UiaJ/75ywRUA/KOX/R06aL4eHLfpuBhtGh8I46rpd8X9WrPWGDNx0eau4llJTs2Dm+0VlwGRCLdGWWFSoIgLZJFm1ALiQ3Lq3R8B3FPavpCKw5E09XOgP4VNMPD+cjlUZgMoMpoGkIXd2ah5NHkfKqnBhV0Uu4Yk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759166313; c=relaxed/simple; bh=nlvxYaLaWRNdcmYPv9KL9mUJqKtD+ww79dSJfh94FPM=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=r5UkvYDimMkpgS/z3s92SgqcTMzRqw2FoLvnsHUrONPHgTT9mJYkYxpnGz/2kgnZLlUAOXwnCMaIigt5m9vsWwXgNfzz/y4JvpN9eoj3U247jLVrrR09hHzPX9Rms5MTvietXI3j30DRWFmMz6ZwwUzlScB4okKJ+KxNf2wjWUA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.com; dkim=pass (2048-bit key) header.d=amazon.com header.i=@amazon.com header.b=pf24lI7k; arc=none smtp.client-ip=3.65.3.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.com header.i=@amazon.com header.b="pf24lI7k" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazoncorp2; t=1759166311; x=1790702311; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=fgKTaT7jj0zRx6jbQ7DJOMsaZramokk9q66dhz8aLl4=; b=pf24lI7k+a3mSamuB5C+3/+v322OPaC9pPhvLCj3vgXxyMhOIEu1pZjR GUSmnaooRVW2y5YKgP6O+RyfL25Tr78H/w3FglTT/St+X5Wa+uDJbtrjj AZisKMHd8Nm+6DljQJFwZTTNGQyjBYCmlyJjWIuwxLEtq0PEypvzj44ev TMXVKJnG1Bd68C+Gl0LhYvbRLrC4LI4gaT3YhLD4yNrmLHq6pdh9ED6P1 J0O8QYnxgqxF8Yz1QHFhW35FMF4Tykdn9GGO8j/TCCPIv1OVquG1reRJR U/H5F6a7Jxy1cKSMIm/+KQEBtsEV4I9t5AGaODExb8G7B3HJboLgCAD50 Q==; X-CSE-ConnectionGUID: OdrA5m4GRTyucYO4c+QrvQ== X-CSE-MsgGUID: Zg99aBY4RPyloVBmDjPLpA== X-IronPort-AV: E=Sophos;i="6.18,302,1751241600"; d="scan'208";a="2846581" Received: from ip-10-6-6-97.eu-central-1.compute.internal (HELO smtpout.naws.eu-central-1.prod.farcaster.email.amazon.dev) ([10.6.6.97]) by internal-fra-out-002.esa.eu-central-1.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Sep 2025 17:18:21 +0000 Received: from EX19MTAEUB002.ant.amazon.com [54.240.197.232:9512] by smtpin.naws.eu-central-1.prod.farcaster.email.amazon.dev [10.0.22.27:2525] with esmtp (Farcaster) id 2b1e5247-c19f-44df-a0dc-a1c63bd8ce5f; Mon, 29 Sep 2025 17:18:20 +0000 (UTC) X-Farcaster-Flow-ID: 2b1e5247-c19f-44df-a0dc-a1c63bd8ce5f Received: from EX19D018EUA004.ant.amazon.com (10.252.50.85) by EX19MTAEUB002.ant.amazon.com (10.252.51.79) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Mon, 29 Sep 2025 17:18:18 +0000 Received: from dev-dsk-farbere-1a-46ecabed.eu-west-1.amazon.com (172.19.116.181) by EX19D018EUA004.ant.amazon.com (10.252.50.85) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Mon, 29 Sep 2025 17:18:15 +0000 From: Eliav Farber To: , , , , , , , , CC: Linus Torvalds , David Laight Subject: [PATCH v2 05/12 6.6.y] minmax: fix up min3() and max3() too Date: Mon, 29 Sep 2025 17:17:26 +0000 Message-ID: <20250929171733.20671-6-farbere@amazon.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20250929171733.20671-1-farbere@amazon.com> References: <20250929171733.20671-1-farbere@amazon.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: EX19D040UWA001.ant.amazon.com (10.13.139.22) To EX19D018EUA004.ant.amazon.com (10.252.50.85) Content-Type: text/plain; charset="utf-8" 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 --- include/linux/minmax.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index 41da6f85a407..98008dd92153 100644 --- 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) =20 +#define __careful_op3(op, x, y, z, ux, uy, uz) ({ \ + __auto_type ux =3D (x); __auto_type uy =3D (y);__auto_type uz =3D (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_)) =20 /** * 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_)) =20 /** * min_not_zero - return the minimum that is _not_ zero, unless both are z= ero --=20 2.47.3 From nobody Thu Oct 2 01:01:46 2025 Received: from fra-out-010.esa.eu-central-1.outbound.mail-perimeter.amazon.com (fra-out-010.esa.eu-central-1.outbound.mail-perimeter.amazon.com [63.178.143.178]) (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 6C87230DEB1; Mon, 29 Sep 2025 17:18:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=63.178.143.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759166317; cv=none; b=PG4Ybrp54CWEeqqCeA84sn+m39uiT6b42Q2Myn2w3grpRNxqJdiEKRbxMD+156Zlv9q2ShT40k+kpY97guItd+0fjpUtGuG8XB049vJ+mKJe/5/3YgO3DEHvgC9eCxXvxu1BQq3NSD3w/Sjr9GqZn3xc/p62umZVMMUY9NaC8A0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759166317; c=relaxed/simple; bh=20xAuZOYw8I0ufWzB2Ksi4V2Oix+SUEkw1tbLPElVss=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=ZVW5P9voIM3wMvv8VTN31AnQeJqz2xibyRyG2i8pImDQhUPrtYwRsu7GZUTI5xNYj1dMxsqhwKwq5MxLzHotnEvvY89UXUpswTEskOoECO0Bzc05Z1DRwmRmyPNQSr3tPGG4LQds8ZS3Bk7zrSoy9Owo7tI/U0+TlPxd/a4aiNY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.com; dkim=pass (2048-bit key) header.d=amazon.com header.i=@amazon.com header.b=Dr43s6V1; arc=none smtp.client-ip=63.178.143.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.com header.i=@amazon.com header.b="Dr43s6V1" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazoncorp2; t=1759166315; x=1790702315; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=OdmfzjzF46YfqPKnSMGZyjhQcZN9I6u6dSg4k1vq8LU=; b=Dr43s6V1tlmdLEPDOzDLVlYU7ieMwrD8EfW5rcYoqsvKX/8Kk/K/uz7c ISQ4mVQ3TlUfpPf5HfDlEQJzerc7KKNAFILi2WbvyNfKabFam1aFAzykE yZME+8Ig44HUKYScbpZ4NA+clxUp+6xZQ8Rs7hvnIZITp2o0NX7ldeL+h /ZeARsJkb1epUDoBqhy7u/zFJSri+l3SwpuUhq2+8n4Qdc9HHZcVIzfG1 WOKxal7yIkaeHS0KdFoPpCaFjmdaIOuku/mSyWR+zT2ej46IO1i23tgwS XYyTTh0VMRWE0Ymg6WjLHfZIUnhBTyS0mKFWodKBwnetIVO00fzFSEqi2 Q==; X-CSE-ConnectionGUID: CNMIemf/Rj+KiZBzB9fIXw== X-CSE-MsgGUID: 1sP7ypjoQgC8eP9cSuPeMA== X-IronPort-AV: E=Sophos;i="6.18,302,1751241600"; d="scan'208";a="2732851" Received: from ip-10-6-11-83.eu-central-1.compute.internal (HELO smtpout.naws.eu-central-1.prod.farcaster.email.amazon.dev) ([10.6.11.83]) by internal-fra-out-010.esa.eu-central-1.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Sep 2025 17:18:25 +0000 Received: from EX19MTAEUC002.ant.amazon.com [54.240.197.228:1676] by smtpin.naws.eu-central-1.prod.farcaster.email.amazon.dev [10.0.44.161:2525] with esmtp (Farcaster) id e040ec3a-e827-43d6-ae7b-6ea88c9b67c1; Mon, 29 Sep 2025 17:18:24 +0000 (UTC) X-Farcaster-Flow-ID: e040ec3a-e827-43d6-ae7b-6ea88c9b67c1 Received: from EX19D018EUA004.ant.amazon.com (10.252.50.85) by EX19MTAEUC002.ant.amazon.com (10.252.51.245) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Mon, 29 Sep 2025 17:18:23 +0000 Received: from dev-dsk-farbere-1a-46ecabed.eu-west-1.amazon.com (172.19.116.181) by EX19D018EUA004.ant.amazon.com (10.252.50.85) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Mon, 29 Sep 2025 17:18:18 +0000 From: Eliav Farber To: , , , , , , , , CC: Andy Shevchenko , Christoph Hellwig , Dan Carpenter , "Jason A. Donenfeld" , Jens Axboe , Lorenzo Stoakes , Mateusz Guzik , "Matthew Wilcox" , Pedro Falcato Subject: [PATCH v2 06/12 6.6.y] minmax.h: add whitespace around operators and after commas Date: Mon, 29 Sep 2025 17:17:27 +0000 Message-ID: <20250929171733.20671-7-farbere@amazon.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20250929171733.20671-1-farbere@amazon.com> References: <20250929171733.20671-1-farbere@amazon.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: EX19D040UWA001.ant.amazon.com (10.13.139.22) To EX19D018EUA004.ant.amazon.com (10.252.50.85) Content-Type: text/plain; charset="utf-8" From: David Laight [ Upstream commit 71ee9b16251ea4bf7c1fe222517c82bdb3220acc ] Patch series "minmax.h: Cleanups and minor optimisations". Some tidyups and minor changes to minmax.h. This patch (of 7): Link: https://lkml.kernel.org/r/c50365d214e04f9ba256d417c8bebbc0@AcuMS.acul= ab.com Link: https://lkml.kernel.org/r/f04b2e1310244f62826267346fde0553@AcuMS.acul= ab.com Signed-off-by: David Laight Cc: Andy Shevchenko Cc: Arnd Bergmann Cc: Christoph Hellwig Cc: Dan Carpenter Cc: Jason A. Donenfeld Cc: Jens Axboe Cc: Lorenzo Stoakes Cc: Mateusz Guzik Cc: Matthew Wilcox Cc: Pedro Falcato Signed-off-by: Andrew Morton Signed-off-by: Eliav Farber --- include/linux/minmax.h | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index 98008dd92153..51b0d988e322 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -51,10 +51,10 @@ * only need to be careful to not cause warnings for * pointer use. */ -#define __signed_type_use(x,ux) (2+__is_nonneg(x,ux)) -#define __unsigned_type_use(x,ux) (1+2*(sizeof(ux)<4)) -#define __sign_use(x,ux) (is_signed_type(typeof(ux))? \ - __signed_type_use(x,ux):__unsigned_type_use(x,ux)) +#define __signed_type_use(x, ux) (2 + __is_nonneg(x, ux)) +#define __unsigned_type_use(x, ux) (1 + 2 * (sizeof(ux) < 4)) +#define __sign_use(x, ux) (is_signed_type(typeof(ux)) ? \ + __signed_type_use(x, ux) : __unsigned_type_use(x, ux)) =20 /* * To avoid warnings about casting pointers to integers @@ -74,15 +74,15 @@ #ifdef CONFIG_64BIT #define __signed_type(ux) long #else - #define __signed_type(ux) typeof(__builtin_choose_expr(sizeof(ux)>4,1LL,= 1L)) + #define __signed_type(ux) typeof(__builtin_choose_expr(sizeof(ux) > 4, 1= LL, 1L)) #endif -#define __is_nonneg(x,ux) statically_true((__signed_type(ux))(x)>=3D0) +#define __is_nonneg(x, ux) statically_true((__signed_type(ux))(x) >=3D 0) =20 -#define __types_ok(x,y,ux,uy) \ - (__sign_use(x,ux) & __sign_use(y,uy)) +#define __types_ok(x, y, ux, uy) \ + (__sign_use(x, ux) & __sign_use(y, uy)) =20 -#define __types_ok3(x,y,z,ux,uy,uz) \ - (__sign_use(x,ux) & __sign_use(y,uy) & __sign_use(z,uz)) +#define __types_ok3(x, y, z, ux, uy, uz) \ + (__sign_use(x, ux) & __sign_use(y, uy) & __sign_use(z, uz)) =20 #define __cmp_op_min < #define __cmp_op_max > @@ -97,7 +97,7 @@ =20 #define __careful_cmp_once(op, x, y, ux, uy) ({ \ __auto_type ux =3D (x); __auto_type uy =3D (y); \ - BUILD_BUG_ON_MSG(!__types_ok(x,y,ux,uy), \ + BUILD_BUG_ON_MSG(!__types_ok(x, y, ux, uy), \ #op"("#x", "#y") signedness error"); \ __cmp(op, ux, uy); }) =20 @@ -114,7 +114,7 @@ static_assert(__builtin_choose_expr(__is_constexpr((lo) > (hi)), \ (lo) <=3D (hi), true), \ "clamp() low limit " #lo " greater than high limit " #hi); \ - BUILD_BUG_ON_MSG(!__types_ok3(val,lo,hi,uval,ulo,uhi), \ + BUILD_BUG_ON_MSG(!__types_ok3(val, lo, hi, uval, ulo, uhi), \ "clamp("#val", "#lo", "#hi") signedness error"); \ __clamp(uval, ulo, uhi); }) =20 @@ -154,7 +154,7 @@ =20 #define __careful_op3(op, x, y, z, ux, uy, uz) ({ \ __auto_type ux =3D (x); __auto_type uy =3D (y);__auto_type uz =3D (z);\ - BUILD_BUG_ON_MSG(!__types_ok3(x,y,z,ux,uy,uz), \ + 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)); }) =20 @@ -326,9 +326,9 @@ static inline bool in_range32(u32 val, u32 start, u32 l= en) * Use these carefully: no type checking, and uses the arguments * multiple times. Use for obvious constants only. */ -#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(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)) =20 #endif /* _LINUX_MINMAX_H */ --=20 2.47.3 From nobody Thu Oct 2 01:01:46 2025 Received: from fra-out-010.esa.eu-central-1.outbound.mail-perimeter.amazon.com (fra-out-010.esa.eu-central-1.outbound.mail-perimeter.amazon.com [63.178.143.178]) (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 1235330DEDE; Mon, 29 Sep 2025 17:18:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=63.178.143.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759166320; cv=none; b=n00cHHZYMzARModsZO0b0hGGB+F1QjGpeEX/CNg1Jzlqz6GXMj99JXA+jd3MwnqqOGnNSx8xLIaHjSFYbpNRzlYiBfCu0+5EVYaHY0UX5vbo6BSTy0xdkmzoj/dw7muLIum64MsOmmwfTOFy4wMbrPTL/1ptVDHovWQFaH7zSy8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759166320; c=relaxed/simple; bh=7o6n/7afD73I8KmTywLVAVDBtvL4BDHX8Eeq0ODATQ8=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=YTzve9uv6phzHotgrZ/7Cm4EbpWtIeR4vrvz6PPGxQpN4up5Tkf3YBP+DtT8mF5WHESww5pdk7SinNygUB/hdPYakaYasLVCaD86MoZDbcM4ncDjJgZzuouMMUfb2d3luyZy06xCvjL2fUC+KTlHDi9rUWUhI28sdP5PaFnjq1E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.com; dkim=pass (2048-bit key) header.d=amazon.com header.i=@amazon.com header.b=KZ4dqJYa; arc=none smtp.client-ip=63.178.143.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.com header.i=@amazon.com header.b="KZ4dqJYa" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazoncorp2; t=1759166318; x=1790702318; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=0AzpApDbX8CFEbC2P7WHNPH2gra+XDIh1eSQ6GUhJZw=; b=KZ4dqJYajn/i3/xDc9UVYpyJCmggOMzj5R7Y0HXBB4MLr73DDECObn3l jQVR4o3ZupS9I9UEmLn9umii6pIng0pLJk7P1m5ZxjUXrEUaH1SFTGHgv OWeHq/8N2z2C1mqj5pQKu/r2ij+cFoK3goQMz0MHcfqewvwPcbtKpOXT2 Gjs8eKY7+rd12d4gs+3iDyLlFEPpvmXVEgCxFj4W/MTem4Foz7q/ftyu+ Y5gf2TgcaxMBvvuykrHmmUtosaLkjXyn9Zsso7eYccHbOOLBdod00iAUb G1ctaFS+iuAnqHuLG4pyt5s/XsdY6sZ9LUR7E6379cAIJs73w1dL1u4PB A==; X-CSE-ConnectionGUID: 3AosZb/kSiKydUr0eD+ZGA== X-CSE-MsgGUID: z4F0nDunQJKEnkmSSBUbPg== X-IronPort-AV: E=Sophos;i="6.18,302,1751241600"; d="scan'208";a="2732856" Received: from ip-10-6-11-83.eu-central-1.compute.internal (HELO smtpout.naws.eu-central-1.prod.farcaster.email.amazon.dev) ([10.6.11.83]) by internal-fra-out-010.esa.eu-central-1.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Sep 2025 17:18:29 +0000 Received: from EX19MTAEUC001.ant.amazon.com [54.240.197.225:30274] by smtpin.naws.eu-central-1.prod.farcaster.email.amazon.dev [10.0.40.83:2525] with esmtp (Farcaster) id 8940fa71-6d8c-4ed4-8a30-c2376665b275; Mon, 29 Sep 2025 17:18:29 +0000 (UTC) X-Farcaster-Flow-ID: 8940fa71-6d8c-4ed4-8a30-c2376665b275 Received: from EX19D018EUA004.ant.amazon.com (10.252.50.85) by EX19MTAEUC001.ant.amazon.com (10.252.51.193) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Mon, 29 Sep 2025 17:18:28 +0000 Received: from dev-dsk-farbere-1a-46ecabed.eu-west-1.amazon.com (172.19.116.181) by EX19D018EUA004.ant.amazon.com (10.252.50.85) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Mon, 29 Sep 2025 17:18:23 +0000 From: Eliav Farber To: , , , , , , , , CC: Andy Shevchenko , Christoph Hellwig , Dan Carpenter , "Jason A. Donenfeld" , Jens Axboe , Lorenzo Stoakes , Mateusz Guzik , "Matthew Wilcox" , Pedro Falcato Subject: [PATCH v2 07/12 6.6.y] minmax.h: update some comments Date: Mon, 29 Sep 2025 17:17:28 +0000 Message-ID: <20250929171733.20671-8-farbere@amazon.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20250929171733.20671-1-farbere@amazon.com> References: <20250929171733.20671-1-farbere@amazon.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: EX19D040UWA001.ant.amazon.com (10.13.139.22) To EX19D018EUA004.ant.amazon.com (10.252.50.85) Content-Type: text/plain; charset="utf-8" From: David Laight [ Upstream commit 10666e99204818ef45c702469488353b5bb09ec7 ] - Change three to several. - Remove the comment about retaining constant expressions, no longer true. - Realign to nearer 80 columns and break on major punctiation. - Add a leading comment to the block before __signed_type() and __is_nonneg= () Otherwise the block explaining the cast is a bit 'floating'. Reword the rest of that comment to improve readability. Link: https://lkml.kernel.org/r/85b050c81c1d4076aeb91a6cded45fee@AcuMS.acul= ab.com Signed-off-by: David Laight Cc: Andy Shevchenko Cc: Arnd Bergmann Cc: Christoph Hellwig Cc: Dan Carpenter Cc: Jason A. Donenfeld Cc: Jens Axboe Cc: Lorenzo Stoakes Cc: Mateusz Guzik Cc: Matthew Wilcox Cc: Pedro Falcato Signed-off-by: Andrew Morton Signed-off-by: Eliav Farber --- include/linux/minmax.h | 53 +++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 29 deletions(-) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index 51b0d988e322..24e4b372649a 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -8,13 +8,10 @@ #include =20 /* - * min()/max()/clamp() macros must accomplish three things: + * min()/max()/clamp() macros must accomplish several things: * * - Avoid multiple evaluations of the arguments (so side-effects like * "x++" happen only once) when non-constant. - * - Retain result as a constant expressions when called with only - * constant expressions (to avoid tripping VLA warnings in stack - * allocation usage). * - Perform signed v unsigned type-checking (to generate compile * errors instead of nasty runtime surprises). * - Unsigned char/short are always promoted to signed int and can be @@ -31,25 +28,23 @@ * bit #0 set if ok for unsigned comparisons * bit #1 set if ok for signed comparisons * - * In particular, statically non-negative signed integer - * expressions are ok for both. + * In particular, statically non-negative signed integer expressions + * are ok for both. * - * NOTE! Unsigned types smaller than 'int' are implicitly - * converted to 'int' in expressions, and are accepted for - * signed conversions for now. This is debatable. + * NOTE! Unsigned types smaller than 'int' are implicitly converted to 'in= t' + * in expressions, and are accepted for signed conversions for now. + * This is debatable. * - * Note that 'x' is the original expression, and 'ux' is - * the unique variable that contains the value. + * Note that 'x' is the original expression, and 'ux' is the unique variab= le + * that contains the value. * - * We use 'ux' for pure type checking, and 'x' for when - * we need to look at the value (but without evaluating - * it for side effects! Careful to only ever evaluate it - * with sizeof() or __builtin_constant_p() etc). + * We use 'ux' for pure type checking, and 'x' for when we need to look at= the + * value (but without evaluating it for side effects! + * Careful to only ever evaluate it with sizeof() or __builtin_constant_p(= ) etc). * - * Pointers end up being checked by the normal C type - * rules at the actual comparison, and these expressions - * only need to be careful to not cause warnings for - * pointer use. + * Pointers end up being checked by the normal C type rules at the actual + * comparison, and these expressions only need to be careful to not cause + * warnings for pointer use. */ #define __signed_type_use(x, ux) (2 + __is_nonneg(x, ux)) #define __unsigned_type_use(x, ux) (1 + 2 * (sizeof(ux) < 4)) @@ -57,19 +52,19 @@ __signed_type_use(x, ux) : __unsigned_type_use(x, ux)) =20 /* - * To avoid warnings about casting pointers to integers - * of different sizes, we need that special sign type. + * Check whether a signed value is always non-negative. * - * On 64-bit we can just always use 'long', since any - * integer or pointer type can just be cast to that. + * A cast is needed to avoid any warnings from values that aren't signed + * integer types (in which case the result doesn't matter). * - * This does not work for 128-bit signed integers since - * the cast would truncate them, but we do not use s128 - * types in the kernel (we do use 'u128', but they will - * be handled by the !is_signed_type() case). + * On 64-bit any integer or pointer type can safely be cast to 'long'. + * But on 32-bit we need to avoid warnings about casting pointers to integ= ers + * of different sizes without truncating 64-bit values so 'long' or 'long = long' + * must be used depending on the size of the value. * - * NOTE! The cast is there only to avoid any warnings - * from when values that aren't signed integer types. + * This does not work for 128-bit signed integers since the cast would tru= ncate + * them, but we do not use s128 types in the kernel (we do use 'u128', + * but they are handled by the !is_signed_type() case). */ #ifdef CONFIG_64BIT #define __signed_type(ux) long --=20 2.47.3 From nobody Thu Oct 2 01:01:46 2025 Received: from fra-out-012.esa.eu-central-1.outbound.mail-perimeter.amazon.com (fra-out-012.esa.eu-central-1.outbound.mail-perimeter.amazon.com [52.57.120.243]) (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 48BE630BB8C; Mon, 29 Sep 2025 17:19:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=52.57.120.243 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759166344; cv=none; b=OCmm69eGtO23jLZ/MEvZVpcJ4Xav0pNpqCeT31MGcx2mC+lBqtMunu9mp4R99yXWi3z4xPZYfHuNe19yl67Nlr7a8s/ew0TH4dZEZqNTM25RC4cX1C/7krhq1bydZ+eZkT+TIDGvpIV7NzAvwKhnNMR2tqhaRS41Nw2yAhxnzGQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759166344; c=relaxed/simple; bh=18hVRcjiq6jqKyPc+H8fDwSKZo20vfz8WvHD3u05Y74=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Fm0SN0l2DPeYhTXT3j77Pv37cNUrVkAacocyw5FuU8naaDfcMa8QznRczI+OdsvmAf/Tu0YkXMMTiZ/v/PQtjgJYED6GT4XWcCeSVympmiyc5iacHcEr1dCLzmdB5BCKopGwOovF5qmSz3s3v7IqJT0A3hTA8xkNVJVR9czrMv0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.com; dkim=pass (2048-bit key) header.d=amazon.com header.i=@amazon.com header.b=GDDV18f+; arc=none smtp.client-ip=52.57.120.243 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.com header.i=@amazon.com header.b="GDDV18f+" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazoncorp2; t=1759166342; x=1790702342; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=FhepguEQ0VZAS3etCeAAj5D/xXUqupnQ1wf0VPifqWY=; b=GDDV18f+Mu5oQDJkG1g76SQYywKOUJxHSjAAh60RaIvg8AtWVYjREpbI p1axxvPzMEzY9EY5Ed+6fRPPlUG+4STwW60nsjDF5OLtUk4NILh4cJVfR C7UH6j3hq/TLbQZU3UEOaNsyF7oTe4xs7pfNmopiVXm/H56AwKz+kbL// FnLfvQzh/G1Rh9YuIqHcpVJZvavRErrW5vP/dYzM2JFUbPB0osD+UAKJQ Syd6G2cs/ss9XQM2YgUQMU0Tz3GYUOzzLV7o7DJOLM9CG+tWWLbGhTzNP 5mO94Df8wjj8XJYwfli1Lv3y26feMKSfhF9a/DdWARrVbxyk7Kt5mk298 g==; X-CSE-ConnectionGUID: kiy92PszTyaFNR/plWi0ag== X-CSE-MsgGUID: kP6Ix5hySjedcNgKTBQkGA== X-IronPort-AV: E=Sophos;i="6.18,302,1751241600"; d="scan'208";a="2738024" Received: from ip-10-6-6-97.eu-central-1.compute.internal (HELO smtpout.naws.eu-central-1.prod.farcaster.email.amazon.dev) ([10.6.6.97]) by internal-fra-out-012.esa.eu-central-1.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Sep 2025 17:18:50 +0000 Received: from EX19MTAEUA002.ant.amazon.com [54.240.197.232:29438] by smtpin.naws.eu-central-1.prod.farcaster.email.amazon.dev [10.0.40.83:2525] with esmtp (Farcaster) id bf8a1347-cab6-4785-899e-c2428983a3d3; Mon, 29 Sep 2025 17:18:50 +0000 (UTC) X-Farcaster-Flow-ID: bf8a1347-cab6-4785-899e-c2428983a3d3 Received: from EX19D018EUA004.ant.amazon.com (10.252.50.85) by EX19MTAEUA002.ant.amazon.com (10.252.50.126) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Mon, 29 Sep 2025 17:18:48 +0000 Received: from dev-dsk-farbere-1a-46ecabed.eu-west-1.amazon.com (172.19.116.181) by EX19D018EUA004.ant.amazon.com (10.252.50.85) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Mon, 29 Sep 2025 17:18:43 +0000 From: Eliav Farber To: , , , , , , , , CC: Andy Shevchenko , Christoph Hellwig , Dan Carpenter , "Jason A. Donenfeld" , Jens Axboe , Lorenzo Stoakes , Mateusz Guzik , "Matthew Wilcox" , Pedro Falcato Subject: [PATCH v2 08/12 6.6.y] minmax.h: reduce the #define expansion of min(), max() and clamp() Date: Mon, 29 Sep 2025 17:17:29 +0000 Message-ID: <20250929171733.20671-9-farbere@amazon.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20250929171733.20671-1-farbere@amazon.com> References: <20250929171733.20671-1-farbere@amazon.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: EX19D040UWB003.ant.amazon.com (10.13.138.8) To EX19D018EUA004.ant.amazon.com (10.252.50.85) Content-Type: text/plain; charset="utf-8" From: David Laight [ Upstream commit b280bb27a9f7c91ddab730e1ad91a9c18a051f41 ] Since the test for signed values being non-negative only relies on __builtion_constant_p() (not is_constexpr()) it can use the 'ux' variable instead of the caller supplied expression. This means that the #define parameters are only expanded twice. Once in the code and once quoted in the error message. Link: https://lkml.kernel.org/r/051afc171806425da991908ed8688a98@AcuMS.acul= ab.com Signed-off-by: David Laight Cc: Andy Shevchenko Cc: Arnd Bergmann Cc: Christoph Hellwig Cc: Dan Carpenter Cc: Jason A. Donenfeld Cc: Jens Axboe Cc: Lorenzo Stoakes Cc: Mateusz Guzik Cc: Matthew Wilcox Cc: Pedro Falcato Signed-off-by: Andrew Morton Signed-off-by: Eliav Farber --- include/linux/minmax.h | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index 24e4b372649a..6f7ea669d305 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -46,10 +46,10 @@ * comparison, and these expressions only need to be careful to not cause * warnings for pointer use. */ -#define __signed_type_use(x, ux) (2 + __is_nonneg(x, ux)) -#define __unsigned_type_use(x, ux) (1 + 2 * (sizeof(ux) < 4)) -#define __sign_use(x, ux) (is_signed_type(typeof(ux)) ? \ - __signed_type_use(x, ux) : __unsigned_type_use(x, ux)) +#define __signed_type_use(ux) (2 + __is_nonneg(ux)) +#define __unsigned_type_use(ux) (1 + 2 * (sizeof(ux) < 4)) +#define __sign_use(ux) (is_signed_type(typeof(ux)) ? \ + __signed_type_use(ux) : __unsigned_type_use(ux)) =20 /* * Check whether a signed value is always non-negative. @@ -71,13 +71,13 @@ #else #define __signed_type(ux) typeof(__builtin_choose_expr(sizeof(ux) > 4, 1= LL, 1L)) #endif -#define __is_nonneg(x, ux) statically_true((__signed_type(ux))(x) >=3D 0) +#define __is_nonneg(ux) statically_true((__signed_type(ux))(ux) >=3D 0) =20 -#define __types_ok(x, y, ux, uy) \ - (__sign_use(x, ux) & __sign_use(y, uy)) +#define __types_ok(ux, uy) \ + (__sign_use(ux) & __sign_use(uy)) =20 -#define __types_ok3(x, y, z, ux, uy, uz) \ - (__sign_use(x, ux) & __sign_use(y, uy) & __sign_use(z, uz)) +#define __types_ok3(ux, uy, uz) \ + (__sign_use(ux) & __sign_use(uy) & __sign_use(uz)) =20 #define __cmp_op_min < #define __cmp_op_max > @@ -92,7 +92,7 @@ =20 #define __careful_cmp_once(op, x, y, ux, uy) ({ \ __auto_type ux =3D (x); __auto_type uy =3D (y); \ - BUILD_BUG_ON_MSG(!__types_ok(x, y, ux, uy), \ + BUILD_BUG_ON_MSG(!__types_ok(ux, uy), \ #op"("#x", "#y") signedness error"); \ __cmp(op, ux, uy); }) =20 @@ -109,7 +109,7 @@ static_assert(__builtin_choose_expr(__is_constexpr((lo) > (hi)), \ (lo) <=3D (hi), true), \ "clamp() low limit " #lo " greater than high limit " #hi); \ - BUILD_BUG_ON_MSG(!__types_ok3(val, lo, hi, uval, ulo, uhi), \ + BUILD_BUG_ON_MSG(!__types_ok3(uval, ulo, uhi), \ "clamp("#val", "#lo", "#hi") signedness error"); \ __clamp(uval, ulo, uhi); }) =20 @@ -149,7 +149,7 @@ =20 #define __careful_op3(op, x, y, z, ux, uy, uz) ({ \ __auto_type ux =3D (x); __auto_type uy =3D (y);__auto_type uz =3D (z);\ - BUILD_BUG_ON_MSG(!__types_ok3(x, y, z, ux, uy, uz), \ + BUILD_BUG_ON_MSG(!__types_ok3(ux, uy, uz), \ #op"3("#x", "#y", "#z") signedness error"); \ __cmp(op, ux, __cmp(op, uy, uz)); }) =20 --=20 2.47.3 From nobody Thu Oct 2 01:01:46 2025 Received: from fra-out-010.esa.eu-central-1.outbound.mail-perimeter.amazon.com (fra-out-010.esa.eu-central-1.outbound.mail-perimeter.amazon.com [63.178.143.178]) (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 07BEF26ACC; Mon, 29 Sep 2025 17:18:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=63.178.143.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759166343; cv=none; b=GWGX2YYSxmFMBkbChIjpYEyInGYIAV8VQvqMXmemRUsffIzamqWdaxc46ySOSZx7ykUMA9MFrw4m2UZUgx+WVmpEYlovnbjraEkr2tgHaEReq+yJYVLmDCdRXAIHt6meqMUReGj8vUpGGIauPadwvHRhALY4y5ZNvnTWvB4P7is= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759166343; c=relaxed/simple; bh=bSiDtvnimcgo6Y3knq8+w917eJDspbmoX3a4FcQGX4g=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=edXMIiM1381esAFNrBe2GRxQzZqO0NLQvJDdEAPid5sHS26jORa7Hld1BbOC8RIAC3uZo/HG8aQzrkoASeJeJO5KSiokG8WeJZyBtUZ5LUE6sobqySbVdCRv6ltydX5YErsFugvtXVco6ySGc7iBLbu2WlkPJ3r09576xWcyqno= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.com; dkim=pass (2048-bit key) header.d=amazon.com header.i=@amazon.com header.b=Z0Ws96be; arc=none smtp.client-ip=63.178.143.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.com header.i=@amazon.com header.b="Z0Ws96be" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazoncorp2; t=1759166340; x=1790702340; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=U4cCFbpHvYq8yWCMIbT6T5sZXi6GGX+iNVIOVWCWFxY=; b=Z0Ws96be+e+BEfnJ1SGT361rpMmrjF4dItx8EQDHccyg1AUP8+/mECHH inH1ykvtb7x+giGReJd+/B7DeuigGm/cL1bUxVUhlQ4RcYOxY5E7UxYi8 ngFvg39yGVZsHvIC2y/CvRbHZ2g4ewrenwED3ZXbN+d+zatTFABRqI4Wl 0+0QW6f05TYtuFaBxzLG1oe9CIyh2Xzkod+sw0qZd+GHjdiXz3AZ3NSTv mFVcop5dU38Ej1VjCDj1lax9vkWYVcpnlbbwmFzCRA5VND9gQ+0q8jd/c rD1Ru1D700oNS76wKKOaTAmS75o/uPGqN6Bwj2oX6JiAu6ymojqdyRSc+ A==; X-CSE-ConnectionGUID: iKIcxmz/RSC5AJfKP9XelQ== X-CSE-MsgGUID: Au0EJcMRSsCw5pEtC8DTkQ== X-IronPort-AV: E=Sophos;i="6.18,302,1751241600"; d="scan'208";a="2732882" Received: from ip-10-6-3-216.eu-central-1.compute.internal (HELO smtpout.naws.eu-central-1.prod.farcaster.email.amazon.dev) ([10.6.3.216]) by internal-fra-out-010.esa.eu-central-1.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Sep 2025 17:18:58 +0000 Received: from EX19MTAEUB001.ant.amazon.com [54.240.197.226:14488] by smtpin.naws.eu-central-1.prod.farcaster.email.amazon.dev [10.0.22.27:2525] with esmtp (Farcaster) id b8ab3764-a569-4a24-b80b-1ceebcc39e9a; Mon, 29 Sep 2025 17:18:57 +0000 (UTC) X-Farcaster-Flow-ID: b8ab3764-a569-4a24-b80b-1ceebcc39e9a Received: from EX19D018EUA004.ant.amazon.com (10.252.50.85) by EX19MTAEUB001.ant.amazon.com (10.252.51.26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Mon, 29 Sep 2025 17:18:53 +0000 Received: from dev-dsk-farbere-1a-46ecabed.eu-west-1.amazon.com (172.19.116.181) by EX19D018EUA004.ant.amazon.com (10.252.50.85) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Mon, 29 Sep 2025 17:18:48 +0000 From: Eliav Farber To: , , , , , , , , CC: Andy Shevchenko , Christoph Hellwig , Dan Carpenter , "Jason A. Donenfeld" , Jens Axboe , Lorenzo Stoakes , Mateusz Guzik , "Matthew Wilcox" , Pedro Falcato Subject: [PATCH v2 09/12 6.6.y] minmax.h: use BUILD_BUG_ON_MSG() for the lo < hi test in clamp() Date: Mon, 29 Sep 2025 17:17:30 +0000 Message-ID: <20250929171733.20671-10-farbere@amazon.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20250929171733.20671-1-farbere@amazon.com> References: <20250929171733.20671-1-farbere@amazon.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: EX19D040UWB003.ant.amazon.com (10.13.138.8) To EX19D018EUA004.ant.amazon.com (10.252.50.85) Content-Type: text/plain; charset="utf-8" From: David Laight [ Upstream commit a5743f32baec4728711bbc01d6ac2b33d4c67040 ] Use BUILD_BUG_ON_MSG(statically_true(ulo > uhi), ...) for the sanity check of the bounds in clamp(). Gives better error coverage and one less expansion of the arguments. Link: https://lkml.kernel.org/r/34d53778977747f19cce2abb287bb3e6@AcuMS.acul= ab.com Signed-off-by: David Laight Cc: Andy Shevchenko Cc: Arnd Bergmann Cc: Christoph Hellwig Cc: Dan Carpenter Cc: Jason A. Donenfeld Cc: Jens Axboe Cc: Lorenzo Stoakes Cc: Mateusz Guzik Cc: Matthew Wilcox Cc: Pedro Falcato Signed-off-by: Andrew Morton Signed-off-by: Eliav Farber --- include/linux/minmax.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index 6f7ea669d305..91aa1b90c1bb 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -106,8 +106,7 @@ __auto_type uval =3D (val); \ __auto_type ulo =3D (lo); \ __auto_type uhi =3D (hi); \ - static_assert(__builtin_choose_expr(__is_constexpr((lo) > (hi)), \ - (lo) <=3D (hi), true), \ + BUILD_BUG_ON_MSG(statically_true(ulo > uhi), \ "clamp() low limit " #lo " greater than high limit " #hi); \ BUILD_BUG_ON_MSG(!__types_ok3(uval, ulo, uhi), \ "clamp("#val", "#lo", "#hi") signedness error"); \ --=20 2.47.3 From nobody Thu Oct 2 01:01:46 2025 Received: from fra-out-002.esa.eu-central-1.outbound.mail-perimeter.amazon.com (fra-out-002.esa.eu-central-1.outbound.mail-perimeter.amazon.com [3.65.3.180]) (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 4CE2430C0F0; Mon, 29 Sep 2025 17:19:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=3.65.3.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759166343; cv=none; b=VRHBGfDOdzV/bMeFUslZXLZgpffmZDqc3AUpwLK4PpItvy+Y6htHHHnAi7dj1BpJ4rOXzCwP5BmXrU3Mlt94BadnSyjYhON4DmZulKSN6BlruZnyhV2bEn8XvMdNBk0jQ9Rrtn1u6fAp+ucPZjhjzsR5PWoVd2ERs0+Fh5Di6tg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759166343; c=relaxed/simple; bh=vXDjGFm+HJwZhe9olHv5adnVqsW2PTDgXntaLnfVnnY=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Zwrb6PnCVVShvq04GwoP8cygR6bhbLl1JJvmatqfp4FAqDPhNY8cNZpOddELd9BZQ8JbaiXM2mjpdg+zQLNoGep0HPSmp9BX2879qyu8pLI6VPrttAO2EL/4G049Kb4IRMIYAH2ILUr3IRP/8Prd+tSUs7xo0C8WvigIlMcRnvk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.com; dkim=pass (2048-bit key) header.d=amazon.com header.i=@amazon.com header.b=Cjx/71gu; arc=none smtp.client-ip=3.65.3.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.com header.i=@amazon.com header.b="Cjx/71gu" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazoncorp2; t=1759166341; x=1790702341; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=eLjmOO9Imra/tK5RPFIsLXDdpmNXUJwWiKD0++d4PGI=; b=Cjx/71gur07Yety6PpZp+YnHNMTtV/L6Mod9oGtaOtdZZo5GZ/9U5G8U mnFD5XbHZI6Vq0mScYD570Wh4SUvqRpl4i210fsQxpa5C8VScluRGRwia uvX/RUBj6/zekc1xl9ScgwqpM4aYltQSBwggyWHsMz3Kmc2Xuw8JHorMm sEiSknJ6UjY3t9WIdZWTfdswxIzWj1b2qfn9Dz6CMYLQ6qkSFR0LgJFuP aBgw3QRB1zcNsMOgGSVF4X2xQpUU/ZG0yJ1cwHtori9B0lK4gMs2Mv4P8 g0F9x9RXa/b3pjvlKnEWfnoqBMhRLWfANm4zQJSDODTz2lNU5Dd36UOiH w==; X-CSE-ConnectionGUID: P7A2vzeJQ0Wx8RPBsgHO8g== X-CSE-MsgGUID: DCr7nH7xQG2H36LvUAxiyw== X-IronPort-AV: E=Sophos;i="6.18,302,1751241600"; d="scan'208";a="2846592" Received: from ip-10-6-3-216.eu-central-1.compute.internal (HELO smtpout.naws.eu-central-1.prod.farcaster.email.amazon.dev) ([10.6.3.216]) by internal-fra-out-002.esa.eu-central-1.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Sep 2025 17:18:59 +0000 Received: from EX19MTAEUB002.ant.amazon.com [54.240.197.232:8450] by smtpin.naws.eu-central-1.prod.farcaster.email.amazon.dev [10.0.22.27:2525] with esmtp (Farcaster) id 71cefb5d-6881-45b6-82a1-3fda942df70c; Mon, 29 Sep 2025 17:18:59 +0000 (UTC) X-Farcaster-Flow-ID: 71cefb5d-6881-45b6-82a1-3fda942df70c Received: from EX19D018EUA004.ant.amazon.com (10.252.50.85) by EX19MTAEUB002.ant.amazon.com (10.252.51.79) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Mon, 29 Sep 2025 17:18:59 +0000 Received: from dev-dsk-farbere-1a-46ecabed.eu-west-1.amazon.com (172.19.116.181) by EX19D018EUA004.ant.amazon.com (10.252.50.85) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Mon, 29 Sep 2025 17:18:54 +0000 From: Eliav Farber To: , , , , , , , , CC: Andy Shevchenko , Christoph Hellwig , Dan Carpenter , "Jason A. Donenfeld" , Jens Axboe , Lorenzo Stoakes , Mateusz Guzik , "Matthew Wilcox" , Pedro Falcato Subject: [PATCH v2 10/12 6.6.y] minmax.h: move all the clamp() definitions after the min/max() ones Date: Mon, 29 Sep 2025 17:17:31 +0000 Message-ID: <20250929171733.20671-11-farbere@amazon.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20250929171733.20671-1-farbere@amazon.com> References: <20250929171733.20671-1-farbere@amazon.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: EX19D040UWB003.ant.amazon.com (10.13.138.8) To EX19D018EUA004.ant.amazon.com (10.252.50.85) Content-Type: text/plain; charset="utf-8" From: David Laight [ Upstream commit c3939872ee4a6b8bdcd0e813c66823b31e6e26f7 ] At some point the definitions for clamp() got added in the middle of the ones for min() and max(). Re-order the definitions so they are more sensibly grouped. Link: https://lkml.kernel.org/r/8bb285818e4846469121c8abc3dfb6e2@AcuMS.acul= ab.com Signed-off-by: David Laight Cc: Andy Shevchenko Cc: Arnd Bergmann Cc: Christoph Hellwig Cc: Dan Carpenter Cc: Jason A. Donenfeld Cc: Jens Axboe Cc: Lorenzo Stoakes Cc: Mateusz Guzik Cc: Matthew Wilcox Cc: Pedro Falcato Signed-off-by: Andrew Morton Signed-off-by: Eliav Farber --- include/linux/minmax.h | 109 +++++++++++++++++++---------------------- 1 file changed, 51 insertions(+), 58 deletions(-) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index 91aa1b90c1bb..75fb7a6ad4c6 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -99,22 +99,6 @@ #define __careful_cmp(op, x, y) \ __careful_cmp_once(op, x, y, __UNIQUE_ID(x_), __UNIQUE_ID(y_)) =20 -#define __clamp(val, lo, hi) \ - ((val) >=3D (hi) ? (hi) : ((val) <=3D (lo) ? (lo) : (val))) - -#define __clamp_once(val, lo, hi, uval, ulo, uhi) ({ \ - __auto_type uval =3D (val); \ - __auto_type ulo =3D (lo); \ - __auto_type uhi =3D (hi); \ - BUILD_BUG_ON_MSG(statically_true(ulo > uhi), \ - "clamp() low limit " #lo " greater than high limit " #hi); \ - BUILD_BUG_ON_MSG(!__types_ok3(uval, ulo, uhi), \ - "clamp("#val", "#lo", "#hi") signedness error"); \ - __clamp(uval, ulo, uhi); }) - -#define __careful_clamp(val, lo, hi) \ - __clamp_once(val, lo, hi, __UNIQUE_ID(v_), __UNIQUE_ID(l_), __UNIQUE_ID(h= _)) - /** * min - return minimum of two values of the same or compatible types * @x: first value @@ -170,6 +154,22 @@ #define max3(x, y, z) \ __careful_op3(max, x, y, z, __UNIQUE_ID(x_), __UNIQUE_ID(y_), __UNIQUE_ID= (z_)) =20 +/** + * min_t - return minimum of two values, using the specified type + * @type: data type to use + * @x: first value + * @y: second value + */ +#define min_t(type, x, y) __cmp_once(min, type, x, y) + +/** + * max_t - return maximum of two values, using the specified type + * @type: data type to use + * @x: first value + * @y: second value + */ +#define max_t(type, x, y) __cmp_once(max, type, x, y) + /** * min_not_zero - return the minimum that is _not_ zero, unless both are z= ero * @x: value1 @@ -180,6 +180,22 @@ typeof(y) __y =3D (y); \ __x =3D=3D 0 ? __y : ((__y =3D=3D 0) ? __x : min(__x, __y)); }) =20 +#define __clamp(val, lo, hi) \ + ((val) >=3D (hi) ? (hi) : ((val) <=3D (lo) ? (lo) : (val))) + +#define __clamp_once(val, lo, hi, uval, ulo, uhi) ({ \ + __auto_type uval =3D (val); \ + __auto_type ulo =3D (lo); \ + __auto_type uhi =3D (hi); \ + BUILD_BUG_ON_MSG(statically_true(ulo > uhi), \ + "clamp() low limit " #lo " greater than high limit " #hi); \ + BUILD_BUG_ON_MSG(!__types_ok3(uval, ulo, uhi), \ + "clamp("#val", "#lo", "#hi") signedness error"); \ + __clamp(uval, ulo, uhi); }) + +#define __careful_clamp(val, lo, hi) \ + __clamp_once(val, lo, hi, __UNIQUE_ID(v_), __UNIQUE_ID(l_), __UNIQUE_ID(h= _)) + /** * clamp - return a value clamped to a given range with strict typechecking * @val: current value @@ -191,28 +207,30 @@ */ #define clamp(val, lo, hi) __careful_clamp(val, lo, hi) =20 -/* - * ..and if you can't take the strict - * types, you can specify one yourself. - * - * Or not use min/max/clamp at all, of course. - */ - /** - * min_t - return minimum of two values, using the specified type - * @type: data type to use - * @x: first value - * @y: second value + * clamp_t - return a value clamped to a given range using a given type + * @type: the type of variable to use + * @val: current value + * @lo: minimum allowable value + * @hi: maximum allowable value + * + * This macro does no typechecking and uses temporary variables of type + * @type to make all the comparisons. */ -#define min_t(type, x, y) __cmp_once(min, type, x, y) +#define clamp_t(type, val, lo, hi) __careful_clamp((type)(val), (type)(lo)= , (type)(hi)) =20 /** - * max_t - return maximum of two values, using the specified type - * @type: data type to use - * @x: first value - * @y: second value + * clamp_val - return a value clamped to a given range using val's type + * @val: current value + * @lo: minimum allowable value + * @hi: maximum allowable value + * + * This macro does no typechecking and uses temporary variables of whatever + * type the input argument @val is. This is useful when @val is an unsign= ed + * type and @lo and @hi are literals that will otherwise be assigned a sig= ned + * integer type. */ -#define max_t(type, x, y) __cmp_once(max, type, x, y) +#define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi) =20 /* * Do not check the array parameter using __must_be_array(). @@ -257,31 +275,6 @@ */ #define max_array(array, len) __minmax_array(max, array, len) =20 -/** - * clamp_t - return a value clamped to a given range using a given type - * @type: the type of variable to use - * @val: current value - * @lo: minimum allowable value - * @hi: maximum allowable value - * - * This macro does no typechecking and uses temporary variables of type - * @type to make all the comparisons. - */ -#define clamp_t(type, val, lo, hi) __careful_clamp((type)(val), (type)(lo)= , (type)(hi)) - -/** - * clamp_val - return a value clamped to a given range using val's type - * @val: current value - * @lo: minimum allowable value - * @hi: maximum allowable value - * - * This macro does no typechecking and uses temporary variables of whatever - * type the input argument @val is. This is useful when @val is an unsign= ed - * type and @lo and @hi are literals that will otherwise be assigned a sig= ned - * integer type. - */ -#define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi) - static inline bool in_range64(u64 val, u64 start, u64 len) { return (val - start) < len; --=20 2.47.3 From nobody Thu Oct 2 01:01:46 2025 Received: from fra-out-013.esa.eu-central-1.outbound.mail-perimeter.amazon.com (fra-out-013.esa.eu-central-1.outbound.mail-perimeter.amazon.com [63.178.132.221]) (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 D3FEB30CD97; Mon, 29 Sep 2025 17:19:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=63.178.132.221 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759166357; cv=none; b=WpsRVKzYQliXxsKdqv29CAp7L4Tm2rUERuJgFLPHQYmgMWhjOVMzv23mWoiipxqtDuAVt4rLoUfXcuXrfLiF3LMYMDr6YCt8uJ/IMrKcgEf6to5meRrWUxofb93VKI7Iv0JoSerYXhcztTpybXNAXREDTGg3/71vSNZGmX8cj5Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759166357; c=relaxed/simple; bh=M+DzBGorF0x2HwKxbRALu5Mz4okNvOwcKTlN8glrxuQ=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=cJL+0f1YdtxuvsptoUns2oZkqCEVALgkebChZescWaqtoyNuK6IHXqW4s2XcBE/PjliE5TDrFUDGeieB4s8vJp0bEApx9EyJKTWxTJkQT8c73Dd52JNjGJzF3id/XdTDzOtsW9uhvbDlxTozdQKaTbw8S8rw2K9DM1hyDYMljkk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.com; dkim=pass (2048-bit key) header.d=amazon.com header.i=@amazon.com header.b=ZYLY3XlI; arc=none smtp.client-ip=63.178.132.221 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.com header.i=@amazon.com header.b="ZYLY3XlI" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazoncorp2; t=1759166355; x=1790702355; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=8djAgabF22BDsxfYcNFa72Q6mXnC3doUyHk+thXHzBc=; b=ZYLY3XlIO1FgRqlQScWp0sCT6wEx9OubwdW21jujwoWScshtEOFQlQYZ QtEYEPulM6xUBK9akJCWz2tOGzilUFvr3BNcNSyUfG51LcEep866vTmfi 8Ea/Pp829rcSl0jrLDSNZFi4nkgop0PSFu7gfBRV+Ini3SHGFVC+0AER+ Z6ktdeKmOWgFbCO20RJp1FHctr8pdK89zRo5rq9YqgJO/F3phIaZSuJjg 8VjxRtKgQ0LSEZIxpozycMUyjrCJ1bNHACao39YFLh8Uss0Hw8iEUSIs2 Cg+4TLrS/OyWbDQBA2bo/t/5AXR4kIj7ZhpP9GJmnSCGuQd28vYEdx/tG A==; X-CSE-ConnectionGUID: 8FZSb+VXTYWGvX/IVLoNPA== X-CSE-MsgGUID: Uu7aFNV6RLi7W7/WdJb4iw== X-IronPort-AV: E=Sophos;i="6.18,302,1751241600"; d="scan'208";a="2735562" Received: from ip-10-6-11-83.eu-central-1.compute.internal (HELO smtpout.naws.eu-central-1.prod.farcaster.email.amazon.dev) ([10.6.11.83]) by internal-fra-out-013.esa.eu-central-1.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Sep 2025 17:19:04 +0000 Received: from EX19MTAEUC002.ant.amazon.com [54.240.197.228:11352] by smtpin.naws.eu-central-1.prod.farcaster.email.amazon.dev [10.0.4.140:2525] with esmtp (Farcaster) id c68d16a4-f3bc-41e0-add9-99d6e576370b; Mon, 29 Sep 2025 17:19:04 +0000 (UTC) X-Farcaster-Flow-ID: c68d16a4-f3bc-41e0-add9-99d6e576370b Received: from EX19D018EUA004.ant.amazon.com (10.252.50.85) by EX19MTAEUC002.ant.amazon.com (10.252.51.245) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Mon, 29 Sep 2025 17:19:04 +0000 Received: from dev-dsk-farbere-1a-46ecabed.eu-west-1.amazon.com (172.19.116.181) by EX19D018EUA004.ant.amazon.com (10.252.50.85) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Mon, 29 Sep 2025 17:18:59 +0000 From: Eliav Farber To: , , , , , , , , CC: Andy Shevchenko , Christoph Hellwig , Dan Carpenter , "Jason A. Donenfeld" , Jens Axboe , Lorenzo Stoakes , Mateusz Guzik , "Matthew Wilcox" , Pedro Falcato Subject: [PATCH v2 11/12 6.6.y] minmax.h: simplify the variants of clamp() Date: Mon, 29 Sep 2025 17:17:32 +0000 Message-ID: <20250929171733.20671-12-farbere@amazon.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20250929171733.20671-1-farbere@amazon.com> References: <20250929171733.20671-1-farbere@amazon.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: EX19D040UWB003.ant.amazon.com (10.13.138.8) To EX19D018EUA004.ant.amazon.com (10.252.50.85) Content-Type: text/plain; charset="utf-8" From: David Laight [ Upstream commit 495bba17cdf95e9703af1b8ef773c55ef0dfe703 ] Always pass a 'type' through to __clamp_once(), pass '__auto_type' from clamp() itself. The expansion of __types_ok3() is reasonable so it isn't worth the added complexity of avoiding it when a fixed type is used for all three values. Link: https://lkml.kernel.org/r/8f69f4deac014f558bab186444bac2e8@AcuMS.acul= ab.com Signed-off-by: David Laight Cc: Andy Shevchenko Cc: Arnd Bergmann Cc: Christoph Hellwig Cc: Dan Carpenter Cc: Jason A. Donenfeld Cc: Jens Axboe Cc: Lorenzo Stoakes Cc: Mateusz Guzik Cc: Matthew Wilcox Cc: Pedro Falcato Signed-off-by: Andrew Morton Signed-off-by: Eliav Farber --- include/linux/minmax.h | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index 75fb7a6ad4c6..2bbdd5b5e07e 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -183,29 +183,29 @@ #define __clamp(val, lo, hi) \ ((val) >=3D (hi) ? (hi) : ((val) <=3D (lo) ? (lo) : (val))) =20 -#define __clamp_once(val, lo, hi, uval, ulo, uhi) ({ \ - __auto_type uval =3D (val); \ - __auto_type ulo =3D (lo); \ - __auto_type uhi =3D (hi); \ +#define __clamp_once(type, val, lo, hi, uval, ulo, uhi) ({ \ + type uval =3D (val); \ + type ulo =3D (lo); \ + type uhi =3D (hi); \ BUILD_BUG_ON_MSG(statically_true(ulo > uhi), \ "clamp() low limit " #lo " greater than high limit " #hi); \ BUILD_BUG_ON_MSG(!__types_ok3(uval, ulo, uhi), \ "clamp("#val", "#lo", "#hi") signedness error"); \ __clamp(uval, ulo, uhi); }) =20 -#define __careful_clamp(val, lo, hi) \ - __clamp_once(val, lo, hi, __UNIQUE_ID(v_), __UNIQUE_ID(l_), __UNIQUE_ID(h= _)) +#define __careful_clamp(type, val, lo, hi) \ + __clamp_once(type, val, lo, hi, __UNIQUE_ID(v_), __UNIQUE_ID(l_), __UNIQU= E_ID(h_)) =20 /** - * clamp - return a value clamped to a given range with strict typechecking + * clamp - return a value clamped to a given range with typechecking * @val: current value * @lo: lowest allowable value * @hi: highest allowable value * - * This macro does strict typechecking of @lo/@hi to make sure they are of= the - * same type as @val. See the unnecessary pointer comparisons. + * This macro checks @val/@lo/@hi to make sure they have compatible + * signedness. */ -#define clamp(val, lo, hi) __careful_clamp(val, lo, hi) +#define clamp(val, lo, hi) __careful_clamp(__auto_type, val, lo, hi) =20 /** * clamp_t - return a value clamped to a given range using a given type @@ -217,7 +217,7 @@ * This macro does no typechecking and uses temporary variables of type * @type to make all the comparisons. */ -#define clamp_t(type, val, lo, hi) __careful_clamp((type)(val), (type)(lo)= , (type)(hi)) +#define clamp_t(type, val, lo, hi) __careful_clamp(type, val, lo, hi) =20 /** * clamp_val - return a value clamped to a given range using val's type @@ -230,7 +230,7 @@ * type and @lo and @hi are literals that will otherwise be assigned a sig= ned * integer type. */ -#define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi) +#define clamp_val(val, lo, hi) __careful_clamp(typeof(val), val, lo, hi) =20 /* * Do not check the array parameter using __must_be_array(). --=20 2.47.3 From nobody Thu Oct 2 01:01:46 2025 Received: from fra-out-014.esa.eu-central-1.outbound.mail-perimeter.amazon.com (fra-out-014.esa.eu-central-1.outbound.mail-perimeter.amazon.com [18.199.210.3]) (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 779B130CB2A; Mon, 29 Sep 2025 17:19:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=18.199.210.3 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759166377; cv=none; b=Ah7iKEBg1kzp+DM4MA8q2rQgdOIeY/qcGcYyDXLs0K5S+VE+M5goLaNurs+funVKk4AOGtLPIB1zpghKYUhJvx3zXY3vjgJUBnHoBgGgfa0lV1/HDOtWE53WEjC28Ydoa93OufZ8Jjlksw5hdifPsmHqKqGOjL+lqGcK/I6tww8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759166377; c=relaxed/simple; bh=V2L3IilwofpV6SrsfVTvut5ipDXkokIOm+dTBT4i7eQ=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=FC+yuG9e+/Yme3UPcDU6bkk0EXvm+2EOROUtXU5JFIIWLRPbfl3yW9MUKw4mGrAUqilYSMSTUjxmOnsbeyE9yettv7OxDcJou7muItTZEoHjqzqKTvaqzKWYvR6YXiOOHTCeXggUQ95+xbDXccb34ukUO2r8hrTOcniVq8ZzZJQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.com; dkim=pass (2048-bit key) header.d=amazon.com header.i=@amazon.com header.b=Xr0dwe2Q; arc=none smtp.client-ip=18.199.210.3 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.com header.i=@amazon.com header.b="Xr0dwe2Q" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazoncorp2; t=1759166375; x=1790702375; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=k0dUe76w+6n5/NfsPbyefGVCom+xh3GGcFcV3Olhmks=; b=Xr0dwe2QwLtt4BBXcGrmxZ5FvxUEdqA/VpTGeqltwztAFD2hMYATiwTE NOHPyhKLbLV1kWuoMux/6lyJhY10YoZWdcsaCJTuxLis51JJs1019Ka1C vpdQLlowIZiM5rnkTC6UMYjSgKlxkJaisy6zAj8YPiJQ+Fs+ByYnyFg1R Q62oWAKNG0yBhEMeog/Fcm/9TICubJUu4toEoEVWeyYhRmqShXwbKdWyu aYnBMCW0kUUF6kA7HbFea0LpsdB+l1d/98IDxOSBXIC/cd7uTy8bYuD4K bv8Dnm5Y0KRvGPP1ePHo3SSGfHoMe4xMYO8A5TCIxG6CpF5+Gdcx0WSSd Q==; X-CSE-ConnectionGUID: /YF4ysAYT1edLZhScn7q6w== X-CSE-MsgGUID: zvVllrdPQZqpUST9AvFqXw== X-IronPort-AV: E=Sophos;i="6.18,302,1751241600"; d="scan'208";a="2733852" Received: from ip-10-6-11-83.eu-central-1.compute.internal (HELO smtpout.naws.eu-central-1.prod.farcaster.email.amazon.dev) ([10.6.11.83]) by internal-fra-out-014.esa.eu-central-1.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Sep 2025 17:19:25 +0000 Received: from EX19MTAEUA001.ant.amazon.com [54.240.197.233:15554] by smtpin.naws.eu-central-1.prod.farcaster.email.amazon.dev [10.0.1.121:2525] with esmtp (Farcaster) id 4fa984ab-060b-473a-863b-f54a1ea42e84; Mon, 29 Sep 2025 17:19:25 +0000 (UTC) X-Farcaster-Flow-ID: 4fa984ab-060b-473a-863b-f54a1ea42e84 Received: from EX19D018EUA004.ant.amazon.com (10.252.50.85) by EX19MTAEUA001.ant.amazon.com (10.252.50.192) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Mon, 29 Sep 2025 17:19:24 +0000 Received: from dev-dsk-farbere-1a-46ecabed.eu-west-1.amazon.com (172.19.116.181) by EX19D018EUA004.ant.amazon.com (10.252.50.85) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Mon, 29 Sep 2025 17:19:19 +0000 From: Eliav Farber To: , , , , , , , , CC: Andy Shevchenko , Christoph Hellwig , Dan Carpenter , "Jason A. Donenfeld" , Jens Axboe , Lorenzo Stoakes , Mateusz Guzik , "Matthew Wilcox" , Pedro Falcato Subject: [PATCH v2 12/12 6.6.y] minmax.h: remove some #defines that are only expanded once Date: Mon, 29 Sep 2025 17:17:33 +0000 Message-ID: <20250929171733.20671-13-farbere@amazon.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20250929171733.20671-1-farbere@amazon.com> References: <20250929171733.20671-1-farbere@amazon.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: EX19D035UWB004.ant.amazon.com (10.13.138.104) To EX19D018EUA004.ant.amazon.com (10.252.50.85) Content-Type: text/plain; charset="utf-8" From: David Laight [ Upstream commit 2b97aaf74ed534fb838d09867d09a3ca5d795208 ] The bodies of __signed_type_use() and __unsigned_type_use() are much the same size as their names - so put the bodies in the only line that expands them. Similarly __signed_type() is defined separately for 64bit and then used exactly once just below. Change the test for __signed_type from CONFIG_64BIT to one based on gcc defined macros so that the code is valid if it gets used outside of a kernel build. Link: https://lkml.kernel.org/r/9386d1ebb8974fbabbed2635160c3975@AcuMS.acul= ab.com Signed-off-by: David Laight Cc: Andy Shevchenko Cc: Arnd Bergmann Cc: Christoph Hellwig Cc: Dan Carpenter Cc: Jason A. Donenfeld Cc: Jens Axboe Cc: Lorenzo Stoakes Cc: Mateusz Guzik Cc: Matthew Wilcox Cc: Pedro Falcato Signed-off-by: Andrew Morton Signed-off-by: Eliav Farber --- include/linux/minmax.h | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index 2bbdd5b5e07e..eaaf5c008e4d 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -46,10 +46,8 @@ * comparison, and these expressions only need to be careful to not cause * warnings for pointer use. */ -#define __signed_type_use(ux) (2 + __is_nonneg(ux)) -#define __unsigned_type_use(ux) (1 + 2 * (sizeof(ux) < 4)) #define __sign_use(ux) (is_signed_type(typeof(ux)) ? \ - __signed_type_use(ux) : __unsigned_type_use(ux)) + (2 + __is_nonneg(ux)) : (1 + 2 * (sizeof(ux) < 4))) =20 /* * Check whether a signed value is always non-negative. @@ -57,7 +55,7 @@ * A cast is needed to avoid any warnings from values that aren't signed * integer types (in which case the result doesn't matter). * - * On 64-bit any integer or pointer type can safely be cast to 'long'. + * On 64-bit any integer or pointer type can safely be cast to 'long long'. * But on 32-bit we need to avoid warnings about casting pointers to integ= ers * of different sizes without truncating 64-bit values so 'long' or 'long = long' * must be used depending on the size of the value. @@ -66,12 +64,12 @@ * them, but we do not use s128 types in the kernel (we do use 'u128', * but they are handled by the !is_signed_type() case). */ -#ifdef CONFIG_64BIT - #define __signed_type(ux) long +#if __SIZEOF_POINTER__ =3D=3D __SIZEOF_LONG_LONG__ +#define __is_nonneg(ux) statically_true((long long)(ux) >=3D 0) #else - #define __signed_type(ux) typeof(__builtin_choose_expr(sizeof(ux) > 4, 1= LL, 1L)) +#define __is_nonneg(ux) statically_true( \ + (typeof(__builtin_choose_expr(sizeof(ux) > 4, 1LL, 1L)))(ux) >=3D 0) #endif -#define __is_nonneg(ux) statically_true((__signed_type(ux))(ux) >=3D 0) =20 #define __types_ok(ux, uy) \ (__sign_use(ux) & __sign_use(uy)) --=20 2.47.3