Skip to contentSkip to conversation

xen/arm: fixes around domain_vpl011_init

PATCH In review Michal Orzel 3 participants 5 messages Mar 23, 2023 – Mar 29, 2023

lore
Share & tools
lore
#0 lore raw

v2 cover xen/arm: fixes around domain_vpl011_init

This series contains fixes around domain_vpl011_init().

Michal Orzel (2):
xen/arm: domain_build: Check return code of domain_vpl011_init xen/arm: vpl011: Fix domain_vpl011_init error path

 xen/arch/arm/domain_build.c |  4 ++++
 xen/arch/arm/vpl011.c       | 47 +++++++++++++++++++++++--------------
 2 files changed, 34 insertions(+), 17 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH v2 1/2 xen/arm: domain_build: Check return code of domain_vpl011_init

We are assigning return code of domain_vpl011_init() to a variable without checking it for an error. Fix it.

Fixes 3580c8b2dfc3 ("xen/arm: if direct-map domain use native UART address and IRQ number for vPL011")

---
Changes in v2:
- none

---
 xen/arch/arm/domain_build.c | 4 ++++
 1 file changed, 4 insertions(+)
xen/arch/arm/domain_build.c +4 -0
index 9707eb7b1bb1..3195c5b6d6ac 100644
@@ -3826,7 +3826,11 @@ static int __init construct_domU(struct domain *d,
      * shall be done first.
      */
     if ( kinfo.vpl011 )
+    {
         rc = domain_vpl011_init(d, NULL);
+        if ( rc < 0 )
+            return rc;
+    }
 
     rc = prepare_dtb_domU(d, &kinfo);
     if ( rc < 0 )
Signature · 1 line
2.25.1
#2 lore raw

PATCH v2 2/2 xen/arm: vpl011: Fix domain_vpl011_init error path

In reply to Michal Orzel

When vgic_reserve_virq() fails and backend is in domain, we should also free the allocated event channel.

When backend is in Xen and call to xzalloc() returns NULL, there is no need to call xfree(). This should be done instead on an error path from vgic_reserve_virq(). Moreover, we should be calling XFREE() to prevent an extra free in domain_vpl011_deinit().

In order not to repeat the same steps twice, call domain_vpl011_deinit() on an error path whenever there is more work to do than returning rc. Since this function can now be called from different places and more than once, add proper guards, use XFREE() instead of xfree() and move vgic_free_virq() to it.

Take the opportunity to return -ENOMEM instead of -EINVAL when memory allocation fails.

Fixes 1ee1e4b0d1ff ("xen/arm: Allow vpl011 to be used by DomU")

---
Changes in v2:
- make use of domain_vpl011_deinit() to prevent code duplication
- use XFREE() instead of xfree()

---
 xen/arch/arm/vpl011.c | 47 +++++++++++++++++++++++++++----------------
 1 file changed, 30 insertions(+), 17 deletions(-)
xen/arch/arm/vpl011.c +30 -17
index 541ec962f189..2fa80bc15ac4 100644
@@ -696,8 +696,8 @@ int domain_vpl011_init(struct domain *d, struct vpl011_init_info *info)
         vpl011->backend.xen = xzalloc(struct vpl011_xen_backend);
         if ( vpl011->backend.xen == NULL )
         {
-            rc = -EINVAL;
-            goto out1;
+            rc = -ENOMEM;
+            goto out;
         }
     }
 
@@ -705,7 +705,7 @@ int domain_vpl011_init(struct domain *d, struct vpl011_init_info *info)
     if ( !rc )
     {
         rc = -EINVAL;
-        goto out2;
+        goto out1;
     }
 
     vpl011->uartfr = TXFE | RXFE;
@@ -717,15 +717,8 @@ int domain_vpl011_init(struct domain *d, struct vpl011_init_info *info)
 
     return 0;
 
-out2:
-    vgic_free_virq(d, vpl011->virq);
-
 out1:
-    if ( vpl011->backend_in_domain )
-        destroy_ring_for_helper(&vpl011->backend.dom.ring_buf,
-                                vpl011->backend.dom.ring_page);
-    else
-        xfree(vpl011->backend.xen);
+    domain_vpl011_deinit(d);
 
 out:
     return rc;
@@ -735,17 +728,37 @@ void domain_vpl011_deinit(struct domain *d)
 {
     struct vpl011 *vpl011 = &d->arch.vpl011;
 
+    if ( vpl011->virq )
+    {
+        vgic_free_virq(d, vpl011->virq);
+
+        /*
+         * Set to invalid irq (we use SPI) to prevent extra free and to avoid
+         * freeing irq that could have already been reserved by someone else.
+         */
+        vpl011->virq = 0;
+    }
+
     if ( vpl011->backend_in_domain )
     {
-        if ( !vpl011->backend.dom.ring_buf )
-            return;
+        if ( vpl011->backend.dom.ring_buf )
+            destroy_ring_for_helper(&vpl011->backend.dom.ring_buf,
+                                    vpl011->backend.dom.ring_page);
+
+        if ( vpl011->evtchn )
+        {
+            free_xen_event_channel(d, vpl011->evtchn);
 
-        free_xen_event_channel(d, vpl011->evtchn);
-        destroy_ring_for_helper(&vpl011->backend.dom.ring_buf,
-                                vpl011->backend.dom.ring_page);
+            /*
+             * Set to invalid event channel port to prevent extra free and to
+             * avoid freeing port that could have already been allocated for
+             * other purposes.
+             */
+            vpl011->evtchn = 0;
+        }
     }
     else
-        xfree(vpl011->backend.xen);
+        XFREE(vpl011->backend.xen);
 }
 
 /*
Signature · 1 line
2.25.1
Reviewed-by on v2 2/2
#3 lore raw

Re: [PATCH v2 2/2] xen/arm: vpl011: Fix domain_vpl011_init error path

Quoted 18 lines · Michal Orzel

When vgic_reserve_virq() fails and backend is in domain, we should also free the allocated event channel.

When backend is in Xen and call to xzalloc() returns NULL, there is no need to call xfree(). This should be done instead on an error path from vgic_reserve_virq(). Moreover, we should be calling XFREE() to prevent an extra free in domain_vpl011_deinit().

In order not to repeat the same steps twice, call domain_vpl011_deinit() on an error path whenever there is more work to do than returning rc. Since this function can now be called from different places and more than once, add proper guards, use XFREE() instead of xfree() and move vgic_free_virq() to it.

Quoted 5 lines

Take the opportunity to return -ENOMEM instead of -EINVAL when memory allocation fails.

Fixes 1ee1e4b0d1ff ("xen/arm: Allow vpl011 to be used by DomU")
Reviewed-by
#4 lore raw

Re: [PATCH v2 2/2] xen/arm: vpl011: Fix domain_vpl011_init error path

Hi,

Quoted 18 lines · Luca Fancellu
Quoted 18 lines · Michal Orzel

When vgic_reserve_virq() fails and backend is in domain, we should also free the allocated event channel.

When backend is in Xen and call to xzalloc() returns NULL, there is no need to call xfree(). This should be done instead on an error path from vgic_reserve_virq(). Moreover, we should be calling XFREE() to prevent an extra free in domain_vpl011_deinit().

In order not to repeat the same steps twice, call domain_vpl011_deinit() on an error path whenever there is more work to do than returning rc. Since this function can now be called from different places and more than once, add proper guards, use XFREE() instead of xfree() and move vgic_free_virq() to it.

Quoted 8 lines
Quoted 5 lines

Take the opportunity to return -ENOMEM instead of -EINVAL when memory allocation fails.

Fixes 1ee1e4b0d1ff ("xen/arm: Allow vpl011 to be used by DomU")

And committed the series.

Cheers,

Signature · 1 line
Julien Grall