[PATCH] arm: use mmap_write_(un)lock for copy_to_user

Mike Rapoport rppt at kernel.org
Tue Sep 29 05:26:51 EDT 2020


On Sat, Sep 26, 2020 at 09:28:54PM +0200, Christian Lamparter wrote:
> changes ARM's copy_to_user to use mmap_*write*_lock
> variants. This is because the data is written to
> user-space and not read.

The mmap lock protects internals of 'struct mm_struct' and they do not
change when the data is copied regardless of its direction.

> Signed-off-by: Christian Lamparter <chunkeey at gmail.com>
> ---
>  arch/arm/lib/uaccess_with_memcpy.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/arch/arm/lib/uaccess_with_memcpy.c b/arch/arm/lib/uaccess_with_memcpy.c
> index 106f83a5ea6d..7491c13fdf0e 100644
> --- a/arch/arm/lib/uaccess_with_memcpy.c
> +++ b/arch/arm/lib/uaccess_with_memcpy.c
> @@ -101,7 +101,7 @@ __copy_to_user_memcpy(void __user *to, const void *from, unsigned long n)
>  	atomic = faulthandler_disabled();
>  
>  	if (!atomic)
> -		mmap_read_lock(current->mm);
> +		mmap_write_lock(current->mm);
>  	while (n) {
>  		pte_t *pte;
>  		spinlock_t *ptl;
> @@ -109,11 +109,11 @@ __copy_to_user_memcpy(void __user *to, const void *from, unsigned long n)
>  
>  		while (!pin_page_for_write(to, &pte, &ptl)) {
>  			if (!atomic)
> -				mmap_read_unlock(current->mm);
> +				mmap_write_unlock(current->mm);
>  			if (__put_user(0, (char __user *)to))
>  				goto out;
>  			if (!atomic)
> -				mmap_read_lock(current->mm);
> +				mmap_write_lock(current->mm);
>  		}
>  
>  		tocopy = (~(unsigned long)to & ~PAGE_MASK) + 1;
> @@ -133,7 +133,7 @@ __copy_to_user_memcpy(void __user *to, const void *from, unsigned long n)
>  			spin_unlock(ptl);
>  	}
>  	if (!atomic)
> -		mmap_read_unlock(current->mm);
> +		mmap_write_unlock(current->mm);
>  
>  out:
>  	return n;
> -- 
> 2.28.0
> 

-- 
Sincerely yours,
Mike.



More information about the linux-arm-kernel mailing list