Skip to contentSkip to conversation

x86/cpufeature: Cleanups and improvements

RFC PATCH In review Borislav Petkov 5 participants 22 messages Jan 21, 2016 – Feb 3, 2016

Subsystems (13)
lore
Share & tools
lore

Borislav Petkov

author maintainer
#0 lore raw

cover x86/cpufeature: Cleanups and improvements

From: Borislav Petkov <[email protected]>

So here's the first version of what we talked about. I've ran randbuilds on it for a night because of patch 1 with all the header reorg and we all know how that can be painful. So this version passes all randbuild configs I had generated.

In talking about patch 1, it is a bit big but if you look at the diffstat, it basically is fixing a bunch of includes and the meat of it being the carving out of the X86_FEATURE_* things to a separate file.

Rest of the patches should be pretty self-explanatory, check the respective commit messages.

I haven't tested it functionally yet (booting in kvm doesn't really count) - thus the RFC tag. I thought I should send it out first so that people can doublecheck me on the direction this is going.

Thanks!

Borislav Petkov (4):
x86/cpufeature: Carve out X86_FEATURE_* x86/cpufeature: Remove static_cpu_has() x86/cpufeature: Get rid of the non-asm goto variant x86/alternatives: Add an auxilary section

Brian Gerst (1):
x86/alternatives: Discard dynamic check after init

 Documentation/kernel-parameters.txt      |   2 +-
 arch/x86/Kconfig.debug                   |  10 -
 arch/x86/boot/cpuflags.h                 |   2 +-
 arch/x86/boot/mkcpustr.c                 |   2 +-
 arch/x86/crypto/crc32-pclmul_glue.c      |   2 +-
 arch/x86/crypto/crc32c-intel_glue.c      |   2 +-
 arch/x86/crypto/crct10dif-pclmul_glue.c  |   2 +-
 arch/x86/entry/common.c                  |   1 +
 arch/x86/entry/entry_32.S                |   2 +-
 arch/x86/entry/vdso/vdso32-setup.c       |   1 -
 arch/x86/entry/vdso/vdso32/system_call.S |   2 +-
 arch/x86/entry/vdso/vma.c                |   1 +
 arch/x86/include/asm/alternative.h       |   6 -
 arch/x86/include/asm/apic.h              |   1 -
 arch/x86/include/asm/arch_hweight.h      |   2 +
 arch/x86/include/asm/cpufeature.h        | 431 ++-----------------------------
 arch/x86/include/asm/cpufeatures.h       | 287 ++++++++++++++++++++
 arch/x86/include/asm/fpu/internal.h      |  15 +-
 arch/x86/include/asm/irq_work.h          |   2 +-
 arch/x86/include/asm/mwait.h             |   2 +
 arch/x86/include/asm/processor.h         |   3 +-
 arch/x86/include/asm/smap.h              |   2 +-
 arch/x86/include/asm/smp.h               |   1 -
 arch/x86/include/asm/thread_info.h       |   2 +-
 arch/x86/include/asm/tlbflush.h          |   1 +
 arch/x86/include/asm/uaccess_64.h        |   2 +-
 arch/x86/kernel/apic/apic_numachip.c     |   4 +-
 arch/x86/kernel/cpu/Makefile             |   2 +-
 arch/x86/kernel/cpu/centaur.c            |   2 +-
 arch/x86/kernel/cpu/common.c             |   6 -
 arch/x86/kernel/cpu/cyrix.c              |   1 +
 arch/x86/kernel/cpu/intel.c              |   2 +-
 arch/x86/kernel/cpu/intel_cacheinfo.c    |   2 +-
 arch/x86/kernel/cpu/match.c              |   2 +-
 arch/x86/kernel/cpu/mkcapflags.sh        |   6 +-
 arch/x86/kernel/cpu/mtrr/main.c          |   2 +-
 arch/x86/kernel/cpu/transmeta.c          |   2 +-
 arch/x86/kernel/e820.c                   |   1 +
 arch/x86/kernel/head_32.S                |   2 +-
 arch/x86/kernel/hpet.c                   |   1 +
 arch/x86/kernel/msr.c                    |   2 +-
 arch/x86/kernel/verify_cpu.S             |   2 +-
 arch/x86/kernel/vm86_32.c                |   2 +-
 arch/x86/kernel/vmlinux.lds.S            |   9 +
 arch/x86/lib/clear_page_64.S             |   2 +-
 arch/x86/lib/copy_page_64.S              |   2 +-
 arch/x86/lib/copy_user_64.S              |   2 +-
 arch/x86/lib/memcpy_64.S                 |   2 +-
 arch/x86/lib/memmove_64.S                |   2 +-
 arch/x86/lib/memset_64.S                 |   2 +-
 arch/x86/mm/setup_nx.c                   |   1 +
 arch/x86/oprofile/op_model_amd.c         |   1 -
 arch/x86/um/asm/barrier.h                |   2 +-
 drivers/cpufreq/intel_pstate.c           |   2 +-
 fs/btrfs/disk-io.c                       |   2 +-
 lib/atomic64_test.c                      |   2 +-
 56 files changed, 371 insertions(+), 487 deletions(-)
 create mode 100644 arch/x86/include/asm/cpufeatures.h
Signature · 1 line
2.3.5
author maintainer
#1 lore raw

PATCH 1/5 x86/cpufeature: Carve out X86_FEATURE_*

From: Borislav Petkov <[email protected]>

Move them to a separate header and have the following dependency:

x86/cpufeatures.h <- x86/processor.h <- x86/cpufeature.h

This makes it easier to use the header in asm code and not include the whole cpufeature.h and add guards for asm.

---
 Documentation/kernel-parameters.txt      |   2 +-
 arch/x86/boot/cpuflags.h                 |   2 +-
 arch/x86/boot/mkcpustr.c                 |   2 +-
 arch/x86/crypto/crc32-pclmul_glue.c      |   2 +-
 arch/x86/crypto/crc32c-intel_glue.c      |   2 +-
 arch/x86/crypto/crct10dif-pclmul_glue.c  |   2 +-
 arch/x86/entry/common.c                  |   1 +
 arch/x86/entry/entry_32.S                |   2 +-
 arch/x86/entry/vdso/vdso32-setup.c       |   1 -
 arch/x86/entry/vdso/vdso32/system_call.S |   2 +-
 arch/x86/entry/vdso/vma.c                |   1 +
 arch/x86/include/asm/alternative.h       |   6 -
 arch/x86/include/asm/apic.h              |   1 -
 arch/x86/include/asm/arch_hweight.h      |   2 +
 arch/x86/include/asm/cpufeature.h        | 283 +-----------------------------
 arch/x86/include/asm/cpufeatures.h       | 287 +++++++++++++++++++++++++++++++
 arch/x86/include/asm/fpu/internal.h      |   1 +
 arch/x86/include/asm/irq_work.h          |   2 +-
 arch/x86/include/asm/mwait.h             |   2 +
 arch/x86/include/asm/processor.h         |   3 +-
 arch/x86/include/asm/smap.h              |   2 +-
 arch/x86/include/asm/smp.h               |   1 -
 arch/x86/include/asm/thread_info.h       |   2 +-
 arch/x86/include/asm/tlbflush.h          |   1 +
 arch/x86/include/asm/uaccess_64.h        |   2 +-
 arch/x86/kernel/cpu/Makefile             |   2 +-
 arch/x86/kernel/cpu/centaur.c            |   2 +-
 arch/x86/kernel/cpu/cyrix.c              |   1 +
 arch/x86/kernel/cpu/intel.c              |   2 +-
 arch/x86/kernel/cpu/intel_cacheinfo.c    |   2 +-
 arch/x86/kernel/cpu/match.c              |   2 +-
 arch/x86/kernel/cpu/mkcapflags.sh        |   6 +-
 arch/x86/kernel/cpu/mtrr/main.c          |   2 +-
 arch/x86/kernel/cpu/transmeta.c          |   2 +-
 arch/x86/kernel/e820.c                   |   1 +
 arch/x86/kernel/head_32.S                |   2 +-
 arch/x86/kernel/hpet.c                   |   1 +
 arch/x86/kernel/msr.c                    |   2 +-
 arch/x86/kernel/verify_cpu.S             |   2 +-
 arch/x86/lib/clear_page_64.S             |   2 +-
 arch/x86/lib/copy_page_64.S              |   2 +-
 arch/x86/lib/copy_user_64.S              |   2 +-
 arch/x86/lib/memcpy_64.S                 |   2 +-
 arch/x86/lib/memmove_64.S                |   2 +-
 arch/x86/lib/memset_64.S                 |   2 +-
 arch/x86/mm/setup_nx.c                   |   1 +
 arch/x86/oprofile/op_model_amd.c         |   1 -
 arch/x86/um/asm/barrier.h                |   2 +-
 lib/atomic64_test.c                      |   2 +-
 49 files changed, 334 insertions(+), 327 deletions(-)
 create mode 100644 arch/x86/include/asm/cpufeatures.h
