From patchwork Thu Dec 12 20:56: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: 13905980 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) (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 57DC31AF0B0 for ; Thu, 12 Dec 2024 20:57:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734037022; cv=none; b=Y4kgrLITINhwTSfhB9r1BiQdVI4Gx4EU1tRFhdPGIQtfRUKKfDxD2Rk73bUZ5E4LSF5QJYtJMw3Vlvl4nFVhHMKwYy/nT8tM77Dnp/faLwRnjNPTvZX8pvAzAv9V5Ge6MsUlad3c3bGeWgbW+2siPMerQ4hb751eySG159ADOMw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734037022; c=relaxed/simple; bh=STe4e+9WEqKYdCIY+5uN2OC0S3vV4Xd+VYk5Fl2odcE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=W1iv9WJIbeE0KUC8qWOz2E1MfwV48Dcc/hmrVPoyMH+O70eqhM4IDGWJn1AkRHNYrtNu4JSG7fQYlb8wDNyJMLmELQWlmvO1/M9bvhfw6exvphJt+Bam3IYKK3xFKlXx0b/gETSR+ziFJnHBZSvmCUGZ0R1YZzblH/wxL2KhKHg= 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=DDjfKFD1; arc=none smtp.client-ip=209.85.214.172 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 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="DDjfKFD1" Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-2165cb60719so9019055ad.0 for ; Thu, 12 Dec 2024 12:57:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1734037019; x=1734641819; darn=vger.kernel.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=d9bocf/te6xqIlX1+3PhmzweHiQ8rAPL41LYUao1zG4=; b=DDjfKFD1KbG1l5UEQv/eWLdfaogq7kyuXEijpz7Dm9ftgAGGKtKlA+ZyPCXnbscyji 8+fRGF50Sa5L8+vnFRBsPCRUXNyeB0KaXiAMVzLzr3C7kfU/zogUCqvtIcBNR1IbymUX ziVXgdd1GbMEekEt8e6XPQCSMPMyH/ByyJH0wI1qEHgVcNdiCzUEdf2xW1lzN7YzEKmm rDZ/v/QYRXdOndvGew7eLcnnQ/7e5ZUB4frRxjDkoz210sDcLPVLSdmiOK8Sk50k6+a/ 7zNzBvCtfcAubuUUBvTqGKhKk3b27qmh9xRnWI3suAwBVa4Tk5BFQfG6SLH8x/60HSkA 2kQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734037019; x=1734641819; 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=d9bocf/te6xqIlX1+3PhmzweHiQ8rAPL41LYUao1zG4=; b=UKPalXWr4JsUBQSO/VO76RD2xQkIr7/4sMW4m+0QnrYULZsHBGCbXdvj7pb58+aaaV OBeXHB2eVmHfK4+GY1UyhPbFl9XNc2Otn3C7zUqXq0Vw8F2gSyP67lRSJJn/n/j2U9lt pVzsoHuWNQPC9h00AYy4wKO7oT0Ca5rWgFC4E23xvoiKt0CQhLZ5UoaoOPBiP6zUDbTV iA+1rqWo9PTdpY+MQmyZKQKiJ0TTby9zv7HRDUbD8KckGDzILMnGGFy9tM0lFaq8qzYH ppy9sjEmlRD5kIJeQLtfFFjcCUOOSKDTU+K5IXlnv/iPKBvzxX4p7LAkLx4LkGIz9c6y pKww== X-Gm-Message-State: AOJu0Yx4+oH9WaqG3VMoW4ssWrzp17nJ7CpVkEsPJWtFqqSD54N0xN20 E+mls4ap6iMMdmOnZAg1wr0Hxkb9pGh0VgwWFslzLQ2LFgvoagAxZ/M6HbjNRjYeww1EtqrnqhH F X-Gm-Gg: ASbGncuHnBQhPBM2wqvVl0qbO32bE6IOV11Nk8+56ek5BMDdVIRk7iJoVJVXr/lWL8t ESeBpyBmhjSX0N27gdDwIQFtlZaldsB2GhmmqJx6WHhv4eaJAQzuBk+gSu/Tpt8yCct1QRjbD9o wg/6H7SMXl3ZB6ysGiNzjq9m6qWNqKa05bs2tWVTBVJO92/PxwdCHdDxOOejd0vEnAtUowxH+59 66OybA68lbjcWlTJQIphE/FkHX8/TcvWjWLl5TIl0jjra1Bq7IBXR78+Q5VS4jfkpB8aw== X-Google-Smtp-Source: AGHT+IFh1FwmitrjM9d4Q6KSLRToIyFObPTDHz3LQbh1hbARG5r4BQI4+oOsAfuzEnKREvzvjAp+Cw== X-Received: by 2002:a17:903:32cf:b0:215:b9a7:5282 with SMTP id d9443c01a7336-218929c3a18mr2340805ad.26.1734037019285; Thu, 12 Dec 2024 12:56:59 -0800 (PST) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2162e53798asm94019785ad.60.2024.12.12.12.56.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Dec 2024 12:56:59 -0800 (PST) From: Atish Patra Date: Thu, 12 Dec 2024 12:56:54 -0800 Subject: [PATCH 1/3] RISC-V: KVM: Redirect instruction access fault trap to guest Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241212-kvm_guest_stat-v1-1-d1a6d0c862d5@rivosinc.com> References: <20241212-kvm_guest_stat-v1-0-d1a6d0c862d5@rivosinc.com> In-Reply-To: <20241212-kvm_guest_stat-v1-0-d1a6d0c862d5@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 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: Atish Patra Signed-off-by: Quan Zhou --- 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 Thu Dec 12 20:56: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: 13905981 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (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 EB0401B1D61 for ; Thu, 12 Dec 2024 20:57:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734037022; cv=none; b=pjRgei6jgC5uNVJD17ZF6roerR+07qEAyEIjM+BfoX6VEqFOw+PmjowGugWRuljJ237tgQDPo4EaGKuYthhgNv9qQMFfRQulJjQgPbthiI1HFeY7GNWlSMuHExj7yN5hfmLP/JrcvdXTb22glPceP7c0ut6VkvkrotV+LUAZocE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734037022; c=relaxed/simple; bh=aB7frMBfUTuHgeG2iqJQRR7h5YH7OQziiHAZLJcCY3M=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ulGm3E7rLSKaIP4uM5qtEW1gpzrmOaKxexGeOVD00gX3/+LQ2QFfPiuP1iaJkbdqLnKoo1T4QRIUzrzU7+MMzro7iq+MtxKDyLTZ3pXysnRZD7GIe6Flb9rDQqiE0G87m9NGDtTCZ11K0+l9lf7E2JCyyTWgajsb7bM+j7N42b0= 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=d+Q8SSrs; arc=none smtp.client-ip=209.85.214.179 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 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="d+Q8SSrs" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-2163b0c09afso10114745ad.0 for ; Thu, 12 Dec 2024 12:57:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1734037020; x=1734641820; darn=vger.kernel.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=PaPMD+BdCyI5hLi3/kV/S6HLELSeYS1h2xBFfdAEcTQ=; b=d+Q8SSrsINXoShh+rKMAzeMdQY1OXx2224FD6ttzuoN81TfnIiUkbd2SalstFDhMrg vhVMAosz+DXP4dFkEXQqq2zJyhEseFgAoqS/fKP2aJKwwKsjIq5PEdLs5yuDw9Ru63S8 AQvkPfUbnfpXSCO0dyevNW74nHrWYWKQ2e0pfGKJMem0zLd+ip8pOk+Ay+wzjqi9fmyr CsGaSxK3S0IT5P200d7+8oYCKY7nxYVUzFq4OCAWAYp8pOkZPSbXbgSYx4XsGi6/GMn7 LUbeMntVtq3le9xl6/4f2QmZ+4DzxK8Ia06XC5gg66vptQbmGgVBrPvTplTBAAO9zTik tNrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734037020; x=1734641820; 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=PaPMD+BdCyI5hLi3/kV/S6HLELSeYS1h2xBFfdAEcTQ=; b=Vc0g0kP0M7HyVj+UGDiOIGaEn20J7+HcnSvSpk+VrLWg/cYQbZLYSPT1bzH91YCK5U nJtntUxDpZC37qEOjYP2ZVF+Js4Aa9twRWiPHBHJf6vNYfaFvgvAKUm9DfmkpN6wpiX1 YUuASQXO8J0cAwld4hBmzA5QS72iU8/aY0z2Wv56DDyYVTjLHKxfcXPpuI6zxy3nanrK 4A31+crQowM0pflUQ7ErJ5bImCc9norrZfVmZwTjBmgSTRLZ1kIJEvCuln/s4sbDvPVC BpEtI4jv5T1ixET4lXIqSsqTdzAEVkb0wrsQ3JskRvfr67YWtMlUek9ZDC1jK5mT9XaC dhpQ== X-Gm-Message-State: AOJu0YzEzn33S+79ZxWRo4A3fP2bHzBUZxRXRD38sYRjcHYB0GSlpf/C wikA0MpBFY4uoemTTO+vf8rmyoPs6XjdGOH2mZwSn+X/mCb0645WMvvDGaaGjKKNARMrAVqxlJK E X-Gm-Gg: ASbGncsJVcI+h5BNLhDtXwXrSVpXFycDxZm+wWWZT89wzBZDtW0bBFJFhcuhD/uaDBK iUmYBVA6LRpzKVRNTkdS5lIboFXA5HScMGV1HU1eaHtatSm1hxOdAr6R3nhVZ0jiu5mXJ6kj2nq Xuu9kosG1nnf1MZDNaem0K3YRit2p7059bU5h2yreg9zLZjQxoIZcsNpwArGL6cyvSmfq61Vz5L Z2ZAwpRpRhtE6BnqE889LJc4tqw3nMpx8S9eRD2yjRcXzhoPf++4bFHo5ANYHgAIB5Mbg== X-Google-Smtp-Source: AGHT+IGLXA772pUTt0BVGAPXfaZJ5WaJQ7JivmCyvoMmFNjKdV2TZP6t/5GpHJeWN5LeMnNdr4yhBA== X-Received: by 2002:a17:902:f908:b0:216:69ca:7714 with SMTP id d9443c01a7336-21892980b8dmr2076045ad.11.1734037020131; Thu, 12 Dec 2024 12:57:00 -0800 (PST) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2162e53798asm94019785ad.60.2024.12.12.12.56.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Dec 2024 12:56:59 -0800 (PST) From: Atish Patra Date: Thu, 12 Dec 2024 12:56:55 -0800 Subject: [PATCH 2/3] RISC-V: KVM: Update firmware counters for various events Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241212-kvm_guest_stat-v1-2-d1a6d0c862d5@rivosinc.com> References: <20241212-kvm_guest_stat-v1-0-d1a6d0c862d5@rivosinc.com> In-Reply-To: <20241212-kvm_guest_stat-v1-0-d1a6d0c862d5@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 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 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 Thu Dec 12 20:56:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Atish Patra X-Patchwork-Id: 13905982 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (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 24F2E1BD9EB for ; Thu, 12 Dec 2024 20:57:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734037023; cv=none; b=Fa62J7ka9iuoW+lZcfaXOenMCEzVOmmpQ8t3QVqyvacaAhmZB3tLCkeJKHKePNTNW/Sg4R7w1UeRzou4RZpikS+07TOjIviSEoX4UldfAA8QMBJWzhupO+biztp6Tc6rszTct3BvARtVKQ7bzeSRXV52QVbKYsdd369rM0wWmkQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734037023; c=relaxed/simple; bh=FCqQrI1H1EUUmtYhOFAa3PCJetDrIooxo67JfXKS4QQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Su48b4HtQekngiKifUulTLexjYkLBGwuRpJoApkmpVjxSJwwuUtUzMmWyHRs9MzBBrTEiWJtn6x+1gavjLwtEKBEneVVO9pkHimNaoeJql519PBP2QO8E2VWpSrhToRVqzrSOjCQ5wnPcwQsG+yx67uwGUelk3eUQzfxp8xTwII= 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=FuWLkwKM; arc=none smtp.client-ip=209.85.214.179 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 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="FuWLkwKM" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-216395e151bso7216285ad.0 for ; Thu, 12 Dec 2024 12:57:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1734037021; x=1734641821; darn=vger.kernel.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=avDzzP0/VkNzm+n9LZOGcxgwhkTK2s0/hK10f7P3aeQ=; b=FuWLkwKMWRa8lYhhr2Vlkk6HI2Jr1W25hqxqYoX7pym8A5plCjOAtLDnmGvGM/e5j4 JQGoOSjWg4zIeBw6GkvoNSxf3feA2YjclyFX9GSBFULEOXM/tlgfM7GRzg/ciUyi5YpD K/fja2yQp+BLAQs4Gc7gK3+5HYC0N9BvTFRA53uvibnDMCsFZ4M3zM1deuI4b5VLNMfa 3/j0Aq1cA2b4r4ThHNe2TeARa224t56irRj1du2O1gw9DPixiCMNC9IedJ3JId3rgWq1 90gN/8u/hbN05BMPp9stihix9Jp4NRmmxzjsFEEXIHpf0PqH6x35hvYHxuM2RTzXf662 Qihg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734037021; x=1734641821; 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=avDzzP0/VkNzm+n9LZOGcxgwhkTK2s0/hK10f7P3aeQ=; b=vbwWxpUe0TN/xuZ5A8NlS3OKWc/35HBniQ8DhnhIkFMTueOCyxai6kSHSZA/8FmX2w 8J7t2Bljpmy+EAPi8nucT6zXJb6eWpfzdnScHnhC9DlsSG973n4Xm3gnVTMt3I+I6eMb eWrNtk035mPoCwiHklme1ny7CSqMRCB98u2eNJg/6gHzDhIAIpJ2Yu9LHbs2wjVxKy01 6ZzU0kmBvSWUrMkH8m2JFLA+dP06BhFWXQRzNMoNAAIKC0QugyyM6SE8S/wwSKC089gE ZTdFjQin2ozll9cMa6fmMOVsNep1bikBDxt4VdvB4OYcHDQ8CpqpbUvkfPx/sC3QVQAF tqKg== X-Gm-Message-State: AOJu0Yw+vz94r5oY0MPe/KuMESAZ+DyyINPVHUj7H/YpOxmEt+dV9aa5 gCZ1raC8EKj6/i4sWp0/AjwiRRYSZVDHM4Ts354wtvQG0suc+q+yCPvm1ycnJB8VWUlAbEneQgp C X-Gm-Gg: ASbGnctWLQNNJWhS9hJtQzk3FBSo0q3p/8Id/5r3ughnw1+8ag6+4lpqDTJxngxr8tD IEBrQkForJqX/K/zZb0B5KMIfKA11zjvS3ELLnFbkOrfQrIjKFC1ArbibviahRobrEfn7QViGza sqAAKg7Q+YIijIN0oIUIHOUhuXBzLjKDQfqxBWgoI2RhnWwI5N29ZjHXQEzDCeCsaKYkRAi+965 31d8QDiTn2phERUJZUWOltOZfg5uEPfisAsaJvLf5gPHeLTQThptqiy0Io1z2pWdVS+zQ== X-Google-Smtp-Source: AGHT+IH/wT2q7FeXEvOXyiLAeM+qgkw89AmTDwfUlGcclhBbzmkDVZQZXv4TvB7hgFblbmwcDG7kHg== X-Received: by 2002:a17:902:cf10:b0:216:5cc8:44e7 with SMTP id d9443c01a7336-2178c872406mr71955495ad.25.1734037021033; Thu, 12 Dec 2024 12:57:01 -0800 (PST) Received: from atishp.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2162e53798asm94019785ad.60.2024.12.12.12.57.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Dec 2024 12:57:00 -0800 (PST) From: Atish Patra Date: Thu, 12 Dec 2024 12:56:56 -0800 Subject: [PATCH 3/3] RISC-V: KVM: Add new exit statstics for redirected traps Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20241212-kvm_guest_stat-v1-3-d1a6d0c862d5@rivosinc.com> References: <20241212-kvm_guest_stat-v1-0-d1a6d0c862d5@rivosinc.com> In-Reply-To: <20241212-kvm_guest_stat-v1-0-d1a6d0c862d5@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 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 frequent. Thus, kvm gets a chance to act upon it or collect statstics about it before redirecting the traps to the guest. We can 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. 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: