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 09C0FC0219E for ; Mon, 17 Feb 2025 10:13:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 593C9280046; Mon, 17 Feb 2025 05:13:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 543CE280040; Mon, 17 Feb 2025 05:13:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 40CAC280046; Mon, 17 Feb 2025 05:13:00 -0500 (EST) 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 1F9EA280040 for ; Mon, 17 Feb 2025 05:13:00 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 8BF9A1A2184 for ; Mon, 17 Feb 2025 10:12:59 +0000 (UTC) X-FDA: 83129023278.24.5576922 Received: from mail-qt1-f177.google.com (mail-qt1-f177.google.com [209.85.160.177]) by imf14.hostedemail.com (Postfix) with ESMTP id BFAAB10000F for ; Mon, 17 Feb 2025 10:12:57 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=FeHJt5ne; spf=pass (imf14.hostedemail.com: domain of tabba@google.com designates 209.85.160.177 as permitted sender) smtp.mailfrom=tabba@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=1739787177; 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=nlKhym8zl9LGwKUmtUYv9YGT5GjDeZYP6xFQFUouwy4=; b=TviNFzvwf0DsmvwVaGRCKHmCAFPOgtP16FsNgqLyCSFRv3/AOm64tgXjkxrIllrdS/oU+2 NLFaeWPXn7fUTz7reCOovybrFK23BTg9ngRLoCCr77SPrvsRLJ/lnwXQ0YZhFXpwJGdnxs eDfSew4oGF8QaaFEQQcL1AVZSIZdin4= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=FeHJt5ne; spf=pass (imf14.hostedemail.com: domain of tabba@google.com designates 209.85.160.177 as permitted sender) smtp.mailfrom=tabba@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1739787177; a=rsa-sha256; cv=none; b=B+k3qdBZMPFWMb5N6D/PzbSMEtpXfPBTP6/4np4mssSB8BVfPSHtZfxZ1tNUIW/+9Hwanv Kmu4mX+Wm9vAH000VWIITNTld0z6btXHcnxSOr8x7iMsRtEjWuj8l8dMunksJvd5V+j4Ek tMLZmLoRIYBoxdB6RTxFid+M9bjF1Z0= Received: by mail-qt1-f177.google.com with SMTP id d75a77b69052e-4718aea0718so415021cf.0 for ; Mon, 17 Feb 2025 02:12:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1739787177; x=1740391977; darn=kvack.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=nlKhym8zl9LGwKUmtUYv9YGT5GjDeZYP6xFQFUouwy4=; b=FeHJt5nepeaecQ5/jQSBsvlBrJgvSsERJPkzXyEbCc/9nF8KSJyGctLH/zXbAFiWjG HEypIMgBFX79PeS8Iqrfl8l0GQPSUhryHlu70WGJzyBdHdVFT7kyJxNMuB2T/Y8hMrkF wcFru6zpgZWOQ537MIdkbc7yjSl+f7nX9prXv/K95eVJyOOPm2sbpMLG9ZpD76J7ZAv1 oiV7iWBN9CNZTEpBgjkPjOvo3vRyBii1oreJX/sNT2vOWZIkOuYzU0cPvWH0T/LW/LY/ BOXjC2WE8UrIuou0N6Ll4mNdbHBezrZFQMn0zcSSXB+m6WbctZ11AKdgtKpTnDemjqfb qxMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739787177; x=1740391977; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=nlKhym8zl9LGwKUmtUYv9YGT5GjDeZYP6xFQFUouwy4=; b=bCt9bAFUtmL/ZmLFDwC++i3R6WanxWhRI6NV2rWXr/9CKqJEEdLcigbQa0DncD1H/8 0lYAvKeq0oI+IoSh7D1E4DXso1FFaXy3/XTQwafzuqTmo+p5IVl5XVB+6USInTAKLM3x Ehig7qFylWWijwIr9/XfvO9oPYKTcfg+WAV6WN9/4et5FEBbvjr+xPcTMGOpJIg/GfFv KHx1gg+xQf0CzI5SU25KNFGcr7IcrC6ILrwLypinZXYI1gAOyhHO9qdtF56RdMqsxbgw 5Hy7ZnSi22a3r+Ypa/tGMvMqVe4RbkfvBjSdx5733h2xYWq0qEx7djkR1dwpKwphS3ri +eqQ== X-Forwarded-Encrypted: i=1; AJvYcCVwdK+QgFaYGu7D4PgA3SiKq9Q4PCh6TEmh7UXLW8BNu0BccA/IDZC/6+nR/P7uAcsXwHYzzBVjlw==@kvack.org X-Gm-Message-State: AOJu0Yxydrg4f8rNpGicJl850O5hozpM0MOrrRSBmCMlvglT4zXqG6bN ccRzyuJiATlcy7nbLolGyB2tUHJ167PuQlPAqYjXmoFUT++Y7ADmkX3FjLrLVLmA9HkabH9eIN0 gunXfjyBvUbSP8VFybkG72V16a2mZ/BZ3AQwH X-Gm-Gg: ASbGncs7ZFUPEDPq1xuRqFiPun+22LxmcopPdpOIlsmbt+A6n5pbok/0DmJWzh4uzHP ZqlvS2nQ9wxTlEHoskhej1wjx8k7roJZmL2oLIOmpdd2MFh7r+cyBVVGuTW+GDRP7exgCs6Y= X-Google-Smtp-Source: AGHT+IGVLbRb2Tt68lERDKYQOH+x23qhLGTcRpfU/KEsvdcIcS3tMurxSoV6UshE+HSt+vvSMY+LmrnRvG808PQzWGs= X-Received: by 2002:ac8:7f12:0:b0:46c:7cf2:d7b2 with SMTP id d75a77b69052e-471dc8efeadmr5803591cf.18.1739787176573; Mon, 17 Feb 2025 02:12:56 -0800 (PST) MIME-Version: 1.0 References: <20250211121128.703390-1-tabba@google.com> <20250211121128.703390-3-tabba@google.com> <4311493d-c709-485a-a36d-456e5c57c593@suse.cz> In-Reply-To: <4311493d-c709-485a-a36d-456e5c57c593@suse.cz> From: Fuad Tabba Date: Mon, 17 Feb 2025 10:12:20 +0000 X-Gm-Features: AWEUYZnhQhBRvgy6eE-Fs5AKJxSeCgZAX5UjioT0ujGOI4-boR97-G0BW1BoAKM Message-ID: Subject: Re: [PATCH v3 02/11] KVM: guest_memfd: Handle final folio_put() of guest_memfd pages To: Vlastimil Babka 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, yu.c.zhang@linux.intel.com, isaku.yamahata@intel.com, mic@digikod.net, 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 Content-Type: text/plain; charset="UTF-8" X-Rspam-User: X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: BFAAB10000F X-Stat-Signature: qmwo9wd5iqf55k6o6amirfqtuq9wjox8 X-HE-Tag: 1739787177-854601 X-HE-Meta: U2FsdGVkX1/D3+CH4kurJow2Ov/g7ptmIRaoCSjC065UvuUk5qp0KUjoXQ613+rE8glcmeF2vAdM0bGnnBfmXKOIU1gTY1EZjavdQchW+At601UUu0ZjObtlKM7en5UZ1RNkpcmKG2RK1hqZBtR0VQSY0hy0EduBEOTwpHKAnI1EZ+0rLX1tgiGWSPgAjiwh+sgv6KTqmZFPmYNjckans4Z78m6MJ+sagaSmVTtTrkLMIKWRlnQFy+7qjeIq6Dmb5aA4biJrKKQzuF/peaxffzz7ZLaJ8mj0t5B2QlrFKSifp003h8hNI2oQMhjkbJkrmA5KqjAXBogsmBJB18drD91V1rWpam7E+oK6gtcNU2vG4UCUY/rYTAe6o2iA1RAEUsQL44nP3K2FCBp4Y1ijHhhec6j67Nwsk2pfZDTVEyUUsGj9qJFdU+5uxLqOeWiDMJ5mdCqZ/bvwSEIGnSxPWA87ToSnx00EXIHrE7TWRNvUep9SnWS1n8/EtiH2RUF2/pW7ijfRM8oAjnfiGCKQWqPW0Si+TzYdnPXWBeSzgLDEBNXBziPYbUBDNCN7eqPjelaj0TLxCvfLFsas2oYc7QfI2rBRzh43Ff8xsUFf+nkYiFC8RyfhNU7g/xoMU/ZoUWM+dXMRbRG0qN3vF9ZOlOPxJtUA9IaYP3kwmBJJNL6MbhqDX/W7nGfzyRyeMuEAhptYZr0T5rqnIK0rK0M/HXWBGch+WXdvpWPWjEBdKQs5Fy6ZqTq7nQjjdOLSRN1ebE0djBB7Ymo3CCu/TI59hok3GkYlA5R0ij/xAMnNcZKBzmBn3uhte42kV9f8gNaGXQbnadkCZ20NSRoEnP4P/p6i3Aek+vOaAdGRqsp4dS9q9AHJc3gEgIvA7mry5r+7letHPEYnfMMqeIuetHGo1+t8yBfRAX1xHSzmZTlHwJ3sM0lGSHomgViNX+aqrsHYfzZMi8MGZqx1dy7P+vP WzWng1wt R8iQmCwsrC/UUt/o/dtwRar+l1UG4H6RTYsuX8JFuwvkVrHUFOq8w/mLxgDX5/7BBPWxAjNTFX2hMg5mfSKI+dlRJtQnByhu7B520uzkEEcPZCZVIFr8oJtC92PHAAAXUmZOEefECsTGI3s5MTnMnFQBXiIK2SFi4E67f/pBvBu0aADSPMSEGPxvWvB+7cpBR8WXQO0mbMiX+ngQIlhAi7XI0Fe16arQe0ZU+qDg6E2AU/z+nduAswt4M+T01mA2auSr3fWmWkhHL5aDBlB8gFT8DZGkn1fRWoZASE9kyy8WRQoomGEI4Ll0VmVlGRo28wu4XwkQT+NMTz3OcbfsyonVYMr1GNn2Tsd3//JCLCDJKzS4= 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: Hi Vlastimil, On Mon, 17 Feb 2025 at 09:49, Vlastimil Babka wrote: > > On 2/11/25 13:11, Fuad Tabba wrote: > > 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 next patch. This will be used in > > It's not clear to me how the code in the next page is facilitated as it > doesn't use any of this? I'm sorry about that, I'm missing the word "series". i.e., > > 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 next patch *series*. I'm referring to this series: https://lore.kernel.org/all/20250117163001.2326672-1-tabba@google.com/ > > 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. > > > > Signed-off-by: Fuad Tabba > > --- > > include/linux/kvm_host.h | 9 +++++++++ > > include/linux/page-flags.h | 17 +++++++++++++++++ > > mm/debug.c | 1 + > > mm/swap.c | 9 +++++++++ > > virt/kvm/guest_memfd.c | 7 +++++++ > > 5 files changed, 43 insertions(+) > > > > diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h > > index f34f4cfaa513..8b5f28f6efff 100644 > > --- a/include/linux/kvm_host.h > > +++ b/include/linux/kvm_host.h > > @@ -2571,4 +2571,13 @@ long kvm_arch_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu, > > struct kvm_pre_fault_memory *range); > > #endif > > > > +#ifdef CONFIG_KVM_GMEM_SHARED_MEM > > +void kvm_gmem_handle_folio_put(struct folio *folio); > > +#else > > +static inline void kvm_gmem_handle_folio_put(struct folio *folio) > > +{ > > + WARN_ON_ONCE(1); > > +} > > Since the caller is guarded by CONFIG_KVM_GMEM_SHARED_MEM, do we need the > CONFIG_KVM_GMEM_SHARED_MEM=n variant at all? No. I'll remove it. > > +#endif > > + > > #endif > > diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h > > index 6dc2494bd002..734afda268ab 100644 > > --- a/include/linux/page-flags.h > > +++ b/include/linux/page-flags.h > > @@ -933,6 +933,17 @@ enum pagetype { > > PGTY_slab = 0xf5, > > PGTY_zsmalloc = 0xf6, > > PGTY_unaccepted = 0xf7, > > + /* > > + * 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 > > Might be a bit misleading as I don't think it's set yet as of this series. > But I guess we can keep it to avoid another update later. You're right, it's not in this series. But as you said, the idea is to have the least amount of churn in the core mm code. > > + * always setting that type once typed folios can be mapped to user > > + * space cleanly. > > + */ > > + PGTY_guestmem = 0xf8, > > > > PGTY_mapcount_underflow = 0xff > > }; > > @@ -1082,6 +1093,12 @@ FOLIO_TYPE_OPS(hugetlb, hugetlb) > > FOLIO_TEST_FLAG_FALSE(hugetlb) > > #endif > > > > +#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 > > Do we need to guard the include? Yes, otherwise allnoconfig complains due to many of the x86 things it drags along if included but KVM isn't configured. I could put it in a different header that doesn't have this problem, but I couldn't think of a better header for this. Thank you, /fuad > > + > > #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/guest_memfd.c b/virt/kvm/guest_memfd.c > > index b2aa6bf24d3a..c6f6792bec2a 100644 > > --- a/virt/kvm/guest_memfd.c > > +++ b/virt/kvm/guest_memfd.c > > @@ -312,6 +312,13 @@ static pgoff_t kvm_gmem_get_index(struct kvm_memory_slot *slot, gfn_t gfn) > > return gfn - slot->base_gfn + slot->gmem.pgoff; > > } > > > > +#ifdef CONFIG_KVM_GMEM_SHARED_MEM > > +void kvm_gmem_handle_folio_put(struct folio *folio) > > +{ > > + WARN_ONCE(1, "A placeholder that shouldn't trigger. Work in progress."); > > +} > > +#endif /* CONFIG_KVM_GMEM_SHARED_MEM */ > > + > > static struct file_operations kvm_gmem_fops = { > > .open = generic_file_open, > > .release = kvm_gmem_release, >