Skip to content

Commit cd9070c

Browse files
author
Christoph Hellwig
committed
scsi: fix the {host,target,device}_blocked counter mess
Seems like these counters are missing any sort of synchronization for updates, as a over 10 year old comment from me noted. Fix this by using atomic counters, and while we're at it also make sure they are in the same cacheline as the _busy counters and not needlessly stored to in every I/O completion. With the new model the _busy counters can temporarily go negative, so all the readers are updated to check for > 0 values. Longer term every successful I/O completion will reset the counters to zero, so the temporarily negative values will not cause any harm. Signed-off-by: Christoph Hellwig <[email protected]> Reviewed-by: Martin K. Petersen <[email protected]> Reviewed-by: Webb Scales <[email protected]> Acked-by: Jens Axboe <[email protected]> Tested-by: Bart Van Assche <[email protected]> Tested-by: Robert Elliott <[email protected]>
1 parent 71e75c9 commit cd9070c

File tree

5 files changed

+58
-53
lines changed

5 files changed

+58
-53
lines changed

drivers/scsi/scsi.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -726,17 +726,16 @@ void scsi_finish_command(struct scsi_cmnd *cmd)
726726

727727
scsi_device_unbusy(sdev);
728728

729-
/*
730-
* Clear the flags which say that the device/host is no longer
731-
* capable of accepting new commands. These are set in scsi_queue.c
732-
* for both the queue full condition on a device, and for a
733-
* host full condition on the host.
734-
*
735-
* XXX(hch): What about locking?
736-
*/
737-
shost->host_blocked = 0;
738-
starget->target_blocked = 0;
739-
sdev->device_blocked = 0;
729+
/*
730+
* Clear the flags that say that the device/target/host is no longer
731+
* capable of accepting new commands.
732+
*/
733+
if (atomic_read(&shost->host_blocked))
734+
atomic_set(&shost->host_blocked, 0);
735+
if (atomic_read(&starget->target_blocked))
736+
atomic_set(&starget->target_blocked, 0);
737+
if (atomic_read(&sdev->device_blocked))
738+
atomic_set(&sdev->device_blocked, 0);
740739

741740
/*
742741
* If we have valid sense information, then some kind of recovery

drivers/scsi/scsi_lib.c

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,16 @@ scsi_set_blocked(struct scsi_cmnd *cmd, int reason)
9999
*/
100100
switch (reason) {
101101
case SCSI_MLQUEUE_HOST_BUSY:
102-
host->host_blocked = host->max_host_blocked;
102+
atomic_set(&host->host_blocked, host->max_host_blocked);
103103
break;
104104
case SCSI_MLQUEUE_DEVICE_BUSY:
105105
case SCSI_MLQUEUE_EH_RETRY:
106-
device->device_blocked = device->max_device_blocked;
106+
atomic_set(&device->device_blocked,
107+
device->max_device_blocked);
107108
break;
108109
case SCSI_MLQUEUE_TARGET_BUSY:
109-
starget->target_blocked = starget->max_target_blocked;
110+
atomic_set(&starget->target_blocked,
111+
starget->max_target_blocked);
110112
break;
111113
}
112114
}
@@ -351,29 +353,35 @@ static void scsi_single_lun_run(struct scsi_device *current_sdev)
351353
spin_unlock_irqrestore(shost->host_lock, flags);
352354
}
353355

354-
static inline int scsi_device_is_busy(struct scsi_device *sdev)
356+
static inline bool scsi_device_is_busy(struct scsi_device *sdev)
355357
{
356-
if (atomic_read(&sdev->device_busy) >= sdev->queue_depth ||
357-
sdev->device_blocked)
358-
return 1;
359-
return 0;
358+
if (atomic_read(&sdev->device_busy) >= sdev->queue_depth)
359+
return true;
360+
if (atomic_read(&sdev->device_blocked) > 0)
361+
return true;
362+
return false;
360363
}
361364

362-
static inline int scsi_target_is_busy(struct scsi_target *starget)
365+
static inline bool scsi_target_is_busy(struct scsi_target *starget)
363366
{
364-
return ((starget->can_queue > 0 &&
365-
atomic_read(&starget->target_busy) >= starget->can_queue) ||
366-
starget->target_blocked);
367+
if (starget->can_queue > 0 &&
368+
atomic_read(&starget->target_busy) >= starget->can_queue)
369+
return true;
370+
if (atomic_read(&starget->target_blocked) > 0)
371+
return true;
372+
return false;
367373
}
368374

369-
static inline int scsi_host_is_busy(struct Scsi_Host *shost)
375+
static inline bool scsi_host_is_busy(struct Scsi_Host *shost)
370376
{
371-
if ((shost->can_queue > 0 &&
372-
atomic_read(&shost->host_busy) >= shost->can_queue) ||
373-
shost->host_blocked || shost->host_self_blocked)
374-
return 1;
375-
376-
return 0;
377+
if (shost->can_queue > 0 &&
378+
atomic_read(&shost->host_busy) >= shost->can_queue)
379+
return true;
380+
if (atomic_read(&shost->host_blocked) > 0)
381+
return true;
382+
if (shost->host_self_blocked)
383+
return true;
384+
return false;
377385
}
378386

