Received: by 2002:a05:7412:40d:b0:e2:908c:2ebd with SMTP id 13csp808819rdf; Tue, 21 Nov 2023 18:19:11 -0800 (PST) X-Google-Smtp-Source: AGHT+IHea5J1zl0rOBdWQeJtInyENY1+OORAe6XlO575N0rolmydEfpUjxUN/XsRbtF68fcd3Oms X-Received: by 2002:a17:90a:f3cc:b0:280:7cd2:429 with SMTP id ha12-20020a17090af3cc00b002807cd20429mr6343462pjb.18.1700619550695; Tue, 21 Nov 2023 18:19:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700619550; cv=none; d=google.com; s=arc-20160816; b=PM00qFRN8D1CY98n9Wn6N+coXY1x5224tgjjmlZg5v2ZeY4kokaD4jaIL6Xd/BL2cG vNLLxaynSbeUtpGkjkok09B1IGz7Z/CDI8OBqQAL36z0NACaah2nW0bKgTKNUdt32pAF jZwl51XSaI3Kf00vrVg2pA7Oia1Ckb1lZ5Ktrz63cfdpBYiZpXY34g/oobMrFq9v5ZzA nkq9K3v8gTS3Iy69otQ2DDe7OKmtXOzS5Ip5GzfnI/WOzGzTHEoCKHk6xZpvWUOuOCk2 yPsh6CNKh/DlBbTSd83FrtENMCS5xJsyZuS3CPUJcU96yluJDSwt5zqsYYWt29y0JdWr MBSA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:subject:user-agent:mime-version:date:message-id :dkim-signature; bh=D74fr661JRzRUG0DomPy9YUlLexvQNH+/EI3J/R5AVE=; fh=qWTbkbSu/G1OnmJtnXVUWkcp6QCOngk7wT9/ExP1W5Y=; b=ah5wMvR8gcQc7JFUcBYB39AN0V7Ac6iHgnDIFHyZScEg6WeunDEupYkKjoROhL3xPN gbk7hTK3XPP+3QlSNVi4MmCcOTT6t3C6NYiWqk2CD8Scdf0BzwOvXsX13TOoe6dJQfR+ 6Y4+T0Q1GZ5wofA8s2+09iXdwJTU4OM9TaT24hcBi3nnyAlyEn4jglYV+tYVUY2MFMW1 tK/71MgN0CnSo231aMJBwtRRJXbHmo5lpdrbbIaGH73mG7byUmwxxPGQA0k8K8tu6v53 G7T+rlLOj/rtrDFRYrgX5qUCGrhN8+jqbc4rhjpIzR3AM9NeHajdn/QCQYU5+mql05Na y/tg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=WzOZbcCQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id gx3-20020a17090b124300b0027cf8287c5csi439942pjb.62.2023.11.21.18.19.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Nov 2023 18:19:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=WzOZbcCQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 2CEB88183EEF; Tue, 21 Nov 2023 18:19:08 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229775AbjKVCTA (ORCPT + 99 others); Tue, 21 Nov 2023 21:19:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47394 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229558AbjKVCS6 (ORCPT ); Tue, 21 Nov 2023 21:18:58 -0500 Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D6DC7199; Tue, 21 Nov 2023 18:18:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1700619534; x=1732155534; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=5o3KNyQ2NXiGAxcrlOf8yoPEXAyka3WGVL/Jm+8LiCw=; b=WzOZbcCQD5kbdf8jVI3zoL37D+QcXO4ScNLedng71Ch6cuZdvUoXCdPi NU4I89CRHFzU0uz2tHxxEza8qR5TK+JW2svqyZrE/b0ZmtKixnhZSEUK0 KBBlOB5lUJwrK1bj025/Mmu3w6EK6jVbODjDTq7xxxgz0k8M+w7CNopZ1 6jWFS5rS4ucGwzXfE5Kw1wnpUx38Djd8QEVhrYn64leror0hB5gAkDEh0 UEiDXVAZTi0THwAE6ZIB+vvJGZmbNvg23BgkcxY1qYXgEi3K/ytIWgpE8 liv1zMqTRhAk1uHtEZzHK6t0s1XHpAKCJBbWY5N6HAJ0VktrwRv2nAjST g==; X-IronPort-AV: E=McAfee;i="6600,9927,10901"; a="395886240" X-IronPort-AV: E=Sophos;i="6.04,217,1695711600"; d="scan'208";a="395886240" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Nov 2023 18:18:54 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10901"; a="760292891" X-IronPort-AV: E=Sophos;i="6.04,217,1695711600"; d="scan'208";a="760292891" Received: from binbinwu-mobl.ccr.corp.intel.com (HELO [10.238.10.126]) ([10.238.10.126]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Nov 2023 18:18:50 -0800 Message-ID: <9810c96a-2156-4653-8055-701c0744528c@linux.intel.com> Date: Wed, 22 Nov 2023 10:18:47 +0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v6 11/16] KVM: x86/tdp_mmu: Split the large page when zap leaf To: Isaku Yamahata Cc: isaku.yamahata@intel.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, isaku.yamahata@gmail.com, Paolo Bonzini , erdemaktas@google.com, Sean Christopherson , Sagi Shahar , David Matlack , Kai Huang , Zhi Wang , chen.bo@intel.com, hang.yuan@intel.com, tina.zhang@intel.com, Xiaoyao Li References: <8b43a9203c34b5330c4ea5901da5dac3458ac98d.1699368363.git.isaku.yamahata@intel.com> <5d9aadbd-975b-4c4d-ba18-ac6e0fb07ba1@linux.intel.com> <20231121110045.GH1109547@ls.amr.corp.intel.com> From: Binbin Wu In-Reply-To: <20231121110045.GH1109547@ls.amr.corp.intel.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Tue, 21 Nov 2023 18:19:08 -0800 (PST) On 11/21/2023 7:00 PM, Isaku Yamahata wrote: > On Tue, Nov 21, 2023 at 05:57:28PM +0800, > Binbin Wu wrote: > >>> diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c >>> index 7873e9ee82ad..a209a67decae 100644 >>> --- a/arch/x86/kvm/mmu/tdp_mmu.c >>> +++ b/arch/x86/kvm/mmu/tdp_mmu.c >>> @@ -964,6 +964,14 @@ bool kvm_tdp_mmu_zap_sp(struct kvm *kvm, struct kvm_mmu_page *sp) >>> return true; >>> } >>> + >>> +static struct kvm_mmu_page *tdp_mmu_alloc_sp_for_split(struct kvm *kvm, >>> + struct tdp_iter *iter, >>> + bool shared); >>> + >>> +static int tdp_mmu_split_huge_page(struct kvm *kvm, struct tdp_iter *iter, >>> + struct kvm_mmu_page *sp, bool shared); >>> + >>> /* >>> * If can_yield is true, will release the MMU lock and reschedule if the >>> * scheduler needs the CPU or there is contention on the MMU lock. If this >>> @@ -975,13 +983,15 @@ static bool tdp_mmu_zap_leafs(struct kvm *kvm, struct kvm_mmu_page *root, >>> gfn_t start, gfn_t end, bool can_yield, bool flush, >>> bool zap_private) >>> { >>> + bool is_private = is_private_sp(root); >>> + struct kvm_mmu_page *split_sp = NULL; >>> struct tdp_iter iter; >>> end = min(end, tdp_mmu_max_gfn_exclusive()); >>> lockdep_assert_held_write(&kvm->mmu_lock); >>> - WARN_ON_ONCE(zap_private && !is_private_sp(root)); >>> + WARN_ON_ONCE(zap_private && !is_private); >>> if (!zap_private && is_private_sp(root)) >> Can use is_private instead of is_private_sp(root) here as well. > I'll update it. > >>> return false; >>> @@ -1006,12 +1016,66 @@ static bool tdp_mmu_zap_leafs(struct kvm *kvm, struct kvm_mmu_page *root, >>> !is_last_spte(iter.old_spte, iter.level)) >>> continue; >>> + if (is_private && kvm_gfn_shared_mask(kvm) && >>> + is_large_pte(iter.old_spte)) { >>> + gfn_t gfn = iter.gfn & ~kvm_gfn_shared_mask(kvm); >>> + gfn_t mask = KVM_PAGES_PER_HPAGE(iter.level) - 1; >>> + struct kvm_memory_slot *slot; >>> + struct kvm_mmu_page *sp; >>> + >>> + slot = gfn_to_memslot(kvm, gfn); >>> + if (kvm_hugepage_test_mixed(slot, gfn, iter.level) || >>> + (gfn & mask) < start || >>> + end < (gfn & mask) + KVM_PAGES_PER_HPAGE(iter.level)) { >>> + WARN_ON_ONCE(!can_yield); >>> + if (split_sp) { >>> + sp = split_sp; >>> + split_sp = NULL; >>> + sp->role = tdp_iter_child_role(&iter); >>> + } else { >>> + WARN_ON(iter.yielded); >>> + if (flush && can_yield) { >>> + kvm_flush_remote_tlbs(kvm); >>> + flush = false; >>> + } >> Is it necessary to do the flush here? > Because tdp_mmu_alloc_sp_for_split() may unlock mmu_lock and block. > While blocking, other thread operates on KVM MMU and gets confused due to > remaining TLB cache. > > >>> + sp = tdp_mmu_alloc_sp_for_split(kvm, &iter, false); >>> + if (iter.yielded) { >>> + split_sp = sp; >>> + continue; >>> + } >>> + } >>> + KVM_BUG_ON(!sp, kvm); >>> + >>> + tdp_mmu_init_sp(sp, iter.sptep, iter.gfn); >>> + if (tdp_mmu_split_huge_page(kvm, &iter, sp, false)) { >>> + kvm_flush_remote_tlbs(kvm); >>> + flush = false; >> Why it needs to flush TLB immediately if tdp_mmu_split_huge_page() fails? > Hmm, we don't need it. When breaking up page table, we need to tlb flush > before issuing TDH.MEM.PAGE.DEMOTE(), not after it. Will remove those two lines. > > >> Also, when KVM MMU write lock is held, it seems tdp_mmu_split_huge_page() >> will not fail. > This can happen with TDX_OPERAND_BUSY with secure-ept tree lock with other > vcpus TDH.VP.ENTER(). TDH.VP.ENTER() can take exclusive lock of secure-EPT. > > >> But let's assume this condition can be triggered, since sp is >> local >> variable, it will lost its value after continue, and split_sp is also NULL, >> it will try to allocate a new sp, memory leakage here? > Nice catch. I'll add split_sp = sp; > > >>> + /* force retry on this gfn. */ >>> + iter.yielded = true; >>> + } else >>> + flush = true; >>> + continue; >>> + } >>> + } >>> + >>> tdp_mmu_iter_set_spte(kvm, &iter, SHADOW_NONPRESENT_VALUE); >>> flush = true; >>> } >>> rcu_read_unlock(); >>> + if (split_sp) { >>> + WARN_ON(!can_yield); >>> + if (flush) { >>> + kvm_flush_remote_tlbs(kvm); >>> + flush = false; >>> + } >> Same here, why we need to do the flush here? >> Can we delay it till the caller do the flush? > No. Because we unlock mmu_lock and may block when freeing memory. But I don't find it may block during freeing memory. Did I miss anything? > >>> + >>> + write_unlock(&kvm->mmu_lock); >>> + tdp_mmu_free_sp(split_sp); >>> + write_lock(&kvm->mmu_lock); >>> + } >>> + >>> /* >>> * Because this flow zaps _only_ leaf SPTEs, the caller doesn't need >>> * to provide RCU protection as no 'struct kvm_mmu_page' will be freed. >>> @@ -1606,8 +1670,6 @@ static struct kvm_mmu_page *tdp_mmu_alloc_sp_for_split(struct kvm *kvm, >>> KVM_BUG_ON(kvm_mmu_page_role_is_private(role) != >>> is_private_sptep(iter->sptep), kvm); >>> - /* TODO: Large page isn't supported for private SPTE yet. */ >>> - KVM_BUG_ON(kvm_mmu_page_role_is_private(role), kvm); >>> /* >>> * Since we are allocating while under the MMU lock we have to be >>