cryptospore / rpms / qemu-kvm

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