Skip to contentSkip to conversation

x86/hvm: finish IOREQ correctly on completion path

PATCH In review Igor Druzhinin 3 participants 5 messages Mar 12, 2019 – Mar 13, 2019

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86/hvm: finish IOREQ correctly on completion path

Since the introduction of linear_{read,write}() helpers in 3bdec530a5 (x86/HVM: split page straddling emulated accesses in more cases) the completion path for IOREQs has been broken: if there is an IOREQ in progress but hvm_copy_{to,from}_guest_linear() returns HVMTRANS_okay (e.g. when P2M type of source/destination has been changed by IOREQ handler) the execution will never re-enter hvmemul_do_io() where IOREQs are completed. This usually results in a domain crash upon the execution of the next IOREQ entering hvmemul_do_io() and finding the remnants of the previous IOREQ in the state machine.

This particular issue has been discovered in relation to p2m_ioreq_server type where an emulator changed the memory type between p2m_ioreq_server and p2m_ram_rw in process of responding to IOREQ which made hvm_copy_..() to behave differently on the way back. But could be also applied to a case where e.g. an emulator balloons memory to/from the guest in response to MMIO read/write, etc.

Fix it by checking if IOREQ completion is required before trying to finish a memory access immediately through hvm_copy_..(), re-enter hvmemul_do_io() otherwise.

---
Changes in v2:
* use test for MMIO cache entry to identify preceding MMIO access
* change the comment message to be more descriptive as suggested

---
 xen/arch/x86/hvm/emulate.c | 35 +++++++++++++++++++++++++++++++----
 1 file changed, 31 insertions(+), 4 deletions(-)
