hw/misc/sifive_u_otp: handling the fails of blk_pread and blk_pwrite
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
@@ -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
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()andblk_pwrite(). Return default value0xffif read fails.Signed-off-by Green Wan <[email protected]>--- hw/misc/sifive_u_otp.c | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-)hw/misc/sifive_u_otp.c +23
-9@@ -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
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
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
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
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 callprintf(), some callerror_setg_errno(), some call fprintf(stderr), some callqemu_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
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 callprintf(), some callerror_setg_errno(), some call fprintf(stderr), some callqemu_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
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 callprintf(), some callerror_setg_errno(), some call fprintf(stderr), some callqemu_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
Alistair Francis
AppliedQuoted 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
-- PMMQuoted 69 lines · Green Wan
Fix code coverage issues by checking return value and handling fail case of
blk_pread()andblk_pwrite(). Return default value0xffif read fails.Signed-off-by Green Wan <[email protected]>--- hw/misc/sifive_u_otp.c | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-)hw/misc/sifive_u_otp.c +23
-9@@ -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
Quoted 5 lines · Green Wan
Fix code coverage issues by checking return value and handling fail case of
blk_pread()andblk_pwrite(). Return default value0xffif read fails.Signed-off-by Green Wan <[email protected]>
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@@ -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