Skip to content

Commit 888cd7e

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 410336f commit 888cd7e

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
@@ -5552,6 +5552,7 @@ struct ata_port *ata_port_alloc(struct ata_host *host)
55525552
mutex_init(&ap->scsi_scan_mutex);
55535553
INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
55545554
INIT_DELAYED_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
5555+
INIT_WORK(&ap->deferred_qc_work, ata_scsi_deferred_qc_work);
55555556
INIT_LIST_HEAD(&ap->eh_done_q);
55565557
init_waitqueue_head(&ap->eh_wait_q);
55575558
init_completion(&ap->park_req_pending);
@@ -6162,6 +6163,10 @@ static void ata_port_detach(struct ata_port *ap)
61626163
}
61636164
}
61646165

6166+
/* Make sure the deferred qc work finished. */
6167+
cancel_work_sync(&ap->deferred_qc_work);
6168+
WARN_ON(ap->deferred_qc);
6169+
61656170
/* Tell EH to disable all devices */
61666171
ap->pflags |= ATA_PFLAG_UNLOADING;
61676172
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
@@ -1657,8 +1657,77 @@ static void ata_qc_done(struct ata_queued_cmd *qc)
16571657
done(cmd);
16581658
}
16591659

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

16901759
ata_qc_done(qc);
1760+
1761+
ata_scsi_schedule_deferred_qc(ap);
16911762
}
16921763

16931764
static int ata_scsi_qc_issue(struct ata_port *ap, struct ata_queued_cmd *qc)
@@ -1697,6 +1768,16 @@ static int ata_scsi_qc_issue(struct ata_port *ap, struct ata_queued_cmd *qc)
16971768
if (!ap->ops->qc_defer)
16981769
goto issue;
16991770

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

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

drivers/ata/libata.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ void ata_scsi_sdev_config(struct scsi_device *sdev);
161161
int ata_scsi_dev_config(struct scsi_device *sdev, struct queue_limits *lim,
162162
struct ata_device *dev);
163163
int __ata_scsi_queuecmd(struct scsi_cmnd *scmd, struct ata_device *dev);
164+
void ata_scsi_deferred_qc_work(struct work_struct *work);
165+
void ata_scsi_requeue_deferred_qc(struct ata_port *ap);
164166

165167
/* libata-eh.c */
166168
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
@@ -899,6 +899,9 @@ struct ata_port {
899899
u64 qc_active;
900900
int nr_active_links; /* #links with active qcs */
901901

902+
struct work_struct deferred_qc_work;
903+
struct ata_queued_cmd *deferred_qc;
904+
902905
struct ata_link link; /* host default link */
903906
struct ata_link *slave_link; /* see ata_slave_link_init() */
904907

0 commit comments

Comments
 (0)