Skip to content

ublk: add shared memory zero-copy support

Ming Lei linux-block 2 versions – Applied

Status from this message: Applied, thanks!

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v2 Latest10 patches 21 repliesApplied
  2. v18 patches 0 repliesSuperseded

Changes in v2 from the cover letter

- rename struct ublk_buf_reg to struct ublk_shmem_buf_reg, add __u32 flags field for extensibility, narrow __u64 len to __u32 (max 4GB per UBLK_SHMEM_ZC_OFF_MASK), remove __u32 reserved (patch 1)
- add UBLK_SHMEM_BUF_READ_ONLY flag: pin pages without FOLL_WRITE, enabling registration of write-sealed memfd buffers (patch 1)
- use backward-compatible struct reading: memset zero + copy min(header->len, sizeof(struct)) (patch 1)
- reorder struct ublk_buf_range fields for better packing (16 bytes vs 24 bytes), change buf_index to unsigned short, add unsigned short flags to store per-range read-only state (patch 1)
- enforce read-only buffer semantics in ublk_try_buf_match(): reject non-WRITE requests on read-only buffers since READ I/O needs to write data into the buffer (patch 2)
- narrow struct ublk_buf::nr_pages to unsigned int, narrow struct ublk_buf_range::base_offset to unsigned int (patch 1)
- add new patch 4: eliminate permanent pages[] array from struct ublk_buf — recover struct page pointers via pfn_to_page() from the maple tree during unregistration, saving 2MB per 1GB buffer
- add UBLK_F_SHMEM_ZC to feat_map in kublk (patch 5)
- add new patch 10: read-only buffer registration selftest with --rdonly_shmem_buf option on null target + hugetlbfs

Patches in v2 Applied

#Subject and filesSizeRepliesApplied
1/10ublk: add UBLK_U_CMD_REG_BUF/UNREG_BUF control commandsdrivers/block/ublk_drv.c include/uapi/linux/ublk_cmd.h+372 -06 replies
2/10ublk: add PFN-based buffer matching in I/O pathdrivers/block/ublk_drv.c+76 -13 replies
3/10ublk: enable UBLK_F_SHMEM_ZC feature flagDocumentation/block/ublk.rst drivers/block/ublk_drv.c include/uapi/linux/ublk_cmd.h+128 -32 replies
4/10ublk: eliminate permanent pages[] array from struct ublk_bufdrivers/block/ublk_drv.c+55 -322 replies
5/10selftests/ublk: add shared memory zero-copy support in kublktools/testing/selftests/ublk/kublk.c tools/testing/selftests/ublk/kublk.h+352 -20 replies
6/10selftests/ublk: add UBLK_F_SHMEM_ZC support for loop targettools/testing/selftests/ublk/file_backed.c+38 -00 replies
7/10selftests/ublk: add shared memory zero-copy testtools/testing/selftests/ublk/Makefile tools/testing/selftests/ublk/test_shmemzc_01.sh+74 -00 replies
8/10selftests/ublk: add hugetlbfs shmem_zc test for loop targettools/testing/selftests/ublk/Makefile tools/testing/selftests/ublk/test_shmemzc_02.sh+69 -00 replies
9/10selftests/ublk: add filesystem fio verify test for shmem_zctools/testing/selftests/ublk/Makefile tools/testing/selftests/ublk/test_common.sh tools/testing/selftests/ublk/test_shmemzc_03.sh+81 -40 replies
10/10selftests/ublk: add read-only buffer registration testtools/testing/selftests/ublk/Makefile tools/testing/selftests/ublk/kublk.c tools/testing/selftests/ublk/kublk.h +1 more+86 -50 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v2 yet.

Read the v2 thread

Cover letter [PATCH v2 00/10] ublk: add shared memory zero-copy support

Hello,

Add shared memory based zero-copy (UBLK_F_SHMEM_ZC) support for ublk.

The ublk server and its client share a memory region (e.g. memfd or hugetlbfs file) via MAP_SHARED mmap. The server registers this region with the kernel via UBLK_U_CMD_REG_BUF, which pins the pages and builds a PFN maple tree. When I/O arrives, the driver looks up bio pages in the maple tree — if they match registered buffer pages, the data is used directly without copying.

