From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (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 85A10378D98 for ; Tue, 23 Jun 2026 01:22:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782177769; cv=none; b=GuC5COZvOyULjwkQ194GL6qk/isa0EbrMh4hWXpvk86O37nsOJb8A1Bjghy7CFcaZ6l5rixUH73QSz8K9Qm5/mCdQdNVnSx4p4QnvLpS0RUhlP9nEXBEI9qpiBV8/aXfn1KlBbKaA9Leq8fDLcUaG3EcIlyBlB5z3VFEe31d50c= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782177769; c=relaxed/simple; bh=dFe2FI4dOmbgg56+MmHgIOFrB/nhq/z0xCIy1SQ3BBI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=n9imZPjJMXI+WZDTvY2nWHSg9NQqZAodKHN7s9N5N8+nb0ZxxBcHWyFKgmt4E0Oix43aXoetaiLpjapJL/3NBMGd9n266Jy1PIDp5OP/cZSKYM4fDl8ilV2BISmJLdSZtJj4y9E9CLcO2pfD7LcbGzKdWpztz9mNEATdPHkihgk= 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=Rd73HW35; arc=none smtp.client-ip=209.85.215.201 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="Rd73HW35" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-c89956023dbso2742844a12.0 for ; Mon, 22 Jun 2026 18:22:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1782177767; x=1782782567; darn=lists.linux.dev; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=QZV3uH/IPwxj9lEYe1ZUXoqPM2qjMTtjeMRmC3XTGao=; b=Rd73HW352fkp3JhkIg+SRij654BSIkXr8X0nLlVmjdxFYuQju7KQopjGNyr4+Q7MmP +n9X7inU1tqeS5XHPlxAFYUge/eZY0zMP03eq60wzo5e+Y/E5IM3wUWSAhyafyitcZWa g3yB4LufExrIvZOGKd7BMnXW/QzZhyBYLo9B9R8ti5dAYW5DVpAHhyuMfDpxUyRi2h8F gr7agy4ZRBEDsBRWYxIaKM/VeMRxfnptaJBJVp4E+prF42rTf7t6ctqO+An+oND6uVje VFa9WAZVy9fmBPy9brozjpsqj50NLfypPuj9OYD5cOANG8tNoLlKnSCzIh+cY9Y9ADIb 0URA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782177767; x=1782782567; h=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; bh=QZV3uH/IPwxj9lEYe1ZUXoqPM2qjMTtjeMRmC3XTGao=; b=cRF4InqaEfbqDYBrM2gsabo6xoWiJMUXMseV5kuv2hL9YC+0FPh2C/O+ois7UP1Ntn 4y2kL22loIeZUlm2d0jR0+0JrMiXwdKuDwQYr2KDtKQdi9kEwbH4HPKTfp3N6wnYWzG9 Ifo5wmcre8+KSKBrzDzbqFCS6fvtdatD0pv9lGT3qbyqzOaCLbohN6yy2SnJHza3cGfw z8cvXemd7qflE1CkGBVKh3uFqQ2UGsrNotQzG2QsiFZoRivg31RZ6hvk8QigN1kAYIoo m5T0S6X9NS2CNNtg35LH0pUYe4Y/GmSlmZ7Ecmm3dArbqKE2xnzRTO+qZte4JgOIDurq QXjw== X-Forwarded-Encrypted: i=1; AFNElJ/p4oUibkYqb9vmLBm1cTpwiLssUSgQNIh8IEilUqrHXD6XRG/f920iSGsUmvoWLN2CKk93BG4AujgX@lists.linux.dev X-Gm-Message-State: AOJu0YxBKQqDhq5i1PZa2FevLGQtN3CysoMbkwjRIylLIZoKuWt+kBB5 Mylzwa9o18EKH4I7eGobnXU4fVLob5IbwT6ShUDNAlBeKWLoQYOkJ450AwSAaX+DXDIa78YuQOG GeHy3Pw== X-Received: from pgbck12.prod.google.com ([2002:a05:6a02:90c:b0:c8a:b173:5516]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:d045:b0:3b4:904f:c9cd with SMTP id adf61e73a8af0-3bc5181907emr14837899637.6.1782177766311; Mon, 22 Jun 2026 18:22:46 -0700 (PDT) Date: Mon, 22 Jun 2026 18:22:45 -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: <20260618-gmem-inplace-conversion-v8-0-9d2959357853@google.com> <20260618-gmem-inplace-conversion-v8-23-9d2959357853@google.com> Message-ID: Subject: Re: [PATCH v8 23/46] KVM: TDX: Make source page optional for KVM_TDX_INIT_MEM_REGION From: Sean Christopherson To: Yan Zhao 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, 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 , 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 Mon, Jun 22, 2026, Yan Zhao wrote: > On Thu, Jun 18, 2026 at 05:32:00PM -0700, Ackerley Tng via B4 Relay wrote: > > From: Ackerley Tng > > > > Update tdx_gmem_post_populate() to handle cases where a source page is > > not explicitly provided. Instead of returning -EOPNOTSUPP when src_page > > is NULL, default to using the page associated with the destination PFN. > > > > This change allows for in-place memory conversion where the data is > > already present in the target PFN, ensuring the TDX module has a valid > > source page reference for the TDH.MEM.PAGE.ADD operation. > > > > Signed-off-by: Ackerley Tng > > Signed-off-by: Sean Christopherson > > --- > > Documentation/virt/kvm/x86/intel-tdx.rst | 4 ++++ > > arch/x86/kvm/vmx/tdx.c | 11 ++++++++--- > > 2 files changed, 12 insertions(+), 3 deletions(-) > > > > diff --git a/Documentation/virt/kvm/x86/intel-tdx.rst b/Documentation/virt/kvm/x86/intel-tdx.rst > > index 6a222e9d09541..74357fe87f9ec 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 NULL for @source_addr to > > +initialize the memory region using memory contents already populated in > > +guest_memfd memory. > > + > > 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 ffe9d0db58c59..56d10333c61a7 100644 > > --- a/arch/x86/kvm/vmx/tdx.c > > +++ b/arch/x86/kvm/vmx/tdx.c > > @@ -3198,8 +3198,12 @@ 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; > > > > - if (!src_page) > > - return -EOPNOTSUPP; > > + if (!src_page) { > > + if (!gmem_in_place_conversion) > When userspace turns on gmem_in_place_conversion while creating guest_memfd > without the MMAP flag, the absence of src_page should still be treated as an > error. Why MMAP? Shouldn't this be a general "if (!src_page && !up-to-date)"? Just because userspace _can_ mmap() the memory doesn't mean userspace _has_ mmap()'d and written memory. And when write() lands, MMAP wouldn't be necessary to initialize the memory. > Additionally, to properly enable in-place copying for the TDX initial memory > region, userspace must not only specify source_addr to NULL, but also follow > a specific sequence (where steps 1/2/3/7 are required only for in-place copy): > 1. create guest_memfd with MMAP flag > 2. mmap the guest_memfd. > 3. convert the initial memory range to shared. > 4. copy initial content to the source page. > 5. convert the initial memory range to private > 6. invoke ioctl KVM_TDX_INIT_MEM_REGION. > 7. do not unmap the source backend. > > So, would it be reasonable to introduce a dedicated flag that allows userspace > to explicitly opt into the in-place copy functionality? e.g., Why? It's userspace's responsibility to get the above right. If userspace fails to provide a src_page when it doesn't want in-place copy, that's a userspace bug.