Skip to content

Commit c760b37

Browse files
committed
Merge tag 'mm-nonmm-stable-2024-05-22-17-30' of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Pull more non-mm updates from Andrew Morton: - A series ("kbuild: enable more warnings by default") from Arnd Bergmann which enables a number of additional build-time warnings. We fixed all the fallout which we could find, there may still be a few stragglers. - Samuel Holland has developed the series "Unified cross-architecture kernel-mode FPU API". This does a lot of consolidation of per-architecture kernel-mode FPU usage and enables the use of newer AMD GPUs on RISC-V. - Tao Su has fixed some selftests build warnings in the series "Selftests: Fix compilation warnings due to missing _GNU_SOURCE definition". - This pull also includes a nilfs2 fixup from Ryusuke Konishi. * tag 'mm-nonmm-stable-2024-05-22-17-30' of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm: (23 commits) nilfs2: make block erasure safe in nilfs_finish_roll_forward() selftests/harness: use 1024 in place of LINE_MAX Revert "selftests/harness: remove use of LINE_MAX" selftests/fpu: allow building on other architectures selftests/fpu: move FP code to a separate translation unit drm/amd/display: use ARCH_HAS_KERNEL_FPU_SUPPORT drm/amd/display: only use hard-float, not altivec on powerpc riscv: add support for kernel-mode FPU x86: implement ARCH_HAS_KERNEL_FPU_SUPPORT powerpc: implement ARCH_HAS_KERNEL_FPU_SUPPORT LoongArch: implement ARCH_HAS_KERNEL_FPU_SUPPORT lib/raid6: use CC_FLAGS_FPU for NEON CFLAGS arm64: crypto: use CC_FLAGS_FPU for NEON CFLAGS arm64: implement ARCH_HAS_KERNEL_FPU_SUPPORT ARM: crypto: use CC_FLAGS_FPU for NEON CFLAGS ARM: implement ARCH_HAS_KERNEL_FPU_SUPPORT arch: add ARCH_HAS_KERNEL_FPU_SUPPORT x86/fpu: fix asm/fpu/types.h include guard kbuild: enable -Wcast-function-type-strict unconditionally kbuild: enable -Wformat-truncation on clang ...
2 parents 5c6f4d6 + db3e24a commit c760b37

File tree

41 files changed

+365
-220
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+365
-220
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
.. SPDX-License-Identifier: GPL-2.0+
2+
3+
Floating-point API
4+
==================
5+
6+
Kernel code is normally prohibited from using floating-point (FP) registers or
7+
instructions, including the C float and double data types. This rule reduces
8+
system call overhead, because the kernel does not need to save and restore the
9+
userspace floating-point register state.
10+
11+
However, occasionally drivers or library functions may need to include FP code.
12+
This is supported by isolating the functions containing FP code to a separate
13+
translation unit (a separate source file), and saving/restoring the FP register
14+
state around calls to those functions. This creates "critical sections" of
15+
floating-point usage.
16+
17+
The reason for this isolation is to prevent the compiler from generating code
18+
touching the FP registers outside these critical sections. Compilers sometimes
19+
use FP registers to optimize inlined ``memcpy`` or variable assignment, as
20+
floating-point registers may be wider than general-purpose registers.
21+
22+
Usability of floating-point code within the kernel is architecture-specific.
23+
Additionally, because a single kernel may be configured to support platforms
24+
both with and without a floating-point unit, FPU availability must be checked
25+
both at build time and at run time.
26+
27+
Several architectures implement the generic kernel floating-point API from
28+
``linux/fpu.h``, as described below. Some other architectures implement their
29+
own unique APIs, which are documented separately.
30+
31+
Build-time API
32+
--------------
33+
34+
Floating-point code may be built if the option ``ARCH_HAS_KERNEL_FPU_SUPPORT``
35+
is enabled. For C code, such code must be placed in a separate file, and that
36+
file must have its compilation flags adjusted using the following pattern::
37+
38+
CFLAGS_foo.o += $(CC_FLAGS_FPU)
39+
CFLAGS_REMOVE_foo.o += $(CC_FLAGS_NO_FPU)
40+
41+
Architectures are expected to define one or both of these variables in their
42+
top-level Makefile as needed. For example::
43+
44+
CC_FLAGS_FPU := -mhard-float
45+
46+
or::
47+
48+
CC_FLAGS_NO_FPU := -msoft-float
49+
50+
Normal kernel code is assumed to use the equivalent of ``CC_FLAGS_NO_FPU``.
51+
52+
Runtime API
53+
-----------
54+
55+
The runtime API is provided in ``linux/fpu.h``. This header cannot be included
56+
from files implementing FP code (those with their compilation flags adjusted as
57+
above). Instead, it must be included when defining the FP critical sections.
58+
59+
.. c:function:: bool kernel_fpu_available( void )
60+
61+
This function reports if floating-point code can be used on this CPU or
62+
platform. The value returned by this function is not expected to change
63+
at runtime, so it only needs to be called once, not before every
64+
critical section.
65+
66+
.. c:function:: void kernel_fpu_begin( void )
67+
void kernel_fpu_end( void )
68+
69+
These functions create a floating-point critical section. It is only
70+
valid to call ``kernel_fpu_begin()`` after a previous call to
71+
``kernel_fpu_available()`` returned ``true``. These functions are only
72+
guaranteed to be callable from (preemptible or non-preemptible) process
73+
context.
74+
75+
Preemption may be disabled inside critical sections, so their size
76+
should be minimized. They are *not* required to be reentrant. If the
77+
caller expects to nest critical sections, it must implement its own
78+
reference counting.

