From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-11.3 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,NICE_REPLY_A, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 52052C43466 for ; Mon, 21 Sep 2020 15:44:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E127820888 for ; Mon, 21 Sep 2020 15:44:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727926AbgIUPos (ORCPT ); Mon, 21 Sep 2020 11:44:48 -0400 Received: from foss.arm.com ([217.140.110.172]:45688 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726810AbgIUPor (ORCPT ); Mon, 21 Sep 2020 11:44:47 -0400 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 A864E147A; Mon, 21 Sep 2020 08:44:46 -0700 (PDT) Received: from [192.168.0.110] (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id B01013F718; Mon, 21 Sep 2020 08:44:44 -0700 (PDT) Subject: Re: [PATCH v6 5/7] KVM: arm64: pmu: Make overflow handler NMI safe To: Will Deacon Cc: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, mark.rutland@arm.com, maz@kernel.org, catalin.marinas@arm.com, swboyd@chromium.org, sumit.garg@linaro.org, Julien Thierry , Julien Thierry , Marc Zyngier , Will Deacon , James Morse , Suzuki K Pouloze , kvm@vger.kernel.org, kvmarm@lists.cs.columbia.edu References: <20200819133419.526889-1-alexandru.elisei@arm.com> <20200819133419.526889-6-alexandru.elisei@arm.com> <20200921134301.GJ2139@willie-the-truck> From: Alexandru Elisei Message-ID: Date: Mon, 21 Sep 2020 16:45:51 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.12.0 MIME-Version: 1.0 In-Reply-To: <20200921134301.GJ2139@willie-the-truck> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Content-Language: en-US Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Will, On 9/21/20 2:43 PM, Will Deacon wrote: > On Wed, Aug 19, 2020 at 02:34:17PM +0100, Alexandru Elisei wrote: >> From: Julien Thierry >> >> kvm_vcpu_kick() is not NMI safe. When the overflow handler is called from >> NMI context, defer waking the vcpu to an irq_work queue. >> >> Cc: Julien Thierry >> Cc: Marc Zyngier >> Cc: Will Deacon >> Cc: Mark Rutland >> Cc: Catalin Marinas >> Cc: James Morse >> Cc: Suzuki K Pouloze >> Cc: kvm@vger.kernel.org >> Cc: kvmarm@lists.cs.columbia.edu >> Signed-off-by: Julien Thierry >> Signed-off-by: Alexandru Elisei >> --- >> arch/arm64/kvm/pmu-emul.c | 25 ++++++++++++++++++++++++- >> include/kvm/arm_pmu.h | 1 + >> 2 files changed, 25 insertions(+), 1 deletion(-) > I'd like an Ack from the KVM side on this one, but some minor comments > inline. > >> diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c >> index f0d0312c0a55..30268397ed06 100644 >> --- a/arch/arm64/kvm/pmu-emul.c >> +++ b/arch/arm64/kvm/pmu-emul.c >> @@ -433,6 +433,22 @@ void kvm_pmu_sync_hwstate(struct kvm_vcpu *vcpu) >> kvm_pmu_update_state(vcpu); >> } >> >> +/** >> + * When perf interrupt is an NMI, we cannot safely notify the vcpu corresponding >> + * to the event. >> + * This is why we need a callback to do it once outside of the NMI context. >> + */ >> +static void kvm_pmu_perf_overflow_notify_vcpu(struct irq_work *work) >> +{ >> + struct kvm_vcpu *vcpu; >> + struct kvm_pmu *pmu; >> + >> + pmu = container_of(work, struct kvm_pmu, overflow_work); >> + vcpu = kvm_pmc_to_vcpu(&pmu->pmc[0]); > Can you spell this kvm_pmc_to_vcpu(pmu->pmc); ? Of course, that is much better. > >> + >> + kvm_vcpu_kick(vcpu); > How do we guarantee that the vCPU is still around by the time this runs? > Sorry to ask such a horrible question, but I don't see anything associating > the workqueue with the lifetime of the vCPU. That's a very nice catch, indeed the code doesn't guarantee that the VM is still around when the work is executed. I will add an irq_work_sync() call to kvm_pmu_vcpu_destroy() (which is called by kvm_vcpu_destroy() -> kvm_arch_vcpu_destroy()), and to kvm_pmu_vcpu_reset(), similar to how x86 handles it. Thanks, Alex