Enhance the hv_vmbus driver to support hibernation
Subsystems (4)
Dexuan Cui
maintainercover Enhance the hv_vmbus driver to support hibernation
Hi,
This is the first patchset to enable hibernation when Linux VM runs on Hyper-V.
A second patchset to enhance the high-level VSC drivers (hv_netvsc, hv_storvsc, etc.) for hibernation will be posted later. The second patchset depends on this first patchset, so I hope this pathset can be accepted soon.
The changes in this patchset are mostly straightforward new code that only runs when the VM enters hibernation, so IMHO it's pretty safe to have this patchset, because the hibernation feature never worked for Linux VM running on Hyper-V.
The patchset is rebased on Linus's tree today.
Please review.
Thanks,
Dexuan
Dexuan Cui (7):
x86/hyper-v: Suspend/resume the hypercall page for hibernation clocksource/drivers: Suspend/resume Hyper-V clocksource for
hibernation
Drivers: hv: vmbus: Split hv_synic_init/cleanup into regs and timer
settings
Drivers: hv: vmbus: Suspend/resume the synic for hibernation
Drivers: hv: vmbus: Ignore the offers when resuming from hibernation
Drivers: hv: vmbus: Suspend/resume the vmbus itself for hibernation
Drivers: hv: vmbus: Implement suspend/resume for VSC drivers for
hibernation
arch/x86/hyperv/hv_init.c | 34 +++++++++++ drivers/clocksource/hyperv_timer.c | 25 ++++++++ drivers/hv/channel_mgmt.c | 28 ++++++++- drivers/hv/connection.c | 3 +- drivers/hv/hv.c | 66 +++++++++++--------- drivers/hv/hyperv_vmbus.h | 4 ++ drivers/hv/vmbus_drv.c | 122 +++++++++++++++++++++++++++++++++++++ include/linux/hyperv.h | 3 + 8 files changed, 253 insertions(+), 32 deletions(-)
Signature · 1 line
1.8.3.1
Dexuan Cui
maintainerPATCH 1/7 x86/hyper-v: Suspend/resume the hypercall page for hibernation
This is needed for hibernation, e.g. when we resume the old kernel, we need to disable the "current" kernel's hypercall page and then resume the old kernel's.
--- arch/x86/hyperv/hv_init.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+)
arch/x86/hyperv/hv_init.c +34 -0
@@ -20,6 +20,7 @@#include <linux/hyperv.h> #include <linux/slab.h> #include <linux/cpuhotplug.h> +#include <linux/syscore_ops.h> #include <clocksource/hyperv_timer.h> void *hv_hypercall_pg;
@@ -214,6 +215,34 @@ static int __init hv_pci_init(void)return 1 ; } +static int hv_suspend (void ) +{ +union hv_x64_msr_hypercall_contents hypercall_msr; + +/* Reset the hypercall page */ +rdmsrl (HV_X64_MSR_HYPERCALL , hypercall_msr.as_uint64); + hypercall_msr.enable =0 ; +wrmsrl (HV_X64_MSR_HYPERCALL , hypercall_msr.as_uint64); + +return 0 ; +} + +static void hv_resume (void ) +{ +union hv_x64_msr_hypercall_contents hypercall_msr; + +/* Re-enable the hypercall page */ +rdmsrl (HV_X64_MSR_HYPERCALL , hypercall_msr.as_uint64); + hypercall_msr.enable =1 ; + hypercall_msr.guest_physical_address =vmalloc_to_pfn (hv_hypercall_pg); +wrmsrl (HV_X64_MSR_HYPERCALL , hypercall_msr.as_uint64); +} + +static struct syscore_ops hv_syscore_ops = { + .suspend = hv_suspend, + .resume = hv_resume, +}; +/* * This function is to be invoked early in the boot sequence after the * hypervisor has been detected.
@@ -294,6 +323,9 @@ void __init hyperv_init(void)/* Register Hyper-V specific clocksource */ hv_init_clocksource (); + +register_syscore_ops (&hv_syscore_ops); +return ; remove_cpuhp_state:
@@ -313,6 +345,8 @@ void hyperv_cleanup(void) {union hv_x64_msr_hypercall_contents hypercall_msr; +unregister_syscore_ops (&hv_syscore_ops); +/* Reset our OS id */ wrmsrl (HV_X64_MSR_GUEST_OS_ID ,0 );
Signature · 1 line
1.8.3.1
RE: [PATCH 1/7] x86/hyper-v: Suspend/resume the hypercall page for hibernation
From: Dexuan Cui <[email protected]> Sent: Monday, July 8, 2019 10:29 PM
Quoted 77 lines
This is needed for hibernation, e.g. when we resume the old kernel, we need to disable the "current" kernel's hypercall page and then resume the old kernel's.
Signed-off-by Dexuan Cui <[email protected]>--- arch/x86/hyperv/hv_init.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+)arch/x86/hyperv/hv_init.c +34
-1@@ -20,6 +20,7 @@#include <linux/hyperv.h> #include <linux/slab.h> #include <linux/cpuhotplug.h> +#include <linux/syscore_ops.h> #include <clocksource/hyperv_timer.h> void *hv_hypercall_pg;@@ -214,6 +215,34 @@ static int __init hv_pci_init(void)return 1 ; } +static int hv_suspend (void ) +{ +union hv_x64_msr_hypercall_contents hypercall_msr; + +/* Reset the hypercall page */ +rdmsrl (HV_X64_MSR_HYPERCALL , hypercall_msr.as_uint64); + hypercall_msr.enable =0 ; +wrmsrl (HV_X64_MSR_HYPERCALL , hypercall_msr.as_uint64); + +return 0 ; +} + +static void hv_resume (void ) +{ +union hv_x64_msr_hypercall_contents hypercall_msr; + +/* Re-enable the hypercall page */ +rdmsrl (HV_X64_MSR_HYPERCALL , hypercall_msr.as_uint64); + hypercall_msr.enable =1 ; + hypercall_msr.guest_physical_address =vmalloc_to_pfn (hv_hypercall_pg); +wrmsrl (HV_X64_MSR_HYPERCALL , hypercall_msr.as_uint64); +} + +static struct syscore_ops hv_syscore_ops = { + .suspend = hv_suspend, + .resume = hv_resume, +}; +/* * This function is to be invoked early in the boot sequence after the * hypervisor has been detected. @@ -294,6 +323,9 @@ void __init hyperv_init(void)/* Register Hyper-V specific clocksource */ hv_init_clocksource (); + +register_syscore_ops (&hv_syscore_ops); +return ; remove_cpuhp_state:@@ -313,6 +345,8 @@ void hyperv_cleanup(void) {union hv_x64_msr_hypercall_contents hypercall_msr; +unregister_syscore_ops (&hv_syscore_ops); +/* Reset our OS id */ wrmsrl (HV_X64_MSR_GUEST_OS_ID ,0 ); --
Quoted 1 line
1.8.3.1
Dexuan Cui
maintainerPATCH 2/7 clocksource/drivers: Suspend/resume Hyper-V clocksource for hibernation
In reply to Dexuan CuiThis is needed for hibernation, e.g. when we resume the old kernel, we need to disable the "current" kernel's TSC page and then resume the old kernel's.
--- drivers/clocksource/hyperv_timer.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)
drivers/clocksource/hyperv_timer.c +25 -0
@@ -237,12 +237,37 @@ static u64 read_hv_clock_tsc(struct clocksource *arg)return read_hv_sched_clock_tsc (); } +static void suspend_hv_clock_tsc (struct clocksource *arg) +{ +u64 tsc_msr; + +/* Disable the TSC page */ +hv_get_reference_tsc (tsc_msr); + tsc_msr &= ~BIT_ULL (0 ); +hv_set_reference_tsc (tsc_msr); +} + + +static void resume_hv_clock_tsc (struct clocksource *arg) +{ +phys_addr_t phys_addr =page_to_phys (vmalloc_to_page (tsc_pg)); +u64 tsc_msr; + +/* Re-enable the TSC page */ +hv_get_reference_tsc (tsc_msr); + tsc_msr &=GENMASK_ULL (11 ,0 ); + tsc_msr |=BIT_ULL (0 ) | (u64 )phys_addr; +hv_set_reference_tsc (tsc_msr); +} +static struct clocksource hyperv_cs_tsc = { .name ="hyperv_clocksource_tsc_page" , .rating =400 , .read = read_hv_clock_tsc, .mask =CLOCKSOURCE_MASK (64 ), .flags =CLOCK_SOURCE_IS_CONTINUOUS , + .suspend= suspend_hv_clock_tsc, + .resume = resume_hv_clock_tsc, };#endif
Signature · 1 line
1.8.3.1
RE: [PATCH 2/7] clocksource/drivers: Suspend/resume Hyper-V clocksource for hibernation
From: Dexuan Cui <[email protected]> Sent: Monday, July 8, 2019 10:29 PM
Quoted 52 lines
This is needed for hibernation, e.g. when we resume the old kernel, we need to disable the "current" kernel's TSC page and then resume the old kernel's.
Signed-off-by Dexuan Cui <[email protected]>--- drivers/clocksource/hyperv_timer.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)drivers/clocksource/hyperv_timer.c +25
-1@@ -237,12 +237,37 @@ static u64 read_hv_clock_tsc(struct clocksource *arg)return read_hv_sched_clock_tsc (); } +static void suspend_hv_clock_tsc (struct clocksource *arg) +{ +u64 tsc_msr; + +/* Disable the TSC page */ +hv_get_reference_tsc (tsc_msr); + tsc_msr &= ~BIT_ULL (0 ); +hv_set_reference_tsc (tsc_msr); +} + + +static void resume_hv_clock_tsc (struct clocksource *arg) +{ +phys_addr_t phys_addr =page_to_phys (vmalloc_to_page (tsc_pg)); +u64 tsc_msr; + +/* Re-enable the TSC page */ +hv_get_reference_tsc (tsc_msr); + tsc_msr &=GENMASK_ULL (11 ,0 ); + tsc_msr |=BIT_ULL (0 ) | (u64 )phys_addr; +hv_set_reference_tsc (tsc_msr); +} +static struct clocksource hyperv_cs_tsc = { .name ="hyperv_clocksource_tsc_page" , .rating =400 , .read = read_hv_clock_tsc, .mask =CLOCKSOURCE_MASK (64 ), .flags =CLOCK_SOURCE_IS_CONTINUOUS , + .suspend= suspend_hv_clock_tsc, + .resume = resume_hv_clock_tsc, };#endif --
Quoted 1 line
1.8.3.1
Dexuan Cui
maintainerPATCH 3/7 Drivers: hv: vmbus: Split hv_synic_init/cleanup into regs and timer settings
In reply to Dexuan CuiThere is only one functional change: the unnecessary check
"if (sctrl.enable != 1) return -EFAULT;" is removed, because when we're in hv_synic_cleanup(), we're absolutely sure sctrl.enable must be 1.
The new functions hv_synic_disable/enable_regs() will be used by a later patch to support hibernation.
--- drivers/hv/hv.c | 66 ++++++++++++++++++++++++++--------------------- drivers/hv/hyperv_vmbus.h | 2 ++ 2 files changed, 39 insertions(+), 29 deletions(-)
drivers/hv/hv.c +37 -29
@@ -154,7 +154,7 @@ void hv_synic_free(void)* retrieve the initialized message and event pages. Otherwise, we create and * initialize the message and event pages. */ -int hv_synic_init (unsigned int cpu) +void hv_synic_enable_regs (unsigned int cpu) {struct hv_per_cpu_context *hv_cpu =per_cpu_ptr (hv_context.cpu_context, cpu);
@@ -196,6 +196,11 @@ int hv_synic_init(unsigned int cpu) sctrl.enable =1 ;hv_set_synic_state (sctrl.as_uint64); +} + +int hv_synic_init (unsigned int cpu) +{ +hv_synic_enable_regs (cpu);hv_stimer_init (cpu);
@@ -205,20 +210,45 @@ int hv_synic_init(unsigned int cpu)/* * hv_synic_cleanup - Cleanup routine for hv_synic_init(). */ -int hv_synic_cleanup (unsigned int cpu) +void hv_synic_disable_regs (unsigned int cpu) {union hv_synic_sint shared_sint;union hv_synic_simp simp;union hv_synic_siefp siefp;union hv_synic_scontrol sctrl; + +hv_get_synint_state (VMBUS_MESSAGE_SINT , shared_sint.as_uint64); + + shared_sint.masked =1 ; + +/* Need to correctly cleanup in the case of SMP!!! */ +/* Disable the interrupt */ +hv_set_synint_state (VMBUS_MESSAGE_SINT , shared_sint.as_uint64); + +hv_get_simp (simp.as_uint64); + simp.simp_enabled =0 ; + simp.base_simp_gpa =0 ; + +hv_set_simp (simp.as_uint64); + +hv_get_siefp (siefp.as_uint64); + siefp.siefp_enabled =0 ; + siefp.base_siefp_gpa =0 ; + +hv_set_siefp (siefp.as_uint64); + +/* Disable the global synic bit */ +hv_get_synic_state (sctrl.as_uint64); + sctrl.enable =0 ; +hv_set_synic_state (sctrl.as_uint64); +} + +int hv_synic_cleanup (unsigned int cpu) +{struct vmbus_channel *channel, *sc;bool channel_found = false;unsigned long flags; -hv_get_synic_state (sctrl.as_uint64); -if (sctrl.enable !=1 ) -return -EFAULT ; -/* * Search for channels which are bound to the CPU we're about to * cleanup. In case we find one and vmbus is still connected we need to
@@ -249,29 +279,7 @@ int hv_synic_cleanup(unsigned int cpu)hv_stimer_cleanup (cpu); -hv_get_synint_state (VMBUS_MESSAGE_SINT , shared_sint.as_uint64); - - shared_sint.masked =1 ; - -/* Need to correctly cleanup in the case of SMP!!! */ -/* Disable the interrupt */ -hv_set_synint_state (VMBUS_MESSAGE_SINT , shared_sint.as_uint64); - -hv_get_simp (simp.as_uint64); - simp.simp_enabled =0 ; - simp.base_simp_gpa =0 ; - -hv_set_simp (simp.as_uint64); - -hv_get_siefp (siefp.as_uint64); - siefp.siefp_enabled =0 ; - siefp.base_siefp_gpa =0 ; - -hv_set_siefp (siefp.as_uint64); - -/* Disable the global synic bit */ - sctrl.enable =0 ; -hv_set_synic_state (sctrl.as_uint64); +hv_synic_disable_regs (cpu);return 0 ; }
drivers/hv/hyperv_vmbus.h +2 -0
@@ -171,8 +171,10 @@ extern int hv_post_message(union hv_connection_id connection_id,extern void hv_synic_free (void ); +extern void hv_synic_enable_regs (unsigned int cpu);extern int hv_synic_init (unsigned int cpu); +extern void hv_synic_disable_regs (unsigned int cpu);extern int hv_synic_cleanup (unsigned int cpu);/* Interface */
Signature · 1 line
1.8.3.1
RE: [PATCH 3/7] Drivers: hv: vmbus: Split hv_synic_init/cleanup into regs and timer settings
From: Dexuan Cui <[email protected]> Sent: Monday, July 8, 2019 10:29 PM
Quoted 7 lines
There is only one functional change: the unnecessary check
"if (sctrl.enable != 1) return-EFAULT;" is removed, because when we're inhv_synic_cleanup(), we're absolutely sure sctrl.enable must be 1.The new functions hv_synic_disable/enable_regs() will be used by a later patch to support hibernation.
Seems like this commit message doesn't really describe the main change. How about:
Break out synic enable and disable operations into separate hv_synic_disable_regs() and hv_synic_enable_regs() functions for use by a later patch to support hibernation.
There is no functional change except the unnecessary check
"if (sctrl.enable != 1) return -EFAULT;" is removed, because when we're in hv_synic_cleanup(), we're absolutely sure sctrl.enable must be 1.
Otherwise,
Quoted 132 lines
Signed-off-by Dexuan Cui <[email protected]>--- drivers/hv/hv.c | 66 ++++++++++++++++++++++++++--------------------- drivers/hv/hyperv_vmbus.h | 2 ++ 2 files changed, 39 insertions(+), 29 deletions(-)drivers/hv/hv.c +37
-29@@ -154,7 +154,7 @@ void hv_synic_free(void)* retrieve the initialized message and event pages. Otherwise, we create and * initialize the message and event pages. */ -int hv_synic_init (unsigned int cpu) +void hv_synic_enable_regs (unsigned int cpu) {struct hv_per_cpu_context *hv_cpu =per_cpu_ptr (hv_context.cpu_context, cpu);@@ -196,6 +196,11 @@ int hv_synic_init(unsigned int cpu) sctrl.enable =1 ;hv_set_synic_state (sctrl.as_uint64); +} + +int hv_synic_init (unsigned int cpu) +{ +hv_synic_enable_regs (cpu);hv_stimer_init (cpu);@@ -205,20 +210,45 @@ int hv_synic_init(unsigned int cpu)/* * hv_synic_cleanup - Cleanup routine for hv_synic_init(). */ -int hv_synic_cleanup (unsigned int cpu) +void hv_synic_disable_regs (unsigned int cpu) {union hv_synic_sint shared_sint;union hv_synic_simp simp;union hv_synic_siefp siefp;union hv_synic_scontrol sctrl; + +hv_get_synint_state (VMBUS_MESSAGE_SINT , shared_sint.as_uint64); + + shared_sint.masked =1 ; + +/* Need to correctly cleanup in the case of SMP!!! */ +/* Disable the interrupt */ +hv_set_synint_state (VMBUS_MESSAGE_SINT , shared_sint.as_uint64); + +hv_get_simp (simp.as_uint64); + simp.simp_enabled =0 ; + simp.base_simp_gpa =0 ; + +hv_set_simp (simp.as_uint64); + +hv_get_siefp (siefp.as_uint64); + siefp.siefp_enabled =0 ; + siefp.base_siefp_gpa =0 ; + +hv_set_siefp (siefp.as_uint64); + +/* Disable the global synic bit */ +hv_get_synic_state (sctrl.as_uint64); + sctrl.enable =0 ; +hv_set_synic_state (sctrl.as_uint64); +} + +int hv_synic_cleanup (unsigned int cpu) +{struct vmbus_channel *channel, *sc;bool channel_found = false;unsigned long flags; -hv_get_synic_state (sctrl.as_uint64); -if (sctrl.enable !=1 ) -return -EFAULT ; -/* * Search for channels which are bound to the CPU we're about to * cleanup. In case we find one and vmbus is still connected we need to @@ -249,29 +279,7 @@ int hv_synic_cleanup(unsigned int cpu)hv_stimer_cleanup (cpu); -hv_get_synint_state (VMBUS_MESSAGE_SINT , shared_sint.as_uint64); - - shared_sint.masked =1 ; - -/* Need to correctly cleanup in the case of SMP!!! */ -/* Disable the interrupt */ -hv_set_synint_state (VMBUS_MESSAGE_SINT , shared_sint.as_uint64); - -hv_get_simp (simp.as_uint64); - simp.simp_enabled =0 ; - simp.base_simp_gpa =0 ; - -hv_set_simp (simp.as_uint64); - -hv_get_siefp (siefp.as_uint64); - siefp.siefp_enabled =0 ; - siefp.base_siefp_gpa =0 ; - -hv_set_siefp (siefp.as_uint64); - -/* Disable the global synic bit */ - sctrl.enable =0 ; -hv_set_synic_state (sctrl.as_uint64); +hv_synic_disable_regs (cpu);return 0 ; }drivers/hv/hyperv_vmbus.h +2
-0@@ -171,8 +171,10 @@ extern int hv_post_message(union hv_connection_id connection_id, extern void hv_synic_free(void); +extern void hv_synic_enable_regs (unsigned int cpu);extern int hv_synic_init (unsigned int cpu); +extern void hv_synic_disable_regs (unsigned int cpu);extern int hv_synic_cleanup (unsigned int cpu);/* Interface */
--
1.8.3.1
Dexuan Cui
maintainerRE: [PATCH 3/7] Drivers: hv: vmbus: Split hv_synic_init/cleanup into regs and timer settings
Quoted 21 lines
From: Michael Kelley <[email protected]>
Sent: Tuesday, July 30, 2019 3:36 PMFrom: Dexuan Cui <[email protected]> Sent: Monday, July 8, 2019 10:29 PM
Quoted 6 lines
There is only one functional change: the unnecessary check
"if (sctrl.enable != 1) return-EFAULT;" is removed, because when we're inhv_synic_cleanup(), we're absolutely sure sctrl.enable must be 1.The new functions hv_synic_disable/enable_regs() will be used by a later
patch
Quoted 1 line
to support hibernation.
Seems like this commit message doesn't really describe the main change. How about:
Break out synic enable and disable operations into separate
hv_synic_disable_regs()andhv_synic_enable_regs()functions for use by a later patch to support hibernation.
Quoted 7 lines
There is no functional change except the unnecessary check
"if (sctrl.enable != 1) return-EFAULT;" is removed, because when we're inhv_synic_cleanup(), we're absolutely sure sctrl.enable must be 1.Otherwise,
Reviewed-by Michael Kelley <[email protected]>
Thanks! I'll use your version as the changelog of v2. I'll change the Subject accordingly.
Thanks,
-- Dexuan
Dexuan Cui
maintainerPATCH 4/7 Drivers: hv: vmbus: Suspend/resume the synic for hibernation
In reply to Dexuan CuiThis is needed when we resume the old kernel from the "current" kernel.
--- drivers/hv/vmbus_drv.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+)
drivers/hv/vmbus_drv.c +40 -0
@@ -30,6 +30,7 @@#include <linux/kdebug.h> #include <linux/efi.h> #include <linux/random.h> +#include <linux/syscore_ops.h> #include <clocksource/hyperv_timer.h> #include "hyperv_vmbus.h"
@@ -2088,6 +2089,41 @@ static void hv_crash_handler(struct pt_regs *regs)hyperv_cleanup (); }; +static int hv_synic_suspend (void ) +{ +/* +* Here we only need to care about CPU0: when the other CPUs are +* offlined, hv_synic_cleanup() has been called for them, and the +* timers on them have been automatically disabled and deleted in +* tick_cleanup_dead_cpu(). +*/ +hv_stimer_cleanup (0 ); + +hv_synic_disable_regs (0 ); + +return 0 ; +} + +static void hv_synic_resume (void ) +{ +/* +* Here we only need to care about CPU0: when the other CPUs are +* onlined, hv_synic_init() has been called, and the timers are +* added there. +* +* Note: we don't need to call hv_stimer_init() for stimer0, because +* it is not deleted before hibernation and it's resumed in +* timekeeping_resume(). +*/ +hv_synic_enable_regs (0 ); +} + +/* The callbacks run only on CPU0, with irqs_disabled. */ +static struct syscore_ops hv_synic_syscore_ops = { + .suspend = hv_synic_suspend, + .resume = hv_synic_resume, +}; +static int __init hv_acpi_init (void ) {int ret, t;
@@ -2118,6 +2154,8 @@ static int __init hv_acpi_init(void)hv_setup_kexec_handler (hv_kexec_handler);hv_setup_crash_handler (hv_crash_handler); +register_syscore_ops (&hv_synic_syscore_ops); +return 0 ; cleanup:
@@ -2130,6 +2168,8 @@ static void __exit vmbus_exit(void) {int cpu; +unregister_syscore_ops (&hv_synic_syscore_ops); +hv_remove_kexec_handler ();hv_remove_crash_handler (); vmbus_connection.conn_state =DISCONNECTED ;
Signature · 1 line
1.8.3.1
Dexuan Cui
maintainerPATCH 5/7 Drivers: hv: vmbus: Ignore the offers when resuming from hibernation
In reply to Dexuan CuiWhen the VM resumes, the host re-sends the offers. We should not add the offers to the global vmbus_connection.chn_list again.
Added some debug code, in case the host screws up the exact info related to the offers.
--- drivers/hv/channel_mgmt.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-)
drivers/hv/channel_mgmt.c +27 -1
@@ -854,12 +854,38 @@ void vmbus_initiate_unload(bool crash)static void vmbus_onoffer (struct vmbus_channel_message_header *hdr) {struct vmbus_channel_offer_channel *offer; -struct vmbus_channel *newchannel; +struct vmbus_channel *oldchannel, *newchannel; +size_t offer_sz; offer = (struct vmbus_channel_offer_channel *)hdr;trace_vmbus_onoffer (offer); +mutex_lock (&vmbus_connection.channel_mutex); + oldchannel =relid2channel (offer->child_relid); +mutex_unlock (&vmbus_connection.channel_mutex); + +if (oldchannel !=NULL ) { +atomic_dec (&vmbus_connection.offer_in_progress); + +/* +* We're resuming from hibernation: we expect the host to send +* exactly the same offers that we had before the hibernation. +*/ + offer_sz =sizeof (*offer); +if (memcmp (offer, &oldchannel->offermsg, offer_sz) ==0 ) +return ; + +pr_err ("Mismatched offer from the host (relid=%d)!\n" , + offer->child_relid); + +print_hex_dump_debug ("Old vmbus offer: " ,DUMP_PREFIX_OFFSET ,4 , +4 , &oldchannel->offermsg, offer_sz, false); +print_hex_dump_debug ("New vmbus offer: " ,DUMP_PREFIX_OFFSET ,4 , +4 , offer, offer_sz, false); +return ; + } +/* Allocate the channel object and save this offer. */ newchannel =alloc_channel ();if (!newchannel) {
Signature · 1 line
1.8.3.1
RE: [PATCH 5/7] Drivers: hv: vmbus: Ignore the offers when resuming from hibernation
From: Dexuan Cui <[email protected]> Sent: Monday, July 8, 2019 10:29 PM
Quoted 32 lines
When the VM resumes, the host re-sends the offers. We should not add the offers to the global vmbus_connection.chn_list again.
Added some debug code, in case the host screws up the exact info related to the offers.
Signed-off-by Dexuan Cui <[email protected]>--- drivers/hv/channel_mgmt.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-)drivers/hv/channel_mgmt.c +6
-1@@ -854,12 +854,38 @@ void vmbus_initiate_unload(bool crash)static void vmbus_onoffer (struct vmbus_channel_message_header *hdr) {struct vmbus_channel_offer_channel *offer; -struct vmbus_channel *newchannel; +struct vmbus_channel *oldchannel, *newchannel; +size_t offer_sz; offer = (struct vmbus_channel_offer_channel *)hdr;trace_vmbus_onoffer (offer); +mutex_lock (&vmbus_connection.channel_mutex); + oldchannel =relid2channel (offer->child_relid); +mutex_unlock (&vmbus_connection.channel_mutex); +
drivers/hv/channel_mgmt.c void vmbus_initiate_unload(bool crash) · line 877
Quoted 10 lines
drivers/hv/channel_mgmt.c +10
-0+if (oldchannel !=NULL ) { +atomic_dec (&vmbus_connection.offer_in_progress); + +/* +* We're resuming from hibernation: we expect the host to send +* exactly the same offers that we had before the hibernation. +*/ + offer_sz =sizeof (*offer); +if (memcmp (offer, &oldchannel->offermsg, offer_sz) ==0 ) +return ;
The offermsg contains "reserved" and "padding" fields. Does Hyper-V guarantee that all these fields are the same in the new offer after resuming from hibernation? Or should a less stringent check be made? For example, I could imagine a newer version of Hyper-V allowing a VM that was hibernated on an older version to be resumed. But one of the reserved fields might be used in the newer version, and the comparison could fail unnecessarily.
drivers/hv/channel_mgmt.c void vmbus_initiate_unload(bool crash) · line 885
Quoted 8 lines
drivers/hv/channel_mgmt.c +8
-0+ +pr_err ("Mismatched offer from the host (relid=%d)!\n" , + offer->child_relid); + +print_hex_dump_debug ("Old vmbus offer: " ,DUMP_PREFIX_OFFSET ,4 , +4 , &oldchannel->offermsg, offer_sz, false); +print_hex_dump_debug ("New vmbus offer: " ,DUMP_PREFIX_OFFSET ,4 , +4 , offer, offer_sz, false);
The third argument to print_hex_dump() is the rowsize and is specified as must be 16 or 32.
Quoted 8 lines
drivers/hv/channel_mgmt.c +3
-1+return ; + } +/* Allocate the channel object and save this offer. */ newchannel =alloc_channel ();if (!newchannel) { --1.8.3.1
Dexuan Cui
maintainerRE: [PATCH 5/7] Drivers: hv: vmbus: Ignore the offers when resuming from hibernation
Quoted 17 lines
From: Michael Kelley <[email protected]>
Sent: Tuesday, July 30, 2019 4:07 PMdrivers/hv/channel_mgmt.c void vmbus_initiate_unload(bool crash) · line 877
Quoted 11 lines
drivers/hv/channel_mgmt.c +11
-0+ +if (oldchannel !=NULL ) { +atomic_dec (&vmbus_connection.offer_in_progress); + +/* +* We're resuming from hibernation: we expect the host to send +* exactly the same offers that we had before the hibernation. +*/ + offer_sz =sizeof (*offer); +if (memcmp (offer, &oldchannel->offermsg, offer_sz) ==0 ) +return ;The offermsg contains "reserved" and "padding" fields. Does Hyper-V guarantee that all these fields are the same in the new offer after resuming from hibernation?
Yes. I confirmed this with Hyper-V team. The reserved/padding fields don't change across hiberantion. BTW, the fields are filled with zeros since they're not used.
Quoted 5 lines
Or should a less stringent check be made? For example,
I could imagine a newer version of Hyper-V allowing a VM that was hibernated on an older version to be resumed. But one of the reserved fields might be used in the newer version, and the comparison could fail unnecessarily.
Upon resume, Linux VM always uses the same version, which was used when the VM firstly booted up before suspend, to re-negotiate with the host.
Quoted 6 lines
drivers/hv/channel_mgmt.c void vmbus_initiate_unload(bool crash)
Quoted 5 lines
drivers/hv/channel_mgmt.c +5
-0+ +pr_err ("Mismatched offer from the host (relid=%d)!\n" , + offer->child_relid); + + print_hex_dump_debug("Old vmbus offer: ", DUMP_PREFIX_OFFSET,4,
Quoted 7 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ 4, &oldchannel->offermsg, offer_sz, false); + print_hex_dump_debug("New vmbus offer: ",
DUMP_PREFIX_OFFSET, 4,drivers/hv/channel_mgmt.c void vmbus_initiate_unload(bool crash) · line 885
Quoted 1 line
drivers/hv/channel_mgmt.c +1
-0+ 4, offer, offer_sz, false);The third argument to
print_hex_dump()is the rowsize and is specified as must be 16 or 32.
Thanks! I misunderstood the argument. I'll change it to 16.
Thanks,
-- Dexuan
Dexuan Cui
maintainerPATCH 6/7 Drivers: hv: vmbus: Suspend/resume the vmbus itself for hibernation
In reply to Dexuan CuiThis is needed when we resume the old kernel from the "current" kernel.
--- drivers/hv/connection.c | 3 +-- drivers/hv/hyperv_vmbus.h | 2 ++ drivers/hv/vmbus_drv.c | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-)
drivers/hv/connection.c +1 -2
@@ -59,8 +59,7 @@ static __u32 vmbus_get_next_version(__u32 current_version) } } -static int vmbus_negotiate_version (struct vmbus_channel_msginfo *msginfo, -__u32 version) +int vmbus_negotiate_version (struct vmbus_channel_msginfo *msginfo,u32 version) {int ret =0 ;unsigned int cur_cpu;
drivers/hv/hyperv_vmbus.h +2 -0
@@ -274,6 +274,8 @@ struct vmbus_msginfo {extern struct vmbus_connection vmbus_connection; +int vmbus_negotiate_version (struct vmbus_channel_msginfo *msginfo,u32 version); +static inline void vmbus_send_interrupt (u32 relid) {sync_set_bit (relid, vmbus_connection.send_int_page);
drivers/hv/vmbus_drv.c +40 -0
@@ -2045,6 +2045,41 @@ static int vmbus_acpi_add(struct acpi_device *device)return ret_val; } +static int vmbus_bus_suspend (struct device *dev) +{ +vmbus_initiate_unload (false); + + vmbus_connection.conn_state =DISCONNECTED ; + +return 0 ; +} + +static int vmbus_bus_resume (struct device *dev) +{ +struct vmbus_channel_msginfo *msginfo; +size_t msgsize; +int ret; + + msgsize =sizeof (*msginfo) + +sizeof (struct vmbus_channel_initiate_contact ); + + msginfo =kzalloc (msgsize,GFP_KERNEL ); + +if (msginfo ==NULL ) +return -ENOMEM ; + + ret =vmbus_negotiate_version (msginfo, vmbus_proto_version); + +kfree (msginfo); + +if (ret !=0 ) +return ret; + +vmbus_request_offers (); + +return 0 ; +} +static const struct acpi_device_id vmbus_acpi_device_ids[] = { {"VMBUS" ,0 }, {"VMBus" ,0 },
@@ -2052,6 +2087,10 @@ static int vmbus_acpi_add(struct acpi_device *device) };MODULE_DEVICE_TABLE (acpi, vmbus_acpi_device_ids); +static const struct dev_pm_ops vmbus_bus_pm = { +SET_SYSTEM_SLEEP_PM_OPS (vmbus_bus_suspend, vmbus_bus_resume) +}; +static struct acpi_driver vmbus_acpi_driver = { .name ="vmbus" , .ids = vmbus_acpi_device_ids,
@@ -2059,6 +2098,7 @@ static int vmbus_acpi_add(struct acpi_device *device) .add = vmbus_acpi_add, .remove = vmbus_acpi_remove, }, + .drv.pm = &vmbus_bus_pm, };static void hv_kexec_handler (void )
Signature · 1 line
1.8.3.1
RE: [PATCH 6/7] Drivers: hv: vmbus: Suspend/resume the vmbus itself for hibernation
From: Dexuan Cui <[email protected]> Sent: Monday, July 8, 2019 10:30 PM
Quoted 2 lines
This is needed when we resume the old kernel from the "current" kernel.
Perhaps a bit more descriptive commit message could be supplied?
Quoted 57 lines
Signed-off-by Dexuan Cui <[email protected]>--- drivers/hv/connection.c | 3 +-- drivers/hv/hyperv_vmbus.h | 2 ++ drivers/hv/vmbus_drv.c | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-)drivers/hv/connection.c +1
-2@@ -59,8 +59,7 @@ static __u32 vmbus_get_next_version(__u32 current_version) } } -static int vmbus_negotiate_version (struct vmbus_channel_msginfo *msginfo, -__u32 version) +int vmbus_negotiate_version (struct vmbus_channel_msginfo *msginfo,u32 version) {int ret =0 ;unsigned int cur_cpu;drivers/hv/hyperv_vmbus.h +2
-0@@ -274,6 +274,8 @@ struct vmbus_msginfo {extern struct vmbus_connection vmbus_connection; +int vmbus_negotiate_version (struct vmbus_channel_msginfo *msginfo,u32 version); +static inline void vmbus_send_interrupt (u32 relid) {sync_set_bit (relid, vmbus_connection.send_int_page);drivers/hv/vmbus_drv.c +14
-0@@ -2045,6 +2045,41 @@ static int vmbus_acpi_add(struct acpi_device *device)return ret_val; } +static int vmbus_bus_suspend (struct device *dev) +{ +vmbus_initiate_unload (false); + + vmbus_connection.conn_state =DISCONNECTED ; + +return 0 ; +} + +static int vmbus_bus_resume (struct device *dev) +{ +struct vmbus_channel_msginfo *msginfo; +size_t msgsize; +int ret;
drivers/hv/vmbus_drv.c static int vmbus_acpi_add(struct acpi_device *device) · line 2071
Quoted 10 lines
drivers/hv/vmbus_drv.c +10
-0+ + msgsize =sizeof (*msginfo) + +sizeof (struct vmbus_channel_initiate_contact ); + + msginfo =kzalloc (msgsize,GFP_KERNEL ); + +if (msginfo ==NULL ) +return -ENOMEM ; + + ret =vmbus_negotiate_version (msginfo, vmbus_proto_version);
I think this code answers my earlier question: Upon resume, we negotiate the same VMbus protocol version we had at time of hibernation, even if running on a newer version of Hyper-V that might support newer protocol versions. Hence the offer
messages should not have any previously reserved fields now being used. A
comment to this effect would be useful.
Quoted 35 lines
drivers/hv/vmbus_drv.c +16
-1+ +kfree (msginfo); + +if (ret !=0 ) +return ret; + +vmbus_request_offers (); + +return 0 ; +} +static const struct acpi_device_id vmbus_acpi_device_ids[] = { {"VMBUS" ,0 }, {"VMBus" ,0 },@@ -2052,6 +2087,10 @@ static int vmbus_acpi_add(struct acpi_device *device) };MODULE_DEVICE_TABLE (acpi, vmbus_acpi_device_ids); +static const struct dev_pm_ops vmbus_bus_pm = { +SET_SYSTEM_SLEEP_PM_OPS (vmbus_bus_suspend, vmbus_bus_resume) +}; +static struct acpi_driver vmbus_acpi_driver = { .name ="vmbus" , .ids = vmbus_acpi_device_ids,@@ -2059,6 +2098,7 @@ static int vmbus_acpi_add(struct acpi_device *device) .add = vmbus_acpi_add, .remove = vmbus_acpi_remove, }, + .drv.pm = &vmbus_bus_pm, };static void hv_kexec_handler (void ) --1.8.3.1
Dexuan Cui
maintainerRE: [PATCH 6/7] Drivers: hv: vmbus: Suspend/resume the vmbus itself for hibernation
Quoted 8 lines
From: Michael Kelley <[email protected]>
Sent: Tuesday, July 30, 2019 4:26 PM
From: Dexuan Cui <[email protected]> Sent: Monday, July 8, 2019 10:30 PMQuoted 2 lines
This is needed when we resume the old kernel from the "current" kernel.
Perhaps a bit more descriptive commit message could be supplied?
I'll use this as the new changelog:
Before Linux enters hibernation, it sends the CHANNELMSG_UNLOAD message to the host so all the offers are gone. After hibernation, Linux needs to re-negotiate with the host using the same vmbus protocol version (which was in use before hibernation), and ask the host to re-offer the vmbus devices.
Quoted 11 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ + ret = vmbus_negotiate_version(msginfo, vmbus_proto_version);I think this code answers my earlier question: Upon resume, we negotiate the same
VMbus protocol version we had at time of hibernation, even if running on a newer
version of Hyper-V that might support newer protocol versions. Hence the offer
messages should not have any previously reserved fields now being used. A
comment to this effect would be useful.
Ok, let me add a comment before the line.
I'll post a v2 of the patchset, including the "[PATCH 4/7]", which needs a small change.
Thanks,
-- Dexuan
Dexuan Cui
maintainerPATCH 7/7 Drivers: hv: vmbus: Implement suspend/resume for VSC drivers for hibernation
In reply to Dexuan CuiThe high-level VSC drivers will implement device-specific callbacks.
--- drivers/hv/vmbus_drv.c | 42 ++++++++++++++++++++++++++++++++++++++++++ include/linux/hyperv.h | 3 +++ 2 files changed, 45 insertions(+)
drivers/hv/vmbus_drv.c +42 -0
@@ -911,6 +911,43 @@ static void vmbus_shutdown(struct device *child_device) drv->shutdown (dev); } +/* +* vmbus_suspend - Suspend a vmbus device +*/ +static int vmbus_suspend (struct device *child_device) +{ +struct hv_driver *drv; +struct hv_device *dev =device_to_hv_device (child_device); + +/* The device may not be attached yet */ +if (!child_device->driver) +return 0 ; + + drv =drv_to_hv_drv (child_device->driver); +if (!drv->suspend) +return -EOPNOTSUPP ; + +return drv->suspend (dev); +} + +/* +* vmbus_resume - Resume a vmbus device +*/ +static int vmbus_resume (struct device *child_device) +{ +struct hv_driver *drv; +struct hv_device *dev =device_to_hv_device (child_device); + +/* The device may not be attached yet */ +if (!child_device->driver) +return 0 ; + + drv =drv_to_hv_drv (child_device->driver); +if (!drv->resume) +return -EOPNOTSUPP ; + +return drv->resume (dev); +}/* * vmbus_device_release - Final callback release of the vmbus child device
@@ -926,6 +963,10 @@ static void vmbus_device_release(struct device *device)kfree (hv_dev); } +static const struct dev_pm_ops vmbus_pm = { +SET_SYSTEM_SLEEP_PM_OPS (vmbus_suspend, vmbus_resume) +}; +/* The one and only one */ static struct bus_type hv_bus = { .name ="vmbus" ,
@@ -936,6 +977,7 @@ static void vmbus_device_release(struct device *device) .uevent = vmbus_uevent, .dev_groups = vmbus_dev_groups, .drv_groups = vmbus_drv_groups, + .pm = &vmbus_pm, };struct onmessage_work_context {
include/linux/hyperv.h +3 -0
@@ -1149,6 +1149,9 @@ struct hv_driver {int (*remove)(struct hv_device *);void (*shutdown)(struct hv_device *); +int (*suspend)(struct hv_device *); +int (*resume)(struct hv_device *); + };/* Base device object */
Signature · 1 line
1.8.3.1
RE: [PATCH 7/7] Drivers: hv: vmbus: Implement suspend/resume for VSC drivers for hibernation
From: Dexuan Cui <[email protected]> Sent: Monday, July 8, 2019 10:30 PM
Quoted 91 lines
The high-level VSC drivers will implement device-specific callbacks.
Signed-off-by Dexuan Cui <[email protected]>--- drivers/hv/vmbus_drv.c | 42 ++++++++++++++++++++++++++++++++++++++++++ include/linux/hyperv.h | 3 +++ 2 files changed, 45 insertions(+)drivers/hv/vmbus_drv.c +42
-0@@ -911,6 +911,43 @@ static void vmbus_shutdown(struct device *child_device) drv->shutdown (dev); } +/* +* vmbus_suspend - Suspend a vmbus device +*/ +static int vmbus_suspend (struct device *child_device) +{ +struct hv_driver *drv; +struct hv_device *dev =device_to_hv_device (child_device); + +/* The device may not be attached yet */ +if (!child_device->driver) +return 0 ; + + drv =drv_to_hv_drv (child_device->driver); +if (!drv->suspend) +return -EOPNOTSUPP ; + +return drv->suspend (dev); +} + +/* +* vmbus_resume - Resume a vmbus device +*/ +static int vmbus_resume (struct device *child_device) +{ +struct hv_driver *drv; +struct hv_device *dev =device_to_hv_device (child_device); + +/* The device may not be attached yet */ +if (!child_device->driver) +return 0 ; + + drv =drv_to_hv_drv (child_device->driver); +if (!drv->resume) +return -EOPNOTSUPP ; + +return drv->resume (dev); +}/* * vmbus_device_release - Final callback release of the vmbus child device @@ -926,6 +963,10 @@ static void vmbus_device_release(struct device *device)kfree (hv_dev); } +static const struct dev_pm_ops vmbus_pm = { +SET_SYSTEM_SLEEP_PM_OPS (vmbus_suspend, vmbus_resume) +}; +/* The one and only one */ static struct bus_type hv_bus = { .name ="vmbus" ,@@ -936,6 +977,7 @@ static void vmbus_device_release(struct device *device) .uevent = vmbus_uevent, .dev_groups = vmbus_dev_groups, .drv_groups = vmbus_drv_groups, + .pm = &vmbus_pm, };struct onmessage_work_context {include/linux/hyperv.h +3
-1@@ -1149,6 +1149,9 @@ struct hv_driver {int (*remove)(struct hv_device *);void (*shutdown)(struct hv_device *); +int (*suspend)(struct hv_device *); +int (*resume)(struct hv_device *); + };/* Base device object */ --
Quoted 1 line
1.8.3.1