Skip to content

Commit 3bd6f43

Browse files
KAGA-KOKOmartinkpetersen
authored andcommitted
scsi: core: Ensure that the SCSI error handler gets woken up
If scsi_eh_scmd_add() is called concurrently with scsi_host_queue_ready() while shost->host_blocked > 0 then it can happen that neither function wakes up the SCSI error handler. Fix this by making every function that decreases the host_busy counter wake up the error handler if necessary and by protecting the host_failed checks with the SCSI host lock. Reported-by: Pavel Tikhomirov <ptikhomirov@virtuozzo.com> References: https://marc.info/?l=linux-kernel&m=150461610630736 Fixes: commit 7466501 ("scsi: convert host_busy to atomic_t") Signed-off-by: Bart Van Assche <bart.vanassche@wdc.com> Reviewed-by: Pavel Tikhomirov <ptikhomirov@virtuozzo.com> Tested-by: Stuart Hayes <stuart.w.hayes@gmail.com> Cc: Konstantin Khorenko <khorenko@virtuozzo.com> Cc: Stuart Hayes <stuart.w.hayes@gmail.com> Cc: Pavel Tikhomirov <ptikhomirov@virtuozzo.com> Cc: Christoph Hellwig <hch@lst.de> Cc: Hannes Reinecke <hare@suse.com> Cc: Johannes Thumshirn <jthumshirn@suse.de> Cc: <stable@vger.kernel.org> Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
1 parent 156baec commit 3bd6f43

File tree

4 files changed

+52
-13
lines changed

4 files changed

+52
-13
lines changed

drivers/scsi/hosts.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,13 +318,18 @@ static void scsi_host_dev_release(struct device *dev)
318318

319319
scsi_proc_hostdir_rm(shost->hostt);
320320

321+
/* Wait for functions invoked through call_rcu(&shost->rcu, ...) */
322+
rcu_barrier();
323+
321324
if (shost->tmf_work_q)
322325
destroy_workqueue(shost->tmf_work_q);
323326
if (shost->ehandler)
324327
kthread_stop(shost->ehandler);
325328
if (shost->work_q)
326329
destroy_workqueue(shost->work_q);
327330

331+
destroy_rcu_head(&shost->rcu);
332+
328333
if (shost->shost_state == SHOST_CREATED) {
329334
/*
330335
* Free the shost_dev device name here if scsi_host_alloc()
@@ -399,6 +404,7 @@ struct Scsi_Host *scsi_host_alloc(struct scsi_host_template *sht, int privsize)
399404
INIT_LIST_HEAD(&shost->starved_list);
400405
init_waitqueue_head(&shost->host_wait);
401406
mutex_init(&shost->scan_mutex);
407+
init_rcu_head(&shost->rcu);
402408

403409
index = ida_simple_get(&host_index_ida, 0, 0, GFP_KERNEL);
404410
if (index < 0)

drivers/scsi/scsi_error.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,17 @@ static void scsi_eh_reset(struct scsi_cmnd *scmd)
220220
}
221221
}
222222

223+
static void scsi_eh_inc_host_failed(struct rcu_head *head)
224+
{
225+
struct Scsi_Host *shost = container_of(head, typeof(*shost), rcu);
226+
unsigned long flags;
227+
228+
spin_lock_irqsave(shost->host_lock, flags);
229+
shost->host_failed++;
230+
scsi_eh_wakeup(shost);
231+
spin_unlock_irqrestore(shost->host_lock, flags);
232+
}
233+
223234
/**
224235
* scsi_eh_scmd_add - add scsi cmd to error handling.
225236
* @scmd: scmd to run eh on.
@@ -242,9 +253,12 @@ void scsi_eh_scmd_add(struct scsi_cmnd *scmd)
242253

243254
scsi_eh_reset(scmd);
244255
list_add_tail(&scmd->eh_entry, &shost->eh_cmd_q);
245-
shost->host_failed++;
246-
scsi_eh_wakeup(shost);
247256
spin_unlock_irqrestore(shost->host_lock, flags);
257+
/*
258+
* Ensure that all tasks observe the host state change before the
259+
* host_failed change.
260+
*/
261+
call_rcu(&shost->rcu, scsi_eh_inc_host_failed);
248262
}
249263

