From patchwork Fri Feb 10 00:31:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 5205 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp660446wrn; Thu, 9 Feb 2023 16:33:15 -0800 (PST) X-Google-Smtp-Source: AK7set/4YyiIr4KbOAH4AxZC1HtW1315B15MEt6eqzP0XZBQSeHYZfg043YDx2ui5/f7v9JQZ6YA X-Received: by 2002:a50:d7d9:0:b0:4aa:a767:7982 with SMTP id m25-20020a50d7d9000000b004aaa7677982mr14779772edj.0.1675989195711; Thu, 09 Feb 2023 16:33:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675989195; cv=none; d=google.com; s=arc-20160816; b=odtEd54Kh7UGNw4vV2QclVm11vWCFA9lYHp/b5MPzyeDQ/oAAIbOVY0PJ5ZwrOIXo0 w0WheSDFrhdA0CqI35wiQj3ovYfW9rzAnJvZ0QlSH+wYu65s6gdvTgqmxM0aBiUmXpQT tXZk5iRa5MwaMQnnMr0fK7T7k72tyrsQ/GUIQlG0gLTUJSEYQQpgmuamCY6Q26kFNvCP 9nXJQIA17MMitPIceqIUBcppLwOsq9/W2zwczg2ovuysjFOhI+bnSyTjt5RD0Xhd47Ow YS3RyiDLZ3DK1hDLFGQh/LHI1bKP5wFm1Pe7R+GIGe+fXIf99hiYiOqOWcUcrG/Wzt7u 9oYQ== 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=YFhz51bmO6L+hGiFm94j+Ms1OEhMmWjzfV3eVwNpPHQ=; b=Lw3ySVDXQxRvj1udjlayBan26yLlMe0t3z0b8ng7drRO+tyeZSTpjbrXQEqtQvYK+e Qw2is1nWSjf3F432V4BV2ZGuTwtnLvXocMNSDHK4dK5D9js+pY78Yyp8yFYVLQC61CYw qPvu9kZwnFpRnCTFQ3NtyfEmDs++x+n1iceP3RMmgD95GfrtoFfbtDsts0N5mS8SbH6m ygF/vJMhIIoq/Y5XOMkHwzHeYz1Bg4yUlQq1JoJi3csLsW1PQDrsQE/cXcTQEb9Qi+u3 YGjPCvlN7jF9Fu6sqK47wFHXpX2Ih6gpt0CCMDJ4+BYNnrBkdaT5CocV+xMqNPY/yrHO NrkA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=W5fHjC8O; 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 j4-20020aa7c404000000b004a24ac0a648si4289124edq.79.2023.02.09.16.32.50; Thu, 09 Feb 2023 16:33:15 -0800 (PST) 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=20210112 header.b=W5fHjC8O; 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 S230076AbjBJAb4 (ORCPT + 99 others); Thu, 9 Feb 2023 19:31:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52088 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229585AbjBJAby (ORCPT ); Thu, 9 Feb 2023 19:31:54 -0500 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 108CEEFBB for ; Thu, 9 Feb 2023 16:31:52 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id y125-20020a25c883000000b0086349255277so3463425ybf.8 for ; Thu, 09 Feb 2023 16:31:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:mime-version:date:reply-to:from:to:cc :subject:date:message-id:reply-to; bh=YFhz51bmO6L+hGiFm94j+Ms1OEhMmWjzfV3eVwNpPHQ=; b=W5fHjC8OEI6F3ULmI/sRLPpDuQzOpP13b9ojrI1N7SNPnHQugQG8bdXqm9nPHY58t+ FMPpMvr446bCY4vMbYK6H4/DnnoTESwtnDC9r/GGGO+KLH+csIULiYqLNKE/x9rKI17n 7L7m9HFtrDqFe9e9eVcu6wPWDUja8aaKZIuMV7UnGKR3k8bExOQ2sLN5RtFlowKe5bzj Td9D61ps5o/YQNsLa773+EpQgjaK8hYzwsWD42tNhhXUY1hdd6rUox9SGtwS2CiOISlv KYmrJWlGDHEPsqPBc0iS5PF1i13fzzti8Z/euSG/Bzk8hui8m+j+1z81s6aFRt98H7OU SvOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=YFhz51bmO6L+hGiFm94j+Ms1OEhMmWjzfV3eVwNpPHQ=; b=faESHOIfed73X8tokWH6TR0GQvkKf3T1rwHDju86FBeb+i2U72kngvj5kCtmJHPuNL mByNuO5lt3tFO94OWvQ8HfQS86tFBhxuYKdDIB1eb4kwY/o771189G0SGwQfmkFsA1eY 8+gRa1Uf+z2FjcNTtcDLKeOrYveHfMuguind2SDtEZ2quiSYi5GQmp6Jm3w1EqkhdDbh nFCJZdKkhd2OeRL6BmKg5g5LpMZnqFhI6c9OctnlPaUbN/webq5Mp8+JZzyFSf9BXZeK MMNQ1epkaGi3scNHx+N36omauTtUoc3GTrrIPpQNIILL7EJSb01K6Baw8RSgduShBGjl O7dw== X-Gm-Message-State: AO0yUKWXarwltMrYGS4+4I7cMmknZXDSeziXUTxPh00cgrD1Sq5AxtHy JO3Vvvq6zcycGBYQ86ObnT6eR90rV/Q= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a0d:e808:0:b0:527:b484:aa14 with SMTP id r8-20020a0de808000000b00527b484aa14mr1691127ywe.263.1675989111337; Thu, 09 Feb 2023 16:31:51 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Feb 2023 00:31:27 +0000 Mime-Version: 1.0 X-Mailer: git-send-email 2.39.1.581.gbfd45094c4-goog Message-ID: <20230210003148.2646712-1-seanjc@google.com> Subject: [PATCH v2 00/21] KVM: x86: Disallow writes to feature MSRs post-KVM_RUN From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiaoyao Li , Like Xu 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,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: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757402046680894421?= X-GMAIL-MSGID: =?utf-8?q?1757402046680894421?= Give feature MSRs that same treatment as CPUID and disallow changing said MSRs after KVM_RUN. Fix a tangentially related bug in the vPMU where KVM leaves the vLBRs enabled after userspace disables the guest's entire vPMU. The bulk of this series is a rework of the vmx_pmu_caps_test, a.k.a. the PERF_CAPABILITIES selftests, to expand its coverage. In addition to verifying that KVM rejects changes after KVM_RUN, verify other bits beyond full-width writes and the LBR format. Note! There is a sneaky, small, but massive change buried halfway through this series that will affect all x86 selftests. Patch Verify KVM preserves userspace writes to "durable" MSRs adds a KVM_GET_MSRS after every KVM_SET_MSRS that writes a single MSR and expects to succeeded. The intent is to opportunistically verify that KVM provides "read what you wrote" for all "durable" MSRs. The PERF_CAPS test was manually verifying this behavior, and while it seems kinda gratuitous, the coverage is quite cheap from both a performance and maintenance cost, i.e. I can't think of a reason _not_ to do it. Applies on https://github.com/kvm-x86/linux/tree/next. Sean Christopherson (21): KVM: x86: Rename kvm_init_msr_list() to clarify it inits multiple lists KVM: x86: Add a helper to query whether or not a vCPU has ever run KVM: x86: Add macros to track first...last VMX feature MSRs KVM: x86: Generate set of VMX feature MSRs using first/last definitions KVM: x86: Disallow writes to immutable feature MSRs after KVM_RUN KVM: x86/pmu: WARN and bug the VM if PMU is refreshed after vCPU has run KVM: x86/pmu: Zero out LBR capabilities during PMU refresh KVM: selftests: Split PMU caps sub-tests to avoid writing MSR after KVM_RUN KVM: selftests: Move 0/initial value PERF_CAPS checks to dedicated sub-test KVM: selftests: Assert that full-width PMC writes are supported if PDCM=1 KVM: selftests: Print out failing MSR and value in vcpu_set_msr() KVM: selftests: Verify KVM preserves userspace writes to "durable" MSRs KVM: selftests: Drop now-redundant checks on PERF_CAPABILITIES writes KVM: selftests: Test all fungible features in PERF_CAPABILITIES KVM: selftests: Test all immutable non-format bits in PERF_CAPABILITIES KVM: selftests: Expand negative testing of guest writes to PERF_CAPABILITIES KVM: selftests: Test post-KVM_RUN writes to PERF_CAPABILITIES KVM: selftests: Drop "all done!" printf() from PERF_CAPABILITIES test KVM: selftests: Refactor LBR_FMT test to avoid use of separate macro KVM: selftests: Add negative testcase for PEBS format in PERF_CAPABILITIES KVM: selftests: Verify LBRs are disabled if vPMU is disabled arch/x86/kvm/cpuid.c | 2 +- arch/x86/kvm/mmu/mmu.c | 2 +- arch/x86/kvm/pmu.c | 3 + arch/x86/kvm/svm/svm.c | 2 +- arch/x86/kvm/vmx/pmu_intel.c | 10 + arch/x86/kvm/vmx/vmx.c | 8 +- arch/x86/kvm/x86.c | 103 +++++--- arch/x86/kvm/x86.h | 13 + .../selftests/kvm/include/x86_64/processor.h | 41 ++- .../selftests/kvm/x86_64/vmx_pmu_caps_test.c | 248 ++++++++++++++---- 10 files changed, 342 insertions(+), 90 deletions(-) base-commit: 62ef199250cd46fb66fe98267137b7f64e0b41b4