2023-03-06 08:43:49

by mawupeng

[permalink] [raw]
Subject: [PATCH -next 0/2] Minor cleanup in PAT

From: Ma Wupeng <[email protected]>

Patch #1 move follow_phys to memtype.c since only pat use this.
Patch #2 drop the unnecessary WARN_ON_ONCE if follow_phys fails.

Ma Wupeng (2):
x86/mm/pat: Move follow_phys to pat-related file
x86/mm/pat: Drop the unnecessary WARN_ON_ONCE if follow_phys fails

arch/x86/mm/pat/memtype.c | 32 ++++++++++++++++++++++++++------
include/linux/mm.h | 2 --
mm/memory.c | 28 ----------------------------
3 files changed, 26 insertions(+), 36 deletions(-)

--
2.25.1



2023-03-06 08:43:53

by mawupeng

[permalink] [raw]
Subject: [PATCH -next 1/2] x86/mm/pat: Move follow_phys to pat-related file

From: Ma Wupeng <[email protected]>

Since only PAT in x86 use follow_phys(), move this to from memory.c to
memtype.c and make it static. Argument flags is always zero in caller
untrack_pfn() and track_pfn_copy(). let's drop it.

Since config HAVE_IOREMAP_PROT is selected by x86, drop this config macro.

Signed-off-by: Ma Wupeng <[email protected]>
---
arch/x86/mm/pat/memtype.c | 27 +++++++++++++++++++++++++--
include/linux/mm.h | 2 --
mm/memory.c | 28 ----------------------------
3 files changed, 25 insertions(+), 32 deletions(-)

diff --git a/arch/x86/mm/pat/memtype.c b/arch/x86/mm/pat/memtype.c
index de10800cd4dd..c138ea0b0e6e 100644
--- a/arch/x86/mm/pat/memtype.c
+++ b/arch/x86/mm/pat/memtype.c
@@ -950,6 +950,29 @@ 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 address,
+ 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;
+
+ *prot = pgprot_val(pte_pgprot(pte));
+ *phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT;
+
+ ret = 0;
+ pte_unmap_unlock(ptep, ptl);
+out:
+ return ret;
+}
+
/*
* track_pfn_copy is called when vma that is covering the pfnmap gets
* copied through copy_page_range().
@@ -969,7 +992,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, vma->vm_start, &prot, &paddr)) {
WARN_ON_ONCE(1);
return -EINVAL;
}
@@ -1056,7 +1079,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, vma->vm_start, &prot, &paddr)) {
WARN_ON_ONCE(1);
return;
}
diff --git a/include/linux/mm.h b/include/linux/mm.h
index 5f16263d176d..226f2f7cefd7 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -2311,8 +2311,6 @@ 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,
void *buf, int len, int write);

diff --git a/mm/memory.c b/mm/memory.c
index 0adf23ea5416..93ad6b45f426 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -5454,34 +5454,6 @@ int follow_pfn(struct vm_area_struct *vma, unsigned long address,
EXPORT_SYMBOL(follow_pfn);

#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;
-
- 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
--
2.25.1


2023-03-06 08:43:57

by mawupeng

[permalink] [raw]
Subject: [PATCH -next 2/2] x86/mm/pat: Drop the unnecessary WARN_ON_ONCE if follow_phys fails

From: Ma Wupeng <[email protected]>

Since there is no obvious reason to keep this WARN_ON_ONCE if follow_phys
fails in track_pfn_copy/untrack_pfn, Drop it.

Signed-off-by: Ma Wupeng <[email protected]>
---
arch/x86/mm/pat/memtype.c | 9 +++------
1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/arch/x86/mm/pat/memtype.c b/arch/x86/mm/pat/memtype.c
index c138ea0b0e6e..8c7f9e6b76e6 100644
--- a/arch/x86/mm/pat/memtype.c
+++ b/arch/x86/mm/pat/memtype.c
@@ -992,10 +992,9 @@ 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, &prot, &paddr)) {
- WARN_ON_ONCE(1);
+ if (follow_phys(vma, vma->vm_start, &prot, &paddr))
return -EINVAL;
- }
+
pgprot = __pgprot(prot);
return reserve_pfn_range(paddr, vma_size, &pgprot, 1);
}
@@ -1079,10 +1078,8 @@ 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, &prot, &paddr)) {
- WARN_ON_ONCE(1);
+ if (follow_phys(vma, vma->vm_start, &prot, &paddr))
return;
- }

size = vma->vm_end - vma->vm_start;
}
--
2.25.1


2023-03-06 09:04:43

by Borislav Petkov

[permalink] [raw]
Subject: Re: [PATCH -next 1/2] x86/mm/pat: Move follow_phys to pat-related file

On Mon, Mar 06, 2023 at 04:43:15PM +0800, Wupeng Ma wrote:
> From: Ma Wupeng <[email protected]>
>
> Since only PAT in x86 use follow_phys(), move this to from memory.c to
> memtype.c and make it static. Argument flags is always zero in caller
> untrack_pfn() and track_pfn_copy(). let's drop it.
>
> Since config HAVE_IOREMAP_PROT is selected by x86, drop this config macro.

* first patch: *only* code movement, no other changes
* second patch: do semantic changes and explain *why* you do them
* third patch:...
*...

In that order please. Otherwise review is unnecessarily complicated.

Also, do not talk about *what* the patch is doing in the commit message
- that should be obvious from the diff itself. Rather, concentrate on
the *why* it needs to be done.

Thx.

--
Regards/Gruss,
Boris.

https://people.kernel.org/tglx/notes-about-netiquette

2023-03-06 09:18:15

by mawupeng

[permalink] [raw]
Subject: Re: [PATCH -next 1/2] x86/mm/pat: Move follow_phys to pat-related file



On 2023/3/6 17:04, Borislav Petkov wrote:
> On Mon, Mar 06, 2023 at 04:43:15PM +0800, Wupeng Ma wrote:
>> From: Ma Wupeng <[email protected]>
>>
>> Since only PAT in x86 use follow_phys(), move this to from memory.c to
>> memtype.c and make it static. Argument flags is always zero in caller
>> untrack_pfn() and track_pfn_copy(). let's drop it.
>>
>> Since config HAVE_IOREMAP_PROT is selected by x86, drop this config macro.
>
> * first patch: *only* code movement, no other changes
> * second patch: do semantic changes and explain *why* you do them
> * third patch:...
> *...
>
> In that order please. Otherwise review is unnecessarily complicated.
>
> Also, do not talk about *what* the patch is doing in the commit message
> - that should be obvious from the diff itself. Rather, concentrate on
> the *why* it needs to be done.

Hi Borislav.

Thanks for your advise.

I will do what you advised and send a v2 later.

>
> Thx.
>