Skip to contentSkip to conversation

tpm: fix PPI RAM migration

PATCH Superseded Marc-André Lureau 2 participants 6 messages Jan 2, 2020 – Jan 3, 2020

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover tpm: fix PPI RAM migration

Hi,

The following series fixes a migration issue with the TPM PPI code due to unaligned host RAM pointer.

Marc-André Lureau (2):
RFC: savevm: check RAM is page_size aligned
tpm-ppi: page-align PPI RAM

 hw/tpm/tpm_ppi.c   | 3 ++-
 migration/savevm.c | 5 +++++
 2 files changed, 7 insertions(+), 1 deletion(-)
Signature · 1 line
2.24.0.308.g228f53135a
#1 lore raw

PATCH 1/2 RFC: savevm: check RAM is page_size aligned

Check the host pointer is correctly aligned, otherwise we may fail during migration in ram_block_discard_range().

---
 migration/savevm.c | 5 +++++
 1 file changed, 5 insertions(+)
migration/savevm.c +5 -0
index a71b930b91..ab6e02011f 100644
@@ -2910,6 +2910,11 @@ err_drain:
 
 void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
 {
+    RAMBlock *rb = mr->ram_block;
+    uintptr_t hostaddr = (uintptr_t)qemu_ram_get_host_addr(rb);
+
+    assert((hostaddr & (qemu_ram_pagesize(rb) - 1)) == 0);
+
     qemu_ram_set_idstr(mr->ram_block,
                        memory_region_name(mr), dev);
     qemu_ram_set_migratable(mr->ram_block);
Signature · 1 line
2.24.0.308.g228f53135a
#2 lore raw

Re: [PATCH 1/2] RFC: savevm: check RAM is page_size aligned

Hi Marc-André,

Quoted 13 lines · Marc-André Lureau

Check the host pointer is correctly aligned, otherwise we may fail during migration in ram_block_discard_range().

---
  migration/savevm.c | 5 +++++
  1 file changed, 5 insertions(+)
migration/savevm.c +0 -0
index a71b930b91..ab6e02011f 100644

migration/savevm.c err_drain: · line 2916

Quoted 7 lines
migration/savevm.c +4 -0
@@ -2910,6 +2910,11 @@ err_drain:
 
  void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
  {
+    RAMBlock *rb = mr->ram_block;
+    uintptr_t hostaddr = (uintptr_t)qemu_ram_get_host_addr(rb);
+
+    assert((hostaddr & (qemu_ram_pagesize(rb) - 1)) == 0);

Can we use the QEMU_PTR_IS_ALIGNED() macro instead?

       assert(QEMU_PTR_IS_ALIGNED(qemu_ram_get_host_addr(rb),
                                  qemu_ram_pagesize(rb)));
Quoted 4 lines
migration/savevm.c +0 -0
      qemu_ram_set_idstr(mr->ram_block,
                         memory_region_name(mr), dev);
      qemu_ram_set_migratable(mr->ram_block);
#3 lore raw

Re: [PATCH 1/2] RFC: savevm: check RAM is page_size aligned

Hi

Quoted 27 lines · Philippe Mathieu-Daudé

Hi Marc-André,

Quoted 13 lines · Marc-André Lureau

Check the host pointer is correctly aligned, otherwise we may fail during migration in ram_block_discard_range().

---
  migration/savevm.c | 5 +++++
  1 file changed, 5 insertions(+)
migration/savevm.c +0 -0
index a71b930b91..ab6e02011f 100644

migration/savevm.c err_drain: · line 2916

Quoted 7 lines
migration/savevm.c +4 -0
@@ -2910,6 +2910,11 @@ err_drain:
 
  void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
  {
+    RAMBlock *rb = mr->ram_block;
+    uintptr_t hostaddr = (uintptr_t)qemu_ram_get_host_addr(rb);
+
+    assert((hostaddr & (qemu_ram_pagesize(rb) - 1)) == 0);

Can we use the QEMU_PTR_IS_ALIGNED() macro instead?

Quoted 3 lines
        assert(QEMU_PTR_IS_ALIGNED(qemu_ram_get_host_addr(rb),
                                   qemu_ram_pagesize(rb)));

nice, good idea

Quoted 5 lines
Quoted 4 lines
migration/savevm.c +0 -0
      qemu_ram_set_idstr(mr->ram_block,
                         memory_region_name(mr), dev);
      qemu_ram_set_migratable(mr->ram_block);
#4 lore raw

PATCH 2/2 tpm-ppi: page-align PPI RAM

In reply to Marc-André Lureau

post-copy migration fails on destination with error such as:

2019-12-26T10:22:44.714644Z qemu-kvm: ram_block_discard_range:

Unaligned start address: 0x559d2afae9a0

Use qemu_memalign() to constrain the PPI RAM memory alignment.

---
 hw/tpm/tpm_ppi.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
hw/tpm/tpm_ppi.c +2 -1
index ff314592b4..6d9c1a3e40 100644
@@ -43,7 +43,8 @@ void tpm_ppi_reset(TPMPPI *tpmppi)
 void tpm_ppi_init(TPMPPI *tpmppi, struct MemoryRegion *m,
                   hwaddr addr, Object *obj)
 {
-    tpmppi->buf = g_malloc0(HOST_PAGE_ALIGN(TPM_PPI_ADDR_SIZE));
+    tpmppi->buf = qemu_memalign(qemu_real_host_page_size,
+                                HOST_PAGE_ALIGN(TPM_PPI_ADDR_SIZE));
     memory_region_init_ram_device_ptr(&tpmppi->ram, obj, "tpm-ppi",
                                       TPM_PPI_ADDR_SIZE, tpmppi->buf);
     vmstate_register_ram(&tpmppi->ram, DEVICE(obj));
Signature · 1 line
2.24.0.308.g228f53135a
Reviewed-by on v1 2/2
#5 lore raw

Re: [PATCH 2/2] tpm-ppi: page-align PPI RAM

Quoted 8 lines · Marc-André Lureau

post-copy migration fails on destination with error such as:

2019-12-26T10:22:44.714644Z qemu-kvm: ram_block_discard_range:

Unaligned start address: 0x559d2afae9a0

Use qemu_memalign() to constrain the PPI RAM memory alignment.

Quoted 19 lines
---
  hw/tpm/tpm_ppi.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
hw/tpm/tpm_ppi.c +2 -1
index ff314592b4..6d9c1a3e40 100644
@@ -43,7 +43,8 @@ void tpm_ppi_reset(TPMPPI *tpmppi)
  void tpm_ppi_init(TPMPPI *tpmppi, struct MemoryRegion *m,
                    hwaddr addr, Object *obj)
  {
-    tpmppi->buf = g_malloc0(HOST_PAGE_ALIGN(TPM_PPI_ADDR_SIZE));
+    tpmppi->buf = qemu_memalign(qemu_real_host_page_size,
+                                HOST_PAGE_ALIGN(TPM_PPI_ADDR_SIZE));
      memory_region_init_ram_device_ptr(&tpmppi->ram, obj, "tpm-ppi",
                                        TPM_PPI_ADDR_SIZE, tpmppi->buf);
      vmstate_register_ram(&tpmppi->ram, DEVICE(obj));