1be5c7
From 27042ff7aca4366c50e8ed66b47487d46774d16a Mon Sep 17 00:00:00 2001
1be5c7
From: Hanna Reitz <hreitz@redhat.com>
1be5c7
Date: Wed, 16 Feb 2022 11:53:55 +0100
1be5c7
Subject: [PATCH 24/24] iotests/graph-changes-while-io: New test
1be5c7
1be5c7
RH-Author: Hanna Reitz <hreitz@redhat.com>
1be5c7
RH-MergeRequest: 189: block: Make bdrv_refresh_limits() non-recursive
1be5c7
RH-Commit: [3/3] b9dffe09bef6cf9b2f0aad69b327ea1df92e847a
1be5c7
RH-Bugzilla: 2072932
1be5c7
RH-Acked-by: Eric Blake <eblake@redhat.com>
1be5c7
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
1be5c7
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
1be5c7
1be5c7
Test the following scenario:
1be5c7
1. Some block node (null-co) attached to a user (here: NBD server) that
1be5c7
   performs I/O and keeps the node in an I/O thread
1be5c7
2. Repeatedly run blockdev-add/blockdev-del to add/remove an overlay
1be5c7
   to/from that node
1be5c7
1be5c7
Each blockdev-add triggers bdrv_refresh_limits(), and because
1be5c7
blockdev-add runs in the main thread, it does not stop the I/O requests.
1be5c7
I/O can thus happen while the limits are refreshed, and when such a
1be5c7
request sees a temporarily invalid block limit (e.g. alignment is 0),
1be5c7
this may easily crash qemu (or the storage daemon in this case).
1be5c7
1be5c7
The block layer needs to ensure that I/O requests to a node are paused
1be5c7
while that node's BlockLimits are refreshed.
1be5c7
1be5c7
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
1be5c7
Reviewed-by: Eric Blake <eblake@redhat.com>
1be5c7
Message-Id: <20220216105355.30729-4-hreitz@redhat.com>
1be5c7
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
1be5c7
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
1be5c7
(cherry picked from commit 971bea8089531af56b1bbd9ce62e756bdf006711)
1be5c7
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
1be5c7
---
1be5c7
 .../qemu-iotests/tests/graph-changes-while-io | 91 +++++++++++++++++++
1be5c7
 .../tests/graph-changes-while-io.out          |  5 +
1be5c7
 2 files changed, 96 insertions(+)
1be5c7
 create mode 100755 tests/qemu-iotests/tests/graph-changes-while-io
1be5c7
 create mode 100644 tests/qemu-iotests/tests/graph-changes-while-io.out
