From patchwork Tue Sep 10 16:30:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Roy X-Patchwork-Id: 13798881 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 80606EDE99A for ; Tue, 10 Sep 2024 16:31:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1A9698D008B; Tue, 10 Sep 2024 12:31:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 132918D0002; Tue, 10 Sep 2024 12:31:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EEE0C8D008B; Tue, 10 Sep 2024 12:31:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id CF7D48D0002 for ; Tue, 10 Sep 2024 12:31:09 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 7482DA0482 for ; Tue, 10 Sep 2024 16:31:09 +0000 (UTC) X-FDA: 82549368258.11.26DBDA1 Received: from smtp-fw-33001.amazon.com (smtp-fw-33001.amazon.com [207.171.190.10]) by imf29.hostedemail.com (Postfix) with ESMTP id 77A5C12001D for ; Tue, 10 Sep 2024 16:31:06 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=amazon.co.uk header.s=amazon201209 header.b=Yp0nyKSJ; dmarc=pass (policy=quarantine) header.from=amazon.co.uk; spf=pass (imf29.hostedemail.com: domain of "prvs=976277991=roypat@amazon.co.uk" designates 207.171.190.10 as permitted sender) smtp.mailfrom="prvs=976277991=roypat@amazon.co.uk" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725985839; a=rsa-sha256; cv=none; b=ECBagnm18tSxen53d5DQxQxkUnrRDJ0Y7G2iaSrxTlWTWpYKPH7m2KrkXAxTQcu6y/UdH6 nKcKKAEHBQA+3WRRYzRGzMXbeN/R+lSohIqwdUeoNPDl+IIYLhRjgQYz4V7D2G22laGoez xCcsq0O4TpIQD9+bAHVas2nteWMHF9Y= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=amazon.co.uk header.s=amazon201209 header.b=Yp0nyKSJ; dmarc=pass (policy=quarantine) header.from=amazon.co.uk; spf=pass (imf29.hostedemail.com: domain of "prvs=976277991=roypat@amazon.co.uk" designates 207.171.190.10 as permitted sender) smtp.mailfrom="prvs=976277991=roypat@amazon.co.uk" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725985839; 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:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=pg7ptibreX/x3/r6fh4gvZhhtdRZTe0WITSJkltiTvM=; b=x5n20GSOXeHp7aa6byDJsQHky2wcXv1RRGmfyMcpSKU5eQ1gK/ohnlbnQyNQAVhJeLgEBa dZHWnbYhiF66E1OcVz14+rv4ITf7Im7QdHUOiAM/QAUhA5X0O0qXd8FmvGatb+Wb+pOXvQ FCSrMz6rZ3AeMIGfAMULeF2z/3961Ug= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.co.uk; i=@amazon.co.uk; q=dns/txt; s=amazon201209; t=1725985867; x=1757521867; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=pg7ptibreX/x3/r6fh4gvZhhtdRZTe0WITSJkltiTvM=; b=Yp0nyKSJgFsxIJ3NYVcOKPnB6C7zEfqnqBwfX9+quI0WFEuKavdkmC0l M/T7BNiDSw1S6Gbfny21MRc03rzWTRwyL3vOA5LuipKH3sxBjoWj15c46 jRSWxthZBDHAJLzCVpcQ9eAXToYuliEbaZZpuHd3ITp4sdgHZzlT9H0Tb 0=; X-IronPort-AV: E=Sophos;i="6.10,217,1719878400"; d="scan'208";a="367280292" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO smtpout.prod.us-east-1.prod.farcaster.email.amazon.dev) ([10.43.8.6]) by smtp-border-fw-33001.sea14.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Sep 2024 16:30:59 +0000 Received: from EX19MTAUEB001.ant.amazon.com [10.0.44.209:7503] by smtpin.naws.us-east-1.prod.farcaster.email.amazon.dev [10.0.42.209:2525] with esmtp (Farcaster) id a1e9aa06-afd3-4fab-a6da-6c43322de62d; Tue, 10 Sep 2024 16:30:57 +0000 (UTC) X-Farcaster-Flow-ID: a1e9aa06-afd3-4fab-a6da-6c43322de62d Received: from EX19D008UEC004.ant.amazon.com (10.252.135.170) by EX19MTAUEB001.ant.amazon.com (10.252.135.108) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Tue, 10 Sep 2024 16:30:45 +0000 Received: from EX19MTAUWB001.ant.amazon.com (10.250.64.248) by EX19D008UEC004.ant.amazon.com (10.252.135.170) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Tue, 10 Sep 2024 16:30:44 +0000 Received: from ua2d7e1a6107c5b.home (172.19.88.180) by mail-relay.amazon.com (10.250.64.254) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34 via Frontend Transport; Tue, 10 Sep 2024 16:30:40 +0000 From: Patrick Roy To: , , , , , , , , , , , , , , , , , , , , CC: Patrick Roy , , , , , Subject: [RFC PATCH v2 00/10] Unmapping guest_memfd from Direct Map Date: Tue, 10 Sep 2024 17:30:26 +0100 Message-ID: <20240910163038.1298452-1-roypat@amazon.co.uk> X-Mailer: git-send-email 2.46.0 MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 77A5C12001D X-Rspamd-Server: rspam01 X-Stat-Signature: mc6i5wzeukg6msu4xcngd41gopph7cho X-HE-Tag: 1725985866-473695 X-HE-Meta: U2FsdGVkX1/Hyp6dwqJX2mEYNCwpi3yNFOEHsWp310Sttc41ojrKqLGjekWfcYOJ39yjaMCogxy0AHI6A/aXMC1MBMxpNJka6bqtqYPMkYA0bFozSTnydf8vALACRAP9CBWbaJ5kbwRHECrUO7LATRcc5S1FzwKKulHMFTRUecvwyrMSlbnOTvr2RbceRxBEvSGQWDhZs5W3l97BJsjsKSt5piRTAuJ0av4S/3H/uKE7YculdPPRLocUidOZUZ92gs+Fw6IZENhpn/GTJ92WKeBdjN0DwyjPaD80PP8zCwVh6GMqXwZFgNw/2BTOcCqBLQKj5i3p8yNKvW5j+BkQBcqpA6pFdxJk3H1D7gXKqaypdCTBiBgwqaW/1lBPlgo4RvExA60o/OWSFITH9I7XdjdeyPjIaT6a+OAA0lLvRw19KN3NKHLaXJugATOBzoTN7yo7qtDk9zne21glH4rigMG3lJ5lAHwcMZ2w2SfPvIvMZplIkqwepwtH6xXRg4ZUO5xMlo005XsbIAfF7ZhVbfAyLLPHwvXJJ/xRTwB+2Fcu8SILakyUWgR/IhuvqYLrfDNtThbVmM4JGxXr1Y4C2hKZYibnCAPjF3Gd4gY9V7EbX3FMJksLQHI6HR0AY94nPX/qDb9w3Vjldy3hLevnI1DGwR28MfypEHezP1/o9+MoIHzkSxJdBwbQs1D48jVdMoPg15FqubZy3wNJAKBWyr2a0onMfXZDquT2kJxLLLvkUZBvVmeoQIAREhGpv/IKz70eLw9J+xUovTyfUpRlJsi7io1pZNTDEdXY5U9wp/horzPsCi3khzHXaeTsMk00oIENPZfieh/CKvwreaZkiwD1eXcBUVqhRTRwGvD/LC2tmZVgSdkoRxOfayf2U3OKgCKrg+Y9tMixa/gsLoGtKdW3UhjRdpsQVQQOngvBSdrrLApcjaf0GVhHZF7N5tvbeOprjrGnWSbNyVMYFq0 9FbPLNVp NHbysjV+nqiDG34sx7tN8qkpyHrSlaPsBmXA7xTEv0AqqRF2Vz0YFFXmg4pb+gBVDAPvXeXXgyl/fQ+X5j31WoabTW2+nkbGVQGCEO1zUfTTXMI5pE6ROvEfFd7IpDv1inSY7/l0Mq3EVKTr6LzI3+MEEcMOsrgJELQMWjf2TPnEIjuVN/9YK0nG2v51gjHZd1cxY6zDig7JoAkXjEFtdG9wLCR0B8pXVLwx4Xu10PudPJcW+XbYTM2lhrXKYbcfPx2HkL8SDnjjUIYku3Qm/qHRTEvbKvmwRgBMnZpp/i5V2hejUX7Hj3rMDfSGRU8ZPNAHZ7qTgrA/LdV2Ods1JpQeq3dVX9wcxvRcgUciMI8UtgLUAOR3UH1xtTbzD3X9ee5dkmMRzX2HXLzJEsr569rk4KOW8vQiUpbrC8HRWw8LFzDGQMG943kGbMrFYpIoau/DFSle9Il3weX18IsaTqt0SRo1xO77d5/TilHh+8+QJ1qMOC5BVC9gvQIZWCwWojZFT65Z5sPDw49GfFk62wl7Ev2SARizl93O9tIjnWQ8XdRbivie6OHnE2csQ6ypQPW+dnclgHVxbhW6rVqMKuZMQvx5hwiRzWC7f72F1ldbyIQwu5d3YRyZ0TMw31oL3E1esfGACMUK5Kdnz0Gsmulob5oSyvJw5rTERzsBRd7UOkwvgwTvXG1MW4g== 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: List-Subscribe: List-Unsubscribe: Hey all, This is an overhaul of my RFC [1] for removing guest_memfd folios from the direct map. The goal of this is to back non-confidential VMs using guest_memfd and protect their memory from a large class of speculative execution issues [1, Table 1]. This RFC series is also the basis of my LPC submission [3]. === Changes to v1 === - Drop patches related to userspace mappings to only focus on direct map removal. - Use a refcount to track temporary direct map reinsertions (Paolo/Elliot) - Implement invalidation of gfn_to_pfn_caches holding gmem pfns (David W.) - Do not assume folios have only a single page (Mike R.) === Implementation === This patch series extends guest_memfd to run "non-confidential" VMs that still have their guest memory removed from the host's direct map. "non-confidential" here means that we wish to treat the VM pretty much the same as a VM with traditional, non-guest_memfd memslots: KVM should be able to access non sensitive parts of guest memory such as page tables and MMIO instructions for MMIO emulation, or things like the kvm-clock page, without requiring the guest collaboration. This patch series thus does two things: First introduce a new `KVM_GMEM_NO_DIRECT_MAP` flag to the `KVM_CREATE_GUEST_MEMFD` ioctl that causes guest_memfd to remove its folios from the direct map immediately after allocation. Then, teach key parts of KVM about how to access guest_memfd memory (if the vm type allows it) via temporary restoration of direct map entries. The parts of KVM which we enlighten like this are - kvm_{read,write}_guest and friends (needed for instruction fetch during MMIO emulation) - paging64_walk_addr_generic (needed to resolve GPAs during MMIO emulation) - pfncache.c (needed for kvm-clock) These are a minimal set needed to boot a non-confidential initrd from guest_memfd (provided one finds a way to load such a thing into guest_memfd. My testing was done with an additional commit on this of this series that allows unconditional userspace mappings of guest_memfd). Instruction fetch for MMIO emulation is special here in the sense that it cannot be solved by having the guest explicitly share memory ahead of time, since such conversions in guest_memfd are destructive, and the guest cannot know which instructions will trigger MMIO ahead of time (TDX for example has a special paravirtual solution for this case). It is thus the original motivation for the approach in this patch series. In terms of the proposed framework for allowing both "shared" and "private" memory inside guest_memfd (with in-place conversions supported) [4], memory with its direct map entries removed can be considered "private", while gmem memory with direct map entries can be considered "shared" (I'm afraid this patch series also hasn't found better names than the horribly overloaded "shared" and "private" for this). Implementing support for accessing guest_memfd in kvm_{read,write}_guest is fairly straight forward, as the operation is a simple "remap->access->unmap" sequence that can be completely done while holding the folio lock. However, "long term" accesses such a gfn_to_pfn_caches, which reinsert parts of gmem into the direct map for long periods of time, proved to be tricky to implement, due to the need to respond to gmem invalidation events (to, for example, avoid modifying direct map entries after a folio has been fallocated away and freed). This part is why this series is still an RFC, because my confidence in getting those patches right is fairly low. For what's implemented here, an alternative would be to just have the guest share page tables the kvm-clock pages ahead of time (while keeping the changes to kvm_{read,write}_guest to handle instruction emulation), however I'm not sure this would work for potential future usecases such as nested virtualization (where the L1 guest cannot know ahead of time where the L2 will place page tables, and thus cannot mark them as shared). === Security === We want to use unmapping guest memory from the host kernel as a security mitigation against transient execution attacks. Temporarily restoring direct map entries whenever KVM requires access to guest memory leaves a gap in this mitigation. We believe this to be acceptable for the above cases, since pages used for paravirtual guest/host communication (e.g. kvm-clock) and guest page tables do not contain sensitive data. MMIO emulation will only end up reading pages containing privileged instructions (e.g. guest kernel code). === Summary === Patches 1-3 are about adding the KVM_GMEM_NO_DIRECT_MAP flag, and providing the basic functions needed to on-demand remap guest_memfd folios. Patch 4 deals with kvm_{read,write}_guest. Patches 4 and 5 are about adding the "sharing refcount" framework for supporting long-term direct map restoration of gmem folios. Patches 7-9 integrate guest_memfd with the pfncache machinery. Patch 10 teaches the guest page table walker about guest_memfd. The first few patches are very similar to parts of Elliot's "mm: Introduce guest_memfd library" RFC series [5]. This series focuses on the non-confidential usecase, while Elliot's series focuses more on userspace mappings for confidential VMs. We've had some discussions on how to marry these two in [6]. === ToDos / Limitations === The main question I am looking for feedback on is whether I am on the right path with the "sharing refcount" idea for long-term, KVM-initiated sharing of gmem folios at all, or whether the last few patches look so horrendous that a completely different solution is needed. Other than that, the patches are of course still missing selftests. Best, Patrick [1]: https://lore.kernel.org/kvm/20240709132041.3625501-1-roypat@amazon.co.uk/T/#mf6eb2d36bab802da411505f46ba154885cb207e6 [2]: https://download.vusec.net/papers/quarantine_raid23.pdf [3]: https://lpc.events/event/18/contributions/1763/ [4]: https://lore.kernel.org/linux-mm/BN9PR11MB5276D7FAC258CFC02F75D0648CB32@BN9PR11MB5276.namprd11.prod.outlook.com/T/#mc944a6fdcd20a35f654c2be99f9c91a117c1bed4 [5]: https://lore.kernel.org/kvm/20240829-guest-memfd-lib-v2-0-b9afc1ff3656@quicinc.com/T/#mbcf942dcccc3726921743251d07b1a3a7e711d3f [6]: https://lore.kernel.org/kvm/20240805-guest-memfd-lib-v1-0-e5a29a4ff5d7@quicinc.com/T/#m785c2c1731be216fd0f6aa4c22d8b4aab146f3c1 Patrick Roy (10): kvm: gmem: Add option to remove gmem from direct map kvm: gmem: Add KVM_GMEM_GET_PFN_SHARED kvm: gmem: Add KVM_GMEM_GET_PFN_LOCKED kvm: Allow reading/writing gmem using kvm_{read,write}_guest kvm: gmem: Refcount internal accesses to gmem kvm: gmem: add tracepoints for gmem share/unshare kvm: pfncache: invalidate when memory attributes change kvm: pfncache: Support caching gmem pfns kvm: pfncache: hook up to gmem invalidation kvm: x86: support walking guest page tables in gmem arch/x86/kvm/mmu/mmu.c | 2 +- arch/x86/kvm/mmu/paging_tmpl.h | 95 ++++++++++++--- include/linux/kvm_host.h | 17 ++- include/linux/kvm_types.h | 2 + include/trace/events/kvm.h | 43 +++++++ include/uapi/linux/kvm.h | 2 + virt/kvm/guest_memfd.c | 216 ++++++++++++++++++++++++++++++--- virt/kvm/kvm_main.c | 91 ++++++++++++++ virt/kvm/kvm_mm.h | 12 ++ virt/kvm/pfncache.c | 144 ++++++++++++++++++++-- 10 files changed, 579 insertions(+), 45 deletions(-) base-commit: 332d2c1d713e232e163386c35a3ba0c1b90df83f