Skip to contentSkip to conversation

x86/fpu: Extend kernel_fpu_begin_mask() for the In-Field Scan driver

PATCH Superseded Chang S. Bae 8 participants 30 messages Apr 30, 2024 – May 10, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover x86/fpu: Extend kernel_fpu_begin_mask() for the In-Field Scan driver

The recent update [1] in the SDM highlights the requirement of initializing the AMX state for executing the scan test:
"... maintaining AMX state in a non-initialized state ... will
prevent the execution of In-Field Scan tests."
which is one of CPU state conditions required for the test's execution.

In situations where AMX workloads are running, the switched-away active user AMX state remains due to the optimization to reduce the state switching cost. A user state reload is fully completed right before returning to userspace. Consequently, if the switched-in kernel task is executing the scan test, this non-initialized AMX state causes the test to be unable to start.

Given the benefit of the scan test in detecting hardware faults, ensuring its seamless execution is not negligible. This necessitates a proper API for the driver code to initialize AMX states. Although fpu_idle_fpregs() may initialize the AMX state, its primary usage should be limited to sleep state handling, making it unsuitable for the scan test.

The across-architecture FPU API, kernel_fpu_begin()/kernel_fpu_end(), is universally established for floating-point SIMD code in the kernel. On x86, kernel_fpu_begin_mask() is available, with kernel_fpu_begin() serving as a wrapper to it for initializing legacy states by default.

The proposed solution extends kernel_fpu_begin_mask() to accept a new option for initializing the AMX state. Additionally, it introduces custom FPU handling wrappers for the In-Field Scan driver, which are variants of kernel_fpu_begin()/kernel_fpu_end(). This approach is considerably compliant with established semantics, following the EFI case with efi_fpu_begin/efi_fpu_end().

Thanks,
Chang

[1] Intel Software Development Manual as of March 2024, Section 18.2 RECOMMENDATIONS FOR SYSTEM SOFTWARE of Vol. 1.
https://www.intel.com/content/www/us/en/developer/articles/technical/intel-sdm.html

Chang S. Bae (2):
x86/fpu: Extend kernel_fpu_begin_mask() to initialize AMX state platform/x86/intel/ifs: Initialize AMX state for the scan test

 arch/x86/include/asm/fpu/api.h           |  1 +
 arch/x86/kernel/fpu/core.c               |  3 +++
 drivers/platform/x86/intel/ifs/ifs.h     | 14 ++++++++++++++
 drivers/platform/x86/intel/ifs/runtest.c |  6 ++++++
 4 files changed, 24 insertions(+)

base-commit: e67572cd2204894179d89bd7b984072f19313b03

Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/2 x86/fpu: Extend kernel_fpu_begin_mask() to initialize AMX state

The In-Field Scan [1] test does not start its operation when the CPU is unprepared. If the AMX state is uninitialized, the scan test will immediately terminate. Therefore, a proper initialization of the AMX state is necessary to run the test.

Although fpu_idle_fpregs() initializes the state, its usage should be limited to specialized cases, primarily before entering the sleep state. The restore_fpregs_from_fpstate() function offers a suitable mechanism for initializing fpstate in general, which remains within the core code.

Extend kernel_fpu_begin_mask() to include AMX state initialization, providing the in-field scan driver code access to the appropriate initialization method while maintaining compliance with established FPU API semantics.

[1] https://docs.kernel.org/arch/x86/ifs.html

---
The necessity for AMX initialization is clarified in the Intel Software Development Manual as of March 2024, particularly in Section 18.2 RECOMMENDATIONS FOR SYSTEM SOFTWARE of Vol. 1.

Side note: restore_fpregs_from_fpstate() also sets the x87 state to a fixed value. However, this only applies to AMD CPUs with the FXSAVE_LEAK quirk.

---
 arch/x86/include/asm/fpu/api.h | 1 +
 arch/x86/kernel/fpu/core.c     | 3 +++
 2 files changed, 4 insertions(+)
arch/x86/include/asm/fpu/api.h +1 -0
index a2be3aefff9f..67887fc45c24 100644
@@ -25,6 +25,7 @@
 /* Kernel FPU states to initialize in kernel_fpu_begin_mask() */
 #define KFPU_387	_BITUL(0)	/* 387 state will be initialized */
 #define KFPU_MXCSR	_BITUL(1)	/* MXCSR will be initialized */
+#define KFPU_AMX	_BITUL(2)	/* AMX will be initialized */
 
 extern void kernel_fpu_begin_mask(unsigned int kfpu_mask);
 extern void kernel_fpu_end(void);
arch/x86/kernel/fpu/core.c +3 -0
index 520deb411a70..0c0235b4a901 100644
@@ -440,6 +440,9 @@ void kernel_fpu_begin_mask(unsigned int kfpu_mask)
 
 	if (unlikely(kfpu_mask & KFPU_387) && boot_cpu_has(X86_FEATURE_FPU))
 		asm volatile ("fninit");
+
+	if (unlikely(kfpu_mask & KFPU_AMX) && boot_cpu_has(X86_FEATURE_AMX_TILE))
+		restore_fpregs_from_fpstate(&init_fpstate, XFEATURE_MASK_XTILE);
 }
 EXPORT_SYMBOL_GPL(kernel_fpu_begin_mask);
 
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 1/2] x86/fpu: Extend kernel_fpu_begin_mask() to initialize AMX state

Quoted 4 lines · Chang Seok

The In-Field Scan [1] test does not start its operation when the CPU is unprepared. If the AMX state is uninitialized, the scan test will immediately terminate. Therefore, a proper initialization of the AMX state is necessary to run the test.

Instead of using new terminology like "unprepared", maybe use the term "non-initialized state" as in the SDM for clarity?

Maintaining AMX state in a non-initialized state will prevent In-field Scan test [1] to abort without making progress.

Quoted 10 lines

Although fpu_idle_fpregs() initializes the state, its usage should be limited to specialized cases, primarily before entering the sleep state. The restore_fpregs_from_fpstate() function offers a suitable mechanism for initializing fpstate in general, which remains within the core code.

Extend kernel_fpu_begin_mask() to include AMX state initialization, providing the in-field scan driver code access to the appropriate initialization method while maintaining compliance with established FPU API semantics.

Maybe simplify the above paragraph with

Extend kernel_fpu_begin_mask() to allow In-field scan driver to initialize AMX state before running tests.

Quoted 45 lines

[1] https://docs.kernel.org/arch/x86/ifs.html

---
The necessity for AMX initialization is clarified in the Intel Software Development Manual as of March 2024, particularly in Section 18.2 RECOMMENDATIONS FOR SYSTEM SOFTWARE of Vol. 1.

Side note: restore_fpregs_from_fpstate() also sets the x87 state to a fixed value. However, this only applies to AMD CPUs with the FXSAVE_LEAK quirk.

---
 arch/x86/include/asm/fpu/api.h | 1 +
 arch/x86/kernel/fpu/core.c     | 3 +++
 2 files changed, 4 insertions(+)
