From patchwork Tue Sep 10 23:43:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 13799486 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 1BD38EE01F4 for ; Tue, 10 Sep 2024 23:45:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 139898D00D8; Tue, 10 Sep 2024 19:44:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0C2988D0002; Tue, 10 Sep 2024 19:44:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EA5B38D00D8; Tue, 10 Sep 2024 19:44:57 -0400 (EDT) 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 B98008D0002 for ; Tue, 10 Sep 2024 19:44:57 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 7C0D4A90A1 for ; Tue, 10 Sep 2024 23:44:57 +0000 (UTC) X-FDA: 82550461434.29.5E2BCF4 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf27.hostedemail.com (Postfix) with ESMTP id 9BC3E4000E for ; Tue, 10 Sep 2024 23:44:55 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=wFJ4q8L5; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf27.hostedemail.com: domain of 39tngZgsKCGsJLTNaUNhcWPPXXPUN.LXVURWdg-VVTeJLT.XaP@flex--ackerleytng.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=39tngZgsKCGsJLTNaUNhcWPPXXPUN.LXVURWdg-VVTeJLT.XaP@flex--ackerleytng.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726011891; a=rsa-sha256; cv=none; b=doqcpfinkyVgOC++TP/O6Ma+hcLMrjtQTHuwyCoezArk5UD4WKsR2bGg+SgSYmLgG17qcj CsB8E+f/4g9d2r7p2hg8SkI2O4xzE9c7UFErMA6XD851oYdiY9rIrZDYrO1pyiVshidYM/ GbDvd+CkGL/L2H+Bt2OutDWjJGVAW4A= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=wFJ4q8L5; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf27.hostedemail.com: domain of 39tngZgsKCGsJLTNaUNhcWPPXXPUN.LXVURWdg-VVTeJLT.XaP@flex--ackerleytng.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=39tngZgsKCGsJLTNaUNhcWPPXXPUN.LXVURWdg-VVTeJLT.XaP@flex--ackerleytng.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1726011891; 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=fsO00i1fJ+fyO29aW7nLdVziXXefzp2jZQcWLLFpi3o=; b=DqtfhmH1bQMoa/mCjgn8F7VFwNzUXp5fIbtFDkPQTKDDA4yygEUSMvnoTzW/YXnlmomKd2 BTb8mXgNQJm7iGijgqEv5lUYyOkEQGx2Gh/+j70KbKmUXP6qPCTtC+sPUsXW+nGnSIL6yH UdZu+y379EdN2PKY8xyGPpDyg8Z6iyI= Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-204e310e050so86960175ad.0 for ; Tue, 10 Sep 2024 16:44:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1726011894; x=1726616694; 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=fsO00i1fJ+fyO29aW7nLdVziXXefzp2jZQcWLLFpi3o=; b=wFJ4q8L5zMPJgZcsJWYniRFajI7nhRHcFk7bRUgQvX4RfQJ4eufqV/j3s8y8PJwZ4x LauAym7GnAVRuF3LlC2IRDTfBoMQ1qglzHVHlf0QfESSTGxDWKvXno/D6j/EJyV76JjZ Y9fOurblG8YTyPgqVkMkjVhQXkcmjOWgJaKgrAInz8BYCK2FaR4t1M/s2h4nNj7wDP66 wVaVSXv3vDbVT+Co45A0rxYCIr08Uj/75L5dYI+R6OHd/uyQk9gcSNf9MOG7dXtXHE3P wU0/XFXCWBOkZn74quDT6TZNn3GHR+DkRk53ET7w3pRtojE+r4mhmSOgF7O/qaLaPBtw FMBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726011894; x=1726616694; 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=fsO00i1fJ+fyO29aW7nLdVziXXefzp2jZQcWLLFpi3o=; b=dghE/hxAXiedR1tMeG9p7/CQXcjOY/+bU10MwxcKRO/vImqc7DoJ/CZwA3JNaxaS4l 3n8hbFxQE02bkt3ARn98mzvs54C7HBn0XggIBGzXHEWHsvFjYEM8XU68vdlDOS3seUlg 7C5a1n6WsFlJ03Zw5miJ0qgjx68aIT4yiz495Tx8m5VUlfuB0ufEjhV8BTuQui9WTd4r BCqTVLDGVdaVwxFExZIRwgl66m/FvIgfPKKjlw0s716yw10CEAf5iJfwXECA0fK/vJOQ 32PJK2pMpxkBOdNr5EZzaZsTDIEpd37kvAPuY3cW7y1qpJf2U7oR0aLbeJVsL+5iGXa5 oB6w== X-Forwarded-Encrypted: i=1; AJvYcCUJRm+89iHmdkhfZl2m/VEFgJxkBA0YlSuKMl3bitdtOSM6qyG2t+6sUWTBm1Jn/7+CNnsl5FdodQ==@kvack.org X-Gm-Message-State: AOJu0YwGwW4uiawGZhvzTqj0ePCjrPwytWR8NysYct32tLX9gZgFz9L4 jnb6rbX8lvuxUlVKyWJ/XT1a3FPqtbloHpXpBWWpynqvCmriS+6Ly8w9z/HGQd/r308UwA76eqB B4XQOgTX9rAheIiPScKiAQg== X-Google-Smtp-Source: AGHT+IGkG5bdlVVJvvD6Ym5sklISkCRvUY93a1/yDRHhOSbI1kqRvzrl9TXy8aPAOijAjgn0S5+iR/vuXr5UMgKZiQ== X-Received: from ackerleytng-ctop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:13f8]) (user=ackerleytng job=sendgmr) by 2002:a17:902:c407:b0:205:5284:e52c with SMTP id d9443c01a7336-2074c7995e4mr761845ad.9.1726011894266; Tue, 10 Sep 2024 16:44:54 -0700 (PDT) Date: Tue, 10 Sep 2024 23:43:44 +0000 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.46.0.598.g6f2099f65c-goog Message-ID: Subject: [RFC PATCH 13/39] KVM: guest_memfd: Make guest mem use guest mem inodes instead of anonymous inodes From: Ackerley Tng To: tabba@google.com, quic_eberman@quicinc.com, roypat@amazon.co.uk, jgg@nvidia.com, peterx@redhat.com, david@redhat.com, rientjes@google.com, fvdl@google.com, jthoughton@google.com, seanjc@google.com, pbonzini@redhat.com, zhiquan1.li@intel.com, fan.du@intel.com, jun.miao@intel.com, isaku.yamahata@intel.com, muchun.song@linux.dev, mike.kravetz@oracle.com Cc: erdemaktas@google.com, vannapurve@google.com, ackerleytng@google.com, qperret@google.com, jhubbard@nvidia.com, willy@infradead.org, shuah@kernel.org, brauner@kernel.org, bfoster@redhat.com, kent.overstreet@linux.dev, pvorel@suse.cz, rppt@kernel.org, richard.weiyang@gmail.com, anup@brainfault.org, haibo1.xu@intel.com, ajones@ventanamicro.com, vkuznets@redhat.com, maciej.wieczor-retman@intel.com, pgonda@google.com, oliver.upton@linux.dev, linux-kernel@vger.kernel.org, linux-mm@kvack.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-fsdevel@kvack.org X-Rspam-User: X-Stat-Signature: jszpuwb3rxionpi47qcdi8h3z6b6h7pg X-Rspamd-Queue-Id: 9BC3E4000E X-Rspamd-Server: rspam02 X-HE-Tag: 1726011895-382946 X-HE-Meta: U2FsdGVkX1/UfIzChjJiKM8gp6QBbe/h+HWnWm0uvc9vI1TL2ob5JmjDME1zs3jtSvH0M7AMcfXXl8xeiVQkIU1dpVU1P+9C3t5JxgH1nsEp8LSygkqrbMU99OCz+/y+6gwiRqUPaWFPma1V8ySRuRMASenQ/B8QZ8bfl9EsXwH9PiGPelq7/y1EE0q6JceHg3Yf4M1OAsRMKu8jVF052FEVB5mK8izpzsLTE7Xyg+HcUhF1Rfb2z6aMgMD/cFjvEI+OreYqCHolS5mhoQuI3rqZvaQXmJqVKGRjDu3n+VtG1MMvu/m5+5gI221E5a4nOAJmIFw36vjo1MrtyUh6zZO5hy1SWk+tG51kKd7yuwpUoDjPfb+ZXYrfcNQW6BXzwtrXwBAWCD5HqjMITuUeGL7bBV3wtsG/vxjjfWx++USsqycr4Ul5Ihhdmpv1UOd3e4+ZHMXJ4J3NzaJDuSweTGoD+ftg0SQNC0VInJqSlEz1sYqrgiACdtgtAeUQyBeOWH3drN3rv9yk2UozJu1/3CphVLW/xdhPAxRg+Cdfh4kpOdGG/cqTluvg7NyrZL25nq+ulqpYKH2S5Rs75eiytBULACgLC+ds2aBbkyA2KXpUaR2McYJvulVmzRT8LLMjCEVoSUezhTazUCWzGM9SrIOmNyYHeF48M+jKijvszSYDRQH/i0BqsuJvYLHSaqxCiS+IERVn4I3yz8k39nkM5N5RPr6++Xnsv0gv+VrOoB5AR1nMSaqxMzJzvJuYmGRJFdeqYBIJ642+4Gp3OQvczJIiEQch7930d4fHDkaf/78wnUXOynNj1g4ICY7SWk3imeZUK4EtmUPQbPyj5ZMNaTsIyKgmoamyD7MtYbZ+xuYzOdMiU1E2KseZTBNHfET6J3cLmz8jE6Uis9Q4U8yL+mT8E644yQAIEPzRaxwazBZ2aaJjnLNNanV3Vw/9RjjNnXtmDCeJ8SeRlheYOBX vf855pkJ 4epkYKNaRPZnQN+r1sMj1l9f1H2KOqJjAOdIfa1bFeXwsdK9NYX7xpH4p0vxoVOonZw8gIYMq5sPAX7JCWezCSQZ9hVg3HmHfVEhlcKJCgeTBFBdYnQfY1xNs58UNKwqUDYM6BjMa+QThBbswk7krA0jAfp3M9mKZTcz9R0qdnApOOijWX//3z6mCvw8qAsiYbiuEC+aZ3S1o+xbpVJbr1iyzXYISUG36ZjPs7cbxEgOUE6ZDUpG7AF/xTKsXi/4RFOV6ISzBl0wQIRoMpdY4T92GPsY2FePbAEAnDUAV2VBJ+8dn/zzy3JXlOcoqPDtuVNXjWge1Q3OErtsqbe9U6GubuqR1vDkGP3DwPumulxc8BHEMPsvcVwpdPdgzKc9dgdnSCg2+3SvTCf4PUx9jTX1qrYp2EkfCaGBnPyrWFqNQ7N2SLNxcml9FLBNtXXAsC4+1ht17CcB33TinDy0IT7FMWjjOtUBRCN7kAh91t3FTc2Ihu8S2k7wWuBpzxWIO7+7U/u697RPv74/ymGPXCO6QwIetOV5zpVb2Qw8/oIHlzCfySXva07FdU2AGYk+cKgcHS1wzQ9PjtEgFMGAJ1/vmvSAgZo7LOB2fPqkygaxm983mfKcCOVxw/X7nvdx74JLH9V5kcd+DHVeJBoBvCJw/BiQ+J63dNlYrOi8nqp9/xUesY5C4WZKQbtaBeldrnWqi4v1SZDMX8RU= 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: 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 --- 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;