From patchwork Tue Dec 24 21:04:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Patra X-Patchwork-Id: 13920542 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 BED0BE7718E for ; Tue, 24 Dec 2024 21:05:19 +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:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=e/AKWhu+MMV5Wt8qWidqEPX8X3hFWWiMGEg3OXQU18A=; b=Kha6rrodmsW36H N8l2NI+9wv5Gb3T6gjSS/EwASFquX8OscEtIMAhnhr2Wq3fekNixhScZFMyxv1nyeYtAY9gGzXBas RLn1u+5ehu7ajXobJNWfj1U11Dc9si/++fHgtGTauU9l2IE1Nn/+ApTtJfsOx7nZE9zgXs0RxFbkA MgaBIrm1ppOdqYRHDEiaiId16+S+MYjKmv/SfSxDWlG5Pn5YLac8b16/KjJC4n8g0lPvhsJX+aYzA VWXMZoFmPG76isvYuE/K4mkkxwnMQf1TAxNrzZsxoQcyaI86jA5BQ9vMvU7LndUe4CbyOBmO+yOnr fRaz5JWJeKEccl5HjQpg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tQC5P-0000000CnTv-1sCu; Tue, 24 Dec 2024 21:05:03 +0000 Received: from mail-pl1-x633.google.com ([2607:f8b0:4864:20::633]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tQC5M-0000000CnRB-0NNg for linux-riscv@lists.infradead.org; Tue, 24 Dec 2024 21:05:02 +0000 Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-216401de828so55240035ad.3 for ; Tue, 24 Dec 2024 13:04:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1735074299; x=1735679099; darn=lists.infradead.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=K7Pb5Vxtmu1FY9/J9EKk+mhOADazj56h0vKNA3c2QKo=; b=zLQqCeTA8kG12fOAPvFCvR1bGqL9AI8qTX9ic4wb7/Np/2oc30/PeLMcQfszohurNu uq1Xc1xt33vT3Q4Rx1GBJdg3SSXzdh0ecz47LH9L626nMI0rE8rBAFLtgYrNh90dL6Vd aSf+lCZBStZ1czE/x6qr36tSWdzIaZzacEspT99bkFXj/V7Jeo/t3Q3XcbsVKxuHFVww Iah/dHPrj+cEIGetq+XLvcn6jRh3djep7jOqWstxJ916/TiR8OQ9W2YedtvBBd8YzTeG I4Ihd5lmdaUljIc5IME0T1ZiA9TwbMkGgnLMqTdfrBUIV4vc8USfiGR7dzMc53WCk8jB +E0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735074299; x=1735679099; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=K7Pb5Vxtmu1FY9/J9EKk+mhOADazj56h0vKNA3c2QKo=; b=gl5oyOZFOUubaCkLpI0v514AY3J82N5AoKQqbaRl3m6XhnOpOxichcCQ+p69O7Fswt XVvrrkzHdwksiUMSmF5Coilt9ykfx4DpqYCOZPmnuUEd7U8ScyBQ1LzNMtfLkc30+P20 aj1dc5q1xb2ZJyFn6c0GMr94j0x+LoWThSPXVyRMbRdeA5mG63GxprB40VsKDRsZ9YmN RKKOhxNoNfvMyYm+o+JFjdjhnZdPdKEqurjjkZkUUpb9n3Vesx1sikKEcCDP+71Oz12I sjJLq5MUUZUpY92R/8/hUnqs0p9ALt4otHMs//XkIar9ph9eZ4ZRRQuygSwIV97WTsQh 5pJQ== X-Forwarded-Encrypted: i=1; AJvYcCXHbYO9KSxPZVYe6na8YbzHFFIX07P1H4So/6yE+enzTNKe8hftWrpJ45wbf9F2AiV7C/30U4zcySgECQ==@lists.infradead.org X-Gm-Message-State: AOJu0YxzuueTDwv1GGnnO/IhO/R4FSn2CDxxOKSycfQCsthJUQbbMGVI /LO+cMav4wzmqLeem633ssiK4tBEDMWHuXYMgrDU6NFDKpmuPipVhLhSqihBcro= X-Gm-Gg: ASbGncvoLaM+mZUuT6cZjqKOBfYPcJhRdcfes5hr6VIYO1ZBGnjwJQATY6T44m2eeue O6lX9DOHh/l4bztzU1a2FVLHQFMGkJONAGr81oxYMiJOnQgiz4NGk4+KsozoD0ZDH0QppBvHnC+ PU3Iw5CkdTcDiII1px8ASVFit0f9U1iDpBNOjMNfpaaVuIn1hca8Ko8cVdfcGCx2G/tIyS/Uyhe XG4T+1nQxagaUteURkMMtAOrp0uMfHJdJcinwVABWFRxLuC0D9hWAOM/PlcMxh8+AV5dA== X-Google-Smtp-Source: AGHT+IEINBS2wMAKPXcEeiQvMr4mHt6N/s5a4KNXcdkRoepKl+xENIOeQWFga5Ts/3lf9tV+pKQhqg== X-Received: by 2002:a05:6a00:3905:b0:725:e73c:c415 with SMTP id d2e1a72fcca58-72abde8fd6fmr24905523b3a.18.1735074299238; Tue, 24 Dec 2024 13:04:59 -0800 (PST) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72aad90c344sm10445925b3a.186.2024.12.24.13.04.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Dec 2024 13:04:58 -0800 (PST) From: Atish Patra Date: Tue, 24 Dec 2024 13:04:53 -0800 Subject: [PATCH v2 1/3] RISC-V: KVM: Redirect instruction access fault trap to guest MIME-Version: 1.0 Message-Id: <20241224-kvm_guest_stat-v2-1-08a77ac36b02@rivosinc.com> References: <20241224-kvm_guest_stat-v2-0-08a77ac36b02@rivosinc.com> In-Reply-To: <20241224-kvm_guest_stat-v2-0-08a77ac36b02@rivosinc.com> To: Anup Patel , Atish Patra , Paul Walmsley , Palmer Dabbelt Cc: kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Atish Patra , Quan Zhou X-Mailer: b4 0.15-dev-13183 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241224_130500_137472_5F123742 X-CRM114-Status: UNSURE ( 8.84 ) X-CRM114-Notice: Please train this message. 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 From: Quan Zhou The M-mode redirects an unhandled instruction access fault trap back to S-mode when not delegating it to VS-mode(hedeleg). However, KVM running in HS-mode terminates the VS-mode software when back from M-mode. The KVM should redirect the trap back to VS-mode, and let VS-mode trap handler decide the next step. Signed-off-by: Quan Zhou Reviewed-by: Anup Patel Signed-off-by: Atish Patra --- arch/riscv/kvm/vcpu_exit.c | 1 + 1 file changed, 1 insertion(+) diff --git a/arch/riscv/kvm/vcpu_exit.c b/arch/riscv/kvm/vcpu_exit.c index fa98e5c024b2..c9f8b2094554 100644 --- a/arch/riscv/kvm/vcpu_exit.c +++ b/arch/riscv/kvm/vcpu_exit.c @@ -187,6 +187,7 @@ int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run, case EXC_STORE_MISALIGNED: case EXC_LOAD_ACCESS: case EXC_STORE_ACCESS: + case EXC_INST_ACCESS: if (vcpu->arch.guest_context.hstatus & HSTATUS_SPV) { kvm_riscv_vcpu_trap_redirect(vcpu, trap); ret = 1; From patchwork Tue Dec 24 21:04:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Patra X-Patchwork-Id: 13920540 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 654B9E7718E for ; Tue, 24 Dec 2024 21:05:12 +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:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=30HPs25huYEiHoIad30ivRy/4ReQ8n9CnIVnl0kvQXw=; b=I9ahE6GnxetD5o pyLitpeJtL55Q1eVcC43TtK8Q7bsd90AZTKEFMm1bBH7Dh/uZdxuVKUx0PvL4A9GB+sIA0x9XaDm4 xQwZL5fbruKkJ7fmKL2ZHotic/vX8VP12rdVF0gfVlDlXSXrlc2be4qJ60mgKQxSHxQqgjdGWYJQ+ QOa3oxwy1fbJpLcw23NpELL5jfV6ljQbD7NunJNurM+CO0UaYhnqgijwe44xhHI6UopdGRbj3Rt1W NxPmkNoUrF6YT7a68L6O7SlS/i9wsLHuVMsjy/bKtNlZTdXyQ69Uf4h6Qa0SpZ+4ciWNIJPfB6PBZ RvDFyKBH+tH7jpAO0GEg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tQC5Q-0000000CnUJ-1jg1; Tue, 24 Dec 2024 21:05:04 +0000 Received: from mail-pl1-x634.google.com ([2607:f8b0:4864:20::634]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tQC5M-0000000CnRk-2pXU for linux-riscv@lists.infradead.org; Tue, 24 Dec 2024 21:05:02 +0000 Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-216401de828so55240105ad.3 for ; Tue, 24 Dec 2024 13:05:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1735074300; x=1735679100; darn=lists.infradead.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=wyrUQMinm9VfIkz//Zqi5azyB73CtB0XBmp3HQwwuxA=; b=l2GPhqpAxO1Ju9VS8vhndc7qDLqctOKN6zkdl+eRkWOJzWnSq+GbsOTPFHL4SAuAfG UX4QAv8JP0ihfn06Ut76ecQ1zB9I0mzuiNhNCNqUqfzFGD4HDMqff/wzWq5VhbqWZb5O AA3enyTIQ/+IKnhXRcSMaNV8yCpB8tEZVba0fZUSCxLFoznveF0VWyrnJr2EaTxS6680 bVK7iAIk+nKF7InWFs7Bt25tkn8g3cc2G4oCuGITmDxVWfyD8phFVTEX/y3uOosUmhgS N33QQxfogWElp4Le2tSN/Pvrmwc2DOqxS45V1N6EtHKPcZN9o8IXkgakyX5snVDR6l63 +TpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735074300; x=1735679100; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wyrUQMinm9VfIkz//Zqi5azyB73CtB0XBmp3HQwwuxA=; b=raLyCcSweGZFJ2e0p1FeataN3UJ4K3CO+RQaCdsv6qSgvwFHTsD5cEUHVDMhTWq8wp Tz+xJXSRXSIs+rw7Z4RottlY3dS7y4sT2Q9sBJNPQPYL5JiNdCLwX5lxeoSesA7CHRDy f1+jxukS9EOFieDjC5RKWmzXh28b7TI4a6/Xvo29/l9c7HTm15Ri1Ui2634Z0TlZ9XRP 00U2YyTtLH2EIlHC3PwuXkDNxfBx191g60PuAlreagtm3TN4dhXLASOWGwuOWkllBNki 0I0M6i1i5WNTgCJUmQJzUK0mt0C7gAPqbPMbZPWdn28aZRpTpAYilzZCeGZvRUQamcDe pNbQ== X-Forwarded-Encrypted: i=1; AJvYcCUjJhiVKi/o0NOKC2MIPVeQzz/lRTJym4rtlzGyHqnyaZtKBqdqHL/vu47yguLRTcncePYfz68W4btpwQ==@lists.infradead.org X-Gm-Message-State: AOJu0YwXSNkXPll4sD3HEc/COw9uSEbRxViyNhqiHQRB06xc3kU9uyM7 9KSOWEhzISiHfEjwdPsROdFtOWxh84v4kTbYoLb5t+RRzeUndnnZ7xR5BYYetOY= X-Gm-Gg: ASbGncunDuNO4TD+HjEFrqFOTashNS43qkUVLjNA3qtHkftoMHQ0dtl35mpReRyiMTF hyTKczBQn/2HXVhVIG8kuI8r9covdW2C4K5Wh0KigPeg4l46YICReGR/34dI1jf3EbUUvBHzlLi 0Pumj6QodBj/S1GcjIetxhzvvphSQDuKDmBVYhldrF8z/lXaT78F7DAke1siOlJMjozN5KwHw1J zZN7SxHrPJBzttGqcaHxiktACdO7Omubmssnsn727xsp5zAuqmuRmsatPv61N8b9Bfd6A== X-Google-Smtp-Source: AGHT+IFDnCQ0fa4NqS6VnWu4yH1L9kNVbzZkBTsq+P+df9RDRyuUZJOIb8A9T8ylvjy7lFyFnN1bzg== X-Received: by 2002:a05:6a21:7e8a:b0:1e1:ae9a:6316 with SMTP id adf61e73a8af0-1e606dfe621mr13695856637.35.1735074300205; Tue, 24 Dec 2024 13:05:00 -0800 (PST) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72aad90c344sm10445925b3a.186.2024.12.24.13.04.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Dec 2024 13:04:59 -0800 (PST) From: Atish Patra Date: Tue, 24 Dec 2024 13:04:54 -0800 Subject: [PATCH v2 2/3] RISC-V: KVM: Update firmware counters for various events MIME-Version: 1.0 Message-Id: <20241224-kvm_guest_stat-v2-2-08a77ac36b02@rivosinc.com> References: <20241224-kvm_guest_stat-v2-0-08a77ac36b02@rivosinc.com> In-Reply-To: <20241224-kvm_guest_stat-v2-0-08a77ac36b02@rivosinc.com> To: Anup Patel , Atish Patra , Paul Walmsley , Palmer Dabbelt Cc: kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Atish Patra X-Mailer: b4 0.15-dev-13183 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241224_130500_724209_643CB7C7 X-CRM114-Status: GOOD ( 12.48 ) 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 SBI PMU specification defines few firmware counters which can be used by the guests to collect the statstics about various traps occurred in the host. Update these counters whenever a corresponding trap is taken Reviewed-by: Anup Patel Signed-off-by: Atish Patra --- arch/riscv/kvm/vcpu_exit.c | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/arch/riscv/kvm/vcpu_exit.c b/arch/riscv/kvm/vcpu_exit.c index c9f8b2094554..acdcd619797e 100644 --- a/arch/riscv/kvm/vcpu_exit.c +++ b/arch/riscv/kvm/vcpu_exit.c @@ -165,6 +165,17 @@ void kvm_riscv_vcpu_trap_redirect(struct kvm_vcpu *vcpu, vcpu->arch.guest_context.sstatus |= SR_SPP; } +static inline int vcpu_redirect(struct kvm_vcpu *vcpu, struct kvm_cpu_trap *trap) +{ + int ret = -EFAULT; + + if (vcpu->arch.guest_context.hstatus & HSTATUS_SPV) { + kvm_riscv_vcpu_trap_redirect(vcpu, trap); + ret = 1; + } + return ret; +} + /* * Return > 0 to return to guest, < 0 on error, 0 (and set exit_reason) on * proper exit to userspace. @@ -183,15 +194,27 @@ int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run, run->exit_reason = KVM_EXIT_UNKNOWN; switch (trap->scause) { case EXC_INST_ILLEGAL: + kvm_riscv_vcpu_pmu_incr_fw(vcpu, SBI_PMU_FW_ILLEGAL_INSN); + ret = vcpu_redirect(vcpu, trap); + break; case EXC_LOAD_MISALIGNED: + kvm_riscv_vcpu_pmu_incr_fw(vcpu, SBI_PMU_FW_MISALIGNED_LOAD); + ret = vcpu_redirect(vcpu, trap); + break; case EXC_STORE_MISALIGNED: + kvm_riscv_vcpu_pmu_incr_fw(vcpu, SBI_PMU_FW_MISALIGNED_STORE); + ret = vcpu_redirect(vcpu, trap); + break; case EXC_LOAD_ACCESS: + kvm_riscv_vcpu_pmu_incr_fw(vcpu, SBI_PMU_FW_ACCESS_LOAD); + ret = vcpu_redirect(vcpu, trap); + break; case EXC_STORE_ACCESS: + kvm_riscv_vcpu_pmu_incr_fw(vcpu, SBI_PMU_FW_ACCESS_STORE); + ret = vcpu_redirect(vcpu, trap); + break; case EXC_INST_ACCESS: - if (vcpu->arch.guest_context.hstatus & HSTATUS_SPV) { - kvm_riscv_vcpu_trap_redirect(vcpu, trap); - ret = 1; - } + ret = vcpu_redirect(vcpu, trap); break; case EXC_VIRTUAL_INST_FAULT: if (vcpu->arch.guest_context.hstatus & HSTATUS_SPV) From patchwork Tue Dec 24 21:04:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Patra X-Patchwork-Id: 13920539 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 7BDE0E77188 for ; Tue, 24 Dec 2024 21:05:11 +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:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=IwadVr4r86+6N+gM/sFAuFMfyVKH+5+xVeaholYTqL8=; b=0Fm9zEc/9ZCBE1 /TUak6+6s9dAojWcN24Dzx8cqF9tineyBNhdlzJLNGfChlPWTzwzvxzhDE8QBxQTWYdlQfIXAw5MA 0aSrbAh+rss8m06NOneuM8YCAEpnygAbAx5tfckVwt9Tabqn23VPAX9G7trXqLqMSTWjLdas5DZvu DYCWbet/zQZr+jo97pFy2EOaKjSH32LYj/dbM8ZjgXRLuqhGaKULMM17GOMgR25patKnh7nd+pul4 FTUKUvt9TFi9RglzeCpnPohmOABHKwXMKleUngNRYGZK34aN89IhmCCw9G0fitNTQ3jEhUTsHegLq p6nfoS+dAftrbOloFI7g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tQC5T-0000000CnW7-03Wd; Tue, 24 Dec 2024 21:05:07 +0000 Received: from mail-pl1-x62e.google.com ([2607:f8b0:4864:20::62e]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tQC5O-0000000CnSo-01JE for linux-riscv@lists.infradead.org; Tue, 24 Dec 2024 21:05:03 +0000 Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-2161eb95317so59176545ad.1 for ; Tue, 24 Dec 2024 13:05:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1735074301; x=1735679101; darn=lists.infradead.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=JlUmPcZY4TN/RdMqQ8PaLtNf3W/Wbsw1dLdhHB5EEeA=; b=KfFx8+6s7fI83WfpMit9MuGEgauR0YLvLZBKkK/EzFI79Ut7pYtYFr6aWo56QKOh2f lNQg9CM8tM4jJe332GeSuMJ84KXysf2Jv/rRU01ALw/6U4Zi4sM+3YfVXKrcBFc0ZKDD dD+JD0CKTY49VGdpPwncFhK3t/N+fRnTg//Pz+TwW+NxGOVhm5x+6bwaH0vtX4A9wPhK n/rl/63t/z8/H4eWEXrS7FmxoBGX6CjlXwDeTLtdh1IbLPqtyh6OgscGKWRMBsPjQ29U Pq0uXEh8N+LYaw1wtyDqYhhcjEXlcz2wfISdsM1bOnvsCYZ5tAlj2PBsK9BjFHuIobbY NeVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735074301; x=1735679101; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=JlUmPcZY4TN/RdMqQ8PaLtNf3W/Wbsw1dLdhHB5EEeA=; b=E/yyfKJGDDrRpohDmp/t6lwbWiSUiXZid4nh6KitadyeJqTjmdvHB6z0+A/v7CX6Ow mvqWtoluyW+ijBA4Re6Oh4MxaTJkpDJYCHuLLjUsSTyC2QSx2CzhSP05TUBzxgpIr3KA 6JesHFJg3aYC4PYnWTt6YYwiPO6BLAFEO5Sk0YxNpEzWdP9Xbv5JW9aIZAneKDqvwR1D 71Ud4VEykq3SqD30y6E/2a/CSLK+WATw0fxLwq10QfriNj5jGJzlVkFFkF1mVXY7/xJK LGg6xcDYC46RDqVgud/3gWyO1f/LIKvAzSgBZIlV6xShHAFGVSUzUe9OWzudHbcVKwqn aBfg== X-Forwarded-Encrypted: i=1; AJvYcCXmGKMHB7NXGWMurkDRnF/BNOz3qZhB9fr5ebKDh3QlRIlvk4G3h9CtyYppL3bOVOSbEjWLgoEFAsb+5A==@lists.infradead.org X-Gm-Message-State: AOJu0YxFGHRwP3Ad3/56fWnd64UXCmBT4gPwHcge5xDvWERLzr23G8Np TqJ3eTyPmjnkCwMtEWWlhsXD177uG5IWhDr+W2go8VwbEOE+pEjGd1TbR+Lw0Cg= X-Gm-Gg: ASbGncsFeZ2r9xC9HjfL02kK2YNzfDSW+Vlwqax+EHrNFrvpJayxByaWr0GdqDx69Pl 4+rXvOQscF7jer/JMyOh+sNifJsL/8s+eBtnwyOrcCufCRDhCgdQwi0RaXv7gcAAPXFqRnBAtDN r/WRDoE2RascamyyQazQkGXwvaENhRIL8RDRLnhK0FapQaTrQZ7V+gdmGWQ8oVANbQLhL0MzI9V +OcAV5qf5gKdOjGUffzKKHTC0HR8FioWecsQaLYx+/vljkuxliXJGXp0LWL+0Fn7BDdFg== X-Google-Smtp-Source: AGHT+IFkAvBUy0y2QznmiDw5xGryBuSrbox2rOO+r9dXtaG1Z1Twj1tRDQZutMEwjnEXldms1wIZJA== X-Received: by 2002:a05:6a20:a121:b0:1db:ebf4:2cb8 with SMTP id adf61e73a8af0-1e5e081c839mr30524444637.38.1735074301156; Tue, 24 Dec 2024 13:05:01 -0800 (PST) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72aad90c344sm10445925b3a.186.2024.12.24.13.05.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Dec 2024 13:05:00 -0800 (PST) From: Atish Patra Date: Tue, 24 Dec 2024 13:04:55 -0800 Subject: [PATCH v2 3/3] RISC-V: KVM: Add new exit statstics for redirected traps MIME-Version: 1.0 Message-Id: <20241224-kvm_guest_stat-v2-3-08a77ac36b02@rivosinc.com> References: <20241224-kvm_guest_stat-v2-0-08a77ac36b02@rivosinc.com> In-Reply-To: <20241224-kvm_guest_stat-v2-0-08a77ac36b02@rivosinc.com> To: Anup Patel , Atish Patra , Paul Walmsley , Palmer Dabbelt Cc: kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Atish Patra X-Mailer: b4 0.15-dev-13183 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241224_130502_057137_B2ABCE8E X-CRM114-Status: UNSURE ( 9.92 ) X-CRM114-Notice: Please train this message. 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 Currently, kvm doesn't delegate the few traps such as misaligned load/store, illegal instruction and load/store access faults because it is not expected to occur in the guest very frequently. Thus, kvm gets a chance to act upon it or collect statistics about it before redirecting the traps to the guest. Collect both guest and host visible statistics during the traps. Enable them so that both guest and host can collect the stats about them if required. Reviewed-by: Anup Patel Signed-off-by: Atish Patra --- arch/riscv/include/asm/kvm_host.h | 5 +++++ arch/riscv/kvm/vcpu.c | 7 ++++++- arch/riscv/kvm/vcpu_exit.c | 5 +++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/kvm_host.h b/arch/riscv/include/asm/kvm_host.h index 35eab6e0f4ae..cc33e35cd628 100644 --- a/arch/riscv/include/asm/kvm_host.h +++ b/arch/riscv/include/asm/kvm_host.h @@ -87,6 +87,11 @@ struct kvm_vcpu_stat { u64 csr_exit_kernel; u64 signal_exits; u64 exits; + u64 instr_illegal_exits; + u64 load_misaligned_exits; + u64 store_misaligned_exits; + u64 load_access_exits; + u64 store_access_exits; }; struct kvm_arch_memory_slot { diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c index e048dcc6e65e..60d684c76c58 100644 --- a/arch/riscv/kvm/vcpu.c +++ b/arch/riscv/kvm/vcpu.c @@ -34,7 +34,12 @@ const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = { STATS_DESC_COUNTER(VCPU, csr_exit_user), STATS_DESC_COUNTER(VCPU, csr_exit_kernel), STATS_DESC_COUNTER(VCPU, signal_exits), - STATS_DESC_COUNTER(VCPU, exits) + STATS_DESC_COUNTER(VCPU, exits), + STATS_DESC_COUNTER(VCPU, instr_illegal_exits), + STATS_DESC_COUNTER(VCPU, load_misaligned_exits), + STATS_DESC_COUNTER(VCPU, store_misaligned_exits), + STATS_DESC_COUNTER(VCPU, load_access_exits), + STATS_DESC_COUNTER(VCPU, store_access_exits), }; const struct kvm_stats_header kvm_vcpu_stats_header = { diff --git a/arch/riscv/kvm/vcpu_exit.c b/arch/riscv/kvm/vcpu_exit.c index acdcd619797e..6e0c18412795 100644 --- a/arch/riscv/kvm/vcpu_exit.c +++ b/arch/riscv/kvm/vcpu_exit.c @@ -195,22 +195,27 @@ int kvm_riscv_vcpu_exit(struct kvm_vcpu *vcpu, struct kvm_run *run, switch (trap->scause) { case EXC_INST_ILLEGAL: kvm_riscv_vcpu_pmu_incr_fw(vcpu, SBI_PMU_FW_ILLEGAL_INSN); + vcpu->stat.instr_illegal_exits++; ret = vcpu_redirect(vcpu, trap); break; case EXC_LOAD_MISALIGNED: kvm_riscv_vcpu_pmu_incr_fw(vcpu, SBI_PMU_FW_MISALIGNED_LOAD); + vcpu->stat.load_misaligned_exits++; ret = vcpu_redirect(vcpu, trap); break; case EXC_STORE_MISALIGNED: kvm_riscv_vcpu_pmu_incr_fw(vcpu, SBI_PMU_FW_MISALIGNED_STORE); + vcpu->stat.store_misaligned_exits++; ret = vcpu_redirect(vcpu, trap); break; case EXC_LOAD_ACCESS: kvm_riscv_vcpu_pmu_incr_fw(vcpu, SBI_PMU_FW_ACCESS_LOAD); + vcpu->stat.load_access_exits++; ret = vcpu_redirect(vcpu, trap); break; case EXC_STORE_ACCESS: kvm_riscv_vcpu_pmu_incr_fw(vcpu, SBI_PMU_FW_ACCESS_STORE); + vcpu->stat.store_access_exits++; ret = vcpu_redirect(vcpu, trap); break; case EXC_INST_ACCESS: