60061b
From ffdec41922a34b6fe4e7e11f259553d65b41563e Mon Sep 17 00:00:00 2001
60061b
From: Stefan Hajnoczi <stefanha@redhat.com>
60061b
Date: Tue, 11 Jan 2022 15:36:13 +0000
60061b
Subject: [PATCH 4/6] iotests/stream-error-on-reset: New test
60061b
60061b
RH-Author: Stefan Hajnoczi <stefanha@redhat.com>
60061b
RH-MergeRequest: 109: block-backend: prevent dangling BDS pointers across aio_poll()
60061b
RH-Commit: [2/2] 0ecb7010d9c121398e7ee22ee47dd85d89bcd941
60061b
RH-Bugzilla: 2021778 2036178
60061b
RH-Acked-by: Hanna Reitz <hreitz@redhat.com>
60061b
RH-Acked-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
60061b
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
60061b
60061b
Author: Hanna Reitz <hreitz@redhat.com>
60061b
60061b
Test the following scenario:
60061b
- Simple stream block in two-layer backing chain (base and top)
60061b
- The job is drained via blk_drain(), then an error occurs while the job
60061b
  settles the ongoing request
60061b
- And so the job completes while in blk_drain()
60061b
60061b
This was reported as a segfault, but is fixed by "block-backend: prevent
60061b
dangling BDS pointers across aio_poll()".
60061b
60061b
Buglink: https://bugzilla.redhat.com/show_bug.cgi?id=2036178
60061b
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
60061b
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
60061b
Message-Id: <20220111153613.25453-3-stefanha@redhat.com>
60061b
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
60061b
(cherry picked from commit 2ca1d5d6b91f8a52a5c651f660b2f58c94bf97ba)
60061b
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
60061b
---
60061b
 .../qemu-iotests/tests/stream-error-on-reset  | 140 ++++++++++++++++++
60061b
 .../tests/stream-error-on-reset.out           |   5 +
60061b
 2 files changed, 145 insertions(+)
60061b
 create mode 100755 tests/qemu-iotests/tests/stream-error-on-reset
60061b
 create mode 100644 tests/qemu-iotests/tests/stream-error-on-reset.out
60061b
60061b
diff --git a/tests/qemu-iotests/tests/stream-error-on-reset b/tests/qemu-iotests/tests/stream-error-on-reset
60061b
new file mode 100755
60061b
index 0000000000..7eaedb24d7
60061b
--- /dev/null
60061b
+++ b/tests/qemu-iotests/tests/stream-error-on-reset
60061b
@@ -0,0 +1,140 @@
60061b
+#!/usr/bin/env python3
60061b
+# group: rw quick
60061b
+#
60061b
+# Test what happens when a stream job completes in a blk_drain().
60061b
+#
60061b
+# Copyright (C) 2022 Red Hat, Inc.
60061b
+#
60061b
+# This program is free software; you can redistribute it and/or modify
60061b
+# it under the terms of the GNU General Public License as published by
60061b
+# the Free Software Foundation; either version 2 of the License, or
60061b
+# (at your option) any later version.
60061b
+#
60061b
+# This program is distributed in the hope that it will be useful,
60061b
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
60061b
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
60061b
+# GNU General Public License for more details.
60061b
+#
60061b
+# You should have received a copy of the GNU General Public License
60061b
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
60061b
+#
60061b
+
60061b
+import os
60061b
+import iotests
60061b
+from iotests import imgfmt, qemu_img_create, qemu_io_silent, QMPTestCase
60061b
+
60061b
+
60061b
+image_size = 1 * 1024 * 1024
60061b
+data_size = 64 * 1024
60061b
+base = os.path.join(iotests.test_dir, 'base.img')
60061b
+top = os.path.join(iotests.test_dir, 'top.img')
60061b
+
60061b
+
60061b
+# We want to test completing a stream job in a blk_drain().
60061b
+#
60061b
+# The blk_drain() we are going to use is a virtio-scsi device resetting,
60061b
+# which we can trigger by resetting the system.
60061b
+#
60061b
+# In order to have the block job complete on drain, we (1) throttle its
60061b
+# base image so we can start the drain after it has begun, but before it
60061b
+# completes, and (2) make it encounter an I/O error on the ensuing write.
60061b
+# (If it completes regularly, the completion happens after the drain for
60061b
+# some reason.)
60061b
+
60061b
+class TestStreamErrorOnReset(QMPTestCase):
60061b
+    def setUp(self) -> None:
60061b
+        """
60061b
+        Create two images:
60061b
+        - base image {base} with {data_size} bytes allocated
60061b
+        - top image {top} without any data allocated
60061b
+
60061b
+        And the following VM configuration:
60061b
+        - base image throttled to {data_size}
60061b
+        - top image with a blkdebug configuration so the first write access
60061b
+          to it will result in an error
60061b
+        - top image is attached to a virtio-scsi device
60061b
+        """
60061b
+        assert qemu_img_create('-f', imgfmt, base, str(image_size)) == 0
60061b
+        assert qemu_io_silent('-c', f'write 0 {data_size}', base) == 0
60061b
+        assert qemu_img_create('-f', imgfmt, top, str(image_size)) == 0
60061b
+
60061b
+        self.vm = iotests.VM()
60061b
+        self.vm.add_args('-accel', 'tcg') # Make throttling work properly
60061b
+        self.vm.add_object(self.vm.qmp_to_opts({
60061b
+            'qom-type': 'throttle-group',
60061b
+            'id': 'thrgr',
60061b
+            'x-bps-total': str(data_size)
60061b
+        }))
60061b
+        self.vm.add_blockdev(self.vm.qmp_to_opts({
60061b
+            'driver': imgfmt,
60061b
+            'node-name': 'base',
60061b
+            'file': {
60061b
+                'driver': 'throttle',
60061b
+                'throttle-group': 'thrgr',
60061b
+                'file': {
60061b
+                    'driver': 'file',
60061b
+                    'filename': base
60061b
+                }
60061b
+            }
60061b
+        }))
60061b
+        self.vm.add_blockdev(self.vm.qmp_to_opts({
60061b
+            'driver': imgfmt,
60061b
+            'node-name': 'top',
60061b
+            'file': {
60061b
+                'driver': 'blkdebug',
60061b
+                'node-name': 'top-blkdebug',
60061b
+                'inject-error': [{
60061b
+                    'event': 'pwritev',
60061b
+                    'immediately': 'true',
60061b
+                    'once': 'true'
60061b
+                }],
60061b
+                'image': {
60061b
+                    'driver': 'file',
60061b
+                    'filename': top
60061b
+                }
60061b
+            },
60061b
+            'backing': 'base'
60061b
+        }))
60061b
+        self.vm.add_device(self.vm.qmp_to_opts({
60061b
+            'driver': 'virtio-scsi',
60061b
+            'id': 'vscsi'
60061b
+        }))
60061b
+        self.vm.add_device(self.vm.qmp_to_opts({
60061b
+            'driver': 'scsi-hd',
60061b
+            'bus': 'vscsi.0',
60061b
+            'drive': 'top'
60061b
+        }))
60061b
+        self.vm.launch()
60061b
+
60061b
+    def tearDown(self) -> None:
60061b
+        self.vm.shutdown()
60061b
+        os.remove(top)
60061b
+        os.remove(base)
60061b
+
60061b
+    def test_stream_error_on_reset(self) -> None:
60061b
+        # Launch a stream job, which will take at least a second to
60061b
+        # complete, because the base image is throttled (so we can
60061b
+        # get in between it having started and it having completed)
60061b
+        res = self.vm.qmp('block-stream', job_id='stream', device='top')
60061b
+        self.assert_qmp(res, 'return', {})
60061b
+
60061b
+        while True:
60061b
+            ev = self.vm.event_wait('JOB_STATUS_CHANGE')
60061b
+            if ev['data']['status'] == 'running':
60061b
+                # Once the stream job is running, reset the system, which
60061b
+                # forces the virtio-scsi device to be reset, thus draining
60061b
+                # the stream job, and making it complete.  Completing
60061b
+                # inside of that drain should not result in a segfault.
60061b
+                res = self.vm.qmp('system_reset')
60061b
+                self.assert_qmp(res, 'return', {})
60061b
+            elif ev['data']['status'] == 'null':
60061b
+                # The test is done once the job is gone
60061b
+                break
60061b
+
60061b
+
60061b
+if __name__ == '__main__':
60061b
+    # Passes with any format with backing file support, but qed and
60061b
+    # qcow1 do not seem to exercise the used-to-be problematic code
60061b
+    # path, so there is no point in having them in this list
60061b
+    iotests.main(supported_fmts=['qcow2', 'vmdk'],
60061b
+                 supported_protocols=['file'])
60061b
diff --git a/tests/qemu-iotests/tests/stream-error-on-reset.out b/tests/qemu-iotests/tests/stream-error-on-reset.out
60061b
new file mode 100644
60061b
index 0000000000..ae1213e6f8
60061b
--- /dev/null
60061b
+++ b/tests/qemu-iotests/tests/stream-error-on-reset.out
60061b
@@ -0,0 +1,5 @@
60061b
+.
60061b
+----------------------------------------------------------------------
60061b
+Ran 1 tests
60061b
+
60061b
+OK
60061b
-- 
60061b
2.27.0
60061b