Skip to contentSkip to conversation

Remaining patches for dynamic node programming using overlay dtbo

PATCH Superseded Henry Wang 4 participants 17 messages May 21, 2024 – May 23, 2024

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Henry Wang

author
#0 lore raw

v3 cover Remaining patches for dynamic node programming using overlay dtbo

Hi all,

This is the remaining series for the full functional "dynamic node programming using overlay dtbo" feature. The first part [1] has already been merged.

Quoting from the original series, the first part has already made Xen aware of new device tree node which means updating the dt_host with overlay node information, and in this series, the goal is to map IRQ and IOMMU during runtime, where we will do the actual IOMMU and IRQ mapping and unmapping to a running domain. Also, documentation of the "dynamic node programming using overlay dtbo" feature is added.

Patch 1 and 2 are fixes of the existing code which is noticed during my local tests, details please see the commit message.

Gitlab CI for this series can be found in [2].

[1] https://lore.kernel.org/xen-devel/[email protected]/
[2] https://gitlab.com/xen-project/people/henryw/xen/-/pipelines/1298425517

Henry Wang (6):
xen/common/dt-overlay: Fix lock issue when add/remove the device tools/xl: Correct the help information and exit code of the dt-overlay
command
xen/arm, doc: Add a DT property to specify IOMMU for Dom0less domUs tools/arm: Introduce the "nr_spis" xl config entry xen/arm: Add XEN_DOMCTL_dt_overlay DOMCTL and related operations
tools: Introduce the "xl dt-overlay {attach,detach}" commands

Vikram Garhwal (2):
xen/arm/gic: Allow routing/removing interrupt to running VMs
docs: Add device tree overlay documentation

 docs/man/xl.cfg.5.pod.in              |  14 +
 docs/misc/arm/device-tree/booting.txt |  16 +
 docs/misc/arm/overlay.txt             |  99 ++++++
 tools/golang/xenlight/helpers.gen.go  |   2 +
 tools/golang/xenlight/types.gen.go    |   1 +
 tools/include/libxl.h                 |  10 +
 tools/include/xenctrl.h               |   3 +
 tools/libs/ctrl/xc_dt_overlay.c       |  31 ++
 tools/libs/light/libxl_arm.c          |   4 +-
 tools/libs/light/libxl_dt_overlay.c   |  28 ++
 tools/libs/light/libxl_types.idl      |   1 +
 tools/xl/xl_cmdtable.c                |   4 +-
 tools/xl/xl_parse.c                   |   3 +
 tools/xl/xl_vmcontrol.c               |  48 ++-
 xen/arch/arm/dom0less-build.c         |  11 +-
 xen/arch/arm/domctl.c                 |   3 +
 xen/arch/arm/gic-vgic.c               |  15 +-
 xen/arch/arm/gic.c                    |  15 -
 xen/arch/arm/vgic/vgic.c              |  10 +-
 xen/common/dt-overlay.c               | 441 ++++++++++++++++++++------
 xen/include/public/domctl.h           |  15 +
 xen/include/public/sysctl.h           |  11 +-
 xen/include/xen/dt-overlay.h          |   7 +
 23 files changed, 644 insertions(+), 148 deletions(-)
 create mode 100644 docs/misc/arm/overlay.txt
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v3 1/8 xen/common/dt-overlay: Fix lock issue when add/remove the device

If CONFIG_DEBUG=y, below assertion will be triggered:
(XEN) Assertion 'rw_is_locked(&dt_host_lock)' failed at drivers/passthrough/device_tree.c:146 (XEN) ----[ Xen-4.19-unstable arm64 debug=y Not tainted ]----
[...]
(XEN) Xen call trace:

(XEN)    [<00000a0000257418>] iommu_remove_dt_device+0x8c/0xd4 (PC)
(XEN)    [<00000a00002573a0>] iommu_remove_dt_device+0x14/0xd4 (LR)
(XEN)    [<00000a000020797c>] dt-overlay.c#remove_node_resources+0x8c/0x90
(XEN)    [<00000a0000207f14>] dt-overlay.c#remove_nodes+0x524/0x648
(XEN)    [<00000a0000208460>] dt_overlay_sysctl+0x428/0xc68
(XEN)    [<00000a00002707f8>] arch_do_sysctl+0x1c/0x2c
(XEN)    [<00000a0000230b40>] do_sysctl+0x96c/0x9ec
(XEN)    [<00000a0000271e08>] traps.c#do_trap_hypercall+0x1e8/0x288
(XEN)    [<00000a0000273490>] do_trap_guest_sync+0x448/0x63c
(XEN)    [<00000a000025c480>] entry.o#guest_sync_slowpath+0xa8/0xd8

(XEN)
(XEN)
(XEN) ****************************************
(XEN) Panic on CPU 0:
(XEN) Assertion 'rw_is_locked(&dt_host_lock)' failed at drivers/passthrough/device_tree.c:146 (XEN) ****************************************

This is because iommu_remove_dt_device() is called without taking the dt_host_lock. dt_host_lock is meant to ensure that the DT node will not disappear behind back. So fix the issue by taking the lock as soon as getting hold of overlay_node.

Similar issue will be observed in adding the dtbo:
(XEN) Assertion 'system_state < SYS_STATE_active || rw_is_locked(&dt_host_lock)' failed at xen-source/xen/drivers/passthrough/device_tree.c:192 (XEN) ----[ Xen-4.19-unstable arm64 debug=y Not tainted ]---- [...]
(XEN) Xen call trace:

(XEN)    [<00000a00002594f4>] iommu_add_dt_device+0x7c/0x17c (PC)
(XEN)    [<00000a0000259494>] iommu_add_dt_device+0x1c/0x17c (LR)
(XEN)    [<00000a0000267db4>] handle_device+0x68/0x1e8
(XEN)    [<00000a0000208ba8>] dt_overlay_sysctl+0x9d4/0xb84
(XEN)    [<00000a000027342c>] arch_do_sysctl+0x24/0x38
(XEN)    [<00000a0000231ac8>] do_sysctl+0x9ac/0xa34
(XEN)    [<00000a0000274b70>] traps.c#do_trap_hypercall+0x230/0x2dc
(XEN)    [<00000a0000276330>] do_trap_guest_sync+0x478/0x688
(XEN)    [<00000a000025e480>] entry.o#guest_sync_slowpath+0xa8/0xd8

This is because the lock is released too early. So fix the issue by releasing the lock after handle_device().

Fixes 7e5c4a8b86f1 ("xen/arm: Implement device tree node removal functionalities")

---
v3:
- Add Julien's Reviewed-by tag.
v2:
- Take the lock as soon as getting hold of overlay_node. Also release the lock after handle_device() when adding dtbo.
v1.1:
- Move the unlock position before the check of rc.

---
 xen/common/dt-overlay.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
xen/common/dt-overlay.c +9 -4
index 1b197381f6..9cece79067 100644
@@ -429,18 +429,24 @@ static int remove_nodes(const struct overlay_track *tracker)
         if ( overlay_node == NULL )
             return -EINVAL;
 
+        write_lock(&dt_host_lock);
+
         rc = remove_descendant_nodes_resources(overlay_node);
         if ( rc )
+        {
+            write_unlock(&dt_host_lock);
             return rc;
+        }
 
         rc = remove_node_resources(overlay_node);
         if ( rc )
+        {
+            write_unlock(&dt_host_lock);
             return rc;
+        }
 
         dt_dprintk("Removing node: %s\n", overlay_node->full_name);
 
-        write_lock(&dt_host_lock);
-
         rc = dt_overlay_remove_node(overlay_node);
         if ( rc )
         {
@@ -604,8 +610,6 @@ static long add_nodes(struct overlay_track *tr, char **nodes_full_path)
             return rc;
         }
 
-        write_unlock(&dt_host_lock);
-
         prev_node->allnext = next_node;
 
         overlay_node = dt_find_node_by_path(overlay_node->full_name);
@@ -619,6 +623,7 @@ static long add_nodes(struct overlay_track *tr, char **nodes_full_path)
         rc = handle_device(hardware_domain, overlay_node, p2m_mmio_direct_c,
                            tr->iomem_ranges,
                            tr->irq_ranges);
+        write_unlock(&dt_host_lock);
         if ( rc )
         {
             printk(XENLOG_ERR "Adding IRQ and IOMMU failed\n");
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH v3 2/8 tools/xl: Correct the help information and exit code of the dt-overlay command

In reply to Henry Wang

Fix the name mismatch in the xl dt-overlay command, the command name should be "dt-overlay" instead of "dt_overlay". Add the missing "," in the cmdtable.

Fix the exit code of the dt-overlay command, use EXIT_FAILURE instead of ERROR_FAIL.

Fixes 61765a07e3d8 ("tools/xl: Add new xl command overlay for device tree overlay support")

---
v3:
- Add Jason's Reviewed-by tag.
v2:
- New patch

---
 tools/xl/xl_cmdtable.c  | 2 +-
 tools/xl/xl_vmcontrol.c | 6 +++---
 2 files changed, 4 insertions(+), 4 deletions(-)
tools/xl/xl_cmdtable.c +1 -1
index 62bdb2aeaa..1f3c6b5897 100644
@@ -635,7 +635,7 @@ const struct cmd_spec cmd_table[] = {
     { "dt-overlay",
       &main_dt_overlay, 0, 1,
       "Add/Remove a device tree overlay",
-      "add/remove <.dtbo>"
+      "add/remove <.dtbo>",
       "-h print this help\n"
     },
 #endif
tools/xl/xl_vmcontrol.c +3 -3
index 98f6bd2e76..02575d5d36 100644
@@ -1278,7 +1278,7 @@ int main_dt_overlay(int argc, char **argv)
     const int overlay_remove_op = 2;
 
     if (argc < 2) {
-        help("dt_overlay");
+        help("dt-overlay");
         return EXIT_FAILURE;
     }
 
@@ -1302,11 +1302,11 @@ int main_dt_overlay(int argc, char **argv)
             fprintf(stderr, "failed to read the overlay device tree file %s\n",
                     overlay_config_file);
             free(overlay_dtb);
-            return ERROR_FAIL;
+            return EXIT_FAILURE;
         }
     } else {
         fprintf(stderr, "overlay dtbo file not provided\n");
-        return ERROR_FAIL;
+        return EXIT_FAILURE;
     }
 
     rc = libxl_dt_overlay(ctx, overlay_dtb, overlay_dtb_size, op);
Signature · 1 line
2.34.1
author
#3 lore raw

PATCH v3 3/8 xen/arm, doc: Add a DT property to specify IOMMU for Dom0less domUs

In reply to Henry Wang

There are some use cases in which the dom0less domUs need to have the XEN_DOMCTL_CDF_iommu set at the domain construction time. For example, the dynamic dtbo feature allows the domain to be assigned a device that is behind the IOMMU at runtime. For these use cases, we need to have a way to specify the domain will need the IOMMU mapping at domain construction time.

Introduce a "passthrough" DT property for Dom0less DomUs following the same entry as the xl.cfg. Currently only provide two options, i.e. "enable" and "disable". Set the XEN_DOMCTL_CDF_iommu at domain construction time based on the property.

---
v3:
- Use a separate variable to cache the condition from the "passthrough" flag separately to improve readability.
- Update the doc to explain the default condition more clearly. v2:
- New patch to replace the original patch in v1:
"[PATCH 03/15] xen/arm: Always enable IOMMU"

---
 docs/misc/arm/device-tree/booting.txt | 16 ++++++++++++++++
 xen/arch/arm/dom0less-build.c         | 11 +++++++++--
 2 files changed, 25 insertions(+), 2 deletions(-)
docs/misc/arm/device-tree/booting.txt +16 -0
index bbd955e9c2..f1fd069c87 100644
@@ -260,6 +260,22 @@ with the following properties:
     value specified by Xen command line parameter gnttab_max_maptrack_frames
     (or its default value if unspecified, i.e. 1024) is used.
 
+- passthrough
+
+    A string property specifying whether IOMMU mappings are enabled for the
+    domain and hence whether it will be enabled for passthrough hardware.
+    Possible property values are:
+
+    - "enabled"
+    IOMMU mappings are enabled for the domain. Note that this option is the
+    default if the user provides the device partial passthrough device tree
+    for the domain.
+
+    - "disabled"
+    IOMMU mappings are disabled for the domain and so hardware may not be
+    passed through. This option is the default if this property is missing
+    and the user does not provide the device partial device tree for the domain.
+
 Under the "xen,domain" compatible node, one or more sub-nodes are present
 for the DomU kernel and ramdisk.
 
xen/arch/arm/dom0less-build.c +9 -2
index 74f053c242..5830a7051d 100644
@@ -848,6 +848,8 @@ static int __init construct_domU(struct domain *d,
 void __init create_domUs(void)
 {
     struct dt_device_node *node;
+    const char *dom0less_iommu;
+    bool iommu = false;
     const struct dt_device_node *cpupool_node,
                                 *chosen = dt_find_node_by_path("/chosen");
 
@@ -895,8 +897,13 @@ void __init create_domUs(void)
             panic("Missing property 'cpus' for domain %s\n",
                   dt_node_name(node));
 
-        if ( dt_find_compatible_node(node, NULL, "multiboot,device-tree") &&
-             iommu_enabled )
+        if ( !dt_property_read_string(node, "passthrough", &dom0less_iommu) &&
+             !strcmp(dom0less_iommu, "enabled") )
+            iommu = true;
+
+        if ( iommu_enabled &&
+             (iommu || dt_find_compatible_node(node, NULL,
+                                               "multiboot,device-tree")) )
             d_cfg.flags |= XEN_DOMCTL_CDF_iommu;
 
         if ( !dt_property_read_u32(node, "nr_spis", &d_cfg.arch.nr_spis) )
Signature · 1 line
2.34.1
author
#4 lore raw

PATCH v3 4/8 tools/arm: Introduce the "nr_spis" xl config entry

In reply to Henry Wang

Currently, the number of SPIs allocated to the domain is only configurable for Dom0less DomUs. Xen domains are supposed to be platform agnostics and therefore the numbers of SPIs for libxl guests should not be based on the hardware.

Introduce a new xl config entry for Arm to provide a method for user to decide the number of SPIs. This would help to avoid bumping the config->arch.nr_spis in libxl everytime there is a new platform with increased SPI numbers.

Update the doc and the golang bindings accordingly.

---
v3:
- Reword documentation to avoid ambiguity.
v2:
- New patch to replace the original patch in v1:
"[PATCH 05/15] tools/libs/light: Increase nr_spi to 160"

---
 docs/man/xl.cfg.5.pod.in             | 14 ++++++++++++++
 tools/golang/xenlight/helpers.gen.go |  2 ++
 tools/golang/xenlight/types.gen.go   |  1 +
 tools/libs/light/libxl_arm.c         |  4 ++--
 tools/libs/light/libxl_types.idl     |  1 +
 tools/xl/xl_parse.c                  |  3 +++
 6 files changed, 23 insertions(+), 2 deletions(-)
docs/man/xl.cfg.5.pod.in +14 -0
index 8f2b375ce9..416d582844 100644
@@ -3072,6 +3072,20 @@ raised.
 
 =back
 
+=over 4
+
+=item B<nr_spis="NR_SPIS">
+
+An optional 32-bit integer parameter specifying the number of SPIs (Shared
+Peripheral Interrupts) to allocate for the domain. If the value specified by
+the `nr_spis` parameter is smaller than the number of SPIs calculated by the
+toolstack based on the devices allocated for the domain, or the `nr_spis`
+parameter is not specified, the value calculated by the toolstack will be used
+for the domain. Otherwise, the value specified by the `nr_spis` parameter will
+be used.
+
+=back
+
 =head3 x86
 
 =over 4
tools/golang/xenlight/helpers.gen.go +2 -0
index b9cb5b33c7..fe5110474d 100644
@@ -1154,6 +1154,7 @@ return fmt.Errorf("invalid union key '%v'", x.Type)}
 x.ArchArm.GicVersion = GicVersion(xc.arch_arm.gic_version)
 x.ArchArm.Vuart = VuartType(xc.arch_arm.vuart)
 x.ArchArm.SveVl = SveType(xc.arch_arm.sve_vl)
+x.ArchArm.NrSpis = uint32(xc.arch_arm.nr_spis)
 if err := x.ArchX86.MsrRelaxed.fromC(&xc.arch_x86.msr_relaxed);err != nil {
 return fmt.Errorf("converting field ArchX86.MsrRelaxed: %v", err)
 }
@@ -1670,6 +1671,7 @@ return fmt.Errorf("invalid union key '%v'", x.Type)}
 xc.arch_arm.gic_version = C.libxl_gic_version(x.ArchArm.GicVersion)
 xc.arch_arm.vuart = C.libxl_vuart_type(x.ArchArm.Vuart)
 xc.arch_arm.sve_vl = C.libxl_sve_type(x.ArchArm.SveVl)
