Skip to contentSkip to conversation

xen/arm: dt overlay fixes

PATCH In review Michal Orzel 3 participants 11 messages Oct 4, 2024 – Oct 10, 2024

lore
Share & tools
lore
#0 lore raw

v2 cover xen/arm: dt overlay fixes

This series contains fixes and improvements after doing some tests with DT overlay support in Xen. I split the series so that each patch handles a separate issue.

Michal Orzel (6):
dt-overlay: Fix NULL pointer dereference
device-tree: Remove __init from unflatten_dt_alloc()
dt-overlay: Remove ASSERT_UNREACHABLE from add_nodes()
dt-overlay: Support target-path being root node
dt-overlay: Ignore nodes that do not have __overlay__ as their subnode
dt-overlay: Print overlay I/O memory ranges in hex

 xen/common/device-tree/device-tree.c |  4 +--
 xen/common/dt-overlay.c              | 46 +++++++++++++++-------------
 2 files changed, 26 insertions(+), 24 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH v2 1/6 dt-overlay: Fix NULL pointer dereference

Attempt to attach an overlay (xl dt-overlay attach) to a domain without first adding this overlay to Xen (xl dt-overlay add) results in an overlay track entry being NULL in handle_attach_overlay_nodes(). This leads to NULL pointer dereference and the following data abort crash:

(XEN) Cannot find any matching tracker with input dtbo. Operation is supported only for prior added dtbo. (XEN) Data Abort Trap. Syndrome=0x5
(XEN) Walking Hypervisor VA 0x40 on CPU0 via TTBR 0x0000000046948000
(XEN) 0TH[0x000] = 0x46940f7f
(XEN) 1ST[0x000] = 0x0
(XEN) CPU0: Unexpected Trap: Data Abort
(XEN) ----[ Xen-4.20-unstable arm64 debug=y Not tainted ]----
...
(XEN) Xen call trace:

(XEN)    [<00000a0000208b30>] dt_overlay_domctl+0x304/0x370 (PC)
(XEN)    [<00000a0000208b30>] dt_overlay_domctl+0x304/0x370 (LR)
(XEN)    [<00000a0000274b7c>] arch_do_domctl+0x48/0x328
Fixes 4c733873b5c2 ("xen/arm: Add XEN_DOMCTL_dt_overlay and device attachment to domains")

---
Changes in v2:
- Add Rb

---
 xen/common/dt-overlay.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
xen/common/dt-overlay.c +5 -2
index d53b4706cd2f..8606b14d1e8e 100644
@@ -908,8 +908,11 @@ static long handle_attach_overlay_nodes(struct domain *d,
  out:
     spin_unlock(&overlay_lock);
 
-    rangeset_destroy(entry->irq_ranges);
-    rangeset_destroy(entry->iomem_ranges);
+    if ( entry )
+    {
+        rangeset_destroy(entry->irq_ranges);
+        rangeset_destroy(entry->iomem_ranges);
+    }
 
     return rc;
 }
Signature · 1 line
2.25.1
#2 lore raw

PATCH v2 2/6 device-tree: Remove __init from unflatten_dt_alloc()

In reply to Michal Orzel

With CONFIG_OVERLAY_DTB=y, unflatten_dt_alloc() is used as part of unflatten_dt_node() used during runtime. In case of a binary compiled such as unflatten_dt_alloc() does not get inlined (e.g. using -Og), attempt to add an overlay to Xen (xl dt-overlay add) results in a crash.

(XEN) Instruction Abort Trap. Syndrome=0x7
(XEN) Walking Hypervisor VA 0xa00002c8cc0 on CPU2 via TTBR 0x0000000040340000 (XEN) 0TH[0x014] = 0x4033ff7f
(XEN) 1ST[0x000] = 0x4033ef7f
(XEN) 2ND[0x001] = 0x4000004033af7f
(XEN) 3RD[0x0c8] = 0x0
(XEN) CPU2: Unexpected Trap: Instruction Abort
(XEN) ----[ Xen-4.20-unstable arm64 debug=y Not tainted ]----
...
(XEN) Xen call trace:

