block/export: vhost-user-blk server tests and input validation
Subsystems (2)
v3 cover block/export: vhost-user-blk server tests and input validation
v3:
* Rebased onto qemu.git/master (7ef8134565dc)
* I asked Coiby Xu for clarification on a comment about vhost-user-blk-test in v2. Please wait for that discussion to finish before merging.
v2:
* Add abrt handler that terminates qemu-storage-daemon to vhost-user-blk-test. No more orphaned processes on test failure. [Peter]
* Fix sector number calculation in vhost-user-blk-server.c
* Introduce VIRTIO_BLK_SECTOR_BITS/SIZE to make code clearer [Max]
* Fix vhost-user-blk-server.c blk_size double byteswap
* Fix vhost-user-blk blkcfg->num_queues endianness [Peter]
* Squashed cleanups into Coiby vhost-user-blk-test commit so the code is easier to review
The vhost-user-blk server test was already in Michael Tsirkin's recent vhost pull request, but was dropped because it exposed vhost-user regressions (b7c1bd9d7848 and the Based-on tag below). Now that the vhost-user regressions are fixed we can re-introduce the test case.
This series adds missing input validation that led to a Coverity report. The virtio-blk read, write, discard, and write zeroes commands need to check sector/byte ranges and other inputs. This solves the issue Peter Maydell raised in "[PATCH for-5.2] block/export/vhost-user-blk-server.c: Avoid potential integer overflow".
Merging just the input validation patches would be possible too, but I prefer to merge the corresponding tests so the code is exercised by the CI.
Coiby Xu (1):
test: new qTest case to test the vhost-user-blk-server
Stefan Hajnoczi (11):
vhost-user-blk: fix blkcfg->num_queues endianness
libqtest: add qtest_socket_server()
libqtest: add qtest_kill_qemu()
libqtest: add qtest_remove_abrt_handler()
tests/qtest: add multi-queue test case to vhost-user-blk-test block/export: fix blk_size double byteswap
block/export: use VIRTIO_BLK_SECTOR_BITS
block/export: fix vhost-user-blk export sector number calculation block/export: port virtio-blk discard/write zeroes input validation
vhost-user-blk-test: test discard/write zeroes invalid inputs block/export: port virtio-blk read/write range check
MAINTAINERS | 2 + tests/qtest/libqos/libqtest.h | 37 + tests/qtest/libqos/vhost-user-blk.h | 48 ++ block/export/vhost-user-blk-server.c | 150 +++- hw/block/vhost-user-blk.c | 7 +- tests/qtest/libqos/vhost-user-blk.c | 130 ++++ tests/qtest/libqtest.c | 82 ++- tests/qtest/vhost-user-blk-test.c | 983 +++++++++++++++++++++++++++ tests/qtest/libqos/meson.build | 1 + tests/qtest/meson.build | 4 + 10 files changed, 1385 insertions(+), 59 deletions(-) create mode 100644 tests/qtest/libqos/vhost-user-blk.h create mode 100644 tests/qtest/libqos/vhost-user-blk.c create mode 100644 tests/qtest/vhost-user-blk-test.c
Signature · 1 line
2.29.2
PATCH v3 1/12 vhost-user-blk: fix blkcfg->num_queues endianness
Treat the num_queues field as virtio-endian. On big-endian hosts the vhost-user-blk num_queues field was in the wrong endianness.
Move the blkcfg.num_queues store operation from realize to vhost_user_blk_update_config() so feature negotiation has finished and we know the endianness of the device. VIRTIO 1.0 devices are little-endian, but in case someone wants to use legacy VIRTIO we support all endianness cases.
--- hw/block/vhost-user-blk.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
hw/block/vhost-user-blk.c +3 -4
@@ -54,6 +54,9 @@ static void vhost_user_blk_update_config(VirtIODevice *vdev, uint8_t *config) {VHostUserBlk *s =VHOST_USER_BLK (vdev); +/* Our num_queues overrides the device backend */ +virtio_stw_p (vdev, &s->blkcfg.num_queues, s->num_queues); +memcpy (config, &s->blkcfg,sizeof (struct virtio_blk_config )); }
@@ -491,10 +494,6 @@ reconnect: goto reconnect; } -if (s->blkcfg.num_queues != s->num_queues) { - s->blkcfg.num_queues = s->num_queues; - } -return ; virtio_err:
Signature · 1 line
2.29.2
Re: [PATCH v3 01/12] vhost-user-blk: fix blkcfg->num_queues endianness
Quoted 9 lines · Stefan Hajnoczi
Treat the
num_queuesfield as virtio-endian. On big-endian hosts the vhost-user-blknum_queuesfield was in the wrong endianness.Move the blkcfg.num_queues store operation from realize to
vhost_user_blk_update_config()so feature negotiation has finished and we know the endianness of the device. VIRTIO 1.0 devices are little-endian, but in case someone wants to use legacy VIRTIO we support all endianness cases.
Quoted 4 lines
Signed-off-by Stefan Hajnoczi <[email protected]>Reviewed-by Raphael Norwitz <[email protected]>Reviewed-by Michael S. Tsirkin <[email protected]>
okay but as we recently discovered config space can in theory be read before FEATURES_OK. Nasty, I know. Things kind of work right now but we really need some other path to notify backends when legacy guest is active. E.g. VDPA also has this problem.
Quoted 32 lines
--- hw/block/vhost-user-blk.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)hw/block/vhost-user-blk.c +3
-4@@ -54,6 +54,9 @@ static void vhost_user_blk_update_config(VirtIODevice *vdev, uint8_t *config) {VHostUserBlk *s =VHOST_USER_BLK (vdev); +/* Our num_queues overrides the device backend */ +virtio_stw_p (vdev, &s->blkcfg.num_queues, s->num_queues); +memcpy (config, &s->blkcfg,sizeof (struct virtio_blk_config )); }@@ -491,10 +494,6 @@ reconnect: goto reconnect; } -if (s->blkcfg.num_queues != s->num_queues) { - s->blkcfg.num_queues = s->num_queues; - } -return ; virtio_err:Signature · 1 line
2.29.2
Re: [PATCH v3 01/12] vhost-user-blk: fix blkcfg->num_queues endianness
Quoted 15 lines · Michael S. Tsirkin
Quoted 9 lines · Stefan Hajnoczi
Treat the
num_queuesfield as virtio-endian. On big-endian hosts the vhost-user-blknum_queuesfield was in the wrong endianness.Move the blkcfg.num_queues store operation from realize to
vhost_user_blk_update_config()so feature negotiation has finished and we know the endianness of the device. VIRTIO 1.0 devices are little-endian, but in case someone wants to use legacy VIRTIO we support all endianness cases.Quoted 4 lines
Signed-off-by Stefan Hajnoczi <[email protected]>Reviewed-by Raphael Norwitz <[email protected]>Reviewed-by Michael S. Tsirkin <[email protected]>
Quoted 4 lines
okay but as we recently discovered config space can in theory be read before
FEATURES_OK. Nasty, I know. Things kind of work right now but we really need some other path to notify backends when legacy guest is active. E.g. VDPA also has this problem.
Thanks for mentioning it.
Do you know specifics about this type of guest driver? If it's only legacy drivers then device implementations can ensure compatibility by using host-endian until FEATURES_OK :). I guess existing code needs to be audited to check if this can be done.
Stefan
PATCH v3 2/12 libqtest: add qtest_socket_server()
In reply to Stefan HajnocziAdd an API that returns a new UNIX domain socket in the listen state. The code for this was already there but only used internally in init_socket().
This new API will be used by vhost-user-blk-test.
--- tests/qtest/libqos/libqtest.h | 8 +++++++ tests/qtest/libqtest.c | 40 ++++++++++++++++++++--------------- 2 files changed, 31 insertions(+), 17 deletions(-)
tests/qtest/libqos/libqtest.h +8 -0
@@ -132,6 +132,14 @@ void qtest_qmp_send(QTestState *s, const char *fmt, ...) void qtest_qmp_send_raw(QTestState *s, const char *fmt, ...) GCC_FMT_ATTR(2, 3); +/** +* qtest_socket_server: +* @socket_path: the UNIX domain socket path +* +* Create and return a listen socket file descriptor, or abort on failure. +*/ +int qtest_socket_server (const char *socket_path); +/** * qtest_vqmp_fds: * @s: #QTestState instance to operate on.
tests/qtest/libqtest.c +23 -17
@@ -81,24 +81,8 @@ static void qtest_client_set_rx_handler(QTestState *s, QTestRecvFn recv);static int init_socket (const char *socket_path) { -struct sockaddr_un addr; -int sock; -int ret; - - sock =socket (PF_UNIX ,SOCK_STREAM ,0 ); - g_assert_cmpint(sock, !=, -1); - - addr.sun_family =AF_UNIX ; -snprintf (addr.sun_path,sizeof (addr.sun_path),"%s" , socket_path); +int sock =qtest_socket_server (socket_path);qemu_set_cloexec (sock); - -do { - ret =bind (sock, (struct sockaddr *)&addr,sizeof (addr)); - }while (ret ==-1 && errno ==EINTR ); - g_assert_cmpint(ret, !=, -1); - ret =listen (sock,1 ); - g_assert_cmpint(ret, !=, -1); -return sock; }
@@ -638,6 +622,28 @@ QDict *qtest_qmp_receive_dict(QTestState *s)return qmp_fd_receive (s->qmp_fd); } +int qtest_socket_server (const char *socket_path) +{ +struct sockaddr_un addr; +int sock; +int ret; + + sock =socket (PF_UNIX ,SOCK_STREAM ,0 ); + g_assert_cmpint(sock, !=, -1); + + addr.sun_family =AF_UNIX ; +snprintf (addr.sun_path,sizeof (addr.sun_path),"%s" , socket_path); + +do { + ret =bind (sock, (struct sockaddr *)&addr,sizeof (addr)); + }while (ret ==-1 && errno ==EINTR ); + g_assert_cmpint(ret, !=, -1); + ret =listen (sock,1 ); + g_assert_cmpint(ret, !=, -1); + +return sock; +} +/** * Allow users to send a message without waiting for the reply, * in the case that they choose to discard all replies up until
Signature · 1 line
2.29.2
PATCH v3 3/12 libqtest: add qtest_kill_qemu()
In reply to Stefan HajnocziTests that manage multiple processes may wish to kill QEMU before destroying the QTestState. Expose a function to do that.
The vhost-user-blk-test testcase will need this.
--- tests/qtest/libqos/libqtest.h | 11 +++++++++++ tests/qtest/libqtest.c | 7 ++++--- 2 files changed, 15 insertions(+), 3 deletions(-)
tests/qtest/libqos/libqtest.h +11 -0
@@ -74,6 +74,17 @@ QTestState *qtest_init_without_qmp_handshake(const char *extra_args);*/ QTestState *qtest_init_with_serial (const char *extra_args,int *sock_fd); +/** +* qtest_kill_qemu: +* @s: #QTestState instance to operate on. +* +* Kill the QEMU process and wait for it to terminate. It is safe to call this +* function multiple times. Normally qtest_quit() is used instead because it +* also frees QTestState. Use qtest_kill_qemu() when you just want to kill QEMU +* and qtest_quit() will be called later. +*/ +void qtest_kill_qemu (QTestState *s); +/** * qtest_quit: * @s: #QTestState instance to operate on.
tests/qtest/libqtest.c +4 -3
@@ -133,7 +133,7 @@ void qtest_set_expected_status(QTestState *s, int status) s->expected_status = status; } -static void kill_qemu (QTestState *s) +void qtest_kill_qemu (QTestState *s) {pid_t pid = s->qemu_pid;int wstatus;
@@ -143,6 +143,7 @@ static void kill_qemu(QTestState *s)kill (pid,SIGTERM );TFR (pid =waitpid (s->qemu_pid, &s->wstatus,0 ));assert (pid == s->qemu_pid); + s->qemu_pid =-1 ; }/*
@@ -169,7 +170,7 @@ static void kill_qemu(QTestState *s)static void kill_qemu_hook_func (void *s) { -kill_qemu (s); +qtest_kill_qemu (s); }static void sigabrt_handler (int signo)
@@ -373,7 +374,7 @@ void qtest_quit(QTestState *s)/* Uninstall SIGABRT handler on last instance */ cleanup_sigabrt_handler (); -kill_qemu (s); +qtest_kill_qemu (s);close (s->fd);close (s->qmp_fd);g_string_free (s->rx, true);
Signature · 1 line
2.29.2
Re: [PATCH v3 03/12] libqtest: add qtest_kill_qemu()
Quoted 11 lines · Stefan Hajnoczi
Tests that manage multiple processes may wish to kill QEMU before destroying the QTestState. Expose a function to do that.
The vhost-user-blk-test testcase will need this.
Signed-off-by Stefan Hajnoczi <[email protected]>Reviewed-by Wainer dos Santos Moschetta <[email protected]>--- tests/qtest/libqos/libqtest.h | 11 +++++++++++ tests/qtest/libqtest.c | 7 ++++--- 2 files changed, 15 insertions(+), 3 deletions(-)
PATCH v3 4/12 libqtest: add qtest_remove_abrt_handler()
In reply to Stefan HajnocziAdd a function to remove previously-added abrt handler functions.
Now that a symmetric pair of add/remove functions exists we can also balance the SIGABRT handler installation. The signal handler was installed each time qtest_add_abrt_handler() was called. Now it is installed when the abrt handler list becomes non-empty and removed again when the list becomes empty.
The qtest_remove_abrt_handler() function will be used by vhost-user-blk-test.
--- tests/qtest/libqos/libqtest.h | 18 ++++++++++++++++++ tests/qtest/libqtest.c | 35 +++++++++++++++++++++++++++++------ 2 files changed, 47 insertions(+), 6 deletions(-)
tests/qtest/libqos/libqtest.h +18 -0
@@ -649,8 +649,26 @@ void qtest_add_data_func_full(const char *str, void *data,g_free (path); \ }while (0 ) +/** +* qtest_add_abrt_handler: +* @fn: Handler function +* @data: Argument that is passed to the handler +* +* Add a handler function that is invoked on SIGABRT. This can be used to +* terminate processes and perform other cleanup. The handler can be removed +* with qtest_remove_abrt_handler(). +*/ void qtest_add_abrt_handler(GHookFunc fn, const void *data); +/** +* qtest_remove_abrt_handler: +* @data: Argument previously passed to qtest_add_abrt_handler() +* +* Remove an abrt handler that was previously added with +* qtest_add_abrt_handler(). +*/ +void qtest_remove_abrt_handler (void *data); +/** * qtest_qmp_assert_success: * @qts: QTestState instance to operate on
tests/qtest/libqtest.c +29 -6
@@ -196,15 +196,30 @@ static void cleanup_sigabrt_handler(void)sigaction (SIGABRT , &sigact_old,NULL ); } +static bool hook_list_is_empty (GHookList *hook_list) +{ +GHook *hook =g_hook_first_valid (hook_list, TRUE); + +if (!hook) { +return false; + } + +g_hook_unref (hook_list, hook); +return true; +} +void qtest_add_abrt_handler (GHookFunc fn,const void *data) {GHook *hook; -/* Only install SIGABRT handler once */ if (!abrt_hooks.is_setup) {g_hook_list_init (&abrt_hooks,sizeof (GHook)); } -setup_sigabrt_handler (); + +/* Only install SIGABRT handler once */ +if (hook_list_is_empty (&abrt_hooks)) { +setup_sigabrt_handler (); + } hook =g_hook_alloc (&abrt_hooks); hook->func = fn;
@@ -213,6 +228,17 @@ void qtest_add_abrt_handler(GHookFunc fn, const void *data)g_hook_prepend (&abrt_hooks, hook); } +void qtest_remove_abrt_handler (void *data) +{ +GHook *hook =g_hook_find_data (&abrt_hooks, TRUE, data); +g_hook_destroy_link (&abrt_hooks, hook); + +/* Uninstall SIGABRT handler on last instance */ +if (hook_list_is_empty (&abrt_hooks)) { +cleanup_sigabrt_handler (); + } +} +static const char *qtest_qemu_binary (void ) {const char *qemu_bin;
@@ -369,10 +395,7 @@ QTestState *qtest_init_with_serial(const char *extra_args, int *sock_fd)void qtest_quit (QTestState *s) { -g_hook_destroy_link (&abrt_hooks,g_hook_find_data (&abrt_hooks, TRUE, s)); - -/* Uninstall SIGABRT handler on last instance */ -cleanup_sigabrt_handler (); +qtest_remove_abrt_handler (s);qtest_kill_qemu (s);close (s->fd);
Signature · 1 line
2.29.2
Re: [PATCH v3 04/12] libqtest: add qtest_remove_abrt_handler()
Quoted 11 lines · Stefan Hajnoczi
Add a function to remove previously-added abrt handler functions.
Now that a symmetric pair of add/remove functions exists we can also balance the SIGABRT handler installation. The signal handler was installed each time
qtest_add_abrt_handler()was called. Now it is installed when the abrt handler list becomes non-empty and removed again when the list becomes empty.The
qtest_remove_abrt_handler()function will be used by vhost-user-blk-test.
Quoted 6 lines
Signed-off-by Stefan Hajnoczi <[email protected]>Reviewed-by Wainer dos Santos Moschetta <[email protected]>--- tests/qtest/libqos/libqtest.h | 18 ++++++++++++++++++ tests/qtest/libqtest.c | 35 +++++++++++++++++++++++++++++------ 2 files changed, 47 insertions(+), 6 deletions(-)
PATCH v3 5/12 test: new qTest case to test the vhost-user-blk-server
In reply to Stefan HajnocziFrom: Coiby Xu <[email protected]>
This test case has the same tests as tests/virtio-blk-test.c except for tests have block_resize. Since the vhost-user-blk export only serves one client one time, two exports are started by qemu-storage-daemon for the hotplug test.
--- MAINTAINERS | 2 + tests/qtest/libqos/vhost-user-blk.h | 48 ++ tests/qtest/libqos/vhost-user-blk.c | 130 +++++ tests/qtest/vhost-user-blk-test.c | 788 ++++++++++++++++++++++++++++ tests/qtest/libqos/meson.build | 1 + tests/qtest/meson.build | 4 + 6 files changed, 973 insertions(+) create mode 100644 tests/qtest/libqos/vhost-user-blk.h create mode 100644 tests/qtest/libqos/vhost-user-blk.c create mode 100644 tests/qtest/vhost-user-blk-test.c
MAINTAINERS +2 -0
@@ -3194,6 +3194,8 @@ F: block/export/vhost-user-blk-server.c F: block/export/vhost-user-blk-server.h F: include/qemu/vhost-user-server.h F: tests/qtest/libqos/vhost-user-blk.c +F: tests/qtest/libqos/vhost-user-blk.h +F: tests/qtest/vhost-user-blk-test.c F: util/vhost-user-server.c FUSE block device exports
tests/qtest/libqos/vhost-user-blk.h new file +48 -0
48 lines not on this page · raw message
tests/qtest/libqos/vhost-user-blk.c new file +130 -0
130 lines not on this page · raw message
tests/qtest/vhost-user-blk-test.c new file +788 -0
788 lines not on this page · raw message
tests/qtest/libqos/meson.build +1 -0
@@ -32,6 +32,7 @@ libqos_srcs = files('../libqtest.c', 'virtio-9p.c', 'virtio-balloon.c', 'virtio-blk.c', + 'vhost-user-blk.c', 'virtio-mmio.c', 'virtio-net.c', 'virtio-pci.c',
tests/qtest/meson.build +4 -0
@@ -210,6 +210,9 @@ if have_virtfs qos_test_ss.add(files('virtio-9p-test.c')) endif qos_test_ss.add(when: 'CONFIG_VHOST_USER', if_true: files('vhost-user-test.c')) +if have_vhost_user_blk_server + qos_test_ss.add(files('vhost-user-blk-test.c')) +endif tpmemu_files = ['tpm-emu.c', 'tpm-util.c', 'tpm-tests.c']
@@ -248,6 +251,7 @@ foreach dir : target_dirs endif qtest_env.set('G_TEST_DBUS_DAEMON', meson.source_root() / 'tests/dbus-vmstate-daemon.sh') qtest_env.set('QTEST_QEMU_BINARY', './qemu-system-' + target_base) + qtest_env.set('QTEST_QEMU_STORAGE_DAEMON_BINARY', './storage-daemon/qemu-storage-daemon') foreach test : target_qtests # Executables are shared across targets, declare them only the first time we
Signature · 1 line
2.29.2
PATCH v3 6/12 tests/qtest: add multi-queue test case to vhost-user-blk-test
In reply to Stefan Hajnoczi--- tests/qtest/vhost-user-blk-test.c | 81 +++++++++++++++++++++++++++++-- 1 file changed, 76 insertions(+), 5 deletions(-)
tests/qtest/vhost-user-blk-test.c +76 -5
@@ -563,6 +563,67 @@ static void pci_hotplug(void *obj, void *data, QGuestAllocator *t_alloc)qpci_unplug_acpi_device_test (qts,"drv1" ,PCI_SLOT_HP ); } +static void multiqueue (void *obj,void *data,QGuestAllocator *t_alloc) +{ +QVirtioPCIDevice *pdev1 = obj; +QVirtioDevice *dev1 = &pdev1->vdev; +QVirtioPCIDevice *pdev8; +QVirtioDevice *dev8; +QTestState *qts = pdev1->pdev->bus->qts; +uint64_t features; +uint16_t num_queues; + +/* +* The primary device has 1 queue and VIRTIO_BLK_F_MQ is not enabled. The +* VIRTIO specification allows VIRTIO_BLK_F_MQ to be enabled when there is +* only 1 virtqueue, but --device vhost-user-blk-pci doesn't do this (which +* is also spec-compliant). +*/ + features =qvirtio_get_features (dev1); + g_assert_cmpint(features & (1u << VIRTIO_BLK_F_MQ), ==, 0); + features = features & ~(QVIRTIO_F_BAD_FEATURE | + (1u <<VIRTIO_RING_F_INDIRECT_DESC ) | + (1u <<VIRTIO_F_NOTIFY_ON_EMPTY ) | + (1u <<VIRTIO_BLK_F_SCSI )); +qvirtio_set_features (dev1, features); + +/* Hotplug a secondary device with 8 queues */ +qtest_qmp_device_add (qts,"vhost-user-blk-pci" ,"drv1" , +"{'addr': %s, 'chardev': 'char2', 'num-queues': 8}" , + stringify(PCI_SLOT_HP) ".0"); + + pdev8 =virtio_pci_new (pdev1->pdev->bus, + &(QPCIAddress ) { + .devfn =QPCI_DEVFN (PCI_SLOT_HP ,0 ) + }); +g_assert_nonnull (pdev8); + g_assert_cmpint(pdev8->vdev.device_type, ==, VIRTIO_ID_BLOCK); + +qos_object_start_hw (&pdev8->obj); + + dev8 = &pdev8->vdev; + features =qvirtio_get_features (dev8); +g_assert_cmpint (features & (1u <<VIRTIO_BLK_F_MQ ), + ==, + (1u <<VIRTIO_BLK_F_MQ )); + features = features & ~(QVIRTIO_F_BAD_FEATURE | + (1u <<VIRTIO_RING_F_INDIRECT_DESC ) | + (1u <<VIRTIO_F_NOTIFY_ON_EMPTY ) | + (1u <<VIRTIO_BLK_F_SCSI ) | + (1u <<VIRTIO_BLK_F_MQ )); +qvirtio_set_features (dev8, features); + + num_queues =qvirtio_config_readw (dev8, + offsetof(struct virtio_blk_config , num_queues)); + g_assert_cmpint(num_queues, ==, 8); + +qvirtio_pci_device_disable (pdev8); +qos_object_destroy (&pdev8->obj); + +/* unplug secondary disk */ +qpci_unplug_acpi_device_test (qts,"drv1" ,PCI_SLOT_HP ); +} +/* * Check that setting the vring addr on a non-existent virtqueue does * not crash.
@@ -682,7 +743,8 @@ static void quit_storage_daemon(void *data)g_free (data); } -static void start_vhost_user_blk (GString *cmd_line,int vus_instances) +static void start_vhost_user_blk (GString *cmd_line,int vus_instances, +int num_queues) {const char *vhost_user_blk_bin =qtest_qemu_storage_daemon_binary ();int i;
@@ -707,8 +769,8 @@ static void start_vhost_user_blk(GString *cmd_line, int vus_instances)g_string_append_printf (storage_daemon_command,"--blockdev driver=file,node-name=disk%d,filename=%s " "--export type=vhost-user-blk,id=disk%d,addr.type=unix,addr.path=%s," -"node-name=disk%i,writable=on " , - i, img_path, i, sock_path, i); +"node-name=disk%i,writable=on,num-queues=%d " , + i, img_path, i, sock_path, i, num_queues);g_string_append_printf (cmd_line,"-chardev socket,id=char%d,path=%s " , i +1 , sock_path);
@@ -742,7 +804,7 @@ static void start_vhost_user_blk(GString *cmd_line, int vus_instances)static void *vhost_user_blk_test_setup (GString *cmd_line,void *arg) { -start_vhost_user_blk (cmd_line,1 ); +start_vhost_user_blk (cmd_line,1 ,1 );return arg; }
@@ -756,7 +818,13 @@ static void *vhost_user_blk_test_setup(GString *cmd_line, void *arg)static void *vhost_user_blk_hotplug_test_setup (GString *cmd_line,void *arg) {/* "-chardev socket,id=char2" is used for pci_hotplug*/ -start_vhost_user_blk (cmd_line,2 ); +start_vhost_user_blk (cmd_line,2 ,1 ); +return arg; +} + +static void *vhost_user_blk_multiqueue_test_setup (GString *cmd_line,void *arg) +{ +start_vhost_user_blk (cmd_line,2 ,8 );return arg; }
@@ -783,6 +851,9 @@ static void register_vhost_user_blk_test(void) opts.before = vhost_user_blk_hotplug_test_setup;qos_add_test ("hotplug" ,"vhost-user-blk-pci" , pci_hotplug, &opts); + + opts.before = vhost_user_blk_multiqueue_test_setup; +qos_add_test ("multiqueue" ,"vhost-user-blk-pci" , multiqueue, &opts); }libqos_init (register_vhost_user_blk_test);
Signature · 1 line
2.29.2
PATCH v3 7/12 block/export: fix blk_size double byteswap
In reply to Stefan HajnocziThe config->blk_size field is little-endian. Use the native-endian blk_size variable to avoid double byteswapping.
--- block/export/vhost-user-blk-server.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
block/export/vhost-user-blk-server.c +1 -1
@@ -356,7 +356,7 @@ vu_blk_initialize_config(BlockDriverState *bs, config->num_queues =cpu_to_le16 (num_queues); config->max_discard_sectors =cpu_to_le32 (32768 ); config->max_discard_seg =cpu_to_le32 (1 ); - config->discard_sector_alignment =cpu_to_le32 (config->blk_size >>9 ); + config->discard_sector_alignment =cpu_to_le32 (blk_size >>9 ); config->max_write_zeroes_sectors =cpu_to_le32 (32768 ); config->max_write_zeroes_seg =cpu_to_le32 (1 ); }
Signature · 1 line
2.29.2
PATCH v3 8/12 block/export: use VIRTIO_BLK_SECTOR_BITS
In reply to Stefan HajnocziUse VIRTIO_BLK_SECTOR_BITS and VIRTIO_BLK_SECTOR_SIZE when dealing with virtio-blk sector numbers. Although the values happen to be the same as BDRV_SECTOR_BITS and BDRV_SECTOR_SIZE, they are conceptually different. This makes it clearer when we are dealing with virtio-blk sector units.
Use VIRTIO_BLK_SECTOR_BITS in vu_blk_initialize_config(). Later patches will use it the new constants the virtqueue request processing code path.
--- block/export/vhost-user-blk-server.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)
block/export/vhost-user-blk-server.c +12 -3
@@ -20,6 +20,13 @@#include "sysemu/block-backend.h" #include "util/block-helpers.h" +/* +* Sector units are 512 bytes regardless of the +* virtio_blk_config->blk_size value. +*/ +#define VIRTIO_BLK_SECTOR_BITS 9 +#define VIRTIO_BLK_SECTOR_SIZE (1ull << VIRTIO_BLK_SECTOR_BITS) +enum {VHOST_USER_BLK_NUM_QUEUES_DEFAULT =1 , };
@@ -347,7 +354,8 @@ vu_blk_initialize_config(BlockDriverState *bs, uint32_t blk_size, uint16_t num_queues) { - config->capacity =cpu_to_le64 (bdrv_getlength (bs) >>BDRV_SECTOR_BITS ); + config->capacity = +cpu_to_le64 (bdrv_getlength (bs) >>VIRTIO_BLK_SECTOR_BITS ); config->blk_size =cpu_to_le32 (blk_size); config->size_max =cpu_to_le32 (0 ); config->seg_max =cpu_to_le32 (128 -2 );
@@ -356,7 +364,8 @@ vu_blk_initialize_config(BlockDriverState *bs, config->num_queues =cpu_to_le16 (num_queues); config->max_discard_sectors =cpu_to_le32 (32768 ); config->max_discard_seg =cpu_to_le32 (1 ); - config->discard_sector_alignment =cpu_to_le32 (blk_size >>9 ); + config->discard_sector_alignment = +cpu_to_le32 (blk_size >>VIRTIO_BLK_SECTOR_BITS ); config->max_write_zeroes_sectors =cpu_to_le32 (32768 ); config->max_write_zeroes_seg =cpu_to_le32 (1 ); }
@@ -383,7 +392,7 @@ static int vu_blk_exp_create(BlockExport *exp, BlockExportOptions *opts,if (vu_opts->has_logical_block_size) { logical_block_size = vu_opts->logical_block_size; }else { - logical_block_size =BDRV_SECTOR_SIZE ; + logical_block_size =VIRTIO_BLK_SECTOR_SIZE ; }check_block_size (exp->id,"logical-block-size" , logical_block_size, &local_err);
Signature · 1 line
2.29.2
PATCH v3 9/12 block/export: fix vhost-user-blk export sector number calculation
In reply to Stefan HajnocziThe driver is supposed to honor the blk_size field but the protocol still uses 512-byte sector numbers. It is incorrect to multiply req->sector_num by blk_size.
VIRTIO 1.1 5.2.5 Device Initialization says:
blk_size can be read to determine the optimal sector size for the driver to use. This does not affect the units used in the protocol (always 512 bytes), but awareness of the correct value can affect performance.
--- block/export/vhost-user-blk-server.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
block/export/vhost-user-blk-server.c +1 -1
@@ -144,7 +144,7 @@ static void coroutine_fn vu_blk_virtio_process_req(void *opaque)break ; } -int64_t offset = req->sector_num * vexp->blk_size; +int64_t offset = req->sector_num <<VIRTIO_BLK_SECTOR_BITS ;QEMUIOVector qiov;if (is_write) {qemu_iovec_init_external (&qiov, out_iov, out_num);
Signature · 1 line
2.29.2
PATCH v3 10/12 block/export: port virtio-blk discard/write zeroes input validation
In reply to Stefan HajnocziValidate discard/write zeroes the same way we do for virtio-blk. Some of these checks are mandated by the VIRTIO specification, others are internal to QEMU.
--- block/export/vhost-user-blk-server.c | 116 +++++++++++++++++++++------ 1 file changed, 93 insertions(+), 23 deletions(-)
block/export/vhost-user-blk-server.c +93 -23
@@ -29,6 +29,8 @@enum {VHOST_USER_BLK_NUM_QUEUES_DEFAULT =1 , +VHOST_USER_BLK_MAX_DISCARD_SECTORS =32768 , +VHOST_USER_BLK_MAX_WRITE_ZEROES_SECTORS =32768 , };struct virtio_blk_inhdr {unsigned char status;
@@ -65,30 +67,102 @@ static void vu_blk_req_complete(VuBlkReq *req)free (req); } +static bool vu_blk_sect_range_ok (VuBlkExport *vexp,uint64_t sector, +size_t size) +{ +uint64_t nb_sectors = size >>BDRV_SECTOR_BITS ; +uint64_t total_sectors; + +if (nb_sectors >BDRV_REQUEST_MAX_SECTORS ) { +return false; + } +if ((sector <<VIRTIO_BLK_SECTOR_BITS ) % vexp->blk_size) { +return false; + } +blk_get_geometry (vexp->export.blk, &total_sectors); +if (sector > total_sectors || nb_sectors > total_sectors - sector) { +return false; + } +return true; +} + static int coroutine_fn -vu_blk_discard_write_zeroes (BlockBackend *blk,struct iovec *iov, +vu_blk_discard_write_zeroes (VuBlkExport *vexp,struct iovec *iov,uint32_t iovcnt,uint32_t type) { +BlockBackend *blk = vexp->export.blk;struct virtio_blk_discard_write_zeroes desc; -ssize_t size =iov_to_buf (iov, iovcnt,0 , &desc,sizeof (desc)); +ssize_t size; +uint64_t sector; +uint32_t num_sectors; +uint32_t max_sectors; +uint32_t flags; +int bytes; + +/* Only one desc is currently supported */ +if (unlikely (iov_size (iov, iovcnt) >sizeof (desc))) { +return VIRTIO_BLK_S_UNSUPP ; + } + + size =iov_to_buf (iov, iovcnt,0 , &desc,sizeof (desc));if (unlikely (size !=sizeof (desc))) { -error_report ("Invalid size %zd, expect %zu" , size,sizeof (desc)); -return -EINVAL ; +error_report ("Invalid size %zd, expected %zu" , size,sizeof (desc)); +return VIRTIO_BLK_S_IOERR ; } -uint64_t range[2 ] = {le64_to_cpu (desc.sector) <<9 , -le32_to_cpu (desc.num_sectors) <<9 }; -if (type ==VIRTIO_BLK_T_DISCARD ) { -if (blk_co_pdiscard (blk, range[0 ], range[1 ]) ==0 ) { -return 0 ; + sector =le64_to_cpu (desc.sector); + num_sectors =le32_to_cpu (desc.num_sectors); + flags =le32_to_cpu (desc.flags); + max_sectors = (type ==VIRTIO_BLK_T_WRITE_ZEROES ) ? +VHOST_USER_BLK_MAX_WRITE_ZEROES_SECTORS : +VHOST_USER_BLK_MAX_DISCARD_SECTORS ; + +/* This check ensures that 'bytes' fits in an int */ +if (unlikely (num_sectors > max_sectors)) { +return VIRTIO_BLK_S_IOERR ; + } + + bytes = num_sectors <<VIRTIO_BLK_SECTOR_BITS ; + +if (unlikely (!vu_blk_sect_range_ok (vexp, sector, bytes))) { +return VIRTIO_BLK_S_IOERR ; + } + +/* +* The device MUST set the status byte to VIRTIO_BLK_S_UNSUPP for discard +* and write zeroes commands if any unknown flag is set. +*/ +if (unlikely (flags & ~VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP )) { +return VIRTIO_BLK_S_UNSUPP ; + } + +if (type ==VIRTIO_BLK_T_WRITE_ZEROES ) { +int blk_flags =0 ; + +if (flags &VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP ) { + blk_flags |=BDRV_REQ_MAY_UNMAP ; + } + +if (blk_co_pwrite_zeroes (blk, sector <<VIRTIO_BLK_SECTOR_BITS , + bytes, blk_flags) ==0 ) { +return VIRTIO_BLK_S_OK ; + } + }else if (type ==VIRTIO_BLK_T_DISCARD ) { +/* +* The device MUST set the status byte to VIRTIO_BLK_S_UNSUPP for +* discard commands if the unmap flag is set. +*/ +if (unlikely (flags &VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP )) { +return VIRTIO_BLK_S_UNSUPP ; } - }else if (type ==VIRTIO_BLK_T_WRITE_ZEROES ) { -if (blk_co_pwrite_zeroes (blk, range[0 ], range[1 ],0 ) ==0 ) { -return 0 ; + +if (blk_co_pdiscard (blk, sector <<VIRTIO_BLK_SECTOR_BITS , + bytes) ==0 ) { +return VIRTIO_BLK_S_OK ; } } -return -EINVAL ; +return VIRTIO_BLK_S_IOERR ; } static void coroutine_fn vu_blk_virtio_process_req(void *opaque)
@@ -177,19 +251,13 @@ static void coroutine_fn vu_blk_virtio_process_req(void *opaque) }case VIRTIO_BLK_T_DISCARD :case VIRTIO_BLK_T_WRITE_ZEROES : { -int rc; -if (!vexp->writable) { req->in->status =VIRTIO_BLK_S_IOERR ;break ; } - rc =vu_blk_discard_write_zeroes (blk, &elem->out_sg[1 ], out_num, type); -if (rc ==0 ) { - req->in->status =VIRTIO_BLK_S_OK ; - }else { - req->in->status =VIRTIO_BLK_S_IOERR ; - } + req->in->status =vu_blk_discard_write_zeroes (vexp, out_iov, out_num, + type);break ; }default :
@@ -362,11 +430,13 @@ vu_blk_initialize_config(BlockDriverState *bs, config->min_io_size =cpu_to_le16 (1 ); config->opt_io_size =cpu_to_le32 (1 ); config->num_queues =cpu_to_le16 (num_queues); - config->max_discard_sectors =cpu_to_le32 (32768 ); + config->max_discard_sectors = +cpu_to_le32 (VHOST_USER_BLK_MAX_DISCARD_SECTORS ); config->max_discard_seg =cpu_to_le32 (1 ); config->discard_sector_alignment =cpu_to_le32 (blk_size >>VIRTIO_BLK_SECTOR_BITS ); - config->max_write_zeroes_sectors =cpu_to_le32 (32768 ); + config->max_write_zeroes_sectors + =cpu_to_le32 (VHOST_USER_BLK_MAX_WRITE_ZEROES_SECTORS ); config->max_write_zeroes_seg =cpu_to_le32 (1 ); }
Signature · 1 line
2.29.2
PATCH v3 11/12 vhost-user-blk-test: test discard/write zeroes invalid inputs
In reply to Stefan HajnocziExercise input validation code paths inblock/export/vhost-user-blk-server.c.
--- tests/qtest/vhost-user-blk-test.c | 124 ++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+)
tests/qtest/vhost-user-blk-test.c +124 -0
@@ -94,6 +94,124 @@ static uint64_t virtio_blk_request(QGuestAllocator *alloc, QVirtioDevice *d,return addr; } +static void test_invalid_discard_write_zeroes (QVirtioDevice *dev, +QGuestAllocator *alloc, +QTestState *qts, +QVirtQueue *vq, +uint32_t type) +{ +QVirtioBlkReq req; +struct virtio_blk_discard_write_zeroes dwz_hdr; +struct virtio_blk_discard_write_zeroes dwz_hdr2[2 ]; +uint64_t req_addr; +uint32_t free_head; +uint8_t status; + +/* More than one dwz is not supported */ + req.type = type; + req.data = (char *) dwz_hdr2; + dwz_hdr2[0 ].sector =0 ; + dwz_hdr2[0 ].num_sectors =1 ; + dwz_hdr2[0 ].flags =0 ; + dwz_hdr2[1 ].sector =1 ; + dwz_hdr2[1 ].num_sectors =1 ; + dwz_hdr2[1 ].flags =0 ; + +virtio_blk_fix_dwz_hdr (dev, &dwz_hdr2[0 ]); +virtio_blk_fix_dwz_hdr (dev, &dwz_hdr2[1 ]); + + req_addr =virtio_blk_request (alloc, dev, &req,sizeof (dwz_hdr2)); + + free_head =qvirtqueue_add (qts, vq, req_addr,16 , false, true); +qvirtqueue_add (qts, vq, req_addr +16 ,sizeof (dwz_hdr2), false, true); +qvirtqueue_add (qts, vq, req_addr +16 +sizeof (dwz_hdr2),1 , true, + false); + +qvirtqueue_kick (qts, dev, vq, free_head); + +qvirtio_wait_used_elem (qts, dev, vq, free_head,NULL , +QVIRTIO_BLK_TIMEOUT_US ); + status =readb (req_addr +16 +sizeof (dwz_hdr2)); + g_assert_cmpint(status, ==, VIRTIO_BLK_S_UNSUPP); + +guest_free (alloc, req_addr); + +/* num_sectors must be less than config->max_write_zeroes_sectors */ + req.type = type; + req.data = (char *) &dwz_hdr; + dwz_hdr.sector =0 ; + dwz_hdr.num_sectors =0xffffffff ; + dwz_hdr.flags =0 ; + +virtio_blk_fix_dwz_hdr (dev, &dwz_hdr); + + req_addr =virtio_blk_request (alloc, dev, &req,sizeof (dwz_hdr)); + + free_head =qvirtqueue_add (qts, vq, req_addr,16 , false, true); +qvirtqueue_add (qts, vq, req_addr +16 ,sizeof (dwz_hdr), false, true); +qvirtqueue_add (qts, vq, req_addr +16 +sizeof (dwz_hdr),1 , true, + false); + +qvirtqueue_kick (qts, dev, vq, free_head); + +qvirtio_wait_used_elem (qts, dev, vq, free_head,NULL , +QVIRTIO_BLK_TIMEOUT_US ); + status =readb (req_addr +16 +sizeof (dwz_hdr)); + g_assert_cmpint(status, ==, VIRTIO_BLK_S_IOERR); + +guest_free (alloc, req_addr); + +/* sector must be less than the device capacity */ + req.type = type; + req.data = (char *) &dwz_hdr; + dwz_hdr.sector =TEST_IMAGE_SIZE /512 +1 ; + dwz_hdr.num_sectors =1 ; + dwz_hdr.flags =0 ; + +virtio_blk_fix_dwz_hdr (dev, &dwz_hdr); + + req_addr =virtio_blk_request (alloc, dev, &req,sizeof (dwz_hdr)); + + free_head =qvirtqueue_add (qts, vq, req_addr,16 , false, true); +qvirtqueue_add (qts, vq, req_addr +16 ,sizeof (dwz_hdr), false, true); +qvirtqueue_add (qts, vq, req_addr +16 +sizeof (dwz_hdr),1 , true, + false); + +qvirtqueue_kick (qts, dev, vq, free_head); + +qvirtio_wait_used_elem (qts, dev, vq, free_head,NULL , +QVIRTIO_BLK_TIMEOUT_US ); + status =readb (req_addr +16 +sizeof (dwz_hdr)); + g_assert_cmpint(status, ==, VIRTIO_BLK_S_IOERR); + +guest_free (alloc, req_addr); + +/* reserved flag bits must be zero */ + req.type = type; + req.data = (char *) &dwz_hdr; + dwz_hdr.sector =0 ; + dwz_hdr.num_sectors =1 ; + dwz_hdr.flags = ~VIRTIO_BLK_WRITE_ZEROES_FLAG_UNMAP ; + +virtio_blk_fix_dwz_hdr (dev, &dwz_hdr); + + req_addr =virtio_blk_request (alloc, dev, &req,sizeof (dwz_hdr)); + + free_head =qvirtqueue_add (qts, vq, req_addr,16 , false, true); +qvirtqueue_add (qts, vq, req_addr +16 ,sizeof (dwz_hdr), false, true); +qvirtqueue_add (qts, vq, req_addr +16 +sizeof (dwz_hdr),1 , true, + false); + +qvirtqueue_kick (qts, dev, vq, free_head); + +qvirtio_wait_used_elem (qts, dev, vq, free_head,NULL , +QVIRTIO_BLK_TIMEOUT_US ); + status =readb (req_addr +16 +sizeof (dwz_hdr)); + g_assert_cmpint(status, ==, VIRTIO_BLK_S_UNSUPP); + +guest_free (alloc, req_addr); +} +/* Returns the request virtqueue so the caller can perform further tests */ static QVirtQueue *test_basic (QVirtioDevice *dev,QGuestAllocator *alloc) {
@@ -235,6 +353,9 @@ static QVirtQueue *test_basic(QVirtioDevice *dev, QGuestAllocator *alloc)g_free (data);guest_free (alloc, req_addr); + +test_invalid_discard_write_zeroes (dev, alloc, qts, vq, +VIRTIO_BLK_T_WRITE_ZEROES ); }if (features & (1u <<VIRTIO_BLK_F_DISCARD )) {
@@ -263,6 +384,9 @@ static QVirtQueue *test_basic(QVirtioDevice *dev, QGuestAllocator *alloc) g_assert_cmpint(status, ==, 0);guest_free (alloc, req_addr); + +test_invalid_discard_write_zeroes (dev, alloc, qts, vq, +VIRTIO_BLK_T_DISCARD ); }if (features & (1u <<VIRTIO_F_ANY_LAYOUT )) {
Signature · 1 line
2.29.2
PATCH v3 12/12 block/export: port virtio-blk read/write range check
In reply to Stefan HajnocziCheck that the sector number and byte count are valid.
--- block/export/vhost-user-blk-server.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-)
block/export/vhost-user-blk-server.c +16 -3
@@ -209,6 +209,8 @@ static void coroutine_fn vu_blk_virtio_process_req(void *opaque)switch (type & ~VIRTIO_BLK_T_BARRIER ) {case VIRTIO_BLK_T_IN :case VIRTIO_BLK_T_OUT : { +QEMUIOVector qiov; +int64_t offset;ssize_t ret =0 ;bool is_write = type &VIRTIO_BLK_T_OUT ; req->sector_num =le64_to_cpu (req->out.sector);
@@ -218,13 +220,24 @@ static void coroutine_fn vu_blk_virtio_process_req(void *opaque)break ; } -int64_t offset = req->sector_num <<VIRTIO_BLK_SECTOR_BITS ; -QEMUIOVector qiov;if (is_write) {qemu_iovec_init_external (&qiov, out_iov, out_num); - ret =blk_co_pwritev (blk, offset, qiov.size, &qiov,0 ); }else {qemu_iovec_init_external (&qiov, in_iov, in_num); + } + +if (unlikely (!vu_blk_sect_range_ok (vexp, + req->sector_num, + qiov.size))) { + req->in->status =VIRTIO_BLK_S_IOERR ; +break ; + } + + offset = req->sector_num <<VIRTIO_BLK_SECTOR_BITS ; + +if (is_write) { + ret =blk_co_pwritev (blk, offset, qiov.size, &qiov,0 ); + }else { ret =blk_co_preadv (blk, offset, qiov.size, &qiov,0 ); }if (ret >=0 ) {
Signature · 1 line
2.29.2
Kevin Wolf
AppliedAm 23.02.2021 um 15:46 hat Stefan Hajnoczi geschrieben:
Quoted 20 lines
v3:
* Rebased onto qemu.git/master (7ef8134565dc)
* I asked Coiby Xu for clarification on a comment about vhost-user-blk-test in v2. Please wait for that discussion to finish before merging.v2:
* Add abrt handler that terminates qemu-storage-daemon to vhost-user-blk-test. No more orphaned processes on test failure. [Peter]
* Fix sector number calculation invhost-user-blk-server.c
* Introduce VIRTIO_BLK_SECTOR_BITS/SIZE to make code clearer [Max]
* Fixvhost-user-blk-server.cblk_sizedouble byteswap
* Fix vhost-user-blkblkcfg->num_queuesendianness [Peter]
* Squashed cleanups into Coiby vhost-user-blk-test commit so the code is easier to reviewThe vhost-user-blk server test was already in Michael Tsirkin's recent vhost pull request, but was dropped because it exposed vhost-user regressions (
b7c1bd9d7848and the Based-on tag below). Now that the vhost-user regressions are fixed we can re-introduce the test case.
Quoted 8 lines
This series adds missing input validation that led to a Coverity report. The virtio-blk read, write, discard, and write zeroes commands need to check sector/byte ranges and other inputs. This solves the issue Peter Maydell raised in "[PATCH for-5.2]
block/export/vhost-user-blk-server.c: Avoid potential integer overflow".Merging just the input validation patches would be possible too, but I prefer to merge the corresponding tests so the code is exercised by the CI.
Thanks, applied to the block branch.
Kevin