Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async I/O (syscalls 245 - 249) #1584

Open
wants to merge 34 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
0b2b56a
Individually stub each AIO syscall
kmeisthax Sep 27, 2021
175c52c
Add a structure in the task for AIO contexts to live in.
kmeisthax Sep 27, 2021
9c4a3f8
Implement `io_setup`
kmeisthax Sep 28, 2021
241fe79
Implement `io_destroy`.
kmeisthax Sep 30, 2021
5f041a3
Store the owning PID of each AIO context, so that resolved requests c…
kmeisthax Oct 1, 2021
9912680
Elaborate on the event structure to be able to hold pending requests,…
kmeisthax Oct 1, 2021
cce2d45
Add method for submitting a pending event.
kmeisthax Oct 1, 2021
314f335
Add functions for retrieving pending events from a context.
kmeisthax Oct 3, 2021
5390cf5
Add AIO syscalls to systrace
kmeisthax Oct 3, 2021
0cf305b
Add aioctx table method for retrieving (and retaining) a context by i…
kmeisthax Oct 3, 2021
032d31d
Half-implement io_submit.
kmeisthax Oct 3, 2021
ecfeae1
Add `aioctx_cancel_event` & other documentation fixes
kmeisthax Oct 3, 2021
da5a606
Add io_submit op so that FDs can handle asynchronous operations.
kmeisthax Oct 3, 2021
a4560f5
Add method to complete pending events.
kmeisthax Oct 4, 2021
9510f30
Add fallback for filesystems that don't yet implement true async I/O.
kmeisthax Oct 4, 2021
1dd76fe
Submitted events must retain the guest address of the IOCB structure …
kmeisthax Oct 4, 2021
8638202
Implement completion polling (io_getevents)
kmeisthax Oct 4, 2021
1c9f39a
Everything except io_setup takes a context ID directly, not a pointer…
kmeisthax Oct 4, 2021
d678aad
Properly initialize the lock and refcount in AIO contexts.
kmeisthax Oct 10, 2021
940d53d
Fix a bunch of bounds checks
kmeisthax Oct 10, 2021
21bd634
Always NULL out contexts in the task context table when removing them…
kmeisthax Oct 10, 2021
b504474
Add E2E test for AIO read and write.
kmeisthax Oct 10, 2021
867e899
Don't specify an enum impl type because gcc doesn't support that C gi…
kmeisthax Oct 11, 2021
09b2e07
In fallback code, treat FDSYNC as a full FSYNC.
kmeisthax Oct 24, 2021
fc90511
Pull the pread/pwrite emulation code out into separate functions.
kmeisthax Oct 25, 2021
8ffda31
Add E2E test for vectored async IO
kmeisthax Oct 25, 2021
f270bc1
Implement vectored async IO fallback
kmeisthax Oct 25, 2021
9769c6e
Fix incorrect/spurious failure on seeking to non-zero offsets
kmeisthax Oct 25, 2021
0a8c063
Remove implicit coercions in tests so that they don't sign-extend thi…
kmeisthax Oct 31, 2021
e8d3c9f
Add a condition variable for waiting on events, and wait for it to be…
kmeisthax Nov 21, 2021
6ea8dce
Formatting & consistency nits
kmeisthax Nov 21, 2021
1174ec7
Rip out and replace the `AIO_IOCB_` family of constants with a host s…
kmeisthax Nov 21, 2021
399265e
Also rip out the `AIO_IO_EVENT` constants for another host-compiled s…
kmeisthax Nov 21, 2021
6c09ad5
Remove e2e tests dependency on `linux/aio_abi.h` header
kmeisthax Nov 21, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Prev Previous commit
Next Next commit
Half-implement io_submit.
We don't have a way to actually tell an FD to run an async I/O operation yet, so that's still TODO'd.
  • Loading branch information
kmeisthax committed Nov 14, 2021
commit 032d31d957df75b28663d188f07499d97bf622b5
8 changes: 4 additions & 4 deletions fs/aio.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ enum aioctx_event_tag {
AIOCTX_COMPLETE = 2,
};

enum aioctx_op {
enum aioctx_op : uint16_t {
AIOCTX_PREAD = 0,
AIOCTX_PWRITE = 1,
AIOCTX_FSYNC = 2,
Expand All @@ -45,13 +45,13 @@ struct aioctx_event_pending {
fd_t fd;

// A guest memory buffer to read to or write from.
addr_t buf;
uint64_t buf;

// The bounds of the guest memory buffer.
size_t size;
uint64_t nbytes;

// The current guest memory buffer offset.
ssize_t offset;
int64_t offset;
};

// A completed I/O event's information.
Expand Down
84 changes: 81 additions & 3 deletions kernel/aio.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@
#include "kernel/aio.h"
#include "fs/aio.h"

// Guest memory offsets for the IOCB structure.
// Calculated by a test program compiled and ran in iSH itself.
const size_t AIO_IOCB_DATA = 0;
kmeisthax marked this conversation as resolved.
Show resolved Hide resolved
const size_t AIO_IOCB_KEY = 8;
const size_t AIO_IOCB_RW_FLAGS = 12;
const size_t AIO_IOCB_LIO_OPCODE = 16;
const size_t AIO_IOCB_REQPRIO = 18;
const size_t AIO_IOCB_FILDES = 20;
const size_t AIO_IOCB_BUF = 24;
const size_t AIO_IOCB_NBYTES = 32;
const size_t AIO_IOCB_OFFSET = 40;
const size_t AIO_IOCB_RESERVED2 = 48;
const size_t AIO_IOCB_FLAGS = 56;
const size_t AIO_IOCB_RESFD = 60;

dword_t sys_io_setup(dword_t nr_events, addr_t ctx_idp) {
STRACE("io_setup(%d, 0x%x)", nr_events, ctx_idp);

Expand Down Expand Up @@ -44,14 +59,77 @@ dword_t sys_io_getevents(addr_t ctx_id, dword_t min_nr, dword_t nr, addr_t event
return _ENOSYS;
}

dword_t sys_io_submit(addr_t ctx_id, dword_t nr, addr_t iocbpp) {
dword_t sys_io_submit(addr_t ctx_id, dword_t u_nr, addr_t iocbpp) {
sdword_t nr = (sdword_t)u_nr;
STRACE("io_submit(0x%x, %d, 0x%x)", ctx_id, nr, iocbpp);

return _ENOSYS;
if (nr < 0) return _EINVAL;

struct aioctx *ctx = aioctx_table_get_and_retain(current->aioctx, ctx_id);
if (ctx == NULL) return _EINVAL;

sdword_t i;
signed int err;
for (i = 0; i < nr; i += 1) {
addr_t iocbp = 0;
if (user_get(iocbpp + i * sizeof(addr_t), iocbp)) goto fault;

uint64_t user_data = 0;
if (user_get(iocbp + AIO_IOCB_DATA, user_data)) goto fault;

struct aioctx_event_pending host_iocb;
if (user_get(iocbp + AIO_IOCB_LIO_OPCODE, host_iocb.op)) goto fault;
if (user_get(iocbp + AIO_IOCB_FILDES, host_iocb.fd)) goto fault;
if (user_get(iocbp + AIO_IOCB_BUF, host_iocb.buf)) goto fault;
if (user_get(iocbp + AIO_IOCB_NBYTES, host_iocb.nbytes)) goto fault;
if (user_get(iocbp + AIO_IOCB_OFFSET, host_iocb.offset)) goto fault;

lock(&current->files->lock);

struct fd *fdp = fdtable_get(current->files, host_iocb.fd);
if (fdp == NULL) {
unlock(&current->files->lock);

// Linux man pages mention only the FIRST FD is checked (why?).
// TODO: It also doesn't say what happens if further IOCBs are
// unchecked, so I'm assuming it halts IOCB processing at this
// point.
if (i == 0) goto badf;
break;
}

err = aioctx_submit_pending_event(ctx, user_data, host_iocb);
if (err < 0) {
// TODO: This assumes the usual pattern of "first IOCB errors, all
// others stop processing without erroring"
unlock(&current->files->lock);

if (i == 0) goto err;
break;
}

// TODO: Actually submit the event to the FD.
unlock(&current->files->lock);
}

aioctx_release(ctx);
return i;

fault:
aioctx_release(ctx);
return _EFAULT;

badf:
aioctx_release(ctx);
return _EBADF;

err:
aioctx_release(ctx);
return err;
}

dword_t sys_io_cancel(addr_t ctx_id, addr_t iocb, addr_t result) {
STRACE("io_submit(0x%x, 0x%x, 0x%x)", ctx_id, iocb, result);

return _ENOSYS;
}