Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp1884480pxv; Fri, 2 Jul 2021 15:07:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx+vS0cKuYp3UzKkNRjzd/A9ngE9Drm48gaiQVVXaqD7rUvld1sfXbHL+Hr0RKC4+72rB6Q X-Received: by 2002:a17:906:31d4:: with SMTP id f20mr1821637ejf.383.1625263643342; Fri, 02 Jul 2021 15:07:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625263643; cv=none; d=google.com; s=arc-20160816; b=mueValX/iwJIA03DDrXXs/tHW/xPtua7+QBmeWlfxHy9JpE4/J/W9zl0UDkvhNfNjj FSguiaHawZC+C936bwyusHJIwdPfuFnAtO5ewZ20qTbSNb3C3UYhmqRl0NwV5d6CoUKk 4GuaBVY7yE0Q5CZle14nhYM/oVmrFRGpD3qzZQK+IYHjeyI5FYPs7nY/beUEBYCe2pbD M2Bhtx/MwjAL+1T/lf5W2qAKDF6rsKQLYdIpSVe6d6OsSiNO3Awi5Y4SJsDGhDRg/WDw u8eMNZ9YwbgcZQDBTS2VjAyRBKKWCkFQW6fMimkzcUTSQANfUDZoMj3QocssC1nsWTf7 QPOA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=npM7nUzGJlKrZe+1ey2QuhYvtlu7vTKemnVHyKpdNeQ=; b=Fi45YtYXgHsD/xx2pkqyMIbfCUs5TwK6gkE3t2y+l7NFCcGfbQx8zlYAvkyufG4lT6 DK76XjIxA1FfLdGAAN5NakY1f2YpFZKkSbkA5P+OZxV5HvIj3vcLcn82iQbSHZQqecsW 9eUervN4YmoKB7RZNn10bDhLWGp6xvOZk1jev3aM26DAQJn+OHvggMoSOBlEGoX80yqS M9Bz5Xhyp95NrBaCbjUiGeWPh8g0darJiEsOXF0Hin4jmdep3KRFB0+pNATkY02prTyc hrHXu+/SedCl+F2T1jYwbe4CVjUZA5JJaAz5vyy+1snNO+vi2dtd23DlSIrNt2YLH9dz wwtA== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id er20si4254223ejc.101.2021.07.02.15.07.00; Fri, 02 Jul 2021 15:07:23 -0700 (PDT) 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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233467AbhGBWIN (ORCPT + 99 others); Fri, 2 Jul 2021 18:08:13 -0400 Received: from mga02.intel.com ([134.134.136.20]:51169 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233055AbhGBWHz (ORCPT ); Fri, 2 Jul 2021 18:07:55 -0400 X-IronPort-AV: E=McAfee;i="6200,9189,10033"; a="195951897" X-IronPort-AV: E=Sophos;i="5.83,320,1616482800"; d="scan'208";a="195951897" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Jul 2021 15:05:22 -0700 X-IronPort-AV: E=Sophos;i="5.83,320,1616482800"; d="scan'208";a="642814720" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by fmsmga006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Jul 2021 15:05:21 -0700 From: isaku.yamahata@intel.com To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H . Peter Anvin" , Paolo Bonzini , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , erdemaktas@google.com, Connor Kuehl , Sean Christopherson , x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Sean Christopherson Subject: [RFC PATCH v2 16/69] KVM: x86/mmu: Zap only leaf SPTEs for deleted/moved memslot by default Date: Fri, 2 Jul 2021 15:04:22 -0700 Message-Id: <78d02fee3a21741cc26f6b6b2fba258cd52f2c3c.1625186503.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sean Christopherson Zap only leaf SPTEs when deleting/moving a memslot by default, and add a module param to allow reverting to the old behavior of zapping all SPTEs at all levels and memslots when any memslot is updated. Signed-off-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- arch/x86/kvm/mmu/mmu.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 8d5876dfc6b7..5b8a640f8042 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -85,6 +85,9 @@ __MODULE_PARM_TYPE(nx_huge_pages_recovery_ratio, "uint"); static bool __read_mostly force_flush_and_sync_on_reuse; module_param_named(flush_on_reuse, force_flush_and_sync_on_reuse, bool, 0644); +static bool __read_mostly memslot_update_zap_all; +module_param(memslot_update_zap_all, bool, 0444); + /* * When setting this variable to true it enables Two-Dimensional-Paging * where the hardware walks 2 page tables: @@ -5480,11 +5483,27 @@ static bool kvm_has_zapped_obsolete_pages(struct kvm *kvm) return unlikely(!list_empty_careful(&kvm->arch.zapped_obsolete_pages)); } +static void kvm_mmu_zap_memslot(struct kvm *kvm, struct kvm_memory_slot *slot) +{ + /* + * Zapping non-leaf SPTEs, a.k.a. not-last SPTEs, isn't required, worst + * case scenario we'll have unused shadow pages lying around until they + * are recycled due to age or when the VM is destroyed. + */ + write_lock(&kvm->mmu_lock); + slot_handle_level(kvm, slot, kvm_zap_rmapp, PG_LEVEL_4K, + KVM_MAX_HUGEPAGE_LEVEL, true); + write_unlock(&kvm->mmu_lock); +} + static void kvm_mmu_invalidate_zap_pages_in_memslot(struct kvm *kvm, struct kvm_memory_slot *slot, struct kvm_page_track_notifier_node *node) { - kvm_mmu_zap_all_fast(kvm); + if (memslot_update_zap_all) + kvm_mmu_zap_all_fast(kvm); + else + kvm_mmu_zap_memslot(kvm, slot); } void kvm_mmu_init_vm(struct kvm *kvm) -- 2.25.1