(XEN)    [<00000a00002c8cc0>] 00000a00002c8cc0 (PC)
(XEN)    [<00000a0000202410>] device-tree.c#unflatten_dt_node+0xd0/0x504 (LR)
(XEN)    [<00000a0000204484>] unflatten_device_tree+0x54/0x1a0
(XEN)    [<00000a000020800c>] dt-overlay.c#handle_add_overlay_nodes+0x290/0x3d4
(XEN)    [<00000a0000208360>] dt_overlay_sysctl+0x8c/0x110
(XEN)    [<00000a000027714c>] arch_do_sysctl+0x1c/0x2c
Fixes 9e9d2c079dc4 ("xen/arm/device: Remove __init from function type")

---
Changes in v2:
- Add Rb

---
 xen/common/device-tree/device-tree.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
xen/common/device-tree/device-tree.c +2 -2
index 8d1017a49d80..d0528c582565 100644
@@ -120,8 +120,8 @@ void dt_child_set_range(__be32 **cellp, int addrcells, int sizecells,
     dt_set_cell(cellp, sizecells, size);
 }
 
-static void __init *unflatten_dt_alloc(unsigned long *mem, unsigned long size,
-                                       unsigned long align)
+static void *unflatten_dt_alloc(unsigned long *mem, unsigned long size,
+                                unsigned long align)
 {
     void *res;
 
Signature · 1 line
2.25.1
#3 lore raw

PATCH v2 3/6 dt-overlay: Remove ASSERT_UNREACHABLE from add_nodes()

In reply to Michal Orzel

The assumption stated in the comment that the code will never get there is incorrect. In overlay_get_nodes_info() we manually combine path from target-path property with the node path by adding '/' as a separator. This can differ from a path obtained by libfdt due to more advanced logic used there which can for instance get rid of excessive slashes. In case of incorrect target-path (e.g. target-path = "//axi"), the comparison in dt_find_node_by_path_from() can fail triggering the assert in debug builds.

Fixes 0c0facdab6f5 ("xen/arm: Implement device tree node addition functionalities")

---
Changes in v2:
- Add Rb,Ab

---
 xen/common/dt-overlay.c | 4 ----
 1 file changed, 4 deletions(-)
xen/common/dt-overlay.c +0 -4
index 8606b14d1e8e..d18bd12bd38d 100644
@@ -596,11 +596,7 @@ static long add_nodes(struct overlay_track *tr, char **nodes_full_path)
         overlay_node = dt_find_node_by_path_from(tr->dt_host_new,
                                                  nodes_full_path[j]);
         if ( overlay_node == NULL )
-        {
-            /* Sanity check. But code will never come here. */
-            ASSERT_UNREACHABLE();
             return -EFAULT;
-        }
 
         /*
          * Find previous and next node to overlay_node in dt_host_new. We will
Signature · 1 line
2.25.1
#4 lore raw

PATCH v2 4/6 dt-overlay: Support target-path being root node

In reply to Michal Orzel

Even though in most cases device nodes are not present directly under the root node, it's a perfectly valid configuration (e.g. Qemu virt machine dtb). At the moment, we don't handle this scenario which leads to unconditional addition of extra leading '/' in the node full path. This makes the attempt to add such device overlay to fail.

---
Changes in v2:
- Use ?: instead of implicit bool->int conversion

---
 xen/common/dt-overlay.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
xen/common/dt-overlay.c +11 -5
index d18bd12bd38d..bfa153250922 100644
@@ -325,6 +325,7 @@ static int overlay_get_nodes_info(const void *fdto, char **nodes_full_path)
             int node_name_len;
             unsigned int target_path_len = strlen(target_path);
             unsigned int node_full_name_len;
+            unsigned int extra_len;
 
             node_name = fdt_get_name(fdto, subnode, &node_name_len);
 
@@ -332,10 +333,13 @@ static int overlay_get_nodes_info(const void *fdto, char **nodes_full_path)
                 return node_name_len;
 
             /*
-             * Magic number 2 is for adding '/' and '\0'. This is done to keep
-             * the node_full_path in the correct full node name format.
+             * Extra length is for adding '/' and '\0' unless the target path is
+             * root in which case we don't add the '/' at the beginning. This is
+             * done to keep the node_full_path in the correct full node name
+             * format.
              */
-            node_full_name_len = target_path_len + node_name_len + 2;
+            extra_len = (target_path_len > 1) ? 2 : 1;
+            node_full_name_len = target_path_len + node_name_len + extra_len;
 
             nodes_full_path[node_num] = xmalloc_bytes(node_full_name_len);
 
@@ -344,9 +348,11 @@ static int overlay_get_nodes_info(const void *fdto, char **nodes_full_path)
 
             memcpy(nodes_full_path[node_num], target_path, target_path_len);
 
-            nodes_full_path[node_num][target_path_len] = '/';
+            /* Target is not root - add separator */
+            if ( target_path_len > 1 )
+                nodes_full_path[node_num][target_path_len++] = '/';
 
-            memcpy(nodes_full_path[node_num] + target_path_len + 1,
+            memcpy(nodes_full_path[node_num] + target_path_len,
                     node_name, node_name_len);
 
             nodes_full_path[node_num][node_full_name_len - 1] = '\0';
Signature · 1 line
2.25.1
Reviewed-by on v2 4/6
#5 lore raw

Re: [PATCH v2 4/6] dt-overlay: Support target-path being root node

Quoted 7 lines · Michal Orzel

Even though in most cases device nodes are not present directly under the root node, it's a perfectly valid configuration (e.g. Qemu virt machine dtb). At the moment, we don't handle this scenario which leads to unconditional addition of extra leading '/' in the node full path. This makes the attempt to add such device overlay to fail.

Quoted 53 lines

---
Changes in v2:
- Use ?: instead of implicit bool->int conversion

---
 xen/common/dt-overlay.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
xen/common/dt-overlay.c +11 -5
index d18bd12bd38d..bfa153250922 100644
@@ -325,6 +325,7 @@ static int overlay_get_nodes_info(const void *fdto, char **nodes_full_path)
             int node_name_len;
             unsigned int target_path_len = strlen(target_path);
             unsigned int node_full_name_len;
+            unsigned int extra_len;
 
             node_name = fdt_get_name(fdto, subnode, &node_name_len);
 
@@ -332,10 +333,13 @@ static int overlay_get_nodes_info(const void *fdto, char **nodes_full_path)
                 return node_name_len;
 
             /*
-             * Magic number 2 is for adding '/' and '\0'. This is done to keep
-             * the node_full_path in the correct full node name format.
+             * Extra length is for adding '/' and '\0' unless the target path is
+             * root in which case we don't add the '/' at the beginning. This is
+             * done to keep the node_full_path in the correct full node name
+             * format.
              */
-            node_full_name_len = target_path_len + node_name_len + 2;
+            extra_len = (target_path_len > 1) ? 2 : 1;
+            node_full_name_len = target_path_len + node_name_len + extra_len;
 
             nodes_full_path[node_num] = xmalloc_bytes(node_full_name_len);
 
@@ -344,9 +348,11 @@ static int overlay_get_nodes_info(const void *fdto, char **nodes_full_path)
 
             memcpy(nodes_full_path[node_num], target_path, target_path_len);
 
-            nodes_full_path[node_num][target_path_len] = '/';
+            /* Target is not root - add separator */
+            if ( target_path_len > 1 )
+                nodes_full_path[node_num][target_path_len++] = '/';
 
-            memcpy(nodes_full_path[node_num] + target_path_len + 1,
+            memcpy(nodes_full_path[node_num] + target_path_len,
                     node_name, node_name_len);
 
             nodes_full_path[node_num][node_full_name_len - 1] = '\0';
Signature · 1 line
2.25.1
#6 lore raw

PATCH v2 5/6 dt-overlay: Ignore nodes that do not have __overlay__ as their subnode

In reply to Michal Orzel

Assumption stated in the comments as if fdt_for_each_subnode() checks for parent < 0 is utterly wrong. If parent is < 0, node offset is set to 0 (i.e. the very first node in the tree) and the loop's body is executed. This incorrect assumption causes overlay_node_count() to also count nodes that do not have __overlay__ as their subnode. The same story goes for overlay_get_nodes_info(), where we end up requiring each node directly under root node to have "target-path" set. DTBOs can specify other nodes including special ones like __symbols__, __fixups__ that can be left to reduce the number of steps a user needs to do to when it comes to invalid phandles.

Fix it by adding checks if overlay < 0 after respective calls to fdt_subnode_offset().

---
Changes in v2:
- New patch

---
 xen/common/dt-overlay.c | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)
xen/common/dt-overlay.c +6 -10
index bfa153250922..4d75b5b36a99 100644
@@ -274,11 +274,9 @@ static unsigned int overlay_node_count(const void *overlay_fdt)
         int overlay;
 
         overlay = fdt_subnode_offset(overlay_fdt, fragment, "__overlay__");
+        if ( overlay < 0 )
+            continue;
 
-        /*
-         * overlay value can be < 0. But fdt_for_each_subnode() loop checks for
-         * overlay >= 0. So, no need for a overlay>=0 check here.
-         */
         fdt_for_each_subnode(subnode, overlay_fdt, overlay)
         {
             num_overlay_nodes++;
@@ -305,6 +303,10 @@ static int overlay_get_nodes_info(const void *fdto, char **nodes_full_path)
         int subnode;
         const char *target_path;
 
+        overlay = fdt_subnode_offset(fdto, fragment, "__overlay__");
+        if ( overlay < 0 )
+            continue;
+
         target = fdt_overlay_target_offset(device_tree_flattened, fdto,
                                            fragment, &target_path);
         if ( target < 0 )
@@ -313,12 +315,6 @@ static int overlay_get_nodes_info(const void *fdto, char **nodes_full_path)
         if ( target_path == NULL )
             return -EINVAL;
 
-        overlay = fdt_subnode_offset(fdto, fragment, "__overlay__");
-
-        /*
-         * overlay value can be < 0. But fdt_for_each_subnode() loop checks for
-         * overlay >= 0. So, no need for a overlay>=0 check here.
-         */
         fdt_for_each_subnode(subnode, fdto, overlay)
         {
             const char *node_name = NULL;
Signature · 1 line
2.25.1
Reviewed-by on v2 5/6
#7 lore raw

Re: [PATCH v2 5/6] dt-overlay: Ignore nodes that do not have __overlay__ as their subnode

Quoted 11 lines · Michal Orzel

Assumption stated in the comments as if fdt_for_each_subnode() checks for parent < 0 is utterly wrong. If parent is < 0, node offset is set to 0 (i.e. the very first node in the tree) and the loop's body is executed. This incorrect assumption causes overlay_node_count() to also count nodes that do not have __overlay__ as their subnode. The same story goes for overlay_get_nodes_info(), where we end up requiring each node directly under root node to have "target-path" set. DTBOs can specify other nodes including special ones like __symbols__, __fixups__ that can be left to reduce the number of steps a user needs to do to when it comes to invalid phandles.

Quoted 4 lines

Fix it by adding checks if overlay < 0 after respective calls to fdt_subnode_offset().

Good catch!

Quoted 52 lines

---
Changes in v2:
- New patch

---
 xen/common/dt-overlay.c | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)
xen/common/dt-overlay.c +6 -10
index bfa153250922..4d75b5b36a99 100644
@@ -274,11 +274,9 @@ static unsigned int overlay_node_count(const void *overlay_fdt)
         int overlay;
 
         overlay = fdt_subnode_offset(overlay_fdt, fragment, "__overlay__");
+        if ( overlay < 0 )
+            continue;
 
-        /*
-         * overlay value can be < 0. But fdt_for_each_subnode() loop checks for
-         * overlay >= 0. So, no need for a overlay>=0 check here.
-         */
         fdt_for_each_subnode(subnode, overlay_fdt, overlay)
         {
             num_overlay_nodes++;
@@ -305,6 +303,10 @@ static int overlay_get_nodes_info(const void *fdto, char **nodes_full_path)
         int subnode;
         const char *target_path;
 
+        overlay = fdt_subnode_offset(fdto, fragment, "__overlay__");
+        if ( overlay < 0 )
+            continue;
+
         target = fdt_overlay_target_offset(device_tree_flattened, fdto,
                                            fragment, &target_path);
         if ( target < 0 )
@@ -313,12 +315,6 @@ static int overlay_get_nodes_info(const void *fdto, char **nodes_full_path)
         if ( target_path == NULL )
             return -EINVAL;
 
-        overlay = fdt_subnode_offset(fdto, fragment, "__overlay__");
-
-        /*
-         * overlay value can be < 0. But fdt_for_each_subnode() loop checks for
-         * overlay >= 0. So, no need for a overlay>=0 check here.
-         */
         fdt_for_each_subnode(subnode, fdto, overlay)
         {
             const char *node_name = NULL;
Signature · 1 line
2.25.1
#8 lore raw

PATCH v2 6/6 dt-overlay: Print overlay I/O memory ranges in hex

In reply to Michal Orzel

Printing I/O memory rangeset ranges in decimal is not very helpful when debugging, so switch to hex by adding RANGESETF_prettyprint_hex flag for iomem_ranges rangeset.

---
Changes in v2:
- New patch

---
 xen/common/dt-overlay.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
xen/common/dt-overlay.c +2 -1
index 4d75b5b36a99..5ce00514ef14 100644
@@ -869,7 +869,8 @@ static long handle_attach_overlay_nodes(struct domain *d,
         goto out;
     }
 
-    entry->iomem_ranges = rangeset_new(d, "Overlay: I/O Memory", 0);
+    entry->iomem_ranges = rangeset_new(d, "Overlay: I/O Memory",
+                                       RANGESETF_prettyprint_hex);
     if (entry->iomem_ranges == NULL)
     {
         rc = -ENOMEM;
Signature · 1 line
2.25.1
Acked-by on v2 6/6
#9 lore raw

Re: [PATCH v2 6/6] dt-overlay: Print overlay I/O memory ranges in hex

Quoted 5 lines · Michal Orzel

Printing I/O memory rangeset ranges in decimal is not very helpful when debugging, so switch to hex by adding RANGESETF_prettyprint_hex flag for iomem_ranges rangeset.

Quoted 24 lines

---
Changes in v2:
- New patch

---
 xen/common/dt-overlay.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
xen/common/dt-overlay.c +2 -1
index 4d75b5b36a99..5ce00514ef14 100644
@@ -869,7 +869,8 @@ static long handle_attach_overlay_nodes(struct domain *d,
         goto out;
     }
 
-    entry->iomem_ranges = rangeset_new(d, "Overlay: I/O Memory", 0);
+    entry->iomem_ranges = rangeset_new(d, "Overlay: I/O Memory",
+                                       RANGESETF_prettyprint_hex);
     if (entry->iomem_ranges == NULL)
     {
         rc = -ENOMEM;
Signature · 1 line
2.25.1
#10 lore raw
In reply to Michal Orzel

Hi Michal,

Quoted 11 lines · Michal Orzel

This series contains fixes and improvements after doing some tests with DT overlay support in Xen. I split the series so that each patch handles a separate issue.

Michal Orzel (6):
dt-overlay: Fix NULL pointer dereference
device-tree: Remove __init from unflatten_dt_alloc()
dt-overlay: Remove ASSERT_UNREACHABLE from add_nodes()
dt-overlay: Support target-path being root node
dt-overlay: Ignore nodes that do not have __overlay__ as their subnode
dt-overlay: Print overlay I/O memory ranges in hex

This is now committed.

Cheers,

Signature · 1 line
Julien Grall