Skip to contentSkip to conversation

ata: pata_ep93xx: fix endianness bug in trailing byte transfer

PATCH Superseded Rosen Penev 2 participants 2 messages Jun 1 libata

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ata: pata_ep93xx: fix endianness bug in trailing byte transfer

ep93xx_pata_data_xfer() handles a trailing odd byte by reading/writing a full 16-bit word and extracting the first byte. The current code uses cpu_to_le16()/le16_to_cpu() wrapping, but then assigns through a u8 pointer (*pad), truncating the value.

On the READ path, *pad = cpu_to_le16(...) truncates the u16 to u8. On little-endian this happens to give the right byte, but on big-endian cpu_to_le16() puts the valid byte in the upper 8 bits and truncation keeps the lower 8 bits (zeros), losing the data.

On the WRITE path, le16_to_cpu(*pad) zero-extends pad[0] (u8) to u16 (0x00BB) and then on big-endian byte-swaps to 0xBB00. The hardware then receives 0x00 as the first byte instead of 0xBB.

The readl()/writel() calls inside ep93xx_pata_read_data() and ep93xx_pata_write_data() already handle endianness correctly, so the trailing byte can just use the raw read/write value directly without the cpu_to_le16/le16_to_cpu wrappers. Remove the pad[] array and simplify.

Assisted-by Opencode:Big-Pickle
---
 drivers/ata/pata_ep93xx.c | 11 ++---------
 1 file changed, 2 insertions(+), 9 deletions(-)
drivers/ata/pata_ep93xx.c +2 -9
index 3f61712af5bb..a0dfdacf54e5 100644
@@ -495,19 +495,12 @@ static unsigned int ep93xx_pata_data_xfer(struct ata_queued_cmd *qc,
 
 	/* Transfer trailing 1 byte, if any. */
 	if (unlikely(buflen & 0x01)) {
-		unsigned char pad[2] = { };
-
 		buf += buflen - 1;
 
 		if (rw == READ) {
-			*pad = cpu_to_le16(
-				ep93xx_pata_read_data(
-					drv_data, IDECTRL_ADDR_DATA));
-			*buf = pad[0];
+			*buf = ep93xx_pata_read_data(drv_data, IDECTRL_ADDR_DATA);
 		} else {
-			pad[0] = *buf;
-			ep93xx_pata_write_data(drv_data, le16_to_cpu(*pad),
-					  IDECTRL_ADDR_DATA);
+			ep93xx_pata_write_data(drv_data, *buf, IDECTRL_ADDR_DATA);
 		}
 		words++;
 	}
Signature · 1 line
2.54.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] The ep93xx_pata_data_xfer() function destroys the words loop counter, returning an incorrect byte count that breaks ATAPI PIO transfers. --

commit c92ab9f0ecc44ddedc25d0e57302962565e67769
Author: Rosen Penev <[email protected]>

ata: pata_ep93xx: fix endianness bug in trailing byte transfer

This commit fixes an endianness bug in ep93xx_pata_data_xfer() where a trailing odd byte was being truncated and incorrectly byte-swapped on big-endian systems. It removes the cpu_to_le16/le16_to_cpu wrappers and uses the raw read/write values directly.

Quoted 7 lines
drivers/ata/pata_ep93xx.c +0 -0
@@ -495,19 +495,12 @@ static unsigned int ep93xx_pata_data_xfer(struct ata_queued_cmd *qc,
 
 	/* Transfer trailing 1 byte, if any. */
 	if (unlikely(buflen & 0x01)) {

[ ... ]

drivers/ata/pata_ep93xx.c static unsigned int ep93xx_pata_data_xfer(struct ata_queued_cmd *qc, · line 505

Quoted 13 lines
drivers/ata/pata_ep93xx.c +2 -7
 		if (rw == READ) {
-			*pad = cpu_to_le16(
-				ep93xx_pata_read_data(
-					drv_data, IDECTRL_ADDR_DATA));
-			*buf = pad[0];
+			*buf = ep93xx_pata_read_data(drv_data, IDECTRL_ADDR_DATA);
 		} else {
-			pad[0] = *buf;
-			ep93xx_pata_write_data(drv_data, le16_to_cpu(*pad),
-					  IDECTRL_ADDR_DATA);
+			ep93xx_pata_write_data(drv_data, *buf, IDECTRL_ADDR_DATA);
 		}
 		words++;

[Severity: High]
This is a pre-existing issue, but does this function return an incorrect byte count that breaks ATAPI PIO transfers?

Earlier in ep93xx_pata_data_xfer(), there is a loop that destroys the words counter:

        unsigned int words = buflen >> 1;
        while (words--)
            /* transfer 2 bytes */

Because of the post-decrement, the words counter underflows to UINT_MAX when the loop terminates. If there is an odd trailing byte, words++ wraps it back to 0, and the function returns 0 since the end of the function is:

        return words << 1;

If there is no trailing byte, the function returns UINT_MAX << 1, which is 0xFFFFFFFE.

Returning 0 for an odd-length transfer causes callers like __atapi_pio_bytes() to not decrement their remaining bytes counter while still advancing the scatter-gather list, resulting in unexpected or too much trailing data errors.

Returning 0xFFFFFFFE causes __atapi_pio_bytes() to subtract that huge amount in a single iteration, instantly dropping the remaining transfer count to zero even if the DRQ block spanned multiple scatter-gather entries.

Could this be fixed by saving the original buffer length and returning that instead of relying on the mutated words counter?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1