Skip to contentSkip to conversation

xen/arm: asm/atomic.h: Fix MISRA C 2012 Rule violations

PATCH Superseded Xenia Ragiadakou 5 participants 13 messages Jul 27, 2022 – Jul 28, 2022

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover xen/arm: asm/atomic.h: Fix MISRA C 2012 Rule violations

This patch series fixes violations of rules 20.7 and 2.5 found in xen/arch/arm/include/asm/atomic.h.
Rule 2.5 (a project should not contain unused macro declarations) is advisory.

Xenia Ragiadakou (2):
xen/arm: asm/atomic.h: Fix MISRA C 2012 Rule 20.7 violation xen/arm: asm/atomic.h: Fix MISRA C 2012 Rule 2.5 violation

 xen/arch/arm/include/asm/atomic.h | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/2 xen/arm: asm/atomic.h: Fix MISRA C 2012 Rule 20.7 violation

The macro parameter 'p' is used as an expression and needs to be enclosed in parentheses.

---
 xen/arch/arm/include/asm/atomic.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
xen/arch/arm/include/asm/atomic.h +4 -4
index ac2798d095..f5ef744b4b 100644
@@ -123,15 +123,15 @@ static always_inline void write_atomic_size(volatile void *p,
 }
 
 #define read_atomic(p) ({                                               \
-    union { typeof(*p) val; char c[0]; } x_;                            \
-    read_atomic_size(p, x_.c, sizeof(*p));                              \
+    union { typeof(*(p)) val; char c[0]; } x_;                          \
+    read_atomic_size((p), x_.c, sizeof(*(p)));                          \
     x_.val;                                                             \
 })
 
 #define write_atomic(p, x)                                              \
     do {                                                                \
-        typeof(*p) x_ = (x);                                            \
-        write_atomic_size(p, &x_, sizeof(*p));                          \
+        typeof(*(p)) x_ = (x);                                          \
+        write_atomic_size((p), &x_, sizeof(*(p)));                      \
     } while ( false )
 
 #define add_sized(p, x) ({                                              \
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 1/2] xen/arm: asm/atomic.h: Fix MISRA C 2012 Rule 20.7 violation

Quoted 2 lines · Xenia Ragiadakou

The macro parameter 'p' is used as an expression and needs to be enclosed in parentheses.

Yes, but ...

