From patchwork Sat Jul 29 01:15:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 12799 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:918b:0:b0:3e4:2afc:c1 with SMTP id s11csp827463vqg; Fri, 28 Jul 2023 20:42:40 -0700 (PDT) X-Google-Smtp-Source: APBJJlGTY192h1AnaPao9ktsrf8qAydJdfLQ4XrGkTQbm4CXAN98F2MiPph2YFHEi6+Vj7LsBDde X-Received: by 2002:a17:902:d503:b0:1b1:99c9:8ce1 with SMTP id b3-20020a170902d50300b001b199c98ce1mr3588000plg.51.1690602160254; Fri, 28 Jul 2023 20:42:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690602160; cv=none; d=google.com; s=arc-20160816; b=nrSJT5OTSGGEhoPhFD+84Ul9XGG+5vxvsbNukX8Ixl/5vt29ISRCbz1EMAPhsK1R3W vGDlVZV6tFkemWn4n86WirMBP4HO39YST4GQ/gAwDoruAsbfFmstBDVyzGBD0UsiWVdW sQbYMmnK+B6MJzBjyQHUDHk82OoM5zcErsMOAUWfAg/qRv1+kgSGk2r+tYkcyFiF5PGu 3WpO3k9T+y3Rt9IBNX0KeO6oYo0YChOXhmjKvtuWtIk2lmxYg2c+mHo+Qp3OJx8EalSx kcweRLNf4GNPerPcjU4FVKtrplSMYWU1sRWEDrcgP+HioWC1T0rgS/adxGQvqBJdmGhW B1vw== 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=nJkpJZhQugJruq9ZGSna/diiNivrwzOkBsEj4Xcrf14=; fh=8Mc5uyvARtESo7rBGdZxKoor3rxsuqGYA/iIBYLTTUU=; b=KMg4jU1nffdNhLJx6LamqegGFdWrTWPJEVkfnMMwTroIVZKqRZt6Y7EAv5zAP/Er3Z C3jyoTIGewgf429YDA1VoGIQjZOurUeAc6jwOG0ZULbXfyPylltVwfJpNsyXExitjDGU 26WOgniQnBotVFoID08SbnSFGJxd+6boGnyp0PLpONmgr7tlvXNFJ+Tv+GQpWMaOnDnB 8EsqvDsc3AyVqJEoWHxD4JTG1sx1gMxjC7wWbT03hTJNHj779YzJMcb7H0u9YhYTYUbM PRwL2kZ8L0aerc97cAWI3ozGzq3cf0FlD/W01e11/JEUjtu6NqOrwJzuDMNVETkDbvrV P9Rg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=3jil1H5C; 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 e9-20020a17090301c900b001b7fd6f95f6si1231881plh.515.2023.07.28.20.42.26; Fri, 28 Jul 2023 20:42:40 -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=3jil1H5C; 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 S233585AbjG2BQP (ORCPT + 99 others); Fri, 28 Jul 2023 21:16:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35776 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231659AbjG2BQO (ORCPT ); Fri, 28 Jul 2023 21:16:14 -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 2A3BE3AA8 for ; Fri, 28 Jul 2023 18:16:13 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-585f254c41aso4504497b3.1 for ; Fri, 28 Jul 2023 18:16:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1690593372; x=1691198172; h=cc:to:from:subject:message-id:mime-version:date:reply-to:from:to:cc :subject:date:message-id:reply-to; bh=nJkpJZhQugJruq9ZGSna/diiNivrwzOkBsEj4Xcrf14=; b=3jil1H5CD9WwizZiZJ5IA2BD9pbWShS/TS6cFpS6ArxZOsfmKXhUSZrAsq/cLYuygd bEPFvXQRgI6MX1uxLhUPOv8mdq+Ji9kI3+yI6qlEl3xXLVi1ikhnTyWnXjAj577Wfomi rE72n/3qt7uBML4Yd4I3/EV8DNVwZmJfz+d3jCH4+IY5Jhl+AWU8NHe04wWTEzxvSaSy 53dV5rfFQr5Gf0hpmexY89nHB07Y+gu7tPGJcG9bEKgQPwsvUR69Oeq2lAFF9iEFwdAh sWlWa72ygUDmrEm+2OKA8ZAWyq54Zjh2R0V8uFE1O7e+UHlgOR8ZpuxsX/B9/KErDsMj bElg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690593372; x=1691198172; 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=nJkpJZhQugJruq9ZGSna/diiNivrwzOkBsEj4Xcrf14=; b=M03XA919pPgmxxLPlmevG86JjS6gK7a9vedOYQJ6fSoRjIZE8Pm5eBP4V6GSQDdXWD 5uY4ufwhDSNdYbiUAeJk5pLn99U4WHGpcn6oW6inSJDhsRTwlSUE32KVxjFGYrKj48/m c9AgQtjeKMf2vAbdAlkQwX94e4+3jOfp2ygOuYyBpTH19a5qUVsTH5ulcIpqotzjQa8d r07cIYZC7rC+Ta965t+xGBpDuxgHxfTQY6Ew4ufcCCvahSmjqRdfrvfjgHgllT+S00o1 dcuJj2YEu3eUwjHtaxH22bJ4jFiBjz1D3e/DOdlN6+k8iOd96PRgT1cCXOpZUxJKkb1c cMfQ== X-Gm-Message-State: ABy/qLZhw2uPBL4keW/HhwvZ0LlGiYT2dt6m8mh8seFq6kcR14J4qP2l 1Ns67uw79WgOlVV59bdzZt9h8ZefDAU= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:7401:0:b0:d06:cbd:1f3e with SMTP id p1-20020a257401000000b00d060cbd1f3emr16966ybc.3.1690593372414; Fri, 28 Jul 2023 18:16:12 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 28 Jul 2023 18:15:47 -0700 Mime-Version: 1.0 X-Mailer: git-send-email 2.41.0.487.g6d72f3e995-goog Message-ID: <20230729011608.1065019-1-seanjc@google.com> Subject: [PATCH v2 00/21] KVM: x86: Add "governed" X86_FEATURE framework From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Vitaly Kuznetsov Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Maxim Levitsky 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_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,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: 1772724850670167508 X-GMAIL-MSGID: 1772724850670167508 Add a framework to manage and cache KVM-governed features, i.e. CPUID based features that require explicit KVM enabling and/or need to be queried semi-frequently by KVM. The idea originally came up in the context of the architectural LBRs series as a way to avoid querying guest CPUID in hot paths without needing a dedicated flag, but as evidenced by the shortlog, the most common usage is to handle the ever- growing list of SVM features that are exposed to L1. The first six patches are fixes and cleanups related to TSC scaling. nSVM has WARN_ONs that can be triggered by userspace at will, and the code is a bit crusty. They aren't directly related to the governed stuff, I stumbled upon the issues they fix when staring at the patch to convert "TSC scaling enabled". I included them here mainly to avoid code conflicts. I'm hoping all of this can go into 6.6, e.g. so that CET support can build on guest_can_use(), but I can always grab the TSC patches for 6.6 if the governed stuff needs more time. Note, I still don't like the name "governed", but no one has suggested anything else, let alone anything better :-) v2: - Add patches to clean up TSC scaling. - Add a comment explaining the virtual VMLOAD/VMLAVE vs. SYSENTER on Intel madness. - Use a governed feature for X86_FEATURE_VMX. - Incorporate KVM capabilities into the main check-and-set helper. [Chao] v1: https://lore.kernel.org/all/20230217231022.816138-1-seanjc@google.com Sean Christopherson (21): KVM: nSVM: Check instead of asserting on nested TSC scaling support KVM: nSVM: Load L1's TSC multiplier based on L1 state, not L2 state KVM: nSVM: Use the "outer" helper for writing multiplier to MSR_AMD64_TSC_RATIO KVM: SVM: Clean up preemption toggling related to MSR_AMD64_TSC_RATIO KVM: x86: Always write vCPU's current TSC offset/ratio in vendor hooks KVM: nSVM: Skip writes to MSR_AMD64_TSC_RATIO if guest state isn't loaded KVM: x86: Add a framework for enabling KVM-governed x86 features KVM: x86/mmu: Use KVM-governed feature framework to track "GBPAGES enabled" KVM: VMX: Recompute "XSAVES enabled" only after CPUID update KVM: VMX: Check KVM CPU caps, not just VMX MSR support, for XSAVE enabling KVM: VMX: Rename XSAVES control to follow KVM's preferred "ENABLE_XYZ" KVM: x86: Use KVM-governed feature framework to track "XSAVES enabled" KVM: nVMX: Use KVM-governed feature framework to track "nested VMX enabled" KVM: nSVM: Use KVM-governed feature framework to track "NRIPS enabled" KVM: nSVM: Use KVM-governed feature framework to track "TSC scaling enabled" KVM: nSVM: Use KVM-governed feature framework to track "vVM{SAVE,LOAD} enabled" KVM: nSVM: Use KVM-governed feature framework to track "LBRv enabled" KVM: nSVM: Use KVM-governed feature framework to track "Pause Filter enabled" KVM: nSVM: Use KVM-governed feature framework to track "vGIF enabled" KVM: nSVM: Use KVM-governed feature framework to track "vNMI enabled" KVM: x86: Disallow guest CPUID lookups when IRQs are disabled arch/x86/include/asm/kvm_host.h | 23 ++++++++- arch/x86/include/asm/vmx.h | 2 +- arch/x86/kvm/cpuid.c | 34 ++++++++++++++ arch/x86/kvm/cpuid.h | 46 ++++++++++++++++++ arch/x86/kvm/governed_features.h | 21 +++++++++ arch/x86/kvm/mmu/mmu.c | 20 ++------ arch/x86/kvm/svm/nested.c | 57 ++++++++++++---------- arch/x86/kvm/svm/svm.c | 81 ++++++++++++++++++-------------- arch/x86/kvm/svm/svm.h | 18 ++----- arch/x86/kvm/vmx/capabilities.h | 2 +- arch/x86/kvm/vmx/hyperv.c | 2 +- arch/x86/kvm/vmx/nested.c | 13 ++--- arch/x86/kvm/vmx/nested.h | 2 +- arch/x86/kvm/vmx/vmx.c | 81 +++++++++++++++----------------- arch/x86/kvm/vmx/vmx.h | 3 +- arch/x86/kvm/x86.c | 9 ++-- 16 files changed, 261 insertions(+), 153 deletions(-) create mode 100644 arch/x86/kvm/governed_features.h base-commit: fdf0eaf11452d72945af31804e2a1048ee1b574c