Documentation/core-api/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Library functionality that is used throughout the kernel.
4848
errseq
4949
wrappers/atomic_t
5050
wrappers/atomic_bitops
51+
floating-point
5152

5253
Low level entry and exit
5354
========================

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,11 @@ KBUILD_CFLAGS += $(CC_FLAGS_CFI)
970970
export CC_FLAGS_CFI
971971
endif
972972

973+
# Architectures can define flags to add/remove for floating-point support
974+
CC_FLAGS_FPU += -D_LINUX_FPU_COMPILATION_UNIT
975+
export CC_FLAGS_FPU
976+
export CC_FLAGS_NO_FPU
977+
973978
ifneq ($(CONFIG_FUNCTION_ALIGNMENT),0)
974979
# Set the minimal function alignment. Use the newer GCC option
975980
# -fmin-function-alignment if it is available, or fall back to -falign-funtions.

arch/Kconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,6 +1594,12 @@ config ARCH_HAS_NONLEAF_PMD_YOUNG
15941594
address translations. Page table walkers that clear the accessed bit
15951595
may use this capability to reduce their search space.
15961596

1597+
config ARCH_HAS_KERNEL_FPU_SUPPORT
1598+
bool
1599+
help
1600+
Architectures that select this option can run floating-point code in
1601+
the kernel, as described in Documentation/core-api/floating-point.rst.
1602+
15971603
source "kernel/gcov/Kconfig"
15981604

15991605
source "scripts/gcc-plugins/Kconfig"

arch/arm/Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ endif
130130
# Accept old syntax despite ".syntax unified"
131131
AFLAGS_NOWARN :=$(call as-option,-Wa$(comma)-mno-warn-deprecated,-Wa$(comma)-W)
132132

133+
# The GCC option -ffreestanding is required in order to compile code containing
134+
# ARM/NEON intrinsics in a non C99-compliant environment (such as the kernel)
135+
CC_FLAGS_FPU := -ffreestanding
136+
# Enable <arm_neon.h>
137+
CC_FLAGS_FPU += -isystem $(shell $(CC) -print-file-name=include)
138+
CC_FLAGS_FPU += -march=armv7-a -mfloat-abi=softfp -mfpu=neon
139+
133140
ifeq ($(CONFIG_THUMB2_KERNEL),y)
134141
CFLAGS_ISA :=-Wa,-mimplicit-it=always $(AFLAGS_NOWARN)
135142
AFLAGS_ISA :=$(CFLAGS_ISA) -Wa$(comma)-mthumb

arch/arm/include/asm/fpu.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* SPDX-License-Identifier: GPL-2.0-only */
2+
/*
3+
* Copyright (C) 2023 SiFive
4+
*/
5+
6+
#ifndef __ASM_FPU_H
7+
#define __ASM_FPU_H
8+
9+
#include <asm/neon.h>
10+
11+
#define kernel_fpu_available() cpu_has_neon()
12+
#define kernel_fpu_begin() kernel_neon_begin()
13+
#define kernel_fpu_end() kernel_neon_end()
14+
15+
#endif /* ! __ASM_FPU_H */

