Skip to contentSkip to conversation

Minor Coverity fixes

PATCH In review Artem Mygaiev 3 participants 8 messages Oct 9, 2019 – Jan 18, 2020

lore
Share & tools
lore
#0 lore raw

cover Minor Coverity fixes

From: Artem Mygaiev <[email protected]>

There is a big amount of insignificant or false positives reported by Coverity, fixing some of them can at least make code more consistent or at least bit more readable.

Artem Mygaiev (3):
Consistent use for lock variables
Remove useless ASSERT condition
Free allocated resource on error

 xen/drivers/char/scif-uart.c       | 2 +-
 xen/drivers/passthrough/arm/smmu.c | 8 +++++---
 xen/xsm/flask/avc.c                | 2 +-
 3 files changed, 7 insertions(+), 5 deletions(-)
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw

PATCH 1/3 Consistent use for lock variable

... for both lock and unlock

Coverity-ID: 1381840

---
 xen/xsm/flask/avc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/xsm/flask/avc.c +1 -1
index 87ea38b7a0..3a9507f62a 100644
@@ -320,7 +320,7 @@ static inline int avc_reclaim_node(void)
         head = &avc_cache.slots[hvalue];
         lock = &avc_cache.slots_lock[hvalue];
 
-        spin_lock_irqsave(&avc_cache.slots_lock[hvalue], flags);
+        spin_lock_irqsave(lock, flags);
         rcu_read_lock(&avc_rcu_lock);
         hlist_for_each_entry(node, next, head, list)
         {
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Acked-by on v1 1/3
#2 lore raw

Re: [Xen-devel] [PATCH 1/3] Consistent use for lock variable

Hi Artem,

Apologies for the late answer.

Quoted 1 line · Artem Mygaiev

... for both lock and unlock

I would suggest the following commit message:

xen/xsm: Use the same lock for lock and unlock

The function avc_reclaim_mode() is not using the same variable for locking and unlocking. While the underlying spinlock is the same, coverity will get confused and think the lock was not released.

Update the code to use the same variable for the lock and unlock part.

Quoted 3 lines

Coverity-ID: 1381840

We also need an hack from Daniel. @Daniel, are you happy with the change?

Quoted 10 lines
---
  xen/xsm/flask/avc.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
xen/xsm/flask/avc.c +0 -0
index 87ea38b7a0..3a9507f62a 100644

xen/xsm/flask/avc.c static inline int avc_reclaim_node(void) · line 326

Quoted 8 lines
xen/xsm/flask/avc.c +1 -1
@@ -320,7 +320,7 @@ static inline int avc_reclaim_node(void)
          head = &avc_cache.slots[hvalue];
          lock = &avc_cache.slots_lock[hvalue];
 
-        spin_lock_irqsave(&avc_cache.slots_lock[hvalue], flags);
+        spin_lock_irqsave(lock, flags);
          rcu_read_lock(&avc_rcu_lock);
          hlist_for_each_entry(node, next, head, list)
          {

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#3 lore raw

PATCH 2/3 Remove useless ASSERT condition

In reply to Artem Mygaiev

cnt is unsigned, so always >=0

Coverity-ID: 1381848

---
 xen/drivers/char/scif-uart.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/drivers/char/scif-uart.c +1 -1
index fa0b8274ca..9d3f66b55b 100644
@@ -205,7 +205,7 @@ static int scif_uart_tx_ready(struct serial_port *port)
 
      /* Check number of data bytes stored in TX FIFO */
     cnt = scif_readw(uart, SCIF_SCFDR) >> 8;
-    ASSERT( cnt >= 0 && cnt <= params->fifo_size );
+    ASSERT( cnt <= params->fifo_size );
 
     return (params->fifo_size - cnt);
 }
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Acked-by on v1 2/3
#4 lore raw

Re: [Xen-devel] [PATCH 2/3] Remove useless ASSERT condition

Hi Artem,

Quoted 4 lines · Artem Mygaiev

cnt is unsigned, so always >=0

Coverity-ID: 1381848

Quoted 10 lines
---
  xen/drivers/char/scif-uart.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
xen/drivers/char/scif-uart.c +0 -0
index fa0b8274ca..9d3f66b55b 100644

xen/drivers/char/scif-uart.c static int scif_uart_tx_ready(struct serial_port *port) · line 211

Quoted 8 lines
xen/drivers/char/scif-uart.c +1 -1
@@ -205,7 +205,7 @@ static int scif_uart_tx_ready(struct serial_port *port)
 
       /* Check number of data bytes stored in TX FIFO */
      cnt = scif_readw(uart, SCIF_SCFDR) >> 8;
-    ASSERT( cnt >= 0 && cnt <= params->fifo_size );
+    ASSERT( cnt <= params->fifo_size );
 
      return (params->fifo_size - cnt);
  }

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#5 lore raw

Re: [Xen-devel] [PATCH 2/3] Remove useless ASSERT condition

Quoted 1 line · Julien Grall

Hi Artem,

Hi Artem,

Quoted 7 lines · Artem Mygaiev
Quoted 4 lines

cnt is unsigned, so always >=0

Coverity-ID: 1381848

I was going through my todo list and noticed I have never committed this patch. Apologies for that.

I have committed it with a slight change in the commit title:

"xen/char: scif-uart: Remove useless ASSERT condition"

In the future, please try to add the component in the title :).

Cheers,

Quoted 21 lines
Quoted 10 lines

---
  xen/drivers/char/scif-uart.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
xen/drivers/char/scif-uart.c +0 -0
index fa0b8274ca..9d3f66b55b 100644

xen/drivers/char/scif-uart.c static int scif_uart_tx_ready(struct serial_port · line 211

Quoted 8 lines
xen/drivers/char/scif-uart.c +1 -1
@@ -205,7 +205,7 @@ static int scif_uart_tx_ready(struct serial_port 
 *port)
        /* Check number of data bytes stored in TX FIFO */
       cnt = scif_readw(uart, SCIF_SCFDR) >> 8;
-    ASSERT( cnt >= 0 && cnt <= params->fifo_size );
+    ASSERT( cnt <= params->fifo_size );
       return (params->fifo_size - cnt);
   }
 

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#6 lore raw

PATCH 3/3 Free allocated resource on error

In reply to Artem Mygaiev

Also do not set mark device as SMMU protected when there are no more SMMU resources left

Coverity-ID: 1381862

---
 xen/drivers/passthrough/arm/smmu.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
xen/drivers/passthrough/arm/smmu.c +5 -3
index f151b9f5b5..cf42335eed 100644
@@ -804,9 +804,6 @@ static int register_smmu_master(struct arm_smmu_device *smmu,
 	master->of_node			= masterspec->np;
 	master->cfg.num_streamids	= masterspec->args_count;
 
-	/* Xen: Let Xen know that the device is protected by an SMMU */
-	dt_device_set_protected(masterspec->np);
-
 	for (i = 0; i < master->cfg.num_streamids; ++i) {
 		u16 streamid = masterspec->args[i];
 
@@ -815,10 +812,15 @@ static int register_smmu_master(struct arm_smmu_device *smmu,
 			dev_err(dev,
 				"stream ID for master device %s greater than maximum allowed (%d)\n",
 				masterspec->np->name, smmu->num_mapping_groups);
+			devm_free(master);
 			return -ERANGE;
 		}
 		master->cfg.streamids[i] = streamid;
 	}
+
+        /* Xen: Let Xen know that the device is protected by an SMMU */
+        dt_device_set_protected(masterspec->np);
+
 	return insert_smmu_master(smmu, master);
 }
 
Signature · 7 lines
2.20.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#7 lore raw

Re: [Xen-devel] [PATCH 3/3] Free allocated resource on error

Hi Artem,

Quoted 2 lines · Artem Mygaiev

Also do not set mark device as SMMU protected when there are no more SMMU resources left

This is a sanity check on the content of the node, not a lack of resource in this case.

TBH, the current placement is probably not correct. But I am not convinced the new placement is better.

Xen 4.12 and earlier will ignore any failure and continue, so we cannot use this device at all. Indeed, Xen will configure the SMMU to deny any transaction. If you fail to initialize the device, then it will be in an unusable state. In this case, we don't want a domain (including Dom0) to use it at all. This could be achieved by calling dt_device_set_protected.

Xen 4.13 will stop booting if any of the SMMU fails to configure (this include Master Device cannot be assigned). So there are no difference there.

My preference is to cater for all Xen versions so we can consider the patch for a backport and potentially revert of the Xen 4.13 behavior (i.e. crashing when one IOMMU is not correctly setup). So we would need to move the call at the beginning of the function.

Quoted 23 lines

Coverity-ID: 1381862

---
  xen/drivers/passthrough/arm/smmu.c | 8 +++++---
  1 file changed, 5 insertions(+), 3 deletions(-)
xen/drivers/passthrough/arm/smmu.c +0 -3
index f151b9f5b5..cf42335eed 100644
@@ -804,9 +804,6 @@ static int register_smmu_master(struct arm_smmu_device *smmu,
  	master->of_node			= masterspec->np;
  	master->cfg.num_streamids	= masterspec->args_count;
 
-	/* Xen: Let Xen know that the device is protected by an SMMU */
-	dt_device_set_protected(masterspec->np);
-
  	for (i = 0; i < master->cfg.num_streamids; ++i) {
  		u16 streamid = masterspec->args[i];
 
@@ -815,10 +812,15 @@ static int register_smmu_master(struct arm_smmu_device *smmu,
  			dev_err(dev,

xen/drivers/passthrough/arm/smmu.c static int register_smmu_master(struct arm_smmu_device *smmu, · line 822

Quoted 10 lines
xen/drivers/passthrough/arm/smmu.c +4 -0
  				"stream ID for master device %s greater than maximum allowed (%d)\n",
  				masterspec->np->name, smmu->num_mapping_groups);
+			devm_free(master);
  			return -ERANGE;
  		}
  		master->cfg.streamids[i] = streamid;
  	}
+
+        /* Xen: Let Xen know that the device is protected by an SMMU */
+        dt_device_set_protected(masterspec->np);

This code is using Linux coding style, so it should be hard tab here.

xen/drivers/passthrough/arm/smmu.c static int register_smmu_master(struct arm_smmu_device *smmu, · line 826

Quoted 5 lines
xen/drivers/passthrough/arm/smmu.c +1 -0
+
  	return insert_smmu_master(smmu, master);
  }
 

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel