Commit 6e2b7044 authored by Vlastimil Babka's avatar Vlastimil Babka Committed by Linus Torvalds

mm, compaction: make fast_isolate_freepages() stay within zone

Compaction always operates on pages from a single given zone when
isolating both pages to migrate and freepages.  Pageblock boundaries are
intersected with zone boundaries to be safe in case zone starts or ends in
the middle of pageblock.  The use of pageblock_pfn_to_page() protects
against non-contiguous pageblocks.

The functions fast_isolate_freepages() and fast_isolate_around() don't
currently protect the fast freepage isolation thoroughly enough against
these corner cases, and can result in freepage isolation operate outside
of zone boundaries:

 - in fast_isolate_freepages() if we get a pfn from the first pageblock
   of a zone that starts in the middle of that pageblock, 'highest' can
   be a pfn outside of the zone.

   If we fail to isolate anything in this function, we may then call
   fast_isolate_around() on a pfn outside of the zone and there
   effectively do a set_pageblock_skip(page_to_pfn(highest)) which may
   currently hit a VM_BUG_ON() in some configurations

 - fast_isolate_around() checks only the zone end boundary and not
   beginning, nor that the pageblock is contiguous (with
   pageblock_pfn_to_page()) so it's possible that we end up calling
   isolate_freepages_block() on a range of pfn's from two different
   zones and end up e.g. isolating freepages under the wrong zone's
   lock.

This patch should fix the above issues.

Link: https://lkml.kernel.org/r/20210217173300.6394-1-vbabka@suse.cz
Fixes: 5a811889 ("mm, compaction: use free lists to quickly locate a migration target")
Signed-off-by: default avatarVlastimil Babka <vbabka@suse.cz>
Acked-by: default avatarDavid Rientjes <rientjes@google.com>
Acked-by: default avatarMel Gorman <mgorman@techsingularity.net>
Cc: Andrea Arcangeli <aarcange@redhat.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: <stable@vger.kernel.org>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 15d28d0d
...@@ -1284,7 +1284,7 @@ static void ...@@ -1284,7 +1284,7 @@ static void
fast_isolate_around(struct compact_control *cc, unsigned long pfn, unsigned long nr_isolated) fast_isolate_around(struct compact_control *cc, unsigned long pfn, unsigned long nr_isolated)
{ {
unsigned long start_pfn, end_pfn; unsigned long start_pfn, end_pfn;
struct page *page = pfn_to_page(pfn); struct page *page;
/* Do not search around if there are enough pages already */ /* Do not search around if there are enough pages already */
if (cc->nr_freepages >= cc->nr_migratepages) if (cc->nr_freepages >= cc->nr_migratepages)
...@@ -1295,8 +1295,12 @@ fast_isolate_around(struct compact_control *cc, unsigned long pfn, unsigned long ...@@ -1295,8 +1295,12 @@ fast_isolate_around(struct compact_control *cc, unsigned long pfn, unsigned long
return; return;
/* Pageblock boundaries */ /* Pageblock boundaries */
start_pfn = pageblock_start_pfn(pfn); start_pfn = max(pageblock_start_pfn(pfn), cc->zone->zone_start_pfn);
end_pfn = min(pageblock_end_pfn(pfn), zone_end_pfn(cc->zone)) - 1; end_pfn = min(pageblock_end_pfn(pfn), zone_end_pfn(cc->zone));
page = pageblock_pfn_to_page(start_pfn, end_pfn, cc->zone);
if (!page)
return;
/* Scan before */ /* Scan before */
if (start_pfn != pfn) { if (start_pfn != pfn) {
...@@ -1398,7 +1402,8 @@ fast_isolate_freepages(struct compact_control *cc) ...@@ -1398,7 +1402,8 @@ fast_isolate_freepages(struct compact_control *cc)
pfn = page_to_pfn(freepage); pfn = page_to_pfn(freepage);
if (pfn >= highest) if (pfn >= highest)
highest = pageblock_start_pfn(pfn); highest = max(pageblock_start_pfn(pfn),
cc->zone->zone_start_pfn);
if (pfn >= low_pfn) { if (pfn >= low_pfn) {
cc->fast_search_fail = 0; cc->fast_search_fail = 0;
...@@ -1468,7 +1473,8 @@ fast_isolate_freepages(struct compact_control *cc) ...@@ -1468,7 +1473,8 @@ fast_isolate_freepages(struct compact_control *cc)
} else { } else {
if (cc->direct_compaction && pfn_valid(min_pfn)) { if (cc->direct_compaction && pfn_valid(min_pfn)) {
page = pageblock_pfn_to_page(min_pfn, page = pageblock_pfn_to_page(min_pfn,
pageblock_end_pfn(min_pfn), min(pageblock_end_pfn(min_pfn),
zone_end_pfn(cc->zone)),
cc->zone); cc->zone);
cc->free_pfn = min_pfn; cc->free_pfn = min_pfn;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment