Skip to contentSkip to conversation

mm: migrate: handle freed page at the first place

PATCH In review Yang Shi 3 participants 4 messages Nov 14, 2019 – Nov 28, 2019

Subsystems (2)
lore
Share & tools
lore

Yang Shi

author
#0 lore raw

PATCH v2 1/1 mm: migrate: handle freed page at the first place

When doing migration if the freed page is met, we just return without migrating it since it is pointless to migrate a freed page. But, the current code allocates target page unconditionally before handling freed page, if the page is freed, the newly allocated will be just freed. It doesn't make too much sense and is just a waste of time although migrating freed page is rare.

So, handle freed page at the before that to avoid unnecessary page allocation and free.

Cc Michal Hocko <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Vlastimil Babka <[email protected]>

---
v2: * Keep thp migration support check before handling freed page per Michal Hocko
* Fixed the build warning reported by 0-day

 mm/migrate.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
mm/migrate.c +5 -9
index 4fe45d1..a8f87cb 100644
@@ -1168,15 +1168,11 @@ static ICE_noinline int unmap_and_move(new_page_t get_new_page,
 				   enum migrate_reason reason)
 {
 	int rc = MIGRATEPAGE_SUCCESS;
-	struct page *newpage;
+	struct page *newpage = NULL;
 
 	if (!thp_migration_supported() && PageTransHuge(page))
 		return -ENOMEM;
 
-	newpage = get_new_page(page, private);
-	if (!newpage)
-		return -ENOMEM;
-
 	if (page_count(page) == 1) {
 		/* page was freed from under us. So we are done. */
 		ClearPageActive(page);
@@ -1187,13 +1183,13 @@ static ICE_noinline int unmap_and_move(new_page_t get_new_page,
 				__ClearPageIsolated(page);
 			unlock_page(page);
 		}
-		if (put_new_page)
-			put_new_page(newpage, private);
-		else
-			put_page(newpage);
 		goto out;
 	}
 
+	newpage = get_new_page(page, private);
+	if (!newpage)
+		return -ENOMEM;
+
 	rc = __unmap_and_move(page, newpage, force, mode);
 	if (rc == MIGRATEPAGE_SUCCESS)
 		set_page_owner_migrate_reason(newpage, reason);
Signature · 1 line
1.8.3.1
Acked-by on v2 1/1
#1 lore raw
Quoted 7 lines · Yang Shi

When doing migration if the freed page is met, we just return without migrating it since it is pointless to migrate a freed page. But, the current code allocates target page unconditionally before handling freed page, if the page is freed, the newly allocated will be just freed. It doesn't make too much sense and is just a waste of time although migrating freed page is rare.

Quoted 7 lines

So, handle freed page at the before that to avoid unnecessary page allocation and free.

Cc Michal Hocko <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Vlastimil Babka <[email protected]>

I would be really surprised if this led to any runtime visible effect but I do agree that one less put_page path looks slightly better. For that reason

Quoted 49 lines

---
v2: * Keep thp migration support check before handling freed page per Michal Hocko
* Fixed the build warning reported by 0-day

 mm/migrate.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
mm/migrate.c +5 -9
index 4fe45d1..a8f87cb 100644
@@ -1168,15 +1168,11 @@ static ICE_noinline int unmap_and_move(new_page_t get_new_page,
 				   enum migrate_reason reason)
 {
 	int rc = MIGRATEPAGE_SUCCESS;
-	struct page *newpage;
+	struct page *newpage = NULL;
 
 	if (!thp_migration_supported() && PageTransHuge(page))
 		return -ENOMEM;
 
-	newpage = get_new_page(page, private);
-	if (!newpage)
-		return -ENOMEM;
-
 	if (page_count(page) == 1) {
 		/* page was freed from under us. So we are done. */
 		ClearPageActive(page);
@@ -1187,13 +1183,13 @@ static ICE_noinline int unmap_and_move(new_page_t get_new_page,
 				__ClearPageIsolated(page);
 			unlock_page(page);
 		}
-		if (put_new_page)
-			put_new_page(newpage, private);
-		else
-			put_page(newpage);
 		goto out;
 	}
 
+	newpage = get_new_page(page, private);
+	if (!newpage)
+		return -ENOMEM;
+
 	rc = __unmap_and_move(page, newpage, force, mode);
 	if (rc == MIGRATEPAGE_SUCCESS)
 		set_page_owner_migrate_reason(newpage, reason);
Signature · 1 line
1.8.3.1
Signature · 2 lines
Michal Hocko
SUSE Labs
author
#2 lore raw
Quoted 11 lines · Michal Hocko
Quoted 10 lines · Yang Shi

When doing migration if the freed page is met, we just return without migrating it since it is pointless to migrate a freed page. But, the current code allocates target page unconditionally before handling freed page, if the page is freed, the newly allocated will be just freed. It doesn't make too much sense and is just a waste of time although migrating freed page is rare.

So, handle freed page at the before that to avoid unnecessary page allocation and free.

Quoted 8 lines
Quoted 4 lines
Cc Michal Hocko <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Vlastimil Babka <[email protected]>

I would be really surprised if this led to any runtime visible effect but I do agree that one less put_page path looks slightly better. For that reason

Thanks!

Quoted 50 lines
Quoted 50 lines

---
v2: * Keep thp migration support check before handling freed page per Michal Hocko
* Fixed the build warning reported by 0-day

  mm/migrate.c | 14 +++++---------
  1 file changed, 5 insertions(+), 9 deletions(-)
mm/migrate.c +5 -9
index 4fe45d1..a8f87cb 100644
@@ -1168,15 +1168,11 @@ static ICE_noinline int unmap_and_move(new_page_t get_new_page,
  				   enum migrate_reason reason)
  {
  	int rc = MIGRATEPAGE_SUCCESS;
-	struct page *newpage;
+	struct page *newpage = NULL;
 
  	if (!thp_migration_supported() && PageTransHuge(page))
  		return -ENOMEM;
 
-	newpage = get_new_page(page, private);
-	if (!newpage)
-		return -ENOMEM;
-
  	if (page_count(page) == 1) {
  		/* page was freed from under us. So we are done. */
  		ClearPageActive(page);
@@ -1187,13 +1183,13 @@ static ICE_noinline int unmap_and_move(new_page_t get_new_page,
  				__ClearPageIsolated(page);
  			unlock_page(page);
  		}
-		if (put_new_page)
-			put_new_page(newpage, private);
-		else
-			put_page(newpage);
  		goto out;
  	}
 
+	newpage = get_new_page(page, private);
+	if (!newpage)
+		return -ENOMEM;
+
  	rc = __unmap_and_move(page, newpage, force, mode);
  	if (rc == MIGRATEPAGE_SUCCESS)
  		set_page_owner_migrate_reason(newpage, reason);
Signature · 1 line
1.8.3.1
#3 lore raw
Quoted 18 lines · On 2019-11-15 at 04:58 Yang Shi wrote:
Quoted 15 lines · Michal Hocko
Quoted 14 lines · Yang Shi

When doing migration if the freed page is met, we just return without migrating it since it is pointless to migrate a freed page.  But, the current code allocates target page unconditionally before handling freed page, if the page is freed, the newly allocated will be just freed.  It doesn't make too much sense and is just a waste of time although migrating freed page is rare.

So, handle freed page at the before that to avoid unnecessary page allocation and free.

Cc Michal Hocko <[email protected]>
Cc Mel Gorman <[email protected]>
Cc Vlastimil Babka <[email protected]>
Quoted 7 lines
Quoted 4 lines

I would be really surprised if this led to any runtime visible effect but I do agree that one less put_page path looks slightly better. For that reason

Thanks!

I think the original design wants to complete all preparation work before real migration starts with lock on both 'page' and 'newpage', and any race condition is caught right before real work start. (with this patch, page_count(page)==1 still possible after allocate 'newpage', but we don't check it. Actually, freeing 'page' would be evenly distributed during the whole procedure of the preparation phase.)

The __unmap_and_move() is called right after page_count() check, and where locking 'page' and 'newpage' is done immediately.

- Xinhai

Quoted 52 lines
Quoted 50 lines
Quoted 50 lines

---
v2: * Keep thp migration support check before handling freed page per Michal Hocko
      * Fixed the build warning reported by 0-day

   mm/migrate.c | 14 +++++---------

   1 file changed, 5 insertions(+), 9 deletions(-)
mm/migrate.c +1 -1
index 4fe45d1..a8f87cb 100644
@@ -1168,15 +1168,11 @@ static ICE_noinline int unmap_and_move(new_page_t get_new_page,
       enum migrate_reason reason)
    {
    int rc = MIGRATEPAGE_SUCCESS;
-	struct page *newpage;
+	struct page *newpage = NULL;


   if (!thp_migration_supported() && PageTransHuge(page)) return -ENOMEM;

mm/migrate.c +0 -4
-	newpage = get_new_page(page, private);
-	if (!newpage)
-	return -ENOMEM;
-

   if (page_count(page) == 1) {
   /* page was freed from under us. So we are done. */ ClearPageActive(page);

mm/migrate.c +0 -4
@@ -1187,13 +1183,13 @@ static ICE_noinline int unmap_and_move(new_page_t get_new_page,
    __ClearPageIsolated(page);
    unlock_page(page);
    }
-	if (put_new_page)
-	put_new_page(newpage, private);
-	else
-	put_page(newpage);
    goto out;
    }

mm/migrate.c +4 -0
+	newpage = get_new_page(page, private);
+	if (!newpage)
+	return -ENOMEM;
+

   rc = __unmap_and_move(page, newpage, force, mode); if (rc == MIGRATEPAGE_SUCCESS) set_page_owner_migrate_reason(newpage, reason); --
1.8.3.1