Skip to contentSkip to conversation

virtio_blk: check for hardsector size from host

PATCH/RFC New Christian Borntraeger 3 participants 21 messages May 27, 2008 – May 30, 2008

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

Rusty, Jens,

I need your opinion on the following patch. It seems to work, but I would like to get some feedback if this patch is the right approach:

---

Currently virtio_blk assumes a 512 byte hard sector size. This can cause trouble / performance issues if the backing has a different block size (like a file on an ext3 file system formatted with 4k block size or a dasd device).

Lets add a feature flag that tells the guest to use a different hard sector size than 512 byte. The host can use stat->st_blksize to determine the right hard sector size for a specific backing.

---
 drivers/block/virtio_blk.c |   10 +++++++++-
 include/linux/virtio_blk.h |    3 +++
 2 files changed, 12 insertions(+), 1 deletion(-)
drivers/block/virtio_blk.c +9 -1
Index: kvm/drivers/block/virtio_blk.c
===================================================================
@@ -196,6 +196,7 @@ static int virtblk_probe(struct virtio_d
 	int err;
 	u64 cap;
 	u32 v;
+	u64 blk_size;
 
 	if (index_to_minor(index) >= 1 << MINORBITS)
 		return -ENOSPC;
@@ -290,6 +291,13 @@ static int virtblk_probe(struct virtio_d
 	if (!err)
 		blk_queue_max_hw_segments(vblk->disk->queue, v);
 
+	/* Host can optionally specify the block size of the device */
+	err = virtio_config_val(vdev, VIRTIO_BLK_F_BLK_SIZE,
+				offsetof(struct virtio_blk_config, blk_size),
+				&blk_size);
+	if (!err)
+		blk_queue_hardsect_size(vblk->disk->queue, blk_size);
+
 	add_disk(vblk->disk);
 	return 0;
 
@@ -329,7 +337,7 @@ static struct virtio_device_id id_table[
 
 static unsigned int features[] = {
 	VIRTIO_BLK_F_BARRIER, VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX,
-	VIRTIO_BLK_F_GEOMETRY, VIRTIO_BLK_F_RO,
+	VIRTIO_BLK_F_GEOMETRY, VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
 };
 
 static struct virtio_driver virtio_blk = {
include/linux/virtio_blk.h +3 -0
Index: kvm/include/linux/virtio_blk.h
===================================================================
@@ -11,6 +11,7 @@
 #define VIRTIO_BLK_F_SEG_MAX	2	/* Indicates maximum # of segments */
 #define VIRTIO_BLK_F_GEOMETRY	4	/* Legacy geometry available  */
 #define VIRTIO_BLK_F_RO		5	/* Disk is read-only */
+#define VIRTIO_BLK_F_BLK_SIZE	6	/* Block size of disk is available*/
 
 struct virtio_blk_config
 {
@@ -26,6 +27,8 @@ struct virtio_blk_config
 		__u8 heads;
 		__u8 sectors;
 	} geometry;
+	/* block size of device (if VIRTIO_BLK_F_BLK_SIZE) */
+	__u64 blk_size;
 } __attribute__((packed));
 
 /* These two define direction. */
#1 lore raw
Quoted 4 lines · On Tuesday 27 May 2008 19:04:59 Christian Borntraeger wrote:

Rusty, Jens,

I need your opinion on the following patch. It seems to work, but I would like to get some feedback if this patch is the right approach:

Looks like the right approach to me. Don't know about the block side of it...

Just that u64 seems like overkill: u32?

Cheers,
Rusty.

#2 lore raw
In reply to Christian Borntraeger
Quoted 4 lines · On Tuesday 27 May 2008 19:04:59 Christian Borntraeger wrote:

Rusty, Jens,

I need your opinion on the following patch. It seems to work, but I would like to get some feedback if this patch is the right approach:

Looks like the right approach to me. Don't know about the block side of it...

Just that u64 seems like overkill: u32?

Cheers,
Rusty.

#3 lore raw

Am Donnerstag, 29. Mai 2008 schrieb Rusty Russell:

Quoted 7 lines · On Tuesday 27 May 2008 19:04:59 Christian Borntraeger wrote:
Quoted 4 lines

Rusty, Jens,

I need your opinion on the following patch. It seems to work, but I would like to get some feedback if this patch is the right approach:

Looks like the right approach to me. Don't know about the block side of

it...

Quoted 2 lines

Just that u64 seems like overkill: u32?

I looked at blksize_t, which seems to be long. So I decided to use u64 which should be enough (but still overkill...) on all platforms.

While we are at it. Since we dont convert endianess any longer, should I change all the __le{..} definitions to __u{..} in virtio_blk.h ?

Christian

#4 lore raw
In reply to Rusty Russell

Am Donnerstag, 29. Mai 2008 schrieb Rusty Russell:

Quoted 7 lines · On Tuesday 27 May 2008 19:04:59 Christian Borntraeger wrote:
Quoted 4 lines

Rusty, Jens,

I need your opinion on the following patch. It seems to work, but I would like to get some feedback if this patch is the right approach:

Looks like the right approach to me. Don't know about the block side of

it...

Quoted 2 lines

Just that u64 seems like overkill: u32?

I looked at blksize_t, which seems to be long. So I decided to use u64 which should be enough (but still overkill...) on all platforms.

While we are at it. Since we dont convert endianess any longer, should I change all the __le{..} definitions to __u{..} in virtio_blk.h ?

Christian

#5 lore raw
In reply to Rusty Russell
Quoted 8 lines · Rusty Russell
Quoted 4 lines · On Tuesday 27 May 2008 19:04:59 Christian Borntraeger wrote:

Rusty, Jens,

I need your opinion on the following patch. It seems to work, but I would like to get some feedback if this patch is the right approach:

Looks like the right approach to me. Don't know about the block side of it...

Looks good to me, definitely an improvement for eg loop backed storage.

Quoted 1 line

Just that u64 seems like overkill: u32?

Definitely, u32 would be just fine, u64 is way overkill :-)

Signature · 1 line
Jens Axboe
#6 lore raw
In reply to Rusty Russell
Quoted 8 lines · Rusty Russell
Quoted 4 lines · On Tuesday 27 May 2008 19:04:59 Christian Borntraeger wrote:

Rusty, Jens,

I need your opinion on the following patch. It seems to work, but I would like to get some feedback if this patch is the right approach:

Looks like the right approach to me. Don't know about the block side of it...

Looks good to me, definitely an improvement for eg loop backed storage.

Quoted 1 line

Just that u64 seems like overkill: u32?

Definitely, u32 would be just fine, u64 is way overkill :-)

Signature · 1 line
Jens Axboe
#7 lore raw
Quoted 10 lines · Jens Axboe
Quoted 8 lines · Rusty Russell
Quoted 4 lines · On Tuesday 27 May 2008 19:04:59 Christian Borntraeger wrote:

Rusty, Jens,

I need your opinion on the following patch. It seems to work, but I would like to get some feedback if this patch is the right approach:

Looks like the right approach to me. Don't know about the block side of it...

Quoted 6 lines

Looks good to me, definitely an improvement for eg loop backed storage.

Quoted 1 line

Just that u64 seems like overkill: u32?

Definitely, u32 would be just fine, u64 is way overkill :-)

