Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp1575968ybk; Sun, 10 May 2020 21:57:50 -0700 (PDT) X-Google-Smtp-Source: APiQypLxlNByIoKPkfnOjLAs3xJl7eCtURR1Fgj/IsT0gnsWlGzEioA98EYGeuSAsHVlRbcjp8/+ X-Received: by 2002:a05:6402:759:: with SMTP id p25mr12191369edy.102.1589173070725; Sun, 10 May 2020 21:57:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589173070; cv=none; d=google.com; s=arc-20160816; b=owiVe9n7xeWO5kbtBO9gHNyEyUZSX1SqLsEEbkLReUBV+L6bptW2h2FGKDz59u7Pfq 5isbjLLtiqsoLAeDbnSPZAIrupe+uby8iHV+tCGn22ApTSHNrWH1QU6pBjF4UD8YcsOj EsXdxUrj1dpKx8ykXpJ+nPB6iSnxPFaNTcPZXRuIXM8WGZnOuoo2yFENdR3ziGUAo0gb /KGV9joP8MPqax2Ww2zYnbyY0LqDlbNriESWQ1Za8GvR/VUIzPH2EiTrjxUW/jc7+z52 +wsa+Cn3FCmjOveYtyNLoKURPgeX64l3vQW9vgZt94kR/BuugfVYe1AFVPEylisPd5WR WZbQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=PuWZj38t1yl3WUQ1gy+aDjP8nkXxj8HTyQv0md36i9k=; b=qcfpH1VWu3rCzBa1fzRZPt7dyPcfMDe4J+0zY2XKZerrcPzJpNYSRRb5MAahrfX2d/ fFHwBYzaTl5A8aabRh/zNOtRoIGVwmEW0ue38naTmuYN3h4NPd199nGDX5LmL0fwBNHA Q9tkHfCG9dlaE9bU9sx0YXo4lNGJyz4tMRNch3MHt7ti8fxLjrjVJRi0fhzMqmQGAmih CZb6Uozszt3yjLe3yM/UhWuhtLAD6ftba5QDp9wFTxUElccFFxqXlDe+Z53ns70tcQMI dgNY7b4J0YbpxttVkrDVNUk10v9S6uVi5LWT7ERS9wfNoDhSxzwjlds2CAibjG+Q5+kF MVqw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=z4of81+q; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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. [23.128.96.18]) by mx.google.com with ESMTP id l6si1444232ejx.387.2020.05.10.21.57.23; Sun, 10 May 2020 21:57:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=z4of81+q; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S1728396AbgEKExd (ORCPT + 99 others); Mon, 11 May 2020 00:53:33 -0400 Received: from mail.kernel.org ([198.145.29.99]:37280 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728356AbgEKExc (ORCPT ); Mon, 11 May 2020 00:53:32 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (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 3766B24953; Mon, 11 May 2020 04:53:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1589172812; bh=gpPTyRYEHCVUZzxNy70fQ1duYwfPpzkaeocUDOMazW8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=z4of81+qAjATUEZZNxQfMtf6JEGravhTuY0P01z2C4v5sYQYsJF+NVfiSAJkH/5+a 3y/SFAIjoASSfQPmB4BB3y0MDO9DqAxAiBFLcO7oAoojbZcrGFp2wajOf+p/hC1DzT lyrhgkYmH9+4ejSmM+FXVzeQE9jAErJhvRp/H+Lc= From: Sasha Levin 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, Sasha Levin , Vegard Nossum Subject: [PATCH v12 04/18] x86/entry/64: Clean up paranoid exit Date: Mon, 11 May 2020 00:52:57 -0400 Message-Id: <20200511045311.4785-5-sashal@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200511045311.4785-1-sashal@kernel.org> References: <20200511045311.4785-1-sashal@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Sasha Levin 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 --- arch/x86/entry/entry_64.S | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/arch/x86/entry/entry_64.S b/arch/x86/entry/entry_64.S index 3063aa9090f9a..0da56e6791b73 100644 --- a/arch/x86/entry/entry_64.S +++ b/arch/x86/entry/entry_64.S @@ -1266,19 +1266,25 @@ SYM_CODE_END(paranoid_entry) SYM_CODE_START_LOCAL(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 restore_regs_and_return_to_kernel -.Lparanoid_exit_no_swapgs: - TRACE_IRQS_IRETQ_DEBUG - /* Always restore stashed CR3 value (see paranoid_entry) */ - RESTORE_CR3 scratch_reg=%rbx save_reg=%r14 - jmp restore_regs_and_return_to_kernel SYM_CODE_END(paranoid_exit) /* -- 2.20.1