Skip to content

Commit 34b69ed

Browse files
committed
Merge tag 'kvm-x86-mmu-6.11' of https://github.com/kvm-x86/linux into HEAD
KVM x86 MMU changes for 6.11 - Don't allocate kvm_mmu_page.shadowed_translation for shadow pages that can't hold leafs SPTEs. - Unconditionally drop mmu_lock when allocating TDP MMU page tables for eager page splitting to avoid stalling vCPUs when splitting huge pages. - Misc cleanups
2 parents 5dcc1e7 + 0089c05 commit 34b69ed

File tree

3 files changed

+35
-63
lines changed

3 files changed

+35
-63
lines changed

arch/x86/kvm/mmu/mmu.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ static gfn_t kvm_mmu_page_get_gfn(struct kvm_mmu_page *sp, int index)
722722
if (sp->role.passthrough)
723723
return sp->gfn;
724724

725-
if (!sp->role.direct)
725+
if (sp->shadowed_translation)
726726
return sp->shadowed_translation[index] >> PAGE_SHIFT;
727727

728728
return sp->gfn + (index << ((sp->role.level - 1) * SPTE_LEVEL_BITS));
@@ -736,7 +736,7 @@ static gfn_t kvm_mmu_page_get_gfn(struct kvm_mmu_page *sp, int index)
736736
*/
737737
static u32 kvm_mmu_page_get_access(struct kvm_mmu_page *sp, int index)
738738
{
739-
if (sp_has_gptes(sp))
739+
if (sp->shadowed_translation)
740740
return sp->shadowed_translation[index] & ACC_ALL;
741741

742742
/*
@@ -757,7 +757,7 @@ static u32 kvm_mmu_page_get_access(struct kvm_mmu_page *sp, int index)
757757
static void kvm_mmu_page_set_translation(struct kvm_mmu_page *sp, int index,
758758
gfn_t gfn, unsigned int access)
759759
{
760-
if (sp_has_gptes(sp)) {
760+
if (sp->shadowed_translation) {
761761
sp->shadowed_translation[index] = (gfn << PAGE_SHIFT) | access;
762762
return;
763763
}
@@ -1700,8 +1700,7 @@ static void kvm_mmu_free_shadow_page(struct kvm_mmu_page *sp)
17001700
hlist_del(&sp->hash_link);
17011701
list_del(&sp->link);
17021702
free_page((unsigned long)sp->spt);
1703-
if (!sp->role.direct)
1704-
free_page((unsigned long)sp->shadowed_translation);
1703+
free_page((unsigned long)sp->shadowed_translation);
17051704
kmem_cache_free(mmu_page_header_cache, sp);
17061705
}
17071706

@@ -2203,7 +2202,7 @@ static struct kvm_mmu_page *kvm_mmu_alloc_shadow_page(struct kvm *kvm,
22032202

22042203
sp = kvm_mmu_memory_cache_alloc(caches->page_header_cache);
22052204
sp->spt = kvm_mmu_memory_cache_alloc(caches->shadow_page_cache);
2206-
if (!role.direct)
2205+
if (!role.direct && role.level <= KVM_MAX_HUGEPAGE_LEVEL)
22072206
sp->shadowed_translation = kvm_mmu_memory_cache_alloc(caches->shadowed_info_cache);
22082207

22092208
set_page_private(virt_to_page(sp->spt), (unsigned long)sp);
@@ -4609,7 +4608,10 @@ int kvm_handle_page_fault(struct kvm_vcpu *vcpu, u64 error_code,
46094608
if (WARN_ON_ONCE(error_code >> 32))
46104609
error_code = lower_32_bits(error_code);
46114610

4612-
/* Ensure the above sanity check also covers KVM-defined flags. */
4611+
/*
4612+
* Restrict KVM-defined flags to bits 63:32 so that it's impossible for
4613+
* them to conflict with #PF error codes, which are limited to 32 bits.
4614+
*/
46134615
BUILD_BUG_ON(lower_32_bits(PFERR_SYNTHETIC_MASK));
46144616

46154617
vcpu->arch.l1tf_flush_l1d = true;
@@ -7049,7 +7051,6 @@ static unsigned long mmu_shrink_scan(struct shrinker *shrink,
70497051

70507052
list_for_each_entry(kvm, &vm_list, vm_list) {
70517053
int idx;
7052-
LIST_HEAD(invalid_list);
70537054

70547055
/*
70557056
* Never scan more than sc->nr_to_scan VM instances.

arch/x86/kvm/mmu/paging_tmpl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,8 @@ static int FNAME(sync_spte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, int
911911
gpa_t pte_gpa;
912912
gfn_t gfn;
913913

914-
if (WARN_ON_ONCE(sp->spt[i] == SHADOW_NONPRESENT_VALUE))
914+
if (WARN_ON_ONCE(sp->spt[i] == SHADOW_NONPRESENT_VALUE ||
915+
!sp->shadowed_translation))
915916
return 0;
916917

917918
first_pte_gpa = FNAME(get_level1_sp_gpa)(sp);

arch/x86/kvm/mmu/tdp_mmu.c

Lines changed: 24 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,17 +1340,15 @@ bool kvm_tdp_mmu_wrprot_slot(struct kvm *kvm,
13401340
return spte_set;
13411341
}
13421342

1343-
static struct kvm_mmu_page *__tdp_mmu_alloc_sp_for_split(gfp_t gfp)
1343+
static struct kvm_mmu_page *tdp_mmu_alloc_sp_for_split(void)
13441344
{
13451345
struct kvm_mmu_page *sp;
13461346

1347-
gfp |= __GFP_ZERO;
1348-
1349-
sp = kmem_cache_alloc(mmu_page_header_cache, gfp);
1347+
sp = kmem_cache_zalloc(mmu_page_header_cache, GFP_KERNEL_ACCOUNT);
13501348
if (!sp)
13511349
return NULL;
13521350

1353-
sp->spt = (void *)__get_free_page(gfp);
1351+
sp->spt = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT);
13541352
if (!sp->spt) {
13551353
kmem_cache_free(mmu_page_header_cache, sp);
13561354
return NULL;
@@ -1359,47 +1357,6 @@ static struct kvm_mmu_page *__tdp_mmu_alloc_sp_for_split(gfp_t gfp)
13591357
return sp;
13601358
}
13611359

1362-
static struct kvm_mmu_page *tdp_mmu_alloc_sp_for_split(struct kvm *kvm,
1363-
struct tdp_iter *iter,
1364-
bool shared)
1365-
{
1366-
struct kvm_mmu_page *sp;
1367-
1368-
kvm_lockdep_assert_mmu_lock_held(kvm, shared);
1369-
1370-
/*
1371-
* Since we are allocating while under the MMU lock we have to be
1372-
* careful about GFP flags. Use GFP_NOWAIT to avoid blocking on direct
1373-
* reclaim and to avoid making any filesystem callbacks (which can end
1374-
* up invoking KVM MMU notifiers, resulting in a deadlock).
1375-
*
1376-
* If this allocation fails we drop the lock and retry with reclaim
1377-
* allowed.
1378-
*/
1379-
sp = __tdp_mmu_alloc_sp_for_split(GFP_NOWAIT | __GFP_ACCOUNT);
1380-
if (sp)
1381-
return sp;
1382-
1383-
rcu_read_unlock();
1384-
1385-
if (shared)
1386-
read_unlock(&kvm->mmu_lock);
1387-
else
1388-
write_unlock(&kvm->mmu_lock);
1389-
1390-
iter->yielded = true;
1391-
sp = __tdp_mmu_alloc_sp_for_split(GFP_KERNEL_ACCOUNT);
1392-
1393-
if (shared)
1394-
read_lock(&kvm->mmu_lock);
1395-
else
1396-
write_lock(&kvm->mmu_lock);
1397-
1398-
rcu_read_lock();
1399-
1400-
return sp;
1401-
}
1402-
14031360
/* Note, the caller is responsible for initializing @sp. */
14041361
static int tdp_mmu_split_huge_page(struct kvm *kvm, struct tdp_iter *iter,
14051362
struct kvm_mmu_page *sp, bool shared)
@@ -1446,7 +1403,6 @@ static int tdp_mmu_split_huge_pages_root(struct kvm *kvm,
14461403
{
14471404
struct kvm_mmu_page *sp = NULL;
14481405
struct tdp_iter iter;
1449-
int ret = 0;
14501406

14511407
rcu_read_lock();
14521408

@@ -1470,17 +1426,31 @@ static int tdp_mmu_split_huge_pages_root(struct kvm *kvm,
14701426
continue;
14711427

14721428
if (!sp) {
1473-
sp = tdp_mmu_alloc_sp_for_split(kvm, &iter, shared);
1429+
rcu_read_unlock();
1430+
1431+
if (shared)
1432+
read_unlock(&kvm->mmu_lock);
1433+
else
1434+
write_unlock(&kvm->mmu_lock);
1435+
1436+
sp = tdp_mmu_alloc_sp_for_split();
1437+
1438+
if (shared)
1439+
read_lock(&kvm->mmu_lock);
1440+
else
1441+
write_lock(&kvm->mmu_lock);
1442+
14741443
if (!sp) {
1475-
ret = -ENOMEM;
14761444
trace_kvm_mmu_split_huge_page(iter.gfn,
14771445
iter.old_spte,
1478-
iter.level, ret);
1479-
break;
1446+
iter.level, -ENOMEM);
1447+
return -ENOMEM;
14801448
}
14811449

1482-
if (iter.yielded)
1483-
continue;
1450+
rcu_read_lock();
1451+
1452+
iter.yielded = true;
1453+
continue;
14841454
}
14851455

14861456
tdp_mmu_init_child_sp(sp, &iter);
@@ -1501,7 +1471,7 @@ static int tdp_mmu_split_huge_pages_root(struct kvm *kvm,
15011471
if (sp)
15021472
tdp_mmu_free_sp(sp);
15031473

1504-
return ret;
1474+
return 0;
15051475
}
15061476

15071477

0 commit comments

Comments
 (0)