Even u16 would work, the block layer doesn't use more than an unsigned short for storing hardware sector size anyway.

Signature · 1 line
Jens Axboe
#8 lore raw
In reply to Jens Axboe
Quoted 10 lines · Jens Axboe
Quoted 8 lines · Rusty Russell
Quoted 4 lines · On Tuesday 27 May 2008 19:04:59 Christian Borntraeger wrote:

Rusty, Jens,

I need your opinion on the following patch. It seems to work, but I would like to get some feedback if this patch is the right approach:

Looks like the right approach to me. Don't know about the block side of it...

Quoted 6 lines

Looks good to me, definitely an improvement for eg loop backed storage.

Quoted 1 line

Just that u64 seems like overkill: u32?

Definitely, u32 would be just fine, u64 is way overkill :-)

Even u16 would work, the block layer doesn't use more than an unsigned short for storing hardware sector size anyway.

Signature · 1 line
Jens Axboe
#9 lore raw

Am Donnerstag, 29. Mai 2008 schrieb Jens Axboe:

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Just that u64 seems like overkill: u32?

Definitely, u32 would be just fine, u64 is way overkill :-)

Even u16 would work, the block layer doesn't use more than an unsigned short for storing hardware sector size anyway.

Thanks, good to know. Do you think, that could change in the future? The virtio definition is going to be a public interface, so if there is a chance that u16 is not enough in the future I would respin the patch with u32, otherwise u16.

