From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-f200.google.com (mail-pg1-f200.google.com [209.85.215.200]) (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 5BCB2222580 for ; Fri, 31 Jul 2026 16:11:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.200 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785514310; cv=none; b=nXwocyr+D2M4WuOYtX3eSxZjMj0LHIWis6dsvVAXJfgQXl8soYgJ2FYJZqI48TXxBWw+GXSIwsHkqUh83bAs0wGry0zZWciktJlubPzWc9MEy4c0Wxt51RZpjdDXDD16cBSfPlzVRzfFADf54YZJPfSn7UncMXG/uTrLmHesNZM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785514310; c=relaxed/simple; bh=ZHuGxhrJ3MrlsbogF2sbcHpAeby7swdSHdnCo3MZvUM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=tqI923bdVrmoB/mInRCOGTusi35zbN7bc5cK9khZxFlbzd9rZ89iZfCmrLqWN3L1TDcTevtzZ895RallqMAaFwhe8rgZ5m4XeUDQJIxfM6p0XPxhGWc0K61XLWK+xdMx7ya4XbBsBOKepX6XHLlKi+7b7Vypqhh2ddd836uDVt4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=iBLjfjxY; arc=none smtp.client-ip=209.85.215.200 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="iBLjfjxY" Received: by mail-pg1-f200.google.com with SMTP id 41be03b00d2f7-c88cfe287e1so903220a12.1 for ; Fri, 31 Jul 2026 09:11:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1785514308; x=1786119108; darn=lists.linux.dev; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=bm06E1ea+m3fCfij6trKrrVxSCnF5pTP6S2pONTnQSo=; b=iBLjfjxYxAINCSI+UsCLZzTBeVX3GCPfjFHuRI7/JfGdtL+qh8GtONJdOkjTws4GZG QcePHURO5G5z1cdT4Xgfb+6yuqJHvuhlqcsqbGxPLlYB7ZItcYDBUGg7TLeBa9pPRqLY 7LPdku04qdWSm7MtGRigKxk2kJem9ab+ZqtVgg/4m8zBE8GD3+rdUuM0TTXf3DopHbm2 o3sEiC++SrDcPQYi0GdUwLbOJVxFajMidWJbFXymR3GhyWbb8rk8x37SdLRElXbyqndz F3rVXj6Fol4+3YxDTjYLvkjWB2h0+RgEb04Wt8tyudnZd/8xzb9nLQ6gD4fCfnOX5L+/ SToA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785514308; x=1786119108; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=bm06E1ea+m3fCfij6trKrrVxSCnF5pTP6S2pONTnQSo=; b=HDSspIsm9F+gca9xgfovSpQhA1qliNVOC4JXpW3g+DYDtt8l+7JlzzuWNxqXv3PugF 8d3KpJSBSuBPsaZM0brlssLTjOXq7IpKtKMRQpwFITL9mHxtqx7JvljtQURehFFn4FN0 iTWKDKtAE6odwoc49slWx5D6WQssDN33UWcfOPiI4UFRsQcD9i549RR9J3IE7iVkNSBS CJSq0Ouc/V+VqE3wwHAMfm74E84ZlRmzEDWKgIdwdnc5KQVsnQJWS3yMJj1WPQjzWUhn NMVdwgAhuEdGXa5CUWV7YVp/R9oE5xUBfNmM2ssewOX1OXxy203r0wfirL99/iz3aO1z fy2A== X-Forwarded-Encrypted: i=1; AHgh+Rqi/5ng0cnbUAYSpkAI84hmdemXZcZE/SwevKPVPMHenUrHV7quhIqcnWS1LNMv/SCprQ5Xd1l/FrxM@lists.linux.dev X-Gm-Message-State: AOJu0Ywezfu6+Iln1iCwoASWbLUUzmBHkbwly56QgkBbko4JsGzVUAk+ HJ6vcR3DQT1v+FxvCy6qpIrl6iHFW9yHU3oDLvvn3WiUBgq+xLODlC8XJ14OtBYiPWpAtx2N/4r L1F18Ew== X-Received: from pgl1.prod.google.com ([2002:a63:b01:0:b0:c99:7baf:125f]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:4091:b0:84e:4d6:78fe with SMTP id d2e1a72fcca58-84ee47dc3aemr250368b3a.3.1785514308039; Fri, 31 Jul 2026 09:11:48 -0700 (PDT) Date: Fri, 31 Jul 2026 09:11:47 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: linux-coco@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260728-gmem-inplace-conversion-v9-0-35f9aec2aed2@google.com> <20260728-gmem-inplace-conversion-v9-18-35f9aec2aed2@google.com> Message-ID: Subject: Re: [PATCH v9 18/41] KVM: TDX: Make source page optional for KVM_TDX_INIT_MEM_REGION From: Sean Christopherson To: Xiaoyao Li Cc: ackerleytng@google.com, aik@amd.com, andrew.jones@linux.dev, binbin.wu@linux.intel.com, brauner@kernel.org, chao.p.peng@linux.intel.com, david@kernel.org, jmattson@google.com, jthoughton@google.com, michael.roth@amd.com, oupton@kernel.org, pankaj.gupta@amd.com, qperret@google.com, rick.p.edgecombe@intel.com, rientjes@google.com, shivankg@amd.com, steven.price@arm.com, tabba@google.com, willy@infradead.org, wyihan@google.com, yan.y.zhao@intel.com, forkloop@google.com, pratyush@kernel.org, suzuki.poulose@arm.com, aneesh.kumar@kernel.org, liam@infradead.org, Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Jonathan Corbet , Shuah Khan , Shuah Khan , Vishal Annapurve , Andrew Morton , Chris Li , Kairui Song , Kemeng Shi , Nhat Pham , Barry Song , Axel Rasmussen , Yuanchu Xie , Wei Xu , Youngjun Park , Qi Zheng , Shakeel Butt , Kiryl Shutsemau , Baoquan He , Jason Gunthorpe , John Hubbard , Peter Xu , Vlastimil Babka , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-coco@lists.linux.dev Content-Type: text/plain; charset="us-ascii" On Fri, Jul 31, 2026, Xiaoyao Li wrote: > On 7/29/2026 8:35 AM, Ackerley Tng via B4 Relay wrote: > > From: Ackerley Tng > > > > Update tdx_gmem_post_populate() to handle cases where userspace requests > > "no source page". To handle "no source page", populate (perform > > TDH.MEM.PAGE.ADD) using memory in-place at the target PFN. > > > > Signed-off-by: Sean Christopherson > > Tested-by: Shivank Garg > > Signed-off-by: Ackerley Tng > > --- > > Documentation/virt/kvm/x86/intel-tdx.rst | 4 ++++ > > arch/x86/kvm/vmx/tdx.c | 8 +++++--- > > 2 files changed, 9 insertions(+), 3 deletions(-) > > > > diff --git a/Documentation/virt/kvm/x86/intel-tdx.rst b/Documentation/virt/kvm/x86/intel-tdx.rst > > index 6a222e9d09541..d8d9409120e61 100644 > > --- a/Documentation/virt/kvm/x86/intel-tdx.rst > > +++ b/Documentation/virt/kvm/x86/intel-tdx.rst > > @@ -158,6 +158,10 @@ KVM_TDX_INIT_MEM_REGION > > Initialize @nr_pages TDX guest private memory starting from @gpa with userspace > > provided data from @source_addr. @source_addr must be PAGE_SIZE-aligned. > > +If guest_memfd in-place conversion is enabled, pass 0 for @source_addr > > +to represent "no source page". A source page is required if in-place > > +conversion is not enabled or not supported. > > + > > Note, before calling this sub command, memory attribute of the range > > [gpa, gpa + nr_pages] needs to be private. Userspace can use > > KVM_SET_MEMORY_ATTRIBUTES to set the attribute. > > diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c > > index d1af0a752e97e..e890da5f18aed 100644 > > --- a/arch/x86/kvm/vmx/tdx.c > > +++ b/arch/x86/kvm/vmx/tdx.c > > @@ -3192,7 +3192,7 @@ static int tdx_gmem_post_populate(struct kvm *kvm, gfn_t gfn, kvm_pfn_t pfn, > > if (KVM_BUG_ON(kvm_tdx->page_add_src, kvm)) > > return -EIO; > > - kvm_tdx->page_add_src = src_page; > > + kvm_tdx->page_add_src = src_page ?: pfn_to_page(pfn); > > ret = kvm_tdp_mmu_map_private_pfn(arg->vcpu, gfn, pfn); > > kvm_tdx->page_add_src = NULL; > > @@ -3238,7 +3238,8 @@ static int tdx_vcpu_init_mem_region(struct kvm_vcpu *vcpu, struct kvm_tdx_cmd *c > > if (copy_from_user(®ion, u64_to_user_ptr(cmd->data), sizeof(region))) > > return -EFAULT; > > - if (!PAGE_ALIGNED(region.source_addr) || !region.source_addr || > > + if (!PAGE_ALIGNED(region.source_addr) || > > + (!gmem_in_place_conversion && !region.source_addr) || > > Sorry, I still want to discuss why we only allow in-place PAGE.ADD for > gmem_in_place_conversion only. Though Yan raised this opinion[1] in previous > v8, I'm not quite following her argument. So let me try again. > > I think in-place PAGE.ADD of TDX doesn't need to depend on > gmem_in_place_conversion. There are two use cases actually. > > 1). Userspace sets the gmem page as private, and invokes the in-place > PAGE.ADD by passing a 0 source_addr. Due to patch 16, the target PFN will be > ADD'ed to TD as a all-0 page. > > 2). Userspace sets the gmem page as shared, and writes the desired content > to it. Then converts the page to private, and invokes the in-place PAGE.ADD > by passing a 0 source_addr. The target PFN will be ADD'ed to TD with desired > content. > > For gmem_in_place_conversion == false, only case 1) is possible. > For gmem_in_place_conversion == true, both case 1) and 2) are possible. > > Basically, this patch is changing the behavior of KVM_TDX_INIT_MEM_REGION. > Before, it returns -EINVAL when region.source_addr == 0. Now it wants to > allow the "region.source_addr == 0" case. If we can allow it > unconditionally, why bother adding the restriction to allow it only when > gmem_in_place_conversion == true? As I said[*] in that thread: : Because retroactively adding support for out-of-place conversion is pointless : (requires a userspace update for a feature that's being deprecated), KVM can't : generally support using the source for out-of-place conversion (it's effectively : an obscure zero-page optimization), and IMO rejecting the out-of-place conversion : scenario is valuable for KVM developers, e.g. to help newcomers understand what : exactly is and isn't possible. [*] https://lore.kernel.org/all/akMPZePBdwQlD74H@google.com