xen/arch/x86/hvm/emulate.c +31 -4
index 754baf6..365fdc9 100644
@@ -952,7 +952,7 @@ static int hvmemul_phys_mmio_access(
  * cache indexed by linear MMIO address.
  */
 static struct hvm_mmio_cache *hvmemul_find_mmio_cache(
-    struct hvm_vcpu_io *vio, unsigned long gla, uint8_t dir)
+    struct hvm_vcpu_io *vio, unsigned long gla, uint8_t dir, bool create)
 {
     unsigned int i;
     struct hvm_mmio_cache *cache;
@@ -966,6 +966,9 @@ static struct hvm_mmio_cache *hvmemul_find_mmio_cache(
             return cache;
     }
 
+    if ( !create )
+        return NULL;
+
     i = vio->mmio_cache_count;
     if( i == ARRAY_SIZE(vio->mmio_cache) )
         return NULL;
@@ -1000,7 +1003,7 @@ static int hvmemul_linear_mmio_access(
 {
     struct hvm_vcpu_io *vio = &current->arch.hvm.hvm_io;
     unsigned long offset = gla & ~PAGE_MASK;
-    struct hvm_mmio_cache *cache = hvmemul_find_mmio_cache(vio, gla, dir);
+    struct hvm_mmio_cache *cache = hvmemul_find_mmio_cache(vio, gla, dir, true);
     unsigned int chunk, buffer_offset = 0;
     paddr_t gpa;
     unsigned long one_rep = 1;
@@ -1089,7 +1092,19 @@ static int linear_read(unsigned long addr, unsigned int bytes, void *p_data,
                        uint32_t pfec, struct hvm_emulate_ctxt *hvmemul_ctxt)
 {
     pagefault_info_t pfinfo;
-    int rc = hvm_copy_from_guest_linear(p_data, addr, bytes, pfec, &pfinfo);
+    struct hvm_vcpu_io *vio = &current->arch.hvm.hvm_io;
+    struct hvm_mmio_cache *cache = hvmemul_find_mmio_cache(vio, addr,
+                                                           IOREQ_READ, false);
+    int rc = HVMTRANS_bad_gfn_to_mfn;
+
+    /*
+     * If there is an MMIO cache entry for that access then we must be re-issuing
+     * an access that was previously handed as MMIO. Thus it is imperative that
+     * we handle this access in the same way to guarantee completion and hence
+     * clean up any interim state.
+     */
+    if ( !cache )
+        rc = hvm_copy_from_guest_linear(p_data, addr, bytes, pfec, &pfinfo);
 
     switch ( rc )
     {
@@ -1132,7 +1147,19 @@ static int linear_write(unsigned long addr, unsigned int bytes, void *p_data,
                         uint32_t pfec, struct hvm_emulate_ctxt *hvmemul_ctxt)
 {
     pagefault_info_t pfinfo;
-    int rc = hvm_copy_to_guest_linear(addr, p_data, bytes, pfec, &pfinfo);
+    struct hvm_vcpu_io *vio = &current->arch.hvm.hvm_io;
+    struct hvm_mmio_cache *cache = hvmemul_find_mmio_cache(vio, addr,
+                                                           IOREQ_WRITE, false);
+    int rc = HVMTRANS_bad_gfn_to_mfn;
+
+    /*
+     * If there is an MMIO cache entry for that access then we must be re-issuing
+     * an access that was previously handed as MMIO. Thus it is imperative that
+     * we handle this access in the same way to guarantee completion and hence
+     * clean up any interim state.
+     */
+    if ( !cache )
+        rc = hvm_copy_to_guest_linear(addr, p_data, bytes, pfec, &pfinfo);
 
     switch ( rc )
     {
Signature · 7 lines
2.7.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 12.03.19 at 17:23, <[email protected]> wrote:

Since the introduction of linear_{read,write}() helpers in 3bdec530a5 (x86/HVM: split page straddling emulated accesses in more cases) the completion path for IOREQs has been broken: if there is an IOREQ in progress but hvm_copy_{to,from}_guest_linear() returns HVMTRANS_okay (e.g. when P2M type of source/destination has been changed by IOREQ handler) the execution will never re-enter hvmemul_do_io() where IOREQs are completed. This usually results in a domain crash upon the execution of the next IOREQ entering hvmemul_do_io() and finding the remnants of the previous IOREQ in the state machine.

Quoted 6 lines

This particular issue has been discovered in relation to p2m_ioreq_server type where an emulator changed the memory type between p2m_ioreq_server and p2m_ram_rw in process of responding to IOREQ which made hvm_copy_..() to behave differently on the way back. But could be also applied to a case where e.g. an emulator balloons memory to/from the guest in response to MMIO read/write, etc.

An emulator ballooning memory? I think Andrew was hinting towards another vCPU of the guest doing some ballooning work in parallel to the insn emulation.

xen/arch/x86/hvm/emulate.c static int linear_read(unsigned long addr, unsigned int bytes, void *p_data, · line 1097

Quoted 8 lines
xen/arch/x86/hvm/emulate.c +3 -1
@@ -1089,7 +1092,19 @@ static int linear_read(unsigned long addr, unsigned int bytes, void *p_data,
                        uint32_t pfec, struct hvm_emulate_ctxt *hvmemul_ctxt)
 {
     pagefault_info_t pfinfo;
-    int rc = hvm_copy_from_guest_linear(p_data, addr, bytes, pfec, &pfinfo);
+    struct hvm_vcpu_io *vio = &current->arch.hvm.hvm_io;
+    struct hvm_mmio_cache *cache = hvmemul_find_mmio_cache(vio, addr,
+                                                           IOREQ_READ, false);

const (and I wonder whether the local variable it worth it in the first place)

xen/arch/x86/hvm/emulate.c static int linear_read(unsigned long addr, unsigned int bytes, void *p_data, · line 1102

Quoted 5 lines
xen/arch/x86/hvm/emulate.c +5 -0
+    int rc = HVMTRANS_bad_gfn_to_mfn;
+
+    /*
+     * If there is an MMIO cache entry for that access then we must be re-issuing
+     * an access that was previously handed as MMIO. Thus it is imperative that

... handled ... ?

xen/arch/x86/hvm/emulate.c static int linear_read(unsigned long addr, unsigned int bytes, void *p_data, · line 1107

Quoted 5 lines
xen/arch/x86/hvm/emulate.c +5 -0
+     * we handle this access in the same way to guarantee completion and hence
+     * clean up any interim state.
+     */
+    if ( !cache )
+        rc = hvm_copy_from_guest_linear(p_data, addr, bytes, pfec, &pfinfo);

This is certainly better than the previous version, but imo still doesn't address the general case. But I'd be willing to accept it as a temporary workaround for the specific case of a write to an (aligned) page table entry leading to a page type change from p2m_ioreq_server to p2m_ram_rw, as it at least doesn't seem to break the case anymore that the introduction of linear_{read,write}() was meant to address.

The more general case that still won't work (afaict) is an access crossing a page boundary, where the second page's type changes behind our backs. The first part of the access won't find a cache entry here, and hence would still go the hvm_copy_{from,to}_guest_linear() path above.

And then there's the more general issue with this caching using (only) guest (linear) addresses as tags: While thinking through the implications here, it became pretty obvious that hvmemul_phys_mmio_access() doing

            if ( dir == IOREQ_READ )
                memcpy(&buffer[offset], &cache->buffer[offset], chunk);
            else if ( memcmp(&buffer[offset], &cache->buffer[offset], chunk) != 0 )
                domain_crash(current->domain);

will wrongly crash the domain if a single insn writes the same linear address more than once with different data. Such a case is pretty simple to construct with AVX512's scatter insns, but even without I'm not sure bad scenarios couldn't be found. Obviously in such a case your new use of the cached info is liable to go wrong, too, albeit possibly in a benign way.

Hence this new use of caching that needs to go away (read:
needs to be replaced) is at best sub-optimal, and may further complicate that future work.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
Quoted 18 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 12.03.19 at 17:23, <[email protected]> wrote:

Since the introduction of linear_{read,write}() helpers in 3bdec530a5 (x86/HVM: split page straddling emulated accesses in more cases) the completion path for IOREQs has been broken: if there is an IOREQ in progress but hvm_copy_{to,from}_guest_linear() returns HVMTRANS_okay (e.g. when P2M type of source/destination has been changed by IOREQ handler) the execution will never re-enter hvmemul_do_io() where IOREQs are completed. This usually results in a domain crash upon the execution of the next IOREQ entering hvmemul_do_io() and finding the remnants of the previous IOREQ in the state machine.

Quoted 6 lines

This particular issue has been discovered in relation to p2m_ioreq_server type where an emulator changed the memory type between p2m_ioreq_server and p2m_ram_rw in process of responding to IOREQ which made hvm_copy_..() to behave differently on the way back. But could be also applied to a case where e.g. an emulator balloons memory to/from the guest in response to MMIO read/write, etc.

Quoted 3 lines

An emulator ballooning memory? I think Andrew was hinting towards another vCPU of the guest doing some ballooning work in parallel to the insn emulation.

Yes, this is another example of what emulator might be doing with P2M type in the process of IOREQ handling. Has nothing to do with what Andrew mentioned.

Quoted 26 lines

xen/arch/x86/hvm/emulate.c static int linear_read(unsigned long addr, unsigned int bytes, void *p_data, · line 1097

Quoted 8 lines
xen/arch/x86/hvm/emulate.c +3 -1
@@ -1089,7 +1092,19 @@ static int linear_read(unsigned long addr, unsigned int bytes, void *p_data,
                        uint32_t pfec, struct hvm_emulate_ctxt *hvmemul_ctxt)
 {
     pagefault_info_t pfinfo;
-    int rc = hvm_copy_from_guest_linear(p_data, addr, bytes, pfec, &pfinfo);
+    struct hvm_vcpu_io *vio = &current->arch.hvm.hvm_io;
+    struct hvm_mmio_cache *cache = hvmemul_find_mmio_cache(vio, addr,
+                                                           IOREQ_READ, false);

const (and I wonder whether the local variable it worth it in the first place)

xen/arch/x86/hvm/emulate.c static int linear_read(unsigned long addr, unsigned int bytes, void *p_data, · line 1102

Quoted 5 lines
xen/arch/x86/hvm/emulate.c +5 -0
+    int rc = HVMTRANS_bad_gfn_to_mfn;
+
+    /*
+     * If there is an MMIO cache entry for that access then we must be re-issuing
+     * an access that was previously handed as MMIO. Thus it is imperative that

... handled ... ?

xen/arch/x86/hvm/emulate.c static int linear_read(unsigned long addr, unsigned int bytes, void *p_data, · line 1107

Quoted 5 lines
xen/arch/x86/hvm/emulate.c +5 -0
+     * we handle this access in the same way to guarantee completion and hence
+     * clean up any interim state.
+     */
+    if ( !cache )
+        rc = hvm_copy_from_guest_linear(p_data, addr, bytes, pfec, &pfinfo);
Quoted 7 lines

This is certainly better than the previous version, but imo still doesn't address the general case. But I'd be willing to accept it as a temporary workaround for the specific case of a write to an (aligned) page table entry leading to a page type change from p2m_ioreq_server to p2m_ram_rw, as it at least doesn't seem to break the case anymore that the introduction of linear_{read,write}() was meant to address.

I don't think it's feasible (and makes sense) to specify P2M type on that (linear) level. We only have access to P2M type on physical layer which is several calls down the stack. And I cannot see now how we can query what the type actually *was* before without introducing some additional stashing array of p2mt somewhere (mmio_cache?).

I agree this is more useful as a temporary workaround - I'll put it into the commit description of the next version.

Quoted 5 lines

The more general case that still won't work (afaict) is an access crossing a page boundary, where the second page's type changes behind our backs. The first part of the access won't find a cache entry here, and hence would still go the hvm_copy_{from,to}_guest_linear() path above.

This could be solved by splitting *all* (not only MMIO as it's done now) linear accesses on page boundary and checking each page separately. Would you consider this for v3? This shouldn't introduce any changes in behavior afaics and is actually more correct imo.

Quoted 17 lines

And then there's the more general issue with this caching using (only) guest (linear) addresses as tags: While thinking through the implications here, it became pretty obvious that hvmemul_phys_mmio_access() doing

            if ( dir == IOREQ_READ )
                memcpy(&buffer[offset], &cache->buffer[offset], chunk);
            else if ( memcmp(&buffer[offset], &cache->buffer[offset], chunk) != 0 )
                domain_crash(current->domain);

will wrongly crash the domain if a single insn writes the same linear address more than once with different data. Such a case is pretty simple to construct with AVX512's scatter insns, but even without I'm not sure bad scenarios couldn't be found. Obviously in such a case your new use of the cached info is liable to go wrong, too, albeit possibly in a benign way.

Quoted 3 lines

Hence this new use of caching that needs to go away (read:
needs to be replaced) is at best sub-optimal, and may further complicate that future work.

As I agreed I'm not thinking this is an ultimate solution but seems good enough for stable-4.12 as we need something to unblock XenGT platform update for Intel.

Igor

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw
Quoted 20 lines
Quoted 1 line
Quoted 1 line · On 13.03.19 at 13:47, <[email protected]> wrote:
Quoted 18 lines · Jan Beulich
Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 12.03.19 at 17:23, <[email protected]> wrote:

Since the introduction of linear_{read,write}() helpers in 3bdec530a5 (x86/HVM: split page straddling emulated accesses in more cases) the completion path for IOREQs has been broken: if there is an IOREQ in progress but hvm_copy_{to,from}_guest_linear() returns HVMTRANS_okay (e.g. when P2M type of source/destination has been changed by IOREQ handler) the execution will never re-enter hvmemul_do_io() where IOREQs are completed. This usually results in a domain crash upon the execution of the next IOREQ entering hvmemul_do_io() and finding the remnants of the previous IOREQ in the state machine.

This particular issue has been discovered in relation to p2m_ioreq_server type where an emulator changed the memory type between p2m_ioreq_server and p2m_ram_rw in process of responding to IOREQ which made hvm_copy_..() to behave differently on the way back. But could be also applied to a case where e.g. an emulator balloons memory to/from the guest in response to MMIO read/write, etc.

Quoted 7 lines
Quoted 3 lines

An emulator ballooning memory? I think Andrew was hinting towards another vCPU of the guest doing some ballooning work in parallel to the insn emulation.

Yes, this is another example of what emulator might be doing with P2M type in the process of IOREQ handling. Has nothing to do with what Andrew mentioned.

I'm struggling to see how an emulator could do so without upsetting the guest.

Quoted 11 lines
Quoted 10 lines

xen/arch/x86/hvm/emulate.c static int linear_read(unsigned long addr, unsigned int bytes, void *p_data, · line 1107

Quoted 2 lines
xen/arch/x86/hvm/emulate.c +2 -0
+    if ( !cache )
+        rc = hvm_copy_from_guest_linear(p_data, addr, bytes, pfec, &pfinfo);

This is certainly better than the previous version, but imo still doesn't address the general case. But I'd be willing to accept it as a temporary workaround for the specific case of a write to an (aligned) page table entry leading to a page type change from p2m_ioreq_server to p2m_ram_rw, as it at least doesn't seem to break the case anymore that the introduction of linear_{read,write}() was meant to address.

Quoted 5 lines

I don't think it's feasible (and makes sense) to specify P2M type on that (linear) level. We only have access to P2M type on physical layer which is several calls down the stack. And I cannot see now how we can query what the type actually *was* before without introducing some additional stashing array of p2mt somewhere (mmio_cache?).

Hmm, some misunderstanding perhaps: I did not mean to suggest to track the p2m type here. I've mentioned types in my reply solely to call out the specific case the change will help with, in order to separate it from the possible cases where it won't help.

Quoted 2 lines

I agree this is more useful as a temporary workaround - I'll put it into the commit description of the next version.

Thanks, and together with this please also mention the known case(s) where this is not going to help.

Quoted 10 lines
Quoted 5 lines

The more general case that still won't work (afaict) is an access crossing a page boundary, where the second page's type changes behind our backs. The first part of the access won't find a cache entry here, and hence would still go the hvm_copy_{from,to}_guest_linear() path above.

This could be solved by splitting *all* (not only MMIO as it's done now) linear accesses on page boundary and checking each page separately. Would you consider this for v3? This shouldn't introduce any changes in behavior afaics and is actually more correct imo.

The reason this wasn't done was (iirc) because this is likely to be less efficient. I'd like to defer to Paul as the maintainer of the code to give you further direction one or the other way.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw
Quoted 73 lines

-----Original Message-----
From: Xen-devel [mailto:[email protected]] On Behalf Of Jan Beulich
Sent: 13 March 2019 14:30
To: Igor Druzhinin <[email protected]>; Paul Durrant <[email protected]>
Cc: Andrew Cooper <[email protected]>; Wei Liu <[email protected]>; xen-devel <xen- [email protected]>; Roger Pau Monne <[email protected]>
Subject: Re: [Xen-devel] [PATCH v2] x86/hvm: finish IOREQ correctly on completion path

Quoted 20 lines
Quoted 1 line
Quoted 1 line · On 13.03.19 at 13:47, <[email protected]> wrote:
Quoted 18 lines · Jan Beulich
Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 12.03.19 at 17:23, <[email protected]> wrote:

Since the introduction of linear_{read,write}() helpers in 3bdec530a5 (x86/HVM: split page straddling emulated accesses in more cases) the completion path for IOREQs has been broken: if there is an IOREQ in progress but hvm_copy_{to,from}_guest_linear() returns HVMTRANS_okay (e.g. when P2M type of source/destination has been changed by IOREQ handler) the execution will never re-enter hvmemul_do_io() where IOREQs are completed. This usually results in a domain crash upon the execution of the next IOREQ entering hvmemul_do_io() and finding the remnants of the previous IOREQ in the state machine.

This particular issue has been discovered in relation to p2m_ioreq_server type where an emulator changed the memory type between p2m_ioreq_server and p2m_ram_rw in process of responding to IOREQ which made hvm_copy_..() to behave differently on the way back. But could be also applied to a case where e.g. an emulator balloons memory to/from the guest in response to MMIO read/write, etc.

Quoted 7 lines
Quoted 3 lines

An emulator ballooning memory? I think Andrew was hinting towards another vCPU of the guest doing some ballooning work in parallel to the insn emulation.

Yes, this is another example of what emulator might be doing with P2M type in the process of IOREQ handling. Has nothing to do with what Andrew mentioned.

I'm struggling to see how an emulator could do so without upsetting the guest.

Quoted 11 lines
Quoted 10 lines

xen/arch/x86/hvm/emulate.c static int linear_read(unsigned long addr, unsigned int bytes, void *p_data, · line 1107

Quoted 2 lines
xen/arch/x86/hvm/emulate.c +2 -0
+    if ( !cache )
+        rc = hvm_copy_from_guest_linear(p_data, addr, bytes, pfec, &pfinfo);

This is certainly better than the previous version, but imo still doesn't address the general case. But I'd be willing to accept it as a temporary workaround for the specific case of a write to an (aligned) page table entry leading to a page type change from p2m_ioreq_server to p2m_ram_rw, as it at least doesn't seem to break the case anymore that the introduction of linear_{read,write}() was meant to address.

Quoted 5 lines

I don't think it's feasible (and makes sense) to specify P2M type on that (linear) level. We only have access to P2M type on physical layer which is several calls down the stack. And I cannot see now how we can query what the type actually *was* before without introducing some additional stashing array of p2mt somewhere (mmio_cache?).

Hmm, some misunderstanding perhaps: I did not mean to suggest to track the p2m type here. I've mentioned types in my reply solely to call out the specific case the change will help with, in order to separate it from the possible cases where it won't help.

Quoted 2 lines

I agree this is more useful as a temporary workaround - I'll put it into the commit description of the next version.

Thanks, and together with this please also mention the known case(s) where this is not going to help.

Quoted 6 lines
Quoted 5 lines

The more general case that still won't work (afaict) is an access crossing a page boundary, where the second page's type changes behind our backs. The first part of the access won't find a cache entry here, and hence would still go the hvm_copy_{from,to}_guest_linear() path above.

Quoted 8 lines
Quoted 4 lines

This could be solved by splitting *all* (not only MMIO as it's done now) linear accesses on page boundary and checking each page separately. Would you consider this for v3? This shouldn't introduce any changes in behavior afaics and is actually more correct imo.

The reason this wasn't done was (iirc) because this is likely to be less efficient. I'd like to defer to Paul as the maintainer of the code to give you further direction one or the other way.

Having talked to Igor, ruling out page straddling at the highest level would seem like the best way forward. I think it removes a lot of confusion.

Paul

Quoted 9 lines

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel