Skip to contentSkip to conversation

s390/kvm: implement clearing part of IPL clear

PATCH In review Christian Borntraeger 6 participants 16 messages Feb 28, 2018 – Mar 5, 2018

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 s390/kvm: implement clearing part of IPL clear

When a guests reboots with diagnose 308 subcode 3 it requests the memory to be cleared. We did not do it so far. This does not only violate the architecture, it also misses the chance to free up that memory on reboot, which would help on host memory over commitment. By using ram_block_discard_range we can cover both cases.

---
 target/s390x/kvm.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
target/s390x/kvm.c +19 -0
index 8f3a422288..2e145ad5c3 100644
@@ -34,6 +34,8 @@
 #include "qapi/error.h"
 #include "qemu/error-report.h"
 #include "qemu/timer.h"
+#include "qemu/rcu_queue.h"
+#include "sysemu/cpus.h"
 #include "sysemu/sysemu.h"
 #include "sysemu/hw_accel.h"
 #include "hw/boards.h"
@@ -41,6 +43,7 @@
 #include "sysemu/device_tree.h"
 #include "exec/gdbstub.h"
 #include "exec/address-spaces.h"
+#include "exec/ram_addr.h"
 #include "trace.h"
 #include "qapi-event.h"
 #include "hw/s390x/s390-pci-inst.h"
@@ -1841,6 +1844,14 @@ static int kvm_arch_handle_debug_exit(S390CPU *cpu)
     return ret;
 }
 
+static void release_all_rams(void)
+{
+    struct RAMBlock *rb;
+
+    QLIST_FOREACH_RCU(rb, &ram_list.blocks, next)
+        ram_block_discard_range(rb, 0, rb->used_length);
+}
+
 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
 {
     S390CPU *cpu = S390_CPU(cs);
@@ -1853,6 +1864,14 @@ int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
             ret = handle_intercept(cpu);
             break;
         case KVM_EXIT_S390_RESET:
+            if (run->s390_reset_flags & KVM_S390_RESET_CLEAR) {
+                /*
+                 * We will stop other CPUs anyway, avoid spurious crashes and
+                 * get all CPUs out. The reset will take care of the resume.
+                 */
+                pause_all_vcpus();
+                release_all_rams();
+            }
             s390_reipl_request();
             break;
         case KVM_EXIT_S390_TSCH:
Signature · 1 line
2.14.3
#1 lore raw
Quoted 5 lines · Christian Borntraeger

When a guests reboots with diagnose 308 subcode 3 it requests the memory to be cleared. We did not do it so far. This does not only violate the architecture, it also misses the chance to free up that memory on reboot, which would help on host memory over commitment. By using ram_block_discard_range we can cover both cases.

Sounds like a good idea. I wonder whether that release_all_ram() function should maybe rather reside in exec.c, so that other machines that want to clear all RAM at reset time can use it, too?

Quoted 27 lines
---
 target/s390x/kvm.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
target/s390x/kvm.c +3 -0
index 8f3a422288..2e145ad5c3 100644
@@ -34,6 +34,8 @@
 #include "qapi/error.h"
 #include "qemu/error-report.h"
 #include "qemu/timer.h"
+#include "qemu/rcu_queue.h"
+#include "sysemu/cpus.h"
 #include "sysemu/sysemu.h"
 #include "sysemu/hw_accel.h"
 #include "hw/boards.h"
@@ -41,6 +43,7 @@
 #include "sysemu/device_tree.h"
 #include "exec/gdbstub.h"
 #include "exec/address-spaces.h"
+#include "exec/ram_addr.h"
 #include "trace.h"
 #include "qapi-event.h"
 #include "hw/s390x/s390-pci-inst.h"

target/s390x/kvm.c static int kvm_arch_handle_debug_exit(S390CPU *cpu) · line 1847

Quoted 4 lines
target/s390x/kvm.c +1 -0
@@ -1841,6 +1844,14 @@ static int kvm_arch_handle_debug_exit(S390CPU *cpu)
     return ret;
 }
 
+static void release_all_rams(void)

s/rams/ram/ maybe?

Quoted 7 lines
target/s390x/kvm.c +5 -0
+{
+    struct RAMBlock *rb;
+
+    QLIST_FOREACH_RCU(rb, &ram_list.blocks, next)
+        ram_block_discard_range(rb, 0, rb->used_length);
 

From a coding style point of view, I think there should be curly braces

around ram_block_discard_range() ?

Quoted 11 lines
target/s390x/kvm.c +3 -0
+}
+
 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
 {
     S390CPU *cpu = S390_CPU(cs);
@@ -1853,6 +1864,14 @@ int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
             ret = handle_intercept(cpu);
             break;
         case KVM_EXIT_S390_RESET:
+            if (run->s390_reset_flags & KVM_S390_RESET_CLEAR) {

target/s390x/kvm.c int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run) · line 1877

Quoted 10 lines
target/s390x/kvm.c +7 -0
+                /*
+                 * We will stop other CPUs anyway, avoid spurious crashes and
+                 * get all CPUs out. The reset will take care of the resume.
+                 */
+                pause_all_vcpus();
+                release_all_rams();
+            }
             s390_reipl_request();
             break;
         case KVM_EXIT_S390_TSCH:

Apart from the cosmetic nits, patch looks good to me.

Thomas

#2 lore raw
Quoted 10 lines · Thomas Huth
Quoted 5 lines · Christian Borntraeger

When a guests reboots with diagnose 308 subcode 3 it requests the memory to be cleared. We did not do it so far. This does not only violate the architecture, it also misses the chance to free up that memory on reboot, which would help on host memory over commitment. By using ram_block_discard_range we can cover both cases.

Sounds like a good idea. I wonder whether that release_all_ram() function should maybe rather reside in exec.c, so that other machines that want to clear all RAM at reset time can use it, too?

You already added Paolo, David - good.
I am open to that. As an alternative we can certainly move this function from s390x/kvm.c to exec.c at a later point in time if a 2nd user comes along.

Quoted 31 lines
Quoted 31 lines
---
 target/s390x/kvm.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
target/s390x/kvm.c +3 -0
index 8f3a422288..2e145ad5c3 100644
@@ -34,6 +34,8 @@
 #include "qapi/error.h"
 #include "qemu/error-report.h"
 #include "qemu/timer.h"
+#include "qemu/rcu_queue.h"
+#include "sysemu/cpus.h"
 #include "sysemu/sysemu.h"
 #include "sysemu/hw_accel.h"
 #include "hw/boards.h"
@@ -41,6 +43,7 @@
 #include "sysemu/device_tree.h"
 #include "exec/gdbstub.h"
 #include "exec/address-spaces.h"
+#include "exec/ram_addr.h"
 #include "trace.h"
 #include "qapi-event.h"
 #include "hw/s390x/s390-pci-inst.h"
@@ -1841,6 +1844,14 @@ static int kvm_arch_handle_debug_exit(S390CPU *cpu)
     return ret;
 }
 
Quoted 3 lines

target/s390x/kvm.c static int kvm_arch_handle_debug_exit(S390CPU *cpu) · line 1847

Quoted 1 line
target/s390x/kvm.c +1 -0
+static void release_all_rams(void)

s/rams/ram/ maybe?

yes.

Quoted 9 lines

target/s390x/kvm.c static int kvm_arch_handle_debug_exit(S390CPU *cpu) · line 1852

Quoted 6 lines
target/s390x/kvm.c +5 -0
+{
+    struct RAMBlock *rb;
+
+    QLIST_FOREACH_RCU(rb, &ram_list.blocks, next)
+        ram_block_discard_range(rb, 0, rb->used_length);

From a coding style point of view, I think there should be curly braces around ram_block_discard_range() ?

yes.

Quoted 16 lines
Quoted 16 lines
target/s390x/kvm.c +8 -0
+}
+
 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
 {
     S390CPU *cpu = S390_CPU(cs);
@@ -1853,6 +1864,14 @@ int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
             ret = handle_intercept(cpu);
             break;
         case KVM_EXIT_S390_RESET:
+            if (run->s390_reset_flags & KVM_S390_RESET_CLEAR) {
+                /*
+                 * We will stop other CPUs anyway, avoid spurious crashes and
+                 * get all CPUs out. The reset will take care of the resume.
+                 */
+                pause_all_vcpus();
Quoted 8 lines

target/s390x/kvm.c int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run) · line 1877

Quoted 6 lines
target/s390x/kvm.c +2 -0
+                release_all_rams();
+            }
             s390_reipl_request();
             break;
         case KVM_EXIT_S390_TSCH:

Apart from the cosmetic nits, patch looks good to me.

Thanks. Will wait with the resend till Paolo,David have some comments.

#3 lore raw
In reply to Thomas Huth
Quoted 9 lines · Thomas Huth
Quoted 5 lines

When a guests reboots with diagnose 308 subcode 3 it requests the memory to be cleared. We did not do it so far. This does not only violate the architecture, it also misses the chance to free up that memory on reboot, which would help on host memory over commitment. By using ram_block_discard_range we can cover both cases.

Sounds like a good idea. I wonder whether that release_all_ram() function should maybe rather reside in exec.c, so that other machines that want to clear all RAM at reset time can use it, too?

Either way is fine for me. With s/rams/ram/ the s390-only patch is ok.

Paolo

#4 lore raw
In reply to Thomas Huth
Quoted 45 lines · * Thomas Huth ([email protected]) wrote:
Quoted 5 lines · Christian Borntraeger

When a guests reboots with diagnose 308 subcode 3 it requests the memory to be cleared. We did not do it so far. This does not only violate the architecture, it also misses the chance to free up that memory on reboot, which would help on host memory over commitment. By using ram_block_discard_range we can cover both cases.

Sounds like a good idea. I wonder whether that release_all_ram() function should maybe rather reside in exec.c, so that other machines that want to clear all RAM at reset time can use it, too?

Quoted 27 lines
---
 target/s390x/kvm.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
target/s390x/kvm.c +3 -0
index 8f3a422288..2e145ad5c3 100644
@@ -34,6 +34,8 @@
 #include "qapi/error.h"
 #include "qemu/error-report.h"
 #include "qemu/timer.h"
+#include "qemu/rcu_queue.h"
+#include "sysemu/cpus.h"
 #include "sysemu/sysemu.h"
 #include "sysemu/hw_accel.h"
 #include "hw/boards.h"
@@ -41,6 +43,7 @@
 #include "sysemu/device_tree.h"
 #include "exec/gdbstub.h"
 #include "exec/address-spaces.h"
+#include "exec/ram_addr.h"
 #include "trace.h"
 #include "qapi-event.h"
 #include "hw/s390x/s390-pci-inst.h"

target/s390x/kvm.c static int kvm_arch_handle_debug_exit(S390CPU *cpu) · line 1847

Quoted 4 lines
target/s390x/kvm.c +1 -0
@@ -1841,6 +1844,14 @@ static int kvm_arch_handle_debug_exit(S390CPU *cpu)
     return ret;
 }
 
+static void release_all_rams(void)

s/rams/ram/ maybe?

Quoted 8 lines

target/s390x/kvm.c static int kvm_arch_handle_debug_exit(S390CPU *cpu) · line 1852

Quoted 5 lines
target/s390x/kvm.c +5 -0
+{
+    struct RAMBlock *rb;
+
+    QLIST_FOREACH_RCU(rb, &ram_list.blocks, next)
+        ram_block_discard_range(rb, 0, rb->used_length);

From a coding style point of view, I think there should be curly braces around ram_block_discard_range() ?

I think this might break if it happens during a postcopy migrate. The destination CPU is running, so it can do a reboot at just the wrong time; and then the pages (that are protected by userfaultfd) would get deallocated and trigger userfaultfd requests if accessed.

Dave

Quoted 24 lines
Quoted 11 lines
target/s390x/kvm.c +3 -0
+}
+
 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
 {
     S390CPU *cpu = S390_CPU(cs);
@@ -1853,6 +1864,14 @@ int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
             ret = handle_intercept(cpu);
             break;
         case KVM_EXIT_S390_RESET:
+            if (run->s390_reset_flags & KVM_S390_RESET_CLEAR) {

target/s390x/kvm.c int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run) · line 1877

Quoted 10 lines
target/s390x/kvm.c +7 -0
+                /*
+                 * We will stop other CPUs anyway, avoid spurious crashes and
+                 * get all CPUs out. The reset will take care of the resume.
+                 */
+                pause_all_vcpus();
+                release_all_rams();
+            }
             s390_reipl_request();
             break;
         case KVM_EXIT_S390_TSCH:

Apart from the cosmetic nits, patch looks good to me.

Quoted 1 line

Thomas

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#5 lore raw
Quoted 55 lines · Dr. David Alan Gilbert
Quoted 45 lines · * Thomas Huth ([email protected]) wrote:
Quoted 5 lines · Christian Borntraeger

When a guests reboots with diagnose 308 subcode 3 it requests the memory to be cleared. We did not do it so far. This does not only violate the architecture, it also misses the chance to free up that memory on reboot, which would help on host memory over commitment. By using ram_block_discard_range we can cover both cases.

Sounds like a good idea. I wonder whether that release_all_ram() function should maybe rather reside in exec.c, so that other machines that want to clear all RAM at reset time can use it, too?

Quoted 27 lines
---
 target/s390x/kvm.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
target/s390x/kvm.c +3 -0
index 8f3a422288..2e145ad5c3 100644
@@ -34,6 +34,8 @@
 #include "qapi/error.h"
 #include "qemu/error-report.h"
 #include "qemu/timer.h"
+#include "qemu/rcu_queue.h"
+#include "sysemu/cpus.h"
 #include "sysemu/sysemu.h"
 #include "sysemu/hw_accel.h"
 #include "hw/boards.h"
@@ -41,6 +43,7 @@
 #include "sysemu/device_tree.h"
 #include "exec/gdbstub.h"
 #include "exec/address-spaces.h"
+#include "exec/ram_addr.h"
 #include "trace.h"
 #include "qapi-event.h"
 #include "hw/s390x/s390-pci-inst.h"

target/s390x/kvm.c static int kvm_arch_handle_debug_exit(S390CPU *cpu) · line 1847

Quoted 4 lines
target/s390x/kvm.c +1 -0
@@ -1841,6 +1844,14 @@ static int kvm_arch_handle_debug_exit(S390CPU *cpu)
     return ret;
 }
 
+static void release_all_rams(void)

s/rams/ram/ maybe?

Quoted 8 lines

target/s390x/kvm.c static int kvm_arch_handle_debug_exit(S390CPU *cpu) · line 1852

