Skip to contentSkip to conversation

scsi: scsi_debug: remove a redundant assignment to variable ret

PATCH In review Colin Ian King 3 participants 6 messages Oct 2, 2024 – Oct 17, 2024

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

PATCH 1/1 scsi: scsi_debug: remove a redundant assignment to variable ret

The variable ret is being assigned a value that is never read, the following break statement exits the loop where ret is being re-assigned a new value. Remove the redundant assignment.

---
 drivers/scsi/scsi_debug.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/scsi/scsi_debug.c +1 -3
index d95f417e24c0..7c60f5acc4a3 100644
@@ -3686,14 +3686,12 @@ static int do_device_access(struct sdeb_store_info *sip, struct scsi_cmnd *scp,
 		sdeb_data_sector_lock(sip, do_write);
 		ret = sg_copy_buffer(sdb->table.sgl, sdb->table.nents,
 		   fsp + (block * sdebug_sector_size),
 		   sdebug_sector_size, sg_skip, do_write);
 		sdeb_data_sector_unlock(sip, do_write);
-		if (ret != sdebug_sector_size) {
-			ret += (i * sdebug_sector_size);
+		if (ret != sdebug_sector_size)
 			break;
-		}
 		sg_skip += sdebug_sector_size;
 		if (++block >= sdebug_store_sectors)
 			block = 0;
 	}
 	ret = num * sdebug_sector_size;
Signature · 1 line
2.39.5
#1 lore raw
Quoted 14 lines · Colin Ian King

The variable ret is being assigned a value that is never read, the following break statement exits the loop where ret is being re-assigned a new value. Remove the redundant assignment.

---
 drivers/scsi/scsi_debug.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/scsi/scsi_debug.c +0 -0
index d95f417e24c0..7c60f5acc4a3 100644

drivers/scsi/scsi_debug.c static int do_device_access(struct sdeb_store_info *sip, struct scsi_cmnd *scp, · line 3687

Quoted 2 lines
drivers/scsi/scsi_debug.c +0 -0
@@ -3686,14 +3686,12 @@ static int do_device_access(struct sdeb_store_info *sip, struct scsi_cmnd *scp,
 		sdeb_data_sector_lock(sip, do_write);
 		ret = sg_copy_buffer(sdb->table.sgl, sdb->table.nents,

You would think there would be a:

	total += ret;

here.

drivers/scsi/scsi_debug.c static int do_device_access(struct sdeb_store_info *sip, struct scsi_cmnd *scp, · line 3697

Quoted 13 lines
drivers/scsi/scsi_debug.c +1 -3
 		   fsp + (block * sdebug_sector_size),
 		   sdebug_sector_size, sg_skip, do_write);
 		sdeb_data_sector_unlock(sip, do_write);
-		if (ret != sdebug_sector_size) {
-			ret += (i * sdebug_sector_size);
+		if (ret != sdebug_sector_size)
 			break;
-		}
 		sg_skip += sdebug_sector_size;
 		if (++block >= sdebug_store_sectors)
 			block = 0;
 	}
 	ret = num * sdebug_sector_size;
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

And that this would be a "return total;"

The comment at the start of the function says that it's supposed to return the actual number of bytes that were copied. And you can see how that was the intention.

But what it actually does is it always reports that it copied the complete number of bytes. #Success #Woohoo

I wouldn't feel comfortable changing it to report partial copies without testing it. Someone needs to look at it more carefully to figure out what the correct fix is.

regards,
dan carpenter

#2 lore raw

Generally, functions which report the number of bytes copied are an anti-pattern.

	bytes = copied();
	if (bytes < 0)
		return bytes;  <-- forgot to handle partial copies

	bytes = copied();
	if (bytes < 0 || bytes != total)
		return bytes;  <-- forgot the error code for partial copies

	bytes = copied();
	if (bytes < sizeof())  <-- negative error codes type promoted to positive
		return -EIO;

We've seen subsystems move away from this. Other subsystems are like "Ugh. Updating all the callers is a headache. Let's either report that everything copied or a negative error code, but not the partial bytes." Now the first two examples above magically work.

regards,
dan carpenter

#3 lore raw
In reply to Dan Carpenter
Quoted 37 lines · Dan Carpenter
Quoted 14 lines · Colin Ian King

The variable ret is being assigned a value that is never read, the following break statement exits the loop where ret is being re-assigned a new value. Remove the redundant assignment.

---
  drivers/scsi/scsi_debug.c | 4 +---
  1 file changed, 1 insertion(+), 3 deletions(-)
drivers/scsi/scsi_debug.c +0 -0
index d95f417e24c0..7c60f5acc4a3 100644

drivers/scsi/scsi_debug.c static int do_device_access(struct sdeb_store_info *sip, struct scsi_cmnd *scp, · line 3687

Quoted 2 lines
drivers/scsi/scsi_debug.c +0 -0
@@ -3686,14 +3686,12 @@ static int do_device_access(struct sdeb_store_info *sip, struct scsi_cmnd *scp,
  		sdeb_data_sector_lock(sip, do_write);
  		ret = sg_copy_buffer(sdb->table.sgl, sdb->table.nents,

You would think there would be a:

	total += ret;

here.

drivers/scsi/scsi_debug.c static int do_device_access(struct sdeb_store_info *sip, struct scsi_cmnd *scp, · line 3697

Quoted 13 lines
drivers/scsi/scsi_debug.c +1 -3
  		   fsp + (block * sdebug_sector_size),
  		   sdebug_sector_size, sg_skip, do_write);T
  		sdeb_data_sector_unlock(sip, do_write);
-		if (ret != sdebug_sector_size) {
-			ret += (i * sdebug_sector_size);
+		if (ret != sdebug_sector_size)
  			break;
-		}
  		sg_skip += sdebug_sector_size;
  		if (++block >= sdebug_store_sectors)
  			block = 0;
  	}
  	ret = num * sdebug_sector_size;
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Quoted 1 line

And that this would be a "return total;"

Right, the function is currently a little messy as there is no variable for "total", and we re-assign ret per loop.

So I think that we can either:
a. introduce a variable to hold "total"
b. this change:

drivers/scsi/scsi_debug.c +0 -0
index af5e3a7f47a9..39218ffc6a31 100644
@@ -3690,13 +3690,14 @@ static int do_device_access(struct

sdeb_store_info *sip, struct scsi_cmnd *scp,

               sdeb_data_sector_unlock(sip, do_write);
               if (ret != sdebug_sector_size) {
                       ret += (i * sdebug_sector_size);
-                       break;
+                       goto out_unlock;
               }
               sg_skip += sdebug_sector_size;
               if (++block >= sdebug_store_sectors)
                       block = 0;
       }
       ret = num * sdebug_sector_size;
+out_unlock:
       sdeb_data_unlock(sip, atomic);

Maybe a. is better, as b. is maintaining some messiness.

Thanks,
John

Quoted 11 lines

The comment at the start of the function says that it's supposed to return the actual number of bytes that were copied. And you can see how that was the intention.

But what it actually does is it always reports that it copied the complete number of bytes. #Success #Woohoo

I wouldn't feel comfortable changing it to report partial copies without testing it. Someone needs to look at it more carefully to figure out what the correct fix is.

#4 lore raw
Quoted 54 lines · John Garry
Quoted 24 lines

Review static int do_device_access(struct · line 3688

Quoted 5 lines

scsi_debug.c

(quoted diff) +0 -0
@@ -3686,14 +3686,12 @@ static int do_device_access(struct 
 sdeb_store_info *sip, struct scsi_cmnd *scp,
           sdeb_data_sector_lock(sip, do_write);
           ret = sg_copy_buffer(sdb->table.sgl, sdb->table.nents,

You would think there would be a:

    total += ret;

here.

Quoted 5 lines
(quoted diff) +0 -0
              fsp + (block * sdebug_sector_size),

             sdebug_sector_size, sg_skip, do_write);T sdeb_data_sector_unlock(sip, do_write); -        if (ret != sdebug_sector_size) {
-            ret += (i * sdebug_sector_size);

Quoted 8 lines

+        if (ret != sdebug_sector_size)
              break;
-        }
          sg_skip += sdebug_sector_size;
          if (++block >= sdebug_store_sectors)
              block = 0;
      }
      ret = num * sdebug_sector_size;

Quoted 2 lines

         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
And that this would be a "return total;"

Right, the function is currently a little messy as there is no variable for "total", and we re-assign ret per loop.

So I think that we can either:
a. introduce a variable to hold "total"
b. this change:

drivers/scsi/scsi_debug.c +2 -1
index af5e3a7f47a9..39218ffc6a31 100644
@@ -3690,13 +3690,14 @@ static int do_device_access(struct
 sdeb_store_info *sip, struct scsi_cmnd *scp,
                sdeb_data_sector_unlock(sip, do_write);
                if (ret != sdebug_sector_size) {
                        ret += (i * sdebug_sector_size);
-                       break;
+                       goto out_unlock;
                }
                sg_skip += sdebug_sector_size;
                if (++block >= sdebug_store_sectors)
                        block = 0;
        }
        ret = num * sdebug_sector_size;
+out_unlock:
        sdeb_data_unlock(sip, atomic);
 
Quoted 1 line

Maybe a. is better, as b. is maintaining some messiness.

BTW, let me know if you are happy for me to send a patch to fix this.

Thanks!

#5 lore raw
In reply to John Garry
Quoted 68 lines · John Garry
Quoted 37 lines · Dan Carpenter
Quoted 14 lines · Colin Ian King

The variable ret is being assigned a value that is never read, the following break statement exits the loop where ret is being re-assigned a new value. Remove the redundant assignment.

---
  drivers/scsi/scsi_debug.c | 4 +---
  1 file changed, 1 insertion(+), 3 deletions(-)
drivers/scsi/scsi_debug.c +0 -0
index d95f417e24c0..7c60f5acc4a3 100644

drivers/scsi/scsi_debug.c static int do_device_access(struct sdeb_store_info *sip, struct scsi_cmnd *scp, · line 3687

Quoted 2 lines
drivers/scsi/scsi_debug.c +0 -0
@@ -3686,14 +3686,12 @@ static int do_device_access(struct sdeb_store_info *sip, struct scsi_cmnd *scp,
  		sdeb_data_sector_lock(sip, do_write);
  		ret = sg_copy_buffer(sdb->table.sgl, sdb->table.nents,

You would think there would be a:

	total += ret;

here.

drivers/scsi/scsi_debug.c static int do_device_access(struct sdeb_store_info *sip, struct scsi_cmnd *scp, · line 3697

Quoted 13 lines
drivers/scsi/scsi_debug.c +1 -3
  		   fsp + (block * sdebug_sector_size),
  		   sdebug_sector_size, sg_skip, do_write);T
  		sdeb_data_sector_unlock(sip, do_write);
-		if (ret != sdebug_sector_size) {
-			ret += (i * sdebug_sector_size);
+		if (ret != sdebug_sector_size)
  			break;
-		}
  		sg_skip += sdebug_sector_size;
  		if (++block >= sdebug_store_sectors)
  			block = 0;
  	}
  	ret = num * sdebug_sector_size;
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Quoted 1 line

And that this would be a "return total;"

Right, the function is currently a little messy as there is no variable for "total", and we re-assign ret per loop.

So I think that we can either:
a. introduce a variable to hold "total"
b. this change:

drivers/scsi/scsi_debug.c +2 -1
index af5e3a7f47a9..39218ffc6a31 100644
@@ -3690,13 +3690,14 @@ static int do_device_access(struct
 sdeb_store_info *sip, struct scsi_cmnd *scp,
               sdeb_data_sector_unlock(sip, do_write);
               if (ret != sdebug_sector_size) {
                       ret += (i * sdebug_sector_size);
-                       break;
+                       goto out_unlock;
               }
               sg_skip += sdebug_sector_size;
               if (++block >= sdebug_store_sectors)
                       block = 0;
       }
       ret = num * sdebug_sector_size;
+out_unlock:
       sdeb_data_unlock(sip, atomic);
 
Quoted 2 lines

Maybe a. is better, as b. is maintaining some messiness.

I'm happy with option a.

regards,
dan carpenter