Skip to contentSkip to conversation

mm/migrate_device: document folio_get requirement before frozen PMD split

PATCH Superseded Usama Arif 4 participants 6 messages Mar 6 – Mar 9

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Usama Arif

author
#0 lore raw

PATCH 1/1 mm/migrate_device: document folio_get requirement before frozen PMD split

split_huge_pmd_address() with freeze=true splits a PMD migration entry into PTE migration entries, consuming one folio reference in the process. The folio_get() before it provides this reference.

Add a comment explaining this relationship and a VM_WARN_ON_ONCE to catch an unexpected refcount != 1 entry state.

---
 mm/migrate_device.c | 5 +++++
 1 file changed, 5 insertions(+)
mm/migrate_device.c +5 -0
index 78c7acf024615..6fa2878848a7e 100644
@@ -908,6 +908,11 @@ static int migrate_vma_split_unmapped_folio(struct migrate_vma *migrate,
 	unsigned long flags;
 	int ret = 0;
 
+	VM_WARN_ON_ONCE(folio_ref_count(folio) != 1);
+	/*
+	 * take a reference, since split_huge_pmd_address() with freeze = true
+	 * drops a reference at the end.
+	 */
 	folio_get(folio);
 	split_huge_pmd_address(migrate->vma, addr, true);
 	ret = folio_split_unmapped(folio, 0);
Signature · 1 line
2.47.3
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · Usama Arif

split_huge_pmd_address() with freeze=true splits a PMD migration entry into PTE migration entries, consuming one folio reference in the process. The folio_get() before it provides this reference.

Quoted 9 lines

Add a comment explaining this relationship and a VM_WARN_ON_ONCE to catch an unexpected refcount != 1 entry state.

---
 mm/migrate_device.c | 5 +++++
 1 file changed, 5 insertions(+)

Thanks for fixing the logic in my suggestion,

Best Regards,
Yan, Zi

Reviewed-by on v1 1/1
#2 lore raw
In reply to Usama Arif
Quoted 10 lines · Usama Arif

split_huge_pmd_address() with freeze=true splits a PMD migration entry into PTE migration entries, consuming one folio reference in the process. The folio_get() before it provides this reference.

Add a comment explaining this relationship and a VM_WARN_ON_ONCE to catch an unexpected refcount != 1 entry state.

LGTM! Thank you for investigating my concerns about this. I'm glad you found the proper answer.

Quoted 23 lines
---
 mm/migrate_device.c | 5 +++++
 1 file changed, 5 insertions(+)
mm/migrate_device.c +5 -1
index 78c7acf024615..6fa2878848a7e 100644
@@ -908,6 +908,11 @@ static int migrate_vma_split_unmapped_folio(struct migrate_vma *migrate,
        unsigned long flags;
        int ret = 0;
 
+       VM_WARN_ON_ONCE(folio_ref_count(folio) != 1);
+       /*
+        * take a reference, since split_huge_pmd_address() with freeze = true
+        * drops a reference at the end.
+        */
        folio_get(folio);
        split_huge_pmd_address(migrate->vma, addr, true);
        ret = folio_split_unmapped(folio, 0);
--

2.47.3

#3 lore raw
In reply to Usama Arif
Quoted 18 lines · Usama Arif

split_huge_pmd_address() with freeze=true splits a PMD migration entry into PTE migration entries, consuming one folio reference in the process. The folio_get() before it provides this reference.

Add a comment explaining this relationship and a VM_WARN_ON_ONCE to catch an unexpected refcount != 1 entry state.

---
 mm/migrate_device.c | 5 +++++
 1 file changed, 5 insertions(+)
mm/migrate_device.c +0 -0
index 78c7acf024615..6fa2878848a7e 100644

mm/migrate_device.c static int migrate_vma_split_unmapped_folio(struct migrate_vma *migrate, · line 911

Quoted 4 lines
mm/migrate_device.c +1 -0
@@ -908,6 +908,11 @@ static int migrate_vma_split_unmapped_folio(struct migrate_vma *migrate,
 	unsigned long flags;
 	int ret = 0;
 
+	VM_WARN_ON_ONCE(folio_ref_count(folio) != 1);

Can't we have speculative references here? In general, asserting that the refcount has an exact value (besides 0) is often shaky.

Quoted 7 lines
mm/migrate_device.c +4 -0
+	/*
+	 * take a reference, since split_huge_pmd_address() with freeze = true
+	 * drops a reference at the end.
+	 */
 	folio_get(folio);
 	split_huge_pmd_address(migrate->vma, addr, true);
 	ret = folio_split_unmapped(folio, 0);
Signature · 3 lines
Cheers,

David
author
#4 lore raw
Quoted 22 lines · David Hildenbrand (Arm)
Quoted 21 lines · Usama Arif

split_huge_pmd_address() with freeze=true splits a PMD migration entry into PTE migration entries, consuming one folio reference in the process. The folio_get() before it provides this reference.

Add a comment explaining this relationship and a VM_WARN_ON_ONCE to catch an unexpected refcount != 1 entry state.

---
 mm/migrate_device.c | 5 +++++
 1 file changed, 5 insertions(+)
mm/migrate_device.c +0 -0
index 78c7acf024615..6fa2878848a7e 100644
@@ -908,6 +908,11 @@ static int migrate_vma_split_unmapped_folio(struct migrate_vma *migrate,
 	unsigned long flags;
 	int ret = 0;
 
Quoted 4 lines

mm/migrate_device.c static int migrate_vma_split_unmapped_folio(struct migrate_vma *migrate, · line 911

Quoted 1 line
mm/migrate_device.c +1 -0
+	VM_WARN_ON_ONCE(folio_ref_count(folio) != 1);

Can't we have speculative references here? In general, asserting that the refcount has an exact value (besides 0) is often shaky.

I hope not at this point in code.

At this point, the folio is locked and unmapped (both done in migrate_vma_collect_huge_pmd()), and the present PMD was set to migration entry. It is isolated from LRU in migrate_device_unmap(). So the folio should not be visible to GUP or reclaim/compaction. Only anon, non-swapcache folios should reach here. So it won't run into any folio_try_get in page cache or swap cache.

The folio_get() done in migrate_vma_split_unmapped_folio() is consumed by split_huge_pmd_address(), and folio_split_unmapped() expects a folio_reference of 1 after this [1].

If its not considered good to assert a non zero refcount value, I can change the warning to a comment, but I think refcount should be 1 at this point, otherwise folio_split_unmapped will fail.

[1] https://elixir.bootlin.com/linux/v6.19.6/source/mm/huge_memory.c#L4137

Quoted 10 lines
Quoted 8 lines
mm/migrate_device.c +4 -0
+	/*
+	 * take a reference, since split_huge_pmd_address() with freeze = true
+	 * drops a reference at the end.
+	 */
 	folio_get(folio);
 	split_huge_pmd_address(migrate->vma, addr, true);
 	ret = folio_split_unmapped(folio, 0);
#5 lore raw
Quoted 31 lines · Usama Arif
Quoted 22 lines · David Hildenbrand (Arm)
Quoted 21 lines · Usama Arif

split_huge_pmd_address() with freeze=true splits a PMD migration entry into PTE migration entries, consuming one folio reference in the process. The folio_get() before it provides this reference.

Add a comment explaining this relationship and a VM_WARN_ON_ONCE to catch an unexpected refcount != 1 entry state.

---
 mm/migrate_device.c | 5 +++++
 1 file changed, 5 insertions(+)
mm/migrate_device.c +0 -0
index 78c7acf024615..6fa2878848a7e 100644
@@ -908,6 +908,11 @@ static int migrate_vma_split_unmapped_folio(struct migrate_vma *migrate,
 	unsigned long flags;
 	int ret = 0;
 
Quoted 4 lines

mm/migrate_device.c static int migrate_vma_split_unmapped_folio(struct migrate_vma *migrate, · line 911

Quoted 1 line
mm/migrate_device.c +1 -0
+	VM_WARN_ON_ONCE(folio_ref_count(folio) != 1);

Can't we have speculative references here? In general, asserting that the refcount has an exact value (besides 0) is often shaky.

Quoted 7 lines

I hope not at this point in code.

At this point, the folio is locked and unmapped (both done in migrate_vma_collect_huge_pmd()), and the present PMD was set to migration entry. It is isolated from LRU in migrate_device_unmap(). So the folio should not be visible to GUP or reclaim/compaction. Only anon, non-swapcache folios should reach here. So it won't run into any folio_try_get in page cache or swap cache.

We have other pfn walkers that can just temporary grab a reference, to immediately back off. So it's not very reliable to depend on that.

Quoted 8 lines

The folio_get() done in migrate_vma_split_unmapped_folio() is consumed by split_huge_pmd_address(), and folio_split_unmapped() expects a folio_reference of 1 after this [1].

If its not considered good to assert a non zero refcount value, I can change the warning to a comment, but I think refcount should be 1 at this point, otherwise folio_split_unmapped will fail.

Well, yes. folio_split_unmapped() will handle this gracefully (as documented), without triggering a warning.

So best to remove the VM_WARN_ON_ONCE().

Signature · 3 lines
Cheers,

David