[PATCH 3/3] x86: avoid copying dynamic FP state from init_task

Benjamin Berg posted 3 patches 1 year ago
[PATCH 3/3] x86: avoid copying dynamic FP state from init_task
Posted by Benjamin Berg 1 year ago
From: Benjamin Berg <benjamin.berg@intel.com>

The init_task instance of struct task_struct is statically allocated and
may not contain the full FP state for userspace. As such, limit the copy
to the valid area of init_task and fill the rest with zero.

Note that the FP state is only needed for userspace, and as such it is
entirely reasonable for init_task to not contain parts of it.

Signed-off-by: Benjamin Berg <benjamin.berg@intel.com>
Fixes: 5aaeb5c01c5b ("x86/fpu, sched: Introduce CONFIG_ARCH_WANTS_DYNAMIC_TASK_STRUCT and use it on x86")
---
 arch/x86/kernel/process.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c
index f63f8fd00a91..1be45fe70cad 100644
--- a/arch/x86/kernel/process.c
+++ b/arch/x86/kernel/process.c
@@ -92,7 +92,15 @@ EXPORT_PER_CPU_SYMBOL_GPL(__tss_limit_invalid);
  */
 int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
 {
-	memcpy(dst, src, arch_task_struct_size);
+	/* init_task is not dynamically sized (incomplete FPU state) */
+	if (unlikely(src == &init_task)) {
+		memcpy(dst, src, sizeof(init_task));
+		memset((void *)dst + sizeof(init_task), 0,
+		       arch_task_struct_size - sizeof(init_task));
+	} else {
+		memcpy(dst, src, arch_task_struct_size);
+	}
+
 #ifdef CONFIG_VM86
 	dst->thread.vm86 = NULL;
 #endif
-- 
2.47.1
Re: [PATCH 3/3] x86: avoid copying dynamic FP state from init_task
Posted by Ingo Molnar 9 months, 3 weeks ago
* Benjamin Berg <benjamin@sipsolutions.net> wrote:

> From: Benjamin Berg <benjamin.berg@intel.com>
> 
> The init_task instance of struct task_struct is statically allocated and
> may not contain the full FP state for userspace. As such, limit the copy
> to the valid area of init_task and fill the rest with zero.
> 
> Note that the FP state is only needed for userspace, and as such it is
> entirely reasonable for init_task to not contain parts of it.
> 
> Signed-off-by: Benjamin Berg <benjamin.berg@intel.com>
> Fixes: 5aaeb5c01c5b ("x86/fpu, sched: Introduce CONFIG_ARCH_WANTS_DYNAMIC_TASK_STRUCT and use it on x86")
> ---
>  arch/x86/kernel/process.c | 10 +++++++++-
>  1 file changed, 9 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c
> index f63f8fd00a91..1be45fe70cad 100644
> --- a/arch/x86/kernel/process.c
> +++ b/arch/x86/kernel/process.c
> @@ -92,7 +92,15 @@ EXPORT_PER_CPU_SYMBOL_GPL(__tss_limit_invalid);
>   */
>  int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
>  {
> -	memcpy(dst, src, arch_task_struct_size);
> +	/* init_task is not dynamically sized (incomplete FPU state) */
> +	if (unlikely(src == &init_task)) {
> +		memcpy(dst, src, sizeof(init_task));
> +		memset((void *)dst + sizeof(init_task), 0,
> +		       arch_task_struct_size - sizeof(init_task));
> +	} else {
> +		memcpy(dst, src, arch_task_struct_size);

Note that this patch, while it still applies cleanly, crashes/hangs the 
x86-64 defconfig kernel bootup in the early boot phase in a KVM guest 
bootup.

Thanks,

	Ingo
Re: [PATCH 3/3] x86: avoid copying dynamic FP state from init_task
Posted by Benjamin Berg 9 months, 3 weeks ago
On Wed, 2025-02-26 at 14:08 +0100, Ingo Molnar wrote:
> 
> * Benjamin Berg <benjamin@sipsolutions.net> wrote:
> 
> > From: Benjamin Berg <benjamin.berg@intel.com>
> > 
> > The init_task instance of struct task_struct is statically allocated and
> > may not contain the full FP state for userspace. As such, limit the copy
> > to the valid area of init_task and fill the rest with zero.
> > 
> > Note that the FP state is only needed for userspace, and as such it is
> > entirely reasonable for init_task to not contain parts of it.
> > 
> > Signed-off-by: Benjamin Berg <benjamin.berg@intel.com>
> > Fixes: 5aaeb5c01c5b ("x86/fpu, sched: Introduce CONFIG_ARCH_WANTS_DYNAMIC_TASK_STRUCT and use it on x86")
> > ---
> >  arch/x86/kernel/process.c | 10 +++++++++-
> >  1 file changed, 9 insertions(+), 1 deletion(-)
> > 
> > diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c
> > index f63f8fd00a91..1be45fe70cad 100644
> > --- a/arch/x86/kernel/process.c
> > +++ b/arch/x86/kernel/process.c
> > @@ -92,7 +92,15 @@ EXPORT_PER_CPU_SYMBOL_GPL(__tss_limit_invalid);
> >   */
> >  int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
> >  {
> > -	memcpy(dst, src, arch_task_struct_size);
> > +	/* init_task is not dynamically sized (incomplete FPU state) */
> > +	if (unlikely(src == &init_task)) {
> > +		memcpy(dst, src, sizeof(init_task));
> > +		memset((void *)dst + sizeof(init_task), 0,
> > +		       arch_task_struct_size - sizeof(init_task));
> > +	} else {
> > +		memcpy(dst, src, arch_task_struct_size);
> 
> Note that this patch, while it still applies cleanly, crashes/hangs the 
> x86-64 defconfig kernel bootup in the early boot phase in a KVM guest
> bootup.

Oh, outch. It seems that arch_task_struct_size can actually become
smaller than sizeof(init_task) if the CPU does not have certain
features.

See fpu__init_task_struct_size, which does:

  int task_size = sizeof(struct task_struct);
  task_size -= sizeof(current->thread.fpu.__fpstate.regs);
  task_size += fpu_kernel_cfg.default_size;

I'll submit a new version of the patch and then also switch to use
memcpy_and_pad.

Benjamin
Re: [PATCH 3/3] x86: avoid copying dynamic FP state from init_task
Posted by Ingo Molnar 9 months, 3 weeks ago
* Benjamin Berg <benjamin@sipsolutions.net> wrote:

> > Note that this patch, while it still applies cleanly, crashes/hangs 
> > the x86-64 defconfig kernel bootup in the early boot phase in a KVM 
> > guest bootup.
> 
> Oh, outch. It seems that arch_task_struct_size can actually become 
> smaller than sizeof(init_task) if the CPU does not have certain 
> features.
> 
> See fpu__init_task_struct_size, which does:
> 
>   int task_size = sizeof(struct task_struct);
>   task_size -= sizeof(current->thread.fpu.__fpstate.regs);
>   task_size += fpu_kernel_cfg.default_size;
> 
> I'll submit a new version of the patch and then also switch to use
> memcpy_and_pad.

Thank you!

	Ingo