From nobody Tue Oct 7 05:22:13 2025 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 94C2327AC2E; Fri, 3 Oct 2025 22:27:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=13.77.154.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759530445; cv=none; b=py3VnXvXoqsXuCvnH0DacWAPiLUf3ZZJ3f9dXK+HyNTT7YhbvQ9W+JOBcHgZ7Qe3JJ+ZzKCPMutrgi5YSVhHCGkWh898xCFecfkfVO/4pPEt+dBNte5V575E3LlkY8bXrP30DI2DXALr3nRSNoIf+uJSOsp9FeeMnuqYudj6SLA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759530445; c=relaxed/simple; bh=zsQY+WqYWAxLA21W3qx/i6NmAIG6aC4pmIlzWNp7h4M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZC6GrJztJjr6tnKqbLQamrIJJ6ZPJAFExH7iRpFmK/bUCakP77WAGOKEPEjlgaXShK1wgyEqPo4HoFy4Y8C8ndPb0droPyoBEktUjjLdD4u5wB6Mz4RT2HY7RG1J5AbEMMHz8JMuqukfKCAXdR2Qy4ZwIw+DHfblfaHEv7Tr3FI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com; spf=pass smtp.mailfrom=linux.microsoft.com; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b=IrTbYs0p; arc=none smtp.client-ip=13.77.154.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b="IrTbYs0p" Received: from romank-3650.corp.microsoft.com (unknown [131.107.1.188]) by linux.microsoft.com (Postfix) with ESMTPSA id 6130F211C27C; Fri, 3 Oct 2025 15:27:22 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 6130F211C27C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1759530442; bh=vtN9ga2NrbyJZ289tK3Dk45vzqdd8mFliIhwRfz5wjM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IrTbYs0pqbWZUaKoEM5lOjztegQ93vkvcrbSUAgJcuBua8op6dmo1OZVl+rRh6otu zFKRlVy/U70uzOGbBRhhzj6RagwDBWBsJcd1AeHhDMPykC5NbOx5OySoq7mbwOnK8i ey5kD+0RwN2FY3JQN9AQP8Kmt04yTM/FD2QUCXXw= From: Roman Kisel To: arnd@arndb.de, bp@alien8.de, corbet@lwn.net, dave.hansen@linux.intel.com, decui@microsoft.com, haiyangz@microsoft.com, hpa@zytor.com, kys@microsoft.com, mikelley@microsoft.com, mingo@redhat.com, tglx@linutronix.de, Tianyu.Lan@microsoft.com, wei.liu@kernel.org, x86@kernel.org, linux-hyperv@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org Cc: benhill@microsoft.com, bperkins@microsoft.com, sunilmut@microsoft.com, romank@linux.microsoft.com Subject: [PATCH hyperv-next v6 13/17] Drivers: hv: Allocate encrypted buffers when requested Date: Fri, 3 Oct 2025 15:27:06 -0700 Message-ID: <20251003222710.6257-14-romank@linux.microsoft.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251003222710.6257-1-romank@linux.microsoft.com> References: <20251003222710.6257-1-romank@linux.microsoft.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Confidential VMBus is built around using buffers not shared with the host. Support allocating encrypted buffers when requested. Signed-off-by: Roman Kisel Reviewed-by: Tianyu Lan Reviewed-by: Michael Kelley --- drivers/hv/channel.c | 49 +++++++++++++++++++++++---------------- drivers/hv/hyperv_vmbus.h | 3 ++- drivers/hv/ring_buffer.c | 5 ++-- 3 files changed, 34 insertions(+), 23 deletions(-) diff --git a/drivers/hv/channel.c b/drivers/hv/channel.c index 162d6aeece7b..d69713201bef 100644 --- a/drivers/hv/channel.c +++ b/drivers/hv/channel.c @@ -444,20 +444,23 @@ static int __vmbus_establish_gpadl(struct vmbus_chann= el *channel, return ret; } =20 - /* - * Set the "decrypted" flag to true for the set_memory_decrypted() - * success case. In the failure case, the encryption state of the - * memory is unknown. Leave "decrypted" as true to ensure the - * memory will be leaked instead of going back on the free list. - */ - gpadl->decrypted =3D true; - ret =3D 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); - return ret; + gpadl->decrypted =3D !((channel->co_external_memory && type =3D=3D HV_GPA= DL_BUFFER) || + (channel->co_ring_buffer && type =3D=3D HV_GPADL_RING)); + if (gpadl->decrypted) { + /* + * The "decrypted" flag being true assumes that set_memory_decrypted() s= ucceeds. + * But if it fails, the encryption state of the memory is unknown. In th= at case, + * leave "decrypted" as true to ensure the memory is leaked instead of g= oing back + * on the free list. + */ + ret =3D 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); + return ret; + } } =20 init_completion(&msginfo->waitevent); @@ -545,8 +548,10 @@ static int __vmbus_establish_gpadl(struct vmbus_channe= l *channel, * left as true so the memory is leaked instead of being * put back on the free list. */ - if (!set_memory_encrypted((unsigned long)kbuffer, PFN_UP(size))) - gpadl->decrypted =3D false; + if (gpadl->decrypted) { + if (!set_memory_encrypted((unsigned long)kbuffer, PFN_UP(size))) + gpadl->decrypted =3D false; + } } =20 return ret; @@ -677,12 +682,13 @@ static int __vmbus_open(struct vmbus_channel *newchan= nel, goto error_clean_ring; =20 err =3D hv_ringbuffer_init(&newchannel->outbound, - page, send_pages, 0); + page, send_pages, 0, newchannel->co_ring_buffer); if (err) goto error_free_gpadl; =20 err =3D hv_ringbuffer_init(&newchannel->inbound, &page[send_pages], - recv_pages, newchannel->max_pkt_size); + recv_pages, newchannel->max_pkt_size, + newchannel->co_ring_buffer); if (err) goto error_free_gpadl; =20 @@ -863,8 +869,11 @@ int vmbus_teardown_gpadl(struct vmbus_channel *channel= , struct vmbus_gpadl *gpad =20 kfree(info); =20 - ret =3D set_memory_encrypted((unsigned long)gpadl->buffer, - PFN_UP(gpadl->size)); + if (gpadl->decrypted) + ret =3D set_memory_encrypted((unsigned long)gpadl->buffer, + PFN_UP(gpadl->size)); + else + ret =3D 0; if (ret) pr_warn("Fail to set mem host visibility in GPADL teardown %d.\n", ret); =20 diff --git a/drivers/hv/hyperv_vmbus.h b/drivers/hv/hyperv_vmbus.h index 552ed782bcfc..f7fc2630c054 100644 --- a/drivers/hv/hyperv_vmbus.h +++ b/drivers/hv/hyperv_vmbus.h @@ -201,7 +201,8 @@ extern int hv_synic_cleanup(unsigned int cpu); void hv_ringbuffer_pre_init(struct vmbus_channel *channel); =20 int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info, - struct page *pages, u32 pagecnt, u32 max_pkt_size); + struct page *pages, u32 pagecnt, u32 max_pkt_size, + bool confidential); =20 void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info); =20 diff --git a/drivers/hv/ring_buffer.c b/drivers/hv/ring_buffer.c index 23ce1fb70de1..3c421a7f78c0 100644 --- a/drivers/hv/ring_buffer.c +++ b/drivers/hv/ring_buffer.c @@ -184,7 +184,8 @@ void hv_ringbuffer_pre_init(struct vmbus_channel *chann= el) =20 /* 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) + struct page *pages, u32 page_cnt, u32 max_pkt_size, + bool confidential) { struct page **pages_wraparound; int i; @@ -208,7 +209,7 @@ int hv_ringbuffer_init(struct hv_ring_buffer_info *ring= _info, =20 ring_info->ring_buffer =3D (struct hv_ring_buffer *) vmap(pages_wraparound, page_cnt * 2 - 1, VM_MAP, - pgprot_decrypted(PAGE_KERNEL)); + confidential ? PAGE_KERNEL : pgprot_decrypted(PAGE_KERNEL)); =20 kfree(pages_wraparound); if (!ring_info->ring_buffer) --=20 2.43.0