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 lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (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 ED62BEB491F for ; Thu, 12 Feb 2026 12:57:11 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.1228921.1534987 (Exim 4.92) (envelope-from ) id 1vqWFT-0000ki-ES; Thu, 12 Feb 2026 12:56:47 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 1228921.1534987; Thu, 12 Feb 2026 12:56:47 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1vqWFT-0000kb-Bp; Thu, 12 Feb 2026 12:56:47 +0000 Received: by outflank-mailman (input) for mailman id 1228921; Thu, 12 Feb 2026 12:56:45 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1vqWFR-0000kO-L9 for xen-devel@lists.xenproject.org; Thu, 12 Feb 2026 12:56:45 +0000 Received: from mail-wm1-x336.google.com (mail-wm1-x336.google.com [2a00:1450:4864:20::336]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 480986e1-0812-11f1-9ccf-f158ae23cfc8; Thu, 12 Feb 2026 13:56:43 +0100 (CET) Received: by mail-wm1-x336.google.com with SMTP id 5b1f17b1804b1-4801bc32725so48666395e9.0 for ; Thu, 12 Feb 2026 04:56:43 -0800 (PST) Received: from [10.156.60.236] (ip-037-024-206-209.um08.pools.vodafone-ip.de. [37.24.206.209]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4836cd7af87sm24982885e9.1.2026.02.12.04.56.42 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 12 Feb 2026 04:56:42 -0800 (PST) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 480986e1-0812-11f1-9ccf-f158ae23cfc8 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1770901003; x=1771505803; darn=lists.xenproject.org; h=content-transfer-encoding:in-reply-to:autocrypt:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:from:to:cc:subject:date:message-id:reply-to; bh=gutz9gHyQftychG/V5j1emZkoWQn85qndlHYdrxJUEc=; b=g4+4NE/gLKs/3kfZHMvNTJjOBBbNZOBtYCIrso/EG+z5KiXkUw5QhdW18flvY8OQTA gowtmFxdJwTOgTsL3A8T2hKpMH85ncFMPIe91yCquq8hBMfFJNNp8vdDV1x4tzooXHNq 69TUeo2NSsb67iUNXnicQ8z1lLNg+mExFMqcvEYZufh9N7ysF+HvCWMfKer/+TBa/E/x 5FhqB3N0rtdY8TWQaqtFcudWOdKb6w3mFQoSqEurjh1Phz9HmkoyqNbT4n1i8xImmn4U G46qaBoETAzUR6UJ6qPQWUbpXxN/JUUWfkDYKty7KtqAQNOgAjgmafVQ7Ot7/uShmceG LHlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770901003; x=1771505803; h=content-transfer-encoding:in-reply-to:autocrypt:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=gutz9gHyQftychG/V5j1emZkoWQn85qndlHYdrxJUEc=; b=sRcEQ5Gn1N+Q/VYnVjfYUz2E0BIVg+hE2PSpy+Hq9tXbcbm5Xd100X+IqksaGn9Bn3 lGueWMCuPteHZZKGGAn4XsIgz0C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477:63a4:88fe with SMTP id 5b1f17b1804b1-483656ae349mr37071205e9.2.1770901003130; Thu, 12 Feb 2026 04:56:43 -0800 (PST) Message-ID: Date: Thu, 12 Feb 2026 13:56:40 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2 2/2] xen/riscv: add p2m context switch handling for VSATP and HGATP To: Oleksii Kurochko Cc: Romain Caritey , Alistair Francis , Connor Davis , Andrew Cooper , Anthony PERARD , Michal Orzel , Julien Grall , =?UTF-8?Q?Roger_Pau_Monn=C3=A9?= , Stefano Stabellini , xen-devel@lists.xenproject.org References: <0e6f450d64ce17f504d73c3429c8e8a9ced0cf06.1770739000.git.oleksii.kurochko@gmail.com> <7cec918c-bd58-4013-9ec2-d43ff7afcd7c@suse.com> <099768df-ec44-454f-8a9e-4897d81309f9@gmail.com> Content-Language: en-US From: Jan Beulich Autocrypt: addr=jbeulich@suse.com; keydata= xsDiBFk3nEQRBADAEaSw6zC/EJkiwGPXbWtPxl2xCdSoeepS07jW8UgcHNurfHvUzogEq5xk hu507c3BarVjyWCJOylMNR98Yd8VqD9UfmX0Hb8/BrA+Hl6/DB/eqGptrf4BSRwcZQM32aZK 7Pj2XbGWIUrZrd70x1eAP9QE3P79Y2oLrsCgbZJfEwCgvz9JjGmQqQkRiTVzlZVCJYcyGGsD /0tbFCzD2h20ahe8rC1gbb3K3qk+LpBtvjBu1RY9drYk0NymiGbJWZgab6t1jM7sk2vuf0Py O9Hf9XBmK0uE9IgMaiCpc32XV9oASz6UJebwkX+zF2jG5I1BfnO9g7KlotcA/v5ClMjgo6Gl MDY4HxoSRu3i1cqqSDtVlt+AOVBJBACrZcnHAUSuCXBPy0jOlBhxPqRWv6ND4c9PH1xjQ3NP nxJuMBS8rnNg22uyfAgmBKNLpLgAGVRMZGaGoJObGf72s6TeIqKJo/LtggAS9qAUiuKVnygo 3wjfkS9A3DRO+SpU7JqWdsveeIQyeyEJ/8PTowmSQLakF+3fote9ybzd880fSmFuIEJldWxp Y2ggPGpiZXVsaWNoQHN1c2UuY29tPsJgBBMRAgAgBQJZN5xEAhsDBgsJCAcDAgQVAggDBBYC AwECHgECF4AACgkQoDSui/t3IH4J+wCfQ5jHdEjCRHj23O/5ttg9r9OIruwAn3103WUITZee e7Sbg12UgcQ5lv7SzsFNBFk3nEQQCACCuTjCjFOUdi5Nm244F+78kLghRcin/awv+IrTcIWF hUpSs1Y91iQQ7KItirz5uwCPlwejSJDQJLIS+QtJHaXDXeV6NI0Uef1hP20+y8qydDiVkv6l IreXjTb7DvksRgJNvCkWtYnlS3mYvQ9NzS9PhyALWbXnH6sIJd2O9lKS1Mrfq+y0IXCP10eS FFGg+Av3IQeFatkJAyju0PPthyTqxSI4lZYuJVPknzgaeuJv/2NccrPvmeDg6Coe7ZIeQ8Yj t0ARxu2xytAkkLCel1Lz1WLmwLstV30g80nkgZf/wr+/BXJW/oIvRlonUkxv+IbBM3dX2OV8 AmRv1ySWPTP7AAMFB/9PQK/VtlNUJvg8GXj9ootzrteGfVZVVT4XBJkfwBcpC/XcPzldjv+3 HYudvpdNK3lLujXeA5fLOH+Z/G9WBc5pFVSMocI71I8bT8lIAzreg0WvkWg5V2WZsUMlnDL9 mpwIGFhlbM3gfDMs7MPMu8YQRFVdUvtSpaAs8OFfGQ0ia3LGZcjA6Ik2+xcqscEJzNH+qh8V m5jjp28yZgaqTaRbg3M/+MTbMpicpZuqF4rnB0AQD12/3BNWDR6bmh+EkYSMcEIpQmBM51qM EKYTQGybRCjpnKHGOxG0rfFY1085mBDZCH5Kx0cl0HVJuQKC+dV2ZY5AqjcKwAxpE75MLFkr wkkEGBECAAkFAlk3nEQCGwwACgkQoDSui/t3IH7nnwCfcJWUDUFKdCsBH/E5d+0ZnMQi+G0A nAuWpQkjM1ASeQwSHEeAWPgskBQL In-Reply-To: <099768df-ec44-454f-8a9e-4897d81309f9@gmail.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On 12.02.2026 12:57, Oleksii Kurochko wrote: > On 2/12/26 11:16 AM, Jan Beulich wrote: >> On 10.02.2026 17:36, Oleksii Kurochko wrote: >>> --- a/xen/arch/riscv/p2m.c >>> +++ b/xen/arch/riscv/p2m.c >>> @@ -1434,3 +1434,126 @@ struct page_info *p2m_get_page_from_gfn(struct p2m_domain *p2m, gfn_t gfn, >>> >>> return get_page(page, p2m->domain) ? page : NULL; >>> } >>> + >>> +void p2m_ctxt_switch_from(struct vcpu *p) >>> +{ >>> + if ( is_idle_vcpu(p) ) >>> + return; >>> + >>> + /* >>> + * No mechanism is provided to atomically change vsatp and hgatp >>> + * together. Hence, to prevent speculative execution causing one >>> + * guest’s VS-stage translations to be cached under another guest’s >>> + * VMID, world-switch code should zero vsatp, then swap hgatp, then >>> + * finally write the new vsatp value what will be done in >>> + * p2m_handle_vmenter(). >>> + */ >>> + p->arch.vsatp = csr_swap(CSR_VSATP, 0); >>> + >>> + /* >>> + * Nothing to do with HGATP as it is constructed each time when >>> + * p2m_handle_vmenter() is called. >>> + */ >>> +} >>> + >>> +void p2m_ctxt_switch_to(struct vcpu *n) >>> +{ >>> + if ( is_idle_vcpu(n) ) >>> + return; >>> + >>> + n->domain->arch.p2m.is_ctxt_switch_finished = false; >> How can the context switch of a vCPU affect domain-wide state? > > It is wrong to have is_ctxt_switch_finished per domain, it should be > vCPU field. > >> >>> + /* >>> + * Nothing to do with HGATP or VSATP, they will be set in >>> + * p2_handle_vmenter() >>> + */ >> Why can this not be done here? > > As VMID should be calculated on VM enter. And I didn't suggest to calculate a new one here. > We can update HGATP and VSATP here with VMID stored before in p2m_ctxt_switch_from(), > but then it is possible when vmid_handle_vmenter() will be called before VM entry > VMID could be changed and it will be needed again to update HGATP and VSATP what > will lead to flushing of VS TLB twice (one in p2m_ctxt_switch_to() and another one > in p2m_handle_vmenter()). Is this a concern resulting from particular logic you expect to appear in the window between context switch and entering the guest, or is this merely an abstract concern? > This is also an answer to ... > >> >>> +} >>> + >>> +void p2m_handle_vmenter(void) >>> +{ >>> + struct p2m_domain *p2m = ¤t->domain->arch.p2m; >> To save yourself (or others) future work, please never open-code p2m_get_hostp2m() >> (applies further up as well, as I notice only now). >> >>> + struct vcpu_vmid *p_vmid = ¤t->arch.vmid; >>> + uint16_t old_vmid, new_vmid; >>> + bool need_flush; >>> + register_t vsatp_old = 0; >>> + >>> + BUG_ON(is_idle_vcpu(current)); >> This is the 3rd use of current - latch into a local variable? >> >>> + /* >>> + * No mechanism is provided to atomically change vsatp and hgatp >>> + * together. Hence, to prevent speculative execution causing one >>> + * guest’s VS-stage translations to be cached under another guest’s >>> + * VMID, world-switch code should zero vsatp, then swap hgatp, then >>> + * finally write the new vsatp value >>> + * >>> + * CSR_VSATP is already set to 0 in p2m_ctxt_switch_from() in the >>> + * case when n->arch.is_p2m_switch_finished = false. Also, there is >>> + * BUG_ON() below to verify that. >>> + */ >>> + if ( p2m->is_ctxt_switch_finished ) >>> + vsatp_old = csr_swap(CSR_VSATP, 0); >> This shouldn't be needed when ... >> >>> + old_vmid = p_vmid->vmid; >>> + need_flush = vmid_handle_vmenter(p_vmid); >>> + new_vmid = p_vmid->vmid; >> ... the VMID doesn't change. Imo you want to drop is_ctxt_switch_finished >> again, handle things normally in p2m_ctxt_switch_to(), and deal with merely >> a changing VMID here. > > ... (check the answer above) > > If it is okay to have potential double VS TLB flush and double update of > HGATP and VSATP when old_vmid != new_vmid then we can do in this way. I think the simpler, straightforward approach should be used initially, with improvements made once a performance issue was actually determined, or once a less ugly (sorry) approach was found. For example, assuming CSR reads aren't overly expensive, it looks to me as if during VM entry - vsatp only needs writing when vsatp.MODE is zero, - hgatp only needs writing when vsatp.MODE is zero or when the VMID needs updating. Jan