From nobody Wed Dec 17 17:23:03 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 29BDB1A2553; Tue, 16 Jul 2024 18:33:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721154826; cv=none; b=Q8cLhgn+cbBYivTRSfqj8A0seC5UZKWMfNlyuDhhhdzuV4knZR4U1cL0CRs6fP+fWECai11Jv9V1UCi5/VLHURSfVaGkfIO3nV02IhU1kqupV7i4zXwikBkpSa0RvyHRh780+i3T+zRfjsUrCDKqYb9b8zaEUsXSdk+hT62txFk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721154826; c=relaxed/simple; bh=ndvNxnnobdXY1qRGFjwKkxZc5JAWm9Mq+lzm6iZ2oDw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=G6uaVHVqpsRfVZWxzPcJ9cWiv7YuDBXdclsm/QPZixvM/bXuXdwYPjFD4xkNrZQP1mYP3AXcq7fR8/p60tYM6dKt0jXziuOvuNj4wV1OLEw+vaCtYEM+MhtS4E47/y7I4P4FmH3g7yrH5SF2nXFJ7XXuHEcgwJ2uLaYGXs9aRkE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=nsrniOlO; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="nsrniOlO" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D9198C4AF0D; Tue, 16 Jul 2024 18:33:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1721154825; bh=ndvNxnnobdXY1qRGFjwKkxZc5JAWm9Mq+lzm6iZ2oDw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nsrniOlOCA9NbqSZROhsTDlJdmUSh/BIZPZSXpXBQ5Kz0mQHgIaawfAfa05qS7c7Z Nt517df9+ryBl4iNVvRHO0QYiHqNe+f2+PE1Y+cT06qDHrkbbJ6KHOMaZ8ZlcO7uBL Khgw3NMxO2iJoplVwD2WmD+mtCc/ZQp5lIQa17/0f8D4VcOdhAxbfiojHSPVSzgD79 o8wwBgfor7wEvlp+fS96m/lE/l3Eo+M/W1el70kLQ/yex2Nae/moHQ2cKyM+Ef27km 32YbxdsRMNi1/FQ8WHzMyUJxWaXDPW/6BmpEzEBygVxVyjWM9cy0WJFPxNgQt1hs8r d5LKPgNkl/6ww== From: SeongJae Park To: stable@vger.kernel.org, gregkh@linuxfoundation.org Cc: David Laight , linux-kernel@vger.kernel.org, David Laight , Andy Shevchenko , Christoph Hellwig , "Jason A . Donenfeld" , Linus Torvalds , Matthew Wilcox , Andrew Morton , SeongJae Park Subject: [PATCH 5.15.y 7/8] minmax: relax check to allow comparison between unsigned arguments and signed constants Date: Tue, 16 Jul 2024 11:33:32 -0700 Message-Id: <20240716183333.138498-8-sj@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240716183333.138498-1-sj@kernel.org> References: <20240716183333.138498-1-sj@kernel.org> 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 Content-Type: text/plain; charset="utf-8" From: David Laight commit 867046cc7027703f60a46339ffde91a1970f2901 upstream. 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.acul= ab.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 (cherry picked from commit 867046cc7027703f60a46339ffde91a1970f2901) Signed-off-by: SeongJae Park --- 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 f76b7145fc11..dd52969698f7 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -9,13 +9,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 consta= nts. + * - 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 =3D=3D (typeof(y) *)1))) @@ -25,9 +30,14 @@ __builtin_choose_expr(__is_constexpr(is_signed_type(typeof(x))), \ is_signed_type(typeof(x)), 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)) +/* 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) \ + (__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)) =20 #define __cmp_op_min < #define __cmp_op_max > --=20 2.39.2