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