Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp237637yba; Mon, 20 May 2019 07:56:31 -0700 (PDT) X-Google-Smtp-Source: APXvYqzdStfoiCQnXw67SUam8HftacmZ8gRogZk1V0+BQ1G+r7SwC6plBWCTvuF3Z93qONbZf4Nf X-Received: by 2002:a17:902:aa45:: with SMTP id c5mr75485619plr.144.1558364191076; Mon, 20 May 2019 07:56:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558364191; cv=none; d=google.com; s=arc-20160816; b=nmc88xr8kbxd7RrdlKJmkikU+/gf/eLusG1CcIVCedWKSWflrowAmhy8FLctyswNSH iHBieRtC1fZskmj9lOSeonn7TR0fD+1q0LFJPiGC5nlo6ixmzkYcIoBQxpMx3vpHjw6j rlJO7vnPizbJ/9R2+vr9jQvVnLOqcSAJ5/jiWJX6p6rGQqAXqMm38CxHAmBEL0pw+tlE KJToEoxplBxsghXR857h8IRTHWerldenmPIONxFLB1PfxOjeN5hV8VLnxB8jpU/Ca3Iq xVEBfUQj4mXBG8hDFBHpK7YftraTgbEj/IixmxC0yN+sqZphVVxXL1+aGMULj1fvRpnV uzkQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=shMuaIpUApA1amQK6mryuvQm8uOYiQRpgw2RF5kNoUA=; b=khL/5auQRL/SVflvOc+1/bvAPryJINfgxfupsUeXsE64crM6XRsAWfTfXzBq1Q3Qd6 8dd+OAu90FhJ19rybhNIPo89KOmT2qDEX7WLHfdyKfrgN/045siGCuqi2bmAP8mw3C2y G3g0/4Janmuq6wuVgd7t6LDravRaokZ3gurMxMOIAk/qn/16uWtElcRAgiIWXceOrhC/ Vh26s1MjSSK+bmPan6DDQoVkNM/X7aVLQ6LxoAQZE0wThi2A7tguKGPJRGcM1RH3MaXD xdNusYan45m0ScwEYuCPW1NvterZ/dT3Xqk0gDTlJNE7YRByQ6DRjzvYYCdct2ZdvoRs 1k3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=f4wrao6x; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o4si18793536plb.69.2019.05.20.07.56.14; Mon, 20 May 2019 07:56:31 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=f4wrao6x; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732816AbfETMQP (ORCPT + 99 others); Mon, 20 May 2019 08:16:15 -0400 Received: from mail.kernel.org ([198.145.29.99]:56888 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732794AbfETMQN (ORCPT ); Mon, 20 May 2019 08:16:13 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 3E92C20862; Mon, 20 May 2019 12:16:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1558354572; bh=hSBsjb0qBcj82K4Q4as66E42q3kuDe2ckcAVEud7e94=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=f4wrao6xPLWLrHsR1ptcdjSLujSlYkIbI1m0607WJlyZZKkjLJFYi3yqko2azmsRi PS1wcR0LsHpUwUyLcEZEE6ywiem95V/Zm06HvAC+rF22M5dJ3VIUieshacWa5bGzDO ukhqes/+gvJhF2hAJV93m6z/kKekTrZxe9EhBENI= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Julien Thierry , "Peter Zijlstra (Intel)" , Andy Lutomirski , Borislav Petkov , Josh Poimboeuf , Linus Torvalds , Thomas Gleixner , stable@kernel.org, Ingo Molnar Subject: [PATCH 4.9 11/44] sched/x86: Save [ER]FLAGS on context switch Date: Mon, 20 May 2019 14:14:00 +0200 Message-Id: <20190520115232.329399629@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190520115230.720347034@linuxfoundation.org> References: <20190520115230.720347034@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Peter Zijlstra commit 6690e86be83ac75832e461c141055b5d601c0a6d upstream. Effectively reverts commit: 2c7577a75837 ("sched/x86_64: Don't save flags on context switch") Specifically because SMAP uses FLAGS.AC which invalidates the claim that the kernel has clean flags. In particular; while preemption from interrupt return is fine (the IRET frame on the exception stack contains FLAGS) it breaks any code that does synchonous scheduling, including preempt_enable(). This has become a significant issue ever since commit: 5b24a7a2aa20 ("Add 'unsafe' user access functions for batched accesses") provided for means of having 'normal' C code between STAC / CLAC, exposing the FLAGS.AC state. So far this hasn't led to trouble, however fix it before it comes apart. Reported-by: Julien Thierry Signed-off-by: Peter Zijlstra (Intel) Acked-by: Andy Lutomirski Cc: Borislav Petkov Cc: Josh Poimboeuf Cc: Linus Torvalds Cc: Peter Zijlstra Cc: Thomas Gleixner Cc: stable@kernel.org Fixes: 5b24a7a2aa20 ("Add 'unsafe' user access functions for batched accesses") Signed-off-by: Ingo Molnar Signed-off-by: Greg Kroah-Hartman --- arch/x86/entry/entry_32.S | 2 ++ arch/x86/entry/entry_64.S | 2 ++ arch/x86/include/asm/switch_to.h | 1 + arch/x86/kernel/process_32.c | 7 +++++++ arch/x86/kernel/process_64.c | 8 ++++++++ 5 files changed, 20 insertions(+) --- a/arch/x86/entry/entry_32.S +++ b/arch/x86/entry/entry_32.S @@ -219,6 +219,7 @@ ENTRY(__switch_to_asm) pushl %ebx pushl %edi pushl %esi + pushfl /* switch stack */ movl %esp, TASK_threadsp(%eax) @@ -241,6 +242,7 @@ ENTRY(__switch_to_asm) #endif /* restore callee-saved registers */ + popfl popl %esi popl %edi popl %ebx --- a/arch/x86/entry/entry_64.S +++ b/arch/x86/entry/entry_64.S @@ -313,6 +313,7 @@ ENTRY(__switch_to_asm) pushq %r13 pushq %r14 pushq %r15 + pushfq /* switch stack */ movq %rsp, TASK_threadsp(%rdi) @@ -335,6 +336,7 @@ ENTRY(__switch_to_asm) #endif /* restore callee-saved registers */ + popfq popq %r15 popq %r14 popq %r13 --- a/arch/x86/include/asm/switch_to.h +++ b/arch/x86/include/asm/switch_to.h @@ -35,6 +35,7 @@ asmlinkage void ret_from_fork(void); /* data that is pointed to by thread.sp */ struct inactive_task_frame { + unsigned long flags; #ifdef CONFIG_X86_64 unsigned long r15; unsigned long r14; --- a/arch/x86/kernel/process_32.c +++ b/arch/x86/kernel/process_32.c @@ -129,6 +129,13 @@ int copy_thread_tls(unsigned long clone_ struct task_struct *tsk; int err; + /* + * For a new task use the RESET flags value since there is no before. + * All the status flags are zero; DF and all the system flags must also + * be 0, specifically IF must be 0 because we context switch to the new + * task with interrupts disabled. + */ + frame->flags = X86_EFLAGS_FIXED; frame->bp = 0; frame->ret_addr = (unsigned long) ret_from_fork; p->thread.sp = (unsigned long) fork_frame; --- a/arch/x86/kernel/process_64.c +++ b/arch/x86/kernel/process_64.c @@ -268,6 +268,14 @@ int copy_thread_tls(unsigned long clone_ childregs = task_pt_regs(p); fork_frame = container_of(childregs, struct fork_frame, regs); frame = &fork_frame->frame; + + /* + * For a new task use the RESET flags value since there is no before. + * All the status flags are zero; DF and all the system flags must also + * be 0, specifically IF must be 0 because we context switch to the new + * task with interrupts disabled. + */ + frame->flags = X86_EFLAGS_FIXED; frame->bp = 0; frame->ret_addr = (unsigned long) ret_from_fork; p->thread.sp = (unsigned long) fork_frame;