Christian

#10 lore raw
In reply to Jens Axboe

Am Donnerstag, 29. Mai 2008 schrieb Jens Axboe:

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Just that u64 seems like overkill: u32?

Definitely, u32 would be just fine, u64 is way overkill :-)

Even u16 would work, the block layer doesn't use more than an unsigned short for storing hardware sector size anyway.

Thanks, good to know. Do you think, that could change in the future? The virtio definition is going to be a public interface, so if there is a chance that u16 is not enough in the future I would respin the patch with u32, otherwise u16.

Christian

#11 lore raw
Quoted 5 lines · Christian Borntraeger

Am Donnerstag, 29. Mai 2008 schrieb Jens Axboe:

Quoted 4 lines
Quoted 3 lines
Quoted 1 line

Just that u64 seems like overkill: u32?

Definitely, u32 would be just fine, u64 is way overkill :-)

Quoted 7 lines
Quoted 2 lines

Even u16 would work, the block layer doesn't use more than an unsigned short for storing hardware sector size anyway.

Thanks, good to know. Do you think, that could change in the future? The virtio definition is going to be a public interface, so if there is a chance that u16 is not enough in the future I would respin the patch with u32, otherwise u16.

I'd say go with the u32, it's the safest option for an exported interface.

Signature · 1 line
Jens Axboe
#12 lore raw
In reply to Christian Borntraeger
Quoted 5 lines · Christian Borntraeger

Am Donnerstag, 29. Mai 2008 schrieb Jens Axboe:

Quoted 4 lines
Quoted 3 lines
Quoted 1 line

Just that u64 seems like overkill: u32?

Definitely, u32 would be just fine, u64 is way overkill :-)

Quoted 7 lines
Quoted 2 lines

Even u16 would work, the block layer doesn't use more than an unsigned short for storing hardware sector size anyway.

Thanks, good to know. Do you think, that could change in the future? The virtio definition is going to be a public interface, so if there is a chance that u16 is not enough in the future I would respin the patch with u32, otherwise u16.

I'd say go with the u32, it's the safest option for an exported interface.

Signature · 1 line
Jens Axboe
#13 lore raw

PATCH 1/1 virtio_config: fix len calculation of config elements

Rusty,

This patch is a prereq for the virtio_blk blocksize patch, please apply it first.

Adding an u32 value to the virtio_blk_config unconvered a small bug the config space defintions:
v is a pointer, to we have to use sizeof(*v) instead of sizeof(v).

---
 include/linux/virtio_config.h |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/virtio_config.h +1 -1
