Skip to contentSkip to conversation

drivers/hv: remove deposited pages from direct map

PATCH Superseded Magnus Kulke 5 participants 14 messages Sep 17 – Oct 5

Subsystems (3)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 drivers/hv: remove deposited pages from direct map

hv_call_deposit_pages() donates pages (deposit) to hypervisor for L2 guest on L1VH systems via HVCALL_DEPOSIT_MEMORY. The hypervisor takes ownership of those pages and per contract revokes root partition access to them, raising a #GP on access from the L1VH root partition.

However, the pages remain mapped in the kernel direct map, so kernel code may still access them even though the hypervisor has revoked access.

Helpers such as "load_unaligned_zeropad()" deliberately read past the end of a buffer and across page boundaries. A read into an unmapped page is tolerated and triggers a #PF, for which the kernel executed a fixup in the exception table.

If such a call steps into a page that has been deposited, the access raises a #GP by the hypervisor from which the above handler cannot recover and the kernel panics:

  Oops: general protection fault, maybe for address 0xff1100941a3dfffc
  RIP: 0010:csum_partial+0xe5/0x110

This condition will appear on L1VH system that have created L2 partitions (and hence deposited pages) and exercise networking code paths such as csum_partial() can trigger this condition when a buffer ends close to a page boundary (e.g fffc in the above example).

The fix is to remove the deposited pages from the direct map before they are passed to the hypervisor, and restore them when the hypervisor returns them again.

We want to avoid flushing the TLB in the loop, so we use the _noflush() variant of set_direct_map_valid() when marking a deposited page invalid and flush the affected page ranges in one go ourselves. In the opposite direction this is not required:

Quoted 4 lines

If a paging-structure entry is modified to change the P flag from 0 to 1, no invalidation is necessary. This is because no TLB entry or paging-structure cache entry is created with information from a paging-structure entry in which the P flag is 0.

(Intel SDM Vol. 3, 4.10.4.3)

---
Changes since v2:
- Checkpatch format fix

Changes since RFC:
- Handle direct-map restoration failures without returning unmapped pages to the allocator.
- Move freeing of withdrawn pages into the restoration helper.

---
 drivers/hv/hv_proc.c           | 77 +++++++++++++++++++++++++++++++++-
 drivers/hv/mshv_root_hv_call.c |  4 +-
 include/asm-generic/mshyperv.h |  4 ++
 3 files changed, 81 insertions(+), 4 deletions(-)
drivers/hv/hv_proc.c +76 -1
index 57b2c64197cb..2a392b45205d 100644
@@ -7,7 +7,9 @@
 #include <linux/cpuhotplug.h>
 #include <linux/minmax.h>
 #include <linux/export.h>
+#include <linux/set_memory.h>
 #include <asm/mshyperv.h>
+#include <asm/tlbflush.h>
 
 /*
  * See struct hv_deposit_memory. The first u64 is partition ID, the rest
@@ -15,6 +17,35 @@
  */
 #define HV_DEPOSIT_MAX (HV_HYP_PAGE_SIZE / sizeof(u64) - 1)
 
+/*
+ * Add or remove a set of physically contiguous page runs from the kernel
+ * direct map. Once a page has been deposited the hypervisor owns it and
+ * revokes root partition access to it.
+ */
+static int hv_deposit_update_direct_map(struct page **pages, int *counts,
+					int num_allocations, bool valid)
+{
+	int i, err, ret = 0;
+
+	for (i = 0; i < num_allocations; ++i) {
+		err = set_direct_map_valid_noflush(pages[i], counts[i], valid);
+		if (err && !ret)
+			ret = err;
+	}
+
+	if (valid)
+		return ret;
+
+	for (i = 0; i < num_allocations; ++i) {
+		unsigned long addr = (unsigned long)page_address(pages[i]);
+		unsigned long size = (unsigned long)counts[i] << PAGE_SHIFT;
+
+		flush_tlb_kernel_range(addr, addr + size);
+	}
+
+	return ret;
+}
+
 /* Deposits exact number of pages. Must be called with interrupts enabled.  */
 int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages)
 {
@@ -72,6 +103,10 @@ int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages)
 	}
 	num_allocations = i;
 
+	ret = hv_deposit_update_direct_map(pages, counts, num_allocations, false);
+	if (ret)
+		goto err_restore_direct_map;
+
 	local_irq_save(flags);
 
 	input_page = *this_cpu_ptr(hyperv_pcpu_input_arg);
@@ -90,12 +125,23 @@ int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages)
 	if (!hv_result_success(status)) {
 		hv_status_err(status, "\n");
 		ret = hv_result_to_errno(status);
-		goto err_free_allocations;
+		goto err_restore_direct_map;
 	}
 
 	ret = 0;
 	goto free_buf;
 
+err_restore_direct_map:
+	/*
+	 * We don't want to return pages to the allocator if weren't able to
+	 * mark them valid in the direct map.
+	 */
+	if (hv_deposit_update_direct_map(pages, counts, num_allocations, true)) {
+		WARN(1, "leaking %d page block(s) that could not be set to valid\n",
+		     num_allocations);
+		goto free_buf;
+	}
+
 err_free_allocations:
 	for (i = 0; i < num_allocations; ++i) {
 		base_pfn = page_to_pfn(pages[i]);
@@ -110,6 +156,35 @@ int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages)
 }
 EXPORT_SYMBOL_GPL(hv_call_deposit_pages);
 
+/*
+ * Put withdrawn pages back in the direct map. Counterpart to the direct map
+ * removal done by hv_call_deposit_pages().
+ */
+void hv_restore_withdrawn_pages(const u64 *pfns, int count)
+{
+	int i, ret = 0;
+	struct page *page;
+
+	for (i = 0; i < count; ++i) {
+		page = pfn_to_page(pfns[i]);
+		ret = set_direct_map_valid_noflush(page, 1, true);
+		/*
+		 * HV_DEPOSIT_MAX is capped at 511, so a deposit range cannot cover
+		 * a 2MiB page, so deposited pages are of 4k granularity and cannot
+		 * be collapses into a 2MiB page, which would require an allocation
+		 * and can potentially fail.
+		 *
+		 * Should it fail anyway we leak the page, if we would hand it
+		 * back to the allocator we would introduce faults into random other
+		 * parts.
+		 */
+		if (WARN_ON_ONCE(ret))
+			continue;
+		__free_page(page);
+	}
+}
+EXPORT_SYMBOL_GPL(hv_restore_withdrawn_pages);
+
 int hv_deposit_memory_node(int node, u64 partition_id,
 			   u64 hv_status)
 {
drivers/hv/mshv_root_hv_call.c +1 -3
index cb55d4d4be2e..150a0c63ebc8 100644
@@ -46,7 +46,6 @@ int hv_call_withdraw_memory(u64 count, int node, u64 partition_id)
 	struct page *page;
 	u16 completed;
 	u64 status, withdrawn = 0;
-	int i;
 	unsigned long flags;
 
 	page = alloc_page(GFP_KERNEL);
@@ -69,8 +68,7 @@ int hv_call_withdraw_memory(u64 count, int node, u64 partition_id)
 
 		completed = hv_repcomp(status);
 
-		for (i = 0; i < completed; i++)
-			__free_page(pfn_to_page(output_page->gpa_page_list[i]));
+		hv_restore_withdrawn_pages(output_page->gpa_page_list, completed);
 
 		if (!hv_result_success(status)) {
 			if (hv_result(status) == HV_STATUS_NO_RESOURCES)
include/asm-generic/mshyperv.h +4 -0
index bf601d67cecb..397c8ec0ce9a 100644
@@ -346,6 +346,7 @@ static inline bool hv_parent_partition(void)
 bool hv_result_needs_memory(u64 status);
 int hv_deposit_memory_node(int node, u64 partition_id, u64 status);
 int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages);
+void hv_restore_withdrawn_pages(const u64 *pfns, int count);
 int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id);
 int hv_call_notify_all_processors_started(void);
 bool hv_lp_exists(u32 lp_index);
