Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp3246234pxk; Mon, 21 Sep 2020 08:46:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzka0AKF72EPU/XSUkMEyflw/vPHPwqqkiDjWRmakldGuWZpx7n8WIqyzs3i0IfzhiOwBaM X-Received: by 2002:a05:6402:22fc:: with SMTP id dn28mr222223edb.365.1600703216333; Mon, 21 Sep 2020 08:46:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600703216; cv=none; d=google.com; s=arc-20160816; b=mcnxzq4jeO5pjsVnCNmBauHmb9C30PO6a+phapz3ROEAaPlRjFr65puiSMTjgALxmP Lr4s4+vs/Z0cocyQkRSYArob2ocygPmx2mC9QY0SXo0n03U0dwTXJ8WtPxoMZxVCg6br FdHQYutWts+On/6Vhq8/xEoOE/7WljsDa69RoMGaLfysMq1VGRS2hi+3eZweYpsQgnKC pPpwpmfSeNVeyeq7D7fOogt2q8HwxvvSLZ34PJYgAVZDF0D8shSLvFzuKB0CBbKSR7gu D82RGgjiFSCxMxpeRUXoXvehwCgPJTpwOS10CPqZ45oAlkEVORC1cY6bwJHclrNKpgcE 9KIA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-language:content-transfer-encoding :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=2LElDWtG40S3fulZ6r+7UkCEgMOR1aFczs2QhfNfbkU=; b=UAG/GaaTpgTrAAHX0EiIhM0ayxkZX1p2+/AFMCmHenHjslGxXNSb9xWKPh9yCLsw+M HRHW5HRKabrdgrIFmimp3H6ZnAf2YuYEiHWZ/RsG3hGzEseFIpNpAB6K0srSdRNChPfl vLpriw/1FvFd8ZH6LovZF6/eP5gp4w/Y8u7GoRH2+FyBOtS4gvzQjqDMzOoXohxr66/a vWC641LXunNEGpaQF7lV2zFEd6fNSS46ehDPMNfj8deLZ/b85rO7rP7MyoBavAkD8cke ERf5IVQKY6EihgaAHaN8E5WicxORylBfOaU94169jp1W3ZK6xzkaFSVoKfutylwkl2U1 Ungw== 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z3si10148774ejw.596.2020.09.21.08.46.32; Mon, 21 Sep 2020 08:46:56 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727917AbgIUPos (ORCPT + 99 others); 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