Skip to contentSkip to conversation

fix max_discard for NBD/gluster block drivers

PATCH In review Denis V. Lunev 4 participants 11 messages Feb 2, 2015 – Feb 3, 2015 Block layer

lore
Share & tools
lore
#10 lore raw

PATCH v3 2/2 nbd: fix max_discard

In reply to Denis V. Lunev

nbd_co_discard calls nbd_client_session_co_discard which uses uint32_t as the length in bytes of the data to discard due to the following definition:

struct nbd_request {
    uint32_t magic;
    uint32_t type;
    uint64_t handle;
    uint64_t from;
    uint32_t len; <-- the length of data to be discarded, in bytes
} QEMU_PACKED;

Thus we should limit bl_max_discard to UINT32_MAX >> BDRV_SECTOR_BITS to avoid overflow.

---
 block/nbd.c | 8 ++++++++
 1 file changed, 8 insertions(+)
block/nbd.c +8 -0
index 04cc845..99af713 100644
@@ -301,6 +301,11 @@ static int nbd_co_flush(BlockDriverState *bs)
     return nbd_client_session_co_flush(&s->client);
 }
 
+static void nbd_refresh_limits(BlockDriverState *bs, Error **errp)
+{
+    bs->bl.max_discard = UINT32_MAX >> BDRV_SECTOR_BITS;
+}
+
 static int nbd_co_discard(BlockDriverState *bs, int64_t sector_num,
                           int nb_sectors)
 {
@@ -396,6 +401,7 @@ static BlockDriver bdrv_nbd = {
     .bdrv_close                 = nbd_close,
     .bdrv_co_flush_to_os        = nbd_co_flush,
     .bdrv_co_discard            = nbd_co_discard,
+    .bdrv_refresh_limits        = nbd_refresh_limits,
     .bdrv_getlength             = nbd_getlength,
     .bdrv_detach_aio_context    = nbd_detach_aio_context,
     .bdrv_attach_aio_context    = nbd_attach_aio_context,
@@ -413,6 +419,7 @@ static BlockDriver bdrv_nbd_tcp = {
     .bdrv_close                 = nbd_close,
     .bdrv_co_flush_to_os        = nbd_co_flush,
     .bdrv_co_discard            = nbd_co_discard,
+    .bdrv_refresh_limits        = nbd_refresh_limits,
     .bdrv_getlength             = nbd_getlength,
     .bdrv_detach_aio_context    = nbd_detach_aio_context,
     .bdrv_attach_aio_context    = nbd_attach_aio_context,
@@ -430,6 +437,7 @@ static BlockDriver bdrv_nbd_unix = {
     .bdrv_close                 = nbd_close,
     .bdrv_co_flush_to_os        = nbd_co_flush,
     .bdrv_co_discard            = nbd_co_discard,
+    .bdrv_refresh_limits        = nbd_refresh_limits,
     .bdrv_getlength             = nbd_getlength,
     .bdrv_detach_aio_context    = nbd_detach_aio_context,
     .bdrv_attach_aio_context    = nbd_attach_aio_context,
Signature · 1 line
1.9.1
#1 lore raw

PATCH v3 1/2 glusterfs: fix max_discard

qemu_gluster_co_discard calculates size to discard as follows

    size_t size = nb_sectors * BDRV_SECTOR_SIZE;
    ret = glfs_discard_async(s->fd, offset, size, &gluster_finish_aiocb, acb);

glfs_discard_async is declared as follows:
int glfs_discard_async (glfs_fd_t *fd, off_t length, size_t lent,
glfs_io_cbk fn, void *data) __THROW
This is problematic on i686 as sizeof(size_t) == 4.

Set bl_max_discard to SIZE_MAX >> BDRV_SECTOR_BITS to avoid overflow on i386.

CC Kevin Wolf <[email protected]>
CC Peter Lieven <[email protected]>
---
 block/gluster.c | 9 +++++++++
 1 file changed, 9 insertions(+)
block/gluster.c +9 -0
index 1eb3a8c..8a8c153 100644
@@ -622,6 +622,11 @@ out:
     return ret;
 }
 
+static void qemu_gluster_refresh_limits(BlockDriverState *bs, Error **errp)
+{
+    bs->bl.max_discard = MIN(SIZE_MAX >> BDRV_SECTOR_BITS, INT_MAX);
+}
+
 #ifdef CONFIG_GLUSTERFS_DISCARD
 static coroutine_fn int qemu_gluster_co_discard(BlockDriverState *bs,
         int64_t sector_num, int nb_sectors)
@@ -735,6 +740,7 @@ static BlockDriver bdrv_gluster = {
 #ifdef CONFIG_GLUSTERFS_DISCARD
     .bdrv_co_discard              = qemu_gluster_co_discard,
 #endif
+    .bdrv_refresh_limits          = qemu_gluster_refresh_limits,
 #ifdef CONFIG_GLUSTERFS_ZEROFILL
     .bdrv_co_write_zeroes         = qemu_gluster_co_write_zeroes,
 #endif
@@ -762,6 +768,7 @@ static BlockDriver bdrv_gluster_tcp = {
 #ifdef CONFIG_GLUSTERFS_DISCARD
     .bdrv_co_discard              = qemu_gluster_co_discard,
 #endif
+    .bdrv_refresh_limits          = qemu_gluster_refresh_limits,
 #ifdef CONFIG_GLUSTERFS_ZEROFILL
     .bdrv_co_write_zeroes         = qemu_gluster_co_write_zeroes,
 #endif
@@ -789,6 +796,7 @@ static BlockDriver bdrv_gluster_unix = {
 #ifdef CONFIG_GLUSTERFS_DISCARD
     .bdrv_co_discard              = qemu_gluster_co_discard,
 #endif
+    .bdrv_refresh_limits          = qemu_gluster_refresh_limits,
 #ifdef CONFIG_GLUSTERFS_ZEROFILL
     .bdrv_co_write_zeroes         = qemu_gluster_co_write_zeroes,
 #endif
@@ -816,6 +824,7 @@ static BlockDriver bdrv_gluster_rdma = {
 #ifdef CONFIG_GLUSTERFS_DISCARD
     .bdrv_co_discard              = qemu_gluster_co_discard,
 #endif
+    .bdrv_refresh_limits          = qemu_gluster_refresh_limits,
 #ifdef CONFIG_GLUSTERFS_ZEROFILL
     .bdrv_co_write_zeroes         = qemu_gluster_co_write_zeroes,
 #endif
Signature · 1 line
1.9.1
Reviewed-by on v3 1/2
#2 lore raw

Re: [Qemu-devel] [PATCH 1/2] glusterfs: fix max_discard

Am 02.02.2015 um 21:09 schrieb Denis V. Lunev:

Quoted 24 lines

qemu_gluster_co_discard calculates size to discard as follows

    size_t size = nb_sectors * BDRV_SECTOR_SIZE;
    ret = glfs_discard_async(s->fd, offset, size, &gluster_finish_aiocb, acb);

glfs_discard_async is declared as follows:
int glfs_discard_async (glfs_fd_t *fd, off_t length, size_t lent,
glfs_io_cbk fn, void *data) __THROW
This is problematic on i686 as sizeof(size_t) == 4.

Set bl_max_discard to SIZE_MAX >> BDRV_SECTOR_BITS to avoid overflow on i386.

CC Kevin Wolf <[email protected]>
CC Peter Lieven <[email protected]>
---
 block/gluster.c | 9 +++++++++
 1 file changed, 9 insertions(+)
block/gluster.c +0 -0
index 1eb3a8c..8a8c153 100644

block/gluster.c out: · line 629

Quoted 8 lines
block/gluster.c +5 -0
@@ -622,6 +622,11 @@ out:
     return ret;
 }
 
+static void qemu_gluster_refresh_limits(BlockDriverState *bs, Error **errp)
+{
+    bs->bl.max_discard = MIN(SIZE_MAX >> BDRV_SECTOR_BITS, INT_MAX);
+}
+

Looking at the gluster code bl.max_transfer_length should have the same limit, but thats a different patch.

Quoted 28 lines
block/gluster.c +3 -0
 #ifdef CONFIG_GLUSTERFS_DISCARD
 static coroutine_fn int qemu_gluster_co_discard(BlockDriverState *bs,
         int64_t sector_num, int nb_sectors)
@@ -735,6 +740,7 @@ static BlockDriver bdrv_gluster = {
 #ifdef CONFIG_GLUSTERFS_DISCARD
     .bdrv_co_discard              = qemu_gluster_co_discard,
 #endif
+    .bdrv_refresh_limits          = qemu_gluster_refresh_limits,
 #ifdef CONFIG_GLUSTERFS_ZEROFILL
     .bdrv_co_write_zeroes         = qemu_gluster_co_write_zeroes,
 #endif
@@ -762,6 +768,7 @@ static BlockDriver bdrv_gluster_tcp = {
 #ifdef CONFIG_GLUSTERFS_DISCARD
     .bdrv_co_discard              = qemu_gluster_co_discard,
 #endif
+    .bdrv_refresh_limits          = qemu_gluster_refresh_limits,
 #ifdef CONFIG_GLUSTERFS_ZEROFILL
     .bdrv_co_write_zeroes         = qemu_gluster_co_write_zeroes,
 #endif
@@ -789,6 +796,7 @@ static BlockDriver bdrv_gluster_unix = {
 #ifdef CONFIG_GLUSTERFS_DISCARD
     .bdrv_co_discard              = qemu_gluster_co_discard,
 #endif
+    .bdrv_refresh_limits          = qemu_gluster_refresh_limits,
 #ifdef CONFIG_GLUSTERFS_ZEROFILL
     .bdrv_co_write_zeroes         = qemu_gluster_co_write_zeroes,
 #endif

block/gluster.c static BlockDriver bdrv_gluster_rdma = { · line 830

Quoted 7 lines
block/gluster.c +1 -0
@@ -816,6 +824,7 @@ static BlockDriver bdrv_gluster_rdma = {
 #ifdef CONFIG_GLUSTERFS_DISCARD
     .bdrv_co_discard              = qemu_gluster_co_discard,
 #endif
+    .bdrv_refresh_limits          = qemu_gluster_refresh_limits,
 #ifdef CONFIG_GLUSTERFS_ZEROFILL
     .bdrv_co_write_zeroes         = qemu_gluster_co_write_zeroes,
 #endif
#3 lore raw

Re: [Qemu-devel] [PATCH 1/2] glusterfs: fix max_discard

Quoted 28 lines · Peter Lieven

Am 02.02.2015 um 21:09 schrieb Denis V. Lunev:

Quoted 27 lines

qemu_gluster_co_discard calculates size to discard as follows

     size_t size = nb_sectors * BDRV_SECTOR_SIZE;
     ret = glfs_discard_async(s->fd, offset, size, &gluster_finish_aiocb, acb);

glfs_discard_async is declared as follows:
int glfs_discard_async (glfs_fd_t *fd, off_t length, size_t lent,
glfs_io_cbk fn, void *data) __THROW
This is problematic on i686 as sizeof(size_t) == 4.

Set bl_max_discard to SIZE_MAX >> BDRV_SECTOR_BITS to avoid overflow on i386.

CC Kevin Wolf <[email protected]>
CC Peter Lieven <[email protected]>
---
  block/gluster.c | 9 +++++++++
  1 file changed, 9 insertions(+)
block/gluster.c +0 -0
index 1eb3a8c..8a8c153 100644
@@ -622,6 +622,11 @@ out:
      return ret;
  }
 
Quoted 6 lines

block/gluster.c out: · line 629

Quoted 5 lines
block/gluster.c +5 -0
+static void qemu_gluster_refresh_limits(BlockDriverState *bs, Error **errp)
+{
+    bs->bl.max_discard = MIN(SIZE_MAX >> BDRV_SECTOR_BITS, INT_MAX);
+}
+

Looking at the gluster code bl.max_transfer_length should have the same limit, but thats a different patch.

ha, the same applies to nbd code too.

I'll do this stuff tomorrow and also I think that some audit in other drivers could reveal something interesting.

Den

#4 lore raw

Re: [Qemu-devel] [PATCH 1/2] glusterfs: fix max_discard

Quoted 36 lines · Denis V. Lunev
Quoted 21 lines · Peter Lieven

Am 02.02.2015 um 21:09 schrieb Denis V. Lunev:

Quoted 20 lines

qemu_gluster_co_discard calculates size to discard as follows

     size_t size = nb_sectors * BDRV_SECTOR_SIZE;
     ret = glfs_discard_async(s->fd, offset, size, 

&gluster_finish_aiocb, acb);

glfs_discard_async is declared as follows:
int glfs_discard_async (glfs_fd_t *fd, off_t length, size_t lent,
glfs_io_cbk fn, void *data) __THROW
This is problematic on i686 as sizeof(size_t) == 4.

Set bl_max_discard to SIZE_MAX >> BDRV_SECTOR_BITS to avoid overflow on i386.

CC Kevin Wolf <[email protected]>
CC Peter Lieven <[email protected]>
---
  block/gluster.c | 9 +++++++++
  1 file changed, 9 insertions(+)
Quoted 14 lines

block/gluster.c out: · line 629

Quoted 13 lines
block/gluster.c +4 -0
index 1eb3a8c..8a8c153 100644
@@ -622,6 +622,11 @@ out:
      return ret;
  }
  +static void qemu_gluster_refresh_limits(BlockDriverState *bs, 
 Error **errp)
+{
+    bs->bl.max_discard = MIN(SIZE_MAX >> BDRV_SECTOR_BITS, INT_MAX);
+}
+

Looking at the gluster code bl.max_transfer_length should have the same limit, but thats a different patch.

Quoted 6 lines

ha, the same applies to nbd code too.

I'll do this stuff tomorrow and also I think that some audit in other drivers could reveal something interesting.

Den

ok. The situation is well rotten here on i686.

The problem comes from the fact that QEMUIOVector and iovec uses size_t as length. All API calls use this abstraction. Thus all conversion operations from nr_sectors to size could bang at any moment.

Putting dirty hands here is problematic from my point of view. Should we really care about this? 32bit applications are becoming old good history of IT... Den

#5 lore raw

Re: [Qemu-devel] [PATCH 1/2] glusterfs: fix max_discard

Am 03.02.2015 um 08:31 schrieb Denis V. Lunev:

Quoted 43 lines · Denis V. Lunev
Quoted 34 lines · Peter Lieven
Quoted 20 lines

Am 02.02.2015 um 21:09 schrieb Denis V. Lunev:

Quoted 19 lines

qemu_gluster_co_discard calculates size to discard as follows

     size_t size = nb_sectors * BDRV_SECTOR_SIZE;
     ret = glfs_discard_async(s->fd, offset, size, &gluster_finish_aiocb, acb);

glfs_discard_async is declared as follows:
int glfs_discard_async (glfs_fd_t *fd, off_t length, size_t lent,
glfs_io_cbk fn, void *data) __THROW
This is problematic on i686 as sizeof(size_t) == 4.

Set bl_max_discard to SIZE_MAX >> BDRV_SECTOR_BITS to avoid overflow on i386.

CC Kevin Wolf <[email protected]>
CC Peter Lieven <[email protected]>
---
  block/gluster.c | 9 +++++++++
  1 file changed, 9 insertions(+)
Quoted 13 lines

block/gluster.c out: · line 628

Quoted 12 lines
block/gluster.c +4 -0
index 1eb3a8c..8a8c153 100644
@@ -622,6 +622,11 @@ out:
      return ret;
  }
  +static void qemu_gluster_refresh_limits(BlockDriverState *bs, Error **errp)
+{
+    bs->bl.max_discard = MIN(SIZE_MAX >> BDRV_SECTOR_BITS, INT_MAX);
+}
+

Looking at the gluster code bl.max_transfer_length should have the same limit, but thats a different patch.

Quoted 6 lines

ha, the same applies to nbd code too.

I'll do this stuff tomorrow and also I think that some audit in other drivers could reveal something interesting.

Den

ok. The situation is well rotten here on i686.

Quoted 8 lines

The problem comes from the fact that QEMUIOVector and iovec uses size_t as length. All API calls use this abstraction. Thus all conversion operations from nr_sectors to size could bang at any moment.

Putting dirty hands here is problematic from my point of view. Should we really care about this? 32bit applications are becoming old good history of IT...

The host has to be 32bit to be in trouble. And at least if we have KVM the host has to support long mode.

I have on my todo to add generic code for honouring bl.max_transfer_length in block.c. We could change default maximum from INT_MAX to SIZE_MAX >> BDRV_SECTOR_BITS for bl.max_transfer_length.

Peter

#6 lore raw

Re: [Qemu-devel] [PATCH 1/2] glusterfs: fix max_discard

Am 03.02.2015 um 12:30 hat Peter Lieven geschrieben:

Quoted 53 lines

Am 03.02.2015 um 08:31 schrieb Denis V. Lunev:

Quoted 43 lines · Denis V. Lunev
Quoted 34 lines · Peter Lieven
Quoted 20 lines

Am 02.02.2015 um 21:09 schrieb Denis V. Lunev:

Quoted 19 lines

qemu_gluster_co_discard calculates size to discard as follows

     size_t size = nb_sectors * BDRV_SECTOR_SIZE;
     ret = glfs_discard_async(s->fd, offset, size, &gluster_finish_aiocb, acb);

glfs_discard_async is declared as follows:
int glfs_discard_async (glfs_fd_t *fd, off_t length, size_t lent,
glfs_io_cbk fn, void *data) __THROW
This is problematic on i686 as sizeof(size_t) == 4.

Set bl_max_discard to SIZE_MAX >> BDRV_SECTOR_BITS to avoid overflow on i386.

CC Kevin Wolf <[email protected]>
CC Peter Lieven <[email protected]>
---
  block/gluster.c | 9 +++++++++
  1 file changed, 9 insertions(+)
Quoted 13 lines

block/gluster.c out: · line 628

Quoted 12 lines
block/gluster.c +4 -0
index 1eb3a8c..8a8c153 100644
@@ -622,6 +622,11 @@ out:
      return ret;
  }
  +static void qemu_gluster_refresh_limits(BlockDriverState *bs, Error **errp)
+{
+    bs->bl.max_discard = MIN(SIZE_MAX >> BDRV_SECTOR_BITS, INT_MAX);
+}
+

Looking at the gluster code bl.max_transfer_length should have the same limit, but thats a different patch.

Quoted 6 lines

ha, the same applies to nbd code too.

I'll do this stuff tomorrow and also I think that some audit in other drivers could reveal something interesting.

Den

ok. The situation is well rotten here on i686.

Quoted 8 lines

The problem comes from the fact that QEMUIOVector and iovec uses size_t as length. All API calls use this abstraction. Thus all conversion operations from nr_sectors to size could bang at any moment.

Putting dirty hands here is problematic from my point of view. Should we really care about this? 32bit applications are becoming old good history of IT...

Quoted 6 lines

The host has to be 32bit to be in trouble. And at least if we have KVM the host has to support long mode.

I have on my todo to add generic code for honouring bl.max_transfer_length in block.c. We could change default maximum from INT_MAX to SIZE_MAX >> BDRV_SECTOR_BITS for bl.max_transfer_length.

So the conclusion is that we'll apply this series as it is and you'll take care of the rest later?

Kevin

#7 lore raw

Re: [Qemu-devel] [PATCH 1/2] glusterfs: fix max_discard

Am 03.02.2015 um 12:37 schrieb Kevin Wolf:

Quoted 53 lines

Am 03.02.2015 um 12:30 hat Peter Lieven geschrieben:

Quoted 52 lines

Am 03.02.2015 um 08:31 schrieb Denis V. Lunev:

Quoted 51 lines · Denis V. Lunev
Quoted 34 lines · Peter Lieven
Quoted 20 lines

Am 02.02.2015 um 21:09 schrieb Denis V. Lunev:

Quoted 19 lines

qemu_gluster_co_discard calculates size to discard as follows

     size_t size = nb_sectors * BDRV_SECTOR_SIZE;
     ret = glfs_discard_async(s->fd, offset, size, &gluster_finish_aiocb, acb);

glfs_discard_async is declared as follows:
int glfs_discard_async (glfs_fd_t *fd, off_t length, size_t lent,
glfs_io_cbk fn, void *data) __THROW
This is problematic on i686 as sizeof(size_t) == 4.

Set bl_max_discard to SIZE_MAX >> BDRV_SECTOR_BITS to avoid overflow on i386.

CC Kevin Wolf <[email protected]>
CC Peter Lieven <[email protected]>
---
  block/gluster.c | 9 +++++++++
  1 file changed, 9 insertions(+)
Quoted 13 lines

block/gluster.c out: · line 628

Quoted 12 lines
block/gluster.c +4 -0
index 1eb3a8c..8a8c153 100644
@@ -622,6 +622,11 @@ out:
      return ret;
  }
  +static void qemu_gluster_refresh_limits(BlockDriverState *bs, Error **errp)
+{
+    bs->bl.max_discard = MIN(SIZE_MAX >> BDRV_SECTOR_BITS, INT_MAX);
+}
+

Looking at the gluster code bl.max_transfer_length should have the same limit, but thats a different patch.

Quoted 6 lines

ha, the same applies to nbd code too.

I'll do this stuff tomorrow and also I think that some audit in other drivers could reveal something interesting.

Den

ok. The situation is well rotten here on i686.

The problem comes from the fact that QEMUIOVector and iovec uses size_t as length. All API calls use this abstraction. Thus all conversion operations from nr_sectors to size could bang at any moment.

Putting dirty hands here is problematic from my point of view. Should we really care about this? 32bit applications are becoming old good history of IT...

Quoted 8 lines
Quoted 6 lines

The host has to be 32bit to be in trouble. And at least if we have KVM the host has to support long mode.

I have on my todo to add generic code for honouring bl.max_transfer_length in block.c. We could change default maximum from INT_MAX to SIZE_MAX >> BDRV_SECTOR_BITS for bl.max_transfer_length.

So the conclusion is that we'll apply this series as it is and you'll take care of the rest later?

Yes, and actually we need a macro like

#define BDRV_MAX_REQUEST_SECTORS MIN(SIZE_MAX >> BDRV_SECTOR_BITS, INT_MAX)

as limit for everything. Because bdrv_check_byte_request already has a size_t argument. So we could already create an overflow in bdrv_check_request when we convert nb_sectors to size_t.

I will create a patch to catch at least this overflow shortly.

Peter

#8 lore raw

Re: [Qemu-devel] [PATCH 1/2] glusterfs: fix max_discard

Quoted 66 lines · Peter Lieven

Am 03.02.2015 um 12:37 schrieb Kevin Wolf:

Quoted 53 lines

Am 03.02.2015 um 12:30 hat Peter Lieven geschrieben:

Quoted 52 lines

Am 03.02.2015 um 08:31 schrieb Denis V. Lunev:

Quoted 51 lines · Denis V. Lunev
Quoted 34 lines · Peter Lieven
Quoted 20 lines

Am 02.02.2015 um 21:09 schrieb Denis V. Lunev:

Quoted 19 lines

qemu_gluster_co_discard calculates size to discard as follows

      size_t size = nb_sectors * BDRV_SECTOR_SIZE;
      ret = glfs_discard_async(s->fd, offset, size, &gluster_finish_aiocb, acb);

glfs_discard_async is declared as follows:
int glfs_discard_async (glfs_fd_t *fd, off_t length, size_t lent,
glfs_io_cbk fn, void *data) __THROW
This is problematic on i686 as sizeof(size_t) == 4.

Set bl_max_discard to SIZE_MAX >> BDRV_SECTOR_BITS to avoid overflow on i386.

CC Kevin Wolf <[email protected]>
CC Peter Lieven <[email protected]>
---
   block/gluster.c | 9 +++++++++
   1 file changed, 9 insertions(+)
Quoted 13 lines

block/gluster.c out: · line 628

Quoted 12 lines
block/gluster.c +4 -0
index 1eb3a8c..8a8c153 100644
@@ -622,6 +622,11 @@ out:
       return ret;
   }
   +static void qemu_gluster_refresh_limits(BlockDriverState *bs, Error **errp)
+{
+    bs->bl.max_discard = MIN(SIZE_MAX >> BDRV_SECTOR_BITS, INT_MAX);
+}
+

Looking at the gluster code bl.max_transfer_length should have the same limit, but thats a different patch.

Quoted 6 lines

ha, the same applies to nbd code too.

I'll do this stuff tomorrow and also I think that some audit in other drivers could reveal something interesting.

Den

ok. The situation is well rotten here on i686.

The problem comes from the fact that QEMUIOVector and iovec uses size_t as length. All API calls use this abstraction. Thus all conversion operations from nr_sectors to size could bang at any moment.

Putting dirty hands here is problematic from my point of view. Should we really care about this? 32bit applications are becoming old good history of IT...

Quoted 8 lines
Quoted 6 lines

The host has to be 32bit to be in trouble. And at least if we have KVM the host has to support long mode.

I have on my todo to add generic code for honouring bl.max_transfer_length in block.c. We could change default maximum from INT_MAX to SIZE_MAX >> BDRV_SECTOR_BITS for bl.max_transfer_length.

So the conclusion is that we'll apply this series as it is and you'll take care of the rest later?

Yes, and actually we need a macro like

#define BDRV_MAX_REQUEST_SECTORS MIN(SIZE_MAX >> BDRV_SECTOR_BITS, INT_MAX)

Quoted 8 lines

as limit for everything. Because bdrv_check_byte_request already has a size_t argument. So we could already create an overflow in bdrv_check_request when we convert nb_sectors to size_t.

I will create a patch to catch at least this overflow shortly.

Peter

I like this macro :)

I vote to move MIN(SIZE_MAX >> BDRV_SECTOR_BITS, INT_MAX) into generic code on discard/write_zero paths immediately and drop this exact patch.

Patch 2 of this set would be better to have additional +bs->bl.max_transfer_length = UINT32_MAX >> BDRV_SECTOR_BITS;

I'll wait Peter's patch and respin on top of it to avoid unnecessary commits.

Den

#9 lore raw

Re: [Qemu-devel] [PATCH 1/2] glusterfs: fix max_discard

Am 03.02.2015 um 12:51 schrieb Denis V. Lunev:

Quoted 71 lines · Peter Lieven
Quoted 70 lines

Am 03.02.2015 um 12:37 schrieb Kevin Wolf:

Quoted 53 lines

Am 03.02.2015 um 12:30 hat Peter Lieven geschrieben:

Quoted 52 lines

Am 03.02.2015 um 08:31 schrieb Denis V. Lunev:

Quoted 51 lines · Denis V. Lunev
Quoted 34 lines · Peter Lieven
Quoted 20 lines

Am 02.02.2015 um 21:09 schrieb Denis V. Lunev:

Quoted 19 lines

qemu_gluster_co_discard calculates size to discard as follows

      size_t size = nb_sectors * BDRV_SECTOR_SIZE;
      ret = glfs_discard_async(s->fd, offset, size, &gluster_finish_aiocb, acb);

glfs_discard_async is declared as follows:
int glfs_discard_async (glfs_fd_t *fd, off_t length, size_t lent,
glfs_io_cbk fn, void *data) __THROW
This is problematic on i686 as sizeof(size_t) == 4.

Set bl_max_discard to SIZE_MAX >> BDRV_SECTOR_BITS to avoid overflow on i386.

CC Kevin Wolf <[email protected]>
CC Peter Lieven <[email protected]>
---
   block/gluster.c | 9 +++++++++
   1 file changed, 9 insertions(+)
Quoted 13 lines

block/gluster.c out: · line 628

Quoted 12 lines
block/gluster.c +4 -0
index 1eb3a8c..8a8c153 100644
@@ -622,6 +622,11 @@ out:
       return ret;
   }
   +static void qemu_gluster_refresh_limits(BlockDriverState *bs, Error **errp)
+{
+    bs->bl.max_discard = MIN(SIZE_MAX >> BDRV_SECTOR_BITS, INT_MAX);
+}
+

Looking at the gluster code bl.max_transfer_length should have the same limit, but thats a different patch.

Quoted 6 lines

ha, the same applies to nbd code too.

I'll do this stuff tomorrow and also I think that some audit in other drivers could reveal something interesting.

Den

ok. The situation is well rotten here on i686.

The problem comes from the fact that QEMUIOVector and iovec uses size_t as length. All API calls use this abstraction. Thus all conversion operations from nr_sectors to size could bang at any moment.

Putting dirty hands here is problematic from my point of view. Should we really care about this? 32bit applications are becoming old good history of IT...

Quoted 8 lines
Quoted 6 lines

The host has to be 32bit to be in trouble. And at least if we have KVM the host has to support long mode.

I have on my todo to add generic code for honouring bl.max_transfer_length in block.c. We could change default maximum from INT_MAX to SIZE_MAX >> BDRV_SECTOR_BITS for bl.max_transfer_length.

So the conclusion is that we'll apply this series as it is and you'll take care of the rest later?

Yes, and actually we need a macro like

#define BDRV_MAX_REQUEST_SECTORS MIN(SIZE_MAX >> BDRV_SECTOR_BITS, INT_MAX)

as limit for everything. Because bdrv_check_byte_request already has a size_t argument. So we could already create an overflow in bdrv_check_request when we convert nb_sectors to size_t.

Quoted 5 lines
Quoted 4 lines

I will create a patch to catch at least this overflow shortly.

Peter

I like this macro :)

I see I looked at an old checkout in bdrv_check_request there is already such a check meanwhile.

static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
int nb_sectors)

{
    if (nb_sectors < 0 || nb_sectors > INT_MAX / BDRV_SECTOR_SIZE) {
        return -EIO;
    }

    return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE,
                                   nb_sectors * BDRV_SECTOR_SIZE);
}
Quoted 10 lines

I vote to move MIN(SIZE_MAX >> BDRV_SECTOR_BITS, INT_MAX) into generic code on discard/write_zero paths immediately and drop this exact patch.

Patch 2 of this set would be better to have additional +bs->bl.max_transfer_length = UINT32_MAX >> BDRV_SECTOR_BITS;

I'll wait Peter's patch and respin on top of it to avoid unnecessary commits.

Den