From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9819427A45C for ; Thu, 29 Jan 2026 15:27:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.180.131 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769700461; cv=none; b=NcETt6Me7uzLzorTIFUkB1glXuXxUA2NV0PsLp6wQHemm1bY0wjFpM058bQivgKJbcNy2zA1XMsyeBcJldsA1RY147xwrGsuh9VWgUcyxrAB1CbB0FemIxEtxBfZE3GyZelDPBFC05UvWqfuMcJZd+zDHofnYx+CcDnd4ndxwno= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769700461; c=relaxed/simple; bh=hr/f1/qoxYAV6yNHyyt9Ytzb2s7niqZHq8CGNFSla/E=; h=Mime-Version:Content-Type:Date:Message-Id:To:From:Subject:Cc: References:In-Reply-To; b=mdhIVjGW0XKFdgJjwbz74jA8ME8DKw/DA3C71ErUgQWTxEAdIjetJZ1l268xXHNMAioEEIUj4x4/TacFT7iXyN5snsahdi/fCtXhZm/jS7SFWY0yucREog5NxQa91G6OkSW8pl2cxPF4HanBTY4QznyuOzQjoy3jbSt5OklBf1U= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com; spf=pass smtp.mailfrom=oss.qualcomm.com; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b=lm+Pl4lw; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b=jCFmthxS; arc=none smtp.client-ip=205.220.180.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b="lm+Pl4lw"; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b="jCFmthxS" Received: from pps.filterd (m0279870.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 60TAPfFB3642239 for ; Thu, 29 Jan 2026 15:27:38 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= ScOC4GuemLF+vD6HqYRKrExatm0/7pF+N+hUpuGlD6w=; b=lm+Pl4lwNanlF8MG x05/DQHvA9ngneLxFBxDmTgdaEi2lEc9+nA+8s9bmWNXByRIYIVPCErqeYr5CYxD EhuAN+7e5w20mADS3ka/V9dwiShZl6g//O1JD4+t1D1q3dBCOBeIPst1Sltynwf5 plcXSMY0k1wG1E8KiAEoa2SoeuInJ0n2JJoqfdXZi1mZmFmhgjhob6nteDNrdwtl m+XeoiHKr5pjeWJewDgTtjuGc9ro4f01UwfaKq7UZDFRLs4zjS5EmvpwfdCkbWdY ppUmw3mo+W01rOKceE7Zf0mUu2mvzK7SDZZG/VIpp19mvArQMm1PMQHkE8PSbnd8 Js1agQ== Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 4c05sr0wmc-1 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NOT) for ; Thu, 29 Jan 2026 15:27:38 +0000 (GMT) Received: by mail-qk1-f200.google.com with SMTP id af79cd13be357-8c6a87029b6so313921785a.1 for ; Thu, 29 Jan 2026 07:27:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oss.qualcomm.com; s=google; t=1769700458; x=1770305258; darn=vger.kernel.org; h=in-reply-to:references:cc:subject:from:to:message-id:date :content-transfer-encoding:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=ScOC4GuemLF+vD6HqYRKrExatm0/7pF+N+hUpuGlD6w=; b=jCFmthxS7ZwbHFiNLo/dBK8Qu8TDq5vDkThr8blrgYGLDj7qiRpsMHKEzH7QKu+lHb ByCtgO7qUyhXIaa+80jCI+XVKe4UIT2sE9zzfGqUiLlBzgSNCDvXr3X6HioGdbMxWR3c RXIJeBo13cT0tVbEb7yHO7Fyd9u9nCcrjnyZCWXLe5YSv0dMUTv/z9mInbqyA09qDClw AOLvRX9n+tQBGVnOAZsfj99gtpeM8QKJvJ3UgYVHH0DKFLVqdMikES3zJ9oZf/pUwr8D 1x7vjQLCY4cNqA44uGjjgZlKbj2JQhTWZrYtPmSIGtTcxkt3GdQDbAn9nJEmS5KENrid +5ww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769700458; x=1770305258; h=in-reply-to:references:cc:subject:from:to:message-id:date :content-transfer-encoding:mime-version:x-gm-gg:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=ScOC4GuemLF+vD6HqYRKrExatm0/7pF+N+hUpuGlD6w=; b=C/8LriT9S1axaPft8TGi3UYC64ShTXUUzRlqg5pOEH7t88JJBTwvLcA/5AbC/gE/Lw GY7L05/5trFBeluGSod25ZBN7OMg3IDbJzp7yCVbeWpJiP3kR7ogH7iisbNN26ETTjtF GdzWOC6nZQDLKuyrRVjVwEDpfwc0n4SmTqVoa2C36cGK2V4fEu5uNKiqv0zZDpA4I2P/ wl8iIlcYxKgk1CvNSjkTz5Stttwd7sWARLSCyIlbh1OBNAXsheZSYjGq4e3F+/xWSjDx QwdGwZRpAf6nCB4TNv9oiAvRVDSWDwh5i0duOKgcq8N5JpvEyEXvYumYcMUOTa1lURuw 83vg== X-Forwarded-Encrypted: i=1; AJvYcCX7Rm+KEnv1ajDOdzs9stZ0B/W9pUJYyjG8pXHIHuCac0ljWOn4cvIhHrWN8BP3gzZoUe8VTranYAAGtXM=@vger.kernel.org X-Gm-Message-State: AOJu0YwOzjYI+f4Uz0eRYbr+NkjV+Gr5b/8dyRj2wnibGitgXrM3Tr2d oxoZCvLGsYvdALzx3LNUxVyaY3n8T7ftHvuyWFg5t4fqY785ESNOMT3MMGUXN/goPFSxMy0OfWI MLe7a7zjbO2wkJIwLXfDDAxfx9RHdBMSbjimUhR8pbtImIDZ3BHt/Tj29F+qdhdOfcPo= X-Gm-Gg: AZuq6aL1q7mHLFbeurSyHtO3EyiKGk5sRHClwnFFAAivczKWzVbp8pPGeP+afnx/sLT NCluNSSrMhKQQuuOWbyMIhwd73fuUSfpPcduQ8JBP1G+HVVr0vEYNgtlcHbrzenY2FeeoXOyveJ IDpXUlA2zHvscwT4HERwP/HYeGOGNz66PYFpO8TaJgoE3MbbX0Ynd21KatiR9D5tUXF2cehA8cX hCoKH9GhUUYWim9G6X3t7rhy3SzrWKd0eI/f480CbivsMda9MjR4uCT+S0d7TE+fJQbSkUdd5Iv woNBHGa3b9z49jz6lflNU6Nk0IIOMN/FM8nWdtZ7SO5NzaLTsWdKpnFL83ebBcA78qeu6p5Gxtz 46kkT72MxonA0x4D6FtpJgWi+sahRCJvaXwKGZTmtHhm5d/Ep X-Received: by 2002:a05:620a:bc8:b0:8b2:eefb:c8a6 with SMTP id af79cd13be357-8c70b92cc4emr1250374785a.88.1769700457602; Thu, 29 Jan 2026 07:27:37 -0800 (PST) X-Received: by 2002:a05:620a:bc8:b0:8b2:eefb:c8a6 with SMTP id af79cd13be357-8c70b92cc4emr1250369285a.88.1769700456838; Thu, 29 Jan 2026 07:27:36 -0800 (PST) Received: from localhost (ip-86-49-253-11.bb.vodafone.cz. [86.49.253.11]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-435e131cefdsm15219531f8f.23.2026.01.29.07.27.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Jan 2026 07:27:36 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Thu, 29 Jan 2026 15:27:35 +0000 Message-Id: To: , , , , , , , , , From: =?utf-8?q?Radim_Kr=C4=8Dm=C3=A1=C5=99?= Subject: Re: [PATCH v3 1/2] RISC-V: KVM: Support runtime configuration for per-VM's HGATP mode Cc: , , , , , , References: <20260125150450.27068-1-fangyu.yu@linux.alibaba.com> <20260125150450.27068-2-fangyu.yu@linux.alibaba.com> In-Reply-To: <20260125150450.27068-2-fangyu.yu@linux.alibaba.com> X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwMTI5MDEwNiBTYWx0ZWRfX1r4C4q053fAy S0I9kI0lZKoFMHJjOwjxOkzDFoVi9yxeSVroJobD4ar+w2dyBuQ1ZZfOLlPEb4GC8762fyBmZXh ss3PdfrotIxUhEg504z+/HW/EMrA94GDq3bGXzcVgCR8MTlzpm+PqsM0+6ueWwt2NmrjAuqysNY Z2mQsshoirVws9l7IleytFaPjbm1fs1kvTOyaxjHfgSFxL4gxXMuAWjZ4rYfbOrdwH4SxVuID3O 7BHaAzlISoVhsECRejeIfioccg3HuGr7ljiPQxgnQR0rLZIeWOpuPPzcPxJH8wq2Xf4lyBPC+Ko 0HBbL2MDzLziHgXxGVb3tZW+pvbF9BhYyTCCaJLrrbgtv8SQnMprm2nukxAoQOyuZuR8nuP1Bk2 oNxHBr4VBdCb9EunMRS6rjwQYFO0wchOd+7N8Ol5jFQJBkuLqvkSwyySSYRQLRlJcgqMSQL6ZYg wetitn6gNLjMwc2E9Jg== X-Proofpoint-ORIG-GUID: ROCZWrDDsjCViggw2sxWcIBDa0kOVb6K X-Authority-Analysis: v=2.4 cv=UsJu9uwB c=1 sm=1 tr=0 ts=697b7c6a cx=c_pps a=hnmNkyzTK/kJ09Xio7VxxA==:117 a=HFCiZzTCIv7qJCpyeE1rag==:17 a=IkcTkHD0fZMA:10 a=vUbySO9Y5rIA:10 a=M51BFTxLslgA:10 a=s4-Qcg_JpJYA:10 a=VkNPw1HP01LnGYTKEx00:22 a=SRrdq9N9AAAA:8 a=f4C2yWSDeSD6iuI_DtIA:9 a=QEXdDO2ut3YA:10 a=PEH46H7Ffwr30OY-TuGO:22 X-Proofpoint-GUID: ROCZWrDDsjCViggw2sxWcIBDa0kOVb6K X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1121,Hydra:6.1.51,FMLib:17.12.100.49 definitions=2026-01-29_02,2026-01-28_03,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 suspectscore=0 lowpriorityscore=0 impostorscore=0 phishscore=0 clxscore=1015 spamscore=0 malwarescore=0 adultscore=0 priorityscore=1501 bulkscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2601150000 definitions=main-2601290106 2026-01-25T23:04:49+08:00, : > From: Fangyu Yu > > Introduces one per-VM architecture-specific fields to support runtime > configuration of the G-stage page table format: > > - kvm->arch.kvm_riscv_gstage_pgd_levels: the corresponding number of page > table levels for the selected mode. > > These fields replace the previous global variables > kvm_riscv_gstage_mode and kvm_riscv_gstage_pgd_levels, enabling different > virtual machines to independently select their G-stage page table format > instead of being forced to share the maximum mode detected by the kernel > at boot time. > > Signed-off-by: Fangyu Yu > --- > diff --git a/arch/riscv/include/asm/kvm_host.h b/arch/riscv/include/asm/k= vm_host.h > @@ -87,6 +87,22 @@ struct kvm_vcpu_stat { > struct kvm_arch_memory_slot { > }; > =20 > +static inline unsigned long kvm_riscv_gstage_mode(unsigned long pgd_leve= ls) > +{ > + switch (pgd_levels) { > + case 2: > + return HGATP_MODE_SV32X4; > + case 3: > + return HGATP_MODE_SV39X4; > + case 4: > + return HGATP_MODE_SV48X4; > + case 5: > + return HGATP_MODE_SV57X4; > + default: > + return HGATP_MODE_OFF; I think default should be an internal error. We can do "case 0: return HGATP_MODE_OFF;", or just error it too since KVM shouldn't ever ask for mode without protection anyway. > diff --git a/arch/riscv/kvm/gstage.c b/arch/riscv/kvm/gstage.c > @@ -319,41 +321,48 @@ void __init kvm_riscv_gstage_mode_detect(void) > +unsigned long kvm_riscv_gstage_gpa_bits(struct kvm_arch *ka) > +{ > + return (HGATP_PAGE_SHIFT + > + ka->kvm_riscv_gstage_pgd_levels * kvm_riscv_gstage_index_bits + > + kvm_riscv_gstage_pgd_xbits); > +} > + > +gpa_t kvm_riscv_gstage_gpa_size(struct kvm_arch *ka) > +{ > + return BIT_ULL(kvm_riscv_gstage_gpa_bits(ka)); > +} Please define these two functions as static inline in the header files. They used to be just macros there, so it'd be safer not put LTO into the equation. > diff --git a/arch/riscv/kvm/main.c b/arch/riscv/kvm/main.c > @@ -105,17 +105,17 @@ static int __init riscv_kvm_init(void) > return rc; > =20 > kvm_riscv_gstage_mode_detect(); > - switch (kvm_riscv_gstage_mode) { > - case HGATP_MODE_SV32X4: > + switch (kvm_riscv_gstage_max_pgd_levels) { > + case 2: > str =3D "Sv32x4"; > break; > - case HGATP_MODE_SV39X4: > + case 3: > str =3D "Sv39x4"; > break; > - case HGATP_MODE_SV48X4: > + case 4: > str =3D "Sv48x4"; > break; > - case HGATP_MODE_SV57X4: > + case 5: > str =3D "Sv57x4"; > break; > default: > @@ -164,7 +164,7 @@ static int __init riscv_kvm_init(void) > (rc) ? slist : "no features"); > } > =20 > - kvm_info("using %s G-stage page table format\n", str); > + kvm_info("Max G-stage page table format %s\n", str); Fun fact: the ISA doesn't define the same hierarchy for hgatp modes as it does for satp modes, so we could have just Sv57x4 and nothing below. We could do just with a code comment that we're assuming vendors will do better, but I'd rather not introduce more assumptions... I think the easiest would be to kvm_riscv_gstage_mode_detect() levels in reverse and stop on the first one that is not supported. (I'll reply with a patch later.) Thanks.