Skip to contentSkip to conversation

address some violations of MISRA C Rule 20.7

PATCH In review Nicola Vetrini 4 participants 22 messages Mar 18, 2024 – Mar 19, 2024

lore
Share & tools
lore
#0 lore raw

cover address some violations of MISRA C Rule 20.7

Hi all,

this series aims to refactor some macros that cause violations of MISRA C Rule 20.7 ("Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses"). All the macros touched by these patches are in some way involved in violations, and the strategy adopted to bring them into compliance is to add parentheses around macro arguments where needed.

Given that the community has previously requested a deviation from the rule, as stated in docs/misra/deviations.rst, and reported below for convenience [1], some macro parameters do not need any adjusting (e.g., when used as lhs to an assignment in statement expressions).

Note that the patch on list.h of the earlier series [2] has been intentionally left out of this series, because it still needs some adjustments.

[1] - Code violating Rule 20.7 is safe when macro parameters are used:

       (1) as function arguments;
       (2) as macro arguments;
       (3) as array indices;
       (4) as lhs in assignments.

[2] https://lore.kernel.org/xen-devel/[email protected]/

Nicola Vetrini (10):
x86/cpufeature: add parentheses to comply with Rule 20.7 AMD/IOMMU: guest: address violations of MISRA C Rule 20.7 xen/xsm: add parentheses to comply with MISRA C Rule 20.7 xen/device_tree: address violations of MISRA C Rule 20.7
EFI: address violations of MISRA C Rule 20.7 xen/arm: smmu: address violations of MISRA C Rule 20.7 xen/efi: efibind: address violations of MISRA C Rule 20.7 xen/notifier: address violations of MISRA C Rule 20.7 xen/wait: address violations of MISRA C Rule 20.7 xen/sched: address violations of MISRA C Rule 20.7

 xen/arch/arm/include/asm/arm64/efibind.h  | 4 ++--
 xen/arch/x86/include/asm/cpufeatureset.h  | 2 +-
 xen/arch/x86/include/asm/x86_64/efibind.h | 4 ++--
 xen/common/sched/private.h                | 2 +-
 xen/drivers/passthrough/amd/iommu_guest.c | 2 +-
 xen/drivers/passthrough/arm/smmu.c        | 2 +-
 xen/include/efi/efierr.h                  | 2 +-
 xen/include/xen/device_tree.h             | 2 +-
 xen/include/xen/notifier.h                | 2 +-
 xen/include/xen/wait.h                    | 4 ++--
 xen/include/xsm/dummy.h                   | 4 ++--
 11 files changed, 15 insertions(+), 15 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/10 x86/cpufeature: add parentheses to comply with Rule 20.7

MISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.

No functional change.

---
 xen/arch/x86/include/asm/cpufeatureset.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/x86/include/asm/cpufeatureset.h +1 -1
index f179229f192f..a9c51bc514a8 100644
@@ -5,7 +5,7 @@
 
 #include <xen/stringify.h>
 
