Unverified Commit c8e577f2 authored by openeuler-ci-bot's avatar openeuler-ci-bot Committed by Gitee
Browse files

!5688 v3 Optimize compaction

Merge Pull Request from: @ci-robot 
 
PR sync from: Liu Shixin <liushixin2@huawei.com>
https://mailweb.openeuler.org/hyperkitty/list/kernel@openeuler.org/message/DMWSULM56UTIJGHSRRVQLLJOCEW6UXUG/ 
v1->v2:
  Introduce NR_PAGE_ORDERS and MAX_PAGE_ORDER to avoid conflicts.
v2->v3:
  Fix compile error.

Optimize compaction v3.

Baolin Wang (2):
  mm: compaction: update the cc->nr_migratepages when allocating or
    freeing the freepages
  mm: compaction: limit the suitable target page order to be less than
    cc->order

Barry Song (1):
  mm: compaction: avoid fast_isolate_freepages blindly choose improper
    pageblock

Hugh Dickins (1):
  mm: add page_rmappable_folio() wrapper

Hyesoo Yu (1):
  mm: page_alloc: check the order of compound page even when the order
    is zero

Kemeng Shi (6):
  mm/compaction: use correct list in move_freelist_{head}/{tail}
  mm/compaction: call list_is_{first}/{last} more intuitively in
    move_freelist_{head}/{tail}
  mm/compaction: correctly return failure with bogus compound_order in
  mm/compaction: remove repeat compact_blockskip_flush check in
    reset_isolation_suitable
  mm/compaction: improve comment of is_via_compact_memory
  mm/compaction: factor out code to test if we should run compaction for
    target order

Liu Shixin (1):
  mm/compaction: introduce NR_PAGE_ORDERS and MAX_PAGE_ORDER

Zi Yan (4):
  mm/page_alloc: remove unused fpi_flags in free_pages_prepare()
  mm/compaction: enable compacting >0 order folios.
  mm/compaction: add support for >0 order folio memory compaction.
  mm/compaction: optimize >0 order folio compaction with free page
    split.


-- 
2.25.1
 
https://gitee.com/openeuler/kernel/issues/I9CXS6 
 
Link:https://gitee.com/openeuler/kernel/pulls/5688

 

