Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp3052713rdh; Thu, 28 Sep 2023 00:38:35 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG/Vqs1ihK1UIh4EJrNp2eMRGU8/cEJU5oEqlUYRzuooPyiOuXbNqmdD81iIvZoxmvtSsT4 X-Received: by 2002:a17:902:f682:b0:1c3:6705:bcee with SMTP id l2-20020a170902f68200b001c36705bceemr467261plg.57.1695886715403; Thu, 28 Sep 2023 00:38:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695886715; cv=none; d=google.com; s=arc-20160816; b=CjER3rxWSoBcAtPnraiX6ef9JWKxFNMl2rUI6pQU+9FKw0IVBBdG0zC8mMyuQPtd8c IkWzbCrrC9yhdLilbN2xQemfHshp6ZaxjkeA9/biFLMpDpjx2GaSw9ztX0NiXzPVFkbR ruCgZ0IZdpzL8lGVM5ZY9o8b33fqya9BPAH9FL651k7q6+5Nc5l367jdkG2GocrrteQ8 BWXe8DqayT9ETWZjEn5N7JQrioQBC85yvrDzpVpX5GsPy1s9lVEM5XL1t7Cw/Cz5A0PD Xqpn3SX3mNkfnKs5IHS19AxZyI6vZ5fnbc2waEDbIcmqPD4qebd8cYQou0Lf/KpuFXeG wBPw== 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:mime-version:date :reply-to:dkim-signature; bh=JIhlczPTAFSXJtuz0kr4uNehhoedtrBaBEaYSQ3/D3w=; fh=zG/Z67+2URffyoJpoy7L2CpNn2n11MMeaG6nGQKLmF8=; b=yopLGyiTzb9120eSYvEjjdrxd4LxPG5bL9LQyxpsHAmM9RbD0hkvTlyjT4UI96VHlb o15beYFcN6vD7+EhcCrQn0hbbuVU3ES7zC+dysL1kezujio0M01C5Q/8IvWi6RDLzDE+ sPDxhU+uOgHFyQK0TzJUaEr1n1fZdtKY6hrKQeJ6L/AHiO8vJFXqwc+dBSw0RHxL1Eb8 ay2rbQtEdHOkfQIrNgxSuEohFdqldMjToqTAaJNQcSQXDmtAWhdfNH0Xu1Fu7snhx4UI y8jbjPPp5OBgWOgI21Xi687EkFFPOUVh40mL9YJZVpVby8Smcp6Yrcvsew6mrNfoglBy q75Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=mdqi3CV6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id jz11-20020a170903430b00b001c61512f2a3si10089173plb.362.2023.09.28.00.38.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Sep 2023 00:38:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=mdqi3CV6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 9663282D7561; Wed, 27 Sep 2023 17:20:07 -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 S229437AbjI1AUC (ORCPT + 99 others); Wed, 27 Sep 2023 20:20:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34074 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229445AbjI1AUB (ORCPT ); Wed, 27 Sep 2023 20:20:01 -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 F3CB8F5 for ; Wed, 27 Sep 2023 17:19:59 -0700 (PDT) Received: by mail-pj1-x104a.google.com with SMTP id 98e67ed59e1d1-27763c2c27dso8903143a91.2 for ; Wed, 27 Sep 2023 17:19:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695860399; x=1696465199; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:reply-to:from:to:cc :subject:date:message-id:reply-to; bh=JIhlczPTAFSXJtuz0kr4uNehhoedtrBaBEaYSQ3/D3w=; b=mdqi3CV6ogd+sLjUE0adY1tjTK8dTH8aEdvxS0QxhkeHAFu0CEAbbBC4MFijtqP1Ly sjZTGe4VhUUb/M9MK4TbEOatIHvLNoIhsPHiP350mptcWfo4FF6STM6mL/vMFyLsSZyT X7mnnqlnhUEEmmE4L+2M7AdtXBciHMTjvxlqNP2WFYUccCbdj2AnNTB2KQ+x1ujJZcnr njQZL90vUj2nrWJOTZk4TWrwQ3bo5KBupnlCZ83mzkdvD6qdy+qZcubw71mooKyD+WTp KimIF/iFxfNWmiysn9zEBCwU46D+J9XKcW+HSFh4ADA1egulhRU8BSKJv9NvKUv5n9Gu T7hA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695860399; x=1696465199; h=cc:to:from:subject:message-id:mime-version:date:reply-to :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=JIhlczPTAFSXJtuz0kr4uNehhoedtrBaBEaYSQ3/D3w=; b=YhVPYa1IiTz8pkdR/tEePv7u4s2LVIPzLNP7ekqYDdhCLNxbCXBuM0fbN/9v0OetOv BDkpMUo3r3BywFcy4c/TCKkRZStALCRbUjEGH5DcLlZCsMO2yqW7NzRRDF5EEfzMN9HS BscIl8IhqlI6NNLHE52gNuMNLpmtAaj8fK1GhulHuS5q81LA6/9q/YUHblsnLfjje8t5 9x34aqEUmYAlwcZUE17MHjqweeFjmQlnkjo1eA+HS9mP/lBEtJCWqt2zkgf0HAYzbSOG JQYbEop/GlJmGduTmnsUv+MMqgfnqoyynVRKnltpNS/+3joVexxbvVyrT5HSX5IyxuIf CNvg== X-Gm-Message-State: AOJu0YxFaWz7LCWb1iVec/CLoTpWq1Tbrs7mYBR/R8SXjggD9nkB8Dqm yVTsEl89zvu0M6vP+7RcUTNIOCK0Qlo= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:903:604:b0:1c0:d418:8806 with SMTP id kg4-20020a170903060400b001c0d4188806mr44172plb.12.1695860399477; Wed, 27 Sep 2023 17:19:59 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 27 Sep 2023 17:19:51 -0700 Mime-Version: 1.0 X-Mailer: git-send-email 2.42.0.582.g8ccd20d70d-goog Message-ID: <20230928001956.924301-1-seanjc@google.com> Subject: [PATCH 0/5] KVM: x86: Fix breakage in KVM_SET_XSAVE's ABI From: Sean Christopherson To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, Sean Christopherson , Paolo Bonzini , Shuah Khan , Nathan Chancellor , Nick Desaulniers Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org, llvm@lists.linux.dev, Tyler Stachecki , Leonardo Bras Content-Type: text/plain; charset="UTF-8" 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Wed, 27 Sep 2023 17:20:07 -0700 (PDT) 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. 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. 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. 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. 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