Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp550261ybk; Sat, 9 May 2020 10:41:00 -0700 (PDT) X-Google-Smtp-Source: APiQypLyyIqAXG4TyYfUSJkb3kAs3k3braDs063wClyjtgqyAy/4tYwz8TqT4CzUnWqZplctQIqR X-Received: by 2002:a05:6402:1762:: with SMTP id da2mr5408112edb.352.1589046060095; Sat, 09 May 2020 10:41:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589046060; cv=none; d=google.com; s=arc-20160816; b=zkiqEv9Xf3rLvRUWc2D/Dg/KeJNP5kX2ieTM3FmWYERMnNERQ7xeIEe/Y8bUQZ7Mz/ kidx91ypzUkjOPdk1iKiEYba1ZNlIisX2d9O1swAN6XO6T5K6ijyI/RZCwv0q4rWEX9p OadDM/1mdEeWMJeZVbNWReaUzN92MnSV5wvvhfuVtxQC3jveHVSTqSc7uoN/5aLyLDNc 4f+RrZpBo2d3VCiPi2bVJLIO8P78yZ6sSA6tDEhPcDhQLfA5qVTFHXycou042Nl+pm+G qW959k/BmArQd/0MOhv5b3+sPETp3HUbxiOyKCyfBijnaeaj4+lYmpnseBZLMpEm2GvO xK0w== 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=SuYx+s2zedpWcdvw9o69S9butnLCJJlhQ6iDa6ESHhY=; b=kishUli+IURNp+cskTIJMhEo4kzgRhJ6UbJ0+IgikU6ny42NuTVesZ5Y0Hdp6+hE9w dYsw6hgcfloUZeJNmDl6kaPJ/GeeEGC4QkKcsNldQ60B7iUnTT2HKX092hr+sDSyemTl iefhPHp/ksuzKpdbDWti/NsmJ6SIyksst5COl28XmRo2b8dO/lh18xZOXEjJIdwYdBRi bJk31zILjJlF1K11HExvmSU3Kp1z2c73szmk+yhIDudsh6zk5rnRxjXoBkkBN8TN7ama Wu5RcqTNTH6DMKgyGjmNboTdTpc0Dw/Gck89njsLQk49RBdXIxqmBy0smBgm9SYO1QEt LAgA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=EcNJuDUZ; 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 u2si3245192ejr.370.2020.05.09.10.40.37; Sat, 09 May 2020 10:41:00 -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=EcNJuDUZ; 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 S1728788AbgEIRiB (ORCPT + 99 others); Sat, 9 May 2020 13:38:01 -0400 Received: from mail.kernel.org ([198.145.29.99]:54314 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728340AbgEIRhF (ORCPT ); Sat, 9 May 2020 13:37:05 -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 3913224960; Sat, 9 May 2020 17:37:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1589045825; bh=PHIzsR/nq6FXDEA4DvjmmqDeRmjCdzHAA3mINfh7ypo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EcNJuDUZWyZRm66TO4qNPm2nAUB/juu9mIjSaO3PkL/J1PjmrH4smIsMExiBt7EF+ Qnm3WjlwUYk43BR2BRDqr+v8Nqoe+uCGctEs7pCd3fJmQKA/cL6mPoyy/35Hq7thrY /Rk3z6hwMI91EOGy75n95D++Kv2PusGYIlfhAe7w= 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 v11 04/18] x86/entry/64: Clean up paranoid exit Date: Sat, 9 May 2020 13:36:41 -0400 Message-Id: <20200509173655.13977-5-sashal@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200509173655.13977-1-sashal@kernel.org> References: <20200509173655.13977-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 0e9504fabe526..3adb3c8e2409b 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