Skip to content

Commit ec20aa4

Browse files
Ming Leiaxboe
authored andcommitted
selftests/ublk: add UBLK_F_SHMEM_ZC support for loop target
Add loop_queue_shmem_zc_io() which handles I/O requests marked with UBLK_IO_F_SHMEM_ZC. When the kernel sets this flag, the request data lives in a registered shared memory buffer — decode index + offset from iod->addr and use the server's mmap as the I/O buffer. The dispatch check in loop_queue_tgt_rw_io() routes SHMEM_ZC requests to this new function, bypassing the normal buffer registration path. Signed-off-by: Ming Lei <ming.lei@redhat.com> Link: https://patch.msgid.link/20260331153207.3635125-7-ming.lei@redhat.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 166b476 commit ec20aa4

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

tools/testing/selftests/ublk/file_backed.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,40 @@ static int loop_queue_flush_io(struct ublk_thread *t, struct ublk_queue *q,
2727
return 1;
2828
}
2929

30+
/*
31+
* Shared memory zero-copy I/O: when UBLK_IO_F_SHMEM_ZC is set, the
32+
* request's data lives in a registered shared memory buffer. Decode
33+
* index + offset from iod->addr and use the server's mmap of that
34+
* buffer as the I/O buffer for the backing file.
35+
*/
36+
static int loop_queue_shmem_zc_io(struct ublk_thread *t, struct ublk_queue *q,
37+
const struct ublksrv_io_desc *iod, int tag)
38+
{
39+
unsigned ublk_op = ublksrv_get_op(iod);
40+
enum io_uring_op op = ublk_to_uring_op(iod, 0);
41+
__u64 file_offset = iod->start_sector << 9;
42+
__u32 len = iod->nr_sectors << 9;
43+
__u32 shmem_idx = ublk_shmem_zc_index(iod->addr);
44+
__u32 shmem_off = ublk_shmem_zc_offset(iod->addr);
45+
struct io_uring_sqe *sqe[1];
46+
void *addr;
47+
48+
if (shmem_idx >= UBLK_BUF_MAX || !shmem_table[shmem_idx].mmap_base)
49+
return -EINVAL;
50+
51+
addr = shmem_table[shmem_idx].mmap_base + shmem_off;
52+
53+
ublk_io_alloc_sqes(t, sqe, 1);
54+
if (!sqe[0])
55+
return -ENOMEM;
56+
57+
io_uring_prep_rw(op, sqe[0], ublk_get_registered_fd(q, 1),
58+
addr, len, file_offset);
59+
io_uring_sqe_set_flags(sqe[0], IOSQE_FIXED_FILE);
60+
sqe[0]->user_data = build_user_data(tag, ublk_op, 0, q->q_id, 1);
61+
return 1;
62+
}
63+
3064
static int loop_queue_tgt_rw_io(struct ublk_thread *t, struct ublk_queue *q,
3165
const struct ublksrv_io_desc *iod, int tag)
3266
{
@@ -41,6 +75,10 @@ static int loop_queue_tgt_rw_io(struct ublk_thread *t, struct ublk_queue *q,
4175
void *addr = io->buf_addr;
4276
unsigned short buf_index = ublk_io_buf_idx(t, q, tag);
4377

78+
/* shared memory zero-copy path */
79+
if (iod->op_flags & UBLK_IO_F_SHMEM_ZC)
80+
return loop_queue_shmem_zc_io(t, q, iod, tag);
81+
4482
if (iod->op_flags & UBLK_IO_F_INTEGRITY) {
4583
ublk_io_alloc_sqes(t, sqe, 1);
4684
/* Use second backing file for integrity data */

0 commit comments

Comments
 (0)