mm/huge_memory: disallow raw PMD mappings of the huge zero page
Subsystems (3)
Lance Yang
reviewerPATCH 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
@@ -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)
Kiryl Shutsemau
reviewerQuoted 10 lines · Lance Yang
vm_mixed_zeropage_allowed()validates zeropage insertion intoVM_MIXEDMAPVMAs. No in-tree user needs to insert the huge zero page throughvmf_insert_pfn_pmd().Return
VM_FAULT_SIGBUSifvmf_insert_pfn_pmd()is asked to map it.Suggested-by Kiryl Shutsemau <[email protected]>Suggested-by David Hildenbrand <[email protected]>Signed-off-by Lance Yang <[email protected]>
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
David Hildenbrand (Arm)
maintainerQuoted 7 lines · Kiryl Shutsemau
Quoted 6 lines · Lance Yang
vm_mixed_zeropage_allowed()validates zeropage insertion intoVM_MIXEDMAPVMAs. No in-tree user needs to insert the huge zero page throughvmf_insert_pfn_pmd().Return
VM_FAULT_SIGBUSifvmf_insert_pfn_pmd()is asked to map it.
Quoted 7 lines
Quoted 4 lines
Suggested-by Kiryl Shutsemau <[email protected]>Suggested-by David Hildenbrand <[email protected]>Signed-off-by Lance Yang <[email protected]>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
David Hildenbrand (Arm)
maintainerQuoted 3 lines · Lance Yang
vm_mixed_zeropage_allowed()validates zeropage insertion intoVM_MIXEDMAPVMAs. No in-tree user needs to insert the huge zero page throughvmf_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_SIGBUSifvmf_insert_pfn_pmd()is asked to map it.Suggested-by Kiryl Shutsemau <[email protected]>Suggested-by David Hildenbrand <[email protected]>Signed-off-by Lance Yang <[email protected]>--- mm/huge_memory.c | 3 +++ 1 file changed, 3 insertions(+)mm/huge_memory.c +3
-0@@ -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
Kiryl Shutsemau
reviewerQuoted 13 lines · David Hildenbrand (Arm)
Quoted 12 lines · Kiryl Shutsemau
Quoted 10 lines · Lance Yang
vm_mixed_zeropage_allowed()validates zeropage insertion intoVM_MIXEDMAPVMAs. No in-tree user needs to insert the huge zero page throughvmf_insert_pfn_pmd().Return
VM_FAULT_SIGBUSifvmf_insert_pfn_pmd()is asked to map it.Suggested-by Kiryl Shutsemau <[email protected]>Suggested-by David Hildenbrand <[email protected]>Signed-off-by Lance Yang <[email protected]>
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
Lance Yang
reviewerQuoted 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 intoVM_MIXEDMAPVMAs. No in-tree user needs to insert the huge zero page throughvmf_insert_pfn_pmd().Return
VM_FAULT_SIGBUSifvmf_insert_pfn_pmd()is asked to map it.Suggested-by Kiryl Shutsemau <[email protected]>Suggested-by David Hildenbrand <[email protected]>Signed-off-by Lance Yang <[email protected]>
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
@@ -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
@@ -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
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 intoVM_MIXEDMAPVMAs. No in-tree user needs to insert the huge zero page throughvmf_insert_pfn_pmd().Return
VM_FAULT_SIGBUSifvmf_insert_pfn_pmd()is asked to map it.Suggested-by Kiryl Shutsemau <[email protected]>Suggested-by David Hildenbrand <[email protected]>Signed-off-by Lance Yang <[email protected]>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 ofhuge_zero_pfncan therefore outlive the folio.
Quoted 7 lines
No in-tree user needs either mapping, so reject both with
VM_FAULT_SIGBUSrather than risk making either shared zero page writable.Suggested-by Kiryl Shutsemau <[email protected]>Suggested-by David Hildenbrand <[email protected]>Signed-off-by Lance Yang <[email protected]>
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
David Hildenbrand (Arm)
maintainerQuoted 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
David Hildenbrand (Arm)
maintainerQuoted 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 ofhuge_zero_pfncan 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_SIGBUSrather 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
Suggested-by Kiryl Shutsemau <[email protected]>Suggested-by David Hildenbrand <[email protected]>Signed-off-by Lance Yang <[email protected]>---
Signature · 3 lines
Cheers, David
Lance Yang
reviewerQuoted 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 ofhuge_zero_pfncan 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_SIGBUSrather 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
Suggested-by Kiryl Shutsemau <[email protected]>Suggested-by David Hildenbrand <[email protected]>Signed-off-by Lance Yang <[email protected]>---
Signature · 3 lines
Cheers, David
David Hildenbrand (Arm)
maintainerQuoted 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 ofhuge_zero_pfncan 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_SIGBUSrather 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 invmf_insert_pfn_pmd()andvmf_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 invm_mixed_ok().
Quoted 3 lines
No in-tree user needs that support, so reject these mappings with
VM_FAULT_SIGBUSrather than complicate the code for now. "
Works for me.
Signature · 3 lines
Cheers, David