Skip to content

Commit 4d4a512

Browse files
Ming Leiaxboe
authored andcommitted
ublk: add PFN-based buffer matching in I/O path
Add ublk_try_buf_match() which walks a request's bio_vecs, looks up each page's PFN in the per-device maple tree, and verifies all pages belong to the same registered buffer at contiguous offsets. Add ublk_iod_is_shmem_zc() inline helper for checking whether a request uses the shmem zero-copy path. Integrate into the I/O path: - ublk_setup_iod(): if pages match a registered buffer, set UBLK_IO_F_SHMEM_ZC and encode buffer index + offset in addr - ublk_start_io(): skip ublk_map_io() for zero-copy requests - __ublk_complete_rq(): skip ublk_unmap_io() for zero-copy requests The feature remains disabled (ublk_support_shmem_zc() returns false) until the UBLK_F_SHMEM_ZC flag is enabled in the next patch. Signed-off-by: Ming Lei <ming.lei@redhat.com> Link: https://patch.msgid.link/20260331153207.3635125-3-ming.lei@redhat.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 2fb0ded commit 4d4a512

1 file changed

Lines changed: 76 additions & 1 deletion

File tree

drivers/block/ublk_drv.c

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,8 @@ struct ublk_params_header {
356356

357357
static void ublk_io_release(void *priv);
358358
static void ublk_stop_dev_unlocked(struct ublk_device *ub);
359+
static bool ublk_try_buf_match(struct ublk_device *ub, struct request *rq,
360+
u32 *buf_idx, u32 *buf_off);
359361
static void ublk_buf_cleanup(struct ublk_device *ub);
360362
static void ublk_abort_queue(struct ublk_device *ub, struct ublk_queue *ubq);
361363
static inline struct request *__ublk_check_and_get_req(struct ublk_device *ub,
@@ -426,6 +428,12 @@ static inline bool ublk_support_shmem_zc(const struct ublk_queue *ubq)
426428
return false;
427429
}
428430

431+
static inline bool ublk_iod_is_shmem_zc(const struct ublk_queue *ubq,
432+
unsigned int tag)
433+
{
434+
return ublk_get_iod(ubq, tag)->op_flags & UBLK_IO_F_SHMEM_ZC;
435+
}
436+
429437
static inline bool ublk_dev_support_shmem_zc(const struct ublk_device *ub)
430438
{
431439
return false;
@@ -1494,6 +1502,18 @@ static blk_status_t ublk_setup_iod(struct ublk_queue *ubq, struct request *req)
14941502
iod->nr_sectors = blk_rq_sectors(req);
14951503
iod->start_sector = blk_rq_pos(req);
14961504

1505+
/* Try shmem zero-copy match before setting addr */
1506+
if (ublk_support_shmem_zc(ubq) && ublk_rq_has_data(req)) {
1507+
u32 buf_idx, buf_off;
1508+
1509+
if (ublk_try_buf_match(ubq->dev, req,
1510+
&buf_idx, &buf_off)) {
1511+
iod->op_flags |= UBLK_IO_F_SHMEM_ZC;
1512+
iod->addr = ublk_shmem_zc_addr(buf_idx, buf_off);
1513+
return BLK_STS_OK;
1514+
}
1515+
}
1516+
14971517
iod->addr = io->buf.addr;
14981518

14991519
return BLK_STS_OK;
@@ -1539,6 +1559,10 @@ static inline void __ublk_complete_rq(struct request *req, struct ublk_io *io,
15391559
req_op(req) != REQ_OP_DRV_IN)
15401560
goto exit;
15411561

1562+
/* shmem zero copy: no data to unmap, pages already shared */
1563+
if (ublk_iod_is_shmem_zc(req->mq_hctx->driver_data, req->tag))
1564+
goto exit;
1565+
15421566
/* for READ request, writing data in iod->addr to rq buffers */
15431567
unmapped_bytes = ublk_unmap_io(need_map, req, io);
15441568

@@ -1697,8 +1721,13 @@ static void ublk_auto_buf_dispatch(const struct ublk_queue *ubq,
16971721
static bool ublk_start_io(const struct ublk_queue *ubq, struct request *req,
16981722
struct ublk_io *io)
16991723
{
1700-
unsigned mapped_bytes = ublk_map_io(ubq, req, io);
1724+
unsigned mapped_bytes;
17011725

1726+
/* shmem zero copy: skip data copy, pages already shared */
1727+
if (ublk_iod_is_shmem_zc(ubq, req->tag))
1728+
return true;
1729+
1730+
mapped_bytes = ublk_map_io(ubq, req, io);
17021731

17031732
/* partially mapped, update io descriptor */
17041733
if (unlikely(mapped_bytes != blk_rq_bytes(req))) {
@@ -5458,7 +5487,53 @@ static void ublk_buf_cleanup(struct ublk_device *ub)
54585487
mtree_destroy(&ub->buf_tree);
54595488
}
54605489

5490+
/* Check if request pages match a registered shared memory buffer */
5491+
static bool ublk_try_buf_match(struct ublk_device *ub,
5492+
struct request *rq,
5493+
u32 *buf_idx, u32 *buf_off)
5494+
{
5495+
struct req_iterator iter;
5496+
struct bio_vec bv;
5497+
int index = -1;
5498+
unsigned long expected_offset = 0;
5499+
bool first = true;
5500+
5501+
rq_for_each_bvec(bv, rq, iter) {
5502+
unsigned long pfn = page_to_pfn(bv.bv_page);
5503+
struct ublk_buf_range *range;
5504+
unsigned long off;
54615505

5506+
range = mtree_load(&ub->buf_tree, pfn);
5507+
if (!range)
5508+
return false;
5509+
5510+
off = range->base_offset +
5511+
(pfn - range->base_pfn) * PAGE_SIZE + bv.bv_offset;
5512+
5513+
if (first) {
5514+
/* Read-only buffer can't serve READ (kernel writes) */
5515+
if ((range->flags & UBLK_SHMEM_BUF_READ_ONLY) &&
5516+
req_op(rq) != REQ_OP_WRITE)
5517+
return false;
5518+
index = range->buf_index;
5519+
expected_offset = off;
5520+
*buf_off = off;
5521+
first = false;
5522+
} else {
5523+
if (range->buf_index != index)
5524+
return false;
5525+
if (off != expected_offset)
5526+
return false;
5527+
}
5528+
expected_offset += bv.bv_len;
5529+
}
5530+
5531+
if (first)
5532+
return false;
5533+
5534+
*buf_idx = index;
5535+
return true;
5536+
}
54625537

54635538
static int ublk_ctrl_uring_cmd_permission(struct ublk_device *ub,
54645539
u32 cmd_op, struct ublksrv_ctrl_cmd *header)

0 commit comments

Comments
 (0)