Skip to contentSkip to conversation

xen/device-tree: FDT dump helper and common discard_initial_modules()

PATCH Superseded Oleksii Kurochko 2 participants 7 messages Oct 1 – Oct 9

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover xen/device-tree: FDT dump helper and common discard_initial_modules()

Two small, independent device-tree changes. Arm is the only user of both at the moment; RISC-V is going to use them as well.

Patch 1 adds device_tree_dump(), which prints an FDT in a DTS-like format under CONFIG_DEVICE_TREE_DEBUG, to make it possible to inspect the device tree Xen generates for a dom0less domain.

Patch 2 moves discard_initial_modules() from Arm to common device-tree code, as everything it uses is already common, so that other device-tree based architectures can reuse it. No functional change.

Build-tested on arm64 and arm32.

CI tests: https://gitlab.com/xen-project/people/olkur/xen/-/pipelines/2900841778

Oleksii Kurochko (2):
xen/device-tree: add a helper to dump an FDT in DTS-like format xen/device-tree: move discard_initial_modules() to common code

 xen/Kconfig.debug                       |   3 +-
 xen/arch/arm/include/asm/setup.h        |   2 -
 xen/arch/arm/setup.c                    |  35 +----
 xen/common/device-tree/Makefile         |   1 +
 xen/common/device-tree/bootinfo.c       |  30 ++++
 xen/common/device-tree/dom0less-build.c |   4 +
 xen/common/device-tree/fdt-dump.c       | 180 ++++++++++++++++++++++++
 xen/include/xen/bootfdt.h               |  13 ++
 xen/include/xen/bootinfo.h              |   8 ++
 9 files changed, 240 insertions(+), 36 deletions(-)
 create mode 100644 xen/common/device-tree/fdt-dump.c

base-commit: f9b72572c8d5b6d312fd0b5014f65bed20efac40

Signature · 1 line
2.55.0
#1 lore raw

PATCH 1/2 xen/device-tree: add a helper to dump an FDT in DTS-like format

The device tree Xen builds for a dom0less domain is only ever consumed by the guest, so a mistake in it surfaces as an obscure failure inside the domain, far away from the code which produced the tree.

Add device_tree_dump(). The output follows dtc's format so that a dump can be diffed against the source DTS or fed back to dtc; the value formatting heuristics are ported from dtc's utilfdt_print_data() for that reason. The walk itself goes through libfdt rather than decoding the tag stream by hand as dtc's fdtdump does.

Dumping a whole tree is only useful while debugging and costs both code size and a lot of console output, hence CONFIG_DEVICE_TREE_DEBUG. Mention the dump in the help text of the option.

---
 xen/Kconfig.debug                       |   3 +-
 xen/common/device-tree/Makefile         |   1 +
 xen/common/device-tree/dom0less-build.c |   4 +
 xen/common/device-tree/fdt-dump.c       | 180 ++++++++++++++++++++++++
 xen/include/xen/bootfdt.h               |  13 ++
 5 files changed, 200 insertions(+), 1 deletion(-)
 create mode 100644 xen/common/device-tree/fdt-dump.c
xen/Kconfig.debug +2 -1
index d900d926c555..664f50bf8a96 100644
@@ -95,7 +95,8 @@ config DEVICE_TREE_DEBUG
 	depends on DEVICE_TREE_PARSE
 	help
 	  Device tree parsing and DOM0 device tree building messages are
-	  logged in the Xen ring buffer.
+	  logged in the Xen ring buffer. The device tree generated for each
+	  dom0less domU is dumped there as well, in a DTS-like format.
 	  If unsure, say N here.
 
 config SCRUB_DEBUG
xen/common/device-tree/Makefile +1 -0
index 9036e455d66a..959dd2a9fd30 100644
@@ -6,6 +6,7 @@ obj-$(CONFIG_DOMAIN_BUILD_HELPERS) += domain-build.init.o
 obj-$(filter $(CONFIG_DOM0LESS_BOOT),$(CONFIG_HAS_DEVICE_TREE_DISCOVERY)) += dom0less-build.init.o
 obj-$(CONFIG_DOM0LESS_BOOT) += dom0less-bindings.init.o
 obj-$(CONFIG_OVERLAY_DTB) += dt-overlay.o
