Skip to contentSkip to conversation

arm/monitor: flush the icache during SMC traps

PATCH In review Tamas K Lengyel 4 participants 9 messages Jan 25, 2017 – Jan 27, 2017 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm/monitor: flush the icache during SMC traps

During an SMC trap it is possible that the user may change the memory from where the SMC was fetched. However, without flushing the icache the SMC may still trigger if the pCPU was idle during the trap. Flush the icache to ensure consistency.

---
Cc: Razvan Cojocaru <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>

---
 xen/arch/arm/monitor.c | 3 +++
 1 file changed, 3 insertions(+)
xen/arch/arm/monitor.c +3 -0
index 59ce8f635f..ae1b97993f 100644
@@ -63,6 +63,9 @@ int monitor_smc(void)
         .reason = VM_EVENT_REASON_PRIVILEGED_CALL
     };
 
+    /* Nuke the icache as the memory may get changed underneath us. */
+    invalidate_icache();
+
     return monitor_traps(current, 1, &req);
 }
 
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Acked-by on v1 1/1
#1 lore raw
Quoted 6 lines · Tamas K Lengyel

During an SMC trap it is possible that the user may change the memory from where the SMC was fetched. However, without flushing the icache the SMC may still trigger if the pCPU was idle during the trap. Flush the icache to ensure consistency.

Fair enough, assuming the ARM maintainers have no objections:

Thanks,
Razvan

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

#2 lore raw
In reply to Tamas K Lengyel

Hi Tamas,

Quoted 1 line · Tamas K Lengyel

During an SMC trap it is possible that the user may change the memory

By user, do you mean the monitor application?

Quoted 3 lines

from where the SMC was fetched. However, without flushing the icache the SMC may still trigger if the pCPU was idle during the trap. Flush the icache to ensure consistency.

invalidate_icache will invalidate the cache to PoU on all the pCPUs. But here you only mention a problem on the current pCPU. So which behavior do you want to achieve?

Quoted 15 lines

---
Cc: Razvan Cojocaru <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>

---
 xen/arch/arm/monitor.c | 3 +++
 1 file changed, 3 insertions(+)
xen/arch/arm/monitor.c +0 -0
index 59ce8f635f..ae1b97993f 100644

xen/arch/arm/monitor.c int monitor_smc(void) · line 68

Quoted 6 lines
xen/arch/arm/monitor.c +3 -0
@@ -63,6 +63,9 @@ int monitor_smc(void)
         .reason = VM_EVENT_REASON_PRIVILEGED_CALL
     };
 
+    /* Nuke the icache as the memory may get changed underneath us. */
+    invalidate_icache();
+

Can you explain why you put this call before the monitor trap and not after? From my understanding the monitoring application may change the memory. But by the time you modify the instruction, the instruction cache may have prefetched the previous instruction. So the problem is still there.

Furthermore, the instruction cache may not snoop the data cache. So you have to ensure the data written reached the memory. Otherwise you may read the previous instruction. Where is it done? If you expect the monitor app to flush the data cache, then please comment it.

Lastly, this looks like to me that all the traps will have this issue. So maybe this should go in monitor_traps instead?

xen/arch/arm/monitor.c int monitor_smc(void) · line 71

Quoted 4 lines
xen/arch/arm/monitor.c +0 -0
     return monitor_traps(current, 1, &req);
 }
 

Cheers,

Signature · 6 lines
Julien Grall

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

A message that is not in the archive (sent off-list or not yet received): CAErYnshBEZsKqaz8xrxFCrQVNefkepGO+4-+KXAqSXjBfY5Xtw@mail.gmail.com

#4 lore raw

(CC xen-devel, Ravzan, and Stefao)

Hi Tamas,

Not sure why you only CC me on the answer. I have CCed again xen-devel as I don't see any sensible discussion in it.

Quoted 11 lines · Tamas K Lengyel
Quoted 8 lines · Julien Grall

Hi Tamas,

Quoted 2 lines · Tamas K Lengyel

During an SMC trap it is possible that the user may change the memory

By user, do you mean the monitor application?

Yes.

It would be worth clarifying in the commit message.

Quoted 44 lines
Quoted 10 lines
Quoted 5 lines

from where the SMC was fetched. However, without flushing the icache the SMC may still trigger if the pCPU was idle during the trap. Flush the icache to ensure consistency.

invalidate_icache will invalidate the cache to PoU on all the pCPUs. But here you only mention a problem on the current pCPU. So which behavior do you want to achieve?

It would be sufficient to flush the icache on the specific pCPU that trapped with the SMC. Didn't see anything defined in Xen to achieve that.

Quoted 19 lines
Quoted 19 lines

---
Cc: Razvan Cojocaru <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>

---
 xen/arch/arm/monitor.c | 3 +++
 1 file changed, 3 insertions(+)
xen/arch/arm/monitor.c +0 -0
index 59ce8f635f..ae1b97993f 100644
@@ -63,6 +63,9 @@ int monitor_smc(void)
         .reason = VM_EVENT_REASON_PRIVILEGED_CALL
     };
 
Quoted 9 lines

xen/arch/arm/monitor.c int monitor_smc(void) · line 68

Quoted 3 lines
xen/arch/arm/monitor.c +3 -0
+    /* Nuke the icache as the memory may get changed underneath us. */
+    invalidate_icache();
+

Can you explain why you put this call before the monitor trap and not after? From my understanding the monitoring application may change the memory. But by the time you modify the instruction, the instruction cache may have prefetched the previous instruction. So the problem is still there.

Quoted 5 lines

Not sure how would that happen exactly? The vCPU gets paused until the user responds to the vm_event request, so where would it perform the prefetch again? Also, with this change I've verified that the repeated SMC issue no longer presents itself (it has been triggered quite regularly on the hikey before).

The ARM ARM provides a set of expected behavior and where to call the cache maintenance instruction. If it is not done correctly, it may not affect your platform but at some point in the future (or even already today) it will break a platform. I wish you good luck to debug that when it happens. It is a really nightmare :).

So what I care the most is what is the correct behavior based on the ARM ARM. A good overview can be found in a talk made by Mark Rutland [1].

What I was concerned about is the instruction cache been shared between multiple processors (for instance L2 cache or higher). A vCPU could also get rescheduled to another processor afterwards. Leading to accessing an out of date instruction cache.

Quoted 5 lines

Also, for multi-vCPU guest another vCPU fetching the SMC before the memory modification happen (ie. just after this flush) is not a problem and is expected behavior. Providing a non-racy setup for trapping on multi-vCPU guests requires other work (like altp2m).

I am not that concerned about the SMC itself, but the fact that you may modify the guest memory whilst it is running. So another vCPU may end up to execute a mix between new and old instructions depending of the state of its instruction cache. That would result to a potential undefined behavior.

Also, you want to make sure that if you write another SMC in memory, it is effectively affecting all the vCPUs now and not a moment after.

So I still think the invalidate_icache should be done afterwards. IHMO modifying guest instructions while other vCPU are running is very fragile as other thread may execute the instructions your are running.

Quoted 11 lines
Quoted 6 lines

Furthermore, the instruction cache may not snoop the data cache. So you have to ensure the data written reached the memory. Otherwise you may read the previous instruction. Where is it done? If you expect the monitor app to flush the data cache, then please comment it.

AFAICT there is no public API for the user to call to request flushing the caches like that. Memory could get changed by the user any time under the VM, not just during event callbacks. So I'm not sure where that comment should be added.

If the monitor app can modify the guest memory at any time. Then you need to provide an interface to clean the data cache + invalidate the instruction cache. Otherwise the guest may execute stale instructions.

An hypercall might not be necessary because I think (this need to be check) that you could do execute both cache flush from the monitor app. So I would document this in the vm_event header, or in a document explaining the good practices to implement a monitor app.

OOI, when you are modifying the guest memory, do you pause all the vCPUs?

