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

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