arch/x86/include/asm/fpu/api.h +1 -0
index a2be3aefff9f..67887fc45c24 100644
@@ -25,6 +25,7 @@
 /* Kernel FPU states to initialize in kernel_fpu_begin_mask() */
 #define KFPU_387	_BITUL(0)	/* 387 state will be initialized */
 #define KFPU_MXCSR	_BITUL(1)	/* MXCSR will be initialized */
+#define KFPU_AMX	_BITUL(2)	/* AMX will be initialized */
 
 extern void kernel_fpu_begin_mask(unsigned int kfpu_mask);
 extern void kernel_fpu_end(void);
arch/x86/kernel/fpu/core.c +3 -0
index 520deb411a70..0c0235b4a901 100644
@@ -440,6 +440,9 @@ void kernel_fpu_begin_mask(unsigned int kfpu_mask)
 
 	if (unlikely(kfpu_mask & KFPU_387) && boot_cpu_has(X86_FEATURE_FPU))
 		asm volatile ("fninit");
+
+	if (unlikely(kfpu_mask & KFPU_AMX) && boot_cpu_has(X86_FEATURE_AMX_TILE))
+		restore_fpregs_from_fpstate(&init_fpstate, XFEATURE_MASK_XTILE);
 }
 EXPORT_SYMBOL_GPL(kernel_fpu_begin_mask);
 
Signature · 1 line
2.34.1
Signature · 2 lines
Cheers,
Ashok
#3 lore raw

PATCH 2/2 platform/x86/intel/ifs: Initialize AMX state for the scan test

In reply to Chang S. Bae

The scan test does not start when the AMX state remains active and is not re-initialized. With the extension of kernel_fpu_begin_mask(), the driver code can now initialize the state properly.

Introduce custom FPU handling wrappers to ensure compliant with the established FPU API semantics, as kernel_fpu_begin() exclusively sets legacy states. This follows the EFI case from commit b0dc553cfc9d ("x86/fpu: Make the EFI FPU calling convention explicit").

Then, use these wrappers to surround the MSR_ACTIVATE_SCAN write to minimize the critical section. To prevent unnecessary delays, invoke ifs_fpu_begin() before entering the rendezvous loop.

---
 drivers/platform/x86/intel/ifs/ifs.h     | 14 ++++++++++++++
 drivers/platform/x86/intel/ifs/runtest.c |  6 ++++++
 2 files changed, 20 insertions(+)
drivers/platform/x86/intel/ifs/ifs.h +14 -0
index 56b9f3e3cf76..71d8b50854b2 100644
@@ -325,4 +325,18 @@ int do_core_test(int cpu, struct device *dev);
 extern struct attribute *plat_ifs_attrs[];
 extern struct attribute *plat_ifs_array_attrs[];
 
+static inline void ifs_fpu_begin(void)
+{
+	/*
+	 * The AMX state must be initialized prior to executing In-Field
+	 * Scan tests, according to Intel SDM.
+	 */
+	kernel_fpu_begin_mask(KFPU_AMX);
+}
+
+static inline void ifs_fpu_end(void)
+{
+	kernel_fpu_end();
+}
+
 #endif
drivers/platform/x86/intel/ifs/runtest.c +6 -0
index 95b4b71fab53..a35eac7c0b44 100644
@@ -188,6 +188,9 @@ static int doscan(void *data)
 	/* Only the first logical CPU on a core reports result */
 	first = cpumask_first(cpu_smt_mask(cpu));
 
+	/* Prepare FPU state before entering the rendezvous loop*/
+	ifs_fpu_begin();
+
 	wait_for_sibling_cpu(&scan_cpus_in, NSEC_PER_SEC);
 
 	/*
@@ -199,6 +202,9 @@ static int doscan(void *data)
 	 * are processed in a single pass) before it retires.
 	 */
 	wrmsrl(MSR_ACTIVATE_SCAN, params->activate->data);
+
+	ifs_fpu_end();
+
 	rdmsrl(MSR_SCAN_STATUS, status.data);
 
 	trace_ifs_status(ifsd->cur_batch, start, stop, status.data);
Signature · 1 line
2.34.1
#4 lore raw

Re: [PATCH 2/2] platform/x86/intel/ifs: Initialize AMX state for the scan test

Quoted 3 lines · Chang Seok

The scan test does not start when the AMX state remains active and is not re-initialized. With the extension of kernel_fpu_begin_mask(), the driver code can now initialize the state properly.

Avoid "re-initialized" and maybe use the same SDM language here as well?

Infield Scan aborts if AMX state is not in initialized state. Use the kernel_fpu_begin_mask(KFPU_AMX) to ensure AMX state is initialized.

Quoted 5 lines

Introduce custom FPU handling wrappers to ensure compliant with the established FPU API semantics, as kernel_fpu_begin() exclusively sets legacy states. This follows the EFI case from commit b0dc553cfc9d ("x86/fpu: Make the EFI FPU calling convention explicit").

The EFI and commit mention is worthy of prior use, but the first line isn't reading well... Maybe

Introduce custom FPU handling wrappers to ensure compliance with the established FPU API semantics. This change follows the EFI case from commit b0dc553cfc9d ("x86/fpu: Make the EFI FPU calling convention explicit").

I dropped the note about legacy states, I didn't quite know how to word that.

Quoted 8 lines

Then, use these wrappers to surround the MSR_ACTIVATE_SCAN write to minimize the critical section. To prevent unnecessary delays, invoke ifs_fpu_begin() before entering the rendezvous loop.

Acked-by on v1 2/2
#5 lore raw

Re: [PATCH 2/2] platform/x86/intel/ifs: Initialize AMX state for the scan test

In reply to Chang S. Bae

Hi,

Quoted 9 lines · Chang S. Bae

The scan test does not start when the AMX state remains active and is not re-initialized. With the extension of kernel_fpu_begin_mask(), the driver code can now initialize the state properly.

Introduce custom FPU handling wrappers to ensure compliant with the established FPU API semantics, as kernel_fpu_begin() exclusively sets legacy states. This follows the EFI case from commit b0dc553cfc9d ("x86/fpu: Make the EFI FPU calling convention explicit").

Quoted 7 lines

Then, use these wrappers to surround the MSR_ACTIVATE_SCAN write to minimize the critical section. To prevent unnecessary delays, invoke ifs_fpu_begin() before entering the rendezvous loop.

Thanks, the patch looks good to me.

I believe this is best merged through the tip/x86 tree together with patch 1/2 :

I have checked and this should not cause any conflicts with the ifs changes current in platform-drivers-x86/for-next.

Regards,

Hans

Quoted 52 lines
---
 drivers/platform/x86/intel/ifs/ifs.h     | 14 ++++++++++++++
 drivers/platform/x86/intel/ifs/runtest.c |  6 ++++++
 2 files changed, 20 insertions(+)
drivers/platform/x86/intel/ifs/ifs.h +14 -0
index 56b9f3e3cf76..71d8b50854b2 100644
@@ -325,4 +325,18 @@ int do_core_test(int cpu, struct device *dev);
 extern struct attribute *plat_ifs_attrs[];
 extern struct attribute *plat_ifs_array_attrs[];
 