Quoted 8 lines
Quoted 4 lines

Lastly, this looks like to me that all the traps will have this issue. So maybe this should go in monitor_traps instead?

Memory traps do not have this issue AFAICT as they prohibit the CPU from fetching the memory to begin with, so the caches wouldn't have stale contents.

I was not speaking about memory traps but potential other system register traps (TTBR,...) that would be added in the future.

Regarding the memory trap. I think you can get into the same problem if you decide to forbid read-write but allow instruction fetch. You would need some cache maintenance instruction here if you decide to modify the memory.

Quoted 3 lines

Tamas

[1] https://events.linuxfoundation.org/sites/events/files/slides/slides_17.pdf

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#5 lore raw

CC correct e-mail address for Stefano.

Quoted 159 lines · Julien Grall

(CC xen-devel, Ravzan, and Stefao)

Hi Tamas,

Not sure why you only CC me on the answer. I have CCed again xen-devel as I don't see any sensible discussion in it.

Quoted 4 lines · Tamas K Lengyel
Quoted 2 lines · Julien Grall

Hi Tamas,

Quoted 8 lines
Quoted 6 lines · Tamas K Lengyel
Quoted 2 lines

During an SMC trap it is possible that the user may change the memory

By user, do you mean the monitor application?

Yes.

It would be worth clarifying in the commit message.

Quoted 47 lines
Quoted 11 lines
Quoted 5 lines

from where the SMC was fetched. However, without flushing the icache the SMC may still trigger if the pCPU was idle during the trap. Flush the icache to ensure consistency.

invalidate_icache will invalidate the cache to PoU on all the pCPUs. But here you only mention a problem on the current pCPU. So which behavior do
you want to achieve?

It would be sufficient to flush the icache on the specific pCPU that trapped with the SMC. Didn't see anything defined in Xen to achieve that.

Quoted 19 lines
Quoted 19 lines

---
Cc: Razvan Cojocaru <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>

---
 xen/arch/arm/monitor.c | 3 +++
 1 file changed, 3 insertions(+)
xen/arch/arm/monitor.c +0 -0
index 59ce8f635f..ae1b97993f 100644
@@ -63,6 +63,9 @@ int monitor_smc(void)
         .reason = VM_EVENT_REASON_PRIVILEGED_CALL
     };
 
Quoted 11 lines

xen/arch/arm/monitor.c int monitor_smc(void) · line 68

Quoted 3 lines
xen/arch/arm/monitor.c +3 -0
+    /* Nuke the icache as the memory may get changed underneath us. */
+    invalidate_icache();
+

Can you explain why you put this call before the monitor trap and not after?
From my understanding the monitoring application may change the memory. But
by the time you modify the instruction, the instruction cache may have prefetched the previous instruction. So the problem is still there.

Quoted 5 lines

Not sure how would that happen exactly? The vCPU gets paused until the user responds to the vm_event request, so where would it perform the prefetch again? Also, with this change I've verified that the repeated SMC issue no longer presents itself (it has been triggered quite regularly on the hikey before).

The ARM ARM provides a set of expected behavior and where to call the cache maintenance instruction. If it is not done correctly, it may not affect your platform but at some point in the future (or even already today) it will break a platform. I wish you good luck to debug that when it happens. It is a really nightmare :).

So what I care the most is what is the correct behavior based on the ARM ARM. A good overview can be found in a talk made by Mark Rutland [1].

What I was concerned about is the instruction cache been shared between multiple processors (for instance L2 cache or higher). A vCPU could also get rescheduled to another processor afterwards. Leading to accessing an out of date instruction cache.

Quoted 5 lines

Also, for multi-vCPU guest another vCPU fetching the SMC before the memory modification happen (ie. just after this flush) is not a problem and is expected behavior. Providing a non-racy setup for trapping on multi-vCPU guests requires other work (like altp2m).

I am not that concerned about the SMC itself, but the fact that you may modify the guest memory whilst it is running. So another vCPU may end up to execute a mix between new and old instructions depending of the state of its instruction cache. That would result to a potential undefined behavior.

