From patchwork Mon Aug 7 23:01:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 132465 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp1793850vqr; Mon, 7 Aug 2023 17:33:10 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF4aWW+fvA+VldD+wewnAfJGD5yNflpRMdNv0BLWOOvfHL1ZPGFqMnX5VpgAsbnYn8oN7sG X-Received: by 2002:a17:90a:246:b0:263:f630:228f with SMTP id t6-20020a17090a024600b00263f630228fmr11091445pje.23.1691454789671; Mon, 07 Aug 2023 17:33:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691454789; cv=none; d=google.com; s=arc-20160816; b=gTKrHkThprnecQgne23krHImerKsp+S1BjzIqHW9KjyX6+RBCrZnwZiswF2Y9Z2eyW rebjCAfz7kGdAAMqQcHjOaHaCe+Kx4emZwQIYkfpeXXwjt1j60bZU66WjATkauOOgNQ6 YJPWqP/ULQI23UKhASZPK9cdPBIsi3+x63Fk2+WLf8ve26YS7IJJDRdFuLv8G3OJCxdd EH4R3j3mw1DL+V/PjwBxqLwM0kCG/BKAMOVC4pqa0Q/3f9TuZRldtz2aFW/CM+I2f4zz VIHL3nOOZO7Kd04TK4OeesW329AkUikMIkt4H0ZrEVx8Oa9lOjviOsQhxBplAgf7BsLD 9wwA== 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=IW9+7phTrNcYq8HIMw1g4ao4IX5+hFD7YSOVD/oAl3g=; fh=vK1aBia+sGK4cV3SQxv+Gz9VvujovOgoxEHe19KkH84=; b=ABhuIvD2yRN9aYJd0NbiugTOpnq4ivlKY+HVlfqBFLt13fLrcXDxn4um/55DEGO+qT t0QIXQ9NzCfCdZLaxyPyubblGeuBCRWMR8YuHQjLlpjMNXUEwC/P5c9L9YKT+d2+Irx3 6DcXIJiY1pmr3EOUuleDsBZtkHMXK25+e0he8CryCKdXJ9Wr+S/6hycDW779y1eLkXyZ kVkHnjWd/jdxyPnzqefG07Grb2YTaK8hDqTlrU2por0iaQcfnuf0BYpM1ERHVqDqCSSk 2IXOdK9tuRsJXm4zA5sYnZthId/vzLpcnPbisatb3lP5LSr4OmcToQ9NANtfiwUDVpya t5SQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=lRc64pQI; 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 lj7-20020a17090b344700b0026826148914si6531499pjb.32.2023.08.07.17.32.56; Mon, 07 Aug 2023 17:33:09 -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=lRc64pQI; 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 S230199AbjHGXCM (ORCPT + 99 others); Mon, 7 Aug 2023 19:02:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40736 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230510AbjHGXBy (ORCPT ); Mon, 7 Aug 2023 19:01:54 -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 442351986 for ; Mon, 7 Aug 2023 16:01:43 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-585f04ffa3eso65813177b3.0 for ; Mon, 07 Aug 2023 16:01:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1691449302; x=1692054102; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=IW9+7phTrNcYq8HIMw1g4ao4IX5+hFD7YSOVD/oAl3g=; b=lRc64pQIaYZ7xSk3fbOMPBysCdTiUIsS1N8aj5ot6y870kPcrhmGq6EX689PwpvC6c r+3FG7+ZLl9sG01Lna2L5/MI0yPhzsFRTa232T1QXVMQdLAmwfNA9KQfLlXgLKwrjV6u e9zTi51fogt3TFiPQ/sxpy2p4tOR4la0fHi25UB8g9U4Z1ABmDP13akxcAdgxabPbhiu hMHjYjuASVHBuT7I1d901Hwfx66g2ysoNw/6PIxj9juKx4sJe/R6gVJhmQo4oZRR/8Jx AcrYHQKHHH1zbx7kjJEvUHSsMfv6NEZHsBV2sGnO68q+TWNWk5PAs2myF6ECdgu5vC+A qmcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691449302; x=1692054102; 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=IW9+7phTrNcYq8HIMw1g4ao4IX5+hFD7YSOVD/oAl3g=; b=BbK7nGFjrO9+splcXd7UsgcPqDXRb8c1FpzvTEw1GcGPV9Ac6kQUGu8BQzO4NojiYF 9cmNBNSurT97SZ+H3YgpCrHijZ7hhjZefMjfiCFJ156WZ30EiDiANo7zjFUjN5eNjy1m A16ZUGBVSlDSF1lsO3tYMAMh6im/n+wjzGsslOe23Sxfzhd6Y31+G9nFEP8FYxwvxii9 /LlsO/GsgjG6CH07JMLRQWL54bVtaf7kTAbmNz6Qr/ZaxECmq4iAP69qKSDPSHeLjNWB 4mqVQHjGbCAjmne96RN109NAzU6e5egdjmoNTKJj5AHRVCqKIJoGfPmgqCs/o1aMH0ot 1xMA== X-Gm-Message-State: AOJu0YyDwOzKqsUu0Eo9IthbB/vowoQf51XDUoLvnYX7ZWCM8WYnD6AV UDMDPLVhFR+shBkPXT8vSWcgFHjc0joo1pWxvA== X-Received: from ackerleytng-ctop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:13f8]) (user=ackerleytng job=sendgmr) by 2002:a25:69c9:0:b0:d51:577e:425d with SMTP id e192-20020a2569c9000000b00d51577e425dmr28980ybc.12.1691449302268; Mon, 07 Aug 2023 16:01:42 -0700 (PDT) Date: Mon, 7 Aug 2023 23:01:10 +0000 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.41.0.640.ga95def55d0-goog Message-ID: Subject: [RFC PATCH 07/11] KVM: x86: Refactor common migration preparation code out of sev_vm_move_enc_context_from 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, Sagi Shahar 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: 1773618897545743294 X-GMAIL-MSGID: 1773618897545743294 Co-developed-by: Sagi Shahar Signed-off-by: Sagi Shahar Co-developed-by: Vishal Annapurve Signed-off-by: Vishal Annapurve Signed-off-by: Ackerley Tng --- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/svm/sev.c | 33 ++++---------------------- arch/x86/kvm/svm/svm.h | 2 +- arch/x86/kvm/x86.c | 42 +++++++++++++++++++++++++++++---- 4 files changed, 43 insertions(+), 36 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index bbefd79b7950..71c1236e4f18 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1711,7 +1711,7 @@ struct kvm_x86_ops { int (*mem_enc_register_region)(struct kvm *kvm, struct kvm_enc_region *argp); int (*mem_enc_unregister_region)(struct kvm *kvm, struct kvm_enc_region *argp); int (*vm_copy_enc_context_from)(struct kvm *kvm, unsigned int source_fd); - int (*vm_move_enc_context_from)(struct kvm *kvm, unsigned int source_fd); + int (*vm_move_enc_context_from)(struct kvm *kvm, struct kvm *source_kvm); void (*guest_memory_reclaimed)(struct kvm *kvm); int (*get_msr_feature)(struct kvm_msr_entry *entry); diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 3c4313417966..e0e206aa3e62 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -1718,35 +1718,15 @@ static int sev_check_source_vcpus(struct kvm *dst, struct kvm *src) return 0; } -int sev_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd) +int sev_vm_move_enc_context_from(struct kvm *kvm, struct kvm *source_kvm) { struct kvm_sev_info *dst_sev = &to_kvm_svm(kvm)->sev_info; struct kvm_sev_info *src_sev, *cg_cleanup_sev; - struct fd f = fdget(source_fd); - struct kvm *source_kvm; bool charged = false; int ret; - if (!f.file) - return -EBADF; - - if (!file_is_kvm(f.file)) { - ret = -EBADF; - goto out_fput; - } - - source_kvm = f.file->private_data; - ret = kvm_mark_migration_in_progress(kvm, source_kvm); - if (ret) - goto out_fput; - ret = kvm_lock_two_vms(kvm, source_kvm); - if (ret) - goto out_mark_migration_done; - - if (sev_guest(kvm) || !sev_guest(source_kvm)) { - ret = -EINVAL; - goto out_unlock; - } + if (sev_guest(kvm) || !sev_guest(source_kvm)) + return -EINVAL; src_sev = &to_kvm_svm(source_kvm)->sev_info; @@ -1785,12 +1765,7 @@ int sev_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd) sev_misc_cg_uncharge(cg_cleanup_sev); put_misc_cg(cg_cleanup_sev->misc_cg); cg_cleanup_sev->misc_cg = NULL; -out_mark_migration_done: - kvm_mark_migration_done(kvm, source_kvm); -out_unlock: - kvm_unlock_two_vms(kvm, source_kvm); -out_fput: - fdput(f); + return ret; } diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index d306e2312b53..4912ac28a3d8 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -721,7 +721,7 @@ int sev_mem_enc_register_region(struct kvm *kvm, int sev_mem_enc_unregister_region(struct kvm *kvm, struct kvm_enc_region *range); int sev_vm_copy_enc_context_from(struct kvm *kvm, unsigned int source_fd); -int sev_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd); +int sev_vm_move_enc_context_from(struct kvm *kvm, struct kvm *source_kvm); void sev_guest_memory_reclaimed(struct kvm *kvm); void pre_sev_run(struct vcpu_svm *svm, int cpu); diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 494b75ef7197..75d48379d94d 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -6325,6 +6325,42 @@ int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event, return 0; } +static int kvm_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd) +{ + int r; + struct kvm *source_kvm; + struct fd f = fdget(source_fd); + + r = -EBADF; + if (!f.file) + return r; + + if (!file_is_kvm(f.file)) + goto out_fdput; + + r = -EINVAL; + source_kvm = f.file->private_data; + if (kvm->arch.vm_type != source_kvm->arch.vm_type) + goto out_fdput; + + r = kvm_mark_migration_in_progress(kvm, source_kvm); + if (r) + goto out_fdput; + + r = kvm_lock_two_vms(kvm, source_kvm); + if (r) + goto out_mark_migration_done; + + r = static_call(kvm_x86_vm_move_enc_context_from)(kvm, source_kvm); + + kvm_unlock_two_vms(kvm, source_kvm); +out_mark_migration_done: + kvm_mark_migration_done(kvm, source_kvm); +out_fdput: + fdput(f); + return r; +} + int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap) { @@ -6463,11 +6499,7 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, r = static_call(kvm_x86_vm_copy_enc_context_from)(kvm, cap->args[0]); break; case KVM_CAP_VM_MOVE_ENC_CONTEXT_FROM: - r = -EINVAL; - if (!kvm_x86_ops.vm_move_enc_context_from) - break; - - r = static_call(kvm_x86_vm_move_enc_context_from)(kvm, cap->args[0]); + r = kvm_vm_move_enc_context_from(kvm, cap->args[0]); break; case KVM_CAP_EXIT_HYPERCALL: if (cap->args[0] & ~KVM_EXIT_HYPERCALL_VALID_MASK) {