Skip to content

Commit c4bdf94

Browse files
Jinank Jainliuw
authored andcommitted
x86/hyperv: Add support for detecting nested hypervisor
Detect if Linux is running as a nested hypervisor in the root partition for Microsoft Hypervisor, using flags provided by MSHV. Expose a new variable hv_nested that is used later for decisions specific to the nested use case. Signed-off-by: Jinank Jain <[email protected]> Reviewed-by: Michael Kelley <[email protected]> Link: https://lore.kernel.org/r/8e3e7112806e81d2292a66a56fe547162754ecea.1672639707.git.jinankjain@linux.microsoft.com Signed-off-by: Wei Liu <[email protected]>
1 parent b7bfaa7 commit c4bdf94

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

arch/x86/include/asm/hyperv-tlfs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@
116116
/* Recommend using the newer ExProcessorMasks interface */
117117
#define HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED BIT(11)
118118

119+
/* Indicates that the hypervisor is nested within a Hyper-V partition. */
120+
#define HV_X64_HYPERV_NESTED BIT(12)
121+
119122
/* Recommend using enlightened VMCS */
120123
#define HV_X64_ENLIGHTENED_VMCS_RECOMMENDED BIT(14)
121124

arch/x86/kernel/cpu/mshyperv.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737

3838
/* Is Linux running as the root partition? */
3939
bool hv_root_partition;
40+
/* Is Linux running on nested Microsoft Hypervisor */
41+
bool hv_nested;
4042
struct ms_hyperv_info ms_hyperv;
4143

4244
#if IS_ENABLED(CONFIG_HYPERV)
@@ -301,6 +303,11 @@ static void __init ms_hyperv_init_platform(void)
301303
pr_info("Hyper-V: running as root partition\n");
302304
}
303305

306+
if (ms_hyperv.hints & HV_X64_HYPERV_NESTED) {
307+
hv_nested = true;
308+
pr_info("Hyper-V: running on a nested hypervisor\n");
309+
}
310+
304311
/*
305312
* Extract host information.
306313
*/

drivers/hv/hv_common.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,20 @@
2525
#include <asm/mshyperv.h>
2626

2727
/*
28-
* hv_root_partition and ms_hyperv are defined here with other Hyper-V
29-
* specific globals so they are shared across all architectures and are
28+
* hv_root_partition, ms_hyperv and hv_nested are defined here with other
29+
* Hyper-V specific globals so they are shared across all architectures and are
3030
* built only when CONFIG_HYPERV is defined. But on x86,
3131
* ms_hyperv_init_platform() is built even when CONFIG_HYPERV is not
32-
* defined, and it uses these two variables. So mark them as __weak
32+
* defined, and it uses these three variables. So mark them as __weak
3333
* here, allowing for an overriding definition in the module containing
3434
* ms_hyperv_init_platform().
3535
*/
3636
bool __weak hv_root_partition;
3737
EXPORT_SYMBOL_GPL(hv_root_partition);
3838

39+
bool __weak hv_nested;
40+
EXPORT_SYMBOL_GPL(hv_nested);
41+
3942
struct ms_hyperv_info __weak ms_hyperv;
4043
EXPORT_SYMBOL_GPL(ms_hyperv);
4144

include/asm-generic/mshyperv.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ struct ms_hyperv_info {
4848
u64 shared_gpa_boundary;
4949
};
5050
extern struct ms_hyperv_info ms_hyperv;
51+
extern bool hv_nested;
5152

5253
extern void * __percpu *hyperv_pcpu_input_arg;
5354
extern void * __percpu *hyperv_pcpu_output_arg;

0 commit comments

Comments
 (0)