From nobody Thu Apr 25 13:50:55 2024 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 27BA1C6FA82 for ; Fri, 23 Sep 2022 10:06:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230232AbiIWKGj (ORCPT ); Fri, 23 Sep 2022 06:06:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47278 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229724AbiIWKGh (ORCPT ); Fri, 23 Sep 2022 06:06:37 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5E10E12FF39 for ; Fri, 23 Sep 2022 03:06:35 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id ED1EF6221E for ; Fri, 23 Sep 2022 10:06:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BE8B9C433C1; Fri, 23 Sep 2022 10:06:33 +0000 (UTC) Authentication-Results: smtp.kernel.org; dkim=pass (1024-bit key) header.d=zx2c4.com header.i=@zx2c4.com header.b="LfjEOtMd" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zx2c4.com; s=20210105; t=1663927591; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=DOibG2G4RROJyTimhcP/64dWLHwNn7uLTgHZvtO2iSw=; b=LfjEOtMd2yrEyJ8RUOs6dRhOhIAHAAZEXwDfPZyzOUCl536hKxPPc+xtRwxU21wFzNipRT 8Nrm4EPfVrLek/A7ChGjfds0kvbAq13FFU45oTqUfyBpzqpQiDCn47NaHHpKIMZrF8IzjE cDdxVn/NlDhlySEV49Z+TDvXtNH/PQM= Received: by mail.zx2c4.com (ZX2C4 Mail Server) with ESMTPSA id e806e730 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Fri, 23 Sep 2022 10:06:31 +0000 (UTC) From: "Jason A. Donenfeld" To: linux-kernel@vger.kernel.org Cc: "Jason A. Donenfeld" , Andy Shevchenko , Andrew Morton , Kees Cook Subject: [PATCH] minmax: clamp more efficiently by avoiding extra comparison Date: Fri, 23 Sep 2022 12:06:21 +0200 Message-Id: <20220923100621.3888015-1-Jason@zx2c4.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Currently the clamp algorithm does: if (val > hi) val =3D hi; if (val < lo) val =3D lo; But since hi > lo by definition, this can be made more efficient with: if (val > hi) val =3D hi; else if (val < lo) val =3D lo; So fix up the clamp and clamp_t functions to do this, adding the same argument checking as for min and min_t. Cc: Andy Shevchenko Cc: Andrew Morton Cc: Kees Cook Signed-off-by: Jason A. Donenfeld --- include/linux/minmax.h | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/include/linux/minmax.h b/include/linux/minmax.h index 5433c08fcc68..0153c9eba013 100644 --- a/include/linux/minmax.h +++ b/include/linux/minmax.h @@ -19,42 +19,58 @@ #define __typecheck(x, y) \ (!!(sizeof((typeof(x) *)1 =3D=3D (typeof(y) *)1))) =20 #define __no_side_effects(x, y) \ (__is_constexpr(x) && __is_constexpr(y)) =20 #define __safe_cmp(x, y) \ (__typecheck(x, y) && __no_side_effects(x, y)) =20 #define __cmp(x, y, op) ((x) op (y) ? (x) : (y)) =20 #define __cmp_once(x, y, unique_x, unique_y, op) ({ \ typeof(x) unique_x =3D (x); \ typeof(y) unique_y =3D (y); \ __cmp(unique_x, unique_y, op); }) =20 #define __careful_cmp(x, y, op) \ __builtin_choose_expr(__safe_cmp(x, y), \ __cmp(x, y, op), \ __cmp_once(x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y), op)) =20 +#define __clamp(val, lo, hi) \ + ((val) >=3D (hi) ? (hi) : ((val) <=3D (lo) ? (lo) : (val))) + +#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); \ + __clamp(unique_val, unique_lo, unique_hi); }) + +#define __careful_clamp(val, lo, hi) \ + __builtin_choose_expr(__typecheck(val, lo) && __typecheck(val, hi) && \ + __is_constexpr(val) && \ + __is_constexpr(lo) && __is_constexpr(hi), \ + __clamp(val, lo, hi), \ + __clamp_once(val, lo, hi, __UNIQUE_ID(__val), __UNIQUE_ID(__lo), __UNIQU= E_ID(__hi))) + /** * min - return minimum of two values of the same or compatible types * @x: first value * @y: second value */ #define min(x, y) __careful_cmp(x, y, <) =20 /** * max - return maximum of two values of the same or compatible types * @x: first value * @y: second value */ #define max(x, y) __careful_cmp(x, y, >) =20 /** * 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) @@ -68,78 +84,78 @@ #define max3(x, y, z) max((typeof(x))max(x, y), z) =20 /** * min_not_zero - return the minimum that is _not_ zero, unless both are z= ero * @x: value1 * @y: value2 */ #define min_not_zero(x, y) ({ \ typeof(x) __x =3D (x); \ typeof(y) __y =3D (y); \ __x =3D=3D 0 ? __y : ((__y =3D=3D 0) ? __x : min(__x, __y)); }) =20 /** * clamp - return a value clamped to a given range with strict 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. */ -#define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi) +#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. */ =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) __careful_cmp((type)(x), (type)(y), <) =20 /** * 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) __careful_cmp((type)(x), (type)(y), >) =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) min_t(type, max_t(type, val, lo), hi) +#define clamp_t(type, val, lo, hi) __careful_clamp((type)(val), (type)(lo)= , (type)(hi)) =20 /** * 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) =20 /** * swap - swap values of @a and @b * @a: first value * @b: second value */ #define swap(a, b) \ do { typeof(a) __tmp =3D (a); (a) =3D (b); (b) =3D __tmp; } while (0) --=20 2.37.3