From: Jez Ng HFI is enabled per package by writing the physical address of a table to MSR_IA32_HW_FEEDBACK_PTR and setting the enable bit in MSR_IA32_HW_FEEDBACK_CONFIG. From then on, hardware writes performance and energy efficiency updates to that table on its own. Nothing disables HFI when the kernel shuts down. After a kexec, hardware keeps writing to the old kernel's table, which is now ordinary free memory in the new kernel. The new kernel does not reprogram HFI unless a thermal netlink client subscribes to events, so the writes continue indefinitely. This was observed on dual-socket Sapphire Rapids systems (Xeon Platinum 8462Y+) that boot a Debian 6.1 kernel and kexec into a 7.0 kernel. Both packages kept writing HFI updates (a timestamp, a changed flag and one row per CPU) into the old kernel's table pages. When the inode hash table landed on one of those pages, the new kernel oopsed in find_inode_fast(). Disabling HFI before kexec made the writes stop. Add a syscore shutdown callback that disables HFI on every package with an online CPU. syscore_shutdown() runs on reboot, power off and kexec, after CPU hotplug is disabled and while the other CPUs are still online, so each package can be reached with smp_call_function_single(). Do not enable HFI again once the callback has run. On kexec, user space keeps running on the other CPUs until machine_shutdown() stops them, and kernel_kexec() enables CPU hotplug again after syscore_shutdown(). A new thermal netlink client, or the first CPU of a package coming online, would otherwise enable HFI after it was disabled. Skip packages where HFI is already disabled. hfi_disable() waits up to 2 ms for hardware to set HFI_UPDATED, which a package that is not running HFI may never do. Fixes: ab09b0744a99 ("thermal: intel: hfi: Enable notification interrupt") Cc: stable@vger.kernel.org Reviewed-by: Andrew James Reviewed-by: John Ranzie Signed-off-by: Jez Ng --- drivers/thermal/intel/intel_hfi.c | 50 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/drivers/thermal/intel/intel_hfi.c b/drivers/thermal/intel/intel_hfi.c index 3273b8fe3d4d..8ff094eccbaf 100644 --- a/drivers/thermal/intel/intel_hfi.c +++ b/drivers/thermal/intel/intel_hfi.c @@ -161,6 +161,7 @@ static DEFINE_PER_CPU(struct hfi_cpu_info, hfi_cpu_info) = { .index = -1 }; static int max_hfi_instances; static int hfi_clients_nr; +static bool hfi_shutdown_done; static struct hfi_instance *hfi_instances; static struct hfi_features hfi_features; @@ -398,6 +399,19 @@ static void hfi_disable(void) } } +/* Caller must hold hfi_instance_lock. */ +static bool hfi_disable_if_enabled(void) +{ + u64 msr_val; + + rdmsrq(MSR_IA32_HW_FEEDBACK_CONFIG, msr_val); + if (!(msr_val & HW_FEEDBACK_CONFIG_HFI_ENABLE_BIT)) + return false; + + hfi_disable(); + return true; +} + /** * intel_hfi_online() - Enable HFI on @cpu * @cpu: CPU in which the HFI will be enabled @@ -483,7 +497,8 @@ void intel_hfi_online(unsigned int cpu) * Enable this HFI instance if this is its first online CPU and * there are user-space clients of thermal events. */ - if (cpumask_weight(hfi_instance->cpus) == 1 && hfi_clients_nr > 0) { + if (cpumask_weight(hfi_instance->cpus) == 1 && hfi_clients_nr > 0 && + !hfi_shutdown_done) { hfi_set_hw_table(hfi_instance); hfi_enable(); } @@ -593,6 +608,11 @@ static void hfi_disable_instance(void *ptr) hfi_disable(); } +static void hfi_shutdown_instance(void *ptr) +{ + hfi_disable_if_enabled(); +} + static void hfi_syscore_resume(void *data) { /* This code runs only on the boot CPU. */ @@ -612,9 +632,35 @@ static int hfi_syscore_suspend(void *data) return 0; } +static void hfi_syscore_shutdown(void *data) +{ + struct hfi_instance *hfi_instance; + int i; + + /* + * Hardware keeps writing to the HFI table after a kexec, into memory + * that the next kernel owns. Disable HFI in every package, and keep it + * disabled: user space runs until machine_shutdown() stops the other + * CPUs, and kexec enables CPU hotplug again, so a new thermal netlink + * client or a CPU coming online could otherwise enable it. + */ + mutex_lock(&hfi_instance_lock); + hfi_shutdown_done = true; + for (i = 0; i < max_hfi_instances; i++) { + hfi_instance = &hfi_instances[i]; + if (cpumask_empty(hfi_instance->cpus)) + continue; + + smp_call_function_single(cpumask_any(hfi_instance->cpus), + hfi_shutdown_instance, hfi_instance, true); + } + mutex_unlock(&hfi_instance_lock); +} + static const struct syscore_ops hfi_pm_ops = { .resume = hfi_syscore_resume, .suspend = hfi_syscore_suspend, + .shutdown = hfi_syscore_shutdown, }; static struct syscore hfi_pm = { @@ -649,7 +695,7 @@ static int hfi_thermal_notify(struct notifier_block *nb, unsigned long state, break; } - if (!func) + if (!func || hfi_shutdown_done) goto out; for (i = 0; i < max_hfi_instances; i++) { -- 2.39.5 From: Jez Ng Since commit b33f3d2677b8 ("thermal: intel: hfi: Enable HFI only when required"), the kernel only programs HFI when a thermal netlink client subscribes to events. Before that, the first CPU of each package to come online wrote a fresh table address and enabled HFI, which replaced any setup a previous kernel left behind. Now, if the previous kernel left HFI enabled, for example across kexec, nothing touches it. Hardware keeps writing updates into the previous kernel's table, which is free memory in this kernel and can be allocated for anything. On dual-socket Sapphire Rapids systems kexec'ing from a 6.1 kernel into 7.0, these writes corrupted the inode hash table and caused oopses in find_inode_fast(). When the first CPU of a package comes online, check MSR_IA32_HW_FEEDBACK_CONFIG and disable HFI if it is already enabled. If a client subscribes later, HFI is re-enabled with this kernel's table as before. This narrows the window but cannot close it. Hardware may still write to the old table until the package's first CPU reaches the thermal online callback, which runs from a device initcall. On the systems above that was about 30 seconds after this kernel started, 21 of which were spent in acpi_init(). In testing there, hardware wrote the table once right after HFI was enabled and not again over several minutes, idle or with every CPU busy, but the rate under other loads is unknown. The complete fix is for the previous kernel to disable HFI on shutdown, as "thermal: intel: hfi: Disable HFI on shutdown and kexec" does; this covers previous kernels that do not. Also update the kernel-doc of intel_hfi_online(), which still described it as always enabling HFI. Fixes: b33f3d2677b8 ("thermal: intel: hfi: Enable HFI only when required") Cc: stable@vger.kernel.org Reviewed-by: Andrew James Reviewed-by: John Ranzie Signed-off-by: Jez Ng --- drivers/thermal/intel/intel_hfi.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/drivers/thermal/intel/intel_hfi.c b/drivers/thermal/intel/intel_hfi.c index 8ff094eccbaf..b97f8113930c 100644 --- a/drivers/thermal/intel/intel_hfi.c +++ b/drivers/thermal/intel/intel_hfi.c @@ -413,13 +413,14 @@ static bool hfi_disable_if_enabled(void) } /** - * intel_hfi_online() - Enable HFI on @cpu - * @cpu: CPU in which the HFI will be enabled + * intel_hfi_online() - Set up HFI on @cpu + * @cpu: CPU that is coming online * - * Enable the HFI to be used in @cpu. The HFI is enabled at the package - * level. The first CPU in the package to come online does the full HFI - * initialization. Subsequent CPUs will just link themselves to the HFI - * instance of their package. + * The HFI is enabled at the package level. The first CPU in the package to + * come online does the full HFI initialization, which includes disabling HFI + * if a previous kernel left it enabled. HFI is enabled only when there are + * thermal netlink clients. Subsequent CPUs will just link themselves to the + * HFI instance of their package. * * This function is called before enabling the thermal vector in the local APIC * in order to ensure that @cpu has an associated HFI instance when it receives @@ -462,6 +463,14 @@ void intel_hfi_online(unsigned int cpu) if (hfi_instance->hdr) goto enable; + /* + * A previous kernel may have left HFI enabled across kexec. Hardware + * would keep writing to that kernel's table, in memory this kernel now + * owns. + */ + if (hfi_disable_if_enabled()) + pr_info("HFI left enabled on package %u, disabled\n", pkg_id); + /* * Hardware is programmed with the physical address of the first page * frame of the table. Hence, the allocated memory must be page-aligned. -- 2.39.5