Received: by 2002:a05:7412:3784:b0:e2:908c:2ebd with SMTP id jk4csp2428474rdb; Wed, 4 Oct 2023 00:13:01 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEprl+KeUTqLvYzWVuSbC9nnacuMjnKo4NE0FG7sCM34C6kjJO6H8L7uFTthFyD6UaUVnGB X-Received: by 2002:a17:903:110d:b0:1c6:17a4:afae with SMTP id n13-20020a170903110d00b001c617a4afaemr1991799plh.4.1696403581472; Wed, 04 Oct 2023 00:13:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696403581; cv=none; d=google.com; s=arc-20160816; b=LiNdL5Zr+54gEVCHgdX/GP4+HPJdKNF8JkL5JEDfOL6NvWXEIWuhb1xvGvDAqSmbX7 0t/yWtJbf5pVdgNREMb9zMrMXU4BMN7wozaj+v1PtrY1u2oYL5j6GXg4i9QZ5L6/SBUB zWOKHbNJUWscaCItlPhlSew+WbP8P84usUgXhLd+lncHqgyMQYeTBhiazIZSIRMxP0sy cc8zvpXKhqwZiAFFQPHSovLdnmGRcTnCRkRE7oRgbaICbXakGpoM0IMtkOkxktX+B7hx hbbNZIfci9Y6Amxke933Fx396csuCDwkcVdNMelIjPlCxvXoxqCRSGus3FGwMbyYA7wd nhhA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=ogdl0KSnIs9uHB330mT8IMZQGmhaWVWoV431L0BUr2w=; fh=34l78sZxR7pVnZXmwQNDr2LkXsawffIl7CCTurwkWBc=; b=DIN6oTG/DVcit5LgL25LNqZf/TneuBek4wdQfduz9npo1L8I6mqlkt3/l7EoHgWrtW UdGSXWi6mQwLVG+ULf0Hz8YUtAdxsboiGkaqm3fhARuHOzULIUUpAytBQYRjQkM6O2gL gLo+D8D7jHcZwxGszmEo775mRruq1MVaSXth+fONsHIgOnYZhEOsaubqIWoGuju6uyiE FxKDvtZ4y6AZxrSLrCII2ym2yNj+aI144UBnU9tI+0qPErVqUKvCchBPnZ2ev9L5J67r reR7sKukVc+38m+co0RaLdc02/b4kRQ7lx70L6RDFZ1Vani6VB5GP/o2XiIz4K/JBuW9 RVjQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=BaeLbA1P; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id s20-20020a17090330d400b001bda18ea0e1si2979100plc.80.2023.10.04.00.13.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Oct 2023 00:13:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=BaeLbA1P; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 6ECAD832C2CB; Wed, 4 Oct 2023 00:13:00 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241371AbjJDHM5 (ORCPT + 99 others); Wed, 4 Oct 2023 03:12:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39724 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232624AbjJDHM4 (ORCPT ); Wed, 4 Oct 2023 03:12:56 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7A3F5AD for ; Wed, 4 Oct 2023 00:12:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1696403522; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=ogdl0KSnIs9uHB330mT8IMZQGmhaWVWoV431L0BUr2w=; b=BaeLbA1PFZfkEpZAeU9VFN/r3mDG9E9Ai1oM6gcANddX82mOa0kurwyY4BLsmWdtnvSEb9 PAodvdOEdRz1Q8ebRm4EdulDdpob8t1fzNrPdVBG7wMLsovIrnb2dm5ngmSugdJJmc8H8g L2Zd9iKfWtzZIn4yIkw5Os2lXfJCPBM= Received: from mail-pl1-f198.google.com (mail-pl1-f198.google.com [209.85.214.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-427-n9m96jVtMGmQGJkQmGlCGA-1; Wed, 04 Oct 2023 03:11:59 -0400 X-MC-Unique: n9m96jVtMGmQGJkQmGlCGA-1 Received: by mail-pl1-f198.google.com with SMTP id d9443c01a7336-1c746bccbdcso16716545ad.2 for ; Wed, 04 Oct 2023 00:11:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696403518; x=1697008318; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=ogdl0KSnIs9uHB330mT8IMZQGmhaWVWoV431L0BUr2w=; b=tUuiZN3qpQjLE4DG3uc6uVKZ37c5FwmXxSjljNb14tjCRz94xiuSEVGMIOog7qf4sV GiztnqzKg1PnQY7YSJ/pg2sI5KSysqO1t0wp/2g8E+fR1GDvP+RuJDmjAPbc6JKfGZvc xCjPBUgznizzBik2uR2rPogWY0KZxoHfUs9w04rUQnOHKVj9zitN4IwJUR5DJLgACGNK 9Jul6Z1pUoKbEsnivICXDQEAdN9BMzn8PW6fUNxNRTGRarfYvx27X06WI5e1JQjI486U O2qEKIYhkGrBJecRPI/wcZUxiypDzvnioyx3Y/JmpeD0kumi8bPfXYi+WBvmC1XGuY8r HK5g== X-Gm-Message-State: AOJu0YyDlL+j7XOOKXjZJ61mo82BbvZW4nlHk3uOSlTaaxyHO48jskqc uJik4vV5OiB62mOte9eUval8CCxrfTtmAEk0EKeKdgDQjJ6lm3z0k+tKSdsWA0Z0nWMwbAclMS3 lj0yeOt3xL8DDjMIt4qQZWh+9rCDVBp7x X-Received: by 2002:a17:902:ba8c:b0:1c6:28f6:954a with SMTP id k12-20020a170902ba8c00b001c628f6954amr1694540pls.64.1696403518408; Wed, 04 Oct 2023 00:11:58 -0700 (PDT) X-Received: by 2002:a17:902:ba8c:b0:1c6:28f6:954a with SMTP id k12-20020a170902ba8c00b001c628f6954amr1694518pls.64.1696403518099; Wed, 04 Oct 2023 00:11:58 -0700 (PDT) Received: from redhat.com ([2804:1b3:a803:bd00:96e0:9a9b:ef6c:8085]) by smtp.gmail.com with ESMTPSA id c1-20020a170903234100b001c728609574sm2864745plh.6.2023.10.04.00.11.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Oct 2023 00:11:57 -0700 (PDT) Date: Wed, 4 Oct 2023 04:11:52 -0300 From: Leonardo Bras To: Sean Christopherson Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, Paolo Bonzini , Shuah Khan , Nathan Chancellor , Nick Desaulniers , linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org, llvm@lists.linux.dev, Tyler Stachecki Subject: Re: [PATCH 0/5] KVM: x86: Fix breakage in KVM_SET_XSAVE's ABI Message-ID: References: <20230928001956.924301-1-seanjc@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230928001956.924301-1-seanjc@google.com> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_NONE 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Wed, 04 Oct 2023 00:13:00 -0700 (PDT) On Wed, Sep 27, 2023 at 05:19:51PM -0700, Sean Christopherson wrote: > Rework how KVM limits guest-unsupported xfeatures to effectively hide > only when saving state for userspace (KVM_GET_XSAVE), i.e. to let userspace > load all host-supported xfeatures (via KVM_SET_XSAVE) irrespective of > what features have been exposed to the guest. Ok, IIUC your changes provide: - KVM_GET_XSAVE will return only guest-supported xfeatures - KVM_SET_XSAVE will allow user to set any xfeatures supported by host Is that correct? > > The effect on KVM_SET_XSAVE was knowingly done by commit ad856280ddea > ("x86/kvm/fpu: Limit guest user_xfeatures to supported bits of XCR0"): > > As a bonus, it will also fail if userspace tries to set fpu features > (with the KVM_SET_XSAVE ioctl) that are not compatible to the guest > configuration. Such features will never be returned by KVM_GET_XSAVE > or KVM_GET_XSAVE2. > > Peventing userspace from doing stupid things is usually a good idea, but in > this case restricting KVM_SET_XSAVE actually exacerbated the problem that > commit ad856280ddea was fixing. As reported by Tyler, rejecting KVM_SET_XSAVE > for guest-unsupported xfeatures breaks live migration from a kernel without > commit ad856280ddea, to a kernel with ad856280ddea. I.e. from a kernel that > saves guest-unsupported xfeatures to a kernel that doesn't allow loading > guest-unuspported xfeatures. So this patch is supposed to fix migration of VM from a host with pre-ad856280ddea (OLD) kernel to a host with ad856280ddea + your set(NEW). Right? Let's get the scenario here, where all machines are the same: 1 - VM created on OLD kernel with a host-supported xfeature F, which is not guest supported. 2 - VM is migrated to a NEW kernel/host, and KVM_SET_XSAVE xfeature F. 3 - VM will be migrated to another host, qemu requests KVM_GET_XSAVE, which returns only guest-supported xfeatures, and this is passed to next host 4 - VM will be started on 3rd host with guest-supported xfeatures, meaning xfeature F is filtered-out, which is not good, because the VM will have less features compared to boot. In fact, I notice something would possibly happen between 2 and 3, since qemu will run KVM_GET_XSAVE at kvm_cpu_synchronize_state() and KVM_SET_XSAVE at kvm_cpu_exec(), which happens quite often (when vcpu stops / resumes for some reason). Also, even if I got something wrong, and for some reason qemu will be able to store the original VM xfeatures between migrations, we have the original issue ad856280ddea was dealing with: newer machines -> older machines migration: 1 - User gets a VM from an OLD kernel, with a newer host (more xfeatures). 2 - User migrates VM to NEW kernel, and we suppose qemu stores original xfeatures (it works). Migration can occur to newer or same gen hosts. 3 - At some point, if migration is attempted to an older host (less xfeatures), qemu will abort the VM. > > To make matters even worse, QEMU doesn't terminate if KVM_SET_XSAVE fails, > and so the end result is that the live migration results (possibly silent) > guest data corruption instead of a failed migration. And this is something that really needs to be fixed in QEMU side. > > Patch 1 refactors the FPU code to let KVM pass in a mask of which xfeatures > to save, patch 2 fixes KVM by passing in guest_supported_xcr0 instead of > modifying user_xfeatures directly. At my current understanding of this patchset, I would not recomment merging it, as it would introduce a lot of undesired behaviors. Please let me know if I got something wrong, so I can review it again. Thanks! Leo > > Patches 3-5 are regression tests. > > I have no objection if anyone wants patches 1 and 2 squashed together, I > split them purely to make review easier. > > Note, this doesn't fix the scenario where a guest is migrated from a "bad" > to a "good" kernel and the target host doesn't support the over-saved set > of xfeatures. I don't see a way to safely handle that in the kernel without > an opt-in, which more or less defeats the purpose of handling it in KVM. > > Sean Christopherson (5): > x86/fpu: Allow caller to constrain xfeatures when copying to uabi > buffer > KVM: x86: Constrain guest-supported xfeatures only at KVM_GET_XSAVE{2} > KVM: selftests: Touch relevant XSAVE state in guest for state test > KVM: selftests: Load XSAVE state into untouched vCPU during state test > KVM: selftests: Force load all supported XSAVE state in state test > > arch/x86/include/asm/fpu/api.h | 3 +- > arch/x86/kernel/fpu/core.c | 5 +- > arch/x86/kernel/fpu/xstate.c | 12 +- > arch/x86/kernel/fpu/xstate.h | 3 +- > arch/x86/kvm/cpuid.c | 8 -- > arch/x86/kvm/x86.c | 37 +++--- > .../selftests/kvm/include/x86_64/processor.h | 23 ++++ > .../testing/selftests/kvm/x86_64/state_test.c | 110 +++++++++++++++++- > 8 files changed, 168 insertions(+), 33 deletions(-) > > > base-commit: 5804c19b80bf625c6a9925317f845e497434d6d3 > -- > 2.42.0.582.g8ccd20d70d-goog >