Skip to content

Commit fe2f8ad

Browse files
David Jefferymartinkpetersen
authored andcommitted
scsi: core: Wake up the error handler when final completions race against each other
The fragile ordering between marking commands completed or failed so that the error handler only wakes when the last running command completes or times out has race conditions. These race conditions can cause the SCSI layer to fail to wake the error handler, leaving I/O through the SCSI host stuck as the error state cannot advance. First, there is an memory ordering issue within scsi_dec_host_busy(). The write which clears SCMD_STATE_INFLIGHT may be reordered with reads counting in scsi_host_busy(). While the local CPU will see its own write, reordering can allow other CPUs in scsi_dec_host_busy() or scsi_eh_inc_host_failed() to see a raised busy count, causing no CPU to see a host busy equal to the host_failed count. This race condition can be prevented with a memory barrier on the error path to force the write to be visible before counting host busy commands. Second, there is a general ordering issue with scsi_eh_inc_host_failed(). By counting busy commands before incrementing host_failed, it can race with a final command in scsi_dec_host_busy(), such that scsi_dec_host_busy() does not see host_failed incremented but scsi_eh_inc_host_failed() counts busy commands before SCMD_STATE_INFLIGHT is cleared by scsi_dec_host_busy(), resulting in neither waking the error handler task. This needs the call to scsi_host_busy() to be moved after host_failed is incremented to close the race condition. Fixes: 6eb045e ("scsi: core: avoid host-wide host_busy counter for scsi_mq") Signed-off-by: David Jeffery <djeffery@redhat.com> Reviewed-by: Bart Van Assche <bvanassche@acm.org> Link: https://patch.msgid.link/20260113161036.6730-1-djeffery@redhat.com Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
1 parent 9eacec5 commit fe2f8ad

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

drivers/scsi/scsi_error.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,20 @@ static void scsi_eh_inc_host_failed(struct rcu_head *head)
282282
{
283283
struct scsi_cmnd *scmd = container_of(head, typeof(*scmd), rcu);
284284
struct Scsi_Host *shost = scmd->device->host;
285-
unsigned int busy = scsi_host_busy(shost);
285+
unsigned int busy;
286286
unsigned long flags;
287287

288288
spin_lock_irqsave(shost->host_lock, flags);
289289
shost->host_failed++;
290+
spin_unlock_irqrestore(shost->host_lock, flags);
291+
/*
292+
* The counting of busy requests needs to occur after adding to
293+
* host_failed or after the lock acquire for adding to host_failed
294+
* to prevent a race with host unbusy and missing an eh wakeup.
295+
*/
296+
busy = scsi_host_busy(shost);
297+
298+
spin_lock_irqsave(shost->host_lock, flags);
290299
scsi_eh_wakeup(shost, busy);
291300
spin_unlock_irqrestore(shost->host_lock, flags);
292301
}

drivers/scsi/scsi_lib.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,14 @@ static void scsi_dec_host_busy(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
376376
rcu_read_lock();
377377
__clear_bit(SCMD_STATE_INFLIGHT, &cmd->state);
378378
if (unlikely(scsi_host_in_recovery(shost))) {
379+
/*
380+
* Ensure the clear of SCMD_STATE_INFLIGHT is visible to
381+
* other CPUs before counting busy requests. Otherwise,
382+
* reordering can cause CPUs to race and miss an eh wakeup
383+
* when no CPU sees all busy requests as done or timed out.
384+
*/
385+
smp_mb();
386+
379387
unsigned int busy = scsi_host_busy(shost);
380388

381389
spin_lock_irqsave(shost->host_lock, flags);

0 commit comments

Comments
 (0)