From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E03A22DE70E for ; Tue, 15 Jul 2025 09:34:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752572081; cv=none; b=eCwi5eYjMgLiwdRTRPpugt4gA1Ci9p3OqGEA0/PffoTP+TtuBgsSJVdCBpRn3mtc4qVMV3SL0ygVuBDkLIFk5B8eQ3y/SBhzclXCwDg3btEb2beTQgg93LKGBqpK0gamUHpuL0Ud/eRbxFuY8qHxuqmsykYshQUVGRzGhK7Mp7Q= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752572081; c=relaxed/simple; bh=s81rdo6xJig6RnBs9z2MASUF9cmSxvjSWz88FGej91E=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=UAXiu9RmWWb8ErAxF13Opn78RdlH05EajaQP2DCFeq8Bvp3E37QA3tNQghwQtFgUb49UKKEEK/N5Wo+gJNi238YBki4S9N6cMVPk0zRINhdtnPicC0fb5kPp36CeMjBNFEkN1jsH85+6tSbafCQo/i3mv7rHWoJniF64BRNcQBg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--tabba.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Ukx/raeD; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--tabba.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Ukx/raeD" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-45624f0be48so4830795e9.3 for ; Tue, 15 Jul 2025 02:34:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752572074; x=1753176874; darn=vger.kernel.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=9qSAaCs5u3stWdE7jpelRBQbwP+260qm4giQezY97SA=; b=Ukx/raeDFM+0wyBFaYfUpoQW9GZTYAhZ4C24CobkQk/aircFqyHigW7KHTeKXtA1c/ 438cNTNBNcIJRKEiP+GUFVcaeWQpbuc4DSrH2xrVlVH5D1cFllgZ+lI3SI0dTUge+L4Q R064R0AlrqyvOumKkyaJM0+kwj4d7IH92IvbzS1MRY4bfRWc8mY1cB8CKUjOHi5tc0X2 xFNccUpGP5QQw7t3tZQfeUHqxex7UtvVMwzLlGYyGa8W65/NcFHu3t9JE6x/xgeHnH/g QcogCub19nNTVWa+W9yxfh+hzsjqg8OM3K5+cnHIqJrfKmjm4l63l5FAhyl29MceRKiv vWmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752572074; x=1753176874; 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=9qSAaCs5u3stWdE7jpelRBQbwP+260qm4giQezY97SA=; b=e1ZVDAgIRT6Kh2EWQqZp/tkPGe2P3N/YXb0gsrDtFtNYETBgUvgsYQi0W9eur/hhrn K+qjr/RKJ67kdXrLRqijuFeI+X5B7b2LDy62ztEznuAcT9OC78CuI6h+kH3nFm7Yttsp HzM708s+B0tOigKmPsozaRmKEA/oo5S60mtnRCyC/WFArhbM3CPEPn0NVUeemqTrjgHA k8LgV5WYvyN3cX1Cu5n6C9w+74mSBm7TOJRwfP0pDbv5Y3SMQOnuCBv5DX8qev2i8hrF fT9q/5JLV0DTxE8SYLAA0LFGvbLc2KXr+rxhnAP4XVWuMgn73/uNbzsG1dTrKqdLA4/2 EKZA== X-Gm-Message-State: AOJu0YxzggpTVSUmCG5+BxB+4XRW3hayNp+vkAOFx5/wCluwuw5FsmO2 K8RkTNVv3J32mSI5cFTnj5TnQLlR2aeGkJCvtIedqJNGL8qonVyxQ9dhXG7Y0pYCuz7p/Tb70uC Od4/GLNp8WnlbkEdpsmrsifOkI3z00lvWW0RS/C1jRpwvEBgeRn1KFvb2hA0aoHvgW2HjW3E/o+ S9KCUeX9tJNpFGW6xzuvK8v6/vMew= X-Google-Smtp-Source: AGHT+IGOJuaP0WOYc4NF1TL35cx/kQk7xjXiKYWsLOJ3hP5PbZs0l8odgkeUlx4VVQ9iglD1xAj61cXy0A== X-Received: from wmsz20.prod.google.com ([2002:a05:600c:c174:b0:456:1075:9202]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:3d8f:b0:43c:fbba:41ba with SMTP id 5b1f17b1804b1-45565edca7emr116887765e9.28.1752572071954; Tue, 15 Jul 2025 02:34:31 -0700 (PDT) Date: Tue, 15 Jul 2025 10:33:48 +0100 In-Reply-To: <20250715093350.2584932-1-tabba@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250715093350.2584932-1-tabba@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250715093350.2584932-20-tabba@google.com> Subject: [PATCH v14 19/21] KVM: Introduce the KVM capability KVM_CAP_GMEM_MMAP From: Fuad Tabba To: kvm@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-mm@kvack.org, kvmarm@lists.linux.dev 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, 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, peterx@redhat.com, pankaj.gupta@amd.com, ira.weiny@intel.com, tabba@google.com Content-Type: text/plain; charset="UTF-8" Introduce the new KVM capability KVM_CAP_GMEM_MMAP. This capability signals to userspace that a KVM instance supports host userspace mapping of guest_memfd-backed memory. The availability of this capability is determined per architecture, and its enablement for a specific guest_memfd instance is controlled by the GUEST_MEMFD_FLAG_MMAP flag at creation time. Update the KVM API documentation to detail the KVM_CAP_GMEM_MMAP capability, the associated GUEST_MEMFD_FLAG_MMAP, and provide essential information regarding support for mmap in guest_memfd. Reviewed-by: David Hildenbrand Reviewed-by: Gavin Shan Reviewed-by: Shivank Garg Signed-off-by: Fuad Tabba --- Documentation/virt/kvm/api.rst | 9 +++++++++ include/uapi/linux/kvm.h | 1 + virt/kvm/kvm_main.c | 4 ++++ 3 files changed, 14 insertions(+) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index 43ed57e048a8..5169066b53b2 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -6407,6 +6407,15 @@ most one mapping per page, i.e. binding multiple memory regions to a single guest_memfd range is not allowed (any number of memory regions can be bound to a single guest_memfd file, but the bound ranges must not overlap). +When the capability KVM_CAP_GMEM_MMAP is supported, the 'flags' field supports +GUEST_MEMFD_FLAG_MMAP. Setting this flag on guest_memfd creation enables mmap() +and faulting of guest_memfd memory to host userspace. + +When the KVM MMU performs a PFN lookup to service a guest fault and the backing +guest_memfd has the GUEST_MEMFD_FLAG_MMAP set, then the fault will always be +consumed from guest_memfd, regardless of whether it is a shared or a private +fault. + See KVM_SET_USER_MEMORY_REGION2 for additional details. 4.143 KVM_PRE_FAULT_MEMORY diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 3beafbf306af..698dd407980f 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -960,6 +960,7 @@ struct kvm_enable_cap { #define KVM_CAP_ARM_EL2 240 #define KVM_CAP_ARM_EL2_E2H0 241 #define KVM_CAP_RISCV_MP_STATE_RESET 242 +#define KVM_CAP_GMEM_MMAP 243 struct kvm_irq_routing_irqchip { __u32 irqchip; diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 46bddac1dacd..f1ac872e01e9 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -4916,6 +4916,10 @@ static int kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg) #ifdef CONFIG_KVM_GMEM case KVM_CAP_GUEST_MEMFD: return !kvm || kvm_arch_supports_gmem(kvm); +#endif +#ifdef CONFIG_KVM_GMEM_SUPPORTS_MMAP + case KVM_CAP_GMEM_MMAP: + return !kvm || kvm_arch_supports_gmem_mmap(kvm); #endif default: break; -- 2.50.0.727.gbf7dc18ff4-goog