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