From mboxrd@z Thu Jan 1 00:00:00 1970 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id BEA90C54F54 for ; Tue, 28 Jul 2026 12:11:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To:From: Subject:Message-ID:References:Mime-Version:In-Reply-To:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=c9jjs1g4NvcQ3GQcie84Y49TXK0CTcsSbKyhpxDCGHo=; b=tVFKZK6TYEn4vzL0zeSrRdm1ch ftFKs+9iBAaIQ/OTrX6cB2I09PhINWVDQBiyLaGGwX3rK3jVapFMrQu2jbTNNYi47BSKCUMZAo4vc 695OWVMzhSRE6qJ6YMs8ZwGpWTwEo+PrQW3ebPcDvOtdjfxcclPoHyy30jgBG9Kx2MTE/boWeCELB kJx2E+baRhzyveT5mGN/Iy2BGBIBY7v0zpOHtPL12bf6e7jOZI2bMv6PAZsirhBMJGD9yZ3Zg55yD IHfE9Bbuul6ImpE2V94bVPtwwbSov0Wgep+twBUFHHAXu7ew2A2iMmoibzw7/iXnWUAtn7FOfvjR6 oEZ+opZQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.99.1 #2 (Red Hat Linux)) id 1wogf4-00000005CmV-2V0Q; Tue, 28 Jul 2026 12:11:54 +0000 Received: from mail-ej1-x647.google.com ([2a00:1450:4864:20::647]) by bombadil.infradead.org with esmtps (Exim 4.99.1 #2 (Red Hat Linux)) id 1wogf2-00000005Cfq-0otz for kexec@lists.infradead.org; Tue, 28 Jul 2026 12:11:53 +0000 Received: by mail-ej1-x647.google.com with SMTP id a640c23a62f3a-c1f548718d0so203300066b.0 for ; Tue, 28 Jul 2026 05:11:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1785240708; x=1785845508; darn=lists.infradead.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=c9jjs1g4NvcQ3GQcie84Y49TXK0CTcsSbKyhpxDCGHo=; b=I7D4RkbVd22UgyMNU2GmgBI3NL5SjcmIhwbw5ucUB4LpRggdbzv6g8rEfk27MAS81L G2dBzKKpY/zx2f9z5ZDzlPYS6220d08Uo9VoGZ4qlDlA42JwRvjTVo7vDE9w7oWcLMTv E4BLTl3+ixR+acx4qjMLueLTtu+EuD0k/yX89WFR3QObtCTL6Kek4uOLLQDgsVBaQEzt KyXvrzEWdZyYiBtYNg+Lbl/iwa31NmG5ybGC4wrw5E5oOzrZF/hrnBZjvsKhN56u8+Ue +xfhF9cow14ljF7ZJaYYxp0AodTGNv27dTMfSB3bVYgC/hVOkfTc+IiqnQ98OUaHeWjG TvFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785240708; x=1785845508; h=content-type: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:content-type; bh=c9jjs1g4NvcQ3GQcie84Y49TXK0CTcsSbKyhpxDCGHo=; b=hdPyHlijLi8GCz/sVnaujwfvRwUMIslNVZtk0Q0VRHxiRGyUup7vng3bLi1yP1Ezae 0dvMDo+SAVl4NhIbDf2o2xJQ/GrWS7iZnHBf37huG024g7MXhG3GehbEm4AyMUXjnPOT tBKlwXZ0yTgFHkjW/ZBRZN4N8icmgsRZspwuLG2j1PFLOvRvulkcOzZouFBuhojiWJsX 6jTlC9wfIykXlWFSGLdJvb4ldEhMHDOmOQMhIJTtL9xQpXu0HGuPWTBD5zxnTZVukZGj DPhXk6gI5habs6bcTxM5kIrqJuwDBNiN4nhbGMhI9BRkWPOMRSnC9y2lKUVk3GFPUiTe 1gWQ== X-Forwarded-Encrypted: i=1; AHgh+RrrLVNUDPzFtbi2zHvcYN9OooDmoCom8oiC4L+TOH7wia5klLY7BlAOxY4eND5A1RxJCaTXpA==@lists.infradead.org X-Gm-Message-State: AOJu0Yx5r+VhmSIC0Pt/5Drcc/a+C5zNCM+kd4FuyOd8JSKgTcPPVH6T 2sCROCeld7KyzePfVcfzOLLN2VB0RyXpmh+ApkYgnjOeoguM2xg180amML2nbKMpdBnIASggKn5 IX5Rb88kgM6xnuBCToA== X-Received: from ejph19.prod.google.com ([2002:a17:906:7cd3:b0:c12:8c27:d430]) (user=tarunsahu job=prod-delivery.src-stubby-dispatcher) by 2002:a17:907:7b82:b0:c15:d6c8:2816 with SMTP id a640c23a62f3a-c1f71e1ea91mr103051766b.27.1785240707658; Tue, 28 Jul 2026 05:11:47 -0700 (PDT) Date: Tue, 28 Jul 2026 12:11:33 +0000 In-Reply-To: <20260728121138.1103610-1-tarunsahu@google.com> Mime-Version: 1.0 References: <20260728121138.1103610-1-tarunsahu@google.com> X-Mailer: git-send-email 2.55.0.229.g6434b31f56-goog Message-ID: <20260728121138.1103610-7-tarunsahu@google.com> Subject: [PATCH v4 06/11] KVM: guest_memfd: Move internal definitions to internal header From: Tarun Sahu To: ackerleytng@google.com, fuad.tabba@linux.dev, Andrew Morton , seanjc@google.com, dmatlack@google.com, Shuah Khan , Jonathan Corbet , david@redhat.com, Tarun Sahu , Pasha Tatashin , Pratyush Yadav , sagis@google.com, Paolo Bonzini , Mike Rapoport , Alexander Graf Cc: linux-kselftest@vger.kernel.org, andre.przywara@arm.com, michael.roth@amd.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, will@kernel.org, vannapurve@google.com, maz@kernel.org, fvdl@google.com, kvm@vger.kernel.org, oliver.upton@linux.dev, kvmarm@lists.linux.dev, alexandru.elisei@arm.com, skhawaja@google.com, aneesh.kumar@kernel.org, linux-doc@vger.kernel.org, David Hildenbrand , yan.y.zhao@intel.com, kexec@lists.infradead.org, suzuki.poulose@arm.com Content-Type: text/plain; charset="UTF-8" X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20260728_051152_270562_5A703659 X-CRM114-Status: GOOD ( 20.74 ) X-BeenThere: kexec@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "kexec" Errors-To: kexec-bounces+kexec=archiver.kernel.org@lists.infradead.org Extract 'struct gmem_file', 'struct gmem_inode', and GMEM_I() from virt/kvm/guest_memfd.c into a new internal header virt/kvm/guest_memfd.h. Also split __kvm_gmem_create() to expose a non-static __kvm_gmem_create_file() helper that returns a 'struct file *' instead of an fd. These internal definitions and helpers allow upcoming guest_memfd Live Update Orchestrator (LUO) preservation code to access guest_memfd internals and reconstruct guest_memfd file instances from preserved state without installing them into a file descriptor table up front. Signed-off-by: Tarun Sahu --- virt/kvm/guest_memfd.c | 68 +++++++++++++++++------------------------- virt/kvm/guest_memfd.h | 39 ++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 40 deletions(-) create mode 100644 virt/kvm/guest_memfd.h diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index db57c5766ab6..dd84bba8974b 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -7,38 +7,12 @@ #include #include #include +#include "guest_memfd.h" #include "kvm_mm.h" static struct vfsmount *kvm_gmem_mnt; -/* - * A guest_memfd instance can be associated multiple VMs, each with its own - * "view" of the underlying physical memory. - * - * The gmem's inode is effectively the raw underlying physical storage, and is - * used to track properties of the physical memory, while each gmem file is - * effectively a single VM's view of that storage, and is used to track assets - * specific to its associated VM, e.g. memslots=>gmem bindings. - */ -struct gmem_file { - struct kvm *kvm; - struct xarray bindings; - struct list_head entry; -}; - -struct gmem_inode { - struct shared_policy policy; - struct inode vfs_inode; - struct list_head gmem_file_list; - - u64 flags; -}; - -static __always_inline struct gmem_inode *GMEM_I(struct inode *inode) -{ - return container_of(inode, struct gmem_inode, vfs_inode); -} #define kvm_gmem_for_each_file(f, inode) \ list_for_each_entry(f, &GMEM_I(inode)->gmem_file_list, entry) @@ -557,23 +531,17 @@ bool __weak kvm_arch_supports_gmem_init_shared(struct kvm *kvm) return true; } -static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags) +struct file *__kvm_gmem_create_file(struct kvm *kvm, loff_t size, u64 flags) { static const char *name = "[kvm-gmem]"; struct gmem_file *f; struct inode *inode; struct file *file; - int fd, err; - - fd = get_unused_fd_flags(0); - if (fd < 0) - return fd; + int err; f = kzalloc_obj(*f); - if (!f) { - err = -ENOMEM; - goto err_fd; - } + if (!f) + return ERR_PTR(-ENOMEM); /* __fput() will take care of fops_put(). */ if (!fops_get(&kvm_gmem_fops)) { @@ -612,8 +580,7 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags) xa_init(&f->bindings); list_add(&f->entry, &GMEM_I(inode)->gmem_file_list); - fd_install(fd, file); - return fd; + return file; err_inode: iput(inode); @@ -621,7 +588,28 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags) fops_put(&kvm_gmem_fops); err_gmem: kfree(f); -err_fd: + return ERR_PTR(err); +} + +static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags) +{ + struct file *file; + int fd, err; + + fd = get_unused_fd_flags(0); + if (fd < 0) + return fd; + + file = __kvm_gmem_create_file(kvm, size, flags); + if (IS_ERR(file)) { + err = PTR_ERR(file); + goto err_put_fd; + } + + fd_install(fd, file); + return fd; + +err_put_fd: put_unused_fd(fd); return err; } diff --git a/virt/kvm/guest_memfd.h b/virt/kvm/guest_memfd.h new file mode 100644 index 000000000000..c528b046dd69 --- /dev/null +++ b/virt/kvm/guest_memfd.h @@ -0,0 +1,39 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __KVM_GUEST_MEMFD_H__ +#define __KVM_GUEST_MEMFD_H__ 1 + +#include +#include +#include + +/* + * A guest_memfd instance can be associated multiple VMs, each with its own + * "view" of the underlying physical memory. + * + * The gmem's inode is effectively the raw underlying physical storage, and is + * used to track properties of the physical memory, while each gmem file is + * effectively a single VM's view of that storage, and is used to track assets + * specific to its associated VM, e.g. memslots=>gmem bindings. + */ +struct gmem_file { + struct kvm *kvm; + struct xarray bindings; + struct list_head entry; +}; + +struct gmem_inode { + struct shared_policy policy; + struct inode vfs_inode; + struct list_head gmem_file_list; + + u64 flags; +}; + +static inline struct gmem_inode *GMEM_I(struct inode *inode) +{ + return container_of(inode, struct gmem_inode, vfs_inode); +} + +struct file *__kvm_gmem_create_file(struct kvm *kvm, loff_t size, u64 flags); + +#endif /* __KVM_GUEST_MEMFD_H__ */ -- 2.55.0.229.g6434b31f56-goog