From mboxrd@z Thu Jan 1 00:00:00 1970 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.ozlabs.org (lists.ozlabs.org [112.213.38.117]) (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 390C8CCD199 for ; Fri, 17 Oct 2025 00:34:29 +0000 (UTC) Received: from boromir.ozlabs.org (localhost [127.0.0.1]) by lists.ozlabs.org (Postfix) with ESMTP id 4cnm554V5gz3dBX; Fri, 17 Oct 2025 11:33:13 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; arc=none smtp.remote-ip="2607:f8b0:4864:20::104a" ARC-Seal: i=1; a=rsa-sha256; d=lists.ozlabs.org; s=201707; t=1760661193; cv=none; b=col62L3/GrsP4j716QN/12qS1OVYyNFa+IBplPL00nRU54a8awjMkLRP2wlQdIonwvGDRdJexoO5AGDVNwfqV87oH1R3ZI+T3Qnx9k6bQlS7z3D50r7FVIW7X+vIZfiyBU+P8svTYp5aw3HOa6KUojX4qWSXyArD2UDXHocb9BUgBY/A1EgJtfH1OXK6ijGJI9OKOPoZWhn3wQof2ESwsELB50YVJG3qlisnwO3VbFbWJX14CrNGXfqenghhfa9AmutP15lI6Mp23X25VVy1zwIT8pjsekdySb1FvLpUB/TzgArpmyFHRlxZd8/QFXuIUbwxHCk8wEkhXRn8o0GuXg== ARC-Message-Signature: i=1; a=rsa-sha256; d=lists.ozlabs.org; s=201707; t=1760661193; c=relaxed/relaxed; bh=tdDvnnMBi79Itp61RxW2nV56QIHnitcyrtTO64NTOt8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=gEIBO+hkzj8RsJEm5RK8udNz+w+coinbF542rsCVA+kir6kByxo+ZzZcgklzCaVtOt487YfP3FmHmEmX75e08KtiTwwo9rvwb+7phN5O3b5Ai8JF/g937lClS8UloETaCdqqlB5hYJY+PVV0d42MA54L91fldDPca+yqvmlK9FywG9WF+HuGYgI8F7rEc3vfaFQ/Pnk5F0sbaYfxp3QO1ghP9OqkOhKKtKXnyOh053VVwg4zP/NUA0Aokjd6yfG/HyFVxDnFFtt8CEaa06CevLj3Ao+okHgQzw09AppXSTJKYdkYEkgWIKEr6RaX8ePLbvYjnJLkrUS4QeBcYpQVDg== ARC-Authentication-Results: i=1; lists.ozlabs.org; dmarc=pass (p=reject dis=none) header.from=google.com; dkim=pass (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20230601 header.b=HI8WmMe9; dkim-atps=neutral; spf=pass (client-ip=2607:f8b0:4864:20::104a; helo=mail-pj1-x104a.google.com; envelope-from=3x47xaaykdpgxjfsohlttlqj.htrqnsz2uuh-ij0qnxyx.t4qfgx.twl@flex--seanjc.bounces.google.com; receiver=lists.ozlabs.org) smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: lists.ozlabs.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20230601 header.b=HI8WmMe9; dkim-atps=neutral Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=flex--seanjc.bounces.google.com (client-ip=2607:f8b0:4864:20::104a; helo=mail-pj1-x104a.google.com; envelope-from=3x47xaaykdpgxjfsohlttlqj.htrqnsz2uuh-ij0qnxyx.t4qfgx.twl@flex--seanjc.bounces.google.com; receiver=lists.ozlabs.org) Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4cnm54727Rz3d8x for ; Fri, 17 Oct 2025 11:33:12 +1100 (AEDT) Received: by mail-pj1-x104a.google.com with SMTP id 98e67ed59e1d1-33428befd39so3313639a91.0 for ; Thu, 16 Oct 2025 17:33:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1760661191; x=1761265991; darn=lists.ozlabs.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=tdDvnnMBi79Itp61RxW2nV56QIHnitcyrtTO64NTOt8=; b=HI8WmMe9c8wWZg9xIp+PO25txN4UIqEJodGGGJ4E13F3v5psQfZYiA1964KnnI6wRW 6SzSGXKhpGzkBmzoDAxDNWoSZdiA2hCo1nHtgiW2TDALzi6JA2/1DcxyFrCSm+eUxjNa 5fkj7hSpySeWWnvbKwBKjgJHCVMr4hr11oVh4H0EabqCCAO6VVrySGagmQyaNdBOIn6U O1Tr+7+hS6cN1MSQ7xJ92EpxAqRztjNi5sKao/6bRlIQpmB2xl7oMctP9OQiUNe0sp2+ TDe63krTqgZJYF9gdRJvATH/ODWrj6XlIOTSFDpcqz5Fir2EZ+EaS3+jhA3o0bkS8xbj rSfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1760661191; x=1761265991; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=tdDvnnMBi79Itp61RxW2nV56QIHnitcyrtTO64NTOt8=; b=U6jTmTgNt5sNCup5Mc7lM7/PRe0ZteEYCdhHKgDVLdXPBcUnWJBMEPlBCMr2X7d3RD Q2Nhe4qxEQe9J2fNqc48+7/hh+n8ycLcYjb2jo3tumWhjtzTuhzZE4EI9t8gqnesAiyI ujT83U3eGZCjYKlKFrL22skhu+WnKGz72MxZ1rG1IRG9Bg60uIhaGVfl6QEorbVQbPcu OAKfr7ro/xVwpsbJ8O80xohCxI9NcuqrbIZyUL8ecH8KsjuYxFijWUszU4zc98TK7fkp j/cm9M49P31DNUHw80w+pD/VkxZ7FV1kZn7Upko+9CqvsUBchRqKNU38jNjKNTKugeE+ NqjQ== X-Forwarded-Encrypted: i=1; AJvYcCXGwEwUtI2JU52g3uoPJZBMYbSXH+cBj+2xAoUb9ePeUEJB0IqNpl5Qn14PyEAuh6ULSL6NzL2alYkBcok=@lists.ozlabs.org X-Gm-Message-State: AOJu0YyCHc5IPJ18pFgudxLdY6OdgxV96fCzAsyG4fKXocqvTKsWcS1N Ji3D4elVFUDlNGS7meYF2Z7T/xrCFVclVnqmHLMspkwy5pm4LL2u33pHmrmm5k3PlRpYHne2rOk qDWjxbg== X-Google-Smtp-Source: AGHT+IFjbvhuJBf5rM9mXEGAaRcwet/6S3ph7hsVNQrCN7ddhA3ec/NJFjzeh48C6n1kB16/mom5cp7HTgE= X-Received: from pjso17.prod.google.com ([2002:a17:90a:c091:b0:32e:3830:65f2]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:1dc4:b0:330:6f13:53fc with SMTP id 98e67ed59e1d1-33bcf8f870bmr1842078a91.27.1760661191207; Thu, 16 Oct 2025 17:33:11 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 16 Oct 2025 17:32:31 -0700 In-Reply-To: <20251017003244.186495-1-seanjc@google.com> X-Mailing-List: linuxppc-dev@lists.ozlabs.org List-Id: List-Help: List-Owner: List-Post: List-Archive: , List-Subscribe: , , List-Unsubscribe: Precedence: list Mime-Version: 1.0 References: <20251017003244.186495-1-seanjc@google.com> X-Mailer: git-send-email 2.51.0.858.gf9c4a03a3a-goog Message-ID: <20251017003244.186495-14-seanjc@google.com> Subject: [PATCH v3 13/25] KVM: TDX: Fold tdx_mem_page_record_premap_cnt() into its sole caller From: Sean Christopherson To: Marc Zyngier , Oliver Upton , Tianrui Zhao , Bibo Mao , Huacai Chen , Madhavan Srinivasan , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Sean Christopherson , Paolo Bonzini , "Kirill A. Shutemov" Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, kvm@vger.kernel.org, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, x86@kernel.org, linux-coco@lists.linux.dev, linux-kernel@vger.kernel.org, Ira Weiny , Kai Huang , Michael Roth , Yan Zhao , Vishal Annapurve , Rick Edgecombe , Ackerley Tng , Binbin Wu Content-Type: text/plain; charset="UTF-8" Fold tdx_mem_page_record_premap_cnt() into tdx_sept_set_private_spte() as providing a one-off helper for effectively three lines of code is at best a wash, and splitting the code makes the comment for smp_rmb() _extremely_ confusing as the comment talks about reading kvm->arch.pre_fault_allowed before kvm_tdx->state, but the immediately visible code does the exact opposite. Opportunistically rewrite the comments to more explicitly explain who is checking what, as well as _why_ the ordering matters. No functional change intended. Reviewed-by: Rick Edgecombe Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/tdx.c | 49 ++++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 28 deletions(-) diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index 6c0adc1b3bd5..c37591730cc5 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -1605,29 +1605,6 @@ static int tdx_mem_page_aug(struct kvm *kvm, gfn_t gfn, return 0; } -/* - * KVM_TDX_INIT_MEM_REGION calls kvm_gmem_populate() to map guest pages; the - * callback tdx_gmem_post_populate() then maps pages into private memory. - * through the a seamcall TDH.MEM.PAGE.ADD(). The SEAMCALL also requires the - * private EPT structures for the page to have been built before, which is - * done via kvm_tdp_map_page(). nr_premapped counts the number of pages that - * were added to the EPT structures but not added with TDH.MEM.PAGE.ADD(). - * The counter has to be zero on KVM_TDX_FINALIZE_VM, to ensure that there - * are no half-initialized shared EPT pages. - */ -static int tdx_mem_page_record_premap_cnt(struct kvm *kvm, gfn_t gfn, - enum pg_level level, kvm_pfn_t pfn) -{ - struct kvm_tdx *kvm_tdx = to_kvm_tdx(kvm); - - if (KVM_BUG_ON(kvm->arch.pre_fault_allowed, kvm)) - return -EIO; - - /* nr_premapped will be decreased when tdh_mem_page_add() is called. */ - atomic64_inc(&kvm_tdx->nr_premapped); - return 0; -} - static int tdx_sept_set_private_spte(struct kvm *kvm, gfn_t gfn, enum pg_level level, kvm_pfn_t pfn) { @@ -1638,14 +1615,30 @@ static int tdx_sept_set_private_spte(struct kvm *kvm, gfn_t gfn, return -EIO; /* - * Read 'pre_fault_allowed' before 'kvm_tdx->state'; see matching - * barrier in tdx_td_finalize(). + * Ensure pre_fault_allowed is read by kvm_arch_vcpu_pre_fault_memory() + * before kvm_tdx->state. Userspace must not be allowed to pre-fault + * arbitrary memory until the initial memory image is finalized. Pairs + * with the smp_wmb() in tdx_td_finalize(). */ smp_rmb(); - if (likely(kvm_tdx->state == TD_STATE_RUNNABLE)) - return tdx_mem_page_aug(kvm, gfn, level, pfn); - return tdx_mem_page_record_premap_cnt(kvm, gfn, level, pfn); + /* + * If the TD isn't finalized/runnable, then userspace is initializing + * the VM image via KVM_TDX_INIT_MEM_REGION. Increment the number of + * pages that need to be mapped and initialized via TDH.MEM.PAGE.ADD. + * KVM_TDX_FINALIZE_VM checks the counter to ensure all mapped pages + * have been added to the image, to prevent running the TD with a + * valid mapping in the mirror EPT, but not in the S-EPT. + */ + if (unlikely(kvm_tdx->state != TD_STATE_RUNNABLE)) { + if (KVM_BUG_ON(kvm->arch.pre_fault_allowed, kvm)) + return -EIO; + + atomic64_inc(&kvm_tdx->nr_premapped); + return 0; + } + + return tdx_mem_page_aug(kvm, gfn, level, pfn); } static int tdx_sept_link_private_spt(struct kvm *kvm, gfn_t gfn, -- 2.51.0.858.gf9c4a03a3a-goog