Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp469698pxa; Wed, 19 Aug 2020 06:37:29 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw1zsBmA85mbqkLQzUXB8xLSmpLfpMOqIxdKcyFaUOx7KiHT/N+KUJuRAiz7OYvk5MJvYo5 X-Received: by 2002:a17:906:2e0c:: with SMTP id n12mr24581468eji.35.1597844249569; Wed, 19 Aug 2020 06:37:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597844249; cv=none; d=google.com; s=arc-20160816; b=nml8TL8h3p6juPQ2LziGtlGeIv8NiS0SY75qRdfbxWAsg72NO0bcxy43f/Y94is6Xv 2xg7IslvjU5Bzg0um+xqCqebY+rtaZzigrHlynyaUjbTFZes8iCtij7/FF2WEMMVd30L ys4EOJZdEOYD071iL1cUkgWYuuTM4KtdjyxAHGsd7GvOVhWfciuQLJFkxol1aWWKjqaq +06dlRwrs8Mv08z/mNNY2loFOgcKOLXms5zmN855RZQH29CSNeDSYPyYQeDdg8gIQynN 2U36aSUvFKDIkIYLS+XtWq127cmpdkKeHIPexixhmWbcCJN8WAu6Id/onKfz63+Fwysq rZKQ== 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=QdYWa449W6yiB5OgKh2Jf7kHVFN6/fjn0nFZC8UXWTs=; b=uA5pBr3FqIswLTzjVpRNd9jJDm58uyFHApC8RoiO+7ezoh6plIRO5pNFGpcV6CYCL7 fv2e7+b2sFE4dGEd/Wj1HyrtQfUtGSbh8B1jThZ8BA/C/JTgPkNj2femAQwxkK+X18Fl IyzBlp4tzKBUer1lLuFoxz0SXv8unNAxaS0OaZ4V+jY951rGmsFnkWgCRbMQE9kSw79v 1lBbN+TLpvWvOO/e715mhwJ+pQ/jkQLW1nTcSbIVOSLWl1oyrFTJcYAzJlhmmJ1mtXTh X7eKzIxcDlqSw2TJBuVGtvxVlEjZaCoL11+aeOH06aRiQH4h5BVlfSdmpDfAsr6ykIcQ Z/DA== 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 w13si14715204ede.238.2020.08.19.06.37.05; Wed, 19 Aug 2020 06:37:29 -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 S1728611AbgHSNeU (ORCPT + 99 others); Wed, 19 Aug 2020 09:34:20 -0400 Received: from foss.arm.com ([217.140.110.172]:36962 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728531AbgHSNdk (ORCPT ); Wed, 19 Aug 2020 09:33:40 -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 EF6A71396; Wed, 19 Aug 2020 06:33:39 -0700 (PDT) Received: from monolith.localdoman (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id D647C3F71F; Wed, 19 Aug 2020 06:33:37 -0700 (PDT) From: Alexandru Elisei To: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Cc: mark.rutland@arm.com, maz@kernel.org, will@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 Subject: [PATCH v6 5/7] KVM: arm64: pmu: Make overflow handler NMI safe Date: Wed, 19 Aug 2020 14:34:17 +0100 Message-Id: <20200819133419.526889-6-alexandru.elisei@arm.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200819133419.526889-1-alexandru.elisei@arm.com> References: <20200819133419.526889-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: 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(-) 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.28.0