Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp898584rbb; Sun, 25 Feb 2024 08:57:59 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVZW08VwQ/ZfIgHT5WIc6asVqO54zfmWnLwnTQTshLwi5Xp1fmdUUXGGyJiT6mKZPFgtgW7FvTbp/mcNwuApsdtgaX+RaSG9wQ0tDroSw== X-Google-Smtp-Source: AGHT+IFPM1RiXal44zE3g9HQ1A9wjj5QmScIpyKtZS++xF8IC0GdwrpODh/zv5m5AywAb1HqPwvR X-Received: by 2002:a17:903:8c7:b0:1dc:7362:7365 with SMTP id lk7-20020a17090308c700b001dc73627365mr5537503plb.4.1708880279231; Sun, 25 Feb 2024 08:57:59 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708880279; cv=pass; d=google.com; s=arc-20160816; b=Sej0l+u9uHJcq3nY5mh2eGuGPq2LG/Keeprpiu2N6SMwFY0jwyO704dtrcF6KGswHZ ySY4NPd5Slxx1nIdGOc81wfgTEv9vKP0HpCxAFuW/Zey9TfHDcZ8LhVBq9b8TofQy3wR uJt55HfvL96GoAhwEcI8GtesyNHyjiiZZlr2GvVT4dpRnEwoGNj6nDOuEo1H5l6JwNGi bEWmV2IAimH8C1GVmVvR5HXvYnzZcK+GT1OnzNqj6CUPwXBdxuHppaBzzIxdr3Nr4KIe jKLW/eIaRma8PZQ4A+XOXpocq33WdpSvWs6BaStwHciisHgcbUNikcoLd2W1zCTgaCey 5amw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:content-language:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:accept-language :in-reply-to:references:message-id:date:thread-index:thread-topic :subject:cc:to:from; bh=rsRmR/5ns1TyNV7cG+3v8oWYZhfwk+FvC/YgG4vlK0I=; fh=nGE692sbVdRvha6RYbzMUXtm1nr/30sV7frdtDN2z14=; b=fe0uEvxDMi3WZh28S2NpHMiGZgfd0bMxOGvkqU6wA+5n6Z0PH4A2N/4VCPrnMOohCk bP5XWafM786GJmqNfCkbKFytmN0C5xVF0ctaq6BRzo6q72oHBaXg+DpFQoJcsEHY5XKI upBzxDG03hi2hHP4X3SI/2KibOxWlZjy503sIzBWvqb8V3M1CvU1xLm6hDyb+b7tbOaL lotGhLUlCAcSIRCk3frIsnIPxUhtreMu/BEOisxdlT8l0FUYIgqQVneH9g4TH0BjJeXk oKKZ4QJXwqhn87j30JuoJmgenqZiu/UeHxTusoJzeCaRA5iFh/m0bxezi4JQ8JuD+7sL 71Mg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=aculab.com dmarc=pass fromdomain=aculab.com); spf=pass (google.com: domain of linux-kernel+bounces-80220-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80220-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=aculab.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id x16-20020a1709027c1000b001da1b31fc38si2342074pll.497.2024.02.25.08.57.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Feb 2024 08:57:59 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-80220-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=aculab.com dmarc=pass fromdomain=aculab.com); spf=pass (google.com: domain of linux-kernel+bounces-80220-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80220-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=aculab.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id EB48A281C3B for ; Sun, 25 Feb 2024 16:57:58 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 83C1417BD4; Sun, 25 Feb 2024 16:57:12 +0000 (UTC) Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.86.151]) (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 0ABE1182D4 for ; Sun, 25 Feb 2024 16:57:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.58.86.151 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708880232; cv=none; b=KjBekuj2Yl4gVKTGZ6o55r+1pUvxKFbafedZJ6dlrDv7YeSE89OBHpLHvbUD9j7O6UQyjCpkwtMfpresrdku99GOjA38AIYIdl6U0KsSV277+tYjkXHnevCtY/2ywwQzrfhxC4TlFOkKIOG/FN2Zo3x+Il4FUeQuWg4WoZVTemc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708880232; c=relaxed/simple; bh=ldiOEy1AqqZRm2jpxplq9I8qQHwbubdPAOsFwLgRl3Q=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: MIME-Version:Content-Type; b=UZ4KMA78Pkf0Xx4aIBjXSX5UVT3zLrXbA9LLdBBJ+A306e+5+MgilN90aZJW9A0G4l/YIXZ3nu84yHHgoJffJsAULZvNPTjIGenC8IdPKCBvxlS/zXlFXUWHYNyfB0kR5USXpu6Z1E8N7MPIM1Ja6M1hkveTiDiv2WtvviSnV3M= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ACULAB.COM; spf=pass smtp.mailfrom=aculab.com; arc=none smtp.client-ip=185.58.86.151 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ACULAB.COM Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=aculab.com Received: from AcuMS.aculab.com (156.67.243.121 [156.67.243.121]) by relay.mimecast.com with ESMTP with both STARTTLS and AUTH (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id uk-mta-209-DBxHGHkZP8yW0XsruhqEOw-1; Sun, 25 Feb 2024 16:57:00 +0000 X-MC-Unique: DBxHGHkZP8yW0XsruhqEOw-1 Received: from AcuMS.Aculab.com (10.202.163.6) by AcuMS.aculab.com (10.202.163.6) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Sun, 25 Feb 2024 16:56:59 +0000 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Sun, 25 Feb 2024 16:56:59 +0000 From: David Laight To: "'linux-kernel@vger.kernel.org'" , "'Linus Torvalds'" , 'Netdev' , "'dri-devel@lists.freedesktop.org'" CC: 'Jens Axboe' , "'Matthew Wilcox (Oracle)'" , 'Christoph Hellwig' , "'linux-btrfs@vger.kernel.org'" , "'Andrew Morton'" , 'Andy Shevchenko' , "'David S . Miller'" , 'Dan Carpenter' , "'Jani Nikula'" Subject: [PATCH next v2 11/11] minmax: min() and max() don't need to return constant expressions Thread-Topic: [PATCH next v2 11/11] minmax: min() and max() don't need to return constant expressions Thread-Index: AdpoC6KUHy5Z1N7yRkiaBkc7ZdEdRQ== Date: Sun, 25 Feb 2024 16:56:58 +0000 Message-ID: References: <0fff52305e584036a777f440b5f474da@AcuMS.aculab.com> In-Reply-To: <0fff52305e584036a777f440b5f474da@AcuMS.aculab.com> Accept-Language: en-GB, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: aculab.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable After changing the handful of places max() was used to size an on-stack array to use max_const() it is no longer necessary for min() and max() to return constant expressions from constant inputs. Remove the associated logic to reduce the expanded text. Remove the 'hack' that allowed max(bool, bool). Fixup the initial block comment to match current reality. Signed-off-by: David Laight --- include/linux/minmax.h | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) Changes for v2: - Typographical and spelling corrections to the commit messages. Patches unchanged. diff --git a/include/linux/minmax.h b/include/linux/minmax.h index c08916588425..5e65c98ff256 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 @@ -22,13 +19,19 @@ * - 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. + * + * The return value of min()/max() is not a constant expression for + * constant parameters - so will trigger a VLA warging if used to size + * an on-stack array. + * Instead use min_const() or max_const() which do generate constant + * expressions and are also valid for static initialisers. */ #define __typecheck(x, y) \ =09(!!(sizeof((typeof(x) *)1 =3D=3D (typeof(y) *)1))) =20 /* Allow unsigned compares against non-negative signed constants. */ #define __is_ok_unsigned(x) \ -=09(is_unsigned_type(typeof(x)) || (__is_constexpr(x) ? (x) + 0 >=3D 0 : 0= )) +=09(is_unsigned_type(typeof(x)) || (__is_constexpr(x) ? (x) >=3D 0 : 0)) =20 /* Check for signed after promoting unsigned char/short to int */ #define __is_ok_signed(x) is_signed_type(typeof((x) + 0)) @@ -53,12 +56,10 @@ =09typeof(y) __y_##uniq =3D (y);=09=09\ =09__cmp(op, __x_##uniq, __y_##uniq); }) =20 -#define __careful_cmp(op, x, y, uniq)=09=09=09=09\ -=09__builtin_choose_expr(__is_constexpr((x) - (y)),=09\ -=09=09__cmp(op, x, y),=09=09=09=09\ -=09=09({ _Static_assert(__types_ok(x, y),=09=09\ -=09=09=09#op "(" #x ", " #y ") signedness error, fix types or consider u" = #op "() before " #op "_t()"); \ -=09=09__cmp_once(op, x, y, uniq); })) +#define __careful_cmp(op, x, y, uniq) ({=09\ +=09_Static_assert(__types_ok(x, y),=09\ +=09=09#op "(" #x ", " #y ") signedness error, fix types or consider u" #op= "() before " #op "_t()"); \ +=09__cmp_once(op, x, y, uniq); }) =20 #define __careful_cmp_const(op, x, y)=09=09=09=09\ =09(BUILD_BUG_ON_ZERO(!__is_constexpr((x) - (y))) +=09\ --=20 2.17.1 - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1= PT, UK Registration No: 1397386 (Wales)