From patchwork Sat Jan 21 00:15:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 46731 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp497270wrn; Fri, 20 Jan 2023 16:20:51 -0800 (PST) X-Google-Smtp-Source: AMrXdXuxi0NLDHFU3oL99lvzTo9NYlYZq5cyo0p7lEBod0I5B35XIBrNXC2VZcHQdoDyY/4JOXI2 X-Received: by 2002:a05:6402:3986:b0:49d:5c6:3e5f with SMTP id fk6-20020a056402398600b0049d05c63e5fmr20248129edb.41.1674260451759; Fri, 20 Jan 2023 16:20:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674260451; cv=none; d=google.com; s=arc-20160816; b=m36IjTASBgcdWdSpOku2Vh+hmpRmbjXjYYgcn++6kRCiH3gK2GaOHt0Sx/rR9FcNXa +n3ThWwuMEcH8DnrQUgPzmhVli4t74Bh9fbbD67OcaWprBv2FyvN1ltuoWcY3zHfdwIB YI9kHPG8y94xjdxCvqAEH5iSjzKb17AvZ4HmXW2uvKjMXLK7E8XGeSWXe1a+oxYl3wUN UWduni39H7IyGWYY6YFPvGU6pOAaD1duoCX05/MCvxsvQh/7W4xc7GOwnIALnneIJ1Ip lPYd7PohJCvzJULjiJ69zi7ofjfy0DfHJX0UhodADv1tIQf7yScCPdUmXbaoppL17cwH KySw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=CoXS9NR8J0lCDisdrTAQ77xqlurQK2TuY0Fdg8pUOxU=; b=oV6Vko1uVZyV14qWvpeZLuZLuQw2JN0mnQ/tBddqi1smikI2WUiV8Ij+Aa1eqkq/Cs t7CSgRUwl9T7lQXqSE/e9hqvcOu/ogCPTFZPqzV90d/9P6MH+mToUr0KuZAsRdybHUp7 Jm8X3v670+WBKbo1+KVC2fY9xyoXxeUM/gr00XxEo6lrbZ0psaxiNCRRsa30eRIHDjz7 6iT/vbnLSUiLicpAAJBYZpu4+VdGiM+jhGXlPMYPC/zvnKg6TBwIitgJY44aH5GVqGyc lxkKyyDDFhMGD8ZMOlO7IljFP6gCrmcKY5poOpUNBHsvYklIsXcLHKgaJYfQDytRi00O uyCg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=E5VrJv2H; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i10-20020a05640242ca00b0049e160ce989si25102722edc.306.2023.01.20.16.20.27; Fri, 20 Jan 2023 16:20:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=E5VrJv2H; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230224AbjAUAT5 (ORCPT + 99 others); Fri, 20 Jan 2023 19:19:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56700 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230218AbjAUATy (ORCPT ); Fri, 20 Jan 2023 19:19:54 -0500 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0B8FF73152 for ; Fri, 20 Jan 2023 16:19:23 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-4c11ae6ab25so65351727b3.8 for ; Fri, 20 Jan 2023 16:19:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=CoXS9NR8J0lCDisdrTAQ77xqlurQK2TuY0Fdg8pUOxU=; b=E5VrJv2Hh0RmxBs1CPSwmu8xgt4qiqRciYcrXNA3andiehTlbQBzyXo+3IpgYQcFos uV4sCqmIxagqP/MnjPBDi0bAGuK0MDw0xKibN0uIeISt3SPn19Y3cYm9HWjtaq9KSG+6 Os2YgJr/XOBiIvcyGQ5QQ00hTP3uTT3ZZpoxL/UMb1wrjKZ9NrenmHZ+OXWIH9G8GEgq bLxNJVQgtdLYNyR7MrtyYZHSTpt4Is23wEhQbbtNNhI3suLr17GaEy3TFlHDk9jwtSi+ hDwvwsuG+iSlsogJYU+6ILeI+CiHuZQzrMVPV9gNi7X0emnR2Hy6GdmvLbpmMFUJz/k8 BTVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=CoXS9NR8J0lCDisdrTAQ77xqlurQK2TuY0Fdg8pUOxU=; b=nBkgpK1+b/bBX1xQ1LVf6JSgMvWTRbSRKWJhbcAkE0CDMniHQe2PAwSLV59UyYIuZU YCaNjwbeqEKUXCtNi/tbZqgkZaJwij7pFk6AQpPs28pR31D7chqUmzD+BlqU7tNWlM7s uKJcp67Tz0DnkKR6kGQpPFiQfhnuzBF8cN8mw/qmoImJO0ofMu5OUVnin/14PDBgxCPw Vx5ipVY+h6KrgwtbIJeOVjSEnEl6zMD8VGkCsAiUD99orS1u9hJ4QxE2b9iF4f0HWyjY +RHxpQ2Ls8RXLP2KSe7RtCYQ5XniEIjEDDENRgDDm5Cb78aBvFrl5xsU5PG9Wq1h1dNi DdJg== X-Gm-Message-State: AFqh2koYZJf7+jxrf8NOEdF8J64GVCIZsxrgTAs7LGgldYRe18Z4ROHd addU/WcEJhbhm9FiL5QkHzVWYDSqubJOEA0klg== X-Received: from ackerleytng-cloudtop-sg.c.googlers.com ([fda3:e722:ac3:cc00:4f:4b78:c0a8:b30]) (user=ackerleytng job=sendgmr) by 2002:a81:8943:0:b0:46f:daaf:f81f with SMTP id z64-20020a818943000000b0046fdaaff81fmr2058115ywf.161.1674260285954; Fri, 20 Jan 2023 16:18:05 -0800 (PST) Date: Sat, 21 Jan 2023 00:15:37 +0000 In-Reply-To: <20230121001542.2472357-1-ackerleytng@google.com> Mime-Version: 1.0 References: <20230121001542.2472357-1-ackerleytng@google.com> X-Mailer: git-send-email 2.39.0.246.g2a6d74b583-goog Message-ID: <20230121001542.2472357-27-ackerleytng@google.com> Subject: [RFC PATCH v3 26/31] KVM: selftests: TDX: Update load_td_memory_region for VM memory backed by restricted memfd From: Ackerley Tng To: linux-kselftest@vger.kernel.org Cc: pbonzini@redhat.com, seanjc@google.com, isaku.yamahata@intel.com, sagis@google.com, erdemaktas@google.com, afranji@google.com, runanwang@google.com, shuah@kernel.org, drjones@redhat.com, maz@kernel.org, bgardon@google.com, jmattson@google.com, dmatlack@google.com, peterx@redhat.com, oupton@google.com, ricarkol@google.com, yang.zhong@intel.com, wei.w.wang@intel.com, xiaoyao.li@intel.com, pgonda@google.com, marcorr@google.com, eesposit@redhat.com, borntraeger@de.ibm.com, eric.auger@redhat.com, wangyanan55@huawei.com, aaronlewis@google.com, vkuznets@redhat.com, pshier@google.com, axelrasmussen@google.com, zhenzhong.duan@intel.com, maciej.szmigiero@oracle.com, like.xu@linux.intel.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Ackerley Tng X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1755589327244345599?= X-GMAIL-MSGID: =?utf-8?q?1755589327244345599?= If guest memory is backed by restricted memfd + UPM is being used, hence encrypted memory region has to be registered + Can avoid making a copy of guest memory before getting TDX to initialize the memory region Signed-off-by: Ackerley Tng --- .../selftests/kvm/lib/x86_64/tdx/tdx_util.c | 43 +++++++++++++++---- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx_util.c b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx_util.c index 4d6615b97770a..77803b7a1d739 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx_util.c +++ b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx_util.c @@ -207,6 +207,23 @@ static void tdx_td_finalizemr(struct kvm_vm *vm) tdx_ioctl(vm->fd, KVM_TDX_FINALIZE_VM, 0, NULL); } +/* + * Other ioctls + */ + +/** + * Register a memory region that may contain encrypted data in KVM. + */ +static void register_encrypted_memory_region( + struct kvm_vm *vm, struct userspace_mem_region *region) +{ + struct kvm_enc_region range = { + .addr = region->region.guest_phys_addr, + .size = region->region.memory_size, + }; + vm_ioctl(vm, KVM_MEMORY_ENCRYPT_REG_REGION, &range); +} + /* * TD creation/setup/finalization */ @@ -393,30 +410,38 @@ static void load_td_memory_region(struct kvm_vm *vm, if (!sparsebit_any_set(pages)) return; + + if (region->region_ext.restricted_fd != -1) + register_encrypted_memory_region(vm, region); + sparsebit_for_each_set_range(pages, i, j) { const uint64_t size_to_load = (j - i + 1) * vm->page_size; const uint64_t offset = (i - lowest_page_in_region) * vm->page_size; const uint64_t hva = hva_base + offset; const uint64_t gpa = gpa_base + offset; - void *source_addr; + void *source_addr = (void *)hva; /* * KVM_TDX_INIT_MEM_REGION ioctl cannot encrypt memory in place, * hence we have to make a copy if there's only one backing * memory source */ - source_addr = mmap(NULL, size_to_load, PROT_READ | PROT_WRITE, - MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); - TEST_ASSERT( - source_addr, - "Could not allocate memory for loading memory region"); - - memcpy(source_addr, (void *)hva, size_to_load); + if (region->region_ext.restricted_fd == -1) { + source_addr = mmap(NULL, size_to_load, PROT_READ | PROT_WRITE, + MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); + TEST_ASSERT( + source_addr, + "Could not allocate memory for loading memory region"); + + memcpy(source_addr, (void *)hva, size_to_load); + memset((void *)hva, 0, size_to_load); + } tdx_init_mem_region(vm, source_addr, gpa, size_to_load); - munmap(source_addr, size_to_load); + if (region->region_ext.restricted_fd == -1) + munmap(source_addr, size_to_load); } }