Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp3500069rdh; Thu, 28 Sep 2023 13:36:45 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE0RdxKuwaIL5nZ+y9qf6hUswIxP+mL+qjl9Ea66HuyhXC8gOKwUKl26OXxw/b00KnHVzT2 X-Received: by 2002:a05:6808:639a:b0:3a7:390c:4c43 with SMTP id ec26-20020a056808639a00b003a7390c4c43mr2029921oib.50.1695933405250; Thu, 28 Sep 2023 13:36:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695933405; cv=none; d=google.com; s=arc-20160816; b=PA/tY5p1wT8fWIZ/PNBPIZpvc0Ylbg+k8tSlRyT98brbq9a7hsxUVijDbXdAEbBW/h GecBzKNQzf8zE6M6vHDm4n3PvO2cN6eJ28WYAaBYQDxBV5qpKxZyKK3DCKZTDUo4V1KZ LEVMwzRH8vtimXsz4N77Ao1009MhnbjPSUmWZlW01c86nnjqghCShj0zfl+p96GNMCd4 4yW1gw9jvT6gC3iUpGVx4RYyi429pEwvpNYBj8mT03F7FhOaTj5STfMrqrQfhnfkc5Jx 7luGcbC+0nSSv3IWeG9uHrKsv4ctxuQxYhaCaZoVzxcq8wyAIkdjLWnJEMK1aAbAsqs2 l62w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=W2Qgpx+gPcVi5iw6zaPVN+JMZ4nnFY6HftGrYu1Yym0=; fh=V2vC7s/1M2hH18+2ttRiDZaHrE0o8WE7Q/UWhHIMKLY=; b=bgnj3kvdxJDIs30NpeICkoYtcdbwIIhmjrIgvqi3wtJoFvf/AX1G/icp0Zf+FA5AuY RX66WQYylti864zK/NnEx+AF3SgH+Vd6VI05CS9D+OwKAKIzNd0qNUVafpUl5c9/QttC 1NLgPR3wuMFn81w3rPLIEZn3pCINmhJ9w4diui9VLQRIqJhCMXial81lmUfVjz1i4lxo b5AX8IzOWq2fmORDGU2NwLYJ55l8NZi4Zp0T/saBn+EzP0qp9wjHb6H6RKMDiJcVT+Uy YNf+Vrg1J0M8CsCqv1eoafe7Q6+o1PknFhUxyOxeaj+jpuoA++7SdOUxLrYIVqYjt2wt ks9g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=PoNZABAc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id x24-20020a056a000bd800b00690d42e3347si17535300pfu.157.2023.09.28.13.36.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Sep 2023 13:36:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=PoNZABAc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id AB3D283F6995; Thu, 28 Sep 2023 11:08:09 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230246AbjI1SHs (ORCPT + 99 others); Thu, 28 Sep 2023 14:07:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51572 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229581AbjI1SHr (ORCPT ); Thu, 28 Sep 2023 14:07:47 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D094ADD for ; Thu, 28 Sep 2023 11:06:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1695924416; h=from:from: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:content-transfer-encoding; bh=W2Qgpx+gPcVi5iw6zaPVN+JMZ4nnFY6HftGrYu1Yym0=; b=PoNZABAcfDf7LYJE9JGqBIY94AEieYLpjOQslxnRdNVlt+G3elPcnEop9+PT/eleRDkSYJ u/tqjjr00KZ5m7a9rQ2Yxw6rnppnjvq2O/iWi49IRTv12zigoj1JpZ/WXm0CWDPBgTKmIG gMn6efk45NzUk2enJKGAL3USw5Fu7iA= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-668-4YkjJcL7OQmrDNyzn_vVhQ-1; Thu, 28 Sep 2023 14:06:52 -0400 X-MC-Unique: 4YkjJcL7OQmrDNyzn_vVhQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 42EBC29AA386; Thu, 28 Sep 2023 18:06:52 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2266D140273C; Thu, 28 Sep 2023 18:06:52 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: seanjc@google.com Subject: [PATCH gmem FIXUP] kvm: guestmem: do not use a file system Date: Thu, 28 Sep 2023 14:06:51 -0400 Message-Id: <20230928180651.1525674-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Spam-Status: No, score=2.7 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Level: ** X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Thu, 28 Sep 2023 11:08:09 -0700 (PDT) Use a run-of-the-mill anonymous inode, there is nothing useful being provided by kvm_gmem_fs. Signed-off-by: Paolo Bonzini --- include/uapi/linux/magic.h | 1 - virt/kvm/guest_mem.c | 110 ++++++++++--------------------------- virt/kvm/kvm_main.c | 6 -- 3 files changed, 30 insertions(+), 87 deletions(-) diff --git a/include/uapi/linux/magic.h b/include/uapi/linux/magic.h index afe9c376c9a5..6325d1d0e90f 100644 --- a/include/uapi/linux/magic.h +++ b/include/uapi/linux/magic.h @@ -101,6 +101,5 @@ #define DMA_BUF_MAGIC 0x444d4142 /* "DMAB" */ #define DEVMEM_MAGIC 0x454d444d /* "DMEM" */ #define SECRETMEM_MAGIC 0x5345434d /* "SECM" */ -#define KVM_GUEST_MEMORY_MAGIC 0x474d454d /* "GMEM" */ #endif /* __LINUX_MAGIC_H__ */ diff --git a/virt/kvm/guest_mem.c b/virt/kvm/guest_mem.c index a819367434e9..73b841a2e1b1 100644 --- a/virt/kvm/guest_mem.c +++ b/virt/kvm/guest_mem.c @@ -3,14 +3,10 @@ #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; @@ -356,23 +352,40 @@ static const struct inode_operations kvm_gmem_iops = { .setattr = kvm_gmem_setattr, }; -static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags, - struct vfsmount *mnt) +static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags) { const char *anon_name = "[kvm-gmem]"; - const struct qstr qname = QSTR_INIT(anon_name, strlen(anon_name)); struct kvm_gmem *gmem; struct inode *inode; struct file *file; int fd, err; - inode = alloc_anon_inode(mnt->mnt_sb); - if (IS_ERR(inode)) - return PTR_ERR(inode); + fd = get_unused_fd_flags(0); + if (fd < 0) + return fd; - err = security_inode_init_security_anon(inode, &qname, NULL); - if (err) - goto err_inode; + gmem = kzalloc(sizeof(*gmem), GFP_KERNEL); + if (!gmem) { + err = -ENOMEM; + goto err_fd; + } + + file = anon_inode_getfile(anon_name, &kvm_gmem_fops, gmem, + O_RDWR); + if (IS_ERR(file)) { + err = PTR_ERR(file); + goto err_gmem; + } + + file->f_flags |= O_LARGEFILE; + + kvm_get_kvm(kvm); + gmem->kvm = kvm; + xa_init(&gmem->bindings); + list_add(&gmem->entry, &file->f_mapping->private_list); + + 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; @@ -385,44 +398,13 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags, /* Unmovable mappings are supposed to be marked unevictable as well. */ WARN_ON_ONCE(!mapping_unevictable(inode->i_mapping)); - fd = get_unused_fd_flags(0); - if (fd < 0) { - err = fd; - goto err_inode; - } - - file = alloc_file_pseudo(inode, mnt, "kvm-gmem", O_RDWR, &kvm_gmem_fops); - if (IS_ERR(file)) { - err = PTR_ERR(file); - goto err_fd; - } - - file->f_flags |= O_LARGEFILE; - file->f_mapping = inode->i_mapping; - - gmem = kzalloc(sizeof(*gmem), GFP_KERNEL); - if (!gmem) { - err = -ENOMEM; - goto err_file; - } - - kvm_get_kvm(kvm); - gmem->kvm = kvm; - xa_init(&gmem->bindings); - - file->private_data = gmem; - - list_add(&gmem->entry, &inode->i_mapping->private_list); - fd_install(fd, file); return fd; -err_file: - fput(file); +err_gmem: + kfree(gmem); err_fd: put_unused_fd(fd); -err_inode: - iput(inode); return err; } @@ -455,7 +437,7 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args) if (!kvm_gmem_is_valid_size(size, flags)) return -EINVAL; - return __kvm_gmem_create(kvm, size, flags, kvm_gmem_mnt); + return __kvm_gmem_create(kvm, size, flags); } int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot, @@ -603,35 +585,3 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, return r; } EXPORT_SYMBOL_GPL(kvm_gmem_get_pfn); - -static int kvm_gmem_init_fs_context(struct fs_context *fc) -{ - if (!init_pseudo(fc, KVM_GUEST_MEMORY_MAGIC)) - return -ENOMEM; - - 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, -}; - -int kvm_gmem_init(void) -{ - kvm_gmem_mnt = kern_mount(&kvm_gmem_fs); - if (IS_ERR(kvm_gmem_mnt)) - return PTR_ERR(kvm_gmem_mnt); - - /* For giggles. Userspace can never map this anyways. */ - kvm_gmem_mnt->mnt_flags |= MNT_NOEXEC; - - return 0; -} - -void kvm_gmem_exit(void) -{ - kern_unmount(kvm_gmem_mnt); - kvm_gmem_mnt = NULL; -} diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index a83dfef1316e..4a1ded1faf84 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -6424,10 +6424,6 @@ int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module) if (r) goto err_async_pf; - r = kvm_gmem_init(); - if (r) - goto err_gmem; - kvm_chardev_ops.owner = module; kvm_preempt_ops.sched_in = kvm_sched_in; @@ -6454,8 +6450,6 @@ int kvm_init(unsigned vcpu_size, unsigned vcpu_align, struct module *module) err_register: kvm_vfio_ops_exit(); err_vfio: - kvm_gmem_exit(); -err_gmem: kvm_async_pf_deinit(); err_async_pf: kvm_irqfd_exit(); -- 2.39.1