Skip to contentSkip to conversation

ata: libata-scsi: reject multi-sector taskfile commands when dev->multi_count is 0

PATCH Superseded Hui Peng 4 participants 7 messages Sep 19 – Sep 21

Subsystems (2)
lore
Share & tools
lore

Hui Peng

author
#0 lore raw

PATCH 1/1 ata: libata-scsi: reject multi-sector taskfile commands when dev->multi_count is 0

In ata_scsi_pass_thru() (drivers/ata/libata-scsi.c), issuing ATA_CMD_READ_MULTI / ATA_CMD_WRITE_MULTI / ATA_CMD_READ_MULTI_EXT / ATA_CMD_WRITE_MULTI_EXT / ATA_CMD_WRITE_MULTI_FUA_EXT via SG_IO when dev->multi_count == 0 sets qc->sect_size = 0 and triggers a divide-by- zero or WARN_ON in the ATA PIO/taskfile path. Reject ATA_PROT_PIO multi- sector commands with -EINVAL when dev->multi_count == 0.

Fixes c6fd280766a0 ("Move libata to drivers/ata.")
Assisted-by LLM

---

drivers/ata/libata-scsi.c +5 -0
index b3666519b648..c99f2845d09b 100644
@@ -3507,6 +3507,11 @@ static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc)
 	if (is_multi_taskfile(tf)) {
 		unsigned int multi_count = 1 << (cdb[1] >> 5);
 
+		if (!dev->multi_count) {
+			fp = (cdb[0] == ATA_16) ? 14 : 9;
+			goto invalid_fld;
+		}
+
 		/* compare the passed through multi_count
 		 * with the cached multi_count of libata
 		 */
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] scsi: incorrect error field pointer for ATA_32 variable length CDBs --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] scsi: incorrect error field pointer for ATA_32 variable length CDBs --

commit 19f1f1458eecf6dc97d38273c4c750d2aff47ab1
Author: Hui Peng <[email protected]>

ata: libata-scsi: reject multi-sector taskfile commands when dev->multi_count is 0

This patch rejects ATA_PROT_PIO multi-sector commands with -EINVAL when dev->multi_count is 0. It does this by catching the condition early and triggering an error path to avoid divide-by-zero or warnings in the taskfile path.

drivers/ata/libata-scsi.c static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc) · line 3477

Quoted 10 lines
drivers/ata/libata-scsi.c +2 -0
index d2160ee7ca7d4..3fc4f0377e991 100644
@@ -3473,6 +3473,11 @@ static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc)
 	if (is_multi_taskfile(tf)) {
 		unsigned int multi_count = 1 << (cdb[1] >> 5);
 
+		if (!dev->multi_count) {
+			fp = (cdb[0] == ATA_16) ? 14 : 9;

[Severity: Low]
Does this fallback to 9 incorrectly handle ATA_32 variable length CDBs?

When an ATA_32 pass-through command specifies a multi-sector command in ata_scsi_pass_thru() but the device does not support it, this code rejects the command and sets the invalid field pointer (fp) to 9. For an ATA_12 command, byte 9 is the command field, but for an ATA_32 variable length CDB (opcode 0x7F), byte 9 is the Service Action lower byte, and the command field is at byte 25.

Could this result in the SCSI sense data incorrectly pointing to byte 9 instead of byte 25 on the error path for ATA_32 commands?

Quoted 6 lines
drivers/ata/libata-scsi.c +3 -0
+			goto invalid_fld;
+		}
+
 		/* compare the passed through multi_count
 		 * with the cached multi_count of libata
 		 */
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
In reply to Hui Peng
Quoted 4 lines · Hui Peng

In ata_scsi_pass_thru() (drivers/ata/libata-scsi.c), issuing ATA_CMD_READ_MULTI / ATA_CMD_WRITE_MULTI / ATA_CMD_READ_MULTI_EXT / ATA_CMD_WRITE_MULTI_EXT / ATA_CMD_WRITE_MULTI_FUA_EXT via SG_IO when dev->multi_count == 0 sets qc->sect_size = 0 and triggers a divide-by-

Where do you see that?

Quoted 2 lines

zero or WARN_ON in the ATA PIO/taskfile path. Reject ATA_PROT_PIO multi- sector commands with -EINVAL when dev->multi_count == 0.

That is not what your patch is doing.

Quoted 3 lines
Fixes c6fd280766a0 ("Move libata to drivers/ata.")
Assisted-by LLM

See below, but you should tell your AI to read the ACS and SAT specs first. Or
better: check the specs *yourself* and analyze what is said before sending patches that are not correct.

drivers/ata/libata-scsi.c static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc) · line 3511

