Skip to contentSkip to conversation

xen/arm: dt overlay fixes

PATCH Superseded Michal Orzel 3 participants 18 messages Sep 19, 2024 – Oct 3, 2024

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover xen/arm: dt overlay fixes

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

Michal Orzel (4):
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

 xen/common/device-tree/device-tree.c |  4 ++--
 xen/common/dt-overlay.c              | 27 ++++++++++++++++-----------
 2 files changed, 18 insertions(+), 13 deletions(-)
Signature · 1 line
2.37.6
#1 lore raw

PATCH 1/4 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")
---
 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.37.6
Reviewed-by on v1 1/4
#2 lore raw

Re: [PATCH 1/4] dt-overlay: Fix NULL pointer dereference

Quoted 14 lines · Michal Orzel

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:

Quoted 6 lines
(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")
Quoted 25 lines
---
 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.37.6
#3 lore raw

Re: [PATCH 1/4] dt-overlay: Fix NULL pointer dereference

In reply to Michal Orzel

Hi Michal,

Quoted 29 lines · Michal Orzel

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")
---
  xen/common/dt-overlay.c | 7 +++++--
  1 file changed, 5 insertions(+), 2 deletions(-)
xen/common/dt-overlay.c +0 -0
index d53b4706cd2f..8606b14d1e8e 100644

xen/common/dt-overlay.c static long handle_attach_overlay_nodes(struct domain *d, · line 915

Quoted 10 lines
xen/common/dt-overlay.c +5 -2
@@ -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);
+    }

While looking at the error paths in handle_attach_overlay_nodes(), I noticed we don't revert any partial changes made by handle_device().

In this case, I am wondering whether it is correct to destroy the rangeset. How would you be able to revert the changes?

Cheers,

Signature · 1 line
Julien Grall
#4 lore raw

Re: [PATCH 1/4] dt-overlay: Fix NULL pointer dereference

Hi Julien,

Quoted 48 lines · Julien Grall

Hi Michal,

Quoted 29 lines · Michal Orzel

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")
---
  xen/common/dt-overlay.c | 7 +++++--
  1 file changed, 5 insertions(+), 2 deletions(-)
xen/common/dt-overlay.c +0 -0
index d53b4706cd2f..8606b14d1e8e 100644

xen/common/dt-overlay.c static long handle_attach_overlay_nodes(struct domain *d, · line 915

Quoted 10 lines
xen/common/dt-overlay.c +5 -2
@@ -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);
+    }

While looking at the error paths in handle_attach_overlay_nodes(), I noticed we don't revert any partial changes made by handle_device().

Quoted 2 lines

In this case, I am wondering whether it is correct to destroy the rangeset. How would you be able to revert the changes?

I guess the same story applies as for the partial add/remove which was stated by Vikram in the commit msg of 7e5c4a8b86f12942de0566b1d61f71d15774364b meaning partial success withe some failures may lead to other failures and might need a system reboot. I did not carefully look into this series, my plan was to fix the issues without changing the functional behavior. FWICS, we do not yet support detachment (only add/remove and attach) and removal of nodes and ranges is only possible if the nodes are assigned to hwdom.

~Michal

#5 lore raw

Re: [PATCH 1/4] dt-overlay: Fix NULL pointer dereference

Quoted 1 line · Michal Orzel

Hi Julien,

Hi Michal,

Quoted 49 lines · Julien Grall
Quoted 48 lines

Hi Michal,

Quoted 29 lines · Michal Orzel

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")
---
   xen/common/dt-overlay.c | 7 +++++--
   1 file changed, 5 insertions(+), 2 deletions(-)
xen/common/dt-overlay.c +0 -0
index d53b4706cd2f..8606b14d1e8e 100644

xen/common/dt-overlay.c static long handle_attach_overlay_nodes(struct domain *d, · line 915

Quoted 10 lines
xen/common/dt-overlay.c +5 -2
@@ -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);
+    }

While looking at the error paths in handle_attach_overlay_nodes(), I noticed we don't revert any partial changes made by handle_device().

Quoted 6 lines
Quoted 2 lines

In this case, I am wondering whether it is correct to destroy the rangeset. How would you be able to revert the changes?

I guess the same story applies as for the partial add/remove which was stated by Vikram in the commit msg of 7e5c4a8b86f12942de0566b1d61f71d15774364b meaning partial success withe some failures may lead to other failures and might need a system reboot. I did not carefully look into this series, my plan was to fix the issues without changing the functional behavior.

Do you mean in this series or forever? If the former, would you be able to outline what you expect after the end of this series? What should work? What should not work?

Quoted 3 lines

FWICS, we do not
yet support detachment (only add/remove and attach) and removal of nodes and ranges is only possible if the nodes are assigned to hwdom.

I need some clarifications. By "we do not yet support detachment", do you mean while a guest is running or do you also include a domain shutting down?

Cheers,

Signature · 1 line
Julien Grall
#6 lore raw

Re: [PATCH 1/4] dt-overlay: Fix NULL pointer dereference

Hi Julien,

Quoted 58 lines · Julien Grall
Quoted 1 line · Michal Orzel

Hi Julien,

Hi Michal,

Quoted 51 lines · Julien Grall
Quoted 50 lines

Hi Michal,

Quoted 29 lines · Michal Orzel

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")
---
   xen/common/dt-overlay.c | 7 +++++--
   1 file changed, 5 insertions(+), 2 deletions(-)
xen/common/dt-overlay.c +0 -0
index d53b4706cd2f..8606b14d1e8e 100644

xen/common/dt-overlay.c static long handle_attach_overlay_nodes(struct domain *d, · line 915

Quoted 10 lines
xen/common/dt-overlay.c +5 -2
@@ -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);
+    }

While looking at the error paths in handle_attach_overlay_nodes(), I noticed we don't revert any partial changes made by handle_device().

In this case, I am wondering whether it is correct to destroy the rangeset. How would you be able to revert the changes?

Quoted 8 lines
Quoted 4 lines

I guess the same story applies as for the partial add/remove which was stated by Vikram in the commit msg of 7e5c4a8b86f12942de0566b1d61f71d15774364b meaning partial success withe some failures may lead to other failures and might need a system reboot. I did not carefully look into this series, my plan was to fix the issues without changing the functional behavior.

Do you mean in this series or forever? If the former, would you be able to outline what you expect after the end of this series? What should work? What should not work?

The goal of this series is to fix the issues I encountered while doing some other DT overlay work as stated in the patch 0. The goal of each patch is clearly outlined in the commit message by stating what is being fixed and why. The first 3 patches contain "Fixes" tag, therefore there is no functional behavior change - only fixing what should have been fixed by the original series. Patch 4 does not have "Fixes" tag although in theory it could. There is no doc mentioning that adding nodes right into "/" is forbidden. Linux supports it so I added support for Xen as well.

After end of *this* series I expect the issues mentioned in each patch to be fixed + the ability to add nodes directly under root node.

It's definitely not the end of fixes for this series. I have already written at least 1 more fix that allows to have other nodes in the overlay tree that should be ignored (at the moment the code assumes that each node one level below root, needs to specify target-path. This is wrong as we can have __fixups__, __symbols__, and other nodes that we should simply ignore according to docs.

Quoted 8 lines
Quoted 4 lines

FWICS, we do not
yet support detachment (only add/remove and attach) and removal of nodes and ranges is only possible if the nodes are assigned to hwdom.

I need some clarifications. By "we do not yet support detachment", do
you mean while a guest is running or do you also include a domain
shutting down?In order to reason about detachment we need to go back to see what is supported in terms of attachment.

At the moment, we can only attach dtbo to 1:1 domUs, therefore we can stop talking about libxl domUs for now. For dom0less domUs, we also cannot really shut down them. So it leaves us with dom0 whose reboot implies system reboot. At the moment there is no code that would release IRQs and unmap MMIOs.

~Michal

#7 lore raw

Re: [PATCH 1/4] dt-overlay: Fix NULL pointer dereference

Hi,

Quoted 66 lines · Michal Orzel

Hi Julien,

Quoted 63 lines · Julien Grall
Quoted 1 line · Michal Orzel

Hi Julien,

Hi Michal,

Quoted 55 lines · Julien Grall
Quoted 48 lines

Hi Michal,

Quoted 29 lines · Michal Orzel

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")
---
    xen/common/dt-overlay.c | 7 +++++--
    1 file changed, 5 insertions(+), 2 deletions(-)
xen/common/dt-overlay.c +0 -0
index d53b4706cd2f..8606b14d1e8e 100644

xen/common/dt-overlay.c static long handle_attach_overlay_nodes(struct domain *d, · line 915

Quoted 10 lines
xen/common/dt-overlay.c +5 -2
@@ -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);
+    }

While looking at the error paths in handle_attach_overlay_nodes(), I noticed we don't revert any partial changes made by handle_device().

Quoted 2 lines

In this case, I am wondering whether it is correct to destroy the rangeset. How would you be able to revert the changes?

I guess the same story applies as for the partial add/remove which was stated by Vikram in the commit msg of 7e5c4a8b86f12942de0566b1d61f71d15774364b meaning partial success withe some failures may lead to other failures and might need a system reboot. I did not carefully look into this series, my plan was to fix the issues without changing the functional behavior.

Quoted 6 lines
Quoted 3 lines

Do you mean in this series or forever? If the former, would you be able to outline what you expect after the end of this series? What should work? What should not work?

The goal of this series is to fix the issues I encountered while doing some other DT overlay work
as stated in the patch 0. The goal of each patch is clearly outlined in the commit message
by stating what is being fixed and why. The first 3 patches contain "Fixes" tag, therefore there> is no functional behavior change - only fixing what should have been

fixed by the original series.

Quoted 7 lines

Patch 4 does not have "Fixes" tag although in theory it could. There is no doc mentioning that adding nodes right into "/" is forbidden. Linux supports it so I added support for Xen as well.

After end of *this* series I expect the issues mentioned in each patch to be fixed + the ability to add nodes directly under root node.

It's definitely not the end of fixes for this series.

Good. This wasn't clear from your wording. Thanks for clarification!

Quoted 9 lines
Quoted 8 lines
Quoted 4 lines

FWICS, we do not
yet support detachment (only add/remove and attach) and removal of nodes and ranges is only possible if the nodes are assigned to hwdom.

I need some clarifications. By "we do not yet support detachment", do
you mean while a guest is running or do you also include a domain
shutting down?In order to reason about detachment we need to go back to see what is supported in terms of attachment.

At the moment, we can only attach dtbo to 1:1 domUs, therefore we can stop talking about libxl domUs for now.

Ah, I missed the check. I thought we already supported device attachment to guest started by xl.

Quoted 2 lines

For dom0less domUs, we also cannot really shut down them. So it leaves us with dom0 whose reboot implies system reboot. At the moment there is no code that would release IRQs and unmap MMIOs.

Understood. Then no action expected from my side for now. But it would be good to keep track of the unknown issues when we will implement detach.

Cheers,

Signature · 1 line
Julien Grall
#8 lore raw

PATCH 2/4 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")
---
 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.37.6
Reviewed-by on v1 2/4
#9 lore raw

Re: [PATCH 2/4] device-tree: Remove __init from unflatten_dt_alloc()

Quoted 22 lines · 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
Quoted 2 lines
Fixes 9e9d2c079dc4 ("xen/arm/device: Remove __init from function type")
Quoted 22 lines
---
 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.37.6
#10 lore raw

PATCH 3/4 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. It's enough for the target-path to be incorrect (i.e. user error), which will lead to an incorrect overall node path and we will end up in this "unreachable" place causing a failure in debug builds.

Fixes 0c0facdab6f5 ("xen/arm: Implement device tree node addition functionalities")
---
 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.37.6
Reviewed-by on v1 3/4
#11 lore raw

Re: [PATCH 3/4] dt-overlay: Remove ASSERT_UNREACHABLE from add_nodes()

Quoted 7 lines · Michal Orzel

The assumption stated in the comment that the code will never get there is incorrect. It's enough for the target-path to be incorrect (i.e. user error), which will lead to an incorrect overall node path and we will end up in this "unreachable" place causing a failure in debug builds.

Fixes 0c0facdab6f5 ("xen/arm: Implement device tree node addition functionalities")
Quoted 23 lines
---
 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.37.6
#12 lore raw

Re: [PATCH 3/4] dt-overlay: Remove ASSERT_UNREACHABLE from add_nodes()

In reply to Michal Orzel

Hi Michal,

Quoted 4 lines · Michal Orzel

The assumption stated in the comment that the code will never get there is incorrect. It's enough for the target-path to be incorrect (i.e. user error), which will lead to an incorrect overall node path and we will end up in this "unreachable" place causing a failure in debug builds.

Looking at the caller, nodes_full_path contain path that was computed from the overlay. So I would have assumed the path would be part of the new DT. What did I miss?

Quoted 13 lines
Fixes 0c0facdab6f5 ("xen/arm: Implement device tree node addition functionalities")
---
  xen/common/dt-overlay.c | 4 ----
  1 file changed, 4 deletions(-)
xen/common/dt-overlay.c +0 -0
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,

xen/common/dt-overlay.c static long add_nodes(struct overlay_track *tr, char **nodes_full_path) · line 602

Quoted 10 lines
xen/common/dt-overlay.c +0 -4
                                                   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

Cheers,

Signature · 1 line
Julien Grall
#13 lore raw

Re: [PATCH 3/4] dt-overlay: Remove ASSERT_UNREACHABLE from add_nodes()

Hi Julien,

Quoted 5 lines · Julien Grall

Hi Michal,

On 19/09/2024 12:42, Michal Orzel wrote:

Quoted 8 lines
Quoted 4 lines

The assumption stated in the comment that the code will never get there is incorrect. It's enough for the target-path to be incorrect (i.e. user error), which will lead to an incorrect overall node path and we will end up in this "unreachable" place causing a failure in debug builds.

Looking at the caller, nodes_full_path contain path that was computed from the overlay. So I would have assumed the path would be part of the new DT. What did I miss?

nodes_full_path contains paths to nodes by combining the name of the node with the target-path string. It's generated in overlay_get_nodes_info(). It's a simple <target-path> + '/' + <node-path> + '\0'. It does not have any dt logic as for paths. On the other hand libfdt contains advanced logic and can tweak the node path if needed. So for example if the target-path is "////" and node path is "foo@0", libfdt full path will be "/foo@0" (notice how it got rid of excessive slashes) but our nodes_full_path[foo] will be "////foo@0". The only place which can spot this difference is our check in add_nodes().

~Michal

Acked-by
#14 lore raw

Re: [PATCH 3/4] dt-overlay: Remove ASSERT_UNREACHABLE from add_nodes()

Quoted 16 lines · Michal Orzel

Hi Julien,

Quoted 5 lines · Julien Grall

Hi Michal,

On 19/09/2024 12:42, Michal Orzel wrote:

Quoted 8 lines
Quoted 4 lines

The assumption stated in the comment that the code will never get there is incorrect. It's enough for the target-path to be incorrect (i.e. user error), which will lead to an incorrect overall node path and we will end up in this "unreachable" place causing a failure in debug builds.

Looking at the caller, nodes_full_path contain path that was computed from the overlay. So I would have assumed the path would be part of the new DT. What did I miss?

Quoted 6 lines

nodes_full_path contains paths to nodes by combining the name of the node with the target-path string. It's generated in overlay_get_nodes_info(). It's a simple <target-path> + '/' + <node-path> + '\0'. It does not have any dt logic as for paths. On the other hand libfdt contains advanced logic and can tweak the node path if needed. So for example if the target-path is "////" and node path is "foo@0", libfdt full path will be "/foo@0" (notice how it got rid of excessive slashes) but our nodes_full_path[foo] will be "////foo@0". The only place which can spot this difference is our check in add_nodes().

Thanks for the clarification. This reads as we want to get the list of nodes using the libfdt logic rather than re-inventing our own.

I would suggest to expand a bit more the commit message with the details you provided. With that:

Cheers,

Signature · 1 line
Julien Grall
#15 lore raw

PATCH 4/4 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.

---
 xen/common/dt-overlay.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
xen/common/dt-overlay.c +11 -5
index d18bd12bd38d..63b28889de90 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) + 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.37.6
#16 lore raw

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

Quoted 26 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.

---
 xen/common/dt-overlay.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
xen/common/dt-overlay.c +1 -0
index d18bd12bd38d..63b28889de90 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;
 

xen/common/dt-overlay.c static int overlay_get_nodes_info(const void *fdto, char **nodes_full_path) · line 341

Quoted 10 lines
xen/common/dt-overlay.c +5 -3
             /*
-             * 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) + 1;

I'd prefer to avoid the implicit bool to int conversion. I think it is mandated by MISRA R10.1, we have a bool exception but I don't think it would cover this type of usage. For instance:

extra_len = ((target_path_len > 1) ? 1 : 0) + 1;

or

extra_len = (target_path_len > 1) ? 2 : 1;

Quoted 21 lines
xen/common/dt-overlay.c +5 -2
+            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.37.6
#17 lore raw

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

Quoted 44 lines · Stefano Stabellini
Quoted 26 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.

---
 xen/common/dt-overlay.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
xen/common/dt-overlay.c +1 -0
index d18bd12bd38d..63b28889de90 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;
 

xen/common/dt-overlay.c static int overlay_get_nodes_info(const void *fdto, char **nodes_full_path) · line 341

Quoted 10 lines
xen/common/dt-overlay.c +5 -3
             /*
-             * 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) + 1;

I'd prefer to avoid the implicit bool to int conversion. I think it is mandated by MISRA R10.1, we have a bool exception but I don't think it would cover this type of usage. For instance:

Quoted 5 lines

extra_len = ((target_path_len > 1) ? 1 : 0) + 1;

or

extra_len = (target_path_len > 1) ? 2 : 1;

Ok, will do this version.

~Michal