In order to avoid creating executable hugepages in the TDP MMU PF
handler, remove the dependency between disallowed_hugepage_adjust and
the shadow_walk_iterator. This will open the function up to being used
by the TDP MMU PF handler in a future patch.

Tested by running kvm-unit-tests and KVM selftests on an Intel Haswell
machine. This series introduced no new failures.

This series can be viewed in Gerrit at:
        https://linux-review.googlesource.com/c/virt/kvm/kvm/+/2538

Signed-off-by: Ben Gardon <bgar...@google.com>
---
 arch/x86/kvm/mmu/mmu.c         | 17 +++++++++--------
 arch/x86/kvm/mmu/paging_tmpl.h |  3 ++-
 2 files changed, 11 insertions(+), 9 deletions(-)

diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index 6344e7863a0f5..f6e6fc9959c04 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -3295,13 +3295,12 @@ static int kvm_mmu_hugepage_adjust(struct kvm_vcpu 
*vcpu, gfn_t gfn,
        return level;
 }
 
-static void disallowed_hugepage_adjust(struct kvm_shadow_walk_iterator it,
-                                      gfn_t gfn, kvm_pfn_t *pfnp, int *levelp)
+static void disallowed_hugepage_adjust(u64 spte, gfn_t gfn, int cur_level,
+                                       kvm_pfn_t *pfnp, int *goal_levelp)
 {
-       int level = *levelp;
-       u64 spte = *it.sptep;
+       int goal_level = *goal_levelp;
 
-       if (it.level == level && level > PG_LEVEL_4K &&
+       if (cur_level == goal_level && goal_level > PG_LEVEL_4K &&
            is_nx_huge_page_enabled() &&
            is_shadow_present_pte(spte) &&
            !is_large_pte(spte)) {
@@ -3312,9 +3311,10 @@ static void disallowed_hugepage_adjust(struct 
kvm_shadow_walk_iterator it,
                 * patching back for them into pfn the next 9 bits of
                 * the address.
                 */
-               u64 page_mask = KVM_PAGES_PER_HPAGE(level) - 
KVM_PAGES_PER_HPAGE(level - 1);
+               u64 page_mask = KVM_PAGES_PER_HPAGE(goal_level) -
+                               KVM_PAGES_PER_HPAGE(goal_level - 1);
                *pfnp |= gfn & page_mask;
-               (*levelp)--;
+               (*goal_levelp)--;
        }
 }
 
@@ -3339,7 +3339,8 @@ static int __direct_map(struct kvm_vcpu *vcpu, gpa_t gpa, 
int write,
                 * We cannot overwrite existing page tables with an NX
                 * large page, as the leaf could be executable.
                 */
-               disallowed_hugepage_adjust(it, gfn, &pfn, &level);
+               disallowed_hugepage_adjust(*it.sptep, gfn, it.level,
+                                          &pfn, &level);
 
                base_gfn = gfn & ~(KVM_PAGES_PER_HPAGE(it.level) - 1);
                if (it.level == level)
diff --git a/arch/x86/kvm/mmu/paging_tmpl.h b/arch/x86/kvm/mmu/paging_tmpl.h
index 4dd6b1e5b8cf7..6a8666cb0d24b 100644
--- a/arch/x86/kvm/mmu/paging_tmpl.h
+++ b/arch/x86/kvm/mmu/paging_tmpl.h
@@ -690,7 +690,8 @@ static int FNAME(fetch)(struct kvm_vcpu *vcpu, gpa_t addr,
                 * We cannot overwrite existing page tables with an NX
                 * large page, as the leaf could be executable.
                 */
-               disallowed_hugepage_adjust(it, gw->gfn, &pfn, &hlevel);
+               disallowed_hugepage_adjust(*it.sptep, gw->gfn, it.level,
+                                          &pfn, &hlevel);
 
                base_gfn = gw->gfn & ~(KVM_PAGES_PER_HPAGE(it.level) - 1);
                if (it.level == hlevel)
-- 
2.28.0.709.gb0816b6eb0-goog

Reply via email to