linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] gup: Avoid multiple user access locking/unlocking in fault_in_{read/write}able
@ 2021-11-16 17:43 Christophe Leroy
  2021-11-17  9:27 ` Andreas Gruenbacher
  0 siblings, 1 reply; 2+ messages in thread
From: Christophe Leroy @ 2021-11-16 17:43 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Christophe Leroy, linux-kernel, linux-mm, Andreas Gruenbacher

fault_in_readable() and fault_in_writeable() perform __get_user()
and __put_user() in a loop, implying multiple user access
locking/unlocking.

To avoid that, use user access blocks.

Cc: Andreas Gruenbacher <agruenba@redhat.com>
Signed-off-by: Christophe Leroy <christophe.leroy@csgroup.eu>
---
 mm/gup.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/mm/gup.c b/mm/gup.c
index 2c51e9748a6a..be2a41feec7d 100644
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -1672,21 +1672,22 @@ size_t fault_in_writeable(char __user *uaddr, size_t size)
 
 	if (unlikely(size == 0))
 		return 0;
+	if (!user_write_access_begin(uaddr, size))
+		return size;
 	if (!PAGE_ALIGNED(uaddr)) {
-		if (unlikely(__put_user(0, uaddr) != 0))
-			return size;
+		unsafe_put_user(0, uaddr, out);
 		uaddr = (char __user *)PAGE_ALIGN((unsigned long)uaddr);
 	}
 	end = (char __user *)PAGE_ALIGN((unsigned long)start + size);
 	if (unlikely(end < start))
 		end = NULL;
 	while (uaddr != end) {
-		if (unlikely(__put_user(0, uaddr) != 0))
-			goto out;
+		unsafe_put_user(0, uaddr, out);
 		uaddr += PAGE_SIZE;
 	}
 
 out:
+	user_write_access_end();
 	if (size > uaddr - start)
 		return size - (uaddr - start);
 	return 0;
@@ -1771,21 +1772,22 @@ size_t fault_in_readable(const char __user *uaddr, size_t size)
 
 	if (unlikely(size == 0))
 		return 0;
+	if (!user_read_access_begin(uaddr, size))
+		return size;
 	if (!PAGE_ALIGNED(uaddr)) {
-		if (unlikely(__get_user(c, uaddr) != 0))
-			return size;
+		unsafe_get_user(c, uaddr, out);
 		uaddr = (const char __user *)PAGE_ALIGN((unsigned long)uaddr);
 	}
 	end = (const char __user *)PAGE_ALIGN((unsigned long)start + size);
 	if (unlikely(end < start))
 		end = NULL;
 	while (uaddr != end) {
-		if (unlikely(__get_user(c, uaddr) != 0))
-			goto out;
+		unsafe_get_user(c, uaddr, out);
 		uaddr += PAGE_SIZE;
 	}
 
 out:
+	user_read_access_end();
 	(void)c;
 	if (size > uaddr - start)
 		return size - (uaddr - start);
-- 
2.31.1


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

* Re: [PATCH] gup: Avoid multiple user access locking/unlocking in fault_in_{read/write}able
  2021-11-16 17:43 [PATCH] gup: Avoid multiple user access locking/unlocking in fault_in_{read/write}able Christophe Leroy
@ 2021-11-17  9:27 ` Andreas Gruenbacher
  0 siblings, 0 replies; 2+ messages in thread
From: Andreas Gruenbacher @ 2021-11-17  9:27 UTC (permalink / raw)
  To: Christophe Leroy; +Cc: Andrew Morton, LKML, Linux-MM

On Tue, Nov 16, 2021 at 6:50 PM Christophe Leroy
<christophe.leroy@csgroup.eu> wrote:
> fault_in_readable() and fault_in_writeable() perform __get_user()
> and __put_user() in a loop, implying multiple user access
> locking/unlocking.
>
> To avoid that, use user access blocks.
>
> Cc: Andreas Gruenbacher <agruenba@redhat.com>
> Signed-off-by: Christophe Leroy <christophe.leroy@csgroup.eu>

Reviewed-by: Andreas Gruenbacher <agruenba@redhat.com>

> ---
>  mm/gup.c | 18 ++++++++++--------
>  1 file changed, 10 insertions(+), 8 deletions(-)
>
> diff --git a/mm/gup.c b/mm/gup.c
> index 2c51e9748a6a..be2a41feec7d 100644
> --- a/mm/gup.c
> +++ b/mm/gup.c
> @@ -1672,21 +1672,22 @@ size_t fault_in_writeable(char __user *uaddr, size_t size)
>
>         if (unlikely(size == 0))
>                 return 0;
> +       if (!user_write_access_begin(uaddr, size))
> +               return size;
>         if (!PAGE_ALIGNED(uaddr)) {
> -               if (unlikely(__put_user(0, uaddr) != 0))
> -                       return size;
> +               unsafe_put_user(0, uaddr, out);
>                 uaddr = (char __user *)PAGE_ALIGN((unsigned long)uaddr);
>         }
>         end = (char __user *)PAGE_ALIGN((unsigned long)start + size);
>         if (unlikely(end < start))
>                 end = NULL;
>         while (uaddr != end) {
> -               if (unlikely(__put_user(0, uaddr) != 0))
> -                       goto out;
> +               unsafe_put_user(0, uaddr, out);
>                 uaddr += PAGE_SIZE;
>         }
>
>  out:
> +       user_write_access_end();
>         if (size > uaddr - start)
>                 return size - (uaddr - start);
>         return 0;
> @@ -1771,21 +1772,22 @@ size_t fault_in_readable(const char __user *uaddr, size_t size)
>
>         if (unlikely(size == 0))
>                 return 0;
> +       if (!user_read_access_begin(uaddr, size))
> +               return size;
>         if (!PAGE_ALIGNED(uaddr)) {
> -               if (unlikely(__get_user(c, uaddr) != 0))
> -                       return size;
> +               unsafe_get_user(c, uaddr, out);
>                 uaddr = (const char __user *)PAGE_ALIGN((unsigned long)uaddr);
>         }
>         end = (const char __user *)PAGE_ALIGN((unsigned long)start + size);
>         if (unlikely(end < start))
>                 end = NULL;
>         while (uaddr != end) {
> -               if (unlikely(__get_user(c, uaddr) != 0))
> -                       goto out;
> +               unsafe_get_user(c, uaddr, out);
>                 uaddr += PAGE_SIZE;
>         }
>
>  out:
> +       user_read_access_end();
>         (void)c;
>         if (size > uaddr - start)
>                 return size - (uaddr - start);
> --
> 2.31.1
>


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

end of thread, other threads:[~2021-11-17  9:28 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-16 17:43 [PATCH] gup: Avoid multiple user access locking/unlocking in fault_in_{read/write}able Christophe Leroy
2021-11-17  9:27 ` Andreas Gruenbacher

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).