Re: [PATCH 01/13] x86/fault: Check user_mode(regs) when avoiding an mmap_sem deadlock

From: Peter Zijlstra
Date: Tue Nov 20 2018 - 03:16:07 EST


On Mon, Nov 19, 2018 at 02:45:25PM -0800, Andy Lutomirski wrote:
> The fault-handling code that takes mmap_sem needs to avoid a
> deadlock that could occur if the kernel took a bad (OOPS-worthy)
> page fault on a user address while holding mmap_sem. This can only
> happen if the faulting instruction was in the kernel
> (i.e. user_mode(regs)). Rather than checking the sw_error_code

!user_mode(regs), surely, as the patch actually does.

> (which will have the USER bit set if the fault was a USER-permission
> access *or* if user_mode(regs)), just check user_mode(regs)
> directly.
>
> The old code would have malfunctioned if the kernel executed a bogus
> WRUSS instruction while holding mmap_sem. Fortunately, that is
> extremely unlikely in current kernels, which don't use WRUSS.
>
> Signed-off-by: Andy Lutomirski <luto@xxxxxxxxxx>
> ---
> arch/x86/mm/fault.c | 7 ++-----
> 1 file changed, 2 insertions(+), 5 deletions(-)
>
> diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c
> index 71d4b9d4d43f..91d4d2722f2e 100644
> --- a/arch/x86/mm/fault.c
> +++ b/arch/x86/mm/fault.c
> @@ -1344,13 +1344,10 @@ void do_user_addr_fault(struct pt_regs *regs,
> * Only do the expensive exception table search when we might be at
> * risk of a deadlock. This happens if we
> * 1. Failed to acquire mmap_sem, and
> - * 2. The access did not originate in userspace. Note: either the
> - * hardware or earlier page fault code may set X86_PF_USER
> - * in sw_error_code.
> + * 2. The access did not originate in userspace.
> */
> if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
> - if (!(sw_error_code & X86_PF_USER) &&
> - !search_exception_tables(regs->ip)) {
> + if (!user_mode(regs) && !search_exception_tables(regs->ip)) {
> /*
> * Fault from code in kernel from
> * which we do not expect faults.
> --
> 2.17.2
>