Skip to content

Commit 2f1e946

Browse files
Ming Leiaxboe
authored andcommitted
selftests/ublk: add shared memory zero-copy test
Add test_shmem_zc_01.sh which tests UBLK_IO_F_SHMEM_ZC on the null target using a hugetlbfs shared buffer. Both kublk (--htlb) and fio (--mem=mmaphuge:<path>) mmap the same hugetlbfs file with MAP_SHARED, sharing physical pages. The kernel PFN match enables zero-copy I/O. Uses standard fio --mem=mmaphuge:<path> (supported since fio 1.10), no patched fio required. Signed-off-by: Ming Lei <ming.lei@redhat.com> Link: https://patch.msgid.link/20260331153207.3635125-8-ming.lei@redhat.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent ec20aa4 commit 2f1e946

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

tools/testing/selftests/ublk/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ TEST_PROGS += test_stripe_06.sh
5252
TEST_PROGS += test_part_01.sh
5353
TEST_PROGS += test_part_02.sh
5454

55+
TEST_PROGS += test_shmemzc_01.sh
56+
5557
TEST_PROGS += test_stress_01.sh
5658
TEST_PROGS += test_stress_02.sh
5759
TEST_PROGS += test_stress_03.sh
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/bin/bash
2+
# SPDX-License-Identifier: GPL-2.0
3+
# Test: shmem_zc with hugetlbfs buffer on null target
4+
#
5+
# kublk and fio both mmap the same hugetlbfs file (MAP_SHARED),
6+
# so they share physical pages. The kernel PFN match enables
7+
# zero-copy I/O without socket-based fd passing.
8+
9+
. "$(cd "$(dirname "$0")" && pwd)"/test_common.sh
10+
11+
ERR_CODE=0
12+
13+
_prep_test "shmem_zc" "null target hugetlbfs shmem zero-copy test"
14+
15+
if ! _have_program fio; then
16+
echo "SKIP: fio not available"
17+
exit "$UBLK_SKIP_CODE"
18+
fi
19+
20+
if ! grep -q hugetlbfs /proc/filesystems; then
21+
echo "SKIP: hugetlbfs not supported"
22+
exit "$UBLK_SKIP_CODE"
23+
fi
24+
25+
# Allocate hugepages
26+
OLD_NR_HP=$(cat /proc/sys/vm/nr_hugepages)
27+
echo 10 > /proc/sys/vm/nr_hugepages
28+
NR_HP=$(cat /proc/sys/vm/nr_hugepages)
29+
if [ "$NR_HP" -lt 2 ]; then
30+
echo "SKIP: cannot allocate hugepages"
31+
echo "$OLD_NR_HP" > /proc/sys/vm/nr_hugepages
32+
exit "$UBLK_SKIP_CODE"
33+
fi
34+
35+
# Mount hugetlbfs
36+
HTLB_MNT=$(mktemp -d "${UBLK_TEST_DIR}/htlb_mnt_XXXXXX")
37+
if ! mount -t hugetlbfs none "$HTLB_MNT"; then
38+
echo "SKIP: cannot mount hugetlbfs"
39+
rmdir "$HTLB_MNT"
40+
echo "$OLD_NR_HP" > /proc/sys/vm/nr_hugepages
41+
exit "$UBLK_SKIP_CODE"
42+
fi
43+
44+
HTLB_FILE="$HTLB_MNT/ublk_buf"
45+
fallocate -l 4M "$HTLB_FILE"
46+
47+
dev_id=$(_add_ublk_dev -t null --shmem_zc --htlb "$HTLB_FILE")
48+
_check_add_dev $TID $?
49+
50+
fio --name=htlb_zc \
51+
--filename=/dev/ublkb"${dev_id}" \
52+
--ioengine=io_uring \
53+
--rw=randwrite \
54+
--direct=1 \
55+
--bs=4k \
56+
--size=4M \
57+
--iodepth=32 \
58+
--mem=mmaphuge:"$HTLB_FILE" \
59+
> /dev/null 2>&1
60+
ERR_CODE=$?
61+
62+
# Delete device first so daemon releases the htlb mmap
63+
_ublk_del_dev "${dev_id}"
64+
65+
rm -f "$HTLB_FILE"
66+
umount "$HTLB_MNT"
67+
rmdir "$HTLB_MNT"
68+
echo "$OLD_NR_HP" > /proc/sys/vm/nr_hugepages
69+
70+
_cleanup_test "shmem_zc"
71+
72+
_show_result $TID $ERR_CODE

0 commit comments

Comments
 (0)