diff mbox

[RFC,4/9] linux-aio: Implement .cancel_async

Message ID 1408622216-9578-5-git-send-email-famz@redhat.com
State New
Headers show

Commit Message

Fam Zheng Aug. 21, 2014, 11:56 a.m. UTC
Just call io_cancel (2), if it fails, it means the request is not
canceled, so the event loop will eventually call
qemu_laio_process_completion.

In qemu_laio_process_completion, change to call the cb unconditionally.
It is required by .cancel_async, and also acceptable by .cancel.

Signed-off-by: Fam Zheng <famz@redhat.com>
---
 block/linux-aio.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

Comments

Stefan Hajnoczi Aug. 21, 2014, 4:31 p.m. UTC | #1
On Thu, Aug 21, 2014 at 07:56:51PM +0800, Fam Zheng wrote:
> @@ -110,6 +109,22 @@ static void qemu_laio_completion_cb(EventNotifier *e)
>      }
>  }
>  
> +static void laio_cancel_async(BlockDriverAIOCB *blockacb)
> +{
> +    struct qemu_laiocb *laiocb = (struct qemu_laiocb *)blockacb;
> +    struct io_event event;
> +    int ret;
> +
> +    ret = io_cancel(laiocb->ctx->ctx, &laiocb->iocb, &event);
> +    laiocb->ret = -ECANCELED;
> +    if (!ret) {
> +        /* iocb is not cancelled, cb will be called by the event loop later */
> +        return;
> +    }

No callback will be invoked if io_cancel(2) every cancels the request
immediately.

The current kernel implementation always returns -EINPROGRESS or some of
other error value.  But some day it might return 0 and this would leak
the request!

> +
> +    laiocb->common.cb(laiocb->common.opaque, laiocb->ret);
> +}

It would be cleaner to reuse laio_cancel_async() from laio_cancel() to
avoid code duplication.  For example, there is a useful comment in
laio_cancel() explaining that io_cancel(2) doesn't cancel I/O in
practice on 2.6.31 era kernels.
Fam Zheng Aug. 22, 2014, 4:56 a.m. UTC | #2
On Thu, 08/21 17:31, Stefan Hajnoczi wrote:
> On Thu, Aug 21, 2014 at 07:56:51PM +0800, Fam Zheng wrote:
> > @@ -110,6 +109,22 @@ static void qemu_laio_completion_cb(EventNotifier *e)
> >      }
> >  }
> >  
> > +static void laio_cancel_async(BlockDriverAIOCB *blockacb)
> > +{
> > +    struct qemu_laiocb *laiocb = (struct qemu_laiocb *)blockacb;
> > +    struct io_event event;
> > +    int ret;
> > +
> > +    ret = io_cancel(laiocb->ctx->ctx, &laiocb->iocb, &event);
> > +    laiocb->ret = -ECANCELED;
> > +    if (!ret) {
> > +        /* iocb is not cancelled, cb will be called by the event loop later */
> > +        return;
> > +    }
> 
> No callback will be invoked if io_cancel(2) every cancels the request
> immediately.
> 
> The current kernel implementation always returns -EINPROGRESS or some of
> other error value.  But some day it might return 0 and this would leak
> the request!
> 
> > +
> > +    laiocb->common.cb(laiocb->common.opaque, laiocb->ret);
> > +}
> 
> It would be cleaner to reuse laio_cancel_async() from laio_cancel() to
> avoid code duplication.  For example, there is a useful comment in
> laio_cancel() explaining that io_cancel(2) doesn't cancel I/O in
> practice on 2.6.31 era kernels.

I'll take a closer look at it.

Fam
diff mbox

Patch

diff --git a/block/linux-aio.c b/block/linux-aio.c
index 7ac7e8c..adf3b2e 100644
--- a/block/linux-aio.c
+++ b/block/linux-aio.c
@@ -79,9 +79,8 @@  static void qemu_laio_process_completion(struct qemu_laio_state *s,
                 ret = -EINVAL;
             }
         }
-
-        laiocb->common.cb(laiocb->common.opaque, ret);
     }
+    laiocb->common.cb(laiocb->common.opaque, ret);
 
     qemu_aio_release(laiocb);
 }
@@ -110,6 +109,22 @@  static void qemu_laio_completion_cb(EventNotifier *e)
     }
 }
 
+static void laio_cancel_async(BlockDriverAIOCB *blockacb)
+{
+    struct qemu_laiocb *laiocb = (struct qemu_laiocb *)blockacb;
+    struct io_event event;
+    int ret;
+
+    ret = io_cancel(laiocb->ctx->ctx, &laiocb->iocb, &event);
+    laiocb->ret = -ECANCELED;
+    if (!ret) {
+        /* iocb is not cancelled, cb will be called by the event loop later */
+        return;
+    }
+
+    laiocb->common.cb(laiocb->common.opaque, laiocb->ret);
+}
+
 static void laio_cancel(BlockDriverAIOCB *blockacb)
 {
     struct qemu_laiocb *laiocb = (struct qemu_laiocb *)blockacb;
@@ -145,6 +160,7 @@  static void laio_cancel(BlockDriverAIOCB *blockacb)
 static const AIOCBInfo laio_aiocb_info = {
     .aiocb_size         = sizeof(struct qemu_laiocb),
     .cancel             = laio_cancel,
+    .cancel_async       = laio_cancel_async,
 };
 
 static void ioq_init(LaioQueue *io_q)