From patchwork Thu Apr 11 00:07:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Kumar Patra X-Patchwork-Id: 13625192 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7EBDBCD1299 for ; Thu, 11 Apr 2024 00:09:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=gyJ44Wk/Z3AZwJ7mezKGYSIx9wQ0v16ZOgR6kmEHeBw=; b=uAb7w8kRTgSnB6 JuMKKyWoJ/jLBgbWfZGpXitt1pYozW/y/Lvvema3HsjII6p+dR7PthcJa3HUf49HxqZifdLWrgzpz N2iV/gU3MJr/9pNlFl7dpKfVMb6cYbFlMTvG0TbwWVRFT7/x+b+WWvA6oYgplQphgTZZR/YzuAHOK 6ncIjrW6erJi55YhaYNi+oLysiSR8xYukLhiD0wN2p56U0pKXVTRJEEmqPJTKe0Pba/52BnubF5Fj bmQyeWPGjDkh8m1aonyqmL4iZS49SjitIcIeB8VC1F2+9oy+LpOfvu3BJ/fgWjlOqrQjcaTGkFV8G j/7Zu4HCyKOUoI5yeIXw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rui0N-00000009aa1-1V65; Thu, 11 Apr 2024 00:09:27 +0000 Received: from mail-pl1-x633.google.com ([2607:f8b0:4864:20::633]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1ruhzn-00000009a1k-3mEe for linux-riscv@lists.infradead.org; Thu, 11 Apr 2024 00:09:00 +0000 Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-1e50a04c317so5587425ad.1 for ; Wed, 10 Apr 2024 17:08:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1712794131; x=1713398931; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=qvJukw934GuSrXeFuup9ak7dnmqxN0Lb+FQnvb7sFPo=; b=dnMIUFehZ22LKPiNiP1iPA/Cl3/d7WDp+q77KAjHUkwa+l/oTnOPC9pL8oaZPjerHe OtyW6VfAgq7B+YjoytTJiUk3JUwJv/OcYfwFrKGp2F170Gz9ZfHg7KnVrxWi/GmVCZDu eG07ZmxqUaJVncKXLAZN64dslzBZbpd6UsyJTIY7E7ORYI1ebt6LOnGimsGDW+k7nqmG uB4EYDqmNM9DkZARIHohjm6WnNP+e1iDywnjzbh2pxTH5LELERUg8ILqUUu4S9BGOv3A ezujPG2a4vbWY1rO6CFlhlfW+uK7+8La0N03AZYZR3apuxDA3N08C3q3T0evMIsCqJuL lvYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712794131; x=1713398931; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qvJukw934GuSrXeFuup9ak7dnmqxN0Lb+FQnvb7sFPo=; b=ASYJgtyiPNKObOejvAhbOpstPXYQj5dtJ7DksudVDRS9HUKP9WuBu/0AKDNb8CXVBC ZYMGo27h97MDWAwyyReqbWwnVWuXVmT+aX4o2Bau/0EgeGsYbrUP+Jgz7BvXKGi4Q199 NNbRwfJGAtx9SGASWl3T6r7/ZW8zmXViu9x6Drrzb9adMqzdczcxLU9iQrRHEIj+SKfB iMVb72cMMAZ6CUPci1qlpvBUNe0WKo7e0yIcgV3PBpTswxLMJlkqyTM51Ufq9qUVBX/U Z9vasRS2Iib0PKhTWtkuGglAcNjG/8mdTeW8fjAGiIfIJAmQdDUTvHxBox13o5zVtDvk iqpg== X-Forwarded-Encrypted: i=1; AJvYcCUSYc3ijtwK85tJqurMtuOAPvunC1I4STWx5SRYaReh6psO+fKJc0scdU3bUeQvwTU3TJBlp/zYsD1/UwEHs+obnRQpS956xALgqhhK43bI X-Gm-Message-State: AOJu0Yzi16EnFhGD6Id794Mo77xVCrEi8Jiatpqt3VQd50nsplFBDStJ UVGnEMPZs34s2pax7w7K4zVEDU30b/DPfTNOh3WYutJcHbmO5KN8GdO8vhQVRHs= X-Google-Smtp-Source: AGHT+IGQl1xQbgxg+nNMIsyytsihji4E3ebv+uAWttmmQwL/4Oe9eSB0Um2dnBeEnpUODnyh1XkRQw== X-Received: by 2002:a17:902:db09:b0:1e3:dd66:58e1 with SMTP id m9-20020a170902db0900b001e3dd6658e1mr5752619plx.44.1712794131255; Wed, 10 Apr 2024 17:08:51 -0700 (PDT) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id f7-20020a170902684700b001e3d8a70780sm130351pln.171.2024.04.10.17.08.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Apr 2024 17:08:50 -0700 (PDT) From: Atish Patra To: linux-kernel@vger.kernel.org Cc: Atish Patra , Andrew Jones , Ajay Kaher , Albert Ou , Alexandre Ghiti , Alexey Makhalov , Anup Patel , Atish Patra , Broadcom internal kernel review list , 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, Will Deacon , x86@kernel.org Subject: [PATCH v6 16/24] RISC-V: KVM: Improve firmware counter read function Date: Wed, 10 Apr 2024 17:07:44 -0700 Message-Id: <20240411000752.955910-17-atishp@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240411000752.955910-1-atishp@rivosinc.com> References: <20240411000752.955910-1-atishp@rivosinc.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240410_170852_111993_E072FC7B X-CRM114-Status: GOOD ( 12.28 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Rename the function to indicate that it is meant for firmware counter read. While at it, add a range sanity check for it as well. Reviewed-by: Andrew Jones Signed-off-by: Atish Patra --- arch/riscv/include/asm/kvm_vcpu_pmu.h | 2 +- arch/riscv/kvm/vcpu_pmu.c | 7 ++++++- arch/riscv/kvm/vcpu_sbi_pmu.c | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/arch/riscv/include/asm/kvm_vcpu_pmu.h b/arch/riscv/include/asm/kvm_vcpu_pmu.h index 55861b5d3382..fa0f535bbbf0 100644 --- a/arch/riscv/include/asm/kvm_vcpu_pmu.h +++ b/arch/riscv/include/asm/kvm_vcpu_pmu.h @@ -89,7 +89,7 @@ int kvm_riscv_vcpu_pmu_ctr_cfg_match(struct kvm_vcpu *vcpu, unsigned long ctr_ba unsigned long ctr_mask, unsigned long flags, unsigned long eidx, u64 evtdata, struct kvm_vcpu_sbi_return *retdata); -int kvm_riscv_vcpu_pmu_ctr_read(struct kvm_vcpu *vcpu, unsigned long cidx, +int kvm_riscv_vcpu_pmu_fw_ctr_read(struct kvm_vcpu *vcpu, unsigned long cidx, struct kvm_vcpu_sbi_return *retdata); int kvm_riscv_vcpu_pmu_fw_ctr_read_hi(struct kvm_vcpu *vcpu, unsigned long cidx, struct kvm_vcpu_sbi_return *retdata); diff --git a/arch/riscv/kvm/vcpu_pmu.c b/arch/riscv/kvm/vcpu_pmu.c index e1409ec9afc0..04db1f993c47 100644 --- a/arch/riscv/kvm/vcpu_pmu.c +++ b/arch/riscv/kvm/vcpu_pmu.c @@ -235,6 +235,11 @@ static int pmu_ctr_read(struct kvm_vcpu *vcpu, unsigned long cidx, u64 enabled, running; int fevent_code; + if (cidx >= kvm_pmu_num_counters(kvpmu) || cidx == 1) { + pr_warn("Invalid counter id [%ld] during read\n", cidx); + return -EINVAL; + } + pmc = &kvpmu->pmc[cidx]; if (pmc->cinfo.type == SBI_PMU_CTR_TYPE_FW) { @@ -747,7 +752,7 @@ int kvm_riscv_vcpu_pmu_fw_ctr_read_hi(struct kvm_vcpu *vcpu, unsigned long cidx, return 0; } -int kvm_riscv_vcpu_pmu_ctr_read(struct kvm_vcpu *vcpu, unsigned long cidx, +int kvm_riscv_vcpu_pmu_fw_ctr_read(struct kvm_vcpu *vcpu, unsigned long cidx, struct kvm_vcpu_sbi_return *retdata) { int ret; diff --git a/arch/riscv/kvm/vcpu_sbi_pmu.c b/arch/riscv/kvm/vcpu_sbi_pmu.c index cf111de51bdb..e4be34e03e83 100644 --- a/arch/riscv/kvm/vcpu_sbi_pmu.c +++ b/arch/riscv/kvm/vcpu_sbi_pmu.c @@ -62,7 +62,7 @@ static int kvm_sbi_ext_pmu_handler(struct kvm_vcpu *vcpu, struct kvm_run *run, ret = kvm_riscv_vcpu_pmu_ctr_stop(vcpu, cp->a0, cp->a1, cp->a2, retdata); break; case SBI_EXT_PMU_COUNTER_FW_READ: - ret = kvm_riscv_vcpu_pmu_ctr_read(vcpu, cp->a0, retdata); + ret = kvm_riscv_vcpu_pmu_fw_ctr_read(vcpu, cp->a0, retdata); break; case SBI_EXT_PMU_COUNTER_FW_READ_HI: if (IS_ENABLED(CONFIG_32BIT))