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+ib4DMCFX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ofilter, 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,