+static inline void ifs_fpu_begin(void)
+{
+	/*
+	 * The AMX state must be initialized prior to executing In-Field
+	 * Scan tests, according to Intel SDM.
+	 */
+	kernel_fpu_begin_mask(KFPU_AMX);
+}
+
+static inline void ifs_fpu_end(void)
+{
+	kernel_fpu_end();
+}
+
 #endif
drivers/platform/x86/intel/ifs/runtest.c +6 -0
index 95b4b71fab53..a35eac7c0b44 100644
@@ -188,6 +188,9 @@ static int doscan(void *data)
 	/* Only the first logical CPU on a core reports result */
 	first = cpumask_first(cpu_smt_mask(cpu));
 
+	/* Prepare FPU state before entering the rendezvous loop*/
+	ifs_fpu_begin();
+
 	wait_for_sibling_cpu(&scan_cpus_in, NSEC_PER_SEC);
 
 	/*
@@ -199,6 +202,9 @@ static int doscan(void *data)
 	 * are processed in a single pass) before it retires.
 	 */
 	wrmsrl(MSR_ACTIVATE_SCAN, params->activate->data);
+
+	ifs_fpu_end();
+
 	rdmsrl(MSR_SCAN_STATUS, status.data);
 
 	trace_ifs_status(ifsd->cur_batch, start, stop, status.data);
#6 lore raw

Re: [PATCH 2/2] platform/x86/intel/ifs: Initialize AMX state for the scan test

Quoted 10 lines · Hans de Goede

Thanks, the patch looks good to me.

I believe this is best merged through the tip/x86 tree together with patch 1/2 :

I have checked and this should not cause any conflicts with the ifs changes current in platform-drivers-x86/for-next.

Thanks!

maintainer
#7 lore raw

Re: [PATCH 2/2] platform/x86/intel/ifs: Initialize AMX state for the scan test

In reply to Chang S. Bae
Quoted 49 lines · Chang S. Bae

The scan test does not start when the AMX state remains active and is not re-initialized. With the extension of kernel_fpu_begin_mask(), the driver code can now initialize the state properly.

Introduce custom FPU handling wrappers to ensure compliant with the established FPU API semantics, as kernel_fpu_begin() exclusively sets legacy states. This follows the EFI case from commit b0dc553cfc9d ("x86/fpu: Make the EFI FPU calling convention explicit").

Then, use these wrappers to surround the MSR_ACTIVATE_SCAN write to minimize the critical section. To prevent unnecessary delays, invoke ifs_fpu_begin() before entering the rendezvous loop.

---
 drivers/platform/x86/intel/ifs/ifs.h     | 14 ++++++++++++++
 drivers/platform/x86/intel/ifs/runtest.c |  6 ++++++
 2 files changed, 20 insertions(+)
drivers/platform/x86/intel/ifs/ifs.h +14 -0
index 56b9f3e3cf76..71d8b50854b2 100644
@@ -325,4 +325,18 @@ int do_core_test(int cpu, struct device *dev);
 extern struct attribute *plat_ifs_attrs[];
 extern struct attribute *plat_ifs_array_attrs[];
 
+static inline void ifs_fpu_begin(void)
+{
+	/*
+	 * The AMX state must be initialized prior to executing In-Field
+	 * Scan tests, according to Intel SDM.
+	 */
+	kernel_fpu_begin_mask(KFPU_AMX);
+}
+
+static inline void ifs_fpu_end(void)
+{
+	kernel_fpu_end();
+}
+
 #endif
drivers/platform/x86/intel/ifs/runtest.c +0 -0
index 95b4b71fab53..a35eac7c0b44 100644

drivers/platform/x86/intel/ifs/runtest.c static int doscan(void *data) · line 191

Quoted 4 lines
drivers/platform/x86/intel/ifs/runtest.c +1 -0
@@ -188,6 +188,9 @@ static int doscan(void *data)
 	/* Only the first logical CPU on a core reports result */
 	first = cpumask_first(cpu_smt_mask(cpu));
 
+	/* Prepare FPU state before entering the rendezvous loop*/

Missing space

Signature · 1 line
 i.
#8 lore raw

Re: [PATCH 2/2] platform/x86/intel/ifs: Initialize AMX state for the scan test

Quoted 5 lines · Ilpo Järvinen

drivers/platform/x86/intel/ifs/runtest.c static int doscan(void *data) · line 191

Quoted 1 line · Chang S. Bae
drivers/platform/x86/intel/ifs/runtest.c +1 -0
+	/* Prepare FPU state before entering the rendezvous loop*/

Missing space

Yeah, right. Thanks for spotting this.

#9 lore raw

Re: [PATCH 2/2] platform/x86/intel/ifs: Initialize AMX state for the scan test

In reply to Chang S. Bae
kernel test robot noticed the following build errors:

Hi Chang,

kernel test robot noticed the following build errors:

[auto build test ERROR on e67572cd2204894179d89bd7b984072f19313b03]

url:    https://github.com/intel-lab-lkp/linux/commits/Chang-S-Bae/x86-fpu-Extend-kernel_fpu_begin_mask-to-initialize-AMX-state/20240501-054234
base:   e67572cd2204894179d89bd7b984072f19313b03
patch link:    https://lore.kernel.org/r/20240430212508.105117-3-chang.seok.bae%40intel.com
patch subject: [PATCH 2/2] platform/x86/intel/ifs: Initialize AMX state for the scan test
config: x86_64-buildonly-randconfig-004-20240505 (https://download.01.org/0day-ci/archive/20240505/[email protected]/config)
compiler: clang version 18.1.4 (https://github.com/llvm/llvm-project e6c3289804a67ea0bb6a86fadbe454dd93b8d855)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240505/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from drivers/platform/x86/intel/ifs/load.c:9:

>> drivers/platform/x86/intel/ifs/ifs.h:334:2: error: call to undeclared function 'kernel_fpu_begin_mask'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
     334 |         kernel_fpu_begin_mask(KFPU_AMX);
         |         ^
>> drivers/platform/x86/intel/ifs/ifs.h:334:24: error: use of undeclared identifier 'KFPU_AMX'
     334 |         kernel_fpu_begin_mask(KFPU_AMX);
         |                               ^
>> drivers/platform/x86/intel/ifs/ifs.h:339:2: error: call to undeclared function 'kernel_fpu_end'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
     339 |         kernel_fpu_end();
         |         ^

3 errors generated.

vim +/kernel_fpu_begin_mask +334 drivers/platform/x86/intel/ifs/ifs.h

327
328 static inline void ifs_fpu_begin(void)
329 {
330 /*
331 * The AMX state must be initialized prior to executing In-Field
332 * Scan tests, according to Intel SDM.
333 */

 > 334		kernel_fpu_begin_mask(KFPU_AMX);
   335	}
   336	
   337	static inline void ifs_fpu_end(void)
   338	{
 > 339		kernel_fpu_end();

340 }
341

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Reviewed-by on v1 2/2
#10 lore raw

Re: [PATCH 2/2] platform/x86/intel/ifs: Initialize AMX state for the scan test

In reply to Chang S. Bae
Quoted 9 lines · Chang S. Bae

The scan test does not start when the AMX state remains active and is not re-initialized. With the extension of kernel_fpu_begin_mask(), the driver code can now initialize the state properly.

Introduce custom FPU handling wrappers to ensure compliant with the established FPU API semantics, as kernel_fpu_begin() exclusively sets legacy states. This follows the EFI case from commit b0dc553cfc9d ("x86/fpu: Make the EFI FPU calling convention explicit").

Quoted 8 lines

Then, use these wrappers to surround the MSR_ACTIVATE_SCAN write to minimize the critical section. To prevent unnecessary delays, invoke ifs_fpu_begin() before entering the rendezvous loop.

---

Looks good to me.

Quoted 9 lines
 drivers/platform/x86/intel/ifs/ifs.h     | 14 ++++++++++++++
 drivers/platform/x86/intel/ifs/runtest.c |  6 ++++++
 2 files changed, 20 insertions(+)
drivers/platform/x86/intel/ifs/ifs.h +0 -0
index 56b9f3e3cf76..71d8b50854b2 100644

drivers/platform/x86/intel/ifs/ifs.h int do_core_test(int cpu, struct device *dev); · line 332

Quoted 8 lines
drivers/platform/x86/intel/ifs/ifs.h +5 -0
@@ -325,4 +325,18 @@ int do_core_test(int cpu, struct device *dev);
 extern struct attribute *plat_ifs_attrs[];
 extern struct attribute *plat_ifs_array_attrs[];
 
+static inline void ifs_fpu_begin(void)
+{
+	/*
+	 * The AMX state must be initialized prior to executing In-Field
+	 * Scan tests, according to Intel SDM.

Nit: May be helpful to include the section title in SDM.

Quoted 34 lines
drivers/platform/x86/intel/ifs/ifs.h +9 -0
+	 */
+	kernel_fpu_begin_mask(KFPU_AMX);
+}
+
+static inline void ifs_fpu_end(void)
+{
+	kernel_fpu_end();
+}
+
 #endif
drivers/platform/x86/intel/ifs/runtest.c +6 -0
index 95b4b71fab53..a35eac7c0b44 100644
@@ -188,6 +188,9 @@ static int doscan(void *data)
 	/* Only the first logical CPU on a core reports result */
 	first = cpumask_first(cpu_smt_mask(cpu));
 
+	/* Prepare FPU state before entering the rendezvous loop*/
+	ifs_fpu_begin();
+
 	wait_for_sibling_cpu(&scan_cpus_in, NSEC_PER_SEC);
 
 	/*
@@ -199,6 +202,9 @@ static int doscan(void *data)
 	 * are processed in a single pass) before it retires.
 	 */
 	wrmsrl(MSR_ACTIVATE_SCAN, params->activate->data);
+
+	ifs_fpu_end();
+
 	rdmsrl(MSR_SCAN_STATUS, status.data);
 
 	trace_ifs_status(ifsd->cur_batch, start, stop, status.data);
Signature · 2 lines
Sathyanarayanan Kuppuswamy
Linux Kernel Developer
#11 lore raw
In reply to Chang S. Bae
Quoted 5 lines · Chang S. Bae

The recent update [1] in the SDM highlights the requirement of initializing the AMX state for executing the scan test:
"... maintaining AMX state in a non-initialized state ... will
prevent the execution of In-Field Scan tests."
which is one of CPU state conditions required for the test's execution.

This brief mention may prompt questions about why the hardware must refuse to run the test under these conditions. Because this is part of its internal, we'd go back to folks who wrote this test and will grab their write-up to provide the logic behind this requirement.

Thanks,
Chang

#12 lore raw
Quoted 2 lines · Chang S. Bae

This brief mention may prompt questions about why the hardware must refuse to run the test under these conditions. Because this is part of its internal, we'd go back to folks who wrote this test and will grab their write-up to provide the logic behind this requirement.

Okay, I got the write-up sooner than expected:

In-Field Scan (IFS) is a destructive test meaning that it will overwrite the existing state to achieve the goal of testing the logic. IFS must ensure that it is able to return the CPU back to the OS in the same architectural state as it was prior to IFS start. To do this architectural state (register state visible to the OS) is saved in a cache [*]. The cache has a limited size. When AMX is in use, the state of the AMX logic that needs to be saved is too large to be accommodated in the cache. Therefore OS must clear the AMX logic from being in use prior to IFS start.

[*] This cache is software-inaccessible space.

Thanks,
Chang

#13 lore raw
In reply to Chang S. Bae
Quoted 5 lines · Chang S. Bae

The recent update [1] in the SDM highlights the requirement of initializing the AMX state for executing the scan test:
"... maintaining AMX state in a non-initialized state ... will
prevent the execution of In-Field Scan tests."
which is one of CPU state conditions required for the test's execution.

This ended up just being phrased weirdly. It's a lot more compact to just say:

AMX must be in its init state for In-Field Scan tests to run.

Quoted 6 lines

In situations where AMX workloads are running, the switched-away active user AMX state remains due to the optimization to reduce the state switching cost. A user state reload is fully completed right before returning to userspace. Consequently, if the switched-in kernel task is executing the scan test, this non-initialized AMX state causes the test to be unable to start.

FPU state in general (and AMX state in particular) is large and expensive to context switch so the kernel tries to leave FPU state alone
even while running kernel tasks. But, this behavior obviously
conflicts with the (new) IFS need for AMX must be in its init state.

Right?

...

Quoted 3 lines

[1] Intel Software Development Manual as of March 2024, Section 18.2 RECOMMENDATIONS FOR SYSTEM SOFTWARE of Vol. 1.
https://www.intel.com/content/www/us/en/developer/articles/technical/intel-sdm.html

Rather than, "we're just following the spec", I think there can be a better explanation here.

These in-field scan tests (IFS) poke the hardware in unique ways. It ends up that IFS and AMX could attempt to use the same hardware resources at the same time and step on each other. While it would be possible to add additional resources to the CPU to allow simultaneous AMX and IFS, the hardware to do this would be relatively expensive. It seems pretty reasonable for software to help out here.

The other argument that could be made is that an admin could isolate the CPUs on which they wanted to run an IFS test. They could use cpusets, or even the task binding API to try and evict AMX workloads from these CPUs. But, the promise of IFS is that it can be run without disturbing workloads _too_ much. Basically anything an admin would do is probably too onerous and high-impact.

So this mechanism provides two things: One, it makes the hardware simpler and two, it takes the admin out of the picture. Thing just work.

#14 lore raw

v2 cover x86/fpu: Extend kernel_fpu_begin_mask() for the In-Field Scan driver

In reply to Chang S. Bae

Following feedback from the previous posting [1], this revision comes with the changelog updates for more clarity:

* The recent change in the SDM alone does not fully explain the underlying reasons. Provide a clearer explanation rather than simply echoing the SDM text.

* While it is feasible for administrators to isolate CPUs running the In-Field Scan tests from those running AMX workloads, this approach is considerably disruptive and conflicts with the goal of its live testing. Add this point to the changelog as well.

Thanks to Dave for highlighting these aspects [2]. Additionally, it is worth noting that the IFS Technical Paper [3] was recently published, which may offer further context on the IFS testing.

Thanks,
Chang

[1] V1: https://lore.kernel.org/lkml/[email protected]/
[2] Feedback: https://lore.kernel.org/lkml/[email protected]/
[3] IFS Paper: https://www.intel.com/content/www/us/en/content-details/822279/finding-faulty-components-in-a-live-fleet-environment.html

Chang S. Bae (2):
x86/fpu: Extend kernel_fpu_begin_mask() to initialize AMX state platform/x86/intel/ifs: Initialize AMX state for the scan test

 arch/x86/include/asm/fpu/api.h           |  1 +
 arch/x86/kernel/fpu/core.c               |  3 +++
 drivers/platform/x86/intel/ifs/ifs.h     | 15 +++++++++++++++
 drivers/platform/x86/intel/ifs/runtest.c |  6 ++++++
 4 files changed, 25 insertions(+)

base-commit: 7598293ab37c92025086de4b0ecd9474013a725f

Signature · 1 line
2.34.1
#15 lore raw

PATCH v2 1/2 x86/fpu: Extend kernel_fpu_begin_mask() to initialize AMX state

The In-Field Scan (IFS) test [1] is a destructive process, overwriting the existing state to test the logic on the fly. As part of this test process, the architectural state is saved before the test begins and then restored upon completion.

However, due to resource constraints in storage, AMX state is excluded from the scope of state recovery. Consequently, AMX state must be in its initialized state for the IFS test to run.

When AMX workloads are running, an active user AMX state remains even after a context switch, optimizing to reduce the state reload cost. In such cases, the test cannot proceed if it is scheduled.

System administrators may attempt to mitigate this issue, by arranging AMX workloads not to run on CPUs selected for the tests. However, this approach is disruptive for managing large-scaled systems, diminishing the benefit of the live testing.

The kernel can help by properly initializing the state before the test. This initialization impacts the performance to some degree. But, this approach is considerably cheaper than adding hardware resources and simpler than a userspace approach.

While fpu_idle_fpregs() can initialize the AMX state, its usage should be limited to specialized cases, primarily before entering the sleep state. The restore_fpregs_from_fpstate() function offers a suitable mechanism for initializing fpstate in general, which remains within the core code.

Extend kernel_fpu_begin_mask() to allow the IFS driver to initialize AMX state through restore_fpregs_from_fpstate().

[1]: https://docs.kernel.org/arch/x86/ifs.html

---
V1 -> V2: Revise the changelog (Dave Hansen and Ashok Raj).

The recently published IFS documentation [2] elaborates its purpose and the requirements of the context restoration after the scan test.

Additionally, the necessity for AMX initialization is emphasized in the Intel Software Development Manual as of March 2024, in Section 18.2 of Vol.1.

Side note: restore_fpregs_from_fpstate() also sets the x87 state to a fixed value. However, this only applies to AMD CPUs with the FXSAVE_LEAK quirk.

[2] IFS Technical Paper: Finding Faulty Components in a Live Fleet Environment
https://www.intel.com/content/www/us/en/content-details/822279/finding-faulty-components-in-a-live-fleet-environment.html

---
 arch/x86/include/asm/fpu/api.h | 1 +
 arch/x86/kernel/fpu/core.c     | 3 +++
 2 files changed, 4 insertions(+)
arch/x86/include/asm/fpu/api.h +1 -0
index a2be3aefff9f..67887fc45c24 100644
@@ -25,6 +25,7 @@
 /* Kernel FPU states to initialize in kernel_fpu_begin_mask() */
 #define KFPU_387	_BITUL(0)	/* 387 state will be initialized */
 #define KFPU_MXCSR	_BITUL(1)	/* MXCSR will be initialized */
+#define KFPU_AMX	_BITUL(2)	/* AMX will be initialized */
 
 extern void kernel_fpu_begin_mask(unsigned int kfpu_mask);
 extern void kernel_fpu_end(void);
arch/x86/kernel/fpu/core.c +3 -0
index 1209c7aebb21..04cc6f14ca42 100644
@@ -440,6 +440,9 @@ void kernel_fpu_begin_mask(unsigned int kfpu_mask)
 
 	if (unlikely(kfpu_mask & KFPU_387) && boot_cpu_has(X86_FEATURE_FPU))
 		asm volatile ("fninit");
+
+	if (unlikely(kfpu_mask & KFPU_AMX) && boot_cpu_has(X86_FEATURE_AMX_TILE))
+		restore_fpregs_from_fpstate(&init_fpstate, XFEATURE_MASK_XTILE);
 }
 EXPORT_SYMBOL_GPL(kernel_fpu_begin_mask);
 
Signature · 1 line
2.34.1
#16 lore raw

Re: [PATCH v2 1/2] x86/fpu: Extend kernel_fpu_begin_mask() to initialize AMX state

arch/x86/kernel/fpu/core.c void kernel_fpu_begin_mask(unsigned int kfpu_mask) · line 444

Quoted 10 lines · Chang S. Bae
arch/x86/kernel/fpu/core.c +2 -0
index 1209c7aebb21..04cc6f14ca42 100644
@@ -440,6 +440,9 @@ void kernel_fpu_begin_mask(unsigned int kfpu_mask)
 
 	if (unlikely(kfpu_mask & KFPU_387) && boot_cpu_has(X86_FEATURE_FPU))
 		asm volatile ("fninit");
+
+	if (unlikely(kfpu_mask & KFPU_AMX) && boot_cpu_has(X86_FEATURE_AMX_TILE))

cpu_feature_enabled() please

Quoted 3 lines
arch/x86/kernel/fpu/core.c +1 -0
+		restore_fpregs_from_fpstate(&init_fpstate, XFEATURE_MASK_XTILE);
 }
 EXPORT_SYMBOL_GPL(kernel_fpu_begin_mask);
