VM_FAULT_RETRY fixes
Subsystems (10)
RFC PATCH 1/1 VM_FAULT_RETRY fixes
Page fault handlers generally react to VM_FAULT_RETRY fromhandle_mm_fault() by repeating the whole thing (starting with locking mmap) with FAULT_FLAG_TRIED added to flags.
However, there are two cases when that's not the right thing
to do:
1) fault has happened in userland and we have a pending signal. In that case we'd better return from fault handler immediately.
2) fault has happened in kernel (e.g. in something like copy_from_user()) and we have a pending *fatal* signal. Solution is to handle that as if handle_mm_fault() had failed; we have come from kernel mode, so we'd better have an exception table entry for the fauling insn. Find it and deal with it; from the copy_from_user() (or whatever it was that triggered our fault) caller's POV it's indistinguishable from running into an unmapped area, so it will fail. The process is not going to survive anyway.
Quietly returning from #PF handler in the second case is asking for livelock - one common case when handle_mm_fault() returns VM_FAULT_RETRY is when it needs to wait for page lock and gets hit by a fatal signal. Running into that in any uaccess primitive will end up repeating the faulting insn again and again, as long as we hit that case in handle_mm_fault(). Eventually it might get out (e.g. trylock manages to get page lock without hitting the "wait for it" codepath), but it's obviously not a good situation.
On x86 it had been noticed and fixed back in 2014, in 26178ec11ef3 "x86:
mm: consolidate VM_FAULT_RETRY handling". Some of the other architectures had it dealt with later - e.g. arm in 2017, the fix is 746a272e44141 "ARM: 8692/1: mm: abort uaccess retries upon fatal signal"; xtensa - in 2021, the fix is 7b9acbb6aad4f "xtensa: fix uaccess-related livelock in do_page_fault", etc.
However, it never had been done on a bunch of architectures - the current mainline still has that bug on alpha, hexagon, itanic, m68k, microblaze, nios2, openrisc, parisc, riscv and sparc (both sparc32 and sparc64). Fixes are trivial, but I've no way to test them for most of those architectures.
PATCH 1/10 alpha: fix livelock in uaccess
alpha equivalent of 26178ec11ef3 "x86: mm: consolidate VM_FAULT_RETRY handling" If e.g. get_user() triggers a page fault and a fatal signal is caught, we might end up with handle_mm_fault() returning VM_FAULT_RETRY and not doing anything to page tables. In such case we must *not* return to the faulting insn - that would repeat the entire thing without making any progress; what we need instead is to treat that as failed (user) memory access.
--- arch/alpha/mm/fault.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
arch/alpha/mm/fault.c +4 -1
@@ -152,8 +152,11 @@ do_page_fault(unsigned long address, unsigned long mmcsr,the fault. */ fault =handle_mm_fault (vma, address, flags, regs); -if (fault_signal_pending (fault, regs)) +if (fault_signal_pending (fault, regs)) { +if (!user_mode (regs)) + goto no_context;return ; + }/* The fault is fully completed (including releasing mmap lock) */ if (fault &VM_FAULT_COMPLETED )
Signature · 1 line
2.30.2
Re: [PATCH 01/10] alpha: fix livelock in uaccess
This series was applied to riscv/linux.git (fixes) by Al Viro <[email protected]>:
Hello:
This series was applied to riscv/linux.git (fixes) by Al Viro <[email protected]>:
Quoted 8 lines · On Tue, 31 Jan 2023 20:03:28 +0000 you wrote:
alpha equivalent of
26178ec11ef3"x86: mm: consolidateVM_FAULT_RETRYhandling" If e.g.get_user()triggers a page fault and a fatal signal is caught, we might end up withhandle_mm_fault()returningVM_FAULT_RETRYand not doing anything to page tables. In such case we must *not* return to the faulting insn - that would repeat the entire thing without making any progress; what we need instead is to treat that as failed (user) memory access.[...]
Here is the summary with links:
- [01/10] alpha: fix livelock in uaccess
(no matching commit)
- [02/10] hexagon: fix livelock in uaccess
(no matching commit)
- [03/10] ia64: fix livelock in uaccess
(no matching commit)
- [04/10] m68k: fix livelock in uaccess
(no matching commit)
- [05/10] microblaze: fix livelock in uaccess
(no matching commit)
- [06/10] nios2: fix livelock in uaccess
(no matching commit)
- [07/10] openrisc: fix livelock in uaccess
(no matching commit)
- [08/10] parisc: fix livelock in uaccess
(no matching commit)
- [09/10] riscv: fix livelock in uaccess
https://git.kernel.org/riscv/c/d835eb3a57de
- [10/10] sparc: fix livelock in uaccess
(no matching commit)
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html
PATCH 2/10 hexagon: fix livelock in uaccess
In reply to Al Virohexagon equivalent of 26178ec11ef3 "x86: mm: consolidate VM_FAULT_RETRY handling" If e.g. get_user() triggers a page fault and a fatal signal is caught, we might end up with handle_mm_fault() returning VM_FAULT_RETRY and not doing anything to page tables. In such case we must *not* return to the faulting insn - that would repeat the entire thing without making any progress; what we need instead is to treat that as failed (user) memory access.
--- arch/hexagon/mm/vm_fault.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
arch/hexagon/mm/vm_fault.c +4 -1
@@ -93,8 +93,11 @@ void do_page_fault(unsigned long address, long cause, struct pt_regs *regs) fault =handle_mm_fault (vma, address, flags, regs); -if (fault_signal_pending (fault, regs)) +if (fault_signal_pending (fault, regs)) { +if (!user_mode (regs)) + goto no_context;return ; + }/* The fault is fully completed (including releasing mmap lock) */ if (fault &VM_FAULT_COMPLETED )
Signature · 1 line
2.30.2
RE: [PATCH 02/10] hexagon: fix livelock in uaccess
Quoted 47 lines
-----Original Message-----
From: Al Viro <[email protected]> On Behalf Of Al Viro
Sent: Tuesday, January 31, 2023 2:04 PM
To: [email protected]
Cc: [email protected]; [email protected]; linux- [email protected]; [email protected]; Michal Simek <[email protected]>; Dinh Nguyen <[email protected]>; [email protected]; [email protected]; linux- [email protected]; [email protected]; Linus Torvalds <[email protected]>
Subject: [PATCH 02/10] hexagon: fix livelock in uaccessWARNING: This email originated from outside of Qualcomm. Please be wary of any links or attachments, and do not enable macros.
hexagon equivalent of
26178ec11ef3"x86: mm: consolidateVM_FAULT_RETRYhandling"
If e.g.get_user()triggers a page fault and a fatal signal is caught, we might end up withhandle_mm_fault()returningVM_FAULT_RETRYand not doing anything
to page tables. In such case we must *not* return to the faulting insn - that would repeat the entire thing without making any progress; what we need
instead is to treat that as failed (user) memory access.Signed-off-by Al Viro <[email protected]>--- arch/hexagon/mm/vm_fault.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)arch/hexagon/mm/vm_fault.c +4
-1@@ -93,8 +93,11 @@ void do_page_fault(unsigned long address, long cause, struct pt_regs *regs) fault =handle_mm_fault (vma, address, flags, regs); -if (fault_signal_pending (fault, regs)) +if (fault_signal_pending (fault, regs)) { +if (!user_mode (regs)) + goto no_context;return ; + }/* The fault is fully completed (including releasing mmap lock) */
Quoted 3 lines
if (fault &
VM_FAULT_COMPLETED)
--
2.30.2
PATCH 3/10 ia64: fix livelock in uaccess
In reply to Al Viroia64 equivalent of 26178ec11ef3 "x86: mm: consolidate VM_FAULT_RETRY handling" If e.g. get_user() triggers a page fault and a fatal signal is caught, we might end up with handle_mm_fault() returning VM_FAULT_RETRY and not doing anything to page tables. In such case we must *not* return to the faulting insn - that would repeat the entire thing without making any progress; what we need instead is to treat that as failed (user) memory access.
--- arch/ia64/mm/fault.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
arch/ia64/mm/fault.c +4 -1
@@ -136,8 +136,11 @@ ia64_do_page_fault (unsigned long address, unsigned long isr, struct pt_regs *re*/ fault =handle_mm_fault (vma, address, flags, regs); -if (fault_signal_pending (fault, regs)) +if (fault_signal_pending (fault, regs)) { +if (!user_mode (regs)) + goto no_context;return ; + }/* The fault is fully completed (including releasing mmap lock) */ if (fault &VM_FAULT_COMPLETED )
Signature · 1 line
2.30.2
PATCH 4/10 m68k: fix livelock in uaccess
In reply to Al Virom68k equivalent of 26178ec11ef3 "x86: mm: consolidate VM_FAULT_RETRY handling" If e.g. get_user() triggers a page fault and a fatal signal is caught, we might end up with handle_mm_fault() returning VM_FAULT_RETRY and not doing anything to page tables. In such case we must *not* return to the faulting insn - that would repeat the entire thing without making any progress; what we need instead is to treat that as failed (user) memory access.
--- arch/m68k/mm/fault.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
arch/m68k/mm/fault.c +4 -1
@@ -138,8 +138,11 @@ int do_page_fault(struct pt_regs *regs, unsigned long address, fault =handle_mm_fault (vma, address, flags, regs);pr_debug ("handle_mm_fault returns %x\n" , fault); -if (fault_signal_pending (fault, regs)) +if (fault_signal_pending (fault, regs)) { +if (!user_mode (regs)) + goto no_context;return 0 ; + }/* The fault is fully completed (including releasing mmap lock) */ if (fault &VM_FAULT_COMPLETED )
Signature · 1 line
2.30.2
Re: [PATCH 04/10] m68k: fix livelock in uaccess
Hello Al,
Quoted 9 lines · Al Viro
m68k equivalent of
26178ec11ef3"x86: mm: consolidateVM_FAULT_RETRYhandling" If e.g.get_user()triggers a page fault and a fatal signal is caught, we might end up withhandle_mm_fault()returningVM_FAULT_RETRYand not doing anything to page tables. In such case we must *not* return to the faulting insn - that would repeat the entire thing without making any progress; what we need instead is to treat that as failed (user) memory access.Signed-off-by Al Viro <[email protected]>
That could be a bug I was chasing back in 2021 but never found. The mmap stressors in stress-ng were triggering a crash on a Mac Quadras, though only rarely. Sometimes it would run all day without a failure.
Last year when I started using GCC 12 to build the kernel, I saw the same workload fail again but the failure mode had become a silent hang/livelock instead of the oopses I got with GCC 6.
When I press the NMI button after the livelock I always see do_page_fault() in the backtrace. So I've been testing your patch. I've been running the same stress-ng reproducer for about 12 hours now with no failures which looks promising.
In case that stress-ng testing is of use:
BTW, how did you identify that bug in do_page_fault()? If its the same bug I was chasing, it could be an old one. The stress-ng logs I collected last year include a crash from a v4.14 build.
Quoted 22 lines
--- arch/m68k/mm/fault.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)arch/m68k/mm/fault.c +4
-1@@ -138,8 +138,11 @@ int do_page_fault(struct pt_regs *regs, unsigned long address, fault =handle_mm_fault (vma, address, flags, regs);pr_debug ("handle_mm_fault returns %x\n" , fault); -if (fault_signal_pending (fault, regs)) +if (fault_signal_pending (fault, regs)) { +if (!user_mode (regs)) + goto no_context;return 0 ; + }/* The fault is fully completed (including releasing mmap lock) */ if (fault &VM_FAULT_COMPLETED )
Re: [PATCH 04/10] m68k: fix livelock in uaccess
Quoted 9 lines · Finn Thain
That could be a bug I was chasing back in 2021 but never found. The mmap stressors in stress-ng were triggering a crash on a Mac Quadras, though only rarely. Sometimes it would run all day without a failure.
Last year when I started using GCC 12 to build the kernel, I saw the same workload fail again but the failure mode had become a silent hang/livelock instead of the oopses I got with GCC 6.
Quoted 7 lines
When I press the NMI button after the livelock I always see
do_page_fault()in the backtrace. So I've been testing your patch. I've been running the same stress-ng reproducer for about 12 hours now with no failures which looks promising.In case that stress-ng testing is of use:
Tested-by Finn Thain <[email protected]>
Could you test the thing that Mark Rutland pointed to? He had an actual test-case for this for the arm64 fixes some years ago.
See
https://lore.kernel.org/all/Y9pD+TMP+%2FSyfeJm@FVFF77S0Q05N/
for his email with links to his old test-case?
Linus
Re: [PATCH 04/10] m68k: fix livelock in uaccess
Quoted 18 lines · Linus Torvalds
Quoted 9 lines · Finn Thain
That could be a bug I was chasing back in 2021 but never found. The mmap stressors in stress-ng were triggering a crash on a Mac Quadras, though only rarely. Sometimes it would run all day without a failure.
Last year when I started using GCC 12 to build the kernel, I saw the same workload fail again but the failure mode had become a silent hang/livelock instead of the oopses I got with GCC 6.
Quoted 7 lines
When I press the NMI button after the livelock I always see
do_page_fault()in the backtrace. So I've been testing your patch. I've been running the same stress-ng reproducer for about 12 hours now with no failures which looks promising.In case that stress-ng testing is of use:
Tested-by Finn Thain <[email protected]>
Quoted 9 lines
Could you test the thing that Mark Rutland pointed to? He had an actual test-case for this for the arm64 fixes some years ago.
See
https://lore.kernel.org/all/Y9pD+TMP+%2FSyfeJm@FVFF77S0Q05N/for his email with links to his old test-case?
With qemu-system-m68k v7.2 runing Linux v6.1, killing Mark's program did not trigger any failure.
With my Quadra 650 running the same binaries, killing Mark's program produces the BUG splat below. I can confirm that Al's patch fixes it.
[ 199.060000] BUG: scheduling while atomic: test/48/0x0003e2e6 [ 199.060000] Modules linked in: [ 199.060000] CPU: 0 PID: 48 Comm: test Tainted: G W 6.1.0-mac #3 [ 199.060000] Stack from 00a6204c: [ 199.060000] 00a6204c 004c1979 004c1979 00000000 00a6215e 00a6206c 00408d62 004c1979 [ 199.060000] 00a62074 0003ad4a 00a620a8 004099e2 00983480 00000000 00a6215e 00000000 [ 199.060000] 00000002 00983480 00a62000 0040970a 00a6209c 00a6209c 00a620c0 00a620c0 [ 199.060000] 00409b94 00000000 00bcf0c0 00a62198 00a357fc 00a6216c 00120e5c 00bcf0d0 [ 199.060000] 00000003 00000001 00000001 0123f039 00000000 00a357fc 00aead38 005870dc [ 199.060000] 005870dc 00a31200 00000000 00000000 00000000 00010000 0000c000 00000000 [ 199.060000] Call Trace: [<00408d62>] dump_stack+0x10/0x16 [ 199.060000] [<0003ad4a>] __schedule_bug+0x5e/0x70 [ 199.060000] [<004099e2>] __schedule+0x2d8/0x446 [ 199.060000] [<0040970a>] __schedule+0x0/0x446 [ 199.060000] [<00409b94>] schedule+0x44/0x8e [ 199.060000] [<00120e5c>] handle_userfault+0x298/0x3de [ 199.060000] [<00010000>] zer_rp2+0x14/0x18 [ 199.060000] [<0000c000>] cu_dmrs+0x0/0x16 [ 199.060000] [<00001200>] kernel_pg_dir+0x200/0x1000 [ 199.060000] [<00010000>] zer_rp2+0x14/0x18 [ 199.060000] [<0000c000>] cu_dmrs+0x0/0x16 [ 199.060000] [<00001200>] kernel_pg_dir+0x200/0x1000 [ 199.060000] [<00010000>] zer_rp2+0x14/0x18 [ 199.060000] [<0000c000>] cu_dmrs+0x0/0x16 [ 199.060000] [<000ab0a0>] handle_mm_fault+0xa34/0xa56 [ 199.060000] [<000c0000>] __alloc_pages_bulk+0x26/0x3f8 [ 199.060000] [<00007056>] do_page_fault+0xd8/0x28a [ 199.060000] [<00006098>] buserr_c+0x1a6/0x6b0 [ 199.060000] [<000056fc>] do_040writeback1+0x0/0x1d8 [ 199.060000] [<00002ac0>] buserr+0x20/0x28 [ 199.060000] [<000062cc>] buserr_c+0x3da/0x6b0 [ 199.060000] [<000056fc>] do_040writeback1+0x0/0x1d8 [ 199.060000] [<00002ac0>] buserr+0x20/0x28 [ 199.060000] [<000062cc>] buserr_c+0x3da/0x6b0 [ 199.060000] [<000056fc>] do_040writeback1+0x0/0x1d8 [ 199.060000] [<00002ac0>] buserr+0x20/0x28 [ 199.060000] [<000062cc>] buserr_c+0x3da/0x6b0 [ 199.060000] [<000056fc>] do_040writeback1+0x0/0x1d8 [ 199.060000] [<00002ac0>] buserr+0x20/0x28 [ 199.060000] [<000062cc>] buserr_c+0x3da/0x6b0 [ 199.060000] [<000056fc>] do_040writeback1+0x0/0x1d8 [ 199.060000] [<00002ac0>] buserr+0x20/0x28 [ 199.060000] [<000062cc>] buserr_c+0x3da/0x6b0 [ 199.060000] [<000056fc>] do_040writeback1+0x0/0x1d8 [ 199.060000] [<00002ac0>] buserr+0x20/0x28 [ 199.060000] [<000062cc>] buserr_c+0x3da/0x6b0 [ 199.060000] [<000056fc>] do_040writeback1+0x0/0x1d8 [ 199.060000] [<00002ac0>] buserr+0x20/0x28 [ 199.060000] [<000062cc>] buserr_c+0x3da/0x6b0 [ 199.060000] [<000056fc>] do_040writeback1+0x0/0x1d8 [ 199.060000] [<00002ac0>] buserr+0x20/0x28 [ 199.060000] [<000062cc>] buserr_c+0x3da/0x6b0 [ 199.060000] [<000056fc>] do_040writeback1+0x0/0x1d8 [ 199.060000] [<00002ac0>] buserr+0x20/0x28 [ 199.060000] [<000062cc>] buserr_c+0x3da/0x6b0 [ 199.060000] [<000056fc>] do_040writeback1+0x0/0x1d8 [ 199.060000] [<00002ac0>] buserr+0x20/0x28 [ 199.060000] [<000062cc>] buserr_c+0x3da/0x6b0 [ 199.060000] [<000056fc>] do_040writeback1+0x0/0x1d8 [ 199.060000] [<00002ac0>] buserr+0x20/0x28 [ 199.060000] [<000062cc>] buserr_c+0x3da/0x6b0 [ 199.060000] [<000056fc>] do_040writeback1+0x0/0x1d8 [ 199.060000] [<00002ac0>] buserr+0x20/0x28 [ 199.060000] [<000062cc>] buserr_c+0x3da/0x6b0 [ 199.060000] [<000056fc>] do_040writeback1+0x0/0x1d8 [ 199.060000] [<00002ac0>] buserr+0x20/0x28 [ 199.060000] [<000062cc>] buserr_c+0x3da/0x6b0 [ 199.060000] [<000056fc>] do_040writeback1+0x0/0x1d8 [ 199.060000] [<00002ac0>] buserr+0x20/0x28 [ 199.060000] [<000062cc>] buserr_c+0x3da/0x6b0 [ 199.060000] [<000056fc>] do_040writeback1+0x0/0x1d8 [ 199.060000] [<00002ac0>] buserr+0x20/0x28 [ 199.060000] [<000062cc>] buserr_c+0x3da/0x6b0 [ 199.060000] [<000056fc>] do_040writeback1+0x0/0x1d8 [ 199.060000] [<00002ac0>] buserr+0x20/0x28 [ 199.060000] [<000062cc>] buserr_c+0x3da/0x6b0 [ 199.060000] [<000056fc>] do_040writeback1+0x0/0x1d8 [ 199.060000] [<00002ac0>] buserr+0x20/0x28 [ 199.060000] [<000062cc>] buserr_c+0x3da/0x6b0 [ 199.060000] [<000056fc>] do_040writeback1+0x0/0x1d8 [ 199.060000] [<00002ac0>] buserr+0x20/0x28 [ 199.060000] [<000062cc>] buserr_c+0x3da/0x6b0 [ 199.060000] [<000056fc>] do_040writeback1+0x0/0x1d8 [ 199.060000] [<00002ac0>] buserr+0x20/0x28 [ 199.060000] [<000062cc>] buserr_c+0x3da/0x6b0 [ 199.060000] [<000056fc>] do_040writeback1+0x0/0x1d8 [ 199.060000] [<00002ac0>] buserr+0x20/0x28 [ 199.060000] [<000062cc>] buserr_c+0x3da/0x6b0 [ 199.060000] [<000056fc>] do_040writeback1+0x0/0x1d8 [ 199.060000] [<00002ac0>] buserr+0x20/0x28 [ 199.060000] [<000062cc>] buserr_c+0x3da/0x6b0 [ 199.060000] [<000056fc>] do_040writeback1+0x0/0x1d8 [ 199.060000] [<00002ac0>] buserr+0x20/0x28 [ 199.060000] [<000062cc>] buserr_c+0x3da/0x6b0 [ 199.060000] [<000056fc>] do_040writeback1+0x0/0x1d8 [ 199.060000] [<00002ac0>] buserr+0x20/0x28 [ 199.060000] [<000062cc>] buserr_c+0x3da/0x6b0 [ 199.060000] [<000056fc>] do_040writeback1+0x0/0x1d8 [ 199.060000] [<00002ac0>] buserr+0x20/0x28 [ 199.060000] [<000062cc>] buserr_c+0x3da/0x6b0 [ 199.060000] [<000056fc>] do_040writeback1+0x0/0x1d8 [ 199.060000] [<00002ac0>] buserr+0x20/0x28 [ 199.060000] [<000062cc>] buserr_c+0x3da/0x6b0 [ 199.060000] [<000056fc>] do_040writeback1+0x0/0x1d8 [ 199.060000] [<00002ac0>] buserr+0x20/0x28 [ 199.060000] [<000062cc>] buserr_c+0x3da/0x6b0 [ 199.060000] [<000056fc>] do_040writeback1+0x0/0x1d8 [ 199.060000] [<00002ac0>] buserr+0x20/0x28 [ 199.060000] [<000062cc>] buserr_c+0x3da/0x6b0 [ 199.060000] [<000056fc>] do_040writeback1+0x0/0x1d8 [ 199.060000] [<00002ac0>] buserr+0x20/0x28 [ 199.060000] [<000062cc>] buserr_c+0x3da/0x6b0 [ 199.060000] [<000056fc>] do_040writeback1+0x0/0x1d8 [ 199.060000] [<00002ac0>] buserr+0x20/0x28 [ 199.060000] [<000062cc>] buserr_c+0x3da/0x6b0 [ 199.060000] [<000056fc>] do_040writeback1+0x0/0x1d8 [ 199.060000] [<00002ac0>] buserr+0x20/0x28 [ 199.060000] [<000062cc>] buserr_c+0x3da/0x6b0 [ 199.060000] [<000056fc>] do_040writeback1+0x0/0x1d8 [ 199.060000] [<00002ac0>] buserr+0x20/0x28 [ 199.060000] [<000062cc>] buserr_c+0x3da/0x6b0 [ 199.060000] [<000056fc>] do_040writeback1+0x0/0x1d8 [ 199.060000] [<00002ac0>] buserr+0x20/0x28 [ 199.060000] [<000062cc>] buserr_c+0x3da/0x6b0 [ 199.060000] [<000056fc>] do_040writeback1+0x0/0x1d8 [ 199.060000] [<00002ac0>] buserr+0x20/0x28 [ 199.060000] [<000062cc>] buserr_c+0x3da/0x6b0 [ 199.060000] [<000056fc>] do_040writeback1+0x0/0x1d8 [ 199.060000] [<00002ac0>] buserr+0x20/0x28 [ 199.060000] [<000062cc>] buserr_c+0x3da/0x6b0 [ 199.060000] [<000056fc>] do_040writeback1+0x0/0x1d8 [ 199.060000] [<00002ac0>] buserr+0x20/0x28 [ 199.060000] [<000062cc>] buserr_c+0x3da/0x6b0 [ 199.060000] [<000056fc>] do_040writeback1+0x0/0x1d8 [ 199.060000] [<00002ac0>] buserr+0x20/0x28 [ 199.060000] [<000062cc>] buserr_c+0x3da/0x6b0 [ 199.060000] [<000056fc>] do_040writeback1+0x0/0x1d8 [ 199.060000] [<00002ac0>] buserr+0x20/0x28 [ 199.060000] [<000062cc>] buserr_c+0x3da/0x6b0 [ 199.060000] [<000056fc>] do_040writeback1+0x0/0x1d8 [ 199.060000] [<00002ac0>] buserr+0x20/0x28 [ 199.060000] [<000062cc>] buserr_c+0x3da/0x6b0 [ 199.060000] [<000056fc>] do_040writeback1+0x0/0x1d8 [ 199.060000] [<00002ac0>] buserr+0x20/0x28 [ 199.060000] [<000062cc>] buserr_c+0x3da/0x6b0 [ 199.060000] [<00001000>] kernel_pg_dir+0x0/0x1000 [ 199.060000] [<00002ac0>] buserr+0x20/0x28 [ 199.060000] [<00001000>] kernel_pg_dir+0x0/0x1000 [ 199.060000] [<00008001>] mac_irq_disable+0x3b/0x98 [ 199.060000] [<00001000>] kernel_pg_dir+0x0/0x1000 [ 199.060000] [<00008001>] mac_irq_disable+0x3b/0x98 [ 199.060000]
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Re: [PATCH 04/10] m68k: fix livelock in uaccess
In reply to Finn ThainQuoted 13 lines · Finn Thain
That could be a bug I was chasing back in 2021 but never found. The mmap stressors in stress-ng were triggering a crash on a Mac Quadras, though only rarely. Sometimes it would run all day without a failure.
Last year when I started using GCC 12 to build the kernel, I saw the same workload fail again but the failure mode had become a silent hang/livelock instead of the oopses I got with GCC 6.
When I press the NMI button after the livelock I always see
do_page_fault()in the backtrace. So I've been testing your patch. I've been running the same stress-ng reproducer for about 12 hours now with no failures which looks promising.
Quoted 6 lines
In case that stress-ng testing is of use:
Tested-by Finn Thain <[email protected]>BTW, how did you identify that bug in
do_page_fault()? If its the same bug I was chasing, it could be an old one. The stress-ng logs I collected last year include a crash from a v4.14 build.
Went to reread the current state of mm/gup.c, decided to reread handle_mm_fault() and its callers, noticed fault_signal_pending() which hadn't been there back when I last crawled through that area, realized what it had replaced, went to check if everything had been converted (arch/um got missed, BTW). Noticed the difference between the architectures (the first hit was on alpha, without the "sod off to no_context if it's a user fault" logics, the last - xtensa, with it). Checked the log for xtensa, found the commit from 2021 adding that part; looked on arm and arm64, found commits from 2017 doing the same thing, then, on x86, Linus' commit from 2014 adding the x86 counterpart... Figuring out what all of those had been for wasn't particularly hard, and it was easy to check which architectures still needed the same thing...
BTW, since these patches would be much easier to backport than any unification work, I think the right thing to do would be to have further unification done on top of them.
Re: [PATCH 04/10] m68k: fix livelock in uaccess
Quoted 4 lines · Al Viro
BTW, since these patches would be much easier to backport than any unification work, I think the right thing to do would be to have further unification done on top of them.
Ack. I'm not NAKing the patches, I was just hoping that we also have some way forward.
So "fix the issues, then unify" sounds like the right thing to do to me.
Linus
Re: [PATCH 04/10] m68k: fix livelock in uaccess
In reply to Al ViroQuoted 8 lines · Al Viro
m68k equivalent of
26178ec11ef3"x86: mm: consolidateVM_FAULT_RETRYhandling" If e.g.get_user()triggers a page fault and a fatal signal is caught, we might end up withhandle_mm_fault()returningVM_FAULT_RETRYand not doing anything to page tables. In such case we must *not* return to the faulting insn - that would repeat the entire thing without making any progress; what we need instead is to treat that as failed (user) memory access.Signed-off-by Al Viro <[email protected]>
Gr{oetje,eeting}s,
Geert
--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]
In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds
PATCH 5/10 microblaze: fix livelock in uaccess
In reply to Al Viromicroblaze equivalent of 26178ec11ef3 "x86: mm: consolidate VM_FAULT_RETRY handling" If e.g. get_user() triggers a page fault and a fatal signal is caught, we might end up with handle_mm_fault() returning VM_FAULT_RETRY and not doing anything to page tables. In such case we must *not* return to the faulting insn - that would repeat the entire thing without making any progress; what we need instead is to treat that as failed (user) memory access.
--- arch/microblaze/mm/fault.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
arch/microblaze/mm/fault.c +4 -1
@@ -219,8 +219,11 @@ void do_page_fault(struct pt_regs *regs, unsigned long address,*/ fault =handle_mm_fault (vma, address, flags, regs); -if (fault_signal_pending (fault, regs)) +if (fault_signal_pending (fault, regs)) { +if (!user_mode (regs)) + goto no_context;return ; + }/* The fault is fully completed (including releasing mmap lock) */ if (fault &VM_FAULT_COMPLETED )
Signature · 1 line
2.30.2
PATCH 6/10 nios2: fix livelock in uaccess
In reply to Al Vironios2 equivalent of 26178ec11ef3 "x86: mm: consolidate VM_FAULT_RETRY handling" If e.g. get_user() triggers a page fault and a fatal signal is caught, we might end up with handle_mm_fault() returning VM_FAULT_RETRY and not doing anything to page tables. In such case we must *not* return to the faulting insn - that would repeat the entire thing without making any progress; what we need instead is to treat that as failed (user) memory access.
--- arch/nios2/mm/fault.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
arch/nios2/mm/fault.c +4 -1
@@ -136,8 +136,11 @@ asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long cause,*/ fault =handle_mm_fault (vma, address, flags, regs); -if (fault_signal_pending (fault, regs)) +if (fault_signal_pending (fault, regs)) { +if (!user_mode (regs)) + goto no_context;return ; + }/* The fault is fully completed (including releasing mmap lock) */ if (fault &VM_FAULT_COMPLETED )
Signature · 1 line
2.30.2
PATCH 7/10 openrisc: fix livelock in uaccess
In reply to Al Viroopenrisc equivalent of 26178ec11ef3 "x86: mm: consolidate VM_FAULT_RETRY handling" If e.g. get_user() triggers a page fault and a fatal signal is caught, we might end up with handle_mm_fault() returning VM_FAULT_RETRY and not doing anything to page tables. In such case we must *not* return to the faulting insn - that would repeat the entire thing without making any progress; what we need instead is to treat that as failed (user) memory access.
--- arch/openrisc/mm/fault.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
arch/openrisc/mm/fault.c +4 -1
@@ -162,8 +162,11 @@ asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long address, fault =handle_mm_fault (vma, address, flags, regs); -if (fault_signal_pending (fault, regs)) +if (fault_signal_pending (fault, regs)) { +if (!user_mode (regs)) + goto no_context;return ; + }/* The fault is fully completed (including releasing mmap lock) */ if (fault &VM_FAULT_COMPLETED )
Signature · 1 line
2.30.2
PATCH 8/10 parisc: fix livelock in uaccess
In reply to Al Viroparisc equivalent of 26178ec11ef3 "x86: mm: consolidate VM_FAULT_RETRY handling" If e.g. get_user() triggers a page fault and a fatal signal is caught, we might end up with handle_mm_fault() returning VM_FAULT_RETRY and not doing anything to page tables. In such case we must *not* return to the faulting insn - that would repeat the entire thing without making any progress; what we need instead is to treat that as failed (user) memory access.
--- arch/parisc/mm/fault.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
arch/parisc/mm/fault.c +4 -1
@@ -308,8 +308,11 @@ void do_page_fault(struct pt_regs *regs, unsigned long code, fault =handle_mm_fault (vma, address, flags, regs); -if (fault_signal_pending (fault, regs)) +if (fault_signal_pending (fault, regs)) { +if (!user_mode (regs)) + goto no_context;return ; + }/* The fault is fully completed (including releasing mmap lock) */ if (fault &VM_FAULT_COMPLETED )
Signature · 1 line
2.30.2
Helge Deller
maintainerRe: [PATCH 08/10] parisc: fix livelock in uaccess
Hi Al,
Quoted 17 lines · Al Viro
parisc equivalent of
26178ec11ef3"x86: mm: consolidateVM_FAULT_RETRYhandling" If e.g.get_user()triggers a page fault and a fatal signal is caught, we might end up withhandle_mm_fault()returningVM_FAULT_RETRYand not doing anything to page tables. In such case we must *not* return to the faulting insn - that would repeat the entire thing without making any progress; what we need instead is to treat that as failed (user) memory access.Signed-off-by Al Viro <[email protected]>--- arch/parisc/mm/fault.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)arch/parisc/mm/fault.c +0
-0
arch/parisc/mm/fault.c void do_page_fault(struct pt_regs *regs, unsigned long code, · line 315
Quoted 9 lines
arch/parisc/mm/fault.c +4
-1@@ -308,8 +308,11 @@ void do_page_fault(struct pt_regs *regs, unsigned long code, fault =handle_mm_fault (vma, address, flags, regs); -if (fault_signal_pending (fault, regs)) +if (fault_signal_pending (fault, regs)) { +if (!user_mode (regs)) + goto no_context;return ; + }
The testcase in
https://lore.kernel.org/lkml/20170822102527.GA14671@leverpostej/
https://lore.kernel.org/linux-arch/[email protected]/
does hang with and without above patch on parisc.
It does not consume CPU in that state and can be killed with ^C.
Any idea?
Helge
Re: [PATCH 08/10] parisc: fix livelock in uaccess
Quoted 36 lines · Helge Deller
Hi Al,
Quoted 17 lines · Al Viro
parisc equivalent of
26178ec11ef3"x86: mm: consolidateVM_FAULT_RETRYhandling" If e.g.get_user()triggers a page fault and a fatal signal is caught, we might end up withhandle_mm_fault()returningVM_FAULT_RETRYand not doing anything to page tables. In such case we must *not* return to the faulting insn - that would repeat the entire thing without making any progress; what we need instead is to treat that as failed (user) memory access.Signed-off-by Al Viro <[email protected]>--- arch/parisc/mm/fault.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)arch/parisc/mm/fault.c +0
-0arch/parisc/mm/fault.c void do_page_fault(struct pt_regs *regs, unsigned long code, · line 315
Quoted 9 lines
arch/parisc/mm/fault.c +4
-1@@ -308,8 +308,11 @@ void do_page_fault(struct pt_regs *regs, unsigned long code, fault =handle_mm_fault (vma, address, flags, regs); -if (fault_signal_pending (fault, regs)) +if (fault_signal_pending (fault, regs)) { +if (!user_mode (regs)) + goto no_context;return ; + }The testcase in
https://lore.kernel.org/lkml/20170822102527.GA14671@leverpostej/
https://lore.kernel.org/linux-arch/[email protected]/
does hang with and without above patch on parisc.
It does not consume CPU in that state and can be killed with ^C.
Quoted 1 line
Any idea?
Still trying to resurrect the parisc box to test on it...
FWIW, right now I've locally confirmed that mainline has the bug in question and that patch fixes it for alpha, sparc32 and sparc64; hexagon, m68k and riscv got acks from other folks; microblaze, nios2 and openrisc I can't test at all (no hardware, no qemu setup); same for parisc64. Itanic and parisc32 I might be able to test, if I manage to resurrect the hardware.
Just to confirm: your "can be killed with ^C" had been on the
mainline parisc kernel (with userfaultfd enable, of course, or it wouldn't hang up at all), right? Was it 32bit or 64bit kernel?
Helge Deller
maintainerRe: [PATCH 08/10] parisc: fix livelock in uaccess
Quoted 39 lines · Al Viro
Quoted 36 lines · Helge Deller
Hi Al,
Quoted 17 lines · Al Viro
parisc equivalent of
26178ec11ef3"x86: mm: consolidateVM_FAULT_RETRYhandling" If e.g.get_user()triggers a page fault and a fatal signal is caught, we might end up withhandle_mm_fault()returningVM_FAULT_RETRYand not doing anything to page tables. In such case we must *not* return to the faulting insn - that would repeat the entire thing without making any progress; what we need instead is to treat that as failed (user) memory access.Signed-off-by Al Viro <[email protected]>--- arch/parisc/mm/fault.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)arch/parisc/mm/fault.c +0
-0arch/parisc/mm/fault.c void do_page_fault(struct pt_regs *regs, unsigned long code, · line 315
Quoted 9 lines
arch/parisc/mm/fault.c +4
-1@@ -308,8 +308,11 @@ void do_page_fault(struct pt_regs *regs, unsigned long code, fault =handle_mm_fault (vma, address, flags, regs); -if (fault_signal_pending (fault, regs)) +if (fault_signal_pending (fault, regs)) { +if (!user_mode (regs)) + goto no_context;return ; + }The testcase in
https://lore.kernel.org/lkml/20170822102527.GA14671@leverpostej/
https://lore.kernel.org/linux-arch/[email protected]/
does hang with and without above patch on parisc.
It does not consume CPU in that state and can be killed with ^C.Quoted 1 line
Any idea?
Quoted 7 lines
Still trying to resurrect the parisc box to test on it...
FWIW, right now I've locally confirmed that mainline has the bug in question and that patch fixes it for alpha, sparc32 and sparc64; hexagon, m68k and riscv got acks from other folks; microblaze, nios2 and openrisc I can't test at all (no hardware, no qemu setup); same for parisc64. Itanic and parisc32 I might be able to test, if I manage to resurrect the hardware.
I can test both parisc32 and parisc64.
Quoted 3 lines
Just to confirm: your "can be killed with ^C" had been on the
mainline parisc kernel (with userfaultfd enable, of course, or it wouldn't hang up at all), right?
It was a recent mainline kernel with your patch.
Quoted 1 line
Was it 32bit or 64bit kernel?
I don't remember. I think I tried both.
Helge
Re: [PATCH 08/10] parisc: fix livelock in uaccess
Quoted 7 lines · Helge Deller
I can test both parisc32 and parisc64.
Quoted 3 lines
Just to confirm: your "can be killed with ^C" had been on the
mainline parisc kernel (with userfaultfd enable, of course, or it wouldn't hang up at all), right?It was a recent mainline kernel with your patch.
Er... Reproducer *is* supposed to block; the bug is that on the kernel without this patch it would (AFAICS) be impossible to kill - not even with kill -9. With bug fixed the behaviour should be "blocks and is killable", i.e. what you've reported.
What does it do on unpatched kernel? *IF* the big is there, it would block and be unkillable by any means. Could you verify that?
Helge Deller
maintainerRe: [PATCH 08/10] parisc: fix livelock in uaccess
Hi Al,
Quoted 10 lines · Al Viro
Quoted 7 lines · Helge Deller
I can test both parisc32 and parisc64.
Quoted 3 lines
Just to confirm: your "can be killed with ^C" had been on the
mainline parisc kernel (with userfaultfd enable, of course, or it wouldn't hang up at all), right?It was a recent mainline kernel with your patch.
Quoted 7 lines
Er... Reproducer *is* supposed to block; the bug is that on the kernel without this patch it would (AFAICS) be impossible to kill - not even with kill -9. With bug fixed the behaviour should be "blocks and is killable", i.e. what you've reported.
What does it do on unpatched kernel? *IF* the big is there, it would block and be unkillable by any means. Could you verify that?
I just tried 32- and 64-bit kernels.
With and without your patch on top of kernel 6.2.
The result is always the same:
The process hangs, but does not consume CPU and can be killed with ^C or kill command.
strace output is:
userfaultfd(0) = 3
ioctl(3, UFFDIO_API, {api=0xaa, features=0 => features=UFFD_FEATURE_EVENT_FORK|UFFD_FEATURE_EVENT_REMAP|UFFD_FEATURE_EVENT_REMOVE|UFFD_FEATURE_MISSING_HUGETLBFS|UFFD_FEATURE_MISSING_SHMEM|UFFD_FEATURE_EVENT_UNMAP|UFFD_FEATURE_SIGBUS|UFFD_FEATURE_THREAD_ID|0x800, ioctls=1<<_UFFDIO_REGISTER|1<<_UFFDIO_UNREGISTER|1<<_UFFDIO_API}) = 0 ioctl(3, UFFDIO_REGISTER, {range={start=0xf7afa000, len=0x1000}, mode=UFFDIO_REGISTER_MODE_MISSING, ioctls=1<<_UFFDIO_WAKE|1<<_UFFDIO_COPY|1<<_UFFDIO_ZEROPAGE}) = 0
fstatfs64(0, 88, {f_type=DEVPTS_SUPER_MAGIC, f_bsize=4096, f_blocks=0, f_bfree=0, f_bavail=0, f_files=0, f_ffree=0, f_fsid={val=[0, 0]}, f_namelen=255, f_frsize=4096, f_flags=ST_VALID|ST_NOSUID|ST_NOEXEC|ST_RELATIME}) = 0
<here it hangs until I kill the process with SIGTERM>
--- SIGTERM {si_signo=SIGTERM, si_code=SI_USER, si_pid=859, si_uid=0} --- +++ killed by SIGTERM +++
Terminated
It seems the code flow doesn't reaches fault_signal_pending(), or it returns false...
Helge
Helge Deller
maintainerRe: [PATCH 08/10] parisc: fix livelock in uaccess
Quoted 26 lines · Helge Deller
Hi Al,
Quoted 10 lines · Al Viro
Quoted 7 lines · Helge Deller
I can test both parisc32 and parisc64.
Quoted 3 lines
Just to confirm: your "can be killed with ^C" had been on the mainline parisc kernel (with userfaultfd enable, of course, or it wouldn't hang up at all), right?
It was a recent mainline kernel with your patch.
Quoted 7 lines
Er... Reproducer *is* supposed to block; the bug is that on the kernel without this patch it would (AFAICS) be impossible to kill - not even with kill -9. With bug fixed the behaviour should be "blocks and is killable", i.e. what you've reported.
What does it do on unpatched kernel? *IF* the big is there, it would block and be unkillable by any means. Could you verify that?
I just tried 32- and 64-bit kernels.
With and without your patch on top of kernel 6.2.
The result is always the same:
The process hangs, but does not consume CPU and can be killed with ^C or kill command.
Quoted 6 lines
strace output is:
userfaultfd(0) = 3
ioctl(3,UFFDIO_API, {api=0xaa, features=0 => features=UFFD_FEATURE_EVENT_FORK|UFFD_FEATURE_EVENT_REMAP|UFFD_FEATURE_EVENT_REMOVE|UFFD_FEATURE_MISSING_HUGETLBFS|UFFD_FEATURE_MISSING_SHMEM|UFFD_FEATURE_EVENT_UNMAP|UFFD_FEATURE_SIGBUS|UFFD_FEATURE_THREAD_ID|0x800, ioctls=1<<_UFFDIO_REGISTER|1<<_UFFDIO_UNREGISTER|1<<_UFFDIO_API}) = 0 ioctl(3,UFFDIO_REGISTER, {range={start=0xf7afa000, len=0x1000}, mode=UFFDIO_REGISTER_MODE_MISSING, ioctls=1<<_UFFDIO_WAKE|1<<_UFFDIO_COPY|1<<_UFFDIO_ZEROPAGE}) = 0
fstatfs64(0, 88, {f_type=DEVPTS_SUPER_MAGIC, f_bsize=4096, f_blocks=0, f_bfree=0, f_bavail=0, f_files=0, f_ffree=0, f_fsid={val=[0, 0]}, f_namelen=255, f_frsize=4096, f_flags=ST_VALID|ST_NOSUID|ST_NOEXEC|ST_RELATIME}) = 0
Ok, one step further...
We get fooled by glibc, which replaces this line in the testcase:
ret = fstatfs(0, (struct statfs *)mem);
and instead executes the 64-bit variant fstatfs64() inside a wrapper in glibc on another address.
I replaced that line in the testcase to use the 32-bit syscall with the right (userfault-specified) address by
ret = syscall(__NR_fstatfs, 0, mem);
and now I see:
userfaultfd(0) = 3
ioctl(3, UFFDIO_API, {api=0xaa, features=0 => features=UFFD_FEATURE_EVENT_FORK|UFFD_FEATURE_EVENT_REMAP|UFFD_FEATURE_EVENT_REMOVE|UFFD_FEATURE_MISSING_HUGETLBFS|UFFD_FEATURE_MISSING_SHMEM|UFFD_FEATURE_EVENT_UNMAP|UFFD_FEATURE_SIGBUS|UFFD_FEATURE_THREAD_ID|0x800, ioctls=1<<_UFFDIO_REGISTER|1<<_UFFDIO_UNREGISTER|1<<_UFFDIO_API}) = 0 ioctl(3, UFFDIO_REGISTER, {range={start=0xf7afa000, len=0x1000}, mode=UFFDIO_REGISTER_MODE_MISSING, ioctls=1<<_UFFDIO_WAKE|1<<_UFFDIO_COPY|1<<_UFFDIO_ZEROPAGE}) = 0
fstatfs(0,
<here it hangs now and is unkillable>
Needs further testing.
Helge
Re: [PATCH 08/10] parisc: fix livelock in uaccess
Hi Al,
Quoted 49 lines · Helge Deller
Quoted 26 lines · Helge Deller
Hi Al,
Quoted 10 lines · Al Viro
Quoted 7 lines · Helge Deller
I can test both parisc32 and parisc64.
Quoted 3 lines
Just to confirm: your "can be killed with ^C" had been on the mainline parisc kernel (with userfaultfd enable, of course, or it wouldn't hang up at all), right?
It was a recent mainline kernel with your patch.
Quoted 7 lines
Er... Reproducer *is* supposed to block; the bug is that on the kernel without this patch it would (AFAICS) be impossible to kill - not even with kill -9. With bug fixed the behaviour should be "blocks and is killable", i.e. what you've reported.
What does it do on unpatched kernel? *IF* the big is there, it would block and be unkillable by any means. Could you verify that?
I just tried 32- and 64-bit kernels.
With and without your patch on top of kernel 6.2.
The result is always the same:
The process hangs, but does not consume CPU and can be killed with ^C or kill command.Quoted 6 lines
strace output is:
userfaultfd(0) = 3
ioctl(3,UFFDIO_API, {api=0xaa, features=0 => features=UFFD_FEATURE_EVENT_FORK|UFFD_FEATURE_EVENT_REMAP|UFFD_FEATURE_EVENT_REMOVE|UFFD_FEATURE_MISSING_HUGETLBFS|UFFD_FEATURE_MISSING_SHMEM|UFFD_FEATURE_EVENT_UNMAP|UFFD_FEATURE_SIGBUS|UFFD_FEATURE_THREAD_ID|0x800, ioctls=1<<_UFFDIO_REGISTER|1<<_UFFDIO_UNREGISTER|1<<_UFFDIO_API}) = 0 ioctl(3,UFFDIO_REGISTER, {range={start=0xf7afa000, len=0x1000}, mode=UFFDIO_REGISTER_MODE_MISSING, ioctls=1<<_UFFDIO_WAKE|1<<_UFFDIO_COPY|1<<_UFFDIO_ZEROPAGE}) = 0
fstatfs64(0, 88, {f_type=DEVPTS_SUPER_MAGIC, f_bsize=4096, f_blocks=0, f_bfree=0, f_bavail=0, f_files=0, f_ffree=0, f_fsid={val=[0, 0]}, f_namelen=255, f_frsize=4096, f_flags=ST_VALID|ST_NOSUID|ST_NOEXEC|ST_RELATIME}) = 0Ok, one step further...
We get fooled by glibc, which replaces this line in the testcase:
ret =fstatfs(0, (struct statfs *)mem);
and instead executes the 64-bit variantfstatfs64()inside a wrapper in glibc on another address.I replaced that line in the testcase to use the 32-bit syscall with the right (userfault-specified) address by
ret =syscall(__NR_fstatfs, 0, mem);
and now I see:
userfaultfd(0) = 3
ioctl(3,UFFDIO_API, {api=0xaa, features=0 => features=UFFD_FEATURE_EVENT_FORK|UFFD_FEATURE_EVENT_REMAP|UFFD_FEATURE_EVENT_REMOVE|UFFD_FEATURE_MISSING_HUGETLBFS|UFFD_FEATURE_MISSING_SHMEM|UFFD_FEATURE_EVENT_UNMAP|UFFD_FEATURE_SIGBUS|UFFD_FEATURE_THREAD_ID|0x800, ioctls=1<<_UFFDIO_REGISTER|1<<_UFFDIO_UNREGISTER|1<<_UFFDIO_API}) = 0 ioctl(3,UFFDIO_REGISTER, {range={start=0xf7afa000, len=0x1000}, mode=UFFDIO_REGISTER_MODE_MISSING, ioctls=1<<_UFFDIO_WAKE|1<<_UFFDIO_COPY|1<<_UFFDIO_ZEROPAGE}) = 0
fstatfs(0,
<here it hangs now and is unkillable>
Quoted 1 line
Needs further testing.
Now I can confirm (with the adjusted reproducer), that your patch allows to kill the process with SIGKILL, while without your patch it's not possibe to kill the process at all.
I've tested with a 32- and 64-bit parisc kernel.
You may add
to the patch.
If you want me to take the patch (with the warning regarding missing msg variable fixed) through the parisc tree, please let me know.
Thank you!
Helge
Re: [PATCH 08/10] parisc: fix livelock in uaccess
Quoted 11 lines · Helge Deller
Now I can confirm (with the adjusted reproducer), that your patch allows to kill the process with SIGKILL, while without your patch it's not possibe to kill the process at all.
I've tested with a 32- and 64-bit parisc kernel.You may add
to the patch.
If you want me to take the patch (with the warning regarding missing msg variable fixed) through the parisc tree, please let me know.
What message do you prefer there? It matters only for the case when we are hitting an oops there, but...
Helge Deller
maintainerRe: [PATCH 08/10] parisc: fix livelock in uaccess
Quoted 11 lines · Al Viro
Quoted 9 lines · Helge Deller
Now I can confirm (with the adjusted reproducer), that your patch allows to kill the process with SIGKILL, while without your patch it's not possibe to kill the process at all.
I've tested with a 32- and 64-bit parisc kernel.You may add
to the patch.
Quoted 5 lines
Quoted 2 lines
If you want me to take the patch (with the warning regarding missing msg variable fixed) through the parisc tree, please let me know.
What message do you prefer there? It matters only for the case when we are hitting an oops there, but...
I have no preference on that message. Maybe "Page fault: fault signal on kernel memory" is too generic, so I'm fine with any better idea/wording you come up with.
Helge
Re: [PATCH 08/10] parisc: fix livelock in uaccess
Quoted 12 lines · Helge Deller
Quoted 11 lines · Al Viro
Quoted 9 lines · Helge Deller
Now I can confirm (with the adjusted reproducer), that your patch allows to kill the process with SIGKILL, while without your patch it's not possibe to kill the process at all.
I've tested with a 32- and 64-bit parisc kernel.You may add
to the patch.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
If you want me to take the patch (with the warning regarding missing msg variable fixed) through the parisc tree, please let me know.
What message do you prefer there? It matters only for the case when we are hitting an oops there, but...
I have no preference on that message. Maybe "Page fault: fault signal on kernel memory" is too generic, so I'm fine with any better idea/wording you come up with.
Ended up going with that... Series reordered and pushed out in #fixes. Status:
m68k, riscv, hexagon, parisc: tested by arch maintainers alpha, sparc: locally tested (both sparc32 and sparc64 parts) ia64, microblaze, nios2, openrisc: builds, but I have no way to test the
resulting kernels (itanic box appears to be pining for fjords and I've no emulator setup for that, the other three I don't even have userland to test with).
Re: [PATCH 08/10] parisc: fix livelock in uaccess
In reply to Al ViroQuoted 17 lines · Al Viro
parisc equivalent of
26178ec11ef3"x86: mm: consolidateVM_FAULT_RETRYhandling" If e.g.get_user()triggers a page fault and a fatal signal is caught, we might end up withhandle_mm_fault()returningVM_FAULT_RETRYand not doing anything to page tables. In such case we must *not* return to the faulting insn - that would repeat the entire thing without making any progress; what we need instead is to treat that as failed (user) memory access.Signed-off-by Al Viro <[email protected]>--- arch/parisc/mm/fault.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)arch/parisc/mm/fault.c +0
-0
arch/parisc/mm/fault.c void do_page_fault(struct pt_regs *regs, unsigned long code, · line 313
Quoted 7 lines
arch/parisc/mm/fault.c +3
-1@@ -308,8 +308,11 @@ void do_page_fault(struct pt_regs *regs, unsigned long code, fault =handle_mm_fault (vma, address, flags, regs); -if (fault_signal_pending (fault, regs)) +if (fault_signal_pending (fault, regs)) { +if (!user_mode (regs)) + goto no_context;
0-day rightfully complains that this leaves 'msg' uninitialized.
arch/parisc/mm/fault.c:427 do_page_fault() error: uninitialized symbol 'msg'
Guenter
Quoted 5 lines
arch/parisc/mm/fault.c +1
-0return ; + }/* The fault is fully completed (including releasing mmap lock) */ if (fault &VM_FAULT_COMPLETED )
Re: [PATCH 08/10] parisc: fix livelock in uaccess
Guenter,
Quoted 27 lines · Guenter Roeck
Quoted 17 lines · Al Viro
parisc equivalent of
26178ec11ef3"x86: mm: consolidateVM_FAULT_RETRYhandling" If e.g.get_user()triggers a page fault and a fatal signal is caught, we might end up withhandle_mm_fault()returningVM_FAULT_RETRYand not doing anything to page tables. In such case we must *not* return to the faulting insn - that would repeat the entire thing without making any progress; what we need instead is to treat that as failed (user) memory access.Signed-off-by Al Viro <[email protected]>--- arch/parisc/mm/fault.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)arch/parisc/mm/fault.c +0
-0arch/parisc/mm/fault.c void do_page_fault(struct pt_regs *regs, unsigned long code, · line 313
Quoted 7 lines
arch/parisc/mm/fault.c +3
-1@@ -308,8 +308,11 @@ void do_page_fault(struct pt_regs *regs, unsigned long code, fault =handle_mm_fault (vma, address, flags, regs); -if (fault_signal_pending (fault, regs)) +if (fault_signal_pending (fault, regs)) { +if (!user_mode (regs)) + goto no_context;0-day rightfully complains that this leaves 'msg' uninitialized.
Quoted 3 lines
arch/parisc/mm/fault.c:427do_page_fault()error: uninitialized symbol 'msg'Guenter
What happens if you initialize msg to "Page fault: no context" right at the start of do_page_fault (and drop the assignment a few lines down as that's now redundant)?
(Wondering if the zero page access on parisc could cause a trip right back into do_page_fault, ad infinitum...)
Cheers,
Michael
Quoted 5 lines
Quoted 5 lines
arch/parisc/mm/fault.c +1
-0return ; + }/* The fault is fully completed (including releasing mmap lock) */ if (fault &VM_FAULT_COMPLETED )
PATCH 9/10 riscv: fix livelock in uaccess
In reply to Al Viroriscv equivalent of 26178ec11ef3 "x86: mm: consolidate VM_FAULT_RETRY handling" If e.g. get_user() triggers a page fault and a fatal signal is caught, we might end up with handle_mm_fault() returning VM_FAULT_RETRY and not doing anything to page tables. In such case we must *not* return to the faulting insn - that would repeat the entire thing without making any progress; what we need instead is to treat that as failed (user) memory access.
--- arch/riscv/mm/fault.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
arch/riscv/mm/fault.c +4 -1
@@ -324,8 +324,11 @@ asmlinkage void do_page_fault(struct pt_regs *regs)* signal first. We do not need to release the mmap_lock because it * would already be released in __lock_page_or_retry in mm/filemap.c. */ -if (fault_signal_pending (fault, regs)) +if (fault_signal_pending (fault, regs)) { +if (!user_mode (regs)) +no_context (regs, addr);return ; + }/* The fault is fully completed (including releasing mmap lock) */ if (fault &VM_FAULT_COMPLETED )
Signature · 1 line
2.30.2
Re: [PATCH 09/10] riscv: fix livelock in uaccess
Quoted 8 lines · Al Viro
riscv equivalent of
26178ec11ef3"x86: mm: consolidateVM_FAULT_RETRYhandling" If e.g.get_user()triggers a page fault and a fatal signal is caught, we might end up withhandle_mm_fault()returningVM_FAULT_RETRYand not doing anything to page tables. In such case we must *not* return to the faulting insn - that would repeat the entire thing without making any progress; what we need instead is to treat that as failed (user) memory access.Signed-off-by Al Viro <[email protected]>
Reproduced with Mark's userland program -- thanks!
Re: [PATCH 09/10] riscv: fix livelock in uaccess
In reply to Al ViroQuoted 8 lines · Al Viro
riscv equivalent of
26178ec11ef3"x86: mm: consolidateVM_FAULT_RETRYhandling" If e.g.get_user()triggers a page fault and a fatal signal is caught, we might end up withhandle_mm_fault()returningVM_FAULT_RETRYand not doing anything to page tables. In such case we must *not* return to the faulting insn - that would repeat the entire thing without making any progress; what we need instead is to treat that as failed (user) memory access.Signed-off-by Al Viro <[email protected]>
Gr{oetje,eeting}s,
Geert
--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]
In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds
PATCH 10/10 sparc: fix livelock in uaccess
In reply to Al Virosparc equivalent of 26178ec11ef3 "x86: mm: consolidate VM_FAULT_RETRY handling" If e.g. get_user() triggers a page fault and a fatal signal is caught, we might end up with handle_mm_fault() returning VM_FAULT_RETRY and not doing anything to page tables. In such case we must *not* return to the faulting insn - that would repeat the entire thing without making any progress; what we need instead is to treat that as failed (user) memory access.
--- arch/sparc/mm/fault_32.c | 5 ++++- arch/sparc/mm/fault_64.c | 7 ++++++- 2 files changed, 10 insertions(+), 2 deletions(-)
arch/sparc/mm/fault_32.c +4 -1
@@ -187,8 +187,11 @@ asmlinkage void do_sparc_fault(struct pt_regs *regs, int text_fault, int write,*/ fault =handle_mm_fault (vma, address, flags, regs); -if (fault_signal_pending (fault, regs)) +if (fault_signal_pending (fault, regs)) { +if (!from_user) + goto no_context;return ; + }/* The fault is fully completed (including releasing mmap lock) */ if (fault &VM_FAULT_COMPLETED )
arch/sparc/mm/fault_64.c +6 -1
@@ -424,8 +424,13 @@ asmlinkage void __kprobes do_sparc64_fault(struct pt_regs *regs) fault =handle_mm_fault (vma, address, flags, regs); -if (fault_signal_pending (fault, regs)) +if (fault_signal_pending (fault, regs)) { +if (regs->tstate &TSTATE_PRIV ) { + insn =get_fault_insn (regs, insn); + goto handle_kernel_fault; + } goto exit_exception; + }/* The fault is fully completed (including releasing mmap lock) */ if (fault &VM_FAULT_COMPLETED )
Signature · 1 line
2.30.2
[ Adding Peter Xu to the participants, he did a lot of "add generic helpers" code a few years ago. Peter, see
https://lore.kernel.org/linux-arch/Y9lz6yk113LmC9SI@ZenIV/
for context ]
Quoted 6 lines · Al Viro
However, it never had been done on a bunch of architectures - the current mainline still has that bug on alpha, hexagon, itanic, m68k, microblaze, nios2, openrisc, parisc, riscv and sparc (both sparc32 and sparc64). Fixes are trivial, but I've no way to test them for most of those architectures.
Gaah. The patches look simple and "trivially correct", but I hate how we duplicate basically the same (complicated) logic across architectures.
Peter improved a bit on things, but I think we could do better.
Yes, all architectures basically have their own special code for special error registers etc, and for various errata and/or special handling for vmalloc addresses of vdso's etc.
And they aren't always translated to the generic flags, looking at alpha, for example, we have code like this:
si_code = SEGV_ACCERR;
if (cause < 0) {
if (!(vma->vm_flags & VM_EXEC))
goto bad_area;
} else if (!cause) {
/* Allow reads even for write-only mappings */
if (!(vma->vm_flags & (VM_READ | VM_WRITE)))
goto bad_area;
} else {
if (!(vma->vm_flags & VM_WRITE))
goto bad_area;
flags |= FAULT_FLAG_WRITE;
}because it uses the alpha internal "cause < 0" logic, instead of having translated it into FAULT_FLAG_INSTRUCTION.
But *if* the alpha code were to just translate it into the FAULT_FLAG_xyz namespace, apretty much *all* of the alpha do_page_fault() could have been then done by a completely generic "generic_page_fault()" that has all of the retry logic, all of the si_code logic, etc etc.
So in a perfect world, we'd only have the special errata handling (*) and the "translate to FAULT_FLAG_xyz" code in the architecture code, and then just call that generic_page_fault() function for what really is pretty much generic.
And then we would never again have these kinds of "architecture got the retry wrong" issues.
Would anybody be interested in trying that? Just converting one or two architectures to a new world order?
But if not, Al's patches all look "obviously fine" to me, but that's because they basically all do the same thing except for that odd special TSTATE_PRIV thing for sparc-64 - why can't that use '!user_mode(regs)' like everybody else?)
Linus
(*) we really have a *lot* of architectures that have gotten 'prefetch' wrong and have errata for prefetch: alpha, arm and x86 all have magic "bogus fault on prefetch" cases.
Quoted 5 lines · Linus Torvalds
But *if* the alpha code were to just translate it into the
FAULT_FLAG_xyznamespace, apretty much *all* of the alphado_page_fault()could have been then done by a completely generic "generic_page_fault()" that has all of the retry logic, all of thesi_codelogic, etc etc.
Umm... What about the semantics of get_user() of unmapped address? Some architectures do quiet EFAULT; some (including alpha) hit the sucker with SIGBUS, no matter what.
Examples:
arm
if (likely(!(fault & (VM_FAULT_ERROR | VM_FAULT_BADMAP | VM_FAULT_BADACCESS))))
return 0;
/*
* If we are in kernel mode at this point, we
* have no context to handle this fault with.
*/
if (!user_mode(regs))
goto no_context;
...
alpha
do_sigbus:
mmap_read_unlock(mm);
/* Send a sigbus, regardless of whether we were in kernel
or user mode. */
force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *) address);
if (!user_mode(regs))
goto no_context;
return;Are we free to modify that behaviour, or is that part of arch-specific ABI?
Quoted 4 lines · Al Viro
Umm... What about the semantics of
get_user()of unmapped address? Some architectures do quiet EFAULT; some (including alpha) hit the sucker with SIGBUS, no matter what.
I think we should strive to just make this all common.
The reason alpha is different is almost certainly not intentional, but a combination of "pure accident" and "nobody actually cares".
Quoted 2 lines
Are we free to modify that behaviour, or is that part of arch-specific ABI?
I'd just unify this all, probably with a preference for existing semantics on x86 (because of "biggest and most varied user base").
That whole "send SIGBUS even for kernel faults" is certainly bogus and against the usual rules. And I may well be to blame for it (I have this memory of disliking how EFAULT as a return code didn't actually return the faulting address). And realistically, it's also just not something that any normal application will ever hit. Giving invalid addresses to system calls is basically always a bug, although there are always special software that do all the crazy corner cases (ie things like emulators tend to do odd things).
I doubt such special software exists on Linux/alpha, though.
So I wouldn't worry about those kinds of oddities overmuch.
*If* somebody then finds a load that cares, we can always fix it later, and I'll go "mea culpa, I didn't think it would matter, and I was wrong".
Linus
Quoted 26 lines · Linus Torvalds
Quoted 4 lines · Al Viro
Umm... What about the semantics of
get_user()of unmapped address? Some architectures do quiet EFAULT; some (including alpha) hit the sucker with SIGBUS, no matter what.I think we should strive to just make this all common.
The reason alpha is different is almost certainly not intentional, but a combination of "pure accident" and "nobody actually cares".
Quoted 2 lines
Are we free to modify that behaviour, or is that part of arch-specific ABI?
I'd just unify this all, probably with a preference for existing semantics on x86 (because of "biggest and most varied user base").
That whole "send SIGBUS even for kernel faults" is certainly bogus and against the usual rules. And I may well be to blame for it (I have this memory of disliking how EFAULT as a return code didn't actually return the faulting address). And realistically, it's also just not something that any normal application will ever hit. Giving invalid addresses to system calls is basically always a bug, although there are always special software that do all the crazy corner cases (ie things like emulators tend to do odd things).
Quoted 7 lines
I doubt such special software exists on Linux/alpha, though.
So I wouldn't worry about those kinds of oddities overmuch.
*If* somebody then finds a load that cares, we can always fix it later, and I'll go "mea culpa, I didn't think it would matter, and I was wrong".
FWIW, from digging through the current tree:
alpha, openrisc, sparc and xtensa send SIGBUS.
m68k: not sure, do_page_fault() callers there are delicate.
mips: really interesting -
/* Kernel mode? Handle exceptions or die */
if (!user_mode(regs))
goto no_context;
/*
* Send a sigbus, regardless of whether we were in kernel
* or user mode.... which is obviously a rudiment of SIGBUS variant, but nowadays it's EFAULT.
Everything else seems to be going with EFAULT.
PS: mips used to be SIGBUS, until this
commit 1d50e5e7a6e0325b1a652c4be296a71dc54a6e96
Author: Andrew Morton <[email protected]> Date: Fri Feb 20 01:33:18 2004 -0800
[PATCH] MIPS mega-patch
From: Ralf Baechle <[email protected]>
Below following 125547 lines of patches, all to arch/mips and include/asm-mips. I'm going to send the remaining stuff of which the one or other bit may need to be discussed in smaller bits.
IOW, details are buried somewhere in historical mips tree, assuming it survives...
Quoted 2 lines · Al Viro
Everything else seems to be going with EFAULT.
So I think fo kernel faults it's always basically up to the exception handler, and sending a signal regardless of that is just wrong.
Of course, an exception handler might choose to send a signal, but it just shouldn't be the do_page_fault() handler that does it.
For user faults, I think the rule ends up being that "if there's no mapping, or if there is a protection fault, then we should do SIGSEGV".
If there's an actual mapping in place, and the mapping has the right permissions for the access, but fails for some *other* reason, then we send SIGBUS.
So a shared mmap past the end of the file (but within the area that was used for mmap) would SIGBUS, while a write to a read-only mapping would SIGSEGV.
Things like unaligned accesses also might SIGBUS rather than SIGSEGV.
And I'm not surprised that there are exceptions to the rule, because almost nothing really cares. In most situations, it's a fatal error. And even when catching them, the end result is generally the same (either "print out helpful message and die", or "longjump to some safe code").
So most of the time it's probably not going to matter all that much which signal gets sent in practice.
Linus
Quoted 2 lines · Linus Torvalds
So most of the time it's probably not going to matter all that much which signal gets sent in practice.
I do also see a common pattern of the possibility to have a generic fault handler like generic_page_fault().
It probably should start with taking the mmap_sem until providing some retval that is much easier to digest further by the arch-dependent code, so it can directly do something rather than parsing the bitmask in a duplicated way (hence the new retval should hopefully not a bitmask anymore but a "what to do").
Maybe it can be something like:
/**
* enum page_fault_retval - Higher level fault retval, generalized from
* vm_fault_reason above that is only used by hardware page fault handlers.
* It generalizes the bitmask-versioned retval into something that the arch
* dependent code should react upon.
*
* @PF_RET_COMPLETED: The page fault is completed successfully
* @PF_RET_BAD_AREA: The page fault address falls in a bad area
* (e.g., vma not found, expand_stack() fails..)
* @PF_RET_ACCESS_ERR: The page fault has access errors
* (e.g., write fault on !VM_WRITE vmas)
* @PF_RET_KERN_FIXUP: The page fault requires kernel fixups
* (e.g., during copy_to_user() but fault failed?)
* @PF_RET_HWPOISON: The page fault encountered poisoned pages
* @PF_RET_SIGNAL: The page fault encountered poisoned pages
* ...
*/
enum page_fault_retval {
PF_RET_DONE = 0,
PF_RET_BAD_AREA,
PF_RET_ACCESS_ERR,
PF_RET_KERN_FIXUP,
PF_RET_HWPOISON,
PF_RET_SIGNAL,
...
};As a start we may still want to return some more information (perhaps still the vm_fault_t alongside? Or another union that will provide different information based on different PF_RET_*). One major thing is I see how we handle VM_FAULT_HWPOISON and also the fact that we encode something more into the bitmask on page sizes (VM_FAULT_HINDEX_MASK).
So the generic helper could, hopefully, hide the complexity of:
- Taking and releasing of mmap lock
- find_vma(), and also relevant checks on access or stack handling
- handle_mm_fault() itself (of course...)
- detect signals
- handle page fault retries (so, in the new layer of retval there should have nothing telling it to retry; it should always be the ultimate result)
- parse different errors into "what the arch code should do", and generalize the common ones, e.g.
- OOM, do pagefault_out_of_memory() for user-mode
- VM_FAULT_SIGSEGV, which should be able to merge into PF_RET_BAD_AREA?
- ...
It'll simplify things if we can unify some small details like whether the -EFAULT above should contain a sigbus.
A trivial detail I found when I was looking at this is, x86_64 passes in different signals to kernelmode_fixup_or_oops() - in do_user_addr_fault() there're three call sites and each of them pass over a differerent signal. IIUC that will only make a difference if there's a nested page fault during the vsyscall emulation (but I may be wrong too because I'm new to this code), and I have no idea when it'll happen and whether that needs to be strictly followed.
Thanks,
Signature · 1 line
Peter Xu
Quoted 11 lines · Peter Xu
I do also see a common pattern of the possibility to have a generic fault handler like
generic_page_fault().It probably should start with taking the
mmap_semuntil providing some retval that is much easier to digest further by the arch-dependent code, so it can directly do something rather than parsing the bitmask in a duplicated way (hence the new retval should hopefully not a bitmask anymore but a "what to do").Maybe it can be something like:
Quoted 9 lines
/**
* enumpage_fault_retval- Higher level fault retval, generalized from
*vm_fault_reasonabove that is only used by hardware page fault handlers.
* It generalizes the bitmask-versioned retval into something that the arch
* dependent code should react upon.
*
* @PF_RET_COMPLETED: The page fault is completed successfully
* @PF_RET_BAD_AREA: The page fault address falls in a bad area
* (e.g., vma not found,expand_stack()fails..)
FWIW, there's a fun discrepancy - VM_FAULT_SIGSEGV may yield SEGV_MAPERR or SEGV_ACCERR; depends upon the architecture. Not that there'd been many places that return VM_FAULT_SIGSEGV these days... Good thing, too, since otherwise e.g. csky would oops...
Quoted 6 lines
* @PF_RET_ACCESS_ERR: The page fault has access errors
* (e.g., write fault on !VM_WRITE vmas)
* @PF_RET_KERN_FIXUP: The page fault requires kernel fixups
* (e.g., duringcopy_to_user()but fault failed?)
* @PF_RET_HWPOISON: The page fault encountered poisoned pages
* @PF_RET_SIGNAL: The page fault encountered poisoned pages
??
Quoted 18 lines
* ...
*/enum page_fault_retval { PF_RET_DONE = 0, PF_RET_BAD_AREA, PF_RET_ACCESS_ERR, PF_RET_KERN_FIXUP, PF_RET_HWPOISON, PF_RET_SIGNAL, ... };As a start we may still want to return some more information (perhaps still the
vm_fault_talongside? Or another union that will provide different information based on different PF_RET_*). One major thing is I see how we handleVM_FAULT_HWPOISONand also the fact that we encode something more into the bitmask on page sizes (VM_FAULT_HINDEX_MASK).
Quoted 4 lines
So the generic helper could, hopefully, hide the complexity of:
- Taking and releasing of mmap lock
-find_vma(), and also relevant checks on access or stack handling
Umm... arm is a bit special here:
if (addr < FIRST_USER_ADDRESS)
return VM_FAULT_BADMAP;
with no counterparts elsewhere.
Quoted 4 lines
-
handle_mm_fault()itself (of course...)
- detect signals
- handle page fault retries (so, in the new layer of retval there should have nothing telling it to retry; it should always be the ultimate result)
agreed.
- unlock mmap; don't leave that to caller.
Quoted 5 lines
- parse different errors into "what the arch code should do", and generalize the common ones, e.g.
- OOM, dopagefault_out_of_memory()for user-mode
-VM_FAULT_SIGSEGV, which should be able to merge intoPF_RET_BAD_AREA?
- ...
AFAICS, all errors in kernel mode => no_context.
Quoted 10 lines
It'll simplify things if we can unify some small details like whether the
-EFAULTabove should contain a sigbus.A trivial detail I found when I was looking at this is,
x86_64passes in different signals tokernelmode_fixup_or_oops()- indo_user_addr_fault()there're three call sites and each of them pass over a differerent signal. IIUC that will only make a difference if there's a nested page fault during the vsyscall emulation (but I may be wrong too because I'm new to this code), and I have no idea when it'll happen and whether that needs to be strictly followed.
From my (very incomplete so far) dig through that pile:
Q: do we still have the cases when handle_mm_fault() does
not return any of VM_FAULT_COMPLETED | VM_FAULT_RETRY | VM_FAULT_ERROR? That gets treated as unlock + VM_FAULT_COMPLETED, but do we still need that?
Q: can VM_FAULT_RETRY be mixed with anything in VM_FAULT_ERROR?
What locking, if that happens?
* details of storing the fault details (for ptrace, mostly)
vary a lot; no chance to unify, AFAICS.
* requirements for vma flags also differ; e.g. read fault on
alpha is explicitly OK with absence of VM_READ if VM_WRITE is there. Probably should go by way of arm and pass the mask that must have non-empty intersection with vma->vm_flags? Because *that* is very likely to be a part of ABI - mmap(2) callers that rely upon the flags being OK for given architecture are quite possible.
* mmap lock is also quite variable in how it's taken;
x86 and arm have fun dance with trylock/search for exception handler/etc. Other architectures do not; OTOH, there's a prefetch stuck in itanic variant, with comment about mmap_sem being performance-critical...
* logics for stack expansion includes this twist:
if (!(vma->vm_flags & VM_GROWSDOWN))
goto map_err;
if (user_mode(regs)) {
/* Accessing the stack below usp is always a bug. The
"+ 256" is there due to some instructions doing
pre-decrement on the stack and that doesn't show up
until later. */
if (address + 256 < rdusp())
goto map_err;
}
if (expand_stack(vma, address))
goto map_err;That's m68k; ISTR similar considerations elsewhere, but I could be wrong.
Quoted 13 lines · Al Viro
* logics for stack expansion includes this twist:
if (!(vma->vm_flags & VM_GROWSDOWN))
goto map_err;
if (user_mode(regs)) {
/* Accessing the stack below usp is always a bug. The
"+ 256" is there due to some instructions doing
pre-decrement on the stack and that doesn't show up
until later. */
if (address + 256 < rdusp())
goto map_err;
}
if (expand_stack(vma, address))
goto map_err;Quoted 2 lines
That's m68k; ISTR similar considerations elsewhere, but I could be wrong.
Hell, yes -
if (!(vma->vm_flags & VM_GROWSDOWN))
goto bad_area;
if (!(fault_code & FAULT_CODE_WRITE)) {
/* Non-faulting loads shouldn't expand stack. */
insn = get_fault_insn(regs, insn);
if ((insn & 0xc0800000) == 0xc0800000) {
unsigned char asi;
if (insn & 0x2000)
asi = (regs->tstate >> 24);
else
asi = (insn >> 5);
if ((asi & 0xf2) == 0x82)
goto bad_area;
}
}
if (expand_stack(vma, address))
goto bad_area;Note that it's very much not a bug - it's a nonfaulting (== speculative) load, and the place where we are heading from bad_area in this case is this in do_kernel_fault():
if (!(fault_code & (FAULT_CODE_WRITE|FAULT_CODE_ITLB)) &&
(insn & 0xc0800000) == 0xc0800000) {
if (insn & 0x2000)
asi = (regs->tstate >> 24);
else
asi = (insn >> 5);
if ((asi & 0xf2) == 0x82) {
if (insn & 0x1000000) {
handle_ldf_stq(insn, regs);
} else {
/* This was a non-faulting load. Just clear the
* destination register(s) and continue with the next
* instruction. -jj
*/
handle_ld_nf(insn, regs);
}
return;(the name is misguiding - it covers userland stuff as well; in this particular case the triggering instruction is non-priveleged)
Quoted 57 lines · Al Viro
Quoted 11 lines · Peter Xu
I do also see a common pattern of the possibility to have a generic fault handler like
generic_page_fault().It probably should start with taking the
mmap_semuntil providing some retval that is much easier to digest further by the arch-dependent code, so it can directly do something rather than parsing the bitmask in a duplicated way (hence the new retval should hopefully not a bitmask anymore but a "what to do").Maybe it can be something like:
Quoted 9 lines
/**
* enumpage_fault_retval- Higher level fault retval, generalized from
*vm_fault_reasonabove that is only used by hardware page fault handlers.
* It generalizes the bitmask-versioned retval into something that the arch
* dependent code should react upon.
*
* @PF_RET_COMPLETED: The page fault is completed successfully
* @PF_RET_BAD_AREA: The page fault address falls in a bad area
* (e.g., vma not found,expand_stack()fails..)FWIW, there's a fun discrepancy -
VM_FAULT_SIGSEGVmay yieldSEGV_MAPERRorSEGV_ACCERR; depends upon the architecture. Not that there'd been many places that returnVM_FAULT_SIGSEGVthese days... Good thing, too, since otherwise e.g. csky would oops...Quoted 6 lines
* @PF_RET_ACCESS_ERR: The page fault has access errors
* (e.g., write fault on !VM_WRITE vmas)
* @PF_RET_KERN_FIXUP: The page fault requires kernel fixups
* (e.g., duringcopy_to_user()but fault failed?)
* @PF_RET_HWPOISON: The page fault encountered poisoned pages
* @PF_RET_SIGNAL: The page fault encountered poisoned pages??
Quoted 20 lines
* ...
*/enum page_fault_retval { PF_RET_DONE = 0, PF_RET_BAD_AREA, PF_RET_ACCESS_ERR, PF_RET_KERN_FIXUP, PF_RET_HWPOISON, PF_RET_SIGNAL, ... };As a start we may still want to return some more information (perhaps still the
vm_fault_talongside? Or another union that will provide different information based on different PF_RET_*). One major thing is I see how we handleVM_FAULT_HWPOISONand also the fact that we encode something more into the bitmask on page sizes (VM_FAULT_HINDEX_MASK).So the generic helper could, hopefully, hide the complexity of:
Quoted 7 lines
Quoted 2 lines
- Taking and releasing of mmap lock
-find_vma(), and also relevant checks on access or stack handlingUmm... arm is a bit special here:
if (addr <FIRST_USER_ADDRESS)
returnVM_FAULT_BADMAP;
with no counterparts elsewhere.
For this specific case IIUC it's the same as bad_area. VM_FAULT_BADMAP is further handled later in do_page_fault() there for either arm/arm64.
This reminded me this, on how arm defines the private retvals, while the generic ones grows and probably no one noticed they can collapse already..
#define VM_FAULT_BADMAP 0x010000
#define VM_FAULT_BADACCESS 0x020000
enum vm_fault_reason {
...
VM_FAULT_HINDEX_MASK = (__force vm_fault_t)0x0f0000,
};VM_FAULT_HINDEX_MASK is only used by VM_FAULT_HWPOISON_LARGE, so I think arm[64] could expect some surprise when it hit hugetlb hwpoison pages... maybe I should prepare a patch for arm.
Quoted 29 lines
Quoted 5 lines
-
handle_mm_fault()itself (of course...)
- detect signals
- handle page fault retries (so, in the new layer of retval there should have nothing telling it to retry; it should always be the ultimate result)agreed.
- unlock mmap; don't leave that to caller.
Quoted 5 lines
- parse different errors into "what the arch code should do", and generalize the common ones, e.g.
- OOM, dopagefault_out_of_memory()for user-mode
-VM_FAULT_SIGSEGV, which should be able to merge intoPF_RET_BAD_AREA?
- ...AFAICS, all errors in kernel mode =>
no_context.Quoted 10 lines
It'll simplify things if we can unify some small details like whether the
-EFAULTabove should contain a sigbus.A trivial detail I found when I was looking at this is,
x86_64passes in different signals tokernelmode_fixup_or_oops()- indo_user_addr_fault()there're three call sites and each of them pass over a differerent signal. IIUC that will only make a difference if there's a nested page fault during the vsyscall emulation (but I may be wrong too because I'm new to this code), and I have no idea when it'll happen and whether that needs to be strictly followed.
Quoted 7 lines
From my (very incomplete so far) dig through that pile:
Q: do we still have the cases whenhandle_mm_fault()does
not return any ofVM_FAULT_COMPLETED|VM_FAULT_RETRY|VM_FAULT_ERROR? That gets treated as unlock +VM_FAULT_COMPLETED, but do we still need that?
Q: canVM_FAULT_RETRYbe mixed with anything inVM_FAULT_ERROR?
What locking, if that happens?
For this one, I don't think they can be mixed. IMHO RETRY only binds with a wait, so if we didn't wait and found issue, we return ERROR; if we decided to wait, we will try nothing more besides return after wait with the RETRY. We should just never check any error at all if the wait happened. Otherwise there's a bug of potential deadlock.
I'll skip some details in this email either above or below; I agree there're so many trivial details to take care of to not break a thing.
IMHO it'll be merely impossible to merge things across most (if not to say, all) archs. It will need to be start from one or at least a few that still shares a major common base - I would still rely on x86 as a start - then we try to use the helper in as much archs as possible.
Even on x86, I do also see challenges so I'm not sure whether a common enough routine can be abstracted indeed. But I believe there's a way to do this because obviously we still see tons of duplicated logics falling around. It may definitely need time to think out where's the best spot to start, and how to gradually move towards covering more archs starting from one.
Thanks,
Quoted 28 lines
* details of storing the fault details (for ptrace, mostly)
vary a lot; no chance to unify, AFAICS.
* requirements for vma flags also differ; e.g. read fault on
alpha is explicitly OK with absence ofVM_READifVM_WRITEis there. Probably should go by way of arm and pass the mask that must have non-empty intersection withvma->vm_flags? Because *that* is very likely to be a part of ABI - mmap(2) callers that rely upon the flags being OK for given architecture are quite possible.
* mmap lock is also quite variable in how it's taken;
x86 and arm have fun dance with trylock/search for exception handler/etc. Other architectures do not; OTOH, there's a prefetch stuck in itanic variant, with comment aboutmmap_sembeing performance-critical...* logics for stack expansion includes this twist: if (!(vma->vm_flags & VM_GROWSDOWN)) goto map_err; if (user_mode(regs)) { /* Accessing the stack below usp is always a bug. The "+ 256" is there due to some instructions doing pre-decrement on the stack and that doesn't show up until later. */ if (address + 256 < rdusp()) goto map_err; } if (expand_stack(vma, address)) goto map_err;That's m68k; ISTR similar considerations elsewhere, but I could be wrong.
Signature · 1 line
Peter Xu
Quoted 11 lines · Peter Xu
IMHO it'll be merely impossible to merge things across most (if not to say, all) archs. It will need to be start from one or at least a few that still shares a major common base - I would still rely on x86 as a start - then we try to use the helper in as much archs as possible.
Even on x86, I do also see challenges so I'm not sure whether a common enough routine can be abstracted indeed. But I believe there's a way to do this because obviously we still see tons of duplicated logics falling around. It may definitely need time to think out where's the best spot to start, and how to gradually move towards covering more archs starting from one.
FWIW, after going through everything from alpha to loongarch (in alphabetic order, skipping the itanic) the following seems to be suitable for all of them:
generic_fault(address, flags, vm_flags, regs)
{
struct mm_struct *mm = current->mm;
struct vm_area_struct *vma;
vm_fault_t fault;
perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
if (unlikely(!mmap_read_trylock(mm))) {
if (!(flags & FAULT_FLAG_USER) &&
!search_exception_tables(instruction_pointer(regs))) {
/*
* Fault from code in kernel from
* which we do not expect faults.
*/
return KERN;
}
retry:
mmap_read_lock(mm);
} else {
might_sleep();
#ifdef CONFIG_DEBUG_VM
if (!(flags & FAULT_FLAG_USER) &&
!search_exception_tables(instruction_pointer(regs)))
return KERN;
#endif
}
vma = find_vma(mm, address);
if (!vma)
goto Eunmapped;
if (unlikely(vma->vm_start > address)) {
if (!(vma->vm_flags & VM_GROWSDOWN))
goto Eunmapped;
if (addr < FIRST_USER_ADDRESS)
goto Eunmapped;
if (expand_stack(vma, address))
goto Eunmapped;
}
/* Ok, we have a good vm_area for this memory access, so
we can handle it. */
if (!(vma->vm_flags & vm_flags))
goto Eaccess;
/* If for any reason at all we couldn't handle the fault,
make sure we exit gracefully rather than endlessly redo
the fault. */
fault = handle_mm_fault(vma, address, flags, regs);
if (unlikely(fault & VM_FAULT_RETRY)) {
if (!(flags & FAULT_FLAG_USER)) {
if (fatal_signal_pending(current))
return KERN;
} else {
if (signal_pending(current))
return FOAD;
}
flags |= FAULT_FLAG_TRIED;
goto retry;
}
if (fault & VM_FAULT_COMPLETED)
return DONE;
mmap_read_unlock(mm);
if (likely(!(fault & VM_FAULT_ERROR)))
return DONE;
if (!(flags & FAULT_FLAG_USER))
return KERN;
if (fault & VM_FAULT_OOM) {
pagefault_out_of_memory();
return FOAD;
}
if (fault & VM_FAULT_SIGSEGV)
return SIGSEGV;
if (fault & VM_FAULT_SIGBUS)
return SIGBUS;
if (fault & VM_FAULT_HWPOISON)
return POISON + PAGE_SHIFT; // POISON == 256
if (fault & VM_FAULT_HWPOISON_LARGE)
return POISON + hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault));
BUG();Eunmapped:
mmap_read_unlock(mm); return flags & FAULT_FLAG_USER ? MAPERR : KERN; Eaccess: mmap_read_unlock(mm); return flags & FAULT_FLAG_USER ? ACCERR : KERN; }
possible return values (and that's obviously not the identifiers to be used for real; for now I'm just looking for feasibility of it all):
DONE success, nothing else to be done
FOAD OOM/fatal signal with VM_FAULT_RETRY/
signal with VM_FAULT_RETRY from userland - nothing to be done here.
KERN kernel mode failed fault, fixup or oops
MAPERR unmapped address, SIGSEGV/SEGV_MAPERR for you
ACCERR nothing in vm_flags present in ->vm_flags of vma;
SIGSEGV/SEGV_ACCERR
SIGSEGV VM_FAULT_SIGSEGV; some architectures treat that
as SEGV_MAPERR, some as SEGV_ACCERR.
SIGBUS VM_FAULT_SIGBUS; SIGBUS/BUS_ADRERR
POISON + shift VM_FAULT_HWPOISON and VM_FAULT_HWPOISON_LARGE, with
log2(affected page size) encoded into return value.
This is obviously not even close to final helper, but... alpha, arc, arm, arm64, csky, hexagon, loongarch convert to that cleanly.
Itanic very much does not (due to weird dual stacks, awful address space layout, etc.), but then git rm arch/ia64 is long overdue.
Fairly typical look after conversion:
arc:
{
struct task_struct *tsk = current;
struct mm_struct *mm = tsk->mm;
unsigned int mask;
unsigned int flags;
unsigned int res;
/*
* NOTE! We MUST NOT take any locks for this case. We may
* be in an interrupt or a critical region, and should
* only copy the information from the master page table,
* nothing more.
*/
if (address >= VMALLOC_START && !user_mode(regs)) {
if (unlikely(handle_kernel_vaddr_fault(address)))
goto no_context;
else
return;
}
/*
* If we're in an interrupt or have no user
* context, we must not take the fault..
*/
if (faulthandler_disabled() || !mm)
goto no_context;
flags = FAULT_FLAG_DEFAULT;
if (user_mode(regs))
flags |= FAULT_FLAG_USER;
mask = VM_READ;
if (regs->ecr_cause & ECR_C_PROTV_STORE) { /* ST/EX */
flags |= FAULT_FLAG_WRITE;
mask = VM_WRITE;
} else if ((regs->ecr_vec == ECR_V_PROTV) &&
(regs->ecr_cause == ECR_C_PROTV_INST_FETCH)) {
mask = VM_EXEC;
}
res = generic_fault(address, flags, mask, regs);
if (likely(res == DONE))
return;
if (res == FOAD)
return;
if (res == KERN) {
no_context:
if (fixup_exception(regs))
return;
die("Oops", regs, address);
}
tsk->thread.fault_address = address;
if (res == SIGBUS)
force_sig_fault(SIGBUS, BUS_ADRERR, (void __user *) address);
else
force_sig_fault(SIGSEGV, res == ACCERR ? SEGV_ACCERR : SEGV_MAPERR,
(void __user *) address);
}Or this arm64:
{
const struct fault_info *inf;
struct mm_struct *mm = current->mm;
unsigned long vm_flags;
unsigned int mm_flags = FAULT_FLAG_DEFAULT;
unsigned long addr = untagged_addr(far);
unsigned int res;
if (kprobe_page_fault(regs, esr))
return 0;
/*
* If we're in an interrupt or have no user context, we must not take
* the fault.
*/
if (faulthandler_disabled() || !mm)
goto no_context;
if (user_mode(regs))
mm_flags |= FAULT_FLAG_USER;
/*
* vm_flags tells us what bits we must have in vma->vm_flags
* for the fault to be benign, __do_page_fault() would check
* vma->vm_flags & vm_flags and returns an error if the
* intersection is empty
*/
if (is_el0_instruction_abort(esr)) {
/* It was exec fault */
vm_flags = VM_EXEC;
mm_flags |= FAULT_FLAG_INSTRUCTION;
} else if (is_write_abort(esr)) {
/* It was write fault */
vm_flags = VM_WRITE;
mm_flags |= FAULT_FLAG_WRITE;
} else {
/* It was read fault */
vm_flags = VM_READ;
/* Write implies read */
vm_flags |= VM_WRITE;
/* If EPAN is absent then exec implies read */
if (!cpus_have_const_cap(ARM64_HAS_EPAN))
vm_flags |= VM_EXEC;
}
if (is_ttbr0_addr(addr) && is_el1_permission_fault(addr, esr, regs)) {
if (is_el1_instruction_abort(esr))
die_kernel_fault("execution of user memory",
addr, esr, regs);
if (!search_exception_tables(regs->pc))
die_kernel_fault("access to user memory outside uaccess routines",
addr, esr, regs);
}
res = generic_fault(addr, mm_flags, vm_flags, regs);
if (likely(res == DONE))
return 0;
if (res == FOAD)
return 0;
if (res == KERN) {
no_context:
__do_kernel_fault(addr, esr, regs);
return 0;
}
inf = esr_to_fault_info(esr);
set_thread_esr(addr, esr);
if (res == SIGBUS) {
/*
* We had some memory, but were unable to successfully fix up
* this page fault.
*/
arm64_force_sig_fault(SIGBUS, BUS_ADRERR, far, inf->name);
} else if (res > POISON) {
arm64_force_sig_mceerr(BUS_MCEERR_AR, far, res - POISON, inf->name);
} else {
/*
* Something tried to access memory that isn't in our memory
* map.
*/
arm64_force_sig_fault(SIGSEGV,
res == ACCERR ? SEGV_ACCERR : SEGV_MAPERR,
far, inf->name);
}
return 0;
}Quoted 14 lines · Al Viro
Quoted 11 lines · Peter Xu
IMHO it'll be merely impossible to merge things across most (if not to say, all) archs. It will need to be start from one or at least a few that still shares a major common base - I would still rely on x86 as a start - then we try to use the helper in as much archs as possible.
Even on x86, I do also see challenges so I'm not sure whether a common enough routine can be abstracted indeed. But I believe there's a way to do this because obviously we still see tons of duplicated logics falling around. It may definitely need time to think out where's the best spot to start, and how to gradually move towards covering more archs starting from one.
Quoted 5 lines
FWIW, after going through everything from alpha to loongarch (in alphabetic order, skipping the itanic) the following seems to be suitable for all of them:
generic_fault(address, flags, vm_flags, regs)
[snip]
After looking through other architectures: that needs changes.
AFAICS, the right approach would be to add a pointer to (uninitialized) kernel_siginfo. And let it pass the signal number, etc. through that. That way all "we want to raise a signal" return values fold together. *IF* the page fault wants to do something extra on SIGSEGV, but not on SIGBUS (we have several such), it can key that upon info.si_signo.
Speaking of SIGSEGV, there's a fun situation with VM_FAULT_SIGSEGV:
1) Only x86 and ppc generate VM_FAULT_SIGSEGV in handle_mm_fault() without hitting WARN_ON_ONCE(). And neither actually returns it to page fault handler - the conditions that would've led to that return value (pkey stuff) are checked in the page fault handler and handle_mm_fault() is not called in such cases.
2) on alpha, hexagon, itanic, loongarch, microblaze, mips, nios2, openrisc, sparc, um and xtensa #PF handler would end up with SEGV_ACCERR if it saw VM_FAULT_SIGNAL.
3) on arm, arm64, arc, m68k, powerpc, s390, sh and x86 - SEGV_MAPERR (except that neither x86 nor powerpc #PF ever see it)
4) on csky and riscv it would end up with BUG()
5) on parisc it's complicated^Wbroken - it tries to decide which one to use after having unlocked mmap and looking at vma in process.
As it is, VM_FAULT_SIGSEGV had ended up as "we need to report some error to get_user_pages() and similar callers in cases when page fault handler would've detected pkey problem and refused to call handle_mm_fault()", with several places where it's "we had been called with bogus arguments; printk and return something to indicate the things had gone wrong". It used to have other uses, but this is what it had become now - grep and you'll see.
AFAICS, all checks for it in page fault handlers are pretty much dead code...
Speaking of really weird stuff, on loongarch we have
this:
/*
* We fault-in kernel-space virtual memory on-demand. The
* 'reference' page table is init_mm.pgd.
*
* NOTE! We MUST NOT take any locks for this case. We may
* be in an interrupt or a critical region, and should
* only copy the information from the master page table,
* nothing more.
*/
if (address & __UA_LIMIT) {
if (!user_mode(regs))
no_context(regs, address);
else
do_sigsegv(regs, write, address, si_code);
return;
}That looks as if it had started off as usual vmalloc page tables propagation in #PF, but... it obviously does nothing of that sort. What's going on there and why do we even bother? After all, we'd better have no vma above that address, so the normal logics would seem to be fine with that case...
Another really weird part there is
/*
* If we're in an interrupt or have no user
* context, we must not take the fault..
*/
if (faulthandler_disabled() || !mm) {
do_sigsegv(regs, write, address, si_code);
return;
}
There should be no way to have this condition for userland
page fault and do_sigsegv() is starting with
/* Kernel mode? Handle exceptions or die */
if (!user_mode(regs)) {
no_context(regs, address);
return;
}
<force SIGSEGV>
What's wrong with just
if (faulthandler_disabled() || !mm) {
no_context(regs, address);
return;
}Am I missing something subtle here?
Helge Deller
maintainerQuoted 26 lines · Linus Torvalds
Quoted 4 lines · Al Viro
Umm... What about the semantics of
get_user()of unmapped address? Some architectures do quiet EFAULT; some (including alpha) hit the sucker with SIGBUS, no matter what.I think we should strive to just make this all common.
The reason alpha is different is almost certainly not intentional, but a combination of "pure accident" and "nobody actually cares".
Quoted 2 lines
Are we free to modify that behaviour, or is that part of arch-specific ABI?
I'd just unify this all, probably with a preference for existing semantics on x86 (because of "biggest and most varied user base").
That whole "send SIGBUS even for kernel faults" is certainly bogus and against the usual rules. And I may well be to blame for it (I have this memory of disliking how EFAULT as a return code didn't actually return the faulting address). And realistically, it's also just not something that any normal application will ever hit. Giving invalid addresses to system calls is basically always a bug, although there are always special software that do all the crazy corner cases (ie things like emulators tend to do odd things).
Quoted 7 lines
I doubt such special software exists on Linux/alpha, though.
So I wouldn't worry about those kinds of oddities overmuch.
*If* somebody then finds a load that cares, we can always fix it later, and I'll go "mea culpa, I didn't think it would matter, and I was wrong".
AFAICS, the only applications which really care about the return code are
- testsuites like LTP (i.e. the fstat05 testcase)
- some (not just debian) packages execute tests at the end of the build process and verify the return codes, i.e. libsigsegv.
The differences between the architectures is currently often taken care of via #ifdefs, so if the return code is harmonized across platforms it would at least help to simplify such testcases.
Helge
Quoted 4 lines · Helge Deller
AFAICS, the only applications which really care about the return code are
- testsuites like LTP (i.e. the fstat05 testcase)
Those have actually shown issues with various library implementations, exactly because real system calls act very differently in this area from library wrappers.
Things like the vdso implementation of gettimeofday() get a SIGSEGV if the timeval or timezone pointer is invalid, while the "real system call" version returns -1/EFAULT instead.
And very similar things happen when glibc ends up wrapping system calls and converting buffers manually. At some point, glibc had a special 'struct stat' and basically converted the native system call to it, so you did 'stat()' on something, and it ended up actually using a private on-stack buffer for the system call, followed by a "convert that kernel 'struct stat' to the glibc 'struct stat'" phase. So once again, instead of -1/EFAULT, you'd first have a successful system call, and then get a SIGSEGV in glibc.
And as you say, test suites would notice. But no actual normal app would ever care.
Of course, there's always the abnormal apps. There _are_ the odd cases that actually catch faults and fix them up, and can then be confused by changes like that.
It's very very rare, but it happens - things like emulators do tend to do some really strange things.
Linus
Quoted 10 lines · Linus Torvalds
Quoted 4 lines · Al Viro
Umm... What about the semantics of
get_user()of unmapped address? Some architectures do quiet EFAULT; some (including alpha) hit the sucker with SIGBUS, no matter what.I think we should strive to just make this all common.
The reason alpha is different is almost certainly not intentional, but a combination of "pure accident" and "nobody actually cares".
BTW, speaking of alpha page faults - maybe I'm misreading the manual, but it seems to imply that interrupts are *not* disabled when entering page fault handler:
Table 24–2 Entry Point Address Registers
Entry Point Value in a0 Value in a1 Value in a2 PS<IPL> entArith Exception summary Register mask UNPREDICTABLE Unchanged entIF Fault or trap type code UNPREDICTABLE UNPREDICTABLE Unchanged entInt Interrupt type Vector Interrupt parameter Priority of interrupt entMM VA MMCSR Cause Unchanged entSys p0 p1 p2 Unchanged entUna VA Opcode Src/Dst Unchanged
So there's nothing to prevent an interrupt hitting just as we reach entMM, with interrupt handler stepping on a vmalloc'ed area and triggering another page fault.
If that is correct, this
/* Synchronize this task's top level page-table
with the "reference" page table from init. */
long index = pgd_index(address);
pgd_t *pgd, *pgd_k;
pgd = current->active_mm->pgd + index;
pgd_k = swapper_pg_dir + index;
if (!pgd_present(*pgd) && pgd_present(*pgd_k)) {
pgd_val(*pgd) = pgd_val(*pgd_k);
return;
}
goto no_context;is not just missing local_irq_save()/local_irq_restore() around that fragment - if it finds pgd already present, it needs to check pte before deciding to proceed to no_context.
Suppose we access vmalloc area and corresponding pgd is not present in current->active_mm. Just as we get to entMM, an interrupt arrives and proceeds to access something covered by the same pgd. OK, current->active_mm is still not present, we get another page fault and do_page_fault() gets to the quoted code. pgd is copied from the swapper_pg_dir, do_page_fault() returns and we get back to the instruction in interrupt handler that had triggered the second #PF. This time around it succeeds. Once the interrupt handler completes we are back to entMM. Once *that* gets to do_page_fault() we hit the quoted code again. Only this time around pgd *is* present and instead of returning we get to no_context. And since it's been a normal access to vmalloc'ed memory, there's nothing to be found in exception table. Oops...
AFAICS, one way to deal with that is to treat
(unlikely) pgd_present(*pgd) as "get to pte, return if it looks legitimate, proceed to no_context if it isn't".
Other bugs in the same area:
* we ought to compare address with VMALLOC_START,
not TASK_SIZE.
* we ought to do that *before* checking for
kernel threads/pagefault_disable() being in effect.
Wait a minute - pgd_present() on alpha has become constant 1 since a73c948952cc "alpha: use pgtable-nopud instead of 4level-fixup"
So that thing had been completely broken for 3 years and nobody had noticed. And that's really completely broken - it stopped copying top-level entries since that commit.
That's also not hard to fix, but...
* CONFIG_ALPHA_LARGE_VMALLOC had been racy all along
* it had very limited use (need for >8Gb of vmalloc
space)
* it had stopped working in late 2019 and nobody cared.
How about removing that kludge? Richard?
Quoted 11 lines · Al Viro
Other bugs in the same area:
* we ought to compare address withVMALLOC_START,
notTASK_SIZE.
* we ought to do that *before* checking for
kernel threads/pagefault_disable() being in effect.Wait a minute -
pgd_present()on alpha has become constant 1 sincea73c948952cc"alpha: use pgtable-nopud instead of 4level-fixup"So that thing had been completely broken for 3 years and nobody had noticed.
I have never noticed because I haven't been able to run a 5.9 or newer kernel on Alpha reliably so have been running a 5.8 kernel for quite some time. Bad commit is about 25788738eb9ce46fe6a0fd84a3ceef5c795d41f0 but bisection proved very difficult because the bug might only show up once per day (memory corruption in user space causing gcc in big long builds to ICE reporting corrupted structures, or similar) and so could never be entirely sure a kernel marked as good was truly good in bisection and went wrong a number of times. There is a good possibility the bad commit is one of facdaa917c4d5a376d or 25788738eb9ce46fe6.
Cheers,
Michael.
Hi Michael!
Quoted 3 lines · Michael Cree
I have never noticed because I haven't been able to run a 5.9 or newer kernel on Alpha reliably so have been running a 5.8 kernel for quite some time.
Didn't people on the debian-alpha mailing list report that newer 6.x kernels work fine on alpha again? Did you try a recent kernel yourself?
Adrian
Signature · 4 lines
.''`. John Paul Adrian Glaubitz : :' : Debian Developer `. `' Physicist `- GPG: 62FF 8A75 84E0 2956 9546 0006 7426 3B37 F5B5 F913
Quoted 10 lines · Michael Cree
Quoted 9 lines · Al Viro
Other bugs in the same area:
* we ought to compare address withVMALLOC_START,
notTASK_SIZE.
* we ought to do that *before* checking for
kernel threads/pagefault_disable() being in effect.Wait a minute -
pgd_present()on alpha has become constant 1 sincea73c948952cc"alpha: use pgtable-nopud instead of 4level-fixup"
Quoted 6 lines
Quoted 2 lines
So that thing had been completely broken for 3 years and nobody had noticed.
I have never noticed because I haven't been able to run a 5.9 or newer kernel on Alpha reliably so have been running a 5.8 kernel for quite some time.
Er... For one thing, commit in question went into 5.5; for another you would only have noticed if you had CONFIG_ALPHA_LARGE_VMALLOC in your .config, so I rather doubt it's the same problem.
Normally alpha has one PGDIR_SIZE worth of vmalloc space, and it's shared between all processes - that one slot in top-level table is filled before we spawn any threads so all of them end up sharing the reference to the same middle-level table, which is where vmalloc stuff gets mapped.
So normally we have 8Gb of vmalloc space and, as usual for 64bit boxen, no need to play with vmalloc handling on page faults. The trouble is with a kludge that tries to give more than 8Gb; it gives just under 2Tb (2Tb-8Gb, actually - 255 slots out 1024 in top-level table). That does need assistance from #PF handler, and that assistance (ifdefed under CONFIG_ALPHA_LARGE_VMALLOC) what I'd been refering to.
To quote Kconfig,
====
# LARGE_VMALLOC is racy, if you *really* need it then fix it first config ALPHA_LARGE_VMALLOC
bool
help
Process creation and other aspects of virtual memory management can be streamlined if we restrict the kernel to one PGD for all vmalloc allocations. This equates to about 8GB.
Under normal circumstances, this is so far and above what is needed as to be laughable. However, there are certain applications (such as benchmark-grade in-kernel web serving) that can make use of as much vmalloc space as is available.
Say N unless you know you need gobs and gobs of vmalloc space.
====
"Racy" probably had been about something along the lines of the scenario I'd mentioned just upthread, but in 5.5 that "racy" escalated to "does not work at all" - if you ever hit a vmalloc-related fault, you are going to get an oops. You still get 8Gb, but beyond that it's broken.
And it's almost certainly not the problem you are seeing...
Quoted 6 lines · Al Viro
"Racy" probably had been about something along the lines of the scenario I'd mentioned just upthread, but in 5.5 that "racy" escalated to "does not work at all" - if you ever hit a vmalloc-related fault, you are going to get an oops. You still get 8Gb, but beyond that it's broken.
And it's almost certainly not the problem you are seeing...
Incidentally, direct store to pgd_val() in there looks fishy wrt barriers - shouldn't there be an smp_wmb() before we shove that sucker into our top-level table's entry?
Quoted 4 lines · Al Viro
BTW, speaking of alpha page faults - maybe I'm misreading the manual, but it seems to imply that interrupts are *not* disabled when entering page fault handler:
Yeah, I think you are right.
And we *thought* we didn't need to disable interrupts like we do on x86, because (unlike x86), we get the fault address in an interrupt-safe way as an argument from palcode, rather than having to read it from a register.
But now interrupts can race with that vmalloc case.
Quoted 3 lines
is not just missing local_irq_save()/local_irq_restore() around that fragment - if it finds pgd already present, it needs to check pte before deciding to proceed to
no_context.
Well, the logic there is that if the pgd was already present, then something *else* wasn't present, so no_context makes perfect sense.
But that assumption does not hold for the "we raced with an interrupt" case, so yes, it's broken.
And as you point out, it's doubly broken because "pgd_present()" doesn't actually do what it historically did, and what that code *thinks* it still does.
So yeah, this looks all broken.
Linus
Quoted 7 lines · Al Viro
On x86 it had been noticed and fixed back in 2014, in
26178ec11ef3"x86:
mm: consolidateVM_FAULT_RETRYhandling". Some of the other architectures had it dealt with later - e.g. arm in 2017, the fix is746a272e44141"ARM: 8692/1: mm: abort uaccess retries upon fatal signal"; xtensa - in 2021, the fix is7b9acbb6aad4f"xtensa: fix uaccess-related livelock indo_page_fault", etc.
Quoted 5 lines
However, it never had been done on a bunch of architectures - the current mainline still has that bug on alpha, hexagon, itanic, m68k, microblaze, nios2, openrisc, parisc, riscv and sparc (both sparc32 and sparc64). Fixes are trivial, but I've no way to test them for most of those architectures.
FWIW, when I fixed arm and arm64 back in 2017, I did report the issue here with a test case (and again in 2021, with maintainers all explciitly Cc'd):
https://lore.kernel.org/lkml/20170822102527.GA14671@leverpostej/
https://lore.kernel.org/linux-arch/[email protected]/
... so if anyone has access to those architectures, that test might be useful for verifying the fix.
Thanks,
Mark.
Geert Uytterhoeven
maintainerHi Mark,
Quoted 14 lines · Mark Rutland
Quoted 7 lines · Al Viro
On x86 it had been noticed and fixed back in 2014, in
26178ec11ef3"x86:
mm: consolidateVM_FAULT_RETRYhandling". Some of the other architectures had it dealt with later - e.g. arm in 2017, the fix is746a272e44141"ARM: 8692/1: mm: abort uaccess retries upon fatal signal"; xtensa - in 2021, the fix is7b9acbb6aad4f"xtensa: fix uaccess-related livelock indo_page_fault", etc.Quoted 5 lines
However, it never had been done on a bunch of architectures - the current mainline still has that bug on alpha, hexagon, itanic, m68k, microblaze, nios2, openrisc, parisc, riscv and sparc (both sparc32 and sparc64). Fixes are trivial, but I've no way to test them for most of those architectures.
Quoted 8 lines
FWIW, when I fixed arm and arm64 back in 2017, I did report the issue here with a test case (and again in 2021, with maintainers all explciitly Cc'd):
https://lore.kernel.org/lkml/20170822102527.GA14671@leverpostej/
https://lore.kernel.org/linux-arch/[email protected]/... so if anyone has access to those architectures, that test might be useful for verifying the fix.
Thanks a lot! This showed the problem on m68k, and confirmed Al's fix.
I'll give it a try on a few more systems later...
Gr{oetje,eeting}s,
Geert
--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]
In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds