Skip to contentSkip to conversation

mm/huge_memory: disallow raw PMD mappings of the huge zero page

PATCH In review Lance Yang 3 participants 11 messages Sep 17 – Sep 19

Subsystems (3)
lore
Share & tools
lore

Lance Yang

author reviewer
#0 lore raw

PATCH 1/1 mm/huge_memory: disallow raw PMD mappings of the huge zero page

vm_mixed_zeropage_allowed() validates zeropage insertion into VM_MIXEDMAP VMAs. No in-tree user needs to insert the huge zero page through vmf_insert_pfn_pmd().

Return VM_FAULT_SIGBUS if vmf_insert_pfn_pmd() is asked to map it.

---
 mm/huge_memory.c | 3 +++
 1 file changed, 3 insertions(+)
mm/huge_memory.c +3 -0
index bfd972b997f9..c02f21ce4f4c 100644
@@ -1728,6 +1728,9 @@ vm_fault_t vmf_insert_pfn_pmd(struct vm_fault *vmf, unsigned long pfn,
 						(VM_PFNMAP|VM_MIXEDMAP));
 	BUG_ON((vma->vm_flags & VM_PFNMAP) && vma_is_cow_mapping(vma));
 
+	if (unlikely(is_huge_zero_pfn(pfn)))
+		return VM_FAULT_SIGBUS;
+
 	pfnmap_setup_cachemode_pfn(pfn, &pgprot);
 
 	return insert_pmd(vma, addr, vmf->pmd, fop, pgprot, write);
Signature · 1 line
2.39.3 (Apple Git-146)
#1 lore raw
Quoted 10 lines · Lance Yang

vm_mixed_zeropage_allowed() validates zeropage insertion into VM_MIXEDMAP VMAs. No in-tree user needs to insert the huge zero page through vmf_insert_pfn_pmd().

Return VM_FAULT_SIGBUS if vmf_insert_pfn_pmd() is asked to map it.

The patch looks fine, but don't we want to cover the same for non-huge zero page in vmf_insert_pfn_prot()?

Signature · 1 line
  Kiryl Shutsemau / Kirill A. Shutemov
#2 lore raw
Quoted 7 lines · Kiryl Shutsemau
Quoted 6 lines · Lance Yang

vm_mixed_zeropage_allowed() validates zeropage insertion into VM_MIXEDMAP VMAs. No in-tree user needs to insert the huge zero page through vmf_insert_pfn_pmd().

Return VM_FAULT_SIGBUS if vmf_insert_pfn_pmd() is asked to map it.

Quoted 7 lines
Quoted 4 lines

The patch looks fine, but don't we want to cover the same for non-huge zero page in vmf_insert_pfn_prot()?

Why would the zeropage be a problem in PFNMAP mapping?

Signature · 3 lines
Cheers,

David
#10 lore raw
In reply to Lance Yang
Quoted 3 lines · Lance Yang

vm_mixed_zeropage_allowed() validates zeropage insertion into VM_MIXEDMAP VMAs. No in-tree user needs to insert the huge zero page through vmf_insert_pfn_pmd().

I think we want a clear description why we would want to forbid it. Is it related to vm_mixed_ok/vm_mixed_zeropage_allowed?

Once again I am confused about which interfaces are actually valid for VM_MIXEDMAP.>

Quoted 24 lines

Return VM_FAULT_SIGBUS if vmf_insert_pfn_pmd() is asked to map it.

---
 mm/huge_memory.c | 3 +++
 1 file changed, 3 insertions(+)
mm/huge_memory.c +3 -0
index bfd972b997f9..c02f21ce4f4c 100644
@@ -1728,6 +1728,9 @@ vm_fault_t vmf_insert_pfn_pmd(struct vm_fault *vmf, unsigned long pfn,
 						(VM_PFNMAP|VM_MIXEDMAP));
 	BUG_ON((vma->vm_flags & VM_PFNMAP) && vma_is_cow_mapping(vma));
 
+	if (unlikely(is_huge_zero_pfn(pfn)))
+		return VM_FAULT_SIGBUS;
+
 	pfnmap_setup_cachemode_pfn(pfn, &pgprot);
 
 	return insert_pmd(vma, addr, vmf->pmd, fop, pgprot, write);
Signature · 3 lines
Cheers,

David
#3 lore raw
In reply to David Hildenbrand (Arm)
Quoted 13 lines · David Hildenbrand (Arm)
Quoted 12 lines · Kiryl Shutsemau
Quoted 10 lines · Lance Yang

vm_mixed_zeropage_allowed() validates zeropage insertion into VM_MIXEDMAP VMAs. No in-tree user needs to insert the huge zero page through vmf_insert_pfn_pmd().

Return VM_FAULT_SIGBUS if vmf_insert_pfn_pmd() is asked to map it.

Quoted 4 lines
Quoted 2 lines

