Skip to contentSkip to conversation

file-posix: Clean up and fix zoned checks

PATCH In review Hanna Czenczek 3 participants 13 messages Aug 24, 2023 – Sep 21, 2023 Block layer

lore
Share & tools
lore
#0 lore raw

cover file-posix: Clean up and fix zoned checks

Hi,

As presented in [1] there is a bug in the zone code in raw_co_prw(), specifically we don’t check whether there actually is zone information before running code that assumes there is (and thus we run into a division by zero). This has now also been reported in [2].

I believe the solution [1] is incomplete, though, which is why I’m sending this separate series: I don’t think checking bs->wps and/or bs->bl.zone_size to determine whether there is zone information is right; for example, we do not have raw_refresh_zoned_limits() clear those values if on a refresh, zone information were to disappear.

It is also weird that we separate checking bs->wps and bs->bl.zone_size at all; raw_refresh_zoned_limits() seems to intend to ensure that either we have information with non-NULL bs->wps and non-zero bs->bl.zone_size, or we don’t.

I think we should have a single flag that tells whether we have valid information or not, and it looks to me like bs->bl.zoned != BLK_Z_NONE is the condition that fits best.

Patch 1 ensures that raw_refresh_zoned_limits() will set bs->bl.zoned to BLK_Z_NONE on error, so that we can actually be sure that this condition tells us whether we have valid information or not.

Patch 2 unifies all conditional checks for zone information to use bs->bl.zoned != BLK_Z_NONE.

Patch 3 is the I/O error path fix, which is not really different from [1].

Patch 4 does a bit of clean-up.

Patch 5 adds a regression test.

[1] https://lists.nongnu.org/archive/html/qemu-devel/2023-06/msg01742.html
[2] https://bugzilla.redhat.com/show_bug.cgi?id=2234374

Hanna Czenczek (5):
file-posix: Clear bs->bl.zoned on error
file-posix: Check bs->bl.zoned for zone info
file-posix: Fix zone update in I/O error path
file-posix: Simplify raw_co_prw's 'out' zone code tests/file-io-error: New test

 block/file-posix.c                         | 42 +++++----
 tests/qemu-iotests/tests/file-io-error     | 99 ++++++++++++++++++++++
 tests/qemu-iotests/tests/file-io-error.out | 31 +++++++
 3 files changed, 150 insertions(+), 22 deletions(-)
 create mode 100755 tests/qemu-iotests/tests/file-io-error
 create mode 100644 tests/qemu-iotests/tests/file-io-error.out
Signature · 1 line
2.41.0
#1 lore raw

PATCH 1/5 file-posix: Clear bs->bl.zoned on error

bs->bl.zoned is what indicates whether the zone information is present and valid; it is the only thing that raw_refresh_zoned_limits() sets if CONFIG_BLKZONED is not defined, and it is also the only thing that it sets if CONFIG_BLKZONED is defined, but there are no zones.

Make sure that it is always set to BLK_Z_NONE if there is an error anywhere in raw_refresh_zoned_limits() so that we do not accidentally announce zones while our information is incomplete or invalid.

This also fixes a memory leak in the last error path in raw_refresh_zoned_limits().

---
 block/file-posix.c | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)
