Skip to contentSkip to conversation

KVM: x86: expose AVX512_BF16 feature to guest

PATCH Applied Jing Liu 3 participants 6 messages Jul 11, 2019 – Jul 15, 2019

Subsystems (3)
lore
Share & tools
lore

Jing Liu

author
#0 lore raw

PATCH 1/1 KVM: x86: expose AVX512_BF16 feature to guest

AVX512 BFLOAT16 instructions support 16-bit BFLOAT16 floating-point format (BF16) for deep learning optimization.

Intel adds AVX512 BFLOAT16 feature in CooperLake, which is CPUID.7.1.EAX[5].

Detailed information of the CPUID bit can be found here,
https://software.intel.com/sites/default/files/managed/c5/15/\
architecture-instruction-set-extensions-programming-reference.pdf.

---

This patch depends on kernel patch https://lkml.org/lkml/2019/6/19/912
and Paolo's patch set https://lkml.org/lkml/2019/7/4/468.

 arch/x86/kvm/cpuid.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
arch/x86/kvm/cpuid.c +11 -1
index 8fc6039..0c125dd 100644
@@ -358,9 +358,13 @@ static inline void do_cpuid_7_mask(struct kvm_cpuid_entry2 *entry, int index)
 		F(SPEC_CTRL_SSBD) | F(ARCH_CAPABILITIES) | F(INTEL_STIBP) |
 		F(MD_CLEAR);
 
+	/* cpuid 7.1.eax */
+	const u32 kvm_cpuid_7_1_eax_x86_features =
+		F(AVX512_BF16);
+
 	switch (index) {
 	case 0:
-		entry->eax = 0;
+		entry->eax = min(entry->eax, 1);
 		entry->ebx &= kvm_cpuid_7_0_ebx_x86_features;
 		cpuid_mask(&entry->ebx, CPUID_7_0_EBX);
 		/* TSC_ADJUST is emulated */
@@ -384,6 +388,12 @@ static inline void do_cpuid_7_mask(struct kvm_cpuid_entry2 *entry, int index)
 		 */
 		entry->edx |= F(ARCH_CAPABILITIES);
 		break;
+	case 1:
+		entry->eax &= kvm_cpuid_7_1_eax_x86_features;
+		entry->ebx = 0;
+		entry->ecx = 0;
+		entry->edx = 0;
+		break;
 	default:
 		WARN_ON_ONCE(1);
 		entry->eax = 0;
Signature · 1 line
1.8.3.1
maintainer Applied
#1 lore raw
Quoted 41 lines · Jing Liu

AVX512 BFLOAT16 instructions support 16-bit BFLOAT16 floating-point format (BF16) for deep learning optimization.

Intel adds AVX512 BFLOAT16 feature in CooperLake, which is CPUID.7.1.EAX[5].

Detailed information of the CPUID bit can be found here,
https://software.intel.com/sites/default/files/managed/c5/15/\
architecture-instruction-set-extensions-programming-reference.pdf.

---

This patch depends on kernel patch https://lkml.org/lkml/2019/6/19/912
and Paolo's patch set https://lkml.org/lkml/2019/7/4/468.

 arch/x86/kvm/cpuid.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
arch/x86/kvm/cpuid.c +5 -1
index 8fc6039..0c125dd 100644
@@ -358,9 +358,13 @@ static inline void do_cpuid_7_mask(struct kvm_cpuid_entry2 *entry, int index)
 		F(SPEC_CTRL_SSBD) | F(ARCH_CAPABILITIES) | F(INTEL_STIBP) |
 		F(MD_CLEAR);
 
+	/* cpuid 7.1.eax */
+	const u32 kvm_cpuid_7_1_eax_x86_features =
+		F(AVX512_BF16);
+
 	switch (index) {
 	case 0:
-		entry->eax = 0;
+		entry->eax = min(entry->eax, 1);
 		entry->ebx &= kvm_cpuid_7_0_ebx_x86_features;
 		cpuid_mask(&entry->ebx, CPUID_7_0_EBX);
 		/* TSC_ADJUST is emulated */
@@ -384,6 +388,12 @@ static inline void do_cpuid_7_mask(struct kvm_cpuid_entry2 *entry, int index)
 		 */
 		entry->edx |= F(ARCH_CAPABILITIES);

arch/x86/kvm/cpuid.c static inline void do_cpuid_7_mask(struct kvm_cpuid_entry2 *entry, int index) · line 399

Quoted 10 lines
arch/x86/kvm/cpuid.c +6 -0
 		break;
+	case 1:
+		entry->eax &= kvm_cpuid_7_1_eax_x86_features;
+		entry->ebx = 0;
+		entry->ecx = 0;
+		entry->edx = 0;
+		break;
 	default:
 		WARN_ON_ONCE(1);
 		entry->eax = 0;

Queued, thanks.

Paolo

author
#2 lore raw

Hi Paolo,
Thanks for your reviewing! There also has Qemu patch sent here,
https://www.mail-archive.com/[email protected]/msg630359.html

Could you please review that? Thanks very much!

Jing

Quoted 57 lines · Paolo Bonzini
Quoted 41 lines · Jing Liu

AVX512 BFLOAT16 instructions support 16-bit BFLOAT16 floating-point format (BF16) for deep learning optimization.

Intel adds AVX512 BFLOAT16 feature in CooperLake, which is CPUID.7.1.EAX[5].

Detailed information of the CPUID bit can be found here,
https://software.intel.com/sites/default/files/managed/c5/15/\
architecture-instruction-set-extensions-programming-reference.pdf.

---

This patch depends on kernel patch https://lkml.org/lkml/2019/6/19/912
and Paolo's patch set https://lkml.org/lkml/2019/7/4/468.

  arch/x86/kvm/cpuid.c | 12 +++++++++++-
  1 file changed, 11 insertions(+), 1 deletion(-)
arch/x86/kvm/cpuid.c +5 -1
index 8fc6039..0c125dd 100644
@@ -358,9 +358,13 @@ static inline void do_cpuid_7_mask(struct kvm_cpuid_entry2 *entry, int index)
  		F(SPEC_CTRL_SSBD) | F(ARCH_CAPABILITIES) | F(INTEL_STIBP) |
  		F(MD_CLEAR);
 
+	/* cpuid 7.1.eax */
+	const u32 kvm_cpuid_7_1_eax_x86_features =
+		F(AVX512_BF16);
+
  	switch (index) {
  	case 0:
-		entry->eax = 0;
+		entry->eax = min(entry->eax, 1);
  		entry->ebx &= kvm_cpuid_7_0_ebx_x86_features;
  		cpuid_mask(&entry->ebx, CPUID_7_0_EBX);
  		/* TSC_ADJUST is emulated */
@@ -384,6 +388,12 @@ static inline void do_cpuid_7_mask(struct kvm_cpuid_entry2 *entry, int index)
  		 */
  		entry->edx |= F(ARCH_CAPABILITIES);

arch/x86/kvm/cpuid.c static inline void do_cpuid_7_mask(struct kvm_cpuid_entry2 *entry, int index) · line 399

Quoted 10 lines
arch/x86/kvm/cpuid.c +6 -0
  		break;
+	case 1:
+		entry->eax &= kvm_cpuid_7_1_eax_x86_features;
+		entry->ebx = 0;
+		entry->ecx = 0;
+		entry->edx = 0;
+		break;
  	default:
  		WARN_ON_ONCE(1);
  		entry->eax = 0;

Queued, thanks.

Paolo

#3 lore raw
In reply to Paolo Bonzini
Quoted 47 lines · Paolo Bonzini
Quoted 45 lines · Jing Liu

AVX512 BFLOAT16 instructions support 16-bit BFLOAT16 floating-point format (BF16) for deep learning optimization.

Intel adds AVX512 BFLOAT16 feature in CooperLake, which is CPUID.7.1.EAX[5].

Detailed information of the CPUID bit can be found here,
https://software.intel.com/sites/default/files/managed/c5/15/\
architecture-instruction-set-extensions-programming-reference.pdf.

---

This patch depends on kernel patch https://lkml.org/lkml/2019/6/19/912
and Paolo's patch set https://lkml.org/lkml/2019/7/4/468.

 arch/x86/kvm/cpuid.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
arch/x86/kvm/cpuid.c +9 -1
index 8fc6039..0c125dd 100644
@@ -358,9 +358,13 @@ static inline void do_cpuid_7_mask(struct kvm_cpuid_entry2 *entry, int index)
              F(SPEC_CTRL_SSBD) | F(ARCH_CAPABILITIES) | F(INTEL_STIBP) |
              F(MD_CLEAR);
 
+     /* cpuid 7.1.eax */
+     const u32 kvm_cpuid_7_1_eax_x86_features =
+             F(AVX512_BF16);
+
      switch (index) {
      case 0:
-             entry->eax = 0;
+             entry->eax = min(entry->eax, 1);
              entry->ebx &= kvm_cpuid_7_0_ebx_x86_features;
              cpuid_mask(&entry->ebx, CPUID_7_0_EBX);
              /* TSC_ADJUST is emulated */
@@ -384,6 +388,12 @@ static inline void do_cpuid_7_mask(struct kvm_cpuid_entry2 *entry, int index)
               */
              entry->edx |= F(ARCH_CAPABILITIES);
              break;
+     case 1:
+             entry->eax &= kvm_cpuid_7_1_eax_x86_features;
+             entry->ebx = 0;
+             entry->ecx = 0;
Quoted 8 lines

arch/x86/kvm/cpuid.c static inline void do_cpuid_7_mask(struct kvm_cpuid_entry2 *entry, int index) · line 399

Quoted 6 lines
arch/x86/kvm/cpuid.c +2 -0
+             entry->edx = 0;
+             break;
      default:
              WARN_ON_ONCE(1);
              entry->eax = 0;

Queued, thanks.

I see this in kvm/queue:

In file included from ./include/linux/list.h:9:0,

                 from ./include/linux/preempt.h:11,
                 from ./include/linux/hardirq.h:5,
                 from ./include/linux/kvm_host.h:7,
                 from /home/kernel/data/kvm/arch/x86/kvm//cpuid.c:12:

/home/kernel/data/kvm/arch/x86/kvm//cpuid.c: In function ‘do_cpuid_7_mask’:
./include/linux/kernel.h:819:29: warning: comparison of distinct pointer types lacks a cast
(!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))

                             ^
./include/linux/kernel.h:833:4: note: in expansion of macro ‘__typecheck’
   (__typecheck(x, y) && __no_side_effects(x, y))
    ^
./include/linux/kernel.h:843:24: note: in expansion of macro ‘__safe_cmp’
  __builtin_choose_expr(__safe_cmp(x, y), \
                        ^
./include/linux/kernel.h:852:19: note: in expansion of macro ‘__careful_cmp’
 #define min(x, y) __careful_cmp(x, y, <)
                   ^
/home/kernel/data/kvm/arch/x86/kvm//cpuid.c:377:16: note: in expansion
of macro ‘min’
   entry->eax = min(entry->eax, 1);
                ^
author
#4 lore raw
Quoted 15 lines · Wanpeng Li
Quoted 14 lines · Paolo Bonzini
Quoted 12 lines · Jing Liu

AVX512 BFLOAT16 instructions support 16-bit BFLOAT16 floating-point format (BF16) for deep learning optimization.

Intel adds AVX512 BFLOAT16 feature in CooperLake, which is CPUID.7.1.EAX[5].

Detailed information of the CPUID bit can be found here,
https://software.intel.com/sites/default/files/managed/c5/15/\
architecture-instruction-set-extensions-programming-reference.pdf.

---

[...]

Quoted 19 lines

/home/kernel/data/kvm/arch/x86/kvm//cpuid.c: In function ‘do_cpuid_7_mask’:
./include/linux/kernel.h:819:29: warning: comparison of distinct pointer types lacks a cast

    (!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))
                              ^
./include/linux/kernel.h:833:4: note: in expansion of macro ‘__typecheck’
    (__typecheck(x, y) && __no_side_effects(x, y))
     ^
./include/linux/kernel.h:843:24: note: in expansion of macro ‘__safe_cmp’
   __builtin_choose_expr(__safe_cmp(x, y), \
                         ^
./include/linux/kernel.h:852:19: note: in expansion of macro ‘__careful_cmp’
  #define min(x, y) __careful_cmp(x, y, <)
                    ^
/home/kernel/data/kvm/arch/x86/kvm//cpuid.c:377:16: note: in expansion
of macro ‘min’
    entry->eax = min(entry->eax, 1);
                 ^

Thanks for the information.

This warning would be fixed by changing to
entry->eax = min(entry->eax, (u32)1);

@Paolo, sorry for trouble. Would you mind if I re-send?

Jing

maintainer
#5 lore raw
Quoted 8 lines · Jing Liu
Quoted 2 lines

Thanks for the information.

This warning would be fixed by changing to
entry->eax = min(entry->eax, (u32)1);

@Paolo, sorry for trouble. Would you mind if I re-send?

No need, I can fix it myself (I'd also use 1u instead of the cast).

Paolo