Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp1288067pxb; Fri, 21 Jan 2022 14:26:23 -0800 (PST) X-Google-Smtp-Source: ABdhPJyimV7e14ZfzY+Ql/WB/NpnzabnrhLoumS5vbtbxQMLzZe7ECXdBFv3PT7Z/cAm4/cmj+I3 X-Received: by 2002:a05:6a00:1988:b0:4c3:b9cd:f09a with SMTP id d8-20020a056a00198800b004c3b9cdf09amr5597283pfl.2.1642803982885; Fri, 21 Jan 2022 14:26:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1642803982; cv=none; d=google.com; s=arc-20160816; b=Jnr7Mgkc6bP5/mVL0KSSKoHLl9cRu6uJhgkVCO0/8nG3Zal7BurgV5EBEQQ5WDUhZ2 wisi5sFdzZwB99SS9TQwDF84lMJglgRX1jL1inwSjey7nRf/xGC0zHdbmUiccgn56Ik2 fZAn03FPIarSxqU46GNJsv8uCocsOp0fjm8WgCV4tN8WhhjwBTmncXfjBGFR1Vl/dLPt GAK3DIR4QlF6aZr8owHFYJKbLDhY2qIwFBjMXHYyAWL4ZGFmrF1fVunF4GjHISbo0B2c W9sdczl6mxTGf3mILEUAy2Z4wqFcJwMn875mvBE/TQUq5hc+56ITzrTsdl1XtV62BPgd 7k3A== 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; bh=a/Fx3R7myKZc6dmGpW6tD0VY/nDI+I2pXUjzfFmpKPE=; b=HGiC+oznLT9cEffrBrns88D5cefdCYKvl/10dm7dU0yqhkDWtWe6Jop/t7Wo6lelFD i8QjCM8eAkb3G67uAHdX5sv3vlSNgSTGTlH6AfSx/vlT05dKRQZmneU4otkhiibrNXZl MctLIHaDxw27BamJQ0TnF5HAWV4aqQp4Fyc1phnTFwMc7MmWXIFxk19lPAoU46+fYiK5 6wBfSgW22j5C36WZTsQD+0SabAXzx8ioMUty46fDOZNvyqQfs+uC83Ew3FRzyUhCFOrb 2Nedxe1NqDp9y67XKTHEe508plHQBuilL1QM8dCaWIytI3B3dFBfRvRXnxNtVw6vQ5Ub 1wxw== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d15si7218797pjd.35.2022.01.21.14.26.10; Fri, 21 Jan 2022 14:26:22 -0800 (PST) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377066AbiATQpK (ORCPT + 99 others); Thu, 20 Jan 2022 11:45:10 -0500 Received: from foss.arm.com ([217.140.110.172]:44806 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234766AbiATQpJ (ORCPT ); Thu, 20 Jan 2022 11:45:09 -0500 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 79F19ED1; Thu, 20 Jan 2022 08:45:08 -0800 (PST) Received: from C02TD0UTHF1T.local (unknown [10.57.38.163]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 80FBF3F73D; Thu, 20 Jan 2022 08:45:02 -0800 (PST) Date: Thu, 20 Jan 2022 16:44:55 +0000 From: Mark Rutland To: linux-kernel@vger.kernel.org Cc: aleksandar.qemu.devel@gmail.com, alexandru.elisei@arm.com, anup.patel@wdc.com, aou@eecs.berkeley.edu, atish.patra@wdc.com, borntraeger@linux.ibm.com, bp@alien8.de, catalin.marinas@arm.com, chenhuacai@kernel.org, dave.hansen@linux.intel.com, frankja@linux.ibm.com, frederic@kernel.org, gor@linux.ibm.com, hca@linux.ibm.com, james.morse@arm.com, jmattson@google.com, joro@8bytes.org, luto@kernel.org, maz@kernel.org, mingo@redhat.com, mpe@ellerman.id.au, nsaenzju@redhat.com, palmer@dabbelt.com, paulmck@kernel.org, paul.walmsley@sifive.com, pbonzini@redhat.com, peterz@infradead.org, seanjc@google.com, suzuki.poulose@arm.com, svens@linux.ibm.com, tglx@linutronix.de, tsbogend@alpha.franken.de, vkuznets@redhat.com, wanpengli@tencent.com, will@kernel.org Subject: Re: [PATCH v2 4/7] kvm/mips: rework guest entry logic Message-ID: <20220120164455.GA15464@C02TD0UTHF1T.local> References: <20220119105854.3160683-1-mark.rutland@arm.com> <20220119105854.3160683-5-mark.rutland@arm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220119105854.3160683-5-mark.rutland@arm.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jan 19, 2022 at 10:58:51AM +0000, Mark Rutland wrote: > In kvm_arch_vcpu_ioctl_run() we use guest_enter_irqoff() and > guest_exit_irqoff() directly, with interrupts masked between these. As > we don't handle any timer ticks during this window, we will not account > time spent within the guest as guest time, which is unfortunate. > > Additionally, we do not inform lockdep or tracing that interrupts will > be enabled during guest execution, which caan lead to misleading traces > and warnings that interrupts have been enabled for overly-long periods. > > This patch fixes these issues by using the new timing and context > entry/exit helpers to ensure that interrupts are handled during guest > vtime but with RCU watching, with a sequence: > > guest_timing_enter_irqoff(); > > guest_state_enter_irqoff(); > < run the vcpu > > guest_state_exit_irqoff(); > > < take any pending IRQs > > > guest_timing_exit_irqoff(); Looking again, this patch isn't sufficient. On MIPS a guest exit will be handled by kvm_mips_handle_exit() *before* returning into the "< run the vcpu >" step above, so we won't call guest_state_exit_irqoff() before using RCU, etc. This'll need some more thought... Mark. > Since instrumentation may make use of RCU, we must also ensure that no > instrumented code is run during the EQS. I've split out the critical > section into a new kvm_mips_enter_exit_vcpu() helper which is marked > noinstr. > > Signed-off-by: Mark Rutland > Cc: Aleksandar Markovic > Cc: Frederic Weisbecker > Cc: Huacai Chen > Cc: Paolo Bonzini > Cc: Paul E. McKenney > Cc: Thomas Bogendoerfer > --- > arch/mips/kvm/mips.c | 37 ++++++++++++++++++++++++++++++++++--- > 1 file changed, 34 insertions(+), 3 deletions(-) > > diff --git a/arch/mips/kvm/mips.c b/arch/mips/kvm/mips.c > index aa20d074d3883..1a961c2434fee 100644 > --- a/arch/mips/kvm/mips.c > +++ b/arch/mips/kvm/mips.c > @@ -438,6 +438,24 @@ int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, > return -ENOIOCTLCMD; > } > > +/* > + * Actually run the vCPU, entering an RCU extended quiescent state (EQS) while > + * the vCPU is running. > + * > + * This must be noinstr as instrumentation may make use of RCU, and this is not > + * safe during the EQS. > + */ > +static int noinstr kvm_mips_vcpu_enter_exit(struct kvm_vcpu *vcpu) > +{ > + int ret; > + > + guest_state_enter_irqoff(); > + ret = kvm_mips_callbacks->vcpu_run(vcpu); > + guest_state_exit_irqoff(); > + > + return ret; > +} > + > int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) > { > int r = -EINTR; > @@ -458,7 +476,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) > lose_fpu(1); > > local_irq_disable(); > - guest_enter_irqoff(); > + guest_timing_enter_irqoff(); > trace_kvm_enter(vcpu); > > /* > @@ -469,10 +487,23 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) > */ > smp_store_mb(vcpu->mode, IN_GUEST_MODE); > > - r = kvm_mips_callbacks->vcpu_run(vcpu); > + r = kvm_mips_vcpu_enter_exit(vcpu); > + > + /* > + * We must ensure that any pending interrupts are taken before > + * we exit guest timing so that timer ticks are accounted as > + * guest time. Transiently unmask interrupts so that any > + * pending interrupts are taken. > + * > + * TODO: is there a barrier which ensures that pending interrupts are > + * recognised? Currently this just hopes that the CPU takes any pending > + * interrupts between the enable and disable. > + */ > + local_irq_enable(); > + local_irq_disable(); > > trace_kvm_out(vcpu); > - guest_exit_irqoff(); > + guest_timing_exit_irqoff(); > local_irq_enable(); > > out: > -- > 2.30.2 > >