Skip to content

Commit 193061e

Browse files
tialaliuw
authored andcommitted
drivers: hv: Mark percpu hvcall input arg page unencrypted in SEV-SNP enlightened guest
Hypervisor needs to access input arg, VMBus synic event and message pages. Mark these pages unencrypted in the SEV-SNP guest and free them only if they have been marked encrypted successfully. Reviewed-by: Dexuan Cui <decui@microsoft.com> Reviewed-by: Michael Kelley <mikelley@microsoft.com> Signed-off-by: Tianyu Lan <tiala@microsoft.com> Signed-off-by: Wei Liu <wei.liu@kernel.org> Link: https://lore.kernel.org/r/20230818102919.1318039-5-ltykernel@gmail.com
1 parent b131035 commit 193061e

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed

drivers/hv/hv.c

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <linux/interrupt.h>
2121
#include <clocksource/hyperv_timer.h>
2222
#include <asm/mshyperv.h>
23+
#include <linux/set_memory.h>
2324
#include "hyperv_vmbus.h"
2425

2526
/* The one and only */
@@ -78,7 +79,7 @@ int hv_post_message(union hv_connection_id connection_id,
7879

7980
int hv_synic_alloc(void)
8081
{
81-
int cpu;
82+
int cpu, ret = -ENOMEM;
8283
struct hv_per_cpu_context *hv_cpu;
8384

8485
/*
@@ -123,26 +124,76 @@ int hv_synic_alloc(void)
123124
goto err;
124125
}
125126
}
127+
128+
if (hv_isolation_type_en_snp()) {
129+
ret = set_memory_decrypted((unsigned long)
130+
hv_cpu->synic_message_page, 1);
131+
if (ret) {
132+
pr_err("Failed to decrypt SYNIC msg page: %d\n", ret);
133+
hv_cpu->synic_message_page = NULL;
134+
135+
/*
136+
* Free the event page here so that hv_synic_free()
137+
* won't later try to re-encrypt it.
138+
*/
139+
free_page((unsigned long)hv_cpu->synic_event_page);
140+
hv_cpu->synic_event_page = NULL;
141+
goto err;
142+
}
143+
144+
ret = set_memory_decrypted((unsigned long)
145+
hv_cpu->synic_event_page, 1);
146+
if (ret) {
147+
pr_err("Failed to decrypt SYNIC event page: %d\n", ret);
148+
hv_cpu->synic_event_page = NULL;
149+
goto err;
150+
}
151+
152+
memset(hv_cpu->synic_message_page, 0, PAGE_SIZE);
153+
memset(hv_cpu->synic_event_page, 0, PAGE_SIZE);
154+
}
126155
}
127156

128157
return 0;
158+
129159
err:
130160
/*
131161
* Any memory allocations that succeeded will be freed when
132162
* the caller cleans up by calling hv_synic_free()
133163
*/
134-
return -ENOMEM;
164+
return ret;
135165
}
136166

137167

138168
void hv_synic_free(void)
139169
{
140-
int cpu;
170+
int cpu, ret;
141171

142172
for_each_present_cpu(cpu) {
143173
struct hv_per_cpu_context *hv_cpu
144174
= per_cpu_ptr(hv_context.cpu_context, cpu);
145175

176+
/* It's better to leak the page if the encryption fails. */
177+
if (hv_isolation_type_en_snp()) {
178+
if (hv_cpu->synic_message_page) {
179+
ret = set_memory_encrypted((unsigned long)
180+
hv_cpu->synic_message_page, 1);
181+
if (ret) {
182+
pr_err("Failed to encrypt SYNIC msg page: %d\n", ret);
183+
hv_cpu->synic_message_page = NULL;
184+
}
185+
}
186+
187+
if (hv_cpu->synic_event_page) {
188+
ret = set_memory_encrypted((unsigned long)
189+
hv_cpu->synic_event_page, 1);
190+
if (ret) {
191+
pr_err("Failed to encrypt SYNIC event page: %d\n", ret);
192+
hv_cpu->synic_event_page = NULL;
193+
}
194+
}
195+
}
196+
146197
free_page((unsigned long)hv_cpu->synic_event_page);
147198
free_page((unsigned long)hv_cpu->synic_message_page);
148199
}

drivers/hv/hv_common.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <linux/kmsg_dump.h>
2525
#include <linux/slab.h>
2626
#include <linux/dma-map-ops.h>
27+
#include <linux/set_memory.h>
2728
#include <asm/hyperv-tlfs.h>
2829
#include <asm/mshyperv.h>
2930

@@ -359,6 +360,7 @@ int hv_common_cpu_init(unsigned int cpu)
359360
u64 msr_vp_index;
360361
gfp_t flags;
361362
int pgcount = hv_root_partition ? 2 : 1;
363+
int ret;
362364

363365
/* hv_cpu_init() can be called with IRQs disabled from hv_resume() */
364366
flags = irqs_disabled() ? GFP_ATOMIC : GFP_KERNEL;
@@ -378,6 +380,17 @@ int hv_common_cpu_init(unsigned int cpu)
378380
outputarg = (void **)this_cpu_ptr(hyperv_pcpu_output_arg);
379381
*outputarg = (char *)(*inputarg) + HV_HYP_PAGE_SIZE;
380382
}
383+
384+
if (hv_isolation_type_en_snp()) {
385+
ret = set_memory_decrypted((unsigned long)*inputarg, pgcount);
386+
if (ret) {
387+
kfree(*inputarg);
388+
*inputarg = NULL;
389+
return ret;
390+
}
391+
392+
memset(*inputarg, 0x00, pgcount * PAGE_SIZE);
393+
}
381394
}
382395

383396
msr_vp_index = hv_get_register(HV_REGISTER_VP_INDEX);

0 commit comments

Comments
 (0)