Skip to contentSkip to conversation

selftests: ublk: fix the build and file offsets on 32-bit

PATCH Applied Sasha Levin 3 participants 3 messages Nov 3 – Nov 4

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests: ublk: fix the build and file offsets on 32-bit

kublk puts pointers into 64-bit UAPI and SQE fields with a plain (__u64) cast. ublk builds with -Werror, so it fails to build on 32-bit architectures:

batch.c:379:42: error: cast from pointer to integer of different size [-Werror=pointer-to-int-cast]

Cast through uintptr_t. kublk also passes 64-bit user copy offsets, which start at UBLKSRV_IO_BUF_OFFSET (2 GB), to pread() and pwrite(), and with glibc those are truncated to a 32-bit off_t there. Build with -D_FILE_OFFSET_BITS=64 as well.

---
Found with 32-bit builds of the selftests (gcc 16 and clang 21 with -m32, with and without -D_TIME_BITS=64 -D_FILE_OFFSET_BITS=64); KernelCI's 32-bit arm builds show the same warnings. The changed files build without them now, and the x86_64 builds don't change.

 tools/testing/selftests/ublk/Makefile | 1 +
 tools/testing/selftests/ublk/batch.c  | 6 +++---
 tools/testing/selftests/ublk/ctrl.c   | 2 +-
 tools/testing/selftests/ublk/kublk.c  | 2 +-
 4 files changed, 6 insertions(+), 5 deletions(-)
tools/testing/selftests/ublk/Makefile +1 -0
index fc64b8f028332..1a50d4695c9fa 100644
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0
 
 CFLAGS += -O3 -Wl,-no-as-needed -Wall -I $(top_srcdir)/usr/include
+CFLAGS += -D_FILE_OFFSET_BITS=64
 ifneq ($(WERROR),0)
 	CFLAGS += -Werror
 endif
tools/testing/selftests/ublk/batch.c +3 -3
index dfc81ee17d076..625a9c32a80b1 100644
@@ -376,10 +376,10 @@ static int __ublk_batch_queue_prep_io_cmds(struct ublk_thread *t, struct ublk_qu
 		if (ublk_queue_use_auto_zc(q))
 			elem->buf_index = ublk_batch_io_buf_idx(t, q, i);
 		else if (!ublk_queue_no_buf(q))
-			elem->buf_addr = (__u64)io->buf_addr;
+			elem->buf_addr = (__u64)(uintptr_t)io->buf_addr;
 	}
 
-	sqe->addr = (__u64)buf;
+	sqe->addr = (__u64)(uintptr_t)buf;
 	sqe->len = t->commit_buf_elem_size * nr_elem;
 
 	ublk_init_batch_cmd(t, q->q_id, sqe, UBLK_U_IO_PREP_IO_CMDS,
@@ -473,7 +473,7 @@ static void __ublk_batch_commit_io_cmds(struct ublk_thread *t,
 
 	ublk_io_alloc_sqes(t, &sqe, 1);
 	buf_idx = cb->buf_idx;
-	sqe->addr = (__u64)cb->elem;
+	sqe->addr = (__u64)(uintptr_t)cb->elem;
 	sqe->len = nr_elem * t->commit_buf_elem_size;
 
 	/* commit isn't per-queue command */
tools/testing/selftests/ublk/ctrl.c +1 -1
index 53d54799f9097..5e0762cfc4eae 100644
@@ -204,7 +204,7 @@ int ublk_ctrl_get_params(struct ublk_dev *dev,
 	struct ublk_ctrl_cmd_data data = {
 		.cmd_op	= UBLK_U_CMD_GET_PARAMS,
 		.flags	= CTRL_CMD_HAS_BUF,
-		.addr = (__u64)params,
+		.addr = (__u64)(uintptr_t)params,
 		.len = sizeof(*params),
 	};
 
tools/testing/selftests/ublk/kublk.c +1 -1
index 5a4ff689c1f7a..115f25351d309 100644
@@ -148,7 +148,7 @@ static int ublk_ctrl_get_affinity(struct ublk_dev *ctrl_dev, cpu_set_t **ptr_buf
 	for (i = 0; i < ctrl_dev->dev_info.nr_hw_queues; i++) {
 		data.data[0] = i;
 		data.len = sizeof(cpu_set_t);
-		data.addr = (__u64)&buf[i];
+		data.addr = (__u64)(uintptr_t)&buf[i];
 
 		ret = __ublk_ctrl_cmd(ctrl_dev, &data);
 		if (ret < 0) {
Signature · 1 line
2.53.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 12 lines · Sasha Levin

kublk puts pointers into 64-bit UAPI and SQE fields with a plain (__u64) cast. ublk builds with -Werror, so it fails to build on 32-bit architectures:

batch.c:379:42: error: cast from pointer to integer of different size [-Werror=pointer-to-int-cast]

Cast through uintptr_t. kublk also passes 64-bit user copy offsets, which start at UBLKSRV_IO_BUF_OFFSET (2 GB), to pread() and pwrite(), and with glibc those are truncated to a 32-bit off_t there. Build with -D_FILE_OFFSET_BITS=64 as well.

Quoted 7 lines

---
Found with 32-bit builds of the selftests (gcc 16 and clang 21 with -m32, with and without -D_TIME_BITS=64 -D_FILE_OFFSET_BITS=64); KernelCI's 32-bit arm builds show the same warnings. The changed files build without them now, and the x86_64 builds don't change.

Thanks,

maintainer Applied
#2 lore raw
In reply to Sasha Levin
Quoted 4 lines · Sasha Levin

kublk puts pointers into 64-bit UAPI and SQE fields with a plain (__u64) cast. ublk builds with -Werror, so it fails to build on 32-bit architectures:

Quoted 8 lines

batch.c:379:42: error: cast from pointer to integer of different size [-Werror=pointer-to-int-cast]

Cast through uintptr_t. kublk also passes 64-bit user copy offsets, which start at UBLKSRV_IO_BUF_OFFSET (2 GB), to pread() and pwrite(), and with glibc those are truncated to a 32-bit off_t there. Build with -D_FILE_OFFSET_BITS=64 as well.

[...]

Applied, thanks!

[1/1] selftests: ublk: fix the build and file offsets on 32-bit

      commit: 3197517875fdcfae1e9b5a4dcaf35cbbd66c413c

Best regards,

Signature · 1 line
Jens Axboe