KVM: s390: vsie: fixes and cleanups.
Subsystems (5)
PATCH 4/5 KVM: s390: vsie: Move conditional reschedule
In reply to David HildenbrandLet's move it to the outer loop, in case we ever run again into long loops, trying to map the prefix. While at it, convert it to cond_resched().
--- arch/s390/kvm/vsie.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
arch/s390/kvm/vsie.c +1 -2
@@ -1000,8 +1000,6 @@ static int do_vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)handle_last_fault (vcpu, vsie_page); -if (need_resched ()) -schedule ();if (test_cpu_flag (CIF_MCCK_PENDING ))s390_handle_mcck ();
@@ -1185,6 +1183,7 @@ static int vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page)kvm_s390_vcpu_has_irq (vcpu,0 ) || kvm_s390_vcpu_sie_inhibited(vcpu)) break; +cond_resched (); }if (rc == -EFAULT ) {
Signature · 1 line
2.25.1
cover KVM: s390: vsie: fixes and cleanups.
Some vsie/gmap fixes and two cleanups/improvements.
Patch #1 fixes an issue reported by Janosch. It was never observed so far, because KVM usually doesn't use a region 1 table for it's guest (unless memory would be exceeding something like 16 EB, which isn't even supported by the HW). Older QEMU+KVM or other hypervisors can trigger this.
Patch #2 fixes a code path that probably was never taken and will most probably not be taken very often in the future - unless somebody really messes up the page tables for a guest (or writes a test for it). At some point, a test case for this would be nice.
Patch #3 fixes a rare possible race. Don't think this is stable material.
Gave it some testing with my limited access to somewhat-fast s390x machines. Booted a Linux kernel, supplying all possible number of page table hiearchies.
David Hildenbrand (5):
KVM: s390: vsie: Fix region 1 ASCE sanity shadow address checks
KVM: s390: vsie: Fix delivery of addressing exceptions
KVM: s390: vsie: Fix possible race when shadowing region 3 tables
KVM: s390: vsie: Move conditional reschedule
KVM: s390: vsie: gmap_table_walk() simplifications
arch/s390/kvm/vsie.c | 4 ++-- arch/s390/mm/gmap.c | 14 ++++++++++---- 2 files changed, 12 insertions(+), 6 deletions(-)
Signature · 1 line
2.25.1
PATCH 1/5 KVM: s390: vsie: Fix region 1 ASCE sanity shadow address checks
In case we have a region 1 ASCE, our shadow/g3 address can have any value. Unfortunately, (-1UL << 64) is undefined and triggers sometimes, rejecting valid shadow addresses when trying to walk our shadow table hierarchy.
The result is that the prefix cannot get mapped and will loop basically forever trying to map it (-EAGAIN loop).
After all, the broken check is only a sanity check, our table shadowing code in kvm_s390_shadow_tables() already checks these conditions, injecting proper translation exceptions. Turn it into a WARN_ON_ONCE().
--- arch/s390/mm/gmap.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
arch/s390/mm/gmap.c +5 -1
@@ -787,14 +787,18 @@ static void gmap_call_notifier(struct gmap *gmap, unsigned long start,static inline unsigned long *gmap_table_walk (struct gmap *gmap,unsigned long gaddr,int level) { +const int asce_type = gmap->asce & _ASCE_TYPE_MASK;unsigned long *table;if ((gmap->asce & _ASCE_TYPE_MASK) +4 < (level *4 ))return NULL ;if (gmap_is_shadow (gmap) && gmap->removed)return NULL ; -if (gaddr & (-1UL << (31 + ((gmap->asce & _ASCE_TYPE_MASK) >>2 )*11 ))) + +if (WARN_ON_ONCE (asce_type != _ASCE_TYPE_REGION1) && + gaddr & (-1UL << (31 + (asce_type >>2 ) *11 )))return NULL ; + table = gmap->table;switch (gmap->asce & _ASCE_TYPE_MASK) {case _ASCE_TYPE_REGION1:
Signature · 1 line
2.25.1
PATCH 2/5 KVM: s390: vsie: Fix delivery of addressing exceptions
In reply to David HildenbrandWhenever we get an -EFAULT, we failed to read in guest 2 physical address space. Such addressing exceptions are reported via a program intercept to the nested hypervisor.
We faked the intercept, we have to return to guest 2. Instead, right now we would be returning -EFAULT from the intercept handler, eventually crashing the VM.
Addressing exceptions can only happen if the g2->g3 page tables reference invalid g2 addresses (say, either a table or the final page is not accessible - so something that basically never happens in sane environments.
Identified by manual code inspection.
--- arch/s390/kvm/vsie.c | 1 + 1 file changed, 1 insertion(+)
arch/s390/kvm/vsie.c +1 -0
@@ -1202,6 +1202,7 @@ static int vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) scb_s->iprcc =PGM_ADDRESSING ; scb_s->pgmilc =4 ; scb_s->gpsw.addr =__rewind_psw (scb_s->gpsw,4 ); + rc =1 ; }return rc; }
Signature · 1 line
2.25.1
PATCH 3/5 KVM: s390: vsie: Fix possible race when shadowing region 3 tables
In reply to David HildenbrandWe have to properly retry again by returning -EINVAL immediately in case somebody else instantiated the table concurrently. We missed to add the goto in this function only. The code now matches the other, similar shadowing functions.
We are overwriting an existing region 2 table entry. All allocated pages are added to the crst_list to be freed later, so they are not lost forever. However, when unshadowing the region 2 table, we wouldn't trigger unshadowing of the original shadowed region 3 table that we replaced. It would get unshadowed when the original region 3 table is modified. As it's not connected to the page table hierarchy anymore, it's not going to get used anymore. However, for a limited time, this page table will stick around, so it's in some sense a temporary memory leak.
Identified by manual code inspection. I don't think this classifies as stable material.
--- arch/s390/mm/gmap.c | 1 + 1 file changed, 1 insertion(+)
arch/s390/mm/gmap.c +1 -0
@@ -1844,6 +1844,7 @@ int gmap_shadow_r3t(struct gmap *sg, unsigned long saddr, unsigned long r3t, goto out_free; }else if (*table & _REGION_ENTRY_ORIGIN) { rc = -EAGAIN ;/* Race with shadow */ + goto out_free; }crst_table_init (s_r3t, _REGION3_ENTRY_EMPTY);/* mark as invalid as long as the parent table is not protected */
Signature · 1 line
2.25.1
PATCH 5/5 KVM: s390: vsie: gmap_table_walk() simplifications
In reply to David HildenbrandLet's use asce_type where applicable. Also, simplify our sanity check for valid table levels and convert it into a WARN_ON_ONCE(). Check if we even have a valid gmap shadow as the very first step.
--- arch/s390/mm/gmap.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
arch/s390/mm/gmap.c +4 -3
@@ -790,17 +790,18 @@ static inline unsigned long *gmap_table_walk(struct gmap *gmap,const int asce_type = gmap->asce & _ASCE_TYPE_MASK;unsigned long *table; -if ((gmap->asce & _ASCE_TYPE_MASK) +4 < (level *4 )) -return NULL ;if (gmap_is_shadow (gmap) && gmap->removed)return NULL ; +if (WARN_ON_ONCE (level > (asce_type >>2 ) +1 )) +return NULL ; +if (WARN_ON_ONCE (asce_type != _ASCE_TYPE_REGION1) && gaddr & (-1UL << (31 + (asce_type >>2 ) *11 )))return NULL ; table = gmap->table; -switch (gmap->asce & _ASCE_TYPE_MASK) { +switch (asce_type) {case _ASCE_TYPE_REGION1: table += (gaddr & _REGION1_INDEX) >> _REGION1_SHIFT;if (level ==4 )
Signature · 1 line
2.25.1
Re: [PATCH v1 1/5] KVM: s390: vsie: Fix region 1 ASCE sanity shadow address checks
In reply to David HildenbrandQuoted 30 lines · David Hildenbrand
In case we have a region 1 ASCE, our shadow/g3 address can have any value. Unfortunately, (-1UL << 64) is undefined and triggers sometimes, rejecting valid shadow addresses when trying to walk our shadow table hierarchy.
The result is that the prefix cannot get mapped and will loop basically forever trying to map it (
-EAGAINloop).After all, the broken check is only a sanity check, our table shadowing code in
kvm_s390_shadow_tables()already checks these conditions, injecting proper translation exceptions. Turn it into aWARN_ON_ONCE().Fixes 4be130a08420 ("s390/mm: add shadow gmap support")Cc <[email protected]> # v4.8+Reported-by Janosch Frank <[email protected]>Signed-off-by David Hildenbrand <[email protected]>--- arch/s390/mm/gmap.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)arch/s390/mm/gmap.c +1
-0@@ -787,14 +787,18 @@ static void gmap_call_notifier(struct gmap *gmap, unsigned long start,static inline unsigned long *gmap_table_walk (struct gmap *gmap,unsigned long gaddr,int level) { +const int asce_type = gmap->asce & _ASCE_TYPE_MASK;unsigned long *table;
arch/s390/mm/gmap.c static void gmap_call_notifier(struct gmap *gmap, unsigned long start, · line 800
Quoted 10 lines
arch/s390/mm/gmap.c +3
-1if ((gmap->asce & _ASCE_TYPE_MASK) +4 < (level *4 ))return NULL ;if (gmap_is_shadow (gmap) && gmap->removed)return NULL ; -if (gaddr & (-1UL << (31 + ((gmap->asce & _ASCE_TYPE_MASK) >>2 )*11 ))) + +if (WARN_ON_ONCE (asce_type != _ASCE_TYPE_REGION1) && + gaddr & (-1UL << (31 + (asce_type >>2 ) *11 )))return NULL ;
This has to be
if (WARN_ON_ONCE(asce_type != _ASCE_TYPE_REGION1 &&
gaddr & (-1UL << (31 + (asce_type >> 2) * 11))))
otherwise we'll get wrong warnings
Signature · 3 lines
Thanks, David / dhildenb
Janosch Frank
maintainerRe: [PATCH v1 5/5] KVM: s390: vsie: gmap_table_walk() simplifications
In reply to David HildenbrandQuoted 20 lines · David Hildenbrand
Let's use
asce_typewhere applicable. Also, simplify our sanity check for valid table levels and convert it into aWARN_ON_ONCE(). Check if we even have a valid gmap shadow as the very first step.Signed-off-by David Hildenbrand <[email protected]>--- arch/s390/mm/gmap.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)arch/s390/mm/gmap.c +0
-2@@ -790,17 +790,18 @@ static inline unsigned long *gmap_table_walk(struct gmap *gmap,const int asce_type = gmap->asce & _ASCE_TYPE_MASK;unsigned long *table; -if ((gmap->asce & _ASCE_TYPE_MASK) +4 < (level *4 )) -return NULL ;if (gmap_is_shadow (gmap) && gmap->removed)
arch/s390/mm/gmap.c static inline unsigned long *gmap_table_walk(struct gmap *gmap, · line 803
Quoted 10 lines
arch/s390/mm/gmap.c +3
-0return NULL ; +if (WARN_ON_ONCE (level > (asce_type >>2 ) +1 )) +return NULL ; +if (WARN_ON_ONCE (asce_type != _ASCE_TYPE_REGION1) && gaddr & (-1UL << (31 + (asce_type >>2 ) *11 )))return NULL ; table = gmap->table;
We could also initialize this variable at the top, no?
Quoted 6 lines
arch/s390/mm/gmap.c +1
-1-switch (gmap->asce & _ASCE_TYPE_MASK) { +switch (asce_type) {case _ASCE_TYPE_REGION1: table += (gaddr & _REGION1_INDEX) >> _REGION1_SHIFT;if (level ==4 )
Re: [PATCH v1 5/5] KVM: s390: vsie: gmap_table_walk() simplifications
Quoted 30 lines · Janosch Frank
Quoted 29 lines · David Hildenbrand
Let's use
asce_typewhere applicable. Also, simplify our sanity check for valid table levels and convert it into aWARN_ON_ONCE(). Check if we even have a valid gmap shadow as the very first step.Signed-off-by David Hildenbrand <[email protected]>--- arch/s390/mm/gmap.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)arch/s390/mm/gmap.c +3
-2@@ -790,17 +790,18 @@ static inline unsigned long *gmap_table_walk(struct gmap *gmap,const int asce_type = gmap->asce & _ASCE_TYPE_MASK;unsigned long *table; -if ((gmap->asce & _ASCE_TYPE_MASK) +4 < (level *4 )) -return NULL ;if (gmap_is_shadow (gmap) && gmap->removed)return NULL ; +if (WARN_ON_ONCE (level > (asce_type >>2 ) +1 )) +return NULL ; +if (WARN_ON_ONCE (asce_type != _ASCE_TYPE_REGION1) && gaddr & (-1UL << (31 + (asce_type >>2 ) *11 )))return NULL ;
Quoted 3 lines
arch/s390/mm/gmap.c static inline unsigned long *gmap_table_walk(struct gmap *gmap, · line 803
Quoted 1 line
arch/s390/mm/gmap.c +0
-0table = gmap->table;We could also initialize this variable at the top, no?
very right, add that.
Signature · 3 lines
Thanks, David / dhildenb
Re: [PATCH v1 1/5] KVM: s390: vsie: Fix region 1 ASCE sanity shadow address checks
In reply to David HildenbrandQuoted 8 lines · David Hildenbrand
In case we have a region 1 ASCE, our shadow/g3 address can have any value. Unfortunately, (-1UL << 64) is undefined and triggers sometimes, rejecting valid shadow addresses when trying to walk our shadow table hierarchy.
The result is that the prefix cannot get mapped and will loop basically forever trying to map it (
-EAGAINloop).
Quoted 8 lines
After all, the broken check is only a sanity check, our table shadowing code in
kvm_s390_shadow_tables()already checks these conditions, injecting proper translation exceptions. Turn it into aWARN_ON_ONCE().Fixes 4be130a08420 ("s390/mm: add shadow gmap support")Cc <[email protected]> # v4.8+Reported-by Janosch Frank <[email protected]>Signed-off-by David Hildenbrand <[email protected]>
With the WARN_ON_ONCE fix applied I don't run into stalls or warnings anymore, so:
Quoted 29 lines
--- arch/s390/mm/gmap.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)arch/s390/mm/gmap.c +5
-1@@ -787,14 +787,18 @@ static void gmap_call_notifier(struct gmap *gmap, unsigned long start,static inline unsigned long *gmap_table_walk (struct gmap *gmap,unsigned long gaddr,int level) { +const int asce_type = gmap->asce & _ASCE_TYPE_MASK;unsigned long *table;if ((gmap->asce & _ASCE_TYPE_MASK) +4 < (level *4 ))return NULL ;if (gmap_is_shadow (gmap) && gmap->removed)return NULL ; -if (gaddr & (-1UL << (31 + ((gmap->asce & _ASCE_TYPE_MASK) >>2 )*11 ))) + +if (WARN_ON_ONCE (asce_type != _ASCE_TYPE_REGION1) && + gaddr & (-1UL << (31 + (asce_type >>2 ) *11 )))return NULL ; + table = gmap->table;switch (gmap->asce & _ASCE_TYPE_MASK) {case _ASCE_TYPE_REGION1:
Re: [PATCH v1 2/5] KVM: s390: vsie: Fix delivery of addressing exceptions
In reply to David HildenbrandQuoted 27 lines · David Hildenbrand
Whenever we get an
-EFAULT, we failed to read in guest 2 physical address space. Such addressing exceptions are reported via a program intercept to the nested hypervisor.We faked the intercept, we have to return to guest 2. Instead, right now we would be returning
-EFAULTfrom the intercept handler, eventually crashing the VM.Addressing exceptions can only happen if the
g2->g3page tables reference invalid g2 addresses (say, either a table or the final page is not accessible - so something that basically never happens in sane environments.Identified by manual code inspection.
Fixes a3508fbe9dc6 ("KVM: s390: vsie: initial support for nested virtualization")Cc <[email protected]> # v4.8+Signed-off-by David Hildenbrand <[email protected]>--- arch/s390/kvm/vsie.c | 1 + 1 file changed, 1 insertion(+)arch/s390/kvm/vsie.c +0
-0
arch/s390/kvm/vsie.c static int vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) · line 1205
Quoted 4 lines
arch/s390/kvm/vsie.c +1
-0@@ -1202,6 +1202,7 @@ static int vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) scb_s->iprcc =PGM_ADDRESSING ; scb_s->pgmilc =4 ; scb_s->gpsw.addr =__rewind_psw (scb_s->gpsw,4 ); + rc =1 ;
kvm_s390_handle_vsie has
return rc < 0 ? rc : 0;
so rc = 0 would result in the same behaviour, correct? Since we DO handle everything as we should, why rc = 1 ?
Re: [PATCH v1 2/5] KVM: s390: vsie: Fix delivery of addressing exceptions
Quoted 38 lines · Christian Borntraeger
Quoted 27 lines · David Hildenbrand
Whenever we get an
-EFAULT, we failed to read in guest 2 physical address space. Such addressing exceptions are reported via a program intercept to the nested hypervisor.We faked the intercept, we have to return to guest 2. Instead, right now we would be returning
-EFAULTfrom the intercept handler, eventually crashing the VM.Addressing exceptions can only happen if the
g2->g3page tables reference invalid g2 addresses (say, either a table or the final page is not accessible - so something that basically never happens in sane environments.Identified by manual code inspection.
Fixes a3508fbe9dc6 ("KVM: s390: vsie: initial support for nested virtualization")Cc <[email protected]> # v4.8+Signed-off-by David Hildenbrand <[email protected]>--- arch/s390/kvm/vsie.c | 1 + 1 file changed, 1 insertion(+)arch/s390/kvm/vsie.c +0
-0arch/s390/kvm/vsie.c static int vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) · line 1205
Quoted 4 lines
arch/s390/kvm/vsie.c +1
-0@@ -1202,6 +1202,7 @@ static int vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) scb_s->iprcc =PGM_ADDRESSING ; scb_s->pgmilc =4 ; scb_s->gpsw.addr =__rewind_psw (scb_s->gpsw,4 ); + rc =1 ;
kvm_s390_handle_vsiehas
Quoted 4 lines
return rc < 0 ? rc : 0;
so rc = 0 would result in the same behaviour, correct?
yes
Quoted 1 line
Since we DO handle everything as we should, why rc = 1 ?
rc == 1 is the internal representation of "we have to go back into g2". rc == 0, in contrast, means "we can go back into g2 (via a NULL intercept) or continue executing g3". Returning rc == 1 instead of rc == 0 at this point is just consistency.
Signature · 3 lines
Thanks, David / dhildenb
Christian Borntraeger
Reviewed-byRe: [PATCH v1 2/5] KVM: s390: vsie: Fix delivery of addressing exceptions
Quoted 44 lines · David Hildenbrand
Quoted 38 lines · Christian Borntraeger
Quoted 27 lines · David Hildenbrand
Whenever we get an
-EFAULT, we failed to read in guest 2 physical address space. Such addressing exceptions are reported via a program intercept to the nested hypervisor.We faked the intercept, we have to return to guest 2. Instead, right now we would be returning
-EFAULTfrom the intercept handler, eventually crashing the VM.Addressing exceptions can only happen if the
g2->g3page tables reference invalid g2 addresses (say, either a table or the final page is not accessible - so something that basically never happens in sane environments.Identified by manual code inspection.
Fixes a3508fbe9dc6 ("KVM: s390: vsie: initial support for nested virtualization")Cc <[email protected]> # v4.8+Signed-off-by David Hildenbrand <[email protected]>--- arch/s390/kvm/vsie.c | 1 + 1 file changed, 1 insertion(+)arch/s390/kvm/vsie.c +0
-0arch/s390/kvm/vsie.c static int vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) · line 1205
Quoted 4 lines
arch/s390/kvm/vsie.c +1
-0@@ -1202,6 +1202,7 @@ static int vsie_run(struct kvm_vcpu *vcpu, struct vsie_page *vsie_page) scb_s->iprcc =PGM_ADDRESSING ; scb_s->pgmilc =4 ; scb_s->gpsw.addr =__rewind_psw (scb_s->gpsw,4 ); + rc =1 ;
kvm_s390_handle_vsiehasQuoted 4 lines
return rc < 0 ? rc : 0;
so rc = 0 would result in the same behaviour, correct?
Quoted 8 lines
yes
Quoted 1 line
Since we DO handle everything as we should, why rc = 1 ?
rc == 1 is the internal representation of "we have to go back into g2". rc == 0, in contrast, means "we can go back into g2 (via a NULL intercept) or continue executing g3". Returning rc == 1 instead of rc == 0 at this point is just consistency.
Ok, I will add something to the patch description.
Re: [PATCH v1 3/5] KVM: s390: vsie: Fix possible race when shadowing region 3 tables
In reply to David HildenbrandQuoted 28 lines · David Hildenbrand
We have to properly retry again by returning
-EINVALimmediately in case somebody else instantiated the table concurrently. We missed to add the goto in this function only. The code now matches the other, similar shadowing functions.We are overwriting an existing region 2 table entry. All allocated pages are added to the
crst_listto be freed later, so they are not lost forever. However, when unshadowing the region 2 table, we wouldn't trigger unshadowing of the original shadowed region 3 table that we replaced. It would get unshadowed when the original region 3 table is modified. As it's not connected to the page table hierarchy anymore, it's not going to get used anymore. However, for a limited time, this page table will stick around, so it's in some sense a temporary memory leak.Identified by manual code inspection. I don't think this classifies as stable material.
Fixes 998f637cc4b9 ("s390/mm: avoid races on region/segment/page table shadowing")Signed-off-by David Hildenbrand <[email protected]>--- arch/s390/mm/gmap.c | 1 + 1 file changed, 1 insertion(+)arch/s390/mm/gmap.c +0
-0
arch/s390/mm/gmap.c int gmap_shadow_r3t(struct gmap *sg, unsigned long saddr, unsigned long r3t, · line 1850
Quoted 7 lines
arch/s390/mm/gmap.c +1
-0@@ -1844,6 +1844,7 @@ int gmap_shadow_r3t(struct gmap *sg, unsigned long saddr, unsigned long r3t, goto out_free; }else if (*table & _REGION_ENTRY_ORIGIN) { rc = -EAGAIN ;/* Race with shadow */ + goto out_free; }crst_table_init (s_r3t, _REGION3_ENTRY_EMPTY);/* mark as invalid as long as the parent table is not protected */