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/5jMTvPSSDy0lgx0LKZt37FtWq18wEt2i5ynJldau6eb6rTPjQ65h8aoqwLFkl1Exi2ZpIBBhvw7jp54Zo0ZbhF8/NnOGTu3GfqbEEjkL27nboH6xUouGt6pXY/8gAbvGg5EVhuS+5o4uzHrfa7Gtf2hSnnabzuSQ8IkDWdYWOA2FNJZ7gG+imq/ORCZqYqnBFUflwnEOqtuXOPLR8F/41ViXSlNujrS6wJOM8/rTM2cLoJctM6yPIxs23CgX+UD0seQx8ksSWKXtFuXTqFB+cCu49kfgryUJh0fE0d9iVhKVZJrUZ81cUIjJmmekokUGvfpP6g+VexWIdQdIK1/16zCZXzmaOJE8vr2pYYNevhoR1qmRh0R+c9moFMH6y+DzBhcCjKajjgvL/8hHYiaSUDh8GLi yO/k7e/R C+4bBIeXMAVZVxIkBvPLrbNbLoysTuc1PQANlehWfsC1agKCfYhCv5gU1CiYgvgLzFlugXhfeWLB5S0uFTuz7LrcZv4ioM1G/D8XAB5B1oFsJE0NXVZgUTfdxwNbo4KUuQbZwKjZCTSSudH4Z1WVGUje/ZgMUqvEXNBzuL3UtGZSrM6+4NbX5NJq7NQIYYBUcT2qp0IuZYsFrgo2iKqHr+GxRKYUFF91/V60EBeVjJUVwbKDPw1Cdv85cjrkPUQb56HFx4p30vkB5Ynkf9uohCA6qleP/2wNh7xxN/b44VgPT1fsD3+R+dviDnaesL3jBmmgqqmROyP+3Jf2p+AY9cOO40h/+BTMuYygG3jqwpN8dDEN0YH0E6TRLfzrao8qtg/+A51BJ2wlg7mCfm5Gdi4oSMZl9GmFDTcBn3qrZJz2BqmPXYK0EBD60eN4cJSx5sZ7+fSpcYilz2P09yk4NEjCi0xr9kYVmrzC0PLMRvqre3z+1llYUgT3qJOD3Hrtc5h09qsV74d05f2rinOjkoKVqFEjgJy8q1/1snhXaXGXHYxgQS7zHk5YeCmc8j8xQK53Qs67pOL1Y6LqWnPWe9JmRRXcWn5vXXXcweolkZ5Fcq8tUbuDtdVsMWPcC+RZqXWJ3cfgDMNNyNtaKkmMRnoexQw== 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; From patchwork Thu Oct 10 08:59:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13829773 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 A73EFCF07D5 for ; Thu, 10 Oct 2024 08:59:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1E1F46B008A; Thu, 10 Oct 2024 04:59:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 195306B008C; Thu, 10 Oct 2024 04:59:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 00C896B0092; Thu, 10 Oct 2024 04:59:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id D5A896B008A for ; Thu, 10 Oct 2024 04:59:41 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 27083AC944 for ; Thu, 10 Oct 2024 08:59:35 +0000 (UTC) X-FDA: 82657094562.05.FC001CA Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf13.hostedemail.com (Postfix) with ESMTP id 6490520010 for ; Thu, 10 Oct 2024 08:59:38 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=L1l4CxOj; spf=pass (imf13.hostedemail.com: domain of 3eZcHZwUKCAYzghhgmuumrk.iusrot03-ssq1giq.uxm@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3eZcHZwUKCAYzghhgmuumrk.iusrot03-ssq1giq.uxm@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728550597; 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=4yV7g5zb1mLRm2mV+FxGCvZ9J58MmHKq+HH3QCFP3/A=; b=lDxjJ9V2ybFeiSQDkt6kl8OnfCTXjNemBfmC4D+e9n840zS/5SEUqtfOI4TL482y3i9QGD srduMDztZjs6RL0m1W/Fu6vTifURDXpt99Pm9OsJTbcfGReC+kP4frSeAHSO4E/O5nrpWs tm6/tTaFHvJoxj+JHQdQLfx9kbQWlaU= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=L1l4CxOj; spf=pass (imf13.hostedemail.com: domain of 3eZcHZwUKCAYzghhgmuumrk.iusrot03-ssq1giq.uxm@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3eZcHZwUKCAYzghhgmuumrk.iusrot03-ssq1giq.uxm@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728550597; a=rsa-sha256; cv=none; b=otITg9CgKvQXsAYm+T1Zp/dELZ3hTQUy6VoKduLydSktT+3lUPTdfp07DgZivq01kAob4N uBcI29gn6uKXeUUkVXRCb6BD0HXEO2YAq573SmwOZqTX1lT4M+FJ5SLygPywMEEYiMhcml VMAOyxVuBcjukO3LyXMnpulhuHHr/mc= Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-430581bd920so4088885e9.3 for ; Thu, 10 Oct 2024 01:59:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728550778; x=1729155578; 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=4yV7g5zb1mLRm2mV+FxGCvZ9J58MmHKq+HH3QCFP3/A=; b=L1l4CxOj7UhhQF93VB0BNxGdLayfVWv+bneM//6ISIqoJNNCasptDSw/r/AYHVJUZE iM/LBvEicgjAOo8vtBMmvwUQt32mj9LTGx9XBzKIghnwYxV0aW9ycQzCUphZD2e0QE6P ftlN9Efi1j4sP8egrMNXjH95sSegZdk0fpGtDdp9XDjhomMMgkND/S8LNy1Cits28DvT x1OLCEwXOpnYSWN1w0MfYjkEISeChTE2vf4yVfZTw9XyUl2/Kx7/quVJBCbu8knEVdXa rLmkgZP6G+Tjh2skahf2L+Km9H4zu27XIIen8CWFaOeT879GWDO3WHXx+HxA84L87EQD YHfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728550778; x=1729155578; 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=4yV7g5zb1mLRm2mV+FxGCvZ9J58MmHKq+HH3QCFP3/A=; b=mABb/b42JPB2RFvSoVB6eTvWiUuRobJYJlaicCQzXs9CGTJTFJoEsir3OOdBN2q69P tTprq3S0ePDn/nimzBTeb+u4k9Gx3tLpr+IlTYX5S9sI/vstKs0v+p8+SlfNoiyCRtOt F3rrj7AbHOe1uA05bQKFI7m3IO2MjX1C71JxqvWtoj9DLq01KIXdCwEYRivvD0JJr/uM CftJVQrtvR7TIHUAvzNCdZd5dvWVz/eLzAOOWLWJXUHbyKKFpNsEEUnT5vm13N5H2oTo Cc00HCSKmStkrEC7adznqrhQ6yMX7jwcsOHb5G0sfQTrwuGT1VM2OnUpEki8tOX7hmCo gwnA== X-Forwarded-Encrypted: i=1; AJvYcCVImnmZGEFYpjr6+9uLHYncpdcHSyra2TxF23eKnS6MKNPw44jkflS1VWuyKlfSSLBtQ0DpuaMMNw==@kvack.org X-Gm-Message-State: AOJu0YwO/I4NoiI1IZQAX5XCCdAZ3jHrwCxOjeIP66r36SkPCCKaN8pn ptYVlMxPHQ0xeSaRdF4jmfZWKPkBzkYecG734B4ao6m5NEdO5l9C0KZZT8df2eaptcRC30iP3g= = X-Google-Smtp-Source: AGHT+IEhajSTUGX0DrF4NwfEPAhnOGAT61tcgNDfAcBGm901JNES8kGjHixw5Cb+5EsBR7BggcxyXkjnrQ== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a05:600c:5357:b0:42c:b45c:5e95 with SMTP id 5b1f17b1804b1-430ccf091e0mr139615e9.1.1728550777855; Thu, 10 Oct 2024 01:59:37 -0700 (PDT) Date: Thu, 10 Oct 2024 09:59:21 +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-3-tabba@google.com> Subject: [PATCH v3 02/11] KVM: guest_memfd: Track mappability within a struct kvm_gmem_private 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: rspam06 X-Rspamd-Queue-Id: 6490520010 X-Stat-Signature: pm6edrhkkzhqj68ppqr133sg38c5mdat X-Rspam-User: X-HE-Tag: 1728550778-58474 X-HE-Meta: U2FsdGVkX1+/IiJ0D8CuD+xWtq/+7Tp19Xy6CnUdOWtzve4tnGpBCveJSkE3cCb+K3KIM3Ojxp8ExhAghBap3Iu1ZsIMGJrsWLhcTeUrRTjP0YTPN3IP5vI4hEyt7aG7G1hCUN2f68qpOnkt+HOTJc0Ai+Bd2KbrZCTBIvFYdeGzvPYeNvj1wxV55+VkD6WoH/gx20/b4lkyFy8AjcnyUQ81nQVWorWutEoX/N6C06E5C6Z6i4zy5AAzDDYzdTI2Nw1dXS7gS0n8nkXA7wR1UI05GU299u8cjr6Hcm9V1NdWfF5Pu26U/jrUjZbz9zzK9d+KLrF4DwL6bqeEErTX7RCVD9S2UIXdQUQ80ixotzEaLPr6XWY6vC5DPPRszauen6ICu2SYcdY0Z1+XClgsmHbqZ4Wqti4aY6htX/B5eJu5YAJzstpaqvXZ3Pw47N1s7zOBa3g0HcRij6B3GajVillwArtX1fR/2V0vb8HAbDQo4Nc5aAQjFm7y4yzpahaQFIl0SQm3U9v9NsJPAtOC91yRw7WwWsN0pq3zsoFZZoiUMj2DXxo9Dkj9gxdGJ0A/ecZWzLrr4JCITpWzBLgsE0zv0/6OozS6+ib4DMCFX4OTRkWxpsrvST4vSNU4YKXZKKDmcXZVzISvxt6Plvw2Ieg5MoqWZK9W19FD79rSmgVapotbPcdeBiLWxXSrb4BvZDJ3vPrhmU3zhC1NUgCqYoBbHRENmlwxAnfyxst2AMru8djU2kgLie6uWzjqmB9xvKrsM7Iz0l/0jtxtxyJq2r/MZvzGornZ3+bOuAK2uCWYvz8E7DpCV0DxWCtFz3xevzopQSAeridabV3n9wOS5jdF4zPSVD1DfOHRqVHzmdnhAb7kIjqKizE6FUDMXVyYUGlGFq1VTVBFs4GTR/1y3ZTC5L2ibAGqKCND+iGF3VddahY0ZDm2t2L6az91OJdgY2FVZiDnqk2sxDg3Qbz l96ERrG8 tGT+Cpn0XPvM2BcljbL0yzUxuCDAF3MJRlYC6cLT7zl36v4GMUp69KunvD9PBcvwgcXuCE9lril2GFtnBVIsoY8W8B/d/xkXDJcs2B7UlZxe6SIXIzx8Pkw5HOOsI8xXbDc5mR+geRh7virq527sIQ7798/ywQO/IHE8aXMzkNonZJzf1RFq12m1fLpujIYMpMUje3gBOvK6KgeghJKZTRTxYQC255iXItUyrE24QRipmsE29l/qLlEYS37A0m6OeCQaiex/9qYxgl6AViFbDByNi/9kh2UyqbxrlcewK18FZ/oW1z2dTkZHtUjve6U/PGyHmcqFMu9KuCK3x3ojAgpvhELgNE14gIGXNHHFwxyBLUnJx+J/NNRQAbhwjLESF7IdD8Sh4rWnA3972UNP51Qb+wHWkCKeVuGtVGHs0FRqpl8/2hRaOrRbOcHwm/Bv4b6YLxhyYWBViOKgWaM3ww6ScM+sYh3hLqUpRICINpp5Iu2jnMj1kXwsfgBDHkgmfuoQhv9pw/LqJa61An5fJHHX9fn1gf8BVP4ykDOvAQPnvob11If02ibxs3lspMB2Iumiij0ENetjqe3fqpDGc78QlH/8Mr6Kjadn/rxdflaM+OT65YP6vFzx9fFQNajg1CEa1+DJQnn6q7L3kkyQaB3YdWiR3k86ftmGqGCr8la7w3rl/JKxr/w8t/w== 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 Track whether guest_memfd memory can be mapped within the inode, since it is property of the guest_memfd's memory contents. The guest_memfd PRIVATE memory attribute is not used for two reasons. First because it reflects the userspace expectation for that memory location, and therefore can be toggled by userspace. The second is, although each guest_memfd file has a 1:1 binding with a KVM instance, the plan is to allow multiple files per inode, e.g. to allow intra-host migration to a new KVM instance, without destroying guest_memfd. Signed-off-by: Ackerley Tng Co-developed-by: Vishal Annapurve Signed-off-by: Vishal Annapurve Co-developed-by: Fuad Tabba Signed-off-by: Fuad Tabba --- virt/kvm/guest_memfd.c | 56 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 5 deletions(-) diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 5d7fd1f708a6..4d3ba346c415 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -18,6 +18,17 @@ struct kvm_gmem { struct list_head entry; }; +struct kvm_gmem_inode_private { +#ifdef CONFIG_KVM_GMEM_MAPPABLE + struct xarray mappable_offsets; +#endif +}; + +static struct kvm_gmem_inode_private *kvm_gmem_private(struct inode *inode) +{ + return inode->i_mapping->i_private_data; +} + /** * folio_file_pfn - like folio_file_page, but return a pfn. * @folio: The folio which contains this index. @@ -307,8 +318,28 @@ static inline struct file *kvm_gmem_get_file(struct kvm_memory_slot *slot) return get_file_active(&slot->gmem.file); } +static void kvm_gmem_evict_inode(struct inode *inode) +{ + struct kvm_gmem_inode_private *private = kvm_gmem_private(inode); + +#ifdef CONFIG_KVM_GMEM_MAPPABLE + /* + * .free_inode can be called before private data is set up if there are + * issues during inode creation. + */ + if (private) + xa_destroy(&private->mappable_offsets); +#endif + + truncate_inode_pages_final(inode->i_mapping); + + kfree(private); + clear_inode(inode); +} + static const struct super_operations kvm_gmem_super_operations = { - .statfs = simple_statfs, + .statfs = simple_statfs, + .evict_inode = kvm_gmem_evict_inode, }; static int kvm_gmem_init_fs_context(struct fs_context *fc) @@ -435,6 +466,7 @@ 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 kvm_gmem_inode_private *private; struct inode *inode; int err; @@ -443,10 +475,19 @@ static struct inode *kvm_gmem_inode_make_secure_inode(const char *name, return inode; err = security_inode_init_security_anon(inode, &qname, NULL); - if (err) { - iput(inode); - return ERR_PTR(err); - } + if (err) + goto out; + + err = -ENOMEM; + private = kzalloc(sizeof(*private), GFP_KERNEL); + if (!private) + goto out; + +#ifdef CONFIG_KVM_GMEM_MAPPABLE + xa_init(&private->mappable_offsets); +#endif + + inode->i_mapping->i_private_data = private; inode->i_private = (void *)(unsigned long)flags; inode->i_op = &kvm_gmem_iops; @@ -459,6 +500,11 @@ static struct inode *kvm_gmem_inode_make_secure_inode(const char *name, WARN_ON_ONCE(!mapping_unevictable(inode->i_mapping)); return inode; + +out: + iput(inode); + + return ERR_PTR(err); } static struct file *kvm_gmem_inode_create_getfile(void *priv, loff_t size, From patchwork Thu Oct 10 08:59:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13829774 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 91C41CF07D9 for ; Thu, 10 Oct 2024 08:59:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 27EFA6B0092; Thu, 10 Oct 2024 04:59:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 231146B0093; Thu, 10 Oct 2024 04:59:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 081D26B0095; Thu, 10 Oct 2024 04:59:45 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id DD0A76B0092 for ; Thu, 10 Oct 2024 04:59:44 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 82F95141BEC for ; Thu, 10 Oct 2024 08:59:41 +0000 (UTC) X-FDA: 82657094688.13.511F67B Received: from mail-ej1-f74.google.com (mail-ej1-f74.google.com [209.85.218.74]) by imf21.hostedemail.com (Postfix) with ESMTP id 9EE3D1C0011 for ; Thu, 10 Oct 2024 08:59:40 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=35EOJcuG; spf=pass (imf21.hostedemail.com: domain of 3fJcHZwUKCAk2jkkjpxxpun.lxvurw36-vvt4jlt.x0p@flex--tabba.bounces.google.com designates 209.85.218.74 as permitted sender) smtp.mailfrom=3fJcHZwUKCAk2jkkjpxxpun.lxvurw36-vvt4jlt.x0p@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728550712; a=rsa-sha256; cv=none; b=Z+C+TKIknV2BkBZuV9kgLl16+9xeepz9eqjG0F5AnBf5SSZ42T5w2HcfotVy/lFzx8q2mN bOj2mKJgDBRfUImTOXb0ukI9ZKIGtb3bhIYVy1gx7VtJhkf7/St6D9vcTu8ToRsLii9g5m qmDvhLJuxEjdkg1Zzg3YH9c68a7AtEw= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=35EOJcuG; spf=pass (imf21.hostedemail.com: domain of 3fJcHZwUKCAk2jkkjpxxpun.lxvurw36-vvt4jlt.x0p@flex--tabba.bounces.google.com designates 209.85.218.74 as permitted sender) smtp.mailfrom=3fJcHZwUKCAk2jkkjpxxpun.lxvurw36-vvt4jlt.x0p@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728550712; 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=zi/pjPr4ta3drldcSk6p6o77eOYFs/2AcZb+H+YLVPU=; b=7bDGT7GmTC9xMCy+Mw9iaR5soMEMfm7UP+1YPzv1mWh8j0DaUHNdyBchAXLBIQYKuezsK8 cLqoeRLX3BM/16QY4jzvpRkeIJTaPMcYyw0PhOIXcUR5mSKkVAHhPY+B6rlV/lBSiXmIou xajyCU4pp+OlRtAUhm8fUGxw+XDw9D4= Received: by mail-ej1-f74.google.com with SMTP id a640c23a62f3a-a996c29edaaso50268266b.0 for ; Thu, 10 Oct 2024 01:59:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728550781; x=1729155581; 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=zi/pjPr4ta3drldcSk6p6o77eOYFs/2AcZb+H+YLVPU=; b=35EOJcuGccCKSy/FD/LbmqKNWAbqM7V0Df1vhK1iUNTwalOy/tB26olOqZLStf5jwj 6+OKiMDDbpwE5RMSoqlFvIOKdKYnAY7B0CRQrBvP4sLyxpGGucpmvCOHkUIDlycgFSf0 UXi1e7Y/XES/IskYeLLA8XbHBb2Y+/nNrltnhtO+g0WSU8J+yDzACGkTyHeBqksNI9Fu JA4lJ0L8HgQQ6Pj/9T7VxmEYLD+Is5zdAvDtnh7ffLXGmFSpPiuwcPL8WZBAxUtU0sFV VMp4AXHGi69KwmdG7iFDpIO18/YjqKbGc1XpOGjH5lnRgbtM0JW3o7xFNl3fLD0hjtWJ CG8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728550781; x=1729155581; 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=zi/pjPr4ta3drldcSk6p6o77eOYFs/2AcZb+H+YLVPU=; b=Ll0KE7pai36dvo9NfYkPsif7V+TRHTuqbF0/BGmCFawPk4Zt/TIiMQEmj7xByYpc4Z Xzgch4dIW7HBAgmxHM2g8AB0IKiZUldj80qL0mycetkX+T+bH5D1Yb1do70qrPeHlOs7 GbzkkYeorMjzr81F7w53yJy+2WJVWd39msmRtWPJAzBvJb4ruEAr7JD5i09fxBYiCU2m k4m8talbODXDa9zrUHyQVT5LnBk24z+bflZyG2d/sVldsKPG769s3HKlvHWolIuSOgYu RV41UQ6fDf+Y2K396rrSHB6iJA1T5QQ+VhHpV4t22Bmv+bjhdGZIyxQlNgOyHzPytgYs LEhQ== X-Forwarded-Encrypted: i=1; AJvYcCVPly8+sUvUU/fdZxWMKWFB+JAugDSvDxj1MXnBWV64Ll5bd2t8Ad8nsI+mXMzzlhB5i6yxxU6Mcg==@kvack.org X-Gm-Message-State: AOJu0YykgjliGL3dc+0C0DjaBnQOJTlPFerN4Ge/RjURZm08KUNLloZe HkCVJJMUCA4NT3sRbbb9iOuHenbTvb9Tat0r8iIOS6q+r7ntq1r4TgCb/Th3oKgEL+pRFMk8Tw= = X-Google-Smtp-Source: AGHT+IFJwsaDfsra9si5hSwL5TQTTo4RhmMq+iKHIyBjxFdz3SDsa44kizohADHJBxPg0EWk39rrAlUrlw== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a17:906:e207:b0:a89:ee2b:e2dd with SMTP id a640c23a62f3a-a998d31c253mr128966b.12.1728550780668; Thu, 10 Oct 2024 01:59:40 -0700 (PDT) Date: Thu, 10 Oct 2024 09:59:22 +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-4-tabba@google.com> Subject: [PATCH v3 03/11] KVM: guest_memfd: Introduce kvm_gmem_get_pfn_locked(), which retains the folio lock 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-Stat-Signature: h5oojk9s17d41684newtdyj1aepezh5u X-Rspamd-Queue-Id: 9EE3D1C0011 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1728550780-628750 X-HE-Meta: U2FsdGVkX18DDHprxg6zVtIWccw8d51bGry0V8QudzHmVr35gxtqocqsI0oJ+zbwVLNy5nWcfmyAjljI7sQPdPE1Vt3MdV3qgJ2tYRoeQpe6TSOSzXBEnPCQfkK7hbPaWvgE846d4QEiRYSQP5oDe/JdqUb619sWAX7ZSff9a302VHP+osl4fLdaWFqzkY8GTlFzkfqWaAIDPnt/pFtCqhXWTB6jwoNqU7goqDciB2oxJMZ0sFe19N8+M4EF5jNradIUoAUxtTndhYEQBuaoBEIBoEySssEPjltkR/96tIqCs3tSxiES4GtCxtrdms/wDeh4PF2o4GdyOUk0BxLs5BR47sFJ/FWSJ/u7oGmRMOLWAfmwMfLYBJSY8tnncwhiBRy+zydZuGf+kaB5MujTIpaPkvYVHaINf90VvfBApGvNc+PI43yoZt1aSOxiNLakq8NCQvGQLiRXm+366usqSFi815aJOTd7DDbJj3zyU8bITGcXo3WfKa+SRI8P521P/OEyb8h5A+ZT7qzcc1d79R4kytX1sU0I4VIWsYMUMRpuOqoow4/Ha+HMWcUG4BvUl1vWiWnSxUwEZyaGIqG0TDS/6nxgOmkVxh0TkDpTIDURY7evXFTQC3sH0b0o6uDrfdK7cFnvEuNJkGtXL3evgOmXTqPkTRDIb93koMWkz8xjhcM40G/wGaWoQelfQL4jEg/sSDZq4xfuJKvvFG/n3e0Iar85dh8gK1KWBUUgmE7cZfENV73kniDM+JAvLY2i1RtWuBXbOqB+U7u+czTkD7GOnID/dFYqtDr/BPIh+aWtjTYAkhjyXYpLkHsKZqZ19jIrh2vJ8H+EfYLnKmgvnskuADPoa9mv19KtfVgJe/9QPBZGKSfmNlY1m9YZEACmRWKEJEkH0bMKveXlWWtPXeAZiWRtNbFjArnwUp9W1ynPN/X/xJ8r7PaS07fPvm7a2y5Q6WvGjvL77wREoAo QHl4lZEz 2+sxWy15YkSJE4dgtgTA8zfUUR6e9oM69bQ4uP027TB1AF7k8mBNIrWOKGvOTLi5IodvgK/qQKp0cOoWPOOIrgosNOUYHfuUI5UaJp31jB0/VdMN8t/+XT1j3AN1Axc84KjJGWvebNXmxat/HReD78bvYfr2bISqV4Bax7gLOM1hGqzGiGGtNvNe11uockE0l+tAwUNbIcv+Bud5rMIxKtGd+wx4yGtfub6aTb290K1ly2Obu/nDZk118kCy9wwa6tts7rO+fHwjGRlr29OfFK96RRYUOOm0TnAmNDpP+x3f/din3J9gyRWKQTbKjM6VSslFxHOCo9I8ZJz3yIO9Zqu6w5w4nijuuyuzbH/HNKKcuwAsXuCuwP72Sqn6gnGY+eGvT25PAHzwUV1r3OEf7MCSLyxkLlA8s+BaqaMy+WWzl0AUVZZOzK7Ydr+eMXIx3+WlfKvb110yE7ESdgyHbS7SO5sbAjEr1nkvPSwPO1nEVonken+J3Ce87WnUtI7a34+2cdk8CBjlJJ2LNewFrc9YH8ei7uzsYeqr/fVADi+0psn+3N7NciPOtwx3KRE6CK9bAllUXxyFHk6IglHQqLmxh1OPz9rnx5hfBv6euQ6sSfYKhRNHRZs8MUNy0SXHRYHqPzYgb3ROPonsCx/r4xn+p+A== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000020, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Create a new variant of kvm_gmem_get_pfn(), which retains the folio lock if it returns successfully. This is needed in subsequent patches in order to protect against races when checking whether a folio can be mapped by the host. Signed-off-by: Fuad Tabba --- include/linux/kvm_host.h | 11 ++++++++++ virt/kvm/guest_memfd.c | 45 +++++++++++++++++++++++++++++++--------- 2 files changed, 46 insertions(+), 10 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index db567d26f7b9..acf85995b582 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2464,6 +2464,9 @@ static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn) #ifdef CONFIG_KVM_PRIVATE_MEM int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn, kvm_pfn_t *pfn, int *max_order); +int kvm_gmem_get_pfn_locked(struct kvm *kvm, struct kvm_memory_slot *slot, + gfn_t gfn, kvm_pfn_t *pfn, int *max_order); + #else static inline int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn, @@ -2472,6 +2475,14 @@ static inline int kvm_gmem_get_pfn(struct kvm *kvm, KVM_BUG_ON(1, kvm); return -EIO; } +static inline int kvm_gmem_get_pfn_locked(struct kvm *kvm, + struct kvm_memory_slot *slot, + gfn_t gfn, + kvm_pfn_t *pfn, int *max_order) +{ + KVM_BUG_ON(1, kvm); + return -EIO; +} #endif /* CONFIG_KVM_PRIVATE_MEM */ #ifdef CONFIG_HAVE_KVM_ARCH_GMEM_PREPARE diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 4d3ba346c415..f414646c475b 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -714,34 +714,59 @@ __kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot, return folio; } -int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, - gfn_t gfn, kvm_pfn_t *pfn, int *max_order) +static int +kvm_gmem_get_pfn_folio_locked(struct kvm *kvm, struct kvm_memory_slot *slot, + gfn_t gfn, kvm_pfn_t *pfn, int *max_order, + struct folio **folio) { struct file *file = kvm_gmem_get_file(slot); - struct folio *folio; bool is_prepared = false; int r = 0; if (!file) return -EFAULT; - folio = __kvm_gmem_get_pfn(file, slot, gfn, pfn, &is_prepared, max_order); - if (IS_ERR(folio)) { - r = PTR_ERR(folio); + *folio = __kvm_gmem_get_pfn(file, slot, gfn, pfn, &is_prepared, max_order); + if (IS_ERR(*folio)) { + r = PTR_ERR(*folio); goto out; } if (!is_prepared) - r = kvm_gmem_prepare_folio(kvm, slot, gfn, folio); + r = kvm_gmem_prepare_folio(kvm, slot, gfn, *folio); - folio_unlock(folio); - if (r < 0) - folio_put(folio); + if (r) { + folio_unlock(*folio); + folio_put(*folio); + } out: fput(file); return r; } + +int kvm_gmem_get_pfn_locked(struct kvm *kvm, struct kvm_memory_slot *slot, + gfn_t gfn, kvm_pfn_t *pfn, int *max_order) +{ + struct folio *folio; + + return kvm_gmem_get_pfn_folio_locked(kvm, slot, gfn, pfn, max_order, &folio); + +} +EXPORT_SYMBOL_GPL(kvm_gmem_get_pfn_locked); + +int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, + gfn_t gfn, kvm_pfn_t *pfn, int *max_order) +{ + struct folio *folio; + int r; + + r = kvm_gmem_get_pfn_folio_locked(kvm, slot, gfn, pfn, max_order, &folio); + if (!r) + folio_unlock(folio); + + return r; +} EXPORT_SYMBOL_GPL(kvm_gmem_get_pfn); #ifdef CONFIG_KVM_GENERIC_PRIVATE_MEM From patchwork Thu Oct 10 08:59:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13829775 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 A8C7ACF07DA for ; Thu, 10 Oct 2024 08:59:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EDB166B0095; Thu, 10 Oct 2024 04:59:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E8D5B6B0096; Thu, 10 Oct 2024 04:59:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BF3276B0098; Thu, 10 Oct 2024 04:59:46 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 8F0E56B0095 for ; Thu, 10 Oct 2024 04:59:46 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 55930120364 for ; Thu, 10 Oct 2024 08:59:43 +0000 (UTC) X-FDA: 82657094772.29.107DE02 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf20.hostedemail.com (Postfix) with ESMTP id 8DDC11C000B for ; Thu, 10 Oct 2024 08:59:43 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=qH5V1LzY; spf=pass (imf20.hostedemail.com: domain of 3f5cHZwUKCAw5mnnms00sxq.o0yxuz69-yyw7mow.03s@flex--tabba.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3f5cHZwUKCAw5mnnms00sxq.o0yxuz69-yyw7mow.03s@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728550648; 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=RaMAnTMU97KOopoBfYjbwx+ePz+tIxWqRe78kfl7LPI=; b=DLg/YtZPat4CuKMz1HHxXITZRTsfVqi9TyTcLQcyX1jxeCyvqGSA1SQlADItxuJdPzEsTA l/0gH7vlKnpgO3YyMHBcx7FO67ZSTLuiQlBod038tL2hLZzFlAm1BrtRIyIrFjvtPZykTD D5dCmNfzuPa2v5e7DPjFr14HHNEs2O4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728550648; a=rsa-sha256; cv=none; b=5SIrOu5rwodGR8BJr80Y5SeyEZWo5c63jogsxqWF75YUSre8mRQ3A7o/GP9zu21sg8x16t OwTHVyzDoeyCe+bJFOPENoItouqvjN5eL/scD5UX7PsaxXqAIXDLoRlc6Oa5d1vqIa61Mh eW4TGKnbQbistNH+9b8yUJliSUWUbic= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=qH5V1LzY; spf=pass (imf20.hostedemail.com: domain of 3f5cHZwUKCAw5mnnms00sxq.o0yxuz69-yyw7mow.03s@flex--tabba.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3f5cHZwUKCAw5mnnms00sxq.o0yxuz69-yyw7mow.03s@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e24b43799e9so647677276.2 for ; Thu, 10 Oct 2024 01:59:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728550783; x=1729155583; 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=RaMAnTMU97KOopoBfYjbwx+ePz+tIxWqRe78kfl7LPI=; b=qH5V1LzYJCwzzzddOwrvedr8Dm2566Al8FlTj96SDSTk3LPe2bLQ1/gIriV49nfLTk eyC2efdle20eTBDZFbQihWwj9wew2iRFK16tK5idw+BYHvL5GIKdej8l1yLA1cqZ81/z T1N3No1i/KsQIcSv7XE1xFhEZT6aO/3xdgr7vEC/ba/pkcHJFh6fCgUJQ08gaHWsbaNp 7iBctOMO2aspEtYgCfPgCkoPVVhM8bVd8iTq1xLNxnoSylwZuH+nkJRdRi3d2N1mIfZH uWjOpWOLfAgYqcENswGjxlg9QqLqYBVt0l1I96uljLokmCZ4+uVw+UGRGjZI6rbmOd6B utog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728550783; x=1729155583; 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=RaMAnTMU97KOopoBfYjbwx+ePz+tIxWqRe78kfl7LPI=; b=h5IU8Sm3aM6T1SN8XWVwATD/kfNa5dt2sQGuodxW1KFspB/mjuO9JjDCJoFMR9b3aW aciSkwIEGageWWAegsTx0zhMqwXulHdOIA/o8jB6k0H05KJO24aqoJ9qtbvRr1nrF9X4 h0pMbgL9PsnVSz5x8QNPUpr0qb2dmmDYskZ7bTo+FgIbioMzRWL5elxqmCGpUh26I5EV 8ejPdu6CqcekqneJEEr6Ig9ysKgokViMyrwOv9+GkHf5sVXNtA2rXEVMlopLuvTGbdQ0 75wiithXOV3oubP76MxD8iWjsHmuEISc49arJs/r0DI2n6T5iu7y45iVP93YF0VM05Ef Zlhw== X-Forwarded-Encrypted: i=1; AJvYcCXLqCAevldAh9oQH/JuSYMyn0aDBXi8MxJ0xlRVc+x0i/3gWJB4/IvTwdqXdVoAAM7CrijcbhdooQ==@kvack.org X-Gm-Message-State: AOJu0Yyz3sHEQAliSe0oE0IZtRsH2ZF2A5EDyMO98Ym54ItinXDZ22E6 dA5UoIC6t5x5M+NA5EQE4uADYuOMbNaf9wc2e18TGBs3UFDEN+qSLmDWUCRGi9gfiMqtjY6+Og= = X-Google-Smtp-Source: AGHT+IFXrnj8DsQckmU8dck3/aR7Cyrtk6dA8VC+TkvI6bTNlQ8hcSmPDes1mOxFCnXb7jNq/oQlri1uFw== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a5b:308:0:b0:e25:d46a:a6b6 with SMTP id 3f1490d57ef6-e28fe41d292mr38495276.8.1728550783417; Thu, 10 Oct 2024 01:59:43 -0700 (PDT) Date: Thu, 10 Oct 2024 09:59:23 +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-5-tabba@google.com> Subject: [PATCH v3 04/11] KVM: guest_memfd: Allow host to mmap guest_memfd() pages when shared 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-Queue-Id: 8DDC11C000B X-Stat-Signature: 363d3dhgtty6mgktkjosnhde63n4eock X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1728550783-675157 X-HE-Meta: U2FsdGVkX1+HGtNy7e+Yf1xz6oOZTaDRNmMgK94aEW8f06M/VZV5QmbuUzgF9i5zCnS25ub+t3vBhbV+hh/NZPvw0mqZ1qrO1WW9tIx4wAwHfWewC3vkiuGJWJcJuurQ2rgKkHDr9bp5BwWr/aO8JjqQTTIv+jjgxrU8ril9AQlOt+h8b77NJ4Q+FRA4cDBXsrQOidZmcQJ+fh5dnGaQFsobSIVdFQdWT4ccnxV9Z+rvMPXDr/2Iz0bLMNj52zE2i45gQGu7hYqeYxcHXB3waspLVf44W7AmaPO+g//yuTUdK3+fkaTkcD3N+hY/klu1WJDl2W97dKk65yNp8fiP6XreP3NwP/11Ltjg8UMmkuI/36/ACJDoE4xhukDZx+7170SH73M91XB68jUhUa2muqLlBGVWz2+jAGdFloekKvhoBnZhik/1a+rlQ0+Ro9cP6AEQizEqtkOyuf7Ki6tGfi4APZgdW+Jza8HzOr/la2ny99mADD4mpiOwm6yzMTl7eAaUnbpnUTmc6fl7i1H4k2UG2Dk5Km1AkTkQNE82relNW69S0OSHXYkwgLC92WhuV1V7ynA7sUUnSTCXgvFS1o/9NfH6QFrNh3hnBQepukhzRZ0T9cWFJ/KRDiSGsROcHhYY/shH7yxYDVqRHPCZPP+Fsq9fCuIWg43emaoHv1xXzQ8zjglK+373FZGT2h8pGLFXzMJyXhYByeFOYBfOBWvxUBwtDHBk5u8p18eUpvxwrC/KMOnXfG0eS9ECaJZW2V6FypV1+D5Rj3OSQZVga0lrSze0LveB0Xw0NBnTXI0Dk1Kuq66PdA6wzODuEFqm4Vp/EJrtqC+7mjDR1DK6YQ0Fm8r65cp6GWKjtXYi9Fad9B3M4iEbtYTbjKAxQkx1wwALJOyDX3vXO9/Gv+SHCrG24CXAdx1SBVkUJ/PHg0yra1s5gd2LVZZ1dNRggMuMwp+PGsnNmrjA/ZKJGnA uIWwA8gJ 44XL3e/r9psjA46vb6pmDaCWCMSupQlbnz0pLjM0mdJ8DNTncwCxb+1R4fmHwR2ZLxyDJpV4AmljIA/zjYwujxB/8G4BlGjPuScgp4oFD3uyjb6AyY7ArqOrcclTvo6ecof2GZ0Zt3mGnpU3bMd9lOo4eL6c03RmXTmbceygf/ccLu1U5d0DZoUqsmLfhsRhddjFC2HIcQfE9Fq9wX6SfyfJn5BBtxsv6hvqxS/DWWEHBVztSaia63Nqjhe+qOak+C/E7hj9qM0cYscZk9AP4/RBxtCWddtDNObwpNPfvh0r7wS9ORJJwAf1GQ+ywU4TocoMOKmPAZzibOHDrx6ixT88qxziFg91ZAaoLpW0SH5uN3JPc9rg4NNlDEfbG+RBiaoQ+rdYpt0dYcoi7EPvfdypDPBxqL5wluUuSWM79SbzCsb+Q93z8AhpeYwt/XDqxCxrqIP2hdiyhmhTMOt0zk26Jf/f3yi1eRdAy9ugXQqtIIGGGoYWlZCqFwj3jbOBKDfp+8kmqyecXfSQbSqGYK1s0RTXElbV0cVophfjajjJ2v/3cnB9AkjLS2namU+5v3NKqTWuPRbUK4STuhpQosk+L6CMIsD4VCO+LNYeywEXsA0avveKPSfanaQ08AK427XeeTLI8ggY5uAfpd/RRs59KbtJp1iOLLpyZzc7aQlB13LKYb8iPdekam9HzqQfNCqJWpFNN9DvfGSQfmWUizT8zkm+yFvm8ZhTmRwt942a33bpMZk+Iy8JRJTS2oe6PK/XpbBsl9SI4pFs= 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: Add support for mmap() and fault() for guest_memfd in the host. The ability to fault in a guest page is contingent on that page being shared with the host. The guest_memfd PRIVATE memory attribute is not used for two reasons. First because it reflects the userspace expectation for that memory location, and therefore can be toggled by userspace. The second is, although each guest_memfd file has a 1:1 binding with a KVM instance, the plan is to allow multiple files per inode, e.g. to allow intra-host migration to a new KVM instance, without destroying guest_memfd. The mapping is restricted to only memory explicitly shared with the host. KVM checks that the host doesn't have any mappings for private memory via the folio's refcount. To avoid races between paths that check mappability and paths that check whether the host has any mappings (via the refcount), the folio lock is held in while either check is being performed. This new feature is gated with a new configuration option, CONFIG_KVM_GMEM_MAPPABLE. Co-developed-by: Ackerley Tng Signed-off-by: Ackerley Tng Co-developed-by: Elliot Berman Signed-off-by: Elliot Berman Signed-off-by: Fuad Tabba --- Note that the functions kvm_gmem_is_mapped(), kvm_gmem_set_mappable(), and int kvm_gmem_clear_mappable() are not used in this patch series. They are intended to be used in future patches [*], which check and toggle mapability when the guest shares/unshares pages with the host. [*] https://android-kvm.googlesource.com/linux/+/refs/heads/tabba/guestmem-6.12-v3-pkvm --- include/linux/kvm_host.h | 52 +++++++++++ virt/kvm/Kconfig | 4 + virt/kvm/guest_memfd.c | 185 +++++++++++++++++++++++++++++++++++++++ virt/kvm/kvm_main.c | 138 +++++++++++++++++++++++++++++ 4 files changed, 379 insertions(+) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index acf85995b582..bda7fda9945e 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2527,4 +2527,56 @@ long kvm_arch_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu, struct kvm_pre_fault_memory *range); #endif +#ifdef CONFIG_KVM_GMEM_MAPPABLE +bool kvm_gmem_is_mappable(struct kvm *kvm, gfn_t gfn, gfn_t end); +bool kvm_gmem_is_mapped(struct kvm *kvm, gfn_t start, gfn_t end); +int kvm_gmem_set_mappable(struct kvm *kvm, gfn_t start, gfn_t end); +int kvm_gmem_clear_mappable(struct kvm *kvm, gfn_t start, gfn_t end); +int kvm_slot_gmem_set_mappable(struct kvm_memory_slot *slot, gfn_t start, + gfn_t end); +int kvm_slot_gmem_clear_mappable(struct kvm_memory_slot *slot, gfn_t start, + gfn_t end); +bool kvm_slot_gmem_is_mappable(struct kvm_memory_slot *slot, gfn_t gfn); +#else +static inline bool kvm_gmem_is_mappable(struct kvm *kvm, gfn_t gfn, gfn_t end) +{ + WARN_ON_ONCE(1); + return false; +} +static inline bool kvm_gmem_is_mapped(struct kvm *kvm, gfn_t start, gfn_t end) +{ + WARN_ON_ONCE(1); + return false; +} +static inline int kvm_gmem_set_mappable(struct kvm *kvm, gfn_t start, gfn_t end) +{ + WARN_ON_ONCE(1); + return -EINVAL; +} +static inline int kvm_gmem_clear_mappable(struct kvm *kvm, gfn_t start, + gfn_t end) +{ + WARN_ON_ONCE(1); + return -EINVAL; +} +static inline int kvm_slot_gmem_set_mappable(struct kvm_memory_slot *slot, + gfn_t start, gfn_t end) +{ + WARN_ON_ONCE(1); + return -EINVAL; +} +static inline int kvm_slot_gmem_clear_mappable(struct kvm_memory_slot *slot, + gfn_t start, gfn_t end) +{ + WARN_ON_ONCE(1); + return -EINVAL; +} +static inline bool kvm_slot_gmem_is_mappable(struct kvm_memory_slot *slot, + gfn_t gfn) +{ + WARN_ON_ONCE(1); + return false; +} +#endif /* CONFIG_KVM_GMEM_MAPPABLE */ + #endif diff --git a/virt/kvm/Kconfig b/virt/kvm/Kconfig index fd6a3010afa8..2cfcb0848e37 100644 --- a/virt/kvm/Kconfig +++ b/virt/kvm/Kconfig @@ -120,3 +120,7 @@ config HAVE_KVM_ARCH_GMEM_PREPARE config HAVE_KVM_ARCH_GMEM_INVALIDATE bool depends on KVM_PRIVATE_MEM + +config KVM_GMEM_MAPPABLE + select KVM_PRIVATE_MEM + bool diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index f414646c475b..df3a6f05a16e 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -370,7 +370,184 @@ static void kvm_gmem_init_mount(void) kvm_gmem_mnt->mnt_flags |= MNT_NOEXEC; } +#ifdef CONFIG_KVM_GMEM_MAPPABLE +static struct folio * +__kvm_gmem_get_pfn(struct file *file, struct kvm_memory_slot *slot, + gfn_t gfn, kvm_pfn_t *pfn, bool *is_prepared, + int *max_order); + +static int gmem_set_mappable(struct inode *inode, pgoff_t start, pgoff_t end) +{ + struct xarray *mappable_offsets = &kvm_gmem_private(inode)->mappable_offsets; + void *xval = xa_mk_value(true); + pgoff_t i; + bool r; + + filemap_invalidate_lock(inode->i_mapping); + for (i = start; i < end; i++) { + r = xa_err(xa_store(mappable_offsets, i, xval, GFP_KERNEL)); + if (r) + break; + } + filemap_invalidate_unlock(inode->i_mapping); + + return r; +} + +static int gmem_clear_mappable(struct inode *inode, pgoff_t start, pgoff_t end) +{ + struct xarray *mappable_offsets = &kvm_gmem_private(inode)->mappable_offsets; + pgoff_t i; + int r = 0; + + filemap_invalidate_lock(inode->i_mapping); + for (i = start; i < end; i++) { + struct folio *folio; + + /* + * Holds the folio lock until after checking its refcount, + * to avoid races with paths that fault in the folio. + */ + folio = kvm_gmem_get_folio(inode, i); + if (WARN_ON_ONCE(IS_ERR(folio))) + continue; + + /* + * Check that the host doesn't have any mappings on clearing + * the mappable flag, because clearing the flag implies that the + * memory will be unshared from the host. Therefore, to maintain + * the invariant that the host cannot access private memory, we + * need to check that it doesn't have any mappings to that + * memory before making it private. + * + * Two references are expected because of kvm_gmem_get_folio(). + */ + if (folio_ref_count(folio) > 2) + r = -EPERM; + else + xa_erase(mappable_offsets, i); + + folio_put(folio); + folio_unlock(folio); + + if (r) + break; + } + filemap_invalidate_unlock(inode->i_mapping); + + return r; +} + +static bool gmem_is_mappable(struct inode *inode, pgoff_t pgoff) +{ + struct xarray *mappable_offsets = &kvm_gmem_private(inode)->mappable_offsets; + bool r; + + filemap_invalidate_lock_shared(inode->i_mapping); + r = xa_find(mappable_offsets, &pgoff, pgoff, XA_PRESENT); + filemap_invalidate_unlock_shared(inode->i_mapping); + + return r; +} + +int kvm_slot_gmem_set_mappable(struct kvm_memory_slot *slot, gfn_t start, gfn_t end) +{ + struct inode *inode = file_inode(slot->gmem.file); + pgoff_t start_off = slot->gmem.pgoff + start - slot->base_gfn; + pgoff_t end_off = start_off + end - start; + + return gmem_set_mappable(inode, start_off, end_off); +} + +int kvm_slot_gmem_clear_mappable(struct kvm_memory_slot *slot, gfn_t start, gfn_t end) +{ + struct inode *inode = file_inode(slot->gmem.file); + pgoff_t start_off = slot->gmem.pgoff + start - slot->base_gfn; + pgoff_t end_off = start_off + end - start; + + return gmem_clear_mappable(inode, start_off, end_off); +} + +bool kvm_slot_gmem_is_mappable(struct kvm_memory_slot *slot, gfn_t gfn) +{ + struct inode *inode = file_inode(slot->gmem.file); + unsigned long pgoff = slot->gmem.pgoff + gfn - slot->base_gfn; + + return gmem_is_mappable(inode, pgoff); +} + +static vm_fault_t kvm_gmem_fault(struct vm_fault *vmf) +{ + struct inode *inode = file_inode(vmf->vma->vm_file); + struct folio *folio; + vm_fault_t ret = VM_FAULT_LOCKED; + + /* + * Holds the folio lock until after checking whether it can be faulted + * in, to avoid races with paths that change a folio's mappability. + */ + folio = kvm_gmem_get_folio(inode, vmf->pgoff); + if (!folio) + return VM_FAULT_SIGBUS; + + if (folio_test_hwpoison(folio)) { + ret = VM_FAULT_HWPOISON; + goto out; + } + + if (!gmem_is_mappable(inode, vmf->pgoff)) { + ret = VM_FAULT_SIGBUS; + goto out; + } + + if (!folio_test_uptodate(folio)) { + unsigned long nr_pages = folio_nr_pages(folio); + unsigned long i; + + for (i = 0; i < nr_pages; i++) + clear_highpage(folio_page(folio, i)); + + folio_mark_uptodate(folio); + } + + vmf->page = folio_file_page(folio, vmf->pgoff); +out: + if (ret != VM_FAULT_LOCKED) { + folio_put(folio); + folio_unlock(folio); + } + + return ret; +} + +static const struct vm_operations_struct kvm_gmem_vm_ops = { + .fault = kvm_gmem_fault, +}; + +static int kvm_gmem_mmap(struct file *file, struct vm_area_struct *vma) +{ + if ((vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) != + (VM_SHARED | VM_MAYSHARE)) { + return -EINVAL; + } + + file_accessed(file); + vm_flags_set(vma, VM_DONTDUMP); + vma->vm_ops = &kvm_gmem_vm_ops; + + return 0; +} +#else +static int gmem_set_mappable(struct inode *inode, pgoff_t start, pgoff_t end) +{ + WARN_ON_ONCE(1); + return -EINVAL; +} +#define kvm_gmem_mmap NULL +#endif /* CONFIG_KVM_GMEM_MAPPABLE */ + static struct file_operations kvm_gmem_fops = { + .mmap = kvm_gmem_mmap, .open = generic_file_open, .release = kvm_gmem_release, .fallocate = kvm_gmem_fallocate, @@ -557,6 +734,14 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags) goto err_gmem; } + if (IS_ENABLED(CONFIG_KVM_GMEM_MAPPABLE)) { + err = gmem_set_mappable(file_inode(file), 0, size >> PAGE_SHIFT); + if (err) { + fput(file); + goto err_gmem; + } + } + kvm_get_kvm(kvm); gmem->kvm = kvm; xa_init(&gmem->bindings); diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 05cbb2548d99..aed9cf2f1685 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3263,6 +3263,144 @@ static int next_segment(unsigned long len, int offset) return len; } +#ifdef CONFIG_KVM_GMEM_MAPPABLE +static bool __kvm_gmem_is_mappable(struct kvm *kvm, gfn_t start, gfn_t end) +{ + struct kvm_memslot_iter iter; + + lockdep_assert_held(&kvm->slots_lock); + + kvm_for_each_memslot_in_gfn_range(&iter, kvm_memslots(kvm), start, end) { + struct kvm_memory_slot *memslot = iter.slot; + gfn_t gfn_start, gfn_end, i; + + gfn_start = max(start, memslot->base_gfn); + gfn_end = min(end, memslot->base_gfn + memslot->npages); + if (WARN_ON_ONCE(gfn_start >= gfn_end)) + continue; + + for (i = gfn_start; i < gfn_end; i++) { + if (!kvm_slot_gmem_is_mappable(memslot, i)) + return false; + } + } + + return true; +} + +bool kvm_gmem_is_mappable(struct kvm *kvm, gfn_t start, gfn_t end) +{ + bool r; + + mutex_lock(&kvm->slots_lock); + r = __kvm_gmem_is_mappable(kvm, start, end); + mutex_unlock(&kvm->slots_lock); + + return r; +} + +static bool kvm_gmem_is_pfn_mapped(struct kvm *kvm, struct kvm_memory_slot *memslot, gfn_t gfn_idx) +{ + struct page *page; + bool is_mapped; + kvm_pfn_t pfn; + + /* + * Holds the folio lock until after checking its refcount, + * to avoid races with paths that fault in the folio. + */ + if (WARN_ON_ONCE(kvm_gmem_get_pfn_locked(kvm, memslot, gfn_idx, &pfn, NULL))) + return false; + + page = pfn_to_page(pfn); + + /* Two references are expected because of kvm_gmem_get_pfn_locked(). */ + is_mapped = page_ref_count(page) > 2; + + put_page(page); + unlock_page(page); + + return is_mapped; +} + +static bool __kvm_gmem_is_mapped(struct kvm *kvm, gfn_t start, gfn_t end) +{ + struct kvm_memslot_iter iter; + + lockdep_assert_held(&kvm->slots_lock); + + kvm_for_each_memslot_in_gfn_range(&iter, kvm_memslots(kvm), start, end) { + struct kvm_memory_slot *memslot = iter.slot; + gfn_t gfn_start, gfn_end, i; + + gfn_start = max(start, memslot->base_gfn); + gfn_end = min(end, memslot->base_gfn + memslot->npages); + if (WARN_ON_ONCE(gfn_start >= gfn_end)) + continue; + + for (i = gfn_start; i < gfn_end; i++) { + if (kvm_gmem_is_pfn_mapped(kvm, memslot, i)) + return true; + } + } + + return false; +} + +bool kvm_gmem_is_mapped(struct kvm *kvm, gfn_t start, gfn_t end) +{ + bool r; + + mutex_lock(&kvm->slots_lock); + r = __kvm_gmem_is_mapped(kvm, start, end); + mutex_unlock(&kvm->slots_lock); + + return r; +} + +static int kvm_gmem_toggle_mappable(struct kvm *kvm, gfn_t start, gfn_t end, + bool is_mappable) +{ + struct kvm_memslot_iter iter; + int r = 0; + + mutex_lock(&kvm->slots_lock); + + kvm_for_each_memslot_in_gfn_range(&iter, kvm_memslots(kvm), start, end) { + struct kvm_memory_slot *memslot = iter.slot; + gfn_t gfn_start, gfn_end; + + gfn_start = max(start, memslot->base_gfn); + gfn_end = min(end, memslot->base_gfn + memslot->npages); + if (WARN_ON_ONCE(start >= end)) + continue; + + if (is_mappable) + r = kvm_slot_gmem_set_mappable(memslot, gfn_start, gfn_end); + else + r = kvm_slot_gmem_clear_mappable(memslot, gfn_start, gfn_end); + + if (WARN_ON_ONCE(r)) + break; + } + + mutex_unlock(&kvm->slots_lock); + + return r; +} + +int kvm_gmem_set_mappable(struct kvm *kvm, gfn_t start, gfn_t end) +{ + return kvm_gmem_toggle_mappable(kvm, start, end, true); +} + +int kvm_gmem_clear_mappable(struct kvm *kvm, gfn_t start, gfn_t end) +{ + return kvm_gmem_toggle_mappable(kvm, start, end, false); +} + +#endif /* CONFIG_KVM_GMEM_MAPPABLE */ + /* Copy @len bytes from guest memory at '(@gfn * PAGE_SIZE) + @offset' to @data */ static int __kvm_read_guest_page(struct kvm_memory_slot *slot, gfn_t gfn, void *data, int offset, int len) From patchwork Thu Oct 10 08:59:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13829776 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 071FBCF07D5 for ; Thu, 10 Oct 2024 08:59:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 14A266B0096; Thu, 10 Oct 2024 04:59:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0CF9A6B0099; Thu, 10 Oct 2024 04:59:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E1A006B009A; Thu, 10 Oct 2024 04:59:48 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id BCF546B0096 for ; Thu, 10 Oct 2024 04:59:48 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 0D904C0B2A for ; Thu, 10 Oct 2024 08:59:45 +0000 (UTC) X-FDA: 82657094856.04.D79E63E Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf06.hostedemail.com (Postfix) with ESMTP id C40F2180012 for ; Thu, 10 Oct 2024 08:59:45 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=XO5AfexE; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf06.hostedemail.com: domain of 3gZcHZwUKCA47oppou22uzs.q20zw18B-00y9oqy.25u@flex--tabba.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3gZcHZwUKCA47oppou22uzs.q20zw18B-00y9oqy.25u@flex--tabba.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728550685; a=rsa-sha256; cv=none; b=tHVMUgWUMNwDRRXrqEnxqeXef3u9RHS8mVZtBYyzSa6U9t8jcDUPyv/4UcScW0WaXLFYZq yop1b/bG9gCczCRucaqZcDuAbIeURi7JvgFpkbZlVrnMNL1rWqHZ7Se1Zg85ixaRvw6+AK /rkiXsqySvtkELDWilc8ejFhRhpnu2g= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=XO5AfexE; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf06.hostedemail.com: domain of 3gZcHZwUKCA47oppou22uzs.q20zw18B-00y9oqy.25u@flex--tabba.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3gZcHZwUKCA47oppou22uzs.q20zw18B-00y9oqy.25u@flex--tabba.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728550685; 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=8uqb2Hq7+/kapW0F00vPZqAJ1bU0uvb9gYwuhhY1Sds=; b=O0rIFmZrE5ohjoG4Xq0lKscSefUON7qVAGu+K3GFDp3j+DAD8GadxNKyTesL/PxWbTJGFT i7xcHVb/xdEoIV/7ONQJGKN2OiLdLzEZmPg51G9gwvYHEmx51g/aQGHWhw+FQ8b+StgUIw W/LTW8zif2RvoP5VAyiwQbfFeUNU2zo= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6e21dcc7044so12980037b3.1 for ; Thu, 10 Oct 2024 01:59:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728550786; x=1729155586; 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=8uqb2Hq7+/kapW0F00vPZqAJ1bU0uvb9gYwuhhY1Sds=; b=XO5AfexE+0DQl49ceULnc8iBFiNWb1sbUerZNmgMDeKYd3SKw/+vd+8lxDX3Zd+lbu XUtmoUvuVLYn04UdclgpCM5xXY3gYj+HXsQWimX+fCkkT9HnREIG1laFrTEIvXP/B6DO NhoSkKNIJ9Rtz8nvsQX2m4f33TfEdZv77ZMpkgAgLOsQmWA6uiaLNAISK+zvDIKiQ5QA OwkvFn3XJ0UjafIlR0tPFtP1gbPrs9zlg4cLA6+Y2hzp/1gno01CRPXTz9KRYudK5Vf8 LXwTQtHVDYiwk3RYw9xms9g1aZVm2CEatR9Qd2jo2QEGM/Yqwla3AAkT4iimDEf0ZPHt J2wQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728550786; x=1729155586; 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=8uqb2Hq7+/kapW0F00vPZqAJ1bU0uvb9gYwuhhY1Sds=; b=vlJdJPTv7LraeEtlePxepZr8Iyjc8JzBSdqhO94P/+ut+cnSI74teq71XrxepspLW5 KSF+CTtjvhMNCesGisJWjMTJ68P7XUV44IWvUE4Jdsq/rrxeA0RZj/e+Eqi/kE/xK7le pM9oas/HWvcd5A/Fm4Vdz783wJGmdu5RQsX126cnDsElxfRJ+NTaobiQ0EDeCLRiVjZp Nyx7XdMYaFUr9cy018pZ2T/QfJvtduPa07W7AQ3SMHNGPgv1cte3gQzJhAApvCS4lnDh YoFaqBSLmD7iTRH+U4BHhDDOhpK5GlFhIvh1IruFBhityPfmkmHsq6jzYXyng4axv2Ah 4OtA== X-Forwarded-Encrypted: i=1; AJvYcCX/JUEpnJCXowyamOfCqqUYKO979yEy0POJXbZYQmfgVT0SNnvB31LbrboYcv/jC6RFunR0PJf4VA==@kvack.org X-Gm-Message-State: AOJu0Yy6wR02fuwBvR039ZVSqCzqWAZ6F3tkVpEcqQKQqzyRWQBny5Y6 A3o8MNcx2In4Gmv6xY3M6+YWo37Ae8UDILs3itlYpCnA1oon1Hj1JJkiNo2YHb9DMnVbzLo9lQ= = X-Google-Smtp-Source: AGHT+IG7SQRbRuMfYH/Rui+CBChrEOHfesWWRZDd/p/7xcfBTKh/y+cfP2FSghBgXkgmmW6X7QZVv1DCpQ== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a81:b545:0:b0:6e2:19f3:ff75 with SMTP id 00721157ae682-6e3221a89dbmr187767b3.6.1728550785757; Thu, 10 Oct 2024 01:59:45 -0700 (PDT) Date: Thu, 10 Oct 2024 09:59:24 +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-6-tabba@google.com> Subject: [PATCH v3 05/11] KVM: guest_memfd: Add guest_memfd support to kvm_(read|/write)_guest_page() 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-Queue-Id: C40F2180012 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: rc193uw9poh5u9nui4g16dkqd8kbexf9 X-HE-Tag: 1728550785-514522 X-HE-Meta: U2FsdGVkX1+BS0lqkZyYHQVzJJXTd2Ia0LmM0051XW1YIPupkV46O/MoUJ2Ue9Jktbt6tRlbbEG5n8ha1f/DNb3dkGiAa6yA5RuppmBY/GGdF7jCX7l/7OwrIdSXYsSuN/1lEMEjRC0QHXVjOKqtFegIdwI051nEV1WnfqDhAsV1gfqHXhoGxNrCJX44YqBAftFZPFAhjs/HkW7Z0zJrPlJ9Ax2KNQfct4LQ7FVMfYhPL9aHWJm0Wh+V11+NEDZ0RpqaBORAwcJa2hFsczTosRMuPREaclBelO1h+Fee7IDHFsz7e5dOAuTbHIt1JrxusMj6OJK9/Kcc/y1hqZG8mmPxsvAcha3rssjrb0uQTotO8k0/F3tBFEXbR2/yVOkEnzXOvbdmc5kPINbd9GhAcStSNj8uYitKVT0yMx0aM9YQUBSU517lugZfx0q4OOnt7AkLb4RmlcdNPfWl/PFaweIzNf3uymCHysG9ytji/fb2NdcYNcdpEGGvo6sdOli4aJNZv9aMnE4Q/fHeZiFl0Pm/a0c5UEXje53FZT1gGFDOf40fjzQmSBHeP/4zueyr5sGhqQ2xRfbbML9YK0DgUr0w7rvbLpMVAe8wzQ0uprQAWVhxB48SUCzX/nlgeEeQAQcFCyrQr3sKXR2fdiBUCG1Z5AHPAHTHyQpcQhYBFpSnJeqicDiJeLoUBx+QIInzJRYwS7SNOAYinWgoumrF79l4TrddlfyMmOop8u5pWUA+mTUlgBaQ4OhFqDh1AF6f2R1GktHFX/N4e7Qx4Kyz7npNFgxf2RYHuNHs0xuXoXa0wzRj5iOcG6D+XldMfIx9cMfO0ebcv+I78j9WZPDl+ZlFWw2ZVnCHbIFLotr+l6AvlxATlfuIjBhkouRaYLcpHh0hRbL312SDxakfl0eH01P79guD8u4MWrdkDwB+gxQbElz1a2JZoOdYFjOWqkGLLv1cW3l5xCROA+qVR7U 34K9tCvN /MEi4rjc76umGzbIGC/dFKdb4enXtedPmGnWrKoqpeqqEbjncOnTqL6D9nnoMvXhJV5XqM1ulPwdUWPQPrirhDcg6ZfcxNWLf4SQOUcZSi6T98vAasDIrKNpOmhHHGVfLixf5IDHTA77PaK+GGcvBcz2d3tSV6zfacmymQ5u8m0I+P+ooJPFTAVmn3J+OKL45N9lMAjR6PfbL+RxVOnc/G/KZ1Kw+sK3dB1rLtYUeX1fpHdeKkKAmH2cFTewNU8yYBDKMpux7jXugLByMUfQOBv5vvGNUjH2CMw2jLrJtErOMm1zPvpWDDiWv182JD1STsQTrH31VSAHv8zSCZdFPwcyAAmeFwdspW2OYMvNWkBWnoI4WlnkROLivHAmFgmIUokk8EvzXrWlb/5wwPjc94zq9EawmOpdfl8Wgf3lH4mbBTnhJXHvrg1iDBWa+8Ap8UXzS/msTZVLDr3kM6rmBYZ67kaGNp0njfd7JHrRWMOE1vSfsz5v8AxOkotEL/74HLYNfZexFU2EfQj8iBnfZnnoR4kXiwAsUCXFvw6yiJe69yVLNCHUlgtMAGNP+AkleDEBiYW2FDGfmn77ymeK+zoMryJr6gA9x/TAnUaPhsxu/HQjctQMWltyYzmaTArAV4O64LY0GOBrucU/9UM6rKC9R9g== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000012, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Make kvm_(read|/write)_guest_page() capable of accessing guest memory for slots that don't have a userspace address, but only if the memory is mappable, which also indicates that it is accessible by the host. Signed-off-by: Fuad Tabba --- virt/kvm/kvm_main.c | 137 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 118 insertions(+), 19 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index aed9cf2f1685..77e6412034b9 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3399,23 +3399,114 @@ int kvm_gmem_clear_mappable(struct kvm *kvm, gfn_t start, gfn_t end) return kvm_gmem_toggle_mappable(kvm, start, end, false); } +static int __kvm_read_guest_memfd_page(struct kvm *kvm, + struct kvm_memory_slot *slot, + gfn_t gfn, void *data, int offset, + int len) +{ + struct page *page; + u64 pfn; + int r; + + /* + * Holds the folio lock until after checking whether it can be faulted + * in, to avoid races with paths that change a folio's mappability. + */ + r = kvm_gmem_get_pfn_locked(kvm, slot, gfn, &pfn, NULL); + if (r) + return r; + + page = pfn_to_page(pfn); + + if (!kvm_gmem_is_mappable(kvm, gfn, gfn + 1)) { + r = -EPERM; + goto unlock; + } + memcpy(data, page_address(page) + offset, len); +unlock: + if (r) + put_page(page); + else + kvm_release_pfn_clean(pfn); + unlock_page(page); + + return r; +} + +static int __kvm_write_guest_memfd_page(struct kvm *kvm, + struct kvm_memory_slot *slot, + gfn_t gfn, const void *data, + int offset, int len) +{ + struct page *page; + u64 pfn; + int r; + + /* + * Holds the folio lock until after checking whether it can be faulted + * in, to avoid races with paths that change a folio's mappability. + */ + r = kvm_gmem_get_pfn_locked(kvm, slot, gfn, &pfn, NULL); + if (r) + return r; + + page = pfn_to_page(pfn); + + if (!kvm_gmem_is_mappable(kvm, gfn, gfn + 1)) { + r = -EPERM; + goto unlock; + } + memcpy(page_address(page) + offset, data, len); +unlock: + if (r) + put_page(page); + else + kvm_release_pfn_dirty(pfn); + unlock_page(page); + + return r; +} +#else +static int __kvm_read_guest_memfd_page(struct kvm *kvm, + struct kvm_memory_slot *slot, + gfn_t gfn, void *data, int offset, + int len) +{ + WARN_ON_ONCE(1); + return -EIO; +} + +static int __kvm_write_guest_memfd_page(struct kvm *kvm, + struct kvm_memory_slot *slot, + gfn_t gfn, const void *data, + int offset, int len) +{ + WARN_ON_ONCE(1); + return -EIO; +} #endif /* CONFIG_KVM_GMEM_MAPPABLE */ /* Copy @len bytes from guest memory at '(@gfn * PAGE_SIZE) + @offset' to @data */ -static int __kvm_read_guest_page(struct kvm_memory_slot *slot, gfn_t gfn, - void *data, int offset, int len) + +static int __kvm_read_guest_page(struct kvm *kvm, struct kvm_memory_slot *slot, + gfn_t gfn, void *data, int offset, int len) { - int r; unsigned long addr; if (WARN_ON_ONCE(offset + len > PAGE_SIZE)) return -EFAULT; + if (IS_ENABLED(CONFIG_KVM_GMEM_MAPPABLE) && + kvm_slot_can_be_private(slot) && + !slot->userspace_addr) { + return __kvm_read_guest_memfd_page(kvm, slot, gfn, data, + offset, len); + } + addr = gfn_to_hva_memslot_prot(slot, gfn, NULL); if (kvm_is_error_hva(addr)) return -EFAULT; - r = __copy_from_user(data, (void __user *)addr + offset, len); - if (r) + if (__copy_from_user(data, (void __user *)addr + offset, len)) return -EFAULT; return 0; } @@ -3425,7 +3516,7 @@ int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset, { struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn); - return __kvm_read_guest_page(slot, gfn, data, offset, len); + return __kvm_read_guest_page(kvm, slot, gfn, data, offset, len); } EXPORT_SYMBOL_GPL(kvm_read_guest_page); @@ -3434,7 +3525,7 @@ int kvm_vcpu_read_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, void *data, { struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn); - return __kvm_read_guest_page(slot, gfn, data, offset, len); + return __kvm_read_guest_page(vcpu->kvm, slot, gfn, data, offset, len); } EXPORT_SYMBOL_GPL(kvm_vcpu_read_guest_page); @@ -3511,22 +3602,30 @@ EXPORT_SYMBOL_GPL(kvm_vcpu_read_guest_atomic); /* Copy @len bytes from @data into guest memory at '(@gfn * PAGE_SIZE) + @offset' */ static int __kvm_write_guest_page(struct kvm *kvm, - struct kvm_memory_slot *memslot, gfn_t gfn, - const void *data, int offset, int len) + struct kvm_memory_slot *slot, gfn_t gfn, + const void *data, int offset, int len) { - int r; - unsigned long addr; - if (WARN_ON_ONCE(offset + len > PAGE_SIZE)) return -EFAULT; - addr = gfn_to_hva_memslot(memslot, gfn); - if (kvm_is_error_hva(addr)) - return -EFAULT; - r = __copy_to_user((void __user *)addr + offset, data, len); - if (r) - return -EFAULT; - mark_page_dirty_in_slot(kvm, memslot, gfn); + if (IS_ENABLED(CONFIG_KVM_GMEM_MAPPABLE) && + kvm_slot_can_be_private(slot) && + !slot->userspace_addr) { + int r = __kvm_write_guest_memfd_page(kvm, slot, gfn, data, + offset, len); + + if (r) + return r; + } else { + unsigned long addr = gfn_to_hva_memslot(slot, gfn); + + if (kvm_is_error_hva(addr)) + return -EFAULT; + if (__copy_to_user((void __user *)addr + offset, data, len)) + return -EFAULT; + } + + mark_page_dirty_in_slot(kvm, slot, gfn); return 0; } From patchwork Thu Oct 10 08:59:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13829777 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 B23FBCF07D9 for ; Thu, 10 Oct 2024 08:59:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3C69E6B009B; Thu, 10 Oct 2024 04:59:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 36A3A6B009A; Thu, 10 Oct 2024 04:59:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1EDD86B009B; Thu, 10 Oct 2024 04:59:51 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id EB0256B0099 for ; Thu, 10 Oct 2024 04:59:50 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 9F6EF121B7D for ; Thu, 10 Oct 2024 08:59:47 +0000 (UTC) X-FDA: 82657094940.26.C4DA275 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf20.hostedemail.com (Postfix) with ESMTP id DFDA31C000B for ; Thu, 10 Oct 2024 08:59:47 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=ZvxhfrkT; spf=pass (imf20.hostedemail.com: domain of 3g5cHZwUKCBA9qrrqw44w1u.s421y3AD-220Bqs0.47w@flex--tabba.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3g5cHZwUKCBA9qrrqw44w1u.s421y3AD-220Bqs0.47w@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728550719; 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=McR6hFD8xu5MydKKfp3q4/vtlv2OyOY0xakrmAL+qLs=; b=jSYlKTDHjhuCedVEZGPsAfyWCQORG3dCQoRteRPp7GrbCq4jBiUlo1tCx3p5NDo7LaIF9t fsR/0T26ydjgqCIe+vXIolnfjNXbMx5RSTnTbO3YRAbrzmf74skKnAEBwBz7V08d3VTijh 4D1BpyGs7j7AwFZPn5sSG5pNYBnIPrk= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=ZvxhfrkT; spf=pass (imf20.hostedemail.com: domain of 3g5cHZwUKCBA9qrrqw44w1u.s421y3AD-220Bqs0.47w@flex--tabba.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3g5cHZwUKCBA9qrrqw44w1u.s421y3AD-220Bqs0.47w@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728550719; a=rsa-sha256; cv=none; b=oDrrWKYh/Q2jW3HJpmpUr7z1Gw3I9CdkecgRUH2i6KYv7ZZT5h9yqUZKKUXaI4FPUjyB1Z arp/8qGlGC2sDwOZrwr8g89bxeAZSvJ42RkoF1pCvh1CwIn2cUSjIjQXqPUEkpq7PxC3RY WLvYPqW6OxIuELwQQTu2yajCx23zrg8= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6e2270a147aso13009107b3.0 for ; Thu, 10 Oct 2024 01:59:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728550788; x=1729155588; 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=McR6hFD8xu5MydKKfp3q4/vtlv2OyOY0xakrmAL+qLs=; b=ZvxhfrkTJKjwcISO+0JioIFO42C5LWKc9evekJ4h/SCDfcyMIvCvT4cB5EkQO4marP 7hUqZJkT72HocByX1rj1lHgGaa0qjchjJTUpdwvtcxFZt9r00pBoiEaxXSnThbvBm1Zf 5Di6I5k1MV7ThsOAzUCVrmRGM4kbVyOa4+OHgE43it0Zj9JnKpIW90rz4qr+vHLOMqXy kRmGp4X1Y3bNO747HPpMfGKaO69/vEBMq6Sh1OQing4P4iufRVdVYVzKDo/wEWSnIAY3 0DwfiAif9Ym0p+sCxFG41dQSl2p/R5M6yPgSRKmvIvhiMKn4m/EbNwQu9z7euwxr89T2 XGsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728550788; x=1729155588; 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=McR6hFD8xu5MydKKfp3q4/vtlv2OyOY0xakrmAL+qLs=; b=bJBpH/KArMKlrgCLPIuIn/FvXoDmY+lWaZdUpDq6wtacnsZfZYWMiLwwn5DIs0rjgb tVcHhNDEA4M0arEBaqz1tqUGmQW/iyhEHtMNz2di0pff1aaFDBCeF3H22nri5x2KaID/ 0ijYiccQrhBaWnQEZCMVZNeo+MzQ2t1LfpOpX5jYa02vS3w3ze8qzppRrNtCf4Q2ibTL DqhymahVAuAavz3PMpkL1RoVS/S1kNyiba+tiukfc3ZXV00PTc5+OGkIXK+/OHAtGR6Y 4BsRnlTKx44VbxPYzRYkYcz4bFJZ0WX6UUCnhTgPMoVdMhAl3uGpkAH1LoB5g8rTPNow W4qg== X-Forwarded-Encrypted: i=1; AJvYcCVQ+2RXMAl3xd07UA6AYiU091DVJZnZY0Jbn8A9nOSaslsFjV6ecbGAsT0z0ba6pv+n0wo61M34OQ==@kvack.org X-Gm-Message-State: AOJu0YzfLrn81cNNyETexRshfg3yTOhIXVoGTWkpV+67+JdhQn2I1uia /q7WXSmkmWK58kPf0B0D9PTH+RCVA8HgsaPM/dPpVfetD8lvR87LaWpKrDCGoSqoERng+F/iUg= = X-Google-Smtp-Source: AGHT+IFvDP0XuuJHxV5RnbWlC7X4GO/XpvIVrGemFE3icy6MnaUivOOGAIqQ7A9+OzK7/cvpWXHNkIHIjw== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a5b:e8e:0:b0:e25:e5b9:8114 with SMTP id 3f1490d57ef6-e28fe362769mr134823276.4.1728550787926; Thu, 10 Oct 2024 01:59:47 -0700 (PDT) Date: Thu, 10 Oct 2024 09:59:25 +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-7-tabba@google.com> Subject: [PATCH v3 06/11] KVM: guest_memfd: Add KVM capability to check if guest_memfd is host mappable 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: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: DFDA31C000B X-Stat-Signature: 8ybbrb8aspy9q9dfkr9paizoyhu8n71d X-HE-Tag: 1728550787-133838 X-HE-Meta: U2FsdGVkX1/jN71RYq6zhiOWhmd1Q9tGZ33nNnbhefzXQXkknG4HN8oAQSOU4uZTd/T2INAXThVJo6aMXG8s5yEZbC6GRYZbux9mAey+k0zohlAC+WffDD9plYvA90uA5+GgBtSPJUlMWRZuuEmHFPOaHOnBwFKFFYzOcZuXgpPIVoIKjl+blGQse3ccfg3CrKdJPjEmVCIjBNfF2v/w7Vk7a6ZyMZ+T0kwc0sT5yq+X76SR5jeCZr/ETPg+NNlyQU9CH0l3+5QlRHL78KYgMF5pxC/zq+zmj9fEjHp2AkrW1ofL3HhhOPWMhljGtQ8uG5qkfjY61LUhTGybbEPPS+jUVO53Mao935tFwDi3UkGOQkXnRBxgf/jCifMGhT/VE4F4sjT/D2dtWp7OpdsMYt962YsKf02iA04w1Np3CJ9OtBN62YCGp8sHkSVlGwjKiTXLP2Bk+WGBOpylO12oFOVbsCNEutqO7YCwFDkJ5reakxeodzPQnph0xd3KiB0A18ICC896VKgn4SDJKNxYdghWyl4tvFrzx3tJN8t/qRy3kBwH6ySN61MN7OasKRsoYze2uNxQiX6G0/iGAmRrQkrPnb/6w9t75LbWEEmxk/JDaa97mlRKLTbX/l21AJbDSDy3qUX+G0sS8xnKX9o0SxJel0ZGTa4Le3KlAjrR7JtnsLpkMkIWZnoBUmuNuZqRuFFVgGMkESMouPqMnDIuh7wNjRGDYN+VhQxW0SZCK2cLrb67uOrzuWWod0tID7zNzKYWV0jkEl6n81FUm9+YOzzsQ5M0jlskirqaKmmDuRMG/+UprsT7Q6rVxXK5Rsi+0k6oBsPNCctVCSmQjhpyWm7mhQS+cvAQbusiK9orZj/+QM2Gj4PawFcpvCyAM+ow2YElfSPe3VzX0G6edvyrhvzOPf+YsA1jlYyaCaO6EE8wWcHAMPNgPeSkQ30zHeMM7NonuEorMgA9aCE48Nk uQyv6C8i +k8vvhFUvtCUU6Q1X6knCr63t61ViFGw1+i8KeEptVLHDRoYy68rTe/CPt5B/y6rBDCYjluW5sauX3c9YwEvKC+3Nzk8YdouCN+nxC6SvVbtLqDEAex/VLe0aNzage15kyoQJxHpytrawQd9nGyVSlrK9iP78NixWU/yU3gSs6sgVagEgBs4icoNCAhoAmEcwVZWgUirxrrg6olVgr2XoQa3N++bp/Sph1j4pKZqYLTKRD36b/d7VKFV96rMbSmwKM12ia7Z9QbxempKvQqIXD6bv9MDj7YeZ+CEbKA8pN1YiJXrtqicyrXvJL8nkf0RLRM6bOrDWTKGQQDpkrF6Rv0371zaRw3DV7MpUmy7T2zfEGrfhTjPWw5tKB5G2/d8aoPE2EbeDChn1ZNtKoJEwUcpbQ09MNma23ccX288aQi6QpUU0e1qIAj0fMUn3nGzQHEz+Q5pUXne42BjOe5oWL6PLJriBw5GZ+2Z7GrYjspXxVBm+wdIyD/2EuoW671vkZjzIjh/zE/gtnzpgrEEYNPSKpMZ2dx4UsbQHY5dyqlLBkn8pm8WEsmvaRKlV0DsZhSA55OWIexBom2HYjAgfyTB/88kCW7Wj6eAs/Qz6sM15am1r7rXFgv0wlkAd7I3P764JR7L1Wx9/eh0jMfM2Cvu7cQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Add the KVM capability KVM_CAP_GUEST_MEMFD_MAPPABLE, which is true if mapping guest memory is supported by the host. Signed-off-by: Fuad Tabba --- include/uapi/linux/kvm.h | 1 + virt/kvm/kvm_main.c | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 637efc055145..2c6057bab71c 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -933,6 +933,7 @@ struct kvm_enable_cap { #define KVM_CAP_PRE_FAULT_MEMORY 236 #define KVM_CAP_X86_APIC_BUS_CYCLES_NS 237 #define KVM_CAP_X86_GUEST_MODE 238 +#define KVM_CAP_GUEST_MEMFD_MAPPABLE 239 struct kvm_irq_routing_irqchip { __u32 irqchip; diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 77e6412034b9..c2ff09197795 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -5176,6 +5176,10 @@ static int kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg) #ifdef CONFIG_KVM_PRIVATE_MEM case KVM_CAP_GUEST_MEMFD: return !kvm || kvm_arch_has_private_mem(kvm); +#endif +#ifdef CONFIG_KVM_GMEM_MAPPABLE + case KVM_CAP_GUEST_MEMFD_MAPPABLE: + return !kvm || kvm_arch_has_private_mem(kvm); #endif default: break; From patchwork Thu Oct 10 08:59:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13829778 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 6DB32CF07DB for ; Thu, 10 Oct 2024 08:59:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 036BC6B009A; Thu, 10 Oct 2024 04:59:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F29406B009C; Thu, 10 Oct 2024 04:59:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DA2496B009D; Thu, 10 Oct 2024 04:59:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id B281C6B009A for ; Thu, 10 Oct 2024 04:59:53 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 0A276AC2DD for ; Thu, 10 Oct 2024 08:59:47 +0000 (UTC) X-FDA: 82657095066.20.FA8FF50 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf13.hostedemail.com (Postfix) with ESMTP id 59DCF20002 for ; Thu, 10 Oct 2024 08:59:50 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=I+d9iPd+; spf=pass (imf13.hostedemail.com: domain of 3hpcHZwUKCBMCtuutz77z4x.v75416DG-553Etv3.7Az@flex--tabba.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3hpcHZwUKCBMCtuutz77z4x.v75416DG-553Etv3.7Az@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728550654; 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=zKaf8nivAag4/4uIL21ZmJPpCRWLdv9h6z/9SiSCBvk=; b=ZqDVUTJamxx2yntOSO/kuYMcF/efNcaHlYMLM16JVIjhrfcxVZODbbbzGETy3icfEIAhle OlkuVtc41HJ0ziQ7My4sCVbgnkVoJ8KHpLpjWiIemyUFSVkZn1ivI/6nJy3rNdZus4pfLy x+2P+30j7iPz/eXQ9S7xwpG4oysH0UI= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728550654; a=rsa-sha256; cv=none; b=EpIYDizCJEw3CjIpJ5ATUcFd1swE48aRij6b7kA6LfgqNLJdF2lhlL8WacTo56R0tFfZbN OOFDfacoYdOtZS1jSSS4zzkGAtiSzimT/rKC9Tl88asYgl291Hke+yJaEV0RXi1hlAErIV WY745mCLPQZeb0BS/L47M1DhObIOqDQ= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=I+d9iPd+; spf=pass (imf13.hostedemail.com: domain of 3hpcHZwUKCBMCtuutz77z4x.v75416DG-553Etv3.7Az@flex--tabba.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3hpcHZwUKCBMCtuutz77z4x.v75416DG-553Etv3.7Az@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e1159fb161fso1191775276.1 for ; Thu, 10 Oct 2024 01:59:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728550791; x=1729155591; 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=zKaf8nivAag4/4uIL21ZmJPpCRWLdv9h6z/9SiSCBvk=; b=I+d9iPd++IwTFGEcPKQ7NiCf6QbNbfcoHbxg54Z9tPO78EBNv4J4I9KlXS+NL7dTI8 sbrdH9PoLbVlZkRAmNULGp+Amae/42OtqeGHb5HTE+zQA9yMAsvRjjb4ij89eek0+M1V h7HO4qCny4a61DCyD5ADPRkOvHuGM5LDEWkUYxsnLVFL5jsjPu8yuq7JLcqYZsFmq6SH szOmryU6F27xuxe/JH1gzYkoeyHyS97tzQ4/DMUNBsHIKqjzdoCLQP3U2D1MrS2iLbh6 HTcU0OPC4dy/jNo1h2B5CbhB2tfGJ0qYi27KkB+gig9CmxxwAkF80hcOgDhgacJaRF7x Kw4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728550791; x=1729155591; 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=zKaf8nivAag4/4uIL21ZmJPpCRWLdv9h6z/9SiSCBvk=; b=okm9gfUy2ssB5PRLxuM6GEsrwbfdJkF7Phu5alqIyQ6ufOANs14E1TAuOazjy+W6r9 GqUToD/Ket5f5n1EK4O+WCtKSwjLuKzc0jXV7/5OVz9sQV/aSHoOIXMka1ero67kgVs0 lAEdaLEsocrqy60DgIU8IuOZ3awoyjmrzu6PziHz+Of8IrfdHs8Ok/9xxxJs3/x78GKN TppnahRGFYRlvDXr7cry1/nK1vJzKUYDW/NpqwwCezp525/jWzE9U/uWmIRjvmzjp1vM R9ROW1BOFasIXatPHWrUz8bVw9lki3jv67RL+STtDWoyivtsFA/GYow20EwhKcIQ2ERt 6qXw== X-Forwarded-Encrypted: i=1; AJvYcCWr2dC70altRoDp9V1CCYJaVzyy3vd6w5iAQliNbOGd5FvfJt1s1IFyK2DMX9rPmEirY+gVizdC3g==@kvack.org X-Gm-Message-State: AOJu0YzuidJZKtpg3malTKKakOXIgIRjnZDdT9n2ww22TJm7dAUVUxtx gVcsYuK7345Hv4bEaHD+qVi75NSTU+clfY8WS9sjT0kOVrUNdzNx2lPd2M+mkczfqJjt4/Fqig= = X-Google-Smtp-Source: AGHT+IFyezTuDqo1giJ/p5guQQYFu/xIk5SZEqJH7PMzqSsZJQW4O7K2L6rTa8b012uIruIWq11czsKArw== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a25:c2c1:0:b0:e22:5bdf:39c1 with SMTP id 3f1490d57ef6-e28fe45b504mr3520276.10.1728550790172; Thu, 10 Oct 2024 01:59:50 -0700 (PDT) Date: Thu, 10 Oct 2024 09:59:26 +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-8-tabba@google.com> Subject: [PATCH v3 07/11] KVM: guest_memfd: Add a guest_memfd() flag to initialize it as mappable 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-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 59DCF20002 X-Stat-Signature: m3qbhfctwztxcrca8gogt8q8d58ieqxy X-HE-Tag: 1728550790-55154 X-HE-Meta: U2FsdGVkX18g44h+HDQCQ3rV2QA1uPiOBc32eqUn6OJdZuo3V4YBBdooRri6Keyq9xycHrk8MW9UdJAPeil5RLAdsUprQzVWmkxhtL0WsDO+9e3sU8WuLEBlOhAOT1ZFBTiHhZFFyUTIrAq7OTYU9lR11UCbYVwyKIHZtr21V+nNTwtj/AchsgbFauRh3S6GLr/bQ3+DRU8+nvMRNMOvoNBX+iAI0Nwb8OMw+DzmFpC3sDymqI6yhIi2Qu/vRnxETCN+jcn3Zc2JPWfMcxY19X/e5AjDZJu94VAgJNEuYe4eqC0EH/3Hc76+n/ECcvGUvj973woHLL76Ou58ZY89Cf5wi9opm/qiwIU8ECkorzUGvsDMRECkHN9B+eNa92VW1FZXqJB7YSU0+ec7j3Oa0WvEKEfkO5VeRPf9lci6zkbvbanice3lZ1WPhk18FR94JB7pr1vNkt7cODlKaQzImPxE/Ei7mrbBW2yod+8jebIrGEXYeSzIptw9TEYJBm3BwFRc4HCxcOE6xEfFOrqzdi2a/BeQ2OCNpkUS10XzamlgwtoFvXOT9t1vfmPHj3mhxF2W5QfZWQlCVqlkm6hM0G2BXKG+KGgR5QuOX5vVL1tRjK98okIij3d4dVesKIG4LU51a6hHNnxf0F8uZ38Tdt/Jdp8iUFvTcaafh+GhwArNR0qpP3RG6NDNCfSUVx+mX7m49+2EtS3A7aiPOvXJeTvXlcCNyRunMYTyjPUXMR5x74WEFrs+ngRGuzkmzwjQKRZsusKv/G8c/jwOjbAtQcz7uJk1RbL8QFyJgBNQf9hxMNE3DEbc+WmwkA8z729f+X+UuPj9/Qqecrly2156BYauj36Ouqe3TOP17fIZ5R2LVXD7qMTxRMjjQ5owOmJqil9hqD3ef45O4hj0mTGu+4uAwLfjvgxME8Y6RyVtieBgElRrqmg99ZywW1YNSEvBtTtDsdbFUSDFuzfGYBU Dh5W/Z2V CwUZBhFxt7Lz4+Csl0rhC3Q2P8ZdsqE9qYu5OVRhVUAX9QOq2RDOuUnW0/81+WK9yEopOXTP51KzUSc2AbmWV+NiIlp+YESh37LSjyQyKU3SORaTACLfy/haxMOUseNmhCu0BLHyzWm7TQTy/N5r7B40foLmXR3p9dbubGGXEo65O1AeaJf32vH/64zDDyUrCy253j5TJqYGETTNGXL8T1UJWV1dofPHpFY8E3wjWnU26VF2X6gPb4KXEh/PxyFlwc7HdbBy1k6wKu+IOVlHwD9RNm0nf1IL0WNTyZDKMQYZsmOxaPvrUqpeUxLATFd83voWMIOYQxNU9MKG0EVCwXHXl4gMhWK6uey8nLira41wuoTs0fZJ6lhDZXJu22nv19Tnegw7lmuMbJsLFhVUW/znZj4OOVejBH2d+QisVylG4ZMHxBi+g0bxcSq7AF1eYpdVH/GcMfmrPweRFUR4l2oFuhtY51NCQqTzXa5c61OqxbKV1KFWGOZP7LwGDBlNMRAtltrcuj9hobRzURhPyZq8Vu5ltKIHjmq+SY9F7mwjhg86tJ1NiSyL2VIV50mwiUrmlAwRFc7KFTUNWvLcPynyUf18eEdNlGJpJxIl0gDVskwPwRY/630zr865zD9O6RO0CxJ0ZF+u6xZJrUoYSdwnpz6k07Qcns7SOlTY/4Bv7bM8dGumkZZZqYw== 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: Not all use cases require guest_memfd() to be mappable by the host when first created. Add a new flag, GUEST_MEMFD_FLAG_INIT_MAPPABLE, which when set on KVM_CREATE_GUEST_MEMFD initializes the memory as mappable by the host. Otherwise, memory is private until shared by the guest with the host. Signed-off-by: Fuad Tabba --- Documentation/virt/kvm/api.rst | 4 ++++ include/uapi/linux/kvm.h | 1 + virt/kvm/guest_memfd.c | 6 +++++- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index e32471977d0a..c503f9443335 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -6380,6 +6380,10 @@ 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). +If the capability KVM_CAP_GUEST_MEMFD_MAPPABLE is supported, then the flags +field supports GUEST_MEMFD_FLAG_INIT_MAPPABLE, which initializes the memory +as mappable by the host. + 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 2c6057bab71c..751f167d0f33 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1558,6 +1558,7 @@ struct kvm_memory_attributes { #define KVM_MEMORY_ATTRIBUTE_PRIVATE (1ULL << 3) #define KVM_CREATE_GUEST_MEMFD _IOWR(KVMIO, 0xd4, struct kvm_create_guest_memfd) +#define GUEST_MEMFD_FLAG_INIT_MAPPABLE BIT(0) struct kvm_create_guest_memfd { __u64 size; diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index df3a6f05a16e..9080fa29cd8c 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -734,7 +734,8 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags) goto err_gmem; } - if (IS_ENABLED(CONFIG_KVM_GMEM_MAPPABLE)) { + if (IS_ENABLED(CONFIG_KVM_GMEM_MAPPABLE) && + (flags & GUEST_MEMFD_FLAG_INIT_MAPPABLE)) { err = gmem_set_mappable(file_inode(file), 0, size >> PAGE_SHIFT); if (err) { fput(file); @@ -763,6 +764,9 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args) u64 flags = args->flags; u64 valid_flags = 0; + if (IS_ENABLED(CONFIG_KVM_GMEM_MAPPABLE)) + valid_flags |= GUEST_MEMFD_FLAG_INIT_MAPPABLE; + if (flags & ~valid_flags) return -EINVAL; From patchwork Thu Oct 10 08:59:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13829779 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 610B8CF07D5 for ; Thu, 10 Oct 2024 08:59:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 69F566B009E; Thu, 10 Oct 2024 04:59:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 650AE6B009F; Thu, 10 Oct 2024 04:59:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4539E6B00A0; Thu, 10 Oct 2024 04:59:57 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 24F3B6B009E for ; Thu, 10 Oct 2024 04:59:57 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id E789FA105D for ; Thu, 10 Oct 2024 08:59:50 +0000 (UTC) X-FDA: 82657095192.19.0608659 Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.73]) by imf16.hostedemail.com (Postfix) with ESMTP id 1DD7618000A for ; Thu, 10 Oct 2024 08:59:53 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=A1tWz6n8; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf16.hostedemail.com: domain of 3iZcHZwUKCBYFwxxw2AA270.yA8749GJ-886Hwy6.AD2@flex--tabba.bounces.google.com designates 209.85.221.73 as permitted sender) smtp.mailfrom=3iZcHZwUKCBYFwxxw2AA270.yA8749GJ-886Hwy6.AD2@flex--tabba.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728550644; 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=qzUThw8QDZNv9fDcMVRqMkWYYerRinZf/dn/Fj9e+Nk=; b=CQA8TPV/bYGYcAX+Je4DmXK45EzW9VholX9dOf/v8CGkkPDwlgmAPPJZUsWTZgqbNvUbFR TOXiZoozVPNYD1mZl8/awM6l4zUXCLO/M1nO0OY1ZqV3cS+XK6P4Z4bMsKVwo+GkIL6T0O fv08VqOepDt+yP8LiruUWitfHKt4Z+c= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728550644; a=rsa-sha256; cv=none; b=OqaTBDgEB4Lns17MrcBIzQzGDc20YfHeRRICxA5HUNosVyhr7ERVHGXTAAwje0fhldjkPf kIVUECEJIDT6IzqODjzwL8tavNeA2cPYP9Zc6g0qBhxrkd/OrY79SRINRCiUeWXGD3tvsE 8KFhLTqslK613Ogk6EloN3WhKoOApBw= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=A1tWz6n8; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf16.hostedemail.com: domain of 3iZcHZwUKCBYFwxxw2AA270.yA8749GJ-886Hwy6.AD2@flex--tabba.bounces.google.com designates 209.85.221.73 as permitted sender) smtp.mailfrom=3iZcHZwUKCBYFwxxw2AA270.yA8749GJ-886Hwy6.AD2@flex--tabba.bounces.google.com Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-37d462b64e3so217066f8f.3 for ; Thu, 10 Oct 2024 01:59:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728550793; x=1729155593; 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=qzUThw8QDZNv9fDcMVRqMkWYYerRinZf/dn/Fj9e+Nk=; b=A1tWz6n8YI4Ztqc95oJV/A5CMwiIIheqhi/O2smARel64cRY1QtE/jJseo6PGUdcwC WZA0ZK2EennpjLqCh8yteAQqlHfu78IS/VraCDnQGWQ7fnpnD9wK+BYBwZxldi8mr2HZ dTvn4o0kpxL4YU/XAfSzrTDbPdgQD3PGp8j1FBHE32Lcmxoybp26Vl5Engrz0dU3F2Ej CIv5DfM+IiVE6MDN754AXPaKadNpDB6HEraY+6G1VexX+zr1ka7B0qVQHPuSCU4lTbVb vOO60hsMps0qi5x2oA6nmWOgq0mlhlGdtiky45mMQ8eMF/4cBeYlWy6Lg9ZCn6+BT72m AbYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728550793; x=1729155593; 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=qzUThw8QDZNv9fDcMVRqMkWYYerRinZf/dn/Fj9e+Nk=; b=j/tzwBiGZHEWCsAUmQgea7+xlybA3SdqRaTCos2b4HgWE2i4YKU/S1lr+RN43AKwWS 8kZaJwcpUkXTIUND2WIxrlY++p4hYPZfPUUqlNd34HtoYlv1OJ0PRclHezf7XraM2XKD TECLluFx/MFX6K6/vmm98IVP69+tt3mMNQH6UCOTmHj/5EGrkoLSt/QXK5NuhWrl7sPJ IBRJ8iOBx1DACBK5KM/zXDp2RKgVYkTGup4be8DKpUUhGIpSVq8DHcLpjvuCepNhP2kB UUNbk0u0MT6LQsOeSOGMCa+SKqJKcZ1OgEr17MvIbxjxWfrGMK95uV7HBkyLiMZmuiNe XcnQ== X-Forwarded-Encrypted: i=1; AJvYcCWEpg2tnqemFoiIASuVQEEF8LpDiodp6gzSt/Cwvhsbj7wj+4tklw0iIZLrDgFYIr+avDTXDJs75A==@kvack.org X-Gm-Message-State: AOJu0YwHSE5/6yRBbInIia8AVLmE4T/WnAoE1o/Z5orS7fPp9Q4B/Bjf u/ex/to9FYZS8bf5HzV8980sGhNu9MSINxtjGN4a7t+8+UGIghgOW7G0FR6m7VtzZhKdSShtCA= = X-Google-Smtp-Source: AGHT+IGnEPVNJA5hNA6zgaReNf096hRtQziouC1E1cxYtRNCKe4dTpHEgBlCo1zAywzne6jSW5D++vxtyg== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a5d:420f:0:b0:37d:31a7:6610 with SMTP id ffacd0b85a97d-37d3a8ef27amr2808f8f.0.1728550793139; Thu, 10 Oct 2024 01:59:53 -0700 (PDT) Date: Thu, 10 Oct 2024 09:59:27 +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-9-tabba@google.com> Subject: [PATCH v3 08/11] KVM: guest_memfd: selftests: guest_memfd mmap() test when mapping is allowed 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: rspam07 X-Rspamd-Queue-Id: 1DD7618000A X-Stat-Signature: ydzqcakapznhkrsk9xcru9jif5wdh7mf X-Rspam-User: X-HE-Tag: 1728550793-701618 X-HE-Meta: U2FsdGVkX1/b0jdDOK58I062+7LZ3ch3i0NnNXBBEXpOt5rcIJb0BZvNdXBuywrU9Ui5xfc+9yRepelQQh8NQnt+VLJPptS5K8dQ+OlNDnkmmPSoEhhNntbaMMp+xymT/AM+e/SjrbhK+Y/AGQrVfyqXHj2Zma4T8L7SmPpSYQIWaugsfVPoit76WcC3rSCj9/cvksvthCk6KqKM+paJ4QmT7CEOrbttbaID7qy8g+FRvE2VbM1PzqFYnrD6Y3+1CEVycQw/+6bjfZzO03MFHqTA5tEMQgXbVm8j1L5/PA9JSDHBwl5IUFzFJ4vBOXEkKvuH/ulp66P3/J/jPim+cdjDhbZlIGK/o1hi+Iox4cIwQLPgvB0DBkCocuwX2IfHN+2PA6c1yos0zHMmr4S28puPSveQkpi9VZ4TbzaVxJTrI39pgWMfK0eHD5anImIulAxwqojsQuziKKQuD65Dk3m4Q8zxOOCkTu4OqITBkuJDUOKCL/AxTkqa95N0enZ/8QDzdpmjvB7tHZcACjjXhbvcEfzKUrdUSpYCeWcKUO/nNTEeYMpXRbRWKHZZhRyHboWoGn2LZpoVSCq/lcelJcW8hMUGKEiOpuPyeSZoryACsH2rty9Ci98foEBYzOGqAIFvSzYGNmJlZLkD5o2c/cEJmJScmv99s7vbUi/OKqccwFWX1l4tcfj2JHgx0qGbGgO1OkSW1sIzI28Vz/ohlBBX7Z3UPIPazaWOk0nWaCQm2jYRiQI7lVJyqSXZSTaDLlFVKW5B95QLR62z6TC0zPloz4dN+ljQaSmKPIxtto/7DGGcrRJ7jnLytMIwOg9ZLAOQvY2qnpQLpFCrzTZ9YV9GaSquM1PUMct4blmDxMLT0ZjbAwlGVQ6LqqJCKpk/m8+44G6FR8NWgygtLZXucUxh7z9V1dl6MwlfwQicbLrjSjXTPySsbDIj3FR8LbtO9k+jODw855k0oxj4UG3 hHpfhKVv BD6JUJXbWtOxairHLvpLS+hnQOYijLSuGg8dauW7C/nEHMGPMPW6TL9bY9UsQfOa+KMFHsC0NqgYGlXi0rijalFFbRy3lNCKzDwEOPm5x+lNK73WxcUUTXx3JwV7AkOgYwdbYCldjSrYYNLb3ieA3WGpt5Ft81t4PKRR5ENihMuvkUoMxb0tj/lEMzu3nXRSbXmCohxNx4EzbBcd8hLDrOqvIsUhDix9PhxIxGzgqJupPIWlMXSwu4v84GGqxBSP5gJdZpInhWoGBPAR1esUz5hSiqliaXSemF1XDmy7btWsXsSGgulCN4anK3BV7B0Tb0GKL34rmRiBh22xh71b+frSUxedk2AI29YPIhU66JKfqbzarYxNx1xaRS/QVjkSQLJSpyV5iy/1nTYpMgqOARTnG47T3pTJ4I8onYNS8c5bNPbrZEqQJLRXgyyL2TktXs6VzsfRTdKGunjDUzU4aSeVQ1x3BMChy5Gyjdn2f54trdhG7aA/JaIiNw13hYQvMgZQWb8TGumms1o//GXOX2nkGO3gevouczElE/HfX6MjmxYvbxRxbHpOiYD8kxIFd885kpxBGFnWb3nBgfBgLX6eatkS0n1Qr9G0wbbBIjCtJM/B8H3sOzPFSTuKarZyhfbcfkmLbAUpFUlsDRzrmHvFMWNKahFDVvFPKPvvIMiNAHcKKJPsDkpGL0o2xwoHb5HaJ 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: Expand the guest_memfd selftests to include testing mapping guest memory if the capability is supported, and that still checks that memory is not mappable if the capability isn't supported. Also, build the guest_memfd selftest for aarch64. Signed-off-by: Fuad Tabba --- tools/testing/selftests/kvm/Makefile | 1 + .../testing/selftests/kvm/guest_memfd_test.c | 57 +++++++++++++++++-- 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index 960cf6a77198..c4937b6c2a97 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -172,6 +172,7 @@ TEST_GEN_PROGS_aarch64 += coalesced_io_test TEST_GEN_PROGS_aarch64 += demand_paging_test TEST_GEN_PROGS_aarch64 += dirty_log_test TEST_GEN_PROGS_aarch64 += dirty_log_perf_test +TEST_GEN_PROGS_aarch64 += guest_memfd_test TEST_GEN_PROGS_aarch64 += guest_print_test TEST_GEN_PROGS_aarch64 += get-reg-list TEST_GEN_PROGS_aarch64 += kvm_create_max_vcpus diff --git a/tools/testing/selftests/kvm/guest_memfd_test.c b/tools/testing/selftests/kvm/guest_memfd_test.c index ba0c8e996035..ae64027d5bd8 100644 --- a/tools/testing/selftests/kvm/guest_memfd_test.c +++ b/tools/testing/selftests/kvm/guest_memfd_test.c @@ -34,12 +34,55 @@ static void test_file_read_write(int fd) "pwrite on a guest_mem fd should fail"); } -static void test_mmap(int fd, size_t page_size) +static void test_mmap_allowed(int fd, size_t total_size) { + size_t page_size = getpagesize(); + char *mem; + int ret; + int i; + + mem = mmap(NULL, total_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + TEST_ASSERT(mem != MAP_FAILED, "mmaping() guest memory should pass."); + + memset(mem, 0xaa, total_size); + for (i = 0; i < total_size; i++) + TEST_ASSERT_EQ(mem[i], 0xaa); + + ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, 0, + page_size); + TEST_ASSERT(!ret, "fallocate the first page should succeed"); + + for (i = 0; i < page_size; i++) + TEST_ASSERT_EQ(mem[i], 0x00); + for (; i < total_size; i++) + TEST_ASSERT_EQ(mem[i], 0xaa); + + memset(mem, 0xaa, total_size); + for (i = 0; i < total_size; i++) + TEST_ASSERT_EQ(mem[i], 0xaa); + + ret = munmap(mem, total_size); + TEST_ASSERT(!ret, "munmap should succeed"); +} + +static void test_mmap_denied(int fd, size_t total_size) +{ + size_t page_size = getpagesize(); char *mem; mem = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); TEST_ASSERT_EQ(mem, MAP_FAILED); + + mem = mmap(NULL, total_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + TEST_ASSERT_EQ(mem, MAP_FAILED); +} + +static void test_mmap(int fd, size_t total_size) +{ + if (kvm_has_cap(KVM_CAP_GUEST_MEMFD_MAPPABLE)) + test_mmap_allowed(fd, total_size); + else + test_mmap_denied(fd, total_size); } static void test_file_size(int fd, size_t page_size, size_t total_size) @@ -172,13 +215,17 @@ static void test_create_guest_memfd_multiple(struct kvm_vm *vm) int main(int argc, char *argv[]) { - size_t page_size; + uint64_t flags = 0; + struct kvm_vm *vm; size_t total_size; + size_t page_size; int fd; - struct kvm_vm *vm; TEST_REQUIRE(kvm_has_cap(KVM_CAP_GUEST_MEMFD)); + if (kvm_has_cap(KVM_CAP_GUEST_MEMFD_MAPPABLE)) + flags |= GUEST_MEMFD_FLAG_INIT_MAPPABLE; + page_size = getpagesize(); total_size = page_size * 4; @@ -187,10 +234,10 @@ int main(int argc, char *argv[]) test_create_guest_memfd_invalid(vm); test_create_guest_memfd_multiple(vm); - fd = vm_create_guest_memfd(vm, total_size, 0); + fd = vm_create_guest_memfd(vm, total_size, flags); test_file_read_write(fd); - test_mmap(fd, page_size); + test_mmap(fd, total_size); test_file_size(fd, page_size, total_size); test_fallocate(fd, page_size, total_size); test_invalid_punch_hole(fd, page_size, total_size); From patchwork Thu Oct 10 08:59:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13829805 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 0CBADCF07D9 for ; Thu, 10 Oct 2024 09:00:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B0B7C6B009F; Thu, 10 Oct 2024 04:59:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ABDB66B00A0; Thu, 10 Oct 2024 04:59:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 90F2D6B00A1; Thu, 10 Oct 2024 04:59:59 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 7185F6B009F for ; Thu, 10 Oct 2024 04:59:59 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 55ED841010 for ; Thu, 10 Oct 2024 08:59:56 +0000 (UTC) X-FDA: 82657095276.27.CCF57DA Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf17.hostedemail.com (Postfix) with ESMTP id C5D3B40004 for ; Thu, 10 Oct 2024 08:59:55 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=HBYW68yD; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf17.hostedemail.com: domain of 3i5cHZwUKCBgHyzzy4CC492.0CA96BIL-AA8Jy08.CF4@flex--tabba.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3i5cHZwUKCBgHyzzy4CC492.0CA96BIL-AA8Jy08.CF4@flex--tabba.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728550769; a=rsa-sha256; cv=none; b=fGaL8t/k12fz0TLz93Qj7f+enkpuil/gjaWOdAcxwJT+SOMBic31ETDo1jHCLv4+CgQpif sLR0VBc/bvsQ2bv/pZo7rJNa2hfyqp/NeywUN9jWWq8rQdwlPZRa6ctTW1RyyHLFlzDoe6 e18zorZ28zGUj/98a5U4edhmC4w71Ig= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=HBYW68yD; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf17.hostedemail.com: domain of 3i5cHZwUKCBgHyzzy4CC492.0CA96BIL-AA8Jy08.CF4@flex--tabba.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3i5cHZwUKCBgHyzzy4CC492.0CA96BIL-AA8Jy08.CF4@flex--tabba.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728550769; 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=HM7gsmExGCXNNGDXzdybypu9GrQXN1fbuf2tdqrKB/Q=; b=sBDDVIOKF63n7aNdltRTSs/IWX8q0Xr8q8AgOiqOKdGbdbW99TiQpTHdcdwcAHExU/3aIp uMu/iWRlM6Ip7xGUb4qBFiTJpS9uimY7Q8T2TzCRKGwyLZdqM4WZ89rpmlU4BJsMOlg8jd pTVfiBkd5esi8QG196IRSEsQKziCy58= Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e24a31ad88aso807764276.1 for ; Thu, 10 Oct 2024 01:59:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728550796; x=1729155596; 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=HM7gsmExGCXNNGDXzdybypu9GrQXN1fbuf2tdqrKB/Q=; b=HBYW68yDcOaLUQ/tlEBYS3lsTAX/BqUsEQ8NmkfGZ4JhRWQuKmlyW0j7KBXcA6Uf6F Flj5/my/3HvqoUTRuBnEcpkWVzasRe2UAdNhG2cOdK3F3IxdRbFiHpwp1UxKSFmNKxhE hx6lbjS7uFeeQs00E4UiPAz69lLhrngfu05WqtjVzSSKavORpYHvLB+EPXMj4HUyPeYY eYFISUtITCWbhyVjDZmsngNcad6sZJps08qIXPHHYiSY/bg0RTP3fM7PIxU2TZ1QP/ll 37JlyfhLonVigZ6Ekbm6OfSAVRCeS3X41ZvgsPT27EJYu0rG8FADO3jiKA475v7UtWtE nCkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728550796; x=1729155596; 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=HM7gsmExGCXNNGDXzdybypu9GrQXN1fbuf2tdqrKB/Q=; b=dFbkYL0OMnDvN3X87jvZTpBlEeaqpcNKUooWwa4nPphThupEfLqMEfn29uClhrW4md JSwLXYmLw8frakd5iYZUCaneMetIWWWNaaSwkrwzRYn0CRB7wL4LGfEbKV05mYk3g7wJ QynCtQNY3gbl4UKLkdkWszPeSP0FP/abAE7rH0+KKe0DvOJ6IypeHF2suwpnyuTRTky2 BXyeEievKtPeQHhalaiOZ+A80SJ/BltUPvJ3LyHpl+H9rycHizPicji9ASTVWJiYSY70 kiDznv271/5fhI5zKUNTaAvWp78qrgMTde7r/UjE0oYaUTfXZp5MVw2lWpnLDCOouxRY h8VQ== X-Forwarded-Encrypted: i=1; AJvYcCVk0NWO0BmzLK+u3S3xVl4G5c3m9QnOo+eySw6HmcewY3hFBdzSguCBSb0KT4kz1ZUJ6XA+dAAVSA==@kvack.org X-Gm-Message-State: AOJu0Yx4WPphOyzHhpX+zbg+ZHXd6PRbS8VrYaZLJlzHdejwA4IFQz0I DBJCqqswrGYQLNTCIqoxNt/AzhpKQgCZi1Z6xMaPt35jSAo0dsVweB8/7ObfcBTOfMgpwU+3BA= = X-Google-Smtp-Source: AGHT+IGFCUFcocuEb3ihc+miy+ZJ2j+wkfEPTs51cWpXQxawruWfXPnQR4cYRFEkkX27O5wF5IDqGcOMOg== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a25:b205:0:b0:e27:3e6a:345 with SMTP id 3f1490d57ef6-e28fe6935c6mr3791276.10.1728550795821; Thu, 10 Oct 2024 01:59:55 -0700 (PDT) Date: Thu, 10 Oct 2024 09:59:28 +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-10-tabba@google.com> Subject: [PATCH v3 09/11] KVM: arm64: Skip VMA checks for slots without userspace address 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-Rspam-User: X-Stat-Signature: dtpks9jfuq3dng6h1wd511h9swuhgi4e X-Rspamd-Queue-Id: C5D3B40004 X-Rspamd-Server: rspam02 X-HE-Tag: 1728550795-686915 X-HE-Meta: U2FsdGVkX1+06rzpyIEMm/CDR/c+U1iSikqCB2yp+NoGc4MSXScVWDL3mw55V6utQn+gWoyZHGDNm3+ZP4nLD2PY8kokCF4ECVwdivFaTdlUt07nZyvevkjjMdtVf3bEsBTcQp3Zy0/UO6Po2zrgMyDMGkxvQ7gsNYKiAi1RV9WGKBm5vW5AGooYwmqeJae2rX+PFd071Jl0NYgSrSWoUYBmBbJIAV+2oDzFnj/vZK5ZMg1RsJmGs2i/K628zS3nVkmpLT5aUKgQxJuY1AAUMeS4VlabC1q41GiUg/CkbRoOv/Xx6g3jfMt6Cach5ax2fxvpz6Z6CcVTKCXxl2se+jWFzfhonOTmqKEztEVd1p9DjLFID2+qPN7ipWL3VGD6XyFniJjLryPaQko+vlacIwYHkTdY0zuGkU333irCc/MhgSEUelUTC0MmuqRltLpopVGIAwsZRtCzOTNb+vrY74/vvUGGuQaxB9EpDZUVP9l160rRDYQw/kkxOp2vxZttu/nHNSIHdS0WvJRSO/+GEVMcQP3SLT7Yzcx4vAj2LNhRN4PisLztQynrkXT8X9LjKL6FyySHkY8di0Y+lzntqxAAStGO9/1OwfaJZK8jyW7e0GkPyn8FMnsmZbfYUfhexbk04nQTBeWStg+gwniToYgdK46UJnR77q5jNZg2LUi68idsmwCwKnaHAt6cG3Lwssy9scNVGCQ+4QGpaZ90QW9bDiguV6N8aeBxyO7MWpyECU7BYso3vq7DB5gEOGY2DuX23mJyrrxNlogwNCAWm8p7mACvwkfkeCgGYthmCbHop+29LNEn77JGSktbz+U9vDuaIPsKS6ukgcqL+zenzmRR2Mamk+bB2F4BqDVOgO/fwk0sH33YLVyVnpXRAa/WVYQwkBVqGAqn3OLeiT+sw+ETTtxFMhyv9yk++7Dt21OWerOpbXfLurnT1wN71ZCGg3rJN/lAnM9jyUJNUh1 voGQevJc S7xbcFAHoRuxc3ZeMAco4EHsBE7h9FfZluMxTjQ5mRm9fuZb2vnvfEZQOGOJdCoDXusrHeDxf4J87WpXgKacbDHTmUUaJbeX4IxLMdHNL5CM9XcG1zwrFL7jmnDejThz5ccJnYFxOJ8Y8PFYAi0vxAhWZ3AtNxsIrlBA+qOBpL1p9/ah18ndtmWHJSm6vgxZk4smsipVZ88aCkpZJdGJcvXpj4mZh7ICH5BmxnrhWrw3idjNwi7dgyaojHr4r3QNuXUH6nLtltCv5Et8K5Jr2fxJKy6KIyTw05BVJ1E34nbV7CeevWk4L19B8/HhKYQsKU7d++qqnN2s88h4d2L4HL+I9XgclmSvEz3M1M7j+fQS5DpkLa3pTJMf/bI5jkI+FPvDkTV25oMGeYaKWekoI6NWeD1GVs4dVIgJC5y2MBKct+lnY0EkoxTurU55I4ijuURuI1LX/5RBLD/+pLk4oPmeS5vVlM9wh+dOIET1aC80Y5nu+IQZs5DKTfDZcueHwyNK3sKBdaIPOUJ7scsxuPjuhevguNdCFpr1X/UdbpJ1IdCkXdDoRpuFMdEaz2MX7wOHvl6LWrtN+Gw5bzXw8NG4dgYYrwn883206 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000006, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Memory slots backed by guest memory might be created with no intention of being mapped by the host. These are recognized by not having a userspace address in the memory slot. VMA checks are neither possible nor necessary for this kind of slot, so skip them. Signed-off-by: Fuad Tabba --- arch/arm64/kvm/mmu.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index a509b63bd4dd..71ceea661701 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -987,6 +987,10 @@ static void stage2_unmap_memslot(struct kvm *kvm, phys_addr_t size = PAGE_SIZE * memslot->npages; hva_t reg_end = hva + size; + /* Host will not map this private memory without a userspace address. */ + if (kvm_slot_can_be_private(memslot) && !hva) + return; + /* * A memory region could potentially cover multiple VMAs, and any holes * between them, so iterate over all of them to find out if we should @@ -2126,6 +2130,10 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm, hva = new->userspace_addr; reg_end = hva + (new->npages << PAGE_SHIFT); + /* Host will not map this private memory without a userspace address. */ + if ((kvm_slot_can_be_private(new)) && !hva) + return 0; + mmap_read_lock(current->mm); /* * A memory region could potentially cover multiple VMAs, and any holes From patchwork Thu Oct 10 08:59:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13829806 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 5E5E4CF07DA for ; Thu, 10 Oct 2024 09:00:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1BD256B00A1; Thu, 10 Oct 2024 05:00:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 16C836B00A2; Thu, 10 Oct 2024 05:00:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ED9C26B00A3; Thu, 10 Oct 2024 05:00:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id CE64D6B00A1 for ; Thu, 10 Oct 2024 05:00:01 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id A7900A16A3 for ; Thu, 10 Oct 2024 08:59:55 +0000 (UTC) X-FDA: 82657095402.19.902EE45 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) by imf29.hostedemail.com (Postfix) with ESMTP id 7EF4712000F for ; Thu, 10 Oct 2024 08:59:58 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=UQfMrjIv; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf29.hostedemail.com: domain of 3jpcHZwUKCBsK12217FF7C5.3FDC9ELO-DDBM13B.FI7@flex--tabba.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3jpcHZwUKCBsK12217FF7C5.3FDC9ELO-DDBM13B.FI7@flex--tabba.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728550688; a=rsa-sha256; cv=none; b=D6GgsAkwdi7XdCMQhklsaDwN+en6Whs+zeYYFr5UIevWOWjmzXENgsZKrY1K6L/RIpqMRj iP2vYAbvaWvRRAQ7n9epp0rsJCNhlB1tkTmOJ7KALzjJsCOm5R9p1pP1hagCTS7FBvSbAd ypWsgBb0t6i8g+T8fa0/iR0mbEke/dk= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=UQfMrjIv; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf29.hostedemail.com: domain of 3jpcHZwUKCBsK12217FF7C5.3FDC9ELO-DDBM13B.FI7@flex--tabba.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3jpcHZwUKCBsK12217FF7C5.3FDC9ELO-DDBM13B.FI7@flex--tabba.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728550688; 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=exyGShkIuMmlgn/y2fH9jdue3XxSmqPMQZyWZJ9IIOc=; b=KtyKux8Jl3qEAMT8JkxBD2xbEq/7CAnAKARyF5VLjkQMF377NwN6+QarcRC6VK2biv3tC/ FD8Zj24S01TilpUtfsk/B8krabpj2GxgrqQBOLJsuvEZeGyMLvqRLWsw5jmWC667pvpwAT xh9ATcjAZGU4RIUxMp8XARhx1gKbyys= Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-42cb080ab53so5505385e9.0 for ; Thu, 10 Oct 2024 01:59:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728550798; x=1729155598; 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=exyGShkIuMmlgn/y2fH9jdue3XxSmqPMQZyWZJ9IIOc=; b=UQfMrjIvvjZ4UFCnH+0dlKKExQTb2ANieco1qbwTisfzBD/nTSDMpXGlZR3LgbBBGx scw9Lsm5H1BEGVikJ3aL02LNtDGi4PXFgtE1nllJB8nYGLmoHmB7Y+pzdtb6NuBf8WM4 vnlMf8E+aWhksV5+cXefUbCuTMgDa5IWGM6DfEsRmwMZmkr29jeyzGgyqtyvBhYmjUt6 pJbTAqmVLgSxKM7EOsmVG4DOwGERu23kkTUnFujVP5V7dGEW+Iy76qRlwRRxjKHwc/m3 JQk5Nu8u1FZAnIJ0Hyy70Co+n/rtx4yLQvVe5KCYUP3LpEnsp9mvLKa/Qyv8jvWawsU+ cfuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728550798; x=1729155598; 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=exyGShkIuMmlgn/y2fH9jdue3XxSmqPMQZyWZJ9IIOc=; b=xDnpZJl2ALTjjQbkxga/yN6xATN4wHGdXG3JbiJQpv/Z9v9uKtizOS0BvXNFpdYlfU NcwBDPxutrceX2imRJSkdoxYmD6Laq1pW3/gdR3BDhBA/UFlyU/jR/46Gk77yd1XVR3/ UeLcl9gfUDbiFCygVYexbsXYwdZSr270gMrEDyBp39oBKz7iU0c0QrC0aZdSxmV4n0V4 fDhc0zzCwABnQ1OvbJrrWVf71g3gqhZYIR8bXnTFohWK+SMHq+sdCiFXFJEKpGmHJWDp so9JrrCEf7TalKS2E1y9Oj0HYYJbu/Xu7H/u27V9/5+0FCxh6e7FYYsVquIJqeLeyO0+ qIPQ== X-Forwarded-Encrypted: i=1; AJvYcCWLIocgo42xtYTMUl55UaKEwN7QseI1+ab7KHi6o7ZvKPcUJIs5lAS5ExxGCOFE2qWfzOTxYqFy+A==@kvack.org X-Gm-Message-State: AOJu0YxmOa+l7Lhe5ZRbfLytRFUdHpTu+S/uqcSzGAx3ZPcuxmA7iHSo V3gdOXopBSl4J3ed8+xXTpAy2SieSEBhq3NdD3zH7F8WdJDnM5tskAloDi5jFyUqnEisEXqFGQ= = X-Google-Smtp-Source: AGHT+IE3WukKHOq7Bmp4xhzc8OQfh5cP/L7vmCsDLrA8EMFDPkbOy1WECbzZ6HHl8nL5SydVZHebiozHSw== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a05:6000:1091:b0:37d:4d36:e178 with SMTP id ffacd0b85a97d-37d4d36e297mr456f8f.7.1728550798081; Thu, 10 Oct 2024 01:59:58 -0700 (PDT) Date: Thu, 10 Oct 2024 09:59:29 +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-11-tabba@google.com> Subject: [PATCH v3 10/11] KVM: arm64: Handle guest_memfd()-backed guest page faults 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: 7EF4712000F X-Stat-Signature: bomb1rce9jgr8f5qa3qmro9x746qyj46 X-Rspam-User: X-HE-Tag: 1728550798-316463 X-HE-Meta: U2FsdGVkX1+iiJ1eVCcWazbT/A86aFPzGkdC+3YoeQVjxSyIk+aCY1tAx2ZnrMPJOp56kLPrlumz6RVS4LhlNCO6R1qjzZvJ9erGEcz9tzkVTMmrb6TICvf5vHLz+OPc/Ul2N3sc3OGUamWhwvbdXt7APGCB1oPx9DWq7r6qRTSiUCu6/tPka90w/XQcGFvdO/sS4LEz10rNK19W+zl1bCBa5zc9Bp5wQSwHc531VH9QdOmfNhhFeC5+hzjCuiQ+bK0edmKDLEaPYHO1YlvqOkb4HDYBA+0fDQ9gAa0+oeWK61UXcfTMfsYv1hhW4JE/LvW/+GVTeoLXbu8HqybW/coqXAjwA0IQxJyAmiZwOEVFWJZAHbag2er/DJ3xCKcdy2k2gBP0RYF9knFwAhn52XwPY59/tcZx84vYmLdBan4ADVA7u0FoJ+KRbcJklReHFOPY3mkaq/qyh5Z1G1eT3BoqpcUURx6YwIGng4QqT7bJTzRwrBJPdQTJWgo82lozzSEXyQkR83falCB+bQuOu49CBuue03LpsnylKQQRHCORSuNJQ+X+VlUxlPpKftZN//viGw3x43jKVW0VfoiDno6bRHfFlEs7vPeLUYkfy7N5pf+KqiRass51XhNqoHDHDo5uF7hTPtIO3Ye/ETb7Ad+otv47T0RpFi7CS9h1tcMlP4LzRl2WADUznn7HDzAFBRI2qrKHerWbONpYt0H7Iix0ZEx1zYBhOTcY3KE+m/l3pdhzO32XW3dxsmral8+BWLZaJJtjZrlQ9chrFot1APQ6fNCzJsoY5qerZNktGUEdb8DAA8BAtgJYqREbxGl2L8OYVYeJbLQFRHYn0wTwQ59dfnyM0/uA9bQrhxIvmogL/4y50exIN/OSEWkIQp+SBGQYRhS4VYVYJkn/8Wk9/Zaj4WBufha9OeubT4yj/UACwNoXtcPeO0s8r3374BGeNjqRaiFfjc8A9keT0+w sRFLnAwq cP3rvP2F6xdB0V7wAUy5pjKaS6L06vbtjddfzpwijhJDylTvtGFHVQf81IhGiflabbogE7/EIxkPnFe8eewnOFQpnTWeLtyBGiWriUMln06HCE2Z+Ag6O1QD4GDt7p7s5ZJG+Yq5Thm8RwUinwg03nfy2RmDwW5gEPMe5XVG0XdBtq+KnO7MItNYVp055I0F8MWYG8BvpThGEU3rEKz0yBvIkrYzjtydGEnpSddV/0tEZBF10aRKIEBaRGHiQU+cBG3JijFSjWXTh01oBC4yVTjkLPHsYHLAaJw93837PrAfSX06kMoiQkfr06cm2iLEkhe6UrO0nVJ9mTZDTPQDGVI1Gb3VXCz/6/CqSlXNg9xaqH/ebs0Qn/+HDKW7TxhgiwMRZH5Oc3crvYWuWUPX3vqhNSQ8F9H3x7oVeIMtJGlTTzcoCzjOo9ZSJLt0TagLMydiVuW5iKT6F9xGabI6n9nkgGljBw9noSENDv73dSSLjS1AjqAO6g7ey6Y9iEcAvxWsSudUzxyrJEPX7jFcMi7j8eWJr9ZrD2VjoisHoF6ngQnSFHW/ZVYGradq55UZ/usLZdKg0Z5hskM2ncDPe3Rhgmx5FqEY7wNA6e2gVRNlU7irwl02D6pdxhxE3Ks6oEL1Sc98s0tBbuWi4t7HbM8y8oA== 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: Add arm64 support for resolving guest page faults on guest_memfd() backed memslots. This support is not contingent on pKVM, or other confidential computing support, and works in both VHE and nVHE modes. Without confidential computing, this support is useful for testing and debugging. In the future, it might also be useful should a user want to use guest_memfd() for all code, whether it's for a protected guest or not. For now, the fault granule is restricted to PAGE_SIZE. Signed-off-by: Fuad Tabba --- arch/arm64/kvm/mmu.c | 112 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 110 insertions(+), 2 deletions(-) diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index 71ceea661701..250c59f0ca5b 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -1422,6 +1422,108 @@ static bool kvm_vma_mte_allowed(struct vm_area_struct *vma) return vma->vm_flags & VM_MTE_ALLOWED; } +static int guest_memfd_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, + struct kvm_memory_slot *memslot, bool fault_is_perm) +{ + struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache; + bool exec_fault = kvm_vcpu_trap_is_exec_fault(vcpu); + bool logging_active = memslot_is_logging(memslot); + struct kvm_pgtable *pgt = vcpu->arch.hw_mmu->pgt; + enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_R; + bool write_fault = kvm_is_write_fault(vcpu); + struct mm_struct *mm = current->mm; + gfn_t gfn = gpa_to_gfn(fault_ipa); + struct kvm *kvm = vcpu->kvm; + struct page *page; + kvm_pfn_t pfn; + int ret; + + /* For now, guest_memfd() only supports PAGE_SIZE granules. */ + if (WARN_ON_ONCE(fault_is_perm && + kvm_vcpu_trap_get_perm_fault_granule(vcpu) != PAGE_SIZE)) { + return -EFAULT; + } + + VM_BUG_ON(write_fault && exec_fault); + + if (fault_is_perm && !write_fault && !exec_fault) { + kvm_err("Unexpected L2 read permission error\n"); + return -EFAULT; + } + + /* + * Permission faults just need to update the existing leaf entry, + * and so normally don't require allocations from the memcache. The + * only exception to this is when dirty logging is enabled at runtime + * and a write fault needs to collapse a block entry into a table. + */ + if (!fault_is_perm || (logging_active && write_fault)) { + ret = kvm_mmu_topup_memory_cache(memcache, + kvm_mmu_cache_min_pages(vcpu->arch.hw_mmu)); + if (ret) + return ret; + } + + /* + * Holds the folio lock until mapped in the guest and its refcount is + * stable, to avoid races with paths that check if the folio is mapped + * by the host. + */ + ret = kvm_gmem_get_pfn_locked(kvm, memslot, gfn, &pfn, NULL); + if (ret) + return ret; + + page = pfn_to_page(pfn); + + /* + * Once it's faulted in, a guest_memfd() page will stay in memory. + * Therefore, count it as locked. + */ + if (!fault_is_perm) { + ret = account_locked_vm(mm, 1, true); + if (ret) + goto unlock_page; + } + + read_lock(&kvm->mmu_lock); + if (write_fault) + prot |= KVM_PGTABLE_PROT_W; + + if (exec_fault) + prot |= KVM_PGTABLE_PROT_X; + + if (cpus_have_final_cap(ARM64_HAS_CACHE_DIC)) + prot |= KVM_PGTABLE_PROT_X; + + /* + * Under the premise of getting a FSC_PERM fault, we just need to relax + * permissions. + */ + if (fault_is_perm) + ret = kvm_pgtable_stage2_relax_perms(pgt, fault_ipa, prot); + else + ret = kvm_pgtable_stage2_map(pgt, fault_ipa, PAGE_SIZE, + __pfn_to_phys(pfn), prot, + memcache, + KVM_PGTABLE_WALK_HANDLE_FAULT | + KVM_PGTABLE_WALK_SHARED); + + /* Mark the page dirty only if the fault is handled successfully */ + if (write_fault && !ret) { + kvm_set_pfn_dirty(pfn); + mark_page_dirty_in_slot(kvm, memslot, gfn); + } + read_unlock(&kvm->mmu_lock); + + if (ret && !fault_is_perm) + account_locked_vm(mm, 1, false); +unlock_page: + put_page(page); + unlock_page(page); + + return ret != -EAGAIN ? ret : 0; +} + static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, struct kvm_s2_trans *nested, struct kvm_memory_slot *memslot, unsigned long hva, @@ -1893,8 +1995,14 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu) goto out_unlock; } - ret = user_mem_abort(vcpu, fault_ipa, nested, memslot, hva, - esr_fsc_is_permission_fault(esr)); + if (kvm_slot_can_be_private(memslot)) { + ret = guest_memfd_abort(vcpu, fault_ipa, memslot, + esr_fsc_is_permission_fault(esr)); + } else { + ret = user_mem_abort(vcpu, fault_ipa, nested, memslot, hva, + esr_fsc_is_permission_fault(esr)); + } + if (ret == 0) ret = 1; out: From patchwork Thu Oct 10 08:59:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13829807 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 D27FECF07D9 for ; Thu, 10 Oct 2024 09:00:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 84D2D6B00A3; Thu, 10 Oct 2024 05:00:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7D5F66B00A4; Thu, 10 Oct 2024 05:00:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 629016B00A5; Thu, 10 Oct 2024 05:00:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 36A3B6B00A3 for ; Thu, 10 Oct 2024 05:00:04 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 2870E1C5CD3 for ; Thu, 10 Oct 2024 09:00:00 +0000 (UTC) X-FDA: 82657095486.21.71A3EA6 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf22.hostedemail.com (Postfix) with ESMTP id 01D05C0020 for ; Thu, 10 Oct 2024 09:00:00 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="b0d/gzxF"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf22.hostedemail.com: domain of 3kJcHZwUKCB0M34439HH9E7.5HFEBGNQ-FFDO35D.HK9@flex--tabba.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3kJcHZwUKCB0M34439HH9E7.5HFEBGNQ-FFDO35D.HK9@flex--tabba.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1728550700; a=rsa-sha256; cv=none; b=O6fEcEo4oUs1zevU8b/4yZyKDB+OCnu5tU1ajmkOsZzCFUUttRIXVuZj4ZompaXJEiOtGD WvobA2AvmRqnYocszmeA70RAfJrOCy/9/CBgsoWvHVJyt2n8eTJBgdoSlRnTAoH01GSoCs /MIx3PehJ/zka7crPi/hZt0QJFDZ0so= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="b0d/gzxF"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf22.hostedemail.com: domain of 3kJcHZwUKCB0M34439HH9E7.5HFEBGNQ-FFDO35D.HK9@flex--tabba.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3kJcHZwUKCB0M34439HH9E7.5HFEBGNQ-FFDO35D.HK9@flex--tabba.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1728550700; 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=vRqRt9/OdUuC94QGlx1b3LLfUnzhlvtZvk9uJQuMY0s=; b=jCZjx73Ut6MTG1IldvdxMl9BkfqObh9iJWXXZ5chovTkSCDTpc0CpRrIY/IutGDjNRYPpC Fs3znMAmJ6zW/8ZEgux/fBjapy3JcwSpV1vuXqRVjefa13BS+uFlAB0PbwZszpzTM3y9tB ctfwdKhZGASydj6TPucBYvt+5sp4qs0= Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e165fc5d94fso1001880276.2 for ; Thu, 10 Oct 2024 02:00:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728550801; x=1729155601; 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=vRqRt9/OdUuC94QGlx1b3LLfUnzhlvtZvk9uJQuMY0s=; b=b0d/gzxFM0AvdNu3c6KG2oAy7mIJechUtXJM6txYDcz73AzdLLciB3x4tJpaDeMC7Z MMsm3jVZhJpuxP6RK4f69VOPN3dZplnVjSh56eDVY3ikNKeoccX3BijlMNuacfK0laO2 K22KWZ4QzjpT7CsJMVGcEuqMQAsNR7OzmRzqb6cQyOohcu4qtsTrxvjmEDtsVyc0qIkU hrqcuPi3InB8ehbEV/fwCGOvqvNOlWqtlvedhs7MRU32LCYva9jlfRLbrmKs3Y+dpyi8 JSByf7wVCiagstSkfDyaKvohDFbdccB0MOJ5Os9RrGYK8Al9bIKC36XHUvhrfHJJannF 7bIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728550801; x=1729155601; 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=vRqRt9/OdUuC94QGlx1b3LLfUnzhlvtZvk9uJQuMY0s=; b=ZjLzaky06VbGB8Neqej0BKxx6xvLafAV1sEC71qJRDITl08hZBvPi3UZOSJwjKnwOP 43WVyvjCsMtxHwnxb8HiBo7lruxwZkEdNtFj4azQEzkZK+cT2EZySkRz4/CkWW+gz1i3 p21LRCjZhASOhmjqfQeGQuxvnNl7mqzLZUh9e8a4DAW0P/0DBEQTFpplI0HwL4t2lv/B KCIIGUS3tR4XEeRv8YIPgFnEdQi0hQQXonhiFcBYQTldJkIBJk+D7DVKZxfz4q1cbu8x tLfQB0QDBiSz6Ov4vvaK2YEt0gAsXGXC4RQ/q2vibvflPLtIe7ABGb0iPZIG7edb95t2 DboQ== X-Forwarded-Encrypted: i=1; AJvYcCWuw9amINFBtR1CqYLS/Exj4JczG7nCmWYsjIngGlJBtW9IssKiw/DCJl06kpBZO1cKGVhXBZbPtg==@kvack.org X-Gm-Message-State: AOJu0YwV0S8ASZj6ohuowp0Yu9Qm3MpQx5U5cc/xolCfB4dHmLYIuWOp zv3g4jdAazy6AKDKSbrIcNMhEgBjerTblqILk9AFPHBgcaKgeBdfr9tnbl9PTZrW8iLlAt/ijw= = X-Google-Smtp-Source: AGHT+IHKOC7WKTbH/ijVUTH38hxdU+Ou0RoKl5cARtWTF5vqoihO6BtV+q30wvio57rQ24Dl8KyceyokuA== X-Received: from fuad.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:1613]) (user=tabba job=sendgmr) by 2002:a05:6902:1812:b0:e24:9f58:dd17 with SMTP id 3f1490d57ef6-e28fe32f042mr55663276.1.1728550800731; Thu, 10 Oct 2024 02:00:00 -0700 (PDT) Date: Thu, 10 Oct 2024 09:59:30 +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-12-tabba@google.com> Subject: [PATCH v3 11/11] KVM: arm64: Enable guest_memfd private memory when pKVM is enabled 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-Queue-Id: 01D05C0020 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 78rbf6ak46adc7qjnwb3w3zyhr4zh1ns X-HE-Tag: 1728550800-494499 X-HE-Meta: U2FsdGVkX1+TWi/TVbBKGoc5draELkRT9gZSCk4hdyj6rhy+7whzP7jMcP5bJ4qJjHfKy3VtI8d2aeaCm8gpbqExjo6pPqHvpRhBSOj5RuICgsdYDGOm+3cea/WRlbHeNgVjVvkF+2aYGjK6SCuPcK9zCBGR7foBVSAAWOkHS/071u2Cr0Vm6kFcFO5ZwC/Q2oKvkJFg3meQEMgF+TfBTdydXxJv7jRrgX5Z9zhPZchrK/FEhqu9o1BsQhtSvUwZA0B/KwklnHipVODhu6aMPeUyHzimUfoqngbPdTkwsj5rPoy5GhEwkqZuQTbYllNbrHN56yoKkvZ4Mx4vOIvscVf1WxPkzFWMLQYJzkxe9+8lz4yp0HQ+OcpTFDzcyRnYlETkdbHDS2ctxDwK2gxF1HjB6nxInAcmrYCL2CKdYBGrJzlBGE9RxiNgNIbGgP+tnA88UsBPSEewmJcyZ/2AdyiQdMjM5EVQBM9eKS4g2Gg4qCAbs0xtzBBoy9ofDEBZADhHtWRjvQNs3L5/8OrFJl+AqeTQ+XMyLQQVn/mQH90bv9GFdL/pJlb2SMCbIMmP8fu4CQ//WXrr3MW21xTcFGxcVCLEAYftk8bCiFZq+2e+DbsziZHSx+5PPEKITPgyEdaTp2uJ2guC7q4KUeCLLpjbuXlSKzPMCK0nIF5+u9C+jyFKgATDUDSNIM5i1XTmCWwhJOqP6TJZ1rSa57Xnco6ic3o74kLph1wiOn+EtlTjMCZiriaXnoCrqpucDGy/pk5J3z4gB730lY3PxXgrKRQaWBZZ6LSQP77Q2oSsAvrKVuXKPO5s+N6yPVXOPGaMWOdMUaeTXDOrdnSz9wB7vGmNo2+9kyoVmnYu2NYC6e+wsRK29mf5XuCIurtxqPrbHvIlTcoP52sYgTLHDrTFcbhxKWHw6aPGVdKBIo4HVX+TBTKwZkVqClxPCFU948qo6TKUTva0727v6uzti9x XDPSRJFo VHwotr3vyuTMcgl6Ypye+yZu1nMNigUAJdwuG6AkJOykVJJ891FjABzt12SdjUZPGGZvCfm5qvzTF+xFNiWIsklEDmzJ/Jh5bRTkdXp2fcu7JxviOphV/mIyrmfcCHSRszub/2LIkEZ/1Tje/GFKNXyGYcuRaqUqtp9IWCd4Bpzw/a7DyeoS9iZAbFb37vA6kpAEA1xtYHhBVhQgRfjsLap60yeY5A8tz0iRnmtp2nnRMY9W/ZghzUUyNjfbLhdDBAJnQ353x9GzqWO3UK+uOFz1Hls5XVJMnPFcBGMSkoRgcS7kPbpw4xDE7d0cL2g9dEphM5EK8yOz4tWmXlIks19s3k82QLaXLew2sWZXUOPuIroqvwVlGYHuiPTM156sDV7vhOq7Mr4TVQ2+X/RyyxoKTNiu1k9NHo9jGsrHo/l/KZ1McibRg/zd7ikeDgSRZ5i5GEhDJCnbCv15YNrbrEVvaXOpc4wPtuRIFuiv+9ytS9p737Yzs4rSNAEwcXJNdLyyJl0fPe71xVL2hEkndw1JNKGBfoKIMwUDulu0kHHeefVUDxx4nKvrZlP0Q/gwBmetH67v7Q3JUsApJoqi2odyKiNF3Wt5Nx/Pje+E3dMKwLKCncduQZpt/TAkahRwsqySaVmVXLjX0qCQAyfLLcm+gRw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.021819, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Implement kvm_arch_has_private_mem() in arm64 when pKVM is enabled, and make it dependent on the configuration option. Also, now that the infrastructure is in place for arm64 to support guest private memory, enable it in the arm64 kernel configuration. Signed-off-by: Fuad Tabba --- arch/arm64/include/asm/kvm_host.h | 3 +++ arch/arm64/kvm/Kconfig | 1 + 2 files changed, 4 insertions(+) diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index 94cff508874b..eec32e537097 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -1496,4 +1496,7 @@ void kvm_set_vm_id_reg(struct kvm *kvm, u32 reg, u64 val); (system_supports_fpmr() && \ kvm_has_feat((k), ID_AA64PFR2_EL1, FPMR, IMP)) +#define kvm_arch_has_private_mem(kvm) \ + (IS_ENABLED(CONFIG_KVM_PRIVATE_MEM) && is_protected_kvm_enabled()) + #endif /* __ARM64_KVM_HOST_H__ */ diff --git a/arch/arm64/kvm/Kconfig b/arch/arm64/kvm/Kconfig index ead632ad01b4..fe3451f244b5 100644 --- a/arch/arm64/kvm/Kconfig +++ b/arch/arm64/kvm/Kconfig @@ -38,6 +38,7 @@ menuconfig KVM select HAVE_KVM_VCPU_RUN_PID_CHANGE select SCHED_INFO select GUEST_PERF_EVENTS if PERF_EVENTS + select KVM_GMEM_MAPPABLE help Support hosting virtualized guest machines.