KVM: s390: Misc bugfixes for 7.4
Claudio Imbrenda
maintainerv3 cover KVM: s390: Misc bugfixes for 7.4
Misc bugfixes.
Some Fixes tags are approximate, since the bugs arise from slow changes behind the scenes in the kernel, and not from an actual bug at the moment of the introduction of the original code.
v2->v3:
* remove all non-bugfix patches
* rebase onto master
v1->v2:
* add "memory" clobber to idte_crste()
* remove superflouos gpa parameter for unpin_scb()
* clear the zero bit in the PGSTE when unmapping, add missing barrier
The remaining issues raised by sashiko in v1 are false positives.
Claudio Imbrenda (4):
KVM: s390: Fix UCONTROL inconsistencies
KVM: s390: Move some skey functions to headers
KVM: s390: Improve unmapping of large ranges
KVM: s390: vsie: Fix pinning using the wrong flag
arch/s390/kvm/gmap/dat.c | 26 ------ arch/s390/kvm/gmap/dat.h | 42 ++++++++- arch/s390/kvm/gmap/gmap.c | 178 +++++++++++++++++++++++++++++--------- arch/s390/kvm/s390/vsie.c | 137 +++++++++++------------------ 4 files changed, 230 insertions(+), 153 deletions(-)
Signature · 1 line
2.56.0
Claudio Imbrenda
maintainerPATCH v3 1/4 KVM: s390: Fix UCONTROL inconsistencies
For UCONTROL VMs, when a vCPU segment entry is mapped but unallocated, the tl field was set to 1. This collides with the present bit, which means that other parts of gmap would mistake the entry for a present one.
Refactor the code to use a different combination of flags to indicate a mapped but unallocated segment.
--- arch/s390/kvm/gmap/dat.h | 16 +++++++++++- arch/s390/kvm/gmap/gmap.c | 54 +++++++++++++++++++-------------------- 2 files changed, 41 insertions(+), 29 deletions(-)
arch/s390/kvm/gmap/dat.h +15 -1
@@ -109,6 +109,8 @@ union pte {#define _REGION3_FR_MASK (_REGION3_MASK >> PAGE_SHIFT)#define _PAGES_PER_SEGMENT _PAGE_ENTRIES#define _PAGES_PER_REGION3 (_PAGES_PER_SEGMENT * _CRST_ENTRIES) +#define _UCAS_ENTRY_LENGTH 0 +#define _UCAS_ENTRY_OFFSET 3/* Soft dirty, needed as macro for atomic operations on ptes */ #define _PAGE_SD 0x002
@@ -421,6 +423,17 @@ static inline union crste _crste_fc0(kvm_pfn_t pfn, int tt)return res; } +static inline union crste _crste_ucas (kvm_pfn_t pfn) +{ +union crste res = { .val =PFN_PHYS (pfn) }; + + res.h.i =1 ; + res.h.tt =TABLE_TYPE_SEGMENT ; + res.h.fc0.tl = _UCAS_ENTRY_LENGTH; + res.h.fc0.tf = _UCAS_ENTRY_OFFSET; +return res; +} +/** * _crste() - Useful constructor for union crste with FC=1 * @pfn: the pfn this pte should point to.
@@ -985,7 +998,8 @@ static inline int dat_create_slot(struct kvm_s390_mmu_cache *mc, union asce ascestatic inline bool crste_is_ucas (union crste crste) { -return is_pmd (crste) && crste.h.i && crste.h.fc0.tl ==1 && crste.h.fc ==0 ; +return is_pmd (crste) && crste.h.i && !crste.h.fc && + crste.h.fc0.tl == _UCAS_ENTRY_LENGTH && crste.h.fc0.tf == _UCAS_ENTRY_OFFSET; }#endif /* ARCH_KVM_GMAP_DAT_H */
arch/s390/kvm/gmap/gmap.c +26 -28
84 lines not on this page · raw message
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH v3 1/4] KVM: s390: Fix UCONTROL inconsistencies
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] KVM: s390: _crste_ucas() collides with _crste_hole() and _crste_none() --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] KVM: s390: _crste_ucas() collides with _crste_hole() and _crste_none() --
commit 8288fc810dc049ff30a9d687a1ca6d691bb508c2
Author: Claudio Imbrenda <[email protected]>
KVM: s390: Fix UCONTROL inconsistencies
This commit changes the flag encoding used for mapped but unallocated UCONTROL vCPU segment entries so that they do not set the present bit, and updates the UCAS gmap helpers accordingly.
Quoted 17 lines
arch/s390/kvm/gmap/dat.h +2
-0@@ -109,6 +109,8 @@ union pte {#define _REGION3_FR_MASK (_REGION3_MASK >> PAGE_SHIFT)#define _PAGES_PER_SEGMENT _PAGE_ENTRIES#define _PAGES_PER_REGION3 (_PAGES_PER_SEGMENT * _CRST_ENTRIES) +#define _UCAS_ENTRY_LENGTH 0 +#define _UCAS_ENTRY_OFFSET 3/* Soft dirty, needed as macro for atomic operations on ptes */ #define _PAGE_SD 0x002@@ -421,6 +423,17 @@ static inline union crste _crste_fc0(kvm_pfn_t pfn, int tt)return res; }
arch/s390/kvm/gmap/dat.h static inline union crste _crste_fc0(kvm_pfn_t pfn, int tt) · line 435
Quoted 10 lines
arch/s390/kvm/gmap/dat.h +10
-0+static inline union crste _crste_ucas (kvm_pfn_t pfn) +{ +union crste res = { .val =PFN_PHYS (pfn) }; + + res.h.i =1 ; + res.h.tt =TABLE_TYPE_SEGMENT ; + res.h.fc0.tl = _UCAS_ENTRY_LENGTH; + res.h.fc0.tf = _UCAS_ENTRY_OFFSET; +return res; +}
[Severity: High]
With _UCAS_ENTRY_LENGTH set to 0, _crste_ucas() leaves bit 63 (res.tok.pr) cleared while setting res.h.i = 1 and placing PFN_PHYS(pfn) across bits 0..51, which overlaps res.tok.type (bits 0..15) and res.tok.par (bits 16..31).
In arch/s390/kvm/gmap/dat.h, _crste_hole() and _crste_none() check:
static inline bool _crste_hole(union crste crste)
{
return crste.h.i && !crste.tok.pr && crste.tok.type != _DAT_TOKEN_NONE;
}#define crste_hole(x) _crste_hole(_CRSTE(x))
static inline bool _crste_none(union crste crste)
{
return crste.h.i && !crste.tok.pr && crste.tok.type == _DAT_TOKEN_NONE;
}When a UCONTROL segment is mapped via KVM_S390_UCAS_MAP to a host address with non-zero bits in 63:48 (i.e. PFN_PHYS(pfn) >= (1UL << 48), which is normal for 5-level s390x mmap addresses), crste_hole() evaluates to true for the UCAS entry.
While this patch passes DAT_WALK_IGN_HOLES in the gmap_ucas_*() helpers, other callers walk vcpu->arch.gmap->asce without DAT_WALK_IGN_HOLES, such as:
- vcpu_check_access_key_gpa() (via KVM_S390_MEM_OP) and handle_iske() -> dat_get_storage_key() -> dat_entry_walk(..., DAT_WALK_ANY, ...)
- handle_rrbe() -> dat_reset_reference_bit() -> dat_entry_walk(..., DAT_WALK_ANY, ...)
In dat_entry_walk(), hitting the segment entry triggers:
if (crste_hole(entry) && !ign_holes) return entry.tok.type == _DAT_TOKEN_PIC ? entry.tok.par : -EFAULT;
Instead of returning 0 at the !crste.s.fc1.pr check in dat_get_storage_key() or dat_reset_reference_bit(), won't this fail with -EFAULT (or return the upper address bits as a bogus program interruption code if entry.tok.type happens to equal _DAT_TOKEN_PIC)?
Should _crste_hole() and _crste_none() exclude crste_is_ucas(crste) (or check !crste.h.fc0.tf)?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Claudio Imbrenda
maintainerPATCH v3 2/4 KVM: s390: Move some skey functions to headers
In reply to Claudio ImbrendaMove some storage key handling functions to headers, since they will be needed from outside, and they are small, so inlining is acceptable.
--- arch/s390/kvm/gmap/dat.c | 26 -------------------------- arch/s390/kvm/gmap/dat.h | 26 ++++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 26 deletions(-)
arch/s390/kvm/gmap/dat.c +0 -26
@@ -159,32 +159,6 @@ bool __must_check dat_crstep_xchg_atomic(union crste *crstep, union crste old, ureturn cspg_crste (crstep, old, new); } -static void dat_set_storage_key_from_pgste (union pte pte,union pgste pgste) -{ -union skey nkey = { .acc = pgste.acc, .fp = pgste.fp }; - -page_set_storage_key (pte_origin (pte), nkey.skey,0 ); -} - -static void dat_move_storage_key (union pte old,union pte new) -{ -page_set_storage_key (pte_origin (new),page_get_storage_key (pte_origin (old)),1 ); -} - -static union pgste dat_save_storage_key_into_pgste (union pte pte,union pgste pgste) -{ -union skey skey; - - skey.skey =page_get_storage_key (pte_origin (pte)); - - pgste.acc = skey.acc; - pgste.fp = skey.fp; - pgste.gr |= skey.r; - pgste.gc |= skey.c; - -return pgste; -} -union pgste __dat_ptep_xchg (union pte *ptep,union pgste pgste,union pte new,gfn_t gfn,union asce asce,bool uses_skeys) {
arch/s390/kvm/gmap/dat.h +26 -0
@@ -1002,4 +1002,30 @@ static inline bool crste_is_ucas(union crste crste) crste.h.fc0.tl == _UCAS_ENTRY_LENGTH && crste.h.fc0.tf == _UCAS_ENTRY_OFFSET; } +static inline void dat_set_storage_key_from_pgste (union pte pte,union pgste pgste) +{ +union skey nkey = { .acc = pgste.acc, .fp = pgste.fp }; + +page_set_storage_key (pte_origin (pte), nkey.skey,0 ); +} + +static inline void dat_move_storage_key (union pte old,union pte new) +{ +page_set_storage_key (pte_origin (new),page_get_storage_key (pte_origin (old)),1 ); +} + +static inline union pgste dat_save_storage_key_into_pgste (union pte pte,union pgste pgste) +{ +union skey skey; + + skey.skey =page_get_storage_key (pte_origin (pte)); + + pgste.acc = skey.acc; + pgste.fp = skey.fp; + pgste.gr |= skey.r; + pgste.gc |= skey.c; + +return pgste; +} +#endif /* ARCH_KVM_GMAP_DAT_H */
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH v3 2/4] KVM: s390: Move some skey functions to headers
Sashiko has reviewed this patch and found no issues.
Quoted 4 lines
Move some storage key handling functions to headers, since they will be needed from outside, and they are small, so inlining is acceptable.
Signed-off-by Claudio Imbrenda <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Claudio Imbrenda
maintainerPATCH v3 3/4 KVM: s390: Improve unmapping of large ranges
In reply to Claudio ImbrendaWhen unmapping guest ranges, each individual leaf DAT entry is invalidated. When unmapping large ranges, this results in a very large amount of invalidations, which is time consuming. Moreover, since the unmap callback is called by KVM while holding a spinlock, this also leads to stall warnings.
Improve the situation by invalidating DAT entries higher in the tree, instead of each leaf individually.
For small invalidations (up to 1 segment = 1M of guest memory), and special cases (like protected VMs) regardless of the size, the old and slow invalidation is performed; all leaf entries are invalidated one by one.
For large invalidations (larger than 1M of guest memory), the new strategy is used:
- Invalidate prefixes on the whole range, extending start and end to 1M granularity
- Iterate through the page table pointed to by each pmd and unshadow entries as needed
- Detatch (invalidate) the pmd
- Save storage keys as needed
- Invalidate each (now detached) pte by simply overwriting it
- Reattach the old pmd to point to the updated page table
--- arch/s390/kvm/gmap/gmap.c | 124 ++++++++++++++++++++++++++++++++++---- 1 file changed, 113 insertions(+), 11 deletions(-)
arch/s390/kvm/gmap/gmap.c +113 -11
@@ -434,17 +434,8 @@ static long _gmap_unmap_crste(union crste *crstep, gfn_t gfn, gfn_t next, structreturn 0 ; } -/** -* gmap_unmap_gfn_range() - Unmap a range of guest addresses. -* @gmap: The gmap to act on. -* @slot: The memslot in which the range is located. -* @start: The first gfn to unmap. -* @end: The gfn after the last one to unmap. -* -* Context: Called with the kvm mmu write lock held. -* Return: false -*/ -bool gmap_unmap_gfn_range (struct gmap *gmap,struct kvm_memory_slot *slot,gfn_t start,gfn_t end) +static bool gmap_unmap_gfn_range_slow (struct gmap *gmap,struct kvm_memory_slot *slot, +gfn_t start,gfn_t end) {const struct dat_walk_ops ops = { .pte_entry = _gmap_unmap_pte,
@@ -462,6 +453,117 @@ bool gmap_unmap_gfn_range(struct gmap *gmap, struct kvm_memory_slot *slot, gfn_treturn false; } +static long _gmap_unmap_pud_fast (union crste *crstep,gfn_t gfn,gfn_t next,struct dat_walk *w) +{ +union crste old = *crstep; + +/* We only care about large puds */ +if (!old.h.fc) +return 0 ; +if (!gfn) +gmap_unmap_prefix (w->priv,0 , next); +if (old.s.fc1.vsie_notif) +gmap_handle_vsie_unshadow_event (w->priv, gfn); + +if (machine_has_tlb_guest ()) +idte_crste (crstep, gfn,IPTE_GUEST_ASCE , w->asce,0 ); +else +idte_crste (crstep, gfn,0 , w->asce,0 ); + *crstep =_CRSTE_EMPTY (old.h.tt); + +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; +struct page_table *pt; +gfn_t first, last; +unsigned int i; + +if (old.h.fc) { +if (old.s.fc1.vsie_notif) +gmap_handle_vsie_unshadow_event (w->priv, gfn); + +if (machine_has_tlb_guest ()) +idte_crste (crstep, gfn,IPTE_GUEST_ASCE , w->asce,0 ); +else +idte_crste (crstep, gfn,0 , w->asce,0 ); +WRITE_ONCE (*crstep,_CRSTE_EMPTY (old.h.tt)); + +return 0 ; + } + +if (old.h.i) +return 0 ; + + pt =dereference_pmd (old.pmd); + first =max (gfn, w->start) & (PTRS_PER_PTE -1 ); + last = (min (next, w->end) -1 ) & (PTRS_PER_PTE -1 ); + +for (i = first; i <= last; i++) +if (pte_needs_unshadow (pt->ptes[i], _PTE_EMPTY, pt->pgstes[i])) +gmap_handle_vsie_unshadow_event (w->priv, gfn + i); + +/* Detach the page table */ +if (machine_has_tlb_guest ()) +idte_crste (crstep, gfn,IPTE_GUEST_ASCE , w->asce,0 ); +else +idte_crste (crstep, gfn,0 , w->asce,0 ); + +for (i = first; i <= last; i++) { +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; + pt->pgstes[i].vsie_notif =0 ; + pt->pgstes[i].vsie_gmem =0 ; + pt->pgstes[i].zero =0 ; + } + +barrier (); +/* Reattach the modified page table */ +WRITE_ONCE (*crstep, old); + +return 0 ; +} + +static bool gmap_unmap_gfn_range_fast (struct gmap *gmap,gfn_t start,gfn_t end) +{ +const struct dat_walk_ops ops = { + .pud_entry = _gmap_unmap_pud_fast, + .pmd_entry = _gmap_unmap_pmd_fast, + }; +bool failed; + +lockdep_assert_held_write (&gmap->kvm->mmu_lock); + +gmap_unmap_prefix (gmap,ALIGN_DOWN (start, _PAGE_ENTRIES),ALIGN (end, _PAGE_ENTRIES)); + + failed =_dat_walk_gfn_range (start, end, gmap->asce, &ops,DAT_WALK_IGN_HOLES , gmap); +KVM_BUG_ON (failed, gmap->kvm); +return false; +} + +/** +* gmap_unmap_gfn_range() - Unmap a range of guest addresses. +* @gmap: The gmap to act on. +* @slot: The memslot in which the range is located. +* @start: The first gfn to unmap. +* @end: The gfn after the last one to unmap. +* +* Context: Called with the kvm mmu write lock held. +* Return: false +*/ +bool gmap_unmap_gfn_range (struct gmap *gmap,struct kvm_memory_slot *slot,gfn_t start,gfn_t end) +{ +if ((end - start <= _PAGES_PER_SEGMENT) || +test_bit (GMAP_FLAG_EXPORT_ON_UNMAP , &gmap->flags) ||is_ucontrol (gmap)) +return gmap_unmap_gfn_range_slow (gmap, slot, start, end); +return gmap_unmap_gfn_range_fast (gmap, start, end); +} +static union pgste __pte_test_and_clear_softdirty (union pte *ptep,union pgste pgste,gfn_t gfn,struct gmap *gmap) {
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH v3 3/4] KVM: s390: Improve unmapping of large ranges
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Critical] KVM: s390: wrong IDTE option flag IPTE_GUEST_ASCE passed to idte_crste() - [Critical] KVM…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Critical] KVM: s390: wrong IDTE option flag IPTE_GUEST_ASCE passed to idte_crste()
- [Critical] KVM: s390: missing table-type bits in idte_crste() for PUD invalidation
- [Medium] KVM: s390: _gmap_unmap_pmd_fast() fails to clear pgste prefix_notif bit --
commit e343f26ffd40139c7f207f60f7208912b2666ca8
Author: Claudio Imbrenda <[email protected]>
KVM: s390: Improve unmapping of large ranges
This commit speeds up gmap_unmap_gfn_range() for guest address ranges larger than one segment (1M) on standard VMs by invalidating higher-level DAT entries with idte_crste() and updating detached page tables in bulk rather than invalidating each leaf PTE individually.
Quoted 4 lines
arch/s390/kvm/gmap/gmap.c +0
-0
[ ... ]
Quoted 11 lines
arch/s390/kvm/gmap/gmap.c +7
-0@@ -462,6 +453,117 @@ bool gmap_unmap_gfn_range(struct gmap *gmap, struct kvm_memory_slot *slot, gfn_treturn false; } +static long _gmap_unmap_pud_fast (union crste *crstep,gfn_t gfn,gfn_t next,struct dat_walk *w) +{ +union crste old = *crstep; + +/* We only care about large puds */ +if (!old.h.fc) +return 0 ;
arch/s390/kvm/gmap/gmap.c bool gmap_unmap_gfn_range(struct gmap *gmap, struct kvm_memory_slot *slot, gfn_t · line 472
Quoted 10 lines
arch/s390/kvm/gmap/gmap.c +10
-0+if (!gfn) +gmap_unmap_prefix (w->priv,0 , next); +if (old.s.fc1.vsie_notif) +gmap_handle_vsie_unshadow_event (w->priv, gfn); + +if (machine_has_tlb_guest ()) +idte_crste (crstep, gfn,IPTE_GUEST_ASCE , w->asce,0 ); +else +idte_crste (crstep, gfn,0 , w->asce,0 ); + *crstep =_CRSTE_EMPTY (old.h.tt);
[Severity: Critical]
Does idte_crste() set the required table-type bits in the R1 operand when invalidating a Region-3 entry here in _gmap_unmap_pud_fast()?
Looking at arch/s390/kvm/gmap/dat.h:idte_crste():
unsigned long table_origin = __pa(crste_table_start(crstep)); unsigned long gaddr = gfn_to_gpa(gfn) & HPAGE_MASK;
table_origin does not OR in the Designation-Type bits (crstep->h.tt << 2), unlike __pudp_idte() in arch/s390/include/asm/pgtable.h which sets _ASCE_TYPE_REGION3 (0x04), or crdte_crste() in arch/s390/kvm/gmap/dat.h which sets new.h.tt << 2.
Because crste_table_start(crstep) is 16K-aligned, bits 60-61 of table_origin are always 00 (Segment Table). When gmap_unmap_gfn_range_fast() walks a 2G large PUD (old.h.tt == TABLE_TYPE_REGION3), won't IDTE interpret the Region-3 table as a Segment Table and index entry 0 using the Segment Index bits of gaddr instead of the Region-3 Index, causing a translation specification exception or corrupting entry 0 while leaving *crstep and its 2G TLB entries un-invalidated before *crstep is overwritten?
Quoted 30 lines
arch/s390/kvm/gmap/gmap.c +30
-0+ +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; +struct page_table *pt; +gfn_t first, last; +unsigned int i; + +if (old.h.fc) { +if (old.s.fc1.vsie_notif) +gmap_handle_vsie_unshadow_event (w->priv, gfn); + +if (machine_has_tlb_guest ()) +idte_crste (crstep, gfn,IPTE_GUEST_ASCE , w->asce,0 ); +else +idte_crste (crstep, gfn,0 , w->asce,0 ); +WRITE_ONCE (*crstep,_CRSTE_EMPTY (old.h.tt)); + +return 0 ; + } + +if (old.h.i) +return 0 ; + + pt =dereference_pmd (old.pmd); + first =max (gfn, w->start) & (PTRS_PER_PTE -1 ); + last = (min (next, w->end) -1 ) & (PTRS_PER_PTE -1 );
arch/s390/kvm/gmap/gmap.c bool gmap_unmap_gfn_range(struct gmap *gmap, struct kvm_memory_slot *slot, gfn_t · line 512
Quoted 10 lines
arch/s390/kvm/gmap/gmap.c +10
-0+ +for (i = first; i <= last; i++) +if (pte_needs_unshadow (pt->ptes[i], _PTE_EMPTY, pt->pgstes[i])) +gmap_handle_vsie_unshadow_event (w->priv, gfn + i); + +/* Detach the page table */ +if (machine_has_tlb_guest ()) +idte_crste (crstep, gfn,IPTE_GUEST_ASCE , w->asce,0 ); +else +idte_crste (crstep, gfn,0 , w->asce,0 );
[Severity: Critical]
Should the three idte_crste() calls in _gmap_unmap_pud_fast() and _gmap_unmap_pmd_fast() pass IDTE_GUEST_ASCE instead of IPTE_GUEST_ASCE?
In arch/s390/include/asm/pgtable.h, the constants are defined as:
#define IPTE_GUEST_ASCE 0x800 ... #define IDTE_PTOA 0x0800 #define IDTE_NODAT 0x1000 #define IDTE_GUEST_ASCE 0x2000
In idte_crste(), opt is ORed into the R2 operand (gaddr | opt). Passing IPTE_GUEST_ASCE (0x800) sets IDTE_PTOA (bit 52, Clearing-by-ASCE) instead of IDTE_GUEST_ASCE (0x2000, bit 50).
When machine_has_tlb_guest() is true, won't this execute IDTE in Clearing-by-ASCE mode with non-zero R1 and gaddr operands (risking a specification exception), fail to flush guest-ASCE TLB entries, and leave *crstep valid in memory while pt->ptes[i] and pt->pgstes[i] are modified below?
arch/s390/kvm/gmap/gmap.c bool gmap_unmap_gfn_range(struct gmap *gmap, struct kvm_memory_slot *slot, gfn_t · line 523
Quoted 11 lines
arch/s390/kvm/gmap/gmap.c +11
-0+ +for (i = first; i <= last; i++) { +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; + pt->pgstes[i].vsie_notif =0 ; + pt->pgstes[i].vsie_gmem =0 ; + pt->pgstes[i].zero =0 ; + }
[Severity: Medium]
Should this loop in _gmap_unmap_pmd_fast() also clear pt->pgstes[i].prefix_notif?
In the slow path, _gmap_unmap_pte() -> gmap_ptep_xchg() -> _gmap_ptep_xchg() clears pgste.prefix_notif when installing _PTE_EMPTY (newpte.h.i == 1):
if (pgste.prefix_notif && (newpte.h.p || newpte.h.i)) {
pgste.prefix_notif = 0;
gmap_unmap_prefix(gmap, gfn, gfn + 1);
}Although gmap_unmap_gfn_range_fast() already calls gmap_unmap_prefix() on the aligned range, leaving pt->pgstes[i].prefix_notif set on _PTE_EMPTY entries means that when the page is later faulted back in via _gmap_link() -> gmap_ptep_xchg(), a read fault (newpte.h.p == 1) will trigger an unnecessary gmap_unmap_prefix(), while a write fault (newpte.h.p == 0 && newpte.h.i == 0) will keep the stale prefix_notif bit set on the new PTE.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Claudio Imbrenda
maintainerPATCH v3 4/4 KVM: s390: vsie: Fix pinning using the wrong flag
In reply to Claudio ImbrendaWhen pinning guest memory using pin_blocks(), gfn_to_page() was used, which under the hood uses FOLL_GET, which is the wrong type of flag for memory that is accessed directly by hardware.
Fix by using kvm_vcpu_{,un}map(), which uses FOLL_PIN and is the correct thing to do.
While technically a bugfix, the previous behaviour can only lead to guest corruption in extremely convoluted test scenarios that cannot happen in real/production systems.
--- arch/s390/kvm/s390/vsie.c | 137 ++++++++++++++------------------------ 1 file changed, 50 insertions(+), 87 deletions(-)
arch/s390/kvm/s390/vsie.c +50 -87
@@ -49,20 +49,21 @@ struct vsie_page {/* address of the last reported fault to guest2 */ unsigned long fault_addr;/* 0x0228 */ /* calculated guest addresses of satellite control blocks */ -gpa_t sca_gpa;/* 0x0230 */ -gpa_t itdba_gpa;/* 0x0238 */ -gpa_t gvrd_gpa;/* 0x0240 */ -gpa_t riccbd_gpa;/* 0x0248 */ -gpa_t sdnx_gpa;/* 0x0250 */ +struct kvm_host_map sca_map;/* 0x0230 */ +struct kvm_host_map itdba_map;/* 0x0260 */ +struct kvm_host_map gvrd_map;/* 0x0290 */ +struct kvm_host_map riccbd_map;/* 0x02c0 */ +struct kvm_host_map sdnx_map;/* 0x02f0 */ +struct kvm_host_map scb_map;/* 0x0320 */ /* * guest address of the original SCB. Remains set for free vsie * pages, so we can properly look them up in our addr_to_page * radix tree. */ -gpa_t scb_gpa;/* 0x0258 */ +gpa_t scb_gpa;/* 0x0350 */ /* the shadow gmap in use by the vsie_page */ -struct gmap_cache gmap_cache;/* 0x0260 */ -__u8 reserved[0x06f8 -0x0278 ];/* 0x0278 */ +struct gmap_cache gmap_cache;/* 0x0358 */ +__u8 reserved[0x06f8 -0x0370 ];/* 0x0370 */ struct kvm_s390_crypto_cb crycb;/* 0x06f8 */ __u8 fac[8 +S390_ARCH_FAC_LIST_SIZE_BYTE ];/* 0x07f8 */ };
@@ -681,72 +682,45 @@ static int map_prefix(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page, struct }/* -* Pin the guest page given by gpa and set hpa to the pinned host address. +* pin_guest_page() - Pin the given guest page and return its hpa. +* @vcpu: the vCPU +* @gpa: the gpa to pin +* @map: the map representing this pinning. +* * Will always be pinned writable. * -* Returns: - 0 on success -* - -EINVAL if the gpa is not valid guest storage +* Returns: - 0 if the gpa is not valid guest storage, +* - The host hpa of the pinned page in case of success */ -static int pin_guest_page (struct kvm *kvm,gpa_t gpa,hpa_t *hpa) +static hpa_t pin_guest_page (struct kvm_vcpu *vcpu,gpa_t gpa,struct kvm_host_map *map) { -struct page *page; - - page =gfn_to_page (kvm,gpa_to_gfn (gpa)); -if (!page) -return -EINVAL ; - *hpa = (hpa_t )page_to_phys (page) + (gpa & ~PAGE_MASK ); -return 0 ; -} - -/* Unpins a page previously pinned via pin_guest_page, marking it as dirty. */ -static void unpin_guest_page (struct kvm *kvm,gpa_t gpa,hpa_t hpa) -{ -kvm_release_page_dirty (pfn_to_page (phys_to_pfn (hpa))); -/* mark the page always as dirty for migration */ -mark_page_dirty (kvm,gpa_to_gfn (gpa)); +if (kvm_vcpu_map (vcpu,gpa_to_gfn (gpa), map)) +return 0 ; +if (!kvm_vcpu_mapped (map)) +return 0 ; +return (hpa_t )PFN_PHYS (map->pfn) + (gpa & ~PAGE_MASK ); }/* unpin all blocks previously pinned by pin_blocks(), marking them dirty */ static void unpin_blocks (struct kvm_vcpu *vcpu,struct vsie_page *vsie_page) {struct kvm_s390_sie_block *scb_s = &vsie_page->scb_s; -hpa_t hpa; - hpa = (u64 ) scb_s->scaoh <<32 | scb_s->scaol; -if (hpa) { -unpin_guest_page (vcpu->kvm, vsie_page->sca_gpa, hpa); - vsie_page->sca_gpa =0 ; - scb_s->scaol =0 ; - scb_s->scaoh =0 ; - } +kvm_vcpu_unmap (vcpu, &vsie_page->sca_map); + scb_s->scaol =0 ; + scb_s->scaoh =0 ; - hpa = scb_s->itdba; -if (hpa) { -unpin_guest_page (vcpu->kvm, vsie_page->itdba_gpa, hpa); - vsie_page->itdba_gpa =0 ; - scb_s->itdba =0 ; - } +kvm_vcpu_unmap (vcpu, &vsie_page->itdba_map); + scb_s->itdba =0 ; - hpa = scb_s->gvrd; -if (hpa) { -unpin_guest_page (vcpu->kvm, vsie_page->gvrd_gpa, hpa); - vsie_page->gvrd_gpa =0 ; - scb_s->gvrd =0 ; - } +kvm_vcpu_unmap (vcpu, &vsie_page->gvrd_map); + scb_s->gvrd =0 ; - hpa = scb_s->riccbd; -if (hpa) { -unpin_guest_page (vcpu->kvm, vsie_page->riccbd_gpa, hpa); - vsie_page->riccbd_gpa =0 ; - scb_s->riccbd =0 ; - } +kvm_vcpu_unmap (vcpu, &vsie_page->riccbd_map); + scb_s->riccbd =0 ; - hpa = scb_s->sdnxo; -if (hpa) { -unpin_guest_page (vcpu->kvm, vsie_page->sdnx_gpa, hpa); - vsie_page->sdnx_gpa =0 ; - scb_s->sdnxo =0 ; - } +kvm_vcpu_unmap (vcpu, &vsie_page->sdnx_map); + scb_s->sdnxo =0 ; }/*
@@ -783,13 +757,12 @@ static int pin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) ((gpa + offsetof(struct bsca_block, cpu[0]) - 1) & PAGE_MASK)) rc =set_validity_icpt (scb_s,0x003bU );if (!rc) { - rc =pin_guest_page (vcpu->kvm, gpa, &hpa); -if (rc) + hpa =pin_guest_page (vcpu, gpa, &vsie_page->sca_map); +if (!hpa) rc =set_validity_icpt (scb_s,0x0034U ); } -if (rc) +if (rc || !hpa) goto unpin; - vsie_page->sca_gpa = gpa; scb_s->scaoh = (u32)((u64 )hpa >>32 ); scb_s->scaol = (u32 )(u64 )hpa; }
@@ -801,12 +774,11 @@ static int pin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) goto unpin; }/* 256 bytes cannot cross page boundaries */ - rc =pin_guest_page (vcpu->kvm, gpa, &hpa); -if (rc) { + hpa =pin_guest_page (vcpu, gpa, &vsie_page->itdba_map); +if (!hpa) { rc =set_validity_icpt (scb_s,0x0080U ); goto unpin; } - vsie_page->itdba_gpa = gpa; scb_s->itdba = hpa; }
@@ -820,12 +792,11 @@ static int pin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)* 512 bytes vector registers cannot cross page boundaries * if this block gets bigger, we have to shadow it. */ - rc =pin_guest_page (vcpu->kvm, gpa, &hpa); -if (rc) { + hpa =pin_guest_page (vcpu, gpa, &vsie_page->gvrd_map); +if (!hpa) { rc =set_validity_icpt (scb_s,0x1310U ); goto unpin; } - vsie_page->gvrd_gpa = gpa; scb_s->gvrd = hpa; }
@@ -836,13 +807,12 @@ static int pin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) goto unpin; }/* 64 bytes cannot cross page boundaries */ - rc =pin_guest_page (vcpu->kvm, gpa, &hpa); -if (rc) { + hpa =pin_guest_page (vcpu, gpa, &vsie_page->riccbd_map); +if (!hpa) { rc =set_validity_icpt (scb_s,0x0043U ); goto unpin; }/* Validity 0x0044 will be checked by SIE */ - vsie_page->riccbd_gpa = gpa; scb_s->riccbd = hpa; } if (((scb_s->ecb & ECB_GS) && !(scb_s->ecd & ECD_HOSTREGMGMT)) ||
@@ -866,12 +836,11 @@ static int pin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)/* Due to alignment rules (checked above) this cannot * cross page boundaries */ - rc =pin_guest_page (vcpu->kvm, gpa, &hpa); -if (rc) { + hpa =pin_guest_page (vcpu, gpa, &vsie_page->sdnx_map); +if (!hpa) { rc =set_validity_icpt (scb_s,0x10b0U ); goto unpin; } - vsie_page->sdnx_gpa = gpa; scb_s->sdnxo = hpa | sdnxc; }return 0 ;
@@ -881,13 +850,9 @@ static int pin_blocks(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) }/* unpin the scb provided by guest 2, marking it as dirty */ -static void unpin_scb (struct kvm_vcpu *vcpu,struct vsie_page *vsie_page, -gpa_t gpa) +static void unpin_scb (struct kvm_vcpu *vcpu,struct vsie_page *vsie_page) { -hpa_t hpa =virt_to_phys (vsie_page->scb_o); - -if (hpa) -unpin_guest_page (vcpu->kvm, gpa, hpa); +kvm_vcpu_unmap (vcpu, &vsie_page->scb_map); vsie_page->scb_o =NULL ; }
@@ -901,12 +866,10 @@ static int pin_scb(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page, gpa_t gpa) {hpa_t hpa; -int rc; - rc =pin_guest_page (vcpu->kvm, gpa, &hpa); -if (rc) { - rc =kvm_s390_inject_program_int (vcpu,PGM_ADDRESSING ); -WARN_ON_ONCE (rc); + hpa =pin_guest_page (vcpu, gpa, &vsie_page->scb_map); +if (!hpa) { +WARN_ON_ONCE (kvm_s390_inject_program_int (vcpu,PGM_ADDRESSING ));return 1 ; } vsie_page->scb_o =phys_to_virt (hpa);
@@ -1601,7 +1564,7 @@ int kvm_s390_handle_vsie(struct kvm_vcpu *vcpu) out_unshadow:unshadow_scb (vcpu, vsie_page); out_unpin_scb: -unpin_scb (vcpu, vsie_page, scb_addr); +unpin_scb (vcpu, vsie_page); out_put:put_vsie_page (vsie_page);
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH v3 4/4] KVM: s390: vsie: Fix pinning using the wrong flag
Sashiko has reviewed this patch and found no issues.
Quoted 7 lines
When pinning guest memory using
pin_blocks(),gfn_to_page()was used, which under the hood usesFOLL_GET, which is the wrong type of flag for memory that is accessed directly by hardware.Fix by using kvm_vcpu_{,un}map(), which uses
FOLL_PINand is the correct thing to do.
Quoted 6 lines
While technically a bugfix, the previous behaviour can only lead to guest corruption in extremely convoluted test scenarios that cannot happen in real/production systems.
Fixes e38c884df921 ("KVM: s390: Switch to new gmap")Signed-off-by Claudio Imbrenda <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4