-#define XEN_CPUFEATURE(name, value) X86_FEATURE_##name = value,
+#define XEN_CPUFEATURE(name, value) X86_FEATURE_##name = (value),
 enum {
 #include <public/arch-x86/cpufeatureset.h>
 #include <asm/cpufeatures.h>
Signature · 1 line
2.34.1
Acked-by on v1 1/10
#2 lore raw

Re: [XEN PATCH 01/10] x86/cpufeature: add parentheses to comply with Rule 20.7

Quoted 9 lines · Nicola Vetrini

MISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.

No functional change.

#3 lore raw

PATCH 2/10 AMD/IOMMU: guest: address violations of MISRA C Rule 20.7

In reply to Nicola Vetrini

MISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.

No functional change.

---
This local helper may disappear as a follow-up to [1], if the function that remains there after the cleanup ends up being removed, so this patch has a slight dependency on what follows up from that thread (in which case it can be dropped).

[1] https://lore.kernel.org/xen-devel/alpine.DEB.2.22.394.2403151724380.853156@ubuntu-linux-20-04-desktop/T/#m9474ad4f35830345a22acd4a665245f7085d4b46

---
 xen/drivers/passthrough/amd/iommu_guest.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/drivers/passthrough/amd/iommu_guest.c +1 -1
index 4c4252eea116..5bfaa48d9d65 100644
@@ -30,7 +30,7 @@
 #define GUEST_ADDRESS_SIZE_6_LEVEL              0x2
 #define HOST_ADDRESS_SIZE_6_LEVEL               0x2
 
-#define reg_to_u64(reg) (((uint64_t)reg.hi << 32) | reg.lo )
+#define reg_to_u64(reg) (((uint64_t)((reg).hi << 32)) | (reg).lo )
 #define u64_to_reg(reg, val) \
     do \
     { \
Signature · 1 line
2.34.1
#4 lore raw

Re: [XEN PATCH 02/10] AMD/IOMMU: guest: address violations of MISRA C Rule 20.7

Quoted 6 lines · Nicola Vetrini

MISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.

Quoted 7 lines

No functional change.

---
This local helper may disappear as a follow-up to [1], if the function that remains there after the cleanup ends up being removed, so this patch has a slight dependency on what follows up from that thread (in which case it can be dropped).

Indeed let's not add more churn to the file before it disappears.

Jan

Quoted 1 line

[1] https://lore.kernel.org/xen-devel/alpine.DEB.2.22.394.2403151724380.853156@ubuntu-linux-20-04-desktop/T/#m9474ad4f35830345a22acd4a665245f7085d4b46

#5 lore raw

PATCH 3/10 xen/xsm: add parentheses to comply with MISRA C Rule 20.7

In reply to Nicola Vetrini

MISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.

No functional change.

---
 xen/include/xsm/dummy.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
xen/include/xsm/dummy.h +2 -2
index 8671af1ba4d3..88039fdd227c 100644
@@ -58,7 +58,7 @@ void __xsm_action_mismatch_detected(void);
 
 #define XSM_DEFAULT_ARG /* */
 #define XSM_DEFAULT_VOID void
-#define XSM_ASSERT_ACTION(def) xsm_default_t action = def; (void)action
+#define XSM_ASSERT_ACTION(def) xsm_default_t action = (def); (void)action
 
 #else /* CONFIG_XSM */
 
@@ -71,7 +71,7 @@ void __xsm_action_mismatch_detected(void);
 #define XSM_INLINE always_inline
 #define XSM_DEFAULT_ARG xsm_default_t action,
 #define XSM_DEFAULT_VOID xsm_default_t action
-#define XSM_ASSERT_ACTION(def) LINKER_BUG_ON(def != action)
+#define XSM_ASSERT_ACTION(def) LINKER_BUG_ON((def) != action)
 
 #endif /* CONFIG_XSM */
 
Signature · 1 line
2.34.1
Reviewed-by on v1 3/10
#6 lore raw

Re: [XEN PATCH 03/10] xen/xsm: add parentheses to comply with MISRA C Rule 20.7

Quoted 9 lines · Nicola Vetrini

MISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.

No functional change.

#7 lore raw

PATCH 4/10 xen/device_tree: address violations of MISRA C Rule 20.7

In reply to Nicola Vetrini

MISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.

No functional change.

---
This patch uniforms the way these macros use parentheses, skipping the already deviated cases of the lhs of an assignment. In principle, all three could have the parentheses on the macro argument in the lhs added.

---
 xen/include/xen/device_tree.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/include/xen/device_tree.h +1 -1
index 6fe2fa8b2123..e6287305a7b5 100644
@@ -250,7 +250,7 @@ dt_find_interrupt_controller(const struct dt_device_match *matches);
     for ( pp = (dn)->properties; (pp) != NULL; pp = (pp)->next )
 
 #define dt_for_each_device_node(dt, dn)                     \
-    for ( dn = dt; (dn) != NULL; dn = (dn)->allnext )
+    for ( dn = (dt); (dn) != NULL; dn = (dn)->allnext )
 
 #define dt_for_each_child_node(dt, dn)                      \
     for ( dn = (dt)->child; (dn) != NULL; dn = (dn)->sibling )
Signature · 1 line
2.34.1
Reviewed-by on v1 4/10
#8 lore raw

Re: [XEN PATCH 04/10] xen/device_tree: address violations of MISRA C Rule 20.7

Quoted 9 lines · Nicola Vetrini

MISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.

No functional change.

#9 lore raw

PATCH 5/10 EFI: address violations of MISRA C Rule 20.7

In reply to Nicola Vetrini

MISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.

No functional change.

---
This file is matched by exclude-list.json, but the fix is rather trivial and actually benefits code that is in scope for compliance.

---
 xen/include/efi/efierr.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/include/efi/efierr.h +1 -1
index dfd3d3cf4867..2ecde0b31302 100644
@@ -22,7 +22,7 @@ Revision History
 
 
 #define EFIWARN(a)                            (a)
-#define EFI_ERROR(a)              (((INTN) a) < 0)
+#define EFI_ERROR(a)              (((INTN)(a)) < 0)
 
 
 #define EFI_SUCCESS                             0
Signature · 1 line
2.34.1
Acked-by on v1 5/10
#10 lore raw

Re: [XEN PATCH 05/10] EFI: address violations of MISRA C Rule 20.7

Quoted 6 lines · Nicola Vetrini

MISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.

Quoted 6 lines

No functional change.

---
This file is matched by exclude-list.json, but the fix is rather trivial and actually benefits code that is in scope for compliance.

Hmm, yes, the change is simple enough to not be a big hindrance even if we were to pull in incremental updates from gnu-efi, so

Albeit preferably with ...

xen/include/efi/efierr.h Revision History · line 25

Quoted 8 lines
xen/include/efi/efierr.h +1 -1
@@ -22,7 +22,7 @@ Revision History
 
 
 #define EFIWARN(a)                            (a)
-#define EFI_ERROR(a)              (((INTN) a) < 0)
+#define EFI_ERROR(a)              (((INTN)(a)) < 0)

... excess parentheses dropped in exchange:

#define EFI_ERROR(a) ((INTN)(a) < 0)

I may take the liberty of doing so while committing.

Jan

#11 lore raw

PATCH 6/10 xen/arm: smmu: address violations of MISRA C Rule 20.7

In reply to Nicola Vetrini

MISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.

No functional change.

---
 xen/drivers/passthrough/arm/smmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/drivers/passthrough/arm/smmu.c +1 -1
index 83196057a937..f2cee82f553a 100644
@@ -326,7 +326,7 @@ static struct iommu_group *iommu_group_get(struct device *dev)
  */
 #define ARM_SMMU_GR0_NS(smmu)						\
 	((smmu)->base +							\
-		((smmu->options & ARM_SMMU_OPT_SECURE_CFG_ACCESS)	\
+		(((smmu)->options & ARM_SMMU_OPT_SECURE_CFG_ACCESS)	\
 			? 0x400 : 0))
 
 /* Page table bits */
Signature · 1 line
2.34.1
Reviewed-by on v1 6/10
#12 lore raw

Re: [XEN PATCH 06/10] xen/arm: smmu: address violations of MISRA C Rule 20.7

Quoted 9 lines · Nicola Vetrini

MISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.

No functional change.

#13 lore raw

PATCH 7/10 xen/efi: efibind: address violations of MISRA C Rule 20.7

In reply to Nicola Vetrini

MISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.

No functional change.

---
This file is matched by exclude-list.json, but the fix is rather trivial and impacts code that in under the scope of MISRA compliance.

---
 xen/arch/arm/include/asm/arm64/efibind.h  | 4 ++--
 xen/arch/x86/include/asm/x86_64/efibind.h | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)
xen/arch/arm/include/asm/arm64/efibind.h +2 -2
index f13eadd4f0ab..a1323d452e2e 100644
@@ -22,9 +22,9 @@ Revision History
 #pragma pack()
 #endif
 
-#define EFIERR(a)           (0x8000000000000000ULL | a)
+#define EFIERR(a)           (0x8000000000000000ULL | (a))
 #define EFI_ERROR_MASK      0x8000000000000000ULL
-#define EFIERR_OEM(a)       (0xc000000000000000ULL | a)
+#define EFIERR_OEM(a)       (0xc000000000000000ULL | (a))
 
 #define BAD_POINTER         0xFBFBFBFBFBFBFBFBULL
 #define MAX_ADDRESS         0xFFFFFFFFFFFFFFFFULL
xen/arch/x86/include/asm/x86_64/efibind.h +2 -2
index e23cd16cb6a0..28bc18c24bb3 100644
@@ -117,9 +117,9 @@ typedef uint64_t   UINTN;
     #endif
 #endif
 
-#define EFIERR(a)           (0x8000000000000000 | a)
+#define EFIERR(a)           (0x8000000000000000 | (a))
 #define EFI_ERROR_MASK      0x8000000000000000
-#define EFIERR_OEM(a)       (0xc000000000000000 | a)
+#define EFIERR_OEM(a)       (0xc000000000000000 | (a))
 
 
 #define BAD_POINTER         0xFBFBFBFBFBFBFBFB
Signature · 1 line
2.34.1
Acked-by on v1 7/10
#14 lore raw

Re: [XEN PATCH 07/10] xen/efi: efibind: address violations of MISRA C Rule 20.7

Quoted 6 lines · Nicola Vetrini

MISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.

Quoted 6 lines

No functional change.

---
This file is matched by exclude-list.json, but the fix is rather trivial and impacts code that in under the scope of MISRA compliance.

On the same basis as the other change:

I wonder though: Where do we draw the line?

Jan

#15 lore raw

Re: [XEN PATCH 07/10] xen/efi: efibind: address violations of MISRA C Rule 20.7

Quoted 9 lines · Jan Beulich
Quoted 8 lines · Nicola Vetrini

MISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.

No functional change.

Quoted 8 lines
Quoted 4 lines

---
This file is matched by exclude-list.json, but the fix is rather trivial and impacts code that in under the scope of MISRA compliance.

On the same basis as the other change:

Thanks.

Quoted 3 lines

I wonder though: Where do we draw the line?

Jan

So far the policy, at least from my side, has been to submit a patch if the change is simple enough, to avoid adding a special case that needs to be documented and maintained. In case concerns were raised, propose a deviation and follow up from there.

Signature · 2 lines
Nicola Vetrini, BSc
Software Engineer, BUGSENG srl (https://bugseng.com)
#16 lore raw

PATCH 8/10 xen/notifier: address violations of MISRA C Rule 20.7

In reply to Nicola Vetrini

MISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.

No functional change.

---
 xen/include/xen/notifier.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/include/xen/notifier.h +1 -1
index 2a952484df43..05359e8a850d 100644
@@ -34,7 +34,7 @@ struct notifier_head {
 };
 
 #define NOTIFIER_HEAD(name) \
-    struct notifier_head name = { .head = LIST_HEAD_INIT(name.head) }
+    struct notifier_head name = { .head = LIST_HEAD_INIT((name).head) }
 
 
 void notifier_chain_register(
Signature · 1 line
2.34.1
Acked-by on v1 8/10
#17 lore raw

Re: [XEN PATCH 08/10] xen/notifier: address violations of MISRA C Rule 20.7

Quoted 9 lines · Nicola Vetrini

MISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.

No functional change.

#18 lore raw

PATCH 9/10 xen/wait: address violations of MISRA C Rule 20.7

In reply to Nicola Vetrini

MISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.

No functional change.

---
 xen/include/xen/wait.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
xen/include/xen/wait.h +2 -2
index 6eb7667d9c7f..1c68bc564b09 100644
@@ -40,12 +40,12 @@ do {                                            \
     if ( condition )                            \
         break;                                  \
     for ( ; ; ) {                               \
-        prepare_to_wait(&wq);                   \
+        prepare_to_wait(&(wq));                 \
         if ( condition )                        \
             break;                              \
         wait();                                 \
     }                                           \
-    finish_wait(&wq);                           \
+    finish_wait(&(wq));                         \
 } while (0)
 
 /* Private functions. */
Signature · 1 line
2.34.1
Acked-by on v1 9/10
#19 lore raw

Re: [XEN PATCH 09/10] xen/wait: address violations of MISRA C Rule 20.7

Quoted 9 lines · Nicola Vetrini

MISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.

No functional change.

#20 lore raw

PATCH 10/10 xen/sched: address violations of MISRA C Rule 20.7

In reply to Nicola Vetrini

MISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.

No functional change.

---
 xen/common/sched/private.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/common/sched/private.h +1 -1
index 459d1dfb11a5..c0e7c96d24f4 100644
@@ -540,7 +540,7 @@ static inline void sched_unit_unpause(const struct sched_unit *unit)
 }
 
 #define REGISTER_SCHEDULER(x) static const struct scheduler *x##_entry \
-  __used_section(".data.schedulers") = &x
+  __used_section(".data.schedulers") = &(x)
 
 struct cpupool
 {
Signature · 1 line
2.34.1
Acked-by on v1 10/10
#21 lore raw

Re: [XEN PATCH 10/10] xen/sched: address violations of MISRA C Rule 20.7

Quoted 19 lines · Nicola Vetrini

MISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.

No functional change.

---
 xen/common/sched/private.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/common/sched/private.h +0 -0
index 459d1dfb11a5..c0e7c96d24f4 100644

xen/common/sched/private.h static inline void sched_unit_unpause(const struct sched_unit *unit) · line 543

Quoted 5 lines
xen/common/sched/private.h +1 -1
@@ -540,7 +540,7 @@ static inline void sched_unit_unpause(const struct sched_unit *unit)
 }
 
 #define REGISTER_SCHEDULER(x) static const struct scheduler *x##_entry \
-  __used_section(".data.schedulers") = &x
+  __used_section(".data.schedulers") = &(x)

Arguably this is safe, because any x which would be problematic in this line wouldn't compile in the line above.

But it's almost certainly not worth the effort of documenting or deviating, so: