From patchwork Mon Aug 7 23:01:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 132466 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp1798790vqr; Mon, 7 Aug 2023 17:47:30 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGr7HVNcYyE5Qx48Unm6BDTyPfSEEuukolKVjLMX+T4TqQkCZz01LdVyaE8mUy5gmfZKzry X-Received: by 2002:a05:6a20:9154:b0:13e:8ce5:bed4 with SMTP id x20-20020a056a20915400b0013e8ce5bed4mr14093246pzc.7.1691455650435; Mon, 07 Aug 2023 17:47:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691455650; cv=none; d=google.com; s=arc-20160816; b=AMlKf55tZVvASLl4T67A8yf4vaup9rQ8Tww6it2r4Zyeh0u1Q+vnnmLGZgusZBCTJX E9FIeXMbv6+zjyMMo/Y7ArpNvhEb/Y4+Z+TTxDsgwGsur17ZDDVWBH0Oc2TX8imQFqPK ePHEk/UNtwYW8r39Zhahisa1c1R8M5yjpNerUUNG1QFncmOPLp8kLj3JYIHbHkS0V0zH X1JHV9RWTeCBEnQ9nlJg3xz+s2BJax5di192u57zM86GzsczyqU6Ei6M02KsMhxEUWHv JMeMrkjHBauxJpeHQeJrfQ3cqy6tJyS2hRdUXpHg9TeSBekDqhe4Xw3O+6txc/I35dzH xWYA== 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=bl1TLMRbvm5qHwbmYRTb4VdYc7Xj9ZoiU46WkqVPij4=; fh=HbRmZS+XZk9IdI9bC1USVLouzBEe1WLRItFSyknhPLs=; b=Cso//LXpdu73NHQI5bb7wJywV7ByIJyJvUL11g5Ei9WULTpPnR8tvUstOmEeDRTqKp H3Wuoy4dJ7Rj1WEOr3e+H0UZ+epJZnOmaT2NZKmS1mh9ZoqFDVrrD7mYDBEn9z23b0F5 ogLuicyZ80VUsoe6UsYr/roluKBIwrYYFIQ1GySpIsEXR3cDg7/uM3I0+lRrcIqm1s0T puee7TMX0z/zD0neDZBKZ/YPANggKRXzfnugbuU+WC69NCU7cJurWwEuR1028+FkhIkU Nm3UinMikTmByxrR/aIB+9Zot5UYIsqop4pIwyL93v1KF6lFnJ2U/k2NHBLpNZmvNls1 d16g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=2BOtUuEU; 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 j34-20020a632322000000b005644829012fsi6475691pgj.701.2023.08.07.17.47.16; Mon, 07 Aug 2023 17:47:30 -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=2BOtUuEU; 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 S230346AbjHGXBn (ORCPT + 99 others); Mon, 7 Aug 2023 19:01:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40334 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229942AbjHGXBc (ORCPT ); Mon, 7 Aug 2023 19:01:32 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 23E8910F0 for ; Mon, 7 Aug 2023 16:01:31 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5840614b13cso86730437b3.0 for ; Mon, 07 Aug 2023 16:01:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1691449290; x=1692054090; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=bl1TLMRbvm5qHwbmYRTb4VdYc7Xj9ZoiU46WkqVPij4=; b=2BOtUuEUFEIj9skMvKxqhTWLGWm0zT85W9Ihp2s4CpbxeUR5ws+01RJ+HQ+yYQxSKj woBvSb4Gk9IPbajI2xb83HAVL2vNi6DqnESZhzmotF6Ol6W/EJeSILA9FOeYje4rZmlQ jwLqD4etekit8dCQHL06GwEffA8P+bK2BwprQsLD6vr9T5p2VjUY/rSCPLXKN8pg0X2n lqCQCQsl6ilU2YFEKJfBss9T/uJCTM8wKKp4u8S1wqiZfR4YgIj+8oKUyz68tt/c7dCt 50DX3ArGIPxPQ5orgu4PasB4iVOVKIP4vUILDfnEp1whQTtvB9OscnFZjDC5X2wkcQqB 1e7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691449290; x=1692054090; 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=bl1TLMRbvm5qHwbmYRTb4VdYc7Xj9ZoiU46WkqVPij4=; b=Iqn1EJqbDNHcdQzD6U/zNMxMoWx1UjYtisswaDL8VR7SkvUvZCAauf4G/CzxWNVmTV xvt7ZDPtOlssFfenM7L9E7GN+Wy9rigpBP8sJY0/yw4AxcIHvB8FEZzJYKMvWwDyXniB uTJMrDenq/5Mc34zPfHfNBhr6Qc7DIUlT/0ehJRW2487UuO0219PwQMWj6YmMbRT0xKC O/RuhxysnWcEfrBGwiXSJxbyCL/xvkyC2Sdbwsh42L7lc6g4n8k6h6WToBuBYwedkSh/ yKOqN83w9Ysp7Bt+iXZKaOPoZ07oboqlXjRr6xnXpG5YbwrXWtOAwK05CTHFTi59RSMX x4WA== X-Gm-Message-State: AOJu0YyiW6n5WiJbCgwdVPKpjOCjaWBKfD9TXzLXFPdKIAVv4lzuIBcs MeSbom304/t08BQqQGXOfkXidmAaWPfrQG0ZDw== X-Received: from ackerleytng-ctop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:13f8]) (user=ackerleytng job=sendgmr) by 2002:a05:690c:c05:b0:576:e268:903d with SMTP id cl5-20020a05690c0c0500b00576e268903dmr109985ywb.2.1691449290326; Mon, 07 Aug 2023 16:01:30 -0700 (PDT) Date: Mon, 7 Aug 2023 23:01:04 +0000 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.41.0.640.ga95def55d0-goog Message-ID: <21569429bcd8be29f1719649aae0067a00172b66.1691446946.git.ackerleytng@google.com> Subject: [RFC PATCH 01/11] KVM: guest_mem: Refactor out kvm_gmem_alloc_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=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: 1773619800156480227 X-GMAIL-MSGID: 1773619800156480227 kvm_gmem_alloc_file() will allocate and build a file out of an inode. Will be reused later by __kvm_gmem_link() Signed-off-by: Ackerley Tng --- virt/kvm/guest_mem.c | 53 ++++++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/virt/kvm/guest_mem.c b/virt/kvm/guest_mem.c index 3a3e38151b45..30d0ab8745ee 100644 --- a/virt/kvm/guest_mem.c +++ b/virt/kvm/guest_mem.c @@ -365,12 +365,42 @@ static const struct inode_operations kvm_gmem_iops = { .setattr = kvm_gmem_setattr, }; +static struct file *kvm_gmem_alloc_file(struct kvm *kvm, struct inode *inode, + struct vfsmount *mnt) +{ + struct file *file; + struct kvm_gmem *gmem; + + gmem = kzalloc(sizeof(*gmem), GFP_KERNEL); + if (!gmem) + return ERR_PTR(-ENOMEM); + + file = alloc_file_pseudo(inode, mnt, "kvm-gmem", O_RDWR, &kvm_gmem_fops); + if (IS_ERR(file)) + goto err; + + file->f_flags |= O_LARGEFILE; + file->f_mapping = inode->i_mapping; + + kvm_get_kvm(kvm); + gmem->kvm = kvm; + xa_init(&gmem->bindings); + + file->private_data = gmem; + + list_add(&gmem->entry, &inode->i_mapping->private_list); + + return file; +err: + kfree(gmem); + return file; +} + static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags, struct vfsmount *mnt) { const char *anon_name = "[kvm-gmem]"; const struct qstr qname = QSTR_INIT(anon_name, strlen(anon_name)); - struct kvm_gmem *gmem; struct inode *inode; struct file *file; int fd, err; @@ -399,34 +429,15 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags, goto err_inode; } - file = alloc_file_pseudo(inode, mnt, "kvm-gmem", O_RDWR, &kvm_gmem_fops); + file = kvm_gmem_alloc_file(kvm, inode, mnt); if (IS_ERR(file)) { err = PTR_ERR(file); goto err_fd; } - file->f_flags |= O_LARGEFILE; - file->f_mapping = inode->i_mapping; - - gmem = kzalloc(sizeof(*gmem), GFP_KERNEL); - if (!gmem) { - err = -ENOMEM; - goto err_file; - } - - kvm_get_kvm(kvm); - gmem->kvm = kvm; - xa_init(&gmem->bindings); - - file->private_data = gmem; - - list_add(&gmem->entry, &inode->i_mapping->private_list); - fd_install(fd, file); return fd; -err_file: - fput(file); err_fd: put_unused_fd(fd); err_inode: From patchwork Mon Aug 7 23:01:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 132476 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp1815845vqr; Mon, 7 Aug 2023 18:36:15 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFVE5MNVE34WPtCQcOw6RDxw9zan597tXhh+jcI7DwuyBeTEUFRQeEqL6ARmnmkylLDdUKr X-Received: by 2002:a05:6359:1409:b0:133:a55:7e26 with SMTP id jw9-20020a056359140900b001330a557e26mr6116449rwb.7.1691458574969; Mon, 07 Aug 2023 18:36:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691458574; cv=none; d=google.com; s=arc-20160816; b=XjUCwrxZF+wcCyPVwto4qxPUwNHEnIl3di09eqfECN5kiYOSb3daLKoSR7ZX5CXgZk S//WClyoz5KW2fA5lH0HZTHqVUHyTHBuB9eTioUEw1f3FriUMZNEZEUfoZ3q20WRK1yL YKtTMj+gfVvdNLuhmLKUO0+nS8eeSk8hlAMd18fDvYSQCh6DuTgGeuBnU7VRnpigiY4g q/wst+m5K4Wag+yb8UoOMmKcJ0bi762RfyQjEyLXxTyoiiW7g3FY7OsIgr3ynBodMbzK jUHNCViF2x1mVmVJQKSrGKYWru13WVzT7m5ZYcEBy7k0G6BYKoENf6fzJejaxJQF5Xra OblA== 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=5cvBt015EcTFztTll0vjYcigrW3DKQzPiIwftCLBNbM=; fh=HbRmZS+XZk9IdI9bC1USVLouzBEe1WLRItFSyknhPLs=; b=ou2elmZ314xm35cRRvXoHB73Fz2469L7Sf938wsN9Grea+OaDwRqW+bds6YOwcncTb ANb+eFS72KiMTOkFsSe+/82+ER59of3EHfmjyZNPZ74XXLUE2TYljAr36Y5QJ+HI6+W4 /Y6wUDdojKQ9dS50FGpLQujfiUh9frl57gX2SbbmbRWUpLldjNlACa765VoqZHwCFEcH bLkgfGWfaqh6RPi0S1NsHU6X/TAe/JpTSAVN6FnX3W1PmFbEb3CYvJFz0awgRtmSZWEW igNHeXool4ck0H331cn7cQKJOKVYJXbkkwNr9shW9pdir7viWM96a6mAl4oQPhhkLLiK KQxw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=4OG8fBnS; 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 f15-20020a63dc4f000000b00548e140a1a4si6261399pgj.644.2023.08.07.18.36.02; Mon, 07 Aug 2023 18:36:14 -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=4OG8fBnS; 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 S230304AbjHGXBq (ORCPT + 99 others); Mon, 7 Aug 2023 19:01:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40348 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229992AbjHGXBe (ORCPT ); Mon, 7 Aug 2023 19:01:34 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AEC2410C0 for ; Mon, 7 Aug 2023 16:01:32 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5868992ddd4so58812657b3.0 for ; Mon, 07 Aug 2023 16:01:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1691449292; x=1692054092; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=5cvBt015EcTFztTll0vjYcigrW3DKQzPiIwftCLBNbM=; b=4OG8fBnSqfhUNNOlfnfz98xpsIB7sZynFZMU9BXRZDeo8igEknP1RefaC4s7LZNeCf qpPMus4Yv/693O6vUY7liG7wBhF61GbZ8Gu50050qv8TPVkOR1TRkquqD7DNpNJoljU+ 9tVv8DfberuiWwZgc+Mhd2FTDtLVWo5AeWlfuEs7knlnoTv4Cr2Q+2WQS9QdlWEc5HtG kYvhjSIf7NxYeJmrftUnpUB4FM2sjylnNKHyYe0/15Ub5mzT68NKrfIaUv/flzcCe0Hd gm1FMmNrSAwZad9C6TB9Ffn68907ViOZrpQdw/VIsQ0pd9YLYsuh0Byhq7ugSxaq5/E5 9FKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691449292; x=1692054092; 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=5cvBt015EcTFztTll0vjYcigrW3DKQzPiIwftCLBNbM=; b=HcXHczcK9k65RuYKsnOOkEJomrSswdWaFRiUI2jAUuQJw1y4wx0k+r5M5DQAFbrrlU L/beu2/Zw445VxMiJ5SfXQRQUaqByYhM+LrE4fx8NT8OIm0CeFnb6Z8HHkhBXtop2tLB dYA/mI/SdN2Hdr2nOaDAntAVPH1oAtioaXbZ+ARZr43R3ztr6tOGkCj/Z8LwdHa48vl7 pHcRY0Xccg19GjWpLnWSJEls9tUHQqWI8sFE/g9m6easS6Bi6oa3whowhvRODZ2Z4Hyl CH9gsfUXq/U38O5Pd+DYF9Q5TYH5nGO7EluNkeqxKKhNSw1sA9RiurqPjFmSDoGDEd9d np8w== X-Gm-Message-State: AOJu0YyBLd5/IbWOAFd+h6xPaX12q+IybYUauJp8HkUq0k20pB++Inu6 U5h7W7ATBN2+VwIUkqMNntv77f1PgSz/F3gu3A== X-Received: from ackerleytng-ctop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:13f8]) (user=ackerleytng job=sendgmr) by 2002:a25:e6d3:0:b0:d10:5b67:843c with SMTP id d202-20020a25e6d3000000b00d105b67843cmr59428ybh.4.1691449291933; Mon, 07 Aug 2023 16:01:31 -0700 (PDT) Date: Mon, 7 Aug 2023 23:01:05 +0000 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.41.0.640.ga95def55d0-goog Message-ID: <053b45023744f62bd97cd4f87f048ab514f42b9d.1691446946.git.ackerleytng@google.com> Subject: [RFC PATCH 02/11] KVM: guest_mem: Add ioctl KVM_LINK_GUEST_MEMFD 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: 1773622867083359177 X-GMAIL-MSGID: 1773622867083359177 KVM_LINK_GUEST_MEMFD will link a gmem fd's underlying inode to a new file (and fd). Signed-off-by: Ackerley Tng --- include/uapi/linux/kvm.h | 8 +++++ virt/kvm/guest_mem.c | 73 ++++++++++++++++++++++++++++++++++++++++ virt/kvm/kvm_main.c | 10 ++++++ virt/kvm/kvm_mm.h | 7 ++++ 4 files changed, 98 insertions(+) diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index eb900344a054..d0e2a2ce0df2 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -2299,4 +2299,12 @@ struct kvm_create_guest_memfd { __u64 reserved[6]; }; +#define KVM_LINK_GUEST_MEMFD _IOWR(KVMIO, 0xd5, struct kvm_link_guest_memfd) + +struct kvm_link_guest_memfd { + __u64 fd; + __u64 flags; + __u64 reserved[6]; +}; + #endif /* __LINUX_KVM_H */ diff --git a/virt/kvm/guest_mem.c b/virt/kvm/guest_mem.c index 30d0ab8745ee..1b3df273f785 100644 --- a/virt/kvm/guest_mem.c +++ b/virt/kvm/guest_mem.c @@ -477,6 +477,79 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args) return __kvm_gmem_create(kvm, size, flags, kvm_gmem_mnt); } +static inline void __kvm_gmem_do_link(struct inode *inode) +{ + /* Refer to simple_link() */ + + inode->i_ctime = current_time(inode); + inc_nlink(inode); + + /* + * ihold() to add additional reference to inode for reference in dentry, + * created in kvm_gmem_alloc_file() -> alloc_file_pseudo(). This is not + * necessary when creating a new file because alloc_inode() creates + * inodes with i_count = 1, which is the refcount for the dentry in the + * file. + */ + ihold(inode); + + /* + * dget() and d_instantiate() complete the setup of a dentry, but those + * have already been done in kvm_gmem_alloc_file() -> + * alloc_file_pseudo() + */ +} + +int kvm_gmem_link(struct kvm *kvm, struct kvm_link_guest_memfd *args) +{ + int ret; + int fd; + struct fd f; + struct kvm_gmem *gmem; + u64 flags = args->flags; + u64 valid_flags = 0; + struct inode *inode; + struct file *dst_file; + + if (flags & ~valid_flags) + return -EINVAL; + + f = fdget(args->fd); + if (!f.file) + return -EINVAL; + + ret = -EINVAL; + if (f.file->f_op != &kvm_gmem_fops) + goto out; + + /* Cannot link a gmem file with the same vm again */ + gmem = f.file->private_data; + if (gmem->kvm == kvm) + goto out; + + ret = fd = get_unused_fd_flags(0); + if (fd < 0) + goto out; + + inode = file_inode(f.file); + dst_file = kvm_gmem_alloc_file(inode, kvm_gmem_mnt); + if (IS_ERR(dst_file)) { + ret = PTR_ERR(dst_file); + goto out_fd; + } + + __kvm_gmem_do_link(inode); + + fd_install(fd, dst_file); + return fd; + +out_fd: + put_unused_fd(fd); +out: + fdput(f); + return ret; +} + int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot, unsigned int fd, loff_t offset) { diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index ee331cf8ba54..51cc8b80ebe0 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -5177,6 +5177,16 @@ static long kvm_vm_ioctl(struct file *filp, r = kvm_gmem_create(kvm, &guest_memfd); break; } + case KVM_LINK_GUEST_MEMFD: { + struct kvm_link_guest_memfd params; + + r = -EFAULT; + if (copy_from_user(¶ms, argp, sizeof(params))) + goto out; + + r = kvm_gmem_link(kvm, ¶ms); + break; + } default: r = kvm_arch_vm_ioctl(filp, ioctl, arg); } diff --git a/virt/kvm/kvm_mm.h b/virt/kvm/kvm_mm.h index 798f20d612bb..f85f452133b3 100644 --- a/virt/kvm/kvm_mm.h +++ b/virt/kvm/kvm_mm.h @@ -41,6 +41,7 @@ static inline void gfn_to_pfn_cache_invalidate_start(struct kvm *kvm, int kvm_gmem_init(void); void kvm_gmem_exit(void); int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args); +int kvm_gmem_link(struct kvm *kvm, struct kvm_link_guest_memfd *args); int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot, unsigned int fd, loff_t offset); void kvm_gmem_unbind(struct kvm_memory_slot *slot); @@ -61,6 +62,12 @@ static inline int kvm_gmem_create(struct kvm *kvm, return -EOPNOTSUPP; } +static inline int kvm_gmem_link(struct kvm *kvm, + struct kvm_link_guest_memfd *args) +{ + return -EOPNOTSUPP; +} + static inline int kvm_gmem_bind(struct kvm *kvm, struct kvm_memory_slot *slot, unsigned int fd, loff_t offset) From patchwork Mon Aug 7 23:01:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 132455 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp1761931vqr; Mon, 7 Aug 2023 16:08:03 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFuYTJ5zDEksrjcEGqgRPOm1s57oL5pti1ofmhp7Pvh86akP6ZWhxXdTTMiKfqL0Fxyya7L X-Received: by 2002:a05:6a20:3d94:b0:140:a6ec:b55f with SMTP id s20-20020a056a203d9400b00140a6ecb55fmr8364794pzi.16.1691449683380; Mon, 07 Aug 2023 16:08:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691449683; cv=none; d=google.com; s=arc-20160816; b=D/rgRWg5Nm0L/rL5bNQ+6+LRHiizNh4O03/SV+9BaCF5Xtv7GGL4U5G+ACehI6nJMz sJrHNB8+9xKw5wXyWKKy4PNpSBDSUVIghtsmM1xx5dRRC104VABTU+gKbZiVKrzQmf9i kjwXpYMXPh/UeRYyq7wgfhXbXy7o75A/qXnMs8U3b8oj6X98uBNvunkTfvQEZ+lNwTjj ka9DMYXiawTFsgs+ldRyu7gyLuyr+I8Y4c9lcUAygmJwqTYW9ivd4Vo0cqDGZKOSHWtv vP44PlU5a4dfLKy/z9bDzZqmbJnLbYP4+GdId/c+w08HMN5wx8MH1FFc1p1EwR+EwMqS J9lQ== 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=OyOCciFW2Ckh/B5HTo1lu/EKNDU2b8Raj4KNQZ3bNy8=; fh=HbRmZS+XZk9IdI9bC1USVLouzBEe1WLRItFSyknhPLs=; b=FaZUeXRQmt3nq9K1fObMX696FcX7VAzyLz84vVBvyHAKhlx3kmDU7x1ctS687D0ARO zJpfzf4HU6LzFhl1iU45ABd1YjlEKHxEbFHaJaGqrtkvk/Gi0TqZCEO1I7th4L62ciyU dJgSY54wqM37AV33uLBNCbcjVv58QA8aAj5JchriSNt9Slf1t5vzA2yP2pRJqlMXpIz5 AAVHhdEvhhN2jCj2qv/3KeGyuZ2uUA6IRih3MdDzu1yWPNcwFk8t5brGOoZC5qHORYx9 7m9pR1DRvMAriBxJw5SS0JEc1qahqbYK27lBjr2B8zmHMXBwpB3pyYNTb5k+KMu+2Cl9 vAKQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=uJOBS3ug; 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 bg26-20020a056a02011a00b0056428dcea78si3511960pgb.17.2023.08.07.16.07.50; Mon, 07 Aug 2023 16:08:03 -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=uJOBS3ug; 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 S230473AbjHGXBv (ORCPT + 99 others); Mon, 7 Aug 2023 19:01:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40356 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230133AbjHGXBg (ORCPT ); Mon, 7 Aug 2023 19:01:36 -0400 Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 17E9910FE for ; Mon, 7 Aug 2023 16:01:35 -0700 (PDT) Received: by mail-pg1-x54a.google.com with SMTP id 41be03b00d2f7-55c04f5827eso3412790a12.1 for ; Mon, 07 Aug 2023 16:01:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1691449294; x=1692054094; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=OyOCciFW2Ckh/B5HTo1lu/EKNDU2b8Raj4KNQZ3bNy8=; b=uJOBS3ugweQYN52NY8pPJP4fC3zUz1c9ojo4XDlVUsFi/jjOlgJHXUyLp5Nc+LyD6W wx7xXrNG1HaypGEEtSSWkQYc/mZdvXmHOXd+WmY7Nmgt6Ohwr2RGh+lKOC5XbxZgSPxh LafphqQEjrKPgmBjxtLTK1R4CSWGCA7SbhmNpnkrvdmR0ni/LKS9/zt7P8JZ6kqTpjqP FaCeA+MEOK1Xfexus/CBFMyDCLf9cu+S4Tcvnqkbomm6V4EaSh8N9J5m511j9MKKq5Kd vdxNFuYw5vsbZDO4/RjPf9H4UmvjjuYAFcn2lbbHHKsW/C0CL1KLHTL0cyNTNOObV5Xi QjNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691449294; x=1692054094; 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=OyOCciFW2Ckh/B5HTo1lu/EKNDU2b8Raj4KNQZ3bNy8=; b=dKmdyKLLJPfsKCsZTFi9afcyodafr9b3zu4z56sV6lHWWjJla5o/lhDntYyRqZ1tdH 0uK7qDI4Cu2/oR0p9lNk93MWFI1f+CtFl0P79pO8BAtqA+27YTItH5HMGN5G1zoRJGoZ DUT+UFMrqdaFRl4JuDM4umTOLUWLygfOcjvOmzEWUBZGLcGt1H6bmtfcx+d7e8TGHj19 NQZZpzKw5ymDshPIKz9ZnJiDxuqgt9AJjGVlBbvW+LaKVJ5egtuIXC4oNr15f21/W3Ee 1yjoiyETR1Z2sJf4mL/0cRuCVEpcbkP5b9A9B6i/fLe5HrA7B6L2QeMwQs9xTf/uB0Vr T8RQ== X-Gm-Message-State: AOJu0YxSGS2oFy3CznnYSerPPT2UY8XfEnb4OruTHYt9rrmfTSj0iOj7 d/dhgmrpOEUL82c2eiq6pm7ftxvCJrS6lFqdIw== X-Received: from ackerleytng-ctop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:13f8]) (user=ackerleytng job=sendgmr) by 2002:a63:b512:0:b0:564:9785:75c with SMTP id y18-20020a63b512000000b005649785075cmr35489pge.10.1691449293726; Mon, 07 Aug 2023 16:01:33 -0700 (PDT) Date: Mon, 7 Aug 2023 23:01:06 +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 03/11] KVM: selftests: Add tests for KVM_LINK_GUEST_MEMFD ioctl 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: 1773613543255943494 X-GMAIL-MSGID: 1773613543255943494 Test that + Invalid inputs should be rejected with EINVAL + Successful inputs return a new (destination) fd + Destination and source fds have the same inode number + No crash on program exit Signed-off-by: Ackerley Tng --- .../testing/selftests/kvm/guest_memfd_test.c | 42 +++++++++++++++++++ .../selftests/kvm/include/kvm_util_base.h | 18 ++++++++ 2 files changed, 60 insertions(+) diff --git a/tools/testing/selftests/kvm/guest_memfd_test.c b/tools/testing/selftests/kvm/guest_memfd_test.c index ad20f11b2d2c..38fe96ea60f9 100644 --- a/tools/testing/selftests/kvm/guest_memfd_test.c +++ b/tools/testing/selftests/kvm/guest_memfd_test.c @@ -105,6 +105,47 @@ static void test_create_guest_memfd_invalid(struct kvm_vm *vm, size_t page_size) ASSERT_EQ(errno, EINVAL); } +static void test_link(struct kvm_vm *src_vm, int src_fd, size_t total_size) +{ + int ret; + int dst_fd; + struct kvm_vm *dst_vm; + struct stat src_stat; + struct stat dst_stat; + + dst_vm = vm_create_barebones(); + + /* Linking with a nonexistent fd */ + dst_fd = __vm_link_guest_memfd(dst_vm, 99, 0); + ASSERT_EQ(dst_fd, -1); + ASSERT_EQ(errno, EINVAL); + + /* Linking with a non-gmem fd */ + dst_fd = __vm_link_guest_memfd(dst_vm, 0, 1); + ASSERT_EQ(dst_fd, -1); + ASSERT_EQ(errno, EINVAL); + + /* Linking with invalid flags */ + dst_fd = __vm_link_guest_memfd(dst_vm, src_fd, 1); + ASSERT_EQ(dst_fd, -1); + ASSERT_EQ(errno, EINVAL); + + /* Linking with an already-associated vm */ + dst_fd = __vm_link_guest_memfd(src_vm, src_fd, 1); + ASSERT_EQ(dst_fd, -1); + ASSERT_EQ(errno, EINVAL); + + dst_fd = __vm_link_guest_memfd(dst_vm, src_fd, 0); + TEST_ASSERT(dst_vm > 0, "linking should succeed with valid inputs"); + TEST_ASSERT(src_fd != dst_fd, "linking should return a different fd"); + + ret = fstat(src_fd, &src_stat); + ASSERT_EQ(ret, 0); + ret = fstat(dst_fd, &dst_stat); + ASSERT_EQ(ret, 0); + TEST_ASSERT(src_stat.st_ino == dst_stat.st_ino, + "src and dst files should have the same inode number"); +} int main(int argc, char *argv[]) { @@ -126,6 +167,7 @@ int main(int argc, char *argv[]) test_mmap(fd, page_size); test_file_size(fd, page_size, total_size); test_fallocate(fd, page_size, total_size); + test_link(vm, fd, total_size); close(fd); } diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index 8bdfadd72349..868925b26a7b 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -495,6 +495,24 @@ static inline int vm_create_guest_memfd(struct kvm_vm *vm, uint64_t size, return fd; } +static inline int __vm_link_guest_memfd(struct kvm_vm *vm, int fd, uint64_t flags) +{ + struct kvm_link_guest_memfd params = { + .fd = fd, + .flags = flags, + }; + + return __vm_ioctl(vm, KVM_LINK_GUEST_MEMFD, ¶ms); +} + +static inline int vm_link_guest_memfd(struct kvm_vm *vm, int fd, uint64_t flags) +{ + int new_fd = __vm_link_guest_memfd(vm, fd, flags); + + TEST_ASSERT(new_fd >= 0, KVM_IOCTL_ERROR(KVM_LINK_GUEST_MEMFD, new_fd)); + return new_fd; +} + void vm_set_user_memory_region(struct kvm_vm *vm, uint32_t slot, uint32_t flags, uint64_t gpa, uint64_t size, void *hva); int __vm_set_user_memory_region(struct kvm_vm *vm, uint32_t slot, uint32_t flags, From patchwork Mon Aug 7 23:01:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 132483 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp1830039vqr; Mon, 7 Aug 2023 19:17:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE5KSixFaESW0RannnlamoZSephtSY9QFrsQMgPeiabuWCsTjyrvugEcgRvuNeY+jJFcuOi X-Received: by 2002:a17:906:51c8:b0:991:c566:979 with SMTP id v8-20020a17090651c800b00991c5660979mr9573342ejk.36.1691461039257; Mon, 07 Aug 2023 19:17:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691461039; cv=none; d=google.com; s=arc-20160816; b=BAnBNv+tvxwjC52LUHsrap4oBlMMhcve0+g69HFiWsesELRpqsWxA1nz39Ez8p83RR dCmeQWDgsuV+enndO3SsmaSNWh3YLClNtmlV9EpijPkWCaZORCbdfBDla5Q7Ch8Rxcts Jtud64GGql4Nx9JdYU077C5o9vtp8Wo5jdaTRGG4i7hWZ4nFmwMgB7Ivy2jL5+6DeNtW MsAunDnytnECd9V7sypHiDx+gRM0P1IfjYHHWT0W+q9pqta/+ha2mSALm0fh9N9lpB7t e6lR9ic6Y1pcwqhWLUQkYCETNW7lVpRqW7lMCYQkgt/JiPveAS6sOterPEI+HYyHTfFO BUsg== 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=Uc0bDe1Q5+nbJUU+pVaB9FCp2PCsV5hV6cNG49jJHNg=; fh=HbRmZS+XZk9IdI9bC1USVLouzBEe1WLRItFSyknhPLs=; b=zxRfJRFQ1lK1bLs30q2z8iMqSMlsrkepCE1jO2ZBFkYV5vhfe9G6KwwlnvQegYRR4E HLL/5DppQwXpm4tY5Op3fDwzPyQHgNaQDTX2p3jMbeqFhyF3IS44FgKCe6LCKc+Nvee8 KpH5mvZBNCQTTks6+ZXh+QEI3kYpew0JcScUW3BwNHGIPya0nLjapU2xGBs/zgHh3IHv qiW6YYxc2GOO2E7LE/0E8i6irg0AqQLOhoSYdi/7zPR42UWN3ejIm9f3eFXbQQ/ro2CS GtqZCPDleaztaFoxjV/BRjh4Awt4rU8J8atRcudPFNMcnOShjHAD7uyUt6GVxXcaDK2L BIzg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=GrSgTtJg; 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 gy18-20020a170906f25200b0099cd34c0f5esi2907587ejb.466.2023.08.07.19.16.56; Mon, 07 Aug 2023 19:17:19 -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=GrSgTtJg; 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 S230429AbjHGXB5 (ORCPT + 99 others); Mon, 7 Aug 2023 19:01:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40398 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230234AbjHGXBi (ORCPT ); Mon, 7 Aug 2023 19:01:38 -0400 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 023F410F4 for ; Mon, 7 Aug 2023 16:01:37 -0700 (PDT) Received: by mail-pl1-x649.google.com with SMTP id d9443c01a7336-1bbf8cb6250so39952375ad.2 for ; Mon, 07 Aug 2023 16:01:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1691449296; x=1692054096; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Uc0bDe1Q5+nbJUU+pVaB9FCp2PCsV5hV6cNG49jJHNg=; b=GrSgTtJgL52TnT/uTWzmQ2rNlZPMbNRraizKQo1lbsrgS8zwcyUDm/ibZ0i0T0QYEI yNGDLdTinm9lF/7glMsZWObw9+hcPz72q1jY6CwmSxTTdKkZON2YroHaQrrQXbAHzbQo heRRBKsGOWIDPIqYmgci7pKiKPglMpS6h9iRl8awq8Lyaj9f+vpI1RSUwe1evQmeQxeA yljxtZt0T+nuOX1tF6nEDUnR3GksjVD7fY4jPs/3Rs3LnZFfDyMDuDHLL3u5JxtDAq1V xs0gZDjgwhRYUB/gtHhjLT5AK2LTUatS5+kQJVwvaYLLe2SctFNprHtCwE+H/4e8eSf+ djuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691449296; x=1692054096; 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=Uc0bDe1Q5+nbJUU+pVaB9FCp2PCsV5hV6cNG49jJHNg=; b=RRYEkGUoE/HFqi+eZTdYTkhxMmBGDLi3Ss7XE8SgDlBKD6PBA9aRbjp6YEVbY66KFJ DPYdKHB+Gg7jhfso9P16yFzk0199YeiFMP8g+gsuceU6G4hFfytFIjVd/FK4XzuMgZZE CRzOc8/c1r8sfkCo/Z7VDTtYg4kvQef3QHxJ0EuYnIQMBtQGo0Nqy3D/+iHMNajhhOta ZvihCd9vbxyhFe/9LrUA1lw0/XoYc8A1ysRCuV8c65iEZ8n4dn3qIPIP0O94JRWuoSbz ND6elkyrG42VH4vWxGlEs34yTYmwVhz3iry4PC5hF8OpivR+cSxU6ogbeAq3EvcFR/MK 39EQ== X-Gm-Message-State: AOJu0YwXUdlXsQ7+ECfReTmBhOGQvfKLonSFUH8OpLSX8QaDCp2FGiAq gvxcg2GjAvYaSjMfUGPx3y32y7Z6djoFXOWCRw== X-Received: from ackerleytng-ctop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:13f8]) (user=ackerleytng job=sendgmr) by 2002:a17:903:1ce:b0:1b9:df8f:888c with SMTP id e14-20020a17090301ce00b001b9df8f888cmr40364plh.8.1691449296351; Mon, 07 Aug 2023 16:01:36 -0700 (PDT) Date: Mon, 7 Aug 2023 23:01:07 +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 04/11] KVM: selftests: Test transferring private memory to another VM 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: 1773625450828843878 X-GMAIL-MSGID: 1773625450828843878 Signed-off-by: Ackerley Tng --- tools/testing/selftests/kvm/Makefile | 1 + .../kvm/x86_64/private_mem_migrate_tests.c | 87 +++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/private_mem_migrate_tests.c diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index cb9450022302..d348ff56c92b 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -82,6 +82,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/platform_info_test TEST_GEN_PROGS_x86_64 += x86_64/pmu_event_filter_test TEST_GEN_PROGS_x86_64 += x86_64/private_mem_conversions_test TEST_GEN_PROGS_x86_64 += x86_64/private_mem_kvm_exits_test +TEST_GEN_PROGS_x86_64 += x86_64/private_mem_migrate_tests TEST_GEN_PROGS_x86_64 += x86_64/set_boot_cpu_id TEST_GEN_PROGS_x86_64 += x86_64/set_sregs_test TEST_GEN_PROGS_x86_64 += x86_64/smaller_maxphyaddr_emulation_test 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 new file mode 100644 index 000000000000..4226de3ebd41 --- /dev/null +++ b/tools/testing/selftests/kvm/x86_64/private_mem_migrate_tests.c @@ -0,0 +1,87 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "kvm_util_base.h" +#include "test_util.h" +#include "ucall_common.h" +#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 + +static void transfer_private_mem_guest_code_src(void) +{ + uint64_t volatile *const ptr = (uint64_t *)TRANSFER_PRIVATE_MEM_GVA; + + *ptr = TRANSFER_PRIVATE_MEM_VALUE; + + GUEST_SYNC1(*ptr); +} + +static void transfer_private_mem_guest_code_dst(void) +{ + uint64_t volatile *const ptr = (uint64_t *)TRANSFER_PRIVATE_MEM_GVA; + + GUEST_SYNC1(*ptr); +} + +static void test_transfer_private_mem(void) +{ + struct kvm_vm *src_vm, *dst_vm; + struct kvm_vcpu *src_vcpu, *dst_vcpu; + int src_memfd, dst_memfd; + struct ucall uc; + + const struct vm_shape shape = { + .mode = VM_MODE_DEFAULT, + .type = KVM_X86_SW_PROTECTED_VM, + }; + + /* 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); + 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, + 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, + 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, + "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); + 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, + 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); + + 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, + "Destination VM should be able to read value transferred"); +} + +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(); + + return 0; +} From patchwork Mon Aug 7 23:01:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 132474 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp1815164vqr; Mon, 7 Aug 2023 18:34:14 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFJnElOCxGKER631Z1zXA3A3mXT/cnQcFzsTnBmYm+cxtejRX1vNvnKioVi263QVNjrSNzc X-Received: by 2002:a05:6a20:4287:b0:134:1011:8582 with SMTP id o7-20020a056a20428700b0013410118582mr14067988pzj.47.1691458454304; Mon, 07 Aug 2023 18:34:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691458454; cv=none; d=google.com; s=arc-20160816; b=tqr95VENEONKgdGEB2/nawNFzt7xeJfNql14hxP0tBn9HPgJort9XL20srCuD4FMMl QDtyy2irplUbZh4eCSKOFY/vuImJcVSndhiRjpq2NhKMJkNI5elP8gcJfQfXfL67xZv9 lTiFtSWGxcfV7ciTq4MjHGvbFZxT5OTh2BNCjZHBgsQH5xrBMF0W6bjZTbiJNElgGN7q Lr+VTMcXeln5SejxzAreax0+X8qeZZnXU4CrQMA85QNPsiGhmlRCOntMQvRLDbijtlDc 6EvRMVj1o4K0atGiqWDcPqjAwRu6ov4d85bHk2UD4oXziDnBG2MKbQBuH2RcepdszX9Z Uq2A== 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=xQQ9HxwXisHRcLOUmLrJOKcqzoMdHrdIoIQ6IecTXIw=; fh=vK1aBia+sGK4cV3SQxv+Gz9VvujovOgoxEHe19KkH84=; b=dGPb8ZwL/L2u+3cM5yEh1iq+FiCQjg2/zjpjjB7ymFNWU3gSox7uycGE8n38aFb1ls E2xV0XX+JJECsRuBfe8UhAEYEmkBhZPuCdXKujCIfnB25U8hXjTwLWLMqgr7D2aC7YAy bYH2Qv/OQVwcbi6rBx3A+FJA/yCsqyyZxF3MBDGDv/QQtxOiZUqN+XDpVP6KaTu4AjAU RAdV/ZHo6W4poQ/jq4VEMSJ2xx/plIjR8a2g6U0RApI1A0g10PdM+m0vUl/i164/XDHM v77q82G5N9FopEEvr0sId5OrQEIOiDqdt29IgInFIU5Ki69NyC6/dGVBbt5MQf727MUU xPQA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=twH1EXTp; 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 kn6-20020a170903078600b001bba894ac4asi6362575plb.274.2023.08.07.18.34.01; Mon, 07 Aug 2023 18:34:14 -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=twH1EXTp; 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 S230134AbjHGXCA (ORCPT + 99 others); Mon, 7 Aug 2023 19:02:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40572 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230431AbjHGXBr (ORCPT ); Mon, 7 Aug 2023 19:01:47 -0400 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 53880170B for ; Mon, 7 Aug 2023 16:01:38 -0700 (PDT) Received: by mail-pj1-x104a.google.com with SMTP id 98e67ed59e1d1-268113acd37so4300395a91.0 for ; Mon, 07 Aug 2023 16:01:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1691449298; x=1692054098; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=xQQ9HxwXisHRcLOUmLrJOKcqzoMdHrdIoIQ6IecTXIw=; b=twH1EXTprHGVwETfKOR51WQSclQGswbqnS6IDXUASDrraJoaBBJYcSIr+OJT2dIZJR leBiyKihkh50aLALvTrdxF+IKAJ3jFcttX4/CKOLWxYHPK/Hq89n7DG+SBlYwcID3qdF r93db5otOGAqx3LNRu2anFQex0nM0yDk4Ocl4C3YoZtmkh0ZpAu9rG9vF8Ge7lA2vx6I txWNeQ43dVlLxzHcjvUKfuL1Y8dBqKNMZLPnHXBO368htDjuIqSLeQJbC51Wv+IDGGQX gOucjyOb8r/lQtjAMCpy64pvNfNhbplU+k6NXtECSvjvhj9qtAksadi1TPZipdkllAL2 VucA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691449298; x=1692054098; 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=xQQ9HxwXisHRcLOUmLrJOKcqzoMdHrdIoIQ6IecTXIw=; b=cvgqG/4JRi2OC1lMvHSW2mRikw3SVBHVrZs8FEqfj2t9JI5gWJccOtG3AbZYA5WcU4 TJFSTqogHiDK4Rbaf4/2jJDjrDo9xWDEzq9MFqCHYSkP1O4V2SZP9e9OEA298xr/qvAH dGOgM9mfLcujyagp5i/yeeHczlTY92SNVKIuff8KTE1LRl1dgu1C7UaSouQOzc6pAzEJ FSXN69yfYiQv2BVTEpgoFLQmqux0pGwrDa+dto8pYLWmuWQCteDbBwvVUxeURiinIDI+ 7WGnog12V7HbrBh3Dklx5/xw9kogV+f7kyZVv0MmBIsy3LJAcSRREnrQgWMTingZWEB9 +GUQ== X-Gm-Message-State: AOJu0YxcyLJ1/C75LIkW//bwARtpWY3R3prj0Eji/HqpTbdFYLaKoriD wJR/VqFWS4UEh4NWdVWeM90PeNhdn7esQDUoog== X-Received: from ackerleytng-ctop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:13f8]) (user=ackerleytng job=sendgmr) by 2002:a17:90b:314c:b0:267:f245:85f1 with SMTP id ip12-20020a17090b314c00b00267f24585f1mr86267pjb.1.1691449298375; Mon, 07 Aug 2023 16:01:38 -0700 (PDT) Date: Mon, 7 Aug 2023 23:01:08 +0000 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.41.0.640.ga95def55d0-goog Message-ID: <5a869a885bb3a74bd77222f75dedcbace266e4cc.1691446946.git.ackerleytng@google.com> Subject: [RFC PATCH 05/11] KVM: x86: Refactor sev's flag migration_in_progress to kvm struct 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: 1773622740404997090 X-GMAIL-MSGID: 1773622740404997090 The migration_in_progress flag will also be needed for migration of non-sev VMs. 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/kvm/svm/sev.c | 17 ++++++----------- arch/x86/kvm/svm/svm.h | 1 - include/linux/kvm_host.h | 1 + 3 files changed, 7 insertions(+), 12 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 07756b7348ae..725289b523c7 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -1556,8 +1556,6 @@ static bool is_cmd_allowed_from_mirror(u32 cmd_id) static int sev_lock_two_vms(struct kvm *dst_kvm, struct kvm *src_kvm) { - struct kvm_sev_info *dst_sev = &to_kvm_svm(dst_kvm)->sev_info; - struct kvm_sev_info *src_sev = &to_kvm_svm(src_kvm)->sev_info; int r = -EBUSY; if (dst_kvm == src_kvm) @@ -1567,10 +1565,10 @@ static int sev_lock_two_vms(struct kvm *dst_kvm, struct kvm *src_kvm) * Bail if these VMs are already involved in a migration to avoid * deadlock between two VMs trying to migrate to/from each other. */ - if (atomic_cmpxchg_acquire(&dst_sev->migration_in_progress, 0, 1)) + if (atomic_cmpxchg_acquire(&dst_kvm->migration_in_progress, 0, 1)) return -EBUSY; - if (atomic_cmpxchg_acquire(&src_sev->migration_in_progress, 0, 1)) + if (atomic_cmpxchg_acquire(&src_kvm->migration_in_progress, 0, 1)) goto release_dst; r = -EINTR; @@ -1583,21 +1581,18 @@ static int sev_lock_two_vms(struct kvm *dst_kvm, struct kvm *src_kvm) unlock_dst: mutex_unlock(&dst_kvm->lock); release_src: - atomic_set_release(&src_sev->migration_in_progress, 0); + atomic_set_release(&src_kvm->migration_in_progress, 0); release_dst: - atomic_set_release(&dst_sev->migration_in_progress, 0); + atomic_set_release(&dst_kvm->migration_in_progress, 0); return r; } static void sev_unlock_two_vms(struct kvm *dst_kvm, struct kvm *src_kvm) { - struct kvm_sev_info *dst_sev = &to_kvm_svm(dst_kvm)->sev_info; - struct kvm_sev_info *src_sev = &to_kvm_svm(src_kvm)->sev_info; - mutex_unlock(&dst_kvm->lock); mutex_unlock(&src_kvm->lock); - atomic_set_release(&dst_sev->migration_in_progress, 0); - atomic_set_release(&src_sev->migration_in_progress, 0); + atomic_set_release(&dst_kvm->migration_in_progress, 0); + atomic_set_release(&src_kvm->migration_in_progress, 0); } /* vCPU mutex subclasses. */ diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 18af7e712a5a..d306e2312b53 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -87,7 +87,6 @@ struct kvm_sev_info { struct list_head mirror_vms; /* List of VMs mirroring */ struct list_head mirror_entry; /* Use as a list entry of mirrors */ struct misc_cg *misc_cg; /* For misc cgroup accounting */ - atomic_t migration_in_progress; }; struct kvm_svm { diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 091bc89ae805..3e03eeca279f 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -842,6 +842,7 @@ struct kvm { #ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES struct xarray mem_attr_array; #endif + atomic_t migration_in_progress; char stats_id[KVM_STATS_NAME_SIZE]; }; From patchwork Mon Aug 7 23:01:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 132475 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp1815599vqr; Mon, 7 Aug 2023 18:35:33 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEe8aBhdBorkciyuIUH+ulWujB8wEgi4gFQgw+hYPG3+Il4JO+Y7T8uC3GMkSyCmHJfxVOO X-Received: by 2002:a17:903:32d1:b0:1b5:522a:1578 with SMTP id i17-20020a17090332d100b001b5522a1578mr10602306plr.29.1691458533015; Mon, 07 Aug 2023 18:35:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691458533; cv=none; d=google.com; s=arc-20160816; b=djciToJXv5UGw0QGpAppkmnMW+oZ00cEGgE5iey5zPUX48okml7FR4sQtIFe+7nqon JQVpPVFnlzhVpQsDvtcuhc7B9u2MZs/c0rZDyrkpbK1PXOriR7St01Tca9hwtm2oUuN5 t0mw1RonzeY5D2uGhkureIJik+Hb1MTzvE+iXSAyHtkpgpektCrYv4i4bg0Hup5bgBPW wY62jrT3DcZj7u+qigN7RLL+VbZvd1ToVhjXaWioR2JVqK4EPlCykzXPkl5BxYiTb/WK uc5Yoe4qO8NEJX+alWto2NPt1Jcaje3RsLVtDarILIhzbA3IXQONMDc2jmqQQzSJtzLK 5MHQ== 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=q+4P4F8zMt4Dd2qn/DvvFJFWH4ILq58+E3D5i6R7pxU=; fh=vK1aBia+sGK4cV3SQxv+Gz9VvujovOgoxEHe19KkH84=; b=Fhjodaduio3naU1SWn2WddsMwzDSNjbJGFIAhW+NhhYVsFBOoU45eu9ik/35WMeUvz af1UCkvUZAq2aVYIUJR6yoloLkreORYlCX7vd8UBNtnosdV+iLSaYSXwUZ7RwTif4MMm rW5pil5atxSc/yMRW2bpwJg8n30f39chPLO/bsxY0Pas/gNJUolN+wummNPgFxpPQLYj E34eznITHr5zWar/zzdh7ZraXGjpiO/Z8obsvn/Lai4JkJ8b7A4982MfVY8ezhZwOYER mnmt6cswiQAisLFg/iGsLpbFw1P5aN3DQfPWUowQemcMpWhvQDIpdQxDGkm82QCQzXx3 fDNg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=Q0k0MahZ; 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 f15-20020a63dc4f000000b00548e140a1a4si6261399pgj.644.2023.08.07.18.35.19; Mon, 07 Aug 2023 18:35:32 -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=Q0k0MahZ; 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 S230497AbjHGXCJ (ORCPT + 99 others); Mon, 7 Aug 2023 19:02:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40396 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229991AbjHGXBx (ORCPT ); Mon, 7 Aug 2023 19:01:53 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4D93A1726 for ; Mon, 7 Aug 2023 16:01:41 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-58473c4f629so58716717b3.1 for ; Mon, 07 Aug 2023 16:01:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1691449300; x=1692054100; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=q+4P4F8zMt4Dd2qn/DvvFJFWH4ILq58+E3D5i6R7pxU=; b=Q0k0MahZIE5dc0IRAXJwKTTZDSg3cZi9yWSlFQHuosf4l1pwmxjRYMo/MoeQmihaZF /Y2YJV0eCS16QuqY2J5LouzDTRRKPh36yzofSbiHq1fXkCRSrQHQ60fAySXo3XtnWoEE gCtigQEluVr3pUX7NMZgK3pdcWTNlmRFpkspeMWfxJty8wvX3j7UJaM07bfYOvR23gEO qadalUmfqPjBKRKKKfUdB/qrNcPo7nYwsM3FclpqeHCX7Li+9n1WiMkWpTTGRnw9J5sl sd+kNZfSjZyU2B3lWLOyXRhsh30tsMnrORLG9tJPdD0PiRGac+EO0SELHeX9PQUK8o7z Lm3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691449300; x=1692054100; 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=q+4P4F8zMt4Dd2qn/DvvFJFWH4ILq58+E3D5i6R7pxU=; b=X3gUmFCLZI3y81ibW21NmhiHyNB1+Y+z5tJlkssHWBNwnvwi+AkmkDqlEJnxFEi/Oa gUBUf5YfG3Qr55DQF32RRPxE7wefH++Bg51dOkwZf/F9OzEEHmWizw8kqPj4l+ytG/cg 7SXlClWM6a4cmdB4bZwzhEq0ahiUVgSo7gbNKGLaK/Tc6OSMG2tExehGNZwy9q1mMzyn i6UIYhntRS01UoJzbNqQ89meJncXF7tGzoqlf2CKMK8E/7I/einuWl05ioK+vsHlH9Dg /XoLpUp0sKCubQIHi6wXsh+CpbOs8ikG7FAu5JjkPN0dnldB9N9urbe5Kqx/Pf2s57ln uorg== X-Gm-Message-State: AOJu0YyLq94ORR6ETqS5qzpzgIxH/U+1Gp2JqBYd9kPEe9Sz+Em/LAyh 3R3E/SKPs+9EnxHpCbJv4e5uRHeAW8aFDVzgRg== X-Received: from ackerleytng-ctop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:13f8]) (user=ackerleytng job=sendgmr) by 2002:a81:ac07:0:b0:583:5471:1717 with SMTP id k7-20020a81ac07000000b0058354711717mr89606ywh.10.1691449300448; Mon, 07 Aug 2023 16:01:40 -0700 (PDT) Date: Mon, 7 Aug 2023 23:01:09 +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 06/11] KVM: x86: Refactor common code out of sev.c 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=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: 1773622822929396439 X-GMAIL-MSGID: 1773622822929396439 Split sev_lock_two_vms() into kvm_mark_migration_in_progress() and kvm_lock_two_vms() and refactor sev.c to use these two new functions. 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/kvm/svm/sev.c | 59 ++++++++++------------------------------ arch/x86/kvm/x86.c | 62 ++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/x86.h | 6 ++++ 3 files changed, 82 insertions(+), 45 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 725289b523c7..3c4313417966 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -1554,47 +1554,6 @@ static bool is_cmd_allowed_from_mirror(u32 cmd_id) return false; } -static int sev_lock_two_vms(struct kvm *dst_kvm, struct kvm *src_kvm) -{ - int r = -EBUSY; - - if (dst_kvm == src_kvm) - return -EINVAL; - - /* - * Bail if these VMs are already involved in a migration to avoid - * deadlock between two VMs trying to migrate to/from each other. - */ - if (atomic_cmpxchg_acquire(&dst_kvm->migration_in_progress, 0, 1)) - return -EBUSY; - - if (atomic_cmpxchg_acquire(&src_kvm->migration_in_progress, 0, 1)) - goto release_dst; - - r = -EINTR; - if (mutex_lock_killable(&dst_kvm->lock)) - goto release_src; - if (mutex_lock_killable_nested(&src_kvm->lock, SINGLE_DEPTH_NESTING)) - goto unlock_dst; - return 0; - -unlock_dst: - mutex_unlock(&dst_kvm->lock); -release_src: - atomic_set_release(&src_kvm->migration_in_progress, 0); -release_dst: - atomic_set_release(&dst_kvm->migration_in_progress, 0); - return r; -} - -static void sev_unlock_two_vms(struct kvm *dst_kvm, struct kvm *src_kvm) -{ - mutex_unlock(&dst_kvm->lock); - mutex_unlock(&src_kvm->lock); - atomic_set_release(&dst_kvm->migration_in_progress, 0); - atomic_set_release(&src_kvm->migration_in_progress, 0); -} - /* vCPU mutex subclasses. */ enum sev_migration_role { SEV_MIGRATION_SOURCE = 0, @@ -1777,9 +1736,12 @@ int sev_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd) } source_kvm = f.file->private_data; - ret = sev_lock_two_vms(kvm, source_kvm); + 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; @@ -1823,8 +1785,10 @@ 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: - sev_unlock_two_vms(kvm, source_kvm); + kvm_unlock_two_vms(kvm, source_kvm); out_fput: fdput(f); return ret; @@ -2057,9 +2021,12 @@ int sev_vm_copy_enc_context_from(struct kvm *kvm, unsigned int source_fd) } source_kvm = f.file->private_data; - ret = sev_lock_two_vms(kvm, source_kvm); + ret = kvm_mark_migration_in_progress(kvm, source_kvm); if (ret) goto e_source_fput; + ret = kvm_lock_two_vms(kvm, source_kvm); + if (ret) + goto e_mark_migration_done; /* * Mirrors of mirrors should work, but let's not get silly. Also @@ -2100,7 +2067,9 @@ int sev_vm_copy_enc_context_from(struct kvm *kvm, unsigned int source_fd) */ e_unlock: - sev_unlock_two_vms(kvm, source_kvm); + kvm_unlock_two_vms(kvm, source_kvm); +e_mark_migration_done: + kvm_mark_migration_done(kvm, source_kvm); e_source_fput: fdput(f); return ret; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index de195ad83ec0..494b75ef7197 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -4340,6 +4340,68 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) } EXPORT_SYMBOL_GPL(kvm_get_msr_common); +int kvm_mark_migration_in_progress(struct kvm *dst_kvm, struct kvm *src_kvm) +{ + int r; + + if (dst_kvm == src_kvm) + return -EINVAL; + + /* + * Bail if these VMs are already involved in a migration to avoid + * deadlock between two VMs trying to migrate to/from each other. + */ + r = -EBUSY; + if (atomic_cmpxchg_acquire(&dst_kvm->migration_in_progress, 0, 1)) + return r; + + if (atomic_cmpxchg_acquire(&src_kvm->migration_in_progress, 0, 1)) + goto release_dst; + + return 0; + +release_dst: + atomic_set_release(&dst_kvm->migration_in_progress, 0); + return r; +} +EXPORT_SYMBOL_GPL(kvm_mark_migration_in_progress); + +void kvm_mark_migration_done(struct kvm *dst_kvm, struct kvm *src_kvm) +{ + atomic_set_release(&dst_kvm->migration_in_progress, 0); + atomic_set_release(&src_kvm->migration_in_progress, 0); +} +EXPORT_SYMBOL_GPL(kvm_mark_migration_done); + +int kvm_lock_two_vms(struct kvm *dst_kvm, struct kvm *src_kvm) +{ + int r; + + if (dst_kvm == src_kvm) + return -EINVAL; + + r = -EINTR; + if (mutex_lock_killable(&dst_kvm->lock)) + return r; + + if (mutex_lock_killable_nested(&src_kvm->lock, SINGLE_DEPTH_NESTING)) + goto unlock_dst; + + return 0; + +unlock_dst: + mutex_unlock(&dst_kvm->lock); + return r; +} +EXPORT_SYMBOL_GPL(kvm_lock_two_vms); + +void kvm_unlock_two_vms(struct kvm *dst_kvm, struct kvm *src_kvm) +{ + mutex_unlock(&dst_kvm->lock); + mutex_unlock(&src_kvm->lock); +} +EXPORT_SYMBOL_GPL(kvm_unlock_two_vms); + /* * Read or write a bunch of msrs. All parameters are kernel addresses. * diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h index 82e3dafc5453..4c6edaf5ac5b 100644 --- a/arch/x86/kvm/x86.h +++ b/arch/x86/kvm/x86.h @@ -539,4 +539,10 @@ int kvm_sev_es_string_io(struct kvm_vcpu *vcpu, unsigned int size, unsigned int port, void *data, unsigned int count, int in); +int kvm_mark_migration_in_progress(struct kvm *dst_kvm, struct kvm *src_kvm); +void kvm_mark_migration_done(struct kvm *dst_kvm, struct kvm *src_kvm); + +int kvm_lock_two_vms(struct kvm *dst_kvm, struct kvm *src_kvm); +void kvm_unlock_two_vms(struct kvm *dst_kvm, struct kvm *src_kvm); + #endif 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) { From patchwork Mon Aug 7 23:01:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 132459 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp1774907vqr; Mon, 7 Aug 2023 16:43:00 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGdcwmSXs79TnBiWPRPHCnXbcUpXr/JKMLwOVrxz15DWby/jWNQxrBic57uEuebx6qVXEzo X-Received: by 2002:a17:907:a058:b0:99b:eb9c:4fb9 with SMTP id gz24-20020a170907a05800b0099beb9c4fb9mr7598472ejc.69.1691451779763; Mon, 07 Aug 2023 16:42:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691451779; cv=none; d=google.com; s=arc-20160816; b=LNZEl1WLzDiMLGzQLurCUG85MiKg0GH78EEheJTgXVVb3X9qQH62tByUuonzgLZoxC 7rLH8VhXIjvL+OZ77Gy77SZkk9Ljl8KRQGe4yUayQ2YU3o6/iazPB+oqaC+KhtSqaQ8R ou9wFR++SDyjCxQ3/vC2lKBz9rYnaoEKQDXDdS6t46Qtm+J4dETnq1njl7wndhQRZ1Iy 5m7Bk6lPz0jz+wmGLr5O1kXJucStOd24wJnwMkdt/+4d3RYLzPMDmgN43pXE3qG/bHQA eJHF9cqNwEOKjxce1zyYOkuPlwrSL8ytcTlycAQQV8g1825CFrkysYlpzyS9EAqX8kGA pppg== 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=4dErXtZJPA2B8bwHAUFICQ3V58GoU0WO1AR+4rLGR9U=; fh=HbRmZS+XZk9IdI9bC1USVLouzBEe1WLRItFSyknhPLs=; b=FWKqrmyY4PkF/U1YY2AJXYo3KFjynBFjJMw4mm/1z5NhfBRrjSOQwdALapEGVFoc6a 07MOAtCbfodFJOpfVdsCmqHPlJB2OAoFnMTEpt9PS4XUXsgH7pMi9Xv7U4Pd4ZipsFzT 0FlCdl2OAN+YEvhjhY7xABN6ROXpJr6Humdx9wqA70EBwEsFeI0teFMWnSw3WpOSnyVi C71Fkb4+Ljwm7Va85Qeb3MiNyWsccls515jpB+Dwwx3M5Wxu5zb7nAhW11Ju6aGVuiIE DRrznhI4gvQEW++nY0BAZCVoQMnovt45/cay5gSSPRqVclbRJkNjKgQ7iclycLytlSCF 6XPQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=ZxlSKTI+; 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 j20-20020a1709066dd400b0099bce4aa78dsi6366525ejt.865.2023.08.07.16.42.35; Mon, 07 Aug 2023 16:42:59 -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=ZxlSKTI+; 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 S231419AbjHGXCP (ORCPT + 99 others); Mon, 7 Aug 2023 19:02:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40396 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231174AbjHGXCH (ORCPT ); Mon, 7 Aug 2023 19:02:07 -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 041A919AD for ; Mon, 7 Aug 2023 16:01:45 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-569e7aec37bso43889447b3.2 for ; Mon, 07 Aug 2023 16:01:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1691449304; x=1692054104; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=4dErXtZJPA2B8bwHAUFICQ3V58GoU0WO1AR+4rLGR9U=; b=ZxlSKTI+laVNBuO8UTZ+ZCGmUs6BNo5I74TnUdV85ACjNmtDKARsLhiKqNMkPh3Mk/ 9RaigGGI96R+oAax6t0XcVzh8hAtDE96WW6YBkgzVqUaGgZ4WbSfnL1Mxr1I+7KubkRZ aaRhDkQDreqT1nDjrN2f4k06gs9+NCb45aivwY5/OAY6/QXCQhJngzrRQGNBLIUz8Cyt hlP0O+Y8+CM3VuMtiAJtfNe0nW6FbV1OSPxS/FzhNkedO0KnlSQqZ9O7eheS8Rab6j0x 7Wn5l0azCIiJBUoh4czhdZ8N3ivr5kK4ri1VgZ0oq/SFtrBJ5mIx+xdOg+mJb52yDZkP lTxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691449304; x=1692054104; 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=4dErXtZJPA2B8bwHAUFICQ3V58GoU0WO1AR+4rLGR9U=; b=OgKy/UcN9FV80ovIBMR4zyiv34nGpL+7zqZYxcFX3aUBP/NFjwQ3hAed7VagTEZ9pI to3LxTdg/X1BkKCHrJ2pmGcXBUJRd7BYPDkxrHnOtcTfB4otgnBp980yM6TOSk1Uh9QK mccagx/lseuDE1mcI8oSLWDbf/jSDn5IY2Ico4FsNyL2F22OfP8li2peL/V1UQW0DQzO 7Na0rqkQ0+rOPsuVuFjo8n1wUHm5zg7kb5smf2gFf/acFMXnmDep9OGU0iM9nhoXlkj3 nSlHv6oJkRCetyjMFitlwuLZI2LdWq0L0kr3CT1Hrm3dO/mMEzmN4Uob9Qbjj1t4lg1b 5hUw== X-Gm-Message-State: AOJu0YwFJu7DZpxklMzsJAlrdZMgRjO7xZu1QP61FUTitSiV8uY7ScYi cBbvmy3omzx0fI+AjATFTPqpVbs8mBVY0eqFEA== X-Received: from ackerleytng-ctop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:13f8]) (user=ackerleytng job=sendgmr) by 2002:a05:6902:1588:b0:d3b:12d3:564e with SMTP id k8-20020a056902158800b00d3b12d3564emr65596ybu.2.1691449304218; Mon, 07 Aug 2023 16:01:44 -0700 (PDT) Date: Mon, 7 Aug 2023 23:01:11 +0000 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.41.0.640.ga95def55d0-goog Message-ID: <196a2130f155cbc0201cce06579f122352c8b236.1691446946.git.ackerleytng@google.com> Subject: [RFC PATCH 08/11] KVM: x86: Let moving encryption context be configurable 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: 1773615741530179171 X-GMAIL-MSGID: 1773615741530179171 SEV-capable VMs may also use the KVM_X86_SW_PROTECTED_VM type, but they will still need architecture-specific handling to move encryption context. Hence, we let moving of encryption context be configurable and store that configuration in a flag. 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 | 2 ++ arch/x86/kvm/x86.c | 9 ++++++++- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 71c1236e4f18..ab45a3d3c867 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1445,6 +1445,8 @@ struct kvm_arch { */ #define SPLIT_DESC_CACHE_MIN_NR_OBJECTS (SPTE_ENT_PER_PAGE + 1) struct kvm_mmu_memory_cache split_desc_cache; + + bool vm_move_enc_ctxt_supported; }; struct kvm_vm_stat { diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index e0e206aa3e62..b09e6477e309 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -256,6 +256,8 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) goto e_no_asid; sev->asid = asid; + kvm->arch.vm_move_enc_ctxt_supported = true; + ret = sev_platform_init(&argp->error); if (ret) goto e_free; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 75d48379d94d..a1a28dd77b94 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -6351,7 +6351,14 @@ static int kvm_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd) if (r) goto out_mark_migration_done; - r = static_call(kvm_x86_vm_move_enc_context_from)(kvm, source_kvm); + /* + * Different types of VMs will allow userspace to define if moving + * encryption context should be supported. + */ + if (kvm->arch.vm_move_enc_ctxt_supported && + kvm_x86_ops.vm_move_enc_context_from) { + r = static_call(kvm_x86_vm_move_enc_context_from)(kvm, source_kvm); + } kvm_unlock_two_vms(kvm, source_kvm); out_mark_migration_done: From patchwork Mon Aug 7 23:01:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 132463 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp1779746vqr; Mon, 7 Aug 2023 16:59:11 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE/+tox6kri4NBNwKOCAPhMM4zxaueQPe2JIZ7CxRU54gtqs6XrwRhgMrQILWg+MysmXdQu X-Received: by 2002:a05:6830:22c1:b0:6b9:4516:7d1e with SMTP id q1-20020a05683022c100b006b945167d1emr8622343otc.30.1691452751058; Mon, 07 Aug 2023 16:59:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691452751; cv=none; d=google.com; s=arc-20160816; b=C++6KBr7TYJYIQX9kYg44dwjs0QrDnKJlqiCsRTUUJBfyxeGJHFtQ6zAdDjhhy5/KG UP82UPCfWWc5BLkijJ+WTj+lCwArYNb6w3PSuIO3RnQKOOCE/B8t6F2OrKbD0p1syR8u PNHZCVF8nnJoDW6KtVtEBMFJ4Ly7hNKnycolA4pl1qL2IosBSAyctZbW7FyChI+2zKsh 1cfqlZp19j4SDc15ClEZtI298Q8Od7r0qHKk3HcwJG3OnrO31otm7wbdkbly9BB630fW EGpjCfxrQo/Vvxs8sW+SCp2+RiqBKVRjszqDel4YlRJ6C/BNiWQZgvtJXnb48wL3chfY jnMw== 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=BmCSN9U26XeKjtggc7A1AMQ3t040EnO1ZTd61OqRXuY=; fh=vK1aBia+sGK4cV3SQxv+Gz9VvujovOgoxEHe19KkH84=; b=Pmdxvky557czAhZl05eXmaXyAL0wxXxayzbunoNQJmQrjhoMwdDyLNihTbOmL8t7Lw Cdb+m2ouOhFPd92Pl0/thQAUosaVZwhmJ+SNpLjt5+kMDuNOthGam5OY80b69uaQAQTw amN2tfztpn3+kMgnGZkMJD/efH6H6f5DGWQpsXMrCmd9gRdMZpz6j+f/RXAO585vR3jQ A4XRxXfyGEIBOEqgdb29/6Ws3m6XNJXZuM6n19rpd7sRi1hPOGgFnbGtou/eOjVhkU1b Wu3ftK3JvsnaoJg/q3Qa4IXHkOaos3trThonrH5FEA1Ua9OI751mdsHyB7Lclcwt7O5T xeVA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=d72qL5ja; 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 s3-20020a63d043000000b0056424e3bb74si5939175pgi.203.2023.08.07.16.58.57; Mon, 07 Aug 2023 16:59:11 -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=d72qL5ja; 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 S230265AbjHGXCV (ORCPT + 99 others); Mon, 7 Aug 2023 19:02:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40744 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229991AbjHGXCK (ORCPT ); Mon, 7 Aug 2023 19:02:10 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0452C1BCF for ; Mon, 7 Aug 2023 16:01:47 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-d47a02fc63fso3355150276.0 for ; Mon, 07 Aug 2023 16:01:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1691449306; x=1692054106; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=BmCSN9U26XeKjtggc7A1AMQ3t040EnO1ZTd61OqRXuY=; b=d72qL5jaNo875D2Ttbag8JHQYT0xztiBQFBU5CxKpoNsYkmS2+1znT1q9wiJEFvc63 +c+Avp84BYasJEda1jz758T5BmgFVY9UR451d1KAluobzBS+p1AxvdBTSy6eF/jtEbm2 MOMjeEHA8cbVLcnuO+BfSIMApEJUigH/OETJLnHKppMM72zxbrbKCcn//LSACaAS9Vov 95P7RI0h7cIj8D7tONzUpe9CtETOANdTi/8X497uExjXNbjmkbWvwFvfnXozz/0ccb2R S3YQE+WFDkENXvnJocQWdvHXkH1wbtnNpLJSq4RXbNBFh5COsIr2Jdq+OPF3tw8UgQOU d6tA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691449306; x=1692054106; 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=BmCSN9U26XeKjtggc7A1AMQ3t040EnO1ZTd61OqRXuY=; b=YCJA14Brb2xGPZqcVUQbH2tKKZsBUpiN31SvRg5e9P6qeGuZwk+jG/s3rFPYJLnpQ1 yC8UCiaFcSzKoqGxsRQED96o6pekELrB/X65+l808Psc2d1FxciAExkgYomCYCRe53hS Q3kwCYcc2/0KDclv06ZFjRR8Vw4JGF/ZWIJd/O8dVz5rL7Dyuu9O2bJSMu7JXnz6+lTZ H9VzNmW26Z9TvZWcIUFZhGrHdVKysMSPhrdIFr7GbPuQAaVxmWCEIvsZ78w2PMPt0fuy 0xGYIsWOIQOSEGeiBAakn4AvYGPC3qUb7F3nr6WE6Wdt3gFCoNr9cEoEUgCZjbwP7VZx Jtjw== X-Gm-Message-State: AOJu0Yw7N5ionThjRCyVpriaDNd5ESh8DBz94jTHo6vDAn9iZoEU34d2 wkkponXV8A6gejuzCFauaormKXsdbIbEY3C1OQ== X-Received: from ackerleytng-ctop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:13f8]) (user=ackerleytng job=sendgmr) by 2002:a25:add3:0:b0:d05:7ba4:67f9 with SMTP id d19-20020a25add3000000b00d057ba467f9mr57902ybe.3.1691449306075; Mon, 07 Aug 2023 16:01:46 -0700 (PDT) Date: Mon, 7 Aug 2023 23:01:12 +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 09/11] KVM: x86: Handle moving of memory context for intra-host migration 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: 1773616759710161516 X-GMAIL-MSGID: 1773616759710161516 Migration of memory context involves moving lpage_info and mem_attr_array from source to destination VM. 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/kvm/x86.c | 110 +++++++++++++++++++++++++++++++++++++++ include/linux/kvm_host.h | 17 ++++++ virt/kvm/guest_mem.c | 25 +++++++++ 3 files changed, 152 insertions(+) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index a1a28dd77b94..12688754c556 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -4402,6 +4402,33 @@ void kvm_unlock_two_vms(struct kvm *dst_kvm, struct kvm *src_kvm) } EXPORT_SYMBOL_GPL(kvm_unlock_two_vms); +static int kvm_lock_vm_memslots(struct kvm *dst_kvm, struct kvm *src_kvm) +{ + int r = -EINVAL; + + if (dst_kvm == src_kvm) + return r; + + r = -EINTR; + if (mutex_lock_killable(&dst_kvm->slots_lock)) + return r; + + if (mutex_lock_killable_nested(&src_kvm->slots_lock, SINGLE_DEPTH_NESTING)) + goto unlock_dst; + + return 0; + +unlock_dst: + mutex_unlock(&dst_kvm->slots_lock); + return r; +} + +static void kvm_unlock_vm_memslots(struct kvm *dst_kvm, struct kvm *src_kvm) +{ + mutex_unlock(&src_kvm->slots_lock); + mutex_unlock(&dst_kvm->slots_lock); +} + /* * Read or write a bunch of msrs. All parameters are kernel addresses. * @@ -6325,6 +6352,78 @@ int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event, return 0; } +static bool memslot_configurations_match(struct kvm_memslots *src_slots, + struct kvm_memslots *dst_slots) +{ + struct kvm_memslot_iter src_iter; + struct kvm_memslot_iter dst_iter; + + kvm_for_each_memslot_pair(&src_iter, src_slots, &dst_iter, dst_slots) { + if (src_iter.slot->base_gfn != dst_iter.slot->base_gfn || + src_iter.slot->npages != dst_iter.slot->npages || + src_iter.slot->flags != dst_iter.slot->flags) + return false; + + if (kvm_slot_can_be_private(dst_iter.slot) && + !kvm_gmem_params_match(src_iter.slot, dst_iter.slot)) + return false; + } + + /* There should be no more nodes to iterate if configurations match */ + return !src_iter.node && !dst_iter.node; +} + +static int kvm_move_memory_ctxt_from(struct kvm *dst, struct kvm *src) +{ + struct kvm_memslot_iter src_iter; + struct kvm_memslot_iter dst_iter; + struct kvm_memslots *src_slots, *dst_slots; + int i; + + /* TODO: Do we also need to check consistency for as_id == SMM? */ + src_slots = __kvm_memslots(src, 0); + dst_slots = __kvm_memslots(dst, 0); + + if (!memslot_configurations_match(src_slots, dst_slots)) + return -EINVAL; + + /* + * Transferring lpage_info is an optimization, lpage_info can be rebuilt + * by the destination VM. + */ + kvm_for_each_memslot_pair(&src_iter, src_slots, &dst_iter, dst_slots) { + for (i = 1; i < KVM_NR_PAGE_SIZES; ++i) { + unsigned long ugfn = dst_iter.slot->userspace_addr >> PAGE_SHIFT; + int level = i + 1; + + /* + * If the gfn and userspace address are not aligned wrt each + * other, skip migrating lpage_info. + */ + if ((dst_iter.slot->base_gfn ^ ugfn) & + (KVM_PAGES_PER_HPAGE(level) - 1)) + continue; + + kvfree(dst_iter.slot->arch.lpage_info[i - 1]); + dst_iter.slot->arch.lpage_info[i - 1] = + src_iter.slot->arch.lpage_info[i - 1]; + src_iter.slot->arch.lpage_info[i - 1] = NULL; + } + } + +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES + /* + * For VMs that don't use private memory, this will just be moving an + * empty xarray pointer. + */ + dst->mem_attr_array.xa_head = src->mem_attr_array.xa_head; + src->mem_attr_array.xa_head = NULL; +#endif + + kvm_vm_dead(src); + return 0; +} + static int kvm_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd) { int r; @@ -6351,6 +6450,14 @@ static int kvm_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd) if (r) goto out_mark_migration_done; + r = kvm_lock_vm_memslots(kvm, source_kvm); + if (r) + goto out_unlock; + + r = kvm_move_memory_ctxt_from(kvm, source_kvm); + if (r) + goto out_unlock_memslots; + /* * Different types of VMs will allow userspace to define if moving * encryption context should be supported. @@ -6360,6 +6467,9 @@ static int kvm_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd) r = static_call(kvm_x86_vm_move_enc_context_from)(kvm, source_kvm); } +out_unlock_memslots: + kvm_unlock_vm_memslots(kvm, source_kvm); +out_unlock: kvm_unlock_two_vms(kvm, source_kvm); out_mark_migration_done: kvm_mark_migration_done(kvm, source_kvm); diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 3e03eeca279f..2f44b5d294a8 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1144,6 +1144,15 @@ static inline bool kvm_memslot_iter_is_valid(struct kvm_memslot_iter *iter, gfn_ kvm_memslot_iter_is_valid(iter, end); \ kvm_memslot_iter_next(iter)) +/* Iterate over a pair of memslots in gfn order until one of the trees end */ +#define kvm_for_each_memslot_pair(iter1, slots1, iter2, slots2) \ + for (kvm_memslot_iter_start(iter1, slots1, 0), \ + kvm_memslot_iter_start(iter2, slots2, 0); \ + kvm_memslot_iter_is_valid(iter1, U64_MAX) && \ + kvm_memslot_iter_is_valid(iter2, U64_MAX); \ + kvm_memslot_iter_next(iter1), \ + kvm_memslot_iter_next(iter2)) + /* * KVM_SET_USER_MEMORY_REGION ioctl allows the following operations: * - create a new memory slot @@ -2359,6 +2368,8 @@ static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn) #ifdef CONFIG_KVM_PRIVATE_MEM int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn, kvm_pfn_t *pfn, int *max_order); +bool kvm_gmem_params_match(struct kvm_memory_slot *slot1, + struct kvm_memory_slot *slot2); #else static inline int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn, @@ -2367,6 +2378,12 @@ static inline int kvm_gmem_get_pfn(struct kvm *kvm, KVM_BUG_ON(1, kvm); return -EIO; } + +static inline bool kvm_gmem_params_match(struct kvm_memory_slot *slot1, + struct kvm_memory_slot *slot2) +{ + return false; +} #endif /* CONFIG_KVM_PRIVATE_MEM */ #endif diff --git a/virt/kvm/guest_mem.c b/virt/kvm/guest_mem.c index 1b3df273f785..2f84e5c67942 100644 --- a/virt/kvm/guest_mem.c +++ b/virt/kvm/guest_mem.c @@ -686,6 +686,31 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, } EXPORT_SYMBOL_GPL(kvm_gmem_get_pfn); +bool kvm_gmem_params_match(struct kvm_memory_slot *slot1, + struct kvm_memory_slot *slot2) +{ + bool ret; + struct file *file1; + struct file *file2; + + if (slot1->gmem.pgoff != slot2->gmem.pgoff) + return false; + + file1 = kvm_gmem_get_file(slot1); + file2 = kvm_gmem_get_file(slot2); + + ret = (file1 && file2 && + file_inode(file1) == file_inode(file2)); + + if (file1) + fput(file1); + if (file2) + fput(file2); + + return ret; +} +EXPORT_SYMBOL_GPL(kvm_gmem_params_match); + static int kvm_gmem_init_fs_context(struct fs_context *fc) { if (!init_pseudo(fc, GUEST_MEMORY_MAGIC)) From patchwork Mon Aug 7 23:01:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ackerley Tng X-Patchwork-Id: 132462 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp1777686vqr; Mon, 7 Aug 2023 16:52:02 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEhLxNWJ/+89LXylyKSHNZgn+YnbmL+UoJEV56sBVpjT3MLL1DcREHVSrL4B/Lhl8NIFEgH X-Received: by 2002:aa7:c99a:0:b0:522:ba6c:9b1b with SMTP id c26-20020aa7c99a000000b00522ba6c9b1bmr9438691edt.26.1691452322080; Mon, 07 Aug 2023 16:52:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691452322; cv=none; d=google.com; s=arc-20160816; b=rUz9KyN1tT4BeFDLPwG9sYy4fiOYtHqtIQlR9kS+EIHbhgL0dV2HZflgFKXlwh6VNA dan9JFr8iPX0328Z+x+yRV4Zos6DxMV1e6gs82pdurbPyMC+HQFWsIW7ox7c9+GtYZSu pLk5aUKKXQ3axUg9ID5P2fvRfXkNg4o3f27G3Zq5FBcMl3SnA0zlYgfcEDPpHDEoZi5X jJvwI9QIRh69oXyjpz2OnZA5X5YRxBlABt7KQOdi6d6QAc9WzUkIF1RBtnh74GYxwnCf VPWCVdYLUYlMTa5IeNJvZ7I5hP7lnLZFMGJ+Dqom7QFtkMThd0pXHIVSQU6txmHWiDzf LGXA== 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=IGhoZooqadODHcw0sKitGtx/x1DrGCR27u12EaEc5Bk=; fh=HbRmZS+XZk9IdI9bC1USVLouzBEe1WLRItFSyknhPLs=; b=Xbonuf6CV2aC60LRHLDxv1KbieVFaikdfGXrAf9wpT1q805hnL4+Cg6Gcx4oefoZYG GhfyImoiMtfO17894kvP/asIexFQufrXP4FpIYIofDOx5GWSX7nDyjBItTr+ZwcqBGGM 7rq75yt9yDHyConWC7R+9/EfvEoPmHNFx1bIYqH/bwLMoZWNf96puLnwmVp4eT/iBL8X 292t760hjSuX3fZS1YK2k4X0Lu65ny/6vGygN4+M3g4X43vauWvW6DB7JGW9G4RkI2AW GTZ6pT2hFyAC2qoiM68pUNWP3VZTGBBnZyE1ICU8EIiPq3gqKdGj6T76kuutlJaruWyi WSyg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=u3QTOXFm; 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 s12-20020a056402164c00b005233d0a02a7si1752759edx.645.2023.08.07.16.51.36; Mon, 07 Aug 2023 16:52:02 -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=u3QTOXFm; 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 S231338AbjHGXCd (ORCPT + 99 others); Mon, 7 Aug 2023 19:02:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40696 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231163AbjHGXCM (ORCPT ); Mon, 7 Aug 2023 19:02:12 -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 E43691FC6 for ; Mon, 7 Aug 2023 16:01:49 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-583f048985bso65662307b3.2 for ; Mon, 07 Aug 2023 16:01:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1691449308; x=1692054108; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=IGhoZooqadODHcw0sKitGtx/x1DrGCR27u12EaEc5Bk=; b=u3QTOXFm9OBaQwq6T7BIR0Q+bo9nIq4PtS/Gtd/NlHHiWA75p5FU8hLg8GrlR6JTSv nrusB4nabH9ryjjrQ7I4j9ROJwbZwrwaKsa8CrPgwuOHhP50Y2KDNbdKPKpX/fHRxy81 G8RPWfOA4YE/Udx3DhY8BK1fLBC2SonctbBdOi6o84iJ+7a8JiOh30rt/h754ZQc7yWk LP4WzrMp9p9au1SC/1Ig3p5XCFuAMSYSvdPpx8/fYaKAOITQKfm/VVAGfP5UzJ/k0fUc apGgcpo18O21U9BdUL1r9W5BQRH6jcGHUToKZe3ZTopV/Wh3lIbtQjTKs60PD2anls+p 7EsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691449308; x=1692054108; 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=IGhoZooqadODHcw0sKitGtx/x1DrGCR27u12EaEc5Bk=; b=ZYYQNDkwKcqbo6y0GIIA+DfBFVEP6YMRwvFXTRbi0qJUd0CaowoTiu8eY10msJPfvp yi1pQK2n9sdvkgYOfSgjPC5jt2WpvFDdK5lC6Xy2ZD85PJ8EYI5+4iVN61WKuOwQJ5JJ cfp8Pw0Mm6mDlwnx2u89HbWQVbr7TIKsrnTOz43OxAAsRX0OKw4gqQi+LuoP1pXS8UQ/ NGnmzFeivsU3jMyBX4/SPNK+IM2XDxrtreji6XxuetdKRqBIefZrY7x8ZM5NsRUhDzCM Gmbz/66yFO0/idQSTdYnOiskP7NK/7Ps12E24xrOHL7N12xNgwZpkBgtnWPJ6R3IFxd8 eDig== X-Gm-Message-State: AOJu0Yw45pHfh4bEC7ro1ytOzBIIzfzdsy4ZvhEq2N05ffQ/NRrQN0X9 KExX+z2DakZTZ30wlFfmnvZxq3mFdncmnvX5sw== X-Received: from ackerleytng-ctop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:13f8]) (user=ackerleytng job=sendgmr) by 2002:a25:8052:0:b0:d3d:74b6:e085 with SMTP id a18-20020a258052000000b00d3d74b6e085mr61692ybn.8.1691449308117; Mon, 07 Aug 2023 16:01:48 -0700 (PDT) Date: Mon, 7 Aug 2023 23:01:13 +0000 In-Reply-To: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.41.0.640.ga95def55d0-goog Message-ID: <6244c632343298a1ee785a20f3eb6d6b1f3d3e0a.1691446946.git.ackerleytng@google.com> Subject: [RFC PATCH 10/11] KVM: selftests: Generalize migration functions from sev_migrate_tests.c 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: 1773616310038370898 X-GMAIL-MSGID: 1773616310038370898 These functions will be used in private (guest mem) migration tests. Signed-off-by: Ackerley Tng --- .../selftests/kvm/include/kvm_util_base.h | 13 +++++ .../selftests/kvm/x86_64/sev_migrate_tests.c | 48 +++++++------------ 2 files changed, 30 insertions(+), 31 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util_base.h b/tools/testing/selftests/kvm/include/kvm_util_base.h index 868925b26a7b..af6ebead5bc3 100644 --- a/tools/testing/selftests/kvm/include/kvm_util_base.h +++ b/tools/testing/selftests/kvm/include/kvm_util_base.h @@ -331,6 +331,19 @@ static inline void vm_enable_cap(struct kvm_vm *vm, uint32_t cap, uint64_t arg0) vm_ioctl(vm, KVM_ENABLE_CAP, &enable_cap); } +static inline int __vm_migrate_from(struct kvm_vm *dst, struct kvm_vm *src) +{ + return __vm_enable_cap(dst, KVM_CAP_VM_MOVE_ENC_CONTEXT_FROM, src->fd); +} + +static inline void vm_migrate_from(struct kvm_vm *dst, struct kvm_vm *src) +{ + int ret; + + ret = __vm_migrate_from(dst, src); + TEST_ASSERT(!ret, "Migration failed, ret: %d, errno: %d\n", ret, errno); +} + static inline void vm_set_memory_attributes(struct kvm_vm *vm, uint64_t gpa, uint64_t size, uint64_t attributes) { diff --git a/tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c b/tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c index c7ef97561038..cee8219fe8d2 100644 --- a/tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c +++ b/tools/testing/selftests/kvm/x86_64/sev_migrate_tests.c @@ -80,20 +80,6 @@ static struct kvm_vm *aux_vm_create(bool with_vcpus) return vm; } -static int __sev_migrate_from(struct kvm_vm *dst, struct kvm_vm *src) -{ - return __vm_enable_cap(dst, KVM_CAP_VM_MOVE_ENC_CONTEXT_FROM, src->fd); -} - - -static void sev_migrate_from(struct kvm_vm *dst, struct kvm_vm *src) -{ - int ret; - - ret = __sev_migrate_from(dst, src); - TEST_ASSERT(!ret, "Migration failed, ret: %d, errno: %d\n", ret, errno); -} - static void test_sev_migrate_from(bool es) { struct kvm_vm *src_vm; @@ -105,13 +91,13 @@ static void test_sev_migrate_from(bool es) dst_vms[i] = aux_vm_create(true); /* Initial migration from the src to the first dst. */ - sev_migrate_from(dst_vms[0], src_vm); + vm_migrate_from(dst_vms[0], src_vm); for (i = 1; i < NR_MIGRATE_TEST_VMS; i++) - sev_migrate_from(dst_vms[i], dst_vms[i - 1]); + vm_migrate_from(dst_vms[i], dst_vms[i - 1]); /* Migrate the guest back to the original VM. */ - ret = __sev_migrate_from(src_vm, dst_vms[NR_MIGRATE_TEST_VMS - 1]); + ret = __vm_migrate_from(src_vm, dst_vms[NR_MIGRATE_TEST_VMS - 1]); TEST_ASSERT(ret == -1 && errno == EIO, "VM that was migrated from should be dead. ret %d, errno: %d\n", ret, errno); @@ -133,7 +119,7 @@ static void *locking_test_thread(void *arg) for (i = 0; i < NR_LOCK_TESTING_ITERATIONS; ++i) { j = i % NR_LOCK_TESTING_THREADS; - __sev_migrate_from(input->vm, input->source_vms[j]); + __vm_migrate_from(input->vm, input->source_vms[j]); } return NULL; @@ -170,7 +156,7 @@ static void test_sev_migrate_parameters(void) vm_no_vcpu = vm_create_barebones(); vm_no_sev = aux_vm_create(true); - ret = __sev_migrate_from(vm_no_vcpu, vm_no_sev); + ret = __vm_migrate_from(vm_no_vcpu, vm_no_sev); TEST_ASSERT(ret == -1 && errno == EINVAL, "Migrations require SEV enabled. ret %d, errno: %d\n", ret, errno); @@ -184,25 +170,25 @@ static void test_sev_migrate_parameters(void) sev_ioctl(sev_es_vm_no_vmsa->fd, KVM_SEV_ES_INIT, NULL); __vm_vcpu_add(sev_es_vm_no_vmsa, 1); - ret = __sev_migrate_from(sev_vm, sev_es_vm); + ret = __vm_migrate_from(sev_vm, sev_es_vm); TEST_ASSERT( ret == -1 && errno == EINVAL, "Should not be able migrate to SEV enabled VM. ret: %d, errno: %d\n", ret, errno); - ret = __sev_migrate_from(sev_es_vm, sev_vm); + ret = __vm_migrate_from(sev_es_vm, sev_vm); TEST_ASSERT( ret == -1 && errno == EINVAL, "Should not be able migrate to SEV-ES enabled VM. ret: %d, errno: %d\n", ret, errno); - ret = __sev_migrate_from(vm_no_vcpu, sev_es_vm); + ret = __vm_migrate_from(vm_no_vcpu, sev_es_vm); TEST_ASSERT( ret == -1 && errno == EINVAL, "SEV-ES migrations require same number of vCPUS. ret: %d, errno: %d\n", ret, errno); - ret = __sev_migrate_from(vm_no_vcpu, sev_es_vm_no_vmsa); + ret = __vm_migrate_from(vm_no_vcpu, sev_es_vm_no_vmsa); TEST_ASSERT( ret == -1 && errno == EINVAL, "SEV-ES migrations require UPDATE_VMSA. ret %d, errno: %d\n", @@ -355,14 +341,14 @@ static void test_sev_move_copy(void) sev_mirror_create(mirror_vm, sev_vm); - sev_migrate_from(dst_mirror_vm, mirror_vm); - sev_migrate_from(dst_vm, sev_vm); + vm_migrate_from(dst_mirror_vm, mirror_vm); + vm_migrate_from(dst_vm, sev_vm); - sev_migrate_from(dst2_vm, dst_vm); - sev_migrate_from(dst2_mirror_vm, dst_mirror_vm); + vm_migrate_from(dst2_vm, dst_vm); + vm_migrate_from(dst2_mirror_vm, dst_mirror_vm); - sev_migrate_from(dst3_mirror_vm, dst2_mirror_vm); - sev_migrate_from(dst3_vm, dst2_vm); + vm_migrate_from(dst3_mirror_vm, dst2_mirror_vm); + vm_migrate_from(dst3_vm, dst2_vm); kvm_vm_free(dst_vm); kvm_vm_free(sev_vm); @@ -384,8 +370,8 @@ static void test_sev_move_copy(void) sev_mirror_create(mirror_vm, sev_vm); - sev_migrate_from(dst_mirror_vm, mirror_vm); - sev_migrate_from(dst_vm, sev_vm); + vm_migrate_from(dst_mirror_vm, mirror_vm); + vm_migrate_from(dst_vm, sev_vm); kvm_vm_free(mirror_vm); kvm_vm_free(dst_mirror_vm); 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; }