From nobody Fri Oct 3 23:02:53 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9AE9B23D7C4 for ; Fri, 22 Aug 2025 15:38:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755877096; cv=none; b=uQCwUwqLIUdUXVPtm9Wh7o+mpcrs5QBMEdKvvf8TC/TxErpXYAokwii8TqBF/fTKZwdvB0qvnuQX3DrwurhzoD/MWl5AaJ2034NHglxiL5VzWjj/WZ2f56l515GgGUY8hBfkvsVkZxJ9mabqRA9Ur2Od7btbV63FnoTWVWMAY+Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755877096; c=relaxed/simple; bh=vtoLwSy+rSGK7Qrg8w4sry4YSsxubqnZXpbq0bBBQ3E=; h=Date:From:To:Cc:Subject:Message-ID:MIME-Version:Content-Type: Content-Disposition:In-Reply-To; b=ef5sNpXJNsKDwQT4U00t5Fj6vEIIgXSHurp9XNNVrlUUli5QU611lZX1tP5G+SNHuU1PTbHqb/Lm/rL3dut05XQZDVdbpZ2igxCyjdQ0Acl5dhSFNoewnjToYC82FzQS07N1whEsoUdWB/F9dTPsuIBbQBJeMmijrNs2sdwvco8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=JmazHfqA; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="JmazHfqA" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755877093; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to; bh=wcb+UP6uLJYVIufEAJyNrWK+nKSeXT9MKLtT9eMyJe4=; b=JmazHfqAVHOB5Tu3xmasw38lwkKIMMoKy3EyV8VNbYU12YYIfugZqB0rpLX5r/yPUfkZCY PYuJL72iGbRbn/fsbSNgiWO4R5zc1hWqnC2Mr6ZNBzgVX9BKETcp9ynNyZO+Ys0KgO2vLN zjeOqt+xUo++z1gSLJFuGTeDpIUyR/Y= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-583-Z-lD4Dc_NTq8iJsVqLjeZw-1; Fri, 22 Aug 2025 11:38:08 -0400 X-MC-Unique: Z-lD4Dc_NTq8iJsVqLjeZw-1 X-Mimecast-MFC-AGG-ID: Z-lD4Dc_NTq8iJsVqLjeZw_1755877086 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 743841800561; Fri, 22 Aug 2025 15:38:05 +0000 (UTC) Received: from dhcp-27-174.brq.redhat.com (unknown [10.45.225.227]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with SMTP id A0633180044F; Fri, 22 Aug 2025 15:38:00 +0000 (UTC) Received: by dhcp-27-174.brq.redhat.com (nbSMTP-1.00) for uid 1000 oleg@redhat.com; Fri, 22 Aug 2025 17:36:45 +0200 (CEST) Date: Fri, 22 Aug 2025 17:36:39 +0200 From: Oleg Nesterov To: Borislav Petkov , Dave Hansen , Deepak Gupta , "H. Peter Anvin" , Ingo Molnar , Mark Brown , Peter Zijlstra , Rick Edgecombe , Sohil Mehta , Thomas Gleixner Cc: linux-kernel@vger.kernel.org, x86@kernel.org Subject: [PATCH v2 1/5] x86/fpu: don't use x86_task_fpu() in copy_xstate_to_uabi_buf() Message-ID: <20250822153639.GA27139@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20250822153603.GA27103@redhat.com> User-Agent: Mutt/1.5.24 (2015-08-30) X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" No functional changes, preparation for the next patches. Change copy_xstate_to_uabi_buf() to take a "struct fpstate *" and "u32 pkru" instead of "struct task_struct *" to avoid x86_task_fpu(tsk). The callers already have "struct fpu *" and can pass fpu->fpstate directly. Signed-off-by: Oleg Nesterov --- arch/x86/kernel/fpu/regset.c | 10 ++++++---- arch/x86/kernel/fpu/xstate.c | 12 ++++++------ arch/x86/kernel/fpu/xstate.h | 4 ++-- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/arch/x86/kernel/fpu/regset.c b/arch/x86/kernel/fpu/regset.c index 0986c2200adc..d280d415b171 100644 --- a/arch/x86/kernel/fpu/regset.c +++ b/arch/x86/kernel/fpu/regset.c @@ -83,7 +83,7 @@ int xfpregs_get(struct task_struct *target, const struct = user_regset *regset, sizeof(fpu->fpstate->regs.fxsave)); } =20 - copy_xstate_to_uabi_buf(to, target, XSTATE_COPY_FX); + copy_xstate_to_uabi_buf(to, fpu->fpstate, target->thread.pkru, XSTATE_COP= Y_FX); return 0; } =20 @@ -130,12 +130,14 @@ int xfpregs_set(struct task_struct *target, const str= uct user_regset *regset, int xstateregs_get(struct task_struct *target, const struct user_regset *r= egset, struct membuf to) { + struct fpu *fpu =3D x86_task_fpu(target); + if (!cpu_feature_enabled(X86_FEATURE_XSAVE)) return -ENODEV; =20 - sync_fpstate(x86_task_fpu(target)); + sync_fpstate(fpu); =20 - copy_xstate_to_uabi_buf(to, target, XSTATE_COPY_XSAVE); + copy_xstate_to_uabi_buf(to, fpu->fpstate, target->thread.pkru, XSTATE_COP= Y_XSAVE); return 0; } =20 @@ -419,7 +421,7 @@ int fpregs_get(struct task_struct *target, const struct= user_regset *regset, struct membuf mb =3D { .p =3D &fxsave, .left =3D sizeof(fxsave) }; =20 /* Handle init state optimized xstate correctly */ - copy_xstate_to_uabi_buf(mb, target, XSTATE_COPY_FP); + copy_xstate_to_uabi_buf(mb, fpu->fpstate, target->thread.pkru, XSTATE_CO= PY_FP); fx =3D &fxsave; } else { fx =3D &fpu->fpstate->regs.fxsave; diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c index 12ed75c1b567..2bd5974d5f0e 100644 --- a/arch/x86/kernel/fpu/xstate.c +++ b/arch/x86/kernel/fpu/xstate.c @@ -1256,7 +1256,8 @@ void __copy_xstate_to_uabi_buf(struct membuf to, stru= ct fpstate *fpstate, /** * copy_xstate_to_uabi_buf - Copy kernel saved xstate to a UABI buffer * @to: membuf descriptor - * @tsk: The task from which to copy the saved xstate + * @fpstate: The fpstate buffer from which to copy + * @pkru_val: The PKRU value to store in the PKRU component * @copy_mode: The requested copy mode * * Converts from kernel XSAVE or XSAVES compacted format to UABI conforming @@ -1265,12 +1266,11 @@ void __copy_xstate_to_uabi_buf(struct membuf to, st= ruct fpstate *fpstate, * * It supports partial copy but @to.pos always starts from zero. */ -void copy_xstate_to_uabi_buf(struct membuf to, struct task_struct *tsk, - enum xstate_copy_mode copy_mode) +void copy_xstate_to_uabi_buf(struct membuf to, struct fpstate *fpstate, + u32 pkru_val, enum xstate_copy_mode copy_mode) { - __copy_xstate_to_uabi_buf(to, x86_task_fpu(tsk)->fpstate, - x86_task_fpu(tsk)->fpstate->user_xfeatures, - tsk->thread.pkru, copy_mode); + __copy_xstate_to_uabi_buf(to, fpstate, fpstate->user_xfeatures, + pkru_val, copy_mode); } =20 static int copy_from_buffer(void *dst, unsigned int offset, unsigned int s= ize, diff --git a/arch/x86/kernel/fpu/xstate.h b/arch/x86/kernel/fpu/xstate.h index 52ce19289989..9d76ded84cdd 100644 --- a/arch/x86/kernel/fpu/xstate.h +++ b/arch/x86/kernel/fpu/xstate.h @@ -46,8 +46,8 @@ struct membuf; extern void __copy_xstate_to_uabi_buf(struct membuf to, struct fpstate *fp= state, u64 xfeatures, u32 pkru_val, enum xstate_copy_mode copy_mode); -extern void copy_xstate_to_uabi_buf(struct membuf to, struct task_struct *= tsk, - enum xstate_copy_mode mode); +extern void copy_xstate_to_uabi_buf(struct membuf to, struct fpstate *fpst= ate, + u32 pkru_val, enum xstate_copy_mode copy_mode); extern int copy_uabi_from_kernel_to_xstate(struct fpstate *fpstate, const = void *kbuf, u32 *pkru); extern int copy_sigframe_from_user_to_xstate(struct task_struct *tsk, cons= t void __user *ubuf); =20 --=20 2.25.1.362.g51ebf55 From nobody Fri Oct 3 23:02:53 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D15602EDD6B for ; Fri, 22 Aug 2025 15:38:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755877098; cv=none; b=aupkumTyWXovjDsKEjiz6T1YMOk39Fo/hiqZ+lqD5Xq00I9bYkK0sayA8p0sSAUV1rk2q2vSTolaB0tSikKKA+ecVM4DC4nm/06/X0i+pbKQYF1VWsaMAYWaJFg0bZ46C4Bi7ZD9bhgzrZ2pjlB/1WeZr8OgfQM4/Yavk4u4Rns= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755877098; c=relaxed/simple; bh=3rig9o0mKKdZLmjoLPG8hkOvBttTRwc/0hPS8s3NewQ=; h=Date:From:To:Cc:Subject:Message-ID:MIME-Version:Content-Type: Content-Disposition:In-Reply-To; b=dDuBj7FO0VapJIN0HzJOarK367LZjhEzxmVBwi+KCys+N+olyGbW7Cixi3Br3Di2Eh79kzBdC8xj0vEXDPlbQhdR4mpMp4mAIatTWZFmzhXR/jsSAPgEMYpnJMO4rPUTbmlfHlaO5Wz68jW/B4yEmgZ5TiTPlFmhYgtCp+PpOTw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=W4GNHCws; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="W4GNHCws" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755877095; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to; bh=oIM+QpfetU8Xpz9SmqBoW9/2mND86Ux35Wm3WP2cxcY=; b=W4GNHCwsyP29meEga4SFlUZ3IuzlSJ7yxum2FJU/QmiyhFzpwwbgp02MoTKIF6RiljPbVJ y/T6W+LQSkMD0xx6S1+xevUnuAvnfrUmTWmI2fldKm6IGdmuICsVqBJoHT7uioD8Pe1bdX gobgxcHOhnhjWvNtDjdg+xp+fj/WG7Q= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-94-fiNxVpoROtmIBoGq9PVRqQ-1; Fri, 22 Aug 2025 11:38:14 -0400 X-MC-Unique: fiNxVpoROtmIBoGq9PVRqQ-1 X-Mimecast-MFC-AGG-ID: fiNxVpoROtmIBoGq9PVRqQ_1755877092 Received: from mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 2143C19560B5; Fri, 22 Aug 2025 15:38:12 +0000 (UTC) Received: from dhcp-27-174.brq.redhat.com (unknown [10.45.225.227]) by mx-prod-int-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with SMTP id ACAD41800447; Fri, 22 Aug 2025 15:38:07 +0000 (UTC) Received: by dhcp-27-174.brq.redhat.com (nbSMTP-1.00) for uid 1000 oleg@redhat.com; Fri, 22 Aug 2025 17:36:52 +0200 (CEST) Date: Fri, 22 Aug 2025 17:36:46 +0200 From: Oleg Nesterov To: Borislav Petkov , Dave Hansen , Deepak Gupta , "H. Peter Anvin" , Ingo Molnar , Mark Brown , Peter Zijlstra , Rick Edgecombe , Sohil Mehta , Thomas Gleixner Cc: linux-kernel@vger.kernel.org, x86@kernel.org Subject: [PATCH v2 2/5] x86/fpu: regset: introduce get_fpstate() helper Message-ID: <20250822153646.GA27144@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20250822153603.GA27103@redhat.com> User-Agent: Mutt/1.5.24 (2015-08-30) X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.93 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" After the previous change the regset get() functions do not really need "struct fpu *", they can use "struct fpstate *" returned by the new helper which also does sync_fpstate(). Signed-off-by: Oleg Nesterov --- arch/x86/kernel/fpu/regset.c | 42 ++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/arch/x86/kernel/fpu/regset.c b/arch/x86/kernel/fpu/regset.c index d280d415b171..f5a803774e1c 100644 --- a/arch/x86/kernel/fpu/regset.c +++ b/arch/x86/kernel/fpu/regset.c @@ -49,6 +49,13 @@ static void sync_fpstate(struct fpu *fpu) fpu_sync_fpstate(fpu); } =20 +static struct fpstate *get_fpstate(struct task_struct *task) +{ + struct fpu *fpu =3D x86_task_fpu(task); + sync_fpstate(fpu); + return fpu->fpstate; +} + /* * Invalidate cached FPU registers before modifying the stopped target * task's fpstate. @@ -71,19 +78,19 @@ static void fpu_force_restore(struct fpu *fpu) int xfpregs_get(struct task_struct *target, const struct user_regset *regs= et, struct membuf to) { - struct fpu *fpu =3D x86_task_fpu(target); + struct fpstate *fpstate; =20 if (!cpu_feature_enabled(X86_FEATURE_FXSR)) return -ENODEV; =20 - sync_fpstate(fpu); + fpstate =3D get_fpstate(target); =20 if (!use_xsave()) { - return membuf_write(&to, &fpu->fpstate->regs.fxsave, - sizeof(fpu->fpstate->regs.fxsave)); + return membuf_write(&to, &fpstate->regs.fxsave, + sizeof(fpstate->regs.fxsave)); } =20 - copy_xstate_to_uabi_buf(to, fpu->fpstate, target->thread.pkru, XSTATE_COP= Y_FX); + copy_xstate_to_uabi_buf(to, fpstate, target->thread.pkru, XSTATE_COPY_FX); return 0; } =20 @@ -130,14 +137,13 @@ int xfpregs_set(struct task_struct *target, const str= uct user_regset *regset, int xstateregs_get(struct task_struct *target, const struct user_regset *r= egset, struct membuf to) { - struct fpu *fpu =3D x86_task_fpu(target); + struct fpstate *fpstate; =20 if (!cpu_feature_enabled(X86_FEATURE_XSAVE)) return -ENODEV; =20 - sync_fpstate(fpu); - - copy_xstate_to_uabi_buf(to, fpu->fpstate, target->thread.pkru, XSTATE_COP= Y_XSAVE); + fpstate =3D get_fpstate(target); + copy_xstate_to_uabi_buf(to, fpstate, target->thread.pkru, XSTATE_COPY_XSA= VE); return 0; } =20 @@ -189,15 +195,15 @@ int ssp_active(struct task_struct *target, const stru= ct user_regset *regset) int ssp_get(struct task_struct *target, const struct user_regset *regset, struct membuf to) { - struct fpu *fpu =3D x86_task_fpu(target); + struct fpstate *fpstate; struct cet_user_state *cetregs; =20 if (!cpu_feature_enabled(X86_FEATURE_USER_SHSTK) || !ssp_active(target, regset)) return -ENODEV; =20 - sync_fpstate(fpu); - cetregs =3D get_xsave_addr(&fpu->fpstate->regs.xsave, XFEATURE_CET_USER); + fpstate =3D get_fpstate(target); + cetregs =3D get_xsave_addr(&fpstate->regs.xsave, XFEATURE_CET_USER); if (WARN_ON(!cetregs)) { /* * This shouldn't ever be NULL because shadow stack was @@ -403,17 +409,17 @@ void convert_to_fxsr(struct fxregs_state *fxsave, int fpregs_get(struct task_struct *target, const struct user_regset *regse= t, struct membuf to) { - struct fpu *fpu =3D x86_task_fpu(target); + struct fpstate *fpstate; struct user_i387_ia32_struct env; struct fxregs_state fxsave, *fx; =20 - sync_fpstate(fpu); - if (!cpu_feature_enabled(X86_FEATURE_FPU)) return fpregs_soft_get(target, regset, to); =20 + fpstate =3D get_fpstate(target); + if (!cpu_feature_enabled(X86_FEATURE_FXSR)) { - return membuf_write(&to, &fpu->fpstate->regs.fsave, + return membuf_write(&to, &fpstate->regs.fsave, sizeof(struct fregs_state)); } =20 @@ -421,10 +427,10 @@ int fpregs_get(struct task_struct *target, const stru= ct user_regset *regset, struct membuf mb =3D { .p =3D &fxsave, .left =3D sizeof(fxsave) }; =20 /* Handle init state optimized xstate correctly */ - copy_xstate_to_uabi_buf(mb, fpu->fpstate, target->thread.pkru, XSTATE_CO= PY_FP); + copy_xstate_to_uabi_buf(mb, fpstate, target->thread.pkru, XSTATE_COPY_FP= ); fx =3D &fxsave; } else { - fx =3D &fpu->fpstate->regs.fxsave; + fx =3D &fpstate->regs.fxsave; } =20 __convert_from_fxsr(&env, target, fx); --=20 2.25.1.362.g51ebf55 From nobody Fri Oct 3 23:02:53 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 28BF225BEFD for ; Fri, 22 Aug 2025 15:38:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755877111; cv=none; b=NwI2W3X2PCLxoZBa5CNieOnwW9xkzYslicBLu53N+HdlIv3EB0ejws4YL2uPcV+E15jm4rGfstyzzXdzjbRVY4lMNypRX49HN2cUxydSlll/JXC87fm4KZfuHyHfGVntQNZXCEN68ShrO9OH2dx+D95LDttjO2Dgr68qmzk+NvA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755877111; c=relaxed/simple; bh=cRBDZ0V4M7USIfEXDXK1MhDoXCarJ9SLFjiHd+H031I=; h=Date:From:To:Cc:Subject:Message-ID:MIME-Version:Content-Type: Content-Disposition:In-Reply-To; b=iBihwSO6KTyrFe2/jeydMeAPQ+K4iSB/n5D/KcWxYrXxq9KyUbZheVjsfGmHbRvdQNR5kw2UDP8a0prACmQFsDox1kCeh9mTu99MZDY46FWsYmsa5yQiPZk0zx4SG2eM5FIqaC20LNFrLwl/PRDjl0eBWTRJZWDEfdF6QhLQdq0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=OQ5fMd7Q; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="OQ5fMd7Q" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755877109; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to; bh=I9OqGrWe4bFx0j7k4ZODRtHJTXRM3rkKeIqs4AxHQOM=; b=OQ5fMd7QY6tNI5q9b4taC8I+8gzHjjb6FLsqNx8KsmDmkeSDcizi6fqiYxM4d4Bd5UbU1W swKRSmDuqambpWqpliWF27Uejgj5kCHI8WNhTQmYINl7AWEznQA9RaQQt4Q9BR3FoZX12d f4i1gF/ECICl8aCxhvCR0FOglHnSQ+8= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-542-nSGTsydMNwuQi9co0eihyw-1; Fri, 22 Aug 2025 11:38:23 -0400 X-MC-Unique: nSGTsydMNwuQi9co0eihyw-1 X-Mimecast-MFC-AGG-ID: nSGTsydMNwuQi9co0eihyw_1755877101 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 58BD1180048E; Fri, 22 Aug 2025 15:38:21 +0000 (UTC) Received: from dhcp-27-174.brq.redhat.com (unknown [10.45.225.227]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with SMTP id 905A51800296; Fri, 22 Aug 2025 15:38:15 +0000 (UTC) Received: by dhcp-27-174.brq.redhat.com (nbSMTP-1.00) for uid 1000 oleg@redhat.com; Fri, 22 Aug 2025 17:37:01 +0200 (CEST) Date: Fri, 22 Aug 2025 17:36:53 +0200 From: Oleg Nesterov To: Borislav Petkov , Dave Hansen , Deepak Gupta , "H. Peter Anvin" , Ingo Molnar , Mark Brown , Peter Zijlstra , Rick Edgecombe , Sohil Mehta , Thomas Gleixner Cc: linux-kernel@vger.kernel.org, x86@kernel.org Subject: [PATCH v2 3/5] x86/fpu: fold sync_fpstate() into get_fpstate() Message-ID: <20250822153653.GA27150@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20250822153603.GA27103@redhat.com> User-Agent: Mutt/1.5.24 (2015-08-30) X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" After the previous change sync_fpstate() has no other callers. Signed-off-by: Oleg Nesterov --- arch/x86/kernel/fpu/regset.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/arch/x86/kernel/fpu/regset.c b/arch/x86/kernel/fpu/regset.c index f5a803774e1c..ecbabdc15ec1 100644 --- a/arch/x86/kernel/fpu/regset.c +++ b/arch/x86/kernel/fpu/regset.c @@ -43,16 +43,12 @@ int regset_xregset_fpregs_active(struct task_struct *ta= rget, const struct user_r * - ptrace to dump fpstate of a stopped task, in which case the registe= rs * have already been saved to fpstate on context switch. */ -static void sync_fpstate(struct fpu *fpu) -{ - if (fpu =3D=3D x86_task_fpu(current)) - fpu_sync_fpstate(fpu); -} - static struct fpstate *get_fpstate(struct task_struct *task) { struct fpu *fpu =3D x86_task_fpu(task); - sync_fpstate(fpu); + + if (task =3D=3D current) + fpu_sync_fpstate(fpu); return fpu->fpstate; } =20 --=20 2.25.1.362.g51ebf55 From nobody Fri Oct 3 23:02:53 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 01B8D25C833 for ; Fri, 22 Aug 2025 15:38:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755877116; cv=none; b=YoJdPSo3YPeRvMUlGX2Z5X0EJYrrfQvzu1/+YNoiF2/MRtuQ82XPOcEXiFKHZb4XUHdzwQVjyXcs7f4lKYPgPwjjqV/jmeCYoulEn0s2Ne8hCqMoOd6/XqD5fDeM4CeFxTW5OhtSliE/rxHn7M6/EZYn/d6BQ09tRTCya+9nbzs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755877116; c=relaxed/simple; bh=IApp4LzrpZfZpq4FOoG4uXSMm/Rh7qJX8SvuMXYVIdo=; h=Date:From:To:Cc:Subject:Message-ID:MIME-Version:Content-Type: Content-Disposition:In-Reply-To; b=tLtZprVlJbnLkmb5j6mL9KKqYGJnsuSflWgqU7gUXBLw2KucSGryG1VBi2JtuwKnsMJrH23ZQj2sE6u5SeA5hr3ffNpHo+H+AbvxeSlmeCBLKAr/7/rVCZr2E0B3hN6amJ1ZatepHDTF9FE+/ZZ2fanhuFwc2Nj2NsmYTnR1ifE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=SUiAyH0I; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="SUiAyH0I" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755877113; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to; bh=iwwQoFvPri9HBUT5vlx/GnO2PxJgU97nR07EdM103u0=; b=SUiAyH0Ie1htWS1NhZFV+W8vz6Q3H5zDxR7mivEOsM3cBo5LzAf/4ybo6wsws03XKcTl+0 Se/8hfAMPJjr5noUkhsvGD+/iQx84/+i1LgIopTj1TqaJZnudafTPXs6mXuHqaTI5wxi7v 9WwJq2TqV8ExT2hgdWLdEsg7y/HDafA= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-674-pk2HpOYUNqapQm2ot-_90g-1; Fri, 22 Aug 2025 11:38:30 -0400 X-MC-Unique: pk2HpOYUNqapQm2ot-_90g-1 X-Mimecast-MFC-AGG-ID: pk2HpOYUNqapQm2ot-_90g_1755877108 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 029761800366; Fri, 22 Aug 2025 15:38:28 +0000 (UTC) Received: from dhcp-27-174.brq.redhat.com (unknown [10.45.225.227]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with SMTP id 8D9221955F24; Fri, 22 Aug 2025 15:38:23 +0000 (UTC) Received: by dhcp-27-174.brq.redhat.com (nbSMTP-1.00) for uid 1000 oleg@redhat.com; Fri, 22 Aug 2025 17:37:08 +0200 (CEST) Date: Fri, 22 Aug 2025 17:37:02 +0200 From: Oleg Nesterov To: Borislav Petkov , Dave Hansen , Deepak Gupta , "H. Peter Anvin" , Ingo Molnar , Mark Brown , Peter Zijlstra , Rick Edgecombe , Sohil Mehta , Thomas Gleixner Cc: linux-kernel@vger.kernel.org, x86@kernel.org Subject: [PATCH v2 4/5] x86/shstk: don't create the shadow stack for PF_USER_WORKERs Message-ID: <20250822153702.GA27153@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20250822153603.GA27103@redhat.com> User-Agent: Mutt/1.5.24 (2015-08-30) X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" If a features_enabled(ARCH_SHSTK_SHSTK) userspace thread creates a PF_USER_WORKER thread, shstk_alloc_thread_stack() allocates the shadow stack for no reason, the new (kernel) thread will never return to usermode. Plus the current code doesn't even look correct, in this case fpu_clone() won't call update_fpu_shstk(). Add the new "bool minimal =3D !!args->fn" argument (which matches that of fpu_clone()) to shstk_alloc_thread_stack() and change it to check this argument along with CLONE_VFORK. With this patch ssp_get() -> ssp_active(target) should never return true if target->flags & PF_USER_WORKER. Signed-off-by: Oleg Nesterov --- arch/x86/include/asm/shstk.h | 4 ++-- arch/x86/kernel/process.c | 2 +- arch/x86/kernel/shstk.c | 9 +++++++-- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/arch/x86/include/asm/shstk.h b/arch/x86/include/asm/shstk.h index ba6f2fe43848..a4ee2baab51c 100644 --- a/arch/x86/include/asm/shstk.h +++ b/arch/x86/include/asm/shstk.h @@ -17,7 +17,7 @@ struct thread_shstk { long shstk_prctl(struct task_struct *task, int option, unsigned long arg2); void reset_thread_features(void); unsigned long shstk_alloc_thread_stack(struct task_struct *p, unsigned lon= g clone_flags, - unsigned long stack_size); + bool minimal, unsigned long stack_size); void shstk_free(struct task_struct *p); int setup_signal_shadow_stack(struct ksignal *ksig); int restore_signal_shadow_stack(void); @@ -28,7 +28,7 @@ static inline long shstk_prctl(struct task_struct *task, = int option, unsigned long arg2) { return -EINVAL; } static inline void reset_thread_features(void) {} static inline unsigned long shstk_alloc_thread_stack(struct task_struct *p, - unsigned long clone_flags, + unsigned long clone_flags, bool minimal, unsigned long stack_size) { return 0; } static inline void shstk_free(struct task_struct *p) {} static inline int setup_signal_shadow_stack(struct ksignal *ksig) { return= 0; } diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c index 1b7960cf6eb0..e932e0e53972 100644 --- a/arch/x86/kernel/process.c +++ b/arch/x86/kernel/process.c @@ -209,7 +209,7 @@ int copy_thread(struct task_struct *p, const struct ker= nel_clone_args *args) * is disabled, new_ssp will remain 0, and fpu_clone() will know not to * update it. */ - new_ssp =3D shstk_alloc_thread_stack(p, clone_flags, args->stack_size); + new_ssp =3D shstk_alloc_thread_stack(p, clone_flags, args->fn, args->stac= k_size); if (IS_ERR_VALUE(new_ssp)) return PTR_ERR((void *)new_ssp); =20 diff --git a/arch/x86/kernel/shstk.c b/arch/x86/kernel/shstk.c index 2ddf23387c7e..6c8c4593e202 100644 --- a/arch/x86/kernel/shstk.c +++ b/arch/x86/kernel/shstk.c @@ -192,7 +192,7 @@ void reset_thread_features(void) } =20 unsigned long shstk_alloc_thread_stack(struct task_struct *tsk, unsigned l= ong clone_flags, - unsigned long stack_size) + bool minimal, unsigned long stack_size) { struct thread_shstk *shstk =3D &tsk->thread.shstk; unsigned long addr, size; @@ -208,8 +208,13 @@ unsigned long shstk_alloc_thread_stack(struct task_str= uct *tsk, unsigned long cl * For CLONE_VFORK the child will share the parents shadow stack. * Make sure to clear the internal tracking of the thread shadow * stack so the freeing logic run for child knows to leave it alone. + * + * If minimal =3D=3D true, the new kernel thread cloned from userspace + * thread will never return to usermode. */ - if (clone_flags & CLONE_VFORK) { + if ((clone_flags & CLONE_VFORK) || minimal) { + if (minimal) + tsk->thread.features &=3D ~ARCH_SHSTK_SHSTK; shstk->base =3D 0; shstk->size =3D 0; return 0; --=20 2.25.1.362.g51ebf55 From nobody Fri Oct 3 23:02:53 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6C8342FFDEB for ; Fri, 22 Aug 2025 15:38:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755877125; cv=none; b=RDd2HpkbXguI2OH//lz0HQxqD4FqhBA1ohAZEClROO08lFXQgvhaZsj1B4D/yhHQYbYjiof3SEgwooKFuYNr8ruimz/JLIpiy/jFaRDnlpk5zAONSKzH1tgXKvpVZW+SQakNlZPFStFURDrRFlpfTNrrHS00wARyZt1c4N+qy9E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755877125; c=relaxed/simple; bh=Eh+zMIqztYIY2ggEJbUI5AiR3H04ggRlpFk382BjQDA=; h=Date:From:To:Cc:Subject:Message-ID:MIME-Version:Content-Type: Content-Disposition:In-Reply-To; b=MRJbs+q/pVd25mgNdN4y3vSzR+Gu03u08sfG44B5LUiWcQd40XY9tEABMBHJgyIKtDPFaOWtpNJW6i81wSGaKLYcLeQQCGUtvZ/67GS51J46hhCW9oCuR/LCArKQKaJx31m5xnxdw+GOAIhUCb5UQEPoZV3j6wG2pi1lAolB5/o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=ft6uXoDS; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="ft6uXoDS" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755877122; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to; bh=sNdAklJ4NfQziNX1nrwBgXGN4LI63ON8uM/hjWZghWw=; b=ft6uXoDSkU1Jg5XTSSe6ykF97dzHUWeWv10PAG9HqhmGrvb5szS9Gt0FIH5Gnoh0X6cnEu 5Vuktapl/lyMJozw9wrUihx7arXiKYpbpkOs4zJkxPjHcC4nogvEr98VdwNeTaU5XNRKMd ShYiEs61Fg0ipTWkiCNNg91pHI8KN9c= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-482-Za_cNxj-OFW4IqN5RCn6hg-1; Fri, 22 Aug 2025 11:38:36 -0400 X-MC-Unique: Za_cNxj-OFW4IqN5RCn6hg-1 X-Mimecast-MFC-AGG-ID: Za_cNxj-OFW4IqN5RCn6hg_1755877115 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id D47001800285; Fri, 22 Aug 2025 15:38:34 +0000 (UTC) Received: from dhcp-27-174.brq.redhat.com (unknown [10.45.225.227]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with SMTP id 8068519560B0; Fri, 22 Aug 2025 15:38:29 +0000 (UTC) Received: by dhcp-27-174.brq.redhat.com (nbSMTP-1.00) for uid 1000 oleg@redhat.com; Fri, 22 Aug 2025 17:37:14 +0200 (CEST) Date: Fri, 22 Aug 2025 17:37:09 +0200 From: Oleg Nesterov To: Borislav Petkov , Dave Hansen , Deepak Gupta , "H. Peter Anvin" , Ingo Molnar , Mark Brown , Peter Zijlstra , Rick Edgecombe , Sohil Mehta , Thomas Gleixner Cc: linux-kernel@vger.kernel.org, x86@kernel.org Subject: [PATCH v2 5/5] x86/fpu: change get_fpstate() to return &init_fpstate if PF_USER_WORKER Message-ID: <20250822153709.GA27159@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20250822153603.GA27103@redhat.com> User-Agent: Mutt/1.5.24 (2015-08-30) X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" PF_USER_WORKERs must never use FPU, this is what kernel_fpu_begin/etc assume. The .regset_get() functions can safely use init_fpstate if target->flags & PF_USER_WORKER. Signed-off-by: Oleg Nesterov --- arch/x86/kernel/fpu/regset.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/arch/x86/kernel/fpu/regset.c b/arch/x86/kernel/fpu/regset.c index ecbabdc15ec1..dfd12d109f00 100644 --- a/arch/x86/kernel/fpu/regset.c +++ b/arch/x86/kernel/fpu/regset.c @@ -45,8 +45,12 @@ int regset_xregset_fpregs_active(struct task_struct *tar= get, const struct user_r */ static struct fpstate *get_fpstate(struct task_struct *task) { - struct fpu *fpu =3D x86_task_fpu(task); + struct fpu *fpu; =20 + if (unlikely(task->flags & PF_USER_WORKER)) + return &init_fpstate; + + fpu =3D x86_task_fpu(task); if (task =3D=3D current) fpu_sync_fpstate(fpu); return fpu->fpstate; --=20 2.25.1.362.g51ebf55