Re: [PATCH v2 1/3] mm: page_alloc: add a watermark check that knows the migratetype

From: Vlastimil Babka (SUSE)

Date: Fri Nov 13 2026 - 15:30:12 EST


On 11/2/26 10:39, Kiryl Shutsemau (Meta) wrote:
> The high-order check in __zone_watermark_ok() accepts a free page of any
> migratetype. Under ALLOC_NOFRAGMENT, a request below pageblock_order can
> only take a page of its own migratetype or claim a whole free block, so
> the check can pass when the allocation cannot succeed.
>
> Telling those apart needs the request's migratetype, but not every check
> wants them told apart. A check whether an allocation can be served now
> has to count only the pages it can take.
>
> A check whether there is enough free memory counts every migratetype:
> kswapd and kcompactd balancing a zone, order-0 thresholds, the
> slowpath's forecast whether reclaim and compaction can still make
> progress. Reclaim and compaction work across migratetypes to free whole
> pageblocks.
>
> Move the body of __zone_watermark_ok() into __zone_watermark_ok_mt(),
> which takes the migratetype as well, and make __zone_watermark_ok() a
> wrapper around it for the second kind of check. These use the new
> function:
>
> - the allocator fast path, zone_watermark_fast();
> - its recheck after node reclaim in get_page_from_freelist();
> - compaction's up-front check, compaction_suit_allocation_order().
>
> Declare both in mm/internal.h: nothing outside mm uses them.

Why not mm/page_alloc.h instead, now that it exists.

> Nothing reads the migratetype yet: no functional change.
>
> Tested-by: Dev Jain <dev.jain@xxxxxxx>
> Signed-off-by: Kiryl Shutsemau (Meta) <kas@xxxxxxxxxx>
> ---
> include/linux/mmzone.h | 3 ---
> mm/compaction.c | 11 ++++++-----
> mm/internal.h | 7 +++++++
> mm/page_alloc.c | 37 ++++++++++++++++++++++++++++---------
> 4 files changed, 41 insertions(+), 17 deletions(-)
>
> diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h
> index ecd1ebd49756..4d9347a399fa 100644
> --- a/include/linux/mmzone.h
> +++ b/include/linux/mmzone.h
> @@ -1637,9 +1637,6 @@ static inline unsigned long pgdat_end_pfn(pg_data_t *pgdat)
> #include <linux/memory_hotplug.h>
>
> void build_all_zonelists(pg_data_t *pgdat);
> -bool __zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
> - int highest_zoneidx, unsigned int alloc_flags,
> - long free_pages);
> bool zone_watermark_ok(struct zone *z, unsigned int order,
> unsigned long mark, int highest_zoneidx,
> unsigned int alloc_flags);
> diff --git a/mm/compaction.c b/mm/compaction.c
> index f1b2060eb201..6c553d0e90f8 100644
> --- a/mm/compaction.c
> +++ b/mm/compaction.c
> @@ -2520,7 +2520,7 @@ bool compaction_zonelist_suitable(struct alloc_context *ac, int order,
> static enum compact_result
> compaction_suit_allocation_order(struct zone *zone, unsigned int order,
> int highest_zoneidx, unsigned int alloc_flags,
> - bool async, bool kcompactd)
> + int migratetype, bool async, bool kcompactd)
> {
> unsigned long free_pages;
> unsigned long watermark;
> @@ -2531,8 +2531,8 @@ compaction_suit_allocation_order(struct zone *zone, unsigned int order,
> free_pages = zone_page_state(zone, NR_FREE_PAGES);
>
> watermark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK);
> - if (__zone_watermark_ok(zone, order, watermark, highest_zoneidx,
> - alloc_flags, free_pages))
> + if (__zone_watermark_ok_mt(zone, order, watermark, highest_zoneidx,
> + alloc_flags, free_pages, migratetype))
> return COMPACT_SUCCESS;
>
> /*
> @@ -2588,6 +2588,7 @@ compact_zone(struct compact_control *cc, struct capture_control *capc)
> ret = compaction_suit_allocation_order(cc->zone, cc->order,
> cc->highest_zoneidx,
> cc->alloc_flags,
> + cc->migratetype,
> cc->mode == MIGRATE_ASYNC,
> !cc->direct_compaction);
> if (ret != COMPACT_CONTINUE)
> @@ -3073,7 +3074,7 @@ static bool kcompactd_node_suitable(pg_data_t *pgdat)
> ret = compaction_suit_allocation_order(zone,
> pgdat->kcompactd_max_order,
> highest_zoneidx, alloc_flags,
> - false, true);
> + MIGRATE_MOVABLE, false, true);
> if (ret == COMPACT_CONTINUE)
> return true;
> }
> @@ -3116,7 +3117,7 @@ static void kcompactd_do_work(pg_data_t *pgdat)
>
> ret = compaction_suit_allocation_order(zone,
> cc.order, zoneid, cc.alloc_flags,
> - false, true);
> + MIGRATE_MOVABLE, false, true);
> if (ret != COMPACT_CONTINUE)
> continue;
>
> diff --git a/mm/internal.h b/mm/internal.h
> index 566cbd2dce48..8e58e2db5e09 100644
> --- a/mm/internal.h
> +++ b/mm/internal.h
> @@ -726,6 +726,13 @@ extern int min_free_kbytes;
> extern int defrag_mode;
>
> void setup_per_zone_wmarks(void);
> +bool __zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
> + int highest_zoneidx, unsigned int alloc_flags,
> + long free_pages);
> +bool __zone_watermark_ok_mt(struct zone *z, unsigned int order,
> + unsigned long mark, int highest_zoneidx,
> + unsigned int alloc_flags, long free_pages,
> + int migratetype);
> void calculate_min_free_kbytes(void);
> int __meminit init_per_zone_wmark_min(void);
>
> diff --git a/mm/page_alloc.c b/mm/page_alloc.c
> index a8cac5e01066..7730660bdd09 100644
> --- a/mm/page_alloc.c
> +++ b/mm/page_alloc.c
> @@ -3587,9 +3587,10 @@ static inline long __zone_watermark_unusable_free(struct zone *z,
> * one free page of a suitable size. Checking now avoids taking the zone lock
> * to check in the allocation paths if no pages are free.
> */
> -bool __zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
> - int highest_zoneidx, unsigned int alloc_flags,
> - long free_pages)
> +bool __zone_watermark_ok_mt(struct zone *z, unsigned int order,
> + unsigned long mark, int highest_zoneidx,
> + unsigned int alloc_flags, long free_pages,
> + int migratetype)
> {
> long min = mark;
> int o;
> @@ -3665,6 +3666,21 @@ bool __zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
> return false;
> }
>
> +/*
> + * For checks that are not about serving one allocation now: kswapd and
> + * kcompactd balancing a zone, order-0 thresholds, forecasts whether
> + * reclaim and compaction can still make progress. They count free pages
> + * of every migratetype: reclaim and compaction work across migratetypes
> + * to free whole pageblocks.
> + */

