KVM: s390: More fixes
Claudio Imbrenda
maintainercover KVM: s390: More fixes
Fix a bunch of gmap bugs. Several patches concern proper handling of the soft-dirty bit for migration.
Claudio Imbrenda (12):
KVM: s390: ucontrol: Do not mask -ENOMEM when translating
KVM: s390: Fix potential leak in kvm_s390_get_io_int()
KVM: s390: Introduce gmap_enable_cmm()
KVM: s390: Allocate page tables when CMMA is used
KVM: s390: Fix dat_entry_walk() with DAT_WALK_CONTINUE
KVM: s390: Fix soft-dirty handling in _gmap_link()
KVM: s390: Fix race in gmap_clear_young_pte()
KVM: s390: Properly adjust page size before calling _gmap_link()
KVM: s390: Fix *_test_and_clear_softdirty() functions
KVM: s390: Do not unnecessarily drop the soft-dirty bit
KVM: s390: Disallow 2G pages for guests up to 2G
KVM: s390: vsie: Fix improper shadowing of non-present host ptes
arch/s390/kvm/gmap/dat.c | 2 +- arch/s390/kvm/gmap/gmap.c | 228 +++++++++++++++++++++++++++------ arch/s390/kvm/gmap/gmap.h | 14 ++ arch/s390/kvm/s390/gaccess.c | 3 +- arch/s390/kvm/s390/interrupt.c | 13 +- arch/s390/kvm/s390/priv.c | 11 +- arch/s390/kvm/s390/s390.c | 19 ++- 7 files changed, 235 insertions(+), 55 deletions(-)
Signature · 1 line
2.56.1
Claudio Imbrenda
maintainerPATCH 1/12 KVM: s390: ucontrol: Do not mask -ENOMEM when translating
Do not mask -ENOMEM or any other error when vcpu_ucontrol_translate() is called. In case of failure, return the error without masking it to -EREMOTE.
--- arch/s390/kvm/gmap/gmap.c | 5 ++++- arch/s390/kvm/s390/s390.c | 10 ++++++---- 2 files changed, 10 insertions(+), 5 deletions(-)
arch/s390/kvm/gmap/gmap.c +4 -1
@@ -937,7 +937,10 @@ static int gmap_ucas_translate_simple(struct gmap *gmap, gpa_t *gaddr, union crs* This allows ucontrol VMs to use the normal fault resolution path, like * normal VMs. * -* Return: %0 in case of success, otherwise %-EREMOTE. +* Return: +* * %0 in case of success, +* * %-ENOMEM if running out of memory, +* * otherwise %-EREMOTE. */ int gmap_ucas_translate (struct kvm_s390_mmu_cache *mc,struct gmap *gmap,gpa_t *gaddr) {
arch/s390/kvm/s390/s390.c +6 -4
@@ -4316,8 +4316,9 @@ static int kvm_s390_fixup_prefix(struct kvm_vcpu *vcpu)gfn_t gfn;int rc; -if (vcpu_ucontrol_translate (vcpu, &gaddr)) -return -EREMOTE ; + rc =vcpu_ucontrol_translate (vcpu, &gaddr); +if (rc) +return rc; gfn =gpa_to_gfn (gaddr); rc =kvm_s390_faultin_gfn_simple (vcpu,NULL , gfn, true);
@@ -4619,8 +4620,9 @@ static int vcpu_dat_fault_handler(struct kvm_vcpu *vcpu, gpa_t gaddr, bool wr) };int rc; -if (vcpu_ucontrol_translate (vcpu, &gaddr)) -return -EREMOTE ; + rc =vcpu_ucontrol_translate (vcpu, &gaddr); +if (rc) +return rc; f.gfn =gpa_to_gfn (gaddr); rc =kvm_s390_faultin_gfn (vcpu,NULL , &f);
Signature · 1 line
2.56.1
Claudio Imbrenda
maintainerPATCH 2/12 KVM: s390: Fix potential leak in kvm_s390_get_io_int()
In reply to Claudio ImbrendaIn case all of the following apply at the same time:
* a higher priority GISA interrupt is pending
* a lower priority classic interrupt is also pending
* the pending interrupt queue is full
then kvm_s390_reinject_io_int() will fail, the classic interrupt is dropped and its memory is leaked.
Fix by checking whether the reinjection of the interrupt succeeded, and free it if not. The interrupt is still dropped, but no leak occurs.
This is not expected to happen during normal guest operation, due to the large maximum size of the pending interrupt queue.
--- arch/s390/kvm/s390/interrupt.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
arch/s390/kvm/s390/interrupt.c +11 -2
@@ -1775,8 +1775,17 @@ struct kvm_s390_interrupt_info *kvm_s390_get_io_int(struct kvm *kvm,if (tmp_inti) { tmp_inti->type =KVM_S390_INT_IO (1 ,0 ,0 ,0 ); tmp_inti->io.io_int_word =isc_to_int_word (isc); -if (inti) -kvm_s390_reinject_io_int (kvm, inti); +if (inti) { +/* +* The maximum number of pending interrupts is so +* high that this is impossible in practice during +* normal guest operation. +* Drop the interrupt if the limit was somehow +* reached anyway, and free it, to avoid a leak. +*/ +if (kvm_s390_reinject_io_int (kvm, inti)) +kfree (inti); + } inti = tmp_inti; }else gisa_set_ipm_gisc (gi->origin, isc);
Signature · 1 line
2.56.1
Claudio Imbrenda
maintainerPATCH 3/12 KVM: s390: Introduce gmap_enable_cmm()
In reply to Claudio ImbrendaIntroduce gmap_enable_cmm_already_locked() and its locking wrapper gmap_enable_cmm() to attempt to allocate all page tables of a running guest. This means that all hugepages will be mapped with 4k pages regardless of the hpage parameter.
Existing mappings with large pages will be split.
If any memory allocation fails, ignore the error and give up. This is only a best effort.
--- arch/s390/kvm/gmap/gmap.c | 48 +++++++++++++++++++++++++++++++++++++++ arch/s390/kvm/gmap/gmap.h | 14 ++++++++++++ 2 files changed, 62 insertions(+)
arch/s390/kvm/gmap/gmap.c +48 -0
@@ -1527,3 +1527,51 @@ struct gmap *gmap_create_shadow(struct kvm_s390_mmu_cache *mc, struct gmap *pare }return new; } + +#if KVM_S390_MANAGES_S390_GUEST +/* Bail out for any allocation failure. This is only a best effort to allocate all page tables. */ +void gmap_enable_cmm_already_locked (struct kvm_s390_mmu_cache *mc,struct gmap *gmap) +{ +struct kvm_memory_slot *ms; +struct kvm_memslots *slots; +gfn_t gfn, start, end; +union crste *crstep; +union pte *ptep; +int flags, bkt; + +lockdep_assert_held (&gmap->kvm->srcu); +lockdep_assert_held (&gmap->kvm->slots_arch_lock); + +if (uses_cmm (gmap)) +return ; + +set_bit (GMAP_FLAG_USES_CMM , &gmap->flags); + + slots =kvm_memslots (gmap->kvm); +if (!slots ||kvm_memslots_empty (slots)) +return ; + +if (WARN_ON_ONCE (is_ucontrol (gmap))) +return ; + + flags =DAT_WALK_ALLOC |DAT_WALK_SPLIT ; + +kvm_for_each_memslot (ms, bkt, slots) { + start =ALIGN (ms->base_gfn, _PAGE_ENTRIES); + end =ALIGN_DOWN (ms->base_gfn + ms->npages, _PAGE_ENTRIES); + +for (gfn = start; gfn < end; gfn += _PAGE_ENTRIES) { +if (kvm_s390_mmu_cache_topup (mc)) +return ; + +guard (write_lock)(&gmap->kvm->mmu_lock); + +if (uses_skeys (gmap)) + flags |=DAT_WALK_USES_SKEYS ; + +dat_entry_walk (mc, gfn, gmap->asce, flags,TABLE_TYPE_PAGE_TABLE , + &crstep, &ptep); + } + } +} +#endif /* KVM_S390_MANAGES_S390_GUEST */
arch/s390/kvm/gmap/gmap.h +14 -0
@@ -102,6 +102,7 @@ void gmap_ucas_unmap(struct gmap *gmap, gfn_t c_gfn, unsigned long count);#if KVM_S390_MANAGES_S390_GUEST int gmap_enable_skeys (struct gmap *gmap); +void gmap_enable_cmm_already_locked (struct kvm_s390_mmu_cache *mc,struct gmap *gmap);#endif /* KVM_S390_MANAGES_S390_GUEST */ int gmap_pv_destroy_range (struct gmap *gmap,gfn_t start,gfn_t end,bool interruptible);
@@ -345,4 +346,17 @@ static inline bool gmap_is_shadow_valid(struct gmap *sg, union asce asce, int ed return sg->guest_asce.val == asce.val && sg->edat_level == edat_level; } +#if KVM_S390_MANAGES_S390_GUEST +static inline void gmap_enable_cmm (struct kvm_s390_mmu_cache *mc,struct gmap *gmap) +{ +/* CMMA already enabled, nothing to do */ +if (uses_cmm (gmap)) +return ; + +guard (srcu)(&gmap->kvm->srcu); +guard (mutex)(&gmap->kvm->slots_arch_lock); + +gmap_enable_cmm_already_locked (mc, gmap); +} +#endif /* KVM_S390_MANAGES_S390_GUEST */ #endif /* ARCH_KVM_GMAP_GMAP_H */
Signature · 1 line
2.56.1
[email protected]
AI reviewRe: [PATCH v1 03/12] KVM: s390: Introduce gmap_enable_cmm()
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] KVM: s390: missing vSIE unshadow in gmap_enable_cmm_already_locked() - [Medium] KVM: s390: premature…
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] KVM: s390: missing vSIE unshadow in gmap_enable_cmm_already_locked()
- [Medium] KVM: s390: premature flag set in gmap_enable_cmm_already_locked()
- [Medium] KVM: s390: gmap_enable_cmm_already_locked() leaves hugepage flags set
--
commit 0b86eaed62523c8498a79fd700936cedd248584a
Author: Claudio Imbrenda <[email protected]>
KVM: s390: Introduce gmap_enable_cmm()
This commit introduces gmap_enable_cmm_already_locked() and gmap_enable_cmm() to mark a gmap as using CMMA, split existing large page mappings, and pre-allocate 4K page tables across guest memslots on a best effort basis.
Quoted 17 lines
arch/s390/kvm/gmap/gmap.c +9
-0@@ -1527,3 +1527,51 @@ struct gmap *gmap_create_shadow(struct kvm_s390_mmu_cache *mc, struct gmap *pare }return new; } + +#if KVM_S390_MANAGES_S390_GUEST +/* Bail out for any allocation failure. This is only a best effort to allocate all page tables. */ +void gmap_enable_cmm_already_locked (struct kvm_s390_mmu_cache *mc,struct gmap *gmap) +{ +struct kvm_memory_slot *ms; +struct kvm_memslots *slots; +gfn_t gfn, start, end; +union crste *crstep;
arch/s390/kvm/gmap/gmap.c struct gmap *gmap_create_shadow(struct kvm_s390_mmu_cache *mc, struct gmap *pare · line 1548
Quoted 10 lines
arch/s390/kvm/gmap/gmap.c +10
-0+union pte *ptep; +int flags, bkt; + +lockdep_assert_held (&gmap->kvm->srcu); +lockdep_assert_held (&gmap->kvm->slots_arch_lock); + +if (uses_cmm (gmap)) +return ; + +set_bit (GMAP_FLAG_USES_CMM , &gmap->flags);
[Severity: Medium]
Should gmap_enable_cmm_already_locked() also clear GMAP_FLAG_ALLOW_HPAGE_1M and GMAP_FLAG_ALLOW_HPAGE_2G on gmap->flags?
Because those flags remain set, any segment that does not keep a pre-allocated 4K page table (for instance, when a memslot is created or moved after CMMA is enabled, or when kvm_s390_mmu_cache_topup() fails mid-loop) will still satisfy gmap_1m_allowed() or gmap_2g_allowed() in gmap_link() and be mapped with 1M or 2G large pages, causing subsequent CMMA operations such as dat_set_cmma_bits() to fail with -EFBIG.
arch/s390/kvm/gmap/gmap.c struct gmap *gmap_create_shadow(struct kvm_s390_mmu_cache *mc, struct gmap *pare · line 1555
Quoted 7 lines
arch/s390/kvm/gmap/gmap.c +7
-0+ + slots =kvm_memslots (gmap->kvm); +if (!slots ||kvm_memslots_empty (slots)) +return ; + +if (WARN_ON_ONCE (is_ucontrol (gmap))) +return ;
[Severity: Medium]
Does setting GMAP_FLAG_USES_CMM before the memslot walk allow concurrent callers of gmap_enable_cmm() to return early before page tables are allocated and large pages are split?
CPU0 CPU1gmap_enable_cmm()
mutex_lock(&kvm->slots_arch_lock)
gmap_enable_cmm_already_locked()
set_bit(GMAP_FLAG_USES_CMM, ...)
gmap_enable_cmm()
if (uses_cmm(gmap))
return; <-- skips lock
for (gfn = start; gfn < end; ...)
kvm_s390_mmu_cache_topup(mc)
dat_entry_walk(...)Because gmap_enable_cmm() checks uses_cmm(gmap) without holding slots_arch_lock, CPU1 observes GMAP_FLAG_USES_CMM while CPU0 is still splitting large pages and sleeping in kvm_s390_mmu_cache_topup(). Setting the flag before the is_ucontrol(gmap) check also leaves GMAP_FLAG_USES_CMM set if is_ucontrol(gmap) triggers.
Quoted 12 lines
arch/s390/kvm/gmap/gmap.c +12
-0+ + flags =DAT_WALK_ALLOC |DAT_WALK_SPLIT ; + +kvm_for_each_memslot (ms, bkt, slots) { + start =ALIGN (ms->base_gfn, _PAGE_ENTRIES); + end =ALIGN_DOWN (ms->base_gfn + ms->npages, _PAGE_ENTRIES); + +for (gfn = start; gfn < end; gfn += _PAGE_ENTRIES) { +if (kvm_s390_mmu_cache_topup (mc)) +return ; + +guard (write_lock)(&gmap->kvm->mmu_lock);
arch/s390/kvm/gmap/gmap.c struct gmap *gmap_create_shadow(struct kvm_s390_mmu_cache *mc, struct gmap *pare · line 1577
Quoted 10 lines
arch/s390/kvm/gmap/gmap.c +10
-0+ +if (uses_skeys (gmap)) + flags |=DAT_WALK_USES_SKEYS ; + +dat_entry_walk (mc, gfn, gmap->asce, flags,TABLE_TYPE_PAGE_TABLE , + &crstep, &ptep); + } + } +} +#endif /* KVM_S390_MANAGES_S390_GUEST */
[Severity: High]
Can splitting large pages via dat_entry_walk() with DAT_WALK_SPLIT leave stale 1M or 2G mappings in vSIE shadow gmaps?
When _do_shadow_crste() shadows a 1M or 2G large page, it records the rmap entry in sg->host_to_rmap only at the 1M-aligned or 2G-aligned base GFN (f->gfn & mask) and sets s.fc1.vsie_notif on the large CRSTE. Unlike gmap_split_huge_pages() -> _gmap_split_crste(), which calls gmap_handle_vsie_unshadow_event() before splitting, dat_entry_walk() with DAT_WALK_SPLIT calls dat_split_crste() and dat_split_ste() directly, copying vsie_notif into all 4K PGSTEs without unshadowing sg:
arch/s390/kvm/gmap/dat.c:dat_split_ste() {
...
pgste_init.prefix_notif = old.s.fc1.prefix_notif;
pgste_init.vsie_notif = old.s.fc1.vsie_notif;
pgste_init.vsie_gmem = old.s.fc1.vsie_notif;
...
}If a 4K subpage at a non-zero offset within that former large page is later unmapped or write-protected, _gmap_handle_vsie_unshadow_event() looks up that exact 4K GFN in sg->host_to_rmap, misses the rmap entry at the aligned base GFN, and leaves the stale large mapping active in sg->asce.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Claudio Imbrenda
maintainerPATCH 4/12 KVM: s390: Allocate page tables when CMMA is used
In reply to Claudio ImbrendaWhen ESSA is executed in the guest for the first time, allocate all page tables and split all existing large page mappings, regardless of the hpage parameter. This also applies if the first ESSA instruction was performed in migration mode. Also fence UCONTROL VMs from using the KVM_S390_SET_CMMA_BITS ioctl, returning -EINVAL.
With the page tables present (even if all the entries within are not present), ESSA will not cause page faults.
Without this fix, ESSA will cause a fault on the first page of each segment (1M block). If userspace is backing the guest with huge pages (hugetlbfs or THP), the page fault will be resolved by faulting in the whole huge page. The result is that at startup the whole guest memory is faulted in, since the guest will mark all of its memory at boot.
With this fix, no extra guest memory is allocated. This will also prevent the first page in each segment from being faulted in, regardless of hugepages.
--- arch/s390/kvm/s390/priv.c | 11 +++++++---- arch/s390/kvm/s390/s390.c | 9 +++++++-- 2 files changed, 14 insertions(+), 6 deletions(-)
arch/s390/kvm/s390/priv.c +7 -4
@@ -1241,14 +1241,15 @@ static int handle_essa(struct kvm_vcpu *vcpu)if (!READ_ONCE (vcpu->kvm->arch.migration_mode)) {/* * CMMA is enabled in the KVM settings, but is disabled in -* the SIE block and in the mm_context, and we are not doing -* a migration. Enable CMMA in the mm_context. -* Since we need to take a write lock to write to the context +* the SIE block and in the gmap, and we are not doing a +* migration. Enable CMMA in the gmap. +* Since we need to take a mutex to write to the gmap * to avoid races with storage keys handling, we check if the * value really needs to be written to; if the value is * already correct, we do nothing and avoid the lock. */ -set_bit (GMAP_FLAG_USES_CMM , &vcpu->arch.gmap->flags); +if (!uses_cmm (vcpu->arch.gmap)) +gmap_enable_cmm (vcpu->arch.mc, vcpu->arch.gmap);/* * If we are here, we are supposed to have CMMA enabled in * the SIE block. Enabling CMMA works on a per-CPU basis,
@@ -1262,6 +1263,8 @@ static int handle_essa(struct kvm_vcpu *vcpu)/* Retry the ESSA instruction */ kvm_s390_retry_instr (vcpu); }else { +if (!uses_cmm (vcpu->arch.gmap)) +gmap_enable_cmm (vcpu->arch.mc, vcpu->arch.gmap); scoped_guard(mutex, &vcpu->kvm->slots_arch_lock)scoped_guard (read_lock, &vcpu->kvm->mmu_lock) i = __do_essa(vcpu, orc);
arch/s390/kvm/s390/s390.c +7 -2
@@ -2349,6 +2349,9 @@ static int kvm_s390_set_cmma_bits(struct kvm *kvm,if (r)return -EFAULT ; + scoped_guard(srcu, &kvm->srcu) +gmap_enable_cmm_already_locked (mc, kvm->arch.gmap); + do { r = kvm_s390_mmu_cache_topup(mc); if (r)
@@ -2359,8 +2362,6 @@ static int kvm_s390_set_cmma_bits(struct kvm *kvm, } }while (r == -ENOMEM ); -set_bit (GMAP_FLAG_USES_CMM , &kvm->arch.gmap->flags); -return r <=0 ? r : -EFAULT ; }
@@ -2991,6 +2992,10 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) r = -EFAULT ;if (copy_from_user (&args, argp,sizeof (args)))break ; +if (kvm_is_ucontrol (kvm)) { + r = -EINVAL ; +break ; + }mutex_lock (&kvm->slots_arch_lock); r =kvm_s390_set_cmma_bits (kvm, &args);mutex_unlock (&kvm->slots_arch_lock);
Signature · 1 line
2.56.1
[email protected]
AI reviewRe: [PATCH v1 04/12] KVM: s390: Allocate page tables when CMMA is used
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] KVM: s390: premature GMAP_FLAG_USES_CMM publication in gmap_enable_cmm() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] KVM: s390: premature GMAP_FLAG_USES_CMM publication in gmap_enable_cmm()
--
commit 4eafe3dbf444099ded4397c5a1073f2aff0effc2
Author: Claudio Imbrenda <[email protected]>
KVM: s390: Allocate page tables when CMMA is used
This commit calls gmap_enable_cmm() on the first ESSA intercept or KVM_S390_SET_CMMA_BITS ioctl to preallocate page tables and split large page mappings, and rejects KVM_S390_SET_CMMA_BITS on ucontrol VMs.
Quoted 14 lines
arch/s390/kvm/s390/priv.c +3
-3@@ -1243,14 +1243,15 @@ static int handle_essa(struct kvm_vcpu *vcpu)if (!READ_ONCE (vcpu->kvm->arch.migration_mode)) {/* * CMMA is enabled in the KVM settings, but is disabled in -* the SIE block and in the mm_context, and we are not doing -* a migration. Enable CMMA in the mm_context. -* Since we need to take a write lock to write to the context +* the SIE block and in the gmap, and we are not doing a +* migration. Enable CMMA in the gmap. +* Since we need to take a mutex to write to the gmap
arch/s390/kvm/s390/priv.c static int handle_essa(struct kvm_vcpu *vcpu) · line 1257
Quoted 10 lines
arch/s390/kvm/s390/priv.c +2
-1* to avoid races with storage keys handling, we check if the * value really needs to be written to; if the value is * already correct, we do nothing and avoid the lock. */ -set_bit (GMAP_FLAG_USES_CMM , &vcpu->arch.gmap->flags); +if (!uses_cmm (vcpu->arch.gmap)) +gmap_enable_cmm (vcpu->arch.mc, vcpu->arch.gmap);/* * If we are here, we are supposed to have CMMA enabled in * the SIE block. Enabling CMMA works on a per-CPU basis,
[Severity: Medium]
Can a second vCPU observe uses_cmm(vcpu->arch.gmap) == true before gmap_enable_cmm() has finished allocating page tables and splitting large pages?
gmap_enable_cmm_already_locked() sets GMAP_FLAG_USES_CMM before walking the memslots:
arch/s390/kvm/gmap/gmap.c:gmap_enable_cmm_already_locked() {
if (uses_cmm(gmap))
return;
set_bit(GMAP_FLAG_USES_CMM, &gmap->flags);
...
kvm_for_each_memslot(ms, bkt, slots) {
...
for (gfn = start; gfn < end; gfn += _PAGE_ENTRIES) {
if (kvm_s390_mmu_cache_topup(mc))
return;
guard(write_lock)(&gmap->kvm->mmu_lock);
...
dat_entry_walk(mc, gfn, gmap->asce, flags, ...);
}
}
}Because both handle_essa() and gmap_enable_cmm() check uses_cmm() locklessly before taking slots_arch_lock, another vCPU intercepting on ESSA while the memslot walk is in progress can skip gmap_enable_cmm() and re-enter SIE with ECB2_CMMA enabled:
CPU0 CPU1handle_essa()
gmap_enable_cmm()
mutex_lock(&slots_arch_lock)
gmap_enable_cmm_already_locked()
set_bit(GMAP_FLAG_USES_CMM, ...)
handle_essa()
if (!uses_cmm(...)) -> false
sie_block->ecb2 |= ECB2_CMMA
kvm_s390_retry_instr()
<re-enters SIE and runs ESSA
on unvisited segments>
kvm_for_each_memslot(...)
dat_entry_walk(...)CPU1 then executes ESSA in hardware on segments that still lack page tables or have unsplit large pages, triggering segment translation faults and faulting in backing hugepages.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Claudio Imbrenda
maintainerPATCH 5/12 KVM: s390: Fix dat_entry_walk() with DAT_WALK_CONTINUE
In reply to Claudio ImbrendaWhen dat_entry_walk() is called specifying the DAT_WALK_CONTINUE flag, the page table walk continues past the specified level, towards ptes.
If TABLE_TYPE_REGION3 (pud) was specified together with DAT_WALK_CONTINUE, but the segment table entry (pmd) is invalid or a large entry, the current code will return a failure, instead of the pmd.
Fix by overriding walk_level with TABLE_TYPE_SEGMENT instead of TABLE_TYPE_PAGE_TABLE. The code handling segment table entries (pmd) will correctly handle the situation and continue to ptes if needed.
--- arch/s390/kvm/gmap/dat.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/s390/kvm/gmap/dat.c +1 -1
@@ -425,7 +425,7 @@ int dat_entry_walk(struct kvm_s390_mmu_cache *mc, gfn_t gfn, union asce asce, inreturn entry.tok.type == _DAT_TOKEN_PIC ? entry.tok.par : -EFAULT ;if (walk_level ==TABLE_TYPE_REGION3 && continue_anyway && !entry.pud.h.fc && !entry.h.i) { - walk_level =TABLE_TYPE_PAGE_TABLE ; + walk_level =TABLE_TYPE_SEGMENT ; allocate = false; }if (walk_level ==TABLE_TYPE_REGION3 || ((leaf || any) && entry.pud.h.fc))
Signature · 1 line
2.56.1
Claudio Imbrenda
maintainerPATCH 6/12 KVM: s390: Fix soft-dirty handling in _gmap_link()
In reply to Claudio ImbrendaWhen resolving a fault in the slow path, via _gmap_link(), the soft-dirty bit of the old entry is used for the new entry, overwriting whatever value it had.
If the new entry would have set the soft-dirty bit, it is overwritten with the old value, potentially losing the soft-dirty status.
Fix by ORing the soft-dirty bit in the new entries with the old one. This way a soft-dirty entry stays soft-dirty, but a previously clean page can become soft-dirty.
Also fix improper check of the old crste. Use crste_none() instead of the improper open coded comparison to _CRSTE_EMPTY(oldval.h.tt).
--- arch/s390/kvm/gmap/gmap.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
arch/s390/kvm/gmap/gmap.c +3 -3
@@ -825,7 +825,7 @@ static int _gmap_link(struct kvm_s390_mmu_cache *mc, struct gmap *gmap, int leve pgste =pgste_get_lock (f->ptep); oldpte = *f->ptep; newpte =_pte (f->pfn, f->writable, f->write_attempt | oldpte.s.d, !f->page); - newpte.s.sd = oldpte.s.sd; + newpte.s.sd |= oldpte.s.sd; oldpte.s.sd =0 ;if (oldpte.val == _PTE_EMPTY.val || oldpte.h.pfra == f->pfn) { pgste =gmap_ptep_xchg (gmap, f->ptep, newpte, pgste, f->gfn);
@@ -841,8 +841,8 @@ static int _gmap_link(struct kvm_s390_mmu_cache *mc, struct gmap *gmap, int leve newval =_crste_fc1 (f->pfn, oldval.h.tt, f->writable, f->write_attempt | oldval.s.fc1.d); newval.s.fc1.s = !f->page; - newval.s.fc1.sd = oldval.s.fc1.sd; -if (oldval.val !=_CRSTE_EMPTY (oldval.h.tt).val && + newval.s.fc1.sd |= oldval.s.fc1.sd; +if (!crste_none (oldval) &&crste_origin_large (oldval) !=crste_origin_large (newval))return -EAGAIN ; f->crste_region3 =is_pud (newval);
Signature · 1 line
2.56.1
Claudio Imbrenda
maintainerPATCH 7/12 KVM: s390: Fix race in gmap_clear_young_pte()
In reply to Claudio ImbrendaIn gmap_clear_young_pte(), pte is being READ_ONCE() outside of the lock for an opportunistic check, and then the same value is reused inside the lock without checking again.
If the actual value of the pte has changed between the opportunistic check and the lock, a stale value for pte is used and written to the pte.
Fix by reading the value of the pte again inside the lock, and performing the check again.
--- arch/s390/kvm/gmap/gmap.c | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-)
arch/s390/kvm/gmap/gmap.c +16 -12
@@ -302,19 +302,23 @@ static long gmap_clear_young_pte(union pte *ptep, gfn_t gfn, gfn_t end, struct dreturn 0 ; pgste =pgste_get_lock (ptep); -if (!pgste.prefix_notif ||gmap_mkold_prefix (p->gmap, gfn, end)) { - new = pte; - new.h.i =1 ; - new.s.y =0 ; -if ((new.s.d || !new.h.p) && !new.s.s) -folio_set_dirty (pfn_folio (pte.h.pfra)); - new.s.d =0 ; - new.h.p =1 ; - - pgste.prefix_notif =0 ; - pgste =__dat_ptep_xchg (ptep, pgste, new, gfn, walk->asce,uses_skeys (p->gmap)); + pte =READ_ONCE (*ptep); +if (pte.s.pr && (pte.s.y || !pte.h.i)) { +if (!pgste.prefix_notif ||gmap_mkold_prefix (p->gmap, gfn, end)) { + new = pte; + new.h.i =1 ; + new.s.y =0 ; +if ((new.s.d || !new.h.p) && !new.s.s) +folio_set_dirty (pfn_folio (pte.h.pfra)); + new.s.d =0 ; + new.h.p =1 ; + + pgste.prefix_notif =0 ; + pgste =__dat_ptep_xchg (ptep, pgste, new, gfn, walk->asce, +uses_skeys (p->gmap)); + } + p->young =1 ; } - p->young =1 ;pgste_set_unlock (ptep, pgste);return 0 ; }
Signature · 1 line
2.56.1
Claudio Imbrenda
maintainerPATCH 8/12 KVM: s390: Properly adjust page size before calling _gmap_link()
In reply to Claudio ImbrendaChecking the size of the folio alone is not enough to determine the maximum allowed page size for a mapping. Multiple vmas with different permissions could be sharing a single large folio.
Fix by checking the actual size of the userspace mapping if the folio was large enough, and adjust the size accordingly before attempting to use large pmds or puds.
This reflects the behaviour on other architectures.
--- arch/s390/kvm/gmap/gmap.c | 88 +++++++++++++++++++++++++++++++++++---- 1 file changed, 79 insertions(+), 9 deletions(-)
arch/s390/kvm/gmap/gmap.c +79 -9
@@ -858,22 +858,92 @@ static int _gmap_link(struct kvm_s390_mmu_cache *mc, struct gmap *gmap, int levereturn rc; } +/** +* gmap_get_userspace_page_size() - Check the actual page size of the mapping +* @kvm: the guest +* @gfn: the guest address whose page size is to be checked +* @slot: the KVM memslot containing @gfn +* +* Manually walk the userspace page tables to find out the page size. +* +* This is safe because kvm->mmu_lock is being held; being a (r/w) spinlock, +* preemption is disabled while it is being held. +* Due to RCU-deferred freeing of page tables, that is enough to guarantee +* that the userspace tables won't disappear while being walked. +* +* Context: kvm->mmu_lock must be held. +* Return: +* * TABLE_TYPE_REGION3 if userspace is mapping with a large pud, +* * TABLE_TYPE_SEGMENT if userspace is mapping with a large pmd, +* * TABLE_TYPE_PAGE_TABLE otherwise, including missing mappings. +*/ +static int gmap_get_userspace_page_size (struct kvm *kvm,gfn_t gfn,struct kvm_memory_slot *slot) +{ +hva_t hva; +pgd_t pgd; +p4d_t p4d; +pud_t pud; +pmd_t pmd; + +lockdep_assert_held (&kvm->mmu_lock); + + hva =__gfn_to_hva_memslot (slot, gfn); + + pgd =READ_ONCE (*pgd_offset (kvm->mm, hva)); +if (pgd_none (pgd)) +return TABLE_TYPE_PAGE_TABLE ; + + p4d =READ_ONCE (*p4d_offset (&pgd, hva)); +if (p4d_none (p4d) || !p4d_present (p4d)) +return TABLE_TYPE_PAGE_TABLE ; + + pud =READ_ONCE (*pud_offset (&p4d, hva)); +if (pud_none (pud) || !pud_present (pud)) +return TABLE_TYPE_PAGE_TABLE ; +if (pud_leaf (pud)) +return TABLE_TYPE_REGION3 ; + + pmd =READ_ONCE (*pmd_offset (&pud, hva)); +if (pmd_none (pmd) || !pmd_present (pmd)) +return TABLE_TYPE_PAGE_TABLE ; +if (pmd_leaf (pmd)) +return TABLE_TYPE_SEGMENT ; + +return TABLE_TYPE_PAGE_TABLE ; +} + +static int gmap_determine_fault_page_size (struct gmap *gmap,struct guest_fault *f, +struct kvm_memory_slot *slot) +{ +unsigned int order; +int level; + +if (!f->page) +return TABLE_TYPE_PAGE_TABLE ; + + order =folio_order (page_folio (f->page)); + level =TABLE_TYPE_PAGE_TABLE ; + +if (order >=get_order (_REGION3_SIZE) &&gmap_2g_allowed (gmap, f, slot)) + level =TABLE_TYPE_REGION3 ; +else if (order >=get_order (_SEGMENT_SIZE) &&gmap_1m_allowed (gmap, f, slot)) + level =TABLE_TYPE_SEGMENT ; + +if (level ==TABLE_TYPE_PAGE_TABLE ) +return level; + +return min (level,gmap_get_userspace_page_size (gmap->kvm, f->gfn, slot)); +} +int gmap_link (struct kvm_s390_mmu_cache *mc,struct gmap *gmap,struct guest_fault *f,struct kvm_memory_slot *slot) { -unsigned int order;int level;lockdep_assert_held (&gmap->kvm->mmu_lock); - level =TABLE_TYPE_PAGE_TABLE ; -if (f->page) { - order =folio_order (page_folio (f->page)); -if (order >=get_order (_REGION3_SIZE) &&gmap_2g_allowed (gmap, f, slot)) - level =TABLE_TYPE_REGION3 ; -else if (order >=get_order (_SEGMENT_SIZE) &&gmap_1m_allowed (gmap, f, slot)) - level =TABLE_TYPE_SEGMENT ; - } + level =gmap_determine_fault_page_size (gmap, f, slot); +return _gmap_link (mc, gmap, level, f); }
Signature · 1 line
2.56.1
Claudio Imbrenda
maintainerPATCH 9/12 KVM: s390: Fix *_test_and_clear_softdirty() functions
In reply to Claudio ImbrendaDo not skip non-present entries, as they can be soft-dirty, and need to be accounted as such.
Check for the invalid bit together with the protected bit when determining whether to skip an entry.
Do not set the protected bit if the invalid bit is still set; the invalid bit is enough.
--- arch/s390/kvm/gmap/gmap.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
arch/s390/kvm/gmap/gmap.c +8 -6
@@ -585,7 +585,7 @@ static union pgste __pte_test_and_clear_softdirty(union pte *ptep, union pgste p {union pte pte =READ_ONCE (*ptep); -if (!pte.s.pr || (pte.h.p && !pte.s.sd)) +if ((pte.h.p || pte.h.i) && !pte.s.sd)return pgste;/*
@@ -593,7 +593,8 @@ static union pgste __pte_test_and_clear_softdirty(union pte *ptep, union pgste p* running, do not reset the protection, leave it marked as dirty. */ if (!pgste.prefix_notif ||gmap_mkold_prefix (gmap, gfn, gfn +1 )) { - pte.h.p =1 ; +if (!pte.h.i) + pte.h.p =1 ; pte.s.sd =0 ; pgste =gmap_ptep_xchg (gmap, ptep, pte, pgste, gfn); }
@@ -625,9 +626,9 @@ static long _crste_test_and_clear_softdirty(union crste *table, gfn_t gfn, gfn_treturn 1 ; do { crste = READ_ONCE(*table); - if (!crste.h.fc) + if (!crste.h.fc && !crste.h.i) return 0; - if (crste.h.p && !crste.s.fc1.sd) + if ((crste.h.p || crste.h.i) && !crste.s.fc1.sd) return 0; /*
@@ -635,10 +636,11 @@ static long _crste_test_and_clear_softdirty(union crste *table, gfn_t gfn, gfn_t* currently running, do not reset the protection, leave it marked as * dirty. */ -if (crste.s.fc1.prefix_notif && !gmap_mkold_prefix (gmap, gfn, end)) +if (crste.h.fc && crste.s.fc1.prefix_notif && !gmap_mkold_prefix (gmap, gfn, end))break ; new = crste; - new.h.p =1 ; +if (!crste.h.i) + new.h.p =1 ; new.s.fc1.sd =0 ; }while (!gmap_crstep_xchg_atomic (gmap, table, crste, new, gfn));
Signature · 1 line
2.56.1
[email protected]
AI reviewRe: [PATCH v1 09/12] KVM: s390: Fix *_test_and_clear_softdirty() functions
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] KVM: s390: __pte_test_and_clear_softdirty() clears pgste.zero on invalid PTEs --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] KVM: s390: __pte_test_and_clear_softdirty() clears pgste.zero on invalid PTEs
--
commit d9b61cfa531bfdb9496d9935489a88705ec5abb5
Author: Claudio Imbrenda <[email protected]>
KVM: s390: Fix *_test_and_clear_softdirty() functions
This commit updates __pte_test_and_clear_softdirty() and _crste_test_and_clear_softdirty() to process non-present soft-dirty entries, check the invalid bit alongside the protected bit when skipping clean entries, and avoid setting the protected bit on invalid entries.
Quoted 14 lines
arch/s390/kvm/gmap/gmap.c +1
-1@@ -585,7 +585,7 @@ static union pgste __pte_test_and_clear_softdirty(union pte *ptep, union pgste p {union pte pte =READ_ONCE (*ptep); -if (!pte.s.pr || (pte.h.p && !pte.s.sd)) +if ((pte.h.p || pte.h.i) && !pte.s.sd)return pgste;/*
arch/s390/kvm/gmap/gmap.c static union pgste __pte_test_and_clear_softdirty(union pte *ptep, union pgste p · line 600
Quoted 9 lines
arch/s390/kvm/gmap/gmap.c +2
-1@@ -593,7 +593,8 @@ static union pgste __pte_test_and_clear_softdirty(union pte *ptep, union pgste p* running, do not reset the protection, leave it marked as dirty. */ if (!pgste.prefix_notif ||gmap_mkold_prefix (gmap, gfn, gfn +1 )) { - pte.h.p =1 ; +if (!pte.h.i) + pte.h.p =1 ; pte.s.sd =0 ; pgste =gmap_ptep_xchg (gmap, ptep, pte, pgste, gfn); }
[Severity: High]
Does calling gmap_ptep_xchg() on an invalid or non-present PTE (pte.h.i == 1) clear pgste.zero?
arch/s390/kvm/gmap/gmap.h:_gmap_ptep_xchg() {
...
pgste.zero = 0;
return __dat_ptep_xchg(ptep, pgste, newpte, gfn, gmap->asce, uses_skeys(gmap));
}When a dirty log sync clears pte.s.sd on an invalid PTE that was discarded via ESSA (pgste.zero == 1), zeroing pgste.zero while pte.h.i remains 1 causes subsequent ESSA operations to report the page content state as preserved (2) instead of logically zero (3), leading to guest memory corruption if the guest relies on ESSA state to detect discarded volatile pages.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
Claudio Imbrenda
maintainerPATCH 10/12 KVM: s390: Do not unnecessarily drop the soft-dirty bit
In reply to Claudio ImbrendaProperly preserve the soft-dirty bit when performing unmaps or other similar operations.
The soft-dirty bit is only cleared by gmap_sync_dirty_log(), which also properly marks the dirty pages as such in the dirty bitmap.
The previous behaviour could lose the soft-dirty bit in some circumstances.
--- arch/s390/kvm/gmap/gmap.c | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-)
arch/s390/kvm/gmap/gmap.c +27 -9
@@ -389,6 +389,7 @@ static long _gmap_unmap_pte(union pte *ptep, gfn_t gfn, gfn_t next, struct dat_wstruct folio *folio =NULL ;unsigned long vmaddr;union pgste pgste; +union pte newpte; pgste =pgste_get_lock (ptep);if (ptep->s.pr && pgste.usage ==PGSTE_GPS_USAGE_UNUSED ) {
@@ -397,7 +398,9 @@ static long _gmap_unmap_pte(union pte *ptep, gfn_t gfn, gfn_t next, struct dat_w }if (ptep->s.pr &&test_bit (GMAP_FLAG_EXPORT_ON_UNMAP , &priv->gmap->flags)) folio =pfn_folio (ptep->h.pfra); - pgste =gmap_ptep_xchg (priv->gmap, ptep, _PTE_EMPTY, pgste, gfn); + newpte = _PTE_EMPTY; + newpte.s.sd = ptep->s.sd; + pgste =gmap_ptep_xchg (priv->gmap, ptep, newpte, pgste, gfn);pgste_set_unlock (ptep, pgste);if (folio)uv_convert_from_secure_folio (folio);
@@ -408,8 +411,8 @@ static long _gmap_unmap_pte(union pte *ptep, gfn_t gfn, gfn_t next, struct dat_wstatic long _gmap_unmap_crste (union crste *crstep,gfn_t gfn,gfn_t next,struct dat_walk *walk) {struct gmap_unmap_priv *priv = walk->priv; +union crste newcrste, old = *crstep;struct folio *folio =NULL ; -union crste old = *crstep;bool ok;if (!old.h.fc)
@@ -417,6 +420,9 @@ static long _gmap_unmap_crste(union crste *crstep, gfn_t gfn, gfn_t next, structif (old.s.fc1.pr &&test_bit (GMAP_FLAG_EXPORT_ON_UNMAP , &priv->gmap->flags)) folio =phys_to_folio (crste_origin_large (old)); + + newcrste =_CRSTE_EMPTY (old.h.tt); + newcrste.s.fc1.sd = old.s.fc1.sd;/* * No races should happen because kvm->mmu_lock is held in write mode, * but the unmap operation could have triggered an unshadow, which
@@ -425,11 +431,11 @@ static long _gmap_unmap_crste(union crste *crstep, gfn_t gfn, gfn_t next, struct* had the vsie_notif bit set. A second failure is not allowed, for * the reasons above. */ - ok =gmap_crstep_xchg_atomic (priv->gmap, crstep, old,_CRSTE_EMPTY (old.h.tt), gfn); + ok =gmap_crstep_xchg_atomic (priv->gmap, crstep, old, newcrste, gfn);if (!ok) {KVM_BUG_ON (!old.s.fc1.vsie_notif, priv->gmap->kvm); old.s.fc1.vsie_notif =0 ; - ok =gmap_crstep_xchg_atomic (priv->gmap, crstep, old,_CRSTE_EMPTY (old.h.tt), gfn); + ok =gmap_crstep_xchg_atomic (priv->gmap, crstep, old, newcrste, gfn);KVM_BUG_ON (!ok, priv->gmap->kvm); }if (folio)
@@ -459,7 +465,7 @@ static bool gmap_unmap_gfn_range_slow(struct gmap *gmap, struct kvm_memory_slotstatic long _gmap_unmap_pud_fast (union crste *crstep,gfn_t gfn,gfn_t next,struct dat_walk *w) { -union crste old = *crstep; +union crste newcrste, old = *crstep;/* We only care about large puds */ if (!old.h.fc)
@@ -480,14 +486,17 @@ static long _gmap_unmap_pud_fast(union crste *crstep, gfn_t gfn, gfn_t next, stridte_crste (crstep, gfn,IDTE_GUEST_ASCE , w->asce,0 ); else idte_crste(crstep, gfn, 0, w->asce, 0); - *crstep =_CRSTE_EMPTY (old.h.tt); + + newcrste =_CRSTE_EMPTY (old.h.tt); + newcrste.s.fc1.sd = old.s.fc1.sd; + *crstep = newcrste;return 0 ; }static long _gmap_unmap_pmd_fast (union crste *crstep,gfn_t gfn,gfn_t next,struct dat_walk *w) { -union crste old = *crstep; +union crste newcrste, old = *crstep;struct page_table *pt;gfn_t first, last;unsigned int i;
@@ -500,7 +509,10 @@ static long _gmap_unmap_pmd_fast(union crste *crstep, gfn_t gfn, gfn_t next, stridte_crste (crstep, gfn,IDTE_GUEST_ASCE , w->asce,0 ); else idte_crste(crstep, gfn, 0, w->asce, 0); -WRITE_ONCE (*crstep,_CRSTE_EMPTY (old.h.tt)); + + newcrste =_CRSTE_EMPTY (old.h.tt); + newcrste.s.fc1.sd = old.s.fc1.sd; + *crstep = newcrste;return 0 ; }
@@ -523,11 +535,15 @@ static long _gmap_unmap_pmd_fast(union crste *crstep, gfn_t gfn, gfn_t next, stridte_crste (crstep, gfn,0 , w->asce,0 );for (i = first; i <= last; i++) { +union pte newpte; +if (pte_hole (pt->ptes[i]))continue ;if (!pt->ptes[i].h.i &&uses_skeys (w->priv)) pt->pgstes[i] =dat_save_storage_key_into_pgste (pt->ptes[i], pt->pgstes[i]); - pt->ptes[i] = _PTE_EMPTY; + newpte = _PTE_EMPTY; + newpte.s.sd = pt->ptes[i].s.sd; + pt->ptes[i] = newpte; pt->pgstes[i].vsie_notif =0 ; pt->pgstes[i].vsie_gmem =0 ; pt->pgstes[i].zero =0 ;
@@ -1115,6 +1131,7 @@ static long _gmap_split_crste(union crste *crstep, gfn_t gfn, gfn_t next, structgmap_unmap_prefix (gmap, gfn, next);if (crste.s.fc1.vsie_notif)gmap_handle_vsie_unshadow_event (gmap, gfn); + newcrste.s.fc1.sd = crste.s.fc1.sd;if (dat_crstep_xchg_atomic (crstep, crste, newcrste, gfn, walk->asce))break ; crste =READ_ONCE (*crstep);
@@ -1290,6 +1307,7 @@ int gmap_protect_rmap(struct kvm_s390_mmu_cache *mc, struct gmap *sg, gfn_t p_gfreturn -EAGAIN ; pte = ptep->s.pr ? *ptep :_pte (pfn, wr, false, false); pte.h.p =1 ; + pte.s.sd |= ptep->s.sd; pgste =_gmap_ptep_xchg (sg->parent, ptep, pte, pgste, p_gfn, false); pgste.vsie_notif =1 ;pgste_set_unlock (ptep, pgste);
Signature · 1 line
2.56.1
[email protected]
AI reviewRe: [PATCH v1 10/12] KVM: s390: Do not unnecessarily drop the soft-dirty bit
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] KVM: s390: unresolvable commit SHA d56cd0c0d4df in Fixes trailer --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] KVM: s390: unresolvable commit SHA d56cd0c0d4df in Fixes trailer --
commit c65dbb8fcb17b43130bab25f810a7051e6e82044
Author: Claudio Imbrenda <[email protected]>
Quoted 8 lines
KVM: s390: Do not unnecessarily drop the soft-dirty bit
Properly preserve the soft-dirty bit when performing unmaps or other similar operations.
The soft-dirty bit is only cleared by
gmap_sync_dirty_log(), which also properly marks the dirty pages as such in the dirty bitmap.
Quoted 7 lines
The previous behaviour could lose the soft-dirty bit in some circumstances.
Fixes d56cd0c0d4df ("KVM: s390: Improve unmapping of large ranges")Fixes a2c17f9270cc ("KVM: s390: New gmap code")Signed-off-by Claudio Imbrenda <[email protected]>
[Severity: Low]
Commit sha d56cd0c0d4df in the first Fixes: trailer cannot be resolved in the repository. In git history, the commit with that title that introduced _gmap_unmap_pud_fast() and _gmap_unmap_pmd_fast() is d71e7918b459 ("KVM:
s390: Improve unmapping of large ranges"). Should the trailer be updated to:
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
Claudio Imbrenda
maintainerPATCH 11/12 KVM: s390: Disallow 2G pages for guests up to 2G
In reply to Claudio ImbrendaIf a guest of size exactly 2G is being backed with a 2G page by userspace, gmap will try to use a 2G page (large pud).
But due to the address space being <= 2G, a segment-type ASCE is used, which means that there are no puds. This causes some checks to fail and ultimately kill the VM.
Fix by disallowing 2G large pages for guests with a segment-type ASCE.
--- arch/s390/kvm/gmap/gmap.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/s390/kvm/gmap/gmap.c +2 -1
@@ -798,7 +798,8 @@ static inline bool gmap_2g_allowed(struct gmap *gmap, struct guest_fault *f,return test_bit (GMAP_FLAG_ALLOW_HPAGE_2G , &gmap->flags) && !((f->gfn ^ f->pfn) & ~_REGION3_FR_MASK) && slot->base_gfn <=ALIGN_DOWN (f->gfn, _PAGES_PER_REGION3) && - slot->base_gfn + slot->npages >=ALIGN (f->gfn +1 , _PAGES_PER_REGION3); + slot->base_gfn + slot->npages >=ALIGN (f->gfn +1 , _PAGES_PER_REGION3) && + gmap->asce.dt >ASCE_TYPE_SEGMENT ; }/**
Signature · 1 line
2.56.1
Claudio Imbrenda
maintainerPATCH 12/12 KVM: s390: vsie: Fix improper shadowing of non-present host ptes
In reply to Claudio ImbrendaIf a host pte that is being shadowed was empty in the old mapping, the new mapping might end up not write-protected, even if it should have been, since empty ptes do not have the p bit set.
Fix by only and-ing the old host p bit if the old entry was present.
--- arch/s390/kvm/s390/gaccess.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/s390/kvm/s390/gaccess.c +2 -1
@@ -1434,7 +1434,8 @@ static int _do_shadow_pte(struct kvm_s390_mmu_cache *mc, struct gmap *sg, gpa_t newpte =_pte (f->pfn, f->writable, !p, ptep_h->s.s); newpte.s.d |= ptep_h->s.d; newpte.s.sd |= ptep_h->s.sd; - newpte.h.p &= ptep_h->h.p; +if (ptep_h->s.pr) + newpte.h.p &= ptep_h->h.p;if (!newpte.h.p && !f->writable) { rc = -EOPNOTSUPP ; }else {
Signature · 1 line
2.56.1
[email protected]
AI reviewSashiko has finished reviewing this 12-patch series:
Sashiko has finished reviewing this 12-patch series:
- Patches 1, 2, 5-8, 11, and 12: no issues found.
- Patch 3 ("KVM: s390: Introduce gmap_enable_cmm()"): 3 potential issues (1 High, 2 Medium)
- Patch 4 ("KVM: s390: Allocate page tables when CMMA is used"): 1 potential issue (1 Medium)
- Patch 9 ("KVM: s390: Fix *_test_and_clear_softdirty() functions"): 1 potential issue (1 High)
- Patch 10 ("KVM: s390: Do not unnecessarily drop the soft-dirty bit"):
1 potential issue (1 Low)
Detailed reviews have been sent as separate replies to patches 3, 4, 9, and 10.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]