Also, you want to make sure that if you write another SMC in memory, it is effectively affecting all the vCPUs now and not a moment after.

So I still think the invalidate_icache should be done afterwards. IHMO modifying guest instructions while other vCPU are running is very fragile as other thread may execute the instructions your are running.

Quoted 9 lines
Quoted 8 lines

Furthermore, the instruction cache may not snoop the data cache. So you have
to ensure the data written reached the memory. Otherwise you may read the
previous instruction. Where is it done? If you expect the monitor app to flush the data cache, then please comment it.

Quoted 4 lines

AFAICT there is no public API for the user to call to request flushing the caches like that. Memory could get changed by the user any time under the VM, not just during event callbacks. So I'm not sure where that comment should be added.

If the monitor app can modify the guest memory at any time. Then you need to provide an interface to clean the data cache + invalidate the instruction cache. Otherwise the guest may execute stale instructions.

An hypercall might not be necessary because I think (this need to be check) that you could do execute both cache flush from the monitor app. So I would document this in the vm_event header, or in a document explaining the good practices to implement a monitor app.

OOI, when you are modifying the guest memory, do you pause all the vCPUs?

Quoted 9 lines
Quoted 5 lines

Lastly, this looks like to me that all the traps will have this issue. So
maybe this should go in monitor_traps instead?

Memory traps do not have this issue AFAICT as they prohibit the CPU from fetching the memory to begin with, so the caches wouldn't have stale contents.

I was not speaking about memory traps but potential other system register traps (TTBR,...) that would be added in the future.

Regarding the memory trap. I think you can get into the same problem if you decide to forbid read-write but allow instruction fetch. You would need some cache maintenance instruction here if you decide to modify the memory.

Quoted 3 lines

Tamas

[1]
https://events.linuxfoundation.org/sites/events/files/slides/slides_17.pdf

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#6 lore raw
In reply to Julien Grall
Quoted 93 lines · Julien Grall

(CC xen-devel, Ravzan, and Stefao)

Hi Tamas,

Not sure why you only CC me on the answer. I have CCed again xen-devel as I don't see any sensible discussion in it.

Quoted 9 lines · Tamas K Lengyel
Quoted 6 lines · Julien Grall

Hi Tamas,

Quoted 2 lines · Tamas K Lengyel
Quoted 8 lines
Quoted 5 lines
Quoted 1 line

During an SMC trap it is possible that the user may change the memory

By user, do you mean the monitor application?

Yes.

It would be worth clarifying in the commit message.

Quoted 50 lines
Quoted 11 lines
Quoted 5 lines

from where the SMC was fetched. However, without flushing the icache the SMC may still trigger if the pCPU was idle during the trap. Flush the icache to ensure consistency.

invalidate_icache will invalidate the cache to PoU on all the pCPUs. But here you only mention a problem on the current pCPU. So which behavior do you want to achieve?

It would be sufficient to flush the icache on the specific pCPU that trapped with the SMC. Didn't see anything defined in Xen to achieve that.

Quoted 19 lines
Quoted 19 lines

---
Cc: Razvan Cojocaru <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>

---
 xen/arch/arm/monitor.c | 3 +++
 1 file changed, 3 insertions(+)
xen/arch/arm/monitor.c +0 -0
index 59ce8f635f..ae1b97993f 100644
@@ -63,6 +63,9 @@ int monitor_smc(void)
         .reason = VM_EVENT_REASON_PRIVILEGED_CALL
     };
 
Quoted 12 lines

xen/arch/arm/monitor.c int monitor_smc(void) · line 68

Quoted 3 lines
xen/arch/arm/monitor.c +3 -0
+    /* Nuke the icache as the memory may get changed underneath us. */
+    invalidate_icache();
+

Can you explain why you put this call before the monitor trap and not after?
From my understanding the monitoring application may change the memory. But
by the time you modify the instruction, the instruction cache may have prefetched the previous instruction. So the problem is still there.

