Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp1575232ybk; Sun, 10 May 2020 21:56:03 -0700 (PDT) X-Google-Smtp-Source: APiQypJW7bJudOdPRzVufXnrNxAU/cIOA+qTDjLFr8jQh23LCsms0ROyQPhIlx1sMWG8asliScDP X-Received: by 2002:a50:f005:: with SMTP id r5mr11011617edl.376.1589172962947; Sun, 10 May 2020 21:56:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589172962; cv=none; d=google.com; s=arc-20160816; b=rsMWXm3KsKE1ZDvjh0eTuvkqYL1QB9UTUjmWne2BEEU+Cj6UwcVF2Qq40ORQYKwAxN 7wrnD5KnnDUQ1TkuzTmIw5tbg5ZaaEkpLY2AygqIZUsQvYEIhofvFdGwiYh+V0hOJiMd rYVnnMdu/jxARLIFX8mcQ9O5aAMaswwQ2EAmYe9vrzCQS7/pBhp8LOEWBvNDNKj7yyOL coBkVEHzZ9ZSou087aUcBm6LHdK+cQmPT+Z2A1i2RyUpzVb2ZjMiRM5sNgpTaMTY+wz/ 8cpk/OwBVMBPKpglEj+eIbMeoZS94os7IGhk2MKCqV1mhWwH0p43LdPMsyYDSGntmBgz l5ew== 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=fUbh4Rmsy4Jg3xyIC2K0QD9tde8rsFJpc0OcSYl9rjc=; b=bCy4CiudmYnLyOixLT6LtK7NCTVwM9TEibZctOF+aTHARuMGl8fzR/FV5mlhsrRMe7 AWToNGLPlladHKTq7ypM1xElkx0JCQ5pz2zlVYgEWP4iJohGptXZdRvp9aQEbAcg0Z78 RBleF1himas0wRevHuna4m7Bedg8h5Zb61s/XiC0f/xEwFyGCcedj34FCX12FHT4oVLT CRTnJYrGCg4XpVr5Y+yqZcaTEYhRUkaDDWmjjvcbZ8lqkup+xuWX9Ugb2hoOtAxLM0q0 scUJZGpfgXnV5tmG0kczQJ00ZQU6z87KaIT2ExcSaLiUFs46KtxRPQDVEYTCxcE+qrrn LFlw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Hu1kEMip; 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 by15si127986edb.259.2020.05.10.21.55.33; Sun, 10 May 2020 21:56:02 -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=Hu1kEMip; 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 S1728888AbgEKExt (ORCPT + 99 others); Mon, 11 May 2020 00:53:49 -0400 Received: from mail.kernel.org ([198.145.29.99]:37826 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728642AbgEKExo (ORCPT ); Mon, 11 May 2020 00:53:44 -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 39D062192A; Mon, 11 May 2020 04:53:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1589172823; bh=Cpeq8BTPLVSwmsx8ZNMVkOr4GPcFGydAh+FmNcyX5NM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Hu1kEMipQMAr9zsOt9SqEoOaki/+bINbDzjCbTisoOg7HYj/KhD941FycSZMQ9nYv CbRxoC9Ru+ZbuhEYrxzUgz+WThydKwDjE6KFeHfwxSIk1PEel0QuX6UAuWEo7CmWm/ /jG5LMb2HUBPfueeEnQWQFEMvFnF8C3HJGoB4WSU= 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 Subject: [PATCH v12 12/18] x86/fsgsbase/64: move save_fsgs to header file Date: Mon, 11 May 2020 00:53:05 -0400 Message-Id: <20200511045311.4785-13-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 Given copy_thread_tls() is now shared between 32 and 64 bit and we need to use save_fsgs() there, move it to a header file. Signed-off-by: Sasha Levin --- arch/x86/kernel/process.h | 72 ++++++++++++++++++++++++++++++++++++ arch/x86/kernel/process_64.c | 68 ---------------------------------- 2 files changed, 72 insertions(+), 68 deletions(-) diff --git a/arch/x86/kernel/process.h b/arch/x86/kernel/process.h index 1d0797b2338a2..2360d340cbf00 100644 --- a/arch/x86/kernel/process.h +++ b/arch/x86/kernel/process.h @@ -37,3 +37,75 @@ static inline void switch_to_extra(struct task_struct *prev, prev_tif & _TIF_WORK_CTXSW_PREV)) __switch_to_xtra(prev, next); } + +#ifdef CONFIG_X86_64 + +enum which_selector { + FS, + GS +}; + +/* + * Saves the FS or GS base for an outgoing thread if FSGSBASE extensions are + * not available. The goal is to be reasonably fast on non-FSGSBASE systems. + * It's forcibly inlined because it'll generate better code and this function + * is hot. + */ +static __always_inline void save_base_legacy(struct task_struct *prev_p, + unsigned short selector, + enum which_selector which) +{ + if (likely(selector == 0)) { + /* + * On Intel (without X86_BUG_NULL_SEG), the segment base could + * be the pre-existing saved base or it could be zero. On AMD + * (with X86_BUG_NULL_SEG), the segment base could be almost + * anything. + * + * This branch is very hot (it's hit twice on almost every + * context switch between 64-bit programs), and avoiding + * the RDMSR helps a lot, so we just assume that whatever + * value is already saved is correct. This matches historical + * Linux behavior, so it won't break existing applications. + * + * To avoid leaking state, on non-X86_BUG_NULL_SEG CPUs, if we + * report that the base is zero, it needs to actually be zero: + * see the corresponding logic in load_seg_legacy. + */ + } else { + /* + * If the selector is 1, 2, or 3, then the base is zero on + * !X86_BUG_NULL_SEG CPUs and could be anything on + * X86_BUG_NULL_SEG CPUs. In the latter case, Linux + * has never attempted to preserve the base across context + * switches. + * + * If selector > 3, then it refers to a real segment, and + * saving the base isn't necessary. + */ + if (which == FS) + prev_p->thread.fsbase = 0; + else + prev_p->thread.gsbase = 0; + } +} + +static __always_inline void save_fsgs(struct task_struct *task) +{ + savesegment(fs, task->thread.fsindex); + savesegment(gs, task->thread.gsindex); + if (static_cpu_has(X86_FEATURE_FSGSBASE)) { + /* + * If FSGSBASE is enabled, we can't make any useful guesses + * about the base, and user code expects us to save the current + * value. Fortunately, reading the base directly is efficient. + */ + task->thread.fsbase = rdfsbase(); + task->thread.gsbase = x86_gsbase_read_cpu_inactive(); + } else { + save_base_legacy(task, task->thread.fsindex, FS); + save_base_legacy(task, task->thread.gsindex, GS); + } +} + +#endif diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c index e066750be89a0..4be88124d81ea 100644 --- a/arch/x86/kernel/process_64.c +++ b/arch/x86/kernel/process_64.c @@ -145,74 +145,6 @@ void release_thread(struct task_struct *dead_task) WARN_ON(dead_task->mm); } -enum which_selector { - FS, - GS -}; - -/* - * Saves the FS or GS base for an outgoing thread if FSGSBASE extensions are - * not available. The goal is to be reasonably fast on non-FSGSBASE systems. - * It's forcibly inlined because it'll generate better code and this function - * is hot. - */ -static __always_inline void save_base_legacy(struct task_struct *prev_p, - unsigned short selector, - enum which_selector which) -{ - if (likely(selector == 0)) { - /* - * On Intel (without X86_BUG_NULL_SEG), the segment base could - * be the pre-existing saved base or it could be zero. On AMD - * (with X86_BUG_NULL_SEG), the segment base could be almost - * anything. - * - * This branch is very hot (it's hit twice on almost every - * context switch between 64-bit programs), and avoiding - * the RDMSR helps a lot, so we just assume that whatever - * value is already saved is correct. This matches historical - * Linux behavior, so it won't break existing applications. - * - * To avoid leaking state, on non-X86_BUG_NULL_SEG CPUs, if we - * report that the base is zero, it needs to actually be zero: - * see the corresponding logic in load_seg_legacy. - */ - } else { - /* - * If the selector is 1, 2, or 3, then the base is zero on - * !X86_BUG_NULL_SEG CPUs and could be anything on - * X86_BUG_NULL_SEG CPUs. In the latter case, Linux - * has never attempted to preserve the base across context - * switches. - * - * If selector > 3, then it refers to a real segment, and - * saving the base isn't necessary. - */ - if (which == FS) - prev_p->thread.fsbase = 0; - else - prev_p->thread.gsbase = 0; - } -} - -static __always_inline void save_fsgs(struct task_struct *task) -{ - savesegment(fs, task->thread.fsindex); - savesegment(gs, task->thread.gsindex); - if (static_cpu_has(X86_FEATURE_FSGSBASE)) { - /* - * If FSGSBASE is enabled, we can't make any useful guesses - * about the base, and user code expects us to save the current - * value. Fortunately, reading the base directly is efficient. - */ - task->thread.fsbase = rdfsbase(); - task->thread.gsbase = x86_gsbase_read_cpu_inactive(); - } else { - save_base_legacy(task, task->thread.fsindex, FS); - save_base_legacy(task, task->thread.gsindex, GS); - } -} - #if IS_ENABLED(CONFIG_KVM) /* * While a process is running,current->thread.fsbase and current->thread.gsbase -- 2.20.1