From patchwork Tue Dec 12 20:46:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sagi Shahar X-Patchwork-Id: 177560 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7993349vqy; Tue, 12 Dec 2023 12:47:43 -0800 (PST) X-Google-Smtp-Source: AGHT+IEdTWLVMfzJdZ96IRPqNgGX6zrddpotYeWzj1iO7VCkC6Zkuf9vZt9Ekck73d1AH61QmD6C X-Received: by 2002:a17:90b:3708:b0:28a:cb6d:9291 with SMTP id mg8-20020a17090b370800b0028acb6d9291mr846203pjb.49.1702414063445; Tue, 12 Dec 2023 12:47:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702414063; cv=none; d=google.com; s=arc-20160816; b=UPOjFIRpwOy1lBJQGxssPwhAj1CX9GB9eXG631E1A1gWW/A6PlxSxsCiV7jNY6fxkF 0kFCT+7LUgvrz2pNvDo7SST1UBGssdyVdd8XgqWsDBLGRbbCgyMoe7JzkukvFmlxr6Cg yx5FNG99CfLClbbVdbJt9KMFWgTspi30T7Rz6PdDLjyKarMy+uQALz9lKbjKASGC6p+d nywo5qKtrcWj2+Gqywh6iMp3nmVGfEfIW8ooHscAfH65PzAK9IyOqr2WgLVdtalS5R2U 7aHG3qLlb74+vDg3qWyc+jdiJyfk6eANfRTzOBnzZNBobGOpgl5UlgB+sZD74M7sRr9T 7z0w== 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=vZ+55M1H0qZo6X91hhLwa23PvdZhvqeKwQZ30mHQwEs=; fh=Y5ua58y+ybdOPwRkMynUdu+Hv1LrozBumh8skcuOhBI=; b=YtMIfcFz9mufi9vqPMbQv7YuBxKAxYLtYPmUvDE4baoIQTrxpp8q/YK9rb92qXqcxS 7PaMs1S5csVI8l2qGWLThAQjy7kTHWN0ddX/omNGwOn33PfnnMN6oMoOii+OWHgaAHCX pT7aoo/a2D3MIP4f0sRpq5khAiMokFw/XFjgPgAG76YqIWyFBBIzao3RiHBX84kEujiP TN7zYTMIoIeP9Z4qs3sw5m3BBBZ+2GOmIRHO6bTtX5T8tCvt5vu07dqwy4eExaXU8/EV WSFkKSqR82LNDuflG4Tlhp+EpihopAzFaGK4It5heKCu3C4WP2Q4gXmJSo2kAdCizAnz SSYA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=sgwQFDBe; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 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 fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id gx4-20020a17090b124400b00280664dd1f4si8278379pjb.9.2023.12.12.12.47.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 12:47:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=sgwQFDBe; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 9FAE980A80D2; Tue, 12 Dec 2023 12:47:35 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377254AbjLLUrT (ORCPT + 99 others); Tue, 12 Dec 2023 15:47:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54842 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233106AbjLLUrN (ORCPT ); Tue, 12 Dec 2023 15:47:13 -0500 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BA2F7110 for ; Tue, 12 Dec 2023 12:47:10 -0800 (PST) Received: by mail-pl1-x649.google.com with SMTP id d9443c01a7336-1d053953954so30441405ad.2 for ; Tue, 12 Dec 2023 12:47:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702414030; x=1703018830; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=vZ+55M1H0qZo6X91hhLwa23PvdZhvqeKwQZ30mHQwEs=; b=sgwQFDBeO7D7AOiCS9F/WPcG2kU+yP66lxhitL1QjGb4ri/z+W1c8FvGKsgJkBynq1 FBKU+vs04Gzf58dwetyTpmzBQWjqsO3MJP7Y9DKvYg5YZQPI4iQtbcUHqM5rIlvP2DVq JqPjBgmfH3D8OrPDfYrQ4gikRWEk8i1iXqWQa87M0Y7UvbbLHfqYyPMsJEBX9PVq6fgz VJzNLR8X522WaMZZ6JyaByDmehRh1vjH1Mz+j99ljatqIpy3NAZux2L374ov1jqyFfhj P43e5kD6u3SOYTZspRW3jHEG8OQwE92TlSSmlryy21XrEP2of0WP28TDdFR5iOHDSdjv bjjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702414030; x=1703018830; 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=vZ+55M1H0qZo6X91hhLwa23PvdZhvqeKwQZ30mHQwEs=; b=dfA/maT8wHxkiBVXR3Qi4QFFvC7hKg9pM9MmgTUFVTl9DCe20O0HOt3NkVeJZnvF5X AXOhInNGf5wfp6CdcIdcTLOamh/P87EJuBL+dtqTjfI9aOSdG/R5SXe7NkAq2i/nvifL DyZg7fFK6j5OZzn04VI2tea8yE5lXpTsHlpQzDv8E8iSNvjtT/oAtn08FYJq3S1mC9vq 40d+W5W5KAktp9j3br484aw/cXG6Zer55TyoLI86CSeJOO+6b0rh94VRu+/4EEAjwh+D gF9DSvIMCww34lj4LbDZFwMB+7t69KVM2pZ2lzMCgtkmS2w39GFKfpMzM7QiPAskl3ej EplA== X-Gm-Message-State: AOJu0Yx+bxN0lp5jK4CJWO0XEGR/DHQfCmp89x7uf6W1BcenSWvH9HtR 7baBAuMsS2eOEJ+kaQ3iJvGpAjKv/g== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a17:902:e84a:b0:1d0:c738:73ad with SMTP id t10-20020a170902e84a00b001d0c73873admr49342plg.7.1702414029871; Tue, 12 Dec 2023 12:47:09 -0800 (PST) Date: Tue, 12 Dec 2023 12:46:22 -0800 In-Reply-To: <20231212204647.2170650-1-sagis@google.com> Mime-Version: 1.0 References: <20231212204647.2170650-1-sagis@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231212204647.2170650-8-sagis@google.com> Subject: [RFC PATCH v5 07/29] KVM: selftests: TDX: Update load_td_memory_region for VM memory backed by guest memfd From: Sagi Shahar To: linux-kselftest@vger.kernel.org, Ackerley Tng , Ryan Afranji , Erdem Aktas , Sagi Shahar , Isaku Yamahata Cc: Sean Christopherson , Paolo Bonzini , Shuah Khan , Peter Gonda , Haibo Xu , Chao Peng , Vishal Annapurve , Roger Wang , Vipin Sharma , jmattson@google.com, dmatlack@google.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, 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 fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Tue, 12 Dec 2023 12:47:35 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785110528895638980 X-GMAIL-MSGID: 1785110528895638980 From: Ackerley Tng 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 Signed-off-by: Ryan Afranji Signed-off-by: Sagi Shahar --- .../selftests/kvm/lib/x86_64/tdx/tdx_util.c | 41 +++++++++++++++---- 1 file changed, 32 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 6b995c3f6153..063ff486fb86 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 @@ -192,6 +192,21 @@ 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) +{ + vm_set_memory_attributes(vm, region->region.guest_phys_addr, + region->region.memory_size, + KVM_MEMORY_ATTRIBUTE_PRIVATE); +} + /* * TD creation/setup/finalization */ @@ -376,30 +391,38 @@ static void load_td_memory_region(struct kvm_vm *vm, if (!sparsebit_any_set(pages)) return; + + if (region->region.guest_memfd != -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.guest_memfd == -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.guest_memfd == -1) + munmap(source_addr, size_to_load); } }