The patch looks fine, but don't we want to cover the same for non-huge zero page in vmf_insert_pfn_prot()?

Why would the zeropage be a problem in PFNMAP mapping?

Nothing enforces that it is read-only.

Signature · 1 line
  Kiryl Shutsemau / Kirill A. Shutemov
author reviewer
#4 lore raw
Quoted 14 lines · Kiryl Shutsemau
Quoted 13 lines · David Hildenbrand (Arm)
Quoted 12 lines · Kiryl Shutsemau
Quoted 10 lines · Lance Yang

vm_mixed_zeropage_allowed() validates zeropage insertion into VM_MIXEDMAP VMAs. No in-tree user needs to insert the huge zero page through vmf_insert_pfn_pmd().

Return VM_FAULT_SIGBUS if vmf_insert_pfn_pmd() is asked to map it.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

The patch looks fine, but don't we want to cover the same for non-huge zero page in vmf_insert_pfn_prot()?

Why would the zeropage be a problem in PFNMAP mapping?

Nothing enforces that it is read-only.

No in-tree user inserts the zeropage through vmf_insert_pfn_prot(), so rejecting it there should be fine.

Something like:

---8<---
The huge zeropage is reclaimable, but a raw PFN mapping does not pin it. A PMD mapping of huge_zero_pfn can therefore outlive the folio.

No in-tree user needs either mapping, so reject both with VM_FAULT_SIGBUS rather than risk making either shared zero page writable.

---

 mm/huge_memory.c | 3 +++
 mm/memory.c      | 3 +++
 2 files changed, 6 insertions(+)
mm/huge_memory.c +3 -0
index b49bffe36d22..de34d64aa763 100644
@@ -1723,6 +1723,9 @@ vm_fault_t vmf_insert_pfn_pmd(struct vm_fault *vmf, unsigned long pfn,
 						(VM_PFNMAP|VM_MIXEDMAP));
 	BUG_ON((vma->vm_flags & VM_PFNMAP) && vma_is_cow_mapping(vma));
 
+	if (unlikely(is_huge_zero_pfn(pfn)))
+		return VM_FAULT_SIGBUS;
+
 	pfnmap_setup_cachemode_pfn(pfn, &pgprot);
 
 	return insert_pmd(vma, addr, vmf->pmd, fop, pgprot, write);
mm/memory.c +3 -0
index 9e4a70421a6b..5d2d73c69fed 100644
@@ -2956,6 +2956,9 @@ vm_fault_t vmf_insert_pfn_prot(struct vm_area_struct *vma, unsigned long addr,
 	BUG_ON((vma->vm_flags & VM_PFNMAP) && vma_is_cow_mapping(vma));
 	BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn));
 
+	if (unlikely(is_zero_pfn(pfn)))
+		return VM_FAULT_SIGBUS;
+
 	if (addr < vma->vm_start || addr >= vma->vm_end)
 		return VM_FAULT_SIGBUS;
 

--

That would keep both zeropages out of writable raw-PFN fault mappings ... wdyt?

Cheers, Lance

reviewer Reviewed-by
#5 lore raw
Quoted 32 lines · Lance Yang
Quoted 14 lines · Kiryl Shutsemau
Quoted 13 lines · David Hildenbrand (Arm)
Quoted 12 lines · Kiryl Shutsemau
Quoted 10 lines · Lance Yang

vm_mixed_zeropage_allowed() validates zeropage insertion into VM_MIXEDMAP VMAs. No in-tree user needs to insert the huge zero page through vmf_insert_pfn_pmd().

Return VM_FAULT_SIGBUS if vmf_insert_pfn_pmd() is asked to map it.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

The patch looks fine, but don't we want to cover the same for non-huge zero page in vmf_insert_pfn_prot()?

Why would the zeropage be a problem in PFNMAP mapping?

Nothing enforces that it is read-only.

No in-tree user inserts the zeropage through vmf_insert_pfn_prot(), so rejecting it there should be fine.

Something like:

---8<---
The huge zeropage is reclaimable, but a raw PFN mapping does not pin it. A PMD mapping of huge_zero_pfn can therefore outlive the folio.

Quoted 7 lines

No in-tree user needs either mapping, so reject both with VM_FAULT_SIGBUS rather than risk making either shared zero page writable.

Signature · 1 line
  Kiryl Shutsemau / Kirill A. Shutemov
#9 lore raw
In reply to Kiryl Shutsemau
Quoted 9 lines · Kiryl Shutsemau
Quoted 6 lines · David Hildenbrand (Arm)
Quoted 3 lines · Kiryl Shutsemau

The patch looks fine, but don't we want to cover the same for non-huge zero page in vmf_insert_pfn_prot()?

Why would the zeropage be a problem in PFNMAP mapping?

Nothing enforces that it is read-only.

Well, ok, that at least would be easy to check.

