Skip to contentSkip to conversation

xen: fixes for UBSAN findings with SMMUv3 enabled

PATCH In review Michal Orzel 3 participants 5 messages Sep 5, 2023

lore
Share & tools
lore
#0 lore raw

v2 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,ffs}() to unsigned int

 xen/drivers/passthrough/arm/smmu-v3.h | 4 ++--
 xen/include/xen/bitops.h              | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH v2 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

---
Changes in v2:
- add Bertrand's Rb

---
 xen/drivers/passthrough/arm/smmu-v3.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
xen/drivers/passthrough/arm/smmu-v3.h +2 -2
index b381ad373845..05f6b1fb7e33 100644
@@ -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
#2 lore raw

PATCH v2 2/2 xen: Change parameter of generic_{fls,ffs}() to unsigned int

In reply to Michal Orzel

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

To keep consistency between the helpers, take the opportunity to:
- replace __inline__ with inline,
- modify generic_ffs() to take parameter of type unsigned int as well (currently no user and the only wrapper generic_ffsl() passes unsigned long).

---
Changes in v2:
- as requested by Jan, s/__inline__/inline/ and modify generic_ffs() for consistency

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. generic_ffs() has no user at the moment.

Note for the future:
The return type could be unsigned as well. However, looking at all variations of the helpers to find first set/clear in the codebase, returning int is the de-facto standard. Changing the return type of the title helpers would result in inconsistency or require to do modifications in other places which is not something I want to do at this stage of release.

---
 xen/include/xen/bitops.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
xen/include/xen/bitops.h +2 -2
index 654f525fb437..edd6817d5356 100644
@@ -18,7 +18,7 @@
  * differs in spirit from the above ffz (man ffs).
  */
 
-static inline int generic_ffs(int x)
+static inline int generic_ffs(unsigned int x)
 {
     int r = 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
Reviewed-by on v2 2/2
#3 lore raw

Re: [PATCH v2 2/2] xen: Change parameter of generic_{fls,ffs}() to unsigned int

Quoted 15 lines · Michal Orzel

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

Quoted 7 lines

To keep consistency between the helpers, take the opportunity to:
- replace __inline__ with inline,
- modify generic_ffs() to take parameter of type unsigned int as well (currently no user and the only wrapper generic_ffsl() passes unsigned long).

Reviewed-by on v2 2/2
#4 lore raw

Re: [PATCH v2 2/2] xen: Change parameter of generic_{fls,ffs}() to unsigned int

In reply to Michal Orzel

Hi Michal,

Quoted 17 lines · Michal Orzel

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

Quoted 7 lines

To keep consistency between the helpers, take the opportunity to:
- replace __inline__ with inline,
- modify generic_ffs() to take parameter of type unsigned int as well (currently no user and the only wrapper generic_ffsl() passes unsigned long).

Kind regards,
Henry