Reviewed-by: default avatarKefeng Wang <wangkefeng.wang@huawei.com>
Reviewed-by: default avatarXu Kuohai <xukuohai@huawei.com>
Signed-off-by: default avatarZheng Zengkai <zhengzengkai@huawei.com>
parents ab8bc242 835fe6a4
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -78,10 +78,10 @@ DEFINE_EVENT(mm_compaction_isolate_template, mm_compaction_fast_isolate_freepage
#ifdef CONFIG_COMPACTION
TRACE_EVENT(mm_compaction_migratepages,

	TP_PROTO(struct compact_control *cc,
	TP_PROTO(unsigned int nr_migratepages,
		unsigned int nr_succeeded),

	TP_ARGS(cc, nr_succeeded),
	TP_ARGS(nr_migratepages, nr_succeeded),

	TP_STRUCT__entry(
		__field(unsigned long, nr_migrated)
@@ -90,7 +90,7 @@ TRACE_EVENT(mm_compaction_migratepages,

	TP_fast_assign(
		__entry->nr_migrated = nr_succeeded;
		__entry->nr_failed = cc->nr_migratepages - nr_succeeded;
		__entry->nr_failed = nr_migratepages - nr_succeeded;
	),

	TP_printk("nr_migrated=%lu nr_failed=%lu",
+241 −121
Original line number Diff line number Diff line
@@ -41,9 +41,22 @@ static inline void count_compact_events(enum vm_event_item item, long delta)
{
	count_vm_events(item, delta);
}

/*
 * order == -1 is expected when compacting proactively via
 * 1. /proc/sys/vm/compact_memory
 * 2. /sys/devices/system/node/nodex/compact
 * 3. /proc/sys/vm/compaction_proactiveness
 */
static inline bool is_via_compact_memory(int order)
{
	return order == -1;
}

#else
#define count_compact_event(item) do { } while (0)
#define count_compact_events(item, delta) do { } while (0)
static inline bool is_via_compact_memory(int order) { return false; }
#endif

#if defined CONFIG_COMPACTION || defined CONFIG_CMA
@@ -67,32 +80,18 @@ static inline void count_compact_events(enum vm_event_item item, long delta)
#define COMPACTION_HPAGE_ORDER	(PMD_SHIFT - PAGE_SHIFT)
#endif

static unsigned long release_freepages(struct list_head *freelist)
{
	struct page *page, *next;
	unsigned long high_pfn = 0;

	list_for_each_entry_safe(page, next, freelist, lru) {
		unsigned long pfn = page_to_pfn(page);
		list_del(&page->lru);
		__free_page(page);
		if (pfn > high_pfn)
			high_pfn = pfn;
	}

	return high_pfn;
}

static void split_map_pages(struct list_head *list)
static void split_map_pages(struct list_head *freepages)
{
	unsigned int i, order, nr_pages;
	unsigned int i, order;
	struct page *page, *next;
	LIST_HEAD(tmp_list);

	list_for_each_entry_safe(page, next, list, lru) {
	for (order = 0; order < NR_PAGE_ORDERS; order++) {
		list_for_each_entry_safe(page, next, &freepages[order], lru) {
			unsigned int nr_pages;

			list_del(&page->lru);

		order = page_private(page);
			nr_pages = 1 << order;

			post_alloc_hook(page, order, __GFP_MOVABLE);
@@ -104,8 +103,33 @@ static void split_map_pages(struct list_head *list)
				page++;
			}
		}
		list_splice_init(&tmp_list, &freepages[0]);
	}
}

static unsigned long release_free_list(struct list_head *freepages)
{
	int order;
	unsigned long high_pfn = 0;

	for (order = 0; order < NR_PAGE_ORDERS; order++) {
		struct page *page, *next;

		list_for_each_entry_safe(page, next, &freepages[order], lru) {
			unsigned long pfn = page_to_pfn(page);

	list_splice(&tmp_list, list);
			list_del(&page->lru);
			/*
			 * Convert free pages into post allocation pages, so
			 * that we can free them via __free_page.
			 */
			post_alloc_hook(page, order, __GFP_MOVABLE);
			__free_pages(page, order);
			if (pfn > high_pfn)
				high_pfn = pfn;
		}
	}
	return high_pfn;
}

#ifdef CONFIG_COMPACTION
@@ -383,6 +407,7 @@ static void __reset_isolation_suitable(struct zone *zone)
	bool source_set = false;
	bool free_set = false;

	/* Only flush if a full compaction finished recently */
	if (!zone->compact_blockskip_flush)
		return;

@@ -435,8 +460,6 @@ void reset_isolation_suitable(pg_data_t *pgdat)
		if (!populated_zone(zone))
			continue;

		/* Only flush if a full compaction finished recently */
		if (zone->compact_blockskip_flush)
		__reset_isolation_suitable(zone);
	}
}
@@ -627,11 +650,12 @@ static unsigned long isolate_freepages_block(struct compact_control *cc,
		if (PageCompound(page)) {
			const unsigned int order = compound_order(page);

			if (likely(order <= MAX_ORDER)) {
			if (blockpfn + (1UL << order) <= end_pfn) {
				blockpfn += (1UL << order) - 1;
				page += (1UL << order) - 1;
				nr_scanned += (1UL << order) - 1;
			}

			goto isolate_fail;
		}

@@ -658,7 +682,7 @@ static unsigned long isolate_freepages_block(struct compact_control *cc,
		nr_scanned += isolated - 1;
		total_isolated += isolated;
		cc->nr_freepages += isolated;
		list_add_tail(&page->lru, freelist);
		list_add_tail(&page->lru, &freelist[order]);

		if (!strict && cc->nr_migratepages <= cc->nr_freepages) {
			blockpfn += isolated;
@@ -679,8 +703,7 @@ static unsigned long isolate_freepages_block(struct compact_control *cc,
		spin_unlock_irqrestore(&cc->zone->lock, flags);

	/*
	 * There is a tiny chance that we have read bogus compound_order(),
	 * so be careful to not go outside of the pageblock.
	 * Be careful to not go outside of the pageblock.
	 */
	if (unlikely(blockpfn > end_pfn))
		blockpfn = end_pfn;
@@ -724,7 +747,11 @@ isolate_freepages_range(struct compact_control *cc,
			unsigned long start_pfn, unsigned long end_pfn)
{
	unsigned long isolated, pfn, block_start_pfn, block_end_pfn;
	LIST_HEAD(freelist);
	int order;
	struct list_head tmp_freepages[NR_PAGE_ORDERS];

	for (order = 0; order < NR_PAGE_ORDERS; order++)
		INIT_LIST_HEAD(&tmp_freepages[order]);

	pfn = start_pfn;
	block_start_pfn = pageblock_start_pfn(pfn);
@@ -755,7 +782,7 @@ isolate_freepages_range(struct compact_control *cc,
			break;

		isolated = isolate_freepages_block(cc, &isolate_start_pfn,
					block_end_pfn, &freelist, 0, true);
					block_end_pfn, tmp_freepages, 0, true);

		/*
		 * In strict mode, isolate_freepages_block() returns 0 if
@@ -772,15 +799,15 @@ isolate_freepages_range(struct compact_control *cc,
		 */
	}

	/* __isolate_free_page() does not map the pages */
	split_map_pages(&freelist);

	if (pfn < end_pfn) {
		/* Loop terminated early, cleanup. */
		release_freepages(&freelist);
		release_free_list(tmp_freepages);
		return 0;
	}

	/* __isolate_free_page() does not map the pages */
	split_map_pages(tmp_freepages);

	/* We don't use freelists for anything. */
	return pfn;
}
@@ -818,6 +845,32 @@ static bool too_many_isolated(struct compact_control *cc)
	return too_many;
}

/**
 * skip_isolation_on_order() - determine when to skip folio isolation based on
 *			       folio order and compaction target order
 * @order:		to-be-isolated folio order
 * @target_order:	compaction target order
 *
 * This avoids unnecessary folio isolations during compaction.
 */
static bool skip_isolation_on_order(int order, int target_order)
{
	/*
	 * Unless we are performing global compaction (i.e.,
	 * is_via_compact_memory), skip any folios that are larger than the
	 * target order: we wouldn't be here if we'd have a free folio with
	 * the desired target_order, so migrating this folio would likely fail
	 * later.
	 */
	if (!is_via_compact_memory(target_order) && order >= target_order)
		return true;
	/*
	 * We limit memory compaction to pageblocks and won't try
	 * creating free blocks of memory that are larger than that.
	 */
	return order >= pageblock_order;
}

/**
 * isolate_migratepages_block() - isolate all migrate-able pages within
 *				  a single pageblock
@@ -948,7 +1001,22 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
			valid_page = page;
		}

		if (PageHuge(page) && cc->alloc_contig) {
		if (PageHuge(page)) {
			/*
			 * skip hugetlbfs if we are not compacting for pages
			 * bigger than its order. THPs and other compound pages
			 * are handled below.
			 */
			if (!cc->alloc_contig) {
				const unsigned int order = compound_order(page);

				if (order <= MAX_PAGE_ORDER) {
					low_pfn += (1UL << order) - 1;
					nr_scanned += (1UL << order) - 1;
				}
				goto isolate_fail;
			}
			/* for alloc_contig case */
			if (locked) {
				unlock_page_lruvec_irqrestore(locked, flags);
				locked = NULL;
@@ -1001,7 +1069,7 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
			 * a valid page order. Consider only values in the
			 * valid order range to prevent low_pfn overflow.
			 */
			if (freepage_order > 0 && freepage_order <= MAX_ORDER) {
			if (freepage_order > 0 && freepage_order <= MAX_PAGE_ORDER) {
				low_pfn += (1UL << freepage_order) - 1;
				nr_scanned += (1UL << freepage_order) - 1;
			}
@@ -1009,22 +1077,25 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
		}

		/*
		 * Regardless of being on LRU, compound pages such as THP and
		 * hugetlbfs are not to be compacted unless we are attempting
		 * an allocation much larger than the huge page size (eg CMA).
		 * We can potentially save a lot of iterations if we skip them
		 * at once. The check is racy, but we can consider only valid
		 * values and the only danger is skipping too much.
		 * Regardless of being on LRU, compound pages such as THP
		 * (hugetlbfs is handled above) are not to be compacted unless
		 * we are attempting an allocation larger than the compound
		 * page size. We can potentially save a lot of iterations if we
		 * skip them at once. The check is racy, but we can consider
		 * only valid values and the only danger is skipping too much.
		 */
		if (PageCompound(page) && !cc->alloc_contig) {
			const unsigned int order = compound_order(page);

			if (likely(order <= MAX_ORDER)) {
			/* Skip based on page order and compaction target order. */
			if (skip_isolation_on_order(order, cc->order)) {
				if (order <= MAX_PAGE_ORDER) {
					low_pfn += (1UL << order) - 1;
					nr_scanned += (1UL << order) - 1;
				}
				goto isolate_fail;
			}
		}

		/*
		 * Check may be lockless but that's ok as we recheck later.
@@ -1148,10 +1219,11 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
			}

			/*
			 * folio become large since the non-locked check,
			 * and it's on LRU.
			 * Check LRU folio order under the lock
			 */
			if (unlikely(folio_test_large(folio) && !cc->alloc_contig)) {
			if (unlikely(skip_isolation_on_order(folio_order(folio),
							     cc->order) &&
				     !cc->alloc_contig)) {
				low_pfn += folio_nr_pages(folio) - 1;
				nr_scanned += folio_nr_pages(folio) - 1;
				folio_set_lru(folio);
@@ -1348,12 +1420,14 @@ static bool suitable_migration_target(struct compact_control *cc,
{
	/* If the page is a large free page, then disallow migration */
	if (PageBuddy(page)) {
		int order = cc->order > 0 ? cc->order : pageblock_order;

		/*
		 * We are checking page_order without zone->lock taken. But
		 * the only small danger is that we skip a potentially suitable
		 * pageblock, so it's not worth to check order for valid range.
		 */
		if (buddy_order_unsafe(page) >= pageblock_order)
		if (buddy_order_unsafe(page) >= order)
			return false;
	}

@@ -1396,8 +1470,8 @@ move_freelist_head(struct list_head *freelist, struct page *freepage)
{
	LIST_HEAD(sublist);

	if (!list_is_last(freelist, &freepage->lru)) {
		list_cut_before(&sublist, freelist, &freepage->lru);
	if (!list_is_first(&freepage->buddy_list, freelist)) {
		list_cut_before(&sublist, freelist, &freepage->buddy_list);
		list_splice_tail(&sublist, freelist);
	}
}
@@ -1413,8 +1487,8 @@ move_freelist_tail(struct list_head *freelist, struct page *freepage)
{
	LIST_HEAD(sublist);

	if (!list_is_first(freelist, &freepage->lru)) {
		list_cut_position(&sublist, freelist, &freepage->lru);
	if (!list_is_last(&freepage->buddy_list, freelist)) {
		list_cut_position(&sublist, freelist, &freepage->buddy_list);
		list_splice_tail(&sublist, freelist);
	}
}
@@ -1441,7 +1515,7 @@ fast_isolate_around(struct compact_control *cc, unsigned long pfn)
	if (!page)
		return;

	isolate_freepages_block(cc, &start_pfn, end_pfn, &cc->freepages, 1, false);
	isolate_freepages_block(cc, &start_pfn, end_pfn, cc->freepages, 1, false);

	/* Skip this pageblock in the future as it's full or nearly full */
	if (start_pfn == end_pfn && !cc->no_set_skip_hint)
@@ -1570,7 +1644,7 @@ static void fast_isolate_freepages(struct compact_control *cc)
				nr_scanned += nr_isolated - 1;
				total_isolated += nr_isolated;
				cc->nr_freepages += nr_isolated;
				list_add_tail(&page->lru, &cc->freepages);
				list_add_tail(&page->lru, &cc->freepages[order]);
				count_compact_events(COMPACTISOLATED, nr_isolated);
			} else {
				/* If isolation fails, abort the search */
@@ -1613,6 +1687,9 @@ static void fast_isolate_freepages(struct compact_control *cc)
						min(pageblock_end_pfn(min_pfn),
						    zone_end_pfn(cc->zone)),
						cc->zone);
					if (page && !suitable_migration_target(cc, page))
						page = NULL;

					cc->free_pfn = min_pfn;
				}
			}
@@ -1644,13 +1721,12 @@ static void isolate_freepages(struct compact_control *cc)
	unsigned long isolate_start_pfn; /* exact pfn we start at */
	unsigned long block_end_pfn;	/* end of current pageblock */
	unsigned long low_pfn;	     /* lowest pfn scanner is able to scan */
	struct list_head *freelist = &cc->freepages;
	unsigned int stride;

	/* Try a small search of the free lists for a candidate */
	fast_isolate_freepages(cc);
	if (cc->nr_freepages)
		goto splitmap;
		return;

	/*
	 * Initialise the free scanner. The starting point is where we last
@@ -1710,7 +1786,7 @@ static void isolate_freepages(struct compact_control *cc)

		/* Found a block suitable for isolating free pages from. */
		nr_isolated = isolate_freepages_block(cc, &isolate_start_pfn,
					block_end_pfn, freelist, stride, false);
					block_end_pfn, cc->freepages, stride, false);

		/* Update the skip hint if the full pageblock was scanned */
		if (isolate_start_pfn == block_end_pfn)
@@ -1751,10 +1827,6 @@ static void isolate_freepages(struct compact_control *cc)
	 * and the loop terminated due to isolate_start_pfn < low_pfn
	 */
	cc->free_pfn = isolate_start_pfn;

splitmap:
	/* __isolate_free_page() does not map the pages */
	split_map_pages(freelist);
}

/*
@@ -1765,19 +1837,47 @@ static struct folio *compaction_alloc(struct folio *src, unsigned long data)
{
	struct compact_control *cc = (struct compact_control *)data;
	struct folio *dst;
	int order = folio_order(src);
	bool has_isolated_pages = false;
	int start_order;
	struct page *freepage;
	unsigned long size;

	if (list_empty(&cc->freepages)) {
		isolate_freepages(cc);
again:
	for (start_order = order; start_order < NR_PAGE_ORDERS; start_order++)
		if (!list_empty(&cc->freepages[start_order]))
			break;

		if (list_empty(&cc->freepages))
	/* no free pages in the list */
	if (start_order == NR_PAGE_ORDERS) {
		if (has_isolated_pages)
			return NULL;
		isolate_freepages(cc);
		has_isolated_pages = true;
		goto again;
	}

	dst = list_entry(cc->freepages.next, struct folio, lru);
	list_del(&dst->lru);
	cc->nr_freepages--;
	freepage = list_first_entry(&cc->freepages[start_order], struct page,
				lru);
	size = 1 << start_order;

	list_del(&freepage->lru);

	while (start_order > order) {
		start_order--;
		size >>= 1;

	return dst;
		list_add(&freepage[size].lru, &cc->freepages[start_order]);
		set_page_private(&freepage[size], start_order);
	}
	dst = (struct folio *)freepage;

	post_alloc_hook(&dst->page, order, __GFP_MOVABLE);
	if (order)
		prep_compound_page(&dst->page, order);
	cc->nr_freepages -= 1 << order;
	cc->nr_migratepages -= 1 << order;
	return page_rmappable_folio(&dst->page);
}

/*
@@ -1788,9 +1888,19 @@ static struct folio *compaction_alloc(struct folio *src, unsigned long data)
static void compaction_free(struct folio *dst, unsigned long data)
{
	struct compact_control *cc = (struct compact_control *)data;
	int order = folio_order(dst);
	struct page *page = &dst->page;

	list_add(&dst->lru, &cc->freepages);
	cc->nr_freepages++;
	if (folio_put_testzero(dst)) {
		free_pages_prepare(page, order);
		list_add(&dst->lru, &cc->freepages[order]);
		cc->nr_freepages += 1 << order;
	}
	cc->nr_migratepages += 1 << order;
	/*
	 * someone else has referenced the page, we cannot take it back to our
	 * free list.
	 */
}

/* possible outcome of isolate_migratepages */
@@ -2069,15 +2179,6 @@ static isolate_migrate_t isolate_migratepages(struct compact_control *cc)
	return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE;
}

/*
 * order == -1 is expected when compacting via
 * /proc/sys/vm/compact_memory
 */
static inline bool is_via_compact_memory(int order)
{
	return order == -1;
}

/*
 * Determine whether kswapd is (or recently was!) running on this node.
 *
@@ -2229,7 +2330,7 @@ static enum compact_result __compact_finished(struct compact_control *cc)

	/* Direct compactor: Is a suitable page free? */
	ret = COMPACT_NO_SUITABLE_PAGE;
	for (order = cc->order; order <= MAX_ORDER; order++) {
	for (order = cc->order; order < NR_PAGE_ORDERS; order++) {
		struct free_area *area = &cc->zone->free_area[order];
		bool can_steal;

@@ -2381,6 +2482,30 @@ bool compaction_zonelist_suitable(struct alloc_context *ac, int order,
	return false;
}

/*
 * Should we do compaction for target allocation order.
 * Return COMPACT_SUCCESS if allocation for target order can be already
 * satisfied
 * Return COMPACT_SKIPPED if compaction for target order is likely to fail
 * Return COMPACT_CONTINUE if compaction for target order should be ran
 */
static enum compact_result
compaction_suit_allocation_order(struct zone *zone, unsigned int order,
				 int highest_zoneidx, unsigned int alloc_flags)
{
	unsigned long watermark;

	watermark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK);
	if (zone_watermark_ok(zone, order, watermark, highest_zoneidx,
			      alloc_flags))
		return COMPACT_SUCCESS;

	if (!compaction_suitable(zone, order, highest_zoneidx))
		return COMPACT_SKIPPED;

	return COMPACT_CONTINUE;
}

static enum compact_result
compact_zone(struct compact_control *cc, struct capture_control *capc)
{
@@ -2390,7 +2515,8 @@ compact_zone(struct compact_control *cc, struct capture_control *capc)
	unsigned long last_migrated_pfn;
	const bool sync = cc->mode != MIGRATE_ASYNC;
	bool update_cached;
	unsigned int nr_succeeded = 0;
	unsigned int nr_succeeded = 0, nr_migratepages;
	int order;

	/*
	 * These counters track activities during zone compaction.  Initialize
@@ -2400,25 +2526,18 @@ compact_zone(struct compact_control *cc, struct capture_control *capc)
	cc->total_free_scanned = 0;
	cc->nr_migratepages = 0;
	cc->nr_freepages = 0;
	INIT_LIST_HEAD(&cc->freepages);
	for (order = 0; order < NR_PAGE_ORDERS; order++)
		INIT_LIST_HEAD(&cc->freepages[order]);
	INIT_LIST_HEAD(&cc->migratepages);

	cc->migratetype = gfp_migratetype(cc->gfp_mask);

	if (!is_via_compact_memory(cc->order)) {
		unsigned long watermark;

		/* Allocation can already succeed, nothing to do */
		watermark = wmark_pages(cc->zone,
					cc->alloc_flags & ALLOC_WMARK_MASK);
		if (zone_watermark_ok(cc->zone, cc->order, watermark,
				      cc->highest_zoneidx, cc->alloc_flags))
			return COMPACT_SUCCESS;

		/* Compaction is likely to fail */
		if (!compaction_suitable(cc->zone, cc->order,
					 cc->highest_zoneidx))
			return COMPACT_SKIPPED;
		ret = compaction_suit_allocation_order(cc->zone, cc->order,
						       cc->highest_zoneidx,
						       cc->alloc_flags);
		if (ret != COMPACT_CONTINUE)
			return ret;
	}

	/*
@@ -2516,11 +2635,17 @@ compact_zone(struct compact_control *cc, struct capture_control *capc)
				pageblock_start_pfn(cc->migrate_pfn - 1));
		}

		/*
		 * Record the number of pages to migrate since the
		 * compaction_alloc/free() will update cc->nr_migratepages
		 * properly.
		 */
		nr_migratepages = cc->nr_migratepages;
		err = migrate_pages(&cc->migratepages, compaction_alloc,
				compaction_free, (unsigned long)cc, cc->mode,
				MR_COMPACTION, &nr_succeeded);

		trace_mm_compaction_migratepages(cc, nr_succeeded);
		trace_mm_compaction_migratepages(nr_migratepages, nr_succeeded);

		/* All pages were either migrated or will be released */
		cc->nr_migratepages = 0;
@@ -2594,7 +2719,7 @@ compact_zone(struct compact_control *cc, struct capture_control *capc)
	 * so we don't leave any returned pages behind in the next attempt.
	 */
	if (cc->nr_freepages > 0) {
		unsigned long free_pfn = release_freepages(&cc->freepages);
		unsigned long free_pfn = release_free_list(cc->freepages);

		cc->nr_freepages = 0;
		VM_BUG_ON(free_pfn == 0);
@@ -2613,7 +2738,6 @@ compact_zone(struct compact_control *cc, struct capture_control *capc)

	trace_mm_compaction_end(cc, start_pfn, end_pfn, sync, ret);

	VM_BUG_ON(!list_empty(&cc->freepages));
	VM_BUG_ON(!list_empty(&cc->migratepages));

	return ret;
@@ -2917,6 +3041,7 @@ static bool kcompactd_node_suitable(pg_data_t *pgdat)
	int zoneid;
	struct zone *zone;
	enum zone_type highest_zoneidx = pgdat->kcompactd_highest_zoneidx;
	enum compact_result ret;

	for (zoneid = 0; zoneid <= highest_zoneidx; zoneid++) {
		zone = &pgdat->node_zones[zoneid];
@@ -2924,14 +3049,10 @@ static bool kcompactd_node_suitable(pg_data_t *pgdat)
		if (!populated_zone(zone))
			continue;

		/* Allocation can already succeed, check other zones */
		if (zone_watermark_ok(zone, pgdat->kcompactd_max_order,
				      min_wmark_pages(zone),
				      highest_zoneidx, 0))
			continue;

		if (compaction_suitable(zone, pgdat->kcompactd_max_order,
					highest_zoneidx))
		ret = compaction_suit_allocation_order(zone,
				pgdat->kcompactd_max_order,
				highest_zoneidx, ALLOC_WMARK_MIN);
		if (ret == COMPACT_CONTINUE)
			return true;
	}

@@ -2954,6 +3075,8 @@ static void kcompactd_do_work(pg_data_t *pgdat)
		.ignore_skip_hint = false,
		.gfp_mask = GFP_KERNEL,
	};
	enum compact_result ret;

	trace_mm_compaction_kcompactd_wake(pgdat->node_id, cc.order,
							cc.highest_zoneidx);
	count_compact_event(KCOMPACTD_WAKE);
@@ -2968,12 +3091,9 @@ static void kcompactd_do_work(pg_data_t *pgdat)
		if (compaction_deferred(zone, cc.order))
			continue;

		/* Allocation can already succeed, nothing to do */
		if (zone_watermark_ok(zone, cc.order,
				      min_wmark_pages(zone), zoneid, 0))
			continue;

		if (!compaction_suitable(zone, cc.order, zoneid))
		ret = compaction_suit_allocation_order(zone,
				cc.order, zoneid, ALLOC_WMARK_MIN);
		if (ret != COMPACT_CONTINUE)
			continue;

		if (kthread_should_stop())
+15 −1
Original line number Diff line number Diff line
@@ -508,6 +508,15 @@ static inline void folio_set_order(struct folio *folio, unsigned int order)

void folio_undo_large_rmappable(struct folio *folio);

static inline struct folio *page_rmappable_folio(struct page *page)
{
	struct folio *folio = (struct folio *)page;

	if (folio && folio_order(folio) > 1)
		folio_prep_large_rmappable(folio);
	return folio;
}

static inline void prep_compound_head(struct page *page, unsigned int order)
{
	struct folio *folio = (struct folio *)page;
@@ -531,6 +540,8 @@ extern void prep_compound_page(struct page *page, unsigned int order);

extern void post_alloc_hook(struct page *page, unsigned int order,
					gfp_t gfp_flags);
extern bool free_pages_prepare(struct page *page, unsigned int order);

#ifdef CONFIG_DYNAMIC_POOL
extern bool dpool_free_page_prepare(struct page *page);
extern int dpool_check_new_page(struct page *page);
@@ -560,6 +571,9 @@ int split_free_page(struct page *free_page,

#if defined CONFIG_COMPACTION || defined CONFIG_CMA

#define NR_PAGE_ORDERS (MAX_ORDER + 1)
#define MAX_PAGE_ORDER	MAX_ORDER

/*
 * in mm/compaction.c
 */
@@ -571,7 +585,7 @@ int split_free_page(struct page *free_page,
 * completes when free_pfn <= migrate_pfn
 */
struct compact_control {
	struct list_head freepages;	/* List of free pages to migrate to */
	struct list_head freepages[NR_PAGE_ORDERS];	/* List of free pages to migrate to */
	struct list_head migratepages;	/* List of pages being migrated */
	unsigned int nr_freepages;	/* Number of isolated free pages */
	unsigned int nr_migratepages;	/* Number of pages to migrate */
+3 −14
Original line number Diff line number Diff line
@@ -2211,10 +2211,7 @@ struct folio *vma_alloc_folio(gfp_t gfp, int order, struct vm_area_struct *vma,
		mpol_cond_put(pol);
		gfp |= __GFP_COMP;
		page = alloc_page_interleave(gfp, order, nid);
		folio = (struct folio *)page;
		if (folio && order > 1)
			folio_prep_large_rmappable(folio);
		goto out;
		return page_rmappable_folio(page);
	}

	if (pol->mode == MPOL_PREFERRED_MANY) {
@@ -2224,10 +2221,7 @@ struct folio *vma_alloc_folio(gfp_t gfp, int order, struct vm_area_struct *vma,
		gfp |= __GFP_COMP;
		page = alloc_pages_preferred_many(gfp, order, node, pol);
		mpol_cond_put(pol);
		folio = (struct folio *)page;
		if (folio && order > 1)
			folio_prep_large_rmappable(folio);
		goto out;
		return page_rmappable_folio(page);
	}

	if (unlikely(IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && hugepage)) {
@@ -2323,12 +2317,7 @@ EXPORT_SYMBOL(alloc_pages);

struct folio *folio_alloc(gfp_t gfp, unsigned order)
{
	struct page *page = alloc_pages(gfp | __GFP_COMP, order);
	struct folio *folio = (struct folio *)page;

	if (folio && order > 1)
		folio_prep_large_rmappable(folio);
	return folio;
	return page_rmappable_folio(alloc_pages(gfp | __GFP_COMP, order));
}
EXPORT_SYMBOL(folio_alloc);

+12 −16
Original line number Diff line number Diff line
@@ -1061,7 +1061,7 @@ static int free_tail_page_prepare(struct page *head_page, struct page *page)
 * on-demand allocation and then freed again before the deferred pages
 * initialization is done, but this is not likely to happen.
 */
static inline bool should_skip_kasan_poison(struct page *page, fpi_t fpi_flags)
static inline bool should_skip_kasan_poison(struct page *page)
{
	if (IS_ENABLED(CONFIG_KASAN_GENERIC))
		return deferred_pages_enabled();
@@ -1080,12 +1080,13 @@ static void kernel_init_pages(struct page *page, int numpages)
	kasan_enable_current();
}

static __always_inline bool free_pages_prepare(struct page *page,
			unsigned int order, fpi_t fpi_flags)
__always_inline bool free_pages_prepare(struct page *page,
			unsigned int order)
{
	int bad = 0;
	bool skip_kasan_poison = should_skip_kasan_poison(page, fpi_flags);
	bool skip_kasan_poison = should_skip_kasan_poison(page);
	bool init = want_init_on_free();
	bool compound = PageCompound(page);

	VM_BUG_ON_PAGE(PageTail(page), page);

@@ -1104,16 +1105,15 @@ static __always_inline bool free_pages_prepare(struct page *page,
		return false;
	}

	VM_BUG_ON_PAGE(compound && compound_order(page) != order, page);

	/*
	 * Check tail pages before head page information is cleared to
	 * avoid checking PageCompound for order-0 pages.
	 */
	if (unlikely(order)) {
		bool compound = PageCompound(page);
		int i;

		VM_BUG_ON_PAGE(compound && compound_order(page) != order, page);

		if (compound)
			page[1].flags &= ~PAGE_FLAGS_SECOND;
		for (i = 1; i < (1 << order); i++) {
@@ -1271,7 +1271,7 @@ static void __free_pages_ok(struct page *page, unsigned int order,
	unsigned long pfn = page_to_pfn(page);
	struct zone *zone = page_zone(page);

	if (!free_pages_prepare(page, order, fpi_flags))
	if (!free_pages_prepare(page, order))
		return;

	/*
@@ -1571,7 +1571,7 @@ static void prep_new_page(struct page *page, unsigned int order, gfp_t gfp_flags
 */
bool dpool_free_page_prepare(struct page *page)
{
	return free_pages_prepare(page, 0, 0);
	return free_pages_prepare(page, 0);
}

int dpool_check_new_page(struct page *page)
@@ -2372,7 +2372,7 @@ static bool free_unref_page_prepare(struct page *page, unsigned long pfn,
{
	int migratetype;

	if (!free_pages_prepare(page, order, FPI_NONE))
	if (!free_pages_prepare(page, order))
		return false;

	migratetype = get_pfnblock_migratetype(page, pfn);
@@ -4846,11 +4846,7 @@ struct folio *__folio_alloc(gfp_t gfp, unsigned int order, int preferred_nid,
{
	struct page *page = __alloc_pages(gfp | __GFP_COMP, order,
					preferred_nid, nodemask);
	struct folio *folio = (struct folio *)page;

	if (folio && order > 1)
		folio_prep_large_rmappable(folio);
	return folio;
	return page_rmappable_folio(page);
}
EXPORT_SYMBOL(__folio_alloc);