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: 13920535 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (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 EE16B3EA76 for ; Tue, 24 Dec 2024 21:04:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735074301; cv=none; b=gViaQNnsrHwrcA0HozdOdfJXJmjczQtQBqZXWpItt594qEMhF/oeF7PC+YbviREW//7C+Zh6ukXLVY78BzkGlNCH0ZL/mKBEwT9d0H3c+dsl0OZ3ZjGpRUjTuVBLKN9MHRdgfZHXwxcbKjYj6zfYOwUCwGWyH9yTLy70DUDBBsI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735074301; c=relaxed/simple; bh=ioVvC8eg4qJkoimf2UXWNQMWPpTA0Ez9yimmyjBlhgc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=q1CfYyP7haYBxFgUOB6RFh/ey18OLs2V4dxm1LDlKbK27AsIdPcAWoWeNsD3DH5S5u8AZyTVH+oac1dVJpZLen1jKf7zVptzka4wDpiKa4RxxedYy/skB1crQKHae0xRj+BcKpMD6Ox/0LE7FqB2qw5bp1DtSVynkfqi+fob9fg= 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=k5fIu+Ab; arc=none smtp.client-ip=209.85.214.174 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="k5fIu+Ab" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-216401de828so55240025ad.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=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=K7Pb5Vxtmu1FY9/J9EKk+mhOADazj56h0vKNA3c2QKo=; b=k5fIu+Ab7Z1B/GGb/nXhoC+5TzRZBKJTpAZld0QORT+VRDpFsLr0v1bmtgiWCInuMh yHKZbUfti1Q9a/m0tfc8Q+T21wqKM6ENPt2rEDtjODdVFSS1mp3unl62UTnitZL9lbjK haTQv7wC0ScghKVtDli4ieOj2lI3Ov8UGuOBDL00S47ZJboDu+4kCWrgPHFGfDsOUidl uINj3K5jzdx3ITExGvJBPe/Txyq5otDrlafaiDCMepLAZus/fqpyZ7ZfOtCRbuXoMsXb 5C9rE/uJ6/PbriMAo8llxhgo1znlLvojeCIvCMoU+B+uoj+q9Bz11QmLlOy6Fo1Jg3vu Kclg== 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=qjpkF73/0zc1BfCYrewXbemcD89ad+bDNFXnSBe7j4Bh8hqWEkxm38Ebg6+/ROWL1p hUxJZ+jVb6XuZilqsE2pmAJQSYzvZe1GcWxyqXuXXGzgD/HlyWcs2DkYU8wujG4yrc3C 8U8g20/srXuZRnRNH1/hkpITWQL0yVGFkkebBB6HEbx6ZRrsI1BbwHc0JmYUxZ8m1xrq V7hYy7b7gAYEzWPXYPBCXxAMhNL736JS05AcPVTwusj7882ngOjEENickQ55QANsMcSG cEI6ztVozlutmsGLpiSZ3qdsr+ErLt9kdl9RfATRYQkBUQBz2NjJFFJWFQorQbGIS07V Jjkw== X-Gm-Message-State: AOJu0YyazyreGnzCbnkuCzee3RMXNe46Xz2EHesQ84BE9EqRWyVSuOpP EHJdwGNq1r3V53VmSwGpYc+xGaSurIVU+E5WSw1gazS8cM5QGas1nvr4M0mG/4I= X-Gm-Gg: ASbGnctJiUCFvbIsMFnupebU1+6SDd4mmsgwdzcJfYMEbKx3TVxJIuA03OcdSxpC4RT ZsXtqB6Fx4i5gJvQUzG9Iy0o2j0ZLQuQv8Q8BVKdNSIv/hxlIxC7+aFwlIcBj3lNshZObXQ6b/w VBIqZALT+xolDna3txQ8pruvbyYcvMGXN0yGN1w7QPj7LPgG+WBDBeyCGi/tS4n9P1CJCvfkKun AAoQBi3HYSGBMy8GMdFvA1ddeThZ1Nn/dckawgpUvv5EHJKUC6iLjl1jMGsG9BgJ1LfYQ== 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 Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 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: 13920536 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 CD48B18E373 for ; Tue, 24 Dec 2024 21:05: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=1735074302; cv=none; b=YuTt68aJLZZhufDqh+9TY4jvY4to5sepEQbK51yfuqbTdwt1SBkCE3g/dc4v+uk2qQJO3SCrLgaW1xTDeIUxVqCaRngbCxvFCS2088IQHkpQC4glAJeq/Aq7fHG2ySZ9KF/XGFuUS6/cP7yjaOpUdQmt6Q2WwoHZ+oNulF4T4eY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735074302; c=relaxed/simple; bh=WU2PoBjHti3mLNEWEzbZTbMwK9e9FEOuAWEzakWaEUw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=bQXgzgdLFe2t2OqGj4ARqh4vkQ0n2zA/Z/3vhqDEccMDo34odaeJtV9Rep43xjp51lXRxBNzH2WUh5PogLyRNvGZ4OkUxVuzVJpMq+RxYg1b0t5CJarwMUAKUUHQQSfnxgV6KPTU2CQQHb/lCkzVuosZFApl97uLD+H0gN8Ov3w= 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=tYv8Kdju; 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="tYv8Kdju" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-216401de828so55240125ad.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=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=wyrUQMinm9VfIkz//Zqi5azyB73CtB0XBmp3HQwwuxA=; b=tYv8Kdjulq8Ma/sQYLJcAfSeK/NMisT4/Z3r8lt/8xlPQonlqpK7//khyOg+jMFiJW 0kI7xqwJTBrGYFx51lwchfc9EWc/QU2W7MD188S+Xgdv9kRatT6X1FWMTxv8HqAVR5ex IAEUmdKbDiNeIp662A1zEJ9vmDlU7BqwSIyjHMXYUquus1JaFG+qUxF9mFPlMYUhsOO+ zVlXXPa8Mh3XJP1ShOvrUP7UV/KSEDS8sYZeS5pWltLr0SHV5EPVEdVLl83wXBlPqsMX nOSTZL/P7H3q83iqJJyC8bhdsKGfopnEOeTDR1BuRf+tVMZ4HJ4PYxlTugT+tpiwfDoH 9VXA== 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=tTAcwBaQX9ROJBxQdFk8aN9b+YfaRQhM7qdNiucB8PGUJzRF5ttQFv+sjIeBtZkMQD li4c4+Ywxn1OTcrhupHMz4KqOwg0BqBhUfjZc/YIj4rofAtYIALwVKoI47PyUeCIY4Ft MIcw0dvW5RJss/o2eHltTC10hYxpcxKi0icJKbIHZBkl07ovcGA8ENdazDCEQwxVvrXQ wdsE5DxH9MXO0daz8rJ5EHm/J+XUA6Ael1CJivIFQwpaH38rhTNRuGMSVvZGMnDoqKa6 1lfTtfjugw38GiLzNfnmods8eu7OlPEolVy0PdlyyVC64DnNSQp1wBb8aZ/bBKabpGwc TdDg== X-Gm-Message-State: AOJu0Ywbj46QJrxs4NgzqrTR22JdgSMkGvyJWdaFPXYxMjqCJXyhwg6e Ohfc/nHtWDBm2ipKk2D0Ic0K/FstImy0TEwAA2Kq0g9HNZ2jc5VIfJdwV1BSRwE= X-Gm-Gg: ASbGncsGaYg0Z7B4maR5seigyOMxuRowe4pw0LinB/3IWsN2VZQfgk9wosn+jLr4y+F GKVlVFIc7NLll+D4m6eWdjOvCXd4DaZj5lQRvFw2EOVz6xu9HXdIUOFdPzWz+gGYvjpmxflRWKK SaQd0X03Pb2evXeLmPKg4InRabe3zKOBc/Cd4bMe2L17EQtPUCaME8EBLYXmaXS18X5OeXEoMTF aKv3nL/3PvJqbvAP2TfQ5ADQbiVJR+hG0CdEVuLQGxDXCiC/HtzTGQliEfHiTbRejmMGg== 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 Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 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: 13920538 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 C36A5191F6A for ; Tue, 24 Dec 2024 21:05:01 +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=1735074303; cv=none; b=pY3c20OMY6rHaNO7dHKP2kghCaQiN3bpG8e1WjNtUaKSGJRmO38sH7W3OyoxGPd0uh85yZP3NG9oQ06LKq3Jp7jJjid54ZE8RINjWBjEdavH0iiSiJzQyXjC0rX4pp0HmNYhZnec2vJGj54a+kCHyOg+RZpyKbv9gsnQe9W89Jc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735074303; c=relaxed/simple; bh=gObRHf7nO/muFv+WFkNrw+716r/n4euNFqf4Yqsnx7A=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=tGTa8rDP3VepYv6+0l5QZiD5m+rQLFZYUeYeyqWUwRJXLGZzx8QCuotZysUKrHCmbO2/etwA3vbXck8DA1wP7KM4IuBNRsgVnGp4WzH+wiG8mzNanJZMDczYbGa0JoUjw5HAm8mK9KRoedjIFU2clGh5yb+5wYR25VZOwndY1nA= 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=2cXJ4suo; 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 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="2cXJ4suo" Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-216634dd574so40264295ad.2 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=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=JlUmPcZY4TN/RdMqQ8PaLtNf3W/Wbsw1dLdhHB5EEeA=; b=2cXJ4suoHepRRVOoXVz+o684aHnCR9Clrq1UaccPnclA9db+2iw4QrJWq58BeiGHNB hpaHeL9Kdk+x09i+8SiVKR5cJnTwIo9F4ZGnfKDPOuubxa+rgoXqiAF3TOGy6TLYCM5D 8FR5RgTT5/o9TkJ0fXFey8g9bI9/yZuN1j3y/J2n/9o53Unca+tX1e4ngpmnSe249SrY TiM50EZe6C/fHePLl6TTTHpPSNIHkzWdfLZgziVNDVUVkOobvYG7ll7H4ACaJlNp/bIY A1czJDvP21+QYfrM4uZm0RHkgAsFqWzDXqVh439uDHmA9MMqWtyjGt0CmpurIOT+cNjl 2pzA== 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=Ed0K+uBIdTVskzGjOaH1XuZ9DxgN0cfiaz+qsyHZFJ2Zo4m3SbgMkkBHAdv0ZQgs1M dvJ8OKr5VaRbsagiqHg5xOVvjtRp3joMdEF0Jiiy0D5CsCTg3/s2+JJgt9Z+IQTokDtb dIPVuT3DL1Jg2ES2UpFT8YkA5dnqsEn2geQoDTj7mcLcImVT7gDcl7sGb27ClYCSCKMr ONRSnFTChBNXEukssoPdzXfJHGICnx97w75seDeZ3uUHwYlcGAq2RbKXcA6GoqO+OkNg w91lsRJMPmSB+GXB6O4WUE4iKBwSKJPfYTx8eQnMXS6ayknEIIjQKAN3/6geujN3ueT+ vvlQ== X-Gm-Message-State: AOJu0Yzsx939+EDuP5Yc5WlEg/iln1tMCSJfKjDoAq5AtoUaejgrI5CS yFTttl5fZJPt49HcKnGCfDFVqQl+ZKyVQoqsZbCATDXKgHL+SerZAxZGVndNucU= X-Gm-Gg: ASbGncuba9IkZReyi8dje9+FH8aRoXVgFE4mIUhkLf8Sg0wLQtUNaBjzNELpjR8xe11 Tg6Pe9Fy6bBNlBjZu+UKWnbM8ausrOuc1F6eVwtjOIsDT97BSlGviuFYP3RVBJ86pqylxvlskHF FPkUSyGUjinE/qJKWds/W2AKg3lyaAs2kBfgGCE7cbWr9opaPkBpQSmkFOVDY9/seroxccP6+qy pV2D6txahScUS5x9c4zWflDGW4q53pGxl+0F6xawG13MKcq3E67qydRsgUCyjGj8qKOQg== 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 Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 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: