From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.ozlabs.org (lists.ozlabs.org [112.213.38.117]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 75583CCD1A2 for ; Fri, 17 Oct 2025 09:12:02 +0000 (UTC) Received: from boromir.ozlabs.org (localhost [127.0.0.1]) by lists.ozlabs.org (Postfix) with ESMTP id 4cnzbj0Z4dz3cYL; Fri, 17 Oct 2025 20:12:01 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; arc=none smtp.remote-ip=52.34.181.151 ARC-Seal: i=1; a=rsa-sha256; d=lists.ozlabs.org; s=201707; t=1760692321; cv=none; b=Em+YtT9Q6zXgyUORcIUiOXkPx3K0oJIkzSG9MNr6a6pVERVqL69yQWuzdnBUCxskY/iEWlsuaqN0din7asI33/XOQNMicbgiaChquspZ9HuQqrqW4huDC3m/vWqTjLlgUscKWyggUVKbPQZwoDa94qoc2ZlvL+LlDL3oGYBSrU8tFX7sbvUpl8mrqDl+8q987RSTW8OmYWUMLI/r82LFDpr4xuBNmF576qg+S+pJfPo3e/rkCHPI+oAsHRQtnQbf5x23Gyqa58H8bOZYOIyygB7RTWI3ILO8IqY1QnI9ctXS+etRIh9sBY986hqst1dQfnoyyEPlZ0qtMP1orL0txw== ARC-Message-Signature: i=1; a=rsa-sha256; d=lists.ozlabs.org; s=201707; t=1760692321; c=relaxed/relaxed; bh=fgKTaT7jj0zRx6jbQ7DJOMsaZramokk9q66dhz8aLl4=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=MP0BpL4SLGdH6XQlNhn/GzUHW5rcz1IrtXDFY22XF6FrMzVEibNehHPypspxxOVymWbeshH5BA1zVkTnrvtM3jvXzih9/gnPuufi2tSgZUC/4a32LGql4QN2CPv+TYTBMacM7TAQO7+QoTr9ikxdpyaV0KhTomRicfk4EY7ZZhzba7IjiD7q9mMLfbGcEK7rDq5RXJhEZBLfATIJDA9KE/0rGccCsaINAXTeQI2gkQVlMQu0AQUn3hd24ntjnyZTGH0YLiQS2cw1/cLclT3kdDPfb0XqR3CHAsBrH/C6j0Yg9BKDRbTlZja2Gc7l7vAUO6pB9SJHvsoW53hPKuec0Q== ARC-Authentication-Results: i=1; lists.ozlabs.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; dkim=pass (2048-bit key; unprotected) header.d=amazon.com header.i=@amazon.com header.a=rsa-sha256 header.s=amazoncorp2 header.b=pAOSc2/6; dkim-atps=neutral; spf=pass (client-ip=52.34.181.151; helo=pdx-out-007.esa.us-west-2.outbound.mail-perimeter.amazon.com; envelope-from=prvs=378230090=farbere@amazon.com; receiver=lists.ozlabs.org) smtp.mailfrom=amazon.com Authentication-Results: lists.ozlabs.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=amazon.com header.i=@amazon.com header.a=rsa-sha256 header.s=amazoncorp2 header.b=pAOSc2/6; dkim-atps=neutral Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=amazon.com (client-ip=52.34.181.151; helo=pdx-out-007.esa.us-west-2.outbound.mail-perimeter.amazon.com; envelope-from=prvs=378230090=farbere@amazon.com; receiver=lists.ozlabs.org) Received: from pdx-out-007.esa.us-west-2.outbound.mail-perimeter.amazon.com (pdx-out-007.esa.us-west-2.outbound.mail-perimeter.amazon.com [52.34.181.151]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4cnzbh1WRkz2xcB for ; Fri, 17 Oct 2025 20:12:00 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazoncorp2; t=1760692320; x=1792228320; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=fgKTaT7jj0zRx6jbQ7DJOMsaZramokk9q66dhz8aLl4=; b=pAOSc2/6URb+XhEyxQwqQNrlSjIXpOrI6ycsn6Q8Ifayu4MZRR5BvF7i g9fNINfQLGuuIknfoBNaxONrKa4nILV+8+fIM2bCSDGCWKY1k1T3PZScw G50atsMobYOOz2ke8bsePJyR3n3AP3Wskas0Txl0HX0RPZThIVoIQpudR X2i4TJ2xIpcuVmD/L9yKYwj2aKgC4e8NCB2VTXXtgr6154M0vqoTlJR7M 0MUh7dRAag/7AIO7Fvq/R6+lt4G4XPUK2KzxBn6oGR1nJ7YueQQzAqSPB 6DZCX0oejbepwB/r2QGrA9IvAhm76cyrNX/FuGFzaRe0nsarkWVLf99nz w==; X-CSE-ConnectionGUID: qMLkkpbwSXiJYW/k24lLIQ== X-CSE-MsgGUID: 6+Ot3sKfRcmDJhoUWJQSSg== X-IronPort-AV: E=Sophos;i="6.18,263,1751241600"; d="scan'208";a="5073508" Received: from ip-10-5-9-48.us-west-2.compute.internal (HELO smtpout.naws.us-west-2.prod.farcaster.email.amazon.dev) ([10.5.9.48]) by internal-pdx-out-007.esa.us-west-2.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Oct 2025 09:11:58 +0000 Received: from EX19MTAUWB002.ant.amazon.com [205.251.233.111:26055] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.16.196:2525] with esmtp (Farcaster) id 1b11fa41-3c34-464f-a82b-488498e167de; Fri, 17 Oct 2025 09:11:58 +0000 (UTC) X-Farcaster-Flow-ID: 1b11fa41-3c34-464f-a82b-488498e167de Received: from EX19D001UWA001.ant.amazon.com (10.13.138.214) by EX19MTAUWB002.ant.amazon.com (10.250.64.231) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Fri, 17 Oct 2025 09:11:57 +0000 Received: from dev-dsk-farbere-1a-46ecabed.eu-west-1.amazon.com (172.19.116.181) by EX19D001UWA001.ant.amazon.com (10.13.138.214) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.20; Fri, 17 Oct 2025 09:11:42 +0000 From: Eliav Farber To: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , CC: Linus Torvalds , David Laight , Arnd Bergmann Subject: [PATCH v2 20/27 5.10.y] minmax: fix up min3() and max3() too Date: Fri, 17 Oct 2025 09:05:12 +0000 Message-ID: <20251017090519.46992-21-farbere@amazon.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20251017090519.46992-1-farbere@amazon.com> References: <20251017090519.46992-1-farbere@amazon.com> X-Mailing-List: linux-erofs@lists.ozlabs.org List-Id: List-Help: List-Owner: List-Post: List-Subscribe: , , List-Unsubscribe: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [172.19.116.181] X-ClientProxiedBy: EX19D031UWC002.ant.amazon.com (10.13.139.212) To EX19D001UWA001.ant.amazon.com (10.13.138.214) 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) +#define __careful_op3(op, x, y, z, ux, uy, uz) ({ \ + __auto_type ux = (x); __auto_type uy = (y);__auto_type uz = (z);\ + BUILD_BUG_ON_MSG(!__types_ok3(x,y,z,ux,uy,uz), \ + #op"3("#x", "#y", "#z") signedness error"); \ + __cmp(op, ux, __cmp(op, uy, uz)); }) + /** * min3 - return minimum of three values * @x: first value * @y: second value * @z: third value */ -#define min3(x, y, z) min((typeof(x))min(x, y), z) +#define min3(x, y, z) \ + __careful_op3(min, x, y, z, __UNIQUE_ID(x_), __UNIQUE_ID(y_), __UNIQUE_ID(z_)) /** * max3 - return maximum of three values @@ -166,7 +173,8 @@ * @y: second value * @z: third value */ -#define max3(x, y, z) max((typeof(x))max(x, y), z) +#define max3(x, y, z) \ + __careful_op3(max, x, y, z, __UNIQUE_ID(x_), __UNIQUE_ID(y_), __UNIQUE_ID(z_)) /** * min_not_zero - return the minimum that is _not_ zero, unless both are zero -- 2.47.3