linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm: bring back update_mmu_cache() to finish_fault()
@ 2022-09-08 20:48 Sergei Antonov
  2022-09-08 22:24 ` Kirill A. Shutemov
  2022-09-09  5:34 ` Greg KH
  0 siblings, 2 replies; 4+ messages in thread
From: Sergei Antonov @ 2022-09-08 20:48 UTC (permalink / raw)
  To: linux-mm, akpm
  Cc: linux-arm-kernel, linux-arch, linux-kernel, stable,
	Sergei Antonov, Kirill A . Shutemov

Running this test program on ARMv4 a few times (sometimes just once)
reproduces the bug.

int main()
{
        unsigned i;
        char paragon[SIZE];
        void* ptr;

        memset(paragon, 0xAA, SIZE);
        ptr = mmap(NULL, SIZE, PROT_READ | PROT_WRITE,
                   MAP_ANON | MAP_SHARED, -1, 0);
        if (ptr == MAP_FAILED) return 1;
        printf("ptr = %p\n", ptr);
        for (i=0;i<10000;i++){
                memset(ptr, 0xAA, SIZE);
                if (memcmp(ptr, paragon, SIZE)) {
                        printf("Unexpected bytes on iteration %u!!!\n", i);
                        break;
                }
        }
        munmap(ptr, SIZE);
}

In the "ptr" buffer there appear runs of zero bytes which are aligned
by 16 and their lengths are multiple of 16.

Linux v5.11 does not have the bug, "git bisect" finds the first bad commit:
f9ce0be71d1f ("mm: Cleanup faultaround and finish_fault() codepaths")

Before the commit update_mmu_cache() was called during a call to
filemap_map_pages() as well as finish_fault(). After the commit
finish_fault() lacks it.

Bring back update_mmu_cache() to finish_fault() to fix the bug.
Also call update_mmu_tlb() only when returning VM_FAULT_NOPAGE to more
closely reproduce the code of alloc_set_pte() function that existed before
the commit.

On many platforms update_mmu_cache() is nop:
 x86, see arch/x86/include/asm/pgtable
 ARMv6+, see arch/arm/include/asm/tlbflush.h
So, it seems, few users ran into this bug.

Fixes: f9ce0be71d1f ("mm: Cleanup faultaround and finish_fault() codepaths")
Signed-off-by: Sergei Antonov <saproj@gmail.com>
Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
---
 mm/memory.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/mm/memory.c b/mm/memory.c
index 4ba73f5aa8bb..a78814413ac0 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -4386,14 +4386,20 @@ vm_fault_t finish_fault(struct vm_fault *vmf)
 
 	vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
 				      vmf->address, &vmf->ptl);
-	ret = 0;
+
 	/* Re-check under ptl */
-	if (likely(!vmf_pte_changed(vmf)))
+	if (likely(!vmf_pte_changed(vmf))) {
 		do_set_pte(vmf, page, vmf->address);
-	else
+
+		/* no need to invalidate: a not-present page won't be cached */
+		update_mmu_cache(vma, vmf->address, vmf->pte);
+
+		ret = 0;
+	} else {
+		update_mmu_tlb(vma, vmf->address, vmf->pte);
 		ret = VM_FAULT_NOPAGE;
+	}
 
-	update_mmu_tlb(vma, vmf->address, vmf->pte);
 	pte_unmap_unlock(vmf->pte, vmf->ptl);
 	return ret;
 }
-- 
2.34.1



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

* Re: [PATCH] mm: bring back update_mmu_cache() to finish_fault()
  2022-09-08 20:48 [PATCH] mm: bring back update_mmu_cache() to finish_fault() Sergei Antonov
