Skip to content

Commit 0a4534b

Browse files
romank-msftliuw
authored andcommitted
Drivers: hv: Allocate encrypted buffers when requested
Confidential VMBus is built around using buffers not shared with the host. Support allocating encrypted buffers when requested. Signed-off-by: Roman Kisel <romank@linux.microsoft.com> Reviewed-by: Tianyu Lan <tiala@microsoft.com> Reviewed-by: Michael Kelley <mhklinux@outlook.com> Signed-off-by: Wei Liu <wei.liu@kernel.org>
1 parent e096fe2 commit 0a4534b

3 files changed

Lines changed: 34 additions & 23 deletions

File tree

drivers/hv/channel.c

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -444,20 +444,23 @@ static int __vmbus_establish_gpadl(struct vmbus_channel *channel,
444444
return ret;
445445
}
446446

447-
/*
448-
* Set the "decrypted" flag to true for the set_memory_decrypted()
449-
* success case. In the failure case, the encryption state of the
450-
* memory is unknown. Leave "decrypted" as true to ensure the
451-
* memory will be leaked instead of going back on the free list.
452-
*/
453-
gpadl->decrypted = true;
454-
ret = set_memory_decrypted((unsigned long)kbuffer,
455-
PFN_UP(size));
456-
if (ret) {
457-
dev_warn(&channel->device_obj->device,
458-
"Failed to set host visibility for new GPADL %d.\n",
459-
ret);
460-
return ret;
447+
gpadl->decrypted = !((channel->co_external_memory && type == HV_GPADL_BUFFER) ||
448+
(channel->co_ring_buffer && type == HV_GPADL_RING));
449+
if (gpadl->decrypted) {
450+
/*
451+
* The "decrypted" flag being true assumes that set_memory_decrypted() succeeds.
452+
* But if it fails, the encryption state of the memory is unknown. In that case,
453+
* leave "decrypted" as true to ensure the memory is leaked instead of going back
454+
* on the free list.
455+
*/
456+
ret = set_memory_decrypted((unsigned long)kbuffer,
457+
PFN_UP(size));
458+
if (ret) {
459+
dev_warn(&channel->device_obj->device,
460+
"Failed to set host visibility for new GPADL %d.\n",
461+
ret);
462+
return ret;
463+
}
461464
}
462465

463466
init_completion(&msginfo->waitevent);
@@ -545,8 +548,10 @@ static int __vmbus_establish_gpadl(struct vmbus_channel *channel,
545548
* left as true so the memory is leaked instead of being
546549
* put back on the free list.
547550
*/
548-
if (!set_memory_encrypted((unsigned long)kbuffer, PFN_UP(size)))
549-
gpadl->decrypted = false;
551+
if (gpadl->decrypted) {
552+
if (!set_memory_encrypted((unsigned long)kbuffer, PFN_UP(size)))
553+
gpadl->decrypted = false;
554+
}
550555
}
551556

552557
return ret;
@@ -677,12 +682,13 @@ static int __vmbus_open(struct vmbus_channel *newchannel,
677682
goto error_clean_ring;
678683

679684
err = hv_ringbuffer_init(&newchannel->outbound,
680-
page, send_pages, 0);
685+
page, send_pages, 0, newchannel->co_ring_buffer);
681686
if (err)
682687
goto error_free_gpadl;
683688

684689
err = hv_ringbuffer_init(&newchannel->inbound, &page[send_pages],
685-
recv_pages, newchannel->max_pkt_size);
690+
recv_pages, newchannel->max_pkt_size,
691+
newchannel->co_ring_buffer);
686692
if (err)
687693
goto error_free_gpadl;
688694

@@ -863,8 +869,11 @@ int vmbus_teardown_gpadl(struct vmbus_channel *channel, struct vmbus_gpadl *gpad
863869

864870
kfree(info);
865871

866-
ret = set_memory_encrypted((unsigned long)gpadl->buffer,
867-
PFN_UP(gpadl->size));
872+
if (gpadl->decrypted)
873+
ret = set_memory_encrypted((unsigned long)gpadl->buffer,
874+
PFN_UP(gpadl->size));
875+
else
876+
ret = 0;
868877
if (ret)
869878
pr_warn("Fail to set mem host visibility in GPADL teardown %d.\n", ret);
870879

drivers/hv/hyperv_vmbus.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ extern int hv_synic_cleanup(unsigned int cpu);
201201
void hv_ringbuffer_pre_init(struct vmbus_channel *channel);
202202

203203
int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
204-
struct page *pages, u32 pagecnt, u32 max_pkt_size);
204+
struct page *pages, u32 pagecnt, u32 max_pkt_size,
205+
bool confidential);
205206

206207
void hv_ringbuffer_cleanup(struct hv_ring_buffer_info *ring_info);
207208

drivers/hv/ring_buffer.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,8 @@ void hv_ringbuffer_pre_init(struct vmbus_channel *channel)
184184

185185
/* Initialize the ring buffer. */
186186
int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
187-
struct page *pages, u32 page_cnt, u32 max_pkt_size)
187+
struct page *pages, u32 page_cnt, u32 max_pkt_size,
188+
bool confidential)
188189
{
189190
struct page **pages_wraparound;
190191
int i;
@@ -208,7 +209,7 @@ int hv_ringbuffer_init(struct hv_ring_buffer_info *ring_info,
208209

209210
ring_info->ring_buffer = (struct hv_ring_buffer *)
210211
vmap(pages_wraparound, page_cnt * 2 - 1, VM_MAP,
211-
pgprot_decrypted(PAGE_KERNEL));
212+
confidential ? PAGE_KERNEL : pgprot_decrypted(PAGE_KERNEL));
212213

213214
kfree(pages_wraparound);
214215
if (!ring_info->ring_buffer)

0 commit comments

Comments
 (0)