netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 0/3] hv: vmbus: make rings and host-visible buffers survive buddy fragmentation
@ 2026-11-24 21:40 Emerson Busson
  2026-11-24 21:40 ` [PATCH v3 1/3] hv: vmbus: encapsulate buffer and GPADL state in struct vmbus_buffer Emerson Busson
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Emerson Busson @ 2026-11-24 21:40 UTC (permalink / raw)
  To: Wei Liu, Haiyang Zhang, Dexuan Cui, Greg Kroah-Hartman
  Cc: Michael Kelley, Easwar Hariharan, Kameron Carr, Andrew Lunn,
	David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	linux-hyperv, netdev, linux-kernel

Opening new VMBus channels or subchannels can fail with -ENOMEM under
memory fragmentation even when plenty of order-0 pages are free, because
vmbus_alloc_ring() asks for a single high-order contiguous block (order-7
for a 512 KiB ring) and vmbus_alloc_requestor() uses contiguous
allocations for its request array and bitmap.

Based on Michael Kelley's review of v1 and the v2 feedback from Wei Liu
and Michael Kelley, v3 goes back to the core VMBus allocation path. It
groups buffer and GPADL state into struct vmbus_buffer, allocates ring
buffers through vmbus_alloc_buffer(), drops struct vmbus_gpadl along
with the duplicate decrypt/re-encrypt paths, and switches requestor
metadata to kvcalloc().

Changes since v2:
- Dropped the reclaim workqueue, UIO page-pinning layer, and KUnit tests
  to keep this series focused on VMBus buffer and ring allocation.
- Split the hv_netvsc fixes and allocations out for separate net /
  net-next submissions.
- Preserved hv_netvsc's revoke-failure leak guard before GPADL teardown.
- Rebased onto hyperv-next.

v1: https://lore.kernel.org/linux-hyperv/20260918014017.2536753-1-emersonbusson@gmail.com/
v2: https://lore.kernel.org/linux-hyperv/20261007190752.336426-1-emersonbusson@gmail.com/

Tested with x86_64 (W=1 C=2) and i386 builds, a local QEMU/KVM boot with
lockdep enabled, and a backport onto 6.18.64.1 running live on Hyper-V
x86_64 (85 VMBus channels, 12 netvsc queues, uio_hv_generic loaded).
Confidential VM paths (SEV-SNP, TDX, Arm64 CCA) were compile-tested and
reviewed by inspection only.

Emerson Busson (3):
  hv: vmbus: encapsulate buffer and GPADL state in struct vmbus_buffer
  hv: vmbus: allocate ring buffers via vmbus_alloc_buffer()
  hv: vmbus: allocate requestor metadata with kvcalloc()

 drivers/hv/channel.c            | 363 +++++++++++++++-----------------
 drivers/hv/hyperv_vmbus.h       |   4 +-
 drivers/hv/ring_buffer.c        |  17 +-
 drivers/net/hyperv/hyperv_net.h |  10 +-
 drivers/net/hyperv/netvsc.c     |  64 +++---
 drivers/uio/uio_hv_generic.c    |  97 ++++-----
 include/linux/hyperv.h          |  43 ++--
 7 files changed, 271 insertions(+), 327 deletions(-)


base-commit: eab80895d447ec164a1d91a1ab377bd3a9dc9044
-- 
2.43.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v3 1/3] hv: vmbus: encapsulate buffer and GPADL state in struct vmbus_buffer
  2026-11-24 21:40 [PATCH v3 0/3] hv: vmbus: make rings and host-visible buffers survive buddy fragmentation Emerson Busson
@ 2026-11-24 21:40 ` Emerson Busson
  2026-11-24 21:40 ` [PATCH v3 2/3] hv: vmbus: allocate ring buffers via vmbus_alloc_buffer() Emerson Busson
  2026-11-24 21:40 ` [PATCH v3 3/3] hv: vmbus: allocate requestor metadata with kvcalloc() Emerson Busson
  2 siblings, 0 replies; 4+ messages in thread
From: Emerson Busson @ 2026-11-24 21:40 UTC (permalink / raw)
  To: Wei Liu, Haiyang Zhang, Dexuan Cui, Greg Kroah-Hartman
  Cc: Michael Kelley, Easwar Hariharan, Kameron Carr, Andrew Lunn,
	David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	linux-hyperv, netdev, linux-kernel

vmbus_alloc_buffer() currently returns the virtual address, chunk array,
and chunk count through separate parameters that callers track alongside
struct vmbus_gpadl. As ring buffers are about to adopt vmbus_alloc_buffer()
as well, repeating those fields in every caller adds boilerplate and splits
allocation lifetime from GPADL teardown and leak handling.

Introduce struct vmbus_buffer to hold the mapping, chunk metadata, GPADL
handle, and a leak flag in one structure. Update vmbus_alloc_buffer(),
vmbus_free_buffer(), vmbus_establish_gpadl(), and vmbus_teardown_gpadl()
to operate on struct vmbus_buffer, and convert both external-buffer users
(hv_netvsc and uio_hv_generic).

Because every external buffer is now allocated and decrypted by
vmbus_alloc_buffer(), drop HV_GPADL_BUFFER_DECRYPTED and
vmbus_establish_gpadl_caller_decrypted(). Keep the leak flag set if GPADL
establishment fails after posting, if GPADL teardown fails, or if hv_netvsc
fails to send its protocol-level buffer revoke message, so neither
vmbus_teardown_gpadl() nor vmbus_free_buffer() releases host-owned memory.

Signed-off-by: Emerson Busson <emersonbusson@gmail.com>
---
 drivers/hv/channel.c            | 317 ++++++++++++++++++--------------
 drivers/net/hyperv/hyperv_net.h |  10 +-
 drivers/net/hyperv/netvsc.c     |  64 ++++---
 drivers/uio/uio_hv_generic.c    |  65 +++----
 include/linux/hyperv.h          |  36 ++--
 5 files changed, 253 insertions(+), 239 deletions(-)

diff --git a/drivers/hv/channel.c b/drivers/hv/channel.c
index a0883ddf32..9807ff35a1 100644
--- a/drivers/hv/channel.c
+++ b/drivers/hv/channel.c
@@ -42,7 +42,6 @@ static inline u32 hv_gpadl_size(enum hv_gpadl_type type, u32 size)
 {
 	switch (type) {
 	case HV_GPADL_BUFFER:
-	case HV_GPADL_BUFFER_DECRYPTED:
 		return size;
 	case HV_GPADL_RING:
 		/* The size of a ringbuffer must be page-aligned */
@@ -103,7 +102,6 @@ static inline u64 hv_gpadl_hvpfn(enum hv_gpadl_type type, void *kbuffer,
 
 	switch (type) {
 	case HV_GPADL_BUFFER:
-	case HV_GPADL_BUFFER_DECRYPTED:
 		break;
 	case HV_GPADL_RING:
 		if (i == 0)
@@ -443,7 +441,8 @@ static void vmbus_free_channel_msginfo(struct vmbus_channel_msginfo *msginfo)
 static int __vmbus_establish_gpadl(struct vmbus_channel *channel,
 				   enum hv_gpadl_type type, void *kbuffer,
 				   u32 size, u32 send_offset,
-				   struct vmbus_gpadl *gpadl)
+				   bool caller_decrypted, u32 *gpadl_handle,
+				   bool *decrypted, bool *leak)
 {
 	struct vmbus_channel_gpadl_header *gpadlmsg;
 	struct vmbus_channel_gpadl_body *gpadl_body;
@@ -452,33 +451,39 @@ static int __vmbus_establish_gpadl(struct vmbus_channel *channel,
 	struct list_head *curr;
 	u32 next_gpadl_handle;
 	unsigned long flags;
+	bool posted = false;
+	bool need_decrypt;
 	int ret = 0;
 
+	if (leak)
+		*leak = false;
+
 	next_gpadl_handle =
 		(atomic_inc_return(&vmbus_connection.next_gpadl_handle) - 1);
 
 	ret = create_gpadl_header(type, kbuffer, size, send_offset, &msginfo);
 	if (ret) {
-		gpadl->decrypted = false;
+		if (decrypted)
+			*decrypted = false;
 		return ret;
 	}
 
-	gpadl->decrypted = !((channel->co_external_memory && type == HV_GPADL_BUFFER) ||
-		(channel->co_ring_buffer && type == HV_GPADL_RING) ||
-		(type == HV_GPADL_BUFFER_DECRYPTED));
-	if (gpadl->decrypted) {
+	need_decrypt = !caller_decrypted &&
+		!((channel->co_external_memory && type == HV_GPADL_BUFFER) ||
+		  (channel->co_ring_buffer && type == HV_GPADL_RING));
+	if (decrypted)
+		*decrypted = need_decrypt;
+	if (need_decrypt) {
 		/*
-		 * The "decrypted" flag being true assumes that set_memory_decrypted() succeeds.
-		 * But if it fails, the encryption state of the memory is unknown. In that case,
-		 * leave "decrypted" as true to ensure the memory is leaked instead of going back
-		 * on the free list.
+		 * If set_memory_decrypted() fails, the encryption state is
+		 * unknown; leave *decrypted true so the memory is leaked.
 		 */
 		ret = set_memory_decrypted((unsigned long)kbuffer,
-					PFN_UP(size));
+					   PFN_UP(size));
 		if (ret) {
 			dev_warn(&channel->device_obj->device,
-				"Failed to set host visibility for new GPADL %d.\n",
-				ret);
+				 "Failed to set host visibility for new GPADL %d.\n",
+				 ret);
 			vmbus_free_channel_msginfo(msginfo);
 			return ret;
 		}
@@ -492,11 +497,9 @@ static int __vmbus_establish_gpadl(struct vmbus_channel *channel,
 	gpadlmsg->child_relid = channel->offermsg.child_relid;
 	gpadlmsg->gpadl = next_gpadl_handle;
 
-
 	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
 	list_add_tail(&msginfo->msglistentry,
 		      &vmbus_connection.chn_msg_list);
-
 	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
 
 	if (channel->rescind) {
@@ -504,6 +507,7 @@ static int __vmbus_establish_gpadl(struct vmbus_channel *channel,
 		goto cleanup;
 	}
 
+	posted = true;
 	ret = vmbus_post_msg(gpadlmsg, msginfo->msgsize -
 			     sizeof(*msginfo), true);
 
@@ -529,11 +533,11 @@ static int __vmbus_establish_gpadl(struct vmbus_channel *channel,
 
 		if (ret != 0)
 			goto cleanup;
-
 	}
 	wait_for_completion(&msginfo->waitevent);
 
 	if (msginfo->response.gpadl_created.creation_status != 0) {
+		posted = false;
 		pr_err("Failed to establish GPADL: err = 0x%x\n",
 		       msginfo->response.gpadl_created.creation_status);
 
@@ -542,15 +546,14 @@ static int __vmbus_establish_gpadl(struct vmbus_channel *channel,
 	}
 
 	if (channel->rescind) {
+		posted = false;
 		ret = -ENODEV;
 		goto cleanup;
 	}
 
 	/* At this point, we received the gpadl created msg */
-	gpadl->gpadl_handle = gpadlmsg->gpadl;
-	gpadl->buffer = kbuffer;
-	gpadl->size = size;
-
+	*gpadl_handle = gpadlmsg->gpadl;
+	posted = false;
 
 cleanup:
 	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
@@ -559,121 +562,100 @@ static int __vmbus_establish_gpadl(struct vmbus_channel *channel,
 
 	vmbus_free_channel_msginfo(msginfo);
 
-	if (ret) {
-		/*
-		 * If set_memory_encrypted() fails, the decrypted flag is
-		 * left as true so the memory is leaked instead of being
-		 * put back on the free list.
-		 */
-		if (gpadl->decrypted) {
-			if (!set_memory_encrypted((unsigned long)kbuffer, PFN_UP(size)))
-				gpadl->decrypted = false;
-		}
+	if (ret && posted && leak)
+		*leak = true;
+
+	if (ret && !posted && need_decrypt) {
+		if (!set_memory_encrypted((unsigned long)kbuffer, PFN_UP(size)) &&
+		    decrypted)
+			*decrypted = false;
 	}
 
 	return ret;
 }
 
-/*
- * vmbus_establish_gpadl - Establish a GPADL for the specified buffer
+/**
+ * vmbus_establish_gpadl - Establish a GPADL for a buffer allocated with
+ *                         vmbus_alloc_buffer().
+ * @channel: VMBus channel to associate the GPADL with
+ * @buffer: buffer descriptor populated by vmbus_alloc_buffer()
  *
- * @channel: a channel
- * @kbuffer: from kmalloc or vmalloc
- * @size: page-size multiple
- * @gpadl: output gpadl
+ * Return: 0 on success, negative errno on failure. If a GPADL message may
+ * have reached the host, buffer->leak is set so vmbus_free_buffer() retains
+ * the backing pages.
  */
-int vmbus_establish_gpadl(struct vmbus_channel *channel, void *kbuffer,
-			  u32 size, struct vmbus_gpadl *gpadl)
+int vmbus_establish_gpadl(struct vmbus_channel *channel,
+			  struct vmbus_buffer *buffer)
 {
-	return __vmbus_establish_gpadl(channel, HV_GPADL_BUFFER, kbuffer, size,
-				       0U, gpadl);
+	return __vmbus_establish_gpadl(channel, HV_GPADL_BUFFER, buffer->addr,
+				       buffer->size, 0U, true,
+				       &buffer->gpadl_handle, NULL,
+				       &buffer->leak);
 }
 EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
 
-/*
- * vmbus_establish_gpadl_caller_decrypted - Establish a GPADL for a buffer
- * that has already been decrypted by the caller.
- *
- * @channel: a channel
- * @kbuffer: from kmalloc or vmalloc; must already be decrypted by the caller
- * @size: page-size multiple
- * @gpadl: output gpadl
- *
- * The caller is responsible for re-encrypting the buffer before freeing it.
- */
-int vmbus_establish_gpadl_caller_decrypted(struct vmbus_channel *channel,
-					   void *kbuffer, u32 size,
-					   struct vmbus_gpadl *gpadl)
-{
-	return __vmbus_establish_gpadl(channel, HV_GPADL_BUFFER_DECRYPTED,
-				       kbuffer, size, 0U, gpadl);
-}
-EXPORT_SYMBOL_GPL(vmbus_establish_gpadl_caller_decrypted);
-
 /**
  * vmbus_free_buffer - release a buffer allocated by vmbus_alloc_buffer().
+ * @buffer: buffer descriptor to release
  *
- * @addr: buffer address, or NULL if none was allocated (e.g. cleanup from a
- *        failed allocation)
- * @chunks: chunks array from vmbus_alloc_buffer(), or NULL
- * @chunk_cnt: number of entries in @chunks
- *
- * When @chunks is NULL the buffer is a plain vzalloc() allocation.
- *
- * Otherwise tear down the vmap, and for each chunk re-encrypt and free
- * the underlying pages. Any chunk that cannot be re-encrypted is leaked.
+ * If the buffer still has an active GPADL or buffer->leak is set, the backing
+ * pages are leaked rather than returned to the allocator. Any chunk that fails
+ * set_memory_encrypted() is likewise leaked while the remaining chunks and
+ * tracking arrays are freed.
  */
-void vmbus_free_buffer(void *addr, struct page **chunks, u32 chunk_cnt)
+void vmbus_free_buffer(struct vmbus_buffer *buffer)
 {
 	u32 i;
 
-	if (!chunks) {
-		vfree(addr);
+	kvfree(buffer->pages);
+	if (buffer->leak || buffer->gpadl_handle) {
+		kvfree(buffer->chunks);
+		memset(buffer, 0, sizeof(*buffer));
+		return;
+	}
+
+	if (!buffer->chunks) {
+		vfree(buffer->addr);
+		memset(buffer, 0, sizeof(*buffer));
 		return;
 	}
 
-	vunmap(addr);
+	vunmap(buffer->addr);
 
-	for (i = 0; i < chunk_cnt; i++) {
+	for (i = 0; i < buffer->chunk_cnt; i++) {
 		unsigned long vaddr =
-			(unsigned long)page_address(chunks[i]);
-		unsigned int order = folio_order(page_folio(chunks[i]));
+			(unsigned long)page_address(buffer->chunks[i]);
+		unsigned int order = folio_order(page_folio(buffer->chunks[i]));
 
 		if (set_memory_encrypted(vaddr, 1U << order))
 			continue;
-		__free_pages(chunks[i], order);
+		__free_pages(buffer->chunks[i], order);
 	}
 
-	kvfree(chunks);
+	kvfree(buffer->chunks);
+	memset(buffer, 0, sizeof(*buffer));
 }
 EXPORT_SYMBOL_GPL(vmbus_free_buffer);
 
 /**
- * vmbus_alloc_buffer - allocate a host-visible, virtually-contiguous buffer.
- *
- * @channel: the channel the buffer will be attached to
- * @size: requested buffer size in bytes (will be rounded up to PAGE_SIZE)
- * @chunks_out: on success, set to the array of underlying chunks, or NULL when
- *              the buffer was allocated with vzalloc()
- * @chunk_cnt_out: on success, set to the number of chunks
- *
- * Buffers not requiring decryption are allocated with vzalloc().
+ * vmbus_alloc_buffer - allocate a virtually contiguous VMBus buffer.
+ * @channel: channel the buffer will be attached to
+ * @size: requested size in bytes (rounded up to PAGE_SIZE)
+ * @confidential: keep the buffer private to the guest
+ * @buffer: output descriptor populated on success
  *
- * Buffers requiring decryption are allocated as a series of
- * physically-contiguous chunks, starting at MAX_PAGE_ORDER and falling back to
- * smaller orders on allocation failure. Each chunk is transitioned to
- * host-visible via set_memory_decrypted() on its direct-map address, then all
- * chunks are combined into a virtually-contiguous range via vmap().
+ * Buffers not requiring decryption are allocated with vzalloc_node().
+ * Host-visible buffers in isolated guests are allocated as physically
+ * contiguous chunks starting at MAX_PAGE_ORDER and falling back down to
+ * order 0, decrypted on their direct-map address, and combined with vmap().
  *
- * Return: the buffer's virtual address, or NULL on failure.
+ * Return: 0 on success, negative errno on failure.
  */
-void *vmbus_alloc_buffer(struct vmbus_channel *channel,
-			 u32 size,
-			 struct page ***chunks_out,
-			 u32 *chunk_cnt_out)
+int vmbus_alloc_buffer(struct vmbus_channel *channel, u32 size,
+		       bool confidential, struct vmbus_buffer *buffer)
 {
-	unsigned long nr_pages = PFN_UP(size);
-	unsigned long remaining = nr_pages;
+	unsigned long nr_pages;
+	unsigned long remaining;
 	unsigned long page_idx = 0;
 	struct page **chunks = NULL;
 	struct page **pages = NULL;
@@ -683,24 +665,36 @@ void *vmbus_alloc_buffer(struct vmbus_channel *channel,
 	u32 i;
 	int ret;
 
-	*chunks_out = NULL;
-	*chunk_cnt_out = 0;
+	memset(buffer, 0, sizeof(*buffer));
 
-	if (!nr_pages)
-		return NULL;
+	if (!size || size > U32_MAX - PAGE_SIZE + 1)
+		return -EINVAL;
+
+	nr_pages = PFN_UP(size);
+	remaining = nr_pages;
+
+	/* If the buffer does not need to be decrypted, just use vzalloc_node() */
+	if (!hv_is_isolation_supported() || confidential) {
+		addr = vzalloc_node(nr_pages << PAGE_SHIFT,
+				    cpu_to_node(channel->target_cpu));
+		if (!addr)
+			return -ENOMEM;
 
-	/* If the buffer does not need to be decrypted, just use vzalloc() */
-	if (!hv_is_isolation_supported() || channel->co_external_memory)
-		return vzalloc(nr_pages << PAGE_SHIFT);
+		buffer->addr = addr;
+		buffer->size = nr_pages << PAGE_SHIFT;
+		return 0;
+	}
 
 	/* Worst case: every chunk is a single page. */
 	chunks = kvmalloc_objs(*chunks, nr_pages, GFP_KERNEL | __GFP_ZERO);
 	if (!chunks)
-		goto err;
+		return -ENOMEM;
 
 	pages = kvmalloc_objs(*pages, nr_pages);
-	if (!pages)
-		goto err;
+	if (!pages) {
+		kvfree(chunks);
+		return -ENOMEM;
+	}
 
 	while (remaining) {
 		struct page *page;
@@ -731,7 +725,7 @@ void *vmbus_alloc_buffer(struct vmbus_channel *channel,
 		if (ret) {
 			/*
 			 * set_memory_decrypted() failed; the page state is
-			 * unknown so it must be leaked rather than freed.
+			 * unknown so leak this chunk and free earlier ones.
 			 */
 			goto err;
 		}
@@ -751,14 +745,18 @@ void *vmbus_alloc_buffer(struct vmbus_channel *channel,
 	memset(addr, 0, nr_pages << PAGE_SHIFT);
 
 	kvfree(pages);
-	*chunks_out = chunks;
-	*chunk_cnt_out = chunk_cnt;
-	return addr;
+	buffer->addr = addr;
+	buffer->chunks = chunks;
+	buffer->size = nr_pages << PAGE_SHIFT;
+	buffer->chunk_cnt = chunk_cnt;
+	return 0;
 
 err:
 	kvfree(pages);
-	vmbus_free_buffer(NULL, chunks, chunk_cnt);
-	return NULL;
+	buffer->chunks = chunks;
+	buffer->chunk_cnt = chunk_cnt;
+	vmbus_free_buffer(buffer);
+	return -ENOMEM;
 }
 EXPORT_SYMBOL_GPL(vmbus_alloc_buffer);
 
@@ -826,6 +824,10 @@ static void vmbus_free_requestor(struct vmbus_requestor *rqstor)
 	bitmap_free(rqstor->req_bitmap);
 }
 
+static int __vmbus_teardown_gpadl(struct vmbus_channel *channel,
+				  u32 *gpadl_handle, void *kbuffer,
+				  u32 size, bool *decrypted, bool *leak);
+
 static int __vmbus_open(struct vmbus_channel *newchannel,
 		       void *userdata, u32 userdatalen,
 		       void (*onchannelcallback)(void *context), void *context)
@@ -866,9 +868,16 @@ static int __vmbus_open(struct vmbus_channel *newchannel,
 				      page_address(newchannel->ringbuffer_page),
 				      (send_pages + recv_pages) << PAGE_SHIFT,
 				      newchannel->ringbuffer_send_offset << PAGE_SHIFT,
-				      &newchannel->ringbuffer_gpadlhandle);
+				      false,
+				      &newchannel->ringbuffer_gpadlhandle.gpadl_handle,
+				      &newchannel->ringbuffer_gpadlhandle.decrypted,
+				      NULL);
 	if (err)
 		goto error_clean_ring;
+	newchannel->ringbuffer_gpadlhandle.buffer =
+		page_address(newchannel->ringbuffer_page);
+	newchannel->ringbuffer_gpadlhandle.size =
+		(send_pages + recv_pages) << PAGE_SHIFT;
 
 	err = hv_ringbuffer_init(&newchannel->outbound,
 				 page, send_pages, 0, newchannel->co_ring_buffer);
@@ -956,7 +965,12 @@ static int __vmbus_open(struct vmbus_channel *newchannel,
 error_free_info:
 	kfree(open_info);
 error_free_gpadl:
-	vmbus_teardown_gpadl(newchannel, &newchannel->ringbuffer_gpadlhandle);
+	__vmbus_teardown_gpadl(newchannel,
+			       &newchannel->ringbuffer_gpadlhandle.gpadl_handle,
+			       newchannel->ringbuffer_gpadlhandle.buffer,
+			       newchannel->ringbuffer_gpadlhandle.size,
+			       &newchannel->ringbuffer_gpadlhandle.decrypted,
+			       NULL);
 error_clean_ring:
 	hv_ringbuffer_cleanup(&newchannel->outbound);
 	hv_ringbuffer_cleanup(&newchannel->inbound);
@@ -999,20 +1013,22 @@ int vmbus_open(struct vmbus_channel *newchannel,
 }
 EXPORT_SYMBOL_GPL(vmbus_open);
 
-/*
- * vmbus_teardown_gpadl -Teardown the specified GPADL handle
- */
-int vmbus_teardown_gpadl(struct vmbus_channel *channel, struct vmbus_gpadl *gpadl)
+static int __vmbus_teardown_gpadl(struct vmbus_channel *channel,
+				  u32 *gpadl_handle, void *kbuffer,
+				  u32 size, bool *decrypted, bool *leak)
 {
 	struct vmbus_channel_gpadl_teardown *msg;
 	struct vmbus_channel_msginfo *info;
 	unsigned long flags;
-	int ret;
+	int ret = 0;
 
 	info = kzalloc(sizeof(*info) +
 		       sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
-	if (!info)
+	if (!info) {
+		if (leak)
+			*leak = true;
 		return -ENOMEM;
+	}
 
 	init_completion(&info->waitevent);
 	info->waiting_channel = channel;
@@ -1021,7 +1037,7 @@ int vmbus_teardown_gpadl(struct vmbus_channel *channel, struct vmbus_gpadl *gpad
 
 	msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
 	msg->child_relid = channel->offermsg.child_relid;
-	msg->gpadl = gpadl->gpadl_handle;
+	msg->gpadl = *gpadl_handle;
 
 	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
 	list_add_tail(&info->msglistentry,
@@ -1041,13 +1057,10 @@ int vmbus_teardown_gpadl(struct vmbus_channel *channel, struct vmbus_gpadl *gpad
 
 	wait_for_completion(&info->waitevent);
 
-	gpadl->gpadl_handle = 0;
-
 post_msg_err:
 	/*
-	 * If the channel has been rescinded;
-	 * we will be awakened by the rescind
-	 * handler; set the error code to zero so we don't leak memory.
+	 * If the channel has been rescinded, the rescind handler wakes us;
+	 * clear the error code so the buffer is not leaked.
 	 */
 	if (channel->rescind)
 		ret = 0;
@@ -1058,18 +1071,35 @@ int vmbus_teardown_gpadl(struct vmbus_channel *channel, struct vmbus_gpadl *gpad
 
 	kfree(info);
 
-	if (gpadl->decrypted)
-		ret = set_memory_encrypted((unsigned long)gpadl->buffer,
-					PFN_UP(gpadl->size));
-	else
-		ret = 0;
-	if (ret)
-		pr_warn("Fail to set mem host visibility in GPADL teardown %d.\n", ret);
-
-	gpadl->decrypted = ret;
+	if (!ret)
+		*gpadl_handle = 0;
+	else if (leak)
+		*leak = true;
+
+	if (!ret && decrypted && *decrypted) {
+		ret = set_memory_encrypted((unsigned long)kbuffer,
+					   PFN_UP(size));
+		if (ret)
+			pr_warn("Fail to set mem host visibility in GPADL teardown %d.\n",
+				ret);
+		*decrypted = !!ret;
+	}
 
 	return ret;
 }
+
+/*
+ * vmbus_teardown_gpadl - Teardown the specified GPADL handle
+ */
+int vmbus_teardown_gpadl(struct vmbus_channel *channel,
+			 struct vmbus_buffer *buffer)
+{
+	if (!buffer->gpadl_handle)
+		return 0;
+
+	return __vmbus_teardown_gpadl(channel, &buffer->gpadl_handle,
+				      NULL, 0, NULL, &buffer->leak);
+}
 EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
 
 void vmbus_reset_channel_cb(struct vmbus_channel *channel)
@@ -1143,7 +1173,12 @@ static int vmbus_close_internal(struct vmbus_channel *channel)
 
 	/* Tear down the gpadl for the channel's ring buffer */
 	else if (channel->ringbuffer_gpadlhandle.gpadl_handle) {
-		ret = vmbus_teardown_gpadl(channel, &channel->ringbuffer_gpadlhandle);
+		ret = __vmbus_teardown_gpadl(channel,
+					     &channel->ringbuffer_gpadlhandle.gpadl_handle,
+					     channel->ringbuffer_gpadlhandle.buffer,
+					     channel->ringbuffer_gpadlhandle.size,
+					     &channel->ringbuffer_gpadlhandle.decrypted,
+					     NULL);
 		if (ret) {
 			pr_err("Close failed: teardown gpadl return %d\n", ret);
 			/*
diff --git a/drivers/net/hyperv/hyperv_net.h b/drivers/net/hyperv/hyperv_net.h
index 4841367fda..a15cb24603 100644
--- a/drivers/net/hyperv/hyperv_net.h
+++ b/drivers/net/hyperv/hyperv_net.h
@@ -1158,21 +1158,15 @@ struct netvsc_device {
 	bool tx_disable; /* if true, do not wake up queue again */
 
 	/* Receive buffer allocated by us but manages by NetVSP */
-	void *recv_buf;
+	struct vmbus_buffer recv_buffer;
 	u32 recv_buf_size; /* allocated bytes */
-	struct page **recv_buf_chunks;
-	u32 recv_buf_chunk_cnt;
-	struct vmbus_gpadl recv_buf_gpadl_handle;
 	u32 recv_section_cnt;
 	u32 recv_section_size;
 	u32 recv_completion_cnt;
 
 	/* Send buffer allocated by us */
-	void *send_buf;
+	struct vmbus_buffer send_buffer;
 	u32 send_buf_size;
-	struct page **send_buf_chunks;
-	u32 send_buf_chunk_cnt;
-	struct vmbus_gpadl send_buf_gpadl_handle;
 	u32 send_section_cnt;
 	u32 send_section_size;
 	unsigned long *send_section_map;
diff --git a/drivers/net/hyperv/netvsc.c b/drivers/net/hyperv/netvsc.c
index fc6548b16c..f668829291 100644
--- a/drivers/net/hyperv/netvsc.c
+++ b/drivers/net/hyperv/netvsc.c
@@ -134,10 +134,8 @@ static void __free_netvsc_device(struct netvsc_device *nvdev)
 
 	kfree(nvdev->extension);
 
-	vmbus_free_buffer(nvdev->recv_buf, nvdev->recv_buf_chunks,
-			  nvdev->recv_buf_chunk_cnt);
-	vmbus_free_buffer(nvdev->send_buf, nvdev->send_buf_chunks,
-			  nvdev->send_buf_chunk_cnt);
+	vmbus_free_buffer(&nvdev->recv_buffer);
+	vmbus_free_buffer(&nvdev->send_buffer);
 	bitmap_free(nvdev->send_section_map);
 
 	for (i = 0; i < VRSS_CHANNEL_MAX; i++) {
@@ -245,6 +243,7 @@ static void netvsc_revoke_recv_buf(struct hv_device *device,
 		if (ret != 0) {
 			netdev_err(ndev, "unable to send "
 				"revoke receive buffer to netvsp\n");
+			net_device->recv_buffer.leak = true;
 			return;
 		}
 		net_device->recv_section_cnt = 0;
@@ -296,6 +295,7 @@ static void netvsc_revoke_send_buf(struct hv_device *device,
 		if (ret != 0) {
 			netdev_err(ndev, "unable to send "
 				   "revoke send buffer to netvsp\n");
+			net_device->send_buffer.leak = true;
 			return;
 		}
 		net_device->send_section_cnt = 0;
@@ -308,9 +308,12 @@ static void netvsc_teardown_recv_gpadl(struct hv_device *device,
 {
 	int ret;
 
-	if (net_device->recv_buf_gpadl_handle.gpadl_handle) {
+	if (net_device->recv_buffer.leak)
+		return;
+
+	if (net_device->recv_buffer.gpadl_handle) {
 		ret = vmbus_teardown_gpadl(device->channel,
-					   &net_device->recv_buf_gpadl_handle);
+					   &net_device->recv_buffer);
 
 		/* If we failed here, we might as well return and have a leak
 		 * rather than continue and a bugchk
@@ -329,9 +332,12 @@ static void netvsc_teardown_send_gpadl(struct hv_device *device,
 {
 	int ret;
 
-	if (net_device->send_buf_gpadl_handle.gpadl_handle) {
+	if (net_device->send_buffer.leak)
+		return;
+
+	if (net_device->send_buffer.gpadl_handle) {
 		ret = vmbus_teardown_gpadl(device->channel,
-					   &net_device->send_buf_gpadl_handle);
+					   &net_device->send_buffer);
 
 		/* If we failed here, we might as well return and have a leak
 		 * rather than continue and a bugchk
@@ -377,15 +383,13 @@ static int netvsc_init_buf(struct hv_device *device,
 		buf_size = min_t(unsigned int, buf_size,
 				 NETVSC_RECEIVE_BUFFER_SIZE_LEGACY);
 
-	net_device->recv_buf =
-		vmbus_alloc_buffer(device->channel, buf_size,
-				   &net_device->recv_buf_chunks,
-				   &net_device->recv_buf_chunk_cnt);
-	if (!net_device->recv_buf) {
+	ret = vmbus_alloc_buffer(device->channel, buf_size,
+				 device->channel->co_external_memory,
+				 &net_device->recv_buffer);
+	if (ret) {
 		netdev_err(ndev,
 			   "unable to allocate receive buffer of size %u\n",
 			   buf_size);
-		ret = -ENOMEM;
 		goto cleanup;
 	}
 
@@ -396,10 +400,8 @@ static int netvsc_init_buf(struct hv_device *device,
 	 * channel.  Note: This call uses the vmbus connection rather
 	 * than the channel to establish the gpadl handle.
 	 */
-	ret = vmbus_establish_gpadl_caller_decrypted(device->channel,
-						     net_device->recv_buf,
-						     buf_size,
-						     &net_device->recv_buf_gpadl_handle);
+	ret = vmbus_establish_gpadl(device->channel,
+				    &net_device->recv_buffer);
 	if (ret != 0) {
 		netdev_err(ndev,
 			"unable to establish receive buffer's gpadl\n");
@@ -410,8 +412,8 @@ static int netvsc_init_buf(struct hv_device *device,
 	init_packet = &net_device->channel_init_pkt;
 	memset(init_packet, 0, sizeof(struct nvsp_message));
 	init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_RECV_BUF;
-	init_packet->msg.v1_msg.send_recv_buf.
-		gpadl_handle = net_device->recv_buf_gpadl_handle.gpadl_handle;
+	init_packet->msg.v1_msg.send_recv_buf.gpadl_handle =
+		net_device->recv_buffer.gpadl_handle;
 	init_packet->msg.v1_msg.
 		send_recv_buf.id = NETVSC_RECEIVE_BUFFER_ID;
 
@@ -487,14 +489,12 @@ static int netvsc_init_buf(struct hv_device *device,
 	buf_size = device_info->send_sections * device_info->send_section_size;
 	buf_size = round_up(buf_size, PAGE_SIZE);
 
-	net_device->send_buf =
-		vmbus_alloc_buffer(device->channel, buf_size,
-				   &net_device->send_buf_chunks,
-				   &net_device->send_buf_chunk_cnt);
-	if (!net_device->send_buf) {
+	ret = vmbus_alloc_buffer(device->channel, buf_size,
+				 device->channel->co_external_memory,
+				 &net_device->send_buffer);
+	if (ret) {
 		netdev_err(ndev, "unable to allocate send buffer of size %u\n",
 			   buf_size);
-		ret = -ENOMEM;
 		goto cleanup;
 	}
 	net_device->send_buf_size = buf_size;
@@ -503,10 +503,8 @@ static int netvsc_init_buf(struct hv_device *device,
 	 * channel.  Note: This call uses the vmbus connection rather
 	 * than the channel to establish the gpadl handle.
 	 */
-	ret = vmbus_establish_gpadl_caller_decrypted(device->channel,
-						     net_device->send_buf,
-						     buf_size,
-						     &net_device->send_buf_gpadl_handle);
+	ret = vmbus_establish_gpadl(device->channel,
+				    &net_device->send_buffer);
 	if (ret != 0) {
 		netdev_err(ndev,
 			   "unable to establish send buffer's gpadl\n");
@@ -518,7 +516,7 @@ static int netvsc_init_buf(struct hv_device *device,
 	memset(init_packet, 0, sizeof(struct nvsp_message));
 	init_packet->hdr.msg_type = NVSP_MSG1_TYPE_SEND_SEND_BUF;
 	init_packet->msg.v1_msg.send_send_buf.gpadl_handle =
-		net_device->send_buf_gpadl_handle.gpadl_handle;
+		net_device->send_buffer.gpadl_handle;
 	init_packet->msg.v1_msg.send_send_buf.id = NETVSC_SEND_BUFFER_ID;
 
 	trace_nvsp_send(ndev, init_packet);
@@ -965,7 +963,7 @@ static void netvsc_copy_to_send_buf(struct netvsc_device *net_device,
 				    struct hv_page_buffer *pb,
 				    bool xmit_more)
 {
-	char *start = net_device->send_buf;
+	char *start = net_device->send_buffer.addr;
 	char *dest = start + (section_index * net_device->send_section_size)
 		     + pend_size;
 	int i;
@@ -1472,7 +1470,7 @@ static int netvsc_receive(struct net_device *ndev,
 	const struct nvsp_message *nvsp = hv_pkt_data(desc);
 	u32 msglen = hv_pkt_datalen(desc);
 	u16 q_idx = channel->offermsg.offer.sub_channel_index;
-	char *recv_buf = net_device->recv_buf;
+	char *recv_buf = net_device->recv_buffer.addr;
 	u32 status = NVSP_STAT_SUCCESS;
 	int i;
 	int count = 0;
diff --git a/drivers/uio/uio_hv_generic.c b/drivers/uio/uio_hv_generic.c
index 7b4cc456c4..9c6449c92a 100644
--- a/drivers/uio/uio_hv_generic.c
+++ b/drivers/uio/uio_hv_generic.c
@@ -27,7 +27,6 @@
 #include <linux/if_ether.h>
 #include <linux/skbuff.h>
 #include <linux/hyperv.h>
-#include <linux/vmalloc.h>
 #include <linux/slab.h>
 
 #include "../hv/hyperv_vmbus.h"
@@ -56,12 +55,10 @@ struct hv_uio_private_data {
 	struct hv_device *device;
 	atomic_t refcnt;
 
-	void	*recv_buf;
-	struct vmbus_gpadl recv_gpadl;
+	struct vmbus_buffer recv_buffer;
 	char	recv_name[32];	/* "recv_4294967295" */
 
-	void	*send_buf;
-	struct vmbus_gpadl send_gpadl;
+	struct vmbus_buffer send_buffer;
 	char	send_name[32];
 };
 
@@ -195,17 +192,13 @@ hv_uio_new_channel(struct vmbus_channel *new_sc)
 static void
 hv_uio_cleanup(struct hv_device *dev, struct hv_uio_private_data *pdata)
 {
-	if (pdata->send_gpadl.gpadl_handle) {
-		vmbus_teardown_gpadl(dev->channel, &pdata->send_gpadl);
-		if (!pdata->send_gpadl.decrypted)
-			vfree(pdata->send_buf);
-	}
+	if (pdata->send_buffer.gpadl_handle)
+		vmbus_teardown_gpadl(dev->channel, &pdata->send_buffer);
+	vmbus_free_buffer(&pdata->send_buffer);
 
-	if (pdata->recv_gpadl.gpadl_handle) {
-		vmbus_teardown_gpadl(dev->channel, &pdata->recv_gpadl);
-		if (!pdata->recv_gpadl.decrypted)
-			vfree(pdata->recv_buf);
-	}
+	if (pdata->recv_buffer.gpadl_handle)
+		vmbus_teardown_gpadl(dev->channel, &pdata->recv_buffer);
+	vmbus_free_buffer(&pdata->recv_buffer);
 }
 
 /* VMBus primary channel is opened on first use */
@@ -303,46 +296,40 @@ hv_uio_probe(struct hv_device *dev,
 	pdata->info.mem[MON_PAGE_MAP].memtype = UIO_MEM_LOGICAL;
 
 	if (channel->device_id == HV_NIC) {
-		pdata->recv_buf = vzalloc(RECV_BUFFER_SIZE);
-		if (!pdata->recv_buf) {
-			ret = -ENOMEM;
+		ret = vmbus_alloc_buffer(channel, RECV_BUFFER_SIZE,
+					 channel->co_external_memory,
+					 &pdata->recv_buffer);
+		if (ret)
 			goto fail_free_ring;
-		}
 
-		ret = vmbus_establish_gpadl(channel, pdata->recv_buf,
-					    RECV_BUFFER_SIZE, &pdata->recv_gpadl);
-		if (ret) {
-			if (!pdata->recv_gpadl.decrypted)
-				vfree(pdata->recv_buf);
+		ret = vmbus_establish_gpadl(channel, &pdata->recv_buffer);
+		if (ret)
 			goto fail_close;
-		}
 
 		/* put Global Physical Address Label in name */
 		snprintf(pdata->recv_name, sizeof(pdata->recv_name),
-			 "recv:%u", pdata->recv_gpadl.gpadl_handle);
+			 "recv:%u", pdata->recv_buffer.gpadl_handle);
 		pdata->info.mem[RECV_BUF_MAP].name = pdata->recv_name;
-		pdata->info.mem[RECV_BUF_MAP].addr = (uintptr_t)pdata->recv_buf;
+		pdata->info.mem[RECV_BUF_MAP].addr =
+			(uintptr_t)pdata->recv_buffer.addr;
 		pdata->info.mem[RECV_BUF_MAP].size = RECV_BUFFER_SIZE;
 		pdata->info.mem[RECV_BUF_MAP].memtype = UIO_MEM_VIRTUAL;
 
-		pdata->send_buf = vzalloc(SEND_BUFFER_SIZE);
-		if (!pdata->send_buf) {
-			ret = -ENOMEM;
+		ret = vmbus_alloc_buffer(channel, SEND_BUFFER_SIZE,
+					 channel->co_external_memory,
+					 &pdata->send_buffer);
+		if (ret)
 			goto fail_close;
-		}
 
-		ret = vmbus_establish_gpadl(channel, pdata->send_buf,
-					    SEND_BUFFER_SIZE, &pdata->send_gpadl);
-		if (ret) {
-			if (!pdata->send_gpadl.decrypted)
-				vfree(pdata->send_buf);
+		ret = vmbus_establish_gpadl(channel, &pdata->send_buffer);
+		if (ret)
 			goto fail_close;
-		}
 
 		snprintf(pdata->send_name, sizeof(pdata->send_name),
-			 "send:%u", pdata->send_gpadl.gpadl_handle);
+			 "send:%u", pdata->send_buffer.gpadl_handle);
 		pdata->info.mem[SEND_BUF_MAP].name = pdata->send_name;
-		pdata->info.mem[SEND_BUF_MAP].addr = (uintptr_t)pdata->send_buf;
+		pdata->info.mem[SEND_BUF_MAP].addr =
+			(uintptr_t)pdata->send_buffer.addr;
 		pdata->info.mem[SEND_BUF_MAP].size = SEND_BUFFER_SIZE;
 		pdata->info.mem[SEND_BUF_MAP].memtype = UIO_MEM_VIRTUAL;
 	}
diff --git a/include/linux/hyperv.h b/include/linux/hyperv.h
index e85a1a043e..17ad83d19a 100644
--- a/include/linux/hyperv.h
+++ b/include/linux/hyperv.h
@@ -70,8 +70,7 @@
  */
 enum hv_gpadl_type {
 	HV_GPADL_BUFFER,
-	HV_GPADL_RING,
-	HV_GPADL_BUFFER_DECRYPTED
+	HV_GPADL_RING
 };
 
 /* Single-page buffer */
@@ -816,6 +815,16 @@ struct vmbus_gpadl {
 	bool decrypted;
 };
 
+struct vmbus_buffer {
+	void *addr;
+	struct page **chunks;
+	struct page **pages;
+	u32 size;
+	u32 chunk_cnt;
+	u32 gpadl_handle;
+	bool leak;
+};
+
 struct vmbus_channel {
 	struct list_head listentry;
 
@@ -1232,25 +1241,16 @@ extern int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel,
 				     u32 bufferlen,
 				     u64 requestid);
 
-extern int vmbus_establish_gpadl(struct vmbus_channel *channel,
-				      void *kbuffer,
-				      u32 size,
-				      struct vmbus_gpadl *gpadl);
-
-extern int vmbus_establish_gpadl_caller_decrypted(struct vmbus_channel *channel,
-						  void *kbuffer,
-						  u32 size,
-						  struct vmbus_gpadl *gpadl);
+int vmbus_establish_gpadl(struct vmbus_channel *channel,
+			  struct vmbus_buffer *buffer);
 
-extern int vmbus_teardown_gpadl(struct vmbus_channel *channel,
-				     struct vmbus_gpadl *gpadl);
+int vmbus_teardown_gpadl(struct vmbus_channel *channel,
+			 struct vmbus_buffer *buffer);
 
-extern void *vmbus_alloc_buffer(struct vmbus_channel *channel,
-				u32 size,
-				struct page ***chunks_out,
-				u32 *chunk_cnt_out);
+int vmbus_alloc_buffer(struct vmbus_channel *channel, u32 size,
+		       bool confidential, struct vmbus_buffer *buffer);
 
-extern void vmbus_free_buffer(void *addr, struct page **chunks, u32 chunk_cnt);
+void vmbus_free_buffer(struct vmbus_buffer *buffer);
 
 void vmbus_reset_channel_cb(struct vmbus_channel *channel);
 
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v3 2/3] hv: vmbus: allocate ring buffers via vmbus_alloc_buffer()
  2026-11-24 21:40 [PATCH v3 0/3] hv: vmbus: make rings and host-visible buffers survive buddy fragmentation Emerson Busson
  2026-11-24 21:40 ` [PATCH v3 1/3] hv: vmbus: encapsulate buffer and GPADL state in struct vmbus_buffer Emerson Busson
@ 2026-11-24 21:40 ` Emerson Busson
  2026-11-24 21:40 ` [PATCH v3 3/3] hv: vmbus: allocate requestor metadata with kvcalloc() Emerson Busson
  2 siblings, 0 replies; 4+ messages in thread
From: Emerson Busson @ 2026-11-24 21:40 UTC (permalink / raw)
  To: Wei Liu, Haiyang Zhang, Dexuan Cui, Greg Kroah-Hartman
  Cc: Michael Kelley, Easwar Hariharan, Kameron Carr, Andrew Lunn,
	David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	linux-hyperv, netdev, linux-kernel

vmbus_alloc_ring() allocates both ring buffers as a single high-order
contiguous block via alloc_pages_node() (e.g., order-7 for a 512 KiB
ring). Under physical memory fragmentation, opening new channels or
subchannels fails with -ENOMEM even when plenty of order-0 pages remain
available.

Allocate ring buffers through vmbus_alloc_buffer() using
channel->co_ring_buffer and embed struct vmbus_buffer directly in
struct vmbus_channel. Ordinary and confidential rings use
vzalloc_node(), while host-visible rings in isolated guests (SNP, TDX,
and CCA) use the chunked direct-map decryption and vmap() path.

Update hv_ringbuffer_init() to take the ring's struct page array and
use kvcalloc()/kvzalloc() for its wraparound page table and packet copy
buffer. Update uio_hv_generic to map non-contiguous ring pages via
mmap_action_map_kernel_pages() (with pgprot_decrypted() for shared
rings) and UIO_MEM_VIRTUAL.

With all GPADL callers now allocating and decrypting buffers through
vmbus_alloc_buffer(), drop struct vmbus_gpadl and remove the duplicate
set_memory_decrypted()/set_memory_encrypted() handling from GPADL
establishment and teardown.

Signed-off-by: Emerson Busson <emersonbusson@gmail.com>
---
 drivers/hv/channel.c         | 206 ++++++++++++-----------------------
 drivers/hv/hyperv_vmbus.h    |   4 +-
 drivers/hv/ring_buffer.c     |  17 +--
 drivers/uio/uio_hv_generic.c |  32 +++---
 include/linux/hyperv.h       |  13 +--
 5 files changed, 98 insertions(+), 174 deletions(-)

diff --git a/drivers/hv/channel.c b/drivers/hv/channel.c
index 9807ff35a1..c0bf154915 100644
--- a/drivers/hv/channel.c
+++ b/drivers/hv/channel.c
@@ -12,6 +12,7 @@
 #include <linux/sched.h>
 #include <linux/wait.h>
 #include <linux/mm.h>
+#include <linux/overflow.h>
 #include <linux/slab.h>
 #include <linux/log2.h>
 #include <linux/module.h>
@@ -154,15 +155,7 @@ void vmbus_free_ring(struct vmbus_channel *channel)
 {
 	hv_ringbuffer_cleanup(&channel->outbound);
 	hv_ringbuffer_cleanup(&channel->inbound);
-
-	if (channel->ringbuffer_page) {
-		/* In a CoCo VM leak the memory if it didn't get re-encrypted */
-		if (!channel->ringbuffer_gpadlhandle.decrypted)
-			__free_pages(channel->ringbuffer_page,
-			     get_order(channel->ringbuffer_pagecount
-				       << PAGE_SHIFT));
-		channel->ringbuffer_page = NULL;
-	}
+	vmbus_free_buffer(&channel->ringbuffer);
 }
 EXPORT_SYMBOL_GPL(vmbus_free_ring);
 
@@ -170,25 +163,33 @@ EXPORT_SYMBOL_GPL(vmbus_free_ring);
 int vmbus_alloc_ring(struct vmbus_channel *newchannel,
 		     u32 send_size, u32 recv_size)
 {
-	struct page *page;
-	int order;
+	struct vmbus_buffer *buffer = &newchannel->ringbuffer;
+	u32 size, page_cnt, i;
+	int ret;
 
-	if (send_size % PAGE_SIZE || recv_size % PAGE_SIZE)
+	if (!send_size || !recv_size ||
+	    send_size % PAGE_SIZE || recv_size % PAGE_SIZE ||
+	    check_add_overflow(send_size, recv_size, &size))
 		return -EINVAL;
 
-	/* Allocate the ring buffer */
-	order = get_order(send_size + recv_size);
-	page = alloc_pages_node(cpu_to_node(newchannel->target_cpu),
-				GFP_KERNEL|__GFP_ZERO, order);
-
-	if (!page)
-		page = alloc_pages(GFP_KERNEL|__GFP_ZERO, order);
+	ret = vmbus_alloc_buffer(newchannel, size, newchannel->co_ring_buffer,
+				 buffer);
+	if (ret)
+		return ret;
 
-	if (!page)
+	page_cnt = size >> PAGE_SHIFT;
+	buffer->pages = kvcalloc(page_cnt, sizeof(*buffer->pages), GFP_KERNEL);
+	if (!buffer->pages) {
+		vmbus_free_buffer(buffer);
 		return -ENOMEM;
+	}
+
+	for (i = 0; i < page_cnt; i++)
+		buffer->pages[i] =
+			vmalloc_to_page(buffer->addr +
+					((unsigned long)i << PAGE_SHIFT));
 
-	newchannel->ringbuffer_page = page;
-	newchannel->ringbuffer_pagecount = (send_size + recv_size) >> PAGE_SHIFT;
+	newchannel->ringbuffer_pagecount = page_cnt;
 	newchannel->ringbuffer_send_offset = send_size >> PAGE_SHIFT;
 
 	return 0;
@@ -432,17 +433,14 @@ static void vmbus_free_channel_msginfo(struct vmbus_channel_msginfo *msginfo)
  *
  * @channel: a channel
  * @type: the type of the corresponding GPADL, only meaningful for the guest.
- * @kbuffer: from kmalloc or vmalloc
- * @size: page-size multiple
+ * @buffer: buffer allocated with vmbus_alloc_buffer()
  * @send_offset: the offset (in bytes) where the send ring buffer starts,
- *              should be 0 for BUFFER type gpadl
- * @gpadl_handle: some funky thing
+ *               should be 0 for BUFFER type gpadl
  */
 static int __vmbus_establish_gpadl(struct vmbus_channel *channel,
-				   enum hv_gpadl_type type, void *kbuffer,
-				   u32 size, u32 send_offset,
-				   bool caller_decrypted, u32 *gpadl_handle,
-				   bool *decrypted, bool *leak)
+				   enum hv_gpadl_type type,
+				   struct vmbus_buffer *buffer,
+				   u32 send_offset)
 {
 	struct vmbus_channel_gpadl_header *gpadlmsg;
 	struct vmbus_channel_gpadl_body *gpadl_body;
@@ -452,42 +450,17 @@ static int __vmbus_establish_gpadl(struct vmbus_channel *channel,
 	u32 next_gpadl_handle;
 	unsigned long flags;
 	bool posted = false;
-	bool need_decrypt;
 	int ret = 0;
 
-	if (leak)
-		*leak = false;
+	buffer->leak = false;
 
 	next_gpadl_handle =
 		(atomic_inc_return(&vmbus_connection.next_gpadl_handle) - 1);
 
-	ret = create_gpadl_header(type, kbuffer, size, send_offset, &msginfo);
-	if (ret) {
-		if (decrypted)
-			*decrypted = false;
+	ret = create_gpadl_header(type, buffer->addr, buffer->size,
+				  send_offset, &msginfo);
+	if (ret)
 		return ret;
-	}
-
-	need_decrypt = !caller_decrypted &&
-		!((channel->co_external_memory && type == HV_GPADL_BUFFER) ||
-		  (channel->co_ring_buffer && type == HV_GPADL_RING));
-	if (decrypted)
-		*decrypted = need_decrypt;
-	if (need_decrypt) {
-		/*
-		 * If set_memory_decrypted() fails, the encryption state is
-		 * unknown; leave *decrypted true so the memory is leaked.
-		 */
-		ret = set_memory_decrypted((unsigned long)kbuffer,
-					   PFN_UP(size));
-		if (ret) {
-			dev_warn(&channel->device_obj->device,
-				 "Failed to set host visibility for new GPADL %d.\n",
-				 ret);
-			vmbus_free_channel_msginfo(msginfo);
-			return ret;
-		}
-	}
 
 	init_completion(&msginfo->waitevent);
 	msginfo->waiting_channel = channel;
@@ -552,7 +525,7 @@ static int __vmbus_establish_gpadl(struct vmbus_channel *channel,
 	}
 
 	/* At this point, we received the gpadl created msg */
-	*gpadl_handle = gpadlmsg->gpadl;
+	buffer->gpadl_handle = gpadlmsg->gpadl;
 	posted = false;
 
 cleanup:
@@ -562,14 +535,8 @@ static int __vmbus_establish_gpadl(struct vmbus_channel *channel,
 
 	vmbus_free_channel_msginfo(msginfo);
 
-	if (ret && posted && leak)
-		*leak = true;
-
-	if (ret && !posted && need_decrypt) {
-		if (!set_memory_encrypted((unsigned long)kbuffer, PFN_UP(size)) &&
-		    decrypted)
-			*decrypted = false;
-	}
+	if (ret && posted)
+		buffer->leak = true;
 
 	return ret;
 }
@@ -587,10 +554,7 @@ static int __vmbus_establish_gpadl(struct vmbus_channel *channel,
 int vmbus_establish_gpadl(struct vmbus_channel *channel,
 			  struct vmbus_buffer *buffer)
 {
-	return __vmbus_establish_gpadl(channel, HV_GPADL_BUFFER, buffer->addr,
-				       buffer->size, 0U, true,
-				       &buffer->gpadl_handle, NULL,
-				       &buffer->leak);
+	return __vmbus_establish_gpadl(channel, HV_GPADL_BUFFER, buffer, 0U);
 }
 EXPORT_SYMBOL_GPL(vmbus_establish_gpadl);
 
@@ -824,17 +788,13 @@ static void vmbus_free_requestor(struct vmbus_requestor *rqstor)
 	bitmap_free(rqstor->req_bitmap);
 }
 
-static int __vmbus_teardown_gpadl(struct vmbus_channel *channel,
-				  u32 *gpadl_handle, void *kbuffer,
-				  u32 size, bool *decrypted, bool *leak);
-
 static int __vmbus_open(struct vmbus_channel *newchannel,
 		       void *userdata, u32 userdatalen,
 		       void (*onchannelcallback)(void *context), void *context)
 {
 	struct vmbus_channel_open_channel *open_msg;
 	struct vmbus_channel_msginfo *open_info = NULL;
-	struct page *page = newchannel->ringbuffer_page;
+	struct vmbus_buffer *buffer = &newchannel->ringbuffer;
 	u32 send_pages, recv_pages;
 	unsigned long flags;
 	int err;
@@ -842,6 +802,10 @@ static int __vmbus_open(struct vmbus_channel *newchannel,
 	if (userdatalen > MAX_USER_DEFINED_BYTES)
 		return -EINVAL;
 
+	if (!buffer->addr || !buffer->pages || buffer->leak ||
+	    buffer->gpadl_handle)
+		return -EINVAL;
+
 	send_pages = newchannel->ringbuffer_send_offset;
 	recv_pages = newchannel->ringbuffer_pagecount - send_pages;
 
@@ -862,29 +826,21 @@ static int __vmbus_open(struct vmbus_channel *newchannel,
 		newchannel->max_pkt_size = VMBUS_DEFAULT_MAX_PKT_SIZE;
 
 	/* Establish the gpadl for the ring buffer */
-	newchannel->ringbuffer_gpadlhandle.gpadl_handle = 0;
-
-	err = __vmbus_establish_gpadl(newchannel, HV_GPADL_RING,
-				      page_address(newchannel->ringbuffer_page),
-				      (send_pages + recv_pages) << PAGE_SHIFT,
-				      newchannel->ringbuffer_send_offset << PAGE_SHIFT,
-				      false,
-				      &newchannel->ringbuffer_gpadlhandle.gpadl_handle,
-				      &newchannel->ringbuffer_gpadlhandle.decrypted,
-				      NULL);
+	buffer->gpadl_handle = 0;
+
+	err = __vmbus_establish_gpadl(newchannel, HV_GPADL_RING, buffer,
+				      newchannel->ringbuffer_send_offset << PAGE_SHIFT);
 	if (err)
 		goto error_clean_ring;
-	newchannel->ringbuffer_gpadlhandle.buffer =
-		page_address(newchannel->ringbuffer_page);
-	newchannel->ringbuffer_gpadlhandle.size =
-		(send_pages + recv_pages) << PAGE_SHIFT;
 
 	err = hv_ringbuffer_init(&newchannel->outbound,
-				 page, send_pages, 0, newchannel->co_ring_buffer);
+				 buffer->pages, send_pages, 0,
+				 newchannel->co_ring_buffer);
 	if (err)
 		goto error_free_gpadl;
 
-	err = hv_ringbuffer_init(&newchannel->inbound, &page[send_pages],
+	err = hv_ringbuffer_init(&newchannel->inbound,
+				 buffer->pages + send_pages,
 				 recv_pages, newchannel->max_pkt_size,
 				 newchannel->co_ring_buffer);
 	if (err)
@@ -906,8 +862,7 @@ static int __vmbus_open(struct vmbus_channel *newchannel,
 	open_msg->header.msgtype = CHANNELMSG_OPENCHANNEL;
 	open_msg->openid = newchannel->offermsg.child_relid;
 	open_msg->child_relid = newchannel->offermsg.child_relid;
-	open_msg->ringbuffer_gpadlhandle
-		= newchannel->ringbuffer_gpadlhandle.gpadl_handle;
+	open_msg->ringbuffer_gpadlhandle = buffer->gpadl_handle;
 	/*
 	 * The unit of ->downstream_ringbuffer_pageoffset is HV_HYP_PAGE and
 	 * the unit of ->ringbuffer_send_offset (i.e. send_pages) is PAGE, so
@@ -965,12 +920,7 @@ static int __vmbus_open(struct vmbus_channel *newchannel,
 error_free_info:
 	kfree(open_info);
 error_free_gpadl:
-	__vmbus_teardown_gpadl(newchannel,
-			       &newchannel->ringbuffer_gpadlhandle.gpadl_handle,
-			       newchannel->ringbuffer_gpadlhandle.buffer,
-			       newchannel->ringbuffer_gpadlhandle.size,
-			       &newchannel->ringbuffer_gpadlhandle.decrypted,
-			       NULL);
+	vmbus_teardown_gpadl(newchannel, buffer);
 error_clean_ring:
 	hv_ringbuffer_cleanup(&newchannel->outbound);
 	hv_ringbuffer_cleanup(&newchannel->inbound);
@@ -1013,20 +963,24 @@ int vmbus_open(struct vmbus_channel *newchannel,
 }
 EXPORT_SYMBOL_GPL(vmbus_open);
 
-static int __vmbus_teardown_gpadl(struct vmbus_channel *channel,
-				  u32 *gpadl_handle, void *kbuffer,
-				  u32 size, bool *decrypted, bool *leak)
+/*
+ * vmbus_teardown_gpadl - Teardown the specified GPADL handle
+ */
+int vmbus_teardown_gpadl(struct vmbus_channel *channel,
+			 struct vmbus_buffer *buffer)
 {
 	struct vmbus_channel_gpadl_teardown *msg;
 	struct vmbus_channel_msginfo *info;
 	unsigned long flags;
 	int ret = 0;
 
+	if (!buffer->gpadl_handle)
+		return 0;
+
 	info = kzalloc(sizeof(*info) +
 		       sizeof(struct vmbus_channel_gpadl_teardown), GFP_KERNEL);
 	if (!info) {
-		if (leak)
-			*leak = true;
+		buffer->leak = true;
 		return -ENOMEM;
 	}
 
@@ -1037,7 +991,7 @@ static int __vmbus_teardown_gpadl(struct vmbus_channel *channel,
 
 	msg->header.msgtype = CHANNELMSG_GPADL_TEARDOWN;
 	msg->child_relid = channel->offermsg.child_relid;
-	msg->gpadl = *gpadl_handle;
+	msg->gpadl = buffer->gpadl_handle;
 
 	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
 	list_add_tail(&info->msglistentry,
@@ -1072,34 +1026,12 @@ static int __vmbus_teardown_gpadl(struct vmbus_channel *channel,
 	kfree(info);
 
 	if (!ret)
-		*gpadl_handle = 0;
-	else if (leak)
-		*leak = true;
-
-	if (!ret && decrypted && *decrypted) {
-		ret = set_memory_encrypted((unsigned long)kbuffer,
-					   PFN_UP(size));
-		if (ret)
-			pr_warn("Fail to set mem host visibility in GPADL teardown %d.\n",
-				ret);
-		*decrypted = !!ret;
-	}
+		buffer->gpadl_handle = 0;
+	else
+		buffer->leak = true;
 
 	return ret;
 }
-
-/*
- * vmbus_teardown_gpadl - Teardown the specified GPADL handle
- */
-int vmbus_teardown_gpadl(struct vmbus_channel *channel,
-			 struct vmbus_buffer *buffer)
-{
-	if (!buffer->gpadl_handle)
-		return 0;
-
-	return __vmbus_teardown_gpadl(channel, &buffer->gpadl_handle,
-				      NULL, 0, NULL, &buffer->leak);
-}
 EXPORT_SYMBOL_GPL(vmbus_teardown_gpadl);
 
 void vmbus_reset_channel_cb(struct vmbus_channel *channel)
@@ -1164,6 +1096,7 @@ static int vmbus_close_internal(struct vmbus_channel *channel)
 	trace_vmbus_close_internal(msg, ret);
 
 	if (ret) {
+		channel->ringbuffer.leak = true;
 		pr_err("Close failed: close post msg return is %d\n", ret);
 		/*
 		 * If we failed to post the close msg,
@@ -1172,13 +1105,8 @@ static int vmbus_close_internal(struct vmbus_channel *channel)
 	}
 
 	/* Tear down the gpadl for the channel's ring buffer */
-	else if (channel->ringbuffer_gpadlhandle.gpadl_handle) {
-		ret = __vmbus_teardown_gpadl(channel,
-					     &channel->ringbuffer_gpadlhandle.gpadl_handle,
-					     channel->ringbuffer_gpadlhandle.buffer,
-					     channel->ringbuffer_gpadlhandle.size,
-					     &channel->ringbuffer_gpadlhandle.decrypted,
-					     NULL);
+	else if (channel->ringbuffer.gpadl_handle) {
+		ret = vmbus_teardown_gpadl(channel, &channel->ringbuffer);
 		if (ret) {
 			pr_err("Close failed: teardown gpadl return %d\n", ret);
 			/*
diff --git a/drivers/hv/hyperv_vmbus.h b/drivers/hv/hyperv_vmbus.h
index 33923621a5..98eacd737f 100644
--- a/drivers/hv/hyperv_vmbus.h
+++ b/drivers/hv/hyperv_vmbus.h
@@ -204,8 +204,8 @@ extern int hv_synic_cleanup(unsigned int cpu);
 void hv_ringbuffer_pre_init(struct vmbus_channel *channel);
 
 int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
-		       struct page *pages, u32 pagecnt, u32 max_pkt_size,
-			   bool confidential);
+		       struct page **pages, u32 pagecnt, u32 max_pkt_size,
+		       bool confidential);
 
 void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info);
 
diff --git a/drivers/hv/ring_buffer.c b/drivers/hv/ring_buffer.c
index d8b085dfdb..15abab9a9a 100644
--- a/drivers/hv/ring_buffer.c
+++ b/drivers/hv/ring_buffer.c
@@ -146,8 +146,8 @@ void hv_ringbuffer_pre_init(struct vmbus_channel *channel)
 
 /* Initialize the ring buffer. */
 int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
-		       struct page *pages, u32 page_cnt, u32 max_pkt_size,
-			   bool confidential)
+		       struct page **pages, u32 page_cnt, u32 max_pkt_size,
+		       bool confidential)
 {
 	struct page **pages_wraparound;
 	int i;
@@ -158,20 +158,21 @@ int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
 	 * First page holds struct hv_ring_buffer, do wraparound mapping for
 	 * the rest.
 	 */
-	pages_wraparound = kzalloc_objs(struct page *, page_cnt * 2 - 1);
+	pages_wraparound = kvcalloc(page_cnt * 2 - 1, sizeof(*pages_wraparound),
+				    GFP_KERNEL);
 	if (!pages_wraparound)
 		return -ENOMEM;
 
-	pages_wraparound[0] = pages;
+	pages_wraparound[0] = pages[0];
 	for (i = 0; i < 2 * (page_cnt - 1); i++)
 		pages_wraparound[i + 1] =
-			&pages[i % (page_cnt - 1) + 1];
+			pages[i % (page_cnt - 1) + 1];
 
 	ring_info->ring_buffer = (struct hv_ring_buffer *)
 		vmap(pages_wraparound, page_cnt * 2 - 1, VM_MAP,
 			confidential ? PAGE_KERNEL : pgprot_decrypted(PAGE_KERNEL));
 
-	kfree(pages_wraparound);
+	kvfree(pages_wraparound);
 	if (!ring_info->ring_buffer)
 		return -ENOMEM;
 
@@ -196,7 +197,7 @@ int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
 
 	/* Initialize buffer that holds copies of incoming packets */
 	if (max_pkt_size) {
-		ring_info->pkt_buffer = kzalloc(max_pkt_size, GFP_KERNEL);
+		ring_info->pkt_buffer = kvzalloc(max_pkt_size, GFP_KERNEL);
 		if (!ring_info->pkt_buffer)
 			return -ENOMEM;
 		ring_info->pkt_buffer_size = max_pkt_size;
@@ -215,7 +216,7 @@ void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info)
 	ring_info->ring_buffer = NULL;
 	mutex_unlock(&ring_info->ring_buffer_mutex);
 
-	kfree(ring_info->pkt_buffer);
+	kvfree(ring_info->pkt_buffer);
 	ring_info->pkt_buffer = NULL;
 	ring_info->pkt_buffer_size = 0;
 }
diff --git a/drivers/uio/uio_hv_generic.c b/drivers/uio/uio_hv_generic.c
index 9c6449c92a..09995cfb5b 100644
--- a/drivers/uio/uio_hv_generic.c
+++ b/drivers/uio/uio_hv_generic.c
@@ -27,6 +27,7 @@
 #include <linux/if_ether.h>
 #include <linux/skbuff.h>
 #include <linux/hyperv.h>
+#include <linux/mm.h>
 #include <linux/slab.h>
 
 #include "../hv/hyperv_vmbus.h"
@@ -147,19 +148,24 @@ static void hv_uio_rescind(struct vmbus_channel *channel)
 	vmbus_device_unregister(channel->device_obj);
 }
 
-/* Function used for mmap of ring buffer sysfs interface.
- * The ring buffer is allocated as contiguous memory by vmbus_open
- */
+/* Function used for mmap of the ring buffer sysfs interface. */
 static int
 hv_uio_ring_mmap_prepare(struct vmbus_channel *channel, struct vm_area_desc *desc)
 {
-	void *ring_buffer = page_address(channel->ringbuffer_page);
+	unsigned long pages = vma_desc_pages(desc);
+	pgoff_t offset = desc->pgoff;
 
-	if (channel->state != CHANNEL_OPENED_STATE)
+	if (channel->state != CHANNEL_OPENED_STATE || !channel->ringbuffer.pages)
 		return -ENODEV;
+	if (!pages || offset >= channel->ringbuffer_pagecount ||
+	    pages > channel->ringbuffer_pagecount - offset)
+		return -EINVAL;
+
+	if (!channel->co_ring_buffer)
+		desc->page_prot = pgprot_decrypted(desc->page_prot);
 
-	mmap_action_simple_ioremap(desc, virt_to_phys(ring_buffer),
-			channel->ringbuffer_pagecount << PAGE_SHIFT);
+	mmap_action_map_kernel_pages(desc, desc->start,
+				     channel->ringbuffer.pages + offset, pages);
 	return 0;
 }
 
@@ -245,7 +251,6 @@ hv_uio_probe(struct hv_device *dev,
 {
 	struct vmbus_channel *channel = dev->channel;
 	struct hv_uio_private_data *pdata;
-	void *ring_buffer;
 	int ret;
 	size_t ring_size = hv_dev_ring_size(channel);
 
@@ -276,12 +281,11 @@ hv_uio_probe(struct hv_device *dev,
 
 	/* mem resources */
 	pdata->info.mem[TXRX_RING_MAP].name = "txrx_rings";
-	ring_buffer = page_address(channel->ringbuffer_page);
-	pdata->info.mem[TXRX_RING_MAP].addr
-		= (uintptr_t)virt_to_phys(ring_buffer);
-	pdata->info.mem[TXRX_RING_MAP].size
-		= channel->ringbuffer_pagecount << PAGE_SHIFT;
-	pdata->info.mem[TXRX_RING_MAP].memtype = UIO_MEM_IOVA;
+	pdata->info.mem[TXRX_RING_MAP].addr =
+		(uintptr_t)channel->ringbuffer.addr;
+	pdata->info.mem[TXRX_RING_MAP].size =
+		channel->ringbuffer_pagecount << PAGE_SHIFT;
+	pdata->info.mem[TXRX_RING_MAP].memtype = UIO_MEM_VIRTUAL;
 
 	pdata->info.mem[INT_PAGE_MAP].name = "int_page";
 	pdata->info.mem[INT_PAGE_MAP].addr
diff --git a/include/linux/hyperv.h b/include/linux/hyperv.h
index 17ad83d19a..41b9976945 100644
--- a/include/linux/hyperv.h
+++ b/include/linux/hyperv.h
@@ -808,13 +808,6 @@ struct vmbus_device {
 
 #define VMBUS_DEFAULT_MAX_PKT_SIZE 4096
 
-struct vmbus_gpadl {
-	u32 gpadl_handle;
-	u32 size;
-	void *buffer;
-	bool decrypted;
-};
-
 struct vmbus_buffer {
 	void *addr;
 	struct page **chunks;
@@ -844,10 +837,8 @@ struct vmbus_channel {
 	bool rescind_ref; /* got rescind msg, got channel reference */
 	struct completion rescind_event;
 
-	struct vmbus_gpadl ringbuffer_gpadlhandle;
-
-	/* Allocated memory for ring buffer */
-	struct page *ringbuffer_page;
+	/* Allocated memory and GPADL state for ring buffer */
+	struct vmbus_buffer ringbuffer;
 	u32 ringbuffer_pagecount;
 	u32 ringbuffer_send_offset;
 	struct hv_ring_buffer_info outbound;	/* send to parent */
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH v3 3/3] hv: vmbus: allocate requestor metadata with kvcalloc()
  2026-11-24 21:40 [PATCH v3 0/3] hv: vmbus: make rings and host-visible buffers survive buddy fragmentation Emerson Busson
  2026-11-24 21:40 ` [PATCH v3 1/3] hv: vmbus: encapsulate buffer and GPADL state in struct vmbus_buffer Emerson Busson
  2026-11-24 21:40 ` [PATCH v3 2/3] hv: vmbus: allocate ring buffers via vmbus_alloc_buffer() Emerson Busson
@ 2026-11-24 21:40 ` Emerson Busson
  2 siblings, 0 replies; 4+ messages in thread
From: Emerson Busson @ 2026-11-24 21:40 UTC (permalink / raw)
  To: Wei Liu, Haiyang Zhang, Dexuan Cui, Greg Kroah-Hartman
  Cc: Michael Kelley, Easwar Hariharan, Kameron Carr, Andrew Lunn,
	David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	linux-hyperv, netdev, linux-kernel

Opening a VMBus channel allocates a request-ID array and bitmap sized
from the ring buffer. For a 128-page NetVSC ring, kcalloc() and
bitmap_zalloc() require multi-page physically contiguous allocations
that can fail under buddy fragmentation even when order-0 pages remain
available.

Switch both guest-private structures to kvcalloc() and free them with
kvfree(), keeping slab allocation for small channels while allowing
vmalloc fallback under fragmentation. Guard against a zero requestor
size and clear the pointers after freeing.

Signed-off-by: Emerson Busson <emersonbusson@gmail.com>
---
 drivers/hv/channel.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/drivers/hv/channel.c b/drivers/hv/channel.c
index c0bf154915..8547b10f3d 100644
--- a/drivers/hv/channel.c
+++ b/drivers/hv/channel.c
@@ -733,10 +733,13 @@ EXPORT_SYMBOL_GPL(vmbus_alloc_buffer);
  */
 static u64 *request_arr_init(u32 size)
 {
-	int i;
+	u32 i;
 	u64 *req_arr;
 
-	req_arr = kcalloc(size, sizeof(u64), GFP_KERNEL);
+	if (!size)
+		return NULL;
+
+	req_arr = kvcalloc(size, sizeof(*req_arr), GFP_KERNEL);
 	if (!req_arr)
 		return NULL;
 
@@ -763,9 +766,10 @@ static int vmbus_alloc_requestor(struct vmbus_requestor *rqstor, u32 size)
 	if (!rqst_arr)
 		return -ENOMEM;
 
-	bitmap = bitmap_zalloc(size, GFP_KERNEL);
+	bitmap = kvcalloc(size / BITS_PER_LONG + !!(size % BITS_PER_LONG),
+			  sizeof(*bitmap), GFP_KERNEL);
 	if (!bitmap) {
-		kfree(rqst_arr);
+		kvfree(rqst_arr);
 		return -ENOMEM;
 	}
 
@@ -784,8 +788,10 @@ static int vmbus_alloc_requestor(struct vmbus_requestor *rqstor, u32 size)
  */
 static void vmbus_free_requestor(struct vmbus_requestor *rqstor)
 {
-	kfree(rqstor->req_arr);
-	bitmap_free(rqstor->req_bitmap);
+	kvfree(rqstor->req_arr);
+	kvfree(rqstor->req_bitmap);
+	rqstor->req_arr = NULL;
+	rqstor->req_bitmap = NULL;
 }
 
 static int __vmbus_open(struct vmbus_channel *newchannel,
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-24 21:40 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24 21:40 [PATCH v3 0/3] hv: vmbus: make rings and host-visible buffers survive buddy fragmentation Emerson Busson
2026-11-24 21:40 ` [PATCH v3 1/3] hv: vmbus: encapsulate buffer and GPADL state in struct vmbus_buffer Emerson Busson
2026-11-24 21:40 ` [PATCH v3 2/3] hv: vmbus: allocate ring buffers via vmbus_alloc_buffer() Emerson Busson
2026-11-24 21:40 ` [PATCH v3 3/3] hv: vmbus: allocate requestor metadata with kvcalloc() Emerson Busson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).