@@ -364,6 +365,9 @@ static inline int hv_call_deposit_pages(int node, u64 partition_id, u32 num_page
 {
 	return -EOPNOTSUPP;
 }
+
+static inline void hv_restore_withdrawn_pages(const u64 *pfns, int count) { }
+
 static inline int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id)
 {
 	return -EOPNOTSUPP;
Signature · 1 line
2.34.1
#1 lore raw
Quoted 35 lines · Magnus Kulke

hv_call_deposit_pages() donates pages (deposit) to hypervisor for L2 guest on L1VH systems via HVCALL_DEPOSIT_MEMORY. The hypervisor takes ownership of those pages and per contract revokes root partition access to them, raising a #GP on access from the L1VH root partition.

However, the pages remain mapped in the kernel direct map, so kernel code may still access them even though the hypervisor has revoked access.

Helpers such as "load_unaligned_zeropad()" deliberately read past the end of a buffer and across page boundaries. A read into an unmapped page is tolerated and triggers a #PF, for which the kernel executed a fixup in the exception table.

If such a call steps into a page that has been deposited, the access raises a #GP by the hypervisor from which the above handler cannot recover and the kernel panics:

   Oops: general protection fault, maybe for address 0xff1100941a3dfffc
   RIP: 0010:csum_partial+0xe5/0x110

This condition will appear on L1VH system that have created L2 partitions (and hence deposited pages) and exercise networking code paths such as csum_partial() can trigger this condition when a buffer ends close to a page boundary (e.g fffc in the above example).

The fix is to remove the deposited pages from the direct map before they are passed to the hypervisor, and restore them when the hypervisor returns them again.

We want to avoid flushing the TLB in the loop, so we use the _noflush() variant of set_direct_map_valid() when marking a deposited page invalid and flush the affected page ranges in one go ourselves. In the opposite direction this is not required:

Quoted 8 lines
Quoted 4 lines

If a paging-structure entry is modified to change the P flag from 0 to 1, no invalidation is necessary. This is because no TLB entry or paging-structure cache entry is created with information from a paging-structure entry in which the P flag is 0.

(Intel SDM Vol. 3, 4.10.4.3)

FYI:

https://lore.kernel.org/linux-hyperv/[email protected]/

Thanks,
-Mukesh

Quoted 180 lines

---
Changes since v2:
- Checkpatch format fix

Changes since RFC:
- Handle direct-map restoration failures without returning unmapped pages to the allocator.
- Move freeing of withdrawn pages into the restoration helper.

---
  drivers/hv/hv_proc.c           | 77 +++++++++++++++++++++++++++++++++-
  drivers/hv/mshv_root_hv_call.c |  4 +-
  include/asm-generic/mshyperv.h |  4 ++
  3 files changed, 81 insertions(+), 4 deletions(-)
drivers/hv/hv_proc.c +76 -1
index 57b2c64197cb..2a392b45205d 100644
@@ -7,7 +7,9 @@
  #include <linux/cpuhotplug.h>
  #include <linux/minmax.h>
  #include <linux/export.h>
+#include <linux/set_memory.h>
  #include <asm/mshyperv.h>
+#include <asm/tlbflush.h>
 
  /*
   * See struct hv_deposit_memory. The first u64 is partition ID, the rest
@@ -15,6 +17,35 @@
   */
  #define HV_DEPOSIT_MAX (HV_HYP_PAGE_SIZE / sizeof(u64) - 1)
 
+/*
+ * Add or remove a set of physically contiguous page runs from the kernel
+ * direct map. Once a page has been deposited the hypervisor owns it and
+ * revokes root partition access to it.
+ */
+static int hv_deposit_update_direct_map(struct page **pages, int *counts,
+					int num_allocations, bool valid)
+{
+	int i, err, ret = 0;
+
+	for (i = 0; i < num_allocations; ++i) {
+		err = set_direct_map_valid_noflush(pages[i], counts[i], valid);
+		if (err && !ret)
+			ret = err;
+	}
+
+	if (valid)
+		return ret;
+
+	for (i = 0; i < num_allocations; ++i) {
+		unsigned long addr = (unsigned long)page_address(pages[i]);
+		unsigned long size = (unsigned long)counts[i] << PAGE_SHIFT;
+
+		flush_tlb_kernel_range(addr, addr + size);
+	}
+
+	return ret;
+}
+
  /* Deposits exact number of pages. Must be called with interrupts enabled.  */
  int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages)
  {
@@ -72,6 +103,10 @@ int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages)
  	}
  	num_allocations = i;
 
+	ret = hv_deposit_update_direct_map(pages, counts, num_allocations, false);
+	if (ret)
+		goto err_restore_direct_map;
+
  	local_irq_save(flags);
 
  	input_page = *this_cpu_ptr(hyperv_pcpu_input_arg);
@@ -90,12 +125,23 @@ int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages)
  	if (!hv_result_success(status)) {
  		hv_status_err(status, "\n");
  		ret = hv_result_to_errno(status);
-		goto err_free_allocations;
+		goto err_restore_direct_map;
  	}
 
  	ret = 0;
  	goto free_buf;
 
+err_restore_direct_map:
+	/*
+	 * We don't want to return pages to the allocator if weren't able to
+	 * mark them valid in the direct map.
+	 */
+	if (hv_deposit_update_direct_map(pages, counts, num_allocations, true)) {
+		WARN(1, "leaking %d page block(s) that could not be set to valid\n",
+		     num_allocations);
+		goto free_buf;
+	}
+
  err_free_allocations:
  	for (i = 0; i < num_allocations; ++i) {
  		base_pfn = page_to_pfn(pages[i]);
@@ -110,6 +156,35 @@ int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages)
  }
  EXPORT_SYMBOL_GPL(hv_call_deposit_pages);
 
+/*
+ * Put withdrawn pages back in the direct map. Counterpart to the direct map
+ * removal done by hv_call_deposit_pages().
+ */
+void hv_restore_withdrawn_pages(const u64 *pfns, int count)
+{
+	int i, ret = 0;
+	struct page *page;
+
+	for (i = 0; i < count; ++i) {
+		page = pfn_to_page(pfns[i]);
+		ret = set_direct_map_valid_noflush(page, 1, true);
+		/*
+		 * HV_DEPOSIT_MAX is capped at 511, so a deposit range cannot cover
+		 * a 2MiB page, so deposited pages are of 4k granularity and cannot
+		 * be collapses into a 2MiB page, which would require an allocation
+		 * and can potentially fail.
+		 *
+		 * Should it fail anyway we leak the page, if we would hand it
+		 * back to the allocator we would introduce faults into random other
+		 * parts.
+		 */
+		if (WARN_ON_ONCE(ret))
+			continue;
+		__free_page(page);
+	}
+}
+EXPORT_SYMBOL_GPL(hv_restore_withdrawn_pages);
+
  int hv_deposit_memory_node(int node, u64 partition_id,
  			   u64 hv_status)
  {
drivers/hv/mshv_root_hv_call.c +1 -3
index cb55d4d4be2e..150a0c63ebc8 100644
@@ -46,7 +46,6 @@ int hv_call_withdraw_memory(u64 count, int node, u64 partition_id)
  	struct page *page;
  	u16 completed;
  	u64 status, withdrawn = 0;
-	int i;
  	unsigned long flags;
 
  	page = alloc_page(GFP_KERNEL);
@@ -69,8 +68,7 @@ int hv_call_withdraw_memory(u64 count, int node, u64 partition_id)
 
  		completed = hv_repcomp(status);
 
-		for (i = 0; i < completed; i++)
-			__free_page(pfn_to_page(output_page->gpa_page_list[i]));
+		hv_restore_withdrawn_pages(output_page->gpa_page_list, completed);
 
  		if (!hv_result_success(status)) {
  			if (hv_result(status) == HV_STATUS_NO_RESOURCES)
include/asm-generic/mshyperv.h +4 -0
index bf601d67cecb..397c8ec0ce9a 100644
@@ -346,6 +346,7 @@ static inline bool hv_parent_partition(void)
  bool hv_result_needs_memory(u64 status);
  int hv_deposit_memory_node(int node, u64 partition_id, u64 status);
  int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages);
+void hv_restore_withdrawn_pages(const u64 *pfns, int count);
  int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id);
  int hv_call_notify_all_processors_started(void);
  bool hv_lp_exists(u32 lp_index);
@@ -364,6 +365,9 @@ static inline int hv_call_deposit_pages(int node, u64 partition_id, u32 num_page
  {
  	return -EOPNOTSUPP;
  }
+
+static inline void hv_restore_withdrawn_pages(const u64 *pfns, int count) { }
+
  static inline int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id)
  {
  	return -EOPNOTSUPP;
#2 lore raw
Quoted 10 lines · Mukesh R

On 9/17/26 13:10, Magnus Kulke wrote:

FYI:

https://lore.kernel.org/linux-hyperv/[email protected]/

Thanks,
-Mukesh

Thanks, the change to a larger single page makes sense. I assume we would still be susceptible to a #GP once the kernel steps into it?

Since this is addressing acute crashes on L1VH, it would be good to merge a fix first IMO. it can then be easily cherrypicked into backports or distro-kernels.

Or would you rather see the problem addressed as part of in your deposit-pages refactoring?

best,

magnus

maintainer
#3 lore raw
Quoted 12 lines · Magnus Kulke
Quoted 10 lines · Mukesh R

On 9/17/26 13:10, Magnus Kulke wrote:

FYI:

https://lore.kernel.org/linux-hyperv/[email protected]/

Thanks,
-Mukesh

Quoted 7 lines

Thanks, the change to a larger single page makes sense. I assume we would still be susceptible to a #GP once the kernel steps into it?

Since this is addressing acute crashes on L1VH, it would be good to merge a fix first IMO. it can then be easily cherrypicked into backports or distro-kernels.

I think we should get a fix that's easy to backport committed first.

Wei

Quoted 6 lines

Or would you rather see the problem addressed as part of in your deposit-pages refactoring?

best,

magnus

#4 lore raw
In reply to Magnus Kulke
Quoted 15 lines · Magnus Kulke
Quoted 10 lines · Mukesh R

On 9/17/26 13:10, Magnus Kulke wrote:

FYI:

https://lore.kernel.org/linux-hyperv/[email protected]/

Thanks,
-Mukesh

Thanks, the change to a larger single page makes sense. I assume we would still be susceptible to a #GP once the kernel steps into it?

Quoted 6 lines

Since this is addressing acute crashes on L1VH, it would be good to merge a fix first IMO. it can then be easily cherrypicked into backports or distro-kernels.

Or would you rather see the problem addressed as part of in your deposit-pages refactoring?

no, we need to backport deposit-pages also (it is already in the internal 6.18) as not only it enhances the deposit, but also fixes couple bugs, so best to keep this separate. the deposit patch does address your comment:

+ HV_DEPOSIT_MAX is capped at 511, so a deposit range cannot cover

 +    * a 2MiB page, so deposited pages are of 4k granularity and cannot
 +    * be collapses into a 2MiB page, which would require an allocation
 +    * and can potentially fail.

IOW, as soon as it's merged, above will not be true always.

Thanks,
-Mukesh

Quoted 3 lines

best,

magnus

#5 lore raw
Quoted 7 lines · Mukesh R
Quoted 1 line · Magnus Kulke

On Thu, Sep 17, 2026 at 05:23:15PM -0700, Mukesh R wrote:

no, we need to backport deposit-pages also (it is already in the internal 6.18) as not only it enhances the deposit, but also fixes couple bugs, so best to keep this separate. the deposit patch does address your comment:

Quoted 6 lines

+ HV_DEPOSIT_MAX is capped at 511, so a deposit range cannot cover

 +    * a 2MiB page, so deposited pages are of 4k granularity and cannot
 +    * be collapses into a 2MiB page, which would require an allocation
 +    * and can potentially fail.

IOW, as soon as it's merged, above will not be true always.

understood, so we'll drop that comment. but we can keep the logic? (leaking the page should the make-valid call fail)

best,

magnus

maintainer
#6 lore raw
In reply to Magnus Kulke
Quoted 35 lines · Magnus Kulke

hv_call_deposit_pages() donates pages (deposit) to hypervisor for L2 guest on L1VH systems via HVCALL_DEPOSIT_MEMORY. The hypervisor takes ownership of those pages and per contract revokes root partition access to them, raising a #GP on access from the L1VH root partition.

However, the pages remain mapped in the kernel direct map, so kernel code may still access them even though the hypervisor has revoked access.

Helpers such as "load_unaligned_zeropad()" deliberately read past the end of a buffer and across page boundaries. A read into an unmapped page is tolerated and triggers a #PF, for which the kernel executed a fixup in the exception table.

If such a call steps into a page that has been deposited, the access raises a #GP by the hypervisor from which the above handler cannot recover and the kernel panics:

  Oops: general protection fault, maybe for address 0xff1100941a3dfffc
  RIP: 0010:csum_partial+0xe5/0x110

This condition will appear on L1VH system that have created L2 partitions (and hence deposited pages) and exercise networking code paths such as csum_partial() can trigger this condition when a buffer ends close to a page boundary (e.g fffc in the above example).

The fix is to remove the deposited pages from the direct map before they are passed to the hypervisor, and restore them when the hypervisor returns them again.

We want to avoid flushing the TLB in the loop, so we use the _noflush() variant of set_direct_map_valid() when marking a deposited page invalid and flush the affected page ranges in one go ourselves. In the opposite direction this is not required:

Quoted 8 lines
Quoted 4 lines

If a paging-structure entry is modified to change the P flag from 0 to 1, no invalidation is necessary. This is because no TLB entry or paging-structure cache entry is created with information from a paging-structure entry in which the P flag is 0.

(Intel SDM Vol. 3, 4.10.4.3)

None of the code added is arch-specific. Hopefully ARM64 is okay.

I will wait for Souradeep and Anirudh's review on the ARM64 side before taking further action.

Wei

Quoted 183 lines

---
Changes since v2:
- Checkpatch format fix

Changes since RFC:
- Handle direct-map restoration failures without returning unmapped pages to the allocator.
- Move freeing of withdrawn pages into the restoration helper.

---
 drivers/hv/hv_proc.c           | 77 +++++++++++++++++++++++++++++++++-
 drivers/hv/mshv_root_hv_call.c |  4 +-
 include/asm-generic/mshyperv.h |  4 ++
 3 files changed, 81 insertions(+), 4 deletions(-)
drivers/hv/hv_proc.c +76 -1
index 57b2c64197cb..2a392b45205d 100644
@@ -7,7 +7,9 @@
 #include <linux/cpuhotplug.h>
 #include <linux/minmax.h>
 #include <linux/export.h>
+#include <linux/set_memory.h>
 #include <asm/mshyperv.h>
+#include <asm/tlbflush.h>
 
 /*
  * See struct hv_deposit_memory. The first u64 is partition ID, the rest
@@ -15,6 +17,35 @@
  */
 #define HV_DEPOSIT_MAX (HV_HYP_PAGE_SIZE / sizeof(u64) - 1)
 
+/*
+ * Add or remove a set of physically contiguous page runs from the kernel
+ * direct map. Once a page has been deposited the hypervisor owns it and
+ * revokes root partition access to it.
+ */
+static int hv_deposit_update_direct_map(struct page **pages, int *counts,
+					int num_allocations, bool valid)
+{
+	int i, err, ret = 0;
+
+	for (i = 0; i < num_allocations; ++i) {
+		err = set_direct_map_valid_noflush(pages[i], counts[i], valid);
+		if (err && !ret)
+			ret = err;
+	}
+
+	if (valid)
+		return ret;
+
+	for (i = 0; i < num_allocations; ++i) {
+		unsigned long addr = (unsigned long)page_address(pages[i]);
+		unsigned long size = (unsigned long)counts[i] << PAGE_SHIFT;
+
+		flush_tlb_kernel_range(addr, addr + size);
+	}
+
+	return ret;
+}
+
 /* Deposits exact number of pages. Must be called with interrupts enabled.  */
 int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages)
 {
@@ -72,6 +103,10 @@ int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages)
 	}
 	num_allocations = i;
 
+	ret = hv_deposit_update_direct_map(pages, counts, num_allocations, false);
+	if (ret)
+		goto err_restore_direct_map;
+
 	local_irq_save(flags);
 
 	input_page = *this_cpu_ptr(hyperv_pcpu_input_arg);
@@ -90,12 +125,23 @@ int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages)
 	if (!hv_result_success(status)) {
 		hv_status_err(status, "\n");
 		ret = hv_result_to_errno(status);
-		goto err_free_allocations;
+		goto err_restore_direct_map;
 	}
 
 	ret = 0;
 	goto free_buf;
 
+err_restore_direct_map:
+	/*
+	 * We don't want to return pages to the allocator if weren't able to
+	 * mark them valid in the direct map.
+	 */
+	if (hv_deposit_update_direct_map(pages, counts, num_allocations, true)) {
+		WARN(1, "leaking %d page block(s) that could not be set to valid\n",
+		     num_allocations);
+		goto free_buf;
+	}
+
 err_free_allocations:
 	for (i = 0; i < num_allocations; ++i) {
 		base_pfn = page_to_pfn(pages[i]);
@@ -110,6 +156,35 @@ int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages)
 }
 EXPORT_SYMBOL_GPL(hv_call_deposit_pages);
 
+/*
+ * Put withdrawn pages back in the direct map. Counterpart to the direct map
+ * removal done by hv_call_deposit_pages().
+ */
+void hv_restore_withdrawn_pages(const u64 *pfns, int count)
+{
+	int i, ret = 0;
+	struct page *page;
+
+	for (i = 0; i < count; ++i) {
+		page = pfn_to_page(pfns[i]);
+		ret = set_direct_map_valid_noflush(page, 1, true);
+		/*
+		 * HV_DEPOSIT_MAX is capped at 511, so a deposit range cannot cover
+		 * a 2MiB page, so deposited pages are of 4k granularity and cannot
+		 * be collapses into a 2MiB page, which would require an allocation
+		 * and can potentially fail.
+		 *
+		 * Should it fail anyway we leak the page, if we would hand it
+		 * back to the allocator we would introduce faults into random other
+		 * parts.
+		 */
+		if (WARN_ON_ONCE(ret))
+			continue;
+		__free_page(page);
+	}
+}
+EXPORT_SYMBOL_GPL(hv_restore_withdrawn_pages);
+
 int hv_deposit_memory_node(int node, u64 partition_id,
 			   u64 hv_status)
 {
drivers/hv/mshv_root_hv_call.c +1 -3
index cb55d4d4be2e..150a0c63ebc8 100644
@@ -46,7 +46,6 @@ int hv_call_withdraw_memory(u64 count, int node, u64 partition_id)
 	struct page *page;
 	u16 completed;
 	u64 status, withdrawn = 0;
-	int i;
 	unsigned long flags;
 
 	page = alloc_page(GFP_KERNEL);
@@ -69,8 +68,7 @@ int hv_call_withdraw_memory(u64 count, int node, u64 partition_id)
 
 		completed = hv_repcomp(status);
 
-		for (i = 0; i < completed; i++)
-			__free_page(pfn_to_page(output_page->gpa_page_list[i]));
+		hv_restore_withdrawn_pages(output_page->gpa_page_list, completed);
 
 		if (!hv_result_success(status)) {
 			if (hv_result(status) == HV_STATUS_NO_RESOURCES)
include/asm-generic/mshyperv.h +4 -0
index bf601d67cecb..397c8ec0ce9a 100644
@@ -346,6 +346,7 @@ static inline bool hv_parent_partition(void)
 bool hv_result_needs_memory(u64 status);
 int hv_deposit_memory_node(int node, u64 partition_id, u64 status);
 int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages);
+void hv_restore_withdrawn_pages(const u64 *pfns, int count);
 int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id);
 int hv_call_notify_all_processors_started(void);
 bool hv_lp_exists(u32 lp_index);
@@ -364,6 +365,9 @@ static inline int hv_call_deposit_pages(int node, u64 partition_id, u32 num_page
 {
 	return -EOPNOTSUPP;
 }
+
+static inline void hv_restore_withdrawn_pages(const u64 *pfns, int count) { }
+
 static inline int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id)
 {
 	return -EOPNOTSUPP;
Signature · 1 line
2.34.1
#7 lore raw
In reply to Magnus Kulke
Quoted 77 lines · Magnus Kulke

hv_call_deposit_pages() donates pages (deposit) to hypervisor for L2 guest on L1VH systems via HVCALL_DEPOSIT_MEMORY. The hypervisor takes ownership of those pages and per contract revokes root partition access to them, raising a #GP on access from the L1VH root partition.

However, the pages remain mapped in the kernel direct map, so kernel code may still access them even though the hypervisor has revoked access.

Helpers such as "load_unaligned_zeropad()" deliberately read past the end of a buffer and across page boundaries. A read into an unmapped page is tolerated and triggers a #PF, for which the kernel executed a fixup in the exception table.

If such a call steps into a page that has been deposited, the access raises a #GP by the hypervisor from which the above handler cannot recover and the kernel panics:

  Oops: general protection fault, maybe for address 0xff1100941a3dfffc
  RIP: 0010:csum_partial+0xe5/0x110

This condition will appear on L1VH system that have created L2 partitions (and hence deposited pages) and exercise networking code paths such as csum_partial() can trigger this condition when a buffer ends close to a page boundary (e.g fffc in the above example).

The fix is to remove the deposited pages from the direct map before they are passed to the hypervisor, and restore them when the hypervisor returns them again.

We want to avoid flushing the TLB in the loop, so we use the _noflush() variant of set_direct_map_valid() when marking a deposited page invalid and flush the affected page ranges in one go ourselves. In the opposite direction this is not required:

Quoted 4 lines

If a paging-structure entry is modified to change the P flag from 0 to 1, no invalidation is necessary. This is because no TLB entry or paging-structure cache entry is created with information from a paging-structure entry in which the P flag is 0.

(Intel SDM Vol. 3, 4.10.4.3)

---
Changes since v2:
- Checkpatch format fix

Changes since RFC:
- Handle direct-map restoration failures without returning unmapped pages to the allocator.
- Move freeing of withdrawn pages into the restoration helper.

---
 drivers/hv/hv_proc.c           | 77 +++++++++++++++++++++++++++++++++-
 drivers/hv/mshv_root_hv_call.c |  4 +-
 include/asm-generic/mshyperv.h |  4 ++
 3 files changed, 81 insertions(+), 4 deletions(-)
drivers/hv/hv_proc.c +4 -0
index 57b2c64197cb..2a392b45205d 100644
@@ -7,7 +7,9 @@
 #include <linux/cpuhotplug.h>
 #include <linux/minmax.h>
 #include <linux/export.h>
+#include <linux/set_memory.h>
 #include <asm/mshyperv.h>
+#include <asm/tlbflush.h>
 
 /*
  * See struct hv_deposit_memory. The first u64 is partition ID, the rest
@@ -15,6 +17,35 @@
  */
 #define HV_DEPOSIT_MAX (HV_HYP_PAGE_SIZE / sizeof(u64) - 1)
 
+/*
+ * Add or remove a set of physically contiguous page runs from the kernel

drivers/hv/hv_proc.c line 31

Quoted 10 lines
drivers/hv/hv_proc.c +10 -0
+ * direct map. Once a page has been deposited the hypervisor owns it and
+ * revokes root partition access to it.
+ */
+static int hv_deposit_update_direct_map(struct page **pages, int *counts,
+					int num_allocations, bool valid)
+{
+	int i, err, ret = 0;
+
+	for (i = 0; i < num_allocations; ++i) {
+		err = set_direct_map_valid_noflush(pages[i], counts[i], valid);

On arm64 can_set_direct_map() can return false making the set_direct_map_valid_noflush() call a no-op.

	bool can_set_direct_map(void)
	{
		/*
		 * rodata_full, DEBUG_PAGEALLOC and a Realm guest all require linear
		 * map to be mapped at page granularity, so that it is possible to
		 * protect/unprotect single pages.
		 *
		 * KFENCE pool requires page-granular mapping if initialized late.
		 *
		 * Realms need to make pages shared/protected at page granularity.
		 */
		return rodata_full || debug_pagealloc_enabled() ||
			arm64_kfence_can_set_direct_map() || is_realm_world();
	}

So, there could be some scenarios where this patch doesn't solve the problem. It is definitely an improvement over what's currently in the tree though.

Quoted 62 lines
drivers/hv/hv_proc.c +35 -1
+		if (err && !ret)
+			ret = err;
+	}
+
+	if (valid)
+		return ret;
+
+	for (i = 0; i < num_allocations; ++i) {
+		unsigned long addr = (unsigned long)page_address(pages[i]);
+		unsigned long size = (unsigned long)counts[i] << PAGE_SHIFT;
+
+		flush_tlb_kernel_range(addr, addr + size);
+	}
+
+	return ret;
+}
+
 /* Deposits exact number of pages. Must be called with interrupts enabled.  */
 int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages)
 {
@@ -72,6 +103,10 @@ int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages)
 	}
 	num_allocations = i;
 
+	ret = hv_deposit_update_direct_map(pages, counts, num_allocations, false);
+	if (ret)
+		goto err_restore_direct_map;
+
 	local_irq_save(flags);
 
 	input_page = *this_cpu_ptr(hyperv_pcpu_input_arg);
@@ -90,12 +125,23 @@ int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages)
 	if (!hv_result_success(status)) {
 		hv_status_err(status, "\n");
 		ret = hv_result_to_errno(status);
-		goto err_free_allocations;
+		goto err_restore_direct_map;
 	}
 
 	ret = 0;
 	goto free_buf;
 
+err_restore_direct_map:
+	/*
+	 * We don't want to return pages to the allocator if weren't able to
+	 * mark them valid in the direct map.
+	 */
+	if (hv_deposit_update_direct_map(pages, counts, num_allocations, true)) {
+		WARN(1, "leaking %d page block(s) that could not be set to valid\n",
+		     num_allocations);
+		goto free_buf;
+	}
+
 err_free_allocations:
 	for (i = 0; i < num_allocations; ++i) {
 		base_pfn = page_to_pfn(pages[i]);
@@ -110,6 +156,35 @@ int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages)
 }
 EXPORT_SYMBOL_GPL(hv_call_deposit_pages);
 
+/*
+ * Put withdrawn pages back in the direct map. Counterpart to the direct map

drivers/hv/hv_proc.c int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages) · line 170

Quoted 10 lines
drivers/hv/hv_proc.c +10 -0
+ * removal done by hv_call_deposit_pages().
+ */
+void hv_restore_withdrawn_pages(const u64 *pfns, int count)
+{
+	int i, ret = 0;
+	struct page *page;
+
+	for (i = 0; i < count; ++i) {
+		page = pfn_to_page(pfns[i]);
+		ret = set_direct_map_valid_noflush(page, 1, true);

Should we batch this? (i.e. collect a batch of contiguous PFNs and restore them at once)

drivers/hv/hv_proc.c int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages) · line 174

Quoted 4 lines
drivers/hv/hv_proc.c +4 -0
+		/*
+		 * HV_DEPOSIT_MAX is capped at 511, so a deposit range cannot cover
+		 * a 2MiB page, so deposited pages are of 4k granularity and cannot
+		 * be collapses into a 2MiB page, which would require an allocation

511 is the limit for one deposit call. But after multiple deposit calls, a deposited range can cover a 2 MiB page.

drivers/hv/hv_proc.c int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages) · line 175

Quoted 1 line
drivers/hv/hv_proc.c +1 -0
+		 * and can potentially fail.

It is unclear to me what requires an allocation and can potentially fail. Could you please clarify?

drivers/hv/hv_proc.c int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages) · line 179

Quoted 4 lines
drivers/hv/hv_proc.c +4 -0
+		 *
+		 * Should it fail anyway we leak the page, if we would hand it
+		 * back to the allocator we would introduce faults into random other
+		 * parts.

I agree this is what we should do. I just don't understand what the first part of this comment block is talking about.

Thanks,
Anirudh.

#8 lore raw
Quoted 8 lines · Anirudh Rayabharam

drivers/hv/hv_proc.c int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages) · line 170

Quoted 4 lines · Magnus Kulke
drivers/hv/hv_proc.c +4 -0
+
+	for (i = 0; i < count; ++i) {
+		page = pfn_to_page(pfns[i]);
+		ret = set_direct_map_valid_noflush(page, 1, true);

Should we batch this? (i.e. collect a batch of contiguous PFNs and restore them at once)

yeah, seems conceivable. let me check.

Quoted 9 lines

drivers/hv/hv_proc.c int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages) · line 174

Quoted 5 lines
drivers/hv/hv_proc.c +4 -0
+		/*
+		 * HV_DEPOSIT_MAX is capped at 511, so a deposit range cannot cover
+		 * a 2MiB page, so deposited pages are of 4k granularity and cannot
+		 * be collapses into a 2MiB page, which would require an allocation

511 is the limit for one deposit call. But after multiple deposit calls, a deposited range can cover a 2 MiB page.

Quoted 5 lines

drivers/hv/hv_proc.c int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages) · line 175

Quoted 1 line
drivers/hv/hv_proc.c +1 -0
+		 * and can potentially fail.

It is unclear to me what requires an allocation and can potentially fail. Could you please clarify?

yeah, admittedly this is properly an overly defensive comment. the point
was: set_direct_map_valid_noflush() is fallible. I understand one of the reasons for it to fail would be reoganization of the direct map, which would imply allocations, which in turn could fail.

but this is contrived and should not happen for the deposit case. Also, with Mukesh's proposed rework, this comment would not be accurate anymore. So, I'd remove the reasoning from the comment (but keep the logic), ack?

Quoted 10 lines

drivers/hv/hv_proc.c int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages) · line 179

Quoted 4 lines
drivers/hv/hv_proc.c +4 -0
+		 *
+		 * Should it fail anyway we leak the page, if we would hand it
+		 * back to the allocator we would introduce faults into random other
+		 * parts.

I agree this is what we should do. I just don't understand what the first part of this comment block is talking about.

Thanks,
Anirudh.

#9 lore raw
Quoted 27 lines · Magnus Kulke
Quoted 8 lines · Anirudh Rayabharam

drivers/hv/hv_proc.c int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages) · line 170

Quoted 4 lines · Magnus Kulke
drivers/hv/hv_proc.c +4 -0
+
+	for (i = 0; i < count; ++i) {
+		page = pfn_to_page(pfns[i]);
+		ret = set_direct_map_valid_noflush(page, 1, true);

Should we batch this? (i.e. collect a batch of contiguous PFNs and restore them at once)

yeah, seems conceivable. let me check.

Quoted 9 lines

drivers/hv/hv_proc.c int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages) · line 174

Quoted 5 lines
drivers/hv/hv_proc.c +4 -0
+		/*
+		 * HV_DEPOSIT_MAX is capped at 511, so a deposit range cannot cover
+		 * a 2MiB page, so deposited pages are of 4k granularity and cannot
+		 * be collapses into a 2MiB page, which would require an allocation

511 is the limit for one deposit call. But after multiple deposit calls, a deposited range can cover a 2 MiB page.

Quoted 5 lines

drivers/hv/hv_proc.c int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages) · line 175

Quoted 1 line
drivers/hv/hv_proc.c +1 -0
+		 * and can potentially fail.

It is unclear to me what requires an allocation and can potentially fail. Could you please clarify?

Quoted 8 lines

yeah, admittedly this is properly an overly defensive comment. the point
was: set_direct_map_valid_noflush() is fallible. I understand one of the reasons for it to fail would be reoganization of the direct map, which would imply allocations, which in turn could fail.

but this is contrived and should not happen for the deposit case. Also, with Mukesh's proposed rework, this comment would not be accurate anymore. So, I'd remove the reasoning from the comment (but keep the logic), ack?

Yeah, sounds good!

Thanks,
Anirudh.

Quoted 11 lines
Quoted 11 lines

drivers/hv/hv_proc.c int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages) · line 179

Quoted 5 lines
drivers/hv/hv_proc.c +4 -0
+		 *
+		 * Should it fail anyway we leak the page, if we would hand it
+		 * back to the allocator we would introduce faults into random other
+		 * parts.

I agree this is what we should do. I just don't understand what the first part of this comment block is talking about.

Thanks,
Anirudh.

#10 lore raw
In reply to Magnus Kulke

From: Magnus Kulke <[email protected]> Sent: Thursday, September 17, 2026 1:11 PM

Quoted 6 lines

hv_call_deposit_pages() donates pages (deposit) to hypervisor for L2 guest on L1VH systems via HVCALL_DEPOSIT_MEMORY. The hypervisor takes ownership of those pages and per contract revokes root partition access to them, raising a #GP on access from the L1VH root partition.

Quoted 7 lines

However, the pages remain mapped in the kernel direct map, so kernel code may still access them even though the hypervisor has revoked access.

Helpers such as "load_unaligned_zeropad()" deliberately read past the end of a buffer and across page boundaries. A read into an unmapped page is tolerated and triggers a #PF, for which the kernel executed

s/executed/executes/

Quoted 8 lines

a fixup in the exception table.

If such a call steps into a page that has been deposited, the access raises a #GP by the hypervisor from which the above handler cannot recover and the kernel panics:

  Oops: general protection fault, maybe for address 0xff1100941a3dfffc
  RIP: 0010:csum_partial+0xe5/0x110

Is there any possibility of updating the load_unaligned_zeropad() fixup handler to handle the #GP like #PF? I had looked at a variant of this problem a few years back for CoCo VMs. See the code comment above hv_vtom_clear_present(). In that case, a smarter load_unaligned_zeropad() fixup handler wasn't an option because these were #VC or #VE exceptions routed to the paravisor instead of the main Linux guest. But in your case, the Linux gets the #GP, so I wondered if a smarter fixup handler would be possible. Of course, both the x86 and arm64 versions would need updates.

Quoted 57 lines

This condition will appear on L1VH system that have created L2 partitions (and hence deposited pages) and exercise networking code paths such as csum_partial() can trigger this condition when a buffer ends close to a page boundary (e.g fffc in the above example).

The fix is to remove the deposited pages from the direct map before they are passed to the hypervisor, and restore them when the hypervisor returns them again.

We want to avoid flushing the TLB in the loop, so we use the _noflush() variant of set_direct_map_valid() when marking a deposited page invalid and flush the affected page ranges in one go ourselves. In the opposite direction this is not required:

Quoted 4 lines

If a paging-structure entry is modified to change the P flag from 0 to 1, no invalidation is necessary. This is because no TLB entry or paging-structure cache entry is created with information from a paging-structure entry in which the P flag is 0.

(Intel SDM Vol. 3, 4.10.4.3)

---
Changes since v2:
- Checkpatch format fix

Changes since RFC:
- Handle direct-map restoration failures without returning unmapped pages to the allocator.
- Move freeing of withdrawn pages into the restoration helper.

---
 drivers/hv/hv_proc.c           | 77 +++++++++++++++++++++++++++++++++-
 drivers/hv/mshv_root_hv_call.c |  4 +-
 include/asm-generic/mshyperv.h |  4 ++
 3 files changed, 81 insertions(+), 4 deletions(-)
drivers/hv/hv_proc.c +4 -0
index 57b2c64197cb..2a392b45205d 100644
@@ -7,7 +7,9 @@
 #include <linux/cpuhotplug.h>
 #include <linux/minmax.h>
 #include <linux/export.h>
+#include <linux/set_memory.h>
 #include <asm/mshyperv.h>
+#include <asm/tlbflush.h>
 
 /*
  * See struct hv_deposit_memory. The first u64 is partition ID, the rest
@@ -15,6 +17,35 @@
  */
 #define HV_DEPOSIT_MAX (HV_HYP_PAGE_SIZE / sizeof(u64) - 1)
 
+/*
+ * Add or remove a set of physically contiguous page runs from the kernel

drivers/hv/hv_proc.c line 31

Quoted 10 lines
drivers/hv/hv_proc.c +10 -0
+ * direct map. Once a page has been deposited the hypervisor owns it and
+ * revokes root partition access to it.
+ */
+static int hv_deposit_update_direct_map(struct page **pages, int *counts,
+					int num_allocations, bool valid)
+{
+	int i, err, ret = 0;
+
+	for (i = 0; i < num_allocations; ++i) {
+		err = set_direct_map_valid_noflush(pages[i], counts[i], valid);

Just a heads up, there's a patch set pending that eliminates the API set_direct_map_valid_noflush(). [1] As described in that cover letter, the the behavior is inconsistent across architectures. The intent is that set_direct_map_invalid_noflush() and set_direct_map_default_noflush() should be used instead. But the latter two currently operate one page at-a-time, so they are being updated to take a page count argument.

Michael

[1] https://lore.kernel.org/lkml/[email protected]/

Quoted 139 lines
drivers/hv/hv_proc.c +62 -1
+		if (err && !ret)
+			ret = err;
+	}
+
+	if (valid)
+		return ret;
+
+	for (i = 0; i < num_allocations; ++i) {
+		unsigned long addr = (unsigned long)page_address(pages[i]);
+		unsigned long size = (unsigned long)counts[i] << PAGE_SHIFT;
+
+		flush_tlb_kernel_range(addr, addr + size);
+	}
+
+	return ret;
+}
+
 /* Deposits exact number of pages. Must be called with interrupts enabled.  */
 int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages)
 {
@@ -72,6 +103,10 @@ int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages)
 	}
 	num_allocations = i;
 
+	ret = hv_deposit_update_direct_map(pages, counts, num_allocations, false);
+	if (ret)
+		goto err_restore_direct_map;
+
 	local_irq_save(flags);
 
 	input_page = *this_cpu_ptr(hyperv_pcpu_input_arg);
@@ -90,12 +125,23 @@ int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages)
 	if (!hv_result_success(status)) {
 		hv_status_err(status, "\n");
 		ret = hv_result_to_errno(status);
-		goto err_free_allocations;
+		goto err_restore_direct_map;
 	}
 
 	ret = 0;
 	goto free_buf;
 
+err_restore_direct_map:
+	/*
+	 * We don't want to return pages to the allocator if weren't able to
+	 * mark them valid in the direct map.
+	 */
+	if (hv_deposit_update_direct_map(pages, counts, num_allocations, true)) {
+		WARN(1, "leaking %d page block(s) that could not be set to valid\n",
+		     num_allocations);
+		goto free_buf;
+	}
+
 err_free_allocations:
 	for (i = 0; i < num_allocations; ++i) {
 		base_pfn = page_to_pfn(pages[i]);
@@ -110,6 +156,35 @@ int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages)
 }
 EXPORT_SYMBOL_GPL(hv_call_deposit_pages);
 
+/*
+ * Put withdrawn pages back in the direct map. Counterpart to the direct map
+ * removal done by hv_call_deposit_pages().
+ */
+void hv_restore_withdrawn_pages(const u64 *pfns, int count)
+{
+	int i, ret = 0;
+	struct page *page;
+
+	for (i = 0; i < count; ++i) {
+		page = pfn_to_page(pfns[i]);
+		ret = set_direct_map_valid_noflush(page, 1, true);
+		/*
+		 * HV_DEPOSIT_MAX is capped at 511, so a deposit range cannot cover
+		 * a 2MiB page, so deposited pages are of 4k granularity and cannot
+		 * be collapses into a 2MiB page, which would require an allocation
+		 * and can potentially fail.
+		 *
+		 * Should it fail anyway we leak the page, if we would hand it
+		 * back to the allocator we would introduce faults into random other
+		 * parts.
+		 */
+		if (WARN_ON_ONCE(ret))
+			continue;
+		__free_page(page);
+	}
+}
+EXPORT_SYMBOL_GPL(hv_restore_withdrawn_pages);
+
 int hv_deposit_memory_node(int node, u64 partition_id,
 			   u64 hv_status)
 {
drivers/hv/mshv_root_hv_call.c +1 -3
index cb55d4d4be2e..150a0c63ebc8 100644
@@ -46,7 +46,6 @@ int hv_call_withdraw_memory(u64 count, int node, u64 partition_id)
 	struct page *page;
 	u16 completed;
 	u64 status, withdrawn = 0;
-	int i;
 	unsigned long flags;
 
 	page = alloc_page(GFP_KERNEL);
@@ -69,8 +68,7 @@ int hv_call_withdraw_memory(u64 count, int node, u64 partition_id)
 
 		completed = hv_repcomp(status);
 
-		for (i = 0; i < completed; i++)
-			__free_page(pfn_to_page(output_page->gpa_page_list[i]));
+		hv_restore_withdrawn_pages(output_page->gpa_page_list, completed);
 
 		if (!hv_result_success(status)) {
 			if (hv_result(status) == HV_STATUS_NO_RESOURCES)
include/asm-generic/mshyperv.h +4 -1
index bf601d67cecb..397c8ec0ce9a 100644
@@ -346,6 +346,7 @@ static inline bool hv_parent_partition(void)
 bool hv_result_needs_memory(u64 status);
 int hv_deposit_memory_node(int node, u64 partition_id, u64 status);
 int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages);
+void hv_restore_withdrawn_pages(const u64 *pfns, int count);
 int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id);
 int hv_call_notify_all_processors_started(void);
 bool hv_lp_exists(u32 lp_index);