Quoted 5 lines

Not sure how would that happen exactly? The vCPU gets paused until the user responds to the vm_event request, so where would it perform the prefetch again? Also, with this change I've verified that the repeated SMC issue no longer presents itself (it has been triggered quite regularly on the hikey before).

The ARM ARM provides a set of expected behavior and where to call the cache maintenance instruction. If it is not done correctly, it may not affect your platform but at some point in the future (or even already today) it will break a platform. I wish you good luck to debug that when it happens. It is a really nightmare :).

Quoted 7 lines

So what I care the most is what is the correct behavior based on the ARM ARM. A good overview can be found in a talk made by Mark Rutland [1].

What I was concerned about is the instruction cache been shared between multiple processors (for instance L2 cache or higher). A vCPU could also get rescheduled to another processor afterwards. Leading to accessing an out of date instruction cache.

I see, yea, in that case the instruction may still trigger regardless. Sigh.

Quoted 8 lines
Quoted 6 lines

Also, for multi-vCPU guest another vCPU fetching the SMC before the memory modification happen (ie. just after this flush) is not a problem and is expected behavior. Providing a non-racy setup for trapping on multi-vCPU guests requires other work (like altp2m).

Quoted 7 lines

I am not that concerned about the SMC itself, but the fact that you may modify the guest memory whilst it is running. So another vCPU may end up to execute a mix between new and old instructions depending of the state of its instruction cache. That would result to a potential undefined behavior.

Also, you want to make sure that if you write another SMC in memory, it is effectively affecting all the vCPUs now and not a moment after.

Yeap.

Quoted 4 lines

So I still think the invalidate_icache should be done afterwards. IHMO modifying guest instructions while other vCPU are running is very fragile as other thread may execute the instructions your are running.

I see your point, just got confused because the return from monitor_traps is not the return from the user. That just sends off the notification to the user. The actual return happens elsewhere once the user replies. That would be the point where the flush should happen.

Quoted 16 lines
Quoted 10 lines
Quoted 8 lines

Furthermore, the instruction cache may not snoop the data cache. So you have
to ensure the data written reached the memory. Otherwise you may read the previous instruction. Where is it done? If you expect the monitor app to flush the data cache, then please comment it.

Quoted 4 lines

AFAICT there is no public API for the user to call to request flushing the caches like that. Memory could get changed by the user any time under the VM, not just during event callbacks. So I'm not sure where that comment should be added.

Quoted 6 lines

If the monitor app can modify the guest memory at any time. Then you need to provide an interface to clean the data cache + invalidate the instruction cache. Otherwise the guest may execute stale instructions.

An hypercall might not be necessary because I think (this need to be check) that you could do execute both cache flush from the monitor app.

True! I'll check what happens if I execute "ic ialluis" in the monitor app instead of Xen. If the monitor application can do its own cache maintenance than that would be the best option.

Quoted 4 lines

So I would document this in the vm_event header, or in a document explaining the good practices to implement a monitor app.

OOI, when you are modifying the guest memory, do you pause all the vCPUs?

Yes, the initial SMC injection happens with all vCPUs paused. This was also just a test-case for single-vCPU systems as removing/adding SMCs into memory with a shared p2m view across vCPUs is known to be racy without pausing all vCPUs.

Quoted 7 lines
Quoted 7 lines
Quoted 5 lines

Lastly, this looks like to me that all the traps will have this issue. So maybe this should go in monitor_traps instead?

Quoted 7 lines
Quoted 3 lines

Memory traps do not have this issue AFAICT as they prohibit the CPU from fetching the memory to begin with, so the caches wouldn't have stale contents.

I was not speaking about memory traps but potential other system register traps (TTBR,...) that would be added in the future.

Gotcha.

Quoted 4 lines

Regarding the memory trap. I think you can get into the same problem if you decide to forbid read-write but allow instruction fetch. You would need some cache maintenance instruction here if you decide to modify the memory.

I guess cache maintenance should happen every time the memory is modified. The best place for that is where the user actually does the modification so I'll check whether that's possible. If so, then that simplifies things on the Xen side.

Thanks!
Tamas

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

#7 lore raw
Quoted 152 lines · Tamas K Lengyel
Quoted 93 lines · Julien Grall

(CC xen-devel, Ravzan, and Stefao)

Hi Tamas,

Not sure why you only CC me on the answer. I have CCed again xen-devel as I don't see any sensible discussion in it.

Quoted 9 lines · Tamas K Lengyel
Quoted 6 lines · Julien Grall

Hi Tamas,

Quoted 2 lines · Tamas K Lengyel
Quoted 8 lines
Quoted 5 lines
Quoted 1 line

During an SMC trap it is possible that the user may change the memory

By user, do you mean the monitor application?

Yes.

It would be worth clarifying in the commit message.

Quoted 50 lines
Quoted 11 lines
Quoted 5 lines

from where the SMC was fetched. However, without flushing the icache the SMC may still trigger if the pCPU was idle during the trap. Flush the icache to ensure consistency.

invalidate_icache will invalidate the cache to PoU on all the pCPUs. But here you only mention a problem on the current pCPU. So which behavior do you want to achieve?

It would be sufficient to flush the icache on the specific pCPU that trapped with the SMC. Didn't see anything defined in Xen to achieve that.

Quoted 19 lines
Quoted 19 lines

---
Cc: Razvan Cojocaru <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>

---
 xen/arch/arm/monitor.c | 3 +++
 1 file changed, 3 insertions(+)
xen/arch/arm/monitor.c +0 -0
index 59ce8f635f..ae1b97993f 100644
@@ -63,6 +63,9 @@ int monitor_smc(void)
         .reason = VM_EVENT_REASON_PRIVILEGED_CALL
     };
 
Quoted 12 lines

xen/arch/arm/monitor.c int monitor_smc(void) · line 68

Quoted 3 lines
xen/arch/arm/monitor.c +3 -0
+    /* Nuke the icache as the memory may get changed underneath us. */
+    invalidate_icache();
+

Can you explain why you put this call before the monitor trap and not after?
From my understanding the monitoring application may change the memory. But
by the time you modify the instruction, the instruction cache may have prefetched the previous instruction. So the problem is still there.

Quoted 5 lines

Not sure how would that happen exactly? The vCPU gets paused until the user responds to the vm_event request, so where would it perform the prefetch again? Also, with this change I've verified that the repeated SMC issue no longer presents itself (it has been triggered quite regularly on the hikey before).

The ARM ARM provides a set of expected behavior and where to call the cache maintenance instruction. If it is not done correctly, it may not affect your platform but at some point in the future (or even already today) it will break a platform. I wish you good luck to debug that when it happens. It is a really nightmare :).

Quoted 7 lines

So what I care the most is what is the correct behavior based on the ARM ARM. A good overview can be found in a talk made by Mark Rutland [1].

What I was concerned about is the instruction cache been shared between multiple processors (for instance L2 cache or higher). A vCPU could also get rescheduled to another processor afterwards. Leading to accessing an out of date instruction cache.

I see, yea, in that case the instruction may still trigger regardless. Sigh.

Quoted 8 lines
Quoted 6 lines

Also, for multi-vCPU guest another vCPU fetching the SMC before the memory modification happen (ie. just after this flush) is not a problem and is expected behavior. Providing a non-racy setup for trapping on multi-vCPU guests requires other work (like altp2m).

Quoted 7 lines

I am not that concerned about the SMC itself, but the fact that you may modify the guest memory whilst it is running. So another vCPU may end up to execute a mix between new and old instructions depending of the state of its instruction cache. That would result to a potential undefined behavior.

Also, you want to make sure that if you write another SMC in memory, it is effectively affecting all the vCPUs now and not a moment after.

Yeap.

Quoted 4 lines

