From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.42 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796257982; b=4fhyLMgEVEQoBiD+b6sPq5LZ+W8ZjnQS/YoODMe+T6xsi6DjOtk6RwtNeo0aMf/TZK4x HXJsxjkeJhcRkb3g4ip47L5DielzfpiUCOfohDLBAzzB7jZ+vGv6wQCN/TDWpRzJ1lZYo ZrXvWJo8TymA4DRUlHHMZf8PJ2Yy9JeX6jtCITes+dqTf/mZ30Z04oSIdHF/Pwd7dN0BC YsVcEGwWbshB3sFT/1/S/ErVbAjTAWTYUcuyu/mPR4LHD0+KeUmeT+nwGsM4HvclBKhgP 4GKWCMds8C652cAg7t0Smha+GH7Xrlm9A7E/UmM4ARrHC+a+SL0UhEZC9q4QEQ/qvAw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796257982; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=h8nCM4rjv5jLg/frT67bbc/QQ0t2hQvCjv8o53yhnCc=; b=ampgRnPt9u/RMD54uZ1srNoo1OJfIiNIiyxETO3kQrilgbCOSX8v6oPGUF01slosWVpO CRh6UUIO4/jnYhFPGLlz0z/O0SS0gtgTSjjbdqSYOhWdvBF/VAuB8hbHLH3WZwCRHRD+0 Yy7V9Ir6qlayyoUScX/d0dBjDYYkNRZ4id1AXRz1xjCH7SkqBIypV8UBksLWatnj6SDe4 ZtCJRyeFibArA+cyY5wAjVI64+fwddynZfmjEmAgQXM8vJ/24okECrnu8StxgNUR84kuI sPIjIO8HpGspMV8oyieAOOfTgzno+YglQ7e1suqqriy74q2+1Hnoymeq1m6Nsfl7E4w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=Vu+naCry; arc=none smtp.remote-ip=209.85.210.42 Received: from mail-ot1-f42.google.com (mail-ot1-f42.google.com [209.85.210.42]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 41BF9606A32D for ; Thu, 3 Dec 2026 00:33:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 41BF9606A32D Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=Vu+naCry Received: by mail-ot1-f42.google.com with SMTP id 46e09a7af769-862b98b2e47so5546959a34.2 for ; Wed, 02 Dec 2026 16:33:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796257981; x=1796862781; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=h8nCM4rjv5jLg/frT67bbc/QQ0t2hQvCjv8o53yhnCc=; b=Vu+naCryTmvGjGn/IELWSTZEkWsJKgMQuPofRrr7hKfxTvyvXQhNCA4+X1uiF0fpeG w6dM+mBkirYvFc5Fd0PS2OVXgWkF1I9zHn44pXGMZC2cd84ewvzA3rZL3BSkqX0Nzm/z nkyJ1eepKuyg58vQ983MKuQ4NBuDsHU/G5yUk6XmBgVPuq/Tr7iepjjH0GM1lfqn8dIs UlaDsrSsfNZIDhDTZ3wzMEMjAQokIXNMRGVeJPVvxoHgze300Gba9Kyngv1b/dBWLv3D rFNqXQJq5FTJfbNWT3gZAnhjihiqSXqOwb7y+UzfJ3nyERIwdgnt84RtCk8UL4HqDy4j 1wwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796257981; x=1796862781; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=h8nCM4rjv5jLg/frT67bbc/QQ0t2hQvCjv8o53yhnCc=; b=zWesGktoXm52wEI2nsyTROvhaW4NCD4ZKZX4bKFR9Zx3XTT3gU96Mf6z7bFpeLi53O lLbSRV4pX5DG1MAQ9r0dUjo1+E1bktMoXBj4qTpxjmkgYJ5uPeD1JcMdUxgBSJe18F86 WC1xCENpnvpMQxJdCcs2N6xwoXOR0cuUNIP1wDgepsPfMztIdb8yCUJqe1IU/3ddIgDC +wAVd6PmiIgq0ngzoZ/cDeiibn9mmkxEDkbdL4jDxX+/WJC2rXZH4CBnGOmh5OqyRKJ5 Cz/0JwiW0Mt8l8I6CFXQM3jeymoJF6Vlg3YnQr/Fn0p0CZexpcSLm+e5zrXFG1qTkUf5 TsKA== X-Forwarded-Encrypted: i=1; AL+hErHuVgtoUz8Ki3DnGxviaSWR9kjwDPGE4QPdiHxW2/tDr0fRrC3ln/a8Ijrs1hQ6Hm36+p/0OWSnE70pXw==@vger.kernel.org X-Gm-Message-State: APuU4Z9fkmvb1Qk9W85vtyVl62YyFU+yIDZOGeWmrPxEZWxE+U8Avpq2 EsKI2NAuwytNjhp3obOMUhCzJFqdVYgkuYMmlG99IIkOlQFEs6loDL1yyKwYEYD4yKM= X-Gm-Gg: AZFdTZfjQdvX1qXjINF8eR/bBcsr7q2LWgGAAzbaiBDEAvXCdpxUyBGP/AVC2ZOe2SX g2n+RHwq73P+zNujZXzjQCi3DJ2/GZdgP3sqfXHzOxtCGemDKGoQ8XNxRowQj3b0HCaQiBpWglZ m9WQwCcJdGz4T5pbtp/FG99+B6PhiuBN2xYp6fzqlfJH6dTFq5QHTAnsvpMnbRFtrkr9PJQb2op Vw927KDLn5gSk6OAjnSlQU4lbm+Mt+o9lZlvfr2X8dA9w4+ROwdZba/BPE1VTz2za6dbriPm0Ne VZ8KQloKOw01BZmbW3MWbSFsnkaKWDjeZdfoN2j/N9pr5lCcUfGla44royLhMjMPKtONw4Thre4 log4rMuff08rdPFFPmQR2b4gFV9Sv4XERbzHX6GdBcLYxSRapjqvKA8zPFfVczuziOdZ96WgYLR mEHyb46dykbGCLLbTXSEYMrfXNlOuW5GdwyzbBLVNqWYEGPCvbyYj+g3N/tNAJ0JSt6VrtZn9dT qKSREKkO76dyz3RQT/Yglxyghu2S6613qPITMcB5M/VfiM7UINgyTIAqrCexkD3OlK83QfE0L3L EwE93tDZyOW1O5ld0jzW1uSWPYxhIOXRgSj1Yhn92w4RNsqtZonzhl4yUIae8DXz1NHTZxHkxER m9ccVRpDiIAYGd887UamW6exetHLKsE1p6PpeC//gGhFfJ7M4WvWEVsZJBw== X-Received: by 2002:a05:6830:4ac9:b0:862:bc33:da97 with SMTP id 46e09a7af769-863152c5e3fmr45546915a34.4.1796257981154; Wed, 02 Dec 2026 16:33:01 -0800 (PST) Received: from fedora-laptop.tail348456.ts.net ([172.245.82.59]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-863169105b7sm32213309a34.22.2026.12.02.16.32.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2026 16:33:00 -0800 (PST) From: Ming Lei To: Jens Axboe , linux-block@vger.kernel.org Cc: Ming Lei , Caleb Sander Mateos , Shuah Khan , linux-kselftest@vger.kernel.org, Utkarsh Singh Subject: [PATCH v4 5/5] selftests: ublk: test that a short request isn't completed in full Date: Wed, 2 Dec 2026 18:32:34 -0600 Message-ID: <20261203003236.1514775-6-tom.leiming@gmail.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261203003236.1514775-1-tom.leiming@gmail.com> References: <20261203003236.1514775-1-tom.leiming@gmail.com> X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Put the loop target's backing file on a tmpfs with room for 16 blocks and one page, and write 17 blocks with O_DIRECT. The server completes the last write short, with one page. Check that the write stops with ENOSPC at block 16, which means the kernel requeued the rest and the server failed it, and that every block reported as written reads back. Then cut the backing file to one page and read the first block. The server completes that read short and the rest reads nothing, so the read must fail. Run it with dd in the copy, user copy, zero copy, auto buffer registration and batch modes, and with fio in shmem zero copy mode, using a hugetlbfs buffer that kublk has registered. Signed-off-by: Ming Lei Tested-by: Utkarsh Singh --- tools/testing/selftests/ublk/Makefile | 1 + tools/testing/selftests/ublk/test_loop_09.sh | 294 +++++++++++++++++++ 2 files changed, 295 insertions(+) create mode 100755 tools/testing/selftests/ublk/test_loop_09.sh diff --git a/tools/testing/selftests/ublk/Makefile b/tools/testing/selftests/ublk/Makefile index 1a50d4695c9f..e3a7712b4d83 100644 --- a/tools/testing/selftests/ublk/Makefile +++ b/tools/testing/selftests/ublk/Makefile @@ -38,6 +38,7 @@ TEST_PROGS += test_loop_05.sh TEST_PROGS += test_loop_06.sh TEST_PROGS += test_loop_07.sh TEST_PROGS += test_loop_08.sh +TEST_PROGS += test_loop_09.sh TEST_PROGS += test_integrity_01.sh TEST_PROGS += test_integrity_02.sh diff --git a/tools/testing/selftests/ublk/test_loop_09.sh b/tools/testing/selftests/ublk/test_loop_09.sh new file mode 100755 index 000000000000..e74181959f43 --- /dev/null +++ b/tools/testing/selftests/ublk/test_loop_09.sh @@ -0,0 +1,294 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# +# Short write and read: the server completes a request with fewer bytes +# than asked. +# +# The loop target's backing file lives on a tmpfs with room for 16 blocks +# and one page. 17 blocks are written with O_DIRECT: the first 16 fit, +# and the server can store only one page of the last one, so it completes +# that request short. The kernel must complete only that page and requeue +# the rest, which then fails with ENOSPC. So the write must stop with +# ENOSPC at block 16, and every block reported as written must read back. +# +# Then the backing file is cut to one page and the first block is read: +# the server completes that read short, the rest reads nothing, so the +# read must fail, while a read of the first page alone must work. + +. "$(cd "$(dirname "$0")" && pwd)"/test_common.sh + +if ! _have_program cmp; then + exit "$UBLK_SKIP_CODE" +fi + +ERR_CODE=0 +NR_RUN=0 +STOP=0 +HTLB_MNT= +OLD_NR_HP= + +PS=$(getconf PAGESIZE) +BS=$((16 * PS)) +NR_FIT=16 +NR_BLKS=$((NR_FIT + 1)) + +_prep_test "loop" "short write and read must not be reported as complete" + +MNT=$(_create_tmp_dir) +BACKFILE="${MNT}/backfile" +PATTERN="${UBLK_TEST_DIR}/pattern" +READBACK="${UBLK_TEST_DIR}/readback" +LOG="${UBLK_TEST_DIR}/io.log" + +_mount_tmpfs() { + if ! mount -t tmpfs -o size=$((NR_FIT * BS + PS)) tmpfs "$MNT"; then + echo "can't mount tmpfs on $MNT" + return 1 + fi + truncate -s $((2 * NR_BLKS * BS)) "$BACKFILE" +} + +_umount_tmpfs() { + mountpoint -q "$MNT" || return 0 + rm -f "$BACKFILE" + if ! umount "$MNT"; then + echo "can't umount $MNT, stop here" + STOP=1 + return 1 + fi +} + +# Add the device on a fresh tmpfs, set DEV_ID. Returns the add result. +_setup_dev() { + local ret + + _mount_tmpfs || return "$UBLK_SKIP_CODE" + DEV_ID=$(_add_ublk_dev -t loop "$@" "$BACKFILE") + ret=$? + if [ "$ret" -ne 0 ]; then + [ "$ret" -ne "$UBLK_SKIP_CODE" ] && echo "can't add ublk device: $*" + _umount_tmpfs + return "$ret" + fi +} + +_cleanup_htlb() { + if [ -n "$HTLB_MNT" ]; then + rm -f "$HTLB_MNT"/buf + umount "$HTLB_MNT" + _remove_tmp_dir "$HTLB_MNT" + HTLB_MNT= + fi + if [ -n "$OLD_NR_HP" ]; then + echo "$OLD_NR_HP" > /proc/sys/vm/nr_hugepages + OLD_NR_HP= + fi +} + +# Don't leave the device, the mounts or the huge pages behind if interrupted. +# The signal trap runs only after the current command, so a device that is +# being added is in .ublk_devs by then. +_exit_cleanup() { + local devs=() + local dev_id + + [ -f "${UBLK_TEST_DIR}/.ublk_devs" ] && + mapfile -t devs < "${UBLK_TEST_DIR}/.ublk_devs" + for dev_id in "${devs[@]}"; do + _ublk_del_dev "$dev_id" + done + _umount_tmpfs + _cleanup_htlb +} +trap _exit_cleanup EXIT +trap 'exit 1' INT TERM + +# Cut the backing file to one page: a read of the first block is completed +# short and its rest reads nothing, so it must fail with EIO. The read +# command follows the device id. +_check_short_read() { + local dev_id=$1 + shift + + truncate -s "$PS" "$BACKFILE" + if "$@" > "$LOG" 2>&1; then + echo "read past the end of the backing file didn't fail" + return 1 + fi + if ! grep -q "Input/output error" "$LOG"; then + echo "read past the end of the backing file didn't fail with EIO" + return 1 + fi + dd if=/dev/ublkb"${dev_id}" of="$READBACK" bs="$PS" count=1 \ + iflag=direct status=none || return 1 +} + +_dd_read_blk() { + LC_ALL=C dd if=/dev/ublkb"$1" of=/dev/null bs="$BS" count=1 iflag=direct +} + +# Check that the first $2 blocks of /dev/ublkb$1 read back as $3 +_check_readback() { + local dev_id=$1 + local nr=$2 + local expect=$3 + + dd if=/dev/ublkb"${dev_id}" of="$READBACK" bs="$BS" count="$nr" \ + iflag=direct status=none + cmp "$READBACK" <(head -c $((nr * BS)) "$expect") +} + +# Returns 0 on pass, UBLK_SKIP_CODE if the mode isn't supported, 255 on fail +_short_write_test() { + local mode="${*:-copy}" + local dev_id + local nr_out + local ret=0 + + _setup_dev "$@" || return $? + dev_id=$DEV_ID + + LC_ALL=C dd if="$PATTERN" of=/dev/ublkb"${dev_id}" bs="$BS" \ + count="$NR_BLKS" oflag=direct 2> "$LOG" + nr_out=$(sed -n 's/^\([0-9]*\)+[0-9]* records out$/\1/p' "$LOG") + nr_out=${nr_out:-0} + + if [ "$nr_out" -ne "$NR_FIT" ]; then + echo "$mode: $nr_out blocks reported written, expected $NR_FIT" + ret=255 + elif ! grep -q "No space left on device" "$LOG"; then + echo "$mode: write didn't fail with ENOSPC" + ret=255 + fi + if ! _check_readback "$dev_id" "$nr_out" "$PATTERN"; then + echo "$mode: data reported as written doesn't read back" + ret=255 + fi + [ "$ret" -ne 0 ] && cat "$LOG" + + if ! _check_short_read "$dev_id" _dd_read_blk "$dev_id" || + ! cmp -n "$PS" "$READBACK" "$PATTERN"; then + echo "$mode: short read isn't handled" + cat "$LOG" + ret=255 + fi + + _ublk_del_dev "${dev_id}" + _umount_tmpfs || ret=255 + return $ret +} + +_fio_read_blk() { + LC_ALL=C fio --name=short_read --filename=/dev/ublkb"$1" --rw=read --bs="$BS" \ + --size="$BS" --direct=1 --ioengine=psync --mem=mmaphuge:"$2" \ + --hugepage-size="$3" +} + +# Same with shmem zero copy: fio writes from a hugetlbfs buffer that kublk +# has registered, so the request pages are the server's buffer. +_short_write_shmem_zc_test() { + local mode="--shmem_zc" + local hp_size + local htlb_file + local dev_id + local nr_ok=$NR_FIT + local ret=0 + + if ! _have_program fio || ! grep -q hugetlbfs /proc/filesystems; then + return "$UBLK_SKIP_CODE" + fi + hp_size=$(awk '/^Hugepagesize:/ { print $2 * 1024 }' /proc/meminfo) + # skip huge pages that are too small, or too big to spare a few + if [ -z "$hp_size" ] || [ "$hp_size" -lt "$BS" ] || + [ "$hp_size" -gt $((64 << 20)) ]; then + return "$UBLK_SKIP_CODE" + fi + + OLD_NR_HP=$(cat /proc/sys/vm/nr_hugepages) + echo $((OLD_NR_HP + 4)) > /proc/sys/vm/nr_hugepages + HTLB_MNT=$(_create_tmp_dir) + htlb_file="${HTLB_MNT}/buf" + if ! mount -t hugetlbfs none "$HTLB_MNT"; then + _remove_tmp_dir "$HTLB_MNT" + HTLB_MNT= + _cleanup_htlb + return "$UBLK_SKIP_CODE" + fi + # fails if the huge pages can't be had, or another test took them + if ! fallocate -l "$hp_size" "$htlb_file"; then + _cleanup_htlb + return "$UBLK_SKIP_CODE" + fi + + if _setup_dev --shmem_zc --htlb "$htlb_file"; then + dev_id=$DEV_ID + # fio fills each write buffer with 0x5a, which is 'Z' + LC_ALL=C fio --name=short_write --filename=/dev/ublkb"${dev_id}" \ + --rw=write --bs="$BS" --size=$((NR_BLKS * BS)) \ + --direct=1 --ioengine=psync --mem=mmaphuge:"$htlb_file" \ + --hugepage-size="$hp_size" \ + --verify=pattern --verify_pattern=0x5a --do_verify=0 \ + > "$LOG" 2>&1 + if ! grep -q "No space left on device" "$LOG"; then + echo "$mode: write didn't fail with ENOSPC" + nr_ok=$NR_BLKS + ret=255 + elif ! grep -q "write offset=$((NR_FIT * BS))," "$LOG"; then + echo "$mode: write failed at the wrong offset" + ret=255 + fi + head -c $((NR_BLKS * BS)) /dev/zero | tr '\0' 'Z' > "$PATTERN.Z" + if ! _check_readback "$dev_id" "$nr_ok" "$PATTERN.Z"; then + echo "$mode: data reported as written doesn't read back" + ret=255 + fi + [ "$ret" -ne 0 ] && cat "$LOG" + + if ! _check_short_read "$dev_id" _fio_read_blk "$dev_id" \ + "$htlb_file" "$hp_size" || + ! cmp -n "$PS" "$READBACK" "$PATTERN.Z"; then + echo "$mode: short read isn't handled" + cat "$LOG" + ret=255 + fi + rm -f "$PATTERN.Z" + + # Delete the device first so kublk releases the buffer + _ublk_del_dev "${dev_id}" + _umount_tmpfs || ret=255 + else + ret=$? + fi + + _cleanup_htlb + return $ret +} + +_run() { + local ret + + [ "$STOP" -ne 0 ] && return + "$@" + ret=$? + if [ "$ret" -eq 0 ]; then + NR_RUN=$((NR_RUN + 1)) + elif [ "$ret" -ne "$UBLK_SKIP_CODE" ]; then + NR_RUN=$((NR_RUN + 1)) + ERR_CODE=255 + fi +} + +dd if=/dev/urandom of="$PATTERN" bs="$BS" count="$NR_BLKS" status=none + +_run _short_write_test +_have_feature "USER_COPY" && _run _short_write_test -u +_have_feature "ZERO_COPY" && _run _short_write_test -z +_have_feature "AUTO_BUF_REG" && _run _short_write_test --auto_zc +_have_feature "BATCH_IO" && _run _short_write_test -b +_have_feature "SHMEM_ZC" && _run _short_write_shmem_zc_test + +rm -f "$PATTERN" "$READBACK" "$LOG" +[ "$STOP" -eq 0 ] && _remove_tmp_dir "$MNT" +[ "$NR_RUN" -eq 0 ] && [ "$ERR_CODE" -eq 0 ] && ERR_CODE=$UBLK_SKIP_CODE +_cleanup_test +_show_result "$TID" "$ERR_CODE" -- 2.56.1