selftests: ublk: add rotating auto_buf index regression test

Batch AUTO_BUF_REG COMMIT must unregister the old auto_buf index before
storing the next one. Fixed per-tag indexing (A == B) masks bugs that
clear after overwriting io->buf.

Add kublk --rotate_auto_buf so each tag alternates between two sparse
buffer indices, and test_batch_04.sh to exercise that path. Without the
driver fix, the request ref stays stuck and I/O hangs; the test uses a
short timeout and kills the ublk daemon to recover. With the fix, a
small write completes quickly.

Signed-off-by: Yang Xiuwei <yangxiuwei@kylinos.cn>
Reviewed-by: Ming Lei <tom.leiming@gmail.com>
Link: https://patch.msgid.link/20260730024050.1062354-1-yangxiuwei@kylinos.cn
Signed-off-by: Jens Axboe <axboe@kernel.dk>
This commit is contained in:
Yang Xiuwei
2026-07-30 10:40:50 +08:00
committed by Jens Axboe
parent ca5a01eee3
commit d507d3cb19
5 changed files with 82 additions and 3 deletions

View File

@@ -23,6 +23,7 @@ TEST_PROGS += test_generic_17.sh
TEST_PROGS += test_batch_01.sh
TEST_PROGS += test_batch_02.sh
TEST_PROGS += test_batch_03.sh
TEST_PROGS += test_batch_04.sh
TEST_PROGS += test_null_01.sh
TEST_PROGS += test_null_02.sh

View File

