git: 5a3bd281672b - main - LinuxKPI: Add explicit software context to FPU sections
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Sun, 24 Dec 2023 08:23:24 UTC
The branch main has been updated by wulf:
URL: https://cgit.FreeBSD.org/src/commit/?id=5a3bd281672bd6bd8e18081c3928dfe76d330a5f
commit 5a3bd281672bd6bd8e18081c3928dfe76d330a5f
Author: Vladimir Kondratyev <wulf@FreeBSD.org>
AuthorDate: 2023-12-24 08:20:01 +0000
Commit: Vladimir Kondratyev <wulf@FreeBSD.org>
CommitDate: 2023-12-24 08:20:01 +0000
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
---
sys/compat/linuxkpi/common/include/linux/compat.h | 6 ++++++
sys/compat/linuxkpi/common/include/linux/sched.h | 2 ++
sys/compat/linuxkpi/common/src/linux_current.c | 18 ++++++++++++++++++
sys/compat/linuxkpi/common/src/linux_fpu.c | 14 ++++++++++----
4 files changed, 36 insertions(+), 4 deletions(-)
diff --git a/sys/compat/linuxkpi/common/include/linux/compat.h b/sys/compat/linuxkpi/common/include/linux/compat.h
index d1a02f612f42..7922e884beb1 100644
--- a/sys/compat/linuxkpi/common/include/linux/compat.h
+++ b/sys/compat/linuxkpi/common/include/linux/compat.h
@@ -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)
diff --git a/sys/compat/linuxkpi/common/include/linux/sched.h b/sys/compat/linuxkpi/common/include/linux/sched.h
index 8cb6b12100d5..77c4f6d3f80f 100644
--- a/sys/compat/linuxkpi/common/include/linux/sched.h
+++ b/sys/compat/linuxkpi/common/include/linux/sched.h
@@ -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 ({ \
diff --git a/sys/compat/linuxkpi/common/src/linux_current.c b/sys/compat/linuxkpi/common/src/linux_current.c
index bec53384361f..668185094cbe 100644
--- a/sys/compat/linuxkpi/common/src/linux_current.c
+++ b/sys/compat/linuxkpi/common/src/linux_current.c
@@ -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);
}
diff --git a/sys/compat/linuxkpi/common/src/linux_fpu.c b/sys/compat/linuxkpi/common/src/linux_fpu.c
index b26dce98774b..d510bc208b88 100644
--- a/sys/compat/linuxkpi/common/src/linux_fpu.c
+++ b/sys/compat/linuxkpi/common/src/linux_fpu.c
@@ -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