Skip to contentSkip to conversation

xen/arm64: domctl: Avoid unreachable code in subarch_do_domctl()

PATCH In review Julien Grall 4 participants 5 messages Oct 23, 2023 – Oct 24, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/arm64: domctl: Avoid unreachable code in subarch_do_domctl()

From: Julien Grall <[email protected]>

The 'break' the XEN_DOMCTL_set_address_size is unreachable and tools like Eclair will report as a violation of Misra Rule 2.1.

Furthermore, the nested switch is not very easy to read. So move out the nested switch in a separate function to improve the readability and hopefully address the MISRA violation.

---

Only compiled tested. Waiting for the CI to confirm there is no regression.

---
 xen/arch/arm/arm64/domctl.c | 34 +++++++++++++++++++---------------
 1 file changed, 19 insertions(+), 15 deletions(-)
xen/arch/arm/arm64/domctl.c +19 -15
index 14fc622e9956..8720d126c97d 100644
@@ -33,27 +33,31 @@ static long switch_mode(struct domain *d, enum domain_type type)
     return 0;
 }
 
+static long set_address_size(struct domain *d, uint32_t address_size)
+{
+    switch ( address_size )
+    {
+    case 32:
+        if ( !cpu_has_el1_32 )
+            return -EINVAL;
+        /* SVE is not supported for 32 bit domain */
+        if ( is_sve_domain(d) )
+            return -EINVAL;
+        return switch_mode(d, DOMAIN_32BIT);
+    case 64:
+        return switch_mode(d, DOMAIN_64BIT);
+    default:
+        return -EINVAL;
+    }
+}
+
 long subarch_do_domctl(struct xen_domctl *domctl, struct domain *d,
                        XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl)
 {
     switch ( domctl->cmd )
     {
     case XEN_DOMCTL_set_address_size:
-        switch ( domctl->u.address_size.size )
-        {
-        case 32:
-            if ( !cpu_has_el1_32 )
-                return -EINVAL;
-            /* SVE is not supported for 32 bit domain */
-            if ( is_sve_domain(d) )
-                return -EINVAL;
-            return switch_mode(d, DOMAIN_32BIT);
-        case 64:
-            return switch_mode(d, DOMAIN_64BIT);
-        default:
-            return -EINVAL;
-        }
-        break;
+        return set_address_size(d, domctl->u.address_size.size);
 
     default:
         return -ENOSYS;
Signature · 1 line
2.40.1
Reviewed-by Tested-by on v1 1/1
#1 lore raw

Hi Julien,

(+Stefano and Bertrand)

Quoted 7 lines · Julien Grall

From: Julien Grall <[email protected]>

The 'break' the XEN_DOMCTL_set_address_size is unreachable and tools like Eclair will report as a violation of Misra Rule 2.1.

Quoted 6 lines

Furthermore, the nested switch is not very easy to read. So move out the nested switch in a separate function to improve the readability and hopefully address the MISRA violation.

Quoted 3 lines

---
Only compiled tested. Waiting for the CI to confirm there is no regression.

I also tested this patch on top of today’s staging in Arm’s internal CI, and this patch looks good.

Kind regards,
Henry

#2 lore raw
In reply to Julien Grall

Hi,

I forgot to CC the maintainers.

Quoted 71 lines · Julien Grall

From: Julien Grall <[email protected]>

The 'break' the XEN_DOMCTL_set_address_size is unreachable and tools like Eclair will report as a violation of Misra Rule 2.1.

Furthermore, the nested switch is not very easy to read. So move out the nested switch in a separate function to improve the readability and hopefully address the MISRA violation.

---

Only compiled tested. Waiting for the CI to confirm there is no regression.

---
  xen/arch/arm/arm64/domctl.c | 34 +++++++++++++++++++---------------
  1 file changed, 19 insertions(+), 15 deletions(-)
xen/arch/arm/arm64/domctl.c +19 -15
index 14fc622e9956..8720d126c97d 100644
@@ -33,27 +33,31 @@ static long switch_mode(struct domain *d, enum domain_type type)
      return 0;
  }
 
+static long set_address_size(struct domain *d, uint32_t address_size)
+{
+    switch ( address_size )
+    {
+    case 32:
+        if ( !cpu_has_el1_32 )
+            return -EINVAL;
+        /* SVE is not supported for 32 bit domain */
+        if ( is_sve_domain(d) )
+            return -EINVAL;
+        return switch_mode(d, DOMAIN_32BIT);
+    case 64:
+        return switch_mode(d, DOMAIN_64BIT);
+    default:
+        return -EINVAL;
+    }
+}
+
  long subarch_do_domctl(struct xen_domctl *domctl, struct domain *d,
                         XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl)
  {
      switch ( domctl->cmd )
      {
      case XEN_DOMCTL_set_address_size:
-        switch ( domctl->u.address_size.size )
-        {
-        case 32:
-            if ( !cpu_has_el1_32 )
-                return -EINVAL;
-            /* SVE is not supported for 32 bit domain */
-            if ( is_sve_domain(d) )
-                return -EINVAL;
-            return switch_mode(d, DOMAIN_32BIT);
-        case 64:
-            return switch_mode(d, DOMAIN_64BIT);
-        default:
-            return -EINVAL;
-        }
-        break;
+        return set_address_size(d, domctl->u.address_size.size);
 
      default:
          return -ENOSYS;
Signature · 1 line
Julien Grall
Reviewed-by on v1 1/1
#3 lore raw
In reply to Julien Grall

Hi Julien,

Quoted 7 lines · Julien Grall

From: Julien Grall <[email protected]>

The 'break' the XEN_DOMCTL_set_address_size is unreachable and tools like Eclair will report as a violation of Misra Rule 2.1.

Quoted 6 lines

Furthermore, the nested switch is not very easy to read. So move out the nested switch in a separate function to improve the readability and hopefully address the MISRA violation.

Cheers
Bertrand

Quoted 64 lines

---

Only compiled tested. Waiting for the CI to confirm there is no regression.
---
xen/arch/arm/arm64/domctl.c | 34 +++++++++++++++++++---------------

1 file changed, 19 insertions(+), 15 deletions(-)
xen/arch/arm/arm64/domctl.c +19 -15
index 14fc622e9956..8720d126c97d 100644
@@ -33,27 +33,31 @@ static long switch_mode(struct domain *d, enum domain_type type)
    return 0;
 }
 
+static long set_address_size(struct domain *d, uint32_t address_size)
+{
+    switch ( address_size )
+    {
+    case 32:
+        if ( !cpu_has_el1_32 )
+            return -EINVAL;
+        /* SVE is not supported for 32 bit domain */
+        if ( is_sve_domain(d) )
+            return -EINVAL;
+        return switch_mode(d, DOMAIN_32BIT);
+    case 64:
+        return switch_mode(d, DOMAIN_64BIT);
+    default:
+        return -EINVAL;
+    }
+}
+
 long subarch_do_domctl(struct xen_domctl *domctl, struct domain *d,
                       XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl)
 {
    switch ( domctl->cmd )
    {
    case XEN_DOMCTL_set_address_size:
-        switch ( domctl->u.address_size.size )
-        {
-        case 32:
-            if ( !cpu_has_el1_32 )
-                return -EINVAL;
-            /* SVE is not supported for 32 bit domain */
-            if ( is_sve_domain(d) )
-                return -EINVAL;
-            return switch_mode(d, DOMAIN_32BIT);
-        case 64:
-            return switch_mode(d, DOMAIN_64BIT);
-        default:
-            return -EINVAL;
-        }
-        break;
+        return set_address_size(d, domctl->u.address_size.size);
 
    default:
        return -ENOSYS;
Signature · 1 line
2.40.1
#4 lore raw
Quoted 9 lines · Bertrand Marquis

Hi Julien,

Quoted 7 lines · Julien Grall

From: Julien Grall <[email protected]>

The 'break' the XEN_DOMCTL_set_address_size is unreachable and tools like Eclair will report as a violation of Misra Rule 2.1.

Quoted 8 lines
Quoted 6 lines

Furthermore, the nested switch is not very easy to read. So move out the nested switch in a separate function to improve the readability and hopefully address the MISRA violation.

I added the patch to my for-4.19 branch