From patchwork Thu Apr 4 13:20:45 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Philippe Brucker X-Patchwork-Id: 10885609 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id BE47B1708 for ; Thu, 4 Apr 2019 13:22:21 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A506A286C0 for ; Thu, 4 Apr 2019 13:22:21 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 99054286DD; Thu, 4 Apr 2019 13:22:21 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AB45C286C0 for ; Thu, 4 Apr 2019 13:22:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729511AbfDDNWT (ORCPT ); Thu, 4 Apr 2019 09:22:19 -0400 Received: from foss.arm.com ([217.140.101.70]:60218 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729275AbfDDNWS (ORCPT ); Thu, 4 Apr 2019 09:22:18 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 4E5131715; Thu, 4 Apr 2019 06:22:18 -0700 (PDT) Received: from ostrya.cambridge.arm.com (ostrya.cambridge.arm.com [10.1.196.129]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 855F93F68F; Thu, 4 Apr 2019 06:22:17 -0700 (PDT) From: Jean-Philippe Brucker To: Will.Deacon@arm.com Cc: Andre.Przywara@arm.com, kvm@vger.kernel.org Subject: [PATCH kvmtool v2 4/9] disk/aio: Refactor AIO code Date: Thu, 4 Apr 2019 14:20:45 +0100 Message-Id: <20190404132050.37309-5-jean-philippe.brucker@arm.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190404132050.37309-1-jean-philippe.brucker@arm.com> References: <20190404132050.37309-1-jean-philippe.brucker@arm.com> MIME-Version: 1.0 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Move all AIO code to a separate file, disk/aio.c, to remove as much #ifdefs as possible. Split the raw read/write disk ops into async and sync, and choose which ones to use depending on CONFIG_HAS_AIO. Note that we fix raw_image__close() which incorrectly checked CONFIG_HAS_VIRTIO instead of CONFIG_HAS_AIO, and closed an unitialized disk->evt. A subsequent commit will complete this refactoring by fixing use of the 'async' disk attribute. Reviewed-by: Andre Przywara Signed-off-by: Jean-Philippe Brucker --- Makefile | 2 + disk/aio.c | 111 +++++++++++++++++++++++++++++++++++++++ disk/core.c | 52 +++++------------- disk/raw.c | 39 +++----------- include/kvm/disk-image.h | 41 ++++++++++++--- include/kvm/read-write.h | 11 ---- util/read-write.c | 36 ------------- 7 files changed, 167 insertions(+), 125 deletions(-) create mode 100644 disk/aio.c diff --git a/Makefile b/Makefile index ec75cd999..c0881252a 100644 --- a/Makefile +++ b/Makefile @@ -275,10 +275,12 @@ endif ifeq ($(call try-build,$(SOURCE_AIO),$(CFLAGS),$(LDFLAGS) -laio),y) CFLAGS_DYNOPT += -DCONFIG_HAS_AIO LIBS_DYNOPT += -laio + OBJS_DYNOPT += disk/aio.o else ifeq ($(call try-build,$(SOURCE_AIO),$(CFLAGS),$(LDFLAGS) -laio -static),y) CFLAGS_STATOPT += -DCONFIG_HAS_AIO LIBS_STATOPT += -laio + OBJS_STATOPT += disk/aio.o else NOTFOUND += aio endif diff --git a/disk/aio.c b/disk/aio.c new file mode 100644 index 000000000..6afcffe5a --- /dev/null +++ b/disk/aio.c @@ -0,0 +1,111 @@ +#include +#include +#include + +#include "kvm/disk-image.h" +#include "kvm/kvm.h" +#include "linux/list.h" + +#define AIO_MAX 256 + +static 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 }; + int ret; + + io_prep_pwritev(iocb, fd, iov, iovcnt, offset); + io_set_eventfd(iocb, ev); + iocb->data = param; + +restart: + ret = io_submit(ctx, 1, ios); + if (ret == -EAGAIN) + goto restart; + return ret; +} + +static 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 }; + int ret; + + io_prep_preadv(iocb, fd, iov, iovcnt, offset); + io_set_eventfd(iocb, ev); + iocb->data = param; + +restart: + ret = io_submit(ctx, 1, ios); + if (ret == -EAGAIN) + goto restart; + return ret; +} + +ssize_t raw_image__read_async(struct disk_image *disk, u64 sector, + const struct iovec *iov, int iovcount, + void *param) +{ + u64 offset = sector << SECTOR_SHIFT; + struct iocb iocb; + + return aio_preadv(disk->ctx, &iocb, disk->fd, iov, iovcount, + offset, disk->evt, param); +} + +ssize_t raw_image__write_async(struct disk_image *disk, u64 sector, + const struct iovec *iov, int iovcount, + void *param) +{ + u64 offset = sector << SECTOR_SHIFT; + struct iocb iocb; + + return aio_pwritev(disk->ctx, &iocb, disk->fd, iov, iovcount, + offset, disk->evt, param); +} + +static void *disk_aio_thread(void *param) +{ + struct disk_image *disk = param; + struct io_event event[AIO_MAX]; + struct timespec notime = {0}; + int nr, i; + u64 dummy; + + kvm__set_thread_name("disk-image-io"); + + while (read(disk->evt, &dummy, sizeof(dummy)) > 0) { + nr = io_getevents(disk->ctx, 1, ARRAY_SIZE(event), event, ¬ime); + for (i = 0; i < nr; i++) + disk->disk_req_cb(event[i].data, event[i].res); + } + + return NULL; +} + +int disk_aio_setup(struct disk_image *disk) +{ + int r; + pthread_t thread; + + disk->evt = eventfd(0, 0); + if (disk->evt < 0) + return -errno; + + io_setup(AIO_MAX, &disk->ctx); + r = pthread_create(&thread, NULL, disk_aio_thread, disk); + if (r) { + r = -errno; + close(disk->evt); + return r; + } + return 0; +} + +void disk_aio_destroy(struct disk_image *disk) +{ + close(disk->evt); + io_destroy(disk->ctx); +} diff --git a/disk/core.c b/disk/core.c index 4c7c4f030..89880703e 100644 --- a/disk/core.c +++ b/disk/core.c @@ -4,11 +4,8 @@ #include "kvm/kvm.h" #include -#include #include -#define AIO_MAX 256 - int debug_iodelay; static int disk_image__close(struct disk_image *disk); @@ -54,27 +51,6 @@ int disk_img_name_parser(const struct option *opt, const char *arg, int unset) return 0; } -#ifdef CONFIG_HAS_AIO -static void *disk_image__thread(void *param) -{ - struct disk_image *disk = param; - struct io_event event[AIO_MAX]; - struct timespec notime = {0}; - int nr, i; - u64 dummy; - - kvm__set_thread_name("disk-image-io"); - - while (read(disk->evt, &dummy, sizeof(dummy)) > 0) { - nr = io_getevents(disk->ctx, 1, ARRAY_SIZE(event), event, ¬ime); - for (i = 0; i < nr; i++) - disk->disk_req_cb(event[i].data, event[i].res); - } - - return NULL; -} -#endif - struct disk_image *disk_image__new(int fd, u64 size, struct disk_image_operations *ops, int use_mmap) @@ -99,26 +75,22 @@ struct disk_image *disk_image__new(int fd, u64 size, disk->priv = mmap(NULL, size, PROT_RW, MAP_PRIVATE | MAP_NORESERVE, fd, 0); if (disk->priv == MAP_FAILED) { r = -errno; - free(disk); - return ERR_PTR(r); + goto err_free_disk; } } -#ifdef CONFIG_HAS_AIO - { - pthread_t thread; + r = disk_aio_setup(disk); + if (r) + goto err_unmap_disk; - disk->evt = eventfd(0, 0); - io_setup(AIO_MAX, &disk->ctx); - r = pthread_create(&thread, NULL, disk_image__thread, disk); - if (r) { - r = -errno; - free(disk); - return ERR_PTR(r); - } - } -#endif return disk; + +err_unmap_disk: + if (disk->priv) + munmap(disk->priv, size); +err_free_disk: + free(disk); + return ERR_PTR(r); } static struct disk_image *disk_image__open(const char *filename, bool readonly, bool direct) @@ -243,6 +215,8 @@ static int disk_image__close(struct disk_image *disk) if (!disk) return 0; + disk_aio_destroy(disk); + if (disk->ops->close) return disk->ops->close(disk); diff --git a/disk/raw.c b/disk/raw.c index 93b2b4e8d..09da7e081 100644 --- a/disk/raw.c +++ b/disk/raw.c @@ -2,38 +2,17 @@ #include -#ifdef CONFIG_HAS_AIO -#include -#endif - -ssize_t raw_image__read(struct disk_image *disk, u64 sector, const struct iovec *iov, +ssize_t raw_image__read_sync(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 + return preadv_in_full(disk->fd, iov, iovcount, sector << SECTOR_SHIFT); } -ssize_t raw_image__write(struct disk_image *disk, u64 sector, const struct iovec *iov, - int iovcount, void *param) +ssize_t raw_image__write_sync(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_pwritev(disk->ctx, &iocb, disk->fd, iov, iovcount, offset, - disk->evt, param); -#else - return pwritev_in_full(disk->fd, iov, iovcount, offset); -#endif + return pwritev_in_full(disk->fd, iov, iovcount, sector << SECTOR_SHIFT); } ssize_t raw_image__read_mmap(struct disk_image *disk, u64 sector, const struct iovec *iov, @@ -79,12 +58,6 @@ 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; } diff --git a/include/kvm/disk-image.h b/include/kvm/disk-image.h index 4746e88c9..953beb2d5 100644 --- a/include/kvm/disk-image.h +++ b/include/kvm/disk-image.h @@ -19,6 +19,10 @@ #include #include +#ifdef CONFIG_HAS_AIO +#include +#endif + #define SECTOR_SHIFT 9 #define SECTOR_SIZE (1UL << SECTOR_SHIFT) @@ -61,10 +65,10 @@ struct disk_image { void (*disk_req_cb)(void *param, long len); bool readonly; bool async; - int evt; #ifdef CONFIG_HAS_AIO io_context_t ctx; -#endif + int evt; +#endif /* CONFIG_HAS_AIO */ const char *wwpn; const char *tpgt; int debug_iodelay; @@ -84,14 +88,39 @@ 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, int flags, struct stat *st); -ssize_t raw_image__read(struct disk_image *disk, u64 sector, - const struct iovec *iov, int iovcount, void *param); -ssize_t raw_image__write(struct disk_image *disk, u64 sector, - const struct iovec *iov, int iovcount, void *param); +ssize_t raw_image__read_sync(struct disk_image *disk, u64 sector, + const struct iovec *iov, int iovcount, void *param); +ssize_t raw_image__write_sync(struct disk_image *disk, u64 sector, + const struct iovec *iov, int iovcount, void *param); ssize_t raw_image__read_mmap(struct disk_image *disk, u64 sector, const struct iovec *iov, int iovcount, void *param); ssize_t raw_image__write_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, long len)); + +#ifdef CONFIG_HAS_AIO +int disk_aio_setup(struct disk_image *disk); +void disk_aio_destroy(struct disk_image *disk); +ssize_t raw_image__read_async(struct disk_image *disk, u64 sector, + const struct iovec *iov, int iovcount, void *param); +ssize_t raw_image__write_async(struct disk_image *disk, u64 sector, + const struct iovec *iov, int iovcount, void *param); + +#define raw_image__read raw_image__read_async +#define raw_image__write raw_image__write_async + +#else /* !CONFIG_HAS_AIO */ +static inline int disk_aio_setup(struct disk_image *disk) +{ + /* No-op */ + return 0; +} +static inline void disk_aio_destroy(struct disk_image *disk) +{ +} +#define raw_image__read raw_image__read_sync +#define raw_image__write raw_image__write_sync +#endif /* CONFIG_HAS_AIO */ + #endif /* KVM__DISK_IMAGE_H */ diff --git a/include/kvm/read-write.h b/include/kvm/read-write.h index acbd6f0b1..8375d7c7d 100644 --- a/include/kvm/read-write.h +++ b/include/kvm/read-write.h @@ -5,10 +5,6 @@ #include #include -#ifdef CONFIG_HAS_AIO -#include -#endif - ssize_t xread(int fd, void *buf, size_t count); ssize_t xwrite(int fd, const void *buf, size_t count); @@ -35,11 +31,4 @@ 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 */ diff --git a/util/read-write.c b/util/read-write.c index bf6fb2fc2..06fc0dfff 100644 --- a/util/read-write.c +++ b/util/read-write.c @@ -337,39 +337,3 @@ 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 }; - int ret; - - io_prep_pwritev(iocb, fd, iov, iovcnt, offset); - io_set_eventfd(iocb, ev); - iocb->data = param; - -restart: - ret = io_submit(ctx, 1, ios); - if (ret == -EAGAIN) - goto restart; - return ret; -} - -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 }; - int ret; - - io_prep_preadv(iocb, fd, iov, iovcnt, offset); - io_set_eventfd(iocb, ev); - iocb->data = param; - -restart: - ret = io_submit(ctx, 1, ios); - if (ret == -EAGAIN) - goto restart; - return ret; -} -#endif