io_cancel return value change
Hi, Kent,
Sorry to bring this up long after your patches have gone in--I am remiss. In the following commit, you changed what was returned by the io_cancel system call:
commit bec68faaf3ba74ed0dcd5dc3a881b30aec542973
Author: Kent Overstreet <[email protected]> Date: Mon May 13 14:45:08 2013 -0700
aio: io_cancel() no longer returns the io_event
Originally, io_event() was documented to return the io_event if cancellation succeeded - the io_event wouldn't be delivered via the ring buffer like it normally would.
But this isn't what the implementation was actually doing; the only driver implementing cancellation, the usb gadget code, never returned an io_event in its cancel function. And aio_complete() was recently changed to no longer suppress event delivery if the kiocb had been cancelled.
This gets rid of the unused io_event argument to kiocb_cancel() and kiocb->ki_cancel(), and changes io_cancel() to return -EINPROGRESS if kiocb->ki_cancel() returned success.
Also tweak the refcounting in kiocb_cancel() to make more sense.
You made a passing reference to previous aio_complete changes as well that I can't easily track down (it wasn't mentioned in the commit log that I can tell, and it isn't obvious to me by looking through the patch set).
At the very least, you need to also include man page updates for this. Honestly, though, we should not have let this patch set go in as is. If any software actually wrote code to handle cancellation, this change would surely cause confusion. That said, I think you got away with this for two reasons:
1) no drivers implements a cancellation routine (except the crazy usb gadgetfs thing)
2) userspace code may not even call io_cancel (who knows?), and if they do, well, see point 1.
My preference would be for this behavior change to be reverted. You could make a case for keeping the change and updating the documentation. I'm not convinced 100% one way or the other. So what do you (and others) think?
Cheers,
Jeff
p.s. I did do a quick google search, and the only caller of io_cancel I could find was a test harness. But, that's certainly not proof that nobody does it!
Quoted 27 lines · Jeff Moyer
Hi, Kent,
Sorry to bring this up long after your patches have gone in--I am remiss. In the following commit, you changed what was returned by the
io_cancelsystem call:commit
bec68faaf3ba74ed0dcd5dc3a881b30aec542973Author: Kent Overstreet <[email protected]> Date: Mon May 13 14:45:08 2013 -0700aio:
io_cancel()no longer returns theio_eventOriginally,
io_event()was documented to return theio_eventif cancellation succeeded - theio_eventwouldn't be delivered via the ring buffer like it normally would.But this isn't what the implementation was actually doing; the only driver implementing cancellation, the usb gadget code, never returned an
io_eventin its cancel function. Andaio_complete()was recently changed to no longer suppress event delivery if the kiocb had been cancelled.This gets rid of the unused
io_eventargument tokiocb_cancel()andkiocb->ki_cancel(), and changesio_cancel()to return-EINPROGRESSifkiocb->ki_cancel()returned success.Also tweak the refcounting in
kiocb_cancel()to make more sense.
Quoted 7 lines
You made a passing reference to previous
aio_completechanges as well that I can't easily track down (it wasn't mentioned in the commit log that I can tell, and it isn't obvious to me by looking through the patch set).At the very least, you need to also include man page updates for this. Honestly, though, we should not have let this patch set go in as is. If
Yes, the man page needs to be updated, but this change in the API is backwards compatible. The existing man page says that it returns 0 on sucess and returns an io_event -- anything other than a 0 return implies that a completion event will be returned later. Cancellation has never provided a 100% guarantee that it will succeed without providing a completion event.
Quoted 7 lines
any software actually wrote code to handle cancellation, this change would surely cause confusion. That said, I think you got away with this for two reasons:
1) no drivers implements a cancellation routine (except the crazy usb gadgetfs thing)
2) userspace code may not even callio_cancel(who knows?), and if they do, well, see point 1.
I implemented some test code to verify that the new approach to cancel works. I have a bunch of other changes against 3.4 that need to be ported to 3.17, but I just haven't had time to work on them. Below are 3 of the patches implementing async pipe reads that need forward porting and should be relatively easy to merge.
Quoted 4 lines
My preference would be for this behavior change to be reverted. You could make a case for keeping the change and updating the documentation. I'm not convinced 100% one way or the other. So what do you (and others) think?
I'm not convinced it needs to be reverted. Anything that's not a file or block device requires cancellation support, and the in-kernel api changes were meant to better handle various race conditions.
Quoted 6 lines
Cheers,
Jeffp.s. I did do a quick google search, and the only caller of
io_cancelI could find was a test harness. But, that's certainly not proof that nobody does it!
You're ignoring the fact that cancellation is used internally to the kernel on process exit or io context destruction. Cancel methods have to work, as otherwise a process will hang at exit.
-ben
Signature · 11 lines
"Thought is the essence of where you are now." commit fff36ea0b18906d0a2254b31b1c2ebf05453ac74 Author: Benjamin LaHaise <[email protected]> Date: Tue Jul 23 13:05:44 2013 -0400 aio/pipe: break out the core __pipe_read() from pipe_read() to prep for aio Move code around from pipe_read() to a helper function __pipe_read() in preparation for adding async read support to pipes.
fs/pipe.c +98 -83
@@ -356,6 +356,103 @@ static const struct pipe_buf_operations packet_pipe_buf_ops = { .get = generic_pipe_buf_get, }; +static int __pipe_read (struct pipe_inode_info *pipe,size_t *total_len, +struct iovec *iov,ssize_t *ret,int *do_wakeup, +struct file *filp) +{ +int bufs; +try_again: + bufs = pipe->nrbufs; +if (bufs) { +int curbuf = pipe->curbuf; +struct pipe_buffer *buf = pipe->bufs + curbuf; +const struct pipe_buf_operations *ops = buf->ops; +void *addr; +size_t chars = buf->len; +int error, atomic; + +if (chars > *total_len) + chars = *total_len; + + error = ops->confirm (pipe, buf); +if (error) { +if (!*ret) + *ret = error; + goto out_break; + } + + atomic = !iov_fault_in_pages_write (iov, chars); +redo: + addr = ops->map (pipe, buf, atomic); + error =pipe_iov_copy_to_user (iov, addr + buf->offset, chars, atomic); + ops->unmap (pipe, buf, addr); +if (unlikely (error)) { +/* +* Just retry with the slow path if we failed. +*/ +if (atomic) { + atomic =0 ; + goto redo; + } +if (!*ret) + *ret = error; + goto out_break; + } + *ret += chars; + buf->offset += chars; + buf->len -= chars; + +/* Was it a packet buffer? Clean up and exit */ +if (buf->flags &PIPE_BUF_FLAG_PACKET ) { + *total_len = chars; + buf->len =0 ; + } + +if (!buf->len) { + buf->ops =NULL ; + ops->release (pipe, buf); + curbuf = (curbuf +1 ) & (pipe->buffers -1 ); + pipe->curbuf = curbuf; + pipe->nrbufs = --bufs; + *do_wakeup =1 ; + } + *total_len -= chars; +if (!*total_len) + goto out_break;/* common path: read succeeded */ + } +if (bufs)/* More to do? */ + goto try_again; +if (!pipe->writers) + goto out_break; +if (!pipe->waiting_writers) { +/* syscall merging: Usually we must not sleep +* if O_NONBLOCK is set, or if we got some data. +* But if a writer sleeps in kernel space, then +* we can wait for that data without violating POSIX. +*/ +if (*ret) + goto out_break; +if (filp->f_flags &O_NONBLOCK ) { + *ret = -EAGAIN ; + goto out_break; + } + } +if (signal_pending (current)) { +if (!*ret) + *ret = -ERESTARTSYS ; + goto out_break; + } +if (*do_wakeup) { +wake_up_interruptible_sync_poll (&pipe->wait,POLLOUT |POLLWRNORM ); +kill_fasync (&pipe->fasync_writers,SIGIO ,POLL_OUT ); + } + +return 0 ; + +out_break: +return 1 ; +} +static ssize_t pipe_read (struct kiocb *iocb,const struct iovec *_iov,unsigned long nr_segs,loff_t pos)
@@ -378,90 +475,8 @@ pipe_read(struct kiocb *iocb, const struct iovec *_iov,mutex_lock (&inode->i_mutex); pipe = inode->i_pipe;for (;;) { -int bufs = pipe->nrbufs; -if (bufs) { -int curbuf = pipe->curbuf; -struct pipe_buffer *buf = pipe->bufs + curbuf; -const struct pipe_buf_operations *ops = buf->ops; -void *addr; -size_t chars = buf->len; -int error, atomic; - -if (chars > total_len) - chars = total_len; - - error = ops->confirm (pipe, buf); -if (error) { -if (!ret) - ret = error; -break ; - } - - atomic = !iov_fault_in_pages_write (iov, chars); -redo: - addr = ops->map (pipe, buf, atomic); - error =pipe_iov_copy_to_user (iov, addr + buf->offset, chars, atomic); - ops->unmap (pipe, buf, addr); -if (unlikely (error)) { -/* -* Just retry with the slow path if we failed. -*/ -if (atomic) { - atomic =0 ; - goto redo; - } -if (!ret) - ret = error; -break ; - } - ret += chars; - buf->offset += chars; - buf->len -= chars; - -/* Was it a packet buffer? Clean up and exit */ -if (buf->flags &PIPE_BUF_FLAG_PACKET ) { - total_len = chars; - buf->len =0 ; - } - -if (!buf->len) { - buf->ops =NULL ; - ops->release (pipe, buf); - curbuf = (curbuf +1 ) & (pipe->buffers -1 ); - pipe->curbuf = curbuf; - pipe->nrbufs = --bufs; - do_wakeup =1 ; - } - total_len -= chars; -if (!total_len) -break ;/* common path: read succeeded */ - } -if (bufs)/* More to do? */ -continue ; -if (!pipe->writers) +if (__pipe_read (pipe, &total_len, iov, &ret, &do_wakeup, filp))break ; -if (!pipe->waiting_writers) { -/* syscall merging: Usually we must not sleep -* if O_NONBLOCK is set, or if we got some data. -* But if a writer sleeps in kernel space, then -* we can wait for that data without violating POSIX. -*/ -if (ret) -break ; -if (filp->f_flags &O_NONBLOCK ) { - ret = -EAGAIN ; -break ; - } - } -if (signal_pending (current)) { -if (!ret) - ret = -ERESTARTSYS ; -break ; - } -if (do_wakeup) { -wake_up_interruptible_sync_poll (&pipe->wait,POLLOUT |POLLWRNORM ); -kill_fasync (&pipe->fasync_writers,SIGIO ,POLL_OUT ); - }pipe_wait (pipe); }mutex_unlock (&inode->i_mutex);
commit 187de6fbe66fd70485d53bb45a7a034ad747a7cb
Author: Benjamin LaHaise <[email protected]> Date: Tue Jul 23 14:57:45 2013 -0400
aio, pipe: implement async pipe read
Add code to pipe_read to implement async reads of pipes without using the threaded aio core. This will allow optimizations to reduce the number of context switches to be made.
fs/pipe.c +89 -0
180 lines not on this page · raw message
include/linux/pipe_fs_i.h +1 -0
@@ -55,6 +55,7 @@ struct pipe_inode_info {struct fasync_struct *fasync_writers;struct inode *inode;struct pipe_buffer *bufs; +struct kiocb *read_iocb; };/*
commit 0ff94f4f7612e2161cacf872565df22758190918
Author: Benjamin LaHaise <[email protected]> Date: Tue Jul 23 15:48:20 2013 -0400
aio/pipe: reduce context switches when async pipe read is in same mm as write
For applications that make use of a pipe internally for waking internal state machines, an async pipe read may occur in the same address space as the write to wake up that pipe. In this case, 2 context switches can be eliminated by completing the async read directly in the pipe writer. On one application making use of this facilty, this results in the number of context switches per second dropping from approximately 35k to 22k.
fs/pipe.c +39 -5
62 lines not on this page · raw message
--
To unsubscribe, send a message with 'unsubscribe linux-aio' in the body to [email protected]. For more info on Linux AIO,
see: http://www.kvack.org/aio/
Don't email: <a href=mailto:"[email protected]">[email protected]</a>
Quoted 9 lines · Benjamin LaHaise
Quoted 2 lines
At the very least, you need to also include man page updates for this. Honestly, though, we should not have let this patch set go in as is. If
Yes, the man page needs to be updated, but this change in the API is backwards compatible. The existing man page says that it returns 0 on sucess and returns an
io_event-- anything other than a 0 return implies that a completion event will be returned later. Cancellation has never provided a 100% guarantee that it will succeed without providing a completion event.
The man page states:
If the AIO context is found, the event will be canceled and then copied into the memory pointed to by result without being placed into the completion queue.
That sounds pretty definitive to me.
Quoted 10 lines
Quoted 7 lines
any software actually wrote code to handle cancellation, this change would surely cause confusion. That said, I think you got away with this for two reasons:
1) no drivers implements a cancellation routine (except the crazy usb gadgetfs thing)
2) userspace code may not even callio_cancel(who knows?), and if they do, well, see point 1.I implemented some test code to verify that the new approach to cancel works.
I have no doubt it works, Ben. :) My main concern is a change to the kernel<->user ABI.
Quoted 8 lines
Quoted 4 lines
My preference would be for this behavior change to be reverted. You could make a case for keeping the change and updating the documentation. I'm not convinced 100% one way or the other. So what do you (and others) think?
I'm not convinced it needs to be reverted. Anything that's not a file or block device requires cancellation support, and the in-kernel api changes were meant to better handle various race conditions.
I don't understand why you're pitting this change against working cancelation support. They seem to be separate issues to me.
Quoted 7 lines
Quoted 3 lines
p.s. I did do a quick google search, and the only caller of
io_cancelI could find was a test harness. But, that's certainly not proof that nobody does it!You're ignoring the fact that cancellation is used internally to the kernel on process exit or io context destruction. Cancel methods have to work, as otherwise a process will hang at exit.
I was not ignoring that at all. We don't provide backwards compatibility for kernel modules, and the modules in-tree are changed when interfaces change. I'm really only concerned about whether we think it matters that the kernel<->user ABI was changed.
I think at some level we're talking past each other. Hopefully I've clarified my questions.
Cheers,
Jeff
Quoted 12 lines · Jeff Moyer
Quoted 9 lines · Benjamin LaHaise
Quoted 2 lines
At the very least, you need to also include man page updates for this. Honestly, though, we should not have let this patch set go in as is. If
Yes, the man page needs to be updated, but this change in the API is backwards compatible. The existing man page says that it returns 0 on sucess and returns an
io_event-- anything other than a 0 return implies that a completion event will be returned later. Cancellation has never provided a 100% guarantee that it will succeed without providing a completion event.
Quoted 7 lines
The man page states:
If the AIO context is found, the event will be canceled and then copied into the memory pointed to by result without being placed into the completion queue.
That sounds pretty definitive to me.
Cancellation is inherently racy. It is and always has been possible for an attempt at cancellation to fail, or for the attempt at cancellation to suceed in the bigger picture yet have the io_cancel() call fail (and cause an event to be delivered). Applications always have and always will have to handle an io_cancel() call potentially failing with the kernel delivering a completion event. You have to read the io_cancel() man page with that understanding in place.
Quoted 2 lines
I have no doubt it works, Ben. :) My main concern is a change to the kernel<->user ABI.
As I said above: an application always has and always will have to handle the case where an io_cancel() returns failure. An application that does handle an error return from io_cancel() is broken, as the condition can occur when the cancel races with the kernel calling aio_complete().
Quoted 4 lines
I was not ignoring that at all. We don't provide backwards compatibility for kernel modules, and the modules in-tree are changed when interfaces change. I'm really only concerned about whether we think it matters that the kernel<->user ABI was changed.
And this change is ABI compatible. Applications aren't being made to handle any failures they haven't already had to cope with. If an application can't handle a transient io_cancel() failure, then the app is buggy and needs to be fixed since that condition can already occur when the cancel races with completion.
-ben
Quoted 5 lines
I think at some level we're talking past each other. Hopefully I've clarified my questions.
Cheers,
Jeff
Signature · 7 lines
"Thought is the essence of where you are now." -- To unsubscribe, send a message with 'unsubscribe linux-aio' in the body to [email protected]. For more info on Linux AIO, see: http://www.kvack.org/aio/ Don't email: <a href=mailto:"[email protected]">[email protected]</a>
Quoted 7 lines · Benjamin LaHaise
Cancellation is inherently racy. It is and always has been possible for an attempt at cancellation to fail, or for the attempt at cancellation to suceed in the bigger picture yet have the
io_cancel()call fail (and cause an event to be delivered). Applications always have and always will have to handle anio_cancel()call potentially failing with the kernel delivering a completion event. You have to read theio_cancel()man page with that understanding in place.
Sorry, Ben, Zach just pointed out what I was so obviously missing. From the changelog:
This gets rid of the unused io_event argument to kiocb_cancel() and kiocb->ki_cancel(), and changes io_cancel() to return -EINPROGRESS if kiocb->ki_cancel() returned success.
Somehow I managed to skip right over that change to return -EINPROGRESS. *facepalm*
Cheers,
Jeff