x86/tdx: Add kexec support
Subsystems (10)
v2 cover x86/tdx: Add kexec support
The patchset adds bits and pieces to get kexec (and crashkernel) work on TDX guest.
The last patch implements CPU offlining according to the approved ACPI spec change poposal[1]. It unlocks kexec with all CPUs visible in the target kernel. It requires BIOS-side enabling. If it missing we fallback to booting 2nd kernel with single CPU.
Please review. I would be glad for any feedback.
v2:
- Rework how unsharing hook ups into kexec codepath;
- Rework kvmclock_disable() fix based on Sean's;
- s/cpu_hotplug_not_supported()/cpu_hotplug_disable_offlining()/;
- use play_dead_common() to implement acpi_mp_play_dead();
- cond_resched() in tdx_shared_memory_show();
- s/target kernel/second kernel/;
- Update commit messages and comments;
[1] https://lore.kernel.org/all/13356251.uLZWGnKmhe@kreacher
Kirill A. Shutemov (13):
x86/acpi: Extract ACPI MADT wakeup code into a separate file kernel/cpu: Add support for declaring CPU offlining not supported cpu/hotplug, x86/acpi: Disable CPU offlining for ACPI MADT wakeup x86/kvm: Do not try to disable kvmclock if it was not enabled x86/kexec: Keep CR4.MCE set during kexec for TDX guest x86/mm: Make x86_platform.guest.enc_status_change_*() return errno x86/mm: Return correct level from lookup_address() if pte is none x86/tdx: Account shared memory
x86/tdx: Convert shared memory back to private on kexec x86/mm: Make e820_end_ram_pfn() cover E820_TYPE_ACPI ranges x86/acpi: Do not attempt to bring up secondary CPUs in kexec case x86/acpi: Rename fields in acpi_madt_multiproc_wakeup structure x86/acpi: Add support for CPU offlining for ACPI MADT wakeup method
arch/x86/Kconfig | 7 + arch/x86/coco/core.c | 1 - arch/x86/coco/tdx/kexec.c | 0 arch/x86/coco/tdx/tdx.c | 205 +++++++++++++++++++- arch/x86/hyperv/ivm.c | 9 +- arch/x86/include/asm/acpi.h | 5 + arch/x86/include/asm/pgtable_types.h | 1 + arch/x86/include/asm/x86_init.h | 5 +- arch/x86/kernel/acpi/Makefile | 11 +- arch/x86/kernel/acpi/boot.c | 88 +-------- arch/x86/kernel/acpi/madt.S | 24 +++ arch/x86/kernel/acpi/madt_wakeup.c | 267 +++++++++++++++++++++++++++ arch/x86/kernel/crash.c | 4 + arch/x86/kernel/e820.c | 9 +- arch/x86/kernel/kvmclock.c | 12 +- arch/x86/kernel/reboot.c | 5 + arch/x86/kernel/relocate_kernel_64.S | 5 + arch/x86/kernel/x86_init.c | 4 +- arch/x86/mm/mem_encrypt_amd.c | 8 +- arch/x86/mm/pat/set_memory.c | 17 +- include/acpi/actbl2.h | 19 +- include/linux/cc_platform.h | 10 - include/linux/cpu.h | 2 + kernel/cpu.c | 12 +- 24 files changed, 586 insertions(+), 144 deletions(-) create mode 100644 arch/x86/coco/tdx/kexec.c create mode 100644 arch/x86/kernel/acpi/madt.S create mode 100644 arch/x86/kernel/acpi/madt_wakeup.c
Signature · 1 line
2.41.0
PATCH v2 1/13 x86/acpi: Extract ACPI MADT wakeup code into a separate file
In order to prepare for the expansion of support for the ACPI MADT wakeup method, move the relevant code into a separate file.
Introduce a new configuration option to clearly indicate dependencies without the use of ifdefs.
There have been no functional changes.
--- arch/x86/Kconfig | 7 +++ arch/x86/include/asm/acpi.h | 5 ++ arch/x86/kernel/acpi/Makefile | 11 ++-- arch/x86/kernel/acpi/boot.c | 86 +----------------------------- arch/x86/kernel/acpi/madt_wakeup.c | 81 ++++++++++++++++++++++++++++ 5 files changed, 100 insertions(+), 90 deletions(-) create mode 100644 arch/x86/kernel/acpi/madt_wakeup.c
arch/x86/Kconfig +7 -0
@@ -1108,6 +1108,13 @@ config X86_LOCAL_APIC depends on X86_64 || SMP || X86_32_NON_STANDARD || X86_UP_APIC || PCI_MSI select IRQ_DOMAIN_HIERARCHY +config X86_ACPI_MADT_WAKEUP + def_bool y + depends on X86_64 + depends on ACPI + depends on SMP + depends on X86_LOCAL_APIC + config X86_IO_APIC def_bool y depends on X86_LOCAL_APIC || X86_UP_IOAPIC
arch/x86/include/asm/acpi.h +5 -0
@@ -73,6 +73,11 @@ static inline bool acpi_skip_set_wakeup_address(void)#define acpi_skip_set_wakeup_address acpi_skip_set_wakeup_address +union acpi_subtable_headers ; + +int __init acpi_parse_mp_wake (union acpi_subtable_headers *header, +const unsigned long end); +/* * Check if the CPU can handle C2 and deeper */
arch/x86/kernel/acpi/Makefile +6 -5
@@ -1,11 +1,12 @@ # SPDX-License-Identifier: GPL-2.0 -obj-$(CONFIG_ACPI) += boot.o -obj-$(CONFIG_ACPI_SLEEP) += sleep.o wakeup_$(BITS).o -obj-$(CONFIG_ACPI_APEI) += apei.o -obj-$(CONFIG_ACPI_CPPC_LIB) += cppc.o +obj-$(CONFIG_ACPI) += boot.o +obj-$(CONFIG_ACPI_SLEEP) += sleep.o wakeup_$(BITS).o +obj-$(CONFIG_ACPI_APEI) += apei.o +obj-$(CONFIG_ACPI_CPPC_LIB) += cppc.o +obj-$(CONFIG_X86_ACPI_MADT_WAKEUP) += madt_wakeup.o ifneq ($(CONFIG_ACPI_PROCESSOR),) -obj-y += cstate.o +obj-y += cstate.o endif
arch/x86/kernel/acpi/boot.c +1 -85
110 lines not on this page · raw message
arch/x86/kernel/acpi/madt_wakeup.c new file +81 -0
81 lines not on this page · raw message
Signature · 1 line
2.41.0
Re: [PATCHv2 01/13] x86/acpi: Extract ACPI MADT wakeup code into a separate file
Quoted 10 lines · Kirill A. Shutemov
In order to prepare for the expansion of support for the ACPI MADT wakeup method, move the relevant code into a separate file.
Introduce a new configuration option to clearly indicate dependencies without the use of ifdefs.
There have been no functional changes.
Signed-off-by Kirill A. Shutemov <[email protected]>---
Looks good to me.
Quoted 269 lines
arch/x86/Kconfig | 7 +++ arch/x86/include/asm/acpi.h | 5 ++ arch/x86/kernel/acpi/Makefile | 11 ++-- arch/x86/kernel/acpi/boot.c | 86 +----------------------------- arch/x86/kernel/acpi/madt_wakeup.c | 81 ++++++++++++++++++++++++++++ 5 files changed, 100 insertions(+), 90 deletions(-) create mode 100644 arch/x86/kernel/acpi/madt_wakeup.carch/x86/Kconfig +7
-0@@ -1108,6 +1108,13 @@ config X86_LOCAL_APIC depends on X86_64 || SMP || X86_32_NON_STANDARD || X86_UP_APIC || PCI_MSI select IRQ_DOMAIN_HIERARCHY +config X86_ACPI_MADT_WAKEUP + def_bool y + depends on X86_64 + depends on ACPI + depends on SMP + depends on X86_LOCAL_APIC + config X86_IO_APIC def_bool y depends on X86_LOCAL_APIC || X86_UP_IOAPICarch/x86/include/asm/acpi.h +5
-0@@ -73,6 +73,11 @@ static inline bool acpi_skip_set_wakeup_address(void)#define acpi_skip_set_wakeup_address acpi_skip_set_wakeup_address +union acpi_subtable_headers ; + +int __init acpi_parse_mp_wake (union acpi_subtable_headers *header, +const unsigned long end); +/* * Check if the CPU can handle C2 and deeper */ arch/x86/kernel/acpi/Makefile +6
-5@@ -1,11 +1,12 @@ # SPDX-License-Identifier: GPL-2.0 -obj-$(CONFIG_ACPI) += boot.o -obj-$(CONFIG_ACPI_SLEEP) += sleep.o wakeup_$(BITS).o -obj-$(CONFIG_ACPI_APEI) += apei.o -obj-$(CONFIG_ACPI_CPPC_LIB) += cppc.o +obj-$(CONFIG_ACPI) += boot.o +obj-$(CONFIG_ACPI_SLEEP) += sleep.o wakeup_$(BITS).o +obj-$(CONFIG_ACPI_APEI) += apei.o +obj-$(CONFIG_ACPI_CPPC_LIB) += cppc.o +obj-$(CONFIG_X86_ACPI_MADT_WAKEUP) += madt_wakeup.o ifneq ($(CONFIG_ACPI_PROCESSOR),) -obj-y += cstate.o +obj-y += cstate.o endifarch/x86/kernel/acpi/boot.c +1
-85@@ -66,13 +66,6 @@ static u64 acpi_lapic_addr __initdata = APIC_DEFAULT_PHYS_BASE;static bool acpi_support_online_capable;#endif -#ifdef CONFIG_X86_64 -/* Physical address of the Multiprocessor Wakeup Structure mailbox */ -static u64 acpi_mp_wake_mailbox_paddr; -/* Virtual address of the Multiprocessor Wakeup Structure mailbox */ -static struct acpi_madt_multiproc_wakeup_mailbox *acpi_mp_wake_mailbox; -#endif -#ifdef CONFIG_X86_IO_APIC /* * Locks related to IOAPIC hotplug @@ -357,60 +350,6 @@ acpi_parse_lapic_nmi(union acpi_subtable_headers * header, const unsigned long e return 0; } - -#ifdef CONFIG_X86_64 -static int acpi_wakeup_cpu (int apicid,unsigned long start_ip) -{ -/* -* Remap mailbox memory only for the first call to acpi_wakeup_cpu(). -* -* Wakeup of secondary CPUs is fully serialized in the core code. -* No need to protect acpi_mp_wake_mailbox from concurrent accesses. -*/ -if (!acpi_mp_wake_mailbox) { - acpi_mp_wake_mailbox =memremap (acpi_mp_wake_mailbox_paddr, -sizeof (*acpi_mp_wake_mailbox), -MEMREMAP_WB ); - } - -/* -* Mailbox memory is shared between the firmware and OS. Firmware will -* listen on mailbox command address, and once it receives the wakeup -* command, the CPU associated with the given apicid will be booted. -* -* The value of 'apic_id' and 'wakeup_vector' must be visible to the -* firmware before the wakeup command is visible. smp_store_release() -* ensures ordering and visibility. -*/ - acpi_mp_wake_mailbox->apic_id = apicid; - acpi_mp_wake_mailbox->wakeup_vector = start_ip; -smp_store_release (&acpi_mp_wake_mailbox->command, -ACPI_MP_WAKE_COMMAND_WAKEUP ); - -/* -* Wait for the CPU to wake up. -* -* The CPU being woken up is essentially in a spin loop waiting to be -* woken up. It should not take long for it wake up and acknowledge by -* zeroing out ->command. -* -* ACPI specification doesn't provide any guidance on how long kernel -* has to wait for a wake up acknowledgement. It also doesn't provide -* a way to cancel a wake up request if it takes too long. -* -* In TDX environment, the VMM has control over how long it takes to -* wake up secondary. It can postpone scheduling secondary vCPU -* indefinitely. Giving up on wake up request and reporting error opens -* possible attack vector for VMM: it can wake up a secondary CPU when -* kernel doesn't expect it. Wait until positive result of the wake up -* request. -*/ -while (READ_ONCE (acpi_mp_wake_mailbox->command)) -cpu_relax (); - -return 0 ; -} -#endif /* CONFIG_X86_64 */ #endif /* CONFIG_X86_LOCAL_APIC */ #ifdef CONFIG_X86_IO_APIC @@ -1160,29 +1099,6 @@ static int __init acpi_parse_madt_lapic_entries(void) }return 0 ; } - -#ifdef CONFIG_X86_64 -static int __init acpi_parse_mp_wake (union acpi_subtable_headers *header, -const unsigned long end) -{ -struct acpi_madt_multiproc_wakeup *mp_wake; - -if (!IS_ENABLED (CONFIG_SMP )) -return -ENODEV ; - - mp_wake = (struct acpi_madt_multiproc_wakeup *)header; -if (BAD_MADT_ENTRY (mp_wake, end)) -return -EINVAL ; - -acpi_table_print_madt_entry (&header->common); - - acpi_mp_wake_mailbox_paddr = mp_wake->base_address; - -apic_update_callback (wakeup_secondary_cpu_64, acpi_wakeup_cpu); - -return 0 ; -} -#endif /* CONFIG_X86_64 */ #endif /* CONFIG_X86_LOCAL_APIC */ #ifdef CONFIG_X86_IO_APIC @@ -1379,7 +1295,7 @@ static void __init acpi_process_madt(void) smp_found_config =1 ; } -#ifdef CONFIG_X86_64 +#ifdef CONFIG_X86_ACPI_MADT_WAKEUP /* * Parse MADT MP Wake entry. */ arch/x86/kernel/acpi/madt_wakeup.c new file +81
-0@@ -0,0 +1,81 @@ +#include <linux/acpi.h> +#include <linux/io.h> +#include <asm/apic.h> +#include <asm/barrier.h> +#include <asm/processor.h> + +/* Physical address of the Multiprocessor Wakeup Structure mailbox */ +static u64 acpi_mp_wake_mailbox_paddr; + +/* Virtual address of the Multiprocessor Wakeup Structure mailbox */ +static struct acpi_madt_multiproc_wakeup_mailbox *acpi_mp_wake_mailbox; + +static int acpi_wakeup_cpu (int apicid,unsigned long start_ip) +{ +/* +* Remap mailbox memory only for the first call to acpi_wakeup_cpu(). +* +* Wakeup of secondary CPUs is fully serialized in the core code. +* No need to protect acpi_mp_wake_mailbox from concurrent accesses. +*/ +if (!acpi_mp_wake_mailbox) { + acpi_mp_wake_mailbox =memremap (acpi_mp_wake_mailbox_paddr, +sizeof (*acpi_mp_wake_mailbox), +MEMREMAP_WB ); + } + +/* +* Mailbox memory is shared between the firmware and OS. Firmware will +* listen on mailbox command address, and once it receives the wakeup +* command, the CPU associated with the given apicid will be booted. +* +* The value of 'apic_id' and 'wakeup_vector' must be visible to the +* firmware before the wakeup command is visible. smp_store_release() +* ensures ordering and visibility. +*/ + acpi_mp_wake_mailbox->apic_id = apicid; + acpi_mp_wake_mailbox->wakeup_vector = start_ip; +smp_store_release (&acpi_mp_wake_mailbox->command, +ACPI_MP_WAKE_COMMAND_WAKEUP ); + +/* +* Wait for the CPU to wake up. +* +* The CPU being woken up is essentially in a spin loop waiting to be +* woken up. It should not take long for it wake up and acknowledge by +* zeroing out ->command. +* +* ACPI specification doesn't provide any guidance on how long kernel +* has to wait for a wake up acknowledgement. It also doesn't provide +* a way to cancel a wake up request if it takes too long. +* +* In TDX environment, the VMM has control over how long it takes to +* wake up secondary. It can postpone scheduling secondary vCPU +* indefinitely. Giving up on wake up request and reporting error opens +* possible attack vector for VMM: it can wake up a secondary CPU when +* kernel doesn't expect it. Wait until positive result of the wake up +* request. +*/ +while (READ_ONCE (acpi_mp_wake_mailbox->command)) +cpu_relax (); + +return 0 ; +} + +int __init acpi_parse_mp_wake (union acpi_subtable_headers *header, +const unsigned long end) +{ +struct acpi_madt_multiproc_wakeup *mp_wake; + + mp_wake = (struct acpi_madt_multiproc_wakeup *)header; +if (BAD_MADT_ENTRY (mp_wake, end)) +return -EINVAL ; + +acpi_table_print_madt_entry (&header->common); + + acpi_mp_wake_mailbox_paddr = mp_wake->base_address; + +apic_update_callback (wakeup_secondary_cpu_64, acpi_wakeup_cpu); + +return 0 ; +}
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
PATCH v2 2/13 kernel/cpu: Add support for declaring CPU offlining not supported
In reply to Kirill A. ShutemovACPI MADT doesn't allow to offline CPU after it got woke up.
Currently offlining hotplug prevented based on the confidential computing attribute which is set for Intel TDX. But TDX is not the only possible user of the wake up method.
Introduce cpu_hotplug_not_supported() that can be called to indicate that CPU offlining should be disabled.
This function is going to replace CC_ATTR_HOTPLUG_DISABLED for ACPI MADT.
--- include/linux/cpu.h | 2 ++ kernel/cpu.c | 13 ++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-)
include/linux/cpu.h +2 -0
@@ -132,6 +132,7 @@ extern void cpus_read_lock(void);extern void cpus_read_unlock (void );extern int cpus_read_trylock (void );extern void lockdep_assert_cpus_held (void ); +extern void cpu_hotplug_disable_offlining (void );extern void cpu_hotplug_disable (void );extern void cpu_hotplug_enable (void );void clear_tasks_mm_cpumask (int cpu);
@@ -147,6 +148,7 @@ static inline void cpus_read_lock(void) { }static inline void cpus_read_unlock (void ) { }static inline int cpus_read_trylock (void ) {return true; }static inline void lockdep_assert_cpus_held (void ) { } +static inline void cpu_hotplug_disable_offlining (void ) { }static inline void cpu_hotplug_disable (void ) { }static inline void cpu_hotplug_enable (void ) { }static inline int remove_cpu (unsigned int cpu) {return -EPERM ; }
kernel/cpu.c +12 -1
@@ -484,6 +484,8 @@ static int cpu_hotplug_disabled;DEFINE_STATIC_PERCPU_RWSEM (cpu_hotplug_lock); +static bool cpu_hotplug_offline_disabled; +void cpus_read_lock (void ) {percpu_down_read (&cpu_hotplug_lock);
@@ -543,6 +545,14 @@ static void lockdep_release_cpus_lock(void)rwsem_release (&cpu_hotplug_lock.dep_map, _THIS_IP_); } +/* Declare CPU offlining not supported */ +void cpu_hotplug_disable_offlining (void ) +{ +cpu_maps_update_begin (); + cpu_hotplug_offline_disabled = true; +cpu_maps_update_done (); +} +/* * Wait for currently running CPU hotplug operations to complete (if any) and * disable future CPU hotplug (from sysfs). The 'cpu_add_remove_lock' protects
@@ -1507,7 +1517,8 @@ static int cpu_down_maps_locked(unsigned int cpu, enum cpuhp_state target)* If the platform does not support hotplug, report it explicitly to * differentiate it from a transient offlining failure. */ -if (cc_platform_has (CC_ATTR_HOTPLUG_DISABLED )) +if (cc_platform_has (CC_ATTR_HOTPLUG_DISABLED ) || + cpu_hotplug_offline_disabled)return -EOPNOTSUPP ;if (cpu_hotplug_disabled)return -EBUSY ;
Signature · 1 line
2.41.0
Re: [PATCHv2 02/13] kernel/cpu: Add support for declaring CPU offlining not supported
Quoted 67 lines · Kirill A. Shutemov
ACPI MADT doesn't allow to offline CPU after it got woke up.
Currently offlining hotplug prevented based on the confidential computing attribute which is set for Intel TDX. But TDX is not the only possible user of the wake up method.
Introduce
cpu_hotplug_not_supported()that can be called to indicate that CPU offlining should be disabled.This function is going to replace
CC_ATTR_HOTPLUG_DISABLEDfor ACPI MADT.Signed-off-by Kirill A. Shutemov <[email protected]>--- include/linux/cpu.h | 2 ++ kernel/cpu.c | 13 ++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-)include/linux/cpu.h +2
-0@@ -132,6 +132,7 @@ extern void cpus_read_lock(void);extern void cpus_read_unlock (void );extern int cpus_read_trylock (void );extern void lockdep_assert_cpus_held (void ); +extern void cpu_hotplug_disable_offlining (void );extern void cpu_hotplug_disable (void );extern void cpu_hotplug_enable (void );void clear_tasks_mm_cpumask (int cpu);@@ -147,6 +148,7 @@ static inline void cpus_read_lock(void) { }static inline void cpus_read_unlock (void ) { }static inline int cpus_read_trylock (void ) {return true; }static inline void lockdep_assert_cpus_held (void ) { } +static inline void cpu_hotplug_disable_offlining (void ) { }static inline void cpu_hotplug_disable (void ) { }static inline void cpu_hotplug_enable (void ) { }static inline int remove_cpu (unsigned int cpu) {return -EPERM ; }kernel/cpu.c +10
-0@@ -484,6 +484,8 @@ static int cpu_hotplug_disabled;DEFINE_STATIC_PERCPU_RWSEM (cpu_hotplug_lock); +static bool cpu_hotplug_offline_disabled; +void cpus_read_lock (void ) {percpu_down_read (&cpu_hotplug_lock);@@ -543,6 +545,14 @@ static void lockdep_release_cpus_lock(void)rwsem_release (&cpu_hotplug_lock.dep_map, _THIS_IP_); } +/* Declare CPU offlining not supported */ +void cpu_hotplug_disable_offlining (void ) +{ +cpu_maps_update_begin (); + cpu_hotplug_offline_disabled = true; +cpu_maps_update_done (); +} +/* * Wait for currently running CPU hotplug operations to complete (if any) and * disable future CPU hotplug (from sysfs). The 'cpu_add_remove_lock' protects
kernel/cpu.c static int cpu_down_maps_locked(unsigned int cpu, enum cpuhp_state target) · line 1524
Quoted 9 lines
kernel/cpu.c +2
-1@@ -1507,7 +1517,8 @@ static int cpu_down_maps_locked(unsigned int cpu, enum cpuhp_state target)* If the platform does not support hotplug, report it explicitly to * differentiate it from a transient offlining failure. */ -if (cc_platform_has (CC_ATTR_HOTPLUG_DISABLED )) +if (cc_platform_has (CC_ATTR_HOTPLUG_DISABLED ) || + cpu_hotplug_offline_disabled)return -EOPNOTSUPP ;if (cpu_hotplug_disabled)return -EBUSY ;
IMHO it's a little bit odd to have two mechanisms in place, even in this middle state patch. Is it better to completely replace CC_ATTR_HOTPLUG_DISABLED with the new cpu_hotplug_offline_disabled in this patch? You can explicitly call cpu_hotplug_disable_offlining() in tdx_early_init() so no functional change is done.
Or I am wondering why cannot just merge this and the next patch together, with a proper justification?
Btw, IMHO the changelog (this and next patch's) seems didn't explain the true reason to replace CC_ATTR_HOTPLUG_DISABLED.
Currently hotplug prevented based on the confidential computing attribute which is set for Intel TDX. But TDX is not the only possible user of the wake up method.
"TDX is not the only possible user of the wake up method" doesn't mean we need to replace CC_ATTR_HOTPLUG_DISABLED. E.g., other CoCo VM type can also select CC_ATTR_HOTPLUG_DISABLED if it uses MADT wake up method.
To me the true reason is the new MADT wake up version actually brings the support of offlining cpu, thus it's more suitable to decide whether the CoCo VM needs to disable CPU offline based on the MADT wake up version, but not the CC_* attributes that is determined by CoCo VM type.
Re: [PATCHv2 02/13] kernel/cpu: Add support for declaring CPU offlining not supported
Quoted 5 lines · Kai
IMHO it's a little bit odd to have two mechanisms in place, even in this middle state patch. Is it better to completely replace
CC_ATTR_HOTPLUG_DISABLEDwith the newcpu_hotplug_offline_disabledin this patch? You can explicitly callcpu_hotplug_disable_offlining()intdx_early_init()so no functional change is done.
I can. But I don't see how it makes a difference.
Quoted 2 lines
Or I am wondering why cannot just merge this and the next patch together, with a proper justification?
Because the very next thing reviewers would ask is to split them :P
Quoted 7 lines
Btw, IMHO the changelog (this and next patch's) seems didn't explain the true reason to replace
CC_ATTR_HOTPLUG_DISABLED.Currently hotplug prevented based on the confidential computing attribute which is set for Intel TDX. But TDX is not the only possible user of the wake up method.
Quoted 8 lines
"TDX is not the only possible user of the wake up method" doesn't mean we need to replace
CC_ATTR_HOTPLUG_DISABLED. E.g., other CoCo VM type can also selectCC_ATTR_HOTPLUG_DISABLEDif it uses MADT wake up method.To me the true reason is the new MADT wake up version actually brings the support of offlining cpu, thus it's more suitable to decide whether the CoCo VM needs to disable CPU offline based on the MADT wake up version, but not the CC_* attributes that is determined by CoCo VM type.
No. MADT is orthogonal to CoCo. It can be implemented outside of CoCo environment and CoCo platform can implement other wake up methods. It is not up to TDX/SEV/whatever to decide if offlining is supported. It is property of the wakeup method implemented on the platform.
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
Re: [PATCHv2 02/13] kernel/cpu: Add support for declaring CPU offlining not supported
Quoted 8 lines · On Mon, 2023-10-23 at 18:31 +0300, [email protected] wrote:
Quoted 5 lines · Kai
IMHO it's a little bit odd to have two mechanisms in place, even in this middle state patch. Is it better to completely replace
CC_ATTR_HOTPLUG_DISABLEDwith the newcpu_hotplug_offline_disabledin this patch? You can explicitly callcpu_hotplug_disable_offlining()intdx_early_init()so no functional change is done.I can. But I don't see how it makes a difference.
Personally I think this is better because it is odd to have two mechanisms in place even temporarily especially when we can avoid it. But no hard opinion. Up to you.
Quoted 22 lines
Quoted 3 lines
Or I am wondering why cannot just merge this and the next patch together, with a proper justification?
Because the very next thing reviewers would ask is to split them :P
Quoted 7 lines
Btw, IMHO the changelog (this and next patch's) seems didn't explain the true reason to replace
CC_ATTR_HOTPLUG_DISABLED.Currently hotplug prevented based on the confidential computing attribute which is set for Intel TDX. But TDX is not the only possible user of the wake up method.
Quoted 8 lines
"TDX is not the only possible user of the wake up method" doesn't mean we need to replace
CC_ATTR_HOTPLUG_DISABLED. E.g., other CoCo VM type can also selectCC_ATTR_HOTPLUG_DISABLEDif it uses MADT wake up method.To me the true reason is the new MADT wake up version actually brings the support of offlining cpu, thus it's more suitable to decide whether the CoCo VM needs to disable CPU offline based on the MADT wake up version, but not the CC_* attributes that is determined by CoCo VM type.
Quoted 5 lines
No. MADT is orthogonal to CoCo. It can be implemented outside of CoCo environment and CoCo platform can implement other wake up methods. It is not up to TDX/SEV/whatever to decide if offlining is supported. It is property of the wakeup method implemented on the platform.
Yeah sure. Can we put this to changelog to make it clearer? :-)
Re: [PATCHv2 02/13] kernel/cpu: Add support for declaring CPU offlining not supported
Quoted 8 lines · Kai Huang
Quoted 7 lines · On Mon, 2023-10-23 at 18:31 +0300, [email protected] wrote:
Quoted 5 lines · Kai
IMHO it's a little bit odd to have two mechanisms in place, even in this middle state patch. Is it better to completely replace
CC_ATTR_HOTPLUG_DISABLEDwith the newcpu_hotplug_offline_disabledin this patch? You can explicitly callcpu_hotplug_disable_offlining()intdx_early_init()so no functional change is done.
Quoted 5 lines
Quoted 1 line
I can. But I don't see how it makes a difference.
Personally I think this is better because it is odd to have two mechanisms in place even temporarily especially when we can avoid it. But no hard opinion. Up to you.
It's not at all better because having this clear split makes it simpler to review and ponder the individual changes.
Thanks,
tglx
Re: [PATCHv2 02/13] kernel/cpu: Add support for declaring CPU offlining not supported
In reply to Kirill A. ShutemovOn Fri, Oct 20 2023 at 18:12, Kirill A. Shutemov wrote:
Subject: cpu/hotplug: ...
Quoted 1 line
ACPI MADT doesn't allow to offline CPU after it got woke up.
ACPI MADT is a table ...
This is about the MADT mailbox wakeup method, right?
Quoted 1 line
Currently offlining hotplug prevented based on the confidential
is prevented
Quoted 5 lines
computing attribute which is set for Intel TDX. But TDX is not the only possible user of the wake up method.
Introduce
cpu_hotplug_not_supported()that can be called to indicate that CPU offlining should be disabled.
Otherwise the changes look good!
Re: [PATCHv2 02/13] kernel/cpu: Add support for declaring CPU offlining not supported
Quoted 14 lines · Thomas Gleixner
On Fri, Oct 20 2023 at 18:12, Kirill A. Shutemov wrote:
Subject: cpu/hotplug: ...
Quoted 1 line
ACPI MADT doesn't allow to offline CPU after it got woke up.
ACPI MADT is a table ...
This is about the MADT mailbox wakeup method, right?
Quoted 1 line
Currently offlining hotplug prevented based on the confidential
is prevented
Quoted 7 lines
Quoted 5 lines
computing attribute which is set for Intel TDX. But TDX is not the only possible user of the wake up method.
Introduce
cpu_hotplug_not_supported()that can be called to indicate that CPU offlining should be disabled.Otherwise the changes look good!
Thanks!
Thomas, could you take a look if the last patch in the series is sane?
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
PATCH v2 3/13 cpu/hotplug, x86/acpi: Disable CPU offlining for ACPI MADT wakeup
In reply to Kirill A. ShutemovACPI MADT doesn't allow to offline CPU after it got woke up.
Currently hotplug prevented based on the confidential computing attribute which is set for Intel TDX. But TDX is not the only possible user of the wake up method.
Disable CPU offlining on ACPI MADT wakeup enumeration.
--- arch/x86/coco/core.c | 1 - arch/x86/kernel/acpi/madt_wakeup.c | 3 +++ include/linux/cc_platform.h | 10 ---------- kernel/cpu.c | 3 +-- 4 files changed, 4 insertions(+), 13 deletions(-)
arch/x86/coco/core.c +0 -1
@@ -20,7 +20,6 @@ static bool noinstr intel_cc_platform_has(enum cc_attr attr) {switch (attr) {case CC_ATTR_GUEST_UNROLL_STRING_IO : -case CC_ATTR_HOTPLUG_DISABLED :case CC_ATTR_GUEST_MEM_ENCRYPT :case CC_ATTR_MEM_ENCRYPT :return true;
arch/x86/kernel/acpi/madt_wakeup.c +3 -0
@@ -1,4 +1,5 @@#include <linux/acpi.h> +#include <linux/cpu.h> #include <linux/io.h> #include <asm/apic.h> #include <asm/barrier.h>
@@ -75,6 +76,8 @@ int __init acpi_parse_mp_wake(union acpi_subtable_headers *header, acpi_mp_wake_mailbox_paddr = mp_wake->base_address; +cpu_hotplug_disable_offlining (); +apic_update_callback (wakeup_secondary_cpu_64, acpi_wakeup_cpu);return 0 ;
include/linux/cc_platform.h +0 -10
@@ -80,16 +80,6 @@ enum cc_attr {* using AMD SEV-SNP features. */ CC_ATTR_GUEST_SEV_SNP, - -/** -* @CC_ATTR_HOTPLUG_DISABLED: Hotplug is not supported or disabled. -* -* The platform/OS is running as a guest/virtual machine does not -* support CPU hotplug feature. -* -* Examples include TDX Guest. -*/ - CC_ATTR_HOTPLUG_DISABLED, };#ifdef CONFIG_ARCH_HAS_CC_PLATFORM
kernel/cpu.c +1 -2
@@ -1517,8 +1517,7 @@ static int cpu_down_maps_locked(unsigned int cpu, enum cpuhp_state target)* If the platform does not support hotplug, report it explicitly to * differentiate it from a transient offlining failure. */ -if (cc_platform_has (CC_ATTR_HOTPLUG_DISABLED ) || - cpu_hotplug_offline_disabled) +if (cpu_hotplug_offline_disabled)return -EOPNOTSUPP ;if (cpu_hotplug_disabled)return -EBUSY ;
Signature · 1 line
2.41.0
PATCH v2 4/13 x86/kvm: Do not try to disable kvmclock if it was not enabled
In reply to Kirill A. Shutemovkvm_guest_cpu_offline() tries to disable kvmclock regardless if it is present in the VM. It leads to write to a MSR that doesn't exist on some configurations, namely in TDX guest:
unchecked MSR access error: WRMSR to 0x12 (tried to write 0x0000000000000000) at rIP: 0xffffffff8110687c (kvmclock_disable+0x1c/0x30)
kvmclock enabling is gated by CLOCKSOURCE and CLOCKSOURCE2 KVM paravirt features.
Do not disable kvmclock if it was not enabled.
--- arch/x86/kernel/kvmclock.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
arch/x86/kernel/kvmclock.c +8 -4
@@ -24,8 +24,8 @@static int kvmclock__initdata =1 ;static int kvmclock_vsyscall__initdata =1 ; -static int msr_kvm_system_time__ro_after_init =MSR_KVM_SYSTEM_TIME ; -static int msr_kvm_wall_clock__ro_after_init =MSR_KVM_WALL_CLOCK ; +static int msr_kvm_system_time__ro_after_init ; +static int msr_kvm_wall_clock__ro_after_init ;static u64 kvm_sched_clock_offset__ro_after_init ;static int __init parse_no_kvmclock (char *arg)
@@ -195,7 +195,8 @@ static void kvm_setup_secondary_clock(void)void kvmclock_disable (void ) { -native_write_msr (msr_kvm_system_time,0 ,0 ); +if (msr_kvm_system_time) +native_write_msr (msr_kvm_system_time,0 ,0 ); }static void __init kvmclock_init_mem (void )
@@ -294,7 +295,10 @@ void __init kvmclock_init(void)if (kvm_para_has_feature (KVM_FEATURE_CLOCKSOURCE2 )) { msr_kvm_system_time =MSR_KVM_SYSTEM_TIME_NEW ; msr_kvm_wall_clock =MSR_KVM_WALL_CLOCK_NEW ; - }else if (!kvm_para_has_feature (KVM_FEATURE_CLOCKSOURCE )) { + }else if (kvm_para_has_feature (KVM_FEATURE_CLOCKSOURCE )) { + msr_kvm_system_time =MSR_KVM_SYSTEM_TIME ; + msr_kvm_wall_clock =MSR_KVM_WALL_CLOCK ; + }else {return ; }
Signature · 1 line
2.41.0
Re: [PATCHv2 04/13] x86/kvm: Do not try to disable kvmclock if it was not enabled
Quoted 12 lines · Kirill A. Shutemov
kvm_guest_cpu_offline()tries to disable kvmclock regardless if it is present in the VM. It leads to write to a MSR that doesn't exist on some configurations, namely in TDX guest:unchecked MSR access error: WRMSR to 0x12 (tried to write 0x0000000000000000) at rIP: 0xffffffff8110687c (kvmclock_disable+0x1c/0x30)kvmclock enabling is gated by CLOCKSOURCE and CLOCKSOURCE2 KVM paravirt features.
Do not disable kvmclock if it was not enabled.
Quoted 7 lines
Signed-off-by Kirill A. Shutemov <[email protected]>Fixes c02027b5742b ("x86/kvm: Disable kvmclock on all CPUs on shutdown")Cc Paolo Bonzini <[email protected]>Cc Wanpeng Li <[email protected]>Cc Vitaly Kuznetsov <[email protected]>Cc Sean Christopherson <[email protected]>---
Re: [PATCHv2 04/13] x86/kvm: Do not try to disable kvmclock if it was not enabled
In reply to Kirill A. ShutemovQuoted 48 lines · Kirill A. Shutemov
kvm_guest_cpu_offline()tries to disable kvmclock regardless if it is present in the VM. It leads to write to a MSR that doesn't exist on some configurations, namely in TDX guest:unchecked MSR access error: WRMSR to 0x12 (tried to write 0x0000000000000000) at rIP: 0xffffffff8110687c (kvmclock_disable+0x1c/0x30)kvmclock enabling is gated by CLOCKSOURCE and CLOCKSOURCE2 KVM paravirt features.
Do not disable kvmclock if it was not enabled.
Signed-off-by Kirill A. Shutemov <[email protected]>Fixes c02027b5742b ("x86/kvm: Disable kvmclock on all CPUs on shutdown")Cc Paolo Bonzini <[email protected]>Cc Wanpeng Li <[email protected]>Cc Vitaly Kuznetsov <[email protected]>Cc Sean Christopherson <[email protected]>--- arch/x86/kernel/kvmclock.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)arch/x86/kernel/kvmclock.c +4
-3@@ -24,8 +24,8 @@static int kvmclock__initdata =1 ;static int kvmclock_vsyscall__initdata =1 ; -static int msr_kvm_system_time__ro_after_init =MSR_KVM_SYSTEM_TIME ; -static int msr_kvm_wall_clock__ro_after_init =MSR_KVM_WALL_CLOCK ; +static int msr_kvm_system_time__ro_after_init ; +static int msr_kvm_wall_clock__ro_after_init ;static u64 kvm_sched_clock_offset__ro_after_init ;static int __init parse_no_kvmclock (char *arg)@@ -195,7 +195,8 @@ static void kvm_setup_secondary_clock(void)void kvmclock_disable (void ) { -native_write_msr (msr_kvm_system_time,0 ,0 ); +if (msr_kvm_system_time) +native_write_msr (msr_kvm_system_time,0 ,0 ); }static void __init kvmclock_init_mem (void )
arch/x86/kernel/kvmclock.c void __init kvmclock_init(void) · line 303
Quoted 10 lines
arch/x86/kernel/kvmclock.c +4
-1@@ -294,7 +295,10 @@ void __init kvmclock_init(void)if (kvm_para_has_feature (KVM_FEATURE_CLOCKSOURCE2 )) { msr_kvm_system_time =MSR_KVM_SYSTEM_TIME_NEW ; msr_kvm_wall_clock =MSR_KVM_WALL_CLOCK_NEW ; - }else if (!kvm_para_has_feature (KVM_FEATURE_CLOCKSOURCE )) { + }else if (kvm_para_has_feature (KVM_FEATURE_CLOCKSOURCE )) { + msr_kvm_system_time =MSR_KVM_SYSTEM_TIME ; + msr_kvm_wall_clock =MSR_KVM_WALL_CLOCK ; + }else {return ; }
This should work, so
but my personal preference would be to change kvm_guest_cpu_offline() to check KVM features explicitly instead of checking MSRs against '0' at least becase it already does so for other features. Completely untested:
arch/x86/kernel/kvm.c +3 -2
@@ -454,7 +454,9 @@ static void kvm_guest_cpu_offline(bool shutdown)kvm_pv_disable_apf ();if (!shutdown)apf_task_wake_all (); -kvmclock_disable (); +if (kvm_para_has_feature (KVM_FEATURE_CLOCKSOURCE2 ) || +kvm_para_has_feature (KVM_FEATURE_CLOCKSOURCE )) +kvmclock_disable (); } --
Vitaly
Re: [PATCHv2 04/13] x86/kvm: Do not try to disable kvmclock if it was not enabled
Quoted 55 lines · Vitaly Kuznetsov
Quoted 30 lines
--- arch/x86/kernel/kvmclock.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)arch/x86/kernel/kvmclock.c +4
-3@@ -24,8 +24,8 @@static int kvmclock__initdata =1 ;static int kvmclock_vsyscall__initdata =1 ; -static int msr_kvm_system_time__ro_after_init =MSR_KVM_SYSTEM_TIME ; -static int msr_kvm_wall_clock__ro_after_init =MSR_KVM_WALL_CLOCK ; +static int msr_kvm_system_time__ro_after_init ; +static int msr_kvm_wall_clock__ro_after_init ;static u64 kvm_sched_clock_offset__ro_after_init ;static int __init parse_no_kvmclock (char *arg)@@ -195,7 +195,8 @@ static void kvm_setup_secondary_clock(void)void kvmclock_disable (void ) { -native_write_msr (msr_kvm_system_time,0 ,0 ); +if (msr_kvm_system_time) +native_write_msr (msr_kvm_system_time,0 ,0 ); }static void __init kvmclock_init_mem (void )arch/x86/kernel/kvmclock.c void __init kvmclock_init(void) · line 303
Quoted 10 lines
arch/x86/kernel/kvmclock.c +4
-1@@ -294,7 +295,10 @@ void __init kvmclock_init(void)if (kvm_para_has_feature (KVM_FEATURE_CLOCKSOURCE2 )) { msr_kvm_system_time =MSR_KVM_SYSTEM_TIME_NEW ; msr_kvm_wall_clock =MSR_KVM_WALL_CLOCK_NEW ; - }else if (!kvm_para_has_feature (KVM_FEATURE_CLOCKSOURCE )) { + }else if (kvm_para_has_feature (KVM_FEATURE_CLOCKSOURCE )) { + msr_kvm_system_time =MSR_KVM_SYSTEM_TIME ; + msr_kvm_wall_clock =MSR_KVM_WALL_CLOCK ; + }else {return ; }This should work, so
Reviewed-by Vitaly Kuznetsov <[email protected]>but my personal preference would be to change
kvm_guest_cpu_offline()to check KVM features explicitly instead of checking MSRs against '0' at least becase it already does so for other features. Completely untested:arch/x86/kernel/kvm.c +0
-0
arch/x86/kernel/kvm.c static void kvm_guest_cpu_offline(bool shutdown) · line 460
Quoted 8 lines
arch/x86/kernel/kvm.c +3
-1@@ -454,7 +454,9 @@ static void kvm_guest_cpu_offline(bool shutdown)kvm_pv_disable_apf ();if (!shutdown)apf_task_wake_all (); -kvmclock_disable (); +if (kvm_para_has_feature (KVM_FEATURE_CLOCKSOURCE2 ) || +kvm_para_has_feature (KVM_FEATURE_CLOCKSOURCE )) +kvmclock_disable (); }
That would result in an unnecessray WRMSR in the case where kvmclock is disabled on the command line. It _should_ be benign given how the code is written, but it's not impossible to imagine a scenario where someone disabled kvmclock in the guest because of a hypervisor bug. And the WRMSR would become a bogus write to MSR 0x0 if someone made a "cleanup" to set msr_kvm_system_time if and only if kvmclock is actually used, e.g. if someone made Kirill's change sans the check in kvmclock_disable().
Vitaly Kuznetsov
reviewerRe: [PATCHv2 04/13] x86/kvm: Do not try to disable kvmclock if it was not enabled
Quoted 65 lines · Sean Christopherson
Quoted 55 lines · Vitaly Kuznetsov
Quoted 30 lines
--- arch/x86/kernel/kvmclock.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)arch/x86/kernel/kvmclock.c +4
-3@@ -24,8 +24,8 @@static int kvmclock__initdata =1 ;static int kvmclock_vsyscall__initdata =1 ; -static int msr_kvm_system_time__ro_after_init =MSR_KVM_SYSTEM_TIME ; -static int msr_kvm_wall_clock__ro_after_init =MSR_KVM_WALL_CLOCK ; +static int msr_kvm_system_time__ro_after_init ; +static int msr_kvm_wall_clock__ro_after_init ;static u64 kvm_sched_clock_offset__ro_after_init ;static int __init parse_no_kvmclock (char *arg)@@ -195,7 +195,8 @@ static void kvm_setup_secondary_clock(void)void kvmclock_disable (void ) { -native_write_msr (msr_kvm_system_time,0 ,0 ); +if (msr_kvm_system_time) +native_write_msr (msr_kvm_system_time,0 ,0 ); }static void __init kvmclock_init_mem (void )arch/x86/kernel/kvmclock.c void __init kvmclock_init(void) · line 303
Quoted 10 lines
arch/x86/kernel/kvmclock.c +4
-1@@ -294,7 +295,10 @@ void __init kvmclock_init(void)if (kvm_para_has_feature (KVM_FEATURE_CLOCKSOURCE2 )) { msr_kvm_system_time =MSR_KVM_SYSTEM_TIME_NEW ; msr_kvm_wall_clock =MSR_KVM_WALL_CLOCK_NEW ; - }else if (!kvm_para_has_feature (KVM_FEATURE_CLOCKSOURCE )) { + }else if (kvm_para_has_feature (KVM_FEATURE_CLOCKSOURCE )) { + msr_kvm_system_time =MSR_KVM_SYSTEM_TIME ; + msr_kvm_wall_clock =MSR_KVM_WALL_CLOCK ; + }else {return ; }This should work, so
Reviewed-by Vitaly Kuznetsov <[email protected]>but my personal preference would be to change
kvm_guest_cpu_offline()to check KVM features explicitly instead of checking MSRs against '0' at least becase it already does so for other features. Completely untested:arch/x86/kernel/kvm.c +0
-0arch/x86/kernel/kvm.c static void kvm_guest_cpu_offline(bool shutdown) · line 460
Quoted 8 lines
arch/x86/kernel/kvm.c +3
-1@@ -454,7 +454,9 @@ static void kvm_guest_cpu_offline(bool shutdown)kvm_pv_disable_apf ();if (!shutdown)apf_task_wake_all (); -kvmclock_disable (); +if (kvm_para_has_feature (KVM_FEATURE_CLOCKSOURCE2 ) || +kvm_para_has_feature (KVM_FEATURE_CLOCKSOURCE )) +kvmclock_disable (); }
Quoted 7 lines
That would result in an unnecessray WRMSR in the case where kvmclock is disabled on the command line. It _should_ be benign given how the code is written, but it's not impossible to imagine a scenario where someone disabled kvmclock in the guest because of a hypervisor bug. And the WRMSR would become a bogus write to MSR
0x0if someone made a "cleanup" to setmsr_kvm_system_timeif and only if kvmclock is actually used, e.g. if someone made Kirill's change sans the check inkvmclock_disable().
True but we don't have such module params to disable other PV features so e.g. KVM_FEATURE_PV_EOI/KVM_FEATURE_MIGRATION_CONTROL are written to unconditionally. Wouldn't it be better to handle parameters like 'no-kvmclock' by clearing the feature bit in kvm_arch_para_features()'s return value so all kvm_para_has_feature() calls for it just return 'false'? We can even do an umbreall "no-kvm-features=<mask>" to cover all possible debug cases.
Signature · 1 line
Vitaly
Re: [PATCHv2 04/13] x86/kvm: Do not try to disable kvmclock if it was not enabled
Quoted 25 lines · Vitaly Kuznetsov
Quoted 15 lines · Sean Christopherson
arch/x86/kernel/kvm.c static void kvm_guest_cpu_offline(bool shutdown) · line 460
Quoted 13 lines · Vitaly Kuznetsov
arch/x86/kernel/kvm.c +3
-1@@ -454,7 +454,9 @@ static void kvm_guest_cpu_offline(bool shutdown)kvm_pv_disable_apf ();if (!shutdown)apf_task_wake_all (); -kvmclock_disable (); +if (kvm_para_has_feature (KVM_FEATURE_CLOCKSOURCE2 ) || +kvm_para_has_feature (KVM_FEATURE_CLOCKSOURCE )) +kvmclock_disable (); }Quoted 7 lines
That would result in an unnecessray WRMSR in the case where kvmclock is disabled on the command line. It _should_ be benign given how the code is written, but it's not impossible to imagine a scenario where someone disabled kvmclock in the guest because of a hypervisor bug. And the WRMSR would become a bogus write to MSR
0x0if someone made a "cleanup" to setmsr_kvm_system_timeif and only if kvmclock is actually used, e.g. if someone made Kirill's change sans the check inkvmclock_disable().
Quoted 7 lines
True but we don't have such module params to disable other PV features so e.g. KVM_FEATURE_PV_EOI/KVM_FEATURE_MIGRATION_CONTROL are written to unconditionally. Wouldn't it be better to handle parameters like 'no-kvmclock' by clearing the feature bit in kvm_arch_para_features()'s return value so all
kvm_para_has_feature()calls for it just return 'false'? We can even do an umbreall "no-kvm-features=<mask>" to cover all possible debug cases.
I don't know that it's worth the effort, or that it'd even be a net positive.
Today, kvm_para_has_feature() goes through to CPUID every time, e.g. we'd have to add a small bit of infrastructure to snapshot and clear bits, or rework things to let kvm_para_has_feature peek at kvmclock.
And things like KVM_FEATURE_PV_TLB_FLUSH would be quite weird, e.g. we either end up leaving the feature bit set while returning "false" for pv_tlb_flush_supported(), or we'd clear the feature bit for a rather large number of conditions that don't really have anything to do with KVM_FEATURE_PV_TLB_FLUSH being available.
static bool pv_tlb_flush_supported(void)
{
return (kvm_para_has_feature(KVM_FEATURE_PV_TLB_FLUSH) &&
!kvm_para_has_hint(KVM_HINTS_REALTIME) &&
kvm_para_has_feature(KVM_FEATURE_STEAL_TIME) &&
!boot_cpu_has(X86_FEATURE_MWAIT) &&
(num_possible_cpus() != 1));}
PATCH v2 5/13 x86/kexec: Keep CR4.MCE set during kexec for TDX guest
In reply to Kirill A. ShutemovTDX guests are not allowed to clear CR4.MCE. Attempt to clear it leads to #VE.
Use alternatives to keep the flag during kexec for TDX guests.
The change doesn't affect non-TDX-guest environments.
--- arch/x86/kernel/relocate_kernel_64.S | 5 +++++ 1 file changed, 5 insertions(+)
arch/x86/kernel/relocate_kernel_64.S +5 -0
@@ -145,11 +145,16 @@ SYM_CODE_START_LOCAL_NOALIGN(identity_mapped) * Set cr4 to a known state: * - physical address extension enabled * - 5-level paging, if it was enabled before + * - Machine check exception on TDX guest. Clearing MCE is not allowed + * in TDX guests. */ movl $X86_CR4_PAE, %eax testq $X86_CR4_LA57, %r13 jz 1f orl $X86_CR4_LA57, %eax +1: + ALTERNATIVE "jmp 1f", "", X86_FEATURE_TDX_GUEST + orl $X86_CR4_MCE, %eax 1: movq %rax, %cr4
Signature · 1 line
2.41.0
PATCH v2 6/13 x86/mm: Make x86_platform.guest.enc_status_change_*() return errno
In reply to Kirill A. ShutemovTDX is going to have more than one reason to failenc_status_change_prepare().
Change the callback to return errno instead of assuming -EIO; enc_status_change_finish() changed too to keep the interface symmetric.
--- arch/x86/coco/tdx/tdx.c | 20 +++++++++++--------- arch/x86/hyperv/ivm.c | 9 +++------ arch/x86/include/asm/x86_init.h | 4 ++-- arch/x86/kernel/x86_init.c | 4 ++-- arch/x86/mm/mem_encrypt_amd.c | 8 ++++---- arch/x86/mm/pat/set_memory.c | 9 +++++---- 6 files changed, 27 insertions(+), 27 deletions(-)
arch/x86/coco/tdx/tdx.c +11 -9
@@ -776,28 +776,30 @@ static bool tdx_enc_status_changed(unsigned long vaddr, int numpages, bool enc)return true; } -static bool tdx_enc_status_change_prepare (unsigned long vaddr,int numpages, -bool enc) +static int tdx_enc_status_change_prepare (unsigned long vaddr,int numpages, +bool enc) {/* * Only handle shared->private conversion here. * See the comment in tdx_early_init(). */ -if (enc) -return tdx_enc_status_changed (vaddr, numpages, enc); -return true; +if (enc && !tdx_enc_status_changed (vaddr, numpages, enc)) +return -EIO ; + +return 0 ; } -static bool tdx_enc_status_change_finish (unsigned long vaddr,int numpages, +static int tdx_enc_status_change_finish (unsigned long vaddr,int numpages,bool enc) {/* * Only handle private->shared conversion here. * See the comment in tdx_early_init(). */ -if (!enc) -return tdx_enc_status_changed (vaddr, numpages, enc); -return true; +if (!enc && !tdx_enc_status_changed (vaddr, numpages, enc)) +return -EIO ; + +return 0 ; }void __init tdx_early_init (void )
arch/x86/hyperv/ivm.c +3 -6
@@ -510,13 +510,12 @@ static int hv_mark_gpa_visibility(u16 count, const u64 pfn[],* with host. This function works as wrap of hv_mark_gpa_visibility() * with memory base and size. */ -static bool hv_vtom_set_host_visibility (unsigned long kbuffer,int pagecount,bool enc) +static int hv_vtom_set_host_visibility (unsigned long kbuffer,int pagecount,bool enc) {enum hv_mem_host_visibility visibility = enc ?VMBUS_PAGE_NOT_VISIBLE :VMBUS_PAGE_VISIBLE_READ_WRITE ;u64 *pfn_array;int ret =0 ; -bool result = true;int i, pfn; pfn_array =kmalloc (HV_HYP_PAGE_SIZE ,GFP_KERNEL );
@@ -530,17 +529,15 @@ static bool hv_vtom_set_host_visibility(unsigned long kbuffer, int pagecount, boif (pfn ==HV_MAX_MODIFY_GPA_REP_COUNT || i == pagecount -1 ) { ret =hv_mark_gpa_visibility (pfn, pfn_array, visibility); -if (ret) { - result = false; +if (ret) goto err_free_pfn_array; - } pfn =0 ; } } err_free_pfn_array:kfree (pfn_array); -return result; +return ret; }static bool hv_vtom_tlb_flush_required (bool private)
arch/x86/include/asm/x86_init.h +2 -2
@@ -150,8 +150,8 @@ struct x86_init_acpi {* @enc_cache_flush_required Returns true if a cache flush is needed before changing page encryption status */ struct x86_guest { -bool (*enc_status_change_prepare)(unsigned long vaddr,int npages,bool enc); -bool (*enc_status_change_finish)(unsigned long vaddr,int npages,bool enc); +int (*enc_status_change_prepare)(unsigned long vaddr,int npages,bool enc); +int (*enc_status_change_finish)(unsigned long vaddr,int npages,bool enc);bool (*enc_tlb_flush_required)(bool enc);bool (*enc_cache_flush_required)(void ); };
arch/x86/kernel/x86_init.c +2 -2
@@ -131,8 +131,8 @@ struct x86_cpuinit_ops x86_cpuinit = {static void default_nmi_init (void ) { }; -static bool enc_status_change_prepare_noop (unsigned long vaddr,int npages,bool enc) {return true; } -static bool enc_status_change_finish_noop (unsigned long vaddr,int npages,bool enc) {return true; } +static int enc_status_change_prepare_noop (unsigned long vaddr,int npages,bool enc) {return 0 ; } +static int enc_status_change_finish_noop (unsigned long vaddr,int npages,bool enc) {return 0 ; }static bool enc_tlb_flush_required_noop (bool enc) {return false; }static bool enc_cache_flush_required_noop (void ) {return false; }static bool is_private_mmio_noop (u64 addr) {return false; }
arch/x86/mm/mem_encrypt_amd.c +4 -4
@@ -282,7 +282,7 @@ static void enc_dec_hypercall(unsigned long vaddr, unsigned long size, bool enc)#endif } -static bool amd_enc_status_change_prepare (unsigned long vaddr,int npages,bool enc) +static int amd_enc_status_change_prepare (unsigned long vaddr,int npages,bool enc) {/* * To maintain the security guarantees of SEV-SNP guests, make sure
@@ -291,11 +291,11 @@ static bool amd_enc_status_change_prepare(unsigned long vaddr, int npages, boolif (cc_platform_has (CC_ATTR_GUEST_SEV_SNP ) && !enc)snp_set_memory_shared (vaddr, npages); -return true; +return 0 ; }/* Return true unconditionally: return value doesn't matter for the SEV side */ -static bool amd_enc_status_change_finish (unsigned long vaddr,int npages,bool enc) +static int amd_enc_status_change_finish (unsigned long vaddr,int npages,bool enc) {/* * After memory is mapped encrypted in the page table, validate it
@@ -307,7 +307,7 @@ static bool amd_enc_status_change_finish(unsigned long vaddr, int npages, bool eif (!cc_platform_has (CC_ATTR_HOST_MEM_ENCRYPT ))enc_dec_hypercall (vaddr, npages <<PAGE_SHIFT , enc); -return true; +return 0 ; }static void __init __set_clr_pte_enc (pte_t *kpte,int level,bool enc)
arch/x86/mm/pat/set_memory.c +5 -4
@@ -2152,8 +2152,9 @@ static int __set_memory_enc_pgtable(unsigned long addr, int numpages, bool enc)cpa_flush (&cpa, x86_platform.guest.enc_cache_flush_required ());/* Notify hypervisor that we are about to set/clr encryption attribute. */ -if (!x86_platform.guest.enc_status_change_prepare (addr, numpages, enc)) -return -EIO ; + ret = x86_platform.guest.enc_status_change_prepare (addr, numpages, enc); +if (ret) +return ret; ret =__change_page_attr_set_clr (&cpa,1 );
@@ -2168,8 +2169,8 @@ static int __set_memory_enc_pgtable(unsigned long addr, int numpages, bool enc)/* Notify hypervisor that we have successfully set/clr encryption attribute. */ if (!ret) { -if (!x86_platform.guest.enc_status_change_finish (addr, numpages, enc)) - ret = -EIO ; + ret = x86_platform.guest.enc_status_change_finish (addr, + numpages, enc); }return ret;
Signature · 1 line
2.41.0
PATCH v2 7/13 x86/mm: Return correct level from lookup_address() if pte is none
In reply to Kirill A. Shutemovlookup_address() only returns correct page table level for the entry if the entry is not none.
Make the helper to always return correct 'level'. It allows to implement iterator over kernel page tables using lookup_address().
Add one more entry into enum pg_level to indicate size of VA covered by one PGD entry in 5-level paging mode.
--- arch/x86/include/asm/pgtable_types.h | 1 + arch/x86/mm/pat/set_memory.c | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-)
arch/x86/include/asm/pgtable_types.h +1 -0
@@ -548,6 +548,7 @@ enum pg_level {PG_LEVEL_2M ,PG_LEVEL_1G ,PG_LEVEL_512G , +PG_LEVEL_256T , PG_LEVEL_NUM };
arch/x86/mm/pat/set_memory.c +4 -4
@@ -666,32 +666,32 @@ pte_t *lookup_address_in_pgd(pgd_t *pgd, unsigned long address,pud_t *pud;pmd_t *pmd; - *level =PG_LEVEL_NONE ; + *level =PG_LEVEL_256T ;if (pgd_none (*pgd))return NULL ; + *level =PG_LEVEL_512G ; p4d =p4d_offset (pgd, address);if (p4d_none (*p4d))return NULL ; - *level =PG_LEVEL_512G ;if (p4d_large (*p4d) || !p4d_present (*p4d))return (pte_t *)p4d; + *level =PG_LEVEL_1G ; pud =pud_offset (p4d, address);if (pud_none (*pud))return NULL ; - *level =PG_LEVEL_1G ;if (pud_large (*pud) || !pud_present (*pud))return (pte_t *)pud; + *level =PG_LEVEL_2M ; pmd =pmd_offset (pud, address);if (pmd_none (*pmd))return NULL ; - *level =PG_LEVEL_2M ;if (pmd_large (*pmd) || !pmd_present (*pmd))return (pte_t *)pmd;
Signature · 1 line
2.41.0
PATCH v2 8/13 x86/tdx: Account shared memory
In reply to Kirill A. ShutemovThe kernel will convert all shared memory back to private during kexec. The direct mapping page tables will provide information on which memory is shared.
It is extremely important to convert all shared memory. If a page is missed, it will cause the second kernel to crash when it accesses it.
Keep track of the number of shared pages. This will allow for cross-checking against the shared information in the direct mapping and reporting if the shared bit is lost.
Include a debugfs interface that allows for the check to be performed at any point.
--- arch/x86/coco/tdx/tdx.c | 69 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+)
arch/x86/coco/tdx/tdx.c +69 -0
@@ -5,6 +5,7 @@#define pr_fmt (fmt) "tdx: " fmt#include <linux/cpufeature.h> +#include <linux/debugfs.h> #include <linux/export.h> #include <linux/io.h> #include <asm/coco.h>
@@ -37,6 +38,13 @@#define TDREPORT_SUBTYPE_0 0 +static atomic_long_t nr_shared; + +static inline bool pte_decrypted (pte_t pte) +{ +return cc_mkdec (pte_val (pte)) ==pte_val (pte); +} +/* Called from __tdx_hypercall() for unrecoverable failure */ noinstr void __noreturn __tdx_hypercall_failed(void) {
@@ -799,6 +807,11 @@ static int tdx_enc_status_change_finish(unsigned long vaddr, int numpages,if (!enc && !tdx_enc_status_changed (vaddr, numpages, enc))return -EIO ; +if (enc) +atomic_long_sub (numpages, &nr_shared); +else +atomic_long_add (numpages, &nr_shared); +return 0 ; }
@@ -874,3 +887,59 @@ void __init tdx_early_init(void)pr_info ("Guest detected\n" ); } + +#ifdef CONFIG_DEBUG_FS +static int tdx_shared_memory_show (struct seq_file *m,void *p) +{ +unsigned long addr, end; +unsigned long found =0 ; + + addr =PAGE_OFFSET ; + end =PAGE_OFFSET +get_max_mapped (); + +while (addr < end) { +unsigned long size; +unsigned int level; +pte_t *pte; + + pte =lookup_address (addr, &level); + size =page_level_size (level); + +if (pte &&pte_decrypted (*pte)) + found += size /PAGE_SIZE ; + + addr += size; + +cond_resched (); + } + +seq_printf (m,"Number of unshared pages in kernel page tables: %16lu\n" , + found); +seq_printf (m,"Number of pages accounted as unshared: %16ld\n" , +atomic_long_read (&nr_shared)); +return 0 ; +} + +static int tdx_shared_memory_open (struct inode *inode,struct file *file) +{ +return single_open (file, tdx_shared_memory_show,NULL ); +} + +static const struct file_operations tdx_shared_memory_fops = { + .open = tdx_shared_memory_open, + .read = seq_read, + .llseek = seq_lseek, + .release = single_release, +}; + +static __init int debug_tdx_shared_memory (void ) +{ +if (!cpu_feature_enabled (X86_FEATURE_TDX_GUEST )) +return 0 ; + +debugfs_create_file ("tdx_shared_memory" ,S_IRUSR , arch_debugfs_dir, +NULL , &tdx_shared_memory_fops); +return 0 ; +} +fs_initcall (debug_tdx_shared_memory); +#endif
Signature · 1 line
2.41.0
PATCH v2 9/13 x86/tdx: Convert shared memory back to private on kexec
In reply to Kirill A. ShutemovTDX guests allocate shared buffers to perform I/O. It is done by allocating pages normally from the buddy allocator and converting them to shared with set_memory_decrypted().
The second kernel has no idea what memory is converted this way. It only sees E820_TYPE_RAM.
Accessing shared memory via private mapping is fatal. It leads to unrecoverable TD exit.
On kexec walk direct mapping and convert all shared memory back to private. It makes all RAM private again and second kernel may use it normally.
--- arch/x86/coco/tdx/kexec.c | 0 arch/x86/coco/tdx/tdx.c | 120 +++++++++++++++++++++++++++++++- arch/x86/include/asm/x86_init.h | 1 + arch/x86/kernel/crash.c | 4 ++ arch/x86/kernel/reboot.c | 5 ++ 5 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 arch/x86/coco/tdx/kexec.c
arch/x86/coco/tdx/kexec.c new file +0 -0
arch/x86/coco/tdx/tdx.c +118 -2
179 lines not on this page · raw message
arch/x86/include/asm/x86_init.h +1 -0
@@ -154,6 +154,7 @@ struct x86_guest {int (*enc_status_change_finish)(unsigned long vaddr,int npages,bool enc);bool (*enc_tlb_flush_required)(bool enc);bool (*enc_cache_flush_required)(void ); +void (*enc_kexec_unshare_mem)(bool crash); };/**
arch/x86/kernel/crash.c +4 -0
@@ -40,6 +40,7 @@#include <asm/intel_pt.h> #include <asm/crash.h> #include <asm/cmdline.h> +#include <asm/tdx.h> /* Used while preparing memory map entries for second kernel */ struct crash_memmap_data {
@@ -107,6 +108,9 @@ void native_machine_crash_shutdown(struct pt_regs *regs)crash_smp_send_stop (); +if (cc_platform_has (CC_ATTR_GUEST_MEM_ENCRYPT )) + x86_platform.guest.enc_kexec_unshare_mem (true); +cpu_emergency_disable_virtualization ();/*
arch/x86/kernel/reboot.c +5 -0
@@ -12,6 +12,7 @@#include <linux/delay.h> #include <linux/objtool.h> #include <linux/pgtable.h> +#include <linux/kexec.h> #include <acpi/reboot.h> #include <asm/io.h> #include <asm/apic.h>
@@ -31,6 +32,7 @@#include <asm/realmode.h> #include <asm/x86_init.h> #include <asm/efi.h> +#include <asm/tdx.h> /* * Power off function, if any
@@ -716,6 +718,9 @@ static void native_machine_emergency_restart(void)void native_machine_shutdown (void ) { +if (cc_platform_has (CC_ATTR_GUEST_MEM_ENCRYPT ) && kexec_in_progress) + x86_platform.guest.enc_kexec_unshare_mem (false); +/* Stop the cpus and apics */ #ifdef CONFIG_X86_IO_APIC /*
Signature · 1 line
2.41.0
PATCH v2 10/13 x86/mm: Make e820_end_ram_pfn() cover E820_TYPE_ACPI ranges
In reply to Kirill A. Shutemove820__end_of_ram_pfn() is used to calculate max_pfn which, among other things, guides where direct mapping ends. Any memory above max_pfn is not going to be present in the direct mapping.
e820__end_of_ram_pfn() finds the end of the ram based on the highest E820_TYPE_RAM range. But it doesn't includes E820_TYPE_ACPI ranges into calculation.
Despite the name, E820_TYPE_ACPI covers not only ACPI data, but also EFI tables and might be required by kernel to function properly.
Usually the problem is hidden because there is some E820_TYPE_RAM memory above E820_TYPE_ACPI. But crashkernel only presents pre-allocated crash memory as E820_TYPE_RAM on boot. If the preallocated range is small, it can fit under the last E820_TYPE_ACPI range.
Modify e820__end_of_ram_pfn() and e820__end_of_low_ram_pfn() to cover E820_TYPE_ACPI memory.
The problem was discovered during debugging kexec for TDX guest. TDX guest uses E820_TYPE_ACPI to store the unaccepted memory bitmap and pass it between the kernels on kexec.
--- arch/x86/kernel/e820.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
arch/x86/kernel/e820.c +5 -4
@@ -827,7 +827,7 @@ u64 __init e820__memblock_alloc_reserved(u64 size, u64 align)/* * Find the highest page frame number we have available */ -static unsigned long __init e820_end_pfn (unsigned long limit_pfn,enum e820_type type) +static unsigned long __init e820_end_ram_pfn (unsigned long limit_pfn) {int i;unsigned long last_pfn =0 ;
@@ -838,7 +838,8 @@ static unsigned long __init e820_end_pfn(unsigned long limit_pfn, enum e820_typeunsigned long start_pfn;unsigned long end_pfn; -if (entry->type != type) +if (entry->type !=E820_TYPE_RAM && + entry->type !=E820_TYPE_ACPI )continue ; start_pfn = entry->addr >>PAGE_SHIFT ;
@@ -864,12 +865,12 @@ static unsigned long __init e820_end_pfn(unsigned long limit_pfn, enum e820_typeunsigned long __init e820__end_of_ram_pfn (void ) { -return e820_end_pfn (MAX_ARCH_PFN ,E820_TYPE_RAM ); +return e820_end_ram_pfn (MAX_ARCH_PFN ); }unsigned long __init e820__end_of_low_ram_pfn (void ) { -return e820_end_pfn (1UL << (32 -PAGE_SHIFT ),E820_TYPE_RAM ); +return e820_end_ram_pfn (1UL << (32 -PAGE_SHIFT )); }static void __init early_panic (char *msg)
Signature · 1 line
2.41.0
PATCH v2 11/13 x86/acpi: Do not attempt to bring up secondary CPUs in kexec case
In reply to Kirill A. ShutemovACPI MADT doesn't allow to offline CPU after it got woke up. It limits
kexec: the second kernel won't be able to use more than one CPU.
Now acpi_mp_wake_mailbox_paddr already has the mailbox address. The acpi_wakeup_cpu() will use it to bring up secondary cpus.
Zero out mailbox address in the ACPI MADT wakeup structure to indicate that the mailbox is not usable. This prevents the kexec()-ed kernel from reading a vaild mailbox, which in turn makes the kexec()-ed kernel only be able to use the boot CPU.
This is Linux-specific protocol and not reflected in ACPI spec.
Booting the second kernel with signle CPU is enough to cover the most common case for kexec -- kdump.
--- arch/x86/kernel/acpi/madt_wakeup.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)
arch/x86/kernel/acpi/madt_wakeup.c +22 -0
@@ -13,6 +13,11 @@ static struct acpi_madt_multiproc_wakeup_mailbox *acpi_mp_wake_mailbox;static int acpi_wakeup_cpu (int apicid,unsigned long start_ip) { +if (!acpi_mp_wake_mailbox_paddr) { +pr_warn_once ("No MADT mailbox: cannot bringup secondary CPUs. Booting with kexec?\n" ); +return -EOPNOTSUPP ; + } +/* * Remap mailbox memory only for the first call to acpi_wakeup_cpu(). *
@@ -78,6 +83,23 @@ int __init acpi_parse_mp_wake(union acpi_subtable_headers *header,cpu_hotplug_disable_offlining (); +/* +* ACPI MADT doesn't allow to offline CPU after it got woke up. +* It limits kexec: the second kernel won't be able to use more than +* one CPU. +* +* Now acpi_mp_wake_mailbox_paddr already has the mailbox address. +* The acpi_wakeup_cpu() will use it to bring up secondary cpus. +* +* Zero out mailbox address in the ACPI MADT wakeup structure to +* indicate that the mailbox is not usable. This prevents the +* kexec()-ed kernel from reading a vaild mailbox, which in turn +* makes the kexec()-ed kernel only be able to use the boot CPU. +* +* This is Linux-specific protocol and not reflected in ACPI spec. +*/ + mp_wake->base_address =0 ; +apic_update_callback (wakeup_secondary_cpu_64, acpi_wakeup_cpu);return 0 ;
Signature · 1 line
2.41.0
Re: [PATCHv2 11/13] x86/acpi: Do not attempt to bring up secondary CPUs in kexec case
Quoted 11 lines · Kirill A. Shutemov
ACPI MADT doesn't allow to offline CPU after it got woke up. It limits
kexec: the second kernel won't be able to use more than one CPU.Now
acpi_mp_wake_mailbox_paddralready has the mailbox address. Theacpi_wakeup_cpu()will use it to bring up secondary cpus.Zero out mailbox address in the ACPI MADT wakeup structure to indicate that the mailbox is not usable. This prevents the kexec()-ed kernel from reading a vaild mailbox, which in turn makes the kexec()-ed kernel only be able to use the boot CPU.
Quoted 6 lines
This is Linux-specific protocol and not reflected in ACPI spec.
Booting the second kernel with signle CPU is enough to cover the most common case for kexec -- kdump.
Signed-off-by Kirill A. Shutemov <[email protected]>
Re: [PATCHv2 11/13] x86/acpi: Do not attempt to bring up secondary CPUs in kexec case
In reply to Kirill A. ShutemovQuoted 47 lines · Kirill A. Shutemov
ACPI MADT doesn't allow to offline CPU after it got woke up. It limits
kexec: the second kernel won't be able to use more than one CPU.Now
acpi_mp_wake_mailbox_paddralready has the mailbox address. Theacpi_wakeup_cpu()will use it to bring up secondary cpus.Zero out mailbox address in the ACPI MADT wakeup structure to indicate that the mailbox is not usable. This prevents the kexec()-ed kernel from reading a vaild mailbox, which in turn makes the kexec()-ed kernel only be able to use the boot CPU.
This is Linux-specific protocol and not reflected in ACPI spec.
Booting the second kernel with signle CPU is enough to cover the most common case for kexec -- kdump.
Signed-off-by Kirill A. Shutemov <[email protected]>--- arch/x86/kernel/acpi/madt_wakeup.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)arch/x86/kernel/acpi/madt_wakeup.c +11
-0@@ -13,6 +13,11 @@ static struct acpi_madt_multiproc_wakeup_mailbox *acpi_mp_wake_mailbox;static int acpi_wakeup_cpu (int apicid,unsigned long start_ip) { +if (!acpi_mp_wake_mailbox_paddr) { +pr_warn_once ("No MADT mailbox: cannot bringup secondary CPUs. Booting with kexec?\n" ); +return -EOPNOTSUPP ; + } +/* * Remap mailbox memory only for the first call to acpi_wakeup_cpu(). * @@ -78,6 +83,23 @@ int __init acpi_parse_mp_wake(union acpi_subtable_headers *header,cpu_hotplug_disable_offlining (); +/* +* ACPI MADT doesn't allow to offline CPU after it got woke up. +* It limits kexec: the second kernel won't be able to use more than +* one CPU. +* +* Now acpi_mp_wake_mailbox_paddr already has the mailbox address.
arch/x86/kernel/acpi/madt_wakeup.c int __init acpi_parse_mp_wake(union acpi_subtable_headers *header, · line 101
Quoted 10 lines
arch/x86/kernel/acpi/madt_wakeup.c +10
-0+* The acpi_wakeup_cpu() will use it to bring up secondary cpus. +* +* Zero out mailbox address in the ACPI MADT wakeup structure to +* indicate that the mailbox is not usable. This prevents the +* kexec()-ed kernel from reading a vaild mailbox, which in turn +* makes the kexec()-ed kernel only be able to use the boot CPU. +* +* This is Linux-specific protocol and not reflected in ACPI spec. +*/ + mp_wake->base_address =0 ;
Is there any way to skip secondary CPU bring-up for kexec case instead of returning error in ->wakeup_secondary_cpu_64()?
Quoted 4 lines
arch/x86/kernel/acpi/madt_wakeup.c +1
-0+apic_update_callback (wakeup_secondary_cpu_64, acpi_wakeup_cpu);return 0 ;
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
Re: [PATCHv2 11/13] x86/acpi: Do not attempt to bring up secondary CPUs in kexec case
Quoted 55 lines · Kuppuswamy Sathyanarayanan
Quoted 52 lines · Kirill A. Shutemov
ACPI MADT doesn't allow to offline CPU after it got woke up. It limits
kexec: the second kernel won't be able to use more than one CPU.Now
acpi_mp_wake_mailbox_paddralready has the mailbox address. Theacpi_wakeup_cpu()will use it to bring up secondary cpus.Zero out mailbox address in the ACPI MADT wakeup structure to indicate that the mailbox is not usable. This prevents the kexec()-ed kernel from reading a vaild mailbox, which in turn makes the kexec()-ed kernel only be able to use the boot CPU.
This is Linux-specific protocol and not reflected in ACPI spec.
Booting the second kernel with signle CPU is enough to cover the most common case for kexec -- kdump.
Signed-off-by Kirill A. Shutemov <[email protected]>--- arch/x86/kernel/acpi/madt_wakeup.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)arch/x86/kernel/acpi/madt_wakeup.c +16
-0@@ -13,6 +13,11 @@ static struct acpi_madt_multiproc_wakeup_mailbox *acpi_mp_wake_mailbox;static int acpi_wakeup_cpu (int apicid,unsigned long start_ip) { +if (!acpi_mp_wake_mailbox_paddr) { +pr_warn_once ("No MADT mailbox: cannot bringup secondary CPUs. Booting with kexec?\n" ); +return -EOPNOTSUPP ; + } +/* * Remap mailbox memory only for the first call to acpi_wakeup_cpu(). * @@ -78,6 +83,23 @@ int __init acpi_parse_mp_wake(union acpi_subtable_headers *header,cpu_hotplug_disable_offlining (); +/* +* ACPI MADT doesn't allow to offline CPU after it got woke up. +* It limits kexec: the second kernel won't be able to use more than +* one CPU. +* +* Now acpi_mp_wake_mailbox_paddr already has the mailbox address. +* The acpi_wakeup_cpu() will use it to bring up secondary cpus. +* +* Zero out mailbox address in the ACPI MADT wakeup structure to +* indicate that the mailbox is not usable. This prevents the +* kexec()-ed kernel from reading a vaild mailbox, which in turn
Quoted 8 lines
arch/x86/kernel/acpi/madt_wakeup.c int __init acpi_parse_mp_wake(union acpi_subtable_headers *header, · line 101
Quoted 5 lines
arch/x86/kernel/acpi/madt_wakeup.c +5
-0+* makes the kexec()-ed kernel only be able to use the boot CPU. +* +* This is Linux-specific protocol and not reflected in ACPI spec. +*/ + mp_wake->base_address =0 ;Is there any way to skip secondary CPU bring-up for kexec case instead of returning error in ->wakeup_secondary_cpu_64()?
I have not found a cleaner way. Do you have ideas?
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
PATCH v2 12/13 x86/acpi: Rename fields in acpi_madt_multiproc_wakeup structure
In reply to Kirill A. ShutemovTo prepare for the addition of support for MADT wakeup structure version 1, it is necessary to provide more appropriate names for the fields in the structure.
The field 'mailbox_version' renamed as 'version'. This field signifies the version of the structure and the related protocols, rather than the version of the mailbox. This field has not been utilized in the code thus far.
The field 'base_address' renamed as 'mailbox_address' to clarify the kind of address it represents. In version 1, the structure includes the reset vector address. Clear and distinct naming helps to prevent any confusion.
--- arch/x86/kernel/acpi/madt_wakeup.c | 4 ++-- include/acpi/actbl2.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)
arch/x86/kernel/acpi/madt_wakeup.c +2 -2
@@ -79,7 +79,7 @@ int __init acpi_parse_mp_wake(union acpi_subtable_headers *header,acpi_table_print_madt_entry (&header->common); - acpi_mp_wake_mailbox_paddr = mp_wake->base_address; + acpi_mp_wake_mailbox_paddr = mp_wake->mailbox_address;cpu_hotplug_disable_offlining ();
@@ -98,7 +98,7 @@ int __init acpi_parse_mp_wake(union acpi_subtable_headers *header,* * This is Linux-specific protocol and not reflected in ACPI spec. */ - mp_wake->base_address =0 ; + mp_wake->mailbox_address =0 ;apic_update_callback (wakeup_secondary_cpu_64, acpi_wakeup_cpu);
include/acpi/actbl2.h +2 -2
@@ -1109,9 +1109,9 @@ struct acpi_madt_generic_translator {struct acpi_madt_multiproc_wakeup {struct acpi_subtable_header header; -u16 mailbox_version; +u16 version;u32 reserved;/* reserved - must be zero */ -u64 base_address; +u64 mailbox_address; };#define ACPI_MULTIPROC_WAKEUP_MB_OS_SIZE 2032
Signature · 1 line
2.41.0
Re: [PATCHv2 12/13] x86/acpi: Rename fields in acpi_madt_multiproc_wakeup structure
Quoted 48 lines · Kirill A. Shutemov
To prepare for the addition of support for MADT wakeup structure version 1, it is necessary to provide more appropriate names for the fields in the structure.
The field '
mailbox_version' renamed as 'version'. This field signifies the version of the structure and the related protocols, rather than the version of the mailbox. This field has not been utilized in the code thus far.The field '
base_address' renamed as 'mailbox_address' to clarify the kind of address it represents. In version 1, the structure includes the reset vector address. Clear and distinct naming helps to prevent any confusion.Signed-off-by Kirill A. Shutemov <[email protected]>--- arch/x86/kernel/acpi/madt_wakeup.c | 4 ++-- include/acpi/actbl2.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)arch/x86/kernel/acpi/madt_wakeup.c +2
-2@@ -79,7 +79,7 @@ int __init acpi_parse_mp_wake(union acpi_subtable_headers *header,acpi_table_print_madt_entry (&header->common); - acpi_mp_wake_mailbox_paddr = mp_wake->base_address; + acpi_mp_wake_mailbox_paddr = mp_wake->mailbox_address;cpu_hotplug_disable_offlining ();@@ -98,7 +98,7 @@ int __init acpi_parse_mp_wake(union acpi_subtable_headers *header,* * This is Linux-specific protocol and not reflected in ACPI spec. */ - mp_wake->base_address =0 ; + mp_wake->mailbox_address =0 ;apic_update_callback (wakeup_secondary_cpu_64, acpi_wakeup_cpu);include/acpi/actbl2.h +0
-0@@ -1109,9 +1109,9 @@ struct acpi_madt_generic_translator {
include/acpi/actbl2.h struct acpi_madt_generic_translator { · line 1117
Quoted 10 lines
include/acpi/actbl2.h +2
-2struct acpi_madt_multiproc_wakeup {struct acpi_subtable_header header; -u16 mailbox_version; +u16 version;u32 reserved;/* reserved - must be zero */ -u64 base_address; +u64 mailbox_address; };#define ACPI_MULTIPROC_WAKEUP_MB_OS_SIZE 2032
Nit:
Looks this patch can be moved to an earlier place because IMHO the justification you provided in the changelog also makes sense even w/o the new wake up structure version. Anyway up to you.
Re: [PATCHv2 12/13] x86/acpi: Rename fields in acpi_madt_multiproc_wakeup structure
In reply to Kirill A. ShutemovQuoted 9 lines · Kirill A. Shutemov
To prepare for the addition of support for MADT wakeup structure version 1, it is necessary to provide more appropriate names for the fields in the structure.
The field '
mailbox_version' renamed as 'version'. This field signifies the version of the structure and the related protocols, rather than the version of the mailbox. This field has not been utilized in the code thus far.
Quoted 7 lines
The field '
base_address' renamed as 'mailbox_address' to clarify the kind of address it represents. In version 1, the structure includes the reset vector address. Clear and distinct naming helps to prevent any confusion.Signed-off-by Kirill A. Shutemov <[email protected]>---
Quoted 42 lines
arch/x86/kernel/acpi/madt_wakeup.c | 4 ++-- include/acpi/actbl2.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)arch/x86/kernel/acpi/madt_wakeup.c +2
-2@@ -79,7 +79,7 @@ int __init acpi_parse_mp_wake(union acpi_subtable_headers *header,acpi_table_print_madt_entry (&header->common); - acpi_mp_wake_mailbox_paddr = mp_wake->base_address; + acpi_mp_wake_mailbox_paddr = mp_wake->mailbox_address;cpu_hotplug_disable_offlining ();@@ -98,7 +98,7 @@ int __init acpi_parse_mp_wake(union acpi_subtable_headers *header,* * This is Linux-specific protocol and not reflected in ACPI spec. */ - mp_wake->base_address =0 ; + mp_wake->mailbox_address =0 ;apic_update_callback (wakeup_secondary_cpu_64, acpi_wakeup_cpu);include/acpi/actbl2.h +2
-2@@ -1109,9 +1109,9 @@ struct acpi_madt_generic_translator {struct acpi_madt_multiproc_wakeup {struct acpi_subtable_header header; -u16 mailbox_version; +u16 version;u32 reserved;/* reserved - must be zero */ -u64 base_address; +u64 mailbox_address; };#define ACPI_MULTIPROC_WAKEUP_MB_OS_SIZE 2032
Signature · 2 lines
Sathyanarayanan Kuppuswamy Linux Kernel Developer
PATCH v2 13/13 x86/acpi: Add support for CPU offlining for ACPI MADT wakeup method
In reply to Kirill A. ShutemovMADT Multiprocessor Wakeup structure version 1 brings support of CPU
offlining: BIOS provides a reset vector where the CPU has to jump to offline itself. The new TEST mailbox command can be used to test the CPU offlined successfully and BIOS has control over it.
Add CPU offling support for ACPI MADT wakeup method by implementing custom cpu_die, play_dead and stop_other_cpus SMP operations.
CPU offlining makes possible to hand over secondary CPUs over kexec, not limiting the second kernel with single CPU.
The change conforms to the approved ACPI spec change proposal. See the Link.
--- arch/x86/kernel/acpi/Makefile | 2 +- arch/x86/kernel/acpi/boot.c | 2 + arch/x86/kernel/acpi/madt.S | 24 ++++ arch/x86/kernel/acpi/madt_wakeup.c | 197 ++++++++++++++++++++++++++--- include/acpi/actbl2.h | 15 ++- 5 files changed, 220 insertions(+), 20 deletions(-) create mode 100644 arch/x86/kernel/acpi/madt.S
arch/x86/kernel/acpi/Makefile +1 -1
@@ -4,7 +4,7 @@ obj-$(CONFIG_ACPI) += boot.o obj-$(CONFIG_ACPI_SLEEP) += sleep.o wakeup_$(BITS).o obj-$(CONFIG_ACPI_APEI) += apei.o obj-$(CONFIG_ACPI_CPPC_LIB) += cppc.o -obj-$(CONFIG_X86_ACPI_MADT_WAKEUP) += madt_wakeup.o +obj-$(CONFIG_X86_ACPI_MADT_WAKEUP) += madt_wakeup.o madt.o ifneq ($(CONFIG_ACPI_PROCESSOR),) obj-y += cstate.o
arch/x86/kernel/acpi/boot.c +2 -0
@@ -22,6 +22,7 @@#include <linux/efi-bgrt.h> #include <linux/serial_core.h> #include <linux/pgtable.h> +#include <linux/sched/hotplug.h> #include <asm/e820/api.h> #include <asm/irqdomain.h>
@@ -33,6 +34,7 @@#include <asm/smp.h> #include <asm/i8259.h> #include <asm/setup.h> +#include <asm/init.h> #include "sleep.h" /* To include x86_acpi_suspend_lowlevel */ static int __initdata acpi_force =0 ;
arch/x86/kernel/acpi/madt.S new file +24 -0
@@ -0,0 +1,24 @@ +#include <linux/linkage.h> +#include <asm/nospec-branch.h> +#include <asm/page_types.h> +#include <asm/processor-flags.h> + + .text + .align PAGE_SIZE +SYM_FUNC_START(asm_acpi_mp_play_dead) + /* Load address of reset vector into RCX to jump when kernel is ready */ + movq acpi_mp_reset_vector_paddr(%rip), %rcx + + /* Turn off global entries. Following CR3 write will flush them. */ + movq %cr4, %rdx + andq $~(X86_CR4_PGE), %rdx + movq %rdx, %cr4 + + /* Switch to identity mapping */ + movq acpi_mp_pgd(%rip), %rax + movq %rax, %cr3 + + /* Jump to reset vector */ + ANNOTATE_RETPOLINE_SAFE + jmp *%rcx +SYM_FUNC_END(asm_acpi_mp_play_dead)
arch/x86/kernel/acpi/madt_wakeup.c +179 -18
224 lines not on this page · raw message
include/acpi/actbl2.h +14 -1
@@ -1112,8 +1112,20 @@ struct acpi_madt_multiproc_wakeup {u16 version;u32 reserved;/* reserved - must be zero */ u64 mailbox_address; +u64 reset_vector; }; +/* Values for Version field above */ + +enum acpi_madt_multiproc_wakeup_version { +ACPI_MADT_MP_WAKEUP_VERSION_NONE =0 , +ACPI_MADT_MP_WAKEUP_VERSION_V1 =1 , +ACPI_MADT_MP_WAKEUP_VERSION_RESERVED =2 ,/* 2 and greater are reserved */ +}; + +#define ACPI_MADT_MP_WAKEUP_SIZE_V0 16 +#define ACPI_MADT_MP_WAKEUP_SIZE_V1 24 +#define ACPI_MULTIPROC_WAKEUP_MB_OS_SIZE 2032#define ACPI_MULTIPROC_WAKEUP_MB_FIRMWARE_SIZE 2048
@@ -1126,7 +1138,8 @@ struct acpi_madt_multiproc_wakeup_mailbox {u8 reserved_firmware[ACPI_MULTIPROC_WAKEUP_MB_FIRMWARE_SIZE ];/* reserved for firmware use */ }; -#define ACPI_MP_WAKE_COMMAND_WAKEUP 1 +#define ACPI_MP_WAKE_COMMAND_WAKEUP 1 +#define ACPI_MP_WAKE_COMMAND_TEST 2/* 17: CPU Core Interrupt Controller (ACPI 6.5) */
Signature · 1 line
2.41.0
Re: [PATCHv2 13/13] x86/acpi: Add support for CPU offlining for ACPI MADT wakeup method
arch/x86/kernel/acpi/madt.S
Quoted 3 lines
arch/x86/kernel/acpi/madt.S new file +0
-0
I think the name 'madt.S' is too generic. How about something be more specific such as madt_reset.S, or madt_playdead.S, etc?
Quoted 10 lines
arch/x86/kernel/acpi/madt.S new file +9
-0@@ -0,0 +1,24 @@ +#include <linux/linkage.h> +#include <asm/nospec-branch.h> +#include <asm/page_types.h> +#include <asm/processor-flags.h> + + .text + .align PAGE_SIZE +SYM_FUNC_START(asm_acpi_mp_play_dead) + /* Load address of reset vector into RCX to jump when kernel is ready */
arch/x86/kernel/acpi/madt.S line 19
Quoted 10 lines
arch/x86/kernel/acpi/madt.S new file +10
-0+ movq acpi_mp_reset_vector_paddr(%rip), %rcx + + /* Turn off global entries. Following CR3 write will flush them. */ + movq %cr4, %rdx + andq $~(X86_CR4_PGE), %rdx + movq %rdx, %cr4 + + /* Switch to identity mapping */ + movq acpi_mp_pgd(%rip), %rax + movq %rax, %cr3
Do we need to switch back to kernel direct-map page table after CPU is wake up again? We do support normal CPU offline/online, but not limited to kexec, right?
Quoted 32 lines
arch/x86/kernel/acpi/madt.S new file +5
-0+ + /* Jump to reset vector */ + ANNOTATE_RETPOLINE_SAFE + jmp *%rcx +SYM_FUNC_END(asm_acpi_mp_play_dead)arch/x86/kernel/acpi/madt_wakeup.c +10
-0@@ -1,8 +1,13 @@#include <linux/acpi.h> #include <linux/cpu.h> +#include <linux/delay.h> #include <linux/io.h> +#include <linux/memblock.h> +#include <linux/pgtable.h> +#include <linux/sched/hotplug.h> #include <asm/apic.h> #include <asm/barrier.h> +#include <asm/init.h> #include <asm/processor.h> /* Physical address of the Multiprocessor Wakeup Structure mailbox */ @@ -11,6 +16,150 @@ static u64 acpi_mp_wake_mailbox_paddr;/* Virtual address of the Multiprocessor Wakeup Structure mailbox */ static struct acpi_madt_multiproc_wakeup_mailbox *acpi_mp_wake_mailbox; +u64 acpi_mp_pgd; +u64 acpi_mp_reset_vector_paddr; + +void asm_acpi_mp_play_dead (void ); +
arch/x86/kernel/acpi/madt_wakeup.c static u64 acpi_mp_wake_mailbox_paddr; · line 33
Quoted 10 lines
arch/x86/kernel/acpi/madt_wakeup.c +9
-0+static void __init *alloc_pgt_page (void *context) +{ +return memblock_alloc (PAGE_SIZE ,PAGE_SIZE ); +} + +/* +* Make sure asm_acpi_mp_play_dead() is present in the identity mapping at +* the same place as in the kernel page tables. The function switches to +* the identity mapping
This function itself doesn't switch to the identity mapping. It just creates the kernel mapping for asm_acpi_mp_play_dead() in the identify mapping page table.
Quoted 2 lines
and has be present at the same spot in before and
+ * after transition.
This part doesn't parse to me. I guess the whole comment can be:
asm_acpi_mp_play_dead() is accessed both before and after switching to the identity mapping. Also map it at the kernel virtual address in the identity mapping table.
Or perhaps even better, put the above comments to the place where this function is called?
Quoted 98 lines
+ */
arch/x86/kernel/acpi/madt_wakeup.c +97
-0+static int __init init_transition_pgtable (pgd_t *pgd) +{ +pgprot_t prot =PAGE_KERNEL_EXEC_NOENC ; +unsigned long vaddr, paddr; +int result = -ENOMEM ; +p4d_t *p4d; +pud_t *pud; +pmd_t *pmd; +pte_t *pte; + + vaddr = (unsigned long )asm_acpi_mp_play_dead; + pgd +=pgd_index (vaddr); +if (!pgd_present (*pgd)) { + p4d = (p4d_t *)alloc_pgt_page (NULL ); +if (!p4d) + goto err; +set_pgd (pgd,__pgd (__pa (p4d) | _KERNPG_TABLE)); + } + p4d =p4d_offset (pgd, vaddr); +if (!p4d_present (*p4d)) { + pud = (pud_t *)alloc_pgt_page (NULL ); +if (!pud) + goto err; +set_p4d (p4d,__p4d (__pa (pud) | _KERNPG_TABLE)); + } + pud =pud_offset (p4d, vaddr); +if (!pud_present (*pud)) { + pmd = (pmd_t *)alloc_pgt_page (NULL ); +if (!pmd) + goto err; +set_pud (pud,__pud (__pa (pmd) | _KERNPG_TABLE)); + } + pmd =pmd_offset (pud, vaddr); +if (!pmd_present (*pmd)) { + pte = (pte_t *)alloc_pgt_page (NULL ); +if (!pte) + goto err; +set_pmd (pmd,__pmd (__pa (pte) | _KERNPG_TABLE)); + } + pte =pte_offset_kernel (pmd, vaddr); + + paddr =__pa (vaddr); +set_pte (pte,pfn_pte (paddr >>PAGE_SHIFT , prot)); + +return 0 ; +err: +return result; +} + +static void acpi_mp_play_dead (void ) +{ +play_dead_common (); +asm_acpi_mp_play_dead (); +} + +static void acpi_mp_cpu_die (unsigned int cpu) +{ +int apicid =per_cpu (x86_cpu_to_apicid, cpu); +unsigned long timeout; + +/* +* Use TEST mailbox command to prove that BIOS got control over +* the CPU before declaring it dead. +* +* BIOS has to clear 'command' field of the mailbox. +*/ + acpi_mp_wake_mailbox->apic_id = apicid; +smp_store_release (&acpi_mp_wake_mailbox->command, +ACPI_MP_WAKE_COMMAND_TEST ); + +/* Don't wait longer than a second. */ + timeout =USEC_PER_SEC ; +while (READ_ONCE (acpi_mp_wake_mailbox->command) && timeout--) +udelay (1 ); +} + +static void acpi_mp_stop_other_cpus (int wait) +{ +smp_shutdown_nonboot_cpus (smp_processor_id ()); +} + +static void acpi_mp_crash_stop_other_cpus (void ) +{ +smp_shutdown_nonboot_cpus (smp_processor_id ()); + +/* The kernel is broken so disable interrupts */ +local_irq_disable (); +} + +static int __init acpi_mp_setup_reset (u64 reset_vector) +{ +pgd_t *pgd; +struct x86_mapping_info info = { + .alloc_pgt_page = alloc_pgt_page, + .page_flag = __PAGE_KERNEL_LARGE_EXEC, + .kernpg_flag = _KERNPG_TABLE_NOENC, + };
arch/x86/kernel/acpi/madt_wakeup.c static u64 acpi_mp_wake_mailbox_paddr; · line 141
Quoted 10 lines
arch/x86/kernel/acpi/madt_wakeup.c +10
-0+ + pgd =alloc_pgt_page (NULL ); + +for (int i =0 ; i < nr_pfn_mapped; i++) { +unsigned long mstart, mend; + mstart = pfn_mapped[i].start <<PAGE_SHIFT ; + mend = pfn_mapped[i].end <<PAGE_SHIFT ; +if (kernel_ident_mapping_init (&info, pgd, mstart, mend)) +return -ENOMEM ; + }
This is for kexec() IIUC. Add a comment?
If we consider normal CPU offline/online case, then I don't think we need the identity mapping for all memory?
Quoted 35 lines
arch/x86/kernel/acpi/madt_wakeup.c +27
-1+ +if (kernel_ident_mapping_init (&info, pgd, +PAGE_ALIGN_DOWN (reset_vector), +PAGE_ALIGN (reset_vector +1 ))) { +return -ENOMEM ; + } + +if (init_transition_pgtable (pgd)) +return -ENOMEM ; + + smp_ops.play_dead = acpi_mp_play_dead; + smp_ops.cpu_die = acpi_mp_cpu_die; + smp_ops.stop_other_cpus = acpi_mp_stop_other_cpus; + smp_ops.crash_stop_other_cpus = acpi_mp_crash_stop_other_cpus; + + acpi_mp_reset_vector_paddr = reset_vector; + acpi_mp_pgd =__pa (pgd); + +return 0 ; +} +static int acpi_wakeup_cpu (int apicid,unsigned long start_ip) {if (!acpi_mp_wake_mailbox_paddr) {@@ -74,31 +223,43 @@ int __init acpi_parse_mp_wake(union acpi_subtable_headers *header,struct acpi_madt_multiproc_wakeup *mp_wake; mp_wake = (struct acpi_madt_multiproc_wakeup *)header; -if (BAD_MADT_ENTRY (mp_wake, end)) +if (!mp_wake) +return -EINVAL ; + +if (end - (unsigned long )mp_wake <ACPI_MADT_MP_WAKEUP_SIZE_V0 ) +return -EINVAL ; +if (mp_wake->header.length <ACPI_MADT_MP_WAKEUP_SIZE_V0 )
arch/x86/kernel/acpi/madt_wakeup.c int __init acpi_parse_mp_wake(union acpi_subtable_headers *header, · line 240
Quoted 10 lines
arch/x86/kernel/acpi/madt_wakeup.c +3
-1return -EINVAL ;acpi_table_print_madt_entry (&header->common); acpi_mp_wake_mailbox_paddr = mp_wake->mailbox_address; -cpu_hotplug_disable_offlining (); +if (mp_wake->version >=ACPI_MADT_MP_WAKEUP_VERSION_V1 && + mp_wake->header.length >=ACPI_MADT_MP_WAKEUP_SIZE_V1 ) { +acpi_mp_setup_reset (mp_wake->reset_vector);
It's better to fallback to "disable offline" if this function fails.
Re: [PATCHv2 13/13] x86/acpi: Add support for CPU offlining for ACPI MADT wakeup method
Quoted 11 lines
Quoted 11 lines
arch/x86/kernel/acpi/madt.S new file +10
-0+ .text + .align PAGE_SIZE +SYM_FUNC_START(asm_acpi_mp_play_dead) + /* Load address of reset vector into RCX to jump when kernel is ready */ + movq acpi_mp_reset_vector_paddr(%rip), %rcx + + /* Turn off global entries. Following CR3 write will flush them. */ + movq %cr4, %rdx + andq $~(X86_CR4_PGE), %rdx + movq %rdx, %cr4
Quoted 8 lines
arch/x86/kernel/acpi/madt.S line 19
Quoted 4 lines
arch/x86/kernel/acpi/madt.S new file +4
-0+ + /* Switch to identity mapping */ + movq acpi_mp_pgd(%rip), %rax + movq %rax, %cr3Do we need to switch back to kernel direct-map page table after CPU is wake up again? We do support normal CPU offline/online, but not limited to kexec, right?
Please ignore this. I found if I am reading right even for TDX guest the new online cpu will start with trampoline_start64 assembly, so it will load kernel page table anyway. Sorry for the noise.
[...]
Quoted 10 lines
arch/x86/kernel/acpi/madt_wakeup.c static u64 acpi_mp_wake_mailbox_paddr; · line 141
Quoted 7 lines
arch/x86/kernel/acpi/madt_wakeup.c +7
-0+for (int i =0 ; i < nr_pfn_mapped; i++) { +unsigned long mstart, mend; + mstart = pfn_mapped[i].start <<PAGE_SHIFT ; + mend = pfn_mapped[i].end <<PAGE_SHIFT ; +if (kernel_ident_mapping_init (&info, pgd, mstart, mend)) +return -ENOMEM ; + }This is for
kexec()IIUC. Add a comment?
Quoted 3 lines
If we consider normal CPU offline/online case, then I don't think we need the identity mapping for all memory?
Also this one. :-)
Quoted 1 line
Re: [PATCHv2 13/13] x86/acpi: Add support for CPU offlining for ACPI MADT wakeup method
In reply to Huang, KaiQuoted 6 lines · Kai
arch/x86/kernel/acpi/madt.S
Quoted 3 lines
arch/x86/kernel/acpi/madt.S new file +0
-0I think the name '
madt.S' is too generic. How about something be more specific such asmadt_reset.S, ormadt_playdead.S, etc?
Okay, madt_playdead.S sounds good.
Quoted 15 lines
Quoted 15 lines
arch/x86/kernel/acpi/madt.S new file +11
-0@@ -11,6 +16,150 @@ static u64 acpi_mp_wake_mailbox_paddr; /* Virtual address of the Multiprocessor Wakeup Structure mailbox */ static struct acpi_madt_multiproc_wakeup_mailbox *acpi_mp_wake_mailbox; +u64 acpi_mp_pgd; +u64 acpi_mp_reset_vector_paddr; + +void asm_acpi_mp_play_dead(void); + +static void __init *alloc_pgt_page(void *context) +{ + return memblock_alloc(PAGE_SIZE, PAGE_SIZE); +} + +/*
Quoted 8 lines
arch/x86/kernel/acpi/madt.S static u64 acpi_mp_wake_mailbox_paddr; · line 33
Quoted 4 lines
arch/x86/kernel/acpi/madt.S new file +3
-0+ * Make sure asm_acpi_mp_play_dead() is present in the identity mapping at + * the same place as in the kernel page tables. The function switches to + * the identity mappingThis function itself doesn't switch to the identity mapping. It just creates the kernel mapping for
asm_acpi_mp_play_dead()in the identify mapping page table.
By "The function" I meant asm_acpi_mp_play_dead(). Yeah, it is not clear.
Will so s/The function/asm_acpi_mp_play_dead()/
Quoted 7 lines
arch/x86/kernel/acpi/madt.S static u64 acpi_mp_wake_mailbox_paddr; · line 36
Quoted 4 lines
arch/x86/kernel/acpi/madt.S new file +3
-1- cpu_hotplug_disable_offlining(); + if (mp_wake->version >= ACPI_MADT_MP_WAKEUP_VERSION_V1 && + mp_wake->header.length >= ACPI_MADT_MP_WAKEUP_SIZE_V1) { + acpi_mp_setup_reset(mp_wake->reset_vector);It's better to fallback to "disable offline" if this function fails.
Okay, will warn to disable offlining.
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
Re: [PATCHv2 13/13] x86/acpi: Add support for CPU offlining for ACPI MADT wakeup method
In reply to Kirill A. ShutemovQuoted 9 lines · Kirill A. Shutemov
MADT Multiprocessor Wakeup structure version 1 brings support of CPU
offlining: BIOS provides a reset vector where the CPU has to jump to offline itself. The new TEST mailbox command can be used to test the CPU offlined successfully and BIOS has control over it.Add CPU offling support for ACPI MADT wakeup method by implementing custom
cpu_die,play_deadandstop_other_cpusSMP operations.CPU offlining makes possible to hand over secondary CPUs over kexec, not
makes it possible
Quoted 1 line
limiting the second kernel with single CPU.
s/with/to/
arch/x86/kernel/acpi/madt.S line 19
Quoted 13 lines
The change conforms to the approved ACPI spec change proposal. See the
arch/x86/kernel/acpi/madt.S new file +12
-0+SYM_FUNC_START(asm_acpi_mp_play_dead) + /* Load address of reset vector into RCX to jump when kernel is ready */ + movq acpi_mp_reset_vector_paddr(%rip), %rcx + + /* Turn off global entries. Following CR3 write will flush them. */ + movq %cr4, %rdx + andq $~(X86_CR4_PGE), %rdx + movq %rdx, %cr4 + + /* Switch to identity mapping */ + movq acpi_mp_pgd(%rip), %rax + movq %rax, %cr3
You can just make this function:
asm_acpi_mp_play_dead(u64 reset_vector, u64 pgd_pa);
then you have the reset vector in RDI and the pgd in RSI and spare the global variables.
arch/x86/kernel/acpi/madt_wakeup.c static u64 acpi_mp_wake_mailbox_paddr; · line 20
Quoted 8 lines
/* Physical address of the Multiprocessor Wakeup Structure mailbox */
arch/x86/kernel/acpi/madt_wakeup.c +2
-0@@ -11,6 +16,150 @@ static u64 acpi_mp_wake_mailbox_paddr;/* Virtual address of the Multiprocessor Wakeup Structure mailbox */ static struct acpi_madt_multiproc_wakeup_mailbox *acpi_mp_wake_mailbox; +u64 acpi_mp_pgd; +u64 acpi_mp_reset_vector_paddr;
See above (static) and __ro_after_init please
arch/x86/kernel/acpi/madt_wakeup.c static u64 acpi_mp_wake_mailbox_paddr; · line 25
Quoted 5 lines
arch/x86/kernel/acpi/madt_wakeup.c +5
-0+ +void asm_acpi_mp_play_dead (void ); + +static void __init *alloc_pgt_page (void *context) +{
What's the purpose of the context argument?
arch/x86/kernel/acpi/madt_wakeup.c static u64 acpi_mp_wake_mailbox_paddr; · line 33
Quoted 8 lines
arch/x86/kernel/acpi/madt_wakeup.c +8
-0+return memblock_alloc (PAGE_SIZE ,PAGE_SIZE ); +} + +/* +* Make sure asm_acpi_mp_play_dead() is present in the identity mapping at +* the same place as in the kernel page tables. The function switches to +* the identity mapping and has be present at the same spot in before and +* after transition.
Why does it need to be there after the CPU jumped to the reset vector?
Quoted 7 lines
arch/x86/kernel/acpi/madt_wakeup.c +7
-0+*/ +static int __init init_transition_pgtable (pgd_t *pgd) +{ +pgprot_t prot =PAGE_KERNEL_EXEC_NOENC ; +unsigned long vaddr, paddr; +int result = -ENOMEM ; +p4d_t *p4d;
arch/x86/kernel/acpi/madt_wakeup.c static u64 acpi_mp_wake_mailbox_paddr; · line 50
Quoted 10 lines
arch/x86/kernel/acpi/madt_wakeup.c +10
-0+pud_t *pud; +pmd_t *pmd; +pte_t *pte; + + vaddr = (unsigned long )asm_acpi_mp_play_dead; + pgd +=pgd_index (vaddr); +if (!pgd_present (*pgd)) { + p4d = (p4d_t *)alloc_pgt_page (NULL ); +if (!p4d) + goto err;
return -ENOMEM?
the error labels is pretty silly without an actual cleanup, right?
arch/x86/kernel/acpi/madt_wakeup.c static u64 acpi_mp_wake_mailbox_paddr; · line 57
Quoted 7 lines
arch/x86/kernel/acpi/madt_wakeup.c +7
-0+set_pgd (pgd,__pgd (__pa (p4d) | _KERNPG_TABLE)); + } + p4d =p4d_offset (pgd, vaddr); +if (!p4d_present (*p4d)) { + pud = (pud_t *)alloc_pgt_page (NULL ); +if (!pud) + goto err;
Ditto. But what mops up the already allocated page above?
arch/x86/kernel/acpi/madt_wakeup.c static u64 acpi_mp_wake_mailbox_paddr; · line 82
Quoted 6 lines
arch/x86/kernel/acpi/madt_wakeup.c +6
-0+set_pte (pte,pfn_pte (paddr >>PAGE_SHIFT , prot)); + +return 0 ; +err: +return result; +}
Can you please move that function to the place where it is used?
arch/x86/kernel/acpi/madt_wakeup.c static u64 acpi_mp_wake_mailbox_paddr; · line 92
Quoted 10 lines
arch/x86/kernel/acpi/madt_wakeup.c +10
-0+ +static void acpi_mp_play_dead (void ) +{ +play_dead_common (); +asm_acpi_mp_play_dead (); +} + +static void acpi_mp_cpu_die (unsigned int cpu) +{ +int apicid =per_cpu (x86_cpu_to_apicid, cpu);
u32 apicid
Quoted 11 lines
arch/x86/kernel/acpi/madt_wakeup.c +11
-0+unsigned long timeout; + +/* +* Use TEST mailbox command to prove that BIOS got control over +* the CPU before declaring it dead. +* +* BIOS has to clear 'command' field of the mailbox. +*/ + acpi_mp_wake_mailbox->apic_id = apicid; +smp_store_release (&acpi_mp_wake_mailbox->command, +ACPI_MP_WAKE_COMMAND_TEST );
arch/x86/kernel/acpi/madt_wakeup.c static u64 acpi_mp_wake_mailbox_paddr; · line 113
Quoted 10 lines
arch/x86/kernel/acpi/madt_wakeup.c +10
-0+ +/* Don't wait longer than a second. */ + timeout =USEC_PER_SEC ; +while (READ_ONCE (acpi_mp_wake_mailbox->command) && timeout--) +udelay (1 ); +} + +static void acpi_mp_stop_other_cpus (int wait) +{ +smp_shutdown_nonboot_cpus (smp_processor_id ());
This clearly was never tested with lockdep. At the point where stop_other_cpus() is invoked the invoking CPU has interrupts disabled...
arch/x86/kernel/acpi/madt_wakeup.c static u64 acpi_mp_wake_mailbox_paddr; · line 118
Quoted 5 lines
arch/x86/kernel/acpi/madt_wakeup.c +5
-0+} + +static void acpi_mp_crash_stop_other_cpus (void ) +{ +smp_shutdown_nonboot_cpus (smp_processor_id ());
Yuck. Crash can happen at arbitrary places. So you really cannot invoke the whole CPU hotplug state machine from here.
There is a reason why the other implementation just kick CPUs into some "safe" state.
Quoted 7 lines
arch/x86/kernel/acpi/madt_wakeup.c +7
-0+/* The kernel is broken so disable interrupts */ +local_irq_disable (); +} + +static int __init acpi_mp_setup_reset (u64 reset_vector) +{ +pgd_t *pgd;
arch/x86/kernel/acpi/madt_wakeup.c static u64 acpi_mp_wake_mailbox_paddr; · line 136
Quoted 10 lines
arch/x86/kernel/acpi/madt_wakeup.c +10
-0+struct x86_mapping_info info = { + .alloc_pgt_page = alloc_pgt_page, + .page_flag = __PAGE_KERNEL_LARGE_EXEC, + .kernpg_flag = _KERNPG_TABLE_NOENC, + }; + + pgd =alloc_pgt_page (NULL ); + +for (int i =0 ; i < nr_pfn_mapped; i++) { +unsigned long mstart, mend;
Missing newline
Quoted 61 lines
arch/x86/kernel/acpi/madt_wakeup.c +37
-10+ mstart = pfn_mapped[i].start <<PAGE_SHIFT ; + mend = pfn_mapped[i].end <<PAGE_SHIFT ; +if (kernel_ident_mapping_init (&info, pgd, mstart, mend)) +return -ENOMEM ; + } + +if (kernel_ident_mapping_init (&info, pgd, +PAGE_ALIGN_DOWN (reset_vector), +PAGE_ALIGN (reset_vector +1 ))) { +return -ENOMEM ; + } + +if (init_transition_pgtable (pgd)) +return -ENOMEM ; + + smp_ops.play_dead = acpi_mp_play_dead; + smp_ops.cpu_die = acpi_mp_cpu_die; + smp_ops.stop_other_cpus = acpi_mp_stop_other_cpus; + smp_ops.crash_stop_other_cpus = acpi_mp_crash_stop_other_cpus; + + acpi_mp_reset_vector_paddr = reset_vector; + acpi_mp_pgd =__pa (pgd); + +return 0 ; +} +static int acpi_wakeup_cpu (int apicid,unsigned long start_ip) {if (!acpi_mp_wake_mailbox_paddr) {@@ -74,31 +223,43 @@ int __init acpi_parse_mp_wake(union acpi_subtable_headers *header,struct acpi_madt_multiproc_wakeup *mp_wake; mp_wake = (struct acpi_madt_multiproc_wakeup *)header; -if (BAD_MADT_ENTRY (mp_wake, end)) +if (!mp_wake) +return -EINVAL ; + +if (end - (unsigned long )mp_wake <ACPI_MADT_MP_WAKEUP_SIZE_V0 ) +return -EINVAL ; +if (mp_wake->header.length <ACPI_MADT_MP_WAKEUP_SIZE_V0 )return -EINVAL ;acpi_table_print_madt_entry (&header->common); acpi_mp_wake_mailbox_paddr = mp_wake->mailbox_address; -cpu_hotplug_disable_offlining (); +if (mp_wake->version >=ACPI_MADT_MP_WAKEUP_VERSION_V1 && + mp_wake->header.length >=ACPI_MADT_MP_WAKEUP_SIZE_V1 ) { +acpi_mp_setup_reset (mp_wake->reset_vector); + }else { +cpu_hotplug_disable_offlining (); -/* -* ACPI MADT doesn't allow to offline CPU after it got woke up. -* It limits kexec: the second kernel won't be able to use more than -* one CPU. -* -* Now acpi_mp_wake_mailbox_paddr already has the mailbox address. -* The acpi_wakeup_cpu() will use it to bring up secondary cpus. -*
arch/x86/kernel/acpi/madt_wakeup.c int __init acpi_parse_mp_wake(union acpi_subtable_headers *header, · line 245
Quoted 10 lines
arch/x86/kernel/acpi/madt_wakeup.c +2
-8-* Zero out mailbox address in the ACPI MADT wakeup structure to -* indicate that the mailbox is not usable. This prevents the -* kexec()-ed kernel from reading a vaild mailbox, which in turn -* makes the kexec()-ed kernel only be able to use the boot CPU. -* -* This is Linux-specific protocol and not reflected in ACPI spec. -*/ - mp_wake->mailbox_address =0 ; +/* +* ACPI MADT doesn't allow to offline CPU after it got woke up.
This is not longer accurate as V1 allows that ....
arch/x86/kernel/acpi/madt_wakeup.c int __init acpi_parse_mp_wake(union acpi_subtable_headers *header, · line 247
Quoted 2 lines
arch/x86/kernel/acpi/madt_wakeup.c +2
-0+* It limits kexec: the second kernel won't be able to use more +* than one CPU.
Thanks,
tglx
Re: [PATCHv2 13/13] x86/acpi: Add support for CPU offlining for ACPI MADT wakeup method
Quoted 10 lines · Thomas Gleixner
Quoted 8 lines · Kirill A. Shutemov
MADT Multiprocessor Wakeup structure version 1 brings support of CPU
offlining: BIOS provides a reset vector where the CPU has to jump to offline itself. The new TEST mailbox command can be used to test the CPU offlined successfully and BIOS has control over it.Add CPU offling support for ACPI MADT wakeup method by implementing custom
cpu_die,play_deadandstop_other_cpusSMP operations.
Quoted 7 lines
Quoted 1 line
CPU offlining makes possible to hand over secondary CPUs over kexec, not
makes it possible
Quoted 1 line
limiting the second kernel with single CPU.
s/with/to/
Okay.
Quoted 16 lines
arch/x86/kernel/acpi/madt.S line 19
Quoted 13 lines
The change conforms to the approved ACPI spec change proposal. See the
arch/x86/kernel/acpi/madt.S new file +12
-0+SYM_FUNC_START(asm_acpi_mp_play_dead) + /* Load address of reset vector into RCX to jump when kernel is ready */ + movq acpi_mp_reset_vector_paddr(%rip), %rcx + + /* Turn off global entries. Following CR3 write will flush them. */ + movq %cr4, %rdx + andq $~(X86_CR4_PGE), %rdx + movq %rdx, %cr4 + + /* Switch to identity mapping */ + movq acpi_mp_pgd(%rip), %rax + movq %rax, %cr3You can just make this function:
Quoted 4 lines
asm_acpi_mp_play_dead(u64 reset_vector, u64 pgd_pa);then you have the reset vector in RDI and the pgd in RSI and spare the global variables.
Yeah, it is better. Thanks.
Quoted 10 lines
arch/x86/kernel/acpi/madt_wakeup.c static u64 acpi_mp_wake_mailbox_paddr; · line 20
Quoted 7 lines
/* Physical address of the Multiprocessor Wakeup Structure mailbox */
arch/x86/kernel/acpi/madt_wakeup.c +2
-0@@ -11,6 +16,150 @@ static u64 acpi_mp_wake_mailbox_paddr;/* Virtual address of the Multiprocessor Wakeup Structure mailbox */ static struct acpi_madt_multiproc_wakeup_mailbox *acpi_mp_wake_mailbox; +u64 acpi_mp_pgd; +u64 acpi_mp_reset_vector_paddr;See above (static) and
__ro_after_initplease
Quoted 7 lines
arch/x86/kernel/acpi/madt_wakeup.c static u64 acpi_mp_wake_mailbox_paddr; · line 25
Quoted 5 lines
arch/x86/kernel/acpi/madt_wakeup.c +5
-0+ +void asm_acpi_mp_play_dead (void ); + +static void __init *alloc_pgt_page (void *context) +{What's the purpose of the context argument?
To conform to x86_mapping_info::alloc_pgt_page type.
I will rename the argument to 'dummy' and add comment.
Quoted 10 lines
arch/x86/kernel/acpi/madt_wakeup.c static u64 acpi_mp_wake_mailbox_paddr; · line 33
Quoted 8 lines
arch/x86/kernel/acpi/madt_wakeup.c +8
-0+return memblock_alloc (PAGE_SIZE ,PAGE_SIZE ); +} + +/* +* Make sure asm_acpi_mp_play_dead() is present in the identity mapping at +* the same place as in the kernel page tables. The function switches to +* the identity mapping and has be present at the same spot in before and +* after transition. Why does it need to be there after the CPU jumped to the reset vector?
After transition to the identity mapping, not after jumping to reset vector. I will adjust the comment.
Quoted 20 lines
Quoted 7 lines
arch/x86/kernel/acpi/madt_wakeup.c +7
-0+*/ +static int __init init_transition_pgtable (pgd_t *pgd) +{ +pgprot_t prot =PAGE_KERNEL_EXEC_NOENC ; +unsigned long vaddr, paddr; +int result = -ENOMEM ; +p4d_t *p4d;arch/x86/kernel/acpi/madt_wakeup.c static u64 acpi_mp_wake_mailbox_paddr; · line 50
Quoted 10 lines
arch/x86/kernel/acpi/madt_wakeup.c +10
-0+pud_t *pud; +pmd_t *pmd; +pte_t *pte; + + vaddr = (unsigned long )asm_acpi_mp_play_dead; + pgd +=pgd_index (vaddr); +if (!pgd_present (*pgd)) { + p4d = (p4d_t *)alloc_pgt_page (NULL ); +if (!p4d) + goto err;return
-ENOMEM?
Quoted 1 line
the error labels is pretty silly without an actual cleanup, right?
Right.
Quoted 9 lines
arch/x86/kernel/acpi/madt_wakeup.c static u64 acpi_mp_wake_mailbox_paddr; · line 57
Quoted 7 lines
arch/x86/kernel/acpi/madt_wakeup.c +7
-0+set_pgd (pgd,__pgd (__pa (p4d) | _KERNPG_TABLE)); + } + p4d =p4d_offset (pgd, vaddr); +if (!p4d_present (*p4d)) { + pud = (pud_t *)alloc_pgt_page (NULL ); +if (!pud) + goto err;Ditto. But what mops up the already allocated page above?
Oops. I will add cleanup in acpi_mp_setup_reset() if kernel_ident_mapping_init() or init_transition_pgtable() fails.
Quoted 8 lines
arch/x86/kernel/acpi/madt_wakeup.c static u64 acpi_mp_wake_mailbox_paddr; · line 82
Quoted 6 lines
arch/x86/kernel/acpi/madt_wakeup.c +6
-0+set_pte (pte,pfn_pte (paddr >>PAGE_SHIFT , prot)); + +return 0 ; +err: +return result; +}Can you please move that function to the place where it is used?
Sure.
Quoted 4 lines
Quoted 4 lines
arch/x86/kernel/acpi/madt_wakeup.c +4
-0+ +static void acpi_mp_play_dead (void ) +{ +play_dead_common ();
Quoted 8 lines
arch/x86/kernel/acpi/madt_wakeup.c static u64 acpi_mp_wake_mailbox_paddr; · line 92
Quoted 6 lines
arch/x86/kernel/acpi/madt_wakeup.c +6
-0+asm_acpi_mp_play_dead (); +} + +static void acpi_mp_cpu_die (unsigned int cpu) +{ +int apicid =per_cpu (x86_cpu_to_apicid, cpu);u32 apicid
Okay.
Quoted 16 lines
Quoted 16 lines
arch/x86/kernel/acpi/madt_wakeup.c +16
-0+unsigned long timeout; + +/* +* Use TEST mailbox command to prove that BIOS got control over +* the CPU before declaring it dead. +* +* BIOS has to clear 'command' field of the mailbox. +*/ + acpi_mp_wake_mailbox->apic_id = apicid; +smp_store_release (&acpi_mp_wake_mailbox->command, +ACPI_MP_WAKE_COMMAND_TEST ); + +/* Don't wait longer than a second. */ + timeout =USEC_PER_SEC ; +while (READ_ONCE (acpi_mp_wake_mailbox->command) && timeout--) +udelay (1 );
Quoted 8 lines
arch/x86/kernel/acpi/madt_wakeup.c static u64 acpi_mp_wake_mailbox_paddr; · line 113
Quoted 5 lines
arch/x86/kernel/acpi/madt_wakeup.c +5
-0+} + +static void acpi_mp_stop_other_cpus (int wait) +{ +smp_shutdown_nonboot_cpus (smp_processor_id ());This clearly was never tested with lockdep. At the point where
stop_other_cpus()is invoked the invoking CPU has interrupts disabled...
Hm. I do have lockdep enabled and there's no reported.
And I am not sure why it should complain. At this point we are running on reboot cpu (after migrate_to_reboot_cpu()).
Could you elaborate on what the problem here?
Quoted 11 lines
arch/x86/kernel/acpi/madt_wakeup.c static u64 acpi_mp_wake_mailbox_paddr; · line 118
Quoted 5 lines
arch/x86/kernel/acpi/madt_wakeup.c +5
-0+} + +static void acpi_mp_crash_stop_other_cpus (void ) +{ +smp_shutdown_nonboot_cpus (smp_processor_id ());Yuck. Crash can happen at arbitrary places. So you really cannot invoke the whole CPU hotplug state machine from here.
There is a reason why the other implementation just kick CPUs into some "safe" state.
Yeah, fair enough.
What about the implementation below? It is heavily inspired by nmi_shootdown_cpus().
BTW, I don't understand why nmi_shootdown_cpus() needs 'crashing_cpu' if it uses apic_send_IPI_allbutself(). 'crashing_cpu' == self, no?
static atomic_t waiting_for_crash_ipi;
static bool crash_ipi_issued;
static int crash_nmi_callback(unsigned int val, struct pt_regs *regs)
{
local_irq_disable();
crash_save_cpu(regs, raw_smp_processor_id());
cpu_emergency_stop_pt();
disable_local_APIC();
/*
* Prepare the CPU for reboot _after_ invoking the callback so that the
* callback can safely use virtualization instructions, e.g. VMCLEAR.
*/
cpu_emergency_disable_virtualization();
atomic_dec(&waiting_for_crash_ipi);
asm_acpi_mp_play_dead(acpi_mp_reset_vector_paddr,
acpi_mp_pgd);
return NMI_HANDLED;
}static void acpi_mp_crash_stop_other_cpus(void)
{
unsigned long timeout;
/* The kernel is broken so disable interrupts */
local_irq_disable();
/*
* Avoid certain doom if a shootdown already occurred; re-registering
* the NMI handler will cause list corruption, modifying the callback
* will do who knows what, etc...
*/
if (WARN_ON_ONCE(crash_ipi_issued))
return;
atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
/* Would it be better to replace the trap vector here? */
if (register_nmi_handler(NMI_LOCAL, crash_nmi_callback,
NMI_FLAG_FIRST, "crash"))
return; /* Return what? */
apic_send_IPI_allbutself(NMI_VECTOR);
WRITE_ONCE(crash_ipi_issued, 1);
/* Don't wait longer than a second. */
timeout = USEC_PER_SEC;
while (atomic_read(&waiting_for_crash_ipi) && timeout--)
udelay(1);
}Quoted 11 lines
Quoted 11 lines
arch/x86/kernel/acpi/madt_wakeup.c +11
-0+/* The kernel is broken so disable interrupts */ +local_irq_disable (); +} + +static int __init acpi_mp_setup_reset (u64 reset_vector) +{ +pgd_t *pgd; +struct x86_mapping_info info = { + .alloc_pgt_page = alloc_pgt_page, + .page_flag = __PAGE_KERNEL_LARGE_EXEC, + .kernpg_flag = _KERNPG_TABLE_NOENC,
Quoted 9 lines
arch/x86/kernel/acpi/madt_wakeup.c static u64 acpi_mp_wake_mailbox_paddr; · line 136
Quoted 6 lines
arch/x86/kernel/acpi/madt_wakeup.c +6
-0+ }; + + pgd =alloc_pgt_page (NULL ); + +for (int i =0 ; i < nr_pfn_mapped; i++) { +unsigned long mstart, mend;Missing newline
Okay.
Quoted 13 lines
Quoted 13 lines
arch/x86/kernel/acpi/madt_wakeup.c +0
-12-/* -* ACPI MADT doesn't allow to offline CPU after it got woke up. -* It limits kexec: the second kernel won't be able to use more than -* one CPU. -* -* Now acpi_mp_wake_mailbox_paddr already has the mailbox address. -* The acpi_wakeup_cpu() will use it to bring up secondary cpus. -* -* Zero out mailbox address in the ACPI MADT wakeup structure to -* indicate that the mailbox is not usable. This prevents the -* kexec()-ed kernel from reading a vaild mailbox, which in turn -* makes the kexec()-ed kernel only be able to use the boot CPU.
Quoted 9 lines
arch/x86/kernel/acpi/madt_wakeup.c int __init acpi_parse_mp_wake(union acpi_subtable_headers *header, · line 245
Quoted 6 lines
arch/x86/kernel/acpi/madt_wakeup.c +2
-4-* -* This is Linux-specific protocol and not reflected in ACPI spec. -*/ - mp_wake->mailbox_address =0 ; +/* +* ACPI MADT doesn't allow to offline CPU after it got woke up. This is not longer accurate as V1 allows that ....
Will fix.
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov