Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp168706pxb; Wed, 24 Feb 2021 22:20:34 -0800 (PST) X-Google-Smtp-Source: ABdhPJwgSTyC8C4Gcv4D5SMVLxKCNeHfYrWZtkFuHgQlWPD3hlBvSjNzkfZKJ4vpc/OYJ7R2/oeT X-Received: by 2002:a17:906:1182:: with SMTP id n2mr1231486eja.234.1614234034165; Wed, 24 Feb 2021 22:20:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614234034; cv=none; d=google.com; s=arc-20160816; b=RMaIHiMp0JcXeViWuLJjcl1lMtYRJBr82XvC4WAnOzBM+sWm7ol2Ump+OdKdRdN/YV 9maTXPsirGCMvReBmA+6H4JhFkdWNGS7HvRdobKAfLyHzmGFjRSLG1KSv//fnlIIebAl bwtKB9x9kjW/VIKpmUskaiu6+Sgulceru2KBqo2UuPUnHT5X0/Uj3Vwg85fQPcmi++el PfAHUODqfIZYciII3aB1/FmuxU4yiYwXeIwWndkQOuG0m2a3P60Gh13VI7se6cr7vgZa JK7vN3Szrxw28CZHmIcrQ9/JjDEaCiTLgYGfaTZdf7FrbSgFsVnOQxV4mbx/jH9ULPF8 rpPg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=RWBwTIueuYncqBnoLNaT06f86OMdHzkNoeJqjL3eUCg=; b=sj22oNU3UEuKfwVEwZzRpvxCJ71QC7U1P23Dk+yZQwLZGIXByqPFNzDjXGdEm2oCmi XcpYVUAug60sU0EM4OvtrRVzyOsQrtm33XvQaMM7/go1snZudu5Y4TGgqvFcBBkj0iK0 xtR4vHAfvLCb5zEk5ocQoIwgmdPehcfkiCPppiGMB2nFy6hmEarhUpp5iMx8zq5M1m8A MtFnfwjjT4dEb7skJcqp9yxYl/xTHTqb+u0k6RlJY4g22yqSJ3AytM2xQKXt93cfalbR IicC+MaBPFANPvFqxYEVd6TvPnY7xPoixbRvlFPIBj9jOchCaoSmmuYU1dmkLoi1m3Ca asDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="Y/EhyyN6"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m22si2776065eje.468.2021.02.24.22.20.10; Wed, 24 Feb 2021 22:20:34 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="Y/EhyyN6"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S233077AbhBYDp6 (ORCPT + 99 others); Wed, 24 Feb 2021 22:45:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56894 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232077AbhBYDp5 (ORCPT ); Wed, 24 Feb 2021 22:45:57 -0500 Received: from mail-io1-xd2c.google.com (mail-io1-xd2c.google.com [IPv6:2607:f8b0:4864:20::d2c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AB355C061574 for ; Wed, 24 Feb 2021 19:45:17 -0800 (PST) Received: by mail-io1-xd2c.google.com with SMTP id g27so3089251iox.2 for ; Wed, 24 Feb 2021 19:45:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=RWBwTIueuYncqBnoLNaT06f86OMdHzkNoeJqjL3eUCg=; b=Y/EhyyN6fQJaxq/oxM7enYR4Lp336hl0snpi8FibiCF6+VrMkfWzhz1MfohCebHuRB D1K+Nm/B3GvAzFMAJrfbguIYCBJXNWbIpcjtsKRmJha86adl+9RXkO9ImqLnzbSFfTS2 Ftxcz66AgxPIp4v3knkNUTUDbt1PETRTnJQHD++F22UGTYrjZiG2Pf692udfOEKlsQHW 62gKmEJc4iSrxjVn+miYswUcxDm0nL7h2XofDrtjfStUXJr8WCF3ijrQWv/inFXT8OW0 75KOrwOVls4zK6qCHOSyYNMuItJtQNN24e/jSOumWd/dz5WUjHvkcujTDGPUfehLzB2M Rdjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=RWBwTIueuYncqBnoLNaT06f86OMdHzkNoeJqjL3eUCg=; b=L6MO63g/85sp4E6os3TwB37Njw++uSjoIdB0bUmWj6olo3RSNRCia3ZGKK2/nMj/pR HS+OBvrOYsMXb4DWEJ8x3C5Jheso+RnzNvAnMJCr8uhQXl2JCIiIW9LFw7kGlsVIDBb/ Q14N1hkx87F+fkdW6kHS3dPfq+NaCrwLL8Sag4Q+bqr+/ntmIPIi8CBl8dUrD0vcjFdM h/Iex+7a3zxXtO/OeQ419quiM2J7KlLZFdHkli98t/l+mk24upTPIn1US1yHR+65FSxh 1kC3dLgGgmcT8ZSEtdZC4cUg+gTWh2bfLI8OLMKeNsG3ajJoU1ZcDT63jGFyLjwoDREE yksw== X-Gm-Message-State: AOAM532ab6NNaA9v7mifNGGleq9FIqDhmcwFrxPAEHOtJ+a+Lx5NJCVA /jBeNUFK36wEmsQ3/0XinD/DAmxxqFORaOcEfGo/zA== X-Received: by 2002:a05:6602:584:: with SMTP id v4mr1020635iox.156.1614224716835; Wed, 24 Feb 2021 19:45:16 -0800 (PST) MIME-Version: 1.0 References: <20210224085915.28751-1-natet@google.com> In-Reply-To: <20210224085915.28751-1-natet@google.com> From: Steve Rutherford Date: Wed, 24 Feb 2021 19:44:40 -0800 Message-ID: Subject: Re: [RFC] KVM: x86: Support KVM VMs sharing SEV context To: Nathan Tempelman Cc: Paolo Bonzini , Tom Lendacky , X86 ML , KVM list , LKML , Sean Christopherson , David Rientjes , Brijesh Singh , Ashish Kalra Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Feb 24, 2021 at 1:00 AM Nathan Tempelman wrote: > > @@ -1186,6 +1195,10 @@ int svm_register_enc_region(struct kvm *kvm, > if (!sev_guest(kvm)) > return -ENOTTY; > > + /* If kvm is mirroring encryption context it isn't responsible for it */ > + if (is_mirroring_enc_context(kvm)) > + return -ENOTTY; > + Is this necessary? Same for unregister. When we looked at sev_pin_memory, I believe we concluded that double pinning was safe. > > if (range->addr > ULONG_MAX || range->size > ULONG_MAX) > return -EINVAL; > > @@ -1252,6 +1265,10 @@ int svm_unregister_enc_region(struct kvm *kvm, > struct enc_region *region; > int ret; > > + /* If kvm is mirroring encryption context it isn't responsible for it */ > + if (is_mirroring_enc_context(kvm)) > + return -ENOTTY; > + > mutex_lock(&kvm->lock); > > if (!sev_guest(kvm)) { > @@ -1282,6 +1299,65 @@ int svm_unregister_enc_region(struct kvm *kvm, > return ret; > } > > +int svm_vm_copy_asid_to(struct kvm *kvm, unsigned int mirror_kvm_fd) > +{ > + struct file *mirror_kvm_file; > + struct kvm *mirror_kvm; > + struct kvm_sev_info *mirror_kvm_sev; > + unsigned int asid; > + int ret; > + > + if (!sev_guest(kvm)) > + return -ENOTTY; You definitely don't want this: this is the function that turns the vm into an SEV guest (marks SEV as active). (Not an issue with this patch, but a broader issue) I believe sev_guest lacks the necessary acquire/release barriers on sev->active, since it's called without the kvm lock. I mean, it's x86, so the only one that's going to hose you is the compiler for this type of access. There should be an smp_rmb() after the access in sev_guest and an smp_wmb() before the access in SEV_GUEST_INIT and here. > > + > + mutex_lock(&kvm->lock); > + > + /* Mirrors of mirrors should work, but let's not get silly */ > + if (is_mirroring_enc_context(kvm)) { > + ret = -ENOTTY; > + goto failed; > + } > + > + mirror_kvm_file = fget(mirror_kvm_fd); > + if (!kvm_is_kvm(mirror_kvm_file)) { > + ret = -EBADF; > + goto failed; > + } > + > + mirror_kvm = mirror_kvm_file->private_data; > + > + if (mirror_kvm == kvm || is_mirroring_enc_context(mirror_kvm)) { Just check if the source is an sev_guest and that the destination is not an sev_guest. I reviewed earlier incarnations of this, and think the high-level idea is sound. I'd like to see kvm-selftests for this patch, and plan on collaborating with AMD to help make those happen.