Skip to contentSkip to conversation

scsi: core: Add a precondition check in scsi_eh_scmd_add()

PATCH Applied Bart Van Assche 3 participants 4 messages Nov 15, 2023 – Nov 25, 2023 SCSI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scsi: core: Add a precondition check in scsi_eh_scmd_add()

Calling scsi_eh_scmd_add() may cause the error handler never to be woken up because this may result in shost->host_failed to become larger than scsi_host_busy(shost). Hence complain if scsi_eh_scmd_add() is called after SCMD_STATE_INFLIGHT has been cleared.

Cc Hannes Reinecke <[email protected]>
Cc Damien Le Moal <[email protected]>
Cc Mike Christie <[email protected]>
Cc John Garry <[email protected]>
Cc Ming Lei <[email protected]>
---
 drivers/scsi/scsi_error.c | 1 +
 1 file changed, 1 insertion(+)
drivers/scsi/scsi_error.c +1 -0
index d7f2d90719fd..0734b3f30ef5 100644
@@ -290,6 +290,7 @@ void scsi_eh_scmd_add(struct scsi_cmnd *scmd)
 	int ret;
 
 	WARN_ON_ONCE(!shost->ehandler);
+	WARN_ON_ONCE(!test_bit(SCMD_STATE_INFLIGHT, &scmd->state));
 
 	spin_lock_irqsave(shost->host_lock, flags);
 	if (scsi_host_set_state(shost, SHOST_RECOVERY)) {
#1 lore raw
Quoted 3 lines · Bart Van Assche

Calling scsi_eh_scmd_add() may cause the error handler never to be woken up because this may result in shost->host_failed to become larger than scsi_host_busy(shost).

This is oddly worded. I think that you need to mention how calling scsi_eh_scmd_add() may lead to this scenario occurring.

Quoted 2 lines

Hence complain if scsi_eh_scmd_add() is called
after SCMD_STATE_INFLIGHT has been cleared.

Now you hint that this mentioned scenario may occur if SCMD_STATE_INFLIGHT was cleared.

Can you provide some info on when scsi_eh_scmd_add() could be called for SCMD_STATE_INFLIGHT cleared? Or is it that you don't know how (it may occur), but it is fatal if it does and we should guard against or warn about it.

Quoted 16 lines
Cc Hannes Reinecke <[email protected]>
Cc Damien Le Moal <[email protected]>
Cc Mike Christie <[email protected]>
Cc John Garry <[email protected]>
Cc Ming Lei <[email protected]>
---
  drivers/scsi/scsi_error.c | 1 +
  1 file changed, 1 insertion(+)
drivers/scsi/scsi_error.c +0 -0
index d7f2d90719fd..0734b3f30ef5 100644

drivers/scsi/scsi_error.c void scsi_eh_scmd_add(struct scsi_cmnd *scmd) · line 293

Quoted 4 lines
drivers/scsi/scsi_error.c +1 -0
@@ -290,6 +290,7 @@ void scsi_eh_scmd_add(struct scsi_cmnd *scmd)
  	int ret;
 
  	WARN_ON_ONCE(!shost->ehandler);
+	WARN_ON_ONCE(!test_bit(SCMD_STATE_INFLIGHT, &scmd->state));

What about if SCMD_STATE_COMPLETE is set - should we also warn about that?

drivers/scsi/scsi_error.c void scsi_eh_scmd_add(struct scsi_cmnd *scmd) · line 296

Quoted 3 lines
drivers/scsi/scsi_error.c +0 -0
  	spin_lock_irqsave(shost->host_lock, flags);
  	if (scsi_host_set_state(shost, SHOST_RECOVERY)) {

Thanks,
John

#2 lore raw
Quoted 7 lines · John Garry
Quoted 3 lines · Bart Van Assche

Calling scsi_eh_scmd_add() may cause the error handler never to be woken up because this may result in shost->host_failed to become larger than scsi_host_busy(shost).

This is oddly worded. I think that you need to mention how calling scsi_eh_scmd_add() may lead to this scenario occurring.

This happened in development code that was never posted on any mailing list. It took me a while to root-cause that issue. Hence this patch.

Quoted 6 lines
Quoted 2 lines

Hence complain if scsi_eh_scmd_add() is called
after SCMD_STATE_INFLIGHT has been cleared.

Now you hint that this mentioned scenario may occur if SCMD_STATE_INFLIGHT was cleared.

Can you provide some info on when scsi_eh_scmd_add() could be called for SCMD_STATE_INFLIGHT cleared? Or is it that you don't know how (it may occur), but it is fatal if it does and we should guard against or warn about it.

If anyone would add a call to scsi_eh_scmd_add() for a command for which SCMD_STATE_INFLIGHT is not set.

Quoted 4 lines
Quoted 2 lines

      WARN_ON_ONCE(!shost->ehandler);
+    WARN_ON_ONCE(!test_bit(SCMD_STATE_INFLIGHT, &scmd->state));

What about if SCMD_STATE_COMPLETE is set - should we also warn about that?

Calls to scsi_eh_scmd_add() for commands for which SCMD_STATE_COMPLETE has been set are much easier to find by reviewing the code than calls for commands for which neither SCMD_STATE_COMPLETE nor SCMD_STATE_INFLIGHT have been set.

Thanks,

Bart.

#3 lore raw
In reply to Bart Van Assche

Bart,

Quoted 5 lines

Calling scsi_eh_scmd_add() may cause the error handler never to be woken up because this may result in shost->host_failed to become larger than scsi_host_busy(shost). Hence complain if scsi_eh_scmd_add() is called after SCMD_STATE_INFLIGHT has been cleared.

Applied to 6.8/scsi-staging, thanks!

Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering