From patchwork Mon Aug 7 23:01:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 13278 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp1770845vqr; Mon, 7 Aug 2023 16:31:20 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE3QO8g24TNc5W79fRGWolqU0LNaZRH48zq7mAbN8nEpVSvTgs3nKhnL6B1fcd1QTjk+kEz X-Received: by 2002:a05:6512:525:b0:4f3:d682:7b1c with SMTP id o5-20020a056512052500b004f3d6827b1cmr6800567lfc.45.1691451080575; Mon, 07 Aug 2023 16:31:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691451080; cv=none; d=google.com; s=arc-20160816; b=OXY3DpemRlGWHxJxSF+w7jiA4O+rXYuRZtFFd1NO45bdHql0RjVTc3u/Kdkr0IHBob uns9Skqc8Wq9Eor2F1V/YL/GxnaMhBnp6DOfcWXXDzFrrS+tcnGOk2kwbqDaeCG7tV7l dzZWEG5qjQPT2omdg0Zf/lsoyrCHFQIroWOLsI4SxyrjPZyED5A65KlI5L+dS2TLWjqh v1tKAN2UI1dyZ5W6cjGJ+16oUObEDn4kbf8TxOGrS5d73ZV5V5PfNGO+dl2AINUdSPeE tbAa6FBMDVfgbxp0qV2MGrwfVXeNAn8T6QCxYkcLxD7CVgCYfqyukdK1pG2PfLBYHfs5 TJww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:from:subject :message-id:mime-version:date:dkim-signature; bh=ss/71oxFBRdAW2cxcHK7UY62Gn+6jbKx5vE5M9l/SE4=; fh=HbRmZS+XZk9IdI9bC1USVLouzBEe1WLRItFSyknhPLs=; b=WBKyBbfgUgVrIxoXJLL5qyxsLfwgAS1RP/MU6HEe1fnle1DO4s0ni4kjxIOSdRVGU+ v+bkOc5Gvwm5pUcVjSxYXD5fwsBwonqiXbyRn5JgJ1pNyjIoXYXIQ6OpZ8hx0ELGFTAE ChzdCJnOLFO1rIS4Nl1fXJ0SEkIfxB0yu45rlncT2/OhMf0S4QKGM+bOtnAuNfmv4QU8 tZI4W3AGhwowOIYg3o1mjDuUOC20LHJHLVvM7pZNSJCLKrt85rf3E/ztQsNv8qNvXjyA TNw75RDyRXSfM98++sdFisaxWwqge/bdPN6LN0l4OSx9opfsjEV3XuOeuaFaIuczRyxG mJ5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b="RRW/dV5l"; 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 by4-20020a0564021b0400b00518c6e070f8si6475679edb.663.2023.08.07.16.30.57; Mon, 07 Aug 2023 16:31:20 -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="RRW/dV5l"; 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 S230042AbjHGXBe (ORCPT + 99 others); Mon, 7 Aug 2023 19:01:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40312 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229736AbjHGXBa (ORCPT ); Mon, 7 Aug 2023 19:01:30 -0400 Received: from mail-pg1-x549.google.com (mail-pg1-x549.google.com [IPv6:2607:f8b0:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EF07C10F3 for ; Mon, 7 Aug 2023 16:01:28 -0700 (PDT) Received: by mail-pg1-x549.google.com with SMTP id 41be03b00d2f7-564fb0b4934so1657631a12.1 for ; Mon, 07 Aug 2023 16:01:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1691449288; x=1692054088; h=content-transfer-encoding:cc:to:from:subject:message-id :mime-version:date:from:to:cc:subject:date:message-id:reply-to; bh=ss/71oxFBRdAW2cxcHK7UY62Gn+6jbKx5vE5M9l/SE4=; b=RRW/dV5l+QziKsyhbbcF4AZKL0bIjPOef7yRlt2G1D1+ZLFr9EXprdfji5tAeSRYC5 zngWiXG3xOEMy7WXlxUFGNwEymvu7pLx4ta778nDj3+qIL/6TiRrAxyPuFZYfLbvqE8G bEc6SH5czQ/0cxaC6vnWcChbq2/tjkSG4TQySEzm2bZIHJMOZGXwADCmlTTE5PLfX9AB EleRnHqXH42bDshPJF3zoHFdpSLmeWcyfXo2AqrrBduVvAnG6F6rWOsqEibgbvSBxyZu /v+TPBGTJiJYYIVnr0B+h+/1DWqjR832+Skd0FWvENVSFlcsihi9lP8Cc3mzikqU5SoY rsVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691449288; x=1692054088; h=content-transfer-encoding:cc:to:from:subject:message-id :mime-version:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=ss/71oxFBRdAW2cxcHK7UY62Gn+6jbKx5vE5M9l/SE4=; b=eQoURMmbvHtdz1Qm+kpnUGaBa1NAzfaiigaCfHu1VR0Id2IK99ETlPQbZsEnvJdIN0 RSJTjRHzy9Zic71yL3hUeswp4elAWqr/G1B5FnhgJTTf4pAiEAl/EdijnNcx0PBEdV3M fuRFyEkjQvEmj6/7v31dYz7tLiWUkNQXdJDoWFO7eP1njzfHJzpLCTXLnkxW2witzZ42 uVX4bV99JD2Ll1bqYJiQqAV8cM8Dw7FGLucLazDPAzYEgDs+EhkYl8JcExJoBq8jN/27 e0WiF7AZePbzcAC+j8vyfhcgEXlC99T53o3QtafEj+HybvHDWHRZVQSqLgrYeD+aNzFG VDDA== X-Gm-Message-State: AOJu0Yzkac+MeCYcXCYmomO2cN7C9qv5HiLufvT/u3xBWgUIcvVERzRv 2lI7pk5w+TCJ8yhgxX6YK9XMp4bfy0H5o905jQ== X-Received: from ackerleytng-ctop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:13f8]) (user=ackerleytng job=sendgmr) by 2002:a63:7d04:0:b0:563:e826:823b with SMTP id y4-20020a637d04000000b00563e826823bmr47777pgc.6.1691449288296; Mon, 07 Aug 2023 16:01:28 -0700 (PDT) Date: Mon, 7 Aug 2023 23:01:03 +0000 Mime-Version: 1.0 X-Mailer: git-send-email 2.41.0.640.ga95def55d0-goog Message-ID: Subject: =?utf-8?q?=5BRFC_PATCH_00/11=5D_New_KVM_ioctl_to_link_a_gmem=C2=A0i?= =?utf-8?q?node_to_a_new_gmem_file?= 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=ham 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: 1773615008083853880 X-GMAIL-MSGID: 1773615008083853880 Hello, This patchset builds upon the code at https://lore.kernel.org/lkml/20230718234512.1690985-1-seanjc@google.com/T/. This code is available at https://github.com/googleprodkernel/linux-cc/tree/kvm-gmem-link-migrate-rfcv1. In guest_mem v11, a split file/inode model was proposed, where memslot bindings belong to the file and pages belong to the inode. This model lends itself well to having different VMs use separate files pointing to the same inode. This RFC proposes an ioctl, KVM_LINK_GUEST_MEMFD, that takes a VM and a gmem fd, and returns another gmem fd referencing a different file and associated with VM. This RFC also includes an update to KVM_CAP_VM_MOVE_ENC_CONTEXT_FROM to migrate memory context (slot->arch.lpage_info and kvm->mem_attr_array) from source to destination vm, intra-host. Intended usage of the two ioctls: 1. Source VM’s fd is passed to destination VM via unix sockets 2. Destination VM uses new ioctl KVM_LINK_GUEST_MEMFD to link source VM’s fd to a new fd. 3. Destination VM will pass new fds to KVM_SET_USER_MEMORY_REGION, which will bind the new file, pointing to the same inode that the source VM’s file points to, to memslots 4. Use KVM_CAP_VM_MOVE_ENC_CONTEXT_FROM to move kvm->mem_attr_array and slot->arch.lpage_info to the destination VM. 5. Run the destination VM as per normal Some other approaches considered were: + Using the linkat() syscall, but that requires a mount/directory for a source fd to be linked to + Using the dup() syscall, but that only duplicates the fd, and both fds point to the same file --- Ackerley Tng (11): KVM: guest_mem: Refactor out kvm_gmem_alloc_file() KVM: guest_mem: Add ioctl KVM_LINK_GUEST_MEMFD KVM: selftests: Add tests for KVM_LINK_GUEST_MEMFD ioctl KVM: selftests: Test transferring private memory to another VM KVM: x86: Refactor sev's flag migration_in_progress to kvm struct KVM: x86: Refactor common code out of sev.c KVM: x86: Refactor common migration preparation code out of sev_vm_move_enc_context_from KVM: x86: Let moving encryption context be configurable KVM: x86: Handle moving of memory context for intra-host migration KVM: selftests: Generalize migration functions from sev_migrate_tests.c KVM: selftests: Add tests for migration of private mem arch/x86/include/asm/kvm_host.h | 4 +- arch/x86/kvm/svm/sev.c | 85 ++----- arch/x86/kvm/svm/svm.h | 3 +- arch/x86/kvm/x86.c | 221 +++++++++++++++++- arch/x86/kvm/x86.h | 6 + include/linux/kvm_host.h | 18 ++ include/uapi/linux/kvm.h | 8 + tools/testing/selftests/kvm/Makefile | 1 + .../testing/selftests/kvm/guest_memfd_test.c | 42 ++++ .../selftests/kvm/include/kvm_util_base.h | 31 +++ .../kvm/x86_64/private_mem_migrate_tests.c | 93 ++++++++ .../selftests/kvm/x86_64/sev_migrate_tests.c | 48 ++-- virt/kvm/guest_mem.c | 151 ++++++++++-- virt/kvm/kvm_main.c | 10 + virt/kvm/kvm_mm.h | 7 + 15 files changed, 596 insertions(+), 132 deletions(-) create mode 100644 tools/testing/selftests/kvm/x86_64/private_mem_migrate_tests.c -- 2.41.0.640.ga95def55d0-goog