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