diff options
| author | Leah Rowe <leah@libreboot.org> | 2026-03-12 16:24:36 +0000 |
|---|---|---|
| committer | Leah Rowe <leah@libreboot.org> | 2026-03-12 16:24:36 +0000 |
| commit | 303c382eae77fbca2c087db9c6597a7c2d6658ec (patch) | |
| tree | 831ffd88b8c0cc40376363134794622a5049af30 | |
| parent | 9656e78c3ad8bf18cbaea56c57aef7ac969de174 (diff) | |
util/nvmutil: implement zero-byte r/w timeout
Signed-off-by: Leah Rowe <leah@libreboot.org>
| -rw-r--r-- | util/nvmutil/nvmutil.c | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/util/nvmutil/nvmutil.c b/util/nvmutil/nvmutil.c index e8c6ae2e..240cb73d 100644 --- a/util/nvmutil/nvmutil.c +++ b/util/nvmutil/nvmutil.c @@ -1461,6 +1461,9 @@ rw_file_once(int fd, uint8_t *mem, size_t len, off_t off, int rw_type, size_t rc) { ssize_t rv; + size_t retries_on_zero = 0; + size_t max_retries = 10; + read_again: rv = do_rw(fd, mem + rc, len - rc, off + rc, rw_type); @@ -1479,17 +1482,17 @@ read_again: */ if ((size_t)rv > (len - rc) /* Prevent overflow */ || rv == 0) { /* Prevent infinite 0-byte loop */ - /* - * TODO: handle rv == 0 this way: - * re-try a finite number of times, - * based on a counter, that resets - * on a non-zero read but then returns - * like below if the counter reaches - * the limit. This will retain the - * current safety, while increasing - * robustness e.g. on unreliable NFS - * shares or really slow media. - */ + if (rv == 0) { + /* + * Fault tolerance against infinite + * zero-byte loop: re-try a finite + * number of times. This mitigates + * otherwise OK but slow filesystems + * e.g. NFS or slow media. + */ + if (retries_on_zero++ < max_retries) + goto read_again; + } errno = EIO; return -1; } |