Quoted 12 lines

---

drivers/ata/libata-scsi.c +2 -0
index b3666519b648..c99f2845d09b 100644
@@ -3507,6 +3507,11 @@ static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc)
 	if (is_multi_taskfile(tf)) {
 		unsigned int multi_count = 1 << (cdb[1] >> 5);
 
+		if (!dev->multi_count) {
+			fp = (cdb[0] == ATA_16) ? 14 : 9;

ACS-3, which is the last ACS specification version without an obsolete definition of multi-sector PIO read/wrtite states that:

If IDENTIFY DEVICE data word 59 bit 8 (see 7.12.7.21) is cleared to zero, and a READ MULTIPLE EXT command is received by the device, and the device has not returned command completion without an error for a SET MULTIPLE MODE command, the device shall return command aborted.

The same statement is also present for WRITE MULTIPLE command.

Now referring to the SAT specifications, you can see that an ABRT (aborted command) error should be reported as failed with the ABORTED COMMAND sense key and NO ADDITIONAL SENSE INFORMATION additional sense code.
So here, the correct error to return is not invalid field.

Quoted 6 lines
drivers/ata/libata-scsi.c +3 -0
+			goto invalid_fld;
+		}
+
 		/* compare the passed through multi_count
 		 * with the cached multi_count of libata
 		 */
Signature · 2 lines
Damien Le Moal
Western Digital Research
author
#3 lore raw

PATCH v2 1/1 ata: libata-scsi: abort multi-sector pass-through commands when multi_count is 0

When an ATA_12, ATA_16, or ATA_32 pass-through command with a multi-sector PIO command opcode (ATA_CMD_READ_MULTI, ATA_CMD_WRITE_MULTI, ATA_CMD_READ_MULTI_EXT, ATA_CMD_WRITE_MULTI_EXT, or ATA_CMD_WRITE_MULTI_FUA_EXT) is submitted via SG_IO on a device where multiple-sector mode is not configured (dev->multi_count == 0), ata_scsi_pass_thru() currently logs a warning and still dispatches the taskfile. When the DRQ interrupt fires, ata_pio_sectors() triggers WARN_ON_ONCE(qc->dev->multi_count == 0) and computes
nsect = min((qc->nbytes - qc->curbytes) / qc->sect_size, 0) = 0, failing to transfer any sectors:

ata1.00: invalid multi_count 1 ignored

  WARNING: drivers/ata/libata-sff.c:666 at ata_pio_sectors+0x27d/0x300
  Call Trace:
   <IRQ>
   ata_sff_hsm_move+0x211/0x22e0
   __ata_sff_port_intr+0x1c8/0x520
   ata_bmdma_port_intr+0xa1/0x5b0
   ata_bmdma_interrupt+0x1f5/0x550

Per ACS-3 section 7.12.7.21, if IDENTIFY DEVICE word 59 bit 8 is cleared to zero (multi_count == 0) and a READ MULTIPLE or WRITE MULTIPLE command is received without a preceding successful SET MULTIPLE MODE command, the device returns command aborted (ABRT). Per the SAT specification, an ABRT error translates to the ABORTED COMMAND sense key with NO ADDITIONAL SENSE INFORMATION (0x00, 0x00).

Fail multi-sector taskfile commands in ata_scsi_pass_thru() with ata_scsi_set_sense(dev, scmd, ABORTED_COMMAND, 0, 0) when dev->multi_count == 0.

Tested in QEMU against Linux 7.3.0-rc3 (-device ide-cf, where dev->multi_count is 0) by issuing ATA_CMD_SET_MULTI (nsect=1) followed by an SG_IO ATA_16 ATA_CMD_READ_MULTI command: on the unfixed kernel this triggers WARNING: drivers/ata/libata-sff.c:666 in ata_pio_sectors(), whereas on the fixed kernel it immediately completes with sense key ABORTED_COMMAND (0x0b, 0x00, 0x00) and 0 warnings.

Fixes 1dce589c38c3 ("libata passthru: support PIO multi commands")
Assisted-by LLM

---
Changes in v2:
- Report sense key ABORTED_COMMAND (0x00, 0x00) via ata_scsi_set_sense() per ACS-3 and SAT specifications instead of ILLEGAL_REQUEST / invalid_fld, as pointed out by Damien Le Moal.
- Update the commit description to accurately describe the ata_pio_sectors() WARN_ON_ONCE(qc->dev->multi_count == 0) and 0-sector transfer path and fix the Fixes: tag to 1dce589c38c3.

 drivers/ata/libata-scsi.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/ata/libata-scsi.c +5 -0
index b3666519b648..29fd6d8d97ce 100644
@@ -3507,6 +3507,11 @@ static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc)
 	if (is_multi_taskfile(tf)) {
 		unsigned int multi_count = 1 << (cdb[1] >> 5);
 
+		if (!dev->multi_count) {
+			ata_scsi_set_sense(dev, scmd, ABORTED_COMMAND, 0, 0);
+			return 1;
+		}
+
 		/* compare the passed through multi_count
 		 * with the cached multi_count of libata
 		 */
Signature · 1 line
2.49.0
maintainer Reviewed-by on v2 1/1
#4 lore raw

Re: [PATCH v2] ata: libata-scsi: abort multi-sector pass-through commands when multi_count is 0

Quoted 38 lines · Hui Peng

When an ATA_12, ATA_16, or ATA_32 pass-through command with a multi-sector PIO command opcode (ATA_CMD_READ_MULTI, ATA_CMD_WRITE_MULTI, ATA_CMD_READ_MULTI_EXT, ATA_CMD_WRITE_MULTI_EXT, or ATA_CMD_WRITE_MULTI_FUA_EXT) is submitted via SG_IO on a device where multiple-sector mode is not configured (dev->multi_count == 0), ata_scsi_pass_thru() currently logs a warning and still dispatches the taskfile. When the DRQ interrupt fires, ata_pio_sectors() triggers WARN_ON_ONCE(qc->dev->multi_count == 0) and computes
nsect = min((qc->nbytes - qc->curbytes) / qc->sect_size, 0) = 0, failing to transfer any sectors:

ata1.00: invalid multi_count 1 ignored

  WARNING: drivers/ata/libata-sff.c:666 at ata_pio_sectors+0x27d/0x300
  Call Trace:
   <IRQ>
   ata_sff_hsm_move+0x211/0x22e0
   __ata_sff_port_intr+0x1c8/0x520
   ata_bmdma_port_intr+0xa1/0x5b0
   ata_bmdma_interrupt+0x1f5/0x550

Per ACS-3 section 7.12.7.21, if IDENTIFY DEVICE word 59 bit 8 is cleared to zero (multi_count == 0) and a READ MULTIPLE or WRITE MULTIPLE command is received without a preceding successful SET MULTIPLE MODE command, the device returns command aborted (ABRT). Per the SAT specification, an ABRT error translates to the ABORTED COMMAND sense key with NO ADDITIONAL SENSE INFORMATION (0x00, 0x00).

Fail multi-sector taskfile commands in ata_scsi_pass_thru() with ata_scsi_set_sense(dev, scmd, ABORTED_COMMAND, 0, 0) when dev->multi_count == 0.

Tested in QEMU against Linux 7.3.0-rc3 (-device ide-cf, where dev->multi_count is 0) by issuing ATA_CMD_SET_MULTI (nsect=1) followed by an SG_IO ATA_16 ATA_CMD_READ_MULTI command: on the unfixed kernel this triggers WARNING: drivers/ata/libata-sff.c:666 in ata_pio_sectors(), whereas on the fixed kernel it immediately completes with sense key ABORTED_COMMAND (0x0b, 0x00, 0x00) and 0 warnings.

Quoted 4 lines
Fixes 1dce589c38c3 ("libata passthru: support PIO multi commands")
Assisted-by LLM

Looks OK.

Signature · 2 lines
Damien Le Moal
Western Digital Research
maintainer
#5 lore raw

Re: [PATCH v2] ata: libata-scsi: abort multi-sector pass-through commands when multi_count is 0

In reply to Hui Peng
Quoted 31 lines · Hui Peng

When an ATA_12, ATA_16, or ATA_32 pass-through command with a multi-sector PIO command opcode (ATA_CMD_READ_MULTI, ATA_CMD_WRITE_MULTI, ATA_CMD_READ_MULTI_EXT, ATA_CMD_WRITE_MULTI_EXT, or ATA_CMD_WRITE_MULTI_FUA_EXT) is submitted via SG_IO on a device where multiple-sector mode is not configured (dev->multi_count == 0), ata_scsi_pass_thru() currently logs a warning and still dispatches the taskfile. When the DRQ interrupt fires, ata_pio_sectors() triggers WARN_ON_ONCE(qc->dev->multi_count == 0) and computes
nsect = min((qc->nbytes - qc->curbytes) / qc->sect_size, 0) = 0, failing to transfer any sectors:

ata1.00: invalid multi_count 1 ignored

  WARNING: drivers/ata/libata-sff.c:666 at ata_pio_sectors+0x27d/0x300
  Call Trace:
   <IRQ>
   ata_sff_hsm_move+0x211/0x22e0
   __ata_sff_port_intr+0x1c8/0x520
   ata_bmdma_port_intr+0xa1/0x5b0
   ata_bmdma_interrupt+0x1f5/0x550

Per ACS-3 section 7.12.7.21, if IDENTIFY DEVICE word 59 bit 8 is cleared to zero (multi_count == 0) and a READ MULTIPLE or WRITE MULTIPLE command is received without a preceding successful SET MULTIPLE MODE command, the device returns command aborted (ABRT). Per the SAT specification, an ABRT error translates to the ABORTED COMMAND sense key with NO ADDITIONAL SENSE INFORMATION (0x00, 0x00).

Fail multi-sector taskfile commands in ata_scsi_pass_thru() with ata_scsi_set_sense(dev, scmd, ABORTED_COMMAND, 0, 0) when dev->multi_count == 0.

Quoted 6 lines

Tested in QEMU against Linux 7.3.0-rc3 (-device ide-cf, where dev->multi_count is 0) by issuing ATA_CMD_SET_MULTI (nsect=1) followed by an SG_IO ATA_16 ATA_CMD_READ_MULTI command: on the unfixed kernel this triggers WARNING: drivers/ata/libata-sff.c:666 in ata_pio_sectors(), whereas on the fixed kernel it immediately completes with sense key ABORTED_COMMAND (0x0b, 0x00, 0x00) and 0 warnings.

Testing information is usually placed after "---" so that it does not get included in the commit message when applied using git am.

I can drop this when applying, no need to send a v3.

Kind regards,
Niklas

maintainer Applied
#6 lore raw

Re: [PATCH v2] ata: libata-scsi: abort multi-sector pass-through commands when multi_count is 0

In reply to Hui Peng
Quoted 11 lines · Hui Peng

When an ATA_12, ATA_16, or ATA_32 pass-through command with a multi-sector PIO command opcode (ATA_CMD_READ_MULTI, ATA_CMD_WRITE_MULTI, ATA_CMD_READ_MULTI_EXT, ATA_CMD_WRITE_MULTI_EXT, or ATA_CMD_WRITE_MULTI_FUA_EXT) is submitted via SG_IO on a device where multiple-sector mode is not configured (dev->multi_count == 0), ata_scsi_pass_thru() currently logs a warning and still dispatches the taskfile. When the DRQ interrupt fires, ata_pio_sectors() triggers WARN_ON_ONCE(qc->dev->multi_count == 0) and computes
nsect = min((qc->nbytes - qc->curbytes) / qc->sect_size, 0) = 0, failing to transfer any sectors:

Quoted 1 line

[...]

Applied to libata/linux.git (for-7.4), thanks!

[1/1] ata: libata-scsi: abort multi-sector pass-through commands when multi_count is 0

      https://git.kernel.org/libata/linux/c/3bab8c7e

Kind regards,
Niklas