Received: by 2002:ac0:aa62:0:0:0:0:0 with SMTP id w31-v6csp1974190ima; Thu, 25 Oct 2018 07:48:09 -0700 (PDT) X-Google-Smtp-Source: AJdET5eHPiWQNi9QHoMDPOShzxxVGihw8HM/HZVRKK6fME5nf1GjD2cItMUH5y5s802kOFYv8062 X-Received: by 2002:a65:62d5:: with SMTP id m21-v6mr1775341pgv.243.1540478889491; Thu, 25 Oct 2018 07:48:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1540478889; cv=none; d=google.com; s=arc-20160816; b=XdXc0H+YlAzl4DY39tIusVmBOD/kgMpJ77Olk6kWdH3z/L0RuhhsMdUsA6CgCqpO9r /mzTPKsrOWvIuLQhl83mOJdiIwp3qEEgvggquBbQI3VJ0n4JEEp1WQUELpY86+c+3LWo LZ98KRf82r1WeLEuM91Jltmr04jeNvMRT5InjqemEOBp53hXXiGL3ODx+8/3r0gIxZ5E rXWFNvCyQZz9PQnhhMjEqixgKViJIA5uDb5Nt3crvTJThFDsGiIzERtDlxJ3jNLltSmW N+s0hBwAHWEiX8z+k8F3ZH8goYRpCYWCc+JHAbRhzZhdJVBAXZuENSwcVbD3k9SMWwz1 LC6g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=d4OmMBpl4mpYwXH0KRX2SzbqyalaZlsZ0UGJdlWnyVs=; b=b/qV06elJ2QR3xebhMp3APvgKPkmWG6/ykvbru5HUunc2acKMh4wD3LCU3m3nkMXQ3 h1IJQ4wvLNInWlHVOQ0c6N1sNLtrgKP5jArYv8q/W42QzBv7esscaADUKK+GBPQGT4li a8MbCOfXFz88v2uswaUXaYDa8HFJZjR9ioBVnf63akiGDSn/ADddmLLK5fncDvU2/o2f izgIYnicyM5ZUrknlRHdUdzNdpwajU32ROVPbh59RElomEdVixsEunqAm2may6PMtxXm jf68qVmPy3V3/9nM2AfJEar+5m6zUsARv7mJf2BFjPoQ++8oq4XCpcW1Jr09/9Re0FK0 xqKQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=pMIlBNKF; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m138-v6si9151319pfd.80.2018.10.25.07.47.53; Thu, 25 Oct 2018 07:48:09 -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=pMIlBNKF; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727823AbeJYWn6 (ORCPT + 99 others); Thu, 25 Oct 2018 18:43:58 -0400 Received: from mail.kernel.org ([198.145.29.99]:52424 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727699AbeJYWn5 (ORCPT ); Thu, 25 Oct 2018 18:43:57 -0400 Received: from sasha-vm.mshome.net (unknown [167.98.65.38]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 1EA262083E; Thu, 25 Oct 2018 14:10:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1540476661; bh=i5uOvyglvWNt+vZl/nI8/GZEViwwpoNlfavDownWPvs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pMIlBNKFl0ojmVTwqxzcRIOkDTd0YdxVv6iLhzxO+1Il5pAtIhuQHHqo2Oohs0aPP /h6MWEPuBT58LtNFYC46mB/E69OHvo8oROJYjUWPDNT6RfNMspGbJZ/1yAfQE9i3gQ BzO5/7x1PZQ6ww4kfrbnlpptjcPI0b/KJ0Mg8UBo= From: Sasha Levin To: stable@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Andy Lutomirski , Thomas Gleixner , Peter Zijlstra , Borislav Petkov , Linus Torvalds , Ingo Molnar , Sasha Levin Subject: [PATCH AUTOSEL 4.14 04/46] x86/power: Fix some ordering bugs in __restore_processor_context() Date: Thu, 25 Oct 2018 10:10:11 -0400 Message-Id: <20181025141053.213330-4-sashal@kernel.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181025141053.213330-1-sashal@kernel.org> References: <20181025141053.213330-1-sashal@kernel.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Andy Lutomirski [ Upstream commit 5b06bbcfc2c621da3009da8decb7511500c293ed ] __restore_processor_context() had a couple of ordering bugs. It restored GSBASE after calling load_gs_index(), and the latter can call into tracing code. It also tried to restore segment registers before restoring the LDT, which is straight-up wrong. Reorder the code so that we restore GSBASE, then the descriptor tables, then the segments. This fixes two bugs. First, it fixes a regression that broke resume under certain configurations due to irqflag tracing in native_load_gs_index(). Second, it fixes resume when the userspace process that initiated suspect had funny segments. The latter can be reproduced by compiling this: // SPDX-License-Identifier: GPL-2.0 /* * ldt_echo.c - Echo argv[1] while using an LDT segment */ int main(int argc, char **argv) { int ret; size_t len; char *buf; const struct user_desc desc = { .entry_number = 0, .base_addr = 0, .limit = 0xfffff, .seg_32bit = 1, .contents = 0, /* Data, grow-up */ .read_exec_only = 0, .limit_in_pages = 1, .seg_not_present = 0, .useable = 0 }; if (argc != 2) errx(1, "Usage: %s STRING", argv[0]); len = asprintf(&buf, "%s\n", argv[1]); if (len < 0) errx(1, "Out of memory"); ret = syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)); if (ret < -1) errno = -ret; if (ret) err(1, "modify_ldt"); asm volatile ("movw %0, %%es" :: "rm" ((unsigned short)7)); write(1, buf, len); return 0; } and running ldt_echo >/sys/power/mem Without the fix, the latter causes a triple fault on resume. Fixes: ca37e57bbe0c ("x86/entry/64: Add missing irqflags tracing to native_load_gs_index()") Reported-by: Jarkko Nikula Signed-off-by: Andy Lutomirski Signed-off-by: Thomas Gleixner Tested-by: Jarkko Nikula Cc: Peter Zijlstra Cc: Borislav Petkov Cc: Linus Torvalds Link: https://lkml.kernel.org/r/6b31721ea92f51ea839e79bd97ade4a75b1eeea2.1512057304.git.luto@kernel.org Signed-off-by: Ingo Molnar Signed-off-by: Sasha Levin --- arch/x86/power/cpu.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/arch/x86/power/cpu.c b/arch/x86/power/cpu.c index 04d5157fe7f8..a51d2dfb57d1 100644 --- a/arch/x86/power/cpu.c +++ b/arch/x86/power/cpu.c @@ -228,8 +228,20 @@ static void notrace __restore_processor_state(struct saved_context *ctxt) load_idt((const struct desc_ptr *)&ctxt->idt_limit); #endif +#ifdef CONFIG_X86_64 /* - * segment registers + * We need GSBASE restored before percpu access can work. + * percpu access can happen in exception handlers or in complicated + * helpers like load_gs_index(). + */ + wrmsrl(MSR_GS_BASE, ctxt->gs_base); +#endif + + fix_processor_context(); + + /* + * Restore segment registers. This happens after restoring the GDT + * and LDT, which happen in fix_processor_context(). */ #ifdef CONFIG_X86_32 loadsegment(es, ctxt->es); @@ -250,13 +262,14 @@ static void notrace __restore_processor_state(struct saved_context *ctxt) load_gs_index(ctxt->gs); asm volatile ("movw %0, %%ss" :: "r" (ctxt->ss)); + /* + * Restore FSBASE and user GSBASE after reloading the respective + * segment selectors. + */ wrmsrl(MSR_FS_BASE, ctxt->fs_base); - wrmsrl(MSR_GS_BASE, ctxt->gs_base); wrmsrl(MSR_KERNEL_GS_BASE, ctxt->gs_kernel_base); #endif - fix_processor_context(); - do_fpu_end(); tsc_verify_tsc_adjust(true); x86_platform.restore_sched_clock_state(); -- 2.17.1