Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp795582pxa; Wed, 5 Aug 2020 13:00:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyb3T4Gf9J/4jTtZWqscJ8yk/9COC2GER2ldIgEpYR88sq8h+5uf10eSHgCJBlbRNLD24Z/ X-Received: by 2002:a17:906:8608:: with SMTP id o8mr1036694ejx.156.1596657600686; Wed, 05 Aug 2020 13:00:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1596657600; cv=none; d=google.com; s=arc-20160816; b=HPeDHpsp3JMowswCUry4httR48sO9OsCczUUx8znclPqjLoyhNe60Pz4P6+yYJP/4D 3sxP9//JfItONWn07y1BAbHBvR49eBA4cYBjk3s0CyDdBKgNmPNC7EEIZZoJ/DhHlI2C tXTE3dJw7zQM7rprxvhHKx24nRS5OVLsMVYctZQEa4qC5VUuWBp2DSxdMw12qKDBqiA6 uECXg8GoLEHTVsY4VYaUKGl3zbzbs0ZO9GLK1AgJ/37Ao1UmJhaNt7EmKRlFCtLdU4vP c3ty0ABcIV/D3FbLHDzQn98VuQjrRSlzA0ScpWrn8PQD4dp2P/pF75APSz2AQU3egNuY 7aNg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature; bh=K8w/qRSPiRGvQfAwvAEG9vaPeWJJC258oA4XZ+7V/5A=; b=IWWsml21PlvOd9ewKuokZHplYc2apjCJF6IlIobq/JVqk0e3EFDBJsdcsjJRpJmeX8 9ytxaTL4GjYvh7uYFz4MiHENEhywCjGQavJ7Z9OgDb3tYzdu6Fw7X6YS2+vr7g2rWKIg T2qjnLNK0hg9I74qFIIHHx8/cSe7cj0Jj12d7wpbSgsTMwxKBl12xKdsC/ImpyNpdY9H eyBJa11PXjUU584V038gyffx5pVlt/vBZoFUy0h7C13p7JnbQ+IoOFJW2Rw2elFdFnZ+ /LveNV5tnbefZ7xHohqhdsQ4ntGm6YUoFFUh4uUxmcW/Gmpy3clDfGVx55CBOHUuemKR JH+w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=avfgyGCI; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o2si1870366ejm.676.2020.08.05.12.59.37; Wed, 05 Aug 2020 13:00:00 -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; dkim=pass header.i=@gmail.com header.s=20161025 header.b=avfgyGCI; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729746AbgHET6k (ORCPT + 99 others); Wed, 5 Aug 2020 15:58:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45416 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727904AbgHEQmM (ORCPT ); Wed, 5 Aug 2020 12:42:12 -0400 Received: from mail-pg1-x543.google.com (mail-pg1-x543.google.com [IPv6:2607:f8b0:4864:20::543]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9C490C0A893E; Wed, 5 Aug 2020 07:13:42 -0700 (PDT) Received: by mail-pg1-x543.google.com with SMTP id p3so24507508pgh.3; Wed, 05 Aug 2020 07:13:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=K8w/qRSPiRGvQfAwvAEG9vaPeWJJC258oA4XZ+7V/5A=; b=avfgyGCIXlq9hgjbIhnPyKTrkyyn6mfx28wepMCd18SZQV5YadO4f1/77RoRfKQk/w 7hWcQobOQlYZsmU2TThr1U1rcS3ZAskU+bXk3/esFuaDmWguaROHOw1cLPbwGvE6+A89 1p3NirG7Bzxc9KqwFuMIfjKIyCoTMQJg5KyiOzVG8Nye336rAHCimiHnLxXpaMGXQtMB hPGvLgLKLBuRkqZF9u3VAhS7s9bCCoJNIYyBAtDVyMoKaBKn/xrYWoOcwJUn85+pe+bm 4ECjc6m1GBH9UTj6EiCLRTBQ2TRgPvq9TVBlRLMoR4ger3biT7guANBuqaZw5bSsPlM2 X8cg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=K8w/qRSPiRGvQfAwvAEG9vaPeWJJC258oA4XZ+7V/5A=; b=kJ9Fp0v3vBnoi2OsYraSNaAkTghC4u2689vM6s9P8NPjJI5cRlXdPE5wxnUtUhqgfS VPAsr2jeOJU6kChJoG8EbamKCE2Egilk+a584vgirK28wltcPkeyFxVK0SddChbrDtDc darTP/rrqVIujdsR0Y+LAttI8ykRznH+7iKEh3k/X/X5OUyr7DoQkZp3Yj7Lyp3/phUT koMW8mE5f/OMUI/IyTh1rOsPv8/Ui1VewrbfYZo7/xdC0mfdF3sDP/VOgCz8CZBrGbwr uBRIfLOHJLHmcRjwdJjbQ57rMVSV+ky4wCVah0eY4U3yTbvyvr7MOqoL0svknsnLMf6F +iyQ== X-Gm-Message-State: AOAM533Skji63u14xl3E+l6IoIBnKwdYDEDhPR2G8dnWt9aUZhti/OIQ mKG8vZkR/Q04a2QxVcj5W70= X-Received: by 2002:a63:3816:: with SMTP id f22mr3228491pga.311.1596636822205; Wed, 05 Aug 2020 07:13:42 -0700 (PDT) Received: from localhost.localdomain ([203.205.141.56]) by smtp.gmail.com with ESMTPSA id a129sm4069834pfd.165.2020.08.05.07.13.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Aug 2020 07:13:41 -0700 (PDT) From: Yulei Zhang To: pbonzini@redhat.com Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, sean.j.christopherson@intel.com, jmattson@google.com, vkuznets@redhat.com, xiaoguangrong.eric@gmail.com, kernellwp@gmail.com, lihaiwei.kernel@gmail.com, Yulei Zhang Subject: [RFC 5/9] Modify the page fault path to meet the direct build EPT requirement Date: Wed, 5 Aug 2020 22:14:31 +0800 Message-Id: <20200805141431.9137-1-yulei.kernel@gmail.com> X-Mailer: git-send-email 2.17.1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Yulei Zhang Refine the fast page fault code so that it can be used in either normal ept mode or direct build EPT mode. Signed-off-by: Yulei Zhang --- arch/x86/kvm/mmu/mmu.c | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index df703deac928..b59a4502d1f6 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -3471,12 +3471,13 @@ static bool page_fault_can_be_fast(u32 error_code) * someone else modified the SPTE from its original value. */ static bool -fast_pf_fix_direct_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, +fast_pf_fix_direct_spte(struct kvm_vcpu *vcpu, gpa_t gpa, u64 *sptep, u64 old_spte, u64 new_spte) { gfn_t gfn; - WARN_ON(!sp->role.direct); + WARN_ON(!vcpu->arch.direct_build_tdp && + (!page_header(__pa(sptep))->role.direct)); /* * Theoretically we could also set dirty bit (and flush TLB) here in @@ -3498,7 +3499,7 @@ fast_pf_fix_direct_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, * The gfn of direct spte is stable since it is * calculated by sp->gfn. */ - gfn = kvm_mmu_page_get_gfn(sp, sptep - sp->spt); + gfn = gpa >> PAGE_SHIFT; kvm_vcpu_mark_page_dirty(vcpu, gfn); } @@ -3526,10 +3527,10 @@ static bool fast_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u32 error_code) { struct kvm_shadow_walk_iterator iterator; - struct kvm_mmu_page *sp; bool fault_handled = false; u64 spte = 0ull; uint retry_count = 0; + int pte_level = 0; if (!page_fault_can_be_fast(error_code)) return false; @@ -3539,12 +3540,20 @@ static bool fast_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, do { u64 new_spte; - for_each_shadow_entry_lockless(vcpu, cr2_or_gpa, iterator, spte) + for_each_shadow_entry_lockless(vcpu, cr2_or_gpa, iterator, spte) { if (!is_shadow_present_pte(spte)) break; + } + + if (iterator.level < PT_PAGE_TABLE_LEVEL) + pte_level = PT_PAGE_TABLE_LEVEL; + else + pte_level = iterator.level; + + WARN_ON(!vcpu->arch.direct_build_tdp && + (pte_level != page_header(__pa(iterator.sptep))->role.level)); - sp = page_header(__pa(iterator.sptep)); - if (!is_last_spte(spte, sp->role.level)) + if (!is_last_spte(spte, pte_level)) break; /* @@ -3587,7 +3596,7 @@ static bool fast_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, * * See the comments in kvm_arch_commit_memory_region(). */ - if (sp->role.level > PT_PAGE_TABLE_LEVEL) + if (pte_level > PT_PAGE_TABLE_LEVEL) break; } @@ -3601,7 +3610,7 @@ static bool fast_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, * since the gfn is not stable for indirect shadow page. See * Documentation/virt/kvm/locking.txt to get more detail. */ - fault_handled = fast_pf_fix_direct_spte(vcpu, sp, + fault_handled = fast_pf_fix_direct_spte(vcpu, cr2_or_gpa, iterator.sptep, spte, new_spte); if (fault_handled) @@ -4153,6 +4162,9 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code, if (fast_page_fault(vcpu, gpa, error_code)) return RET_PF_RETRY; + if (vcpu->arch.direct_build_tdp) + return RET_PF_EMULATE; + mmu_seq = vcpu->kvm->mmu_notifier_seq; smp_rmb(); -- 2.17.1