Skip to contentSkip to conversation

pflash: Avoid warnings from coverity

PATCH In review Stefan Weil 5 participants 10 messages Sep 19, 2012 – Sep 24, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pflash: Avoid warnings from coverity

hw/pflash_cfi01.c:209:
check_return: Calling function "bdrv_write" without checking return value (as is done elsewhere 35 out of 37 times).

hw/pflash_cfi02.c:144:
unterminated_default: The default case is not terminated by a 'break' statement.

hw/pflash_cfi02.c:238:
check_return: Calling function "bdrv_write" without checking return value (as is done elsewhere 35 out of 37 times).

---
 hw/pflash_cfi01.c |    6 ++++--
 hw/pflash_cfi02.c |    7 +++++--
 2 files changed, 9 insertions(+), 4 deletions(-)
hw/pflash_cfi01.c +4 -2
index 56ed33f..27ae4aa 100644
@@ -205,8 +205,10 @@ static void pflash_update(pflash_t *pfl, int offset,
         /* round to sectors */
         offset = offset >> 9;
         offset_end = (offset_end + 511) >> 9;
-        bdrv_write(pfl->bs, offset, pfl->storage + (offset << 9),
-                   offset_end - offset);
+        if (bdrv_write(pfl->bs, offset, pfl->storage + (offset << 9),
+                       offset_end - offset) == -1) {
+            fprintf(stderr, "pflash: Error writing to flash storage\n");
+        }
     }
 }
 
hw/pflash_cfi02.c +5 -2
index 3e2002e..bca7d1b 100644
@@ -147,6 +147,7 @@ static uint32_t pflash_read (pflash_t *pfl, target_phys_addr_t offset,
         DPRINTF("%s: unknown command state: %x\n", __func__, pfl->cmd);
         pfl->wcycle = 0;
         pfl->cmd = 0;
+        /* Fall through */
     case 0x80:
         /* We accept reads during second unlock sequence... */
     case 0x00:
@@ -236,8 +237,10 @@ static void pflash_update(pflash_t *pfl, int offset,
         /* round to sectors */
         offset = offset >> 9;
         offset_end = (offset_end + 511) >> 9;
-        bdrv_write(pfl->bs, offset, pfl->storage + (offset << 9),
-                   offset_end - offset);
+        if (bdrv_write(pfl->bs, offset, pfl->storage + (offset << 9),
+                       offset_end - offset) == -1) {
+            fprintf(stderr, "pflash: Error writing to flash storage\n");
+        }
     }
 }
 
Signature · 1 line
1.7.10
#1 lore raw
Quoted 20 lines · Stefan Weil

hw/pflash_cfi01.c:209:
check_return: Calling function "bdrv_write" without checking return value (as is done elsewhere 35 out of 37 times).

hw/pflash_cfi02.c:144:
unterminated_default: The default case is not terminated by a 'break' statement.

hw/pflash_cfi02.c:238:
check_return: Calling function "bdrv_write" without checking return value (as is done elsewhere 35 out of 37 times).

---
 hw/pflash_cfi01.c |    6 ++++--
 hw/pflash_cfi02.c |    7 +++++--
 2 files changed, 9 insertions(+), 4 deletions(-)
hw/pflash_cfi01.c +0 -0
index 56ed33f..27ae4aa 100644