block/file-posix.c +12 -9
index b16e9c21a1..2b88b9eefa 100644
@@ -1412,11 +1412,9 @@ static void raw_refresh_zoned_limits(BlockDriverState *bs, struct stat *st,
     BlockZoneModel zoned;
     int ret;
 
-    bs->bl.zoned = BLK_Z_NONE;
-
     ret = get_sysfs_zoned_model(st, &zoned);
     if (ret < 0 || zoned == BLK_Z_NONE) {
-        return;
+        goto no_zoned;
     }
     bs->bl.zoned = zoned;
 
@@ -1437,10 +1435,10 @@ static void raw_refresh_zoned_limits(BlockDriverState *bs, struct stat *st,
     if (ret < 0) {
         error_setg_errno(errp, -ret, "Unable to read chunk_sectors "
                                      "sysfs attribute");
-        return;
+        goto no_zoned;
     } else if (!ret) {
         error_setg(errp, "Read 0 from chunk_sectors sysfs attribute");
-        return;
+        goto no_zoned;
     }
     bs->bl.zone_size = ret << BDRV_SECTOR_BITS;
 
@@ -1448,10 +1446,10 @@ static void raw_refresh_zoned_limits(BlockDriverState *bs, struct stat *st,
     if (ret < 0) {
         error_setg_errno(errp, -ret, "Unable to read nr_zones "
                                      "sysfs attribute");
-        return;
+        goto no_zoned;
     } else if (!ret) {
         error_setg(errp, "Read 0 from nr_zones sysfs attribute");
-        return;
+        goto no_zoned;
     }
     bs->bl.nr_zones = ret;
 
@@ -1472,10 +1470,15 @@ static void raw_refresh_zoned_limits(BlockDriverState *bs, struct stat *st,
     ret = get_zones_wp(bs, s->fd, 0, bs->bl.nr_zones, 0);
     if (ret < 0) {
         error_setg_errno(errp, -ret, "report wps failed");
-        bs->wps = NULL;
-        return;
+        goto no_zoned;
     }
     qemu_co_mutex_init(&bs->wps->colock);
+    return;
+
+no_zoned:
+    bs->bl.zoned = BLK_Z_NONE;
+    g_free(bs->wps);
+    bs->wps = NULL;
 }
 #else /* !defined(CONFIG_BLKZONED) */
 static void raw_refresh_zoned_limits(BlockDriverState *bs, struct stat *st,
Signature · 1 line
2.41.0
Reviewed-by on v1 1/5
#2 lore raw

Re: [PATCH 1/5] file-posix: Clear bs->bl.zoned on error

Quoted 10 lines · Hanna Czenczek <[email protected]> 于2023年8月24日周四 23:53写道:

bs->bl.zoned is what indicates whether the zone information is present and valid; it is the only thing that raw_refresh_zoned_limits() sets if CONFIG_BLKZONED is not defined, and it is also the only thing that it sets if CONFIG_BLKZONED is defined, but there are no zones.

Make sure that it is always set to BLK_Z_NONE if there is an error anywhere in raw_refresh_zoned_limits() so that we do not accidentally announce zones while our information is incomplete or invalid.

Quoted 7 lines

This also fixes a memory leak in the last error path in raw_refresh_zoned_limits().

---
 block/file-posix.c | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)
#3 lore raw

PATCH 2/5 file-posix: Check bs->bl.zoned for zone info

In reply to Hanna Czenczek

Instead of checking bs->wps or bs->bl.zone_size for whether zone information is present, check bs->bl.zoned. That is the flag that raw_refresh_zoned_limits() reliably sets to indicate zone support. If it is set to something other than BLK_Z_NONE, other values and objects like bs->wps and bs->bl.zone_size must be non-null/zero and valid; if it is not, we cannot rely on their validity.

---
 block/file-posix.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
block/file-posix.c +7 -5
index 2b88b9eefa..46e22403fe 100644
@@ -2455,9 +2455,10 @@ static int coroutine_fn raw_co_prw(BlockDriverState *bs, uint64_t offset,
     if (fd_open(bs) < 0)
         return -EIO;
 #if defined(CONFIG_BLKZONED)
-    if ((type & (QEMU_AIO_WRITE | QEMU_AIO_ZONE_APPEND)) && bs->wps) {
+    if ((type & (QEMU_AIO_WRITE | QEMU_AIO_ZONE_APPEND)) &&
+        bs->bl.zoned != BLK_Z_NONE) {
         qemu_co_mutex_lock(&bs->wps->colock);
-        if (type & QEMU_AIO_ZONE_APPEND && bs->bl.zone_size) {
+        if (type & QEMU_AIO_ZONE_APPEND) {
             int index = offset / bs->bl.zone_size;
             offset = bs->wps->wp[index];
         }
@@ -2508,8 +2509,8 @@ out:
 {
     BlockZoneWps *wps = bs->wps;
     if (ret == 0) {
-        if ((type & (QEMU_AIO_WRITE | QEMU_AIO_ZONE_APPEND))
-            && wps && bs->bl.zone_size) {
+        if ((type & (QEMU_AIO_WRITE | QEMU_AIO_ZONE_APPEND)) &&
+            bs->bl.zoned != BLK_Z_NONE) {
             uint64_t *wp = &wps->wp[offset / bs->bl.zone_size];
             if (!BDRV_ZT_IS_CONV(*wp)) {
                 if (type & QEMU_AIO_ZONE_APPEND) {
@@ -2529,7 +2530,8 @@ out:
         }
     }
 
-    if ((type & (QEMU_AIO_WRITE | QEMU_AIO_ZONE_APPEND)) && wps) {
+    if ((type & (QEMU_AIO_WRITE | QEMU_AIO_ZONE_APPEND)) &&
+        bs->blk.zoned != BLK_Z_NONE) {
         qemu_co_mutex_unlock(&wps->colock);
     }
 }
Signature · 1 line
2.41.0
Reviewed-by on v1 2/5
#4 lore raw

Re: [PATCH 2/5] file-posix: Check bs->bl.zoned for zone info

Quoted 8 lines · Hanna Czenczek <[email protected]> 于2023年8月24日周四 23:53写道:

Instead of checking bs->wps or bs->bl.zone_size for whether zone information is present, check bs->bl.zoned. That is the flag that raw_refresh_zoned_limits() reliably sets to indicate zone support. If it is set to something other than BLK_Z_NONE, other values and objects like bs->wps and bs->bl.zone_size must be non-null/zero and valid; if it is not, we cannot rely on their validity.

Quoted 4 lines
---
 block/file-posix.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
#5 lore raw

PATCH 3/5 file-posix: Fix zone update in I/O error path

In reply to Hanna Czenczek

We must check that zone information is present before running update_zones_wp().

---
 block/file-posix.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
block/file-posix.c +2 -1
index 46e22403fe..a050682e97 100644
@@ -2525,7 +2525,8 @@ out:
             }
         }
     } else {
-        if (type & (QEMU_AIO_WRITE | QEMU_AIO_ZONE_APPEND)) {
+        if ((type & (QEMU_AIO_WRITE | QEMU_AIO_ZONE_APPEND)) &&
+            bs->bl.zoned != BLK_Z_NONE) {
             update_zones_wp(bs, s->fd, 0, 1);
         }
     }
Signature · 1 line
2.41.0
Reviewed-by on v1 3/5
#6 lore raw

Re: [PATCH 3/5] file-posix: Fix zone update in I/O error path

Quoted 10 lines · Hanna Czenczek <[email protected]> 于2023年8月24日周四 23:53写道:

We must check that zone information is present before running update_zones_wp().

---
 block/file-posix.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
#7 lore raw

PATCH 4/5 file-posix: Simplify raw_co_prw's 'out' zone code

In reply to Hanna Czenczek

We duplicate the same condition three times here, pull it out to the top level.

---
 block/file-posix.c | 18 +++++-------------
 1 file changed, 5 insertions(+), 13 deletions(-)
block/file-posix.c +5 -13
index a050682e97..aa89789737 100644
@@ -2506,11 +2506,10 @@ static int coroutine_fn raw_co_prw(BlockDriverState *bs, uint64_t offset,
 
 out:
 #if defined(CONFIG_BLKZONED)
-{
-    BlockZoneWps *wps = bs->wps;
-    if (ret == 0) {
-        if ((type & (QEMU_AIO_WRITE | QEMU_AIO_ZONE_APPEND)) &&
-            bs->bl.zoned != BLK_Z_NONE) {
+    if ((type & (QEMU_AIO_WRITE | QEMU_AIO_ZONE_APPEND)) &&
+        bs->bl.zoned != BLK_Z_NONE) {
+        BlockZoneWps *wps = bs->wps;
+        if (ret == 0) {
             uint64_t *wp = &wps->wp[offset / bs->bl.zone_size];
             if (!BDRV_ZT_IS_CONV(*wp)) {
                 if (type & QEMU_AIO_ZONE_APPEND) {
@@ -2523,19 +2522,12 @@ out:
                     *wp = offset + bytes;
                 }
             }
-        }
-    } else {
-        if ((type & (QEMU_AIO_WRITE | QEMU_AIO_ZONE_APPEND)) &&
-            bs->bl.zoned != BLK_Z_NONE) {
+        } else {
             update_zones_wp(bs, s->fd, 0, 1);
         }
-    }
 
-    if ((type & (QEMU_AIO_WRITE | QEMU_AIO_ZONE_APPEND)) &&
-        bs->blk.zoned != BLK_Z_NONE) {
         qemu_co_mutex_unlock(&wps->colock);
     }
-}
 #endif
     return ret;
 }
Signature · 1 line
2.41.0
Reviewed-by on v1 4/5
#8 lore raw

Re: [PATCH 4/5] file-posix: Simplify raw_co_prw's 'out' zone code

Quoted 8 lines · Hanna Czenczek <[email protected]> 于2023年8月24日周四 23:53写道:

We duplicate the same condition three times here, pull it out to the top level.

---
 block/file-posix.c | 18 +++++-------------
 1 file changed, 5 insertions(+), 13 deletions(-)
#9 lore raw

PATCH 5/5 tests/file-io-error: New test

In reply to Hanna Czenczek

This is a regression test for
https://bugzilla.redhat.com/show_bug.cgi?id=2234374.

All this test needs to do is trigger an I/O error inside of file-posix (specifically raw_co_prw()). One reliable way to do this without requiring special privileges is to use a FUSE export, which allows us to inject any error that we want, e.g. via blkdebug.

---
 tests/qemu-iotests/tests/file-io-error     | 99 ++++++++++++++++++++++
 tests/qemu-iotests/tests/file-io-error.out | 31 +++++++
 2 files changed, 130 insertions(+)
 create mode 100755 tests/qemu-iotests/tests/file-io-error
 create mode 100644 tests/qemu-iotests/tests/file-io-error.out
tests/qemu-iotests/tests/file-io-error new file +99 -0
new file mode 100755
index 0000000000..a47ad0a678
@@ -0,0 +1,99 @@
+#!/usr/bin/env bash
+# group: rw
+#
+# Produce an I/O error in file-posix, and hope that it is not catastrophic.
+# Regression test for: https://bugzilla.redhat.com/show_bug.cgi?id=2234374
+#
+# Copyright (C) 2023 Red Hat, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+
+seq=$(basename "$0")
+echo "QA output created by $seq"
+
+status=1	# failure is the default!
+
+_cleanup()
+{
+    _cleanup_qemu
+    rm -f "$TEST_DIR/fuse-export"
+}
+trap "_cleanup; exit \$status" 0 1 2 3 15
+
+# get standard environment, filters and checks
+. ../common.rc
+. ../common.filter
+. ../common.qemu
+
+# Format-agnostic (we do not use any), but we do test the file protocol
+_supported_proto file
+_require_drivers blkdebug null-co
+
+# This is a regression test of a bug in which flie-posix would access zone
+# information in case of an I/O error even when there is no zone information,
+# resulting in a division by zero.
+# To reproduce the problem, we need to trigger an I/O error inside of
+# file-posix, which can be done (rootless) by providing a FUSE export that
+# presents only errors when accessed.
+
+_launch_qemu
+_send_qemu_cmd $QEMU_HANDLE \
+    "{'execute': 'qmp_capabilities'}" \
+
+_send_qemu_cmd $QEMU_HANDLE \
+    "{'execute': 'blockdev-add',
+      'arguments': {
+          'driver': 'blkdebug',
+          'node-name': 'node0',
+          'inject-error': [{'event': 'none'}],
+          'image': {
+              'driver': 'null-co'
+          }
+      }}" \
+    'return'
+
+# FUSE mountpoint must exist and be a regular file
+touch "$TEST_DIR/fuse-export"
+
+_send_qemu_cmd $QEMU_HANDLE \
+    "{'execute': 'block-export-add',
+      'arguments': {
+          'id': 'exp0',
+          'type': 'fuse',
+          'node-name': 'node0',
+          'mountpoint': '$TEST_DIR/fuse-export',
+          'writable': true
+      }}" \
+    'return'
+
+echo
+# This should fail, but gracefully, i.e. just print an I/O error, not crash.
+$QEMU_IO -f file -c 'write 0 64M' "$TEST_DIR/fuse-export" | _filter_qemu_io
+echo
+
+_send_qemu_cmd $QEMU_HANDLE \
+    "{'execute': 'block-export-del',
+      'arguments': {'id': 'exp0'}}" \
+    'return'
+
+_send_qemu_cmd $QEMU_HANDLE \
+    "{'execute': 'blockdev-del',
+      'arguments': {'node-name': 'node0'}}" \
+    'return'
+
+# success, all done
+echo "*** done"
+rm -f $seq.full
+status=0
tests/qemu-iotests/tests/file-io-error.out new file +31 -0
new file mode 100644
index 0000000000..4e6758c62c
@@ -0,0 +1,31 @@
+QA output created by file-io-error
+{'execute': 'qmp_capabilities'}
+{'execute': 'blockdev-add',
+      'arguments': {
+          'driver': 'blkdebug',
+          'node-name': 'node0',
+          'inject-error': [{'event': 'none'}],
+          'image': {
+              'driver': 'null-co'
+          }
+      }}
+{"return": {}}
+{'execute': 'block-export-add',
+      'arguments': {
+          'id': 'exp0',
+          'type': 'fuse',
+          'node-name': 'node0',
+          'mountpoint': 'TEST_DIR/fuse-export',
+          'writable': true
+      }}
+{"return": {}}
+
+write failed: Input/output error
+
+{'execute': 'block-export-del',
+      'arguments': {'id': 'exp0'}}
+{"return": {}}
+{'execute': 'blockdev-del',
+      'arguments': {'node-name': 'node0'}}
+{"return": {}}
+*** done
Signature · 1 line
2.41.0
#10 lore raw
In reply to Hanna Czenczek

Hi Hanna,

Quoted 7 lines · Hanna Czenczek <[email protected]> 于2023年8月24日周四 23:53写道:

Hi,

As presented in [1] there is a bug in the zone code in raw_co_prw(), specifically we don’t check whether there actually is zone information before running code that assumes there is (and thus we run into a division by zero). This has now also been reported in [2].

Thanks for catching the bugs and your work.

Quoted 7 lines

I believe the solution [1] is incomplete, though, which is why I’m sending this separate series: I don’t think checking bs->wps and/or bs->bl.zone_size to determine whether there is zone information is right; for example, we do not have raw_refresh_zoned_limits() clear those values if on a refresh, zone information were to disappear.

Quoted 8 lines

It is also weird that we separate checking bs->wps and bs->bl.zone_size at all; raw_refresh_zoned_limits() seems to intend to ensure that either we have information with non-NULL bs->wps and non-zero bs->bl.zone_size, or we don’t.

I think we should have a single flag that tells whether we have valid information or not, and it looks to me like bs->bl.zoned != BLK_Z_NONE is the condition that fits best.

The former way only checks zone information when it is being used to avoid divide-by-zero or nullptr errors. Putting the error path with non-zoned model implies a zoned device must have non-zero zone size and allocated write pointers. Given that no other parts are changing the zone_size to 0 and free wps, It does simplify the code path.

Thanks,
Sam

#11 lore raw
In reply to Hanna Czenczek
Quoted 32 lines · Hanna Czenczek

Hi,

As presented in [1] there is a bug in the zone code in raw_co_prw(), specifically we don’t check whether there actually is zone information before running code that assumes there is (and thus we run into a division by zero). This has now also been reported in [2].

I believe the solution [1] is incomplete, though, which is why I’m sending this separate series: I don’t think checking bs->wps and/or bs->bl.zone_size to determine whether there is zone information is right; for example, we do not have raw_refresh_zoned_limits() clear those values if on a refresh, zone information were to disappear.

It is also weird that we separate checking bs->wps and bs->bl.zone_size at all; raw_refresh_zoned_limits() seems to intend to ensure that either we have information with non-NULL bs->wps and non-zero bs->bl.zone_size, or we don’t.

I think we should have a single flag that tells whether we have valid information or not, and it looks to me like bs->bl.zoned != BLK_Z_NONE is the condition that fits best.

Patch 1 ensures that raw_refresh_zoned_limits() will set bs->bl.zoned to BLK_Z_NONE on error, so that we can actually be sure that this condition tells us whether we have valid information or not.

Patch 2 unifies all conditional checks for zone information to use bs->bl.zoned != BLK_Z_NONE.

Patch 3 is the I/O error path fix, which is not really different from [1].

Quoted 7 lines

Patch 4 does a bit of clean-up.

Patch 5 adds a regression test.

[1] https://lists.nongnu.org/archive/html/qemu-devel/2023-06/msg01742.html
[2] https://bugzilla.redhat.com/show_bug.cgi?id=2234374

Is this stable-worthy (at least 1-3)? From the bug description it smells like it should be in 8.1.x, or maybe whole series.

Thanks,

/mjt

#12 lore raw

21.09.2023 21:21, Michael Tokarev wrote:
..

Quoted 2 lines

Is this stable-worthy (at least 1-3)?  From the bug description it smells like it should be in 8.1.x, or maybe whole series.

N/M, this whole patchset has been Cc'd qemu-stable already.

Thanks,

/mjt