379387
static void scsi_starved_list_run(struct Scsi_Host *shost)
@@ -1256,14 +1264,14 @@ static inline int scsi_dev_queue_ready(struct request_queue *q,
12561264
unsigned int busy;
12571265

12581266
busy = atomic_inc_return(&sdev->device_busy) - 1;
1259-
if (sdev->device_blocked) {
1267+
if (atomic_read(&sdev->device_blocked)) {
12601268
if (busy)
12611269
goto out_dec;
12621270

12631271
/*
12641272
* unblock after device_blocked iterates to zero
12651273
*/
1266-
if (--sdev->device_blocked != 0) {
1274+
if (atomic_dec_return(&sdev->device_blocked) > 0) {
12671275
blk_delay_queue(q, SCSI_QUEUE_DELAY);
12681276
goto out_dec;
12691277
}
@@ -1302,19 +1310,15 @@ static inline int scsi_target_queue_ready(struct Scsi_Host *shost,
13021310
}
13031311

13041312
busy = atomic_inc_return(&starget->target_busy) - 1;
1305-
if (starget->target_blocked) {
1313+
if (atomic_read(&starget->target_blocked) > 0) {
13061314
if (busy)
13071315
goto starved;
13081316

13091317
/*
13101318
* unblock after target_blocked iterates to zero
13111319
*/
1312-
spin_lock_irq(shost->host_lock);
1313-
if (--starget->target_blocked != 0) {
1314-
spin_unlock_irq(shost->host_lock);
1320+
if (atomic_dec_return(&starget->target_blocked) > 0)
13151321
goto out_dec;
1316-
}
1317-
spin_unlock_irq(shost->host_lock);
13181322

13191323
SCSI_LOG_MLQUEUE(3, starget_printk(KERN_INFO, starget,
13201324
"unblocking target at zero depth\n"));
@@ -1349,19 +1353,15 @@ static inline int scsi_host_queue_ready(struct request_queue *q,
13491353
return 0;
13501354

13511355
busy = atomic_inc_return(&shost->host_busy) - 1;
1352-
if (shost->host_blocked) {
1356+
if (atomic_read(&shost->host_blocked) > 0) {
13531357
if (busy)
13541358
goto starved;
13551359

13561360
/*
13571361
* unblock after host_blocked iterates to zero
13581362
*/
1359-
spin_lock_irq(shost->host_lock);
1360-
if (--shost->host_blocked != 0) {
1361-
spin_unlock_irq(shost->host_lock);
1363+
if (atomic_dec_return(&shost->host_blocked) > 0)
13621364
goto out_dec;
1363-
}
1364-
spin_unlock_irq(shost->host_lock);
13651365

13661366
SCSI_LOG_MLQUEUE(3,
13671367
shost_printk(KERN_INFO, shost,

drivers/scsi/scsi_sysfs.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,6 @@ static int scsi_sdev_check_buf_bit(const char *buf)
584584
/*
585585
* Create the actual show/store functions and data structures.
586586
*/
587-
sdev_rd_attr (device_blocked, "%d\n");
588587
sdev_rd_attr (type, "%d\n");
589588
sdev_rd_attr (scsi_level, "%d\n");
590589
sdev_rd_attr (vendor, "%.8s\n");
@@ -600,6 +599,15 @@ sdev_show_device_busy(struct device *dev, struct device_attribute *attr,
600599
}
601600
static DEVICE_ATTR(device_busy, S_IRUGO, sdev_show_device_busy, NULL);
602601

602+
static ssize_t
603+
sdev_show_device_blocked(struct device *dev, struct device_attribute *attr,
604+
char *buf)
605+
{
606+
struct scsi_device *sdev = to_scsi_device(dev);
607+
return snprintf(buf, 20, "%d\n", atomic_read(&sdev->device_blocked));
608+
}
609+
static DEVICE_ATTR(device_blocked, S_IRUGO, sdev_show_device_blocked, NULL);
610+
603611
/*
604612
* TODO: can we make these symlinks to the block layer ones?
605613
*/

include/scsi/scsi_device.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ struct scsi_device {
8282
struct list_head same_target_siblings; /* just the devices sharing same target id */
8383

8484
atomic_t device_busy; /* commands actually active on LLDD */
85+
atomic_t device_blocked; /* Device returned QUEUE_FULL. */
86+
8587
spinlock_t list_lock;
8688
struct list_head cmd_list; /* queue of in use SCSI Command structures */
8789
struct list_head starved_entry;
@@ -180,8 +182,6 @@ struct scsi_device {
180182
struct list_head event_list; /* asserted events */
181183
struct work_struct event_work;
182184

183-
unsigned int device_blocked; /* Device returned QUEUE_FULL. */
184-
185185
unsigned int max_device_blocked; /* what device_blocked counts down from */
186186
#define SCSI_DEFAULT_DEVICE_BLOCKED 3
187187

@@ -291,12 +291,13 @@ struct scsi_target {
291291
* the same target will also. */
292292
/* commands actually active on LLD. */
293293
atomic_t target_busy;
294+
atomic_t target_blocked;
295+
294296
/*
295297
* LLDs should set this in the slave_alloc host template callout.
296298
* If set to zero then there is not limit.
297299
*/
298300
unsigned int can_queue;
299-
unsigned int target_blocked;
300301
unsigned int max_target_blocked;
301302
#define SCSI_DEFAULT_TARGET_BLOCKED 3
302303

include/scsi/scsi_host.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,8 @@ struct Scsi_Host {
583583
struct blk_queue_tag *bqt;
584584

585585
atomic_t host_busy; /* commands actually active on low-level */
586+
atomic_t host_blocked;
587+
586588
unsigned int host_failed; /* commands that failed.
587589
protected by host_lock */
588590
unsigned int host_eh_scheduled; /* EH scheduled without command */
@@ -681,11 +683,6 @@ struct Scsi_Host {
681683
*/
682684
struct workqueue_struct *tmf_work_q;
683685

684-
/*
685-
* Host has rejected a command because it was busy.
686-
*/
687-
unsigned int host_blocked;
688-
689686
/*
690687
* Value host_blocked counts down from
691688
*/

0 commit comments

Comments
 (0)