#17 lore raw

Re: [PATCH v2 1/2] x86/fpu: Extend kernel_fpu_begin_mask() to initialize AMX state

In reply to Chang S. Bae

The subject should probably be:

Allow kernel FPU users to initialize AMX state

Quoted 4 lines · Chang S. Bae

The In-Field Scan (IFS) test [1] is a destructive process, overwriting the existing state to test the logic on the fly. As part of this test process, the architectural state is saved before the test begins and then restored upon completion.

This should say "_most_ architectural state".

Quoted 3 lines

However, due to resource constraints in storage, AMX state is excluded from the scope of state recovery. Consequently, AMX state must be in its initialized state for the IFS test to run.

This doesn't mention how this issue got introduced. Are we all bad at reading the SDM? :)

Quoted 3 lines

When AMX workloads are running, an active user AMX state remains even after a context switch, optimizing to reduce the state reload cost. In such cases, the test cannot proceed if it is scheduled.

This is a bit out of the blue. What does scheduling have do do with IFS?

Quoted 4 lines

System administrators may attempt to mitigate this issue, by arranging AMX workloads not to run on CPUs selected for the tests. However, this approach is disruptive for managing large-scaled systems, diminishing the benefit of the live testing.

I personally prefer to go and mention alternative solutions *after* I've discussed the things that are truly relevant to the problem and fix. I think this distracts from the real issue.

