ata: libata-eh: Fix detection of deferred qc timeouts
PATCH 1/1 ata: libata-eh: Fix detection of deferred qc timeouts
If the ata_qc_for_each_raw() loop finishes without finding a matching scmd, qc will hold a pointer to the last element examined, which is accociated with i == ATA_MAX_QUEUE - 1. This element can match ap->deferred_qc. If that happens, the condition qc == ap->deferred_qc evaluates to true despite the loop not breaking on a scmd match. In that case, the error handler mistakenly intercepts a command that completed normally after an unrelated SCSI timeout, returning a timeout error instead of success.
Fix the problem by checking for i < ATA_MAX_QUEUE in addition to qc == ap->deferred_qc.
The problem was found by an experimental code review agent based on gemini-3.1-pro while reviewing backports into v6.18.y.
--- drivers/ata/libata-eh.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/ata/libata-eh.c +1 -1
@@ -647,7 +647,7 @@ void ata_scsi_cmd_error_handler(struct Scsi_Host *host, struct ata_port *ap,break ; } -if (qc == ap->deferred_qc) { +if (i <ATA_MAX_QUEUE && qc == ap->deferred_qc) {/* * This is a deferred command that timed out while * waiting for the command queue to drain. Since the qc
Signature · 1 line
2.45.2
Quoted 9 lines · Guenter Roeck
If the
ata_qc_for_each_raw()loop finishes without finding a matchingscmd,qcwill hold a pointer to the last element examined, which is accociated withi == ATA_MAX_QUEUE - 1. This element can matchap->deferred_qc. If that happens, the conditionqc == ap->deferred_qcevaluates to true despite the loop not breaking on a scmd match. In that case, the error handler mistakenly intercepts a command that completed normally after an unrelated SCSI timeout, returning a timeout error instead of success.
Quoted 5 lines
Fix the problem by checking for i <
ATA_MAX_QUEUEin addition to qc ==ap->deferred_qc.The problem was found by an experimental code review agent based on gemini-3.1-pro while reviewing backports into v6.18.y.
Please remove all the quotes:
If the ata_qc_for_each_raw() loop finishes without finding a matching SCSI command for any QC, the variable qc will hold a pointer to the last element examined, which has the tag i == ATA_MAX_QUEUE - 1. This qc can match the port deferred QC (ap->deferred_qc).
If that happens, the condition qc == ap->deferred_qc evaluates to true despite the loop not breaking with a match on the SCSI command for this QC. In that case, the error handler mistakenly intercepts a command that not completed yet and that has not timed out, and thus erroneously returning a timeout error.
Fix the problem by checking for i < ATA_MAX_QUEUE in addition to qc == ap->deferred_qc.
The problem was found by an experimental code review agent based on gemini-3.1-pro while reviewing backports into v6.18.y.
Quoted 5 lines
Assisted-by Gemini:gemini-3.1-proCc Damien Le Moal <[email protected]>Cc Niklas Cassel <[email protected]>Fixes eddb98ad9364 ("ata: libata-eh: correctly handle deferred qc timeouts")Signed-off-by Guenter Roeck <[email protected]>
With the above, feel free to add:
Thanks !
Signature · 2 lines
Damien Le Moal Western Digital Research
Niklas Cassel
maintainerQuoted 18 lines · Damien Le Moal
Quoted 9 lines · Guenter Roeck
If the
ata_qc_for_each_raw()loop finishes without finding a matchingscmd,qcwill hold a pointer to the last element examined, which is accociated withi == ATA_MAX_QUEUE - 1. This element can matchap->deferred_qc. If that happens, the conditionqc == ap->deferred_qcevaluates to true despite the loop not breaking on a scmd match. In that case, the error handler mistakenly intercepts a command that completed normally after an unrelated SCSI timeout, returning a timeout error instead of success.Quoted 5 lines
Fix the problem by checking for i <
ATA_MAX_QUEUEin addition to qc ==ap->deferred_qc.The problem was found by an experimental code review agent based on gemini-3.1-pro while reviewing backports into v6.18.y.
Please remove all the quotes:
Quoted 8 lines
If the
ata_qc_for_each_raw()loop finishes without finding a matching SCSI command for any QC, the variable qc will hold a pointer to the last element examined, which has the tag i ==ATA_MAX_QUEUE- 1. This qc can match the port deferred QC (ap->deferred_qc).If that happens, the condition qc ==
ap->deferred_qcevaluates to true despite the loop not breaking with a match on the SCSI command for this QC. In that case, the error handler mistakenly intercepts a command that not completed yet
Could even write:
"that has not been issued yet"
(while it is true that it has not been completed yet, it has not even been issued to the drive yet.)
Quoted 23 lines
and that has not timed out, and thus erroneously returning a timeout error.
Fix the problem by checking for i <
ATA_MAX_QUEUEin addition to qc ==ap->deferred_qc.The problem was found by an experimental code review agent based on gemini-3.1-pro while reviewing backports into v6.18.y.
Quoted 5 lines
Assisted-by Gemini:gemini-3.1-proCc Damien Le Moal <[email protected]>Cc Niklas Cassel <[email protected]>Fixes eddb98ad9364 ("ata: libata-eh: correctly handle deferred qc timeouts")Signed-off-by Guenter Roeck <[email protected]>With the above, feel free to add:
Reviewed-by Damien Le Moal <[email protected]>Thanks !
Signature · 2 lines
Damien Le Moal Western Digital Research
Quoted 10 lines · Guenter Roeck
If the
ata_qc_for_each_raw()loop finishes without finding a matchingscmd,qcwill hold a pointer to the last element examined, which is accociated withi == ATA_MAX_QUEUE - 1. This element can matchap->deferred_qc. If that happens, the conditionqc == ap->deferred_qcevaluates to true despite the loop not breaking on a scmd match. In that case, the error handler mistakenly intercepts a command that completed normally after an unrelated SCSI timeout, returning a timeout error instead of success.[...]
Applied commit log changes from Damien and myself.
Applied to libata/linux.git (libata-for-7.0-fixes), thanks!
[1/1] ata: libata-eh: Fix detection of deferred qc timeouts
https://git.kernel.org/libata/linux/c/ee0e6e69
Kind regards,
Niklas
Quoted 10 lines · Niklas Cassel
Quoted 9 lines · Guenter Roeck
If the
ata_qc_for_each_raw()loop finishes without finding a matchingscmd,qcwill hold a pointer to the last element examined, which is accociated withi == ATA_MAX_QUEUE - 1. This element can matchap->deferred_qc. If that happens, the conditionqc == ap->deferred_qcevaluates to true despite the loop not breaking on a scmd match. In that case, the error handler mistakenly intercepts a command that completed normally after an unrelated SCSI timeout, returning a timeout error instead of success.
Quoted 7 lines
Quoted 1 line
[...]
Applied commit log changes from Damien and myself.
Applied to libata/linux.git (libata-for-7.0-fixes), thanks!
Thanks!
Guenter