Quoted 5 lines
target/s390x/kvm.c +5 -0
+{
+    struct RAMBlock *rb;
+
+    QLIST_FOREACH_RCU(rb, &ram_list.blocks, next)
+        ram_block_discard_range(rb, 0, rb->used_length);

From a coding style point of view, I think there should be curly braces around ram_block_discard_range() ?

Quoted 4 lines

I think this might break if it happens during a postcopy migrate. The destination CPU is running, so it can do a reboot at just the wrong time; and then the pages (that are protected by userfaultfd) would get deallocated and trigger userfaultfd requests if accessed.

Yes, userfaultd/postcopy is really fragile and relies on things that are not necessarily true (e.g. virito-balloon can also invalidate pages).

The right thing here would be to actually terminate the postcopy migrate but return it as "successful" (since we are going to clear that RAM anyway). Do you see a good way to achieve that?

Quoted 31 lines

Dave

Quoted 24 lines
Quoted 11 lines
target/s390x/kvm.c +3 -0
+}
+
 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
 {
     S390CPU *cpu = S390_CPU(cs);
@@ -1853,6 +1864,14 @@ int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
             ret = handle_intercept(cpu);
             break;
         case KVM_EXIT_S390_RESET:
+            if (run->s390_reset_flags & KVM_S390_RESET_CLEAR) {

target/s390x/kvm.c int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run) · line 1877

Quoted 10 lines
target/s390x/kvm.c +7 -0
+                /*
+                 * We will stop other CPUs anyway, avoid spurious crashes and
+                 * get all CPUs out. The reset will take care of the resume.
+                 */
+                pause_all_vcpus();
+                release_all_rams();
+            }
             s390_reipl_request();
             break;
         case KVM_EXIT_S390_TSCH:

Apart from the cosmetic nits, patch looks good to me.

Quoted 1 line

Thomas

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#6 lore raw
Quoted 58 lines · * Christian Borntraeger ([email protected]) wrote:
Quoted 55 lines · Dr. David Alan Gilbert
Quoted 53 lines · * Thomas Huth ([email protected]) wrote:
Quoted 5 lines · Christian Borntraeger

When a guests reboots with diagnose 308 subcode 3 it requests the memory to be cleared. We did not do it so far. This does not only violate the architecture, it also misses the chance to free up that memory on reboot, which would help on host memory over commitment. By using ram_block_discard_range we can cover both cases.

Sounds like a good idea. I wonder whether that release_all_ram() function should maybe rather reside in exec.c, so that other machines that want to clear all RAM at reset time can use it, too?

Quoted 27 lines
---
 target/s390x/kvm.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
target/s390x/kvm.c +3 -0
index 8f3a422288..2e145ad5c3 100644
@@ -34,6 +34,8 @@
 #include "qapi/error.h"
 #include "qemu/error-report.h"
 #include "qemu/timer.h"
+#include "qemu/rcu_queue.h"
+#include "sysemu/cpus.h"
 #include "sysemu/sysemu.h"
 #include "sysemu/hw_accel.h"
 #include "hw/boards.h"
@@ -41,6 +43,7 @@
 #include "sysemu/device_tree.h"
 #include "exec/gdbstub.h"
 #include "exec/address-spaces.h"
+#include "exec/ram_addr.h"
 #include "trace.h"
 #include "qapi-event.h"
 #include "hw/s390x/s390-pci-inst.h"

target/s390x/kvm.c static int kvm_arch_handle_debug_exit(S390CPU *cpu) · line 1847

Quoted 4 lines
target/s390x/kvm.c +1 -0
@@ -1841,6 +1844,14 @@ static int kvm_arch_handle_debug_exit(S390CPU *cpu)
     return ret;
 }
 
+static void release_all_rams(void)

s/rams/ram/ maybe?

target/s390x/kvm.c static int kvm_arch_handle_debug_exit(S390CPU *cpu) · line 1852

Quoted 5 lines
target/s390x/kvm.c +5 -0
+{
+    struct RAMBlock *rb;
+
+    QLIST_FOREACH_RCU(rb, &ram_list.blocks, next)
+        ram_block_discard_range(rb, 0, rb->used_length);

From a coding style point of view, I think there should be curly braces around ram_block_discard_range() ?

Quoted 7 lines
Quoted 4 lines

I think this might break if it happens during a postcopy migrate. The destination CPU is running, so it can do a reboot at just the wrong time; and then the pages (that are protected by userfaultfd) would get deallocated and trigger userfaultfd requests if accessed.

Yes, userfaultd/postcopy is really fragile and relies on things that are not necessarily true (e.g. virito-balloon can also invalidate pages).

That's why we use qemu_balloon_inhibit around postcopy to stop ballooning; I'm not aware of anything else that does the same.

Quoted 3 lines

The right thing here would be to actually terminate the postcopy migrate but return it as "successful" (since we are going to clear that RAM anyway). Do you see a good way to achieve that?

There's no current mechanism to do it; I think it would have to involve some interaction with the source as well though to tell it that you didn't need that area of RAM anyway.

However, there are more problems:
a) Even forgetting the userfault problem, this is racy since during
postcopy you're still receiving blocks from the source at the same time; so some of the area that you've discarded might get overwritten by data from the source.

b) Your release_all_rams seems to do all RAM Blocks - won't that nuke
any ROMs as well? Or maybe even flash?

c) In a normal precopy migration, I think you may also get old data;
Paolo said that an MADV_DONTNEED won't cause the dirty flags to be set, so if the migrate has already sent the data for a page, and then this happens, before the CPUs are stopped during the migration, when you restart on the destination you'll have the old data.

Dave

Quoted 28 lines
Quoted 28 lines

Dave

Quoted 24 lines
Quoted 11 lines
target/s390x/kvm.c +3 -0
+}
+
 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
 {
     S390CPU *cpu = S390_CPU(cs);
@@ -1853,6 +1864,14 @@ int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
             ret = handle_intercept(cpu);
             break;
         case KVM_EXIT_S390_RESET:
+            if (run->s390_reset_flags & KVM_S390_RESET_CLEAR) {

target/s390x/kvm.c int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run) · line 1877

Quoted 10 lines
target/s390x/kvm.c +7 -0
+                /*
+                 * We will stop other CPUs anyway, avoid spurious crashes and
+                 * get all CPUs out. The reset will take care of the resume.
+                 */
+                pause_all_vcpus();
+                release_all_rams();
+            }
             s390_reipl_request();
             break;
         case KVM_EXIT_S390_TSCH:

Apart from the cosmetic nits, patch looks good to me.

Quoted 5 lines
Quoted 4 lines
Quoted 1 line

Thomas

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#7 lore raw
Quoted 64 lines · Dr. David Alan Gilbert
Quoted 63 lines · * Christian Borntraeger ([email protected]) wrote:
Quoted 59 lines · Dr. David Alan Gilbert
Quoted 45 lines · * Thomas Huth ([email protected]) wrote:
Quoted 5 lines · Christian Borntraeger

When a guests reboots with diagnose 308 subcode 3 it requests the memory to be cleared. We did not do it so far. This does not only violate the architecture, it also misses the chance to free up that memory on reboot, which would help on host memory over commitment. By using ram_block_discard_range we can cover both cases.

Sounds like a good idea. I wonder whether that release_all_ram() function should maybe rather reside in exec.c, so that other machines that want to clear all RAM at reset time can use it, too?

Quoted 27 lines
---
 target/s390x/kvm.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
target/s390x/kvm.c +3 -0
index 8f3a422288..2e145ad5c3 100644
@@ -34,6 +34,8 @@
 #include "qapi/error.h"
 #include "qemu/error-report.h"
 #include "qemu/timer.h"
+#include "qemu/rcu_queue.h"
+#include "sysemu/cpus.h"
 #include "sysemu/sysemu.h"
 #include "sysemu/hw_accel.h"
 #include "hw/boards.h"
@@ -41,6 +43,7 @@
 #include "sysemu/device_tree.h"
 #include "exec/gdbstub.h"
 #include "exec/address-spaces.h"
+#include "exec/ram_addr.h"
 #include "trace.h"
 #include "qapi-event.h"
 #include "hw/s390x/s390-pci-inst.h"

target/s390x/kvm.c static int kvm_arch_handle_debug_exit(S390CPU *cpu) · line 1847

Quoted 4 lines
target/s390x/kvm.c +1 -0
@@ -1841,6 +1844,14 @@ static int kvm_arch_handle_debug_exit(S390CPU *cpu)
     return ret;
 }
 
+static void release_all_rams(void)

s/rams/ram/ maybe?

Quoted 8 lines

target/s390x/kvm.c static int kvm_arch_handle_debug_exit(S390CPU *cpu) · line 1852

Quoted 5 lines
target/s390x/kvm.c +5 -0
+{
+    struct RAMBlock *rb;
+
+    QLIST_FOREACH_RCU(rb, &ram_list.blocks, next)
+        ram_block_discard_range(rb, 0, rb->used_length);

From a coding style point of view, I think there should be curly braces around ram_block_discard_range() ?

I think this might break if it happens during a postcopy migrate. The destination CPU is running, so it can do a reboot at just the wrong time; and then the pages (that are protected by userfaultfd) would get deallocated and trigger userfaultfd requests if accessed.

Quoted 5 lines
Quoted 2 lines

Yes, userfaultd/postcopy is really fragile and relies on things that are not necessarily true (e.g. virito-balloon can also invalidate pages).

That's why we use qemu_balloon_inhibit around postcopy to stop ballooning; I'm not aware of anything else that does the same.

we also have at least the pte_unused thing in mm/rmap.c that clearly predates userfaultfd. We might need to look into this as well....

Quoted 9 lines
Quoted 4 lines

The right thing here would be to actually terminate the postcopy migrate but return it as "successful" (since we are going to clear that RAM anyway). Do you see a good way to achieve that?

There's no current mechanism to do it; I think it would have to involve some interaction with the source as well though to tell it that you didn't need that area of RAM anyway.

Quoted 5 lines

However, there are more problems:
a) Even forgetting the userfault problem, this is racy since during
postcopy you're still receiving blocks from the source at the same time; so some of the area that you've discarded might get overwritten by data from the source.

So how do you handle the case when the target system writes to memory that is still in flight? Can we build on that mechanism?

Quoted 3 lines

b) Your release_all_rams seems to do all RAM Blocks - won't that nuke
any ROMs as well? Or maybe even flash?

ROMs loaded with load_elf (like our s390-ccw.img) are reloaded on every reset. See rom_reset in /hw/core/loader.c

Is this different with the x86 bios?

Quoted 6 lines

c) In a normal precopy migration, I think you may also get old data;
Paolo said that an MADV_DONTNEED won't cause the dirty flags to be set, so if the migrate has already sent the data for a page, and then this happens, before the CPUs are stopped during the migration, when you restart on the destination you'll have the old data.

Yes, looks like we might get non-cleared data. Could we maybe combine fixing and optimizing: we can stop tranmitting the memory and do a clean startup on the target side. In other words could we actually use the reset clear trigger to speed up migration?

Quoted 39 lines

Dave

Quoted 28 lines
Quoted 28 lines

Dave

Quoted 24 lines
Quoted 11 lines
target/s390x/kvm.c +3 -0
+}
+
 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
 {
     S390CPU *cpu = S390_CPU(cs);
@@ -1853,6 +1864,14 @@ int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
             ret = handle_intercept(cpu);
             break;
         case KVM_EXIT_S390_RESET:
+            if (run->s390_reset_flags & KVM_S390_RESET_CLEAR) {

target/s390x/kvm.c int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run) · line 1877

Quoted 10 lines
target/s390x/kvm.c +7 -0
+                /*
+                 * We will stop other CPUs anyway, avoid spurious crashes and
+                 * get all CPUs out. The reset will take care of the resume.
+                 */
+                pause_all_vcpus();
+                release_all_rams();
+            }
             s390_reipl_request();
             break;
         case KVM_EXIT_S390_TSCH:

Apart from the cosmetic nits, patch looks good to me.

Quoted 5 lines
Quoted 4 lines
Quoted 1 line

Thomas

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#8 lore raw
Quoted 67 lines · * Christian Borntraeger ([email protected]) wrote:
Quoted 64 lines · Dr. David Alan Gilbert
Quoted 63 lines · * Christian Borntraeger ([email protected]) wrote:
Quoted 59 lines · Dr. David Alan Gilbert
Quoted 45 lines · * Thomas Huth ([email protected]) wrote:
Quoted 5 lines · Christian Borntraeger

When a guests reboots with diagnose 308 subcode 3 it requests the memory to be cleared. We did not do it so far. This does not only violate the architecture, it also misses the chance to free up that memory on reboot, which would help on host memory over commitment. By using ram_block_discard_range we can cover both cases.

Sounds like a good idea. I wonder whether that release_all_ram() function should maybe rather reside in exec.c, so that other machines that want to clear all RAM at reset time can use it, too?

Quoted 27 lines
---
 target/s390x/kvm.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
target/s390x/kvm.c +3 -0
index 8f3a422288..2e145ad5c3 100644
@@ -34,6 +34,8 @@
 #include "qapi/error.h"
 #include "qemu/error-report.h"
 #include "qemu/timer.h"
+#include "qemu/rcu_queue.h"
+#include "sysemu/cpus.h"
 #include "sysemu/sysemu.h"
 #include "sysemu/hw_accel.h"
 #include "hw/boards.h"
@@ -41,6 +43,7 @@
 #include "sysemu/device_tree.h"
 #include "exec/gdbstub.h"
 #include "exec/address-spaces.h"
+#include "exec/ram_addr.h"
 #include "trace.h"
 #include "qapi-event.h"
 #include "hw/s390x/s390-pci-inst.h"

target/s390x/kvm.c static int kvm_arch_handle_debug_exit(S390CPU *cpu) · line 1847

Quoted 4 lines
target/s390x/kvm.c +1 -0
@@ -1841,6 +1844,14 @@ static int kvm_arch_handle_debug_exit(S390CPU *cpu)
     return ret;
 }
 
+static void release_all_rams(void)

s/rams/ram/ maybe?

Quoted 8 lines

target/s390x/kvm.c static int kvm_arch_handle_debug_exit(S390CPU *cpu) · line 1852

Quoted 5 lines
target/s390x/kvm.c +5 -0
+{
+    struct RAMBlock *rb;
+
+    QLIST_FOREACH_RCU(rb, &ram_list.blocks, next)
+        ram_block_discard_range(rb, 0, rb->used_length);

From a coding style point of view, I think there should be curly braces around ram_block_discard_range() ?

I think this might break if it happens during a postcopy migrate. The destination CPU is running, so it can do a reboot at just the wrong time; and then the pages (that are protected by userfaultfd) would get deallocated and trigger userfaultfd requests if accessed.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Yes, userfaultd/postcopy is really fragile and relies on things that are not necessarily true (e.g. virito-balloon can also invalidate pages).

That's why we use qemu_balloon_inhibit around postcopy to stop ballooning; I'm not aware of anything else that does the same.

we also have at least the pte_unused thing in mm/rmap.c that clearly predates userfaultfd. We might need to look into this as well....

I've not come across that; what does that do?

Quoted 9 lines
Quoted 9 lines
Quoted 4 lines

The right thing here would be to actually terminate the postcopy migrate but return it as "successful" (since we are going to clear that RAM anyway). Do you see a good way to achieve that?

There's no current mechanism to do it; I think it would have to involve some interaction with the source as well though to tell it that you didn't need that area of RAM anyway.

Quoted 8 lines
Quoted 5 lines

However, there are more problems:
a) Even forgetting the userfault problem, this is racy since during
postcopy you're still receiving blocks from the source at the same time; so some of the area that you've discarded might get overwritten by data from the source.

So how do you handle the case when the target system writes to memory that is still in flight? Can we build on that mechanism?

Once we've entered postcopy, a page is basically in one of two states:
a) Not yet received - i.e. marked absent with MADV_DONTNEED; if the
guest tries to write to it then it'll block with userfault and ask the source for the page; so the write wont happen until the page arrives.
b) Received - we've already got the page from the source; the source
never resends a page (once in postcopy) so now the destination can just write to the page.

Once in postcopy, a page is received at most once (i.e. if it's not been received during precopy).

I can imagine two ways of curing it:
a) Simple but slow; just read all the pages before doing the
discard, this forces it to wait for the pages to be received.
b) More complex but fast; Add a message on the return path to the
source telling it that you're going to discard a range; the source then marks it's notes as cleared for those pages and then sends some form of ack, and at that point you drop it.

A 3rd; incomplete way; would be just to drop the userfaultfd on the destination for the RAMBlocks that are being cleared; but this does leave the source state in a bit of a mess.

Quoted 5 lines
Quoted 2 lines

b) Your release_all_rams seems to do all RAM Blocks - won't that nuke
any ROMs as well? Or maybe even flash?

ROMs loaded with load_elf (like our s390-ccw.img) are reloaded on every reset. See rom_reset in /hw/core/loader.c

Ah, so this is happening after your reset code you've added?

Quoted 1 line

Is this different with the x86 bios?

Not sure; I know x86 keeps some mirrored copies of ROMs across reboots but I don't fully understand the mechanisms we use. But the other case I was thinking of was stuff like pflash on x86 which are the flash images holding variable data.
(Also watch out for the way ram_block_discard_range deals with file backed memory; discarding is actually quite hard in some cases).

Quoted 10 lines
Quoted 5 lines

c) In a normal precopy migration, I think you may also get old data;
Paolo said that an MADV_DONTNEED won't cause the dirty flags to be set, so if the migrate has already sent the data for a page, and then this happens, before the CPUs are stopped during the migration, when you restart on the destination you'll have the old data.

Yes, looks like we might get non-cleared data. Could we maybe combine fixing and optimizing: we can stop tranmitting the memory and do a clean startup on the target side. In other words could we actually use the reset clear trigger to speed up migration?

They're separate problems because they happen on opposite sides; on the source you've got a chance of doing that type of hack, but it would be a bit invasive.

Dave

Quoted 34 lines
Quoted 34 lines

Dave

Quoted 28 lines
Quoted 28 lines

Dave

Quoted 24 lines
Quoted 11 lines
target/s390x/kvm.c +3 -0
+}
+
 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
 {
     S390CPU *cpu = S390_CPU(cs);
@@ -1853,6 +1864,14 @@ int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
             ret = handle_intercept(cpu);
             break;
         case KVM_EXIT_S390_RESET:
+            if (run->s390_reset_flags & KVM_S390_RESET_CLEAR) {

target/s390x/kvm.c int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run) · line 1877

Quoted 10 lines
target/s390x/kvm.c +7 -0
+                /*
+                 * We will stop other CPUs anyway, avoid spurious crashes and
+                 * get all CPUs out. The reset will take care of the resume.
+                 */
+                pause_all_vcpus();
+                release_all_rams();
+            }
             s390_reipl_request();
             break;
         case KVM_EXIT_S390_TSCH:

Apart from the cosmetic nits, patch looks good to me.

Quoted 9 lines
Quoted 8 lines
Quoted 5 lines
Quoted 4 lines
Quoted 1 line

Thomas

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#9 lore raw
Quoted 71 lines · Dr. David Alan Gilbert
Quoted 70 lines · * Christian Borntraeger ([email protected]) wrote:
Quoted 67 lines · Dr. David Alan Gilbert
Quoted 65 lines · * Christian Borntraeger ([email protected]) wrote:
Quoted 55 lines · Dr. David Alan Gilbert
Quoted 45 lines · * Thomas Huth ([email protected]) wrote:
Quoted 5 lines · Christian Borntraeger

When a guests reboots with diagnose 308 subcode 3 it requests the memory to be cleared. We did not do it so far. This does not only violate the architecture, it also misses the chance to free up that memory on reboot, which would help on host memory over commitment. By using ram_block_discard_range we can cover both cases.

Sounds like a good idea. I wonder whether that release_all_ram() function should maybe rather reside in exec.c, so that other machines that want to clear all RAM at reset time can use it, too?

Quoted 27 lines
---
 target/s390x/kvm.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
target/s390x/kvm.c +3 -0
index 8f3a422288..2e145ad5c3 100644
@@ -34,6 +34,8 @@
 #include "qapi/error.h"
 #include "qemu/error-report.h"
 #include "qemu/timer.h"
+#include "qemu/rcu_queue.h"
+#include "sysemu/cpus.h"
 #include "sysemu/sysemu.h"
 #include "sysemu/hw_accel.h"
 #include "hw/boards.h"
@@ -41,6 +43,7 @@
 #include "sysemu/device_tree.h"
 #include "exec/gdbstub.h"
 #include "exec/address-spaces.h"
+#include "exec/ram_addr.h"
 #include "trace.h"
 #include "qapi-event.h"
 #include "hw/s390x/s390-pci-inst.h"

target/s390x/kvm.c static int kvm_arch_handle_debug_exit(S390CPU *cpu) · line 1847

Quoted 4 lines
target/s390x/kvm.c +1 -0
@@ -1841,6 +1844,14 @@ static int kvm_arch_handle_debug_exit(S390CPU *cpu)
     return ret;
 }
 
+static void release_all_rams(void)

s/rams/ram/ maybe?

Quoted 8 lines

target/s390x/kvm.c static int kvm_arch_handle_debug_exit(S390CPU *cpu) · line 1852

Quoted 5 lines
target/s390x/kvm.c +5 -0
+{
+    struct RAMBlock *rb;
+
+    QLIST_FOREACH_RCU(rb, &ram_list.blocks, next)
+        ram_block_discard_range(rb, 0, rb->used_length);

From a coding style point of view, I think there should be curly braces around ram_block_discard_range() ?

Quoted 4 lines

I think this might break if it happens during a postcopy migrate. The destination CPU is running, so it can do a reboot at just the wrong time; and then the pages (that are protected by userfaultfd) would get deallocated and trigger userfaultfd requests if accessed.

Yes, userfaultd/postcopy is really fragile and relies on things that are not necessarily true (e.g. virito-balloon can also invalidate pages).

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

That's why we use qemu_balloon_inhibit around postcopy to stop ballooning; I'm not aware of anything else that does the same.

we also have at least the pte_unused thing in mm/rmap.c that clearly predates userfaultfd. We might need to look into this as well....

I've not come across that; what does that do?

It can drop a page on page out if the page is no longer of value. It is used by the CMMA (guest page hinting) code of s390x.

see kernel mm/rmap.c

static bool try_to_unmap_one(struct page *page, struct vm_area_struct *vma,
unsigned long address, void *arg)
{
[...]

                } else if (pte_unused(pteval)) {
                        /*
                         * The guest indicated that the page content is of no
                         * interest anymore. Simply discard the pte, vmscan
                         * will take care of the rest.
                         */
			dec_mm_counter(mm, mm_counter(page));
                        /* We have to invalidate as we cleared the pte */
                        mmu_notifier_invalidate_range(mm, address,
                                                      address + PAGE_SIZE);
                } else if (IS_ENABLED(CONFIG_MIGRATION) &&
                                (flags & (TTU_MIGRATION|TTU_SPLIT_FREEZE))) {

[...]

Quoted 30 lines
Quoted 10 lines
Quoted 10 lines
Quoted 5 lines

The right thing here would be to actually terminate the postcopy migrate but return it as "successful" (since we are going to clear that RAM anyway). Do you see a good way to achieve that?

There's no current mechanism to do it; I think it would have to involve some interaction with the source as well though to tell it that you didn't need that area of RAM anyway.

Quoted 8 lines
Quoted 5 lines

However, there are more problems:
a) Even forgetting the userfault problem, this is racy since during
postcopy you're still receiving blocks from the source at the same time; so some of the area that you've discarded might get overwritten by data from the source.

So how do you handle the case when the target system writes to memory that is still in flight? Can we build on that mechanism?

Once we've entered postcopy, a page is basically in one of two states:
a) Not yet received - i.e. marked absent with MADV_DONTNEED; if the
guest tries to write to it then it'll block with userfault and ask the source for the page; so the write wont happen until the page arrives.
b) Received - we've already got the page from the source; the source
never resends a page (once in postcopy) so now the destination can just write to the page.