+xc.arch_arm.nr_spis = C.uint32_t(x.ArchArm.NrSpis)
 if err := x.ArchX86.MsrRelaxed.toC(&xc.arch_x86.msr_relaxed); err != nil {
 return fmt.Errorf("converting field ArchX86.MsrRelaxed: %v", err)
 }
tools/golang/xenlight/types.gen.go +1 -0
index 5b293755d7..c9e45b306f 100644
@@ -597,6 +597,7 @@ ArchArm struct {
 GicVersion GicVersion
 Vuart VuartType
 SveVl SveType
+NrSpis uint32
 }
 ArchX86 struct {
 MsrRelaxed Defbool
tools/libs/light/libxl_arm.c +2 -2
index 1cb89fa584..a4029e3ac8 100644
@@ -181,8 +181,8 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
 
     LOG(DEBUG, "Configure the domain");
 
-    config->arch.nr_spis = nr_spis;
-    LOG(DEBUG, " - Allocate %u SPIs", nr_spis);
+    config->arch.nr_spis = max(nr_spis, d_config->b_info.arch_arm.nr_spis);
+    LOG(DEBUG, " - Allocate %u SPIs", config->arch.nr_spis);
 
     switch (d_config->b_info.arch_arm.gic_version) {
     case LIBXL_GIC_VERSION_DEFAULT:
tools/libs/light/libxl_types.idl +1 -0
index 79e9c656cc..4e65e6fda5 100644
@@ -722,6 +722,7 @@ libxl_domain_build_info = Struct("domain_build_info",[
     ("arch_arm", Struct(None, [("gic_version", libxl_gic_version),
                                ("vuart", libxl_vuart_type),
                                ("sve_vl", libxl_sve_type),
+                               ("nr_spis", uint32),
                               ])),
     ("arch_x86", Struct(None, [("msr_relaxed", libxl_defbool),
                               ])),
tools/xl/xl_parse.c +3 -0
index c504ab3711..e3a4800f6e 100644
@@ -2935,6 +2935,9 @@ skip_usbdev:
         }
     }
 
+    if (!xlu_cfg_get_long (config, "nr_spis", &l, 0))
+        b_info->arch_arm.nr_spis = l;
+
     parse_vkb_list(config, d_config);
 
     d_config->virtios = NULL;
Signature · 1 line
2.34.1
Reviewed-by on v3 4/8
#5 lore raw

Re: [PATCH v3 4/8] tools/arm: Introduce the "nr_spis" xl config entry

Quoted 5 lines · Henry Wang

Currently, the number of SPIs allocated to the domain is only configurable for Dom0less DomUs. Xen domains are supposed to be platform agnostics and therefore the numbers of SPIs for libxl guests should not be based on the hardware.

Quoted 8 lines

Introduce a new xl config entry for Arm to provide a method for user to decide the number of SPIs. This would help to avoid bumping the config->arch.nr_spis in libxl everytime there is a new platform with increased SPI numbers.

Update the doc and the golang bindings accordingly.

Thanks,
Jason

author
#6 lore raw

PATCH v3 5/8 xen/arm/gic: Allow routing/removing interrupt to running VMs

In reply to Henry Wang

From: Vikram Garhwal <[email protected]>

Currently, routing/removing physical interrupts are only allowed at the domain creation/destroy time. For use cases such as dynamic device tree overlay adding/removing, the routing/removing of physical IRQ to running domains should be allowed.

Removing the above-mentioned domain creation/dying check. Since this will introduce interrupt state unsync issues for cases when the interrupt is active or pending in the guest, therefore for these cases we simply reject the operation. Do it for both new and old vGIC implementations.

---
v3:
- Update in-code comments.
- Correct the if conditions.
- Add taking/releasing the vgic lock of the vcpu. v2:
- Reject the case where the IRQ is active or pending in guest.

---
 xen/arch/arm/gic-vgic.c  | 15 ++++++++++++---
 xen/arch/arm/gic.c       | 15 ---------------
 xen/arch/arm/vgic/vgic.c | 10 +++++++---
 3 files changed, 19 insertions(+), 21 deletions(-)
xen/arch/arm/gic-vgic.c +12 -3
index 56490dbc43..956c11ba13 100644
@@ -439,24 +439,33 @@ int vgic_connect_hw_irq(struct domain *d, struct vcpu *v, unsigned int virq,
 
     /* We are taking to rank lock to prevent parallel connections. */
     vgic_lock_rank(v_target, rank, flags);
+    spin_lock(&v_target->arch.vgic.lock);
 
     if ( connect )
     {
-        /* The VIRQ should not be already enabled by the guest */
+        /*
+         * The VIRQ should not be already enabled by the guest nor
+         * active/pending in the guest.
+         */
         if ( !p->desc &&
-             !test_bit(GIC_IRQ_GUEST_ENABLED, &p->status) )
+             !test_bit(GIC_IRQ_GUEST_ENABLED, &p->status) &&
+             !test_bit(GIC_IRQ_GUEST_VISIBLE, &p->status) &&
+             !test_bit(GIC_IRQ_GUEST_ACTIVE, &p->status) )
             p->desc = desc;
         else
             ret = -EBUSY;
     }
     else
     {
-        if ( desc && p->desc != desc )
+        if ( (desc && p->desc != desc) ||
+             test_bit(GIC_IRQ_GUEST_VISIBLE, &p->status) ||
+             test_bit(GIC_IRQ_GUEST_ACTIVE, &p->status) )
             ret = -EINVAL;
         else
             p->desc = NULL;
     }
 
+    spin_unlock(&v_target->arch.vgic.lock);
     vgic_unlock_rank(v_target, rank, flags);
 
     return ret;
xen/arch/arm/gic.c +0 -15
index 44c40e86de..3ebd89940a 100644
@@ -135,14 +135,6 @@ int gic_route_irq_to_guest(struct domain *d, unsigned int virq,
     ASSERT(virq < vgic_num_irqs(d));
     ASSERT(!is_lpi(virq));
 
-    /*
-     * When routing an IRQ to guest, the virtual state is not synced
-     * back to the physical IRQ. To prevent get unsync, restrict the
-     * routing to when the Domain is been created.
-     */
-    if ( d->creation_finished )
-        return -EBUSY;
-
     ret = vgic_connect_hw_irq(d, NULL, virq, desc, true);
     if ( ret )
         return ret;
@@ -167,13 +159,6 @@ int gic_remove_irq_from_guest(struct domain *d, unsigned int virq,
     ASSERT(test_bit(_IRQ_GUEST, &desc->status));
     ASSERT(!is_lpi(virq));
 
-    /*
-     * Removing an interrupt while the domain is running may have
-     * undesirable effect on the vGIC emulation.
-     */
-    if ( !d->is_dying )
-        return -EBUSY;
-
     desc->handler->shutdown(desc);
 
     /* EOI the IRQ if it has not been done by the guest */
xen/arch/arm/vgic/vgic.c +7 -3
index b9463a5f27..78554c11e2 100644
@@ -876,8 +876,11 @@ int vgic_connect_hw_irq(struct domain *d, struct vcpu *vcpu,
 
     if ( connect )                      /* assign a mapped IRQ */
     {
-        /* The VIRQ should not be already enabled by the guest */
-        if ( !irq->hw && !irq->enabled )
+        /*
+         * The VIRQ should not be already enabled by the guest nor
+         * active/pending in the guest
+         */
+        if ( !irq->hw && !irq->enabled && !irq->active && !irq->pending_latch )
         {
             irq->hw = true;
             irq->hwintid = desc->irq;
@@ -887,7 +890,8 @@ int vgic_connect_hw_irq(struct domain *d, struct vcpu *vcpu,
     }
     else                                /* remove a mapped IRQ */
     {
-        if ( desc && irq->hwintid != desc->irq )
+        if ( (desc && irq->hwintid != desc->irq) ||
+             irq->active || irq->pending_latch )
         {
             ret = -EINVAL;
         }
Signature · 1 line
2.34.1
#7 lore raw

Re: [PATCH v3 5/8] xen/arm/gic: Allow routing/removing interrupt to running VMs

Hi,

Quoted 34 lines · Henry Wang

From: Vikram Garhwal <[email protected]>

Currently, routing/removing physical interrupts are only allowed at the domain creation/destroy time. For use cases such as dynamic device tree overlay adding/removing, the routing/removing of physical IRQ to running domains should be allowed.

Removing the above-mentioned domain creation/dying check. Since this will introduce interrupt state unsync issues for cases when the interrupt is active or pending in the guest, therefore for these cases we simply reject the operation. Do it for both new and old vGIC implementations.

---
v3:
- Update in-code comments.
- Correct the if conditions.
- Add taking/releasing the vgic lock of the vcpu. v2:
- Reject the case where the IRQ is active or pending in guest.

---
  xen/arch/arm/gic-vgic.c  | 15 ++++++++++++---
  xen/arch/arm/gic.c       | 15 ---------------
  xen/arch/arm/vgic/vgic.c | 10 +++++++---
  3 files changed, 19 insertions(+), 21 deletions(-)
xen/arch/arm/gic-vgic.c +0 -0
index 56490dbc43..956c11ba13 100644

xen/arch/arm/gic-vgic.c int vgic_connect_hw_irq(struct domain *d, struct vcpu *v, unsigned int virq, · line 442

Quoted 4 lines
xen/arch/arm/gic-vgic.c +1 -0
@@ -439,24 +439,33 @@ int vgic_connect_hw_irq(struct domain *d, struct vcpu *v, unsigned int virq,
 
      /* We are taking to rank lock to prevent parallel connections. */
      vgic_lock_rank(v_target, rank, flags);
+    spin_lock(&v_target->arch.vgic.lock);

I know this is what Stefano suggested, but v_target would point to the current affinity whereas the interrupt may be pending/active on the "previous" vCPU. So it is a little unclear whether v_target is the correct lock. Do you have more pointer to show this is correct?

Also, while looking at the locking, I noticed that we are not doing anything with GIC_IRQ_GUEST_MIGRATING. In gic_update_one_lr(), we seem to assume that if the flag is set, then p->desc cannot be NULL.

Can we reach vgic_connect_hw_irq() with the flag set?

What about the other flags? Is this going to be a concern if we don't reset them?

Cheers,

Signature · 1 line
Julien Grall
author
#8 lore raw

Re: [PATCH v3 5/8] xen/arm/gic: Allow routing/removing interrupt to running VMs

Hi Julien,

Quoted 14 lines · Julien Grall

Hi,

xen/arch/arm/gic-vgic.c int vgic_connect_hw_irq(struct domain *d, · line 442

Quoted 9 lines · Henry Wang
xen/arch/arm/gic-vgic.c +1 -0
index 56490dbc43..956c11ba13 100644
@@ -439,24 +439,33 @@ int vgic_connect_hw_irq(struct domain *d, 
 struct vcpu *v, unsigned int virq,
         /* We are taking to rank lock to prevent parallel connections. */
       vgic_lock_rank(v_target, rank, flags);
+    spin_lock(&v_target->arch.vgic.lock);

I know this is what Stefano suggested, but v_target would point to the current affinity whereas the interrupt may be pending/active on the "previous" vCPU. So it is a little unclear whether v_target is the correct lock. Do you have more pointer to show this is correct?

No I think you are correct, we have discussed this in the initial version of this patch. Sorry.

I followed the way from that discussion to note down the vcpu ID and retrieve here, below is the diff, would this make sense to you?

xen/arch/arm/gic-vgic.c +0 -0
index 956c11ba13..134ed4e107 100644
@@ -439,7 +439,7 @@ int vgic_connect_hw_irq(struct domain *d, struct vcpu *v, unsigned int virq,
 

     /* We are taking to rank lock to prevent parallel connections. */ vgic_lock_rank(v_target, rank, flags);
-    spin_lock(&v_target->arch.vgic.lock);
+ spin_lock(&d->vcpu[p->spi_vcpu_id]->arch.vgic.lock);

     if ( connect )
     {
@@ -465,7 +465,7 @@ int vgic_connect_hw_irq(struct domain *d, struct vcpu *v, unsigned int virq, p->desc = NULL;
     }

-    spin_unlock(&v_target->arch.vgic.lock);
+ spin_unlock(&d->vcpu[p->spi_vcpu_id]->arch.vgic.lock); vgic_unlock_rank(v_target, rank, flags);

     return ret;

xen/arch/arm/include/asm/vgic.h +0 -0
index 79b73a0dbb..f4075d3e75 100644
@@ -85,6 +85,7 @@ struct pending_irq

     uint8_t priority;
     uint8_t lpi_priority;       /* Caches the priority if this is an LPI. */ uint8_t lpi_vcpu_id;        /* The VCPU for an LPI. */
+    uint8_t spi_vcpu_id;        /* The VCPU for an SPI. */
     /* inflight is used to append instances of pending_irq to
      * vgic.inflight_irqs */
     struct list_head inflight;

xen/arch/arm/vgic.c +0 -0
index c04fc4f83f..e852479f13 100644
@@ -632,6 +632,7 @@ void vgic_inject_irq(struct domain *d, struct vcpu *v, unsigned int virq,

     }
     list_add_tail(&n->inflight, &v->arch.vgic.inflight_irqs); out:
+    n->spi_vcpu_id = v->vcpu_id; spin_unlock_irqrestore(&v->arch.vgic.lock, flags);

     /* we have a new higher priority irq, inject it into the guest */ vcpu_kick(v);

Quoted 3 lines

Also, while looking at the locking, I noticed that we are not doing anything with GIC_IRQ_GUEST_MIGRATING. In gic_update_one_lr(), we seem to assume that if the flag is set, then p->desc cannot be NULL.

Can we reach vgic_connect_hw_irq() with the flag set?

I think even from the perspective of making the code extra safe, we should also check GIC_IRQ_GUEST_MIGRATING as the LR is allocated for this case. I will also add the check of GIC_IRQ_GUEST_MIGRATING here.

Quoted 1 line

What about the other flags? Is this going to be a concern if we don't reset them?

I don't think so, if I am not mistaken, no LR will be allocated with other flags set.

Kind regards,
Henry

Quoted 3 lines

Cheers,

#9 lore raw

Re: [PATCH v3 5/8] xen/arm/gic: Allow routing/removing interrupt to running VMs

Quoted 83 lines · Henry Wang

Hi Julien,

Quoted 17 lines · Julien Grall

Hi,

xen/arch/arm/gic-vgic.c int vgic_connect_hw_irq(struct domain *d, struct · line 442

Quoted 9 lines · Henry Wang
xen/arch/arm/gic-vgic.c +1 -0
index 56490dbc43..956c11ba13 100644
@@ -439,24 +439,33 @@ int vgic_connect_hw_irq(struct domain *d, struct
 vcpu *v, unsigned int virq,
         /* We are taking to rank lock to prevent parallel connections. */
       vgic_lock_rank(v_target, rank, flags);
+    spin_lock(&v_target->arch.vgic.lock);

I know this is what Stefano suggested, but v_target would point to the current affinity whereas the interrupt may be pending/active on the "previous" vCPU. So it is a little unclear whether v_target is the correct lock. Do you have more pointer to show this is correct?

No I think you are correct, we have discussed this in the initial version of this patch. Sorry.

I followed the way from that discussion to note down the vcpu ID and retrieve here, below is the diff, would this make sense to you?

xen/arch/arm/gic-vgic.c +1 -1
index 956c11ba13..134ed4e107 100644
@@ -439,7 +439,7 @@ int vgic_connect_hw_irq(struct domain *d, struct vcpu *v,
 unsigned int virq,
 
      /* We are taking to rank lock to prevent parallel connections. */
      vgic_lock_rank(v_target, rank, flags);
-    spin_lock(&v_target->arch.vgic.lock);
+ spin_lock(&d->vcpu[p->spi_vcpu_id]->arch.vgic.lock);
 
      if ( connect )

     {

xen/arch/arm/gic-vgic.c +1 -1
@@ -465,7 +465,7 @@ int vgic_connect_hw_irq(struct domain *d, struct vcpu *v,
 unsigned int virq,
              p->desc = NULL;
      }
 
-    spin_unlock(&v_target->arch.vgic.lock);
+ spin_unlock(&d->vcpu[p->spi_vcpu_id]->arch.vgic.lock);
      vgic_unlock_rank(v_target, rank, flags);
 

     return ret;

xen/arch/arm/include/asm/vgic.h +0 -0
index 79b73a0dbb..f4075d3e75 100644
@@ -85,6 +85,7 @@ struct pending_irq
      uint8_t priority;

     uint8_t lpi_priority;       /* Caches the priority if this is an LPI. */ uint8_t lpi_vcpu_id;        /* The VCPU for an LPI. */
+    uint8_t spi_vcpu_id;        /* The VCPU for an SPI. */
     /* inflight is used to append instances of pending_irq to
      * vgic.inflight_irqs */
     struct list_head inflight;

xen/arch/arm/vgic.c +0 -0
index c04fc4f83f..e852479f13 100644
@@ -632,6 +632,7 @@ void vgic_inject_irq(struct domain *d, struct vcpu *v,
 unsigned int virq,
      }
      list_add_tail(&n->inflight, &v->arch.vgic.inflight_irqs);

 out:
+    n->spi_vcpu_id = v->vcpu_id; spin_unlock_irqrestore(&v->arch.vgic.lock, flags);

     /* we have a new higher priority irq, inject it into the guest */ vcpu_kick(v);

Quoted 4 lines

Also, while looking at the locking, I noticed that we are not doing anything with GIC_IRQ_GUEST_MIGRATING. In gic_update_one_lr(), we seem to assume that if the flag is set, then p->desc cannot be NULL.

Quoted 5 lines
Quoted 1 line

Can we reach vgic_connect_hw_irq() with the flag set?

I think even from the perspective of making the code extra safe, we should also check GIC_IRQ_GUEST_MIGRATING as the LR is allocated for this case. I will also add the check of GIC_IRQ_GUEST_MIGRATING here.

Yes. I think it might be easier to check for GIC_IRQ_GUEST_MIGRATING early and return error immediately in that case. Otherwise, we can continue and take spin_lock(&v_target->arch.vgic.lock) because no migration is in progress

Quoted 8 lines
Quoted 2 lines

What about the other flags? Is this going to be a concern if we don't reset them?

I don't think so, if I am not mistaken, no LR will be allocated with other flags set.

Kind regards,
Henry

author
#10 lore raw

Re: [PATCH v3 5/8] xen/arm/gic: Allow routing/removing interrupt to running VMs

Hi Stefano,

Quoted 82 lines · Stefano Stabellini
Quoted 81 lines · Henry Wang

Hi Julien,

Quoted 16 lines · Julien Grall

Hi,

xen/arch/arm/gic-vgic.c int vgic_connect_hw_irq(struct domain *d, struct · line 442

Quoted 9 lines · Henry Wang
xen/arch/arm/gic-vgic.c +1 -0
index 56490dbc43..956c11ba13 100644
@@ -439,24 +439,33 @@ int vgic_connect_hw_irq(struct domain *d, struct
 vcpu *v, unsigned int virq,
         /* We are taking to rank lock to prevent parallel connections. */
       vgic_lock_rank(v_target, rank, flags);
+    spin_lock(&v_target->arch.vgic.lock);

I know this is what Stefano suggested, but v_target would point to the current affinity whereas the interrupt may be pending/active on the "previous" vCPU. So it is a little unclear whether v_target is the correct lock. Do you have more pointer to show this is correct?

No I think you are correct, we have discussed this in the initial version of this patch. Sorry.

I followed the way from that discussion to note down the vcpu ID and retrieve here, below is the diff, would this make sense to you?

xen/arch/arm/gic-vgic.c +1 -1
index 956c11ba13..134ed4e107 100644
@@ -439,7 +439,7 @@ int vgic_connect_hw_irq(struct domain *d, struct vcpu *v,
 unsigned int virq,
 
      /* We are taking to rank lock to prevent parallel connections. */
      vgic_lock_rank(v_target, rank, flags);
-    spin_lock(&v_target->arch.vgic.lock);
+ spin_lock(&d->vcpu[p->spi_vcpu_id]->arch.vgic.lock);
 
      if ( connect )

     {

xen/arch/arm/gic-vgic.c +1 -1
@@ -465,7 +465,7 @@ int vgic_connect_hw_irq(struct domain *d, struct vcpu *v,
 unsigned int virq,
              p->desc = NULL;
      }
 
-    spin_unlock(&v_target->arch.vgic.lock);
+ spin_unlock(&d->vcpu[p->spi_vcpu_id]->arch.vgic.lock);
      vgic_unlock_rank(v_target, rank, flags);
 

     return ret;

xen/arch/arm/include/asm/vgic.h +1 -0
index 79b73a0dbb..f4075d3e75 100644
@@ -85,6 +85,7 @@ struct pending_irq
      uint8_t priority;
      uint8_t lpi_priority;       /* Caches the priority if this is an LPI. */
      uint8_t lpi_vcpu_id;        /* The VCPU for an LPI. */
+    uint8_t spi_vcpu_id;        /* The VCPU for an SPI. */
      /* inflight is used to append instances of pending_irq to
       * vgic.inflight_irqs */
      struct list_head inflight;
xen/arch/arm/vgic.c +1 -0
index c04fc4f83f..e852479f13 100644
@@ -632,6 +632,7 @@ void vgic_inject_irq(struct domain *d, struct vcpu *v,
 unsigned int virq,
      }
      list_add_tail(&n->inflight, &v->arch.vgic.inflight_irqs);
  out:
+    n->spi_vcpu_id = v->vcpu_id;
      spin_unlock_irqrestore(&v->arch.vgic.lock, flags);
 

     /* we have a new higher priority irq, inject it into the guest */ vcpu_kick(v);

Quoted 4 lines

Also, while looking at the locking, I noticed that we are not doing anything with GIC_IRQ_GUEST_MIGRATING. In gic_update_one_lr(), we seem to assume that if the flag is set, then p->desc cannot be NULL.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Can we reach vgic_connect_hw_irq() with the flag set?

I think even from the perspective of making the code extra safe, we should also check GIC_IRQ_GUEST_MIGRATING as the LR is allocated for this case. I will also add the check of GIC_IRQ_GUEST_MIGRATING here.

Yes. I think it might be easier to check for GIC_IRQ_GUEST_MIGRATING early and return error immediately in that case. Otherwise, we can continue and take spin_lock(&v_target->arch.vgic.lock) because no migration is in progress

Ok, this makes sense to me, I will add

    if( test_bit(GIC_IRQ_GUEST_MIGRATING, &p->status) ) {
        vgic_unlock_rank(v_target, rank, flags); return -EBUSY;
    }

right after taking the vgic rank lock.

Kind regards,
Henry

#11 lore raw

Re: [PATCH v3 5/8] xen/arm/gic: Allow routing/removing interrupt to running VMs

Hi Henry,

Quoted 97 lines · Henry Wang
Quoted 85 lines · Stefano Stabellini
Quoted 84 lines · Henry Wang

Hi Julien,

Quoted 16 lines · Julien Grall

Hi,

xen/arch/arm/gic-vgic.c int vgic_connect_hw_irq(struct domain *d, struct · line 442

Quoted 9 lines · Henry Wang
xen/arch/arm/gic-vgic.c +1 -0
index 56490dbc43..956c11ba13 100644
@@ -439,24 +439,33 @@ int vgic_connect_hw_irq(struct domain *d, struct
 vcpu *v, unsigned int virq,
          /* We are taking to rank lock to prevent parallel connections. */
        vgic_lock_rank(v_target, rank, flags);
+    spin_lock(&v_target->arch.vgic.lock);

I know this is what Stefano suggested, but v_target would point to the current affinity whereas the interrupt may be pending/active on the "previous" vCPU. So it is a little unclear whether v_target is the correct lock. Do you have more pointer to show this is correct?

No I think you are correct, we have discussed this in the initial version of this patch. Sorry.

I followed the way from that discussion to note down the vcpu ID and retrieve here, below is the diff, would this make sense to you?

xen/arch/arm/gic-vgic.c +1 -1
index 956c11ba13..134ed4e107 100644
@@ -439,7 +439,7 @@ int vgic_connect_hw_irq(struct domain *d, struct 
 vcpu *v,
 unsigned int virq,
 
       /* We are taking to rank lock to prevent parallel connections. */
       vgic_lock_rank(v_target, rank, flags);
-    spin_lock(&v_target->arch.vgic.lock);
+ spin_lock(&d->vcpu[p->spi_vcpu_id]->arch.vgic.lock);
 

      if ( connect )
      {

xen/arch/arm/gic-vgic.c +1 -1
@@ -465,7 +465,7 @@ int vgic_connect_hw_irq(struct domain *d, struct 
 vcpu *v,
 unsigned int virq,
               p->desc = NULL;
       }
 
-    spin_unlock(&v_target->arch.vgic.lock);
+ spin_unlock(&d->vcpu[p->spi_vcpu_id]->arch.vgic.lock);
       vgic_unlock_rank(v_target, rank, flags);

      return ret;

xen/arch/arm/include/asm/vgic.h +0 -0
index 79b73a0dbb..f4075d3e75 100644
@@ -85,6 +85,7 @@ struct pending_irq
       uint8_t priority;

      uint8_t lpi_priority;       /* Caches the priority if this is an LPI. */ uint8_t lpi_vcpu_id;        /* The VCPU for an LPI. */
+    uint8_t spi_vcpu_id;        /* The VCPU for an SPI. */
      /* inflight is used to append instances of pending_irq to
       * vgic.inflight_irqs */
      struct list_head inflight;

xen/arch/arm/vgic.c +0 -0
index c04fc4f83f..e852479f13 100644
@@ -632,6 +632,7 @@ void vgic_inject_irq(struct domain *d, struct 
 vcpu *v,
 unsigned int virq,
       }
       list_add_tail(&n->inflight, &v->arch.vgic.inflight_irqs);

  out:
+    n->spi_vcpu_id = v->vcpu_id; spin_unlock_irqrestore(&v->arch.vgic.lock, flags);

      /* we have a new higher priority irq, inject it into the guest */ vcpu_kick(v);

Quoted 4 lines

Also, while looking at the locking, I noticed that we are not doing anything with GIC_IRQ_GUEST_MIGRATING. In gic_update_one_lr(), we seem to assume that if the flag is set, then p->desc cannot be NULL.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Can we reach vgic_connect_hw_irq() with the flag set?

I think even from the perspective of making the code extra safe, we should also check GIC_IRQ_GUEST_MIGRATING as the LR is allocated for this case. I will also add the check of GIC_IRQ_GUEST_MIGRATING here.

Yes. I think it might be easier to check for GIC_IRQ_GUEST_MIGRATING early and return error immediately in that case. Otherwise, we can continue and take spin_lock(&v_target->arch.vgic.lock) because no migration is in progress

Ok, this makes sense to me, I will add

Quoted 7 lines

    if( test_bit(GIC_IRQ_GUEST_MIGRATING, &p->status) ) {
        vgic_unlock_rank(v_target, rank, flags); return -EBUSY;
    }

right after taking the vgic rank lock.

I think that would be ok. I have to admit, I am still a bit wary about allowing to remove interrupts when the domain is running.

I am less concerned about the add part. Do you need the remove part now? If not, I would suggest to split in two so we can get the most of this series merged for 4.19 and continue to deal with the remove path in the background.

I will answer here to the other reply:

Quoted 1 line

I don't think so, if I am not mistaken, no LR will be allocated with other flags set.

I wasn't necessarily thinking about the LR allocation. I was more thinking whether there are any flags that could still be set.

IOW, will the vIRQ like new once vgic_connect_hw_irq() is succesful?

Also, while looking at the flags, I noticed we clear _IRQ_INPROGRESS before vgic_connect_hw_irq(). Shouldn't we only clear *after*?

This brings to another question. You don't special case a dying domain. If the domain is crashing, wouldn't this mean it wouldn't be possible to destroy it?

Cheers,

Signature · 1 line
Julien Grall
author
#12 lore raw

Re: [PATCH v3 5/8] xen/arm/gic: Allow routing/removing interrupt to running VMs

Hi Julien, Stefano,

Quoted 25 lines · Julien Grall

Hi Henry,

Quoted 22 lines · Henry Wang
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Also, while looking at the locking, I noticed that we are not doing anything with GIC_IRQ_GUEST_MIGRATING. In gic_update_one_lr(), we seem to assume that if the flag is set, then p->desc cannot be NULL.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Can we reach vgic_connect_hw_irq() with the flag set?

I think even from the perspective of making the code extra safe, we should also check GIC_IRQ_GUEST_MIGRATING as the LR is allocated for this case. I will also add the check of GIC_IRQ_GUEST_MIGRATING here.

Yes. I think it might be easier to check for GIC_IRQ_GUEST_MIGRATING early and return error immediately in that case. Otherwise, we can continue and take spin_lock(&v_target->arch.vgic.lock) because no migration is in progress

Ok, this makes sense to me, I will add

     if( test_bit(GIC_IRQ_GUEST_MIGRATING, &p->status) ) {
         vgic_unlock_rank(v_target, rank, flags); return -EBUSY;
     }

right after taking the vgic rank lock.

Summary of our yesterday's discussion on Matrix:
For the split of patch mentioned in...

Quoted 3 lines

I think that would be ok. I have to admit, I am still a bit wary about allowing to remove interrupts when the domain is running.

I am less concerned about the add part. Do you need the remove part now? If not, I would suggest to split in two so we can get the most of this series merged for 4.19 and continue to deal with the remove path in the background.

...here, I will do that in the next version.

Quoted 10 lines

I will answer here to the other reply:

Quoted 1 line

I don't think so, if I am not mistaken, no LR will be allocated with

other flags set.

I wasn't necessarily thinking about the LR allocation. I was more thinking whether there are any flags that could still be set.

IOW, will the vIRQ like new once vgic_connect_hw_irq() is succesful?

Also, while looking at the flags, I noticed we clear _IRQ_INPROGRESS before vgic_connect_hw_irq(). Shouldn't we only clear *after*?

This is a good catch, with the logic of vgic_connect_hw_irq() extended to reject the invalid cases, it is indeed safer to clear the _IRQ_INPROGRESS  after the successful vgic_connect_hw_irq(). I will move it after.

Quoted 1 line

This brings to another question. You don't special case a dying domain. If the domain is crashing, wouldn't this mean it wouldn't be possible to destroy it?

Another good point, thanks. I will try to make a special case of the dying domain.

Kind regards,
Henry

Quoted 3 lines

Cheers,

author
#13 lore raw

PATCH v3 6/8 xen/arm: Add XEN_DOMCTL_dt_overlay DOMCTL and related operations

In reply to Henry Wang

In order to support the dynamic dtbo device assignment to a running VM, the add/remove of the DT overlay and the attach/detach of the device from the DT overlay should happen separately. Therefore, repurpose the existing XEN_SYSCTL_dt_overlay to only add the DT overlay to Xen device tree, instead of assigning the device to the hardware domain at the same time. Add the XEN_DOMCTL_dt_overlay with operations XEN_DOMCTL_DT_OVERLAY_{ATTACH,DETACH} to do/undo the device assignment to the domain.

The hypervisor firstly checks the DT overlay passed from the toolstack is valid. Then the device nodes are retrieved from the overlay tracker based on the DT overlay. The attach/detach of the device is implemented by map/unmap the IRQ and IOMMU resources. Note that with these changes, the device de-registration from the IOMMU driver should only happen at the time when the DT overlay is removed from the Xen device tree.

---
v3:
- Style fixes for arch-selection #ifdefs.
- Do not include public/domctl.h, only add a forward declaration of struct xen_domctl_dt_overlay.
- Extract the overlay track entry finding logic to a function, drop the unused variables.
- Use op code 1&2 for XEN_DOMCTL_DT_OVERLAY_{ATTACH,DETACH}. v2:
- New patch.

---
 xen/arch/arm/domctl.c        |   3 +
 xen/common/dt-overlay.c      | 438 +++++++++++++++++++++++++++--------
 xen/include/public/domctl.h  |  15 ++
 xen/include/public/sysctl.h  |  11 +-
 xen/include/xen/dt-overlay.h |   7 +
 5 files changed, 367 insertions(+), 107 deletions(-)
xen/arch/arm/domctl.c +3 -0
index ad56efb0f5..12a12ee781 100644
@@ -5,6 +5,7 @@
  * Copyright (c) 2012, Citrix Systems
  */
 
+#include <xen/dt-overlay.h>
 #include <xen/errno.h>
 #include <xen/guest_access.h>
 #include <xen/hypercall.h>
@@ -176,6 +177,8 @@ long arch_do_domctl(struct xen_domctl *domctl, struct domain *d,
 
         return rc;
     }
+    case XEN_DOMCTL_dt_overlay:
+        return dt_overlay_domctl(d, &domctl->u.dt_overlay);
     default:
         return subarch_do_domctl(domctl, d, u_domctl);
     }
xen/common/dt-overlay.c +338 -100

550 lines not on this page · raw message

xen/include/public/domctl.h +15 -0
index a33f9ec32b..81efb39014 100644
@@ -1190,6 +1190,17 @@ struct xen_domctl_vmtrace_op {
 typedef struct xen_domctl_vmtrace_op xen_domctl_vmtrace_op_t;
 DEFINE_XEN_GUEST_HANDLE(xen_domctl_vmtrace_op_t);
 
+#if defined(__arm__) || defined(__aarch64__)
+struct xen_domctl_dt_overlay {
+    XEN_GUEST_HANDLE_64(const_void) overlay_fdt;  /* IN: overlay fdt. */
+    uint32_t overlay_fdt_size;              /* IN: Overlay dtb size. */
+#define XEN_DOMCTL_DT_OVERLAY_ATTACH                1
+#define XEN_DOMCTL_DT_OVERLAY_DETACH                2
+    uint8_t overlay_op;                     /* IN: Attach or detach. */
+    uint8_t pad[3];                         /* IN: Must be zero. */
+};
+#endif
+
 struct xen_domctl {
     uint32_t cmd;
 #define XEN_DOMCTL_createdomain                   1
@@ -1277,6 +1288,7 @@ struct xen_domctl {
 #define XEN_DOMCTL_vmtrace_op                    84
 #define XEN_DOMCTL_get_paging_mempool_size       85
 #define XEN_DOMCTL_set_paging_mempool_size       86
+#define XEN_DOMCTL_dt_overlay                    87
 #define XEN_DOMCTL_gdbsx_guestmemio            1000
 #define XEN_DOMCTL_gdbsx_pausevcpu             1001
 #define XEN_DOMCTL_gdbsx_unpausevcpu           1002
@@ -1339,6 +1351,9 @@ struct xen_domctl {
         struct xen_domctl_vuart_op          vuart_op;
         struct xen_domctl_vmtrace_op        vmtrace_op;
         struct xen_domctl_paging_mempool    paging_mempool;
+#if defined(__arm__) || defined(__aarch64__)
+        struct xen_domctl_dt_overlay        dt_overlay;
+#endif
         uint8_t                             pad[128];
     } u;
 };
xen/include/public/sysctl.h +4 -7
index febaa4b16a..3a6e7d48f0 100644
@@ -1184,14 +1184,11 @@ typedef struct xen_sysctl_cpu_policy xen_sysctl_cpu_policy_t;
 DEFINE_XEN_GUEST_HANDLE(xen_sysctl_cpu_policy_t);
 #endif
 
-#if defined(__arm__) || defined (__aarch64__)
+#if defined(__arm__) || defined(__aarch64__)
 /*
  * XEN_SYSCTL_dt_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.
+ * Performs addition/removal of device tree nodes under parent node using dtbo
+ * from dt_host.
  */
 struct xen_sysctl_dt_overlay {
     XEN_GUEST_HANDLE_64(const_void) overlay_fdt;  /* IN: overlay fdt. */
@@ -1265,7 +1262,7 @@ struct xen_sysctl {
         struct xen_sysctl_cpu_policy        cpu_policy;
 #endif
 
-#if defined(__arm__) || defined (__aarch64__)
+#if defined(__arm__) || defined(__aarch64__)
         struct xen_sysctl_dt_overlay        dt_overlay;
 #endif
         uint8_t                             pad[128];
xen/include/xen/dt-overlay.h +7 -0
index c0567741ee..7f0f3741b5 100644
@@ -39,15 +39,22 @@ struct overlay_track {
 };
 
 struct xen_sysctl_dt_overlay;
+struct xen_domctl_dt_overlay;
 
 #ifdef CONFIG_OVERLAY_DTB
 long dt_overlay_sysctl(struct xen_sysctl_dt_overlay *op);
+long dt_overlay_domctl(struct domain *d, struct xen_domctl_dt_overlay *op);
 #else
 #include <xen/errno.h>
 static inline long dt_overlay_sysctl(struct xen_sysctl_dt_overlay *op)
 {
     return -EOPNOTSUPP;
 }
+static inline long dt_overlay_domctl(struct domain *d,
+                                     struct xen_domctl_dt_overlay *op)
+{
+    return -EOPNOTSUPP;
+}
 #endif
 
 #endif /* __XEN_DT_OVERLAY_H__ */
Signature · 1 line
2.34.1
author
#14 lore raw

PATCH v3 7/8 tools: Introduce the "xl dt-overlay {attach,detach}" commands

In reply to Henry Wang

With the XEN_DOMCTL_dt_overlay DOMCTL added, users should be able to attach/detach devices from the provided DT overlay to domains. Support this by introducing a new set of "xl dt-overlay" commands and related documentation, i.e. "xl dt-overlay {attach,detach}". Slightly rework the command option parsing logic.

---
v3:
- Introduce new API libxl_dt_overlay_domain() and co., instead of reusing existing API libxl_dt_overlay().
- Add in-code comments for the LIBXL_DT_OVERLAY_* macros.
- Use find_domain() to avoid getting domain_id from strtol(). v2:
- New patch.

---
 tools/include/libxl.h               | 10 +++++++
 tools/include/xenctrl.h             |  3 +++
 tools/libs/ctrl/xc_dt_overlay.c     | 31 +++++++++++++++++++++
 tools/libs/light/libxl_dt_overlay.c | 28 +++++++++++++++++++
 tools/xl/xl_cmdtable.c              |  4 +--
 tools/xl/xl_vmcontrol.c             | 42 ++++++++++++++++++++---------
 6 files changed, 104 insertions(+), 14 deletions(-)
tools/include/libxl.h +10 -0
index 62cb07dea6..6cc6d6bf6a 100644
@@ -2549,8 +2549,18 @@ libxl_device_pci *libxl_device_pci_list(libxl_ctx *ctx, uint32_t domid,
 void libxl_device_pci_list_free(libxl_device_pci* list, int num);
 
 #if defined(__arm__) || defined(__aarch64__)
+/* Values should keep consistent with the op from XEN_SYSCTL_dt_overlay */
+#define LIBXL_DT_OVERLAY_ADD                   1
+#define LIBXL_DT_OVERLAY_REMOVE                2
 int libxl_dt_overlay(libxl_ctx *ctx, void *overlay,
                      uint32_t overlay_size, uint8_t overlay_op);
+
+/* Values should keep consistent with the op from XEN_DOMCTL_dt_overlay */
+#define LIBXL_DT_OVERLAY_DOMAIN_ATTACH         1
+#define LIBXL_DT_OVERLAY_DOMAIN_DETACH         2
+int libxl_dt_overlay_domain(libxl_ctx *ctx, uint32_t domain_id,
+                            void *overlay_dt, uint32_t overlay_dt_size,
+                            uint8_t overlay_op);
 #endif
 
 /*
tools/include/xenctrl.h +3 -0
index 4996855944..9ceca0cffc 100644
@@ -2657,6 +2657,9 @@ int xc_domain_cacheflush(xc_interface *xch, uint32_t domid,
 #if defined(__arm__) || defined(__aarch64__)
 int xc_dt_overlay(xc_interface *xch, void *overlay_fdt,
                   uint32_t overlay_fdt_size, uint8_t overlay_op);
+int xc_dt_overlay_domain(xc_interface *xch, void *overlay_fdt,
+                         uint32_t overlay_fdt_size, uint8_t overlay_op,
+                         uint32_t domain_id);
 #endif
 
 /* Compat shims */
tools/libs/ctrl/xc_dt_overlay.c +31 -0
index c2224c4d15..ea1da522d1 100644
@@ -48,3 +48,34 @@ err:
 
     return err;
 }
+
+int xc_dt_overlay_domain(xc_interface *xch, void *overlay_fdt,
+                         uint32_t overlay_fdt_size, uint8_t overlay_op,
+                         uint32_t domain_id)
+{
+    int err;
+    struct xen_domctl domctl = {
+        .cmd = XEN_DOMCTL_dt_overlay,
+        .domain = domain_id,
+        .u.dt_overlay = {
+            .overlay_op = overlay_op,
+            .overlay_fdt_size = overlay_fdt_size,
+        }
+    };
+
+    DECLARE_HYPERCALL_BOUNCE(overlay_fdt, overlay_fdt_size,
+                             XC_HYPERCALL_BUFFER_BOUNCE_IN);
+
+    if ( (err = xc_hypercall_bounce_pre(xch, overlay_fdt)) )
+        goto err;
+
+    set_xen_guest_handle(domctl.u.dt_overlay.overlay_fdt, overlay_fdt);
+
+    if ( (err = do_domctl(xch, &domctl)) != 0 )
+        PERROR("%s failed", __func__);
+
+err:
+    xc_hypercall_bounce_post(xch, overlay_fdt);
+
+    return err;
+}
tools/libs/light/libxl_dt_overlay.c +28 -0
index a6c709a6dc..00503b76bd 100644
@@ -69,3 +69,31 @@ out:
     return rc;
 }
 
+int libxl_dt_overlay_domain(libxl_ctx *ctx, uint32_t domain_id,
+                            void *overlay_dt, uint32_t overlay_dt_size,
+                            uint8_t overlay_op)
+{
+    int rc;
+    int r;
+    GC_INIT(ctx);
+
+    if (check_overlay_fdt(gc, overlay_dt, overlay_dt_size)) {
+        LOG(ERROR, "Overlay DTB check failed");
+        rc = ERROR_FAIL;
+        goto out;
+    } else {
+        LOG(DEBUG, "Overlay DTB check passed");
+        rc = 0;
+    }
+
+    r = xc_dt_overlay_domain(ctx->xch, overlay_dt, overlay_dt_size, overlay_op,
+                             domain_id);
+    if (r) {
+        LOG(ERROR, "%s: Attaching/Detaching overlay dtb failed.", __func__);
+        rc = ERROR_FAIL;
+    }
+
+out:
+    GC_FREE;
+    return rc;
+}
tools/xl/xl_cmdtable.c +2 -2
index 1f3c6b5897..37770b20e3 100644
@@ -634,8 +634,8 @@ const struct cmd_spec cmd_table[] = {
 #ifdef LIBXL_HAVE_DT_OVERLAY
     { "dt-overlay",
       &main_dt_overlay, 0, 1,
-      "Add/Remove a device tree overlay",
-      "add/remove <.dtbo>",
+      "Add/Remove a device tree overlay to Xen device tree, attach/detach the device to a domain",
+      "<operation=add|remove> <.dtbo> OR <operation=attach|detach> <.dtbo> <Domain>",
       "-h print this help\n"
     },
 #endif
tools/xl/xl_vmcontrol.c +30 -12

69 lines not on this page · raw message

Signature · 1 line
2.34.1
Reviewed-by on v3 7/8
#15 lore raw

Re: [PATCH v3 7/8] tools: Introduce the "xl dt-overlay {attach,detach}" commands

Quoted 7 lines · Henry Wang

With the XEN_DOMCTL_dt_overlay DOMCTL added, users should be able to attach/detach devices from the provided DT overlay to domains. Support this by introducing a new set of "xl dt-overlay" commands and related documentation, i.e. "xl dt-overlay {attach,detach}". Slightly rework the command option parsing logic.

Thanks,
Jason

author
#16 lore raw

PATCH v3 8/8 docs: Add device tree overlay documentation

In reply to Henry Wang

From: Vikram Garhwal <[email protected]>

---
v3:
- No change.
v2:
- Update the content based on the changes in this version.

---
 docs/misc/arm/overlay.txt | 99 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 99 insertions(+)
 create mode 100644 docs/misc/arm/overlay.txt
docs/misc/arm/overlay.txt new file +99 -0
new file mode 100644
index 0000000000..811a6de369
@@ -0,0 +1,99 @@
+# Device Tree Overlays support in Xen
+
+Xen now supports dynamic device assignment to running domains,
+i.e. adding/removing nodes (using .dtbo) to/from Xen device tree, and
+attaching/detaching them to/from a running domain with given $domid.
+
+Dynamic node assignment works in two steps:
+
+## Add/Remove device tree overlay to/from Xen device tree
+
+1. Xen tools check the dtbo given and parse all other user provided arguments
+2. Xen tools pass the dtbo to Xen hypervisor via hypercall.
+3. Xen hypervisor applies/removes the dtbo to/from Xen device tree.
+
+## Attach/Detach device from the DT overlay to/from domain
+
+1. Xen tools check the dtbo given and parse all other user provided arguments
+2. Xen tools pass the dtbo to Xen hypervisor via hypercall.
+3. Xen hypervisor attach/detach the device to/from the user-provided $domid by
+   mapping/unmapping node resources in the DT overlay.
+
+# Examples
+
+Here are a few examples on how to use it.
+
+## Dom0 device add
+
+For assigning a device tree overlay to Dom0, user should firstly properly
+prepare the DT overlay. More information about device tree overlays can be
+found in [1]. Then, in Dom0, enter the following:
+
+    (dom0) xl dt-overlay add overlay.dtbo
+
+This will allocate the devices mentioned in overlay.dtbo to Xen device tree.
+
+To assign the newly added device from the dtbo to Dom0:
+
+    (dom0) xl dt-overlay attach overlay.dtbo 0
+
+Next, if the user wants to add the same device tree overlay to dom0
+Linux, execute the following:
+
+    (dom0) mkdir -p /sys/kernel/config/device-tree/overlays/new_overlay
+    (dom0) cat overlay.dtbo > /sys/kernel/config/device-tree/overlays/new_overlay/dtbo
+
+Finally if needed, the relevant Linux kernel drive can be loaded using:
+
+    (dom0) modprobe module_name.ko
+
+## Dom0 device remove
+
+For removing the device from Dom0, first detach the device from Dom0:
+
+    (dom0) xl dt-overlay detach overlay.dtbo 0
+
+NOTE: The user is expected to unload any Linux kernel modules which
+might be accessing the devices in overlay.dtbo before detach the device.
+Detaching devices without unloading the modules might result in a crash.
+
+Then remove the overlay from Xen device tree:
+
+    (dom0) xl dt-overlay remove overlay.dtbo
+
+## DomU device add/remove
+
+All the nodes in dtbo will be assigned to a domain; the user will need
+to prepare the dtb for the domU. For example, the `interrupt-parent` property
+of the DomU overlay should be changed to the Xen hardcoded value `0xfde8`.
+Below assumes the properly written DomU dtbo is `overlay_domu.dtbo`.
+
+User will need to create the DomU with below properties properly configured
+in the xl config file:
+- `iomem`
+- `passthrough` (if IOMMU is needed)
+
+User will also need to modprobe the relevant drivers.
+
+Example for domU device add:
+
+    (dom0) xl dt-overlay add overlay.dtbo            # If not executed before
+    (dom0) xl dt-overlay attach overlay.dtbo $domid
+    (dom0) xl console $domid                         # To access $domid console
+
+Next, if the user needs to modify/prepare the overlay.dtbo suitable for
+the domU:
+
+    (domU) mkdir -p /sys/kernel/config/device-tree/overlays/new_overlay
+    (domU) cat overlay_domu.dtbo > /sys/kernel/config/device-tree/overlays/new_overlay/dtbo
+
+Finally, if needed, the relevant Linux kernel drive can be probed:
+
+    (domU) modprobe module_name.ko
+
+Example for domU overlay remove:
+
+    (dom0) xl dt-overlay detach overlay.dtbo $domid
+    (dom0) xl dt-overlay remove overlay.dtbo
+
+[1] https://www.kernel.org/doc/Documentation/devicetree/overlay-notes.txt
Signature · 1 line
2.34.1