@@ -364,6 +365,9 @@ static inline int hv_call_deposit_pages(int node, u64 partition_id, u32 num_page
 {
 	return -EOPNOTSUPP;
 }
+
+static inline void hv_restore_withdrawn_pages(const u64 *pfns, int count) { }
+
 static inline int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id)
 {
 	return -EOPNOTSUPP;
--

2.34.1

#11 lore raw
Quoted 11 lines · Michael Kelley

From: Magnus Kulke <[email protected]> Sent: Thursday, September 17, 2026 1:11 PM

Quoted 10 lines

hv_call_deposit_pages() donates pages (deposit) to hypervisor for L2 guest on L1VH systems via HVCALL_DEPOSIT_MEMORY. The hypervisor takes ownership of those pages and per contract revokes root partition access to them, raising a #GP on access from the L1VH root partition.

However, the pages remain mapped in the kernel direct map, so kernel code may still access them even though the hypervisor has revoked access.

Quoted 5 lines
Quoted 3 lines

Helpers such as "load_unaligned_zeropad()" deliberately read past the end of a buffer and across page boundaries. A read into an unmapped page is tolerated and triggers a #PF, for which the kernel executed

s/executed/executes/

ack

Quoted 10 lines
Quoted 9 lines

a fixup in the exception table.

If such a call steps into a page that has been deposited, the access raises a #GP by the hypervisor from which the above handler cannot recover and the kernel panics:

  Oops: general protection fault, maybe for address 0xff1100941a3dfffc
  RIP: 0010:csum_partial+0xe5/0x110
Quoted 9 lines

Is there any possibility of updating the load_unaligned_zeropad() fixup handler to handle the #GP like #PF? I had looked at a variant of this problem a few years back for CoCo VMs. See the code comment above hv_vtom_clear_present(). In that case, a smarter load_unaligned_zeropad() fixup handler wasn't an option because these were #VC or #VE exceptions routed to the paravisor instead of the main Linux guest. But in your case, the Linux gets the #GP, so I wondered if a smarter fixup handler would be possible. Of course, both the x86 and arm64 versions would need updates.

I wouldn't rule it out, but I found it daunting. We don't have a proper faulting address ("maybe for address"). AFAIU, for a #PF the CR2 is populated with the actual faulting address, so the fixup handler knows that it is within the trailing bytes. For a #GP this page is hardcored to 0, so we would have to have another sourcefor the actual fault address.

Quoted 66 lines
Quoted 66 lines

This condition will appear on L1VH system that have created L2 partitions (and hence deposited pages) and exercise networking code paths such as csum_partial() can trigger this condition when a buffer ends close to a page boundary (e.g fffc in the above example).

The fix is to remove the deposited pages from the direct map before they are passed to the hypervisor, and restore them when the hypervisor returns them again.

We want to avoid flushing the TLB in the loop, so we use the _noflush() variant of set_direct_map_valid() when marking a deposited page invalid and flush the affected page ranges in one go ourselves. In the opposite direction this is not required:

Quoted 4 lines

If a paging-structure entry is modified to change the P flag from 0 to 1, no invalidation is necessary. This is because no TLB entry or paging-structure cache entry is created with information from a paging-structure entry in which the P flag is 0.

(Intel SDM Vol. 3, 4.10.4.3)

---
Changes since v2:
- Checkpatch format fix

Changes since RFC:
- Handle direct-map restoration failures without returning unmapped pages to the allocator.
- Move freeing of withdrawn pages into the restoration helper.

---
 drivers/hv/hv_proc.c           | 77 +++++++++++++++++++++++++++++++++-
 drivers/hv/mshv_root_hv_call.c |  4 +-
 include/asm-generic/mshyperv.h |  4 ++
 3 files changed, 81 insertions(+), 4 deletions(-)
drivers/hv/hv_proc.c +13 -0
index 57b2c64197cb..2a392b45205d 100644
@@ -7,7 +7,9 @@
 #include <linux/cpuhotplug.h>
 #include <linux/minmax.h>
 #include <linux/export.h>
+#include <linux/set_memory.h>
 #include <asm/mshyperv.h>
+#include <asm/tlbflush.h>
 
 /*
  * See struct hv_deposit_memory. The first u64 is partition ID, the rest
@@ -15,6 +17,35 @@
  */
 #define HV_DEPOSIT_MAX (HV_HYP_PAGE_SIZE / sizeof(u64) - 1)
 
+/*
+ * Add or remove a set of physically contiguous page runs from the kernel
+ * direct map. Once a page has been deposited the hypervisor owns it and
+ * revokes root partition access to it.
+ */
+static int hv_deposit_update_direct_map(struct page **pages, int *counts,
+					int num_allocations, bool valid)
+{
+	int i, err, ret = 0;
+
+	for (i = 0; i < num_allocations; ++i) {
Quoted 9 lines

drivers/hv/hv_proc.c line 31

Quoted 1 line
drivers/hv/hv_proc.c +1 -0
+		err = set_direct_map_valid_noflush(pages[i], counts[i], valid);

Just a heads up, there's a patch set pending that eliminates the API set_direct_map_valid_noflush(). [1] As described in that cover letter, the the behavior is inconsistent across architectures. The intent is that set_direct_map_invalid_noflush() and set_direct_map_default_noflush() should be used instead. But the latter two currently operate one page at-a-time, so they are being updated to take a page count argument.

I will keep an eye on that.

Quoted 2 lines

Michael

thanks,

magnus

Quoted 141 lines

[1] https://lore.kernel.org/lkml/[email protected]/

Quoted 139 lines
drivers/hv/hv_proc.c +62 -1
+		if (err && !ret)
+			ret = err;
+	}
+
+	if (valid)
+		return ret;
+
+	for (i = 0; i < num_allocations; ++i) {
+		unsigned long addr = (unsigned long)page_address(pages[i]);
+		unsigned long size = (unsigned long)counts[i] << PAGE_SHIFT;
+
+		flush_tlb_kernel_range(addr, addr + size);
+	}
+
+	return ret;
+}
+
 /* Deposits exact number of pages. Must be called with interrupts enabled.  */
 int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages)
 {
@@ -72,6 +103,10 @@ int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages)
 	}
 	num_allocations = i;
 
+	ret = hv_deposit_update_direct_map(pages, counts, num_allocations, false);
+	if (ret)
+		goto err_restore_direct_map;
+
 	local_irq_save(flags);
 
 	input_page = *this_cpu_ptr(hyperv_pcpu_input_arg);
@@ -90,12 +125,23 @@ int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages)
 	if (!hv_result_success(status)) {
 		hv_status_err(status, "\n");
 		ret = hv_result_to_errno(status);
-		goto err_free_allocations;
+		goto err_restore_direct_map;
 	}
 
 	ret = 0;
 	goto free_buf;
 
+err_restore_direct_map:
+	/*
+	 * We don't want to return pages to the allocator if weren't able to
+	 * mark them valid in the direct map.
+	 */
+	if (hv_deposit_update_direct_map(pages, counts, num_allocations, true)) {
+		WARN(1, "leaking %d page block(s) that could not be set to valid\n",
+		     num_allocations);
+		goto free_buf;
+	}
+
 err_free_allocations:
 	for (i = 0; i < num_allocations; ++i) {
 		base_pfn = page_to_pfn(pages[i]);
@@ -110,6 +156,35 @@ int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages)
 }
 EXPORT_SYMBOL_GPL(hv_call_deposit_pages);
 
+/*
+ * Put withdrawn pages back in the direct map. Counterpart to the direct map
+ * removal done by hv_call_deposit_pages().
+ */
+void hv_restore_withdrawn_pages(const u64 *pfns, int count)
+{
+	int i, ret = 0;
+	struct page *page;
+
+	for (i = 0; i < count; ++i) {
+		page = pfn_to_page(pfns[i]);
+		ret = set_direct_map_valid_noflush(page, 1, true);
+		/*
+		 * HV_DEPOSIT_MAX is capped at 511, so a deposit range cannot cover
+		 * a 2MiB page, so deposited pages are of 4k granularity and cannot
+		 * be collapses into a 2MiB page, which would require an allocation
+		 * and can potentially fail.
+		 *
+		 * Should it fail anyway we leak the page, if we would hand it
+		 * back to the allocator we would introduce faults into random other
+		 * parts.
+		 */
+		if (WARN_ON_ONCE(ret))
+			continue;
+		__free_page(page);
+	}
+}
+EXPORT_SYMBOL_GPL(hv_restore_withdrawn_pages);
+
 int hv_deposit_memory_node(int node, u64 partition_id,
 			   u64 hv_status)
 {
drivers/hv/mshv_root_hv_call.c +1 -3
index cb55d4d4be2e..150a0c63ebc8 100644
@@ -46,7 +46,6 @@ int hv_call_withdraw_memory(u64 count, int node, u64 partition_id)
 	struct page *page;
 	u16 completed;
 	u64 status, withdrawn = 0;
-	int i;
 	unsigned long flags;
 
 	page = alloc_page(GFP_KERNEL);
@@ -69,8 +68,7 @@ int hv_call_withdraw_memory(u64 count, int node, u64 partition_id)
 
 		completed = hv_repcomp(status);
 
-		for (i = 0; i < completed; i++)
-			__free_page(pfn_to_page(output_page->gpa_page_list[i]));
+		hv_restore_withdrawn_pages(output_page->gpa_page_list, completed);
 
 		if (!hv_result_success(status)) {
 			if (hv_result(status) == HV_STATUS_NO_RESOURCES)
include/asm-generic/mshyperv.h +4 -1
index bf601d67cecb..397c8ec0ce9a 100644
@@ -346,6 +346,7 @@ static inline bool hv_parent_partition(void)
 bool hv_result_needs_memory(u64 status);
 int hv_deposit_memory_node(int node, u64 partition_id, u64 status);
 int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages);
+void hv_restore_withdrawn_pages(const u64 *pfns, int count);
 int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id);
 int hv_call_notify_all_processors_started(void);
 bool hv_lp_exists(u32 lp_index);
@@ -364,6 +365,9 @@ static inline int hv_call_deposit_pages(int node, u64 partition_id, u32 num_page
 {
 	return -EOPNOTSUPP;
 }
+
+static inline void hv_restore_withdrawn_pages(const u64 *pfns, int count) { }
+
 static inline int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id)
 {
 	return -EOPNOTSUPP;
--

2.34.1

#12 lore raw

From: Magnus Kulke <[email protected]> Sent: Wednesday, September 30, 2026 4:33 AM

Quoted 41 lines · Michael Kelley
Quoted 11 lines

From: Magnus Kulke <[email protected]> Sent: Thursday, September 17, 2026 1:11 PM

Quoted 10 lines

hv_call_deposit_pages() donates pages (deposit) to hypervisor for L2 guest on L1VH systems via HVCALL_DEPOSIT_MEMORY. The hypervisor takes ownership of those pages and per contract revokes root partition access to them, raising a #GP on access from the L1VH root partition.

However, the pages remain mapped in the kernel direct map, so kernel code may still access them even though the hypervisor has revoked access.

Quoted 5 lines
Quoted 3 lines

Helpers such as "load_unaligned_zeropad()" deliberately read past the end of a buffer and across page boundaries. A read into an unmapped page is tolerated and triggers a #PF, for which the kernel executed

s/executed/executes/

ack

Quoted 10 lines
Quoted 9 lines

a fixup in the exception table.

If such a call steps into a page that has been deposited, the access raises a #GP by the hypervisor from which the above handler cannot recover and the kernel panics:

  Oops: general protection fault, maybe for address 0xff1100941a3dfffc
  RIP: 0010:csum_partial+0xe5/0x110
Quoted 9 lines

Is there any possibility of updating the load_unaligned_zeropad() fixup handler to handle the #GP like #PF? I had looked at a variant of this problem a few years back for CoCo VMs. See the code comment above hv_vtom_clear_present(). In that case, a smarter load_unaligned_zeropad() fixup handler wasn't an option because these were #VC or #VE exceptions routed to the paravisor instead of the main Linux guest. But in your case, the Linux gets the #GP, so I wondered if a smarter fixup handler would be possible. Of course, both the x86 and arm64 versions would need updates.

Quoted 7 lines

I wouldn't rule it out, but I found it daunting. We don't have a proper faulting address ("maybe for address"). AFAIU, for a #PF the CR2 is populated with the actual faulting address, so the fixup handler knows that it is within the trailing bytes. For a #GP this page is hardcored to 0, so we would have to have another sourcefor the actual fault address.

I went back and refreshed my memory on the discussion from July 2023. At least on x86, the faulting address is used only as a sanity check in ex_handler_zeropad(). The #GP exception handler already calls fixup_exception(), but it calls with fault_addr set to zero, so ex_handler_zeropad() always returns false (i.e., it says it can't do the fixup). From looking at the code, it appears the deposited pages case would "just work" if the sanity test against fault_addr was removed in ex_handler_zeropad(). There was a past discussion about removing the sanity test [1], but at the time there wasn't a strong reason for or against, so it was left in (having been added relatively recently in c4e34dd99f2e). An option for the deposited pages problem would be to make the case now for removing the sanity test.

I haven't looked at the situation on arm64.

It's your call on how to proceed -- I just wanted to make sure the full context is available to you.

Michael

[1] https://lore.kernel.org/lkml/CAHk-=wipev18s9sErt+iNO_rzgyvGTce38fr1CYO0U_hGVGy2Q@mail.gmail.com/

#13 lore raw
Quoted 15 lines · Michael Kelley

From: Magnus Kulke <[email protected]> Sent: Wednesday, September 30, 2026 4:33 AM

I went back and refreshed my memory on the discussion from July 2023. At least on x86, the faulting address is used only as a sanity check in ex_handler_zeropad(). The #GP exception handler already calls fixup_exception(), but it calls with fault_addr set to zero, so ex_handler_zeropad() always returns false (i.e., it says it can't do the fixup). From looking at the code, it appears the deposited pages case would "just work" if the sanity test against fault_addr was removed in ex_handler_zeropad(). There was a past discussion about removing the sanity test [1], but at the time there wasn't a strong reason for or against, so it was left in (having been added relatively recently in c4e34dd99f2e). An option for the deposited pages problem would be to make the case now for removing the sanity test.

Quoted 7 lines

I haven't looked at the situation on arm64.

It's your call on how to proceed -- I just wanted to make sure the full context is available to you.

Michael

Thanks for the context, that's indeed helpful. I'll discuss this with the mshv team. I'm still leaning towards disabling deposited pages from the direct map because this seems semantically the right thing, but Linus' statement gives some confidence that we can make the fixup work for #GPs.

best,

magnus

Quoted 1 line

[1] https://lore.kernel.org/lkml/CAHk-=wipev18s9sErt+iNO_rzgyvGTce38fr1CYO0U_hGVGy2Q@mail.gmail.com/