Skip to contentSkip to conversation

remove follow_pfn v2

PATCH In review Christoph Hellwig 3 participants 6 messages Mar 28, 2024

Subsystems (6)
Memory management x86 MM core x86 MM +2 more subsystems
lore
Share & tools
lore
#0 lore raw

Hi all,

this series open codes follow_pfn in the only remaining caller, although the code there remains questionable. It then also moves follow_phys into the only user and simplifies it a bit.

Changes since v1:
- spelling fixes
- keep an empty line

Diffstat:

 arch/x86/mm/pat/memtype.c |   23 +++++++++++++++-
 drivers/virt/acrn/mm.c    |   10 +++++--
 include/linux/mm.h        |    4 --
 mm/memory.c               |   64 +---------------------------------------------
 mm/nommu.c                |   21 ---------------
 5 files changed, 31 insertions(+), 91 deletions(-)
#1 lore raw

PATCH 1/3 virt: acrn: stop using follow_pfn

Switch from follow_pfn to follow_pte so that we can get rid of follow_pfn. Note that this doesn't fix any of the pre-existing raciness and lack of permission checking in the code.

---
 drivers/virt/acrn/mm.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/virt/acrn/mm.c +8 -2
index fa5d9ca6be5706..69c3f619f88196 100644
@@ -171,18 +171,24 @@ int acrn_vm_ram_map(struct acrn_vm *vm, struct acrn_vm_memmap *memmap)
 	mmap_read_lock(current->mm);
 	vma = vma_lookup(current->mm, memmap->vma_base);
 	if (vma && ((vma->vm_flags & VM_PFNMAP) != 0)) {
+		spinlock_t *ptl;
+		pte_t *ptep;
+
 		if ((memmap->vma_base + memmap->len) > vma->vm_end) {
 			mmap_read_unlock(current->mm);
 			return -EINVAL;
 		}
 
-		ret = follow_pfn(vma, memmap->vma_base, &pfn);
-		mmap_read_unlock(current->mm);
+		ret = follow_pte(vma->vm_mm, memmap->vma_base, &ptep, &ptl);
 		if (ret < 0) {
+			mmap_read_unlock(current->mm);
 			dev_dbg(acrn_dev.this_device,
 				"Failed to lookup PFN at VMA:%pK.\n", (void *)memmap->vma_base);
 			return ret;
 		}
+		pfn = pte_pfn(ptep_get(ptep));
+		pte_unmap_unlock(ptep, ptl);
+		mmap_read_unlock(current->mm);
 
 		return acrn_mm_region_add(vm, memmap->user_vm_pa,
 			 PFN_PHYS(pfn), memmap->len,
Signature · 1 line
2.39.2
#2 lore raw

PATCH 2/3 mm: remove follow_pfn

In reply to Christoph Hellwig

Remove follow_pfn now that the last user is gone.

---
 include/linux/mm.h |  2 --
 mm/memory.c        | 36 ++----------------------------------
 mm/nommu.c         | 21 ---------------------
 3 files changed, 2 insertions(+), 57 deletions(-)
include/linux/mm.h +0 -2
index 0436b919f1c7fc..9cd2c69f913601 100644
@@ -2393,8 +2393,6 @@ int
 copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma);
 int follow_pte(struct mm_struct *mm, unsigned long address,
 	       pte_t **ptepp, spinlock_t **ptlp);
-int follow_pfn(struct vm_area_struct *vma, unsigned long address,
-	unsigned long *pfn);
 int follow_phys(struct vm_area_struct *vma, unsigned long address,
 		unsigned int flags, unsigned long *prot, resource_size_t *phys);
 int generic_access_phys(struct vm_area_struct *vma, unsigned long addr,
mm/memory.c +2 -34
index 904f70b994985a..33559720f0aab9 100644
@@ -5882,8 +5882,8 @@ int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
  * Only IO mappings and raw PFN mappings are allowed.  The mmap semaphore
  * should be taken for read.
  *
- * KVM uses this function.  While it is arguably less bad than ``follow_pfn``,
- * it is not a good general-purpose API.
+ * KVM uses this function.  While it is arguably less bad than the historic
+ * ``follow_pfn``, it is not a good general-purpose API.
  *
  * Return: zero on success, -ve otherwise.
  */
@@ -5925,38 +5925,6 @@ int follow_pte(struct mm_struct *mm, unsigned long address,
 }
 EXPORT_SYMBOL_GPL(follow_pte);
 
-/**
- * follow_pfn - look up PFN at a user virtual address
- * @vma: memory mapping
- * @address: user virtual address
- * @pfn: location to store found PFN
- *
- * Only IO mappings and raw PFN mappings are allowed.
- *
- * This function does not allow the caller to read the permissions
- * of the PTE.  Do not use it.
- *
- * Return: zero and the pfn at @pfn on success, -ve otherwise.
- */
-int follow_pfn(struct vm_area_struct *vma, unsigned long address,
-	unsigned long *pfn)
-{
-	int ret = -EINVAL;
-	spinlock_t *ptl;
-	pte_t *ptep;
-
-	if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
-		return ret;
-
-	ret = follow_pte(vma->vm_mm, address, &ptep, &ptl);
-	if (ret)
-		return ret;
-	*pfn = pte_pfn(ptep_get(ptep));
-	pte_unmap_unlock(ptep, ptl);
-	return 0;
-}
-EXPORT_SYMBOL(follow_pfn);
-
 #ifdef CONFIG_HAVE_IOREMAP_PROT
 int follow_phys(struct vm_area_struct *vma,
 		unsigned long address, unsigned int flags,
mm/nommu.c +0 -21
index 5ec8f44e7ce976..38d3ecc30c3ba9 100644
@@ -110,27 +110,6 @@ unsigned int kobjsize(const void *objp)
 	return page_size(page);
 }
 
-/**
- * follow_pfn - look up PFN at a user virtual address
- * @vma: memory mapping
- * @address: user virtual address
- * @pfn: location to store found PFN
- *
- * Only IO mappings and raw PFN mappings are allowed.
- *
- * Returns zero and the pfn at @pfn on success, -ve otherwise.
- */
-int follow_pfn(struct vm_area_struct *vma, unsigned long address,
-	unsigned long *pfn)
-{
-	if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
-		return -EINVAL;
-
-	*pfn = address >> PAGE_SHIFT;
-	return 0;
-}
-EXPORT_SYMBOL(follow_pfn);
-
 void vfree(const void *addr)
 {
 	kfree(addr);
Signature · 1 line
2.39.2
#3 lore raw

PATCH 3/3 mm: move follow_phys to arch/x86/mm/pat/memtype.c

In reply to Christoph Hellwig

follow_phys is only used by two callers in arch/x86/mm/pat/memtype.c. Move it there and hardcode the two arguments that get the same values passed by both callers.

---
 arch/x86/mm/pat/memtype.c | 23 +++++++++++++++++++++--
 include/linux/mm.h        |  2 --
 mm/memory.c               | 28 ----------------------------
 3 files changed, 21 insertions(+), 32 deletions(-)
arch/x86/mm/pat/memtype.c +21 -2
index 0d72183b5dd028..c64f07b0fc2099 100644
@@ -947,6 +947,25 @@ static void free_pfn_range(u64 paddr, unsigned long size)
 		memtype_free(paddr, paddr + size);
 }
 
+static int follow_phys(struct vm_area_struct *vma, unsigned long *prot,
+		resource_size_t *phys)
+{
+	pte_t *ptep, pte;
+	spinlock_t *ptl;
+
+	if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
+		return -EINVAL;
+
+	if (follow_pte(vma->vm_mm, vma->vm_start, &ptep, &ptl))
+		return -EINVAL;
+
+	pte = ptep_get(ptep);
+	*prot = pgprot_val(pte_pgprot(pte));
+	*phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT;
+	pte_unmap_unlock(ptep, ptl);
+	return 0;
+}
+
 /*
  * track_pfn_copy is called when vma that is covering the pfnmap gets
  * copied through copy_page_range().
@@ -966,7 +985,7 @@ int track_pfn_copy(struct vm_area_struct *vma)
 		 * reserve the whole chunk covered by vma. We need the
 		 * starting address and protection from pte.
 		 */
-		if (follow_phys(vma, vma->vm_start, 0, &prot, &paddr)) {
+		if (follow_phys(vma, &prot, &paddr)) {
 			WARN_ON_ONCE(1);
 			return -EINVAL;
 		}
@@ -1053,7 +1072,7 @@ void untrack_pfn(struct vm_area_struct *vma, unsigned long pfn,
 	/* free the chunk starting from pfn or the whole chunk */
 	paddr = (resource_size_t)pfn << PAGE_SHIFT;
 	if (!paddr && !size) {
-		if (follow_phys(vma, vma->vm_start, 0, &prot, &paddr)) {
+		if (follow_phys(vma, &prot, &paddr)) {
 			WARN_ON_ONCE(1);
 			return;
 		}
include/linux/mm.h +0 -2
index 9cd2c69f913601..51cfc8267da755 100644
@@ -2393,8 +2393,6 @@ int
 copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma);
 int follow_pte(struct mm_struct *mm, unsigned long address,
 	       pte_t **ptepp, spinlock_t **ptlp);
-int follow_phys(struct vm_area_struct *vma, unsigned long address,
-		unsigned int flags, unsigned long *prot, resource_size_t *phys);
 int generic_access_phys(struct vm_area_struct *vma, unsigned long addr,
 			void *buf, int len, int write);
 
mm/memory.c +0 -28
index 33559720f0aab9..86c9eb92b5e03c 100644
@@ -5926,34 +5926,6 @@ int follow_pte(struct mm_struct *mm, unsigned long address,
 EXPORT_SYMBOL_GPL(follow_pte);
 
 #ifdef CONFIG_HAVE_IOREMAP_PROT
-int follow_phys(struct vm_area_struct *vma,
-		unsigned long address, unsigned int flags,
-		unsigned long *prot, resource_size_t *phys)
-{
-	int ret = -EINVAL;
-	pte_t *ptep, pte;
-	spinlock_t *ptl;
-
-	if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
-		goto out;
-
-	if (follow_pte(vma->vm_mm, address, &ptep, &ptl))
-		goto out;
-	pte = ptep_get(ptep);
-
-	if ((flags & FOLL_WRITE) && !pte_write(pte))
-		goto unlock;
-
-	*prot = pgprot_val(pte_pgprot(pte));
-	*phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT;
-
-	ret = 0;
-unlock:
-	pte_unmap_unlock(ptep, ptl);
-out:
-	return ret;
-}
-
 /**
  * generic_access_phys - generic implementation for iomem mmap access
  * @vma: the vma to access
Signature · 1 line
2.39.2
Reviewed-by on v1 3/3
#4 lore raw

Re: [PATCH 3/3] mm: move follow_phys to arch/x86/mm/pat/memtype.c

Quoted 16 lines · Christoph Hellwig

follow_phys is only used by two callers in arch/x86/mm/pat/memtype.c. Move it there and hardcode the two arguments that get the same values passed by both callers.

---
  arch/x86/mm/pat/memtype.c | 23 +++++++++++++++++++++--
  include/linux/mm.h        |  2 --
  mm/memory.c               | 28 ----------------------------
  3 files changed, 21 insertions(+), 32 deletions(-)
arch/x86/mm/pat/memtype.c +0 -0
index 0d72183b5dd028..c64f07b0fc2099 100644

arch/x86/mm/pat/memtype.c static void free_pfn_range(u64 paddr, unsigned long size) · line 952

Quoted 6 lines
arch/x86/mm/pat/memtype.c +3 -0
@@ -947,6 +947,25 @@ static void free_pfn_range(u64 paddr, unsigned long size)
  		memtype_free(paddr, paddr + size);
  }
 
+static int follow_phys(struct vm_area_struct *vma, unsigned long *prot,
+		resource_size_t *phys)
+{

With an implicit lookup address, the function name is a bit unfortunate.

"lookup_vma_pat_info()" ... not sure.

Signature · 3 lines
Cheers,

David / dhildenb
#5 lore raw

Re: [PATCH 3/3] mm: move follow_phys to arch/x86/mm/pat/memtype.c

In reply to Christoph Hellwig

Hi Christoph,

Quoted 25 lines · Christoph Hellwig

follow_phys is only used by two callers in arch/x86/mm/pat/memtype.c. Move it there and hardcode the two arguments that get the same values passed by both callers.

---
 arch/x86/mm/pat/memtype.c | 23 +++++++++++++++++++++--
 include/linux/mm.h        |  2 --
 mm/memory.c               | 28 ----------------------------
 3 files changed, 21 insertions(+), 32 deletions(-)
arch/x86/mm/pat/memtype.c +6 -0
index 0d72183b5dd028..c64f07b0fc2099 100644
@@ -947,6 +947,25 @@ static void free_pfn_range(u64 paddr, unsigned long size)
 		memtype_free(paddr, paddr + size);
 }
 
+static int follow_phys(struct vm_area_struct *vma, unsigned long *prot,
+		resource_size_t *phys)
+{
+	pte_t *ptep, pte;
+	spinlock_t *ptl;
+

arch/x86/mm/pat/memtype.c static void free_pfn_range(u64 paddr, unsigned long size) · line 965

Quoted 10 lines
arch/x86/mm/pat/memtype.c +10 -0
+	if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
+		return -EINVAL;
+
+	if (follow_pte(vma->vm_mm, vma->vm_start, &ptep, &ptl))
+		return -EINVAL;
+
+	pte = ptep_get(ptep);
+	*prot = pgprot_val(pte_pgprot(pte));
+	*phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT;
+	pte_unmap_unlock(ptep, ptl);

memtype.c needs to include highmem.h for highmem-internal.h, otherwise I get a build failure (ARCH=i386 allmodconfig):

In file included from include/linux/mm.h:30,

                   from include/linux/memblock.h:12,
                   from arch/x86/mm/pat/memtype.c:35:
  arch/x86/mm/pat/memtype.c: In function 'follow_phys':
  include/linux/pgtable.h:105:9: error: implicit declaration of function 'kunmap_local' [-Werror=implicit-function-declaration]
    105 |         kunmap_local((pte));    \
        |         ^~~~~~~~~~~~
  include/linux/mm.h:3014:9: note: in expansion of macro 'pte_unmap'
   3014 |         pte_unmap(pte);                                 \
        |         ^~~~~~~~~
  arch/x86/mm/pat/memtype.c:965:9: note: in expansion of macro 'pte_unmap_unlock'
    965 |         pte_unmap_unlock(ptep, ptl);
        |         ^~~~~~~~~~~~~~~~

cc1: all warnings being treated as errors

Cheers,
Nathan

arch/x86/mm/pat/memtype.c +1 -0
index c64f07b0fc20..143d1e3d3fd2 100644
@@ -39,6 +39,7 @@
 #include <linux/pfn_t.h>
 #include <linux/slab.h>
 #include <linux/mm.h>
+#include <linux/highmem.h>
 #include <linux/fs.h>
 #include <linux/rbtree.h>