So I still think the invalidate_icache should be done afterwards. IHMO modifying guest instructions while other vCPU are running is very fragile as other thread may execute the instructions your are running.

I see your point, just got confused because the return from monitor_traps is not the return from the user. That just sends off the notification to the user. The actual return happens elsewhere once the user replies. That would be the point where the flush should happen.

Quoted 20 lines
Quoted 10 lines
Quoted 8 lines

Furthermore, the instruction cache may not snoop the data cache. So you have
to ensure the data written reached the memory. Otherwise you may read the previous instruction. Where is it done? If you expect the monitor app to flush the data cache, then please comment it.

Quoted 4 lines

AFAICT there is no public API for the user to call to request flushing the caches like that. Memory could get changed by the user any time under the VM, not just during event callbacks. So I'm not sure where that comment should be added.

If the monitor app can modify the guest memory at any time. Then you need to provide an interface to clean the data cache + invalidate the instruction cache. Otherwise the guest may execute stale instructions.

Quoted 7 lines
Quoted 2 lines

An hypercall might not be necessary because I think (this need to be check) that you could do execute both cache flush from the monitor app.

True! I'll check what happens if I execute "ic ialluis" in the monitor app instead of Xen. If the monitor application can do its own cache maintenance than that would be the best option.

So according to the manual (C5.3.9-10) IALLU and IALLUIS are not available from EL0. IVAU can be enabled to execute in EL0; however on my 4.1 kernel it generates a permission fault. I'm also not sure if IVAU would actually be a solution. So IMHO a hypercall would be a good way to enable safe flushing of the caches while all vCPUs of the target domain are paused. Doing so only in a vm_event reply may be insufficient as the memory could be changed outside of vm_event callbacks. Thoughts?

Tamas

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

#8 lore raw
Quoted 162 lines · Tamas K Lengyel
Quoted 152 lines · Tamas K Lengyel
Quoted 93 lines · Julien Grall

(CC xen-devel, Ravzan, and Stefao)

Hi Tamas,

Not sure why you only CC me on the answer. I have CCed again xen-devel as I don't see any sensible discussion in it.

Quoted 9 lines · Tamas K Lengyel
Quoted 6 lines · Julien Grall

Hi Tamas,

Quoted 2 lines · Tamas K Lengyel
Quoted 8 lines
Quoted 5 lines
Quoted 1 line

During an SMC trap it is possible that the user may change the memory

By user, do you mean the monitor application?

Yes.

It would be worth clarifying in the commit message.

Quoted 50 lines
Quoted 11 lines
Quoted 5 lines

from where the SMC was fetched. However, without flushing the icache the SMC may still trigger if the pCPU was idle during the trap. Flush the icache to ensure consistency.

invalidate_icache will invalidate the cache to PoU on all the pCPUs. But here you only mention a problem on the current pCPU. So which behavior do you want to achieve?

It would be sufficient to flush the icache on the specific pCPU that trapped with the SMC. Didn't see anything defined in Xen to achieve that.

Quoted 19 lines
Quoted 19 lines

---
Cc: Razvan Cojocaru <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>

---
 xen/arch/arm/monitor.c | 3 +++
 1 file changed, 3 insertions(+)
xen/arch/arm/monitor.c +0 -0
index 59ce8f635f..ae1b97993f 100644
@@ -63,6 +63,9 @@ int monitor_smc(void)
         .reason = VM_EVENT_REASON_PRIVILEGED_CALL
     };
 
Quoted 12 lines

xen/arch/arm/monitor.c int monitor_smc(void) · line 68

Quoted 3 lines
xen/arch/arm/monitor.c +3 -0
+    /* Nuke the icache as the memory may get changed underneath us. */
+    invalidate_icache();
+

Can you explain why you put this call before the monitor trap and not after?
From my understanding the monitoring application may change the memory. But
by the time you modify the instruction, the instruction cache may have prefetched the previous instruction. So the problem is still there.

Quoted 5 lines

Not sure how would that happen exactly? The vCPU gets paused until the user responds to the vm_event request, so where would it perform the prefetch again? Also, with this change I've verified that the repeated SMC issue no longer presents itself (it has been triggered quite regularly on the hikey before).

The ARM ARM provides a set of expected behavior and where to call the cache maintenance instruction. If it is not done correctly, it may not affect your platform but at some point in the future (or even already today) it will break a platform. I wish you good luck to debug that when it happens. It is a really nightmare :).

Quoted 7 lines

So what I care the most is what is the correct behavior based on the ARM ARM. A good overview can be found in a talk made by Mark Rutland [1].

What I was concerned about is the instruction cache been shared between multiple processors (for instance L2 cache or higher). A vCPU could also get rescheduled to another processor afterwards. Leading to accessing an out of date instruction cache.

I see, yea, in that case the instruction may still trigger regardless. Sigh.

Quoted 8 lines
Quoted 6 lines

Also, for multi-vCPU guest another vCPU fetching the SMC before the memory modification happen (ie. just after this flush) is not a problem and is expected behavior. Providing a non-racy setup for trapping on multi-vCPU guests requires other work (like altp2m).

Quoted 7 lines

I am not that concerned about the SMC itself, but the fact that you may modify the guest memory whilst it is running. So another vCPU may end up to execute a mix between new and old instructions depending of the state of its instruction cache. That would result to a potential undefined behavior.

Also, you want to make sure that if you write another SMC in memory, it is effectively affecting all the vCPUs now and not a moment after.

Yeap.

Quoted 4 lines

So I still think the invalidate_icache should be done afterwards. IHMO modifying guest instructions while other vCPU are running is very fragile as other thread may execute the instructions your are running.

I see your point, just got confused because the return from monitor_traps is not the return from the user. That just sends off the notification to the user. The actual return happens elsewhere once the user replies. That would be the point where the flush should happen.

Quoted 20 lines
Quoted 10 lines
Quoted 8 lines

Furthermore, the instruction cache may not snoop the data cache. So you have
to ensure the data written reached the memory. Otherwise you may read the previous instruction. Where is it done? If you expect the monitor app to flush the data cache, then please comment it.

Quoted 4 lines

AFAICT there is no public API for the user to call to request flushing the caches like that. Memory could get changed by the user any time under the VM, not just during event callbacks. So I'm not sure where that comment should be added.

If the monitor app can modify the guest memory at any time. Then you need to provide an interface to clean the data cache + invalidate the instruction cache. Otherwise the guest may execute stale instructions.

Quoted 7 lines
Quoted 2 lines

An hypercall might not be necessary because I think (this need to be check) that you could do execute both cache flush from the monitor app.

True! I'll check what happens if I execute "ic ialluis" in the monitor app instead of Xen. If the monitor application can do its own cache maintenance than that would be the best option.

Quoted 4 lines

So according to the manual (C5.3.9-10) IALLU and IALLUIS are not available from EL0. IVAU can be enabled to execute in EL0; however on my 4.1 kernel it generates a permission fault. I'm also not sure if IVAU would actually be a solution.

I think there is a Linux syscall available for this kind of things, called cacheflush. See arch/arm/kernel/traps.c:arm_syscall.

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

#9 lore raw

Hi Stefano,

Quoted 11 lines · Stefano Stabellini
Quoted 7 lines · Tamas K Lengyel
Quoted 1 line · Tamas K Lengyel

On Thu, Jan 26, 2017 at 4:30 AM, Julien Grall <[email protected]> wrote:

So according to the manual (C5.3.9-10) IALLU and IALLUIS are not available from EL0. IVAU can be enabled to execute in EL0; however on my 4.1 kernel it generates a permission fault. I'm also not sure if IVAU would actually be a solution.

I think there is a Linux syscall available for this kind of things, called cacheflush. See arch/arm/kernel/traps.c:arm_syscall.

The cacheflush system is only implement on ARM32. And even thought, we have a domctl cacheflush for this purpose.

Cheers,

Signature · 6 lines
Julien Grall

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