hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/drivers/scsi/scsi_error.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * scsi_error.c Copyright (C) 1997 Eric Youngdale
34 *
....@@ -115,6 +116,14 @@
115116 return 1;
116117 }
117118
119
+static bool scsi_cmd_retry_allowed(struct scsi_cmnd *cmd)
120
+{
121
+ if (cmd->allowed == SCSI_CMD_RETRIES_NO_LIMIT)
122
+ return true;
123
+
124
+ return ++cmd->retries <= cmd->allowed;
125
+}
126
+
118127 /**
119128 * scmd_eh_abort_handler - Handle command aborts
120129 * @work: command to be aborted.
....@@ -150,7 +159,7 @@
150159 "eh timeout, not retrying "
151160 "aborted command\n"));
152161 } else if (!scsi_noretry_cmd(scmd) &&
153
- (++scmd->retries <= scmd->allowed)) {
162
+ scsi_cmd_retry_allowed(scmd)) {
154163 SCSI_LOG_ERROR_RECOVERY(3,
155164 scmd_printk(KERN_WARNING, scmd,
156165 "retry aborted command\n"));
....@@ -297,19 +306,11 @@
297306
298307 if (rtn == BLK_EH_DONE) {
299308 /*
300
- * For blk-mq, we must set the request state to complete now
301
- * before sending the request to the scsi error handler. This
302
- * will prevent a use-after-free in the event the LLD manages
303
- * to complete the request before the error handler finishes
304
- * processing this timed out request.
305
- *
306
- * If the request was already completed, then the LLD beat the
307
- * time out handler from transferring the request to the scsi
308
- * error handler. In that case we can return immediately as no
309
- * further action is required.
309
+ * If scsi_done() has already set SCMD_STATE_COMPLETE, do not
310
+ * modify *scmd.
310311 */
311
- if (req->q->mq_ops && !blk_mq_mark_complete(req))
312
- return rtn;
312
+ if (test_and_set_bit(SCMD_STATE_COMPLETE, &scmd->state))
313
+ return BLK_EH_DONE;
313314 if (scsi_abort_command(scmd) != SUCCESS) {
314315 set_host_byte(scmd, DID_TIME_OUT);
315316 scsi_eh_scmd_add(scmd);
....@@ -337,9 +338,6 @@
337338 wait_event(sdev->host->host_wait, !scsi_host_in_recovery(sdev->host));
338339
339340 online = scsi_device_online(sdev);
340
-
341
- SCSI_LOG_ERROR_RECOVERY(5, sdev_printk(KERN_INFO, sdev,
342
- "%s: rtn: %d\n", __func__, online));
343341
344342 return online;
345343 }
....@@ -601,7 +599,7 @@
601599 set_host_byte(scmd, DID_ALLOC_FAILURE);
602600 return SUCCESS;
603601 }
604
- /* FALLTHROUGH */
602
+ fallthrough;
605603 case COPY_ABORTED:
606604 case VOLUME_OVERFLOW:
607605 case MISCOMPARE:
....@@ -623,7 +621,7 @@
623621 return ADD_TO_MLQUEUE;
624622 else
625623 set_host_byte(scmd, DID_TARGET_FAILURE);
626
- /* FALLTHROUGH */
624
+ fallthrough;
627625
628626 case ILLEGAL_REQUEST:
629627 if (sshdr.asc == 0x20 || /* Invalid command operation code */
....@@ -736,7 +734,7 @@
736734 switch (status_byte(scmd->result)) {
737735 case GOOD:
738736 scsi_handle_queue_ramp_up(scmd->device);
739
- /* FALLTHROUGH */
737
+ fallthrough;
740738 case COMMAND_TERMINATED:
741739 return SUCCESS;
742740 case CHECK_CONDITION:
....@@ -757,7 +755,7 @@
757755 return FAILED;
758756 case QUEUE_FULL:
759757 scsi_handle_queue_full(scmd->device);
760
- /* fall through */
758
+ fallthrough;
761759 case BUSY:
762760 return NEEDS_RETRY;
763761 default:
....@@ -968,7 +966,6 @@
968966 ses->cmnd = scmd->cmnd;
969967 ses->data_direction = scmd->sc_data_direction;
970968 ses->sdb = scmd->sdb;
971
- ses->next_rq = scmd->request->next_rq;
972969 ses->result = scmd->result;
973970 ses->resid_len = scmd->req.resid_len;
974971 ses->underflow = scmd->underflow;
....@@ -980,7 +977,6 @@
980977 scmd->cmnd = ses->eh_cmnd;
981978 memset(scmd->cmnd, 0, BLK_MAX_CDB);
982979 memset(&scmd->sdb, 0, sizeof(scmd->sdb));
983
- scmd->request->next_rq = NULL;
984980 scmd->result = 0;
985981 scmd->req.resid_len = 0;
986982
....@@ -1034,7 +1030,6 @@
10341030 scmd->cmnd = ses->cmnd;
10351031 scmd->sc_data_direction = ses->data_direction;
10361032 scmd->sdb = ses->sdb;
1037
- scmd->request->next_rq = ses->next_rq;
10381033 scmd->result = ses->result;
10391034 scmd->req.resid_len = ses->resid_len;
10401035 scmd->underflow = ses->underflow;
....@@ -1063,7 +1058,7 @@
10631058 struct scsi_device *sdev = scmd->device;
10641059 struct Scsi_Host *shost = sdev->host;
10651060 DECLARE_COMPLETION_ONSTACK(done);
1066
- unsigned long timeleft = timeout;
1061
+ unsigned long timeleft = timeout, delay;
10671062 struct scsi_eh_save ses;
10681063 const unsigned long stall_for = msecs_to_jiffies(100);
10691064 int rtn;
....@@ -1074,7 +1069,29 @@
10741069
10751070 scsi_log_send(scmd);
10761071 scmd->scsi_done = scsi_eh_done;
1077
- rtn = shost->hostt->queuecommand(shost, scmd);
1072
+
1073
+ /*
1074
+ * Lock sdev->state_mutex to avoid that scsi_device_quiesce() can
1075
+ * change the SCSI device state after we have examined it and before
1076
+ * .queuecommand() is called.
1077
+ */
1078
+ mutex_lock(&sdev->state_mutex);
1079
+ while (sdev->sdev_state == SDEV_BLOCK && timeleft > 0) {
1080
+ mutex_unlock(&sdev->state_mutex);
1081
+ SCSI_LOG_ERROR_RECOVERY(5, sdev_printk(KERN_DEBUG, sdev,
1082
+ "%s: state %d <> %d\n", __func__, sdev->sdev_state,
1083
+ SDEV_BLOCK));
1084
+ delay = min(timeleft, stall_for);
1085
+ timeleft -= delay;
1086
+ msleep(jiffies_to_msecs(delay));
1087
+ mutex_lock(&sdev->state_mutex);
1088
+ }
1089
+ if (sdev->sdev_state != SDEV_BLOCK)
1090
+ rtn = shost->hostt->queuecommand(shost, scmd);
1091
+ else
1092
+ rtn = SCSI_MLQUEUE_DEVICE_BUSY;
1093
+ mutex_unlock(&sdev->state_mutex);
1094
+
10781095 if (rtn) {
10791096 if (timeleft > stall_for) {
10801097 scsi_eh_restore_cmnd(scmd, &ses);
....@@ -1247,11 +1264,18 @@
12471264 * upper level.
12481265 */
12491266 if (rtn == SUCCESS)
1250
- /* we don't want this command reissued, just
1251
- * finished with the sense data, so set
1252
- * retries to the max allowed to ensure it
1253
- * won't get reissued */
1254
- scmd->retries = scmd->allowed;
1267
+ /*
1268
+ * We don't want this command reissued, just finished
1269
+ * with the sense data, so set retries to the max
1270
+ * allowed to ensure it won't get reissued. If the user
1271
+ * has requested infinite retries, we also want to
1272
+ * finish this command, so force completion by setting
1273
+ * retries and allowed to the same value.
1274
+ */
1275
+ if (scmd->allowed == SCSI_CMD_RETRIES_NO_LIMIT)
1276
+ scmd->retries = scmd->allowed = 1;
1277
+ else
1278
+ scmd->retries = scmd->allowed;
12551279 else if (rtn != NEEDS_RETRY)
12561280 continue;
12571281
....@@ -1285,7 +1309,7 @@
12851309 case NEEDS_RETRY:
12861310 if (retry_cnt--)
12871311 goto retry_tur;
1288
- /*FALLTHRU*/
1312
+ fallthrough;
12891313 case SUCCESS:
12901314 return 0;
12911315 default:
....@@ -1395,6 +1419,7 @@
13951419 sdev_printk(KERN_INFO, sdev,
13961420 "%s: skip START_UNIT, past eh deadline\n",
13971421 current->comm));
1422
+ scsi_device_put(sdev);
13981423 break;
13991424 }
14001425 stu_scmd = NULL;
....@@ -1461,6 +1486,7 @@
14611486 sdev_printk(KERN_INFO, sdev,
14621487 "%s: skip BDR, past eh deadline\n",
14631488 current->comm));
1489
+ scsi_device_put(sdev);
14641490 break;
14651491 }
14661492 bdr_scmd = NULL;
....@@ -1720,7 +1746,7 @@
17201746 if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
17211747 status_byte(scmd->result) == RESERVATION_CONFLICT)
17221748 return 0;
1723
- /* fall through */
1749
+ fallthrough;
17241750 case DID_SOFT_ERROR:
17251751 return (scmd->request->cmd_flags & REQ_FAILFAST_DRIVER);
17261752 }
....@@ -1736,8 +1762,8 @@
17361762 if (scmd->request->cmd_flags & REQ_FAILFAST_DEV ||
17371763 blk_rq_is_passthrough(scmd->request))
17381764 return 1;
1739
- else
1740
- return 0;
1765
+
1766
+ return 0;
17411767 }
17421768
17431769 /**
....@@ -1791,7 +1817,7 @@
17911817 set_host_byte(scmd, DID_TIME_OUT);
17921818 return SUCCESS;
17931819 }
1794
- /* FALLTHROUGH */
1820
+ fallthrough;
17951821 case DID_NO_CONNECT:
17961822 case DID_BAD_TARGET:
17971823 /*
....@@ -1835,7 +1861,7 @@
18351861 * lower down
18361862 */
18371863 break;
1838
- /* fallthrough */
1864
+ fallthrough;
18391865 case DID_BUS_BUSY:
18401866 case DID_PARITY:
18411867 goto maybe_retry;
....@@ -1873,7 +1899,7 @@
18731899 * the case of trying to send too many commands to a
18741900 * tagged queueing device.
18751901 */
1876
- /* FALLTHROUGH */
1902
+ fallthrough;
18771903 case BUSY:
18781904 /*
18791905 * device can't talk to us at the moment. Should only
....@@ -1886,7 +1912,7 @@
18861912 if (scmd->cmnd[0] == REPORT_LUNS)
18871913 scmd->device->sdev_target->expecting_lun_change = 0;
18881914 scsi_handle_queue_ramp_up(scmd->device);
1889
- /* FALLTHROUGH */
1915
+ fallthrough;
18901916 case COMMAND_TERMINATED:
18911917 return SUCCESS;
18921918 case TASK_ABORTED:
....@@ -1925,8 +1951,7 @@
19251951 * the request was not marked fast fail. Note that above,
19261952 * even if the request is marked fast fail, we still requeue
19271953 * for queue congestion conditions (QUEUE_FULL or BUSY) */
1928
- if ((++scmd->retries) <= scmd->allowed
1929
- && !scsi_noretry_cmd(scmd)) {
1954
+ if (scsi_cmd_retry_allowed(scmd) && !scsi_noretry_cmd(scmd)) {
19301955 return NEEDS_RETRY;
19311956 } else {
19321957 /*
....@@ -1938,7 +1963,7 @@
19381963
19391964 static void eh_lock_door_done(struct request *req, blk_status_t status)
19401965 {
1941
- __blk_put_request(req->q, req);
1966
+ blk_put_request(req);
19421967 }
19431968
19441969 /**
....@@ -2072,8 +2097,7 @@
20722097 list_for_each_entry_safe(scmd, next, done_q, eh_entry) {
20732098 list_del_init(&scmd->eh_entry);
20742099 if (scsi_device_online(scmd->device) &&
2075
- !scsi_noretry_cmd(scmd) &&
2076
- (++scmd->retries <= scmd->allowed)) {
2100
+ !scsi_noretry_cmd(scmd) && scsi_cmd_retry_allowed(scmd)) {
20772101 SCSI_LOG_ERROR_RECOVERY(3,
20782102 scmd_printk(KERN_INFO, scmd,
20792103 "%s: flush retry cmd\n",
....@@ -2357,22 +2381,22 @@
23572381 rtn = scsi_try_bus_device_reset(scmd);
23582382 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
23592383 break;
2360
- /* FALLTHROUGH */
2384
+ fallthrough;
23612385 case SG_SCSI_RESET_TARGET:
23622386 rtn = scsi_try_target_reset(scmd);
23632387 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
23642388 break;
2365
- /* FALLTHROUGH */
2389
+ fallthrough;
23662390 case SG_SCSI_RESET_BUS:
23672391 rtn = scsi_try_bus_reset(scmd);
23682392 if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
23692393 break;
2370
- /* FALLTHROUGH */
2394
+ fallthrough;
23712395 case SG_SCSI_RESET_HOST:
23722396 rtn = scsi_try_host_reset(scmd);
23732397 if (rtn == SUCCESS)
23742398 break;
2375
- /* FALLTHROUGH */
2399
+ fallthrough;
23762400 default:
23772401 rtn = FAILED;
23782402 break;
....@@ -2395,14 +2419,12 @@
23952419 wake_up(&shost->host_wait);
23962420 scsi_run_host_queues(shost);
23972421
2398
- scsi_put_command(scmd);
23992422 kfree(rq);
24002423
24012424 out_put_autopm_host:
24022425 scsi_autopm_put_host(shost);
24032426 return error;
24042427 }
2405
-EXPORT_SYMBOL(scsi_ioctl_reset);
24062428
24072429 bool scsi_command_normalize_sense(const struct scsi_cmnd *cmd,
24082430 struct scsi_sense_hdr *sshdr)