Re: [PATCH 3/3, v4] x86/fpu: Remove init_task FPU state dependencies, add debugging warning for PF_KTHREAD tasks

From: Ingo Molnar
Date: Thu Jun 06 2024 - 04:51:48 EST



* Ingo Molnar <mingo@xxxxxxxxxx> wrote:

> I changed the debug check to test for PF_KTHREAD, and to return NULL:
>
> +#ifdef CONFIG_X86_DEBUG_FPU
> +struct fpu *x86_task_fpu(struct task_struct *task)
> +{
> + if (WARN_ON_ONCE(task->flags & PF_KTHREAD))
> + return NULL;
> +
> + return (void *)task + sizeof(*task);
> +}
> +#endif
>
> ... and the NULL we return will likely crash & exit any kthreads attempting
> to use the FPU context area - which I think is preferable to returning
> invalid memory that may then be corrupted.
>
> Hopefully this remains a hypothethical concern. :-)
>
> Alternatively, this may be one of the very few cases where a BUG_ON() might
> be justified? This condition is not recoverable in any sane fashion IMO.

And promptly this triggered in live testing, because while kthreads do not
use the FPU context area, the current fpu__drop() code does call
x86_task_cpu() even for kthreads...

See the two new 4/3 and 5/3 patches in this thread I've sent that clean
this up:

[PATCH 4/3] x86/fpu: Push 'fpu' pointer calculation into the fpu__drop() call
[PATCH 5/3] x86/fpu: Make sure x86_task_fpu() doesn't get called for PF_KTHREAD tasks during exit

I'll also reorder the patches to apply these fpu__drop() changes before
adding the debug warning.

Thanks,

Ingo