From nobody Wed May 14 18:55:45 2025
Received: from mail-wr1-f41.google.com (mail-wr1-f41.google.com
 [209.85.221.41])
	(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 E9CAA24A06C
	for <linux-kernel@vger.kernel.org>; Thu,  3 Apr 2025 11:32:13 +0000 (UTC)
Authentication-Results: smtp.subspace.kernel.org;
 arc=none smtp.client-ip=209.85.221.41
ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
	t=1743679935; cv=none;
 b=SZBJfnRlxdn/oNqDy61bAKu4XfYrDv0C3R2jE6qG03/xn3qO9s2RtxaZQ75ngJBayY6z/LHU6/WXqoOhtHBHRBybaWGwIgr0SBOJdIIwVgx/zaSUyafr5h6DpnN+8uZMaw1S/XkO8m6EsjHX0dcu/No9U8bhVwteka2BerlvhMA=
ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org;
	s=arc-20240116; t=1743679935; c=relaxed/simple;
	bh=jffCTzK2ifUcsRuyU60Vh5EyDsYilh1FFdL2XDp/iQo=;
	h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:
	 MIME-Version:Content-Type;
 b=SH6Z3bMkfuXOoxg1ZOk3dE1AfDj2zVKSJJV0MDhxvj1OddNJCVQ4qzbbRRzVhONDH5SCtMlLxdQ8w9girhVMikI9MQmjzu1o4mHRh6weZj4XUMMgaZ2svlnzKGwxWH6weC3+xRgllNzWMGyWz3v4fMFUveP1z0LYj6IHT6XYGCc=
ARC-Authentication-Results: i=1; smtp.subspace.kernel.org;
 dmarc=none (p=none dis=none) header.from=ventanamicro.com;
 spf=pass smtp.mailfrom=ventanamicro.com;
 dkim=pass (2048-bit key) header.d=ventanamicro.com header.i=@ventanamicro.com
 header.b=QkYw8+K8; arc=none smtp.client-ip=209.85.221.41
Authentication-Results: smtp.subspace.kernel.org;
 dmarc=none (p=none dis=none) header.from=ventanamicro.com
Authentication-Results: smtp.subspace.kernel.org;
 spf=pass smtp.mailfrom=ventanamicro.com
Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key) header.d=ventanamicro.com header.i=@ventanamicro.com
 header.b="QkYw8+K8"
Received: by mail-wr1-f41.google.com with SMTP id
 ffacd0b85a97d-3912a28e629so59555f8f.1
        for <linux-kernel@vger.kernel.org>;
 Thu, 03 Apr 2025 04:32:13 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=ventanamicro.com; s=google; t=1743679932; x=1744284732;
 darn=vger.kernel.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=M+bBWXikjKTcV8ccgilNOkXVbdx4GamWUb2bvML3ssQ=;
        b=QkYw8+K8Y1aOHkQhVnDONrAN5/+jtuBP/vOoVOcOluUnlXSx2nXcK+dr0u9Q0YWrvW
         67sdppl95BJWLI6Tw4gVIyx6EDhRyIgN4TKqCvRBlNcabHVpsjhZ5+Tk6NZUHTPTViiY
         fy92jQwedB2hJ1e292umhzjbdRWI5pM1MGaUfN2Zr55Isrs1BewFInpv69FId6Q9Y/GL
         L/rLTS7HCiUX5w3c5yvpM4FUEMOOwkIU4paO+QZDPh5q1XwTZiWSR5z6VM5ThxCY1UsD
         +LEKzvC/RYKFQPRm/JOUP8IWoNEdOq+yyMtubegsxExpxmXoTi4z3JgA/dahzbUV/KEv
         ehXQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20230601; t=1743679932; x=1744284732;
        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=M+bBWXikjKTcV8ccgilNOkXVbdx4GamWUb2bvML3ssQ=;
        b=xEoTPEZGN0ce6S6idyqu5UmbEuV0i+EDq0wfqFw5mNECJ2WrKhS6FDaOowfBZccTwf
         2xLaaq1qZADrfjR4WMQ6C4UQ9rVSjZROkBmAWTEQ+UiE21X/Ep0x1d5f7ZRf7tB30irl
         BFJuvAAg4kYoVnonYC4gtWeK+mgQIo1AojEunTarzFlMY6ZoZpj+dBGG5YKin+vEE9Ri
         v0SObRB4iHn5hhJVQFBPeJqXYgOOstd3BGCiWPPVxqhK8xV2IVJ02RvVnq8xixj90E0g
         gek4sPHiksp0Pz9NO8KszCuvAzhhhsYwpKsw4GRc2BahDwNbIEb9ImetYuHDhxQCdG4G
         Sflw==
X-Forwarded-Encrypted: i=1;
 AJvYcCXsgocnffTNspwqc3cLzGBpQVZIXs5E/52XJWlkH3m2JNMNLzSMegczYiPtRrel/9Ho8ojXCwyNL6ExGt0=@vger.kernel.org
