* Can buffer passed to io_prep_pwrite/io_submit be reused before io_getevents is called?
@ 2018-05-04 17:11 Scott Forman (scforman)
2018-05-07 14:21 ` Jeff Moyer
0 siblings, 1 reply; 2+ messages in thread
From: Scott Forman (scforman) @ 2018-05-04 17:11 UTC (permalink / raw)
To: fio
Hi,
I have an application where I will call io_prep_pwrite and io_submit in a loop, each time passing the same buffer to io_prep_pwrite, but with different contents.
Then after a certain number of loops, I will call io_getevents before going on to the next file.
Is this safe to do, or is there a race condition between the buffer being written to file and being reused for the next call to io_prep_pwrite?
Below is some code to illustrate my question (absent any error handling).
Thanks for any input you can provide.
Scott
int main() {
struct iocb iocb;
struct iocb * iocbs[1];
iocbs[0] = &iocb;
int fd = open("/tmp/test", O_WRONLY | O_CREAT);
io_context_t ctx;
memset(&ctx, 0, sizeof(ctx));
io_setup(10, &ctx);
char msg [128];
int loopCnt = 0;
int fileOffset = 0;
for (; loopCnt < 10; ++loopCnt) {
int len = snprintf(msg, sizeof(msg), "message number %d\n", loopCnt);
io_prep_pwrite(&iocb, fd, (void *)msg, len, fileOffset);
fileOffset += len;
io_submit(ctx, 1, iocbs);
}
struct io_event events[10];
io_getevents(ctx, 10, 10, events, NULL);
close(fd);
io_destroy(ctx);
return 0;
}
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: Can buffer passed to io_prep_pwrite/io_submit be reused before io_getevents is called?
2018-05-04 17:11 Can buffer passed to io_prep_pwrite/io_submit be reused before io_getevents is called? Scott Forman (scforman)
@ 2018-05-07 14:21 ` Jeff Moyer
0 siblings, 0 replies; 2+ messages in thread
From: Jeff Moyer @ 2018-05-07 14:21 UTC (permalink / raw)
To: Scott Forman (scforman); +Cc: fio
Hi, Scott,
I'll note that this question is probably better suited to
linux-aio@vger.kernel.org. Response below.
"Scott Forman (scforman)" <scforman@cisco.com> writes:
> Hi,
>
> I have an application where I will call io_prep_pwrite and io_submit
> in a loop, each time passing the same buffer to io_prep_pwrite, but
> with different contents.
>
> Then after a certain number of loops, I will call io_getevents before
> going on to the next file.
>
> Is this safe to do, or is there a race condition between the buffer
> being written to file and being reused for the next call to
> io_prep_pwrite?
>
> Below is some code to illustrate my question (absent any error
> handling).
>
> Thanks for any input you can provide.
>
> Scott
>
> int main() {
> struct iocb iocb;
> struct iocb * iocbs[1];
> iocbs[0] = &iocb;
>
> int fd = open("/tmp/test", O_WRONLY | O_CREAT);
>
> io_context_t ctx;
> memset(&ctx, 0, sizeof(ctx));
> io_setup(10, &ctx);
>
> char msg [128];
> int loopCnt = 0;
> int fileOffset = 0;
>
> for (; loopCnt < 10; ++loopCnt) {
> int len = snprintf(msg, sizeof(msg), "message number %d\n", loopCnt);
>
> io_prep_pwrite(&iocb, fd, (void *)msg, len, fileOffset);
> fileOffset += len;
>
> io_submit(ctx, 1, iocbs);
> }
>
> struct io_event events[10];
> io_getevents(ctx, 10, 10, events, NULL);
> close(fd);
> io_destroy(ctx);
>
> return 0;
> }
What you did will work today, but probably only by accident (on your
part). Linux native asynchronous I/O is only asynchronous when the file
is opened with O_DIRECT. In the code above, when the io_submit call
returns, the data transfer is complete.
If you had used direct I/O, you would instead be modifying the contents
of a buffer that is queued for I/O (or in the middle of a DMA). That
will result in data corruption.
Cheers,
Jeff
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2018-05-07 14:21 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-05-04 17:11 Can buffer passed to io_prep_pwrite/io_submit be reused before io_getevents is called? Scott Forman (scforman)
2018-05-07 14:21 ` Jeff Moyer
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.