Received: by 2002:a05:7412:d1aa:b0:fc:a2b0:25d7 with SMTP id ba42csp23622rdb; Sun, 28 Jan 2024 11:37:44 -0800 (PST) X-Google-Smtp-Source: AGHT+IH6E2ftOLSccor9DAVJSpd2giAT10QM6dneFZSmryOHiTVAA2cq/8mAbwvvuLV83jw17zlB X-Received: by 2002:a05:6830:6605:b0:6e1:1236:987c with SMTP id cp5-20020a056830660500b006e11236987cmr5446334otb.20.1706470664517; Sun, 28 Jan 2024 11:37:44 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706470664; cv=pass; d=google.com; s=arc-20160816; b=FS/xIbq/gcI8lYHh9uCfepuKBSrAvWV9gc9xmu/rmnSxcCVCMa096KvD4TAhuiKv0u 1e+i37mLqxIuiiOyKlFybr7uejJ572N/RGszyo/OpiaRKiKMb97tH2I0wlQ8nbw6/PMZ wmrMdrpf88VSikO3ZWmSFMZD+rdv5QAXVAkcUXdBdQLWQifNLKuaCfsR8gNWAvRdGXRd jgkV5QVoo01J5FvRrH2zgTNhC+UlLGorxlzC0D14mUlnt3QxByz3mIoQ7ig5efg6ZrOo 8TwKQEpe6ApKS/P3O+4OAMmU2WLIhxBb+F8YwRGpkoNdYXJguzjJBDdFXEXjUlGQUgwT r1TA== 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=TRCcDO3q5w0JYVEsx6WTA5bthRI1zvGrwZKfn0reSVw=; fh=jAOu/RGlQxfhPk7PcGNzOfYLHKZO+STAaG1daOB/TAU=; b=mFME9AH5UDnlxohq4c2hXEpJcVZ91I1TC1dDTDae8PznnjT3W2FFrl2POMFvlk22yP sHfYpZCR5DMmT1dIvNQAd94NpllmKfl6NrZF5IoFqOSBVtgz2rlnrLOODck2y9R7bhpD mEMKlVXeHKdbqhB3FuPBLN8bfuFRkhEHJOVLgpVltB2rorFnT6wzPwZXwnvmboz22j8q tZRJNbYGq7zlT0AUjqunDbFFP2sqGGQjk77QV42TWeBbjTDJ1sRWzQoLn+IPj7A1VxAR YLS/xB0CxAAXqz+QLX2eRQeKyNzcWIElSzX3YNrbhX+bMlfGMYs77kakfbm03LXqMC5W 81ig== 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-41911-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-41911-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. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id s26-20020a056a00179a00b006de04ea77dbsi4070273pfg.293.2024.01.28.11.37.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 28 Jan 2024 11:37:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-41911-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; 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-41911-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-41911-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 0ADF0282CBF for ; Sun, 28 Jan 2024 19:37:23 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7C44931A82; Sun, 28 Jan 2024 19:37:14 +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 28CDD33CF1 for ; Sun, 28 Jan 2024 19:37:12 +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=1706470633; cv=none; b=UaaCZ4Vvr51aNsVz1h3n9yXU6qEm2AnH8nbYZ8fyI6vIXTEfQJWXiDr4hGo4y839lVxFqLEYnptcjbCLJr2a0WjZO043yq1wuvAGKJg72awlMRusa8uGOevApFTwDLLlSQm0e8tC7OQqSA4toPE8rwgQlOFGoYoIpaCcC5rk9fQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706470633; c=relaxed/simple; bh=ZrixvhkJhbKZGck4jka4XG69l4/CfS4dYctLM5kLuEc=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: MIME-Version:Content-Type; b=hWICuv9HhKCrA6Wb8UWthvfoFaSwKOkWDQpIpROV5RW4/JbJtemug9+mVJAu2BxEI+BwiyfpvDhTK34Wo/DQACBE3ewbocOfjsNWtlHTlv6y4fx398mYOtNCj1IsXX5h2SUz9jNbidZWORxNqec0P77I1Nu6Aroyxvf+plWIpfk= 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-227-RgW8bFtTPC263FLYeWYyZg-1; Sun, 28 Jan 2024 19:37:05 +0000 X-MC-Unique: RgW8bFtTPC263FLYeWYyZg-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, 28 Jan 2024 19:36:41 +0000 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Sun, 28 Jan 2024 19:36:41 +0000 From: David Laight To: "'linux-kernel@vger.kernel.org'" , "'Linus Torvalds'" , 'Netdev' , "'dri-devel@lists.freedesktop.org'" CC: 'Andy Shevchenko' , 'Andrew Morton' , "'Matthew Wilcox (Oracle)'" , 'Christoph Hellwig' , "'Dan Carpenter'" , 'Linus Walleij' , "'David S . Miller'" , "'linux-btrfs@vger.kernel.org'" , 'Jens Axboe' Subject: [PATCH next 11/11] minmax: min() and max() don't need to return constant expressions Thread-Topic: [PATCH next 11/11] minmax: min() and max() don't need to return constant expressions Thread-Index: AdpSIVojuBa8D6X6RNmhjGfjyElxVg== Date: Sun, 28 Jan 2024 19:36:41 +0000 Message-ID: <30b5bc6c60a147f9985b47fb1cc08d2e@AcuMS.aculab.com> References: <0ca26166dd2a4ff5a674b84704ff1517@AcuMS.aculab.com> In-Reply-To: <0ca26166dd2a4ff5a674b84704ff1517@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(-) 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)