linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v4 0/3] Cleanup for PAT
@ 2024-02-20  3:48 Wupeng Ma
  2024-02-20  3:48 ` [PATCH v4 1/3] x86/mm/pat: Move follow_phys to pat-related file Wupeng Ma
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Wupeng Ma @ 2024-02-20  3:48 UTC (permalink / raw)
  To: akpm, dave.hansen, luto, tglx, peterz, hpa
  Cc: linux-kernel, x86, mawupeng1, bp, mingo, rdunlap, bhelgaas, linux-mm

From: Ma Wupeng <mawupeng1@huawei.com>

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

Changelog since v3:
- rebase to latest linux
- fix compile warnings

Changelog since v2:
- rebase to latest linux

Changelog since v1:
- split patch #1 into two patches based on Boris's advise

Ma Wupeng (3):
  x86/mm/pat: Move follow_phys to pat-related file
  x86/mm/pat: Cleanup unused parameter in follow_phys
  x86/mm/pat: Remove 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



^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v4 1/3] x86/mm/pat: Move follow_phys to pat-related file
  2024-02-20  3:48 [PATCH v4 0/3] Cleanup for PAT Wupeng Ma
@ 2024-02-20  3:48 ` Wupeng Ma
  2024-02-20  3:48 ` [PATCH v4 2/3] x86/mm/pat: Cleanup unused parameter in follow_phys Wupeng Ma
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 7+ messages in thread
From: Wupeng Ma @ 2024-02-20  3:48 UTC (permalink / raw)
  To: akpm, dave.hansen, luto, tglx, peterz, hpa
  Cc: linux-kernel, x86, mawupeng1, bp, mingo, rdunlap, bhelgaas, linux-mm

From: Ma Wupeng <mawupeng1@huawei.com>

Since only PAT in x86 use follow_phys(), move this to from memory.c to
memtype.c and make it static.

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

Signed-off-by: Ma Wupeng <mawupeng1@huawei.com>
---
 arch/x86/mm/pat/memtype.c | 28 ++++++++++++++++++++++++++++
 include/linux/mm.h        |  2 --
 mm/memory.c               | 28 ----------------------------
 3 files changed, 28 insertions(+), 30 deletions(-)

diff --git a/arch/x86/mm/pat/memtype.c b/arch/x86/mm/pat/memtype.c
index 0904d7e8e126..d8c37aaaf041 100644
--- a/arch/x86/mm/pat/memtype.c
+++ b/arch/x86/mm/pat/memtype.c
@@ -950,6 +950,34 @@ 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 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;
+}
+
 /*
  * track_pfn_copy is called when vma that is covering the pfnmap gets
  * copied through copy_page_range().
diff --git a/include/linux/mm.h b/include/linux/mm.h
index f5a97dec5169..75c683f2f5ae 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -2375,8 +2375,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 15f8b10ea17c..86ad7f1ca3af 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -5802,34 +5802,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_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
-- 
2.25.1



^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v4 2/3] x86/mm/pat: Cleanup unused parameter in follow_phys
  2024-02-20  3:48 [PATCH v4 0/3] Cleanup for PAT Wupeng Ma
  2024-02-20  3:48 ` [PATCH v4 1/3] x86/mm/pat: Move follow_phys to pat-related file Wupeng Ma
