Skip to contentSkip to conversation

kvm tools: Switch to using an enum for disk image types

PATCH In review Sasha Levin 2 participants 11 messages Nov 2, 2011 KVM

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/10 kvm tools: Switch to using an enum for disk image types

---
 tools/kvm/disk/qcow.c              |    8 ++++----
 tools/kvm/disk/raw.c               |    2 +-
 tools/kvm/include/kvm/disk-image.h |    7 +++++--
 3 files changed, 10 insertions(+), 7 deletions(-)
tools/kvm/disk/qcow.c +4 -4
index 167e97e..bdf6bd8 100644
@@ -1107,9 +1107,9 @@ static struct disk_image *qcow2_probe(int fd, bool readonly)
 	 * Do not use mmap use read/write instead
 	 */
 	if (readonly)
-		disk_image = disk_image__new(fd, h->size, &qcow_disk_readonly_ops, DISK_IMAGE_NOMMAP);
+		disk_image = disk_image__new(fd, h->size, &qcow_disk_readonly_ops, DISK_IMAGE_REGULAR);
 	else
-		disk_image = disk_image__new(fd, h->size, &qcow_disk_ops, DISK_IMAGE_NOMMAP);
+		disk_image = disk_image__new(fd, h->size, &qcow_disk_ops, DISK_IMAGE_REGULAR);
 
 	if (!disk_image)
 		goto free_refcount_table;
@@ -1238,9 +1238,9 @@ static struct disk_image *qcow1_probe(int fd, bool readonly)
 	 * Do not use mmap use read/write instead
 	 */
 	if (readonly)
-		disk_image = disk_image__new(fd, h->size, &qcow_disk_readonly_ops, DISK_IMAGE_NOMMAP);
+		disk_image = disk_image__new(fd, h->size, &qcow_disk_readonly_ops, DISK_IMAGE_REGULAR);
 	else
-		disk_image = disk_image__new(fd, h->size, &qcow_disk_ops, DISK_IMAGE_NOMMAP);
+		disk_image = disk_image__new(fd, h->size, &qcow_disk_ops, DISK_IMAGE_REGULAR);
 
 	if (!disk_image)
 		goto free_l1_table;
tools/kvm/disk/raw.c +1 -1
index 7f3f8db..66c52be 100644
@@ -78,5 +78,5 @@ struct disk_image *raw_image__probe(int fd, struct stat *st, bool readonly)
 		/*
 		 * Use read/write instead of mmap
 		 */
-		return disk_image__new(fd, st->st_size, &raw_image_iov_ops, DISK_IMAGE_NOMMAP);
+		return disk_image__new(fd, st->st_size, &raw_image_iov_ops, DISK_IMAGE_REGULAR);
 }
tools/kvm/include/kvm/disk-image.h +5 -2
index 75b54f9..6acde53 100644
@@ -21,8 +21,11 @@
 #define SECTOR_SHIFT		9
 #define SECTOR_SIZE		(1UL << SECTOR_SHIFT)
 
-#define DISK_IMAGE_MMAP		0
-#define DISK_IMAGE_NOMMAP	1
+enum {
+	DISK_IMAGE_REGULAR,
+	DISK_IMAGE_MMAP,
+};
+
 #define MAX_DISK_IMAGES         4
 
 struct disk_image;
Signature · 1 line
1.7.7.1
#1 lore raw

PATCH v3 2/10 kvm tools: Remove the non-iov interface from disk image ops

---
 tools/kvm/disk/core.c              |   48 ++++++----------------------------
 tools/kvm/disk/qcow.c              |   47 +++++++++++++++++++++++++++++++--
 tools/kvm/disk/raw.c               |   50 +++++++++++++++++++++---------------
 tools/kvm/include/kvm/disk-image.h |   26 +++++++-----------
 4 files changed, 91 insertions(+), 80 deletions(-)
tools/kvm/disk/core.c +8 -40
index 20e1990..78495cc 100644
@@ -134,36 +134,19 @@ void disk_image__close_all(struct disk_image **disks, int count)
 ssize_t disk_image__read(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount)
 {
 	ssize_t total = 0;
-	ssize_t nr;
 
 	if (debug_iodelay)
 		msleep(debug_iodelay);
 
-	if (disk->ops->read_sector_iov) {
-		/*
-		 * Try mulitple buffer based operation first
-		 */
-		total		= disk->ops->read_sector_iov(disk, sector, iov, iovcount);
+	if (disk->ops->read_sector) {
+		total = disk->ops->read_sector(disk, sector, iov, iovcount);
 		if (total < 0) {
 			pr_info("disk_image__read error: total=%ld\n", (long)total);
 			return -1;
 		}
-	} else if (disk->ops->read_sector) {
-		/*
-		 * Fallback to single buffer based operation
-		 */
-		while (iovcount--) {
-			nr 	= disk->ops->read_sector(disk, sector, iov->iov_base, iov->iov_len);
-			if (nr != (ssize_t)iov->iov_len) {
-				pr_info("disk_image__read error: nr = %ld iov_len=%ld\n", (long)nr, (long)iov->iov_len);
-				return -1;
-			}
-			sector	+= iov->iov_len >> SECTOR_SHIFT;
-			iov++;
-			total 	+= nr;
-		}
-	} else
+	} else {
 		die("No disk image operation for read\n");
+	}
 
 	return total;
 }