Please see details on document added in patch 3.

Patches 1-4 implement the kernel side:
- buffer register/unregister control commands with PFN coalescing, including read-only buffer support (UBLK_SHMEM_BUF_READ_ONLY)
- PFN-based matching in the I/O path, with enforcement that read-only buffers reject non-WRITE requests
- UBLK_F_SHMEM_ZC feature flag
- eliminate permanent pages[] array from struct ublk_buf; the maple tree already stores PFN ranges, so pages[] becomes temporary

Patches 5-10 add kublk (selftest server) support and tests:
- hugetlbfs buffer sharing (both kublk and fio mmap the same file)
- null target and loop target tests with fio verify
- filesystem-level test (ext4 on ublk, fio verify on a file)
- read-only buffer registration test (--rdonly_shmem_buf)

Changes since V1:
- rename struct ublk_buf_reg to struct ublk_shmem_buf_reg, add __u32 flags field for extensibility, narrow __u64 len to __u32 (max 4GB per UBLK_SHMEM_ZC_OFF_MASK), remove __u32 reserved (patch 1)
- add UBLK_SHMEM_BUF_READ_ONLY flag: pin pages without FOLL_WRITE, enabling registration of write-sealed memfd buffers (patch 1)
- use backward-compatible struct reading: memset zero + copy min(header->len, sizeof(struct)) (patch 1)
- reorder struct ublk_buf_range fields for better packing (16 bytes vs 24 bytes), change buf_index to unsigned short, add unsigned short flags to store per-range read-only state (patch 1)
- enforce read-only buffer semantics in ublk_try_buf_match(): reject non-WRITE requests on read-only buffers since READ I/O needs to write data into the buffer (patch 2)
- narrow struct ublk_buf::nr_pages to unsigned int, narrow struct ublk_buf_range::base_offset to unsigned int (patch 1)
- add new patch 4: eliminate permanent pages[] array from struct ublk_buf — recover struct page pointers via pfn_to_page() from the maple tree during unregistration, saving 2MB per 1GB buffer
- add UBLK_F_SHMEM_ZC to feat_map in kublk (patch 5)
- add new patch 10: read-only buffer registration selftest with --rdonly_shmem_buf option on null target + hugetlbfs

Ming Lei (10):
ublk: add UBLK_U_CMD_REG_BUF/UNREG_BUF control commands
ublk: add PFN-based buffer matching in I/O path
ublk: enable UBLK_F_SHMEM_ZC feature flag
ublk: eliminate permanent pages[] array from struct ublk_buf selftests/ublk: add shared memory zero-copy support in kublk selftests/ublk: add UBLK_F_SHMEM_ZC support for loop target selftests/ublk: add shared memory zero-copy test selftests/ublk: add hugetlbfs shmem_zc test for loop target selftests/ublk: add filesystem fio verify test for shmem_zc selftests/ublk: add read-only buffer registration test

 Documentation/block/ublk.rst                  | 117 +++++
 drivers/block/ublk_drv.c                      | 403 +++++++++++++++++-
 include/uapi/linux/ublk_cmd.h                 |  79 ++++
 tools/testing/selftests/ublk/Makefile         |   5 +
 tools/testing/selftests/ublk/file_backed.c    |  38 ++
 tools/testing/selftests/ublk/kublk.c          | 347 ++++++++++++++-
 tools/testing/selftests/ublk/kublk.h          |  15 +
 tools/testing/selftests/ublk/test_common.sh   |  15 +-
 .../testing/selftests/ublk/test_shmemzc_01.sh |  72 ++++
 .../testing/selftests/ublk/test_shmemzc_02.sh |  68 +++
 .../testing/selftests/ublk/test_shmemzc_03.sh |  69 +++
 .../testing/selftests/ublk/test_shmemzc_04.sh |  72 ++++
 12 files changed, 1292 insertions(+), 8 deletions(-)
 create mode 100755 tools/testing/selftests/ublk/test_shmemzc_01.sh
 create mode 100755 tools/testing/selftests/ublk/test_shmemzc_02.sh
 create mode 100755 tools/testing/selftests/ublk/test_shmemzc_03.sh
 create mode 100755 tools/testing/selftests/ublk/test_shmemzc_04.sh

--
2.53.0