@ 2022-09-08 22:24 ` Kirill A. Shutemov
  2022-09-09 10:10   ` Will Deacon
  2022-09-09  5:34 ` Greg KH
  1 sibling, 1 reply; 4+ messages in thread
From: Kirill A. Shutemov @ 2022-09-08 22:24 UTC (permalink / raw)
  To: Sergei Antonov
  Cc: linux-mm, akpm, linux-arm-kernel, linux-arch, linux-kernel,
	stable, Kirill A . Shutemov, Will Deacon

On Thu, Sep 08, 2022 at 11:48:09PM +0300, Sergei Antonov wrote:
> Running this test program on ARMv4 a few times (sometimes just once)
> reproduces the bug.
> 
> int main()
> {
>         unsigned i;
>         char paragon[SIZE];
>         void* ptr;
> 
>         memset(paragon, 0xAA, SIZE);
>         ptr = mmap(NULL, SIZE, PROT_READ | PROT_WRITE,
>                    MAP_ANON | MAP_SHARED, -1, 0);
>         if (ptr == MAP_FAILED) return 1;
>         printf("ptr = %p\n", ptr);
>         for (i=0;i<10000;i++){
>                 memset(ptr, 0xAA, SIZE);
>                 if (memcmp(ptr, paragon, SIZE)) {
>                         printf("Unexpected bytes on iteration %u!!!\n", i);
>                         break;
>                 }
>         }
>         munmap(ptr, SIZE);
> }
> 
> In the "ptr" buffer there appear runs of zero bytes which are aligned
> by 16 and their lengths are multiple of 16.
> 
> Linux v5.11 does not have the bug, "git bisect" finds the first bad commit:
> f9ce0be71d1f ("mm: Cleanup faultaround and finish_fault() codepaths")
> 
> Before the commit update_mmu_cache() was called during a call to
> filemap_map_pages() as well as finish_fault(). After the commit
> finish_fault() lacks it.
> 
> Bring back update_mmu_cache() to finish_fault() to fix the bug.
> Also call update_mmu_tlb() only when returning VM_FAULT_NOPAGE to more
> closely reproduce the code of alloc_set_pte() function that existed before
> the commit.
> 
> On many platforms update_mmu_cache() is nop:
>  x86, see arch/x86/include/asm/pgtable
>  ARMv6+, see arch/arm/include/asm/tlbflush.h
> So, it seems, few users ran into this bug.
> 
> Fixes: f9ce0be71d1f ("mm: Cleanup faultaround and finish_fault() codepaths")
> Signed-off-by: Sergei Antonov <saproj@gmail.com>
> Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>

+Will.

Seems I confused update_mmu_tlb() with update_mmu_cache() :/

Looks good to me:

Acked-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>

> ---
>  mm/memory.c | 14 ++++++++++----
>  1 file changed, 10 insertions(+), 4 deletions(-)
> 
> diff --git a/mm/memory.c b/mm/memory.c
> index 4ba73f5aa8bb..a78814413ac0 100644
> --- a/mm/memory.c
> +++ b/mm/memory.c
> @@ -4386,14 +4386,20 @@ vm_fault_t finish_fault(struct vm_fault *vmf)
>  
>  	vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd,
>  				      vmf->address, &vmf->ptl);
> -	ret = 0;
> +
>  	/* Re-check under ptl */
> -	if (likely(!vmf_pte_changed(vmf)))
> +	if (likely(!vmf_pte_changed(vmf))) {
>  		do_set_pte(vmf, page, vmf->address);
> -	else
> +
> +		/* no need to invalidate: a not-present page won't be cached */
> +		update_mmu_cache(vma, vmf->address, vmf->pte);
> +
> +		ret = 0;
> +	} else {
> +		update_mmu_tlb(vma, vmf->address, vmf->pte);
>  		ret = VM_FAULT_NOPAGE;
> +	}
>  
> -	update_mmu_tlb(vma, vmf->address, vmf->pte);
>  	pte_unmap_unlock(vmf->pte, vmf->ptl);
>  	return ret;
>  }
> -- 
> 2.34.1
> 

-- 
  Kiryl Shutsemau / Kirill A. Shutemov


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

* Re: [PATCH] mm: bring back update_mmu_cache() to finish_fault()
  2022-09-08 20:48 [PATCH] mm: bring back update_mmu_cache() to finish_fault() Sergei Antonov
  2022-09-08 22:24 ` Kirill A. Shutemov
@ 2022-09-09  5:34 ` Greg KH
  1 sibling, 0 replies; 4+ messages in thread
From: Greg KH @ 2022-09-09  5:34 UTC (permalink / raw)
  To: Sergei Antonov
  Cc: linux-mm, akpm, linux-arm-kernel, linux-arch, linux-kernel,
	stable, Kirill A . Shutemov

On Thu, Sep 08, 2022 at 11:48:09PM +0300, Sergei Antonov wrote:
> Running this test program on ARMv4 a few times (sometimes just once)
> reproduces the bug.
> 
> int main()
> {
>         unsigned i;
>         char paragon[SIZE];
>         void* ptr;
> 
>         memset(paragon, 0xAA, SIZE);
>         ptr = mmap(NULL, SIZE, PROT_READ | PROT_WRITE,
>                    MAP_ANON | MAP_SHARED, -1, 0);
>         if (ptr == MAP_FAILED) return 1;
>         printf("ptr = %p\n", ptr);
>         for (i=0;i<10000;i++){
>                 memset(ptr, 0xAA, SIZE);
>                 if (memcmp(ptr, paragon, SIZE)) {
>                         printf("Unexpected bytes on iteration %u!!!\n", i);
>                         break;
>                 }
>         }
>         munmap(ptr, SIZE);
> }
> 
> In the "ptr" buffer there appear runs of zero bytes which are aligned
> by 16 and their lengths are multiple of 16.
> 
> Linux v5.11 does not have the bug, "git bisect" finds the first bad commit:
> f9ce0be71d1f ("mm: Cleanup faultaround and finish_fault() codepaths")
> 
> Before the commit update_mmu_cache() was called during a call to
> filemap_map_pages() as well as finish_fault(). After the commit
> finish_fault() lacks it.
> 
> Bring back update_mmu_cache() to finish_fault() to fix the bug.
> Also call update_mmu_tlb() only when returning VM_FAULT_NOPAGE to more
> closely reproduce the code of alloc_set_pte() function that existed before
> the commit.
> 
> On many platforms update_mmu_cache() is nop:
>  x86, see arch/x86/include/asm/pgtable
>  ARMv6+, see arch/arm/include/asm/tlbflush.h
> So, it seems, few users ran into this bug.
> 
> Fixes: f9ce0be71d1f ("mm: Cleanup faultaround and finish_fault() codepaths")
> Signed-off-by: Sergei Antonov <saproj@gmail.com>
> Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
> ---
>  mm/memory.c | 14 ++++++++++----
>  1 file changed, 10 insertions(+), 4 deletions(-)
> 

<formletter>

This is not the correct way to submit patches for inclusion in the
stable kernel tree.  Please read:
    https://www.kernel.org/doc/html/latest/process/stable-kernel-rules.html
for how to do this properly.

</formletter>


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

* Re: [PATCH] mm: bring back update_mmu_cache() to finish_fault()
  2022-09-08 22:24 ` Kirill A. Shutemov
