Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A63B8C05027 for ; Thu, 2 Feb 2023 18:28:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232110AbjBBS2Q (ORCPT ); Thu, 2 Feb 2023 13:28:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60506 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230140AbjBBS2O (ORCPT ); Thu, 2 Feb 2023 13:28:14 -0500 Received: from mail-pf1-x449.google.com (mail-pf1-x449.google.com [IPv6:2607:f8b0:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7DFFA15CB2 for ; Thu, 2 Feb 2023 10:28:12 -0800 (PST) Received: by mail-pf1-x449.google.com with SMTP id k14-20020aa7972e000000b00593a8232ac3so1359069pfg.22 for ; Thu, 02 Feb 2023 10:28:12 -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:from:to:cc:subject :date:message-id:reply-to; bh=0+SBcx6MVrgVk2LoIRaHLt69jku4c6rTYPgXbWSbqGk=; b=Q5aRqr2sxq2uX4adungANbB3rre1+leu5U94x534oEHG3jQYGfB1uazHzkIWOlqGpy /z4TBMvW1Svu0LZamqE7g5s3jlKbKuih5/C6XwBaJgE5AW9xerqakVfbN2FEm9JY1XhN I9TTllwSDjMN9g01dh7skwySOK78XhJuXdQKMSt4lj/gaYH8lsKeWYVhZydc70p64JrM J8YMri7UEGb4yA+G3KTzc5QYpzSKlsBqTcS/qgCCEb8sipemc3wWQwlLDFBd9upsjfQY SPUmZBRLkxrcgFgKGCjTIAtoeJXKWV+eihHSlIHTkLcfyvL/yZfmlKnrr+d/1+B2kgDK GeDw== 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:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=0+SBcx6MVrgVk2LoIRaHLt69jku4c6rTYPgXbWSbqGk=; b=oMpcSUQEOzgRWqs9VpTCw3tsUUj+7TSaMKlBuVYq7PXngVyqEdkB8RcY+1rGECRSLM LJC91afvTc2aSWhOKbY6yjvwm2tpAogdh25yfIFNugzujhaRP0gWX8s9JvMpiIgOz2IS Rhtqv1yPYsQfN2u3nmqNz72KeOJw+lMLe0PhObuJcNQOAJOKA4Wa7Qx7FUlxlPnwTMWv oEiBBP4bti4GWzyDGHgQqaXgClCi2XDl5WID1C3/1K6yXOJipvzEgbfxgJ6qhNe2zPul qWaIhYkl3niZioJxGw8X7T9Je3DIJFizrM9kbIQOqMPoZKISVwfrFm98QIjTouVhS1g0 iVjQ== X-Gm-Message-State: AO0yUKXK99u+Kh4PSxeDjEa1gI4e+phMlYdxQjJ8FzDX+05uqtUNjEMN vkjzXpr801Kpw4f1ujPKD735/6pYJEqXqX5SzSiH1Ly+Y8ZLEZYuGULa4ApQMEa8LDPuuWZ1py2 BhCv54PxXK7gndll4Qe/UGusKa45J/EiBLfcH2po+I2nwnHQjuxT+VulsnGJ8sIf0nz7RwfXL X-Google-Smtp-Source: AK7set91y1jj7PTgsJ5vCMdecR8eG7ZR8ipAA11/zzQ08GevhxMRiFrc6z6IefELTRiYqRf+6XlVagoBGRU4 X-Received: from sweer.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:e45]) (user=bgardon job=sendgmr) by 2002:a17:90b:ec2:b0:22c:4751:82b2 with SMTP id gz2-20020a17090b0ec200b0022c475182b2mr726051pjb.56.1675362491780; Thu, 02 Feb 2023 10:28:11 -0800 (PST) Date: Thu, 2 Feb 2023 18:27:48 +0000 Mime-Version: 1.0 X-Mailer: git-send-email 2.39.1.519.gcb327c4b5f-goog Message-ID: <20230202182809.1929122-1-bgardon@google.com> Subject: [PATCH 00/21] KVM: x86/MMU: Formalize the Shadow MMU From: Ben Gardon To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , Peter Xu , Sean Christopherson , David Matlack , Vipin Sharma , Ricardo Koller , Ben Gardon Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This series makes the Shadow MMU a distinct part of the KVM x86 MMU, implemented in separate files, with a defined interface to common code. When the TDP (Two Dimensional Paging) MMU was added to x86 KVM, it came in a separate file with a (reasonably) clear interface. This lead to many points in the KVM MMU like this: if (tdp_mmu_on()) kvm_tdp_mmu_do_stuff() if (memslots_have_rmaps()) /* Do whatever was being done before */ The implementations of various functions which preceded the TDP MMU have remained scattered around mmu.c with no clear identity or interface. Over the last couple years, the KVM x86 community has settled on calling the KVM MMU implementation which preceded the TDP MMU the "Shadow MMU", as it grew from shadow paging, which supported virtualization on hardware pre-TDP. (Note that the Shadow MMU can also build TDP page tables, and doesn't only do shadow paging, so the meaning is a bit overloaded.) Splitting it out into separate files will give a clear interface and make it easier to distinguish common x86 MMU code from the code specific to the two MMU implementations. Patches 1-3 are cleanups from Sean Patches 4-6 prepare for the refactor by adding files and exporting functions. Patch 7 the big move, transferring 3.5K lines from mmu.c to shadow_mmu.c (It may be best if whoever ends up preparing the pull request with this patch just dumps my version and re-does the move so that no code is lost.) Patches 8 and 9 move the includes for paging_tmpl.h to shadow_mmu.c Patches 10-17 clean up the interface between the Shadow MMU and common MMU code. The last few patches are in response to feedback on the RFC and move additional code to the Shadow MMU. Patch 7 is an enormous change, and doing it all at once in a single commit all but guarantees merge conflicts and makes it hard to review. I don't have a good answer to this problem as there's no easy way to move 3.5K lines between files. I tried moving the code bit-by-bit but the intermediate steps added complexity and ultimately the 50+ patches it created didn't seem any easier to review. Doing the big move all at once at least makes it easier to get past when doing Git archeology, and doing it at the beginning of the series allows the rest of the commits to still show up in Git blame. I've tested this series on an Intel Skylake host with kvm-unit-tests and selftests. RFC -> v1: - RFC: https://lore.kernel.org/all/20221221222418.3307832-1-bgardon@google.com/ - Moved some more Shadow MMU content to shadow_mmu.c. David Matlack pointed out some code I'd missed in the first pass. Added commits to the end of the series to achieve this. - Dropped is_cpuid_PSE36 and moved all the BUILD_MMU_ROLE*() macros to mmu_internal, also as suggested by David Matlack. - Added copyright comments to the tops of shadow_mmu.c and .h - Tacked some cleanups from Sean onto the beginning of the series. Ben Gardon (18): KVM: x86/MMU: Add shadow_mmu.(c|h) KVM: x86/MMU: Expose functions for the Shadow MMU KVM: x86/mmu: Get rid of is_cpuid_PSE36() KVM: x86/MMU: Move the Shadow MMU implementation to shadow_mmu.c KVM: x86/MMU: Expose functions for paging_tmpl.h KVM: x86/MMU: Move paging_tmpl.h includes to shadow_mmu.c KVM: x86/MMU: Clean up Shadow MMU exports KVM: x86/MMU: Cleanup shrinker interface with Shadow MMU KVM: x86/MMU: Clean up naming of exported Shadow MMU functions KVM: x86/MMU: Fix naming on prepare / commit zap page functions KVM: x86/MMU: Factor Shadow MMU wrprot / clear dirty ops out of mmu.c KVM: x86/MMU: Remove unneeded exports from shadow_mmu.c KVM: x86/MMU: Wrap uses of kvm_handle_gfn_range in mmu.c KVM: x86/MMU: Add kvm_shadow_mmu_ to the last few functions in shadow_mmu.h KVM: x86/mmu: Move split cache topup functions to shadow_mmu.c KVM: x86/mmu: Move Shadow MMU part of kvm_mmu_zap_all() to shadow_mmu.h KVM: x86/mmu: Move Shadow MMU init/teardown to shadow_mmu.c KVM: x86/mmu: Split out Shadow MMU lockless walk begin/end Sean Christopherson (3): KVM: x86/mmu: Rename slot rmap walkers to add clarity and clean up code KVM: x86/mmu: Replace comment with an actual lockdep assertion on mmu_lock KVM: x86/mmu: Clean up mmu.c functions that put return type on separate line arch/x86/kvm/Makefile | 2 +- arch/x86/kvm/debugfs.c | 1 + arch/x86/kvm/mmu/mmu.c | 4834 ++++--------------------------- arch/x86/kvm/mmu/mmu_internal.h | 87 +- arch/x86/kvm/mmu/paging_tmpl.h | 15 +- arch/x86/kvm/mmu/shadow_mmu.c | 3692 +++++++++++++++++++++++ arch/x86/kvm/mmu/shadow_mmu.h | 132 + 7 files changed, 4498 insertions(+), 4265 deletions(-) create mode 100644 arch/x86/kvm/mmu/shadow_mmu.c create mode 100644 arch/x86/kvm/mmu/shadow_mmu.h base-commit: 7cb79f433e75b05d1635aefaa851cfcd1cb7dc4f -- 2.39.1.519.gcb327c4b5f-goog