From patchwork Tue Jul 9 13:20:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Roy X-Patchwork-Id: 13727949 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 08080C2BD09 for ; Tue, 9 Jul 2024 13:21:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8E07A6B00A3; Tue, 9 Jul 2024 09:21:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 86A496B00A7; Tue, 9 Jul 2024 09:21:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6E30A6B00A8; Tue, 9 Jul 2024 09:21:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 4CF266B00A3 for ; Tue, 9 Jul 2024 09:21:02 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id E4BB81A152B for ; Tue, 9 Jul 2024 13:21:01 +0000 (UTC) X-FDA: 82320274722.18.8F35EE1 Received: from smtp-fw-52004.amazon.com (smtp-fw-52004.amazon.com [52.119.213.154]) by imf21.hostedemail.com (Postfix) with ESMTP id 029A41C001A for ; Tue, 9 Jul 2024 13:20:59 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=amazon.co.uk header.s=amazon201209 header.b=bEsxCxdN; spf=pass (imf21.hostedemail.com: domain of "prvs=913fd7204=roypat@amazon.co.uk" designates 52.119.213.154 as permitted sender) smtp.mailfrom="prvs=913fd7204=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=1720531245; 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=myefINasqoLP8LFmYmmDEFb4CuSNvTYNmrkp8V5RNNQ=; b=kkHuAT8g+HICsBQC7WVtXlmQf7epGgLeHHui/kJ2kw2/8GQDrmclbm0S6mmFDfHiluN/Om ESkKrT6XiMCnB8tzPwpmdfTYfwQUrLhkhIb7/cM+QNRdc6mRbiCDjWeRK7gnharN4zAESk C3TD5qKpzox7DnHPy2Z7sdvtFgj3P7I= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=amazon.co.uk header.s=amazon201209 header.b=bEsxCxdN; spf=pass (imf21.hostedemail.com: domain of "prvs=913fd7204=roypat@amazon.co.uk" designates 52.119.213.154 as permitted sender) smtp.mailfrom="prvs=913fd7204=roypat@amazon.co.uk"; dmarc=pass (policy=quarantine) header.from=amazon.co.uk ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1720531245; a=rsa-sha256; cv=none; b=j/p488vNBEyd9+py7nuj3jVdw2kuobMD7mimdVkxHyRdtVR6gVy0KqvhZbigZxfVnNXz8v Hqu14QD0tooeYteg4C1uOBdCS3gLxMQq6a9WQeD1OYnj342ZQhj/SSS6IoBzcr2hfqXRli +uUuFRyJRYY6Y8heqaU4gGQblPUxMjg= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.co.uk; i=@amazon.co.uk; q=dns/txt; s=amazon201209; t=1720531260; x=1752067260; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=myefINasqoLP8LFmYmmDEFb4CuSNvTYNmrkp8V5RNNQ=; b=bEsxCxdN3ru94aKZb0/1q4Gst3DjyxBRIiFbG8KuVk28wAnM/z/Xpc9s WRlKtr2wazKJVhx5mHreB1pfy2RkrhEmP1VU1iNKLU6wIwVOL5FOontin 1XSKRl2kDsQ+Za/E9B8ZDSBLejoVfWuuusJRbh4twHSnA8Yid6P5Mu4ja 0=; X-IronPort-AV: E=Sophos;i="6.09,195,1716249600"; d="scan'208";a="217222124" Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO smtpout.prod.us-east-1.prod.farcaster.email.amazon.dev) ([10.43.8.2]) by smtp-border-fw-52004.iad7.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Jul 2024 13:20:58 +0000 Received: from EX19MTAUEB001.ant.amazon.com [10.0.0.204:19435] by smtpin.naws.us-east-1.prod.farcaster.email.amazon.dev [10.0.13.24:2525] with esmtp (Farcaster) id 18a4efc4-1b3b-4e58-98d8-17e4bb420299; Tue, 9 Jul 2024 13:20:57 +0000 (UTC) X-Farcaster-Flow-ID: 18a4efc4-1b3b-4e58-98d8-17e4bb420299 Received: from EX19D008UEA004.ant.amazon.com (10.252.134.191) 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, 9 Jul 2024 13:20:57 +0000 Received: from EX19MTAUEC001.ant.amazon.com (10.252.135.222) by EX19D008UEA004.ant.amazon.com (10.252.134.191) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Tue, 9 Jul 2024 13:20:56 +0000 Received: from ua2d7e1a6107c5b.ant.amazon.com (172.19.88.180) by mail-relay.amazon.com (10.252.135.200) 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, 9 Jul 2024 13:20:54 +0000 From: Patrick Roy To: , , , , , CC: Patrick Roy , , , , , , , , , , , , , , , , , Subject: [RFC PATCH 1/8] kvm: Allow reading/writing gmem using kvm_{read,write}_guest Date: Tue, 9 Jul 2024 14:20:29 +0100 Message-ID: <20240709132041.3625501-2-roypat@amazon.co.uk> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240709132041.3625501-1-roypat@amazon.co.uk> References: <20240709132041.3625501-1-roypat@amazon.co.uk> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 029A41C001A X-Stat-Signature: zea6isqq8r58ay4cu63oo1h9yx4sxmm3 X-HE-Tag: 1720531259-448239 X-HE-Meta: U2FsdGVkX1/udc607P0FYmyzvL6SMwe2oH7wLDrBEXxxn3tt+7GuGJ/gQDStRiruB+8EN1fmWJCHInrrSbIqvHVQArxM1Yi+XkmgQFSSJr2CrtaedEEna4np2ubeh+9XAY2vo/Ecj+xRfrYW+7g38hh00y1nyD1KRlQ6kR2HhyQ4mBoWW5ME75xUFt0SWuIOfwq/7sqjXpM57qPXlbSKmN6tbn+S+J5SJL/2zmkKFM1Ehv1DvCV81ww/DgLJGcLgj4mNIj6bFM8eH/fedB9i2JM8xpMPJVJcjzB/1m1HuWWLEnPROL2+XWl7qjLlKkUSx1cHE4V6A7fH3EGDc8gdnsMBCDXAYC9VpHeDy6DBIQXi0eOp9Vyck5TSWn1Qn88KnO7GhsVy15iUwAlBFdCTZ4SR1MGU5QlUTuWlFp5RL4vSPkrbtfUpJkbafJ3N5xx/BN0yKBCetGNigUgr/aZS8FY5B86vhU2NrCkvTbNRmeDHHpFkDiFx1WRtvWV6Tfn2M3YmIKhwbB8f78QWryysh3S6GbCGH5pXL+mKapuXLMvlUYDrAb1CYDmkjTWRMEdM95LBt17Ymnfj7nfxE/zJqmWPF9N6BF/rjjjpIB1aN4DLj19bvNGssWp5unKfbTJeDh0f3jAqPcai9ur2y09A3Ynhrq0CxzirFCVoEjTMAipx5TfQchTU7XCWEW5l/GNWWC3P+6Dm6HMgytz5NEAcdGt/l8UzacsBfOLW3xaudGRXxcsfMxhTR6OVtnRA0avue1tF+INpJ60gDW3PmLnznv1kgT/n6Dj5rqLvFINO19JzyPF7v3rGRoMKUMCVK87Gv8zFY5JkcqaRhdqDP+ppnnsqv/TyklIuaP21OR437jpuTRhxZLwa7pc3hpLYfTFewRPpBKHA4C6bohsJLkcQCB3dtvQHvMdPyou9e12Olb0fin9gqyZhtwQrMi8iW55EDRAWvhzEQWE4yFMPHlB y+lV7bCE Nc3BWtrbFygRi8Bp8mrfaLQFZD7oxMtsGZYjo5/PrbDZUNsvWCnXtaGjS6Sk7QVpR+pS6Q0fp0VvJA/rS4wgZvpSMVTaBqeZyrt4wozXLO6lTRYGV+vHycaUU1SjziXBesSDA9Ky0xAuV1me3OvlUEPn+3ILRXXsMBR/mtn6Mv0Tcl7K0wz3tndJAAB0Dtfg0JY5pksRjy5wItrib1YxaLJ16LFRuEO4Rb0mGhhK1bVCJe+h+U3Dij1CxUWVFyoh/snpqPaFLDNk0Zo74G3bdcfN+OsTbZe0NjJ1gTfhVSUy7KK9VfC6mIdrxBYfGRX/EQGjtMHRGgX8Df7eirLm5N7n8zgH321WQwhe/2fRslTTAVyVzSrmc4ln/iJ+aBQjsX2t+VtvfAnGuya2sHhXtxvsIGunGShDqZjp9 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-private memory without causing a host-kernel panic (e.g. currently only if the vm type is KVM_SW_PROTECTED_VM), allow `kvm_{read,write}_guest` to access gfns that are set to "private". If KVM cannot access guest-private 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 guest-private memory via kernel virtual addresses/the direct map. In the special case of guest_memfd, it does not have to worry about gfn->pfn mappings being invalidated, since guest_memfd pages are immovable. Signed-off-by: Patrick Roy --- include/linux/kvm_host.h | 5 +++ virt/kvm/kvm_main.c | 85 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) base-commit: 771df9ffadb8204e61d3e98f36c5067102aab78f diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 2a6679b46427..8f980aafd5ca 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2407,6 +2407,11 @@ static inline void kvm_prepare_memory_fault_exit(struct kvm_vcpu *vcpu, vcpu->run->memory_fault.flags |= KVM_MEMORY_EXIT_FLAG_PRIVATE; } +static inline bool kvm_can_access_gmem(struct kvm *kvm) +{ + return kvm->arch.vm_type == KVM_X86_SW_PROTECTED_VM; +} + #ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES static inline unsigned long kvm_get_memory_attributes(struct kvm *kvm, gfn_t gfn) { diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 8c7cbc9ec9ee..b3b3de70a4df 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 page *page; + void *kaddr; + + if (!kvm_can_access_gmem(kvm)) + return -EFAULT; + + r = kvm_gmem_get_pfn(kvm, memslot, gfn, &pfn, NULL); + + if (r < 0) + return -EFAULT; + + page = pfn_to_page(pfn); + lock_page(page); + kaddr = page_address(page) + offset; + memcpy(data, kaddr, len); + unlock_page(page); + put_page(page); + return 0; +} + +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) +{ + if (!kvm_can_access_gmem(vcpu->kvm)) { + kvm_prepare_memory_fault_exit(vcpu, gfn + offset, len, false, + false, true); + return -EFAULT; + } + return __kvm_read_guest_private_page(vcpu->kvm, memslot, gfn, data, offset, len); +} + 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,52 @@ 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 page *page; + void *kaddr; + + if (!kvm_can_access_gmem(kvm)) + return -EFAULT; + + r = kvm_gmem_get_pfn(kvm, memslot, gfn, &pfn, NULL); + + if (r < 0) + return -EFAULT; + + page = pfn_to_page(pfn); + lock_page(page); + kaddr = page_address(page) + offset; + memcpy(kaddr, data, len); + unlock_page(page); + put_page(page); + + return 0; +} + +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) +{ + if (!kvm_can_access_gmem(vcpu->kvm)) { + kvm_prepare_memory_fault_exit(vcpu, gfn + offset, len, true, + false, true); + return -EFAULT; + } + return __kvm_write_guest_private_page(vcpu->kvm, memslot, gfn, data, offset, len); +} + 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 +3487,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); From patchwork Tue Jul 9 13:20: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: 13727950 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 19F88C3DA41 for ; Tue, 9 Jul 2024 13:21:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9CD8B6B00A8; Tue, 9 Jul 2024 09:21:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 954DE6B00AA; Tue, 9 Jul 2024 09:21:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7A7976B00AB; Tue, 9 Jul 2024 09:21:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 52D5E6B00A8 for ; Tue, 9 Jul 2024 09:21:05 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id D09251A18DA for ; Tue, 9 Jul 2024 13:21:04 +0000 (UTC) X-FDA: 82320274848.06.73A3293 Received: from smtp-fw-52002.amazon.com (smtp-fw-52002.amazon.com [52.119.213.150]) by imf14.hostedemail.com (Postfix) with ESMTP id DD2E910001A for ; Tue, 9 Jul 2024 13:21:02 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=amazon.co.uk header.s=amazon201209 header.b=TRYTELdB; spf=pass (imf14.hostedemail.com: domain of "prvs=913fd7204=roypat@amazon.co.uk" designates 52.119.213.150 as permitted sender) smtp.mailfrom="prvs=913fd7204=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=1720531232; 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=/DNQp/z0GUBgLTV7ZRmRKhZQi2WgjcFO0AlHckf358s=; b=lDkCPF1mfZFXl8l+ea8I5uQRujQ6RBWrpvLJm7iGXGtFl/tzzBBrD/eRGpyXlel21iHKvc gXIKvTDecBrNQFc/eKxucBi1eBqA8ZLMUwAfCcJcuCcG7WCdgXMQxPQfAD/HnorKuHPhhu WNk0RrLsn8KzGvktwJg4+Nx+fuC07sA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1720531232; a=rsa-sha256; cv=none; b=Y/bzvv2t/m93rLIwIeYX5Xcoremk4z19J2tc7alvTNpdyBdDfuzwJEnA4JLqdT8zXI9Taw qOKBDc8c+NmhB2r4rn4YIOswsIpz8qvcOtDDXhEz7ePEtF1MV8V7vgf/Il8HK2UKRNwPOb yrnQdcGK0MKQynVsQYwq1Y+StMCZ5l0= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=amazon.co.uk header.s=amazon201209 header.b=TRYTELdB; spf=pass (imf14.hostedemail.com: domain of "prvs=913fd7204=roypat@amazon.co.uk" designates 52.119.213.150 as permitted sender) smtp.mailfrom="prvs=913fd7204=roypat@amazon.co.uk"; dmarc=pass (policy=quarantine) header.from=amazon.co.uk DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.co.uk; i=@amazon.co.uk; q=dns/txt; s=amazon201209; t=1720531262; x=1752067262; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=/DNQp/z0GUBgLTV7ZRmRKhZQi2WgjcFO0AlHckf358s=; b=TRYTELdBQH+qRpT5u2tcoKSXuWhZRosBu9ZITcrOF2+38mj1AX4heVYP hV3VJPUCYqTfjuGvqr0XKNLamXhVLyvAR6bQ6987mkWFlE6JxfEj1cBPW 3qfseaMrEk1iTh1gkY179Kd2KTgoEF8vyKazOzTrs0TbggXBfDWp8zBts 8=; X-IronPort-AV: E=Sophos;i="6.09,195,1716249600"; d="scan'208";a="644664049" 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-52002.iad7.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Jul 2024 13:21:01 +0000 Received: from EX19MTAUEA002.ant.amazon.com [10.0.0.204:55215] by smtpin.naws.us-east-1.prod.farcaster.email.amazon.dev [10.0.17.85:2525] with esmtp (Farcaster) id cb50a819-87b3-4fab-a17a-29e4e0b8d5a4; Tue, 9 Jul 2024 13:21:00 +0000 (UTC) X-Farcaster-Flow-ID: cb50a819-87b3-4fab-a17a-29e4e0b8d5a4 Received: from EX19D008UEC001.ant.amazon.com (10.252.135.232) by EX19MTAUEA002.ant.amazon.com (10.252.134.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Tue, 9 Jul 2024 13:21:00 +0000 Received: from EX19MTAUEC001.ant.amazon.com (10.252.135.222) by EX19D008UEC001.ant.amazon.com (10.252.135.232) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Tue, 9 Jul 2024 13:21:00 +0000 Received: from ua2d7e1a6107c5b.ant.amazon.com (172.19.88.180) by mail-relay.amazon.com (10.252.135.200) 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, 9 Jul 2024 13:20:57 +0000 From: Patrick Roy To: , , , , , CC: Patrick Roy , , , , , , , , , , , , , , , , , Subject: [RFC PATCH 2/8] kvm: use slowpath in gfn_to_hva_cache if memory is private Date: Tue, 9 Jul 2024 14:20:30 +0100 Message-ID: <20240709132041.3625501-3-roypat@amazon.co.uk> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240709132041.3625501-1-roypat@amazon.co.uk> References: <20240709132041.3625501-1-roypat@amazon.co.uk> MIME-Version: 1.0 X-Stat-Signature: w1ehet1h5h3fkzh16im3gtnxqom9pcwc X-Rspamd-Queue-Id: DD2E910001A X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1720531262-351064 X-HE-Meta: U2FsdGVkX1/8oC29ARNtNO549rqknBzbVvRa2jUTKPSytTF9LsfJoX+XSd7X2dJGYAMs4jOS0h0qVgessklK2OyriPX/lCVsQsPn7MJOr1A90j1Z68HefTGDTVMzPu8SHtYu5iuXXWjnZ+M9yr19Ix4xWO3Yh7brPYmCJZrPD4dRyAMSqly3VwWdiZRboNZbyeI0ItlTt2TDykpMu3XD4MyDagdSNQ8A39L/WSziUGg9Apl+6iJ/OvudGRWZo28F4yzoNBR2den40RRW4vTfbiaat4D3bf0kh35+oNupISYrSZ2s+NKd6BWE+iE+/gLGRWGiAy98XO+eodVNxdhNp83Z+s243+FNExyERXn7y1nUVIdURX9KPG4b6b6+Xb83yEjGFPCS0uO0lGCSEmPqYZbwJCl/yf0XHufngsoDKVQxW7/lGd4fTfYIx/3rNheVL2GnUzO+IFrWaUEao67a+e0xR+uzt0GLCCqvZal6V2dUbnmUZ/ATlvPd244faH13in8v7+KKrxc8/prIFS6XO1hJ4ckYJ0bGkrd5hPS5iKokUyyazA4IncZ+0uUv+9x5lCoKdBhg3YM7Q1z3wxHu5o3mNf0uTHzfsPYuCZ1BCh+YH/dRksm4guqi8rlFIom67soB53ROeF/04gSZrwdrKl1HjY5vc8yAhbVGq0zCbK5X/d2cxecON3GzmAl6xf4h7aWSWSKQozC+VLhaYxul/DC2B1hbYxnOIQc4nYLrql58CsEe8XcsY5W0v84w/OmiSv1uUbKHR2pDoWA7nt6zdL+m5a2dgMXdAPJ3nJ30jh+pE4tzEM4yA8gObOcVCjGl020+NMQpGaa8/bDxNwlu6BdyRPxAGfhaUSvIHwPbJVo3VUkwdJ30utm/xkOxB2jPH3owPZgx0ibZTqJ3Cle6yrFMEx1GPleXwU6xZx04CL2zytnaWY6krYkVWPVV7S6T1++MBvb2nyH+OP7JHGe rHnVuS+o huyEtpoeWjRD5NVM8CplHXqTcXCBIDsr5eYjgijNwjHdhVKaQPs+MQrxcMsUIAsvPTmcOcwRHau0BTGtBn0h9EATwUtsssxvkB0jwbMjAdvjLCey7mK0ukQGhc7Pv342iJsUI6LgS6sDiiFLMYm7saK1JPWu/2P3lMvkZ74DAHVWGWrFVFOVPPDeLvCYh5igeM7t+shplfTWA/aeyxbYxCPqWFRAD1HhumwwiR/IKUXizwPUFVx4iGKvpGFBeVGV5l9FnAoWA8ITuQlBL0FpSY2h8qLDLHZwG6lu1rp8dlntVkSFPEfWvPwR3YLedGl+9Q3HeyclR1WnEkuok2zQuF6HmRy4j0Ro/ThpdSAzEQuewT8lFIcl/sfjsv/PBypUzzHw0xVHxO6Rk3WtMKCKiBQ7oAf8LBTvq87zOY4AzaTvWgQfr4nysH2+3L9YC9oW2Zhaa 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: Currently, KVM uses gfn_to_hva_caches to cache gfn->memslot->userspace host virtual address (uhva) translations. If a gfn is backed by guest_memfd however, there is no uhva-equivalent item we could possible cache, since accesses go through a file descriptor instead of a VMA. Thus, we effectively disable gfn_to_hva_caches in the case where gfns are gmem-backed, and instead do a gfn->pfn translation on the fly by calling `kvm_{read,write}_guest` inside `kvm_{read,write}_guest_cached`. Signed-off-by: Patrick Roy --- virt/kvm/kvm_main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index b3b3de70a4df..4357f7cdf040 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3603,7 +3603,7 @@ int kvm_write_guest_offset_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, if (kvm_is_error_hva(ghc->hva)) return -EFAULT; - if (unlikely(!ghc->memslot)) + if (unlikely(!ghc->memslot || kvm_mem_is_private(kvm, gpa_to_gfn(gpa)))) return kvm_write_guest(kvm, gpa, data, len); r = __copy_to_user((void __user *)ghc->hva + offset, data, len); @@ -3641,7 +3641,7 @@ int kvm_read_guest_offset_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, if (kvm_is_error_hva(ghc->hva)) return -EFAULT; - if (unlikely(!ghc->memslot)) + if (unlikely(!ghc->memslot || kvm_mem_is_private(kvm, gpa_to_gfn(gpa)))) return kvm_read_guest(kvm, gpa, data, len); r = __copy_from_user(data, (void __user *)ghc->hva + offset, len); From patchwork Tue Jul 9 13:20:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Roy X-Patchwork-Id: 13727953 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 886C0C3DA47 for ; Tue, 9 Jul 2024 13:21:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8BB626B00B0; Tue, 9 Jul 2024 09:21:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7F7526B00B1; Tue, 9 Jul 2024 09:21:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 55DE06B00B2; Tue, 9 Jul 2024 09:21:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 1AF676B00B0 for ; Tue, 9 Jul 2024 09:21:18 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id D5770A503D for ; Tue, 9 Jul 2024 13:21:17 +0000 (UTC) X-FDA: 82320275394.21.30028D0 Received: from smtp-fw-9102.amazon.com (smtp-fw-9102.amazon.com [207.171.184.29]) by imf30.hostedemail.com (Postfix) with ESMTP id DE90B8001D for ; Tue, 9 Jul 2024 13:21:15 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=amazon.co.uk header.s=amazon201209 header.b=IBGmho88; dmarc=pass (policy=quarantine) header.from=amazon.co.uk; spf=pass (imf30.hostedemail.com: domain of "prvs=913fd7204=roypat@amazon.co.uk" designates 207.171.184.29 as permitted sender) smtp.mailfrom="prvs=913fd7204=roypat@amazon.co.uk" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1720531253; a=rsa-sha256; cv=none; b=lQiuocnswDsTMV6m0PNkM0VueYto4+5l9jsa0vXkVM7kNdmeJtTihBNcSP6MgpB2G+iJkn amtqkgJKLUsQiB8UmOX7e7Dm9+jgTHssoyTNHPnPopiyoU6ZWIX2gQLaIcaEBGuOlvUiQ1 VYRJ5Y3eePCUudqQx1e/+F/efHbx5ZI= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=amazon.co.uk header.s=amazon201209 header.b=IBGmho88; dmarc=pass (policy=quarantine) header.from=amazon.co.uk; spf=pass (imf30.hostedemail.com: domain of "prvs=913fd7204=roypat@amazon.co.uk" designates 207.171.184.29 as permitted sender) smtp.mailfrom="prvs=913fd7204=roypat@amazon.co.uk" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1720531253; 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=j7xh8Fp0rGL+JNyIcONb4iIMzBYUq25BVxGN/Q+52J4=; b=Z5LjGe5dMZquV3UrfdDLvQ/RDLxp305o9d2x3xW4BCj7lQc6syXZu3KUwnJAmbUxlV0qY+ lQ508aF2ZsMEUIJsZxSDXjy4iCS3ibNt3fha+PICfkWhsav0uXnPVr+b1Tju2hNKQjlbB7 Wq0mTxtS/PgE2jQcCk48xd4oLwVPywg= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.co.uk; i=@amazon.co.uk; q=dns/txt; s=amazon201209; t=1720531276; x=1752067276; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=j7xh8Fp0rGL+JNyIcONb4iIMzBYUq25BVxGN/Q+52J4=; b=IBGmho88qkJBK1FznX1Awd2MCCKuFbT+w20JujnLxX1RJto2mAXbmuGX EZkpIakmXASfZfgGtkrDebXGFXaVfAFCeCb+2XKAYdqNt3juF7EzhT5ox s+W4NsgRiO8dKvqsMie6mKdgmNafALOBl0W3+e/Q0m5hrJUk8/DClH3cM I=; X-IronPort-AV: E=Sophos;i="6.09,195,1716249600"; d="scan'208";a="432897833" Received: from pdx4-co-svc-p1-lb2-vlan3.amazon.com (HELO smtpout.prod.us-east-1.prod.farcaster.email.amazon.dev) ([10.25.36.214]) by smtp-border-fw-9102.sea19.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Jul 2024 13:21:09 +0000 Received: from EX19MTAUEA001.ant.amazon.com [10.0.0.204:54070] by smtpin.naws.us-east-1.prod.farcaster.email.amazon.dev [10.0.19.12:2525] with esmtp (Farcaster) id 9adfff71-39c0-4eea-be63-6a4028520ef9; Tue, 9 Jul 2024 13:21:07 +0000 (UTC) X-Farcaster-Flow-ID: 9adfff71-39c0-4eea-be63-6a4028520ef9 Received: from EX19D008UEA004.ant.amazon.com (10.252.134.191) 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, 9 Jul 2024 13:21:04 +0000 Received: from EX19MTAUEC001.ant.amazon.com (10.252.135.222) by EX19D008UEA004.ant.amazon.com (10.252.134.191) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Tue, 9 Jul 2024 13:21:04 +0000 Received: from ua2d7e1a6107c5b.ant.amazon.com (172.19.88.180) by mail-relay.amazon.com (10.252.135.200) 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, 9 Jul 2024 13:21:01 +0000 From: Patrick Roy To: , , , , , CC: Patrick Roy , , , , , , , , , , , , , , , , , Subject: [RFC PATCH 3/8] kvm: pfncache: enlighten about gmem Date: Tue, 9 Jul 2024 14:20:31 +0100 Message-ID: <20240709132041.3625501-4-roypat@amazon.co.uk> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240709132041.3625501-1-roypat@amazon.co.uk> References: <20240709132041.3625501-1-roypat@amazon.co.uk> MIME-Version: 1.0 X-Rspamd-Queue-Id: DE90B8001D X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: k99fyeygtkxwkzhb8witxhrc7d4s1juz X-HE-Tag: 1720531275-574605 X-HE-Meta: U2FsdGVkX1/cP7J3nEbTUxWdNf4dEPOQDIF9kLpbD/rvzjP5WkYeNNx3KZr/o75gG3OVbL2hASrEdZV+biLzJO0QAjX9HL/CZ0YGd3B8LkzaQBteNC1q/rOiD/TX295t7F8YyyXytY72aRqtEp+XRQ61DSJEe3zDsUuz+m18Oa4wh8eNaYTXCGV+Y2aug5V9ah0iNcSXH77y/+uhV1PM2OYwE09t06rgVm1RcXCSiVhmxpkzDUvY/t7vBv0HPmo9oTdscy7N8r4goq3TGYOAxF4ipPSNnUQtZL289Jx+58rPgi5IkY9aZrb2MgsDHf5qMk9qA0xI9TZLvNaTHz53/ewteeOyn48UbG9rJLQqrimSC5aVHdijaJwMfoLtNJ9nMf5hXt4AXywoKju4yqB46L0QT27Sq4iYro4R//nB2ioZhI0sp/Lo++34hzW3nQa2HBUtVaGHPjRJb4dNFWfXcAdyQ0+Ap3NuP7ijSOv969TExXhFiuj/OaR1Mj6KbcHud70Ebw/nXd9S33XrY85PHiMdHF4HgXYRrbatNXukSx6Exx03jX2kV/iHPj542nxGB5z19CaLe+WKEfqf2CjXDbe2qLodlPEx5r/1Np7mIDch8pQdmb40BYhrKWAB47UfsxqDEP5OxjoejOBjB/jzNNLIKEp/a4GEpTIuprWAYb7EjhHBwImQx3z5k/KOXqkwsDoXCqhBJSsJKtPuunmQr5OQfqRFJJVZ/Ty7VcvdUhHvM4O/cO+SxDL0IIoJ0EcmIUzmonLPcWv1tqZeyyzod9oc/7Tv3BZuHN0W6Pyz5ttD/plhN054b4aSsS9TZ6ugLYWeUmHft+CRjMzOrNBgBhAGQk4ZWkQ+jxm5PVE2zkjkDSUVo6QivTXYDle0fNMTl4w96Bb+Z7SGHtGzbl2AOLQK0NAs/P10rRgqsDgOiLfQdwL3129NQo6utCeb31kMMTXSDMzeN/7s52axUjs 3vU/ecgX o0U9TRhE2ud3/1o0/Ux8W17SoTboshc2NiuOhooAH2x4Mgk5rDKkhc/cuWD0+/39/KXKi2SPX9jXot3H7MKClXe5NEKHiIQCSWtxsUZ1CC+V7l8Y70WcdBIxTFXgfV9h2bama1436LPD+6yLANA3IGWHJCeiSWdFbiPQIn47X6YIDcVQ1vz+FVlAnZQFnKdfLmlBn25pr0KiVXqSvmpjMlVkp1HJC2WHOsFgKUyZ0nUvdrpT0wvvpfayWiBO/rsqhJi5ddWawty+yKycNZuT3EHpxcFlhuzqQqhIImQh3qyCT8d/Px+1JgOSDjRroq4GJlOByvakZW/5gRsInK5iCSbM8b6nqxNI/nZ466MY1HbYGQRs= 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: KVM uses gfn_to_pfn_caches to cache translations from gfn all the way to the pfn (for example, kvm-clock caches the page storing the page used for guest/host communication this way). Unlike the gfn_to_hva_cache, where no equivalent caching semantics were possible to gmem-backed gfns (see also 858e8068a750 ("kvm: pfncache: enlighten about gmem")), here it is possible to simply cache the pfn returned by `kvm_gmem_get_pfn`. Additionally, gfn_to_pfn_caches now invalidate whenever a cached gfn's attributes are flipped from shared to private (or vice-versa). Signed-off-by: Patrick Roy --- include/linux/kvm_types.h | 1 + virt/kvm/pfncache.c | 41 +++++++++++++++++++++++++++++++++------ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/include/linux/kvm_types.h b/include/linux/kvm_types.h index 827ecc0b7e10..8f85f01f6bb0 100644 --- a/include/linux/kvm_types.h +++ b/include/linux/kvm_types.h @@ -70,6 +70,7 @@ struct gfn_to_pfn_cache { kvm_pfn_t pfn; bool active; bool valid; + bool is_private; }; #ifdef KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE diff --git a/virt/kvm/pfncache.c b/virt/kvm/pfncache.c index f0039efb9e1e..6430e0a49558 100644 --- a/virt/kvm/pfncache.c +++ b/virt/kvm/pfncache.c @@ -90,6 +90,9 @@ bool kvm_gpc_check(struct gfn_to_pfn_cache *gpc, unsigned long len) if (!kvm_gpc_is_valid_len(gpc->gpa, gpc->uhva, len)) return false; + if (gpc->is_private != kvm_mem_is_private(gpc->kvm, gpa_to_gfn(gpc->gpa))) + return false; + if (!gpc->valid) return false; @@ -159,6 +162,7 @@ static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cache *gpc) kvm_pfn_t new_pfn = KVM_PFN_ERR_FAULT; void *new_khva = NULL; unsigned long mmu_seq; + gfn_t gfn; lockdep_assert_held(&gpc->refresh_lock); @@ -173,6 +177,7 @@ static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cache *gpc) do { mmu_seq = gpc->kvm->mmu_invalidate_seq; + gfn = gpa_to_gfn(gpc->gpa); smp_rmb(); write_unlock_irq(&gpc->lock); @@ -197,10 +202,19 @@ static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cache *gpc) cond_resched(); } - /* We always request a writeable mapping */ - new_pfn = hva_to_pfn(gpc->uhva, false, false, NULL, true, NULL); - if (is_error_noslot_pfn(new_pfn)) - goto out_error; + if (gpc->is_private) { + int r = kvm_gmem_get_pfn(gpc->kvm, gfn_to_memslot(gpc->kvm, gfn), gfn, + &new_pfn, NULL); + + if (r) + goto out_error; + } else { + /* We always request a writeable mapping */ + new_pfn = hva_to_pfn(gpc->uhva, false, false, NULL, + true, NULL); + if (is_error_noslot_pfn(new_pfn)) + goto out_error; + } /* * Obtain a new kernel mapping if KVM itself will access the @@ -252,6 +266,7 @@ static int __kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, gpa_t gpa, unsigned l unsigned long old_uhva; kvm_pfn_t old_pfn; bool hva_change = false; + bool old_private; void *old_khva; int ret; @@ -271,8 +286,21 @@ static int __kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, gpa_t gpa, unsigned l old_pfn = gpc->pfn; old_khva = (void *)PAGE_ALIGN_DOWN((uintptr_t)gpc->khva); old_uhva = PAGE_ALIGN_DOWN(gpc->uhva); + old_private = gpc->is_private; + + gpc->is_private = kvm_mem_is_private(gpc->kvm, gpa_to_gfn(gpa)); + + if (gpc->is_private && !kvm_can_access_gmem(gpc->kvm)) { + ret = -EFAULT; + goto out_unlock; + } if (kvm_is_error_gpa(gpa)) { + if (WARN_ON_ONCE(gpc->is_private)) { + ret = -EINVAL; + goto out_unlock; + } + page_offset = offset_in_page(uhva); gpc->gpa = INVALID_GPA; @@ -316,9 +344,10 @@ static int __kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, gpa_t gpa, unsigned l /* * If the userspace HVA changed or the PFN was already invalid, - * drop the lock and do the HVA to PFN lookup again. + * drop the lock and do the HVA to PFN lookup again. Also + * recompute the pfn if the gfn changed from shared to private (or vice-versa). */ - if (!gpc->valid || hva_change) { + if (!gpc->valid || hva_change || gpc->is_private != old_private) { ret = hva_to_pfn_retry(gpc); } else { /* From patchwork Tue Jul 9 13:20:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Roy X-Patchwork-Id: 13727952 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 F25F0C2BD09 for ; Tue, 9 Jul 2024 13:21:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8B9F26B00AE; Tue, 9 Jul 2024 09:21:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8429B6B00B0; Tue, 9 Jul 2024 09:21:17 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 695EB6B00B1; Tue, 9 Jul 2024 09:21:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 46DF96B00AE for ; Tue, 9 Jul 2024 09:21:17 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 9DACD161903 for ; Tue, 9 Jul 2024 13:21:16 +0000 (UTC) X-FDA: 82320275352.19.AD40811 Received: from smtp-fw-52004.amazon.com (smtp-fw-52004.amazon.com [52.119.213.154]) by imf06.hostedemail.com (Postfix) with ESMTP id 741A018001B for ; Tue, 9 Jul 2024 13:21:14 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=amazon.co.uk header.s=amazon201209 header.b=GkdSXk8L; spf=pass (imf06.hostedemail.com: domain of "prvs=913fd7204=roypat@amazon.co.uk" designates 52.119.213.154 as permitted sender) smtp.mailfrom="prvs=913fd7204=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=1720531258; 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=OgAfYDQpLUE9XUDFTSU5X0VWjGezZLvoUAAPFhPmJtQ=; b=Bg9IMbVOKzCEdRCLej4bV7jVSPwWguzELa1jDcR1L+HUS3eA/71PkoWbnL5bvs0NfhWT5T Z1vhRD4mixLy4azR7mTi19LHRBywb/W6eXV/oSeJylx2861Ehicr3zs/4Tm9HvcNm7SbWK yueZTSGJC/ba/EXZyHD2qBr4A3Cw4WE= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=amazon.co.uk header.s=amazon201209 header.b=GkdSXk8L; spf=pass (imf06.hostedemail.com: domain of "prvs=913fd7204=roypat@amazon.co.uk" designates 52.119.213.154 as permitted sender) smtp.mailfrom="prvs=913fd7204=roypat@amazon.co.uk"; dmarc=pass (policy=quarantine) header.from=amazon.co.uk ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1720531258; a=rsa-sha256; cv=none; b=A71LNDEztg1GC03ApIFnmq2kaa5aZb6lPYO84aL4ztqYBrN6nFtmHP+IrVUVFSFsyDBl5N 8LKLVpyK76h6JZusKKMoQ01sEurwoDg7UqVPJpKwCk14Sr5KHX3VZ8p022+z5T/ue1dAF6 T/QIXPu49mXMqmbd5Yw4oC6cWyojaU4= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.co.uk; i=@amazon.co.uk; q=dns/txt; s=amazon201209; t=1720531274; x=1752067274; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=OgAfYDQpLUE9XUDFTSU5X0VWjGezZLvoUAAPFhPmJtQ=; b=GkdSXk8LlIwg1Kmq6Vi7DvXx54EzN/bGMH5q3nn6Oqv/u4zENz8XHdhi sApr6OVPdYiXVqGaaehc8K+l3vtnQlAj230ZK6PjO5UxhIGm7qWKK83aZ /5fksCl2vPsgQQSbpjYI10J0+/QLeEvaUowQ0Mnn8801tUi21J+Rj8vpF 8=; X-IronPort-AV: E=Sophos;i="6.09,195,1716249600"; d="scan'208";a="217222162" Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO smtpout.prod.us-east-1.prod.farcaster.email.amazon.dev) ([10.43.8.2]) by smtp-border-fw-52004.iad7.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Jul 2024 13:21:14 +0000 Received: from EX19MTAUEC002.ant.amazon.com [10.0.0.204:13938] by smtpin.naws.us-east-1.prod.farcaster.email.amazon.dev [10.0.50.89:2525] with esmtp (Farcaster) id 57bc0aa8-df5f-4f40-9cd0-d79eb3ced12e; Tue, 9 Jul 2024 13:21:13 +0000 (UTC) X-Farcaster-Flow-ID: 57bc0aa8-df5f-4f40-9cd0-d79eb3ced12e Received: from EX19D008UEA004.ant.amazon.com (10.252.134.191) by EX19MTAUEC002.ant.amazon.com (10.252.135.253) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Tue, 9 Jul 2024 13:21:07 +0000 Received: from EX19MTAUEC001.ant.amazon.com (10.252.135.222) by EX19D008UEA004.ant.amazon.com (10.252.134.191) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Tue, 9 Jul 2024 13:21:07 +0000 Received: from ua2d7e1a6107c5b.ant.amazon.com (172.19.88.180) by mail-relay.amazon.com (10.252.135.200) 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, 9 Jul 2024 13:21:04 +0000 From: Patrick Roy To: , , , , , CC: Patrick Roy , , , , , , , , , , , , , , , , , Subject: [RFC PATCH 4/8] kvm: x86: support walking guest page tables in gmem Date: Tue, 9 Jul 2024 14:20:32 +0100 Message-ID: <20240709132041.3625501-5-roypat@amazon.co.uk> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240709132041.3625501-1-roypat@amazon.co.uk> References: <20240709132041.3625501-1-roypat@amazon.co.uk> MIME-Version: 1.0 X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: 741A018001B X-Stat-Signature: uzzk7ns5iwfbjjo43ayi4em9rj9m5zwn X-Rspam-User: X-HE-Tag: 1720531274-63084 X-HE-Meta: U2FsdGVkX19Fu7R4Mu3hsXxjTW/DKDs4QaI7RNsGplbD9ts00lPUb5UuA44SdEGz8tkazMMkJ4aZXosH36yWsqc4ri8/GtCZuDA7zbwfK1gKR7PEi6e/8VnKbl6V2itUZurcDHJ/mkBMIPpRMWjINpX4uwoiOB2L1QDUweFOaIr6JC2UvZUeZuhWOCuLgiN1dh9buHUy9+TuQG7U+nOcR0EcQeS+RDZR5TSQvH72vqO9c9tcdDqsn9ExzvBi2gn42uc/Sx4WSmP302S23U4gqiA1rxSyYOJmv7Ex4CAFwjlO+G3od8C9iIfWU3dB4KBYTVAMyp74jGiwZ9000EKo1CXpMvfkQOCsN6U1eH8H13krXlZO+2fTIhbKFu4jVqtrGA/LEPy1ZJ998AOhJcIvifhS5mrIY9RhIGCx4ZZ9hc8Karsa7a44NC427BSr9GSu3ajBhAon2wTGl3209PHF2bA51il/sIGyh7hruLWezZ6GYpDhpdUvTn79if8+R0q7c/k/5EOkasfnyx4n/39XKB34O00qBhZ/sV1hcsy2wxujTbJLVZSch+cMDxOzOl8z98X5gSqilXMT/3WYxBynm/A6RQbJafzGdxTjzYDoY+RUIRhr/uuBll12xcFHWHlJhdvHIgGLOVctGl+px5cTs31wLnr7O6BG8M29IYD2tog5hFplnZF0OqXN2cAGXt+41+ZF3QhbhPrPoWrHl5IzRCjjHeR/5FGYexvCLBMzdqfAWvvVvN/vZcFPhlewrGk7vT0znbIdJEDcEDdI+POfssYxfR7ZmueRgFV5ddjZyiW2cf+ePP4sqvSyzuJLNsO7U4uLbRStFPqM0cT3wKOiQm+Jn1U/swgBfYIJRYghCEJYw/WT5W4koekFy/ttaC8/DlcAhl/YBYKBRSJs/lx+LDEGxe1v/SwdUQhuraHqjz9DOq7BwE47WO2uur3zcRQBfrcEU8iyWgfIAbDcVPx baZNa3PH YbX6XO+HzyPQhYrGgEpuq8Q3+NZLHK9Z/TSxpGjb8z+4p//B+FzAIXNe67V9YKaBjPZgBuGoE0sqKUmPO0V1qWAFzn4IQYy5FwjcST8ZRlgLByWCdCiSIonQSStaHrU9/hIgb0rKlYQopot0r4PhYIBRUs06+tfSLnHP8lo67p+4xnvoRDWc6ci3/YnudnRFFggsAv+OXSpJkVjDtJmQBU87qdQaUUnXMkxyfh99noFt6Fsft9LdA8qCL3497RsVl77DSzNBy+7m3XzvjBcW3FSCyGGoeAHEBFu6kJhUGt1rUUjIo1KDBqOSfP9yIacuix9oJ5KLLmBf9H6GIM5PQBz+EIlKUO6eIllyKg3iPZbOE9NWGSx8FQgnduA== 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: Update the logic in paging_tmpl.h to work with guest_private memory. If KVM cannot access gmem and the guest's page tables are in gfns marked as private, then error out. Let the guest page table walker access gmem by making it use gfn_to_pfn_caches, which are already gmem aware, and will later also handle on-demand mapping of gmem once it supports being removed from the direct map. We re-use the gfn_to_pfn_cache here to avoid implementing yet another remapping solution to support the cmpxchg used to set the "accessed" bit on guest PTEs. The only case that now needs some special handling is page tables in read-only memslots, as gfn_to_pfn_caches cannot be used for readonly memory. In this case, use kvm_vcpu_read_guest (which is also gmem aware), as there is no need to cache the gfn->pfn translation in this case (there is no need to do a cmpxchg on the PTE as the walker does not set the accessed bit for read-only ptes). Signed-off-by: Patrick Roy --- arch/x86/kvm/mmu/paging_tmpl.h | 94 ++++++++++++++++++++++++++++------ 1 file changed, 77 insertions(+), 17 deletions(-) diff --git a/arch/x86/kvm/mmu/paging_tmpl.h b/arch/x86/kvm/mmu/paging_tmpl.h index 69941cebb3a8..ddf3b4bd479e 100644 --- a/arch/x86/kvm/mmu/paging_tmpl.h +++ b/arch/x86/kvm/mmu/paging_tmpl.h @@ -84,7 +84,7 @@ struct guest_walker { pt_element_t ptes[PT_MAX_FULL_LEVELS]; pt_element_t prefetch_ptes[PTE_PREFETCH_NUM]; gpa_t pte_gpa[PT_MAX_FULL_LEVELS]; - pt_element_t __user *ptep_user[PT_MAX_FULL_LEVELS]; + struct gfn_to_pfn_cache ptep_caches[PT_MAX_FULL_LEVELS]; bool pte_writable[PT_MAX_FULL_LEVELS]; unsigned int pt_access[PT_MAX_FULL_LEVELS]; unsigned int pte_access; @@ -201,7 +201,7 @@ static int FNAME(update_accessed_dirty_bits)(struct kvm_vcpu *vcpu, { unsigned level, index; pt_element_t pte, orig_pte; - pt_element_t __user *ptep_user; + struct gfn_to_pfn_cache *pte_cache; gfn_t table_gfn; int ret; @@ -210,10 +210,12 @@ static int FNAME(update_accessed_dirty_bits)(struct kvm_vcpu *vcpu, return 0; for (level = walker->max_level; level >= walker->level; --level) { + unsigned long flags; + pte = orig_pte = walker->ptes[level - 1]; table_gfn = walker->table_gfn[level - 1]; - ptep_user = walker->ptep_user[level - 1]; - index = offset_in_page(ptep_user) / sizeof(pt_element_t); + pte_cache = &walker->ptep_caches[level - 1]; + index = offset_in_page(pte_cache->khva) / sizeof(pt_element_t); if (!(pte & PT_GUEST_ACCESSED_MASK)) { trace_kvm_mmu_set_accessed_bit(table_gfn, index, sizeof(pte)); pte |= PT_GUEST_ACCESSED_MASK; @@ -246,11 +248,26 @@ static int FNAME(update_accessed_dirty_bits)(struct kvm_vcpu *vcpu, if (unlikely(!walker->pte_writable[level - 1])) continue; - ret = __try_cmpxchg_user(ptep_user, &orig_pte, pte, fault); + read_lock_irqsave(&pte_cache->lock, flags); + while (!kvm_gpc_check(pte_cache, sizeof(pte))) { + read_unlock_irqrestore(&pte_cache->lock, flags); + + ret = kvm_gpc_refresh(pte_cache, sizeof(pte)); + if (ret) + return ret; + + read_lock_irqsave(&pte_cache->lock, flags); + } + ret = __try_cmpxchg((pt_element_t *)pte_cache->khva, &orig_pte, pte, sizeof(pte)); + + if (!ret) + kvm_gpc_mark_dirty_in_slot(pte_cache); + + read_unlock_irqrestore(&pte_cache->lock, flags); + if (ret) return ret; - kvm_vcpu_mark_page_dirty(vcpu, table_gfn); walker->ptes[level - 1] = pte; } return 0; @@ -296,6 +313,12 @@ static inline bool FNAME(is_last_gpte)(struct kvm_mmu *mmu, return gpte & PT_PAGE_SIZE_MASK; } + +static void FNAME(walk_deactivate_gpcs)(struct guest_walker *walker) { + for (unsigned int level = 0; level < PT_MAX_FULL_LEVELS; ++level) + kvm_gpc_deactivate(&walker->ptep_caches[level]); +} + /* * Fetch a guest pte for a guest virtual address, or for an L2's GPA. */ @@ -305,7 +328,6 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker, { int ret; pt_element_t pte; - pt_element_t __user *ptep_user; gfn_t table_gfn; u64 pt_access, pte_access; unsigned index, accessed_dirty, pte_pkey; @@ -320,8 +342,17 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker, u16 errcode = 0; gpa_t real_gpa; gfn_t gfn; + struct gfn_to_pfn_cache *pte_cache; trace_kvm_mmu_pagetable_walk(addr, access); + + for (unsigned int level = 0; level < PT_MAX_FULL_LEVELS; ++level) { + pte_cache = &walker->ptep_caches[level]; + + memset(pte_cache, 0, sizeof(*pte_cache)); + kvm_gpc_init(pte_cache, vcpu->kvm); + } + retry_walk: walker->level = mmu->cpu_role.base.level; pte = kvm_mmu_get_guest_pgd(vcpu, mmu); @@ -362,11 +393,13 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker, do { struct kvm_memory_slot *slot; - unsigned long host_addr; + unsigned long flags; pt_access = pte_access; --walker->level; + pte_cache = &walker->ptep_caches[walker->level - 1]; + index = PT_INDEX(addr, walker->level); table_gfn = gpte_to_gfn(pte); offset = index * sizeof(pt_element_t); @@ -396,15 +429,36 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker, if (!kvm_is_visible_memslot(slot)) goto error; - host_addr = gfn_to_hva_memslot_prot(slot, gpa_to_gfn(real_gpa), - &walker->pte_writable[walker->level - 1]); - if (unlikely(kvm_is_error_hva(host_addr))) - goto error; + /* + * gfn_to_pfn_cache expects the memory to be writable. However, + * if the memory is not writable, we do not need caching in the + * first place, as we only need it to later potentially write + * the access bit (which we cannot do anyway if the memory is + * readonly). + */ + if (slot->flags & KVM_MEM_READONLY) { + if (kvm_vcpu_read_guest(vcpu, real_gpa + offset, &pte, sizeof(pte))) + goto error; + } else { + if (kvm_gpc_activate(pte_cache, real_gpa + offset, + sizeof(pte))) + goto error; - ptep_user = (pt_element_t __user *)((void *)host_addr + offset); - if (unlikely(__get_user(pte, ptep_user))) - goto error; - walker->ptep_user[walker->level - 1] = ptep_user; + read_lock_irqsave(&pte_cache->lock, flags); + while (!kvm_gpc_check(pte_cache, sizeof(pte))) { + read_unlock_irqrestore(&pte_cache->lock, flags); + + if (kvm_gpc_refresh(pte_cache, sizeof(pte))) + goto error; + + read_lock_irqsave(&pte_cache->lock, flags); + } + + pte = *(pt_element_t *)pte_cache->khva; + read_unlock_irqrestore(&pte_cache->lock, flags); + + walker->pte_writable[walker->level - 1] = true; + } trace_kvm_mmu_paging_element(pte, walker->level); @@ -467,13 +521,19 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker, addr, write_fault); if (unlikely(ret < 0)) goto error; - else if (ret) + else if (ret) { + FNAME(walk_deactivate_gpcs)(walker); goto retry_walk; + } } + FNAME(walk_deactivate_gpcs)(walker); + return 1; error: + FNAME(walk_deactivate_gpcs)(walker); + errcode |= write_fault | user_fault; if (fetch_fault && (is_efer_nx(mmu) || is_cr4_smep(mmu))) errcode |= PFERR_FETCH_MASK; From patchwork Tue Jul 9 13:20:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Roy X-Patchwork-Id: 13727954 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 D134CC3DA41 for ; Tue, 9 Jul 2024 13:21:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 733186B00B3; Tue, 9 Jul 2024 09:21:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6BBFB6B00B4; Tue, 9 Jul 2024 09:21:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 537DA6B00B5; Tue, 9 Jul 2024 09:21:26 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 2BF736B00B3 for ; Tue, 9 Jul 2024 09:21:26 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id DC2EA1618C2 for ; Tue, 9 Jul 2024 13:21:25 +0000 (UTC) X-FDA: 82320275730.01.5CDF233 Received: from smtp-fw-9105.amazon.com (smtp-fw-9105.amazon.com [207.171.188.204]) by imf20.hostedemail.com (Postfix) with ESMTP id C89A11C001B for ; Tue, 9 Jul 2024 13:21:22 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=amazon.co.uk header.s=amazon201209 header.b=SJ4rfNYq; dmarc=pass (policy=quarantine) header.from=amazon.co.uk; spf=pass (imf20.hostedemail.com: domain of "prvs=913fd7204=roypat@amazon.co.uk" designates 207.171.188.204 as permitted sender) smtp.mailfrom="prvs=913fd7204=roypat@amazon.co.uk" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1720531260; a=rsa-sha256; cv=none; b=rkvWypcqIlzXy/O6UqIVyj5aRBIxSge7rcfSQoYQWMy2urG/lKNr1B8mCyaajV2as6EKdO xw2XuLdyYnYDwFeIxz8cJVRXKwsAAzvnXsoeTGL7Y67BdtYwFf+w/FwusbGaSfdziJYVe0 21OW1uyX1BBmNSX7Cf0i5PW6Um3YDMU= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=amazon.co.uk header.s=amazon201209 header.b=SJ4rfNYq; dmarc=pass (policy=quarantine) header.from=amazon.co.uk; spf=pass (imf20.hostedemail.com: domain of "prvs=913fd7204=roypat@amazon.co.uk" designates 207.171.188.204 as permitted sender) smtp.mailfrom="prvs=913fd7204=roypat@amazon.co.uk" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1720531260; 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=+mzO007algvViExHdnpknhkipITD3cHhlfbBcPgvLYk=; b=q83OdqtzFpcXbslKGNJJmaS7vFxgA0rlMDnjk1vE81nT4xjpZUu0nrjgFpxgDB1b/aGP34 kpvYnlOmcaAcWeyelLOVgnmrVC27eXWtqSG2ydRryQslLhml5kvW0pQhfTF6TDL45wn305 wMh0h+zh6L1Yko2qadMpMglne7yQt7M= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.co.uk; i=@amazon.co.uk; q=dns/txt; s=amazon201209; t=1720531283; x=1752067283; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=+mzO007algvViExHdnpknhkipITD3cHhlfbBcPgvLYk=; b=SJ4rfNYqkGBW89jP4cSGSgwSqCGGFeraGPQGashE8UVRJol6I0HjpRPE ZKIvfahZlC7ki/iojpPdOr4CVcPyZSJniPUKUatv1wdSmvy3ikqrp2QOY 0BBbE0rdpBQt+luO4kYI/0djHT7jlSBT/xH8wAyC//2eFVhNLxPzgEnTI Q=; X-IronPort-AV: E=Sophos;i="6.09,195,1716249600"; d="scan'208";a="739970201" 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-9105.sea19.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Jul 2024 13:21:15 +0000 Received: from EX19MTAUEC001.ant.amazon.com [10.0.0.204:50152] by smtpin.naws.us-east-1.prod.farcaster.email.amazon.dev [10.0.92.61:2525] with esmtp (Farcaster) id 1a418e6e-d33b-4bef-8883-26ade4dd88c5; Tue, 9 Jul 2024 13:21:13 +0000 (UTC) X-Farcaster-Flow-ID: 1a418e6e-d33b-4bef-8883-26ade4dd88c5 Received: from EX19D008UEA004.ant.amazon.com (10.252.134.191) by EX19MTAUEC001.ant.amazon.com (10.252.135.222) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Tue, 9 Jul 2024 13:21:10 +0000 Received: from EX19MTAUEC001.ant.amazon.com (10.252.135.222) by EX19D008UEA004.ant.amazon.com (10.252.134.191) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Tue, 9 Jul 2024 13:21:10 +0000 Received: from ua2d7e1a6107c5b.ant.amazon.com (172.19.88.180) by mail-relay.amazon.com (10.252.135.200) 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, 9 Jul 2024 13:21:07 +0000 From: Patrick Roy To: , , , , , CC: Patrick Roy , , , , , , , , , , , , , , , , , Subject: [RFC PATCH 5/8] kvm: gmem: add option to remove guest private memory from direct map Date: Tue, 9 Jul 2024 14:20:33 +0100 Message-ID: <20240709132041.3625501-6-roypat@amazon.co.uk> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240709132041.3625501-1-roypat@amazon.co.uk> References: <20240709132041.3625501-1-roypat@amazon.co.uk> MIME-Version: 1.0 X-Rspamd-Queue-Id: C89A11C001B X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: prnsmyiyrtf36opwmz69ca9pqfxmu4r8 X-HE-Tag: 1720531282-460892 X-HE-Meta: U2FsdGVkX1/zVfGZUMzDdMrHXTRKP+hTkQVZrDKcOvA6oI6MJDFpHPCDfW3XgMscTWTy7sF8O35v3q+vNCPX9HmDohE7KJXTwcwWBAU0A2tbKX0bQyH0g2flpbsjoDdjMGv4Pqfe01VNyu6SuuVBty2Psz9od3+C0f9GNsUbVEA47AvALwwK+ZF0IC7Mxr7NnSZp5sl2lwwiCeiqpCE90oh+98ZkY6P0pP4kgrIzHycJ13hIoLgkMBQEQyCQVR/i1vHYC+UXv0QQ7qGBlRMz6yFMKp1Z7CQ+iaUwjEuuO8UAZtgtwfa0tDKe5weK/oGbboWFPt86gIy9K+Vk6QtYJ9OsxklmDtDq0pBsFCNHYylz2AqkhHCEL9TdojRT+PQhr+/8/U2DxO2qDOl+FwFzR05KlZkg++xa5egwsiH0p5E1piwy+zEdQKJpw+O9z3ILBYCdEIvuWee5A7sUi8hCpOIk0lO9tIQCzgK87lt0j6G4kQiMK4Y2itqKus8McjPXA6T2xQnVMW4Tbd1T9tl7NfOSUUmIvOGVpWRduA7KsietbWjmqSTZGUiLZHmgVTCVkXyvwcWHHB2gDuSYNQCZh3hTT/cWkb8nIiE1ZipbXSwjSVUM7qsUdobf+kD7LLIyuTh3PnyPOUMif3s0tVzYrv9/YovZN6exq7IuGnSs2ikf33fSOqE7Zmn9p33/ikwGtmTqxTIfM0I4Nk9cwHAW5XnxqncUd8XYrRba2xGvAMmXLmKEaZbqWOWgOppuVvzrCQrkTEjepiz0fYac2GB444oE0a0jTQr6NsfP7dR9CDpslPg8OGoubPQhM4cgwza0ZGo4nnD1wtR6AhVZ/OGN5aw03K6b7pnYrjxRluYHdT4kSAm0vYY6lNKBz5IfmHKKklXfpX03IZiBDpgXIGvot5IuWWa6uyEB+C8CENewRIaH5VNkHbpujOcJo2CDeiKrqDANpGlhf2tYJxv3RbB hB47oVKO ODKAokXTtJDE9VfXKnmG3iWaazGw/P7ok5xFYruv5YB+k3H8HYHoGZV8iXt8RZWTy12CeS2f/LTiIr99uwS9PhaBBX772YhbByBqeYlWb1AgpuYEAj5EadKX7sNn1VuQxWAv6xV9GdReHVbelmfDj+e1iNlDLuQ9JqD7n4Ywl6JSI8TTVVXZiMhCkZNe3hU0vofvALm6hdqis2T5OSx2+FS5iK0YWEpLe2rjU6rykLFuNEXEg1PgmnfU2kSij4DfVOwg8i0PS/GpU1E8HlcqsvNWR9aaq6Q9xA2VJxSNATBmLDqyD+vLTUXt0bzv99snnrCE3owQ/fZSsk1IPYdFYjVY+0msB6W6HQZHjU7J5AX3B7v8pdfYggTgfGxr9nURCz/qPzQIb50zJ6S1Unt4+QHItpC+HG3ydbNHY0Y2p4TmBmHE= 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: While guest_memfd is not available to be mapped by userspace, it is still accessible through the kernel's direct map. This means that in scenarios where guest-private memory is not hardware protected, it can be speculatively read and its contents potentially leaked through hardware side-channels. Removing guest-private memory from the direct map, thus mitigates a large class of speculative execution issues [1, Table 1]. This patch adds a flag to the `KVM_CREATE_GUEST_MEMFD` which, if set, removes the struct pages backing guest-private memory from the direct map. Should `CONFIG_HAVE_KVM_GMEM_{INVALIDATE, PREPARE}` be set, pages are removed after preparation and before invalidation, so that the prepare/invalidate routines do not have to worry about potentially absent direct map entries. Direct map removal do not reuse the `KVM_GMEM_PREPARE` machinery, since `prepare` can be called multiple time, and it is the responsibility of the preparation routine to not "prepare" the same folio twice [2]. Thus, instead explicitly check if `filemap_grab_folio` allocated a new folio, and remove the returned folio from the direct map only if this was the case. The patch uses release_folio instead of free_folio to reinsert pages back into the direct map as by the time free_folio is called, folio->mapping can already be NULL. This means that a call to folio_inode inside free_folio might deference a NULL pointer, leaving no way to access the inode which stores the flags that allow determining whether the page was removed from the direct map in the first place. Lastly, the patch uses set_direct_map_{invalid,default}_noflush instead of `set_memory_[n]p` to avoid expensive flushes of TLBs and the L*-cache hierarchy. This is especially important once KVM restores direct map entries on-demand in later patches, where simple FIO benchmarks of a virtio-blk device have shown that TLB flushes on a Intel(R) Xeon(R) Platinum 8375C CPU @ 2.90GHz resulted in 80% degradation in throughput compared to a non-flushing solution. Not flushing the TLB means that until TLB entries for temporarily restored direct map entries get naturally evicted, they can be used during speculative execution, and effectively "unhide" the memory for longer than intended. We consider this acceptable, as the only pages that are temporarily reinserted into the direct map like this will either hold PV data structures (kvm-clock, asyncpf, etc), or pages containing privileged instructions inside the guest kernel image (in the MMIO emulation case). [1]: https://download.vusec.net/papers/quarantine_raid23.pdf Signed-off-by: Patrick Roy --- include/uapi/linux/kvm.h | 2 ++ virt/kvm/guest_memfd.c | 52 ++++++++++++++++++++++++++++++++++------ 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index e065d9fe7ab2..409116aa23c9 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1563,4 +1563,6 @@ struct kvm_create_guest_memfd { __u64 reserved[6]; }; +#define KVM_GMEM_NO_DIRECT_MAP (1ULL << 0) + #endif /* __LINUX_KVM_H */ diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 9148b9679bb1..dc9b0c2d0b0e 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -4,6 +4,7 @@ #include #include #include +#include #include "kvm_mm.h" @@ -49,9 +50,16 @@ static int kvm_gmem_prepare_folio(struct inode *inode, pgoff_t index, struct fol return 0; } +static bool kvm_gmem_not_present(struct inode *inode) +{ + return ((unsigned long)inode->i_private & KVM_GMEM_NO_DIRECT_MAP) != 0; +} + static struct folio *kvm_gmem_get_folio(struct inode *inode, pgoff_t index, bool prepare) { struct folio *folio; + bool zap_direct_map = false; + int r; /* TODO: Support huge pages. */ folio = filemap_grab_folio(inode->i_mapping, index); @@ -74,16 +82,30 @@ static struct folio *kvm_gmem_get_folio(struct inode *inode, pgoff_t index, bool for (i = 0; i < nr_pages; i++) clear_highpage(folio_page(folio, i)); + // We need to clear the folio before calling kvm_gmem_prepare_folio, + // but can only remove it from the direct map _after_ preparation is done. + zap_direct_map = kvm_gmem_not_present(inode); + folio_mark_uptodate(folio); } if (prepare) { - int r = kvm_gmem_prepare_folio(inode, index, folio); - if (r < 0) { - folio_unlock(folio); - folio_put(folio); - return ERR_PTR(r); - } + r = kvm_gmem_prepare_folio(inode, index, folio); + if (r < 0) + goto out_err; + } + + if (zap_direct_map) { + r = set_direct_map_invalid_noflush(&folio->page); + if (r < 0) + goto out_err; + + // We use the private flag to track whether the folio has been removed + // from the direct map. This is because inside of ->free_folio, + // we do not have access to the address_space anymore, meaning we + // cannot check folio_inode(folio)->i_private to determine whether + // KVM_GMEM_NO_DIRECT_MAP was set. + folio_set_private(folio); } /* @@ -91,6 +113,10 @@ static struct folio *kvm_gmem_get_folio(struct inode *inode, pgoff_t index, bool * unevictable and there is no storage to write back to. */ return folio; +out_err: + folio_unlock(folio); + folio_put(folio); + return ERR_PTR(r); } static void kvm_gmem_invalidate_begin(struct kvm_gmem *gmem, pgoff_t start, @@ -354,10 +380,22 @@ static void kvm_gmem_free_folio(struct folio *folio) } #endif +static void kvm_gmem_invalidate_folio(struct folio *folio, size_t start, size_t end) +{ + if (start == 0 && end == PAGE_SIZE) { + // We only get here if PG_private is set, which only happens if kvm_gmem_not_present + // returned true in kvm_gmem_get_folio. Thus no need to do that check again. + BUG_ON(set_direct_map_default_noflush(&folio->page)); + + folio_clear_private(folio); + } +} + static const struct address_space_operations kvm_gmem_aops = { .dirty_folio = noop_dirty_folio, .migrate_folio = kvm_gmem_migrate_folio, .error_remove_folio = kvm_gmem_error_folio, + .invalidate_folio = kvm_gmem_invalidate_folio, #ifdef CONFIG_HAVE_KVM_GMEM_INVALIDATE .free_folio = kvm_gmem_free_folio, #endif @@ -443,7 +481,7 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args) { loff_t size = args->size; u64 flags = args->flags; - u64 valid_flags = 0; + u64 valid_flags = KVM_GMEM_NO_DIRECT_MAP; if (flags & ~valid_flags) return -EINVAL; From patchwork Tue Jul 9 13:20:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Roy X-Patchwork-Id: 13727956 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 545E8C2BD09 for ; Tue, 9 Jul 2024 13:21:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 783D96B00B5; Tue, 9 Jul 2024 09:21:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6BDE66B00B7; Tue, 9 Jul 2024 09:21:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4E9426B00B8; Tue, 9 Jul 2024 09:21:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 252286B00B5 for ; Tue, 9 Jul 2024 09:21:28 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id E13D116184E for ; Tue, 9 Jul 2024 13:21:27 +0000 (UTC) X-FDA: 82320275814.03.BF2E4B8 Received: from smtp-fw-9105.amazon.com (smtp-fw-9105.amazon.com [207.171.188.204]) by imf20.hostedemail.com (Postfix) with ESMTP id E2AA31C001B for ; Tue, 9 Jul 2024 13:21:25 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=amazon.co.uk header.s=amazon201209 header.b="P/eETkF+"; dmarc=pass (policy=quarantine) header.from=amazon.co.uk; spf=pass (imf20.hostedemail.com: domain of "prvs=913fd7204=roypat@amazon.co.uk" designates 207.171.188.204 as permitted sender) smtp.mailfrom="prvs=913fd7204=roypat@amazon.co.uk" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1720531263; a=rsa-sha256; cv=none; b=Q/HjKSRHYIpBqqfyfMDHMzUCjIjMEsBZ4fpLkn8gA7BtsLOFNgdiK9awqHgyg8vLTj76ui +vwrkxpuPjJTC7ARbQ+lujiP5wfMHju6DdFukDpuiJ2NQ3K4FdlUTW5NUVX4otWVEIU/N/ jyEnejlhTc+PYuuefEPqkp9iRmcOKoI= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=amazon.co.uk header.s=amazon201209 header.b="P/eETkF+"; dmarc=pass (policy=quarantine) header.from=amazon.co.uk; spf=pass (imf20.hostedemail.com: domain of "prvs=913fd7204=roypat@amazon.co.uk" designates 207.171.188.204 as permitted sender) smtp.mailfrom="prvs=913fd7204=roypat@amazon.co.uk" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1720531263; 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=mg7XA6znVd/yr9yTmGtkTxgi1xD8jDw5CHJVcz9oSG8=; b=WxqhRWawoHG1fVyBj0Bky3/Evn6t3/1/HuB+Th0fppMkVkPUU2LNWmn4rjl4Ll7oJsVncc fujET3+S/ric+IMMaUI7BfdXcfUqYpRD7L+yqksU4mVHR6pdM+MA2pQs+vC6EJBZs79PWi hOfbQczBPKUXagor4UgIC7C/4KdiLFA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.co.uk; i=@amazon.co.uk; q=dns/txt; s=amazon201209; t=1720531287; x=1752067287; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=mg7XA6znVd/yr9yTmGtkTxgi1xD8jDw5CHJVcz9oSG8=; b=P/eETkF+ZOdasU07Gb8QBf10Z2+RX3YdvKf+RIlRYmVBT+BQ2ltomaeB tX66CpxZrPlU+Ragl5VOnC+KGpkPrPb6oImVwuHiESHICG1YwRkRRoeex F+UjX8F2OpsYCQp2d0eIiREJdx8YPqyYvhQyEO8/+4Heg0Fv9HrDTkwae I=; X-IronPort-AV: E=Sophos;i="6.09,195,1716249600"; d="scan'208";a="739970210" 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-9105.sea19.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Jul 2024 13:21:17 +0000 Received: from EX19MTAUEA002.ant.amazon.com [10.0.0.204:6203] by smtpin.naws.us-east-1.prod.farcaster.email.amazon.dev [10.0.50.89:2525] with esmtp (Farcaster) id 2ee5a019-f3c3-4312-aa43-5ccfe349ee40; Tue, 9 Jul 2024 13:21:15 +0000 (UTC) X-Farcaster-Flow-ID: 2ee5a019-f3c3-4312-aa43-5ccfe349ee40 Received: from EX19D008UEA002.ant.amazon.com (10.252.134.125) by EX19MTAUEA002.ant.amazon.com (10.252.134.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Tue, 9 Jul 2024 13:21:13 +0000 Received: from EX19MTAUEC001.ant.amazon.com (10.252.135.222) 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, 9 Jul 2024 13:21:13 +0000 Received: from ua2d7e1a6107c5b.ant.amazon.com (172.19.88.180) by mail-relay.amazon.com (10.252.135.200) 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, 9 Jul 2024 13:21:11 +0000 From: Patrick Roy To: , , , , , CC: Patrick Roy , , , , , , , , , , , , , , , , , Subject: [RFC PATCH 6/8] kvm: gmem: Temporarily restore direct map entries when needed Date: Tue, 9 Jul 2024 14:20:34 +0100 Message-ID: <20240709132041.3625501-7-roypat@amazon.co.uk> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240709132041.3625501-1-roypat@amazon.co.uk> References: <20240709132041.3625501-1-roypat@amazon.co.uk> MIME-Version: 1.0 X-Rspamd-Queue-Id: E2AA31C001B X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 8jq3r1f3wobk1f8yqto3yyc8xnquotb8 X-HE-Tag: 1720531285-347636 X-HE-Meta: U2FsdGVkX185vCTny1L0Mso0Tz3zTEiFYovhv+RRx0HOPtBcAKBWnCf3BibPXevniEXAAa18ziTwbgGZRy6d1Lnh70hoAWgXi7M+rx/HHbXSAwNLFz+J76x/8KZAHoYMFaYY/fEtUGFmtDDV1g/axnLAokACxeFmr9fnR8qCkiDZfjLUN8DpBDg5gopYOvvjG6iSxtjxZ9gCmSIb6G9YAmZUB4QJvaUJH6dqblu35MCFZ9VDjcyPSdkjgpOG3UrmTDATFarqOY6jZMVdSjvLe/MOixb3t32C1zu+qmLHDJ9YBLzXS1bsE/6KIK4wxrnyrtwkYlsWgLYlC7VCte2NPlXkhMpe4g0EsGdoMecXZ2a82B2/XoMfSotM6MznDMSOsAUmZAMVbNHtJi4DKCS5xFnTfhUEYh8/jcrpdLR2aE7U53GJvR94y+6f3H9TN+tBhxtjAj8+wcppWU/zPP2IQOnw4E6T+sQyR4OvSBWCUQLQfBsFsyXXqD5f9jBhQ1cIsdLvTM7MA6HABnbiIzK2dR3+6NdHzmkQ3fhI3xINEksheLqFz3dwjSJ/qvXLBwbDik0Vqklds9+lUKbCsucl5dQUm4j9+JW3MxsoRB2i175FYcX3LXolJmwQsEEUTTceg4/FUvHCDXwnVHOdHoxceuHxTH+S/zmRyLDxgewtl6/Z14QCde808m2a2Y3Y5TAv5DVr9oQEna5fIuZu43K6g4e8c97xLHtfJ/JBqwUqUR8zFlAkFWAn3MjcqSZ17EQPqPGgRRmEgwzr+aiWd85Hz/uVEju7n1wJqgbqGxrn4RXvAENTduacDn33aa2LLe4m1gH9D65abK36O2rYd/u+0MmmyfrX0YNbIjKTJpqbRNIz+ExwEB9rtRX0zffvD6jVOR50nd3SON/zDhj4rC4ZyFKCraQMT13WfD0ZeAUO18/Y98DZIH2sRiEiYXTFh6tPjUR33tV7rzYhJU+4YpV 11EH43An 8g9fcoxzcM1qvRGTq5Nra4g7X2Mg+LAVLCu5tZ/ULs9HRJsIIJcKs9MX+MjryXGHzVuGJ0xZe8LFWKRXGbiGo9e4RKEPezCXbF+sycNipItM5KhD79QnTOCdGtyPO3R6JByZAmEk3PONycG0qc5kgL3OIcic5VoyNkJdQipkfljnbkOKmZv+KgdPVCP/5fRYj37Z08rFeTYvqcNdhyPmBI/JO0LziWJbDHaQy5v++GivPVKNbCP+ArWNMETSumPUghoxJLbgI70zDpeRkG+D88RGmTWHJTuqdxpXwDbDghKkTP0yz4mLgnMK7ZYiURtZTTqkXtTer/zHxbAyz/+qqGQK1wformBY6pwkJe5G01mNsfjqNmROcHqEpgg== 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_GMEM_NO_DIRECT_MAP is set, and KVM tries to internally access guest-private memory inside kvm_{read,write}_guest, or via a gfn_to_pfn_cache, temporarily restore the direct map entry. To avoid race conditions between two threads restoring or zapping direct map entries for the same page and potentially interfering with each other (e.g. unfortune interweavings of map->read->unmap in the form of map(A)->map(B)->read(A)->unmap(A)->read(B) [BOOM]), the following invariant is upheld in this patch: - Only a single gfn_to_pfn_cache can exist for any given pfn, and - All non-gfn_to_pfn_cache code paths that temporarily restore direct map entries complete the entire map->access->unmap critical section while holding the folio lock. To remember whether a given folio currently has a direct map entry, use the PG_private flag. If this flag is set, then the folio is removed from the direct map, otherwise it is present in the direct map. Modifications of this flag, together with the corresponding direct map manipulations, must happen while holding the folio's lock. A gfn_to_pfn_cache cannot hold the folio lock for its entire lifetime, so it operates as follows: In gpc_map, under folio lock, restore the direct map entry and set PG_private to 0. In gpc_unmap, zap the direct map entry again and set PG_private back to 1. If inside gpc_map the cache finds a folio that has PG_private set to 0, it knows that another gfn_to_pfn_cache is currently active for the given pfn (as this is the only scenario in which PG_private can be 0 without the folio lock being held), and so it returns -EINVAL. The only other interesting scenario is then if kvm_{read,write}_guest is called for a gfn whose translation is currently cached inside a gfn_to_pfn_cache. In this case, kvm_{read,write}_guest notices that PG_private is 0 and skips all direct map manipulations. Since it is holding the folio lock, it can be sure that gpc_unmap cannot concurrently zap the direct map entries while kvm_{read,write}_guest still needs them. Note that this implementation is slightly too restrictive, as sometimes multiple gfn_to_pfn_caches need to be active for the same gfn (for example, each vCPU has its own kvm-clock structure, which they all try to put into the same gfn). Signed-off-by: Patrick Roy --- virt/kvm/kvm_main.c | 59 +++++++++++++++++++++--------- virt/kvm/pfncache.c | 89 +++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 123 insertions(+), 25 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 4357f7cdf040..f968f1f3d7f7 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -52,6 +52,7 @@ #include #include +#include #include #include "coalesced_mmio.h" @@ -3291,8 +3292,8 @@ static int __kvm_read_guest_private_page(struct kvm *kvm, void *data, int offset, int len) { kvm_pfn_t pfn; - int r; - struct page *page; + int r = 0; + struct folio *folio; void *kaddr; if (!kvm_can_access_gmem(kvm)) @@ -3303,13 +3304,24 @@ static int __kvm_read_guest_private_page(struct kvm *kvm, if (r < 0) return -EFAULT; - page = pfn_to_page(pfn); - lock_page(page); - kaddr = page_address(page) + offset; - memcpy(data, kaddr, len); - unlock_page(page); - put_page(page); - return 0; + folio = pfn_folio(pfn); + folio_lock(folio); + kaddr = folio_address(folio); + if (folio_test_private(folio)) { + r = set_direct_map_default_noflush(&folio->page); + if (r) + goto out_unlock; + } + memcpy(data, kaddr + offset, len); + if (folio_test_private(folio)) { + r = set_direct_map_invalid_noflush(&folio->page); + if (r) + goto out_unlock; + } +out_unlock: + folio_unlock(folio); + folio_put(folio); + return r; } static int __kvm_vcpu_read_guest_private_page(struct kvm_vcpu *vcpu, @@ -3437,8 +3449,8 @@ static int __kvm_write_guest_private_page(struct kvm *kvm, const void *data, int offset, int len) { kvm_pfn_t pfn; - int r; - struct page *page; + int r = 0; + struct folio *folio; void *kaddr; if (!kvm_can_access_gmem(kvm)) @@ -3449,14 +3461,25 @@ static int __kvm_write_guest_private_page(struct kvm *kvm, if (r < 0) return -EFAULT; - page = pfn_to_page(pfn); - lock_page(page); - kaddr = page_address(page) + offset; - memcpy(kaddr, data, len); - unlock_page(page); - put_page(page); + folio = pfn_folio(pfn); + folio_lock(folio); + kaddr = folio_address(folio); + if (folio_test_private(folio)) { + r = set_direct_map_default_noflush(&folio->page); + if (r) + goto out_unlock; + } + memcpy(kaddr + offset, data, len); + if (folio_test_private(folio)) { + r = set_direct_map_invalid_noflush(&folio->page); + if (r) + goto out_unlock; + } - return 0; +out_unlock: + folio_unlock(folio); + folio_put(folio); + return r; } static int __kvm_vcpu_write_guest_private_page(struct kvm_vcpu *vcpu, diff --git a/virt/kvm/pfncache.c b/virt/kvm/pfncache.c index 6430e0a49558..95d2d5cdaa12 100644 --- a/virt/kvm/pfncache.c +++ b/virt/kvm/pfncache.c @@ -16,6 +16,9 @@ #include #include #include +#include + +#include #include "kvm_mm.h" @@ -99,10 +102,68 @@ bool kvm_gpc_check(struct gfn_to_pfn_cache *gpc, unsigned long len) return true; } -static void *gpc_map(kvm_pfn_t pfn) +static int gpc_map_gmem(kvm_pfn_t pfn) { - if (pfn_valid(pfn)) + int r = 0; + struct folio *folio = pfn_folio(pfn); + struct inode *inode = folio_inode(folio); + + if (((unsigned long)inode->i_private & KVM_GMEM_NO_DIRECT_MAP) == 0) + goto out; + + /* We need to avoid race conditions where set_memory_np is called for + * pages that other parts of KVM still try to access. We use the + * PG_private bit for this. If it is set, then the page is removed from + * the direct map. If it is cleared, the page is present in the direct + * map. All changes to this bit, and all modifications of the direct + * map entries for the page happen under the page lock. The _only_ + * place where a page will be in the direct map while the page lock is + * _not_ held is if it is inside a gpc. All other parts of KVM that + * temporarily re-insert gmem pages into the direct map (currently only + * guest_{read,write}_page) take the page lock before the direct map + * entry is restored, and hold it until it is zapped again. This means + * - If we reach gpc_map while, say, guest_read_page is operating on + * this page, we block on acquiring the page lock until + * guest_read_page is done. + * - If we reach gpc_map while another gpc is already caching this + * page, the page is present in the direct map and the PG_private + * flag is cleared. Int his case, we return -EINVAL below to avoid + * two gpcs caching the same page (since we do not ref-count + * insertions back into the direct map, when the first cache gets + * invalidated it would "break" the second cache that assumes the + * page is present in the direct map until the second cache itself + * gets invalidated). + * - Lastly, if guest_read_page is called for a page inside of a gpc, + * it will see that the PG_private flag is cleared, and thus assume + * it is present in the direct map (and leave the direct map entry + * untouched). Since it will be holding the page lock, it cannot race + * with gpc_unmap. + */ + folio_lock(folio); + if (folio_test_private(folio)) { + r = set_direct_map_default_noflush(&folio->page); + if (r) + goto out_unlock; + + folio_clear_private(folio); + } else { + r = -EINVAL; + } +out_unlock: + folio_unlock(folio); +out: + return r; +} + +static void *gpc_map(kvm_pfn_t pfn, bool private) +{ + if (pfn_valid(pfn)) { + if (private) { + if (gpc_map_gmem(pfn)) + return NULL; + } return kmap(pfn_to_page(pfn)); + } #ifdef CONFIG_HAS_IOMEM return memremap(pfn_to_hpa(pfn), PAGE_SIZE, MEMREMAP_WB); @@ -111,13 +172,27 @@ static void *gpc_map(kvm_pfn_t pfn) #endif } -static void gpc_unmap(kvm_pfn_t pfn, void *khva) +static void gpc_unmap(kvm_pfn_t pfn, void *khva, bool private) { /* Unmap the old pfn/page if it was mapped before. */ if (is_error_noslot_pfn(pfn) || !khva) return; if (pfn_valid(pfn)) { + if (private) { + struct folio *folio = pfn_folio(pfn); + struct inode *inode = folio_inode(folio); + + if ((unsigned long)inode->i_private & + KVM_GMEM_NO_DIRECT_MAP) { + folio_lock(folio); + BUG_ON(folio_test_private(folio)); + BUG_ON(set_direct_map_invalid_noflush( + &folio->page)); + folio_set_private(folio); + folio_unlock(folio); + } + } kunmap(pfn_to_page(pfn)); return; } @@ -195,7 +270,7 @@ static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cache *gpc) * the existing mapping and didn't create a new one. */ if (new_khva != old_khva) - gpc_unmap(new_pfn, new_khva); + gpc_unmap(new_pfn, new_khva, gpc->is_private); kvm_release_pfn_clean(new_pfn); @@ -224,7 +299,7 @@ static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cache *gpc) if (new_pfn == gpc->pfn) new_khva = old_khva; else - new_khva = gpc_map(new_pfn); + new_khva = gpc_map(new_pfn, gpc->is_private); if (!new_khva) { kvm_release_pfn_clean(new_pfn); @@ -379,7 +454,7 @@ static int __kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, gpa_t gpa, unsigned l write_unlock_irq(&gpc->lock); if (unmap_old) - gpc_unmap(old_pfn, old_khva); + gpc_unmap(old_pfn, old_khva, old_private); return ret; } @@ -500,6 +575,6 @@ void kvm_gpc_deactivate(struct gfn_to_pfn_cache *gpc) list_del(&gpc->list); spin_unlock(&kvm->gpc_lock); - gpc_unmap(old_pfn, old_khva); + gpc_unmap(old_pfn, old_khva, gpc->is_private); } } From patchwork Tue Jul 9 13:20:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Roy X-Patchwork-Id: 13727957 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 722FEC2BD09 for ; Tue, 9 Jul 2024 13:21:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 90B3A6B00B7; Tue, 9 Jul 2024 09:21:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 891AB6B00B8; Tue, 9 Jul 2024 09:21:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 699086B00B9; Tue, 9 Jul 2024 09:21:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 464296B00B7 for ; Tue, 9 Jul 2024 09:21:30 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id EC2ADC196C for ; Tue, 9 Jul 2024 13:21:29 +0000 (UTC) X-FDA: 82320275898.13.A782AE0 Received: from smtp-fw-33001.amazon.com (smtp-fw-33001.amazon.com [207.171.190.10]) by imf13.hostedemail.com (Postfix) with ESMTP id F1C9B2002C for ; Tue, 9 Jul 2024 13:21:26 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=amazon.co.uk header.s=amazon201209 header.b=PlwmWrIi; spf=pass (imf13.hostedemail.com: domain of "prvs=913fd7204=roypat@amazon.co.uk" designates 207.171.190.10 as permitted sender) smtp.mailfrom="prvs=913fd7204=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=1720531256; 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=Zs9pi34/tgrMwAZ4z/XLHKURo1LH266GsNUebv4yq5Y=; b=j+qLTrID/2tWPBejkKSzAuaob6GuKdP4s/w7BDOP+sdEqLBD4MUf9BQYZ+00/ZdAc3+wU4 xsAfi1Cw+ua0OBHr0kDlW6g/phdthSqImxGYGCP9IGyVCBjD2G9Wyu+dFxbIP4W13W1gPb pqrbKTDbrevDId78+0eFu1YsC824F9M= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1720531256; a=rsa-sha256; cv=none; b=6fVJKm8psGHjhB4p7PK1sDAlnrXffPeSVOZWuc8dtJ6SiyTLVJ8KbGWhVo+zzRsyBFLWCD XzKv2miiJuiIxVsmVYkpJMBFzQlZr240OSkWb+EGUqSoBnVZ65q7prAUwvbnbqPQ1pPxXA XyLDlg7Vo9dKnG9LyPM5g9pm30r2q+8= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=amazon.co.uk header.s=amazon201209 header.b=PlwmWrIi; spf=pass (imf13.hostedemail.com: domain of "prvs=913fd7204=roypat@amazon.co.uk" designates 207.171.190.10 as permitted sender) smtp.mailfrom="prvs=913fd7204=roypat@amazon.co.uk"; dmarc=pass (policy=quarantine) header.from=amazon.co.uk DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.co.uk; i=@amazon.co.uk; q=dns/txt; s=amazon201209; t=1720531287; x=1752067287; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Zs9pi34/tgrMwAZ4z/XLHKURo1LH266GsNUebv4yq5Y=; b=PlwmWrIiBE+IVPEi67xgHs9KoDET7GLIOHr6V70yGpa0nuwRMOjv9/j/ gM8D/sZeij5p+PRz1AgcA1RbCdi123ZtYs7VgXAcNYLtqaCYQzOib3iUn h/eqFNp5BtjZiXs7XZ+NJQf+zWHEcdLSg+HDSOoBGxgl6OtNLjHZdiJAD Q=; X-IronPort-AV: E=Sophos;i="6.09,195,1716249600"; d="scan'208";a="355121480" 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; 09 Jul 2024 13:21:21 +0000 Received: from EX19MTAUEA001.ant.amazon.com [10.0.0.204:8257] by smtpin.naws.us-east-1.prod.farcaster.email.amazon.dev [10.0.59.218:2525] with esmtp (Farcaster) id f29e8f74-c847-4100-b6a1-572dc2245bb7; Tue, 9 Jul 2024 13:21:19 +0000 (UTC) X-Farcaster-Flow-ID: f29e8f74-c847-4100-b6a1-572dc2245bb7 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, 9 Jul 2024 13:21:17 +0000 Received: from EX19MTAUEC001.ant.amazon.com (10.252.135.222) 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, 9 Jul 2024 13:21:17 +0000 Received: from ua2d7e1a6107c5b.ant.amazon.com (172.19.88.180) by mail-relay.amazon.com (10.252.135.200) 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, 9 Jul 2024 13:21:14 +0000 From: Patrick Roy To: , , , , , CC: Patrick Roy , , , , , , , , , , , , , , , , , Subject: [RFC PATCH 7/8] mm: secretmem: use AS_INACCESSIBLE to prohibit GUP Date: Tue, 9 Jul 2024 14:20:35 +0100 Message-ID: <20240709132041.3625501-8-roypat@amazon.co.uk> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240709132041.3625501-1-roypat@amazon.co.uk> References: <20240709132041.3625501-1-roypat@amazon.co.uk> MIME-Version: 1.0 X-Stat-Signature: cfy6ug7umypjtdt1376yhnwfam57q9eo X-Rspamd-Queue-Id: F1C9B2002C X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1720531286-646730 X-HE-Meta: U2FsdGVkX18vV9Bg4h7hOvDqKmAyqvev2sst/Uz6+QVrx2n/wream/EMLlPoG7CSHGH5+DDy90EeSb5V5rCDnn92dOtz3x3rnm3zGHPtUXufAU75wxhcQVsdDWAtPanH3+LXeDcftqU3efppdsrPuOrCJOLNAY6dBqZAIiALMF9R1snIsIGqF+g8nj0bvScb/zIp1gqKNfvU55C+CqsdmIMEiPX2Ab1TdpuPM7A7khvSnVpcJTcvCI3T20FfvkV609EnjiaaXqoTXAQ9gMdO1ZAvm86CiA5JRwZ43qi6o3S1nbGppqZ/4CgTrFXRtPH0O/n4dTq8CwndfNr4/RxtfKOiuPNKE9RbuowqDBt2emzgUaJ8dOjA8byxdSlMT1t4W61psuRbMhMbABGMmpuGEH2A73HiI4dTv2qFh4YDc0Va6tllIk1tNph2pNCw4t+z5R9NCM90PMqDxne6uuzJv3CsftldUNqpmr+PcsypbFPpNOBrW7kM8dATlUlX2AL817rnBPGDnACvrdv7OCY/gkPjVBS8NN5+Gf7lS0eZwNHBjydWeQhuouJjXIfMjKIpfa1A93uGuxmHvFxdiZrx8FUm5fE5hRPu7ZpV9F/1F8YEpWQZhQPmNdSl5EV9vXz1MZzU4RlIAAuVngsk3j7ixpxe2iy/BPNyp9jUon0BtIxBF2DtiQ1FDXnaBqhTsRCb+P1pRRDBdyvvDUBnIpwMoSN8Y5WwCeW7WQTdlKFOIopVYOMB7g8JpS8qAvXE6kHdXaQKPkZ2a+oG2AzAykr5s7GW0cphvj9yOc+sVVqFMzgsrE2GP4Qpw6XW3e7r3HR9aj5vMmkXn1cWc0VW9gILRXC6IuTrFMzi6MXQ1qQ0zCInpXd+oHQB5oZ57yAcmEJRHvtVJCTa01K/svmJagoBgol5CrXC+Ww2IwN/z37HzYrYrEVweMGsXKOxGLLgVyfgqa2tSPGUOUCvZAFnCoD DzZlHPsg EQnH3ilaEvq8qdyRX4GeHjIdW1lrqixTSmZD3aKjg8IWQtwOfjimBP7H1QzRfTC8gV8SjCq+nBHGhDAxFX61fmkWpNiXatXh5+jPGaWiKyy1P0BCCLbUr1Hqqiz1yU3ZSgnqMZ6FCXj9WFy2HmHFZpU6pBJQdnGZycm+QOOT3olGtIvHzVSqXmdE9p3jGehrHg5vITtWeYUH3VoxwJO607ldjPUJrl7V5uRLa6Rg/SBNnpTJKzde1vCYXJ5Bqy0RGUgy88yOt3frLZERM5vvseU3k+zNqiYoKccrB7Vaz70q7qMYHLLiR0Gfxct1lGyFKO7fyLmbXy7J24s7V5phN9KYQzXAizDY2PrMXaxmvFCgBzXExc49sKdFwb2jUU0HakI9OLHyANkmm4F4bcffAk6jebsVymT73kF4+ 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: Inside of vma_is_secretmem and secretmem_mapping, instead of checking whether a vm_area_struct/address_space has the secretmem ops structure attached to it, check whether the address_space has the AS_INACCESSIBLE bit set. Then set the AS_INACCESSIBLE flag for secretmem's address_space. This means that get_user_pages and friends are disables for all adress_spaces that set AS_INACCESIBLE. The AS_INACCESSIBLE flag was introduced in commit c72ceafbd12c ("mm: Introduce AS_INACCESSIBLE for encrypted/confidential memory") specifically for guest_memfd to indicate that no reads and writes should ever be done to guest_memfd address_spaces. Disallowing gup seems like a reasonable semantic extension, and means that potential future mmaps of guest_memfd cannot be GUP'd. Signed-off-by: Patrick Roy --- include/linux/secretmem.h | 13 +++++++++++-- mm/secretmem.c | 6 +----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/include/linux/secretmem.h b/include/linux/secretmem.h index e918f96881f5..886c8f7eb63e 100644 --- a/include/linux/secretmem.h +++ b/include/linux/secretmem.h @@ -8,10 +8,19 @@ extern const struct address_space_operations secretmem_aops; static inline bool secretmem_mapping(struct address_space *mapping) { - return mapping->a_ops == &secretmem_aops; + return mapping->flags & AS_INACCESSIBLE; +} + +static inline bool vma_is_secretmem(struct vm_area_struct *vma) +{ + struct file *file = vma->vm_file; + + if (!file) + return false; + + return secretmem_mapping(file->f_inode->i_mapping); } -bool vma_is_secretmem(struct vm_area_struct *vma); bool secretmem_active(void); #else diff --git a/mm/secretmem.c b/mm/secretmem.c index 3afb5ad701e1..fd03a84a1cb5 100644 --- a/mm/secretmem.c +++ b/mm/secretmem.c @@ -136,11 +136,6 @@ static int secretmem_mmap(struct file *file, struct vm_area_struct *vma) return 0; } -bool vma_is_secretmem(struct vm_area_struct *vma) -{ - return vma->vm_ops == &secretmem_vm_ops; -} - static const struct file_operations secretmem_fops = { .release = secretmem_release, .mmap = secretmem_mmap, @@ -218,6 +213,7 @@ static struct file *secretmem_file_create(unsigned long flags) inode->i_op = &secretmem_iops; inode->i_mapping->a_ops = &secretmem_aops; + inode->i_mapping->flags |= AS_INACCESSIBLE; /* pretend we are a normal file with zero size */ inode->i_mode |= S_IFREG; From patchwork Tue Jul 9 13:20:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Roy X-Patchwork-Id: 13727955 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 83F5EC2BD09 for ; Tue, 9 Jul 2024 13:21:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D06F56B00B4; Tue, 9 Jul 2024 09:21:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C89676B00B5; Tue, 9 Jul 2024 09:21:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A8D566B00B6; Tue, 9 Jul 2024 09:21:27 -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 80C706B00B4 for ; Tue, 9 Jul 2024 09:21:27 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 46E061218D1 for ; Tue, 9 Jul 2024 13:21:27 +0000 (UTC) X-FDA: 82320275814.13.B7A413B Received: from smtp-fw-9102.amazon.com (smtp-fw-9102.amazon.com [207.171.184.29]) by imf03.hostedemail.com (Postfix) with ESMTP id 39ADA20025 for ; Tue, 9 Jul 2024 13:21:25 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=amazon.co.uk header.s=amazon201209 header.b=dXktH00b; spf=pass (imf03.hostedemail.com: domain of "prvs=913fd7204=roypat@amazon.co.uk" designates 207.171.184.29 as permitted sender) smtp.mailfrom="prvs=913fd7204=roypat@amazon.co.uk"; dmarc=pass (policy=quarantine) header.from=amazon.co.uk ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1720531269; a=rsa-sha256; cv=none; b=GGXSzyrU/3HwHoxE2xAT2J3vVjBsr5YPKyMqf1b2hWp4F6Es/1DreMF7mq6azhYen7Tn7r r3eN6FEw8pKqDM0QdNw7x6ra4zQXBrMRjmoiQxZGafVhQczKAgx2Mw3txo72d78KykXf/s 4EDPJ/ACFhX9km7dpnINKg4BfXKctpM= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=amazon.co.uk header.s=amazon201209 header.b=dXktH00b; spf=pass (imf03.hostedemail.com: domain of "prvs=913fd7204=roypat@amazon.co.uk" designates 207.171.184.29 as permitted sender) smtp.mailfrom="prvs=913fd7204=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=1720531269; 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=OYFwwPMzkwoeUqGjPsHTCVZ/9U32HO+9FB3G59spnNM=; b=sP1yDqfFKJuzzw3coozgZcxtQ3K8NMc0VQkxsXRX7xRuAcWsCfkfRlSVoLgPvj6hcuQ85o 7z5BjOMCVoBq2m1YvSpj3OhmKijpnEKOveBTZvUvFN6ZkkHuN4Y2XoYiOxvMPU64L4o5Od en+S6L5exkS3TGF83nbOsIldD7P6xSk= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.co.uk; i=@amazon.co.uk; q=dns/txt; s=amazon201209; t=1720531286; x=1752067286; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=OYFwwPMzkwoeUqGjPsHTCVZ/9U32HO+9FB3G59spnNM=; b=dXktH00bgudEtkH8gqyQWtLeEwchY/Rsn+qT4/HUHkrq3l33oKGZZQv4 r75OAD8uJZVtyBzyHiIN7Zx8fJHXgYf2sRgWTIUKF5TguvJQIMMu6d/qm l+QB6cPwtgHWFY+SyPxE3xt2lyDfFZl36P+15mPQ4pJ7AnSb0YfBC+q2x 0=; X-IronPort-AV: E=Sophos;i="6.09,195,1716249600"; d="scan'208";a="432897857" Received: from pdx4-co-svc-p1-lb2-vlan3.amazon.com (HELO smtpout.prod.us-east-1.prod.farcaster.email.amazon.dev) ([10.25.36.214]) by smtp-border-fw-9102.sea19.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Jul 2024 13:21:23 +0000 Received: from EX19MTAUEB001.ant.amazon.com [10.0.0.204:11016] by smtpin.naws.us-east-1.prod.farcaster.email.amazon.dev [10.0.59.218:2525] with esmtp (Farcaster) id b525f254-64fd-4b54-ab1d-bef2d53e8187; Tue, 9 Jul 2024 13:21:21 +0000 (UTC) X-Farcaster-Flow-ID: b525f254-64fd-4b54-ab1d-bef2d53e8187 Received: from EX19D008UEA004.ant.amazon.com (10.252.134.191) 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, 9 Jul 2024 13:21:20 +0000 Received: from EX19MTAUEC001.ant.amazon.com (10.252.135.222) by EX19D008UEA004.ant.amazon.com (10.252.134.191) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Tue, 9 Jul 2024 13:21:20 +0000 Received: from ua2d7e1a6107c5b.ant.amazon.com (172.19.88.180) by mail-relay.amazon.com (10.252.135.200) 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, 9 Jul 2024 13:21:17 +0000 From: Patrick Roy To: , , , , , CC: Patrick Roy , , , , , , , , , , , , , , , , , Subject: [RFC PATCH 8/8] kvm: gmem: Allow restricted userspace mappings Date: Tue, 9 Jul 2024 14:20:36 +0100 Message-ID: <20240709132041.3625501-9-roypat@amazon.co.uk> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240709132041.3625501-1-roypat@amazon.co.uk> References: <20240709132041.3625501-1-roypat@amazon.co.uk> MIME-Version: 1.0 X-Stat-Signature: zh71dtypiaynsfbfhf8jdcas6p6hxjiz X-Rspamd-Queue-Id: 39ADA20025 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1720531285-363323 X-HE-Meta: U2FsdGVkX1/lJFW/Z14h5RUd4aNSQGYq3IvsvODCFjhWcy1V3RPElmaoWuljHBBNMpd+zEM5KO/6U5zAsIyqhOG/I2/IW3mHR+CCmu1v1G9JZXEqa6YKyPcdb9AbQ0uMDXGuvhP/QksiZqQaBlEWzu0raLjP1jC+PaYMc3hQEo4Zm8Zs3vI+2d/9Trnmf8eYThaG+UAZxOx15srIi+tDLZIBPGER/IXFBx5iNVIIjBcYEavHBwd/tjRVhgw7fQq4mlBDjjxK3gdZssX5p2OGcYYkrLonC+PRjb627R7SP7/VPltyDKxMJ3f3PTZNftE9c0+wcL/BqZx2awluXcAF9X8vq5ba6bI2sMFgx/e5kBWf4Oinznb010GSRk2M4u2gsUTR0Ldk2aRe0sqNz6gEVN5TKVhpBiuXgIln5ad/nySBg3DSWQUK1Ng1SoauTq0LxKES4N9adoKhv2ZRFlRzxuarV9Duw1IF502ZaxjSWIvsW5cP2TP9e0QCL8/iQlGhWMPOQw104PVYTL/yRuiC5uzJfvMcfVt5dMN+FOVVDAgduqNJDppbOa5bxof1p9eOJ3b6IzmCSRf6zZBOEgVUuzJd0d0ImX8V29Rd7QS6dBiWl3Je8/flImQGDv5qd+AOIapzFcUsRiHl8dRtAMsO6Fkij+CLQGH6uwi6CE5WTZsCaR5yEpFU4ht2FWQy325rhZ21R6meOwEKmUgfF6p/gl5xJarQfZin7KRmSyNNIyGFcSZHtFa4J/7lpOpQxUbJFh0ZcCorNtTyYDJI/6y4NGJ8KF2Z7AwX5P/Gwbvm2ioP5/z51sRJ+iBI0dLmjifGQnWfTplOmfooCc0O0BDfllweupZc12B9w4njqzgjlPabPRnF6LsTGfbQuv1lKBqlzUCEcWZ48BlkWL/ISFV5ZKcU/CznTw4OwzJTzW8Kmzt/mrEg/KURJNwfXMd6XqGVuW10k6mEd7ejTHUCVhw 9S66xmvy 9qh/6/d3QIWL8SNrdWIzHsQ22RKsFnM/HgG59/RZTfiwXNILyUnh8eQ72CKF3yGdVt8r3J6BYofABuRkMGii42WFTEpUMFfRRDAHS1QqhXgkjkCLcOYWkPqfc8OCD4BNaeYjWmxtPq4ofJN8MsC/DlkSFUBCfnBYghik7my4LClXF1AoTfshqILudZ6e8PQguQCKA/0FOX98Tj0WVFz80tcBjIjp+mMt22z7eSG/UVAzAgAwNzVFiHGtCJq7Kr7YpvImFrDAprPWSuynainH2sI9Es3LB9I68+m2s24t2jX64R0lKm9oug8zf5vGEovlwXrs1TcOkerI4xlc0X/EcZD13CujZLPV+ro0a 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: Allow mapping guest_memfd into userspace. Since AS_INACCESSIBLE is set on the underlying address_space struct, no GUP of guest_memfd will be possible. Signed-off-by: Patrick Roy --- virt/kvm/guest_memfd.c | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index dc9b0c2d0b0e..101ec2b248bf 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -319,7 +319,37 @@ static inline struct file *kvm_gmem_get_file(struct kvm_memory_slot *slot) return get_file_active(&slot->gmem.file); } +static vm_fault_t kvm_gmem_fault(struct vm_fault *vmf) +{ + struct folio *folio; + + folio = kvm_gmem_get_folio(file_inode(vmf->vma->vm_file), vmf->pgoff, true); + + if (!folio) + return VM_FAULT_SIGBUS; + + vmf->page = folio_file_page(folio, vmf->pgoff); + + return VM_FAULT_LOCKED; +} + +static const struct vm_operations_struct kvm_gmem_vm_ops = { + .fault = kvm_gmem_fault +}; + +static int kvm_gmem_mmap(struct file *file, struct vm_area_struct *vma) +{ + if ((vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) == 0) + return -EINVAL; + + vm_flags_set(vma, VM_DONTDUMP); + vma->vm_ops = &kvm_gmem_vm_ops; + + return 0; +} + static struct file_operations kvm_gmem_fops = { + .mmap = kvm_gmem_mmap, .open = generic_file_open, .release = kvm_gmem_release, .fallocate = kvm_gmem_fallocate, @@ -594,7 +624,6 @@ static int __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot, return -EFAULT; } - gmem = file->private_data; if (xa_load(&gmem->bindings, index) != slot) { WARN_ON_ONCE(xa_load(&gmem->bindings, index)); return -EIO;