Skip to contentSkip to conversation

mm/gup: speed up check_and_migrate_cma_pages() on huge page

PATCH In review Pingfan Liu 3 participants 4 messages Jun 27, 2019 – Jun 28, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v5 1/1 mm/gup: speed up check_and_migrate_cma_pages() on huge page

Both hugetlb and thp locate on the same migration type of pageblock, since they are allocated from a free_list[]. Based on this fact, it is enough to check on a single subpage to decide the migration type of the whole huge page. By this way, it saves (2M/4K - 1) times loop for pmd_huge on x86, similar on other archs.

Furthermore, when executing isolate_huge_page(), it avoid taking global hugetlb_lock many times, and meanless remove/add to the local link list cma_page_list.

Cc Andrew Morton <[email protected]>
Cc Ira Weiny <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc "Kirill A. Shutemov" <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc John Hubbard <[email protected]>
Cc "Aneesh Kumar K.V" <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Keith Busch <[email protected]>
Cc Mike Kravetz <[email protected]>

---
v3 -> v4: fix C language precedence issue
v4 -> v5: drop the check PageCompound() and improve notes

 mm/gup.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)
mm/gup.c +15 -8
index ddde097..1deaad2 100644
@@ -1336,25 +1336,30 @@ static long check_and_migrate_cma_pages(struct task_struct *tsk,
 					struct vm_area_struct **vmas,
 					unsigned int gup_flags)
 {
-	long i;
+	long i, step;
 	bool drain_allow = true;
 	bool migrate_allow = true;
 	LIST_HEAD(cma_page_list);
 
 check_again:
-	for (i = 0; i < nr_pages; i++) {
+	for (i = 0; i < nr_pages;) {
+
+		struct page *head = compound_head(pages[i]);
+
+		/*
+		 * gup may start from a tail page. Advance step by the left
+		 * part.
+		 */
+		step = (1 << compound_order(head)) - (pages[i] - head);
 		/*
 		 * If we get a page from the CMA zone, since we are going to
 		 * be pinning these entries, we might as well move them out
 		 * of the CMA zone if possible.
 		 */
-		if (is_migrate_cma_page(pages[i])) {
-
-			struct page *head = compound_head(pages[i]);
-
-			if (PageHuge(head)) {
+		if (is_migrate_cma_page(head)) {
+			if (PageHuge(head))
 				isolate_huge_page(head, &cma_page_list);
-			} else {
+			else {
 				if (!PageLRU(head) && drain_allow) {
 					lru_add_drain_all();
 					drain_allow = false;
@@ -1369,6 +1374,8 @@ static long check_and_migrate_cma_pages(struct task_struct *tsk,
 				}
 			}
 		}
+
+		i += step;
 	}
 
 	if (!list_empty(&cma_page_list)) {
Signature · 1 line
2.7.5
maintainer
#1 lore raw
Quoted 10 lines · On Thu, 27 Jun 2019 13:15:45 +0800 Pingfan Liu <[email protected]> wrote:

Both hugetlb and thp locate on the same migration type of pageblock, since they are allocated from a free_list[]. Based on this fact, it is enough to check on a single subpage to decide the migration type of the whole huge page. By this way, it saves (2M/4K - 1) times loop for pmd_huge on x86, similar on other archs.

Furthermore, when executing isolate_huge_page(), it avoid taking global hugetlb_lock many times, and meanless remove/add to the local link list cma_page_list.

Thanks, looks good to me. Have any timing measurements been taken?

mm/gup.c static long check_and_migrate_cma_pages(struct task_struct *tsk, · line 1339

Quoted 10 lines

...

mm/gup.c +1 -1
@@ -1336,25 +1336,30 @@ static long check_and_migrate_cma_pages(struct task_struct *tsk,
 					struct vm_area_struct **vmas,
 					unsigned int gup_flags)
 {
-	long i;
+	long i, step;

I'll make these variables unsigned long - to match nr_pages and because we have no need for them to be negative.

Quoted 1 line

...

#2 lore raw
Quoted 9 lines · Andrew Morton
Quoted 6 lines · On Thu, 27 Jun 2019 13:15:45 +0800 Pingfan Liu <[email protected]> wrote:

Both hugetlb and thp locate on the same migration type of pageblock, since they are allocated from a free_list[]. Based on this fact, it is enough to check on a single subpage to decide the migration type of the whole huge page. By this way, it saves (2M/4K - 1) times loop for pmd_huge on x86, similar on other archs.

Quoted 6 lines
Quoted 4 lines

Furthermore, when executing isolate_huge_page(), it avoid taking global hugetlb_lock many times, and meanless remove/add to the local link list cma_page_list.

Thanks, looks good to me. Have any timing measurements been taken?

Not yet. It is a little hard to force huge page to be allocated CMA area. Should I provide the measurements?

Quoted 14 lines

mm/gup.c static long check_and_migrate_cma_pages(struct task_struct *tsk, · line 1339

Quoted 11 lines

...

mm/gup.c +1 -1
@@ -1336,25 +1336,30 @@ static long check_and_migrate_cma_pages(struct task_struct *tsk,
                                      struct vm_area_struct **vmas,
                                      unsigned int gup_flags)
 {
-     long i;
+     long i, step;

I'll make these variables unsigned long - to match nr_pages and because we have no need for them to be negative.

OK, will fix it.

Thanks,
Pingfan

Quoted 2 lines
Quoted 2 lines

...

Reviewed-by on v5 1/1
#3 lore raw
In reply to Pingfan Liu
Quoted 11 lines · Pingfan Liu

Both hugetlb and thp locate on the same migration type of pageblock, since they are allocated from a free_list[]. Based on this fact, it is enough to check on a single subpage to decide the migration type of the whole huge page. By this way, it saves (2M/4K - 1) times loop for pmd_huge on x86, similar on other archs.

Furthermore, when executing isolate_huge_page(), it avoid taking global hugetlb_lock many times, and meanless remove/add to the local link list cma_page_list.

Quoted 72 lines
Cc Andrew Morton <[email protected]>
Cc Ira Weiny <[email protected]>
Cc Mike Rapoport <[email protected]>
Cc "Kirill A. Shutemov" <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc John Hubbard <[email protected]>
Cc "Aneesh Kumar K.V" <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Keith Busch <[email protected]>
Cc Mike Kravetz <[email protected]>

---
v3 -> v4: fix C language precedence issue
v4 -> v5: drop the check PageCompound() and improve notes

 mm/gup.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)
mm/gup.c +15 -8
index ddde097..1deaad2 100644
@@ -1336,25 +1336,30 @@ static long check_and_migrate_cma_pages(struct task_struct *tsk,
 					struct vm_area_struct **vmas,
 					unsigned int gup_flags)
 {
-	long i;
+	long i, step;
 	bool drain_allow = true;
 	bool migrate_allow = true;
 	LIST_HEAD(cma_page_list);
 
 check_again:
-	for (i = 0; i < nr_pages; i++) {
+	for (i = 0; i < nr_pages;) {
+
+		struct page *head = compound_head(pages[i]);
+
+		/*
+		 * gup may start from a tail page. Advance step by the left
+		 * part.
+		 */
+		step = (1 << compound_order(head)) - (pages[i] - head);
 		/*
 		 * If we get a page from the CMA zone, since we are going to
 		 * be pinning these entries, we might as well move them out
 		 * of the CMA zone if possible.
 		 */
-		if (is_migrate_cma_page(pages[i])) {
-
-			struct page *head = compound_head(pages[i]);
-
-			if (PageHuge(head)) {
+		if (is_migrate_cma_page(head)) {
+			if (PageHuge(head))
 				isolate_huge_page(head, &cma_page_list);
-			} else {
+			else {
 				if (!PageLRU(head) && drain_allow) {
 					lru_add_drain_all();
 					drain_allow = false;
@@ -1369,6 +1374,8 @@ static long check_and_migrate_cma_pages(struct task_struct *tsk,
 				}
 			}
 		}
+
+		i += step;
 	}
 
 	if (!list_empty(&cma_page_list)) {
Signature · 1 line
2.7.5