From patchwork Fri Jan 17 16:29:50 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13943551 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 E7484C02183 for ; Fri, 17 Jan 2025 16:30:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 53AED6B0095; Fri, 17 Jan 2025 11:30:16 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 44AB26B0098; Fri, 17 Jan 2025 11:30:16 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 29EB76B0096; Fri, 17 Jan 2025 11:30:16 -0500 (EST) 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 005026B0093 for ; Fri, 17 Jan 2025 11:30:15 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 8A2E5160587 for ; Fri, 17 Jan 2025 16:30:15 +0000 (UTC) X-FDA: 83017481190.22.70A381E Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf01.hostedemail.com (Postfix) with ESMTP id A976E4000A for ; Fri, 17 Jan 2025 16:30:13 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="tjoD/Hta"; spf=pass (imf01.hostedemail.com: domain of 3lIWKZwUKCA06noont11tyr.p1zyv07A-zzx8npx.14t@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3lIWKZwUKCA06noont11tyr.p1zyv07A-zzx8npx.14t@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=1737131413; 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=FO66kchh8bGzCA1fstTaZk+RuA83oGXAMjee9RM67m8=; b=XzG+hYIYYLXDyJtTatRGmmR2cWhAHFS3XlfhMENT/I8+JloxvtzuphjS1+qvMQeFwfwvDf /o0OTLqIdFHLnX1VXrWO63wjVCU1DnDMb6JbAMIxPjoMXWebgahFitlM3MeRadkI/F+EdF krWWUNhvOwfRg5szjpqbGpG7vF/iSdk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737131413; a=rsa-sha256; cv=none; b=Jmx5OUQ1hTYHKBfl1bgqk5TVkdTbEza55VcUNOwnVTdK3FdzsHteQYy9ppetEgi0zs3G7E Wqr6J5E+v/FI3FGa55tHvBIpeLm6BCswX0xEgfcMVrTzbCSBwIdOkgSiWS1BJKENeEkmt5 d1XaEEVEkIDhsVVtpSTrPl/y703IXKE= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="tjoD/Hta"; spf=pass (imf01.hostedemail.com: domain of 3lIWKZwUKCA06noont11tyr.p1zyv07A-zzx8npx.14t@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3lIWKZwUKCA06noont11tyr.p1zyv07A-zzx8npx.14t@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4359206e1e4so18938175e9.2 for ; Fri, 17 Jan 2025 08:30:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737131412; x=1737736212; 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=FO66kchh8bGzCA1fstTaZk+RuA83oGXAMjee9RM67m8=; b=tjoD/HtaRhO/wY+zzXzHGCgqyoz0OkCPiX5Dfa6d1vlgDYXDTuuqufJByDSvOxSIlA mZlE+mm5kRFjyRL0qjcj9UfmzA58pfd7jzm5VW7McLxS+JgDYErtyOx3jrAF2cFlLVvA xsf0o1c0fIB1Jr0Lh3F5PiYWarXdMxD3Ky0axensX+Fu+HePceA1e9BbZGjFciFY57Di l5fnQRNUCAoLaVslOgZXOWJRhPk9RDRW8iNCNcwrRSsq0k0Cl2UaGfQ5i74tA3frzY1x xOvBnlYa8R3YjMkkeMH2yqiz+FAGn427HdWLh/3bTbOz4wvA4rVJttlSYwJobJqWIMLd B2wQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737131412; x=1737736212; 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=FO66kchh8bGzCA1fstTaZk+RuA83oGXAMjee9RM67m8=; b=bRNGyMBMvaXRMb4hVcrJRpVjj3vIlpfUYipWBV2ZfzZ7ebHkREamqMKkrswUhaU8tp 8eWku+UorHDZvbEC+POsQ4WFj6UyRf8tOPCFSDV4H5OGnw3LzFENdcfagyvNPai0lnXK x7SeUTZn30Vb9JeAMnKALk8fyL/F+WrnlSsb9ssy2An4fi3+IX9NWFAKLXwWpMgAfnO6 SQ6V9Ue/aF+7DV95dw9kaAv00yOKXh6CgxVa13lV4/WiUq2eEaqgCbn0EOpSzE0kAnWG av24hB6x0rbLlY+njvwPgGx847Cj3AFcP54aBQBu8uOsuXDTYGyEZZli4A7KMsTC+X8A gMyg== X-Forwarded-Encrypted: i=1; AJvYcCWiH3gZaQSJnYknax596y1MPAyVW6fRPz1qnVqpY8zm11EWK5j7pU4ncJb/Ijfm8YfgtJbwNuDQYw==@kvack.org X-Gm-Message-State: AOJu0Yw6c0AK5KXfoLxSgxCPuhKd1TDBQEtH8jgQKzzPtaoVmoVqXC6V pPL2tm1+JLPFu264lQ87rMd59ZxSN9g8eDkJqhHjLL67VDlx8lbXP3hRKSJWXUDvZ8ni4UlLTQ= = X-Google-Smtp-Source: AGHT+IFZgD2R16tEYYHXnP1+7iY9g8PAift3he39jh3cPUQ7kJHFSZqengXdwyt2ds9qO8MFRhz37dcAmQ== X-Received: from wmbfm15.prod.google.com ([2002:a05:600c:c0f:b0:434:9e7b:42c1]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:5112:b0:434:a734:d279 with SMTP id 5b1f17b1804b1-438913f27femr40187145e9.16.1737131412324; Fri, 17 Jan 2025 08:30:12 -0800 (PST) Date: Fri, 17 Jan 2025 16:29:50 +0000 In-Reply-To: <20250117163001.2326672-1-tabba@google.com> Mime-Version: 1.0 References: <20250117163001.2326672-1-tabba@google.com> X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog Message-ID: <20250117163001.2326672-5-tabba@google.com> Subject: [RFC PATCH v5 04/15] 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-Queue-Id: A976E4000A X-Stat-Signature: worp89ji1wrdmxyiqnu8mdjnmjwb5359 X-Rspam-User: X-Rspamd-Server: rspam12 X-HE-Tag: 1737131413-181998 X-HE-Meta: U2FsdGVkX18d4Xc5YiVQAuPZ3fkyH8m0BP0u0J36v2PP5CFV6j4ufAW7ogwRF32k7UCeijz6qfkHaFqNGZlKLvLhg88Alk5Dik1AFj0aRe9LvhgH5izaOkJ8f28jcrEAYPqM0DGh53pxluT50b1IlksqVo8xoOlCHfBjaLzZa51Kn25jT0+i97/tMeit0Hkf4fUX+nrigwfa0vyJ1DklY4qXRtwuHuFAEI/qFQaxrNx5uYV7l1b5g8dE4RkUQskQJuE1WVjzczmVGF9qWsjd8rBjRYtYoEishRv9CC9bInhLgmWFM2imeHP+N6CAp05ekqRwFOJfMHF+IYOs8+kBUYTnY8N8p6qe4yEJAHW4vTRzViHY5spt3cnOg3lJcfqNoLXeuHxg1ixsaBub3P9DfvDsQSul7jjIBY0eazDdU1brJjJyva2/iXwBjNtYGXhOQZytrheCh9BKiYQsnWaKo+emHZ5KV84rDQ5h+R3uYtYHLQ1S0COwnku5nbkoxoSJvaUAvS934nM0sZR3GMdOXUMbsu7AlqMKRZzDL46jDGx/UnSuewdRCpTM/RbrkbqKIVPOuCEzA8STl1AfoYReOPqlPiZ6S5crX1iaYlG5vNWv3P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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 6453658d2650..0a7b6cf8bd8f 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. @@ -312,8 +323,28 @@ static pgoff_t kvm_gmem_get_index(struct kvm_memory_slot *slot, gfn_t gfn) return gfn - slot->base_gfn + slot->gmem.pgoff; } +static void kvm_gmem_evict_inode(struct inode *inode) +{ + struct kvm_gmem_inode_private *private = kvm_gmem_private(inode); + +#ifdef CONFIG_KVM_GMEM_MAPPABLE + /* + * .evict_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) @@ -440,6 +471,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; @@ -448,10 +480,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; @@ -464,6 +505,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,