From: Shivansh Dhiman <shivansh.dhiman@amd.com>
Add NUMA mempolicy support to the filemap allocation path by introducing
new APIs that take a mempolicy argument:
- filemap_grab_folio_mpol()
- filemap_alloc_folio_mpol()
- __filemap_get_folio_mpol()
These APIs allow callers to specify a NUMA policy during page cache
allocations, enabling fine-grained control over memory placement. This is
particularly needed by KVM when using guest-memfd memory backends, where
the guest memory needs to be allocated according to the NUMA policy
specified by VMM.
The existing non-mempolicy APIs remain unchanged and continue to use the
default allocation behavior.
Signed-off-by: Shivansh Dhiman <shivansh.dhiman@amd.com>
Co-developed-by: Shivank Garg <shivankg@amd.com>
Signed-off-by: Shivank Garg <shivankg@amd.com>
---
include/linux/pagemap.h | 41 +++++++++++++++++++++++++++++++++++++++++
mm/filemap.c | 27 +++++++++++++++++++++++----
2 files changed, 64 insertions(+), 4 deletions(-)
diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h
index e63fbfbd5b0f..6558c672740d 100644
--- a/include/linux/pagemap.h
+++ b/include/linux/pagemap.h
@@ -647,15 +647,24 @@ static inline void *detach_page_private(struct page *page)
#ifdef CONFIG_NUMA
struct folio *filemap_alloc_folio_noprof(gfp_t gfp, unsigned int order);
+struct folio *filemap_alloc_folio_mpol_noprof(gfp_t gfp, unsigned int order,
+ struct mempolicy *mpol, pgoff_t ilx);
#else
static inline struct folio *filemap_alloc_folio_noprof(gfp_t gfp, unsigned int order)
{
return folio_alloc_noprof(gfp, order);
}
+static inline struct folio *filemap_alloc_folio_mpol_noprof(gfp_t gfp,
+ unsigned int order, struct mempolicy *mpol, pgoff_t ilx)
+{
+ return filemap_alloc_folio_noprof(gfp, order);
+}
#endif
#define filemap_alloc_folio(...) \
alloc_hooks(filemap_alloc_folio_noprof(__VA_ARGS__))
+#define filemap_alloc_folio_mpol(...) \
+ alloc_hooks(filemap_alloc_folio_mpol_noprof(__VA_ARGS__))
static inline struct page *__page_cache_alloc(gfp_t gfp)
{
@@ -747,6 +756,8 @@ static inline fgf_t fgf_set_order(size_t size)
void *filemap_get_entry(struct address_space *mapping, pgoff_t index);
struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index,
fgf_t fgp_flags, gfp_t gfp);
+struct folio *__filemap_get_folio_mpol(struct address_space *mapping,
+ pgoff_t index, fgf_t fgp_flags, gfp_t gfp, struct mempolicy *mpol, pgoff_t ilx);
struct page *pagecache_get_page(struct address_space *mapping, pgoff_t index,
fgf_t fgp_flags, gfp_t gfp);
@@ -805,6 +816,36 @@ static inline struct folio *filemap_grab_folio(struct address_space *mapping,
mapping_gfp_mask(mapping));
}
+/**
+ * filemap_grab_folio_mpol - grab a folio from the page cache.
+ * @mapping: The address space to search.
+ * @index: The page index.
+ * @mpol: The mempolicy to apply when allocating a new folio.
+ * @ilx: The interleave index, for use only with MPOL_INTERLEAVE or
+ * MPOL_WEIGHTED_INTERLEAVE.
+ *
+ * Same as filemap_grab_folio(), except that it allocates the folio using
+ * given memory policy.
+ *
+ * Return: A found or created folio. ERR_PTR(-ENOMEM) if no folio is found
+ * and failed to create a folio.
+ */
+#ifdef CONFIG_NUMA
+static inline struct folio *filemap_grab_folio_mpol(struct address_space *mapping,
+ pgoff_t index, struct mempolicy *mpol, pgoff_t ilx)
+{
+ return __filemap_get_folio_mpol(mapping, index,
+ FGP_LOCK | FGP_ACCESSED | FGP_CREAT,
+ mapping_gfp_mask(mapping), mpol, ilx);
+}
+#else
+static inline struct folio *filemap_grab_folio_mpol(struct address_space *mapping,
+ pgoff_t index, struct mempolicy *mpol, pgoff_t ilx)
+{
+ return filemap_grab_folio(mapping, index);
+}
+#endif /* CONFIG_NUMA */
+
/**
* find_get_page - find and get a page reference
* @mapping: the address_space to search
diff --git a/mm/filemap.c b/mm/filemap.c
index bada249b9fb7..c7e913b91636 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -1007,6 +1007,15 @@ struct folio *filemap_alloc_folio_noprof(gfp_t gfp, unsigned int order)
return folio_alloc_noprof(gfp, order);
}
EXPORT_SYMBOL(filemap_alloc_folio_noprof);
+
+struct folio *filemap_alloc_folio_mpol_noprof(gfp_t gfp, unsigned int order,
+ struct mempolicy *mpol, pgoff_t ilx)
+{
+ if (mpol)
+ return folio_alloc_mpol_noprof(gfp, order, mpol,
+ ilx, numa_node_id());
+ return filemap_alloc_folio_noprof(gfp, order);
+}
#endif
/*
@@ -1891,11 +1900,14 @@ void *filemap_get_entry(struct address_space *mapping, pgoff_t index)
}
/**
- * __filemap_get_folio - Find and get a reference to a folio.
+ * __filemap_get_folio_mpol - Find and get a reference to a folio.
* @mapping: The address_space to search.
* @index: The page index.
* @fgp_flags: %FGP flags modify how the folio is returned.
* @gfp: Memory allocation flags to use if %FGP_CREAT is specified.
+ * @mpol: The mempolicy to apply when allocating a new folio.
+ * @ilx: The interleave index, for use only with MPOL_INTERLEAVE or
+ * MPOL_WEIGHTED_INTERLEAVE.
*
* Looks up the page cache entry at @mapping & @index.
*
@@ -1906,8 +1918,8 @@ void *filemap_get_entry(struct address_space *mapping, pgoff_t index)
*
* Return: The found folio or an ERR_PTR() otherwise.
*/
-struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index,
- fgf_t fgp_flags, gfp_t gfp)
+struct folio *__filemap_get_folio_mpol(struct address_space *mapping, pgoff_t index,
+ fgf_t fgp_flags, gfp_t gfp, struct mempolicy *mpol, pgoff_t ilx)
{
struct folio *folio;
@@ -1977,7 +1989,7 @@ struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index,
err = -ENOMEM;
if (order > min_order)
alloc_gfp |= __GFP_NORETRY | __GFP_NOWARN;
- folio = filemap_alloc_folio(alloc_gfp, order);
+ folio = filemap_alloc_folio_mpol(alloc_gfp, order, mpol, ilx);
if (!folio)
continue;
@@ -2024,6 +2036,13 @@ struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index,
folio_clear_dropbehind(folio);
return folio;
}
+EXPORT_SYMBOL_GPL(__filemap_get_folio_mpol);
+
+struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index,
+ fgf_t fgp_flags, gfp_t gfp)
+{
+ return __filemap_get_folio_mpol(mapping, index, fgp_flags, gfp, NULL, 0);
+}
EXPORT_SYMBOL(__filemap_get_folio);
static inline struct folio *find_get_entry(struct xa_state *xas, pgoff_t max,
--
2.43.0
On Wed, Jun 18, 2025 at 11:29:31AM +0000, Shivank Garg wrote: > From: Shivansh Dhiman <shivansh.dhiman@amd.com> > > Add NUMA mempolicy support to the filemap allocation path by introducing > new APIs that take a mempolicy argument: > - filemap_grab_folio_mpol() > - filemap_alloc_folio_mpol() > - __filemap_get_folio_mpol() You don't use these APIs in this series, so I can't evaludate whether any of my suggestiosn for improving this patch would actually work. NACK. Introduce the APIs *with a user*. Come on, this isn't a new requirement.
On 6/19/2025 9:33 PM, Matthew Wilcox wrote: > On Wed, Jun 18, 2025 at 11:29:31AM +0000, Shivank Garg wrote: >> From: Shivansh Dhiman <shivansh.dhiman@amd.com> >> >> Add NUMA mempolicy support to the filemap allocation path by introducing >> new APIs that take a mempolicy argument: >> - filemap_grab_folio_mpol() >> - filemap_alloc_folio_mpol() >> - __filemap_get_folio_mpol() > > You don't use these APIs in this series, so I can't evaludate whether > any of my suggestiosn for improving this patch would actually work. > NACK. Introduce the APIs *with a user*. Come on, this isn't a new > requirement. Hi willy, Thank you for the feedback. filemap_grab_folio_mpol() is used in [Patch 6/7] in kvm_gmem_prepare_folio(). filemap_alloc_folio_mpol() and __filemap_get_folio_mpol()) are internally used to support the filemap_grab_folio_mpol(). Thanks, Shivank
On Fri, Jun 20, 2025 at 11:29:20AM +0530, Shivank Garg wrote: > filemap_grab_folio_mpol() is used in [Patch 6/7] in kvm_gmem_prepare_folio(). > > filemap_alloc_folio_mpol() and __filemap_get_folio_mpol()) are internally used > to support the filemap_grab_folio_mpol(). That's not better. We don't add unused functions, and unless there's something coming that's going to use them, the entire structure of this is wrong. filemap_grab_folio() is a convenience function that avoids us having to specify the other two arguments to __filemap_get_folio(). Since there's no indication at this point that there are going to be more callers of it, filemap_grab_folio_mpol() should not even exist. I'll send a pair of patches which should be sufficient for your needs.
On 6/20/2025 8:04 PM, Matthew Wilcox wrote: > On Fri, Jun 20, 2025 at 11:29:20AM +0530, Shivank Garg wrote: >> filemap_grab_folio_mpol() is used in [Patch 6/7] in kvm_gmem_prepare_folio(). >> >> filemap_alloc_folio_mpol() and __filemap_get_folio_mpol()) are internally used >> to support the filemap_grab_folio_mpol(). > > That's not better. We don't add unused functions, and unless there's > something coming that's going to use them, the entire structure of this > is wrong. > > filemap_grab_folio() is a convenience function that avoids us having to > specify the other two arguments to __filemap_get_folio(). Since there's > no indication at this point that there are going to be more callers of > it, filemap_grab_folio_mpol() should not even exist. > > I'll send a pair of patches which should be sufficient for your needs. Thank you willy :) I'll them add to my series. Thanks, Shivank
On Fri, Jun 20, 2025 at 08:22:49PM +0530, Shivank Garg wrote: > > > On 6/20/2025 8:04 PM, Matthew Wilcox wrote: > > On Fri, Jun 20, 2025 at 11:29:20AM +0530, Shivank Garg wrote: > >> filemap_grab_folio_mpol() is used in [Patch 6/7] in kvm_gmem_prepare_folio(). > >> > >> filemap_alloc_folio_mpol() and __filemap_get_folio_mpol()) are internally used > >> to support the filemap_grab_folio_mpol(). > > > > That's not better. We don't add unused functions, and unless there's > > something coming that's going to use them, the entire structure of this > > is wrong. > > > > filemap_grab_folio() is a convenience function that avoids us having to > > specify the other two arguments to __filemap_get_folio(). Since there's > > no indication at this point that there are going to be more callers of > > it, filemap_grab_folio_mpol() should not even exist. > > > > I'll send a pair of patches which should be sufficient for your needs. > > Thank you willy :) > I'll them add to my series. Thanks. You probably want to touch up the commit messages, I didn't spend very long on them.
On 6/20/25 07:59, Shivank Garg wrote: > > > On 6/19/2025 9:33 PM, Matthew Wilcox wrote: >> On Wed, Jun 18, 2025 at 11:29:31AM +0000, Shivank Garg wrote: >>> From: Shivansh Dhiman <shivansh.dhiman@amd.com> >>> >>> Add NUMA mempolicy support to the filemap allocation path by introducing >>> new APIs that take a mempolicy argument: >>> - filemap_grab_folio_mpol() >>> - filemap_alloc_folio_mpol() >>> - __filemap_get_folio_mpol() >> >> You don't use these APIs in this series, so I can't evaludate whether >> any of my suggestiosn for improving this patch would actually work. >> NACK. Introduce the APIs *with a user*. Come on, this isn't a new >> requirement. > > Hi willy, > > Thank you for the feedback. > > filemap_grab_folio_mpol() is used in [Patch 6/7] in kvm_gmem_prepare_folio(). > > filemap_alloc_folio_mpol() and __filemap_get_folio_mpol()) are internally used > to support the filemap_grab_folio_mpol(). Maybe they can be static then and don't need to be declared in the header. > Thanks, > Shivank
On 6/18/25 13:29, Shivank Garg wrote: > From: Shivansh Dhiman <shivansh.dhiman@amd.com> > > Add NUMA mempolicy support to the filemap allocation path by introducing > new APIs that take a mempolicy argument: > - filemap_grab_folio_mpol() > - filemap_alloc_folio_mpol() > - __filemap_get_folio_mpol() > > These APIs allow callers to specify a NUMA policy during page cache > allocations, enabling fine-grained control over memory placement. This is > particularly needed by KVM when using guest-memfd memory backends, where > the guest memory needs to be allocated according to the NUMA policy > specified by VMM. > > The existing non-mempolicy APIs remain unchanged and continue to use the > default allocation behavior. > > Signed-off-by: Shivansh Dhiman <shivansh.dhiman@amd.com> > Co-developed-by: Shivank Garg <shivankg@amd.com> > Signed-off-by: Shivank Garg <shivankg@amd.com> I think __filemap_get_folio() could become a static inline wrapper for __filemap_get_folio_mpol in pagemap.h. Otherwise, Acked-by: Vlastimil Babka <vbabka@suse.cz>
guest_memfd needs to support memory policies so add an argument
to filemap_alloc_folio(). All existing users pass NULL, the first
user will show up later in this series.
Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
---
fs/bcachefs/fs-io-buffered.c | 2 +-
fs/btrfs/compression.c | 3 ++-
fs/btrfs/verity.c | 2 +-
fs/erofs/zdata.c | 2 +-
fs/f2fs/compress.c | 2 +-
include/linux/pagemap.h | 6 +++---
mm/filemap.c | 13 +++++++++----
mm/readahead.c | 2 +-
8 files changed, 19 insertions(+), 13 deletions(-)
diff --git a/fs/bcachefs/fs-io-buffered.c b/fs/bcachefs/fs-io-buffered.c
index 66bacdd49f78..392344232b16 100644
--- a/fs/bcachefs/fs-io-buffered.c
+++ b/fs/bcachefs/fs-io-buffered.c
@@ -124,7 +124,7 @@ static int readpage_bio_extend(struct btree_trans *trans,
if (folio && !xa_is_value(folio))
break;
- folio = filemap_alloc_folio(readahead_gfp_mask(iter->mapping), order);
+ folio = filemap_alloc_folio(readahead_gfp_mask(iter->mapping), order, NULL);
if (!folio)
break;
diff --git a/fs/btrfs/compression.c b/fs/btrfs/compression.c
index 48d07939fee4..8430ccf70887 100644
--- a/fs/btrfs/compression.c
+++ b/fs/btrfs/compression.c
@@ -475,7 +475,8 @@ static noinline int add_ra_bio_pages(struct inode *inode,
}
folio = filemap_alloc_folio(mapping_gfp_constraint(mapping,
- ~__GFP_FS), 0);
+ ~__GFP_FS),
+ 0, NULL);
if (!folio)
break;
diff --git a/fs/btrfs/verity.c b/fs/btrfs/verity.c
index b7a96a005487..c43a789ba6d2 100644
--- a/fs/btrfs/verity.c
+++ b/fs/btrfs/verity.c
@@ -742,7 +742,7 @@ static struct page *btrfs_read_merkle_tree_page(struct inode *inode,
}
folio = filemap_alloc_folio(mapping_gfp_constraint(inode->i_mapping, ~__GFP_FS),
- 0);
+ 0, NULL);
if (!folio)
return ERR_PTR(-ENOMEM);
diff --git a/fs/erofs/zdata.c b/fs/erofs/zdata.c
index fe8071844724..00e9160a0d24 100644
--- a/fs/erofs/zdata.c
+++ b/fs/erofs/zdata.c
@@ -562,7 +562,7 @@ static void z_erofs_bind_cache(struct z_erofs_frontend *fe)
* Allocate a managed folio for cached I/O, or it may be
* then filled with a file-backed folio for in-place I/O
*/
- newfolio = filemap_alloc_folio(gfp, 0);
+ newfolio = filemap_alloc_folio(gfp, 0, NULL);
if (!newfolio)
continue;
newfolio->private = Z_EROFS_PREALLOCATED_FOLIO;
diff --git a/fs/f2fs/compress.c b/fs/f2fs/compress.c
index b3c1df93a163..7ef937dd7624 100644
--- a/fs/f2fs/compress.c
+++ b/fs/f2fs/compress.c
@@ -1942,7 +1942,7 @@ void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
return;
}
- cfolio = filemap_alloc_folio(__GFP_NOWARN | __GFP_IO, 0);
+ cfolio = filemap_alloc_folio(__GFP_NOWARN | __GFP_IO, 0, NULL);
if (!cfolio)
return;
diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h
index e63fbfbd5b0f..c176aeeb38db 100644
--- a/include/linux/pagemap.h
+++ b/include/linux/pagemap.h
@@ -646,9 +646,9 @@ static inline void *detach_page_private(struct page *page)
}
#ifdef CONFIG_NUMA
-struct folio *filemap_alloc_folio_noprof(gfp_t gfp, unsigned int order);
+struct folio *filemap_alloc_folio_noprof(gfp_t gfp, unsigned int order, struct mempolicy *policy);
#else
-static inline struct folio *filemap_alloc_folio_noprof(gfp_t gfp, unsigned int order)
+static inline struct folio *filemap_alloc_folio_noprof(gfp_t gfp, unsigned int order, struct mempolicy *policy)
{
return folio_alloc_noprof(gfp, order);
}
@@ -659,7 +659,7 @@ static inline struct folio *filemap_alloc_folio_noprof(gfp_t gfp, unsigned int o
static inline struct page *__page_cache_alloc(gfp_t gfp)
{
- return &filemap_alloc_folio(gfp, 0)->page;
+ return &filemap_alloc_folio(gfp, 0, NULL)->page;
}
static inline gfp_t readahead_gfp_mask(struct address_space *x)
diff --git a/mm/filemap.c b/mm/filemap.c
index bada249b9fb7..a26df313207d 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -989,11 +989,16 @@ int filemap_add_folio(struct address_space *mapping, struct folio *folio,
EXPORT_SYMBOL_GPL(filemap_add_folio);
#ifdef CONFIG_NUMA
-struct folio *filemap_alloc_folio_noprof(gfp_t gfp, unsigned int order)
+struct folio *filemap_alloc_folio_noprof(gfp_t gfp, unsigned int order,
+ struct mempolicy *policy)
{
int n;
struct folio *folio;
+ if (policy)
+ return folio_alloc_mpol_noprof(gfp, order, policy,
+ NO_INTERLEAVE_INDEX, numa_node_id());
+
if (cpuset_do_page_mem_spread()) {
unsigned int cpuset_mems_cookie;
do {
@@ -1977,7 +1982,7 @@ struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index,
err = -ENOMEM;
if (order > min_order)
alloc_gfp |= __GFP_NORETRY | __GFP_NOWARN;
- folio = filemap_alloc_folio(alloc_gfp, order);
+ folio = filemap_alloc_folio(alloc_gfp, order, NULL);
if (!folio)
continue;
@@ -2516,7 +2521,7 @@ static int filemap_create_folio(struct kiocb *iocb, struct folio_batch *fbatch)
if (iocb->ki_flags & (IOCB_NOWAIT | IOCB_WAITQ))
return -EAGAIN;
- folio = filemap_alloc_folio(mapping_gfp_mask(mapping), min_order);
+ folio = filemap_alloc_folio(mapping_gfp_mask(mapping), min_order, NULL);
if (!folio)
return -ENOMEM;
if (iocb->ki_flags & IOCB_DONTCACHE)
@@ -3854,7 +3859,7 @@ static struct folio *do_read_cache_folio(struct address_space *mapping,
folio = filemap_get_folio(mapping, index);
if (IS_ERR(folio)) {
folio = filemap_alloc_folio(gfp,
- mapping_min_folio_order(mapping));
+ mapping_min_folio_order(mapping), NULL);
if (!folio)
return ERR_PTR(-ENOMEM);
index = mapping_align_index(mapping, index);
diff --git a/mm/readahead.c b/mm/readahead.c
index 20d36d6b055e..0b2aec0231e6 100644
--- a/mm/readahead.c
+++ b/mm/readahead.c
@@ -183,7 +183,7 @@ static struct folio *ractl_alloc_folio(struct readahead_control *ractl,
{
struct folio *folio;
- folio = filemap_alloc_folio(gfp_mask, order);
+ folio = filemap_alloc_folio(gfp_mask, order, NULL);
if (folio && ractl->dropbehind)
__folio_set_dropbehind(folio);
--
2.47.2
On 6/20/25 16:34, Matthew Wilcox (Oracle) wrote: > guest_memfd needs to support memory policies so add an argument > to filemap_alloc_folio(). All existing users pass NULL, the first > user will show up later in this series. > > Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org> Reviewed-by: Vlastimil Babka <vbabka@suse.cz>
> guest_memfd needs to support memory policies so add an argument
> to filemap_alloc_folio(). All existing users pass NULL, the first
> user will show up later in this series.
>
> Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
Reviewed-by: Pankaj Gupta <pankaj.gupta@amd.com>
> ---
> fs/bcachefs/fs-io-buffered.c | 2 +-
> fs/btrfs/compression.c | 3 ++-
> fs/btrfs/verity.c | 2 +-
> fs/erofs/zdata.c | 2 +-
> fs/f2fs/compress.c | 2 +-
> include/linux/pagemap.h | 6 +++---
> mm/filemap.c | 13 +++++++++----
> mm/readahead.c | 2 +-
> 8 files changed, 19 insertions(+), 13 deletions(-)
>
> diff --git a/fs/bcachefs/fs-io-buffered.c b/fs/bcachefs/fs-io-buffered.c
> index 66bacdd49f78..392344232b16 100644
> --- a/fs/bcachefs/fs-io-buffered.c
> +++ b/fs/bcachefs/fs-io-buffered.c
> @@ -124,7 +124,7 @@ static int readpage_bio_extend(struct btree_trans *trans,
> if (folio && !xa_is_value(folio))
> break;
>
> - folio = filemap_alloc_folio(readahead_gfp_mask(iter->mapping), order);
> + folio = filemap_alloc_folio(readahead_gfp_mask(iter->mapping), order, NULL);
> if (!folio)
> break;
>
> diff --git a/fs/btrfs/compression.c b/fs/btrfs/compression.c
> index 48d07939fee4..8430ccf70887 100644
> --- a/fs/btrfs/compression.c
> +++ b/fs/btrfs/compression.c
> @@ -475,7 +475,8 @@ static noinline int add_ra_bio_pages(struct inode *inode,
> }
>
> folio = filemap_alloc_folio(mapping_gfp_constraint(mapping,
> - ~__GFP_FS), 0);
> + ~__GFP_FS),
> + 0, NULL);
> if (!folio)
> break;
>
> diff --git a/fs/btrfs/verity.c b/fs/btrfs/verity.c
> index b7a96a005487..c43a789ba6d2 100644
> --- a/fs/btrfs/verity.c
> +++ b/fs/btrfs/verity.c
> @@ -742,7 +742,7 @@ static struct page *btrfs_read_merkle_tree_page(struct inode *inode,
> }
>
> folio = filemap_alloc_folio(mapping_gfp_constraint(inode->i_mapping, ~__GFP_FS),
> - 0);
> + 0, NULL);
> if (!folio)
> return ERR_PTR(-ENOMEM);
>
> diff --git a/fs/erofs/zdata.c b/fs/erofs/zdata.c
> index fe8071844724..00e9160a0d24 100644
> --- a/fs/erofs/zdata.c
> +++ b/fs/erofs/zdata.c
> @@ -562,7 +562,7 @@ static void z_erofs_bind_cache(struct z_erofs_frontend *fe)
> * Allocate a managed folio for cached I/O, or it may be
> * then filled with a file-backed folio for in-place I/O
> */
> - newfolio = filemap_alloc_folio(gfp, 0);
> + newfolio = filemap_alloc_folio(gfp, 0, NULL);
> if (!newfolio)
> continue;
> newfolio->private = Z_EROFS_PREALLOCATED_FOLIO;
> diff --git a/fs/f2fs/compress.c b/fs/f2fs/compress.c
> index b3c1df93a163..7ef937dd7624 100644
> --- a/fs/f2fs/compress.c
> +++ b/fs/f2fs/compress.c
> @@ -1942,7 +1942,7 @@ void f2fs_cache_compressed_page(struct f2fs_sb_info *sbi, struct page *page,
> return;
> }
>
> - cfolio = filemap_alloc_folio(__GFP_NOWARN | __GFP_IO, 0);
> + cfolio = filemap_alloc_folio(__GFP_NOWARN | __GFP_IO, 0, NULL);
> if (!cfolio)
> return;
>
> diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h
> index e63fbfbd5b0f..c176aeeb38db 100644
> --- a/include/linux/pagemap.h
> +++ b/include/linux/pagemap.h
> @@ -646,9 +646,9 @@ static inline void *detach_page_private(struct page *page)
> }
>
> #ifdef CONFIG_NUMA
> -struct folio *filemap_alloc_folio_noprof(gfp_t gfp, unsigned int order);
> +struct folio *filemap_alloc_folio_noprof(gfp_t gfp, unsigned int order, struct mempolicy *policy);
> #else
> -static inline struct folio *filemap_alloc_folio_noprof(gfp_t gfp, unsigned int order)
> +static inline struct folio *filemap_alloc_folio_noprof(gfp_t gfp, unsigned int order, struct mempolicy *policy)
> {
> return folio_alloc_noprof(gfp, order);
> }
> @@ -659,7 +659,7 @@ static inline struct folio *filemap_alloc_folio_noprof(gfp_t gfp, unsigned int o
>
> static inline struct page *__page_cache_alloc(gfp_t gfp)
> {
> - return &filemap_alloc_folio(gfp, 0)->page;
> + return &filemap_alloc_folio(gfp, 0, NULL)->page;
> }
>
> static inline gfp_t readahead_gfp_mask(struct address_space *x)
> diff --git a/mm/filemap.c b/mm/filemap.c
> index bada249b9fb7..a26df313207d 100644
> --- a/mm/filemap.c
> +++ b/mm/filemap.c
> @@ -989,11 +989,16 @@ int filemap_add_folio(struct address_space *mapping, struct folio *folio,
> EXPORT_SYMBOL_GPL(filemap_add_folio);
>
> #ifdef CONFIG_NUMA
> -struct folio *filemap_alloc_folio_noprof(gfp_t gfp, unsigned int order)
> +struct folio *filemap_alloc_folio_noprof(gfp_t gfp, unsigned int order,
> + struct mempolicy *policy)
> {
> int n;
> struct folio *folio;
>
> + if (policy)
> + return folio_alloc_mpol_noprof(gfp, order, policy,
> + NO_INTERLEAVE_INDEX, numa_node_id());
> +
> if (cpuset_do_page_mem_spread()) {
> unsigned int cpuset_mems_cookie;
> do {
> @@ -1977,7 +1982,7 @@ struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index,
> err = -ENOMEM;
> if (order > min_order)
> alloc_gfp |= __GFP_NORETRY | __GFP_NOWARN;
> - folio = filemap_alloc_folio(alloc_gfp, order);
> + folio = filemap_alloc_folio(alloc_gfp, order, NULL);
> if (!folio)
> continue;
>
> @@ -2516,7 +2521,7 @@ static int filemap_create_folio(struct kiocb *iocb, struct folio_batch *fbatch)
> if (iocb->ki_flags & (IOCB_NOWAIT | IOCB_WAITQ))
> return -EAGAIN;
>
> - folio = filemap_alloc_folio(mapping_gfp_mask(mapping), min_order);
> + folio = filemap_alloc_folio(mapping_gfp_mask(mapping), min_order, NULL);
> if (!folio)
> return -ENOMEM;
> if (iocb->ki_flags & IOCB_DONTCACHE)
> @@ -3854,7 +3859,7 @@ static struct folio *do_read_cache_folio(struct address_space *mapping,
> folio = filemap_get_folio(mapping, index);
> if (IS_ERR(folio)) {
> folio = filemap_alloc_folio(gfp,
> - mapping_min_folio_order(mapping));
> + mapping_min_folio_order(mapping), NULL);
> if (!folio)
> return ERR_PTR(-ENOMEM);
> index = mapping_align_index(mapping, index);
> diff --git a/mm/readahead.c b/mm/readahead.c
> index 20d36d6b055e..0b2aec0231e6 100644
> --- a/mm/readahead.c
> +++ b/mm/readahead.c
> @@ -183,7 +183,7 @@ static struct folio *ractl_alloc_folio(struct readahead_control *ractl,
> {
> struct folio *folio;
>
> - folio = filemap_alloc_folio(gfp_mask, order);
> + folio = filemap_alloc_folio(gfp_mask, order, NULL);
> if (folio && ractl->dropbehind)
> __folio_set_dropbehind(folio);
>
This allows guest_memfd to pass in a memory policy.
Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
---
include/linux/pagemap.h | 10 ++++++++--
mm/filemap.c | 10 ++++++----
2 files changed, 14 insertions(+), 6 deletions(-)
diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h
index c176aeeb38db..1cfbf7b8f573 100644
--- a/include/linux/pagemap.h
+++ b/include/linux/pagemap.h
@@ -745,11 +745,17 @@ static inline fgf_t fgf_set_order(size_t size)
}
void *filemap_get_entry(struct address_space *mapping, pgoff_t index);
-struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index,
- fgf_t fgp_flags, gfp_t gfp);
+struct folio *__filemap_get_folio_mpol(struct address_space *mapping,
+ pgoff_t index, fgf_t fgf_flags, gfp_t gfp, struct mempolicy *);
struct page *pagecache_get_page(struct address_space *mapping, pgoff_t index,
fgf_t fgp_flags, gfp_t gfp);
+static inline struct folio *__filemap_get_folio(struct address_space *mapping,
+ pgoff_t index, fgf_t fgf_flags, gfp_t gfp)
+{
+ return __filemap_get_folio_mpol(mapping, index, fgf_flags, gfp, NULL);
+}
+
/**
* filemap_get_folio - Find and get a folio.
* @mapping: The address_space to search.
diff --git a/mm/filemap.c b/mm/filemap.c
index a26df313207d..597d146cbb3a 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -1896,11 +1896,12 @@ void *filemap_get_entry(struct address_space *mapping, pgoff_t index)
}
/**
- * __filemap_get_folio - Find and get a reference to a folio.
+ * __filemap_get_folio_mpol - Find and get a reference to a folio.
* @mapping: The address_space to search.
* @index: The page index.
* @fgp_flags: %FGP flags modify how the folio is returned.
* @gfp: Memory allocation flags to use if %FGP_CREAT is specified.
+ * @policy: NUMA memory allocation policy to follow.
*
* Looks up the page cache entry at @mapping & @index.
*
@@ -1911,8 +1912,9 @@ void *filemap_get_entry(struct address_space *mapping, pgoff_t index)
*
* Return: The found folio or an ERR_PTR() otherwise.
*/
-struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index,
- fgf_t fgp_flags, gfp_t gfp)
+struct folio *__filemap_get_folio_mpol(struct address_space *mapping,
+ pgoff_t index, fgf_t fgp_flags, gfp_t gfp,
+ struct mempolicy *policy)
{
struct folio *folio;
@@ -1982,7 +1984,7 @@ struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index,
err = -ENOMEM;
if (order > min_order)
alloc_gfp |= __GFP_NORETRY | __GFP_NOWARN;
- folio = filemap_alloc_folio(alloc_gfp, order, NULL);
+ folio = filemap_alloc_folio(alloc_gfp, order, policy);
if (!folio)
continue;
--
2.47.2
On 6/20/25 16:34, Matthew Wilcox (Oracle) wrote: > This allows guest_memfd to pass in a memory policy. > > Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org> Reviewed-by: Vlastimil Babka <vbabka@suse.cz>
> This allows guest_memfd to pass in a memory policy.
>
> Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
Reviewed-by: Pankaj Gupta <pankaj.gupta@amd.com>
> ---
> include/linux/pagemap.h | 10 ++++++++--
> mm/filemap.c | 10 ++++++----
> 2 files changed, 14 insertions(+), 6 deletions(-)
>
> diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h
> index c176aeeb38db..1cfbf7b8f573 100644
> --- a/include/linux/pagemap.h
> +++ b/include/linux/pagemap.h
> @@ -745,11 +745,17 @@ static inline fgf_t fgf_set_order(size_t size)
> }
>
> void *filemap_get_entry(struct address_space *mapping, pgoff_t index);
> -struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index,
> - fgf_t fgp_flags, gfp_t gfp);
> +struct folio *__filemap_get_folio_mpol(struct address_space *mapping,
> + pgoff_t index, fgf_t fgf_flags, gfp_t gfp, struct mempolicy *);
> struct page *pagecache_get_page(struct address_space *mapping, pgoff_t index,
> fgf_t fgp_flags, gfp_t gfp);
>
> +static inline struct folio *__filemap_get_folio(struct address_space *mapping,
> + pgoff_t index, fgf_t fgf_flags, gfp_t gfp)
> +{
> + return __filemap_get_folio_mpol(mapping, index, fgf_flags, gfp, NULL);
> +}
> +
> /**
> * filemap_get_folio - Find and get a folio.
> * @mapping: The address_space to search.
> diff --git a/mm/filemap.c b/mm/filemap.c
> index a26df313207d..597d146cbb3a 100644
> --- a/mm/filemap.c
> +++ b/mm/filemap.c
> @@ -1896,11 +1896,12 @@ void *filemap_get_entry(struct address_space *mapping, pgoff_t index)
> }
>
> /**
> - * __filemap_get_folio - Find and get a reference to a folio.
> + * __filemap_get_folio_mpol - Find and get a reference to a folio.
> * @mapping: The address_space to search.
> * @index: The page index.
> * @fgp_flags: %FGP flags modify how the folio is returned.
> * @gfp: Memory allocation flags to use if %FGP_CREAT is specified.
> + * @policy: NUMA memory allocation policy to follow.
> *
> * Looks up the page cache entry at @mapping & @index.
> *
> @@ -1911,8 +1912,9 @@ void *filemap_get_entry(struct address_space *mapping, pgoff_t index)
> *
> * Return: The found folio or an ERR_PTR() otherwise.
> */
> -struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index,
> - fgf_t fgp_flags, gfp_t gfp)
> +struct folio *__filemap_get_folio_mpol(struct address_space *mapping,
> + pgoff_t index, fgf_t fgp_flags, gfp_t gfp,
> + struct mempolicy *policy)
> {
> struct folio *folio;
>
> @@ -1982,7 +1984,7 @@ struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index,
> err = -ENOMEM;
> if (order > min_order)
> alloc_gfp |= __GFP_NORETRY | __GFP_NOWARN;
> - folio = filemap_alloc_folio(alloc_gfp, order, NULL);
> + folio = filemap_alloc_folio(alloc_gfp, order, policy);
> if (!folio)
> continue;
>
On Fri, Jun 20, 2025 at 03:34:47PM +0100, Matthew Wilcox (Oracle) wrote:
> +struct folio *__filemap_get_folio_mpol(struct address_space *mapping,
> + pgoff_t index, fgf_t fgp_flags, gfp_t gfp,
> + struct mempolicy *policy)
> {
> struct folio *folio;
>
> @@ -1982,7 +1984,7 @@ struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index,
> err = -ENOMEM;
> if (order > min_order)
> alloc_gfp |= __GFP_NORETRY | __GFP_NOWARN;
> - folio = filemap_alloc_folio(alloc_gfp, order, NULL);
> + folio = filemap_alloc_folio(alloc_gfp, order, policy);
> if (!folio)
> continue;
This is missing the EXPORT_SYMBOL_GPL() change. Sorry about that.
I'm sure you can fix it up ;-) I only tested "make O=.build-all/ -j16
mm/ fs/" (on an allmodconfig) which doesn't get as far as making sure
that modules can still see all the symbols they need.
On Fri, 20 Jun 2025 17:53:15 +0100 Matthew Wilcox <willy@infradead.org> wrote:
> On Fri, Jun 20, 2025 at 03:34:47PM +0100, Matthew Wilcox (Oracle) wrote:
> > +struct folio *__filemap_get_folio_mpol(struct address_space *mapping,
> > + pgoff_t index, fgf_t fgp_flags, gfp_t gfp,
> > + struct mempolicy *policy)
> > {
> > struct folio *folio;
> >
> > @@ -1982,7 +1984,7 @@ struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index,
> > err = -ENOMEM;
> > if (order > min_order)
> > alloc_gfp |= __GFP_NORETRY | __GFP_NOWARN;
> > - folio = filemap_alloc_folio(alloc_gfp, order, NULL);
> > + folio = filemap_alloc_folio(alloc_gfp, order, policy);
> > if (!folio)
> > continue;
>
> This is missing the EXPORT_SYMBOL_GPL() change
I added this:
--- a/mm/filemap.c~filemap-add-__filemap_get_folio_mpol-fix
+++ a/mm/filemap.c
@@ -2032,7 +2032,7 @@ no_page:
folio_clear_dropbehind(folio);
return folio;
}
-EXPORT_SYMBOL(__filemap_get_folio);
+EXPORT_SYMBOL(__filemap_get_folio_mpol);
static inline struct folio *find_get_entry(struct xa_state *xas, pgoff_t max,
xa_mark_t mark)
_
On 6/23/2025 12:13 AM, Andrew Morton wrote:
> On Fri, 20 Jun 2025 17:53:15 +0100 Matthew Wilcox <willy@infradead.org> wrote:
>
>> On Fri, Jun 20, 2025 at 03:34:47PM +0100, Matthew Wilcox (Oracle) wrote:
>>> +struct folio *__filemap_get_folio_mpol(struct address_space *mapping,
>>> + pgoff_t index, fgf_t fgp_flags, gfp_t gfp,
>>> + struct mempolicy *policy)
>>> {
>>> struct folio *folio;
>>>
>>> @@ -1982,7 +1984,7 @@ struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index,
>>> err = -ENOMEM;
>>> if (order > min_order)
>>> alloc_gfp |= __GFP_NORETRY | __GFP_NOWARN;
>>> - folio = filemap_alloc_folio(alloc_gfp, order, NULL);
>>> + folio = filemap_alloc_folio(alloc_gfp, order, policy);
>>> if (!folio)
>>> continue;
>>
>> This is missing the EXPORT_SYMBOL_GPL() change
>
> I added this:
>
> --- a/mm/filemap.c~filemap-add-__filemap_get_folio_mpol-fix
> +++ a/mm/filemap.c
> @@ -2032,7 +2032,7 @@ no_page:
> folio_clear_dropbehind(folio);
> return folio;
> }
> -EXPORT_SYMBOL(__filemap_get_folio);
> +EXPORT_SYMBOL(__filemap_get_folio_mpol);
>
> static inline struct folio *find_get_entry(struct xa_state *xas, pgoff_t max,
> xa_mark_t mark)
> _
>
Hi Andrew,
Thank you for addressing this.
If you don’t mind me asking,
I was curious why we used EXPORT_SYMBOL instead of EXPORT_SYMBOL_GPL here.
I had previously received feedback recommending the use of EXPORT_SYMBOL_GPL
to better align with the kernel’s licensing philosophy, which made sense to me.
Thanks,
Shivank
On 6/22/25 21:02, Shivank Garg wrote: > > Hi Andrew, > > Thank you for addressing this. > > If you don’t mind me asking, > I was curious why we used EXPORT_SYMBOL instead of EXPORT_SYMBOL_GPL here. > I had previously received feedback recommending the use of EXPORT_SYMBOL_GPL > to better align with the kernel’s licensing philosophy, which made sense to me. That's the recommendation for new symbols, but this has become effectively a rename (plus a new parameter) so it's a bit different situation. > Thanks, > Shivank >
On 6/23/2025 12:46 PM, Vlastimil Babka wrote: > On 6/22/25 21:02, Shivank Garg wrote: >> >> Hi Andrew, >> >> Thank you for addressing this. >> >> If you don’t mind me asking, >> I was curious why we used EXPORT_SYMBOL instead of EXPORT_SYMBOL_GPL here. >> I had previously received feedback recommending the use of EXPORT_SYMBOL_GPL >> to better align with the kernel’s licensing philosophy, which made sense to me. > > That's the recommendation for new symbols, but this has become effectively a > rename (plus a new parameter) so it's a bit different situation. agreed, Thanks.
On Mon, 23 Jun 2025 00:32:05 +0530 Shivank Garg <shivankg@amd.com> wrote:
> > -EXPORT_SYMBOL(__filemap_get_folio);
> > +EXPORT_SYMBOL(__filemap_get_folio_mpol);
> >
> > static inline struct folio *find_get_entry(struct xa_state *xas, pgoff_t max,
> > xa_mark_t mark)
> > _
> >
>
> Hi Andrew,
>
> Thank you for addressing this.
>
> If you don’t mind me asking,
> I was curious why we used EXPORT_SYMBOL instead of EXPORT_SYMBOL_GPL here.
> I had previously received feedback recommending the use of EXPORT_SYMBOL_GPL
> to better align with the kernel’s licensing philosophy, which made sense to me.
Making this _GPL would effectively switch __filemap_get_folio() from
non-GPL to GPL. Leaving it at non-GPL is less disruptive and Matthew's
patch did not have the intention of changing licensing.
Also,
hp2:/usr/src/25> grep "EXPORT_SYMBOL(" mm/filemap.c|wc -l
48
hp2:/usr/src/25> grep "EXPORT_SYMBOL_GPL(" mm/filemap.c|wc -l
9
On 6/23/2025 3:46 AM, Andrew Morton wrote:
> On Mon, 23 Jun 2025 00:32:05 +0530 Shivank Garg <shivankg@amd.com> wrote:
>
>>> -EXPORT_SYMBOL(__filemap_get_folio);
>>> +EXPORT_SYMBOL(__filemap_get_folio_mpol);
>>>
>>> static inline struct folio *find_get_entry(struct xa_state *xas, pgoff_t max,
>>> xa_mark_t mark)
>>> _
>>>
>>
>> Hi Andrew,
>>
>> Thank you for addressing this.
>>
>> If you don’t mind me asking,
>> I was curious why we used EXPORT_SYMBOL instead of EXPORT_SYMBOL_GPL here.
>> I had previously received feedback recommending the use of EXPORT_SYMBOL_GPL
>> to better align with the kernel’s licensing philosophy, which made sense to me.
>
> Making this _GPL would effectively switch __filemap_get_folio() from
> non-GPL to GPL. Leaving it at non-GPL is less disruptive and Matthew's
> patch did not have the intention of changing licensing.
>
> Also,
>
> hp2:/usr/src/25> grep "EXPORT_SYMBOL(" mm/filemap.c|wc -l
> 48
> hp2:/usr/src/25> grep "EXPORT_SYMBOL_GPL(" mm/filemap.c|wc -l
> 9
>
>
Can you pick these revised patches:
https://lore.kernel.org/linux-mm/20250623093939.1323623-4-shivankg@amd.com
I did some touch-up on commit description, changed some code alignments to make it more readable
and fixed couple of checkpatch.pl warnings.
Thanks,
Shivank
On 6/23/2025 3:46 AM, Andrew Morton wrote:
> On Mon, 23 Jun 2025 00:32:05 +0530 Shivank Garg <shivankg@amd.com> wrote:
>
>>> -EXPORT_SYMBOL(__filemap_get_folio);
>>> +EXPORT_SYMBOL(__filemap_get_folio_mpol);
>>>
>>> static inline struct folio *find_get_entry(struct xa_state *xas, pgoff_t max,
>>> xa_mark_t mark)
>>> _
>>>
>>
>> Hi Andrew,
>>
>> Thank you for addressing this.
>>
>> If you don’t mind me asking,
>> I was curious why we used EXPORT_SYMBOL instead of EXPORT_SYMBOL_GPL here.
>> I had previously received feedback recommending the use of EXPORT_SYMBOL_GPL
>> to better align with the kernel’s licensing philosophy, which made sense to me.
>
> Making this _GPL would effectively switch __filemap_get_folio() from
> non-GPL to GPL. Leaving it at non-GPL is less disruptive and Matthew's
> patch did not have the intention of changing licensing.
>
> Also,
>
> hp2:/usr/src/25> grep "EXPORT_SYMBOL(" mm/filemap.c|wc -l
> 48
> hp2:/usr/src/25> grep "EXPORT_SYMBOL_GPL(" mm/filemap.c|wc -l
> 9
>
>
Thank you for the explanation.
This makes sense to me.
Reviewed-by: Shivank Garg <shivankg@amd.com>
Thanks,
Shivank
© 2016 - 2026 Red Hat, Inc.