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