Skip to contentSkip to conversation

mm/migrate_device: Do not access pgmap for non zone device pages

PATCH Superseded Balbir Singh 3 participants 7 messages Feb 19, 2025 – Feb 21, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/migrate_device: Do not access pgmap for non zone device pages

page_pgmap() is referenced before checking if the page is a zone device page and this triggers the warning in page_pgmap(). Refactor the code to use the helper function after relevant checks.

Cc Alistair Popple <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Dan Williams <[email protected]>

---

Fixes 7f1cfd71153b ("mm: allow compound zone device pages") on

mm-unstable

 mm/migrate_device.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
mm/migrate_device.c +8 -5
index 6771893d4601..e0bf771edb6f 100644
@@ -153,14 +153,17 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp,
 				goto next;
 			}
 			page = vm_normal_page(migrate->vma, addr, pte);
-			pgmap = page_pgmap(page);
 			if (page && !is_zone_device_page(page) &&
 			    !(migrate->flags & MIGRATE_VMA_SELECT_SYSTEM))
 				goto next;
-			else if (page && is_device_coherent_page(page) &&
-			    (!(migrate->flags & MIGRATE_VMA_SELECT_DEVICE_COHERENT) ||
-			     pgmap->owner != migrate->pgmap_owner))
-				goto next;
+			else if (page && is_device_coherent_page(page)) {
+				pgmap = page_pgmap(page);
+
+				if (!(migrate->flags &
+					MIGRATE_VMA_SELECT_DEVICE_COHERENT) ||
+					pgmap->owner != migrate->pgmap_owner)
+					goto next;
+			}
 			mpfn = migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE;
 			mpfn |= pte_write(pte) ? MIGRATE_PFN_WRITE : 0;
 		}
Signature · 1 line
2.48.1
#1 lore raw
Quoted 9 lines · Balbir Singh

page_pgmap() is referenced before checking if the page is a zone device page and this triggers the warning in page_pgmap(). Refactor the code to use the helper function after relevant checks.

Cc Alistair Popple <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Dan Williams <[email protected]>
Quoted 5 lines

---

Fixes 7f1cfd71153b ("mm: allow compound zone device pages") on

mm-unstable

Is there actually something broken? At least for now, reading folio->pgmap should just work, although it might be garbage.

Quoted 18 lines
  mm/migrate_device.c | 13 ++++++++-----
  1 file changed, 8 insertions(+), 5 deletions(-)
mm/migrate_device.c +0 -3
index 6771893d4601..e0bf771edb6f 100644
@@ -153,14 +153,17 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp,
  				goto next;
  			}
  			page = vm_normal_page(migrate->vma, addr, pte);
-			pgmap = page_pgmap(page);
  			if (page && !is_zone_device_page(page) &&
  			    !(migrate->flags & MIGRATE_VMA_SELECT_SYSTEM))
  				goto next;
-			else if (page && is_device_coherent_page(page) &&
-			    (!(migrate->flags & MIGRATE_VMA_SELECT_DEVICE_COHERENT) ||

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

Quoted 10 lines
mm/migrate_device.c +8 -2
-			     pgmap->owner != migrate->pgmap_owner))
-				goto next;
+			else if (page && is_device_coherent_page(page)) {
+				pgmap = page_pgmap(page);
+
+				if (!(migrate->flags &
+					MIGRATE_VMA_SELECT_DEVICE_COHERENT) ||
+					pgmap->owner != migrate->pgmap_owner)
+					goto next;
+			}

Coding style wants you to use

if () {

} else if {

}

Not

if ()
else if {

}

Something simpler might be

page_pgmap(page)->owner != migrate->pgmap_owner

Quoted 3 lines
mm/migrate_device.c +0 -0
  			mpfn = migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE;
  			mpfn |= pte_write(pte) ? MIGRATE_PFN_WRITE : 0;
  		}
Signature · 3 lines
Cheers,

David / dhildenb
#2 lore raw
Quoted 10 lines · David Hildenbrand
Quoted 9 lines · Balbir Singh

page_pgmap() is referenced before checking if the page is a zone device page and this triggers the warning in page_pgmap(). Refactor the code to use the helper function after relevant checks.

Cc Alistair Popple <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Dan Williams <[email protected]>
Quoted 8 lines
Quoted 5 lines

---

Fixes 7f1cfd71153b ("mm: allow compound zone device pages") on

mm-unstable

Is there actually something broken? At least for now, reading folio->pgmap should just work, although it might be garbage.

It triggers the VM_WARN_ON_ONCE_PAGE

static inline struct dev_pagemap *page_pgmap(const struct page *page)

{
	VM_WARN_ON_ONCE_PAGE(!is_zone_device_page(page), page);
	return page_folio(page)->pgmap;
}

Nothing is broken, because the code below has checks for is_device_coherent_page(), but in general I think the WARN_ON is correct because it warns us against garbage and it's propagation if the correct checks are not in place.

Quoted 35 lines
Quoted 20 lines

  mm/migrate_device.c | 13 ++++++++-----

  1 file changed, 8 insertions(+), 5 deletions(-)
mm/migrate_device.c +0 -1
index 6771893d4601..e0bf771edb6f 100644
@@ -153,14 +153,17 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp,
                   goto next;
               }
               page = vm_normal_page(migrate->vma, addr, pte);
-            pgmap = page_pgmap(page);

              if (page && !is_zone_device_page(page) && !(migrate->flags & MIGRATE_VMA_SELECT_SYSTEM)) goto next;
-            else if (page && is_device_coherent_page(page) && -                (!(migrate->flags & MIGRATE_VMA_SELECT_DEVICE_COHERENT) || -                 pgmap->owner != migrate->pgmap_owner))
-                goto next;

Quoted 8 lines

+            else if (page && is_device_coherent_page(page)) { +                pgmap = page_pgmap(page);
+
+                if (!(migrate->flags &
+                    MIGRATE_VMA_SELECT_DEVICE_COHERENT) || +                    pgmap->owner != migrate->pgmap_owner) +                    goto next;
+            }

Coding style wants you to use

if () {

} else if {

Quoted 9 lines

}

Not

if ()
else if {

}

Ack, checkpatch.pl missed it, but agreed

Quoted 5 lines

Something simpler might be

page_pgmap(page)->owner != migrate->pgmap_owner

Yep, I had that and dropped it, the four clauses made it feel that it might benefit from a split.

Quoted 5 lines
Quoted 3 lines

              mpfn = migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE; mpfn |= pte_write(pte) ? MIGRATE_PFN_WRITE : 0; }

Thanks for the review!
Balbir

#3 lore raw
Quoted 23 lines · Balbir Singh
Quoted 10 lines · David Hildenbrand
Quoted 9 lines · Balbir Singh

page_pgmap() is referenced before checking if the page is a zone device page and this triggers the warning in page_pgmap(). Refactor the code to use the helper function after relevant checks.

Cc Alistair Popple <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Dan Williams <[email protected]>
Quoted 8 lines
Quoted 5 lines

---

Fixes 7f1cfd71153b ("mm: allow compound zone device pages") on

mm-unstable

Is there actually something broken? At least for now, reading folio->pgmap should just work, although it might be garbage.

It triggers the VM_WARN_ON_ONCE_PAGE

static inline struct dev_pagemap *page_pgmap(const struct page *page)

Quoted 8 lines
{
	VM_WARN_ON_ONCE_PAGE(!is_zone_device_page(page), page);
	return page_folio(page)->pgmap;
}

Nothing is broken, because the code below has checks for is_device_coherent_page(), but in general I think the WARN_ON is correct because it warns us against garbage and it's propagation if the correct checks are not in place.

Ah! Now I read your "triggers the warning in page_pgmap()" in the description.

It's usually a good idea to just include the splat you observed, if you did, and call it "triggers the VM_WARN_ON_ONCE_PAGE".

The "Fixes:" should go above the "---" in that case.

Quoted 50 lines
Quoted 37 lines
Quoted 20 lines


   mm/migrate_device.c | 13 ++++++++-----
   1 file changed, 8 insertions(+), 5 deletions(-)
mm/migrate_device.c +0 -3
index 6771893d4601..e0bf771edb6f 100644
@@ -153,14 +153,17 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp,
                   goto next;
               }
               page = vm_normal_page(migrate->vma, addr, pte);
-            pgmap = page_pgmap(page);
               if (page && !is_zone_device_page(page) &&
                   !(migrate->flags & MIGRATE_VMA_SELECT_SYSTEM))
                   goto next;
-            else if (page && is_device_coherent_page(page) &&
-                (!(migrate->flags & MIGRATE_VMA_SELECT_DEVICE_COHERENT) ||

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

Quoted 10 lines
mm/migrate_device.c +8 -2
-                 pgmap->owner != migrate->pgmap_owner))
-                goto next;
+            else if (page && is_device_coherent_page(page)) {
+                pgmap = page_pgmap(page);
+
+                if (!(migrate->flags &
+                    MIGRATE_VMA_SELECT_DEVICE_COHERENT) ||
+                    pgmap->owner != migrate->pgmap_owner)
+                    goto next;
+            }

Coding style wants you to use

if () {

} else if {

Quoted 9 lines

}

Not

if ()
else if {

}

Ack, checkpatch.pl missed it, but agreed

Quoted 1 line
Quoted 7 lines
Quoted 4 lines

Something simpler might be

page_pgmap(page)->owner != migrate->pgmap_owner

Yep, I had that and dropped it, the four clauses made it feel that it might benefit from a split.

Right, the mixture of && and || is confusing.

So with the {}

Thanks!

Signature · 3 lines
Cheers,

David / dhildenb
#4 lore raw
Quoted 29 lines · David Hildenbrand
Quoted 28 lines · Balbir Singh
Quoted 10 lines · David Hildenbrand
Quoted 9 lines · Balbir Singh

page_pgmap() is referenced before checking if the page is a zone device page and this triggers the warning in page_pgmap(). Refactor the code to use the helper function after relevant checks.

Cc Alistair Popple <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Dan Williams <[email protected]>
Quoted 8 lines
Quoted 5 lines

---

Fixes 7f1cfd71153b ("mm: allow compound zone device pages") on

mm-unstable

Is there actually something broken? At least for now, reading folio->pgmap should just work, although it might be garbage.

It triggers the VM_WARN_ON_ONCE_PAGE

static inline struct dev_pagemap *page_pgmap(const struct page *page) {
    VM_WARN_ON_ONCE_PAGE(!is_zone_device_page(page), page); return page_folio(page)->pgmap;
}

Quoted 7 lines
Quoted 3 lines

Nothing is broken, because the code below has checks for is_device_coherent_page(), but in general I think the WARN_ON is correct because it warns us against garbage and it's propagation if the correct checks are not in place.

Ah! Now I read your "triggers the warning in page_pgmap()" in the description.

It's usually a good idea to just include the splat you observed, if you did, and call it "triggers the VM_WARN_ON_ONCE_PAGE".

Sure

Quoted 2 lines

The "Fixes:" should go above the "---" in that case.

I was not sure if the commit-ids in mm-unstable are persistent, so I put it below the --- to avoid confusion

Quoted 59 lines
Quoted 51 lines
Quoted 38 lines
Quoted 23 lines

   mm/migrate_device.c | 13 ++++++++-----

   1 file changed, 8 insertions(+), 5 deletions(-)
mm/migrate_device.c +0 -1
index 6771893d4601..e0bf771edb6f 100644
@@ -153,14 +153,17 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp,
                    goto next;
                }
                page = vm_normal_page(migrate->vma, addr, pte);
-            pgmap = page_pgmap(page);

               if (page && !is_zone_device_page(page) && !(migrate->flags & MIGRATE_VMA_SELECT_SYSTEM)) goto next;
-            else if (page && is_device_coherent_page(page) && -                (!(migrate->flags & MIGRATE_VMA_SELECT_DEVICE_COHERENT) || -                 pgmap->owner != migrate->pgmap_owner))
-                goto next;

Quoted 8 lines

+            else if (page && is_device_coherent_page(page)) { +                pgmap = page_pgmap(page);
+
+                if (!(migrate->flags &
+                    MIGRATE_VMA_SELECT_DEVICE_COHERENT) || +                    pgmap->owner != migrate->pgmap_owner) +                    goto next;
+            }

Coding style wants you to use

if () {

} else if {

Quoted 9 lines

}

Not

if ()
else if {

}

Ack, checkpatch.pl missed it, but agreed

Quoted 1 line
Quoted 7 lines
Quoted 4 lines

Something simpler might be

page_pgmap(page)->owner != migrate->pgmap_owner

Yep, I had that and dropped it, the four clauses made it feel that it might benefit from a split.

Quoted 9 lines

Right, the mixture of && and || is confusing.

So with the {}

Thanks!

Thanks, I can respin it with the {} as needed.

Balbir

reviewer Reviewed-by
#5 lore raw
Quoted 38 lines · Balbir Singh
Quoted 29 lines · David Hildenbrand
Quoted 28 lines · Balbir Singh
Quoted 10 lines · David Hildenbrand
Quoted 9 lines · Balbir Singh

page_pgmap() is referenced before checking if the page is a zone device page and this triggers the warning in page_pgmap(). Refactor the code to use the helper function after relevant checks.

Cc Alistair Popple <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Dan Williams <[email protected]>
Quoted 8 lines
Quoted 5 lines

---

Fixes 7f1cfd71153b ("mm: allow compound zone device pages") on

mm-unstable

Is there actually something broken? At least for now, reading folio->pgmap should just work, although it might be garbage.

It triggers the VM_WARN_ON_ONCE_PAGE

static inline struct dev_pagemap *page_pgmap(const struct page *page) {
    VM_WARN_ON_ONCE_PAGE(!is_zone_device_page(page), page); return page_folio(page)->pgmap;
}

