Skip to contentSkip to conversation

libnvme: do not byte swap command dwords

PATCH Applied Keith Busch 4 participants 5 messages Apr 3, 2025 NVMe

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libnvme: do not byte swap command dwords

From: Keith Busch <[email protected]>

The dwords always need to be set in the cpu native format. The driver will handle setting up the SQE in the nvme little-endian order, and big endian is never used, so this byte swapping was wrong.

---
 src/nvme/ioctl.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
src/nvme/ioctl.c +3 -3
index 18b228d9..86c2b702 100644
@@ -1920,9 +1920,9 @@ static int nvme_set_var_size_tags(__u32 *cmd_dw2, __u32 *cmd_dw3, __u32 *cmd_dw1
 		return -1;
 	}
 
-	*cmd_dw2 = cpu_to_be32(cdw2);
-	*cmd_dw3 = cpu_to_be32(cdw3);
-	*cmd_dw14 = cpu_to_be32(cdw14);
+	*cmd_dw2 = cdw2;
+	*cmd_dw3 = cdw3;
+	*cmd_dw14 = cdw14;
 	return 0;
 }
 
Signature · 1 line
2.47.1
#1 lore raw
Quoted 17 lines · Keith Busch

From: Keith Busch <[email protected]>

The dwords always need to be set in the cpu native format. The driver will handle setting up the SQE in the nvme little-endian order, and big endian is never used, so this byte swapping was wrong.

---
 src/nvme/ioctl.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
src/nvme/ioctl.c +0 -0
index 18b228d9..86c2b702 100644

src/nvme/ioctl.c static int nvme_set_var_size_tags(__u32 *cmd_dw2, __u32 *cmd_dw3, __u32 *cmd_dw1 · line 1925

Quoted 9 lines
src/nvme/ioctl.c +3 -3
@@ -1920,9 +1920,9 @@ static int nvme_set_var_size_tags(__u32 *cmd_dw2, __u32 *cmd_dw3, __u32 *cmd_dw1
 		return -1;
 	}
 
-	*cmd_dw2 = cpu_to_be32(cdw2);
-	*cmd_dw3 = cpu_to_be32(cdw3);
-	*cmd_dw14 = cpu_to_be32(cdw14);
+	*cmd_dw2 = cdw2;
+	*cmd_dw3 = cdw3;
+	*cmd_dw14 = cdw14;

in this case shouldn't we use cpu_to_le32?

maintainer
#2 lore raw
Quoted 21 lines · Daniel Wagner
Quoted 20 lines · Keith Busch

From: Keith Busch <[email protected]>

The dwords always need to be set in the cpu native format. The driver will handle setting up the SQE in the nvme little-endian order, and big endian is never used, so this byte swapping was wrong.

---
 src/nvme/ioctl.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
src/nvme/ioctl.c +0 -0
index 18b228d9..86c2b702 100644
@@ -1920,9 +1920,9 @@ static int nvme_set_var_size_tags(__u32 *cmd_dw2, __u32 *cmd_dw3, __u32 *cmd_dw1
 		return -1;
 	}
 
Quoted 8 lines

src/nvme/ioctl.c static int nvme_set_var_size_tags(__u32 *cmd_dw2, __u32 *cmd_dw3, __u32 *cmd_dw1 · line 1925

Quoted 6 lines
src/nvme/ioctl.c +3 -3
-	*cmd_dw2 = cpu_to_be32(cdw2);
-	*cmd_dw3 = cpu_to_be32(cdw3);
-	*cmd_dw14 = cpu_to_be32(cdw14);
+	*cmd_dw2 = cdw2;
+	*cmd_dw3 = cdw3;
+	*cmd_dw14 = cdw14;

in this case shouldn't we use cpu_to_le32?

No, user space was only uses cpu native endian when constructing the command. The driver handles the endian conversion if necessary.

That conversion is just for the command control side. The driver doesn't do any such manipulation on the data plane, so endianness in payloads for commands like "copy", "dsm", or PI meatada need to be handled by the application.

#4 lore raw
In reply to Keith Busch
Quoted 5 lines · Keith Busch

The dwords always need to be set in the cpu native format. The driver will handle setting up the SQE in the nvme little-endian order, and big endian is never used, so this byte swapping was wrong.

Applied, thanks!

[1/1] libnvme: do not byte swap command dwords

      commit: 89ac31f536baf15c1834f877b4c1fb9aca1cf94f

Best regards,

Signature · 1 line
Daniel Wagner <[email protected]>