From patchwork Wed Jan 22 15:27:30 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13947429 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 67973C0218C for ; Wed, 22 Jan 2025 15:27:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D00156B0088; Wed, 22 Jan 2025 10:27:46 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CB0186B0089; Wed, 22 Jan 2025 10:27:46 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B51C66B008A; Wed, 22 Jan 2025 10:27:46 -0500 (EST) 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 9A91D6B0088 for ; Wed, 22 Jan 2025 10:27:46 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id CE818AF20A for ; Wed, 22 Jan 2025 15:27:45 +0000 (UTC) X-FDA: 83035467690.26.A68D051 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf03.hostedemail.com (Postfix) with ESMTP id E06392000B for ; Wed, 22 Jan 2025 15:27:43 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=13SGn2ce; spf=pass (imf03.hostedemail.com: domain of 3bg6RZwUKCBMCtuutz77z4x.v75416DG-553Etv3.7Az@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3bg6RZwUKCBMCtuutz77z4x.v75416DG-553Etv3.7Az@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1737559664; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ZDcetJFcXF4ZQue9UQkLlQm0kzILtlW6lFAnbaIvucU=; b=8omZ8yxJTc4TAUq7kOMDt3kBwjOyrIC3BqUBgbXVPyjWHLnQtHYE9oRY25W9NrkTRoGl0W D8pD9DedMEZjKMWMB5f2I4rUoID382dSZ4cbun4mVCmvud/DOZ19GepRhTiOYrK0leSsc0 MclehWg+nrpHQy6VtW6p2It8x1HGI6o= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737559664; a=rsa-sha256; cv=none; b=asjGai39P5pZg19G86+bs0ockIMKzJmn/fayBXjoUMQLiXee5QiGn27SRJXu5mJbdecTi1 gFshwHti3MulzF4HMMZzT13CZbvw/T3Xsee9E9yEN8v5ttfUVocmBzd2XhdTbw26qflyJn hNIIu04Ah7QKQYwB7gt3eoh2TiAWRvg= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=13SGn2ce; spf=pass (imf03.hostedemail.com: domain of 3bg6RZwUKCBMCtuutz77z4x.v75416DG-553Etv3.7Az@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3bg6RZwUKCBMCtuutz77z4x.v75416DG-553Etv3.7Az@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4361fc2b2d6so38933445e9.3 for ; Wed, 22 Jan 2025 07:27:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737559662; x=1738164462; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=ZDcetJFcXF4ZQue9UQkLlQm0kzILtlW6lFAnbaIvucU=; b=13SGn2ce3iz27HjoYGyhYSuEJAH5RbCm4VB8b5OXuam1Px2uoprt8x+LnVHYiv5K6t GTkzQYMoe39j22htRpYeQ5bwPjKqHGgnTw/mxSqCMAgMhSsEwTg+RYMTu+psh/C2cU60 HGm93XgWDSXa2VbfPNhpM+Ctb2ym7wN1Jfh0WTkThUSocBFVG5R4swsmMOhjRnRHhu9G nSuZr9RKMhjaZZHzoVbqKEEsbquXW3K4oFpL53jxYx2xsC6szVMr2752LRJCc2jqIoZj I58xGjMsktA+1MyoZdD3f2FjteX7vRPINNWnxUtfFiAXqwLvhCmlxdfI9EMoAchQhdS6 MEIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737559662; x=1738164462; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ZDcetJFcXF4ZQue9UQkLlQm0kzILtlW6lFAnbaIvucU=; b=V5wV8tJFyQgogrFqMu9oy1uq9aWShET5LLgRzmR42Ep4EfhW+JZrqG2Xhk1Pa9is5f Wv6Xh2RUdqI3UejrxW75GM8ufrGB02d6BuHhZFaqBTy/tsQ3+g/YsbyeGz7DP14X2Hg7 ZGZQtwrMYLPU4Pdgx+2Yskb+8n5iI4g0mmuU2pW1qP28eoiAlsTbRUNT3uLhntrQ0Mp6 HzF3XjEJwtbMcHg9BwK0SmHXrHxvr5qD8ejVM0ONzns8oc19zmBW/ZlTj704kN/0bhiU UnLeP1EkUhyQyHOOtdigwYQ6lPyxzlausVLLRDsOfYYcMxfTebWAezhyBHJgmbtkOCcG SNvA== X-Forwarded-Encrypted: i=1; AJvYcCX4LNtTnyDiH0ToD7gvIDY1jCAbiJjs9SR9hg9pkdYenvccd5+J6MyzM+vaB7HTXMSomiXWASHeRg==@kvack.org X-Gm-Message-State: AOJu0YyD/75AtzKiCSZNZEaJPsprUlYxb7OowDeyYz7dIF4GJ6h6KnFH LgEksqm10p1SIs1IUsFz22Aa8PMq0G6Q+wdiW1666916+nc36AzHegL9OAZZ0Rxh7S0jtYAf2A= = X-Google-Smtp-Source: AGHT+IF8KsDXB6MILYgdI4bjHbLtTHjfa5HGKAzChHRDB4DnJXkZq0oaXJyTUpB5YHGdtSJAjVRTemZsTA== X-Received: from wmow21.prod.google.com ([2002:a05:600c:4755:b0:436:e755:a053]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:5101:b0:434:f925:f5c9 with SMTP id 5b1f17b1804b1-438913c85e0mr201044935e9.6.1737559662645; Wed, 22 Jan 2025 07:27:42 -0800 (PST) Date: Wed, 22 Jan 2025 15:27:30 +0000 In-Reply-To: <20250122152738.1173160-1-tabba@google.com> Mime-Version: 1.0 References: <20250122152738.1173160-1-tabba@google.com> X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog Message-ID: <20250122152738.1173160-2-tabba@google.com> Subject: [RFC PATCH v1 1/9] KVM: guest_memfd: Allow host to mmap guest_memfd() pages From: Fuad Tabba To: kvm@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-mm@kvack.org Cc: pbonzini@redhat.com, chenhuacai@kernel.org, mpe@ellerman.id.au, anup@brainfault.org, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, seanjc@google.com, viro@zeniv.linux.org.uk, brauner@kernel.org, willy@infradead.org, akpm@linux-foundation.org, xiaoyao.li@intel.com, yilun.xu@intel.com, chao.p.peng@linux.intel.com, jarkko@kernel.org, amoorthy@google.com, dmatlack@google.com, yu.c.zhang@linux.intel.com, isaku.yamahata@intel.com, mic@digikod.net, vbabka@suse.cz, vannapurve@google.com, ackerleytng@google.com, mail@maciej.szmigiero.name, david@redhat.com, michael.roth@amd.com, wei.w.wang@intel.com, liam.merwick@oracle.com, isaku.yamahata@gmail.com, kirill.shutemov@linux.intel.com, suzuki.poulose@arm.com, steven.price@arm.com, quic_eberman@quicinc.com, quic_mnalajal@quicinc.com, quic_tsoni@quicinc.com, quic_svaddagi@quicinc.com, quic_cvanscha@quicinc.com, quic_pderrin@quicinc.com, quic_pheragu@quicinc.com, catalin.marinas@arm.com, james.morse@arm.com, yuzenghui@huawei.com, oliver.upton@linux.dev, maz@kernel.org, will@kernel.org, qperret@google.com, keirf@google.com, roypat@amazon.co.uk, shuah@kernel.org, hch@infradead.org, jgg@nvidia.com, rientjes@google.com, jhubbard@nvidia.com, fvdl@google.com, hughd@google.com, jthoughton@google.com, tabba@google.com X-Rspamd-Queue-Id: E06392000B X-Stat-Signature: si391h48gtp8zk569xpfi6hii6dgitqy X-Rspam-User: X-Rspamd-Server: rspam12 X-HE-Tag: 1737559663-276078 X-HE-Meta: U2FsdGVkX1//HpbPAjqhNjKEZsltCZ/LxcrbeZjJcJwb2+N1ZoT7LcW5B0A904FNJoH7at7B3KDGOKhNIkBX7NeemoJy2y9CXPldfMTki2fi6D82/a2hUAZTKzeez6/xPTNQj4p605qBMUM96Myda7l+AqZSCbWrFu4AHar6Drl7QiGsV+lnwkpXFrKqL2TmtKYCOMirHXkeKNMIGlNS8tqpuFTd4hDIT/wa5cw/vBnkH83dDwBYIkQueQ840EY0X2xKHtlfOZFrAL93BHxiXfC925r05HzdNjsaksNfANmgMTeYtVbwjKX8PulU6HoP5DnIVRY/dQow9Viqgya/Ib1pvL4XlzjxjY80JlPpQVhQfmPneOSdfX8kxNc7NDAxl7ClISm+xwdAvY8M7YHfZKTaodh2vqyFWhY9H8HT7ynakU9iZHFRYy1g/a6041cnZ/kJ8me5cJfwXZZ61gjCzw1dWouHxRjx8+6GmPMV/9z6XERMpDHx8sD/PfTNXVYE0/a5ibpu86h86iJ5DgkJqor6fPFCKXD2Erj4sJxgSWMZ0jczM/kvIXbAXKD1Pf1TEO9J3xxGPUjjY79IaQHZ+HHPKbOmwTQ1ZpgHJGLly34BIwlQ0zNi+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=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: Add support for mmap() and fault() for guest_memfd in the host for VMs that support in place conversion between shared and private. To that end, this patch adds the ability to check whether the architecture has that support, and only allows mmap() if that's the case. Additionally, this is gated with a new configuration option, CONFIG_KVM_GMEM_MAPPABLE. Signed-off-by: Fuad Tabba --- arch/x86/include/asm/kvm_host.h | 2 + include/linux/kvm_host.h | 11 +++++ virt/kvm/Kconfig | 4 ++ virt/kvm/guest_memfd.c | 71 +++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index e159e44a6a1b..c0e149bc1d79 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -2206,6 +2206,8 @@ void kvm_configure_mmu(bool enable_tdp, int tdp_forced_root_level, #define kvm_arch_has_private_mem(kvm) false #endif +#define kvm_arch_private_mem_inplace(kvm) false + #define kvm_arch_has_readonly_mem(kvm) (!(kvm)->arch.has_protected_state) static inline u16 kvm_read_ldt(void) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 401439bb21e3..ebca0ab4c5e2 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -717,6 +717,17 @@ static inline bool kvm_arch_has_private_mem(struct kvm *kvm) } #endif +/* + * Arch code must define kvm_arch_private_mem_inplace if support for private + * memory is enabled it supports in-place conversion between shared and private. + */ +#if !defined(kvm_arch_private_mem_inplace) && !IS_ENABLED(CONFIG_KVM_PRIVATE_MEM) +static inline bool kvm_arch_private_mem_inplace(struct kvm *kvm) +{ + return false; +} +#endif + #ifndef kvm_arch_has_readonly_mem static inline bool kvm_arch_has_readonly_mem(struct kvm *kvm) { diff --git a/virt/kvm/Kconfig b/virt/kvm/Kconfig index 54e959e7d68f..59400fd8f539 100644 --- a/virt/kvm/Kconfig +++ b/virt/kvm/Kconfig @@ -124,3 +124,7 @@ config HAVE_KVM_ARCH_GMEM_PREPARE config HAVE_KVM_ARCH_GMEM_INVALIDATE bool depends on KVM_PRIVATE_MEM + +config KVM_GMEM_MAPPABLE + select KVM_PRIVATE_MEM + bool diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 47a9f68f7b24..9ee162bf6bde 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -307,7 +307,78 @@ static pgoff_t kvm_gmem_get_index(struct kvm_memory_slot *slot, gfn_t gfn) return gfn - slot->base_gfn + slot->gmem.pgoff; } +#ifdef CONFIG_KVM_GMEM_MAPPABLE +static vm_fault_t kvm_gmem_fault(struct vm_fault *vmf) +{ + struct inode *inode = file_inode(vmf->vma->vm_file); + struct folio *folio; + vm_fault_t ret = VM_FAULT_LOCKED; + + filemap_invalidate_lock_shared(inode->i_mapping); + + folio = kvm_gmem_get_folio(inode, vmf->pgoff); + if (IS_ERR(folio)) { + ret = VM_FAULT_SIGBUS; + goto out_filemap; + } + + if (folio_test_hwpoison(folio)) { + ret = VM_FAULT_HWPOISON; + goto out_folio; + } + + if (!folio_test_uptodate(folio)) { + unsigned long nr_pages = folio_nr_pages(folio); + unsigned long i; + + for (i = 0; i < nr_pages; i++) + clear_highpage(folio_page(folio, i)); + + folio_mark_uptodate(folio); + } + + vmf->page = folio_file_page(folio, vmf->pgoff); + +out_folio: + if (ret != VM_FAULT_LOCKED) { + folio_unlock(folio); + folio_put(folio); + } + +out_filemap: + filemap_invalidate_unlock_shared(inode->i_mapping); + + return ret; +} + +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) +{ + struct kvm_gmem *gmem = file->private_data; + + if (!kvm_arch_private_mem_inplace(gmem->kvm)) + return -ENODEV; + + if ((vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) != + (VM_SHARED | VM_MAYSHARE)) { + return -EINVAL; + } + + file_accessed(file); + vm_flags_set(vma, VM_DONTDUMP); + vma->vm_ops = &kvm_gmem_vm_ops; + + return 0; +} +#else +#define kvm_gmem_mmap NULL +#endif /* CONFIG_KVM_GMEM_MAPPABLE */ + static struct file_operations kvm_gmem_fops = { + .mmap = kvm_gmem_mmap, .open = generic_file_open, .release = kvm_gmem_release, .fallocate = kvm_gmem_fallocate,