Skip to content

Commit a42150f

Browse files
bonzinigregkh
authored andcommitted
mm: provide a saner PTE walking API for modules
commit 9fd6dad upstream. Currently, the follow_pfn function is exported for modules but follow_pte is not. However, follow_pfn is very easy to misuse, because it does not provide protections (so most of its callers assume the page is writable!) and because it returns after having already unlocked the page table lock. Provide instead a simplified version of follow_pte that does not have the pmdpp and range arguments. The older version survives as follow_invalidate_pte() for use by fs/dax.c. Reviewed-by: Jason Gunthorpe <jgg@nvidia.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 83d42c2 commit a42150f

4 files changed

Lines changed: 45 additions & 11 deletions

File tree

‎fs/dax.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -810,11 +810,12 @@ static void dax_entry_mkclean(struct address_space *mapping, pgoff_t index,
810810
address = pgoff_address(index, vma);
811811

812812
/*
813-
* Note because we provide range to follow_pte it will call
813+
* follow_invalidate_pte() will use the range to call
814814
* mmu_notifier_invalidate_range_start() on our behalf before
815815
* taking any lock.
816816
*/
817-
if (follow_pte(vma->vm_mm, address, &range, &ptep, &pmdp, &ptl))
817+
if (follow_invalidate_pte(vma->vm_mm, address, &range, &ptep,
818+
&pmdp, &ptl))
818819
continue;
819820

820821
/*

‎include/linux/mm.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,9 +1655,11 @@ void free_pgd_range(struct mmu_gather *tlb, unsigned long addr,
16551655
unsigned long end, unsigned long floor, unsigned long ceiling);
16561656
int
16571657
copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma);
1658+
int follow_invalidate_pte(struct mm_struct *mm, unsigned long address,
1659+
struct mmu_notifier_range *range, pte_t **ptepp,
1660+
pmd_t **pmdpp, spinlock_t **ptlp);
16581661
int follow_pte(struct mm_struct *mm, unsigned long address,
1659-
struct mmu_notifier_range *range, pte_t **ptepp, pmd_t **pmdpp,
1660-
spinlock_t **ptlp);
1662+
pte_t **ptepp, spinlock_t **ptlp);
16611663
int follow_pfn(struct vm_area_struct *vma, unsigned long address,
16621664
unsigned long *pfn);
16631665
int follow_phys(struct vm_area_struct *vma, unsigned long address,

‎mm/memory.c‎

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4707,9 +4707,9 @@ int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
47074707
}
47084708
#endif /* __PAGETABLE_PMD_FOLDED */
47094709

4710-
int follow_pte(struct mm_struct *mm, unsigned long address,
4711-
struct mmu_notifier_range *range, pte_t **ptepp, pmd_t **pmdpp,
4712-
spinlock_t **ptlp)
4710+
int follow_invalidate_pte(struct mm_struct *mm, unsigned long address,
4711+
struct mmu_notifier_range *range, pte_t **ptepp,
4712+
pmd_t **pmdpp, spinlock_t **ptlp)
47134713
{
47144714
pgd_t *pgd;
47154715
p4d_t *p4d;
@@ -4774,6 +4774,34 @@ int follow_pte(struct mm_struct *mm, unsigned long address,
47744774
return -EINVAL;
47754775
}
47764776

4777+
/**
4778+
* follow_pte - look up PTE at a user virtual address
4779+
* @mm: the mm_struct of the target address space
4780+
* @address: user virtual address
4781+
* @ptepp: location to store found PTE
4782+
* @ptlp: location to store the lock for the PTE
4783+
*
4784+
* On a successful return, the pointer to the PTE is stored in @ptepp;
4785+
* the corresponding lock is taken and its location is stored in @ptlp.
4786+
* The contents of the PTE are only stable until @ptlp is released;
4787+
* any further use, if any, must be protected against invalidation
4788+
* with MMU notifiers.
4789+
*
4790+
* Only IO mappings and raw PFN mappings are allowed. The mmap semaphore
4791+
* should be taken for read.
4792+
*
4793+
* KVM uses this function. While it is arguably less bad than ``follow_pfn``,
4794+
* it is not a good general-purpose API.
4795+
*
4796+
* Return: zero on success, -ve otherwise.
4797+
*/
4798+
int follow_pte(struct mm_struct *mm, unsigned long address,
4799+
pte_t **ptepp, spinlock_t **ptlp)
4800+
{
4801+
return follow_invalidate_pte(mm, address, NULL, ptepp, NULL, ptlp);
4802+
}
4803+
EXPORT_SYMBOL_GPL(follow_pte);
4804+
47774805
/**
47784806
* follow_pfn - look up PFN at a user virtual address
47794807
* @vma: memory mapping
@@ -4782,6 +4810,9 @@ int follow_pte(struct mm_struct *mm, unsigned long address,
47824810
*
47834811
* Only IO mappings and raw PFN mappings are allowed.
47844812
*
4813+
* This function does not allow the caller to read the permissions
4814+
* of the PTE. Do not use it.
4815+
*
47854816
* Return: zero and the pfn at @pfn on success, -ve otherwise.
47864817
*/
47874818
int follow_pfn(struct vm_area_struct *vma, unsigned long address,
@@ -4794,7 +4825,7 @@ int follow_pfn(struct vm_area_struct *vma, unsigned long address,
47944825
if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
47954826
return ret;
47964827

4797-
ret = follow_pte(vma->vm_mm, address, NULL, &ptep, NULL, &ptl);
4828+
ret = follow_pte(vma->vm_mm, address, &ptep, &ptl);
47984829
if (ret)
47994830
return ret;
48004831
*pfn = pte_pfn(*ptep);
@@ -4815,7 +4846,7 @@ int follow_phys(struct vm_area_struct *vma,
48154846
if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
48164847
goto out;
48174848

4818-
if (follow_pte(vma->vm_mm, address, NULL, &ptep, NULL, &ptl))
4849+
if (follow_pte(vma->vm_mm, address, &ptep, &ptl))
48194850
goto out;
48204851
pte = *ptep;
48214852

‎virt/kvm/kvm_main.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1893,7 +1893,7 @@ static int hva_to_pfn_remapped(struct vm_area_struct *vma,
18931893
spinlock_t *ptl;
18941894
int r;
18951895

1896-
r = follow_pte(vma->vm_mm, addr, NULL, &ptep, NULL, &ptl);
1896+
r = follow_pte(vma->vm_mm, addr, &ptep, &ptl);
18971897
if (r) {
18981898
/*
18991899
* get_user_pages fails for VM_IO and VM_PFNMAP vmas and does
@@ -1908,7 +1908,7 @@ static int hva_to_pfn_remapped(struct vm_area_struct *vma,
19081908
if (r)
19091909
return r;
19101910

1911-
r = follow_pte(vma->vm_mm, addr, NULL, &ptep, NULL, &ptl);
1911+
r = follow_pte(vma->vm_mm, addr, &ptep, &ptl);
19121912
if (r)
19131913
return r;
19141914
}

0 commit comments

Comments
 (0)