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