Skip to contentSkip to conversation

mm/migrate_device: Cleanup up PMD Checks and warnings

PATCH Applied Sunny Patel 5 participants 10 messages Apr 19 – Apr 25

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

PATCH v2 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, making the control flow
clearer and avoiding duplicate condition checks.

---

Changes in v2:
- Removed additional check for pmd_present() as is_huge_zero_pmd() already checks for pmd_present().

Missed it in first version of this patch.

 mm/migrate_device.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
mm/migrate_device.c +3 -6
index 8079676c8f1f..2c7e89a6d1b1 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,11 +864,9 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate,
 	if (userfaultfd_missing(vma))
 		goto unlock_abort;
 
-	if (!pmd_none(*pmdp)) {
-		if (!is_huge_zero_pmd(*pmdp))
-			goto unlock_abort;
+	if (is_huge_zero_pmd(*pmdp))
 		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);
Signature · 1 line
2.43.0
reviewer Acked-by on v2 1/1
#1 lore raw
Quoted 12 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, making the control flow
clearer and avoiding duplicate condition checks.

---

Quoted 9 lines

Changes in v2:
- Removed additional check for pmd_present() as is_huge_zero_pmd() already checks for pmd_present().

Missed it in first version of this patch.

 mm/migrate_device.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

LGTM.

--
Best Regards,
Yan, Zi

reviewer Reviewed-by on v2 1/1
#2 lore raw
In reply to Sunny Patel
Quoted 39 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, making the control flow
clearer and avoiding duplicate condition checks.

---

Changes in v2:
- Removed additional check for pmd_present() as is_huge_zero_pmd() already checks for pmd_present().

Missed it in first version of this patch.

 mm/migrate_device.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
mm/migrate_device.c +1 -2
index 8079676c8f1f..2c7e89a6d1b1 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,11 +864,9 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate,
 	if (userfaultfd_missing(vma))
 		goto unlock_abort;
 

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

Quoted 10 lines
mm/migrate_device.c +2 -4
-	if (!pmd_none(*pmdp)) {
-		if (!is_huge_zero_pmd(*pmdp))
-			goto unlock_abort;
+	if (is_huge_zero_pmd(*pmdp))
 		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);

LGTM, feel free to add my

in the future versions.

---
Best Regards,
Huang, Ying

maintainer Acked-by on v2 1/1
#3 lore raw
In reply to Sunny Patel
Quoted 11 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, making the control flow
clearer and avoiding duplicate condition checks.

---

Signature · 3 lines
Cheers,

David
maintainer
#4 lore raw
In reply to Sunny Patel
Quoted 8 lines · On Sun, 19 Apr 2026 23:17:43 +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, making the control flow
clearer and avoiding duplicate condition checks.

AI review might have found a bug in the surrounding code:

	https://sashiko.dev/#/patchset/[email protected]
#5 lore raw
Quoted 8 lines · On Sun, 19 Apr 2026 23:17:43 +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, making the control flow
clearer and avoiding duplicate condition checks.

AI review might have found a bug in the surrounding code:

        https://sashiko.dev/#/patchset/[email protected]

After looking into the AI comment found two potential leak.

1. The goto should be unlock_abort instead of abort to ensure the spinlock is released.

2. The pgtable allocation at the top of the function is indeed not freed on the unlock_abort path. Will add a pte_free(vma->vm_mm, pgtable).

Since this patch is more of Cleanup on PMD checks and Warnings Is it ok if I send different patch to address this potential leak issues or need it in this revision itself?

Thanks,
Sunny Patel

maintainer
#6 lore raw
Quoted 19 lines · On Fri, 24 Apr 2026 22:52:08 +0530 Sunny Patel <[email protected]>…
Quoted 8 lines · On Sun, 19 Apr 2026 23:17:43 +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, making the control flow
clearer and avoiding duplicate condition checks.

AI review might have found a bug in the surrounding code:

        https://sashiko.dev/#/patchset/[email protected]

After looking into the AI comment found two potential leak.

1. The goto should be unlock_abort instead of abort to ensure the spinlock is released.

Quoted 8 lines

2. The pgtable allocation at the top of the function is indeed not freed on the unlock_abort path. Will add a pte_free(vma->vm_mm, pgtable).

Since this patch is more of Cleanup on PMD checks and Warnings Is it ok if I send different patch to address this potential leak issues or need it in this revision itself?

A separate patch, please.

It's best to prepare the bugfix patch against current Linus mainline, then any cleanups come on top of the bugfix.

Thanks.

#7 lore raw
In reply to Andrew Morton
Quoted 8 lines · On Sun, 19 Apr 2026 23:17:43 +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, making the control flow
clearer and avoiding duplicate condition checks.

AI review might have found a bug in the surrounding code:

        https://sashiko.dev/#/patchset/[email protected]

NOTE: Ignore the previous message was sent on different Subject Header by mistake.

After looking into the AI comment found two potential leak.

1. The goto should be unlock_abort instead of abort to ensure the spinlock is released.

2. The pgtable allocation at the top of the function is indeed not freed on the unlock_abort path. Will add a pte_free(vma->vm_mm, pgtable).

Since this patch is more of Cleanup on PMD checks and Warnings Is it ok if I send different patch to address this potential leak issues or need it in this revision itself?

Thanks,
Sunny Patel

#8 lore raw
Quoted 22 lines · Sunny Patel
Quoted 8 lines · On Sun, 19 Apr 2026 23:17:43 +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, making the control flow
clearer and avoiding duplicate condition checks.

AI review might have found a bug in the surrounding code:

        https://sashiko.dev/#/patchset/[email protected]

NOTE: Ignore the previous message was sent on different Subject Header by mistake.

After looking into the AI comment found two potential leak.

1. The goto should be unlock_abort instead of abort to ensure the spinlock is released.

Quoted 8 lines

2. The pgtable allocation at the top of the function is indeed not freed on the unlock_abort path. Will add a pte_free(vma->vm_mm, pgtable).

Since this patch is more of Cleanup on PMD checks and Warnings Is it ok if I send different patch to address this potential leak issues or need it in this revision itself?

No, independent issues -> independent patches :)

Signature · 3 lines
Cheers,

David