+obj-$(CONFIG_DEVICE_TREE_DEBUG) += fdt-dump.init.o
 obj-$(CONFIG_HAS_DEVICE_TREE_DISCOVERY) += intc.o
 obj-$(CONFIG_DOMAIN_BUILD_HELPERS) += kernel.o
 obj-$(CONFIG_STATIC_EVTCHN) += static-evtchn.init.o
xen/common/device-tree/dom0less-build.c +4 -0
index fcbeb8adbd73..182201384faf 100644
@@ -1,5 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0-only */
 
+#include <xen/bootfdt.h>
 #include <xen/bootinfo.h>
 #include <xen/device_tree.h>
 #include <xen/dom0less-build.h>
@@ -587,6 +588,9 @@ static int __init prepare_dtb_domU(struct domain *d, struct kernel_info *kinfo)
     if ( ret < 0 )
         goto err;
 
+    dt_dprintk("Device tree for %pd:\n", d);
+    device_tree_dump(kinfo->fdt);
+
     return 0;
 
   err:
xen/common/device-tree/fdt-dump.c new file +180 -0

180 lines not on this page · raw message

xen/include/xen/bootfdt.h +13 -0
index 0e82ccea2f84..08983375209b 100644
@@ -99,6 +99,19 @@ int device_tree_for_each_node(const void *fdt, int node,
                               device_tree_node_func func,
                               void *data);
 
+/*
+ * device_tree_dump - print a device tree blob in a DTS-like format
+ * @fdt: flat device tree.
+ *
+ * The tree is printed at debug log level and is only meant to be used for
+ * debugging, so it is a no-op unless CONFIG_DEVICE_TREE_DEBUG is enabled.
+ */
+#ifdef CONFIG_DEVICE_TREE_DEBUG
+void device_tree_dump(const void *fdt);
+#else
+static inline void device_tree_dump(const void *fdt) {}
+#endif
+
 typedef enum {
     BOOTMOD_XEN,
     BOOTMOD_FDT,
Signature · 1 line
2.55.0
#2 lore raw

Re: [PATCH 1/2] xen/device-tree: add a helper to dump an FDT in DTS-like format

Quoted 10 lines · Oleksii Kurochko

The device tree Xen builds for a dom0less domain is only ever consumed by the guest, so a mistake in it surfaces as an obscure failure inside the domain, far away from the code which produced the tree.

Add device_tree_dump(). The output follows dtc's format so that a dump can be diffed against the source DTS or fed back to dtc; the value formatting heuristics are ported from dtc's utilfdt_print_data() for that reason. The walk itself goes through libfdt rather than decoding the tag stream by hand as dtc's fdtdump does.

Quoted 3 lines

Dumping a whole tree is only useful while debugging and costs both code size and a lot of console output, hence CONFIG_DEVICE_TREE_DEBUG. Mention the dump in the help text of the option.

We should also dump the DTB for dom0/hwdom in prepare_dtb_hwdom(). Please add that to this patch.

Quoted 16 lines
---
 xen/Kconfig.debug                       |   3 +-
 xen/common/device-tree/Makefile         |   1 +
 xen/common/device-tree/dom0less-build.c |   4 +
 xen/common/device-tree/fdt-dump.c       | 180 ++++++++++++++++++++++++
 xen/include/xen/bootfdt.h               |  13 ++
 5 files changed, 200 insertions(+), 1 deletion(-)
 create mode 100644 xen/common/device-tree/fdt-dump.c
xen/Kconfig.debug +0 -0
index d900d926c555..664f50bf8a96 100644

xen/Kconfig.debug config DEVICE_TREE_DEBUG · line 99

Quoted 6 lines
xen/Kconfig.debug +2 -1
@@ -95,7 +95,8 @@ config DEVICE_TREE_DEBUG
 	depends on DEVICE_TREE_PARSE
 	help
 	  Device tree parsing and DOM0 device tree building messages are
-	  logged in the Xen ring buffer.
+	  logged in the Xen ring buffer. The device tree generated for each
+	  dom0less domU is dumped there as well, in a DTS-like format.

I think we could have a separate option DEVICE_TREE_DUMP that depends on DEVICE_TREE_PARSE. Sometimes all we need is just to see how the generated DTB look like without seeing thousands of other DT debug messages. Especially that nothing in fdt-dump.c depends on DEVICE_TREE_DEBUG.

Quoted 27 lines
xen/Kconfig.debug +0 -0
 	  If unsure, say N here.
 
 config SCRUB_DEBUG
xen/common/device-tree/Makefile +1 -0
index 9036e455d66a..959dd2a9fd30 100644
@@ -6,6 +6,7 @@ obj-$(CONFIG_DOMAIN_BUILD_HELPERS) += domain-build.init.o
 obj-$(filter $(CONFIG_DOM0LESS_BOOT),$(CONFIG_HAS_DEVICE_TREE_DISCOVERY)) += dom0less-build.init.o
 obj-$(CONFIG_DOM0LESS_BOOT) += dom0less-bindings.init.o
 obj-$(CONFIG_OVERLAY_DTB) += dt-overlay.o
+obj-$(CONFIG_DEVICE_TREE_DEBUG) += fdt-dump.init.o
 obj-$(CONFIG_HAS_DEVICE_TREE_DISCOVERY) += intc.o
 obj-$(CONFIG_DOMAIN_BUILD_HELPERS) += kernel.o
 obj-$(CONFIG_STATIC_EVTCHN) += static-evtchn.init.o
xen/common/device-tree/dom0less-build.c +1 -0
index fcbeb8adbd73..182201384faf 100644
@@ -1,5 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0-only */
 
+#include <xen/bootfdt.h>
 #include <xen/bootinfo.h>
 #include <xen/device_tree.h>
 #include <xen/dom0less-build.h>

xen/common/device-tree/dom0less-build.c static int __init prepare_dtb_domU(struct domain *d, struct kernel_info *kinfo) · line 591

Quoted 4 lines
xen/common/device-tree/dom0less-build.c +1 -0
@@ -587,6 +588,9 @@ static int __init prepare_dtb_domU(struct domain *d, struct kernel_info *kinfo)
     if ( ret < 0 )
         goto err;
 
+    dt_dprintk("Device tree for %pd:\n", d);

Move this message to device_tree_dump.

Quoted 45 lines
xen/common/device-tree/dom0less-build.c +2 -0
+    device_tree_dump(kinfo->fdt);
+
     return 0;
 
   err:
xen/common/device-tree/fdt-dump.c new file +34 -0
new file mode 100644
index 000000000000..fc3d9eca5891
@@ -0,0 +1,180 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Dump a flattened device tree blob in a DTS-like format.
+ *
+ * is_printable_string() and print_value() are derived from
+ * util_is_printable_string() and utilfdt_print_data() in the device tree
+ * compiler (dtc).
+ *
+ * Copyright 2011 The Chromium Authors, All Rights Reserved.
+ * Copyright 2008 Jon Loeliger, Freescale Semiconductor, Inc.
+ * util_is_printable_string contributed by
+ *      Pantelis Antoniou <pantelis.antoniou AT gmail.com>
+ */
+
+#include <xen/bootfdt.h>
+#include <xen/ctype.h>
+#include <xen/init.h>
+#include <xen/lib.h>
+#include <xen/libfdt/libfdt.h>
+#include <xen/types.h>
+
+/* Deeper nodes are still printed, but not indented any further. */
+#define DUMP_MAX_INDENT_DEPTH 32
+
+/* Indentation, in characters, of a node at depth @depth. */
+static int __init indent_of(int depth)
+{
+    return 2 * min(depth, DUMP_MAX_INDENT_DEPTH);
+}
+
+static bool __init is_printable_string(const void *data, unsigned int len)
+{
+    const char *s = data;
+    const char *se = s + len;

xen/common/device-tree/fdt-dump.c line 44

Quoted 10 lines
xen/common/device-tree/fdt-dump.c new file +10 -0
+
+    /* A zero length property, or one which isn't NUL terminated, isn't one. */
+    if ( (len == 0) || (s[len - 1] != '\0') )
+        return false;
+
+    while ( s < se )
+    {
+        const char *ss = s;
+
+        while ( (s < se) && *s && isprint(*s) )

Xen's is_print() behaves differently than what dtc uses. You need to combine it with is_ascii. Otherwise 0xff would be marked as printable.

~Michal

#3 lore raw

Re: [PATCH 1/2] xen/device-tree: add a helper to dump an FDT in DTS-like format

Quoted 13 lines · Michal
Quoted 10 lines · Oleksii Kurochko

The device tree Xen builds for a dom0less domain is only ever consumed by the guest, so a mistake in it surfaces as an obscure failure inside the domain, far away from the code which produced the tree.

Add device_tree_dump(). The output follows dtc's format so that a dump can be diffed against the source DTS or fed back to dtc; the value formatting heuristics are ported from dtc's utilfdt_print_data() for that reason. The walk itself goes through libfdt rather than decoding the tag stream by hand as dtc's fdtdump does.

Quoted 5 lines
Quoted 3 lines

Dumping a whole tree is only useful while debugging and costs both code size and a lot of console output, hence CONFIG_DEVICE_TREE_DEBUG. Mention the dump in the help text of the option.

We should also dump the DTB for dom0/hwdom in prepare_dtb_hwdom(). Please add that to this patch.

It makes sense to me. I will add it.

Quoted 12 lines
Quoted 12 lines
---
  xen/Kconfig.debug                       |   3 +-
  xen/common/device-tree/Makefile         |   1 +
  xen/common/device-tree/dom0less-build.c |   4 +
  xen/common/device-tree/fdt-dump.c       | 180 ++++++++++++++++++++++++
  xen/include/xen/bootfdt.h               |  13 ++
  5 files changed, 200 insertions(+), 1 deletion(-)

create mode 100644 xen/common/device-tree/fdt-dump.c

Quoted 15 lines

xen/Kconfig.debug config DEVICE_TREE_DEBUG · line 99

Quoted 11 lines
xen/Kconfig.debug +2 -1
index d900d926c555..664f50bf8a96 100644
@@ -95,7 +95,8 @@ config DEVICE_TREE_DEBUG
  	depends on DEVICE_TREE_PARSE
  	help
  	  Device tree parsing and DOM0 device tree building messages are
-	  logged in the Xen ring buffer.
+	  logged in the Xen ring buffer. The device tree generated for each
+	  dom0less domU is dumped there as well, in a DTS-like format.

I think we could have a separate option DEVICE_TREE_DUMP that depends on DEVICE_TREE_PARSE. Sometimes all we need is just to see how the generated DTB look like without seeing thousands of other DT debug messages. Especially that nothing in fdt-dump.c depends on DEVICE_TREE_DEBUG.

Agree, it will be better to have DEVICE_TREE_DUMP. I will introduce the following config:

config DEVICE_TREE_DUMP
bool "Dump generated device trees"
depends on DEVICE_TREE_PARSE
help
The device tree Xen generates for dom0/hwdom and for each om0less domain is printed on the Xen console in a DTS-like format. The dump is logged at debug level, so on a non-debug build it is only shown with "loglvl=all".
If unsure, say N here.

Quoted 31 lines
Quoted 31 lines
xen/Kconfig.debug +0 -0
  	  If unsure, say N here.
 
  config SCRUB_DEBUG
xen/common/device-tree/Makefile +1 -0
index 9036e455d66a..959dd2a9fd30 100644
@@ -6,6 +6,7 @@ obj-$(CONFIG_DOMAIN_BUILD_HELPERS) += domain-build.init.o
  obj-$(filter $(CONFIG_DOM0LESS_BOOT),$(CONFIG_HAS_DEVICE_TREE_DISCOVERY)) += dom0less-build.init.o
  obj-$(CONFIG_DOM0LESS_BOOT) += dom0less-bindings.init.o
  obj-$(CONFIG_OVERLAY_DTB) += dt-overlay.o
+obj-$(CONFIG_DEVICE_TREE_DEBUG) += fdt-dump.init.o
  obj-$(CONFIG_HAS_DEVICE_TREE_DISCOVERY) += intc.o
  obj-$(CONFIG_DOMAIN_BUILD_HELPERS) += kernel.o
  obj-$(CONFIG_STATIC_EVTCHN) += static-evtchn.init.o
xen/common/device-tree/dom0less-build.c +1 -0
index fcbeb8adbd73..182201384faf 100644
@@ -1,5 +1,6 @@
  /* SPDX-License-Identifier: GPL-2.0-only */
 
+#include <xen/bootfdt.h>
  #include <xen/bootinfo.h>
  #include <xen/device_tree.h>
  #include <xen/dom0less-build.h>
@@ -587,6 +588,9 @@ static int __init prepare_dtb_domU(struct domain *d, struct kernel_info *kinfo)
      if ( ret < 0 )
          goto err;
 
Quoted 3 lines

xen/common/device-tree/dom0less-build.c static int __init prepare_dtb_domU(struct domain *d, struct kernel_info *kinfo) · line 591

Quoted 1 line
xen/common/device-tree/dom0less-build.c +1 -0
+    dt_dprintk("Device tree for %pd:\n", d);

Move this message to device_tree_dump.

I'll move.

Quoted 49 lines
Quoted 49 lines
xen/common/device-tree/dom0less-build.c +2 -0
+    device_tree_dump(kinfo->fdt);
+
      return 0;
 
    err:
xen/common/device-tree/fdt-dump.c new file +38 -0
new file mode 100644
index 000000000000..fc3d9eca5891
@@ -0,0 +1,180 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Dump a flattened device tree blob in a DTS-like format.
+ *
+ * is_printable_string() and print_value() are derived from
+ * util_is_printable_string() and utilfdt_print_data() in the device tree
+ * compiler (dtc).
+ *
+ * Copyright 2011 The Chromium Authors, All Rights Reserved.
+ * Copyright 2008 Jon Loeliger, Freescale Semiconductor, Inc.
+ * util_is_printable_string contributed by
+ *      Pantelis Antoniou <pantelis.antoniou AT gmail.com>
+ */
+
+#include <xen/bootfdt.h>
+#include <xen/ctype.h>
+#include <xen/init.h>
+#include <xen/lib.h>
+#include <xen/libfdt/libfdt.h>
+#include <xen/types.h>
+
+/* Deeper nodes are still printed, but not indented any further. */
+#define DUMP_MAX_INDENT_DEPTH 32
+
+/* Indentation, in characters, of a node at depth @depth. */
+static int __init indent_of(int depth)
+{
+    return 2 * min(depth, DUMP_MAX_INDENT_DEPTH);
+}
+
+static bool __init is_printable_string(const void *data, unsigned int len)
+{
+    const char *s = data;
+    const char *se = s + len;
+
+    /* A zero length property, or one which isn't NUL terminated, isn't one. */
+    if ( (len == 0) || (s[len - 1] != '\0') )
+        return false;
Quoted 8 lines

xen/common/device-tree/fdt-dump.c line 44

Quoted 6 lines
xen/common/device-tree/fdt-dump.c new file +6 -0
+
+    while ( s < se )
+    {
+        const char *ss = s;
+
+        while ( (s < se) && *s && isprint(*s) )

Xen's is_print() behaves differently than what dtc uses. You need to combine it with is_ascii. Otherwise 0xff would be marked as printable.

I'll add the is_ascii() before isprint():

     /*
      * Xen's isprint() also accepts the Latin-1 characters 0xa0-0xff,
      * while the one dtc uses only accepts ASCII ones.
      */
     while ( (s < se) && *s && isascii(*s) && isprint(*s) )
        ...

Thanks for review!

~ Oleksii

#4 lore raw

PATCH 2/2 xen/device-tree: move discard_initial_modules() to common code

In reply to Oleksii Kurochko

Everything discard_initial_modules() touches is already common code:
bootinfo.modules, using_static_heap and fw_unreserved_regions(). Move it out of Arm into common/device-tree/bootinfo.c, next to the boot allocator handling of the same memory, and declare it in xen/bootinfo.h, so that other device-tree based architectures can reuse it instead of growing a copy of their own.

The arch-specific remove_early_mappings() is not moved along with it:
how the early FDT mapping is torn down is up to the architecture. As the DTB is one of the boot modules being released, the mapping has to go once they are discarded, so call it from start_xen() right after discard_initial_modules(), which keeps the common function free of any arch dependency.

While moving it, use unsigned int for the loop counter and replace the "goto out" by a plain return, as there is nothing left to do at the label.

No functional change.

---
 xen/arch/arm/include/asm/setup.h  |  2 --
 xen/arch/arm/setup.c              | 35 ++-----------------------------
 xen/common/device-tree/bootinfo.c | 30 ++++++++++++++++++++++++++
 xen/include/xen/bootinfo.h        |  8 +++++++
 4 files changed, 40 insertions(+), 35 deletions(-)
xen/arch/arm/include/asm/setup.h +0 -2
index c491c567298f..02fdf2b56c9c 100644
@@ -40,8 +40,6 @@ int acpi_make_efi_nodes(void *fdt, struct membank tbl_add[]);
 
 void create_dom0(void);
 
-void discard_initial_modules(void);
-
 void init_pdx(void);
 void setup_mm(void);
 
xen/arch/arm/setup.c +2 -33
index 16f899dff57b..a65c3d2a655e 100644
@@ -210,39 +210,6 @@ static void __init processor_id(void)
     processor_setup();
 }
 
-void __init discard_initial_modules(void)
-{
-    struct boot_modules *mi = &bootinfo.modules;
-    int i;
-
-    /*
-     * When using static heap feature, don't give boot_modules memory back to
-     * the heap allocator
-     */
-    if ( using_static_heap )
-        goto out;
-
-    for ( i = 0; i < mi->nr_mods; i++ )
-    {
-        paddr_t s = mi->module[i].start;
-        paddr_t e = s + PAGE_ALIGN(mi->module[i].size);
-
-        if ( mi->module[i].kind == BOOTMOD_XEN )
-            continue;
-
-        if ( !mfn_valid(maddr_to_mfn(s)) ||
-             !mfn_valid(maddr_to_mfn(e)) )
-            continue;
-
-        fw_unreserved_regions(s, e, init_domheap_pages, 0);
-    }
-
-    mi->nr_mods = 0;
-
- out:
-    remove_early_mappings();
-}
-
 /* Relocate the FDT in Xen heap */
 static void __init relocate_fdt(const void **dtb_vaddr, size_t dtb_size)
 {
@@ -509,6 +476,8 @@ void asmlinkage __init noreturn start_xen(unsigned long fdt_paddr)
      */
     discard_initial_modules();
 
+    remove_early_mappings();
+
     heap_init_late();
 
     init_constructors();
xen/common/device-tree/bootinfo.c +30 -0
index c637c45ad594..6772bf2df002 100644
@@ -471,6 +471,36 @@ void __init populate_boot_allocator(void)
     }
 }
 
+void __init discard_initial_modules(void)
+{
+    struct boot_modules *mi = &bootinfo.modules;
+    unsigned int i;
+
+    /*
+     * When using static heap feature, don't give boot_modules memory back to
+     * the heap allocator
+     */
+    if ( using_static_heap )
+        return;
+
+    for ( i = 0; i < mi->nr_mods; i++ )
+    {
+        paddr_t s = mi->module[i].start;
+        paddr_t e = s + PAGE_ALIGN(mi->module[i].size);
+
+        if ( mi->module[i].kind == BOOTMOD_XEN )
+            continue;
+
+        if ( !mfn_valid(maddr_to_mfn(s)) ||
+             !mfn_valid(maddr_to_mfn(e)) )
+            continue;
+
+        fw_unreserved_regions(s, e, init_domheap_pages, 0);
+    }
+
+    mi->nr_mods = 0;
+}
+
 /*
  * Local variables:
  * mode: C
xen/include/xen/bootinfo.h +8 -0
index dbf492c2e36e..60ef25f3cf83 100644
@@ -214,4 +214,12 @@ void fw_unreserved_regions(paddr_t s, paddr_t e,
                            void (*cb)(paddr_t ps, paddr_t pe),
                            unsigned int first);
 
+/*
+ * Return the memory occupied by the boot modules to the heap allocator.
+ *
+ * It needs to be called **before** heap_init_late() so that the released
+ * modules are scrubbed (unless suppressed).
+ */
+void discard_initial_modules(void);
+
 #endif /* XEN_BOOTINFO_H */
Signature · 1 line
2.55.0
Reviewed-by on v1 2/2
#5 lore raw

Re: [PATCH 2/2] xen/device-tree: move discard_initial_modules() to common code

Quoted 85 lines · Oleksii Kurochko

Everything discard_initial_modules() touches is already common code:
bootinfo.modules, using_static_heap and fw_unreserved_regions(). Move it out of Arm into common/device-tree/bootinfo.c, next to the boot allocator handling of the same memory, and declare it in xen/bootinfo.h, so that other device-tree based architectures can reuse it instead of growing a copy of their own.

The arch-specific remove_early_mappings() is not moved along with it:
how the early FDT mapping is torn down is up to the architecture. As the DTB is one of the boot modules being released, the mapping has to go once they are discarded, so call it from start_xen() right after discard_initial_modules(), which keeps the common function free of any arch dependency.

While moving it, use unsigned int for the loop counter and replace the "goto out" by a plain return, as there is nothing left to do at the label.

No functional change.

---
 xen/arch/arm/include/asm/setup.h  |  2 --
 xen/arch/arm/setup.c              | 35 ++-----------------------------
 xen/common/device-tree/bootinfo.c | 30 ++++++++++++++++++++++++++
 xen/include/xen/bootinfo.h        |  8 +++++++
 4 files changed, 40 insertions(+), 35 deletions(-)
xen/arch/arm/include/asm/setup.h +0 -2
index c491c567298f..02fdf2b56c9c 100644
@@ -40,8 +40,6 @@ int acpi_make_efi_nodes(void *fdt, struct membank tbl_add[]);
 
 void create_dom0(void);
 
-void discard_initial_modules(void);
-
 void init_pdx(void);
 void setup_mm(void);
 
xen/arch/arm/setup.c +0 -33
index 16f899dff57b..a65c3d2a655e 100644
@@ -210,39 +210,6 @@ static void __init processor_id(void)
     processor_setup();
 }
 
-void __init discard_initial_modules(void)
-{
-    struct boot_modules *mi = &bootinfo.modules;
-    int i;
-
-    /*
-     * When using static heap feature, don't give boot_modules memory back to
-     * the heap allocator
-     */
-    if ( using_static_heap )
-        goto out;
-
-    for ( i = 0; i < mi->nr_mods; i++ )
-    {
-        paddr_t s = mi->module[i].start;
-        paddr_t e = s + PAGE_ALIGN(mi->module[i].size);
-
-        if ( mi->module[i].kind == BOOTMOD_XEN )
-            continue;
-
-        if ( !mfn_valid(maddr_to_mfn(s)) ||
-             !mfn_valid(maddr_to_mfn(e)) )
-            continue;
-
-        fw_unreserved_regions(s, e, init_domheap_pages, 0);
-    }
-
-    mi->nr_mods = 0;
-
- out:
-    remove_early_mappings();
-}
-
 /* Relocate the FDT in Xen heap */
 static void __init relocate_fdt(const void **dtb_vaddr, size_t dtb_size)
 {

xen/arch/arm/setup.c void asmlinkage __init noreturn start_xen(unsigned long fdt_paddr) · line 477

Quoted 2 lines
xen/arch/arm/setup.c +0 -0
@@ -509,6 +476,8 @@ void asmlinkage __init noreturn start_xen(unsigned long fdt_paddr)
      */
     discard_initial_modules();

The comment above is now duplicated with the one you added in xen/bootinfo.h. I'd drop it from here (can be done on commit).

Other than that:

NIT for the future: if you send a series of patches, please move the fixes/code movement changes at the beginning of the series.

~Michal

#6 lore raw

Re: [PATCH 2/2] xen/device-tree: move discard_initial_modules() to common code

Quoted 84 lines · Michal
Quoted 81 lines · Oleksii Kurochko

Everything discard_initial_modules() touches is already common code:
bootinfo.modules, using_static_heap and fw_unreserved_regions(). Move it out of Arm into common/device-tree/bootinfo.c, next to the boot allocator handling of the same memory, and declare it in xen/bootinfo.h, so that other device-tree based architectures can reuse it instead of growing a copy of their own.

The arch-specific remove_early_mappings() is not moved along with it:
how the early FDT mapping is torn down is up to the architecture. As the DTB is one of the boot modules being released, the mapping has to go once they are discarded, so call it from start_xen() right after discard_initial_modules(), which keeps the common function free of any arch dependency.

While moving it, use unsigned int for the loop counter and replace the "goto out" by a plain return, as there is nothing left to do at the label.

No functional change.

---
  xen/arch/arm/include/asm/setup.h  |  2 --
  xen/arch/arm/setup.c              | 35 ++-----------------------------
  xen/common/device-tree/bootinfo.c | 30 ++++++++++++++++++++++++++
  xen/include/xen/bootinfo.h        |  8 +++++++
  4 files changed, 40 insertions(+), 35 deletions(-)
xen/arch/arm/include/asm/setup.h +0 -2
index c491c567298f..02fdf2b56c9c 100644
@@ -40,8 +40,6 @@ int acpi_make_efi_nodes(void *fdt, struct membank tbl_add[]);
 
  void create_dom0(void);
 
-void discard_initial_modules(void);
-
  void init_pdx(void);
  void setup_mm(void);
 
xen/arch/arm/setup.c +0 -33
index 16f899dff57b..a65c3d2a655e 100644
@@ -210,39 +210,6 @@ static void __init processor_id(void)
      processor_setup();
  }
 
-void __init discard_initial_modules(void)
-{
-    struct boot_modules *mi = &bootinfo.modules;
-    int i;
-
-    /*
-     * When using static heap feature, don't give boot_modules memory back to
-     * the heap allocator
-     */
-    if ( using_static_heap )
-        goto out;
-
-    for ( i = 0; i < mi->nr_mods; i++ )
-    {
-        paddr_t s = mi->module[i].start;
-        paddr_t e = s + PAGE_ALIGN(mi->module[i].size);
-
-        if ( mi->module[i].kind == BOOTMOD_XEN )
-            continue;
-
-        if ( !mfn_valid(maddr_to_mfn(s)) ||
-             !mfn_valid(maddr_to_mfn(e)) )
-            continue;
-
-        fw_unreserved_regions(s, e, init_domheap_pages, 0);
-    }
-
-    mi->nr_mods = 0;
-
- out:
-    remove_early_mappings();
-}
-
Quoted 8 lines

xen/arch/arm/setup.c void asmlinkage __init noreturn start_xen(unsigned long fdt_paddr) · line 477

Quoted 6 lines
xen/arch/arm/setup.c +0 -0
  /* Relocate the FDT in Xen heap */
  static void __init relocate_fdt(const void **dtb_vaddr, size_t dtb_size)
  {
@@ -509,6 +476,8 @@ void asmlinkage __init noreturn start_xen(unsigned long fdt_paddr)
       */
      discard_initial_modules();

The comment above is now duplicated with the one you added in xen/bootinfo.h. I'd drop it from here (can be done on commit).

I will drop it in v2 (if it won't merged at the moment when I will start address the comments for this patch series).

Quoted 3 lines

Other than that:

Thanks!

Quoted 4 lines

NIT for the future: if you send a series of patches, please move the fixes/code movement changes at the beginning of the series.

I will take into account that in the future.

~ Oleksii