Skip to content

Commit

Permalink
gup: avoid multiple user access locking/unlocking in fault_in_{read/w…
Browse files Browse the repository at this point in the history
…rite}able

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.

Link: https://lkml.kernel.org/r/720dcf79314acca1a78fae56d478cc851952149d.1637084492.git.christophe.leroy@csgroup.eu
Signed-off-by: Christophe Leroy <[email protected]>
Reviewed-by: Andreas Gruenbacher <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Mark Brown <[email protected]>
  • Loading branch information
chleroy authored and broonie committed Dec 10, 2021
1 parent 5edd128 commit 4ca3db5
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions mm/gup.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 4ca3db5

Please sign in to comment.