Skip to contentSkip to conversation

hyperv: Move some features to common code

PATCH Superseded Nuno Das Neves 2 participants 7 messages Jan 23, 2025 – Jan 29, 2025

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover hyperv: Move some features to common code

There are several bits of Hyper-V-related code that today live in arch/x86 but are not really specific to x86_64 and will work on arm64 too.

Some of these will be needed in the upcoming mshv driver code (for Linux as root partition on Hyper-V). So this is a good time to move them to drivers/hv.

---
Changes in v2:
* Fix dependence on percpu output page by using a stack variable for the hypercall output [Michael Kelley]
* Remove unnecessary WARN()s [Michael Kelley]
* Define hv_current_partition_id in hv_common.c [Michael Kelley]
* Move entire hv_proc.c to drivers/hv [Michael Kelley]

Nuno Das Neves (2):
hyperv: Move hv_current_partition_id to arch-generic code
hyperv: Move arch/x86/hyperv/hv_proc.c to drivers/hv

 arch/x86/hyperv/Makefile                  |  2 +-
 arch/x86/hyperv/hv_init.c                 | 26 -----------------------
 arch/x86/include/asm/mshyperv.h           |  6 ------
 drivers/hv/Makefile                       |  2 +-
 drivers/hv/hv_common.c                    | 23 ++++++++++++++++++++
 {arch/x86/hyperv => drivers/hv}/hv_proc.c |  4 ----
 include/asm-generic/mshyperv.h            |  5 +++++
 7 files changed, 30 insertions(+), 38 deletions(-)
 rename {arch/x86/hyperv => drivers/hv}/hv_proc.c (98%)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 1/2 hyperv: Move hv_current_partition_id to arch-generic code

From: Nuno Das Neves <[email protected]>

Move hv_current_partition_id and hv_get_partition_id() to hv_common.c. These aren't specific to x86_64 and will be needed by common code.

Set hv_current_partition_id to HV_PARTITION_ID_SELF by default.

Use a stack variable for the output of the hypercall. This allows moving the call of hv_get_partition_id() to hv_common_init() before the percpu pages are initialized.

Remove the BUG()s. Failing to get the id need not crash the machine.

---
 arch/x86/hyperv/hv_init.c       | 26 --------------------------
 arch/x86/include/asm/mshyperv.h |  2 --
 drivers/hv/hv_common.c          | 23 +++++++++++++++++++++++
 include/asm-generic/mshyperv.h  |  1 +
 4 files changed, 24 insertions(+), 28 deletions(-)
arch/x86/hyperv/hv_init.c +0 -26
index 173005e6a95d..6b9f6f9f704d 100644
@@ -34,9 +34,6 @@
 #include <clocksource/hyperv_timer.h>
 #include <linux/highmem.h>
 
-u64 hv_current_partition_id = ~0ull;
-EXPORT_SYMBOL_GPL(hv_current_partition_id);
-
 void *hv_hypercall_pg;
 EXPORT_SYMBOL_GPL(hv_hypercall_pg);
 
@@ -393,24 +390,6 @@ static void __init hv_stimer_setup_percpu_clockev(void)
 		old_setup_percpu_clockev();
 }
 
-static void __init hv_get_partition_id(void)
-{
-	struct hv_get_partition_id *output_page;
-	u64 status;
-	unsigned long flags;
-
-	local_irq_save(flags);
-	output_page = *this_cpu_ptr(hyperv_pcpu_output_arg);
-	status = hv_do_hypercall(HVCALL_GET_PARTITION_ID, NULL, output_page);
-	if (!hv_result_success(status)) {
-		/* No point in proceeding if this failed */
-		pr_err("Failed to get partition ID: %lld\n", status);
-		BUG();
-	}
-	hv_current_partition_id = output_page->partition_id;
-	local_irq_restore(flags);
-}
-
 #if IS_ENABLED(CONFIG_HYPERV_VTL_MODE)
 static u8 __init get_vtl(void)
 {
@@ -605,11 +584,6 @@ void __init hyperv_init(void)
 
 	register_syscore_ops(&hv_syscore_ops);
 
-	if (cpuid_ebx(HYPERV_CPUID_FEATURES) & HV_ACCESS_PARTITION_ID)
-		hv_get_partition_id();
-
-	BUG_ON(hv_root_partition && hv_current_partition_id == ~0ull);
-
 #ifdef CONFIG_PCI_MSI
 	/*
 	 * If we're running as root, we want to create our own PCI MSI domain.
arch/x86/include/asm/mshyperv.h +0 -2
index f91ab1e75f9f..8d3ada3e8d0d 100644
@@ -43,8 +43,6 @@ extern bool hyperv_paravisor_present;
 
 extern void *hv_hypercall_pg;
 
-extern u64 hv_current_partition_id;
-
 extern union hv_ghcb * __percpu *hv_ghcb_pg;
 
 bool hv_isolation_type_snp(void);
drivers/hv/hv_common.c +23 -0
index af5d1dc451f6..1da19b64ef16 100644
@@ -31,6 +31,9 @@
 #include <hyperv/hvhdk.h>
 #include <asm/mshyperv.h>
 
+u64 hv_current_partition_id = HV_PARTITION_ID_SELF;
+EXPORT_SYMBOL_GPL(hv_current_partition_id);
+
 /*
  * hv_root_partition, ms_hyperv and hv_nested are defined here with other
  * Hyper-V specific globals so they are shared across all architectures and are
@@ -283,6 +286,23 @@ static inline bool hv_output_page_exists(void)
 	return hv_root_partition || IS_ENABLED(CONFIG_HYPERV_VTL_MODE);
 }
 
+static void __init hv_get_partition_id(void)
+{
+	/*
+	 * Note in this case the output can be on the stack because it is just
+	 * a single u64 and hence won't cross a page boundary.
+	 */
+	struct hv_get_partition_id output;
+	u64 status;
+
+	status = hv_do_hypercall(HVCALL_GET_PARTITION_ID, NULL, &output);
+	if (!hv_result_success(status)) {
+		pr_err("Hyper-V: failed to get partition ID: %#lx\n", status);
+		return;
+	}
+	hv_current_partition_id = output.partition_id;
+}
+
 int __init hv_common_init(void)
 {
 	int i;
@@ -298,6 +318,9 @@ int __init hv_common_init(void)
 	if (hv_is_isolation_supported())
 		sysctl_record_panic_msg = 0;
 
+	if (ms_hyperv.priv_high & HV_ACCESS_PARTITION_ID)
+		hv_get_partition_id();
+
 	/*
 	 * Hyper-V expects to get crash register data or kmsg when
 	 * crash enlightment is available and system crashes. Set
include/asm-generic/mshyperv.h +1 -0
index a7bbe504e4f3..98100466e0b2 100644
@@ -58,6 +58,7 @@ struct ms_hyperv_info {
 };
 extern struct ms_hyperv_info ms_hyperv;
 extern bool hv_nested;
+extern u64 hv_current_partition_id;
 
 extern void * __percpu *hyperv_pcpu_input_arg;
 extern void * __percpu *hyperv_pcpu_output_arg;
Signature · 1 line
2.34.1
#2 lore raw

RE: [PATCH v2 1/2] hyperv: Move hv_current_partition_id to arch-generic code

From: Nuno Das Neves <[email protected]> Sent: Wednesday, January 22, 2025 5:48 PM

Quoted 99 lines

Move hv_current_partition_id and hv_get_partition_id() to hv_common.c. These aren't specific to x86_64 and will be needed by common code.

Set hv_current_partition_id to HV_PARTITION_ID_SELF by default.

Use a stack variable for the output of the hypercall. This allows moving the call of hv_get_partition_id() to hv_common_init() before the percpu pages are initialized.

Remove the BUG()s. Failing to get the id need not crash the machine.

---
 arch/x86/hyperv/hv_init.c       | 26 --------------------------
 arch/x86/include/asm/mshyperv.h |  2 --
 drivers/hv/hv_common.c          | 23 +++++++++++++++++++++++
 include/asm-generic/mshyperv.h  |  1 +
 4 files changed, 24 insertions(+), 28 deletions(-)
arch/x86/hyperv/hv_init.c +0 -26
index 173005e6a95d..6b9f6f9f704d 100644
@@ -34,9 +34,6 @@
 #include <clocksource/hyperv_timer.h>
 #include <linux/highmem.h>
 
-u64 hv_current_partition_id = ~0ull;
-EXPORT_SYMBOL_GPL(hv_current_partition_id);
-
 void *hv_hypercall_pg;
 EXPORT_SYMBOL_GPL(hv_hypercall_pg);
 
@@ -393,24 +390,6 @@ static void __init hv_stimer_setup_percpu_clockev(void)
 		old_setup_percpu_clockev();
 }
 
-static void __init hv_get_partition_id(void)
-{
-	struct hv_get_partition_id *output_page;
-	u64 status;
-	unsigned long flags;
-
-	local_irq_save(flags);
-	output_page = *this_cpu_ptr(hyperv_pcpu_output_arg);
-	status = hv_do_hypercall(HVCALL_GET_PARTITION_ID, NULL, output_page);
-	if (!hv_result_success(status)) {
-		/* No point in proceeding if this failed */
-		pr_err("Failed to get partition ID: %lld\n", status);
-		BUG();
-	}
-	hv_current_partition_id = output_page->partition_id;
-	local_irq_restore(flags);
-}
-
 #if IS_ENABLED(CONFIG_HYPERV_VTL_MODE)
 static u8 __init get_vtl(void)
 {
@@ -605,11 +584,6 @@ void __init hyperv_init(void)
 
 	register_syscore_ops(&hv_syscore_ops);
 
-	if (cpuid_ebx(HYPERV_CPUID_FEATURES) & HV_ACCESS_PARTITION_ID)
-		hv_get_partition_id();
-
-	BUG_ON(hv_root_partition && hv_current_partition_id == ~0ull);
-
 #ifdef CONFIG_PCI_MSI
 	/*
 	 * If we're running as root, we want to create our own PCI MSI domain.
arch/x86/include/asm/mshyperv.h +0 -2
index f91ab1e75f9f..8d3ada3e8d0d 100644
@@ -43,8 +43,6 @@ extern bool hyperv_paravisor_present;
 
 extern void *hv_hypercall_pg;
 
-extern u64 hv_current_partition_id;
-
 extern union hv_ghcb * __percpu *hv_ghcb_pg;
 
 bool hv_isolation_type_snp(void);
drivers/hv/hv_common.c +3 -0
index af5d1dc451f6..1da19b64ef16 100644
@@ -31,6 +31,9 @@
 #include <hyperv/hvhdk.h>
 #include <asm/mshyperv.h>
 
+u64 hv_current_partition_id = HV_PARTITION_ID_SELF;
+EXPORT_SYMBOL_GPL(hv_current_partition_id);
+
 /*
  * hv_root_partition, ms_hyperv and hv_nested are defined here with other
  * Hyper-V specific globals so they are shared across all architectures and are

drivers/hv/hv_common.c static inline bool hv_output_page_exists(void) · line 295

Quoted 10 lines
drivers/hv/hv_common.c +7 -0
@@ -283,6 +286,23 @@ static inline bool hv_output_page_exists(void)
 	return hv_root_partition || IS_ENABLED(CONFIG_HYPERV_VTL_MODE);
 }
 
+static void __init hv_get_partition_id(void)
+{
+	/*
+	 * Note in this case the output can be on the stack because it is just
+	 * a single u64 and hence won't cross a page boundary.
+	 */
+	struct hv_get_partition_id output;

It's unfortunate that the structure name "hv_get_partition_id" is also the name of this function. Could the structure name be changed to follow the pattern of having "output" in the name, like other hypercall parameters? It's not a blocker if it can't be changed. I was just surprised to search for "hv_get_partition_id" and find both uses.

Also, see the comment at the beginning of hv_query_ext_cap() regarding using a local stack variable as hypercall input or output. The comment originated here [1]. At that time, I didn't investigate Sunil's assertion any further, and I'm still unsure whether it is really true. But perhaps for consistency and safety we should follow what it says.

[1] https://lore.kernel.org/linux-hyperv/SN4PR2101MB0880DB0606A5A0B72AD244B4C06A9@SN4PR2101MB0880.namprd21.prod.outlook.com/

Quoted 14 lines
drivers/hv/hv_common.c +10 -0
+	u64 status;
+
+	status = hv_do_hypercall(HVCALL_GET_PARTITION_ID, NULL, &output);
+	if (!hv_result_success(status)) {
+		pr_err("Hyper-V: failed to get partition ID: %#lx\n", status);
+		return;
+	}
+	hv_current_partition_id = output.partition_id;
+}
+
 int __init hv_common_init(void)
 {
 	int i;

drivers/hv/hv_common.c int __init hv_common_init(void) · line 322

Quoted 5 lines
drivers/hv/hv_common.c +2 -0
@@ -298,6 +318,9 @@ int __init hv_common_init(void)
 	if (hv_is_isolation_supported())
 		sysctl_record_panic_msg = 0;
 
+	if (ms_hyperv.priv_high & HV_ACCESS_PARTITION_ID)
+		hv_get_partition_id();

I don't see how this works. On the x86 side, hv_common_init() is called before the guest ID is set and the hypercall page is setup. So the hypercall in hv_get_partition_id() should fail.

Michael

Quoted 18 lines
drivers/hv/hv_common.c +1 -0
+
 	/*
 	 * Hyper-V expects to get crash register data or kmsg when
 	 * crash enlightment is available and system crashes. Set
include/asm-generic/mshyperv.h +1 -1
index a7bbe504e4f3..98100466e0b2 100644
@@ -58,6 +58,7 @@ struct ms_hyperv_info {
 };
 extern struct ms_hyperv_info ms_hyperv;
 extern bool hv_nested;
+extern u64 hv_current_partition_id;
 
 extern void * __percpu *hyperv_pcpu_input_arg;
 extern void * __percpu *hyperv_pcpu_output_arg;
--

2.34.1

#3 lore raw

Re: [PATCH v2 1/2] hyperv: Move hv_current_partition_id to arch-generic code

Quoted 103 lines · Michael Kelley

From: Nuno Das Neves <[email protected]> Sent: Wednesday, January 22, 2025 5:48 PM

Quoted 102 lines

Move hv_current_partition_id and hv_get_partition_id() to hv_common.c. These aren't specific to x86_64 and will be needed by common code.

Set hv_current_partition_id to HV_PARTITION_ID_SELF by default.

Use a stack variable for the output of the hypercall. This allows moving the call of hv_get_partition_id() to hv_common_init() before the percpu pages are initialized.

Remove the BUG()s. Failing to get the id need not crash the machine.

---
 arch/x86/hyperv/hv_init.c       | 26 --------------------------
 arch/x86/include/asm/mshyperv.h |  2 --
 drivers/hv/hv_common.c          | 23 +++++++++++++++++++++++
 include/asm-generic/mshyperv.h  |  1 +
 4 files changed, 24 insertions(+), 28 deletions(-)
arch/x86/hyperv/hv_init.c +0 -26
index 173005e6a95d..6b9f6f9f704d 100644
@@ -34,9 +34,6 @@
 #include <clocksource/hyperv_timer.h>
 #include <linux/highmem.h>
 
-u64 hv_current_partition_id = ~0ull;
-EXPORT_SYMBOL_GPL(hv_current_partition_id);
-
 void *hv_hypercall_pg;
 EXPORT_SYMBOL_GPL(hv_hypercall_pg);
 
@@ -393,24 +390,6 @@ static void __init hv_stimer_setup_percpu_clockev(void)
 		old_setup_percpu_clockev();
 }
 
-static void __init hv_get_partition_id(void)
-{
-	struct hv_get_partition_id *output_page;
-	u64 status;
-	unsigned long flags;
-
-	local_irq_save(flags);
-	output_page = *this_cpu_ptr(hyperv_pcpu_output_arg);
-	status = hv_do_hypercall(HVCALL_GET_PARTITION_ID, NULL, output_page);
-	if (!hv_result_success(status)) {
-		/* No point in proceeding if this failed */
-		pr_err("Failed to get partition ID: %lld\n", status);
-		BUG();
-	}
-	hv_current_partition_id = output_page->partition_id;
-	local_irq_restore(flags);
-}
-
 #if IS_ENABLED(CONFIG_HYPERV_VTL_MODE)
 static u8 __init get_vtl(void)
 {
@@ -605,11 +584,6 @@ void __init hyperv_init(void)
 
 	register_syscore_ops(&hv_syscore_ops);
 
-	if (cpuid_ebx(HYPERV_CPUID_FEATURES) & HV_ACCESS_PARTITION_ID)
-		hv_get_partition_id();
-
-	BUG_ON(hv_root_partition && hv_current_partition_id == ~0ull);
-
 #ifdef CONFIG_PCI_MSI
 	/*
 	 * If we're running as root, we want to create our own PCI MSI domain.
arch/x86/include/asm/mshyperv.h +0 -2
index f91ab1e75f9f..8d3ada3e8d0d 100644
@@ -43,8 +43,6 @@ extern bool hyperv_paravisor_present;
 
 extern void *hv_hypercall_pg;
 
-extern u64 hv_current_partition_id;
-
 extern union hv_ghcb * __percpu *hv_ghcb_pg;
 
 bool hv_isolation_type_snp(void);
drivers/hv/hv_common.c +3 -0
index af5d1dc451f6..1da19b64ef16 100644
@@ -31,6 +31,9 @@
 #include <hyperv/hvhdk.h>
 #include <asm/mshyperv.h>
 
+u64 hv_current_partition_id = HV_PARTITION_ID_SELF;
+EXPORT_SYMBOL_GPL(hv_current_partition_id);
+
 /*
  * hv_root_partition, ms_hyperv and hv_nested are defined here with other
  * Hyper-V specific globals so they are shared across all architectures and are
@@ -283,6 +286,23 @@ static inline bool hv_output_page_exists(void)
 	return hv_root_partition || IS_ENABLED(CONFIG_HYPERV_VTL_MODE);
 }
 
Quoted 14 lines

drivers/hv/hv_common.c static inline bool hv_output_page_exists(void) · line 295

Quoted 7 lines
drivers/hv/hv_common.c +7 -0
+static void __init hv_get_partition_id(void)
+{
+	/*
+	 * Note in this case the output can be on the stack because it is just
+	 * a single u64 and hence won't cross a page boundary.
+	 */
+	struct hv_get_partition_id output;

It's unfortunate that the structure name "hv_get_partition_id" is also the name of this function. Could the structure name be changed to follow the pattern of having "output" in the name, like other hypercall parameters? It's not a blocker if it can't be changed. I was just surprised to search for "hv_get_partition_id" and find both uses.

hv_output_get_partition_id is really the "correct" name from the Hyper-V code, so it makes sense to just change it to that in this patch.

Quoted 8 lines

Also, see the comment at the beginning of hv_query_ext_cap() regarding using a local stack variable as hypercall input or output. The comment originated here [1]. At that time, I didn't investigate Sunil's assertion any further, and I'm still unsure whether it is really true. But perhaps for consistency and safety we should follow what it says.

[1] https://lore.kernel.org/linux-hyperv/SN4PR2101MB0880DB0606A5A0B72AD244B4C06A9@SN4PR2101MB0880.namprd21.prod.outlook.com/

Hmm, from some cursory research it does look like stack variables can't be used with virt_to_phys().

I thought about just using &hv_current_partition directly - I *think* that will work - but in the end I think it's just simpler to just move calls so the percpu output page can be used as normal. That may save some additional back-and-forth as well as explanatory comments in the code.

I will also add a check for hv_output_page_exists() here, as a precaution in case the HV_ACCESS_PARTITION_ID privilege ever becomes decoupled from that (as it stands, I believe that permission is only for the root partition, but you never know).

Quoted 17 lines
Quoted 17 lines
drivers/hv/hv_common.c +10 -0
+	u64 status;
+
+	status = hv_do_hypercall(HVCALL_GET_PARTITION_ID, NULL, &output);
+	if (!hv_result_success(status)) {
+		pr_err("Hyper-V: failed to get partition ID: %#lx\n", status);
+		return;
+	}
+	hv_current_partition_id = output.partition_id;
+}
+
 int __init hv_common_init(void)
 {
 	int i;
@@ -298,6 +318,9 @@ int __init hv_common_init(void)
 	if (hv_is_isolation_supported())
 		sysctl_record_panic_msg = 0;
 
Quoted 7 lines

drivers/hv/hv_common.c int __init hv_common_init(void) · line 322

Quoted 2 lines
drivers/hv/hv_common.c +2 -0
+	if (ms_hyperv.priv_high & HV_ACCESS_PARTITION_ID)
+		hv_get_partition_id();

I don't see how this works. On the x86 side, hv_common_init() is called before the guest ID is set and the hypercall page is setup. So the hypercall in hv_get_partition_id() should fail.

Oh, I tried to get too clever. I will put it back where it was and add it on the arm64 side to hyperv_init() after the per-cpu init as I mentioned above.

Thanks for the comments,
Nuno

Quoted 20 lines

Michael

Quoted 18 lines
drivers/hv/hv_common.c +1 -0
+
 	/*
 	 * Hyper-V expects to get crash register data or kmsg when
 	 * crash enlightment is available and system crashes. Set
include/asm-generic/mshyperv.h +1 -1
index a7bbe504e4f3..98100466e0b2 100644
@@ -58,6 +58,7 @@ struct ms_hyperv_info {
 };
 extern struct ms_hyperv_info ms_hyperv;
 extern bool hv_nested;
+extern u64 hv_current_partition_id;
 
 extern void * __percpu *hyperv_pcpu_input_arg;
 extern void * __percpu *hyperv_pcpu_output_arg;
--

2.34.1

#4 lore raw

RE: [PATCH v2 1/2] hyperv: Move hv_current_partition_id to arch-generic code

From: Nuno Das Neves <[email protected]> Sent: Tuesday, January 28, 2025 4:46 PM

Quoted 22 lines · Michael Kelley
Quoted 20 lines

From: Nuno Das Neves <[email protected]> Sent: Wednesday, January 22, 2025 5:48 PM

Quoted 19 lines

Move hv_current_partition_id and hv_get_partition_id() to hv_common.c. These aren't specific to x86_64 and will be needed by common code.

Set hv_current_partition_id to HV_PARTITION_ID_SELF by default.

Use a stack variable for the output of the hypercall. This allows moving the call of hv_get_partition_id() to hv_common_init() before the percpu pages are initialized.

Remove the BUG()s. Failing to get the id need not crash the machine.

---
 arch/x86/hyperv/hv_init.c       | 26 --------------------------
 arch/x86/include/asm/mshyperv.h |  2 --
 drivers/hv/hv_common.c          | 23 +++++++++++++++++++++++
 include/asm-generic/mshyperv.h  |  1 +
 4 files changed, 24 insertions(+), 28 deletions(-)

[snip]

Quoted 52 lines
Quoted 18 lines
Quoted 18 lines
drivers/hv/hv_common.c +3 -0
index af5d1dc451f6..1da19b64ef16 100644
@@ -31,6 +31,9 @@
 #include <hyperv/hvhdk.h>
 #include <asm/mshyperv.h>
 
+u64 hv_current_partition_id = HV_PARTITION_ID_SELF;
+EXPORT_SYMBOL_GPL(hv_current_partition_id);
+
 /*
  * hv_root_partition, ms_hyperv and hv_nested are defined here with other
  * Hyper-V specific globals so they are shared across all architectures and are
@@ -283,6 +286,23 @@ static inline bool hv_output_page_exists(void)
 	return hv_root_partition || IS_ENABLED(CONFIG_HYPERV_VTL_MODE);
 }
 
Quoted 14 lines

drivers/hv/hv_common.c static inline bool hv_output_page_exists(void) · line 295

Quoted 7 lines
drivers/hv/hv_common.c +7 -0
+static void __init hv_get_partition_id(void)
+{
+	/*
+	 * Note in this case the output can be on the stack because it is just
+	 * a single u64 and hence won't cross a page boundary.
+	 */
+	struct hv_get_partition_id output;

It's unfortunate that the structure name "hv_get_partition_id" is also the name of this function. Could the structure name be changed to follow the pattern of having "output" in the name, like other hypercall parameters? It's not a blocker if it can't be changed. I was just surprised to search for "hv_get_partition_id" and find both uses.

hv_output_get_partition_id is really the "correct" name from the Hyper-V code, so it makes sense to just change it to that in this patch.

Quoted 8 lines

Also, see the comment at the beginning of hv_query_ext_cap() regarding using a local stack variable as hypercall input or output. The comment originated here [1]. At that time, I didn't investigate Sunil's assertion any further, and I'm still unsure whether it is really true. But perhaps for consistency and safety we should follow what it says.

[1] https://lore.kernel.org/linux-hyperv/SN4PR2101MB0880DB0606A5A0B72AD244B4C06A9@SN4PR2101MB0880.namprd21.prod.outlook.com/

Hmm, from some cursory research it does look like stack variables can't be used with virt_to_phys().

I thought about just using &hv_current_partition directly - I *think* that will work - but in the end I think it's just simpler to just move calls so the percpu output page can be used as normal. That may save some additional back-and-forth as well as explanatory comments in the code.

Quoted 4 lines

I will also add a check for hv_output_page_exists() here, as a precaution in case the HV_ACCESS_PARTITION_ID privilege ever becomes decoupled from that (as it stands, I believe that permission is only for the root partition, but you never know).

Or just use the hyperv_pcpu_input_page, even though the use here is for output. Then you don't have to worry about whether the output page exists. FWIW, I'm working on a set of changes that encapsulates the assignment of the per-cpu hypercall argument pages, and it does away with the distinction between the input and output pages. But that will come sometime after this patch.

Michael

Quoted 32 lines
Quoted 18 lines
Quoted 18 lines
drivers/hv/hv_common.c +10 -0
+	u64 status;
+
+	status = hv_do_hypercall(HVCALL_GET_PARTITION_ID, NULL, &output);
+	if (!hv_result_success(status)) {
+		pr_err("Hyper-V: failed to get partition ID: %#lx\n", status);
+		return;
+	}
+	hv_current_partition_id = output.partition_id;
+}
+
 int __init hv_common_init(void)
 {
 	int i;
@@ -298,6 +318,9 @@ int __init hv_common_init(void)
 	if (hv_is_isolation_supported())
 		sysctl_record_panic_msg = 0;
 
Quoted 7 lines

drivers/hv/hv_common.c int __init hv_common_init(void) · line 322

Quoted 2 lines
drivers/hv/hv_common.c +2 -0
+	if (ms_hyperv.priv_high & HV_ACCESS_PARTITION_ID)
+		hv_get_partition_id();

I don't see how this works. On the x86 side, hv_common_init() is called before the guest ID is set and the hypercall page is setup. So the hypercall in hv_get_partition_id() should fail.

Oh, I tried to get too clever. I will put it back where it was and add it on the arm64 side to hyperv_init() after the per-cpu init as I mentioned above.

Thanks for the comments,
Nuno

#5 lore raw

PATCH v2 2/2 hyperv: Move arch/x86/hyperv/hv_proc.c to drivers/hv

In reply to Nuno Das Neves

These helpers are not specific to x86_64 and will be needed by common code. Remove some unnecessary #includes.

---
 arch/x86/hyperv/Makefile                  | 2 +-
 arch/x86/include/asm/mshyperv.h           | 4 ----
 drivers/hv/Makefile                       | 2 +-
 {arch/x86/hyperv => drivers/hv}/hv_proc.c | 4 ----
 include/asm-generic/mshyperv.h            | 4 ++++
 5 files changed, 6 insertions(+), 10 deletions(-)
 rename {arch/x86/hyperv => drivers/hv}/hv_proc.c (98%)
arch/x86/hyperv/Makefile +1 -1
index 3a1548054b48..d55f494f471d 100644
@@ -1,6 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0-only
 obj-y			:= hv_init.o mmu.o nested.o irqdomain.o ivm.o
-obj-$(CONFIG_X86_64)	+= hv_apic.o hv_proc.o
+obj-$(CONFIG_X86_64)	+= hv_apic.o
 obj-$(CONFIG_HYPERV_VTL_MODE)	+= hv_vtl.o
 
 ifdef CONFIG_X86_64
arch/x86/include/asm/mshyperv.h +0 -4
index 8d3ada3e8d0d..7dfca93ef048 100644
@@ -56,10 +56,6 @@ u64 hv_tdx_hypercall(u64 control, u64 param1, u64 param2);
 #define HV_AP_INIT_GPAT_DEFAULT		0x0007040600070406ULL
 #define HV_AP_SEGMENT_LIMIT		0xffffffff
 
-int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages);
-int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id);
-int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags);
-
 /*
  * If the hypercall involves no input or output parameters, the hypervisor
  * ignores the corresponding GPA pointer.
drivers/hv/Makefile +1 -1
index b992c0ed182b..9afcabb3fbd2 100644
@@ -13,4 +13,4 @@ hv_vmbus-$(CONFIG_HYPERV_TESTING)	+= hv_debugfs.o
 hv_utils-y := hv_util.o hv_kvp.o hv_snapshot.o hv_utils_transport.o
 
 # Code that must be built-in
-obj-$(subst m,y,$(CONFIG_HYPERV)) += hv_common.o
+obj-$(subst m,y,$(CONFIG_HYPERV)) += hv_common.o hv_proc.o
drivers/hv/hv_proc.c renamed from arch/x86/hyperv/hv_proc.c +0 -4
similarity index 98%
rename from arch/x86/hyperv/hv_proc.c
rename to drivers/hv/hv_proc.c
index ac4c834d4435..3e410489f480 100644
@@ -6,11 +6,7 @@
 #include <linux/slab.h>
 #include <linux/cpuhotplug.h>
 #include <linux/minmax.h>
-#include <asm/hypervisor.h>
 #include <asm/mshyperv.h>
-#include <asm/apic.h>
-
-#include <asm/trace/hyperv.h>
 
 /*
  * See struct hv_deposit_memory. The first u64 is partition ID, the rest
include/asm-generic/mshyperv.h +4 -0
index 98100466e0b2..faf5d27a76b1 100644
@@ -217,6 +217,10 @@ void *hv_alloc_hyperv_page(void);
 void *hv_alloc_hyperv_zeroed_page(void);
 void hv_free_hyperv_page(void *addr);
 
+int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages);
+int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id);
+int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags);
+
 /**
  * hv_cpu_number_to_vp_number() - Map CPU to VP.
  * @cpu_number: CPU number in Linux terms
Signature · 1 line
2.34.1
Reviewed-by on v2 2/2
#6 lore raw

RE: [PATCH v2 2/2] hyperv: Move arch/x86/hyperv/hv_proc.c to drivers/hv

From: Nuno Das Neves <[email protected]> Sent: Wednesday, January 22, 2025 5:48 PM

Quoted 86 lines

These helpers are not specific to x86_64 and will be needed by common code. Remove some unnecessary #includes.

---
 arch/x86/hyperv/Makefile                  | 2 +-
 arch/x86/include/asm/mshyperv.h           | 4 ----
 drivers/hv/Makefile                       | 2 +-
 {arch/x86/hyperv => drivers/hv}/hv_proc.c | 4 ----
 include/asm-generic/mshyperv.h            | 4 ++++
 5 files changed, 6 insertions(+), 10 deletions(-)
 rename {arch/x86/hyperv => drivers/hv}/hv_proc.c (98%)
arch/x86/hyperv/Makefile +1 -1
index 3a1548054b48..d55f494f471d 100644
@@ -1,6 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0-only
 obj-y			:= hv_init.o mmu.o nested.o irqdomain.o ivm.o
-obj-$(CONFIG_X86_64)	+= hv_apic.o hv_proc.o
+obj-$(CONFIG_X86_64)	+= hv_apic.o
 obj-$(CONFIG_HYPERV_VTL_MODE)	+= hv_vtl.o
 
 ifdef CONFIG_X86_64
arch/x86/include/asm/mshyperv.h +0 -4
index 8d3ada3e8d0d..7dfca93ef048 100644
@@ -56,10 +56,6 @@ u64 hv_tdx_hypercall(u64 control, u64 param1, u64 param2);
 #define HV_AP_INIT_GPAT_DEFAULT		0x0007040600070406ULL
 #define HV_AP_SEGMENT_LIMIT		0xffffffff
 
-int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages);
-int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id);
-int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags);
-
 /*
  * If the hypercall involves no input or output parameters, the hypervisor
  * ignores the corresponding GPA pointer.
drivers/hv/Makefile +1 -1
index b992c0ed182b..9afcabb3fbd2 100644
@@ -13,4 +13,4 @@ hv_vmbus-$(CONFIG_HYPERV_TESTING)	+= hv_debugfs.o
 hv_utils-y := hv_util.o hv_kvp.o hv_snapshot.o hv_utils_transport.o
 
 # Code that must be built-in
-obj-$(subst m,y,$(CONFIG_HYPERV)) += hv_common.o
+obj-$(subst m,y,$(CONFIG_HYPERV)) += hv_common.o hv_proc.o
drivers/hv/hv_proc.c renamed from arch/x86/hyperv/hv_proc.c +0 -4
similarity index 98%
rename from arch/x86/hyperv/hv_proc.c
rename to drivers/hv/hv_proc.c
index ac4c834d4435..3e410489f480 100644
@@ -6,11 +6,7 @@
 #include <linux/slab.h>
 #include <linux/cpuhotplug.h>
 #include <linux/minmax.h>
-#include <asm/hypervisor.h>
 #include <asm/mshyperv.h>
-#include <asm/apic.h>
-
-#include <asm/trace/hyperv.h>
 
 /*
  * See struct hv_deposit_memory. The first u64 is partition ID, the rest
include/asm-generic/mshyperv.h +4 -1
index 98100466e0b2..faf5d27a76b1 100644
@@ -217,6 +217,10 @@ void *hv_alloc_hyperv_page(void);
 void *hv_alloc_hyperv_zeroed_page(void);
 void hv_free_hyperv_page(void *addr);
 
+int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages);
+int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id);
+int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags);
+
 /**
  * hv_cpu_number_to_vp_number() - Map CPU to VP.
  * @cpu_number: CPU number in Linux terms
--
Quoted 1 line

2.34.1