Skip to contentSkip to conversation

xen/arm: fixes around domain_vpl011_init

PATCH Superseded Michal Orzel 3 participants 12 messages Mar 22, 2023 – Mar 23, 2023

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover xen/arm: fixes around domain_vpl011_init

This series contains two trivial 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       | 11 +++++++----
 2 files changed, 11 insertions(+), 4 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

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

Re: [PATCH 1/2] xen/arm: domain_build: Check return code of domain_vpl011_init

Quoted 9 lines · Michal Orzel

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")

Hi Michal,

Looks good to me,

Acked-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] xen/arm: domain_build: Check return code of domain_vpl011_init

In reply to Michal Orzel

Hi,

Quoted 5 lines · Michal Orzel

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")

Cheers,

Signature · 1 line
Julien Grall
#4 lore raw

PATCH 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(). Also, take the opportunity to return -ENOMEM instead of -EINVAL when memory allocation fails.

Fixes 1ee1e4b0d1ff ("xen/arm: Allow vpl011 to be used by DomU")
---
 xen/arch/arm/vpl011.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
xen/arch/arm/vpl011.c +7 -4
index 541ec962f189..df29a65ad365 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;
         }
     }
 
@@ -720,12 +720,15 @@ int domain_vpl011_init(struct domain *d, struct vpl011_init_info *info)
 out2:
     vgic_free_virq(d, vpl011->virq);
 
+    if ( vpl011->backend_in_domain )
+        free_xen_event_channel(d, vpl011->evtchn);
+    else
+        xfree(vpl011->backend.xen);
+
 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);
 
 out:
     return rc;
Signature · 1 line
2.25.1
Reviewed-by on v1 2/2
#5 lore raw

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

Quoted 7 lines · Michal Orzel

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

Quoted 7 lines

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(). Also, take the opportunity to return -ENOMEM instead of -EINVAL when memory allocation fails.

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

Also this one looks good to me:

#6 lore raw

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

In reply to Michal Orzel

Hi Michal,

Quoted 6 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().

Most likely this was implemented this way to avoid a double "if ( vpl011->backend_in_domain)". TBH, I am not very thrilled with this approach. Could we instead consider to use domain_pl011_deinit()? (A couple of tweak would be necessary to use it)

Quoted 25 lines

Also, take the opportunity to return -ENOMEM
instead of -EINVAL when memory allocation fails.

Fixes 1ee1e4b0d1ff ("xen/arm: Allow vpl011 to be used by DomU")
---
  xen/arch/arm/vpl011.c | 11 +++++++----
  1 file changed, 7 insertions(+), 4 deletions(-)
xen/arch/arm/vpl011.c +2 -2
index 541ec962f189..df29a65ad365 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;
          }
      }
 

xen/arch/arm/vpl011.c int domain_vpl011_init(struct domain *d, struct vpl011_init_info *info) · line 726

Quoted 7 lines
xen/arch/arm/vpl011.c +4 -0
@@ -720,12 +720,15 @@ int domain_vpl011_init(struct domain *d, struct vpl011_init_info *info)
  out2:
      vgic_free_virq(d, vpl011->virq);
 
+    if ( vpl011->backend_in_domain )
+        free_xen_event_channel(d, vpl011->evtchn);
+    else
+        xfree(vpl011->backend.xen);

There is another bug here (unrelated to your change). You want to use XFREE() to avoid an extra free in domain_pl011_deinit(). Can you look at it?

Quoted 10 lines
xen/arch/arm/vpl011.c +1 -2
+
  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);
 
  out:
      return rc;
Signature · 1 line
Julien Grall
#7 lore raw

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

Hi Julien,

Quoted 8 lines · Julien Grall

Hi Michal,

Quoted 3 lines · Michal Orzel

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

Quoted 8 lines
Quoted 3 lines

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().

Most likely this was implemented this way to avoid a double "if ( vpl011->backend_in_domain)". TBH, I am not very thrilled with this approach. Could we instead consider to use domain_pl011_deinit()? (A couple of tweak would be necessary to use it)

I think we could. More about it later.

Quoted 29 lines
Quoted 29 lines

Also, take the opportunity to return -ENOMEM
instead of -EINVAL when memory allocation fails.

Fixes 1ee1e4b0d1ff ("xen/arm: Allow vpl011 to be used by DomU")
---
  xen/arch/arm/vpl011.c | 11 +++++++----
  1 file changed, 7 insertions(+), 4 deletions(-)
xen/arch/arm/vpl011.c +2 -2
index 541ec962f189..df29a65ad365 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;
          }
      }
 
@@ -720,12 +720,15 @@ int domain_vpl011_init(struct domain *d, struct vpl011_init_info *info)
  out2:
      vgic_free_virq(d, vpl011->virq);
 
Quoted 7 lines

xen/arch/arm/vpl011.c int domain_vpl011_init(struct domain *d, struct vpl011_init_info *info) · line 726

Quoted 4 lines
xen/arch/arm/vpl011.c +4 -0
+    if ( vpl011->backend_in_domain )
+        free_xen_event_channel(d, vpl011->evtchn);
+    else
+        xfree(vpl011->backend.xen);

There is another bug here (unrelated to your change). You want to use XFREE() to avoid an extra free in domain_pl011_deinit(). Can you look at it?

Strictly speaking this is not a bug. Memory allocation can only happen if backend is in Xen. This means, that if vpl011 init fails, we will call free only once (domain_vpl011_deinit will not be called on this path i.e. we will invoke panic after construct_domU). Of course, we could switch to XFREE just for sanity.

Quoted 9 lines
Quoted 9 lines
xen/arch/arm/vpl011.c +1 -2
+
  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);
 
Quoted 3 lines

xen/arch/arm/vpl011.c int domain_vpl011_init(struct domain *d, struct vpl011_init_info *info) · line 734

Quoted 2 lines
xen/arch/arm/vpl011.c +0 -0
  out:
      return rc;

Solution to reuse domain_pl011_deinit would be as follows:

     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;

@@ -737,12 +733,15 @@ void domain_vpl011_deinit(struct domain *d)

     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);

- free_xen_event_channel(d, vpl011->evtchn);
- destroy_ring_for_helper(&vpl011->backend.dom.ring_buf,
- vpl011->backend.dom.ring_page);
+ if ( vpl011->evtchn )
+ {
+ free_xen_event_channel(d, vpl011->evtchn);
+ vpl011->evtchn = 0;
+ }

     }
     else
         xfree(vpl011->backend.xen);

However there is one problem with guarding free_xen_event_channel to be called only once. Even without allocating event channel, vpl011->evtchn is 0 by default. So doing:
if ( is_port_valid(vpl011->evtchn) )

    free_xen_event_channel(d, vpl011->evtchn);

would not help us as evtchn 0 is always there. So in my proposal I'm assuming (I think correctly) that vpl011->evtchn cannot be 0 after successful evtchn allocation because 0 is "reserved". But I might be wrong in which case I'm clueless how to do the proper check.

~Michal

#8 lore raw

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

Quoted 57 lines · Michal Orzel

Hi Julien,

Quoted 8 lines · Julien Grall

Hi Michal,

Quoted 3 lines · Michal Orzel

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

Quoted 8 lines
Quoted 3 lines

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().

Most likely this was implemented this way to avoid a double "if ( vpl011->backend_in_domain)". TBH, I am not very thrilled with this approach. Could we instead consider to use domain_pl011_deinit()? (A couple of tweak would be necessary to use it)

I think we could. More about it later.

Quoted 29 lines
Quoted 29 lines

Also, take the opportunity to return -ENOMEM
instead of -EINVAL when memory allocation fails.

Fixes 1ee1e4b0d1ff ("xen/arm: Allow vpl011 to be used by DomU")
---
   xen/arch/arm/vpl011.c | 11 +++++++----
   1 file changed, 7 insertions(+), 4 deletions(-)
xen/arch/arm/vpl011.c +2 -2
index 541ec962f189..df29a65ad365 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;
           }
       }
 
@@ -720,12 +720,15 @@ int domain_vpl011_init(struct domain *d, struct vpl011_init_info *info)
   out2:
       vgic_free_virq(d, vpl011->virq);
 
Quoted 7 lines

xen/arch/arm/vpl011.c int domain_vpl011_init(struct domain *d, struct vpl011_init_info *info) · line 726

Quoted 4 lines
xen/arch/arm/vpl011.c +4 -0
+    if ( vpl011->backend_in_domain )
+        free_xen_event_channel(d, vpl011->evtchn);
+    else
+        xfree(vpl011->backend.xen);

There is another bug here (unrelated to your change). You want to use XFREE() to avoid an extra free in domain_pl011_deinit(). Can you look at it?

Quoted 3 lines

Strictly speaking this is not a bug. Memory allocation can only happen if backend is in Xen. This means, that if vpl011 init fails, we will call free only once (domain_vpl011_deinit will not be called on this path i.e. we will invoke panic after construct_domU).

Well yes, in the current use this is not a real bug (it is only latent). But the same reasoning is also true for adding the call to free_xen_event_channel() because we would not continue to run the domain if domain_vpl011_init() is failing (even when the backend is in the domain). And even if we were going to continue this is just a channel that cannot be used. It will get free when the domain is destroyed (either explicitly in deinit() or by evtchn_destroy()).

Quoted 1 line

Of course, we could switch to XFREE just for sanity.

This is just not about sanity here. You are relying on how the caller is behaving. And we have no guarantee this is going to be the same forever. For instance, one may decide that it would fine to continue even if construct_domU() is failing (e.g. because the domain is not critical). At this point, this would become a real bug.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
xen/arch/arm/vpl011.c +1 -2
+
   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);
 
Quoted 6 lines
Quoted 3 lines

xen/arch/arm/vpl011.c int domain_vpl011_init(struct domain *d, struct vpl011_init_info *info) · line 734

Quoted 2 lines
xen/arch/arm/vpl011.c +0 -0
   out:
       return rc;

Solution to reuse domain_pl011_deinit would be as follows:

      vgic_free_virq(d, vpl011->virq);

We should move this call in domain_vpl011_deinit();

Quoted 22 lines

out1:

xen/arch/arm/vpl011.c +4 -8
-    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;
@@ -737,12 +733,15 @@ void domain_vpl011_deinit(struct domain *d)
 
      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);
 
-        free_xen_event_channel(d, vpl011->evtchn);

xen/arch/arm/vpl011.c void domain_vpl011_deinit(struct domain *d) · line 747

Quoted 10 lines
xen/arch/arm/vpl011.c +5 -2
-        destroy_ring_for_helper(&vpl011->backend.dom.ring_buf,
-                                vpl011->backend.dom.ring_page);
+        if ( vpl011->evtchn )
+        {
+            free_xen_event_channel(d, vpl011->evtchn);
+            vpl011->evtchn = 0;
+        }
      }
      else
          xfree(vpl011->backend.xen);

Now, it is more clearer that this will need to become an XFREE().

Quoted 7 lines

However there is one problem with guarding free_xen_event_channel to be called only once. Even without allocating event channel, vpl011->evtchn is 0 by default. So doing:
if ( is_port_valid(vpl011->evtchn) )

     free_xen_event_channel(d, vpl011->evtchn);

would not help us as evtchn 0 is always there. So in my proposal I'm assuming (I think correctly) that vpl011->evtchn cannot be 0 after successful evtchn allocation because 0 is "reserved".

0 was reserved because it is used as the "invalid event channel". So your check above is correct.

Quoted 3 lines

But I might be wrong in which case I'm clueless how to do the proper check.

~Michal

Signature · 1 line
Julien Grall
#9 lore raw

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

Hi Julien,

Quoted 72 lines · Julien Grall
Quoted 57 lines · Michal Orzel

Hi Julien,

Quoted 8 lines · Julien Grall

