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: 13905986 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 E7505E7717F for ; Thu, 12 Dec 2024 20:57:16 +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=4op5XkAZ4qLFiHInjARZySHNJbmMyBjK0bBqcXvIen4=; b=EG9YZKB91WqU87 fnwzOv+XF7dR3bmxV1ZgMPJQ0yvP2l0asjfekyOaUcvVWfjHdA2Dp6B8mjpQRB/TGhog1XDncpXhk KfZLNwapJAW/xDMTfuM8Qwexuce36BX1aT856BGN7sXP02o7TIOyFUYG4Ni99+elMYDAd3L6u5E0f lIffBdpr7+DaeUzOpp3drCCEeRAmnJf7GnwaPjobysoqXs5Risr3VODOTzq0CR1EZ+sKPkg1nAgGz uZER+p9q7Nhrt+mTD0wdyyh04ofhHp2hEbplld4gypybzWisKLhkQF395sGs9zfaIVOuESJhD4064 /ZVHgASL1ACNHf7jO/aQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tLqF6-00000001rvy-1sRj; Thu, 12 Dec 2024 20:57:04 +0000 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tLqF2-00000001rsy-1aK3 for linux-riscv@lists.infradead.org; Thu, 12 Dec 2024 20:57:01 +0000 Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-2165cb60719so9019005ad.0 for ; Thu, 12 Dec 2024 12:56:59 -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=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=d9bocf/te6xqIlX1+3PhmzweHiQ8rAPL41LYUao1zG4=; b=X6KFyyB3yf9fzTj4SN4TaM98YNrvrtfLPtfO5CRRdwbNn0b92/I9ITH7JXBz5OAkE0 4JszwqpA1AvsrQB1BKVE0Q8z01TAeB4tGCNW8n6IfC7U9zBdu3+ZVpyiFQZr2j6yX0oP Iie2FP8hs6KQ+ydVwO+LIE3/ng3M2XGG6ceXrhLkgGRd7+7I47CLJuUcxVr3x+/SBAHO E/OJQFl4o/ahKApLEEZyztFvW0Vdyo9HtX8qV7CNE42BwmDHhrrgXlrVJsa4n43zWwg5 UvtYeJnz8Ao4PI9ER2qGmeUwJF2m6Umpyi0invXbvIvfiaSpKzs1wQWxda1SPmNmWKKF IVMQ== 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=BCsmPA+AynjGRSqguYm4Qu7n/kbe6YE4KO6olBr4udhjHv7yMNEPd6fSJJccD9FuiC wnypre/p2pjNZ8tdHL+btnnfASqc8QcQe9chY3KOOZit2UiaoxWJOAVrIY2GaS2HkJwk wEzjME78lrzSdYvHtIopghd373M7ZFv7Z8nq7PaYkAFWUC0tZDe8TulCpurZ1ULtcT4P VD0/jQOjC+w4LDabd9kXCBeFOldkQMM0usQQ2Qndyj87U1/E5Sxv2GVx6zPsA9LRfiE3 nq2Rhyb8GnrB3mPrepPHrn0DWxE2ie5Cr9J9a3NkJuyh6oOqQxOSbCc8aBm4KUU4eEx7 8vxw== X-Forwarded-Encrypted: i=1; AJvYcCWH3rh5Xjf2AY2VYHLOTZQOlWCMPH47tsQFcgVU86/rIUi/tP2+e8fZqoFYJnEv2DL7wkV9QHN8DSNU1Q==@lists.infradead.org X-Gm-Message-State: AOJu0YxPcxX5ksWosrUuf6dOEZCha6FmdrP8+mtqDYk+zmhvewqyjWFw SemYMSIlyHt3VTSiGOqXOCn7vM2KdwmBToo8tplJrUhjnvHd3j4irc+YKcMgpYk= X-Gm-Gg: ASbGncs70nrMNwk+wxjiX2BXruvK/eg9Nu3J5PZ7t9Sdx6js+hvkMb/zT1SOruC/r6U DtR/wbfli0OSO5PIGXEZSz2jo2EhUBE+TgHCz5jBU2y4XFfywxhWzRip+0sr+DfI3mhVdFUR+cu 5BSjoEzCbLa8uar7EkRakJnvngaP5cye039q9hGDdNOCXjxjfjp5dxIMS4k9Up1P0JyjIDsO6bW aWWJh1IsJdkY/07naKQeCZO0KML2wjjfvLtQgADw3Roqz3vTe5bougC7SNzifbZR7fbAA== 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 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241212_125700_420944_33ACB37E X-CRM114-Status: UNSURE ( 8.67 ) 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: 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: 13905984 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 2CD31E77182 for ; Thu, 12 Dec 2024 20:57:10 +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=TFNCUiGhn8dmk4C3qSZwnP1f1krjym2UdwBAK4gO740=; b=R/Unwx3voUYmgv 6zO1V9tSb6EwRfZjI94SBoewdLlIabCKuVF5/RZWp9eTRzOO/LXHGwwmR4gpJ6hJ6A2rtqbKLrWAH KcFlv42fgddaEpV0qhHMuedngahZMJWYjUvDRqGON/9YZ/niLoTQuBKZDdLImaX1t3a/7glXAVVrO lrKbpM0oWbF0g/Z/NdD18T3fiQN/j1ccZloKuQmPyv8MmHn6wBLh3SRS0uDsemv763Eien3Hw5cSC wtsiigfEK/2KsOSJ5tCdaOUl2iy0E6y2kJyepvG9Vu8ESBZtZ7Gows0kzfULFgrTT5Xio2P6YEDAs cQSXjkEWt7wQnevk0bwQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tLqF7-00000001rwr-1oWo; Thu, 12 Dec 2024 20:57:05 +0000 Received: from mail-pl1-x62d.google.com ([2607:f8b0:4864:20::62d]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tLqF3-00000001rtU-0xkd for linux-riscv@lists.infradead.org; Thu, 12 Dec 2024 20:57:02 +0000 Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-2156e078563so8529315ad.2 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=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=PaPMD+BdCyI5hLi3/kV/S6HLELSeYS1h2xBFfdAEcTQ=; b=XtnL73VBv0KVXSYwThucRk0t+KO9eXvzijPnAz5ROe22pZP/3voz07mnlNW7udJOYK +vs/lnp5+Wzmec3OpPtRBWUHFTRMjyA8LpgaJ71e8YI2q/1It2SOJMcTTRIwm3JCA+a0 O7NBwnchXR7M3ln2NF+uc5pye8GDSGfQrSCFhShsApoPa/xQqPXpZQSK9R0NZLsH9EaD FZsdPYMvnFYkdOhQ1zmO86mww0fPzcUf6ZneF09oNs5+CrfWadaFJSTZEbHnb87zFSMv w6Zj/0LinPwJukK7dnk38Iz7sY/+WnSvlKECrsfQWyKAbucMAk5UHLOiBqlvghEMEPiW W1/Q== 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=Vd5mf8510s2Ty16KgCmx7106WXZ/9YKyBwwN/pQAYuI3WFvUx+XKIQFVhHUn5JwxwL M6wkTlG10345Nqa5Hpp0Zqeqn1qiuekkcF+UfLumf33DlWNtYoUrnd+q08kQr+BMttZC FRslJpmkSgHCmfhSaeLPfuIrjIhtbOoUjZeSvVgGcc3nc/ld88p7IlFlRBQOntQ3LmHs NHe/c1jMlBtcbI0TLev+vvO5N02b83ZcW+DL8qMdro668n+voTlPtSRp8G3GC0uFdGN1 shqSjRWraUmZ83ECqXjN8FVm99fCRyB+F4QQoWxWJ5joeJaKxmdf21dmSJaEdGrwSfzn BxUA== X-Forwarded-Encrypted: i=1; AJvYcCWDwIECqlo9EXnArmwH9Xm7nmgLfVSHhT7IhcM3a8+oGi9cdmWfQZDMJORLEmhWbIS96JSQt7u5xFM1VQ==@lists.infradead.org X-Gm-Message-State: AOJu0YwCccf28YEADN3mwWo3TMuOniPn+3mDgTaI0amPO9mPQJky0js8 RO1SCilc7WxSyZBE3AzuLdy4VZ6tNeGOrzlDKFwGFktTuVfoJk/dCBzvlv0ifnQ= X-Gm-Gg: ASbGncskkIjVtuplvxkfeqfsEQNcso/cHEQLWYT/7hQSOyisRD+Sx9QWArnsdj7QsPc CpgZEOJtRYDgA8BZ848dFEeURZlGCxCbBfWh11FO5DY4DZ0qZztVl9TDbg+3TiPpXBXwgK3ItwG fTobIqiVp+/LvCx3yPiZ0jHMXzp2UQyWwTqVuwgWS1qhL7IfXKrMF12Y0PyPtajpaZZlB2FOpwh ghkjgI4NNG2oJHNtRi9aCMeC36S2eM8NVsZP9unmgKFw4mYpxQDM4TE3UMnnYWmwfESWw== 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 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241212_125701_271053_2C84D046 X-CRM114-Status: GOOD ( 12.42 ) 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 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: 13905985 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 207D5E77183 for ; Thu, 12 Dec 2024 20:57: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=ZWnxKlAyrT9uf66THZNLfDG7/VBkcvjFav86aSll2l0=; b=IVEO486HBZaKuW qbQVZLhCHg1r5XI74qqdmP4lTlfBwvrZx6cygSy2FPYnDZz8tJ3TWscY2EhNy/tCIREZ7t33xBZQf t9pzSSTx4B1AbYy19ugzJ5TrtUk2M6rGYYG9epxgQm1IrsczNgqbyiGDwTgWx43LEP91JunS4ZHuW fjH+K6JU9v6b+5iiE41GRLUr8pVSORTNXVx2Zrb7Z2MUtp0gXmIZfwU44CGOgWsckZH320VRD3h7u 0Db+eyywOojM6lwGwoAZsr2jVs6fFs8IAFUVY1wc6/AyBo5/LRN20losXOCOJpKeRVdT0BZftHwVw wxR/e3KnNlqo6NED+8iw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tLqF8-00000001ryA-2tb2; Thu, 12 Dec 2024 20:57:06 +0000 Received: from mail-pl1-x62b.google.com ([2607:f8b0:4864:20::62b]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tLqF4-00000001ruF-0gIh for linux-riscv@lists.infradead.org; Thu, 12 Dec 2024 20:57:03 +0000 Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-216395e151bso7216255ad.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=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=avDzzP0/VkNzm+n9LZOGcxgwhkTK2s0/hK10f7P3aeQ=; b=JIGt1Azs7bkA/oAbMRDMcQZfGAhpTXuNSsFuFIG/LeBRmxWA0K3JwAySdh6flySoDH vB+OhCpQbt9ylX6fx9W7ceITnhsgUp8hWLxV0S+LZ0dqfl8la3/KGKheRIs0HYpCFw1A 97jcLT70N2csJs1EY77/Uy1KgzZ/l7fXBwwsmfu/FxSrCMaIhlJVZQJMPK9oeObtZeSN F5mxasO53RmCMieKc5q71kJOjMInwUJkqSgEjre+BGFJHqWXkHT8XomQrsBYOSFQ0eYi B2crLgklJha+CwggvCtH78r02RyaOUx9p3TdKb6fgotIUjZQnjwTx14smkSlX1T9sanu +dCA== 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=eS/cvSz4nbxtJTwduFjindFuG3zg2PpRDN+Kqf4JeqAv7i8SEc1n5UxjvCE+2pThOF CDGQnStlMU2bxfuGo/j3a1C84LwUWXupcLHzouvNqwqZaOX34bh5HlQkz4+X0FqCcclW KEoX324Chd4ITGbEDVfXeXLihhxcaAFeNgPjIAU6n4C7lCOB+HLUUrVsLN0khFJxsvg+ NgWMr1IBcNzRHmeCKBnIhqw3N01ld2jorT2bSJqGJjCVNH8f6LwMVKxfDQO/kJMfnkFR +R9waWzii4yAzI2Kzdvq1zxMZoCxZ02ZYOMUS2Od0VWcWBFh3gzsu6dRA3+neFzaA6c5 oCeQ== X-Forwarded-Encrypted: i=1; AJvYcCVUvUTgM57LrImKxdERR5OaUQ84rOa/sL2wKHOUAMNd9tu/u4wzTXs7aYyuM3fnUTemCVATgXWOxqaVFA==@lists.infradead.org X-Gm-Message-State: AOJu0Yzd+x12mvd3VCB4E4EwOfZFvd+xJACDXmydNEPLmqxg983r2+ct ICvLblYic3bIlNkjCQz0vRngqBO7krMcvLLbsZjQ/2w03X2GCzpj/rgXpQe+3KmzNQ8eGCjuxnZ + X-Gm-Gg: ASbGncvrbCcJASWHuYgDRqpYWxbiyqAi4odojd5AbbbrT8UX/5YwgOb4D1r2mPh6EAC 1uzt/Iy1bYYftOiYbYI/0NgMn5D2byalbnHx8NmGfsFJjhTbzf/PKdSZ014ubfk0Y5CAKs1tK92 k0Dr8SW9LgKYjKMuYEFsEAbF4cR0bdHmce8J1jzv9UJhNh7KcDHbsrQE0nzwE7ou1K9XGQyRp5H Rp1EVir7C96pLysZQTtjl9p9JLpLzxcDIEybRjM3HR2ycwXOl628eI6lR9YgH3mhwiGKw== 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 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241212_125702_193846_962DF8F9 X-CRM114-Status: UNSURE ( 9.78 ) 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 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: