Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

scsi: core: Fix a race between scsi_done() and scsi_timeout()

If there is a race between scsi_done() and scsi_timeout() and if
scsi_timeout() loses the race, scsi_timeout() should not reset the request
timer. Hence change the return value for this case from BLK_EH_RESET_TIMER
into BLK_EH_DONE.

Although the block layer holds a reference on a request (req->ref) while
calling a timeout handler, restarting the timer (blk_add_timer()) while a
request is being completed is racy.

Reviewed-by: Mike Christie <michael.christie@oracle.com>
Cc: Keith Busch <kbusch@kernel.org>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Ming Lei <ming.lei@redhat.com>
Cc: John Garry <john.garry@huawei.com>
Cc: Hannes Reinecke <hare@suse.de>
Reported-by: Adrian Hunter <adrian.hunter@intel.com>
Fixes: 15f73f5b3e59 ("blk-mq: move failure injection out of blk_mq_complete_request")
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
Link: https://lore.kernel.org/r/20221018202958.1902564-2-bvanassche@acm.org
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>

authored by

Bart Van Assche and committed by
Martin K. Petersen
978b7922 24b3e45c

+3 -11
+3 -11
drivers/scsi/scsi_error.c
··· 343 343 344 344 if (rtn == BLK_EH_DONE) { 345 345 /* 346 - * Set the command to complete first in order to prevent a real 347 - * completion from releasing the command while error handling 348 - * is using it. If the command was already completed, then the 349 - * lower level driver beat the timeout handler, and it is safe 350 - * to return without escalating error recovery. 351 - * 352 - * If timeout handling lost the race to a real completion, the 353 - * block layer may ignore that due to a fake timeout injection, 354 - * so return RESET_TIMER to allow error handling another shot 355 - * at this command. 346 + * If scsi_done() has already set SCMD_STATE_COMPLETE, do not 347 + * modify *scmd. 356 348 */ 357 349 if (test_and_set_bit(SCMD_STATE_COMPLETE, &scmd->state)) 358 - return BLK_EH_RESET_TIMER; 350 + return BLK_EH_DONE; 359 351 if (scsi_abort_command(scmd) != SUCCESS) { 360 352 set_host_byte(scmd, DID_TIME_OUT); 361 353 scsi_eh_scmd_add(scmd);