Skip to contentSkip to conversation

hv: Remove dependencies on guest page size

PATCH Applied Maya Nakamura 6 participants 14 messages Jul 12, 2019 – Sep 3, 2019 1 message not in the archive

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

v4 cover hv: Remove dependencies on guest page size

The Linux guest page size and hypervisor page size concepts are different, even though they happen to be the same value on x86. Hyper-V code mixes up the two, so this patchset begins to address that by creating and using a set of Hyper-V specific page definitions.

A major benefit of those new definitions is that they support non-x86 architectures, such as ARM64, that use different page sizes. On ARM64, the guest page size may not be 4096, and Hyper-V always runs with a page size of 4096.

In this patchset, the first two patches lay the foundation for the others, creating definitions and preparing for allocation of memory with the size and alignment that Hyper-V expects as a page. Patch 3 applies the page size definition where the guest VM and Hyper-V communicate, and where the code intends to use the Hyper-V page size. The last two patches set the ring buffer size to a fixed value, removing the dependency on the guest page size.

This is the initial set of changes to the Hyper-V code, and future patches will make additional changes using the same foundation, for example, replace __vmalloc() and related functions when Hyper-V pages are intended.

Changes in v4 (all apply to patch 2 only):
- Remove file name from the subject.
- Include prototypes of two new functions.
- Add another Link tag.

Changes in v3:
- Simplify expression for BUILD_BUG_ON() in patch 2.
- Add Link and Reviewed-by tags.

Change in v2:
- Replace patch 2 with a new one.

Maya Nakamura (5):
x86: hv: hyperv-tlfs.h: Create and use Hyper-V page definitions
x86: hv: Add functions to allocate/deallocate page for Hyper-V
hv: vmbus: Replace page definition with Hyper-V specific one
HID: hv: Remove dependencies on PAGE_SIZE for ring buffer
Input: hv: Remove dependencies on PAGE_SIZE for ring buffer

 arch/x86/hyperv/hv_init.c             | 14 ++++++++++++++
 arch/x86/include/asm/hyperv-tlfs.h    | 12 +++++++++++-
 arch/x86/include/asm/mshyperv.h       |  5 ++++-
 drivers/hid/hid-hyperv.c              |  4 ++--
 drivers/hv/hyperv_vmbus.h             |  8 ++++----
 drivers/input/serio/hyperv-keyboard.c |  4 ++--
 6 files changed, 37 insertions(+), 10 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH v4 1/5 x86: hv: hyperv-tlfs.h: Create and use Hyper-V page definitions

Define HV_HYP_PAGE_SHIFT, HV_HYP_PAGE_SIZE, and HV_HYP_PAGE_MASK because the Linux guest page size and hypervisor page size concepts are different, even though they happen to be the same value on x86.

Also, replace PAGE_SIZE with HV_HYP_PAGE_SIZE.

---
 arch/x86/include/asm/hyperv-tlfs.h | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
arch/x86/include/asm/hyperv-tlfs.h +11 -1
index af78cd72b8f3..7a2705694f5b 100644
@@ -12,6 +12,16 @@
 #include <linux/types.h>
 #include <asm/page.h>
 
+/*
+ * While not explicitly listed in the TLFS, Hyper-V always runs with a page size
+ * of 4096. These definitions are used when communicating with Hyper-V using
+ * guest physical pages and guest physical page addresses, since the guest page
+ * size may not be 4096 on all architectures.
+ */
+#define HV_HYP_PAGE_SHIFT      12
+#define HV_HYP_PAGE_SIZE       BIT(HV_HYP_PAGE_SHIFT)
+#define HV_HYP_PAGE_MASK       (~(HV_HYP_PAGE_SIZE - 1))
+
 /*
  * The below CPUID leaves are present if VersionAndFeatures.HypervisorPresent
  * is set by CPUID(HvCpuIdFunctionVersionAndFeatures).
@@ -847,7 +857,7 @@ union hv_gpa_page_range {
  * count is equal with how many entries of union hv_gpa_page_range can
  * be populated into the input parameter page.
  */
-#define HV_MAX_FLUSH_REP_COUNT ((PAGE_SIZE - 2 * sizeof(u64)) /	\
+#define HV_MAX_FLUSH_REP_COUNT ((HV_HYP_PAGE_SIZE - 2 * sizeof(u64)) /	\
 				sizeof(union hv_gpa_page_range))
 
 struct hv_guest_mapping_flush_list {
Signature · 1 line
2.17.1
#3 lore raw

PATCH v4 2/5 x86: hv: Add functions to allocate/deallocate page for Hyper-V

In reply to Maya Nakamura

Introduce two new functions, hv_alloc_hyperv_page() and hv_free_hyperv_page(), to allocate/deallocate memory with the size and alignment that Hyper-V expects as a page. Although currently they are not used, they are ready to be used to allocate/deallocate memory on x86 when their ARM64 counterparts are implemented, keeping symmetry between architectures with potentially different guest page sizes.

---
 arch/x86/hyperv/hv_init.c       | 14 ++++++++++++++
 arch/x86/include/asm/mshyperv.h |  5 ++++-
 2 files changed, 18 insertions(+), 1 deletion(-)
arch/x86/hyperv/hv_init.c +14 -0
index 0e033ef11a9f..e8960a83add7 100644
@@ -37,6 +37,20 @@ EXPORT_SYMBOL_GPL(hyperv_pcpu_input_arg);
 u32 hv_max_vp_index;
 EXPORT_SYMBOL_GPL(hv_max_vp_index);
 
+void *hv_alloc_hyperv_page(void)
+{
+	BUILD_BUG_ON(PAGE_SIZE != HV_HYP_PAGE_SIZE);
+
+	return (void *)__get_free_page(GFP_KERNEL);
+}
+EXPORT_SYMBOL_GPL(hv_alloc_hyperv_page);
+
+void hv_free_hyperv_page(unsigned long addr)
+{
+	free_page(addr);
+}
+EXPORT_SYMBOL_GPL(hv_free_hyperv_page);
+
 static int hv_cpu_init(unsigned int cpu)
 {
 	u64 msr_vp_index;
arch/x86/include/asm/mshyperv.h +4 -1
index 2a793bf6ebb0..32ec9df39a99 100644
@@ -218,7 +218,8 @@ static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu)
 
 void __init hyperv_init(void);
 void hyperv_setup_mmu_ops(void);
-
+void *hv_alloc_hyperv_page(void);
+void hv_free_hyperv_page(unsigned long addr);
 void hyperv_reenlightenment_intr(struct pt_regs *regs);
 void set_hv_tscchange_cb(void (*cb)(void));
 void clear_hv_tscchange_cb(void);
@@ -241,6 +242,8 @@ static inline void hv_apic_init(void) {}
 #else /* CONFIG_HYPERV */
 static inline void hyperv_init(void) {}
 static inline void hyperv_setup_mmu_ops(void) {}
+static inline void *hv_alloc_hyperv_page(void) { return NULL; }
+static inline void hv_free_hyperv_page(unsigned long addr) {}
 static inline void set_hv_tscchange_cb(void (*cb)(void)) {}
 static inline void clear_hv_tscchange_cb(void) {}
 static inline void hyperv_stop_tsc_emulation(void) {};
Signature · 1 line
2.17.1
#5 lore raw

PATCH v4 3/5 hv: vmbus: Replace page definition with Hyper-V specific one

In reply to Maya Nakamura

Replace PAGE_SIZE with HV_HYP_PAGE_SIZE because the guest page size may not be 4096 on all architectures and Hyper-V always runs with a page size of 4096.

---
 drivers/hv/hyperv_vmbus.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/hv/hyperv_vmbus.h +4 -4
index 362e70e9d145..019469c3cbca 100644
@@ -192,11 +192,11 @@ int hv_ringbuffer_read(struct vmbus_channel *channel,
 		       u64 *requestid, bool raw);
 
 /*
- * Maximum channels is determined by the size of the interrupt page
- * which is PAGE_SIZE. 1/2 of PAGE_SIZE is for send endpoint interrupt
- * and the other is receive endpoint interrupt
+ * Maximum channels, 16348, is determined by the size of the interrupt page,
+ * which is HV_HYP_PAGE_SIZE. 1/2 of HV_HYP_PAGE_SIZE is to send endpoint
+ * interrupt, and the other is to receive endpoint interrupt.
  */
-#define MAX_NUM_CHANNELS	((PAGE_SIZE >> 1) << 3)	/* 16348 channels */
+#define MAX_NUM_CHANNELS	((HV_HYP_PAGE_SIZE >> 1) << 3)
 
 /* The value here must be in multiple of 32 */
 /* TODO: Need to make this configurable */
Signature · 1 line
2.17.1
#9 lore raw

PATCH v4 4/5 HID: hv: Remove dependencies on PAGE_SIZE for ring buffer

In reply to Maya Nakamura

Define the ring buffer size as a constant expression because it should not depend on the guest page size.

---
 drivers/hid/hid-hyperv.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/hid/hid-hyperv.c +2 -2
index 7795831d37c2..cc5b09b87ab0 100644
@@ -104,8 +104,8 @@ struct synthhid_input_report {
 
 #pragma pack(pop)
 
-#define INPUTVSC_SEND_RING_BUFFER_SIZE		(10*PAGE_SIZE)
-#define INPUTVSC_RECV_RING_BUFFER_SIZE		(10*PAGE_SIZE)
+#define INPUTVSC_SEND_RING_BUFFER_SIZE		(40 * 1024)
+#define INPUTVSC_RECV_RING_BUFFER_SIZE		(40 * 1024)
 
 
 enum pipe_prot_msg_type {
Signature · 1 line
2.17.1
#13 lore raw

PATCH v4 5/5 Input: hv: Remove dependencies on PAGE_SIZE for ring buffer

In reply to Maya Nakamura

Define the ring buffer size as a constant expression because it should not depend on the guest page size.

---
 drivers/input/serio/hyperv-keyboard.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/input/serio/hyperv-keyboard.c +2 -2
index 8e457e50f837..88ae7c2ac3c8 100644
@@ -75,8 +75,8 @@ struct synth_kbd_keystroke {
 
 #define HK_MAXIMUM_MESSAGE_SIZE 256
 
-#define KBD_VSC_SEND_RING_BUFFER_SIZE		(10 * PAGE_SIZE)
-#define KBD_VSC_RECV_RING_BUFFER_SIZE		(10 * PAGE_SIZE)
+#define KBD_VSC_SEND_RING_BUFFER_SIZE		(40 * 1024)
+#define KBD_VSC_RECV_RING_BUFFER_SIZE		(40 * 1024)
 
 #define XTKBD_EMUL0     0xe0
 #define XTKBD_EMUL1     0xe1
Signature · 1 line
2.17.1
maintainer Applied
#14 lore raw

Re: [PATCH v4 5/5] Input: hv: Remove dependencies on PAGE_SIZE for ring buffer

Quoted 5 lines · Maya Nakamura

Define the ring buffer size as a constant expression because it should not depend on the guest page size.

Applied, thank you.

Quoted 22 lines
---
 drivers/input/serio/hyperv-keyboard.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/input/serio/hyperv-keyboard.c +2 -2
index 8e457e50f837..88ae7c2ac3c8 100644
@@ -75,8 +75,8 @@ struct synth_kbd_keystroke {
 
 #define HK_MAXIMUM_MESSAGE_SIZE 256
 
-#define KBD_VSC_SEND_RING_BUFFER_SIZE		(10 * PAGE_SIZE)
-#define KBD_VSC_RECV_RING_BUFFER_SIZE		(10 * PAGE_SIZE)
+#define KBD_VSC_SEND_RING_BUFFER_SIZE		(40 * 1024)
+#define KBD_VSC_RECV_RING_BUFFER_SIZE		(40 * 1024)
 
 #define XTKBD_EMUL0     0xe0
 #define XTKBD_EMUL1     0xe1
Signature · 1 line
2.17.1
Signature · 1 line
Dmitry
Acked-by on v4 3/5
#6 lore raw

Re: [PATCH v4 3/5] hv: vmbus: Replace page definition with Hyper-V specific one

In reply to Maya Nakamura
Quoted 7 lines · Maya Nakamura

Replace PAGE_SIZE with HV_HYP_PAGE_SIZE because the guest page size may not be 4096 on all architectures and Hyper-V always runs with a page size of 4096.

Thomas, if you're taking this series, could you grab this patch as well please (dependencies)?

--
Thanks,
Sasha

#7 lore raw

Re: [PATCH v4 3/5] hv: vmbus: Replace page definition with Hyper-V specific one

Quoted 5 lines · Sasha Levin
Quoted 4 lines · Maya Nakamura

Replace PAGE_SIZE with HV_HYP_PAGE_SIZE because the guest page size may not be 4096 on all architectures and Hyper-V always runs with a page size of 4096.

Quoted 8 lines
Quoted 3 lines

Thomas, if you're taking this series, could you grab this patch as well please (dependencies)?

Picked it up. The three commits are in

git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git x86/hyperv

If you have patches dependend on those either send them my way or pull the branch into your hyper-v tree.

Thanks,

tglx

#2 lore raw

[tip:x86/hyperv] x86/hyperv: Create and use Hyper-V page definitions

In reply to Maya Nakamura
x86/hyperv: Create and use Hyper-V page definitions
Commit-ID:  fcd3f6222a4ece735d0b3ffb93f646eff693aa69
Gitweb:     https://git.kernel.org/tip/fcd3f6222a4ece735d0b3ffb93f646eff693aa69
Author:     Maya Nakamura <[email protected]>
AuthorDate: Fri, 12 Jul 2019 08:14:47 +0000
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Mon, 22 Jul 2019 11:06:44 +0200

x86/hyperv: Create and use Hyper-V page definitions

Define HV_HYP_PAGE_SHIFT, HV_HYP_PAGE_SIZE, and HV_HYP_PAGE_MASK because the Linux guest page size and hypervisor page size concepts are different, even though they happen to be the same value on x86.

Also, replace PAGE_SIZE with HV_HYP_PAGE_SIZE.

---
 arch/x86/include/asm/hyperv-tlfs.h | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
arch/x86/include/asm/hyperv-tlfs.h +11 -1
index af78cd72b8f3..7a2705694f5b 100644
@@ -12,6 +12,16 @@
 #include <linux/types.h>
 #include <asm/page.h>
 
+/*
+ * While not explicitly listed in the TLFS, Hyper-V always runs with a page size
+ * of 4096. These definitions are used when communicating with Hyper-V using
+ * guest physical pages and guest physical page addresses, since the guest page
+ * size may not be 4096 on all architectures.
+ */
+#define HV_HYP_PAGE_SHIFT      12
+#define HV_HYP_PAGE_SIZE       BIT(HV_HYP_PAGE_SHIFT)
+#define HV_HYP_PAGE_MASK       (~(HV_HYP_PAGE_SIZE - 1))
+
 /*
  * The below CPUID leaves are present if VersionAndFeatures.HypervisorPresent
  * is set by CPUID(HvCpuIdFunctionVersionAndFeatures).
@@ -847,7 +857,7 @@ union hv_gpa_page_range {
  * count is equal with how many entries of union hv_gpa_page_range can
  * be populated into the input parameter page.
  */
-#define HV_MAX_FLUSH_REP_COUNT ((PAGE_SIZE - 2 * sizeof(u64)) /	\
+#define HV_MAX_FLUSH_REP_COUNT ((HV_HYP_PAGE_SIZE - 2 * sizeof(u64)) /	\
 				sizeof(union hv_gpa_page_range))
 
 struct hv_guest_mapping_flush_list {
#4 lore raw

[tip:x86/hyperv] x86/hyperv: Add functions to allocate/deallocate page for Hyper-V

In reply to Maya Nakamura
x86/hyperv: Add functions to allocate/deallocate page for Hyper-V
Commit-ID:  8c3e44bde7fd1b8291515f046008225711ac7beb
Gitweb:     https://git.kernel.org/tip/8c3e44bde7fd1b8291515f046008225711ac7beb
Author:     Maya Nakamura <[email protected]>
AuthorDate: Fri, 12 Jul 2019 08:21:25 +0000
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Mon, 22 Jul 2019 11:06:45 +0200

x86/hyperv: Add functions to allocate/deallocate page for Hyper-V

Introduce two new functions, hv_alloc_hyperv_page() and hv_free_hyperv_page(), to allocate/deallocate memory with the size and alignment that Hyper-V expects as a page. Although currently they are not used, they are ready to be used to allocate/deallocate memory on x86 when their ARM64 counterparts are implemented, keeping symmetry between architectures with potentially different guest page sizes.

---
 arch/x86/hyperv/hv_init.c       | 14 ++++++++++++++
 arch/x86/include/asm/mshyperv.h |  5 ++++-
 2 files changed, 18 insertions(+), 1 deletion(-)
arch/x86/hyperv/hv_init.c +14 -0
index 0d258688c8cf..d314cf1e15fd 100644
@@ -37,6 +37,20 @@ EXPORT_SYMBOL_GPL(hyperv_pcpu_input_arg);
 u32 hv_max_vp_index;
 EXPORT_SYMBOL_GPL(hv_max_vp_index);
 
+void *hv_alloc_hyperv_page(void)
+{
+	BUILD_BUG_ON(PAGE_SIZE != HV_HYP_PAGE_SIZE);
+
+	return (void *)__get_free_page(GFP_KERNEL);
+}
+EXPORT_SYMBOL_GPL(hv_alloc_hyperv_page);
+
+void hv_free_hyperv_page(unsigned long addr)
+{
+	free_page(addr);
+}
+EXPORT_SYMBOL_GPL(hv_free_hyperv_page);
+
 static int hv_cpu_init(unsigned int cpu)
 {
 	u64 msr_vp_index;
arch/x86/include/asm/mshyperv.h +4 -1
index 2ef31cc8c529..f4138aeb4280 100644
@@ -218,7 +218,8 @@ static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu)
 
 void __init hyperv_init(void);
 void hyperv_setup_mmu_ops(void);
-
+void *hv_alloc_hyperv_page(void);
+void hv_free_hyperv_page(unsigned long addr);
 void hyperv_reenlightenment_intr(struct pt_regs *regs);
 void set_hv_tscchange_cb(void (*cb)(void));
 void clear_hv_tscchange_cb(void);
@@ -241,6 +242,8 @@ static inline void hv_apic_init(void) {}
 #else /* CONFIG_HYPERV */
 static inline void hyperv_init(void) {}
 static inline void hyperv_setup_mmu_ops(void) {}
+static inline void *hv_alloc_hyperv_page(void) { return NULL; }
+static inline void hv_free_hyperv_page(unsigned long addr) {}
 static inline void set_hv_tscchange_cb(void (*cb)(void)) {}
 static inline void clear_hv_tscchange_cb(void) {}
 static inline void hyperv_stop_tsc_emulation(void) {};
#8 lore raw

[tip:x86/hyperv] drivers: hv: vmbus: Replace page definition with Hyper-V specific one

In reply to Maya Nakamura
drivers: hv: vmbus: Replace page definition with Hyper-V specific one
Commit-ID:  83527ef7abf7c02c33a90b00f0954db35415adbd
Gitweb:     https://git.kernel.org/tip/83527ef7abf7c02c33a90b00f0954db35415adbd
Author:     Maya Nakamura <[email protected]>
AuthorDate: Fri, 12 Jul 2019 08:25:18 +0000
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Mon, 22 Jul 2019 11:06:45 +0200

drivers: hv: vmbus: Replace page definition with Hyper-V specific one

Replace PAGE_SIZE with HV_HYP_PAGE_SIZE because the guest page size may not be 4096 on all architectures and Hyper-V always runs with a page size of 4096.

---
 drivers/hv/hyperv_vmbus.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/hv/hyperv_vmbus.h +4 -4
index 362e70e9d145..6bf64cb6e31a 100644
@@ -192,11 +192,11 @@ int hv_ringbuffer_read(struct vmbus_channel *channel,
 		       u64 *requestid, bool raw);
 
 /*
- * Maximum channels is determined by the size of the interrupt page
- * which is PAGE_SIZE. 1/2 of PAGE_SIZE is for send endpoint interrupt
- * and the other is receive endpoint interrupt
+ * The Maximum number of channels (16348) is determined by the size of the
+ * interrupt page, which is HV_HYP_PAGE_SIZE. 1/2 of HV_HYP_PAGE_SIZE is to
+ * send endpoint interrupts, and the other is to receive endpoint interrupts.
  */
-#define MAX_NUM_CHANNELS	((PAGE_SIZE >> 1) << 3)	/* 16348 channels */
+#define MAX_NUM_CHANNELS	((HV_HYP_PAGE_SIZE >> 1) << 3)
 
 /* The value here must be in multiple of 32 */
 /* TODO: Need to make this configurable */
maintainer Acked-by
#11 lore raw

RE: [PATCH v4 4/5] HID: hv: Remove dependencies on PAGE_SIZE for ring buffer

In reply to a message not in the archive
Quoted 5 lines · Michael Kelley

From: Maya Nakamura <[email protected]> Sent: Friday, July 12, 2019 1:28 AM

Quoted 4 lines

Define the ring buffer size as a constant expression because it should not depend on the guest page size.

Quoted 7 lines
Quoted 2 lines

Jiri and Benjamin -- OK if this small patch for the Hyper-V HID driver
goes through the Hyper-V tree maintained by Sasha Levin? It's a purely
Hyper-V change so the ring buffer size isn't bigger when running on ARM64 where the page size might be 16K or 64K.

Yeah; FWIW feel free to add

Thanks,

Signature · 2 lines
Jiri Kosina
SUSE Labs
Applied
#12 lore raw

Re: [PATCH v4 4/5] HID: hv: Remove dependencies on PAGE_SIZE for ring buffer

Quoted 10 lines · Jiri Kosina
Quoted 8 lines · Michael Kelley

From: Maya Nakamura <[email protected]> Sent: Friday, July 12, 2019 1:28 AM

Quoted 6 lines

Define the ring buffer size as a constant expression because it should not depend on the guest page size.

Quoted 8 lines
Quoted 4 lines

Jiri and Benjamin -- OK if this small patch for the Hyper-V HID driver
goes through the Hyper-V tree maintained by Sasha Levin? It's a purely
Hyper-V change so the ring buffer size isn't bigger when running on ARM64 where the page size might be 16K or 64K.

Yeah; FWIW feel free to add

Queued up for hyperv-next, thanks!

--
Thanks,
Sasha