Documentation/kernel-parameters.txt +1 -1
index 3ea869d7a31c..fa9931f43f0c 100644
@@ -665,7 +665,7 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
 
 	clearcpuid=BITNUM [X86]
 			Disable CPUID feature X for the kernel. See
-			arch/x86/include/asm/cpufeature.h for the valid bit
+			arch/x86/include/asm/cpufeatures.h for the valid bit
 			numbers. Note the Linux specific bits are not necessarily
 			stable over kernel options, but the vendor specific
 			ones should be.
arch/x86/boot/cpuflags.h +1 -1
index ea97697e51e4..4cb404fd45ce 100644
@@ -1,7 +1,7 @@
 #ifndef BOOT_CPUFLAGS_H
 #define BOOT_CPUFLAGS_H
 
-#include <asm/cpufeature.h>
+#include <asm/cpufeatures.h>
 #include <asm/processor-flags.h>
 
 struct cpu_features {
arch/x86/boot/mkcpustr.c +1 -1
index 637097e66a62..f72498dc90d2 100644
@@ -17,7 +17,7 @@
 
 #include "../include/asm/required-features.h"
 #include "../include/asm/disabled-features.h"
-#include "../include/asm/cpufeature.h"
+#include "../include/asm/cpufeatures.h"
 #include "../kernel/cpu/capflags.c"
 
 int main(void)
arch/x86/crypto/crc32-pclmul_glue.c +1 -1
index 07d2c6c86a54..27226df3f7d8 100644
@@ -33,7 +33,7 @@
 #include <linux/crc32.h>
 #include <crypto/internal/hash.h>
 
-#include <asm/cpufeature.h>
+#include <asm/cpufeatures.h>
 #include <asm/cpu_device_id.h>
 #include <asm/fpu/api.h>
 
arch/x86/crypto/crc32c-intel_glue.c +1 -1
index 0e9871693f24..0857b1a1de3b 100644
@@ -30,7 +30,7 @@
 #include <linux/kernel.h>
 #include <crypto/internal/hash.h>
 
-#include <asm/cpufeature.h>
+#include <asm/cpufeatures.h>
 #include <asm/cpu_device_id.h>
 #include <asm/fpu/internal.h>
 
arch/x86/crypto/crct10dif-pclmul_glue.c +1 -1
index a3fcfc97a311..cd4df9322501 100644
@@ -30,7 +30,7 @@
 #include <linux/string.h>
 #include <linux/kernel.h>
 #include <asm/fpu/api.h>
-#include <asm/cpufeature.h>
+#include <asm/cpufeatures.h>
 #include <asm/cpu_device_id.h>
 
 asmlinkage __u16 crc_t10dif_pcl(__u16 crc, const unsigned char *buf,
arch/x86/entry/common.c +1 -0
index 03663740c866..02e3d89bb137 100644
@@ -26,6 +26,7 @@
 #include <asm/traps.h>
 #include <asm/vdso.h>
 #include <asm/uaccess.h>
+#include <asm/cpufeature.h>
 
 #define CREATE_TRACE_POINTS
 #include <trace/events/syscalls.h>
arch/x86/entry/entry_32.S +1 -1
index 77d8c5112900..4c5228352744 100644
@@ -40,7 +40,7 @@
 #include <asm/processor-flags.h>
 #include <asm/ftrace.h>
 #include <asm/irq_vectors.h>
-#include <asm/cpufeature.h>
+#include <asm/cpufeatures.h>
 #include <asm/alternative-asm.h>
 #include <asm/asm.h>
 #include <asm/smap.h>
arch/x86/entry/vdso/vdso32-setup.c +0 -1
index 08a317a9ae4b..7853b53959cd 100644
@@ -11,7 +11,6 @@
 #include <linux/kernel.h>
 #include <linux/mm_types.h>
 
-#include <asm/cpufeature.h>
 #include <asm/processor.h>
 #include <asm/vdso.h>
 
arch/x86/entry/vdso/vdso32/system_call.S +1 -1
index 3a1d9297074b..0109ac6cb79c 100644
@@ -3,7 +3,7 @@
 */
 
 #include <asm/dwarf2.h>
-#include <asm/cpufeature.h>
+#include <asm/cpufeatures.h>
 #include <asm/alternative-asm.h>
 
 /*
arch/x86/entry/vdso/vma.c +1 -0
index b8f69e264ac4..5471ac362147 100644
@@ -20,6 +20,7 @@
 #include <asm/page.h>
 #include <asm/hpet.h>
 #include <asm/desc.h>
+#include <asm/cpufeature.h>
 
 #if defined(CONFIG_X86_64)
 unsigned int __read_mostly vdso64_enabled = 1;
arch/x86/include/asm/alternative.h +0 -6
index 7bfc85bbb8ff..99afb665a004 100644
@@ -152,12 +152,6 @@ static inline int alternatives_text_reserved(void *start, void *end)
 	".popsection"
 
 /*
- * This must be included *after* the definition of ALTERNATIVE due to
- * <asm/arch_hweight.h>
- */
-#include <asm/cpufeature.h>
-
-/*
  * Alternative instructions for different CPU types or capabilities.
  *
  * This allows to use optimized instructions even on generic binary
arch/x86/include/asm/apic.h +0 -1
index c80f6b6f3da2..0899cfc8dfe8 100644
@@ -6,7 +6,6 @@
 
 #include <asm/alternative.h>
 #include <asm/cpufeature.h>
-#include <asm/processor.h>
 #include <asm/apicdef.h>
 #include <linux/atomic.h>
 #include <asm/fixmap.h>
arch/x86/include/asm/arch_hweight.h +2 -0
index 259a7c1ef709..02e799fa43d1 100644
@@ -1,6 +1,8 @@
 #ifndef _ASM_X86_HWEIGHT_H
 #define _ASM_X86_HWEIGHT_H
 
+#include <asm/cpufeatures.h>
+
 #ifdef CONFIG_64BIT
 /* popcnt %edi, %eax -- redundant REX prefix for alignment */
 #define POPCNT32 ".byte 0xf3,0x40,0x0f,0xb8,0xc7"
arch/x86/include/asm/cpufeature.h +1 -282

289 lines not on this page · raw message

arch/x86/include/asm/cpufeatures.h new file +287 -0

287 lines not on this page · raw message

arch/x86/include/asm/fpu/internal.h +1 -0
index 0fd440df63f1..d01199def781 100644
@@ -17,6 +17,7 @@
 #include <asm/user.h>
 #include <asm/fpu/api.h>
 #include <asm/fpu/xstate.h>
+#include <asm/cpufeature.h>
 
 /*
  * High level FPU state handling functions:
arch/x86/include/asm/irq_work.h +1 -1
index 78162f8e248b..d0afb05c84fc 100644
@@ -1,7 +1,7 @@
 #ifndef _ASM_IRQ_WORK_H
 #define _ASM_IRQ_WORK_H
 
-#include <asm/processor.h>
+#include <asm/cpufeature.h>
 
 static inline bool arch_irq_work_has_interrupt(void)
 {
arch/x86/include/asm/mwait.h +2 -0
index c70689b5e5aa..0deeb2d26df7 100644
@@ -3,6 +3,8 @@
 
 #include <linux/sched.h>
 
+#include <asm/cpufeature.h>
+
 #define MWAIT_SUBSTATE_MASK		0xf
 #define MWAIT_CSTATE_MASK		0xf
 #define MWAIT_SUBSTATE_SIZE		4
arch/x86/include/asm/processor.h +1 -2
index 2d5a50cb61a2..491a3d9dbb15 100644
@@ -13,7 +13,7 @@ struct vm86;
 #include <asm/types.h>
 #include <uapi/asm/sigcontext.h>
 #include <asm/current.h>
-#include <asm/cpufeature.h>
+#include <asm/cpufeatures.h>
 #include <asm/page.h>
 #include <asm/pgtable_types.h>
 #include <asm/percpu.h>
@@ -24,7 +24,6 @@ struct vm86;
 #include <asm/fpu/types.h>
 
 #include <linux/personality.h>
-#include <linux/cpumask.h>
 #include <linux/cache.h>
 #include <linux/threads.h>
 #include <linux/math64.h>
arch/x86/include/asm/smap.h +1 -1
index ba665ebd17bb..db333300bd4b 100644
@@ -15,7 +15,7 @@
 
 #include <linux/stringify.h>
 #include <asm/nops.h>
-#include <asm/cpufeature.h>
+#include <asm/cpufeatures.h>
 
 /* "Raw" instruction opcodes */
 #define __ASM_CLAC	.byte 0x0f,0x01,0xca
arch/x86/include/asm/smp.h +0 -1
index dfcf0727623b..20a3de5cb3b0 100644
@@ -16,7 +16,6 @@
 #endif
 #include <asm/thread_info.h>
 #include <asm/cpumask.h>
-#include <asm/cpufeature.h>
 
 extern int smp_num_siblings;
 extern unsigned int num_processors;
arch/x86/include/asm/thread_info.h +1 -1
index c7b551028740..c0778fcab06d 100644
@@ -49,7 +49,7 @@
  */
 #ifndef __ASSEMBLY__
 struct task_struct;
-#include <asm/processor.h>
+#include <asm/cpufeature.h>
 #include <linux/atomic.h>
 
 struct thread_info {
arch/x86/include/asm/tlbflush.h +1 -0
index 6df2029405a3..0bb31cb8c73b 100644
@@ -5,6 +5,7 @@
 #include <linux/sched.h>
 
 #include <asm/processor.h>
+#include <asm/cpufeature.h>
 #include <asm/special_insns.h>
 
 #ifdef CONFIG_PARAVIRT
arch/x86/include/asm/uaccess_64.h +1 -1
index f2f9b39b274a..d83a55b95a48 100644
@@ -8,7 +8,7 @@
 #include <linux/errno.h>
 #include <linux/lockdep.h>
 #include <asm/alternative.h>
-#include <asm/cpufeature.h>
+#include <asm/cpufeatures.h>
 #include <asm/page.h>
 
 /*
arch/x86/kernel/cpu/Makefile +1 -1
index 58031303e304..faa7b5204129 100644
@@ -64,7 +64,7 @@ ifdef CONFIG_X86_FEATURE_NAMES
 quiet_cmd_mkcapflags = MKCAP   $@
       cmd_mkcapflags = $(CONFIG_SHELL) $(srctree)/$(src)/mkcapflags.sh $< $@
 
-cpufeature = $(src)/../../include/asm/cpufeature.h
+cpufeature = $(src)/../../include/asm/cpufeatures.h
 
 targets += capflags.c
 $(obj)/capflags.c: $(cpufeature) $(src)/mkcapflags.sh FORCE
arch/x86/kernel/cpu/centaur.c +1 -1
index ae20be6e483c..6608c03c2126 100644
@@ -1,7 +1,7 @@
 #include <linux/bitops.h>
 #include <linux/kernel.h>
 
-#include <asm/processor.h>
+#include <asm/cpufeature.h>
 #include <asm/e820.h>
 #include <asm/mtrr.h>
 #include <asm/msr.h>
arch/x86/kernel/cpu/cyrix.c +1 -0
index aaf152e79637..15e47c1cd412 100644
@@ -8,6 +8,7 @@
 #include <linux/timer.h>
 #include <asm/pci-direct.h>
 #include <asm/tsc.h>
+#include <asm/cpufeature.h>
 
 #include "cpu.h"
 
arch/x86/kernel/cpu/intel.c +1 -1
index 565648bc1a0a..9299e3bdfad6 100644
@@ -8,7 +8,7 @@
 #include <linux/module.h>
 #include <linux/uaccess.h>
 
-#include <asm/processor.h>
+#include <asm/cpufeature.h>
 #include <asm/pgtable.h>
 #include <asm/msr.h>
 #include <asm/bugs.h>
arch/x86/kernel/cpu/intel_cacheinfo.c +1 -1
index 0b6c52388cf4..341449c49f34 100644
@@ -14,7 +14,7 @@
 #include <linux/sysfs.h>
 #include <linux/pci.h>
 
-#include <asm/processor.h>
+#include <asm/cpufeature.h>
 #include <asm/amd_nb.h>
 #include <asm/smp.h>
 
arch/x86/kernel/cpu/match.c +1 -1
index afa9f0d487ea..fbb5e90557a5 100644
@@ -1,5 +1,5 @@
 #include <asm/cpu_device_id.h>
-#include <asm/processor.h>
+#include <asm/cpufeature.h>
 #include <linux/cpu.h>
 #include <linux/module.h>
 #include <linux/slab.h>
arch/x86/kernel/cpu/mkcapflags.sh +3 -3
index 3f20710a5b23..6988c74409a8 100644
@@ -1,6 +1,6 @@
 #!/bin/sh
 #
-# Generate the x86_cap/bug_flags[] arrays from include/asm/cpufeature.h
+# Generate the x86_cap/bug_flags[] arrays from include/asm/cpufeatures.h
 #
 
 IN=$1
@@ -49,8 +49,8 @@ dump_array()
 trap 'rm "$OUT"' EXIT
 
 (
-	echo "#ifndef _ASM_X86_CPUFEATURE_H"
-	echo "#include <asm/cpufeature.h>"
+	echo "#ifndef _ASM_X86_CPUFEATURES_H"
+	echo "#include <asm/cpufeatures.h>"
 	echo "#endif"
 	echo ""
 
arch/x86/kernel/cpu/mtrr/main.c +1 -1
index 5c3d149ee91c..74f1d90f9c29 100644
@@ -47,7 +47,7 @@
 #include <linux/smp.h>
 #include <linux/syscore_ops.h>
 
-#include <asm/processor.h>
+#include <asm/cpufeature.h>
 #include <asm/e820.h>
 #include <asm/mtrr.h>
 #include <asm/msr.h>
arch/x86/kernel/cpu/transmeta.c +1 -1
index 252da7aceca6..a19a663282b5 100644
@@ -1,6 +1,6 @@
 #include <linux/kernel.h>
 #include <linux/mm.h>
-#include <asm/processor.h>
+#include <asm/cpufeature.h>
 #include <asm/msr.h>
 #include "cpu.h"
 
arch/x86/kernel/e820.c +1 -0
index 569c1e4f96fe..b3c2a697820a 100644
@@ -24,6 +24,7 @@
 #include <asm/e820.h>
 #include <asm/proto.h>
 #include <asm/setup.h>
+#include <asm/cpufeature.h>
 
 /*
  * The e820 map is the map that gets modified e.g. with command line parameters
arch/x86/kernel/head_32.S +1 -1
index 6bc9ae24b6d2..af1112980dd4 100644
@@ -19,7 +19,7 @@
 #include <asm/setup.h>
 #include <asm/processor-flags.h>
 #include <asm/msr-index.h>
-#include <asm/cpufeature.h>
+#include <asm/cpufeatures.h>
 #include <asm/percpu.h>
 #include <asm/nops.h>
 #include <asm/bootparam.h>
arch/x86/kernel/hpet.c +1 -0
index b8e6ff5cd5d0..be0ebbb6d1d1 100644
@@ -12,6 +12,7 @@
 #include <linux/pm.h>
 #include <linux/io.h>
 
+#include <asm/cpufeature.h>
 #include <asm/irqdomain.h>
 #include <asm/fixmap.h>
 #include <asm/hpet.h>
arch/x86/kernel/msr.c +1 -1
index 64f9616f93f1..7f3550acde1b 100644
@@ -40,7 +40,7 @@
 #include <linux/uaccess.h>
 #include <linux/gfp.h>
 
-#include <asm/processor.h>
+#include <asm/cpufeature.h>
 #include <asm/msr.h>
 
 static struct class *msr_class;
arch/x86/kernel/verify_cpu.S +1 -1
index 07efb35ee4bc..014ea59aa153 100644
@@ -30,7 +30,7 @@
  * 	appropriately. Either display a message or halt.
  */
 
-#include <asm/cpufeature.h>
+#include <asm/cpufeatures.h>
 #include <asm/msr-index.h>
 
 verify_cpu:
arch/x86/lib/clear_page_64.S +1 -1
index a2fe51b00cce..65be7cfaf947 100644
@@ -1,5 +1,5 @@
 #include <linux/linkage.h>
-#include <asm/cpufeature.h>
+#include <asm/cpufeatures.h>
 #include <asm/alternative-asm.h>
 
 /*
arch/x86/lib/copy_page_64.S +1 -1
index 009f98216b7e..24ef1c2104d4 100644
@@ -1,7 +1,7 @@
 /* Written 2003 by Andi Kleen, based on a kernel by Evandro Menezes */
 
 #include <linux/linkage.h>
-#include <asm/cpufeature.h>
+#include <asm/cpufeatures.h>
 #include <asm/alternative-asm.h>
 
 /*
arch/x86/lib/copy_user_64.S +1 -1
index 982ce34f4a9b..fba343062055 100644
@@ -10,7 +10,7 @@
 #include <asm/current.h>
 #include <asm/asm-offsets.h>
 #include <asm/thread_info.h>
-#include <asm/cpufeature.h>
+#include <asm/cpufeatures.h>
 #include <asm/alternative-asm.h>
 #include <asm/asm.h>
 #include <asm/smap.h>
arch/x86/lib/memcpy_64.S +1 -1
index 16698bba87de..a0de849435ad 100644
@@ -1,7 +1,7 @@
 /* Copyright 2002 Andi Kleen */
 
 #include <linux/linkage.h>
-#include <asm/cpufeature.h>
+#include <asm/cpufeatures.h>
 #include <asm/alternative-asm.h>
 
 /*
arch/x86/lib/memmove_64.S +1 -1
index ca2afdd6d98e..90ce01bee00c 100644
@@ -6,7 +6,7 @@
  *	- Copyright 2011 Fenghua Yu <[email protected]>
  */
 #include <linux/linkage.h>
-#include <asm/cpufeature.h>
+#include <asm/cpufeatures.h>
 #include <asm/alternative-asm.h>
 
 #undef memmove
arch/x86/lib/memset_64.S +1 -1
index 2661fad05827..c9c81227ea37 100644
@@ -1,7 +1,7 @@
 /* Copyright 2002 Andi Kleen, SuSE Labs */
 
 #include <linux/linkage.h>
-#include <asm/cpufeature.h>
+#include <asm/cpufeatures.h>
 #include <asm/alternative-asm.h>
 
 .weak memset
arch/x86/mm/setup_nx.c +1 -0
index 92e2eacb3321..f65a33f505b6 100644
@@ -4,6 +4,7 @@
 
 #include <asm/pgtable.h>
 #include <asm/proto.h>
+#include <asm/cpufeature.h>
 
 static int disable_nx;
 
arch/x86/oprofile/op_model_amd.c +0 -1
index 50d86c0e9ba4..660a83c8287b 100644
@@ -24,7 +24,6 @@
 #include <asm/nmi.h>
 #include <asm/apic.h>
 #include <asm/processor.h>
-#include <asm/cpufeature.h>
 
 #include "op_x86_model.h"
 #include "op_counter.h"
arch/x86/um/asm/barrier.h +1 -1
index 174781a404ff..00c319048d52 100644
@@ -3,7 +3,7 @@
 
 #include <asm/asm.h>
 #include <asm/segment.h>
-#include <asm/cpufeature.h>
+#include <asm/cpufeatures.h>
 #include <asm/cmpxchg.h>
 #include <asm/nops.h>
 
lib/atomic64_test.c +1 -1
index d62de8bf022d..123481814320 100644
@@ -17,7 +17,7 @@
 #include <linux/atomic.h>
 
 #ifdef CONFIG_X86
-#include <asm/processor.h>	/* for boot_cpu_has below */
+#include <asm/cpufeature.h>	/* for boot_cpu_has below */
 #endif
 
 #define TEST(bit, op, c_op, val)				\
Signature · 1 line
2.3.5
maintainer
#2 lore raw

Re: [PATCH 1/5] x86/cpufeature: Carve out X86_FEATURE_*

Quoted 1 line · Borislav Petkov

+#ifndef _ASM_X86_REQUIRED_FEATURES_H

Why do we have all these include guards at the point of the #include? This is not standard procedure.

-hpa

author maintainer
#3 lore raw

Re: [PATCH 1/5] x86/cpufeature: Carve out X86_FEATURE_*

Quoted 5 lines · H. Peter Anvin
Quoted 1 line · Borislav Petkov

+#ifndef _ASM_X86_REQUIRED_FEATURES_H

Why do we have all these include guards at the point of the #include? This is not standard procedure.

Some uapi monkey business:

abbf1590de22 ("UAPI: Partition the header include path sets and add uapi/ header directories")

The funny thing is, the headers themselves have the guards too. I'll try to remove them to see what breaks.

Signature · 4 lines
Regards/Gruss,
    Boris.

ECO tip #101: Trim your mails when you reply.
author maintainer
#4 lore raw

Re: [PATCH 1/5] x86/cpufeature: Carve out X86_FEATURE_*

Quoted 7 lines · Borislav Petkov
Quoted 5 lines · H. Peter Anvin
Quoted 1 line · Borislav Petkov

+#ifndef _ASM_X86_REQUIRED_FEATURES_H

Why do we have all these include guards at the point of the #include? This is not standard procedure.

Quoted 6 lines

Some uapi monkey business:

abbf1590de22 ("UAPI: Partition the header include path sets and add uapi/ header directories")

The funny thing is, the headers themselves have the guards too. I'll try to remove them to see what breaks.

Ah, here it is:

In file included from arch/x86/boot/mkcpustr.c:20:0:
arch/x86/boot/../include/asm/cpufeatures.h:4:35: fatal error: asm/required-features.h: No such file or directory compilation terminated.
make[1]: *** [arch/x86/boot/mkcpustr] Error 1
make[1]: *** Waiting for unfinished jobs....
make: *** [bzImage] Error 2
make: *** Waiting for unfinished jobs....

That's when you remove the #ifndef guards.

And I think the commit above does this special dancing to keep cpufeature.h a kernel-only header and not make it an uapi one. David, am I close?

Thanks.

Signature · 4 lines
Regards/Gruss,
    Boris.

ECO tip #101: Trim your mails when you reply.
#5 lore raw

Re: [PATCH 1/5] x86/cpufeature: Carve out X86_FEATURE_*

Quoted 8 lines · Borislav Petkov

In file included from arch/x86/boot/mkcpustr.c:20:0:
arch/x86/boot/../include/asm/cpufeatures.h:4:35: fatal error: asm/required-features.h: No such file or directory compilation terminated.
make[1]: *** [arch/x86/boot/mkcpustr] Error 1
make[1]: *** Waiting for unfinished jobs....
make: *** [bzImage] Error 2
make: *** Waiting for unfinished jobs....

Quoted 5 lines

That's when you remove the #ifndef guards.

And I think the commit above does this special dancing to keep cpufeature.h a kernel-only header and not make it an uapi one. David, am I close?

Yes. CONFIG_ symbols should be considered verboten in UAPI headers and cpufeature.h is all about using them.

David

author maintainer
#6 lore raw

PATCH 2/5 x86/cpufeature: Remove static_cpu_has()

In reply to Borislav Petkov

From: Borislav Petkov <[email protected]>

Well, this one doesn't work properly before alternatives have run. And it was supposed to provide an optimized JMP because the assumption was that the offset it is jumping to is within a signed byte and thus a two-byte JMP.

So I did an x86_64 allyesconfig build and dumped all possible sites where static_cpu_has() was used. The optimization amounted to all in all 12(!) places where static_cpu_has() had generated a 2-byte JMP. Which has saved us a whopping 36 bytes!

This clearly is not worth the trouble so we can remove it. The only place where the optimization might count - in __switch_to() - we will handle differently. But that's not subject of this patch.

---
 arch/x86/Kconfig.debug               | 10 ----
 arch/x86/include/asm/cpufeature.h    | 99 +++---------------------------------
 arch/x86/include/asm/fpu/internal.h  | 14 ++---
 arch/x86/kernel/apic/apic_numachip.c |  4 +-
 arch/x86/kernel/cpu/common.c         |  4 +-
 arch/x86/kernel/vm86_32.c            |  2 +-
 drivers/cpufreq/intel_pstate.c       |  2 +-
 fs/btrfs/disk-io.c                   |  2 +-
 8 files changed, 21 insertions(+), 116 deletions(-)
arch/x86/Kconfig.debug +0 -10
index 9b18ed97a8a2..68a2d1f0a683 100644
@@ -350,16 +350,6 @@ config DEBUG_IMR_SELFTEST
 
 	  If unsure say N here.
 
-config X86_DEBUG_STATIC_CPU_HAS
-	bool "Debug alternatives"
-	depends on DEBUG_KERNEL
-	---help---
-	  This option causes additional code to be generated which
-	  fails if static_cpu_has() is used before alternatives have
-	  run.
-
-	  If unsure, say N.
-
 config X86_DEBUG_FPU
 	bool "Debug the x86 FPU code"
 	depends on DEBUG_KERNEL
arch/x86/include/asm/cpufeature.h +7 -92

146 lines not on this page · raw message

arch/x86/include/asm/fpu/internal.h +7 -7
index d01199def781..c2e46eb96b6d 100644
@@ -59,22 +59,22 @@ extern u64 fpu__get_supported_xfeatures_mask(void);
  */
 static __always_inline __pure bool use_eager_fpu(void)
 {
-	return static_cpu_has_safe(X86_FEATURE_EAGER_FPU);
+	return static_cpu_has(X86_FEATURE_EAGER_FPU);
 }
 
 static __always_inline __pure bool use_xsaveopt(void)
 {
-	return static_cpu_has_safe(X86_FEATURE_XSAVEOPT);
+	return static_cpu_has(X86_FEATURE_XSAVEOPT);
 }
 
 static __always_inline __pure bool use_xsave(void)
 {
-	return static_cpu_has_safe(X86_FEATURE_XSAVE);
+	return static_cpu_has(X86_FEATURE_XSAVE);
 }
 
 static __always_inline __pure bool use_fxsr(void)
 {
-	return static_cpu_has_safe(X86_FEATURE_FXSR);
+	return static_cpu_has(X86_FEATURE_FXSR);
 }
 
 /*
@@ -301,7 +301,7 @@ static inline void copy_xregs_to_kernel_booting(struct xregs_state *xstate)
 
 	WARN_ON(system_state != SYSTEM_BOOTING);
 
-	if (static_cpu_has_safe(X86_FEATURE_XSAVES))
+	if (static_cpu_has(X86_FEATURE_XSAVES))
 		XSTATE_OP(XSAVES, xstate, lmask, hmask, err);
 	else
 		XSTATE_OP(XSAVE, xstate, lmask, hmask, err);
@@ -323,7 +323,7 @@ static inline void copy_kernel_to_xregs_booting(struct xregs_state *xstate)
 
 	WARN_ON(system_state != SYSTEM_BOOTING);
 
-	if (static_cpu_has_safe(X86_FEATURE_XSAVES))
+	if (static_cpu_has(X86_FEATURE_XSAVES))
 		XSTATE_OP(XRSTORS, xstate, lmask, hmask, err);
 	else
 		XSTATE_OP(XRSTOR, xstate, lmask, hmask, err);
@@ -461,7 +461,7 @@ static inline void copy_kernel_to_fpregs(union fpregs_state *fpstate)
 	 * pending. Clear the x87 state here by setting it to fixed values.
 	 * "m" is a random variable that should be in L1.
 	 */
-	if (unlikely(static_cpu_has_bug_safe(X86_BUG_FXSAVE_LEAK))) {
+	if (unlikely(static_cpu_has_bug(X86_BUG_FXSAVE_LEAK))) {
 		asm volatile(
 			"fnclex\n\t"
 			"emms\n\t"
arch/x86/kernel/apic/apic_numachip.c +2 -2
index c80c02c6ec49..ab5c2c685a3c 100644
@@ -30,7 +30,7 @@ static unsigned int numachip1_get_apic_id(unsigned long x)
 	unsigned long value;
 	unsigned int id = (x >> 24) & 0xff;
 
-	if (static_cpu_has_safe(X86_FEATURE_NODEID_MSR)) {
+	if (static_cpu_has(X86_FEATURE_NODEID_MSR)) {
 		rdmsrl(MSR_FAM10H_NODE_ID, value);
 		id |= (value << 2) & 0xff00;
 	}
@@ -178,7 +178,7 @@ static void fixup_cpu_id(struct cpuinfo_x86 *c, int node)
 	this_cpu_write(cpu_llc_id, node);
 
 	/* Account for nodes per socket in multi-core-module processors */
-	if (static_cpu_has_safe(X86_FEATURE_NODEID_MSR)) {
+	if (static_cpu_has(X86_FEATURE_NODEID_MSR)) {
 		rdmsrl(MSR_FAM10H_NODE_ID, val);
 		nodes = ((val >> 3) & 7) + 1;
 	}
arch/x86/kernel/cpu/common.c +2 -2
index 37830de8f60a..a57ec0df9cc3 100644
@@ -1483,11 +1483,11 @@ void warn_pre_alternatives(void)
 EXPORT_SYMBOL_GPL(warn_pre_alternatives);
 #endif
 
-inline bool __static_cpu_has_safe(u16 bit)
+inline bool __static_cpu_has(u16 bit)
 {
 	return boot_cpu_has(bit);
 }
-EXPORT_SYMBOL_GPL(__static_cpu_has_safe);
+EXPORT_SYMBOL_GPL(__static_cpu_has);
 
 static void bsp_resume(void)
 {
arch/x86/kernel/vm86_32.c +1 -1
index e574b8546518..3dce1ca0a653 100644
@@ -362,7 +362,7 @@ static long do_sys_vm86(struct vm86plus_struct __user *user_vm86, bool plus)
 	/* make room for real-mode segments */
 	tsk->thread.sp0 += 16;
 
-	if (static_cpu_has_safe(X86_FEATURE_SEP))
+	if (static_cpu_has(X86_FEATURE_SEP))
 		tsk->thread.sysenter_cs = 0;
 
 	load_sp0(tss, &tsk->thread);
drivers/cpufreq/intel_pstate.c +1 -1
index cd83d477e32d..3a4b39afc0ab 100644
@@ -1431,7 +1431,7 @@ static int __init intel_pstate_init(void)
 	if (!all_cpu_data)
 		return -ENOMEM;
 
-	if (static_cpu_has_safe(X86_FEATURE_HWP) && !no_hwp) {
+	if (static_cpu_has(X86_FEATURE_HWP) && !no_hwp) {
 		pr_info("intel_pstate: HWP enabled\n");
 		hwp_active++;
 	}
fs/btrfs/disk-io.c +1 -1
index e99ccd6ffb2c..87ce6129876b 100644
@@ -924,7 +924,7 @@ static int check_async_write(struct inode *inode, unsigned long bio_flags)
 	if (bio_flags & EXTENT_BIO_TREE_LOG)
 		return 0;
 #ifdef CONFIG_X86
-	if (static_cpu_has_safe(X86_FEATURE_XMM4_2))
+	if (static_cpu_has(X86_FEATURE_XMM4_2))
 		return 0;
 #endif
 	return 1;
Signature · 1 line
2.3.5
#7 lore raw

Re: [PATCH 2/5] x86/cpufeature: Remove static_cpu_has()

Quoted 7 lines · Borislav Petkov

From: Borislav Petkov <[email protected]>

Well, this one doesn't work properly before alternatives have run. And it was supposed to provide an optimized JMP because the assumption was that the offset it is jumping to is within a signed byte and thus a two-byte JMP.

Looks good except for the subject line -- you didn't actually remove static_cpu_has :)

--Andy

author maintainer
#8 lore raw

Re: [PATCH 2/5] x86/cpufeature: Remove static_cpu_has()

Quoted 2 lines · Andy Lutomirski

Looks good except for the subject line -- you didn't actually remove static_cpu_has :)

Yeah, a proper explanation didn't fit in the commit name line. So I did:

"x86/cpufeature: Remove the old unsafe static_cpu_has()

... and rename the safe one to static_cpu_has(), thereby making the safe variant the default.

..."

Signature · 4 lines
Regards/Gruss,
    Boris.

ECO tip #101: Trim your mails when you reply.
maintainer
#9 lore raw

Re: [PATCH 2/5] x86/cpufeature: Remove static_cpu_has()

Quoted 11 lines · Borislav Petkov
Quoted 2 lines · Andy Lutomirski

Looks good except for the subject line -- you didn't actually remove static_cpu_has :)

Yeah, a proper explanation didn't fit in the commit name line. So I did:

"x86/cpufeature: Remove the old unsafe static_cpu_has()

... and rename the safe one to static_cpu_has(), thereby making the safe variant the default.

Replace the old static_cpu_has() with safe variant

-hpa

author maintainer
#10 lore raw

Re: [PATCH 2/5] x86/cpufeature: Remove static_cpu_has()

Quoted 1 line · H. Peter Anvin

Replace the old static_cpu_has() with safe variant

Even better,
thanks!

Signature · 4 lines
Regards/Gruss,
    Boris.

ECO tip #101: Trim your mails when you reply.
maintainer
#11 lore raw

Re: [PATCH 2/5] x86/cpufeature: Remove static_cpu_has()

In reply to Borislav Petkov
Quoted 21 lines · Borislav Petkov

From: Borislav Petkov <[email protected]>

Well, this one doesn't work properly before alternatives have run. And it was supposed to provide an optimized JMP because the assumption was that the offset it is jumping to is within a signed byte and thus a two-byte JMP.

So I did an x86_64 allyesconfig build and dumped all possible sites where static_cpu_has() was used. The optimization amounted to all in all 12(!) places where static_cpu_has() had generated a 2-byte JMP. Which has saved us a whopping 36 bytes!

This clearly is not worth the trouble so we can remove it. The only place where the optimization might count - in __switch_to() - we will handle differently. But that's not subject of this patch.

arch/x86/include/asm/cpufeature.h +0 -0
index 3cce9f3c5cb1..2b0abb241205 100644

arch/x86/include/asm/cpufeature.h extern const char * const x86_bug_flags[NBUGINTS*32]; · line 132

Quoted 9 lines
arch/x86/include/asm/cpufeature.h +1 -1
@@ -125,103 +125,20 @@ extern const char * const x86_bug_flags[NBUGINTS*32];
 #define cpu_has_osxsave		boot_cpu_has(X86_FEATURE_OSXSAVE)
 #define cpu_has_hypervisor	boot_cpu_has(X86_FEATURE_HYPERVISOR)
 /*
- * Do not add any more of those clumsy macros - use static_cpu_has_safe() for
+ * Do not add any more of those clumsy macros - use static_cpu_has() for
  * fast paths and boot_cpu_has() otherwise!
  */
 
 #if __GNUC__ >= 4 && defined(CONFIG_X86_FAST_FEATURE_TESTS)

Just replace the __GNUC__ >= 4 with CC_HACE_ASM_GOTO in this test and get rid of the tests of CC_HAVE_ASM_GOTO in the rest of the code.

arch/x86/include/asm/cpufeature.h static __always_inline __pure bool _static_cpu_has_safe(u16 bit) · line 226

Quoted 3 lines
arch/x86/include/asm/cpufeature.h +0 -0
 /*
  * gcc 3.x is too stupid to do the static test; fall back to dynamic.
  */

... and modify this comment accordingly.

-hpa

author maintainer
#12 lore raw

PATCH 3/5 x86/cpufeature: Get rid of the non-asm goto variant

In reply to Borislav Petkov

From: Borislav Petkov <[email protected]>

I can simply quote hpa from the mail:

"Get rid of the non-asm goto variant and just fall back to dynamic if asm goto is unavailable. It doesn't make any sense, really, if it is supposed to be safe, and by now the asm goto-capable gcc is in more wide use. (Originally the gcc 3.x fallback to pure dynamic didn't exist, either.)"

Booy, am I lazy.

---
 arch/x86/include/asm/cpufeature.h | 38 +-------------------------------------
 1 file changed, 1 insertion(+), 37 deletions(-)
arch/x86/include/asm/cpufeature.h +1 -37
index 2b0abb241205..e4759223f82c 100644
@@ -174,43 +174,7 @@ static __always_inline __pure bool _static_cpu_has(u16 bit)
 	t_dynamic:
 		return __static_cpu_has(bit);
 #else
-		u8 flag;
-		/* Open-coded due to __stringify() in ALTERNATIVE() */
-		asm volatile("1: movb $2,%0\n"
-			     "2:\n"
-			     ".section .altinstructions,\"a\"\n"
-			     " .long 1b - .\n"		/* src offset */
-			     " .long 3f - .\n"		/* repl offset */
-			     " .word %P2\n"		/* always replace */
-			     " .byte 2b - 1b\n"		/* source len */
-			     " .byte 4f - 3f\n"		/* replacement len */
-			     " .byte 0\n"		/* pad len */
-			     ".previous\n"
-			     ".section .discard,\"aw\",@progbits\n"
-			     " .byte 0xff + (4f-3f) - (2b-1b)\n" /* size check */
-			     ".previous\n"
-			     ".section .altinstr_replacement,\"ax\"\n"
-			     "3: movb $0,%0\n"
-			     "4:\n"
-			     ".previous\n"
-			     ".section .altinstructions,\"a\"\n"
-			     " .long 1b - .\n"		/* src offset */
-			     " .long 5f - .\n"		/* repl offset */
-			     " .word %P1\n"		/* feature bit */
-			     " .byte 4b - 3b\n"		/* src len */
-			     " .byte 6f - 5f\n"		/* repl len */
-			     " .byte 0\n"		/* pad len */
-			     ".previous\n"
-			     ".section .discard,\"aw\",@progbits\n"
-			     " .byte 0xff + (6f-5f) - (4b-3b)\n" /* size check */
-			     ".previous\n"
-			     ".section .altinstr_replacement,\"ax\"\n"
-			     "5: movb $1,%0\n"
-			     "6:\n"
-			     ".previous\n"
-			     : "=qm" (flag)
-			     : "i" (bit), "i" (X86_FEATURE_ALWAYS));
-		return (flag == 2 ? __static_cpu_has(bit) : flag);
+		return boot_cpu_has(bit);
 #endif /* CC_HAVE_ASM_GOTO */
 }
 
Signature · 1 line
2.3.5
maintainer
#13 lore raw

Re: [PATCH 3/5] x86/cpufeature: Get rid of the non-asm goto variant

Quoted 4 lines · Borislav Petkov

From: Borislav Petkov <[email protected]>

I can simply quote hpa from the mail:

Quoted 8 lines

"Get rid of the non-asm goto variant and just fall back to dynamic if asm goto is unavailable. It doesn't make any sense, really, if it is supposed to be safe, and by now the asm goto-capable gcc is in more wide use. (Originally the gcc 3.x fallback to pure dynamic didn't exist, either.)"

Booy, am I lazy.

	;)

	-hpa
author maintainer
#14 lore raw

PATCH 4/5 x86/alternatives: Add an auxilary section

In reply to Borislav Petkov

From: Borislav Petkov <[email protected]>

Add .altinstr_aux for additional instructions which will be used before and/or during patching. All stuff which needs more sophisticated patching should go there. See next patch.

---
 arch/x86/kernel/vmlinux.lds.S | 9 +++++++++
 1 file changed, 9 insertions(+)
arch/x86/kernel/vmlinux.lds.S +9 -0
index 74e4bf11f562..1335ff4854ae 100644
@@ -244,6 +244,15 @@ SECTIONS
 	 */
 	.altinstr_replacement : AT(ADDR(.altinstr_replacement) - LOAD_OFFSET) {
 		*(.altinstr_replacement)
+		/*
+		 * Section for code used exclusively before alternatives are
+		 * run. All references to such code must be patched out by
+		 * alternatives, normally by using a patch with
+		 * X86_FEATURE_ALWAYS.
+		 *
+		 * See static_cpu_has() for an example.
+		 */
+		*(.altinstr_aux)
 	}
 
 	/*
Signature · 1 line
2.3.5
maintainer
#15 lore raw

Re: [PATCH 4/5] x86/alternatives: Add an auxilary section

Quoted 19 lines · Borislav Petkov

From: Borislav Petkov <[email protected]>

Add .altinstr_aux for additional instructions which will be used before and/or during patching. All stuff which needs more sophisticated patching should go there. See next patch.

---
 arch/x86/kernel/vmlinux.lds.S | 9 +++++++++
 1 file changed, 9 insertions(+)
arch/x86/kernel/vmlinux.lds.S +0 -0
index 74e4bf11f562..1335ff4854ae 100644
@@ -244,6 +244,15 @@ SECTIONS
 	 */
 	.altinstr_replacement : AT(ADDR(.altinstr_replacement) - LOAD_OFFSET) {
 		*(.altinstr_replacement)

arch/x86/kernel/vmlinux.lds.S SECTIONS · line 256

Quoted 10 lines
arch/x86/kernel/vmlinux.lds.S +9 -0
+		/*
+		 * Section for code used exclusively before alternatives are
+		 * run. All references to such code must be patched out by
+		 * alternatives, normally by using a patch with
+		 * X86_FEATURE_ALWAYS.
+		 *
+		 * See static_cpu_has() for an example.
+		 */
+		*(.altinstr_aux)
 	}

NAK on this being part of .altinstr_replacement (if anything it ought to simply be part of .text.init). Otherwise fine.

-hpa

author maintainer
#16 lore raw

Re: [PATCH 4/5] x86/alternatives: Add an auxilary section

Quoted 2 lines · H. Peter Anvin

NAK on this being part of .altinstr_replacement (if anything it ought to simply be part of .text.init). Otherwise fine.

You mean .init.text. Brian had it there in his original version. Out of curiosity, why don't you want it in .altinstr_replacement? To avoid any mixup?

Signature · 4 lines
Regards/Gruss,
    Boris.

ECO tip #101: Trim your mails when you reply.
maintainer
#17 lore raw

Re: [PATCH 4/5] x86/alternatives: Add an auxilary section

Quoted 8 lines · Borislav Petkov
Quoted 2 lines · H. Peter Anvin

NAK on this being part of .altinstr_replacement (if anything it ought to simply be part of .text.init). Otherwise fine.

You mean .init.text. Brian had it there in his original version. Out of curiosity, why don't you want it in .altinstr_replacement? To avoid any mixup?

We covered that already:

.altinstr_replacement doesn't need to be +X (and shouldn't be, but that's another patch.)

.altinstr_replacement is also "special" in that all the instructions are offset and not run in place. These are just plain code stubs and are executed in place, as is. The only reason to have a special section for these at all is to avoid the "init code referenced from non-init-code" warning.

-hpa

author maintainer
#18 lore raw

PATCH 5/5 x86/alternatives: Discard dynamic check after init

In reply to Borislav Petkov

From: Brian Gerst <[email protected]>

Move the code to do the dynamic check to the altinstr_aux section so that it is discarded after alternatives have run and a static branch has been chosen.

Boris: change it to do TESTB, as hpa suggests.

Cc Andrew Morton <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Boris Ostrovsky <[email protected]>
Cc Dave Young <[email protected]>
Cc Denys Vlasenko <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Kristen Carlson Accardi <[email protected]>
Cc Laura Abbott <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc "Peter Zijlstra (Intel)" <[email protected]>
Cc Prarit Bhargava <[email protected]>
Cc Ross Zwisler <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc x86-ml <[email protected]>
---
 arch/x86/include/asm/cpufeature.h | 17 +++++++++++------
 arch/x86/kernel/cpu/common.c      |  6 ------
 2 files changed, 11 insertions(+), 12 deletions(-)
arch/x86/include/asm/cpufeature.h +11 -6
index e4759223f82c..77c51f4c15b7 100644
@@ -131,7 +131,6 @@ extern const char * const x86_bug_flags[NBUGINTS*32];
 
 #if __GNUC__ >= 4 && defined(CONFIG_X86_FAST_FEATURE_TESTS)
 extern void warn_pre_alternatives(void);
-extern bool __static_cpu_has(u16 bit);
 
 /*
  * Static testing of CPU features.  Used the same as boot_cpu_has().
@@ -141,7 +140,7 @@ extern bool __static_cpu_has(u16 bit);
 static __always_inline __pure bool _static_cpu_has(u16 bit)
 {
 #ifdef CC_HAVE_ASM_GOTO
-		asm_volatile_goto("1: jmp %l[t_dynamic]\n"
+		asm_volatile_goto("1: jmp 6f\n"
 			 "2:\n"
 			 ".skip -(((5f-4f) - (2b-1b)) > 0) * "
 			         "((5f-4f) - (2b-1b)),0x90\n"
@@ -166,13 +165,19 @@ static __always_inline __pure bool _static_cpu_has(u16 bit)
 			 " .byte 0\n"			/* repl len */
 			 " .byte 0\n"			/* pad len */
 			 ".previous\n"
-			 : : "i" (bit), "i" (X86_FEATURE_ALWAYS)
-			 : : t_dynamic, t_no);
+			 ".section .altinstr_aux,\"ax\"\n"
+			 "6: testb %[bitnum],%[cap_word]\n"
+			 "   jnz %l[t_yes]\n"
+			 "   jmp %l[t_no]\n"
+			 ".previous\n"
+			 : : "i" (bit), "i" (X86_FEATURE_ALWAYS),
+			     [bitnum] "i" (1 << (bit & 7)),
+			     [cap_word] "m" (((const char *)boot_cpu_data.x86_capability)[bit >> 3])
+			 : : t_yes, t_no);
+	t_yes:
 		return true;
 	t_no:
 		return false;
-	t_dynamic:
-		return __static_cpu_has(bit);
 #else
 		return boot_cpu_has(bit);
 #endif /* CC_HAVE_ASM_GOTO */
arch/x86/kernel/cpu/common.c +0 -6
index a57ec0df9cc3..897c65bd3faa 100644
@@ -1483,12 +1483,6 @@ void warn_pre_alternatives(void)
 EXPORT_SYMBOL_GPL(warn_pre_alternatives);
 #endif
 
-inline bool __static_cpu_has(u16 bit)
-{
-	return boot_cpu_has(bit);
-}
-EXPORT_SYMBOL_GPL(__static_cpu_has);
-
 static void bsp_resume(void)
 {
 	if (this_cpu->c_bsp_resume)
Signature · 1 line
2.3.5
maintainer
#19 lore raw

Re: [PATCH 5/5] x86/alternatives: Discard dynamic check after init

Quoted 7 lines · Borislav Petkov

From: Brian Gerst <[email protected]>

Move the code to do the dynamic check to the altinstr_aux section so that it is discarded after alternatives have run and a static branch has been chosen.

Boris: change it to do TESTB, as hpa suggests.

[Also document that this changes it from C code to assembly, which makes it *substantially* smaller while avoiding a completely unnecessary call to an out of line function.]

Quoted 14 lines

* Static testing of CPU features. Used the same as boot_cpu_has().

arch/x86/include/asm/cpufeature.h +1 -1
@@ -141,7 +140,7 @@ extern bool __static_cpu_has(u16 bit);
 static __always_inline __pure bool _static_cpu_has(u16 bit)
 {
 #ifdef CC_HAVE_ASM_GOTO
-		asm_volatile_goto("1: jmp %l[t_dynamic]\n"
+		asm_volatile_goto("1: jmp 6f\n"
 			 "2:\n"
 			 ".skip -(((5f-4f) - (2b-1b)) > 0) * "
 			         "((5f-4f) - (2b-1b)),0x90\n"
@@ -166,13 +165,19 @@ static __always_inline __pure bool _static_cpu_has(u16 bit)
 			 " .byte 0\n"			/* repl len */
 			 " .byte 0\n"			/* pad len */
 			 ".previous\n"

arch/x86/include/asm/cpufeature.h static __always_inline __pure bool _static_cpu_has(u16 bit) · line 175

Quoted 10 lines
arch/x86/include/asm/cpufeature.h +8 -2
-			 : : "i" (bit), "i" (X86_FEATURE_ALWAYS)
-			 : : t_dynamic, t_no);
+			 ".section .altinstr_aux,\"ax\"\n"
+			 "6: testb %[bitnum],%[cap_word]\n"
+			 "   jnz %l[t_yes]\n"
+			 "   jmp %l[t_no]\n"
+			 ".previous\n"
+			 : : "i" (bit), "i" (X86_FEATURE_ALWAYS),
+			     [bitnum] "i" (1 << (bit & 7)),
+			     [cap_word] "m" (((const char *)boot_cpu_data.x86_capability)[bit >> 3])

Change to [cap_byte] as we normally refer to this array by 32-bit words.

Quoted 28 lines
arch/x86/include/asm/cpufeature.h +2 -2
+			 : : t_yes, t_no);
+	t_yes:
 		return true;
 	t_no:
 		return false;
-	t_dynamic:
-		return __static_cpu_has(bit);
 #else
 		return boot_cpu_has(bit);
 #endif /* CC_HAVE_ASM_GOTO */
arch/x86/kernel/cpu/common.c +0 -6
index a57ec0df9cc3..897c65bd3faa 100644
@@ -1483,12 +1483,6 @@ void warn_pre_alternatives(void)
 EXPORT_SYMBOL_GPL(warn_pre_alternatives);
 #endif
 
-inline bool __static_cpu_has(u16 bit)
-{
-	return boot_cpu_has(bit);
-}
-EXPORT_SYMBOL_GPL(__static_cpu_has);
-
 static void bsp_resume(void)
 {
 	if (this_cpu->c_bsp_resume)
#20 lore raw
In reply to Borislav Petkov
Quoted 20 lines · Borislav Petkov

From: Borislav Petkov <[email protected]>

So here's the first version of what we talked about. I've ran randbuilds on it for a night because of patch 1 with all the header reorg and we all know how that can be painful. So this version passes all randbuild configs I had generated.

In talking about patch 1, it is a bit big but if you look at the diffstat, it basically is fixing a bunch of includes and the meat of it being the carving out of the X86_FEATURE_* things to a separate file.

Rest of the patches should be pretty self-explanatory, check the respective commit messages.

I haven't tested it functionally yet (booting in kvm doesn't really count) - thus the RFC tag. I thought I should send it out first so that people can doublecheck me on the direction this is going.

Thanks!

Quoted 8 lines

Borislav Petkov (4):
x86/cpufeature: Carve out X86_FEATURE_* x86/cpufeature: Remove static_cpu_has() x86/cpufeature: Get rid of the non-asm goto variant x86/alternatives: Add an auxilary section

Brian Gerst (1):
x86/alternatives: Discard dynamic check after init

Tested it and it works. Looks good to me. Only thing was it didn't apply to tip/master due to a new cpuid flag added.

--
Brian Gerst

author maintainer
#21 lore raw
Quoted 1 line · Brian Gerst

Tested it and it works. Looks good to me.

Cool, thanks for testing! I'm going to send a v2 at some point, though.

Quoted 2 lines

Only thing was it didn't apply to tip/master due to a new cpuid flag added.

Yeah, I'm going to rebase everything to 4.5-rc1 anyway, once it comes out. I have a bunch of other branches pending too.

Signature · 4 lines
Regards/Gruss,
    Boris.

ECO tip #101: Trim your mails when you reply.