Quoted 9 lines

The kernel can help by properly initializing the state before the test. This initialization impacts the performance to some degree. But, this approach is considerably cheaper than adding hardware resources and simpler than a userspace approach.

While fpu_idle_fpregs() can initialize the AMX state, its usage should be limited to specialized cases, primarily before entering the sleep state. The restore_fpregs_from_fpstate() function offers a suitable mechanism for initializing fpstate in general, which remains within the core code.

I'm not sure those last two paragraphs add much value. I'd try to banish most of that content to *after* you talk about the solution. Or maybe put it in the cover letter.

Quoted 2 lines

Extend kernel_fpu_begin_mask() to allow the IFS driver to initialize AMX state through restore_fpregs_from_fpstate().

The function names are pretty blatantly obvious from the patch. No need to copy them here.

As I look closer, I'm not sure I think this is a good match for the two other KFPU_* flags. I don't think either KFPU_387 or KFPU_MXCSR causes any XFEATURE to be tracked as init. The SDM says that FNINIT "sets the FPU control, status, tag, instruction pointer, and data pointer registers to their default states."

Off the top of my head, I don't know how that maps to the XSAVE init tracking but I do know that MXCSR has a very weird mapping to the first two XSAVE features.

I really think it would be simplest to just have this whole thing do this:

	kernel_fpu_begin();

	// Zap AMX state

	kernel_fpu_end();

Where the zap is either an os_xrstor() or an explicit tile release instruction.

It's just a matter of whether you want this to work like a regular old kernel FPU user or you want to tie it to *only* being able to run in its own kernel thread. -- Aside: I don't think I realized IFS had its own thread earlier.

#18 lore raw

Re: [PATCH v2 1/2] x86/fpu: Extend kernel_fpu_begin_mask() to initialize AMX state

Quoted 8 lines · Dave Hansen
Quoted 3 lines · Chang S. Bae

However, due to resource constraints in storage, AMX state is excluded from the scope of state recovery. Consequently, AMX state must be in its initialized state for the IFS test to run.

This doesn't mention how this issue got introduced. Are we all bad at reading the SDM? :)

Ah, I'd rather zap out this SDM sentence.

Quoted 5 lines
Quoted 3 lines

When AMX workloads are running, an active user AMX state remains even after a context switch, optimizing to reduce the state reload cost. In such cases, the test cannot proceed if it is scheduled.

This is a bit out of the blue. What does scheduling have do do with IFS?

$ echo <cpu#> > /sys/devices/virtual/misc/intel_ifs_0/run_test

Then,
run_test_store()
-> do_core_test()
-> ifs_test_core()

    -> stop_core_cpuslocked()
      -> stop_cpus()
        -> queue_stop_cpus_work()
          -> cpu_stop_queue_work()
            -> wake_q_add()
            -> wake_up_q()

So, the CPU stopper threads for <cpu#> and its sibling to execute doscan() are queued up with the highest priority.

queue_stop_cpus_work() has

	for_each_cpu(cpu, cpumask) {
		work = &per_cpu(cpu_stopper.stop_work, cpu);
		work->fn = fn;
		work->arg = arg;
		work->done = done;
		work->caller = _RET_IP_;
		if (cpu_stop_queue_work(cpu, work))
			queued = true;
	}

Those threads are created during early boot via smpboot_register_percpu_thread().

Quoted 3 lines

I'm not sure those last two paragraphs add much value. I'd try to banish most of that content to *after* you talk about the solution. Or maybe put it in the cover letter.

It looks like lots of distractions coming from bunch of alternatives in different levels.

Thanks,
Chang

PS: Let me respond the solution discussion separately. I do want to experiment the init-track behavior a bit.

#19 lore raw

Re: [PATCH v2 1/2] x86/fpu: Extend kernel_fpu_begin_mask() to initialize AMX state

Quoted 11 lines · Chang S. Bae
Quoted 8 lines · Dave Hansen
Quoted 3 lines · Chang S. Bae

However, due to resource constraints in storage, AMX state is excluded from the scope of state recovery. Consequently, AMX state must be in its initialized state for the IFS test to run.

This doesn't mention how this issue got introduced.  Are we all bad at reading the SDM? :)

Ah, I'd rather zap out this SDM sentence.

My point is that this is fixing a bug. Where did that bug come from? What got screwed up here?

Hint: I don't think us software folks screwed up here. It was likely the folks that built the two hardware features (AMX and IFS) forgot to talk to each other, or someone forgot to document the AMX clobbering aspect of the architecture.

Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

When AMX workloads are running, an active user AMX state remains even after a context switch, optimizing to reduce the state reload cost. In such cases, the test cannot proceed if it is scheduled.

This is a bit out of the blue.  What does scheduling have do do with IFS?

...

Quoted 2 lines

So, the CPU stopper threads for <cpu#> and its sibling to execute doscan() are queued up with the highest priority.

...

But this is the IFS implementation *today*. The explanation depends on IFS being implemented with something that context switches. It also depends on folks expecting context switches to always switch FPU state.

I'd just say:

The kernel generally runs with live user FPU state, including AMX. That state can prevent IFS tests from running.

That's _much_ more simple, generic and also fully explains the situation. It also isn't dependent on the IFS stop_cpus_run() implementation of today, which could totally change tomorrow.

The underlying rule has zero to do with scheduling or context switching optimizations.

#20 lore raw

Re: [PATCH v2 1/2] x86/fpu: Extend kernel_fpu_begin_mask() to initialize AMX state

In reply to Dave Hansen
Quoted 10 lines · Dave Hansen

As I look closer, I'm not sure I think this is a good match for the two other KFPU_* flags. I don't think either KFPU_387 or KFPU_MXCSR causes any XFEATURE to be tracked as init. The SDM says that FNINIT "sets the FPU control, status, tag, instruction pointer, and data pointer registers to their default states."

Off the top of my head, I don't know how that maps to the XSAVE init tracking but I do know that MXCSR has a very weird mapping to the first two XSAVE features.

FNINIT does *not* set the component to be tracked as init. Indeed, the SSE component is somewhat convoluted. AMX state will be likely tracked as init. But, I believe this init tracking mechanism is implementation-specific, not architectural. Beyond this intricacy of init-tracking, KFPU_* flags all initialize each state:

/* Kernel FPU states to initialize in kernel_fpu_begin_mask() */
#define KFPU_387 _BITUL(0) /* 387 state will be initialized */
#define KFPU_MXCSR _BITUL(1) /* MXCSR will be initialized */

Quoted 10 lines

I really think it would be simplest to just have this whole thing do this:

	kernel_fpu_begin();

	// Zap AMX state

	kernel_fpu_end();

Where the zap is either an os_xrstor() or an explicit tile release instruction.

If I understand correctly, the change could be something like this, although I'm not sure about this new API naming and prototype at this point:

arch/x86/include/asm/fpu/api.h +1 -0
index a2be3aefff9f..906634402ea6 100644
@@ -28,6 +28,7 @@
 
 extern void kernel_fpu_begin_mask(unsigned int kfpu_mask);
 extern void kernel_fpu_end(void);
+extern void kernel_fpu_reset(void);
 extern bool irq_fpu_usable(void);
 extern void fpregs_mark_activate(void);
 
arch/x86/kernel/fpu/core.c +9 -0
index 1209c7aebb21..0351f9ee3e49 100644
@@ -452,6 +452,15 @@ void kernel_fpu_end(void)
 }
 EXPORT_SYMBOL_GPL(kernel_fpu_end);
 
+void kernel_fpu_reset(void)
+{
+       kernel_fpu_begin();
+       if (cpu_feature_enabled(X86_FEATURE_AMX_TILE))
+               tile_release();
+       kernel_fpu_end();
+}
+EXPORT_SYMBOL(kernel_fpu_reset);
+
 /*
  * Sync the FPU register state to current's memory register state when the
  * current task owns the FPU. The hardware register state is preserved.
drivers/platform/x86/intel/ifs/ifs.h +1 -0
index 56b9f3e3cf76..5e7ba94b4054 100644
@@ -129,6 +129,7 @@
  */
 #include <linux/device.h>
 #include <linux/miscdevice.h>
+#include <asm/fpu/api.h>
 
 #define MSR_ARRAY_BIST                         0x00000105
 #define MSR_COPY_SCAN_HASHES                   0x000002c2
drivers/platform/x86/intel/ifs/runtest.c +2 -0
index 95b4b71fab53..33ef4962aeba 100644
@@ -188,6 +188,8 @@ static int doscan(void *data)
        /* Only the first logical CPU on a core reports result */
        first = cpumask_first(cpu_smt_mask(cpu));
 
+       kernel_fpu_reset();
+
        wait_for_sibling_cpu(&scan_cpus_in, NSEC_PER_SEC);
 
        /*
Quoted 4 lines

It's just a matter of whether you want this to work like a regular old kernel FPU user or you want to tie it to *only* being able to run in its own kernel thread. -- Aside: I don't think I realized IFS had its own thread earlier.

While both approaches aim for the same functionality, the code change seems to be smaller for the above. At the same time, it is notable that these new APIs, fpu_idle_fpregs() and the other, are *only* for AMX.

1. The diff state of this series' approach:
 arch/x86/include/asm/fpu/api.h           |  1 + arch/x86/kernel/fpu/core.c               |  3 +++ drivers/platform/x86/intel/ifs/ifs.h     | 15 +++++++++++++++ drivers/platform/x86/intel/ifs/runtest.c |  6 ++++++

 4 files changed, 25 insertions(+)

2. The above code changes:

 arch/x86/include/asm/fpu/api.h           | 1 +
 arch/x86/kernel/fpu/core.c               | 9 +++++++++
 drivers/platform/x86/intel/ifs/ifs.h     | 1 +
 drivers/platform/x86/intel/ifs/runtest.c | 2 ++
 4 files changed, 13 insertions(+)

Thanks,
Chang

#21 lore raw

Re: [PATCH v2 1/2] x86/fpu: Extend kernel_fpu_begin_mask() to initialize AMX state

Quoted 9 lines · Chang S. Bae
arch/x86/kernel/fpu/core.c +9 -0
+void kernel_fpu_reset(void)
+{
+       kernel_fpu_begin();
+       if (cpu_feature_enabled(X86_FEATURE_AMX_TILE))
+               tile_release();
+       kernel_fpu_end();
+}
+EXPORT_SYMBOL(kernel_fpu_reset);
+

...

drivers/platform/x86/intel/ifs/runtest.c static int doscan(void *data) · line 193

Quoted 9 lines
drivers/platform/x86/intel/ifs/runtest.c +2 -0
@@ -188,6 +188,8 @@ static int doscan(void *data)
         /* Only the first logical CPU on a core reports result */
         first = cpumask_first(cpu_smt_mask(cpu));
 
+       kernel_fpu_reset();
+
         wait_for_sibling_cpu(&scan_cpus_in, NSEC_PER_SEC);

Remember, kernel_fpu_begin/end() mark a section of code that needs the FPU. Once code calls kernel_fpu_end(), it no longer owns the FPU and all bets are off. A interrupt could theoretically come in and do whatever it wants.

I _assume_ that this is practically impossible since the stop_machine() infrastructure keeps interrupts at bay. But it's rather subtle.

I'd probably just do this:

+ kernel_fpu_begin();
+ // AMX *MUST* be in the init state for the wrmsr() to work.
+ // But, the more in the init state, the less state the test
+ // has to save and restore. Just zap everything.
+ restore_fpregs_from_fpstate(&init_fpstate,
+ fpu_user_cfg.max_features);
+

        wrmsrl(MSR_ACTIVATE_SCAN, params->activate->data);
        rdmsrl(MSR_SCAN_STATUS, status.data);

+ kernel_fpu_end();

That's dirt simple. It doesn't require new infrastructure. It doesn't call an opaque new helper. It doesn't require a feature check. It probably makes the IFS test run faster. It will also magically work for any fancy new feature that comes along which *ALSO* needs to be in its init state ... with zero changes to this code. For bonus points, this code is quite universal. It will work, as-is, in a bunch of kernel contexts if future deranged kernel developer copies and pastes it. The code you suggested above can race unless it's called under stop_machine() and isn't safe to copy elsewhere.

Three lines of code:

1. IFS declares its need to own the FPU for a moment, like any other kernel_fpu_begin() user. It's not a special snowflake. It is boring.
2. IFS zaps the FPU state
3. IFS gives up the FPU

Am I out of my mind? What am I missing? Why bother with _anything_ more complicated than this?

#22 lore raw

Re: [PATCH v2 1/2] x86/fpu: Extend kernel_fpu_begin_mask() to initialize AMX state

Review

Quoted 2 lines · Dave Hansen
(quoted diff) +2 -0
+	restore_fpregs_from_fpstate(&init_fpstate,	
+				    fpu_user_cfg.max_features);

There is *one* subtlety here. This assumes that 'init_fpstate' has AMX marked as being in its init state. But I think that's a pretty safe assumption.

#23 lore raw

Re: [PATCH v2 1/2] x86/fpu: Extend kernel_fpu_begin_mask() to initialize AMX state

In reply to Dave Hansen

Review

Quoted 10 lines · Dave Hansen

I'd probably just do this:

(quoted diff) +7 -0
+	kernel_fpu_begin();
+	// AMX *MUST* be in the init state for the wrmsr() to work.
+	// But, the more in the init state, the less state the test
+	// has to save and restore.  Just zap everything.
+	restore_fpregs_from_fpstate(&init_fpstate,	
+				    fpu_user_cfg.max_features);
+

I assume that this snippet goes to the IFS driver side. Then, we need to introduce and export a new wrapper for this. restore_fpregs_from_fpstate() and its arguments are not accessible as of now.

Also, I think we should encapsulate them. If we follow this style, we could have invoked tilerelease() directly from the idle driver, right?

Quoted 4 lines
         wrmsrl(MSR_ACTIVATE_SCAN, params->activate->data);
         rdmsrl(MSR_SCAN_STATUS, status.data);
