From patchwork Tue Sep 10 16:30:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Roy X-Patchwork-Id: 13798886 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 172DDECE58A for ; Tue, 10 Sep 2024 16:31:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A0E938D0091; Tue, 10 Sep 2024 12:31:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 970B48D0002; Tue, 10 Sep 2024 12:31:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 79DF78D0091; Tue, 10 Sep 2024 12:31:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 5B5078D0002 for ; Tue, 10 Sep 2024 12:31:36 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id D303BC085F for ; Tue, 10 Sep 2024 16:31:35 +0000 (UTC) X-FDA: 82549369350.15.78C5044 Received: from smtp-fw-80009.amazon.com (smtp-fw-80009.amazon.com [99.78.197.220]) by imf11.hostedemail.com (Postfix) with ESMTP id A6E6D4002E for ; Tue, 10 Sep 2024 16:31:33 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=amazon.co.uk header.s=amazon201209 header.b=nU2GnVhY; spf=pass (imf11.hostedemail.com: domain of "prvs=976277991=roypat@amazon.co.uk" designates 99.78.197.220 as permitted sender) smtp.mailfrom="prvs=976277991=roypat@amazon.co.uk"; dmarc=pass (policy=quarantine) header.from=amazon.co.uk ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725985757; 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:in-reply-to:references:references:dkim-signature; bh=Mg6x7TwXBxF2qrgNjQW5TE5s/jw6SkTYEEB7LumyV7k=; b=k0VjTOSzko/dJ8cqMv/Ym1Zrwtk149DTu9KuSNMFhEHkPsz8QizsEHTPJ3tjjVkXLxm/sf 7MiNAeHw1LHYVsD68wD1aRdzDRwaUqO9gBNCiOZAiWyCTcrO4jH6U2QrvU3J919ykUZ+7B 9cXN6qGKsDsTMYba534BIhX9F/0qfK0= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=amazon.co.uk header.s=amazon201209 header.b=nU2GnVhY; spf=pass (imf11.hostedemail.com: domain of "prvs=976277991=roypat@amazon.co.uk" designates 99.78.197.220 as permitted sender) smtp.mailfrom="prvs=976277991=roypat@amazon.co.uk"; dmarc=pass (policy=quarantine) header.from=amazon.co.uk ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725985757; a=rsa-sha256; cv=none; b=TpPHIX1WJfX3OYslZnEVEtrY1h5tlKcqhGBSiLgGWw6YCH+eX2ym5pGKo0NaNa9ZLBNVFU lQAqW7M7D9uNp/aztWmgY9IV5eXtq76ru1aPI0bYWGk2RiMlXJFq21kNue8szkc/xHih10 isFQBYCsVmkfPQz5ajkJydRzA/VdT/8= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.co.uk; i=@amazon.co.uk; q=dns/txt; s=amazon201209; t=1725985893; x=1757521893; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Mg6x7TwXBxF2qrgNjQW5TE5s/jw6SkTYEEB7LumyV7k=; b=nU2GnVhY3B3qwMWmK5ILxoRjcXxNSrWX/lzeGwS7l1Ku1twqTfpSXKTd aqw/Ur+J5F6h98IiH2NabE+XcIBlM/GXHsEJGKFytfykhcFZwh1gC9WvA DqTmVTNUu6XRlkt7cHaGNSGHQZQwPWefvtieZoIATf12vuXwZiubOafyr 0=; X-IronPort-AV: E=Sophos;i="6.10,217,1719878400"; d="scan'208";a="124249487" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO smtpout.prod.us-east-1.prod.farcaster.email.amazon.dev) ([10.25.36.210]) by smtp-border-fw-80009.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 10 Sep 2024 16:31:20 +0000 Received: from EX19MTAUEA001.ant.amazon.com [10.0.29.78:64554] by smtpin.naws.us-east-1.prod.farcaster.email.amazon.dev [10.0.48.28:2525] with esmtp (Farcaster) id c989717d-c1d0-4610-a59c-ea42657013e3; Tue, 10 Sep 2024 16:31:19 +0000 (UTC) X-Farcaster-Flow-ID: c989717d-c1d0-4610-a59c-ea42657013e3 Received: from EX19D008UEA002.ant.amazon.com (10.252.134.125) by EX19MTAUEA001.ant.amazon.com (10.252.134.203) 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:31:09 +0000 Received: from EX19MTAUWB001.ant.amazon.com (10.250.64.248) by EX19D008UEA002.ant.amazon.com (10.252.134.125) 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:31:08 +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:31:04 +0000 From: Patrick Roy To: , , , , , , , , , , , , , , , , , , , , CC: Patrick Roy , , , , , Subject: [RFC PATCH v2 04/10] kvm: Allow reading/writing gmem using kvm_{read,write}_guest Date: Tue, 10 Sep 2024 17:30:30 +0100 Message-ID: <20240910163038.1298452-5-roypat@amazon.co.uk> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240910163038.1298452-1-roypat@amazon.co.uk> References: <20240910163038.1298452-1-roypat@amazon.co.uk> MIME-Version: 1.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: A6E6D4002E X-Stat-Signature: wqhwy4ofo9dkt9c6hedttrgpcssr6fzy X-Rspam-User: X-HE-Tag: 1725985893-257373 X-HE-Meta: U2FsdGVkX19UGLYnQI6OYizTGXJeINinxWQjTjITskpv4YB7LtrWr/26bmmZvOv/ZNR5uTmRXgvxrL1cpb/sgGR2V24hKRVeM1TgSMjul5aMZGdfb6huUK57hNutVO2kxgYNdw7YgDvHJA+xwDb8lDXN/8iWm/Hrasj07IzsNMAZcKwRZg17FDEG/SpdJsbG8Tb2cgwy9ItBLVJNgCSwBiFktkLwGhNd0nUWrqDoWrSi5jsD8FfFiJr9pXKtdHU49NJXpX7miHHyfz5usjzUy/Q5QaXFRdHEvP8HMxVvADp1eA83zoH4JKcvTsB9/4v7xge1ABO9LGVVi5e48hqJinM5j702hYPNPAt1Hyqs5yRJker0Eri/0ZaqlxXnmeHuW6u8j3F5mFbMx9fbCBudq06IA6/WsbFeCilT8Zl10w5rqdG/BqsANUjYQhvsesjuArNux8PJe/Rhx+OyIrY0G7P8WkoDDskFwvQ4UAscnK18Xng1kWwI3esfZ7M4n46Row/8aZhvb7jZ49aBxp1+U1/oEP+fLjvf3IUiBqSjxj65WphP4sRpTV1mWOfvsOJ2cscYxS6PANMyUuHQxl6TrL6n5RkOJxHOxkpvedamZk0KiWcTa0RWERfcwsHedR3pwMzjtHVNNvpGIMJvfcHRbfAkhoBIuIjwtge6Wa5thnDbO1lTmyhFi4V8gk30vnQi3/w+BwVFkHj/B+3TS8tN/XXR+6x8rwbNAEUryixtwZqVcrv7sPj9oqXjOHYlzUR4+MXi/C1ZAmR2hd37Yj7rHUsisIGGpWlFegOykuqYF0wLbch1C/+iEC/gNcNQGlA+pgd3bMcNUBBJWsotvkG1ZgLtMicAc6f3moD7VJ5RnL2rzp+Ow5EV1XtAjru6MGf6Uu38ygFzBXWJHgVaPYTgYNqVIg2RImaT7JjrO4O7YxE7JBFubrDD1hzKl9fwxmfam9y5kEqf2WupMSzqLFU OAvsCjcN HSMVUzJAtyaQOJaRO/bqnGAdKmcmO1LF5Wmi9UQ7J6q3xQHcuCVEiHyZNGI3WGU29pjLWGXfhqs31tKy6+wjwEKXll92XaoKD05U4D9WgBCeXxEOmAynWPCHFlAwpwYakqNjcGVL1f0W7h1TTK3a6d3rjWLOGbCrSKjAwRSdHaeeORkrFaWpwa3NjbkpNJ3jkTlMAnoXml3WQmwKxFYWX67CcWqsu20DF91gULasfXhiOvBLFJDpCehQsN0JyY0PwoCWcXbZh6Eo1oaJItF9W+QLs6ZElcEkgPJ42fGytwO3o9sijvKLu+Egmps0lq3cnCwskzDPBVUmfTOVZ0FYBCIB69emFYSgsHDF7MqqDzzunnViHo9qGLEArfMfsd90IxUSBUQ4zZTNooSEqreTo33cNvaHtqcjncWUsWgR5ndKhi6154ChwGo5HlA== 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: If KVM can access guest_memfd memory (or at least convert it into a state in which KVM can access it) without causing a host-kernel panic (e.g. currently only if the vm type is KVM_X86_SW_PROTECTED_VM), allow `kvm_{read,write}_guest` to access gfns that are backed by gmem. If KVM cannot access guest_memfd memory (say, because it is running a TDX VM), prepare a KVM_EXIT_MEMORY_FAULT (if possible) and return -EFAULT. KVM can only prepare the memory fault exit inside the `kvm_vcpu_{read,write}_guest` variant, as it needs a vcpu reference to assign the exit reason to. KVM accesses to gmem are done via the direct map (as no userspace mappings exist, and even if they existed, they wouldn't be reflected into the memslots). If `KVM_GMEM_NO_DIRECT_MAP` is set, then temporarily reinsert the accessed folio into the direct map. Hold the folio lock for the entire duration of the access to prevent concurrent direct map modifications from taking place (as these might remove the direct map entry while kvm_{read,write}_guest is using it, which would result in a panic). Signed-off-by: Patrick Roy --- virt/kvm/kvm_main.c | 83 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index d0788d0a72cc0..13347fb03d4a9 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3286,11 +3286,51 @@ static int __kvm_read_guest_page(struct kvm_memory_slot *slot, gfn_t gfn, return 0; } +static int __kvm_read_guest_private_page(struct kvm *kvm, + struct kvm_memory_slot *memslot, gfn_t gfn, + void *data, int offset, int len) +{ + kvm_pfn_t pfn; + int r; + struct folio *folio; + + r = kvm_gmem_get_pfn(kvm, memslot, gfn, &pfn, NULL, + KVM_GMEM_GET_PFN_SHARED | KVM_GMEM_GET_PFN_LOCKED); + + if (r < 0) + return r; + + folio = pfn_folio(pfn); + memcpy(data, folio_address(folio) + offset, len); + r = kvm_gmem_put_shared_pfn(pfn); + folio_unlock(folio); + folio_put(folio); + return r; +} + +static int __kvm_vcpu_read_guest_private_page(struct kvm_vcpu *vcpu, + struct kvm_memory_slot *memslot, gfn_t gfn, + void *data, int offset, int len) +{ + int r = __kvm_read_guest_private_page(vcpu->kvm, memslot, gfn, data, offset, len); + + /* kvm not allowed to access gmem */ + if (r == -EPERM) { + kvm_prepare_memory_fault_exit(vcpu, gfn + offset, len, false, + false, true); + return -EFAULT; + } + + return r; +} + int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset, int len) { struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn); + if (kvm_mem_is_private(kvm, gfn)) + return __kvm_read_guest_private_page(kvm, slot, gfn, data, offset, len); return __kvm_read_guest_page(slot, gfn, data, offset, len); } EXPORT_SYMBOL_GPL(kvm_read_guest_page); @@ -3300,6 +3340,8 @@ int kvm_vcpu_read_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, void *data, { struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn); + if (kvm_mem_is_private(vcpu->kvm, gfn)) + return __kvm_vcpu_read_guest_private_page(vcpu, slot, gfn, data, offset, len); return __kvm_read_guest_page(slot, gfn, data, offset, len); } EXPORT_SYMBOL_GPL(kvm_vcpu_read_guest_page); @@ -3390,11 +3432,50 @@ static int __kvm_write_guest_page(struct kvm *kvm, return 0; } +static int __kvm_write_guest_private_page(struct kvm *kvm, + struct kvm_memory_slot *memslot, gfn_t gfn, + const void *data, int offset, int len) +{ + kvm_pfn_t pfn; + int r; + struct folio *folio; + + r = kvm_gmem_get_pfn(kvm, memslot, gfn, &pfn, NULL, + KVM_GMEM_GET_PFN_SHARED | KVM_GMEM_GET_PFN_LOCKED); + + if (r < 0) + return r; + + folio = pfn_folio(pfn); + memcpy(folio_address(folio) + offset, data, len); + r = kvm_gmem_put_shared_pfn(pfn); + folio_unlock(folio); + folio_put(folio); + return r; +} + +static int __kvm_vcpu_write_guest_private_page(struct kvm_vcpu *vcpu, + struct kvm_memory_slot *memslot, gfn_t gfn, + const void *data, int offset, int len) +{ + int r = __kvm_write_guest_private_page(vcpu->kvm, memslot, gfn, data, offset, len); + + if (r == -EPERM) { + kvm_prepare_memory_fault_exit(vcpu, gfn + offset, len, true, + false, true); + return -EFAULT; + } + + return r; +} + int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn, const void *data, int offset, int len) { struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn); + if (kvm_mem_is_private(kvm, gfn)) + return __kvm_write_guest_private_page(kvm, slot, gfn, data, offset, len); return __kvm_write_guest_page(kvm, slot, gfn, data, offset, len); } EXPORT_SYMBOL_GPL(kvm_write_guest_page); @@ -3404,6 +3485,8 @@ int kvm_vcpu_write_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, { struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn); + if (kvm_mem_is_private(vcpu->kvm, gfn)) + return __kvm_vcpu_write_guest_private_page(vcpu, slot, gfn, data, offset, len); return __kvm_write_guest_page(vcpu->kvm, slot, gfn, data, offset, len); } EXPORT_SYMBOL_GPL(kvm_vcpu_write_guest_page);