Index: kvm/include/linux/virtio_config.h
===================================================================
@@ -99,7 +99,7 @@ static inline bool virtio_has_feature(co
  * The return value is -ENOENT if the feature doesn't exist.  Otherwise
  * the config value is copied into whatever is pointed to by v. */
 #define virtio_config_val(vdev, fbit, offset, v) \
-	virtio_config_buf((vdev), (fbit), (offset), (v), sizeof(v))
+	virtio_config_buf((vdev), (fbit), (offset), (v), sizeof(*v))
 
 static inline int virtio_config_buf(struct virtio_device *vdev,
 				    unsigned int fbit,
#14 lore raw

PATCH 1/1 virtio_config: fix len calculation of config elements

In reply to Jens Axboe

Rusty,

This patch is a prereq for the virtio_blk blocksize patch, please apply it first.

Adding an u32 value to the virtio_blk_config unconvered a small bug the config space defintions:
v is a pointer, to we have to use sizeof(*v) instead of sizeof(v).

---
 include/linux/virtio_config.h |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/virtio_config.h +1 -1
Index: kvm/include/linux/virtio_config.h
===================================================================
@@ -99,7 +99,7 @@ static inline bool virtio_has_feature(co
  * The return value is -ENOENT if the feature doesn't exist.  Otherwise
  * the config value is copied into whatever is pointed to by v. */
 #define virtio_config_val(vdev, fbit, offset, v) \
-	virtio_config_buf((vdev), (fbit), (offset), (v), sizeof(v))
+	virtio_config_buf((vdev), (fbit), (offset), (v), sizeof(*v))
 
 static inline int virtio_config_buf(struct virtio_device *vdev,
 				    unsigned int fbit,
#17 lore raw

PATCH v2 1/1 virtio_blk: check for hardsector size from host

In reply to Jens Axboe

Currently virtio_blk assumes a 512 byte hard sector size. This can cause trouble / performance issues if the backing has a different block size (like a file on an ext3 file system formatted with 4k block size or a dasd).

Lets add a feature flag that tells the guest to use a different hard sector size than 512 byte.

---
 drivers/block/virtio_blk.c |   10 +++++++++-
 include/linux/virtio_blk.h |    3 +++
 2 files changed, 12 insertions(+), 1 deletion(-)
drivers/block/virtio_blk.c +9 -1
Index: kvm/drivers/block/virtio_blk.c
===================================================================
@@ -196,6 +196,7 @@ static int virtblk_probe(struct virtio_d
 	int err;
 	u64 cap;
 	u32 v;
+	u32 blk_size;
 
 	if (index_to_minor(index) >= 1 << MINORBITS)
 		return -ENOSPC;
@@ -290,6 +291,13 @@ static int virtblk_probe(struct virtio_d
 	if (!err)
 		blk_queue_max_hw_segments(vblk->disk->queue, v);
 
+	/* Host can optionally specify the block size of the device */
+	err = virtio_config_val(vdev, VIRTIO_BLK_F_BLK_SIZE,
+				offsetof(struct virtio_blk_config, blk_size),
+				&blk_size);
+	if (!err)
+		blk_queue_hardsect_size(vblk->disk->queue, blk_size);
+
 	add_disk(vblk->disk);
 	return 0;
 
@@ -329,7 +337,7 @@ static struct virtio_device_id id_table[
 
 static unsigned int features[] = {
 	VIRTIO_BLK_F_BARRIER, VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX,
-	VIRTIO_BLK_F_GEOMETRY, VIRTIO_BLK_F_RO,
+	VIRTIO_BLK_F_GEOMETRY, VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
 };
 
 static struct virtio_driver virtio_blk = {
include/linux/virtio_blk.h +3 -0
Index: kvm/include/linux/virtio_blk.h
===================================================================
@@ -11,6 +11,7 @@
 #define VIRTIO_BLK_F_SEG_MAX	2	/* Indicates maximum # of segments */
 #define VIRTIO_BLK_F_GEOMETRY	4	/* Legacy geometry available  */
 #define VIRTIO_BLK_F_RO		5	/* Disk is read-only */
+#define VIRTIO_BLK_F_BLK_SIZE	6	/* Block size of disk is available*/
 
 struct virtio_blk_config
 {
@@ -26,6 +27,8 @@ struct virtio_blk_config
 		__u8 heads;
 		__u8 sectors;
 	} geometry;
+	/* block size of device (if VIRTIO_BLK_F_BLK_SIZE) */
+	__u32 blk_size;
 } __attribute__((packed));
 
 /* These two define direction. */
#18 lore raw

PATCH v2 1/1 virtio_blk: check for hardsector size from host

In reply to Jens Axboe

Currently virtio_blk assumes a 512 byte hard sector size. This can cause trouble / performance issues if the backing has a different block size (like a file on an ext3 file system formatted with 4k block size or a dasd).

Lets add a feature flag that tells the guest to use a different hard sector size than 512 byte.

---
 drivers/block/virtio_blk.c |   10 +++++++++-
 include/linux/virtio_blk.h |    3 +++
 2 files changed, 12 insertions(+), 1 deletion(-)
drivers/block/virtio_blk.c +9 -1
Index: kvm/drivers/block/virtio_blk.c
===================================================================
@@ -196,6 +196,7 @@ static int virtblk_probe(struct virtio_d
 	int err;
 	u64 cap;
 	u32 v;
+	u32 blk_size;
 
 	if (index_to_minor(index) >= 1 << MINORBITS)
 		return -ENOSPC;
@@ -290,6 +291,13 @@ static int virtblk_probe(struct virtio_d
 	if (!err)
 		blk_queue_max_hw_segments(vblk->disk->queue, v);
 
+	/* Host can optionally specify the block size of the device */
+	err = virtio_config_val(vdev, VIRTIO_BLK_F_BLK_SIZE,
+				offsetof(struct virtio_blk_config, blk_size),
+				&blk_size);
+	if (!err)
+		blk_queue_hardsect_size(vblk->disk->queue, blk_size);
+
 	add_disk(vblk->disk);
 	return 0;
 
@@ -329,7 +337,7 @@ static struct virtio_device_id id_table[
 
 static unsigned int features[] = {
 	VIRTIO_BLK_F_BARRIER, VIRTIO_BLK_F_SEG_MAX, VIRTIO_BLK_F_SIZE_MAX,
-	VIRTIO_BLK_F_GEOMETRY, VIRTIO_BLK_F_RO,
+	VIRTIO_BLK_F_GEOMETRY, VIRTIO_BLK_F_RO, VIRTIO_BLK_F_BLK_SIZE,
 };
 
 static struct virtio_driver virtio_blk = {
include/linux/virtio_blk.h +3 -0
Index: kvm/include/linux/virtio_blk.h
===================================================================
@@ -11,6 +11,7 @@
 #define VIRTIO_BLK_F_SEG_MAX	2	/* Indicates maximum # of segments */
 #define VIRTIO_BLK_F_GEOMETRY	4	/* Legacy geometry available  */
 #define VIRTIO_BLK_F_RO		5	/* Disk is read-only */
+#define VIRTIO_BLK_F_BLK_SIZE	6	/* Block size of disk is available*/
 
 struct virtio_blk_config
 {
@@ -26,6 +27,8 @@ struct virtio_blk_config
 		__u8 heads;
 		__u8 sectors;
 	} geometry;
+	/* block size of device (if VIRTIO_BLK_F_BLK_SIZE) */
+	__u32 blk_size;
 } __attribute__((packed));
 
 /* These two define direction. */
#15 lore raw

Re: [PATCH] virtio_config: fix len calculation of config elements

In reply to Christian Borntraeger
Quoted 1 line · On Thursday 29 May 2008 19:08:01 Christian Borntraeger wrote:

v is a pointer, to we have to use sizeof(*v) instead of sizeof(v).

How embarassing. Applied, thanks.

Rusty.

#16 lore raw

Re: [PATCH] virtio_config: fix len calculation of config elements

In reply to Christian Borntraeger
Quoted 1 line · On Thursday 29 May 2008 19:08:01 Christian Borntraeger wrote:

v is a pointer, to we have to use sizeof(*v) instead of sizeof(v).

How embarassing. Applied, thanks.

Rusty.

#19 lore raw
In reply to Christian Borntraeger
Quoted 9 lines · On Thursday 29 May 2008 19:08:26 Christian Borntraeger wrote:

Currently virtio_blk assumes a 512 byte hard sector size. This can cause trouble / performance issues if the backing has a different block size (like a file on an ext3 file system formatted with 4k block size or a dasd).

Lets add a feature flag that tells the guest to use a different hard sector size than 512 byte.

Thanks. I've queued this for next kernel (the other two will go striaght-away).

Cheers,
Rusty.

#20 lore raw
In reply to Christian Borntraeger
Quoted 9 lines · On Thursday 29 May 2008 19:08:26 Christian Borntraeger wrote:

Currently virtio_blk assumes a 512 byte hard sector size. This can cause trouble / performance issues if the backing has a different block size (like a file on an ext3 file system formatted with 4k block size or a dasd).

Lets add a feature flag that tells the guest to use a different hard sector size than 512 byte.

Thanks. I've queued this for next kernel (the other two will go striaght-away).

Cheers,
Rusty.