hw/pflash_cfi01.c static void pflash_update(pflash_t *pfl, int offset, · line 211

Quoted 9 lines
hw/pflash_cfi01.c +4 -2
@@ -205,8 +205,10 @@ static void pflash_update(pflash_t *pfl, int offset,
         /* round to sectors */
         offset = offset >> 9;
         offset_end = (offset_end + 511) >> 9;
-        bdrv_write(pfl->bs, offset, pfl->storage + (offset << 9),
-                   offset_end - offset);
+        if (bdrv_write(pfl->bs, offset, pfl->storage + (offset << 9),
+                       offset_end - offset) == -1) {
+            fprintf(stderr, "pflash: Error writing to flash storage\n");
+        }

...shouldn't we be propagating the write error up to the guest? It feels like this change is just silencing the coverity error without actually fixing the underlying problem.

-- PMM

#2 lore raw

Am 19.09.2012 18:45, schrieb Peter Maydell:

Quoted 34 lines · Stefan Weil
Quoted 20 lines

hw/pflash_cfi01.c:209:
check_return: Calling function "bdrv_write" without checking return value (as is done elsewhere 35 out of 37 times).

hw/pflash_cfi02.c:144:
unterminated_default: The default case is not terminated by a 'break' statement.

hw/pflash_cfi02.c:238:
check_return: Calling function "bdrv_write" without checking return value (as is done elsewhere 35 out of 37 times).

---
  hw/pflash_cfi01.c |    6 ++++--
  hw/pflash_cfi02.c |    7 +++++--
  2 files changed, 9 insertions(+), 4 deletions(-)
hw/pflash_cfi01.c +0 -0
index 56ed33f..27ae4aa 100644

hw/pflash_cfi01.c static void pflash_update(pflash_t *pfl, int offset, · line 211

Quoted 9 lines
hw/pflash_cfi01.c +4 -2
@@ -205,8 +205,10 @@ static void pflash_update(pflash_t *pfl, int offset,
          /* round to sectors */
          offset = offset >> 9;
          offset_end = (offset_end + 511) >> 9;
-        bdrv_write(pfl->bs, offset, pfl->storage + (offset << 9),
-                   offset_end - offset);
+        if (bdrv_write(pfl->bs, offset, pfl->storage + (offset << 9),
+                       offset_end - offset) == -1) {
+            fprintf(stderr, "pflash: Error writing to flash storage\n");
+        }

...shouldn't we be propagating the write error up to the guest? It feels like this change is just silencing the coverity error without actually fixing the underlying problem.

Quoted 1 line

-- PMM

As far as I know, there is no concept how a flash memory can report a write error. Flash memory which cannot be written is unusable.

hw/nand.c uses a similar handling for write errors, but writes the error message to stdout (no good idea).

- Stefan

#3 lore raw
Quoted 8 lines · Stefan Weil

Am 19.09.2012 18:45, schrieb Peter Maydell:

Quoted 3 lines

...shouldn't we be propagating the write error up to the guest? It feels like this change is just silencing the coverity error without actually fixing the underlying problem.

As far as I know, there is no concept how a flash memory can report a write error. Flash memory which cannot be written is unusable.

Just looking at the callers of the pflash_update() function, they have cases where they set bits in the status register for errors, so there clearly is a mechanism for reporting errors back to the guest.

-- PMM

#4 lore raw
In reply to Stefan Weil
Quoted 20 lines · Stefan Weil

hw/pflash_cfi01.c:209:
check_return: Calling function "bdrv_write" without checking return value (as is done elsewhere 35 out of 37 times).

hw/pflash_cfi02.c:144:
unterminated_default: The default case is not terminated by a 'break' statement.

hw/pflash_cfi02.c:238:
check_return: Calling function "bdrv_write" without checking return value (as is done elsewhere 35 out of 37 times).

---
 hw/pflash_cfi01.c |    6 ++++--
 hw/pflash_cfi02.c |    7 +++++--
 2 files changed, 9 insertions(+), 4 deletions(-)
hw/pflash_cfi01.c +0 -0
index 56ed33f..27ae4aa 100644

hw/pflash_cfi01.c static void pflash_update(pflash_t *pfl, int offset, · line 209

Quoted 7 lines
hw/pflash_cfi01.c +2 -2
@@ -205,8 +205,10 @@ static void pflash_update(pflash_t *pfl, int offset,
         /* round to sectors */
         offset = offset >> 9;
         offset_end = (offset_end + 511) >> 9;
-        bdrv_write(pfl->bs, offset, pfl->storage + (offset << 9),
-                   offset_end - offset);
+        if (bdrv_write(pfl->bs, offset, pfl->storage + (offset << 9),
+                       offset_end - offset) == -1) {

bdrv_write() returns -errno, not -1.

hw/pflash_cfi01.c static void pflash_update(pflash_t *pfl, int offset, · line 211

Quoted 2 lines
hw/pflash_cfi01.c +2 -0
+            fprintf(stderr, "pflash: Error writing to flash storage\n");
+        }

Please report the errno and possibly bdrv_get_device_name() to uniquely identify this block device.

Peter's comments about reporting errors to the guest make sense to me. I'm not sure how much work that involves, printing the error is a step in the right direction but we shouldn't forget the TODO.

Stefan

#5 lore raw
Quoted 6 lines · Stefan Hajnoczi

Please report the errno and possibly bdrv_get_device_name() to uniquely identify this block device.

Peter's comments about reporting errors to the guest make sense to me. I'm not sure how much work that involves, printing the error is a step in the right direction but we shouldn't forget the TODO.

I don't think printing errors to stderr at this layer is the right thing to do at all. You can make a case for "print nothing and let the guest handle it", and also for "log errors in the block layer" (which would make all block device users consistent and mean we only needed one bit of code for this rather than lots), but I don't see a case for printing errors in the middle of the stack like this.

(I am also somewhat reluctant to add further random fprintfs without instituting a proper controllable logging mechanism first.)

-- PMM

#6 lore raw
In reply to Stefan Hajnoczi

Am 22.09.2012 18:29, schrieb Stefan Hajnoczi:

Quoted 1 line · Stefan Weil

[snip]

Quoted 6 lines

hw/pflash_cfi01.c static void pflash_update(pflash_t *pfl, int offset, · line 209

Quoted 5 lines
          offset_end = (offset_end + 511) >> 9;
hw/pflash_cfi01.c +2 -2
-        bdrv_write(pfl->bs, offset, pfl->storage + (offset << 9),
-                   offset_end - offset);
+        if (bdrv_write(pfl->bs, offset, pfl->storage + (offset << 9),
+                       offset_end - offset) == -1) {

bdrv_write() returns -errno, not -1.

Thanks. It looks like we have more code which uses the wrong check (and which I copied). So more patches are needed.

Should we also replace code which does bdrv_write() != 0 or !bdrv_write() by bdrv_write() < 0 to get more uniform code (and the same for bdrv_read*), even it is not strictly wrong?

Maybe Kevin as block maintainer should decide that.

Quoted 4 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+            fprintf(stderr, "pflash: Error writing to flash storage\n");
+        }

Please report the errno and possibly bdrv_get_device_name() to uniquely identify this block device.

That would be overkill here: writing flash memory is not used very often (even on real hardware it is typically only used for firmware updates). I expect that anyone who does a firmware update in a QEMU guest will know the name of the flash image file.

Usually I replace the flash image file on the QEMU host when I want to exchange the firmware (much easier than flashing in the guest).

Reporting errno might be more reasonable.Are there other values than EIO (e.g. defective media) and ENOSPC (disk full) which could occur?

A common solution for all users of bdrv_write in the block layer would be even better. VirtualBox for example stops the guest when ENOSPC (disk full) occurs, so it's possible for users to fix that and resume the emulation.

Quoted 5 lines

Peter's comments about reporting errors to the guest make sense to me. I'm not sure how much work that involves, printing the error is a step in the right direction but we shouldn't forget the TODO.

Stefan

There is no 1:1 mapping of block write errors on the host to an error statusin the flash controller.

Currently the code in pflash_cfi01.c sets an error status which can be (mis-)used for block write errors. I just prepared a patch which does this.

In pflash_cfi02.c I did not see such error status handling, therefore I'd stick to printing an error message there.

Regards

Stefan W.

#7 lore raw

Am 22.09.2012 20:53, schrieb Stefan Weil:

Quoted 10 lines

Am 22.09.2012 18:29, schrieb Stefan Hajnoczi:

Quoted 1 line · Stefan Weil

[snip]

Quoted 6 lines

hw/pflash_cfi01.c static void pflash_update(pflash_t *pfl, int offset, · line 209

Quoted 5 lines
          offset_end = (offset_end + 511) >> 9;
hw/pflash_cfi01.c +2 -2
-        bdrv_write(pfl->bs, offset, pfl->storage + (offset << 9),
-                   offset_end - offset);
+        if (bdrv_write(pfl->bs, offset, pfl->storage + (offset << 9),
+                       offset_end - offset) == -1) {

bdrv_write() returns -errno, not -1.

Quoted 8 lines

Thanks. It looks like we have more code which uses the wrong check (and which I copied). So more patches are needed.

Should we also replace code which does bdrv_write() != 0 or !bdrv_write() by bdrv_write() < 0 to get more uniform code (and the same for bdrv_read*), even it is not strictly wrong?

Maybe Kevin as block maintainer should decide that.

Yes, I very much prefer ret < 0 checks for all block layer functions.

Quoted 10 lines
Quoted 4 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+            fprintf(stderr, "pflash: Error writing to flash storage\n");
+        }

Please report the errno and possibly bdrv_get_device_name() to uniquely identify this block device.

That would be overkill here: writing flash memory is not used very often (even on real hardware it is typically only used for firmware updates). I expect that anyone who does a firmware update in a QEMU guest will know the name of the flash image file.

Quoted 5 lines

Usually I replace the flash image file on the QEMU host when I want to exchange the firmware (much easier than flashing in the guest).

Reporting errno might be more reasonable.Are there other values than EIO (e.g. defective media) and ENOSPC (disk full) which could occur?

Basically anything that the OS can return. The block layer may internally generate things like -EACCES for writing to read-only images, or -ENOMEDIUM (not sure if it's possible for pflash).

Quoted 4 lines

A common solution for all users of bdrv_write in the block layer would be even better. VirtualBox for example stops the guest when ENOSPC (disk full) occurs, so it's possible for users to fix that and resume the emulation.

virtio-blk/IDE/scsi-disk do that.

Quoted 3 lines
Quoted 3 lines

Peter's comments about reporting errors to the guest make sense to me. I'm not sure how much work that involves, printing the error is a step in the right direction but we shouldn't forget the TODO.

Shouldn't we avoid fprintfs that can be triggered by the guest?

Kevin

#8 lore raw
Quoted 42 lines · Kevin Wolf

Am 22.09.2012 20:53, schrieb Stefan Weil:

Quoted 10 lines

Am 22.09.2012 18:29, schrieb Stefan Hajnoczi:

Quoted 1 line · Stefan Weil

[snip]

Quoted 6 lines

hw/pflash_cfi01.c static void pflash_update(pflash_t *pfl, int offset, · line 209

Quoted 5 lines
          offset_end = (offset_end + 511) >> 9;
hw/pflash_cfi01.c +2 -2
-        bdrv_write(pfl->bs, offset, pfl->storage + (offset << 9),
-                   offset_end - offset);
+        if (bdrv_write(pfl->bs, offset, pfl->storage + (offset << 9),
+                       offset_end - offset) == -1) {

bdrv_write() returns -errno, not -1.

Quoted 8 lines

Thanks. It looks like we have more code which uses the wrong check (and which I copied). So more patches are needed.

Should we also replace code which does bdrv_write() != 0 or !bdrv_write() by bdrv_write() < 0 to get more uniform code (and the same for bdrv_read*), even it is not strictly wrong?

Maybe Kevin as block maintainer should decide that.

Yes, I very much prefer ret < 0 checks for all block layer functions.

Quoted 10 lines
Quoted 4 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+            fprintf(stderr, "pflash: Error writing to flash storage\n");
+        }

Please report the errno and possibly bdrv_get_device_name() to uniquely identify this block device.

That would be overkill here: writing flash memory is not used very often (even on real hardware it is typically only used for firmware updates). I expect that anyone who does a firmware update in a QEMU guest will know the name of the flash image file.

Quoted 5 lines

Usually I replace the flash image file on the QEMU host when I want to exchange the firmware (much easier than flashing in the guest).

Reporting errno might be more reasonable.Are there other values than EIO (e.g. defective media) and ENOSPC (disk full) which could occur?

Basically anything that the OS can return. The block layer may internally generate things like -EACCES for writing to read-only images, or -ENOMEDIUM (not sure if it's possible for pflash).

Quoted 6 lines
Quoted 4 lines

A common solution for all users of bdrv_write in the block layer would be even better. VirtualBox for example stops the guest when ENOSPC (disk full) occurs, so it's possible for users to fix that and resume the emulation.

virtio-blk/IDE/scsi-disk do that.

Doing it in the block layer for all devices would be cleaner conceptually. If I remember correctly, we did it in devices instead, because that was much simpler.

Quoted 5 lines
Quoted 3 lines
Quoted 3 lines

Peter's comments about reporting errors to the guest make sense to me. I'm not sure how much work that involves, printing the error is a step in the right direction but we shouldn't forget the TODO.

Shouldn't we avoid fprintfs that can be triggered by the guest?

Yes, we should.

Besides, mumbling to stderr is no excuse for a device model to behave incorrectly. Adding a print is a step in the right direction only insofar as it makes the brokenness of the device model more obvious.

#9 lore raw

Am 24.09.2012 10:41, schrieb Markus Armbruster:

Quoted 49 lines
Quoted 47 lines · Kevin Wolf

Am 22.09.2012 20:53, schrieb Stefan Weil:

Quoted 10 lines

Am 22.09.2012 18:29, schrieb Stefan Hajnoczi:

Quoted 1 line · Stefan Weil

[snip]

Quoted 6 lines

hw/pflash_cfi01.c static void pflash_update(pflash_t *pfl, int offset, · line 209

Quoted 5 lines
          offset_end = (offset_end + 511) >> 9;
hw/pflash_cfi01.c +2 -2
-        bdrv_write(pfl->bs, offset, pfl->storage + (offset << 9),
-                   offset_end - offset);
+        if (bdrv_write(pfl->bs, offset, pfl->storage + (offset << 9),
+                       offset_end - offset) == -1) {

bdrv_write() returns -errno, not -1.

Quoted 8 lines

Thanks. It looks like we have more code which uses the wrong check (and which I copied). So more patches are needed.

Should we also replace code which does bdrv_write() != 0 or !bdrv_write() by bdrv_write() < 0 to get more uniform code (and the same for bdrv_read*), even it is not strictly wrong?

Maybe Kevin as block maintainer should decide that.

Yes, I very much prefer ret < 0 checks for all block layer functions.

Quoted 10 lines
Quoted 4 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+            fprintf(stderr, "pflash: Error writing to flash storage\n");
+        }

Please report the errno and possibly bdrv_get_device_name() to uniquely identify this block device.

That would be overkill here: writing flash memory is not used very often (even on real hardware it is typically only used for firmware updates). I expect that anyone who does a firmware update in a QEMU guest will know the name of the flash image file.

Quoted 5 lines

Usually I replace the flash image file on the QEMU host when I want to exchange the firmware (much easier than flashing in the guest).

Reporting errno might be more reasonable.Are there other values than EIO (e.g. defective media) and ENOSPC (disk full) which could occur?

Basically anything that the OS can return. The block layer may internally generate things like -EACCES for writing to read-only images, or -ENOMEDIUM (not sure if it's possible for pflash).

Quoted 4 lines

A common solution for all users of bdrv_write in the block layer would be even better. VirtualBox for example stops the guest when ENOSPC (disk full) occurs, so it's possible for users to fix that and resume the emulation.

Quoted 5 lines
Quoted 1 line

virtio-blk/IDE/scsi-disk do that.

Doing it in the block layer for all devices would be cleaner conceptually. If I remember correctly, we did it in devices instead, because that was much simpler.

I believe today it wouldn't be too hard to implement the request queueing in the block layer. However, we can't change it without breaking migration, we'd need a VMState for the block layer.

Kevin