Skip to contentSkip to conversation

mm: disallow raw PFN mappings of huge/shared zeropage

PATCH Applied Lance Yang 4 participants 4 messages Sep 21

Subsystems (3)
lore
Share & tools
lore

Lance Yang

author reviewer
#0 lore raw

PATCH v2 1/1 mm: disallow raw PFN mappings of huge/shared zeropage

From: Lance Yang <[email protected]>

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.

---
v1 -> v2:
- Reject the shared zeropage in vmf_insert_pfn_prot() (per Kiryl) - thanks!
- Explain the VMA validation and read-only requirements (per David) - thanks!
- Pick up Kiryl's Reviewed-by tag.
- https://lore.kernel.org/all/[email protected]/

 mm/huge_memory.c | 3 +++
 mm/memory.c      | 3 +++
 2 files changed, 6 insertions(+)
mm/huge_memory.c +3 -0
index 8aa2daba3739..1d4fe4452c98 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);
mm/memory.c +3 -0
index 926276d41920..7d821b110df0 100644
@@ -2860,6 +2860,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;
 
Signature · 1 line
2.49.0
maintainer Acked-by on v2 1/1
#1 lore raw
Quoted 10 lines · Lance Yang

From: Lance Yang <[email protected]>

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.

Quoted 6 lines

---

Signature · 3 lines
Cheers,

David
reviewer Reviewed-by on v2 1/1
#2 lore raw
In reply to Lance Yang
Quoted 7 lines · Lance Yang

From: Lance Yang <[email protected]>

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 8 lines

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

Seems sensible to me!

Quoted 40 lines

---
v1 -> v2:
- Reject the shared zeropage in vmf_insert_pfn_prot() (per Kiryl) - thanks!
- Explain the VMA validation and read-only requirements (per David) - thanks!
- Pick up Kiryl's Reviewed-by tag.
- https://lore.kernel.org/all/[email protected]/

 mm/huge_memory.c | 3 +++
 mm/memory.c      | 3 +++
 2 files changed, 6 insertions(+)
mm/huge_memory.c +3 -0
index 8aa2daba3739..1d4fe4452c98 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);
mm/memory.c +3 -1
index 926276d41920..7d821b110df0 100644
@@ -2860,6 +2860,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;
 
--
Quoted 1 line

2.49.0

--
Cheers, Lorenzo

reviewer Acked-by on v2 1/1
#3 lore raw
In reply to Lance Yang
Quoted 22 lines · Lance Yang

From: Lance Yang <[email protected]>

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.

---
v1 -> v2:
- Reject the shared zeropage in vmf_insert_pfn_prot() (per Kiryl) - thanks!
- Explain the VMA validation and read-only requirements (per David) - thanks!
- Pick up Kiryl's Reviewed-by tag.
- https://lore.kernel.org/all/[email protected]/

Quoted 4 lines
 mm/huge_memory.c | 3 +++
 mm/memory.c      | 3 +++
 2 files changed, 6 insertions(+)

Makes sense to me.

Best Regards,
Yan, Zi