Skip to contentSkip to conversation

scsi: ibmvscsi: Don't use rc uninitialized in ibmvscsi_do_work

PATCH Superseded Nathan Chancellor 5 participants 13 messages May 31, 2019 – Jun 5, 2019

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scsi: ibmvscsi: Don't use rc uninitialized in ibmvscsi_do_work

clang warns:

drivers/scsi/ibmvscsi/ibmvscsi.c:2126:7: warning: variable 'rc' is used uninitialized whenever switch case is taken [-Wsometimes-uninitialized]

        case IBMVSCSI_HOST_ACTION_NONE:
             ^~~~~~~~~~~~~~~~~~~~~~~~~
drivers/scsi/ibmvscsi/ibmvscsi.c:2151:6: note: uninitialized use occurs
here
        if (rc) {
            ^~

Initialize rc to zero so that the atomic_set and dev_err statement don't trigger for the cases that just break.

Fixes 035a3c4046b5 ("scsi: ibmvscsi: redo driver work thread to use enum action states")
---
 drivers/scsi/ibmvscsi/ibmvscsi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/scsi/ibmvscsi/ibmvscsi.c +1 -1
index 727c31dc11a0..6714d8043e62 100644
@@ -2118,7 +2118,7 @@ static unsigned long ibmvscsi_get_desired_dma(struct vio_dev *vdev)
 static void ibmvscsi_do_work(struct ibmvscsi_host_data *hostdata)
 {
 	unsigned long flags;
-	int rc;
+	int rc = 0;
 	char *action = "reset";
 
 	spin_lock_irqsave(hostdata->host->host_lock, flags);
Signature · 1 line
2.22.0.rc2
#1 lore raw

Hi Nathan,

Quoted 26 lines · Nathan Chancellor

clang warns:

drivers/scsi/ibmvscsi/ibmvscsi.c:2126:7: warning: variable 'rc' is used uninitialized whenever switch case is taken [-Wsometimes-uninitialized]

        case IBMVSCSI_HOST_ACTION_NONE:
             ^~~~~~~~~~~~~~~~~~~~~~~~~
drivers/scsi/ibmvscsi/ibmvscsi.c:2151:6: note: uninitialized use occurs
here
        if (rc) {
            ^~

Initialize rc to zero so that the atomic_set and dev_err statement don't trigger for the cases that just break.

Fixes 035a3c4046b5 ("scsi: ibmvscsi: redo driver work thread to use enum action states")
---
 drivers/scsi/ibmvscsi/ibmvscsi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/scsi/ibmvscsi/ibmvscsi.c +0 -0
index 727c31dc11a0..6714d8043e62 100644

drivers/scsi/ibmvscsi/ibmvscsi.c static unsigned long ibmvscsi_get_desired_dma(struct vio_dev *vdev) · line 2124

Quoted 8 lines
drivers/scsi/ibmvscsi/ibmvscsi.c +1 -1
@@ -2118,7 +2118,7 @@ static unsigned long ibmvscsi_get_desired_dma(struct vio_dev *vdev)
 static void ibmvscsi_do_work(struct ibmvscsi_host_data *hostdata)
 {
 	unsigned long flags;
-	int rc;
+	int rc = 0;
 	char *action = "reset";
 
 	spin_lock_irqsave(hostdata->host->host_lock, flags);

It's always preferable IMHO to keep any initialisation as localised as possible, so that the compiler can continue to warn about uninitialised usages elsewhere. In this case that would mean doing the rc = 0 in the switch, something like:

drivers/scsi/ibmvscsi/ibmvscsi.c +3 -3
index 727c31dc11a0..7ee5755cf636 100644
@@ -2123,9 +2123,6 @@ static void ibmvscsi_do_work(struct ibmvscsi_host_data *hostdata)
 
        spin_lock_irqsave(hostdata->host->host_lock, flags);
        switch (hostdata->action) {
-       case IBMVSCSI_HOST_ACTION_NONE:
-       case IBMVSCSI_HOST_ACTION_UNBLOCK:
-               break;
        case IBMVSCSI_HOST_ACTION_RESET:
                spin_unlock_irqrestore(hostdata->host->host_lock, flags);
                rc = ibmvscsi_reset_crq_queue(&hostdata->queue, hostdata);
@@ -2142,7 +2139,10 @@ static void ibmvscsi_do_work(struct ibmvscsi_host_data *hostdata)
                if (!rc)
                        rc = ibmvscsi_send_crq(hostdata, 0xC001000000000000LL, 0);
                break;
+       case IBMVSCSI_HOST_ACTION_NONE:
+       case IBMVSCSI_HOST_ACTION_UNBLOCK:
        default:
+               rc = 0;
                break;
        }
 

But then that makes me wonder if that's actually correct?

If we get an action that we don't recognise should we just throw it away like that? (by doing hostdata->action = IBMVSCSI_HOST_ACTION_NONE). Tyrel?

cheers

#2 lore raw

Hi Michael,

Quoted 6 lines · Michael Ellerman

Hi Nathan,

It's always preferable IMHO to keep any initialisation as localised as possible, so that the compiler can continue to warn about uninitialised usages elsewhere. In this case that would mean doing the rc = 0 in the switch, something like:

I am certainly okay with implementing this in a v2. I mulled over which would be preferred, I suppose I guessed wrong :) Thank you for the review and input.

Quoted 27 lines
drivers/scsi/ibmvscsi/ibmvscsi.c +3 -3
index 727c31dc11a0..7ee5755cf636 100644
@@ -2123,9 +2123,6 @@ static void ibmvscsi_do_work(struct ibmvscsi_host_data *hostdata)
 
        spin_lock_irqsave(hostdata->host->host_lock, flags);
        switch (hostdata->action) {
-       case IBMVSCSI_HOST_ACTION_NONE:
-       case IBMVSCSI_HOST_ACTION_UNBLOCK:
-               break;
        case IBMVSCSI_HOST_ACTION_RESET:
                spin_unlock_irqrestore(hostdata->host->host_lock, flags);
                rc = ibmvscsi_reset_crq_queue(&hostdata->queue, hostdata);
@@ -2142,7 +2139,10 @@ static void ibmvscsi_do_work(struct ibmvscsi_host_data *hostdata)
                if (!rc)
                        rc = ibmvscsi_send_crq(hostdata, 0xC001000000000000LL, 0);
                break;
+       case IBMVSCSI_HOST_ACTION_NONE:
+       case IBMVSCSI_HOST_ACTION_UNBLOCK:
        default:
+               rc = 0;
                break;
        }
 
Quoted 4 lines

But then that makes me wonder if that's actually correct?

If we get an action that we don't recognise should we just throw it away like that? (by doing hostdata->action = IBMVSCSI_HOST_ACTION_NONE). Tyrel?

However, because of this, I will hold off on v2 until Tyrel can give some feedback.

Thanks,
Nathan

#3 lore raw
In reply to Michael Ellerman
Quoted 69 lines · Michael Ellerman

Hi Nathan,

Quoted 26 lines · Nathan Chancellor

clang warns:

drivers/scsi/ibmvscsi/ibmvscsi.c:2126:7: warning: variable 'rc' is used uninitialized whenever switch case is taken [-Wsometimes-uninitialized]

        case IBMVSCSI_HOST_ACTION_NONE:
             ^~~~~~~~~~~~~~~~~~~~~~~~~
drivers/scsi/ibmvscsi/ibmvscsi.c:2151:6: note: uninitialized use occurs
here
        if (rc) {
            ^~

Initialize rc to zero so that the atomic_set and dev_err statement don't trigger for the cases that just break.

Fixes 035a3c4046b5 ("scsi: ibmvscsi: redo driver work thread to use enum action states")
---
 drivers/scsi/ibmvscsi/ibmvscsi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/scsi/ibmvscsi/ibmvscsi.c +0 -0
index 727c31dc11a0..6714d8043e62 100644

drivers/scsi/ibmvscsi/ibmvscsi.c static unsigned long ibmvscsi_get_desired_dma(struct vio_dev *vdev) · line 2124

Quoted 8 lines
drivers/scsi/ibmvscsi/ibmvscsi.c +1 -1
@@ -2118,7 +2118,7 @@ static unsigned long ibmvscsi_get_desired_dma(struct vio_dev *vdev)
 static void ibmvscsi_do_work(struct ibmvscsi_host_data *hostdata)
 {
 	unsigned long flags;
-	int rc;
+	int rc = 0;
 	char *action = "reset";
 
 	spin_lock_irqsave(hostdata->host->host_lock, flags);

It's always preferable IMHO to keep any initialisation as localised as possible, so that the compiler can continue to warn about uninitialised usages elsewhere. In this case that would mean doing the rc = 0 in the switch, something like:

drivers/scsi/ibmvscsi/ibmvscsi.c +3 -3
index 727c31dc11a0..7ee5755cf636 100644
@@ -2123,9 +2123,6 @@ static void ibmvscsi_do_work(struct ibmvscsi_host_data *hostdata)
 
        spin_lock_irqsave(hostdata->host->host_lock, flags);
        switch (hostdata->action) {
-       case IBMVSCSI_HOST_ACTION_NONE:
-       case IBMVSCSI_HOST_ACTION_UNBLOCK:
-               break;
        case IBMVSCSI_HOST_ACTION_RESET:
                spin_unlock_irqrestore(hostdata->host->host_lock, flags);
                rc = ibmvscsi_reset_crq_queue(&hostdata->queue, hostdata);
@@ -2142,7 +2139,10 @@ static void ibmvscsi_do_work(struct ibmvscsi_host_data *hostdata)
                if (!rc)
                        rc = ibmvscsi_send_crq(hostdata, 0xC001000000000000LL, 0);
                break;
+       case IBMVSCSI_HOST_ACTION_NONE:
+       case IBMVSCSI_HOST_ACTION_UNBLOCK:
        default:
+               rc = 0;
                break;
        }
 
Quoted 4 lines

But then that makes me wonder if that's actually correct?

If we get an action that we don't recognise should we just throw it away like that? (by doing hostdata->action = IBMVSCSI_HOST_ACTION_NONE). Tyrel?

On initial pass I was ok with this, but after thinking on it I think it is more subtle.

The right approach is to set rc = 0 for HOST_ACTION_UNBLOCK as we want to fall through. For HOST_ACTION_NONE and default we need to return directly from the function.

-Tyrel

Quoted 3 lines

cheers

#4 lore raw
In reply to Nathan Chancellor

Le 31/05/2019 à 20:53, Nathan Chancellor a écrit :

Quoted 26 lines

clang warns:

drivers/scsi/ibmvscsi/ibmvscsi.c:2126:7: warning: variable 'rc' is used uninitialized whenever switch case is taken [-Wsometimes-uninitialized]

         case IBMVSCSI_HOST_ACTION_NONE:
              ^~~~~~~~~~~~~~~~~~~~~~~~~
drivers/scsi/ibmvscsi/ibmvscsi.c:2151:6: note: uninitialized use occurs
here
         if (rc) {
             ^~

Initialize rc to zero so that the atomic_set and dev_err statement don't trigger for the cases that just break.

Fixes 035a3c4046b5 ("scsi: ibmvscsi: redo driver work thread to use enum action states")
---
  drivers/scsi/ibmvscsi/ibmvscsi.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/scsi/ibmvscsi/ibmvscsi.c +0 -0
index 727c31dc11a0..6714d8043e62 100644

drivers/scsi/ibmvscsi/ibmvscsi.c static unsigned long ibmvscsi_get_desired_dma(struct vio_dev *vdev) · line 2121

Quoted 5 lines
drivers/scsi/ibmvscsi/ibmvscsi.c +1 -1
@@ -2118,7 +2118,7 @@ static unsigned long ibmvscsi_get_desired_dma(struct vio_dev *vdev)
  static void ibmvscsi_do_work(struct ibmvscsi_host_data *hostdata)
  {
  	unsigned long flags;
-	int rc;
+	int rc = 0;

I don't think the above is the best solution, as it hides the warning instead of really fixing it.

Your problem is that some legs of the switch are missing setting the value of rc, it would therefore be better to fix the legs instead of setting a default value which may not be correct for every case, allthough it may be at the time being.

Christophe

Quoted 4 lines
drivers/scsi/ibmvscsi/ibmvscsi.c +0 -0
  	char *action = "reset";
 
  	spin_lock_irqsave(hostdata->host->host_lock, flags);
#5 lore raw

PATCH v2 1/1 scsi: ibmvscsi: Don't use rc uninitialized in ibmvscsi_do_work

In reply to Nathan Chancellor

clang warns:

drivers/scsi/ibmvscsi/ibmvscsi.c:2126:7: warning: variable 'rc' is used uninitialized whenever switch case is taken [-Wsometimes-uninitialized]

        case IBMVSCSI_HOST_ACTION_NONE:
             ^~~~~~~~~~~~~~~~~~~~~~~~~
drivers/scsi/ibmvscsi/ibmvscsi.c:2151:6: note: uninitialized use occurs
here
        if (rc) {
            ^~

Initialize rc to zero in the case statements that clang mentions so that the atomic_set and dev_err statement don't trigger for them.

Fixes 035a3c4046b5 ("scsi: ibmvscsi: redo driver work thread to use enum action states")

---

v1 -> v2:

* Initialize rc in the case statements, rather than at the top of the function, as suggested by Michael.

 drivers/scsi/ibmvscsi/ibmvscsi.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/scsi/ibmvscsi/ibmvscsi.c +3 -3
index 65053daef5f7..3b5647d622d9 100644
@@ -2109,9 +2109,6 @@ static void ibmvscsi_do_work(struct ibmvscsi_host_data *hostdata)
 
 	spin_lock_irqsave(hostdata->host->host_lock, flags);
 	switch (hostdata->action) {
-	case IBMVSCSI_HOST_ACTION_NONE:
-	case IBMVSCSI_HOST_ACTION_UNBLOCK:
-		break;
 	case IBMVSCSI_HOST_ACTION_RESET:
 		spin_unlock_irqrestore(hostdata->host->host_lock, flags);
 		rc = ibmvscsi_reset_crq_queue(&hostdata->queue, hostdata);
@@ -2128,7 +2125,10 @@ static void ibmvscsi_do_work(struct ibmvscsi_host_data *hostdata)
 		if (!rc)
 			rc = ibmvscsi_send_crq(hostdata, 0xC001000000000000LL, 0);
 		break;
+	case IBMVSCSI_HOST_ACTION_NONE:
+	case IBMVSCSI_HOST_ACTION_UNBLOCK:
 	default:
+		rc = 0;
 		break;
 	}
 
Signature · 1 line
2.22.0.rc3
Acked-by on v2 1/1
#6 lore raw
Quoted 11 lines · Nathan Chancellor

clang warns:

drivers/scsi/ibmvscsi/ibmvscsi.c:2126:7: warning: variable 'rc' is used uninitialized whenever switch case is taken [-Wsometimes-uninitialized]

        case IBMVSCSI_HOST_ACTION_NONE:
             ^~~~~~~~~~~~~~~~~~~~~~~~~
drivers/scsi/ibmvscsi/ibmvscsi.c:2151:6: note: uninitialized use occurs
here
        if (rc) {
            ^~
Quoted 7 lines

Initialize rc to zero in the case statements that clang mentions so that the atomic_set and dev_err statement don't trigger for them.

Fixes 035a3c4046b5 ("scsi: ibmvscsi: redo driver work thread to use enum action states")
#7 lore raw
Quoted 15 lines · Tyrel Datwyler
Quoted 14 lines · Nathan Chancellor

clang warns:

drivers/scsi/ibmvscsi/ibmvscsi.c:2126:7: warning: variable 'rc' is used uninitialized whenever switch case is taken [-Wsometimes-uninitialized]

        case IBMVSCSI_HOST_ACTION_NONE:
             ^~~~~~~~~~~~~~~~~~~~~~~~~
drivers/scsi/ibmvscsi/ibmvscsi.c:2151:6: note: uninitialized use occurs
here
        if (rc) {
            ^~

Initialize rc to zero in the case statements that clang mentions so that the atomic_set and dev_err statement don't trigger for them.

Quoted 7 lines
Quoted 4 lines
Fixes 035a3c4046b5 ("scsi: ibmvscsi: redo driver work thread to use enum action states")

On second thought NACK. See my response to Michael earlier in the thread.

I think this is the better solution:

drivers/scsi/ibmvscsi/ibmvscsi.c +0 -0
index 727c31dc11a0..c3cf05dd8733 100644
@@ -2123,8 +2123,8 @@ static void ibmvscsi_do_work(struct ibmvscsi_host_data

*hostdata)

        spin_lock_irqsave(hostdata->host->host_lock, flags);
        switch (hostdata->action) {
-       case IBMVSCSI_HOST_ACTION_NONE:
        case IBMVSCSI_HOST_ACTION_UNBLOCK:
+               rc = 0;
                break;
        case IBMVSCSI_HOST_ACTION_RESET:
                spin_unlock_irqrestore(hostdata->host->host_lock, flags);
@@ -2142,8 +2142,9 @@ static void ibmvscsi_do_work(struct ibmvscsi_host_data
*hostdata)
                if (!rc)
                        rc = ibmvscsi_send_crq(hostdata, 0xC001000000000000LL, 0);
                break;
+       case IBMVSCSI_HOST_ACTION_NONE:
        default:
-               break;
+               return;
        }

        hostdata->action = IBMVSCSI_HOST_ACTION_NONE;
#8 lore raw
Quoted 44 lines · Tyrel Datwyler
Quoted 15 lines · Tyrel Datwyler
Quoted 14 lines · Nathan Chancellor

clang warns:

drivers/scsi/ibmvscsi/ibmvscsi.c:2126:7: warning: variable 'rc' is used uninitialized whenever switch case is taken [-Wsometimes-uninitialized]

        case IBMVSCSI_HOST_ACTION_NONE:
             ^~~~~~~~~~~~~~~~~~~~~~~~~
drivers/scsi/ibmvscsi/ibmvscsi.c:2151:6: note: uninitialized use occurs
here
        if (rc) {
            ^~

Initialize rc to zero in the case statements that clang mentions so that the atomic_set and dev_err statement don't trigger for them.

Quoted 7 lines
Quoted 4 lines
Fixes 035a3c4046b5 ("scsi: ibmvscsi: redo driver work thread to use enum action states")

On second thought NACK. See my response to Michael earlier in the thread.

I think this is the better solution:

drivers/scsi/ibmvscsi/ibmvscsi.c +1 -1
index 727c31dc11a0..c3cf05dd8733 100644
@@ -2123,8 +2123,8 @@ static void ibmvscsi_do_work(struct ibmvscsi_host_data
 *hostdata)
 
        spin_lock_irqsave(hostdata->host->host_lock, flags);
        switch (hostdata->action) {
-       case IBMVSCSI_HOST_ACTION_NONE:
        case IBMVSCSI_HOST_ACTION_UNBLOCK:
+               rc = 0;
                break;
        case IBMVSCSI_HOST_ACTION_RESET:
                spin_unlock_irqrestore(hostdata->host->host_lock, flags);

drivers/scsi/ibmvscsi/ibmvscsi.c static void ibmvscsi_do_work(struct ibmvscsi_host_data · line 2147

Quoted 7 lines
drivers/scsi/ibmvscsi/ibmvscsi.c +1 -1
@@ -2142,8 +2142,9 @@ static void ibmvscsi_do_work(struct ibmvscsi_host_data
 *hostdata)
                if (!rc)
                        rc = ibmvscsi_send_crq(hostdata, 0xC001000000000000LL, 0);
                break;
+       case IBMVSCSI_HOST_ACTION_NONE:
        default:
-               break;

Need a spin_unlock_irqrestore() here before the return.

-Tyrel

Quoted 5 lines
drivers/scsi/ibmvscsi/ibmvscsi.c +1 -0
+               return;
        }
 
        hostdata->action = IBMVSCSI_HOST_ACTION_NONE;
#9 lore raw

PATCH v3 1/1 scsi: ibmvscsi: Don't use rc uninitialized in ibmvscsi_do_work

In reply to Nathan Chancellor

clang warns:

drivers/scsi/ibmvscsi/ibmvscsi.c:2126:7: warning: variable 'rc' is used uninitialized whenever switch case is taken [-Wsometimes-uninitialized]

        case IBMVSCSI_HOST_ACTION_NONE:
             ^~~~~~~~~~~~~~~~~~~~~~~~~
drivers/scsi/ibmvscsi/ibmvscsi.c:2151:6: note: uninitialized use occurs
here
        if (rc) {
            ^~

Initialize rc in the IBMVSCSI_HOST_ACTION_UNBLOCK case statement then shuffle IBMVSCSI_HOST_ACTION_NONE down to the default case statement and make it return early so that rc is never used uninitialized in this function.

Fixes 035a3c4046b5 ("scsi: ibmvscsi: redo driver work thread to use enum action states")

---

v1 -> v2:

* Initialize rc in the case statements, rather than at the top of the function, as suggested by Michael.

v2 -> v3:

* default and IBMVSCSI_HOST_ACTION_NONE now return early from the function, as requested by Tyrel.

 drivers/scsi/ibmvscsi/ibmvscsi.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/scsi/ibmvscsi/ibmvscsi.c +4 -2
index 65053daef5f7..7f66a7783209 100644
@@ -2109,8 +2109,8 @@ static void ibmvscsi_do_work(struct ibmvscsi_host_data *hostdata)
 
 	spin_lock_irqsave(hostdata->host->host_lock, flags);
 	switch (hostdata->action) {
-	case IBMVSCSI_HOST_ACTION_NONE:
 	case IBMVSCSI_HOST_ACTION_UNBLOCK:
+		rc = 0;
 		break;
 	case IBMVSCSI_HOST_ACTION_RESET:
 		spin_unlock_irqrestore(hostdata->host->host_lock, flags);
@@ -2128,8 +2128,10 @@ static void ibmvscsi_do_work(struct ibmvscsi_host_data *hostdata)
 		if (!rc)
 			rc = ibmvscsi_send_crq(hostdata, 0xC001000000000000LL, 0);
 		break;
+	case IBMVSCSI_HOST_ACTION_NONE:
 	default:
-		break;
+		spin_unlock_irqrestore(hostdata->host->host_lock, flags);
+		return;
 	}
 
 	hostdata->action = IBMVSCSI_HOST_ACTION_NONE;
Signature · 1 line
2.22.0.rc3
Acked-by on v3 1/1
#10 lore raw
Quoted 16 lines · Nathan Chancellor

clang warns:

drivers/scsi/ibmvscsi/ibmvscsi.c:2126:7: warning: variable 'rc' is used uninitialized whenever switch case is taken [-Wsometimes-uninitialized]

        case IBMVSCSI_HOST_ACTION_NONE:
             ^~~~~~~~~~~~~~~~~~~~~~~~~
drivers/scsi/ibmvscsi/ibmvscsi.c:2151:6: note: uninitialized use occurs
here
        if (rc) {
            ^~

Initialize rc in the IBMVSCSI_HOST_ACTION_UNBLOCK case statement then shuffle IBMVSCSI_HOST_ACTION_NONE down to the default case statement and make it return early so that rc is never used uninitialized in this function.

Quoted 5 lines
Fixes 035a3c4046b5 ("scsi: ibmvscsi: redo driver work thread to use enum action states")
#11 lore raw
In reply to Nathan Chancellor

Nathan,

Quoted 6 lines

clang warns:

drivers/scsi/ibmvscsi/ibmvscsi.c:2126:7: warning: variable 'rc' is used uninitialized whenever switch case is taken [-Wsometimes-uninitialized]

        case IBMVSCSI_HOST_ACTION_NONE:
             ^~~~~~~~~~~~~~~~~~~~~~~~~

Applied to 5.3/scsi-queue, thanks!

Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering
#12 lore raw
Quoted 10 lines · Martin K. Petersen

Nathan,

Quoted 6 lines

clang warns:

drivers/scsi/ibmvscsi/ibmvscsi.c:2126:7: warning: variable 'rc' is used uninitialized whenever switch case is taken [-Wsometimes-uninitialized]

        case IBMVSCSI_HOST_ACTION_NONE:
             ^~~~~~~~~~~~~~~~~~~~~~~~~

Applied to 5.3/scsi-queue, thanks!

Thanks all.

cheers