Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp247824yba; Mon, 20 May 2019 08:05:34 -0700 (PDT) X-Google-Smtp-Source: APXvYqyiuEr13P1xuTxES+tWidkxfTJeRPmnMJVXERNq8zmECujM4MIhJjz/2asajzuRCq9iGfhI X-Received: by 2002:a17:902:e40a:: with SMTP id ci10mr28553492plb.195.1558364734253; Mon, 20 May 2019 08:05:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558364734; cv=none; d=google.com; s=arc-20160816; b=bTX1fjypRshhrbWQVbPA79LuBuMV943cmoQpjJqats1qosc2CUg+vTuc8Z0tYkyQLk Am7L0o1oRmNu4xpjGhX2UOGiWa8xIlTAJjn9/X1eVKh2DKD8ZzAkvFFGrtuzyR9O2Mjm J577QyjA29BcHBriScn7sbLUMNeqsDZdcGwVnOqT4vVSCYGFP3Qq8Kni9FrYqNZ3lqHR ofXeVTbzO05Nbi/XW2SJAHkVjitFOIbq/sA2dE0DqUosFBe5iWiaR+opBVvlj6HOFUSn DefgIpjp1SPEyOoI+UhJRJIXBDT585Gxv9q7UubmvMQ2BUsW9Gbuv0KAxmwe0m8+XC/4 WV+w== 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=vNC5zx3CPVpkHGz2MAmLjRF+8ojCb086Kvrj1du/whQ=; b=lPYzhYQvQLuEwBGd2CHAl+gXuizssY7U5U39TAbBeq1HkpNN2v+wm3/a6lH7VcJ173 GX3SRazZIEI6+HV28EaujBesnk7l25N3lty+a8mIo35gzLaf7ERwDjw18CtdbSvgUo5P rJsykYQ7wcemMiLd7EX6ju8fIOEF2hGFq6PyPoP+sdK/+2yhv780hQXqwNi5vaJ20Mx1 3/Hl6SLeNNTaYmMdFrRNjPRwyWsdvAHgu4NguSma2oEXBXUs8byFSllEac1r/mlLTjsP 0TjnSenfvcq+oMKBDBZDpmLWU02fLSnAgPprDJAnULp4dWfc/DnnQ2engNAD1lUoPycg lvLQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=bJmOsrS1; 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 ay11si17232318plb.423.2019.05.20.08.05.19; Mon, 20 May 2019 08:05:34 -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=bJmOsrS1; 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 S2387894AbfETMTw (ORCPT + 99 others); Mon, 20 May 2019 08:19:52 -0400 Received: from mail.kernel.org ([198.145.29.99]:33074 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387881AbfETMTu (ORCPT ); Mon, 20 May 2019 08:19:50 -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 560BD20656; Mon, 20 May 2019 12:19:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1558354789; bh=m2voSuVvE1XFDGeDabAu20m7trhvVa9G2eC5AgnSOKU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bJmOsrS1hdOF3VLjRoKNNJUY4yegWD7m35TnbwPBKpxKF880EQgrfwdW+unzF/TRw x0/Qs1gfvN1G7M0K2E8HgwdLQtox5I6RLVCsAcgIVDZxLcfhQiLnTOkMDIDWYsihYM bhMWRJDbeIDL3a9pPN5eRSYjcSzKdemhpdxAEbY4= 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.14 16/63] sched/x86: Save [ER]FLAGS on context switch Date: Mon, 20 May 2019 14:13:55 +0200 Message-Id: <20190520115232.817430690@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190520115231.137981521@linuxfoundation.org> References: <20190520115231.137981521@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 @@ -234,6 +234,7 @@ ENTRY(__switch_to_asm) pushl %ebx pushl %edi pushl %esi + pushfl /* switch stack */ movl %esp, TASK_threadsp(%eax) @@ -256,6 +257,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 @@ -342,6 +342,7 @@ ENTRY(__switch_to_asm) pushq %r13 pushq %r14 pushq %r15 + pushfq /* switch stack */ movq %rsp, TASK_threadsp(%rdi) @@ -364,6 +365,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 @@ -41,6 +41,7 @@ asmlinkage void ret_from_fork(void); * order of the fields must match the code in __switch_to_asm(). */ 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 @@ -132,6 +132,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 @@ -278,6 +278,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;