ARM: cache coherence problem in guestcopy.c
Quoted 15 lines
But all of the caches on this platform are PIPT (right?) so isn't it actually:
(consumer) (producer)
xen DomU
\ / (writing path)
\ /
\ /
(reading path) \ /\ / (cache) || || \/
Quoted 6 lines
_______________________________________
| mfn | (physical memory)
---------------------------------------Or are you saying that the writing path is uncached?
Oops my mistake. As far as I know, it is PIPT and the writing also should be cached.
Quoted 7 lines
I was chatting with Tim and he suggested that the issue might be the ReOrder Buffer, which is virtually tagged. In that case a DMB ought to be sufficient and not a full cache flush, we think.
We were also speculating that we probably want some DMBs in context_switch_{from,to} as well as at
return_to_guest.
Actually, I just learned ReOrder Buffer, and it looks like so.
Best,
Jaeyong
Quoted 4 lines · Jaeyong Yoo
Quoted 2 lines
We were also speculating that we probably want some DMBs in context_switch_{from,to} as well as at
return_to_guest.Actually, I just learned ReOrder Buffer, and it looks like so.
Right. Tim and I were speculating about where and why the barriers were needed:
In unmap_domain_page: This ensures any writes to domain memory via the Xen mapping are seen by subsequent accesses via the guest mapping. Likewise map_domain_page needs one to ensure that previous accesses via the guest mapping will be observed (this is the issue you observed).
In context_switch_from: To ensure any writes done on a VCPU are seen before it is rescheduled, potentially on another PCPU. It seems likely that you'd want one in context_switch_to for the complementary reasons,
In return_to_guest: This is similar to the unmap_domain_page one but handles writes to pages which are permanently shared by Xen and the guest, e.g. the shared info page or anything mapped with share_xen_page_with_guest etc. This one is debatable because all users of this are, I think, expected to get their own barriers correct since this these pages are typically part of some lock free datastructure arrangement.
Ian.
Quoted 5 lines · Ian Campbell
Quoted 4 lines · Jaeyong Yoo
Quoted 2 lines
We were also speculating that we probably want some DMBs in context_switch_{from,to} as well as at
return_to_guest.Actually, I just learned ReOrder Buffer, and it looks like so.
Does this patch help with the issue you are seeing?
I think in theory it should *BUT* (and it's a big But)...
... it doesn't work for me on an issue I've been seeing booting dom0 on the Foundation model. However doing the dmb in map_domain_page() *twice* does work for me. In fact doing a dmb+nop works too. This is inexplicable to me. It may be a model bug or it may (more likely) be indicative of some other underlying issue. I also tried dsb() instead of dmb() and that doesn't make a difference.
Anyway, I'm interested about its behaviour on real hardware.
These are doing full system barriers. I think in reality only a "dmb nsh" should be needed for this use case, since we only care about the local processor. I didn't want to go there when the supposedly more obvious case wasn't doing as I expected!
Ian.
xen/arch/arm/mm.c +14 -0
@@ -218,6 +218,13 @@ void *map_domain_page(unsigned long mfn)local_irq_save (flags); +/* +* Ensure that any previous writes which occurred via another +* mapping, specifically the guest's own mapping have been +* completed such that they are visible via this mapping. +*/ +dmb (); +/* The map is laid out as an open-addressed hash table where each * entry is a 2MB superpage pte. We use the available bits of each * PTE as a reference count; when the refcount is zero the slot can
@@ -286,6 +297,13 @@ void unmap_domain_page(const void *va) map[slot].pt.avail--; +/* +* Ensure that any writes which occurred via this mapping have been +* completed such that they are visible via other virtual +* mappings, specifically the guest's own mapping. +*/ +dmb (); +local_irq_restore (flags); }
Quoted 6 lines · Ian Campbell
Quoted 4 lines · Jaeyong Yoo
Quoted 2 lines
We were also speculating that we probably want some DMBs in context_switch_{from,to} as well as at
return_to_guest.Actually, I just learned ReOrder Buffer, and it looks like so.
Quoted 7 lines
Right. Tim and I were speculating about where and why the barriers were needed:
In
unmap_domain_page: This ensures any writes to domain memory via the Xen mapping are seen by subsequent accesses via the guest mapping. Likewisemap_domain_pageneeds one to ensure that previous accesses via the guest mapping will be observed (this is the issue you observed).
What if the guest doesn't have caching enabled?
It seems to me that the only way to make sure that the guest is going to be able to see the changes made by Xen is flushing the dcache. Unless we mandate that all Xen guests are required to have caching enabled.
Quoted 3 lines
In
context_switch_from: To ensure any writes done on a VCPU are seen before it is rescheduled, potentially on another PCPU. It seems likely that you'd want one incontext_switch_tofor the complementary reasons,
Is the cache shared across multiple pcpus? Because if it isn't then we need to flush the dcache in this case.
Quoted 7 lines
In
return_to_guest: This is similar to theunmap_domain_pageone but handles writes to pages which are permanently shared by Xen and the guest, e.g. the shared info page or anything mapped withshare_xen_page_with_guestetc. This one is debatable because all users of this are, I think, expected to get their own barriers correct since this these pages are typically part of some lock free datastructure arrangement.
same as above
Quoted 10 lines · Stefano Stabellini
Quoted 9 lines · Ian Campbell
Quoted 4 lines · Jaeyong Yoo
Quoted 2 lines
We were also speculating that we probably want some DMBs in context_switch_{from,to} as well as at
return_to_guest.Actually, I just learned ReOrder Buffer, and it looks like so.
Right. Tim and I were speculating about where and why the barriers were needed:
Quoted 6 lines
Quoted 4 lines
In
unmap_domain_page: This ensures any writes to domain memory via the Xen mapping are seen by subsequent accesses via the guest mapping. Likewisemap_domain_pageneeds one to ensure that previous accesses via the guest mapping will be observed (this is the issue you observed).What if the guest doesn't have caching enabled?
Erk! I think we can mandate that the guest use caching for memory it shares with the hypervisor (i.e. hypercall args & responses, shared pages, granted pages). Otherwise we end up flushing the dcache before and after every copy.
In any case, this is something that should get documented. :)
Quoted 6 lines
Quoted 3 lines
In
context_switch_from: To ensure any writes done on a VCPU are seen before it is rescheduled, potentially on another PCPU. It seems likely that you'd want one incontext_switch_tofor the complementary reasons,Is the cache shared across multiple pcpus? Because if it isn't then we need to flush the dcache in this case.
AIUI this is what marking accesses 'shareable' is for: to tell the caches to keep them coherent. If not, we're equally in trouble for all Xen's internal data structures.
Tim.
Quoted 16 lines · Tim Deegan
Quoted 15 lines · Stefano Stabellini
Quoted 13 lines · Ian Campbell
Quoted 4 lines · Jaeyong Yoo
Quoted 2 lines
We were also speculating that we probably want some DMBs in context_switch_{from,to} as well as at
return_to_guest.Actually, I just learned ReOrder Buffer, and it looks like so.
Right. Tim and I were speculating about where and why the barriers were needed:
In
unmap_domain_page: This ensures any writes to domain memory via the Xen mapping are seen by subsequent accesses via the guest mapping. Likewisemap_domain_pageneeds one to ensure that previous accesses via the guest mapping will be observed (this is the issue you observed).
Quoted 6 lines
Quoted 1 line
What if the guest doesn't have caching enabled?
Erk! I think we can mandate that the guest use caching for memory it shares with the hypervisor (i.e. hypercall args & responses, shared pages, granted pages). Otherwise we end up flushing the dcache before and after every copy.
Yes. Worse because of the reusing/reference counting scheme used in map_domain_page I'm not sure we can tell when we need to flush the cache vs. discard it (because the guest has written something useful behind the cache).
Quoted 1 line
In any case, this is something that should get documented. :)
Yes.
Quoted 10 lines
Quoted 6 lines
Quoted 3 lines
In
context_switch_from: To ensure any writes done on a VCPU are seen before it is rescheduled, potentially on another PCPU. It seems likely that you'd want one incontext_switch_tofor the complementary reasons,Is the cache shared across multiple pcpus? Because if it isn't then we need to flush the dcache in this case.
AIUI this is what marking accesses 'shareable' is for: to tell the caches to keep them coherent. If not, we're equally in trouble for all Xen's internal data structures.
Right they are either snooped or shared, depending on the level and the implementation. Either way it is fine for us I think.
Ian.