From patchwork Wed Jan 29 17:23:13 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13953968 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 06768C02193 for ; Wed, 29 Jan 2025 17:23:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6C7C22801D5; Wed, 29 Jan 2025 12:23:35 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6501B280067; Wed, 29 Jan 2025 12:23:35 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3B7AE2801D5; Wed, 29 Jan 2025 12:23:35 -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 13D51280067 for ; Wed, 29 Jan 2025 12:23:35 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 37E66160F6C for ; Wed, 29 Jan 2025 17:23:34 +0000 (UTC) X-FDA: 83061161148.19.F594BE6 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf01.hostedemail.com (Postfix) with ESMTP id 47C0F4000B for ; Wed, 29 Jan 2025 17:23:32 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=vNWDnH92; spf=pass (imf01.hostedemail.com: domain of 3EmSaZwUKCIc4lmmlrzzrwp.nzxwty58-xxv6lnv.z2r@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3EmSaZwUKCIc4lmmlrzzrwp.nzxwty58-xxv6lnv.z2r@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=1738171412; 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=1nv4yVODl7ioMW6jJkbW1mbC87Wz7QQ6PPy9XEa8BJQ=; b=tTE2cmQTrXHaObhgsbv6hlUA9vrM5tCOSHpb2UQNz1qlzUVU5JCupMLDDir/ukBTeKekkZ bD75B7MZyxc48XP10kT03WX9TbmpPfBe0Aeg1KVNaH/31NqCaNTXKv9cslSeLIlHi4iwSn dULN8UrQGD+UGlPqGm9OBBGWgEps0so= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=vNWDnH92; spf=pass (imf01.hostedemail.com: domain of 3EmSaZwUKCIc4lmmlrzzrwp.nzxwty58-xxv6lnv.z2r@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3EmSaZwUKCIc4lmmlrzzrwp.nzxwty58-xxv6lnv.z2r@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738171412; a=rsa-sha256; cv=none; b=iGy2YS0Vqc9kpyNZr+Dlt9YQ2kQl0b4gd++7NAX7+zmr42hY7fN9bZ4lk1wV1D1+1zEs3Y Sr517BgtpL9FCj/m5x7Asaygx7ZYlVA77Zl6sZD8t3vyNF/ez68uc9YZzUNb3ChdjKCWrg iLGy4JFVj4NyiOMGWojwM1L//fyVm+c= Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-438d9f1faa0so10642395e9.1 for ; Wed, 29 Jan 2025 09:23:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738171411; x=1738776211; 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=1nv4yVODl7ioMW6jJkbW1mbC87Wz7QQ6PPy9XEa8BJQ=; b=vNWDnH92DLaXVpDlF7UDa4mEbDP+3Q1b1zTswZvUb3kBinHim28xcoGnAo1MYaAy/+ 84Wng2XfgrPbuubXENicVxtEUpHPJb6JOK9OxATUPl5172p25TVDOB63qW1yZeojw73K 878J7bdI9i/zdhX2WUvNsl2aCIF0e+qSWZh4DOGn+1zmtf81lLUtEMfq8Rdhc9/1V3Q3 KBvud5j9ZpIQ0hZN2rFLYK14Sl0jwEpqC/AQ3mjKEMrPar1/pALxI8maewdn4+3J5Sjk gN9WmEapcjWKb3HirwHkYFX0zuTO0s1aCLD27Eoj1OieEdshnWdSDY3VwCxxzUfxxbL+ hadw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738171411; x=1738776211; 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=1nv4yVODl7ioMW6jJkbW1mbC87Wz7QQ6PPy9XEa8BJQ=; b=C/pZoV+0pxwEfsmQkOHJUTBEPzFsGKB0tSyyL2YOPCA1mKbzMIeYmgQvtmakBvrTl9 qkB0tFXGREv688fqlrf9FeaBcTgXQ+szbOFi0iKEQq8k56pnBi6stkWup+WFnRCGOurQ H4/h7WDMFJecUroEQ8wIYADsUGJ7vSk9M9HYD5EuZFt+cokzAkq/p4cbP5eIzyo1YMxd +P3IwkA1zOEaUpD6HMPDVPryyqHWtP7Imcx2syZX83Vn1hhCcUtgeb2Y6cK2T5nDqh8+ 5tx7X4LefxHjK3EHhOJYGtwSjiyULj2fxOK1Esldo8FXzDqi2JN/5j9KNk8DRZvOY9Bk knLw== X-Forwarded-Encrypted: i=1; AJvYcCVa0/hjz0w2TH6NRwQMmNE2T/O2NZpCeVnpWyYEO9SiltvzTxdaKzMFheD/45uon2UsVlByI+Cuag==@kvack.org X-Gm-Message-State: AOJu0Yw6p5+WfMOkq3DXY6m0T0BDCBLGuhd2fs5UmjvPp3Ms4pInIZuZ K0L+mf2bSA/jCSp3/2NNJ8WLb5dqgATzV5e7w3Hy7odcbULp4b+BHumLi8cGJs3FVNFkqrH2Ig= = X-Google-Smtp-Source: AGHT+IFIuIn6HWlnbbn3OX1lNHWQpyl+hx+IsFXmYLoKnyKlPSNVYQFkHXDzngFkRTaSpQvhKnRXZbiDkQ== X-Received: from wmbjm10.prod.google.com ([2002:a05:600c:6aca:b0:435:21e:7bec]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:a04:b0:436:e3ea:4447 with SMTP id 5b1f17b1804b1-438dc436a08mr37793435e9.30.1738171410842; Wed, 29 Jan 2025 09:23:30 -0800 (PST) Date: Wed, 29 Jan 2025 17:23:13 +0000 In-Reply-To: <20250129172320.950523-1-tabba@google.com> Mime-Version: 1.0 References: <20250129172320.950523-1-tabba@google.com> X-Mailer: git-send-email 2.48.1.262.g85cc9f2d1e-goog Message-ID: <20250129172320.950523-5-tabba@google.com> Subject: [RFC PATCH v2 04/11] KVM: guest_memfd: Add KVM capability to check if guest_memfd is shared 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: 47C0F4000B X-Stat-Signature: ajtzsrm7hka69o4m4suqi8td1zj19wrs X-Rspamd-Server: rspam08 X-Rspam-User: X-HE-Tag: 1738171412-422299 X-HE-Meta: U2FsdGVkX1/0hz+Hw5CJJ06w8ZbUuUjW7BCSlqupEoj1FA2spb+C8emYv1/GXFR8xT9pSOSZXeiMRGyk1XzbPtLfp4LVz9OX9zbNoAEV8c8vdWq0h0mOy1LSGFZ+4QgbpiWMavh4cHkzH3PAGoOs86fHkzjk34szS6JdZ6nt/aggZjJKV5J2dTX9cGeKHeG9E2Xanm9q7pXDBEMgvP2rdUbERKaH9e5cKkpki8CV7/xEk2gGz0GWPI5bjndYkgZTwZPvr/MvWPgJONJw0WPLUaANtPN7ZdzaersVeN9H6NDw+GEWyhdi/yOBihgvqjc3MqRRa8llqzHhVbn8qTyfu8Klv2pKTfsHFWs7j62FVGnDXutEf/q18PBA4mFQRH2vu67zdjxEzjN8ua76jMymwBhYjhAjsFaM1jzW1p2FkXFBvdU0kPyvYyMYc/dGPIgCQS6aYwULelpzBvQF7XbFQvTW6WH/Pf4VINBix4y2Qt+qjQXdISo4QFWVkVDJl0GwBBJXu0QWGzKjaJGQ9ofpZ3ZfJqY4nFUiUhgh0x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AlZZXd46PaWnJM3jF4Ahg== 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_GMEM_SHARED_MEM, which indicates that the VM supports shared memory in guest_memfd, or that the host can create VMs that support shared memory. Supporting shared memory implies that memory can be mapped when shared with the host. For now, this checks only whether the VM type supports sharing guest_memfd backed memory. In the future, it will be expanded to check whether the specific memory address is shared with the host. Signed-off-by: Fuad Tabba --- include/uapi/linux/kvm.h | 1 + virt/kvm/guest_memfd.c | 13 +++++++++++++ virt/kvm/kvm_main.c | 4 ++++ 3 files changed, 18 insertions(+) diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 502ea63b5d2e..3ac805c5abf1 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_GMEM_SHARED_MEM 239 struct kvm_irq_routing_irqchip { __u32 irqchip; diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 86441581c9ae..4e1144ed3446 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -308,6 +308,13 @@ static pgoff_t kvm_gmem_get_index(struct kvm_memory_slot *slot, gfn_t gfn) } #ifdef CONFIG_KVM_GMEM_SHARED_MEM +static bool kvm_gmem_is_shared(struct file *file, pgoff_t pgoff) +{ + struct kvm_gmem *gmem = file->private_data; + + return kvm_arch_gmem_supports_shared_mem(gmem->kvm); +} + static vm_fault_t kvm_gmem_fault(struct vm_fault *vmf) { struct inode *inode = file_inode(vmf->vma->vm_file); @@ -327,6 +334,12 @@ static vm_fault_t kvm_gmem_fault(struct vm_fault *vmf) goto out_folio; } + /* Must be called with folio lock held, i.e., after kvm_gmem_get_folio() */ + if (!kvm_gmem_is_shared(vmf->vma->vm_file, vmf->pgoff)) { + ret = VM_FAULT_SIGBUS; + goto out_folio; + } + if (WARN_ON_ONCE(folio_test_guestmem(folio))) { ret = VM_FAULT_SIGBUS; goto out_folio; diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index de2c11dae231..40e4ed512923 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -4792,6 +4792,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_SHARED_MEM + case KVM_CAP_GMEM_SHARED_MEM: + return !kvm || kvm_arch_gmem_supports_shared_mem(kvm); #endif default: break;