Once in postcopy, a page is received at most once (i.e. if it's not been received during precopy).

Quoted 7 lines

I can imagine two ways of curing it:
a) Simple but slow; just read all the pages before doing the
discard, this forces it to wait for the pages to be received.
b) More complex but fast; Add a message on the return path to the
source telling it that you're going to discard a range; the source then marks it's notes as cleared for those pages and then sends some form of ack, and at that point you drop it.

this looks like the most promising approach, but some work.

Quoted 6 lines

A 3rd; incomplete way; would be just to drop the userfaultfd on the destination for the RAMBlocks that are being cleared; but this does leave the source state in a bit of a mess.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

b) Your release_all_rams seems to do all RAM Blocks - won't that nuke
any ROMs as well? Or maybe even flash?

ROMs loaded with load_elf (like our s390-ccw.img) are reloaded on every reset. See rom_reset in /hw/core/loader.c

Ah, so this is happening after your reset code you've added?

Yes, I am stopping all CPU, clear the memory. And then I call system_reset.

#10 lore raw
Quoted 104 lines · Christian Borntraeger
Quoted 71 lines · Dr. David Alan Gilbert
Quoted 70 lines · * Christian Borntraeger ([email protected]) wrote:
Quoted 67 lines · Dr. David Alan Gilbert
Quoted 65 lines · * Christian Borntraeger ([email protected]) wrote:
Quoted 55 lines · Dr. David Alan Gilbert
Quoted 45 lines · * Thomas Huth ([email protected]) wrote:
Quoted 5 lines · Christian Borntraeger

When a guests reboots with diagnose 308 subcode 3 it requests the memory to be cleared. We did not do it so far. This does not only violate the architecture, it also misses the chance to free up that memory on reboot, which would help on host memory over commitment. By using ram_block_discard_range we can cover both cases.

Sounds like a good idea. I wonder whether that release_all_ram() function should maybe rather reside in exec.c, so that other machines that want to clear all RAM at reset time can use it, too?

Quoted 27 lines
---
 target/s390x/kvm.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
target/s390x/kvm.c +3 -0
index 8f3a422288..2e145ad5c3 100644
@@ -34,6 +34,8 @@
 #include "qapi/error.h"
 #include "qemu/error-report.h"
 #include "qemu/timer.h"
+#include "qemu/rcu_queue.h"
+#include "sysemu/cpus.h"
 #include "sysemu/sysemu.h"
 #include "sysemu/hw_accel.h"
 #include "hw/boards.h"
@@ -41,6 +43,7 @@
 #include "sysemu/device_tree.h"
 #include "exec/gdbstub.h"
 #include "exec/address-spaces.h"
+#include "exec/ram_addr.h"
 #include "trace.h"
 #include "qapi-event.h"
 #include "hw/s390x/s390-pci-inst.h"

target/s390x/kvm.c static int kvm_arch_handle_debug_exit(S390CPU *cpu) · line 1847

Quoted 4 lines
target/s390x/kvm.c +1 -0
@@ -1841,6 +1844,14 @@ static int kvm_arch_handle_debug_exit(S390CPU *cpu)
     return ret;
 }
 
+static void release_all_rams(void)

s/rams/ram/ maybe?

Quoted 8 lines

target/s390x/kvm.c static int kvm_arch_handle_debug_exit(S390CPU *cpu) · line 1852

Quoted 5 lines
target/s390x/kvm.c +5 -0
+{
+    struct RAMBlock *rb;
+
+    QLIST_FOREACH_RCU(rb, &ram_list.blocks, next)
+        ram_block_discard_range(rb, 0, rb->used_length);

From a coding style point of view, I think there should be curly braces around ram_block_discard_range() ?

Quoted 4 lines

I think this might break if it happens during a postcopy migrate. The destination CPU is running, so it can do a reboot at just the wrong time; and then the pages (that are protected by userfaultfd) would get deallocated and trigger userfaultfd requests if accessed.

Yes, userfaultd/postcopy is really fragile and relies on things that are not necessarily true (e.g. virito-balloon can also invalidate pages).

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

That's why we use qemu_balloon_inhibit around postcopy to stop ballooning; I'm not aware of anything else that does the same.

we also have at least the pte_unused thing in mm/rmap.c that clearly predates userfaultfd. We might need to look into this as well....

I've not come across that; what does that do?

It can drop a page on page out if the page is no longer of value. It is used by the CMMA (guest page hinting) code of s390x.

see kernel mm/rmap.c

static bool try_to_unmap_one(struct page *page, struct vm_area_struct *vma,
unsigned long address, void *arg)
{
[...]

                } else if (pte_unused(pteval)) {
                        /*
                         * The guest indicated that the page content is of no
                         * interest anymore. Simply discard the pte, vmscan
                         * will take care of the rest.
                         */
			dec_mm_counter(mm, mm_counter(page));
                        /* We have to invalidate as we cleared the pte */
                        mmu_notifier_invalidate_range(mm, address,
                                                      address + PAGE_SIZE);
                } else if (IS_ENABLED(CONFIG_MIGRATION) &&
                                (flags & (TTU_MIGRATION|TTU_SPLIT_FREEZE))) {
Quoted 3 lines

[...]

Maybe something like this in the kernel

mm/rmap.c +1 -1
index 47db27f8049e..9bdf4d448987 100644
@@ -1483,7 +1483,7 @@ static bool try_to_unmap_one(struct page *page, struct vm_area_struct *vma,
                                set_pte_at(mm, address, pvmw.pte, pteval);
                        }
 
-               } else if (pte_unused(pteval)) {
+               } else if (pte_unused(pteval) && !vma->vm_userfaultfd_ctx.ctx) {
                        /*
                         * The guest indicated that the page content is of no
                         * interest anymore. Simply discard the pte, vmscan

could help?

#11 lore raw
Quoted 128 lines · * Christian Borntraeger ([email protected]) wrote:
Quoted 104 lines · Christian Borntraeger
Quoted 71 lines · Dr. David Alan Gilbert
Quoted 70 lines · * Christian Borntraeger ([email protected]) wrote:
Quoted 67 lines · Dr. David Alan Gilbert
Quoted 65 lines · * Christian Borntraeger ([email protected]) wrote:
Quoted 55 lines · Dr. David Alan Gilbert
Quoted 45 lines · * Thomas Huth ([email protected]) wrote:
Quoted 5 lines · Christian Borntraeger

When a guests reboots with diagnose 308 subcode 3 it requests the memory to be cleared. We did not do it so far. This does not only violate the architecture, it also misses the chance to free up that memory on reboot, which would help on host memory over commitment. By using ram_block_discard_range we can cover both cases.

Sounds like a good idea. I wonder whether that release_all_ram() function should maybe rather reside in exec.c, so that other machines that want to clear all RAM at reset time can use it, too?

Quoted 27 lines
---
 target/s390x/kvm.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
target/s390x/kvm.c +3 -0
index 8f3a422288..2e145ad5c3 100644
@@ -34,6 +34,8 @@
 #include "qapi/error.h"
 #include "qemu/error-report.h"
 #include "qemu/timer.h"
+#include "qemu/rcu_queue.h"
+#include "sysemu/cpus.h"
 #include "sysemu/sysemu.h"
 #include "sysemu/hw_accel.h"
 #include "hw/boards.h"
@@ -41,6 +43,7 @@
 #include "sysemu/device_tree.h"
 #include "exec/gdbstub.h"
 #include "exec/address-spaces.h"
+#include "exec/ram_addr.h"
 #include "trace.h"
 #include "qapi-event.h"
 #include "hw/s390x/s390-pci-inst.h"

target/s390x/kvm.c static int kvm_arch_handle_debug_exit(S390CPU *cpu) · line 1847

Quoted 4 lines
target/s390x/kvm.c +1 -0
@@ -1841,6 +1844,14 @@ static int kvm_arch_handle_debug_exit(S390CPU *cpu)
     return ret;
 }
 
+static void release_all_rams(void)

s/rams/ram/ maybe?

Quoted 8 lines

target/s390x/kvm.c static int kvm_arch_handle_debug_exit(S390CPU *cpu) · line 1852

Quoted 5 lines
target/s390x/kvm.c +5 -0
+{
+    struct RAMBlock *rb;
+
+    QLIST_FOREACH_RCU(rb, &ram_list.blocks, next)
+        ram_block_discard_range(rb, 0, rb->used_length);

From a coding style point of view, I think there should be curly braces around ram_block_discard_range() ?

Quoted 4 lines

I think this might break if it happens during a postcopy migrate. The destination CPU is running, so it can do a reboot at just the wrong time; and then the pages (that are protected by userfaultfd) would get deallocated and trigger userfaultfd requests if accessed.

Yes, userfaultd/postcopy is really fragile and relies on things that are not necessarily true (e.g. virito-balloon can also invalidate pages).

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

That's why we use qemu_balloon_inhibit around postcopy to stop ballooning; I'm not aware of anything else that does the same.

we also have at least the pte_unused thing in mm/rmap.c that clearly predates userfaultfd. We might need to look into this as well....

I've not come across that; what does that do?

It can drop a page on page out if the page is no longer of value. It is used by the CMMA (guest page hinting) code of s390x.

see kernel mm/rmap.c

static bool try_to_unmap_one(struct page *page, struct vm_area_struct *vma,
unsigned long address, void *arg)
{
[...]

                } else if (pte_unused(pteval)) {
                        /*
                         * The guest indicated that the page content is of no
                         * interest anymore. Simply discard the pte, vmscan
                         * will take care of the rest.
                         */
			dec_mm_counter(mm, mm_counter(page));
                        /* We have to invalidate as we cleared the pte */
                        mmu_notifier_invalidate_range(mm, address,
                                                      address + PAGE_SIZE);
                } else if (IS_ENABLED(CONFIG_MIGRATION) &&
                                (flags & (TTU_MIGRATION|TTU_SPLIT_FREEZE))) {
Quoted 3 lines

[...]

Maybe something like this in the kernel

mm/rmap.c +1 -1
index 47db27f8049e..9bdf4d448987 100644
@@ -1483,7 +1483,7 @@ static bool try_to_unmap_one(struct page *page, struct vm_area_struct *vma,
                                set_pte_at(mm, address, pvmw.pte, pteval);
                        }
 
-               } else if (pte_unused(pteval)) {
+               } else if (pte_unused(pteval) && !vma->vm_userfaultfd_ctx.ctx) {
                        /*
                         * The guest indicated that the page content is of no
                         * interest anymore. Simply discard the pte, vmscan
Quoted 1 line

could help?

I guess so, but please check with aarcange; I don't know the mm code.

Dave

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#12 lore raw
In reply to Christian Borntraeger
Quoted 104 lines · * Christian Borntraeger ([email protected]) wrote:
Quoted 71 lines · Dr. David Alan Gilbert
Quoted 70 lines · * Christian Borntraeger ([email protected]) wrote:
Quoted 67 lines · Dr. David Alan Gilbert
Quoted 65 lines · * Christian Borntraeger ([email protected]) wrote:
Quoted 55 lines · Dr. David Alan Gilbert
Quoted 45 lines · * Thomas Huth ([email protected]) wrote:
Quoted 5 lines · Christian Borntraeger

When a guests reboots with diagnose 308 subcode 3 it requests the memory to be cleared. We did not do it so far. This does not only violate the architecture, it also misses the chance to free up that memory on reboot, which would help on host memory over commitment. By using ram_block_discard_range we can cover both cases.

Sounds like a good idea. I wonder whether that release_all_ram() function should maybe rather reside in exec.c, so that other machines that want to clear all RAM at reset time can use it, too?

Quoted 27 lines
---
 target/s390x/kvm.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
target/s390x/kvm.c +3 -0
index 8f3a422288..2e145ad5c3 100644
@@ -34,6 +34,8 @@
 #include "qapi/error.h"
 #include "qemu/error-report.h"
 #include "qemu/timer.h"
+#include "qemu/rcu_queue.h"
+#include "sysemu/cpus.h"
 #include "sysemu/sysemu.h"
 #include "sysemu/hw_accel.h"
 #include "hw/boards.h"
@@ -41,6 +43,7 @@
 #include "sysemu/device_tree.h"
 #include "exec/gdbstub.h"
 #include "exec/address-spaces.h"
+#include "exec/ram_addr.h"
 #include "trace.h"
 #include "qapi-event.h"
 #include "hw/s390x/s390-pci-inst.h"

target/s390x/kvm.c static int kvm_arch_handle_debug_exit(S390CPU *cpu) · line 1847

Quoted 4 lines
target/s390x/kvm.c +1 -0
@@ -1841,6 +1844,14 @@ static int kvm_arch_handle_debug_exit(S390CPU *cpu)
     return ret;
 }
 
+static void release_all_rams(void)

s/rams/ram/ maybe?

Quoted 8 lines

target/s390x/kvm.c static int kvm_arch_handle_debug_exit(S390CPU *cpu) · line 1852

Quoted 5 lines
target/s390x/kvm.c +5 -0
+{
+    struct RAMBlock *rb;
+
+    QLIST_FOREACH_RCU(rb, &ram_list.blocks, next)
+        ram_block_discard_range(rb, 0, rb->used_length);

From a coding style point of view, I think there should be curly braces around ram_block_discard_range() ?

Quoted 4 lines

I think this might break if it happens during a postcopy migrate. The destination CPU is running, so it can do a reboot at just the wrong time; and then the pages (that are protected by userfaultfd) would get deallocated and trigger userfaultfd requests if accessed.

Yes, userfaultd/postcopy is really fragile and relies on things that are not necessarily true (e.g. virito-balloon can also invalidate pages).

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

That's why we use qemu_balloon_inhibit around postcopy to stop ballooning; I'm not aware of anything else that does the same.

we also have at least the pte_unused thing in mm/rmap.c that clearly predates userfaultfd. We might need to look into this as well....

I've not come across that; what does that do?

It can drop a page on page out if the page is no longer of value. It is used by the CMMA (guest page hinting) code of s390x.

see kernel mm/rmap.c

static bool try_to_unmap_one(struct page *page, struct vm_area_struct *vma,
unsigned long address, void *arg)
{
[...]

                } else if (pte_unused(pteval)) {
                        /*
                         * The guest indicated that the page content is of no
                         * interest anymore. Simply discard the pte, vmscan
                         * will take care of the rest.
                         */
			dec_mm_counter(mm, mm_counter(page));
                        /* We have to invalidate as we cleared the pte */
                        mmu_notifier_invalidate_range(mm, address,
                                                      address + PAGE_SIZE);
                } else if (IS_ENABLED(CONFIG_MIGRATION) &&
                                (flags & (TTU_MIGRATION|TTU_SPLIT_FREEZE))) {
Quoted 1 line

[...]

OK, probably best to check with Andrea what the best way to get that happy with userfault is.

Quoted 38 lines
Quoted 30 lines
Quoted 10 lines
Quoted 10 lines
Quoted 5 lines

The right thing here would be to actually terminate the postcopy migrate but return it as "successful" (since we are going to clear that RAM anyway). Do you see a good way to achieve that?

There's no current mechanism to do it; I think it would have to involve some interaction with the source as well though to tell it that you didn't need that area of RAM anyway.

Quoted 8 lines
Quoted 5 lines

However, there are more problems:
a) Even forgetting the userfault problem, this is racy since during
postcopy you're still receiving blocks from the source at the same time; so some of the area that you've discarded might get overwritten by data from the source.

So how do you handle the case when the target system writes to memory that is still in flight? Can we build on that mechanism?

Once we've entered postcopy, a page is basically in one of two states:
a) Not yet received - i.e. marked absent with MADV_DONTNEED; if the
guest tries to write to it then it'll block with userfault and ask the source for the page; so the write wont happen until the page arrives.
b) Received - we've already got the page from the source; the source
never resends a page (once in postcopy) so now the destination can just write to the page.

