From patchwork Thu Oct 10 08:59:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13829772 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 400E4CF07D9 for ; Thu, 10 Oct 2024 08:59:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C92926B0089; Thu, 10 Oct 2024 04:59:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C40B66B008A; Thu, 10 Oct 2024 04:59:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AE14B6B008C; Thu, 10 Oct 2024 04:59:39 -0400 (EDT) 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 8DA526B0089 for ; Thu, 10 Oct 2024 04:59:39 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id C4D7EAC62A for ; Thu, 10 Oct 2024 08:59:32 +0000 (UTC) X-FDA: 82657094478.14.59F0482 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) by imf02.hostedemail.com (Postfix) with ESMTP id 8215480004 for ; Thu, 10 Oct 2024 08:59:35 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=WtDhegT6; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf02.hostedemail.com: domain of 3d5cHZwUKCAQxeffeksskpi.gsqpmry1-qqozego.svk@flex--tabba.bounces.google.com designates 209.85.221.74 as permitted sender) smtp.mailfrom=3d5cHZwUKCAQxeffeksskpi.gsqpmry1-qqozego.svk@flex--tabba.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728550665; a=rsa-sha256; cv=none; b=FvSFZhp3KxrspGw69aSB4Y55j7bDv+/3DxLCJrp0oIPVN4Przxm623KEbgtkM72LRRRIGa 6edBdXDHdoJbDRvg/LxnKsaNf8V18fojgAFdhccmqoKR3hcF0Pg3Mzc7RQRw1fcranp30v US7/spaKjN77Tq2xNCjCWzsCS6ie+q0= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=WtDhegT6; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf02.hostedemail.com: domain of 3d5cHZwUKCAQxeffeksskpi.gsqpmry1-qqozego.svk@flex--tabba.bounces.google.com designates 209.85.221.74 as permitted sender) smtp.mailfrom=3d5cHZwUKCAQxeffeksskpi.gsqpmry1-qqozego.svk@flex--tabba.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728550665; 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=Vyku6e6IRAvv3Cnewk6FjoCA5VWGrQWG+Ki8Kqf1epQ=; b=CdbQv2L1VfOh/jlaAZOBY61LZm4SzbesT+QTctbqH/aHvmR5Wx6pu87d9+5YTNrvM9+FLH rOVfOV8B8VgOM2ghveuXJ0bpHaObwuz49/NhjPUjkOgqT9BQWbVkJluBAvir+vRc2f4NmQ 4vu7GfmgCnjqR3ssUoEHFagKgaqoGyQ= Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-37d45f1e935so265908f8f.0 for ; Thu, 10 Oct 2024 01:59:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728550776; x=1729155576; 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=Vyku6e6IRAvv3Cnewk6FjoCA5VWGrQWG+Ki8Kqf1epQ=; b=WtDhegT6eTlC+DpGc6LSLEdrlW84nEmTXzPtqA2jnIDnk+qv8p5BDjeRszbY781RB+ qrlwFxy3KwrImCntUT4BlhHRkf5Q3pMGS2kvqlAT8kQ47CfG9W1iQPK3L6ZieR1slvaJ yxc9OG9BpDxVJ6ru7GdGHTDSi2cM65itWuMWyuLcCdMLVEA3Au/6wi03ub3oEe+D6soT rhiWUSuCWbMk613HX6x8Z9HZLzootQ5N/sGwxcljaxfc/8z9Jdfx4t3SrAbQra9Hsrx1 zQBTH5GfzQAKXhLGu+1ZbW35tf6MLq0HbQkh1EbRPHP+zWMCdgrqYqD4lMWCm0fGZFS+ IxEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728550776; x=1729155576; 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=Vyku6e6IRAvv3Cnewk6FjoCA5VWGrQWG+Ki8Kqf1epQ=; b=haiOTUQmDvdaenDiWuX/gp0GG/aa3/zGzc01/P9jdU2vrr7QMM/KabIRpgLTiS8tCi LATda28/r97/49YSKM8fuODp+XhZbX2u8s9iLbGOnPWzr2ajYF9yR65yhOrBbMFdYmjt zoHAUT4qnORbg/nmPBkDwIIF7g/m6BZbBiK7fXrci6eio/AorbLX1hp2MRN/3UIAwBdb +A454bEApQbvEkBctT2iGjvr9Bv8jgPWoUdWseCjHIFxrzcr0GibomIWhj7Ou1BvaRZ/ R1t8fOODcoVzByIpXSl9cHpMWdSxnGh6Fq0i3yctqAwphXSo8OKKk0WjSk1GAElUWok8 jW+w== X-Forwarded-Encrypted: i=1; AJvYcCUEp6+KCoV1W/0geIwjgRxvAf0vw58BH9ZL2wtDRUDSnc0MEvlD/4MqgCD2KAd8QXYZZxuyuv+rWg==@kvack.org X-Gm-Message-State: AOJu0YyaStPLJNGSasSs+lMtjZcXwgaTEvyfc4/atUK5Di3io//RvY6N dNRHcJtNaZk8TlHyR0nC6KXb7C+uJvcc+ShrDZH5UZlKBDo1+cWFmhOYmNUKw8Qt/6TUv/8TNQ= = X-Google-Smtp-Source: AGHT+IGSZpfTUz1T03Ghz7pMPW+WsgsV7vYF/u4tI/gDmrDsGEArgV4hEd1O0Gy7y58cVD5g/Q9cGyXW2w== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:adf:ed10:0:b0:37d:4332:e91a with SMTP id ffacd0b85a97d-37d4332ea66mr2783f8f.0.1728550775123; Thu, 10 Oct 2024 01:59:35 -0700 (PDT) Date: Thu, 10 Oct 2024 09:59:20 +0100 In-Reply-To: <20241010085930.1546800-1-tabba@google.com> Mime-Version: 1.0 References: <20241010085930.1546800-1-tabba@google.com> X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog Message-ID: <20241010085930.1546800-2-tabba@google.com> Subject: [PATCH v3 01/11] KVM: guest_memfd: Make guest mem use guest mem inodes instead of anonymous inodes 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: rspam12 X-Rspamd-Queue-Id: 8215480004 X-Stat-Signature: 3qafrscsczf3sjxyg57b7rm9c8zay3p3 X-Rspam-User: X-HE-Tag: 1728550775-982583 X-HE-Meta: U2FsdGVkX1/zOen/w2wAf1dIq6dx+oSKAEIb7oe2cUe6jWrpeYzaLP6/I4P2CY7XLkNkiTdce8VGPQ2HIh1GYkrUon/Y7r1+Ey6vizd8+FWqTz8yc7mirxWemKo6pDeXkCGpKB2/ygaapnyE8uqOXYH1CJU0uSvlUPgKtIbykuodzDWIfRWXRcS99Es5Q34DlTUj4cCpGMgne3X25D9INECj4pE0LFUaEjAlYJ7Ar6H0z5klG7xtPWkmqYJP4vv0DOt8RIVog8ydTCuwhVRFQboo9AVhj6cRdxGkQXXkwpU/5pXXmBOaxlnVHKvkyckbM4FOQgpI+9xtEBLa0V+s+lROAnHevBK4uTyL+KKoUQIrKzNZIvbhvY9pb6L0NQbkhSvhvpRi4xUSaC/QO76J2G1wc1Ler1HIzByYjvllvxTq1MS/RI1NI6WbQYC+h8L5Y7rSTdwNfKjx4/V6v3RRNIM3jU7uSwC0prhefBsFPEpbs7K/vyl2KnqCVT1iopZO7OErdsh9LEM8608t/45hnGNV96Saw6YG63iDQdeymDuB0m+Fy5/49Gs/5jMTvPSSDy0lgx0LKZt37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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: From: Ackerley Tng Using guest mem inodes allows us to store metadata for the backing memory on the inode. Metadata will be added in a later patch to support HugeTLB pages. Metadata about backing memory should not be stored on the file, since the file represents a guest_memfd's binding with a struct kvm, and metadata about backing memory is not unique to a specific binding and struct kvm. Signed-off-by: Ackerley Tng Signed-off-by: Fuad Tabba --- include/uapi/linux/magic.h | 1 + virt/kvm/guest_memfd.c | 119 ++++++++++++++++++++++++++++++------- 2 files changed, 100 insertions(+), 20 deletions(-) diff --git a/include/uapi/linux/magic.h b/include/uapi/linux/magic.h index bb575f3ab45e..169dba2a6920 100644 --- a/include/uapi/linux/magic.h +++ b/include/uapi/linux/magic.h @@ -103,5 +103,6 @@ #define DEVMEM_MAGIC 0x454d444d /* "DMEM" */ #define SECRETMEM_MAGIC 0x5345434d /* "SECM" */ #define PID_FS_MAGIC 0x50494446 /* "PIDF" */ +#define GUEST_MEMORY_MAGIC 0x474d454d /* "GMEM" */ #endif /* __LINUX_MAGIC_H__ */ diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 8f079a61a56d..5d7fd1f708a6 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -1,12 +1,17 @@ // SPDX-License-Identifier: GPL-2.0 +#include +#include #include #include #include +#include #include #include #include "kvm_mm.h" +static struct vfsmount *kvm_gmem_mnt; + struct kvm_gmem { struct kvm *kvm; struct xarray bindings; @@ -302,6 +307,38 @@ static inline struct file *kvm_gmem_get_file(struct kvm_memory_slot *slot) return get_file_active(&slot->gmem.file); } +static const struct super_operations kvm_gmem_super_operations = { + .statfs = simple_statfs, +}; + +static int kvm_gmem_init_fs_context(struct fs_context *fc) +{ + struct pseudo_fs_context *ctx; + + if (!init_pseudo(fc, GUEST_MEMORY_MAGIC)) + return -ENOMEM; + + ctx = fc->fs_private; + ctx->ops = &kvm_gmem_super_operations; + + return 0; +} + +static struct file_system_type kvm_gmem_fs = { + .name = "kvm_guest_memory", + .init_fs_context = kvm_gmem_init_fs_context, + .kill_sb = kill_anon_super, +}; + +static void kvm_gmem_init_mount(void) +{ + kvm_gmem_mnt = kern_mount(&kvm_gmem_fs); + BUG_ON(IS_ERR(kvm_gmem_mnt)); + + /* For giggles. Userspace can never map this anyways. */ + kvm_gmem_mnt->mnt_flags |= MNT_NOEXEC; +} + static struct file_operations kvm_gmem_fops = { .open = generic_file_open, .release = kvm_gmem_release, @@ -311,6 +348,8 @@ static struct file_operations kvm_gmem_fops = { void kvm_gmem_init(struct module *module) { kvm_gmem_fops.owner = module; + + kvm_gmem_init_mount(); } static int kvm_gmem_migrate_folio(struct address_space *mapping, @@ -392,11 +431,67 @@ static const struct inode_operations kvm_gmem_iops = { .setattr = kvm_gmem_setattr, }; +static struct inode *kvm_gmem_inode_make_secure_inode(const char *name, + loff_t size, u64 flags) +{ + const struct qstr qname = QSTR_INIT(name, strlen(name)); + struct inode *inode; + int err; + + inode = alloc_anon_inode(kvm_gmem_mnt->mnt_sb); + if (IS_ERR(inode)) + return inode; + + err = security_inode_init_security_anon(inode, &qname, NULL); + if (err) { + iput(inode); + return ERR_PTR(err); + } + + inode->i_private = (void *)(unsigned long)flags; + inode->i_op = &kvm_gmem_iops; + inode->i_mapping->a_ops = &kvm_gmem_aops; + inode->i_mode |= S_IFREG; + inode->i_size = size; + mapping_set_gfp_mask(inode->i_mapping, GFP_HIGHUSER); + mapping_set_inaccessible(inode->i_mapping); + /* Unmovable mappings are supposed to be marked unevictable as well. */ + WARN_ON_ONCE(!mapping_unevictable(inode->i_mapping)); + + return inode; +} + +static struct file *kvm_gmem_inode_create_getfile(void *priv, loff_t size, + u64 flags) +{ + static const char *name = "[kvm-gmem]"; + struct inode *inode; + struct file *file; + + if (kvm_gmem_fops.owner && !try_module_get(kvm_gmem_fops.owner)) + return ERR_PTR(-ENOENT); + + inode = kvm_gmem_inode_make_secure_inode(name, size, flags); + if (IS_ERR(inode)) + return ERR_CAST(inode); + + file = alloc_file_pseudo(inode, kvm_gmem_mnt, name, O_RDWR, + &kvm_gmem_fops); + if (IS_ERR(file)) { + iput(inode); + return file; + } + + file->f_mapping = inode->i_mapping; + file->f_flags |= O_LARGEFILE; + file->private_data = priv; + + return file; +} + static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags) { - const char *anon_name = "[kvm-gmem]"; struct kvm_gmem *gmem; - struct inode *inode; struct file *file; int fd, err; @@ -410,32 +505,16 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags) goto err_fd; } - file = anon_inode_create_getfile(anon_name, &kvm_gmem_fops, gmem, - O_RDWR, NULL); + file = kvm_gmem_inode_create_getfile(gmem, size, flags); if (IS_ERR(file)) { err = PTR_ERR(file); goto err_gmem; } - file->f_flags |= O_LARGEFILE; - - inode = file->f_inode; - WARN_ON(file->f_mapping != inode->i_mapping); - - inode->i_private = (void *)(unsigned long)flags; - inode->i_op = &kvm_gmem_iops; - inode->i_mapping->a_ops = &kvm_gmem_aops; - inode->i_mode |= S_IFREG; - inode->i_size = size; - mapping_set_gfp_mask(inode->i_mapping, GFP_HIGHUSER); - mapping_set_inaccessible(inode->i_mapping); - /* Unmovable mappings are supposed to be marked unevictable as well. */ - WARN_ON_ONCE(!mapping_unevictable(inode->i_mapping)); - kvm_get_kvm(kvm); gmem->kvm = kvm; xa_init(&gmem->bindings); - list_add(&gmem->entry, &inode->i_mapping->i_private_list); + list_add(&gmem->entry, &file_inode(file)->i_mapping->i_private_list); fd_install(fd, file); return fd;