diff mbox series

[v1,2/2] mm/mprotect: drop pgprot_t parameter from change_protection()

Message ID 20221223155616.297723-3-david@redhat.com (mailing list archive)
State New
Headers show
Series mm: uffd-wp + change_protection() cleanups | expand

Commit Message

David Hildenbrand Dec. 23, 2022, 3:56 p.m. UTC
Being able to provide a custom protection opens the door for
inconsistencies and BUGs: for example, accidentally allowing for more
permissions than desired by other mechanisms (e.g., softdirty tracking).
vma->vm_page_prot should be the single source of truth.

Only PROT_NUMA is special: there is no way we can erroneously allow
for more permissions when removing all permissions. Special-case using
the MM_CP_PROT_NUMA flag.

Signed-off-by: David Hildenbrand <david@redhat.com>
---
 include/linux/mm.h |  3 +--
 mm/mempolicy.c     |  3 +--
 mm/mprotect.c      | 14 +++++++++++---
 mm/userfaultfd.c   |  3 +--
 4 files changed, 14 insertions(+), 9 deletions(-)

Comments

kernel test robot Dec. 24, 2022, 3:08 a.m. UTC | #1
Hi David,

I love your patch! Yet something to improve:

[auto build test ERROR on akpm-mm/mm-everything]

url:    https://github.com/intel-lab-lkp/linux/commits/David-Hildenbrand/mm-userfaultfd-rely-on-vma-vm_page_prot-in-uffd_wp_range/20221224-000206
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20221223155616.297723-3-david%40redhat.com
patch subject: [PATCH v1 2/2] mm/mprotect: drop pgprot_t parameter from change_protection()
config: mips-malta_defconfig
compiler: clang version 16.0.0 (https://github.com/llvm/llvm-project f5700e7b69048de958172fb513b336564e7f8709)
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # install mips cross compiling tool for clang build
        # apt-get install binutils-mipsel-linux-gnu
        # https://github.com/intel-lab-lkp/linux/commit/c77a6bee317925cf3d078bc97a42d644503b9f1a
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review David-Hildenbrand/mm-userfaultfd-rely-on-vma-vm_page_prot-in-uffd_wp_range/20221224-000206
        git checkout c77a6bee317925cf3d078bc97a42d644503b9f1a
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=mips olddefconfig
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=mips SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

>> mm/mprotect.c:523:13: error: use of undeclared identifier 'PAGE_NONE'
                   newprot = PAGE_NONE;
                             ^
   1 error generated.


vim +/PAGE_NONE +523 mm/mprotect.c

   507	
   508	unsigned long change_protection(struct mmu_gather *tlb,
   509			       struct vm_area_struct *vma, unsigned long start,
   510			       unsigned long end, unsigned long cp_flags)
   511	{
   512		pgprot_t newprot = vma->vm_page_prot;
   513		unsigned long pages;
   514	
   515		BUG_ON((cp_flags & MM_CP_UFFD_WP_ALL) == MM_CP_UFFD_WP_ALL);
   516	
   517		/*
   518		 * Ordinary protection updates (mprotect, uffd-wp, softdirty tracking)
   519		 * are expected to reflect their requirements via VMA flags such that
   520		 * vma_set_page_prot() will adjust vma->vm_page_prot accordingly.
   521		 */
   522		if (cp_flags & MM_CP_PROT_NUMA)
 > 523			newprot = PAGE_NONE;
   524	
   525		if (is_vm_hugetlb_page(vma))
   526			pages = hugetlb_change_protection(vma, start, end, newprot,
   527							  cp_flags);
   528		else
   529			pages = change_protection_range(tlb, vma, start, end, newprot,
   530							cp_flags);
   531	
   532		return pages;
   533	}
   534
kernel test robot Dec. 24, 2022, 4:59 a.m. UTC | #2
Hi David,

I love your patch! Yet something to improve:

[auto build test ERROR on akpm-mm/mm-everything]

url:    https://github.com/intel-lab-lkp/linux/commits/David-Hildenbrand/mm-userfaultfd-rely-on-vma-vm_page_prot-in-uffd_wp_range/20221224-000206
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20221223155616.297723-3-david%40redhat.com
patch subject: [PATCH v1 2/2] mm/mprotect: drop pgprot_t parameter from change_protection()
config: arc-tb10x_defconfig
compiler: arc-elf-gcc (GCC) 12.1.0
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/intel-lab-lkp/linux/commit/c77a6bee317925cf3d078bc97a42d644503b9f1a
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review David-Hildenbrand/mm-userfaultfd-rely-on-vma-vm_page_prot-in-uffd_wp_range/20221224-000206
        git checkout c77a6bee317925cf3d078bc97a42d644503b9f1a
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=arc olddefconfig
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=arc SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

   mm/mprotect.c: In function 'change_protection':
>> mm/mprotect.c:523:27: error: 'PAGE_NONE' undeclared (first use in this function); did you mean 'PAGE_U_NONE'?
     523 |                 newprot = PAGE_NONE;
         |                           ^~~~~~~~~
         |                           PAGE_U_NONE
   mm/mprotect.c:523:27: note: each undeclared identifier is reported only once for each function it appears in


vim +523 mm/mprotect.c

   507	
   508	unsigned long change_protection(struct mmu_gather *tlb,
   509			       struct vm_area_struct *vma, unsigned long start,
   510			       unsigned long end, unsigned long cp_flags)
   511	{
   512		pgprot_t newprot = vma->vm_page_prot;
   513		unsigned long pages;
   514	
   515		BUG_ON((cp_flags & MM_CP_UFFD_WP_ALL) == MM_CP_UFFD_WP_ALL);
   516	
   517		/*
   518		 * Ordinary protection updates (mprotect, uffd-wp, softdirty tracking)
   519		 * are expected to reflect their requirements via VMA flags such that
   520		 * vma_set_page_prot() will adjust vma->vm_page_prot accordingly.
   521		 */
   522		if (cp_flags & MM_CP_PROT_NUMA)
 > 523			newprot = PAGE_NONE;
   524	
   525		if (is_vm_hugetlb_page(vma))
   526			pages = hugetlb_change_protection(vma, start, end, newprot,
   527							  cp_flags);
   528		else
   529			pages = change_protection_range(tlb, vma, start, end, newprot,
   530							cp_flags);
   531	
   532		return pages;
   533	}
   534
David Hildenbrand Dec. 24, 2022, 5:01 p.m. UTC | #3
On 23.12.22 16:56, David Hildenbrand wrote:
> Being able to provide a custom protection opens the door for
> inconsistencies and BUGs: for example, accidentally allowing for more
> permissions than desired by other mechanisms (e.g., softdirty tracking).
> vma->vm_page_prot should be the single source of truth.
> 
> Only PROT_NUMA is special: there is no way we can erroneously allow
> for more permissions when removing all permissions. Special-case using
> the MM_CP_PROT_NUMA flag.
> 
> Signed-off-by: David Hildenbrand <david@redhat.com>
> ---
>   include/linux/mm.h |  3 +--
>   mm/mempolicy.c     |  3 +--
>   mm/mprotect.c      | 14 +++++++++++---
>   mm/userfaultfd.c   |  3 +--
>   4 files changed, 14 insertions(+), 9 deletions(-)
> 
> diff --git a/include/linux/mm.h b/include/linux/mm.h
> index f3f196e4d66d..b8be8c33ca20 100644
> --- a/include/linux/mm.h
> +++ b/include/linux/mm.h
> @@ -2148,8 +2148,7 @@ bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
>   			     pte_t pte);
>   extern unsigned long change_protection(struct mmu_gather *tlb,
>   			      struct vm_area_struct *vma, unsigned long start,
> -			      unsigned long end, pgprot_t newprot,
> -			      unsigned long cp_flags);
> +			      unsigned long end, unsigned long cp_flags);
>   extern int mprotect_fixup(struct mmu_gather *tlb, struct vm_area_struct *vma,
>   			  struct vm_area_struct **pprev, unsigned long start,
>   			  unsigned long end, unsigned long newflags);
> diff --git a/mm/mempolicy.c b/mm/mempolicy.c
> index 61aa9aedb728..c3f02703a710 100644
> --- a/mm/mempolicy.c
> +++ b/mm/mempolicy.c
> @@ -635,8 +635,7 @@ unsigned long change_prot_numa(struct vm_area_struct *vma,
>   
>   	tlb_gather_mmu(&tlb, vma->vm_mm);
>   
> -	nr_updated = change_protection(&tlb, vma, addr, end, PAGE_NONE,
> -				       MM_CP_PROT_NUMA);
> +	nr_updated = change_protection(&tlb, vma, addr, end, MM_CP_PROT_NUMA);
>   	if (nr_updated)
>   		count_vm_numa_events(NUMA_PTE_UPDATES, nr_updated);
>   
> diff --git a/mm/mprotect.c b/mm/mprotect.c
> index 908df12caa26..569cefa668a6 100644
> --- a/mm/mprotect.c
> +++ b/mm/mprotect.c
> @@ -503,13 +503,21 @@ static unsigned long change_protection_range(struct mmu_gather *tlb,
>   
>   unsigned long change_protection(struct mmu_gather *tlb,
>   		       struct vm_area_struct *vma, unsigned long start,
> -		       unsigned long end, pgprot_t newprot,
> -		       unsigned long cp_flags)
> +		       unsigned long end, unsigned long cp_flags)
>   {
> +	pgprot_t newprot = vma->vm_page_prot;
>   	unsigned long pages;
>   
>   	BUG_ON((cp_flags & MM_CP_UFFD_WP_ALL) == MM_CP_UFFD_WP_ALL);
>   
> +	/*
> +	 * Ordinary protection updates (mprotect, uffd-wp, softdirty tracking)
> +	 * are expected to reflect their requirements via VMA flags such that
> +	 * vma_set_page_prot() will adjust vma->vm_page_prot accordingly.
> +	 */
> +	if (cp_flags & MM_CP_PROT_NUMA)
> +		newprot = PAGE_NONE;
> +
>   	if (is_vm_hugetlb_page(vma))
>   		pages = hugetlb_change_protection(vma, start, end, newprot,
>   						  cp_flags);
> @@ -638,7 +646,7 @@ mprotect_fixup(struct mmu_gather *tlb, struct vm_area_struct *vma,
>   		mm_cp_flags |= MM_CP_TRY_CHANGE_WRITABLE;
>   	vma_set_page_prot(vma);
>   
> -	change_protection(tlb, vma, start, end, vma->vm_page_prot, mm_cp_flags);
> +	change_protection(tlb, vma, start, end, mm_cp_flags);
>   
>   	/*
>   	 * Private VM_LOCKED VMA becoming writable: trigger COW to avoid major
> diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c
> index 351e8d6b398b..be7ee9d82e72 100644
> --- a/mm/userfaultfd.c
> +++ b/mm/userfaultfd.c
> @@ -744,8 +744,7 @@ void uffd_wp_range(struct mm_struct *dst_mm, struct vm_area_struct *dst_vma,
>   	if (vma_wants_manual_pte_write_upgrade(dst_vma))
>   		mm_cp_flags |= MM_CP_TRY_CHANGE_WRITABLE;
>   	tlb_gather_mmu(&tlb, dst_mm);
> -	change_protection(&tlb, dst_vma, start, start + len, vma->vm_page_prot,
> -			  mm_cp_flags);
> +	change_protection(&tlb, dst_vma, start, start + len, mm_cp_flags);
>   	tlb_finish_mmu(&tlb);
>   }
>   

The following fix for compilation errors without PAGE_NONE:


 From a164d6cf728e353294aa9e65b8ead5241c800421 Mon Sep 17 00:00:00 2001
From: David Hildenbrand <david@redhat.com>
Date: Sat, 24 Dec 2022 15:01:18 +0100
Subject: [PATCH] fixup: mm/mprotect: drop pgprot_t parameter from
  change_protection()

PAGE_NONE might not be defined without CONFIG_NUMA_BALANCING

Signed-off-by: David Hildenbrand <david@redhat.com>
---
  mm/mprotect.c | 4 ++++
  1 file changed, 4 insertions(+)

diff --git a/mm/mprotect.c b/mm/mprotect.c
index 569cefa668a6..809832954898 100644
--- a/mm/mprotect.c
+++ b/mm/mprotect.c
@@ -510,6 +510,7 @@ unsigned long change_protection(struct mmu_gather *tlb,
  
  	BUG_ON((cp_flags & MM_CP_UFFD_WP_ALL) == MM_CP_UFFD_WP_ALL);
  
+#ifdef CONFIG_NUMA_BALANCING
  	/*
  	 * Ordinary protection updates (mprotect, uffd-wp, softdirty tracking)
  	 * are expected to reflect their requirements via VMA flags such that
@@ -517,6 +518,9 @@ unsigned long change_protection(struct mmu_gather *tlb,
  	 */
  	if (cp_flags & MM_CP_PROT_NUMA)
  		newprot = PAGE_NONE;
+#else
+	WARN_ON_ONCE(cp_flags & MM_CP_PROT_NUMA);
+#endif
  
  	if (is_vm_hugetlb_page(vma))
  		pages = hugetlb_change_protection(vma, start, end, newprot,
diff mbox series

Patch

diff --git a/include/linux/mm.h b/include/linux/mm.h
index f3f196e4d66d..b8be8c33ca20 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -2148,8 +2148,7 @@  bool can_change_pte_writable(struct vm_area_struct *vma, unsigned long addr,
 			     pte_t pte);
 extern unsigned long change_protection(struct mmu_gather *tlb,
 			      struct vm_area_struct *vma, unsigned long start,
-			      unsigned long end, pgprot_t newprot,
-			      unsigned long cp_flags);
+			      unsigned long end, unsigned long cp_flags);
 extern int mprotect_fixup(struct mmu_gather *tlb, struct vm_area_struct *vma,
 			  struct vm_area_struct **pprev, unsigned long start,
 			  unsigned long end, unsigned long newflags);
diff --git a/mm/mempolicy.c b/mm/mempolicy.c
index 61aa9aedb728..c3f02703a710 100644
--- a/mm/mempolicy.c
+++ b/mm/mempolicy.c
@@ -635,8 +635,7 @@  unsigned long change_prot_numa(struct vm_area_struct *vma,
 
 	tlb_gather_mmu(&tlb, vma->vm_mm);
 
-	nr_updated = change_protection(&tlb, vma, addr, end, PAGE_NONE,
-				       MM_CP_PROT_NUMA);
+	nr_updated = change_protection(&tlb, vma, addr, end, MM_CP_PROT_NUMA);
 	if (nr_updated)
 		count_vm_numa_events(NUMA_PTE_UPDATES, nr_updated);
 
diff --git a/mm/mprotect.c b/mm/mprotect.c
index 908df12caa26..569cefa668a6 100644
--- a/mm/mprotect.c
+++ b/mm/mprotect.c
@@ -503,13 +503,21 @@  static unsigned long change_protection_range(struct mmu_gather *tlb,
 
 unsigned long change_protection(struct mmu_gather *tlb,
 		       struct vm_area_struct *vma, unsigned long start,
-		       unsigned long end, pgprot_t newprot,
-		       unsigned long cp_flags)
+		       unsigned long end, unsigned long cp_flags)
 {
+	pgprot_t newprot = vma->vm_page_prot;
 	unsigned long pages;
 
 	BUG_ON((cp_flags & MM_CP_UFFD_WP_ALL) == MM_CP_UFFD_WP_ALL);
 
+	/*
+	 * Ordinary protection updates (mprotect, uffd-wp, softdirty tracking)
+	 * are expected to reflect their requirements via VMA flags such that
+	 * vma_set_page_prot() will adjust vma->vm_page_prot accordingly.
+	 */
+	if (cp_flags & MM_CP_PROT_NUMA)
+		newprot = PAGE_NONE;
+
 	if (is_vm_hugetlb_page(vma))
 		pages = hugetlb_change_protection(vma, start, end, newprot,
 						  cp_flags);
@@ -638,7 +646,7 @@  mprotect_fixup(struct mmu_gather *tlb, struct vm_area_struct *vma,
 		mm_cp_flags |= MM_CP_TRY_CHANGE_WRITABLE;
 	vma_set_page_prot(vma);
 
-	change_protection(tlb, vma, start, end, vma->vm_page_prot, mm_cp_flags);
+	change_protection(tlb, vma, start, end, mm_cp_flags);
 
 	/*
 	 * Private VM_LOCKED VMA becoming writable: trigger COW to avoid major
diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c
index 351e8d6b398b..be7ee9d82e72 100644
--- a/mm/userfaultfd.c
+++ b/mm/userfaultfd.c
@@ -744,8 +744,7 @@  void uffd_wp_range(struct mm_struct *dst_mm, struct vm_area_struct *dst_vma,
 	if (vma_wants_manual_pte_write_upgrade(dst_vma))
 		mm_cp_flags |= MM_CP_TRY_CHANGE_WRITABLE;
 	tlb_gather_mmu(&tlb, dst_mm);
-	change_protection(&tlb, dst_vma, start, start + len, vma->vm_page_prot,
-			  mm_cp_flags);
+	change_protection(&tlb, dst_vma, start, start + len, mm_cp_flags);
 	tlb_finish_mmu(&tlb);
 }