Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp3263199pxb; Mon, 1 Nov 2021 10:43:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx6I14BUGYxUXb9SdqoqiDw6Bj5ESYZiqK5/mpJZGht98iXsD9AHuuovDTxKZYu2lcQ5YOi X-Received: by 2002:a50:d885:: with SMTP id p5mr43333660edj.255.1635788626570; Mon, 01 Nov 2021 10:43:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635788626; cv=none; d=google.com; s=arc-20160816; b=rQgMcwlSL8FgHVFJgXcK/mwUdLDY5kUGgGJtoASsznF8YDVajmixzBgU9JXJv1coY6 T4c0tzxSoh+B3aMEMJgy7Lw66Xrt4GO388qiBpEx1cdVDDBe8R7uJMJ0obW5Zwzz2jvv CF0u90/G9drcog0z3ZOG6yS+BOKxKb1ZdhOZQ2gwwF2/nSZT/nIYuHo5GL7YUHul8PKa RR++nw4gISZzhQMv5SSdC1hce7I/knNMqW++XYtxj+f1concIckllCTlpDcN3ldvuTIW MTsY7VyfL3h/ySRO017xH+jIiwd31wc8+aztQLTNbg0Eck84y+msZbyj4IrZTegknWpA tGbQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=rtTqRA7823rLhhDHR/FeABtCXlD+vQwUZXFhSKbl7nw=; b=Rpm0q9yFxkDOsQltiGaONvBjUQPSIoEqM4YQoQUiCN9ubmyCDGF8zivZVY58gxEnZR a0m39b2RtfIWonXX1EZJP07vwQc3rk1Vy8mSPE0+cJs6/GsWWjnR8iYsez/JZ+4U8uzH 5Ndozzc6kE1esjB6F5TDoHqwf2r4mIQ4lz0B86LKjUasc9/90RpzAiOiMj2Dlc0RUFl1 SPNnGk1qni/88uuhXd0MoyxE7+YVGpu4k1buIYngAP1MP/y8e81abwxzuDWbn+5RKUts ih2im55l+bJXXSEXllfQixhEs1nHR1pQRtKeDXIvhFMJxAJYgjiGmJvkGF6wqlAWRjul dDww== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b="otcBvN/3"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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. [23.128.96.18]) by mx.google.com with ESMTP id p8si27022883edj.225.2021.11.01.10.43.20; Mon, 01 Nov 2021 10:43:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b="otcBvN/3"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S230206AbhKARoK (ORCPT + 99 others); Mon, 1 Nov 2021 13:44:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43744 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229834AbhKARoH (ORCPT ); Mon, 1 Nov 2021 13:44:07 -0400 Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6C309C061764 for ; Mon, 1 Nov 2021 10:41:34 -0700 (PDT) Received: by mail-pj1-x102a.google.com with SMTP id gt5so4323212pjb.1 for ; Mon, 01 Nov 2021 10:41:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=rtTqRA7823rLhhDHR/FeABtCXlD+vQwUZXFhSKbl7nw=; b=otcBvN/3IcuDrr15J0K5CvKDj9ic0jccTokbwB/sVPFst2f0LLD/XC7PPmPO9eJXRu P2OBNUe1RlpO0zOo26l/3DPAdKxfcfvQY3tyTT+VRt2u50F02VgF8OTA49ToznF2ILGt Yy6m85iKMGx1XCrOjugojlIHRrq3kXSGg8wFybO1VALqYGLus50IE17TDpBh8OqfqKK1 AAKVEsJcdEBK8202Jfb1yydMhJbNy9mJ8dChJ1pRBEEAKk0vwWevUCT/VxYAjLKOhhmx ip94OeR4ZQJbCq2qfgBZUaSsgLQoBT6PDF+7fUOqfu8QIWy5qh2RFzJV0Hmhvl6+AMgp M0Sw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=rtTqRA7823rLhhDHR/FeABtCXlD+vQwUZXFhSKbl7nw=; b=JhVGZ7lp9Fkw82v4/nu4J1VGIZXDN4FFsla4xhnuye6fqBRmsyW0SfuXqjHomGhdKm L7xhYqziDd33s1OKLjt5V+zBtpcDq0f5iReazEGKrBOSUHl3ozFj8HSiHO8gzkOpLfWv j3A+yRBZ2DGeCJqRUbCl2hz0Yr4N2isIeqx1q8ZYeMH2cNShTHfU1kEe1atTZMSwfS2O 8M/GBiNASBWmuGd+Pa+o+gk8JUHiIq3A8OuGDPKH+1lBLqWBGKwx4Jg+IrVK/JeUIBtv NLeGj1sFq8YgkxDPg3Wy3H+0LQ4YtfmsxMFsBkr16/M/GlCWEnWYMC74I0Y6Vo9xdw+O aI6Q== X-Gm-Message-State: AOAM531klcfnkS8djHVKRBPzkyBmt5ym2f2PQd9c2a82tk9Sp//Ab6qu Tx2D1Oc9XU2gsHBZ4iGyju3QWg== X-Received: by 2002:a17:90b:3ec6:: with SMTP id rm6mr365778pjb.27.1635788493615; Mon, 01 Nov 2021 10:41:33 -0700 (PDT) Received: from google.com (157.214.185.35.bc.googleusercontent.com. [35.185.214.157]) by smtp.gmail.com with ESMTPSA id cv1sm86275pjb.48.2021.11.01.10.41.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Nov 2021 10:41:33 -0700 (PDT) Date: Mon, 1 Nov 2021 17:41:29 +0000 From: Sean Christopherson To: Maxim Levitsky Cc: Marc Zyngier , Huacai Chen , Aleksandar Markovic , Paul Mackerras , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Paolo Bonzini , James Morse , Alexandru Elisei , Suzuki K Poulose , Atish Patra , David Hildenbrand , Cornelia Huck , Claudio Imbrenda , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-mips@vger.kernel.org, kvm@vger.kernel.org, kvm-ppc@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , Oliver Upton , Jing Zhang Subject: Re: [PATCH v2 26/43] KVM: VMX: Read Posted Interrupt "control" exactly once per loop iteration Message-ID: References: <20211009021236.4122790-1-seanjc@google.com> <20211009021236.4122790-27-seanjc@google.com> <20a17d75855dfb9bd496466fcd9f14baab0b2bda.camel@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20a17d75855dfb9bd496466fcd9f14baab0b2bda.camel@redhat.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Nov 01, 2021, Maxim Levitsky wrote: > On Thu, 2021-10-28 at 15:55 +0000, Sean Christopherson wrote: > > On Thu, Oct 28, 2021, Maxim Levitsky wrote: > > > On Fri, 2021-10-08 at 19:12 -0700, Sean Christopherson wrote: > > > I wish there was a way to mark fields in a struct, as requiring 'READ_ONCE' on them > > > so that compiler would complain if this isn't done, or automatically use 'READ_ONCE' > > > logic. > > > > Hmm, I think you could make an argument that ON and thus the whole "control" > > word should be volatile. AFAICT, tagging just "on" as volatile actually works. > > There's even in a clause in Documentation/process/volatile-considered-harmful.rst > > that calls this out as a (potentially) legitimate use case. > > > > - Pointers to data structures in coherent memory which might be modified > > by I/O devices can, sometimes, legitimately be volatile. > > > > That said, I think I actually prefer forcing the use of READ_ONCE. The descriptor > > requires more protections than what volatile provides, namely that all writes need > > to be atomic. So given that volatile alone isn't sufficient, I'd prefer to have > > the code itself be more self-documenting. > > I took a look at how READ_ONCE/WRITE_ONCE is implemented and indeed they use volatile > (the comment above __READ_ONCE is worth gold...), so there is a bit of contradiction: > > volatile-considered-harmful.rst states not to mark struct members volatile since > you usually need more that than (very true often) and yet, I also heard that > READ_ONCE/WRITE_ONCE is very encouraged to be used to fields that are used in lockless > algorithms, even when not strictly needed, > so why not to just mark the field and then use it normally? I guess that > explicit READ_ONCE/WRITE_ONCE is much more readable/visible that a volatile > in some header file. Are you asking about this PI field in particular, or for any field in general? In this particular case, visibility and documentation is really the only difference, functionally the result is the same. But that's also very much related to why this case gets the exception listed above. The "use it normally" part is also why I don't want to tag the field volatile since writing the field absolutely cannot be done "normally", it must be done atomically, and volatile doesn't capture that detail. If you're asking about fields in general, the "volatile is harmful" guideline is to deter usage of volatile for cases where the field/variable in question is not intrinsically volatile. As the docs call out, using volatile in those cases often leads to worse code generation because the compiler is disallowed from optimizing accesses that are protected through other mechanisms. A good example in x86 KVM is the READ_ONCE(sp->unsync) in mmu_try_to_unsync_pages() to force the compiler to emit a load of sp->unsync after acquiring mmu_unsync_pages_lock. Tagging "unsync" as volatile is unnecessary since the vast majority of its usage is protected by holding a spinlock for write, and would prevent optimizing references in kvm_mmu_get_page() and other flows that are protected by mmu_lock in the legacy MMU.