Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753434AbdLMOWU (ORCPT ); Wed, 13 Dec 2017 09:22:20 -0500 Received: from zeniv.linux.org.uk ([195.92.253.2]:60428 "EHLO ZenIV.linux.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752936AbdLMOWQ (ORCPT ); Wed, 13 Dec 2017 09:22:16 -0500 Date: Wed, 13 Dec 2017 14:22:12 +0000 From: Al Viro To: Jakub Kicinski Cc: Linus Torvalds , netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [RFC][PATCH] new byteorder primitives - ..._{replace,get}_bits() Message-ID: <20171213142212.GD21978@ZenIV.linux.org.uk> References: <20171212194532.GA7062@ZenIV.linux.org.uk> <20171212120409.64b6362e@cakuba.netronome.com> <20171212234856.GZ21978@ZenIV.linux.org.uk> <20171212155933.03c88eab@cakuba.netronome.com> <20171213003659.GA21978@ZenIV.linux.org.uk> <20171212170437.4b129e50@cakuba.netronome.com> <20171213013056.GB21978@ZenIV.linux.org.uk> <20171212173528.340cd002@cakuba.netronome.com> <20171213015125.GC21978@ZenIV.linux.org.uk> <20171212184400.13b27cf8@cakuba.netronome.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20171212184400.13b27cf8@cakuba.netronome.com> User-Agent: Mutt/1.9.0 (2017-09-02) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2792 Lines: 63 On Tue, Dec 12, 2017 at 06:44:00PM -0800, Jakub Kicinski wrote: > On Wed, 13 Dec 2017 01:51:25 +0000, Al Viro wrote: > > On Tue, Dec 12, 2017 at 05:35:28PM -0800, Jakub Kicinski wrote: > > > > > It used to be __always_inline, but apparently LLVM/clang doesn't > > > propagate constants :( > > > > > > 4e59532541c8 ("nfp: don't depend on compiler constant propagation") > > > > Doesn't propagate constants or doesn't have exact same set of > > rules for __builtin_constant_p()? IOW, if you dropped that > > BUILD_BUG_ON(), what would be left after optimizations? > > Hm. You're right. It just doesn't recognize the parameter as constant > in __builtin_constant_p(). FWIW, clang does propagate them well enough to detect and optimize multiplication by constant power of two. With the variant I've posted. The check on field overflow (which works on gcc builds) does nothing on clang - __builtin_constant_p() gives a flat-out false for arguments of inline function there. I can understand their reasoning, even though it's inconvenient in cases like this one - semantics of __builtin_constant_p() is a fucking mess and the less you rely upon its details, the safer you are... Hell knows - a part of that can be recovered by taking the check into a wrapper; as in static __always_inline __le32_replace_bits(__le32 old, u32 v, u32 mask) { __le32 m = cpu_to_le32(mask); return (old & ~m) | cpu_to_le32(v * mask/mask_to_multiplier(mask))); } #define le32_replace_bits(old, v, mask) ({ \ typeof(v) ____v = (v); \ typeof(mask) ____m = (mask); \ if (__builtin_constant_p(____v)) \ if (v & ~(____m / mask_to_multiplier(____m))) \ __field_overflow(); \ __l32_replace_bits(old, ____v, ____m); \ }) That would give that sanity check a better coverage on clang builds, but... does it really buy us enough to bother, especially since all those macros would have to be spelled out; you can have a macro expanding to definition of static inline, but you can't have a macro expanding to anything that would contain a preprocessor directive, including #define. And with that kind of sanity checks, the first build with gcc will catch everything missed by clang builds anyway. IMO it's not worth the trouble; let's go with the check inside of static inline and accept that on clang builds it'll do nothing. Next question: where do we put that bunch? I've put it into linux/byteorder/generic.h, so that anything picking fixed-endian primitives would pick those as well; I hadn't thought of linux/bitfield.h at the time. We certainly could put it there instead - it's never pulled by other headers, so adding #include into linux/bitfield.h is not going to cause header order problems. Not sure... Linus, do you have any preferences in that area?