Skip to content

Commit d486650

Browse files
Ming Leiaxboe
authored andcommitted
selftests/ublk: add hugetlbfs shmem_zc test for loop target
Add test_shmem_zc_02.sh which tests the UBLK_IO_F_SHMEM_ZC zero-copy path on the loop target using a hugetlbfs shared buffer. Both kublk and fio mmap the same hugetlbfs file with MAP_SHARED, sharing physical pages. The kernel's PFN matching enables zero-copy — the loop target reads/writes directly from the shared buffer to the backing file. 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-9-ming.lei@redhat.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 2f1e946 commit d486650

2 files changed

Lines changed: 69 additions & 0 deletions

File tree

tools/testing/selftests/ublk/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ TEST_PROGS += test_part_01.sh
5353
TEST_PROGS += test_part_02.sh
5454

5555
TEST_PROGS += test_shmemzc_01.sh
56+
TEST_PROGS += test_shmemzc_02.sh
5657

5758
TEST_PROGS += test_stress_01.sh
5859
TEST_PROGS += test_stress_02.sh
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/bin/bash
2+
# SPDX-License-Identifier: GPL-2.0
3+
# Test: shmem_zc with hugetlbfs buffer on loop 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" "loop 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+
_create_backfile 0 128M
48+
BACKFILE="${UBLK_BACKFILES[0]}"
49+
50+
dev_id=$(_add_ublk_dev -t loop --shmem_zc --htlb "$HTLB_FILE" "$BACKFILE")
51+
_check_add_dev $TID $?
52+
53+
_run_fio_verify_io --filename=/dev/ublkb"${dev_id}" \
54+
--size=128M \
55+
--mem=mmaphuge:"$HTLB_FILE"
56+
ERR_CODE=$?
57+
58+
# Delete device first so daemon releases the htlb mmap
59+
_ublk_del_dev "${dev_id}"
60+
61+
rm -f "$HTLB_FILE"
62+
umount "$HTLB_MNT"
63+
rmdir "$HTLB_MNT"
64+
echo "$OLD_NR_HP" > /proc/sys/vm/nr_hugepages
65+
66+
_cleanup_test "shmem_zc"
67+
68+
_show_result $TID $ERR_CODE

0 commit comments

Comments
 (0)