250264
/**

drivers/scsi/scsi_lib.c

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -318,22 +318,39 @@ static void scsi_init_cmd_errh(struct scsi_cmnd *cmd)
318318
cmd->cmd_len = scsi_command_size(cmd->cmnd);
319319
}
320320

321-
void scsi_device_unbusy(struct scsi_device *sdev)
321+
/*
322+
* Decrement the host_busy counter and wake up the error handler if necessary.
323+
* Avoid as follows that the error handler is not woken up if shost->host_busy
324+
* == shost->host_failed: use call_rcu() in scsi_eh_scmd_add() in combination
325+
* with an RCU read lock in this function to ensure that this function in its
326+
* entirety either finishes before scsi_eh_scmd_add() increases the
327+
* host_failed counter or that it notices the shost state change made by
328+
* scsi_eh_scmd_add().
329+
*/
330+
static void scsi_dec_host_busy(struct Scsi_Host *shost)
322331
{
323-
struct Scsi_Host *shost = sdev->host;
324-
struct scsi_target *starget = scsi_target(sdev);
325332
unsigned long flags;
326333

334+
rcu_read_lock();
327335
atomic_dec(&shost->host_busy);
328-
if (starget->can_queue > 0)
329-
atomic_dec(&starget->target_busy);
330-
331-
if (unlikely(scsi_host_in_recovery(shost) &&
332-
(shost->host_failed || shost->host_eh_scheduled))) {
336+
if (unlikely(scsi_host_in_recovery(shost))) {
333337
spin_lock_irqsave(shost->host_lock, flags);
334-
scsi_eh_wakeup(shost);
338+
if (shost->host_failed || shost->host_eh_scheduled)
339+
scsi_eh_wakeup(shost);
335340
spin_unlock_irqrestore(shost->host_lock, flags);
336341
}
342+
rcu_read_unlock();
343+
}
344+
345+
void scsi_device_unbusy(struct scsi_device *sdev)
346+
{
347+
struct Scsi_Host *shost = sdev->host;
348+
struct scsi_target *starget = scsi_target(sdev);
349+
350+
scsi_dec_host_busy(shost);
351+
352+
if (starget->can_queue > 0)
353+
atomic_dec(&starget->target_busy);
337354

338355
atomic_dec(&sdev->device_busy);
339356
}
@@ -1532,7 +1549,7 @@ static inline int scsi_host_queue_ready(struct request_queue *q,
15321549
list_add_tail(&sdev->starved_entry, &shost->starved_list);
15331550
spin_unlock_irq(shost->host_lock);
15341551
out_dec:
1535-
atomic_dec(&shost->host_busy);
1552+
scsi_dec_host_busy(shost);
15361553
return 0;
15371554
}
15381555

@@ -2018,7 +2035,7 @@ static blk_status_t scsi_queue_rq(struct blk_mq_hw_ctx *hctx,
20182035
return BLK_STS_OK;
20192036

20202037
out_dec_host_busy:
2021-
atomic_dec(&shost->host_busy);
2038+
scsi_dec_host_busy(shost);
20222039
out_dec_target_busy:
20232040
if (scsi_target(sdev)->can_queue > 0)
20242041
atomic_dec(&scsi_target(sdev)->target_busy);

include/scsi/scsi_host.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,8 @@ struct Scsi_Host {
571571
struct blk_mq_tag_set tag_set;
572572
};
573573

574+
struct rcu_head rcu;
575+
574576
atomic_t host_busy; /* commands actually active on low-level */
575577
atomic_t host_blocked;
576578

0 commit comments

Comments
 (0)