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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id DC761EEAA61 for ; Thu, 14 Sep 2023 18:15:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 52BB18D0005; Thu, 14 Sep 2023 14:15:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4DB548D0001; Thu, 14 Sep 2023 14:15:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3A2E38D0005; Thu, 14 Sep 2023 14:15:57 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 2A23A8D0001 for ; Thu, 14 Sep 2023 14:15:57 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id EF0BE160680 for ; Thu, 14 Sep 2023 18:15:56 +0000 (UTC) X-FDA: 81236006712.26.6D7BA72 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf14.hostedemail.com (Postfix) with ESMTP id 3A7AB100009 for ; Thu, 14 Sep 2023 18:15:55 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=ZeIhHDqS; spf=pass (imf14.hostedemail.com: domain of 32k0DZQYKCLEjVReaTXffXcV.TfdcZelo-ddbmRTb.fiX@flex--seanjc.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=32k0DZQYKCLEjVReaTXffXcV.TfdcZelo-ddbmRTb.fiX@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1694715355; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=NotwC0wYPH9Bmm+eHoPAvBDfUZeidJiPlRdc5f2ZPWA=; b=xBfreqsH5uuMhFU4e45OKbMc7lopVECD4RJdEwrY2XRxDNmbH0M91drLYQtSJYjBBQaAUO emmx2vJhmNUpxhfLNti4BHfjFBKt1yuNopXZ0VPJ3/idO8WADdAhIwCFXOcxpk9Qy2S+mz npaXMnZVRSnC3KHQ/z4vO8SnlkEHmsk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694715355; a=rsa-sha256; cv=none; b=rz0N8RZik/xz1njMM5mJZhiZWDU3VHL/WYOZTdbQetzHqOt/dlbPcE6uKj1PJrZNISwPUP dcRjlKoA/XDvJYVkFUk8tuVo+/Nf4SRuTuQ7uQEYyTDwF31zPxqHlZtTMn//vGwRxLvajm ag20xWobIBHSs74K8xtXX9cVLQhCh4k= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=ZeIhHDqS; spf=pass (imf14.hostedemail.com: domain of 32k0DZQYKCLEjVReaTXffXcV.TfdcZelo-ddbmRTb.fiX@flex--seanjc.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=32k0DZQYKCLEjVReaTXffXcV.TfdcZelo-ddbmRTb.fiX@flex--seanjc.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-59bbae05970so18022467b3.1 for ; Thu, 14 Sep 2023 11:15:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694715354; x=1695320154; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=NotwC0wYPH9Bmm+eHoPAvBDfUZeidJiPlRdc5f2ZPWA=; b=ZeIhHDqSnB/Mz8EehQMkHVkZT9GM0WTeaIF4zqIFFjlPW+I/Ag1Dsg2151v3iloU8I fDuQIewJwYafg8DdOtnV32kyFQU/W3g2/sIPNe33oUiX2KDOmqdoH1ftq1FKy78I3yC4 EHVMPFmZmXsiQ8GX/QBSzKmS28Pg4xwCnxHmhyS+pw83p4xq6sRI++Tn4iYTcTeg/pAD qf+Lv3/oY0M3ue5TaU7zGp+ZsWbyUesNd31Qol3zPjZzKgps1RcdGmuAb69gyri8aFIl ThFJRiLZH2B8WB2ggIuf04HShs8ZQjrQx6LE0OXN/KhJELaFRP1Iloc+nW1/+YssvcL4 QWlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694715354; x=1695320154; 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=NotwC0wYPH9Bmm+eHoPAvBDfUZeidJiPlRdc5f2ZPWA=; b=regKNVt7wfDtFt+wFXz8HFQurETI08qtrOlJlJqLDT7p+a9xLAg9YM3V27dEIGNwEI D9JegN7MtrqtGw/skjiF7jYDyr9b14GhSQEJmmS0mDQFJl5jIjYylDpd6pEJvYks9B2S 7w7XHXzXGsw3qXXW/N0OVAD6zw4va/zRy1+cPslkDF4TR7ftkh2LR43eP7H7AjEtUZpY 8pQn5uct2ULFx8uSRaXvBwEIPd2OhDHeHq0oyHezorzTs/GXC8KqAS60gbnXOHCM7fKE NdwoRU1TAbmrB4FELp1Q1f0ciL9S5Z64fbe7ETH2spW2wuS8gk6q/AhvFn5NlrWmCsS3 aIIg== X-Gm-Message-State: AOJu0YzytaAW+QyjDI8PMAAfZRwMl3LZfr1Roy6YV8PelGWV5BklJisj bbXkA2ON0GVs1G3i7eW5JRfO4/J3/Qs= X-Google-Smtp-Source: AGHT+IGPJqA/+ZACxXOUgdzWN4hGcwV50i6jcfcPa2GrWEQEeACHClOtxAN3Jr9h2WjxVFoJdPIyBK0jy04= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:690c:3187:b0:59b:e81f:62b3 with SMTP id fd7-20020a05690c318700b0059be81f62b3mr91025ywb.10.1694715354294; Thu, 14 Sep 2023 11:15:54 -0700 (PDT) Date: Thu, 14 Sep 2023 11:15:52 -0700 In-Reply-To: Mime-Version: 1.0 References: Message-ID: Subject: Re: [RFC PATCH v11 12/29] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory From: Sean Christopherson To: Ackerley Tng Cc: pbonzini@redhat.com, maz@kernel.org, oliver.upton@linux.dev, chenhuacai@kernel.org, mpe@ellerman.id.au, anup@brainfault.org, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, willy@infradead.org, akpm@linux-foundation.org, paul@paul-moore.com, jmorris@namei.org, serge@hallyn.com, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, chao.p.peng@linux.intel.com, tabba@google.com, jarkko@kernel.org, yu.c.zhang@linux.intel.com, vannapurve@google.com, mail@maciej.szmigiero.name, vbabka@suse.cz, david@redhat.com, qperret@google.com, michael.roth@amd.com, wei.w.wang@intel.com, liam.merwick@oracle.com, isaku.yamahata@gmail.com, kirill.shutemov@linux.intel.com Content-Type: text/plain; charset="us-ascii" X-Rspamd-Queue-Id: 3A7AB100009 X-Rspam-User: X-Stat-Signature: wwfh9u396ehpprht8bqbq371wnz7fo4f X-Rspamd-Server: rspam03 X-HE-Tag: 1694715355-755052 X-HE-Meta: U2FsdGVkX18X9yIUm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bvz6fpr4k9l/OEmzG0uDcdgPxYrfJ81NvAVSC/RnZvss+gQ3j+jcEeK+xcjvCihr8pF9a1mr2GgiNuLdmIh+vkRZzwptNvkHXEdB1Wquaau2/7hpg9GWjBD90j8dUWMTiCD4fUQJWlhIB0JIQ2wGz6ZGhWYjqV3AUf/zqjqX+p/pjFD9TWzOk6fqduXLKeQ+pZaudL3PBx+jx4pTtYy4NuqNxK+gUvQ9iLe+KEkBsxYncH3WL4oneM0yQ0ImTirkrECGmcfwsm23KZw4bMRlRwQd/s+yQxBngaqZUp/bop+fkNgg2Tyjv6ttZKe4IIxnkmWuSuBYzq7tWteAr4HNArs1OGQuaK4/GcYaXuc1l07kkytQ8iVOG/E6ll+SZqriBpFhzKknQqsx/RpkA0Fwyo23c4YLnHdwfFYMwd+MeEI1P1wgmLFvoAGgexPwZ0jJSedJsNAr/izz8JoYeqvgLiJW6yq6/lSg8YiW1eEhgit4z/Mj61v4nN6tBS7Y3iY3hu2Sg119N2bptkvK+LL7wmVsHWvynDZxT6u3qiE8mD2SDpc1U8NoXMceDdjlpr/zOESPVZBq3BQ2Mlv34gpP06j3Oxg3rEhknprEzndhcEEwobbU0ZHcz5Spc4kTiJSFTI5S3UEg21l X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Mon, Aug 28, 2023, Ackerley Tng wrote: > Sean Christopherson writes: > >> If we track struct kvm with the inode, then I think (a), (b) and (c) can > >> be independent of the refcounting method. What do you think? > > > > No go. Because again, the inode (physical memory) is coupled to the virtual machine > > as a thing, not to a "struct kvm". Or more concretely, the inode is coupled to an > > ASID or an HKID, and there can be multiple "struct kvm" objects associated with a > > single ASID. And at some point in the future, I suspect we'll have multiple KVM > > objects per HKID too. > > > > The current SEV use case is for the migration helper, where two KVM objects share > > a single ASID (the "real" VM and the helper). I suspect TDX will end up with > > similar behavior where helper "VMs" can use the HKID of the "real" VM. For KVM, > > that means multiple struct kvm objects being associated with a single HKID. > > > > To prevent use-after-free, KVM "just" needs to ensure the helper instances can't > > outlive the real instance, i.e. can't use the HKID/ASID after the owning virtual > > machine has been destroyed. > > > > To put it differently, "struct kvm" is a KVM software construct that _usually_, > > but not always, is associated 1:1 with a virtual machine. > > > > And FWIW, stashing the pointer without holding a reference would not be a complete > > solution, because it couldn't guard against KVM reusing a pointer. E.g. if a > > struct kvm was unbound and then freed, KVM could reuse the same memory for a new > > struct kvm, with a different ASID/HKID, and get a false negative on the rebinding > > check. > > I agree that inode (physical memory) is coupled to the virtual machine > as a more generic concept. > > I was hoping that in the absence of CC hardware providing a HKID/ASID, > the struct kvm pointer could act as a representation of the "virtual > machine". You're definitely right that KVM could reuse a pointer and so > that idea doesn't stand. > > I thought about generating UUIDs to represent "virtual machines" in the > absence of CC hardware, and this UUID could be transferred during > intra-host migration, but this still doesn't take host userspace out of > the TCB. A malicious host VMM could just use the migration ioctl to copy > the UUID to a malicious dumper VM, which would then pass checks with a > gmem file linked to the malicious dumper VM. This is fine for HKID/ASIDs > because the memory is encrypted; with UUIDs there's no memory > encryption. I don't understand what problem you're trying to solve. I don't see a need to provide a single concrete representation/definition of a "virtual machine". E.g. there's no need for a formal definition to securely perform intrahost migration, KVM just needs to ensure that the migration doesn't compromise guest security, functionality, etc. That gets a lot more complex if the target KVM instance (module, not "struct kvm") is a different KVM, e.g. when migrating to a different host. Then there needs to be a way to attest that the target is trusted and whatnot, but that still doesn't require there to be a formal definition of a "virtual machine". > Circling back to the original topic, was associating the file with > struct kvm at gmem file creation time meant to constrain the use of the > gmem file to one struct kvm, or one virtual machine, or something else? It's meant to keep things as simple as possible (relatively speaking). A 1:1 association between a KVM instance and a gmem instance means we don't have to worry about the edge cases and oddities I pointed out earlier in this thread. > Follow up questions: > > 1. Since the physical memory's representation is the inode and should be > coupled to the virtual machine (as a concept, not struct kvm), should > the binding/coupling be with the file, or the inode? Both. The @kvm instance is bound to a file, because the file is that @kvm's view of the underlying memory, e.g. effectively provides the translation of guest addresses to host memory. The @kvm instance is indirectly bound to the inode because the file is bound to the inode. > 2. Should struct kvm still be bound to the file/inode at gmem file > creation time, since Yes. > + struct kvm isn't a good representation of a "virtual machine" I don't see how this is relevant, because as above, I don't see why we need a canonical represenation of a virtual machine. > + we currently don't have anything that really represents a "virtual > machine" without hardware support HKIDs and ASIDs don't provide a "virtual machine" representation either. E.g. if a TDX guest is live migrated to a different host, it will likely have a different HKID, and definitely have a different encryption key, but it's still the same virtual machine. > I'd also like to bring up another userspace use case that Google has: > re-use of gmem files for rebooting guests when the KVM instance is > destroyed and rebuilt. > > When rebooting a VM there are some steps relating to gmem that are > performance-sensitive: If we (Google) really cared about performance, then we shouldn't destroy and recreate the VM in the first place. E.g. the cost of zapping, freeing, re-allocating and re-populating SPTEs is far from trivial. Pulling RESET shouldn't change what memory that is assigned to a VM, and reseting stats is downright bizarre IMO. In other words, I think Google's approach of destroying the VM to emulate a reboot is asinine. I'm not totally against extending KVM's uAPI to play nice with such an approach, but I'm not exactly sympathetic either. > a. Zeroing pages from the old VM when we close a gmem file/inode > b. Deallocating pages from the old VM when we close a gmem file/inode > c. Allocating pages for the new VM from the new gmem file/inode > d. Zeroing pages on page allocation > > We want to reuse the gmem file to save re-allocating pages (b. and c.), > and one of the two page zeroing allocations (a. or d.). > > Binding the gmem file to a struct kvm on creation time means the gmem > file can't be reused with another VM on reboot. Not without KVM's assistance, which userspace will need for TDX and SNP VMs no matter what, e.g. to ensure the new and old KVM instance get the same HKID/ASID. And we've already mapped out the more complex case of intrahost migration, so I don't expect this to be at all challenging to implement. > Also, host userspace is forced to close the gmem file to allow the old VM to > be freed. Yes, but that can happen after the "new" VM has instantiated its file/view of guest memory. > For other places where files pin KVM, like the stats fd pinning vCPUs, I > guess that matters less since there isn't much of a penalty to close and > re-open the stats fd.