From mboxrd@z Thu Jan 1 00:00:00 1970 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4D949C3DA49 for ; Tue, 30 Jul 2024 12:04:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D2B986B0088; Tue, 30 Jul 2024 08:04:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CDB756B0089; Tue, 30 Jul 2024 08:04:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B7CAD6B008A; Tue, 30 Jul 2024 08:04:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 969886B0088 for ; Tue, 30 Jul 2024 08:04:11 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 45A52C020F for ; Tue, 30 Jul 2024 12:04:11 +0000 (UTC) X-FDA: 82396285902.02.67E2A1E Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.86.151]) by imf17.hostedemail.com (Postfix) with ESMTP id 5839E40028 for ; Tue, 30 Jul 2024 12:04:07 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=none; spf=pass (imf17.hostedemail.com: domain of david.laight@aculab.com designates 185.58.86.151 as permitted sender) smtp.mailfrom=david.laight@aculab.com; dmarc=pass (policy=none) header.from=aculab.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722340994; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=HJcMGpt6pIDHz2KqeFBadQCCCFtnqARC6nbFZdZBOfY=; b=4MWgCl00/JVOznvRb6Cf/3dpMICdRgLDnCEuSMIjpqFOrfCT/IGuFqSUcjj2QRg1u/UtiE CiqME0d7ydl4AKhgBtO5cejYcEmjGtmCUX0O1H4mHVvJP5PjDFrTUCvHAwB0UdvB7QMnxZ uJTbh16M2RfZT05QoEF7XFAi2x0uOVg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722340994; a=rsa-sha256; cv=none; b=bUn4c+tLNCTgPprnU8Junrc+IVEYZ0y2kYX6r1XtVYMmDtgfg0AJqmjaMMYvmSzkujXa19 XFXhlZCSLhskCIiF0p8UrsXs39CIU22LBEDQRfPEzgc+XbRtNFHPGrVju48qJcNBmatu/e thFxKq3CXclbYTzveRbb3OJwiB/tKc4= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=none; spf=pass (imf17.hostedemail.com: domain of david.laight@aculab.com designates 185.58.86.151 as permitted sender) smtp.mailfrom=david.laight@aculab.com; dmarc=pass (policy=none) header.from=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-319-YJgdhREpNjWCHCFE7JUojg-1; Tue, 30 Jul 2024 13:04:04 +0100 X-MC-Unique: YJgdhREpNjWCHCFE7JUojg-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; Tue, 30 Jul 2024 13:03:24 +0100 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Tue, 30 Jul 2024 13:03:24 +0100 From: David Laight To: 'Arnd Bergmann' , Linus Torvalds CC: "linux-kernel@vger.kernel.org" , Jens Axboe , Matthew Wilcox , Christoph Hellwig , Andrew Morton , "Andy Shevchenko" , Dan Carpenter , "Jason A . Donenfeld" , "pedro.falcato@gmail.com" , Mateusz Guzik , "linux-mm@kvack.org" , "Lorenzo Stoakes" Subject: RE: [PATCH v2 1/8] minmax: Put all the clamp() definitions together Thread-Topic: [PATCH v2 1/8] minmax: Put all the clamp() definitions together Thread-Index: Adrg+PsJWBOjJVDsQ/+qaYGHKHGutQAEaRMAAANJSHAAAfkwAAACaxqgADc22WIAGyOt4A== Date: Tue, 30 Jul 2024 12:03:24 +0000 Message-ID: <424323bd65354ecbad256657d5782bc2@AcuMS.aculab.com> References: <402c3c617c29465c898b1af55e3c6095@AcuMS.aculab.com> <5cd3e11780df40b0b771da5548966ebd@AcuMS.aculab.com> In-Reply-To: Accept-Language: en-GB, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted x-originating-ip: [10.202.205.107] 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 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 5839E40028 X-Stat-Signature: ssj5gbaay5zyaxnhpx5xrpqa8iubxpac X-HE-Tag: 1722341047-534256 X-HE-Meta: U2FsdGVkX19Bnvc8c/J6uxuA53xH+OFLf+8KLQ+D4m/ldl9nlgHrE65GttNFLnvhKDIiekTw44Ggq8eD05AmV7MeonQQpAlq64hablL45Q5uFJ/TPKcE7it1EZmwKWzeaWZRFTDg7h9gtUAnHrZixjt9BgDtXzUIceJ/hErHMLmCZ6BXpECZoCXI33Ui74Y/Sfg6GvOGKscSUb3wQZi1C0vUhXNpUEe3svKK49M8a2Lhk/xnbzpFShH7v3+66H0hGJpEc+vJ21uWft7hFremkorqNhpKCMItFJQyJS4CdaSti7ll7EKxWAvxtFnc8q3fpntg/F9S2PD1Mmjj3OF41E5KXG5P+eFdO3NhZIyIgS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=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Arnd Bergmann > Sent: 29 July 2024 23:25 .. > - My macros use __builtin_choose_expr() instead of ?: to > ensure that the arguments are constant, this produces a > relatively clear compiler warning when they are not. > Without that, I would expect random drivers to start > using MIN()/MAX() in places where it's not safe. Yes, I think you either want temporaries or a constant check. Losing the signedness check is less of a problem. Since 'constantness' is only important for array sizes and static initialisers the constant check is unlikely to be a problem. Just adding __builtin_choose_expr((x)+(y),0,0) to the result is enough and quite simple. Then each argument is expanded 3 times. (cf once for temporaries without a signedness check.)) >=20 > - I went with the belts-and-suspenders version of MIN()/MAX() > that works when comparing a negative constant against > an unsigned one. This requires expanding each argument > four or five times instead of two, so you might still > want the simpler version (like MIN_T/MAX_T): I'm not sure that is worth it, there are very few negative values (except error values) in the entire kernel. And where ever the value is used a carefully created negative constant is likely to be promoted to unsigned. I'm not sure I like the casts in MIN_T() and MAX_T() either. As with min_t() someone will use too small a type. OTOH umin(x, y) could now be defined as: =09min_t(u64, (x) + 0 + 0u, (y) + 0 + 0u) (which will never sign extend). Then some of the bloating min() that are known to generate unsigned values can be changed to umin(). It is also worth noting that umin() is likely to generate better code than an _min() (that doesn't have the type check) because comparing a 32bit signed type (assumed positive) with a 64bit value won't require the (pointless) sign extension code (particularly nasty on 32bit). In my tests the compiler optimised for the high 32bits being zero (from the zero extend) - so extending to 64bits doesn't matter. The typeof((x) + 0) in the signedness test (a different email) that matches the C promotion of u8 and u16 might have been added before I did the change to allow unsigned comparisons against positive integer constants. Another anti-bloat is to do all the type tests on the temporaries. So something based on: #define ok_unsigned(_x, x) =09(is_unsigned_type((typeof(_x)) ? 1 : if_constexpr((x), (x) >=3D 0, 0)))= =20 #define min(x, y) =09__auto_type _x =3D x; =09__auto_type _y =3D y; =09if (!is_signed_type(typeof(_x + _y)) =09=09&& !(ok_unsigned(_x, x) && ok_unsigned(_y, y))) =09 error(); =09__x < _y ? _x : _y; which only has 3 expansions of each term. (Of course the 'uniq' needed for nested calls don't help.) =09David =09 - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1= PT, UK Registration No: 1397386 (Wales)