Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp3432513iog; Mon, 27 Jun 2022 16:16:44 -0700 (PDT) X-Google-Smtp-Source: AGRyM1toavQno/QNW3L7CMNUHdEHBg5jHFBNZoxKA0KleVt09UrfOcebXc8EkEUbFdko0QamQvDt X-Received: by 2002:a17:903:22c3:b0:16a:5850:5773 with SMTP id y3-20020a17090322c300b0016a58505773mr1867885plg.127.1656371803958; Mon, 27 Jun 2022 16:16:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656371803; cv=none; d=google.com; s=arc-20160816; b=L0KrOj9dMIwgFXMn0dwUY3AF7Wt2xdKBjnyAZMPGMMp0UOzzH+h4ozJrj4tiJ93nW/ VKRfBCtkOHXh9AMPWWfmn2GiKoohtfPilY1C6wkv7uzKs14Frc0ovGnUqOd/E95Q4R1O iR4MSKwUPNiB/20afXbo+Eqvs/JYOfoBw3EzHIwz1ZN+AuildN3l3zYy2wrywM/lybpW glhz62/r7W95YcsrwNi9iRQYGqjasrAMGmD2VC3wljhHzR7FXsC2v0zwCEYCxvwxTYoF UKBJgWyKEEJXEwQmZzNw0Thm+oTSFuWl/uHph+OQk2X3qpKrFqfUF0dMmztxfOFeixQG VAaA== 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 :dkim-signature; bh=eMVrdbNA7cP+nv0QoUZC3hADxP2wKl58ymzsBCmM2z0=; b=VK3xdqdYeuTtfi6CQ+uppTrW+p6da7SYx/OXQEWyK5qB0V+EwLe0jnG8+VYDdZWJVP FYpAwtDeT1s/X6Ak8W1LlzTEker4nsafx5gIc/5SnAr3hpLrXKn727ITWlVcOpwu9dx7 31el8gr6Ntq2Djn7Eqji/Vt1puE8Nkk3gehXUNL4oQAU8b9J4Z4PLF1hdOzZplBqVkQp Xrh0snWTIee2zzRKB/g0jOI13sDjC771TTnXcws3YzxakcITlTyr599hSAmzTdGXp8Au 42dpZ0AobY5nYkYfX5HoOA1hCu0FHsxuy4DuV/N0FI9s7qI5bnRUPtBa29n4W8QXmwBQ EqrQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Lj55I9Ps; 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=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j4-20020a633c04000000b0040d4bd5d708si15977724pga.848.2022.06.27.16.16.32; Mon, 27 Jun 2022 16:16:43 -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=@intel.com header.s=Intel header.b=Lj55I9Ps; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241737AbiF0V6N (ORCPT + 99 others); Mon, 27 Jun 2022 17:58:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59354 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241426AbiF0VzK (ORCPT ); Mon, 27 Jun 2022 17:55:10 -0400 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EE5F864DA; Mon, 27 Jun 2022 14:54:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1656366897; x=1687902897; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=GceQwT7f2Dr4epTMxHX2Ah64EI/4O0gxsG+G77DQUAw=; b=Lj55I9PsyRNEPdbuOukjUmIfqw2IpjTzuLqoIpP8q5YQ0wUqW+cGP/UI +Ggs/qrMKPMK9vm+Xstf7RE1VfmF+sUlIybq87Dp24nEWBWwdlJXGly46 rW/4iusZ7F7mCSG5hEnn/yQ2r5DBjgf4mmpOzK0czPq15oCHAQc9/6j5+ OmFZAFQQb2Bge/sKfUJVcu5Tja5KAPOKuEcaaTMxfDfI2OlCqiQTol4Kr Tmm4dALCWWF6yJ2wfI9vDeSrIiGBGeAVsaY9+KMoQDh6ijQkXcwPcd4QJ p9Y7Ykz4paFpruL4UabAGfTLEymiC/Btc1yP3pxgBcb5b9LqLhYf4cpmf A==; X-IronPort-AV: E=McAfee;i="6400,9594,10391"; a="281609553" X-IronPort-AV: E=Sophos;i="5.92,227,1650956400"; d="scan'208";a="281609553" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Jun 2022 14:54:53 -0700 X-IronPort-AV: E=Sophos;i="5.92,227,1650956400"; d="scan'208";a="657863564" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Jun 2022 14:54:53 -0700 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Paolo Bonzini , Sean Christopherson Subject: [PATCH v7 040/102] KVM: x86/mmu: Zap only leaf SPTEs for deleted/moved memslot for private mmu Date: Mon, 27 Jun 2022 14:53:32 -0700 Message-Id: <27acc4b2957e1297640d1d8b2a43f7c08e3885d5.1656366338.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 X-Spam-Status: No, score=-7.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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 From: Sean Christopherson For kvm mmu that has shared bit mask, zap only leaf SPTEs when deleting/moving a memslot. The existing kvm_mmu_zap_memslot() depends on role.invalid with read lock of mmu_lock so that other vcpu can operate on kvm mmu concurrently. Mark the root page table invalid, unlink it from page table pointer of CPU, process the page table. It doesn't work for private page table to unlink the root page table because it requires all SPTE entry to be non-present. Instead, with write-lock of mmu_lock and zap only leaf SPTEs for kvm mmu with shared bit mask. Signed-off-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- arch/x86/kvm/mmu/mmu.c | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 80d7c7709af3..c517c7bca105 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -5854,11 +5854,44 @@ 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) +{ + bool flush = false; + + write_lock(&kvm->mmu_lock); + + /* + * 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. + */ + if (is_tdp_mmu_enabled(kvm)) { + struct kvm_gfn_range range = { + .slot = slot, + .start = slot->base_gfn, + .end = slot->base_gfn + slot->npages, + .may_block = false, + }; + + flush = kvm_tdp_mmu_unmap_gfn_range(kvm, &range, flush); + } else { + flush = slot_handle_level(kvm, slot, kvm_zap_rmapp, PG_LEVEL_4K, + KVM_MAX_HUGEPAGE_LEVEL, true); + } + if (flush) + kvm_flush_remote_tlbs(kvm); + + 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 (kvm_gfn_shared_mask(kvm)) + kvm_mmu_zap_memslot(kvm, slot); + else + kvm_mmu_zap_all_fast(kvm); } int kvm_mmu_init_vm(struct kvm *kvm) -- 2.25.1