Skip to contentSkip to conversation

mm/migrate_device: Cleanup up PMD Checks and warnings

PATCH Superseded Sunny Patel 5 participants 10 messages Apr 14 – Apr 19

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/migrate_device: Cleanup up PMD Checks and warnings

Remove the odd VM_WARN_ON_FOLIO(!folio, folio) usage and replace it with a simpler VM_WARN_ON_ONCE(!folio) check.

Drop the redundant VM_WARN_ON_ONCE(!pmd_none(*pmdp) && !is_huge_zero_pmd(*pmdp)).

Refactor the PMD checks to use pmd_present(), making the control flow clearer and avoiding duplicate condition checks.

---
 mm/migrate_device.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
mm/migrate_device.c +4 -4
index 8079676c8f1f..fe1c1999aa2d 100644
@@ -807,8 +807,7 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate,
 	bool flush = false;
 	unsigned long i;
 
-	VM_WARN_ON_FOLIO(!folio, folio);
-	VM_WARN_ON_ONCE(!pmd_none(*pmdp) && !is_huge_zero_pmd(*pmdp));
+	VM_WARN_ON_ONCE(!folio);
 
 	if (!thp_vma_suitable_order(vma, addr, HPAGE_PMD_ORDER))
 		return -EINVAL;
@@ -865,12 +864,13 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate,
 	if (userfaultfd_missing(vma))
 		goto unlock_abort;
 
-	if (!pmd_none(*pmdp)) {
+	if (pmd_present(*pmdp)) {
 		if (!is_huge_zero_pmd(*pmdp))
 			goto unlock_abort;
 		flush = true;
-	} else if (!pmd_none(*pmdp))
+	} else if (!pmd_none(*pmdp)) {
 		goto unlock_abort;
+	}
 
 	add_mm_counter(vma->vm_mm, MM_ANONPAGES, HPAGE_PMD_NR);
 	folio_add_new_anon_rmap(folio, vma, addr, RMAP_EXCLUSIVE);
Signature · 1 line
2.43.0
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 19 lines · Sunny Patel

Remove the odd VM_WARN_ON_FOLIO(!folio, folio) usage and replace it with a simpler VM_WARN_ON_ONCE(!folio) check.

Drop the redundant VM_WARN_ON_ONCE(!pmd_none(*pmdp) && !is_huge_zero_pmd(*pmdp)).

Refactor the PMD checks to use pmd_present(), making the control flow clearer and avoiding duplicate condition checks.

---
 mm/migrate_device.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
mm/migrate_device.c +0 -0
index 8079676c8f1f..fe1c1999aa2d 100644

mm/migrate_device.c static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, · line 810

Quoted 6 lines
mm/migrate_device.c +1 -2
@@ -807,8 +807,7 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate,
 	bool flush = false;
 	unsigned long i;
 
-	VM_WARN_ON_FOLIO(!folio, folio);
-	VM_WARN_ON_ONCE(!pmd_none(*pmdp) && !is_huge_zero_pmd(*pmdp));
+	VM_WARN_ON_ONCE(!folio);

I'd test for "page" here instead. But it doesn't matter as of today.

Quoted 9 lines
mm/migrate_device.c +1 -1
 	if (!thp_vma_suitable_order(vma, addr, HPAGE_PMD_ORDER))
 		return -EINVAL;
@@ -865,12 +864,13 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate,
 	if (userfaultfd_missing(vma))
 		goto unlock_abort;
 
-	if (!pmd_none(*pmdp)) {
+	if (pmd_present(*pmdp)) {

mm/migrate_device.c static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, · line 876

Quoted 10 lines
mm/migrate_device.c +2 -1
 		if (!is_huge_zero_pmd(*pmdp))
 			goto unlock_abort;
 		flush = true;
-	} else if (!pmd_none(*pmdp))
+	} else if (!pmd_none(*pmdp)) {
 		goto unlock_abort;
+	}
 
 	add_mm_counter(vma->vm_mm, MM_ANONPAGES, HPAGE_PMD_NR);
 	folio_add_new_anon_rmap(folio, vma, addr, RMAP_EXCLUSIVE);

is_huge_zero_pmd() checks pmd_present(), so we didn't have a bug before.

We could also do:

if (is_huge_zero_pmd(*pmdp)) {
	flush = true;
} else if (!pmd_none(*pmdp)) {
	goto unlock_abort;
}

But I don't particularly care

Signature · 3 lines
Cheers,

David
#2 lore raw
Quoted 1 line · On Thu, 16 Apr 2026 21:44:15 +0200 "David Hildenbrand (Arm)"…

On 4/14/26 16:13, Sunny Patel wrote:

[...]

Quoted 19 lines
Quoted 6 lines
mm/migrate_device.c +1 -1
@@ -865,12 +864,13 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate,
 	if (userfaultfd_missing(vma))
 		goto unlock_abort;
 
-	if (!pmd_none(*pmdp)) {
+	if (pmd_present(*pmdp)) {

mm/migrate_device.c static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, · line 876

Quoted 10 lines
mm/migrate_device.c +2 -1
 		if (!is_huge_zero_pmd(*pmdp))
 			goto unlock_abort;
 		flush = true;
-	} else if (!pmd_none(*pmdp))
+	} else if (!pmd_none(*pmdp)) {
 		goto unlock_abort;
+	}
 
 	add_mm_counter(vma->vm_mm, MM_ANONPAGES, HPAGE_PMD_NR);
 	folio_add_new_anon_rmap(folio, vma, addr, RMAP_EXCLUSIVE);

is_huge_zero_pmd() checks pmd_present(), so we didn't have a bug before.

Quoted 7 lines

We could also do:

if (is_huge_zero_pmd(*pmdp)) {
	flush = true;
} else if (!pmd_none(*pmdp)) {
	goto unlock_abort;
}

Then we could even further remove the braces and reduce one more line, nice!

Thanks,
SJ

[...]

#3 lore raw
Quoted 25 lines · SeongJae Park
Quoted 1 line · On Thu, 16 Apr 2026 21:44:15 +0200 "David Hildenbrand (Arm)"…

On 4/14/26 16:13, Sunny Patel wrote:

[...]

Quoted 21 lines
Quoted 6 lines
mm/migrate_device.c +1 -1
@@ -865,12 +864,13 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate,
 	if (userfaultfd_missing(vma))
 		goto unlock_abort;
 
-	if (!pmd_none(*pmdp)) {
+	if (pmd_present(*pmdp)) {

mm/migrate_device.c static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, · line 876

Quoted 10 lines
mm/migrate_device.c +2 -1
 		if (!is_huge_zero_pmd(*pmdp))
 			goto unlock_abort;
 		flush = true;
-	} else if (!pmd_none(*pmdp))
+	} else if (!pmd_none(*pmdp)) {
 		goto unlock_abort;
+	}
 
 	add_mm_counter(vma->vm_mm, MM_ANONPAGES, HPAGE_PMD_NR);
 	folio_add_new_anon_rmap(folio, vma, addr, RMAP_EXCLUSIVE);

is_huge_zero_pmd() checks pmd_present(), so we didn't have a bug before.

We could also do:

Quoted 7 lines
Quoted 5 lines
if (is_huge_zero_pmd(*pmdp)) {
	flush = true;
} else if (!pmd_none(*pmdp)) {
	goto unlock_abort;
}

Then we could even further remove the braces and reduce one more line, nice!

Indeed :)

Signature · 3 lines
Cheers,

David
#4 lore raw
Quoted 25 lines · SeongJae Park
Quoted 1 line · On Thu, 16 Apr 2026 21:44:15 +0200 "David Hildenbrand (Arm)"…

On 4/14/26 16:13, Sunny Patel wrote:

[...]

Quoted 21 lines
Quoted 6 lines
mm/migrate_device.c +1 -1
@@ -865,12 +864,13 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate,
 	if (userfaultfd_missing(vma))
 		goto unlock_abort;
 
-	if (!pmd_none(*pmdp)) {
+	if (pmd_present(*pmdp)) {

mm/migrate_device.c static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, · line 876

Quoted 10 lines
mm/migrate_device.c +2 -1
 		if (!is_huge_zero_pmd(*pmdp))
 			goto unlock_abort;
 		flush = true;
-	} else if (!pmd_none(*pmdp))
+	} else if (!pmd_none(*pmdp)) {
 		goto unlock_abort;
+	}
 
 	add_mm_counter(vma->vm_mm, MM_ANONPAGES, HPAGE_PMD_NR);
 	folio_add_new_anon_rmap(folio, vma, addr, RMAP_EXCLUSIVE);

is_huge_zero_pmd() checks pmd_present(), so we didn't have a bug before.

We could also do:

Quoted 7 lines
Quoted 5 lines
if (is_huge_zero_pmd(*pmdp)) {
	flush = true;
} else if (!pmd_none(*pmdp)) {
	goto unlock_abort;
}

Then we could even further remove the braces and reduce one more line, nice!

is_huge_zero_pmd() didn't check for pmd_present as of now as per the current implementation of it so additional check require for pmd_present(). Please let me know if anything needs to add in here.

Thanks,
Sunny Patel

#5 lore raw
Quoted 15 lines · Sunny Patel
Quoted 14 lines · SeongJae Park

On Thu, 16 Apr 2026 21:44:15 +0200 "David Hildenbrand (Arm)" <[email protected]> wrote:

[...]

Quoted 10 lines

is_huge_zero_pmd() checks pmd_present(), so we didn't have a bug before.

We could also do:

if (is_huge_zero_pmd(*pmdp)) {
	flush = true;
} else if (!pmd_none(*pmdp)) {
	goto unlock_abort;
}
Quoted 3 lines
Quoted 1 line

Then we could even further remove the braces and reduce one more line, nice!

is_huge_zero_pmd() didn't check for pmd_present as of now as per the current implementation of it so additional check require for pmd_present().

I don't know what you mean. Here is the code in the tree:
static inline bool is_huge_zero_pmd(pmd_t pmd)

{
	return pmd_present(pmd) && is_huge_zero_pfn(pmd_pfn(pmd));
}
Signature · 3 lines
Cheers,

David
#6 lore raw

got it. thanks for the pointer.
Sent the new version of this patch. please check on it.

Thanks,
Sunny Patel

reviewer
#7 lore raw
In reply to David Hildenbrand (Arm)
Quoted 51 lines · David Hildenbrand (Arm)
Quoted 19 lines · Sunny Patel

Remove the odd VM_WARN_ON_FOLIO(!folio, folio) usage and replace it with a simpler VM_WARN_ON_ONCE(!folio) check.

Drop the redundant VM_WARN_ON_ONCE(!pmd_none(*pmdp) && !is_huge_zero_pmd(*pmdp)).

Refactor the PMD checks to use pmd_present(), making the control flow clearer and avoiding duplicate condition checks.

---
 mm/migrate_device.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
mm/migrate_device.c +0 -0
index 8079676c8f1f..fe1c1999aa2d 100644

mm/migrate_device.c static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, · line 810

Quoted 6 lines
mm/migrate_device.c +1 -2
@@ -807,8 +807,7 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate,
 	bool flush = false;
 	unsigned long i;
 
-	VM_WARN_ON_FOLIO(!folio, folio);
-	VM_WARN_ON_ONCE(!pmd_none(*pmdp) && !is_huge_zero_pmd(*pmdp));
+	VM_WARN_ON_ONCE(!folio);

I'd test for "page" here instead. But it doesn't matter as of today.

Quoted 9 lines
mm/migrate_device.c +1 -1
 	if (!thp_vma_suitable_order(vma, addr, HPAGE_PMD_ORDER))
 		return -EINVAL;
@@ -865,12 +864,13 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate,
 	if (userfaultfd_missing(vma))
 		goto unlock_abort;
 
-	if (!pmd_none(*pmdp)) {
+	if (pmd_present(*pmdp)) {

mm/migrate_device.c static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, · line 876

Quoted 10 lines
mm/migrate_device.c +2 -1
 		if (!is_huge_zero_pmd(*pmdp))
 			goto unlock_abort;
 		flush = true;
-	} else if (!pmd_none(*pmdp))
+	} else if (!pmd_none(*pmdp)) {
 		goto unlock_abort;
+	}
 
 	add_mm_counter(vma->vm_mm, MM_ANONPAGES, HPAGE_PMD_NR);
 	folio_add_new_anon_rmap(folio, vma, addr, RMAP_EXCLUSIVE);

is_huge_zero_pmd() checks pmd_present(), so we didn't have a bug before.

Quoted 7 lines

We could also do:

if (is_huge_zero_pmd(*pmdp)) {
	flush = true;
} else if (!pmd_none(*pmdp)) {
	goto unlock_abort;
}

I think that this is better. And because the patch is for code cleanup, why not do it better.

Quoted 3 lines

But I don't particularly care

---
Best Regards,
Huang, Ying

reviewer Acked-by on v1 1/1
#8 lore raw
In reply to Sunny Patel
Quoted 6 lines · Sunny Patel

Remove the odd VM_WARN_ON_FOLIO(!folio, folio) usage and replace it with a simpler VM_WARN_ON_ONCE(!folio) check.

Drop the redundant VM_WARN_ON_ONCE(!pmd_none(*pmdp) && !is_huge_zero_pmd(*pmdp)).

Quoted 8 lines

Refactor the PMD checks to use pmd_present(), making the control flow clearer and avoiding duplicate condition checks.

---
 mm/migrate_device.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

Best Regards,
Yan, Zi

Acked-by on v1 1/1
#9 lore raw
In reply to Sunny Patel
Quoted 10 lines · On Tue, 14 Apr 2026 19:43:38 +0530 Sunny Patel <[email protected]>…

Remove the odd VM_WARN_ON_FOLIO(!folio, folio) usage and replace it with a simpler VM_WARN_ON_ONCE(!folio) check.

Drop the redundant VM_WARN_ON_ONCE(!pmd_none(*pmdp) && !is_huge_zero_pmd(*pmdp)).

Refactor the PMD checks to use pmd_present(), making the control flow clearer and avoiding duplicate condition checks.

[...]

mm/migrate_device.c static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate, · line 873

Quoted 13 lines
mm/migrate_device.c +3 -2
@@ -865,12 +864,13 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate,
 	if (userfaultfd_missing(vma))
 		goto unlock_abort;
 
-	if (!pmd_none(*pmdp)) {
+	if (pmd_present(*pmdp)) {
 		if (!is_huge_zero_pmd(*pmdp))
 			goto unlock_abort;
 		flush = true;
-	} else if (!pmd_none(*pmdp))
+	} else if (!pmd_none(*pmdp)) {
 		goto unlock_abort;
+	}

I like this coding style [1] fix.

[1] https://docs.kernel.org/process/coding-style.html#placing-braces-and-spaces

Thanks,
SJ

[...]