@ 2024-02-20  3:48 ` Wupeng Ma
  2024-02-20  3:48 ` [PATCH v4 3/3] x86/mm/pat: Remove WARN_ON_ONCE if follow_phys fails Wupeng Ma
  2024-02-20  8:37 ` [PATCH v4 0/3] Cleanup for PAT Xin Li
  3 siblings, 0 replies; 7+ messages in thread
From: Wupeng Ma @ 2024-02-20  3:48 UTC (permalink / raw)
  To: akpm, dave.hansen, luto, tglx, peterz, hpa
  Cc: linux-kernel, x86, mawupeng1, bp, mingo, rdunlap, bhelgaas, linux-mm

From: Ma Wupeng <mawupeng1@huawei.com>

Parameter flags is always zero in caller untrack_pfn() and
track_pfn_copy(). let's drop it.

Signed-off-by: Ma Wupeng <mawupeng1@huawei.com>
---
 arch/x86/mm/pat/memtype.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)

diff --git a/arch/x86/mm/pat/memtype.c b/arch/x86/mm/pat/memtype.c
index d8c37aaaf041..5b56f08f8ce6 100644
--- a/arch/x86/mm/pat/memtype.c
+++ b/arch/x86/mm/pat/memtype.c
@@ -950,8 +950,7 @@ 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 int flags,
+static int follow_phys(struct vm_area_struct *vma, unsigned long address,
 		unsigned long *prot, resource_size_t *phys)
 {
 	int ret = -EINVAL;
@@ -965,14 +964,10 @@ static int follow_phys(struct vm_area_struct *vma,
 		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;
@@ -997,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;
 		}
@@ -1084,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;
 		}
-- 
2.25.1



^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v4 3/3] x86/mm/pat: Remove WARN_ON_ONCE if follow_phys fails
  2024-02-20  3:48 [PATCH v4 0/3] Cleanup for PAT Wupeng Ma
  2024-02-20  3:48 ` [PATCH v4 1/3] x86/mm/pat: Move follow_phys to pat-related file Wupeng Ma
  2024-02-20  3:48 ` [PATCH v4 2/3] x86/mm/pat: Cleanup unused parameter in follow_phys Wupeng Ma
@ 2024-02-20  3:48 ` Wupeng Ma
  2024-02-20  8:37 ` [PATCH v4 0/3] Cleanup for PAT Xin Li
  3 siblings, 0 replies; 7+ messages in thread
From: Wupeng Ma @ 2024-02-20  3:48 UTC (permalink / raw)
  To: akpm, dave.hansen, luto, tglx, peterz, hpa
  Cc: linux-kernel, x86, mawupeng1, bp, mingo, rdunlap, bhelgaas, linux-mm

From: Ma Wupeng <mawupeng1@huawei.com>

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 <mawupeng1@huawei.com>
---
 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 5b56f08f8ce6..5b3a7a2b21e3 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



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v4 0/3] Cleanup for PAT
  2024-02-20  3:48 [PATCH v4 0/3] Cleanup for PAT Wupeng Ma
                   ` (2 preceding siblings ...)
  2024-02-20  3:48 ` [PATCH v4 3/3] x86/mm/pat: Remove WARN_ON_ONCE if follow_phys fails Wupeng Ma
@ 2024-02-20  8:37 ` Xin Li
  2024-02-20  9:06   ` mawupeng
  3 siblings, 1 reply; 7+ messages in thread
From: Xin Li @ 2024-02-20  8:37 UTC (permalink / raw)
  To: Wupeng Ma, akpm, dave.hansen, luto, tglx, peterz, hpa
  Cc: linux-kernel, x86, bp, mingo, rdunlap, bhelgaas, linux-mm

On 2/19/2024 7:48 PM, Wupeng Ma wrote:
> From: Ma Wupeng <mawupeng1@huawei.com>
> 

This patch set is all about follow_phys() cleanups, so "Cleanup for PAT"
seems too generic.

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

I'm more curious why follow_phys() ended up this way?

follow_phys() was introduced in commit 28b2ee20c7cba ("access_process_vm
device memory infrastructure") in 2008 for getting a physical page address
for a virtual address, and used in generic_access_phys(). And later it's
used in x86 PAT code.

Commit 03668a4debf4f ("mm: use generic follow_pte() in follow_phys()") made
follow_phys() more of a wrapper of follow_pte(), and commit 96667f8a4382d
("mm: Close race in generic_access_phys") replaced follow_phys() with
follow_pte() in generic_access_phys(). And the end result is that
follow_phys() is used in x86 PAT code only.

As follow_phys() in untrack_pfn() can be replaced with follow_pfn(), then
maybe we don't have to keep follow_phys(), and just use follow_pte() in
track_pfn_copy()?

Thanks!
     Xin

> 
> Changelog since v3:
> - rebase to latest linux
> - fix compile warnings
> 
> Changelog since v2:
> - rebase to latest linux
> 
> Changelog since v1:
> - split patch #1 into two patches based on Boris's advise
> 
> Ma Wupeng (3):
>    x86/mm/pat: Move follow_phys to pat-related file
>    x86/mm/pat: Cleanup unused parameter in follow_phys
>    x86/mm/pat: Remove 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(-)
> 




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v4 0/3] Cleanup for PAT
  2024-02-20  8:37 ` [PATCH v4 0/3] Cleanup for PAT Xin Li
@ 2024-02-20  9:06   ` mawupeng
  2024-02-20 18:46     ` Xin Li
  0 siblings, 1 reply; 7+ messages in thread
From: mawupeng @ 2024-02-20  9:06 UTC (permalink / raw)
  To: xin, akpm, dave.hansen, luto, tglx, peterz, hpa
  Cc: mawupeng1, linux-kernel, x86, bp, mingo, rdunlap, bhelgaas, linux-mm



On 2024/2/20 16:37, Xin Li wrote:
> On 2/19/2024 7:48 PM, Wupeng Ma wrote:
>> From: Ma Wupeng <mawupeng1@huawei.com>
>>
> 
> This patch set is all about follow_phys() cleanups, so "Cleanup for PAT"
> seems too generic.
> 
>> Patch #1 move follow_phys to memtype.c since only pat use this.
>> Patch #2 cleanup parameter in follow_phys.
>> Patch #3 drop the unnecessary WARN_ON_ONCE if follow_phys fails.
> 
> I'm more curious why follow_phys() ended up this way?
> 
> follow_phys() was introduced in commit 28b2ee20c7cba ("access_process_vm
> device memory infrastructure") in 2008 for getting a physical page address
> for a virtual address, and used in generic_access_phys(). And later it's
> used in x86 PAT code.
> 
> Commit 03668a4debf4f ("mm: use generic follow_pte() in follow_phys()") made
> follow_phys() more of a wrapper of follow_pte(), and commit 96667f8a4382d
> ("mm: Close race in generic_access_phys") replaced follow_phys() with
> follow_pte() in generic_access_phys(). And the end result is that
> follow_phys() is used in x86 PAT code only.

Thanks for the explanation. I have a better understanding of the history of
this function.

> 
> As follow_phys() in untrack_pfn() can be replaced with follow_pfn(), then

Yes, this can be replaced with follow_pfn().

> maybe we don't have to keep follow_phys(), and just use follow_pte() in
> track_pfn_copy()?

As follow_phys() will return unsigned long *prot which is need in track_pfn_copy(),
we need to do something with this.

Can we replace follow_pfn with follow_phys()?

Thanks!
Ma

> 
> Thanks!
>     Xin
> 
>>
>> Changelog since v3:
>> - rebase to latest linux
>> - fix compile warnings
>>
>> Changelog since v2:
>> - rebase to latest linux
>>
>> Changelog since v1:
>> - split patch #1 into two patches based on Boris's advise
>>
>> Ma Wupeng (3):
>>    x86/mm/pat: Move follow_phys to pat-related file
>>    x86/mm/pat: Cleanup unused parameter in follow_phys
>>    x86/mm/pat: Remove 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(-)
>>
> 
> 


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v4 0/3] Cleanup for PAT
  2024-02-20  9:06   ` mawupeng
@ 2024-02-20 18:46     ` Xin Li
  0 siblings, 0 replies; 7+ messages in thread
From: Xin Li @ 2024-02-20 18:46 UTC (permalink / raw)
  To: mawupeng, akpm, dave.hansen, luto, tglx, peterz, hpa
  Cc: linux-kernel, x86, bp, mingo, rdunlap, bhelgaas, linux-mm

On 2/20/2024 1:06 AM, mawupeng wrote:

> On 2024/2/20 16:37, Xin Li wrote:
>> On 2/19/2024 7:48 PM, Wupeng Ma wrote:
>> follow_phys() was introduced in commit 28b2ee20c7cba ("access_process_vm
>> device memory infrastructure") in 2008 for getting a physical page address
>> for a virtual address, and used in generic_access_phys(). And later it's
>> used in x86 PAT code.
>>
>> Commit 03668a4debf4f ("mm: use generic follow_pte() in follow_phys()") made
>> follow_phys() more of a wrapper of follow_pte(), and commit 96667f8a4382d
>> ("mm: Close race in generic_access_phys") replaced follow_phys() with
>> follow_pte() in generic_access_phys(). And the end result is that
>> follow_phys() is used in x86 PAT code only.
> 
> Thanks for the explanation. I have a better understanding of the history of
> this function.
> 

"git blame" tells the story.

>>
>> As follow_phys() in untrack_pfn() can be replaced with follow_pfn(), then
> 
> Yes, this can be replaced with follow_pfn().
> 
>> maybe we don't have to keep follow_phys(), and just use follow_pte() in
>> track_pfn_copy()?
> 
> As follow_phys() will return unsigned long *prot which is need in track_pfn_copy(),
> we need to do something with this.

Commit 96667f8a4382d did that already.

> Can we replace follow_pfn with follow_phys()?

Sorry, I don't get your point.

Thanks!
     Xin


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2024-02-20 18:47 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-20  3:48 [PATCH v4 0/3] Cleanup for PAT Wupeng Ma
2024-02-20  3:48 ` [PATCH v4 1/3] x86/mm/pat: Move follow_phys to pat-related file Wupeng Ma
2024-02-20  3:48 ` [PATCH v4 2/3] x86/mm/pat: Cleanup unused parameter in follow_phys Wupeng Ma
2024-02-20  3:48 ` [PATCH v4 3/3] x86/mm/pat: Remove WARN_ON_ONCE if follow_phys fails Wupeng Ma
2024-02-20  8:37 ` [PATCH v4 0/3] Cleanup for PAT Xin Li
2024-02-20  9:06   ` mawupeng
2024-02-20 18:46     ` Xin Li

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox