lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <20230712155421.875491-1-jgowans@amazon.com>
Date:   Wed, 12 Jul 2023 17:54:21 +0200
From:   James Gowans <jgowans@...zon.com>
To:     unlisted-recipients:; (no To-header on input)
CC:     <linux-mm@...ck.org>, <linux-kernel@...r.kernel.org>,
        James Gowans <jgowans@...zon.com>,
        Jan H . Schönherr <jschoenh@...zon.de>,
        Andrew Morton <akpm@...ux-foundation.org>,
        Vlastimil Babka <vbabka@...e.cz>,
        Baolin Wang <baolin.wang@...ux.alibaba.com>,
        Mel Gorman <mgorman@...hsingularity.net>,
        Matthew Wilcox <willy@...radead.org>,
        Johannes Weiner <hannes@...xchg.org>,
        Kefeng Wang <wangkefeng.wang@...wei.com>,
        Minghao Chi <chi.minghao@....com.cn>
Subject: [RFC] mm: compaction: suitable_migration_target checks for higher order buddies

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 <jschoenh@...zon.de>
Signed-off-by: James Gowans <jgowans@...zon.com>
Cc: Andrew Morton <akpm@...ux-foundation.org>
Cc: Vlastimil Babka <vbabka@...e.cz>
Cc: Baolin Wang <baolin.wang@...ux.alibaba.com>
Cc: Mel Gorman <mgorman@...hsingularity.net>
Cc: Matthew Wilcox <willy@...radead.org>
Cc: Johannes Weiner <hannes@...xchg.org>
Cc: Kefeng Wang <wangkefeng.wang@...wei.com>
Cc: Minghao Chi <chi.minghao@....com.cn>
---
 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

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