Received: by 2002:ab2:1149:0:b0:1f3:1f8c:d0c6 with SMTP id z9csp1725713lqz; Mon, 1 Apr 2024 15:36:59 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUsXONnvc+PU42iKjYmiZDAIbb/FWKdIiWSX5YmpqCxuNEZ8as2BOcFpGVTUw3qDQihxlR7AURcMmDK6UOSpYvGCu7yG0mmPhjZi4o+wQ== X-Google-Smtp-Source: AGHT+IHsagJaJBw5NPIYUTM1mmPNqjpszfS4mUQjnwypi/FXH1f6xB2zvKluB7T524aBvVtRiczo X-Received: by 2002:a05:6359:4c87:b0:183:9a25:852a with SMTP id kk7-20020a0563594c8700b001839a25852amr9418366rwc.7.1712011018686; Mon, 01 Apr 2024 15:36:58 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712011018; cv=pass; d=google.com; s=arc-20160816; b=ngXG5wKYOFy5wo2AEz5HCJwZOGx3LYyN1fcTCV5yk6c5RXqSCLY4gnxhn+fm5XAd1/ G9wSa3dDdlDtUJPWemKBmulK643vBkTJpkuA5IMXxLHgCIFNfPaRFwkYMUdSXEzgKfDq hhI4qU2kSSFljy/tziFH+PO/teobH/PUye8MlVwGRQQUu+S9qiC8o8f5UoeKTRaBEnB+ EtiSZWBzB/nNauynIAhPg6SC0/Yt1aqJSvstTzjH5OmEvMrfi/zhiIg//oMsJ0uNIiEH 6f6tpyGOrY+RCjiZdTjunpkxPs5dO1weK/1w6Q2l0LH9ZKpEoA3f4Zd2H6XyKiIqcoB9 N2fg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=WvDPAlziQ7mAcfBxPAH8ZIQ8p10MVAgkj4gxes2yMQc=; fh=U9HtF1EGc4FMKdmmHTdnyNd+KUY9kZIh2qfAWOHGP3A=; b=EWQu6T094NRRauMUgNVSzvuegHFAIxUcIteRbf7Qr6TemY2pz/GE+sU5tpSMldE2jZ WGPKzz4H6DlBJC3QDta2IqsAX8KGnIRch5jFRd1pAiTWGgzf0j5EtRHceMMojpbzNGDR xdMqYP+MNr5N5zd81eldhQ9HwWUx0/7k2qopfAt1lc3y4cKL6OqSEFpa+fc95J6adf5X s7V/t0IRtdMg4tG92L3KcD++aZZTEMqrL7E9+2v+DGmYmX+Q4u6Upalskhwh9yEd6njX rfWlC79beSXXhXDq+dRMiw0BioKkRBxUfkjYATZjk+3xFLFHJF244ahbvhrO08gabenA kjpQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@atishpatra.org header.s=google header.b=DisrqAxU; arc=pass (i=1 spf=pass spfdomain=atishpatra.org dkim=pass dkdomain=atishpatra.org); spf=pass (google.com: domain of linux-kernel+bounces-127138-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-127138-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id f32-20020a635120000000b005dbec9c2919si9872432pgb.379.2024.04.01.15.36.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Apr 2024 15:36:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-127138-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@atishpatra.org header.s=google header.b=DisrqAxU; arc=pass (i=1 spf=pass spfdomain=atishpatra.org dkim=pass dkdomain=atishpatra.org); spf=pass (google.com: domain of linux-kernel+bounces-127138-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-127138-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id B3795B217E0 for ; Mon, 1 Apr 2024 22:36:43 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 11C8456B62; Mon, 1 Apr 2024 22:36:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=atishpatra.org header.i=@atishpatra.org header.b="DisrqAxU" Received: from mail-lj1-f169.google.com (mail-lj1-f169.google.com [209.85.208.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 058E72E822 for ; Mon, 1 Apr 2024 22:36:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.169 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712010991; cv=none; b=BdHhaWL9NeUtPDKUBOgDrPugN2NPZ41MsuLsXQ9wMQNnYcunGPS7v0gHmXYB2v+v4U7pI9ZTceRon+MU6MeuyLSgKZXo5upAM9j68yuCF5egRxoiFLRGKAFAc5bXTiExykjjaPW5SzHB54I7c+CiI82F06mMbq/zQ/GHq9s7B8s= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712010991; c=relaxed/simple; bh=l7tANeGH1NplxmfJX0fGMQhsUOfslz3MessZ0Nyt/LE=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=AF0UdopYU0x9aLxlEC/4GkismABdrR0dW7oQNudC9Cm2WK9EJQ3AEnqPIi1mbGKCa2mgC+IT/1PLvxMwPSCxvzVSc7PEtUQTloOg9/GMWr4fBcGcEmgaeXhpckptleLKont4TQh1NyOM4IQPRY7Jb+I6uGjjBnrGa3Tbc6MnAuk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=atishpatra.org; spf=pass smtp.mailfrom=atishpatra.org; dkim=pass (1024-bit key) header.d=atishpatra.org header.i=@atishpatra.org header.b=DisrqAxU; arc=none smtp.client-ip=209.85.208.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=atishpatra.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=atishpatra.org Received: by mail-lj1-f169.google.com with SMTP id 38308e7fff4ca-2d48f03a8bbso47528371fa.0 for ; Mon, 01 Apr 2024 15:36:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=atishpatra.org; s=google; t=1712010987; x=1712615787; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=WvDPAlziQ7mAcfBxPAH8ZIQ8p10MVAgkj4gxes2yMQc=; b=DisrqAxUy3BCQfM4f/Zt71IkTmx7bHrq4pBldO3p7LqiZu0SRspCq420BwVxoHtfe8 7wX4LjTV1/zVeTPTgyibfcMHN2fOMpg7ytgym7ORAOPgq27uK63bYaOj5VKwj7jOdEKf l8HuUEwqz2y7SMu4muEBMDwEKKVrfN6XflxCc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712010987; x=1712615787; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=WvDPAlziQ7mAcfBxPAH8ZIQ8p10MVAgkj4gxes2yMQc=; b=AC8/AUeapbg06HyJVhBGc5ubpzVwWm8HhmAia7JcUKW0P9ugXwJh9Gwf47NVeMucSn Ez6jpv9gi82HxkYzI0oCW5ZrVeCbQokfPDwAsULA732dD/wFh9c7mjcsD5MDh8MLTBvu jvsblSgF/keCU59E1jFTiXSlYLoLt0oPqAwddsupi2XChegVmQochJiTyxzX8x4nU26f qRWCvzTQLy/M/ml7AyeUifqrOwbyeKhGBPllnKKdBqQo1hk2mrjWpeZUuhFRIsTE3D6B BoJhXQCmsK1iLT12i8QKP1VzpMXgjmVsQAjf2EdAvvdjZqQ8UGwz9GZQ4cZtZpcGWt0Y oTGg== X-Forwarded-Encrypted: i=1; AJvYcCWF9RIkWPlxKnuME4MywwnyQJXyOQlvNxdFEc3kZPBg9/PtN5Yx2de5/z6PxYe0fBJbDK2Tl8UnH8XTJLnpNyEw4OHQ4w1rJOfYlS8f X-Gm-Message-State: AOJu0YyWuyB2Ag+C7aW0P2VaTNSjeCFLR9iHAmrCr5jrohcN/xoh3BKl km9OX7dbMc7hYR6S8PpBRYHNaDjMbbCTNZli8zWQ34JcTvkFBUoLUOw46aJXb0fdqoQd9ECavMk eZMusnVasblX4Q6uzthrCqXw/UmJoMLwnRYdEd/IRSycuESfZ6A== X-Received: by 2002:a2e:b710:0:b0:2d5:9f6f:1df2 with SMTP id j16-20020a2eb710000000b002d59f6f1df2mr4822642ljo.0.1712010986601; Mon, 01 Apr 2024 15:36:26 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240229010130.1380926-1-atishp@rivosinc.com> <20240229010130.1380926-9-atishp@rivosinc.com> <20240302-6ae8fe37b90f127bc9be737f@orel> In-Reply-To: <20240302-6ae8fe37b90f127bc9be737f@orel> From: Atish Patra Date: Mon, 1 Apr 2024 15:36:14 -0700 Message-ID: Subject: Re: [PATCH v4 08/15] RISC-V: KVM: Implement SBI PMU Snapshot feature To: Andrew Jones Cc: Atish Patra , linux-kernel@vger.kernel.org, Anup Patel , Albert Ou , Alexandre Ghiti , Conor Dooley , Guo Ren , Icenowy Zheng , kvm-riscv@lists.infradead.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-riscv@lists.infradead.org, Mark Rutland , Palmer Dabbelt , Paolo Bonzini , Paul Walmsley , Shuah Khan , Will Deacon Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Sat, Mar 2, 2024 at 1:49=E2=80=AFAM Andrew Jones wrote: > > On Wed, Feb 28, 2024 at 05:01:23PM -0800, Atish Patra wrote: > > PMU Snapshot function allows to minimize the number of traps when the > > guest access configures/access the hpmcounters. If the snapshot feature > > is enabled, the hypervisor updates the shared memory with counter > > data and state of overflown counters. The guest can just read the > > shared memory instead of trap & emulate done by the hypervisor. > > > > This patch doesn't implement the counter overflow yet. > > > > Reviewed-by: Anup Patel > > Signed-off-by: Atish Patra > > --- > > arch/riscv/include/asm/kvm_vcpu_pmu.h | 7 ++ > > arch/riscv/kvm/vcpu_pmu.c | 120 +++++++++++++++++++++++++- > > arch/riscv/kvm/vcpu_sbi_pmu.c | 3 + > > drivers/perf/riscv_pmu_sbi.c | 2 +- > > 4 files changed, 129 insertions(+), 3 deletions(-) > > > > diff --git a/arch/riscv/include/asm/kvm_vcpu_pmu.h b/arch/riscv/include= /asm/kvm_vcpu_pmu.h > > index 395518a1664e..586bab84be35 100644 > > --- a/arch/riscv/include/asm/kvm_vcpu_pmu.h > > +++ b/arch/riscv/include/asm/kvm_vcpu_pmu.h > > @@ -50,6 +50,10 @@ struct kvm_pmu { > > bool init_done; > > /* Bit map of all the virtual counter used */ > > DECLARE_BITMAP(pmc_in_use, RISCV_KVM_MAX_COUNTERS); > > + /* The address of the counter snapshot area (guest physical addre= ss) */ > > + gpa_t snapshot_addr; > > + /* The actual data of the snapshot */ > > + struct riscv_pmu_snapshot_data *sdata; > > }; > > > > #define vcpu_to_pmu(vcpu) (&(vcpu)->arch.pmu_context) > > @@ -85,6 +89,9 @@ int kvm_riscv_vcpu_pmu_ctr_cfg_match(struct kvm_vcpu = *vcpu, unsigned long ctr_ba > > int kvm_riscv_vcpu_pmu_ctr_read(struct kvm_vcpu *vcpu, unsigned long c= idx, > > struct kvm_vcpu_sbi_return *retdata); > > void kvm_riscv_vcpu_pmu_init(struct kvm_vcpu *vcpu); > > +int kvm_riscv_vcpu_pmu_setup_snapshot(struct kvm_vcpu *vcpu, unsigned = long saddr_low, > > + unsigned long saddr_high, unsigned = long flags, > > + struct kvm_vcpu_sbi_return *retdata= ); > > I prefer to name this function > > kvm_riscv_vcpu_pmu_snapshot_set_shmem > Sure. > > void kvm_riscv_vcpu_pmu_deinit(struct kvm_vcpu *vcpu); > > void kvm_riscv_vcpu_pmu_reset(struct kvm_vcpu *vcpu); > > > > diff --git a/arch/riscv/kvm/vcpu_pmu.c b/arch/riscv/kvm/vcpu_pmu.c > > index 29bf4ca798cb..74865e6050a1 100644 > > --- a/arch/riscv/kvm/vcpu_pmu.c > > +++ b/arch/riscv/kvm/vcpu_pmu.c > > @@ -311,6 +311,81 @@ int kvm_riscv_vcpu_pmu_read_hpm(struct kvm_vcpu *v= cpu, unsigned int csr_num, > > return ret; > > } > > > > +static void kvm_pmu_clear_snapshot_area(struct kvm_vcpu *vcpu) > > +{ > > + struct kvm_pmu *kvpmu =3D vcpu_to_pmu(vcpu); > > + int snapshot_area_size =3D sizeof(struct riscv_pmu_snapshot_data)= ; > > + > > + if (kvpmu->sdata) { > > + memset(kvpmu->sdata, 0, snapshot_area_size); > > + if (kvpmu->snapshot_addr !=3D INVALID_GPA) > > It's a KVM bug if we have non-null sdata but snapshot_addr is INVALID_GPA= , > right? Maybe we should warn if we see that. We can also move the memset > inside the if block. > Added a warning. > > + kvm_vcpu_write_guest(vcpu, kvpmu->snapshot_addr, > > + kvpmu->sdata, snapshot_area_= size); > > + kfree(kvpmu->sdata); > > + kvpmu->sdata =3D NULL; > > + } > > + kvpmu->snapshot_addr =3D INVALID_GPA; > > +} > > + > > +int kvm_riscv_vcpu_pmu_setup_snapshot(struct kvm_vcpu *vcpu, unsigned = long saddr_low, > > + unsigned long saddr_high, unsigned = long flags, > > + struct kvm_vcpu_sbi_return *retdata= ) > > +{ > > + struct kvm_pmu *kvpmu =3D vcpu_to_pmu(vcpu); > > + int snapshot_area_size =3D sizeof(struct riscv_pmu_snapshot_data)= ; > > + int sbiret =3D 0; > > + gpa_t saddr; > > + unsigned long hva; > > + bool writable; > > + > > + if (!kvpmu) { > > + sbiret =3D SBI_ERR_INVALID_PARAM; > > + goto out; > > + } > > Need to check that flags is zero or return SBI_ERR_INVALID_PARAM. > Fixed. > > + > > + if (saddr_low =3D=3D -1 && saddr_high =3D=3D -1) { > > We introduced SBI_STA_SHMEM_DISABLE for these magic -1's for STA. Since > SBI is using the -1 approach for all its shmem, then maybe we should > rename SBI_STA_SHMEM_DISABLE to SBI_SHMEM_DISABLE and then use them here > too. > Fixed > > + kvm_pmu_clear_snapshot_area(vcpu); > > + return 0; > > + } > > + > > + saddr =3D saddr_low; > > + > > + if (saddr_high !=3D 0) { > > + if (IS_ENABLED(CONFIG_32BIT)) > > + saddr |=3D ((gpa_t)saddr << 32); > > + else > > + sbiret =3D SBI_ERR_INVALID_ADDRESS; > > + goto out; > > + } > > + > > + if (kvm_is_error_gpa(vcpu->kvm, saddr)) { > > + sbiret =3D SBI_ERR_INVALID_PARAM; > > + goto out; > > + } > > Does the check above provide anything more than what the check below does= ? > You are correct. I have removed the check > > + > > + hva =3D kvm_vcpu_gfn_to_hva_prot(vcpu, saddr >> PAGE_SHIFT, &writ= able); > > + if (kvm_is_error_hva(hva) || !writable) { > > + sbiret =3D SBI_ERR_INVALID_ADDRESS; > > + goto out; > > + } > > + > > + kvpmu->snapshot_addr =3D saddr; > > + kvpmu->sdata =3D kzalloc(snapshot_area_size, GFP_ATOMIC); > > + if (!kvpmu->sdata) > > Should reset snapshot_addr to INVALID_GPA here on error. Or maybe we > should just set snapshot_addr to saddr at the bottom of this function if > we make it. > Done. > > + return -ENOMEM; > > + > > + if (kvm_vcpu_write_guest(vcpu, saddr, kvpmu->sdata, snapshot_area= _size)) { > > + kfree(kvpmu->sdata); > > + kvpmu->snapshot_addr =3D INVALID_GPA; > > + sbiret =3D SBI_ERR_FAILURE; > > I agree we should return this SBI error for this case, but unfortunately > the spec is missing the > > SBI_ERR_FAILED - The request failed for unspecified or unknown other rea= sons. > > that we have for other SBI functions. I guess we should keep the code lik= e > this and open a PR to the spec. > I have created a blanket github issue for now. I will send a PR. > > + } > > + > > +out: > > + retdata->err_val =3D sbiret; > > + > > + return 0; > > +} > > + > > int kvm_riscv_vcpu_pmu_num_ctrs(struct kvm_vcpu *vcpu, > > struct kvm_vcpu_sbi_return *retdata) > > { > > @@ -344,20 +419,33 @@ int kvm_riscv_vcpu_pmu_ctr_start(struct kvm_vcpu = *vcpu, unsigned long ctr_base, > > int i, pmc_index, sbiret =3D 0; > > struct kvm_pmc *pmc; > > int fevent_code; > > + bool snap_flag_set =3D flags & SBI_PMU_START_FLAG_INIT_FROM_SNAPS= HOT; > > This function should confirm no undefined bits are set in flags and the > spec should specify that the reserved flags must be zero otherwise an > invalid param will be returned. > > Also here would should confirm that only one of the two flags is set, > otherwise return invalid param, as they've specified to be mutually > exclusive. > That makes sense. Update the same github issue. (https://github.com/riscv-non-isa/riscv-sbi-doc/issues/145) I will make the necessary changes in a separate series after the spec is me= rged. > Regarding the spec, the note about the counter value not being modified > unless SBI_PMU_START_SET_INIT_VALUE is set should be modified to state > unless either of the two flags are set (so I think we need another spec > PR). > > (The same flags checking/specifying comments apply to the other functions > with flags too.) > Noted (https://github.com/riscv-non-isa/riscv-sbi-doc/issues/146). > > > > if (kvm_pmu_validate_counter_mask(kvpmu, ctr_base, ctr_mask) < 0)= { > > sbiret =3D SBI_ERR_INVALID_PARAM; > > goto out; > > } > > > > + if (snap_flag_set && kvpmu->snapshot_addr =3D=3D INVALID_GPA) { > > + sbiret =3D SBI_ERR_NO_SHMEM; > > + goto out; > > + } > > + > > /* Start the counters that have been configured and requested by = the guest */ > > for_each_set_bit(i, &ctr_mask, RISCV_MAX_COUNTERS) { > > pmc_index =3D i + ctr_base; > > if (!test_bit(pmc_index, kvpmu->pmc_in_use)) > > continue; > > pmc =3D &kvpmu->pmc[pmc_index]; > > - if (flags & SBI_PMU_START_FLAG_SET_INIT_VALUE) > > + if (flags & SBI_PMU_START_FLAG_SET_INIT_VALUE) { > > pmc->counter_val =3D ival; > > + } else if (snap_flag_set) { > > + kvm_vcpu_read_guest(vcpu, kvpmu->snapshot_addr, k= vpmu->sdata, > > + sizeof(struct riscv_pmu_snaps= hot_data)); > > The snapshot read should be outside the for_each_set_bit() loop and we > should warn and abort the counter starting if the read fails. > Fixed. This should also fall under the SBI_ERR_FAILURE category. > > + /* The counter index in the snapshot are relative= to the counter base */ > > + pmc->counter_val =3D kvpmu->sdata->ctr_values[i]; > > + } > > + > > if (pmc->cinfo.type =3D=3D SBI_PMU_CTR_TYPE_FW) { > > fevent_code =3D get_event_code(pmc->event_idx); > > if (fevent_code >=3D SBI_PMU_FW_MAX) { > > @@ -398,14 +486,21 @@ int kvm_riscv_vcpu_pmu_ctr_stop(struct kvm_vcpu *= vcpu, unsigned long ctr_base, > > { > > struct kvm_pmu *kvpmu =3D vcpu_to_pmu(vcpu); > > int i, pmc_index, sbiret =3D 0; > > + u64 enabled, running; > > struct kvm_pmc *pmc; > > int fevent_code; > > + bool snap_flag_set =3D flags & SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT; > > > > - if (kvm_pmu_validate_counter_mask(kvpmu, ctr_base, ctr_mask) < 0)= { > > + if ((kvm_pmu_validate_counter_mask(kvpmu, ctr_base, ctr_mask) < 0= )) { > > Added unnecessary () here. > Fixed. > > sbiret =3D SBI_ERR_INVALID_PARAM; > > goto out; > > } > > > > + if (snap_flag_set && kvpmu->snapshot_addr =3D=3D INVALID_GPA) { > > + sbiret =3D SBI_ERR_NO_SHMEM; > > + goto out; > > + } > > + > > /* Stop the counters that have been configured and requested by t= he guest */ > > for_each_set_bit(i, &ctr_mask, RISCV_MAX_COUNTERS) { > > pmc_index =3D i + ctr_base; > > @@ -438,9 +533,28 @@ int kvm_riscv_vcpu_pmu_ctr_stop(struct kvm_vcpu *v= cpu, unsigned long ctr_base, > > } else { > > sbiret =3D SBI_ERR_INVALID_PARAM; > > } > > + > > + if (snap_flag_set && !sbiret) { > > + if (pmc->cinfo.type =3D=3D SBI_PMU_CTR_TYPE_FW) > > + pmc->counter_val =3D kvpmu->fw_event[feve= nt_code].value; > > + else if (pmc->perf_event) > > + pmc->counter_val +=3D perf_event_read_val= ue(pmc->perf_event, > > + = &enabled, &running); > > + /* TODO: Add counter overflow support when sscofp= mf support is added */ > > + kvpmu->sdata->ctr_values[i] =3D pmc->counter_val; > > + kvm_vcpu_write_guest(vcpu, kvpmu->snapshot_addr, = kvpmu->sdata, > > + sizeof(struct riscv_pmu_snap= shot_data)); > > Should just set a boolean here saying that the snapshot needs an update > and then do the update outside the for_each_set_bit loop. > Done. > > + } > > + > > if (flags & SBI_PMU_STOP_FLAG_RESET) { > > pmc->event_idx =3D SBI_PMU_EVENT_IDX_INVALID; > > clear_bit(pmc_index, kvpmu->pmc_in_use); > > + if (snap_flag_set) { > > + /* Clear the snapshot area for the upcomi= ng deletion event */ > > + kvpmu->sdata->ctr_values[i] =3D 0; > > + kvm_vcpu_write_guest(vcpu, kvpmu->snapsho= t_addr, kvpmu->sdata, > > + sizeof(struct riscv_= pmu_snapshot_data)); > > The spec isn't clear on this (so we should clarify it), but I'd expect > that a caller who set both the reset and the snapshot flag would want > the snapshot from before the reset when this call completes and then > assume that when they start counting again, and look at the snapshot > again, that those new counts would be from the reset values. Or maybe > not :-) Maybe they want to do a reset and take a snapshot in order to > look at the snapshot and confirm the reset happened? Either way, it > seems we should only do one of the two here. Either update the snapshot > before resetting, and not again after reset, or reset and then update > the snapshot (with no need to update before). > The reset call should happen when the event is deleted by the perf framework in supervisor. If we don't clear the values, the shared memory may have stale data of last read counters which is not ideal. That's why, I am clearing it upon resetting. The actual counter value should be read while stopping the counters. I thought the current description is clear enough as it says "SBI_PMU_STOP_FLAG_RESET - Reset the counter to event mapping." Do you feel we should be more explicit about this ? > > + } > > } > > } > > > > @@ -566,6 +680,7 @@ void kvm_riscv_vcpu_pmu_init(struct kvm_vcpu *vcpu) > > kvpmu->num_hw_ctrs =3D num_hw_ctrs + 1; > > kvpmu->num_fw_ctrs =3D SBI_PMU_FW_MAX; > > memset(&kvpmu->fw_event, 0, SBI_PMU_FW_MAX * sizeof(struct kvm_fw= _event)); > > + kvpmu->snapshot_addr =3D INVALID_GPA; > > > > if (kvpmu->num_hw_ctrs > RISCV_KVM_MAX_HW_CTRS) { > > pr_warn_once("Limiting the hardware counters to 32 as spe= cified by the ISA"); > > @@ -625,6 +740,7 @@ void kvm_riscv_vcpu_pmu_deinit(struct kvm_vcpu *vcp= u) > > } > > bitmap_zero(kvpmu->pmc_in_use, RISCV_MAX_COUNTERS); > > memset(&kvpmu->fw_event, 0, SBI_PMU_FW_MAX * sizeof(struct kvm_fw= _event)); > > + kvm_pmu_clear_snapshot_area(vcpu); > > } > > > > void kvm_riscv_vcpu_pmu_reset(struct kvm_vcpu *vcpu) > > diff --git a/arch/riscv/kvm/vcpu_sbi_pmu.c b/arch/riscv/kvm/vcpu_sbi_pm= u.c > > index b70179e9e875..9f61136e4bb1 100644 > > --- a/arch/riscv/kvm/vcpu_sbi_pmu.c > > +++ b/arch/riscv/kvm/vcpu_sbi_pmu.c > > @@ -64,6 +64,9 @@ static int kvm_sbi_ext_pmu_handler(struct kvm_vcpu *v= cpu, struct kvm_run *run, > > case SBI_EXT_PMU_COUNTER_FW_READ: > > ret =3D kvm_riscv_vcpu_pmu_ctr_read(vcpu, cp->a0, retdata= ); > > break; > > + case SBI_EXT_PMU_SNAPSHOT_SET_SHMEM: > > + ret =3D kvm_riscv_vcpu_pmu_setup_snapshot(vcpu, cp->a0, c= p->a1, cp->a2, retdata); > > + break; > > default: > > retdata->err_val =3D SBI_ERR_NOT_SUPPORTED; > > } > > diff --git a/drivers/perf/riscv_pmu_sbi.c b/drivers/perf/riscv_pmu_sbi.= c > > index 8de5721e8019..1a22ce1ff8c8 100644 > > --- a/drivers/perf/riscv_pmu_sbi.c > > +++ b/drivers/perf/riscv_pmu_sbi.c > > @@ -802,7 +802,7 @@ static noinline void pmu_sbi_start_ovf_ctrs_snapsho= t(struct cpu_hw_events *cpu_h > > struct riscv_pmu_snapshot_data *sdata =3D cpu_hw_evt->snapshot_ad= dr; > > > > for_each_set_bit(idx, cpu_hw_evt->used_hw_ctrs, RISCV_MAX_COUNTER= S) { > > - if (ctr_ovf_mask & (1 << idx)) { > > + if (ctr_ovf_mask & (BIT(idx))) { > > event =3D cpu_hw_evt->events[idx]; > > hwc =3D &event->hw; > > max_period =3D riscv_pmu_ctr_get_width_mask(event= ); > > -- > > 2.34.1 > > > > Thanks, > drew --=20 Regards, Atish