@@ -175,37 +158,22 @@ ssize_t disk_image__read(struct disk_image *disk, u64 sector, const struct iovec
 ssize_t disk_image__write(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount)
 {
 	ssize_t total = 0;
-	ssize_t nr;
 
 	if (debug_iodelay)
 		msleep(debug_iodelay);
 
-	if (disk->ops->write_sector_iov) {
+	if (disk->ops->write_sector) {
 		/*
 		 * Try writev based operation first
 		 */
-		total = disk->ops->write_sector_iov(disk, sector, iov, iovcount);
+		total = disk->ops->write_sector(disk, sector, iov, iovcount);
 		if (total < 0) {
 			pr_info("disk_image__write error: total=%ld\n", (long)total);
 			return -1;
 		}
-	} else if (disk->ops->write_sector) {
-		/*
-		 * Fallback to single buffer based operation
-		 */
-		while (iovcount--) {
-			nr	 = disk->ops->write_sector(disk, sector, iov->iov_base, iov->iov_len);
-			if (nr != (ssize_t)iov->iov_len) {
-				pr_info("disk_image__write error: nr=%ld iov_len=%ld\n", (long)nr, (long)iov->iov_len);
-				return -1;
-			}
-
-			sector	+= iov->iov_len >> SECTOR_SHIFT;
-			iov++;
-			total	+= nr;
-		}
-	} else
+	} else {
 		die("No disk image operation for read\n");
+	}
 
 	return total;
 }
tools/kvm/disk/qcow.c +44 -3

71 lines not on this page · raw message

tools/kvm/disk/raw.c +29 -21

97 lines not on this page · raw message

tools/kvm/include/kvm/disk-image.h +10 -16
index 6acde53..6a6ae6f 100644
@@ -31,18 +31,8 @@ enum {
 struct disk_image;
 
 struct disk_image_operations {
-	/*
-	 * The following two are used for reading or writing with a single buffer.
-	 * The implentation can use readv/writev or memcpy.
-	 */
-	ssize_t (*read_sector)(struct disk_image *disk, u64 sector, void *dst, u32 dst_len);
-	ssize_t (*write_sector)(struct disk_image *disk, u64 sector, void *src, u32 src_len);
-	/*
-	 * The following two are used for reading or writing with multiple buffers.
-	 * The implentation can use readv/writev or memcpy.
-	 */
-	ssize_t (*read_sector_iov)(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount);
-	ssize_t (*write_sector_iov)(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount);
+	ssize_t (*read_sector)(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount);
+	ssize_t (*write_sector)(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount);
 	int (*flush)(struct disk_image *disk);
 	int (*close)(struct disk_image *disk);
 };
@@ -67,10 +57,14 @@ ssize_t disk_image__get_serial(struct disk_image *disk, void *buffer, ssize_t *l
 struct disk_image *raw_image__probe(int fd, struct stat *st, bool readonly);
 struct disk_image *blkdev__probe(const char *filename, struct stat *st);
 
-ssize_t raw_image__read_sector_iov(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount);
-ssize_t raw_image__write_sector_iov(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount);
-ssize_t raw_image__read_sector(struct disk_image *disk, u64 sector, void *dst, u32 dst_len);
-ssize_t raw_image__write_sector(struct disk_image *disk, u64 sector, void *src, u32 src_len);
+ssize_t raw_image__read_sector(struct disk_image *disk, u64 sector,
+				const struct iovec *iov, int iovcount);
+ssize_t raw_image__write_sector(struct disk_image *disk, u64 sector,
+				const struct iovec *iov, int iovcount);
+ssize_t raw_image__read_sector_mmap(struct disk_image *disk, u64 sector,
+				const struct iovec *iov, int iovcount);
+ssize_t raw_image__write_sector_mmap(struct disk_image *disk, u64 sector,
+				const struct iovec *iov, int iovcount);
 int raw_image__close(struct disk_image *disk);
 
 #endif /* KVM__DISK_IMAGE_H */
Signature · 1 line
1.7.7.1
#2 lore raw

PATCH v3 3/10 kvm tools: Modify disk ops usage

In reply to Sasha Levin

This patch modifies the definition and usage of ops for read only, mmap and regular IO.

There is no longer a mix between iov and mmap, and read only no longer implies mmap (although it will try to use it first).

This allows for more flexibility defining different ops for different scenarios.

---
 tools/kvm/disk/core.c |    6 ++++--
 tools/kvm/disk/raw.c  |   46 ++++++++++++++++++++++++++++++----------------
 2 files changed, 34 insertions(+), 18 deletions(-)
tools/kvm/disk/core.c +4 -2
index 78495cc..9a84f39 100644
@@ -20,8 +20,10 @@ struct disk_image *disk_image__new(int fd, u64 size, struct disk_image_operation
 		 * The write to disk image will be discarded
 		 */
 		disk->priv = mmap(NULL, size, PROT_RW, MAP_PRIVATE | MAP_NORESERVE, fd, 0);
-		if (disk->priv == MAP_FAILED)
-			die("mmap() failed");
+		if (disk->priv == MAP_FAILED) {
+			free(disk);
+			disk = NULL;
+		}
 	}
 
 	return disk;
tools/kvm/disk/raw.c +30 -16

89 lines not on this page · raw message

Signature · 1 line
1.7.7.1
#3 lore raw

PATCH v3 4/10 kvm tools: Modify behaviour on missing ops ptr

In reply to Sasha Levin

In case a read or write op ptr is missing simply ignore it instead of critically failing. This provides an easier way to prevent read or write in specific scenarios.

---
 tools/kvm/disk/core.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
tools/kvm/disk/core.c +2 -2
index 9a84f39..4f51cbe 100644
@@ -147,7 +147,7 @@ ssize_t disk_image__read(struct disk_image *disk, u64 sector, const struct iovec
 			return -1;
 		}
 	} else {
-		die("No disk image operation for read\n");
+		/* Do nothing */
 	}
 
 	return total;
@@ -174,7 +174,7 @@ ssize_t disk_image__write(struct disk_image *disk, u64 sector, const struct iove
 			return -1;
 		}
 	} else {
-		die("No disk image operation for read\n");
+		/* Do nothing */
 	}
 
 	return total;
Signature · 1 line
1.7.7.1
#4 lore raw

PATCH v3 5/10 kvm tools: Add optional callback on disk op completion

In reply to Sasha Levin

This patch adds an optional callback to be called when a disk op completes.

Currently theres not much use for it, but it is the infrastructure for adding aio support.

---
 tools/kvm/disk/core.c              |   22 ++++++++++++++++++----
 tools/kvm/disk/qcow.c              |    6 +++---
 tools/kvm/disk/raw.c               |   12 ++++++++----
 tools/kvm/include/kvm/disk-image.h |   24 +++++++++++++++---------
 tools/kvm/virtio/blk.c             |    6 ++++--
 5 files changed, 48 insertions(+), 22 deletions(-)
tools/kvm/disk/core.c +18 -4
index 4f51cbe..c1cb52f 100644
@@ -133,7 +133,8 @@ void disk_image__close_all(struct disk_image **disks, int count)
  * Fill iov with disk data, starting from sector 'sector'.
  * Return amount of bytes read.
  */
-ssize_t disk_image__read(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount)
+ssize_t disk_image__read(struct disk_image *disk, u64 sector, const struct iovec *iov,
+				int iovcount, void *param)
 {
 	ssize_t total = 0;
 
@@ -141,7 +142,7 @@ ssize_t disk_image__read(struct disk_image *disk, u64 sector, const struct iovec
 		msleep(debug_iodelay);
 
 	if (disk->ops->read_sector) {
-		total = disk->ops->read_sector(disk, sector, iov, iovcount);
+		total = disk->ops->read_sector(disk, sector, iov, iovcount, param);
 		if (total < 0) {
 			pr_info("disk_image__read error: total=%ld\n", (long)total);
 			return -1;
@@ -150,6 +151,9 @@ ssize_t disk_image__read(struct disk_image *disk, u64 sector, const struct iovec
 		/* Do nothing */
 	}
 
+	if (disk->disk_req_cb)
+		disk->disk_req_cb(param);
+
 	return total;
 }
 
@@ -157,7 +161,8 @@ ssize_t disk_image__read(struct disk_image *disk, u64 sector, const struct iovec
  * Write iov to disk, starting from sector 'sector'.
  * Return amount of bytes written.
  */
-ssize_t disk_image__write(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount)
+ssize_t disk_image__write(struct disk_image *disk, u64 sector, const struct iovec *iov,
+				int iovcount, void *param)
 {
 	ssize_t total = 0;
 
@@ -168,7 +173,8 @@ ssize_t disk_image__write(struct disk_image *disk, u64 sector, const struct iove
 		/*
 		 * Try writev based operation first
 		 */
-		total = disk->ops->write_sector(disk, sector, iov, iovcount);
+
+		total = disk->ops->write_sector(disk, sector, iov, iovcount, param);
 		if (total < 0) {
 			pr_info("disk_image__write error: total=%ld\n", (long)total);
 			return -1;
@@ -177,6 +183,9 @@ ssize_t disk_image__write(struct disk_image *disk, u64 sector, const struct iove
 		/* Do nothing */
 	}
 
+	if (disk->disk_req_cb)
+		disk->disk_req_cb(param);
+
 	return total;
 }
 
@@ -190,3 +199,8 @@ ssize_t disk_image__get_serial(struct disk_image *disk, void *buffer, ssize_t *l
 	*len = snprintf(buffer, *len, "%llu%llu%llu", (u64)st.st_dev, (u64)st.st_rdev, (u64)st.st_ino);
 	return *len;
 }
+
+void disk_image__set_callback(struct disk_image *disk, void (*disk_req_cb)(void *param))
+{
+	disk->disk_req_cb = disk_req_cb;
+}
tools/kvm/disk/qcow.c +3 -3
index 288782e..ba65ab6 100644
@@ -489,7 +489,7 @@ static ssize_t qcow_read_sector_single(struct disk_image *disk, u64 sector,
 }
 
 static ssize_t qcow_read_sector(struct disk_image *disk, u64 sector,
-				const struct iovec *iov, int iovcount)
+				const struct iovec *iov, int iovcount, void *param)
 {
 	ssize_t nr, total = 0;
 
@@ -917,7 +917,7 @@ static ssize_t qcow_write_sector_single(struct disk_image *disk, u64 sector, voi
 }
 
 static ssize_t qcow_write_sector(struct disk_image *disk, u64 sector,
-				const struct iovec *iov, int iovcount)
+				const struct iovec *iov, int iovcount, void *param)
 {
 	ssize_t nr, total = 0;
 
@@ -937,7 +937,7 @@ static ssize_t qcow_write_sector(struct disk_image *disk, u64 sector,
 }
 
 static ssize_t qcow_nowrite_sector(struct disk_image *disk, u64 sector,
-				const struct iovec *iov, int iovcount)
+				const struct iovec *iov, int iovcount, void *param)
 {
 	/* I/O error */
 	pr_info("%s: no write support\n", __func__);
tools/kvm/disk/raw.c +8 -4
index 7255eb4..51e8318 100644
@@ -1,20 +1,23 @@
 #include "kvm/disk-image.h"
 
-ssize_t raw_image__read_sector(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount)
+ssize_t raw_image__read_sector(struct disk_image *disk, u64 sector, const struct iovec *iov,
+				int iovcount, void *param)
 {
 	u64 offset = sector << SECTOR_SHIFT;
 
 	return preadv_in_full(disk->fd, iov, iovcount, offset);
 }
 
-ssize_t raw_image__write_sector(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount)
+ssize_t raw_image__write_sector(struct disk_image *disk, u64 sector, const struct iovec *iov,
+				int iovcount, void *param)
 {
 	u64 offset = sector << SECTOR_SHIFT;
 
 	return pwritev_in_full(disk->fd, iov, iovcount, offset);
 }
 
-ssize_t raw_image__read_sector_mmap(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount)
+ssize_t raw_image__read_sector_mmap(struct disk_image *disk, u64 sector, const struct iovec *iov,
+				int iovcount, void *param)
 {
 	u64 offset = sector << SECTOR_SHIFT;
 	ssize_t total = 0;
@@ -31,7 +34,8 @@ ssize_t raw_image__read_sector_mmap(struct disk_image *disk, u64 sector, const s
 	return total;
 }
 
-ssize_t raw_image__write_sector_mmap(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount)
+ssize_t raw_image__write_sector_mmap(struct disk_image *disk, u64 sector, const struct iovec *iov,
+				int iovcount, void *param)
 {
 	u64 offset = sector << SECTOR_SHIFT;
 	ssize_t total = 0;
tools/kvm/include/kvm/disk-image.h +15 -9
index 6a6ae6f..f22213f 100644
@@ -31,8 +31,10 @@ enum {
 struct disk_image;
 
 struct disk_image_operations {
-	ssize_t (*read_sector)(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount);
-	ssize_t (*write_sector)(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount);
+	ssize_t (*read_sector)(struct disk_image *disk, u64 sector, const struct iovec *iov,
+				int iovcount, void *param);
+	ssize_t (*write_sector)(struct disk_image *disk, u64 sector, const struct iovec *iov,
+				int iovcount, void *param);
 	int (*flush)(struct disk_image *disk);
 	int (*close)(struct disk_image *disk);
 };
@@ -42,6 +44,8 @@ struct disk_image {
 	u64				size;
 	struct disk_image_operations	*ops;
 	void				*priv;
+	void				*disk_req_cb_param;
+	void				(*disk_req_cb)(void *param);
 };
 
 struct disk_image *disk_image__open(const char *filename, bool readonly);
@@ -50,21 +54,23 @@ struct disk_image *disk_image__new(int fd, u64 size, struct disk_image_operation
 int disk_image__close(struct disk_image *disk);
 void disk_image__close_all(struct disk_image **disks, int count);
 int disk_image__flush(struct disk_image *disk);
-ssize_t disk_image__read(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount);
-ssize_t disk_image__write(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount);
+ssize_t disk_image__read(struct disk_image *disk, u64 sector, const struct iovec *iov,
+				int iovcount, void *param);
+ssize_t disk_image__write(struct disk_image *disk, u64 sector, const struct iovec *iov,
+				int iovcount, void *param);
 ssize_t disk_image__get_serial(struct disk_image *disk, void *buffer, ssize_t *len);
 
 struct disk_image *raw_image__probe(int fd, struct stat *st, bool readonly);
 struct disk_image *blkdev__probe(const char *filename, struct stat *st);
 
 ssize_t raw_image__read_sector(struct disk_image *disk, u64 sector,
-				const struct iovec *iov, int iovcount);
+				const struct iovec *iov, int iovcount, void *param);
 ssize_t raw_image__write_sector(struct disk_image *disk, u64 sector,
-				const struct iovec *iov, int iovcount);
+				const struct iovec *iov, int iovcount, void *param);
 ssize_t raw_image__read_sector_mmap(struct disk_image *disk, u64 sector,
-				const struct iovec *iov, int iovcount);
+				const struct iovec *iov, int iovcount, void *param);
 ssize_t raw_image__write_sector_mmap(struct disk_image *disk, u64 sector,
-				const struct iovec *iov, int iovcount);
+				const struct iovec *iov, int iovcount, void *param);
 int raw_image__close(struct disk_image *disk);
-
+void disk_image__set_callback(struct disk_image *disk, void (*disk_req_cb)(void *param));
 #endif /* KVM__DISK_IMAGE_H */
tools/kvm/virtio/blk.c +4 -2
index 6ffa753..9db293e 100644
@@ -77,10 +77,12 @@ static void virtio_blk_do_io_request(struct kvm *kvm, void *param)
 
 	switch (req->type) {
 	case VIRTIO_BLK_T_IN:
-		block_cnt	= disk_image__read(bdev->disk, req->sector, iov + 1, in + out - 2);
+		block_cnt	= disk_image__read(bdev->disk, req->sector, iov + 1,
+					in + out - 2, NULL);
 		break;
 	case VIRTIO_BLK_T_OUT:
-		block_cnt	= disk_image__write(bdev->disk, req->sector, iov + 1, in + out - 2);
+		block_cnt	= disk_image__write(bdev->disk, req->sector, iov + 1,
+					in + out - 2, NULL);
 		break;
 	case VIRTIO_BLK_T_FLUSH:
 		block_cnt       = disk_image__flush(bdev->disk);
Signature · 1 line
1.7.7.1
#5 lore raw

PATCH v3 6/10 kvm tools: Remove qcow nowrite function

In reply to Sasha Levin

It is no longer needed due to previous changes.

---
 tools/kvm/disk/qcow.c |    9 ---------
 1 files changed, 0 insertions(+), 9 deletions(-)
tools/kvm/disk/qcow.c +0 -9
index ba65ab6..f9598de 100644
@@ -936,14 +936,6 @@ static ssize_t qcow_write_sector(struct disk_image *disk, u64 sector,
 	return total;
 }
 
-static ssize_t qcow_nowrite_sector(struct disk_image *disk, u64 sector,
-				const struct iovec *iov, int iovcount, void *param)
-{
-	/* I/O error */
-	pr_info("%s: no write support\n", __func__);
-	return -1;
-}
-
 static int qcow_disk_flush(struct disk_image *disk)
 {
 	struct qcow *q = disk->priv;
@@ -1013,7 +1005,6 @@ static int qcow_disk_close(struct disk_image *disk)
 
 static struct disk_image_operations qcow_disk_readonly_ops = {
 	.read_sector		= qcow_read_sector,
-	.write_sector		= qcow_nowrite_sector,
 	.close			= qcow_disk_close,
 };
 
Signature · 1 line
1.7.7.1
#6 lore raw

PATCH v3 7/10 kvm tools: Split io request from completion

In reply to Sasha Levin

This patch splits IO request processing from completion notification.

---
 tools/kvm/disk/core.c              |    6 +-
 tools/kvm/include/kvm/disk-image.h |    4 +-
 tools/kvm/include/kvm/virtio-blk.h |    1 +
 tools/kvm/virtio/blk.c             |  111 ++++++++++++++++++++++++------------
 4 files changed, 81 insertions(+), 41 deletions(-)
tools/kvm/disk/core.c +3 -3
index c1cb52f..a5e6713 100644
@@ -152,7 +152,7 @@ ssize_t disk_image__read(struct disk_image *disk, u64 sector, const struct iovec
 	}
 
 	if (disk->disk_req_cb)
-		disk->disk_req_cb(param);
+		disk->disk_req_cb(param, total);
 
 	return total;
 }
@@ -184,7 +184,7 @@ ssize_t disk_image__write(struct disk_image *disk, u64 sector, const struct iove
 	}
 
 	if (disk->disk_req_cb)
-		disk->disk_req_cb(param);
+		disk->disk_req_cb(param, total);
 
 	return total;
 }
@@ -200,7 +200,7 @@ ssize_t disk_image__get_serial(struct disk_image *disk, void *buffer, ssize_t *l
 	return *len;
 }
 
-void disk_image__set_callback(struct disk_image *disk, void (*disk_req_cb)(void *param))
+void disk_image__set_callback(struct disk_image *disk, void (*disk_req_cb)(void *param, long len))
 {
 	disk->disk_req_cb = disk_req_cb;
 }
tools/kvm/include/kvm/disk-image.h +2 -2
index f22213f..632e54d 100644
@@ -45,7 +45,7 @@ struct disk_image {
 	struct disk_image_operations	*ops;
 	void				*priv;
 	void				*disk_req_cb_param;
-	void				(*disk_req_cb)(void *param);
+	void				(*disk_req_cb)(void *param, long len);
 };
 
 struct disk_image *disk_image__open(const char *filename, bool readonly);
@@ -72,5 +72,5 @@ ssize_t raw_image__read_sector_mmap(struct disk_image *disk, u64 sector,
 ssize_t raw_image__write_sector_mmap(struct disk_image *disk, u64 sector,
 				const struct iovec *iov, int iovcount, void *param);
 int raw_image__close(struct disk_image *disk);
-void disk_image__set_callback(struct disk_image *disk, void (*disk_req_cb)(void *param));
+void disk_image__set_callback(struct disk_image *disk, void (*disk_req_cb)(void *param, long len));
 #endif /* KVM__DISK_IMAGE_H */
tools/kvm/include/kvm/virtio-blk.h +1 -0
index 8c4fb91..63731a9 100644
@@ -8,5 +8,6 @@ struct kvm;
 void virtio_blk__init(struct kvm *kvm, struct disk_image *disk);
 void virtio_blk__init_all(struct kvm *kvm);
 void virtio_blk__delete_all(struct kvm *kvm);
+void virtio_blk_complete(void *param, long len);
 
 #endif /* KVM__BLK_VIRTIO_H */
tools/kvm/virtio/blk.c +75 -36

186 lines not on this page · raw message

Signature · 1 line
1.7.7.1
#7 lore raw

PATCH v3 8/10 kvm tools: Hook virtio-blk completion to disk op completion

In reply to Sasha Levin

This patch connects the completion processing in virtio-blk to the completion notification coming from disk image.

---
 tools/kvm/virtio/blk.c |   10 ++++++----
 1 files changed, 6 insertions(+), 4 deletions(-)
tools/kvm/virtio/blk.c +6 -4
index be53111..5969d27 100644
@@ -119,26 +119,26 @@ static void virtio_blk_do_io_request(struct kvm *kvm, struct blk_dev_req *req)
 	switch (req_hdr->type) {
 	case VIRTIO_BLK_T_IN:
 		block_cnt	= disk_image__read(bdev->disk, req_hdr->sector, iov + 1,
-					in + out - 2, NULL);
+					in + out - 2, req);
 		break;
 	case VIRTIO_BLK_T_OUT:
 		block_cnt	= disk_image__write(bdev->disk, req_hdr->sector, iov + 1,
-					in + out - 2, NULL);
+					in + out - 2, req);
 		break;
 	case VIRTIO_BLK_T_FLUSH:
 		block_cnt       = disk_image__flush(bdev->disk);
+		virtio_blk_complete(req, block_cnt);
 		break;
 	case VIRTIO_BLK_T_GET_ID:
 		block_cnt	= VIRTIO_BLK_ID_BYTES;
 		disk_image__get_serial(bdev->disk, (iov + 1)->iov_base, &block_cnt);
+		virtio_blk_complete(req, block_cnt);
 		break;
 	default:
 		pr_warning("request type %d", req_hdr->type);
 		block_cnt	= -1;
 		break;
 	}
-
-	virtio_blk_complete(req, block_cnt);
 }
 
 static void virtio_blk_do_io(struct kvm *kvm, struct virt_queue *vq, struct blk_dev *bdev)
@@ -261,6 +261,8 @@ void virtio_blk__init(struct kvm *kvm, struct disk_image *disk)
 	for (i = 0; i < ARRAY_SIZE(bdev->reqs); i++)
 		list_add(&bdev->reqs[i].list, &bdev->req_list);
 
+	disk_image__set_callback(bdev->disk, virtio_blk_complete);
+
 	if (compat_id != -1)
 		compat_id = compat__add_message("virtio-blk device was not detected",
 						"While you have requested a virtio-blk device, "
Signature · 1 line
1.7.7.1
#8 lore raw

PATCH v3 9/10 kvm tools: Add aio read write functions

In reply to Sasha Levin

This patch adds basic native vectored AIO functions.

These functions should be optimized to process multiple io requests at once.

---
 tools/kvm/Makefile                 |    7 +++++++
 tools/kvm/config/feature-tests.mak |   10 ++++++++++
 tools/kvm/include/kvm/read-write.h |   11 +++++++++++
 tools/kvm/read-write.c             |   26 ++++++++++++++++++++++++++
 4 files changed, 54 insertions(+), 0 deletions(-)
tools/kvm/Makefile +7 -0
index d9baa69..d4598ca 100644
@@ -115,6 +115,13 @@ ifeq ($(has_ZLIB),y)
 	LIBS	+= -lz
 endif
 
+FLAGS_AIO := $(CFLAGS) -laio
+has_AIO := $(call try-cc,$(SOURCE_AIO),$(FLAGS_AIO))
+ifeq ($(has_AIO),y)
+	CFLAGS	+= -DCONFIG_HAS_AIO
+	LIBS	+= -laio
+endif
+
 DEPS	:= $(patsubst %.o,%.d,$(OBJS))
 
 # Exclude BIOS object files from header dependencies.
tools/kvm/config/feature-tests.mak +10 -0
index a75c31d..be9ebc0 100644
@@ -156,3 +156,13 @@ int main(void)
 	return 0;
 }
 endef
+
+define SOURCE_AIO
+#include <libaio.h>
+
+int main(void)
+{
+	io_setup(0, NULL);
+	return 0;
+}
+endef
tools/kvm/include/kvm/read-write.h +11 -0
index 3351103..67571f9 100644
@@ -5,6 +5,10 @@
 #include <sys/uio.h>
 #include <unistd.h>
 
+#ifdef CONFIG_HAS_AIO
+#include <libaio.h>
+#endif
+
 ssize_t xread(int fd, void *buf, size_t count);
 ssize_t xwrite(int fd, const void *buf, size_t count);
 
@@ -29,4 +33,11 @@ ssize_t xpwritev(int fd, const struct iovec *iov, int iovcnt, off_t offset);
 ssize_t preadv_in_full(int fd, const struct iovec *iov, int iovcnt, off_t offset);
 ssize_t pwritev_in_full(int fd, const struct iovec *iov, int iovcnt, off_t offset);
 
+#ifdef CONFIG_HAS_AIO
+int aio_preadv(io_context_t ctx, struct iocb *iocb, int fd, const struct iovec *iov, int iovcnt,
+		off_t offset, int ev, void *param);
+int aio_pwritev(io_context_t ctx, struct iocb *iocb, int fd, const struct iovec *iov, int iovcnt,
+		off_t offset, int ev, void *param);
+#endif
+
 #endif /* KVM_READ_WRITE_H */
tools/kvm/read-write.c +26 -0
index 737fb26..55473ba 100644
@@ -316,3 +316,29 @@ ssize_t pwritev_in_full(int fd, const struct iovec *iov, int iovcnt, off_t offse
 
 	return total;
 }
+
+#ifdef CONFIG_HAS_AIO
+int aio_pwritev(io_context_t ctx, struct iocb *iocb, int fd, const struct iovec *iov, int iovcnt,
+		off_t offset, int ev, void *param)
+{
+	struct iocb *ios[1] = { iocb };
+
+	io_prep_pwritev(iocb, fd, iov, iovcnt, offset);
+	io_set_eventfd(iocb, ev);
+	iocb->data = param;
+
+	return io_submit(ctx, 1, ios);
+}
+
+int aio_preadv(io_context_t ctx, struct iocb *iocb, int fd, const struct iovec *iov, int iovcnt,
+		off_t offset, int ev, void *param)
+{
+	struct iocb *ios[1] = { iocb };
+
+	io_prep_preadv(iocb, fd, iov, iovcnt, offset);
+	io_set_eventfd(iocb, ev);
+	iocb->data = param;
+
+	return io_submit(ctx, 1, ios);
+}
+#endif
\ No newline at end of file
Signature · 1 line
1.7.7.1
#9 lore raw

PATCH v3 10/10 kvm tools: Use native vectored AIO in virtio-blk

In reply to Sasha Levin

This patch hooks AIO support into virtio-blk, allowing for faster IO.

---
 tools/kvm/disk/core.c              |   39 ++++++++++++++++++++++++++++++++++-
 tools/kvm/disk/qcow.c              |    4 +++
 tools/kvm/disk/raw.c               |   36 +++++++++++++++++++++++++++++++-
 tools/kvm/include/kvm/disk-image.h |    5 ++++
 tools/kvm/virtio/blk.c             |   16 +++++---------
 5 files changed, 86 insertions(+), 14 deletions(-)
tools/kvm/disk/core.c +37 -2
index a5e6713..cc2a1f6 100644
@@ -1,8 +1,32 @@
 #include "kvm/disk-image.h"
 #include "kvm/qcow.h"
+#include "kvm/virtio-blk.h"
+
+#include <sys/eventfd.h>
+#include <sys/poll.h>
+
+#define AIO_MAX 32
 
 int debug_iodelay;
 
+#ifdef CONFIG_HAS_AIO
+static void *disk_image__thread(void *param)
+{
+	struct disk_image *disk = param;
+	u64 dummy;
+
+	while (read(disk->evt, &dummy, sizeof(dummy)) > 0) {
+		struct io_event event;
+		struct timespec notime = {0};
+
+		while (io_getevents(disk->ctx, 1, 1, &event, &notime) > 0)
+			disk->disk_req_cb(event.data, event.res);
+	}
+
+	return NULL;
+}
+#endif
+
 struct disk_image *disk_image__new(int fd, u64 size, struct disk_image_operations *ops, int use_mmap)
 {
 	struct disk_image *disk;
@@ -26,6 +50,16 @@ struct disk_image *disk_image__new(int fd, u64 size, struct disk_image_operation
 		}
 	}
 
+#ifdef CONFIG_HAS_AIO
+	if (disk) {
+		pthread_t thread;
+
+		disk->evt = eventfd(0, 0);
+		io_setup(AIO_MAX, &disk->ctx);
+		if (pthread_create(&thread, NULL, disk_image__thread, disk) != 0)
+			die("Failed starting IO thread");
+	}
+#endif
 	return disk;
 }
 
@@ -87,6 +121,7 @@ struct disk_image **disk_image__open_all(const char **filenames, bool *readonly,
 			goto error;
 		}
 	}
+
 	return disks;
 error:
 	for (i = 0; i < count; i++)
@@ -151,7 +186,7 @@ ssize_t disk_image__read(struct disk_image *disk, u64 sector, const struct iovec
 		/* Do nothing */
 	}
 
-	if (disk->disk_req_cb)
+	if (!disk->async && disk->disk_req_cb)
 		disk->disk_req_cb(param, total);
 
 	return total;
@@ -183,7 +218,7 @@ ssize_t disk_image__write(struct disk_image *disk, u64 sector, const struct iove
 		/* Do nothing */
 	}
 
-	if (disk->disk_req_cb)
+	if (!disk->async && disk->disk_req_cb)
 		disk->disk_req_cb(param, total);
 
 	return total;
tools/kvm/disk/qcow.c +4 -0
index f9598de..87385df 100644
@@ -1145,6 +1145,8 @@ static struct disk_image *qcow2_probe(int fd, bool readonly)
 
 	if (!disk_image)
 		goto free_refcount_table;
+
+	disk_image->async = 1;
 	disk_image->priv = q;
 
 	return disk_image;
@@ -1276,6 +1278,8 @@ static struct disk_image *qcow1_probe(int fd, bool readonly)
 
 	if (!disk_image)
 		goto free_l1_table;
+
+	disk_image->async = 1;
 	disk_image->priv = q;
 
 	return disk_image;
tools/kvm/disk/raw.c +34 -2
index 51e8318..e979046 100644
@@ -1,11 +1,20 @@
 #include "kvm/disk-image.h"
 
+#include <libaio.h>
+
 ssize_t raw_image__read_sector(struct disk_image *disk, u64 sector, const struct iovec *iov,
 				int iovcount, void *param)
 {
 	u64 offset = sector << SECTOR_SHIFT;
 
+#ifdef CONFIG_HAS_AIO
+	struct iocb iocb;
+
+	return aio_preadv(disk->ctx, &iocb, disk->fd, iov, iovcount, offset,
+				disk->evt, param);
+#else
 	return preadv_in_full(disk->fd, iov, iovcount, offset);
+#endif
 }
 
 ssize_t raw_image__write_sector(struct disk_image *disk, u64 sector, const struct iovec *iov,
@@ -13,7 +22,14 @@ ssize_t raw_image__write_sector(struct disk_image *disk, u64 sector, const struc
 {
 	u64 offset = sector << SECTOR_SHIFT;
 
+#ifdef CONFIG_HAS_AIO
+	struct iocb iocb;
+
+	return aio_pwritev(disk->ctx, &iocb, disk->fd, iov, iovcount, offset,
+				disk->evt, param);
+#else
 	return pwritev_in_full(disk->fd, iov, iovcount, offset);
+#endif
 }
 
 ssize_t raw_image__read_sector_mmap(struct disk_image *disk, u64 sector, const struct iovec *iov,
@@ -59,6 +75,12 @@ int raw_image__close(struct disk_image *disk)
 	if (disk->priv != MAP_FAILED)
 		ret = munmap(disk->priv, disk->size);
 
+	close(disk->evt);
+
+#ifdef CONFIG_HAS_VIRTIO
+	io_destroy(disk->ctx);
+#endif
+
 	return ret;
 }
 
@@ -83,6 +105,7 @@ struct disk_image_operations ro_ops_nowrite = {
 
 struct disk_image *raw_image__probe(int fd, struct stat *st, bool readonly)
 {
+	struct disk_image *disk;
 
 	if (readonly) {
 		/*
@@ -94,8 +117,12 @@ struct disk_image *raw_image__probe(int fd, struct stat *st, bool readonly)
 		disk = disk_image__new(fd, st->st_size, &ro_ops, DISK_IMAGE_MMAP);
 		if (disk == NULL) {
 			ro_ops = raw_image_regular_ops;
-			ro_ops.write_sector = NULL;
+
 			disk = disk_image__new(fd, st->st_size, &ro_ops_nowrite, DISK_IMAGE_REGULAR);
+#ifdef CONFIG_HAS_VIRTIO
+			if (disk)
+				disk->async = 1;
+#endif
 		}
 
 		return disk;
@@ -103,6 +130,11 @@ struct disk_image *raw_image__probe(int fd, struct stat *st, bool readonly)
 		/*
 		 * Use read/write instead of mmap
 		 */
-		return disk_image__new(fd, st->st_size, &raw_image_regular_ops, DISK_IMAGE_REGULAR);
+		disk = disk_image__new(fd, st->st_size, &raw_image_regular_ops, DISK_IMAGE_REGULAR);
+#ifdef CONFIG_HAS_VIRTIO
+		if (disk)
+			disk->async = 1;
+#endif
+		return disk;
 	}
 }
tools/kvm/include/kvm/disk-image.h +5 -0
index 632e54d..56c08da 100644
@@ -46,6 +46,11 @@ struct disk_image {
 	void				*priv;
 	void				*disk_req_cb_param;
 	void				(*disk_req_cb)(void *param, long len);
+	bool				async;
+	int				evt;
+#ifdef CONFIG_HAS_AIO
+	io_context_t			ctx;
+#endif
 };
 
 struct disk_image *disk_image__open(const char *filename, bool readonly);
tools/kvm/virtio/blk.c +6 -10
index 5969d27..223e79f 100644
@@ -2,7 +2,6 @@
 
 #include "kvm/virtio-pci-dev.h"
 #include "kvm/disk-image.h"
-#include "kvm/virtio.h"
 #include "kvm/mutex.h"
 #include "kvm/util.h"
 #include "kvm/kvm.h"
@@ -11,23 +10,23 @@
 #include "kvm/ioeventfd.h"
 #include "kvm/guest_compat.h"
 #include "kvm/virtio-pci.h"
+#include "kvm/virtio.h"
 
 #include <linux/virtio_ring.h>
 #include <linux/virtio_blk.h>
-
 #include <linux/kernel.h>
 #include <linux/list.h>
 #include <linux/types.h>
 #include <pthread.h>
 
 #define VIRTIO_BLK_MAX_DEV		4
-#define NUM_VIRT_QUEUES			1
 
-#define VIRTIO_BLK_QUEUE_SIZE		128
 /*
  * the header and status consume too entries
  */
 #define DISK_SEG_MAX			(VIRTIO_BLK_QUEUE_SIZE - 2)
+#define VIRTIO_BLK_QUEUE_SIZE		128
+#define NUM_VIRT_QUEUES			1
 
 struct blk_dev_req {
 	struct list_head		list;
@@ -86,8 +85,8 @@ void virtio_blk_complete(void *param, long len)
 	u8 *status;
 
 	/* status */
-	status			= req->iov[req->out + req->in - 1].iov_base;
-	*status			= (len < 0) ? VIRTIO_BLK_S_IOERR : VIRTIO_BLK_S_OK;
+	status	= req->iov[req->out + req->in - 1].iov_base;
+	*status	= (len < 0) ? VIRTIO_BLK_S_IOERR : VIRTIO_BLK_S_OK;
 
 	mutex_lock(&bdev->mutex);
 	virt_queue__set_used_elem(req->vq, req->head, len);
@@ -103,17 +102,14 @@ static void virtio_blk_do_io_request(struct kvm *kvm, struct blk_dev_req *req)
 	struct virtio_blk_outhdr *req_hdr;
 	ssize_t block_cnt;
 	struct blk_dev *bdev;
-	struct virt_queue *queue;
 	struct iovec *iov;
-	u16 out, in, head;
+	u16 out, in;
 
 	block_cnt	= -1;
 	bdev		= req->bdev;
-	queue		= req->vq;
 	iov		= req->iov;
 	out		= req->out;
 	in		= req->in;
-	head		= req->head;
 	req_hdr		= iov[0].iov_base;
 
 	switch (req_hdr->type) {
Signature · 1 line
1.7.7.1
#10 lore raw

Re: [PATCH v3 10/10] kvm tools: Use native vectored AIO in virtio-blk

Quoted 3 lines · Sasha Levin

This patch hooks AIO support into virtio-blk, allowing for faster IO.

Applied the whole series, thanks a lot Sasha!