Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp1228630ybp; Fri, 4 Oct 2019 11:22:05 -0700 (PDT) X-Google-Smtp-Source: APXvYqymbaQJrUFvthBGKg7Nwj9xMEGALFztDSxpnm5XHtHEWUvzTVOZseqS15Cfr0mCDqQOxGLP X-Received: by 2002:a17:906:d154:: with SMTP id br20mr13740842ejb.79.1570213325245; Fri, 04 Oct 2019 11:22:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570213325; cv=none; d=google.com; s=arc-20160816; b=HN/ywtU7qs0t4rukjPQUYXmLunQvaTy+seJynHQJMCPz6WUWwRb3Nu3LByXY6oEtmM J+bbbXQHpTFqaS8wNqOzSZfhf8uXY7oa5VTp9Ic7OiGFAY7UVm7ud2xRurmsh4VrbiR3 6BOCASU/QnfEq0xLpYhrrF9ohl4CmcIzEzWCR0rymo5H3IqzXdkbB2LmZMvd2s+dc2EO CuFe5kevs4ISm68tJEXgmT3QUsR6AfZWVEbEL0d0UjKdWt0XGCsKd6gSlpsAPfvUSih0 tOzoqO0IM+WZmrMRqqmptEXtIKOUZVPBHAYGQxFJ1u0sAZ+WjVG5p3N9RV/Xqmp+u7Ww lXWw== 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; bh=j6RCggjq8nraHTyBhw67B2j3jzcqG9KU7LJz/1Hkc0E=; b=gWJAxQXtkM8D8e2gSAi3CX9pY7wzh1hi7hj/jyV8yEcFKtZQ8BEtmOB/QaqJE9tzBa o64CF4MRYQ3XGcAXignEBUuKvmdlp0zGSB5jAY/Riiqctwic/i3xRyoxn0DQGlY/eRe7 Bi0TlkQRbQilQkfcwJ8Firoi4sQZbQlE+ZB6DmIi9oVAQLOugz822KHHyzQfsVa66tyN 98kX34DEGuTh4445xj1KHPCsbDKSijUm0QgFmOZv+kE9Bl17BQ0FFR5WV08jPJTLrsJ8 /cMkYLzW6ujzkJcLs4WoYwMq/j0hjQdVRYKQS7lwYZEadaQ6E25bZO5SPsN6OrwtG45c lZLg== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o22si3271424eju.25.2019.10.04.11.21.40; Fri, 04 Oct 2019 11:22:05 -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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730502AbfJDSRB (ORCPT + 99 others); Fri, 4 Oct 2019 14:17:01 -0400 Received: from mga07.intel.com ([134.134.136.100]:15508 "EHLO mga07.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730477AbfJDSRA (ORCPT ); Fri, 4 Oct 2019 14:17:00 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga105.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 04 Oct 2019 11:17:00 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.67,257,1566889200"; d="scan'208";a="204394759" Received: from chang-linux-3.sc.intel.com ([172.25.66.185]) by orsmga002.jf.intel.com with ESMTP; 04 Oct 2019 11:16:59 -0700 From: "Chang S. Bae" To: linux-kernel@vger.kernel.org, tglx@linutronix.de, bp@alien8.de, luto@kernel.org Cc: hpa@zytor.com, dave.hansen@intel.com, tony.luck@intel.com, ak@linux.intel.com, ravi.v.shankar@intel.com, chang.seok.bae@intel.com, Vegard Nossum Subject: [PATCH v9 04/17] x86/entry/64: Clean up paranoid exit Date: Fri, 4 Oct 2019 11:15:56 -0700 Message-Id: <1570212969-21888-5-git-send-email-chang.seok.bae@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1570212969-21888-1-git-send-email-chang.seok.bae@intel.com> References: <1570212969-21888-1-git-send-email-chang.seok.bae@intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Andy Lutomirski All that paranoid exit needs to do is to disable IRQs, handle IRQ tracing, then restore CR3, and restore GS base. Simply do those actions in that order. Cleaning up the spaghetti code. Signed-off-by: Andy Lutomirski Signed-off-by: Chang S. Bae Reviewed-by: Tony Luck Cc: Thomas Gleixner Cc: Borislav Petkov Cc: Andy Lutomirski Cc: H. Peter Anvin Cc: Dave Hansen Cc: Tony Luck Cc: Andi Kleen Cc: Vegard Nossum --- Changes from v8: none Changes from v7: * Included as a new patch. Took the cleanup part from the Andy Lutomirski's original patch [*] and edited its changelog a little bit. [*] https://lkml.kernel.org/r/59725ceb08977359489fbed979716949ad45f616.1562035429.git.luto@kernel.org --- arch/x86/entry/entry_64.S | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/arch/x86/entry/entry_64.S b/arch/x86/entry/entry_64.S index b7c3ea4..dd0d62a 100644 --- a/arch/x86/entry/entry_64.S +++ b/arch/x86/entry/entry_64.S @@ -1265,20 +1265,25 @@ END(paranoid_entry) ENTRY(paranoid_exit) UNWIND_HINT_REGS DISABLE_INTERRUPTS(CLBR_ANY) + + /* + * The order of operations is important. IRQ tracing requires + * kernel GS base and CR3. RESTORE_CR3 requires kernel GS base. + * + * NB to anyone to try to optimize this code: this code does + * not execute at all for exceptions from user mode. Those + * exceptions go through error_exit instead. + */ TRACE_IRQS_OFF_DEBUG - testl %ebx, %ebx /* swapgs needed? */ - jnz .Lparanoid_exit_no_swapgs - TRACE_IRQS_IRETQ - /* Always restore stashed CR3 value (see paranoid_entry) */ - RESTORE_CR3 scratch_reg=%rbx save_reg=%r14 + RESTORE_CR3 scratch_reg=%rax save_reg=%r14 + + /* If EBX is 0, SWAPGS is required */ + testl %ebx, %ebx + jnz restore_regs_and_return_to_kernel + + /* We are returning to a context with user GS base */ SWAPGS_UNSAFE_STACK - jmp .Lparanoid_exit_restore -.Lparanoid_exit_no_swapgs: - TRACE_IRQS_IRETQ_DEBUG - /* Always restore stashed CR3 value (see paranoid_entry) */ - RESTORE_CR3 scratch_reg=%rbx save_reg=%r14 -.Lparanoid_exit_restore: - jmp restore_regs_and_return_to_kernel + jmp restore_regs_and_return_to_kernel END(paranoid_exit) /* -- 2.7.4