Hi Michal,

Quoted 3 lines · Michal Orzel

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

Quoted 8 lines
Quoted 3 lines

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().

Most likely this was implemented this way to avoid a double "if ( vpl011->backend_in_domain)". TBH, I am not very thrilled with this approach. Could we instead consider to use domain_pl011_deinit()? (A couple of tweak would be necessary to use it)

I think we could. More about it later.

Quoted 29 lines
Quoted 29 lines

Also, take the opportunity to return -ENOMEM
instead of -EINVAL when memory allocation fails.

Fixes 1ee1e4b0d1ff ("xen/arm: Allow vpl011 to be used by DomU")
---
   xen/arch/arm/vpl011.c | 11 +++++++----
   1 file changed, 7 insertions(+), 4 deletions(-)
xen/arch/arm/vpl011.c +2 -2
index 541ec962f189..df29a65ad365 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;
           }
       }
 
@@ -720,12 +720,15 @@ int domain_vpl011_init(struct domain *d, struct vpl011_init_info *info)
   out2:
       vgic_free_virq(d, vpl011->virq);
 
Quoted 7 lines

xen/arch/arm/vpl011.c int domain_vpl011_init(struct domain *d, struct vpl011_init_info *info) · line 726

Quoted 4 lines
xen/arch/arm/vpl011.c +4 -0
+    if ( vpl011->backend_in_domain )
+        free_xen_event_channel(d, vpl011->evtchn);
+    else
+        xfree(vpl011->backend.xen);

There is another bug here (unrelated to your change). You want to use XFREE() to avoid an extra free in domain_pl011_deinit(). Can you look at it?

Quoted 3 lines

Strictly speaking this is not a bug. Memory allocation can only happen if backend is in Xen. This means, that if vpl011 init fails, we will call free only once (domain_vpl011_deinit will not be called on this path i.e. we will invoke panic after construct_domU).

Well yes, in the current use this is not a real bug (it is only latent). But the same reasoning is also true for adding the call to free_xen_event_channel() because we would not continue to run the domain if domain_vpl011_init() is failing (even when the backend is in the domain). And even if we were going to continue this is just a channel that cannot be used. It will get free when the domain is destroyed (either explicitly in deinit() or by evtchn_destroy()).

Quoted 6 lines
Quoted 1 line

Of course, we could switch to XFREE just for sanity.

This is just not about sanity here. You are relying on how the caller is behaving. And we have no guarantee this is going to be the same forever. For instance, one may decide that it would fine to continue even if construct_domU() is failing (e.g. because the domain is not critical). At this point, this would become a real bug.

ok, makes sense.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
xen/arch/arm/vpl011.c +1 -2
+
   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);
 
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

xen/arch/arm/vpl011.c int domain_vpl011_init(struct domain *d, struct vpl011_init_info *info) · line 734

Quoted 2 lines
xen/arch/arm/vpl011.c +0 -0
   out:
       return rc;

Solution to reuse domain_pl011_deinit would be as follows:

      vgic_free_virq(d, vpl011->virq);

We should move this call in domain_vpl011_deinit();

True and I think it does not need any guard as in case of a not registered virq it will just clear the already cleared bit.

Quoted 37 lines
Quoted 23 lines

out1:

xen/arch/arm/vpl011.c +4 -8
-    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;
@@ -737,12 +733,15 @@ void domain_vpl011_deinit(struct domain *d)
 
      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);
 
-        free_xen_event_channel(d, vpl011->evtchn);

xen/arch/arm/vpl011.c void domain_vpl011_deinit(struct domain *d) · line 747

Quoted 10 lines
xen/arch/arm/vpl011.c +5 -2
-        destroy_ring_for_helper(&vpl011->backend.dom.ring_buf,
-                                vpl011->backend.dom.ring_page);
+        if ( vpl011->evtchn )
+        {
+            free_xen_event_channel(d, vpl011->evtchn);
+            vpl011->evtchn = 0;
+        }
      }
      else
          xfree(vpl011->backend.xen);

