scsi: mpi3mr: Fix error return, start_time, and timeout check in mpi3mr_bring_ioc_ready()
Subsystems (3)
v2 cover scsi: mpi3mr: Fix error return, start_time, and timeout check in mpi3mr_bring_ioc_ready()
Split the mpi3mr_bring_ioc_ready() error-handling and retry fixes into three independent patches with individual Fixes: tags as requested by Laurence Oberman:
1. Return -1 instead of 0 on failure exits in mpi3mr_bring_ioc_ready() when retval is 0 (such as unrecoverable controller faults, hot-unplug during reset/diag-save waits, or ready-state faults and timeouts after mpi3mr_setup_admin_qpair() sets retval = 0).
2. Initialize start_time = jiffies on entry and guard the post-enable timestamp reset with a boolean flag so pre-enable failures and subsequent retries never read an uninitialized start_time.
3. Rewrite 'elapsed_time_sec < (mrioc->ready_timeout - 60)' as 'elapsed_time_sec + 60 < mrioc->ready_timeout' to avoid 32-bit unsigned underflow if a controller reports ready_timeout < 60.
v2:
- Split v1 into three patches, each with its own Fixes: tag (Laurence Oberman).
- Move 'if (!retval) retval = -1;' to 'out_device_not_present:' in patch 1 so a single hunk covers both out_failed fallthroughs and early out_device_not_present jumps cleanly for stable backports.
- Add Laurence's Reviewed-by tag on patches 1 and 2, and note the
510s (570s - 60s) firmware timeout math from commit 9634bb07083c in
patch 3.v1: https://lore.kernel.org/r/[email protected]
Ian Rogers (3):
scsi: mpi3mr: Return error on all failure exits in
mpi3mr_bring_ioc_ready()
scsi: mpi3mr: Initialize start_time on all paths in
mpi3mr_bring_ioc_ready()
scsi: mpi3mr: Avoid ready_timeout underflow in
mpi3mr_bring_ioc_ready()
drivers/scsi/mpi3mr/mpi3mr_fw.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
PATCH v2 1/3 scsi: mpi3mr: Return error on all failure exits in mpi3mr_bring_ioc_ready()
In mpi3mr_bring_ioc_ready(), all success paths return 0 directly, while all failure paths either fall through out_failed or jump to out_device_not_present and return retval.
However, retval is initialized to 0 at function entry and overwritten with 0 once mpi3mr_setup_admin_qpair() succeeds. As a result, failure paths that do not set retval themselves - including unrecoverable fault checks, PCI device removal during reset or diagnostic-save waits, failure to reach MRIOC_STATE_RESET, and ready-loop fault/reset-history exits or timeouts - return 0 (success) to mpi3mr_init_ioc() and mpi3mr_reinit_ioc(), causing initialization to proceed against a failed or absent controller.
Set retval = -1 at out_device_not_present whenever retval is 0 so every failure exit returns a non-zero error while preserving specific error codes from helper calls.
Found during an x86_64 LLVM=1 allyesconfig warning audit with AI coding assistant help. Verified with a standalone C logic reproducer and by building drivers/scsi/mpi3mr/mpi3mr_fw.o with LLVM=1 W=1 (-Werror); not runtime-tested on MPI3 hardware.
--- drivers/scsi/mpi3mr/mpi3mr_fw.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/scsi/mpi3mr/mpi3mr_fw.c +2 -0
@@ -1744,6 +1744,8 @@ out_failed:"failed to bring to ready state, current state: %s\n" , mpi3mr_iocstate_name(ioc_state)); out_device_not_present: +if (!retval) + retval =-1 ;return retval; }
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
PATCH v2 2/3 scsi: mpi3mr: Initialize start_time on all paths in mpi3mr_bring_ioc_ready()
In reply to Ian RogersIn mpi3mr_bring_ioc_ready(), start_time is only assigned after writing MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC when retry == 0, which sits after three earlier 'goto out_failed' error paths (soft reset failure, failure to reach MRIOC_STATE_RESET, and admin queue setup failure).
If any of those pre-enable steps fails on the first attempt, out_failed computes elapsed_time_sec from an uninitialized start_time. Furthermore, if out_failed decides to retry (retry == 1) and the second attempt reaches the ready-polling loop, 'if (retry == 0)' skips assigning start_time, leaving the ready-polling loop comparing against uninitialized stack memory as well.
Initialize start_time = jiffies at function entry to cover pre-enable failure exits, and replace 'if (retry == 0)' with a boolean enable_started check so start_time is reset the first time IOC enable is written without pre-enable waits draining ready_timeout.
Found via Clang -Wconditional-uninitialized on an x86_64 allyesconfig build with AI coding assistant help. Verified with a standalone C reproducer under ASan/UBSan and by building drivers/scsi/mpi3mr/mpi3mr_fw.o with LLVM=1 W=1 (-Werror) and -Wconditional-uninitialized; not runtime-tested on MPI3 hardware.
--- drivers/scsi/mpi3mr/mpi3mr_fw.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/scsi/mpi3mr/mpi3mr_fw.c +5 -2
@@ -1582,8 +1582,9 @@ static int mpi3mr_bring_ioc_ready(struct mpi3mr_ioc *mrioc)int retval =0 ;enum mpi3mr_iocstate ioc_state;u64 base_info; +bool enable_started = false;u8 retry =0 ; -u64 start_time, elapsed_time_sec; +u64 start_time = jiffies, elapsed_time_sec; retry_bring_ioc_ready:
@@ -1690,8 +1691,10 @@ retry_bring_ioc_ready: ioc_config |=MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC ;writel (ioc_config, &mrioc->sysif_regs->ioc_configuration); -if (retry ==0 ) +if (!enable_started) { start_time = jiffies; + enable_started = true; + } timeout = mrioc->ready_timeout *10 ; do {
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
PATCH v2 3/3 scsi: mpi3mr: Avoid ready_timeout underflow in mpi3mr_bring_ioc_ready()
In reply to Ian RogersCommit 9634bb07083c ("scsi: mpi3mr: Enhance the Enable Controller retry logic") caps cumulative enable retries at 'mrioc->ready_timeout - 60' (510 seconds on firmware advertising a 570-second ready timeout) so a retry is not started with less than 60 seconds remaining.
Because mrioc->ready_timeout is a u32 read from the controller's ioc_information register, any value below 60 (for example 0 from a broken or disconnected controller) wraps around in 32-bit unsigned arithmetic (e.g. 0xffffffc4) before zero-extending to u64 for the comparison with elapsed_time_sec, spuriously retrying instead of aborting.
Rewrite the check as 'elapsed_time_sec + 60 < mrioc->ready_timeout' to avoid unsigned underflow while preserving the 60-second margin when ready_timeout >= 60. (Note that if any firmware legitimately advertises ready_timeout < 60, no retries will be attempted.)
Found during code inspection alongside a Clang warning audit with AI coding assistant help. Verified with a standalone C reproducer under UBSan and by building drivers/scsi/mpi3mr/mpi3mr_fw.o with LLVM=1 W=1 (-Werror); not runtime-tested on MPI3 hardware.
--- drivers/scsi/mpi3mr/mpi3mr_fw.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/scsi/mpi3mr/mpi3mr_fw.c +1 -1
@@ -1734,7 +1734,7 @@ retry_bring_ioc_ready: out_failed: elapsed_time_sec =jiffies_to_msecs (jiffies - start_time)/1000 ; -if ((retry <2 ) && (elapsed_time_sec < (mrioc->ready_timeout -60 ))) { +if (retry <2 && elapsed_time_sec +60 < mrioc->ready_timeout) { retry++; ioc_warn(mrioc, "retrying to bring IOC ready, retry_count:%d\n"
Signature · 1 line
2.56.1.750.gf70dc682cf-goog
Quoted 41 lines · Ian Rogers
Split the
mpi3mr_bring_ioc_ready()error-handling and retry fixes into
three independent patches with individual Fixes: tags as requested by Laurence Oberman:1. Return -1 instead of 0 on failure exits in
mpi3mr_bring_ioc_ready()
when retval is 0 (such as unrecoverable controller faults, hot- unplug
during reset/diag-save waits, or ready-state faults and timeouts aftermpi3mr_setup_admin_qpair()sets retval = 0).
2. Initializestart_time= jiffies on entry and guard the post-enable timestamp reset with a boolean flag so pre-enable failures and subsequent retries never read an uninitializedstart_time.
3. Rewrite 'elapsed_time_sec< (mrioc->ready_timeout- 60)' as 'elapsed_time_sec+ 60 <mrioc->ready_timeout' to avoid 32-bit unsigned underflow if a controller reportsready_timeout< 60.v2:
- Split v1 into three patches, each with its own Fixes: tag (Laurence
Oberman).
- Move 'if (!retval) retval = -1;' to 'out_device_not_present:' in patch 1 so a single hunk covers bothout_failedfallthroughs and earlyout_device_not_presentjumps cleanly for stable backports.
- Add Laurence's Reviewed-by tag on patches 1 and 2, and note the 510s (570s - 60s) firmware timeout math from commit9634bb07083cin
patch 3.v1:
https://lore.kernel.org/r/[email protected]Ian Rogers (3):
scsi: mpi3mr: Return error on all failure exits inmpi3mr_bring_ioc_ready()
scsi: mpi3mr: Initializestart_timeon all paths inmpi3mr_bring_ioc_ready()
scsi: mpi3mr: Avoidready_timeoutunderflow inmpi3mr_bring_ioc_ready()
Quoted 2 lines
drivers/scsi/mpi3mr/mpi3mr_fw.c | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)
Hello Ian
Thanks for the quick v2. Tested the series on 7.4.0-rc1 with a Broadcom SAS40xx/41xx (FW 8.5.1.0.0.1), two SAS disks attached.
Firmware advertises "ready timeout: 510 seconds" (ioc_info 0xff000000), before and after the series.
Tests, all with mpi3mr_bring_ioc_ready() reaching ready on the first attempt and no "retrying to bring IOC ready" or "failed to bring" messages:
- boot probe
- modprobe -r / modprobe (mpi3mr_init_ioc path)
- sg_reset -H under fio 4k randread QD32 (mpi3mr_reinit_ioc path); host reset completed in ~45s including diag save and port enable, fio completed with no errors
- PCI remove/rescan of the controller
- 20 iterations of reload + host reset under fio: 40 successful ready transitions, no retries, no failures, no fio errors
The failure paths themselves were not exercised, as they are hard to trigger on healthy hardware; this confirms no regression on the good path.
Minor, 3/3 changelog: the IOC info timeout field is 8 bits in 2s units, so ready_timeout tops out at 510s and the retry window at 450s; the "510 seconds" in 9634bb07083c most likely refers to the maximum ready timeout rather than the retry cap, so 570s cannot be advertised. Doesn't affect the code change. Martin may be able to fix it up on apply.
The question to Broadcom on firmware advertising ready_timeout < 60 is still open for 3/3; on this controller it makes no difference.
For the series:
Regards
Laurence