Skip to contentSkip to conversation

Revert "mm/migrate: __unmap_and_move() push good newpage to LRU"

RFC PATCH In review Yosry Ahmed 3 participants 8 messages Jun 18, 2023 – Jun 20, 2023

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

RFC PATCH 5/5 Revert "mm/migrate: __unmap_and_move() push good newpage to LRU"

This reverts commit c3096e6782b733158bf34f6bbb4567808d4e0740.

That commit made sure we immediately add the new page to the LRU before remove_migration_ptes() is called in migrate_move_folio() (used to be __unmap_and_move() back then), such that the rmap walk will rebuild the correct mlock_count for the page again. This was needed because the mlock_count was lost when the page is isolated. This is no longer the case since mlock_count no longer overlays page->lru.

Revert the commit (the code was foliated afterward the commit, so the revert is updated as such).

---
 mm/migrate.c | 24 +++++++++---------------
 1 file changed, 9 insertions(+), 15 deletions(-)
mm/migrate.c +9 -15
index 01cac26a3127..68f693731865 100644
@@ -1279,19 +1279,6 @@ static int migrate_folio_move(free_page_t put_new_page, unsigned long private,
 	if (unlikely(!is_lru))
 		goto out_unlock_both;
 
-	/*
-	 * When successful, push dst to LRU immediately: so that if it
-	 * turns out to be an mlocked page, remove_migration_ptes() will
-	 * automatically build up the correct dst->mlock_count for it.
-	 *
-	 * We would like to do something similar for the old page, when
-	 * unsuccessful, and other cases when a page has been temporarily
-	 * isolated from the unevictable LRU: but this case is the easiest.
-	 */
-	folio_add_lru(dst);
-	if (page_was_mapped)
-		lru_add_drain();
-
 	if (page_was_mapped)
 		remove_migration_ptes(src, dst, false);
 
@@ -1301,9 +1288,16 @@ static int migrate_folio_move(free_page_t put_new_page, unsigned long private,
 	/*
 	 * If migration is successful, decrease refcount of dst,
 	 * which will not free the page because new page owner increased
-	 * refcounter.
+	 * refcounter. As well, if it is LRU folio, add the folio to LRU
+	 * list in here. Use the old state of the isolated source folio to
+	 * determine if we migrated a LRU folio. dst was already unlocked
+	 * and possibly modified by its owner - don't rely on the folio
+	 * state.
 	 */
-	folio_put(dst);
+	if (unlikely(!is_lru))
+		folio_put(dst);
+	else
+		folio_putback_lru(dst);
 
 	/*
 	 * A folio that has been migrated has all references removed
Signature · 1 line
2.41.0.162.gfafddb0af9-goog
Reviewed-by on v1 5/5
#1 lore raw

Hi, Yosry,

Quoted 22 lines · Yosry Ahmed

This reverts commit c3096e6782b733158bf34f6bbb4567808d4e0740.

That commit made sure we immediately add the new page to the LRU before remove_migration_ptes() is called in migrate_move_folio() (used to be __unmap_and_move() back then), such that the rmap walk will rebuild the correct mlock_count for the page again. This was needed because the mlock_count was lost when the page is isolated. This is no longer the case since mlock_count no longer overlays page->lru.

Revert the commit (the code was foliated afterward the commit, so the revert is updated as such).

---
 mm/migrate.c | 24 +++++++++---------------
 1 file changed, 9 insertions(+), 15 deletions(-)
mm/migrate.c +0 -0
index 01cac26a3127..68f693731865 100644

mm/migrate.c static int migrate_folio_move(free_page_t put_new_page, unsigned long private, · line 1280

Quoted 2 lines
mm/migrate.c +0 -0
@@ -1279,19 +1279,6 @@ static int migrate_folio_move(free_page_t put_new_page, unsigned long private,
 	if (unlikely(!is_lru))
 		goto out_unlock_both;

The patch itself looks good to me! Thanks!

And, it seems that we can remove the above 2 lines and "out_unlock_both" label now. That can make the code simpler a little. Right?

Best Regards,
Huang, Ying

Quoted 35 lines
mm/migrate.c +9 -15
-	/*
-	 * When successful, push dst to LRU immediately: so that if it
-	 * turns out to be an mlocked page, remove_migration_ptes() will
-	 * automatically build up the correct dst->mlock_count for it.
-	 *
-	 * We would like to do something similar for the old page, when
-	 * unsuccessful, and other cases when a page has been temporarily
-	 * isolated from the unevictable LRU: but this case is the easiest.
-	 */
-	folio_add_lru(dst);
-	if (page_was_mapped)
-		lru_add_drain();
-
 	if (page_was_mapped)
 		remove_migration_ptes(src, dst, false);
 
@@ -1301,9 +1288,16 @@ static int migrate_folio_move(free_page_t put_new_page, unsigned long private,
 	/*
 	 * If migration is successful, decrease refcount of dst,
 	 * which will not free the page because new page owner increased
-	 * refcounter.
+	 * refcounter. As well, if it is LRU folio, add the folio to LRU
+	 * list in here. Use the old state of the isolated source folio to
+	 * determine if we migrated a LRU folio. dst was already unlocked
+	 * and possibly modified by its owner - don't rely on the folio
+	 * state.
 	 */
-	folio_put(dst);
+	if (unlikely(!is_lru))
+		folio_put(dst);
+	else
+		folio_putback_lru(dst);
 
 	/*
 	 * A folio that has been migrated has all references removed
#2 lore raw
Quoted 32 lines · Ying

Hi, Yosry,

Quoted 22 lines · Yosry Ahmed

This reverts commit c3096e6782b733158bf34f6bbb4567808d4e0740.

That commit made sure we immediately add the new page to the LRU before remove_migration_ptes() is called in migrate_move_folio() (used to be __unmap_and_move() back then), such that the rmap walk will rebuild the correct mlock_count for the page again. This was needed because the mlock_count was lost when the page is isolated. This is no longer the case since mlock_count no longer overlays page->lru.

Revert the commit (the code was foliated afterward the commit, so the revert is updated as such).

---
 mm/migrate.c | 24 +++++++++---------------
 1 file changed, 9 insertions(+), 15 deletions(-)
mm/migrate.c +0 -0
index 01cac26a3127..68f693731865 100644

mm/migrate.c static int migrate_folio_move(free_page_t put_new_page, unsigned long private, · line 1280

Quoted 2 lines
mm/migrate.c +0 -0
@@ -1279,19 +1279,6 @@ static int migrate_folio_move(free_page_t put_new_page, unsigned long private,
      if (unlikely(!is_lru))
              goto out_unlock_both;

The patch itself looks good to me! Thanks!

Quoted 1 line

Thanks for taking a look!

Quoted 3 lines

And, it seems that we can remove the above 2 lines and "out_unlock_both" label now. That can make the code simpler a little. Right?

I am not familiar with this code. If we remove the above condition then pages that have is_lru == 0 (i.e __PageMovable(src) is true) and page_was_mapped == 1 will call remove_migration_ptes(). This wouldn't happen without removing the above 2 lines. If this combination is impossible (is_lru == 0 && page_was_mapped == 1), then yeah we can remove the above condition.

It looks like __SetPageMovable() is only called by zsmalloc, z3fold, and balloon_page_insert(). The former 2 will never have those pages mapped into userspace. I am not familiar with balloon_page_insert(), but my gut feeling is that those are pages used by the driver and are also not mapped into userspace.

So I guess we can just remove the condition, but a confirmation for the above would be reassuring :)

Quoted 40 lines

Best Regards,
Huang, Ying

Quoted 35 lines
mm/migrate.c +9 -15
-     /*
-      * When successful, push dst to LRU immediately: so that if it
-      * turns out to be an mlocked page, remove_migration_ptes() will
-      * automatically build up the correct dst->mlock_count for it.
-      *
-      * We would like to do something similar for the old page, when
-      * unsuccessful, and other cases when a page has been temporarily
-      * isolated from the unevictable LRU: but this case is the easiest.
-      */
-     folio_add_lru(dst);
-     if (page_was_mapped)
-             lru_add_drain();
-
      if (page_was_mapped)
              remove_migration_ptes(src, dst, false);
 
@@ -1301,9 +1288,16 @@ static int migrate_folio_move(free_page_t put_new_page, unsigned long private,
      /*
       * If migration is successful, decrease refcount of dst,
       * which will not free the page because new page owner increased
-      * refcounter.
+      * refcounter. As well, if it is LRU folio, add the folio to LRU
+      * list in here. Use the old state of the isolated source folio to
+      * determine if we migrated a LRU folio. dst was already unlocked
+      * and possibly modified by its owner - don't rely on the folio
+      * state.
       */
-     folio_put(dst);
+     if (unlikely(!is_lru))
+             folio_put(dst);
+     else
+             folio_putback_lru(dst);
 
      /*
       * A folio that has been migrated has all references removed
#3 lore raw
Quoted 48 lines · Yosry Ahmed
Quoted 32 lines · Ying

Hi, Yosry,

Quoted 22 lines · Yosry Ahmed

This reverts commit c3096e6782b733158bf34f6bbb4567808d4e0740.

That commit made sure we immediately add the new page to the LRU before remove_migration_ptes() is called in migrate_move_folio() (used to be __unmap_and_move() back then), such that the rmap walk will rebuild the correct mlock_count for the page again. This was needed because the mlock_count was lost when the page is isolated. This is no longer the case since mlock_count no longer overlays page->lru.

Revert the commit (the code was foliated afterward the commit, so the revert is updated as such).

---
 mm/migrate.c | 24 +++++++++---------------
 1 file changed, 9 insertions(+), 15 deletions(-)
mm/migrate.c +0 -0
index 01cac26a3127..68f693731865 100644

mm/migrate.c static int migrate_folio_move(free_page_t put_new_page, unsigned long private, · line 1280

Quoted 2 lines
mm/migrate.c +0 -0
@@ -1279,19 +1279,6 @@ static int migrate_folio_move(free_page_t put_new_page, unsigned long private,
      if (unlikely(!is_lru))
              goto out_unlock_both;

The patch itself looks good to me! Thanks!

Quoted 1 line

Thanks for taking a look!

Quoted 3 lines

And, it seems that we can remove the above 2 lines and "out_unlock_both" label now. That can make the code simpler a little. Right?

I am not familiar with this code. If we remove the above condition then pages that have is_lru == 0 (i.e __PageMovable(src) is true) and page_was_mapped == 1 will call remove_migration_ptes(). This wouldn't happen without removing the above 2 lines. If this combination is impossible (is_lru == 0 && page_was_mapped == 1), then yeah we can remove the above condition.

Quoted 5 lines

It looks like __SetPageMovable() is only called by zsmalloc, z3fold, and balloon_page_insert(). The former 2 will never have those pages mapped into userspace. I am not familiar with balloon_page_insert(), but my gut feeling is that those are pages used by the driver and are also not mapped into userspace.

You can take a look at migrate_folio_unmap(), where "page_was_mapped" will not be set to 1 if !is_lru.

Best Regards,
Huang, Ying

Quoted 43 lines

So I guess we can just remove the condition, but a confirmation for the above would be reassuring :)

Quoted 40 lines

Best Regards,
Huang, Ying

Quoted 35 lines
mm/migrate.c +9 -15
-     /*
-      * When successful, push dst to LRU immediately: so that if it
-      * turns out to be an mlocked page, remove_migration_ptes() will
-      * automatically build up the correct dst->mlock_count for it.
-      *
-      * We would like to do something similar for the old page, when
-      * unsuccessful, and other cases when a page has been temporarily
-      * isolated from the unevictable LRU: but this case is the easiest.
-      */
-     folio_add_lru(dst);
-     if (page_was_mapped)
-             lru_add_drain();
-
      if (page_was_mapped)
              remove_migration_ptes(src, dst, false);
 
@@ -1301,9 +1288,16 @@ static int migrate_folio_move(free_page_t put_new_page, unsigned long private,
      /*
       * If migration is successful, decrease refcount of dst,
       * which will not free the page because new page owner increased
-      * refcounter.
+      * refcounter. As well, if it is LRU folio, add the folio to LRU
+      * list in here. Use the old state of the isolated source folio to
+      * determine if we migrated a LRU folio. dst was already unlocked
+      * and possibly modified by its owner - don't rely on the folio
+      * state.
       */
-     folio_put(dst);
+     if (unlikely(!is_lru))
+             folio_put(dst);
+     else
+             folio_putback_lru(dst);
 
      /*
       * A folio that has been migrated has all references removed
#4 lore raw
Quoted 51 lines · Ying
Quoted 48 lines · Yosry Ahmed
Quoted 32 lines · Ying

Hi, Yosry,

Quoted 22 lines · Yosry Ahmed

This reverts commit c3096e6782b733158bf34f6bbb4567808d4e0740.

That commit made sure we immediately add the new page to the LRU before remove_migration_ptes() is called in migrate_move_folio() (used to be __unmap_and_move() back then), such that the rmap walk will rebuild the correct mlock_count for the page again. This was needed because the mlock_count was lost when the page is isolated. This is no longer the case since mlock_count no longer overlays page->lru.

Revert the commit (the code was foliated afterward the commit, so the revert is updated as such).

---
 mm/migrate.c | 24 +++++++++---------------
 1 file changed, 9 insertions(+), 15 deletions(-)
mm/migrate.c +0 -0
index 01cac26a3127..68f693731865 100644

mm/migrate.c static int migrate_folio_move(free_page_t put_new_page, unsigned long private, · line 1280

Quoted 2 lines
mm/migrate.c +0 -0
@@ -1279,19 +1279,6 @@ static int migrate_folio_move(free_page_t put_new_page, unsigned long private,
      if (unlikely(!is_lru))
              goto out_unlock_both;

The patch itself looks good to me! Thanks!

Quoted 1 line

Thanks for taking a look!

Quoted 3 lines

And, it seems that we can remove the above 2 lines and "out_unlock_both" label now. That can make the code simpler a little. Right?

I am not familiar with this code. If we remove the above condition then pages that have is_lru == 0 (i.e __PageMovable(src) is true) and page_was_mapped == 1 will call remove_migration_ptes(). This wouldn't happen without removing the above 2 lines. If this combination is impossible (is_lru == 0 && page_was_mapped == 1), then yeah we can remove the above condition.

Quoted 8 lines
Quoted 5 lines

It looks like __SetPageMovable() is only called by zsmalloc, z3fold, and balloon_page_insert(). The former 2 will never have those pages mapped into userspace. I am not familiar with balloon_page_insert(), but my gut feeling is that those are pages used by the driver and are also not mapped into userspace.

You can take a look at migrate_folio_unmap(), where "page_was_mapped" will not be set to 1 if !is_lru.

Oh I was looking in the wrong place huh, thanks. Will remove it when I respin!

Quoted 48 lines

Best Regards,
Huang, Ying

Quoted 43 lines

So I guess we can just remove the condition, but a confirmation for the above would be reassuring :)

Quoted 40 lines

Best Regards,
Huang, Ying

Quoted 35 lines
mm/migrate.c +9 -15
-     /*
-      * When successful, push dst to LRU immediately: so that if it
-      * turns out to be an mlocked page, remove_migration_ptes() will
-      * automatically build up the correct dst->mlock_count for it.
-      *
-      * We would like to do something similar for the old page, when
-      * unsuccessful, and other cases when a page has been temporarily
-      * isolated from the unevictable LRU: but this case is the easiest.
-      */
-     folio_add_lru(dst);
-     if (page_was_mapped)
-             lru_add_drain();
-
      if (page_was_mapped)
              remove_migration_ptes(src, dst, false);
 
@@ -1301,9 +1288,16 @@ static int migrate_folio_move(free_page_t put_new_page, unsigned long private,
      /*
       * If migration is successful, decrease refcount of dst,
       * which will not free the page because new page owner increased
-      * refcounter.
+      * refcounter. As well, if it is LRU folio, add the folio to LRU
+      * list in here. Use the old state of the isolated source folio to
+      * determine if we migrated a LRU folio. dst was already unlocked
+      * and possibly modified by its owner - don't rely on the folio
+      * state.
       */
-     folio_put(dst);
+     if (unlikely(!is_lru))
+             folio_put(dst);
+     else
+             folio_putback_lru(dst);
 
      /*
       * A folio that has been migrated has all references removed
#5 lore raw
In reply to Yosry Ahmed
Quoted 48 lines · Yosry Ahmed
Quoted 32 lines · Ying

Hi, Yosry,

Quoted 22 lines · Yosry Ahmed

This reverts commit c3096e6782b733158bf34f6bbb4567808d4e0740.

That commit made sure we immediately add the new page to the LRU before remove_migration_ptes() is called in migrate_move_folio() (used to be __unmap_and_move() back then), such that the rmap walk will rebuild the correct mlock_count for the page again. This was needed because the mlock_count was lost when the page is isolated. This is no longer the case since mlock_count no longer overlays page->lru.

Revert the commit (the code was foliated afterward the commit, so the revert is updated as such).

---
  mm/migrate.c | 24 +++++++++---------------
  1 file changed, 9 insertions(+), 15 deletions(-)
mm/migrate.c +0 -0
index 01cac26a3127..68f693731865 100644

mm/migrate.c static int migrate_folio_move(free_page_t put_new_page, unsigned long private, · line 1280

Quoted 2 lines
mm/migrate.c +0 -0
@@ -1279,19 +1279,6 @@ static int migrate_folio_move(free_page_t put_new_page, unsigned long private,
       if (unlikely(!is_lru))
               goto out_unlock_both;

The patch itself looks good to me! Thanks!

Quoted 1 line

Thanks for taking a look!

Quoted 3 lines

And, it seems that we can remove the above 2 lines and "out_unlock_both" label now. That can make the code simpler a little. Right?

I am not familiar with this code. If we remove the above condition then pages that have is_lru == 0 (i.e __PageMovable(src) is true) and page_was_mapped == 1 will call remove_migration_ptes(). This wouldn't happen without removing the above 2 lines. If this combination is impossible (is_lru == 0 && page_was_mapped == 1), then yeah we can remove the above condition.

Quoted 5 lines

It looks like __SetPageMovable() is only called by zsmalloc, z3fold, and balloon_page_insert(). The former 2 will never have those pages mapped into userspace. I am not familiar with balloon_page_insert(), but my gut feeling is that those are pages used by the driver and are also not mapped into userspace.

On XEN, there is xen_alloc_ballooned_pages(), which ends up mapping balloon-inflated pages into user space (for something like MMIO IIRC). But the XEN balloon does not use the balloon compaction framework, so __SetPageMovable() does not apply.

The other balloon_page_insert() users (VMware balloon, CMM, virtio-balloon) shouldn't be doing something like that.

Signature · 3 lines
Cheers,

David / dhildenb
#6 lore raw
Quoted 55 lines · David Hildenbrand
Quoted 48 lines · Yosry Ahmed
Quoted 32 lines · Ying

Hi, Yosry,

Quoted 22 lines · Yosry Ahmed

This reverts commit c3096e6782b733158bf34f6bbb4567808d4e0740.

That commit made sure we immediately add the new page to the LRU before remove_migration_ptes() is called in migrate_move_folio() (used to be __unmap_and_move() back then), such that the rmap walk will rebuild the correct mlock_count for the page again. This was needed because the mlock_count was lost when the page is isolated. This is no longer the case since mlock_count no longer overlays page->lru.

Revert the commit (the code was foliated afterward the commit, so the revert is updated as such).

---
   mm/migrate.c | 24 +++++++++---------------
   1 file changed, 9 insertions(+), 15 deletions(-)
mm/migrate.c +0 -0
index 01cac26a3127..68f693731865 100644

mm/migrate.c static int migrate_folio_move(free_page_t put_new_page, unsigned long private, · line 1280

Quoted 2 lines
mm/migrate.c +0 -0
@@ -1279,19 +1279,6 @@ static int migrate_folio_move(free_page_t put_new_page, unsigned long private,
        if (unlikely(!is_lru))
                goto out_unlock_both;

The patch itself looks good to me! Thanks!

Quoted 1 line

Thanks for taking a look!

Quoted 3 lines

And, it seems that we can remove the above 2 lines and "out_unlock_both" label now. That can make the code simpler a little. Right?

I am not familiar with this code. If we remove the above condition then pages that have is_lru == 0 (i.e __PageMovable(src) is true) and page_was_mapped == 1 will call remove_migration_ptes(). This wouldn't happen without removing the above 2 lines. If this combination is impossible (is_lru == 0 && page_was_mapped == 1), then yeah we can remove the above condition.

Quoted 5 lines

It looks like __SetPageMovable() is only called by zsmalloc, z3fold, and balloon_page_insert(). The former 2 will never have those pages mapped into userspace. I am not familiar with balloon_page_insert(), but my gut feeling is that those are pages used by the driver and are also not mapped into userspace.

Quoted 7 lines

On XEN, there is xen_alloc_ballooned_pages(), which ends up mapping balloon-inflated pages into user space (for something like MMIO IIRC). But the XEN balloon does not use the balloon compaction framework, so __SetPageMovable() does not apply.

The other balloon_page_insert() users (VMware balloon, CMM, virtio-balloon) shouldn't be doing something like that.

Ah, and I remember they even can't, because in balloon_page_insert() we also do a __SetPageOffline(). And such typed pages cannot be mapped into user space (because the type overlays the mapcount).

Signature · 3 lines
Cheers,

David / dhildenb
#7 lore raw
Quoted 62 lines · David Hildenbrand
Quoted 60 lines · David Hildenbrand
Quoted 48 lines · Yosry Ahmed
Quoted 32 lines · Ying

Hi, Yosry,

Quoted 22 lines · Yosry Ahmed

This reverts commit c3096e6782b733158bf34f6bbb4567808d4e0740.

That commit made sure we immediately add the new page to the LRU before remove_migration_ptes() is called in migrate_move_folio() (used to be __unmap_and_move() back then), such that the rmap walk will rebuild the correct mlock_count for the page again. This was needed because the mlock_count was lost when the page is isolated. This is no longer the case since mlock_count no longer overlays page->lru.

Revert the commit (the code was foliated afterward the commit, so the revert is updated as such).

---
   mm/migrate.c | 24 +++++++++---------------
   1 file changed, 9 insertions(+), 15 deletions(-)
mm/migrate.c +0 -0
index 01cac26a3127..68f693731865 100644

mm/migrate.c static int migrate_folio_move(free_page_t put_new_page, unsigned long private, · line 1280

Quoted 2 lines
mm/migrate.c +0 -0
@@ -1279,19 +1279,6 @@ static int migrate_folio_move(free_page_t put_new_page, unsigned long private,
        if (unlikely(!is_lru))
                goto out_unlock_both;

The patch itself looks good to me! Thanks!

Quoted 1 line

Thanks for taking a look!

Quoted 3 lines

And, it seems that we can remove the above 2 lines and "out_unlock_both" label now. That can make the code simpler a little. Right?

I am not familiar with this code. If we remove the above condition then pages that have is_lru == 0 (i.e __PageMovable(src) is true) and page_was_mapped == 1 will call remove_migration_ptes(). This wouldn't happen without removing the above 2 lines. If this combination is impossible (is_lru == 0 && page_was_mapped == 1), then yeah we can remove the above condition.

Quoted 5 lines

It looks like __SetPageMovable() is only called by zsmalloc, z3fold, and balloon_page_insert(). The former 2 will never have those pages mapped into userspace. I am not familiar with balloon_page_insert(), but my gut feeling is that those are pages used by the driver and are also not mapped into userspace.

On XEN, there is xen_alloc_ballooned_pages(), which ends up mapping balloon-inflated pages into user space (for something like MMIO IIRC). But the XEN balloon does not use the balloon compaction framework, so __SetPageMovable() does not apply.

Quoted 6 lines
Quoted 2 lines

The other balloon_page_insert() users (VMware balloon, CMM, virtio-balloon) shouldn't be doing something like that.

Ah, and I remember they even can't, because in balloon_page_insert() we also do a __SetPageOffline(). And such typed pages cannot be mapped into user space (because the type overlays the mapcount).

Thanks David, good to know! I will remove the condition as Ying suggested in the next version then!

Quoted 6 lines

--
Cheers,

David / dhildenb