block/file-win32, win32-aio: convert to coroutine I/O and add bounce-buffer pool
Convert file-win32 and win32-aio from legacy BlockAIOCB + BH completion
callbacks to native coroutine block driver hooks (.bdrv_co_preadv,
.bdrv_co_pwritev, and .bdrv_co_flush_to_disk), matching file-posix and
linux-aio:
- Stack-allocate Win32AioCoReq across qemu_coroutine_yield() and wake
waiting coroutines directly via aio_co_schedule() from the IOCP
waiter thread, returning synchronously without yielding or scheduling
a BH whenever FILE_SKIP_COMPLETION_PORT_ON_SUCCESS completes inline.
- Add a per-QEMUWin32AIOState mutex-protected 3-bucket page-aligned
bounce-buffer slab pool (64 KiB x16, 256 KiB x8, 1 MiB x8) to
eliminate per-request _aligned_malloc/_aligned_free churn on
multi-iov and unaligned direct I/O writes while safely falling back
to qemu_try_blockalign() for oversized or stricter-alignment requests.
- Convert the thread-pool fallback (paio_co_submit) to
thread_pool_submit_co() for both bdrv_file and bdrv_host_device.
(cherry picked from commit e60f01ecb6)
reims-vgpu's take of PR 22, whole, as benched (planner's verdict 12-02).
reims-ctl runs every Windows disk with aio=threads and cache=writeback,
through the file driver. On that path this PR fixes reads that failed and
came back as zeros with success, a leaked request on a failed flush, and a
truncate that could set the wrong end of file. Both silent-corruption
defects reproduced on the base and are clean here. The coroutine hooks
need this commit's win32-aio.c, so the PR is carried whole. aio=native is
not turned on by this.
A change the PR does not name: buffered files get request alignment 1
where it was the volume's sector size. That is what the POSIX side does
for buffered files. No 4096-byte-sector volume was available to run it on.
Bench on the Windows mini, venue C:, its system disk, a USB NVMe (the
only disk online). qemu-img bench, qcow2 over 4 GiB, -t writeback -i
threads. Seconds a1/a2/b1/b2 (A = the fork before, B = this PR), then
the a and b spreads:
1M seq write QD16 8.13/7.57/7.39/7.43 7.4% 0.5%
1M overwrite 6.05/3.83/5.02/3.24 57.7% 55.0%
1M seq read 7.01/7.10/6.78/6.94 1.2% 2.4%
4K seq write QD16 3.74/3.82/3.79/3.84 2.1% 1.2%
4K seq read QD16 24.23/24.15/23.56/22.75 0.3% 3.5%
4K rand write QD16 1.39/1.51/1.41/1.33 9.2% 5.8%
4K rand read QD16 19.25/19.61/19.45/16.41 1.9% 18.5%
4K rand write QD1 0.57/0.60/0.61/0.71 6.0% 16.9%
4K rand read QD1 4.41/4.24/4.19/3.89 4.1% 7.8%
1M rand write QD16 1.97/1.44/2.38/1.71 36.9% 39.2%
1M rand read QD16 8.35/8.09/7.99/6.76 3.2% 18.2%
4K, a flush per write 9.04/9.39/9.09/7.99 3.9% 13.7%
64K QD16, flush per 16 1.17/1.18/1.32/1.02 1.5% 29.6%
No row is slower beyond 3% and beyond both spreads. qemu-img check and
compare: every arm identical to a1. A byte-range lock held by another
process: the base reads zeros with success ("Pattern verification failed
at offset 0"); this PR answers "Input/output error". 400 truncates under
writes: the base lost the tail (offset 66060288); this PR is clean at
464 MiB.
This commit is contained in:
1 parent
2655904c24
commit
da50fbf90e
3 files changed
+200
-199
No files matched your search
+55
-75
@@ -51,10 +51,6 @@ typedef struct RawWin32AIOData {
|
||||
size_t aio_nbytes;
|
||||
int64_t aio_offset;
|
||||
int aio_type;
|
||||
bool direct;
|
||||
uint32_t req_align;
|
||||
size_t mem_align;
|
||||
int ret;
|
||||
} RawWin32AIOData;
|
||||
|
||||
typedef struct BDRVRawState {
|
||||
@@ -147,14 +143,10 @@ static int handle_aiocb_rw(RawWin32AIOData *aiocb, size_t *transferred)
|
||||
static int aio_worker(void *arg)
|
||||
{
|
||||
RawWin32AIOData *aiocb = arg;
|
||||
int ret = aiocb->ret;
|
||||
int ret = 0;
|
||||
size_t count;
|
||||
QEMUIOVector *qiov = aiocb->qiov;
|
||||
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
switch (aiocb->aio_type & QEMU_AIO_TYPE_MASK) {
|
||||
case QEMU_AIO_READ:
|
||||
ret = handle_aiocb_rw(aiocb, &count);
|
||||
@@ -185,57 +177,47 @@ static int aio_worker(void *arg)
|
||||
break;
|
||||
}
|
||||
|
||||
out:
|
||||
if (aiocb->bounce_buf) {
|
||||
qemu_vfree(aiocb->bounce_buf);
|
||||
}
|
||||
g_free(aiocb);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static BlockAIOCB *paio_submit(BlockDriverState *bs, HANDLE hfile,
|
||||
int64_t offset, QEMUIOVector *qiov, int64_t bytes,
|
||||
BlockCompletionFunc *cb, void *opaque, int type)
|
||||
static int coroutine_fn paio_co_submit(BlockDriverState *bs, HANDLE hfile,
|
||||
int64_t offset, QEMUIOVector *qiov, int64_t bytes, int type)
|
||||
{
|
||||
RawWin32AIOData *acb = g_new0(RawWin32AIOData, 1);
|
||||
|
||||
acb->hfile = hfile;
|
||||
acb->aio_type = type;
|
||||
acb->aio_offset = offset;
|
||||
acb->qiov = qiov;
|
||||
acb->direct = bs->open_flags & BDRV_O_NOCACHE;
|
||||
acb->req_align = bs->bl.request_alignment;
|
||||
acb->mem_align = bdrv_min_mem_align(bs);
|
||||
RawWin32AIOData acb = {
|
||||
.hfile = hfile,
|
||||
.aio_type = type,
|
||||
.aio_offset = offset,
|
||||
.qiov = qiov,
|
||||
};
|
||||
int ret;
|
||||
|
||||
if (bytes < 0 || bytes > BDRV_REQUEST_MAX_BYTES ||
|
||||
(qiov && qiov->size != (uint64_t)bytes)) {
|
||||
acb->ret = -EINVAL;
|
||||
goto submit;
|
||||
return -EINVAL;
|
||||
}
|
||||
acb->aio_nbytes = bytes;
|
||||
if (!acb->direct || bytes == 0) {
|
||||
goto submit;
|
||||
}
|
||||
if (!QEMU_IS_ALIGNED(offset, acb->req_align) ||
|
||||
!QEMU_IS_ALIGNED(bytes, acb->req_align)) {
|
||||
acb->ret = -EINVAL;
|
||||
goto submit;
|
||||
}
|
||||
if (qiov->niov > 1 ||
|
||||
!QEMU_PTR_IS_ALIGNED(qiov->iov[0].iov_base, acb->mem_align)) {
|
||||
acb->bounce_buf = qemu_try_blockalign(bs, bytes);
|
||||
if (!acb->bounce_buf) {
|
||||
acb->ret = -ENOMEM;
|
||||
goto submit;
|
||||
acb.aio_nbytes = bytes;
|
||||
if (bytes && (bs->open_flags & BDRV_O_NOCACHE)) {
|
||||
if (!QEMU_IS_ALIGNED(offset, bs->bl.request_alignment) ||
|
||||
!QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
if (type & QEMU_AIO_WRITE) {
|
||||
iov_to_buf(qiov->iov, qiov->niov, 0, acb->bounce_buf, bytes);
|
||||
if (qiov->niov > 1 ||
|
||||
!QEMU_PTR_IS_ALIGNED(qiov->iov[0].iov_base,
|
||||
bdrv_min_mem_align(bs))) {
|
||||
acb.bounce_buf = qemu_try_blockalign(bs, bytes);
|
||||
if (!acb.bounce_buf) {
|
||||
return -ENOMEM;
|
||||
}
|
||||
if (type & QEMU_AIO_WRITE) {
|
||||
iov_to_buf(qiov->iov, qiov->niov, 0, acb.bounce_buf, bytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
submit:
|
||||
trace_file_paio_submit(acb, opaque, offset, (int)acb->aio_nbytes, type);
|
||||
return thread_pool_submit_aio(aio_worker, acb, cb, opaque);
|
||||
trace_file_paio_submit(&acb, NULL, offset, (int)acb.aio_nbytes, type);
|
||||
ret = thread_pool_submit_co(aio_worker, &acb);
|
||||
qemu_vfree(acb.bounce_buf);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int set_sparse(int fd)
|
||||
@@ -628,41 +610,39 @@ fail:
|
||||
return ret;
|
||||
}
|
||||
|
||||
static BlockAIOCB *raw_aio_preadv(BlockDriverState *bs,
|
||||
int64_t offset, int64_t bytes,
|
||||
QEMUIOVector *qiov, BdrvRequestFlags flags,
|
||||
BlockCompletionFunc *cb, void *opaque)
|
||||
static int coroutine_fn raw_co_preadv(BlockDriverState *bs,
|
||||
int64_t offset, int64_t bytes,
|
||||
QEMUIOVector *qiov,
|
||||
BdrvRequestFlags flags)
|
||||
{
|
||||
BDRVRawState *s = bs->opaque;
|
||||
|
||||
if (s->aio) {
|
||||
return win32_aio_submit(bs, s->aio, s->hfile, offset, bytes, qiov,
|
||||
cb, opaque, QEMU_AIO_READ);
|
||||
} else {
|
||||
return paio_submit(bs, s->hfile, offset, qiov, bytes,
|
||||
cb, opaque, QEMU_AIO_READ);
|
||||
return win32_aio_co_submit(bs, s->aio, s->hfile, offset, bytes, qiov,
|
||||
QEMU_AIO_READ);
|
||||
}
|
||||
return paio_co_submit(bs, s->hfile, offset, qiov, bytes, QEMU_AIO_READ);
|
||||
}
|
||||
|
||||
static BlockAIOCB *raw_aio_pwritev(BlockDriverState *bs,
|
||||
int64_t offset, int64_t bytes,
|
||||
QEMUIOVector *qiov, BdrvRequestFlags flags,
|
||||
BlockCompletionFunc *cb, void *opaque)
|
||||
static int coroutine_fn raw_co_pwritev(BlockDriverState *bs,
|
||||
int64_t offset, int64_t bytes,
|
||||
QEMUIOVector *qiov,
|
||||
BdrvRequestFlags flags)
|
||||
{
|
||||
BDRVRawState *s = bs->opaque;
|
||||
|
||||
if (s->aio) {
|
||||
return win32_aio_submit(bs, s->aio, s->hfile, offset, bytes, qiov,
|
||||
cb, opaque, QEMU_AIO_WRITE);
|
||||
} else {
|
||||
return paio_submit(bs, s->hfile, offset, qiov, bytes,
|
||||
cb, opaque, QEMU_AIO_WRITE);
|
||||
return win32_aio_co_submit(bs, s->aio, s->hfile, offset, bytes, qiov,
|
||||
QEMU_AIO_WRITE);
|
||||
}
|
||||
return paio_co_submit(bs, s->hfile, offset, qiov, bytes, QEMU_AIO_WRITE);
|
||||
}
|
||||
|
||||
static BlockAIOCB *raw_aio_flush(BlockDriverState *bs,
|
||||
BlockCompletionFunc *cb, void *opaque)
|
||||
static int coroutine_fn raw_co_flush_to_disk(BlockDriverState *bs)
|
||||
{
|
||||
BDRVRawState *s = bs->opaque;
|
||||
return paio_submit(bs, s->hfile, 0, NULL, 0, cb, opaque, QEMU_AIO_FLUSH);
|
||||
|
||||
return paio_co_submit(bs, s->hfile, 0, NULL, 0, QEMU_AIO_FLUSH);
|
||||
}
|
||||
|
||||
static void raw_close(BlockDriverState *bs)
|
||||
@@ -949,9 +929,9 @@ BlockDriver bdrv_file = {
|
||||
.bdrv_reopen_commit = raw_reopen_commit,
|
||||
.bdrv_reopen_abort = raw_reopen_abort,
|
||||
|
||||
.bdrv_aio_preadv = raw_aio_preadv,
|
||||
.bdrv_aio_pwritev = raw_aio_pwritev,
|
||||
.bdrv_aio_flush = raw_aio_flush,
|
||||
.bdrv_co_preadv = raw_co_preadv,
|
||||
.bdrv_co_pwritev = raw_co_pwritev,
|
||||
.bdrv_co_flush_to_disk = raw_co_flush_to_disk,
|
||||
|
||||
.bdrv_detach_aio_context = raw_detach_aio_context,
|
||||
.bdrv_attach_aio_context = raw_attach_aio_context,
|
||||
@@ -1120,9 +1100,9 @@ static BlockDriver bdrv_host_device = {
|
||||
.bdrv_close = raw_close,
|
||||
.bdrv_refresh_limits = hdev_refresh_limits,
|
||||
|
||||
.bdrv_aio_preadv = raw_aio_preadv,
|
||||
.bdrv_aio_pwritev = raw_aio_pwritev,
|
||||
.bdrv_aio_flush = raw_aio_flush,
|
||||
.bdrv_co_preadv = raw_co_preadv,
|
||||
.bdrv_co_pwritev = raw_co_pwritev,
|
||||
.bdrv_co_flush_to_disk = raw_co_flush_to_disk,
|
||||
|
||||
.bdrv_detach_aio_context = raw_detach_aio_context,
|
||||
.bdrv_attach_aio_context = raw_attach_aio_context,
|
||||
|
||||
+143
-121
@@ -27,11 +27,12 @@
|
||||
#include "block/block_int.h"
|
||||
#include "qemu/aio.h"
|
||||
#include "block/raw-aio.h"
|
||||
#include "qemu/aiocb.h"
|
||||
#include "qemu/error-report.h"
|
||||
#include "qemu/iov.h"
|
||||
#include "qemu/lockable.h"
|
||||
#include "qemu/memalign.h"
|
||||
#include "qemu/thread.h"
|
||||
#include "qemu/units.h"
|
||||
#include <windows.h>
|
||||
|
||||
enum {
|
||||
@@ -39,25 +40,39 @@ enum {
|
||||
WIN32_AIO_KEY_STOP,
|
||||
};
|
||||
|
||||
#define WIN32_AIO_BOUNCE_MAX_CACHED 16
|
||||
|
||||
static const struct {
|
||||
size_t size;
|
||||
unsigned int capacity;
|
||||
} bounce_buckets[] = {
|
||||
{ 64 * KiB, WIN32_AIO_BOUNCE_MAX_CACHED },
|
||||
{ 256 * KiB, 8 },
|
||||
{ MiB, 8 },
|
||||
};
|
||||
|
||||
typedef struct Win32AioBounceBucket {
|
||||
void *buffers[WIN32_AIO_BOUNCE_MAX_CACHED];
|
||||
unsigned int count;
|
||||
} Win32AioBounceBucket;
|
||||
|
||||
struct QEMUWin32AIOState {
|
||||
HANDLE hIOCP;
|
||||
QemuThread waiter_thread;
|
||||
AioContext *aio_ctx;
|
||||
bool skip_iocp_on_success;
|
||||
QemuMutex pool_lock;
|
||||
size_t pool_align;
|
||||
Win32AioBounceBucket pool[ARRAY_SIZE(bounce_buckets)];
|
||||
};
|
||||
|
||||
typedef struct QEMUWin32AIOCB {
|
||||
BlockAIOCB common;
|
||||
AioContext *req_ctx;
|
||||
DWORD nbytes;
|
||||
typedef struct Win32AioCoReq {
|
||||
OVERLAPPED ov;
|
||||
QEMUIOVector *qiov;
|
||||
void *bounce_buf;
|
||||
bool is_read;
|
||||
int ret;
|
||||
Coroutine *co;
|
||||
AioContext *req_ctx;
|
||||
DWORD win32_err;
|
||||
DWORD transferred;
|
||||
} QEMUWin32AIOCB;
|
||||
} Win32AioCoReq;
|
||||
|
||||
static int win32_error_to_errno(DWORD err)
|
||||
{
|
||||
@@ -79,39 +94,53 @@ static int win32_error_to_errno(DWORD err)
|
||||
}
|
||||
}
|
||||
|
||||
static void win32_aio_completion_cb_bh(void *opaque)
|
||||
static void *win32_aio_bounce_alloc(QEMUWin32AIOState *aio,
|
||||
BlockDriverState *bs, size_t bytes,
|
||||
int *bucket_out)
|
||||
{
|
||||
QEMUWin32AIOCB *waiocb = opaque;
|
||||
int ret = waiocb->ret;
|
||||
int bucket = 0;
|
||||
void *buf;
|
||||
|
||||
if (ret == 0 && waiocb->win32_err != ERROR_SUCCESS &&
|
||||
!(waiocb->is_read && waiocb->win32_err == ERROR_HANDLE_EOF)) {
|
||||
ret = win32_error_to_errno(waiocb->win32_err);
|
||||
while (bucket < ARRAY_SIZE(bounce_buckets) &&
|
||||
bytes > bounce_buckets[bucket].size) {
|
||||
bucket++;
|
||||
}
|
||||
if (bucket == ARRAY_SIZE(bounce_buckets) ||
|
||||
bdrv_min_mem_align(bs) > aio->pool_align) {
|
||||
*bucket_out = -1;
|
||||
return qemu_try_blockalign(bs, bytes);
|
||||
}
|
||||
if (ret == 0) {
|
||||
DWORD count = waiocb->transferred;
|
||||
QEMUIOVector *qiov = waiocb->qiov;
|
||||
|
||||
if (count > waiocb->nbytes ||
|
||||
(!waiocb->is_read && count < waiocb->nbytes)) {
|
||||
ret = -EIO;
|
||||
} else if (waiocb->is_read) {
|
||||
if (waiocb->bounce_buf && count) {
|
||||
iov_from_buf(qiov->iov, qiov->niov, 0, waiocb->bounce_buf,
|
||||
count);
|
||||
}
|
||||
if (count < waiocb->nbytes) {
|
||||
qemu_iovec_memset(qiov, count, 0, waiocb->nbytes - count);
|
||||
}
|
||||
WITH_QEMU_LOCK_GUARD(&aio->pool_lock) {
|
||||
Win32AioBounceBucket *pool = &aio->pool[bucket];
|
||||
|
||||
if (pool->count) {
|
||||
*bucket_out = bucket;
|
||||
return pool->buffers[--pool->count];
|
||||
}
|
||||
}
|
||||
|
||||
if (waiocb->bounce_buf) {
|
||||
qemu_vfree(waiocb->bounce_buf);
|
||||
buf = qemu_try_memalign(aio->pool_align, bounce_buckets[bucket].size);
|
||||
if (buf) {
|
||||
*bucket_out = bucket;
|
||||
}
|
||||
waiocb->common.cb(waiocb->common.opaque, ret);
|
||||
aio_context_unref(waiocb->req_ctx);
|
||||
qemu_aio_unref(waiocb);
|
||||
return buf;
|
||||
}
|
||||
|
||||
static void win32_aio_bounce_free(QEMUWin32AIOState *aio, void *buf, int bucket)
|
||||
{
|
||||
if (!buf) {
|
||||
return;
|
||||
}
|
||||
if (bucket >= 0) {
|
||||
Win32AioBounceBucket *pool = &aio->pool[bucket];
|
||||
QEMU_LOCK_GUARD(&aio->pool_lock);
|
||||
|
||||
if (pool->count < bounce_buckets[bucket].capacity) {
|
||||
pool->buffers[pool->count++] = buf;
|
||||
return;
|
||||
}
|
||||
}
|
||||
qemu_vfree(buf);
|
||||
}
|
||||
|
||||
static void *win32_aio_waiter(void *opaque)
|
||||
@@ -122,7 +151,9 @@ static void *win32_aio_waiter(void *opaque)
|
||||
DWORD count = 0;
|
||||
ULONG_PTR key;
|
||||
OVERLAPPED *ov;
|
||||
QEMUWin32AIOCB *waiocb;
|
||||
Win32AioCoReq *req;
|
||||
AioContext *ctx;
|
||||
Coroutine *co;
|
||||
BOOL ok = GetQueuedCompletionStatus(s->hIOCP, &count, &key, &ov,
|
||||
INFINITE);
|
||||
DWORD err = ok ? ERROR_SUCCESS : GetLastError();
|
||||
@@ -137,121 +168,102 @@ static void *win32_aio_waiter(void *opaque)
|
||||
}
|
||||
|
||||
assert(key == WIN32_AIO_KEY_IO);
|
||||
waiocb = container_of(ov, QEMUWin32AIOCB, ov);
|
||||
waiocb->win32_err = err;
|
||||
waiocb->transferred = err == ERROR_HANDLE_EOF ? 0 : count;
|
||||
aio_bh_schedule_oneshot(waiocb->req_ctx, win32_aio_completion_cb_bh,
|
||||
waiocb);
|
||||
req = container_of(ov, Win32AioCoReq, ov);
|
||||
ctx = req->req_ctx;
|
||||
co = req->co;
|
||||
req->win32_err = err;
|
||||
req->transferred = err == ERROR_HANDLE_EOF ? 0 : count;
|
||||
aio_co_schedule(ctx, co);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static const AIOCBInfo win32_aiocb_info = {
|
||||
.aiocb_size = sizeof(QEMUWin32AIOCB),
|
||||
};
|
||||
|
||||
BlockAIOCB *win32_aio_submit(BlockDriverState *bs,
|
||||
int coroutine_fn win32_aio_co_submit(BlockDriverState *bs,
|
||||
QEMUWin32AIOState *aio, HANDLE hfile,
|
||||
uint64_t offset, uint64_t bytes, QEMUIOVector *qiov,
|
||||
BlockCompletionFunc *cb, void *opaque, int type)
|
||||
uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int type)
|
||||
{
|
||||
QEMUWin32AIOCB *waiocb;
|
||||
BlockAIOCB *acb;
|
||||
Win32AioCoReq req;
|
||||
void *bounce_buf = NULL;
|
||||
int bucket = -1;
|
||||
void *buf;
|
||||
BOOL ok;
|
||||
DWORD err;
|
||||
DWORD sync_transferred = 0;
|
||||
DWORD transferred;
|
||||
DWORD win32_err;
|
||||
int ret = 0;
|
||||
bool direct = bs->open_flags & BDRV_O_NOCACHE;
|
||||
bool is_read = (type & QEMU_AIO_TYPE_MASK) == QEMU_AIO_READ;
|
||||
|
||||
assert(aio->aio_ctx);
|
||||
waiocb = qemu_aio_get(&win32_aiocb_info, bs, cb, opaque);
|
||||
acb = &waiocb->common;
|
||||
waiocb->req_ctx = qemu_get_current_aio_context();
|
||||
waiocb->nbytes = 0;
|
||||
waiocb->qiov = qiov;
|
||||
waiocb->bounce_buf = NULL;
|
||||
waiocb->is_read = (type & QEMU_AIO_TYPE_MASK) == QEMU_AIO_READ;
|
||||
waiocb->ret = 0;
|
||||
waiocb->win32_err = ERROR_SUCCESS;
|
||||
waiocb->transferred = 0;
|
||||
waiocb->ov = (OVERLAPPED) {
|
||||
.Offset = (DWORD)offset,
|
||||
.OffsetHigh = (DWORD)(offset >> 32),
|
||||
.hEvent = NULL,
|
||||
};
|
||||
|
||||
aio_context_ref(waiocb->req_ctx);
|
||||
|
||||
if (bytes != qiov->size || bytes > BDRV_REQUEST_MAX_BYTES ||
|
||||
bytes > UINT32_MAX) {
|
||||
waiocb->ret = -EINVAL;
|
||||
goto complete;
|
||||
return -EINVAL;
|
||||
}
|
||||
waiocb->nbytes = (DWORD)bytes;
|
||||
if (bytes == 0) {
|
||||
goto complete;
|
||||
return 0;
|
||||
}
|
||||
if (direct && (!QEMU_IS_ALIGNED(offset, bs->bl.request_alignment) ||
|
||||
!QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment))) {
|
||||
waiocb->ret = -EINVAL;
|
||||
goto complete;
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (qiov->niov > 1 ||
|
||||
(direct && !QEMU_PTR_IS_ALIGNED(qiov->iov[0].iov_base,
|
||||
bdrv_min_mem_align(bs)))) {
|
||||
waiocb->bounce_buf = qemu_try_blockalign(bs, bytes);
|
||||
if (!waiocb->bounce_buf) {
|
||||
waiocb->ret = -ENOMEM;
|
||||
goto complete;
|
||||
bounce_buf = win32_aio_bounce_alloc(aio, bs, bytes, &bucket);
|
||||
if (!bounce_buf) {
|
||||
return -ENOMEM;
|
||||
}
|
||||
if (!waiocb->is_read) {
|
||||
iov_to_buf(qiov->iov, qiov->niov, 0, waiocb->bounce_buf, bytes);
|
||||
if (!is_read) {
|
||||
iov_to_buf(qiov->iov, qiov->niov, 0, bounce_buf, bytes);
|
||||
}
|
||||
}
|
||||
buf = waiocb->bounce_buf ? waiocb->bounce_buf : qiov->iov[0].iov_base;
|
||||
buf = bounce_buf ? bounce_buf : qiov->iov[0].iov_base;
|
||||
|
||||
if (waiocb->is_read) {
|
||||
DWORD transferred = 0;
|
||||
|
||||
ok = ReadFile(hfile, buf, waiocb->nbytes,
|
||||
aio->skip_iocp_on_success ? &transferred : NULL,
|
||||
&waiocb->ov);
|
||||
if (ok) {
|
||||
if (aio->skip_iocp_on_success) {
|
||||
waiocb->transferred = transferred;
|
||||
goto complete;
|
||||
}
|
||||
/* Synchronous success still queues a completion packet. */
|
||||
return acb;
|
||||
}
|
||||
/* The waiter can queue the coroutine before the system call returns. */
|
||||
req = (Win32AioCoReq) {
|
||||
.ov = {
|
||||
.Offset = (DWORD)offset,
|
||||
.OffsetHigh = (DWORD)(offset >> 32),
|
||||
.hEvent = NULL,
|
||||
},
|
||||
.co = qemu_coroutine_self(),
|
||||
.req_ctx = qemu_get_current_aio_context(),
|
||||
};
|
||||
if (is_read) {
|
||||
ok = ReadFile(hfile, buf, (DWORD)bytes,
|
||||
aio->skip_iocp_on_success ? &sync_transferred : NULL,
|
||||
&req.ov);
|
||||
} else {
|
||||
DWORD transferred = 0;
|
||||
ok = WriteFile(hfile, buf, (DWORD)bytes,
|
||||
aio->skip_iocp_on_success ? &sync_transferred : NULL,
|
||||
&req.ov);
|
||||
}
|
||||
win32_err = ok ? ERROR_SUCCESS : GetLastError();
|
||||
if (win32_err == ERROR_IO_PENDING || (ok && !aio->skip_iocp_on_success)) {
|
||||
qemu_coroutine_yield();
|
||||
win32_err = req.win32_err;
|
||||
transferred = req.transferred;
|
||||
} else {
|
||||
transferred = ok ? sync_transferred : 0;
|
||||
}
|
||||
|
||||
ok = WriteFile(hfile, buf, waiocb->nbytes,
|
||||
aio->skip_iocp_on_success ? &transferred : NULL,
|
||||
&waiocb->ov);
|
||||
if (ok) {
|
||||
if (aio->skip_iocp_on_success) {
|
||||
waiocb->transferred = transferred;
|
||||
goto complete;
|
||||
}
|
||||
/* Synchronous success still queues a completion packet. */
|
||||
return acb;
|
||||
if (win32_err != ERROR_SUCCESS &&
|
||||
!(is_read && win32_err == ERROR_HANDLE_EOF)) {
|
||||
ret = win32_error_to_errno(win32_err);
|
||||
} else if (transferred > bytes || (!is_read && transferred < bytes)) {
|
||||
ret = -EIO;
|
||||
} else if (is_read) {
|
||||
if (bounce_buf && transferred) {
|
||||
iov_from_buf(qiov->iov, qiov->niov, 0, bounce_buf, transferred);
|
||||
}
|
||||
if (transferred < bytes) {
|
||||
qemu_iovec_memset(qiov, transferred, 0, bytes - transferred);
|
||||
}
|
||||
}
|
||||
err = GetLastError();
|
||||
if (err == ERROR_IO_PENDING) {
|
||||
return acb;
|
||||
}
|
||||
waiocb->win32_err = err;
|
||||
if (!waiocb->is_read || err != ERROR_HANDLE_EOF) {
|
||||
waiocb->ret = win32_error_to_errno(err);
|
||||
}
|
||||
|
||||
complete:
|
||||
aio_bh_schedule_oneshot(waiocb->req_ctx, win32_aio_completion_cb_bh,
|
||||
waiocb);
|
||||
return acb;
|
||||
win32_aio_bounce_free(aio, bounce_buf, bucket);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int win32_aio_attach(QEMUWin32AIOState *aio, HANDLE hfile)
|
||||
@@ -280,7 +292,7 @@ void win32_aio_detach_aio_context(QEMUWin32AIOState *aio,
|
||||
AioContext *old_context)
|
||||
{
|
||||
assert(aio->aio_ctx == old_context);
|
||||
/* Block layer drain has waited for all request callbacks. */
|
||||
/* Block layer drain has waited for all requests to finish. */
|
||||
if (!PostQueuedCompletionStatus(aio->hIOCP, 0, WIN32_AIO_KEY_STOP, NULL)) {
|
||||
error_report("PostQueuedCompletionStatus failed: %lu", GetLastError());
|
||||
abort();
|
||||
@@ -308,12 +320,22 @@ QEMUWin32AIOState *win32_aio_init(void)
|
||||
}
|
||||
s = g_new0(QEMUWin32AIOState, 1);
|
||||
s->hIOCP = port;
|
||||
qemu_mutex_init(&s->pool_lock);
|
||||
s->pool_align = qemu_real_host_page_size();
|
||||
return s;
|
||||
}
|
||||
|
||||
void win32_aio_cleanup(QEMUWin32AIOState *aio)
|
||||
{
|
||||
assert(!aio->aio_ctx);
|
||||
for (int i = 0; i < ARRAY_SIZE(aio->pool); i++) {
|
||||
Win32AioBounceBucket *pool = &aio->pool[i];
|
||||
|
||||
for (unsigned int j = 0; j < pool->count; j++) {
|
||||
qemu_vfree(pool->buffers[j]);
|
||||
}
|
||||
}
|
||||
qemu_mutex_destroy(&aio->pool_lock);
|
||||
CloseHandle(aio->hIOCP);
|
||||
g_free(aio);
|
||||
}
|
||||
@@ -92,10 +92,9 @@ typedef struct QEMUWin32AIOState QEMUWin32AIOState;
|
||||
QEMUWin32AIOState *win32_aio_init(void);
|
||||
void win32_aio_cleanup(QEMUWin32AIOState *aio);
|
||||
int win32_aio_attach(QEMUWin32AIOState *aio, HANDLE hfile);
|
||||
BlockAIOCB *win32_aio_submit(BlockDriverState *bs,
|
||||
int coroutine_fn win32_aio_co_submit(BlockDriverState *bs,
|
||||
QEMUWin32AIOState *aio, HANDLE hfile,
|
||||
uint64_t offset, uint64_t bytes, QEMUIOVector *qiov,
|
||||
BlockCompletionFunc *cb, void *opaque, int type);
|
||||
uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int type);
|
||||
void win32_aio_detach_aio_context(QEMUWin32AIOState *aio,
|
||||
AioContext *old_context);
|
||||
void win32_aio_attach_aio_context(QEMUWin32AIOState *aio,
|
||||
|
||||
Reference in new issue
Block a user