Skip to contentSkip to conversation

mm/compaction: count pages and stop correctly during page isolation.

PATCH Superseded Zi Yan 6 participants 19 messages Oct 29, 2020 – Nov 2, 2020

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Zi Yan

author
#0 lore raw

PATCH 1/1 mm/compaction: count pages and stop correctly during page isolation.

From: Zi Yan <[email protected]>

In isolate_migratepages_block, when cc->alloc_contig is true, we are able to isolate compound pages, nr_migratepages and nr_isolated did not count compound pages correctly, causing us to isolate more pages than we thought. Use thp_nr_pages to count pages. Otherwise, we might be trapped in too_many_isolated while loop, since the actual isolated pages can go up to COMPACT_CLUSTER_MAX*512=16384, where COMPACT_CLUSTER_MAX is 32, since we stop isolation after cc->nr_migratepages reaches to COMPACT_CLUSTER_MAX.

In addition, after we fix the issue above, cc->nr_migratepages could never be equal to COMPACT_CLUSTER_MAX if compound pages are isolated, thus page isolation could not stop as we intended. Change the isolation stop condition to >=.

---
 mm/compaction.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
mm/compaction.c +4 -4
index ee1f8439369e..0683a4999581 100644
@@ -1012,8 +1012,8 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
 
 isolate_success:
 		list_add(&page->lru, &cc->migratepages);
-		cc->nr_migratepages++;
-		nr_isolated++;
+		cc->nr_migratepages += thp_nr_pages(page);
+		nr_isolated += thp_nr_pages(page);
 
 		/*
 		 * Avoid isolating too much unless this block is being
@@ -1021,7 +1021,7 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
 		 * or a lock is contended. For contention, isolate quickly to
 		 * potentially remove one source of contention.
 		 */
