Skip to contentSkip to conversation

mm/memfd: drop redundant hugetlb allocation mask narrowing

PATCH Applied Hongfu Li 4 participants 5 messages Nov 19

Subsystems (2)
lore
Share & tools
lore

Hongfu Li

author
#0 lore raw

PATCH v2 1/1 mm/memfd: drop redundant hugetlb allocation mask narrowing

From: Hongfu Li <[email protected]>

memfd_alloc_folio() derives its allocation mask from htlb_alloc_mask() and then clears __GFP_HIGHMEM and __GFP_MOVABLE. This mask handling was introduced by commit 89c1905d9c14 ("mm/gup: introduce memfd_pin_folios() for pinning memfd folios"), when the fallback path in alloc_hugetlb_folio_nodemask() could still call alloc_migrate_hugetlb_folio() and reach the buddy allocator, so the mask decided the zone and migratetype of freshly allocated pages.

Since commit 26a8ea80929c ("mm/hugetlb: fix memfd_pin_folios resv_huge_pages leak"), this call site uses alloc_hugetlb_folio_reserve(), which only dequeues folios from the existing hugetlb pool and never falls back to the buddy allocator. The mask therefore no longer decides where any page comes from.

Long-term pinning safety no longer depends on the mask either:
memfd_pin_folios() obtains its folios (dequeued from the pool, or freshly allocated) under memalloc_pin_save(), so PF_MEMALLOC_PIN makes dequeue_hugetlb_folio_node_exact() skip any folio that fails folio_is_longterm_pinnable(), and current_gfp_context() already clears __GFP_MOVABLE from any page allocator request made under PF_MEMALLOC_PIN.

Clearing __GFP_HIGHMEM was a lowmem constraint for these folios, which were meant to be accessed by a DMA driver. It is not required any more:
the users of memfd_pin_folios() work on pages and scatterlists, which support highmem, as does the shmem path with its default GFP_HIGHUSER_MOVABLE mask.

Drop the narrowing, which has had no effect since 26a8ea80929c.

---
Changes in v2:
- Drop patch 2.
- Remove the now stale comment about the zone and migration constraints in patch 1.
- Update the commit message to explain why __GFP_HIGHMEM can be removed in patch 1.
- Link to v1: https://patch.msgid.link/[email protected]

---
 mm/memfd.c | 7 -------
 1 file changed, 7 deletions(-)
