yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

Blame SOURCES/kvm-spapr-Improve-handling-of-fdt-buffer-size.patch

c687bc
From 04f7fe2423a4de8d2fea7068b3fb316e15e76eaa Mon Sep 17 00:00:00 2001
c687bc
From: Greg Kurz <gkurz@redhat.com>
c687bc
Date: Tue, 19 Jan 2021 15:09:49 -0500
c687bc
Subject: [PATCH 1/9] spapr: Improve handling of fdt buffer size
c687bc
c687bc
RH-Author: Greg Kurz <gkurz@redhat.com>
c687bc
Message-id: <20210119150954.1017058-2-gkurz@redhat.com>
c687bc
Patchwork-id: 100682
c687bc
O-Subject: [RHEL-8.4.0 qemu-kvm PATCH v2 1/6] spapr: Improve handling of fdt buffer size
c687bc
Bugzilla: 1901837
c687bc
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
c687bc
RH-Acked-by: Laurent Vivier <lvivier@redhat.com>
c687bc
RH-Acked-by: David Gibson <dgibson@redhat.com>
c687bc
c687bc
From: David Gibson <david@gibson.dropbear.id.au>
c687bc
c687bc
Previously, spapr_build_fdt() constructed the device tree in a fixed
c687bc
buffer of size FDT_MAX_SIZE.  This is a bit inflexible, but more
c687bc
importantly it's awkward for the case where we use it during CAS.  In
c687bc
that case the guest firmware supplies a buffer and we have to
c687bc
awkwardly check that what we generated fits into it afterwards, after
c687bc
doing a lot of size checks during spapr_build_fdt().
c687bc
c687bc
Simplify this by having spapr_build_fdt() take a 'space' parameter.
c687bc
For the CAS case, we pass in the buffer size provided by SLOF, for the
c687bc
machine init case, we continue to pass FDT_MAX_SIZE.
c687bc
c687bc
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
c687bc
Reviewed-by: Cedric Le Goater <clg@fr.ibm.com>
c687bc
Reviewed-by: Greg Kurz <groug@kaod.org>
c687bc
(cherry picked from commit 97b32a6afa78ae68fb16344b9a144b6f433f42a2)
c687bc
Signed-off-by: Greg Kurz <gkurz@redhat.com>
c687bc
Signed-off-by: Jon Maloy <jmaloy.redhat.com>
c687bc
---
c687bc
 hw/ppc/spapr.c | 33 +++++++++++----------------------
c687bc
 1 file changed, 11 insertions(+), 22 deletions(-)
c687bc
c687bc
diff --git a/hw/ppc/spapr.c b/hw/ppc/spapr.c
c687bc
index c74079702d..92f63ad035 100644
c687bc
--- a/hw/ppc/spapr.c
c687bc
+++ b/hw/ppc/spapr.c
c687bc
@@ -918,7 +918,8 @@ static bool spapr_hotplugged_dev_before_cas(void)
c687bc
     return false;
c687bc
 }
c687bc
 
c687bc
-static void *spapr_build_fdt(SpaprMachineState *spapr, bool reset);
c687bc
+static void *spapr_build_fdt(SpaprMachineState *spapr, bool reset,
c687bc
+                             size_t space);
c687bc
 
c687bc
 int spapr_h_cas_compose_response(SpaprMachineState *spapr,
c687bc
                                  target_ulong addr, target_ulong size,
c687bc
@@ -931,24 +932,17 @@ int spapr_h_cas_compose_response(SpaprMachineState *spapr,
c687bc
         return 1;
c687bc
     }
c687bc
 
c687bc
-    if (size < sizeof(hdr) || size > FW_MAX_SIZE) {
c687bc
-        error_report("SLOF provided an unexpected CAS buffer size "
c687bc
-                     TARGET_FMT_lu " (min: %zu, max: %u)",
c687bc
-                     size, sizeof(hdr), FW_MAX_SIZE);
c687bc
+    if (size < sizeof(hdr)) {
c687bc
+        error_report("SLOF provided insufficient CAS buffer "
c687bc
+                     TARGET_FMT_lu " (min: %zu)", size, sizeof(hdr));
c687bc
         exit(EXIT_FAILURE);
c687bc
     }
c687bc
 
c687bc
     size -= sizeof(hdr);
c687bc
 
c687bc
-    fdt = spapr_build_fdt(spapr, false);
c687bc
+    fdt = spapr_build_fdt(spapr, false, size);
c687bc
     _FDT((fdt_pack(fdt)));
c687bc
 
c687bc
-    if (fdt_totalsize(fdt) + sizeof(hdr) > size) {
c687bc
-        g_free(fdt);
c687bc
-        trace_spapr_cas_failed(size);
c687bc
-        return -1;
c687bc
-    }
c687bc
-
c687bc
     cpu_physical_memory_write(addr, &hdr, sizeof(hdr));
c687bc
     cpu_physical_memory_write(addr + sizeof(hdr), fdt, fdt_totalsize(fdt));
c687bc
     trace_spapr_cas_continue(fdt_totalsize(fdt) + sizeof(hdr));
c687bc
@@ -1198,7 +1192,8 @@ static void spapr_dt_hypervisor(SpaprMachineState *spapr, void *fdt)
c687bc
     }
c687bc
 }
c687bc
 
c687bc
-static void *spapr_build_fdt(SpaprMachineState *spapr, bool reset)
c687bc
+static void *spapr_build_fdt(SpaprMachineState *spapr, bool reset,
c687bc
+                             size_t space)
c687bc
 {
c687bc
     MachineState *machine = MACHINE(spapr);
c687bc
     MachineClass *mc = MACHINE_GET_CLASS(machine);
c687bc
@@ -1208,8 +1203,8 @@ static void *spapr_build_fdt(SpaprMachineState *spapr, bool reset)
c687bc
     SpaprPhbState *phb;
c687bc
     char *buf;
c687bc
 
c687bc
-    fdt = g_malloc0(FDT_MAX_SIZE);
c687bc
-    _FDT((fdt_create_empty_tree(fdt, FDT_MAX_SIZE)));
c687bc
+    fdt = g_malloc0(space);
c687bc
+    _FDT((fdt_create_empty_tree(fdt, space)));
c687bc
 
c687bc
     /* Root node */
c687bc
     _FDT(fdt_setprop_string(fdt, 0, "device_type", "chrp"));
c687bc
@@ -1724,19 +1719,13 @@ static void spapr_machine_reset(MachineState *machine)
c687bc
      */
c687bc
     fdt_addr = MIN(spapr->rma_size, RTAS_MAX_ADDR) - FDT_MAX_SIZE;
c687bc
 
c687bc
-    fdt = spapr_build_fdt(spapr, true);
c687bc
+    fdt = spapr_build_fdt(spapr, true, FDT_MAX_SIZE);
c687bc
 
c687bc
     rc = fdt_pack(fdt);
c687bc
 
c687bc
     /* Should only fail if we've built a corrupted tree */
c687bc
     assert(rc == 0);
c687bc
 
c687bc
-    if (fdt_totalsize(fdt) > FDT_MAX_SIZE) {
c687bc
-        error_report("FDT too big ! 0x%x bytes (max is 0x%x)",
c687bc
-                     fdt_totalsize(fdt), FDT_MAX_SIZE);
c687bc
-        exit(1);
c687bc
-    }
c687bc
-
c687bc
     /* Load the fdt */
c687bc
     qemu_fdt_dumpdtb(fdt, fdt_totalsize(fdt));
c687bc
     cpu_physical_memory_write(fdt_addr, fdt, fdt_totalsize(fdt));
c687bc
-- 
c687bc
2.18.2
c687bc