Skip to contentSkip to conversation

hw/misc/sifive_u_otp: handling the fails of blk_pread and blk_pwrite

PATCH Applied Green Wan 4 participants 10 messages Nov 4, 2020 – Jan 15, 2021 SiFive drivers

lore
Share & tools
lore

Green Wan

author
#0 lore raw

PATCH 1/1 hw/misc/sifive_u_otp: handling the fails of blk_pread and blk_pwrite

Fix code coverage issues by checking return value and handling fail case of blk_pread() and blk_pwrite(). Return default value 0xff if read fails.

---
 hw/misc/sifive_u_otp.c | 31 +++++++++++++++++++++++--------
 1 file changed, 23 insertions(+), 8 deletions(-)
hw/misc/sifive_u_otp.c +23 -8
index 60066375ab..4314727d0d 100644
@@ -62,8 +62,13 @@ static uint64_t sifive_u_otp_read(void *opaque, hwaddr addr, unsigned int size)
             if (s->blk) {
                 int32_t buf;
 
-                blk_pread(s->blk, s->pa * SIFIVE_U_OTP_FUSE_WORD, &buf,
-                          SIFIVE_U_OTP_FUSE_WORD);
+                if (blk_pread(s->blk, s->pa * SIFIVE_U_OTP_FUSE_WORD, &buf,
+                              SIFIVE_U_OTP_FUSE_WORD) < 0) {
+                    qemu_log_mask(LOG_GUEST_ERROR,
+                                  "read error index<%d>\n", s->pa);
+                    return 0xff;
+                }
+
                 return buf;
             }
 
@@ -160,8 +165,12 @@ static void sifive_u_otp_write(void *opaque, hwaddr addr,
 
             /* write to backend */
             if (s->blk) {
-                blk_pwrite(s->blk, s->pa * SIFIVE_U_OTP_FUSE_WORD,
-                           &s->fuse[s->pa], SIFIVE_U_OTP_FUSE_WORD, 0);
+                if (blk_pwrite(s->blk, s->pa * SIFIVE_U_OTP_FUSE_WORD,
+                               &s->fuse[s->pa], SIFIVE_U_OTP_FUSE_WORD,
+                               0) < 0) {
+                    qemu_log_mask(LOG_GUEST_ERROR,
+                                  "write error index<%d>\n", s->pa);
+                }
             }
 
             /* update written bit */
@@ -248,12 +257,18 @@ static void sifive_u_otp_reset(DeviceState *dev)
         int index = SIFIVE_U_OTP_SERIAL_ADDR;
 
         serial_data = s->serial;
-        blk_pwrite(s->blk, index * SIFIVE_U_OTP_FUSE_WORD,
-                   &serial_data, SIFIVE_U_OTP_FUSE_WORD, 0);
+        if (blk_pwrite(s->blk, index * SIFIVE_U_OTP_FUSE_WORD,
+                       &serial_data, SIFIVE_U_OTP_FUSE_WORD, 0) < 0) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "write error index<%d>\n", index);
+        }
 
         serial_data = ~(s->serial);
-        blk_pwrite(s->blk, (index + 1) * SIFIVE_U_OTP_FUSE_WORD,
-                   &serial_data, SIFIVE_U_OTP_FUSE_WORD, 0);
+        if (blk_pwrite(s->blk, (index + 1) * SIFIVE_U_OTP_FUSE_WORD,
+                       &serial_data, SIFIVE_U_OTP_FUSE_WORD, 0) < 0) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "write error index<%d>\n", index + 1);
+        }
     }
 
     /* Initialize write-once map */
Signature · 1 line
2.17.1
#1 lore raw

Ping! This patch was trying to fix a Coverity issue (CID 1435959, 1435960, 1435961) -- is anybody planning to review it?

(I'm not entirely sure 'guest error' is the right warning category, but I don't know the specifics of this device.)

thanks
-- PMM

Quoted 69 lines · Green Wan

Fix code coverage issues by checking return value and handling fail case of blk_pread() and blk_pwrite(). Return default value 0xff if read fails.

---
 hw/misc/sifive_u_otp.c | 31 +++++++++++++++++++++++--------
 1 file changed, 23 insertions(+), 8 deletions(-)
hw/misc/sifive_u_otp.c +23 -9
index 60066375ab..4314727d0d 100644
@@ -62,8 +62,13 @@ static uint64_t sifive_u_otp_read(void *opaque, hwaddr addr, unsigned int size)
             if (s->blk) {
                 int32_t buf;
 
-                blk_pread(s->blk, s->pa * SIFIVE_U_OTP_FUSE_WORD, &buf,
-                          SIFIVE_U_OTP_FUSE_WORD);
+                if (blk_pread(s->blk, s->pa * SIFIVE_U_OTP_FUSE_WORD, &buf,
+                              SIFIVE_U_OTP_FUSE_WORD) < 0) {
+                    qemu_log_mask(LOG_GUEST_ERROR,
+                                  "read error index<%d>\n", s->pa);
+                    return 0xff;
+                }
+
                 return buf;
             }
 
