From patchwork Wed Aug 2 14:27:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wang, Wei W" X-Patchwork-Id: 129883 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f41:0:b0:3e4:2afc:c1 with SMTP id v1csp524717vqx; Wed, 2 Aug 2023 08:09:05 -0700 (PDT) X-Google-Smtp-Source: APBJJlE697g4H0oBmxq3iQMR0Dm6W5oj+hD4v5YSxl/tK1G8c0V/+eXdTyk7UOjc+SCAxzD+wMr1 X-Received: by 2002:a05:6a00:10c3:b0:687:7daf:2a2f with SMTP id d3-20020a056a0010c300b006877daf2a2fmr1070563pfu.28.1690988945467; Wed, 02 Aug 2023 08:09:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690988945; cv=none; d=google.com; s=arc-20160816; b=Hb6adrTDxkWSQWnSDmLP8JaDfgcARqmT8DkKUUR2pBWUdH8iiV+CL6eH19Jg5PBj6O 9NJRd/MDvIEZ3frdox/FNkD78+J4gkCm0ZOZiTyZqzV9w9e60qXBpoRdlkrLxKr1oTnK pWfgZAmQsL6CS6uudvEfl/6AFdvewgb+8F9QlJHVMRDrQqrHrJKreZMSHxfD+gUbKIZC 7GjvsL+TfHLkG2ZKNtUrdsJ4p70ow5cmXxvzUsZcohPKb5OgtveIgqcvxAKw7qjZwv/M zP6T7Ua4LaAcvJJGa6L3g7DA2irzGAiRYV3wVsJZ1FzVJ4/UgYyKP4gWj6OOUU27M8qT ISLQ== 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=EsKxhJinaXC9gB7Djw+YNEalODCBePW9iBnoqcDjoE8=; fh=s2Hqq+TzQJImRU1T7W/rT1w9p5e5Sk18qgBDXxffkvI=; b=i2UpOjyYpOHcunVRGQOQSJxgWRXKtN0O/7Zd8BjZoxkywdzVqnvivNsRkoZrwzZD9h e9ooC3UHCFArAh57h93QIHi6dJ0qGn4nD4+cW2TIakvSvjSu1vRYeht4rmJwCVlEwdfQ Arn8V57LprHRwbPrN5J7L3Cm1Ej+GQ1jiOcszSOWEEJAtr7yZiJ/JtZVaT2hPCHbeIgh n3PRRvDNFK6rc5QopwTZoUswGabjGQ2TrkN9IOKWDjzpHTFOyitjvxyj7HjvxKWsFOlM bkozm8BhKrzf85NcPy2M4fFCAt+Hwh3dUGNbzcZ9CpB5VePKd5oatMTzjyoaVYFQvP5U GVkA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Jk5n2srX; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t29-20020a056a00139d00b006870b8f5593si9397465pfg.231.2023.08.02.08.08.49; Wed, 02 Aug 2023 08:09:05 -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=Jk5n2srX; 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 S234138AbjHBOiL (ORCPT + 99 others); Wed, 2 Aug 2023 10:38:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39798 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234164AbjHBOiH (ORCPT ); Wed, 2 Aug 2023 10:38:07 -0400 Received: from mgamail.intel.com (unknown [134.134.136.100]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AFC5A213D; Wed, 2 Aug 2023 07:38:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690987085; x=1722523085; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=24AT3BpfQv6J5OZlOJTbIyfrZvqPdwnxbYzIc/ExOpA=; b=Jk5n2srX3FKZreQgC0czRN9fQriAMFNiYTNXdnPDqA/A0KVchZZIjwFb 4meuB9pVQ4m0C9t+tl2ng4Ea1Nf2RxXm/0pii8D1TT/Efx4e5yPhvqzCH T73vIpkEAinYP7tINpog+lYf8NBitSIjL2pCX8I2dxdhpOs8gVl0eWz3E gkUPDmUMDJ4l9lqs29f4ljdvyyvHIF/TM8rwY4+f+x7P1LcSv9MhhqYnT cZ13pWesnRfOssD/OPMOj4SF4ECfiWbYBTGi758uc30n7fd/XmYNWY9PI P0efyF9Q5XvVSl2n8UBTmPM7a/aXyn5kA+e+H+9oYY8SAcbBx88K+ZtGn w==; X-IronPort-AV: E=McAfee;i="6600,9927,10790"; a="435925564" X-IronPort-AV: E=Sophos;i="6.01,249,1684825200"; d="scan'208";a="435925564" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Aug 2023 07:27:42 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10789"; a="764230763" X-IronPort-AV: E=Sophos;i="6.01,249,1684825200"; d="scan'208";a="764230763" Received: from tdx-lm.sh.intel.com ([10.239.53.27]) by orsmga001.jf.intel.com with ESMTP; 02 Aug 2023 07:27:39 -0700 From: Wei Wang To: seanjc@google.com, pbonzini@redhat.com, bgardon@google.com, dmatlack@google.com Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Wei Wang Subject: [PATCH v1] KVM: x86/mmu: refactor kvm_tdp_mmu_map Date: Wed, 2 Aug 2023 22:27:37 +0800 Message-Id: <20230802142737.5572-1-wei.w.wang@intel.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773130424940875031 X-GMAIL-MSGID: 1773130424940875031 The implementation of kvm_tdp_mmu_map is a bit long. It essentially does three things: 1) adjust the leaf entry level (e.g. 4KB, 2MB or 1GB) to map according to the hugepage configurations; 2) map the nonleaf entries of the tdp page table; and 3) map the target leaf entry. Improve the readabiliy by moving the implementation of 2) above into a subfunction, kvm_tdp_mmu_map_nonleaf, and removing the unnecessary "goto"s. No functional changes intended. Signed-off-by: Wei Wang --- arch/x86/kvm/mmu/tdp_mmu.c | 76 ++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 35 deletions(-) diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c index 512163d52194..0b29a7f853b5 100644 --- a/arch/x86/kvm/mmu/tdp_mmu.c +++ b/arch/x86/kvm/mmu/tdp_mmu.c @@ -1057,43 +1057,33 @@ static int tdp_mmu_link_sp(struct kvm *kvm, struct tdp_iter *iter, static int tdp_mmu_split_huge_page(struct kvm *kvm, struct tdp_iter *iter, struct kvm_mmu_page *sp, bool shared); -/* - * Handle a TDP page fault (NPT/EPT violation/misconfiguration) by installing - * page tables and SPTEs to translate the faulting guest physical address. - */ -int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) +static int kvm_tdp_mmu_map_nonleafs(struct kvm_vcpu *vcpu, + struct kvm_page_fault *fault, + struct tdp_iter *iter) { struct kvm_mmu *mmu = vcpu->arch.mmu; struct kvm *kvm = vcpu->kvm; - struct tdp_iter iter; struct kvm_mmu_page *sp; - int ret = RET_PF_RETRY; - - kvm_mmu_hugepage_adjust(vcpu, fault); - - trace_kvm_mmu_spte_requested(fault); - - rcu_read_lock(); - - tdp_mmu_for_each_pte(iter, mmu, fault->gfn, fault->gfn + 1) { - int r; + int ret; + tdp_mmu_for_each_pte((*iter), mmu, fault->gfn, fault->gfn + 1) { if (fault->nx_huge_page_workaround_enabled) - disallowed_hugepage_adjust(fault, iter.old_spte, iter.level); + disallowed_hugepage_adjust(fault, iter->old_spte, + iter->level); /* * If SPTE has been frozen by another thread, just give up and * retry, avoiding unnecessary page table allocation and free. */ - if (is_removed_spte(iter.old_spte)) - goto retry; + if (is_removed_spte(iter->old_spte)) + return RET_PF_RETRY; - if (iter.level == fault->goal_level) - goto map_target_level; + if (iter->level == fault->goal_level) + return RET_PF_CONTINUE; /* Step down into the lower level page table if it exists. */ - if (is_shadow_present_pte(iter.old_spte) && - !is_large_pte(iter.old_spte)) + if (is_shadow_present_pte(iter->old_spte) && + !is_large_pte(iter->old_spte)) continue; /* @@ -1101,26 +1091,26 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) * needs to be split. */ sp = tdp_mmu_alloc_sp(vcpu); - tdp_mmu_init_child_sp(sp, &iter); + tdp_mmu_init_child_sp(sp, iter); sp->nx_huge_page_disallowed = fault->huge_page_disallowed; - if (is_shadow_present_pte(iter.old_spte)) - r = tdp_mmu_split_huge_page(kvm, &iter, sp, true); + if (is_shadow_present_pte(iter->old_spte)) + ret = tdp_mmu_split_huge_page(kvm, iter, sp, true); else - r = tdp_mmu_link_sp(kvm, &iter, sp, true); + ret = tdp_mmu_link_sp(kvm, iter, sp, true); /* * Force the guest to retry if installing an upper level SPTE * failed, e.g. because a different task modified the SPTE. */ - if (r) { + if (ret) { tdp_mmu_free_sp(sp); - goto retry; + return RET_PF_RETRY; } if (fault->huge_page_disallowed && - fault->req_level >= iter.level) { + fault->req_level >= iter->level) { spin_lock(&kvm->arch.tdp_mmu_pages_lock); if (sp->nx_huge_page_disallowed) track_possible_nx_huge_page(kvm, sp); @@ -1132,13 +1122,29 @@ int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) * The walk aborted before reaching the target level, e.g. because the * iterator detected an upper level SPTE was frozen during traversal. */ - WARN_ON_ONCE(iter.level == fault->goal_level); - goto retry; + WARN_ON_ONCE(iter->level == fault->goal_level); + return RET_PF_RETRY; +} -map_target_level: - ret = tdp_mmu_map_handle_target_level(vcpu, fault, &iter); +/* + * Handle a TDP page fault (NPT/EPT violation/misconfiguration) by installing + * page tables and SPTEs to translate the faulting guest physical address. + */ +int kvm_tdp_mmu_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault) +{ + struct tdp_iter iter; + int ret; + + kvm_mmu_hugepage_adjust(vcpu, fault); + + trace_kvm_mmu_spte_requested(fault); + + rcu_read_lock(); + + ret = kvm_tdp_mmu_map_nonleafs(vcpu, fault, &iter); + if (ret == RET_PF_CONTINUE) + ret = tdp_mmu_map_handle_target_level(vcpu, fault, &iter); -retry: rcu_read_unlock(); return ret; }