LinuxKPI: Add explicit software context to FPU sections

Amdgpu driver does a lot of memory allocations in FPU-protected sections
of code for certain display cores, e.g. for DCN30. This does not work
currently on FreeBSD as its malloc function can not be run within a
critical section. Allocate memory for FPU context to overcome such
restriction.

Sponsored by:	Serenity Cyber Security, LLC
Reviewed by:	manu (previous version), markj
MFC after:	1 week
Differential Revision:	https://reviews.freebsd.org/D42822
This commit is contained in:
Vladimir Kondratyev 2023-12-24 11:20:01 +03:00
parent 04952a9456
commit 5a3bd28167
4 changed files with 36 additions and 4 deletions

View file

@ -33,6 +33,11 @@
#include <sys/proc.h>
#include <sys/malloc.h>
#if defined(__aarch64__) || defined(__arm__) || defined(__amd64__) || \
defined(__i386__) || defined(__powerpc64__)
#define LKPI_HAVE_FPU_CTX
#endif
struct domainset;
struct thread;
struct task_struct;
@ -40,6 +45,7 @@ struct task_struct;
extern int linux_alloc_current(struct thread *, int flags);
extern void linux_free_current(struct task_struct *);
extern struct domainset *linux_get_vm_domain_set(int node);
extern int linux_set_fpu_ctx(struct task_struct *);
static inline void
linux_set_current(struct thread *td)

View file

@ -64,6 +64,7 @@
#define TASK_COMM_LEN (MAXCOMLEN + 1)
struct seq_file;
struct fpu_kern_ctx;
struct work_struct;
struct task_struct {
@ -89,6 +90,7 @@ struct task_struct {
struct task_struct *group_leader;
unsigned rcu_section[TS_RCU_TYPE_MAX];
unsigned int fpu_ctx_level;
struct fpu_kern_ctx *fpu_ctx;
};
#define current ({ \

View file

@ -43,6 +43,10 @@
#include <sys/sysctl.h>
#include <vm/uma.h>
#ifdef LKPI_HAVE_FPU_CTX
#include <machine/fpu.h>
#endif
#ifdef DEV_APIC
extern u_int first_msi_irq, num_msi_irqs;
#endif
@ -153,6 +157,16 @@ linux_alloc_current(struct thread *td, int flags)
return (0);
}
int
linux_set_fpu_ctx(struct task_struct *task)
{
#ifdef LKPI_HAVE_FPU_CTX
if (task->fpu_ctx == NULL && curthread->td_critnest == 0)
task->fpu_ctx = fpu_kern_alloc_ctx(FPU_KERN_NOWAIT);
#endif
return (task->fpu_ctx != NULL ? 0 : ENOMEM);
}
struct mm_struct *
linux_get_task_mm(struct task_struct *task)
{
@ -176,6 +190,10 @@ void
linux_free_current(struct task_struct *ts)
{
mmput(ts->mm);
#ifdef LKPI_HAVE_FPU_CTX
if (ts->fpu_ctx != NULL)
fpu_kern_free_ctx(ts->fpu_ctx);
#endif
uma_zfree(linux_current_zone, ts);
}

View file

@ -30,11 +30,12 @@
#include <sys/proc.h>
#include <sys/kernel.h>
#include <linux/compat.h>
#include <linux/sched.h>
#include <asm/fpu/api.h>
#if defined(__aarch64__) || defined(__amd64__) || defined(__i386__)
#ifdef LKPI_HAVE_FPU_CTX
#include <machine/fpu.h>
@ -47,15 +48,20 @@
void
lkpi_kernel_fpu_begin(void)
{
if ((current->fpu_ctx_level)++ == 0)
fpu_kern_enter(curthread, NULL, FPU_KERN_NOCTX);
int err;
if ((current->fpu_ctx_level)++ == 0) {
err = linux_set_fpu_ctx(current);
fpu_kern_enter(curthread, current->fpu_ctx,
err == 0 ? FPU_KERN_KTHR : FPU_KERN_NOCTX);
}
}
void
lkpi_kernel_fpu_end(void)
{
if (--(current->fpu_ctx_level) == 0)
fpu_kern_leave(curthread, NULL);
fpu_kern_leave(curthread, current->fpu_ctx);
}
#else