There are other reasons why we'd want to disallow it ... similar to the check in vm_mixed_ok().

Which reminds me that we should try getting rid of vmf_insert_mixed() at some point ... but it's tricky.

Signature · 3 lines
Cheers,

David
#6 lore raw
In reply to Lance Yang
Quoted 8 lines · Lance Yang
Quoted 5 lines · Kiryl Shutsemau
Quoted 2 lines · David Hildenbrand (Arm)

Why would the zeropage be a problem in PFNMAP mapping?

Nothing enforces that it is read-only.

Quoted 8 lines

No in-tree user inserts the zeropage through vmf_insert_pfn_prot(), so rejecting it there should be fine.

Something like:

---8<---
The huge zeropage is reclaimable, but a raw PFN mapping does not pin it. A PMD mapping of huge_zero_pfn can therefore outlive the folio.

Well, just with any other folio that we PFNMAP, the mapping would have to hold a reference. mm_get_huge_zero_folio() can be used for that, or just grabbing a folio reference (not so nice, but works; GUP uses that, for example, to keep the huge zero folio alive).

Quoted 3 lines

No in-tree user needs either mapping, so reject both with VM_FAULT_SIGBUS rather than risk making either shared zero page writable.

That is the better reason. You could also just say that handling shared zeropages correctly is more involved (see vm_mixed_ok()), and as there are no users, we can just disallow it instead of trying to fix it.

Quoted 6 lines

---

Signature · 3 lines
Cheers,

David
author reviewer
#7 lore raw
Quoted 24 lines · David Hildenbrand (Arm)
Quoted 8 lines · Lance Yang
Quoted 5 lines · Kiryl Shutsemau
Quoted 2 lines · David Hildenbrand (Arm)

Why would the zeropage be a problem in PFNMAP mapping?

Nothing enforces that it is read-only.

Quoted 8 lines

No in-tree user inserts the zeropage through vmf_insert_pfn_prot(), so rejecting it there should be fine.

Something like:

---8<---
The huge zeropage is reclaimable, but a raw PFN mapping does not pin it. A PMD mapping of huge_zero_pfn can therefore outlive the folio.

Well, just with any other folio that we PFNMAP, the mapping would have to hold a reference. mm_get_huge_zero_folio() can be used for that, or just grabbing a folio reference (not so nice, but works; GUP uses that, for example, to keep the huge zero folio alive).

Quoted 1 line
Quoted 6 lines
Quoted 2 lines

No in-tree user needs either mapping, so reject both with VM_FAULT_SIGBUS rather than risk making either shared zero page writable.

That is the better reason. You could also just say that handling shared zeropages correctly is more involved (see vm_mixed_ok()), and as there are no users, we can just disallow it instead of trying to fix it.

Ah I see, thanks! obviously, I hadn't fully understood that ...

How about this instead?

"
Handling the huge/shared zeropage correctly in vmf_insert_pfn_pmd() and vmf_insert_pfn_prot() is more involved. We would need to check whether the VMA allows it and keep the mapping read-only, similar to the checks in vm_mixed_ok().

No in-tree user needs that support, so reject these mappings with VM_FAULT_SIGBUS rather than complicate the code for now. "

Thanks, Lance

Quoted 12 lines
Quoted 6 lines

---

Signature · 3 lines
Cheers,

David
#8 lore raw
Quoted 37 lines · Lance Yang
Quoted 18 lines · David Hildenbrand (Arm)
Quoted 10 lines · Lance Yang

No in-tree user inserts the zeropage through vmf_insert_pfn_prot(), so rejecting it there should be fine.

Something like:

---8<---
The huge zeropage is reclaimable, but a raw PFN mapping does not pin it. A PMD mapping of huge_zero_pfn can therefore outlive the folio.

Well, just with any other folio that we PFNMAP, the mapping would have to hold a reference. mm_get_huge_zero_folio() can be used for that, or just grabbing a folio reference (not so nice, but works; GUP uses that, for example, to keep the huge zero folio alive).

Quoted 1 line
Quoted 6 lines
Quoted 2 lines

No in-tree user needs either mapping, so reject both with VM_FAULT_SIGBUS rather than risk making either shared zero page writable.

That is the better reason. You could also just say that handling shared zeropages correctly is more involved (see vm_mixed_ok()), and as there are no users, we can just disallow it instead of trying to fix it.

Ah I see, thanks! obviously, I hadn't fully understood that ...

How about this instead?

"
Handling the huge/shared zeropage correctly in vmf_insert_pfn_pmd() and vmf_insert_pfn_prot() is more involved. We would need to check whether the VMA allows it and keep the mapping read-only, similar to the checks in vm_mixed_ok().

Quoted 3 lines

No in-tree user needs that support, so reject these mappings with VM_FAULT_SIGBUS rather than complicate the code for now. "

Works for me.

Signature · 3 lines
Cheers,

David