xen/arch/arm/include/asm/atomic.h static always_inline void write_atomic_size(volatile void *p, · line 127

Quoted 10 lines
xen/arch/arm/include/asm/atomic.h +2 -2
@@ -123,15 +123,15 @@ static always_inline void write_atomic_size(volatile void *p,
 }
 
 #define read_atomic(p) ({                                               \
-    union { typeof(*p) val; char c[0]; } x_;                            \
-    read_atomic_size(p, x_.c, sizeof(*p));                              \
+    union { typeof(*(p)) val; char c[0]; } x_;                          \
+    read_atomic_size((p), x_.c, sizeof(*(p)));                          \

... not in the first argument's case - that's not an expression. Too few parentheses are a risk, but too many are as well, as they negatively affect readability.

Jan

#3 lore raw

Re: [PATCH 1/2] xen/arm: asm/atomic.h: Fix MISRA C 2012 Rule 20.7 violation

Hi Jan,

Quoted 11 lines · Jan Beulich
Quoted 2 lines · Xenia Ragiadakou

The macro parameter 'p' is used as an expression and needs to be enclosed in parentheses.

Yes, but ...

Quoted 5 lines
xen/arch/arm/include/asm/atomic.h +0 -0
@@ -123,15 +123,15 @@ static always_inline void write_atomic_size(volatile void *p,
  }
 
Quoted 10 lines

xen/arch/arm/include/asm/atomic.h static always_inline void write_atomic_size(volatile void *p, · line 127

Quoted 5 lines
xen/arch/arm/include/asm/atomic.h +2 -2
  #define read_atomic(p) ({                                               \
-    union { typeof(*p) val; char c[0]; } x_;                            \
-    read_atomic_size(p, x_.c, sizeof(*p));                              \
+    union { typeof(*(p)) val; char c[0]; } x_;                          \
+    read_atomic_size((p), x_.c, sizeof(*(p)));                          \

... not in the first argument's case - that's not an expression. Too few parentheses are a risk, but too many are as well, as they negatively affect readability.

Yes you are right. Here
write_atomic_size((p), &x_, sizeof(*(p)));
as well.

I will fix and resend.

Signature · 1 line
Xenia
#4 lore raw

PATCH 2/2 xen/arm: asm/atomic.h: Fix MISRA C 2012 Rule 2.5 violation

In reply to Xenia Ragiadakou

Remove unused macro atomic_xchg().

---
 xen/arch/arm/include/asm/atomic.h | 2 --
 1 file changed, 2 deletions(-)
xen/arch/arm/include/asm/atomic.h +0 -2
index f5ef744b4b..a2dc125291 100644
@@ -223,8 +223,6 @@ static inline int atomic_add_unless(atomic_t *v, int a, int u)
     return __atomic_add_unless(v, a, u);
 }
 
-#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
-
 #endif /* __ARCH_ARM_ATOMIC__ */
 /*
  * Local variables:
Signature · 1 line
2.34.1
#5 lore raw

Re: [PATCH 2/2] xen/arm: asm/atomic.h: Fix MISRA C 2012 Rule 2.5 violation

Hi Xenia,

Quoted 12 lines · Xenia Ragiadakou

Remove unused macro atomic_xchg().

---
  xen/arch/arm/include/asm/atomic.h | 2 --
  1 file changed, 2 deletions(-)
xen/arch/arm/include/asm/atomic.h +0 -0
index f5ef744b4b..a2dc125291 100644

xen/arch/arm/include/asm/atomic.h static inline int atomic_add_unless(atomic_t *v, int a, int u) · line 227

Quoted 5 lines
xen/arch/arm/include/asm/atomic.h +0 -2
@@ -223,8 +223,6 @@ static inline int atomic_add_unless(atomic_t *v, int a, int u)
      return __atomic_add_unless(v, a, u);
  }
 
-#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
-

While I agree this is unused today, the wrapper is quite trivial and part of the generic API (x86 also provides one). So I am not in favor of removing it just to please MISRA.

That said, if Bertrand and Stefano agrees with removing it then you should also remove the x86 version to avoid inconsistency.

Cheers,

Signature · 1 line
Julien Grall
#6 lore raw

Re: [PATCH 2/2] xen/arm: asm/atomic.h: Fix MISRA C 2012 Rule 2.5 violation

Hi Julien,

Quoted 10 lines · Julien Grall

Hi Xenia,

Quoted 7 lines · Xenia Ragiadakou

Remove unused macro atomic_xchg().

---
  xen/arch/arm/include/asm/atomic.h | 2 --

  1 file changed, 2 deletions(-)
Quoted 12 lines

xen/arch/arm/include/asm/atomic.h static inline int atomic_add_unless(atomic_t *v, · line 227

Quoted 10 lines
xen/arch/arm/include/asm/atomic.h +0 -2
index f5ef744b4b..a2dc125291 100644
@@ -223,8 +223,6 @@ static inline int atomic_add_unless(atomic_t *v, 
 int a, int u)
       return __atomic_add_unless(v, a, u);
   }
-#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
-

While I agree this is unused today, the wrapper is quite trivial and part of the generic API (x86 also provides one). So I am not in favor of removing it just to please MISRA.

That's fine, the rule 2.5 is advisory. I sent the patch because I noticed that the macro was unused, just in case ...

Quoted 5 lines

That said, if Bertrand and Stefano agrees with removing it then you should also remove the x86 version to avoid inconsistency.

Cheers,

Signature · 1 line
Xenia
#7 lore raw

Re: [PATCH 2/2] xen/arm: asm/atomic.h: Fix MISRA C 2012 Rule 2.5 violation

In reply to Julien Grall

Hi Julien,

Quoted 22 lines · Julien Grall

Hi Xenia,

Quoted 10 lines · Xenia Ragiadakou

Remove unused macro atomic_xchg().

---
xen/arch/arm/include/asm/atomic.h | 2 --

1 file changed, 2 deletions(-)
xen/arch/arm/include/asm/atomic.h +0 -0
index f5ef744b4b..a2dc125291 100644

xen/arch/arm/include/asm/atomic.h static inline int atomic_add_unless(atomic_t *v, int a, int u) · line 226

Quoted 4 lines
xen/arch/arm/include/asm/atomic.h +0 -2
@@ -223,8 +223,6 @@ static inline int atomic_add_unless(atomic_t *v, int a, int u)
 return __atomic_add_unless(v, a, u);
 }
-#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
-

While I agree this is unused today, the wrapper is quite trivial and part of the generic API (x86 also provides one). So I am not in favor of removing it just to please MISRA.

Quoted 1 line

That said, if Bertrand and Stefano agrees with removing it then you should also remove the x86 version to avoid inconsistency.

I think we can keep this and maybe add a comment on top to document a known violation:
/* TODO: MISRA_VIOLATION 2.5 */

The FUSA SIG is still working on defining how to document those in the code.

I think I suggested one way to do this at some point but the discussion never finished.

Cheers
Bertrand

Quoted 5 lines

Cheers,

Signature · 1 line
Julien Grall
#8 lore raw

Re: [PATCH 2/2] xen/arm: asm/atomic.h: Fix MISRA C 2012 Rule 2.5 violation

Quoted 1 line · Bertrand Marquis

Hi Julien,

Hi Bertrand,

Quoted 23 lines
Quoted 23 lines · Julien Grall

Hi Xenia,

Quoted 10 lines · Xenia Ragiadakou

Remove unused macro atomic_xchg().

---
xen/arch/arm/include/asm/atomic.h | 2 --

1 file changed, 2 deletions(-)
xen/arch/arm/include/asm/atomic.h +0 -0
index f5ef744b4b..a2dc125291 100644

xen/arch/arm/include/asm/atomic.h static inline int atomic_add_unless(atomic_t *v, int a, int u) · line 226

Quoted 4 lines
xen/arch/arm/include/asm/atomic.h +0 -2
@@ -223,8 +223,6 @@ static inline int atomic_add_unless(atomic_t *v, int a, int u)
 return __atomic_add_unless(v, a, u);
 }