@ 2022-09-09 10:10   ` Will Deacon
  0 siblings, 0 replies; 4+ messages in thread
From: Will Deacon @ 2022-09-09 10:10 UTC (permalink / raw)
  To: Kirill A. Shutemov
  Cc: Sergei Antonov, linux-mm, akpm, linux-arm-kernel, linux-arch,
	linux-kernel, stable, Kirill A . Shutemov

On Fri, Sep 09, 2022 at 01:24:10AM +0300, Kirill A. Shutemov wrote:
> On Thu, Sep 08, 2022 at 11:48:09PM +0300, Sergei Antonov wrote:
> > Running this test program on ARMv4 a few times (sometimes just once)
> > reproduces the bug.
> > 
> > int main()
> > {
> >         unsigned i;
> >         char paragon[SIZE];
> >         void* ptr;
> > 
> >         memset(paragon, 0xAA, SIZE);
> >         ptr = mmap(NULL, SIZE, PROT_READ | PROT_WRITE,
> >                    MAP_ANON | MAP_SHARED, -1, 0);
> >         if (ptr == MAP_FAILED) return 1;
> >         printf("ptr = %p\n", ptr);
> >         for (i=0;i<10000;i++){
> >                 memset(ptr, 0xAA, SIZE);
> >                 if (memcmp(ptr, paragon, SIZE)) {
> >                         printf("Unexpected bytes on iteration %u!!!\n", i);
> >                         break;
> >                 }
> >         }
> >         munmap(ptr, SIZE);
> > }
> > 
> > In the "ptr" buffer there appear runs of zero bytes which are aligned
> > by 16 and their lengths are multiple of 16.
> > 
> > Linux v5.11 does not have the bug, "git bisect" finds the first bad commit:
> > f9ce0be71d1f ("mm: Cleanup faultaround and finish_fault() codepaths")
> > 
> > Before the commit update_mmu_cache() was called during a call to
> > filemap_map_pages() as well as finish_fault(). After the commit
> > finish_fault() lacks it.
> > 
> > Bring back update_mmu_cache() to finish_fault() to fix the bug.
> > Also call update_mmu_tlb() only when returning VM_FAULT_NOPAGE to more
> > closely reproduce the code of alloc_set_pte() function that existed before
> > the commit.
> > 
> > On many platforms update_mmu_cache() is nop:
> >  x86, see arch/x86/include/asm/pgtable
> >  ARMv6+, see arch/arm/include/asm/tlbflush.h
> > So, it seems, few users ran into this bug.
> > 
> > Fixes: f9ce0be71d1f ("mm: Cleanup faultaround and finish_fault() codepaths")
> > Signed-off-by: Sergei Antonov <saproj@gmail.com>
> > Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
> 
> +Will.
> 
> Seems I confused update_mmu_tlb() with update_mmu_cache() :/

Urgh, that thing is pretty horrible! But anyway, I agree that this change
looks correct based on the other callers in the file.

> Looks good to me:
> 
> Acked-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>

I'm assuming Andrew will pick this up. Otherwise, please let me know if
I should route it via the arm64 tree.

Will


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

end of thread, other threads:[~2022-09-09 10:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-08 20:48 [PATCH] mm: bring back update_mmu_cache() to finish_fault() Sergei Antonov
2022-09-08 22:24 ` Kirill A. Shutemov
2022-09-09 10:10   ` Will Deacon
2022-09-09  5:34 ` Greg KH

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).