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, From patchwork Wed Jan 22 15:27:31 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13947430 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 0C567C02182 for ; Wed, 22 Jan 2025 15:27:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 958806B008C; Wed, 22 Jan 2025 10:27:48 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 86AB66B0092; Wed, 22 Jan 2025 10:27:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 70CD5280001; Wed, 22 Jan 2025 10:27:48 -0500 (EST) 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 4F6996B008C for ; Wed, 22 Jan 2025 10:27:48 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 03C1A1A0395 for ; Wed, 22 Jan 2025 15:27:47 +0000 (UTC) X-FDA: 83035467816.07.9D13A38 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) by imf01.hostedemail.com (Postfix) with ESMTP id 210D340008 for ; Wed, 22 Jan 2025 15:27:45 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="JIo24/yH"; spf=pass (imf01.hostedemail.com: domain of 3cA6RZwUKCBUEvwwv19916z.x97638FI-775Gvx5.9C1@flex--tabba.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3cA6RZwUKCBUEvwwv19916z.x97638FI-775Gvx5.9C1@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=1737559666; 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=Jgc9nfq2iOlwc6s0mq0Zmi5Z5E3sXJP1ktmubqSKFNg=; b=tuOOyBgqM3RmtS5w0u2STJREFqpCNtoc3sBP261sVacQCfgEwEgcwY5KqaOIWszgpNm2xV a5QjqaeCgwLvNSj2KbN/QTxFJu8bwkcDflTI2+o5tugCz2gDpasO3eUenvTdkjKJai5GO9 d6DCFM6SaLt4hwwhQUGD1a/ejpuAv2Q= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="JIo24/yH"; spf=pass (imf01.hostedemail.com: domain of 3cA6RZwUKCBUEvwwv19916z.x97638FI-775Gvx5.9C1@flex--tabba.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3cA6RZwUKCBUEvwwv19916z.x97638FI-775Gvx5.9C1@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737559666; a=rsa-sha256; cv=none; b=k6yuf4huZGEdScpkIWUUO3lKmeTAwwR2XVOTsxOd297fxu78b1mk057Hi4sMYOkRXPUboM a7iA5PsYjBogKi49+0aj2h7pps6UDXv2yZMJ1XgJGrDsdRgsXXg/vTDwoOaYGltbSzVnjY rY2w/C4PsHPErkTIlwIU9sB4L+yeU7A= Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-43628594d34so38939905e9.2 for ; Wed, 22 Jan 2025 07:27:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737559665; x=1738164465; 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=Jgc9nfq2iOlwc6s0mq0Zmi5Z5E3sXJP1ktmubqSKFNg=; b=JIo24/yHtvAgDCE9bJsSv2rGHgygZegVZ8uM/7gfVUoyzuqKpDXJO+1RsC9vVtMwsy 1uMAmCIAAiWhLQzng4jM3YEBIs7vLMLRwcFpd4QGD5eV2QS3rjngFMIaA4bGCFvH2tc0 Z6CczEdDep0AHs6q8rMtkNqNprXmgjsflNDTVaoQV1l3SGppZYUloxMJdZqrPrZkKoBU oN78wjgk57Lz5XIMf6cOr+KKW47ZQZxEcQsLgKMeA92PzcZawsJWX/Cld1vDxWDGUSEf 1rRmaa/1EakbI8gFFvIYLLUpuPjruyxXRy2wp/e/cQ4hjqC1H5YJmOK7ZYHOz/z11f9m ViVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737559665; x=1738164465; 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=Jgc9nfq2iOlwc6s0mq0Zmi5Z5E3sXJP1ktmubqSKFNg=; b=Pt39U9pQeUWira+J4FzSyXdMm6P8C53tBrIyQwYfdBIr7vuKD8GKgcHJGVQMTtX0oS ScJsD7t2aTVkBbJo/GTe2wYJVAn1bz6eD3x50b5unOoHG3lusQanlErBAH2vU32oUNOe /oD9Hqo4N7Oz/xmNSV10hdyYNvp1vAw8fCdSzLZcj1EDUa288lFQ2A4iA5L0vqWVM65/ EmMYjFCHsZ7sxGTPI8uUeL2siW9E3ICcRwrcxL9w8yxTJoIkahdiDJbtHNMzd/xtchhQ Sng1h0fp3m9Dw+bsSxRHGqI5XJ0PuFSknx4xUbhg4zYvNrnXP1FQ7aD4awRno91zhrtv /+ig== X-Forwarded-Encrypted: i=1; AJvYcCVc0oxdcfjbiuO5RFZhZ5KqyGvfUi2eOvlhF1yzfWhg7n54fuAnAS19LtX8KwvBKFqG/zBrh5rgIg==@kvack.org X-Gm-Message-State: AOJu0YyOzmI03hTAIP4K+FTS3wpoayHN69PAexSjvNiBMg2Z7UOJUrmb j9G9aqkF4OXUnqpYJFXzzp7hAdf0G0VJtOuQCzmJyJWeMeWho7rhUcD0ASXrH/c3rFhESZrbGw= = X-Google-Smtp-Source: AGHT+IHEkzSzLwVsFu5ntmtLdr5cJ4QHwoT2gxzzZv4Yr44awgwt7PF8ZqHc/3pFw4mwJzLSVOUAJ9pbQg== X-Received: from wmof18.prod.google.com ([2002:a05:600c:44d2:b0:438:af71:5fbb]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:310a:b0:434:f335:855 with SMTP id 5b1f17b1804b1-43891440ab1mr187994315e9.28.1737559664727; Wed, 22 Jan 2025 07:27:44 -0800 (PST) Date: Wed, 22 Jan 2025 15:27:31 +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-3-tabba@google.com> Subject: [RFC PATCH v1 2/9] KVM: guest_memfd: Add guest_memfd support to kvm_(read|/write)_guest_page() 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: 210D340008 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: drmekgd1ioofk1xs1osxeirkoc1bywd5 X-HE-Tag: 1737559665-984697 X-HE-Meta: U2FsdGVkX186/BDB5Wr2nuGUWfCG3CeFMoIignDSZ/LtVJJiJJ7RMP+NCPIfFkGu2VAcYaRyZa3wQxj2ovS8NSWtRrOWMVgwzHeWfb/WbgK5scsXJc5tBySu9vGwCl/0F8Ha6Gfpnyc9n7Ws8cJC9RMb3yyxe/a+sWDvCBStfRel0RGWbjOdzwev737MGAetMFw6uiAtYrdHLjeG3ha54dPqUkrkx2CWJ4I/BH6LVt98+yuydsCnuR1dTwVz2P+ZWbbMp+YkVPWu24P2Zxb6sJ9I5nvFPW3WoeOQ4R/6Lc+vRnVJm9iEPnHCERZEl83mOlKXElXjMq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rOl1nQfuR7ZTy3wjzgQ5dzqglxmn4Nxozu77aRCGxHU+hDKFMqBpzRZk/qyjhRUPdpT+UdauzAV8Me/i9TFztuhZJ/2TzwImWkrUwYY7d2ZxkzQ/+2VHzLUUA+Fcq7X/hcSUmUr2b6CECIcwcS6LsJEmiIOJkxXpswB/k9vUYB0z366mO8X/8rQHCvW3yw2EnmS8Wh7zcWqUuDo1gvLZHlU65hw/xZxOukYpAqh/4ez831l4+dGfOUEQ== 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: Make kvm_(read|/write)_guest_page() capable of accessing guest memory for slots that don't have a userspace address, but only if the memory is mappable, which also indicates that it is accessible by the host. Signed-off-by: Fuad Tabba --- virt/kvm/kvm_main.c | 118 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 99 insertions(+), 19 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index de2c11dae231..ad9802012a3f 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3094,21 +3094,93 @@ static int next_segment(unsigned long len, int offset) return len; } -/* Copy @len bytes from guest memory at '(@gfn * PAGE_SIZE) + @offset' to @data */ -static int __kvm_read_guest_page(struct kvm_memory_slot *slot, gfn_t gfn, - void *data, int offset, int len) +#ifdef CONFIG_KVM_GMEM_MAPPABLE +static int __kvm_read_guest_memfd_page(struct kvm *kvm, + struct kvm_memory_slot *slot, + gfn_t gfn, void *data, int offset, + int len) +{ + struct page *page; + u64 pfn; + int r; + + /* + * Holds the folio lock until after checking whether it can be faulted + * in, to avoid races with paths that change a folio's mappability. + */ + r = kvm_gmem_get_pfn(kvm, slot, gfn, &pfn, &page, NULL); + if (r) + return r; + + memcpy(data, page_address(page) + offset, len); + kvm_release_page_clean(page); + + return r; +} + +static int __kvm_write_guest_memfd_page(struct kvm *kvm, + struct kvm_memory_slot *slot, + gfn_t gfn, const void *data, + int offset, int len) { + struct page *page; + u64 pfn; int r; + + /* + * Holds the folio lock until after checking whether it can be faulted + * in, to avoid races with paths that change a folio's mappability. + */ + r = kvm_gmem_get_pfn(kvm, slot, gfn, &pfn, &page, NULL); + if (r) + return r; + + memcpy(page_address(page) + offset, data, len); + kvm_release_page_dirty(page); + + return r; +} +#else +static int __kvm_read_guest_memfd_page(struct kvm *kvm, + struct kvm_memory_slot *slot, + gfn_t gfn, void *data, int offset, + int len) +{ + WARN_ON_ONCE(1); + return -EIO; +} + +static int __kvm_write_guest_memfd_page(struct kvm *kvm, + struct kvm_memory_slot *slot, + gfn_t gfn, const void *data, + int offset, int len) +{ + WARN_ON_ONCE(1); + return -EIO; +} +#endif /* CONFIG_KVM_GMEM_MAPPABLE */ + +/* Copy @len bytes from guest memory at '(@gfn * PAGE_SIZE) + @offset' to @data */ + +static int __kvm_read_guest_page(struct kvm *kvm, struct kvm_memory_slot *slot, + gfn_t gfn, void *data, int offset, int len) +{ unsigned long addr; if (WARN_ON_ONCE(offset + len > PAGE_SIZE)) return -EFAULT; + if (kvm_arch_private_mem_inplace(kvm) && + kvm_slot_can_be_private(slot) && + !slot->userspace_addr) { + return __kvm_read_guest_memfd_page(kvm, slot, gfn, data, + offset, len); + } + addr = gfn_to_hva_memslot_prot(slot, gfn, NULL); if (kvm_is_error_hva(addr)) return -EFAULT; - r = __copy_from_user(data, (void __user *)addr + offset, len); - if (r) + if (__copy_from_user(data, (void __user *)addr + offset, len)) return -EFAULT; return 0; } @@ -3118,7 +3190,7 @@ int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset, { struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn); - return __kvm_read_guest_page(slot, gfn, data, offset, len); + return __kvm_read_guest_page(kvm, slot, gfn, data, offset, len); } EXPORT_SYMBOL_GPL(kvm_read_guest_page); @@ -3127,7 +3199,7 @@ 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); - return __kvm_read_guest_page(slot, gfn, data, offset, len); + return __kvm_read_guest_page(vcpu->kvm, slot, gfn, data, offset, len); } EXPORT_SYMBOL_GPL(kvm_vcpu_read_guest_page); @@ -3204,22 +3276,30 @@ EXPORT_SYMBOL_GPL(kvm_vcpu_read_guest_atomic); /* Copy @len bytes from @data into guest memory at '(@gfn * PAGE_SIZE) + @offset' */ static int __kvm_write_guest_page(struct kvm *kvm, - struct kvm_memory_slot *memslot, gfn_t gfn, - const void *data, int offset, int len) + struct kvm_memory_slot *slot, gfn_t gfn, + const void *data, int offset, int len) { - int r; - unsigned long addr; - if (WARN_ON_ONCE(offset + len > PAGE_SIZE)) return -EFAULT; - addr = gfn_to_hva_memslot(memslot, gfn); - if (kvm_is_error_hva(addr)) - return -EFAULT; - r = __copy_to_user((void __user *)addr + offset, data, len); - if (r) - return -EFAULT; - mark_page_dirty_in_slot(kvm, memslot, gfn); + if (kvm_arch_private_mem_inplace(kvm) && + kvm_slot_can_be_private(slot) && + !slot->userspace_addr) { + int r = __kvm_write_guest_memfd_page(kvm, slot, gfn, data, + offset, len); + + if (r) + return r; + } else { + unsigned long addr = gfn_to_hva_memslot(slot, gfn); + + if (kvm_is_error_hva(addr)) + return -EFAULT; + if (__copy_to_user((void __user *)addr + offset, data, len)) + return -EFAULT; + } + + mark_page_dirty_in_slot(kvm, slot, gfn); return 0; } From patchwork Wed Jan 22 15:27:32 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13947431 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 CE455C02182 for ; Wed, 22 Jan 2025 15:27:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 141BD6B0093; Wed, 22 Jan 2025 10:27:51 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0F1056B0095; Wed, 22 Jan 2025 10:27:51 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E85306B0096; Wed, 22 Jan 2025 10:27:50 -0500 (EST) 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 C9BB26B0093 for ; Wed, 22 Jan 2025 10:27:50 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 5C8C0120379 for ; Wed, 22 Jan 2025 15:27:50 +0000 (UTC) X-FDA: 83035467900.19.DE335DD Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) by imf19.hostedemail.com (Postfix) with ESMTP id 7F7F21A000A for ; Wed, 22 Jan 2025 15:27:48 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=PuB7O4Ms; spf=pass (imf19.hostedemail.com: domain of 3cw6RZwUKCBgHyzzy4CC492.0CA96BIL-AA8Jy08.CF4@flex--tabba.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3cw6RZwUKCBgHyzzy4CC492.0CA96BIL-AA8Jy08.CF4@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=1737559668; 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=FPlZyanj/PcXychEccc3pvyCzWy399cCYc+EAQwmx4Q=; b=19B4lgd1adFYadLYEVQecouJJStFkur9b5hb0VV6mV/MwfkFCJ7NBIOTWxuWeODaRACjQp 66vVJ4/6jp7c0WpAyW8rs5vssNJLP6yYjV5OokGIGgXArpFZ5tjXqFFwIuwsPPlJZ2q2GH /6Nv0n5r29Vw9YNIGOSiJqB0IaBSxRk= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=PuB7O4Ms; spf=pass (imf19.hostedemail.com: domain of 3cw6RZwUKCBgHyzzy4CC492.0CA96BIL-AA8Jy08.CF4@flex--tabba.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3cw6RZwUKCBgHyzzy4CC492.0CA96BIL-AA8Jy08.CF4@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737559668; a=rsa-sha256; cv=none; b=0soSqCi9rRvmfG5aGTcX8nbziudWGThFMMkFgrO/umrpBkJ0twq38mC5R560fHWomDT5En cHnu/hvwx4qwihtKvbxK1v5ve0Ht3vsfLAEBn07mDT9Waw24y2KfJUlifnBL9P9WUzp5QU n47W/5spju+AVgW891MuYJQpwyo5omQ= Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-4361d4e8359so53591665e9.3 for ; Wed, 22 Jan 2025 07:27:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737559667; x=1738164467; 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=FPlZyanj/PcXychEccc3pvyCzWy399cCYc+EAQwmx4Q=; b=PuB7O4MsWZqiJMtSPocwglWmBY4/rJY+IW0jE296R698zRw4m/Ic4IS3EMdq7WX/PV 4fpXNgx21Z3pOWmg81N8kG0mRS7sLCe5jaNPCY7QCcczLtnDp4qfr91Kf1WEYHmCzKwA jy64wpqDwAFyxapN78X7Tny9A9ifswWYnb6XCI7wKv01STYl+oKwKvlPhy03sTtjx6bw 4oFMLWUmkSV13ZPHPai6sXqveiLn029PEgR1U8aT75ODAsJiP5pELkEbelDgn7lJY8Jm 9IDraQbWGEuxslSRDRMTY0S711SPxiCYPyMa3ThSHeFu9kP3gSNbvJCzaji+04ERILF6 9PBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737559667; x=1738164467; 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=FPlZyanj/PcXychEccc3pvyCzWy399cCYc+EAQwmx4Q=; b=nbQjapObDl9w/f3corypXAgW2Xd5+/q7gdxo6JwV7Ieym4X3nDwjbgSHFwTXzQtdjs 6xLTHNWaO+sSmF9qqPwbL+SirnJGGH2O/FNcF3WVIhN/svyzj+G7QkGMXt5z2SWTbL9F 4/6oCPFmw9Fo9hzfzbtYRmJb9GoADLKNU6J9FsRD9SRfWUmu8srZEdwo8CZ1Yzz8IOSl 7zUda3+83/sslvCpXVAUMddZhsMx6IH82xvgDAp2PPIHLk2xy3eR2r8V/dqaRiiB5Fs4 3V71vmZqPgTENQuQ+/9RRs95XqChnx8aSLF1XWgX7Maa3GLyWTCasLq0fja9uur3rwqg YT7Q== X-Forwarded-Encrypted: i=1; AJvYcCU5a+cxvxPiFVdDclqjIcbV4u5SnIVN1iLeHzO2FVy56gh2w7dTWq5Fp0nk/fCdWVy8dianPgSa8g==@kvack.org X-Gm-Message-State: AOJu0Ywo54P5cAZhKTDzyb2O86e/GIEuW0qOt56C1Q323UnADxS/Gvdb qtQRP4c2GVD3uxvR4wXzJg2CsklT9tcSMhS2MGsE/SkuvxxeDHghIEXAU68TFgg1Za8T0bPauw= = X-Google-Smtp-Source: AGHT+IErLAZbRWKwqQFdhagPuo0iBKk0JPSfX6aLNC1OqdIBiMLMztGVedsgoVH8z63knrYGAMOhcPpfwA== X-Received: from wmbbi26.prod.google.com ([2002:a05:600c:3d9a:b0:438:ad3b:591a]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:3b0a:b0:434:9c60:95a3 with SMTP id 5b1f17b1804b1-438913ca93cmr223239065e9.11.1737559667157; Wed, 22 Jan 2025 07:27:47 -0800 (PST) Date: Wed, 22 Jan 2025 15:27:32 +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-4-tabba@google.com> Subject: [RFC PATCH v1 3/9] KVM: guest_memfd: Add KVM capability to check if guest_memfd is host mappable 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: 7F7F21A000A X-Stat-Signature: apa5m7ufkwmccx1fhxnncf7ta99ju7n6 X-Rspamd-Server: rspam08 X-Rspam-User: X-HE-Tag: 1737559668-694904 X-HE-Meta: U2FsdGVkX1+JxSbIJu55VnoAcnkbGUBruVcdduFllDiauggLgBS6xdQvU1R+3TL5a6Q18YN4rK26KIUTYT9r3A6xOGalE1iLqicMPox+dScvbAaJiIkInxoUXz3qQrZ4XzMFZOCDrvrrIV/7UxtGy5H6KJq1bhyIp6TbsG6ByuDIyXwB+jnUlbhcmLOUD7NxqP8hmsHdwgMINfYNbBmn/Vp0T+0boL/6TITctMM9Iq/ThzlbSofRYqwAmOhaX+F52k0JMG2EgvFFR4YtzZrw8ghHYs748YfaBwuDsoRQHI6r4vkfADbNNPhAgyMNtVXOt0CcnbtkCTo0gS0MTpS0ZeMCLfZRNRIdBk8qc0kTY1nkoy5OvskU0yXTkeBIIICdD4+rQ5ulXbt1kVM4A8YvVR/U+IRxpThmzoX+lKv33g1P0EIamwM3dTeqQkba2U/ptsrmN06pEDBE9GlUw8RgEs3gI1EFpqtxtbOo3S3xRa9kiuFHQyK5tYSMvCQX9UCa5IJ5fQangjuiYJB4uqBY4liGnz5Auom2EaBxHZS3Yixq5g2fTJIBLpVP5V7YatEzH4xq0hrt+2z3rHpyB+Nv+SjkTXDQWe5Oev5v8SlMwSkyQEfgQsXpAQ3MGSEpkN61rmU+npBvE6q5P58GxzJ8Wxpdw5TMDYZOyxI8K+ub7S3afhUXApQvATdFvBN9jQfsnImYu00/49Rcgm6CNbUgIDlVR86Sg5+ftddC/G+4MraN0ydVUWR0dce1nm4Y5Q9AnxjleZmG65YLwePiyhzWQZx6GAa8SLkF31mPUwiMz/slK/K0P1/SvDWJ478DXVCJHgyo3lIxNoOtC7jIJ9p70ypF3qlD9vLfCgqh6jgRULTvAb12A+sDb9DjawGomlQ1qUateqArb+QES24AQIga47rqtBh4fj0SbDALnRRaHey6u3gff8mXwErseQgsOlbhlNDryuILKZ2OCYfAdVj /CDXMvfn y2Q9hS5c60F/Nn+fMancGE45Cn4hkDQnkpe4sMKV0mMoiqeIOzSZ807GeKxX7tHOsioK4WpDjDF5pFax1ier/eY99c0Eozh7UJp+jd2OkruwWkhW+tLqVwIcnPqOXdvCY4h59rKuN46IX6NH8v6SoSWakDyMcCCeQQJrS94kAwsDtjf7GzZCKP9Nokmrn5qDuBDif0ToeWAXe+/zruG6V0w5XIReaN0BSibbJOjx/NsDYH0bg1nbxvZT9Ro43HhY5Q6rKFBAeo/Y4ufDP48UEuX7pGNJdNmU5wQjaO2zV7ViIX9rTYUKqQRVyqo+e+tldAczkUPSMMHVvtB3VbYr6pVinyhXnn+aa81TNjzNsGgDXSYBDEw/jeBoRO5MiIgi68lZXTnRu0srldJvAS5QqBOmHrMDdiPgsLtPBb3FEYQ4/mDo4W9YBTw1yYPyl+VnOWOwhVHcKNiaZqL51IYRn98KPEqrZFCBVN8JLYKHXrUyj6pwDdZVRrn1hruPH7evS7kgTXTi2UFEPs6oCZsDUym+Pu7CzS2uxZNMJOb40AnK9rCQthOGEYISLAq2nW0Ibqbir6VgZK14+Iu8hO6VuWzgsuAQ4Fh2a8egd+l0APHOrPqh2OqawVpOCb7of38fTBMq3guHaegi3Nf/BmSvQPdWFWA== 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: Add the KVM capability KVM_CAP_GUEST_MEMFD_MAPPABLE, which is true if mapping guest memory is supported by the host. Signed-off-by: Fuad Tabba --- include/uapi/linux/kvm.h | 1 + virt/kvm/kvm_main.c | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 502ea63b5d2e..021f8ef9979b 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -933,6 +933,7 @@ struct kvm_enable_cap { #define KVM_CAP_PRE_FAULT_MEMORY 236 #define KVM_CAP_X86_APIC_BUS_CYCLES_NS 237 #define KVM_CAP_X86_GUEST_MODE 238 +#define KVM_CAP_GUEST_MEMFD_MAPPABLE 239 struct kvm_irq_routing_irqchip { __u32 irqchip; diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index ad9802012a3f..9cd6690b7955 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -4872,6 +4872,10 @@ static int kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg) #ifdef CONFIG_KVM_PRIVATE_MEM case KVM_CAP_GUEST_MEMFD: return !kvm || kvm_arch_has_private_mem(kvm); +#endif +#ifdef CONFIG_KVM_GMEM_MAPPABLE + case KVM_CAP_GUEST_MEMFD_MAPPABLE: + return !kvm || kvm_arch_private_mem_inplace(kvm); #endif default: break; From patchwork Wed Jan 22 15:27:33 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13947432 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 BCDBBC02182 for ; Wed, 22 Jan 2025 15:27:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F28486B0096; Wed, 22 Jan 2025 10:27:52 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id ED3846B0098; Wed, 22 Jan 2025 10:27:52 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D748E6B0099; Wed, 22 Jan 2025 10:27:52 -0500 (EST) 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 AF2506B0096 for ; Wed, 22 Jan 2025 10:27:52 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 690BB1C643A for ; Wed, 22 Jan 2025 15:27:52 +0000 (UTC) X-FDA: 83035467984.22.2ED17AE Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf11.hostedemail.com (Postfix) with ESMTP id 981E140016 for ; Wed, 22 Jan 2025 15:27:50 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=dKhPmb04; spf=pass (imf11.hostedemail.com: domain of 3dQ6RZwUKCBoJ01106EE6B4.2ECB8DKN-CCAL02A.EH6@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3dQ6RZwUKCBoJ01106EE6B4.2ECB8DKN-CCAL02A.EH6@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=1737559670; 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=shrLyHvMMRQKlPtYXYDSAaDe6YKxFfwFxlxNmwxqD3g=; b=irwxOC2OdPes8HchfBN1KAedIl1gzH7Gpl/KA/PubeEa8OVZin865ZsHxbyzLKLIM9ahPW 5lHsbk+7ZnpMs33+5ccczXQACN2Ps+SVcRRjxSVAv4EnebASfIM/fwu0QCmmhohsJf/9Ra iyUbvu4BlrJ0ZgJZDsgjO6bzLN9LLIU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737559670; a=rsa-sha256; cv=none; b=RMNVDljCce8yGiluQg4Y7zJawUPoaPf2rLaK8f+XVJzsy0R2/qAx5XjHkA/58ilHnx+doe AwREsOj26bFTe3mkehsXLIi0dKeisal3qn+sRtWav7BCm6vBwoPQzQCtxxjjWTLyA9RJKX eVXq4q0to1Nri5NfAumNy7FEG85Xkuo= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=dKhPmb04; spf=pass (imf11.hostedemail.com: domain of 3dQ6RZwUKCBoJ01106EE6B4.2ECB8DKN-CCAL02A.EH6@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3dQ6RZwUKCBoJ01106EE6B4.2ECB8DKN-CCAL02A.EH6@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4361c040ba8so38966425e9.1 for ; Wed, 22 Jan 2025 07:27:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737559669; x=1738164469; 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=shrLyHvMMRQKlPtYXYDSAaDe6YKxFfwFxlxNmwxqD3g=; b=dKhPmb0412eZ55hfyRMfphpH6CeyGP84GgxR+Iw2zOs8+FaXcelUr8HNgzMHUcNrI1 dmWb5wGNclo6teaawqlichw0tZW9a9OMDgfjYcRIkPlMAGdq/a8X2sMswmoGqGtknfBc oNvfnB9C9/Io+wnOnWt/rIO4ZDvdoDMrvEWcCIS+JHF3xsm+RGmJOC22O4+gvSM4Lkn1 1aWBDJY0/UfcBkJCdXCYy7d9P6KZ5oWkqFnl/zTHl58uEQpiqgeV0lfK48Zaag8EG9v3 R3NRBKgNmY7yyxbgCi2lO7VUkXT5taPIgur4+VinzCy1x1w8/wbUymFR51yC/lJ17vD6 1rTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737559669; x=1738164469; 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=shrLyHvMMRQKlPtYXYDSAaDe6YKxFfwFxlxNmwxqD3g=; b=LVQifRMMimsSx7B0t4WHcobCpIxPEMTdiq0V0gTYUfdHkpi4rbn0/H9yZEwJQlMUiH cbcJaXc926+5dMf/bG6uwRLNb2bUwtBqFPjM3Qhljyrct1Q3PMRsqhrcd9rLlkjQ9+4T R89ubyq0wgFnMvJA5+5xyJZm4Aw3LW0h+xVWTExhCD4a8jHRmUkCp5n84ENFa9+plEWD DHxXD4J95DsDLIOBrJC/d2QBcexA71PSDzecgZqJWGz6jjoISYIu6Ff3oLtKTEvCatiN 4XOJxF4ho/5FwowQvK9OC2VZBCk90R7PjAdIwGGRFQ8mYfYBhLjzej4JRbtDXTz2RswN yflA== X-Forwarded-Encrypted: i=1; AJvYcCVS8Qb+yrAUnXe3V8jujH7LsqsLpLPaD6jD0B0Rs/6RgYhUGntSN/FewpNN7tuheuHblqUzyeeUOg==@kvack.org X-Gm-Message-State: AOJu0YwQC2RhYlPva7Q7JiEGxWTe9sJjuQcM2TKfeU3IKWBkXWcFfeQK xRrkH8L3k5AUeI00RdYay79vF/tDwxbvGTz/vy4PKcoER+Eej2l4JcKyNMlvtgYc3LFaS/+hRQ= = X-Google-Smtp-Source: AGHT+IHTlIkTn1+cn775KydFPeyXeNoykYbZ6JOdiKLvBv9T8k9MPGDx/xX8UX12ynhVUVVwAikKzSjQIA== X-Received: from wmee10.prod.google.com ([2002:a05:600c:218a:b0:434:f1d0:7dc9]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:3585:b0:434:f297:8e85 with SMTP id 5b1f17b1804b1-438913d5da1mr237493785e9.10.1737559669276; Wed, 22 Jan 2025 07:27:49 -0800 (PST) Date: Wed, 22 Jan 2025 15:27:33 +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-5-tabba@google.com> Subject: [RFC PATCH v1 4/9] KVM: arm64: Skip VMA checks for slots without userspace address 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-Stat-Signature: 6x65wmeku5oagefgdrxa4p9xxukhm63k X-Rspamd-Queue-Id: 981E140016 X-Rspam-User: X-Rspamd-Server: rspam06 X-HE-Tag: 1737559670-653114 X-HE-Meta: U2FsdGVkX19kWkGeXnLte8tclUc9gvDd2Bl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q5EjTYpFwzy3V7tb8se80Fk74T57A0Dl2smrZdiHJ5OnXs6AFNN4zMfu+mcqDCvFGJvgBTpLXJclR31RTm5EZQnjpgqEucOUhgVWzfML8Uxfq3uvyxwgqejeXtVFWuq9B4SU4r9MdgwQxFHoxqVFF8qpWxpk/518QQqoaznth0Uxw+P7wq5GfM1sj1T/bse7u7j7FO42fou8rDItcZ2bGX1ES0WCchlzdrpwyv/eXG54lDJtQ5VdENPCUtGDrQtNjdelVaqCOxgBltDogb5rhDCHLjmqmgjrAfdUUz96qJY1xw27ad+9zFQRWlJnbMUSq+lc7cC4RTNGVmtCpX2IR5Fg18T3aWOMP2ZBkOxN4Ysby1L5udvHes9dmW+xaRJkgSH8w2hvX3UyQhLWLrHKohsHpR8kXPSZUgOFJEvPAZhJfY6JLDd7EqLJgqCeAHK4mdobReiSNZaKtBq0ZulGVrNO/CJZHybJwaQCsn5HRtbFmv2KuiF81ywoCrEcI/oSu 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: Memory slots backed by guest memory might be created with no intention of being mapped by the host. These are recognized by not having a userspace address in the memory slot. VMA checks are neither possible nor necessary for this kind of slot, so skip them. Signed-off-by: Fuad Tabba --- arch/arm64/kvm/mmu.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index c9d46ad57e52..342a9bd3848f 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -988,6 +988,10 @@ static void stage2_unmap_memslot(struct kvm *kvm, phys_addr_t size = PAGE_SIZE * memslot->npages; hva_t reg_end = hva + size; + /* Host will not map this private memory without a userspace address. */ + if (kvm_slot_can_be_private(memslot) && !hva) + return; + /* * A memory region could potentially cover multiple VMAs, and any holes * between them, so iterate over all of them to find out if we should @@ -2133,6 +2137,10 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm, hva = new->userspace_addr; reg_end = hva + (new->npages << PAGE_SHIFT); + /* Host will not map this private memory without a userspace address. */ + if ((kvm_slot_can_be_private(new)) && !hva) + return 0; + mmap_read_lock(current->mm); /* * A memory region could potentially cover multiple VMAs, and any holes From patchwork Wed Jan 22 15:27:34 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13947433 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 E6B60C02181 for ; Wed, 22 Jan 2025 15:28:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 671C26B0098; Wed, 22 Jan 2025 10:27:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5F9636B0099; Wed, 22 Jan 2025 10:27:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2EDED6B009A; Wed, 22 Jan 2025 10:27:55 -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 0B6776B0098 for ; Wed, 22 Jan 2025 10:27:55 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id B4C041603E7 for ; Wed, 22 Jan 2025 15:27:54 +0000 (UTC) X-FDA: 83035468068.24.1F5ACFE Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf27.hostedemail.com (Postfix) with ESMTP id D0DA44001A for ; Wed, 22 Jan 2025 15:27:52 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=3rnVH0T4; spf=pass (imf27.hostedemail.com: domain of 3dw6RZwUKCBwL23328GG8D6.4GEDAFMP-EECN24C.GJ8@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3dw6RZwUKCBwL23328GG8D6.4GEDAFMP-EECN24C.GJ8@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=1737559672; 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=3HB24ys9olZtoFbzLvLFGPKqOLdaE1YgibZ0bUHAkc0=; b=PzeN4AnfbI1sOlSxJ0uZ9au+d86lQ8z7s8WrAfevEaumUEQwzwqI09B9iPbnxj4ht+tftG 18Aaj1aY0B7/4H9Xu8GijTzoJuiUNH3dSvyn1YygZ+2Ac18ucawQbCzqY2SIdlIy/WiJAv N8HNPMHgRXkYn75GrfaXnRrNWFPE78U= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737559672; a=rsa-sha256; cv=none; b=WZjaicslwH/deyrojjdunRIfDVWW4aBY/Ol2HU1GRKFkwvVtxh48WDj920O6PFM7vqUdFm SYeK9W9FlUiF7z6Z3iGo4lQsNyFmbjgde9meOlRjylUFzwD9fekxlvrd7R4IQZi5ESJ0Tz J3ntYDMhEe1wQMeC84szkF5Y9g8BzJ8= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=3rnVH0T4; spf=pass (imf27.hostedemail.com: domain of 3dw6RZwUKCBwL23328GG8D6.4GEDAFMP-EECN24C.GJ8@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3dw6RZwUKCBwL23328GG8D6.4GEDAFMP-EECN24C.GJ8@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-43625ceae52so38341545e9.0 for ; Wed, 22 Jan 2025 07:27:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737559671; x=1738164471; 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=3HB24ys9olZtoFbzLvLFGPKqOLdaE1YgibZ0bUHAkc0=; b=3rnVH0T4+jzHST3nO2oko+JlilRYkPJl5F9cUSXiV8QF1+Ts36T2o/LvLnc/Njyc9X x0SQEhIjGF8xzPMJ8gMZmKa31ZLfcmWGJLplfo3ehxoAN46gwex6Ajda6AEjqJlb09+F Wvh9a14wlu/3BSFU5dOLgvaVlmyNciT5vpQIDzQM1Z3NNG3FrfOzNIZFpLbRh2wwGWUe pwIvVmJs1SYB2+BXZBpIRfXgGIlSB/unj8OwxtoNy+BgRwPl1n0OdTCuUVwQMFk8cLvF BD8BmS5mimkBUKPqjzzluD7xvLNtfCFrsECvHrNnmooLGPAD3v2+T6HdbeV4DrYuF3X/ daXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737559671; x=1738164471; 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=3HB24ys9olZtoFbzLvLFGPKqOLdaE1YgibZ0bUHAkc0=; b=iG4AuXYkTh+CSMzgMqlB4iu4XlVrKGYUJHOLNmpJnkLqx51YZMiTj+5LgZrXexADCM MRfI57pjhxpa1q+rNm9/rmcjTgImZzQk2rUyXyip6wWmWE69X4TXkcMrq+VYSMC3nkOq M9diSDWmrWE2th/P/+uztRCKW81Df21VAsWg88jxDyF8rfCue0P3f5RLkgC3PvA1IATE ssAcW4E8nClzPGmxKIuMvx4XzxsnibYFGNgHp7M6ScHkzHQieXGix+LX0FZAhKTPb2o/ M2zcn0g1cDMZ06NqcocYj7OoWWg3DSjuevMoDDdBYrdOm2MnhiaVU0D1G/qePaR1THyC YYRA== X-Forwarded-Encrypted: i=1; AJvYcCUGs+lVNj2HTpW2R1YxAt32bAnV3MpM+pKZcmRa4wDQMRlspLzmQdyVYL3RlbxC28ssGhcJpbGo1g==@kvack.org X-Gm-Message-State: AOJu0YwXvyeqMTt2TdpSXXyTb4StD1aR0saD5U2tis3THusBIccbhP33 NbWvTU6pZFpA7c5sisq5RwBDuqHOYB97y1SoHwEwnr2B/rnlNEI28YTPMBupgkgu5xqCcR7vxA= = X-Google-Smtp-Source: AGHT+IGcoJWLBW4MQy/zV2gFico8bxDEFCQzZhOhvpQ0d8FBU2ybuc79Jv0oLXdNK7/4bqI1+6VtomwTiA== X-Received: from wmbjv16.prod.google.com ([2002:a05:600c:5710:b0:436:e723:5be6]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:5112:b0:434:a5bc:70fc with SMTP id 5b1f17b1804b1-438913cfa0emr202083105e9.8.1737559671492; Wed, 22 Jan 2025 07:27:51 -0800 (PST) Date: Wed, 22 Jan 2025 15:27:34 +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-6-tabba@google.com> Subject: [RFC PATCH v1 5/9] KVM: arm64: Refactor user_mem_abort() calculation of force_pte 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: D0DA44001A X-Stat-Signature: x9rgq6et5qo8h97x53epzc53pyaon5mi X-Rspam-User: X-Rspamd-Server: rspam12 X-HE-Tag: 1737559672-758530 X-HE-Meta: U2FsdGVkX19aIFShcHTy13DZa9a8k3vPEj+FA06weERxVYqpRx6itsARoEs6ThBxuzqZpoDgJ3oPgttlWR44ejusngIL+0aLnI5PUjV2RVe01gyZb3zpWdiRNK6WKSJrRiVtnggnPgJKR6NP63HZuvu1lUnXN8MXPafeg+VtO35cPZdIGbKt8lFbdWXYkH8B3msHpI9i9qoTiahqy8rzXwy4T2bsJcMhN3SplIF1X9AwtG0+JQ1qWUVH3psMTne3QQnlp+CeEnqmsEcO7ux3Tn5I2bGSo2jjHftU66iLmzBYYNxLfoA13t/EMZ6KZunp8G0zb2iA6lvecgfLIkVh8qufUky9vi2kYl+R9gXGW4Im3SK1EZPVkga3buTPYTY0yRhucFCtMLWjwmEI0A2hyQoh1YvOckgnJ8ilgkPfH92+kVVtr/IDaI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I4MltJ2+L87ICGfrmTTGdeqJNqvncgKvpGuXV2YjUtg/NKP2g+DUFSw41XI9LYwZODQW9KKu9R8cQgSUIu6UdGItUTXVM+vYaM60gDYVH9N94WXMILq5iWAryd9KrMYNgubOMscuaziA== 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: To simplify the code and to make the assumptions clearer, refactor user_mem_abort() by immediately setting force_pte to true if logging_active is true. Also, add a check to ensure that the assumption that logging_active is guaranteed to never be true for VM_PFNMAP memslot is true. Signed-off-by: Fuad Tabba --- arch/arm64/kvm/mmu.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index 342a9bd3848f..9b1921c1a1a0 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -1440,7 +1440,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, bool fault_is_perm) { int ret = 0; - bool write_fault, writable, force_pte = false; + bool write_fault, writable; bool exec_fault, mte_allowed; bool device = false, vfio_allow_any_uc = false; unsigned long mmu_seq; @@ -1452,6 +1452,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, gfn_t gfn; kvm_pfn_t pfn; bool logging_active = memslot_is_logging(memslot); + bool force_pte = logging_active; long vma_pagesize, fault_granule; enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_R; struct kvm_pgtable *pgt; @@ -1497,12 +1498,13 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, * logging_active is guaranteed to never be true for VM_PFNMAP * memslots. */ - if (logging_active) { - force_pte = true; + if (WARN_ON_ONCE(logging_active && (vma->vm_flags & VM_PFNMAP))) + return -EFAULT; + + if (force_pte) vma_shift = PAGE_SHIFT; - } else { + else vma_shift = get_vma_page_shift(vma, hva); - } switch (vma_shift) { #ifndef __PAGETABLE_PMD_FOLDED From patchwork Wed Jan 22 15:27:35 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13947434 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 B4C68C02182 for ; Wed, 22 Jan 2025 15:28:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B7ABB6B009A; Wed, 22 Jan 2025 10:27:57 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B50FA6B009B; Wed, 22 Jan 2025 10:27:57 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9CEC5280001; Wed, 22 Jan 2025 10:27:57 -0500 (EST) 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 77EDD6B009A for ; Wed, 22 Jan 2025 10:27:57 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 1C16E45445 for ; Wed, 22 Jan 2025 15:27:57 +0000 (UTC) X-FDA: 83035468194.09.F93B4AD Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf26.hostedemail.com (Postfix) with ESMTP id 38B2714000F for ; Wed, 22 Jan 2025 15:27:54 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=ySXEXIGa; spf=pass (imf26.hostedemail.com: domain of 3eQ6RZwUKCB4N4554AIIAF8.6IGFCHOR-GGEP46E.ILA@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3eQ6RZwUKCB4N4554AIIAF8.6IGFCHOR-GGEP46E.ILA@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=1737559675; 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=Hci5FfrptrEYAgO84bIQek1TVo92GCx/us5uG9wbkFo=; b=TFLajvGkN588hU0UWiUm/QEgTj/ZAMJezQLyud+xSi5qY/kydBYMv9J88ATzyptEPyGjFy hWsU4Ki2qU7xVb0Tg501ZhafN1Jy//beEnUtoCWgoZUvZJJRi25BaHcjTWT2ocRuA+Ux4l K8hliGtv0kr/25WVwgIFHp3Wm8MVRCc= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=ySXEXIGa; spf=pass (imf26.hostedemail.com: domain of 3eQ6RZwUKCB4N4554AIIAF8.6IGFCHOR-GGEP46E.ILA@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3eQ6RZwUKCB4N4554AIIAF8.6IGFCHOR-GGEP46E.ILA@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737559675; a=rsa-sha256; cv=none; b=bXLrzgU64hb8tN+JDW9wECnWW54GLdLvgo0uxPts/Rx8DCzTAqf9KKGVgWiLiZSnh31INk 4tJ+nBQgpbQrl1Gbgv34oXfauoKQuumU34REEHNStalboIpwJxY/ILW6D2q1btNkdH7J+c HG3bGI2hEIbPqgoHstMFk0GiSQC51j4= Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4361fc2b2d6so38934885e9.3 for ; Wed, 22 Jan 2025 07:27:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737559674; x=1738164474; 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=Hci5FfrptrEYAgO84bIQek1TVo92GCx/us5uG9wbkFo=; b=ySXEXIGalqcVm3ZfQiZKX0Z5TkquJ6Hza9tvfwbjkByAflg7yaAgGItvkoTegbb7WH 3rX6k3vkSWga+3+fZ4Pl+RxEdc5oclW+cT/LZHyWW1iJCDUmKP6qJbNBPOAsuUz5b7lL lo98uyGF+Gr/+Cg/f84rEtsNQrWgNTz3THVEIOSfGOvxQkeF0rwAjaJP222rgqf3P5rW QQS+kONQDsm77IhJshoCZ8kgJ+M/4JGI4fej1yTzcjtwMjlal4JTMuDl8HuIS0kypNRz 6d9MnV/ibhyZWchqlgXj5Ysa+8/NGXE8eMsio3IAD0QYTIoIE8N8jMed+HFRBmrwAEfa Ou7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737559674; x=1738164474; 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=Hci5FfrptrEYAgO84bIQek1TVo92GCx/us5uG9wbkFo=; b=rDKju3Vz1wi3fONbUpKeLgSy4DwIYq2BGVux7FdT6TAI1S6mRri0yhSUe2vYZvnd5u iG3Oot2jtHs0dBVEjjTlapuOIxR9J0anAby7ARwGY4L4C+2Sr/etHEOKQti9EKeHpCv8 caet5btrUT/SQRo92tidnAzeP9/bTD7fw5ZKQQRQD743LyrrK5ZoK9CgV7HquTvFypwG C1i/jDn9fAcpYF7grtYjpKbcouN63+ynnTqhuSKlcJXr+buA+SKoNPwLYu1N+PoFETuh 2+vIaHQ9jNojBkCB9M637ZSkbAwK8cq+ZdH2TSRQOgx28w4jaL/oodii/D8mPRr+EsKN 3Srg== X-Forwarded-Encrypted: i=1; AJvYcCVsw2Y3xsIkVvlFhcWE3Aez0z0EMMENhEI3IowVNgGtkUvRx0dECA8D56IhNISdQc05/VNMtISG7g==@kvack.org X-Gm-Message-State: AOJu0YyrV5tuXaHX/tIx5A0gYlhOX5jeAKOFoHtehRmLLWCE8ttVaeHy MimoO2uQ9cXb+V3djDPtTBx+7uSx3K6DLIteZHQfxUj/qigIxNP93feM6O+8cz1SNIWNMoAWLw= = X-Google-Smtp-Source: AGHT+IE2Fntu2qopdn+owQhaJ80RY4NIoyvMlJMQPvu8Ke49lFI8KlAtVqYK+DTJXCt4814HosjTb2ygng== X-Received: from wmrk9.prod.google.com ([2002:a05:600c:b49:b0:434:f0a3:7876]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:450d:b0:436:840b:261c with SMTP id 5b1f17b1804b1-438914340afmr185743115e9.19.1737559673695; Wed, 22 Jan 2025 07:27:53 -0800 (PST) Date: Wed, 22 Jan 2025 15:27:35 +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-7-tabba@google.com> Subject: [RFC PATCH v1 6/9] KVM: arm64: Handle guest_memfd()-backed guest page faults 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-Server: rspam02 X-Rspamd-Queue-Id: 38B2714000F X-Stat-Signature: 7971pnyz68izpcbw9aon31y45pahujsk X-Rspam-User: X-HE-Tag: 1737559674-959266 X-HE-Meta: U2FsdGVkX19IqUTxML4XPjcmpkp6wJa5voAqbyUeAn6DEKB99j398ItOTBivkUm+rUkFl0FJ+KOEOOawxE/XqHoUUfvz2yzipHf+4/4GYGOP1krmJhBQ+ZND3lw66bJa4apckrpj+OHKT5G79HM6S0XF559ho2Q50RD9wGYvo8ptK6jaT/SIQwSEU8E9F5jn+NnkgPaiLLmnJdNCbpqhE8CJhwF7iyVynEB31HScBzPDD2uxF8dXa+kCD2WKGWGroK8065PDJzJljaHtPu+qr0/H8r6tOgMePAYJGSn6592OFeq2tbQFudGgLD6Db+XGd48QpWpi36uiObAORuszth1CS/ntSVROOiu3qqzmOwI9+bDUnAZihapwy9sQQDiM0eKBLSt8ToTD6aTvHBvQUTYy5KuU47a0ZGvaSa2rTE3rpL4JfCPaBEbUXYgysKbM8JLt2smx5htcUcrf9cE343tgXUcmEHCr6feqy04inFOt7gD+TEzPOeT1BM/rA/J/I4BAsW4dtzZUMi5Sy3fydZXWLfGo6GRoLzS9Mh7k5ImA2aIT0EYpMU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== 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: Add arm64 support for resolving guest page faults on guest_memfd() backed memslots. This support is not contingent on pKVM, or other confidential computing support, and works in both VHE and nVHE modes. Without confidential computing, this support is useful for testing and debugging. In the future, it might also be useful should a user want to use guest_memfd() for all code, whether it's for a protected guest or not. For now, the fault granule is restricted to PAGE_SIZE. Signed-off-by: Fuad Tabba --- arch/arm64/kvm/mmu.c | 86 ++++++++++++++++++++++++++++------------ include/linux/kvm_host.h | 5 +++ virt/kvm/kvm_main.c | 5 --- 3 files changed, 66 insertions(+), 30 deletions(-) diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index 9b1921c1a1a0..adf23618e2a0 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -1434,6 +1434,39 @@ static bool kvm_vma_mte_allowed(struct vm_area_struct *vma) return vma->vm_flags & VM_MTE_ALLOWED; } +static kvm_pfn_t faultin_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, + gfn_t gfn, bool write_fault, bool *writable, + struct page **page, bool is_private) +{ + kvm_pfn_t pfn; + int ret; + + if (!is_private) + return __kvm_faultin_pfn(slot, gfn, write_fault ? FOLL_WRITE : 0, writable, page); + + *writable = false; + + if (WARN_ON_ONCE(write_fault && memslot_is_readonly(slot))) + return KVM_PFN_ERR_NOSLOT_MASK; + + ret = kvm_gmem_get_pfn(kvm, slot, gfn, &pfn, page, NULL); + if (!ret) { + *writable = write_fault; + return pfn; + } + + if (ret == -EHWPOISON) + return KVM_PFN_ERR_HWPOISON; + + return KVM_PFN_ERR_NOSLOT_MASK; +} + +static bool is_private_mem(struct kvm *kvm, struct kvm_memory_slot *memslot, phys_addr_t ipa) +{ + return kvm_arch_has_private_mem(kvm) && kvm_slot_can_be_private(memslot) && + (kvm_mem_is_private(kvm, ipa >> PAGE_SHIFT) || !memslot->userspace_addr); +} + static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, struct kvm_s2_trans *nested, struct kvm_memory_slot *memslot, unsigned long hva, @@ -1441,24 +1474,25 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, { int ret = 0; bool write_fault, writable; - bool exec_fault, mte_allowed; + bool exec_fault, mte_allowed = false; bool device = false, vfio_allow_any_uc = false; unsigned long mmu_seq; phys_addr_t ipa = fault_ipa; struct kvm *kvm = vcpu->kvm; struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache; - struct vm_area_struct *vma; + struct vm_area_struct *vma = NULL; short vma_shift; gfn_t gfn; kvm_pfn_t pfn; bool logging_active = memslot_is_logging(memslot); - bool force_pte = logging_active; - long vma_pagesize, fault_granule; + bool is_private = is_private_mem(kvm, memslot, fault_ipa); + bool force_pte = logging_active || is_private; + long vma_pagesize, fault_granule = PAGE_SIZE; enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_R; struct kvm_pgtable *pgt; struct page *page; - if (fault_is_perm) + if (fault_is_perm && !is_private) fault_granule = kvm_vcpu_trap_get_perm_fault_granule(vcpu); write_fault = kvm_is_write_fault(vcpu); exec_fault = kvm_vcpu_trap_is_exec_fault(vcpu); @@ -1482,24 +1516,30 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, return ret; } + mmap_read_lock(current->mm); + /* * Let's check if we will get back a huge page backed by hugetlbfs, or * get block mapping for device MMIO region. */ - mmap_read_lock(current->mm); - vma = vma_lookup(current->mm, hva); - if (unlikely(!vma)) { - kvm_err("Failed to find VMA for hva 0x%lx\n", hva); - mmap_read_unlock(current->mm); - return -EFAULT; - } + if (!is_private) { + vma = vma_lookup(current->mm, hva); + if (unlikely(!vma)) { + kvm_err("Failed to find VMA for hva 0x%lx\n", hva); + mmap_read_unlock(current->mm); + return -EFAULT; + } - /* - * logging_active is guaranteed to never be true for VM_PFNMAP - * memslots. - */ - if (WARN_ON_ONCE(logging_active && (vma->vm_flags & VM_PFNMAP))) - return -EFAULT; + /* + * logging_active is guaranteed to never be true for VM_PFNMAP + * memslots. + */ + if (WARN_ON_ONCE(logging_active && (vma->vm_flags & VM_PFNMAP))) + return -EFAULT; + + vfio_allow_any_uc = vma->vm_flags & VM_ALLOW_ANY_UNCACHED; + mte_allowed = kvm_vma_mte_allowed(vma); + } if (force_pte) vma_shift = PAGE_SHIFT; @@ -1570,17 +1610,14 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, } gfn = ipa >> PAGE_SHIFT; - mte_allowed = kvm_vma_mte_allowed(vma); - - vfio_allow_any_uc = vma->vm_flags & VM_ALLOW_ANY_UNCACHED; /* Don't use the VMA after the unlock -- it may have vanished */ vma = NULL; /* * Read mmu_invalidate_seq so that KVM can detect if the results of - * vma_lookup() or __kvm_faultin_pfn() become stale prior to - * acquiring kvm->mmu_lock. + * vma_lookup() or faultin_pfn() become stale prior to acquiring + * kvm->mmu_lock. * * Rely on mmap_read_unlock() for an implicit smp_rmb(), which pairs * with the smp_wmb() in kvm_mmu_invalidate_end(). @@ -1588,8 +1625,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, mmu_seq = vcpu->kvm->mmu_invalidate_seq; mmap_read_unlock(current->mm); - pfn = __kvm_faultin_pfn(memslot, gfn, write_fault ? FOLL_WRITE : 0, - &writable, &page); + pfn = faultin_pfn(kvm, memslot, gfn, write_fault, &writable, &page, is_private); if (pfn == KVM_PFN_ERR_HWPOISON) { kvm_send_hwpoison_signal(hva, vma_shift); return 0; diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index ebca0ab4c5e2..f059958b98fd 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1864,6 +1864,11 @@ static inline int memslot_id(struct kvm *kvm, gfn_t gfn) return gfn_to_memslot(kvm, gfn)->id; } +static inline bool memslot_is_readonly(const struct kvm_memory_slot *slot) +{ + return slot->flags & KVM_MEM_READONLY; +} + static inline gfn_t hva_to_gfn_memslot(unsigned long hva, struct kvm_memory_slot *slot) { diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 9cd6690b7955..10c3168db473 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2622,11 +2622,6 @@ unsigned long kvm_host_page_size(struct kvm_vcpu *vcpu, gfn_t gfn) return size; } -static bool memslot_is_readonly(const struct kvm_memory_slot *slot) -{ - return slot->flags & KVM_MEM_READONLY; -} - static unsigned long __gfn_to_hva_many(const struct kvm_memory_slot *slot, gfn_t gfn, gfn_t *nr_pages, bool write) { From patchwork Wed Jan 22 15:27:36 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13947435 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 7F23AC02182 for ; Wed, 22 Jan 2025 15:28:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 958646B009C; Wed, 22 Jan 2025 10:27:59 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8E21B280002; Wed, 22 Jan 2025 10:27:59 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 75E9F280001; Wed, 22 Jan 2025 10:27:59 -0500 (EST) 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 561626B009C for ; Wed, 22 Jan 2025 10:27:59 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 1E7DCA0342 for ; Wed, 22 Jan 2025 15:27:59 +0000 (UTC) X-FDA: 83035468278.23.E636E85 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf20.hostedemail.com (Postfix) with ESMTP id 459561C0018 for ; Wed, 22 Jan 2025 15:27:57 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=2ge24xzW; spf=pass (imf20.hostedemail.com: domain of 3ew6RZwUKCCAP6776CKKCHA.8KIHEJQT-IIGR68G.KNC@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3ew6RZwUKCCAP6776CKKCHA.8KIHEJQT-IIGR68G.KNC@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=1737559677; 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=5uY+HDVZ7Dp1D+/uAf3D/YK52BSVzxXpdvAU1b299wE=; b=FLSDIcRiSLJx0NjJ5pdwzXSuKVugmiCrVuBSZ81UtYl8Wl+vuKazXWKK7LFr94KTT8N0sx SB8Jnk92oJFjo3+oJn8hPQbgNyfypJFQfJtkKLS2WGL0IjM4y6enf6Enfbzbo+k0j+5ezH KiBDM4Mo+0sa9n+BCI1dbtx65na28Rg= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=2ge24xzW; spf=pass (imf20.hostedemail.com: domain of 3ew6RZwUKCCAP6776CKKCHA.8KIHEJQT-IIGR68G.KNC@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3ew6RZwUKCCAP6776CKKCHA.8KIHEJQT-IIGR68G.KNC@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737559677; a=rsa-sha256; cv=none; b=wDer446tgwZGz2SYGb+JqOKOsMN2A0q0r27aP6L+e3WtnB8cRDwl2duP4qq60Tnp5O+yfp +rIauO4q1h7jFbedX1Tb8818mWx8yvp+5BbuPIGMXsWYwjmjlkSvSz1ZYMFb3YmrTxHuP5 P1C+ZIHqM7MBWZ3UOqRT6/w1NLWsOy4= Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4362153dcd6so36915475e9.2 for ; Wed, 22 Jan 2025 07:27:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737559676; x=1738164476; 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=5uY+HDVZ7Dp1D+/uAf3D/YK52BSVzxXpdvAU1b299wE=; b=2ge24xzW+ityfS9uZ+cNTeaDyZVRrdYdMfDuPWEeUNVACbrCajpO3FnyTQVbENfp5Q 9g5N1NHeairXZc9vkmUSEJv2vkvi4XZQwIyWPLG2HXCpgPNgTDUbO6EcH21NxSL/xosy TWu4/y45D3HRGzB5nSgYRmGPPeEEQZYNp8o8YyusqBh73DseaeSmuJzIEXn31fJ63rNC pM81bETmiwbpgoE7xEIF1WQL5NCKdZXgqwBw99HgSaLyLvxbwjCpWMzX5bBxsT+zez/W ql284hiGJsWX5vU5lUUNsTtYrwVRs3Yltlw+mkhvz6oCCJyQttrm2eSjlbazaS/8ZLnO w9Xg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737559676; x=1738164476; 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=5uY+HDVZ7Dp1D+/uAf3D/YK52BSVzxXpdvAU1b299wE=; b=ZJbT1IxJ1nXhBmaQceOVR7+LHkV2bZgzleL2qdY3nlWZzAYC54m3aDwjw449gJwaEC z+x7Z23ewGm2Lv/AA212CzAILZIO/NyNDV22KvD1CmFz6/4cuA1hBP8Q0ick5Q2legSx 37FvGRmmTTFOgbyEtA58WyOhEInBItsQgjPjLnlLLDttRUrsSR8KylsiUtgPer8ytnnY W/yueuSIzGMnrNPfAI8Ol6xxi4Lh4iUKDdiPN1cQ5MdFQMYQ+moPlqHQVs/JwnW1gQf3 VU3C/vXUytiBJbPn477H/DnnBiOrfuf0aEpLHvPl1yA6sBIzkCsc0ISJ1cS2QI/zVoFs o3XQ== X-Forwarded-Encrypted: i=1; AJvYcCWNnCLM5K/vflVc3I/Ty2OJwwytKxxCG0vEnXFp08/aJZCvdYBMHTyE/M4SK33wzd8LKuj+y1h8rg==@kvack.org X-Gm-Message-State: AOJu0YwnqIY1OuIGLVtvdEKgSP6G712cBGfrXZJE8SkUY4eyMpJvV4Wm hQZogqLjZngjmpt6UiWm6B09XQAHZEmA2dDqjJHo+ENeQxVz9Cuk3RX/9whIqBsUVTpMnAWjvw= = X-Google-Smtp-Source: AGHT+IF2hKFLXA7PBCoJ+LiMdi1Un48iNomJH581YLTvT7wM4Qq5KK+uhWlMUrwC1rIHE2BbM0N2m173nA== X-Received: from wmom15.prod.google.com ([2002:a05:600c:460f:b0:436:1a60:654e]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4894:b0:434:a7e7:a1ca with SMTP id 5b1f17b1804b1-43891427762mr182016925e9.20.1737559675909; Wed, 22 Jan 2025 07:27:55 -0800 (PST) Date: Wed, 22 Jan 2025 15:27:36 +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-8-tabba@google.com> Subject: [RFC PATCH v1 7/9] KVM: arm64: Introduce KVM_VM_TYPE_ARM_SW_PROTECTED machine type 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-Server: rspam02 X-Rspamd-Queue-Id: 459561C0018 X-Stat-Signature: hrjzqjxpkj6eohhb79nna1i3b6mefxr1 X-Rspam-User: X-HE-Tag: 1737559677-888155 X-HE-Meta: U2FsdGVkX18UIdi09RgG6VOnXn4JjHsRUzSWiJtQMXWeUxZeQ2B+dP271hv2vIQZr5aUsa6fMAYDQS3+XUHMEiE2NgGSx993wjPdj2QK6Wan2hoGzgQvM6+aOnC88AZdAP5x8jmpmllqF5PgoR5pDSfcQ3KPazsPMkGzdiHQYejBSyBaV9tqYtVdC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KtTYRyNhEMmQoWav2gcCJE7GxWLPeLHzRKw8CG/9+3Tv8a3hXlyEA5wE9Q5sqIFThP4Wy9OLT2ubCMRCiHuZFqu7/MzX4fBqxSt0OMTnOcK5CLuQP+htmjLQ3yn5UBg29h3cgf/O7pS2YJ/G8BGyq9swpu1DPJdbTBMbA6yux7MspYbHQSNNrs1kkncapSQNtlQjIQjRYJf0hnqUGJL5AgNdC7fS81/+ALytFmswJEa4i62gEr4dJS8x5y8PFgVa6YIfWOyTe1pZlw52zH5W9gv4NtSr5FwDj7Uo7eN3+bDB62Bjz/5cAorHpxVo+kMo1y8tQxGh3OTwtR0JPB3VmnAvhDgF5dgFg3sdcFbqIMOrowhsn4UMaoiXuRihBkPYWg== 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: Introduce a new virtual machine type, KVM_VM_TYPE_ARM_SW_PROTECTED, to serve as a development and testing vehicle for Confidential (CoCo) VMs, similar to the x86 KVM_X86_SW_PROTECTED_VM type. Initially, this is used to test guest_memfd without needing any underlying protection. Similar to the x86 type, this is currently only for development and testing. Do not use KVM_VM_TYPE_ARM_SW_PROTECTED for "real" VMs, and especially not in production. The behavior and effective ABI for software-protected VMs is unstable. Signed-off-by: Fuad Tabba --- Documentation/virt/kvm/api.rst | 5 +++++ arch/arm64/include/asm/kvm_host.h | 10 ++++++++++ arch/arm64/kvm/arm.c | 5 +++++ arch/arm64/kvm/mmu.c | 3 --- include/uapi/linux/kvm.h | 6 ++++++ 5 files changed, 26 insertions(+), 3 deletions(-) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index f15b61317aad..7953b07c8c2b 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -214,6 +214,11 @@ exposed by the guest CPUs in ID_AA64MMFR0_EL1[PARange]. It only affects size of the address translated by the stage2 level (guest physical to host physical address translations). +KVM_VM_TYPE_ARM_SW_PROTECTED is currently only for development and testing of +confidential VMs without having underlying support. Do not use +KVM_VM_TYPE_ARM_SW_PROTECTED for "real" VMs, and especially not in production. +The behavior and effective ABI for software-protected VMs is unstable. + 4.3 KVM_GET_MSR_INDEX_LIST, KVM_GET_MSR_FEATURE_INDEX_LIST ---------------------------------------------------------- diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index e18e9244d17a..2fdc7e24ae8e 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -380,6 +380,8 @@ struct kvm_arch { * the associated pKVM instance in the hypervisor. */ struct kvm_protected_vm pkvm; + + unsigned long vm_type; }; struct kvm_vcpu_fault_info { @@ -1529,4 +1531,12 @@ void kvm_set_vm_id_reg(struct kvm *kvm, u32 reg, u64 val); #define kvm_has_s1poe(k) \ (kvm_has_feat((k), ID_AA64MMFR3_EL1, S1POE, IMP)) +#define kvm_arch_has_private_mem(kvm) \ + (IS_ENABLED(CONFIG_KVM_PRIVATE_MEM) && \ + ((kvm)->arch.vm_type & KVM_VM_TYPE_ARM_SW_PROTECTED)) + +#define kvm_arch_private_mem_inplace(kvm) \ + (IS_ENABLED(CONFIG_KVM_GMEM_MAPPABLE) && \ + ((kvm)->arch.vm_type & KVM_VM_TYPE_ARM_SW_PROTECTED)) + #endif /* __ARM64_KVM_HOST_H__ */ diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index a102c3aebdbc..ecdb8db619d8 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -171,6 +171,9 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) { int ret; + if (type & ~KVM_VM_TYPE_MASK) + return -EINVAL; + mutex_init(&kvm->arch.config_lock); #ifdef CONFIG_LOCKDEP @@ -212,6 +215,8 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) bitmap_zero(kvm->arch.vcpu_features, KVM_VCPU_MAX_FEATURES); + kvm->arch.vm_type = type; + return 0; err_free_cpumask: diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index adf23618e2a0..b6cbe11dea48 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -869,9 +869,6 @@ static int kvm_init_ipa_range(struct kvm_s2_mmu *mmu, unsigned long type) u64 mmfr0, mmfr1; u32 phys_shift; - if (type & ~KVM_VM_TYPE_ARM_IPA_SIZE_MASK) - return -EINVAL; - phys_shift = KVM_VM_TYPE_ARM_IPA_SIZE(type); if (is_protected_kvm_enabled()) { phys_shift = kvm_ipa_limit; diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 021f8ef9979b..5e10a5903a58 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -656,6 +656,12 @@ struct kvm_enable_cap { #define KVM_VM_TYPE_ARM_IPA_SIZE_MASK 0xffULL #define KVM_VM_TYPE_ARM_IPA_SIZE(x) \ ((x) & KVM_VM_TYPE_ARM_IPA_SIZE_MASK) + +#define KVM_VM_TYPE_ARM_SW_PROTECTED (1UL << 9) + +#define KVM_VM_TYPE_MASK (KVM_VM_TYPE_ARM_IPA_SIZE_MASK | \ + KVM_VM_TYPE_ARM_SW_PROTECTED) + /* * ioctls for /dev/kvm fds: */ From patchwork Wed Jan 22 15:27:37 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13947436 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 68A21C02181 for ; Wed, 22 Jan 2025 15:28:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1B8186B009E; Wed, 22 Jan 2025 10:28:02 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 11AE3280001; Wed, 22 Jan 2025 10:28:02 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EAEA66B00A0; Wed, 22 Jan 2025 10:28:01 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id BDE4B6B009E for ; Wed, 22 Jan 2025 10:28:01 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 44E9BACCE5 for ; Wed, 22 Jan 2025 15:28:01 +0000 (UTC) X-FDA: 83035468362.18.6FAB217 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf27.hostedemail.com (Postfix) with ESMTP id 6C2C440008 for ; Wed, 22 Jan 2025 15:27:59 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=D7DaHWB0; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf27.hostedemail.com: domain of 3fg6RZwUKCCMS9AA9FNNFKD.BNLKHMTW-LLJU9BJ.NQF@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3fg6RZwUKCCMS9AA9FNNFKD.BNLKHMTW-LLJU9BJ.NQF@flex--tabba.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737559679; a=rsa-sha256; cv=none; b=xqawdF7lYb7tBSng2DEGR1JCiHM3hcJO5D0ZYXe47z3egXXUCFIncT7bjZWVvhEhmcnPB0 Td27dZlz4mXd3nidzxFHkLNguIFBftkrCpi08uAtSaNOAmQPsllpuhrQ6msHLgWkCM5VVA /q9J7+0JcFHYvn925KfvSbaMecYzPFU= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=D7DaHWB0; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf27.hostedemail.com: domain of 3fg6RZwUKCCMS9AA9FNNFKD.BNLKHMTW-LLJU9BJ.NQF@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3fg6RZwUKCCMS9AA9FNNFKD.BNLKHMTW-LLJU9BJ.NQF@flex--tabba.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1737559679; 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=npQ4G6IRMYUbsOMeJWf56VvRGJDHzOqgY/NEKvIEjT0=; b=c8x1fyrWX6jb2toZxF9wxqgyEdeEpr92ZmTk3oSIQMnEQ9ppIUvjmaQ5LigIMkoRDG6QFz dc0BBJU5FCLCI9wM386RZBFXsjCKP0DTCN3VwAveJM8MfWxmlwxFtUiP99q417PFPfflzt Soi5PHMiH0nOUPfEpBYfarKvAHn5LJo= Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-43619b135bcso36416925e9.1 for ; Wed, 22 Jan 2025 07:27:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737559678; x=1738164478; 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=npQ4G6IRMYUbsOMeJWf56VvRGJDHzOqgY/NEKvIEjT0=; b=D7DaHWB04UuPaMoysk+224qznzWctNmVKzEbzrPwRi16wII/MAukQzhfeSRpJWkpPm 3QWpOnTv15y1WnzXiyPejkk56GS4uPUGCAdexdW33ik7QuCY9PrUNbP4sMDzLogeJJAJ HtywQ945Tb3XYsbdGqkTzRKMlxww4xlJTd9Ls0HJhKjRWxmH8NPn/vWskWAULJR/nVT0 IDz/TjtCwbeNAyXO3sf88soB+XN4UKr4jWW6eICBTm7KMOmjoLCB6eZ2BNNH1GZPcvn7 +8dCD0gd7qM9y0fiCOYe8M+3XDi1HCNFPKTgkKoyhY4qMNirZrPoRummW5aO79cI/JTG efjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737559678; x=1738164478; 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=npQ4G6IRMYUbsOMeJWf56VvRGJDHzOqgY/NEKvIEjT0=; b=Riv0i78RgGzgF3w0goI2ZNpkcuX9YGU/n7Qwv7X4RSJD5aTIIAEpP5os9OeWR5yOOB 52ra8CgQnDLZn62VRQiIAoaluwkdrK/GWfDg8Nq1RC32UK2bDNKqU8FBOU4PLnHrBHO0 PupPShLRvLid00+HTOXcH1tqShWYqX8uaR81/x5q0nBJABwau2feQP2V2Wfz6t4Zb0FE Vq3ONPIgWszsou9qFQym2MSpG2R+pmhgT3OlLcXgsEVUKfUq3Z9hdMhHEMYY6p4/ijXl F81n0gPJC8DElRklVxPSCvBs2LOrrv0A1xlWPDwFAhimNwY9GBMOhUtQ6XhxUaPtKflS yO2A== X-Forwarded-Encrypted: i=1; AJvYcCWJ5Ht4JAyC1bEjw0DuHj+zgT/XebFWBZ+unZdlzAlOUVnR1777HdlRgFpwMvj37+fvrJPBVtLZcg==@kvack.org X-Gm-Message-State: AOJu0Yw2vewFph3q9mSijbMmhK32vbHy8MDjxavy6S3a5UPdmVB24GTN wPUsqpJeE+YkLObZbOoCPxK1bH/jxa+CpI0Tl9khMSk7Hy3SpqRfmnOBp4ouVM+tnmhj1mQFpA= = X-Google-Smtp-Source: AGHT+IFMt51o/dm1bu2dWWRmK/754r3bTaAX9g4hoTpYJtAfuY1vj2C8A/Y3n5D7Vdmv0hIc0uGo5eq33Q== X-Received: from wmbjn3.prod.google.com ([2002:a05:600c:6b03:b0:434:a7ee:3c40]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4c24:b0:434:f3a1:b214 with SMTP id 5b1f17b1804b1-4389da1e6c7mr184498125e9.28.1737559678054; Wed, 22 Jan 2025 07:27:58 -0800 (PST) Date: Wed, 22 Jan 2025 15:27:37 +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-9-tabba@google.com> Subject: [RFC PATCH v1 8/9] KVM: guest_memfd: selftests: guest_memfd mmap() test when mapping is allowed 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-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 6C2C440008 X-Stat-Signature: qax9855tjz3yiyuwy5i5zy5gc7yb4nba X-HE-Tag: 1737559679-652041 X-HE-Meta: U2FsdGVkX18MjsZU8aWKSdFPUS3l4jyXJ2Nge8YtzURqs1AshM2H2bamBTpb5wFTRpou+YgVquJrPzbD0k6kj7t/mxNGIUJdr7hIQdDqA+E/iuzZWZb5RM6fsoLV4RTnXgAUjCxK5CD4XZMGNmgmnlz66Ms6eJXs2VXpb+Yf1xRbfly7DUDcgjsMzgH7kasZrOXdYKpgKtafEld4wyvRIdccB3ADsasuWdcj8Bxl8cuQvOkzEhEbwALK9ykmn7q0gELZRspk0pQaOyJVC/FQUVP6lfIffnzoTlBGyEWFuPxqCqr/TlwFtDCHWWFF3eGYgvcnwAciHk7aKtHemK2c774dKMnMt1RJ9Wp/FFbd4kOhny7Y4P3UbzvEwCFWg/fkb0hqRHEkofZ1/QakOAP4UmBJZ0Ju8KJLhuUxB8mjjJa9q3nCzrP7XIO872tec1uQfmmr8eRIfUk92xdB+PcE+TvHciyCOZ7U4BIfSuhKQOStF6j/ctHdqmY17uqf42B6l+JB624w0q6c4CL0XdyKjtSKblZW9rwvanXgYhKLbO01oFHgMyxAzrlZR4XNx+x4AkzPu8FQjFfHS97QSKHD6b60pnDO/KLidiQIbHQB44jAPo36WBeDX9BhEOrcE/h1fW0WGSEd9M7b9sqHrCKyxZba7sEJywyLdzjH3xnqd0ElAozRcHVKMIEewBksGyaGo+p3ie/prH15vxNY369tX94qpgBHlsdFMijxPyI+QVOENm8VJSZKNKspa5cMayINTaeThYv2tsLkcH9MGKLVgcclk4h+x09YKkhrAn4XmMm8VrJj6BDxhr/sO5HY/jg5MbshJjX7hqACFYipDUd9xDoXrwFCEHxiDbAqUzKtwFxkBMV3wdR7O7vJugIJ+IeDwi3kXqAe6aYYH0/NaHtAULRubgMQKEwjG+PnVOFCsdfjhJod+8n3rYh2AmADOCqhaRs6VdM33Vwg2KJCN8w mZdQzPde U2x1NC18lDzeiu2WrhmXZw55kNK6UmdAy+Lqt3NO5gVW0eSkp4YWwEKm2Bnfw0MBuZBN9bzvNo6B0rq4lwUxLOg2MbRGBL5UCuD+EZNjxWihcPncAtcl+y6oYV678Ehui8Vfo9WTY9Akk/zhgGdbhfE3DsnHcO5OjBFDZ00PVXV5G2gR9u9iwPsyfNi4xOw2Wy17bOXrieBA3nc+bV58HdcJyJkm/dx/0HIRrp+oFCTgfXjt2ZRgN4rwBhpFokmf0T84jnq8CG8aJ72OpXqiQqN4ovu/3UqZAeiWSycLFmdcoGtipzlKiwe8guKGqOi6hdpmab2RC9mP3CgOkyNlvUkYf0W6849Et4cTyH6oexCyij2mj/wAkErHmd1Oiw12Av2fqGTBR2D5Wty0EQaTkeZwwimaYAhOb0+eK0Ech39T/DdqYbTp6J74NrQOdHXoRDJBRTNoflzZWcwSFtrV1ycRz1KOnv48tk8PzcJXfOkrqkIqBuGAumOXDVmjBq1OuvUm308Lr0bxqMFn7ZbM0NTSi0zXEeh3OzollsHyY0u5eanzU/EueWWBT4NqOPlkgYUd5VLriP5vLha3GnZ9zhgv7j8IF9hIvQA3QhVrBErJG7xiCl3VcWJeowlRvBXGuJor2cIjBtcsbxeMJqy+huJI7fw== 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: Expand the guest_memfd selftests to include testing mapping guest memory if the capability is supported, and that still checks that memory is not mappable if the capability isn't supported. Also, build the guest_memfd selftest for aarch64. Signed-off-by: Fuad Tabba --- tools/testing/selftests/kvm/Makefile | 1 + .../testing/selftests/kvm/guest_memfd_test.c | 60 +++++++++++++++++-- tools/testing/selftests/kvm/lib/kvm_util.c | 3 +- 3 files changed, 57 insertions(+), 7 deletions(-) diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index 41593d2e7de9..c998eb3c3b77 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -174,6 +174,7 @@ TEST_GEN_PROGS_aarch64 += coalesced_io_test TEST_GEN_PROGS_aarch64 += demand_paging_test TEST_GEN_PROGS_aarch64 += dirty_log_test TEST_GEN_PROGS_aarch64 += dirty_log_perf_test +TEST_GEN_PROGS_aarch64 += guest_memfd_test TEST_GEN_PROGS_aarch64 += guest_print_test TEST_GEN_PROGS_aarch64 += get-reg-list TEST_GEN_PROGS_aarch64 += kvm_create_max_vcpus diff --git a/tools/testing/selftests/kvm/guest_memfd_test.c b/tools/testing/selftests/kvm/guest_memfd_test.c index ce687f8d248f..6c501cb865f3 100644 --- a/tools/testing/selftests/kvm/guest_memfd_test.c +++ b/tools/testing/selftests/kvm/guest_memfd_test.c @@ -34,12 +34,55 @@ static void test_file_read_write(int fd) "pwrite on a guest_mem fd should fail"); } -static void test_mmap(int fd, size_t page_size) +static void test_mmap_allowed(int fd, size_t total_size) { + size_t page_size = getpagesize(); + char *mem; + int ret; + int i; + + mem = mmap(NULL, total_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + TEST_ASSERT(mem != MAP_FAILED, "mmaping() guest memory should pass."); + + memset(mem, 0xaa, total_size); + for (i = 0; i < total_size; i++) + TEST_ASSERT_EQ(mem[i], 0xaa); + + ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, 0, + page_size); + TEST_ASSERT(!ret, "fallocate the first page should succeed"); + + for (i = 0; i < page_size; i++) + TEST_ASSERT_EQ(mem[i], 0x00); + for (; i < total_size; i++) + TEST_ASSERT_EQ(mem[i], 0xaa); + + memset(mem, 0xaa, total_size); + for (i = 0; i < total_size; i++) + TEST_ASSERT_EQ(mem[i], 0xaa); + + ret = munmap(mem, total_size); + TEST_ASSERT(!ret, "munmap should succeed"); +} + +static void test_mmap_denied(int fd, size_t total_size) +{ + size_t page_size = getpagesize(); char *mem; mem = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); TEST_ASSERT_EQ(mem, MAP_FAILED); + + mem = mmap(NULL, total_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + TEST_ASSERT_EQ(mem, MAP_FAILED); +} + +static void test_mmap(int fd, size_t total_size) +{ + if (kvm_has_cap(KVM_CAP_GUEST_MEMFD_MAPPABLE)) + test_mmap_allowed(fd, total_size); + else + test_mmap_denied(fd, total_size); } static void test_file_size(int fd, size_t page_size, size_t total_size) @@ -172,17 +215,24 @@ static void test_create_guest_memfd_multiple(struct kvm_vm *vm) int main(int argc, char *argv[]) { - size_t page_size; + unsigned long type = VM_TYPE_DEFAULT; + struct kvm_vm *vm; size_t total_size; + size_t page_size; int fd; - struct kvm_vm *vm; TEST_REQUIRE(kvm_has_cap(KVM_CAP_GUEST_MEMFD)); + if (kvm_has_cap(KVM_CAP_GUEST_MEMFD_MAPPABLE)) { +#ifdef __aarch64__ + type = KVM_VM_TYPE_ARM_SW_PROTECTED; +#endif + } + page_size = getpagesize(); total_size = page_size * 4; - vm = vm_create_barebones(); + vm = vm_create_barebones_type(type); test_create_guest_memfd_invalid(vm); test_create_guest_memfd_multiple(vm); @@ -190,7 +240,7 @@ int main(int argc, char *argv[]) fd = vm_create_guest_memfd(vm, total_size, 0); test_file_read_write(fd); - test_mmap(fd, page_size); + test_mmap(fd, total_size); test_file_size(fd, page_size, total_size); test_fallocate(fd, page_size, total_size); test_invalid_punch_hole(fd, page_size, total_size); diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index 480e3a40d197..098ea04ec099 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -347,9 +347,8 @@ struct kvm_vm *____vm_create(struct vm_shape shape) } #ifdef __aarch64__ - TEST_ASSERT(!vm->type, "ARM doesn't support test-provided types"); if (vm->pa_bits != 40) - vm->type = KVM_VM_TYPE_ARM_IPA_SIZE(vm->pa_bits); + vm->type |= KVM_VM_TYPE_ARM_IPA_SIZE(vm->pa_bits); #endif vm_open(vm); From patchwork Wed Jan 22 15:27:38 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13947437 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 B4D6FC02181 for ; Wed, 22 Jan 2025 15:28:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4C380280002; Wed, 22 Jan 2025 10:28:16 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 47293280001; Wed, 22 Jan 2025 10:28:16 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 33AD5280002; Wed, 22 Jan 2025 10:28:16 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 192BF280001 for ; Wed, 22 Jan 2025 10:28:16 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id B6472140344 for ; Wed, 22 Jan 2025 15:28:15 +0000 (UTC) X-FDA: 83035468950.23.CD2886A Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) by imf03.hostedemail.com (Postfix) with ESMTP id 942D420024 for ; Wed, 22 Jan 2025 15:28:01 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=bYH6LOWV; spf=pass (imf03.hostedemail.com: domain of 3gA6RZwUKCCUUBCCBHPPHMF.DPNMJOVY-NNLWBDL.PSH@flex--tabba.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3gA6RZwUKCCUUBCCBHPPHMF.DPNMJOVY-NNLWBDL.PSH@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=1737559681; 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=6IPKS54xTLh0G0JL9zAQyWtImcHIleOjiond1cpHBfM=; b=CwAokrF3qdV+hlwzbvTKRj/E1vMIKEmiw1rE3R1gcH55eOHTOHnz4oyvwa7iAxJdfhfwBW Tp+u/GrSKl0zsbuLVM0Xz50UBDa8H77iNFCiCgqoHB+Y0S1zcAn18OBnqnFdbArLpbtJfD R535vgLaycwg9F08oZD4ikG1FVVsUsQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737559681; a=rsa-sha256; cv=none; b=kWn7sOZkZp9W31Q+7GAqpAUTnSs7IXeBfnsW/Ysl9iJCgfV5CDKB/CjIZTC2bXsM0tWn3D xjgqvZCVzwGi8jFGF7UFxq5jVjxhrC0pGsmFgwIK6LqKc+0NdCH4cE5yEdrcRV8uRbJfqT B7VcjOwoL9nfteBLph5bRmSJk/WxP3s= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=bYH6LOWV; spf=pass (imf03.hostedemail.com: domain of 3gA6RZwUKCCUUBCCBHPPHMF.DPNMJOVY-NNLWBDL.PSH@flex--tabba.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3gA6RZwUKCCUUBCCBHPPHMF.DPNMJOVY-NNLWBDL.PSH@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-4362f893bfaso38288525e9.1 for ; Wed, 22 Jan 2025 07:28:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737559680; x=1738164480; 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=6IPKS54xTLh0G0JL9zAQyWtImcHIleOjiond1cpHBfM=; b=bYH6LOWVGlSS0UktV9p0Rl1zahWrP0INwKYdmA8EZYX10/iWB0XCvA9nSFaGwGIAAF FtxICPFMAc+PvGuu0wdWMn1lidw+ADFj8tGR8xn30LfDI4EjUDSVsCxiaETZarEpT5Xs I+XA+E2WafOlgrxSyZmsBmcHZLCch7TQdAgRJFq3f+Jk4Kb6jTGNqFlX//qSgUOWqoxR wOfPONJQQ0lUI+IOCPAaL2FFDF75JnoxuMLs+VABYc1MIbK1d7sqUAonPyWEu7E3uxTP P8nzgLg7apAS1WfARl+QN/Q+Ac8vSnSZbqSeFdmso91PAmJrc3bEZicG1f/zOJVCc/k8 devA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737559680; x=1738164480; 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=6IPKS54xTLh0G0JL9zAQyWtImcHIleOjiond1cpHBfM=; b=JCsVEkCxhnt1KPelDBgqr1nmMWRtvQY9I2v7op8ErHkjuR4mFbETu2gtfjfUtrJQ4o VdaYTkReJYDQFMj4J9/CLIEJC1HH7/Oh92T94lHAAi4iHMcOZCB1xkXnurZcIDW/K2Tq iAhQZhM0WsLzwZh7oD4Boj/siPxF3yx8HB9CTNLRixiVINmjUns+6ZfGulOm7CERrgJD zCa5ILM2fGwLfljEfCddCZSoGqMMxMsTD6jbL3o8LjbU2K/oADIHJagBcoa1BYuLjlxv lWQ+absGFGBDeSOm7zKD3Z9aXRdPTbsfwxlvOriX5KTWt9/eoA03libA3dkrCwiSRUJI DHIQ== X-Forwarded-Encrypted: i=1; AJvYcCXWzWUWeEho9LgBzdPjKEWEKZWPvtSMq4lZaJtR75BXSGV4I0TbL4DTNimxUYhbg2LexeC2hILXeg==@kvack.org X-Gm-Message-State: AOJu0Yzh7XcgzvnL2r5zCgYQVRHzf9qNuJd7+kiq1Yam2qSsbne6cnMZ UFzQlMEkSBjh+Wfvwf+DyORrL4dlHcrmlLBylBEDS4Lopy+ZrGIw+93QzJ3exf4zwUBCm9tYMQ= = X-Google-Smtp-Source: AGHT+IHKqC3XO4cCp4hQMJG/KxA/l5U2Z4NRyzkp8RVnNUMgP+pHap4S48R+U41XmL0XQuq/sUwK5vqtFQ== X-Received: from wmjt19.prod.google.com ([2002:a7b:c3d3:0:b0:42c:bfc2:aa72]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4508:b0:431:547e:81d0 with SMTP id 5b1f17b1804b1-438913e1c00mr237078585e9.11.1737559680156; Wed, 22 Jan 2025 07:28:00 -0800 (PST) Date: Wed, 22 Jan 2025 15:27:38 +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-10-tabba@google.com> Subject: [RFC PATCH v1 9/9] KVM: arm64: Enable mapping guest_memfd in arm64 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-Stat-Signature: cuzm9zf98h4zacckgx96673pixj6mbo1 X-Rspam-User: X-Rspamd-Queue-Id: 942D420024 X-Rspamd-Server: rspam03 X-HE-Tag: 1737559681-101206 X-HE-Meta: U2FsdGVkX19CY92vLLRsnOhUPG62XSRgNBuzAll7HKipnRLZ6UdfEoOr8qrst1r0zeyGVOhDzSxNUhw5GVIjmsBJpMo87gTGMgf8xAfM4M1cRbcrXYST5VB/VZDezSOAxQ4n8EaxKdoGmWCndStHGMltm3u3LLGVY2HSu8VW8qZDYSozKIC11qPixqGA0+eup1a/iady9F97BEtfdch404BLTgePuVkcFbrqOqjNtMjTWhrTi9wHIcR5T3xqljGIa8LOxsE9GXMudaXIsukdnA5+WMTs3/YiENtXQ7yF+ovGM4uzefKM1XEDXAW/6oAWiKTKAHvdiPwNvBQoQz5d3HS/WaRLTrZIvmHgiku3vEkgb9ge86LeUvgMt/5/jqFCF7FaOGJ6FfknaHoVPC1cCNmdz6qeR/3fnDU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++CvP7Gd2rsTdEZj1hdk0Qh5X7WocRHcVplKZrFeBWJB55Zigo7c6xVgZ1N1TM6oDptNVK8Yuaz6XGUdgTOsREYzd2UMod01td+Zxb4gqWqhqposta7qeblt0LgWv0WI0JuVYYwH96ZuyKND5icawb95tQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000029, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Enabled mapping guest_memfd in arm64, which would only apply to VMs with the type KVM_VM_TYPE_ARM_SW_PROTECTED. Signed-off-by: Fuad Tabba --- arch/arm64/kvm/Kconfig | 1 + 1 file changed, 1 insertion(+) diff --git a/arch/arm64/kvm/Kconfig b/arch/arm64/kvm/Kconfig index ead632ad01b4..fe3451f244b5 100644 --- a/arch/arm64/kvm/Kconfig +++ b/arch/arm64/kvm/Kconfig @@ -38,6 +38,7 @@ menuconfig KVM select HAVE_KVM_VCPU_RUN_PID_CHANGE select SCHED_INFO select GUEST_PERF_EVENTS if PERF_EVENTS + select KVM_GMEM_MAPPABLE help Support hosting virtualized guest machines.