Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ptp: kvm: Use decrypted memory in confidential guest on x86

KVM_HC_CLOCK_PAIRING currently fails inside SEV-SNP guests because the
guest passes an address to static data to the host. In confidential
computing the host can't access arbitrary guest memory so handling the
hypercall runs into an "rmpfault". To make the hypercall work, the guest
needs to explicitly mark the memory as decrypted. Do that in
kvm_arch_ptp_init(), but retain the previous behavior for
non-confidential guests to save us from having to allocate memory.

Add a new arch-specific function (kvm_arch_ptp_exit()) to free the
allocation and mark the memory as encrypted again.

Signed-off-by: Jeremi Piotrowski <jpiotrowski@linux.microsoft.com>
Link: https://lore.kernel.org/r/20230308150531.477741-1-jpiotrowski@linux.microsoft.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

Jeremi Piotrowski and committed by
Jakub Kicinski
6365ba64 1118aa4c

+54 -11
+4
drivers/ptp/ptp_kvm_arm.c
··· 22 22 return 0; 23 23 } 24 24 25 + void kvm_arch_ptp_exit(void) 26 + { 27 + } 28 + 25 29 int kvm_arch_ptp_get_clock(struct timespec64 *ts) 26 30 { 27 31 return kvm_arch_ptp_get_crosststamp(NULL, ts, NULL);
+1
drivers/ptp/ptp_kvm_common.c
··· 130 130 static void __exit ptp_kvm_exit(void) 131 131 { 132 132 ptp_clock_unregister(kvm_ptp_clock.ptp_clock); 133 + kvm_arch_ptp_exit(); 133 134 } 134 135 135 136 static int __init ptp_kvm_init(void)
+48 -11
drivers/ptp/ptp_kvm_x86.c
··· 14 14 #include <uapi/linux/kvm_para.h> 15 15 #include <linux/ptp_clock_kernel.h> 16 16 #include <linux/ptp_kvm.h> 17 + #include <linux/set_memory.h> 17 18 18 19 static phys_addr_t clock_pair_gpa; 19 - static struct kvm_clock_pairing clock_pair; 20 + static struct kvm_clock_pairing clock_pair_glbl; 21 + static struct kvm_clock_pairing *clock_pair; 20 22 21 23 int kvm_arch_ptp_init(void) 22 24 { 25 + struct page *p; 23 26 long ret; 24 27 25 28 if (!kvm_para_available()) 26 29 return -ENODEV; 27 30 28 - clock_pair_gpa = slow_virt_to_phys(&clock_pair); 29 - if (!pvclock_get_pvti_cpu0_va()) 30 - return -ENODEV; 31 + if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) { 32 + p = alloc_page(GFP_KERNEL | __GFP_ZERO); 33 + if (!p) 34 + return -ENOMEM; 35 + 36 + clock_pair = page_address(p); 37 + ret = set_memory_decrypted((unsigned long)clock_pair, 1); 38 + if (ret) { 39 + __free_page(p); 40 + clock_pair = NULL; 41 + goto nofree; 42 + } 43 + } else { 44 + clock_pair = &clock_pair_glbl; 45 + } 46 + 47 + clock_pair_gpa = slow_virt_to_phys(clock_pair); 48 + if (!pvclock_get_pvti_cpu0_va()) { 49 + ret = -ENODEV; 50 + goto err; 51 + } 31 52 32 53 ret = kvm_hypercall2(KVM_HC_CLOCK_PAIRING, clock_pair_gpa, 33 54 KVM_CLOCK_PAIRING_WALLCLOCK); 34 - if (ret == -KVM_ENOSYS) 35 - return -ENODEV; 55 + if (ret == -KVM_ENOSYS) { 56 + ret = -ENODEV; 57 + goto err; 58 + } 36 59 37 60 return ret; 61 + 62 + err: 63 + kvm_arch_ptp_exit(); 64 + nofree: 65 + return ret; 66 + } 67 + 68 + void kvm_arch_ptp_exit(void) 69 + { 70 + if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) { 71 + WARN_ON(set_memory_encrypted((unsigned long)clock_pair, 1)); 72 + free_page((unsigned long)clock_pair); 73 + clock_pair = NULL; 74 + } 38 75 } 39 76 40 77 int kvm_arch_ptp_get_clock(struct timespec64 *ts) ··· 86 49 return -EOPNOTSUPP; 87 50 } 88 51 89 - ts->tv_sec = clock_pair.sec; 90 - ts->tv_nsec = clock_pair.nsec; 52 + ts->tv_sec = clock_pair->sec; 53 + ts->tv_nsec = clock_pair->nsec; 91 54 92 55 return 0; 93 56 } ··· 118 81 pr_err_ratelimited("clock pairing hypercall ret %lu\n", ret); 119 82 return -EOPNOTSUPP; 120 83 } 121 - tspec->tv_sec = clock_pair.sec; 122 - tspec->tv_nsec = clock_pair.nsec; 123 - *cycle = __pvclock_read_cycles(src, clock_pair.tsc); 84 + tspec->tv_sec = clock_pair->sec; 85 + tspec->tv_nsec = clock_pair->nsec; 86 + *cycle = __pvclock_read_cycles(src, clock_pair->tsc); 124 87 } while (pvclock_read_retry(src, version)); 125 88 126 89 *cs = &kvm_clock;
+1
include/linux/ptp_kvm.h
··· 14 14 struct clocksource; 15 15 16 16 int kvm_arch_ptp_init(void); 17 + void kvm_arch_ptp_exit(void); 17 18 int kvm_arch_ptp_get_clock(struct timespec64 *ts); 18 19 int kvm_arch_ptp_get_crosststamp(u64 *cycle, 19 20 struct timespec64 *tspec, struct clocksource **cs);