From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from pdx-out-011.esa.us-west-2.outbound.mail-perimeter.amazon.com (pdx-out-011.esa.us-west-2.outbound.mail-perimeter.amazon.com [52.35.192.45]) (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 956B02D6638; Fri, 17 Oct 2025 09:09:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=52.35.192.45 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760692197; cv=none; b=lqyVXguKj2tXeSLv9uSTPvxGxQZyVXRX2WSxQmvLWw7sxqZUHvP7T8Qf23Gaw9s4c+Dp243Hq1aKSyzNLyZpKLoZC1sXe2FhADu+uSOLVoiWld81CuiBGCUJAjvZAs7d2mv9zrsr5XX7THY0cD1jXZCDdxC6PlNQJBgbXqE/TfQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760692197; c=relaxed/simple; bh=OMdwAX0D4RWxyOV+A/UOqWcXqJeRVU0cG+oVZfFnsG4=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=ArlKDeDT7yNGYKy6GMTPg6ydy9ajYD5rnnHHhUuLtaJuaqUqOWkJzT7GQtogpS3lDaRvlGBirbk2kAEmHoS8OavoxpqBWnyfuuR6tejIWaE5jywZmGg4HEijRuwD3fpszxcuCSj312Kv0y42FxKjMSoOSZFbWZwgRPPhtKG4qyU= 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=iVN/SnvN; arc=none smtp.client-ip=52.35.192.45 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="iVN/SnvN" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazoncorp2; t=1760692195; x=1792228195; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Up26bfkjgG7bBpXTUDNOne3Gaz9xL27H6G5Ic5zP1jo=; b=iVN/SnvNcBBem03Al1EKX6dybktoakLheX4tzCAyFI90YYpMcnC8ARfT L+XOFjKKxSdSUDyPhKxSiZX5VSEDCkBla4btwA4UyB9+5LY3TPGjOuG6D JAHPEgeLteH6OPHLBLHUoEtdr2KUTytQwj5HxXiooiHlr8EuWwdaApJ3K 70CLZfLPecz+cyHptl6OONkrDiNghjVixXvU0Nd5sNBLfjfibdOIkdxS+ 8M2N84txGXUeL0p/nC87sAikkCGdxi43tMuByH5ev1EG9QxUdLzws0Af8 /+gLqqWxriQ1NMY9H4tda7CV0KN21NU7yoKXAo/mxAVGkULEMEy5gpUsT g==; X-CSE-ConnectionGUID: pM8WHE1nRvKgwn/rOnJAWg== X-CSE-MsgGUID: YGz+lVfQQYmjOS9lVJ+cjQ== X-IronPort-AV: E=Sophos;i="6.19,236,1754956800"; d="scan'208";a="4861413" Received: from ip-10-5-6-203.us-west-2.compute.internal (HELO smtpout.naws.us-west-2.prod.farcaster.email.amazon.dev) ([10.5.6.203]) by internal-pdx-out-011.esa.us-west-2.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Oct 2025 09:09:55 +0000 Received: from EX19MTAUWB001.ant.amazon.com [205.251.233.51:2949] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.38.191:2525] with esmtp (Farcaster) id c5c8bfd9-8a04-4ce3-815c-bb9754b4108b; Fri, 17 Oct 2025 09:09:54 +0000 (UTC) X-Farcaster-Flow-ID: c5c8bfd9-8a04-4ce3-815c-bb9754b4108b Received: from EX19D001UWA001.ant.amazon.com (10.13.138.214) by EX19MTAUWB001.ant.amazon.com (10.250.64.248) 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:09:35 +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:09:20 +0000 From: Eliav Farber To: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , CC: Christoph Hellwig , Linus Torvalds Subject: [PATCH v2 12/27 5.10.y] minmax: relax check to allow comparison between unsigned arguments and signed constants Date: Fri, 17 Oct 2025 09:05:04 +0000 Message-ID: <20251017090519.46992-13-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> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: EX19D037UWB003.ant.amazon.com (10.13.138.115) To EX19D001UWA001.ant.amazon.com (10.13.138.214) From: David Laight [ Upstream commit 867046cc7027703f60a46339ffde91a1970f2901 ] Allow (for example) min(unsigned_var, 20). The opposite min(signed_var, 20u) is still errored. Since a comparison between signed and unsigned never makes the unsigned value negative it is only necessary to adjust the __types_ok() test. Link: https://lkml.kernel.org/r/633b64e2f39e46bb8234809c5595b8c7@AcuMS.aculab.com Signed-off-by: David Laight Cc: Andy Shevchenko Cc: Christoph Hellwig Cc: Jason A. Donenfeld Cc: Linus Torvalds Cc: Matthew Wilcox (Oracle) Signed-off-by: Andrew Morton Signed-off-by: Eliav Farber --- include/linux/minmax.h | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index 842c1db62ffe..2ec559284a9f 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -10,13 +10,18 @@ /* * min()/max()/clamp() macros must accomplish three things: * - * - avoid multiple evaluations of the arguments (so side-effects like + * - Avoid multiple evaluations of the arguments (so side-effects like * "x++" happen only once) when non-constant. - * - perform signed v unsigned type-checking (to generate compile - * errors instead of nasty runtime surprises). - * - retain result as a constant expressions when called with only + * - 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 + * compared against signed or unsigned arguments. + * - Unsigned arguments can be compared against non-negative signed constants. + * - Comparison of a signed argument against an unsigned constant fails + * even if the constant is below __INT_MAX__ and could be cast to int. */ #define __typecheck(x, y) \ (!!(sizeof((typeof(x) *)1 == (typeof(y) *)1))) @@ -26,9 +31,14 @@ __builtin_choose_expr(__is_constexpr(is_signed_type(typeof(x))), \ is_signed_type(typeof(x)), 0) -#define __types_ok(x, y) \ - (__is_signed(x) == __is_signed(y) || \ - __is_signed((x) + 0) == __is_signed((y) + 0)) +/* True for a non-negative signed int constant */ +#define __is_noneg_int(x) \ + (__builtin_choose_expr(__is_constexpr(x) && __is_signed(x), x, -1) >= 0) + +#define __types_ok(x, y) \ + (__is_signed(x) == __is_signed(y) || \ + __is_signed((x) + 0) == __is_signed((y) + 0) || \ + __is_noneg_int(x) || __is_noneg_int(y)) #define __cmp_op_min < #define __cmp_op_max > -- 2.47.3