Skip to contentSkip to conversation

block/file-posix: use unsigned int for zones consistently

PATCH In review Stefan Hajnoczi 2 participants 2 messages Apr 10, 2023 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 block/file-posix: use unsigned int for zones consistently

Avoid mixing int and unsigned int for zone index and count values. This eliminates the possibility of accidental negative write pointer array indices. It also makes code review easier because we don't need to worry about signed/unsigned comparisons.

In practice I don't think zoned devices are likely to exceed MAX_INT zones any time soon, so this is mostly a code cleanup.

Cc Sam Li <[email protected]>
Cc Dmitry Fomichev <[email protected]>
Cc Damien Le Moal <[email protected]>
---
 block/file-posix.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

This is a cleanup on top of "[PATCH v9 0/4] Add zone append write for zoned device".

Based-on: <[email protected]>

block/file-posix.c +7 -5
index 32b16bc4fb..77fbf9e33e 100644
@@ -1338,8 +1338,9 @@ static int get_zones_wp(BlockDriverState *bs, int fd, int64_t offset,
     size_t rep_size;
     uint64_t sector = offset >> BDRV_SECTOR_BITS;
     BlockZoneWps *wps = bs->wps;
-    int j = offset / bs->bl.zone_size;
-    int ret, n = 0, i = 0;
+    unsigned int j = offset / bs->bl.zone_size;
+    int ret;
+    unsigned int n = 0, i = 0;
     rep_size = sizeof(struct blk_zone_report) + nrz * sizeof(struct blk_zone);
     g_autofree struct blk_zone_report *rep = NULL;
 
@@ -2092,7 +2093,8 @@ static int handle_aiocb_zone_report(void *opaque)
     struct blk_zone *blkz;
     size_t rep_size;
     unsigned int nrz;
-    int ret, n = 0, i = 0;
+    int ret;
+    unsigned int n = 0, i = 0;
 
     nrz = *nr_zones;
     rep_size = sizeof(struct blk_zone_report) + nrz * sizeof(struct blk_zone);
@@ -3507,11 +3509,11 @@ static int coroutine_fn raw_co_zone_mgmt(BlockDriverState *bs, BlockZoneOp op,
             return ret;
         }
     } else if (zo == BLKRESETZONE) {
-        for (int j = 0; j < nrz; ++j) {
+        for (unsigned int j = 0; j < nrz; ++j) {
             wp[j] = offset + j * zone_size;
         }
     } else if (zo == BLKFINISHZONE) {
-        for (int j = 0; j < nrz; ++j) {
+        for (unsigned int j = 0; j < nrz; ++j) {
             /* The zoned device allows the last zone smaller that the
              * zone size. */
             wp[j] = MIN(offset + (j + 1) * zone_size, offset + len);
Signature · 1 line
2.39.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 13 lines · Stefan Hajnoczi <[email protected]> 于2023年4月10日周一 21:49写道:

Avoid mixing int and unsigned int for zone index and count values. This eliminates the possibility of accidental negative write pointer array indices. It also makes code review easier because we don't need to worry about signed/unsigned comparisons.

In practice I don't think zoned devices are likely to exceed MAX_INT zones any time soon, so this is mostly a code cleanup.

Cc Sam Li <[email protected]>
Cc Dmitry Fomichev <[email protected]>
Cc Damien Le Moal <[email protected]>
Quoted 8 lines
---
 block/file-posix.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

This is a cleanup on top of "[PATCH v9 0/4] Add zone append write for zoned device".

Based-on: <[email protected]>

Quoted 44 lines
block/file-posix.c +7 -6
index 32b16bc4fb..77fbf9e33e 100644
@@ -1338,8 +1338,9 @@ static int get_zones_wp(BlockDriverState *bs, int fd, int64_t offset,
     size_t rep_size;
     uint64_t sector = offset >> BDRV_SECTOR_BITS;
     BlockZoneWps *wps = bs->wps;
-    int j = offset / bs->bl.zone_size;
-    int ret, n = 0, i = 0;
+    unsigned int j = offset / bs->bl.zone_size;
+    int ret;
+    unsigned int n = 0, i = 0;
     rep_size = sizeof(struct blk_zone_report) + nrz * sizeof(struct blk_zone);
     g_autofree struct blk_zone_report *rep = NULL;
 
@@ -2092,7 +2093,8 @@ static int handle_aiocb_zone_report(void *opaque)
     struct blk_zone *blkz;
     size_t rep_size;
     unsigned int nrz;
-    int ret, n = 0, i = 0;
+    int ret;
+    unsigned int n = 0, i = 0;
 
     nrz = *nr_zones;
     rep_size = sizeof(struct blk_zone_report) + nrz * sizeof(struct blk_zone);
@@ -3507,11 +3509,11 @@ static int coroutine_fn raw_co_zone_mgmt(BlockDriverState *bs, BlockZoneOp op,
             return ret;
         }
     } else if (zo == BLKRESETZONE) {
-        for (int j = 0; j < nrz; ++j) {
+        for (unsigned int j = 0; j < nrz; ++j) {
             wp[j] = offset + j * zone_size;
         }
     } else if (zo == BLKFINISHZONE) {
-        for (int j = 0; j < nrz; ++j) {
+        for (unsigned int j = 0; j < nrz; ++j) {
             /* The zoned device allows the last zone smaller that the
              * zone size. */
             wp[j] = MIN(offset + (j + 1) * zone_size, offset + len);
--

2.39.2