Received: by 2002:ab2:69cc:0:b0:1fd:c486:4f03 with SMTP id n12csp22604lqp; Mon, 10 Jun 2024 16:41:09 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX52cNMvb32L5o+JIJ1T/tSV/0CKiu2RtWJoijlqafdZTNBImwhH4g17a0hiy+STi8d/TL+l4nRsTjky5PEefJFXqXAJiZHONnkCaCFbQ== X-Google-Smtp-Source: AGHT+IEcS+BbWEhqlkhh6fyLdHf8cOZI2l/KnrS9R+y0Pl5x+Zs/ekWzbTNZ5dbUWsH5VtZDwthq X-Received: by 2002:a0d:e2ca:0:b0:62c:e8a2:305 with SMTP id 00721157ae682-62ce8a203a2mr73919157b3.41.1718062869116; Mon, 10 Jun 2024 16:41:09 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718062869; cv=pass; d=google.com; s=arc-20160816; b=YIBsuw4JlCKajtyCEArHuySGgRdqhz6ILD0Pjxkvn0tlFjCYdo0yatWbpiM0C2at6X r0uT/NcrXn+Jt+Cwwg6YejnT4RyVIavbF2g1Yqb+Of07teIY/YY3xJMOxa1pJCs3pxY/ 0Fv7WTBreYuTp96aYbESlEOW3iuEmhDwF8lenxZbe4pbuKdZAzOjLZrLnoOy0WSfF0am YcE7gFhdk6HsYvgWYJ+Zk4vqUy4AjvKs6k7pBV6BzqFdj6ZqmdBbandoiCQa+wPxa6tL GEjIp1MOETvpUZLIHSlZcKcGU3HNWqVrq8xL77cZQFacLnNzGv3xCLbvFNAv83NoeCOF bz5w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :in-reply-to:date:dkim-signature; bh=w0zaitEACw+zYcor2/0Rz63A8WWn/XjClt8MhVkc46Y=; fh=Kz5X10h5kFlEdstOAH7ThqOk9+JMntPNdDy6j89vbWQ=; b=x8BqGgID/BabsBGiVO7emoU5DjPQpvN8yl4BvUZ3k+XOL2oz0//sxYXtFDe7wfHwhU A0dj88v6nfVqM/triaB5VPckcsStipvpdytXxtYGYeAG1tKHzVAowc09dUlQgcvrflkt eu3A8agC6/ESTBX7w+AUgWucAxQp9hyoMSYxoUTKl7Zu4zf9P0lPeCu2/zr+QjAE1hAL ToYJ7q+O826neOx+sYWEravNgqd/jgPvQFxgZYuBihNEbQ2GrqKGgZH7nTSwWFZs03Px 0rhUK5RdYdKa/iczmybj/RetNFM78h80c+eMY/gxQR6fPMGj+EPzS+wi4mkn+6Nbd2d1 erFQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=nLCZN5Ju; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-209065-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-209065-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id 6a1803df08f44-6b086364a67si19650776d6.292.2024.06.10.16.41.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jun 2024 16:41:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-209065-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=nLCZN5Ju; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-209065-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-209065-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id C34B11C2102B for ; Mon, 10 Jun 2024 23:41:08 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8B65115253D; Mon, 10 Jun 2024 23:40:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="nLCZN5Ju" 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 169BA3C0C for ; Mon, 10 Jun 2024 23:40:54 +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=1718062856; cv=none; b=RtkSPsFaw/3oXigl059ScSV7GMJp+U1TBbTRPvUDFPAIh1ExZ7hndh4MgF2BuTytatTA5KvhhtzAODtsqMJM22BOPa+MkVQBwPfyGh5pP+6F08K1aKe8ecpY0P7U+iNKcyBpYEzIGZ8pGq6eccMVKfyEj3YOfjw0z14F880x5EE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718062856; c=relaxed/simple; bh=az8iAxY1+IX8BluLFMbVS7wpU2m8WrFvmMATqq5V84E=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=nA+9YeZr23YmKPup0quNO9tbZ0T9vrJqWPbsdQgq9Sxc45wlbXKxc7Aa5KKbv5350/jJ/YtDsxdH0sz9jfxC1dCvPxwzzUPS5gH07I6bPiffDzk+voFQpwST6uIScOZCcCPbDTfXqLaedDATTlHaaz8sNm5JaQ/R/pM4kJOiGRY= 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=nLCZN5Ju; 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 Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-6f18acad837so1085079a12.3 for ; Mon, 10 Jun 2024 16:40:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1718062854; x=1718667654; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=w0zaitEACw+zYcor2/0Rz63A8WWn/XjClt8MhVkc46Y=; b=nLCZN5Ju+6AUxoZytuEEQXuKGR8RQKZT7MZ/agBSpjTVDm90ya1fvy8QzdfWQFBH1r 0NbM3TtEBvq2xy46SZPng+LSWMB/Eha7NQms6fUgudiK07nlQ+5ndyWi+Jerz9pwZk++ TMXGOHyvx2hLXydbm1KcbcPH3YgMuFUeYfUSHUCr+5W7tqtt+s0IdqPiKy4rpeLaRMih cFYzQwsDyHw2bGIrsm2zYZ9XRo0IAXjD2LulOtuhfDjPmn2BryLZ6sZUhoyTMUWtCUtM lQbTwuiYEZ0HyHP8YbzDcW1FkO/pkBus0F8riVm0eYakzMSRRe6KJJ9jVRKko3e7Wbts DywA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718062854; x=1718667654; h=content-transfer-encoding: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=w0zaitEACw+zYcor2/0Rz63A8WWn/XjClt8MhVkc46Y=; b=NROzJo34GwAjBDpZduwVeQyV2rWZTmoGMOdTIhSGqTJDS3A0b9ZFceSC82hcNHByWy ynHkXKMzmx3LMvpxzgQgQcLgyHcNDQWvAoquKPwQF+2avm4LMsciEk/LsKeSoO71dXvH Gc6W+rZ4/OMOqsQXG+JfuHmWlPpHmmPjeGPwejw0C53JXCz2CAE12nYaiRzsm77pvt6i xPgo79ajAAfX81OPzwq0N77Tysk/gRUgrfi8wTBm/yKaZfXlY4MkGdwsaIOZw5JnB2S4 R+UBYxGPTVZ/zV7RbF1ER35Usu7mW1oEtY1YmVoTbdEpbe213KsJR5jAUQoyuuNan3Hu 6EkQ== X-Forwarded-Encrypted: i=1; AJvYcCVs3F1vapjE730MMBFbqqaMBm1gMessK3Tp6OwXd+jpvcVpBvNIrhR72MngIefDoCYr2ymd50c4Jqf2F+NzlJsXoWWdBRQ1NKEac0GV X-Gm-Message-State: AOJu0YwizEdMOfKXZ5ORqipWkQLlNsIK9/Hw4F3OwtB7ZipBLVWp6hap LNycg29ITj47VVx+BIOeuV+zobZdzBIan2r4+d5YY4kw4AOJq/f9het/rXXRP1KdXleU1LEJzA7 bxw== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a02:482:b0:6e5:ef07:5922 with SMTP id 41be03b00d2f7-6e5ef075a4cmr24462a12.1.1718062854162; Mon, 10 Jun 2024 16:40:54 -0700 (PDT) Date: Mon, 10 Jun 2024 16:40:52 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240423235013.GO3596705@ls.amr.corp.intel.com> <20240425011248.GP3596705@ls.amr.corp.intel.com> Message-ID: Subject: Re: [PATCH 09/11] KVM: guest_memfd: Add interface for populating gmem pages with user data From: Sean Christopherson To: Paolo Bonzini Cc: Isaku Yamahata , linux-kernel@vger.kernel.org, kvm@vger.kernel.org, michael.roth@amd.com, isaku.yamahata@linux.intel.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable On Tue, Jun 11, 2024, Paolo Bonzini wrote: > On 6/10/24 23:48, Paolo Bonzini wrote: > > On Sat, Jun 8, 2024 at 1:03=E2=80=AFAM Sean Christopherson wrote: > > > SNP folks and/or Paolo, what's the plan for this? I don't see how wh= at's sitting > > > in kvm/next can possibly be correct without conditioning population o= n the folio > > > being !uptodate. > >=20 > > I don't think I have time to look at it closely until Friday; but > > thanks for reminding me. >=20 > Ok, I'm officially confused. I think I understand what you did in your > suggested code. Limiting it to the bare minimum (keeping the callback > instead of CONFIG_HAVE_KVM_GMEM_INITIALIZE) it would be something > like what I include at the end of the message. >=20 > But the discussion upthread was about whether to do the check for > RMP state in sev.c, or do it in common code using folio_mark_uptodate(). > I am not sure what you mean by "cannot possibly be correct", and > whether it's referring to kvm_gmem_populate() in general or the > callback in sev_gmem_post_populate(). Doing fallocate() before KVM_SEV_SNP_LAUNCH_UPDATE will cause the latter to= fail. That likely works for QEMU, at least for now, but it's unnecessarily restri= ctive and IMO incorrect/wrong. E.g. a more convoluted, fallocate() + PUNCH_HOLE + KVM_SEV_SNP_LAUNCH_UPDAT= E will work (I think? AFAICT adding and removing pages directly to/from the RMP d= oesn't affect SNP's measurement, only pages that are added via SNP_LAUNCH_UPDATE a= ffect the measurement). Punting the sanity check to vendor code is also gross and will make it hard= er to provide a consistent, unified ABI for all architectures. E.g. SNP returns = -EINVAL if the page is already assigned, which is quite misleading. > The change below looks like just an optimization to me, which > suggests that I'm missing something glaring. I really dislike @prepare. There are two paths that should actually initia= lize the contents of the folio, and they are mutually exclusive and have meaning= fully different behavior. Faulting in memory via kvm_gmem_get_pfn() explicitly z= eros the folio _if necessary_, whereas kvm_gmem_populate() initializes the folio= with user-provided data _and_ requires that the folio be !uptodate. If we fix the above oddity where fallocate() initializes memory, then there= 's no need to try and handle the initialization in a common chokepoint as the = two relevant paths will naturally have unique code. The below is also still suboptimal for TDX, as KVM will zero the memory and= then TDX-module will also zero memory on PAGE.AUGA. And I find SNP to be the odd one. IIUC, the ASP (the artist formerly known= as the PSP) doesn't provide any guarantees about the contents of a page that i= s assigned to a guest without bouncing through SNP_LAUNCH_UPDATE. It'd be ni= ce to explicitly document that somewhere in the SNP code. E.g. if guest_memfd inv= okes a common kvm_gmem_initialize_folio() or whatever, then SNP's implementation= can clearly capture that KVM zeros the page to protect the _host_ data. > diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c > index d4206e53a9c81..a0417ef5b86eb 100644 > --- a/virt/kvm/guest_memfd.c > +++ b/virt/kvm/guest_memfd.c > @@ -52,37 +52,39 @@ static int kvm_gmem_prepare_folio(struct inode *inode= , pgoff_t index, struct fol > static struct folio *kvm_gmem_get_folio(struct inode *inode, pgoff_t ind= ex, bool prepare) > { > struct folio *folio; > + int r; > /* TODO: Support huge pages. */ > folio =3D filemap_grab_folio(inode->i_mapping, index); > if (IS_ERR(folio)) > return folio; > - /* > - * Use the up-to-date flag to track whether or not the memory has been > - * zeroed before being handed off to the guest. There is no backing > - * storage for the memory, so the folio will remain up-to-date until > - * it's removed. > - * > - * TODO: Skip clearing pages when trusted firmware will do it when > - * assigning memory to the guest. > - */ > - if (!folio_test_uptodate(folio)) { > - unsigned long nr_pages =3D folio_nr_pages(folio); > - unsigned long i; > + if (prepare) { > + /* > + * Use the up-to-date flag to track whether or not the memory has > + * been handed off to the guest. There is no backing storage for > + * the memory, so the folio will remain up-to-date until it's > + * removed. > + * > + * Take the occasion of the first prepare operation to clear it. > + */ > + if (!folio_test_uptodate(folio)) { > + unsigned long nr_pages =3D folio_nr_pages(folio); > + unsigned long i; > - for (i =3D 0; i < nr_pages; i++) > - clear_highpage(folio_page(folio, i)); > + for (i =3D 0; i < nr_pages; i++) > + clear_highpage(folio_page(folio, i)); > + } > + > + r =3D kvm_gmem_prepare_folio(inode, index, folio); > + if (r < 0) > + goto err_unlock_put; > folio_mark_uptodate(folio); > - } > - > - if (prepare) { > - int r =3D kvm_gmem_prepare_folio(inode, index, folio); > - if (r < 0) { > - folio_unlock(folio); > - folio_put(folio); > - return ERR_PTR(r); > + } else { > + if (folio_test_uptodate(folio)) { > + r =3D -EEXIST; > + goto err_unlock_put; > } > }