From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753266AbdKMW7z (ORCPT ); Mon, 13 Nov 2017 17:59:55 -0500 Received: from mail-wm0-f42.google.com ([74.125.82.42]:56423 "EHLO mail-wm0-f42.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753157AbdKMW7r (ORCPT ); Mon, 13 Nov 2017 17:59:47 -0500 X-Google-Smtp-Source: AGs4zMY05jp0ZCj3zmWBAmHdDqM30WV+U4w+QrMxFguSyufix7hBZH66pe5eftjr2kUP2X1GsvS2wQ== From: Rasmus Villemoes To: Linus Torvalds Cc: Patrick McLean , Al Viro , Bruce Fields , "Darrick J. Wong" , Linux Kernel Mailing List , Linux NFS Mailing List , stable , Thorsten Leemhuis Subject: bit tweaks [was: Re: [nfsd4] potentially hardware breaking regression in 4.14-rc and 4.13.11] Organization: D03 References: X-Hashcash: 1:20:171113:chutzpah@gentoo.org::hGLpFk+GziBjVOi2:0000000000000000000000000000000000000000001ykp X-Hashcash: 1:20:171113:linux-kernel@vger.kernel.org::hlyKLuVwGqApIHOE:0000000000000000000000000000000002a2C X-Hashcash: 1:20:171113:darrick.wong@oracle.com::gc+BT2cFGL2DOQ3q:000000000000000000000000000000000000002/6e X-Hashcash: 1:20:171113:stable@vger.kernel.org::tDU1AB5vUdKn4OHW:0000000000000000000000000000000000000003T19 X-Hashcash: 1:20:171113:linux-nfs@vger.kernel.org::uE8BYeJl/qHM1xW/:0000000000000000000000000000000000003gSO X-Hashcash: 1:20:171113:regressions@leemhuis.info::NLjZeFGoXQW9/dTY:00000000000000000000000000000000000042CP X-Hashcash: 1:20:171113:bfields@redhat.com::12h2PD1BwuZsGfin:00000000000000000000000000000000000000000009lCt X-Hashcash: 1:20:171113:torvalds@linux-foundation.org::tBnOQJarPllKwkYO:000000000000000000000000000000008Vq/ X-Hashcash: 1:20:171113:viro@zeniv.linux.org.uk::4UZ/AEawzTrln4gv:00000000000000000000000000000000000000BN/m Date: Mon, 13 Nov 2017 23:59:43 +0100 In-Reply-To: (Linus Torvalds's message of "Wed, 8 Nov 2017 18:40:22 -0800") Message-ID: <87efp1zsww.fsf_-_@rasmusvillemoes.dk> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Nov 09 2017, Linus Torvalds wrote: > The code disassembles to > > 0: 83 c9 08 or $0x8,%ecx > 3: 40 f6 c6 04 test $0x4,%sil > 7: 0f 45 d1 cmovne %ecx,%edx > a: 89 d1 mov %edx,%ecx > c: 80 cd 04 or $0x4,%ch > f: 40 f6 c6 08 test $0x8,%sil > 13: 0f 45 d1 cmovne %ecx,%edx > 16: 89 d1 mov %edx,%ecx > 18: 80 cd 08 or $0x8,%ch > 1b: 40 f6 c6 10 test $0x10,%sil > 1f: 0f 45 d1 cmovne %ecx,%edx > 22: 89 d1 mov %edx,%ecx > 24: 80 cd 10 or $0x10,%ch > 27: 83 e6 20 and $0x20,%esi > 2a:* 48 8b b7 30 02 00 00 mov 0x230(%rdi),%rsi <-- trapping instruction > 31: 0f 45 d1 cmovne %ecx,%edx > 34: 83 ca 20 or $0x20,%edx > 37: 89 f1 mov %esi,%ecx > 39: 83 e1 10 and $0x10,%ecx > 3c: 89 cf mov %ecx,%edi > > and all those odd cmovne and bit-ops are just the bit selection code > in flags_by_mnt(), which is inlined through calculate_f_flags (which > is _also_ inlined) into vfs_statfs(). > > Sadly, gcc makes a mess of it and actually generates code that looks > like the original C. I would have hoped that gcc could have turned > > if (x & BIT) > y |= OTHER_BIT; > > into > > y |= (x & BIT) shifted-by-the-bit-difference-between BIT/OTHER_BIT; > > but that doesn't happen. Actually, new enough gcc (7.1, I think) does contain a pattern that does this, but unfortunately only if one spells it y |= (x & BIT) ? OTHER_BIT : 0; which is half-way to doing it by hand, I suppose. Doing the - if (mnt_flags & MNT_READONLY) - flags |= ST_RDONLY; + flags |= (mnt_flags & MNT_READONLY) ? ST_RDONLY : 0; and pasting into godbolt.org, one can apparently get gcc to compile it to flags_by_mnt(int): leal (%rdi,%rdi), %edx movl %edi, %eax sarl $6, %eax movl %edx, %ecx andl $1, %eax andl $12, %edx andl $2, %ecx orl %ecx, %eax orl %eax, %edx movl %edi, %eax sall $7, %eax andl $7168, %eax orl %edx, %eax ret Rasmus