@@ -535,7 +535,7 @@ void ublk_batch_complete_io(struct ublk_thread *t, struct ublk_queue *q,
elem = (struct ublk_batch_elem *)(cb->elem + cb->done * t->commit_buf_elem_size);
elem->tag = tag;
elem->buf_index = ublk_batch_io_buf_idx(t, q, tag);
elem->buf_index = ublk_batch_io_buf_idx_next(t, q, tag);
elem->result = res;
if (!ublk_queue_no_buf(q))

View File

@@ -543,9 +543,14 @@ static int ublk_thread_init(struct ublk_thread *t, unsigned long long extra_flag
unsigned max_nr_ios_per_thread = nr_ios / dev->nthreads;
max_nr_ios_per_thread += !!(nr_ios % dev->nthreads);
t->auto_buf_stride = max_nr_ios_per_thread;
t->nr_bufs = max_nr_ios_per_thread;
if ((extra_flags & UBLKS_Q_ROTATE_AUTO_BUF) &&
(dev->dev_info.flags & UBLK_F_AUTO_BUF_REG))
t->nr_bufs *= 2;
} else {
t->nr_bufs = 0;
t->auto_buf_stride = 0;
}
if (ublk_dev_batch_io(dev))
@@ -1439,6 +1444,8 @@ static int ublk_start_daemon(const struct dev_ctx *ctx, struct ublk_dev *dev)
extra_flags = UBLKS_Q_AUTO_BUF_REG_FALLBACK;
if (ctx->no_ublk_fixed_fd)
extra_flags |= UBLKS_Q_NO_UBLK_FIXED_FD;
if (ctx->rotate_auto_buf)
extra_flags |= UBLKS_Q_ROTATE_AUTO_BUF;
for (i = 0; i < dinfo->nr_hw_queues; i++) {
dev->q[i].dev = dev;
@@ -2072,7 +2079,7 @@ static void __cmd_create_help(char *exe, bool recovery)
printf("\t[--nthreads threads] [--per_io_tasks]\n");
printf("\t[--integrity_capable] [--integrity_reftag] [--metadata_size SIZE] "
"[--pi_offset OFFSET] [--csum_type ip|t10dif|nvme] [--tag_size SIZE]\n");
printf("\t[--batch|-b] [--no_auto_part_scan]\n");
printf("\t[--batch|-b] [--rotate_auto_buf] [--no_auto_part_scan]\n");
printf("\t[--io_desc_size SIZE]\n");
printf("\t[target options] [backfile1] [backfile2] ...\n");
printf("\tdefault: nr_queues=2(max 32), depth=128(max 1024), dev_id=-1(auto allocation)\n");
@@ -2147,6 +2154,7 @@ int main(int argc, char *argv[])
{ "tag_size", 1, NULL, 0 },
{ "safe", 0, NULL, 0 },
{ "batch", 0, NULL, 'b'},
{ "rotate_auto_buf", 0, NULL, 0 },
{ "no_auto_part_scan", 0, NULL, 0 },
{ "shmem_zc", 0, NULL, 0 },
{ "htlb", 1, NULL, 0 },
@@ -2236,6 +2244,8 @@ int main(int argc, char *argv[])
ctx.flags |= UBLK_F_AUTO_BUF_REG;
if (!strcmp(longopts[option_idx].name, "auto_zc_fallback"))
ctx.auto_zc_fallback = 1;
if (!strcmp(longopts[option_idx].name, "rotate_auto_buf"))
ctx.rotate_auto_buf = 1;
if (!strcmp(longopts[option_idx].name, "nthreads"))
ctx.nthreads = strtol(optarg, NULL, 10);
if (!strcmp(longopts[option_idx].name, "per_io_tasks"))
@@ -2347,6 +2357,13 @@ int main(int argc, char *argv[])
return -EINVAL;
}
if (ctx.rotate_auto_buf &&
!((ctx.flags & UBLK_F_AUTO_BUF_REG) &&
(ctx.flags & UBLK_F_BATCH_IO))) {
ublk_err("rotate_auto_buf requires --auto_zc and --batch\n");
return -EINVAL;
}
i = optind;
while (i < argc && ctx.nr_files < MAX_BACK_FILES) {
ctx.files[ctx.nr_files++] = argv[i++];

View File

@@ -82,6 +82,7 @@ struct dev_ctx {
unsigned int safe_stop:1;
unsigned int no_auto_part_scan:1;
unsigned int rdonly_shmem_buf:1;
unsigned int rotate_auto_buf:1;
__u32 integrity_flags;
__u8 metadata_size;
__u8 pi_offset;
@@ -135,6 +136,7 @@ struct ublk_io {
unsigned short buf_index;
unsigned short tgt_ios;
unsigned char auto_buf_phase;
void *private_data;
};
@@ -185,6 +187,7 @@ struct ublk_queue {
#define UBLKS_Q_AUTO_BUF_REG_FALLBACK (1ULL << 63)
#define UBLKS_Q_NO_UBLK_FIXED_FD (1ULL << 62)
#define UBLKS_Q_PREPARED (1ULL << 61)
#define UBLKS_Q_ROTATE_AUTO_BUF (1ULL << 60)
__u64 flags;
int ublk_fd; /* cached ublk char device fd */
__u8 metadata_size;
@@ -234,6 +237,7 @@ struct ublk_thread {
unsigned int io_inflight;
unsigned short nr_bufs;
unsigned short auto_buf_stride;
/* followings are for BATCH_IO */
unsigned short commit_buf_start;
@@ -552,7 +556,20 @@ static inline unsigned short ublk_batch_io_buf_idx(
const struct ublk_thread *t, const struct ublk_queue *q,
unsigned tag)
{
return ublk_queue_idx_in_thread(t, q) * q->q_depth + tag;
unsigned short base = ublk_queue_idx_in_thread(t, q) * q->q_depth + tag;
if (q->flags & UBLKS_Q_ROTATE_AUTO_BUF)
return base + q->ios[tag].auto_buf_phase * t->auto_buf_stride;
return base;
}
static inline unsigned short ublk_batch_io_buf_idx_next(
const struct ublk_thread *t, struct ublk_queue *q,
unsigned tag)
{
if (q->flags & UBLKS_Q_ROTATE_AUTO_BUF)
q->ios[tag].auto_buf_phase ^= 1;
return ublk_batch_io_buf_idx(t, q, tag);
}
/* Queue UBLK_U_IO_PREP_IO_CMDS for a specific queue with batch elements */

View File

@@ -0,0 +1,44 @@
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
#
# --rotate_auto_buf: COMMIT must unregister old auto_buf index before store.
. "$(cd "$(dirname "$0")" && pwd)"/test_common.sh
ERR_CODE=0
if ! _have_feature "BATCH_IO" || ! _have_feature "AUTO_BUF_REG"; then
exit "$UBLK_SKIP_CODE"
fi
if ! _have_program fio || ! _have_program timeout; then
exit "$UBLK_SKIP_CODE"
fi
_prep_test "generic" "batch auto_buf unregister with rotating index"
_create_backfile 0 64M
dev_id=$(_add_ublk_dev_no_settle -t loop -q 1 --nthreads 1 -b --auto_zc \
--rotate_auto_buf "${UBLK_BACKFILES[0]}")
_check_add_dev $TID $?
for ((i = 0; i < 50; i++)); do
[ -b /dev/ublkb"${dev_id}" ] && break
sleep 0.1
done
[ -b /dev/ublkb"${dev_id}" ] || { _cleanup_test; _show_result $TID 1; }
timeout -k 2 5 fio --name=job1 --filename=/dev/ublkb"${dev_id}" \
--ioengine=libaio --rw=write --direct=1 --bs=4k --iodepth=1 --size=64k \
> /dev/null 2>&1
ERR_CODE=$?
if [ "$ERR_CODE" -ne 0 ]; then
kill -9 "$(_get_ublk_daemon_pid "$dev_id" 2>/dev/null)" 2>/dev/null || true
sleep 0.5
pkill -9 fio 2>/dev/null || true
ERR_CODE=1
fi
_cleanup_test
_show_result $TID $ERR_CODE