From mboxrd@z Thu Jan 1 00:00:00 1970 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 9D32FC28B23 for ; Fri, 7 Mar 2025 17:04:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CC1A46B0083; Fri, 7 Mar 2025 12:04:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C49696B0085; Fri, 7 Mar 2025 12:04:13 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AEB436B0088; Fri, 7 Mar 2025 12:04:13 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 9181D6B0083 for ; Fri, 7 Mar 2025 12:04:13 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 60F5B1209D0 for ; Fri, 7 Mar 2025 17:04:14 +0000 (UTC) X-FDA: 83195378028.15.35E65E1 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) by imf28.hostedemail.com (Postfix) with ESMTP id 977BBC0027 for ; Fri, 7 Mar 2025 17:04:12 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=m0kAqU5Z; spf=pass (imf28.hostedemail.com: domain of 3CyfLZwsKCMoqs0u71uE93ww44w1u.s421y3AD-220Bqs0.47w@flex--ackerleytng.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=3CyfLZwsKCMoqs0u71uE93ww44w1u.s421y3AD-220Bqs0.47w@flex--ackerleytng.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=1741367052; 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:dkim-signature; bh=5rmlhlPewNEBG+pQdCXEcSr0zWXtYtplwY6Z+RVb7xs=; b=aST2uLKdSDIKUqYMHrdhDBLEb2+nSGEqjzAkgH3cti4jUGvaWibt6YcXGc2dsojm629pqP TQi3ia8YIVHY2ZBoE4AyauXALBMaZIZ3Opobcv2sVJX48bmmxZeMMe0rNvgVEaLRu/bvPj TVwW2QT2D0JExy/9Rgkb6hZke/12tfc= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=m0kAqU5Z; spf=pass (imf28.hostedemail.com: domain of 3CyfLZwsKCMoqs0u71uE93ww44w1u.s421y3AD-220Bqs0.47w@flex--ackerleytng.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=3CyfLZwsKCMoqs0u71uE93ww44w1u.s421y3AD-220Bqs0.47w@flex--ackerleytng.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1741367052; a=rsa-sha256; cv=none; b=Cj8fsOsMvtgtanN5EB+vHvftSzJuAoN5rru4OtNx5FbW4+30XE+37WY9QCelbG/pFnlvFj 06D5ywpiVOzXJJGHcVV3kaG1Ghs/SgJv5FZkS7Vl26xAphCvrCXIcRolgdrfhCg43ojNxl H4hXUJYzr68LuYFgQ9hs4MxLb5Aqmuk= Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2ff8c5d185aso738715a91.1 for ; Fri, 07 Mar 2025 09:04:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1741367051; x=1741971851; darn=kvack.org; h=cc:to:from:subject:message-id:mime-version:in-reply-to:date:from:to :cc:subject:date:message-id:reply-to; bh=5rmlhlPewNEBG+pQdCXEcSr0zWXtYtplwY6Z+RVb7xs=; b=m0kAqU5Z5+MPHRlBC8QqVSqmWlvHb68EeAHbQBnXw+aUriyJtTFlG15fb769kzqww6 TeCtEBHZVp0/ZJD9fPRMKCrCk0N43MdPPgER3pIcttWsidRCS9Jya9XnHQxXoQ8mb8oM wWOh2Ol8PFACCjXlrZb7BB0Feer2ibfK/ARWmrR+Sc1zowGuT4fYEMzfAPXsMV61h1Z+ BxrFQn5FWgBcdtSIMXRAjf5fg1WZPBaGBCMDPiMrXeUq3x0Xtdmo24WDyvffUAhoar2y Z7KU/b/KxCh8/88jD6U9BCeReAlcAHdBPs64AsiC96QNtA82vr4igSJkgNY6TZiZQcFa R+CA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741367051; x=1741971851; h=cc:to:from:subject:message-id:mime-version:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=5rmlhlPewNEBG+pQdCXEcSr0zWXtYtplwY6Z+RVb7xs=; b=aifuJd+EwbzcINYSU/XFCPMNbw41Dmh7zl8z6vrqH72ig0jh8bR6jEYQnMlwUbPdn4 XqFxZAkb7rIytAItVB5ScQXMcRlI9iibr1VmItUXCrf0pxl95c+jS9cWytx7KC9zON6u vbO2w3LU2o0yTRYUKSVXchK34Tt4a9BjgFqCqeConWBYG8kRyLmSpbx6VH/7EuXjY0f6 w4oDl9wdPgdeQiam7nO3yTIKdNzAt8VnwNwuv0vUkmeWdZqza4+Jci8zyV1J2K51cwuM ta+yUkCWUHIdQHAFTMPJuSoftd5BtFdb+UFphL4uqzRlsVlTwU8xh+bqMyoIxkubRLag y20w== X-Forwarded-Encrypted: i=1; AJvYcCVH8ujdFSOyCBsli4hFx7NQPb/ekE+kaB0afOrTuWr7/ouTloZncPMK6krR0es4DMR9xmDO9N6T5w==@kvack.org X-Gm-Message-State: AOJu0YxH5CziuVz8mMoljEKj5Cxo0b8zqycnHsU2KkTNnT2snRJ2qAd6 kWLvO0Awmjya7tOm7gLDgr4r9tNOERf/flC+VvoAvpRm5JL4UVdv2p6DHl3drdzI2K6u28U/YDt 7w0BgPTgLrnjduPQbFyN9Rw== X-Google-Smtp-Source: AGHT+IG6gHdllcDNQV4+hoCTAlEMUZwJ3RkEUZgkLlJluFlZCeWSULH+9PhODAFdQurLMAd2+VXO5p8Z3HLOThloWQ== X-Received: from pjbsp5.prod.google.com ([2002:a17:90b:52c5:b0:2fa:1771:e276]) (user=ackerleytng job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:4d12:b0:2fe:8c22:48b0 with SMTP id 98e67ed59e1d1-2ff7cea6125mr6687647a91.15.1741367051284; Fri, 07 Mar 2025 09:04:11 -0800 (PST) Date: Fri, 07 Mar 2025 17:04:10 +0000 In-Reply-To: <20250303171013.3548775-3-tabba@google.com> (message from Fuad Tabba on Mon, 3 Mar 2025 17:10:06 +0000) Mime-Version: 1.0 Message-ID: Subject: Re: [PATCH v5 2/9] KVM: guest_memfd: Handle final folio_put() of guest_memfd pages From: Ackerley Tng To: Fuad Tabba Cc: kvm@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-mm@kvack.org, 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, isaku.yamahata@intel.com, mic@digikod.net, vbabka@suse.cz, vannapurve@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, peterx@redhat.com, tabba@google.com Content-Type: text/plain; charset="UTF-8" X-Rspam-User: X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 977BBC0027 X-Stat-Signature: au93eytd7wkky8sz4bkrjm453591dmdq X-HE-Tag: 1741367052-617591 X-HE-Meta: U2FsdGVkX1+k7onm/gTXz2hFSstXx9PbDwVAl0YI7Tuxqr7XSgH6XDk9//URZt2ZGP0A/qgHYrtFA8G5TFsIPFQW0RfbQEZEL8xbBHOfyLoWANza7aJmGvueBQDpcQ4+QqV0Y3nlOf3YdB9gUG1B75vCk9PZxrTr8YqRCddyLF4XVSctVczSUXcnPrIE5cu6HjGS3yfsOI0z+3QoyfqN1rJSwmWxKokyClPE7AufYugzXZ9swilY7MDsG6+3If11x5l7NpK+Fw/CANiNoRKcbgn6TovX6PHFZqkZw2OC389Hk7kJNj4PQxi8KvKOhbnIseTQRUxHJ0DLR5HI/qkpqBmIHHZcno2WAdNurSuh2PrlClzViyIqO419kJk+80hbeed2GLdru+6flF+oEasxv6XysOLut6f6Z7OL9KRtMtJwHH7USK9TdLxfptXbnnwmvc3tpMd5M8exXxbj4jOUwiJHfE9nb+fe3tenqJSCxlRnc3pamOO+Sz+2emz7zaew1clwwRx+OUST9AxSgCf/e6H5aUpiGksVUxuDJmjPdCNGMpKEtUhUWdnapRS2sjCZSz2/OLi/BG8PlmT1C2kijEe7k+YasbOC6vZO9QqqR+nNZeesT/ngA79WKXaSS9u+Nw42Ic2VNeuJX6ex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1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Fuad Tabba writes: > Before transitioning a guest_memfd folio to unshared, thereby > disallowing access by the host and allowing the hypervisor to > transition its view of the guest page as private, we need to be > sure that the host doesn't have any references to the folio. > > This patch introduces a new type for guest_memfd folios, which > isn't activated in this series but is here as a placeholder and > to facilitate the code in the subsequent patch series. This will > be used in the future to register a callback that informs the > guest_memfd subsystem when the last reference is dropped, > therefore knowing that the host doesn't have any remaining > references. > > This patch also introduces the configuration option, > KVM_GMEM_SHARED_MEM, which toggles support for mapping > guest_memfd shared memory at the host. > > Signed-off-by: Fuad Tabba > Acked-by: Vlastimil Babka > Acked-by: David Hildenbrand > --- > include/linux/kvm_host.h | 7 +++++++ > include/linux/page-flags.h | 16 ++++++++++++++++ > mm/debug.c | 1 + > mm/swap.c | 9 +++++++++ > virt/kvm/Kconfig | 5 +++++ > 5 files changed, 38 insertions(+) > > diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h > index f34f4cfaa513..7788e3625f6d 100644 > --- a/include/linux/kvm_host.h > +++ b/include/linux/kvm_host.h > @@ -2571,4 +2571,11 @@ long kvm_arch_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu, > struct kvm_pre_fault_memory *range); > #endif > > +#ifdef CONFIG_KVM_GMEM_SHARED_MEM > +static inline void kvm_gmem_handle_folio_put(struct folio *folio) > +{ > + WARN_ONCE(1, "A placeholder that shouldn't trigger. Work in progress."); > +} > +#endif > + > #endif Following up with the discussion at the guest_memfd biweekly call on the guestmem library, I think this folio_put() handler for guest_memfd could be the first function that's refactored out into (placeholder name) mm/guestmem.c. This folio_put() handler has to stay in memory even after KVM (as a module) is unloaded from memory, and so it is a good candidate for the first function in the guestmem library. Along those lines, CONFIG_KVM_GMEM_SHARED_MEM in this patch can be renamed CONFIG_GUESTMEM, and CONFIG_GUESTMEM will guard the existence of PGTY_guestmem. CONFIG_KVM_GMEM_SHARED_MEM can be introduced in the next patch of this series, which could, in Kconfig, select CONFIG_GUESTMEM. > diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h > index 6dc2494bd002..daeee9a38e4c 100644 > --- a/include/linux/page-flags.h > +++ b/include/linux/page-flags.h > @@ -933,6 +933,7 @@ enum pagetype { > PGTY_slab = 0xf5, > PGTY_zsmalloc = 0xf6, > PGTY_unaccepted = 0xf7, > + PGTY_guestmem = 0xf8, > > PGTY_mapcount_underflow = 0xff > }; > @@ -1082,6 +1083,21 @@ FOLIO_TYPE_OPS(hugetlb, hugetlb) > FOLIO_TEST_FLAG_FALSE(hugetlb) > #endif > > +/* > + * guestmem folios are used to back VM memory as managed by guest_memfd. Once > + * the last reference is put, instead of freeing these folios back to the page > + * allocator, they are returned to guest_memfd. > + * > + * For now, guestmem will only be set on these folios as long as they cannot be > + * mapped to user space ("private state"), with the plan of always setting that > + * type once typed folios can be mapped to user space cleanly. > + */ > +#ifdef CONFIG_KVM_GMEM_SHARED_MEM > +FOLIO_TYPE_OPS(guestmem, guestmem) > +#else > +FOLIO_TEST_FLAG_FALSE(guestmem) > +#endif > + > PAGE_TYPE_OPS(Zsmalloc, zsmalloc, zsmalloc) > > /* > diff --git a/mm/debug.c b/mm/debug.c > index 8d2acf432385..08bc42c6cba8 100644 > --- a/mm/debug.c > +++ b/mm/debug.c > @@ -56,6 +56,7 @@ static const char *page_type_names[] = { > DEF_PAGETYPE_NAME(table), > DEF_PAGETYPE_NAME(buddy), > DEF_PAGETYPE_NAME(unaccepted), > + DEF_PAGETYPE_NAME(guestmem), > }; > > static const char *page_type_name(unsigned int page_type) > diff --git a/mm/swap.c b/mm/swap.c > index 47bc1bb919cc..241880a46358 100644 > --- a/mm/swap.c > +++ b/mm/swap.c > @@ -38,6 +38,10 @@ > #include > #include > > +#ifdef CONFIG_KVM_GMEM_SHARED_MEM > +#include > +#endif > + > #include "internal.h" > > #define CREATE_TRACE_POINTS > @@ -101,6 +105,11 @@ static void free_typed_folio(struct folio *folio) > case PGTY_hugetlb: > free_huge_folio(folio); > return; > +#endif > +#ifdef CONFIG_KVM_GMEM_SHARED_MEM > + case PGTY_guestmem: > + kvm_gmem_handle_folio_put(folio); > + return; > #endif > default: > WARN_ON_ONCE(1); > diff --git a/virt/kvm/Kconfig b/virt/kvm/Kconfig > index 54e959e7d68f..37f7734cb10f 100644 > --- a/virt/kvm/Kconfig > +++ b/virt/kvm/Kconfig > @@ -124,3 +124,8 @@ config HAVE_KVM_ARCH_GMEM_PREPARE > config HAVE_KVM_ARCH_GMEM_INVALIDATE > bool > depends on KVM_PRIVATE_MEM > + > +config KVM_GMEM_SHARED_MEM > + select KVM_PRIVATE_MEM > + depends on !KVM_GENERIC_MEMORY_ATTRIBUTES Enforcing that KVM_GENERIC_MEMORY_ATTRIBUTES is not selected should not be a strict requirement. Fuad explained in an offline chat that this is just temporary. If we have CONFIG_GUESTMEM, then this question is moot, I think CONFIG_GUESTMEM would just be independent of everything else; other configs would depend on CONFIG_GUESTMEM. > + bool