Skip to content

Commit 5d61a38

Browse files
damien-lemoalgregkh
authored andcommitted
ata: libata-scsi: avoid Non-NCQ command starvation
commit 0ea8408 upstream. When a non-NCQ command is issued while NCQ commands are being executed, ata_scsi_qc_issue() indicates to the SCSI layer that the command issuing should be deferred by returning SCSI_MLQUEUE_XXX_BUSY. This command deferring is correct and as mandated by the ACS specifications since NCQ and non-NCQ commands cannot be mixed. However, in the case of a host adapter using multiple submission queues, when the target device is under a constant load of NCQ commands, there are no guarantees that requeueing the non-NCQ command will be executed later and it may be deferred again repeatedly as other submission queues can constantly issue NCQ commands from different CPUs ahead of the non-NCQ command. This can lead to very long delays for the execution of non-NCQ commands, and even complete starvation for these commands in the worst case scenario. Since the block layer and the SCSI layer do not distinguish between queueable (NCQ) and non queueable (non-NCQ) commands, libata-scsi SAT implementation must ensure forward progress for non-NCQ commands in the presence of NCQ command traffic. This is similar to what SAS HBAs with a hardware/firmware based SAT implementation do. Implement such forward progress guarantee by limiting requeueing of non-NCQ commands from ata_scsi_qc_issue(): when a non-NCQ command is received and NCQ commands are in-flight, do not force a requeue of the non-NCQ command by returning SCSI_MLQUEUE_XXX_BUSY and instead return 0 to indicate that the command was accepted but hold on to the qc using the new deferred_qc field of struct ata_port. This deferred qc will be issued using the work item deferred_qc_work running the function ata_scsi_deferred_qc_work() once all in-flight commands complete, which is checked with the port qc_defer() callback return value indicating that no further delay is necessary. This check is done using the helper function ata_scsi_schedule_deferred_qc() which is called from ata_scsi_qc_complete(). This thus excludes this mechanism from all internal non-NCQ commands issued by ATA EH. When a port deferred_qc is non NULL, that is, the port has a command waiting for the device queue to drain, the issuing of all incoming commands (both NCQ and non-NCQ) is deferred using the regular busy mechanism. This simplifies the code and also avoids potential denial of service problems if a user issues too many non-NCQ commands. Finally, whenever ata EH is scheduled, regardless of the reason, a deferred qc is always requeued so that it can be retried once EH completes. This is done by calling the function ata_scsi_requeue_deferred_qc() from ata_eh_set_pending(). This avoids the need for any special processing for the deferred qc in case of NCQ error, link or device reset, or device timeout. Reported-by: Xingui Yang <yangxingui@huawei.com> Reported-by: Igor Pylypiv <ipylypiv@google.com> Fixes: bdb0130 ("scsi: Add host and host template flag 'host_tagset'") Cc: stable@vger.kernel.org Signed-off-by: Damien Le Moal <dlemoal@kernel.org> Reviewed-by: Niklas Cassel <cassel@kernel.org> Reviewed-by: Martin K. Petersen <martin.petersen@oracle.com> Reviewed-by: John Garry <john.g.garry@oracle.com> Tested-by: Igor Pylypiv <ipylypiv@google.com> Tested-by: Xingui Yang <yangxingui@huawei.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 4dad7ef commit 5d61a38

5 files changed

Lines changed: 109 additions & 0 deletions

File tree

drivers/ata/libata-core.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5620,6 +5620,7 @@ struct ata_port *ata_port_alloc(struct ata_host *host)
56205620
mutex_init(&ap->scsi_scan_mutex);
56215621
INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
56225622
INIT_DELAYED_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
5623+
INIT_WORK(&ap->deferred_qc_work, ata_scsi_deferred_qc_work);
56235624
INIT_LIST_HEAD(&ap->eh_done_q);
56245625
init_waitqueue_head(&ap->eh_wait_q);
56255626
init_completion(&ap->park_req_pending);
@@ -6232,6 +6233,10 @@ static void ata_port_detach(struct ata_port *ap)
62326233
}
62336234
}
62346235

6236+
/* Make sure the deferred qc work finished. */
6237+
cancel_work_sync(&ap->deferred_qc_work);
6238+
WARN_ON(ap->deferred_qc);
6239+
62356240
/* Tell EH to disable all devices */
62366241
ap->pflags |= ATA_PFLAG_UNLOADING;
62376242
ata_port_schedule_eh(ap);

drivers/ata/libata-eh.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,12 @@ static void ata_eh_set_pending(struct ata_port *ap, bool fastdrain)
917917

918918
ap->pflags |= ATA_PFLAG_EH_PENDING;
919919

920+
/*
921+
* If we have a deferred qc, requeue it so that it is retried once EH
922+
* completes.
923+
*/
924+
ata_scsi_requeue_deferred_qc(ap);
925+
920926
if (!fastdrain)
921927
return;
922928

drivers/ata/libata-scsi.c

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,8 +1658,77 @@ static void ata_qc_done(struct ata_queued_cmd *qc)
16581658
done(cmd);
16591659
}
16601660