-#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
-

While I agree this is unused today, the wrapper is quite trivial and part of the generic API (x86 also provides one). So I am not in favor of removing it just to please MISRA.

Quoted 4 lines
Quoted 1 line

That said, if Bertrand and Stefano agrees with removing it then you should also remove the x86 version to avoid inconsistency.

I think we can keep this and maybe add a comment on top to document a known violation:
/* TODO: MISRA_VIOLATION 2.5 */

While I am fine with this goal of the comment (i.e. indicating where Xen is not MISRA compliant), I think this is one place where I would rather not want one because it can get stale if someones decide to use the function.

Cheers,

Signature · 1 line
Julien Grall
#9 lore raw

Re: [PATCH 2/2] xen/arm: asm/atomic.h: Fix MISRA C 2012 Rule 2.5 violation

Hi Julien,

Quoted 31 lines · Julien Grall
Quoted 1 line · Bertrand Marquis

Hi Julien,

Hi Bertrand,

Quoted 22 lines
Quoted 22 lines · Julien Grall

Hi Xenia,

Quoted 10 lines · Xenia Ragiadakou

Remove unused macro atomic_xchg().

---
xen/arch/arm/include/asm/atomic.h | 2 --

1 file changed, 2 deletions(-)
xen/arch/arm/include/asm/atomic.h +0 -0
index f5ef744b4b..a2dc125291 100644

xen/arch/arm/include/asm/atomic.h static inline int atomic_add_unless(atomic_t *v, int a, int u) · line 226

Quoted 4 lines
xen/arch/arm/include/asm/atomic.h +0 -2
@@ -223,8 +223,6 @@ static inline int atomic_add_unless(atomic_t *v, int a, int u)
 return __atomic_add_unless(v, a, u);
 }
-#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
-

While I agree this is unused today, the wrapper is quite trivial and part of the generic API (x86 also provides one). So I am not in favor of removing it just to please MISRA.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

That said, if Bertrand and Stefano agrees with removing it then you should also remove the x86 version to avoid inconsistency.

I think we can keep this and maybe add a comment on top to document a known violation:
/* TODO: MISRA_VIOLATION 2.5 */

While I am fine with this goal of the comment (i.e. indicating where Xen is not MISRA compliant), I think this is one place where I would rather not want one because it can get stale if someones decide to use the function.

I think the one doing that will have to update the comment otherwise we will never manage to have an analysis without findings. Having those kind of comments in the code for violation also means that they must be updated if the violation is solved.

Maybe we will need a run ignoring those to identify possible violations which are not violations anymore but this might be hard to do.

Cheers
Bertrand

Quoted 5 lines

Cheers,

Signature · 1 line
Julien Grall
#10 lore raw

Re: [PATCH 2/2] xen/arm: asm/atomic.h: Fix MISRA C 2012 Rule 2.5 violation

Quoted 31 lines · Bertrand Marquis
Quoted 31 lines · Julien Grall
Quoted 1 line · Bertrand Marquis

Hi Julien,

Hi Bertrand,

Quoted 22 lines
Quoted 22 lines · Julien Grall

Hi Xenia,

Quoted 10 lines · Xenia Ragiadakou

Remove unused macro atomic_xchg().

---
xen/arch/arm/include/asm/atomic.h | 2 --

1 file changed, 2 deletions(-)
xen/arch/arm/include/asm/atomic.h +0 -0
index f5ef744b4b..a2dc125291 100644

xen/arch/arm/include/asm/atomic.h static inline int atomic_add_unless(atomic_t *v, int a, int u) · line 226

Quoted 4 lines
xen/arch/arm/include/asm/atomic.h +0 -2
@@ -223,8 +223,6 @@ static inline int atomic_add_unless(atomic_t *v, int a, int u)
 return __atomic_add_unless(v, a, u);
 }
-#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
-

While I agree this is unused today, the wrapper is quite trivial and part of the generic API (x86 also provides one). So I am not in favor of removing it just to please MISRA.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

That said, if Bertrand and Stefano agrees with removing it then you should also remove the x86 version to avoid inconsistency.

I think we can keep this and maybe add a comment on top to document a known violation:
/* TODO: MISRA_VIOLATION 2.5 */

While I am fine with this goal of the comment (i.e. indicating where Xen is not MISRA compliant), I think this is one place where I would rather not want one because it can get stale if someones decide to use the function.

I think the one doing that will have to update the comment otherwise we will never manage to have an analysis without findings.

I was under the impression that Xen will never officially follow some of the MISRA rules. So I would expect the tools to be able to detect such cases so we don't have to add a comment for every deviation on something we will never support.

Quoted 1 line

Having those kind of comments in the code for violation also means that they must be updated if the violation is solved.

Right, but for thing like unused function, this is quite easy to miss by both the developer and reviewers. So we are going to end up to comments for nothing.

Quoted 2 lines

Maybe we will need a run ignoring those to identify possible violations which are not violations anymore but this might be hard to do.

TBH, I think it would be best if we can teach the tools to ignore certain rules.

Cheers,

Signature · 1 line
Julien Grall
#11 lore raw

Re: [PATCH 2/2] xen/arm: asm/atomic.h: Fix MISRA C 2012 Rule 2.5 violation

Hi Julien,

Quoted 45 lines · Julien Grall
Quoted 31 lines · Bertrand Marquis
Quoted 31 lines · Julien Grall
Quoted 1 line · Bertrand Marquis

Hi Julien,

Hi Bertrand,

Quoted 22 lines
Quoted 22 lines · Julien Grall

Hi Xenia,

Quoted 10 lines · Xenia Ragiadakou

Remove unused macro atomic_xchg().

---
xen/arch/arm/include/asm/atomic.h | 2 --

1 file changed, 2 deletions(-)
xen/arch/arm/include/asm/atomic.h +0 -0
index f5ef744b4b..a2dc125291 100644

xen/arch/arm/include/asm/atomic.h static inline int atomic_add_unless(atomic_t *v, int a, int u) · line 226

Quoted 4 lines
xen/arch/arm/include/asm/atomic.h +0 -2
@@ -223,8 +223,6 @@ static inline int atomic_add_unless(atomic_t *v, int a, int u)
 return __atomic_add_unless(v, a, u);
 }
-#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
-

While I agree this is unused today, the wrapper is quite trivial and part of the generic API (x86 also provides one). So I am not in favor of removing it just to please MISRA.

Quoted 6 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

That said, if Bertrand and Stefano agrees with removing it then you should also remove the x86 version to avoid inconsistency.

I think we can keep this and maybe add a comment on top to document a known violation:
/* TODO: MISRA_VIOLATION 2.5 */

While I am fine with this goal of the comment (i.e. indicating where Xen is not MISRA compliant), I think this is one place where I would rather not want one because it can get stale if someones decide to use the function.

I think the one doing that will have to update the comment otherwise we will never manage to have an analysis without findings.

I was under the impression that Xen will never officially follow some of the MISRA rules. So I would expect the tools to be able to detect such cases so we don't have to add a comment for every deviation on something we will never support.

Quoted 7 lines
Quoted 1 line

Having those kind of comments in the code for violation also means that they must be updated if the violation is solved.

Right, but for thing like unused function, this is quite easy to miss by both the developer and reviewers. So we are going to end up to comments for nothing.

Quoted 1 line

Maybe we will need a run ignoring those to identify possible violations which are not violations anymore but this might be hard to do.

TBH, I think it would be best if we can teach the tools to ignore certain rules.

Definitely it is possible to instruct the tool to ignore this you are right and for 2.5 we should (for some reason I was under the impression that we said we would follow 2.5 but accept deviations).

@Xenia: please ignore and do not add a comment for this.

I think we will need to distinguish 2 kind of not following:
- not following at all (disable in the tools)
- accepting some deviations (documented in the code)

As much as we can, I think we should target the second unless we have a lot of violations.

Cheers
Bertrand

Quoted 5 lines

Cheers,

Signature · 1 line
Julien Grall
#12 lore raw

Re: [PATCH 2/2] xen/arm: asm/atomic.h: Fix MISRA C 2012 Rule 2.5 violation

Quoted 41 lines · Bertrand Marquis
Quoted 41 lines · Julien Grall
Quoted 28 lines · Bertrand Marquis
Quoted 28 lines · Julien Grall
Quoted 1 line · Bertrand Marquis

Hi Julien,

Hi Bertrand,

Quoted 22 lines
Quoted 22 lines · Julien Grall

Hi Xenia,

Quoted 10 lines · Xenia Ragiadakou

Remove unused macro atomic_xchg().

---
xen/arch/arm/include/asm/atomic.h | 2 --

1 file changed, 2 deletions(-)
xen/arch/arm/include/asm/atomic.h +0 -0
index f5ef744b4b..a2dc125291 100644

xen/arch/arm/include/asm/atomic.h static inline int atomic_add_unless(atomic_t *v, int a, int u) · line 226

Quoted 4 lines
xen/arch/arm/include/asm/atomic.h +0 -2
@@ -223,8 +223,6 @@ static inline int atomic_add_unless(atomic_t *v, int a, int u)
 return __atomic_add_unless(v, a, u);
 }
-#define atomic_xchg(v, new) (xchg(&((v)->counter), new))
-

While I agree this is unused today, the wrapper is quite trivial and part of the generic API (x86 also provides one). So I am not in favor of removing it just to please MISRA.

Quoted 6 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

That said, if Bertrand and Stefano agrees with removing it then you should also remove the x86 version to avoid inconsistency.

I think we can keep this and maybe add a comment on top to document a known violation:
/* TODO: MISRA_VIOLATION 2.5 */

While I am fine with this goal of the comment (i.e. indicating where Xen is not MISRA compliant), I think this is one place where I would rather not want one because it can get stale if someones decide to use the function.

I think the one doing that will have to update the comment otherwise we will never manage to have an analysis without findings.

I was under the impression that Xen will never officially follow some of the MISRA rules. So I would expect the tools to be able to detect such cases so we don't have to add a comment for every deviation on something we will never support.

Quoted 1 line

Having those kind of comments in the code for violation also means that they must be updated if the violation is solved.

Quoted 7 lines
Quoted 5 lines

Right, but for thing like unused function, this is quite easy to miss by both the developer and reviewers. So we are going to end up to comments for nothing.

Quoted 1 line

Maybe we will need a run ignoring those to identify possible violations which are not violations anymore but this might be hard to do.

TBH, I think it would be best if we can teach the tools to ignore certain rules.

Definitely it is possible to instruct the tool to ignore this you are right and for 2.5 we should (for some reason I was under the impression that we said we would follow 2.5 but accept deviations).

Absolutely possible, basically we (the community) are the ones providing the list of rules to the MISRA C checkers.

Quoted 5 lines

@Xenia: please ignore and do not add a comment for this.

I think we will need to distinguish 2 kind of not following:
- not following at all (disable in the tools)
- accepting some deviations (documented in the code)

Yes, exactly right.

Quoted 1 line

As much as we can, I think we should target the second unless we have a lot of violations.

+1