Skip to contentSkip to conversation

ppc/amigaone: Check blk_pwrite return value

PATCH In review BALATON Zoltan 3 participants 5 messages Mar 14, 2025 – Mar 20, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ppc/amigaone: Check blk_pwrite return value

Coverity reported that return value of blk_pwrite() maybe should not be ignored. We can't do much if this happens other than report an error but let's do that to silence this report.

Resolves: Coverity CID 1593725

---
 hw/ppc/amigaone.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
hw/ppc/amigaone.c +8 -6
index 483512125f..5d787c3059 100644
@@ -108,8 +108,8 @@ static void nvram_write(void *opaque, hwaddr addr, uint64_t val,
     uint8_t *p = memory_region_get_ram_ptr(&s->mr);
 
     p[addr] = val;
-    if (s->blk) {
-        blk_pwrite(s->blk, addr, 1, &val, 0);
+    if (s->blk && blk_pwrite(s->blk, addr, 1, &val, 0) < 0) {
+        error_report("%s: could not write %s", __func__, blk_name(s->blk));
     }
 }
 
@@ -151,15 +151,17 @@ static void nvram_realize(DeviceState *dev, Error **errp)
         *c = cpu_to_be32(CRC32_DEFAULT_ENV);
         /* Also copies terminating \0 as env is terminated by \0\0 */
         memcpy(p + 4, default_env, sizeof(default_env));
-        if (s->blk) {
-            blk_pwrite(s->blk, 0, sizeof(crc) + sizeof(default_env), p, 0);
+        if (s->blk &&
+            blk_pwrite(s->blk, 0, sizeof(crc) + sizeof(default_env), p, 0) < 0
+           ) {
+            error_report("%s: could not write %s", __func__, blk_name(s->blk));
         }
         return;
     }
     if (*c == 0) {
         *c = cpu_to_be32(crc32(0, p + 4, NVRAM_SIZE - 4));
-        if (s->blk) {
-            blk_pwrite(s->blk, 0, 4, p, 0);
+        if (s->blk && blk_pwrite(s->blk, 0, 4, p, 0) < 0) {
+            error_report("%s: could not write %s", __func__, blk_name(s->blk));
         }
     }
     if (be32_to_cpu(*c) != crc) {
Signature · 1 line
2.41.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · BALATON Zoltan

Coverity reported that return value of blk_pwrite() maybe should not be ignored. We can't do much if this happens other than report an error but let's do that to silence this report.

Resolves: Coverity CID 1593725

Quoted 41 lines
---
 hw/ppc/amigaone.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
hw/ppc/amigaone.c +8 -6
index 483512125f..5d787c3059 100644
@@ -108,8 +108,8 @@ static void nvram_write(void *opaque, hwaddr addr, uint64_t val,
     uint8_t *p = memory_region_get_ram_ptr(&s->mr);
 
     p[addr] = val;
-    if (s->blk) {
-        blk_pwrite(s->blk, addr, 1, &val, 0);
+    if (s->blk && blk_pwrite(s->blk, addr, 1, &val, 0) < 0) {
+        error_report("%s: could not write %s", __func__, blk_name(s->blk));
     }
 }
 
@@ -151,15 +151,17 @@ static void nvram_realize(DeviceState *dev, Error **errp)
         *c = cpu_to_be32(CRC32_DEFAULT_ENV);
         /* Also copies terminating \0 as env is terminated by \0\0 */
         memcpy(p + 4, default_env, sizeof(default_env));
-        if (s->blk) {
-            blk_pwrite(s->blk, 0, sizeof(crc) + sizeof(default_env), p, 0);
+        if (s->blk &&
+            blk_pwrite(s->blk, 0, sizeof(crc) + sizeof(default_env), p, 0) < 0
+           ) {
+            error_report("%s: could not write %s", __func__, blk_name(s->blk));
         }
         return;
     }
     if (*c == 0) {
         *c = cpu_to_be32(crc32(0, p + 4, NVRAM_SIZE - 4));
-        if (s->blk) {
-            blk_pwrite(s->blk, 0, 4, p, 0);
+        if (s->blk && blk_pwrite(s->blk, 0, 4, p, 0) < 0) {
+            error_report("%s: could not write %s", __func__, blk_name(s->blk));
         }
     }
     if (be32_to_cpu(*c) != crc) {
#2 lore raw
In reply to BALATON Zoltan
Quoted 15 lines · BALATON Zoltan

Coverity reported that return value of blk_pwrite() maybe should not be ignored. We can't do much if this happens other than report an error but let's do that to silence this report.

Resolves: Coverity CID 1593725

---
  hw/ppc/amigaone.c | 14 ++++++++------
  1 file changed, 8 insertions(+), 6 deletions(-)
hw/ppc/amigaone.c +0 -0
index 483512125f..5d787c3059 100644

hw/ppc/amigaone.c static void nvram_write(void *opaque, hwaddr addr, uint64_t val, · line 108

Quoted 1 line
hw/ppc/amigaone.c +0 -0
@@ -108,8 +108,8 @@ static void nvram_write(void *opaque, hwaddr addr, uint64_t val,
      uint8_t *p = memory_region_get_ram_ptr(&s->mr);

why is the nvram never read ?

hw/ppc/amigaone.c static void nvram_write(void *opaque, hwaddr addr, uint64_t val, · line 112

Quoted 6 lines
hw/ppc/amigaone.c +2 -2
      p[addr] = val;
-    if (s->blk) {
-        blk_pwrite(s->blk, addr, 1, &val, 0);
+    if (s->blk && blk_pwrite(s->blk, addr, 1, &val, 0) < 0) {
+        error_report("%s: could not write %s", __func__, blk_name(s->blk));

hmm, guest_error maybe ? since this is a runtime error.

hw/ppc/amigaone.c static void nvram_realize(DeviceState *dev, Error **errp) · line 157

Quoted 13 lines
hw/ppc/amigaone.c +4 -2
      }
  }
 
@@ -151,15 +151,17 @@ static void nvram_realize(DeviceState *dev, Error **errp)
          *c = cpu_to_be32(CRC32_DEFAULT_ENV);
          /* Also copies terminating \0 as env is terminated by \0\0 */
          memcpy(p + 4, default_env, sizeof(default_env));
-        if (s->blk) {
-            blk_pwrite(s->blk, 0, sizeof(crc) + sizeof(default_env), p, 0);
+        if (s->blk &&
+            blk_pwrite(s->blk, 0, sizeof(crc) + sizeof(default_env), p, 0) < 0
+           ) {
+            error_report("%s: could not write %s", __func__, blk_name(s->blk));

This should use the errp parameter.

hw/ppc/amigaone.c static void nvram_realize(DeviceState *dev, Error **errp) · line 164

Quoted 9 lines
hw/ppc/amigaone.c +2 -2
          }
          return;
      }
      if (*c == 0) {
          *c = cpu_to_be32(crc32(0, p + 4, NVRAM_SIZE - 4));
-        if (s->blk) {
-            blk_pwrite(s->blk, 0, 4, p, 0);
+        if (s->blk && blk_pwrite(s->blk, 0, 4, p, 0) < 0) {
+            error_report("%s: could not write %s", __func__, blk_name(s->blk));

same here.

Thanks,

C.

Quoted 3 lines
hw/ppc/amigaone.c +0 -0
          }
      }
      if (be32_to_cpu(*c) != crc) {
#3 lore raw
Quoted 11 lines · Cédric Le Goater
Quoted 10 lines · BALATON Zoltan

Coverity reported that return value of blk_pwrite() maybe should not be ignored. We can't do much if this happens other than report an error but let's do that to silence this report.

Resolves: Coverity CID 1593725

---
  hw/ppc/amigaone.c | 14 ++++++++------
  1 file changed, 8 insertions(+), 6 deletions(-)
Quoted 9 lines

hw/ppc/amigaone.c static void nvram_write(void *opaque, hwaddr addr, · line 109

Quoted 7 lines
hw/ppc/amigaone.c +0 -0
index 483512125f..5d787c3059 100644
@@ -108,8 +108,8 @@ static void nvram_write(void *opaque, hwaddr addr, 
 uint64_t val,
      uint8_t *p = memory_region_get_ram_ptr(&s->mr);

why is the nvram never read ?

There's a comment about that. It's a rom_device which maps the memory region directly so does not go through the read callback. But I thin there must be a read callback and cannot be null so we have an empty one. Previously I had one that worked in case romd mode is turned off but Nick said having dead code is not wanted and better to mark it unreachable.

Quoted 8 lines

hw/ppc/amigaone.c static void nvram_write(void *opaque, hwaddr addr, uint64_t val,

Quoted 6 lines
hw/ppc/amigaone.c +2 -2
        p[addr] = val;
-    if (s->blk) {
-        blk_pwrite(s->blk, addr, 1, &val, 0);
+    if (s->blk && blk_pwrite(s->blk, addr, 1, &val, 0) < 0) {
+        error_report("%s: could not write %s", __func__, 
 blk_name(s->blk));

hmm, guest_error maybe ? since this is a runtime error.

It's not a guest error but some problem on the host with the backing file.

Quoted 30 lines
Quoted 15 lines
hw/ppc/amigaone.c +0 -0
      }
  }

@@ -151,15 +151,17 @@ static void nvram_realize(DeviceState *dev, Error
**errp)

          *c = cpu_to_be32(CRC32_DEFAULT_ENV);
          /* Also copies terminating \0 as env is terminated by \0\0 */
          memcpy(p + 4, default_env, sizeof(default_env));
hw/ppc/amigaone.c +0 -2
-        if (s->blk) {
-            blk_pwrite(s->blk, 0, sizeof(crc) + sizeof(default_env), p, 

0);

hw/ppc/amigaone.c +2 -0
+        if (s->blk &&
+            blk_pwrite(s->blk, 0, sizeof(crc) + sizeof(default_env), p, 0) 

< 0

hw/ppc/amigaone.c +2 -0
+           ) {
+            error_report("%s: could not write %s", __func__, 
Quoted 1 line

blk_name(s->blk));

This should use the errp parameter.

Quoted 10 lines
          }
          return;
      }
      if (*c == 0) {
          *c = cpu_to_be32(crc32(0, p + 4, NVRAM_SIZE - 4));
hw/ppc/amigaone.c +2 -2
-        if (s->blk) {
-            blk_pwrite(s->blk, 0, 4, p, 0);
+        if (s->blk && blk_pwrite(s->blk, 0, 4, p, 0) < 0) {
+            error_report("%s: could not write %s", __func__, 

blk_name(s->blk));

Quoted 1 line

same here.

It could but I think it's not needed. It still works without the backing file and the guest works, just may not save the NVRAM contents which is a problem on the host. So the error is reported but I'm not sure it should abort. In practice if there's some fatal error with the backing file the

blk_set_perm(s->blk, BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE,

                     BLK_PERM_ALL, &error_fatal);

earlier will catch that so it won't even get here.

Regards,
BALATON Zoltan

#4 lore raw
Quoted 76 lines · BALATON Zoltan
Quoted 11 lines · Cédric Le Goater
Quoted 10 lines · BALATON Zoltan

Coverity reported that return value of blk_pwrite() maybe should not be ignored. We can't do much if this happens other than report an error but let's do that to silence this report.

Resolves: Coverity CID 1593725

---
  hw/ppc/amigaone.c | 14 ++++++++------
  1 file changed, 8 insertions(+), 6 deletions(-)
Quoted 9 lines

hw/ppc/amigaone.c static void nvram_write(void *opaque, hwaddr addr, · line 109

Quoted 7 lines
hw/ppc/amigaone.c +0 -0
index 483512125f..5d787c3059 100644
@@ -108,8 +108,8 @@ static void nvram_write(void *opaque, hwaddr addr, 
 uint64_t val,
      uint8_t *p = memory_region_get_ram_ptr(&s->mr);

why is the nvram never read ?

There's a comment about that. It's a rom_device which maps the memory region directly so does not go through the read callback. But I thin there must be a read callback and cannot be null so we have an empty one. Previously I had one that worked in case romd mode is turned off but Nick said having dead code is not wanted and better to mark it unreachable.

Quoted 8 lines

hw/ppc/amigaone.c static void nvram_write(void *opaque, hwaddr addr, uint64_t val,

Quoted 6 lines
hw/ppc/amigaone.c +2 -2
        p[addr] = val;
-    if (s->blk) {
-        blk_pwrite(s->blk, addr, 1, &val, 0);
+    if (s->blk && blk_pwrite(s->blk, addr, 1, &val, 0) < 0) {
+        error_report("%s: could not write %s", __func__, 
 blk_name(s->blk));

hmm, guest_error maybe ? since this is a runtime error.

It's not a guest error but some problem on the host with the backing file.

Quoted 30 lines
Quoted 15 lines
hw/ppc/amigaone.c +0 -0
      }
  }

@@ -151,15 +151,17 @@ static void nvram_realize(DeviceState *dev, Error
**errp)

          *c = cpu_to_be32(CRC32_DEFAULT_ENV);
          /* Also copies terminating \0 as env is terminated by \0\0 */
          memcpy(p + 4, default_env, sizeof(default_env));
hw/ppc/amigaone.c +0 -2
-        if (s->blk) {
-            blk_pwrite(s->blk, 0, sizeof(crc) + sizeof(default_env), p, 

0);

hw/ppc/amigaone.c +2 -0
+        if (s->blk &&
+            blk_pwrite(s->blk, 0, sizeof(crc) + sizeof(default_env), p, 0) 

< 0

hw/ppc/amigaone.c +2 -0
+           ) {
+            error_report("%s: could not write %s", __func__, 
Quoted 1 line

blk_name(s->blk));

This should use the errp parameter.

Quoted 10 lines
          }
          return;
      }
      if (*c == 0) {
          *c = cpu_to_be32(crc32(0, p + 4, NVRAM_SIZE - 4));
hw/ppc/amigaone.c +2 -2
-        if (s->blk) {
-            blk_pwrite(s->blk, 0, 4, p, 0);
+        if (s->blk && blk_pwrite(s->blk, 0, 4, p, 0) < 0) {
+            error_report("%s: could not write %s", __func__, 

blk_name(s->blk));

Quoted 1 line

same here.

It could but I think it's not needed. It still works without the backing file and the guest works, just may not save the NVRAM contents which is a problem on the host. So the error is reported but I'm not sure it should abort. In practice if there's some fatal error with the backing file the

Quoted 4 lines

blk_set_perm(s->blk, BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE,

                      BLK_PERM_ALL, &error_fatal);

earlier will catch that so it won't even get here.

I'll take it as is since it seems to be an improvement. Some other nvram devices with backing files do the same thing with write failures.

Thanks,
Nick