Re: [PATCH v3 21/26] mm/page_alloc: implement FREETYPE_UNMAPPED allocations

Yosry Ahmed <[email protected]>
Newsgroups org.kernel.vger.linux-kernel,org.kvack.linux-mm
Message-ID <[email protected]>
On Sun, Jul 26, 2026 at 10:22:54PM +0000, Brendan Jackman wrote:
> Currently FREETYPE_UNMAPPED allocs will always fail because, although the
> lists exist to hold them, there is no way to actually create an unmapped
> page block. This commit adds one, and also the logic to map it back
> again when that's needed.
> 
> Doing this at pageblock granularity ensures that the pageblock flags can
> be used to infer which freetype a page belongs to. It also provides nice
> batching of TLB flushes, and also avoids creating too much unnecessary
> TLB fragmentation in the physmap.
> 
> There are some functional requirements for flipping a block:
> 
>  - Unmapping requires a TLB shootdown, meaning IRQs must be enabled.
> 
>  - Updating the pagetables might require allocating a pagetable to break
>    down a huge page. This would deadlock if the zone lock was held.
> 
> This makes allocations that need to change sensitivity _somewhat_
> similar to those that need to fallback to a different migratetype. But,
> the locking requirements mean that this can't just be squashed into the
> existing "fallback" allocator logic, instead a new allocator path just
> for this purpose is needed.
> 
> The new path is assumed to be much cheaper than the really heavyweight
> stuff like compaction and reclaim. But at present it is treated as less
> desirable than the mobility-related "fallback" and "stealing" logic.
> This might turn out to need revision (in particular, maybe it's a
> problem that __rmqueue_steal(), which causes fragmentation, happens
> before __rmqueue_direct_map()), but that should be treated as a subsequent
> optimisation project.
> 
> Adding alloc_flags to gfp_freetype() requires moving it to
> mm/page_alloc.h so it can refer to ALLOC_UNMAPPED. It was already only
> used in internal mm code.
> 
> Now that unmapped pageblocks actually exist, exclude them from
> migration. Migrating unmapped pages via the mermap should be possible
> but that's something to be added later when needed.
> 
> Signed-off-by: Brendan Jackman <[email protected]>
> ---
>  mm/Kconfig      |   7 +-
>  mm/compaction.c |   8 ++-
>  mm/page_alloc.c | 214 +++++++++++++++++++++++++++++++++++++++++++++++++-------
>  mm/page_alloc.h |  13 +++-
>  mm/page_owner.c |   6 +-
>  5 files changed, 215 insertions(+), 33 deletions(-)
> 
> diff --git a/mm/Kconfig b/mm/Kconfig
> index aa5f041ce1a67..6a89cbf8e8c6e 100644
> --- a/mm/Kconfig
> +++ b/mm/Kconfig
> @@ -1527,9 +1527,10 @@ config MERMAP_KUNIT_TEST
>  
>  	  If unsure, say N.
>  
> +config PAGE_ALLOC_UNMAPPED
> +	bool
> +	depends on !HIGHMEM
> +
>  source "mm/damon/Kconfig"
>  
>  endmenu
> -
> -config PAGE_ALLOC_UNMAPPED
> -	bool
> diff --git a/mm/compaction.c b/mm/compaction.c
> index 67b01af024e17..c9eb3947ffc79 100644
> --- a/mm/compaction.c
> +++ b/mm/compaction.c
> @@ -1393,6 +1393,9 @@ static bool suitable_migration_source(struct compact_control *cc,

There is a direct compaction check above the context lines:

	/*
         * Background compaction produces blocks for the zone at
         * large, with no particular allocation context. Allow all
         * block types, including CMA.
         */
        if (!cc->direct_compaction)
                return true;

>  	block_ft = get_pageblock_freetype(page);
>  	block_mt = free_to_migratetype(block_ft);
>  
> +	if (freetype_unmapped(get_pageblock_freetype(page)))
> +		return false;

..and I think this freetype_unmapped() check should be moved above it.
Otherwise kcompactd (for e.g.) won't check for unmapped pages and
crashes.

[..]

> +#ifdef CONFIG_PAGE_ALLOC_UNMAPPED
> +/* Try to allocate a page by mapping/unmapping a block from the direct map. */
> +static inline struct page *
> +__rmqueue_direct_map(struct zone *zone, unsigned int request_order,
> +		     unsigned int alloc_flags, freetype_t freetype)
> +{
> +	unsigned int ft_flags_other = freetype_flags(freetype) ^ FREETYPE_UNMAPPED;
> +	freetype_t ft_other = migrate_to_freetype(free_to_migratetype(freetype),
> +						  ft_flags_other);
> +	bool want_mapped = !(freetype_flags(freetype) & FREETYPE_UNMAPPED);
> +	enum rmqueue_mode rmqm = RMQUEUE_NORMAL;
> +	unsigned long irq_flags;
> +	int nr_pageblocks, nr_freed;
> +	struct page *page;
> +	int alloc_order;
> +	int err;
> +
> +	if (freetype_idx(ft_other) < 0)
> +		return NULL;

For a movable mapped allocation, ft_other will end up being movable
unmapped, which will immediately fail this check before trying to
convert a pageblock. This leads to premature OOM kills.

> +
> +	/*
> +	 * Might need a TLB shootdown. Even if IRQs are on this isn't
> +	 * safe if the caller holds a lock (in case the other CPUs need that
> +	 * lock to handle the shootdown IPI).
> +	 */
> +	if (alloc_flags & ALLOC_NOBLOCK)
> +		return NULL;
> +
> +	if (!can_set_direct_map() || alloc_flags & ALLOC_NOLOCK)
> +		return NULL;
> +
> +	lockdep_assert(!irqs_disabled() || unlikely(early_boot_irqs_disabled));
> +
> +	/*
> +	 * Need to [un]map a whole pageblock (otherwise it might require
> +	 * allocating pagetables). First allocate it.
> +	 */
> +	alloc_order = max(request_order, pageblock_order);
> +	nr_pageblocks = 1 << (alloc_order - pageblock_order);
> +	spin_lock_irqsave(&zone->lock, irq_flags);
> +	/* First try a block that already has the right migratetype. */
> +	page = __rmqueue(zone, alloc_order, ft_other, alloc_flags, &rmqm);
> +	if (!page) {
> +		/* Fallback to changing a block's migratetype. */
> +		rmqm = RMQUEUE_CLAIM;
> +		page = __rmqueue(zone, alloc_order, ft_other, alloc_flags, &rmqm);

IIUC, the first call to __rmqueue() will already try RMQUEUE_CLAIM. Is
this redundant?
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.