arch/arm/lib/Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ $(obj)/csumpartialcopy.o: $(obj)/csumpartialcopygeneric.S
4040
$(obj)/csumpartialcopyuser.o: $(obj)/csumpartialcopygeneric.S
4141

4242
ifeq ($(CONFIG_KERNEL_MODE_NEON),y)
43-
NEON_FLAGS := -march=armv7-a -mfloat-abi=softfp -mfpu=neon
44-
CFLAGS_xor-neon.o += $(NEON_FLAGS)
43+
CFLAGS_xor-neon.o += $(CC_FLAGS_FPU)
4544
obj-$(CONFIG_XOR_BLOCKS) += xor-neon.o
4645
endif
4746

arch/arm64/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ config ARM64
3030
select ARCH_HAS_GCOV_PROFILE_ALL
3131
select ARCH_HAS_GIGANTIC_PAGE
3232
select ARCH_HAS_KCOV
33+
select ARCH_HAS_KERNEL_FPU_SUPPORT if KERNEL_MODE_NEON
3334
select ARCH_HAS_KEEPINITRD
3435
select ARCH_HAS_MEMBARRIER_SYNC_CORE
3536
select ARCH_HAS_NMI_SAFE_THIS_CPU_OPS

arch/arm64/Makefile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,14 @@ ifeq ($(CONFIG_BROKEN_GAS_INST),y)
3636
$(warning Detected assembler with broken .inst; disassembly will be unreliable)
3737
endif
3838

39-
KBUILD_CFLAGS += -mgeneral-regs-only \
39+
# The GCC option -ffreestanding is required in order to compile code containing
40+
# ARM/NEON intrinsics in a non C99-compliant environment (such as the kernel)
41+
CC_FLAGS_FPU := -ffreestanding
42+
# Enable <arm_neon.h>
43+
CC_FLAGS_FPU += -isystem $(shell $(CC) -print-file-name=include)
44+
CC_FLAGS_NO_FPU := -mgeneral-regs-only
45+
46+
KBUILD_CFLAGS += $(CC_FLAGS_NO_FPU) \
4047
$(compat_vdso) $(cc_has_k_constraint)
4148
KBUILD_CFLAGS += $(call cc-disable-warning, psabi)
4249
KBUILD_AFLAGS += $(compat_vdso)

arch/arm64/include/asm/fpu.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* SPDX-License-Identifier: GPL-2.0-only */
2+
/*
3+
* Copyright (C) 2023 SiFive
4+
*/
5+
6+
#ifndef __ASM_FPU_H
7+
#define __ASM_FPU_H
8+
9+
#include <asm/neon.h>
10+
11+
#define kernel_fpu_available() cpu_has_neon()
12+
#define kernel_fpu_begin() kernel_neon_begin()
13+
#define kernel_fpu_end() kernel_neon_end()
14+
15+
#endif /* ! __ASM_FPU_H */

arch/arm64/lib/Makefile

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ lib-y := clear_user.o delay.o copy_from_user.o \
77

88
ifeq ($(CONFIG_KERNEL_MODE_NEON), y)
99
obj-$(CONFIG_XOR_BLOCKS) += xor-neon.o
10-
CFLAGS_REMOVE_xor-neon.o += -mgeneral-regs-only
11-
CFLAGS_xor-neon.o += -ffreestanding
12-
# Enable <arm_neon.h>
13-
CFLAGS_xor-neon.o += -isystem $(shell $(CC) -print-file-name=include)
10+
CFLAGS_xor-neon.o += $(CC_FLAGS_FPU)
11+
CFLAGS_REMOVE_xor-neon.o += $(CC_FLAGS_NO_FPU)
1412
endif
1513

1614
lib-$(CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE) += uaccess_flushcache.o

