From patchwork Mon Aug 7 23:01:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 132467 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp1801218vqr; Mon, 7 Aug 2023 17:55:08 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF+vfFT2pXfE5XI/CZJNNIpoHKgBfNLLdMUXKkt+u1+nWm8VgnuS9Yw0Xj798ml0fMl4CP6 X-Received: by 2002:a05:6358:99a8:b0:134:61a5:7f05 with SMTP id j40-20020a05635899a800b0013461a57f05mr12119328rwb.10.1691456108045; Mon, 07 Aug 2023 17:55:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691456107; cv=none; d=google.com; s=arc-20160816; b=HFZqvowWtwsxs6W5TxJej1eg+nu2GOfiBzks+tFtFrGhs8fzzIBCn2hLO/j1+BQeyG d/vlGY+ulWgUvIwfwrtiRLW11mm+OMrau4toJOmMcMphom/GCjMfcDoCXfv5yWiOYtue 3wwNufK6v9SNhxZpNsqvd6929Et9XF7bArX+nB0znZL646TkEzEwuLaLTp6hkdWoeZ06 u7oaCXGrJvUNqy+7tcsZ8PGbeHP585/DgN9UsiKe1wtnXfO7bJjA+fbpv3owE3f8gBJR ZzLKFDJiwum+Y8/hnjYwj5Kz5c+i7K+riGkpzGs4PKT89akjN5RxX2XIt1JtKGGbnhgS 5cXQ== 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=/9gOMf8CCEdwWA0iNCqRkk5ADeV+gCZbuh7Q87rmQao=; fh=HbRmZS+XZk9IdI9bC1USVLouzBEe1WLRItFSyknhPLs=; b=rEPI/na1pEQcsrLKN6dSdQdtWK31sElqdG9l5qb6i0eeMrUI89Yz2keb6Zwb+El8lU J5BJo2Ja2cnpnTAuy9wzCLoB7kzjscXxaBDhn5Az3hIgZcvFnT9k4duqkOK0Ub016r1f Mry0WjhFZdYIcnLYfr0pqEM7wqeQm4UI768XzBu4xW5iXlpjsOPAyR1YXAnrKPbgsrT9 oG1nvPNpI1Tp/GFgSoJXu0SUwo+620Ztu9zgIc3U5oq1LD7X+wcflo6SZni7Q10JNKXq DPIOOfKnLMmNOpjjNFEp4Y5BVoIkEI596QdinC3QJ4o/d5UqAuFlWoNwM/awEBumxTWs cyDQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=rVjslmt7; 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 j186-20020a638bc3000000b005634a72454asi6220288pge.37.2023.08.07.17.54.54; Mon, 07 Aug 2023 17:55:07 -0700 (PDT) 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=20221208 header.b=rVjslmt7; 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 S231445AbjHGXCl (ORCPT + 99 others); Mon, 7 Aug 2023 19:02:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41256 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231438AbjHGXCP (ORCPT ); Mon, 7 Aug 2023 19:02:15 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6C4542102 for ; Mon, 7 Aug 2023 16:01:51 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5865afcb825so59678967b3.0 for ; Mon, 07 Aug 2023 16:01:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1691449310; x=1692054110; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=/9gOMf8CCEdwWA0iNCqRkk5ADeV+gCZbuh7Q87rmQao=; b=rVjslmt7PSMAvIXx1/0lnOTs1uidu2pd6MwAtjXg1BJCgGiswAFxbp+++vbV00nz1C mP5FjKvxw3fVLnrTV9OEgaDRwI1UhHdFG7sOpwC2Z1Quk+yHZLS5jsXGTh1LxOTFcsl1 +ZbYET4l4TyZbPdW3Kzr+L2Uqg1/oB9OZfxr3g5sjZEdoxesh/XIQg4+s7XBqtfjO4r1 JhtWPyqglk0oIjBk3RcMgWyniTAKu+Ln1v/qPXePbFdqPwSuMShil7b6DcdfXZYAL/KU 6YtHnfdC1+brh7t3JX4jhFM3n7enc6fRK02z5AJ4UKsvLX6ABMqW/j5nw73bgssBZv5v refg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691449310; x=1692054110; 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=/9gOMf8CCEdwWA0iNCqRkk5ADeV+gCZbuh7Q87rmQao=; b=GrMs6octBDiI0yS90f2QXhtr9gCV1m2190LXDYOFnVg/2NB/qjypcd2yCw3WkG4CJo k1ePgVDbjaav0HYVdBMhckj5qh/ihpm2hYl9a3+FNEN3TxZu4UHX3t/PxOnJMkD3i6EW jTY7OvXrP7l3fnUYjNUdC6v3ple/nSjbcF5CGyTjrZwE+tv5WXpMOPrghPUP5UZajRFa R//o7jHIDmcL+a/D3ETrJKIMDA0Td3unLnXenft2YQvrANxwpuKzfaGbig/httdOfEuL mmiTMfdbOmoy1Sg/ft62TWANU+v5p6Q1H3OHwoE17gwieFGoguE6+GEptQ7A+FAS6NKj b+Pg== X-Gm-Message-State: AOJu0YzfjkPOowV5rRpZ4cdnCd6WBJq4fdFWuHnGtpab5QJo2c9liPXo KmjQzTVBrtqc3OItwW1SCJyJz8+4gJEdKGGxiA== X-Received: from ackerleytng-ctop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:13f8]) (user=ackerleytng job=sendgmr) by 2002:a81:ae1b:0:b0:579:f832:74b with SMTP id m27-20020a81ae1b000000b00579f832074bmr107515ywh.10.1691449310023; Mon, 07 Aug 2023 16:01:50 -0700 (PDT) Date: Mon, 7 Aug 2023 23:01:14 +0000 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.41.0.640.ga95def55d0-goog Message-ID: <49b8587dab22fe1c0e31444a27e47246d2615a58.1691446946.git.ackerleytng@google.com> Subject: [RFC PATCH 11/11] KVM: selftests: Add tests for migration of private mem From: Ackerley Tng To: pbonzini@redhat.com, seanjc@google.com, tglx@linutronix.de, x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, hpa@zytor.com, shuah@kernel.org, andrew.jones@linux.dev, ricarkol@google.com, chao.p.peng@linux.intel.com, tabba@google.com, jarkko@kernel.org, yu.c.zhang@linux.intel.com, vannapurve@google.com, ackerleytng@google.com, erdemaktas@google.com, mail@maciej.szmigiero.name, vbabka@suse.cz, david@redhat.com, qperret@google.com, michael.roth@amd.com, wei.w.wang@intel.com, liam.merwick@oracle.com, isaku.yamahata@gmail.com, kirill.shutemov@linux.intel.com 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_BLOCKED,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: INBOX X-GMAIL-THRID: 1773620279772495756 X-GMAIL-MSGID: 1773620279772495756 Tests that private mem (in guest_mem files) can be migrated. Also demonstrates the migration flow. Signed-off-by: Ackerley Tng --- .../kvm/x86_64/private_mem_migrate_tests.c | 54 ++++++++++--------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/private_mem_migrate_tests.c b/tools/testing/selftests/kvm/x86_64/private_mem_migrate_tests.c index 4226de3ebd41..2691497cf207 100644 --- a/tools/testing/selftests/kvm/x86_64/private_mem_migrate_tests.c +++ b/tools/testing/selftests/kvm/x86_64/private_mem_migrate_tests.c @@ -5,28 +5,28 @@ #include #include -#define TRANSFER_PRIVATE_MEM_TEST_SLOT 10 -#define TRANSFER_PRIVATE_MEM_GPA ((uint64_t)(1ull << 32)) -#define TRANSFER_PRIVATE_MEM_GVA TRANSFER_PRIVATE_MEM_GPA -#define TRANSFER_PRIVATE_MEM_VALUE 0xdeadbeef +#define MIGRATE_PRIVATE_MEM_TEST_SLOT 10 +#define MIGRATE_PRIVATE_MEM_GPA ((uint64_t)(1ull << 32)) +#define MIGRATE_PRIVATE_MEM_GVA MIGRATE_PRIVATE_MEM_GPA +#define MIGRATE_PRIVATE_MEM_VALUE 0xdeadbeef -static void transfer_private_mem_guest_code_src(void) +static void migrate_private_mem_data_guest_code_src(void) { - uint64_t volatile *const ptr = (uint64_t *)TRANSFER_PRIVATE_MEM_GVA; + uint64_t volatile *const ptr = (uint64_t *)MIGRATE_PRIVATE_MEM_GVA; - *ptr = TRANSFER_PRIVATE_MEM_VALUE; + *ptr = MIGRATE_PRIVATE_MEM_VALUE; GUEST_SYNC1(*ptr); } -static void transfer_private_mem_guest_code_dst(void) +static void migrate_private_mem_guest_code_dst(void) { - uint64_t volatile *const ptr = (uint64_t *)TRANSFER_PRIVATE_MEM_GVA; + uint64_t volatile *const ptr = (uint64_t *)MIGRATE_PRIVATE_MEM_GVA; GUEST_SYNC1(*ptr); } -static void test_transfer_private_mem(void) +static void test_migrate_private_mem_data(bool migrate) { struct kvm_vm *src_vm, *dst_vm; struct kvm_vcpu *src_vcpu, *dst_vcpu; @@ -40,40 +40,43 @@ static void test_transfer_private_mem(void) /* Build the source VM, use it to write to private memory */ src_vm = __vm_create_shape_with_one_vcpu( - shape, &src_vcpu, 0, transfer_private_mem_guest_code_src); + shape, &src_vcpu, 0, migrate_private_mem_data_guest_code_src); src_memfd = vm_create_guest_memfd(src_vm, SZ_4K, 0); - vm_mem_add(src_vm, DEFAULT_VM_MEM_SRC, TRANSFER_PRIVATE_MEM_GPA, - TRANSFER_PRIVATE_MEM_TEST_SLOT, 1, KVM_MEM_PRIVATE, + vm_mem_add(src_vm, DEFAULT_VM_MEM_SRC, MIGRATE_PRIVATE_MEM_GPA, + MIGRATE_PRIVATE_MEM_TEST_SLOT, 1, KVM_MEM_PRIVATE, src_memfd, 0); - virt_map(src_vm, TRANSFER_PRIVATE_MEM_GVA, TRANSFER_PRIVATE_MEM_GPA, 1); - vm_set_memory_attributes(src_vm, TRANSFER_PRIVATE_MEM_GPA, SZ_4K, + virt_map(src_vm, MIGRATE_PRIVATE_MEM_GVA, MIGRATE_PRIVATE_MEM_GPA, 1); + vm_set_memory_attributes(src_vm, MIGRATE_PRIVATE_MEM_GPA, SZ_4K, KVM_MEMORY_ATTRIBUTE_PRIVATE); vcpu_run(src_vcpu); TEST_ASSERT_KVM_EXIT_REASON(src_vcpu, KVM_EXIT_IO); get_ucall(src_vcpu, &uc); - TEST_ASSERT(uc.args[0] == TRANSFER_PRIVATE_MEM_VALUE, + TEST_ASSERT(uc.args[0] == MIGRATE_PRIVATE_MEM_VALUE, "Source VM should be able to write to private memory"); /* Build the destination VM with linked fd */ dst_vm = __vm_create_shape_with_one_vcpu( - shape, &dst_vcpu, 0, transfer_private_mem_guest_code_dst); + shape, &dst_vcpu, 0, migrate_private_mem_guest_code_dst); dst_memfd = vm_link_guest_memfd(dst_vm, src_memfd, 0); - vm_mem_add(dst_vm, DEFAULT_VM_MEM_SRC, TRANSFER_PRIVATE_MEM_GPA, - TRANSFER_PRIVATE_MEM_TEST_SLOT, 1, KVM_MEM_PRIVATE, + vm_mem_add(dst_vm, DEFAULT_VM_MEM_SRC, MIGRATE_PRIVATE_MEM_GPA, + MIGRATE_PRIVATE_MEM_TEST_SLOT, 1, KVM_MEM_PRIVATE, dst_memfd, 0); - virt_map(dst_vm, TRANSFER_PRIVATE_MEM_GVA, TRANSFER_PRIVATE_MEM_GPA, 1); - vm_set_memory_attributes(dst_vm, TRANSFER_PRIVATE_MEM_GPA, SZ_4K, - KVM_MEMORY_ATTRIBUTE_PRIVATE); + virt_map(dst_vm, MIGRATE_PRIVATE_MEM_GVA, MIGRATE_PRIVATE_MEM_GPA, 1); + if (migrate) + vm_migrate_from(dst_vm, src_vm); + else + vm_set_memory_attributes(dst_vm, MIGRATE_PRIVATE_MEM_GPA, SZ_4K, + KVM_MEMORY_ATTRIBUTE_PRIVATE); vcpu_run(dst_vcpu); TEST_ASSERT_KVM_EXIT_REASON(dst_vcpu, KVM_EXIT_IO); get_ucall(dst_vcpu, &uc); - TEST_ASSERT(uc.args[0] == TRANSFER_PRIVATE_MEM_VALUE, + TEST_ASSERT(uc.args[0] == MIGRATE_PRIVATE_MEM_VALUE, "Destination VM should be able to read value transferred"); } @@ -81,7 +84,10 @@ int main(int argc, char *argv[]) { TEST_REQUIRE(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SW_PROTECTED_VM)); - test_transfer_private_mem(); + test_migrate_private_mem_data(false); + + if (kvm_check_cap(KVM_CAP_VM_MOVE_ENC_CONTEXT_FROM)) + test_migrate_private_mem_data(true); return 0; }