Huge page compaction finds free target pages to which source pages can
be migrated when compacting. A huge page sized and aligned block is
considered a valid source of target pages if it passes the
suitable_migration_target() test. One of the things which
suitable_migration_target() does is to ensure that the entire block
isn't currently free. It would counter productive to use an already
fully free huge page sized block as a migration target because using
pages from that free huge page block would decrease the number of
available huge pages in the system.
suitable_migration_source() attempts to ensure that the supplied block
is not currently a fully free block by checking PageBuddy flag on the
starting page of the huge page sized and aligned block. This approach is
flawed: the buddy list can and does maintain buddies at a larger order
than huge page size. For example on a typical x86 system, huge page
pageblock_order is 2 MiB, but the buddy list MAX_ORDER is 4 MiB. Because
of this, a pageblock_order sized block may be free because it is part of
a larger order buddy list buddy, but the pageblock_order sized block
won't itself be part of the buddy list, only the larger order block will
be. The current suitable_migration_target() implementation of just
checking the PageBuddy flag on the pageblock_order block is hence
insufficient as it will appear that the block is not free and hence try
to use it as a source of migration target pages.
Enhance suitable_migration_target() to cater for this case by scanning
up the buddy orders from the current pageblock_order page to MAX_ORDER
to see if any of the larger page blocks have the PageBuddy flag set.
In practice incorrectly considering a page block as a suitable migration
target doesn't actually cause the block to be broken down. That block is
passed to isolate_freepages_block() which will scan it for any pages
currently in the buddy list. The assumption is that buddy list nodes
will be found because the entire block is not free. In the case
described above actually no buddy list nodes will be found because the
higher order block is free. It's just unnecessary scanning.
As such, the user visible effect of this change is only (in theory [1])
very slightly faster huge compaction by avoiding scanning entirely free
blocks for free pages. Even if the effect is negligible, this change
better conveys what the function is attempting to do: check whether this
page block is entirely free or not.
[1] I have not actually measured whether the difference is noticeable.
Notes and caveats for this RFC:
- If the supplied struct page is already the "left most" page in a
MAX_ORDER block, the page will be checked multiple times unnecessarily.
Iterating up the orders will result in zeroing bits which were already
zero. Not sure if we want to get fancier here and detect this by finding
the starting order?
- The PFN bit masking is somewhat yucky. We could use helper functions
but the ones I know of rely on knowing the existing order of the
supplied struct page, which this function is currently oblivious to.
- Is this change even worth it? My contention is "yes" or else this
function wouldn't bother checking the PageBuddy flag today - it clearly
wants to try to avoid unnecessary scans... Either let's do the job
properly or delete the check rather than a half job.
Suggested-by: Jan H. Schönherr <[email protected]>
Signed-off-by: James Gowans <[email protected]>
Cc: Andrew Morton <[email protected]>
Cc: Vlastimil Babka <[email protected]>
Cc: Baolin Wang <[email protected]>
Cc: Mel Gorman <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Cc: Johannes Weiner <[email protected]>
Cc: Kefeng Wang <[email protected]>
Cc: Minghao Chi <[email protected]>
---
mm/compaction.c | 39 ++++++++++++++++++++++++++++++++-------
1 file changed, 32 insertions(+), 7 deletions(-)
diff --git a/mm/compaction.c b/mm/compaction.c
index 9641e2131901..fb0e37d99364 100644
--- a/mm/compaction.c
+++ b/mm/compaction.c
@@ -1342,15 +1342,40 @@ static bool suitable_migration_source(struct compact_control *cc,
static bool suitable_migration_target(struct compact_control *cc,
struct page *page)
{
- /* If the page is a large free page, then disallow migration */
- if (PageBuddy(page)) {
+ unsigned int higher_order;
+ /*
+ * If the supplied page is part of a pageblock_order or larger free
+ * block it is not a suitable migration target block. Detect this case
+ * by starting at the pageorder_block aligned page and scan upwards to
+ * MAX_ORDER aligned page. Scan to see if any of the struct pages are
+ * in the buddy list for the order of the larger block. Disallow
+ * migration if so.
+ */
+ for (higher_order = pageblock_order; higher_order <= MAX_ORDER; ++higher_order) {
+ struct page *higher_order_page;
+ unsigned long higher_order_pfn;
/*
- * 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.
+ * This is legal provided that struct pages are always initialised
+ * to at least start at MAX_ORDER alignment.
*/
- if (buddy_order_unsafe(page) >= pageblock_order)
- return false;
+ higher_order_pfn &= ~((1 << higher_order) - 1);
+ higher_order_page = pfn_to_page(higher_order_pfn);
+ if (PageBuddy(higher_order_page)) {
+ /*
+ * 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(higher_order_page) >= higher_order)
+ return false;
+ /*
+ * This is a buddy but not a sufficiently large buddy.
+ * There will never be a larger one above this.
+ */
+ else
+ break;
+ }
+
}
if (cc->ignore_block_suitable)
--
2.25.1
On Wed, Jul 12, 2023 at 05:54:21PM +0200, James Gowans wrote:
> Huge page compaction finds free target pages to which source pages can
> be migrated when compacting. A huge page sized and aligned block is
> considered a valid source of target pages if it passes the
> suitable_migration_target() test. One of the things which
> suitable_migration_target() does is to ensure that the entire block
> isn't currently free. It would counter productive to use an already
> fully free huge page sized block as a migration target because using
> pages from that free huge page block would decrease the number of
> available huge pages in the system.
>
> suitable_migration_source() attempts to ensure that the supplied block
> is not currently a fully free block by checking PageBuddy flag on the
> starting page of the huge page sized and aligned block. This approach is
> flawed: the buddy list can and does maintain buddies at a larger order
> than huge page size. For example on a typical x86 system, huge page
> pageblock_order is 2 MiB, but the buddy list MAX_ORDER is 4 MiB. Because
> of this, a pageblock_order sized block may be free because it is part of
> a larger order buddy list buddy, but the pageblock_order sized block
> won't itself be part of the buddy list, only the larger order block will
> be. The current suitable_migration_target() implementation of just
> checking the PageBuddy flag on the pageblock_order block is hence
> insufficient as it will appear that the block is not free and hence try
> to use it as a source of migration target pages.
>
> Enhance suitable_migration_target() to cater for this case by scanning
> up the buddy orders from the current pageblock_order page to MAX_ORDER
> to see if any of the larger page blocks have the PageBuddy flag set.
>
> In practice incorrectly considering a page block as a suitable migration
> target doesn't actually cause the block to be broken down. That block is
> passed to isolate_freepages_block() which will scan it for any pages
> currently in the buddy list. The assumption is that buddy list nodes
> will be found because the entire block is not free. In the case
> described above actually no buddy list nodes will be found because the
> higher order block is free. It's just unnecessary scanning.
>
> As such, the user visible effect of this change is only (in theory [1])
> very slightly faster huge compaction by avoiding scanning entirely free
> blocks for free pages. Even if the effect is negligible, this change
> better conveys what the function is attempting to do: check whether this
> page block is entirely free or not.
>
> [1] I have not actually measured whether the difference is noticeable.
This is an interesting find. But because it's working correctly right
now, this patch is a performance optimization, so numbers would help.
> @@ -1342,15 +1342,40 @@ static bool suitable_migration_source(struct compact_control *cc,
> static bool suitable_migration_target(struct compact_control *cc,
> struct page *page)
> {
> - /* If the page is a large free page, then disallow migration */
> - if (PageBuddy(page)) {
> + unsigned int higher_order;
> + /*
> + * If the supplied page is part of a pageblock_order or larger free
> + * block it is not a suitable migration target block. Detect this case
> + * by starting at the pageorder_block aligned page and scan upwards to
> + * MAX_ORDER aligned page. Scan to see if any of the struct pages are
> + * in the buddy list for the order of the larger block. Disallow
> + * migration if so.
> + */
> + for (higher_order = pageblock_order; higher_order <= MAX_ORDER; ++higher_order) {
> + struct page *higher_order_page;
> + unsigned long higher_order_pfn;
> /*
> - * 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.
> + * This is legal provided that struct pages are always initialised
> + * to at least start at MAX_ORDER alignment.
> */
> - if (buddy_order_unsafe(page) >= pageblock_order)
> - return false;
> + higher_order_pfn &= ~((1 << higher_order) - 1);
> + higher_order_page = pfn_to_page(higher_order_pfn);
> + if (PageBuddy(higher_order_page)) {
> + /*
> + * 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(higher_order_page) >= higher_order)
> + return false;
> + /*
> + * This is a buddy but not a sufficiently large buddy.
> + * There will never be a larger one above this.
> + */
> + else
> + break;
> + }
One thing that's unfortunate is that isolate_freepages() will still
just skip one pageblock, even if you find the buddy further away than
that. This would check the same range at least twice (or more,
depending on the distance between pageblock_order and MAX_ORDER).
Instead of returning bool, it could make sense to return the pfn of
where you find the buddy, and then have isolate_freepages() skip and
continue the search at the pageblock below that.
Btw, this is also fixed by my patches that add the MIGRATE_FREE
type[1]. It has isolate_freepages() check this block type instead of
PageBuddy(), and that's set for all subblocks in a larger buddy.
[1] https://lore.kernel.org/lkml/[email protected]/ ]