xen: fixes for UBSAN findings with SMMUv3 enabled
cover xen: fixes for UBSAN findings with SMMUv3 enabled
This series contains fixes for UBSAN findings reported when running with SMMUv3 enabled.
Michal Orzel (2):
xen/arm: smmuv3: Add missing U for shifted constant
xen: Change parameter of generic_fls() to unsigned int
xen/drivers/passthrough/arm/smmu-v3.h | 4 ++-- xen/include/xen/bitops.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-)
Signature · 1 line
2.25.1
PATCH 1/2 xen/arm: smmuv3: Add missing U for shifted constant
When running with SMMUv3 and UBSAN enabled, the following is printed:
(XEN) UBSAN: Undefined behaviour in drivers/passthrough/arm/smmu-v3.c:297:12 (XEN) left shift of 1 by 31 places cannot be represented in type 'int'
This refers to shift in Q_OVERFLOW_FLAG that is missing 'U' suffix. While there, also fix the same in GBPA_UPDATE.
This should address MISRA Rule 7.2:
A "u" or "U" suffix shall be applied to all integer constants that are represented in an unsigned type
--- xen/drivers/passthrough/arm/smmu-v3.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
xen/drivers/passthrough/arm/smmu-v3.h +2 -2
@@ -87,7 +87,7 @@#define CR2_E2H (1 << 0)#define ARM_SMMU_GBPA 0x44 -#define GBPA_UPDATE (1 << 31) +#define GBPA_UPDATE (1U << 31)#define GBPA_ABORT (1 << 20)#define ARM_SMMU_IRQ_CTRL 0x50
@@ -159,7 +159,7 @@#define Q_IDX (llq, p) ((p) & ((1 << (llq)->max_n_shift) - 1))#define Q_WRP (llq, p) ((p) & (1 << (llq)->max_n_shift)) -#define Q_OVERFLOW_FLAG (1 << 31) +#define Q_OVERFLOW_FLAG (1U << 31)#define Q_OVF (p) ((p) & Q_OVERFLOW_FLAG)#define Q_ENT (q, p) ((q)->base + \ Q_IDX(&((q)->llq), p) * \
Signature · 1 line
2.25.1
Re: [PATCH 1/2] xen/arm: smmuv3: Add missing U for shifted constant
Hi Michal,
Quoted 7 lines · Michal Orzel
When running with SMMUv3 and UBSAN enabled, the following is printed:
(XEN) UBSAN: Undefined behaviour in
drivers/passthrough/arm/smmu-v3.c:297:12(XEN) left shift of 1 by 31 places cannot be represented in type 'int'
Quoted 8 lines
This refers to shift in
Q_OVERFLOW_FLAGthat is missing 'U' suffix. While there, also fix the same inGBPA_UPDATE.This should address MISRA Rule 7.2:
A "u" or "U" suffix shall be applied to all integer constants that are represented in an unsigned typeSigned-off-by Michal Orzel <[email protected]>
Cheers
Bertrand
Quoted 29 lines
---
xen/drivers/passthrough/arm/smmu-v3.h| 4 ++--1 file changed, 2 insertions(+), 2 deletions(-)xen/drivers/passthrough/arm/smmu-v3.h +2
-2@@ -87,7 +87,7 @@#define CR2_E2H (1 << 0)#define ARM_SMMU_GBPA 0x44 -#define GBPA_UPDATE (1 << 31) +#define GBPA_UPDATE (1U << 31)#define GBPA_ABORT (1 << 20)#define ARM_SMMU_IRQ_CTRL 0x50@@ -159,7 +159,7 @@#define Q_IDX (llq, p) ((p) & ((1 << (llq)->max_n_shift) - 1))#define Q_WRP (llq, p) ((p) & (1 << (llq)->max_n_shift)) -#define Q_OVERFLOW_FLAG (1 << 31) +#define Q_OVERFLOW_FLAG (1U << 31)#define Q_OVF (p) ((p) & Q_OVERFLOW_FLAG)#define Q_ENT (q, p) ((q)->base + \ Q_IDX(&((q)->llq), p) * \Signature · 1 line
2.25.1
PATCH 2/2 xen: Change parameter of generic_fls() to unsigned int
In reply to Michal OrzelWhen running with SMMUv3 and UBSAN enabled on arm64, there are a lot of warnings printed related to shifting into sign bit in generic_fls() as it takes parameter of type int.
Example:
(XEN) UBSAN: Undefined behaviour in ./include/xen/bitops.h:69:11 (XEN) left shift of 134217728 by 4 places cannot be represented in type 'int'
It does not make a lot of sense to ask for the last set bit of a negative value. We don't have a direct user of this helper and all the wrappers pass value of type unsigned {int,long}.
Linux did the same as part of commit:3fc2579e6f16 ("fls: change parameter to unsigned int")
---
It looks like generic_fls() is only used by Arm and invoked only if the arguement passed is a compile time constant. This is true for SMMUv3 which makes use of ffs64() in FIELD_{PREP,GET} macros.
--- xen/include/xen/bitops.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
xen/include/xen/bitops.h +1 -1
@@ -51,7 +51,7 @@ static inline int generic_ffs(int x)* fls: find last bit set. */ -static __inline__int generic_fls (int x) +static __inline__int generic_fls (unsigned int x) {int r =32 ;
Signature · 1 line
2.25.1
Re: [PATCH 2/2] xen: Change parameter of generic_fls() to unsigned int
xen/include/xen/bitops.h static inline int generic_ffs(int x) · line 57
Quoted 11 lines · Michal Orzel
xen/include/xen/bitops.h +1
-1@@ -51,7 +51,7 @@ static inline int generic_ffs(int x)* fls: find last bit set. */ -static __inline__int generic_fls (int x) +static __inline__int generic_fls (unsigned int x) {int r =32 ;
Even if perhaps not affected by UBSAN, generic_ffs() then wants taking care of as well, imo. If additionally you switch __inline__ to inline, things will become nicely symmetric with generic_f{f,l}sl().
Another aspect that may be nice to take care of at this occasion is their return values: None of them can return negative values, so return type would better be unsigned int.
Jan
Re: [PATCH 2/2] xen: Change parameter of generic_fls() to unsigned int
Quoted 9 lines · Jan Beulich
Quoted 6 lines · Michal Orzel
xen/include/xen/bitops.h +0
-0@@ -51,7 +51,7 @@ static inline int generic_ffs(int x)* fls: find last bit set. */
Quoted 9 lines
xen/include/xen/bitops.h static inline int generic_ffs(int x) · line 57
Quoted 5 lines
xen/include/xen/bitops.h +1
-1-static __inline__int generic_fls (int x) +static __inline__int generic_fls (unsigned int x) {int r =32 ;Even if perhaps not affected by UBSAN,
generic_ffs()then wants taking care of as well, imo. If additionally you switch__inline__to inline, things will become nicely symmetric with generic_f{f,l}sl().
If others agree, I can switch generic_ffs() to take unsigned as well (together with s/__inline__/inline/). However, such change would no longer fall in fixes category (i.e. nothing wrong with ffs() taking int):
- is it ok at this stage of release? (not sure but no problem for me to do this)
- is it ok to mix a fix with non-fix change? (I always prefer fixes to be done as standalone changes)
Quoted 4 lines
Another aspect that may be nice to take care of at this occasion is their return values: None of them can return negative values, so return type would better be unsigned int.
Looking at all the variations (including arch asm specific) of helpers to find first set/clear in the codebase, returning int is the de-facto standard. So, this would result in some inconsistency or require changing at least the direct callers to also return unsigned.
I'd prefer not to be required to add this extra churn at this release stage.
~Michal
Re: [PATCH 2/2] xen: Change parameter of generic_fls() to unsigned int
Quoted 17 lines · Michal Orzel
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Michal Orzel
xen/include/xen/bitops.h +0
-0@@ -51,7 +51,7 @@ static inline int generic_ffs(int x)* fls: find last bit set. */ Quoted 9 lines
xen/include/xen/bitops.h static inline int generic_ffs(int x) · line 57
Quoted 5 lines
xen/include/xen/bitops.h +1
-1-static __inline__int generic_fls (int x) +static __inline__int generic_fls (unsigned int x) {int r =32 ;Even if perhaps not affected by UBSAN,
generic_ffs()then wants taking care of as well, imo. If additionally you switch__inline__to inline, things will become nicely symmetric with generic_f{f,l}sl().
Quoted 3 lines
If others agree, I can switch
generic_ffs()to take unsigned as well (together with s/__inline__/inline/). However, such change would no longer fall in fixes category (i.e. nothing wrong withffs()taking int):
- is it ok at this stage of release? (not sure but no problem for me to do this)
I'd say yes, but the release manager would have the ultimate say.
Quoted 1 line
- is it ok to mix a fix with non-fix change? (I always prefer fixes to be done as standalone changes)
Imo it is, to avoid introducing an inconsistency. While such may not themselves be bugs, they increase the risk of introducing some.
Jan
Henry Wang
Reviewed-byRe: [PATCH 2/2] xen: Change parameter of generic_fls() to unsigned int
Hi Jan and Michal,
Quoted 20 lines · Jan Beulich
Quoted 17 lines · Michal Orzel
Quoted 16 lines · Jan Beulich
xen/include/xen/bitops.h static inline int generic_ffs(int x) · line 57
Quoted 11 lines · Michal Orzel
xen/include/xen/bitops.h +1
-1@@ -51,7 +51,7 @@ static inline int generic_ffs(int x)* fls: find last bit set. */ -static __inline__int generic_fls (int x) +static __inline__int generic_fls (unsigned int x) {int r =32 ;Even if perhaps not affected by UBSAN,
generic_ffs()then wants taking care of as well, imo. If additionally you switch__inline__to inline, things will become nicely symmetric with generic_f{f,l}sl().
Quoted 5 lines
Quoted 3 lines
If others agree, I can switch
generic_ffs()to take unsigned as well (together with s/__inline__/inline/). However, such change would no longer fall in fixes category (i.e. nothing wrong withffs()taking int):
- is it ok at this stage of release? (not sure but no problem for me to do this)I'd say yes, but the release manager would have the ultimate say.
From the release aspect, I am fine with doing the above-mentioned extra changes, as we are not in the code freeze stage. From the development point of view, I think whether doing such extra changes or not is supposed to be an agreement between the developer and the maintainer.
Also this patch itself looks good to me so:
Kind regards,
Henry
Quoted 8 lines
Quoted 2 lines
- is it ok to mix a fix with non-fix change? (I always prefer fixes to be done as standalone changes)
Imo it is, to avoid introducing an inconsistency. While such may not themselves be bugs, they increase the risk of introducing some.
Jan