Skip to contentSkip to conversation

mm/migrate_device.c: Fix a misleading and out-dated comment

PATCH Superseded Alistair Popple 3 participants 8 messages Aug 25, 2022 – Aug 29, 2022

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Alistair Popple

author reviewer
#0 lore raw

PATCH 1/1 mm/migrate_device.c: Fix a misleading and out-dated comment

Commit ab09243aa95a ("mm/migrate.c: remove MIGRATE_PFN_LOCKED") changed the way trylock_page() in migrate_vma_collect_pmd() works without updating the comment. Reword the comment to be less misleading and a better reflection of what happens.

Fixes ab09243aa95a ("mm/migrate.c: remove MIGRATE_PFN_LOCKED")
---
 mm/migrate_device.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
mm/migrate_device.c +5 -3
index 5052093d0262..0736f846de0b 100644
@@ -179,9 +179,11 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp,
 		get_page(page);
 
 		/*
-		 * Optimize for the common case where page is only mapped once
-		 * in one process. If we can lock the page, then we can safely
-		 * set up a special migration page table entry now.
+		 * If we can't lock the page we can't migrate it. If we can it's
+		 * safe to set up a migration entry now. In the common case
+		 * where the page is mapped once in a single process setting up
+		 * the migration entry now is an optimisation to avoid walking
+		 * the rmap later with try_to_migrate().
 		 */
 		if (trylock_page(page)) {
 			bool anon_exclusive;
Signature · 1 line
2.35.1
#1 lore raw
Quoted 19 lines · Alistair Popple

Commit ab09243aa95a ("mm/migrate.c: remove MIGRATE_PFN_LOCKED") changed the way trylock_page() in migrate_vma_collect_pmd() works without updating the comment. Reword the comment to be less misleading and a better reflection of what happens.

Fixes ab09243aa95a ("mm/migrate.c: remove MIGRATE_PFN_LOCKED")
---
 mm/migrate_device.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
mm/migrate_device.c +0 -0
index 5052093d0262..0736f846de0b 100644
@@ -179,9 +179,11 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp,
 		get_page(page);
 

mm/migrate_device.c static int migrate_vma_collect_pmd(pmd_t *pmdp, · line 187

Quoted 10 lines
mm/migrate_device.c +5 -3
 		/*
-		 * Optimize for the common case where page is only mapped once
-		 * in one process. If we can lock the page, then we can safely
-		 * set up a special migration page table entry now.
+		 * If we can't lock the page we can't migrate it. If we can it's
+		 * safe to set up a migration entry now. In the common case
+		 * where the page is mapped once in a single process setting up
+		 * the migration entry now is an optimisation to avoid walking
+		 * the rmap later with try_to_migrate().
 		 */

IMHO the last sentence can still be a bit confusing - here we 100% rely on the trylock() to proceed or we'll stop migration right away. IMHO that means this is not an optimization, since optimizations should always be optional but not the case here.

Meanwhile it'll be great to also mention about why trylock is needed and no further attempt to use lock_page(). The comment in prepare() previously was great but unfortunately that code clip was removed.

In short, do you think something like this might be clearer?

/*
* We rely on the trylock() to migrate the pte. If this
* fails, we'll fail the migration of this page. IOW, the
* migration is very much best-effort, just like we'll also
* bail out if we found page pinned by other users after
* page being locked.
*
* We don't use lock_page() here or even later. It's
* because if to do so we may need to take the locks for
* multiple pages one by one, and the order to take the
* page locks is unclear. For example, if two processes
* want to lock multiple pages but in different order it
* can lead to deadlock.
*/

Thanks,

Signature · 1 line
Peter Xu
author reviewer
#2 lore raw
Quoted 20 lines · Peter Xu
Quoted 19 lines · Alistair Popple

Commit ab09243aa95a ("mm/migrate.c: remove MIGRATE_PFN_LOCKED") changed the way trylock_page() in migrate_vma_collect_pmd() works without updating the comment. Reword the comment to be less misleading and a better reflection of what happens.

Fixes ab09243aa95a ("mm/migrate.c: remove MIGRATE_PFN_LOCKED")
---
 mm/migrate_device.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
mm/migrate_device.c +0 -0
index 5052093d0262..0736f846de0b 100644
@@ -179,9 +179,11 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp,
 		get_page(page);
 
Quoted 15 lines

mm/migrate_device.c static int migrate_vma_collect_pmd(pmd_t *pmdp, · line 187

Quoted 10 lines
mm/migrate_device.c +5 -3
 		/*
-		 * Optimize for the common case where page is only mapped once
-		 * in one process. If we can lock the page, then we can safely
-		 * set up a special migration page table entry now.
+		 * If we can't lock the page we can't migrate it. If we can it's
+		 * safe to set up a migration entry now. In the common case
+		 * where the page is mapped once in a single process setting up
+		 * the migration entry now is an optimisation to avoid walking
+		 * the rmap later with try_to_migrate().
 		 */

IMHO the last sentence can still be a bit confusing - here we 100% rely on the trylock() to proceed or we'll stop migration right away. IMHO that means this is not an optimization, since optimizations should always be optional but not the case here.

We have to lock the page here, we don't have to install the migration entries. Installing the migration entries here is optional and is the optimisation.

Quoted 3 lines

Meanwhile it'll be great to also mention about why trylock is needed and no further attempt to use lock_page(). The comment in prepare() previously was great but unfortunately that code clip was removed.

Will add.

Quoted 1 line

In short, do you think something like this might be clearer?

I think it's important to mention the optimisation, otherwise the temptation is to remove the installation of migration entries here and rely on try_to_migrate() to do it later. I would actually like to be able to do that because it simplifies the code in many ways but based on my testing the optimisation turns out to be very worth while.

Quoted 6 lines

/*
* We rely on the trylock() to migrate the pte. If this
* fails, we'll fail the migration of this page. IOW, the
* migration is very much best-effort, just like we'll also
* bail out if we found page pinned by other users after
* page being locked.

Honestly I think this describes what the code does rather than why and is likely to become outdated and confusing. IMHO it's quite clear from the code that the migration will fail here if we can't lock the page.

And the fact that migration can fail is already covered in the existing documentation. See for example the extensive comment on migrate_vma_setup() about how the whole flow works. Eg:

* Once migrate_vma_pages() returns the caller may inspect which pages were
* successfully migrated, and which were not. Successfully migrated pages will
* have the MIGRATE_PFN_MIGRATE flag set for their src array entry.

Quoted 10 lines

*
* We don't use lock_page() here or even later. It's
* because if to do so we may need to take the locks for
* multiple pages one by one, and the order to take the
* page locks is unclear. For example, if two processes
* want to lock multiple pages but in different order it
* can lead to deadlock.
*/

Thanks,

#3 lore raw
Quoted 39 lines · Alistair Popple
Quoted 20 lines · Peter Xu
Quoted 19 lines · Alistair Popple

Commit ab09243aa95a ("mm/migrate.c: remove MIGRATE_PFN_LOCKED") changed the way trylock_page() in migrate_vma_collect_pmd() works without updating the comment. Reword the comment to be less misleading and a better reflection of what happens.

Fixes ab09243aa95a ("mm/migrate.c: remove MIGRATE_PFN_LOCKED")
---
 mm/migrate_device.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
mm/migrate_device.c +0 -0
index 5052093d0262..0736f846de0b 100644
@@ -179,9 +179,11 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp,
 		get_page(page);
 
Quoted 15 lines

mm/migrate_device.c static int migrate_vma_collect_pmd(pmd_t *pmdp, · line 187

Quoted 10 lines
mm/migrate_device.c +5 -3
 		/*
-		 * Optimize for the common case where page is only mapped once
-		 * in one process. If we can lock the page, then we can safely
-		 * set up a special migration page table entry now.
+		 * If we can't lock the page we can't migrate it. If we can it's
+		 * safe to set up a migration entry now. In the common case
+		 * where the page is mapped once in a single process setting up
+		 * the migration entry now is an optimisation to avoid walking
+		 * the rmap later with try_to_migrate().
 		 */

IMHO the last sentence can still be a bit confusing - here we 100% rely on the trylock() to proceed or we'll stop migration right away. IMHO that means this is not an optimization, since optimizations should always be optional but not the case here.

Quoted 3 lines

We have to lock the page here, we don't have to install the migration entries. Installing the migration entries here is optional and is the optimisation.

I see what you mean now.

Quoted 22 lines
Quoted 4 lines

Meanwhile it'll be great to also mention about why trylock is needed and no further attempt to use lock_page(). The comment in prepare() previously was great but unfortunately that code clip was removed.

Will add.

Quoted 1 line

In short, do you think something like this might be clearer?

I think it's important to mention the optimisation, otherwise the temptation is to remove the installation of migration entries here and rely on try_to_migrate() to do it later. I would actually like to be able to do that because it simplifies the code in many ways but based on my testing the optimisation turns out to be very worth while.

Quoted 6 lines

/*
* We rely on the trylock() to migrate the pte. If this
* fails, we'll fail the migration of this page. IOW, the
* migration is very much best-effort, just like we'll also
* bail out if we found page pinned by other users after
* page being locked.

Quoted 3 lines

Honestly I think this describes what the code does rather than why and is likely to become outdated and confusing. IMHO it's quite clear from the code that the migration will fail here if we can't lock the page.

If you see that's what I was struggling to understand previously, so not clear at least to me. :) Since normally a function like page migration should (from the gut feeling) not rely on trylock only.

Quoted 8 lines

And the fact that migration can fail is already covered in the existing documentation. See for example the extensive comment on migrate_vma_setup() about how the whole flow works. Eg:

* Once migrate_vma_pages() returns the caller may inspect which pages were
* successfully migrated, and which were not. Successfully migrated pages will
* have the MIGRATE_PFN_MIGRATE flag set for their src array entry.

IIUC trylock failure could be detected even earlier than that. E.g. the ppc code has:

	ret = migrate_vma_setup(&mig);
	if (ret)
		return -1;

	spage = migrate_pfn_to_page(*mig.src);
	if (!spage || !(*mig.src & MIGRATE_PFN_MIGRATE))
		goto out_finalize;

So afaict it won't even reach migrate_vma_pages() by missing of the flag MIGRATE_PFN_VALID in the src mpfn.

Probably because the migrate_vma_*() API is extensive enough so it's hard to summarize its usage in a short paragraph. I'm probably asking too much..

No strong opinion on the rest as long as we can add back some explanation on why lock_page() is not used.

Thanks again for doing this.

Signature · 1 line
Peter Xu
#4 lore raw
Quoted 68 lines · Peter Xu
Quoted 39 lines · Alistair Popple
Quoted 20 lines · Peter Xu
Quoted 19 lines · Alistair Popple

Commit ab09243aa95a ("mm/migrate.c: remove MIGRATE_PFN_LOCKED") changed the way trylock_page() in migrate_vma_collect_pmd() works without updating the comment. Reword the comment to be less misleading and a better reflection of what happens.

Fixes ab09243aa95a ("mm/migrate.c: remove MIGRATE_PFN_LOCKED")
---
 mm/migrate_device.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
mm/migrate_device.c +0 -0
index 5052093d0262..0736f846de0b 100644
@@ -179,9 +179,11 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp,
 		get_page(page);
 
Quoted 15 lines

mm/migrate_device.c static int migrate_vma_collect_pmd(pmd_t *pmdp, · line 187

Quoted 10 lines
mm/migrate_device.c +5 -3
 		/*
-		 * Optimize for the common case where page is only mapped once
-		 * in one process. If we can lock the page, then we can safely
-		 * set up a special migration page table entry now.
+		 * If we can't lock the page we can't migrate it. If we can it's
+		 * safe to set up a migration entry now. In the common case
+		 * where the page is mapped once in a single process setting up
+		 * the migration entry now is an optimisation to avoid walking
+		 * the rmap later with try_to_migrate().
 		 */

IMHO the last sentence can still be a bit confusing - here we 100% rely on the trylock() to proceed or we'll stop migration right away. IMHO that means this is not an optimization, since optimizations should always be optional but not the case here.

Quoted 3 lines

We have to lock the page here, we don't have to install the migration entries. Installing the migration entries here is optional and is the optimisation.

I see what you mean now.

Quoted 22 lines
Quoted 4 lines

Meanwhile it'll be great to also mention about why trylock is needed and no further attempt to use lock_page(). The comment in prepare() previously was great but unfortunately that code clip was removed.

Will add.

Quoted 1 line

In short, do you think something like this might be clearer?

I think it's important to mention the optimisation, otherwise the temptation is to remove the installation of migration entries here and rely on try_to_migrate() to do it later. I would actually like to be able to do that because it simplifies the code in many ways but based on my testing the optimisation turns out to be very worth while.

Quoted 6 lines

/*
* We rely on the trylock() to migrate the pte. If this
* fails, we'll fail the migration of this page. IOW, the
* migration is very much best-effort, just like we'll also
* bail out if we found page pinned by other users after
* page being locked.

Quoted 7 lines
Quoted 3 lines

Honestly I think this describes what the code does rather than why and is likely to become outdated and confusing. IMHO it's quite clear from the code that the migration will fail here if we can't lock the page.

If you see that's what I was struggling to understand previously, so not clear at least to me. :) Since normally a function like page migration should (from the gut feeling) not rely on trylock only.

IIRC, ordinary page migration will also only trylock. See isolate_movable_page().

Signature · 3 lines
Thanks,

David / dhildenb
#5 lore raw
Quoted 73 lines · David Hildenbrand
Quoted 72 lines · Peter Xu
Quoted 39 lines · Alistair Popple
Quoted 20 lines · Peter Xu
Quoted 19 lines · Alistair Popple

Commit ab09243aa95a ("mm/migrate.c: remove MIGRATE_PFN_LOCKED") changed the way trylock_page() in migrate_vma_collect_pmd() works without updating the comment. Reword the comment to be less misleading and a better reflection of what happens.

Fixes ab09243aa95a ("mm/migrate.c: remove MIGRATE_PFN_LOCKED")
---
 mm/migrate_device.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
mm/migrate_device.c +0 -0
index 5052093d0262..0736f846de0b 100644
@@ -179,9 +179,11 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp,
 		get_page(page);
 
Quoted 15 lines

mm/migrate_device.c static int migrate_vma_collect_pmd(pmd_t *pmdp, · line 187

Quoted 10 lines
mm/migrate_device.c +5 -3
 		/*
-		 * Optimize for the common case where page is only mapped once
-		 * in one process. If we can lock the page, then we can safely
-		 * set up a special migration page table entry now.
+		 * If we can't lock the page we can't migrate it. If we can it's
+		 * safe to set up a migration entry now. In the common case
+		 * where the page is mapped once in a single process setting up
+		 * the migration entry now is an optimisation to avoid walking
+		 * the rmap later with try_to_migrate().
 		 */

IMHO the last sentence can still be a bit confusing - here we 100% rely on the trylock() to proceed or we'll stop migration right away. IMHO that means this is not an optimization, since optimizations should always be optional but not the case here.

Quoted 3 lines

We have to lock the page here, we don't have to install the migration entries. Installing the migration entries here is optional and is the optimisation.

I see what you mean now.

Quoted 25 lines
Quoted 4 lines

Meanwhile it'll be great to also mention about why trylock is needed and no further attempt to use lock_page(). The comment in prepare() previously was great but unfortunately that code clip was removed.

Will add.

Quoted 1 line

In short, do you think something like this might be clearer?

I think it's important to mention the optimisation, otherwise the temptation is to remove the installation of migration entries here and rely on try_to_migrate() to do it later. I would actually like to be able to do that because it simplifies the code in many ways but based on my testing the optimisation turns out to be very worth while.

Quoted 6 lines

/*
* We rely on the trylock() to migrate the pte. If this
* fails, we'll fail the migration of this page. IOW, the
* migration is very much best-effort, just like we'll also
* bail out if we found page pinned by other users after
* page being locked.

Honestly I think this describes what the code does rather than why and is likely to become outdated and confusing. IMHO it's quite clear from the code that the migration will fail here if we can't lock the page.

Quoted 6 lines
Quoted 3 lines

If you see that's what I was struggling to understand previously, so not clear at least to me. :) Since normally a function like page migration should (from the gut feeling) not rely on trylock only.

IIRC, ordinary page migration will also only trylock. See isolate_movable_page().

Fair enough.

I think it depends on what we want to do with the migration. I think not even all users of isolate_movable_page() only depend on trylock, and it can retry with lock_page later. E.g.:

- soft_offline_in_use_page
- isolate_page
- isolate_movable_page <----- trylock here
- (if isolate_page fails...) migrate_pages
- unmap_and_move <----- lock_page here after 2 unsuccessful rounds

And I also agree migration may always fail (e.g. by spurious page refcounts being taken), so feel free to ignore the "gut feeling" - that's indeed kind of subjective.

Thanks,

Signature · 1 line
Peter Xu
author reviewer
#6 lore raw

Peter Xu <[email protected]> writes:

[...]

Quoted 44 lines
Quoted 25 lines
Quoted 25 lines
Quoted 24 lines
Quoted 3 lines

Meanwhile it'll be great to also mention about why trylock is needed and no further attempt to use lock_page(). The comment in prepare() previously was great but unfortunately that code clip was removed.

Will add.

Quoted 1 line

In short, do you think something like this might be clearer?

I think it's important to mention the optimisation, otherwise the temptation is to remove the installation of migration entries here and rely on try_to_migrate() to do it later. I would actually like to be able to do that because it simplifies the code in many ways but based on my testing the optimisation turns out to be very worth while.

Quoted 6 lines

/*
* We rely on the trylock() to migrate the pte. If this
* fails, we'll fail the migration of this page. IOW, the
* migration is very much best-effort, just like we'll also
* bail out if we found page pinned by other users after
* page being locked.

Honestly I think this describes what the code does rather than why and is likely to become outdated and confusing. IMHO it's quite clear from the code that the migration will fail here if we can't lock the page.

Quoted 6 lines
Quoted 3 lines

If you see that's what I was struggling to understand previously, so not clear at least to me. :) Since normally a function like page migration should (from the gut feeling) not rely on trylock only.

IIRC, ordinary page migration will also only trylock. See isolate_movable_page().

Fair enough.

I think it depends on what we want to do with the migration. I think not even all users of isolate_movable_page() only depend on trylock, and it can retry with lock_page later. E.g.:

- soft_offline_in_use_page
- isolate_page
- isolate_movable_page <----- trylock here
- (if isolate_page fails...) migrate_pages
- unmap_and_move <----- lock_page here after 2 unsuccessful rounds

Quoted 3 lines

And I also agree migration may always fail (e.g. by spurious page refcounts being taken), so feel free to ignore the "gut feeling" - that's indeed kind of subjective.

So how about the following:

/*
* We rely on trylock_page() to avoid deadlock between
* concurrent migrations where each is waiting on the others
* page lock. If we can't immediately lock the page we fail this
* migration as it is only best effort anyway.
*
* If we can lock the page it's safe to set up a migration entry
* now. In the common case where the page is mapped once in a
* single process setting up the migration entry now is an
* optimisation to avoid walking the rmap later with
* try_to_migrate().
*/

Thanks.

- Alistair

#7 lore raw
Quoted 14 lines · Alistair Popple

So how about the following:

/*
* We rely on trylock_page() to avoid deadlock between
* concurrent migrations where each is waiting on the others
* page lock. If we can't immediately lock the page we fail this
* migration as it is only best effort anyway.
*
* If we can lock the page it's safe to set up a migration entry
* now. In the common case where the page is mapped once in a
* single process setting up the migration entry now is an
* optimisation to avoid walking the rmap later with
* try_to_migrate().
*/

Good to me, thanks.

Signature · 1 line
Peter Xu