Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp1273157img; Tue, 19 Mar 2019 04:19:08 -0700 (PDT) X-Google-Smtp-Source: APXvYqzkUXgROjr6bywkPmlj58LPZfAg0f83XkOY1JiLn/Jz5hKRIzqWS+avyiQtweFUVcrdWCl5 X-Received: by 2002:a63:5b4b:: with SMTP id l11mr497910pgm.82.1552994348384; Tue, 19 Mar 2019 04:19:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1552994348; cv=none; d=google.com; s=arc-20160816; b=txNUpxnj8V/1CmDCjyiQ/4KYj+GCcA50zL17CwQR7uTAI/jZFBshaEgwzscghHR+HF K4o0ZAiI2ViksuexcsS6QyUGdnp4zVkf4rSDyq0sXpOLnNgksZTBCXO7DbLCl9GUF0DG NvffyGYg/15ZSUwpTCJn6XonRycej3vjKMszxX5IJR9Eq4DJoeTjUGsfS9wVubpKlL+I BeXnkIpr6qJZvQVbsN5Elyo/mIzjg6qygVsRlFMfoN+7IwEWkCwFyoPkkBiMkCaZtgYR gWNaeMw9z1B3NGKQCEjoK4AS7T4HKtYJ+mEvK0RQYiAPfHIcmZZ6wBh2nLvn2ZAplTSF yycA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=YFE++n+6Eg30Gx7jH6uNm8tdUcNwoEUkWXwRnaDmLJ4=; b=N5FZ1sE9s2x3OvcsZbv/q2hg8WcWphc54YzjYSpYuADVnwKuKNOAQVHCVAkioKn+wo 2HMVwGoF8oziBJfQri38EntKbR0SiviuzQnASNQ/rjEN09i3thBeGdUXOkTOs/gJEcF8 0i+FyzQr3FYz1+9n06r+cl5XUcZM9mQOJQxM2F8ZC3EGqj3TNgsGypc/XqrU9SMPg9rk MScYjSGpwDf6DnAADsrApmKSUTdhVSQv0ORAXczqilG0Ar8OWprAemTlPa6CX3A5jKOw adq7BHAPyxLP9LKawcvBKXeILmPvOBnqQ5gpXPxKeah/myq1fiwjqYWOBAEcfJahzC8H jWGQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=jV77mtP0; 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 z3si11873122pgr.90.2019.03.19.04.18.52; Tue, 19 Mar 2019 04:19:08 -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=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=jV77mtP0; 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 S1727210AbfCSLQt (ORCPT + 99 others); Tue, 19 Mar 2019 07:16:49 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:47302 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725862AbfCSLQt (ORCPT ); Tue, 19 Mar 2019 07:16:49 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=In-Reply-To:Content-Type:MIME-Version :References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=YFE++n+6Eg30Gx7jH6uNm8tdUcNwoEUkWXwRnaDmLJ4=; b=jV77mtP0gfTs6Pgt+a7kIwjx2 mP/9iSEHAlHr+aRaYsFDJEtUfcCVXv+vQonhkLFy0R/gY23Cr9pUih8C+12Qv835KMOzu8hanjzmn /ZL5YthBLcmwOFCgauPmmIKLy7jQ1A+nD0lb1X5C2odCGU1M2QD2Z5yoSXHYz4hEKG3HlKrQZxXHA /dOIcMGYb9JxWYOZN9mD1aRK1bDmD+o7/1uQDtn4bS6XZ8j4IoTUsIun7VZtvXRkwKVOVcFwHJfRl J0vIbsi7sQ7NHqhaW2TDfprhGdv9YJRfcHdGfjr+Rm7LBnO7/w7N4xdNqS3nKvnMv92BmYqLdjuAq TVNi8eHiA==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=hirez.programming.kicks-ass.net) by bombadil.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1h6CjS-0000su-Jm; Tue, 19 Mar 2019 11:16:34 +0000 Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 1685F23E8CA31; Tue, 19 Mar 2019 12:16:33 +0100 (CET) Date: Tue, 19 Mar 2019 12:16:33 +0100 From: Peter Zijlstra To: torvalds@linux-foundation.org, tglx@linutronix.de, hpa@zytor.com, julien.thierry@arm.com, will.deacon@arm.com, luto@amacapital.net, mingo@kernel.org, catalin.marinas@arm.com, james.morse@arm.com, valentin.schneider@arm.com, brgerst@gmail.com, jpoimboe@redhat.com, luto@kernel.org, bp@alien8.de, dvlasenk@redhat.com Cc: linux-kernel@vger.kernel.org, dvyukov@google.com, rostedt@goodmis.org Subject: [PATCH 01/25] sched/x86: Save [ER]FLAGS on context switch Message-ID: <20190319111633.GY6058@hirez.programming.kicks-ass.net> References: <20190318153840.906404905@infradead.org> <20190318155139.963285969@infradead.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190318155139.963285969@infradead.org> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org New patch #1 --- Subject: sched/x86: Save [ER]FLAGS on context switch From: Peter Zijlstra Date: Thu Feb 14 10:30:52 CET 2019 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. Fixes: 5b24a7a2aa20 ("Add 'unsafe' user access functions for batched accesses") Acked-by: Andy Lutomirski Reported-by: Julien Thierry Signed-off-by: Peter Zijlstra (Intel) --- 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 @@ -650,6 +650,7 @@ ENTRY(__switch_to_asm) pushl %ebx pushl %edi pushl %esi + pushfl /* switch stack */ movl %esp, TASK_threadsp(%eax) @@ -672,6 +673,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 @@ -291,6 +291,7 @@ ENTRY(__switch_to_asm) pushq %r13 pushq %r14 pushq %r15 + pushfq /* switch stack */ movq %rsp, TASK_threadsp(%rdi) @@ -313,6 +314,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 @@ -127,6 +127,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 @@ -392,6 +392,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;