Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp2091014pxb; Wed, 9 Feb 2022 10:37:32 -0800 (PST) X-Google-Smtp-Source: ABdhPJwz/TXLsbmqp9xy2TxcbjlH/DcT8u3aqJc84dQcDDbVg61HrD9qm6otE6DMAWeXWRmrF//6 X-Received: by 2002:a17:90b:3a8b:: with SMTP id om11mr4841722pjb.163.1644431851801; Wed, 09 Feb 2022 10:37:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644431851; cv=none; d=google.com; s=arc-20160816; b=zb66neC7UKLNOGBWoyEMO93CXrQgYvOrBvEjK6tN4QUYFNKqTwYcqnLNbuTlXTxHpD Nwz0xtv/P5mxG6W5f1nsNALTNfWPrrH6fExcftcHw9gwcHhg6OA5JbAtYOSkPFDsBhFw g8MefT0wSNpnsDYgIWPPdCA5hXBhrvijFExItpmpMpCvIdHV6x8cug80bjWvvwPHWH/m 5ThGNNBmMOUlFdWOS1BxeZDoKZsFhXqfYjvcgP/CXoVzi6AIPjehRUYuMJm0suHXxxOf HB6NgewuutL0jssugpePK2xS5Xc0k6R66vuyG9h7qrW/Ut8XWmNqe5XJg9ZG+5I4EdEY +/eA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=5pN9Ao8qux5sDas/M+PXtuDF/FNADkM/NosE28cGRmQ=; b=KJ41OLpLsrf44hYEXb+LeDYfapw+a1+XiPx8RuJQZbr2xKrt0gLqakebgj7YgM/qLI LRl9KSRvUTz0pOL7+jFXkAkzT1ZZK36YVAkAdFIlQWAsrgaAHxOEuW/f9bHRn3MO6IA1 TnBgvwyPpJla47xDBOHpsch8mFl+terpjRz5o6GbPTShO5IEUAf6zIAZkgjDXej3c8Gm /evxyVDqEDl0ODYrInbtFR7bat7yrWkUtkbJXuFt3LxkK6OscKZIhjhKYLex9e9g/P9S eYQLtDIEho1y/k0eZI8b7Xro2SM/Tm7B7Id7vihMBmzzIMWcQqshC2izPtJ3g5Kqa4cd PsmQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=bGEZkUgO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id n131si2173112pfd.304.2022.02.09.10.37.19; Wed, 09 Feb 2022 10:37:31 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=bGEZkUgO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234266AbiBIRAd (ORCPT + 99 others); Wed, 9 Feb 2022 12:00:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33652 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229673AbiBIRAc (ORCPT ); Wed, 9 Feb 2022 12:00:32 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 67FA0C0613C9 for ; Wed, 9 Feb 2022 09:00:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1644426034; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=5pN9Ao8qux5sDas/M+PXtuDF/FNADkM/NosE28cGRmQ=; b=bGEZkUgOSnx7bsfT5tcVELXtczbt5B2oIRtoHfuqiCNR/18pwQ8dlH2/5dlozYzdEh06cG pp/EeSWFL7lGbgx+GXFaLDrMnGPfN7srpP/BcNEEkPVBoUOuCi6tp/B4GUJF6XIvBAcgRz O8yRzWnq/hBiF5qJe3qZBFpP/mjIc+Y= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-125-1lHF9GKiPH--L8ThemyFVA-1; Wed, 09 Feb 2022 12:00:30 -0500 X-MC-Unique: 1lHF9GKiPH--L8ThemyFVA-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 74C0F1091DA1; Wed, 9 Feb 2022 17:00:29 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1BAA27CD66; Wed, 9 Feb 2022 17:00:21 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: vkuznets@redhat.com, mlevitsk@redhat.com, dmatlack@google.com, seanjc@google.com Subject: [PATCH 00/12] KVM: MMU: do not unload MMU roots on all role changes Date: Wed, 9 Feb 2022 12:00:08 -0500 Message-Id: <20220209170020.1775368-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE 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 The TDP MMU has a performance regression compared to the legacy MMU when CR0 changes often. This was reported for the grsecurity kernel, which uses CR0.WP to implement kernel W^X. In that case, each change to CR0.WP unloads the MMU and causes a lot of unnecessary work. When running nested, this can even cause the L1 to hardly make progress, as the L0 hypervisor it is overwhelmed by the amount of MMU work that is needed. Initially, my plan for this was to pull kvm_mmu_unload from kvm_mmu_reset_context into kvm_init_mmu. Therefore I started by separating the CPU setup (CR0/CR4/EFER, SMM, guest mode, etc.) from the shadow page table format. Right now the "MMU role" is a messy mix of the two and, whenever something is different between the MMU and the CPU, it is stored as an extra field in struct kvm_mmu; for extra bonus complication, sometimes the same thing is stored in both the role and an extra field. The aim was to keep kvm_mmu_unload only if the MMU role changed, and drop it if the CPU role changed. I even posted that cleanup, but it occurred to me later that even a conditional kvm_mmu_unload in kvm_init_mmu would be overkill. kvm_mmu_unload is only needed in the rare cases where a TLB flush is needed (e.g. CR0.PG changing from 1 to 0) or where the guest page table interpretation changes in way not captured by the role (that is, CPUID changes). But the implementation of fast PGD switching is subtle and requires a call to kvm_mmu_new_pgd (and therefore knowing the new MMU role) before kvm_init_mmu, therefore kvm_mmu_reset_context chickens and drops all the roots. Therefore, the meat of this series is a reorganization of fast PGD switching; it makes it possible to call kvm_mmu_new_pgd *after* the MMU has been set up, just using the MMU role instead of kvm_mmu_calc_root_page_role. Patches 1 to 3 are bugfixes found while working on the series. Patches 4 to 5 add more sanity checks that triggered a lot during development. Patches 6 and 7 are related cleanups. In particular patch 7 makes the cache lookup code a bit more pleasant. Patches 8 to 9 rework the fast PGD switching. Patches 10 and 11 are cleanups enabled by the rework, and the only survivors of the CPU role patchset. Finally, patch 12 optimizes kvm_mmu_reset_context. Paolo Paolo Bonzini (12): KVM: x86: host-initiated EFER.LME write affects the MMU KVM: MMU: move MMU role accessors to header KVM: x86: do not deliver asynchronous page faults if CR0.PG=0 KVM: MMU: WARN if PAE roots linger after kvm_mmu_unload KVM: MMU: avoid NULL-pointer dereference on page freeing bugs KVM: MMU: rename kvm_mmu_reload KVM: x86: use struct kvm_mmu_root_info for mmu->root KVM: MMU: do not consult levels when freeing roots KVM: MMU: look for a cached PGD when going from 32-bit to 64-bit KVM: MMU: load new PGD after the shadow MMU is initialized KVM: MMU: remove kvm_mmu_calc_root_page_role KVM: x86: do not unload MMU roots on all role changes arch/x86/include/asm/kvm_host.h | 3 +- arch/x86/kvm/mmu.h | 28 +++- arch/x86/kvm/mmu/mmu.c | 253 ++++++++++++++++---------------- arch/x86/kvm/mmu/mmu_audit.c | 4 +- arch/x86/kvm/mmu/paging_tmpl.h | 2 +- arch/x86/kvm/mmu/tdp_mmu.c | 2 +- arch/x86/kvm/mmu/tdp_mmu.h | 2 +- arch/x86/kvm/svm/nested.c | 6 +- arch/x86/kvm/vmx/nested.c | 8 +- arch/x86/kvm/vmx/vmx.c | 2 +- arch/x86/kvm/x86.c | 39 +++-- 11 files changed, 190 insertions(+), 159 deletions(-) -- 2.31.1