What a slop comment, sigh. Can you rewrite it normally?

> +bool __zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
> + int highest_zoneidx, unsigned int alloc_flags,
> + long free_pages)
> +{
> + return __zone_watermark_ok_mt(z, order, mark, highest_zoneidx,
> + alloc_flags, free_pages, MIGRATE_MOVABLE);
> +}
> +
> bool zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
> int highest_zoneidx, unsigned int alloc_flags)
> {
> @@ -3697,8 +3713,8 @@ static inline bool zone_watermark_fast(struct zone *z, unsigned int order,
> return true;
> }
>
> - if (__zone_watermark_ok(z, order, mark, highest_zoneidx, alloc_flags,
> - free_pages))
> + if (__zone_watermark_ok_mt(z, order, mark, highest_zoneidx, alloc_flags,
> + free_pages, gfp_migratetype(gfp_mask)))
> return true;
>
> /*
> @@ -3710,8 +3726,9 @@ static inline bool zone_watermark_fast(struct zone *z, unsigned int order,
> if (unlikely(!order && (alloc_flags & ALLOC_MIN_RESERVE) && z->watermark_boost
> && ((alloc_flags & ALLOC_WMARK_MASK) == WMARK_MIN))) {
> mark = z->_watermark[WMARK_MIN];
> - return __zone_watermark_ok(z, order, mark, highest_zoneidx,
> - alloc_flags, free_pages);
> + return __zone_watermark_ok_mt(z, order, mark, highest_zoneidx,
> + alloc_flags, free_pages,
> + gfp_migratetype(gfp_mask));
> }
>
> return false;
> @@ -3923,8 +3940,10 @@ get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
> continue;
>
> /* did we reclaim enough */
> - if (!zone_watermark_ok(zone, order, mark,
> - ac->highest_zoneidx, alloc_flags))
> + if (!__zone_watermark_ok_mt(zone, order, mark,
> + ac->highest_zoneidx, alloc_flags,
> + zone_page_state(zone, NR_FREE_PAGES),
> + gfp_migratetype(gfp_mask)))
> continue;
> }
>