mm/memfd.c +0 -7
index 0e3ff3eebfa8..4c359d0fa923 100644
@@ -72,12 +72,6 @@ struct folio *memfd_alloc_folio(struct file *memfd, pgoff_t idx)
 	gfp_t gfp_mask;
 
 	if (is_file_hugepages(memfd)) {
-		/*
-		 * The folio would most likely be accessed by a DMA driver,
-		 * therefore, we have zone memory constraints where we can
-		 * alloc from. Also, the folio will be pinned for an indefinite
-		 * amount of time, so it is not expected to be migrated away.
-		 */
 		struct inode *inode = file_inode(memfd);
 		struct hstate *h = hstate_file(memfd);
 		int err = -ENOMEM;
@@ -85,7 +79,6 @@ struct folio *memfd_alloc_folio(struct file *memfd, pgoff_t idx)
 		u32 hash;
 
 		gfp_mask = htlb_alloc_mask(h);
-		gfp_mask &= ~(__GFP_HIGHMEM | __GFP_MOVABLE);
 		idx >>= huge_page_order(h);
 
 		/*

---
base-commit: 0bce4c3c7589a80b09a4f21fcb41811cca5153a1
change-id: 20261117-for-memfd-e11675edfe65

Best regards,

Signature · 1 line
Hongfu Li <[email protected]>
Acked-by on v2 1/1
#1 lore raw
Quoted 33 lines · Hongfu Li

From: Hongfu Li <[email protected]>

memfd_alloc_folio() derives its allocation mask from htlb_alloc_mask() and then clears __GFP_HIGHMEM and __GFP_MOVABLE. This mask handling was introduced by commit 89c1905d9c14 ("mm/gup: introduce memfd_pin_folios() for pinning memfd folios"), when the fallback path in alloc_hugetlb_folio_nodemask() could still call alloc_migrate_hugetlb_folio() and reach the buddy allocator, so the mask decided the zone and migratetype of freshly allocated pages.

Since commit 26a8ea80929c ("mm/hugetlb: fix memfd_pin_folios resv_huge_pages leak"), this call site uses alloc_hugetlb_folio_reserve(), which only dequeues folios from the existing hugetlb pool and never falls back to the buddy allocator. The mask therefore no longer decides where any page comes from.

Long-term pinning safety no longer depends on the mask either:
memfd_pin_folios() obtains its folios (dequeued from the pool, or freshly allocated) under memalloc_pin_save(), so PF_MEMALLOC_PIN makes dequeue_hugetlb_folio_node_exact() skip any folio that fails folio_is_longterm_pinnable(), and current_gfp_context() already clears __GFP_MOVABLE from any page allocator request made under PF_MEMALLOC_PIN.

Clearing __GFP_HIGHMEM was a lowmem constraint for these folios, which were meant to be accessed by a DMA driver. It is not required any more:
the users of memfd_pin_folios() work on pages and scatterlists, which support highmem, as does the shmem path with its default GFP_HIGHUSER_MOVABLE mask.

Quoted 3 lines

Drop the narrowing, which has had no effect since 26a8ea80929c.

Acked-by on v2 1/1
#2 lore raw
In reply to Hongfu Li
Quoted 29 lines · Hongfu Li

From: Hongfu Li <[email protected]>

memfd_alloc_folio() derives its allocation mask from htlb_alloc_mask() and then clears __GFP_HIGHMEM and __GFP_MOVABLE. This mask handling was introduced by commit 89c1905d9c14 ("mm/gup: introduce memfd_pin_folios() for pinning memfd folios"), when the fallback path in alloc_hugetlb_folio_nodemask() could still call alloc_migrate_hugetlb_folio() and reach the buddy allocator, so the mask decided the zone and migratetype of freshly allocated pages.

Since commit 26a8ea80929c ("mm/hugetlb: fix memfd_pin_folios resv_huge_pages leak"), this call site uses alloc_hugetlb_folio_reserve(), which only dequeues folios from the existing hugetlb pool and never falls back to the buddy allocator. The mask therefore no longer decides where any page comes from.

Long-term pinning safety no longer depends on the mask either:
memfd_pin_folios() obtains its folios (dequeued from the pool, or freshly allocated) under memalloc_pin_save(), so PF_MEMALLOC_PIN makes dequeue_hugetlb_folio_node_exact() skip any folio that fails folio_is_longterm_pinnable(), and current_gfp_context() already clears __GFP_MOVABLE from any page allocator request made under PF_MEMALLOC_PIN.

Clearing __GFP_HIGHMEM was a lowmem constraint for these folios, which were meant to be accessed by a DMA driver. It is not required any more:
the users of memfd_pin_folios() work on pages and scatterlists, which support highmem, as does the shmem path with its default GFP_HIGHUSER_MOVABLE mask.

Quoted 3 lines

Drop the narrowing, which has had no effect since 26a8ea80929c.

Signature · 2 lines
Oscar Salvador
SUSE Labs
Reviewed-by on v2 1/1
#3 lore raw
In reply to Hongfu Li
Quoted 29 lines · Hongfu Li

From: Hongfu Li <[email protected]>

memfd_alloc_folio() derives its allocation mask from htlb_alloc_mask() and then clears __GFP_HIGHMEM and __GFP_MOVABLE. This mask handling was introduced by commit 89c1905d9c14 ("mm/gup: introduce memfd_pin_folios() for pinning memfd folios"), when the fallback path in alloc_hugetlb_folio_nodemask() could still call alloc_migrate_hugetlb_folio() and reach the buddy allocator, so the mask decided the zone and migratetype of freshly allocated pages.

Since commit 26a8ea80929c ("mm/hugetlb: fix memfd_pin_folios resv_huge_pages leak"), this call site uses alloc_hugetlb_folio_reserve(), which only dequeues folios from the existing hugetlb pool and never falls back to the buddy allocator. The mask therefore no longer decides where any page comes from.

Long-term pinning safety no longer depends on the mask either:
memfd_pin_folios() obtains its folios (dequeued from the pool, or freshly allocated) under memalloc_pin_save(), so PF_MEMALLOC_PIN makes dequeue_hugetlb_folio_node_exact() skip any folio that fails folio_is_longterm_pinnable(), and current_gfp_context() already clears __GFP_MOVABLE from any page allocator request made under PF_MEMALLOC_PIN.

Clearing __GFP_HIGHMEM was a lowmem constraint for these folios, which were meant to be accessed by a DMA driver. It is not required any more:
the users of memfd_pin_folios() work on pages and scatterlists, which support highmem, as does the shmem path with its default GFP_HIGHUSER_MOVABLE mask.

Quoted 4 lines

Drop the narrowing, which has had no effect since 26a8ea80929c.

---

Signature · 3 lines
Cheers,

David
#4 lore raw
In reply to Hongfu Li
Quoted 9 lines · Hongfu Li

memfd_alloc_folio() derives its allocation mask from htlb_alloc_mask() and then clears __GFP_HIGHMEM and __GFP_MOVABLE. This mask handling was introduced by commit 89c1905d9c14 ("mm/gup: introduce memfd_pin_folios() for pinning memfd folios"), when the fallback path in alloc_hugetlb_folio_nodemask() could still call alloc_migrate_hugetlb_folio() and reach the buddy allocator, so the mask decided the zone and migratetype of freshly allocated pages.

[...]

Applied, thanks!

tree: mm/core.git
branch: shmem-7.5.misc

Applied patches will appear soon in mm-next, followed by linux-next.

Please report any outstanding bugs that were missed during review, so we can either drop the patch series or decide how to fix it up.

It is encouraged to provide Acked-bys, Reviewed-bys, and Tested-bys even though the patches have already been applied. If possible, patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. Further, this is not a guarantee that the patches will go upstream. If in doubt, please check the listed branch.

[1/1] mm/memfd: drop redundant hugetlb allocation mask narrowing

      https://git.kernel.org/mm/core/c/b2f133fb
Signature · 3 lines
Cheers,

David