Skip to content

Commit 23b3b6f

Browse files
ming1axboe
authored andcommitted
ublk: widen ublk_shmem_buf_reg.len to __u64 for 4GB buffer support
The __u32 len field cannot represent a 4GB buffer (0x100000000 overflows to 0). Change it to __u64 so buffers up to 4GB can be registered. Add a reserved field for alignment and validate it is zero. The kernel enforces a default max of 4GB (UBLK_SHMEM_BUF_SIZE_MAX) which may be increased in future. Signed-off-by: Ming Lei <tom.leiming@gmail.com> Link: https://patch.msgid.link/20260409133020.3780098-2-tom.leiming@gmail.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent d0cc5f5 commit 23b3b6f

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

drivers/block/ublk_drv.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@
6363
#define UBLK_CMD_REG_BUF _IOC_NR(UBLK_U_CMD_REG_BUF)
6464
#define UBLK_CMD_UNREG_BUF _IOC_NR(UBLK_U_CMD_UNREG_BUF)
6565

66+
/* Default max shmem buffer size: 4GB (may be increased in future) */
67+
#define UBLK_SHMEM_BUF_SIZE_MAX (1ULL << 32)
68+
6669
#define UBLK_IO_REGISTER_IO_BUF _IOC_NR(UBLK_U_IO_REGISTER_IO_BUF)
6770
#define UBLK_IO_UNREGISTER_IO_BUF _IOC_NR(UBLK_U_IO_UNREGISTER_IO_BUF)
6871

@@ -5351,11 +5354,15 @@ static int ublk_ctrl_reg_buf(struct ublk_device *ub,
53515354
if (buf_reg.flags & ~UBLK_SHMEM_BUF_READ_ONLY)
53525355
return -EINVAL;
53535356

5357+
if (buf_reg.reserved)
5358+
return -EINVAL;
5359+
53545360
addr = buf_reg.addr;
53555361
size = buf_reg.len;
53565362
nr_pages = size >> PAGE_SHIFT;
53575363

5358-
if (!size || !PAGE_ALIGNED(size) || !PAGE_ALIGNED(addr))
5364+
if (!size || size > UBLK_SHMEM_BUF_SIZE_MAX ||
5365+
!PAGE_ALIGNED(size) || !PAGE_ALIGNED(addr))
53595366
return -EINVAL;
53605367

53615368
disk = ublk_get_disk(ub);

include/uapi/linux/ublk_cmd.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,9 @@
8989
/* Parameter buffer for UBLK_U_CMD_REG_BUF, pointed to by ctrl_cmd.addr */
9090
struct ublk_shmem_buf_reg {
9191
__u64 addr; /* userspace virtual address of shared memory */
92-
__u32 len; /* buffer size in bytes (page-aligned, max 4GB) */
92+
__u64 len; /* buffer size in bytes, page-aligned, default max 4GB */
9393
__u32 flags;
94+
__u32 reserved;
9495
};
9596

9697
/* Pin pages without FOLL_WRITE; usable with write-sealed memfd */

0 commit comments

Comments
 (0)