Skip to contentSkip to conversation

target/arm: Add MTE support to KVM guest

RFC PATCH In review Haibo Xu 3 participants 15 messages Mar 17, 2021 – Apr 1, 2021

lore
Share & tools
lore

Haibo Xu

author
#0 lore raw

v2 cover target/arm: Add MTE support to KVM guest

This version is rebased against kernel patches v10, and only header files are updated since v1[1].

This series add support for MTE(Memory Tagging Extension)[2] in KVM guest. It's based on Steven Price's kernel KVM patches v10[3], and has been tested to ensure that test case[4] can be passed in a KVM guest. Basic pre-copy migration test also passed between two MTE enabled kvm guest.

This is a RFC patch series becuase:
(1) Need to add some arm MTE specific codes to the ram migration loop. There may be better way to do that in a more abstract way.
(2) Only pre-copy migration is supported and tested currently, post-copy as well as compress/zero page migration are still WIP.

All kinds of feedbacks are very welcomed, especially for the migration support.

Note:
(1) To support MTE migration, tags for one page are appended to the page data during ram save iteration which make it easier to sync the page data and tags.

[1] https://lore.kernel.org/qemu-devel/[email protected]/
[2] https://community.arm.com/developer/ip-products/processors/b/
processors-ip-blog/posts/enhancing-memory-safety
[3] https://patchew.org/QEMU/[email protected]/
[4] https://elixir.bootlin.com/linux/latest/source/Documentation/
arm64/memory-tagging-extension.rst

Haibo Xu (5):
Update Linux headers with new MTE support
Add basic MTE support to KVM guest
Add APIs to get/set MTE tags
Add migration support for KVM guest with MTE
Enable the MTE support for KVM guest

 hw/arm/virt.c             | 44 +++++++++++++-------
 include/hw/arm/virt.h     |  2 +
 include/migration/misc.h  |  1 +
 linux-headers/linux/kvm.h | 16 ++++++++
 migration/ram.c           | 86 ++++++++++++++++++++++++++++++++++++++-
 target/arm/cpu.c          |  2 +-
 target/arm/kvm.c          |  9 ++++
 target/arm/kvm64.c        | 31 ++++++++++++++
 target/arm/kvm_arm.h      |  2 +
 9 files changed, 177 insertions(+), 16 deletions(-)
Signature · 1 line
2.17.1
author
#1 lore raw

RFC PATCH v2 1/5 Update Linux headers with new MTE support

---
 linux-headers/linux/kvm.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
linux-headers/linux/kvm.h +16 -0
index 020b62a619..941743b3a7 100644
@@ -1056,6 +1056,7 @@ struct kvm_ppc_resize_hpt {
 #define KVM_CAP_ENFORCE_PV_FEATURE_CPUID 190
 #define KVM_CAP_SYS_HYPERV_CPUID 191
 #define KVM_CAP_DIRTY_LOG_RING 192
+#define KVM_CAP_ARM_MTE 195
 
 #ifdef KVM_CAP_IRQ_ROUTING
 
@@ -1241,6 +1242,20 @@ struct kvm_arm_device_addr {
 	__u64 addr;
 };
 
+#define KVM_ARM_TAGS_TO_GUEST           0
+#define KVM_ARM_TAGS_FROM_GUEST         1
+
+struct kvm_arm_copy_mte_tags {
+	__u64 guest_ipa;
+	__u64 length;
+	union {
+		void *addr;
+		__u64 padding;
+	};
+	__u64 flags;
+	__u64 reserved[2];
+};
+
 /*
  * Device control API, available with KVM_CAP_DEVICE_CTRL
  */
@@ -1396,6 +1411,7 @@ struct kvm_s390_ucas_mapping {
 /* Available with KVM_CAP_PMU_EVENT_FILTER */
 #define KVM_SET_PMU_EVENT_FILTER  _IOW(KVMIO,  0xb2, struct kvm_pmu_event_filter)
 #define KVM_PPC_SVM_OFF		  _IO(KVMIO,  0xb3)
+#define KVM_ARM_MTE_COPY_TAGS	  _IOR(KVMIO,  0xb4, struct kvm_arm_copy_mte_tags)
 
 /* ioctl for vm fd */
 #define KVM_CREATE_DEVICE	  _IOWR(KVMIO,  0xe0, struct kvm_create_device)
Signature · 1 line
2.17.1
author
#6 lore raw

RFC PATCH v2 4/5 Add migration support for KVM guest with MTE

In reply to Haibo Xu

To make it easier to keep the page tags sync with the page data, tags for one page are appended to the data during ram save iteration.

This patch only add the pre-copy migration support. Post-copy and compress as well as zero page saving are not supported yet.

---
 include/hw/arm/virt.h    |  2 +
 include/migration/misc.h |  1 +
 migration/ram.c          | 86 +++++++++++++++++++++++++++++++++++++++-
 3 files changed, 88 insertions(+), 1 deletion(-)
include/hw/arm/virt.h +2 -0
index 921416f918..8b28cde8bf 100644
@@ -166,6 +166,8 @@ struct VirtMachineState {
     PCIBus *bus;
     char *oem_id;
     char *oem_table_id;
+    /* migrate memory tags */
+    NotifierWithReturn precopy_notifier;
 };
 
 #define VIRT_ECAM_ID(high) (high ? VIRT_HIGH_PCIE_ECAM : VIRT_PCIE_ECAM)
include/migration/misc.h +1 -0
index bccc1b6b44..005133f471 100644
@@ -38,6 +38,7 @@ void precopy_add_notifier(NotifierWithReturn *n);
 void precopy_remove_notifier(NotifierWithReturn *n);
 int precopy_notify(PrecopyNotifyReason reason, Error **errp);
 void precopy_enable_free_page_optimization(void);
+void precopy_enable_metadata_migration(void);
 
 void ram_mig_init(void);
 void qemu_guest_free_page_hint(void *addr, size_t len);
migration/ram.c +85 -1

150 lines not on this page · raw message

Signature · 1 line
2.17.1
author
#3 lore raw

RFC PATCH v2 3/5 Add APIs to get/set MTE tags

In reply to Haibo Xu

MTE spec provide instructions to retrieve the memory tags:
(1) LDG, at 16 bytes granularity, and available in both user

    and kernel space;

(2) LDGM, at 256 bytes granularity in maximum, and only available in kernel space

To improve the performance, KVM has exposed the LDGM capability to user space by providing a new APIs. This patch is just a wrapper for the KVM APIs.

---
 target/arm/kvm64.c   | 24 ++++++++++++++++++++++++
 target/arm/kvm_arm.h |  2 ++
 2 files changed, 26 insertions(+)
target/arm/kvm64.c +24 -0
index 73a191f8e1..3157025316 100644
@@ -1606,3 +1606,27 @@ bool kvm_arm_verify_ext_dabt_pending(CPUState *cs)
     }
     return false;
 }
+
+int kvm_arm_mte_get_tags(uint64_t ipa, uint64_t len, uint8_t *buf)
+{
+    struct kvm_arm_copy_mte_tags args = {
+        .guest_ipa = ipa,
+        .length = len,
+        .addr = buf,
+        .flags = KVM_ARM_TAGS_FROM_GUEST,
+    };
+
+    return kvm_vm_ioctl(kvm_state, KVM_ARM_MTE_COPY_TAGS, &args);
+}
+
+int kvm_arm_mte_set_tags(uint64_t ipa, uint64_t len, uint8_t *buf)
+{
+    struct kvm_arm_copy_mte_tags args = {
+        .guest_ipa = ipa,
+        .length = len,
+        .addr = buf,
+        .flags = KVM_ARM_TAGS_TO_GUEST,
+    };
+
+    return kvm_vm_ioctl(kvm_state, KVM_ARM_MTE_COPY_TAGS, &args);
+}
target/arm/kvm_arm.h +2 -0
index 34f8daa377..bbb833d6c6 100644
@@ -360,6 +360,8 @@ int kvm_arm_vgic_probe(void);
 
 void kvm_arm_pmu_set_irq(CPUState *cs, int irq);
 void kvm_arm_pmu_init(CPUState *cs);
+int kvm_arm_mte_get_tags(uint64_t ipa, uint64_t len, uint8_t *buf);
+int kvm_arm_mte_set_tags(uint64_t ipa, uint64_t len, uint8_t *buf);
 
 /**
  * kvm_arm_pvtime_init:
Signature · 1 line
2.17.1
author
#13 lore raw

RFC PATCH v2 5/5 Enable the MTE support for KVM guest

In reply to Haibo Xu
---
 hw/arm/virt.c | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)
hw/arm/virt.c +19 -3
index 76658b93a3..36cfdb29e9 100644
@@ -79,6 +79,7 @@
 #include "hw/virtio/virtio-iommu.h"
 #include "hw/char/pl011.h"
 #include "qemu/guest-random.h"
+#include "migration/misc.h"
 
 #define DEFINE_VIRT_MACHINE_LATEST(major, minor, latest) \
     static void virt_##major##_##minor##_class_init(ObjectClass *oc, \
@@ -828,6 +829,21 @@ static void virt_powerdown_req(Notifier *n, void *opaque)
     }
 }
 
+static int virt_precopy_notify(NotifierWithReturn *n, void *data)
+{
+    PrecopyNotifyData *pnd = data;
+
+    switch (pnd->reason) {
+    case PRECOPY_NOTIFY_SETUP:
+        precopy_enable_metadata_migration();
+        break;
+    default:
+        break;
+    }
+
+    return 0;
+}
+
 static void create_gpio_keys(char *fdt, DeviceState *pl061_dev,
                              uint32_t phandle)
 {
@@ -1912,9 +1928,9 @@ static void machvirt_init(MachineState *machine)
     }
 
     if (vms->mte && kvm_enabled()) {
-        error_report("mach-virt: KVM does not support providing "
-                     "MTE to the guest CPU");
-        exit(1);
+        /* connect migration precopy request */
+        vms->precopy_notifier.notify = virt_precopy_notify;
+        precopy_add_notifier(&vms->precopy_notifier);
     }
 
     create_fdt(vms);
