yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
958e1b
From e660f3651e349f658fe393917bab3ef8da44448c Mon Sep 17 00:00:00 2001
958e1b
From: Laszlo Ersek <lersek@redhat.com>
958e1b
Date: Fri, 7 Nov 2014 17:18:10 +0100
958e1b
Subject: [PATCH 23/41] dump: eliminate DumpState.page_size ("guest's page
958e1b
 size")
958e1b
958e1b
Message-id: <1415380693-16593-24-git-send-email-lersek@redhat.com>
958e1b
Patchwork-id: 62211
958e1b
O-Subject: [RHEL-7.1 qemu-kvm PATCH 23/26] dump: eliminate DumpState.page_size ("guest's page size")
958e1b
Bugzilla: 1157798
958e1b
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
958e1b
RH-Acked-by: Luiz Capitulino <lcapitulino@redhat.com>
958e1b
RH-Acked-by: dgibson <dgibson@redhat.com>
958e1b
958e1b
Use TARGET_PAGE_SIZE and ~TARGET_PAGE_MASK instead.
958e1b
958e1b
"DumpState.page_size" has type "size_t", whereas TARGET_PAGE_SIZE has type
958e1b
"int". TARGET_PAGE_MASK is of type "int" and has negative value. The patch
958e1b
affects the implicit type conversions as follows:
958e1b
958e1b
- create_header32() and create_header64(): assigned to "block_size", which
958e1b
  has type "uint32_t". No change.
958e1b
958e1b
- get_next_page(): "block->target_start", "block->target_end" and "addr"
958e1b
  have type "hwaddr" (uint64_t).
958e1b
958e1b
  Before the patch,
958e1b
  - if "size_t" was "uint64_t", then no additional conversion was done as
958e1b
    part of the usual arithmetic conversions,
958e1b
  - If "size_t" was "uint32_t", then it was widened to uint64_t as part of
958e1b
    the usual arithmetic conversions,
958e1b
  for the remainder and addition operators.
958e1b
958e1b
  After the patch,
958e1b
  - "~TARGET_PAGE_MASK" expands to  ~~((1 << TARGET_PAGE_BITS) - 1). It
958e1b
    has type "int" and positive value (only least significant bits set).
958e1b
    That's converted (widened) to "uint64_t" for the bit-ands. No visible
958e1b
    change.
958e1b
  - The same holds for the (addr + TARGET_PAGE_SIZE) addition.
958e1b
958e1b
- write_dump_pages():
958e1b
  - TARGET_PAGE_SIZE passed as argument to a bunch of functions that all
958e1b
    have prototypes. No change.
958e1b
958e1b
  - When incrementing "offset_data" (of type "off_t"): given that we never
958e1b
    build for ILP32_OFF32 (see "-D_FILE_OFFSET_BITS=64" in configure),
958e1b
    "off_t" is always "int64_t", and we only need to consider:
958e1b
    - ILP32_OFFBIG: "size_t" is "uint32_t".
958e1b
      - before: int64_t += uint32_t. Page size converted to int64_t for
958e1b
        the addition.
958e1b
      - after:  int64_t += int32_t. No change.
958e1b
    - LP64_OFF64: "size_t" is "uint64_t".
958e1b
      - before: int64_t += uint64_t. Offset converted to uint64_t for the
958e1b
        addition, then the uint64_t result is converted to int64_t for
958e1b
        storage.
958e1b
      - after:  int64_t += int32_t. Same as the ILP32_OFFBIG/after case.
958e1b
        No visible change.
958e1b
958e1b
  - (size_out < s->page_size) comparisons, and (size_out = s->page_size)
958e1b
    assignment:
958e1b
    - before: "size_out" is of type "size_t", no implicit conversion for
958e1b
              either operator.
958e1b
    - after: TARGET_PAGE_SIZE (of type "int" and positive value) is
958e1b
             converted to "size_t" (for the relop because the latter is
958e1b
             one of "uint32_t" and "uint64_t"). No visible change.
958e1b
958e1b
- dump_init():
958e1b
  - DIV_ROUND_UP(DIV_ROUND_UP(s->max_mapnr, CHAR_BIT), s->page_size): The
958e1b
    innermost "DumpState.max_mapnr" field has type uint64_t, which
958e1b
    propagates through all implicit conversions at hand:
958e1b
958e1b
    #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
958e1b
958e1b
    regardless of the page size macro argument's type. In the outer macro
958e1b
    replacement, the page size is converted from uint32_t and int32_t
958e1b
    alike to uint64_t.
958e1b
958e1b
  - (tmp * s->page_size) multiplication: "tmp" has size "uint64_t"; the
958e1b
    RHS is converted to that type from uint32_t and int32_t just the same
958e1b
    if it's not uint64_t to begin with.
958e1b
958e1b
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
958e1b
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
958e1b
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
958e1b
(cherry picked from commit 2f859f80c2077e00237ea1dfae2523ebd8377f5f)
958e1b
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
958e1b
---
958e1b
 dump.c | 51 +++++++++++++++++++++++++--------------------------
958e1b
 1 file changed, 25 insertions(+), 26 deletions(-)
958e1b
958e1b
diff --git a/dump.c b/dump.c
958e1b
index bc82b55..f8e0fd7 100644
958e1b
--- a/dump.c
958e1b
+++ b/dump.c
958e1b
@@ -90,7 +90,6 @@ typedef struct DumpState {
958e1b
     uint8_t *note_buf;          /* buffer for notes */
958e1b
     size_t note_buf_offset;     /* the writing place in note_buf */
958e1b
     uint32_t nr_cpus;           /* number of guest's cpu */
958e1b
-    size_t page_size;           /* guest's page size */
958e1b
     uint64_t max_mapnr;         /* the biggest guest's phys-mem's number */
958e1b
     size_t len_dump_bitmap;     /* the size of the place used to store
958e1b
                                    dump_bitmap in vmcore */
958e1b
@@ -811,7 +810,7 @@ static int create_header32(DumpState *s)
958e1b
 
958e1b
     strncpy(dh->signature, KDUMP_SIGNATURE, strlen(KDUMP_SIGNATURE));
958e1b
     dh->header_version = cpu_convert_to_target32(6, endian);
958e1b
-    block_size = s->page_size;
958e1b
+    block_size = TARGET_PAGE_SIZE;
958e1b
     dh->block_size = cpu_convert_to_target32(block_size, endian);
958e1b
     sub_hdr_size = sizeof(struct KdumpSubHeader32) + s->note_size;
958e1b
     sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
958e1b
@@ -918,7 +917,7 @@ static int create_header64(DumpState *s)
958e1b
 
958e1b
     strncpy(dh->signature, KDUMP_SIGNATURE, strlen(KDUMP_SIGNATURE));
958e1b
     dh->header_version = cpu_convert_to_target32(6, endian);
958e1b
-    block_size = s->page_size;
958e1b
+    block_size = TARGET_PAGE_SIZE;
958e1b
     dh->block_size = cpu_convert_to_target32(block_size, endian);
958e1b
     sub_hdr_size = sizeof(struct KdumpSubHeader64) + s->note_size;
958e1b
     sub_hdr_size = DIV_ROUND_UP(sub_hdr_size, block_size);
958e1b
@@ -1089,8 +1088,8 @@ static bool get_next_page(GuestPhysBlock **blockptr, uint64_t *pfnptr,
958e1b
     if (!block) {
958e1b
         block = QTAILQ_FIRST(&s->guest_phys_blocks.head);
958e1b
         *blockptr = block;
958e1b
-        assert(block->target_start % s->page_size == 0);
958e1b
-        assert(block->target_end % s->page_size == 0);
958e1b
+        assert((block->target_start & ~TARGET_PAGE_MASK) == 0);
958e1b
+        assert((block->target_end & ~TARGET_PAGE_MASK) == 0);
958e1b
         *pfnptr = paddr_to_pfn(block->target_start);
958e1b
         if (bufptr) {
958e1b
             *bufptr = block->host_addr;
958e1b
@@ -1102,7 +1101,7 @@ static bool get_next_page(GuestPhysBlock **blockptr, uint64_t *pfnptr,
958e1b
     addr = pfn_to_paddr(*pfnptr);
958e1b
 
958e1b
     if ((addr >= block->target_start) &&
958e1b
-        (addr + s->page_size <= block->target_end)) {
958e1b
+        (addr + TARGET_PAGE_SIZE <= block->target_end)) {
958e1b
         buf = block->host_addr + (addr - block->target_start);
958e1b
     } else {
958e1b
         /* the next page is in the next block */
958e1b
@@ -1111,8 +1110,8 @@ static bool get_next_page(GuestPhysBlock **blockptr, uint64_t *pfnptr,
958e1b
         if (!block) {
958e1b
             return false;
958e1b
         }
958e1b
-        assert(block->target_start % s->page_size == 0);
958e1b
-        assert(block->target_end % s->page_size == 0);
958e1b
+        assert((block->target_start & ~TARGET_PAGE_MASK) == 0);
958e1b
+        assert((block->target_end & ~TARGET_PAGE_MASK) == 0);
958e1b
         *pfnptr = paddr_to_pfn(block->target_start);
958e1b
         buf = block->host_addr;
958e1b
     }
958e1b
@@ -1297,7 +1296,7 @@ static int write_dump_pages(DumpState *s)
958e1b
     prepare_data_cache(&page_data, s, offset_data);
958e1b
 
958e1b
     /* prepare buffer to store compressed data */
958e1b
-    len_buf_out = get_len_buf_out(s->page_size, s->flag_compress);
958e1b
+    len_buf_out = get_len_buf_out(TARGET_PAGE_SIZE, s->flag_compress);
958e1b
     if (len_buf_out == 0) {
958e1b
         dump_error(s, "dump: failed to get length of output buffer.\n");
958e1b
         goto out;
958e1b
@@ -1313,19 +1312,19 @@ static int write_dump_pages(DumpState *s)
958e1b
      * init zero page's page_desc and page_data, because every zero page
958e1b
      * uses the same page_data
958e1b
      */
958e1b
-    pd_zero.size = cpu_convert_to_target32(s->page_size, endian);
958e1b
+    pd_zero.size = cpu_convert_to_target32(TARGET_PAGE_SIZE, endian);
958e1b
     pd_zero.flags = cpu_convert_to_target32(0, endian);
958e1b
     pd_zero.offset = cpu_convert_to_target64(offset_data, endian);
958e1b
     pd_zero.page_flags = cpu_convert_to_target64(0, endian);
958e1b
-    buf = g_malloc0(s->page_size);
958e1b
-    ret = write_cache(&page_data, buf, s->page_size, false);
958e1b
+    buf = g_malloc0(TARGET_PAGE_SIZE);
958e1b
+    ret = write_cache(&page_data, buf, TARGET_PAGE_SIZE, false);
958e1b
     g_free(buf);
958e1b
     if (ret < 0) {
958e1b
         dump_error(s, "dump: failed to write page data(zero page).\n");
958e1b
         goto out;
958e1b
     }
958e1b
 
958e1b
-    offset_data += s->page_size;
958e1b
+    offset_data += TARGET_PAGE_SIZE;
958e1b
 
958e1b
     /*
958e1b
      * dump memory to vmcore page by page. zero page will all be resided in the
958e1b
@@ -1333,7 +1332,7 @@ static int write_dump_pages(DumpState *s)
958e1b
      */
958e1b
     while (get_next_page(&block_iter, &pfn_iter, &buf, s)) {
958e1b
         /* check zero page */
958e1b
-        if (is_zero_page(buf, s->page_size)) {
958e1b
+        if (is_zero_page(buf, TARGET_PAGE_SIZE)) {
958e1b
             ret = write_cache(&page_desc, &pd_zero, sizeof(PageDescriptor),
958e1b
                               false);
958e1b
             if (ret < 0) {
958e1b
@@ -1354,8 +1353,9 @@ static int write_dump_pages(DumpState *s)
958e1b
              */
958e1b
              size_out = len_buf_out;
958e1b
              if ((s->flag_compress & DUMP_DH_COMPRESSED_ZLIB) &&
958e1b
-                    (compress2(buf_out, (uLongf *)&size_out, buf, s->page_size,
958e1b
-                    Z_BEST_SPEED) == Z_OK) && (size_out < s->page_size)) {
958e1b
+                 (compress2(buf_out, (uLongf *)&size_out, buf,
958e1b
+                            TARGET_PAGE_SIZE, Z_BEST_SPEED) == Z_OK) &&
958e1b
+                 (size_out < TARGET_PAGE_SIZE)) {
958e1b
                 pd.flags = cpu_convert_to_target32(DUMP_DH_COMPRESSED_ZLIB,
958e1b
                                                    endian);
958e1b
                 pd.size  = cpu_convert_to_target32(size_out, endian);
958e1b
@@ -1367,9 +1367,9 @@ static int write_dump_pages(DumpState *s)
958e1b
                 }
958e1b
 #ifdef CONFIG_LZO
958e1b
             } else if ((s->flag_compress & DUMP_DH_COMPRESSED_LZO) &&
958e1b
-                    (lzo1x_1_compress(buf, s->page_size, buf_out,
958e1b
+                    (lzo1x_1_compress(buf, TARGET_PAGE_SIZE, buf_out,
958e1b
                     (lzo_uint *)&size_out, wrkmem) == LZO_E_OK) &&
958e1b
-                    (size_out < s->page_size)) {
958e1b
+                    (size_out < TARGET_PAGE_SIZE)) {
958e1b
                 pd.flags = cpu_convert_to_target32(DUMP_DH_COMPRESSED_LZO,
958e1b
                                                    endian);
958e1b
                 pd.size  = cpu_convert_to_target32(size_out, endian);
958e1b
@@ -1382,9 +1382,9 @@ static int write_dump_pages(DumpState *s)
958e1b
 #endif
958e1b
 #ifdef CONFIG_SNAPPY
958e1b
             } else if ((s->flag_compress & DUMP_DH_COMPRESSED_SNAPPY) &&
958e1b
-                    (snappy_compress((char *)buf, s->page_size,
958e1b
+                    (snappy_compress((char *)buf, TARGET_PAGE_SIZE,
958e1b
                     (char *)buf_out, &size_out) == SNAPPY_OK) &&
958e1b
-                    (size_out < s->page_size)) {
958e1b
+                    (size_out < TARGET_PAGE_SIZE)) {
958e1b
                 pd.flags = cpu_convert_to_target32(
958e1b
                                         DUMP_DH_COMPRESSED_SNAPPY, endian);
958e1b
                 pd.size  = cpu_convert_to_target32(size_out, endian);
958e1b
@@ -1398,13 +1398,13 @@ static int write_dump_pages(DumpState *s)
958e1b
             } else {
958e1b
                 /*
958e1b
                  * fall back to save in plaintext, size_out should be
958e1b
-                 * assigned to s->page_size
958e1b
+                 * assigned TARGET_PAGE_SIZE
958e1b
                  */
958e1b
                 pd.flags = cpu_convert_to_target32(0, endian);
958e1b
-                size_out = s->page_size;
958e1b
+                size_out = TARGET_PAGE_SIZE;
958e1b
                 pd.size = cpu_convert_to_target32(size_out, endian);
958e1b
 
958e1b
-                ret = write_cache(&page_data, buf, s->page_size, false);
958e1b
+                ret = write_cache(&page_data, buf, TARGET_PAGE_SIZE, false);
958e1b
                 if (ret < 0) {
958e1b
                     dump_error(s, "dump: failed to write page data.\n");
958e1b
                     goto out;
958e1b
@@ -1616,13 +1616,12 @@ static int dump_init(DumpState *s, int fd, bool has_format,
958e1b
     }
958e1b
 
958e1b
     s->nr_cpus = nr_cpus;
958e1b
-    s->page_size = TARGET_PAGE_SIZE;
958e1b
 
958e1b
     get_max_mapnr(s);
958e1b
 
958e1b
     uint64_t tmp;
958e1b
-    tmp = DIV_ROUND_UP(DIV_ROUND_UP(s->max_mapnr, CHAR_BIT), s->page_size);
958e1b
-    s->len_dump_bitmap = tmp * s->page_size;
958e1b
+    tmp = DIV_ROUND_UP(DIV_ROUND_UP(s->max_mapnr, CHAR_BIT), TARGET_PAGE_SIZE);
958e1b
+    s->len_dump_bitmap = tmp * TARGET_PAGE_SIZE;
958e1b
 
958e1b
     /* init for kdump-compressed format */
958e1b
     if (has_format && format != DUMP_GUEST_MEMORY_FORMAT_ELF) {
958e1b
-- 
958e1b
1.8.3.1
958e1b