From patchwork Wed Jan 10 06:22:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zheyun Shen X-Patchwork-Id: 13515669 Received: from smtp238.sjtu.edu.cn (smtp238.sjtu.edu.cn [202.120.2.238]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 631C838DE8 for ; Wed, 10 Jan 2024 06:27:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=sjtu.edu.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sjtu.edu.cn Received: from mta90.sjtu.edu.cn (unknown [10.118.0.90]) by smtp238.sjtu.edu.cn (Postfix) with ESMTPS id DC5B98B23; Wed, 10 Jan 2024 14:22:11 +0800 (CST) Received: from mstore135.sjtu.edu.cn (unknown [10.118.0.135]) by mta90.sjtu.edu.cn (Postfix) with ESMTP id B4AD837C878; Wed, 10 Jan 2024 14:22:11 +0800 (CST) Date: Wed, 10 Jan 2024 14:22:11 +0800 (CST) From: Zheyun Shen To: qemu-devel@nongnu.org, kvm@vger.kernel.org Cc: pbonzini@redhat.com, mtosatti@redhat.com Message-ID: <982162224.1083330.1704867731391.JavaMail.zimbra@sjtu.edu.cn> Subject: [PATCH] target/i386/sev: Add an option to allow SEV not to pin memory Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Mailer: Zimbra 10.0.5_GA_4574 (ZimbraWebClient - GC120 (Win)/10.0.5_GA_4574) Thread-Index: 2z/N+LsUvJeJl+Li7PEkNfi8agF9BA== Thread-Topic: target/i386/sev: Add an option to allow SEV not to pin memory For now, SEV pins guest's memory to avoid swapping or moving ciphertext, but leading to the inhibition of Memory Ballooning. In Memory Ballooning, only guest's free pages will be relocated in balloon inflation and deflation, so the difference of plaintext doesn't matter to guest. Memory Ballooning is a nice memory overcommitment technology can be used in CVM based on SEV and SEV-ES, so userspace tools can provide an option to allow SEV not to pin memory. Signed-off-by: Zheyun Shen --- qapi/qom.json | 7 ++++++- qemu-options.hx | 5 ++++- target/i386/sev.c | 39 ++++++++++++++++++++++++++++++++++----- 3 files changed, 44 insertions(+), 7 deletions(-) -- 2.34.1 diff --git a/qapi/qom.json b/qapi/qom.json index 95516ba..c23397c 100644 --- a/qapi/qom.json +++ b/qapi/qom.json @@ -882,6 +882,10 @@ # @kernel-hashes: if true, add hashes of kernel/initrd/cmdline to a # designated guest firmware page for measured boot with -kernel # (default: false) (since 6.2) + +# @pin-memory: if true, sev initialization will pin guest's +# memory by registering to kvm, and disable ram block discard. +# (default: true) # # Since: 2.12 ## @@ -893,7 +897,8 @@ '*handle': 'uint32', '*cbitpos': 'uint32', 'reduced-phys-bits': 'uint32', - '*kernel-hashes': 'bool' } } + '*kernel-hashes': 'bool', + '*pin-memory': 'bool' } } ## # @ThreadContextProperties: diff --git a/qemu-options.hx b/qemu-options.hx index b66570a..1add214 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -5668,7 +5668,7 @@ SRST -object secret,id=sec0,keyid=secmaster0,format=base64,\\ data=$SECRET,iv=$(pin_memory) { + return; + } int r; struct kvm_enc_region range; ram_addr_t offset; @@ -256,6 +260,9 @@ static void sev_ram_block_removed(RAMBlockNotifier *n, void *host, size_t size, size_t max_size) { + if (!sev_guest->pin_memory) { + return; + } int r; struct kvm_enc_region range; ram_addr_t offset; @@ -353,6 +360,20 @@ static void sev_guest_set_kernel_hashes(Object *obj, bool value, Error **errp) sev->kernel_hashes = value; } +static bool sev_guest_get_pin_memory(Object *obj, Error **errp) +{ + SevGuestState *sev = SEV_GUEST(obj); + + return sev->pin_memory; +} + +static void sev_guest_set_pin_memory(Object *obj, bool value, Error **errp) +{ + SevGuestState *sev = SEV_GUEST(obj); + + sev->pin_memory = value; +} + static void sev_guest_class_init(ObjectClass *oc, void *data) { @@ -376,6 +397,11 @@ sev_guest_class_init(ObjectClass *oc, void *data) sev_guest_set_kernel_hashes); object_class_property_set_description(oc, "kernel-hashes", "add kernel hashes to guest firmware for measured Linux boot"); + object_class_property_add_bool(oc, "pin-memory", + sev_guest_get_pin_memory, + sev_guest_set_pin_memory); + object_class_property_set_description(oc, "pin-memory", + "pin guest memory at initialization"); } static void @@ -383,6 +409,7 @@ sev_guest_instance_init(Object *obj) { SevGuestState *sev = SEV_GUEST(obj); + sev->pin_memory = true; sev->sev_device = g_strdup(DEFAULT_SEV_DEVICE); sev->policy = DEFAULT_GUEST_POLICY; object_property_add_uint32_ptr(obj, "policy", &sev->policy, @@ -920,11 +947,13 @@ int sev_kvm_init(ConfidentialGuestSupport *cgs, Error **errp) return 0; } - ret = ram_block_discard_disable(true); - if (ret) { - error_report("%s: cannot disable RAM discard", __func__); - return -1; - } + if (sev->pin_memory) { + ret = ram_block_discard_disable(true); + if (ret) { + error_report("%s: cannot disable RAM discard", __func__); + return -1; + } + } sev_guest = sev; sev->state = SEV_STATE_UNINIT;