Skip to content

Commit

Permalink
LinuxKPI: Make FPU sections thread-safe and use the NOCTX flag.
Browse files Browse the repository at this point in the history
Reviewed by:	kib
Submitted by:	[email protected]
Differential Revision:	https://reviews.freebsd.org/D29921
MFC after:	1 week
Sponsored by:	NVIDIA Networking
  • Loading branch information
hselasky committed Jul 31, 2021
1 parent f7f76c2 commit 469884c
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 56 deletions.
40 changes: 6 additions & 34 deletions sys/compat/linuxkpi/common/include/asm/fpu/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,41 +28,13 @@
#ifndef _FPU_API_H_
#define _FPU_API_H_

#if defined(__aarch64__) || defined(__amd64__) || defined(__i386__)
#define kernel_fpu_begin() \
lkpi_kernel_fpu_begin()

#include <machine/fpu.h>
#define kernel_fpu_end() \
lkpi_kernel_fpu_end()

extern struct fpu_kern_ctx *__lkpi_fpu_ctx;
extern unsigned int __lkpi_fpu_ctx_level;

static inline void
kernel_fpu_begin()
{
if (__lkpi_fpu_ctx_level++ == 0) {
fpu_kern_enter(curthread, __lkpi_fpu_ctx, FPU_KERN_NORMAL);
}
}

static inline void
kernel_fpu_end()
{
if (--__lkpi_fpu_ctx_level == 0) {
fpu_kern_leave(curthread, __lkpi_fpu_ctx);
}
}

#else

static inline void
kernel_fpu_begin()
{
}

static inline void
kernel_fpu_end()
{
}

#endif
extern void lkpi_kernel_fpu_begin(void);
extern void lkpi_kernel_fpu_end(void);

#endif /* _FPU_API_H_ */
3 changes: 2 additions & 1 deletion sys/compat/linuxkpi/common/include/linux/sched.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ struct task_struct {
int bsd_interrupt_value;
struct work_struct *work; /* current work struct, if set */
struct task_struct *group_leader;
unsigned rcu_section[TS_RCU_TYPE_MAX];
unsigned rcu_section[TS_RCU_TYPE_MAX];
unsigned int fpu_ctx_level;
};

#define current ({ \
Expand Down
43 changes: 33 additions & 10 deletions sys/compat/linuxkpi/common/src/linux_fpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,44 @@
#include <sys/proc.h>
#include <sys/kernel.h>

#include <linux/sched.h>

#include <asm/fpu/api.h>

#if defined(__aarch64__) || defined(__amd64__) || defined(__i386__)

#include <machine/fpu.h>

struct fpu_kern_ctx *__lkpi_fpu_ctx;
unsigned int __lkpi_fpu_ctx_level = 0;
/*
* Technically the Linux API isn't supposed to allow nesting sections
* either, but currently used versions of GPU drivers rely on nesting
* working, so we only enter the section on the outermost level.
*/

void
lkpi_kernel_fpu_begin(void)
{
if ((current->fpu_ctx_level)++ == 0)
fpu_kern_enter(curthread, NULL, FPU_KERN_NOCTX);
}

void
lkpi_kernel_fpu_end(void)
{
if (--(current->fpu_ctx_level) == 0)
fpu_kern_leave(curthread, NULL);
}

#else

static void
linux_fpu_init(void *arg __unused)
void
lkpi_kernel_fpu_begin(void)
{
__lkpi_fpu_ctx = fpu_kern_alloc_ctx(0);
}
SYSINIT(linux_fpu, SI_SUB_EVENTHANDLER, SI_ORDER_SECOND, linux_fpu_init, NULL);

static void
linux_fpu_uninit(void *arg __unused)
void
lkpi_kernel_fpu_end(void)
{
fpu_kern_free_ctx(__lkpi_fpu_ctx);
}
SYSUNINIT(linux_fpu, SI_SUB_EVENTHANDLER, SI_ORDER_SECOND, linux_fpu_uninit, NULL);

#endif
3 changes: 0 additions & 3 deletions sys/conf/files.amd64
Original file line number Diff line number Diff line change
Expand Up @@ -398,9 +398,6 @@ x86/x86/mptable_pci.c optional mptable pci
x86/x86/msi.c optional pci
x86/xen/pv.c optional xenhvm

compat/linuxkpi/common/src/linux_fpu.c optional compat_linuxkpi \
compile-with "${LINUXKPI_C}"

contrib/openzfs/module/zcommon/zfs_fletcher_avx512.c optional zfs compile-with "${ZFS_C}"
contrib/openzfs/module/zcommon/zfs_fletcher_intel.c optional zfs compile-with "${ZFS_C}"
contrib/openzfs/module/zcommon/zfs_fletcher_sse.c optional zfs compile-with "${ZFS_C}"
Expand Down
4 changes: 0 additions & 4 deletions sys/conf/files.arm64
Original file line number Diff line number Diff line change
Expand Up @@ -560,10 +560,6 @@ arm64/rockchip/clk/rk3399_pmucru.c optional fdt soc_rockchip_rk3399
# Xilinx
arm/xilinx/uart_dev_cdnc.c optional uart soc_xilinx_zynq

# Linuxkpi
compat/linuxkpi/common/src/linux_fpu.c optional compat_linuxkpi \
compile-with "${LINUXKPI_C}"

# Cloudabi
arm64/cloudabi32/cloudabi32_sysvec.c optional compat_cloudabi32
arm64/cloudabi64/cloudabi64_sysvec.c optional compat_cloudabi64
Expand Down
3 changes: 0 additions & 3 deletions sys/conf/files.i386
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,3 @@ x86/x86/local_apic.c optional apic
x86/x86/mptable.c optional apic
x86/x86/mptable_pci.c optional apic pci
x86/x86/msi.c optional apic pci

compat/linuxkpi/common/src/linux_fpu.c optional compat_linuxkpi \
compile-with "${LINUXKPI_C}"
3 changes: 2 additions & 1 deletion sys/modules/linuxkpi/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ SRCS= linux_compat.c \
linux_dmi.c \
linux_domain.c \
linux_firmware.c \
linux_fpu.c \
linux_hrtimer.c \
linux_idr.c \
linux_kmod.c \
Expand All @@ -29,7 +30,7 @@ SRCS= linux_compat.c \

.if ${MACHINE_CPUARCH} == "aarch64" || ${MACHINE_CPUARCH} == "amd64" || \
${MACHINE_CPUARCH} == "i386"
SRCS+= opt_acpi.h acpi_if.h linux_acpi.c linux_fpu.c
SRCS+= opt_acpi.h acpi_if.h linux_acpi.c
.endif

SRCS+= ${LINUXKPI_GENSRCS}
Expand Down

0 comments on commit 469884c

Please sign in to comment.