Signature · 1 line
2.17.1
author
#2 lore raw

RFC PATCH v2 2/5 Add basic MTE support to KVM guest

In reply to Haibo Xu

Enable the virt machine feature "mte" to work with KVM guest. This feature is still hiden from the user in this patch, and will be available in a later patch.

---
 hw/arm/virt.c      | 22 +++++++++++-----------
 target/arm/cpu.c   |  2 +-
 target/arm/kvm.c   |  9 +++++++++
 target/arm/kvm64.c |  7 +++++++
 4 files changed, 28 insertions(+), 12 deletions(-)
hw/arm/virt.c +11 -11
index aa2bbd14e0..76658b93a3 100644
@@ -1988,18 +1988,18 @@ static void machvirt_init(MachineState *machine)
         }
 
         if (vms->mte) {
+            /*
+             * The property exists only if MemTag is supported.
+             * If it is, we must allocate the ram to back that up.
+             */
+            if (!object_property_find(cpuobj, "tag-memory")) {
+                error_report("MTE requested, but not supported "
+                             "by the guest CPU");
+                exit(1);
+            }
+
             /* Create the memory region only once, but link to all cpus. */
-            if (!tag_sysmem) {
-                /*
-                 * The property exists only if MemTag is supported.
-                 * If it is, we must allocate the ram to back that up.
-                 */
-                if (!object_property_find(cpuobj, "tag-memory")) {
-                    error_report("MTE requested, but not supported "
-                                 "by the guest CPU");
-                    exit(1);
-                }
-
+            if (!tag_sysmem && !kvm_enabled()) {
                 tag_sysmem = g_new(MemoryRegion, 1);
                 memory_region_init(tag_sysmem, OBJECT(machine),
                                    "tag-memory", UINT64_MAX / 32);
target/arm/cpu.c +1 -1
index ae04884408..47bf817b61 100644
@@ -1847,7 +1847,7 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
                                cpu->secure_memory);
     }
 
-    if (cpu->tag_memory != NULL) {
+    if (cpu->tag_memory != NULL && !kvm_enabled()) {
         cpu_address_space_init(cs, ARMASIdx_TagNS, "cpu-tag-memory",
                                cpu->tag_memory);
         if (has_secure) {
target/arm/kvm.c +9 -0
index d8381ba224..3403e621ac 100644
@@ -32,6 +32,7 @@
 #include "hw/boards.h"
 #include "hw/irq.h"
 #include "qemu/log.h"
+#include "hw/arm/virt.h"
 
 const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
     KVM_CAP_LAST_INFO
@@ -274,6 +275,14 @@ int kvm_arch_init(MachineState *ms, KVMState *s)
         }
     }
 
+    if (kvm_check_extension(s, KVM_CAP_ARM_MTE) &&
+        object_dynamic_cast(OBJECT(ms), TYPE_VIRT_MACHINE) &&
+        VIRT_MACHINE(ms)->mte) {
+            if (kvm_vm_enable_cap(s, KVM_CAP_ARM_MTE, 0)) {
+                error_report("Failed to enable KVM_CAP_ARM_MTE cap");
+            }
+    }
+
     return ret;
 }
 
target/arm/kvm64.c +7 -0
index dff85f6db9..73a191f8e1 100644
@@ -500,6 +500,7 @@ bool kvm_arm_get_host_cpu_features(ARMHostCPUFeatures *ahcf)
      */
     int fdarray[3];
     bool sve_supported;
+    bool mte_supported;
     uint64_t features = 0;
     uint64_t t;
     int err;
@@ -646,6 +647,7 @@ bool kvm_arm_get_host_cpu_features(ARMHostCPUFeatures *ahcf)
     }
 
     sve_supported = ioctl(fdarray[0], KVM_CHECK_EXTENSION, KVM_CAP_ARM_SVE) > 0;
+    mte_supported = ioctl(fdarray[0], KVM_CHECK_EXTENSION, KVM_CAP_ARM_MTE) > 0;
 
     kvm_arm_destroy_scratch_host_vcpu(fdarray);
 
@@ -659,6 +661,11 @@ bool kvm_arm_get_host_cpu_features(ARMHostCPUFeatures *ahcf)
         t = FIELD_DP64(t, ID_AA64PFR0, SVE, 1);
         ahcf->isar.id_aa64pfr0 = t;
     }
+    if (mte_supported) {
+        t = ahcf->isar.id_aa64pfr1;
+        t = FIELD_DP64(t, ID_AA64PFR1, MTE, 2);
+        ahcf->isar.id_aa64pfr1 = t;
+    }
 
     /*
      * We can assume any KVM supporting CPU is at least a v8
Signature · 1 line
2.17.1
#7 lore raw

Re: [RFC PATCH v2 4/5] Add migration support for KVM guest with MTE

In reply to Haibo Xu
Quoted 9 lines · * Haibo Xu ([email protected]) wrote:

To make it easier to keep the page tags sync with the page data, tags for one page are appended to the data during ram save iteration.

This patch only add the pre-copy migration support. Post-copy and compress as well as zero page saving are not supported yet.

My guess is that this doesn't work with a lot of other options; e.g. postcopy and probably compression and a bunch of other things. Postcopy I can see you'll need some interesting kernel changes for - you'd need to be able to atomically place a page with it's tag data.

You probably need to add stuff to migrate_caps_check to disable features that you don't support.

Quoted 49 lines
---
 include/hw/arm/virt.h    |  2 +
 include/migration/misc.h |  1 +
 migration/ram.c          | 86 +++++++++++++++++++++++++++++++++++++++-
 3 files changed, 88 insertions(+), 1 deletion(-)
include/hw/arm/virt.h +2 -0
index 921416f918..8b28cde8bf 100644
@@ -166,6 +166,8 @@ struct VirtMachineState {
     PCIBus *bus;
     char *oem_id;
     char *oem_table_id;
+    /* migrate memory tags */
+    NotifierWithReturn precopy_notifier;
 };
 
 #define VIRT_ECAM_ID(high) (high ? VIRT_HIGH_PCIE_ECAM : VIRT_PCIE_ECAM)
include/migration/misc.h +1 -0
index bccc1b6b44..005133f471 100644
@@ -38,6 +38,7 @@ void precopy_add_notifier(NotifierWithReturn *n);
 void precopy_remove_notifier(NotifierWithReturn *n);
 int precopy_notify(PrecopyNotifyReason reason, Error **errp);
 void precopy_enable_free_page_optimization(void);
+void precopy_enable_metadata_migration(void);
 
 void ram_mig_init(void);
 void qemu_guest_free_page_hint(void *addr, size_t len);
migration/ram.c +2 -0
index 72143da0ac..e67b798c3b 100644
@@ -53,10 +53,12 @@
 #include "block.h"
 #include "sysemu/sysemu.h"
 #include "sysemu/cpu-throttle.h"
+#include "sysemu/kvm.h"
 #include "savevm.h"
 #include "qemu/iov.h"
 #include "multifd.h"
 #include "sysemu/runstate.h"
+#include "kvm_arm.h"
 
 #if defined(__linux__)
 #include "qemu/userfaultfd.h"

migration/ram.c line 85

Quoted 4 lines
migration/ram.c +1 -0
@@ -80,6 +82,9 @@
 #define RAM_SAVE_FLAG_XBZRLE   0x40
 /* 0x80 is reserved in migration.h start with 0x100 next */
 #define RAM_SAVE_FLAG_COMPRESS_PAGE    0x100
+#define RAM_SAVE_FLAG_MTE              0x200

I think that's using the last free bit in the flags field, which is a bit painful.

Quoted 30 lines
migration/ram.c +12 -0
+#define MTE_GRANULE_SIZE   (16)
 
 static inline bool is_zero_range(uint8_t *p, uint64_t size)
 {
@@ -317,6 +322,8 @@ struct RAMState {
     bool ram_bulk_stage;
     /* The free page optimization is enabled */
     bool fpo_enabled;
+    /* The RAM meta data(e.t memory tag) is enabled */
+    bool metadata_enabled;
     /* How many times we have dirty too many pages */
     int dirty_rate_high_cnt;
     /* these variables are used for bitmap sync */
@@ -394,6 +401,15 @@ void precopy_enable_free_page_optimization(void)
     ram_state->fpo_enabled = true;
 }
 
+void precopy_enable_metadata_migration(void)
+{
+    if (!ram_state) {
+        return;
+    }
+
+    ram_state->metadata_enabled = true;
+}
+
 uint64_t ram_bytes_remaining(void)
 {
     return ram_state ? (ram_state->migration_dirty_pages * TARGET_PAGE_SIZE) :

migration/ram.c static bool control_save_page(RAMState *rs, RAMBlock *block, ram_addr_t offset, · line 1157

Quoted 8 lines
migration/ram.c +5 -0
@@ -1134,6 +1150,61 @@ static bool control_save_page(RAMState *rs, RAMBlock *block, ram_addr_t offset,
     return true;
 }
 
+static int save_normal_page_mte_tags(QEMUFile *f, uint8_t *addr)
+{
+    uint8_t *tag_buf = NULL;
+    uint64_t ipa;
+    int size = TARGET_PAGE_SIZE / MTE_GRANULE_SIZE;

This function needs to be mostly somewhere aarch specific; or somehow made more generic.
We shouldn't have RAM_SAVE_FLAG_MTE as well - we should be something like RAM_SAVE_FLAG_ARCH_METADATA and that way other architectures with something else glued onto pages can do something similar. Try and keep migration/ architecture independent.

migration/ram.c static bool control_save_page(RAMState *rs, RAMBlock *block, ram_addr_t offset, · line 1161

Quoted 3 lines
migration/ram.c +3 -0
+    if (kvm_physical_memory_addr_from_host(kvm_state, addr, &ipa)) {
+        /* Buffer for the page tags(one byte per tag) */
+        tag_buf = g_try_malloc0(size);

It feels like you want to allocate tag_buf in setup and free it at the end rather than doing this in every page.

migration/ram.c static bool control_save_page(RAMState *rs, RAMBlock *block, ram_addr_t offset, · line 1168

Quoted 7 lines
migration/ram.c +7 -0
+        if (!tag_buf) {
+            error_report("%s: Error allocating MTE tag_buf", __func__);
+            return 0;
+        }
+
+        if (kvm_arm_mte_get_tags(ipa, TARGET_PAGE_SIZE, tag_buf) < 0) {
+            error_report("%s: Can't get MTE tags from guest", __func__);

For any error like this you probably want to say the addresses to make it easier to debug when it fails.

Quoted 20 lines
migration/ram.c +20 -0
+            g_free(tag_buf);
+            return 0;
+        }
+
+        qemu_put_buffer(f, tag_buf, size);
+
+        g_free(tag_buf);
+
+        return size;
+    }
+
+    return 0;
+}
+
+static void load_normal_page_mte_tags(QEMUFile *f, uint8_t *addr)
+{
+    uint8_t *tag_buf = NULL;
+    uint64_t ipa;
+    int size = TARGET_PAGE_SIZE / MTE_GRANULE_SIZE;
+

migration/ram.c static bool control_save_page(RAMState *rs, RAMBlock *block, ram_addr_t offset, · line 1198

Quoted 10 lines
migration/ram.c +10 -0
+    if (kvm_physical_memory_addr_from_host(kvm_state, addr, &ipa)) {
+        /* Buffer for the page tags(one byte per tag) */
+        tag_buf = g_try_malloc0(size);
+        if (!tag_buf) {
+            error_report("%s: Error allocating MTE tag_buf", __func__);
+            return;
+        }
+
+        qemu_get_buffer(f, tag_buf, size);
+        if (kvm_arm_mte_set_tags(ipa, TARGET_PAGE_SIZE, tag_buf) < 0) {

what protections are there here to stop you setting the mte on something useful, like part of the host kernel or qemu?

Quoted 65 lines
migration/ram.c +23 -1
+            error_report("%s: Can't set MTE tags to guest", __func__);
+        }
+
+        g_free(tag_buf);
+    }
+
+    return;
+}
+
 /*
  * directly send the page to the stream
  *
@@ -1148,6 +1219,10 @@ static bool control_save_page(RAMState *rs, RAMBlock *block, ram_addr_t offset,
 static int save_normal_page(RAMState *rs, RAMBlock *block, ram_addr_t offset,
                             uint8_t *buf, bool async)
 {
+    if (rs->metadata_enabled) {
+        offset |= RAM_SAVE_FLAG_MTE;
+    }
+
     ram_counters.transferred += save_page_header(rs, rs->f, block,
                                                  offset | RAM_SAVE_FLAG_PAGE);
     if (async) {
@@ -1159,6 +1234,11 @@ static int save_normal_page(RAMState *rs, RAMBlock *block, ram_addr_t offset,
     }
     ram_counters.transferred += TARGET_PAGE_SIZE;
     ram_counters.normal++;
+
+    if (rs->metadata_enabled) {
+        ram_counters.transferred += save_normal_page_mte_tags(rs->f, buf);
+    }
+
     return 1;
 }
 
@@ -2189,6 +2269,7 @@ static void ram_state_reset(RAMState *rs)
     rs->last_version = ram_list.version;
     rs->ram_bulk_stage = true;
     rs->fpo_enabled = false;
+    rs->metadata_enabled = false;
 }
 
 #define MAX_WAIT 50 /* ms, half buffered_file limit */
@@ -3779,7 +3860,7 @@ static int ram_load_precopy(QEMUFile *f)
             trace_ram_load_loop(block->idstr, (uint64_t)addr, flags, host);
         }
 
-        switch (flags & ~RAM_SAVE_FLAG_CONTINUE) {
+        switch (flags & ~(RAM_SAVE_FLAG_CONTINUE | RAM_SAVE_FLAG_MTE)) {
         case RAM_SAVE_FLAG_MEM_SIZE:
             /* Synchronize RAM block list */
             total_ram_bytes = addr;
@@ -3849,6 +3930,9 @@ static int ram_load_precopy(QEMUFile *f)
 
         case RAM_SAVE_FLAG_PAGE:
             qemu_get_buffer(f, host, TARGET_PAGE_SIZE);
+            if (flags & RAM_SAVE_FLAG_MTE) {
+                load_normal_page_mte_tags(f, host);
+            }
             break;
 
         case RAM_SAVE_FLAG_COMPRESS_PAGE:
Signature · 1 line
2.17.1
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
author
#8 lore raw

Re: [RFC PATCH v2 4/5] Add migration support for KVM guest with MTE

Quoted 12 lines · Dr. David Alan Gilbert
Quoted 9 lines · * Haibo Xu ([email protected]) wrote:

To make it easier to keep the page tags sync with the page data, tags for one page are appended to the data during ram save iteration.

This patch only add the pre-copy migration support. Post-copy and compress as well as zero page saving are not supported yet.

Quoted 8 lines

My guess is that this doesn't work with a lot of other options; e.g. postcopy and probably compression and a bunch of other things. Postcopy I can see you'll need some interesting kernel changes for - you'd need to be able to atomically place a page with it's tag data.

You probably need to add stuff to migrate_caps_check to disable features that you don't support.

Hi David,

Thanks so much for the comments!

You are right, this RFC patch only supports pre-copy mode, no postcopy, no compression.
As a RFC, here just want to finalize the tag migration process, that is:
1. let the tag go with the page data(the current choice) which may be a little complex to put
them into the current migration process.
2. migrate them separately which is easy to implement with the current migration(treat the tags
as device status), but it would be hard to keep the page data and
tag to sync with each other.
3. Any other ways?

Once the tag migration process is finalized, a new formal patch series with postcopy as well as
compression should be reworked.

What's more, you mentioned that "some interesting kernel changes are needed to atomically
place a page with it's tag data". You mean a single kernel API to store page data and tag in
the migration load process?

Regards,
Haibo

Quoted 46 lines
Quoted 46 lines
---
 include/hw/arm/virt.h    |  2 +
 include/migration/misc.h |  1 +
 migration/ram.c          | 86 +++++++++++++++++++++++++++++++++++++++-
 3 files changed, 88 insertions(+), 1 deletion(-)
include/hw/arm/virt.h +2 -0
index 921416f918..8b28cde8bf 100644
@@ -166,6 +166,8 @@ struct VirtMachineState {
     PCIBus *bus;
     char *oem_id;
     char *oem_table_id;
+    /* migrate memory tags */
+    NotifierWithReturn precopy_notifier;
 };
 
 #define VIRT_ECAM_ID(high) (high ? VIRT_HIGH_PCIE_ECAM : VIRT_PCIE_ECAM)
include/migration/misc.h +1 -0
index bccc1b6b44..005133f471 100644
@@ -38,6 +38,7 @@ void precopy_add_notifier(NotifierWithReturn *n);
 void precopy_remove_notifier(NotifierWithReturn *n);
 int precopy_notify(PrecopyNotifyReason reason, Error **errp);
 void precopy_enable_free_page_optimization(void);
+void precopy_enable_metadata_migration(void);
 
 void ram_mig_init(void);
 void qemu_guest_free_page_hint(void *addr, size_t len);
migration/ram.c +2 -0
index 72143da0ac..e67b798c3b 100644
@@ -53,10 +53,12 @@
 #include "block.h"
 #include "sysemu/sysemu.h"
 #include "sysemu/cpu-throttle.h"
+#include "sysemu/kvm.h"
 #include "savevm.h"
 #include "qemu/iov.h"
 #include "multifd.h"
 #include "sysemu/runstate.h"
+#include "kvm_arm.h"
 
Quoted 11 lines

migration/ram.c line 85

Quoted 7 lines
migration/ram.c +1 -0
 #if defined(__linux__)
 #include "qemu/userfaultfd.h"
@@ -80,6 +82,9 @@
 #define RAM_SAVE_FLAG_XBZRLE   0x40
 /* 0x80 is reserved in migration.h start with 0x100 next */
 #define RAM_SAVE_FLAG_COMPRESS_PAGE    0x100
+#define RAM_SAVE_FLAG_MTE              0x200

I think that's using the last free bit in the flags field, which is a bit painful.

Yes, only 0x80(reserved) and 0x200 are available.

Quoted 33 lines
Quoted 33 lines
migration/ram.c +12 -0
+#define MTE_GRANULE_SIZE   (16)
 
 static inline bool is_zero_range(uint8_t *p, uint64_t size)
 {
@@ -317,6 +322,8 @@ struct RAMState {
     bool ram_bulk_stage;
     /* The free page optimization is enabled */
     bool fpo_enabled;
+    /* The RAM meta data(e.t memory tag) is enabled */
+    bool metadata_enabled;
     /* How many times we have dirty too many pages */
     int dirty_rate_high_cnt;
     /* these variables are used for bitmap sync */
@@ -394,6 +401,15 @@ void precopy_enable_free_page_optimization(void)
     ram_state->fpo_enabled = true;
 }
 
+void precopy_enable_metadata_migration(void)
+{
+    if (!ram_state) {
+        return;
+    }
+
+    ram_state->metadata_enabled = true;
+}
+
 uint64_t ram_bytes_remaining(void)
 {
     return ram_state ? (ram_state->migration_dirty_pages * TARGET_PAGE_SIZE) :
@@ -1134,6 +1150,61 @@ static bool control_save_page(RAMState *rs, RAMBlock *block, ram_addr_t offset,
     return true;
 }
 
Quoted 12 lines

migration/ram.c static bool control_save_page(RAMState *rs, RAMBlock *block, ram_addr_t offset, · line 1157

Quoted 5 lines
migration/ram.c +5 -0
+static int save_normal_page_mte_tags(QEMUFile *f, uint8_t *addr)
+{
+    uint8_t *tag_buf = NULL;
+    uint64_t ipa;
+    int size = TARGET_PAGE_SIZE / MTE_GRANULE_SIZE;

This function needs to be mostly somewhere aarch specific; or somehow made more generic.
We shouldn't have RAM_SAVE_FLAG_MTE as well - we should be something like RAM_SAVE_FLAG_ARCH_METADATA and that way other architectures with something else glued onto pages can do something similar. Try and keep migration/ architecture independent.

Yes, it should be arch independent and handled with a more abstract way.

Quoted 7 lines

migration/ram.c static bool control_save_page(RAMState *rs, RAMBlock *block, ram_addr_t offset, · line 1161

Quoted 4 lines
migration/ram.c +3 -0
+    if (kvm_physical_memory_addr_from_host(kvm_state, addr, &ipa)) {
+        /* Buffer for the page tags(one byte per tag) */
+        tag_buf = g_try_malloc0(size);

It feels like you want to allocate tag_buf in setup and free it at the end rather than doing this in every page.

Yes, the tag buffer allocation could be moved to migration save/load setup and cleanup function.

Quoted 9 lines

migration/ram.c static bool control_save_page(RAMState *rs, RAMBlock *block, ram_addr_t offset, · line 1168

Quoted 8 lines
migration/ram.c +7 -0
+        if (!tag_buf) {
+            error_report("%s: Error allocating MTE tag_buf", __func__);
+            return 0;
+        }
+
+        if (kvm_arm_mte_get_tags(ipa, TARGET_PAGE_SIZE, tag_buf) < 0) {
+            error_report("%s: Can't get MTE tags from guest", __func__);
Quoted 3 lines

For any error like this you probably want to say the addresses to make it easier to debug when it fails.

Good suggestion! Will add the "address" info to the log.

Quoted 25 lines
Quoted 25 lines
migration/ram.c +25 -0
+            g_free(tag_buf);
+            return 0;
+        }
+
+        qemu_put_buffer(f, tag_buf, size);
+
+        g_free(tag_buf);
+
+        return size;
+    }
+
+    return 0;
+}
+
+static void load_normal_page_mte_tags(QEMUFile *f, uint8_t *addr)
+{
+    uint8_t *tag_buf = NULL;
+    uint64_t ipa;
+    int size = TARGET_PAGE_SIZE / MTE_GRANULE_SIZE;
+
+    if (kvm_physical_memory_addr_from_host(kvm_state, addr, &ipa)) {
+        /* Buffer for the page tags(one byte per tag) */
+        tag_buf = g_try_malloc0(size);
+        if (!tag_buf) {
+            error_report("%s: Error allocating MTE tag_buf", __func__);
Quoted 8 lines

migration/ram.c static bool control_save_page(RAMState *rs, RAMBlock *block, ram_addr_t offset, · line 1198

Quoted 5 lines
migration/ram.c +5 -0
+            return;
+        }
+
+        qemu_get_buffer(f, tag_buf, size);
+        if (kvm_arm_mte_set_tags(ipa, TARGET_PAGE_SIZE, tag_buf) < 0) {

what protections are there here to stop you setting the mte on something useful, like part of the host kernel or qemu?

Kernel would do some parameter check(e.t ipa within a valid slot) before setting the tag data.

Quoted 69 lines
Quoted 64 lines
migration/ram.c +23 -2
+            error_report("%s: Can't set MTE tags to guest", __func__);
+        }
+
+        g_free(tag_buf);
+    }
+
+    return;
+}
+
 /*
  * directly send the page to the stream
  *
@@ -1148,6 +1219,10 @@ static bool control_save_page(RAMState *rs, RAMBlock *block, ram_addr_t offset,
 static int save_normal_page(RAMState *rs, RAMBlock *block, ram_addr_t offset,
                             uint8_t *buf, bool async)
 {
+    if (rs->metadata_enabled) {
+        offset |= RAM_SAVE_FLAG_MTE;
+    }
+
     ram_counters.transferred += save_page_header(rs, rs->f, block,
                                                  offset | RAM_SAVE_FLAG_PAGE);
     if (async) {
@@ -1159,6 +1234,11 @@ static int save_normal_page(RAMState *rs, RAMBlock *block, ram_addr_t offset,
     }
     ram_counters.transferred += TARGET_PAGE_SIZE;
     ram_counters.normal++;
+
+    if (rs->metadata_enabled) {
+        ram_counters.transferred += save_normal_page_mte_tags(rs->f, buf);
+    }
+
     return 1;
 }
 
@@ -2189,6 +2269,7 @@ static void ram_state_reset(RAMState *rs)
     rs->last_version = ram_list.version;
     rs->ram_bulk_stage = true;
     rs->fpo_enabled = false;
+    rs->metadata_enabled = false;
 }
 
 #define MAX_WAIT 50 /* ms, half buffered_file limit */
@@ -3779,7 +3860,7 @@ static int ram_load_precopy(QEMUFile *f)
             trace_ram_load_loop(block->idstr, (uint64_t)addr, flags, host);
         }
 
-        switch (flags & ~RAM_SAVE_FLAG_CONTINUE) {
+        switch (flags & ~(RAM_SAVE_FLAG_CONTINUE | RAM_SAVE_FLAG_MTE)) {
         case RAM_SAVE_FLAG_MEM_SIZE:
             /* Synchronize RAM block list */
             total_ram_bytes = addr;
@@ -3849,6 +3930,9 @@ static int ram_load_precopy(QEMUFile *f)
 
         case RAM_SAVE_FLAG_PAGE:
             qemu_get_buffer(f, host, TARGET_PAGE_SIZE);
+            if (flags & RAM_SAVE_FLAG_MTE) {
+                load_normal_page_mte_tags(f, host);
+            }
             break;
 
         case RAM_SAVE_FLAG_COMPRESS_PAGE:
--
Quoted 2 lines

2.17.1

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

#4 lore raw

Re: [RFC PATCH v2 3/5] Add APIs to get/set MTE tags

In reply to Haibo Xu
Quoted 25 lines · Haibo Xu

MTE spec provide instructions to retrieve the memory tags:
(1) LDG, at 16 bytes granularity, and available in both user

    and kernel space;

(2) LDGM, at 256 bytes granularity in maximum, and only available in kernel space

To improve the performance, KVM has exposed the LDGM capability to user space by providing a new APIs. This patch is just a wrapper for the KVM APIs.

---
 target/arm/kvm64.c   | 24 ++++++++++++++++++++++++
 target/arm/kvm_arm.h |  2 ++
 2 files changed, 26 insertions(+)
target/arm/kvm64.c +1 -0
index 73a191f8e1..3157025316 100644
@@ -1606,3 +1606,27 @@ bool kvm_arm_verify_ext_dabt_pending(CPUState *cs)
     }
     return false;
 }
+

target/arm/kvm64.c bool kvm_arm_verify_ext_dabt_pending(CPUState *cs) · line 1619

Quoted 10 lines
target/arm/kvm64.c +10 -0
+int kvm_arm_mte_get_tags(uint64_t ipa, uint64_t len, uint8_t *buf)
+{
+    struct kvm_arm_copy_mte_tags args = {
+        .guest_ipa = ipa,
+        .length = len,
+        .addr = buf,
+        .flags = KVM_ARM_TAGS_FROM_GUEST,
+    };
+
+    return kvm_vm_ioctl(kvm_state, KVM_ARM_MTE_COPY_TAGS, &args);

Just a question, how fast/slow are this calls?

My understanding is that we are making a kvm call for each page that we want to migrate, right?

Each time that we want to send it.

Later, Juan.

Quoted 26 lines
target/arm/kvm64.c +13 -0
+}
+
+int kvm_arm_mte_set_tags(uint64_t ipa, uint64_t len, uint8_t *buf)
+{
+    struct kvm_arm_copy_mte_tags args = {
+        .guest_ipa = ipa,
+        .length = len,
+        .addr = buf,
+        .flags = KVM_ARM_TAGS_TO_GUEST,
+    };
+
+    return kvm_vm_ioctl(kvm_state, KVM_ARM_MTE_COPY_TAGS, &args);
+}
target/arm/kvm_arm.h +2 -0
index 34f8daa377..bbb833d6c6 100644
@@ -360,6 +360,8 @@ int kvm_arm_vgic_probe(void);
 
 void kvm_arm_pmu_set_irq(CPUState *cs, int irq);
 void kvm_arm_pmu_init(CPUState *cs);
+int kvm_arm_mte_get_tags(uint64_t ipa, uint64_t len, uint8_t *buf);
+int kvm_arm_mte_set_tags(uint64_t ipa, uint64_t len, uint8_t *buf);
 
 /**
  * kvm_arm_pvtime_init:
#14 lore raw

Re: [RFC PATCH v2 5/5] Enable the MTE support for KVM guest

In reply to Haibo Xu
Quoted 42 lines · Haibo Xu
---
 hw/arm/virt.c | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)
hw/arm/virt.c +16 -0
index 76658b93a3..36cfdb29e9 100644
@@ -79,6 +79,7 @@
 #include "hw/virtio/virtio-iommu.h"
 #include "hw/char/pl011.h"
 #include "qemu/guest-random.h"
+#include "migration/misc.h"
 
 #define DEFINE_VIRT_MACHINE_LATEST(major, minor, latest) \
     static void virt_##major##_##minor##_class_init(ObjectClass *oc, \
@@ -828,6 +829,21 @@ static void virt_powerdown_req(Notifier *n, void *opaque)
     }
 }
 
+static int virt_precopy_notify(NotifierWithReturn *n, void *data)
+{
+    PrecopyNotifyData *pnd = data;
+
+    switch (pnd->reason) {
+    case PRECOPY_NOTIFY_SETUP:
+        precopy_enable_metadata_migration();
+        break;
+    default:
+        break;
+    }
+
+    return 0;
+}
+
 static void create_gpio_keys(char *fdt, DeviceState *pl061_dev,
                              uint32_t phandle)
 {
@@ -1912,9 +1928,9 @@ static void machvirt_init(MachineState *machine)
     }
 

hw/arm/virt.c static void machvirt_init(MachineState *machine) · line 1936

Quoted 10 lines
hw/arm/virt.c +3 -3
     if (vms->mte && kvm_enabled()) {
-        error_report("mach-virt: KVM does not support providing "
-                     "MTE to the guest CPU");
-        exit(1);
+        /* connect migration precopy request */
+        vms->precopy_notifier.notify = virt_precopy_notify;
+        precopy_add_notifier(&vms->precopy_notifier);
     }
 
     create_fdt(vms);

Why are you using a notifier here?
It is not enough for you just esetup at this point a variable somewhere
"foo->mte_enabled = true"

And create a function that just does:

bool is_mte_enabled(vode)

{
        return foo->mte_enabled;
}

And just check that everywhere?

Later, Juan.

#11 lore raw

Re: [RFC PATCH v2 4/5] Add migration support for KVM guest with MTE

In reply to Haibo Xu
Quoted 8 lines · Haibo Xu

To make it easier to keep the page tags sync with the page data, tags for one page are appended to the data during ram save iteration.

This patch only add the pre-copy migration support. Post-copy and compress as well as zero page saving are not supported yet.

Quoted 7 lines

#define RAM_SAVE_FLAG_XBZRLE 0x40
/* 0x80 is reserved in migration.h start with 0x100 next */
#define RAM_SAVE_FLAG_COMPRESS_PAGE 0x100
+#define RAM_SAVE_FLAG_MTE 0x200

Flags are really a scarce resource. You are using one here, when you know that you will always have the feature enable (or not), so you can do better during negotiation IMHO.

migration/ram.c void precopy_enable_free_page_optimization(void) · line 411

Quoted 8 lines
migration/ram.c +8 -0
+void precopy_enable_metadata_migration(void)
+{
+    if (!ram_state) {
+        return;
+    }
+
+    ram_state->metadata_enabled = true;
+}

My understanding is that in your following patch, if mte is enabled, you will always sent mte tags, for all pages needed, right?

migration/ram.c static bool control_save_page(RAMState *rs, RAMBlock *block, ram_addr_t offset, · line 1161

Quoted 9 lines
migration/ram.c +9 -0
+static int save_normal_page_mte_tags(QEMUFile *f, uint8_t *addr)
+{
+    uint8_t *tag_buf = NULL;
+    uint64_t ipa;
+    int size = TARGET_PAGE_SIZE / MTE_GRANULE_SIZE;
+
+    if (kvm_physical_memory_addr_from_host(kvm_state, addr, &ipa)) {
+        /* Buffer for the page tags(one byte per tag) */
+        tag_buf = g_try_malloc0(size);

size of the buffer is known at start of migration. Just get a buffer and reuse it?

Do zero pages have mte tags? From migration point of view, a zero page is a page that is just full of zeros, i.e. nothing else special. Because you are not sending any for them.

migration/ram.c static bool control_save_page(RAMState *rs, RAMBlock *block, ram_addr_t offset, · line 1223

Quoted 6 lines
migration/ram.c +2 -0
@@ -1148,6 +1219,10 @@ static bool control_save_page(RAMState *rs, RAMBlock *block, ram_addr_t offset,
 static int save_normal_page(RAMState *rs, RAMBlock *block, ram_addr_t offset,
                             uint8_t *buf, bool async)
 {
+    if (rs->metadata_enabled) {
+        offset |= RAM_SAVE_FLAG_MTE;

You don't really need the flag, for you normal pages are just TARGET_PAGE_SIZE + (TARGET_PAGE_SIZE/MTE_)

migration/ram.c static int save_normal_page(RAMState *rs, RAMBlock *block, ram_addr_t offset, · line 1238

Quoted 11 lines
migration/ram.c +4 -0
+    }
+
     ram_counters.transferred += save_page_header(rs, rs->f, block,
                                                  offset | RAM_SAVE_FLAG_PAGE);
     if (async) {
@@ -1159,6 +1234,11 @@ static int save_normal_page(RAMState *rs, RAMBlock *block, ram_addr_t offset,
     }
     ram_counters.transferred += TARGET_PAGE_SIZE;
     ram_counters.normal++;
+
+    if (rs->metadata_enabled) {

See? You are not checking the flag, you are checking the bool setup at the beggining of migration.

Quoted 15 lines
migration/ram.c +4 -0
+        ram_counters.transferred += save_normal_page_mte_tags(rs->f, buf);
+    }
+
     return 1;
 }
 
@@ -2189,6 +2269,7 @@ static void ram_state_reset(RAMState *rs)
     rs->last_version = ram_list.version;
     rs->ram_bulk_stage = true;
     rs->fpo_enabled = false;
+    rs->metadata_enabled = false;
 }
 
 #define MAX_WAIT 50 /* ms, half buffered_file limit */

migration/ram.c static int ram_load_precopy(QEMUFile *f) · line 3863

Quoted 5 lines
migration/ram.c +1 -1
@@ -3779,7 +3860,7 @@ static int ram_load_precopy(QEMUFile *f)
             trace_ram_load_loop(block->idstr, (uint64_t)addr, flags, host);
         }
 
-        switch (flags & ~RAM_SAVE_FLAG_CONTINUE) {
+        switch (flags & ~(RAM_SAVE_FLAG_CONTINUE | RAM_SAVE_FLAG_MTE)) {

Creating the flag is hurting you here also.

migration/ram.c static int ram_load_precopy(QEMUFile *f) · line 3935

Quoted 10 lines
migration/ram.c +3 -0
         case RAM_SAVE_FLAG_MEM_SIZE:
             /* Synchronize RAM block list */
             total_ram_bytes = addr;
@@ -3849,6 +3930,9 @@ static int ram_load_precopy(QEMUFile *f)
 
         case RAM_SAVE_FLAG_PAGE:
             qemu_get_buffer(f, host, TARGET_PAGE_SIZE);
+            if (flags & RAM_SAVE_FLAG_MTE) {
+                load_normal_page_mte_tags(f, host);
+            }

I don't claim to understand the MTE, but my understanding is that if we are using MTE, all pages have to have MTE flags, right?

So, somtehing like

is_mte_enabled()

that I told in the other thread looks like a good idea.

Later, Juan.

Quoted 3 lines
migration/ram.c +0 -0
             break;
 
         case RAM_SAVE_FLAG_COMPRESS_PAGE:
#9 lore raw

Re: [RFC PATCH v2 4/5] Add migration support for KVM guest with MTE

In reply to Haibo Xu
Quoted 25 lines · * Haibo Xu ([email protected]) wrote:
Quoted 12 lines · Dr. David Alan Gilbert
Quoted 9 lines · * Haibo Xu ([email protected]) wrote:

To make it easier to keep the page tags sync with the page data, tags for one page are appended to the data during ram save iteration.

This patch only add the pre-copy migration support. Post-copy and compress as well as zero page saving are not supported yet.

Quoted 8 lines

My guess is that this doesn't work with a lot of other options; e.g. postcopy and probably compression and a bunch of other things. Postcopy I can see you'll need some interesting kernel changes for - you'd need to be able to atomically place a page with it's tag data.

You probably need to add stuff to migrate_caps_check to disable features that you don't support.

Hi David,

Quoted 8 lines

Thanks so much for the comments!

You are right, this RFC patch only supports pre-copy mode, no postcopy, no compression.
As a RFC, here just want to finalize the tag migration process, that is:
1. let the tag go with the page data(the current choice) which may be a little complex to put
them into the current migration process.

I think it's probably the easiest.

Quoted 9 lines

2. migrate them separately which is easy to implement with the current migration(treat the tags
as device status), but it would be hard to keep the page data and
tag to sync with each other.
3. Any other ways?

Once the tag migration process is finalized, a new formal patch series with postcopy as well as
compression should be reworked.

It's probably best to look what you would have to do to fit it in with those before finalising the basic version; not necessary implement them
- just get an idea.

Quoted 5 lines

What's more, you mentioned that "some interesting kernel changes are needed to atomically
place a page with it's tag data". You mean a single kernel API to store page data and tag in
the migration load process?

Yes; see migration/postcopy-ram.c postcopy_place_page - it has the problem of placing the whole page in memory before the guest starts usign it; I think for MTE you're going to have to put the MTE data in at the same time.

Dave

Quoted 245 lines

Regards,
Haibo

Quoted 46 lines
Quoted 46 lines
---
 include/hw/arm/virt.h    |  2 +
 include/migration/misc.h |  1 +
 migration/ram.c          | 86 +++++++++++++++++++++++++++++++++++++++-
 3 files changed, 88 insertions(+), 1 deletion(-)
include/hw/arm/virt.h +2 -0
index 921416f918..8b28cde8bf 100644
@@ -166,6 +166,8 @@ struct VirtMachineState {
     PCIBus *bus;
     char *oem_id;
     char *oem_table_id;
+    /* migrate memory tags */
+    NotifierWithReturn precopy_notifier;
 };
 
 #define VIRT_ECAM_ID(high) (high ? VIRT_HIGH_PCIE_ECAM : VIRT_PCIE_ECAM)
include/migration/misc.h +1 -0
index bccc1b6b44..005133f471 100644
@@ -38,6 +38,7 @@ void precopy_add_notifier(NotifierWithReturn *n);
 void precopy_remove_notifier(NotifierWithReturn *n);
 int precopy_notify(PrecopyNotifyReason reason, Error **errp);
 void precopy_enable_free_page_optimization(void);
+void precopy_enable_metadata_migration(void);
 
 void ram_mig_init(void);
 void qemu_guest_free_page_hint(void *addr, size_t len);
migration/ram.c +2 -0
index 72143da0ac..e67b798c3b 100644
@@ -53,10 +53,12 @@
 #include "block.h"
 #include "sysemu/sysemu.h"
 #include "sysemu/cpu-throttle.h"
+#include "sysemu/kvm.h"
 #include "savevm.h"
 #include "qemu/iov.h"
 #include "multifd.h"
 #include "sysemu/runstate.h"
+#include "kvm_arm.h"
 
Quoted 11 lines

migration/ram.c line 85

Quoted 7 lines
migration/ram.c +1 -0
 #if defined(__linux__)
 #include "qemu/userfaultfd.h"
@@ -80,6 +82,9 @@
 #define RAM_SAVE_FLAG_XBZRLE   0x40
 /* 0x80 is reserved in migration.h start with 0x100 next */
 #define RAM_SAVE_FLAG_COMPRESS_PAGE    0x100
+#define RAM_SAVE_FLAG_MTE              0x200

I think that's using the last free bit in the flags field, which is a bit painful.

Yes, only 0x80(reserved) and 0x200 are available.

Quoted 33 lines
Quoted 33 lines
migration/ram.c +12 -0
+#define MTE_GRANULE_SIZE   (16)
 
 static inline bool is_zero_range(uint8_t *p, uint64_t size)
 {
@@ -317,6 +322,8 @@ struct RAMState {
     bool ram_bulk_stage;
     /* The free page optimization is enabled */
     bool fpo_enabled;
+    /* The RAM meta data(e.t memory tag) is enabled */
+    bool metadata_enabled;
     /* How many times we have dirty too many pages */
     int dirty_rate_high_cnt;
     /* these variables are used for bitmap sync */
@@ -394,6 +401,15 @@ void precopy_enable_free_page_optimization(void)
     ram_state->fpo_enabled = true;
 }
 
+void precopy_enable_metadata_migration(void)
+{
+    if (!ram_state) {
+        return;
+    }
+
+    ram_state->metadata_enabled = true;
+}
+
 uint64_t ram_bytes_remaining(void)
 {
     return ram_state ? (ram_state->migration_dirty_pages * TARGET_PAGE_SIZE) :
@@ -1134,6 +1150,61 @@ static bool control_save_page(RAMState *rs, RAMBlock *block, ram_addr_t offset,
     return true;
 }
 
Quoted 12 lines

migration/ram.c static bool control_save_page(RAMState *rs, RAMBlock *block, ram_addr_t offset, · line 1157

Quoted 5 lines
migration/ram.c +5 -0
+static int save_normal_page_mte_tags(QEMUFile *f, uint8_t *addr)
+{
+    uint8_t *tag_buf = NULL;
+    uint64_t ipa;
+    int size = TARGET_PAGE_SIZE / MTE_GRANULE_SIZE;

This function needs to be mostly somewhere aarch specific; or somehow made more generic.
We shouldn't have RAM_SAVE_FLAG_MTE as well - we should be something like RAM_SAVE_FLAG_ARCH_METADATA and that way other architectures with something else glued onto pages can do something similar. Try and keep migration/ architecture independent.

Yes, it should be arch independent and handled with a more abstract way.

Quoted 7 lines

migration/ram.c static bool control_save_page(RAMState *rs, RAMBlock *block, ram_addr_t offset, · line 1161

Quoted 4 lines
migration/ram.c +3 -0
+    if (kvm_physical_memory_addr_from_host(kvm_state, addr, &ipa)) {
+        /* Buffer for the page tags(one byte per tag) */
+        tag_buf = g_try_malloc0(size);

It feels like you want to allocate tag_buf in setup and free it at the end rather than doing this in every page.

Yes, the tag buffer allocation could be moved to migration save/load setup and cleanup function.

Quoted 9 lines

migration/ram.c static bool control_save_page(RAMState *rs, RAMBlock *block, ram_addr_t offset, · line 1168

Quoted 8 lines
migration/ram.c +7 -0
+        if (!tag_buf) {
+            error_report("%s: Error allocating MTE tag_buf", __func__);
+            return 0;
+        }
+
+        if (kvm_arm_mte_get_tags(ipa, TARGET_PAGE_SIZE, tag_buf) < 0) {
+            error_report("%s: Can't get MTE tags from guest", __func__);
Quoted 3 lines

For any error like this you probably want to say the addresses to make it easier to debug when it fails.

Good suggestion! Will add the "address" info to the log.

Quoted 25 lines
Quoted 25 lines
migration/ram.c +25 -0
+            g_free(tag_buf);
+            return 0;
+        }
+
+        qemu_put_buffer(f, tag_buf, size);
+
+        g_free(tag_buf);
+
+        return size;
+    }
+
+    return 0;
+}
+
+static void load_normal_page_mte_tags(QEMUFile *f, uint8_t *addr)
+{
+    uint8_t *tag_buf = NULL;
+    uint64_t ipa;
+    int size = TARGET_PAGE_SIZE / MTE_GRANULE_SIZE;
+
+    if (kvm_physical_memory_addr_from_host(kvm_state, addr, &ipa)) {
+        /* Buffer for the page tags(one byte per tag) */
+        tag_buf = g_try_malloc0(size);
+        if (!tag_buf) {
+            error_report("%s: Error allocating MTE tag_buf", __func__);
Quoted 8 lines

migration/ram.c static bool control_save_page(RAMState *rs, RAMBlock *block, ram_addr_t offset, · line 1198

Quoted 5 lines
migration/ram.c +5 -0
+            return;
+        }
+
+        qemu_get_buffer(f, tag_buf, size);
+        if (kvm_arm_mte_set_tags(ipa, TARGET_PAGE_SIZE, tag_buf) < 0) {

what protections are there here to stop you setting the mte on something useful, like part of the host kernel or qemu?

Kernel would do some parameter check(e.t ipa within a valid slot) before setting the tag data.

Quoted 69 lines
Quoted 64 lines
migration/ram.c +23 -2
+            error_report("%s: Can't set MTE tags to guest", __func__);
+        }
+
+        g_free(tag_buf);
+    }
+
+    return;
+}
+
 /*
  * directly send the page to the stream
  *
@@ -1148,6 +1219,10 @@ static bool control_save_page(RAMState *rs, RAMBlock *block, ram_addr_t offset,
 static int save_normal_page(RAMState *rs, RAMBlock *block, ram_addr_t offset,
                             uint8_t *buf, bool async)
 {
+    if (rs->metadata_enabled) {
+        offset |= RAM_SAVE_FLAG_MTE;
+    }
+
     ram_counters.transferred += save_page_header(rs, rs->f, block,
                                                  offset | RAM_SAVE_FLAG_PAGE);
     if (async) {
@@ -1159,6 +1234,11 @@ static int save_normal_page(RAMState *rs, RAMBlock *block, ram_addr_t offset,
     }
     ram_counters.transferred += TARGET_PAGE_SIZE;
     ram_counters.normal++;
+
+    if (rs->metadata_enabled) {
+        ram_counters.transferred += save_normal_page_mte_tags(rs->f, buf);
+    }
+
     return 1;
 }
 
@@ -2189,6 +2269,7 @@ static void ram_state_reset(RAMState *rs)
     rs->last_version = ram_list.version;
     rs->ram_bulk_stage = true;
     rs->fpo_enabled = false;
+    rs->metadata_enabled = false;
 }
 
 #define MAX_WAIT 50 /* ms, half buffered_file limit */
@@ -3779,7 +3860,7 @@ static int ram_load_precopy(QEMUFile *f)
             trace_ram_load_loop(block->idstr, (uint64_t)addr, flags, host);
         }
 
-        switch (flags & ~RAM_SAVE_FLAG_CONTINUE) {
+        switch (flags & ~(RAM_SAVE_FLAG_CONTINUE | RAM_SAVE_FLAG_MTE)) {
         case RAM_SAVE_FLAG_MEM_SIZE:
             /* Synchronize RAM block list */
             total_ram_bytes = addr;
@@ -3849,6 +3930,9 @@ static int ram_load_precopy(QEMUFile *f)
 
         case RAM_SAVE_FLAG_PAGE:
             qemu_get_buffer(f, host, TARGET_PAGE_SIZE);
+            if (flags & RAM_SAVE_FLAG_MTE) {
+                load_normal_page_mte_tags(f, host);
+            }
             break;
 
         case RAM_SAVE_FLAG_COMPRESS_PAGE:
--
Quoted 2 lines

2.17.1

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

Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
author
#5 lore raw

Re: [RFC PATCH v2 3/5] Add APIs to get/set MTE tags

In reply to Juan Quintela
Quoted 31 lines · Juan Quintela
Quoted 29 lines · Haibo Xu

MTE spec provide instructions to retrieve the memory tags:
(1) LDG, at 16 bytes granularity, and available in both user

    and kernel space;

(2) LDGM, at 256 bytes granularity in maximum, and only available in kernel space

To improve the performance, KVM has exposed the LDGM capability to user space by providing a new APIs. This patch is just a wrapper for the KVM APIs.

---
 target/arm/kvm64.c   | 24 ++++++++++++++++++++++++
 target/arm/kvm_arm.h |  2 ++
 2 files changed, 26 insertions(+)
target/arm/kvm64.c +5 -0
index 73a191f8e1..3157025316 100644
@@ -1606,3 +1606,27 @@ bool kvm_arm_verify_ext_dabt_pending(CPUState *cs)
     }
     return false;
 }
+
+int kvm_arm_mte_get_tags(uint64_t ipa, uint64_t len, uint8_t *buf)
+{
+    struct kvm_arm_copy_mte_tags args = {
+        .guest_ipa = ipa,
Quoted 9 lines

target/arm/kvm64.c bool kvm_arm_verify_ext_dabt_pending(CPUState *cs) · line 1619

Quoted 6 lines
target/arm/kvm64.c +6 -0
+        .length = len,
+        .addr = buf,
+        .flags = KVM_ARM_TAGS_FROM_GUEST,
+    };
+
+    return kvm_vm_ioctl(kvm_state, KVM_ARM_MTE_COPY_TAGS, &args);

Just a question, how fast/slow are this calls?

There is no performance data for this API yet, but at least it's more efficient than that
to only be able to access a single tag by the EL0 "LDG" instruction. We will try to
collect some performance data for this KVM API later.

Quoted 3 lines

My understanding is that we are making a kvm call for each page that we want to migrate, right?

Yes, currently I chose to append the tag values to the page data during the migration.

Quoted 32 lines

Each time that we want to send it.

Later, Juan.

Quoted 26 lines
target/arm/kvm64.c +13 -0
+}
+
+int kvm_arm_mte_set_tags(uint64_t ipa, uint64_t len, uint8_t *buf)
+{
+    struct kvm_arm_copy_mte_tags args = {
+        .guest_ipa = ipa,
+        .length = len,
+        .addr = buf,
+        .flags = KVM_ARM_TAGS_TO_GUEST,
+    };
+
+    return kvm_vm_ioctl(kvm_state, KVM_ARM_MTE_COPY_TAGS, &args);
+}
target/arm/kvm_arm.h +2 -0
index 34f8daa377..bbb833d6c6 100644
@@ -360,6 +360,8 @@ int kvm_arm_vgic_probe(void);
 
 void kvm_arm_pmu_set_irq(CPUState *cs, int irq);
 void kvm_arm_pmu_init(CPUState *cs);
+int kvm_arm_mte_get_tags(uint64_t ipa, uint64_t len, uint8_t *buf);
+int kvm_arm_mte_set_tags(uint64_t ipa, uint64_t len, uint8_t *buf);
 
 /**
  * kvm_arm_pvtime_init:
author
#12 lore raw

Re: [RFC PATCH v2 4/5] Add migration support for KVM guest with MTE

In reply to Juan Quintela
Quoted 13 lines · Juan Quintela
Quoted 9 lines · Haibo Xu

To make it easier to keep the page tags sync with the page data, tags for one page are appended to the data during ram save iteration.

This patch only add the pre-copy migration support. Post-copy and compress as well as zero page saving are not supported yet.

Quoted 9 lines
Quoted 4 lines

#define RAM_SAVE_FLAG_XBZRLE 0x40
/* 0x80 is reserved in migration.h start with 0x100 next */
#define RAM_SAVE_FLAG_COMPRESS_PAGE 0x100
+#define RAM_SAVE_FLAG_MTE 0x200

Flags are really a scarce resource. You are using one here, when you know that you will always have the feature enable (or not), so you can do better during negotiation IMHO.

Yes, any suggestions are welcomed to finalize the MTE migration support!

Quoted 12 lines

migration/ram.c void precopy_enable_free_page_optimization(void) · line 411

Quoted 9 lines
migration/ram.c +8 -0
+void precopy_enable_metadata_migration(void)
+{
+    if (!ram_state) {
+        return;
+    }
+
+    ram_state->metadata_enabled = true;
+}

My understanding is that in your following patch, if mte is enabled, you will always sent mte tags, for all pages needed, right?

Yes, for the current kernel support, we can't tell whether the MTE was enabled for a specific page.

Quoted 13 lines

migration/ram.c static bool control_save_page(RAMState *rs, RAMBlock *block, ram_addr_t offset, · line 1161

Quoted 10 lines
migration/ram.c +9 -0
+static int save_normal_page_mte_tags(QEMUFile *f, uint8_t *addr)
+{
+    uint8_t *tag_buf = NULL;
+    uint64_t ipa;
+    int size = TARGET_PAGE_SIZE / MTE_GRANULE_SIZE;
+
+    if (kvm_physical_memory_addr_from_host(kvm_state, addr, &ipa)) {
+        /* Buffer for the page tags(one byte per tag) */
+        tag_buf = g_try_malloc0(size);

size of the buffer is known at start of migration. Just get a buffer and reuse it?

Yes, we can pre-allocate the buffer during the migration setup time

Quoted 5 lines

Do zero pages have mte tags? From migration point of view, a zero page is a page that is just full of zeros, i.e. nothing else special. Because you are not sending any for them.

Yes, I think we can do some optimization for the zero page migration support.

Quoted 12 lines

migration/ram.c static bool control_save_page(RAMState *rs, RAMBlock *block, ram_addr_t offset, · line 1223

Quoted 8 lines
migration/ram.c +2 -0
@@ -1148,6 +1219,10 @@ static bool control_save_page(RAMState *rs, RAMBlock *block, ram_addr_t offset,
 static int save_normal_page(RAMState *rs, RAMBlock *block, ram_addr_t offset,
                             uint8_t *buf, bool async)
 {
+    if (rs->metadata_enabled) {
+        offset |= RAM_SAVE_FLAG_MTE;

You don't really need the flag, for you normal pages are just TARGET_PAGE_SIZE + (TARGET_PAGE_SIZE/MTE_)

So you suggest to use the size field to indicate whether tags are available?

Quoted 15 lines

migration/ram.c static int save_normal_page(RAMState *rs, RAMBlock *block, ram_addr_t offset, · line 1238

Quoted 12 lines
migration/ram.c +4 -0
+    }
+
     ram_counters.transferred += save_page_header(rs, rs->f, block,
                                                  offset | RAM_SAVE_FLAG_PAGE);
     if (async) {
@@ -1159,6 +1234,11 @@ static int save_normal_page(RAMState *rs, RAMBlock *block, ram_addr_t offset,
     }
     ram_counters.transferred += TARGET_PAGE_SIZE;
     ram_counters.normal++;
+
+    if (rs->metadata_enabled) {

See? You are not checking the flag, you are checking the bool setup at the beggining of migration.

The idea is to only migrate the memory tags when MTE was enabled for the VM. Could you be more elaborate on "You are not checking the flag"?

Quoted 38 lines
Quoted 16 lines
migration/ram.c +4 -0
+        ram_counters.transferred += save_normal_page_mte_tags(rs->f, buf);
+    }
+
     return 1;
 }
 
@@ -2189,6 +2269,7 @@ static void ram_state_reset(RAMState *rs)
     rs->last_version = ram_list.version;
     rs->ram_bulk_stage = true;
     rs->fpo_enabled = false;
+    rs->metadata_enabled = false;
 }
 
 #define MAX_WAIT 50 /* ms, half buffered_file limit */

migration/ram.c static int ram_load_precopy(QEMUFile *f) · line 3863

Quoted 5 lines
migration/ram.c +1 -1
@@ -3779,7 +3860,7 @@ static int ram_load_precopy(QEMUFile *f)
             trace_ram_load_loop(block->idstr, (uint64_t)addr, flags, host);
         }
 
-        switch (flags & ~RAM_SAVE_FLAG_CONTINUE) {
+        switch (flags & ~(RAM_SAVE_FLAG_CONTINUE | RAM_SAVE_FLAG_MTE)) {

Creating the flag is hurting you here also.

migration/ram.c static int ram_load_precopy(QEMUFile *f) · line 3935

Quoted 10 lines
migration/ram.c +3 -0
         case RAM_SAVE_FLAG_MEM_SIZE:
             /* Synchronize RAM block list */
             total_ram_bytes = addr;
@@ -3849,6 +3930,9 @@ static int ram_load_precopy(QEMUFile *f)
 
         case RAM_SAVE_FLAG_PAGE:
             qemu_get_buffer(f, host, TARGET_PAGE_SIZE);
+            if (flags & RAM_SAVE_FLAG_MTE) {
+                load_normal_page_mte_tags(f, host);
+            }

I don't claim to understand the MTE, but my understanding is that if we are using MTE, all pages have to have MTE flags, right?

Quoted 6 lines

So, somtehing like

is_mte_enabled()

that I told in the other thread looks like a good idea.

Yes, we do need a function like this.

Quoted 6 lines

Later, Juan.

Quoted 3 lines
migration/ram.c +0 -0
             break;
 
         case RAM_SAVE_FLAG_COMPRESS_PAGE:
author
#10 lore raw

Re: [RFC PATCH v2 4/5] Add migration support for KVM guest with MTE

In reply to Dr. David Alan Gilbert
Quoted 29 lines · Dr. David Alan Gilbert
Quoted 27 lines · * Haibo Xu ([email protected]) wrote:
Quoted 12 lines · Dr. David Alan Gilbert
Quoted 9 lines · * Haibo Xu ([email protected]) wrote:

To make it easier to keep the page tags sync with the page data, tags for one page are appended to the data during ram save iteration.

This patch only add the pre-copy migration support. Post-copy and compress as well as zero page saving are not supported yet.

Quoted 8 lines

My guess is that this doesn't work with a lot of other options; e.g. postcopy and probably compression and a bunch of other things. Postcopy I can see you'll need some interesting kernel changes for - you'd need to be able to atomically place a page with it's tag data.

You probably need to add stuff to migrate_caps_check to disable features that you don't support.

Hi David,

Thanks so much for the comments!

Quoted 9 lines
Quoted 6 lines

You are right, this RFC patch only supports pre-copy mode, no postcopy, no compression.
As a RFC, here just want to finalize the tag migration process, that is:
1. let the tag go with the page data(the current choice) which may be a little complex to put
them into the current migration process.

I think it's probably the easiest.

Agree!

Quoted 6 lines
Quoted 6 lines

2. migrate them separately which is easy to implement with the current migration(treat the tags
as device status), but it would be hard to keep the page data and
tag to sync with each other.
3. Any other ways?

Quoted 8 lines
Quoted 3 lines

Once the tag migration process is finalized, a new formal patch series with postcopy as well as
compression should be reworked.

It's probably best to look what you would have to do to fit it in with those before finalising the basic version; not necessary implement them
- just get an idea.

Thanks for the suggestion!

Quoted 11 lines
Quoted 5 lines

What's more, you mentioned that "some interesting kernel changes are needed to atomically
place a page with it's tag data". You mean a single kernel API to store page data and tag in
the migration load process?

Yes; see migration/postcopy-ram.c postcopy_place_page - it has the problem of placing the whole page in memory before the guest starts usign it; I think for MTE you're going to have to put the MTE data in at the same time.

Good point! Will check this with the kernel guys.

Quoted 250 lines

Dave

Quoted 172 lines

Regards,
Haibo

Quoted 46 lines
Quoted 46 lines
---
 include/hw/arm/virt.h    |  2 +
 include/migration/misc.h |  1 +
 migration/ram.c          | 86 +++++++++++++++++++++++++++++++++++++++-
 3 files changed, 88 insertions(+), 1 deletion(-)
include/hw/arm/virt.h +2 -0
index 921416f918..8b28cde8bf 100644
@@ -166,6 +166,8 @@ struct VirtMachineState {
     PCIBus *bus;
     char *oem_id;
     char *oem_table_id;
+    /* migrate memory tags */
+    NotifierWithReturn precopy_notifier;
 };
 
 #define VIRT_ECAM_ID(high) (high ? VIRT_HIGH_PCIE_ECAM : VIRT_PCIE_ECAM)
include/migration/misc.h +1 -0
index bccc1b6b44..005133f471 100644
@@ -38,6 +38,7 @@ void precopy_add_notifier(NotifierWithReturn *n);
 void precopy_remove_notifier(NotifierWithReturn *n);
 int precopy_notify(PrecopyNotifyReason reason, Error **errp);
 void precopy_enable_free_page_optimization(void);
+void precopy_enable_metadata_migration(void);
 
 void ram_mig_init(void);
 void qemu_guest_free_page_hint(void *addr, size_t len);
migration/ram.c +2 -0
index 72143da0ac..e67b798c3b 100644
@@ -53,10 +53,12 @@
 #include "block.h"
 #include "sysemu/sysemu.h"
 #include "sysemu/cpu-throttle.h"
+#include "sysemu/kvm.h"
 #include "savevm.h"
 #include "qemu/iov.h"
 #include "multifd.h"
 #include "sysemu/runstate.h"
+#include "kvm_arm.h"
 
Quoted 11 lines

migration/ram.c line 85

Quoted 7 lines
migration/ram.c +1 -0
 #if defined(__linux__)
 #include "qemu/userfaultfd.h"
@@ -80,6 +82,9 @@
 #define RAM_SAVE_FLAG_XBZRLE   0x40
 /* 0x80 is reserved in migration.h start with 0x100 next */
 #define RAM_SAVE_FLAG_COMPRESS_PAGE    0x100
+#define RAM_SAVE_FLAG_MTE              0x200

I think that's using the last free bit in the flags field, which is a bit painful.

Yes, only 0x80(reserved) and 0x200 are available.

Quoted 33 lines
Quoted 33 lines
migration/ram.c +12 -0
+#define MTE_GRANULE_SIZE   (16)
 
 static inline bool is_zero_range(uint8_t *p, uint64_t size)
 {
@@ -317,6 +322,8 @@ struct RAMState {
     bool ram_bulk_stage;
     /* The free page optimization is enabled */
     bool fpo_enabled;
+    /* The RAM meta data(e.t memory tag) is enabled */
+    bool metadata_enabled;
     /* How many times we have dirty too many pages */
     int dirty_rate_high_cnt;
     /* these variables are used for bitmap sync */
@@ -394,6 +401,15 @@ void precopy_enable_free_page_optimization(void)
     ram_state->fpo_enabled = true;
 }
 
+void precopy_enable_metadata_migration(void)
+{
+    if (!ram_state) {
+        return;
+    }
+
+    ram_state->metadata_enabled = true;
+}
+
 uint64_t ram_bytes_remaining(void)
 {
     return ram_state ? (ram_state->migration_dirty_pages * TARGET_PAGE_SIZE) :
@@ -1134,6 +1150,61 @@ static bool control_save_page(RAMState *rs, RAMBlock *block, ram_addr_t offset,
     return true;
 }
 
Quoted 12 lines

migration/ram.c static bool control_save_page(RAMState *rs, RAMBlock *block, ram_addr_t offset, · line 1157

Quoted 5 lines
migration/ram.c +5 -0
+static int save_normal_page_mte_tags(QEMUFile *f, uint8_t *addr)
+{
+    uint8_t *tag_buf = NULL;
+    uint64_t ipa;
+    int size = TARGET_PAGE_SIZE / MTE_GRANULE_SIZE;

This function needs to be mostly somewhere aarch specific; or somehow made more generic.
We shouldn't have RAM_SAVE_FLAG_MTE as well - we should be something like RAM_SAVE_FLAG_ARCH_METADATA and that way other architectures with something else glued onto pages can do something similar. Try and keep migration/ architecture independent.

Yes, it should be arch independent and handled with a more abstract way.

Quoted 7 lines

migration/ram.c static bool control_save_page(RAMState *rs, RAMBlock *block, ram_addr_t offset, · line 1161

Quoted 4 lines
migration/ram.c +3 -0
+    if (kvm_physical_memory_addr_from_host(kvm_state, addr, &ipa)) {
+        /* Buffer for the page tags(one byte per tag) */
+        tag_buf = g_try_malloc0(size);

It feels like you want to allocate tag_buf in setup and free it at the end rather than doing this in every page.

Yes, the tag buffer allocation could be moved to migration save/load setup and cleanup function.

Quoted 9 lines

migration/ram.c static bool control_save_page(RAMState *rs, RAMBlock *block, ram_addr_t offset, · line 1168

Quoted 8 lines
migration/ram.c +7 -0
+        if (!tag_buf) {
+            error_report("%s: Error allocating MTE tag_buf", __func__);
+            return 0;
+        }
+
+        if (kvm_arm_mte_get_tags(ipa, TARGET_PAGE_SIZE, tag_buf) < 0) {
+            error_report("%s: Can't get MTE tags from guest", __func__);
Quoted 3 lines

For any error like this you probably want to say the addresses to make it easier to debug when it fails.

Good suggestion! Will add the "address" info to the log.

Quoted 25 lines
Quoted 25 lines
migration/ram.c +25 -0
+            g_free(tag_buf);
+            return 0;
+        }
+
+        qemu_put_buffer(f, tag_buf, size);
+
+        g_free(tag_buf);
+
+        return size;
+    }
+
+    return 0;
+}
+
+static void load_normal_page_mte_tags(QEMUFile *f, uint8_t *addr)
+{
+    uint8_t *tag_buf = NULL;
+    uint64_t ipa;
+    int size = TARGET_PAGE_SIZE / MTE_GRANULE_SIZE;
+
+    if (kvm_physical_memory_addr_from_host(kvm_state, addr, &ipa)) {
+        /* Buffer for the page tags(one byte per tag) */
+        tag_buf = g_try_malloc0(size);
+        if (!tag_buf) {
+            error_report("%s: Error allocating MTE tag_buf", __func__);
Quoted 8 lines

migration/ram.c static bool control_save_page(RAMState *rs, RAMBlock *block, ram_addr_t offset, · line 1198

Quoted 5 lines
migration/ram.c +5 -0
+            return;
+        }
+
+        qemu_get_buffer(f, tag_buf, size);
+        if (kvm_arm_mte_set_tags(ipa, TARGET_PAGE_SIZE, tag_buf) < 0) {

what protections are there here to stop you setting the mte on something useful, like part of the host kernel or qemu?

Quoted 73 lines

Kernel would do some parameter check(e.t ipa within a valid slot) before setting the tag data.

Quoted 69 lines
Quoted 64 lines
migration/ram.c +23 -2
+            error_report("%s: Can't set MTE tags to guest", __func__);
+        }
+
+        g_free(tag_buf);
+    }
+
+    return;
+}
+
 /*
  * directly send the page to the stream
  *
@@ -1148,6 +1219,10 @@ static bool control_save_page(RAMState *rs, RAMBlock *block, ram_addr_t offset,
 static int save_normal_page(RAMState *rs, RAMBlock *block, ram_addr_t offset,
                             uint8_t *buf, bool async)
 {
+    if (rs->metadata_enabled) {
+        offset |= RAM_SAVE_FLAG_MTE;
+    }
+
     ram_counters.transferred += save_page_header(rs, rs->f, block,
                                                  offset | RAM_SAVE_FLAG_PAGE);
     if (async) {
@@ -1159,6 +1234,11 @@ static int save_normal_page(RAMState *rs, RAMBlock *block, ram_addr_t offset,
     }
     ram_counters.transferred += TARGET_PAGE_SIZE;
     ram_counters.normal++;
+
+    if (rs->metadata_enabled) {
+        ram_counters.transferred += save_normal_page_mte_tags(rs->f, buf);
+    }
+
     return 1;
 }
 
@@ -2189,6 +2269,7 @@ static void ram_state_reset(RAMState *rs)
     rs->last_version = ram_list.version;
     rs->ram_bulk_stage = true;
     rs->fpo_enabled = false;
+    rs->metadata_enabled = false;
 }
 
 #define MAX_WAIT 50 /* ms, half buffered_file limit */
@@ -3779,7 +3860,7 @@ static int ram_load_precopy(QEMUFile *f)
             trace_ram_load_loop(block->idstr, (uint64_t)addr, flags, host);
         }
 
-        switch (flags & ~RAM_SAVE_FLAG_CONTINUE) {
+        switch (flags & ~(RAM_SAVE_FLAG_CONTINUE | RAM_SAVE_FLAG_MTE)) {
         case RAM_SAVE_FLAG_MEM_SIZE:
             /* Synchronize RAM block list */
             total_ram_bytes = addr;
@@ -3849,6 +3930,9 @@ static int ram_load_precopy(QEMUFile *f)
 
         case RAM_SAVE_FLAG_PAGE:
             qemu_get_buffer(f, host, TARGET_PAGE_SIZE);
+            if (flags & RAM_SAVE_FLAG_MTE) {
+                load_normal_page_mte_tags(f, host);
+            }
             break;
 
         case RAM_SAVE_FLAG_COMPRESS_PAGE:
--
Quoted 2 lines

2.17.1

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

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