Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp887145pxb; Wed, 27 Oct 2021 14:29:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz0K0h8TDNcReAwakBw1Ee50/plCWtuvXtLqF0+EX0X19HeLX4Trmg7vk+F9Y2wytO6q90u X-Received: by 2002:a05:6402:b28:: with SMTP id bo8mr525146edb.180.1635370186884; Wed, 27 Oct 2021 14:29:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635370186; cv=none; d=google.com; s=arc-20160816; b=Gs0jNGRA88BgizPTAmCHUd1YWKYVOuLSyOtdojLig0a9FEDK17AsT57CbndjqXaVRf L8Ah4LupLyLuW9FecPCn3Kgb6nMxZWFOZlSn0AGktkjFbmUYYDBArJ3z8cStVG664T+S +g4VX4MoSb9o50S//Uo9Eorjk7HoIeodaHKO0goH4UrcLoo4yyUeHkKw1qG1Qn4mxuoU 2Cp0xiRbFn7rNFVTAKgxzkAUkEfItVoPM4rTRg1mRfWJPxfS0p617DUEngW80l6AOpaE kgxIX8a78zG/VWc+iCFPr2ZAKqcuz8vp/kN7Fwxg0W9KU+jXY5NVgFEPez6vjNCaqyBY neZg== 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=dcpH/p742iSNJ4bZTW0zt1TGCcBsOCFOjXwA1nHOArQ=; b=PlUtnmDEy3BkvkZFR1grFf8z8A8frNgp7DYaAwg5wqA9mkWSpTg7sqSd2GmtKrfJIv o4vlDiUSZoHxUPRVhYA4WrmZ+LoZkRbvMYB+wPB1+aUI8sTdTGq+FLc4ZppqDEDK01e4 NPoA2wHQB2vFewVIPlJDYt+TiXHDtPUrzSqrg7rsoU5AoadtuPAIT9X5+vRwoo37HYK0 z/erR17cxBqf5RaQUZvD+VSKyhJdBhHyNVelOTOXhWJQrdQWUVMwJOBT8UgVIYxWDVu2 Ya5KNt51WKi1+/rJsfR3erI9GWBSzW2aWPO2dCrfi5RhmxhkrbuqTdcV1VVDHUqLC1Wx KGLw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=Tm86IoMH; 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 y11si1406385edc.524.2021.10.27.14.29.24; Wed, 27 Oct 2021 14:29: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=Tm86IoMH; 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 S239004AbhJ0QHP (ORCPT + 97 others); Wed, 27 Oct 2021 12:07:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38042 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229484AbhJ0QHK (ORCPT ); Wed, 27 Oct 2021 12:07:10 -0400 Received: from mail-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0BBFAC061570 for ; Wed, 27 Oct 2021 09:04:45 -0700 (PDT) Received: by mail-pj1-x1029.google.com with SMTP id na16-20020a17090b4c1000b0019f5bb661f9so2476891pjb.0 for ; Wed, 27 Oct 2021 09:04:45 -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=dcpH/p742iSNJ4bZTW0zt1TGCcBsOCFOjXwA1nHOArQ=; b=Tm86IoMHOyj+zpaOtCSE4VFzqAdP+9ruKNcRadlOsFvChh7ill13kZR5jWD9NHE65h 64jvZDMu9kIVfatwV9NqtMoLtCasfA5hteZXUvebb1KfemQP+3PqYeGQAgGx9M3p7bU8 qNPAFnYhitn+E30o4EXNDe5pTB+zWAuxmiSCE4KEPuXZlCuroSJybkJylXO0PsvVFrn8 WHatzxWVbSvDonjRBU4GlRNXf74WN3TzY8Vqi6OJ+Ctj4jxWd/Ui1c+kFiATf0mtE0jn ZZyVyj8OcnNeewioZwWE0jsQa9pZSUdW+6oR7be9c4lXCDaENvCYDfdnIza70+cJL6Io dmAw== 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=dcpH/p742iSNJ4bZTW0zt1TGCcBsOCFOjXwA1nHOArQ=; b=IVB1utpHpmWaWaru5VGKU7ZmT5Oo+nptwYqqm9Nvif0WkzWb0f2tw/UqHXp1nXdj5/ Xksixyb0Qio21hC3NCP3v2E0TaLRQmgxkYUhTvbvXOylpGH0VJxHVhYTrqZlXTWj7wrJ oecbK20ThL9PArLf3zkq7QbbfQEVHBFESM0MIMZHRtZx9YQS6SUnPYVzrjMsrIuhcNns yhcUVcjjybwooWvWr8MjdDrUSJXN/EtsByh9wzVEKJ9Tvak2Ag+0+iNGyXLoXCR2S7hY CldmowM1u89KS28kAi32wLh/ptm+wN3RowLM+7qLOhgk1VDcXgkabtR9CXFYFmDdwwp9 gHsg== X-Gm-Message-State: AOAM530xzTaVSG5zio5YKWVprTi3izA0M8zQ0NjpedwTb7Bd4L707BcC DCcNo3/pVnB80+cVBqdMTTa0GA== X-Received: by 2002:a17:90b:1b46:: with SMTP id nv6mr6766883pjb.162.1635350684192; Wed, 27 Oct 2021 09:04:44 -0700 (PDT) Received: from google.com (157.214.185.35.bc.googleusercontent.com. [35.185.214.157]) by smtp.gmail.com with ESMTPSA id z15sm242123pga.16.2021.10.27.09.04.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Oct 2021 09:04:43 -0700 (PDT) Date: Wed, 27 Oct 2021 16:04:40 +0000 From: Sean Christopherson To: Paolo Bonzini Cc: Marc Zyngier , Huacai Chen , Aleksandar Markovic , Paul Mackerras , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , 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 39/43] KVM: VMX: Don't do full kick when triggering posted interrupt "fails" Message-ID: References: <20211009021236.4122790-1-seanjc@google.com> <20211009021236.4122790-40-seanjc@google.com> <335822ac-b98b-1eec-4911-34e4d0e99907@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <335822ac-b98b-1eec-4911-34e4d0e99907@redhat.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Oct 25, 2021, Paolo Bonzini wrote: > On 09/10/21 04:12, Sean Christopherson wrote: > > + /* > > + * The smp_wmb() in kvm_make_request() pairs with the smp_mb_*() > > + * after setting vcpu->mode in vcpu_enter_guest(), thus the vCPU > > + * is guaranteed to see the event request if triggering a posted > > + * interrupt "fails" because vcpu->mode != IN_GUEST_MODE. > > This explanation doesn't make much sense to me. This is just the usual > request/kick pattern explained in Documentation/virt/kvm/vcpu-requests.rst; > except that we don't bother with a "kick" out of guest mode because the > entry always goes through kvm_check_request (in the nVMX case) or > sync_pir_to_irr (if non-nested) and completes the delivery itself. > > In other word, it is a similar idea as patch 43/43. > > What this smp_wmb() pair with, is the smp_mb__after_atomic in > kvm_check_request(KVM_REQ_EVENT, vcpu). I don't think that's correct. There is no kvm_check_request() in the relevant path. kvm_vcpu_exit_request() uses kvm_request_pending(), which is just a READ_ONCE() without a barrier. The smp_mb__after_atomic ensures that any assets that were modified prior to making the request are seen by the vCPU handling the request. It does not provide any guarantees for a different vCPU/task making a request and checking vcpu->mode versus the target vCPU setting vcpu->mode and checking for a pending request. > Setting the interrupt in the PIR orders before kvm_make_request in this > thread, and orders after kvm_make_request in the vCPU thread. > > Here, instead: > > > + /* > > + * The implied barrier in pi_test_and_set_on() pairs with the smp_mb_*() > > + * after setting vcpu->mode in vcpu_enter_guest(), thus the vCPU is > > + * guaranteed to see PID.ON=1 and sync the PIR to IRR if triggering a > > + * posted interrupt "fails" because vcpu->mode != IN_GUEST_MODE. > > + */ > > if (vcpu != kvm_get_running_vcpu() && > > !kvm_vcpu_trigger_posted_interrupt(vcpu, false)) > > - kvm_vcpu_kick(vcpu); > > + kvm_vcpu_wake_up(vcpu); > > it pairs with the smp_mb__after_atomic in vmx_sync_pir_to_irr(). As > explained again in vcpu-requests.rst, the ON bit has the same function as > vcpu->request in the previous case. Same as above, I don't think that's correct. The smp_mb__after_atomic() ensures that there's no race between the IOMMU writing vIRR and setting ON, and KVM clearing ON and processing the vIRR. pi_test_on() is not an atomic operation, and there's no memory barrier if ON=0. It's the same behavior as kvm_check_request(), but again the ordering with respect to vcpu->mode isn't being handled by PID.ON/kvm_check_request(). AIUI, this is the barrier that's paired with the PI barriers. This is even called out in (2). vcpu->mode = IN_GUEST_MODE; srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); /* * 1) We should set ->mode before checking ->requests. Please see * the comment in kvm_vcpu_exiting_guest_mode(). * * 2) For APICv, we should set ->mode before checking PID.ON. This * pairs with the memory barrier implicit in pi_test_and_set_on * (see vmx_deliver_posted_interrupt). * * 3) This also orders the write to mode from any reads to the page * tables done while the VCPU is running. Please see the comment * in kvm_flush_remote_tlbs. */ smp_mb__after_srcu_read_unlock();