Received: by 2002:ab2:3350:0:b0:1f4:6588:b3a7 with SMTP id o16csp1681330lqe; Mon, 8 Apr 2024 17:33:26 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVhfqVIn7/iYsjmHp77Fxa3WJxZ9ssZU/sHHpA3L+3xvJjibdiHnIFaeGPPLtnGR545OetBWdnAYZiAOc5RUApIlj4BzXlCUdcYFrgXkA== X-Google-Smtp-Source: AGHT+IECHBosD9TkmbpFZxDJZyMXI/xgQIOJl3zh2PIDVjZ9FoQOb3ajrNKBkkX4yMcwxhCfrT8K X-Received: by 2002:a17:902:ce8e:b0:1e4:7cc5:2292 with SMTP id f14-20020a170902ce8e00b001e47cc52292mr2477028plg.49.1712622806508; Mon, 08 Apr 2024 17:33:26 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712622806; cv=pass; d=google.com; s=arc-20160816; b=uukp6hJafakHDeD8vJm48MmLR2vxU3PKSIkGI1xCDG7qRxKy6ZZsNALky5rSOKupn2 NgcsY7Rk1v5IQYU2TT0Fv3JXnMJPc9NmnQKIBb3fPQ35g3bQeS0/QuLyGSi1LxHOwhvS 44gHnZ5FukVkUR4fFJ8ttsFNvifFTTkk3DXa/+OCO0OTO7WK/P5SZicv4N8zXh5NCiTo 47JEDreQqLp9l2hG5a2G+yVXteG5Vdj/oN3S104gKIgp3h9+7c9IiMLIR1wxCGkO1PHA Ul2ANm9Kd6EeUctECpd7jC5ge5MuOKE2gM7SYW2JjOFAVk7qMjtSeoNjj0DbvLcImdyq uzCw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=f/CJmT3YaooE8CB2QIvUgCnYPke09ytqabQq9mjiodg=; fh=7UGrb3v1rJK2pHiZ+fx5JbbeetHQ6CcJ/jeNkqtgDVI=; b=bPeLof++Rl29gQ2YUMAxGkVVvTF5Vc66NT45vi/+007Uly4hGNyOTfUADP1r6sNaaS bDXGd8Ym6bNWUGV0Gh9Bc7mOjzA5b18dtXWx1Yg/vByAIZ9Oi9PAl4m/bhlPkp8jgeCQ DHJzWzTnVgT/ODt6nU1GPOfDx/5GdHiA4+q74MfWjfvP2sqzz6Viud+OcIulxiC3uZ2X KvuKpfhJdQAM2vu0fVEFMbDRzq2PdmSeVD7yVcXS8NKbusyiEwNWqZT1+TvlmnPqoCgy piZFDRvMsOYp4sAnzs7Jr1JaYQBTjCWrSOkDngZo8fu9eMV+kqIWAQQn5OiGY46q4UL5 2cFQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=xxjMqRFJ; arc=pass (i=1 spf=pass spfdomain=rivosinc.com dkim=pass dkdomain=rivosinc-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-136047-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-136047-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id c3-20020a170903234300b001e0b5f9fb06si7388927plh.13.2024.04.08.17.33.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Apr 2024 17:33:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-136047-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=xxjMqRFJ; arc=pass (i=1 spf=pass spfdomain=rivosinc.com dkim=pass dkdomain=rivosinc-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-136047-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-136047-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 1B0CC284BD4 for ; Tue, 9 Apr 2024 00:33:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D909163BF; Tue, 9 Apr 2024 00:33:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b="xxjMqRFJ" Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) (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 5F86D10F2 for ; Tue, 9 Apr 2024 00:33:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.178 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712622795; cv=none; b=lmDKp40HJUJddHYTPzVU3GPHSGyl5SfcT9CVhdxIE2PEpG761oa4smmCHoZ7A+V9MLGZHt9Sy+Cvi4OkDFrAIBwQdWsdcmsQpJ67DZ7NjRCD/3j++GwD519hRecrx1RYMc/7BwiFHso0EtH6YAcika9J6D9DEuWc9sTdB4yK8XY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712622795; c=relaxed/simple; bh=DJ/hQ2jxsHQO4QqjkfXY3cUKzfZdwjLfN1fz+yoHqxE=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=Rwiwo352xy0Oa8RKb9In0c1vo5vR1Mj42YjNEx6JsIULaJP7EBVHF+2DCfbRjVd4Rj9qh8bF51Fb2aCUpi5+MzDOa8uknI4goVnj7GhCtiRHkNn9p7kE4s+j281PIngHzwFH1lqAG7BfC9HTtcZVFnmqpSIAMU+gnK+OLdUnEtI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b=xxjMqRFJ; arc=none smtp.client-ip=209.85.214.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rivosinc.com Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-1e4149e7695so12194765ad.0 for ; Mon, 08 Apr 2024 17:33:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1712622793; x=1713227593; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=f/CJmT3YaooE8CB2QIvUgCnYPke09ytqabQq9mjiodg=; b=xxjMqRFJX1wc7+u49+U3+8x4Pqk2B9OVV5fbFWwUO0aym8a7lbG27R2Jfo5QDgbCVc 9QoP3iiqOXwRqbneH+3iWosL/p+xAwh3Hq7f1p0LywVzeRmq2lgFNM32OlwinE7UzbVX yMW9teTKnrj1Al46kiue0AB2n3296lIF42hqhQFGgeE1vYpTHhiuXQuxwc2hZ4nNKwRH isA0vOLM6Aa4TQeEtVhR2R81nZamtF1/iOsjZOC3ar487+3vl16xMVtexSifyI/awolf +wrULRrO5VVEyPLcc+k6aohGJJjWcxCSy9zQwhsImkE65xdVBXi8GyYTRLgqnsLsV/Cc G8gQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712622793; x=1713227593; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=f/CJmT3YaooE8CB2QIvUgCnYPke09ytqabQq9mjiodg=; b=kXLKfWyUwFPEMxJkSyZ/PxTsQvppzBjgEbV7GKWouT6DtOHiMBnt6b+9ckfisbND3W P2jCrp5XAP75TKNfbRkx68ViliQiGpkK5fFtmki0txkrDNrtTlOZj95Czf5cuZOslmzD PeM+aV8hHAZllI3fIiykYz1UZAzqO8MeFuFBcTQ9A66KYRi/5ceyrP8B7bTUx4s+tU9b Wv7lzasH7Ne4OQu9IM1ymcWIONb89gH2L9MPYByU4Y09hC3CUYjRFCHEGEUD75MeseRa 1YY6rVa9skpaxt8kFrIqPfes3Rn6spCzwPZYCNoGGMBG6zhaL9Aek4wNyYgdIKMXNrN3 ltkQ== X-Gm-Message-State: AOJu0Yxm4+LO6TWcgKBfpAKhYXSuM2vZQzQFlcpL/5L/T741wVZrI3fh ANMEM90JZf8+bNwfiWN4y1RJSxmfO4533XCFNLyrgqo0NA7frbCVVdHtBR8v640= X-Received: by 2002:a17:903:404b:b0:1e4:35b9:f150 with SMTP id n11-20020a170903404b00b001e435b9f150mr2977778pla.11.1712622792665; Mon, 08 Apr 2024 17:33:12 -0700 (PDT) Received: from ?IPV6:2601:647:4180:9630::e8c1? ([2601:647:4180:9630::e8c1]) by smtp.gmail.com with ESMTPSA id m10-20020a170902db0a00b001e0e977f655sm7621722plx.159.2024.04.08.17.33.11 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 08 Apr 2024 17:33:12 -0700 (PDT) Message-ID: Date: Mon, 8 Apr 2024 17:33:10 -0700 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v5 12/22] RISC-V: KVM: Implement SBI PMU Snapshot feature Content-Language: en-US To: Andrew Jones Cc: linux-kernel@vger.kernel.org, Anup Patel , Ajay Kaher , Alexandre Ghiti , Alexey Makhalov , Conor Dooley , Juergen Gross , 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 , virtualization@lists.linux.dev, VMware PV-Drivers Reviewers , Will Deacon , x86@kernel.org References: <20240403080452.1007601-1-atishp@rivosinc.com> <20240403080452.1007601-13-atishp@rivosinc.com> <20240405-1060c986299eaac3528c7d4f@orel> From: Atish Patra In-Reply-To: <20240405-1060c986299eaac3528c7d4f@orel> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 4/5/24 04:23, Andrew Jones wrote: > On Wed, Apr 03, 2024 at 01:04:41AM -0700, 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 | 121 +++++++++++++++++++++++++- >> arch/riscv/kvm/vcpu_sbi_pmu.c | 3 + >> 3 files changed, 130 insertions(+), 1 deletion(-) >> >> diff --git a/arch/riscv/include/asm/kvm_vcpu_pmu.h b/arch/riscv/include/asm/kvm_vcpu_pmu.h >> index 395518a1664e..77a1fc4d203d 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 address) */ >> + 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 cidx, >> struct kvm_vcpu_sbi_return *retdata); >> void kvm_riscv_vcpu_pmu_init(struct kvm_vcpu *vcpu); >> +int kvm_riscv_vcpu_pmu_snapshot_set_shmem(struct kvm_vcpu *vcpu, unsigned long saddr_low, >> + unsigned long saddr_high, unsigned long flags, >> + struct kvm_vcpu_sbi_return *retdata); >> 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 2d9929bbc2c8..f706c688b338 100644 >> --- a/arch/riscv/kvm/vcpu_pmu.c >> +++ b/arch/riscv/kvm/vcpu_pmu.c >> @@ -14,6 +14,7 @@ >> #include >> #include >> #include >> +#include >> #include >> >> #define kvm_pmu_num_counters(pmu) ((pmu)->num_hw_ctrs + (pmu)->num_fw_ctrs) >> @@ -311,6 +312,80 @@ int kvm_riscv_vcpu_pmu_read_hpm(struct kvm_vcpu *vcpu, unsigned int csr_num, >> return ret; >> } >> >> +static void kvm_pmu_clear_snapshot_area(struct kvm_vcpu *vcpu) >> +{ >> + struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu); >> + int snapshot_area_size = sizeof(struct riscv_pmu_snapshot_data); >> + >> + if (kvpmu->sdata) { >> + if (kvpmu->snapshot_addr != INVALID_GPA) { >> + memset(kvpmu->sdata, 0, snapshot_area_size); >> + kvm_vcpu_write_guest(vcpu, kvpmu->snapshot_addr, >> + kvpmu->sdata, snapshot_area_size); >> + } else { >> + pr_warn("snapshot address invalid\n"); >> + } >> + kfree(kvpmu->sdata); >> + kvpmu->sdata = NULL; >> + } >> + kvpmu->snapshot_addr = INVALID_GPA; >> +} >> + >> +int kvm_riscv_vcpu_pmu_snapshot_set_shmem(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 = vcpu_to_pmu(vcpu); >> + int snapshot_area_size = sizeof(struct riscv_pmu_snapshot_data); >> + int sbiret = 0; >> + gpa_t saddr; >> + unsigned long hva; >> + bool writable; >> + >> + if (!kvpmu || flags) { >> + sbiret = SBI_ERR_INVALID_PARAM; >> + goto out; >> + } >> + >> + if (saddr_low == SBI_SHMEM_DISABLE && saddr_high == SBI_SHMEM_DISABLE) { >> + kvm_pmu_clear_snapshot_area(vcpu); >> + return 0; >> + } >> + >> + saddr = saddr_low; >> + >> + if (saddr_high != 0) { >> + if (IS_ENABLED(CONFIG_32BIT)) >> + saddr |= ((gpa_t)saddr << 32); > > saddr |= ((gpa_t)saddr_high << 32) > Oops. Thanks for catching it. Fixed. >> + else >> + sbiret = SBI_ERR_INVALID_ADDRESS; >> + goto out; >> + } >> + > > Thanks, > drew