@@ -160,8 +165,12 @@ static void sifive_u_otp_write(void *opaque, hwaddr addr,
 
             /* write to backend */
             if (s->blk) {
-                blk_pwrite(s->blk, s->pa * SIFIVE_U_OTP_FUSE_WORD,
-                           &s->fuse[s->pa], SIFIVE_U_OTP_FUSE_WORD, 0);
+                if (blk_pwrite(s->blk, s->pa * SIFIVE_U_OTP_FUSE_WORD,
+                               &s->fuse[s->pa], SIFIVE_U_OTP_FUSE_WORD,
+                               0) < 0) {
+                    qemu_log_mask(LOG_GUEST_ERROR,
+                                  "write error index<%d>\n", s->pa);
+                }
             }
 
             /* update written bit */
@@ -248,12 +257,18 @@ static void sifive_u_otp_reset(DeviceState *dev)
         int index = SIFIVE_U_OTP_SERIAL_ADDR;
 
         serial_data = s->serial;
-        blk_pwrite(s->blk, index * SIFIVE_U_OTP_FUSE_WORD,
-                   &serial_data, SIFIVE_U_OTP_FUSE_WORD, 0);
+        if (blk_pwrite(s->blk, index * SIFIVE_U_OTP_FUSE_WORD,
+                       &serial_data, SIFIVE_U_OTP_FUSE_WORD, 0) < 0) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "write error index<%d>\n", index);
+        }
 
         serial_data = ~(s->serial);
-        blk_pwrite(s->blk, (index + 1) * SIFIVE_U_OTP_FUSE_WORD,
-                   &serial_data, SIFIVE_U_OTP_FUSE_WORD, 0);
+        if (blk_pwrite(s->blk, (index + 1) * SIFIVE_U_OTP_FUSE_WORD,
+                       &serial_data, SIFIVE_U_OTP_FUSE_WORD, 0) < 0) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "write error index<%d>\n", index + 1);
+        }
     }
 
     /* Initialize write-once map */
--

2.17.1

#2 lore raw
Quoted 7 lines · Peter Maydell

Ping! This patch was trying to fix a Coverity issue (CID 1435959, 1435960, 1435961) -- is anybody planning to review it?

(I'm not entirely sure 'guest error' is the right warning category, but I don't know the specifics of this device.)

I think we should just use 'printf' instead of log a "guest error" because the guest does nothing wrong.

Regards,
Bin

#3 lore raw
Quoted 6 lines · Bin Meng
Quoted 4 lines · Peter Maydell

Ping! This patch was trying to fix a Coverity issue (CID 1435959, 1435960, 1435961) -- is anybody planning to review it?

Quoted 6 lines
Quoted 3 lines

(I'm not entirely sure 'guest error' is the right warning category, but I don't know the specifics of this device.)

I think we should just use 'printf' instead of log a "guest error" because the guest does nothing wrong.

printf is definitely the wrong thing... you need to either report the error back to the guest if the interface the guest is using has a facility for reporting read/write failures, or log or report it to the user using one of our APIs for that.

thanks
-- PMM

#4 lore raw
Quoted 12 lines · Peter Maydell
Quoted 10 lines · Bin Meng
Quoted 7 lines · Peter Maydell

Ping! This patch was trying to fix a Coverity issue (CID 1435959, 1435960, 1435961) -- is anybody planning to review it?

(I'm not entirely sure 'guest error' is the right warning category, but I don't know the specifics of this device.)

Quoted 7 lines
Quoted 2 lines

I think we should just use 'printf' instead of log a "guest error" because the guest does nothing wrong.

printf is definitely the wrong thing... you need to either report the error back to the guest if the interface the guest is using has a facility for reporting read/write failures, or log or report it to the user using one of our APIs for that.

It seems the hardware does not have a mechanism to report to the software when hardware cannot fulfill the task requested by software.

I checked all existence of block_pwrite() callers. It looks like this is not handled consistently. Some indeed call printf(), some call error_setg_errno(), some call fprintf(stderr), some call qemu_log()
...

Regards,
Bin

#5 lore raw
Quoted 22 lines · Bin Meng
Quoted 12 lines · Peter Maydell
Quoted 10 lines · Bin Meng
Quoted 7 lines · Peter Maydell

Ping! This patch was trying to fix a Coverity issue (CID 1435959, 1435960, 1435961) -- is anybody planning to review it?

(I'm not entirely sure 'guest error' is the right warning category, but I don't know the specifics of this device.)

Quoted 7 lines
Quoted 2 lines

I think we should just use 'printf' instead of log a "guest error" because the guest does nothing wrong.

printf is definitely the wrong thing... you need to either report the error back to the guest if the interface the guest is using has a facility for reporting read/write failures, or log or report it to the user using one of our APIs for that.

Quoted 7 lines

It seems the hardware does not have a mechanism to report to the software when hardware cannot fulfill the task requested by software.

I checked all existence of block_pwrite() callers. It looks like this is not handled consistently. Some indeed call printf(), some call error_setg_errno(), some call fprintf(stderr), some call qemu_log()
...

Logging a guest error seems like the best bet, I'm not really sure what else we would do.

Alistair

Quoted 3 lines

Regards,
Bin

#6 lore raw
Quoted 12 lines · Alistair Francis
Quoted 10 lines · Bin Meng

On Fri, Jan 15, 2021 at 9:55 PM Peter Maydell <[email protected]> wrote

Quoted 4 lines

printf is definitely the wrong thing... you need to either report the error back to the guest if the interface the guest is using has a facility for reporting read/write failures, or log or report it to the user using one of our APIs for that.

It seems the hardware does not have a mechanism to report to the software when hardware cannot fulfill the task requested by software.

Quoted 7 lines
Quoted 4 lines

I checked all existence of block_pwrite() callers. It looks like this is not handled consistently. Some indeed call printf(), some call error_setg_errno(), some call fprintf(stderr), some call qemu_log()
...

Logging a guest error seems like the best bet, I'm not really sure what else we would do.

Looking at the other options, I think error_report() of some kind is probably the best bet here.

thanks
-- PMM

#7 lore raw
Quoted 19 lines · Peter Maydell
Quoted 17 lines · Alistair Francis
Quoted 14 lines · Bin Meng

On Fri, Jan 15, 2021 at 9:55 PM Peter Maydell <[email protected]> wrote

Quoted 4 lines

printf is definitely the wrong thing... you need to either report the error back to the guest if the interface the guest is using has a facility for reporting read/write failures, or log or report it to the user using one of our APIs for that.

It seems the hardware does not have a mechanism to report to the software when hardware cannot fulfill the task requested by software.

I checked all existence of block_pwrite() callers. It looks like this is not handled consistently. Some indeed call printf(), some call error_setg_errno(), some call fprintf(stderr), some call qemu_log()
...

Quoted 5 lines
Quoted 2 lines

Logging a guest error seems like the best bet, I'm not really sure what else we would do.

Looking at the other options, I think error_report() of some kind is probably the best bet here.

Ok

@Green Wan do you mind sending a new version?

Alistair

Quoted 3 lines

thanks
-- PMM

#8 lore raw
In reply to Peter Maydell
Quoted 6 lines · Peter Maydell

Ping! This patch was trying to fix a Coverity issue (CID 1435959, 1435960, 1435961) -- is anybody planning to review it?

(I'm not entirely sure 'guest error' is the right warning category, but I don't know the specifics of this device.)

Thanks for the ping, this feel through the cracks somehow.

Applied to riscv-to-apply.next

Alistair

Quoted 74 lines

thanks
-- PMM

Quoted 69 lines · Green Wan

Fix code coverage issues by checking return value and handling fail case of blk_pread() and blk_pwrite(). Return default value 0xff if read fails.

---
 hw/misc/sifive_u_otp.c | 31 +++++++++++++++++++++++--------
 1 file changed, 23 insertions(+), 8 deletions(-)
hw/misc/sifive_u_otp.c +23 -9
index 60066375ab..4314727d0d 100644
@@ -62,8 +62,13 @@ static uint64_t sifive_u_otp_read(void *opaque, hwaddr addr, unsigned int size)
             if (s->blk) {
                 int32_t buf;
 
-                blk_pread(s->blk, s->pa * SIFIVE_U_OTP_FUSE_WORD, &buf,
-                          SIFIVE_U_OTP_FUSE_WORD);
+                if (blk_pread(s->blk, s->pa * SIFIVE_U_OTP_FUSE_WORD, &buf,
+                              SIFIVE_U_OTP_FUSE_WORD) < 0) {
+                    qemu_log_mask(LOG_GUEST_ERROR,
+                                  "read error index<%d>\n", s->pa);
+                    return 0xff;
+                }
+
                 return buf;
             }
 