1661+
void ata_scsi_deferred_qc_work(struct work_struct *work)
1662+
{
1663+
struct ata_port *ap =
1664+
container_of(work, struct ata_port, deferred_qc_work);
1665+
struct ata_queued_cmd *qc;
1666+
unsigned long flags;
1667+
1668+
spin_lock_irqsave(ap->lock, flags);
1669+
1670+
/*
1671+
* If we still have a deferred qc and we are not in EH, issue it. In
1672+
* such case, we should not need any more deferring the qc, so warn if
1673+
* qc_defer() says otherwise.
1674+
*/
1675+
qc = ap->deferred_qc;
1676+
if (qc && !ata_port_eh_scheduled(ap)) {
1677+
WARN_ON_ONCE(ap->ops->qc_defer(qc));
1678+
ap->deferred_qc = NULL;
1679+
ata_qc_issue(qc);
1680+
}
1681+
1682+
spin_unlock_irqrestore(ap->lock, flags);
1683+
}
1684+
1685+
void ata_scsi_requeue_deferred_qc(struct ata_port *ap)
1686+
{
1687+
struct ata_queued_cmd *qc = ap->deferred_qc;
1688+
struct scsi_cmnd *scmd;
1689+
1690+
lockdep_assert_held(ap->lock);
1691+
1692+
/*
1693+
* If we have a deferred qc when a reset occurs or NCQ commands fail,
1694+
* do not try to be smart about what to do with this deferred command
1695+
* and simply retry it by completing it with DID_SOFT_ERROR.
1696+
*/
1697+
if (!qc)
1698+
return;
1699+
1700+
scmd = qc->scsicmd;
1701+
ap->deferred_qc = NULL;
1702+
ata_qc_free(qc);
1703+
scmd->result = (DID_SOFT_ERROR << 16);
1704+
scsi_done(scmd);
1705+
}
1706+
1707+
static void ata_scsi_schedule_deferred_qc(struct ata_port *ap)
1708+
{
1709+
struct ata_queued_cmd *qc = ap->deferred_qc;
1710+
1711+
lockdep_assert_held(ap->lock);
1712+
1713+
/*
1714+
* If we have a deferred qc, then qc_defer() is defined and we can use
1715+
* this callback to determine if this qc is good to go, unless EH has
1716+
* been scheduled.
1717+
*/
1718+
if (!qc)
1719+
return;
1720+
1721+
if (ata_port_eh_scheduled(ap)) {
1722+
ata_scsi_requeue_deferred_qc(ap);
1723+
return;
1724+
}
1725+
if (!ap->ops->qc_defer(qc))
1726+
queue_work(system_highpri_wq, &ap->deferred_qc_work);
1727+
}
1728+
16611729
static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
16621730
{
1731+
struct ata_port *ap = qc->ap;
16631732
struct scsi_cmnd *cmd = qc->scsicmd;
16641733
u8 *cdb = cmd->cmnd;
16651734
bool have_sense = qc->flags & ATA_QCFLAG_SENSE_VALID;
@@ -1689,6 +1758,8 @@ static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
16891758
}
16901759

16911760
ata_qc_done(qc);
1761+
1762+
ata_scsi_schedule_deferred_qc(ap);
16921763
}
16931764

16941765
static int ata_scsi_qc_issue(struct ata_port *ap, struct ata_queued_cmd *qc)
@@ -1698,6 +1769,16 @@ static int ata_scsi_qc_issue(struct ata_port *ap, struct ata_queued_cmd *qc)
16981769
if (!ap->ops->qc_defer)
16991770
goto issue;
17001771

1772+
/*
1773+
* If we already have a deferred qc, then rely on the SCSI layer to
1774+
* requeue and defer all incoming commands until the deferred qc is
1775+
* processed, once all on-going commands complete.
1776+
*/
1777+
if (ap->deferred_qc) {
1778+
ata_qc_free(qc);
1779+
return SCSI_MLQUEUE_DEVICE_BUSY;
1780+
}
1781+
17011782
/* Check if the command needs to be deferred. */
17021783
ret = ap->ops->qc_defer(qc);
17031784
switch (ret) {
@@ -1716,6 +1797,18 @@ static int ata_scsi_qc_issue(struct ata_port *ap, struct ata_queued_cmd *qc)
17161797
}
17171798

17181799
if (ret) {
1800+
/*
1801+
* We must defer this qc: if this is not an NCQ command, keep
1802+
* this qc as a deferred one and report to the SCSI layer that
1803+
* we issued it so that it is not requeued. The deferred qc will
1804+
* be issued with the port deferred_qc_work once all on-going
1805+
* commands complete.
1806+
*/
1807+
if (!ata_is_ncq(qc->tf.protocol)) {
1808+
ap->deferred_qc = qc;
1809+
return 0;
1810+
}
1811+
17191812
/* Force a requeue of the command to defer its execution. */
17201813
ata_qc_free(qc);
17211814
return ret;

drivers/ata/libata.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ void ata_scsi_sdev_config(struct scsi_device *sdev);
165165
int ata_scsi_dev_config(struct scsi_device *sdev, struct queue_limits *lim,
166166
struct ata_device *dev);
167167
int __ata_scsi_queuecmd(struct scsi_cmnd *scmd, struct ata_device *dev);
168+
void ata_scsi_deferred_qc_work(struct work_struct *work);
169+
void ata_scsi_requeue_deferred_qc(struct ata_port *ap);
168170

169171
/* libata-eh.c */
170172
extern unsigned int ata_internal_cmd_timeout(struct ata_device *dev, u8 cmd);

include/linux/libata.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,9 @@ struct ata_port {
903903
u64 qc_active;
904904
int nr_active_links; /* #links with active qcs */
905905

906+
struct work_struct deferred_qc_work;
907+
struct ata_queued_cmd *deferred_qc;
908+
906909
struct ata_link link; /* host default link */
907910
struct ata_link *slave_link; /* see ata_slave_link_init() */
908911

0 commit comments

Comments
 (0)