Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp5768646imm; Mon, 23 Jul 2018 05:52:41 -0700 (PDT) X-Google-Smtp-Source: AAOMgpcwNYNS9+tBABV0KY2WV3gC1m58F3z0kVy2D2BJTmIs+HeAzCh7uLcaCecUYcYfSnCMgfjS X-Received: by 2002:a63:144b:: with SMTP id 11-v6mr12378532pgu.219.1532350361507; Mon, 23 Jul 2018 05:52:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1532350361; cv=none; d=google.com; s=arc-20160816; b=shp21Ze7hUhd5Z1a++qSBWBiseA3D6S9rKRj51VcviibdvMGhUjghR/+0vb9ywsYIZ PQgLveKym2gwD0xoXrZd6hUL85URRSQVK1XIEY1d039SoRcG8I+T4blIaQgKZM942pao UOilOiP8N1oEQgcPsWUvr/0vMFshmT6Dx0Xf5YY2UzeL32uguNTYVWGeQxcjdWJv2lre pqCCEWdJ/Gyjz9eD3Dt7sXF04RPN+teSnlrp/PS0Hvu81RDozK06LNfAGRV9kQcgDjVn MglEMVAw5auZanyrmITak2oOfNBNGoxofwuo9ikmUmGJ+fP8qHLd6T7GtdMFsp7zZpdt 6BSQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=HZR4nsZERh8csENk4TefCbBGte/P9V3j1Xqave5X3VE=; b=pdV08x2G5ePyp4WXt9VxINPyz85C/CJe95kcGjy+FOAwiC+7Mu12ltqWs20mY7xCN/ IRmKSLlPGM4CMrZca3y0qF6zx5f+J838JJpEqRdEurWHG4RMN3cFcbHspAr3JR0xQmF3 6/+tYsEC7B/uInzBEwAroPO/OyLpllbL4icZihuQuO7L0APb3nwNr7k9tdW2BnJNx7xx mzlYvMdUCGoR2yke0PEefMutmc0GqqRutSChOfD70+XjFWX/gUA0KbRGjK5jqygqIZDw I9sgoLHmV5gNs3DOxGWwtdreK3yknDp8cfruPlQMOCkV9+mGg7HxE0JuD9oK30X3v2E8 Ql6g== ARC-Authentication-Results: i=1; mx.google.com; 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 6-v6si8851819pgg.366.2018.07.23.05.52.27; Mon, 23 Jul 2018 05:52:41 -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; 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 S2388564AbeGWNv5 (ORCPT + 99 others); Mon, 23 Jul 2018 09:51:57 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:51470 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389457AbeGWNr5 (ORCPT ); Mon, 23 Jul 2018 09:47:57 -0400 Received: from localhost (LFbn-1-12238-233.w90-92.abo.wanadoo.fr [90.92.53.233]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 49EC4BE6; Mon, 23 Jul 2018 12:46:12 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Kyle Huey , Peter Zijlstra , Andy Lutomirski , Thomas Gleixner , David Woodhouse , "Srivatsa S. Bhat" , "Matt Helsley (VMware)" , Alexey Makhalov , Bo Gan Subject: [PATCH 4.4 064/107] x86/process: Correct and optimize TIF_BLOCKSTEP switch Date: Mon, 23 Jul 2018 14:41:58 +0200 Message-Id: <20180723122416.323704941@linuxfoundation.org> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180723122413.003644357@linuxfoundation.org> References: <20180723122413.003644357@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.4-stable review patch. If anyone has any objections, please let me know. ------------------ From: Kyle Huey commit b9894a2f5bd18b1691cb6872c9afe32b148d0132 upstream The debug control MSR is "highly magical" as the blockstep bit can be cleared by hardware under not well documented circumstances. So a task switch relying on the bit set by the previous task (according to the previous tasks thread flags) can trip over this and not update the flag for the next task. To fix this its required to handle DEBUGCTLMSR_BTF when either the previous or the next or both tasks have the TIF_BLOCKSTEP flag set. While at it avoid branching within the TIF_BLOCKSTEP case and evaluating boot_cpu_data twice in kernels without CONFIG_X86_DEBUGCTLMSR. x86_64: arch/x86/kernel/process.o text data bss dec hex 3024 8577 16 11617 2d61 Before 3008 8577 16 11601 2d51 After i386: No change [ tglx: Made the shift value explicit, use a local variable to make the code readable and massaged changelog] Originally-by: Thomas Gleixner Signed-off-by: Kyle Huey Cc: Peter Zijlstra Cc: Andy Lutomirski Link: http://lkml.kernel.org/r/20170214081104.9244-3-khuey@kylehuey.com Signed-off-by: Thomas Gleixner Signed-off-by: David Woodhouse Signed-off-by: Greg Kroah-Hartman Signed-off-by: Srivatsa S. Bhat Reviewed-by: Matt Helsley (VMware) Reviewed-by: Alexey Makhalov Reviewed-by: Bo Gan Signed-off-by: Greg Kroah-Hartman --- arch/x86/include/asm/msr-index.h | 1 + arch/x86/kernel/process.c | 12 +++++++----- 2 files changed, 8 insertions(+), 5 deletions(-) --- a/arch/x86/include/asm/msr-index.h +++ b/arch/x86/include/asm/msr-index.h @@ -150,6 +150,7 @@ /* DEBUGCTLMSR bits (others vary by model): */ #define DEBUGCTLMSR_LBR (1UL << 0) /* last branch recording */ +#define DEBUGCTLMSR_BTF_SHIFT 1 #define DEBUGCTLMSR_BTF (1UL << 1) /* single-step on branches */ #define DEBUGCTLMSR_TR (1UL << 6) #define DEBUGCTLMSR_BTS (1UL << 7) --- a/arch/x86/kernel/process.c +++ b/arch/x86/kernel/process.c @@ -223,13 +223,15 @@ void __switch_to_xtra(struct task_struct propagate_user_return_notify(prev_p, next_p); - if ((tifp ^ tifn) & _TIF_BLOCKSTEP) { - unsigned long debugctl = get_debugctlmsr(); + if ((tifp & _TIF_BLOCKSTEP || tifn & _TIF_BLOCKSTEP) && + arch_has_block_step()) { + unsigned long debugctl, msk; + rdmsrl(MSR_IA32_DEBUGCTLMSR, debugctl); debugctl &= ~DEBUGCTLMSR_BTF; - if (tifn & _TIF_BLOCKSTEP) - debugctl |= DEBUGCTLMSR_BTF; - update_debugctlmsr(debugctl); + msk = tifn & _TIF_BLOCKSTEP; + debugctl |= (msk >> TIF_BLOCKSTEP) << DEBUGCTLMSR_BTF_SHIFT; + wrmsrl(MSR_IA32_DEBUGCTLMSR, debugctl); } if ((tifp ^ tifn) & _TIF_NOTSC) {