Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp191662yba; Fri, 5 Apr 2019 04:55:46 -0700 (PDT) X-Google-Smtp-Source: APXvYqxf4JpWX3Sw1F17GNNc05gNhoGMaNEX/c8RZD7/0fj+nV3hpQxceZDcE3aA8rVonkaxbm+c X-Received: by 2002:a63:3dc8:: with SMTP id k191mr11578098pga.286.1554465346911; Fri, 05 Apr 2019 04:55:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554465346; cv=none; d=google.com; s=arc-20160816; b=vEefCyhw/VDrHUGpu7zxK5Y9alUzY2G5n72NLwxZ+NefzpvmwTqceUa0UOdeFaCWCr 33wn3sUtjhWIvcu+IE8rq4DFP2jm4j3eM7oz0EqRR+qttguQYJsQpwY5ItzC8Bw6vaoC mYpvZRpPsDEKN981xntuh4Tm5FPQdPy1lcrBIhQqS3jMOv/Q69qxTorE8YwwYubI4DEj Irq6FM4DeAGrGSg38i6I+fB3L/ekftPGsAPaSZ+LGWIf0vLMcY0Gf5xt+9JObK//iJzo hvS6yd1IeIAO4AMRiAovPYdvLWGbFDxfSdsJ7NfZUwTDWUDuQRMFrad4QYZXj3Ce1xwQ B5xg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=9u9Fu4LfYkIEsmgAmArLp0TPaKFcct1JYuONU35iZzs=; b=fDCWURXV0kWULgfrhWjND0oHNNWKhyecSRplcZwN9QHPo9xizbn892SJjcooDHzWjZ BdU774H3q4+TOsVv7MXdnuLr+Zo8UWBIZwBrbgDsRC6cZ3KWZvMpbtz6ksFaCl01uHGZ rZGS5u2vZl1SpkRFrlYMSZt+4aGPC7Eck+YrUzcSe/bOJBgwB2iWG4JToHX4p0bSNg3B bdm6/SakRS4WQo722jknuesu1oLqjW/MzyVO+0ToQrA3maYNgFv032QRDWa1pZXqipU2 Ujux1f2yXD1GnMQaM/yS6sXjTnvi8KyfkNpQG/8u5R2V7DrjT8x3fjGzo34GfwegEgZG Cvnw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=SgbfYDAY; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i37si18687858pgb.436.2019.04.05.04.55.31; Fri, 05 Apr 2019 04:55:46 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=SgbfYDAY; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730255AbfDELxm (ORCPT + 99 others); Fri, 5 Apr 2019 07:53:42 -0400 Received: from mail-vs1-f66.google.com ([209.85.217.66]:45333 "EHLO mail-vs1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726513AbfDELxm (ORCPT ); Fri, 5 Apr 2019 07:53:42 -0400 Received: by mail-vs1-f66.google.com with SMTP id o10so710542vsp.12 for ; Fri, 05 Apr 2019 04:53:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=9u9Fu4LfYkIEsmgAmArLp0TPaKFcct1JYuONU35iZzs=; b=SgbfYDAYF1CHLhWRdBSBXUFTKQr3/4Gi5OzrNNSRzkVP0z2sM9W5GnXKWQMuEdmebY w+3xkWzSpUS0bmQQZloUdmm2bkmuRjU9RMpAogh5E0+m+CkQp2678rGNCxwKkHwlb77f 5jVYWDer8Lv4sXZpcOJ1v8yJIdt9//gBFU2iiC/QjPz929n3trHmlS9StfTOvHRgDx0p SSlP0d8xuwFbvDa9e+68ycHsMnyoRxvsTYEjRM/4szCf3RHMAu4vp86fzGdpHYXv5AnD 7EUfl6d9ncNlU8/hbFV2xVqX9Io/HQ6nHcFyjxORucpUwQxP6M2xDcP653b8pWYRNALB q43g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=9u9Fu4LfYkIEsmgAmArLp0TPaKFcct1JYuONU35iZzs=; b=FjG2TjCGMlQn4aF2b4SpYeaYqgr63IuPWWv7tYqDqr6Kp2ZPRrRUUPwc026P5prMyf ZxRPHq8p0qLRcG9RkIuQZnH09hVJudMxNgfHd+a0W+RAUMyng8yEBOR+egw9OWzDWdIk gyca3NnQFvPhTY94cU6bpsL+sYgui2xBBS9xAFcerpcz1i4AOTs4Wg6LIRaEuopSVmka faidlBkZ40fD4bB9NcA2Tv1fl9A4w6E+dZySD99+E83JZ/Bpcy8o6v0J+Lg+CWLpWtVX hP0BgAnvXx6LE+anNfk05Nk96L3cACvKYhMzE2tNPyR5ZjGg39uE5XhmtGlUBRkDtdqj +rNg== X-Gm-Message-State: APjAAAWJy1g1RDXN8A/+dU8rXBmngB+1SR/nLUWpq5NdNt6G/fjGfmS1 vJ7cazYFJP/MkFw5qy65d14eIs0slPnRGaatm+D+ng== X-Received: by 2002:a67:870a:: with SMTP id j10mr7369756vsd.161.1554465220986; Fri, 05 Apr 2019 04:53:40 -0700 (PDT) MIME-Version: 1.0 References: <20190402112813.193378-1-glider@google.com> <20190405093931.GA28890@gmail.com> In-Reply-To: <20190405093931.GA28890@gmail.com> From: Alexander Potapenko Date: Fri, 5 Apr 2019 13:53:29 +0200 Message-ID: Subject: Re: [PATCH v2] x86/asm: fix assembly constraints in bitops To: Ingo Molnar Cc: Paul McKenney , "H. Peter Anvin" , Peter Zijlstra , LKML , Dmitriy Vyukov , James Y Knight , x86@kernel.org, Ingo Molnar , Peter Zijlstra , Linus Torvalds , Borislav Petkov Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Apr 5, 2019 at 11:39 AM Ingo Molnar wrote: > > > * Alexander Potapenko wrote: > > > 1. Use memory clobber in bitops that touch arbitrary memory > > > > Certain bit operations that read/write bits take a base pointer and an > > arbitrarily large offset to address the bit relative to that base. > > Inline assembly constraints aren't expressive enough to tell the > > compiler that the assembly directive is going to touch a specific memor= y > > location of unknown size, therefore we have to use the "memory" clobber > > to indicate that the assembly is going to access memory locations other > > than those listed in the inputs/outputs. > > To indicate that BTR/BTS instructions don't necessarily touch the first > > sizeof(long) bytes of the argument, we also move the address to assembl= y > > inputs. > > > > This particular change leads to size increase of 124 kernel functions i= n > > a defconfig build. For some of them the diff is in NOP operations, othe= r > > end up re-reading values from memory and may potentially slow down the > > execution. But without these clobbers the compiler is free to cache > > the contents of the bitmaps and use them as if they weren't changed by > > the inline assembly. > > > > 2. Use byte-sized arguments for operations touching single bytes. > > > > Passing a long value to ANDB/ORB/XORB instructions makes the compiler > > treat sizeof(long) bytes as being clobbered, which isn't the case. This > > may theoretically lead to worse code in the case of heavy optimization. > > > > Signed-off-by: Alexander Potapenko > > Cc: Dmitry Vyukov > > Cc: Paul E. McKenney > > Cc: H. Peter Anvin > > Cc: Peter Zijlstra > > Cc: James Y Knight > > --- > > v2: > > -- renamed the patch > > -- addressed comment by Peter Zijlstra: don't use "+m" for functions > > returning void > > -- fixed input types for operations touching single bytes > > --- > > arch/x86/include/asm/bitops.h | 41 +++++++++++++++-------------------- > > 1 file changed, 18 insertions(+), 23 deletions(-) > > I'm wondering what the primary motivation for the patch is: > > - Does it fix an actual miscompilation, or only a theoretical miscompila= tion? Depends on what we name an actual miscompilation. I've built a defconfig kernel and looked through some of the functions generated by GCC 7.3.0 with and without this clobber, and didn't spot any miscompilations. However there is a (trivial) theoretical case where this code leads to miscompilation: https://lkml.org/lkml/2019/3/28/393 using just GCC 8.3.0 with -O2. It isn't hard to imagine someone writes such a function in the kernel somed= ay. So the primary motivation is to fix an existing misuse of the asm directive, which happens to work in certain configurations now, but isn't guaranteed to work under different circumstances. > - If it fixes an existing miscompilation: > > - Does it fix a miscompilation triggered by current/future versions of= GCC? > - Does it fix a miscompilation triggered by current/future versions of= Clang? > > - Also, is the miscompilation triggered by 'usual' kernel configs, or > does it require exotics such as weird debug options or GCC plugins, > etc? > > I.e. a bit more context would be useful. > > Thanks, > > Ingo --=20 Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Stra=C3=9Fe, 33 80636 M=C3=BCnchen Gesch=C3=A4ftsf=C3=BChrer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg