[PATCH] xen/sched: resolve MISRA R18.3 pointer comparison

Dmytro Prokopchuk1 posted 1 patch 1 week, 6 days ago
xen/common/sched/core.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] xen/sched: resolve MISRA R18.3 pointer comparison
Posted by Dmytro Prokopchuk1 1 week, 6 days ago
MISRA C Rule 18.3 states: "The relational operators >, >=, < and <=
shall not be applied to objects of pointer type except where they
point into the same object".

sched_spin_lock_double() takes the lock at the lower address first.
The two spinlock_t pointers are not required to point into the same
object, so compare the addresses as integers.

unsigned long is the right integer type here. It is pointer-sized on
every architecture Xen supports, so the cast keeps the full address,
and an unsigned comparison still means "lower address first". A signed
long would interpret an address with the high bit set as negative, so
the order would no longer follow that rule.

No functional change.

Signed-off-by: Dmytro Prokopchuk <dmytro_prokopchuk1@epam.com>
---
Test CI pipeline:
https://gitlab.com/xen-project/people/dimaprkp4k/xen/-/pipelines/3048801945
---
 xen/common/sched/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/xen/common/sched/core.c b/xen/common/sched/core.c
index 88ae40f954..10b6eecccd 100644
--- a/xen/common/sched/core.c
+++ b/xen/common/sched/core.c
@@ -377,7 +377,7 @@ static always_inline void sched_spin_lock_double(
     {
         *flags = _spin_lock_irqsave(lock1);
     }
-    else if ( lock1 < lock2 )
+    else if ( (unsigned long)lock1 < (unsigned long)lock2 )
     {
         *flags = _spin_lock_irqsave(lock1);
         _spin_lock(lock2);
-- 
2.43.0
Re: [PATCH] xen/sched: resolve MISRA R18.3 pointer comparison
Posted by Jürgen Groß 1 week, 6 days ago
On 10.11.26 10:59, Dmytro Prokopchuk1 wrote:
> MISRA C Rule 18.3 states: "The relational operators >, >=, < and <=
> shall not be applied to objects of pointer type except where they
> point into the same object".
> 
> sched_spin_lock_double() takes the lock at the lower address first.
> The two spinlock_t pointers are not required to point into the same
> object, so compare the addresses as integers.
> 
> unsigned long is the right integer type here. It is pointer-sized on
> every architecture Xen supports, so the cast keeps the full address,
> and an unsigned comparison still means "lower address first". A signed
> long would interpret an address with the high bit set as negative, so
> the order would no longer follow that rule.
> 
> No functional change.
> 
> Signed-off-by: Dmytro Prokopchuk <dmytro_prokopchuk1@epam.com>
Reviewed-by: Juergen Gross <jgross@suse.com>


Juergen