Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp421067ybt; Wed, 17 Jun 2020 04:41:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzvRlXiblA/YEL5pjibFixpypEnkSb/iEGyAjXdZxHz1eeU0Ke7YercQofTxd719HjfQ32I X-Received: by 2002:a17:907:1118:: with SMTP id qu24mr6850209ejb.287.1592394073929; Wed, 17 Jun 2020 04:41:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592394073; cv=none; d=google.com; s=arc-20160816; b=Bj9rHXGRGCaZeoGm3Zv6IKeJ+QGoEpa4g2WM+eD+xt0L8rdw7wF8+1WraMsC+ncGfk /hHe6LK6nGgdGxE94KpdFBpFd7uHjNalDr8XBE5QDt5D6yVf4QxqMW3nirAds8ge7yM7 WahsvodGy4fIY41CsxONYtchNXbW+OsVtagDpDTysp6bSMa7Tw5eiw1b2hPl/t2wCCNT WUl4YcS5oOHISpyHb5gYNiaOfLblldsweoHD087ahotOiboWgaK5cLvhPsgJK4JED+sH x5i8cEO3wNy4S7xuJehDQRyGpN/EXFVyHsl4MEfdktbQcKTiUSN9rKVbBmMrv5zpiotS 1vug== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=a8V5KY4KMYQU0lCQ3+E7r+JDtSv6yWazfx78Wph7xNc=; b=O2hlhtP9oaoQ9bXlrn1Mdj1CPgbC8LEwuhvWw36aA6iljlrG1k7Cf28VLnkw6mFzQS 0KG3qE9fJ2OgDzwithHSsQnlAa8tz2k1R0pP710L2ZoxKU0tVZ57W0/kXKuAaupr+Tq0 54McH+a1XlnH5KDTbR3HV4GtRMORjcFjBsxjobiSyoE8pjB3ZpNEgjMk8z+Lpco1boSN Zhg40c6ZNtx4ybH1xJGxQpkyJ9+s2QYCM9n1v9jzitG3jOqT2r1ZcL9o8vngQ6+htB1f n2VnWfGNBlODJX5Xg0b7lcYJ6qVzw4XlBQfwYwClAcWrbIULKM7uUOFRnxjYs0Jp2v6v mOOQ== 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 m4si11404330edv.337.2020.06.17.04.40.51; Wed, 17 Jun 2020 04:41:13 -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 S1726280AbgFQLih (ORCPT + 99 others); Wed, 17 Jun 2020 07:38:37 -0400 Received: from foss.arm.com ([217.140.110.172]:56306 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726980AbgFQLid (ORCPT ); Wed, 17 Jun 2020 07:38:33 -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 8816F1045; Wed, 17 Jun 2020 04:38:32 -0700 (PDT) Received: from monolith.arm.com (unknown [10.37.8.7]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 14DF43F71F; Wed, 17 Jun 2020 04:38:29 -0700 (PDT) From: Alexandru Elisei To: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Cc: maz@kernel.org, will@kernel.org, catalin.marinas@arm.com, mark.rutland@arm.com, Julien Thierry , Julien Thierry , Marc Zyngier , James Morse , Suzuki K Pouloze , Will Deacon , kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org Subject: [PATCH v5 5/7] arm64: kvm: pmu: Make overflow handler NMI safe Date: Wed, 17 Jun 2020 12:38:49 +0100 Message-Id: <20200617113851.607706-6-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200617113851.607706-1-alexandru.elisei@arm.com> References: <20200617113851.607706-1-alexandru.elisei@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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: James Morse Cc: Suzuki K Pouloze Cc: Will Deacon Cc: Mark Rutland Cc: Catalin Marinas Cc: kvmarm@lists.cs.columbia.edu Cc: kvm@vger.kernel.org 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(-) 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]); + + kvm_vcpu_kick(vcpu); +} + /** * When the perf event overflows, set the overflow status and inform the vcpu. */ @@ -465,7 +481,11 @@ static void kvm_pmu_perf_overflow(struct perf_event *perf_event, if (kvm_pmu_overflow_status(vcpu)) { kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu); - kvm_vcpu_kick(vcpu); + + if (!in_nmi()) + kvm_vcpu_kick(vcpu); + else + irq_work_queue(&vcpu->arch.pmu.overflow_work); } cpu_pmu->pmu.start(perf_event, PERF_EF_RELOAD); @@ -764,6 +784,9 @@ static int kvm_arm_pmu_v3_init(struct kvm_vcpu *vcpu) return ret; } + init_irq_work(&vcpu->arch.pmu.overflow_work, + kvm_pmu_perf_overflow_notify_vcpu); + vcpu->arch.pmu.created = true; return 0; } diff --git a/include/kvm/arm_pmu.h b/include/kvm/arm_pmu.h index 6db030439e29..dbf4f08d42e5 100644 --- a/include/kvm/arm_pmu.h +++ b/include/kvm/arm_pmu.h @@ -27,6 +27,7 @@ struct kvm_pmu { bool ready; bool created; bool irq_level; + struct irq_work overflow_work; }; #define kvm_arm_pmu_v3_ready(v) ((v)->arch.pmu.ready) -- 2.27.0