Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp268973yba; Mon, 20 May 2019 08:24:17 -0700 (PDT) X-Google-Smtp-Source: APXvYqw6/55vIl4jH7U0DWwEQVkeypucJ+ABrOAVnOU0578qh+B2m5yMkNwZjsG1eOObYPWoq1mX X-Received: by 2002:a17:902:e60a:: with SMTP id cm10mr68083657plb.316.1558365857275; Mon, 20 May 2019 08:24:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558365857; cv=none; d=google.com; s=arc-20160816; b=1KP8UKVfeKnfO7rvDglmBUBbRgZubRQ9Z+lIMHFWrQXPcvJbE8IiUfBaPZ2tO1hzet ET8G5C/Z89vddxnt/2TF2Ww3NEwI+9wUMOGoYsO/45XYiHkTOOviNeSeIr5vcBOu1Tiy JeZw08KcT4o8qDiO7JWJEH55SAqgkkPOuR9ps4HivNG9LjcobJuCuvJha2cCr/m/ZeuQ 1et68A3XSusmMom7g/yGhVTI7pFubB7/7gi37nPeprwiVUGL4drSylIOX9hzFAM0tXWT gRL9Uh6M6+jA8f1J8ptfoFznvEhRZ29bhQNwXAY16HsO3jnsFY5PGKLIXSxS8pdYMYSi g0zw== 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=nV7zyutnEZgy6oEb10jMXXf4kkOGAKubsE4ViyZoV/Q=; b=flFAV1Z/4PCSmJspC4R+N5jnLZrvU/7B9nbX6ydTZoJ7hgF/Ap8NC+VNGQsOwcVfVL 8SHgOJQJ31cEvqvcNTkHu+U6+prsef460QxUWsVvozHwY0mcmnTJ4IhUcbe3M1icOXJ0 gTyNurS1kwmjWt2tUQD6l66eDYVERVQ2OOLankyaxCucn4cae4uXvlNdj7hjkPE7mmuu JlPSSmfZR2vFXa5SNyc/f5oOHI3znDAhJmFG7ZCcwY0wMBRcRxydrca9VpoDFaVZZo67 TbZkb5/tD0jYViUy/o3Sohku/dlQfJ1mtKX+WwM7l57sBOo12gCA5zMc+GgjqZbX+ePP FO0A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="eGl1M/jF"; 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 3si18035283pfp.288.2019.05.20.08.24.02; Mon, 20 May 2019 08:24:17 -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="eGl1M/jF"; 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 S2388179AbfETMVa (ORCPT + 99 others); Mon, 20 May 2019 08:21:30 -0400 Received: from mail.kernel.org ([198.145.29.99]:35136 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387506AbfETMV2 (ORCPT ); Mon, 20 May 2019 08:21:28 -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 CC9D5213F2; Mon, 20 May 2019 12:21:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1558354887; bh=JDbR6yD0S0b+vjlwwNjCZXrruQmzJ3nnstiZDzZHZ2I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eGl1M/jFDYoS13k/JyBENb8W+N0vVcwEVZ0NTEbcOzJlz2dlF54P2eNpB+ZwrImNx SGKyWMOKht3850Pz6ccOwf2FK6zhMxYjdmsNIfsDGIuCa9NhhnRwxSVgeH0Yydnegb y8XJQQyrnoatsE9dCYZqD8ha6BumoJn9jgehd1BE= 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.19 017/105] sched/x86: Save [ER]FLAGS on context switch Date: Mon, 20 May 2019 14:13:23 +0200 Message-Id: <20190520115248.202554691@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190520115247.060821231@linuxfoundation.org> References: <20190520115247.060821231@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 @@ -648,6 +648,7 @@ ENTRY(__switch_to_asm) pushl %ebx pushl %edi pushl %esi + pushfl /* switch stack */ movl %esp, TASK_threadsp(%eax) @@ -670,6 +671,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 @@ -352,6 +352,7 @@ ENTRY(__switch_to_asm) pushq %r13 pushq %r14 pushq %r15 + pushfq /* switch stack */ movq %rsp, TASK_threadsp(%rdi) @@ -374,6 +375,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 @@ -40,6 +40,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 @@ -130,6 +130,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 @@ -300,6 +300,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;