Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp8422150ybi; Tue, 23 Jul 2019 08:16:39 -0700 (PDT) X-Google-Smtp-Source: APXvYqwE0d6faO2sK3xGUvNHTvUvYAUdi9tnhBdJ6T27tTTgLBZxDYjqnj/C39ZpIf+lketP0T5I X-Received: by 2002:a17:902:2808:: with SMTP id e8mr78163347plb.317.1563894999792; Tue, 23 Jul 2019 08:16:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563894999; cv=none; d=google.com; s=arc-20160816; b=DJeUR8kZLV672yHgztDH23kGBIpnmnXMZxubR1o0cl0yUwcbrovsZYZQWO/dtfem6h qohswslUA2T7m7bWxWj4NlVHVw5Qa3wk0d7OKEYoojp5+729mJZB9MOR9dTf0CVJ5foZ ac5+wuXh19fJIQl0DaiH33GI6DnlqdX3JtSfgYH8P8CSOiZFllK/AApZmScQLppo1xSV VCH1wesJaNaTPw5pjHJzXqAIJT+GU8Q2TeEUfvxvmw4uu6coF3Pbwxi1GJgU4v5x/PYm VfSGIlXmDeELwWtYgXjRg7m77vmN9V4aZSkrBOfSk77YkbI3O13nhAaGgqAKiPaUoICt PF5w== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=bvBDPFjxJDVYRFuBCHtsnAo08GJzSdpoC3XpuBUzF28=; b=QmJiL2yM17NXsH/PFh8DKUpKccV1SLkS5VOIL3NTcXn0JwxeOwm+cFSIvLYpYMxPAr bChjMbv7cUnbxHe8gMjEYY4pcwMl1oHqJsl2mEIdI1IlhCWoohwNiyzLgVHiRS6TaXMb 8V29MCLcaPBuTtvY5kHpCRw0kWkF0/wogWJEhxaWOY/fcyJzSPsh6aXyuBP8GIEJa7Wd HJTENNHUmLvyfOrRh/REh69d3Kp5yr9mxl8aAHX5edeMp7VYcxPylav8Z04CUB1tI5A1 4JhpzHLCnaJvoZQUFNiZ9IpkpO6dJ7lp5B1cy2L/b8rgK92PN9X59KyJLZK+diPz+b+m /vlQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d4si11230986pgc.75.2019.07.23.08.16.23; Tue, 23 Jul 2019 08:16:39 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733145AbfGWIRP (ORCPT + 99 others); Tue, 23 Jul 2019 04:17:15 -0400 Received: from foss.arm.com ([217.140.110.172]:50648 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727892AbfGWIRO (ORCPT ); Tue, 23 Jul 2019 04:17:14 -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 EF7CD344; Tue, 23 Jul 2019 01:17:13 -0700 (PDT) Received: from [10.1.197.45] (e112298-lin.cambridge.arm.com [10.1.197.45]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 8F2393F71F; Tue, 23 Jul 2019 01:17:12 -0700 (PDT) Subject: Re: [PATCH v2] KVM: arm/arm64: Introduce kvm_pmu_vcpu_init() to setup PMU counter idx To: Zenghui Yu , maz@kernel.org, kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org Cc: marc.zyngier@arm.com, james.morse@arm.com, suzuki.poulose@arm.com, julien.thierry.kdev@gmail.com, linux-kernel@vger.kernel.org, wanghaibin.wang@huawei.com, andrew.murray@arm.com References: <1563437710-30756-1-git-send-email-yuzenghui@huawei.com> From: Julien Thierry Message-ID: Date: Tue, 23 Jul 2019 09:17:09 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: <1563437710-30756-1-git-send-email-yuzenghui@huawei.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Zenghui, On 18/07/2019 09:15, Zenghui Yu wrote: > We use "pmc->idx" and the "chained" bitmap to determine if the pmc is > chained, in kvm_pmu_pmc_is_chained(). But idx might be uninitialized > (and random) when we doing this decision, through a KVM_ARM_VCPU_INIT > ioctl -> kvm_pmu_vcpu_reset(). And the test_bit() against this random > idx will potentially hit a KASAN BUG [1]. > > In general, idx is the static property of a PMU counter that is not > expected to be modified across resets, as suggested by Julien. It > looks more reasonable if we can setup the PMU counter idx for a vcpu > in its creation time. Introduce a new function - kvm_pmu_vcpu_init() > for this basic setup. Oh, and the KASAN BUG will get fixed this way. > > [1] https://www.spinics.net/lists/kvm-arm/msg36700.html > > Fixes: 80f393a23be6 ("KVM: arm/arm64: Support chained PMU counters") > Suggested-by: Andrew Murray > Suggested-by: Julien Thierry > Cc: Marc Zyngier > Signed-off-by: Zenghui Yu > --- > > Changes since v1: > - Introduce kvm_pmu_vcpu_init() in vcpu's creation time, move the > assignment of pmc->idx into it. > - Thus change the subject. The old one is "KVM: arm/arm64: Assign > pmc->idx before kvm_pmu_stop_counter()". > > Julien, I haven't collected your Acked-by into this version. If you're > still happy with the change, please Ack again. Thanks! > Thanks for making the change. This looks good to me: Acked-by: Julien Thierry Thanks, Julien > include/kvm/arm_pmu.h | 2 ++ > virt/kvm/arm/arm.c | 2 ++ > virt/kvm/arm/pmu.c | 18 +++++++++++++++--- > 3 files changed, 19 insertions(+), 3 deletions(-) > > diff --git a/include/kvm/arm_pmu.h b/include/kvm/arm_pmu.h > index 16c769a..6db0304 100644 > --- a/include/kvm/arm_pmu.h > +++ b/include/kvm/arm_pmu.h > @@ -34,6 +34,7 @@ struct kvm_pmu { > u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx); > void kvm_pmu_set_counter_value(struct kvm_vcpu *vcpu, u64 select_idx, u64 val); > u64 kvm_pmu_valid_counter_mask(struct kvm_vcpu *vcpu); > +void kvm_pmu_vcpu_init(struct kvm_vcpu *vcpu); > void kvm_pmu_vcpu_reset(struct kvm_vcpu *vcpu); > void kvm_pmu_vcpu_destroy(struct kvm_vcpu *vcpu); > void kvm_pmu_disable_counter_mask(struct kvm_vcpu *vcpu, u64 val); > @@ -71,6 +72,7 @@ static inline u64 kvm_pmu_valid_counter_mask(struct kvm_vcpu *vcpu) > { > return 0; > } > +static inline void kvm_pmu_vcpu_init(struct kvm_vcpu *vcpu) {} > static inline void kvm_pmu_vcpu_reset(struct kvm_vcpu *vcpu) {} > static inline void kvm_pmu_vcpu_destroy(struct kvm_vcpu *vcpu) {} > static inline void kvm_pmu_disable_counter_mask(struct kvm_vcpu *vcpu, u64 val) {} > diff --git a/virt/kvm/arm/arm.c b/virt/kvm/arm/arm.c > index f645c0f..c704fa6 100644 > --- a/virt/kvm/arm/arm.c > +++ b/virt/kvm/arm/arm.c > @@ -340,6 +340,8 @@ int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) > /* Set up the timer */ > kvm_timer_vcpu_init(vcpu); > > + kvm_pmu_vcpu_init(vcpu); > + > kvm_arm_reset_debug_ptr(vcpu); > > return kvm_vgic_vcpu_init(vcpu); > diff --git a/virt/kvm/arm/pmu.c b/virt/kvm/arm/pmu.c > index 3dd8238..362a018 100644 > --- a/virt/kvm/arm/pmu.c > +++ b/virt/kvm/arm/pmu.c > @@ -215,6 +215,20 @@ static void kvm_pmu_stop_counter(struct kvm_vcpu *vcpu, struct kvm_pmc *pmc) > } > > /** > + * kvm_pmu_vcpu_init - assign pmu counter idx for cpu > + * @vcpu: The vcpu pointer > + * > + */ > +void kvm_pmu_vcpu_init(struct kvm_vcpu *vcpu) > +{ > + int i; > + struct kvm_pmu *pmu = &vcpu->arch.pmu; > + > + for (i = 0; i < ARMV8_PMU_MAX_COUNTERS; i++) > + pmu->pmc[i].idx = i; > +} > + > +/** > * kvm_pmu_vcpu_reset - reset pmu state for cpu > * @vcpu: The vcpu pointer > * > @@ -224,10 +238,8 @@ void kvm_pmu_vcpu_reset(struct kvm_vcpu *vcpu) > int i; > struct kvm_pmu *pmu = &vcpu->arch.pmu; > > - for (i = 0; i < ARMV8_PMU_MAX_COUNTERS; i++) { > + for (i = 0; i < ARMV8_PMU_MAX_COUNTERS; i++) > kvm_pmu_stop_counter(vcpu, &pmu->pmc[i]); > - pmu->pmc[i].idx = i; > - } > > bitmap_zero(vcpu->arch.pmu.chained, ARMV8_PMU_MAX_COUNTER_PAIRS); > } > -- Julien Thierry