Now, it is more clearer that this will need to become an XFREE().

Quoted 1 line
Quoted 8 lines
Quoted 6 lines

However there is one problem with guarding free_xen_event_channel to be called only once. Even without allocating event channel, vpl011->evtchn is 0 by default. So doing:
if ( is_port_valid(vpl011->evtchn) )

     free_xen_event_channel(d, vpl011->evtchn);

would not help us as evtchn 0 is always there. So in my proposal I'm assuming (I think correctly) that vpl011->evtchn cannot be 0 after successful evtchn allocation because 0 is "reserved".

0 was reserved because it is used as the "invalid event channel". So your check above is correct.

ok, let me send a v2 then.

~Michal

#10 lore raw

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

Quoted 95 lines · Michal Orzel

Hi Julien,

Quoted 72 lines · Julien Grall
Quoted 57 lines · Michal Orzel

Hi Julien,

Quoted 8 lines · Julien Grall

Hi Michal,

Quoted 3 lines · Michal Orzel

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

Quoted 8 lines
Quoted 3 lines

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().

Most likely this was implemented this way to avoid a double "if ( vpl011->backend_in_domain)". TBH, I am not very thrilled with this approach. Could we instead consider to use domain_pl011_deinit()? (A couple of tweak would be necessary to use it)

I think we could. More about it later.

Quoted 29 lines
Quoted 29 lines

Also, take the opportunity to return -ENOMEM
instead of -EINVAL when memory allocation fails.

Fixes 1ee1e4b0d1ff ("xen/arm: Allow vpl011 to be used by DomU")
---
    xen/arch/arm/vpl011.c | 11 +++++++----
    1 file changed, 7 insertions(+), 4 deletions(-)
xen/arch/arm/vpl011.c +2 -2
index 541ec962f189..df29a65ad365 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;
            }
        }
 
@@ -720,12 +720,15 @@ int domain_vpl011_init(struct domain *d, struct vpl011_init_info *info)
    out2:
        vgic_free_virq(d, vpl011->virq);
 
Quoted 7 lines

xen/arch/arm/vpl011.c int domain_vpl011_init(struct domain *d, struct vpl011_init_info *info) · line 726

Quoted 4 lines
xen/arch/arm/vpl011.c +4 -0
+    if ( vpl011->backend_in_domain )
+        free_xen_event_channel(d, vpl011->evtchn);
+    else
+        xfree(vpl011->backend.xen);

There is another bug here (unrelated to your change). You want to use XFREE() to avoid an extra free in domain_pl011_deinit(). Can you look at it?

Quoted 3 lines

Strictly speaking this is not a bug. Memory allocation can only happen if backend is in Xen. This means, that if vpl011 init fails, we will call free only once (domain_vpl011_deinit will not be called on this path i.e. we will invoke panic after construct_domU).

Well yes, in the current use this is not a real bug (it is only latent). But the same reasoning is also true for adding the call to free_xen_event_channel() because we would not continue to run the domain if domain_vpl011_init() is failing (even when the backend is in the domain). And even if we were going to continue this is just a channel that cannot be used. It will get free when the domain is destroyed (either explicitly in deinit() or by evtchn_destroy()).

Quoted 6 lines
Quoted 1 line

Of course, we could switch to XFREE just for sanity.

This is just not about sanity here. You are relying on how the caller is behaving. And we have no guarantee this is going to be the same forever. For instance, one may decide that it would fine to continue even if construct_domU() is failing (e.g. because the domain is not critical). At this point, this would become a real bug.

ok, makes sense.

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 11 lines
xen/arch/arm/vpl011.c +1 -2
+
    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);
 
    out:
        return rc;
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

Solution to reuse domain_pl011_deinit would be as follows:

       vgic_free_virq(d, vpl011->virq);

We should move this call in domain_vpl011_deinit();

True and I think it does not need any guard as in case of a not registered virq it will just clear the already cleared bit.

Technically it could have been reserved by someone else afterwards. So it would be best to 0 it (we allocate a SPI so we could use 0 as invalid).

Cheers,

Signature · 1 line
Julien Grall
#11 lore raw

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

Quoted 100 lines · Julien Grall
Quoted 97 lines · Michal Orzel

Hi Julien,

Quoted 72 lines · Julien Grall
Quoted 57 lines · Michal Orzel

Hi Julien,

Quoted 8 lines · Julien Grall

Hi Michal,

Quoted 3 lines · Michal Orzel

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

Quoted 8 lines
Quoted 3 lines

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().

Most likely this was implemented this way to avoid a double "if ( vpl011->backend_in_domain)". TBH, I am not very thrilled with this approach. Could we instead consider to use domain_pl011_deinit()? (A couple of tweak would be necessary to use it)

I think we could. More about it later.

Quoted 29 lines
Quoted 29 lines

Also, take the opportunity to return -ENOMEM
instead of -EINVAL when memory allocation fails.

Fixes 1ee1e4b0d1ff ("xen/arm: Allow vpl011 to be used by DomU")
---
    xen/arch/arm/vpl011.c | 11 +++++++----
    1 file changed, 7 insertions(+), 4 deletions(-)
xen/arch/arm/vpl011.c +2 -2
index 541ec962f189..df29a65ad365 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;
            }
        }
 
@@ -720,12 +720,15 @@ int domain_vpl011_init(struct domain *d, struct vpl011_init_info *info)
    out2:
        vgic_free_virq(d, vpl011->virq);
 
Quoted 7 lines

xen/arch/arm/vpl011.c int domain_vpl011_init(struct domain *d, struct vpl011_init_info *info) · line 726

Quoted 4 lines
xen/arch/arm/vpl011.c +4 -0
+    if ( vpl011->backend_in_domain )
+        free_xen_event_channel(d, vpl011->evtchn);
+    else
+        xfree(vpl011->backend.xen);

There is another bug here (unrelated to your change). You want to use XFREE() to avoid an extra free in domain_pl011_deinit(). Can you look at it?

Quoted 3 lines

Strictly speaking this is not a bug. Memory allocation can only happen if backend is in Xen. This means, that if vpl011 init fails, we will call free only once (domain_vpl011_deinit will not be called on this path i.e. we will invoke panic after construct_domU).

Well yes, in the current use this is not a real bug (it is only latent). But the same reasoning is also true for adding the call to free_xen_event_channel() because we would not continue to run the domain if domain_vpl011_init() is failing (even when the backend is in the domain). And even if we were going to continue this is just a channel that cannot be used. It will get free when the domain is destroyed (either explicitly in deinit() or by evtchn_destroy()).

Quoted 6 lines
Quoted 1 line

Of course, we could switch to XFREE just for sanity.

This is just not about sanity here. You are relying on how the caller is behaving. And we have no guarantee this is going to be the same forever. For instance, one may decide that it would fine to continue even if construct_domU() is failing (e.g. because the domain is not critical). At this point, this would become a real bug.

ok, makes sense.

Quoted 14 lines
Quoted 14 lines
Quoted 9 lines
Quoted 9 lines
xen/arch/arm/vpl011.c +1 -2
+
    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);
 
Quoted 3 lines

xen/arch/arm/vpl011.c int domain_vpl011_init(struct domain *d, struct vpl011_init_info *info) · line 734

Quoted 2 lines
xen/arch/arm/vpl011.c +0 -0
    out:
        return rc;

Solution to reuse domain_pl011_deinit would be as follows:

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
       vgic_free_virq(d, vpl011->virq);

We should move this call in domain_vpl011_deinit();

True and I think it does not need any guard as in case of a not registered virq it will just clear the already cleared bit.

Technically it could have been reserved by someone else afterwards. So it would be best to 0 it (we allocate a SPI so we could use 0 as invalid).

Hmm, ok so the same handling as for event channel:
if ( vpl011->virq )

{
    vgic_free_virq(d, vpl011->virq);
    vpl011->virq = 0;
}

~Michal