Once in postcopy, a page is received at most once (i.e. if it's not been received during precopy).

Quoted 7 lines

I can imagine two ways of curing it:
a) Simple but slow; just read all the pages before doing the
discard, this forces it to wait for the pages to be received.
b) More complex but fast; Add a message on the return path to the
source telling it that you're going to discard a range; the source then marks it's notes as cleared for those pages and then sends some form of ack, and at that point you drop it.

Quoted 1 line

this looks like the most promising approach, but some work.

Yes, you can add a new MIG_RP_MSG_ type for the destination to tell the source; that's pretty easy. Remember that there will still be pages in flight after you've sent this message so you'll have to wait for those to clear out.

Quoted 9 lines
Quoted 9 lines

A 3rd; incomplete way; would be just to drop the userfaultfd on the destination for the RAMBlocks that are being cleared; but this does leave the source state in a bit of a mess.

Quoted 3 lines
Quoted 2 lines

b) Your release_all_rams seems to do all RAM Blocks - won't that nuke
any ROMs as well? Or maybe even flash?

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

ROMs loaded with load_elf (like our s390-ccw.img) are reloaded on every reset. See rom_reset in /hw/core/loader.c

Ah, so this is happening after your reset code you've added?

Yes, I am stopping all CPU, clear the memory. And then I call system_reset.

OK.

Dave

--
Dr. David Alan Gilbert / [email protected] / Manchester, UK

#13 lore raw
In reply to Christian Borntraeger
Quoted 49 lines · Christian Borntraeger

When a guests reboots with diagnose 308 subcode 3 it requests the memory to be cleared. We did not do it so far. This does not only violate the architecture, it also misses the chance to free up that memory on reboot, which would help on host memory over commitment. By using ram_block_discard_range we can cover both cases.

---
 target/s390x/kvm.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
target/s390x/kvm.c +11 -0
index 8f3a422288..2e145ad5c3 100644
@@ -34,6 +34,8 @@
 #include "qapi/error.h"
 #include "qemu/error-report.h"
 #include "qemu/timer.h"
+#include "qemu/rcu_queue.h"
+#include "sysemu/cpus.h"
 #include "sysemu/sysemu.h"
 #include "sysemu/hw_accel.h"
 #include "hw/boards.h"
@@ -41,6 +43,7 @@
 #include "sysemu/device_tree.h"
 #include "exec/gdbstub.h"
 #include "exec/address-spaces.h"
+#include "exec/ram_addr.h"
 #include "trace.h"
 #include "qapi-event.h"
 #include "hw/s390x/s390-pci-inst.h"
@@ -1841,6 +1844,14 @@ static int kvm_arch_handle_debug_exit(S390CPU *cpu)
     return ret;
 }
 
+static void release_all_rams(void)
+{
+    struct RAMBlock *rb;
+
+    QLIST_FOREACH_RCU(rb, &ram_list.blocks, next)
+        ram_block_discard_range(rb, 0, rb->used_length);
+}
+
 int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
 {
     S390CPU *cpu = S390_CPU(cs);
@@ -1853,6 +1864,14 @@ int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
             ret = handle_intercept(cpu);

target/s390x/kvm.c int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run) · line 1874

Quoted 10 lines
target/s390x/kvm.c +8 -0
             break;
         case KVM_EXIT_S390_RESET:
+            if (run->s390_reset_flags & KVM_S390_RESET_CLEAR) {
+                /*
+                 * We will stop other CPUs anyway, avoid spurious crashes and
+                 * get all CPUs out. The reset will take care of the resume.
+                 */
+                pause_all_vcpus();
+                release_all_rams();
+            }

Hm, I have a patch lying around that tries to match pause/resume calls, and only starts all CPUs again, once the last resume is called. (I assume that there is the chance for a race between 2 callers of pause/resume - you think it cannot happen, but as pause_all_vcpus() will temporarily give up the qemu mutex, bad things can happen).

Can't we communicate that information to our ipl device and do the clearing during the reset?

Quoted 4 lines
target/s390x/kvm.c +0 -0
             s390_reipl_request();
             break;
         case KVM_EXIT_S390_TSCH:
Signature · 3 lines
Thanks,

David / dhildenb
#14 lore raw
In reply to Christian Borntraeger
Quoted 10 lines · On Wed, 28 Feb 2018 19:53:20 +0000 Christian Borntraeger…

When a guests reboots with diagnose 308 subcode 3 it requests the memory to be cleared. We did not do it so far. This does not only violate the architecture, it also misses the chance to free up that memory on reboot, which would help on host memory over commitment. By using ram_block_discard_range we can cover both cases.

---
 target/s390x/kvm.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

So, this needs more thought (and additional kernel changes)? (Trying to pick up patches that make sense for 2.12.)

#15 lore raw
Quoted 9 lines · Cornelia Huck
Quoted 6 lines · On Wed, 28 Feb 2018 19:53:20 +0000 Christian Borntraeger…

When a guests reboots with diagnose 308 subcode 3 it requests the memory to be cleared. We did not do it so far. This does not only violate the architecture, it also misses the chance to free up that memory on reboot, which would help on host memory over commitment. By using ram_block_discard_range we can cover both cases.

Quoted 7 lines
Quoted 4 lines
---
 target/s390x/kvm.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

So, this needs more thought (and additional kernel changes)? (Trying to pick up patches that make sense for 2.12.)

Yes, lets defer this.