arch/loongarch/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ config LOONGARCH
1919
select ARCH_HAS_FAST_MULTIPLIER
2020
select ARCH_HAS_FORTIFY_SOURCE
2121
select ARCH_HAS_KCOV
22+
select ARCH_HAS_KERNEL_FPU_SUPPORT if CPU_HAS_FPU
2223
select ARCH_HAS_NMI_SAFE_THIS_CPU_OPS
2324
select ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
2425
select ARCH_HAS_PTE_SPECIAL

arch/loongarch/Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ endif
2626
32bit-emul = elf32loongarch
2727
64bit-emul = elf64loongarch
2828

29+
CC_FLAGS_FPU := -mfpu=64
30+
CC_FLAGS_NO_FPU := -msoft-float
31+
2932
ifdef CONFIG_UNWINDER_ORC
3033
orc_hash_h := arch/$(SRCARCH)/include/generated/asm/orc_hash.h
3134
orc_hash_sh := $(srctree)/scripts/orc_hash.sh
@@ -59,7 +62,7 @@ ld-emul = $(64bit-emul)
5962
cflags-y += -mabi=lp64s
6063
endif
6164

62-
cflags-y += -pipe -msoft-float
65+
cflags-y += -pipe $(CC_FLAGS_NO_FPU)
6366
LDFLAGS_vmlinux += -static -n -nostdlib
6467

6568
# When the assembler supports explicit relocation hint, we must use it.

arch/loongarch/include/asm/fpu.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
struct sigcontext;
2323

24+
#define kernel_fpu_available() cpu_has_fpu
2425
extern void kernel_fpu_begin(void);
2526
extern void kernel_fpu_end(void);
2627

arch/powerpc/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ config PPC
137137
select ARCH_HAS_GCOV_PROFILE_ALL
138138
select ARCH_HAS_HUGEPD if HUGETLB_PAGE
139139
select ARCH_HAS_KCOV
140+
select ARCH_HAS_KERNEL_FPU_SUPPORT if PPC_FPU
140141
select ARCH_HAS_MEMBARRIER_CALLBACKS
141142
select ARCH_HAS_MEMBARRIER_SYNC_CORE
142143
select ARCH_HAS_MEMREMAP_COMPAT_ALIGN if PPC_64S_HASH_MMU

arch/powerpc/Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ CFLAGS-$(CONFIG_PPC32) += $(call cc-option, $(MULTIPLEWORD))
149149

150150
CFLAGS-$(CONFIG_PPC32) += $(call cc-option,-mno-readonly-in-sdata)
151151

152+
CC_FLAGS_FPU := $(call cc-option,-mhard-float)
153+
CC_FLAGS_NO_FPU := $(call cc-option,-msoft-float)
154+
152155
ifdef CONFIG_FUNCTION_TRACER
153156
ifdef CONFIG_ARCH_USING_PATCHABLE_FUNCTION_ENTRY
154157
KBUILD_CPPFLAGS += -DCC_USING_PATCHABLE_FUNCTION_ENTRY
@@ -170,7 +173,7 @@ asinstr := $(call as-instr,lis 9$(comma)foo@high,-DHAVE_AS_ATHIGH=1)
170173

171174
KBUILD_CPPFLAGS += -I $(srctree)/arch/powerpc $(asinstr)
172175
KBUILD_AFLAGS += $(AFLAGS-y)
173-
KBUILD_CFLAGS += $(call cc-option,-msoft-float)
176+
KBUILD_CFLAGS += $(CC_FLAGS_NO_FPU)
174177
KBUILD_CFLAGS += $(CFLAGS-y)
175178
CPP = $(CC) -E $(KBUILD_CFLAGS)
176179

arch/powerpc/include/asm/fpu.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* SPDX-License-Identifier: GPL-2.0-only */
2+
/*
3+
* Copyright (C) 2023 SiFive
4+
*/
5+
6+
#ifndef _ASM_POWERPC_FPU_H
7+
#define _ASM_POWERPC_FPU_H
8+
9+
#include <linux/preempt.h>
10+
11+
#include <asm/cpu_has_feature.h>
12+
#include <asm/switch_to.h>
13+
14+
#define kernel_fpu_available() (!cpu_has_feature(CPU_FTR_FPU_UNAVAILABLE))
15+
16+
static inline void kernel_fpu_begin(void)
17+
{
18+
preempt_disable();
19+
enable_kernel_fp();
20+
}
21+
22+
static inline void kernel_fpu_end(void)
23+
{
24+
disable_kernel_fp();
25+
preempt_enable();
26+
}
27+
28+
#endif /* ! _ASM_POWERPC_FPU_H */

arch/riscv/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ config RISCV
2828
select ARCH_HAS_GCOV_PROFILE_ALL
2929
select ARCH_HAS_GIGANTIC_PAGE
3030
select ARCH_HAS_KCOV
31+
select ARCH_HAS_KERNEL_FPU_SUPPORT if 64BIT && FPU
3132
select ARCH_HAS_MEMBARRIER_CALLBACKS
3233
select ARCH_HAS_MEMBARRIER_SYNC_CORE
3334
select ARCH_HAS_MMIOWB

arch/riscv/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ KBUILD_CFLAGS += -march=$(shell echo $(riscv-march-y) | sed -E 's/(rv32ima|rv64i
9191

9292
KBUILD_AFLAGS += -march=$(riscv-march-y)
9393

94+
# For C code built with floating-point support, exclude V but keep F and D.
95+
CC_FLAGS_FPU := -march=$(shell echo $(riscv-march-y) | sed -E 's/(rv32ima|rv64ima)([^v_]*)v?/\1\2/')
96+
9497
KBUILD_CFLAGS += -mno-save-restore
9598
KBUILD_CFLAGS += -DCONFIG_PAGE_OFFSET=$(CONFIG_PAGE_OFFSET)
9699

arch/riscv/include/asm/fpu.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* SPDX-License-Identifier: GPL-2.0-only */
2+
/*
3+
* Copyright (C) 2023 SiFive
4+
*/
5+
6+
#ifndef _ASM_RISCV_FPU_H
7+
#define _ASM_RISCV_FPU_H
8+
9+
#include <asm/switch_to.h>
10+
11+
#define kernel_fpu_available() has_fpu()
12+
13+
void kernel_fpu_begin(void);
14+
void kernel_fpu_end(void);
15+
16+
#endif /* ! _ASM_RISCV_FPU_H */

arch/riscv/kernel/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ obj-$(CONFIG_RISCV_MISALIGNED) += unaligned_access_speed.o
6767
obj-$(CONFIG_RISCV_PROBE_UNALIGNED_ACCESS) += copy-unaligned.o
6868

6969
obj-$(CONFIG_FPU) += fpu.o
70+
obj-$(CONFIG_FPU) += kernel_mode_fpu.o
7071
obj-$(CONFIG_RISCV_ISA_V) += vector.o
7172
obj-$(CONFIG_RISCV_ISA_V) += kernel_mode_vector.o
7273
obj-$(CONFIG_SMP) += smpboot.o

arch/riscv/kernel/kernel_mode_fpu.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Copyright (C) 2023 SiFive
4+
*/
5+
6+
#include <linux/export.h>
7+
#include <linux/preempt.h>
8+
9+
#include <asm/csr.h>
10+
#include <asm/fpu.h>
11+
#include <asm/processor.h>
12+
#include <asm/switch_to.h>
13+
14+
void kernel_fpu_begin(void)
15+
{
16+
preempt_disable();
17+
fstate_save(current, task_pt_regs(current));
18+
csr_set(CSR_SSTATUS, SR_FS);
19+
}
20+
EXPORT_SYMBOL_GPL(kernel_fpu_begin);
21+
22+
void kernel_fpu_end(void)
23+
{
24+
csr_clear(CSR_SSTATUS, SR_FS);
25+
fstate_restore(current, task_pt_regs(current));
26+
preempt_enable();
27+
}
28+
EXPORT_SYMBOL_GPL(kernel_fpu_end);

arch/x86/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ config X86
8585
select ARCH_HAS_FORTIFY_SOURCE
8686
select ARCH_HAS_GCOV_PROFILE_ALL
8787
select ARCH_HAS_KCOV if X86_64
88+
select ARCH_HAS_KERNEL_FPU_SUPPORT
8889
select ARCH_HAS_MEM_ENCRYPT
8990
select ARCH_HAS_MEMBARRIER_SYNC_CORE
9091
select ARCH_HAS_NMI_SAFE_THIS_CPU_OPS

0 commit comments

Comments
 (0)