1be5c7
1be5c7
diff --git a/tests/qemu-iotests/tests/graph-changes-while-io b/tests/qemu-iotests/tests/graph-changes-while-io
1be5c7
new file mode 100755
1be5c7
index 0000000000..567e8cf21e
1be5c7
--- /dev/null
1be5c7
+++ b/tests/qemu-iotests/tests/graph-changes-while-io
1be5c7
@@ -0,0 +1,91 @@
1be5c7
+#!/usr/bin/env python3
1be5c7
+# group: rw
1be5c7
+#
1be5c7
+# Test graph changes while I/O is happening
1be5c7
+#
1be5c7
+# Copyright (C) 2022 Red Hat, Inc.
1be5c7
+#
1be5c7
+# This program is free software; you can redistribute it and/or modify
1be5c7
+# it under the terms of the GNU General Public License as published by
1be5c7
+# the Free Software Foundation; either version 2 of the License, or
1be5c7
+# (at your option) any later version.
1be5c7
+#
1be5c7
+# This program is distributed in the hope that it will be useful,
1be5c7
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1be5c7
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1be5c7
+# GNU General Public License for more details.
1be5c7
+#
1be5c7
+# You should have received a copy of the GNU General Public License
1be5c7
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1be5c7
+#
1be5c7
+
1be5c7
+import os
1be5c7
+from threading import Thread
1be5c7
+import iotests
1be5c7
+from iotests import imgfmt, qemu_img, qemu_img_create, QMPTestCase, \
1be5c7
+        QemuStorageDaemon
1be5c7
+
1be5c7
+
1be5c7
+top = os.path.join(iotests.test_dir, 'top.img')
1be5c7
+nbd_sock = os.path.join(iotests.sock_dir, 'nbd.sock')
1be5c7
+
1be5c7
+
1be5c7
+def do_qemu_img_bench() -> None:
1be5c7
+    """
1be5c7
+    Do some I/O requests on `nbd_sock`.
1be5c7
+    """
1be5c7
+    assert qemu_img('bench', '-f', 'raw', '-c', '2000000',
1be5c7
+                    f'nbd+unix:///node0?socket={nbd_sock}') == 0
1be5c7
+
1be5c7
+
1be5c7
+class TestGraphChangesWhileIO(QMPTestCase):
1be5c7
+    def setUp(self) -> None:
1be5c7
+        # Create an overlay that can be added at runtime on top of the
1be5c7
+        # null-co block node that will receive I/O
1be5c7
+        assert qemu_img_create('-f', imgfmt, '-F', 'raw', '-b', 'null-co://',
1be5c7
+                               top) == 0
1be5c7
+
1be5c7
+        # QSD instance with a null-co block node in an I/O thread,
1be5c7
+        # exported over NBD (on `nbd_sock`, export name "node0")
1be5c7
+        self.qsd = QemuStorageDaemon(
1be5c7
+            '--object', 'iothread,id=iothread0',
1be5c7
+            '--blockdev', 'null-co,node-name=node0,read-zeroes=true',
1be5c7
+            '--nbd-server', f'addr.type=unix,addr.path={nbd_sock}',
1be5c7
+            '--export', 'nbd,id=exp0,node-name=node0,iothread=iothread0,' +
1be5c7
+                        'fixed-iothread=true,writable=true',
1be5c7
+            qmp=True
1be5c7
+        )
1be5c7
+
1be5c7
+    def tearDown(self) -> None:
1be5c7
+        self.qsd.stop()
1be5c7
+
1be5c7
+    def test_blockdev_add_while_io(self) -> None:
1be5c7
+        # Run qemu-img bench in the background
1be5c7
+        bench_thr = Thread(target=do_qemu_img_bench)
1be5c7
+        bench_thr.start()
1be5c7
+
1be5c7
+        # While qemu-img bench is running, repeatedly add and remove an
1be5c7
+        # overlay to/from node0
1be5c7
+        while bench_thr.is_alive():
1be5c7
+            result = self.qsd.qmp('blockdev-add', {
1be5c7
+                'driver': imgfmt,
1be5c7
+                'node-name': 'overlay',
1be5c7
+                'backing': 'node0',
1be5c7
+                'file': {
1be5c7
+                    'driver': 'file',
1be5c7
+                    'filename': top
1be5c7
+                }
1be5c7
+            })
1be5c7
+            self.assert_qmp(result, 'return', {})
1be5c7
+
1be5c7
+            result = self.qsd.qmp('blockdev-del', {
1be5c7
+                'node-name': 'overlay'
1be5c7
+            })
1be5c7
+            self.assert_qmp(result, 'return', {})
1be5c7
+
1be5c7
+        bench_thr.join()
1be5c7
+
1be5c7
+if __name__ == '__main__':
1be5c7
+    # Format must support raw backing files
1be5c7
+    iotests.main(supported_fmts=['qcow', 'qcow2', 'qed'],
1be5c7
+                 supported_protocols=['file'])
1be5c7
diff --git a/tests/qemu-iotests/tests/graph-changes-while-io.out b/tests/qemu-iotests/tests/graph-changes-while-io.out
1be5c7
new file mode 100644
1be5c7
index 0000000000..ae1213e6f8
1be5c7
--- /dev/null
1be5c7
+++ b/tests/qemu-iotests/tests/graph-changes-while-io.out
1be5c7
@@ -0,0 +1,5 @@
1be5c7
+.
1be5c7
+----------------------------------------------------------------------
1be5c7
+Ran 1 tests
1be5c7
+
1be5c7
+OK
1be5c7
-- 
1be5c7
2.35.3
1be5c7