Skip to contentSkip to conversation

block: file-posix queue

PATCH Superseded Paolo Bonzini 6 participants 28 messages Jun 3, 2021 – Jun 17, 2021

Subsystems (2)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover block: file-posix queue

Hi Kevin,

this is a combination of two series that both affect host block device support in block/file-posix.c. Joelle's series is unchanged, while mine was adjusted according to your review of v2.

v1->v2: add missing patch

v2->v3: add max_hw_transfer to BlockLimits

Joelle van Dyne (3):
block: feature detection for host block support
block: check for sys/disk.h
block: detect DKIOCGETBLOCKCOUNT/SIZE before use

Paolo Bonzini (4):
file-posix: fix max_iov for /dev/sg devices
scsi-generic: pass max_segments via max_iov field in BlockLimits
block: add max_hw_transfer to BlockLimits
file-posix: try BLKSECTGET on block devices too, do not round to power
of 2

 block.c                        |   2 +-
 block/block-backend.c          |  12 ++++
 block/file-posix.c             | 104 ++++++++++++++++++++-------------
 block/io.c                     |   1 +
 hw/scsi/scsi-generic.c         |   6 +-
 include/block/block_int.h      |   7 +++
 include/sysemu/block-backend.h |   1 +
 meson.build                    |   7 ++-
 qapi/block-core.json           |  10 +++-
 9 files changed, 102 insertions(+), 48 deletions(-)
Signature · 1 line
2.31.1
#1 lore raw

PATCH v3 1/7 file-posix: fix max_iov for /dev/sg devices

Even though it was only called for devices that have bs->sg set (which must be character devices),
sg_get_max_segments looked at /sys/dev/block which only works for block devices.

On Linux the sg driver has its own way to provide the maximum number of iovecs in a scatter/gather list.

---
 block/file-posix.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
block/file-posix.c +11 -0
index f37dfc10b3..58db526cc2 100644
@@ -1180,6 +1180,17 @@ static int sg_get_max_segments(int fd)
         goto out;
     }
 
+    if (S_ISCHR(st->st_mode)) {
+        if (ioctl(fd, SG_GET_SG_TABLESIZE, &ret) == 0) {
+            return ret;
+        }
+        return -EIO;
+    }
+
+    if (!S_ISBLK(st->st_mode)) {
+        return -ENOTSUP;
+    }
+
     sysfspath = g_strdup_printf("/sys/dev/block/%u:%u/queue/max_segments",
                                 major(st.st_rdev), minor(st.st_rdev));
     sysfd = open(sysfspath, O_RDONLY);
Signature · 1 line
2.31.1
#2 lore raw

Re: [PATCH v3 1/7] file-posix: fix max_iov for /dev/sg devices

Quoted 18 lines · Paolo Bonzini

Even though it was only called for devices that have bs->sg set (which must be character devices),
sg_get_max_segments looked at /sys/dev/block which only works for block devices.

On Linux the sg driver has its own way to provide the maximum number of iovecs in a scatter/gather list.

---
  block/file-posix.c | 11 +++++++++++
  1 file changed, 11 insertions(+)
block/file-posix.c +0 -0
index f37dfc10b3..58db526cc2 100644

block/file-posix.c static int sg_get_max_segments(int fd) · line 1185

Quoted 6 lines
block/file-posix.c +3 -0
@@ -1180,6 +1180,17 @@ static int sg_get_max_segments(int fd)
          goto out;
      }
 
+    if (S_ISCHR(st->st_mode)) {
+        if (ioctl(fd, SG_GET_SG_TABLESIZE, &ret) == 0) {
+            return ret;

So, this is a new most-probable path for char-dev, yes?

Is it better do it at start of the function to avoid extra fstat() ?

Quoted 12 lines
block/file-posix.c +8 -0
+        }
+        return -EIO;
+    }
+
+    if (!S_ISBLK(st->st_mode)) {
+        return -ENOTSUP;
+    }
+
      sysfspath = g_strdup_printf("/sys/dev/block/%u:%u/queue/max_segments",
                                  major(st.st_rdev), minor(st.st_rdev));
      sysfd = open(sysfspath, O_RDONLY);
Signature · 2 lines
Best regards,
Vladimir
#3 lore raw

Re: [PATCH v3 1/7] file-posix: fix max_iov for /dev/sg devices

Quoted 27 lines · Vladimir Sementsov-Ogievskiy
Quoted 18 lines · Paolo Bonzini

Even though it was only called for devices that have bs->sg set (which must be character devices),
sg_get_max_segments looked at /sys/dev/block which only works for block devices.

On Linux the sg driver has its own way to provide the maximum number of iovecs in a scatter/gather list.

---
  block/file-posix.c | 11 +++++++++++

  1 file changed, 11 insertions(+)
block/file-posix.c +0 -0
index f37dfc10b3..58db526cc2 100644

block/file-posix.c static int sg_get_max_segments(int fd) · line 1184

Quoted 5 lines
block/file-posix.c +3 -0
@@ -1180,6 +1180,17 @@ static int sg_get_max_segments(int fd)
           goto out;
       }
+    if (S_ISCHR(st->st_mode)) {
+        if (ioctl(fd, SG_GET_SG_TABLESIZE, &ret) == 0) {
+            return ret;

So, this is a new most-probable path for char-dev, yes?

Quoted 1 line

Is it better do it at start of the function to avoid extra fstat() ?

Haha, stupid question, sorry my morning sleepy eyes. fstat is used for outer if condition.

Quoted 15 lines
Quoted 13 lines
block/file-posix.c +8 -0
+        }
+        return -EIO;
+    }
+
+    if (!S_ISBLK(st->st_mode)) {
+        return -ENOTSUP;
+    }
+
       sysfspath = g_strdup_printf("/sys/dev/block/%u:%u/queue/max_segments",
                                   major(st.st_rdev), minor(st.st_rdev));
       sysfd = open(sysfspath, O_RDONLY);
 
Signature · 2 lines
Best regards,
Vladimir
#4 lore raw

Re: [PATCH v3 1/7] file-posix: fix max_iov for /dev/sg devices

In reply to Paolo Bonzini
Quoted 4 lines · Paolo Bonzini

Even though it was only called for devices that have bs->sg set (which must be character devices),
sg_get_max_segments looked at /sys/dev/block which only works for block devices.

I assume, you keep /sys/dev/block code branch here only for following converting of the function to be hdev_get_max_hw_transfer()? Worth mentioning here?

Quoted 14 lines

On Linux the sg driver has its own way to provide the maximum number of iovecs in a scatter/gather list.

---
  block/file-posix.c | 11 +++++++++++
  1 file changed, 11 insertions(+)
block/file-posix.c +0 -0
index f37dfc10b3..58db526cc2 100644

block/file-posix.c static int sg_get_max_segments(int fd) · line 1183

Quoted 4 lines
block/file-posix.c +1 -0
@@ -1180,6 +1180,17 @@ static int sg_get_max_segments(int fd)
          goto out;
      }
 
+    if (S_ISCHR(st->st_mode)) {

Should it be instead "if (bs->sg) {" ? As SG_GET_SG_TABLESIZE looks like sg-specific, not for any chardev.

Also, st is not a pointer, so here and in the next if condition it should be s/st->/st./

Quoted 14 lines
block/file-posix.c +10 -0
+        if (ioctl(fd, SG_GET_SG_TABLESIZE, &ret) == 0) {
+            return ret;
+        }
+        return -EIO;
+    }
+
+    if (!S_ISBLK(st->st_mode)) {
+        return -ENOTSUP;
+    }
+
      sysfspath = g_strdup_printf("/sys/dev/block/%u:%u/queue/max_segments",
                                  major(st.st_rdev), minor(st.st_rdev));
      sysfd = open(sysfspath, O_RDONLY);
Signature · 2 lines
Best regards,
Vladimir
Reviewed-by on v3 1/7
#5 lore raw

Re: [PATCH v3 1/7] file-posix: fix max_iov for /dev/sg devices

In reply to Paolo Bonzini
Quoted 22 lines · Paolo Bonzini

Even though it was only called for devices that have bs->sg set (which must be character devices),
sg_get_max_segments looked at /sys/dev/block which only works for block devices.

On Linux the sg driver has its own way to provide the maximum number of iovecs in a scatter/gather list.

---
  block/file-posix.c | 11 +++++++++++
  1 file changed, 11 insertions(+)
block/file-posix.c +1 -0
index f37dfc10b3..58db526cc2 100644
@@ -1180,6 +1180,17 @@ static int sg_get_max_segments(int fd)
          goto out;
      }
 
+    if (S_ISCHR(st->st_mode)) {

block/file-posix.c static int sg_get_max_segments(int fd) · line 1193

Quoted 10 lines
block/file-posix.c +10 -0
+        if (ioctl(fd, SG_GET_SG_TABLESIZE, &ret) == 0) {
+            return ret;
+        }
+        return -EIO;
+    }
+
+    if (!S_ISBLK(st->st_mode)) {
+        return -ENOTSUP;
+    }
+

With %s/->/./:

(To answer Vladimir’s question, I don’t believe the condition should be bs->sg, because bs->sg == true is a given for this function anyway. Therefore, there’s no need to check whether the char device is an SG device.)

#6 lore raw

PATCH v3 2/7 scsi-generic: pass max_segments via max_iov field in BlockLimits

In reply to Paolo Bonzini

I/O to a disk via read/write is not limited by the number of segments allowed by the host adapter; the kernel can split requests if needed, and the limit imposed by the host adapter can be very low (256k or so) to avoid that SG_IO returns EINVAL if memory is heavily fragmented.

Since this value is only interesting for SG_IO-based I/O, do not include it in the max_transfer and only take it into account when patching the block limits VPD page in the scsi-generic device.

---
 block/file-posix.c     | 3 +--
 hw/scsi/scsi-generic.c | 6 ++++--
 2 files changed, 5 insertions(+), 4 deletions(-)
block/file-posix.c +1 -2
index 58db526cc2..e3241a0dd3 100644
@@ -1239,8 +1239,7 @@ static void raw_refresh_limits(BlockDriverState *bs, Error **errp)
 
         ret = sg_get_max_segments(s->fd);
         if (ret > 0) {
-            bs->bl.max_transfer = MIN(bs->bl.max_transfer,
-                                      ret * qemu_real_host_page_size);
+            bs->bl.max_iov = ret;
         }
     }
 
hw/scsi/scsi-generic.c +4 -2
index 98c30c5d5c..82e1e2ee79 100644
@@ -179,10 +179,12 @@ static void scsi_handle_inquiry_reply(SCSIGenericReq *r, SCSIDevice *s)
         (r->req.cmd.buf[1] & 0x01)) {
         page = r->req.cmd.buf[2];
         if (page == 0xb0) {
-            uint32_t max_transfer =
-                blk_get_max_transfer(s->conf.blk) / s->blocksize;
+            uint32_t max_transfer = blk_get_max_transfer(s->conf.blk);
+            uint32_t max_iov = blk_get_max_iov(s->conf.blk);
 
             assert(max_transfer);
+            max_transfer = MIN_NON_ZERO(max_transfer, max_iov * qemu_real_host_page_size)
+                / s->blocksize;
             stl_be_p(&r->buf[8], max_transfer);
             /* Also take care of the opt xfer len. */
             stl_be_p(&r->buf[12],
Signature · 1 line
2.31.1
Reviewed-by on v3 2/7
#7 lore raw

Re: [PATCH v3 2/7] scsi-generic: pass max_segments via max_iov field in BlockLimits

Quoted 9 lines · Paolo Bonzini

I/O to a disk via read/write is not limited by the number of segments allowed by the host adapter; the kernel can split requests if needed, and the limit imposed by the host adapter can be very low (256k or so) to avoid that SG_IO returns EINVAL if memory is heavily fragmented.

Since this value is only interesting for SG_IO-based I/O, do not include it in the max_transfer and only take it into account when patching the block limits VPD page in the scsi-generic device.

Quoted 5 lines
---
  block/file-posix.c     | 3 +--
  hw/scsi/scsi-generic.c | 6 ++++--
  2 files changed, 5 insertions(+), 4 deletions(-)
#8 lore raw

Re: [PATCH v3 2/7] scsi-generic: pass max_segments via max_iov field in BlockLimits

In reply to Paolo Bonzini
Quoted 35 lines · Paolo Bonzini

I/O to a disk via read/write is not limited by the number of segments allowed by the host adapter; the kernel can split requests if needed, and the limit imposed by the host adapter can be very low (256k or so) to avoid that SG_IO returns EINVAL if memory is heavily fragmented.

Since this value is only interesting for SG_IO-based I/O, do not include it in the max_transfer and only take it into account when patching the block limits VPD page in the scsi-generic device.

---
  block/file-posix.c     | 3 +--
  hw/scsi/scsi-generic.c | 6 ++++--
  2 files changed, 5 insertions(+), 4 deletions(-)
block/file-posix.c +1 -2
index 58db526cc2..e3241a0dd3 100644
@@ -1239,8 +1239,7 @@ static void raw_refresh_limits(BlockDriverState *bs, Error **errp)
 
          ret = sg_get_max_segments(s->fd);
          if (ret > 0) {
-            bs->bl.max_transfer = MIN(bs->bl.max_transfer,
-                                      ret * qemu_real_host_page_size);
+            bs->bl.max_iov = ret;
          }
      }
 
hw/scsi/scsi-generic.c +0 -0
index 98c30c5d5c..82e1e2ee79 100644
@@ -179,10 +179,12 @@ static void scsi_handle_inquiry_reply(SCSIGenericReq *r, SCSIDevice *s)
          (r->req.cmd.buf[1] & 0x01)) {

hw/scsi/scsi-generic.c static void scsi_handle_inquiry_reply(SCSIGenericReq *r, SCSIDevice *s) · line 187

Quoted 10 lines
hw/scsi/scsi-generic.c +4 -2
          page = r->req.cmd.buf[2];
          if (page == 0xb0) {
-            uint32_t max_transfer =
-                blk_get_max_transfer(s->conf.blk) / s->blocksize;
+            uint32_t max_transfer = blk_get_max_transfer(s->conf.blk);
+            uint32_t max_iov = blk_get_max_iov(s->conf.blk);
 
              assert(max_transfer);
+            max_transfer = MIN_NON_ZERO(max_transfer, max_iov * qemu_real_host_page_size)
+                / s->blocksize;

Now that I ran checkpatch for patch 3, I saw that it complains about this line being longer than 80 characters. I think it could be split so it doesn’t exceed that limit. It looks a bit like you intentionally exceeded the warning limit, but didn’t exceed the error limit (of 90). Is that so?

Max

#9 lore raw

Re: [PATCH v3 2/7] scsi-generic: pass max_segments via max_iov field in BlockLimits

Yes, I try to avoid the 90 character limit (though sometimes that's hard too, hello QOM) but I don't think much of the lower one.

Paolo

Il mar 15 giu 2021, 18:20 Max Reitz <[email protected]> ha scritto:

Quoted 62 lines · Paolo Bonzini
Quoted 1 line

I/O to a disk via read/write is not limited by the number of segments

allowed

Quoted 1 line

by the host adapter; the kernel can split requests if needed, and the

limit

Quoted 1 line

imposed by the host adapter can be very low (256k or so) to avoid that

SG_IO

block/file-posix.c static void raw_refresh_limits(BlockDriverState

Quoted 17 lines

returns EINVAL if memory is heavily fragmented.

Since this value is only interesting for SG_IO-based I/O, do not include it in the max_transfer and only take it into account when patching the block limits VPD page in the scsi-generic device.

---
  block/file-posix.c     | 3 +--
  hw/scsi/scsi-generic.c | 6 ++++--
  2 files changed, 5 insertions(+), 4 deletions(-)
block/file-posix.c +0 -0
index 58db526cc2..e3241a0dd3 100644
@@ -1239,8 +1239,7 @@ static void raw_refresh_limits(BlockDriverState

*bs, Error **errp)

hw/scsi/scsi-generic.c static void

Quoted 14 lines
block/file-posix.c +1 -2
          ret = sg_get_max_segments(s->fd);
          if (ret > 0) {
-            bs->bl.max_transfer = MIN(bs->bl.max_transfer,
-                                      ret * qemu_real_host_page_size);
+            bs->bl.max_iov = ret;
          }
      }
 
hw/scsi/scsi-generic.c +0 -0
index 98c30c5d5c..82e1e2ee79 100644
@@ -179,10 +179,12 @@ static void

scsi_handle_inquiry_reply(SCSIGenericReq *r, SCSIDevice *s)

hw/scsi/scsi-generic.c static void scsi_handle_inquiry_reply(SCSIGenericReq *r, SCSIDevice *s)

Quoted 10 lines
hw/scsi/scsi-generic.c +3 -2
          (r->req.cmd.buf[1] & 0x01)) {
          page = r->req.cmd.buf[2];
          if (page == 0xb0) {
-            uint32_t max_transfer =
-                blk_get_max_transfer(s->conf.blk) / s->blocksize;
+            uint32_t max_transfer = blk_get_max_transfer(s->conf.blk);
+            uint32_t max_iov = blk_get_max_iov(s->conf.blk);
 
              assert(max_transfer);
+            max_transfer = MIN_NON_ZERO(max_transfer, max_iov *

qemu_real_host_page_size)

hw/scsi/scsi-generic.c static void scsi_handle_inquiry_reply(SCSIGenericReq *r, SCSIDevice *s) · line 187

Quoted 1 line
hw/scsi/scsi-generic.c +1 -0
+                / s->blocksize;

Now that I ran checkpatch for patch 3, I saw that it complains about this line being longer than 80 characters. I think it could be split so it doesn’t exceed that limit. It looks a bit like you intentionally exceeded the warning limit, but didn’t exceed the error limit (of 90). Is that so?

Max

#10 lore raw

PATCH v3 3/7 block: add max_hw_transfer to BlockLimits

In reply to Paolo Bonzini

For block host devices, I/O can happen through either the kernel file descriptor I/O system calls (preadv/pwritev, io_submit, io_uring) or the SCSI passthrough ioctl SG_IO.

In the latter case, the size of each transfer can be limited by the HBA, while for file descriptor I/O the kernel is able to split and merge I/O in smaller pieces as needed. Applying the HBA limits to file descriptor I/O results in more system calls and suboptimal performance, so this patch splits the max_transfer limit in two:
max_transfer remains valid and is used in general, while max_hw_transfer is limited to the maximum hardware size. max_hw_transfer can then be included by the scsi-generic driver in the block limits page, to ensure that the stricter hardware limit is used.

---
 block/block-backend.c          | 12 ++++++++++++
 block/file-posix.c             |  2 +-
 block/io.c                     |  1 +
 hw/scsi/scsi-generic.c         |  2 +-
 include/block/block_int.h      |  7 +++++++
 include/sysemu/block-backend.h |  1 +
 6 files changed, 23 insertions(+), 2 deletions(-)
block/block-backend.c +12 -0
index 15f1ea4288..2ea1412a54 100644
@@ -1953,6 +1953,18 @@ uint32_t blk_get_request_alignment(BlockBackend *blk)
     return bs ? bs->bl.request_alignment : BDRV_SECTOR_SIZE;
 }
 
+/* Returns the maximum hardware transfer length, in bytes; guaranteed nonzero */
+uint64_t blk_get_max_hw_transfer(BlockBackend *blk)
+{
+    BlockDriverState *bs = blk_bs(blk);
+    uint64_t max = INT_MAX;
+
+    if (bs) {
+        max = MIN_NON_ZERO(bs->bl.max_hw_transfer, bs->bl.max_transfer);
+    }
+    return max;
+}
+
 /* Returns the maximum transfer length, in bytes; guaranteed nonzero */
 uint32_t blk_get_max_transfer(BlockBackend *blk)
 {
block/file-posix.c +1 -1
index e3241a0dd3..f55f92d0f5 100644
@@ -1234,7 +1234,7 @@ static void raw_refresh_limits(BlockDriverState *bs, Error **errp)
         int ret = sg_get_max_transfer_length(s->fd);
 
         if (ret > 0 && ret <= BDRV_REQUEST_MAX_BYTES) {
-            bs->bl.max_transfer = pow2floor(ret);
+            bs->bl.max_hw_transfer = pow2floor(ret);
         }
 
         ret = sg_get_max_segments(s->fd);
block/io.c +1 -0
index 323854d063..089b99bb0c 100644
@@ -127,6 +127,7 @@ static void bdrv_merge_limits(BlockLimits *dst, const BlockLimits *src)
 {
     dst->opt_transfer = MAX(dst->opt_transfer, src->opt_transfer);
     dst->max_transfer = MIN_NON_ZERO(dst->max_transfer, src->max_transfer);
+    dst->max_hw_transfer = MIN_NON_ZERO(dst->max_hw_transfer, src->max_hw_transfer);
     dst->opt_mem_alignment = MAX(dst->opt_mem_alignment,
                                  src->opt_mem_alignment);
     dst->min_mem_alignment = MAX(dst->min_mem_alignment,
hw/scsi/scsi-generic.c +1 -1
index 82e1e2ee79..3762dce749 100644
@@ -179,7 +179,7 @@ static void scsi_handle_inquiry_reply(SCSIGenericReq *r, SCSIDevice *s)
         (r->req.cmd.buf[1] & 0x01)) {
         page = r->req.cmd.buf[2];
         if (page == 0xb0) {
-            uint32_t max_transfer = blk_get_max_transfer(s->conf.blk);
+            uint64_t max_transfer = blk_get_max_hw_transfer(s->conf.blk);
             uint32_t max_iov = blk_get_max_iov(s->conf.blk);
 
             assert(max_transfer);
include/block/block_int.h +7 -0
index 057d88b1fc..f1a54db0f8 100644
@@ -695,6 +695,13 @@ typedef struct BlockLimits {
      * clamped down. */
     uint32_t max_transfer;
 
+    /* Maximal hardware transfer length in bytes.  Applies whenever
+     * transfers to the device bypass the kernel I/O scheduler, for
+     * example with SG_IO.  If larger than max_transfer or if zero,
+     * blk_get_max_hw_transfer will fall back to max_transfer.
+     */
+    uint64_t max_hw_transfer;
+
     /* memory alignment, in bytes so that no bounce buffer is needed */
     size_t min_mem_alignment;
 
include/sysemu/block-backend.h +1 -0
index 5423e3d9c6..9ac5f7bbd3 100644
@@ -208,6 +208,7 @@ void blk_eject(BlockBackend *blk, bool eject_flag);
 int blk_get_flags(BlockBackend *blk);
 uint32_t blk_get_request_alignment(BlockBackend *blk);
 uint32_t blk_get_max_transfer(BlockBackend *blk);
+uint64_t blk_get_max_hw_transfer(BlockBackend *blk);
 int blk_get_max_iov(BlockBackend *blk);
 void blk_set_guest_block_size(BlockBackend *blk, int align);
 void *blk_try_blockalign(BlockBackend *blk, size_t size);
Signature · 1 line
2.31.1
#11 lore raw

Re: [PATCH v3 3/7] block: add max_hw_transfer to BlockLimits

Quoted 15 lines · Paolo Bonzini

For block host devices, I/O can happen through either the kernel file descriptor I/O system calls (preadv/pwritev, io_submit, io_uring) or the SCSI passthrough ioctl SG_IO.

In the latter case, the size of each transfer can be limited by the HBA, while for file descriptor I/O the kernel is able to split and merge I/O in smaller pieces as needed. Applying the HBA limits to file descriptor I/O results in more system calls and suboptimal performance, so this patch splits the max_transfer limit in two:
max_transfer remains valid and is used in general, while max_hw_transfer is limited to the maximum hardware size. max_hw_transfer can then be included by the scsi-generic driver in the block limits page, to ensure that the stricter hardware limit is used.

block/block-backend.c uint32_t blk_get_request_alignment(BlockBackend *blk) · line 1960

Quoted 5 lines
block/block-backend.c +5 -0
+/* Returns the maximum hardware transfer length, in bytes; guaranteed nonzero */
+uint64_t blk_get_max_hw_transfer(BlockBackend *blk)
+{
+    BlockDriverState *bs = blk_bs(blk);
+    uint64_t max = INT_MAX;

This is an unaligned value; should we instead round it down to the request_alignment granularity?

Quoted 13 lines
block/block-backend.c +8 -0
+
+    if (bs) {
+        max = MIN_NON_ZERO(bs->bl.max_hw_transfer, bs->bl.max_transfer);
+    }
+    return max;
+}
+
 /* Returns the maximum transfer length, in bytes; guaranteed nonzero */
 uint32_t blk_get_max_transfer(BlockBackend *blk)
 {
 
+++ b/include/block/block_int.h

block/block-backend.c typedef struct BlockLimits { · line 698

Quoted 4 lines
block/block-backend.c +1 -0
@@ -695,6 +695,13 @@ typedef struct BlockLimits {
      * clamped down. */
     uint32_t max_transfer;
 
+    /* Maximal hardware transfer length in bytes.  Applies whenever

Leading /* on its own line, per our style.

include/block/block_int.h typedef struct BlockLimits { · line 702

Quoted 4 lines
include/block/block_int.h +4 -0
+     * transfers to the device bypass the kernel I/O scheduler, for
+     * example with SG_IO.  If larger than max_transfer or if zero,
+     * blk_get_max_hw_transfer will fall back to max_transfer.
+     */

Should we mandate any additional requirements on this value such as multiple of request_alignment or even power-of-2?

Quoted 5 lines
include/block/block_int.h +2 -0
+    uint64_t max_hw_transfer;
+
     /* memory alignment, in bytes so that no bounce buffer is needed */
     size_t min_mem_alignment;
 
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#12 lore raw

Re: [PATCH v3 3/7] block: add max_hw_transfer to BlockLimits

Quoted 8 lines · Eric Blake

block/block-backend.c uint32_t blk_get_request_alignment(BlockBackend *blk) · line 1960

Quoted 5 lines
block/block-backend.c +5 -0
+/* Returns the maximum hardware transfer length, in bytes; guaranteed nonzero */
+uint64_t blk_get_max_hw_transfer(BlockBackend *blk)
+{
+    BlockDriverState *bs = blk_bs(blk);
+    uint64_t max = INT_MAX;

This is an unaligned value; should we instead round it down to the request_alignment granularity?

See below...

Quoted 8 lines

include/block/block_int.h typedef struct BlockLimits { · line 698

Quoted 6 lines

+++ b/include/block/block_int.h

include/block/block_int.h +1 -0
@@ -695,6 +695,13 @@ typedef struct BlockLimits {
       * clamped down. */
      uint32_t max_transfer;
 
+    /* Maximal hardware transfer length in bytes.  Applies whenever

Leading /* on its own line, per our style.

The whole file still uses this style, I can change it if desired or do it later for the whole file or even the whole block subsystem.

Quoted 7 lines

include/block/block_int.h typedef struct BlockLimits { · line 702

Quoted 4 lines
include/block/block_int.h +4 -0
+     * transfers to the device bypass the kernel I/O scheduler, for
+     * example with SG_IO.  If larger than max_transfer or if zero,
+     * blk_get_max_hw_transfer will fall back to max_transfer.
+     */

Should we mandate any additional requirements on this value such as multiple of request_alignment or even power-of-2?

Certainly not power of 2. Multiple of request_alignment probably makes sense, but max_transfer doesn't have that limit.

Paolo

Quoted 6 lines
Quoted 5 lines
include/block/block_int.h +2 -0
+    uint64_t max_hw_transfer;
+
      /* memory alignment, in bytes so that no bounce buffer is needed */
      size_t min_mem_alignment;
 
#13 lore raw

Re: [PATCH v3 3/7] block: add max_hw_transfer to BlockLimits

In reply to Paolo Bonzini
Quoted 32 lines · Paolo Bonzini

For block host devices, I/O can happen through either the kernel file descriptor I/O system calls (preadv/pwritev, io_submit, io_uring) or the SCSI passthrough ioctl SG_IO.

In the latter case, the size of each transfer can be limited by the HBA, while for file descriptor I/O the kernel is able to split and merge I/O in smaller pieces as needed. Applying the HBA limits to file descriptor I/O results in more system calls and suboptimal performance, so this patch splits the max_transfer limit in two:
max_transfer remains valid and is used in general, while max_hw_transfer is limited to the maximum hardware size. max_hw_transfer can then be included by the scsi-generic driver in the block limits page, to ensure that the stricter hardware limit is used.

---
  block/block-backend.c          | 12 ++++++++++++
  block/file-posix.c             |  2 +-
  block/io.c                     |  1 +
  hw/scsi/scsi-generic.c         |  2 +-
  include/block/block_int.h      |  7 +++++++
  include/sysemu/block-backend.h |  1 +
  6 files changed, 23 insertions(+), 2 deletions(-)
block/block-backend.c +0 -0
index 15f1ea4288..2ea1412a54 100644
@@ -1953,6 +1953,18 @@ uint32_t blk_get_request_alignment(BlockBackend *blk)
      return bs ? bs->bl.request_alignment : BDRV_SECTOR_SIZE;
  }
 

block/block-backend.c uint32_t blk_get_request_alignment(BlockBackend *blk) · line 1965

Quoted 10 lines
block/block-backend.c +10 -0
+/* Returns the maximum hardware transfer length, in bytes; guaranteed nonzero */
+uint64_t blk_get_max_hw_transfer(BlockBackend *blk)
+{
+    BlockDriverState *bs = blk_bs(blk);
+    uint64_t max = INT_MAX;
+
+    if (bs) {
+        max = MIN_NON_ZERO(bs->bl.max_hw_transfer, bs->bl.max_transfer);
+    }
+    return max;

Both max_hw_transfer and max_transfer can be 0, so this could return 0, contrary to what the comment above promises.

Should max be initialized to 0 with a MIN_NON_ZERO(max, INT_MAX) here (like blk_get_max_transfer() does it)?

(As for the rest, I think aligning to the request alignment makes sense, but then again we don’t do that for max_transfer either, so... this at least wouldn’t be a new bug.

Regarding the comment, checkpatch complains about it, so it should be fixed so that /* is on its own line.

Speaking of checkpatch, now that I ran it, it also complains about the new line in bdrv_merge_limits() exceeding 80 characters, so that should be fixed, too.)

Max

#14 lore raw

Re: [PATCH v3 3/7] block: add max_hw_transfer to BlockLimits

Quoted 15 lines · Max Reitz

block/block-backend.c uint32_t blk_get_request_alignment(BlockBackend *blk) · line 1965

Quoted 12 lines

  }
+/* Returns the maximum hardware transfer length, in bytes; guaranteed nonzero */

block/block-backend.c +9 -0
+uint64_t blk_get_max_hw_transfer(BlockBackend *blk)
+{
+    BlockDriverState *bs = blk_bs(blk);
+    uint64_t max = INT_MAX;
+
+    if (bs) {
+        max = MIN_NON_ZERO(bs->bl.max_hw_transfer, bs->bl.max_transfer);
+    }
+    return max;

Both max_hw_transfer and max_transfer can be 0, so this could return 0, contrary to what the comment above promises.

Quoted 1 line

Should max be initialized to 0 with a MIN_NON_ZERO(max, INT_MAX) here (like blk_get_max_transfer() does it)?

Yes, something to that effect.

Quoted 1 line

(As for the rest, I think aligning to the request alignment makes sense, but then again we don’t do that for max_transfer either, so... this at least wouldn’t be a new bug.

Ok, will do. I will also add a new patch to align max_transfer to the request alignment.

Quoted 1 line

Regarding the comment, checkpatch complains about it, so it should be fixed so that /* is on its own line.

That makes it different from every other comment in block_int.h though.
Is it okay to fix all of them in a follow-up?

Paolo

Quoted 1 line

Speaking of checkpatch, now that I ran it, it also complains about the new line in bdrv_merge_limits() exceeding 80 characters, so that should be fixed, too.)

#15 lore raw

Re: [PATCH v3 3/7] block: add max_hw_transfer to BlockLimits

Quoted 21 lines · Paolo Bonzini
Quoted 16 lines · Max Reitz
Quoted 10 lines

  }
+/* Returns the maximum hardware transfer length, in bytes; guaranteed nonzero */

block/block-backend.c +7 -0
+uint64_t blk_get_max_hw_transfer(BlockBackend *blk)
+{
+    BlockDriverState *bs = blk_bs(blk);
+    uint64_t max = INT_MAX;
+
+    if (bs) {
+        max = MIN_NON_ZERO(bs->bl.max_hw_transfer, 
Quoted 3 lines

bs->bl.max_transfer);
+    }
+    return max;

Both max_hw_transfer and max_transfer can be 0, so this could return 0, contrary to what the comment above promises.

Quoted 1 line

Should max be initialized to 0 with a MIN_NON_ZERO(max, INT_MAX) here (like blk_get_max_transfer() does it)?

Yes, something to that effect.

Quoted 7 lines
Quoted 1 line

(As for the rest, I think aligning to the request alignment makes sense, but then again we don’t do that for max_transfer either, so... this at least wouldn’t be a new bug.

Ok, will do.  I will also add a new patch to align max_transfer to the request alignment.

Quoted 1 line

Regarding the comment, checkpatch complains about it, so it should be fixed so that /* is on its own line.

That makes it different from every other comment in block_int.h though.  Is it okay to fix all of them in a follow-up?

The reason it’s different is that the comment style in question was added to checkpatch only relatively recently. I can’t speak for others, but I’m a simple person. I just do what makes checkpatch happy. :)

Given that the checkpatch complaint is only a warning, I think it’s OK to keep the comment as it is here, and perhaps optionally fix all comments in block_int.h in a follow-up. I don’t think we need to fix existing comments, but, well, it wouldn’t be wrong.

Max

#16 lore raw

Re: [PATCH v3 3/7] block: add max_hw_transfer to BlockLimits

Quoted 1 line · Max Reitz

Given that the checkpatch complaint is only a warning, I think it’s OK to keep the comment as it is here, and perhaps optionally fix all comments in block_int.h in a follow-up. I don’t think we need to fix existing comments, but, well, it wouldn’t be wrong.

We don't need to, but sometimes the benefit (of not discussing checkpatch over and over) does seem to outweigh the churn. Thanks for the review!

Paolo

#17 lore raw

PATCH v3 4/7 file-posix: try BLKSECTGET on block devices too, do not round to power of 2

In reply to Paolo Bonzini

bs->sg is only true for character devices, but block devices can also be used with scsi-block and scsi-generic. Unfortunately BLKSECTGET returns bytes in an int for /dev/sgN devices, and sectors in a short for block devices, so account for that in the code.

The maximum transfer also need not be a power of 2 (for example I have seen disks with 1280 KiB maximum transfer) so there's no need to pass the result through pow2floor.

This patch reintroduces the logic that was removed in commit 867eccfed8 ("file-posix: Use max transfer length/segment count only for SCSI passthrough", 2019-07-12). The removal was motivated by the performance regression when using a block device's max_transfer with kernel I/O; the new, separate max_hw_transfer limit avoids reintroducing the same regression.

---
 block/file-posix.c | 40 ++++++++++++++++++++++------------------
 1 file changed, 22 insertions(+), 18 deletions(-)
block/file-posix.c +22 -18
index f55f92d0f5..1439293f63 100644
@@ -1149,22 +1149,27 @@ static void raw_reopen_abort(BDRVReopenState *state)
     s->reopen_state = NULL;
 }
 
-static int sg_get_max_transfer_length(int fd)
+static int hdev_get_max_hw_transfer(int fd, struct stat *st)
 {
 #ifdef BLKSECTGET
-    int max_bytes = 0;
-
-    if (ioctl(fd, BLKSECTGET, &max_bytes) == 0) {
-        return max_bytes;
+    if (S_ISBLK(st->st_mode)) {
+        unsigned short max_sectors = 0;
+        if (ioctl(fd, BLKSECTGET, &max_sectors) == 0) {
+            return max_sectors * 512;
+        }
     } else {
-        return -errno;
+        int max_bytes = 0;
+        if (ioctl(fd, BLKSECTGET, &max_bytes) == 0) {
+            return max_bytes;
+        }
     }
+    return -errno;
 #else
     return -ENOSYS;
 #endif
 }
 
-static int sg_get_max_segments(int fd)
+static int hdev_get_max_segments(int fd, struct stat *st)
 {
 #ifdef CONFIG_LINUX
     char buf[32];
@@ -1173,12 +1178,6 @@ static int sg_get_max_segments(int fd)
     int ret;
     int sysfd = -1;
     long max_segments;
-    struct stat st;
-
-    if (fstat(fd, &st)) {
-        ret = -errno;
-        goto out;
-    }
 
     if (S_ISCHR(st->st_mode)) {
         if (ioctl(fd, SG_GET_SG_TABLESIZE, &ret) == 0) {
@@ -1192,7 +1191,7 @@ static int sg_get_max_segments(int fd)
     }
 
     sysfspath = g_strdup_printf("/sys/dev/block/%u:%u/queue/max_segments",
-                                major(st.st_rdev), minor(st.st_rdev));
+                                major(st->st_rdev), minor(st->st_rdev));
     sysfd = open(sysfspath, O_RDONLY);
     if (sysfd == -1) {
         ret = -errno;
@@ -1229,15 +1228,20 @@ out:
 static void raw_refresh_limits(BlockDriverState *bs, Error **errp)
 {
     BDRVRawState *s = bs->opaque;
+    struct stat st;
+
+    if (fstat(s->fd, &st)) {
+        return;
+    }
 
-    if (bs->sg) {
-        int ret = sg_get_max_transfer_length(s->fd);
+    if (bs->sg || S_ISBLK(st.st_mode)) {
+        int ret = hdev_get_max_hw_transfer(s->fd, &st);
 
         if (ret > 0 && ret <= BDRV_REQUEST_MAX_BYTES) {
-            bs->bl.max_hw_transfer = pow2floor(ret);
+            bs->bl.max_hw_transfer = ret;
         }
 
-        ret = sg_get_max_segments(s->fd);
+        ret = hdev_get_max_segments(s->fd, &st);
         if (ret > 0) {
             bs->bl.max_iov = ret;
         }
Signature · 1 line
2.31.1
#18 lore raw

Re: [PATCH v3 4/7] file-posix: try BLKSECTGET on block devices too, do not round to power of 2

Quoted 9 lines · Paolo Bonzini

bs->sg is only true for character devices, but block devices can also be used with scsi-block and scsi-generic. Unfortunately BLKSECTGET returns bytes in an int for /dev/sgN devices, and sectors in a short for block devices, so account for that in the code.

The maximum transfer also need not be a power of 2 (for example I have seen disks with 1280 KiB maximum transfer) so there's no need to pass the result through pow2floor.

Quoted 6 lines

This patch reintroduces the logic that was removed in commit 867eccfed8 ("file-posix: Use max transfer length/segment count only for SCSI passthrough", 2019-07-12). The removal was motivated by the performance regression when using a block device's max_transfer with kernel I/O; the new, separate max_hw_transfer limit avoids reintroducing the same regression.

(And the fact that the result of hdev_get_max_segments() is no longer used to cap max_transfer, but is just stored in max_iov instead.)

Quoted 31 lines
---
  block/file-posix.c | 40 ++++++++++++++++++++++------------------
  1 file changed, 22 insertions(+), 18 deletions(-)
block/file-posix.c +9 -6
index f55f92d0f5..1439293f63 100644
@@ -1149,22 +1149,27 @@ static void raw_reopen_abort(BDRVReopenState *state)
      s->reopen_state = NULL;
  }
 
-static int sg_get_max_transfer_length(int fd)
+static int hdev_get_max_hw_transfer(int fd, struct stat *st)
  {
  #ifdef BLKSECTGET
-    int max_bytes = 0;
-
-    if (ioctl(fd, BLKSECTGET, &max_bytes) == 0) {
-        return max_bytes;
+    if (S_ISBLK(st->st_mode)) {
+        unsigned short max_sectors = 0;
+        if (ioctl(fd, BLKSECTGET, &max_sectors) == 0) {
+            return max_sectors * 512;
+        }
      } else {
-        return -errno;
+        int max_bytes = 0;
+        if (ioctl(fd, BLKSECTGET, &max_bytes) == 0) {
+            return max_bytes;

block/file-posix.c static void raw_reopen_abort(BDRVReopenState *state) · line 1172

Quoted 10 lines
block/file-posix.c +3 -1
+        }
      }
+    return -errno;
  #else
      return -ENOSYS;
  #endif
  }
 
-static int sg_get_max_segments(int fd)
+static int hdev_get_max_segments(int fd, struct stat *st)

OK, now I see why in patch 1 you treated st as a pointer. Still, needs to be st. in patch 1, and changed to st-> in this patch only.

Quoted 26 lines
block/file-posix.c +1 -7
  {
  #ifdef CONFIG_LINUX
      char buf[32];
@@ -1173,12 +1178,6 @@ static int sg_get_max_segments(int fd)
      int ret;
      int sysfd = -1;
      long max_segments;
-    struct stat st;
-
-    if (fstat(fd, &st)) {
-        ret = -errno;
-        goto out;
-    }
 
      if (S_ISCHR(st->st_mode)) {
          if (ioctl(fd, SG_GET_SG_TABLESIZE, &ret) == 0) {
@@ -1192,7 +1191,7 @@ static int sg_get_max_segments(int fd)
      }
 
      sysfspath = g_strdup_printf("/sys/dev/block/%u:%u/queue/max_segments",
-                                major(st.st_rdev), minor(st.st_rdev));
+                                major(st->st_rdev), minor(st->st_rdev));
      sysfd = open(sysfspath, O_RDONLY);
      if (sysfd == -1) {
          ret = -errno;

block/file-posix.c out: · line 1235

Quoted 8 lines
block/file-posix.c +5 -0
@@ -1229,15 +1228,20 @@ out:
  static void raw_refresh_limits(BlockDriverState *bs, Error **errp)
  {
      BDRVRawState *s = bs->opaque;
+    struct stat st;
+
+    if (fstat(s->fd, &st)) {
+        return;
+    }

First, I think if we ignore an error, there should be a comment explaining why that’s OK.

It is OK here because inquiring transfer limits is best-effort.

However, the alignment probes below the following block are completely independent of st. Therefore, I don’t think we should skip them if fstat() failed here; only the if (bs->sg || ...) block should be skipped.

Max

Quoted 16 lines
block/file-posix.c +4 -4
-    if (bs->sg) {
-        int ret = sg_get_max_transfer_length(s->fd);
+    if (bs->sg || S_ISBLK(st.st_mode)) {
+        int ret = hdev_get_max_hw_transfer(s->fd, &st);
 
          if (ret > 0 && ret <= BDRV_REQUEST_MAX_BYTES) {
-            bs->bl.max_hw_transfer = pow2floor(ret);
+            bs->bl.max_hw_transfer = ret;
          }
 
-        ret = sg_get_max_segments(s->fd);
+        ret = hdev_get_max_segments(s->fd, &st);
          if (ret > 0) {
              bs->bl.max_iov = ret;
          }
#19 lore raw

PATCH v3 5/7 block: feature detection for host block support

In reply to Paolo Bonzini

From: Joelle van Dyne <[email protected]>

On Darwin (iOS), there are no system level APIs for directly accessing host block devices. We detect this at configure time.

Message-Id: <[email protected]>

---
 block/file-posix.c   | 33 ++++++++++++++++++++++-----------
 meson.build          |  6 +++++-
 qapi/block-core.json | 10 +++++++---
 3 files changed, 34 insertions(+), 15 deletions(-)
block/file-posix.c +22 -11
index 1439293f63..5821e1afed 100644
@@ -42,6 +42,8 @@
 #include "scsi/constants.h"
 
 #if defined(__APPLE__) && (__MACH__)
+#include <sys/ioctl.h>
+#if defined(HAVE_HOST_BLOCK_DEVICE)
 #include <paths.h>
 #include <sys/param.h>
 #include <IOKit/IOKitLib.h>
@@ -52,6 +54,7 @@
 //#include <IOKit/storage/IOCDTypes.h>
 #include <IOKit/storage/IODVDMedia.h>
 #include <CoreFoundation/CoreFoundation.h>
+#endif /* defined(HAVE_HOST_BLOCK_DEVICE) */
 #endif
 
 #ifdef __sun__
@@ -180,7 +183,17 @@ typedef struct BDRVRawReopenState {
     bool check_cache_dropped;
 } BDRVRawReopenState;
 
-static int fd_open(BlockDriverState *bs);
+static int fd_open(BlockDriverState *bs)
+{
+    BDRVRawState *s = bs->opaque;
+
+    /* this is just to ensure s->fd is sane (its called by io ops) */
+    if (s->fd >= 0) {
+        return 0;
+    }
+    return -EIO;
+}
+
 static int64_t raw_getlength(BlockDriverState *bs);
 
 typedef struct RawPosixAIOData {
@@ -3028,6 +3041,7 @@ static BlockStatsSpecific *raw_get_specific_stats(BlockDriverState *bs)
     return stats;
 }
 
+#if defined(HAVE_HOST_BLOCK_DEVICE)
 static BlockStatsSpecific *hdev_get_specific_stats(BlockDriverState *bs)
 {
     BlockStatsSpecific *stats = g_new(BlockStatsSpecific, 1);
@@ -3037,6 +3051,7 @@ static BlockStatsSpecific *hdev_get_specific_stats(BlockDriverState *bs)
 
     return stats;
 }
+#endif /* HAVE_HOST_BLOCK_DEVICE */
 
 static QemuOptsList raw_create_opts = {
     .name = "raw-create-opts",
@@ -3252,6 +3267,8 @@ BlockDriver bdrv_file = {
 /***********************************************/
 /* host device */
 
+#if defined(HAVE_HOST_BLOCK_DEVICE)
+
 #if defined(__APPLE__) && defined(__MACH__)
 static kern_return_t GetBSDPath(io_iterator_t mediaIterator, char *bsdPath,
                                 CFIndex maxPathSize, int flags);
@@ -3544,16 +3561,6 @@ hdev_co_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
 }
 #endif /* linux */
 
-static int fd_open(BlockDriverState *bs)
-{
-    BDRVRawState *s = bs->opaque;
-
-    /* this is just to ensure s->fd is sane (its called by io ops) */
-    if (s->fd >= 0)
-        return 0;
-    return -EIO;
-}
-
 static coroutine_fn int
 hdev_co_pdiscard(BlockDriverState *bs, int64_t offset, int bytes)
 {
@@ -3877,6 +3884,8 @@ static BlockDriver bdrv_host_cdrom = {
 };
 #endif /* __FreeBSD__ */
 
+#endif /* HAVE_HOST_BLOCK_DEVICE */
+
 static void bdrv_file_init(void)
 {
     /*
@@ -3884,6 +3893,7 @@ static void bdrv_file_init(void)
      * registered last will get probed first.
      */
     bdrv_register(&bdrv_file);
+#if defined(HAVE_HOST_BLOCK_DEVICE)
     bdrv_register(&bdrv_host_device);
 #ifdef __linux__
     bdrv_register(&bdrv_host_cdrom);
@@ -3891,6 +3901,7 @@ static void bdrv_file_init(void)
 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
     bdrv_register(&bdrv_host_cdrom);
 #endif
+#endif /* HAVE_HOST_BLOCK_DEVICE */
 }
 
 block_init(bdrv_file_init);
meson.build +5 -1
index 913cf2a41a..9387fc5799 100644
@@ -183,7 +183,7 @@ if targetos == 'windows'
                                       include_directories: include_directories('.'))
 elif targetos == 'darwin'
   coref = dependency('appleframeworks', modules: 'CoreFoundation')
-  iokit = dependency('appleframeworks', modules: 'IOKit')
+  iokit = dependency('appleframeworks', modules: 'IOKit', required: false)
 elif targetos == 'sunos'
   socket = [cc.find_library('socket'),
             cc.find_library('nsl'),
@@ -1072,6 +1072,9 @@ if get_option('cfi')
   add_global_link_arguments(cfi_flags, native: false, language: ['c', 'cpp', 'objc'])
 endif
 
+have_host_block_device = (targetos != 'darwin' or
+    cc.has_header('IOKit/storage/IOMedia.h'))
+
 #################
 # config-host.h #
 #################
@@ -1165,6 +1168,7 @@ config_host_data.set('HAVE_PTY_H', cc.has_header('pty.h'))
 config_host_data.set('HAVE_SYS_IOCCOM_H', cc.has_header('sys/ioccom.h'))
 config_host_data.set('HAVE_SYS_KCOV_H', cc.has_header('sys/kcov.h'))
 config_host_data.set('HAVE_SYSTEM_FUNCTION', cc.has_function('system', prefix: '#include <stdlib.h>'))
+config_host_data.set('HAVE_HOST_BLOCK_DEVICE', have_host_block_device)
 
 config_host_data.set('CONFIG_PREADV', cc.has_function('preadv', prefix: '#include <sys/uio.h>'))
 
qapi/block-core.json +7 -3
index 2ea294129e..2dd41be156 100644
@@ -897,7 +897,8 @@
   'discriminator': 'driver',
   'data': {
       'file': 'BlockStatsSpecificFile',
-      'host_device': 'BlockStatsSpecificFile',
+      'host_device': { 'type': 'BlockStatsSpecificFile',
+                       'if': 'defined(HAVE_HOST_BLOCK_DEVICE)' },
       'nvme': 'BlockStatsSpecificNvme' } }
 
 ##
@@ -2814,7 +2815,9 @@
 { 'enum': 'BlockdevDriver',
   'data': [ 'blkdebug', 'blklogwrites', 'blkreplay', 'blkverify', 'bochs',
             'cloop', 'compress', 'copy-on-read', 'dmg', 'file', 'ftp', 'ftps',
-            'gluster', 'host_cdrom', 'host_device', 'http', 'https', 'iscsi',
+            'gluster', 'host_cdrom',
+            {'name': 'host_device', 'if': 'defined(HAVE_HOST_BLOCK_DEVICE)' },
+            'http', 'https', 'iscsi',
             'luks', 'nbd', 'nfs', 'null-aio', 'null-co', 'nvme', 'parallels',
             'preallocate', 'qcow', 'qcow2', 'qed', 'quorum', 'raw', 'rbd',
             { 'name': 'replication', 'if': 'defined(CONFIG_REPLICATION)' },
@@ -3996,7 +3999,8 @@
       'ftps':       'BlockdevOptionsCurlFtps',
       'gluster':    'BlockdevOptionsGluster',
       'host_cdrom': 'BlockdevOptionsFile',
-      'host_device':'BlockdevOptionsFile',
+      'host_device': { 'type': 'BlockdevOptionsFile',
+                       'if': 'defined(HAVE_HOST_BLOCK_DEVICE)' },
       'http':       'BlockdevOptionsCurlHttp',
       'https':      'BlockdevOptionsCurlHttps',
       'iscsi':      'BlockdevOptionsIscsi',
Signature · 1 line
2.31.1
#20 lore raw

Re: [PATCH v3 5/7] block: feature detection for host block support

Quoted 13 lines · Paolo Bonzini

From: Joelle van Dyne <[email protected]>

On Darwin (iOS), there are no system level APIs for directly accessing host block devices. We detect this at configure time.

Message-Id: <[email protected]>

---
  block/file-posix.c   | 33 ++++++++++++++++++++++-----------
  meson.build          |  6 +++++-
  qapi/block-core.json | 10 +++++++---
  3 files changed, 34 insertions(+), 15 deletions(-)

[...]

Quoted 15 lines
qapi/block-core.json +2 -1
index 2ea294129e..2dd41be156 100644
@@ -897,7 +897,8 @@
    'discriminator': 'driver',
    'data': {
        'file': 'BlockStatsSpecificFile',
-      'host_device': 'BlockStatsSpecificFile',
+      'host_device': { 'type': 'BlockStatsSpecificFile',
+                       'if': 'defined(HAVE_HOST_BLOCK_DEVICE)' },
        'nvme': 'BlockStatsSpecificNvme' } }
 
  ##

qapi/block-core.json line 2819

Quoted 6 lines
qapi/block-core.json +2 -1
@@ -2814,7 +2815,9 @@
  { 'enum': 'BlockdevDriver',
    'data': [ 'blkdebug', 'blklogwrites', 'blkreplay', 'blkverify', 'bochs',
              'cloop', 'compress', 'copy-on-read', 'dmg', 'file', 'ftp', 'ftps',
-            'gluster', 'host_cdrom', 'host_device', 'http', 'https', 'iscsi',
+            'gluster', 'host_cdrom',
+            {'name': 'host_device', 'if': 'defined(HAVE_HOST_BLOCK_DEVICE)' },

Shouldn’t this be done for host_cdrom, too? (and below)

Apart from that, looks good to me.

Quoted 14 lines
qapi/block-core.json +3 -1
+            'http', 'https', 'iscsi',
              'luks', 'nbd', 'nfs', 'null-aio', 'null-co', 'nvme', 'parallels',
              'preallocate', 'qcow', 'qcow2', 'qed', 'quorum', 'raw', 'rbd',
              { 'name': 'replication', 'if': 'defined(CONFIG_REPLICATION)' },
@@ -3996,7 +3999,8 @@
        'ftps':       'BlockdevOptionsCurlFtps',
        'gluster':    'BlockdevOptionsGluster',
        'host_cdrom': 'BlockdevOptionsFile',
-      'host_device':'BlockdevOptionsFile',
+      'host_device': { 'type': 'BlockdevOptionsFile',
+                       'if': 'defined(HAVE_HOST_BLOCK_DEVICE)' },
        'http':       'BlockdevOptionsCurlHttp',
        'https':      'BlockdevOptionsCurlHttps',
        'iscsi':      'BlockdevOptionsIscsi',
#21 lore raw

PATCH v3 6/7 block: check for sys/disk.h

In reply to Paolo Bonzini

From: Joelle van Dyne <[email protected]>

Some BSD platforms do not have this header.

Message-Id: <[email protected]>

---
 block.c     | 2 +-
 meson.build | 1 +
 2 files changed, 2 insertions(+), 1 deletion(-)
block.c +1 -1
index 3f456892d0..1d37f133a8 100644
@@ -54,7 +54,7 @@
 #ifdef CONFIG_BSD
 #include <sys/ioctl.h>
 #include <sys/queue.h>
-#ifndef __DragonFly__
+#if defined(HAVE_SYS_DISK_H)
 #include <sys/disk.h>
 #endif
 #endif
meson.build +1 -0
index 9387fc5799..8c44c37f9a 100644
@@ -1169,6 +1169,7 @@ config_host_data.set('HAVE_SYS_IOCCOM_H', cc.has_header('sys/ioccom.h'))
 config_host_data.set('HAVE_SYS_KCOV_H', cc.has_header('sys/kcov.h'))
 config_host_data.set('HAVE_SYSTEM_FUNCTION', cc.has_function('system', prefix: '#include <stdlib.h>'))
 config_host_data.set('HAVE_HOST_BLOCK_DEVICE', have_host_block_device)
+config_host_data.set('HAVE_SYS_DISK_H', cc.has_header('sys/disk.h'))
 
 config_host_data.set('CONFIG_PREADV', cc.has_function('preadv', prefix: '#include <sys/uio.h>'))
 
Signature · 1 line
2.31.1
Reviewed-by on v3 6/7
#22 lore raw

Re: [PATCH v3 6/7] block: check for sys/disk.h

Quoted 7 lines · Paolo Bonzini

From: Joelle van Dyne <[email protected]>

Some BSD platforms do not have this header.

Quoted 6 lines

Message-Id: <[email protected]>

---
  block.c     | 2 +-
  meson.build | 1 +
  2 files changed, 2 insertions(+), 1 deletion(-)
#23 lore raw

PATCH v3 7/7 block: detect DKIOCGETBLOCKCOUNT/SIZE before use

In reply to Paolo Bonzini

From: Joelle van Dyne <[email protected]>

iOS hosts do not have these defined so we fallback to the default behaviour.

Message-Id: <[email protected]>

---
 block/file-posix.c | 21 +++++++++------------
 1 file changed, 9 insertions(+), 12 deletions(-)
block/file-posix.c +9 -12
index 5821e1afed..4e2f7cf508 100644
@@ -2322,8 +2322,11 @@ static int64_t raw_getlength(BlockDriverState *bs)
 again:
 #endif
     if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
+        size = 0;
 #ifdef DIOCGMEDIASIZE
-        if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
+        if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size)) {
+            size = 0;
+        }
 #elif defined(DIOCGPART)
         {
                 struct partinfo pi;
@@ -2332,9 +2335,7 @@ again:
                 else
                         size = 0;
         }
-        if (size == 0)
-#endif
-#if defined(__APPLE__) && defined(__MACH__)
+#elif defined(DKIOCGETBLOCKCOUNT) && defined(DKIOCGETBLOCKSIZE)
         {
             uint64_t sectors = 0;
             uint32_t sector_size = 0;
@@ -2342,19 +2343,15 @@ again:
             if (ioctl(fd, DKIOCGETBLOCKCOUNT, &sectors) == 0
                && ioctl(fd, DKIOCGETBLOCKSIZE, &sector_size) == 0) {
                 size = sectors * sector_size;
-            } else {
-                size = lseek(fd, 0LL, SEEK_END);
-                if (size < 0) {
-                    return -errno;
-                }
             }
         }
-#else
-        size = lseek(fd, 0LL, SEEK_END);
+#endif
+        if (size == 0) {
+            size = lseek(fd, 0LL, SEEK_END);
+        }
         if (size < 0) {
             return -errno;
         }
-#endif
 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
         switch(s->type) {
         case FTYPE_CD:
Signature · 1 line
2.31.1
#24 lore raw

Re: [PATCH v3 7/7] block: detect DKIOCGETBLOCKCOUNT/SIZE before use

Quoted 19 lines · Paolo Bonzini

From: Joelle van Dyne <[email protected]>

iOS hosts do not have these defined so we fallback to the default behaviour.

Message-Id: <[email protected]>

---
  block/file-posix.c | 21 +++++++++------------
  1 file changed, 9 insertions(+), 12 deletions(-)
block/file-posix.c +0 -0
index 5821e1afed..4e2f7cf508 100644

block/file-posix.c static int64_t raw_getlength(BlockDriverState *bs) · line 2327

Quoted 7 lines
block/file-posix.c +2 -1
@@ -2322,8 +2322,11 @@ static int64_t raw_getlength(BlockDriverState *bs)
  again:
  #endif
      if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
+        size = 0;
  #ifdef DIOCGMEDIASIZE
-        if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
+        if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size)) {

Pre-existing, but I feel compelled to express my unease about this cast.

block/file-posix.c again: · line 2338

Quoted 13 lines
block/file-posix.c +3 -3
+            size = 0;
+        }
  #elif defined(DIOCGPART)
          {
                  struct partinfo pi;
@@ -2332,9 +2335,7 @@ again:
                  else
                          size = 0;
          }
-        if (size == 0)
-#endif
-#if defined(__APPLE__) && defined(__MACH__)
+#elif defined(DKIOCGETBLOCKCOUNT) && defined(DKIOCGETBLOCKSIZE)

As far a I can tell, before this patch, if the DIOCGMEDIASIZE ioctl failed, we fell back to this DKIOCGETBLOCKCOUNT/SIZE block (if compiled in). Now this is an #elif and so will not be used if DIOCGMEDIASIZE was defined. Is that intentional?

This may be fine, and apart from that, this patch looks good to me, but this change in behavior wasn’t mentioned in the commit message, hence me asking.

Quoted 27 lines
block/file-posix.c +4 -8
          {
              uint64_t sectors = 0;
              uint32_t sector_size = 0;
@@ -2342,19 +2343,15 @@ again:
              if (ioctl(fd, DKIOCGETBLOCKCOUNT, &sectors) == 0
                 && ioctl(fd, DKIOCGETBLOCKSIZE, &sector_size) == 0) {
                  size = sectors * sector_size;
-            } else {
-                size = lseek(fd, 0LL, SEEK_END);
-                if (size < 0) {
-                    return -errno;
-                }
              }
          }
-#else
-        size = lseek(fd, 0LL, SEEK_END);
+#endif
+        if (size == 0) {
+            size = lseek(fd, 0LL, SEEK_END);
+        }
          if (size < 0) {
              return -errno;
          }
-#endif
  #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
          switch(s->type) {
          case FTYPE_CD:
#25 lore raw

Re: [PATCH v3 7/7] block: detect DKIOCGETBLOCKCOUNT/SIZE before use

Quoted 15 lines · Max Reitz
Quoted 14 lines · Paolo Bonzini

From: Joelle van Dyne <[email protected]>

iOS hosts do not have these defined so we fallback to the default behaviour.

Message-Id: <[email protected]>

---
  block/file-posix.c | 21 +++++++++------------
  1 file changed, 9 insertions(+), 12 deletions(-)
Quoted 14 lines

block/file-posix.c static int64_t raw_getlength(BlockDriverState *bs) · line 2327

Quoted 12 lines
block/file-posix.c +2 -1
index 5821e1afed..4e2f7cf508 100644
@@ -2322,8 +2322,11 @@ static int64_t raw_getlength(BlockDriverState *bs)
  again:
  #endif
      if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
+        size = 0;
  #ifdef DIOCGMEDIASIZE
-        if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
+        if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size)) {

Pre-existing, but I feel compelled to express my unease about this cast.

We set -D_FILE_OFFSET_BITS=64, so IIUC, off_t should be 64-bits on both 32 and 64 bit build hosts. IIUC, it is defined to be a signed integer. So while off_t may not have the same typedef as int64_t, it should be the same size and signedness. I expect we have other code with this same assumption about off-t/int64_t interchangeability.

We could assert sizeof(int64_t) == sizeof(off_t) in a header somewhere if we want to be super paranoid.

Quoted 42 lines
Quoted 7 lines
block/file-posix.c +2 -0
+            size = 0;
+        }
  #elif defined(DIOCGPART)
          {
                  struct partinfo pi;

block/file-posix.c again: · line 2338

Quoted 7 lines
block/file-posix.c +1 -3
@@ -2332,9 +2335,7 @@ again:
                  else
                          size = 0;
          }
-        if (size == 0)
-#endif
-#if defined(__APPLE__) && defined(__MACH__)
+#elif defined(DKIOCGETBLOCKCOUNT) && defined(DKIOCGETBLOCKSIZE)

As far a I can tell, before this patch, if the DIOCGMEDIASIZE ioctl failed, we fell back to this DKIOCGETBLOCKCOUNT/SIZE block (if compiled in). Now this is an #elif and so will not be used if DIOCGMEDIASIZE was defined. Is that intentional?

This may be fine, and apart from that, this patch looks good to me, but this change in behavior wasn’t mentioned in the commit message, hence me asking.

Quoted 19 lines
block/file-posix.c +3 -7
          {
              uint64_t sectors = 0;
              uint32_t sector_size = 0;
@@ -2342,19 +2343,15 @@ again:
              if (ioctl(fd, DKIOCGETBLOCKCOUNT, &sectors) == 0
                 && ioctl(fd, DKIOCGETBLOCKSIZE, &sector_size) == 0) {
                  size = sectors * sector_size;
-            } else {
-                size = lseek(fd, 0LL, SEEK_END);
-                if (size < 0) {
-                    return -errno;
-                }
              }
          }
-#else
-        size = lseek(fd, 0LL, SEEK_END);
+#endif
+        if (size == 0) {
+            size = lseek(fd, 0LL, SEEK_END);
Quoted 10 lines

block/file-posix.c again: · line 2357

Quoted 8 lines
block/file-posix.c +1 -1
+        }
          if (size < 0) {
              return -errno;
          }
-#endif
  #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
          switch(s->type) {
          case FTYPE_CD:

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#26 lore raw
In reply to Paolo Bonzini
Quoted 23 lines · Paolo Bonzini

Hi Kevin,

this is a combination of two series that both affect host block device support in block/file-posix.c. Joelle's series is unchanged, while mine was adjusted according to your review of v2.

v1->v2: add missing patch

v2->v3: add max_hw_transfer to BlockLimits

Joelle van Dyne (3):
block: feature detection for host block support
block: check for sys/disk.h
block: detect DKIOCGETBLOCKCOUNT/SIZE before use

Paolo Bonzini (4):
file-posix: fix max_iov for /dev/sg devices
scsi-generic: pass max_segments via max_iov field in BlockLimits
block: add max_hw_transfer to BlockLimits
file-posix: try BLKSECTGET on block devices too, do not round to power
of 2

Quoted 11 lines
 block.c                        |   2 +-
 block/block-backend.c          |  12 ++++
 block/file-posix.c             | 104 ++++++++++++++++++++-------------
 block/io.c                     |   1 +
 hw/scsi/scsi-generic.c         |   6 +-
 include/block/block_int.h      |   7 +++
 include/sysemu/block-backend.h |   1 +
 meson.build                    |   7 ++-
 qapi/block-core.json           |  10 +++-
 9 files changed, 102 insertions(+), 48 deletions(-)

Hi Paolo and everyone!

I used to have a patch series that was about to fix the block limits of the scsi-block, which I think is similar to this patch series.

Sorry that I kind of forgot about it for too much time.

https://www.mail-archive.com/[email protected]/msg768261.html

I'll need some time to swap-in this area so that I could compare our patches to see if we missed something.

Best regards,
Maxim Levitsky

#27 lore raw
Quoted 9 lines · Maxim Levitsky

I used to have a patch series that was about to fix the block limits of the scsi-block, which I think is similar to this patch series.

Sorry that I kind of forgot about it for too much time.

https://www.mail-archive.com/[email protected]/msg768261.html

I'll need some time to swap-in this area so that I could compare our patches to see if we missed something.

They are indeed very similar; the only substantial change is that my patches also clamp max_hw_transfer to max_transfer.

I picked up patch 5 from your old submission and queued it, since it's a SCSI change.

Paolo