Quoted 7 lines
Quoted 3 lines

Nothing is broken, because the code below has checks for is_device_coherent_page(), but in general I think the WARN_ON is correct because it warns us against garbage and it's propagation if the correct checks are not in place.

Ah! Now I read your "triggers the warning in page_pgmap()" in the description.

It's usually a good idea to just include the splat you observed, if you did, and call it "triggers the VM_WARN_ON_ONCE_PAGE".

Quoted 7 lines

Sure

Quoted 2 lines

The "Fixes:" should go above the "---" in that case.

I was not sure if the commit-ids in mm-unstable are persistent, so I put it below the --- to avoid confusion

They are not, but I'm unsure what the process is here. I guess either I roll this fix into my series and repost or maybe Andrew takes it as is a separate fix on top of my series?

Either way the fix looks good, so with the code style comment below fixed feel free to add:

Quoted 72 lines
Quoted 59 lines
Quoted 51 lines
Quoted 38 lines
Quoted 23 lines

   mm/migrate_device.c | 13 ++++++++-----

   1 file changed, 8 insertions(+), 5 deletions(-)
mm/migrate_device.c +0 -1
index 6771893d4601..e0bf771edb6f 100644
@@ -153,14 +153,17 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp,
                    goto next;
                }
                page = vm_normal_page(migrate->vma, addr, pte);
-            pgmap = page_pgmap(page);

               if (page && !is_zone_device_page(page) && !(migrate->flags & MIGRATE_VMA_SELECT_SYSTEM)) goto next;
-            else if (page && is_device_coherent_page(page) && -                (!(migrate->flags & MIGRATE_VMA_SELECT_DEVICE_COHERENT) || -                 pgmap->owner != migrate->pgmap_owner))
-                goto next;

Quoted 8 lines

+            else if (page && is_device_coherent_page(page)) { +                pgmap = page_pgmap(page);
+
+                if (!(migrate->flags &
+                    MIGRATE_VMA_SELECT_DEVICE_COHERENT) || +                    pgmap->owner != migrate->pgmap_owner) +                    goto next;
+            }

Coding style wants you to use

if () {

} else if {

Quoted 9 lines

}

Not

if ()
else if {

}

Ack, checkpatch.pl missed it, but agreed

Quoted 1 line
Quoted 7 lines
Quoted 4 lines

Something simpler might be

page_pgmap(page)->owner != migrate->pgmap_owner

Yep, I had that and dropped it, the four clauses made it feel that it might benefit from a split.

Quoted 9 lines

Right, the mixture of && and || is confusing.

So with the {}

Thanks!

Thanks, I can respin it with the {} as needed.

Balbir

#6 lore raw
Quoted 42 lines · Alistair Popple
Quoted 41 lines · Balbir Singh
Quoted 29 lines · David Hildenbrand
Quoted 28 lines · Balbir Singh
Quoted 10 lines · David Hildenbrand
Quoted 9 lines · Balbir Singh

page_pgmap() is referenced before checking if the page is a zone device page and this triggers the warning in page_pgmap(). Refactor the code to use the helper function after relevant checks.

Cc Alistair Popple <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Dan Williams <[email protected]>
Quoted 8 lines
Quoted 5 lines

---

Fixes 7f1cfd71153b ("mm: allow compound zone device pages") on

mm-unstable

Is there actually something broken? At least for now, reading folio->pgmap should just work, although it might be garbage.

It triggers the VM_WARN_ON_ONCE_PAGE

static inline struct dev_pagemap *page_pgmap(const struct page *page) {
    VM_WARN_ON_ONCE_PAGE(!is_zone_device_page(page), page); return page_folio(page)->pgmap;
}

Quoted 7 lines
Quoted 3 lines

Nothing is broken, because the code below has checks for is_device_coherent_page(), but in general I think the WARN_ON is correct because it warns us against garbage and it's propagation if the correct checks are not in place.

Ah! Now I read your "triggers the warning in page_pgmap()" in the description.

It's usually a good idea to just include the splat you observed, if you did, and call it "triggers the VM_WARN_ON_ONCE_PAGE".

Sure

Quoted 1 line
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

The "Fixes:" should go above the "---" in that case.

I was not sure if the commit-ids in mm-unstable are persistent, so I put it below the --- to avoid confusion

They are not, but I'm unsure what the process is here. I guess either I roll this fix into my series and repost or maybe Andrew takes it as is a separate fix on top of my series?

Ah, I completely ignored that this is not in mm/mm-stable yet.

Yes, that should best just be squashed into the original commit as long as it is not in mm/mm-stable.

Signature · 3 lines
Cheers,

David / dhildenb