@@ -160,8 +165,12 @@ static void sifive_u_otp_write(void *opaque, hwaddr addr,
 
             /* write to backend */
             if (s->blk) {
-                blk_pwrite(s->blk, s->pa * SIFIVE_U_OTP_FUSE_WORD,
-                           &s->fuse[s->pa], SIFIVE_U_OTP_FUSE_WORD, 0);
+                if (blk_pwrite(s->blk, s->pa * SIFIVE_U_OTP_FUSE_WORD,
+                               &s->fuse[s->pa], SIFIVE_U_OTP_FUSE_WORD,
+                               0) < 0) {
+                    qemu_log_mask(LOG_GUEST_ERROR,
+                                  "write error index<%d>\n", s->pa);
+                }
             }
 
             /* update written bit */
@@ -248,12 +257,18 @@ static void sifive_u_otp_reset(DeviceState *dev)
         int index = SIFIVE_U_OTP_SERIAL_ADDR;
 
         serial_data = s->serial;
-        blk_pwrite(s->blk, index * SIFIVE_U_OTP_FUSE_WORD,
-                   &serial_data, SIFIVE_U_OTP_FUSE_WORD, 0);
+        if (blk_pwrite(s->blk, index * SIFIVE_U_OTP_FUSE_WORD,
+                       &serial_data, SIFIVE_U_OTP_FUSE_WORD, 0) < 0) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "write error index<%d>\n", index);
+        }
 
         serial_data = ~(s->serial);
-        blk_pwrite(s->blk, (index + 1) * SIFIVE_U_OTP_FUSE_WORD,
-                   &serial_data, SIFIVE_U_OTP_FUSE_WORD, 0);
+        if (blk_pwrite(s->blk, (index + 1) * SIFIVE_U_OTP_FUSE_WORD,
+                       &serial_data, SIFIVE_U_OTP_FUSE_WORD, 0) < 0) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "write error index<%d>\n", index + 1);
+        }
     }
 
     /* Initialize write-once map */
--

2.17.1

Reviewed-by on v1 1/1
#9 lore raw
In reply to Green Wan
Quoted 5 lines · Green Wan

Fix code coverage issues by checking return value and handling fail case of blk_pread() and blk_pwrite(). Return default value 0xff if read fails.

Alistair

Quoted 65 lines
---
 hw/misc/sifive_u_otp.c | 31 +++++++++++++++++++++++--------
 1 file changed, 23 insertions(+), 8 deletions(-)
hw/misc/sifive_u_otp.c +23 -9
index 60066375ab..4314727d0d 100644
@@ -62,8 +62,13 @@ static uint64_t sifive_u_otp_read(void *opaque, hwaddr addr, unsigned int size)
             if (s->blk) {
                 int32_t buf;
 
-                blk_pread(s->blk, s->pa * SIFIVE_U_OTP_FUSE_WORD, &buf,
-                          SIFIVE_U_OTP_FUSE_WORD);
+                if (blk_pread(s->blk, s->pa * SIFIVE_U_OTP_FUSE_WORD, &buf,
+                              SIFIVE_U_OTP_FUSE_WORD) < 0) {
+                    qemu_log_mask(LOG_GUEST_ERROR,
+                                  "read error index<%d>\n", s->pa);
+                    return 0xff;
+                }
+
                 return buf;
             }
 
@@ -160,8 +165,12 @@ static void sifive_u_otp_write(void *opaque, hwaddr addr,
 
             /* write to backend */
             if (s->blk) {
-                blk_pwrite(s->blk, s->pa * SIFIVE_U_OTP_FUSE_WORD,
-                           &s->fuse[s->pa], SIFIVE_U_OTP_FUSE_WORD, 0);
+                if (blk_pwrite(s->blk, s->pa * SIFIVE_U_OTP_FUSE_WORD,
+                               &s->fuse[s->pa], SIFIVE_U_OTP_FUSE_WORD,
+                               0) < 0) {
+                    qemu_log_mask(LOG_GUEST_ERROR,
+                                  "write error index<%d>\n", s->pa);
+                }
             }
 
             /* update written bit */
@@ -248,12 +257,18 @@ static void sifive_u_otp_reset(DeviceState *dev)
         int index = SIFIVE_U_OTP_SERIAL_ADDR;
 
         serial_data = s->serial;
-        blk_pwrite(s->blk, index * SIFIVE_U_OTP_FUSE_WORD,
-                   &serial_data, SIFIVE_U_OTP_FUSE_WORD, 0);
+        if (blk_pwrite(s->blk, index * SIFIVE_U_OTP_FUSE_WORD,
+                       &serial_data, SIFIVE_U_OTP_FUSE_WORD, 0) < 0) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "write error index<%d>\n", index);
+        }
 
         serial_data = ~(s->serial);
-        blk_pwrite(s->blk, (index + 1) * SIFIVE_U_OTP_FUSE_WORD,
-                   &serial_data, SIFIVE_U_OTP_FUSE_WORD, 0);
+        if (blk_pwrite(s->blk, (index + 1) * SIFIVE_U_OTP_FUSE_WORD,
+                       &serial_data, SIFIVE_U_OTP_FUSE_WORD, 0) < 0) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "write error index<%d>\n", index + 1);
+        }
     }
 
     /* Initialize write-once map */
--

2.17.1