Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp775301ybz; Wed, 29 Apr 2020 09:10:07 -0700 (PDT) X-Google-Smtp-Source: APiQypLaBnMLW9yWrSj/nrOpufoyWr+rY606MVfVnPQq7Pux7fs5NvQ+RfsSXsC4TxUOtwsMm/B/ X-Received: by 2002:a50:cb84:: with SMTP id k4mr3247501edi.89.1588176606868; Wed, 29 Apr 2020 09:10:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588176606; cv=none; d=google.com; s=arc-20160816; b=BX94plkvvTmYEtKydqmz6c0AjGYTE9TrXx78SDf1h7SI88A0LbNJFpACFRb4arrSpd XHtb4rI97Z51SCvnJslDRhhWGn9xAsXPzojGIO1jAjFJ2zxczetfC5dCrkniBWV7EKZ7 aoHzfFjf/F4pTh1eOgB8MPnBbQepnzMuwFLXp6r+iItpmS/FH5Myr39f0gFGwseFs2Gu Zww/JRFP3pSmuK3lWiwLbxPXus7MU9qauGZROS45q37dd+YOEuvVqumgfqD8aiIzO9FR UBSdmt3cm/87PsH6bTl3+S4HFIMdzLY8ouQHc6WcZtPLgKy4C/F6oK42Pyz96hf0yfja gqGQ== 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 :ironport-sdr:ironport-sdr; bh=acdUAIvk7rQtF2W0mM3/4B1V5ZvtJYB/a8JDYXtQO4o=; b=dFACaE/V6ZiYe9AUP6oHfQRC9hGKk7IAJTSq4ZNRTsieV/WOg5aROKFT5HZt9B+KIB caUoLnPSLJgaoPCrC4il6gc1axYxc2Gr1Hm0E+aH17QMcvKhCq5M7qMOw3yI0n6K2oHo vWf5ITXCqjn5Q9b7S6sPjgVttekfZHtKsXOZL4IJUpe05X9DBs0GbbTr9CHsS3hGAGSP 9A2w70lhOl4RfupuX7PTWK/zjWR2EcgYoNUaQ4RUD6I1iZSyHiPW+xK8To+VkKkFzpxV gG3ZsKgIPYiv1+WaV/J2VGhGHsO27QO05jQUjdkzvSOBOn/0XXJFgZM5D0XISpvNHD9i g8rQ== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id de20si162422edb.451.2020.04.29.09.09.36; Wed, 29 Apr 2020 09:10:06 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726669AbgD2QHZ (ORCPT + 99 others); Wed, 29 Apr 2020 12:07:25 -0400 Received: from mga09.intel.com ([134.134.136.24]:38373 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726476AbgD2QHZ (ORCPT ); Wed, 29 Apr 2020 12:07:25 -0400 IronPort-SDR: CsQmFUCTs+RZVg0GZUvPhEMlKVn/24YK+mIObKqHd6LMWXmgTiddNaCq8Ng/hjBaztSIqSscDv QebxlEOEOYBw== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Apr 2020 09:07:24 -0700 IronPort-SDR: Vgv+BBLXnG4azl63Xu3Gy0K31BLI7IcGhyr31F2JTnjgWQl8BW1HKCTTAzLUYlVMjsT2IBOUhB Kjsfdtpx6rOA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.73,332,1583222400"; d="scan'208";a="294217667" Received: from yyu32-desk.sc.intel.com ([143.183.136.146]) by orsmga008.jf.intel.com with ESMTP; 29 Apr 2020 09:07:23 -0700 From: Yu-cheng Yu To: Borislav Petkov , linux-kernel@vger.kernel.org, x86@kernel.org, "H. Peter Anvin" , Thomas Gleixner , Ingo Molnar , Dave Hansen , Tony Luck , Andy Lutomirski , Rik van Riel , "Ravi V. Shankar" , Sebastian Andrzej Siewior , Fenghua Yu , Peter Zijlstra Cc: Yu-cheng Yu Subject: [PATCH v3 05/10] x86/fpu/xstate: Define new functions for clearing fpregs and xstates Date: Wed, 29 Apr 2020 09:06:44 -0700 Message-Id: <20200429160644.28584-1-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20200328164307.17497-6-yu-cheng.yu@intel.com> References: <20200328164307.17497-6-yu-cheng.yu@intel.com> 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: Fenghua Yu Currently, fpu__clear() clears all fpregs and xstates. Once XSAVES supervisor states are introduced, supervisor settings (e.g. CET xstates) must remain active for signals; It is necessary to have separate functions: - Create fpu__clear_user_states(): clear only user settings for signals; - Create fpu__clear_all(): clear both user and supervisor settings in flush_thread(). Also modify copy_init_fpstate_to_fpregs() to take a mask from above two functions. Signed-off-by: Fenghua Yu Co-developed-by: Yu-cheng Yu Signed-off-by: Yu-cheng Yu Reviewed-by: Dave Hansen Reviewed-by: Tony Luck v3: - Put common code into a static function fpu__clear(), with a parameter user_only. v2: - Fixed an issue where fpu__clear_user_states() drops supervisor xstates. - Revise commit log. --- arch/x86/include/asm/fpu/internal.h | 3 +- arch/x86/kernel/fpu/core.c | 49 +++++++++++++++++++---------- arch/x86/kernel/fpu/signal.c | 4 +-- arch/x86/kernel/process.c | 2 +- arch/x86/kernel/signal.c | 2 +- 5 files changed, 39 insertions(+), 21 deletions(-) diff --git a/arch/x86/include/asm/fpu/internal.h b/arch/x86/include/asm/fpu/internal.h index ccb1bb32ad7d..a42fcb4b690d 100644 --- a/arch/x86/include/asm/fpu/internal.h +++ b/arch/x86/include/asm/fpu/internal.h @@ -31,7 +31,8 @@ extern void fpu__save(struct fpu *fpu); extern int fpu__restore_sig(void __user *buf, int ia32_frame); extern void fpu__drop(struct fpu *fpu); extern int fpu__copy(struct task_struct *dst, struct task_struct *src); -extern void fpu__clear(struct fpu *fpu); +extern void fpu__clear_user_states(struct fpu *fpu); +extern void fpu__clear_all(struct fpu *fpu); extern int fpu__exception_code(struct fpu *fpu, int trap_nr); extern int dump_fpu(struct pt_regs *ptregs, struct user_i387_struct *fpstate); diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c index 12c70840980e..7fddd5d60443 100644 --- a/arch/x86/kernel/fpu/core.c +++ b/arch/x86/kernel/fpu/core.c @@ -294,12 +294,10 @@ void fpu__drop(struct fpu *fpu) * Clear FPU registers by setting them up from * the init fpstate: */ -static inline void copy_init_fpstate_to_fpregs(void) +static inline void copy_init_fpstate_to_fpregs(u64 features_mask) { - fpregs_lock(); - if (use_xsave()) - copy_kernel_to_xregs(&init_fpstate.xsave, -1); + copy_kernel_to_xregs(&init_fpstate.xsave, features_mask); else if (static_cpu_has(X86_FEATURE_FXSR)) copy_kernel_to_fxregs(&init_fpstate.fxsave); else @@ -307,9 +305,6 @@ static inline void copy_init_fpstate_to_fpregs(void) if (boot_cpu_has(X86_FEATURE_OSPKE)) copy_init_pkru_to_fpregs(); - - fpregs_mark_activate(); - fpregs_unlock(); } /* @@ -318,18 +313,40 @@ static inline void copy_init_fpstate_to_fpregs(void) * Called by sys_execve(), by the signal handler code and by various * error paths. */ -void fpu__clear(struct fpu *fpu) +static void fpu__clear(struct fpu *fpu, int user_only) { - WARN_ON_FPU(fpu != ¤t->thread.fpu); /* Almost certainly an anomaly */ + WARN_ON_FPU(fpu != ¤t->thread.fpu); - fpu__drop(fpu); + if (!static_cpu_has(X86_FEATURE_FPU)) { + fpu__drop(fpu); + fpu__initialize(fpu); + return; + } - /* - * Make sure fpstate is cleared and initialized. - */ - fpu__initialize(fpu); - if (static_cpu_has(X86_FEATURE_FPU)) - copy_init_fpstate_to_fpregs(); + fpregs_lock(); + + if (user_only) { + if (!fpregs_state_valid(fpu, smp_processor_id()) && + xfeatures_mask_supervisor()) + copy_kernel_to_xregs(&fpu->state.xsave, + xfeatures_mask_supervisor()); + copy_init_fpstate_to_fpregs(xfeatures_mask_user()); + } else { + copy_init_fpstate_to_fpregs(xfeatures_mask_all); + } + + fpregs_mark_activate(); + fpregs_unlock(); +} + +void fpu__clear_user_states(struct fpu *fpu) +{ + fpu__clear(fpu, 1); +} + +void fpu__clear_all(struct fpu *fpu) +{ + fpu__clear(fpu, 0); } /* diff --git a/arch/x86/kernel/fpu/signal.c b/arch/x86/kernel/fpu/signal.c index 3df0cfae535f..cd6eafba12da 100644 --- a/arch/x86/kernel/fpu/signal.c +++ b/arch/x86/kernel/fpu/signal.c @@ -289,7 +289,7 @@ static int __fpu__restore_sig(void __user *buf, void __user *buf_fx, int size) IS_ENABLED(CONFIG_IA32_EMULATION)); if (!buf) { - fpu__clear(fpu); + fpu__clear_user_states(fpu); return 0; } @@ -416,7 +416,7 @@ static int __fpu__restore_sig(void __user *buf, void __user *buf_fx, int size) err_out: if (ret) - fpu__clear(fpu); + fpu__clear_user_states(fpu); return ret; } diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c index 9da70b279dad..de182b84723a 100644 --- a/arch/x86/kernel/process.c +++ b/arch/x86/kernel/process.c @@ -191,7 +191,7 @@ void flush_thread(void) flush_ptrace_hw_breakpoint(tsk); memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array)); - fpu__clear(&tsk->thread.fpu); + fpu__clear_all(&tsk->thread.fpu); } void disable_TSC(void) diff --git a/arch/x86/kernel/signal.c b/arch/x86/kernel/signal.c index 83b74fb38c8f..0052bbe5dfd4 100644 --- a/arch/x86/kernel/signal.c +++ b/arch/x86/kernel/signal.c @@ -732,7 +732,7 @@ handle_signal(struct ksignal *ksig, struct pt_regs *regs) /* * Ensure the signal handler starts with the new fpu state. */ - fpu__clear(fpu); + fpu__clear_user_states(fpu); } signal_setup_done(failed, ksig, stepping); } -- 2.21.0