Additional refactoring of Hyper-V arch specific code
Subsystems (4)
cover Additional refactoring of Hyper-V arch specific code
This patch set moves additional Hyper-V code under arch/x86 into arch-neutral hv_common.c where it can be shared by x86 and and ARM64 implementations. The move reduces the overall lines of code across both architectures, and removes code under arch/ that isn't really architecture-specific.
The code is moved into hv_common.c because it must be built-in to the kernel image, and not be part of a module.
No functional changes are intended.
Michael Kelley (3):
Drivers: hv: Make portions of Hyper-V init code be arch neutral
Drivers: hv: Add arch independent default functions for some Hyper-V
handlers
Drivers: hv: Move Hyper-V misc functionality to arch-neutral code
arch/x86/hyperv/hv_init.c | 101 +++----------------- arch/x86/include/asm/mshyperv.h | 4 - arch/x86/kernel/cpu/mshyperv.c | 24 ----- drivers/hv/hv_common.c | 198 ++++++++++++++++++++++++++++++++++++++++ include/asm-generic/mshyperv.h | 10 ++ 5 files changed, 219 insertions(+), 118 deletions(-)
Signature · 1 line
1.8.3.1
PATCH 1/3 Drivers: hv: Make portions of Hyper-V init code be arch neutral
The code to allocate and initialize the hv_vp_index array is architecture neutral. Similarly, the code to allocate and populate the hypercall input and output arg pages is architecture neutral. Move both sets of code out from arch/x86 and into utility functions in drivers/hv/hv_common.c that can be shared by Hyper-V initialization on ARM64.
No functional changes. However, the allocation of the hypercall input and output arg pages is done differently so that the size is always the Hyper-V page size, even if not the same as the guest page size (such as with ARM64's 64K page size).
--- arch/x86/hyperv/hv_init.c | 91 +++------------------------- arch/x86/include/asm/mshyperv.h | 4 -- arch/x86/kernel/cpu/mshyperv.c | 7 --- drivers/hv/hv_common.c | 131 ++++++++++++++++++++++++++++++++++++++++ include/asm-generic/mshyperv.h | 10 +++ 5 files changed, 151 insertions(+), 92 deletions(-)
arch/x86/hyperv/hv_init.c +10 -81
@@ -39,48 +39,17 @@/* Storage to save the hypercall page temporarily for hibernation */ static void *hv_hypercall_pg_saved; -u32 *hv_vp_index; -EXPORT_SYMBOL_GPL (hv_vp_index); -struct hv_vp_assist_page **hv_vp_assist_page;EXPORT_SYMBOL_GPL (hv_vp_assist_page); -void __percpu **hyperv_pcpu_input_arg; -EXPORT_SYMBOL_GPL (hyperv_pcpu_input_arg); - -void __percpu **hyperv_pcpu_output_arg; -EXPORT_SYMBOL_GPL (hyperv_pcpu_output_arg); - -u32 hv_max_vp_index; -EXPORT_SYMBOL_GPL (hv_max_vp_index); -static int hv_cpu_init (unsigned int cpu) { -u64 msr_vp_index;struct hv_vp_assist_page **hvp = &hv_vp_assist_page[smp_processor_id ()]; -void **input_arg; -struct page *pg; - -/* hv_cpu_init() can be called with IRQs disabled from hv_resume() */ - pg =alloc_pages (irqs_disabled () ?GFP_ATOMIC :GFP_KERNEL , hv_root_partition ?1 :0 ); -if (unlikely (!pg)) -return -ENOMEM ; - - input_arg = (void **)this_cpu_ptr (hyperv_pcpu_input_arg); - *input_arg =page_address (pg); -if (hv_root_partition) { -void **output_arg; - - output_arg = (void **)this_cpu_ptr (hyperv_pcpu_output_arg); - *output_arg =page_address (pg +1 ); - } - - msr_vp_index =hv_get_register (HV_REGISTER_VP_INDEX ); - - hv_vp_index[smp_processor_id ()] = msr_vp_index; +int ret; -if (msr_vp_index > hv_max_vp_index) - hv_max_vp_index = msr_vp_index; + ret =hv_common_cpu_init (cpu); +if (ret) +return ret;if (!hv_vp_assist_page)return 0 ;
@@ -198,25 +167,8 @@ static int hv_cpu_die(unsigned int cpu) {struct hv_reenlightenment_control re_ctrl;unsigned int new_cpu; -unsigned long flags; -void **input_arg; -void *pg; -local_irq_save (flags); - input_arg = (void **)this_cpu_ptr (hyperv_pcpu_input_arg); - pg = *input_arg; - *input_arg =NULL ; - -if (hv_root_partition) { -void **output_arg; - - output_arg = (void **)this_cpu_ptr (hyperv_pcpu_output_arg); - *output_arg =NULL ; - } - -local_irq_restore (flags); - -free_pages ((unsigned long )pg, hv_root_partition ?1 :0 ); +hv_common_cpu_die (cpu);if (hv_vp_assist_page && hv_vp_assist_page[cpu])wrmsrl (HV_X64_MSR_VP_ASSIST_PAGE ,0 );
@@ -368,7 +320,7 @@ void __init hyperv_init(void) {u64 guest_id, required_msrs;union hv_x64_msr_hypercall_contents hypercall_msr; -int cpuhp, i; +int cpuhp;if (x86_hyper_type !=X86_HYPER_MS_HYPERV )return ;
@@ -380,36 +332,14 @@ void __init hyperv_init(void)if ((ms_hyperv.features & required_msrs) != required_msrs)return ; -/* -* Allocate the per-CPU state for the hypercall input arg. -* If this allocation fails, we will not be able to setup -* (per-CPU) hypercall input page and thus this failure is -* fatal on Hyper-V. -*/ - hyperv_pcpu_input_arg = alloc_percpu(void *); - -BUG_ON (hyperv_pcpu_input_arg ==NULL ); - -/* Allocate the per-CPU state for output arg for root */ -if (hv_root_partition) { - hyperv_pcpu_output_arg = alloc_percpu(void *); -BUG_ON (hyperv_pcpu_output_arg ==NULL ); - } - -/* Allocate percpu VP index */ - hv_vp_index =kmalloc_array (num_possible_cpus (),sizeof (*hv_vp_index), -GFP_KERNEL ); -if (!hv_vp_index) +if (hv_common_init ())return ; -for (i =0 ; i <num_possible_cpus (); i++) - hv_vp_index[i] =VP_INVAL ; - hv_vp_assist_page =kcalloc (num_possible_cpus (),sizeof (*hv_vp_assist_page),GFP_KERNEL );if (!hv_vp_assist_page) { ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED ; - goto free_vp_index; + goto common_free; } cpuhp = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online",
@@ -507,9 +437,8 @@ void __init hyperv_init(void) free_vp_assist_page:kfree (hv_vp_assist_page); hv_vp_assist_page =NULL ; -free_vp_index: -kfree (hv_vp_index); - hv_vp_index =NULL ; +common_free: +hv_common_free (); }/*
arch/x86/include/asm/mshyperv.h +0 -4
@@ -36,8 +36,6 @@ static inline u64 hv_get_register(unsigned int reg)extern int hyperv_init_cpuhp;extern void *hv_hypercall_pg; -extern void __percpu **hyperv_pcpu_input_arg; -extern void __percpu **hyperv_pcpu_output_arg;extern u64 hv_current_partition_id;
@@ -170,8 +168,6 @@ int hyperv_fill_flush_guest_mapping_list(struct hv_guest_mapping_flush_list *flush, u64 start_gfn, u64 end_gfn); -extern bool hv_root_partition; -#ifdef CONFIG_X86_64 void hv_apic_init (void );void __init hv_init_spinlocks (void );
arch/x86/kernel/cpu/mshyperv.c +0 -7
@@ -34,13 +34,6 @@#include <clocksource/hyperv_timer.h> #include <asm/numa.h> -/* Is Linux running as the root partition? */ -bool hv_root_partition; -EXPORT_SYMBOL_GPL (hv_root_partition); - -struct ms_hyperv_info ms_hyperv; -EXPORT_SYMBOL_GPL (ms_hyperv); -#if IS_ENABLED (CONFIG_HYPERV )static void (*vmbus_handler)(void );static void (*hv_stimer0_handler)(void );
drivers/hv/hv_common.c +131 -0
141 lines not on this page · raw message
include/asm-generic/mshyperv.h +10 -0
@@ -38,6 +38,9 @@ struct ms_hyperv_info { };extern struct ms_hyperv_info ms_hyperv; +extern void __percpu **hyperv_pcpu_input_arg; +extern void __percpu **hyperv_pcpu_output_arg; +extern u64 hv_do_hypercall (u64 control,void *inputaddr,void *outputaddr);extern u64 hv_do_fast_hypercall8 (u16 control,u64 input8);
@@ -151,6 +154,8 @@ static inline void vmbus_signal_eom(struct hv_message *msg, u32 old_msg_type)extern int vmbus_interrupt;extern int vmbus_irq; +extern bool hv_root_partition; +#if IS_ENABLED (CONFIG_HYPERV )/* * Hypervisor's notion of virtual processor ID is different from
@@ -164,6 +169,11 @@ static inline void vmbus_signal_eom(struct hv_message *msg, u32 old_msg_type)/* Sentinel value for an uninitialized entry in hv_vp_index array */ #define VP_INVAL U32_MAX +int __init hv_common_init (void ); +void __init hv_common_free (void ); +int hv_common_cpu_init (unsigned int cpu); +int hv_common_cpu_die (unsigned int cpu); +void *hv_alloc_hyperv_page (void );void *hv_alloc_hyperv_zeroed_page (void );void hv_free_hyperv_page (unsigned long addr);
Signature · 1 line
1.8.3.1
Wei Liu
maintainerRe: [PATCH 1/3] Drivers: hv: Make portions of Hyper-V init code be arch neutral
On Sun, Jul 11, 2021 at 08:25:14PM -0700, Michael Kelley wrote:
[...]
drivers/hv/hv_common.c line 110
Quoted 12 lines
drivers/hv/hv_common.c +12
-0+int hv_common_cpu_init (unsigned int cpu) +{ +void **inputarg, **outputarg; +u64 msr_vp_index; +gfp_t flags; +int pgcount = hv_root_partition ?2 :1 ; + +/* hv_cpu_init() can be called with IRQs disabled from hv_resume() */ + flags =irqs_disabled () ?GFP_ATOMIC :GFP_KERNEL ; + + inputarg = (void **)this_cpu_ptr (hyperv_pcpu_input_arg); + *inputarg =kmalloc (pgcount *HV_HYP_PAGE_SIZE , flags);
This is changed from alloc_pages to kmalloc. Does it ensure the alignment is still correct?
Wei.
Wei Liu
maintainerRe: [PATCH 1/3] Drivers: hv: Make portions of Hyper-V init code be arch neutral
Quoted 17 lines · Wei Liu
On Sun, Jul 11, 2021 at 08:25:14PM -0700, Michael Kelley wrote:
[...]drivers/hv/hv_common.c line 110
Quoted 12 lines
drivers/hv/hv_common.c +12
-0+int hv_common_cpu_init (unsigned int cpu) +{ +void **inputarg, **outputarg; +u64 msr_vp_index; +gfp_t flags; +int pgcount = hv_root_partition ?2 :1 ; + +/* hv_cpu_init() can be called with IRQs disabled from hv_resume() */ + flags =irqs_disabled () ?GFP_ATOMIC :GFP_KERNEL ; + + inputarg = (void **)this_cpu_ptr (hyperv_pcpu_input_arg); + *inputarg =kmalloc (pgcount *HV_HYP_PAGE_SIZE , flags);This is changed from
alloc_pagesto kmalloc. Does it ensure the alignment is still correct?
kmalloc is rather complex and can be backed by either SLUB, SLAB or SLOB, all of which differ from the others.
I _think_ for large allocations (> 1 native page) they tend to pass the request on to the page allocator, but still there is a level of indirection.
If the host page size is 64KiB, while the allocation is only 4KiB or 8KiB, could there be a chance that they become misaligned?
Wei.
Quoted 2 lines
Wei.
RE: [PATCH 1/3] Drivers: hv: Make portions of Hyper-V init code be arch neutral
In reply to Wei LiuFrom: Wei Liu <[email protected]> Sent: Monday, July 12, 2021 11:24 AM
Quoted 19 lines · Michael Kelley
[...]
drivers/hv/hv_common.c line 110
Quoted 12 lines
drivers/hv/hv_common.c +12
-0+int hv_common_cpu_init (unsigned int cpu) +{ +void **inputarg, **outputarg; +u64 msr_vp_index; +gfp_t flags; +int pgcount = hv_root_partition ?2 :1 ; + +/* hv_cpu_init() can be called with IRQs disabled from hv_resume() */ + flags =irqs_disabled () ?GFP_ATOMIC :GFP_KERNEL ; + + inputarg = (void **)this_cpu_ptr (hyperv_pcpu_input_arg); + *inputarg =kmalloc (pgcount *HV_HYP_PAGE_SIZE , flags);This is changed from
alloc_pagesto kmalloc. Does it ensure the alignment is still correct?
Quoted 1 line
Wei.
It does. The alignment guarantee made by kmalloc() was changed a couple of years ago. See commit 59bb47985c1d. Here's the current text from the comments preceding kmalloc():
* The allocated object address is aligned to at least ARCH_KMALLOC_MINALIGN
* bytes. For @size of power of two bytes, the alignment is also guaranteed
* to be at least to the size.
Michael
Wei Liu
maintainerRe: [PATCH 1/3] Drivers: hv: Make portions of Hyper-V init code be arch neutral
Quoted 22 lines · Michael Kelley
From: Wei Liu <[email protected]> Sent: Monday, July 12, 2021 11:24 AM
Quoted 19 lines · Michael Kelley
[...]
drivers/hv/hv_common.c line 110
Quoted 12 lines
drivers/hv/hv_common.c +12
-0+int hv_common_cpu_init (unsigned int cpu) +{ +void **inputarg, **outputarg; +u64 msr_vp_index; +gfp_t flags; +int pgcount = hv_root_partition ?2 :1 ; + +/* hv_cpu_init() can be called with IRQs disabled from hv_resume() */ + flags =irqs_disabled () ?GFP_ATOMIC :GFP_KERNEL ; + + inputarg = (void **)this_cpu_ptr (hyperv_pcpu_input_arg); + *inputarg =kmalloc (pgcount *HV_HYP_PAGE_SIZE , flags);This is changed from
alloc_pagesto kmalloc. Does it ensure the alignment is still correct?Quoted 1 line
Wei.
Quoted 7 lines
It does. The alignment guarantee made by
kmalloc()was changed a couple of years ago. See commit59bb47985c1d. Here's the current text from the comments precedingkmalloc():* The allocated object address is aligned to at least
ARCH_KMALLOC_MINALIGN
* bytes. For @size of power of two bytes, the alignment is also guaranteed
* to be at least to the size.
OK. That's good.
Wei.
Quoted 3 lines
Michael
PATCH 2/3 Drivers: hv: Add arch independent default functions for some Hyper-V handlers
In reply to Michael KelleyArchitecture independent Hyper-V code calls various arch-specific handlers when needed. To aid in supporting multiple architectures, provide weak defaults that can be overridden by arch-specific implementations where appropriate. But when arch-specific overrides aren't needed or haven't been implemented yet for a particular architecture, these stubs reduce the amount of clutter under arch/.
No functional change.
--- arch/x86/hyperv/hv_init.c | 2 -- arch/x86/kernel/cpu/mshyperv.c | 6 ------ drivers/hv/hv_common.c | 49 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 8 deletions(-)
arch/x86/hyperv/hv_init.c +0 -2
@@ -468,7 +468,6 @@ void hyperv_cleanup(void) hypercall_msr.as_uint64 =0 ;wrmsrl (HV_X64_MSR_REFERENCE_TSC , hypercall_msr.as_uint64); } -EXPORT_SYMBOL_GPL (hyperv_cleanup);void hyperv_report_panic (struct pt_regs *regs,long err,bool in_die) {
@@ -542,4 +541,3 @@ bool hv_is_isolation_supported(void) {return hv_get_isolation_type () !=HV_ISOLATION_TYPE_NONE ; } -EXPORT_SYMBOL_GPL (hv_is_isolation_supported);
arch/x86/kernel/cpu/mshyperv.c +0 -6
@@ -58,14 +58,12 @@ void hv_setup_vmbus_handler(void (*handler)(void)) { vmbus_handler = handler; } -EXPORT_SYMBOL_GPL (hv_setup_vmbus_handler);void hv_remove_vmbus_handler (void ) {/* We have no way to deallocate the interrupt gate */ vmbus_handler =NULL ; } -EXPORT_SYMBOL_GPL (hv_remove_vmbus_handler);/* * Routines to do per-architecture handling of stimer0
@@ -100,25 +98,21 @@ void hv_setup_kexec_handler(void (*handler)(void)) { hv_kexec_handler = handler; } -EXPORT_SYMBOL_GPL (hv_setup_kexec_handler);void hv_remove_kexec_handler (void ) { hv_kexec_handler =NULL ; } -EXPORT_SYMBOL_GPL (hv_remove_kexec_handler);void hv_setup_crash_handler (void (*handler)(struct pt_regs *regs)) { hv_crash_handler = handler; } -EXPORT_SYMBOL_GPL (hv_setup_crash_handler);void hv_remove_crash_handler (void ) { hv_crash_handler =NULL ; } -EXPORT_SYMBOL_GPL (hv_remove_crash_handler);#ifdef CONFIG_KEXEC_CORE static void hv_machine_shutdown (void )
drivers/hv/hv_common.c +49 -0
58 lines not on this page · raw message
Signature · 1 line
1.8.3.1
PATCH 3/3 Drivers: hv: Move Hyper-V misc functionality to arch-neutral code
In reply to Michael KelleyThe check for whether hibernation is possible, and the enabling of Hyper-V panic notification during kexec, are both architecture neutral. Move the code from under arch/x86 and into drivers/hv/hv_common.c where it can also be used for ARM64.
No functional change.
--- arch/x86/hyperv/hv_init.c | 8 +------- arch/x86/kernel/cpu/mshyperv.c | 11 ----------- drivers/hv/hv_common.c | 18 ++++++++++++++++++ 3 files changed, 19 insertions(+), 18 deletions(-)
arch/x86/hyperv/hv_init.c +1 -7
@@ -7,10 +7,10 @@* Author : K. Y. Srinivasan <[email protected]> */ -#include <linux/acpi.h> #include <linux/efi.h> #include <linux/types.h> #include <linux/bitfield.h> +#include <linux/io.h> #include <asm/apic.h> #include <asm/desc.h> #include <asm/hypervisor.h>
@@ -523,12 +523,6 @@ bool hv_is_hyperv_initialized(void) }EXPORT_SYMBOL_GPL (hv_is_hyperv_initialized); -bool hv_is_hibernation_supported (void ) -{ -return !hv_root_partition &&acpi_sleep_state_supported (ACPI_STATE_S4 ); -} -EXPORT_SYMBOL_GPL (hv_is_hibernation_supported); -enum hv_isolation_type hv_get_isolation_type (void ) {if (!(ms_hyperv.priv_high &HV_ISOLATION ))
arch/x86/kernel/cpu/mshyperv.c +0 -11
@@ -17,7 +17,6 @@#include <linux/irq.h> #include <linux/kexec.h> #include <linux/i8253.h> -#include <linux/panic_notifier.h> #include <linux/random.h> #include <asm/processor.h> #include <asm/hypervisor.h>
@@ -322,16 +321,6 @@ static void __init ms_hyperv_init_platform(void) ms_hyperv.nested_features); } -/* -* Hyper-V expects to get crash register data or kmsg when -* crash enlightment is available and system crashes. Set -* crash_kexec_post_notifiers to be true to make sure that -* calling crash enlightment interface before running kdump -* kernel. -*/ -if (ms_hyperv.misc_features &HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE ) - crash_kexec_post_notifiers = true; -#ifdef CONFIG_X86_LOCAL_APIC if (ms_hyperv.features &HV_ACCESS_FREQUENCY_MSRS && ms_hyperv.misc_features &HV_FEATURE_FREQUENCY_MSRS_AVAILABLE ) {
drivers/hv/hv_common.c +18 -0
@@ -13,9 +13,11 @@*/ #include <linux/types.h> +#include <linux/acpi.h> #include <linux/export.h> #include <linux/bitfield.h> #include <linux/cpumask.h> +#include <linux/panic_notifier.h> #include <linux/ptrace.h> #include <linux/slab.h> #include <asm/hyperv-tlfs.h>
@@ -64,6 +66,16 @@ int __init hv_common_init(void)int i;/* +* Hyper-V expects to get crash register data or kmsg when +* crash enlightment is available and system crashes. Set +* crash_kexec_post_notifiers to be true to make sure that +* calling crash enlightment interface before running kdump +* kernel. +*/ +if (ms_hyperv.misc_features &HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE ) + crash_kexec_post_notifiers = true; + +/* * Allocate the per-CPU state for the hypercall input arg. * If this allocation fails, we will not be able to setup * (per-CPU) hypercall input page and thus this failure is
@@ -197,6 +209,12 @@ bool hv_query_ext_cap(u64 cap_query) }EXPORT_SYMBOL_GPL (hv_query_ext_cap); +bool hv_is_hibernation_supported (void ) +{ +return !hv_root_partition &&acpi_sleep_state_supported (ACPI_STATE_S4 ); +} +EXPORT_SYMBOL_GPL (hv_is_hibernation_supported); +/* These __weak functions provide default "no-op" behavior and * may be overridden by architecture specific versions. Architectures * for which the default "no-op" behavior is sufficient can leave
Signature · 1 line
1.8.3.1
Quoted 16 lines · Michael Kelley
This patch set moves additional Hyper-V code under
arch/x86into arch-neutralhv_common.cwhere it can be shared by x86 and and ARM64 implementations. The move reduces the overall lines of code across both architectures, and removes code underarch/that isn't really architecture-specific.The code is moved into
hv_common.cbecause it must be built-in to the kernel image, and not be part of a module.No functional changes are intended.
Michael Kelley (3):
Drivers: hv: Make portions of Hyper-V init code be arch neutral
Drivers: hv: Add arch independent default functions for some Hyper-V
handlers
Drivers: hv: Move Hyper-V misc functionality to arch-neutral code
Quoted 7 lines
arch/x86/hyperv/hv_init.c | 101 +++----------------- arch/x86/include/asm/mshyperv.h | 4 - arch/x86/kernel/cpu/mshyperv.c | 24 ----- drivers/hv/hv_common.c | 198 ++++++++++++++++++++++++++++++++++++++++ include/asm-generic/mshyperv.h | 10 ++ 5 files changed, 219 insertions(+), 118 deletions(-)
Applied to hyperv-next. Thanks.
Quoted 3 lines
Signature · 1 line
1.8.3.1