X-Gm-Message-State: AOJu0YzCV0rTejUMhtaaPC03hV5LpWTq2EKqMh1m3qfU/CSiyCHeFsnO
	DUgEw+08LNiyK/2CMnQ4dmt9jbGzaLAnWDdBHTMzdxBIPho8+8EN3wnVtqAK7oc=
X-Gm-Gg: ASbGnctLuNJ0l+TWyfmBM8WkbndWALH+J2Z/FC5DImlC2XhDrB0QsxdbTFObi27yqjq
	AIL1guGT+nlz8SNBqTcDoMBAeuO+mz9BtJbmona6hG19Hhc+a44f48eBmHrl10Fk2S2gYHhQ78D
	98NUoiehL+wrAy5RGIc8/pneoI27+QnP39y3MyE/3/T0pBxrI0VjGDAn6nxxDrZv4xT+Xx3vJpX
	MgeNroQW6Sb3/C7dwNig+g4pdLIp5J5czffa4sBcTn+NpGOjq7ggfWCgOpmmKlIkYMvA6O7vBA8
	9yPpJHveifPWd8n9m2MqCjpwtSX1/jz4h0ALUcDAkVk4UoEFNDGkuu/I0pXEy2MEkVdL0bIPMB7
	HjQ==
X-Google-Smtp-Source: 
 AGHT+IFKuI/VQYzBaPbrBORZ5Fz1t5HIbnJ+TER1Sbfneisom4goMlCFQ2arAW6gwbRupN7/O0Rx8w==
X-Received: by 2002:a05:6000:144a:b0:391:29ab:c9df with SMTP id
 ffacd0b85a97d-39c2470802cmr3357803f8f.4.1743679932289;
        Thu, 03 Apr 2025 04:32:12 -0700 (PDT)
Received: from localhost (cst2-173-141.cust.vodafone.cz. [31.30.173.141])
        by smtp.gmail.com with ESMTPSA id
 5b1f17b1804b1-43ec366a699sm15541695e9.38.2025.04.03.04.32.11
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Thu, 03 Apr 2025 04:32:11 -0700 (PDT)
From: =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= <rkrcmar@ventanamicro.com>
To: kvm-riscv@lists.infradead.org
Cc: kvm@vger.kernel.org,
	linux-riscv@lists.infradead.org,
	linux-kernel@vger.kernel.org,
	Anup Patel <anup@brainfault.org>,
	Atish Patra <atishp@atishpatra.org>,
	Paul Walmsley <paul.walmsley@sifive.com>,
	Palmer Dabbelt <palmer@dabbelt.com>,
	Albert Ou <aou@eecs.berkeley.edu>,
	Alexandre Ghiti <alex@ghiti.fr>,
	Andrew Jones <ajones@ventanamicro.com>,
	Mayuresh Chitale <mchitale@ventanamicro.com>
Subject: [PATCH 1/5] KVM: RISC-V: refactor vector state reset
Date: Thu,  3 Apr 2025 13:25:20 +0200
Message-ID: <20250403112522.1566629-4-rkrcmar@ventanamicro.com>
X-Mailer: git-send-email 2.48.1
In-Reply-To: <20250403112522.1566629-3-rkrcmar@ventanamicro.com>
References: <20250403112522.1566629-3-rkrcmar@ventanamicro.com>
Precedence: bulk
X-Mailing-List: linux-kernel@vger.kernel.org
List-Id: <linux-kernel.vger.kernel.org>
List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org>
List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org>
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable

Do not depend on the reset structures.

vector.datap is a kernel memory pointer that needs to be preserved as it
is not a part of the guest vector data.

Signed-off-by: Radim Kr=C4=8Dm=C3=A1=C5=99 <rkrcmar@ventanamicro.com>
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
---
 arch/riscv/include/asm/kvm_vcpu_vector.h |  6 ++----
 arch/riscv/kvm/vcpu.c                    |  5 ++++-
 arch/riscv/kvm/vcpu_vector.c             | 13 +++++++------
 3 files changed, 13 insertions(+), 11 deletions(-)

diff --git a/arch/riscv/include/asm/kvm_vcpu_vector.h b/arch/riscv/include/=
asm/kvm_vcpu_vector.h
index 27f5bccdd8b0..57a798a4cb0d 100644
--- a/arch/riscv/include/asm/kvm_vcpu_vector.h
+++ b/arch/riscv/include/asm/kvm_vcpu_vector.h
@@ -33,8 +33,7 @@ void kvm_riscv_vcpu_guest_vector_restore(struct kvm_cpu_c=
ontext *cntx,
 					 unsigned long *isa);
 void kvm_riscv_vcpu_host_vector_save(struct kvm_cpu_context *cntx);
 void kvm_riscv_vcpu_host_vector_restore(struct kvm_cpu_context *cntx);
-int kvm_riscv_vcpu_alloc_vector_context(struct kvm_vcpu *vcpu,
-					struct kvm_cpu_context *cntx);
+int kvm_riscv_vcpu_alloc_vector_context(struct kvm_vcpu *vcpu);
 void kvm_riscv_vcpu_free_vector_context(struct kvm_vcpu *vcpu);
 #else
=20
@@ -62,8 +61,7 @@ static inline void kvm_riscv_vcpu_host_vector_restore(str=
uct kvm_cpu_context *cn
 {
 }
=20
-static inline int kvm_riscv_vcpu_alloc_vector_context(struct kvm_vcpu *vcp=
u,
-						      struct kvm_cpu_context *cntx)
+static inline int kvm_riscv_vcpu_alloc_vector_context(struct kvm_vcpu *vcp=
u)
 {
 	return 0;
 }
diff --git a/arch/riscv/kvm/vcpu.c b/arch/riscv/kvm/vcpu.c
index 60d684c76c58..2fb75288ecfe 100644
--- a/arch/riscv/kvm/vcpu.c
+++ b/arch/riscv/kvm/vcpu.c
@@ -57,6 +57,7 @@ static void kvm_riscv_reset_vcpu(struct kvm_vcpu *vcpu)
 	struct kvm_vcpu_csr *reset_csr =3D &vcpu->arch.guest_reset_csr;
 	struct kvm_cpu_context *cntx =3D &vcpu->arch.guest_context;
 	struct kvm_cpu_context *reset_cntx =3D &vcpu->arch.guest_reset_context;
+	void *vector_datap =3D cntx->vector.datap;
 	bool loaded;
=20
 	/**
@@ -79,6 +80,8 @@ static void kvm_riscv_reset_vcpu(struct kvm_vcpu *vcpu)
=20
 	kvm_riscv_vcpu_fp_reset(vcpu);
=20
+	/* Restore datap as it's not a part of the guest context. */
+	cntx->vector.datap =3D vector_datap;
 	kvm_riscv_vcpu_vector_reset(vcpu);
=20
 	kvm_riscv_vcpu_timer_reset(vcpu);
@@ -143,7 +146,7 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
 	cntx->hstatus |=3D HSTATUS_SPV;
 	spin_unlock(&vcpu->arch.reset_cntx_lock);
=20
-	if (kvm_riscv_vcpu_alloc_vector_context(vcpu, cntx))
+	if (kvm_riscv_vcpu_alloc_vector_context(vcpu))
 		return -ENOMEM;
=20
 	/* By default, make CY, TM, and IR counters accessible in VU mode */
diff --git a/arch/riscv/kvm/vcpu_vector.c b/arch/riscv/kvm/vcpu_vector.c
index d92d1348045c..a5f88cb717f3 100644
--- a/arch/riscv/kvm/vcpu_vector.c
+++ b/arch/riscv/kvm/vcpu_vector.c
@@ -22,6 +22,9 @@ void kvm_riscv_vcpu_vector_reset(struct kvm_vcpu *vcpu)
 	struct kvm_cpu_context *cntx =3D &vcpu->arch.guest_context;
=20
 	cntx->sstatus &=3D ~SR_VS;
+
+	cntx->vector.vlenb =3D riscv_v_vsize / 32;
+
 	if (riscv_isa_extension_available(isa, v)) {
 		cntx->sstatus |=3D SR_VS_INITIAL;
 		WARN_ON(!cntx->vector.datap);
@@ -70,13 +73,11 @@ void kvm_riscv_vcpu_host_vector_restore(struct kvm_cpu_=
context *cntx)
 		__kvm_riscv_vector_restore(cntx);
 }
=20
-int kvm_riscv_vcpu_alloc_vector_context(struct kvm_vcpu *vcpu,
-					struct kvm_cpu_context *cntx)
+int kvm_riscv_vcpu_alloc_vector_context(struct kvm_vcpu *vcpu)
 {
-	cntx->vector.datap =3D kmalloc(riscv_v_vsize, GFP_KERNEL);
-	if (!cntx->vector.datap)
+	vcpu->arch.guest_context.vector.datap =3D kzalloc(riscv_v_vsize, GFP_KERN=
EL);
+	if (!vcpu->arch.guest_context.vector.datap)
 		return -ENOMEM;
-	cntx->vector.vlenb =3D riscv_v_vsize / 32;
=20
 	vcpu->arch.host_context.vector.datap =3D kzalloc(riscv_v_vsize, GFP_KERNE=
L);
 	if (!vcpu->arch.host_context.vector.datap)
@@ -87,7 +88,7 @@ int kvm_riscv_vcpu_alloc_vector_context(struct kvm_vcpu *=
vcpu,
=20
 void kvm_riscv_vcpu_free_vector_context(struct kvm_vcpu *vcpu)
 {
-	kfree(vcpu->arch.guest_reset_context.vector.datap);
+	kfree(vcpu->arch.guest_context.vector.datap);
 	kfree(vcpu->arch.host_context.vector.datap);
 }
 #endif
--=20
2.48.1