You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2021/08/09 05:28:45 UTC

[GitHub] [incubator-nuttx] ken-voly opened a new pull request #4299: bchlib: Improve error handling

ken-voly opened a new pull request #4299:
URL: https://github.com/apache/incubator-nuttx/pull/4299


   ## Summary
   The return value of some bchlib functions were ignored in the bchlib driver. Ignoring the return values creates a bug where even though a read/write fails, the read/write function reports that the operation was successful. This pull request adds checking for the return values of some functions in `bchlib_read.c`, `bchlib_write.c`, and `bchlib_cache.c` in order to fix this bug.
   
   ## Impact
   This pull request adds checks for the return value of `bchlib_readsector()`. Previously, if a sector failed to be loaded using `bchlib_readsector()`, the read/write could report that the operation was successful. With this commit, the return value of `bchlib_readsector()` is always checked and the entire read/write operation fails if `bchlib_readsector()` returns a negative value.
   
   This pull request also fixes the error handling in some `bchlib_cache.c` functions. In particular, when an error occurs in `bchlib_readsector()` or `bchlib_flushsector()`, the function returns immediately. This prevents a bug where a sector can be marked not dirty after an unsuccessful flush or a sector could be marked as loaded into the cache after an unsuccessful read. Previously, two subsequent calls to `bchlib_readsector()` or `bchlib_flushsector()` are always reported as successful.
   
   ## Testing
   I tested the fixed bch driver with the `master` branch of PX4 on an STM32H743 Nucleo board. After initializing an FRAM using the `ramtron` and `bchlib` drivers, I disconnected the CS pin of the FRAM in order to make the reads and writes fail.
   
   ### Case 1: read - no fix
   
   Even though the read fails, the failure is not communicated back to the caller of `read()`. This could lead to the caller using garbage data.
   
   ```
   nsh> param load /fs/mtd_params
   ftl_read: sector: 0 nsectors: 1
   ramtron_bread: startblock: 00000000 nblocks: 1
   ramtron_read: offset: 00000000 nbytes: 512
   ramtron_read: ERROR: read status failed - got 0x00
   ramtron_read: return nbytes: -5
   ftl_reload: ERROR: Read 1 blocks starting at block 0 failed: -5
   bchlib_readsector: Read failed: 135573926
   INFO  [parameters] BSON document size 0 bytes, decoded 5 bytes
   ```
   
   ### Case 2: read - fixed
   
   With this PR, the caller is properly notified that the read failed, allowing the caller to retry the read.
   
   ```
   nsh> param load /fs/mtd_params
   ftl_read: sector: 0 nsectors: 1
   ramtron_bread: startblock: 00000000 nblocks: 1
   ramtron_read: offset: 00000000 nbytes: 512
   ramtron_read: ERROR: read status failed - got 0x00
   ramtron_read: return nbytes: -5
   ftl_reload: ERROR: Read 1 blocks starting at block 0 failed: -5
   bchlib_readsector: Read failed: -5
   ftl_read: sector: 0 nsectors: 1
   ramtron_bread: startblock: 00000000 nblocks: 1
   ramtron_read: offset: 00000000 nbytes: 512
   ERROR [pramtron_read: ERROR: read status failed - got 0x00
   ramtron_read: return nbytes: -5
   ftl_reload: ERROR: Read 1 blocks starting at block 0 failed: -5
   bchlib_readsector: Read failed: -5
   ftl_read: sector: 0 nsectors: 1
   ramtron_bread: startblock: 00000000 nblocks: 1
   ramtron_read: offset: 00000000 nbytes: 512
   arameterramtron_read: ERROR: read status failed - got 0x00
   ramtron_read: return nbytes: -5
   ftl_reload: ERROR: Read 1 blocks starting at block 0 failed: -5
   bchlib_readsector: Read failed: -5
   ftl_read: sector: 0 nsectors: 1
   ramtron_bread: startblock: 00000000 nblocks: 1
   ramtron_read: offset: 00000000 nbytes: 512
   s] paramramtron_read: ERROR: read status failed - got 0x00
   ramtron_read: return nbytes: -5
   ftl_reload: ERROR: Read 1 blocks starting at block 0 failed: -5
   bchlib_readsector: Read failed: -5
    import bson decoder init failed attempt 1, retrying
   ERROR [parameters] param import bson decoder init failed attempt 2, retrying
   ERROR [parameters] param import bson decoder init failed attempt 3, retrying
   ERROR [parameters] param import bson decoder init failed attempt 4, retrying
   ERROR [param] importing from '/fs/mtd_params' failed (-1)
   ```
   
   ### Case 3: write - no fix
   
   Even though the write fails, the failure is not communicated back to the caller of `write()`.
   
   ```
   nsh> param save /fs/mtd_params
   ftl_read: sector: 0 nsectors: 1
   ramtron_bread: startblock: 00000000 nblocks: 1
   ramtron_read: offset: 00000000 nbytes: 512
   ramtron_read: ERROR: read status failed - got 0x00
   ramtron_read: return nbytes: -5
   ftl_reload: ERROR: Read 1 blocks starting at block 0 failed: -5
   bchlib_readsector: Read failed: 135573926
   ftl_write: sector: 0 nsectors: 1
   ramtron_erase: startblock: 00000000 nblocks: 1
   ramtron_erase: On RAMTRON devices erasing makes no sense, returning as OK
   ftl_flush: Write 512 bytes into erase block=0 at offset=0
   ramtron_bwrite: startblock: 00000000 nblocks: 1
   ramtron_pagewrite: page: 00000000 offset: 00000000
   ramtron_writeenable: Enabled
   ramtron_pagewrite: Written
   ramtron_waitwritecomplete: Complete
   ```
   
   ### Case 4: write - fixed
   
   With this PR, the caller is notified that the write failed, allowing it to perform
   
   ```
   nsh> param save /fs/mtd_params
   ftl_read: sector: 0 nsectors: 1
   ramtron_bread: startblock: 00000000 nblocks: 1
   ramtron_read: offset: 00000000 nbytes: 512
   ramtron_read: ERROR: read status failed - got 0x00
   ramtron_read: return nbytes: -5
   ftl_reload: ERROR: Read 1 blocks starting at block 0 failed: -5
   bchlib_readsector: Read failed: -5
   ERROR [parameters] BSON encoder finialize failed
   ERROR [param] exporting to '/fs/mtd_params' failed (-1)
   ```
   
   ### Case 5: read - without bchlib_cache.c fixes
   
   Without the fixes in `bchlib_cache.c`, two subsequent calls to `bchlib_readsector()` would always result in the second call reporting a success even if the first failed. Here the `param` command attempts to reread the parameters after the first read fails. Because the error handling is not fixed, the second attempt always succeeds, even if the memory is disconnected. With the fixes from the PR, the sector is not marked as loaded in the cache on a read fail and the output looks like **Case 2**.
   
   ```
   nsh> param load /fs/mtd_params
   ftl_read: sector: 0 nsectors: 1
   ramtron_bread: startblock: 00000000 nblocks: 1
   ramtron_read: offset: 00000000 nbytes: 512
   ramtron_read: ERROR: read status failed - got 0x00
   ramtron_read: return nbytes: -5
   ftl_reload: ERROR: Read 1 blocks starting at block 0 failed: -5
   bchlib_readsector: Read failed: -5
   ERROR [parameters] param import bson decoder init failed attempt 1, retrying
   INFO  [parameters] BSON document size 0 bytes, decoded 5 bytes
   ```
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] ken-voly commented on pull request #4299: bchlib: Improve error handling

Posted by GitBox <gi...@apache.org>.
ken-voly commented on pull request #4299:
URL: https://github.com/apache/incubator-nuttx/pull/4299#issuecomment-894961509


   There is also an unchecked return value of `bchlib_flushsector()` in `bch_close()`. Should we have the close fail if the flush fails?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] ken-voly commented on pull request #4299: bchlib: Improve error handling

Posted by GitBox <gi...@apache.org>.
ken-voly commented on pull request #4299:
URL: https://github.com/apache/incubator-nuttx/pull/4299#issuecomment-895361547


   > > There is also an unchecked return value of `bchlib_flushsector()` in `bch_close()`. Should we have the close fail if the flush fails?
   > 
   > close is a special one, we have to continue the release process
   
   Sounds good. I was worried that `close()` might be a special case because changing `close()` might result in a file descriptor that could not be closed.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 commented on pull request #4299: bchlib: Improve error handling

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on pull request #4299:
URL: https://github.com/apache/incubator-nuttx/pull/4299#issuecomment-895116000


   > There is also an unchecked return value of `bchlib_flushsector()` in `bch_close()`. Should we have the close fail if the flush fails?
   
   close is a special one, we have to continue the release process


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [incubator-nuttx] xiaoxiang781216 merged pull request #4299: bchlib: Improve error handling

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 merged pull request #4299:
URL: https://github.com/apache/incubator-nuttx/pull/4299


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org