From nobody Thu Dec 18 15:31:58 2025 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9ABB225C703 for ; Wed, 14 May 2025 23:43:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747266237; cv=none; b=DAONtRcAG5W3Pj9r9TK1CWZ30DNbmYlmg7e1dUzMJPbA+oA725Jb/rMEO4DqOT2p9MEqVjDzzBBz54hZPBtnNTKXtBPoj9920pvwpXyVA0d5wemsO7h+lvKUPoXqHwlzznphEOwv6t6vqX6Gzt2SrYmO6oy99nURyNUjpnZW56Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747266237; c=relaxed/simple; bh=IMiKEMd6OxkBoCNVWLOzENvmPQZSLrc1NWT7HaivxWo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=JlpCMwUC2M6GKrqHXF441sd6mGWnaB28+ZlVq0M53QQ6lYMukmh4k7z0hMrDCCVDOHFvwtjpz/m1fSO0D/LIR50zfFK7vBHJ3QAwAzaa9DlDabAvOECZqZBeizeNF3JZNL3iMwsuL6pgHRoIYKtBn+EH3wyLjt4MQ00hnJPRI60= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ackerleytng.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=lWpVxQE4; arc=none smtp.client-ip=209.85.210.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ackerleytng.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="lWpVxQE4" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-74292762324so272254b3a.0 for ; Wed, 14 May 2025 16:43:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747266235; x=1747871035; darn=vger.kernel.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=uuq1bEexdRhfFEX1jCABHc+UNgsIInH8N9P0pZ09IXA=; b=lWpVxQE4i8lmzDx5Y94IGz4ODljbyX6Edr1/iYC4JCNTqGAzQ6GikucSHNnRdq0k9N wODK+Rtr5JQD3u7Q4n1hTrv1wMtqWIsI6jlbUXvyaBIzUK1CVwoWo00ULPQ5LC/wZqiS /bz3GW6jBJR7jEyjvg4tIouNzElyQ9t89PXCO6piR8SqKWCbJX3JZRXVQIbVHhmGrzTR EXveltb979x2WHXeqroFn69h5lENXc+dbWBkAiJzhre355kM2mZbge3awmsRNQ2huF6w A/wQlyKBLnINIQSFc6jVv4srKLs4mHAC1wZXBUH4Ml4webkymYKUtL1aKuFuz6lkdWst nnyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747266235; x=1747871035; 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=uuq1bEexdRhfFEX1jCABHc+UNgsIInH8N9P0pZ09IXA=; b=YM6/rjz9+L301VYu6XCqCurpaXvDYvsi0KZn/c/7mnqQ7800vb7/vJ6h+nosdnOKYq 60czeVxN1SxfthZGqaq0bi0EguwSR26QK0mbhZOtNp+7XEdcwMIE7V+C9TgC2ZC8qnDg s8S7hBCs5WwxApKc5nq7OQMUBS7qMyI//lOh7akNvytks8R9naB7Aa6SslRnnOaYQnwe Ctghxy4O4mf/mfbNoLO5VuDkMOk0smmNzaRUbbX4iS0rbP9n6JIxbUr6fqFem8MsuUF2 lwtRa0W1Y+n6xGsAI00B+L8fThOZo9SLWeTcrv0BRziwdjjI4rWQhZ5xNjZPg+UOj3+h IzJw== X-Forwarded-Encrypted: i=1; AJvYcCVfyStszW/vnyyn+ucqcgsCQ58576hb06eHc9bJOGXvCMgIY3iP5jWPS11lQh8yOMhGd7OF0sRJKdRzECE=@vger.kernel.org X-Gm-Message-State: AOJu0YwRCh9LREEdQrh0pndmcSwF9yCdmaF770mI1WMkRwLLP3p4ItGL HGKbu4iC8vLCHFGp6ozNM5KomS1PEtid5dHDPhbnj9FwGET1kvXAID/6Se58FXJBTmN3QKDgZEj 5KkThgLwJ02eTvpjsxxHDZQ== X-Google-Smtp-Source: AGHT+IEP7NX45yVSRggtsTELkRXbv8SEWvt+7ORsl2x8HhwmdlC6ogf+zGJHadz5Krd2YgHwh61qzF+l2O1bbO5BGw== X-Received: from pfmv16.prod.google.com ([2002:a62:a510:0:b0:736:3cd5:ba3f]) (user=ackerleytng job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:181c:b0:740:9d7c:aeb9 with SMTP id d2e1a72fcca58-74289377cb0mr7546070b3a.21.1747266234724; Wed, 14 May 2025 16:43:54 -0700 (PDT) Date: Wed, 14 May 2025 16:42:16 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.49.0.1045.g170613ef41-goog Message-ID: Subject: [RFC PATCH v2 37/51] filemap: Pass address_space mapping to ->free_folio() From: Ackerley Tng To: kvm@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, x86@kernel.org, linux-fsdevel@vger.kernel.org Cc: ackerleytng@google.com, aik@amd.com, ajones@ventanamicro.com, akpm@linux-foundation.org, amoorthy@google.com, anthony.yznaga@oracle.com, anup@brainfault.org, aou@eecs.berkeley.edu, bfoster@redhat.com, binbin.wu@linux.intel.com, brauner@kernel.org, catalin.marinas@arm.com, chao.p.peng@intel.com, chenhuacai@kernel.org, dave.hansen@intel.com, david@redhat.com, dmatlack@google.com, dwmw@amazon.co.uk, erdemaktas@google.com, fan.du@intel.com, fvdl@google.com, graf@amazon.com, haibo1.xu@intel.com, hch@infradead.org, hughd@google.com, ira.weiny@intel.com, isaku.yamahata@intel.com, jack@suse.cz, james.morse@arm.com, jarkko@kernel.org, jgg@ziepe.ca, jgowans@amazon.com, jhubbard@nvidia.com, jroedel@suse.de, jthoughton@google.com, jun.miao@intel.com, kai.huang@intel.com, keirf@google.com, kent.overstreet@linux.dev, kirill.shutemov@intel.com, liam.merwick@oracle.com, maciej.wieczor-retman@intel.com, mail@maciej.szmigiero.name, maz@kernel.org, mic@digikod.net, michael.roth@amd.com, mpe@ellerman.id.au, muchun.song@linux.dev, nikunj@amd.com, nsaenz@amazon.es, oliver.upton@linux.dev, palmer@dabbelt.com, pankaj.gupta@amd.com, paul.walmsley@sifive.com, pbonzini@redhat.com, pdurrant@amazon.co.uk, peterx@redhat.com, pgonda@google.com, pvorel@suse.cz, qperret@google.com, quic_cvanscha@quicinc.com, quic_eberman@quicinc.com, quic_mnalajal@quicinc.com, quic_pderrin@quicinc.com, quic_pheragu@quicinc.com, quic_svaddagi@quicinc.com, quic_tsoni@quicinc.com, richard.weiyang@gmail.com, rick.p.edgecombe@intel.com, rientjes@google.com, roypat@amazon.co.uk, rppt@kernel.org, seanjc@google.com, shuah@kernel.org, steven.price@arm.com, steven.sistare@oracle.com, suzuki.poulose@arm.com, tabba@google.com, thomas.lendacky@amd.com, usama.arif@bytedance.com, vannapurve@google.com, vbabka@suse.cz, viro@zeniv.linux.org.uk, vkuznets@redhat.com, wei.w.wang@intel.com, will@kernel.org, willy@infradead.org, xiaoyao.li@intel.com, yan.y.zhao@intel.com, yilun.xu@intel.com, yuzenghui@huawei.com, zhiquan1.li@intel.com, Mike Day Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Elliot Berman The plan is to be able to support multiple allocators for guest_memfd folios. To allow each allocator to handle release of a folio from a guest_memfd filemap, ->free_folio() needs to retrieve allocator information that is stored on the guest_memfd inode. ->free_folio() shouldn't assume that folio->mapping is set/valid, and the mapping is well-known to callers of .free_folio(). Hence, pass address_space mapping to ->free_folio() for the callback to retrieve any necessary information. Link: https://lore.kernel.org/all/15f665b4-2d33-41ca-ac50-fafe24ade32f@redh= at.com/ Suggested-by: David Hildenbrand Acked-by: David Hildenbrand Change-Id: I8bac907832a0b2491fa403a6ab72fcef1b4713ee Signed-off-by: Elliot Berman Tested-by: Mike Day Signed-off-by: Ackerley Tng --- Documentation/filesystems/locking.rst | 2 +- Documentation/filesystems/vfs.rst | 15 +++++++++------ fs/nfs/dir.c | 9 +++++++-- fs/orangefs/inode.c | 3 ++- include/linux/fs.h | 2 +- mm/filemap.c | 9 +++++---- mm/secretmem.c | 3 ++- mm/vmscan.c | 4 ++-- virt/kvm/guest_memfd.c | 3 ++- 9 files changed, 31 insertions(+), 19 deletions(-) diff --git a/Documentation/filesystems/locking.rst b/Documentation/filesyst= ems/locking.rst index 0ec0bb6eb0fb..c3d7430481ae 100644 --- a/Documentation/filesystems/locking.rst +++ b/Documentation/filesystems/locking.rst @@ -263,7 +263,7 @@ prototypes:: sector_t (*bmap)(struct address_space *, sector_t); void (*invalidate_folio) (struct folio *, size_t start, size_t len); bool (*release_folio)(struct folio *, gfp_t); - void (*free_folio)(struct folio *); + void (*free_folio)(struct address_space *, struct folio *); int (*direct_IO)(struct kiocb *, struct iov_iter *iter); int (*migrate_folio)(struct address_space *, struct folio *dst, struct folio *src, enum migrate_mode); diff --git a/Documentation/filesystems/vfs.rst b/Documentation/filesystems/= vfs.rst index ae79c30b6c0c..bba1ac848f96 100644 --- a/Documentation/filesystems/vfs.rst +++ b/Documentation/filesystems/vfs.rst @@ -833,7 +833,7 @@ cache in your filesystem. The following members are de= fined: sector_t (*bmap)(struct address_space *, sector_t); void (*invalidate_folio) (struct folio *, size_t start, size_t len); bool (*release_folio)(struct folio *, gfp_t); - void (*free_folio)(struct folio *); + void (*free_folio)(struct address_space *, struct folio *); ssize_t (*direct_IO)(struct kiocb *, struct iov_iter *iter); int (*migrate_folio)(struct mapping *, struct folio *dst, struct folio *src, enum migrate_mode); @@ -1011,11 +1011,14 @@ cache in your filesystem. The following members ar= e defined: clear the uptodate flag if it cannot free private data yet. =20 ``free_folio`` - free_folio is called once the folio is no longer visible in the - page cache in order to allow the cleanup of any private data. - Since it may be called by the memory reclaimer, it should not - assume that the original address_space mapping still exists, and - it should not block. + free_folio is called once the folio is no longer visible in + the page cache in order to allow the cleanup of any private + data. Since it may be called by the memory reclaimer, it + should not assume that the original address_space mapping + still exists at folio->mapping. The mapping the folio used to + belong to is instead passed for free_folio to read any + information it might need from the mapping. free_folio should + not block. =20 ``direct_IO`` called by the generic read/write routines to perform direct_IO - diff --git a/fs/nfs/dir.c b/fs/nfs/dir.c index bd23fc736b39..148433f6d9d4 100644 --- a/fs/nfs/dir.c +++ b/fs/nfs/dir.c @@ -55,7 +55,7 @@ static int nfs_closedir(struct inode *, struct file *); static int nfs_readdir(struct file *, struct dir_context *); static int nfs_fsync_dir(struct file *, loff_t, loff_t, int); static loff_t nfs_llseek_dir(struct file *, loff_t, int); -static void nfs_readdir_clear_array(struct folio *); +static void nfs_free_folio(struct address_space *, struct folio *); static int nfs_do_create(struct inode *dir, struct dentry *dentry, umode_t mode, int open_flags); =20 @@ -69,7 +69,7 @@ const struct file_operations nfs_dir_operations =3D { }; =20 const struct address_space_operations nfs_dir_aops =3D { - .free_folio =3D nfs_readdir_clear_array, + .free_folio =3D nfs_free_folio, }; =20 #define NFS_INIT_DTSIZE PAGE_SIZE @@ -230,6 +230,11 @@ static void nfs_readdir_clear_array(struct folio *foli= o) kunmap_local(array); } =20 +static void nfs_free_folio(struct address_space *mapping, struct folio *fo= lio) +{ + nfs_readdir_clear_array(folio); +} + static void nfs_readdir_folio_reinit_array(struct folio *folio, u64 last_c= ookie, u64 change_attr) { diff --git a/fs/orangefs/inode.c b/fs/orangefs/inode.c index 5ac743c6bc2e..884cc5295f3e 100644 --- a/fs/orangefs/inode.c +++ b/fs/orangefs/inode.c @@ -449,7 +449,8 @@ static bool orangefs_release_folio(struct folio *folio,= gfp_t foo) return !folio_test_private(folio); } =20 -static void orangefs_free_folio(struct folio *folio) +static void orangefs_free_folio(struct address_space *mapping, + struct folio *folio) { kfree(folio_detach_private(folio)); } diff --git a/include/linux/fs.h b/include/linux/fs.h index 0fded2e3c661..9862ea92a2af 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -455,7 +455,7 @@ struct address_space_operations { sector_t (*bmap)(struct address_space *, sector_t); void (*invalidate_folio) (struct folio *, size_t offset, size_t len); bool (*release_folio)(struct folio *, gfp_t); - void (*free_folio)(struct folio *folio); + void (*free_folio)(struct address_space *mapping, struct folio *folio); ssize_t (*direct_IO)(struct kiocb *, struct iov_iter *iter); /* * migrate the contents of a folio to the specified target. If diff --git a/mm/filemap.c b/mm/filemap.c index bed7160db214..a02c3d8e00e8 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -226,11 +226,11 @@ void __filemap_remove_folio(struct folio *folio, void= *shadow) =20 void filemap_free_folio(struct address_space *mapping, struct folio *folio) { - void (*free_folio)(struct folio *); + void (*free_folio)(struct address_space*, struct folio *); =20 free_folio =3D mapping->a_ops->free_folio; if (free_folio) - free_folio(folio); + free_folio(mapping, folio); =20 folio_put_refs(folio, folio_nr_pages(folio)); } @@ -820,7 +820,8 @@ EXPORT_SYMBOL(file_write_and_wait_range); void replace_page_cache_folio(struct folio *old, struct folio *new) { struct address_space *mapping =3D old->mapping; - void (*free_folio)(struct folio *) =3D mapping->a_ops->free_folio; + void (*free_folio)(struct address_space *, struct folio *) =3D + mapping->a_ops->free_folio; pgoff_t offset =3D old->index; XA_STATE(xas, &mapping->i_pages, offset); =20 @@ -849,7 +850,7 @@ void replace_page_cache_folio(struct folio *old, struct= folio *new) __lruvec_stat_add_folio(new, NR_SHMEM); xas_unlock_irq(&xas); if (free_folio) - free_folio(old); + free_folio(mapping, old); folio_put(old); } EXPORT_SYMBOL_GPL(replace_page_cache_folio); diff --git a/mm/secretmem.c b/mm/secretmem.c index c0e459e58cb6..178507c1b900 100644 --- a/mm/secretmem.c +++ b/mm/secretmem.c @@ -152,7 +152,8 @@ static int secretmem_migrate_folio(struct address_space= *mapping, return -EBUSY; } =20 -static void secretmem_free_folio(struct folio *folio) +static void secretmem_free_folio(struct address_space *mapping, + struct folio *folio) { set_direct_map_default_noflush(&folio->page); folio_zero_segment(folio, 0, folio_size(folio)); diff --git a/mm/vmscan.c b/mm/vmscan.c index 3783e45bfc92..b8add4d0cf18 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -788,7 +788,7 @@ static int __remove_mapping(struct address_space *mappi= ng, struct folio *folio, xa_unlock_irq(&mapping->i_pages); put_swap_folio(folio, swap); } else { - void (*free_folio)(struct folio *); + void (*free_folio)(struct address_space *, struct folio *); =20 free_folio =3D mapping->a_ops->free_folio; /* @@ -817,7 +817,7 @@ static int __remove_mapping(struct address_space *mappi= ng, struct folio *folio, spin_unlock(&mapping->host->i_lock); =20 if (free_folio) - free_folio(folio); + free_folio(mapping, folio); } =20 return 1; diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 24d270b9b725..c578d0ebe314 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -1319,7 +1319,8 @@ static void kvm_gmem_invalidate(struct folio *folio) static inline void kvm_gmem_invalidate(struct folio *folio) {} #endif =20 -static void kvm_gmem_free_folio(struct folio *folio) +static void kvm_gmem_free_folio(struct address_space *mapping, + struct folio *folio) { folio_clear_unevictable(folio); =20 --=20 2.49.0.1045.g170613ef41-goog