Blame SOURCES/kvm-dump-fix-kdump-to-work-over-non-aligned-blocks.patch

62547e
From deaf4e0f5e90d227b7b9f3e5d1dff7fd0bc0206a Mon Sep 17 00:00:00 2001
62547e
From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= <marcandre.lureau@redhat.com>
62547e
Date: Mon, 5 Sep 2022 16:06:21 +0400
62547e
Subject: [PATCH 31/42] dump: fix kdump to work over non-aligned blocks
62547e
MIME-Version: 1.0
62547e
Content-Type: text/plain; charset=UTF-8
62547e
Content-Transfer-Encoding: 8bit
62547e
62547e
RH-Author: Cédric Le Goater <clg@redhat.com>
62547e
RH-MergeRequest: 226: s390: Enhanced Interpretation for PCI Functions and Secure Execution guest dump
62547e
RH-Bugzilla: 1664378 2043909
62547e
RH-Acked-by: Thomas Huth <thuth@redhat.com>
62547e
RH-Acked-by: Cornelia Huck <cohuck@redhat.com>
62547e
RH-Acked-by: Jon Maloy <jmaloy@redhat.com>
62547e
RH-Commit: [31/41] b307bdce4a4791fc30160fa2a1678bd238f2432e
62547e
62547e
Rewrite get_next_page() to work over non-aligned blocks. When it
62547e
encounters non aligned addresses, it will try to fill a page provided by
62547e
the caller.
62547e
62547e
This solves a kdump crash with "tpm-crb-cmd" RAM memory region,
62547e
qemu-kvm: ../dump/dump.c:1162: _Bool get_next_page(GuestPhysBlock **,
62547e
uint64_t *, uint8_t **, DumpState *): Assertion `(block->target_start &
62547e
~target_page_mask) == 0' failed.
62547e
62547e
because:
62547e
guest_phys_block_add_section: target_start=00000000fed40080 target_end=00000000fed41000: added (count: 4)
62547e
62547e
Fixes:
62547e
https://bugzilla.redhat.com/show_bug.cgi?id=2120480
62547e
62547e
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
62547e
Acked-by: David Hildenbrand <david@redhat.com>
62547e
(cherry picked from commit 94d788408d2d5a6474c99b2c9cf06913b9db7c58)
62547e
Signed-off-by: Cédric Le Goater <clg@redhat.com>
62547e
---
62547e
 dump/dump.c | 79 +++++++++++++++++++++++++++++++++++++----------------
62547e
 1 file changed, 56 insertions(+), 23 deletions(-)
62547e
62547e
diff --git a/dump/dump.c b/dump/dump.c
62547e
index 1c49232390..88177fa886 100644
62547e
--- a/dump/dump.c
62547e
+++ b/dump/dump.c
62547e
@@ -1117,50 +1117,81 @@ static uint64_t dump_pfn_to_paddr(DumpState *s, uint64_t pfn)
62547e
 }
62547e
 
62547e
 /*
62547e
- * exam every page and return the page frame number and the address of the page.
62547e
- * bufptr can be NULL. note: the blocks here is supposed to reflect guest-phys
62547e
- * blocks, so block->target_start and block->target_end should be interal
62547e
- * multiples of the target page size.
62547e
+ * Return the page frame number and the page content in *bufptr. bufptr can be
62547e
+ * NULL. If not NULL, *bufptr must contains a target page size of pre-allocated
62547e
+ * memory. This is not necessarily the memory returned.
62547e
  */
62547e
 static bool get_next_page(GuestPhysBlock **blockptr, uint64_t *pfnptr,
62547e
                           uint8_t **bufptr, DumpState *s)
62547e
 {
62547e
     GuestPhysBlock *block = *blockptr;
62547e
-    hwaddr addr, target_page_mask = ~((hwaddr)s->dump_info.page_size - 1);
62547e
-    uint8_t *buf;
62547e
+    uint32_t page_size = s->dump_info.page_size;
62547e
+    uint8_t *buf = NULL, *hbuf;
62547e
+    hwaddr addr;
62547e
 
62547e
     /* block == NULL means the start of the iteration */
62547e
     if (!block) {
62547e
         block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
62547e
         *blockptr = block;
62547e
         addr = block->target_start;
62547e
+        *pfnptr = dump_paddr_to_pfn(s, addr);
62547e
     } else {
62547e
-        addr = dump_pfn_to_paddr(s, *pfnptr + 1);
62547e
+        *pfnptr += 1;
62547e
+        addr = dump_pfn_to_paddr(s, *pfnptr);
62547e
     }
62547e
     assert(block != NULL);
62547e
 
62547e
-    if ((addr >= block->target_start) &&
62547e
-        (addr + s->dump_info.page_size <= block->target_end)) {
62547e
-        buf = block->host_addr + (addr - block->target_start);
62547e
-    } else {
62547e
-        /* the next page is in the next block */
62547e
-        block = QTAILQ_NEXT(block, next);
62547e
-        *blockptr = block;
62547e
-        if (!block) {
62547e
-            return false;
62547e
+    while (1) {
62547e
+        if (addr >= block->target_start && addr < block->target_end) {
62547e
+            size_t n = MIN(block->target_end - addr, page_size - addr % page_size);
62547e
+            hbuf = block->host_addr + (addr - block->target_start);
62547e
+            if (!buf) {
62547e
+                if (n == page_size) {
62547e
+                    /* this is a whole target page, go for it */
62547e
+                    assert(addr % page_size == 0);
62547e
+                    buf = hbuf;
62547e
+                    break;
62547e
+                } else if (bufptr) {
62547e
+                    assert(*bufptr);
62547e
+                    buf = *bufptr;
62547e
+                    memset(buf, 0, page_size);
62547e
+                } else {
62547e
+                    return true;
62547e
+                }
62547e
+            }
62547e
+
62547e
+            memcpy(buf + addr % page_size, hbuf, n);
62547e
+            addr += n;
62547e
+            if (addr % page_size == 0) {
62547e
+                /* we filled up the page */
62547e
+                break;
62547e
+            }
62547e
+        } else {
62547e
+            /* the next page is in the next block */
62547e
+            *blockptr = block = QTAILQ_NEXT(block, next);
62547e
+            if (!block) {
62547e
+                break;
62547e
+            }
62547e
+
62547e
+            addr = block->target_start;
62547e
+            /* are we still in the same page? */
62547e
+            if (dump_paddr_to_pfn(s, addr) != *pfnptr) {
62547e
+                if (buf) {
62547e
+                    /* no, but we already filled something earlier, return it */
62547e
+                    break;
62547e
+                } else {
62547e
+                    /* else continue from there */
62547e
+                    *pfnptr = dump_paddr_to_pfn(s, addr);
62547e
+                }
62547e
+            }
62547e
         }
62547e
-        addr = block->target_start;
62547e
-        buf = block->host_addr;
62547e
     }
62547e
 
62547e
-    assert((block->target_start & ~target_page_mask) == 0);
62547e
-    assert((block->target_end & ~target_page_mask) == 0);
62547e
-    *pfnptr = dump_paddr_to_pfn(s, addr);
62547e
     if (bufptr) {
62547e
         *bufptr = buf;
62547e
     }
62547e
 
62547e
-    return true;
62547e
+    return buf != NULL;
62547e
 }
62547e
 
62547e
 static void write_dump_bitmap(DumpState *s, Error **errp)
62547e
@@ -1306,6 +1337,7 @@ static void write_dump_pages(DumpState *s, Error **errp)
62547e
     uint8_t *buf;
62547e
     GuestPhysBlock *block_iter = NULL;
62547e
     uint64_t pfn_iter;
62547e
+    g_autofree uint8_t *page = NULL;
62547e
 
62547e
     /* get offset of page_desc and page_data in dump file */
62547e
     offset_desc = s->offset_page;
62547e
@@ -1341,12 +1373,13 @@ static void write_dump_pages(DumpState *s, Error **errp)
62547e
     }
62547e
 
62547e
     offset_data += s->dump_info.page_size;
62547e
+    page = g_malloc(s->dump_info.page_size);
62547e
 
62547e
     /*
62547e
      * dump memory to vmcore page by page. zero page will all be resided in the
62547e
      * first page of page section
62547e
      */
62547e
-    while (get_next_page(&block_iter, &pfn_iter, &buf, s)) {
62547e
+    for (buf = page; get_next_page(&block_iter, &pfn_iter, &buf, s); buf = page) {
62547e
         /* check zero page */
62547e
         if (is_zero_page(buf, s->dump_info.page_size)) {
62547e
             ret = write_cache(&page_desc, &pd_zero, sizeof(PageDescriptor),
62547e
-- 
62547e
2.37.3
62547e