linux-stable/arch/riscv/lib
Alexandre Ghiti 4b05b99390
riscv: uaccess: Return the number of bytes effectively not copied
It was reported that the riscv kernel hangs while executing the test
in [1].

Indeed, the test hangs when trying to write a buffer to a file. The
problem is that the riscv implementation of raw_copy_from_user() does not
return the correct number of bytes not written when an exception happens
and is fixed up, instead it always returns the initial size to copy,
even if some bytes were actually copied.

generic_perform_write() pre-faults the user pages and bails out if nothing
can be written, otherwise it will access the userspace buffer: here the
riscv implementation keeps returning it was not able to copy any byte
though the pre-faulting indicates otherwise. So generic_perform_write()
keeps retrying to access the user memory and ends up in an infinite
loop.

Note that before the commit mentioned in [1] that introduced this
regression, it worked because generic_perform_write() would bail out if
only one byte could not be written.

So fix this by returning the number of bytes effectively not written in
__asm_copy_[to|from]_user() and __clear_user(), as it is expected.

Link: https://lore.kernel.org/linux-riscv/20230309151841.bomov6hq3ybyp42a@debian/ [1]
Fixes: ebcbd75e39 ("riscv: Fix the bug in memory access fixup code")
Reported-by: Bo YU <tsu.yubo@gmail.com>
Closes: https://lore.kernel.org/linux-riscv/20230309151841.bomov6hq3ybyp42a@debian/#t
Reported-by: Aurelien Jarno <aurelien@aurel32.net>
Closes: https://lore.kernel.org/linux-riscv/ZNOnCakhwIeue3yr@aurel32.net/
Signed-off-by: Alexandre Ghiti <alexghiti@rivosinc.com>
Reviewed-by: Björn Töpel <bjorn@rivosinc.com>
Tested-by: Aurelien Jarno <aurelien@aurel32.net>
Reviewed-by: Aurelien Jarno <aurelien@aurel32.net>
Link: https://lore.kernel.org/r/20230811150604.1621784-1-alexghiti@rivosinc.com
Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
2023-08-16 07:30:06 -07:00
..
clear_page.S RISC-V: Use Zicboz in clear_page when available 2023-03-14 21:26:06 -07:00
delay.c include/linux/delay.h: replace kernel.h with the necessary inclusions 2021-11-09 10:02:49 -08:00
error-inject.c riscv: Add support for function error injection 2021-01-14 15:09:09 -08:00
Makefile RISC-V: Use Zicboz in clear_page when available 2023-03-14 21:26:06 -07:00
memcpy.S riscv: Allow to downgrade paging mode from the command line 2023-04-26 07:30:52 -07:00
memmove.S riscv: Allow to downgrade paging mode from the command line 2023-04-26 07:30:52 -07:00
memset.S riscv: Add KASAN support 2020-01-22 13:09:58 -08:00
strcmp.S riscv: lib: Include hwcap.h directly 2023-03-14 20:51:24 -07:00
strlen.S riscv: Allow to downgrade paging mode from the command line 2023-04-26 07:30:52 -07:00
strncmp.S riscv: lib: Include hwcap.h directly 2023-03-14 20:51:24 -07:00
tishift.S riscv: Less inefficient gcc tishift helpers (and export their symbols) 2020-01-18 19:13:41 -08:00
uaccess.S riscv: uaccess: Return the number of bytes effectively not copied 2023-08-16 07:30:06 -07:00