mirror of
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2025-09-18 22:14:16 +00:00
selftests: ublk: add kernel selftests for ublk
Both ublk driver and userspace heavily depends on io_uring subsystem, and tools/testing/selftests/ should be the best place for holding this cross-subsystem tests. Add basic read/write IO test over this ublk null disk, and make sure ublk working. More tests will be added. Signed-off-by: Ming Lei <ming.lei@redhat.com> Link: https://lore.kernel.org/r/20250228161919.2869102-2-ming.lei@redhat.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
This commit is contained in:
parent
ed9f3112a8
commit
6aecda00b7
10 changed files with 1466 additions and 0 deletions
|
@ -24237,6 +24237,7 @@ S: Maintained
|
|||
F: Documentation/block/ublk.rst
|
||||
F: drivers/block/ublk_drv.c
|
||||
F: include/uapi/linux/ublk_cmd.h
|
||||
F: tools/testing/selftests/ublk/
|
||||
|
||||
UBSAN
|
||||
M: Kees Cook <kees@kernel.org>
|
||||
|
|
|
@ -113,6 +113,7 @@ endif
|
|||
TARGETS += tmpfs
|
||||
TARGETS += tpm2
|
||||
TARGETS += tty
|
||||
TARGETS += ublk
|
||||
TARGETS += uevent
|
||||
TARGETS += user_events
|
||||
TARGETS += vDSO
|
||||
|
|
3
tools/testing/selftests/ublk/.gitignore
vendored
Normal file
3
tools/testing/selftests/ublk/.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
kublk
|
||||
/tools
|
||||
*-verify.state
|
12
tools/testing/selftests/ublk/Makefile
Normal file
12
tools/testing/selftests/ublk/Makefile
Normal file
|
@ -0,0 +1,12 @@
|
|||
# SPDX-License-Identifier: GPL-2.0
|
||||
|
||||
CFLAGS += -O3 -Wl,-no-as-needed -Wall -I $(top_srcdir)
|
||||
LDLIBS += -lpthread -lm -luring
|
||||
|
||||
TEST_PROGS := test_null_01.sh
|
||||
|
||||
TEST_GEN_PROGS_EXTENDED = kublk
|
||||
|
||||
include ../lib.mk
|
||||
|
||||
$(TEST_GEN_PROGS_EXTENDED): kublk.c null.c
|
1
tools/testing/selftests/ublk/config
Normal file
1
tools/testing/selftests/ublk/config
Normal file
|
@ -0,0 +1 @@
|
|||
CONFIG_BLK_DEV_UBLK=m
|
1081
tools/testing/selftests/ublk/kublk.c
Normal file
1081
tools/testing/selftests/ublk/kublk.c
Normal file
File diff suppressed because it is too large
Load diff
252
tools/testing/selftests/ublk/kublk.h
Normal file
252
tools/testing/selftests/ublk/kublk.h
Normal file
|
@ -0,0 +1,252 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0 */
|
||||
#ifndef KUBLK_INTERNAL_H
|
||||
#define KUBLK_INTERNAL_H
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
#include <pthread.h>
|
||||
#include <getopt.h>
|
||||
#include <limits.h>
|
||||
#include <poll.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/inotify.h>
|
||||
#include <sys/wait.h>
|
||||
#include <sys/eventfd.h>
|
||||
#include <liburing.h>
|
||||
#include <linux/ublk_cmd.h>
|
||||
|
||||
#define __maybe_unused __attribute__((unused))
|
||||
#define MAX_BACK_FILES 4
|
||||
#ifndef min
|
||||
#define min(a, b) ((a) < (b) ? (a) : (b))
|
||||
#endif
|
||||
|
||||
/****************** part 1: libublk ********************/
|
||||
|
||||
#define CTRL_DEV "/dev/ublk-control"
|
||||
#define UBLKC_DEV "/dev/ublkc"
|
||||
#define UBLKB_DEV "/dev/ublkb"
|
||||
#define UBLK_CTRL_RING_DEPTH 32
|
||||
#define ERROR_EVTFD_DEVID -2
|
||||
|
||||
/* queue idle timeout */
|
||||
#define UBLKSRV_IO_IDLE_SECS 20
|
||||
|
||||
#define UBLK_IO_MAX_BYTES 65536
|
||||
#define UBLK_MAX_QUEUES 4
|
||||
#define UBLK_QUEUE_DEPTH 128
|
||||
|
||||
#define UBLK_DBG_DEV (1U << 0)
|
||||
#define UBLK_DBG_QUEUE (1U << 1)
|
||||
#define UBLK_DBG_IO_CMD (1U << 2)
|
||||
#define UBLK_DBG_IO (1U << 3)
|
||||
#define UBLK_DBG_CTRL_CMD (1U << 4)
|
||||
#define UBLK_LOG (1U << 5)
|
||||
|
||||
struct ublk_dev;
|
||||
struct ublk_queue;
|
||||
|
||||
struct dev_ctx {
|
||||
char tgt_type[16];
|
||||
unsigned long flags;
|
||||
unsigned nr_hw_queues;
|
||||
unsigned queue_depth;
|
||||
int dev_id;
|
||||
int nr_files;
|
||||
char *files[MAX_BACK_FILES];
|
||||
unsigned int logging:1;
|
||||
unsigned int all:1;
|
||||
|
||||
int _evtfd;
|
||||
};
|
||||
|
||||
struct ublk_ctrl_cmd_data {
|
||||
__u32 cmd_op;
|
||||
#define CTRL_CMD_HAS_DATA 1
|
||||
#define CTRL_CMD_HAS_BUF 2
|
||||
__u32 flags;
|
||||
|
||||
__u64 data[2];
|
||||
__u64 addr;
|
||||
__u32 len;
|
||||
};
|
||||
|
||||
struct ublk_io {
|
||||
char *buf_addr;
|
||||
|
||||
#define UBLKSRV_NEED_FETCH_RQ (1UL << 0)
|
||||
#define UBLKSRV_NEED_COMMIT_RQ_COMP (1UL << 1)
|
||||
#define UBLKSRV_IO_FREE (1UL << 2)
|
||||
unsigned short flags;
|
||||
unsigned short refs; /* used by target code only */
|
||||
|
||||
int result;
|
||||
};
|
||||
|
||||
struct ublk_tgt_ops {
|
||||
const char *name;
|
||||
int (*init_tgt)(struct ublk_dev *);
|
||||
void (*deinit_tgt)(struct ublk_dev *);
|
||||
|
||||
int (*queue_io)(struct ublk_queue *, int tag);
|
||||
void (*tgt_io_done)(struct ublk_queue *,
|
||||
int tag, const struct io_uring_cqe *);
|
||||
};
|
||||
|
||||
struct ublk_tgt {
|
||||
unsigned long dev_size;
|
||||
unsigned int sq_depth;
|
||||
unsigned int cq_depth;
|
||||
const struct ublk_tgt_ops *ops;
|
||||
struct ublk_params params;
|
||||
char backing_file[1024 - 8 - sizeof(struct ublk_params)];
|
||||
};
|
||||
|
||||
struct ublk_queue {
|
||||
int q_id;
|
||||
int q_depth;
|
||||
unsigned int cmd_inflight;
|
||||
unsigned int io_inflight;
|
||||
struct ublk_dev *dev;
|
||||
const struct ublk_tgt_ops *tgt_ops;
|
||||
char *io_cmd_buf;
|
||||
struct io_uring ring;
|
||||
struct ublk_io ios[UBLK_QUEUE_DEPTH];
|
||||
#define UBLKSRV_QUEUE_STOPPING (1U << 0)
|
||||
#define UBLKSRV_QUEUE_IDLE (1U << 1)
|
||||
#define UBLKSRV_NO_BUF (1U << 2)
|
||||
unsigned state;
|
||||
pid_t tid;
|
||||
pthread_t thread;
|
||||
};
|
||||
|
||||
struct ublk_dev {
|
||||
struct ublk_tgt tgt;
|
||||
struct ublksrv_ctrl_dev_info dev_info;
|
||||
struct ublk_queue q[UBLK_MAX_QUEUES];
|
||||
|
||||
int fds[2]; /* fds[0] points to /dev/ublkcN */
|
||||
int nr_fds;
|
||||
int ctrl_fd;
|
||||
struct io_uring ring;
|
||||
};
|
||||
|
||||
#ifndef offsetof
|
||||
#define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER)
|
||||
#endif
|
||||
|
||||
#ifndef container_of
|
||||
#define container_of(ptr, type, member) ({ \
|
||||
unsigned long __mptr = (unsigned long)(ptr); \
|
||||
((type *)(__mptr - offsetof(type, member))); })
|
||||
#endif
|
||||
|
||||
#define round_up(val, rnd) \
|
||||
(((val) + ((rnd) - 1)) & ~((rnd) - 1))
|
||||
|
||||
|
||||
extern unsigned int ublk_dbg_mask;
|
||||
extern int ublk_queue_io_cmd(struct ublk_queue *q, struct ublk_io *io, unsigned tag);
|
||||
|
||||
static inline int is_target_io(__u64 user_data)
|
||||
{
|
||||
return (user_data & (1ULL << 63)) != 0;
|
||||
}
|
||||
|
||||
static inline __u64 build_user_data(unsigned tag, unsigned op,
|
||||
unsigned tgt_data, unsigned is_target_io)
|
||||
{
|
||||
assert(!(tag >> 16) && !(op >> 8) && !(tgt_data >> 16));
|
||||
|
||||
return tag | (op << 16) | (tgt_data << 24) | (__u64)is_target_io << 63;
|
||||
}
|
||||
|
||||
static inline unsigned int user_data_to_tag(__u64 user_data)
|
||||
{
|
||||
return user_data & 0xffff;
|
||||
}
|
||||
|
||||
static inline unsigned int user_data_to_op(__u64 user_data)
|
||||
{
|
||||
return (user_data >> 16) & 0xff;
|
||||
}
|
||||
|
||||
static inline void ublk_err(const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
vfprintf(stderr, fmt, ap);
|
||||
}
|
||||
|
||||
static inline void ublk_log(const char *fmt, ...)
|
||||
{
|
||||
if (ublk_dbg_mask & UBLK_LOG) {
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
vfprintf(stdout, fmt, ap);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void ublk_dbg(int level, const char *fmt, ...)
|
||||
{
|
||||
if (level & ublk_dbg_mask) {
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, fmt);
|
||||
vfprintf(stdout, fmt, ap);
|
||||
}
|
||||
}
|
||||
|
||||
static inline struct io_uring_sqe *ublk_queue_alloc_sqe(struct ublk_queue *q)
|
||||
{
|
||||
unsigned left = io_uring_sq_space_left(&q->ring);
|
||||
|
||||
if (left < 1)
|
||||
io_uring_submit(&q->ring);
|
||||
return io_uring_get_sqe(&q->ring);
|
||||
}
|
||||
|
||||
static inline void *ublk_get_sqe_cmd(const struct io_uring_sqe *sqe)
|
||||
{
|
||||
return (void *)&sqe->cmd;
|
||||
}
|
||||
|
||||
static inline void ublk_mark_io_done(struct ublk_io *io, int res)
|
||||
{
|
||||
io->flags |= (UBLKSRV_NEED_COMMIT_RQ_COMP | UBLKSRV_IO_FREE);
|
||||
io->result = res;
|
||||
}
|
||||
|
||||
static inline const struct ublksrv_io_desc *ublk_get_iod(const struct ublk_queue *q, int tag)
|
||||
{
|
||||
return (struct ublksrv_io_desc *)&(q->io_cmd_buf[tag * sizeof(struct ublksrv_io_desc)]);
|
||||
}
|
||||
|
||||
static inline void ublk_set_sqe_cmd_op(struct io_uring_sqe *sqe, __u32 cmd_op)
|
||||
{
|
||||
__u32 *addr = (__u32 *)&sqe->off;
|
||||
|
||||
addr[0] = cmd_op;
|
||||
addr[1] = 0;
|
||||
}
|
||||
|
||||
static inline int ublk_complete_io(struct ublk_queue *q, unsigned tag, int res)
|
||||
{
|
||||
struct ublk_io *io = &q->ios[tag];
|
||||
|
||||
ublk_mark_io_done(io, res);
|
||||
|
||||
return ublk_queue_io_cmd(q, io, tag);
|
||||
}
|
||||
|
||||
extern const struct ublk_tgt_ops null_tgt_ops;
|
||||
|
||||
#endif
|
38
tools/testing/selftests/ublk/null.c
Normal file
38
tools/testing/selftests/ublk/null.c
Normal file
|
@ -0,0 +1,38 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0 */
|
||||
|
||||
#include "kublk.h"
|
||||
|
||||
static int ublk_null_tgt_init(struct ublk_dev *dev)
|
||||
{
|
||||
const struct ublksrv_ctrl_dev_info *info = &dev->dev_info;
|
||||
unsigned long dev_size = 250UL << 30;
|
||||
|
||||
dev->tgt.dev_size = dev_size;
|
||||
dev->tgt.params = (struct ublk_params) {
|
||||
.types = UBLK_PARAM_TYPE_BASIC,
|
||||
.basic = {
|
||||
.logical_bs_shift = 9,
|
||||
.physical_bs_shift = 12,
|
||||
.io_opt_shift = 12,
|
||||
.io_min_shift = 9,
|
||||
.max_sectors = info->max_io_buf_bytes >> 9,
|
||||
.dev_sectors = dev_size >> 9,
|
||||
},
|
||||
};
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int ublk_null_queue_io(struct ublk_queue *q, int tag)
|
||||
{
|
||||
const struct ublksrv_io_desc *iod = ublk_get_iod(q, tag);
|
||||
|
||||
ublk_complete_io(q, tag, iod->nr_sectors << 9);
|
||||
return 0;
|
||||
}
|
||||
|
||||
const struct ublk_tgt_ops null_tgt_ops = {
|
||||
.name = "null",
|
||||
.init_tgt = ublk_null_tgt_init,
|
||||
.queue_io = ublk_null_queue_io,
|
||||
};
|
58
tools/testing/selftests/ublk/test_common.sh
Executable file
58
tools/testing/selftests/ublk/test_common.sh
Executable file
|
@ -0,0 +1,58 @@
|
|||
#!/bin/bash
|
||||
# SPDX-License-Identifier: GPL-2.0
|
||||
|
||||
_check_root() {
|
||||
local ksft_skip=4
|
||||
|
||||
if [ $UID != 0 ]; then
|
||||
echo please run this as root >&2
|
||||
exit $ksft_skip
|
||||
fi
|
||||
}
|
||||
|
||||
_remove_ublk_devices() {
|
||||
${UBLK_PROG} del -a
|
||||
}
|
||||
|
||||
_get_ublk_dev_state() {
|
||||
${UBLK_PROG} list -n "$1" | grep "state" | awk '{print $11}'
|
||||
}
|
||||
|
||||
_get_ublk_daemon_pid() {
|
||||
${UBLK_PROG} list -n "$1" | grep "pid" | awk '{print $7}'
|
||||
}
|
||||
|
||||
_prep_test() {
|
||||
_check_root
|
||||
local type=$1
|
||||
shift 1
|
||||
echo "ublk $type: $@"
|
||||
}
|
||||
|
||||
_show_result()
|
||||
{
|
||||
if [ $2 -ne 0 ]; then
|
||||
echo "$1 : [FAIL]"
|
||||
else
|
||||
echo "$1 : [PASS]"
|
||||
fi
|
||||
}
|
||||
|
||||
_cleanup_test() {
|
||||
${UBLK_PROG} del -n $1
|
||||
}
|
||||
|
||||
_add_ublk_dev() {
|
||||
local kublk_temp=`mktemp /tmp/kublk-XXXXXX`
|
||||
${UBLK_PROG} add $@ > ${kublk_temp} 2>&1
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "fail to add ublk dev $@"
|
||||
exit -1
|
||||
fi
|
||||
local dev_id=`grep "dev id" ${kublk_temp} | awk -F '[ :]' '{print $3}'`
|
||||
udevadm settle
|
||||
rm -f ${kublk_temp}
|
||||
echo ${dev_id}
|
||||
}
|
||||
|
||||
export UBLK_PROG=$(pwd)/kublk
|
19
tools/testing/selftests/ublk/test_null_01.sh
Executable file
19
tools/testing/selftests/ublk/test_null_01.sh
Executable file
|
@ -0,0 +1,19 @@
|
|||
#!/bin/bash
|
||||
# SPDX-License-Identifier: GPL-2.0
|
||||
|
||||
. test_common.sh
|
||||
|
||||
TID="null_01"
|
||||
ERR_CODE=0
|
||||
|
||||
_prep_test "null" "basic IO test"
|
||||
|
||||
dev_id=`_add_ublk_dev -t null`
|
||||
|
||||
# run fio over the two disks
|
||||
fio --name=job1 --filename=/dev/ublkb${dev_id} --ioengine=libaio --rw=readwrite --iodepth=32 --size=256M > /dev/null 2>&1
|
||||
ERR_CODE=$?
|
||||
|
||||
_cleanup_test ${dev_id} "null"
|
||||
|
||||
_show_result $TID $ERR_CODE
|
Loading…
Add table
Reference in a new issue