Skip to contentSkip to conversation

dynamic node programming using overlay dtbo

RFC PATCH Superseded Vikram Garhwal 6 participants 28 messages Nov 9, 2021 – Dec 24, 2021

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover dynamic node programming using overlay dtbo

Hi,
This RFC patch series is for introducing dynamic programming i.e. add/remove the devices during run time. Using "xl overlay" a device can be added/removed with dtbo.

For adding a node using dynamic programming:
1. flatten device tree overlay node will be added to a fdt
2. Updated fdt will be unflattened to a new dt_host_new
3. Extract the newly added node information from dt_host_new
4. Add the added node under correct parent in original dt_host.
3. Map interrupt and iomem region as required.

For removing a node:
1. Find the node with given path.
2. Check if the node is used by any of dom0 or domus. Removes the node
only when it's not used by any domain.
3. Removes IRQ permissions and MMIO access.
5. Find the node in dt_host and delete the device node entry from dt_host.
6. Free the overlay_tracker entry which means free dt_host_new also(created
in adding node step).

Question: I am not sure how to enable CONFIG_OVERLAY_DTB in tool stack like the way we do in xen using menuconfig or editing .config file. Would it be possible for someone in tool stack expertise to guide me?

Reminder: This patch series has dependency on libfdt v1.6.1. A patch for libfdt upgrade was already sent earlier with below subject:

    [XEN][PATCH v2 0/1] Update libfdt to v1.6.1

Change Log:
v1 -> v2:
Add support for multiple node addition/removal using dtbo. Replaced fpga-add and fpga-remove with one hypercall overlay_op. Moved common domain_build.c function to device.c
Add OVERLAY_DTB configuration.
Renamed overlay_get_target() to fdt_overlay_get_target().
Split remove_device patch into two patches.
Moved overlay_add/remove code to sysctl and changed it from domctl to sysctl. Added all overlay code under CONFIG_OVERLAY_DTB
Renamed all tool domains fpga function to overlay
Addressed code issues from v1 review.

Regards,
Vikram

Vikram Garhwal (12):
device tree: Remove __init from function type
xen: arm: Add CONFIG_OVERLAY_DTB
libfdt: Keep fdt functions after init for CONFIG_OVERLAY_DTB.
libfdt: Add fdt_ prefix to overlay_get_target()
device tree: Add _dt_find_node_by_path() to find nodes in device tree xen/smmu: Add remove_device callback for smmu_iommu ops xen/smmu: Add remove_device callback for smmu_iommu ops xen/arm: Implement device tree node removal functionalities xen/arm: Implement device tree node addition functionalities tools/libs/ctrl: Implement new xc interfaces for dt overlay tools/libs/light: Implement new libxl functions for device tree
overlay ops
tools/xl: Add new xl command overlay

 tools/include/libxl.h                 |   5 +
 tools/include/xenctrl.h               |   5 +
 tools/libs/ctrl/Makefile              |   1 +
 tools/libs/ctrl/xc_overlay.c          |  51 +++
 tools/libs/light/Makefile             |   3 +
 tools/libs/light/libxl_overlay.c      |  65 ++++
 tools/xl/xl.h                         |   8 +
 tools/xl/xl_cmdtable.c                |   8 +
 tools/xl/xl_vmcontrol.c               |  47 +++
 xen/arch/arm/Kconfig                  |   8 +
 xen/arch/arm/device.c                 | 146 +++++++++
 xen/arch/arm/domain_build.c           | 150 ---------
 xen/common/device_tree.c              | 122 +++++++-
 xen/common/libfdt/Makefile            |   3 +
 xen/common/libfdt/fdt_overlay.c       |  12 +-
 xen/common/sysctl.c                   | 571 ++++++++++++++++++++++++++++++++++
 xen/drivers/passthrough/arm/smmu.c    |  54 ++++
 xen/drivers/passthrough/device_tree.c |  30 ++
 xen/include/asm-arm/domain_build.h    |  10 +
 xen/include/public/sysctl.h           |  23 ++
 xen/include/xen/device_tree.h         |  20 ++
 xen/include/xen/iommu.h               |   2 +
 xen/include/xen/libfdt/libfdt.h       |   3 +
 23 files changed, 1180 insertions(+), 167 deletions(-)
 create mode 100644 tools/libs/ctrl/xc_overlay.c
 create mode 100644 tools/libs/light/libxl_overlay.c
Signature · 1 line
2.7.4
#1 lore raw

RFC PATCH v2 1/12 device tree: Remove __init from function type

Change function type of following function to access during runtime:
1. map_irq_to_domain()
2. handle_device_interrupt()
3. map_range_to_domain()
4. unflatten_dt_node()
5. unflatten_device_tree()

Move map_irq_to_domain(), handle_device_interrupt() and map_range_to_domain() to device.c.

Move map_range_data declaration to domain_build.h.

These changes are done to support the dynamic programming of a nodes where an overlay node will be added to fdt and unflattened node will be added to dt_host. Furthermore, IRQ and mmio mapping will be done for the added node.

---
 xen/arch/arm/device.c              | 146 ++++++++++++++++++++++++++++++++++++
 xen/arch/arm/domain_build.c        | 150 -------------------------------------
 xen/common/device_tree.c           |  18 ++---
 xen/include/asm-arm/domain_build.h |  10 +++
 xen/include/xen/device_tree.h      |   5 ++
 5 files changed, 170 insertions(+), 159 deletions(-)
xen/arch/arm/device.c +146 -0
index 70cd6c1..c7cf2b9 100644
@@ -21,6 +21,8 @@
 #include <xen/errno.h>
 #include <xen/init.h>
 #include <xen/lib.h>
+#include <xen/iocap.h>
+#include <asm/domain_build.h>
 
 extern const struct device_desc _sdevice[], _edevice[];
 extern const struct acpi_device_desc _asdevice[], _aedevice[];
@@ -84,6 +86,150 @@ enum device_class device_get_class(const struct dt_device_node *dev)
     return DEVICE_UNKNOWN;
 }
 
+int map_irq_to_domain(struct domain *d, unsigned int irq,
+                             bool need_mapping, const char *devname)
+{
+    int res;
+
+    res = irq_permit_access(d, irq);
+    if ( res )
+    {
+        printk(XENLOG_ERR "Unable to permit to dom%u access to IRQ %u\n",
+               d->domain_id, irq);
+        return res;
+    }
+
+    if ( need_mapping )
+    {
+        /*
+         * Checking the return of vgic_reserve_virq is not
+         * necessary. It should not fail except when we try to map
+         * the IRQ twice. This can legitimately happen if the IRQ is shared
+         */
+        vgic_reserve_virq(d, irq);
+
+        res = route_irq_to_guest(d, irq, irq, devname);
+        if ( res < 0 )
+        {
+            printk(XENLOG_ERR "Unable to map IRQ%"PRId32" to dom%d\n",
+                   irq, d->domain_id);
+            return res;
+        }
+    }
+
+    dt_dprintk("  - IRQ: %u\n", irq);
+    return 0;
+}
+
+int map_range_to_domain(const struct dt_device_node *dev,
+                                      u64 addr, u64 len,
+                                      void *data)
+{
+    struct map_range_data *mr_data = data;
+    struct domain *d = mr_data->d;
+    bool need_mapping = !dt_device_for_passthrough(dev);
+    int res;
+
+    /*
+     * reserved-memory regions are RAM carved out for a special purpose.
+     * They are not MMIO and therefore a domain should not be able to
+     * manage them via the IOMEM interface.
+     */
+    if ( strncasecmp(dt_node_full_name(dev), "/reserved-memory/",
+                     strlen("/reserved-memory/")) != 0 )
+    {
+        res = iomem_permit_access(d, paddr_to_pfn(addr),
+                paddr_to_pfn(PAGE_ALIGN(addr + len - 1)));
+        if ( res )
+        {
+            printk(XENLOG_ERR "Unable to permit to dom%d access to"
+                    " 0x%"PRIx64" - 0x%"PRIx64"\n",
+                    d->domain_id,
+                    addr & PAGE_MASK, PAGE_ALIGN(addr + len) - 1);
+            return res;
+        }
+    }
+
+    if ( need_mapping )
+    {
+        res = map_regions_p2mt(d,
+                               gaddr_to_gfn(addr),
+                               PFN_UP(len),
+                               maddr_to_mfn(addr),
+                               mr_data->p2mt);
+
+        if ( res < 0 )
+        {
+            printk(XENLOG_ERR "Unable to map 0x%"PRIx64
+                   " - 0x%"PRIx64" in domain %d\n",
+                   addr & PAGE_MASK, PAGE_ALIGN(addr + len) - 1,
+                   d->domain_id);
+            return res;
+        }
+    }
+
+    dt_dprintk("  - MMIO: %010"PRIx64" - %010"PRIx64" P2MType=%x\n",
+               addr, addr + len, mr_data->p2mt);
+
+    return 0;
+}
+
+/*
+ * handle_device_interrupts retrieves the interrupts configuration from
+ * a device tree node and maps those interrupts to the target domain.
+ *
+ * Returns:
+ *   < 0 error
+ *   0   success
+ */
+int handle_device_interrupts(struct domain *d,
+                                           struct dt_device_node *dev,
+                                           bool need_mapping)
+{
+    unsigned int i, nirq;
+    int res;
+    struct dt_raw_irq rirq;
+
+    nirq = dt_number_of_irq(dev);
+
+    /* Give permission and map IRQs */
+    for ( i = 0; i < nirq; i++ )
+    {
+        res = dt_device_get_raw_irq(dev, i, &rirq);
+        if ( res )
+        {
+            printk(XENLOG_ERR "Unable to retrieve irq %u for %s\n",
+                   i, dt_node_full_name(dev));
+            return res;
+        }
+
+        /*
+         * Don't map IRQ that have no physical meaning
+         * ie: IRQ whose controller is not the GIC
+         */
+        if ( rirq.controller != dt_interrupt_controller )
+        {
+            dt_dprintk("irq %u not connected to primary controller. Connected to %s\n",
+                      i, dt_node_full_name(rirq.controller));
+            continue;
+        }
+
+        res = platform_get_irq(dev, i);
+        if ( res < 0 )
+        {
+            printk(XENLOG_ERR "Unable to get irq %u for %s\n",
+                   i, dt_node_full_name(dev));
+            return res;
+        }
+
+        res = map_irq_to_domain(d, res, need_mapping, dt_node_name(dev));
+        if ( res )
+            return res;
+    }
+
+    return 0;
+}
+
 /*
  * Local variables:
  * mode: C
xen/arch/arm/domain_build.c +0 -150

174 lines not on this page · raw message

xen/common/device_tree.c +9 -9
index 92ce59e..88f3f7e 100644
@@ -1821,12 +1821,12 @@ int dt_count_phandle_with_args(const struct dt_device_node *np,
  * @allnextpp: pointer to ->allnext from last allocated device_node
  * @fpsize: Size of the node path up at the current depth.
  */
-static unsigned long __init unflatten_dt_node(const void *fdt,
-                                              unsigned long mem,
-                                              unsigned long *p,
-                                              struct dt_device_node *dad,
-                                              struct dt_device_node ***allnextpp,
-                                              unsigned long fpsize)
+static unsigned long unflatten_dt_node(const void *fdt,
+                                unsigned long mem,
+                                unsigned long *p,
+                                struct dt_device_node *dad,
+                                struct dt_device_node ***allnextpp,
+                                unsigned long fpsize)
 {
     struct dt_device_node *np;
     struct dt_property *pp, **prev_pp = NULL;
@@ -2057,7 +2057,7 @@ static unsigned long __init unflatten_dt_node(const void *fdt,
 }
 
 /**
- * __unflatten_device_tree - create tree of device_nodes from flat blob
+ * unflatten_device_tree - create tree of device_nodes from flat blob
  *
  * unflattens a device-tree, creating the
  * tree of struct device_node. It also fills the "name" and "type"
@@ -2066,7 +2066,7 @@ static unsigned long __init unflatten_dt_node(const void *fdt,
  * @fdt: The fdt to expand
  * @mynodes: The device_node tree created by the call
  */
-static void __init __unflatten_device_tree(const void *fdt,
+void unflatten_device_tree(const void *fdt,
                                            struct dt_device_node **mynodes)
 {
     unsigned long start, mem, size;
@@ -2189,7 +2189,7 @@ dt_find_interrupt_controller(const struct dt_device_match *matches)
 
 void __init dt_unflatten_host_device_tree(void)
 {
-    __unflatten_device_tree(device_tree_flattened, &dt_host);
+    unflatten_device_tree(device_tree_flattened, &dt_host);
     dt_alias_scan();
 }
 
xen/include/asm-arm/domain_build.h +10 -0
index 34ceddc..17449b1 100644
@@ -4,10 +4,20 @@
 #include <xen/sched.h>
 #include <asm/kernel.h>
 
+struct map_range_data
+{
+    struct domain *d;
+    p2m_type_t p2mt;
+};
+
 int map_irq_to_domain(struct domain *d, unsigned int irq,
                       bool need_mapping, const char *devname);
 int make_chosen_node(const struct kernel_info *kinfo);
 void evtchn_allocate(struct domain *d);
+int handle_device_interrupts(struct domain *d, struct dt_device_node *dev,
+                             bool need_mapping);
+int map_range_to_domain(const struct dt_device_node *dev, u64 addr, u64 len,
+                        void *data);
 
 #ifndef CONFIG_ACPI
 static inline int prepare_acpi(struct domain *d, struct kernel_info *kinfo)
xen/include/xen/device_tree.h +5 -0
index 573e4a0..9fc63ab 100644
@@ -177,6 +177,11 @@ int device_tree_for_each_node(const void *fdt, int node,
  */
 void dt_unflatten_host_device_tree(void);
 
+/*
+ * unflatten any device tree.
+ */
+void unflatten_device_tree(const void *fdt, struct dt_device_node **mynodes);
+
 /**
  * IRQ translation callback
  * TODO: For the moment we assume that we only have ONE
Signature · 1 line
2.7.4
#2 lore raw

RFC PATCH v2 2/12 xen: arm: Add CONFIG_OVERLAY_DTB

In reply to Vikram Garhwal
---
 xen/arch/arm/Kconfig | 8 ++++++++
 1 file changed, 8 insertions(+)
xen/arch/arm/Kconfig +8 -0
index ecfa682..895f0ee 100644
@@ -46,6 +46,14 @@ config HAS_ITS
         bool "GICv3 ITS MSI controller support (UNSUPPORTED)" if UNSUPPORTED
         depends on GICV3 && !NEW_VGIC
 
+config OVERLAY_DTB
+    depends on MPSOC_PLATFORM
+    bool "Enable DTB overlay"
+    default y
+    ---help---
+
+    Allows dynamic addition/removal of Xen device tree nodes using a dtbo.
+
 config HVM
         def_bool y
 
Signature · 1 line
2.7.4
#3 lore raw

Re: [XEN][RFC PATCH v2 02/12] xen: arm: Add CONFIG_OVERLAY_DTB

Hi,

Please add a short commit message explaning how this is going to be used.

Quoted 10 lines · Vikram Garhwal
---
  xen/arch/arm/Kconfig | 8 ++++++++
  1 file changed, 8 insertions(+)
xen/arch/arm/Kconfig +0 -0
index ecfa682..895f0ee 100644

xen/arch/arm/Kconfig config HAS_ITS · line 50

Quoted 5 lines
xen/arch/arm/Kconfig +2 -0
@@ -46,6 +46,14 @@ config HAS_ITS
          bool "GICv3 ITS MSI controller support (UNSUPPORTED)" if UNSUPPORTED
          depends on GICV3 && !NEW_VGIC
 
+config OVERLAY_DTB
+    depends on MPSOC_PLATFORM

None of the code looks to be Xilinx specific and this will prevent one to build a generic Xen and test your code. So can you remove the dependency?

xen/arch/arm/Kconfig config HAS_ITS · line 52

Quoted 2 lines
xen/arch/arm/Kconfig +2 -0
+    bool "Enable DTB overlay"
+    default y

In general, for new feature, we want to disable them by default and gate them with UNSUPPORTED.

They can move off UNSUPPORTED once they are ready enough to be supported (and possibly security supported).

Cheers,

Signature · 1 line
Julien Grall
#4 lore raw

RFC PATCH v2 3/12 libfdt: Keep fdt functions after init for CONFIG_OVERLAY_DTB.

In reply to Vikram Garhwal

This is done to access fdt library function which are required for adding device tree overlay nodes for dynamic programming of nodes.

---
 xen/common/libfdt/Makefile | 3 +++
 1 file changed, 3 insertions(+)
xen/common/libfdt/Makefile +3 -0
index 6bd207c..f838f5f 100644
@@ -1,7 +1,10 @@
 include Makefile.libfdt
 
 SECTIONS := text data $(SPECIAL_DATA_SECTIONS)
+
+ifneq ($(CONFIG_OVERLAY_DTB),y)
 OBJCOPYFLAGS := $(foreach s,$(SECTIONS),--rename-section .$(s)=.init.$(s))
+endif
 
 obj-y += libfdt.o
 nocov-y += libfdt.o
Signature · 1 line
2.7.4
#5 lore raw

Re: [XEN][RFC PATCH v2 03/12] libfdt: Keep fdt functions after init for CONFIG_OVERLAY_DTB.

Quoted 13 lines · Vikram Garhwal

This is done to access fdt library function which are required for adding device tree overlay nodes for dynamic programming of nodes.

---
 xen/common/libfdt/Makefile | 3 +++
 1 file changed, 3 insertions(+)
xen/common/libfdt/Makefile +0 -0
index 6bd207c..f838f5f 100644

xen/common/libfdt/Makefile line 7

Quoted 7 lines
xen/common/libfdt/Makefile +3 -0
@@ -1,7 +1,10 @@
 include Makefile.libfdt
 
 SECTIONS := text data $(SPECIAL_DATA_SECTIONS)
+
+ifneq ($(CONFIG_OVERLAY_DTB),y)
 OBJCOPYFLAGS := $(foreach s,$(SECTIONS),--rename-section .$(s)=.init.$(s))
+endif

I think you'd better suppress the objcopy step altogether in that case.

Jan

Acked-by on v2 3/12
#6 lore raw

Re: [XEN][RFC PATCH v2 03/12] libfdt: Keep fdt functions after init for CONFIG_OVERLAY_DTB.

In reply to Vikram Garhwal

Hi Vikram,

Quoted 13 lines · Vikram Garhwal

This is done to access fdt library function which are required for adding device tree overlay nodes for dynamic programming of nodes.

---
  xen/common/libfdt/Makefile | 3 +++
  1 file changed, 3 insertions(+)
xen/common/libfdt/Makefile +0 -0
index 6bd207c..f838f5f 100644

xen/common/libfdt/Makefile line 4

Quoted 4 lines
xen/common/libfdt/Makefile +1 -0
@@ -1,7 +1,10 @@
  include Makefile.libfdt
 
  SECTIONS := text data $(SPECIAL_DATA_SECTIONS)
+

NIT: I would add a short comment explaining why we need to keep after init.

xen/common/libfdt/Makefile line 7

Quoted 3 lines
xen/common/libfdt/Makefile +2 -0
+ifneq ($(CONFIG_OVERLAY_DTB),y)
  OBJCOPYFLAGS := $(foreach s,$(SECTIONS),--rename-section .$(s)=.init.$(s))
+endif

Cheers,

Signature · 1 line
Julien Grall
#7 lore raw

RFC PATCH v2 4/12 libfdt: Add fdt_ prefix to overlay_get_target()

In reply to Vikram Garhwal

Add fdt_ prefix to overlay_get_target() and remove static type. This is done to get the target path for all the overlay nodes. This is useful to find which nodes are to be added/removed in dt_host.

Also, sending this patch to dtc mailing list to avoid the divergence.

---
 xen/common/libfdt/fdt_overlay.c | 12 ++++++------
 xen/include/xen/libfdt/libfdt.h |  3 +++
 2 files changed, 9 insertions(+), 6 deletions(-)
xen/common/libfdt/fdt_overlay.c +6 -6
index 7b95e2b..194f51b 100644
@@ -42,13 +42,13 @@ static uint32_t overlay_get_target_phandle(const void *fdto, int fragment)
 }
 
 /**
- * overlay_get_target - retrieves the offset of a fragment's target
+ * fdt_overlay_get_target - retrieves the offset of a fragment's target
  * @fdt: Base device tree blob
  * @fdto: Device tree overlay blob
  * @fragment: node offset of the fragment in the overlay
  * @pathp: pointer which receives the path of the target (or NULL)
  *
- * overlay_get_target() retrieves the target offset in the base
+ * fdt_overlay_get_target() retrieves the target offset in the base
  * device tree of a fragment, no matter how the actual targeting is
  * done (through a phandle or a path)
  *
@@ -56,7 +56,7 @@ static uint32_t overlay_get_target_phandle(const void *fdto, int fragment)
  *      the targeted node offset in the base device tree
  *      Negative error code on error
  */
-static int overlay_get_target(const void *fdt, const void *fdto,
+int fdt_overlay_get_target(const void *fdt, const void *fdto,
 			      int fragment, char const **pathp)
 {
 	uint32_t phandle;
@@ -638,7 +638,7 @@ static int overlay_merge(void *fdt, void *fdto)
 		if (overlay < 0)
 			return overlay;
 
-		target = overlay_get_target(fdt, fdto, fragment, NULL);
+		target = fdt_overlay_get_target(fdt, fdto, fragment, NULL);
 		if (target < 0)
 			return target;
 
@@ -781,7 +781,7 @@ static int overlay_symbol_update(void *fdt, void *fdto)
 			return -FDT_ERR_BADOVERLAY;
 
 		/* get the target of the fragment */
-		ret = overlay_get_target(fdt, fdto, fragment, &target_path);
+		ret = fdt_overlay_get_target(fdt, fdto, fragment, &target_path);
 		if (ret < 0)
 			return ret;
 		target = ret;
@@ -803,7 +803,7 @@ static int overlay_symbol_update(void *fdt, void *fdto)
 
 		if (!target_path) {
 			/* again in case setprop_placeholder changed it */
-			ret = overlay_get_target(fdt, fdto, fragment, &target_path);
+			ret = fdt_overlay_get_target(fdt, fdto, fragment, &target_path);
 			if (ret < 0)
 				return ret;
 			target = ret;
xen/include/xen/libfdt/libfdt.h +3 -0
index c71689e..1f549d0 100644
@@ -2115,6 +2115,9 @@ int fdt_overlay_apply(void *fdt, void *fdto);
 
 const char *fdt_strerror(int errval);
 
+int fdt_overlay_get_target(const void *fdt, const void *fdto, int fragment,
+                           char const **pathp);
+
 #ifdef __cplusplus
 }
 #endif
Signature · 1 line
2.7.4
#8 lore raw

Re: [XEN][RFC PATCH v2 04/12] libfdt: Add fdt_ prefix to overlay_get_target()

Hi,

Quoted 5 lines · Vikram Garhwal

Add fdt_ prefix to overlay_get_target() and remove static type. This is done to get the target path for all the overlay nodes. This is useful to find which nodes are to be added/removed in dt_host.

Also, sending this patch to dtc mailing list to avoid the divergence.

Can you add a link to the thread on the DTC ML?

Cheers,

Signature · 1 line
Julien Grall
#9 lore raw

RFC PATCH v2 5/12 device tree: Add _dt_find_node_by_path() to find nodes in device tree

In reply to Vikram Garhwal

Add _dt_find_by_path() to find a matching node with path for a dt_device_node.

---
 xen/common/device_tree.c      | 10 ++++++++--
 xen/include/xen/device_tree.h |  9 +++++++++
 2 files changed, 17 insertions(+), 2 deletions(-)
xen/common/device_tree.c +8 -2
index 88f3f7e..26d2e28 100644
@@ -358,17 +358,23 @@ struct dt_device_node *dt_find_node_by_type(struct dt_device_node *from,
     return np;
 }
 
-struct dt_device_node *dt_find_node_by_path(const char *path)
+struct dt_device_node *_dt_find_node_by_path(struct dt_device_node *dt,
+                                             const char *path)
 {
     struct dt_device_node *np;
 
-    dt_for_each_device_node(dt_host, np)
+    dt_for_each_device_node(dt, np)
         if ( np->full_name && (dt_node_cmp(np->full_name, path) == 0) )
             break;
 
     return np;
 }
 
+struct dt_device_node *dt_find_node_by_path(const char *path)
+{
+    return _dt_find_node_by_path(dt_host, path);
+}
+
 void dt_print_node_names(struct dt_device_node *dt)
 {
     struct dt_device_node *np;
xen/include/xen/device_tree.h +9 -0
index 9fc63ab..5ba26a0 100644
@@ -525,6 +525,15 @@ struct dt_device_node *dt_find_node_by_alias(const char *alias);
  */
 struct dt_device_node *dt_find_node_by_path(const char *path);
 
+/**
+ * _dt_find_node_by_path - Find a node matching a full DT path
+ * @dt_node: The device tree to search
+ * @path: The full path to match
+ *
+ * Returns a node pointer.
+ */
+struct dt_device_node *_dt_find_node_by_path(struct dt_device_node *dt,
+                                             const char *path);
 
 /**
  * dt_find_node_by_gpath - Same as dt_find_node_by_path but retrieve the
Signature · 1 line
2.7.4
#10 lore raw

RFC PATCH v2 6/12 xen/smmu: Add remove_device callback for smmu_iommu ops

In reply to Vikram Garhwal

Add remove_device callback for removing the device entry from smmu-master using following steps:
1. Find if SMMU master exists for the device node.
2. Remove the SMMU master

---
 xen/drivers/passthrough/arm/smmu.c | 54 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
xen/drivers/passthrough/arm/smmu.c +54 -0
index c9dfc4c..1a32e2c 100644
@@ -816,6 +816,17 @@ static int insert_smmu_master(struct arm_smmu_device *smmu,
 	return 0;
 }
 
+static int remove_smmu_master(struct arm_smmu_device *smmu,
+			      struct arm_smmu_master *master)
+{
+	if (!(smmu->masters.rb_node))
+		return -ENOENT;
+
+	rb_erase(&master->node, &smmu->masters);
+
+	return 0;
+}
+
 static int arm_smmu_dt_add_device_legacy(struct arm_smmu_device *smmu,
 					 struct device *dev,
 					 struct iommu_fwspec *fwspec)
@@ -853,6 +864,32 @@ static int arm_smmu_dt_add_device_legacy(struct arm_smmu_device *smmu,
 	return insert_smmu_master(smmu, master);
 }
 
+static int arm_smmu_dt_remove_device_legacy(struct arm_smmu_device *smmu,
+					 struct device *dev)
+{
+	struct arm_smmu_master *master;
+	struct device_node *dev_node = dev_get_dev_node(dev);
+	int ret;
+
+	master = find_smmu_master(smmu, dev_node);
+	if (master == NULL) {
+		dev_err(dev,
+			"No registrations found for master device %s\n",
+			dev_node->name);
+		return -EINVAL;
+	}
+
+	ret = remove_smmu_master(smmu, master);
+
+	if (ret)
+		return ret;
+
+    dev_node->is_protected = false;
+
+	kfree(master);
+	return 0;
+}
+
 static int register_smmu_master(struct arm_smmu_device *smmu,
 				struct device *dev,
 				struct of_phandle_args *masterspec)
@@ -876,6 +913,22 @@ static int register_smmu_master(struct arm_smmu_device *smmu,
 					     fwspec);
 }
 
+static int arm_smmu_dt_remove_device_generic(u8 devfn, struct device *dev)
+{
+	struct arm_smmu_device *smmu;
+	struct iommu_fwspec *fwspec;
+
+	fwspec = dev_iommu_fwspec_get(dev);
+	if (fwspec == NULL)
+		return -ENXIO;
+
+	smmu = find_smmu(fwspec->iommu_dev);
+	if (smmu == NULL)
+		return -ENXIO;
+
+	return arm_smmu_dt_remove_device_legacy(smmu, dev);
+}
+
 static int arm_smmu_dt_add_device_generic(u8 devfn, struct device *dev)
 {
 	struct arm_smmu_device *smmu;
@@ -2876,6 +2929,7 @@ static const struct iommu_ops arm_smmu_iommu_ops = {
     .init = arm_smmu_iommu_domain_init,
     .hwdom_init = arm_smmu_iommu_hwdom_init,
     .add_device = arm_smmu_dt_add_device_generic,
+    .remove_device = arm_smmu_dt_remove_device_generic,
     .teardown = arm_smmu_iommu_domain_teardown,
     .iotlb_flush = arm_smmu_iotlb_flush,
     .iotlb_flush_all = arm_smmu_iotlb_flush_all,
Signature · 1 line
2.7.4
#11 lore raw

Re: [XEN][RFC PATCH v2 06/12] xen/smmu: Add remove_device callback for smmu_iommu ops

Hi,

Quoted 15 lines · Vikram Garhwal

Add remove_device callback for removing the device entry from smmu-master using following steps:
1. Find if SMMU master exists for the device node.
2. Remove the SMMU master

---
  xen/drivers/passthrough/arm/smmu.c | 54 ++++++++++++++++++++++++++++++++++++++
  1 file changed, 54 insertions(+)
xen/drivers/passthrough/arm/smmu.c +0 -0
index c9dfc4c..1a32e2c 100644

xen/drivers/passthrough/arm/smmu.c static int insert_smmu_master(struct arm_smmu_device *smmu, · line 822

Quoted 7 lines
xen/drivers/passthrough/arm/smmu.c +4 -0
@@ -816,6 +816,17 @@ static int insert_smmu_master(struct arm_smmu_device *smmu,
  	return 0;
  }
 
+static int remove_smmu_master(struct arm_smmu_device *smmu,
+			      struct arm_smmu_master *master)
+{
+	if (!(smmu->masters.rb_node))

Coding style: The inner () are not necessary.

Also, is this check only there for code hardening purpose? If yes, then I would add an ASSERT_UNREACHABLE() in the if body to allow catching the error in debug build.

Quoted 18 lines
xen/drivers/passthrough/arm/smmu.c +11 -0
+		return -ENOENT;
+
+	rb_erase(&master->node, &smmu->masters);
+
+	return 0;
+}
+
  static int arm_smmu_dt_add_device_legacy(struct arm_smmu_device *smmu,
  					 struct device *dev,
  					 struct iommu_fwspec *fwspec)
@@ -853,6 +864,32 @@ static int arm_smmu_dt_add_device_legacy(struct arm_smmu_device *smmu,
  	return insert_smmu_master(smmu, master);
  }
 
+static int arm_smmu_dt_remove_device_legacy(struct arm_smmu_device *smmu,
+					 struct device *dev)
+{
+	struct arm_smmu_master *master;

xen/drivers/passthrough/arm/smmu.c static int arm_smmu_dt_add_device_legacy(struct arm_smmu_device *smmu, · line 880

Quoted 10 lines
xen/drivers/passthrough/arm/smmu.c +10 -0
+	struct device_node *dev_node = dev_get_dev_node(dev);
+	int ret;
+
+	master = find_smmu_master(smmu, dev_node);
+	if (master == NULL) {
+		dev_err(dev,
+			"No registrations found for master device %s\n",
+			dev_node->name);
+		return -EINVAL;
+	}

Even if the IOMMU subsystem check it, it would be good that the SMMU driver also check the device is not currently used before removing it.

If it is, then we should return -EBUSY.

xen/drivers/passthrough/arm/smmu.c static int arm_smmu_dt_add_device_legacy(struct arm_smmu_device *smmu, · line 887

Quoted 7 lines
xen/drivers/passthrough/arm/smmu.c +7 -0
+
+	ret = remove_smmu_master(smmu, master);
+
+	if (ret)
+		return ret;
+
+    dev_node->is_protected = false;

Coding style: The indentation looks off compare to the other line.

Quoted 32 lines
xen/drivers/passthrough/arm/smmu.c +21 -0
+
+	kfree(master);
+	return 0;
+}
+
  static int register_smmu_master(struct arm_smmu_device *smmu,
  				struct device *dev,
  				struct of_phandle_args *masterspec)
@@ -876,6 +913,22 @@ static int register_smmu_master(struct arm_smmu_device *smmu,
  					     fwspec);
  }
 
+static int arm_smmu_dt_remove_device_generic(u8 devfn, struct device *dev)
+{
+	struct arm_smmu_device *smmu;
+	struct iommu_fwspec *fwspec;
+
+	fwspec = dev_iommu_fwspec_get(dev);
+	if (fwspec == NULL)
+		return -ENXIO;
+
+	smmu = find_smmu(fwspec->iommu_dev);
+	if (smmu == NULL)
+		return -ENXIO;
+
+	return arm_smmu_dt_remove_device_legacy(smmu, dev);
+}
+
  static int arm_smmu_dt_add_device_generic(u8 devfn, struct device *dev)
  {
  	struct arm_smmu_device *smmu;

xen/drivers/passthrough/arm/smmu.c static const struct iommu_ops arm_smmu_iommu_ops = { · line 2935

Quoted 7 lines
xen/drivers/passthrough/arm/smmu.c +1 -0
@@ -2876,6 +2929,7 @@ static const struct iommu_ops arm_smmu_iommu_ops = {
      .init = arm_smmu_iommu_domain_init,
      .hwdom_init = arm_smmu_iommu_hwdom_init,
      .add_device = arm_smmu_dt_add_device_generic,
+    .remove_device = arm_smmu_dt_remove_device_generic,
      .teardown = arm_smmu_iommu_domain_teardown,
      .iotlb_flush = arm_smmu_iotlb_flush,
      .iotlb_flush_all = arm_smmu_iotlb_flush_all,

Cheers,

Signature · 1 line
Julien Grall
#12 lore raw

RFC PATCH v2 7/12 xen/smmu: Add remove_device callback for smmu_iommu ops

In reply to Vikram Garhwal

Add remove_device callback for removing the device entry from smmu-master using
following steps:
1. Find if SMMU master exists for the device node.
2. Remove the SMMU master

---
 xen/drivers/passthrough/device_tree.c | 30 ++++++++++++++++++++++++++++++
 xen/include/xen/iommu.h               |  2 ++
 2 files changed, 32 insertions(+)
xen/drivers/passthrough/device_tree.c +30 -0
index 98f2aa0..9d9eed8 100644
@@ -127,6 +127,36 @@ int iommu_release_dt_devices(struct domain *d)
     return 0;
 }
 
+int iommu_remove_dt_device(struct dt_device_node *np)
+{
+    const struct iommu_ops *ops = iommu_get_ops();
+    struct device *dev = dt_to_dev(np);
+    int rc;
+
+    if ( !ops )
+        return -EOPNOTSUPP;
+
+    if ( iommu_dt_device_is_assigned(np) )
+        return -EPERM;
+
+    /*
+     * The driver which supports generic IOMMU DT bindings must have
+     * these callback implemented.
+     */
+    if ( !ops->remove_device )
+        return -EOPNOTSUPP;
+
+    /*
+     * Remove master device from the IOMMU if latter is present and available.
+     */
+    rc = ops->remove_device(0, dev);
+
+    if ( rc == 0 )
+        iommu_fwspec_free(dev);
+
+    return rc;
+}
+
 int iommu_add_dt_device(struct dt_device_node *np)
 {
     const struct iommu_ops *ops = iommu_get_ops();
xen/include/xen/iommu.h +2 -0
index 6b2cdff..c4d5d12 100644
@@ -215,6 +215,8 @@ int iommu_release_dt_devices(struct domain *d);
  */
 int iommu_add_dt_device(struct dt_device_node *np);
 
+int iommu_remove_dt_device(struct dt_device_node *np);
+
 int iommu_do_dt_domctl(struct xen_domctl *, struct domain *,
                        XEN_GUEST_HANDLE_PARAM(xen_domctl_t));
 
Signature · 1 line
2.7.4
#13 lore raw

Re: [XEN][RFC PATCH v2 07/12] xen/smmu: Add remove_device callback for smmu_iommu ops

Hi,

Quoted 4 lines · Vikram Garhwal

Add remove_device callback for removing the device entry from smmu-master using
following steps:
1. Find if SMMU master exists for the device node.
2. Remove the SMMU master

The commit title and message doesn't match the code.

Quoted 16 lines
---
  xen/drivers/passthrough/device_tree.c | 30 ++++++++++++++++++++++++++++++
  xen/include/xen/iommu.h               |  2 ++
  2 files changed, 32 insertions(+)
xen/drivers/passthrough/device_tree.c +1 -0
index 98f2aa0..9d9eed8 100644
@@ -127,6 +127,36 @@ int iommu_release_dt_devices(struct domain *d)
      return 0;
  }
 
+int iommu_remove_dt_device(struct dt_device_node *np)

xen/drivers/passthrough/device_tree.c int iommu_release_dt_devices(struct domain *d) · line 140

Quoted 10 lines
xen/drivers/passthrough/device_tree.c +10 -0
+{
+    const struct iommu_ops *ops = iommu_get_ops();
+    struct device *dev = dt_to_dev(np);
+    int rc;
+
+    if ( !ops )
+        return -EOPNOTSUPP;
+
+    if ( iommu_dt_device_is_assigned(np) )
+        return -EPERM;

EPERM means the caller doesn't have the permission to request it. However, dom0 will have the permission to remote the device. The problem is the device is currently assigned to a domain. So it would be better to return EBUSY.

Also, most of the function wants to be protected with dtdevs_lock to prevent concurrent access to add/remove/assign/deassign.

I would create a version of iommu_dt_device_is_assigned() (maybe called iommu_dt_device_is_assigned_locked()) that would do the same but with the caller hold the lock.

Quoted 27 lines
xen/drivers/passthrough/device_tree.c +19 -0
+
+    /*
+     * The driver which supports generic IOMMU DT bindings must have
+     * these callback implemented.
+     */
+    if ( !ops->remove_device )
+        return -EOPNOTSUPP;
+
+    /*
+     * Remove master device from the IOMMU if latter is present and available.
+     */
+    rc = ops->remove_device(0, dev);
+
+    if ( rc == 0 )
+        iommu_fwspec_free(dev);
+
+    return rc;
+}
+
  int iommu_add_dt_device(struct dt_device_node *np)
  {
      const struct iommu_ops *ops = iommu_get_ops();
xen/include/xen/iommu.h +0 -0
index 6b2cdff..c4d5d12 100644

xen/include/xen/iommu.h int iommu_release_dt_devices(struct domain *d); · line 222

Quoted 8 lines
xen/include/xen/iommu.h +2 -0
@@ -215,6 +215,8 @@ int iommu_release_dt_devices(struct domain *d);
   */
  int iommu_add_dt_device(struct dt_device_node *np);
 
+int iommu_remove_dt_device(struct dt_device_node *np);
+
  int iommu_do_dt_domctl(struct xen_domctl *, struct domain *,
                         XEN_GUEST_HANDLE_PARAM(xen_domctl_t));
 

Cheers,

Signature · 1 line
Julien Grall
#14 lore raw

RFC PATCH v2 8/12 xen/arm: Implement device tree node removal functionalities

In reply to Vikram Garhwal

Introduce sysctl XEN_SYSCTL_overlay to remove device-tree nodes added using device tree overlay.

xl overlay remove file.dtbo:
Removes all the nodes in a given dtbo.
First, removes IRQ permissions and MMIO accesses. Next, it finds the nodes in dt_host and delete the device node entries from dt_host.

The nodes get removed only if it is not used by any of dom0 or domus. If even one of the node in dtbo is not available for removal i.e. either not there in dt_host or currently used by any domain, in that case we don't remove any node in the given dtbo.

Also, added overlay_track struct to keep the track of added node through device tree overlay. overlay_track has dt_host_new which is unflattened form of updated fdt and name of overlay node. When a node is removed, we also free the memory used by overlay_track for the particular overlay node.

---
 xen/common/device_tree.c      |  53 ++++++
 xen/common/sysctl.c           | 372 ++++++++++++++++++++++++++++++++++++++++++
 xen/include/public/sysctl.h   |  23 +++
 xen/include/xen/device_tree.h |   4 +
 4 files changed, 452 insertions(+)
xen/common/device_tree.c +53 -0
index 26d2e28..19320e1 100644
@@ -385,6 +385,59 @@ void dt_print_node_names(struct dt_device_node *dt)
     return;
 }
 
+#if defined (CONFIG_OVERLAY_DTB)
+int overlay_remove_node(struct dt_device_node *device_node)
+{
+    struct dt_device_node *np;
+    struct dt_device_node *parent_node;
+    struct dt_device_node *current_node;
+
+    parent_node = device_node->parent;
+
+    current_node = parent_node;
+
+    if ( parent_node == NULL )
+    {
+        dt_dprintk("%s's parent node not found\n", device_node->name);
+        return -EFAULT;
+    }
+
+    np = parent_node->child;
+
+    if ( np == NULL )
+    {
+        dt_dprintk("parent node %s's not found\n", parent_node->name);
+        return -EFAULT;
+    }
+
+    /* If node to be removed is only child node or first child. */
+    if ( np->name == device_node->name )
+    {
+        current_node->allnext = np->next;
+        return 0;
+    }
+
+    for ( np = parent_node->child; np->sibling != NULL; np = np->sibling )
+    {
+        current_node = np;
+        if ( np->sibling->name == device_node->name )
+        {
+            /* Found the node. Now we remove it. */
+            current_node->allnext = np->allnext->allnext;
+
+            if ( np->sibling->sibling )
+                current_node->sibling = np->sibling->sibling;
+            else
+                current_node->sibling = NULL;
+
+            break;
+        }
+    }
+
+    return 0;
+}
+#endif
+
 int dt_find_node_by_gpath(XEN_GUEST_HANDLE(char) u_path, uint32_t u_plen,
                           struct dt_device_node **node)
 {
xen/common/sysctl.c +372 -0

384 lines not on this page · raw message

xen/include/public/sysctl.h +23 -0
index 3e53681..6624724 100644
@@ -1065,6 +1065,25 @@ typedef struct xen_sysctl_cpu_policy xen_sysctl_cpu_policy_t;
 DEFINE_XEN_GUEST_HANDLE(xen_sysctl_cpu_policy_t);
 #endif
 
+#if defined (CONFIG_OVERLAY_DTB)
+#define XEN_SYSCTL_DT_OVERLAY_ADD                   1
+#define XEN_SYSCTL_DT_OVERLAY_REMOVE                2
+
+/*
+ * XEN_SYSCTL_overlay
+ * Performs addition/removal of device tree nodes under parent node using dtbo.
+ * This does in three steps:
+ *  - Adds/Removes the nodes from dt_host.
+ *  - Adds/Removes IRQ permission for the nodes.
+ *  - Adds/Removes MMIO accesses.
+ */
+struct xen_sysctl_overlay_dt {
+    XEN_GUEST_HANDLE_64(void) overlay_fdt;
+    uint32_t overlay_fdt_size;  /* Overlay dtb size. */
+    uint8_t overlay_op; /* Add or remove. */
+};
+#endif
+
 struct xen_sysctl {
     uint32_t cmd;
 #define XEN_SYSCTL_readconsole                    1
@@ -1095,6 +1114,7 @@ struct xen_sysctl {
 #define XEN_SYSCTL_livepatch_op                  27
 /* #define XEN_SYSCTL_set_parameter              28 */
 #define XEN_SYSCTL_get_cpu_policy                29
+#define XEN_SYSCTL_overlay                       30
     uint32_t interface_version; /* XEN_SYSCTL_INTERFACE_VERSION */
     union {
         struct xen_sysctl_readconsole       readconsole;
@@ -1125,6 +1145,9 @@ struct xen_sysctl {
 #if defined(__i386__) || defined(__x86_64__)
         struct xen_sysctl_cpu_policy        cpu_policy;
 #endif
+#if defined (CONFIG_OVERLAY_DTB)
+        struct xen_sysctl_overlay_dt       overlay_dt;
+#endif
         uint8_t                             pad[128];
     } u;
 };
xen/include/xen/device_tree.h +4 -0
index 5ba26a0..cf29cf5 100644
@@ -553,6 +553,10 @@ int dt_find_node_by_gpath(XEN_GUEST_HANDLE(char) u_path, uint32_t u_plen,
  */
 void dt_print_node_names(struct dt_device_node *dt);
 
+#if defined (CONFIG_OVERLAY_DTB)
+int overlay_remove_node(struct dt_device_node *device_node);
+#endif
+
 /**
  * dt_get_parent - Get a node's parent if any
  * @node: Node to get parent
Signature · 1 line
2.7.4
#15 lore raw

Re: [XEN][RFC PATCH v2 08/12] xen/arm: Implement device tree node removal functionalities

Quoted 2 lines · Vikram Garhwal

Introduce sysctl XEN_SYSCTL_overlay to remove device-tree nodes added using device tree overlay.

XEN_SYSCTL_overlay is too generic a name imo.

Quoted 63 lines
xen/common/sysctl.c +59 -0
@@ -476,6 +781,73 @@ long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl)
             copyback = 1;
         break;
 
+#if defined (CONFIG_OVERLAY_DTB)
+    case XEN_SYSCTL_overlay:
+    {
+        void *overlay_fdt;
+        char **node_full_path = NULL;
+        int num_overlay_nodes;
+
+        if ( op->u.overlay_dt.overlay_fdt_size > 0 )
+            overlay_fdt = xmalloc_bytes(op->u.overlay_dt.overlay_fdt_size);
+        else
+        {
+            ret = -EINVAL;
+            break;
+        }
+
+        if ( overlay_fdt == NULL )
+        {
+            ret = -ENOMEM;
+            break;
+        }
+
+        ret = copy_from_guest(overlay_fdt, op->u.overlay_dt.overlay_fdt,
+                             op->u.overlay_dt.overlay_fdt_size);
+        if ( ret )
+        {
+            gprintk(XENLOG_ERR, "copy from guest failed\n");
+            xfree(overlay_fdt);
+
+            ret = -EFAULT;
+            break;
+        }
+
+        if ( op->u.overlay_dt.overlay_op == XEN_SYSCTL_DT_OVERLAY_ADD )
+        {
+            ret = handle_add_overlay_nodes(overlay_fdt,
+                                           op->u.overlay_dt.overlay_fdt_size);
+        } else if ( op->u.overlay_dt.overlay_op ==
+                                        XEN_SYSCTL_DT_OVERLAY_REMOVE )
+        {
+            ret = check_overlay_fdt(overlay_fdt,
+                                    op->u.overlay_dt.overlay_fdt_size);
+            if ( ret )
+            {
+                ret = -EFAULT;
+                break;
+            }
+
+            num_overlay_nodes = overlay_node_count(overlay_fdt);
+            if ( num_overlay_nodes == 0 )
+            {
+                ret = -ENOMEM;
+                break;
+            }
+
+            overlay_get_node_info(overlay_fdt, &node_full_path,
+                                  num_overlay_nodes);
+
+            ret = handle_remove_overlay_nodes(node_full_path,
+                                              num_overlay_nodes);

xen/common/sysctl.c long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl) · line 852

Quoted 10 lines
xen/common/sysctl.c +8 -0
+        }
+
+        xfree(node_full_path);
+
+        break;
+    }
+#endif
+
     default:
         ret = arch_do_sysctl(op, u_sysctl);

Seeing this call is even in (patch) context - would you mind clarifying why you don't add the new code to arch_do_sysctl() (perhaps by way of further forwarding to a new dt_sysctl(), which could then live in a DT- specific source file)?

Quoted 13 lines
xen/include/public/sysctl.h +7 -0
@@ -1065,6 +1065,25 @@ typedef struct xen_sysctl_cpu_policy xen_sysctl_cpu_policy_t;
 DEFINE_XEN_GUEST_HANDLE(xen_sysctl_cpu_policy_t);
 #endif
 
+#if defined (CONFIG_OVERLAY_DTB)
+#define XEN_SYSCTL_DT_OVERLAY_ADD                   1
+#define XEN_SYSCTL_DT_OVERLAY_REMOVE                2
+
+/*
+ * XEN_SYSCTL_overlay
+ * Performs addition/removal of device tree nodes under parent node using dtbo.

xen/include/public/sysctl.h typedef struct xen_sysctl_cpu_policy xen_sysctl_cpu_policy_t; · line 1084

Quoted 10 lines
xen/include/public/sysctl.h +10 -0
+ * This does in three steps:
+ *  - Adds/Removes the nodes from dt_host.
+ *  - Adds/Removes IRQ permission for the nodes.
+ *  - Adds/Removes MMIO accesses.
+ */
+struct xen_sysctl_overlay_dt {
+    XEN_GUEST_HANDLE_64(void) overlay_fdt;
+    uint32_t overlay_fdt_size;  /* Overlay dtb size. */
+    uint8_t overlay_op; /* Add or remove. */
+};

Please make padding explicit and check that it's zero on input (so it can later be assigned a purpose without needing to bump the sysctl interface version).

xen/include/public/sysctl.h struct xen_sysctl { · line 1150

Quoted 7 lines
xen/include/public/sysctl.h +3 -0
@@ -1125,6 +1145,9 @@ struct xen_sysctl {
 #if defined(__i386__) || defined(__x86_64__)
         struct xen_sysctl_cpu_policy        cpu_policy;
 #endif
+#if defined (CONFIG_OVERLAY_DTB)
+        struct xen_sysctl_overlay_dt       overlay_dt;
+#endif

No CONFIG_* dependencies in public headers, please.

Jan

#16 lore raw

Re: [XEN][RFC PATCH v2 08/12] xen/arm: Implement device tree node removal functionalities

In reply to Vikram Garhwal

Hi,

Quoted 1 line · Vikram Garhwal

Introduce sysctl XEN_SYSCTL_overlay to remove device-tree nodes added using

I agree with Jan about the name being too generic. I will comment on this a bit further down.

Quoted 30 lines

device tree overlay.

xl overlay remove file.dtbo:
Removes all the nodes in a given dtbo.
First, removes IRQ permissions and MMIO accesses. Next, it finds the nodes in dt_host and delete the device node entries from dt_host.

The nodes get removed only if it is not used by any of dom0 or domus. If even one of the node in dtbo is not available for removal i.e. either not there in dt_host or currently used by any domain, in that case we don't remove any node in the given dtbo.

Also, added overlay_track struct to keep the track of added node through device tree overlay. overlay_track has dt_host_new which is unflattened form of updated fdt and name of overlay node. When a node is removed, we also free the memory used by overlay_track for the particular overlay node.

---
  xen/common/device_tree.c      |  53 ++++++
  xen/common/sysctl.c           | 372 ++++++++++++++++++++++++++++++++++++++++++
  xen/include/public/sysctl.h   |  23 +++
  xen/include/xen/device_tree.h |   4 +
  4 files changed, 452 insertions(+)
xen/common/device_tree.c +0 -0
index 26d2e28..19320e1 100644

xen/common/device_tree.c void dt_print_node_names(struct dt_device_node *dt) · line 389

Quoted 5 lines
xen/common/device_tree.c +2 -0
@@ -385,6 +385,59 @@ void dt_print_node_names(struct dt_device_node *dt)
      return;
  }
 
+#if defined (CONFIG_OVERLAY_DTB)
+int overlay_remove_node(struct dt_device_node *device_node)

This want to be prefixed with dt_* so it is clear which components it is touching. But I think all the DT overlay code (including sysctl) should be moved in a new file (maybe dt_overlay.c) to spreading the overlay code and growing the size of sysctl.c.

Quoted 15 lines
xen/common/device_tree.c +15 -0
+{
+    struct dt_device_node *np;
+    struct dt_device_node *parent_node;
+    struct dt_device_node *current_node;
+
+    parent_node = device_node->parent;
+
+    current_node = parent_node;
+
+    if ( parent_node == NULL )
+    {
+        dt_dprintk("%s's parent node not found\n", device_node->name);
+        return -EFAULT;
+    }
+

xen/common/device_tree.c void dt_print_node_names(struct dt_device_node *dt) · line 414

Quoted 10 lines
xen/common/device_tree.c +10 -0
+    np = parent_node->child;
+
+    if ( np == NULL )
+    {
+        dt_dprintk("parent node %s's not found\n", parent_node->name);
+        return -EFAULT;
+    }
+
+    /* If node to be removed is only child node or first child. */
+    if ( np->name == device_node->name )

Why are you checking the equality between the fields name rather than the node itself?

If it is intended, then I think this wants to be explained because often people wants to check the names are equal (e.g str*cmp()) rather than the pointer where the names are stored.

xen/common/device_tree.c void dt_print_node_names(struct dt_device_node *dt) · line 423

Quoted 9 lines
xen/common/device_tree.c +9 -0
+    {
+        current_node->allnext = np->next;
+        return 0;
+    }
+
+    for ( np = parent_node->child; np->sibling != NULL; np = np->sibling )
+    {
+        current_node = np;
+        if ( np->sibling->name == device_node->name )

Same question.

Quoted 25 lines
xen/common/device_tree.c +17 -0
+        {
+            /* Found the node. Now we remove it. */
+            current_node->allnext = np->allnext->allnext;
+
+            if ( np->sibling->sibling )
+                current_node->sibling = np->sibling->sibling;
+            else
+                current_node->sibling = NULL;
+
+            break;
+        }
+    }
+
+    return 0;
+}
+#endif
+
  int dt_find_node_by_gpath(XEN_GUEST_HANDLE(char) u_path, uint32_t u_plen,
                            struct dt_device_node **node)
  {
xen/common/sysctl.c +0 -0
index f2dab72..fca47f5 100644

xen/common/sysctl.c line 31

Quoted 4 lines
xen/common/sysctl.c +1 -0
@@ -28,6 +28,311 @@
  #include <xen/livepatch.h>
  #include <xen/coverage.h>
 
+#if defined (CONFIG_OVERLAY_DTB)

This can be shortend to #ifdef CONFIG_<...>.

xen/common/sysctl.c line 39

Quoted 8 lines
xen/common/sysctl.c +8 -0
+#include <xen/list.h>
+#include <xen/libfdt/libfdt.h>
+#include <xen/xmalloc.h>
+#include <xen/device_tree.h>
+#include <asm/domain_build.h>
+#endif
+
+#if defined (CONFIG_OVERLAY_DTB)

Same here.

Quoted 4 lines
xen/common/sysctl.c +4 -0
+static LIST_HEAD(overlay_tracker);
+static DEFINE_SPINLOCK(overlay_lock);
+
+/*

xen/common/sysctl.c line 53

Quoted 10 lines
xen/common/sysctl.c +10 -0
+ * overlay_node_track describes information about added nodes through dtbo.
+ * @dt_host_new: Pointer to the updated dt_host_new unflattened 'updated fdt'.
+ * @node_fullname: Store the name of nodes.
+ * @entry: List pointer.
+ */
+struct overlay_track {
+    struct list_head entry;
+    struct dt_device_node *dt_host_new;
+    char **node_fullname;
+    uint8_t num_nodes;

Any reason to restrict to 256 nodes?

xen/common/sysctl.c line 57

Quoted 4 lines
xen/common/sysctl.c +4 -0
+};
+
+/* Basic sanity check for the dtbo tool stack provided to Xen. */
+static int check_overlay_fdt(void *overlay_fdt, uint32_t overlay_fdt_size)

This function doesn't modify overlay_fdt. So I think it should be const if fdt_total_size() and fdt_check_header() allows it.

Quoted 4 lines
xen/common/sysctl.c +4 -0
+{
+    if ( (fdt_totalsize(overlay_fdt) != overlay_fdt_size) ||
+          fdt_check_header(overlay_fdt) )
+    {

xen/common/sysctl.c line 71

Quoted 10 lines
xen/common/sysctl.c +10 -0
+        printk(XENLOG_ERR "The overlay FDT is not a valid Flat Device Tree\n");
+        return -EINVAL;
+    }
+
+    return 0;
+}
+
+static int overlay_node_count(void *fdto)
+{
+    int num_overlay_nodes = 0;

The name suggests this should be an unsiged int.

xen/common/sysctl.c line 80

Quoted 9 lines
xen/common/sysctl.c +9 -0
+    int fragment;
+
+    fdt_for_each_subnode(fragment, fdto, 0)
+    {
+
+        int subnode;
+        int overlay;
+
+        overlay = fdt_subnode_offset(fdto, fragment, "__overlay__");

This may return < 0 if __overlay__ is not found. From my understanding, fdt_for_each_subnode() would end up to start from 0.

So I think you want to add a check here.

Quoted 6 lines
xen/common/sysctl.c +6 -0
+
+        fdt_for_each_subnode(subnode, fdto, overlay)
+        {
+            num_overlay_nodes++;
+        }
+    }

xen/common/sysctl.c line 96

Quoted 10 lines
xen/common/sysctl.c +10 -0
+
+    return num_overlay_nodes;
+}
+
+/*
+ * overlay_get_node_info will get the all node's full name with path. This is
+ * useful when checking node for duplication i.e. dtbo tries to add nodes which
+ * already exists in device tree.
+ */
+static void overlay_get_node_info(void *fdto, char ***node_full_path,

You will retrieve mutiple nodes. So I think the function wants to be named 'overlay_get_nodes_info()'. Same for node_full_path.

Furthermore, you could drop one * if you return the list of paths. This will also allow you to return an error when xmalloc fails (see below)

Lastly, AFAICT, fdto can be const.

xen/common/sysctl.c line 97

Quoted 1 line
xen/common/sysctl.c +1 -0
+                                  int num_overlay_nodes)

This coud be unsigned int.

xen/common/sysctl.c line 100

Quoted 3 lines
xen/common/sysctl.c +3 -0
+{
+    int fragment;
+    int node_num = 0;

This could be unsigned int.

xen/common/sysctl.c line 102

Quoted 2 lines
xen/common/sysctl.c +2 -0
+
+    *node_full_path = xmalloc_bytes(num_overlay_nodes * sizeof(char *));

Memory allocation can fail.

xen/common/sysctl.c line 112

Quoted 10 lines
xen/common/sysctl.c +10 -0
+
+    fdt_for_each_subnode(fragment, fdto, 0)
+    {
+        int target;
+        int overlay;
+        int subnode;
+        const char *target_path;
+
+        target = fdt_overlay_get_target(device_tree_flattened, fdto, fragment,
+                                    &target_path);

fdt_overlay_get_target() can fail. Also, the indentation looks strange.

xen/common/sysctl.c line 113

Quoted 1 line
xen/common/sysctl.c +1 -0
+        overlay = fdt_subnode_offset(fdto, fragment, "__overlay__");

fdt_subnode_offset can fail.

xen/common/sysctl.c line 117

Quoted 4 lines
xen/common/sysctl.c +4 -0
+
+        fdt_for_each_subnode(subnode, fdto, overlay)
+        {
+            const char *node_name = fdt_get_name(fdto, subnode, NULL);

AFAIU, fdt_get_name() can return NULL;

xen/common/sysctl.c line 118

Quoted 1 line
xen/common/sysctl.c +1 -0
+            int node_name_len = strlen(node_name);

fdt_get_name() can already provide the len for you. Can you re-use it?

xen/common/sysctl.c line 120

Quoted 2 lines
xen/common/sysctl.c +2 -0
+            int target_path_len = strlen(target_path);
+            int node_full_name_len = target_path_len + node_name_len + 2;

node_name_len, target_path_len, node_full_name_len can be unsigned. Also, I would add a comment explain what '2' refers to.

xen/common/sysctl.c line 122

Quoted 2 lines
xen/common/sysctl.c +2 -0
+
+            (*node_full_path)[node_num] = xmalloc_bytes(node_full_name_len);

xmalloc_bytes() can fail.

Quoted 9 lines
xen/common/sysctl.c +9 -0
+
+            memcpy((*node_full_path)[node_num], target_path, target_path_len);
+
+            (*node_full_path)[node_num][target_path_len] = '/';
+
+            memcpy((*node_full_path)[node_num] + target_path_len + 1, node_name,
+                   node_name_len);
+
+            (*node_full_path)[node_num][node_full_name_len - 1] = '\0';

xen/common/sysctl.c line 141

Quoted 10 lines
xen/common/sysctl.c +10 -0
+
+            node_num++;
+        }
+    }
+}
+
+/*
+ * Checks if all the devices node listed are present in dt_host and used by any
+ * domain.
+ */

Why do we need to handle all the nodes together? I think the code would end up to be simpler if we were handling node by node.

xen/common/sysctl.c line 147

Quoted 6 lines
xen/common/sysctl.c +6 -0
+static int check_nodes(char **full_dt_node_path, uint32_t num_nodes)
+{
+    int rc = 0;
+    unsigned int i;
+    struct dt_device_node *overlay_node;
+    uint32_t ret = 0;

AFAICT, you are storing a domid here, so this wants to be a domid_t and possible renamed to domid.

xen/common/sysctl.c line 158

Quoted 11 lines
xen/common/sysctl.c +11 -0
+
+    for ( i = 0; i < num_nodes; i++ ) {
+        dt_dprintk("Finding node %s in the dt_host\n", full_dt_node_path[i]);
+
+        overlay_node = dt_find_node_by_path(full_dt_node_path[i]);
+
+        if ( overlay_node == NULL )
+        {
+            rc = -EINVAL;
+
+            printk(XENLOG_G_ERR "Device %s is not present in the tree."

You seem to use a mix of XENLOG_G_ERR and XENLOG_ERR. However, it is not entirely clear some messages are more critical than the other. Could you clarify?

xen/common/sysctl.c line 159

Quoted 1 line
xen/common/sysctl.c +1 -0
+                   " Removing nodes failed\n", full_dt_node_path[i]);

Coding style: We don't split error message even if they are more than 80 lines. This helps grepping them in the log.

xen/common/sysctl.c line 168

Quoted 9 lines
xen/common/sysctl.c +9 -0
+            return rc;
+        }
+
+        ret = dt_device_used_by(overlay_node);
+
+        dt_dprintk("Checking if node %s is used by any domain\n",
+                   full_dt_node_path[i]);
+
+        if ( ret != 0 && ret != DOMID_IO )

In the commit message you wrote:

"The nodes get removed only if it is not used by any of dom0 or domus"

This implies that this should return -EINVAL for domid as well. Can you make sure the two matches? (I am not sure which one you want).

Also, what does prevent the device to not be assigned for the check?

xen/common/sysctl.c line 170

Quoted 2 lines
xen/common/sysctl.c +2 -0
+        {
+            rc = -EINVAL;

NIT: This is a bit pointless to set rc when it is just used 2 lines below in the return. The alternative is to replace the return with a break.

xen/common/sysctl.c line 173

Quoted 3 lines
xen/common/sysctl.c +3 -0
+
+            printk(XENLOG_G_ERR "Device %s as it is being used by domain %d."
+                   " Removing nodes failed\n", full_dt_node_path[i], ret);

Coding style: Same about the error message.

xen/common/sysctl.c line 182

Quoted 9 lines
xen/common/sysctl.c +9 -0
+            return rc;
+        }
+    }
+
+    return rc;
+}
+
+/* Remove nodes from dt_host. */
+static int remove_nodes(char **full_dt_node_path, uint32_t num_nodes)

In the commit message, you said you wanted to remove all the nodes together. But this function could possibly fail leaving some nodes present or not.

As I wrote above, I think handling node by node would be fine. However, we need to make sure that the remove operation can be called again to clean-up the rest of the nodes.

Quoted 16 lines
xen/common/sysctl.c +16 -0
+{
+    struct domain *d = hardware_domain;
+    int rc = 0;
+    struct dt_device_node *overlay_node;
+    unsigned int naddr;
+    unsigned int i, j, nirq;
+    struct dt_raw_irq rirq;
+    u64 addr, size;
+
+    for ( j = 0; j < num_nodes; j++ ) {
+        dt_dprintk("Removing node: %s\n", full_dt_node_path[j]);
+
+        overlay_node = dt_find_node_by_path(full_dt_node_path[j]);
+
+        nirq = dt_number_of_irq(overlay_node);
+

xen/common/sysctl.c line 208

Quoted 10 lines
xen/common/sysctl.c +10 -0
+        /* Remove IRQ permission */
+        for ( i = 0; i < nirq; i++ )
+        {
+            rc = dt_device_get_raw_irq(overlay_node, i, &rirq);
+            if ( rc )
+            {
+                printk(XENLOG_ERR "Unable to retrieve irq %u for %s\n",
+                       i, dt_node_full_name(overlay_node));
+                return rc;
+            }

The interrupt may not be routed to the GIC, in which case we want to skip them. So you want to check the controller is equal to dt_interrupt_controller.

The code is also quite similar to handle_device_interrupts(). So I would abstract the code to avoid duplication.

That said, I find a bit odd to have to parse the overlay again on remove given you expect the same to be passed. I think it might be better to use rangeset to keep track of the interrupts added with that specific overlay.

This will reduce the possibility that remove can go wrong.

xen/common/sysctl.c line 218

Quoted 10 lines
xen/common/sysctl.c +10 -0
+
+            rc = platform_get_irq(overlay_node, i);
+            if ( rc < 0 )
+            {
+                printk(XENLOG_ERR "Unable to get irq %u for %s\n",
+                       i, dt_node_full_name(overlay_node));
+                return rc;
+            }
+
+            rc = irq_deny_access(d, rc);

A few things:

1) IRQs can be assigned to another domain without the device being
assigned. So this want to be checked.
2) This is assuming the IRQ was not shared. I am not entirely sure
how to solve this one. Maybe a TODO and note in the documentation will do for now. Stefano?

Quoted 17 lines
xen/common/sysctl.c +17 -0
+            if ( rc )
+            {
+                printk(XENLOG_ERR "unable to revoke access for irq %u for %s\n",
+                       i, dt_node_full_name(overlay_node));
+                return rc;
+            }
+        }
+
+        rc = iommu_remove_dt_device(overlay_node);
+        if ( rc != 0 && rc != -ENXIO )
+            return rc;
+
+        naddr = dt_number_of_address(overlay_node);
+
+        /* Remove mmio access. */
+        for ( i = 0; i < naddr; i++ )
+        {

xen/common/sysctl.c line 245

Quoted 10 lines
xen/common/sysctl.c +10 -0
+            rc = dt_device_get_address(overlay_node, i, &addr, &size);
+            if ( rc )
+            {
+                printk(XENLOG_ERR "Unable to retrieve address %u for %s\n",
+                       i, dt_node_full_name(overlay_node));
+                return rc;
+            }
+
+            rc = iomem_deny_access(d, paddr_to_pfn(addr),
+                                   paddr_to_pfn(PAGE_ALIGN(addr + size - 1)));

Same remarks as for the interrupts here. Also, this remove the permission but not the mappings in the P2M. Is it intended?

Quoted 14 lines
xen/common/sysctl.c +14 -0
+            if ( rc )
+            {
+                printk(XENLOG_ERR "Unable to remove dom%d access to"
+                        " 0x%"PRIx64" - 0x%"PRIx64"\n",
+                        d->domain_id,
+                        addr & PAGE_MASK, PAGE_ALIGN(addr + size) - 1);
+                return rc;
+            }
+        }
+
+        rc = overlay_remove_node(overlay_node);
+        if ( rc )
+            return rc;
+    }

xen/common/sysctl.c line 269

Quoted 10 lines
xen/common/sysctl.c +10 -0
+
+    return rc;
+}
+
+/*
+ * First finds the device node to remove. Check if the device is being used by
+ * any dom and finally remove it from dt_host. IOMMU is already being taken care
+ * while destroying the domain.
+ */
+static long handle_remove_overlay_nodes(char **full_dt_node_path,

This is an array of nodes name, right? If so, please add a 's' to be make it clear.

xen/common/sysctl.c line 270

Quoted 1 line
xen/common/sysctl.c +1 -0
+                                        uint32_t num_nodes)

I think num_nodes can simply be unsigned int.

xen/common/sysctl.c line 282

Quoted 12 lines
xen/common/sysctl.c +12 -0
+{
+    int rc = 0;
+    struct overlay_track *entry, *temp, *track;
+    bool found_entry = false;
+    unsigned int i;
+
+    spin_lock(&overlay_lock);
+
+    /*
+     * First check if dtbo is correct i.e. it should one of the dtbo which was
+     * used when dynamically adding the node.
+     */

Technically, you can still pass a dtbo with the same node names but with different properties inside.

I am not suggesting to check it, but I think it would be good to explain the limitation (this will be important when we decide to support/security support it).

xen/common/sysctl.c line 288

Quoted 6 lines
xen/common/sysctl.c +6 -0
+    list_for_each_entry_safe( entry, temp, &overlay_tracker, entry )
+    {
+        /* Checking the num of nodes first. If not same skip to next entry. */
+        if ( num_nodes == entry->num_nodes )
+        {
+            for( i = 0; i < num_nodes; i++ ) {

Coding style:

for ( ... )
{

xen/common/sysctl.c line 289

Quoted 1 line
xen/common/sysctl.c +1 -0
+                if( strcmp(full_dt_node_path[i], entry->node_fullname[i]) )

Coding style: Space after 'if'

Quoted 4 lines
xen/common/sysctl.c +4 -0
+                {
+                    /* Node name didn't match. Skip to next entry. */
+                    break;
+                }

xen/common/sysctl.c line 303

Quoted 10 lines
xen/common/sysctl.c +10 -0
+            }
+
+            /* Found one tracker with all node name matching. */
+            track = entry;
+            found_entry = true;
+            break;
+        }
+    }
+
+    if ( found_entry == false ) {

Coding style: The { should be on its own line.

Quoted 35 lines
xen/common/sysctl.c +31 -0
+        rc = -EINVAL;
+
+        printk(XENLOG_G_ERR "Cannot find any matching tracker with input dtbo."
+               " Removing nodes is supported for only prior added dtbo. Please"
+               " provide a valid dtbo which was used to add the nodes.\n") > +        goto out;
+
+    }
+
+    rc = check_nodes(full_dt_node_path, num_nodes);
+
+    if ( rc )
+        goto out;
+
+    rc = remove_nodes(full_dt_node_path, num_nodes);
+
+    if ( rc ) {
+        printk(XENLOG_G_ERR "Removing node failed\n");
+        goto out;
+    }
+
+    list_del(&entry->entry);
+    xfree(entry->node_fullname);
+    xfree(entry->dt_host_new);
+    xfree(entry);
+
+out:
+    spin_unlock(&overlay_lock);
+    return rc;
+}
+#endif
+
  long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl)
  {
      long ret = 0;

xen/common/sysctl.c long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl) · line 789

Quoted 9 lines
xen/common/sysctl.c +6 -0
@@ -476,6 +781,73 @@ long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl)
              copyback = 1;
          break;
 
+#if defined (CONFIG_OVERLAY_DTB)
+    case XEN_SYSCTL_overlay:
+    {
+        void *overlay_fdt;
+        char **node_full_path = NULL;
+        int num_overlay_nodes;

I think this can be unsigned int.

xen/common/sysctl.c long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl) · line 797

Quoted 8 lines
xen/common/sysctl.c +8 -0
+
+        if ( op->u.overlay_dt.overlay_fdt_size > 0 )
+            overlay_fdt = xmalloc_bytes(op->u.overlay_dt.overlay_fdt_size);
+        else
+        {
+            ret = -EINVAL;
+            break;
+        }

I would re-order the code so the failure case is first. I.e:

if ( op->... <= 0 )
{
ret = -EINVAL;
break;
}

overlay_fdt = xmalloc...

xen/common/sysctl.c long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl) · line 809

Quoted 12 lines
xen/common/sysctl.c +12 -0
+
+        if ( overlay_fdt == NULL )
+        {
+            ret = -ENOMEM;
+            break;
+        }
+
+        ret = copy_from_guest(overlay_fdt, op->u.overlay_dt.overlay_fdt,
+                             op->u.overlay_dt.overlay_fdt_size);
+        if ( ret )
+        {
+            gprintk(XENLOG_ERR, "copy from guest failed\n");

See my remark about the printk() above. I think we need to be consistent in how we use it in the overlay code.

xen/common/sysctl.c long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl) · line 816

Quoted 7 lines
xen/common/sysctl.c +7 -0
+            xfree(overlay_fdt);
+
+            ret = -EFAULT;
+            break;
+        }
+
+        if ( op->u.overlay_dt.overlay_op == XEN_SYSCTL_DT_OVERLAY_ADD )

The code would be easier to read if you use a switch. However...

xen/common/sysctl.c long do_sysctl(XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl) · line 819

Quoted 3 lines
xen/common/sysctl.c +3 -0
+        {
+            ret = handle_add_overlay_nodes(overlay_fdt,
+                                           op->u.overlay_dt.overlay_fdt_size);

... this function doesn't exist. So this will break compilation.

Quoted 39 lines
xen/common/sysctl.c +31 -0
+        } else if ( op->u.overlay_dt.overlay_op ==
+                                        XEN_SYSCTL_DT_OVERLAY_REMOVE )
+        {
+            ret = check_overlay_fdt(overlay_fdt,
+                                    op->u.overlay_dt.overlay_fdt_size);
+            if ( ret )
+            {
+                ret = -EFAULT;
+                break;
+            }
+
+            num_overlay_nodes = overlay_node_count(overlay_fdt);
+            if ( num_overlay_nodes == 0 )
+            {
+                ret = -ENOMEM;
+                break;
+            }
+
+            overlay_get_node_info(overlay_fdt, &node_full_path,
+                                  num_overlay_nodes);
+
+            ret = handle_remove_overlay_nodes(node_full_path,
+                                              num_overlay_nodes);
+        }
+
+        xfree(node_full_path);
+
+        break;
+    }
+#endif
+
      default:
          ret = arch_do_sysctl(op, u_sysctl);
          copyback = 0;
xen/include/public/sysctl.h +0 -0
index 3e53681..6624724 100644

xen/include/public/sysctl.h typedef struct xen_sysctl_cpu_policy xen_sysctl_cpu_policy_t; · line 1069

Quoted 5 lines
xen/include/public/sysctl.h +2 -0
@@ -1065,6 +1065,25 @@ typedef struct xen_sysctl_cpu_policy xen_sysctl_cpu_policy_t;
  DEFINE_XEN_GUEST_HANDLE(xen_sysctl_cpu_policy_t);
  #endif
 
+#if defined (CONFIG_OVERLAY_DTB)
+#define XEN_SYSCTL_DT_OVERLAY_ADD                   1

I would add this one only when it is introduced.

xen/include/public/sysctl.h typedef struct xen_sysctl_cpu_policy xen_sysctl_cpu_policy_t; · line 1070

Quoted 1 line
xen/include/public/sysctl.h +1 -0
+#define XEN_SYSCTL_DT_OVERLAY_REMOVE                2

Here you are using XEN_SYSCTL_DT_OVERLAY...

xen/include/public/sysctl.h typedef struct xen_sysctl_cpu_policy xen_sysctl_cpu_policy_t; · line 1073

Quoted 3 lines
xen/include/public/sysctl.h +3 -0
+
+/*
+ * XEN_SYSCTL_overlay

... here overlay and...

xen/include/public/sysctl.h typedef struct xen_sysctl_cpu_policy xen_sysctl_cpu_policy_t; · line 1080

Quoted 7 lines
xen/include/public/sysctl.h +7 -0
+ * Performs addition/removal of device tree nodes under parent node using dtbo.
+ * This does in three steps:
+ *  - Adds/Removes the nodes from dt_host.
+ *  - Adds/Removes IRQ permission for the nodes.
+ *  - Adds/Removes MMIO accesses.
+ */
+struct xen_sysctl_overlay_dt {

... here overlay_dt. For the hypercall, it is important to keep the naming consistent. In this case, I would use

* defines: XEN_SYSCTL_DT_OVERLAY_<...>
* subop: XEN_SYSCTL_dt_overlay
* structure: xen_sysctl_dt_overlay

Quoted 32 lines
xen/include/public/sysctl.h +10 -0
+    XEN_GUEST_HANDLE_64(void) overlay_fdt;
+    uint32_t overlay_fdt_size;  /* Overlay dtb size. */
+    uint8_t overlay_op; /* Add or remove. */
+};
+#endif
+
  struct xen_sysctl {
      uint32_t cmd;
  #define XEN_SYSCTL_readconsole                    1
@@ -1095,6 +1114,7 @@ struct xen_sysctl {
  #define XEN_SYSCTL_livepatch_op                  27
  /* #define XEN_SYSCTL_set_parameter              28 */
  #define XEN_SYSCTL_get_cpu_policy                29
+#define XEN_SYSCTL_overlay                       30
      uint32_t interface_version; /* XEN_SYSCTL_INTERFACE_VERSION */
      union {
          struct xen_sysctl_readconsole       readconsole;
@@ -1125,6 +1145,9 @@ struct xen_sysctl {
  #if defined(__i386__) || defined(__x86_64__)
          struct xen_sysctl_cpu_policy        cpu_policy;
  #endif
+#if defined (CONFIG_OVERLAY_DTB)
+        struct xen_sysctl_overlay_dt       overlay_dt;
+#endif
          uint8_t                             pad[128];
      } u;
  };
xen/include/xen/device_tree.h +0 -0
index 5ba26a0..cf29cf5 100644

xen/include/xen/device_tree.h int dt_find_node_by_gpath(XEN_GUEST_HANDLE(char) u_path, uint32_t u_plen, · line 562

Quoted 10 lines
xen/include/xen/device_tree.h +4 -0
@@ -553,6 +553,10 @@ int dt_find_node_by_gpath(XEN_GUEST_HANDLE(char) u_path, uint32_t u_plen,
   */
  void dt_print_node_names(struct dt_device_node *dt);
 
+#if defined (CONFIG_OVERLAY_DTB)
+int overlay_remove_node(struct dt_device_node *device_node);
+#endif
+
  /**
   * dt_get_parent - Get a node's parent if any
   * @node: Node to get parent

Cheers,

Signature · 1 line
Julien Grall
#17 lore raw

Re: [XEN][RFC PATCH v2 08/12] xen/arm: Implement device tree node removal functionalities

Quoted 445 lines · Julien Grall

Hi,

Quoted 1 line · Vikram Garhwal

Introduce sysctl XEN_SYSCTL_overlay to remove device-tree nodes added using

I agree with Jan about the name being too generic. I will comment on this a bit further down.

Quoted 37 lines

device tree overlay.

xl overlay remove file.dtbo:
Removes all the nodes in a given dtbo.
First, removes IRQ permissions and MMIO accesses. Next, it finds the
nodes
in dt_host and delete the device node entries from dt_host.

The nodes get removed only if it is not used by any of dom0 or domus.
If
even one of the node in dtbo is not available for removal i.e. either
not
there in dt_host or currently used by any domain, in that case we don't remove any node in the given dtbo.

Also, added overlay_track struct to keep the track of added node through device
tree overlay. overlay_track has dt_host_new which is unflattened form of updated
fdt and name of overlay node. When a node is removed, we also free the memory
used by overlay_track for the particular overlay node.

---
  xen/common/device_tree.c      |  53 ++++++
  xen/common/sysctl.c           | 372
++++++++++++++++++++++++++++++++++++++++++
  xen/include/public/sysctl.h   |  23 +++
  xen/include/xen/device_tree.h |   4 +
  4 files changed, 452 insertions(+)
xen/common/device_tree.c +0 -0
index 26d2e28..19320e1 100644

xen/common/device_tree.c void dt_print_node_names(struct dt_device_node *dt) · line 388

Quoted 4 lines
xen/common/device_tree.c +1 -0
@@ -385,6 +385,59 @@ void dt_print_node_names(struct dt_device_node *dt)
      return;
  }
  +#if defined (CONFIG_OVERLAY_DTB)
+int overlay_remove_node(struct dt_device_node *device_node)

This want to be prefixed with dt_* so it is clear which components it is touching. But I think all the DT overlay code (including sysctl) should be moved in a new file (maybe dt_overlay.c) to spreading the overlay code and growing
the size of sysctl.c.

Quoted 15 lines
xen/common/device_tree.c +15 -0
+{
+    struct dt_device_node *np;
+    struct dt_device_node *parent_node;
+    struct dt_device_node *current_node;
+
+    parent_node = device_node->parent;
+
+    current_node = parent_node;
+
+    if ( parent_node == NULL )
+    {
+        dt_dprintk("%s's parent node not found\n", device_node->name);
+        return -EFAULT;
+    }
+

xen/common/device_tree.c void dt_print_node_names(struct dt_device_node *dt) · line 414

Quoted 10 lines
xen/common/device_tree.c +10 -0
+    np = parent_node->child;
+
+    if ( np == NULL )
+    {
+        dt_dprintk("parent node %s's not found\n", parent_node->name);
+        return -EFAULT;
+    }
+
+    /* If node to be removed is only child node or first child. */
+    if ( np->name == device_node->name )

Why are you checking the equality between the fields name rather than the node itself?

If it is intended, then I think this wants to be explained because often people wants to check the names are equal (e.g str*cmp()) rather than the pointer where the names are stored.

xen/common/device_tree.c void dt_print_node_names(struct dt_device_node *dt) · line 423

Quoted 9 lines
xen/common/device_tree.c +9 -0
+    {
+        current_node->allnext = np->next;
+        return 0;
+    }
+
+    for ( np = parent_node->child; np->sibling != NULL; np = np->sibling )
+    {
+        current_node = np;
+        if ( np->sibling->name == device_node->name )

Same question.

Quoted 25 lines
xen/common/device_tree.c +17 -0
+        {
+            /* Found the node. Now we remove it. */
+            current_node->allnext = np->allnext->allnext;
+
+            if ( np->sibling->sibling )
+                current_node->sibling = np->sibling->sibling;
+            else
+                current_node->sibling = NULL;
+
+            break;
+        }
+    }
+
+    return 0;
+}
+#endif
+
  int dt_find_node_by_gpath(XEN_GUEST_HANDLE(char) u_path, uint32_t u_plen,
                            struct dt_device_node **node)
  {
xen/common/sysctl.c +0 -0
index f2dab72..fca47f5 100644

xen/common/sysctl.c line 30

Quoted 3 lines
xen/common/sysctl.c +0 -0
@@ -28,6 +28,311 @@
  #include <xen/livepatch.h>
  #include <xen/coverage.h>
  +#if defined (CONFIG_OVERLAY_DTB)

This can be shortend to #ifdef CONFIG_<...>.

xen/common/sysctl.c line 39

Quoted 8 lines
xen/common/sysctl.c +8 -0
+#include <xen/list.h>
+#include <xen/libfdt/libfdt.h>
+#include <xen/xmalloc.h>
+#include <xen/device_tree.h>
+#include <asm/domain_build.h>
+#endif
+
+#if defined (CONFIG_OVERLAY_DTB)

Same here.

Quoted 10 lines
xen/common/sysctl.c +6 -0
+static LIST_HEAD(overlay_tracker);
+static DEFINE_SPINLOCK(overlay_lock);
+
+/*
+ * overlay_node_track describes information about added nodes through dtbo.
+ * @dt_host_new: Pointer to the updated dt_host_new unflattened 'updated

fdt'.
+ * @node_fullname: Store the name of nodes.
+ * @entry: List pointer.
+ */

xen/common/sysctl.c line 53

Quoted 5 lines
xen/common/sysctl.c +5 -0
+struct overlay_track {
+    struct list_head entry;
+    struct dt_device_node *dt_host_new;
+    char **node_fullname;
+    uint8_t num_nodes;

Any reason to restrict to 256 nodes?

xen/common/sysctl.c line 57

Quoted 4 lines
xen/common/sysctl.c +4 -0
+};
+
+/* Basic sanity check for the dtbo tool stack provided to Xen. */
+static int check_overlay_fdt(void *overlay_fdt, uint32_t overlay_fdt_size)

This function doesn't modify overlay_fdt. So I think it should be const if fdt_total_size() and fdt_check_header() allows it.

Quoted 6 lines
xen/common/sysctl.c +5 -0
+{
+    if ( (fdt_totalsize(overlay_fdt) != overlay_fdt_size) ||
+          fdt_check_header(overlay_fdt) )
+    {
+        printk(XENLOG_ERR "The overlay FDT is not a valid Flat Device

Tree\n");

xen/common/sysctl.c line 71

Quoted 9 lines
xen/common/sysctl.c +9 -0
+        return -EINVAL;
+    }
+
+    return 0;
+}
+
+static int overlay_node_count(void *fdto)
+{
+    int num_overlay_nodes = 0;

The name suggests this should be an unsiged int.

xen/common/sysctl.c line 80

Quoted 9 lines
xen/common/sysctl.c +9 -0
+    int fragment;
+
+    fdt_for_each_subnode(fragment, fdto, 0)
+    {
+
+        int subnode;
+        int overlay;
+
+        overlay = fdt_subnode_offset(fdto, fragment, "__overlay__");

This may return < 0 if __overlay__ is not found. From my understanding, fdt_for_each_subnode() would end up to start from 0.

So I think you want to add a check here.

Quoted 12 lines
xen/common/sysctl.c +12 -0
+
+        fdt_for_each_subnode(subnode, fdto, overlay)
+        {
+            num_overlay_nodes++;
+        }
+    }
+
+    return num_overlay_nodes;
+}
+
+/*
+ * overlay_get_node_info will get the all node's full name with path. This
Quoted 6 lines

is
+ * useful when checking node for duplication i.e. dtbo tries to add nodes which
+ * already exists in device tree.
+ */
+static void overlay_get_node_info(void *fdto, char ***node_full_path,

You will retrieve mutiple nodes. So I think the function wants to be named 'overlay_get_nodes_info()'. Same for node_full_path.

Furthermore, you could drop one * if you return the list of paths. This will also allow you to return an error when xmalloc fails (see below)

Lastly, AFAICT, fdto can be const.

Quoted 1 line

+ int num_overlay_nodes)

This coud be unsigned int.

xen/common/sysctl.c line 100

Quoted 3 lines
xen/common/sysctl.c +3 -0
+{
+    int fragment;
+    int node_num = 0;

This could be unsigned int.

xen/common/sysctl.c

Quoted 2 lines
xen/common/sysctl.c +2 -0
+
+    *node_full_path = xmalloc_bytes(num_overlay_nodes * sizeof(char *));

Memory allocation can fail.

Quoted 11 lines
xen/common/sysctl.c +9 -0
+
+    fdt_for_each_subnode(fragment, fdto, 0)
+    {
+        int target;
+        int overlay;
+        int subnode;
+        const char *target_path;
+
+        target = fdt_overlay_get_target(device_tree_flattened, fdto,

fragment,
+ &target_path);

fdt_overlay_get_target() can fail. Also, the indentation looks strange.

Quoted 1 line

+ overlay = fdt_subnode_offset(fdto, fragment, "__overlay__");

fdt_subnode_offset can fail.

xen/common/sysctl.c line 117

Quoted 4 lines
xen/common/sysctl.c +4 -0
+
+        fdt_for_each_subnode(subnode, fdto, overlay)
+        {
+            const char *node_name = fdt_get_name(fdto, subnode, NULL);

AFAIU, fdt_get_name() can return NULL;

Quoted 1 line

+ int node_name_len = strlen(node_name);

fdt_get_name() can already provide the len for you. Can you re-use it?

xen/common/sysctl.c line 120

Quoted 2 lines
xen/common/sysctl.c +2 -0
+            int target_path_len = strlen(target_path);
+            int node_full_name_len = target_path_len + node_name_len + 2;

node_name_len, target_path_len, node_full_name_len can be unsigned. Also, I would add a comment explain what '2' refers to.

Quoted 3 lines
xen/common/sysctl.c +2 -0
+
+            (*node_full_path)[node_num] =

xmalloc_bytes(node_full_name_len);

xmalloc_bytes() can fail.

Quoted 19 lines
xen/common/sysctl.c +2 -0
+
+            memcpy((*node_full_path)[node_num], target_path,

target_path_len);

xen/common/sysctl.c +4 -0
+
+            (*node_full_path)[node_num][target_path_len] = '/';
+
+            memcpy((*node_full_path)[node_num] + target_path_len + 1,

node_name,

xen/common/sysctl.c +11 -0
+                   node_name_len);
+
+            (*node_full_path)[node_num][node_full_name_len - 1] = '\0';
+
+            node_num++;
+        }
+    }
+}
+
+/*
+ * Checks if all the devices node listed are present in dt_host and used by
Quoted 3 lines

any
+ * domain.
+ */

Why do we need to handle all the nodes together? I think the code would end up to be simpler if we were handling node by node.

xen/common/sysctl.c line 147

Quoted 6 lines
xen/common/sysctl.c +6 -0
+static int check_nodes(char **full_dt_node_path, uint32_t num_nodes)
+{
+    int rc = 0;
+    unsigned int i;
+    struct dt_device_node *overlay_node;
+    uint32_t ret = 0;

AFAICT, you are storing a domid here, so this wants to be a domid_t and possible renamed to domid.

xen/common/sysctl.c line 158

Quoted 12 lines
xen/common/sysctl.c +3 -0
+
+    for ( i = 0; i < num_nodes; i++ ) {
+        dt_dprintk("Finding node %s in the dt_host\n",

full_dt_node_path[i]);

xen/common/sysctl.c +8 -0
+
+        overlay_node = dt_find_node_by_path(full_dt_node_path[i]);
+
+        if ( overlay_node == NULL )
+        {
+            rc = -EINVAL;
+
+            printk(XENLOG_G_ERR "Device %s is not present in the tree."

You seem to use a mix of XENLOG_G_ERR and XENLOG_ERR. However, it is not entirely clear some messages are more critical than the other. Could you clarify?

Quoted 1 line

+ " Removing nodes failed\n", full_dt_node_path[i]);

Coding style: We don't split error message even if they are more than 80 lines. This helps grepping them in the log.

xen/common/sysctl.c line 168

Quoted 9 lines
xen/common/sysctl.c +9 -0
+            return rc;
+        }
+
+        ret = dt_device_used_by(overlay_node);
+
+        dt_dprintk("Checking if node %s is used by any domain\n",
+                   full_dt_node_path[i]);
+
+        if ( ret != 0 && ret != DOMID_IO )

In the commit message you wrote:

"The nodes get removed only if it is not used by any of dom0 or domus"

This implies that this should return -EINVAL for domid as well. Can you make sure the two matches? (I am not sure which one you want).

Also, what does prevent the device to not be assigned for the check?

xen/common/sysctl.c

Quoted 2 lines
xen/common/sysctl.c +2 -0
+        {
+            rc = -EINVAL;

NIT: This is a bit pointless to set rc when it is just used 2 lines below in the return. The alternative is to replace the return with a break.

Quoted 4 lines
xen/common/sysctl.c +2 -0
+
+            printk(XENLOG_G_ERR "Device %s as it is being used by domain

%d."
+ " Removing nodes failed\n", full_dt_node_path[i], ret);

Coding style: Same about the error message.

xen/common/sysctl.c line 182

Quoted 9 lines
xen/common/sysctl.c +9 -0
+            return rc;
+        }
+    }
+
+    return rc;
+}
+
+/* Remove nodes from dt_host. */
+static int remove_nodes(char **full_dt_node_path, uint32_t num_nodes)

In the commit message, you said you wanted to remove all the nodes together. But this function could possibly fail leaving some nodes present or not.

As I wrote above, I think handling node by node would be fine. However, we need to make sure that the remove operation can be called again to clean-up the rest of the nodes.

Quoted 16 lines
xen/common/sysctl.c +16 -0
+{
+    struct domain *d = hardware_domain;
+    int rc = 0;
+    struct dt_device_node *overlay_node;
+    unsigned int naddr;
+    unsigned int i, j, nirq;
+    struct dt_raw_irq rirq;
+    u64 addr, size;
+
+    for ( j = 0; j < num_nodes; j++ ) {
+        dt_dprintk("Removing node: %s\n", full_dt_node_path[j]);
+
+        overlay_node = dt_find_node_by_path(full_dt_node_path[j]);
+
+        nirq = dt_number_of_irq(overlay_node);
+

xen/common/sysctl.c line 208

Quoted 10 lines
xen/common/sysctl.c +10 -0
+        /* Remove IRQ permission */
+        for ( i = 0; i < nirq; i++ )
+        {
+            rc = dt_device_get_raw_irq(overlay_node, i, &rirq);
+            if ( rc )
+            {
+                printk(XENLOG_ERR "Unable to retrieve irq %u for %s\n",
+                       i, dt_node_full_name(overlay_node));
+                return rc;
+            }

The interrupt may not be routed to the GIC, in which case we want to skip them. So you want to check the controller is equal to dt_interrupt_controller.

The code is also quite similar to handle_device_interrupts(). So I would abstract the code to avoid duplication.

That said, I find a bit odd to have to parse the overlay again on remove given you expect the same to be passed. I think it might be better to use rangeset to keep track of the interrupts added with that specific overlay.

This will reduce the possibility that remove can go wrong.

xen/common/sysctl.c line 218

Quoted 10 lines
xen/common/sysctl.c +10 -0
+
+            rc = platform_get_irq(overlay_node, i);
+            if ( rc < 0 )
+            {
+                printk(XENLOG_ERR "Unable to get irq %u for %s\n",
+                       i, dt_node_full_name(overlay_node));
+                return rc;
+            }
+
+            rc = irq_deny_access(d, rc);

A few things:

Quoted 5 lines

1) IRQs can be assigned to another domain without the device being assigned.
So this want to be checked.
2) This is assuming the IRQ was not shared. I am not entirely sure how to
solve this one. Maybe a TODO and note in the documentation will do for now. Stefano?

I think it is fine if we don't handle shared IRQs for now. A note in the documentation and a TODO is OK.

#18 lore raw

RFC PATCH v2 9/12 xen/arm: Implement device tree node addition functionalities

In reply to Vikram Garhwal

Update sysctl XEN_SYSCTL_overlay to enable support for dtbo nodes addition using device tree overlay.

xl overlay add file.dtbo:
Each time overlay nodes are added using .dtbo, a new fdt(memcpy of device_tree_flattened) is created and updated with overlay nodes. This updated fdt is further unflattened to a dt_host_new. Next, it checks if any of the overlay nodes already exists in the dt_host. If overlay nodes doesn't exist then find the overlay nodes in dt_host_new, find the overlay node's parent in dt_host and add the nodes as child under their parent in the dt_host. The node is attached as the last node under target parent.

Finally, add IRQs, add device to IOMMUs, set permissions and map MMIO for the overlay node.

When a node is added using overlay, a new entry is allocated in the overlay_track to keep the track of memory allocation due to addition of overlay node. This is helpful for freeing the memory allocated when a device tree node is removed.

---
 xen/common/device_tree.c      |  41 +++++++++
 xen/common/sysctl.c           | 199 ++++++++++++++++++++++++++++++++++++++++++
 xen/include/xen/device_tree.h |   2 +
 3 files changed, 242 insertions(+)
xen/common/device_tree.c +41 -0
index 19320e1..5dff64c 100644
@@ -386,6 +386,47 @@ void dt_print_node_names(struct dt_device_node *dt)
 }
 
 #if defined (CONFIG_OVERLAY_DTB)
+int overlay_add_node(struct dt_device_node *device_node,
+                  const char *parent_node_path)
+{
+    struct dt_device_node *parent_node;
+    struct dt_device_node *np;
+    struct dt_device_node *next_node;
+    struct dt_device_node *new_node;
+
+    parent_node = dt_find_node_by_path(parent_node_path);
+
+    new_node = device_node;
+
+    if ( new_node == NULL )
+        return -EINVAL;
+
+    if ( parent_node == NULL )
+    {
+        dt_dprintk("Node not found. Partial dtb will not be added");
+        return -EINVAL;
+    }
+
+    /*
+     * If node is found. We can attach the device_node as a child of the
+     * parent node. Iterate to the last child node of parent.
+     */
+
+    for ( np = parent_node->child; np->sibling != NULL; np = np->sibling )
+    {
+    }
+
+    next_node = np->allnext;
+    new_node->parent = parent_node;
+    np->sibling = new_node;
+    np->allnext = new_node;
+    /* Now plug next_node at the end of device_node. */
+    new_node->sibling = next_node;
+    new_node->allnext = next_node;
+    np->sibling->sibling = NULL;
+    return 0;
+}
+
 int overlay_remove_node(struct dt_device_node *device_node)
 {
     struct dt_device_node *np;
xen/common/sysctl.c +199 -0

205 lines not on this page · raw message

xen/include/xen/device_tree.h +2 -0
index cf29cf5..eafb269 100644
@@ -554,6 +554,8 @@ int dt_find_node_by_gpath(XEN_GUEST_HANDLE(char) u_path, uint32_t u_plen,
 void dt_print_node_names(struct dt_device_node *dt);
 
 #if defined (CONFIG_OVERLAY_DTB)
+int overlay_add_node(struct dt_device_node *device_node,
+                  const char *parent_node_path);
 int overlay_remove_node(struct dt_device_node *device_node);
 #endif
 
Signature · 1 line
2.7.4
#19 lore raw

Re: [XEN][RFC PATCH v2 09/12] xen/arm: Implement device tree node addition functionalities

Quoted 5 lines · Vikram Garhwal
xen/common/sysctl.c +0 -0
@@ -331,6 +331,205 @@ out:
     spin_unlock(&overlay_lock);
     return rc;

xen/common/sysctl.c out: · line 342

Quoted 10 lines
xen/common/sysctl.c +9 -0
 }
+
+/*
+ * Adds device tree nodes under target node.
+ * We use dt_host_new to unflatten the updated device_tree_flattened. This is
+ * done to avoid the removal of device_tree generation, iomem regions mapping to
+ * hardware domain done by handle_node().
+ */
+static long handle_add_overlay_nodes(void *overlay_fdt,
+                                     uint32_t overlay_fdt_size)

You adding a static function here without any caller got me puzzled. First I thought you'd be introducing a build failure this was, but it's really patch 8 which does by introducing a call to this function without the function actually being there.

Jan

#20 lore raw

Re: [XEN][RFC PATCH v2 09/12] xen/arm: Implement device tree node addition functionalities

Hi Jan,
Thanks for reviewing the patches.
Yeah, I messed it up while breaking the code into multiple patches. For next v3 patch series, I will move the function call "handle_add_overlay_nodes()"(in patch 8/12) to 9/12 patch.

Thanks,
Vikram
________________________________
From: Jan Beulich <[email protected]>
Sent: Tuesday, November 9, 2021 3:19 AM
To: Vikram Garhwal <[email protected]>
Cc: [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>; Wei Liu <[email protected]>; [email protected] <[email protected]>
Subject: Re: [XEN][RFC PATCH v2 09/12] xen/arm: Implement device tree node addition functionalities

Quoted 5 lines · Vikram Garhwal
xen/common/sysctl.c +0 -0
@@ -331,6 +331,205 @@ out:
     spin_unlock(&overlay_lock);
     return rc;

xen/common/sysctl.c out: · line 342

Quoted 10 lines
xen/common/sysctl.c +9 -0
 }
+
+/*
+ * Adds device tree nodes under target node.
+ * We use dt_host_new to unflatten the updated device_tree_flattened. This is
+ * done to avoid the removal of device_tree generation, iomem regions mapping to
+ * hardware domain done by handle_node().
+ */
+static long handle_add_overlay_nodes(void *overlay_fdt,
+                                     uint32_t overlay_fdt_size)

You adding a static function here without any caller got me puzzled. First I thought you'd be introducing a build failure this was, but it's really patch 8 which does by introducing a call to this function without the function actually being there.

Jan

#21 lore raw

RFC PATCH v2 10/12 tools/libs/ctrl: Implement new xc interfaces for dt overlay

In reply to Vikram Garhwal

xc_dt_overlay() sends the device tree binary overlay, size of .dtbo and overlay operation type i.e. add or remove to xen.

---
 tools/include/xenctrl.h      |  5 +++++
 tools/libs/ctrl/Makefile     |  1 +
 tools/libs/ctrl/xc_overlay.c | 51 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 57 insertions(+)
 create mode 100644 tools/libs/ctrl/xc_overlay.c
tools/include/xenctrl.h +5 -0
index 07b96e6..cfd7c5c 100644
@@ -2684,6 +2684,11 @@ int xc_livepatch_replace(xc_interface *xch, char *name, uint32_t timeout, uint32
 int xc_domain_cacheflush(xc_interface *xch, uint32_t domid,
                          xen_pfn_t start_pfn, xen_pfn_t nr_pfns);
 
+#if defined (CONFIG_OVERLAY_DTB)
+int xc_dt_overlay(xc_interface *xch, void *overlay_fdt, int overlay_fdt_size,
+                  uint8_t overlayop);
+#endif
+
 /* Compat shims */
 #include "xenctrl_compat.h"
 
tools/libs/ctrl/Makefile +1 -0
index 519246b..a21a949 100644
@@ -3,6 +3,7 @@ include $(XEN_ROOT)/tools/Rules.mk
 
 SRCS-y       += xc_altp2m.c
 SRCS-y       += xc_cpupool.c
+SRCS-$(CONFIG_OVERLAY_DTB) += xc_overlay.c
 SRCS-y       += xc_domain.c
 SRCS-y       += xc_evtchn.c
 SRCS-y       += xc_gnttab.c
tools/libs/ctrl/xc_overlay.c new file +51 -0

51 lines not on this page · raw message

Signature · 1 line
2.7.4
#22 lore raw

Re: [XEN][RFC PATCH v2 10/12] tools/libs/ctrl: Implement new xc interfaces for dt overlay

Quoted 32 lines · Vikram Garhwal

xc_dt_overlay() sends the device tree binary overlay, size of .dtbo and overlay operation type i.e. add or remove to xen.

---
 tools/include/xenctrl.h      |  5 +++++
 tools/libs/ctrl/Makefile     |  1 +
 tools/libs/ctrl/xc_overlay.c | 51 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 57 insertions(+)
 create mode 100644 tools/libs/ctrl/xc_overlay.c
tools/include/xenctrl.h +5 -0
index 07b96e6..cfd7c5c 100644
@@ -2684,6 +2684,11 @@ int xc_livepatch_replace(xc_interface *xch, char *name, uint32_t timeout, uint32
 int xc_domain_cacheflush(xc_interface *xch, uint32_t domid,
                          xen_pfn_t start_pfn, xen_pfn_t nr_pfns);
 
+#if defined (CONFIG_OVERLAY_DTB)
+int xc_dt_overlay(xc_interface *xch, void *overlay_fdt, int overlay_fdt_size,
+                  uint8_t overlayop);
+#endif
+
 /* Compat shims */
 #include "xenctrl_compat.h"
 
tools/libs/ctrl/Makefile +0 -0
index 519246b..a21a949 100644

tools/libs/ctrl/Makefile include $(XEN_ROOT)/tools/Rules.mk · line 6

Quoted 4 lines
tools/libs/ctrl/Makefile +1 -0
@@ -3,6 +3,7 @@ include $(XEN_ROOT)/tools/Rules.mk
 
 SRCS-y       += xc_altp2m.c
 SRCS-y       += xc_cpupool.c
+SRCS-$(CONFIG_OVERLAY_DTB) += xc_overlay.c

So, this patch seems to introduce the use of CONFIG_OVERLAY_DTB, is there a reason why the new functionality can't be always builtin?

Thanks,

Signature · 1 line
Anthony PERARD
#23 lore raw

Re: [XEN][RFC PATCH v2 10/12] tools/libs/ctrl: Implement new xc interfaces for dt overlay

On Thu, Nov 11, 2021 at 04:54:25PM +0000, Anthony PERARD wrote:
Hi Anthony,

Quoted 34 lines · Vikram Garhwal
Quoted 33 lines

xc_dt_overlay() sends the device tree binary overlay, size of .dtbo and overlay operation type i.e. add or remove to xen.

---
 tools/include/xenctrl.h      |  5 +++++
 tools/libs/ctrl/Makefile     |  1 +
 tools/libs/ctrl/xc_overlay.c | 51 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 57 insertions(+)
 create mode 100644 tools/libs/ctrl/xc_overlay.c
tools/include/xenctrl.h +5 -0
index 07b96e6..cfd7c5c 100644
@@ -2684,6 +2684,11 @@ int xc_livepatch_replace(xc_interface *xch, char *name, uint32_t timeout, uint32
 int xc_domain_cacheflush(xc_interface *xch, uint32_t domid,
                          xen_pfn_t start_pfn, xen_pfn_t nr_pfns);
 
+#if defined (CONFIG_OVERLAY_DTB)
+int xc_dt_overlay(xc_interface *xch, void *overlay_fdt, int overlay_fdt_size,
+                  uint8_t overlayop);
+#endif
+
 /* Compat shims */
 #include "xenctrl_compat.h"
 
tools/libs/ctrl/Makefile +0 -0
index 519246b..a21a949 100644
@@ -3,6 +3,7 @@ include $(XEN_ROOT)/tools/Rules.mk
 
Quoted 7 lines

tools/libs/ctrl/Makefile include $(XEN_ROOT)/tools/Rules.mk · line 6

Quoted 3 lines
tools/libs/ctrl/Makefile +1 -0
 SRCS-y       += xc_altp2m.c
 SRCS-y       += xc_cpupool.c
+SRCS-$(CONFIG_OVERLAY_DTB) += xc_overlay.c

So, this patch seems to introduce the use of CONFIG_OVERLAY_DTB, is there a reason why the new functionality can't be always builtin?

Above, if you meant removing "CONFIG_OEVRLAY_DTB" then here is my answer:
This feature is supported by ARM based FPGA devices only so there were a few comments on v1 series to keep the code inside a config only. Now, for the tool side also I kept the CONFIG_OVERLAY_DTB to align the xen-tools with Xen.

Although, now i saw your comments on patch 10 regarding "always provide libxl_dt_overlay() but which would return ENOSYS when libxl is built without CONFIG_OVERLAY_DTB". That seems better approach here for all three xen-tool patches.

Initially, i was not sure what to do here that's why i wrote a question in the cover letter about this.

Also, do you know how to enable this config via menuconfig when building the Xen tools? I know how to enable for Xen but not sure about tools.

Thanks for reviewing this.

Regards,
Vikram Garhwal

Quoted 4 lines

Thanks,

Signature · 1 line
Anthony PERARD
#24 lore raw

Re: [XEN][RFC PATCH v2 10/12] tools/libs/ctrl: Implement new xc interfaces for dt overlay

Quoted 18 lines · Vikram Garhwal

On Thu, Nov 11, 2021 at 04:54:25PM +0000, Anthony PERARD wrote:
Hi Anthony,

Quoted 6 lines · Vikram Garhwal
Quoted 1 line

+SRCS-$(CONFIG_OVERLAY_DTB) += xc_overlay.c

So, this patch seems to introduce the use of CONFIG_OVERLAY_DTB, is there a reason why the new functionality can't be always builtin?

Above, if you meant removing "CONFIG_OEVRLAY_DTB" then here is my answer:
This feature is supported by ARM based FPGA devices only so there were a few comments on v1 series to keep the code inside a config only. Now, for the tool side also I kept the CONFIG_OVERLAY_DTB to align the xen-tools with Xen.

Although, now i saw your comments on patch 10 regarding "always provide libxl_dt_overlay() but which would return ENOSYS when libxl is built without CONFIG_OVERLAY_DTB". That seems better approach here for all three xen-tool patches.

Quoted 5 lines

Initially, i was not sure what to do here that's why i wrote a question in the cover letter about this.

Also, do you know how to enable this config via menuconfig when building the Xen tools? I know how to enable for Xen but not sure about tools.

It isn't possible to use the configuration of the hypervisor to build the tool. We use autoconf (configure.ac, ...) to configure the tools but I don't think in this case that having CONFIG_OVERLAY_DTB for the tools is the right thing to do. In the tools, we don't really have a way to select functionalities available in the different libraries, or it is mostly based on the architecture or operating system or available system libraries.

Contrary to the hypervisor side, the added code in the libraries is mostly glue which calls the hypervisor, so their isn't really a need to avoid building it. If the functionality isn't available in the hypervisor, it should return an error and the library can deal with that error.

You might want to limit the build to Arm, but I don't know if that "overlay dtb" thing is really Arm specific (even though device trees are mostly use on Arm I guess).

Signature · 1 line
Anthony PERARD
#25 lore raw

RFC PATCH v2 11/12 tools/libs/light: Implement new libxl functions for device tree overlay ops

In reply to Vikram Garhwal
---
 tools/include/libxl.h            |  5 ++++
 tools/libs/light/Makefile        |  3 ++
 tools/libs/light/libxl_overlay.c | 65 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 73 insertions(+)
 create mode 100644 tools/libs/light/libxl_overlay.c
tools/include/libxl.h +5 -0
index 2e8679d..3dcb3e7 100644
@@ -2406,6 +2406,11 @@ libxl_device_pci *libxl_device_pci_list(libxl_ctx *ctx, uint32_t domid,
                                         int *num);
 void libxl_device_pci_list_free(libxl_device_pci* list, int num);
 
+#if defined (CONFIG_OVERLAY_DTB)
+int libxl_dt_overlay(libxl_ctx *ctx, void *overlay,
+                     int overlay_size, uint8_t op);
+#endif
+
 /*
  * Turns the current process into a backend device service daemon
  * for a driver domain.
tools/libs/light/Makefile +3 -0
index 194bc5f..0fffa93 100644
@@ -117,6 +117,9 @@ SRCS-y += libxl_genid.c
 SRCS-y += _libxl_types.c
 SRCS-y += libxl_flask.c
 SRCS-y += _libxl_types_internal.c
+ifeq ($(CONFIG_OVERLAY_DTB),y)
+SRCS-y += libxl_overlay.o
+endif
 
 ifeq ($(CONFIG_LIBNL),y)
 CFLAGS_LIBXL += $(LIBNL3_CFLAGS)
tools/libs/light/libxl_overlay.c new file +65 -0

65 lines not on this page · raw message

Signature · 1 line
2.7.4
#26 lore raw

Re: [XEN][RFC PATCH v2 11/12] tools/libs/light: Implement new libxl functions for device tree overlay ops

Quoted 13 lines · Vikram Garhwal
---
 tools/include/libxl.h            |  5 ++++
 tools/libs/light/Makefile        |  3 ++
 tools/libs/light/libxl_overlay.c | 65 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 73 insertions(+)
 create mode 100644 tools/libs/light/libxl_overlay.c
tools/include/libxl.h +0 -0
index 2e8679d..3dcb3e7 100644

tools/include/libxl.h libxl_device_pci *libxl_device_pci_list(libxl_ctx *ctx, uint32_t domid, · line 2413

Quoted 8 lines
tools/include/libxl.h +5 -0
@@ -2406,6 +2406,11 @@ libxl_device_pci *libxl_device_pci_list(libxl_ctx *ctx, uint32_t domid,
                                         int *num);
 void libxl_device_pci_list_free(libxl_device_pci* list, int num);
 
+#if defined (CONFIG_OVERLAY_DTB)
+int libxl_dt_overlay(libxl_ctx *ctx, void *overlay,
+                     int overlay_size, uint8_t op);
+#endif
+

This won't work. "libxl.h" is a public header to be installed, so CONFIG_OVERLAY_DTB won't mean anything to other application making use of libxl.

Instead, can you always provide libxl_dt_overlay() but which would return ENOSYS when libxl is built without CONFIG_OVERLAY_DTB?

tools/libs/light/Makefile SRCS-y += libxl_genid.c · line 121

Quoted 13 lines
tools/include/libxl.h +0 -0
 /*
  * Turns the current process into a backend device service daemon
  * for a driver domain.
tools/libs/light/Makefile +2 -0
index 194bc5f..0fffa93 100644
@@ -117,6 +117,9 @@ SRCS-y += libxl_genid.c
 SRCS-y += _libxl_types.c
 SRCS-y += libxl_flask.c
 SRCS-y += _libxl_types_internal.c
+ifeq ($(CONFIG_OVERLAY_DTB),y)
+SRCS-y += libxl_overlay.o

FYI, the "-y" is so that you can do

    SRCS-$(CONFIG_OVERLAY_DTB) += libxl_overlay.o

;-)

Quoted 18 lines
tools/libs/light/libxl_overlay.c new file +12 -0
new file mode 100644
index 0000000..d965aee
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2021 Xilinx Inc.
+ * Author Vikram Garhwal <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; version 2.1 only. with the special
+ * exception on linking described in file LICENSE.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

tools/libs/light/libxl_overlay.c line 22

Quoted 10 lines
tools/libs/light/libxl_overlay.c new file +10 -0
+ * GNU Lesser General Public License for more details.
+ */
+
+#include "libxl_osdeps.h" /* must come before any other headers */
+#include "libxl_internal.h"
+#include <libfdt.h>
+#include <xenguest.h>
+#include <xenctrl.h>
+
+static int check_overlay_fdt(libxl__gc *gc, void *fdt, size_t size)

Does "fdt" can have a better type than a plain pointer?

There is a "size" provided, is it the size of the blob pointed by "fdt"? If so, can the size been the first thing to be check? Surely there is a minimum size for *fdt.

Quoted 14 lines
tools/libs/light/libxl_overlay.c new file +14 -0
+{
+    int r;
+
+    if (fdt_magic(fdt) != FDT_MAGIC) {
+        LOG(ERROR, "Overlay FDT is not a valid Flat Device Tree");
+        return ERROR_FAIL;
+    }
+
+    r = fdt_check_header(fdt);
+    if (r) {
+        LOG(ERROR, "Failed to check the overlay FDT (%d)", r);
+        return ERROR_FAIL;
+    }
+

tools/libs/light/libxl_overlay.c line 46

Quoted 10 lines
tools/libs/light/libxl_overlay.c new file +10 -0
+    if (fdt_totalsize(fdt) > size) {
+        LOG(ERROR, "Overlay FDT totalsize is too big");
+        return ERROR_FAIL;
+    }
+
+    return 0;
+}
+
+int libxl_dt_overlay(libxl_ctx *ctx, void *overlay_dt, int overlay_dt_size,
+                     uint8_t op)

What is "op"? What the function is supposed to do beside calling libxc?

tools/libs/light/libxl_overlay.c line 49

Quoted 3 lines
tools/libs/light/libxl_overlay.c new file +3 -0
+{
+    int rc = 0;
+    GC_INIT(ctx);

The function should have "GC_FREE;" before returning.

tools/libs/light/libxl_overlay.c line 55

Quoted 6 lines
tools/libs/light/libxl_overlay.c new file +6 -0
+
+    if (check_overlay_fdt(gc, overlay_dt, overlay_dt_size)) {
+        LOG(ERROR, "Overlay DTB check failed\n");
+        return ERROR_FAIL;
+    } else
+        LOG(DEBUG, "Overlay DTB check passed\n");

FYI, there is no need for "\n" when using LOG().

tools/libs/light/libxl_overlay.c line 58

Quoted 3 lines
tools/libs/light/libxl_overlay.c new file +3 -0
+
+    /* We don't need to do  xc_interface_open here. */
+    rc = xc_dt_overlay(ctx->xch, overlay_dt, overlay_dt_size, op);

"rc" is reserved for libxl error code, xc_* return value should be stored in "r" instead.

tools/libs/light/libxl_overlay.c line 65

Quoted 7 lines
tools/libs/light/libxl_overlay.c new file +7 -0
+
+    if (rc)
+        LOG(ERROR, "%s: Adding/Removing overlay dtb failed.\n", __func__);
+
+    return rc;
+}
+

Thanks,

Signature · 1 line
Anthony PERARD
#27 lore raw

RFC PATCH v2 12/12 tools/xl: Add new xl command overlay

In reply to Vikram Garhwal
---
 tools/xl/xl.h           |  8 ++++++++
 tools/xl/xl_cmdtable.c  |  8 ++++++++
 tools/xl/xl_vmcontrol.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 63 insertions(+)
tools/xl/xl.h +8 -0
index 7e23f30..2ee75a0 100644
@@ -98,6 +98,11 @@ struct save_file_header {
 
 #define SAVEFILE_BYTEORDER_VALUE ((uint32_t)0x01020304UL)
 
+#if defined (CONFIG_OVERLAY_DTB)
+#define XL_DT_OVERLAY_ADD                   1
+#define XL_DT_OVERLAY_REMOVE                2
+#endif
+
 void save_domain_core_begin(uint32_t domid,
                             int preserve_domid,
                             const char *override_config_file,
@@ -140,6 +145,9 @@ int main_shutdown(int argc, char **argv);
 int main_reboot(int argc, char **argv);
 int main_list(int argc, char **argv);
 int main_vm_list(int argc, char **argv);
+#if defined (CONFIG_OVERLAY_DTB)
+int main_dt_overlay(int argc, char **argv);
+#endif
 int main_create(int argc, char **argv);
 int main_config_update(int argc, char **argv);
 int main_button_press(int argc, char **argv);
tools/xl/xl_cmdtable.c +8 -0
index 661323d..dd43920 100644
@@ -20,6 +20,14 @@
 #include "xl.h"
 
 const struct cmd_spec cmd_table[] = {
+#if defined (CONFIG_OVERLAY_DTB)
+    { "overlay",
+      &main_dt_overlay, 1, 1,
+      "Add/Remove a device tree overlay",
+      "add/remove <.dtbo>"
+      "-h print this help\n"
+    },
+#endif
     { "create",
       &main_create, 1, 1,
       "Create a domain from config file <filename>",
tools/xl/xl_vmcontrol.c +47 -0

53 lines not on this page · raw message

Signature · 1 line
2.7.4