-		if (cc->nr_migratepages == COMPACT_CLUSTER_MAX &&
+		if (cc->nr_migratepages >= COMPACT_CLUSTER_MAX &&
 		    !cc->rescan && !cc->contended) {
 			++low_pfn;
 			break;
@@ -1132,7 +1132,7 @@ isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn,
 		if (!pfn)
 			break;
 
-		if (cc->nr_migratepages == COMPACT_CLUSTER_MAX)
+		if (cc->nr_migratepages >= COMPACT_CLUSTER_MAX)
 			break;
 	}
 
Signature · 1 line
2.28.0
#1 lore raw
Quoted 9 lines · Zi Yan

From: Zi Yan <[email protected]>

In isolate_migratepages_block, when cc->alloc_contig is true, we are able to isolate compound pages, nr_migratepages and nr_isolated did not count compound pages correctly, causing us to isolate more pages than we thought. Use thp_nr_pages to count pages. Otherwise, we might be trapped in too_many_isolated while loop, since the actual isolated pages can go up to COMPACT_CLUSTER_MAX*512=16384, where COMPACT_CLUSTER_MAX is 32,

Is it that easy to run into? 16384 doesn't seem like too many pages, just 64MB.

Quoted 7 lines

since we stop isolation after cc->nr_migratepages reaches to COMPACT_CLUSTER_MAX.

In addition, after we fix the issue above, cc->nr_migratepages could never be equal to COMPACT_CLUSTER_MAX if compound pages are isolated, thus page isolation could not stop as we intended. Change the isolation stop condition to >=.

The fix looks sane to me. Reviewed-by: Yang Shi <[email protected]>

Shall you add Fixes tag to commit 1da2f328fa643bd72197dfed0c655148af31e4eb? And may cc stable.

Quoted 43 lines
---
 mm/compaction.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
mm/compaction.c +4 -5
index ee1f8439369e..0683a4999581 100644
@@ -1012,8 +1012,8 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
 
 isolate_success:
                list_add(&page->lru, &cc->migratepages);
-               cc->nr_migratepages++;
-               nr_isolated++;
+               cc->nr_migratepages += thp_nr_pages(page);
+               nr_isolated += thp_nr_pages(page);
 
                /*
                 * Avoid isolating too much unless this block is being
@@ -1021,7 +1021,7 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
                 * or a lock is contended. For contention, isolate quickly to
                 * potentially remove one source of contention.
                 */
-               if (cc->nr_migratepages == COMPACT_CLUSTER_MAX &&
+               if (cc->nr_migratepages >= COMPACT_CLUSTER_MAX &&
                    !cc->rescan && !cc->contended) {
                        ++low_pfn;
                        break;
@@ -1132,7 +1132,7 @@ isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn,
                if (!pfn)
                        break;
 
-               if (cc->nr_migratepages == COMPACT_CLUSTER_MAX)
+               if (cc->nr_migratepages >= COMPACT_CLUSTER_MAX)
                        break;
        }
 
--

2.28.0

reviewer
#2 lore raw
Quoted 4 lines · Yang Shi
Quoted 3 lines · Zi Yan

From: Zi Yan <[email protected]>

Quoted 8 lines
Quoted 6 lines

In isolate_migratepages_block, when cc->alloc_contig is true, we are able to isolate compound pages, nr_migratepages and nr_isolated did not count compound pages correctly, causing us to isolate more pages than we thought. Use thp_nr_pages to count pages. Otherwise, we might be trapped in too_many_isolated while loop, since the actual isolated pages can go up to COMPACT_CLUSTER_MAX*512=16384, where COMPACT_CLUSTER_MAX is 32,

Is it that easy to run into? 16384 doesn't seem like too many pages, just 64MB.

I hit this when I was running oom01 from ltp to test my PUD THP patchset, which allocates PUD THPs from CMA regions and splits them into PMD THPs due to memory pressure. I am not sure if it is common that in the upstream kernel PMD THPs will be allocated in CMA regions due to allocation fallback.

Quoted 10 lines
Quoted 8 lines

since we stop isolation after cc->nr_migratepages reaches to COMPACT_CLUSTER_MAX.

In addition, after we fix the issue above, cc->nr_migratepages could never be equal to COMPACT_CLUSTER_MAX if compound pages are isolated, thus page isolation could not stop as we intended. Change the isolation stop condition to >=.

The fix looks sane to me. Reviewed-by: Yang Shi <[email protected]>

Thanks.

Quoted 3 lines

Shall you add Fixes tag to commit 1da2f328fa643bd72197dfed0c655148af31e4eb? And may cc stable.

Sure.

Fixes 1da2f328fa64 (“mm,thp,compaction,cma: allow THP migration for CMA allocations”)

stable cc’ed.

Quoted 40 lines
Quoted 40 lines
---
 mm/compaction.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
mm/compaction.c +4 -4
index ee1f8439369e..0683a4999581 100644
@@ -1012,8 +1012,8 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
 
 isolate_success:
                list_add(&page->lru, &cc->migratepages);
-               cc->nr_migratepages++;
-               nr_isolated++;
+               cc->nr_migratepages += thp_nr_pages(page);
+               nr_isolated += thp_nr_pages(page);
 
                /*
                 * Avoid isolating too much unless this block is being
@@ -1021,7 +1021,7 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
                 * or a lock is contended. For contention, isolate quickly to
                 * potentially remove one source of contention.
                 */
-               if (cc->nr_migratepages == COMPACT_CLUSTER_MAX &&
+               if (cc->nr_migratepages >= COMPACT_CLUSTER_MAX &&
                    !cc->rescan && !cc->contended) {
                        ++low_pfn;
                        break;
@@ -1132,7 +1132,7 @@ isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn,
                if (!pfn)
                        break;
 
-               if (cc->nr_migratepages == COMPACT_CLUSTER_MAX)
+               if (cc->nr_migratepages >= COMPACT_CLUSTER_MAX)
                        break;
        }
 
Quoted 4 lines
Quoted 4 lines

--
2.28.0

—
Best Regards,
Yan Zi

maintainer
#3 lore raw
Quoted 9 lines · On Thu, 29 Oct 2020 17:31:28 -0400 Zi Yan <[email protected]> wrote:
Quoted 3 lines

Shall you add Fixes tag to commit 1da2f328fa643bd72197dfed0c655148af31e4eb? And may cc stable.

Sure.

Fixes 1da2f328fa64 (“mm,thp,compaction,cma: allow THP migration for CMA allocations”)

stable cc'ed.

A think a cc:stable really requires a description of the end-user visible effects of the bug. Could you please provide that?

reviewer
#4 lore raw
Quoted 6 lines · Andrew Morton
Quoted 4 lines · On Thu, 29 Oct 2020 17:31:28 -0400 Zi Yan <[email protected]> wrote:
Quoted 3 lines

Shall you add Fixes tag to commit 1da2f328fa643bd72197dfed0c655148af31e4eb? And may cc stable.

Quoted 8 lines
Quoted 5 lines

Sure.

Fixes 1da2f328fa64 (“mm,thp,compaction,cma: allow THP migration for CMA allocations”)

stable cc'ed.

A think a cc:stable really requires a description of the end-user visible effects of the bug. Could you please provide that?

Sure.

For example, in a system with 16GB memory and an 8GB CMA region reserved by hugetlb_cma, if we first allocate 10GB THPs and mlock them (so some THPs are allocated in the CMA region and mlocked), reserving 6 1GB hugetlb pages via /sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages will get stuck (looping in too_many_isolated function) until we kill either task. With the patch applied, oom will kill the application with 10GB THPs and let hugetlb page reservation finish.

—
Best Regards,
Yan Zi

reviewer
#5 lore raw
In reply to Zi Yan

[Cc Vlastimil]

Quoted 26 lines · Zi Yan

From: Zi Yan <[email protected]>

In isolate_migratepages_block, when cc->alloc_contig is true, we are able to isolate compound pages, nr_migratepages and nr_isolated did not count compound pages correctly, causing us to isolate more pages than we thought. Use thp_nr_pages to count pages. Otherwise, we might be trapped in too_many_isolated while loop, since the actual isolated pages can go up to COMPACT_CLUSTER_MAX*512=16384, where COMPACT_CLUSTER_MAX is 32, since we stop isolation after cc->nr_migratepages reaches to COMPACT_CLUSTER_MAX.

In addition, after we fix the issue above, cc->nr_migratepages could never be equal to COMPACT_CLUSTER_MAX if compound pages are isolated, thus page isolation could not stop as we intended. Change the isolation stop condition to >=.

---
 mm/compaction.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
mm/compaction.c +0 -0
index ee1f8439369e..0683a4999581 100644

mm/compaction.c isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, · line 1016

Quoted 7 lines
mm/compaction.c +2 -2
@@ -1012,8 +1012,8 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
 
 isolate_success:
 		list_add(&page->lru, &cc->migratepages);
-		cc->nr_migratepages++;
-		nr_isolated++;
+		cc->nr_migratepages += thp_nr_pages(page);
+		nr_isolated += thp_nr_pages(page);

Does thp_nr_pages work for __PageMovable pages?

Signature · 2 lines
Michal Hocko
SUSE Labs
reviewer
#6 lore raw
Quoted 30 lines · Michal Hocko

[Cc Vlastimil]

Quoted 27 lines · Zi Yan

From: Zi Yan <[email protected]>

In isolate_migratepages_block, when cc->alloc_contig is true, we are able to isolate compound pages, nr_migratepages and nr_isolated did not count compound pages correctly, causing us to isolate more pages than we thought. Use thp_nr_pages to count pages. Otherwise, we might be trapped in too_many_isolated while loop, since the actual isolated pages can go up to COMPACT_CLUSTER_MAX*512=16384, where COMPACT_CLUSTER_MAX is 32, since we stop isolation after cc->nr_migratepages reaches to COMPACT_CLUSTER_MAX.

In addition, after we fix the issue above, cc->nr_migratepages could never be equal to COMPACT_CLUSTER_MAX if compound pages are isolated, thus page isolation could not stop as we intended. Change the isolation stop condition to >=.

---
 mm/compaction.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
mm/compaction.c +0 -0
index ee1f8439369e..0683a4999581 100644
@@ -1012,8 +1012,8 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
 
Quoted 8 lines

mm/compaction.c isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, · line 1016

Quoted 6 lines
mm/compaction.c +2 -2
 isolate_success:
 		list_add(&page->lru, &cc->migratepages);
-		cc->nr_migratepages++;
-		nr_isolated++;
+		cc->nr_migratepages += thp_nr_pages(page);
+		nr_isolated += thp_nr_pages(page);

Does thp_nr_pages work for __PageMovable pages?

Yes. It is the same as compound_nr() but compiled
to 1 when THP is not enabled.

—
Best Regards,
Yan Zi

reviewer
#7 lore raw
Quoted 41 lines · Zi Yan
Quoted 30 lines · Michal Hocko

[Cc Vlastimil]

Quoted 27 lines · Zi Yan

From: Zi Yan <[email protected]>

In isolate_migratepages_block, when cc->alloc_contig is true, we are able to isolate compound pages, nr_migratepages and nr_isolated did not count compound pages correctly, causing us to isolate more pages than we thought. Use thp_nr_pages to count pages. Otherwise, we might be trapped in too_many_isolated while loop, since the actual isolated pages can go up to COMPACT_CLUSTER_MAX*512=16384, where COMPACT_CLUSTER_MAX is 32, since we stop isolation after cc->nr_migratepages reaches to COMPACT_CLUSTER_MAX.

In addition, after we fix the issue above, cc->nr_migratepages could never be equal to COMPACT_CLUSTER_MAX if compound pages are isolated, thus page isolation could not stop as we intended. Change the isolation stop condition to >=.

---
 mm/compaction.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
mm/compaction.c +0 -0
index ee1f8439369e..0683a4999581 100644
@@ -1012,8 +1012,8 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
 
Quoted 8 lines

mm/compaction.c isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, · line 1016

Quoted 6 lines
mm/compaction.c +2 -2
 isolate_success:
 		list_add(&page->lru, &cc->migratepages);
-		cc->nr_migratepages++;
-		nr_isolated++;
+		cc->nr_migratepages += thp_nr_pages(page);
+		nr_isolated += thp_nr_pages(page);

Does thp_nr_pages work for __PageMovable pages?

Quoted 2 lines

Yes. It is the same as compound_nr() but compiled
to 1 when THP is not enabled.

I am sorry but I do not follow. First of all the implementation of the two is different and also I was asking about __PageMovable which should never be THP IIRC. Can they be compound though?

Signature · 2 lines
Michal Hocko
SUSE Labs
reviewer
#8 lore raw
Quoted 42 lines · Michal Hocko
Quoted 41 lines · Zi Yan
Quoted 38 lines · Michal Hocko

[Cc Vlastimil]

Quoted 26 lines · Zi Yan

From: Zi Yan <[email protected]>

In isolate_migratepages_block, when cc->alloc_contig is true, we are able to isolate compound pages, nr_migratepages and nr_isolated did not count compound pages correctly, causing us to isolate more pages than we thought. Use thp_nr_pages to count pages. Otherwise, we might be trapped in too_many_isolated while loop, since the actual isolated pages can go up to COMPACT_CLUSTER_MAX*512=16384, where COMPACT_CLUSTER_MAX is 32, since we stop isolation after cc->nr_migratepages reaches to COMPACT_CLUSTER_MAX.

In addition, after we fix the issue above, cc->nr_migratepages could never be equal to COMPACT_CLUSTER_MAX if compound pages are isolated, thus page isolation could not stop as we intended. Change the isolation stop condition to >=.

---
 mm/compaction.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
mm/compaction.c +0 -0
index ee1f8439369e..0683a4999581 100644

mm/compaction.c isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, · line 1016

Quoted 7 lines
mm/compaction.c +2 -2
@@ -1012,8 +1012,8 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
 
 isolate_success:
 		list_add(&page->lru, &cc->migratepages);
-		cc->nr_migratepages++;
-		nr_isolated++;
+		cc->nr_migratepages += thp_nr_pages(page);
+		nr_isolated += thp_nr_pages(page);

Does thp_nr_pages work for __PageMovable pages?

Quoted 6 lines
Quoted 2 lines

Yes. It is the same as compound_nr() but compiled
to 1 when THP is not enabled.

I am sorry but I do not follow. First of all the implementation of the two is different and also I was asking about __PageMovable which should never be THP IIRC. Can they be compound though?

__PageMovable, non-lru movable pages, can be compound and thp_nr_page cannot be used for it, since when THP is off, thp_nr_page will return the wrong number. I got confused by its name, sorry.

But __PageMovable is irrelevant to this patch, since we are using __isolate_lru_page to isolate pages. non-lru __PageMovable should not appear after isolate_succes. thp_nr_pages can be used here.

—
Best Regards,
Yan Zi

reviewer
#9 lore raw
Quoted 47 lines · Zi Yan
Quoted 45 lines · Michal Hocko
Quoted 43 lines · Zi Yan
Quoted 30 lines · Michal Hocko

[Cc Vlastimil]

Quoted 27 lines · Zi Yan

From: Zi Yan <[email protected]>

In isolate_migratepages_block, when cc->alloc_contig is true, we are able to isolate compound pages, nr_migratepages and nr_isolated did not count compound pages correctly, causing us to isolate more pages than we thought. Use thp_nr_pages to count pages. Otherwise, we might be trapped in too_many_isolated while loop, since the actual isolated pages can go up to COMPACT_CLUSTER_MAX*512=16384, where COMPACT_CLUSTER_MAX is 32, since we stop isolation after cc->nr_migratepages reaches to COMPACT_CLUSTER_MAX.

In addition, after we fix the issue above, cc->nr_migratepages could never be equal to COMPACT_CLUSTER_MAX if compound pages are isolated, thus page isolation could not stop as we intended. Change the isolation stop condition to >=.

---
 mm/compaction.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
mm/compaction.c +0 -0
index ee1f8439369e..0683a4999581 100644
@@ -1012,8 +1012,8 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
 
Quoted 8 lines

mm/compaction.c isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, · line 1016

Quoted 6 lines
mm/compaction.c +2 -2
 isolate_success:
 		list_add(&page->lru, &cc->migratepages);
-		cc->nr_migratepages++;
-		nr_isolated++;
+		cc->nr_migratepages += thp_nr_pages(page);
+		nr_isolated += thp_nr_pages(page);

Does thp_nr_pages work for __PageMovable pages?

Yes. It is the same as compound_nr() but compiled
to 1 when THP is not enabled.

Quoted 7 lines
Quoted 3 lines

I am sorry but I do not follow. First of all the implementation of the two is different and also I was asking about __PageMovable which should never be THP IIRC. Can they be compound though?

__PageMovable, non-lru movable pages, can be compound and thp_nr_page cannot be used for it, since when THP is off, thp_nr_page will return the wrong number. I got confused by its name, sorry.

OK, this matches my understanding. Good we are on the same page.

Quoted 3 lines

But __PageMovable is irrelevant to this patch, since we are using __isolate_lru_page to isolate pages. non-lru __PageMovable should not appear after isolate_succes. thp_nr_pages can be used here.

But this is still not clear to me. __PageMovable pages are isolated by isolate_movable_page and then jump to isolate_succes. Does that somehow changes the nature of the page being compound or tat thp_nr_page would start working on those pages.

Signature · 2 lines
Michal Hocko
SUSE Labs
#17 lore raw
In reply to Zi Yan
Quoted 10 lines · Zi Yan

From: Zi Yan <[email protected]>

In isolate_migratepages_block, when cc->alloc_contig is true, we are able to isolate compound pages, nr_migratepages and nr_isolated did not count compound pages correctly, causing us to isolate more pages than we thought. Use thp_nr_pages to count pages. Otherwise, we might be trapped in too_many_isolated while loop, since the actual isolated pages can go up to COMPACT_CLUSTER_MAX*512=16384, where COMPACT_CLUSTER_MAX is 32, since we stop isolation after cc->nr_migratepages reaches to COMPACT_CLUSTER_MAX.

I wonder if a better fix would be to adjust the too_many_isolated() check so that if we have non-zero cc->nr_migratepages, we bail out from further isolation and migrate what we have immediately, instead of looping.

Because I can also imagine a hypothetical situation where multiple threads in parallel cause too_many_isolated() to be true, and will all loop there forever. The proposed fix should prevent such situation as well, AFAICT.

Quoted 44 lines

In addition, after we fix the issue above, cc->nr_migratepages could never be equal to COMPACT_CLUSTER_MAX if compound pages are isolated, thus page isolation could not stop as we intended. Change the isolation stop condition to >=.

---
  mm/compaction.c | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
mm/compaction.c +4 -4
index ee1f8439369e..0683a4999581 100644
@@ -1012,8 +1012,8 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
 
  isolate_success:
  		list_add(&page->lru, &cc->migratepages);
-		cc->nr_migratepages++;
-		nr_isolated++;
+		cc->nr_migratepages += thp_nr_pages(page);
+		nr_isolated += thp_nr_pages(page);
 
  		/*
  		 * Avoid isolating too much unless this block is being
@@ -1021,7 +1021,7 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
  		 * or a lock is contended. For contention, isolate quickly to
  		 * potentially remove one source of contention.
  		 */
-		if (cc->nr_migratepages == COMPACT_CLUSTER_MAX &&
+		if (cc->nr_migratepages >= COMPACT_CLUSTER_MAX &&
  		    !cc->rescan && !cc->contended) {
  			++low_pfn;
  			break;
@@ -1132,7 +1132,7 @@ isolate_migratepages_range(struct compact_control *cc, unsigned long start_pfn,
  		if (!pfn)
  			break;
 
-		if (cc->nr_migratepages == COMPACT_CLUSTER_MAX)
+		if (cc->nr_migratepages >= COMPACT_CLUSTER_MAX)
  			break;
  	}
 
reviewer
#10 lore raw
In reply to Michal Hocko
Quoted 58 lines · Michal Hocko
Quoted 47 lines · Zi Yan
Quoted 45 lines · Michal Hocko
Quoted 43 lines · Zi Yan
Quoted 30 lines · Michal Hocko

[Cc Vlastimil]

Quoted 27 lines · Zi Yan

From: Zi Yan <[email protected]>

In isolate_migratepages_block, when cc->alloc_contig is true, we are able to isolate compound pages, nr_migratepages and nr_isolated did not count compound pages correctly, causing us to isolate more pages than we thought. Use thp_nr_pages to count pages. Otherwise, we might be trapped in too_many_isolated while loop, since the actual isolated pages can go up to COMPACT_CLUSTER_MAX*512=16384, where COMPACT_CLUSTER_MAX is 32, since we stop isolation after cc->nr_migratepages reaches to COMPACT_CLUSTER_MAX.

In addition, after we fix the issue above, cc->nr_migratepages could never be equal to COMPACT_CLUSTER_MAX if compound pages are isolated, thus page isolation could not stop as we intended. Change the isolation stop condition to >=.

---
 mm/compaction.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
mm/compaction.c +0 -0
index ee1f8439369e..0683a4999581 100644
@@ -1012,8 +1012,8 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
 
Quoted 8 lines

mm/compaction.c isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, · line 1016

Quoted 6 lines
mm/compaction.c +2 -2
 isolate_success:
 		list_add(&page->lru, &cc->migratepages);
-		cc->nr_migratepages++;
-		nr_isolated++;
+		cc->nr_migratepages += thp_nr_pages(page);
+		nr_isolated += thp_nr_pages(page);

Does thp_nr_pages work for __PageMovable pages?

Yes. It is the same as compound_nr() but compiled
to 1 when THP is not enabled.

Quoted 7 lines
Quoted 3 lines

I am sorry but I do not follow. First of all the implementation of the two is different and also I was asking about __PageMovable which should never be THP IIRC. Can they be compound though?

__PageMovable, non-lru movable pages, can be compound and thp_nr_page cannot be used for it, since when THP is off, thp_nr_page will return the wrong number. I got confused by its name, sorry.

OK, this matches my understanding. Good we are on the same page.

Quoted 8 lines
Quoted 3 lines

But __PageMovable is irrelevant to this patch, since we are using __isolate_lru_page to isolate pages. non-lru __PageMovable should not appear after isolate_succes. thp_nr_pages can be used here.

But this is still not clear to me. __PageMovable pages are isolated by isolate_movable_page and then jump to isolate_succes. Does that somehow changes the nature of the page being compound or tat thp_nr_page would start working on those pages.

Ah, I missed that part of the code. If __PageMovable can reach the code, we should use compound_nr instead. Will send v2 to fix it. Thanks.

—
Best Regards,
Yan Zi

#11 lore raw
In reply to Michal Hocko
Quoted 25 lines · Michal Hocko
Quoted 14 lines · Zi Yan
Quoted 12 lines · Michal Hocko
Quoted 10 lines · Zi Yan
Quoted 5 lines · Michal Hocko

[Cc Vlastimil]

On Thu 29-10-20 16:04:35, Zi Yan wrote:

Does thp_nr_pages work for __PageMovable pages?

Yes. It is the same as compound_nr() but compiled
to 1 when THP is not enabled.

Quoted 7 lines
Quoted 3 lines

I am sorry but I do not follow. First of all the implementation of the two is different and also I was asking about __PageMovable which should never be THP IIRC. Can they be compound though?

__PageMovable, non-lru movable pages, can be compound and thp_nr_page cannot be used for it, since when THP is off, thp_nr_page will return the wrong number. I got confused by its name, sorry.

OK, this matches my understanding. Good we are on the same page.

Quoted 8 lines
Quoted 3 lines

But __PageMovable is irrelevant to this patch, since we are using __isolate_lru_page to isolate pages. non-lru __PageMovable should not appear after isolate_succes. thp_nr_pages can be used here.

But this is still not clear to me. __PageMovable pages are isolated by isolate_movable_page and then jump to isolate_succes. Does that somehow changes the nature of the page being compound or tat thp_nr_page would start working on those pages.

Agreed that page movable can appear after isolate_success. compound_nr() should work for both.
Note that too_many_isolated() doesn't see __PageMovable isolated pages, as they are not counted as NR_ISOLATED_FILE/NR_ISOLATED_ANON, AFAIK. So in that sense they are irrelevant to the bug at hand... for now.

reviewer
#18 lore raw
In reply to Vlastimil Babka
Quoted 12 lines · Vlastimil Babka
Quoted 10 lines · Zi Yan

From: Zi Yan <[email protected]>

In isolate_migratepages_block, when cc->alloc_contig is true, we are able to isolate compound pages, nr_migratepages and nr_isolated did not count compound pages correctly, causing us to isolate more pages than we thought. Use thp_nr_pages to count pages. Otherwise, we might be trapped in too_many_isolated while loop, since the actual isolated pages can go up to COMPACT_CLUSTER_MAX*512=16384, where COMPACT_CLUSTER_MAX is 32, since we stop isolation after cc->nr_migratepages reaches to COMPACT_CLUSTER_MAX.

Quoted 1 line

I wonder if a better fix would be to adjust the too_many_isolated() check so that if we have non-zero cc->nr_migratepages, we bail out from further isolation and migrate what we have immediately, instead of looping.

I just tested your fix and it works too. The difference is that with your fix alloc_contig_range will fail quickly without killing the user application mlocking THPs in the CMA region (for more context, please see my other email to Andrew explaining how to reproduce in userspace), whereas my fix will oom the user application and make alloc_contig_range successful at the end.

Anyway, I will add your fix below and send v2:

mm/compaction.c +3 -0
index ee1f8439369e..8fa11637ccfd 100644
@@ -817,6 +817,9 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
         * delay for some time until fewer pages are isolated
         */
        while (unlikely(too_many_isolated(pgdat))) {
+               /* stop isolation if there are still pages not migrated */
+               if (cc->nr_migratepages)
+                       return 0;
                /* async migration should just abort */
                if (cc->mode == MIGRATE_ASYNC)
                        return 0;
Quoted 2 lines

Because I can also imagine a hypothetical situation where multiple threads in parallel cause too_many_isolated() to be true, and will all loop there forever. The proposed fix should prevent such situation as well, AFAICT.

Yes. oom01 from ltp tests the multi-threaded situation and my fix works there too.

—
Best Regards,
Yan Zi

#12 lore raw
In reply to Michal Hocko
Quoted 43 lines · Michal Hocko
Quoted 41 lines · Zi Yan
Quoted 38 lines · Michal Hocko

[Cc Vlastimil]

Quoted 26 lines · Zi Yan

From: Zi Yan <[email protected]>

In isolate_migratepages_block, when cc->alloc_contig is true, we are able to isolate compound pages, nr_migratepages and nr_isolated did not count compound pages correctly, causing us to isolate more pages than we thought. Use thp_nr_pages to count pages. Otherwise, we might be trapped in too_many_isolated while loop, since the actual isolated pages can go up to COMPACT_CLUSTER_MAX*512=16384, where COMPACT_CLUSTER_MAX is 32, since we stop isolation after cc->nr_migratepages reaches to COMPACT_CLUSTER_MAX.

In addition, after we fix the issue above, cc->nr_migratepages could never be equal to COMPACT_CLUSTER_MAX if compound pages are isolated, thus page isolation could not stop as we intended. Change the isolation stop condition to >=.

---
 mm/compaction.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
mm/compaction.c +0 -0
index ee1f8439369e..0683a4999581 100644

mm/compaction.c isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, · line 1016

Quoted 7 lines
mm/compaction.c +2 -2
@@ -1012,8 +1012,8 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
 
 isolate_success:
           list_add(&page->lru, &cc->migratepages);
-          cc->nr_migratepages++;
-          nr_isolated++;
+          cc->nr_migratepages += thp_nr_pages(page);
+          nr_isolated += thp_nr_pages(page);

Does thp_nr_pages work for __PageMovable pages?

Quoted 6 lines
Quoted 2 lines

Yes. It is the same as compound_nr() but compiled
to 1 when THP is not enabled.

I am sorry but I do not follow. First of all the implementation of the two is different and also I was asking about __PageMovable which should never be THP IIRC. Can they be compound though?

I have the same question, can they be compound? If they can be compound, PageTransHuge() can't tell from THP and compound movable page, right?

Quoted 5 lines

--
Michal Hocko
SUSE Labs

reviewer
#13 lore raw
Quoted 47 lines · Yang Shi
Quoted 46 lines · Michal Hocko
Quoted 43 lines · Zi Yan
Quoted 30 lines · Michal Hocko

[Cc Vlastimil]

Quoted 27 lines · Zi Yan

From: Zi Yan <[email protected]>

In isolate_migratepages_block, when cc->alloc_contig is true, we are able to isolate compound pages, nr_migratepages and nr_isolated did not count compound pages correctly, causing us to isolate more pages than we thought. Use thp_nr_pages to count pages. Otherwise, we might be trapped in too_many_isolated while loop, since the actual isolated pages can go up to COMPACT_CLUSTER_MAX*512=16384, where COMPACT_CLUSTER_MAX is 32, since we stop isolation after cc->nr_migratepages reaches to COMPACT_CLUSTER_MAX.

In addition, after we fix the issue above, cc->nr_migratepages could never be equal to COMPACT_CLUSTER_MAX if compound pages are isolated, thus page isolation could not stop as we intended. Change the isolation stop condition to >=.

---
 mm/compaction.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
mm/compaction.c +0 -0
index ee1f8439369e..0683a4999581 100644
@@ -1012,8 +1012,8 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
 
Quoted 8 lines

mm/compaction.c isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, · line 1016

Quoted 6 lines
mm/compaction.c +2 -2
 isolate_success:
           list_add(&page->lru, &cc->migratepages);
-          cc->nr_migratepages++;
-          nr_isolated++;
+          cc->nr_migratepages += thp_nr_pages(page);
+          nr_isolated += thp_nr_pages(page);

Does thp_nr_pages work for __PageMovable pages?

Yes. It is the same as compound_nr() but compiled
to 1 when THP is not enabled.

Quoted 7 lines
Quoted 3 lines

I am sorry but I do not follow. First of all the implementation of the two is different and also I was asking about __PageMovable which should never be THP IIRC. Can they be compound though?

I have the same question, can they be compound? If they can be compound, PageTransHuge() can't tell from THP and compound movable page, right?

Right. I have updated the patch and use compound_nr instead.

—
Best Regards,
Yan Zi

#14 lore raw
Quoted 54 lines · Zi Yan
Quoted 51 lines · Yang Shi
Quoted 49 lines · Michal Hocko
Quoted 41 lines · Zi Yan
Quoted 30 lines · Michal Hocko

[Cc Vlastimil]

Quoted 27 lines · Zi Yan

From: Zi Yan <[email protected]>

In isolate_migratepages_block, when cc->alloc_contig is true, we are able to isolate compound pages, nr_migratepages and nr_isolated did not count compound pages correctly, causing us to isolate more pages than we thought. Use thp_nr_pages to count pages. Otherwise, we might be trapped in too_many_isolated while loop, since the actual isolated pages can go up to COMPACT_CLUSTER_MAX*512=16384, where COMPACT_CLUSTER_MAX is 32, since we stop isolation after cc->nr_migratepages reaches to COMPACT_CLUSTER_MAX.

In addition, after we fix the issue above, cc->nr_migratepages could never be equal to COMPACT_CLUSTER_MAX if compound pages are isolated, thus page isolation could not stop as we intended. Change the isolation stop condition to >=.

---
 mm/compaction.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
mm/compaction.c +0 -0
index ee1f8439369e..0683a4999581 100644
@@ -1012,8 +1012,8 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
 
Quoted 8 lines

mm/compaction.c isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, · line 1016

Quoted 6 lines
mm/compaction.c +2 -2
 isolate_success:
           list_add(&page->lru, &cc->migratepages);
-          cc->nr_migratepages++;
-          nr_isolated++;
+          cc->nr_migratepages += thp_nr_pages(page);
+          nr_isolated += thp_nr_pages(page);

Does thp_nr_pages work for __PageMovable pages?

Quoted 2 lines

Yes. It is the same as compound_nr() but compiled
to 1 when THP is not enabled.

I am sorry but I do not follow. First of all the implementation of the two is different and also I was asking about __PageMovable which should never be THP IIRC. Can they be compound though?

Quoted 5 lines
Quoted 3 lines

I have the same question, can they be compound? If they can be compound, PageTransHuge() can't tell from THP and compound movable page, right?

Right. I have updated the patch and use compound_nr instead.

Thanks. Actually I'm wondering what kind of movable page could be compound. Any real examples?

Quoted 4 lines

—
Best Regards,
Yan Zi

#15 lore raw
Quoted 55 lines · Yang Shi
Quoted 54 lines · Zi Yan
Quoted 51 lines · Yang Shi
Quoted 49 lines · Michal Hocko
Quoted 41 lines · Zi Yan
Quoted 30 lines · Michal Hocko

[Cc Vlastimil]

Quoted 27 lines · Zi Yan

From: Zi Yan <[email protected]>

In isolate_migratepages_block, when cc->alloc_contig is true, we are able to isolate compound pages, nr_migratepages and nr_isolated did not count compound pages correctly, causing us to isolate more pages than we thought. Use thp_nr_pages to count pages. Otherwise, we might be trapped in too_many_isolated while loop, since the actual isolated pages can go up to COMPACT_CLUSTER_MAX*512=16384, where COMPACT_CLUSTER_MAX is 32, since we stop isolation after cc->nr_migratepages reaches to COMPACT_CLUSTER_MAX.

In addition, after we fix the issue above, cc->nr_migratepages could never be equal to COMPACT_CLUSTER_MAX if compound pages are isolated, thus page isolation could not stop as we intended. Change the isolation stop condition to >=.

---
 mm/compaction.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
mm/compaction.c +0 -0
index ee1f8439369e..0683a4999581 100644
@@ -1012,8 +1012,8 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
 
Quoted 8 lines

mm/compaction.c isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, · line 1016

Quoted 6 lines
mm/compaction.c +2 -2
 isolate_success:
           list_add(&page->lru, &cc->migratepages);
-          cc->nr_migratepages++;
-          nr_isolated++;
+          cc->nr_migratepages += thp_nr_pages(page);
+          nr_isolated += thp_nr_pages(page);

Does thp_nr_pages work for __PageMovable pages?

Quoted 2 lines

Yes. It is the same as compound_nr() but compiled
to 1 when THP is not enabled.

I am sorry but I do not follow. First of all the implementation of the two is different and also I was asking about __PageMovable which should never be THP IIRC. Can they be compound though?

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

I have the same question, can they be compound? If they can be compound, PageTransHuge() can't tell from THP and compound movable page, right?

Right. I have updated the patch and use compound_nr instead.

Thanks. Actually I'm wondering what kind of movable page could be compound. Any real examples?

Looks like there's currently none. Compaction also wouldn't work properly with movable pages with order>0 as the free page scanner looks for order-0 pages only. But it won't hurt to use compound_nr() anyway.

Quoted 5 lines
Quoted 4 lines

—
Best Regards,
Yan Zi

#16 lore raw
Quoted 61 lines · Vlastimil Babka
Quoted 59 lines · Yang Shi
Quoted 58 lines · Zi Yan
Quoted 54 lines · Yang Shi
Quoted 43 lines · Michal Hocko
Quoted 41 lines · Zi Yan
Quoted 38 lines · Michal Hocko

[Cc Vlastimil]

Quoted 26 lines · Zi Yan

From: Zi Yan <[email protected]>

In isolate_migratepages_block, when cc->alloc_contig is true, we are able to isolate compound pages, nr_migratepages and nr_isolated did not count compound pages correctly, causing us to isolate more pages than we thought. Use thp_nr_pages to count pages. Otherwise, we might be trapped in too_many_isolated while loop, since the actual isolated pages can go up to COMPACT_CLUSTER_MAX*512=16384, where COMPACT_CLUSTER_MAX is 32, since we stop isolation after cc->nr_migratepages reaches to COMPACT_CLUSTER_MAX.

In addition, after we fix the issue above, cc->nr_migratepages could never be equal to COMPACT_CLUSTER_MAX if compound pages are isolated, thus page isolation could not stop as we intended. Change the isolation stop condition to >=.

---
 mm/compaction.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
mm/compaction.c +0 -0
index ee1f8439369e..0683a4999581 100644

mm/compaction.c isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, · line 1016

Quoted 7 lines
mm/compaction.c +2 -2
@@ -1012,8 +1012,8 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn,
 
 isolate_success:
           list_add(&page->lru, &cc->migratepages);
-          cc->nr_migratepages++;
-          nr_isolated++;
+          cc->nr_migratepages += thp_nr_pages(page);
+          nr_isolated += thp_nr_pages(page);

Does thp_nr_pages work for __PageMovable pages?

Quoted 6 lines
Quoted 2 lines

Yes. It is the same as compound_nr() but compiled
to 1 when THP is not enabled.

I am sorry but I do not follow. First of all the implementation of the two is different and also I was asking about __PageMovable which should never be THP IIRC. Can they be compound though?

I have the same question, can they be compound? If they can be compound, PageTransHuge() can't tell from THP and compound movable page, right?

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Right. I have updated the patch and use compound_nr instead.

Thanks. Actually I'm wondering what kind of movable page could be compound. Any real examples?

Looks like there's currently none. Compaction also wouldn't work properly with movable pages with order>0 as the free page scanner looks for order-0 pages only. But it won't hurt to use compound_nr() anyway.

Thanks, yes this is what I thought otherwise we have troubles in migration path too.

Quoted 7 lines
Quoted 6 lines
Quoted 5 lines

—
Best Regards,
Yan Zi