arch/x86/kernel/fpu/core.c +1 -0
+	kernel_fpu_end();
#24 lore raw

Re: [PATCH v2 1/2] x86/fpu: Extend kernel_fpu_begin_mask() to initialize AMX state

Quoted 12 lines · Chang S. Bae
Quoted 10 lines · Dave Hansen

I'd probably just do this:

+    kernel_fpu_begin();
+    // AMX *MUST* be in the init state for the wrmsr() to work. +    // But, the more in the init state, the less state the test +    // has to save and restore.  Just zap everything. +    restore_fpregs_from_fpstate(&init_fpstate,
+                    fpu_user_cfg.max_features);
+

Quoted 4 lines

I assume that this snippet goes to the IFS driver side. Then, we need to introduce and export a new wrapper for this. restore_fpregs_from_fpstate() and its arguments are not accessible as of now.

Yes, a new wrapper to initialize all the user FPU state is fine.

Quoted 2 lines

Also, I think we should encapsulate them. If we follow this style, we could have invoked tilerelease() directly from the idle driver, right?

You could have... But I think the point there truly was to do a minimal amount of work because you're going idle and need to race to get there. The path to idle is super hot and you don't want to do _any_ additional work. It's worth writing highly AMX-specific code because generic code would be slow there.

The IFS path is a super duper slow one. It takes *FOREVER*. I like the idea of being simple, dumb and slow.

#25 lore raw

Re: [PATCH v2 1/2] x86/fpu: Extend kernel_fpu_begin_mask() to initialize AMX state

In reply to Dave Hansen
Quoted 10 lines · Dave Hansen

Three lines of code:

1. IFS declares its need to own the FPU for a moment, like any other kernel_fpu_begin() user. It's not a special snowflake. It is boring.
2. IFS zaps the FPU state
3. IFS gives up the FPU

Am I out of my mind? What am I missing? Why bother with _anything_ more complicated than this?

Right. Keep it simple :)

#26 lore raw

PATCH v2 2/2 platform/x86/intel/ifs: Initialize AMX state for the scan test

In reply to Chang S. Bae

In-Field Scan aborts if AMX state is not in initialized state. Use kernel_fpu_begin_mask(KFPU_AMX) to ensure AMX state is initialized.

Introduce custom FPU handling wrappers to ensure compliance with the established FPU API semantics. This change follows the EFI case from commit b0dc553cfc9d ("x86/fpu: Make the EFI FPU calling convention explicit").

Then, use these wrappers to surround the MSR_ACTIVATE_SCAN write to minimize the critical section. To prevent unnecessary delays, invoke ifs_fpu_begin() before entering the rendezvous loop.

---
V1 -> V2: Massage the changelog (Ashok), add a space to the code comment
(Ilpo), and include the header file explicitly (0-day).

---
 drivers/platform/x86/intel/ifs/ifs.h     | 15 +++++++++++++++
 drivers/platform/x86/intel/ifs/runtest.c |  6 ++++++
 2 files changed, 21 insertions(+)
drivers/platform/x86/intel/ifs/ifs.h +15 -0
index 56b9f3e3cf76..a2ec0dab809b 100644
@@ -129,6 +129,7 @@
  */
 #include <linux/device.h>
 #include <linux/miscdevice.h>
+#include <asm/fpu/api.h>
 
 #define MSR_ARRAY_BIST				0x00000105
 #define MSR_COPY_SCAN_HASHES			0x000002c2
@@ -325,4 +326,18 @@ int do_core_test(int cpu, struct device *dev);
 extern struct attribute *plat_ifs_attrs[];
 extern struct attribute *plat_ifs_array_attrs[];
 
+static inline void ifs_fpu_begin(void)
+{
+	/*
+	 * The AMX state must be initialized prior to executing In-Field
+	 * Scan tests, according to Intel SDM.
+	 */
+	kernel_fpu_begin_mask(KFPU_AMX);
+}
+
+static inline void ifs_fpu_end(void)
+{
+	kernel_fpu_end();
+}
+
 #endif
drivers/platform/x86/intel/ifs/runtest.c +6 -0
index 95b4b71fab53..ca2b496a6b01 100644
@@ -188,6 +188,9 @@ static int doscan(void *data)
 	/* Only the first logical CPU on a core reports result */
 	first = cpumask_first(cpu_smt_mask(cpu));
 
+	/* Prepare FPU state before entering the rendezvous loop */
+	ifs_fpu_begin();
+
 	wait_for_sibling_cpu(&scan_cpus_in, NSEC_PER_SEC);
 
 	/*
@@ -199,6 +202,9 @@ static int doscan(void *data)
 	 * are processed in a single pass) before it retires.
 	 */
 	wrmsrl(MSR_ACTIVATE_SCAN, params->activate->data);
+
+	ifs_fpu_end();
+
 	rdmsrl(MSR_SCAN_STATUS, status.data);
 
 	trace_ifs_status(ifsd->cur_batch, start, stop, status.data);
Signature · 1 line
2.34.1
Reviewed-by on v2 2/2
#27 lore raw

Re: [PATCH v2 2/2] platform/x86/intel/ifs: Initialize AMX state for the scan test

Quoted 8 lines · Chang Seok

In-Field Scan aborts if AMX state is not in initialized state. Use kernel_fpu_begin_mask(KFPU_AMX) to ensure AMX state is initialized.

Introduce custom FPU handling wrappers to ensure compliance with the established FPU API semantics. This change follows the EFI case from commit b0dc553cfc9d ("x86/fpu: Make the EFI FPU calling convention explicit").

Quoted 8 lines

Then, use these wrappers to surround the MSR_ACTIVATE_SCAN write to minimize the critical section. To prevent unnecessary delays, invoke ifs_fpu_begin() before entering the rendezvous loop.

for both patches.

maintainer Reviewed-by on the v2 series
#29 lore raw
In reply to Chang S. Bae
Quoted 19 lines · Chang S. Bae

Following feedback from the previous posting [1], this revision comes with the changelog updates for more clarity:

* The recent change in the SDM alone does not fully explain the underlying reasons. Provide a clearer explanation rather than simply echoing the SDM text.

* While it is feasible for administrators to isolate CPUs running the In-Field Scan tests from those running AMX workloads, this approach is considerably disruptive and conflicts with the goal of its live testing. Add this point to the changelog as well.

Thanks to Dave for highlighting these aspects [2]. Additionally, it is worth noting that the IFS Technical Paper [3] was recently published, which may offer further context on the IFS testing.

Thanks,
Chang

Quoted 7 lines

[1] V1: https://lore.kernel.org/lkml/[email protected]/
[2] Feedback: https://lore.kernel.org/lkml/[email protected]/
[3] IFS Paper: https://www.intel.com/content/www/us/en/content-details/822279/finding-faulty-components-in-a-live-fleet-environment.html

Chang S. Bae (2):
x86/fpu: Extend kernel_fpu_begin_mask() to initialize AMX state platform/x86/intel/ifs: Initialize AMX state for the scan test

For both patches:

Signature · 1 line
 i.